2015-12-10 09:20:58 +01:00
|
|
|
/*
|
|
|
|
* Extban that combines other extbans.
|
|
|
|
*
|
|
|
|
* Basic example:
|
|
|
|
* $&:~a,m:*!*@gateway/web/cgi-irc*
|
|
|
|
* Which means: match unidentified webchat users.
|
|
|
|
* ("m" is another new extban type, which just does a normal match).
|
|
|
|
*
|
|
|
|
* More complicated example:
|
|
|
|
* $&:~a,|:(m:*!*@gateway/web/foo,m:*!*@gateway/web/bar)
|
|
|
|
* Which means: unidentified and using the foo or bar gateway.
|
|
|
|
*
|
|
|
|
* Rules:
|
|
|
|
*
|
|
|
|
* - Optional pair of parens around data.
|
|
|
|
*
|
|
|
|
* - component bans are separated by commas, but commas between
|
|
|
|
* matching pairs of parens are skipped.
|
|
|
|
*
|
|
|
|
* - Unbalanced parens are an error.
|
|
|
|
*
|
|
|
|
* - Parens, commas and backslashes can be escaped by backslashes.
|
|
|
|
*
|
|
|
|
* - A backslash before any character other than a paren or backslash
|
|
|
|
* is just a backslash (backslash and character are both used).
|
|
|
|
*
|
|
|
|
* - Non-existant extbans are invalid.
|
|
|
|
* This is primarily for consistency with non-combined bans:
|
|
|
|
* the ircd does not let you set +b $f unless the 'f' extban is loaded,
|
|
|
|
* so setting $&:f should be impossible too.
|
|
|
|
*
|
|
|
|
* Issues:
|
|
|
|
* - Backslashes double inside nested bans.
|
|
|
|
* Hopefully acceptable because they should be rare.
|
|
|
|
*
|
|
|
|
* - Is performance good enough?
|
|
|
|
* I suspect it is, but have done no load testing.
|
|
|
|
*/
|
|
|
|
|
2016-08-18 07:33:38 +02:00
|
|
|
namespace mode = ircd::chan::mode;
|
2016-08-17 05:01:20 +02:00
|
|
|
namespace ext = mode::ext;
|
2016-08-13 05:05:54 +02:00
|
|
|
using namespace ircd;
|
|
|
|
|
2016-03-09 08:29:41 +01:00
|
|
|
static const char extb_desc[] = "Combination ($&, $|) extban types";
|
|
|
|
|
|
|
|
// #define MOD_DEBUG(s) sendto_realops_snomask(SNO_DEBUG, L_NETWIDE, (s))
|
|
|
|
#define MOD_DEBUG(s)
|
2016-08-17 05:01:20 +02:00
|
|
|
#define RETURN_INVALID { recursion_depth--; return INVALID; }
|
2015-12-10 09:20:58 +01:00
|
|
|
|
|
|
|
static int _modinit(void);
|
|
|
|
static void _moddeinit(void);
|
2016-08-22 03:57:43 +02:00
|
|
|
static int eb_or(const char *data, client::client *client_p, chan::chan *chptr, mode::type);
|
|
|
|
static int eb_and(const char *data, client::client *client_p, chan::chan *chptr, mode::type);
|
|
|
|
static int eb_combi(const char *data, client::client *client_p, chan::chan *chptr, mode::type, bool is_and);
|
2015-12-13 14:49:19 +01:00
|
|
|
static int recursion_depth = 0;
|
2015-12-10 09:20:58 +01:00
|
|
|
|
2016-03-07 07:31:41 +01:00
|
|
|
DECLARE_MODULE_AV2(extb_extended, _modinit, _moddeinit, NULL, NULL, NULL, NULL, NULL, extb_desc);
|
2015-12-10 09:20:58 +01:00
|
|
|
|
|
|
|
static int
|
|
|
|
_modinit(void)
|
|
|
|
{
|
2016-08-17 05:01:20 +02:00
|
|
|
ext::table['&'] = eb_and;
|
|
|
|
ext::table['|'] = eb_or;
|
2015-12-10 09:20:58 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_moddeinit(void)
|
|
|
|
{
|
2016-08-17 05:01:20 +02:00
|
|
|
ext::table['&'] = NULL;
|
|
|
|
ext::table['|'] = NULL;
|
2015-12-10 09:20:58 +01:00
|
|
|
}
|
|
|
|
|
2016-08-22 03:57:43 +02:00
|
|
|
static int eb_or(const char *data, client::client *client_p,
|
2016-08-18 07:33:38 +02:00
|
|
|
chan::chan *chptr, mode::type type)
|
2015-12-10 09:20:58 +01:00
|
|
|
{
|
2016-08-17 05:01:20 +02:00
|
|
|
return eb_combi(data, client_p, chptr, type, false);
|
2015-12-10 09:20:58 +01:00
|
|
|
}
|
|
|
|
|
2016-08-22 03:57:43 +02:00
|
|
|
static int eb_and(const char *data, client::client *client_p,
|
2016-08-18 07:33:38 +02:00
|
|
|
chan::chan *chptr, mode::type type)
|
2015-12-10 09:20:58 +01:00
|
|
|
{
|
2016-08-17 05:01:20 +02:00
|
|
|
return eb_combi(data, client_p, chptr, type, true);
|
2015-12-10 09:20:58 +01:00
|
|
|
}
|
|
|
|
|
2016-08-22 03:57:43 +02:00
|
|
|
static int eb_combi(const char *data, client::client *client_p,
|
2016-08-18 07:33:38 +02:00
|
|
|
chan::chan *chptr, mode::type type, bool is_and)
|
2015-12-10 09:20:58 +01:00
|
|
|
{
|
2016-08-17 05:01:20 +02:00
|
|
|
using namespace ext;
|
|
|
|
|
2015-12-10 09:20:58 +01:00
|
|
|
const char *p, *banend;
|
2016-03-26 00:47:30 +01:00
|
|
|
bool have_result = false;
|
2015-12-13 18:38:27 +01:00
|
|
|
int allowed_nodes = 11;
|
2015-12-10 09:20:58 +01:00
|
|
|
size_t datalen;
|
|
|
|
|
2015-12-13 18:38:27 +01:00
|
|
|
if (recursion_depth >= 5) {
|
2016-03-09 08:29:41 +01:00
|
|
|
MOD_DEBUG("combo invalid: recursion depth too high");
|
2016-08-17 05:01:20 +02:00
|
|
|
return INVALID;
|
2015-12-13 14:49:19 +01:00
|
|
|
}
|
|
|
|
|
2015-12-10 09:20:58 +01:00
|
|
|
if (EmptyString(data)) {
|
2016-03-09 08:29:41 +01:00
|
|
|
MOD_DEBUG("combo invalid: empty data");
|
2016-08-17 05:01:20 +02:00
|
|
|
return INVALID;
|
2015-12-10 09:20:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
datalen = strlen(data);
|
2016-08-18 07:33:38 +02:00
|
|
|
if (datalen > chan::ban::LEN) {
|
2015-12-10 09:20:58 +01:00
|
|
|
/* I'd be sad if this ever happened, but if it does we
|
|
|
|
* could overflow the buffer used below, so...
|
|
|
|
*/
|
2016-03-09 08:29:41 +01:00
|
|
|
MOD_DEBUG("combo invalid: > BANLEN");
|
2016-08-17 05:01:20 +02:00
|
|
|
return INVALID;
|
2015-12-10 09:20:58 +01:00
|
|
|
}
|
|
|
|
banend = data + datalen;
|
|
|
|
|
|
|
|
if (data[0] == '(') {
|
|
|
|
p = data + 1;
|
|
|
|
banend--;
|
|
|
|
if (*banend != ')') {
|
2016-03-09 08:29:41 +01:00
|
|
|
MOD_DEBUG("combo invalid: starting but no closing paren");
|
2016-08-17 05:01:20 +02:00
|
|
|
return INVALID;
|
2015-12-10 09:20:58 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Empty combibans are invalid. */
|
|
|
|
if (banend == p) {
|
2016-03-09 08:29:41 +01:00
|
|
|
MOD_DEBUG("combo invalid: no data (after removing parens)");
|
2016-08-17 05:01:20 +02:00
|
|
|
return INVALID;
|
2015-12-10 09:20:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Implementation note:
|
|
|
|
* I want it to be impossible to set a syntactically invalid combi-ban.
|
|
|
|
* (mismatched parens).
|
|
|
|
* That is: valid_extban should return false for those.
|
|
|
|
* Ideally we do not parse the entire ban when actually matching it:
|
|
|
|
* we can just short-circuit if we already know the ban is valid.
|
|
|
|
* Unfortunately there is no separate hook or mode_type for validation,
|
|
|
|
* so we always keep parsing even after we have determined a result.
|
|
|
|
*/
|
|
|
|
|
2015-12-13 14:49:19 +01:00
|
|
|
recursion_depth++;
|
|
|
|
|
2015-12-13 17:58:05 +01:00
|
|
|
while (--allowed_nodes) {
|
2016-03-26 00:47:30 +01:00
|
|
|
bool invert = false;
|
2016-08-18 07:33:38 +02:00
|
|
|
char *child_data, child_data_buf[chan::ban::LEN];
|
2016-08-17 05:01:20 +02:00
|
|
|
ext::func f;
|
2015-12-10 09:20:58 +01:00
|
|
|
|
|
|
|
if (*p == '~') {
|
2016-03-26 00:47:30 +01:00
|
|
|
invert = true;
|
2015-12-10 09:20:58 +01:00
|
|
|
p++;
|
|
|
|
if (p == banend) {
|
2016-03-09 08:29:41 +01:00
|
|
|
MOD_DEBUG("combo invalid: no data after ~");
|
2015-12-13 14:49:19 +01:00
|
|
|
RETURN_INVALID;
|
2015-12-10 09:20:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-17 05:01:20 +02:00
|
|
|
f = ext::table[uint8_t(*p++)];
|
2015-12-10 09:20:58 +01:00
|
|
|
if (!f) {
|
2016-03-09 08:29:41 +01:00
|
|
|
MOD_DEBUG("combo invalid: non-existant child extban");
|
2015-12-13 14:49:19 +01:00
|
|
|
RETURN_INVALID;
|
2015-12-10 09:20:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (*p == ':') {
|
|
|
|
unsigned int parencount = 0;
|
2016-03-26 00:47:30 +01:00
|
|
|
bool escaped = false, done = false;
|
2015-12-10 09:20:58 +01:00
|
|
|
char *o;
|
|
|
|
|
|
|
|
p++;
|
|
|
|
|
|
|
|
/* Possible optimization: we can skip the actual copy if
|
|
|
|
* we already have_result.
|
|
|
|
*/
|
|
|
|
o = child_data = child_data_buf;
|
2016-03-26 00:47:30 +01:00
|
|
|
while (true) {
|
2015-12-10 09:20:58 +01:00
|
|
|
if (p == banend) {
|
|
|
|
if (parencount) {
|
2016-03-09 08:29:41 +01:00
|
|
|
MOD_DEBUG("combo invalid: EOD while in parens");
|
2015-12-13 14:49:19 +01:00
|
|
|
RETURN_INVALID;
|
2015-12-10 09:20:58 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (escaped) {
|
|
|
|
if (*p != '(' && *p != ')' && *p != '\\' && *p != ',')
|
|
|
|
*o++ = '\\';
|
|
|
|
*o++ = *p++;
|
2016-03-26 00:47:30 +01:00
|
|
|
escaped = false;
|
2015-12-10 09:20:58 +01:00
|
|
|
} else {
|
|
|
|
switch (*p) {
|
|
|
|
case '\\':
|
2016-03-26 00:47:30 +01:00
|
|
|
escaped = true;
|
2015-12-10 09:20:58 +01:00
|
|
|
break;
|
|
|
|
case '(':
|
|
|
|
parencount++;
|
|
|
|
*o++ = *p;
|
|
|
|
break;
|
|
|
|
case ')':
|
|
|
|
if (!parencount) {
|
2016-03-09 08:29:41 +01:00
|
|
|
MOD_DEBUG("combo invalid: negative parencount");
|
2015-12-13 14:49:19 +01:00
|
|
|
RETURN_INVALID;
|
2015-12-10 09:20:58 +01:00
|
|
|
}
|
|
|
|
parencount--;
|
|
|
|
*o++ = *p;
|
|
|
|
break;
|
|
|
|
case ',':
|
|
|
|
if (parencount)
|
|
|
|
*o++ = *p;
|
|
|
|
else
|
2016-03-26 00:47:30 +01:00
|
|
|
done = true;
|
2015-12-10 09:20:58 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*o++ = *p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (done)
|
|
|
|
break;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*o = '\0';
|
|
|
|
} else {
|
|
|
|
child_data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!have_result) {
|
2016-08-17 05:01:20 +02:00
|
|
|
int child_result = f(child_data, client_p, chptr, type);
|
2015-12-10 09:20:58 +01:00
|
|
|
|
2016-08-17 05:01:20 +02:00
|
|
|
if (child_result == INVALID) {
|
2016-03-09 08:29:41 +01:00
|
|
|
MOD_DEBUG("combo invalid: child invalid");
|
2015-12-13 14:49:19 +01:00
|
|
|
RETURN_INVALID;
|
2015-12-10 09:20:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert child_result to a plain boolean result */
|
|
|
|
if (invert)
|
2016-08-17 05:01:20 +02:00
|
|
|
child_result = child_result == NOMATCH;
|
2015-12-10 09:20:58 +01:00
|
|
|
else
|
2016-08-17 05:01:20 +02:00
|
|
|
child_result = child_result == MATCH;
|
2015-12-10 09:20:58 +01:00
|
|
|
|
|
|
|
if (is_and ? !child_result : child_result)
|
2016-03-26 00:47:30 +01:00
|
|
|
have_result = true;
|
2015-12-10 09:20:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (p == banend)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (*p++ != ',') {
|
2016-03-09 08:29:41 +01:00
|
|
|
MOD_DEBUG("combo invalid: no ',' after ban");
|
2015-12-13 14:49:19 +01:00
|
|
|
RETURN_INVALID;
|
2015-12-10 09:20:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (p == banend) {
|
2016-03-09 08:29:41 +01:00
|
|
|
MOD_DEBUG("combo invalid: banend after ','");
|
2015-12-13 14:49:19 +01:00
|
|
|
RETURN_INVALID;
|
2015-12-10 09:20:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-13 18:06:04 +01:00
|
|
|
/* at this point, *p should == banend */
|
|
|
|
if (p != banend) {
|
2016-03-09 08:29:41 +01:00
|
|
|
MOD_DEBUG("combo invalid: more child extbans than allowed");
|
2015-12-13 18:06:04 +01:00
|
|
|
RETURN_INVALID;
|
|
|
|
}
|
|
|
|
|
2015-12-13 14:49:19 +01:00
|
|
|
recursion_depth--;
|
|
|
|
|
2015-12-10 09:20:58 +01:00
|
|
|
if (is_and)
|
2016-08-17 05:01:20 +02:00
|
|
|
return have_result ? NOMATCH : MATCH;
|
2015-12-10 09:20:58 +01:00
|
|
|
else
|
2016-08-17 05:01:20 +02:00
|
|
|
return have_result ? MATCH : NOMATCH;
|
2015-12-10 09:20:58 +01:00
|
|
|
}
|