4
mirror of git://git.acid.vegas/unrealircd.git synced 2024-12-26 16:26:38 +00:00
unrealircd/src/api-extban.c

454 lines
12 KiB
C
Raw Normal View History

2020-03-29 09:16:53 +00:00
/************************************************************************
* IRC - Internet Relay Chat, api-extban.c
* (C) 2003 Bram Matthys (Syzop) and the UnrealIRCd Team
*
* See file AUTHORS in IRC package for additional names of
* the programmers.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 1, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "unrealircd.h"
2022-01-15 05:16:34 +00:00
/** List of all extbans, their handlers, etc */
MODVAR Extban *extbans = NULL;
2020-03-29 09:16:53 +00:00
void set_isupport_extban(void)
{
2022-01-15 05:16:34 +00:00
char extbanstr[512];
Extban *e;
char *p = extbanstr;
for (e = extbans; e; e = e->next)
*p++ = e->letter;
*p = '\0';
2020-03-29 09:16:53 +00:00
ISupportSetFmt(NULL, "EXTBAN", "~,%s", extbanstr);
}
2022-11-20 04:12:40 +00:00
Extban *findmod_by_bantype_raw(const char *str, int ban_name_length)
2020-03-29 09:16:53 +00:00
{
2022-01-15 05:16:34 +00:00
Extban *e;
2022-11-20 04:12:40 +00:00
for (e=extbans; e; e = e->next)
{
if ((ban_name_length == 1) && (e->letter == str[0]))
return e;
if (e->name)
{
int namelen = strlen(e->name);
if ((namelen == ban_name_length) && !strncmp(e->name, str, namelen))
return e;
}
}
return NULL;
}
Extban *findmod_by_bantype(const char *str, const char **remainder)
{
2022-01-15 05:16:34 +00:00
int ban_name_length;
const char *p = strchr(str, ':');
if (!p || !p[1])
{
if (remainder)
*remainder = NULL;
return NULL;
}
if (remainder)
*remainder = p+1;
2020-03-29 09:16:53 +00:00
2022-01-15 05:16:34 +00:00
ban_name_length = p - str - 1;
2022-11-20 04:12:40 +00:00
return findmod_by_bantype_raw(str+1, ban_name_length);
2020-03-29 09:16:53 +00:00
}
2022-01-15 05:16:34 +00:00
/* Check if this is a valid extended ban name */
int is_valid_extban_name(const char *p)
{
if (!*p)
return 0; /* empty name */
2023-05-05 22:12:01 +00:00
if (strlen(p) > 32)
return 0; /* too long */
2022-01-15 05:16:34 +00:00
for (; *p; p++)
2023-05-05 22:12:01 +00:00
if (!islower(*p) && !isdigit(*p) && !strchr("_-", *p))
2022-01-15 05:16:34 +00:00
return 0;
return 1;
}
static void extban_add_sorted(Extban *n)
{
Extban *m;
if (extbans == NULL)
{
extbans = n;
return;
}
for (m = extbans; m; m = m->next)
{
if (m->letter == '\0')
abort();
if (sort_character_lowercase_before_uppercase(n->letter, m->letter))
{
/* Insert us before */
if (m->prev)
m->prev->next = n;
else
extbans = n; /* new head */
n->prev = m->prev;
n->next = m;
m->prev = n;
return;
}
if (!m->next)
{
/* Append us at end */
m->next = n;
n->prev = m;
return;
}
}
}
2020-03-29 09:16:53 +00:00
Extban *ExtbanAdd(Module *module, ExtbanInfo req)
{
2022-01-15 05:16:34 +00:00
Extban *e;
2022-11-20 04:12:40 +00:00
ModuleObject *banobj;
2022-01-15 05:16:34 +00:00
int existing = 0;
if (!req.name)
{
module->errorcode = MODERR_INVALID;
unreal_log(ULOG_ERROR, "module", "EXTBANADD_API_ERROR", NULL,
"ExtbanAdd(): name must be specified for ban (new in U6). Module: $module_name",
log_data_string("module_name", module->header->name));
return NULL;
}
2020-03-29 09:16:53 +00:00
2022-01-15 05:16:34 +00:00
if (!req.is_banned_events && req.is_banned)
2020-03-29 09:16:53 +00:00
{
2022-01-15 05:16:34 +00:00
module->errorcode = MODERR_INVALID;
unreal_log(ULOG_ERROR, "module", "EXTBANADD_API_ERROR", NULL,
"ExtbanAdd(): module must indicate via .is_banned_events on which BANCHK_* "
"events to listen on (new in U6). Module: $module_name",
log_data_string("module_name", module->header->name));
return NULL;
2020-03-29 09:16:53 +00:00
}
2022-01-15 05:16:34 +00:00
if (!isalnum(req.letter))
2020-03-29 09:16:53 +00:00
{
2022-01-15 05:16:34 +00:00
module->errorcode = MODERR_INVALID;
unreal_log(ULOG_ERROR, "module", "EXTBANADD_API_ERROR", NULL,
"ExtbanAdd(): module tried to add extban which is not alphanumeric. "
"Module: $module_name",
log_data_string("module_name", module->header->name));
2020-03-29 09:16:53 +00:00
return NULL;
}
2022-01-15 05:16:34 +00:00
if (!is_valid_extban_name(req.name))
{
module->errorcode = MODERR_INVALID;
unreal_log(ULOG_ERROR, "module", "EXTBANADD_API_ERROR", NULL,
"ExtbanAdd(): module tried to add extban with an invalid name ($extban_name). "
"Module: $module_name",
log_data_string("module_name", module->header->name),
log_data_string("extban_name", req.name));
return NULL;
}
2023-05-05 22:12:01 +00:00
if (!req.conv_param)
{
module->errorcode = MODERR_INVALID;
unreal_log(ULOG_ERROR, "module", "EXTBANADD_API_ERROR", NULL,
"ExtbanAdd(): conv_param event missing. Module: $module_name",
log_data_string("module_name", module->header->name));
return NULL;
}
2022-01-15 05:16:34 +00:00
for (e=extbans; e; e = e->next)
{
if (e->letter == req.letter)
{
2022-11-20 04:12:40 +00:00
/* Extban already exists in our list, let's see... */
2022-01-15 05:16:34 +00:00
if (e->unloaded)
{
e->unloaded = 0;
existing = 1;
break;
2022-11-20 04:12:40 +00:00
} else
if ((module->flags == MODFLAG_TESTING) && e->preregistered)
{
/* We are in MOD_INIT (yeah confusing, isn't it?)
* and the extban already exists and it was preregistered.
* Then go ahead with really registering it.
*/
e->preregistered = 0;
existing = 1;
2023-05-05 22:12:01 +00:00
break;
2022-11-20 04:12:40 +00:00
} else
if (module->flags == MODFLAG_NONE)
{
/* Better don't touch it, as we may still fail at this stage
* and if we would set .conv_param etc to this and the new module
* gets unloaded because of a config typo then we would be screwed
* (now we are not).
* NOTE: this does mean that if you hot-load an extban module
* then it may only be available for config stuff the 2nd rehash.
*/
return e;
} else
{
module->errorcode = MODERR_EXISTS;
2022-01-15 05:16:34 +00:00
return NULL;
}
}
}
if (!e)
{
/* Not found, create */
e = safe_alloc(sizeof(Extban));
e->letter = req.letter;
extban_add_sorted(e);
}
e->letter = req.letter;
safe_strdup(e->name, req.name);
e->is_ok = req.is_ok;
e->conv_param = req.conv_param;
e->is_banned = req.is_banned;
e->is_banned_events = req.is_banned_events;
e->owner = module;
e->options = req.options;
2022-11-20 04:12:40 +00:00
if (module->flags == MODFLAG_NONE)
e->preregistered = 1;
banobj = safe_alloc(sizeof(ModuleObject));
banobj->object.extban = e;
banobj->type = MOBJ_EXTBAN;
AddListItem(banobj, module->objects);
module->errorcode = MODERR_NOERROR;
2020-03-29 09:16:53 +00:00
set_isupport_extban();
2022-01-15 05:16:34 +00:00
return e;
2020-03-29 09:16:53 +00:00
}
2022-01-15 05:16:34 +00:00
static void unload_extban_commit(Extban *e)
2020-03-29 09:16:53 +00:00
{
2022-01-15 05:16:34 +00:00
/* Should we mass unban everywhere?
* Hmmm. Not needed per se, user can always unset
* themselves. Leaning towards no atm.
*/
// noop
2020-03-29 09:16:53 +00:00
2022-01-15 05:16:34 +00:00
/* Then unload the extban */
DelListItem(e, extbans);
2022-11-20 04:12:40 +00:00
safe_free(e->name);
2022-01-15 05:16:34 +00:00
safe_free(e);
set_isupport_extban();
}
2022-11-20 04:12:40 +00:00
/** Unload all unused extended bans after a REHASH */
void unload_all_unused_extbans(void)
{
Extban *e, *e_next;
for (e=extbans; e; e = e_next)
{
e_next = e->next;
if (e->letter && e->unloaded)
{
unload_extban_commit(e);
}
}
}
2022-01-15 05:16:34 +00:00
void ExtbanDel(Extban *e)
{
/* Always free the module object */
if (e->owner)
2020-03-29 09:16:53 +00:00
{
ModuleObject *banobj;
2022-01-15 05:16:34 +00:00
for (banobj = e->owner->objects; banobj; banobj = banobj->next)
2020-03-29 09:16:53 +00:00
{
2022-01-15 05:16:34 +00:00
if (banobj->type == MOBJ_EXTBAN && banobj->object.extban == e)
2020-03-29 09:16:53 +00:00
{
2022-01-15 05:16:34 +00:00
DelListItem(banobj, e->owner->objects);
2020-03-29 09:16:53 +00:00
safe_free(banobj);
break;
}
}
}
2022-01-15 05:16:34 +00:00
/* Whether we can actually (already) free the Extban, it depends... */
if (loop.rehashing)
e->unloaded = 1;
else
unload_extban_commit(e);
}
2020-03-29 09:16:53 +00:00
/** General is_ok for n!u@h stuff that also deals with recursive extbans.
*/
2022-01-15 05:16:34 +00:00
int extban_is_ok_nuh_extban(BanContext *b)
2020-03-29 09:16:53 +00:00
{
int isok;
static int extban_is_ok_recursion = 0;
/* Mostly copied from clean_ban_mask - but note MyUser checks aren't needed here: extban->is_ok() according to cmd_mode isn't called for nonlocal. */
2022-01-15 05:16:34 +00:00
if (is_extended_ban(b->banstr))
2020-03-29 09:16:53 +00:00
{
2022-01-15 05:16:34 +00:00
const char *nextbanstr;
Extban *extban = NULL;
/* We're dealing with a stacked extended ban.
* Rules:
* 1) You can only stack once, so: ~x:~y:something and not ~x:~y:~z...
* 2) The second item may never be an action modifier, nor have the
* EXTBOPT_NOSTACKCHILD letter set (for things like a textban).
*/
2020-03-29 09:16:53 +00:00
if (extban_is_ok_recursion)
2022-01-15 05:16:34 +00:00
return 0; /* Rule #1 violation (more than one stacked extban) */
2020-03-29 09:16:53 +00:00
2022-01-15 05:16:34 +00:00
if ((b->is_ok_check == EXBCHK_PARAM) && RESTRICT_EXTENDEDBANS && !ValidatePermissionsForPath("immune:restrict-extendedbans",b->client,NULL,b->channel,NULL))
2020-03-29 09:16:53 +00:00
{
/* Test if this specific extban has been disabled.
* (We can be sure RESTRICT_EXTENDEDBANS is not *. Else this extended ban wouldn't be happening at all.)
*/
2022-01-15 05:16:34 +00:00
if (strchr(RESTRICT_EXTENDEDBANS, b->banstr[1]))
2020-03-29 09:16:53 +00:00
{
2022-01-15 05:16:34 +00:00
sendnotice(b->client, "Setting/removing of extended bantypes '%s' has been disabled.", RESTRICT_EXTENDEDBANS);
2020-03-29 09:16:53 +00:00
return 0; /* Fail */
}
}
2022-01-15 05:16:34 +00:00
extban = findmod_by_bantype(b->banstr, &nextbanstr);
if (!extban)
2020-03-29 09:16:53 +00:00
{
2022-01-15 05:16:34 +00:00
if (b->what == MODE_DEL)
2020-03-29 09:16:53 +00:00
{
return 1; /* Always allow killing unknowns. */
}
return 0; /* Don't add unknown extbans. */
}
2022-01-15 05:16:34 +00:00
if ((extban->options & EXTBOPT_ACTMODIFIER) || (extban->options & EXTBOPT_NOSTACKCHILD))
{
/* Rule #2 violation */
return 0;
}
2020-03-29 09:16:53 +00:00
/* Now we have to ask the stacked extban if it's ok. */
2022-01-15 05:16:34 +00:00
if (extban->is_ok)
2020-03-29 09:16:53 +00:00
{
2022-01-15 05:16:34 +00:00
b->banstr = nextbanstr;
2020-03-29 09:16:53 +00:00
extban_is_ok_recursion++;
2022-01-15 05:16:34 +00:00
isok = extban->is_ok(b);
2020-03-29 09:16:53 +00:00
extban_is_ok_recursion--;
return isok;
}
}
return 1; /* Either not an extban, or extban has NULL is_ok. Good to go. */
}
/** Some kind of general conv_param routine,
* to ensure the parameter is nick!user@host.
* most of the code is just copied from clean_ban_mask.
*/
2022-01-15 05:16:34 +00:00
const char *extban_conv_param_nuh(BanContext *b, Extban *extban)
2020-03-29 09:16:53 +00:00
{
char tmpbuf[USERLEN + NICKLEN + HOSTLEN + 32];
2023-05-05 22:12:01 +00:00
static char retbuf[USERLEN + NICKLEN + HOSTLEN + 32];
2020-03-29 09:16:53 +00:00
2022-01-15 05:16:34 +00:00
/* Work on a copy */
strlcpy(tmpbuf, b->banstr, sizeof(retbuf));
2023-05-05 22:12:01 +00:00
return convert_regular_ban(tmpbuf, retbuf, sizeof(retbuf));
2020-03-29 09:16:53 +00:00
}
/** conv_param to deal with stacked extbans.
*/
2022-01-15 05:16:34 +00:00
const char *extban_conv_param_nuh_or_extban(BanContext *b, Extban *self_extban)
2020-03-29 09:16:53 +00:00
{
#if (USERLEN + NICKLEN + HOSTLEN + 32) > 256
#error "wtf?"
#endif
static char retbuf[256];
static char printbuf[256];
char *mask;
char tmpbuf[USERLEN + NICKLEN + HOSTLEN + 32];
2022-01-15 05:16:34 +00:00
const char *ret = NULL;
const char *nextbanstr;
Extban *extban = NULL;
2020-03-29 09:16:53 +00:00
static int extban_recursion = 0;
2022-01-15 05:16:34 +00:00
if (!is_extended_ban(b->banstr))
return extban_conv_param_nuh(b, self_extban);
/* We're dealing with a stacked extended ban.
* Rules:
* 1) You can only stack once, so: ~x:~y:something and not ~x:~y:~z...
* 2) The second item may never be an action modifier, nor have the
* EXTBOPT_NOSTACKCHILD letter set (for things like a textban).
*/
/* Rule #1. Yes the recursion check is also in extban_is_ok_nuh_extban,
* but it's possible to get here without the is_ok() function ever
* being called (think: non-local client). And no, don't delete it
* there either. It needs to be in BOTH places. -- Syzop
*/
if (extban_recursion)
return NULL;
strlcpy(tmpbuf, b->banstr, sizeof(tmpbuf));
extban = findmod_by_bantype(tmpbuf, &nextbanstr);
if (!extban)
2020-03-29 09:16:53 +00:00
{
2022-01-15 05:16:34 +00:00
/* Handling unknown bantypes in is_ok. Assume that it's ok here. */
return b->banstr;
}
2020-03-29 09:16:53 +00:00
2022-01-15 05:16:34 +00:00
b->banstr = nextbanstr;
if ((extban->options & EXTBOPT_ACTMODIFIER) || (extban->options & EXTBOPT_NOSTACKCHILD))
{
/* Rule #2 violation */
return NULL;
2020-03-29 09:16:53 +00:00
}
2022-01-15 05:16:34 +00:00
2023-05-05 22:12:01 +00:00
extban_recursion++;
ret = extban->conv_param(b, extban);
extban_recursion--;
ret = prefix_with_extban(ret, b, extban, retbuf, sizeof(retbuf));
return ret;
2022-01-15 05:16:34 +00:00
}
char *prefix_with_extban(const char *remainder, BanContext *b, Extban *extban, char *buf, size_t buflen)
{
/* Yes, we support this because it makes code at the caller cleaner */
if (remainder == NULL)
return NULL;
if (iConf.named_extended_bans && !(b->conv_options & BCTX_CONV_OPTION_WRITE_LETTER_BANS))
snprintf(buf, buflen, "~%s:%s", extban->name, remainder);
else
snprintf(buf, buflen, "~%c:%s", extban->letter, remainder);
return buf;
2020-03-29 09:16:53 +00:00
}