From a1479a87757fb5e9b48f3c8bb5dd9cc247406f1a Mon Sep 17 00:00:00 2001 From: "rob rob@31f1291d-b8d6-0310-a050-a5561fc1590b" Date: Mon, 19 Jul 2004 14:08:30 +0000 Subject: BUILD : 1.7.4 (264) BUGS : N/A NOTES : Switched to autoconf - try to commit part 1 git-svn-id: svn://svn.anope.org/anope/trunk@264 31f1291d-b8d6-0310-a050-a5561fc1590b git-svn-id: http://anope.svn.sourceforge.net/svnroot/anope/trunk@169 5417fbe8-f217-4b02-8779-1006273d7864 --- src/operserv.c | 5064 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 5064 insertions(+) create mode 100644 src/operserv.c (limited to 'src/operserv.c') diff --git a/src/operserv.c b/src/operserv.c new file mode 100644 index 000000000..e070b55b0 --- /dev/null +++ b/src/operserv.c @@ -0,0 +1,5064 @@ +/* OperServ functions. + * + * (C) 2003 Anope Team + * Contact us at info@anope.org + * + * Please read COPYING and README for furhter details. + * + * Based on the original code of Epona by Lara. + * Based on the original code of Services by Andy Church. + * + * $Id$ + * + */ + +#include "services.h" +#include "pseudo.h" + +extern Module *mod_current_module; +extern int mod_current_op; +extern User *mod_current_user; +extern ModuleHash *MODULE_HASH[MAX_CMD_HASH]; +/*************************************************************************/ + +struct clone { + char *host; + long time; +}; + +/* List of most recent users - statically initialized to zeros */ +static struct clone clonelist[CLONE_DETECT_SIZE]; + +/* Which hosts have we warned about, and when? This is used to keep us + * from sending out notices over and over for clones from the same host. */ +static struct clone warnings[CLONE_DETECT_SIZE]; + +/* List of Services administrators */ +SList servadmins; +/* List of Services operators */ +SList servopers; +/* AKILL, SGLINE, SQLINE and SZLINE lists */ +SList akills, sglines, sqlines, szlines; + +/*************************************************************************/ + +static void get_operserv_stats(long *nrec, long *memuse); + +static int compare_adminlist_entries(SList * slist, void *item1, + void *item2); +static int compare_operlist_entries(SList * slist, void *item1, + void *item2); +static void free_adminlist_entry(SList * slist, void *item); +static void free_operlist_entry(SList * slist, void *item); + +static int is_akill_entry_equal(SList * slist, void *item1, void *item2); +static void free_akill_entry(SList * slist, void *item); +#ifdef IRC_BAHAMUT +static int is_sgline_entry_equal(SList * slist, void *item1, void *item2); +static void free_sgline_entry(SList * slist, void *item); +#endif +static int is_sqline_entry_equal(SList * slist, void *item1, void *item2); +static void free_sqline_entry(SList * slist, void *item); +#ifdef IRC_BAHAMUT +static int is_szline_entry_equal(SList * slist, void *item1, void *item2); +static void free_szline_entry(SList * slist, void *item); +#endif + +static int do_help(User * u); +static int do_global(User * u); +static int do_stats(User * u); +static int do_admin(User * u); +static int do_oper(User * u); +static int do_os_mode(User * u); +static int do_clearmodes(User * u); +static int do_os_kick(User * u); +static int do_akill(User * u); +static int do_sgline(User * u); +static int do_sqline(User * u); +static int do_szline(User * u); +static int do_set(User * u); +static int do_noop(User * u); +static int do_jupe(User * u); +static int do_raw(User * u); +static int do_update(User * u); +static int do_reload(User * u); +static int do_os_quit(User * u); +static int do_shutdown(User * u); +static int do_restart(User * u); +static int do_ignorelist(User * u); +static int do_clearignore(User * u); +static int do_killclones(User * u); +static int do_chanlist(User * u); +static int do_userlist(User * u); +static int do_ignoreuser(User * u); +static int do_staff(User * u); +static int do_defcon(User * u); +static int do_chankill(User * u); +static void defcon_sendlvls(User * u); +char *defconReverseModes(const char *modes); +int DefConModesSet = 0; +time_t DefContimer; +void runDefCon(void); +void resetDefCon(int level); +void oper_global(char *nick, char *fmt, ...); + +#ifdef USE_MODULES +int do_modload(User * u); +int do_modunload(User * u); +int do_modlist(User * u); +int do_modinfo(User * u); +static int showModuleCmdLoaded(CommandHash * cmdList, char *mod_name, + User * u); +static int showModuleMsgLoaded(MessageHash * msgList, char *mod_name, + User * u); +#endif + + +#ifdef USE_OSSVS +#ifndef IRC_HYBRID +static int do_operumodes(User * u); +#endif +static int do_svsnick(User * u); +#endif + +#if defined(IRC_UNREAL) && defined(USE_OSSVS) +static int do_operoline(User * u); +#endif + +#ifdef DEBUG_COMMANDS +static void send_clone_lists(User * u); +static int do_matchwild(User * u); +#endif + +/* OperServ restart needs access to this if were gonna avoid sending ourself a signal */ +extern int do_restart_services(void); +void moduleAddOperServCmds(void); +/*************************************************************************/ + +/* Options for the lists */ +SListOpts akopts = { 0, NULL, &is_akill_entry_equal, &free_akill_entry }; +SListOpts saopts = { SLISTF_SORT, &compare_adminlist_entries, NULL, + &free_adminlist_entry +}; + +#ifdef IRC_BAHAMUT +SListOpts sgopts = { 0, NULL, &is_sgline_entry_equal, &free_sgline_entry }; +#endif +SListOpts soopts = + { SLISTF_SORT, &compare_operlist_entries, NULL, &free_operlist_entry }; +SListOpts sqopts = + { SLISTF_SORT, NULL, &is_sqline_entry_equal, &free_sqline_entry }; +#ifdef IRC_BAHAMUT +SListOpts szopts = { 0, NULL, &is_szline_entry_equal, &free_szline_entry }; +#endif + +/*************************************************************************/ +/* *INDENT-OFF* */ +void moduleAddOperServCmds(void) { + Command *c; + c = createCommand("HELP", do_help, NULL, -1, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); + c = createCommand("GLOBAL", do_global, NULL, OPER_HELP_GLOBAL, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); + c = createCommand("STATS", do_stats, NULL, OPER_HELP_STATS, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); + c = createCommand("UPTIME", do_stats, NULL, OPER_HELP_STATS, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); + + /* Anyone can use the LIST option to the ADMIN and OPER commands; those + * routines check privileges to ensure that only authorized users + * modify the list. */ + c = createCommand("ADMIN", do_admin, NULL, OPER_HELP_ADMIN, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); + c = createCommand("OPER", do_oper, NULL, OPER_HELP_OPER, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); + c = createCommand("STAFF", do_staff, NULL, OPER_HELP_STAFF, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); + /* Similarly, anyone can use *NEWS LIST, but *NEWS {ADD,DEL} are + * reserved for Services admins. */ + c = createCommand("LOGONNEWS", do_logonnews, NULL, NEWS_HELP_LOGON, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); + c = createCommand("OPERNEWS", do_opernews, NULL, NEWS_HELP_OPER, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); + c = createCommand("RANDOMNEWS", do_randomnews, NULL, NEWS_HELP_RANDOM, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); + + /* Commands for Services opers: */ + c = createCommand("MODE", do_os_mode, is_services_oper,OPER_HELP_MODE, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); + c = createCommand("CLEARMODES", do_clearmodes, is_services_oper,OPER_HELP_CLEARMODES, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); + c = createCommand("KICK", do_os_kick, is_services_oper,OPER_HELP_KICK, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); + c = createCommand("KILLCLONES", do_killclones, is_services_oper,OPER_HELP_KILLCLONES, -1,-1,-1, -1); addCoreCommand(OPERSERV,c); + c = createCommand("AKILL", do_akill, is_services_oper,OPER_HELP_AKILL, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); + c = createCommand("SGLINE", do_sgline, is_services_oper,OPER_HELP_SGLINE, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); + c = createCommand("SQLINE", do_sqline, is_services_oper,OPER_HELP_SQLINE, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); + c = createCommand("SZLINE", do_szline, is_services_oper,OPER_HELP_SZLINE, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); + + /* Commands for Services admins: */ + c = createCommand("SET", do_set, is_services_admin,OPER_HELP_SET, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); + c = createCommand("SET READONLY", NULL, NULL,OPER_HELP_SET_READONLY, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); + c = createCommand("SET LOGCHAN",NULL, NULL,OPER_HELP_SET_LOGCHAN, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); + c = createCommand("SET DEBUG", NULL, NULL,OPER_HELP_SET_DEBUG, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); + c = createCommand("SET NOEXPIRE",NULL, NULL,OPER_HELP_SET_NOEXPIRE, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); + c = createCommand("SET SUPERADMIN",NULL, NULL,OPER_HELP_SET_SUPERADMIN, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); +#ifdef USE_OSSVS + c = createCommand("SVSNICK", do_svsnick, is_services_admin,OPER_HELP_SVSNICK, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); +#ifndef IRC_HYBRID + c = createCommand("UMODE", do_operumodes, is_services_admin,OPER_HELP_UMODE, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); +#endif +#endif + c = createCommand("NOOP", do_noop, is_services_admin,OPER_HELP_NOOP, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); + c = createCommand("JUPE", do_jupe, is_services_admin,OPER_HELP_JUPE, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); + c = createCommand("RAW", do_raw, is_services_admin,OPER_HELP_RAW, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); + c = createCommand("IGNORE", do_ignoreuser, is_services_admin,OPER_HELP_IGNORE, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); +#if defined(IRC_UNREAL) && defined(USE_OSSVS) + c = createCommand("OLINE", do_operoline, is_services_admin,OPER_HELP_OLINE, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); +#endif + c = createCommand("UPDATE", do_update, is_services_admin,OPER_HELP_UPDATE, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); + c = createCommand("RELOAD", do_reload, is_services_admin,OPER_HELP_RELOAD, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); + c = createCommand("QUIT", do_os_quit, is_services_admin,OPER_HELP_QUIT, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); + c = createCommand("SHUTDOWN", do_shutdown, is_services_admin,OPER_HELP_SHUTDOWN, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); + c = createCommand("RESTART", do_restart, is_services_admin,OPER_HELP_RESTART, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); +#ifndef STREAMLINED + c = createCommand("SESSION", do_session, is_services_admin,OPER_HELP_SESSION, -1,-1,-1, -1); addCoreCommand(OPERSERV,c); + c = createCommand("EXCEPTION", do_exception, is_services_admin,OPER_HELP_EXCEPTION, -1,-1,-1, -1); addCoreCommand(OPERSERV,c); +#endif + c = createCommand("CHANLIST", do_chanlist, is_services_admin,OPER_HELP_CHANLIST, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); + c = createCommand("USERLIST", do_userlist, is_services_admin,OPER_HELP_USERLIST, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); + c = createCommand("CACHE", do_cache, is_services_admin,OPER_HELP_CACHE, -1,-1,-1,-1); addCoreCommand(OPERSERV,c); + c = createCommand("DEFCON", do_defcon, is_services_admin, OPER_HELP_DEFCON,-1,-1,-1,-1); addCoreCommand(OPERSERV,c); + c = createCommand("CHANKILL", do_chankill, is_services_admin, OPER_HELP_CHANKILL,-1,-1,-1,-1); addCoreCommand(OPERSERV,c); + /* Commands for Services root: */ +#ifdef USE_MODULES + c = createCommand("MODLOAD", do_modload, is_services_root, -1,-1,-1,-1,OPER_HELP_MODLOAD); addCoreCommand(OPERSERV,c); + c = createCommand("MODUNLOAD", do_modunload, is_services_root, -1,-1,-1,-1,OPER_HELP_MODUNLOAD); addCoreCommand(OPERSERV,c); + c = createCommand("MODLIST", do_modlist, is_services_root, -1,-1,-1,-1,OPER_HELP_MODLIST); addCoreCommand(OPERSERV,c); + c = createCommand("MODINFO", do_modinfo, is_services_root, -1,-1,-1,-1,OPER_HELP_MODINFO); addCoreCommand(OPERSERV,c); +#endif +#ifdef DEBUG_COMMANDS + c = createCommand("LISTTIMERS", send_timeout_list, is_services_root, -1,-1,-1,-1,-1); addCoreCommand(OPERSERV,c); + c = createCommand("MATCHWILD", do_matchwild, is_services_root, -1,-1,-1,-1,-1); addCoreCommand(OPERSERV,c); + c = createCommand("LISTCLONES", send_clone_lists, is_services_root, -1,-1,-1,-1,-1); addCoreCommand(OPERSERV,c); +#endif +} + +/* *INDENT-ON* */ +/*************************************************************************/ +/*************************************************************************/ + +/* OperServ initialization. */ + +void os_init(void) +{ + Command *cmd; + moduleAddOperServCmds(); + cmd = findCommand(OPERSERV, "GLOBAL"); + if (cmd) + cmd->help_param1 = s_GlobalNoticer; + cmd = findCommand(OPERSERV, "ADMIN"); + if (cmd) + cmd->help_param1 = s_NickServ; + cmd = findCommand(OPERSERV, "OPER"); + if (cmd) + cmd->help_param1 = s_NickServ; + + /* Initialization of the lists */ + slist_init(&servadmins); + servadmins.opts = &saopts; + slist_init(&servopers); + servopers.opts = &soopts; + + slist_init(&akills); + akills.opts = &akopts; + slist_init(&sglines); +#ifdef IRC_BAHAMUT + sglines.opts = &sgopts; +#endif + slist_init(&sqlines); + sqlines.opts = &sqopts; + slist_init(&szlines); +#ifdef IRC_BAHAMUT + szlines.opts = &szopts; +#endif +} + +/*************************************************************************/ + +/* Main OperServ routine. */ + +void operserv(User * u, char *buf) +{ + char *cmd; + char *s; + + alog("%s: %s: %s", s_OperServ, u->nick, buf); + + cmd = strtok(buf, " "); + if (!cmd) { + return; + } else if (stricmp(cmd, "\1PING") == 0) { + if (!(s = strtok(NULL, ""))) + s = "\1"; + notice(s_OperServ, u->nick, "\1PING %s", s); + } else { + mod_run_cmd(s_OperServ, u, OPERSERV, cmd); + } +} + +static void get_operserv_stats(long *nrec, long *memuse) +{ + int i; + long mem = 0, count = 0, mem2 = 0, count2 = 0; + Akill *ak; + SXLine *sx; + + if (CheckClones) { + mem = sizeof(struct clone) * CLONE_DETECT_SIZE * 2; + for (i = 0; i < CLONE_DETECT_SIZE; i++) { + if (clonelist[i].host) { + count++; + mem += strlen(clonelist[i].host) + 1; + } + if (warnings[i].host) { + count++; + mem += strlen(warnings[i].host) + 1; + } + } + } + + count += akills.count; + mem += akills.capacity; + mem += akills.count * sizeof(Akill); + + for (i = 0; i < akills.count; i++) { + ak = akills.list[i]; + mem += strlen(ak->user) + 1; + mem += strlen(ak->host) + 1; + mem += strlen(ak->by) + 1; + mem += strlen(ak->reason) + 1; + } + +#ifdef IRC_BAHAMUT + + count += sglines.count; + mem += sglines.capacity; + mem += sglines.count * sizeof(SXLine); + + for (i = 0; i < sglines.count; i++) { + sx = sglines.list[i]; + mem += strlen(sx->mask) + 1; + mem += strlen(sx->by) + 1; + mem += strlen(sx->reason) + 1; + } + +#endif + + count += sqlines.count; + mem += sqlines.capacity; + mem += sqlines.count * sizeof(SXLine); + + for (i = 0; i < sqlines.count; i++) { + sx = sqlines.list[i]; + mem += strlen(sx->mask) + 1; + mem += strlen(sx->by) + 1; + mem += strlen(sx->reason) + 1; + } + +#ifdef IRC_BAHAMUT + + count += szlines.count; + mem += szlines.capacity; + mem += szlines.count * sizeof(SXLine); + + for (i = 0; i < szlines.count; i++) { + sx = szlines.list[i]; + mem += strlen(sx->mask) + 1; + mem += strlen(sx->by) + 1; + mem += strlen(sx->reason) + 1; + } + +#endif + + get_news_stats(&count2, &mem2); + count += count2; + mem += mem2; + get_exception_stats(&count2, &mem2); + count += count2; + mem += mem2; + + *nrec = count; + *memuse = mem; +} + +/*************************************************************************/ +/**************************** Privilege checks ***************************/ +/*************************************************************************/ + +/* Load old AKILL data. */ + +#define SAFE(x) do { \ + if ((x) < 0) { \ + if (!forceload) \ + fatal("Read error on %s", AutokillDBName); \ + break; \ + } \ +} while (0) + +static void load_old_akill(void) +{ + dbFILE *f; + int i, j; + int16 tmp16; + int32 tmp32; + char buf[NICKMAX], mask2[BUFSIZE], *mask, *s; + Akill *ak, *entry; + + if (! + (f = + open_db("AKILL", AutokillDBName ? AutokillDBName : "akill.db", + "r", 9))) + return; + + get_file_version(f); + + read_int16(&tmp16, f); + slist_setcapacity(&akills, tmp16); + + for (j = 0; j < akills.capacity; j++) { + ak = scalloc(sizeof(Akill), 1); + + SAFE(read_string(&mask, f)); + s = strchr(mask, '@'); + *s = 0; + s++; + ak->user = sstrdup(mask); + ak->host = sstrdup(s); + SAFE(read_string(&ak->reason, f)); + SAFE(read_buffer(buf, f)); + if (!*buf) + ak->by = sstrdup(""); + else + ak->by = sstrdup(buf); + SAFE(read_int32(&tmp32, f)); + ak->seton = tmp32 ? tmp32 : time(NULL); + SAFE(read_int32(&tmp32, f)); + ak->expires = tmp32; + + /* Sanity checks *sigh* */ + + /* No nicknames allowed! */ + if (strchr(ak->user, '!')) { + s_rakill(ak->user, ak->host); + free(ak); + continue; + } + + snprintf(mask2, sizeof(mask2), "%s@%s", ak->user, ak->host); + + /* Is the mask already in the AKILL list? */ + if (slist_indexof(&akills, mask2) != -1) { + free(ak); + continue; + } + + /* Checks whether there is an AKILL that already covers + * the one we want to add, and whether there are AKILLs + * that would be covered by this one. Expiry time + * does *also* matter. + */ + + if (akills.count > 0) { + + for (i = akills.count - 1; i >= 0; i--) { + + char amask[BUFSIZE]; + + entry = akills.list[i]; + + if (!entry) + continue; + + snprintf(amask, sizeof(amask), "%s@%s", entry->user, + entry->host); + + if (match_wild_nocase(amask, mask2) + && (entry->expires >= ak->expires + || entry->expires == 0)) { + s_rakill(ak->user, ak->host); + free(ak); + ak = NULL; + break; + } + + if (match_wild_nocase(mask2, amask) + && (entry->expires <= ak->expires || ak->expires == 0)) + slist_delete(&akills, i); + } + + } + + if (ak) + slist_add(&akills, ak); + } + + close_db(f); +} + +#undef SAFE + +/* Load OperServ data. */ + +#define SAFE(x) do { \ + if ((x) < 0) { \ + if (!forceload) \ + fatal("Read error on %s", OperDBName); \ + failed = 1; \ + break; \ + } \ +} while (0) + +void load_os_dbase(void) +{ + dbFILE *f; + int16 i, n, ver, c; + HostCache *hc, **hclast, *hcprev; + int16 tmp16; + int32 tmp32; + char *s; + int failed = 0; + + if (!(f = open_db(s_OperServ, OperDBName, "r", OPER_VERSION))) + return; + + ver = get_file_version(f); + + if (ver <= 9) { + NickAlias *na; + + SAFE(read_int16(&n, f)); + for (i = 0; i < n && !failed; i++) { + SAFE(read_string(&s, f)); + if (s) { + na = findnick(s); + if (na) { + na->nc->flags |= NI_SERVICES_ADMIN; + if (slist_indexof(&servadmins, na) == -1) + slist_add(&servadmins, na); + } + free(s); + } + } + if (!failed) + SAFE(read_int16(&n, f)); + for (i = 0; i < n && !failed; i++) { + SAFE(read_string(&s, f)); + if (s) { + na = findnick(s); + if (na) { + na->nc->flags |= NI_SERVICES_OPER; + if (slist_indexof(&servopers, na) == -1) + slist_add(&servopers, na); + } + free(s); + } + } + } + + if (ver >= 7) { + int32 tmp32; + SAFE(read_int32(&maxusercnt, f)); + SAFE(read_int32(&tmp32, f)); + maxusertime = tmp32; + } + + if (ver <= 10) + load_old_akill(); + else { + Akill *ak; + + read_int16(&tmp16, f); + slist_setcapacity(&akills, tmp16); + + for (i = 0; i < akills.capacity; i++) { + ak = scalloc(sizeof(Akill), 1); + + SAFE(read_string(&ak->user, f)); + SAFE(read_string(&ak->host, f)); + SAFE(read_string(&ak->by, f)); + SAFE(read_string(&ak->reason, f)); + SAFE(read_int32(&tmp32, f)); + ak->seton = tmp32; + SAFE(read_int32(&tmp32, f)); + ak->expires = tmp32; + + slist_add(&akills, ak); + } + } + + if (ver >= 11) { + SXLine *sx; + + read_int16(&tmp16, f); + slist_setcapacity(&sglines, tmp16); + + for (i = 0; i < sglines.capacity; i++) { + sx = scalloc(sizeof(SXLine), 1); + + SAFE(read_string(&sx->mask, f)); + SAFE(read_string(&sx->by, f)); + SAFE(read_string(&sx->reason, f)); + SAFE(read_int32(&tmp32, f)); + sx->seton = tmp32; + SAFE(read_int32(&tmp32, f)); + sx->expires = tmp32; + + slist_add(&sglines, sx); + } + + if (ver >= 13) { + read_int16(&tmp16, f); + slist_setcapacity(&sqlines, tmp16); + + for (i = 0; i < sqlines.capacity; i++) { + sx = scalloc(sizeof(SXLine), 1); + + SAFE(read_string(&sx->mask, f)); + SAFE(read_string(&sx->by, f)); + SAFE(read_string(&sx->reason, f)); + SAFE(read_int32(&tmp32, f)); + sx->seton = tmp32; + SAFE(read_int32(&tmp32, f)); + sx->expires = tmp32; + + slist_add(&sqlines, sx); + } + } + + read_int16(&tmp16, f); + slist_setcapacity(&szlines, tmp16); + + for (i = 0; i < szlines.capacity; i++) { + sx = scalloc(sizeof(SXLine), 1); + + SAFE(read_string(&sx->mask, f)); + SAFE(read_string(&sx->by, f)); + SAFE(read_string(&sx->reason, f)); + SAFE(read_int32(&tmp32, f)); + sx->seton = tmp32; + SAFE(read_int32(&tmp32, f)); + sx->expires = tmp32; + + slist_add(&szlines, sx); + } + } + + if (ver >= 12) { + for (i = 0; i < 1024 && !failed; i++) { + hclast = &hcache[i]; + hcprev = NULL; + + while ((c = getc_db(f)) != 0) { + if (c != 1) + fatal("Invalid format in %s", OperDBName); + + hc = scalloc(1, sizeof(HostCache)); + + SAFE(read_string(&hc->host, f)); + SAFE(read_int16(&tmp16, f)); + hc->status = tmp16; + SAFE(read_int32(&tmp32, f)); + hc->used = tmp32; + + *hclast = hc; + hclast = &hc->next; + hc->prev = hcprev; + hcprev = hc; + } /* while (getc_db(f) != 0) */ + + *hclast = NULL; + } /* for (i) */ + } + + close_db(f); + +} + +#undef SAFE + +/*************************************************************************/ + +/* Save OperServ data. */ + +#define SAFE(x) do { \ + if ((x) < 0) { \ + restore_db(f); \ + log_perror("Write error on %s", OperDBName); \ + if (time(NULL) - lastwarn > WarningTimeout) { \ + wallops(NULL, "Write error on %s: %s", OperDBName, \ + strerror(errno)); \ + lastwarn = time(NULL); \ + } \ + return; \ + } \ +} while (0) + +void save_os_dbase(void) +{ + int i; + dbFILE *f; + static time_t lastwarn = 0; + Akill *ak; + SXLine *sx; + HostCache *hc; + + if (!(f = open_db(s_OperServ, OperDBName, "w", OPER_VERSION))) + return; + SAFE(write_int32(maxusercnt, f)); + SAFE(write_int32(maxusertime, f)); + + SAFE(write_int16(akills.count, f)); + for (i = 0; i < akills.count; i++) { + ak = akills.list[i]; + + SAFE(write_string(ak->user, f)); + SAFE(write_string(ak->host, f)); + SAFE(write_string(ak->by, f)); + SAFE(write_string(ak->reason, f)); + SAFE(write_int32(ak->seton, f)); + SAFE(write_int32(ak->expires, f)); + } + + SAFE(write_int16(sglines.count, f)); + for (i = 0; i < sglines.count; i++) { + sx = sglines.list[i]; + + SAFE(write_string(sx->mask, f)); + SAFE(write_string(sx->by, f)); + SAFE(write_string(sx->reason, f)); + SAFE(write_int32(sx->seton, f)); + SAFE(write_int32(sx->expires, f)); + } + + SAFE(write_int16(sqlines.count, f)); + for (i = 0; i < sqlines.count; i++) { + sx = sqlines.list[i]; + + SAFE(write_string(sx->mask, f)); + SAFE(write_string(sx->by, f)); + SAFE(write_string(sx->reason, f)); + SAFE(write_int32(sx->seton, f)); + SAFE(write_int32(sx->expires, f)); + } + + SAFE(write_int16(szlines.count, f)); + for (i = 0; i < szlines.count; i++) { + sx = szlines.list[i]; + + SAFE(write_string(sx->mask, f)); + SAFE(write_string(sx->by, f)); + SAFE(write_string(sx->reason, f)); + SAFE(write_int32(sx->seton, f)); + SAFE(write_int32(sx->expires, f)); + } + + for (i = 0; i < 1024; i++) { + for (hc = hcache[i]; hc; hc = hc->next) { + /* Don't save in-progress scans */ + if (hc->status < HC_NORMAL) + continue; + + SAFE(write_int8(1, f)); + + SAFE(write_string(hc->host, f)); + SAFE(write_int16(hc->status, f)); + SAFE(write_int32(hc->used, f)); + + } /* for (hc) */ + SAFE(write_int8(0, f)); + } /* for (i) */ + + close_db(f); + +} + +#undef SAFE + +/*************************************************************************/ + +void save_os_rdb_dbase(void) +{ +#ifdef USE_RDB + if (!rdb_open()) + return; + rdb_save_os_db(maxusercnt, maxusertime, &akills, &sglines, &sqlines, + &szlines, hcache[0]); + rdb_close(); +#endif +} + +/*************************************************************************/ + +/* Removes the nick structure from OperServ lists. */ + +void os_remove_nick(NickCore * nc) +{ + slist_remove(&servadmins, nc); + slist_remove(&servopers, nc); +} + +/*************************************************************************/ + +/* Does the given user have Services root privileges? + Now enhanced. */ + +int is_services_root(User * u) +{ + if ((NSStrictPrivileges && !is_oper(u)) + || (!skeleton && !nick_identified(u))) + return 0; + if (skeleton || (u->na->nc->flags & NI_SERVICES_ROOT)) + return 1; + return 0; +} + +/*************************************************************************/ + +/* Does the given user have Services admin privileges? */ + +int is_services_admin(User * u) +{ + if ((NSStrictPrivileges && !is_oper(u)) + || (!skeleton && !nick_identified(u))) + return 0; + if (skeleton + || (u->na->nc->flags & (NI_SERVICES_ADMIN | NI_SERVICES_ROOT))) + return 1; + return 0; +} + +/*************************************************************************/ + +/* Does the given user have Services oper privileges? */ + +int is_services_oper(User * u) +{ + if ((NSStrictPrivileges && !is_oper(u)) + || (!skeleton && !nick_identified(u))) + return 0; + if (skeleton + || (u->na->nc-> + flags & (NI_SERVICES_OPER | NI_SERVICES_ADMIN | + NI_SERVICES_ROOT))) + return 1; + return 0; +} + +/*************************************************************************/ + +/* Is the given nick a Services root nick? */ + +int nick_is_services_root(NickCore * nc) +{ + if (nc->flags & (NI_SERVICES_ROOT)) + return 1; + + return 0; +} + +/*************************************************************************/ + +/* Is the given nick a Services admin/root nick? */ + +int nick_is_services_admin(NickCore * nc) +{ + if (nc->flags & (NI_SERVICES_ADMIN | NI_SERVICES_ROOT)) + return 1; + + return 0; +} + +/*************************************************************************/ + +/* Is the given nick a Services oper/admin/root nick? */ + +int nick_is_services_oper(NickCore * nc) +{ + if (nc-> + flags & (NI_SERVICES_OPER | NI_SERVICES_ADMIN | NI_SERVICES_ROOT)) + return 1; + + return 0; +} + +/*************************************************************************/ +/**************************** Clone detection ****************************/ +/*************************************************************************/ + +/* We just got a new user; does it look like a clone? If so, send out a + * wallops. + */ + +void check_clones(User * user) +{ +#ifndef STREAMLINED + int i, clone_count; + long last_time; + + if (!CheckClones) + return; + + if (clonelist[0].host) + free(clonelist[0].host); + i = CLONE_DETECT_SIZE - 1; + memmove(clonelist, clonelist + 1, sizeof(struct clone) * i); + clonelist[i].host = sstrdup(GetHost(user)); + last_time = clonelist[i].time = time(NULL); + clone_count = 1; + while (--i >= 0 && clonelist[i].host) { + if (clonelist[i].time < last_time - CloneMaxDelay) + break; + if (stricmp(clonelist[i].host, GetHost(user)) == 0) { + ++clone_count; + last_time = clonelist[i].time; + if (clone_count >= CloneMinUsers) + break; + } + } + if (clone_count >= CloneMinUsers) { + /* Okay, we have clones. Check first to see if we already know + * about them. */ + for (i = CLONE_DETECT_SIZE - 1; i >= 0 && warnings[i].host; --i) { + if (stricmp(warnings[i].host, GetHost(user)) == 0) + break; + } + if (i < 0 + || warnings[i].time < user->my_signon - CloneWarningDelay) { + /* Send out the warning, and note it. */ + wallops(s_OperServ, + "\2WARNING\2 - possible clones detected from %s", + GetHost(user)); + alog("%s: possible clones detected from %s", s_OperServ, + GetHost(user)); + i = CLONE_DETECT_SIZE - 1; + if (warnings[0].host) + free(warnings[0].host); + memmove(warnings, warnings + 1, sizeof(struct clone) * i); + warnings[i].host = sstrdup(GetHost(user)); + warnings[i].time = clonelist[i].time; + if (KillClones) + kill_user(s_OperServ, user->nick, "Clone kill"); + } + } +#endif /* !STREAMLINED */ +} + +/*************************************************************************/ + +#ifdef DEBUG_COMMANDS + +/* Send clone arrays to given nick. */ + +static void send_clone_lists(User * u) +{ + int i; + + if (!CheckClones) { + notice(s_OperServ, u->nick, "CheckClones not enabled."); + return; + } + + notice(s_OperServ, u->nick, "clonelist[]"); + for (i = 0; i < CLONE_DETECT_SIZE; i++) { + if (clonelist[i].host) + notice(s_OperServ, u->nick, " %10ld %s", clonelist[i].time, + clonelist[i].host ? clonelist[i].host : "(null)"); + } + notice(s_OperServ, u->nick, "warnings[]"); + for (i = 0; i < CLONE_DETECT_SIZE; i++) { + if (clonelist[i].host) + notice(s_OperServ, u->nick, " %10ld %s", warnings[i].time, + warnings[i].host ? warnings[i].host : "(null)"); + } +} + +#endif /* DEBUG_COMMANDS */ + +/*************************************************************************/ +/*********************** OperServ command functions **********************/ +/*************************************************************************/ + +/* HELP command. */ + +static int do_help(User * u) +{ + const char *cmd = strtok(NULL, ""); + + if (!cmd) { + notice_help(s_OperServ, u, OPER_HELP); + if (is_services_oper(u)) + notice_help(s_OperServ, u, OPER_HELP_OPER_CMD); + if (is_services_admin(u)) + notice_help(s_OperServ, u, OPER_HELP_ADMIN_CMD); +#ifdef USE_MODULES + if (is_services_root(u)) + notice_help(s_OperServ, u, OPER_HELP_ROOT_CMD); +#endif + moduleDisplayHelp(5, u); + notice_help(s_OperServ, u, OPER_HELP_LOGGED); + } else { + mod_help_cmd(s_OperServ, u, OPERSERV, cmd); + } + return MOD_CONT; +} + +/*************************************************************************/ + +static int do_global(User * u) +{ + char *msg = strtok(NULL, ""); + + if (!msg) { + syntax_error(s_OperServ, u, "GLOBAL", OPER_GLOBAL_SYNTAX); + return MOD_CONT; + } + if (WallOSGlobal) + wallops(s_OperServ, "\2%s\2 just used GLOBAL command.", u->nick); + oper_global(u->nick, "%s", msg); + return MOD_CONT; +} + +Server *server_global(Server * s, char *msg) +{ + Server *sl; + + while (s) { + notice_server(s_GlobalNoticer, s, "%s", msg); + if (s->links) { + sl = server_global(s->links, msg); + if (sl) + s = sl; + else + s = s->next; + } else { + s = s->next; + } + } + return s; + +} + +void oper_global(char *nick, char *fmt, ...) +{ + va_list args; + char msg[2048]; /* largest valid message is 512, this should cover any global */ + char dmsg[2048]; /* largest valid message is 512, this should cover any global */ + + va_start(args, fmt); + vsnprintf(msg, sizeof(msg), fmt, args); + va_end(args); + + /* I don't like the way this is coded... */ + if ((nick) && (!AnonymousGlobal)) { + snprintf(dmsg, sizeof(dmsg), "[%s] %s", nick, msg); + server_global(servlist, dmsg); + } else { + server_global(servlist, msg); + } + +} + +/*************************************************************************/ + +/* STATS command. */ + +static int do_stats(User * u) +{ + time_t uptime = time(NULL) - start_time; + char *extra = strtok(NULL, ""); + int days = uptime / 86400, hours = (uptime / 3600) % 24, + mins = (uptime / 60) % 60, secs = uptime % 60; + struct tm *tm; + char timebuf[64]; + + if (extra && stricmp(extra, "ALL") != 0) { + if (stricmp(extra, "AKILL") == 0) { + int timeout; + /* AKILLs */ + notice_lang(s_OperServ, u, OPER_STATS_AKILL_COUNT, + akills.count); + timeout = AutokillExpiry + 59; + if (timeout >= 172800) + notice_lang(s_OperServ, u, OPER_STATS_AKILL_EXPIRE_DAYS, + timeout / 86400); + else if (timeout >= 86400) + notice_lang(s_OperServ, u, OPER_STATS_AKILL_EXPIRE_DAY); + else if (timeout >= 7200) + notice_lang(s_OperServ, u, OPER_STATS_AKILL_EXPIRE_HOURS, + timeout / 3600); + else if (timeout >= 3600) + notice_lang(s_OperServ, u, OPER_STATS_AKILL_EXPIRE_HOUR); + else if (timeout >= 120) + notice_lang(s_OperServ, u, OPER_STATS_AKILL_EXPIRE_MINS, + timeout / 60); + else if (timeout >= 60) + notice_lang(s_OperServ, u, OPER_STATS_AKILL_EXPIRE_MIN); + else + notice_lang(s_OperServ, u, OPER_STATS_AKILL_EXPIRE_NONE); +#ifdef IRC_BAHAMUT + /* SGLINEs */ + notice_lang(s_OperServ, u, OPER_STATS_SGLINE_COUNT, + sglines.count); + timeout = SGLineExpiry + 59; + if (timeout >= 172800) + notice_lang(s_OperServ, u, OPER_STATS_SGLINE_EXPIRE_DAYS, + timeout / 86400); + else if (timeout >= 86400) + notice_lang(s_OperServ, u, OPER_STATS_SGLINE_EXPIRE_DAY); + else if (timeout >= 7200) + notice_lang(s_OperServ, u, OPER_STATS_SGLINE_EXPIRE_HOURS, + timeout / 3600); + else if (timeout >= 3600) + notice_lang(s_OperServ, u, OPER_STATS_SGLINE_EXPIRE_HOUR); + else if (timeout >= 120) + notice_lang(s_OperServ, u, OPER_STATS_SGLINE_EXPIRE_MINS, + timeout / 60); + else if (timeout >= 60) + notice_lang(s_OperServ, u, OPER_STATS_SGLINE_EXPIRE_MIN); + else + notice_lang(s_OperServ, u, OPER_STATS_SGLINE_EXPIRE_NONE); +#endif + /* SQLINEs */ + notice_lang(s_OperServ, u, OPER_STATS_SQLINE_COUNT, + sqlines.count); + timeout = SQLineExpiry + 59; + if (timeout >= 172800) + notice_lang(s_OperServ, u, OPER_STATS_SQLINE_EXPIRE_DAYS, + timeout / 86400); + else if (timeout >= 86400) + notice_lang(s_OperServ, u, OPER_STATS_SQLINE_EXPIRE_DAY); + else if (timeout >= 7200) + notice_lang(s_OperServ, u, OPER_STATS_SQLINE_EXPIRE_HOURS, + timeout / 3600); + else if (timeout >= 3600) + notice_lang(s_OperServ, u, OPER_STATS_SQLINE_EXPIRE_HOUR); + else if (timeout >= 120) + notice_lang(s_OperServ, u, OPER_STATS_SQLINE_EXPIRE_MINS, + timeout / 60); + else if (timeout >= 60) + notice_lang(s_OperServ, u, OPER_STATS_SQLINE_EXPIRE_MIN); + else + notice_lang(s_OperServ, u, OPER_STATS_SQLINE_EXPIRE_NONE); +#ifdef IRC_BAHAMUT + /* SZLINEs */ + notice_lang(s_OperServ, u, OPER_STATS_SZLINE_COUNT, + szlines.count); + timeout = SZLineExpiry + 59; + if (timeout >= 172800) + notice_lang(s_OperServ, u, OPER_STATS_SZLINE_EXPIRE_DAYS, + timeout / 86400); + else if (timeout >= 86400) + notice_lang(s_OperServ, u, OPER_STATS_SZLINE_EXPIRE_DAY); + else if (timeout >= 7200) + notice_lang(s_OperServ, u, OPER_STATS_SZLINE_EXPIRE_HOURS, + timeout / 3600); + else if (timeout >= 3600) + notice_lang(s_OperServ, u, OPER_STATS_SZLINE_EXPIRE_HOUR); + else if (timeout >= 120) + notice_lang(s_OperServ, u, OPER_STATS_SZLINE_EXPIRE_MINS, + timeout / 60); + else if (timeout >= 60) + notice_lang(s_OperServ, u, OPER_STATS_SZLINE_EXPIRE_MIN); + else + notice_lang(s_OperServ, u, OPER_STATS_SZLINE_EXPIRE_NONE); +#endif + return MOD_CONT; + } else if (!stricmp(extra, "RESET")) { + if (is_services_admin(u)) { + maxusercnt = usercnt; + notice_lang(s_OperServ, u, OPER_STATS_RESET); + } else { + notice_lang(s_OperServ, u, PERMISSION_DENIED); + } + return MOD_CONT; + } else { + notice_lang(s_OperServ, u, OPER_STATS_UNKNOWN_OPTION, extra); + } + } + + notice_lang(s_OperServ, u, OPER_STATS_CURRENT_USERS, usercnt, opcnt); + tm = localtime(&maxusertime); + strftime_lang(timebuf, sizeof(timebuf), u, STRFTIME_DATE_TIME_FORMAT, + tm); + notice_lang(s_OperServ, u, OPER_STATS_MAX_USERS, maxusercnt, timebuf); + if (days > 1) { + notice_lang(s_OperServ, u, OPER_STATS_UPTIME_DHMS, + days, hours, mins, secs); + } else if (days == 1) { + notice_lang(s_OperServ, u, OPER_STATS_UPTIME_1DHMS, + days, hours, mins, secs); + } else { + if (hours > 1) { + if (mins != 1) { + if (secs != 1) { + notice_lang(s_OperServ, u, OPER_STATS_UPTIME_HMS, + hours, mins, secs); + } else { + notice_lang(s_OperServ, u, OPER_STATS_UPTIME_HM1S, + hours, mins, secs); + } + } else { + if (secs != 1) { + notice_lang(s_OperServ, u, OPER_STATS_UPTIME_H1MS, + hours, mins, secs); + } else { + notice_lang(s_OperServ, u, OPER_STATS_UPTIME_H1M1S, + hours, mins, secs); + } + } + } else if (hours == 1) { + if (mins != 1) { + if (secs != 1) { + notice_lang(s_OperServ, u, OPER_STATS_UPTIME_1HMS, + hours, mins, secs); + } else { + notice_lang(s_OperServ, u, OPER_STATS_UPTIME_1HM1S, + hours, mins, secs); + } + } else { + if (secs != 1) { + notice_lang(s_OperServ, u, OPER_STATS_UPTIME_1H1MS, + hours, mins, secs); + } else { + notice_lang(s_OperServ, u, OPER_STATS_UPTIME_1H1M1S, + hours, mins, secs); + } + } + } else { + if (mins != 1) { + if (secs != 1) { + notice_lang(s_OperServ, u, OPER_STATS_UPTIME_MS, + mins, secs); + } else { + notice_lang(s_OperServ, u, OPER_STATS_UPTIME_M1S, + mins, secs); + } + } else { + if (secs != 1) { + notice_lang(s_OperServ, u, OPER_STATS_UPTIME_1MS, + mins, secs); + } else { + notice_lang(s_OperServ, u, OPER_STATS_UPTIME_1M1S, + mins, secs); + } + } + } + } + + if (extra && stricmp(extra, "ALL") == 0 && is_services_admin(u)) { + long count, mem; + + notice_lang(s_OperServ, u, OPER_STATS_BYTES_READ, + total_read / 1024); + notice_lang(s_OperServ, u, OPER_STATS_BYTES_WRITTEN, + total_written / 1024); + + get_user_stats(&count, &mem); + notice_lang(s_OperServ, u, OPER_STATS_USER_MEM, count, + (mem + 512) / 1024); + get_channel_stats(&count, &mem); + notice_lang(s_OperServ, u, OPER_STATS_CHANNEL_MEM, count, + (mem + 512) / 1024); + get_core_stats(&count, &mem); + notice_lang(s_OperServ, u, OPER_STATS_GROUPS_MEM, count, + (mem + 512) / 1024); + get_aliases_stats(&count, &mem); + notice_lang(s_OperServ, u, OPER_STATS_ALIASES_MEM, count, + (mem + 512) / 1024); + get_chanserv_stats(&count, &mem); + notice_lang(s_OperServ, u, OPER_STATS_CHANSERV_MEM, count, + (mem + 512) / 1024); + get_botserv_stats(&count, &mem); + notice_lang(s_OperServ, u, OPER_STATS_BOTSERV_MEM, count, + (mem + 512) / 1024); + get_operserv_stats(&count, &mem); + notice_lang(s_OperServ, u, OPER_STATS_OPERSERV_MEM, count, + (mem + 512) / 1024); + get_session_stats(&count, &mem); + notice_lang(s_OperServ, u, OPER_STATS_SESSIONS_MEM, count, + (mem + 512) / 1024); +#ifdef USE_THREADS + if (ProxyDetect) { + get_proxy_stats(&count, &mem); + notice_lang(s_OperServ, u, OPER_STATS_PROXY_MEM, count, + (mem + 512) / 1024); + } +#endif + } + return MOD_CONT; +} + +/*************************************************************************/ + +/* make Services ignore users for a certain time */ + +static int do_ignoreuser(User * u) +{ + char *cmd = strtok(NULL, " "); + int t; + + if (!cmd) { + notice_lang(s_OperServ, u, OPER_IGNORE_SYNTAX); + return MOD_CONT; + } + + if (!stricmp(cmd, "ADD")) { + + char *time = strtok(NULL, " "); + char *nick = strtok(NULL, " "); + char *rest = strtok(NULL, ""); + + if (!nick) { + notice_lang(s_OperServ, u, OPER_IGNORE_SYNTAX); + return MOD_CONT; + } else if (!time) { + notice_lang(s_OperServ, u, OPER_IGNORE_SYNTAX); + return MOD_CONT; + } else { + t = dotime(time); + rest = NULL; + + if (t <= -1) { + notice_lang(s_OperServ, u, OPER_IGNORE_VALID_TIME); + return MOD_CONT; + } else if (t == 0) { + t = 157248000; /* if 0 is given, we set time to 157248000 seconds == 5 years (let's hope the next restart will be before that time ;-)) */ + add_ignore(nick, t); + notice_lang(s_OperServ, u, OPER_IGNORE_PERM_DONE, nick); + } else { + add_ignore(nick, t); + notice_lang(s_OperServ, u, OPER_IGNORE_TIME_DONE, nick, + time); + } + } + } else if (!stricmp(cmd, "LIST")) { + do_ignorelist(u); + } + + else if (!stricmp(cmd, "DEL")) { + char *nick = strtok(NULL, " "); + if (!nick) { + notice_lang(s_OperServ, u, OPER_IGNORE_SYNTAX); + } else { + if (get_ignore(nick) == 0) { + notice_lang(s_OperServ, u, OPER_IGNORE_LIST_NOMATCH, nick); + return MOD_CONT; + } else { + delete_ignore(nick); + notice_lang(s_OperServ, u, OPER_IGNORE_DEL_DONE, nick); + } + } + } else if (!stricmp(cmd, "CLEAR")) { + do_clearignore(u); + + } else + notice_lang(s_OperServ, u, OPER_IGNORE_SYNTAX); + return MOD_CONT; +} + +/*************************************************************************/ + +/* deletes a nick from the ignore list */ + +void delete_ignore(const char *nick) +{ + IgnoreData *ign, *prev; + IgnoreData **whichlist = &ignore[tolower(nick[0])]; + + for (ign = *whichlist, prev = NULL; ign; prev = ign, ign = ign->next) { + if (stricmp(ign->who, nick) == 0) + break; + } + if (prev) + prev->next = ign->next; + else + *whichlist = ign->next; + free(ign); + ign = NULL; +} + +/*************************************************************************/ + +/* shows the Services ignore list */ + +static int do_ignorelist(User * u) +{ + int sent_header = 0; + IgnoreData *id; + int i; + + for (i = 0; i < 256; i++) { + for (id = ignore[i]; id; id = id->next) { + if (!sent_header) { + notice_lang(s_OperServ, u, OPER_IGNORE_LIST); + sent_header = 1; + } + notice(s_OperServ, u->nick, "%s", id->who); + } + } + if (!sent_header) + notice_lang(s_OperServ, u, OPER_IGNORE_LIST_EMPTY); + return MOD_CONT; +} + +/**************************************************************************/ +/* Cleares the Services ignore list */ + +static int do_clearignore(User * u) +{ + IgnoreData *id = NULL, *next = NULL; + int i; + for (i = 0; i < 256; i++) { + for (id = ignore[i]; id; id = next) { + next = id->next; + free(id); + if (!next) { + ignore[i] = NULL; + } + } + } + notice_lang(s_OperServ, u, OPER_IGNORE_LIST_CLEARED); + return MOD_CONT; +} + +/**************************************************************************/ + +/* Channel mode changing (MODE command). */ + +static int do_os_mode(User * u) +{ + int ac; + char **av; + char *chan = strtok(NULL, " "), *modes = strtok(NULL, ""); + Channel *c; + + if (!chan || !modes) { + syntax_error(s_OperServ, u, "MODE", OPER_MODE_SYNTAX); + return MOD_CONT; + } + + if (!(c = findchan(chan))) { + notice_lang(s_OperServ, u, CHAN_X_NOT_IN_USE, chan); + } else if (c->bouncy_modes) { + notice_lang(s_OperServ, u, OPER_BOUNCY_MODES_U_LINE); + return MOD_CONT; +#ifdef CMODE_A + } else if ((!is_services_admin(u)) && (c->mode & CMODE_A)) { + notice_lang(s_OperServ, u, PERMISSION_DENIED); + return MOD_CONT; +#endif + } else { + send_mode(s_OperServ, chan, "%s", modes); + + ac = split_buf(modes, &av, 1); + chan_set_modes(s_OperServ, c, ac, av, 0); + + if (WallOSMode) + wallops(s_OperServ, "%s used MODE %s on %s", u->nick, modes, + chan); + } + return MOD_CONT; +} + +/**************************************************************************/ + +/** + * Change any user's UMODES + * + * modified to be part of the SuperAdmin directive -jester + * check user flag for SuperAdmin -rob + */ +#ifdef USE_OSSVS +#ifndef IRC_HYBRID +static int do_operumodes(User * u) +{ + char *nick = strtok(NULL, " "); + char *modes = strtok(NULL, ""); + + User *u2; + + /* Only allow this if SuperAdmin is enabled */ + if (!u->isSuperAdmin) { + notice_lang(s_OperServ, u, OPER_SUPER_ADMIN_ONLY); + return MOD_CONT; + } + + if (!nick || !modes) { + syntax_error(s_OperServ, u, "UMODE", OPER_UMODE_SYNTAX); + return MOD_CONT; + } + + /** + * Only accept a +/- mode string + *-rob + **/ + if ((modes[0] != '+') && (modes[0] != '-')) { + syntax_error(s_OperServ, u, "UMODE", OPER_UMODE_SYNTAX); + return MOD_CONT; + } + if (!(u2 = finduser(nick))) { + notice_lang(s_OperServ, u, NICK_X_NOT_IN_USE, nick); + } else { + send_mode(s_OperServ, nick, "%s", modes); + + change_user_mode(u2, modes, NULL); + + notice_lang(s_OperServ, u, OPER_UMODE_SUCCESS, nick); + notice_lang(s_OperServ, u2, OPER_UMODE_CHANGED, u->nick); + + if (WallOSMode) + wallops(s_OperServ, "\2%s\2 used UMODE on %s", u->nick, nick); + } + return MOD_CONT; +} +#endif +#endif +/**************************************************************************/ + +/** + * give Operflags to any user + * + * modified to be part of the SuperAdmin directive -jester + * check u-> for SuperAdmin -rob + */ +#if defined (IRC_UNREAL) && defined (USE_OSSVS) + +static int do_operoline(User * u) +{ + char *nick = strtok(NULL, " "); + char *flags = strtok(NULL, ""); + User *u2 = NULL; + + /* Only allow this if SuperAdmin is enabled */ + if (!u->isSuperAdmin) { + notice_lang(s_OperServ, u, OPER_SUPER_ADMIN_ONLY); + return MOD_CONT; + } + + if (!nick || !flags) { + syntax_error(s_OperServ, u, "OLINE", OPER_OLINE_SYNTAX); + return MOD_CONT; + } else { + u2 = finduser(nick); + +/* let's check whether the user is online */ + + if (!finduser(nick)) { + notice_lang(s_OperServ, u, NICK_X_NOT_IN_USE, nick); + } else if (u2 && flags[0] == '+') { + send_cmd(s_OperServ, "SVSO %s %s", nick, flags); + send_mode(s_OperServ, nick, "+o"); + change_user_mode(u2, "+o", NULL); + notice_lang(s_OperServ, u2, OPER_OLINE_IRCOP); + notice_lang(s_OperServ, u, OPER_OLINE_SUCCESS, flags, nick); + wallops(s_OperServ, "\2%s\2 used OLINE for %s", u->nick, nick); + } else if (u2 && flags[0] == '-') { + send_cmd(s_OperServ, "SVSO %s %s", nick, flags); + notice_lang(s_OperServ, u, OPER_OLINE_SUCCESS, flags, nick); + wallops(s_OperServ, "\2%s\2 used OLINE for %s", u->nick, nick); + } else + syntax_error(s_OperServ, u, "OLINE", OPER_OLINE_SYNTAX); + } + return MOD_CONT; +} +#endif +/*************************************************************************/ + +/* Clear all modes from a channel. */ + +static int do_clearmodes(User * u) +{ + char *s; + int i; + char *argv[2]; + char *chan = strtok(NULL, " "); + Channel *c; + int all = 0; + int count; /* For saving ban info */ + char **bans; /* For saving ban info */ +#ifdef HAS_EXCEPT + int exceptcount; /* For saving except info */ + char **excepts; /* For saving except info */ +#endif + struct c_userlist *cu, *next; + + if (!chan) { + syntax_error(s_OperServ, u, "CLEARMODES", OPER_CLEARMODES_SYNTAX); + } else if (!(c = findchan(chan))) { + notice_lang(s_OperServ, u, CHAN_X_NOT_IN_USE, chan); + } else if (c->bouncy_modes) { + notice_lang(s_OperServ, u, OPER_BOUNCY_MODES_U_LINE); + return MOD_CONT; + } else { + s = strtok(NULL, " "); + if (s) { + if (stricmp(s, "ALL") == 0) { + all = 1; + } else { + syntax_error(s_OperServ, u, "CLEARMODES", + OPER_CLEARMODES_SYNTAX); + return MOD_CONT; + } + } + + if (WallOSClearmodes) + wallops(s_OperServ, "%s used CLEARMODES%s on %s", u->nick, + all ? " ALL" : "", chan); + + if (all) { + /* Clear mode +o */ + for (cu = c->users; cu; cu = next) { + next = cu->next; + + if (!chan_has_user_status(c, cu->user, CUS_OP)) + continue; + + argv[0] = sstrdup("-o"); + argv[1] = cu->user->nick; + + send_mode(s_OperServ, c->name, "-o %s", cu->user->nick); + chan_set_modes(s_OperServ, c, 2, argv, 0); + + free(argv[0]); + } + + /* Clear mode +v */ + for (cu = c->users; cu; cu = next) { + next = cu->next; + + if (!chan_has_user_status(c, cu->user, CUS_VOICE)) + continue; + + argv[0] = sstrdup("-v"); + argv[1] = sstrdup(cu->user->nick); + + send_mode(s_OperServ, c->name, "-v %s", cu->user->nick); + chan_set_modes(s_OperServ, c, 2, argv, 0); + + free(argv[0]); + } +#ifdef HAS_HALFOP + /* Clear mode +h */ + for (cu = c->users; cu; cu = next) { + next = cu->next; + + if (!chan_has_user_status(c, cu->user, CUS_HALFOP)) + continue; + + argv[0] = sstrdup("-h"); + argv[1] = sstrdup(cu->user->nick); + + send_mode(s_OperServ, c->name, "-h %s", cu->user->nick); + chan_set_modes(s_OperServ, c, 2, argv, 0); + + free(argv[0]); + } +#endif + } + + /* Clear modes */ + send_mode(s_OperServ, c->name, "%s %s", MODESTOREMOVE, + c->key ? c->key : ""); + argv[0] = sstrdup(MODESTOREMOVE); + argv[1] = c->key ? c->key : NULL; + chan_set_modes(s_OperServ, c, c->key ? 2 : 1, argv, 0); + free(argv[0]); + + /* Clear bans */ + count = c->bancount; + bans = scalloc(sizeof(char *) * count, 1); + + for (i = 0; i < count; i++) + bans[i] = sstrdup(c->bans[i]); + + for (i = 0; i < count; i++) { + argv[0] = sstrdup("-b"); + argv[1] = bans[i]; + send_mode(s_OperServ, c->name, "-b %s", argv[1]); + chan_set_modes(s_OperServ, c, 2, argv, 0); + free(argv[1]); + free(argv[0]); + } + + free(bans); + +#ifdef HAS_EXCEPT + /* Clear excepts */ + exceptcount = c->exceptcount; + excepts = scalloc(sizeof(char *) * exceptcount, 1); + + for (i = 0; i < exceptcount; i++) + excepts[i] = sstrdup(c->excepts[i]); + + for (i = 0; i < exceptcount; i++) { + argv[0] = sstrdup("-e"); + argv[1] = excepts[i]; + send_mode(s_OperServ, c->name, "-e %s", argv[1]); + chan_set_modes(s_OperServ, c, 2, argv, 0); + free(argv[1]); + free(argv[0]); + } + + free(excepts); +#endif + } + + notice_lang(s_OperServ, u, OPER_CLEARMODES_ALL_DONE, chan); + return MOD_CONT; +} + +/*************************************************************************/ + +/* Kick a user from a channel (KICK command). */ + +static int do_os_kick(User * u) +{ + char *argv[3]; + char *chan, *nick, *s; + Channel *c; + + chan = strtok(NULL, " "); + nick = strtok(NULL, " "); + s = strtok(NULL, ""); + if (!chan || !nick || !s) { + syntax_error(s_OperServ, u, "KICK", OPER_KICK_SYNTAX); + return MOD_CONT; + } + if (!(c = findchan(chan))) { + notice_lang(s_OperServ, u, CHAN_X_NOT_IN_USE, chan); + } else if (c->bouncy_modes) { + notice_lang(s_OperServ, u, OPER_BOUNCY_MODES_U_LINE); + return MOD_CONT; + } + send_cmd(s_OperServ, "KICK %s %s :%s (%s)", chan, nick, u->nick, s); + if (WallOSKick) + wallops(s_OperServ, "%s used KICK on %s/%s", u->nick, nick, chan); + argv[0] = sstrdup(chan); + argv[1] = sstrdup(nick); + argv[2] = sstrdup(s); + do_kick(s_OperServ, 3, argv); + free(argv[2]); + free(argv[1]); + free(argv[0]); + return MOD_CONT; +} + +/*************************************************************************/ + +/* Forcefully change a user's nickname */ +#ifdef USE_OSSVS + +static int do_svsnick(User * u) +{ + char *nick = strtok(NULL, " "); + char *newnick = strtok(NULL, " "); + + NickAlias *na; + char *c; + + /* Only allow this if SuperAdmin is enabled */ + if (!u->isSuperAdmin) { + notice_lang(s_OperServ, u, OPER_SUPER_ADMIN_ONLY); + return MOD_CONT; + } + + if (!nick || !newnick) { + syntax_error(s_OperServ, u, "SVSNICK", OPER_SVSNICK_SYNTAX); + return MOD_CONT; + } + + /* Truncate long nicknames to NICKMAX-2 characters */ + if (strlen(newnick) > (NICKMAX - 2)) { + notice_lang(s_NickServ, u, NICK_X_TRUNCATED, + newnick, NICKMAX - 2, newnick); + newnick[NICKMAX - 2] = '\0'; + } + + /* Check for valid characters */ + if (*newnick == '-' || isdigit(*newnick)) { + notice_lang(s_OperServ, u, NICK_X_ILLEGAL, newnick); + return MOD_CONT; + } +#define isvalid(c) (((c) >= 'A' && (c) <= '~') || isdigit(c) || (c) == '-') + for (c = newnick; *c && (c - newnick) < NICKMAX; c++) { + if (!isvalid(*c) || isspace(*c)) { + notice_lang(s_OperServ, u, NICK_X_ILLEGAL, nick); + return MOD_CONT; + } + } + + /* Check for a nick in use or a forbidden/suspended nick */ + if (!finduser(nick)) { + notice_lang(s_OperServ, u, NICK_X_NOT_IN_USE, nick); + } else if (finduser(newnick)) { + notice_lang(s_NickServ, u, NICK_X_IN_USE, newnick); + } else if ((na = findnick(newnick)) && (na->status & NS_VERBOTEN)) { + notice_lang(s_NickServ, u, NICK_X_FORBIDDEN, newnick); + } else { + notice_lang(s_OperServ, u, OPER_SVSNICK_NEWNICK, nick, newnick); + wallops(s_OperServ, "%s used SVSNICK to change %s to %s", + u->nick, nick, newnick); + send_cmd(NULL, "SVSNICK %s %s :%ld", nick, newnick, time(NULL)); + } + return MOD_CONT; +} +#endif +/*************************************************************************/ + +/* Adds an AKILL to the list. Returns >= 0 on success, -1 if it fails, -2 + * if only the expiry time was changed. + * The success result is the number of AKILLs that were deleted to successfully add one. + */ + +int add_akill(User * u, char *mask, const char *by, const time_t expires, + const char *reason) +{ + int deleted = 0, i; + char *user, *mask2, *host; + Akill *entry; + + /* Checks whether there is an AKILL that already covers + * the one we want to add, and whether there are AKILLs + * that would be covered by this one. The masks AND the + * expiry times are used to determine this, because some + * AKILLs may become useful when another one expires. + * If so, warn the user in the first case and cleanup + * the useless AKILLs in the second. + */ + + if (akills.count > 0) { + + for (i = akills.count - 1; i >= 0; i--) { + char amask[BUFSIZE]; + + entry = akills.list[i]; + + if (!entry) + continue; + + snprintf(amask, sizeof(amask), "%s@%s", entry->user, + entry->host); + + if (!stricmp(amask, mask)) { + /* We change the AKILL expiry time if its current one is less than the new. + * This is preferable to be sure we don't change an important AKILL + * accidentely. + */ + if (entry->expires >= expires || entry->expires == 0) { + if (u) + notice_lang(s_OperServ, u, OPER_AKILL_EXISTS, + mask); + return -1; + } else { + entry->expires = expires; + if (u) + notice_lang(s_OperServ, u, OPER_AKILL_CHANGED, + amask); + return -2; + } + } + + if (match_wild_nocase(amask, mask) + && (entry->expires >= expires || entry->expires == 0)) { + if (u) + notice_lang(s_OperServ, u, OPER_AKILL_ALREADY_COVERED, + mask, amask); + return -1; + } + + if (match_wild_nocase(mask, amask) + && (entry->expires <= expires || expires == 0)) { + slist_delete(&akills, i); + deleted++; + } + } + + } + + /* We can now check whether the list is full or not. */ + if (slist_full(&akills)) { + if (u) + notice_lang(s_OperServ, u, OPER_AKILL_REACHED_LIMIT, + akills.limit); + return -1; + } + + /* We can now (really) add the AKILL. */ + mask2 = sstrdup(mask); + host = strchr(mask2, '@'); + if (!host) + return -1; + user = mask2; + *host = 0; + host++; + + entry = scalloc(sizeof(Akill), 1); + if (!entry) + return -1; + + entry->user = sstrdup(user); + entry->host = sstrdup(host); + entry->by = sstrdup(by); + entry->reason = sstrdup(reason); + entry->seton = time(NULL); + entry->expires = expires; + + slist_add(&akills, entry); + + if (AkillOnAdd) + s_akill(entry->user, entry->host, entry->by, entry->seton, + entry->expires, entry->reason); + + free(mask2); + + return deleted; +} + +/* Does the user match any AKILLs? */ + +int check_akill(const char *nick, const char *username, const char *host, + const char *vhost, const char *ip) +{ + int i; + Akill *ak; + + /** + * If DefCon is set to NO new users - kill the user ;). + **/ + if (checkDefCon(DEFCON_NO_NEW_CLIENTS)) { + kill_user(s_OperServ, nick, DefConAkillReason); + return 1; + } + + if (akills.count == 0) + return 0; + + for (i = 0; i < aki