m_chgboth / m_chgident.c

/*
 * Copyright (c) 2005 William Pitcock <nenolod -at- nenolod.net>
 * and Jilles Tjoelker <jilles -at- stack.nl>
 * All rights reserved.
 *
 * Redistribution in both source and binary forms are permitted
 * provided that the above copyright notice remains unchanged.
 *
 * m_chgident.c: A module to change ident (/username) dynamically
 * 
 * Based heavily on m_chghost.c from the Charybdis IRCd
 */

#include "stdinc.h"
#include "send.h"
#include "channel.h"
#include "client.h"
#include "common.h"
#include "config.h"
#include "ircd.h"
#include "numeric.h"
#include "s_conf.h"
#include "s_newconf.h"
#include "s_serv.h"
#include "s_user.h"
#include "hash.h"
#include "msg.h"
#include "parse.h"
#include "modules.h"
#include "whowas.h"
#include "monitor.h"

static int ms_chgident(struct Client *, struct Client *, int, const char **);
static int me_chgident(struct Client *, struct Client *, int, const char **);
static int mo_chgident(struct Client *, struct Client *, int, const char **);

struct Message chgident_msgtab = {
	"CHGIDENT", 0, 0, 0, MFLG_SLOW,
	{mg_ignore, mg_not_oper, {ms_chgident, 3}, {ms_chgident, 3}, {me_chgident, 3}, {mo_chgident, 3}}
};

mapi_clist_av1 chgident_clist[] = { &chgident_msgtab, NULL };

DECLARE_MODULE_AV1(chgident, NULL, NULL, chgident_clist, NULL, NULL, "$Revision: 3424 $");

/* clean_host()
 *
 * input	- host to check
 * output	- 0 if erroneous, else 0
 * side effects -
 */
static int
clean_ident(const char *host)
{
	int len = 0;
	const char *last_slash = 0;
	
	if (*host == '\0' || *host == ':')
		return 0;

	for(; *host; host++)
	{
		len++;

		if(!IsHostChar(*host))
			return 0;
		if(*host == '/')
			last_slash = host;
	}

	if(len > HOSTLEN)
		return 0;

	if(last_slash && IsDigit(last_slash[1]))
		return 0;

	return 1;
}

static int
do_chgident(struct Client *source_p, struct Client *target_p,
		const char *newhost, int is_encap)
{
	if (!clean_ident(newhost))
	{
		sendto_realops_snomask(SNO_GENERAL, is_encap ? L_ALL : L_NETWIDE, "%s attempted to change ident for %s to %s (invalid)",
				IsServer(source_p) ? source_p->name : get_oper_name(source_p),
				target_p->name, newhost);
		return 0;
	}
	change_nick_user_host(target_p, target_p->name, newhost, target_p->host, 0, "Changing ident");
	if (MyClient(source_p))
		sendto_one_notice(source_p, ":Changed ident for %s to %s", target_p->name, newhost);
	if (!IsServer(source_p) && !IsService(source_p))
		sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s changed ident for %s to %s", get_oper_name(source_p), target_p->name, newhost);
	return 1;
}

/*
 * ms_chghost
 * parv[1] = target
 * parv[2] = host
 */
static int
ms_chgident(struct Client *client_p, struct Client *source_p,
	int parc, const char *parv[])
{
	struct Client *target_p;

	if (!(target_p = find_person(parv[1])))
		return -1;

	if (do_chgident(source_p, target_p, parv[2], 0))
	{
		sendto_server(client_p, NULL,
			CAP_EUID | CAP_TS6, NOCAPS, ":%s CHGIDENT %s %s",
			use_id(source_p), use_id(target_p), parv[2]);
		sendto_server(client_p, NULL,
			CAP_TS6, CAP_EUID, ":%s ENCAP * CHGIDENT %s :%s",
			use_id(source_p), use_id(target_p), parv[2]);
	}

	return 0;
}

/*
 * me_chghost
 * parv[1] = target
 * parv[2] = host
 */
static int
me_chgident(struct Client *client_p, struct Client *source_p,
	int parc, const char *parv[])
{
	struct Client *target_p;

	if (!(target_p = find_person(parv[1])))
		return -1;

	do_chgident(source_p, target_p, parv[2], 1);

	return 0;
}

/*
 * mo_chghost
 * parv[1] = target
 * parv[2] = host
 */
/* Disable this because of the abuse potential -- jilles
 * No, make it toggleable via ./configure. --nenolod
 */
static int
mo_chgident(struct Client *client_p, struct Client *source_p,
	int parc, const char *parv[])
{
	struct Client *target_p;

	if(!IsOperAdmin(source_p))
	{
		sendto_one(source_p, form_str(ERR_NOPRIVS),
			   me.name, source_p->name, "admin");
		return 0;
	}

	if (!(target_p = find_named_person(parv[1])))
	{
		sendto_one_numeric(source_p, ERR_NOSUCHNICK,
				form_str(ERR_NOSUCHNICK), parv[1]);
		return 0;
	}

	if(IsService(target_p))
	{
		sendto_one_notice(source_p, ":%s is part of services", parv[1]);
		return 0;
	}

	if (!clean_ident(parv[2]))
	{
		sendto_one_notice(source_p, ":Ident %s is invalid", parv[2]);
		return 0;
	}

	do_chgident(source_p, target_p, parv[2], 0);

	sendto_server(NULL, NULL,
		CAP_EUID | CAP_TS6, NOCAPS, ":%s CHGIDENT %s %s",
		use_id(source_p), use_id(target_p), parv[2]);
	sendto_server(NULL, NULL,
		CAP_TS6, CAP_EUID, ":%s ENCAP * CHGIDENT %s :%s",
		use_id(source_p), use_id(target_p), parv[2]);

	return 0;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.