diff options
| author | viper viper@31f1291d-b8d6-0310-a050-a5561fc1590b <viper viper@31f1291d-b8d6-0310-a050-a5561fc1590b@5417fbe8-f217-4b02-8779-1006273d7864> | 2008-09-03 20:11:12 +0000 |
|---|---|---|
| committer | viper viper@31f1291d-b8d6-0310-a050-a5561fc1590b <viper viper@31f1291d-b8d6-0310-a050-a5561fc1590b@5417fbe8-f217-4b02-8779-1006273d7864> | 2008-09-03 20:11:12 +0000 |
| commit | 790dc8a4bf326a7598fc41aa8c018e867edee33a (patch) | |
| tree | 281aa0f14877734a92336a21355eb945a8d1517e | |
| parent | 482a8aa766c9e1a96ff63b44ed319fe51588c35c (diff) | |
BUILD : 1.7.21 (1426) BUGS : 876 NOTES : We now support CIDR in channel ban/invite/except lists. Introduces new CIDR capable generic lists system.
git-svn-id: svn://svn.anope.org/anope/trunk@1426 31f1291d-b8d6-0310-a050-a5561fc1590b
git-svn-id: http://anope.svn.sourceforge.net/svnroot/anope/trunk@1141 5417fbe8-f217-4b02-8779-1006273d7864
| -rw-r--r-- | Changes | 1 | ||||
| -rw-r--r-- | include/extern.h | 21 | ||||
| -rw-r--r-- | include/services.h | 39 | ||||
| -rw-r--r-- | src/actions.c | 57 | ||||
| -rw-r--r-- | src/botserv.c | 33 | ||||
| -rw-r--r-- | src/channels.c | 592 | ||||
| -rw-r--r-- | src/chanserv.c | 25 | ||||
| -rw-r--r-- | src/core/cs_clear.c | 87 | ||||
| -rw-r--r-- | src/core/os_clearmodes.c | 79 | ||||
| -rw-r--r-- | src/misc.c | 168 | ||||
| -rw-r--r-- | src/protocol/bahamut.c | 3 | ||||
| -rw-r--r-- | src/protocol/charybdis.c | 9 | ||||
| -rw-r--r-- | src/protocol/dreamforge.c | 3 | ||||
| -rw-r--r-- | src/protocol/hybrid.c | 7 | ||||
| -rw-r--r-- | src/protocol/inspircd10.c | 1 | ||||
| -rw-r--r-- | src/protocol/inspircd11.c | 1 | ||||
| -rw-r--r-- | src/protocol/plexus2.c | 3 | ||||
| -rw-r--r-- | src/protocol/plexus3.c | 3 | ||||
| -rw-r--r-- | src/protocol/ptlink.c | 3 | ||||
| -rw-r--r-- | src/protocol/rageircd.c | 3 | ||||
| -rw-r--r-- | src/protocol/ratbox.c | 3 | ||||
| -rw-r--r-- | src/protocol/shadowircd.c | 3 | ||||
| -rw-r--r-- | src/protocol/solidircd.c | 3 | ||||
| -rw-r--r-- | src/protocol/ultimate2.c | 1 | ||||
| -rw-r--r-- | src/protocol/ultimate3.c | 3 | ||||
| -rw-r--r-- | src/protocol/unreal31.c | 1 | ||||
| -rw-r--r-- | src/protocol/unreal32.c | 3 | ||||
| -rw-r--r-- | src/protocol/viagra.c | 1 | ||||
| -rw-r--r-- | src/users.c | 50 | ||||
| -rw-r--r-- | version.log | 6 |
30 files changed, 860 insertions, 352 deletions
@@ -3,6 +3,7 @@ Anope Version S V N 01/24 A Added a check for hosts too long in BotServs bots. [ #00] 02/12 A Added SUSPENDED to NS LIST keywords. [#869] 02/20 A Added NS SASET LANGUAGE. [#872] +09/03 A Added CIDR support in channel bans/excepts. [#876] 01/15 F BOT_NOT_ASSIGNED language string error. [#828] 01/15 F listchans now shows if a channel is suspended. [#825] 01/15 F listnicks now shows if a nickname is suspended. [#825] diff --git a/include/extern.h b/include/extern.h index c85600751..4284b09de 100644 --- a/include/extern.h +++ b/include/extern.h @@ -141,6 +141,19 @@ E void do_mass_mode(char *modes); E void chan_set_correct_modes(User * user, Channel * c, int give_modes); E void restore_unsynced_topics(void); +E Entry *entry_create(char *mask); +E Entry *entry_add(EList *list, char *mask); +E void entry_delete(EList *list, Entry *e); +E EList *list_create(); +E int entry_match(Entry *e, char *nick, char *user, char *host, uint32 ip); +E int entry_match_mask(Entry *e, char *mask, uint32 ip); +E Entry *elist_match(EList *list, char *nick, char *user, char *host, uint32 ip); +E Entry *elist_match_mask(EList *list, char *mask, uint32 ip); +E Entry *elist_match_user(EList *list, User *u); +E Entry *elist_find_mask(EList *list, char *mask); +E long get_memuse(EList *list); + + #define whosends(ci) ((!(ci) || !((ci)->botflags & BS_SYMBIOSIS) || !(ci)->bi || !(ci)->c || (ci)->c->usercount < BSMinUsers) ? s_ChanServ : (ci)->bi->nick) /**** chanserv.c ****/ @@ -862,6 +875,14 @@ E void ntoa(struct in_addr addr, char *ipaddr, int len); E char **buildStringList(char *src, int *number); E void binary_to_hex(unsigned char *bin, char *hex, int length); +E uint32 cidr_to_netmask(uint16 cidr); +E uint16 netmask_to_cidr(uint32 mask); + +E int str_is_wildcard(const char *str); +E int str_is_pure_wildcard(const char *str); + +E uint32 str_is_ip(char *str); +E int str_is_cidr(char *str, uint32 * ip, uint32 * mask, char **host); /**** modules.c ****/ diff --git a/include/services.h b/include/services.h index 48920b201..3d8ee2be1 100644 --- a/include/services.h +++ b/include/services.h @@ -217,6 +217,8 @@ extern int shutdown(int, int); typedef struct server_ Server; typedef struct user_ User; typedef struct channel_ Channel; +typedef struct c_elist EList; +typedef struct c_elist_entry Entry; typedef struct ModuleData_ ModuleData; /* ModuleData struct */ typedef struct memo_ Memo; typedef struct nickrequest_ NickRequest; @@ -356,12 +358,15 @@ struct ircdvars_ { int p10; /* ircd is P10 */ char *nickchars; /* character set */ int sync; /* reports sync state */ + int cidrchanbei; /* channel bans/excepts/invites support CIDR (syntax: +b *!*@192.168.0.0/15) + * 0 for no support, 1 for strict cidr support, anything else + * for ircd specific support (nefarious only cares about first /mask) */ }; struct ircdcapab_ { uint32 noquit; uint32 tsmode; - uint32 unconnect; + uint32 unconnect; uint32 nickip; uint32 nsjoin; uint32 zip; @@ -962,6 +967,16 @@ struct userdata_ { int16 times; }; +/* Channelban type flags */ +#define ENTRYTYPE_NONE 0x00000000 +#define ENTRYTYPE_CIDR4 0x00000001 +#define ENTRYTYPE_NICK_WILD 0x00000004 +#define ENTRYTYPE_NICK 0x00000008 +#define ENTRYTYPE_USER_WILD 0x00000010 +#define ENTRYTYPE_USER 0x00000020 +#define ENTRYTYPE_HOST_WILD 0x00000040 +#define ENTRYTYPE_HOST 0x00000080 + struct channel_ { Channel *next, *prev; char name[CHANMAX]; @@ -975,12 +990,9 @@ struct channel_ { char *key; /* NULL if none */ char *redirect; /* +L; NULL if none */ char *flood; /* +f; NULL if none */ - int32 bancount, bansize; - char **bans; - int32 exceptcount, exceptsize; - char **excepts; - int32 invitecount, invitesize; - char **invite; + EList *bans; + EList *excepts; + EList *invites; struct c_userlist { struct c_userlist *next, *prev; User *user; @@ -998,6 +1010,19 @@ struct channel_ { int16 topic_sync; /* Is the topic in sync? */ }; +struct c_elist { + Entry *entries; + int32 count; +}; + +struct c_elist_entry { + Entry *next, *prev; + uint32 type; + uint32 cidr_ip; /* IP mask for CIDR matching */ + uint32 cidr_mask; /* Netmask for CIDR matching */ + char *nick, *user, *host, *mask; +}; + /*************************************************************************/ /* Constants for news types. */ diff --git a/src/actions.c b/src/actions.c index d7c1ecda1..acf945420 100644 --- a/src/actions.c +++ b/src/actions.c @@ -130,13 +130,13 @@ void sqline(char *mask, char *reason) */ void common_unban(ChannelInfo * ci, char *nick) { - int count, i; - char *av[4], **bans; - int ac; + char *av[4]; + char *host = NULL; char buf[BUFSIZE]; + int ac; + uint32 ip = 0; User *u; - char *host = NULL; - int matchfound = 0; + Entry *ban, *next; if (!ci || !ci->c || !nick) { return; @@ -146,6 +146,9 @@ void common_unban(ChannelInfo * ci, char *nick) return; } + if (!ci->c->bans || (ci->c->bans->count == 0)) + return; + if (u->hostip == NULL) { host = host_resolve(u->host); /* we store the just resolved hostname so we don't @@ -156,6 +159,9 @@ void common_unban(ChannelInfo * ci, char *nick) } else { host = sstrdup(u->hostip); } + /* Convert the host to an IP.. */ + if (host) + ip = str_is_ip(host); if (ircd->svsmode_unban) { anope_cmd_unban(ci->name, nick); @@ -171,44 +177,21 @@ void common_unban(ChannelInfo * ci, char *nick) av[1] = sstrdup("-b"); ac = 3; } - count = ci->c->bancount; - bans = scalloc(sizeof(char *) * count, 1); - memcpy(bans, ci->c->bans, sizeof(char *) * count); - for (i = 0; i < count; i++) { - if (match_usermask(bans[i], u)) { - anope_cmd_mode(whosends(ci), ci->name, "-b %s", bans[i]); + + for (ban = ci->c->bans->entries; ban; ban = next) { + next = ban->next; + if (entry_match(ban, u->nick, u->username, u->host, ip) || + entry_match(ban, u->nick, u->username, u->vhost, ip)) { + anope_cmd_mode(whosends(ci), ci->name, "-b %s", ban->mask); if (ircdcap->tsmode) - av[3] = bans[i]; + av[3] = ban->mask; else - av[2] = bans[i]; + av[2] = ban->mask; do_cmode(whosends(ci), ac, av); - matchfound++; } - if (host) { - /* prevent multiple unbans if the first one was successful in - locating the ban for us. This is due to match_userip() checks - the vhost again, and thus can return false positive results - for the function. but won't prevent thus from clearing out - the bans against an IP address since the first would fail and - the second would match - TSL - */ - if (!matchfound) { - if (match_userip(bans[i], u, host)) { - anope_cmd_mode(whosends(ci), ci->name, "-b %s", - bans[i]); - if (ircdcap->tsmode) - av[3] = bans[i]; - else - av[2] = bans[i]; - - do_cmode(whosends(ci), ac, av); - } - } - } - matchfound = 0; } - free(bans); + if (ircdcap->tsmode) free(av[2]); else diff --git a/src/botserv.c b/src/botserv.c index 57719a003..f8eea632e 100644 --- a/src/botserv.c +++ b/src/botserv.c @@ -757,25 +757,17 @@ static UserData *get_user_data(Channel * c, User * u) void bot_join(ChannelInfo * ci) { - int i; - if (!ci || !ci->c || !ci->bi) return; if (BSSmartJoin) { /* We check for bans */ - int count = ci->c->bancount; - if (count) { - char botmask[BUFSIZE]; + if (ci->c->bans && ci->c->bans->count) { char buf[BUFSIZE]; - char **bans = scalloc(sizeof(char *) * count, 1); char *av[4]; + Entry *ban, *next; int ac; - memcpy(bans, ci->c->bans, sizeof(char *) * count); - snprintf(botmask, sizeof(botmask), "%s!%s@%s", ci->bi->nick, - ci->bi->user, ci->bi->host); - if (ircdcap->tsmode) { snprintf(buf, BUFSIZE - 1, "%ld", (long int) time(NULL)); av[0] = ci->c->name; @@ -788,21 +780,16 @@ void bot_join(ChannelInfo * ci) ac = 3; } - for (i = 0; i < count; i++) { - if (match_wild_nocase(ci->c->bans[i], botmask)) { - anope_cmd_mode(ci->bi->nick, ci->name, "-b %s", - bans[i]); + for (ban = ci->c->bans->entries; ban; ban = next) { + next = ban->next; + if (entry_match(ban, ci->bi->nick, ci->bi->user, ci->bi->host, 0)) { + anope_cmd_mode(whosends(ci), ci->name, "-b %s", ban->mask); if (ircdcap->tsmode) - av[3] = sstrdup(bans[i]); + av[3] = ban->mask; else - av[2] = sstrdup(bans[i]); - - do_cmode(ci->bi->nick, ac, av); + av[2] = ban->mask; - if (ircdcap->tsmode) - free(av[3]); - else - free(av[2]); + do_cmode(whosends(ci), ac, av); } } @@ -810,8 +797,6 @@ void bot_join(ChannelInfo * ci) free(av[2]); else free(av[1]); - - free(bans); } /* Should we be invited? */ diff --git a/src/channels.c b/src/channels.c index 297c8b8ec..89beeadc7 100644 --- a/src/channels.c +++ b/src/channels.c @@ -427,7 +427,7 @@ void get_channel_stats(long *nrec, long *memuse) Channel *chan; struct c_userlist *cu; BanData *bd; - int i, j; + int i; for (i = 0; i < 1024; i++) { for (chan = chanlist[i]; chan; chan = chan->next) { @@ -445,24 +445,12 @@ void get_channel_stats(long *nrec, long *memuse) if (chan->redirect) mem += strlen(chan->redirect) + 1; } - mem += sizeof(char *) * chan->bansize; - for (j = 0; j < chan->bancount; j++) { - if (chan->bans[j]) - mem += strlen(chan->bans[j]) + 1; - } + mem += get_memuse(chan->bans); if (ircd->except) { - mem += sizeof(char *) * chan->exceptsize; - for (j = 0; j < chan->exceptcount; j++) { - if (chan->excepts[j]) - mem += strlen(chan->excepts[j]) + 1; - } + mem += get_memuse(chan->excepts); } if (ircd->invitemode) { - mem += sizeof(char *) * chan->invitesize; - for (j = 0; j < chan->invitecount; j++) { - if (chan->invite[j]) - mem += strlen(chan->invite[j]) + 1; - } + mem += get_memuse(chan->invites); } for (cu = chan->users; cu; cu = cu->next) { mem += sizeof(*cu); @@ -1270,6 +1258,7 @@ void do_topic(const char *source, int ac, char **av) void add_ban(Channel * chan, char *mask) { + Entry *ban; /* check for NULL values otherwise we will segfault */ if (!chan || !mask) { if (debug) { @@ -1278,25 +1267,28 @@ void add_ban(Channel * chan, char *mask) return; } + /* Check if the list already exists, if not create it. + * Create a new ban and add it to the list.. ~ Viper */ + if (!chan->bans) + chan->bans = list_create(); + ban = entry_add(chan->bans, mask); + + if (!ban) + fatal("Creating new ban entry failed"); + + /* Check whether it matches a botserv bot after adding internally + * and parsing it through cidr support. ~ Viper */ if (s_BotServ && BSSmartJoin && chan->ci && chan->ci->bi && chan->usercount >= BSMinUsers) { - char botmask[BUFSIZE]; BotInfo *bi = chan->ci->bi; - snprintf(botmask, sizeof(botmask), "%s!%s@%s", bi->nick, bi->user, - bi->host); - if (match_wild_nocase(mask, botmask)) { + if (entry_match(ban, bi->nick, bi->user, bi->host, 0)) { anope_cmd_mode(bi->nick, chan->name, "-b %s", mask); + entry_delete(chan->bans, ban); return; } } - if (chan->bancount >= chan->bansize) { - chan->bansize += 8; - chan->bans = srealloc(chan->bans, sizeof(char *) * chan->bansize); - } - chan->bans[chan->bancount++] = sstrdup(mask); - if (debug) alog("debug: Added ban %s to channel %s", mask, chan->name); } @@ -1305,18 +1297,22 @@ void add_ban(Channel * chan, char *mask) void add_exception(Channel * chan, char *mask) { + Entry *exception; + if (!chan || !mask) { if (debug) alog("debug: add_exception called with NULL values"); return; } - if (chan->exceptcount >= chan->exceptsize) { - chan->exceptsize += 8; - chan->excepts = - srealloc(chan->excepts, sizeof(char *) * chan->exceptsize); - } - chan->excepts[chan->exceptcount++] = sstrdup(mask); + /* Check if the list already exists, if not create it. + * Create a new exception and add it to the list.. ~ Viper */ + if (!chan->excepts) + chan->excepts = list_create(); + exception = entry_add(chan->excepts, mask); + + if (!exception) + fatal("Creating new exception entry failed"); if (debug) alog("debug: Added except %s to channel %s", mask, chan->name); @@ -1326,18 +1322,22 @@ void add_exception(Channel * chan, char *mask) void add_invite(Channel * chan, char *mask) { + Entry *invite; + if (!chan || !mask) { if (debug) alog("debug: add_invite called with NULL values"); return; } - if (chan->invitecount >= chan->invitesize) { - chan->invitesize += 8; - chan->invite = - srealloc(chan->invite, sizeof(char *) * chan->invitesize); - } - chan->invite[chan->invitecount++] = sstrdup(mask); + /* Check if the list already exists, if not create it. + * Create a new invite and add it to the list.. ~ Viper */ + if (!chan->invites) + chan->invites = list_create(); + invite = entry_add(chan->invites, mask); + + if (!invite) + fatal("Creating new exception entry failed"); if (debug) alog("debug: Added invite %s to channel %s", mask, chan->name); @@ -1621,7 +1621,6 @@ Channel *chan_create(char *chan, time_t ts) void chan_delete(Channel * c) { BanData *bd, *next; - int i; if (debug) alog("debug: Deleting channel %s", c->name); @@ -1650,37 +1649,26 @@ void chan_delete(Channel * c) free(c->redirect); } - for (i = 0; i < c->bancount; ++i) { - if (c->bans[i]) - free(c->bans[i]); - else - alog("channel: BUG freeing %s: bans[%d] is NULL!", c->name, i); + if (c->bans && c->bans->count) { + while (c->bans->entries) { + entry_delete(c->bans, c->bans->entries); + } } - if (c->bansize) - free(c->bans); if (ircd->except) { - for (i = 0; i < c->exceptcount; ++i) { - if (c->excepts[i]) - free(c->excepts[i]); - else - alog("channel: BUG freeing %s: excepts[%d] is NULL!", - c->name, i); + if (c->excepts && c->excepts->count) { + while (c->excepts->entries) { + entry_delete(c->excepts, c->excepts->entries); + } } - if (c->exceptsize) - free(c->excepts); } if (ircd->invitemode) { - for (i = 0; i < c->invitecount; ++i) { - if (c->invite[i]) - free(c->invite[i]); - else - alog("channel: BUG freeing %s: invite[%d] is NULL!", - c->name, i); + if (c->invites && c->invites->count) { + while (c->invites->entries) { + entry_delete(c->invites, c->invites->entries); + } } - if (c->invitesize) - free(c->invite); } if (c->next) @@ -1697,28 +1685,20 @@ void chan_delete(Channel * c) void del_ban(Channel * chan, char *mask) { - char **s = chan->bans; - int i = 0; AutoKick *akick; + Entry *ban; /* Sanity check as it seems some IRCD will just send -b without a mask */ - if (!mask) { + if (!mask || !chan->bans || (chan->bans->count == 0)) return; - } - while (i < chan->bancount && strcmp(*s, mask) != 0) { - i++; - s++; - } + ban = elist_find_mask(chan->bans, mask); - if (i < chan->bancount) { - chan->bancount--; - if (i < chan->bancount) - memmove(s, s + 1, sizeof(char *) * (chan->bancount - i)); + if (ban) { + entry_delete(chan->bans, ban); if (debug) - alog("debug: Deleted ban %s from channel %s", mask, - chan->name); + alog("debug: Deleted ban %s from channel %s", mask, chan->name); } if (chan->ci && (akick = is_stuck(chan->ci, mask))) @@ -1729,58 +1709,41 @@ void del_ban(Channel * chan, char *mask) void del_exception(Channel * chan, char *mask) { - int i; - int reset = 0; + Entry *exception; /* Sanity check as it seems some IRCD will just send -e without a mask */ - if (!mask) { + if (!mask|| !chan->excepts || (chan->excepts->count == 0)) return; - } - for (i = 0; i < chan->exceptcount; i++) { - if ((!reset) && (stricmp(chan->excepts[i], mask) == 0)) { - free(chan->excepts[i]); - reset = 1; - } - if (reset) - chan->excepts[i] = - (i == chan->exceptcount) ? NULL : chan->excepts[i + 1]; - } + exception = elist_find_mask(chan->excepts, mask); - if (reset) - chan->exceptcount--; + if (exception) { + entry_delete(chan->excepts, exception); - if (debug) - alog("debug: Deleted except %s to channel %s", mask, chan->name); + if (debug) + alog("debug: Deleted except %s to channel %s", mask, chan->name); + } } /*************************************************************************/ void del_invite(Channel * chan, char *mask) { - int i; - int reset = 0; + Entry *invite; /* Sanity check as it seems some IRCD will just send -I without a mask */ - if (!mask) { + if (!mask || !chan->invites || (chan->invites->count == 0)) { return; } - for (i = 0; i < chan->invitecount; i++) { - if ((!reset) && (stricmp(chan->invite[i], mask) == 0)) { - free(chan->invite[i]); - reset = 1; - } - if (reset) - chan->invite[i] = - (i == chan->invitecount) ? NULL : chan->invite[i + 1]; - } + invite = elist_find_mask(chan->invites, mask); - if (reset) - chan->invitecount--; + if (invite) { + entry_delete(chan->invites, invite); - if (debug) - alog("debug: Deleted invite %s to channel %s", mask, chan->name); + if (debug) + alog("debug: Deleted invite %s to channel %s", mask, chan->name); + } } @@ -1936,3 +1899,414 @@ void restore_unsynced_topics(void) } /*************************************************************************/ + +/** + * This handles creating a new Entry. + * This function destroys and free's the given mask as a side effect. + * @param mask Host/IP/CIDR mask to convert to an entry + * @return Entry struct for the given mask, NULL if creation failed + */ +Entry *entry_create(char *mask) +{ + Entry *entry; + char *nick = NULL, *user, *host, *cidrhost; + int do_free; + uint32 ip, cidr; + + entry = scalloc(1, sizeof(Entry)); + entry->type = ENTRYTYPE_NONE; + entry->prev = NULL; + entry->next = NULL; + entry->nick = NULL; + entry->user = NULL; + entry->host = NULL; + entry->mask = sstrdup(mask); + + host = strchr(mask, '@'); + if (host) { + *host++ = '\0'; + /* If the user is purely a wildcard, ignore it */ + if (str_is_pure_wildcard(mask)) + user = NULL; + else { + + /* There might be a nick too */ + user = strchr(mask, '!'); + if (user) { + *user++ = '\0'; + /* If the nick is purely a wildcard, ignore it */ + if (str_is_pure_wildcard(mask)) + nick = NULL; + else + nick = mask; + } else { + nick = NULL; + user = mask; + } + } + } else { + /* It is possibly an extended ban/invite mask, but we do + * not support these at this point.. ~ Viper */ + /* If there's no user in the mask, assume a pure wildcard */ + user = NULL; + host = mask; + } + + if (nick) { + entry->nick = sstrdup(nick); + /* Check if we have a wildcard user */ + if (str_is_wildcard(nick)) + entry->type |= ENTRYTYPE_NICK_WILD; + else + entry->type |= ENTRYTYPE_NICK; + } + + if (user) { + entry->user = sstrdup(user); + /* Check if we have a wildcard user */ + if (str_is_wildcard(user)) + entry->type |= ENTRYTYPE_USER_WILD; + else + entry->type |= ENTRYTYPE_USER; + } + + /* Only check the host if it's not a pure wildcard */ + if (*host && !str_is_pure_wildcard(host)) { + if (ircd->cidrchanbei && str_is_cidr(host, &ip, &cidr, &cidrhost)) { + entry->cidr_ip = ip; + entry->cidr_mask = cidr; + entry->type |= ENTRYTYPE_CIDR4; + host = cidrhost; + do_free = 1; + } else if (ircd->cidrchanbei && strchr(host, '/')) { + /* Most IRCd's don't enforce sane bans therefore it is not + * so unlikely we will encounter this. + * Currently we only support strict CIDR without taking into + * account quirks of every single ircd (nef) that ignore everything + * after the first /cidr. To add this, sanitaze before sending to + * str_is_cidr() as this expects a standard cidr. + * Add it to the internal list (so it is included in for example clear) + * but do not use if during matching.. ~ Viper */ + entry->type = ENTRYTYPE_NONE; + } else { + entry->host = sstrdup(host); + if (str_is_wildcard(host)) + entry->type |= ENTRYTYPE_HOST_WILD; + else + entry->type |= ENTRYTYPE_HOST; + } + } + free(mask); + + return entry; +} + + +/** + * Create an entry and add it at the beginning of given list. + * @param list The List the mask should be added to + * @param mask The mask to parse and add to the list + * @return Pointer to newly added entry. NULL if it fails. + */ +Entry *entry_add(EList *list, char *mask) +{ + Entry *e; + char *hostmask; + + hostmask = sstrdup(mask); + e = entry_create(hostmask); + + if (!e) + return NULL; + + e->next = list->entries; + e->prev = NULL; + + if (list->entries) + list->entries->prev = e; + list->entries = e; + list->count++; + + return e; +} + + +/** + * Delete the given entry from a given list. + * @param list Linked list from which entry needs to be removed. + * @param e The entry to be deleted, must be member of list. + */ +void entry_delete(EList *list, Entry *e) +{ + if (!list || !e) + return; + + if (e->next) + e->next->prev = e->prev; + if (e->prev) + e->prev->next = e->next; + + if (list->entries == e) + list->entries = e->next; + + if (e->nick) + free(e->nick); + if (e->user) + free(e->user); + if (e->host) + free(e->host); + free(e->mask); + free(e); + + list->count--; +} + + +/** + * Create and initialize a new entrylist + * @return Pointer to the created EList object + **/ +EList *list_create() { + EList *list; + + list = scalloc(1, sizeof(EList)); + list->entries = NULL; + list->count = 0; + + return list; +} + + +/** + * Match the given Entry to the given user/host and optional IP addy + * @param e Entry struct to match against + * @param nick Nick to match against + * @param user User to match against + * @param host Host to match against + * @param ip IP to match against, set to 0 to not match this + * @return 1 for a match, 0 for no match + */ +int entry_match(Entry *e, char *nick, char *user, char *host, uint32 ip) +{ + /* If we don't get an entry, or it s an invalid one, no match ~ Viper*/ + if (!e || e->type == ENTRYTYPE_NONE) + return 0; + + if (ircd->cidrchanbei && (e->type & ENTRYTYPE_CIDR4) && + (!ip || (ip && ((ip & e->cidr_mask) != e->cidr_ip)))) + return 0; + if ((e->type & ENTRYTYPE_NICK) && (stricmp(e->nick, nick) != 0)) + return 0; + if ((e->type & ENTRYTYPE_USER) && (stricmp(e->user, user) != 0)) + return 0; + if ((e->type & ENTRYTYPE_HOST) && (stricmp(e->host, host) != 0)) + return 0; + if ((e->type & ENTRYTYPE_NICK_WILD) && !match_wild_nocase(e->nick, nick)) + return 0; + if ((e->type & ENTRYTYPE_USER_WILD) && !match_wild_nocase(e->user, user)) + return 0; + if ((e->type & ENTRYTYPE_HOST_WILD) && !match_wild_nocase(e->host, host)) + return 0; + + return 1; +} + +/** + * Match the given Entry to the given hostmask and optional IP a |
