5 FUTURE: natural (sort of) language parsing
8 PPA: if multiple users match the same user@host or *@host it'll send multiple glines?!
11 #include "regexgline.h"
12 #include "../lib/version.h"
13 #include "../dbapi/dbapi.h"
14 #include "../lib/stringbuf.h"
15 #include "../core/hooks.h"
16 #include "../server/server.h"
17 #include "../lib/strlfunc.h"
20 #define INSTANT_IDENT_GLINE 1
21 #define INSTANT_HOST_GLINE 2
22 #define INSTANT_KILL 3
23 #define DELAYED_IDENT_GLINE 4
24 #define DELAYED_HOST_GLINE 5
25 #define DELAYED_KILL 6
27 #define RESERVED_NICK_GLINE_DURATION 3600 /* 1h */
29 MODULE_VERSION("1.44");
31 typedef struct rg_glinenode
{
33 struct rg_struct
*reason
;
35 struct rg_glinenode
*next
;
38 typedef struct rg_glinelist
{
39 struct rg_glinenode
*start
;
40 struct rg_glinenode
*end
;
43 typedef struct rg_delay
{
46 struct rg_struct
*reason
;
48 struct rg_delay
*next
;
51 #define GLINE_HEADER " ID Expires Set by Class Type Last seen (ago) Hits(p) Hits Reason"
55 void rg_setdelay(nick
*np
, struct rg_struct
*reason
, short punish
);
56 void rg_deletedelay(rg_delay
*delay
);
57 void rg_dodelay(void *arg
);
59 void rg_dogline(struct rg_glinelist
*gll
, nick
*np
, struct rg_struct
*rp
, char *matched
);
60 void rg_flush_schedule(void *arg
);
62 static char *gvhost(nick
*np
);
64 static DBModuleIdentifier dbid
;
65 static unsigned long highestid
= 0;
66 static int attached
= 0, started
= 0;
68 static unsigned int getrgmarker(void);
70 #define RESERVED_NICK_CLASS "reservednick"
71 /* shadowserver only reports classes[0] */
72 static const char *classes
[] = { "drone", "proxy", "spam", "other", RESERVED_NICK_CLASS
, (char *)0 };
75 sstring
*max_casualties
, *max_spew
, *expiry_time
, *max_per_gline
;
77 max_casualties
= getcopyconfigitem("regexgline", "maxcasualties", RGStringise(RG_MAX_CASUALTIES_DEFAULT
), 8);
78 if(!protectedatoi(max_casualties
->content
, &rg_max_casualties
))
79 rg_max_casualties
= RG_MAX_CASUALTIES_DEFAULT
;
81 freesstring(max_casualties
);
83 max_spew
= getcopyconfigitem("regexgline", "maxspew", RGStringise(RG_MAX_SPEW_DEFAULT
), 8);
84 if(!protectedatoi(max_spew
->content
, &rg_max_spew
))
85 rg_max_spew
= RG_MAX_SPEW_DEFAULT
;
87 freesstring(max_spew
);
89 expiry_time
= getcopyconfigitem("regexgline", "expirytime", RGStringise(RG_EXPIRY_TIME_DEFAULT
), 8);
90 if(!protectedatoi(expiry_time
->content
, &rg_expiry_time
))
91 rg_expiry_time
= RG_EXPIRY_TIME_DEFAULT
;
93 freesstring(expiry_time
);
95 max_per_gline
= getcopyconfigitem("regexgline", "maxpergline", RGStringise(RG_MAX_PER_GLINE_DEFAULT
), 8);
96 if(!protectedatoi(max_per_gline
->content
, &rg_max_per_gline
))
97 rg_max_per_gline
= RG_MAX_PER_GLINE_DEFAULT
;
99 freesstring(max_per_gline
);
108 Error("regexgline", ERR_STOP
, "Could not connect to database.");
113 struct rg_struct
*gp
= rg_list
, *oldgp
;
114 rg_delay
*delay
, *delaynext
;
117 deregisterhook(HOOK_NICK_NEWNICK
, &rg_nick
);
118 deregisterhook(HOOK_NICK_RENAME
, &rg_nick
);
119 deregisterhook(HOOK_NICK_LOSTNICK
, &rg_lostnick
);
120 deregistercontrolcmd("regexspew", rg_spew
);
121 deregistercontrolcmd("regexglist", rg_glist
);
122 deregistercontrolcmd("regexdelgline", rg_delgline
);
123 deregistercontrolcmd("regexgline", rg_gline
);
124 deregistercontrolcmd("regexidlookup", rg_idlist
);
128 for(delay
=rg_delays
;delay
;delay
=delaynext
) {
129 delaynext
=delay
->next
;
130 deleteschedule(delay
->sch
, rg_dodelay
, delay
);
136 deleteschedule(rg_schedule
, &rg_checkexpiry
, NULL
);
140 deleteallschedules(rg_flush_schedule
);
141 rg_flush_schedule(NULL
);
143 for(gp
=rg_list
;gp
;) {
146 rg_freestruct(oldgp
);
150 dbdetach("regexgline");
155 static int ignorable_nick(nick
*np
) {
156 if(IsOper(np
) || IsService(np
) || IsXOper(np
) || SIsService(&serverlist
[homeserver(np
->numeric
)]))
161 void rg_checkexpiry(void *arg
) {
162 struct rg_struct
*rp
= rg_list
, *lp
= NULL
;
163 time_t current
= time(NULL
);
166 if (current
>= rp
->expires
) {
167 dbquery("DELETE FROM regexglines WHERE id = %d", rp
->id
);
184 void rg_setdelay(nick
*np
, rg_struct
*reason
, short punish
) {
186 delay
= (rg_delay
*)malloc(sizeof(rg_delay
));
190 killuser(NULL
, np
, "%s (ID: %08lx)", reason
->reason
->content
, reason
->glineid
);
195 delay
->reason
= reason
;
196 delay
->punish
= punish
;
197 delay
->next
= rg_delays
;
200 delay
->sch
= scheduleoneshot(time(NULL
) + (RG_MINIMUM_DELAY_TIME
+ (rand() % RG_MAXIMUM_RAND_TIME
)), rg_dodelay
, delay
);
203 static void rg_shadowserver(nick
*np
, struct rg_struct
*reason
, int type
) {
206 if(reason
->class != classes
[0]) /* drone */
209 snprintf(buf
, sizeof(buf
), "regex-ban %lu %s!%s@%s %s %s", time(NULL
), np
->nick
, np
->ident
, np
->host
->name
->content
, reason
->mask
->content
, serverlist
[homeserver(np
->numeric
)].name
->content
);
211 triggerhook(HOOK_SHADOW_SERVER
, (void *)buf
);
214 void rg_deletedelay(rg_delay
*delay
) {
215 rg_delay
*temp
, *prev
;
217 for (temp
=rg_delays
;temp
;temp
=temp
->next
) {
220 rg_delays
= temp
->next
;
222 prev
->next
= temp
->next
;
232 void rg_dodelay(void *arg
) {
233 rg_delay
*delay
= (rg_delay
*)arg
;
234 char hostname
[RG_MASKLEN
];
235 int hostlen
, usercount
= 0;
237 /* User or regex gline no longer exists */
238 if((!delay
->np
) || (!delay
->reason
)) {
239 rg_deletedelay(delay
);
243 hostlen
= RGBuildHostname(hostname
, delay
->np
);
245 /* User has wisely changed nicknames */
246 if(pcre_exec(delay
->reason
->regex
, delay
->reason
->hint
, hostname
, hostlen
, 0, 0, NULL
, 0) < 0) {
247 rg_deletedelay(delay
);
251 if (delay
->reason
->type
== DELAYED_HOST_GLINE
) {
252 usercount
= delay
->np
->host
->clonecount
;
253 snprintf(hostname
, sizeof(hostname
), "*@%s", IPtostr(delay
->np
->p_ipaddr
));
256 if((delay
->reason
->type
== DELAYED_IDENT_GLINE
) || (usercount
> rg_max_per_gline
)) {
259 for(usercount
=0,tnp
=delay
->np
->host
->nicks
;tnp
;tnp
=tnp
->nextbyhost
)
260 if(!ircd_strcmp(delay
->np
->ident
, tnp
->ident
))
263 snprintf(hostname
, sizeof(hostname
), "%s@%s", delay
->np
->ident
, IPtostr(delay
->np
->p_ipaddr
));
266 if ((delay
->reason
->type
== DELAYED_KILL
) || (usercount
> rg_max_per_gline
)) {
267 controlwall(NO_OPER
, NL_HITS
, "%s matched delayed kill regex %08lx (class: %s)", gvhost(delay
->np
), delay
->reason
->glineid
, delay
->reason
->class);
269 rg_shadowserver(delay
->np
, delay
->reason
, DELAYED_KILL
);
270 killuser(NULL
, delay
->np
, "%s (ID: %08lx)", delay
->reason
->reason
->content
, delay
->reason
->glineid
);
274 if (delay
->reason
->type
== DELAYED_IDENT_GLINE
) {
275 controlwall(NO_OPER
, NL_HITS
, "%s matched delayed user@host gline regex %08lx (class: %s, hit %d user%s)", gvhost(delay
->np
), delay
->reason
->glineid
, delay
->reason
->class, usercount
, (usercount
!=1)?"s":"");
276 } else if (delay
->reason
->type
== DELAYED_HOST_GLINE
) {
277 controlwall(NO_OPER
, NL_HITS
, "%s matched delayed *@host gline regex %08lx (class: %s, hit %d user%s)", gvhost(delay
->np
), delay
->reason
->glineid
, delay
->reason
->class, usercount
, (usercount
!=1)?"s":"");
282 rg_shadowserver(delay
->np
, delay
->reason
, delay
->reason
->type
);
283 irc_send("%s GL * +%s %d %jd :AUTO: %s (ID: %08lx)\r\n", mynumeric
->content
, hostname
, rg_expiry_time
, (intmax_t)time(NULL
), delay
->reason
->reason
->content
, delay
->reason
->glineid
);
284 rg_deletedelay(delay
);
287 void rg_initglinelist(struct rg_glinelist
*gll
) {
292 void rg_flushglines(struct rg_glinelist
*gll
) {
293 struct rg_glinenode
*nn
, *pn
;
294 for(nn
=gll
->start
;nn
;nn
=pn
) {
296 if(nn
->punish
== INSTANT_KILL
) {
297 controlwall(NO_OPER
, NL_HITS
, "%s matched kill regex %08lx (class: %s)", gvhost(nn
->np
), nn
->reason
->glineid
, nn
->reason
->class);
299 rg_shadowserver(nn
->np
, nn
->reason
, nn
->punish
);
300 killuser(NULL
, nn
->np
, "%s (ID: %08lx)", nn
->reason
->reason
->content
, nn
->reason
->glineid
);
301 } else if ((nn
->punish
== DELAYED_IDENT_GLINE
) || (nn
->punish
== DELAYED_HOST_GLINE
) || (nn
->punish
== DELAYED_KILL
)) {
302 rg_setdelay(nn
->np
, nn
->reason
, nn
->punish
);
307 rg_initglinelist(gll
);
310 static void dbloaddata(DBConn
*dbconn
, void *arg
) {
311 DBResult
*dbres
= dbgetresult(dbconn
);
313 if(!dbquerysuccessful(dbres
)) {
314 Error("chanserv", ERR_ERROR
, "Error loading DB");
318 if (dbnumfields(dbres
) != 9) {
319 Error("regexgline", ERR_ERROR
, "DB format error");
323 while(dbfetchrow(dbres
)) {
324 unsigned long id
, hitssaved
;
326 char *gline
, *setby
, *reason
, *expires
, *type
, *class;
328 id
= strtoul(dbgetvalue(dbres
, 0), NULL
, 10);
332 gline
= dbgetvalue(dbres
, 1);
333 setby
= dbgetvalue(dbres
, 2);
334 reason
= dbgetvalue(dbres
, 3);
335 expires
= dbgetvalue(dbres
, 4);
336 type
= dbgetvalue(dbres
, 5);
337 class = dbgetvalue(dbres
, 6);
339 lastseen
= strtoul(dbgetvalue(dbres
, 7), NULL
, 10);
340 hitssaved
= strtoul(dbgetvalue(dbres
, 8), NULL
, 10);
342 if (!rg_newsstruct(id
, gline
, setby
, reason
, expires
, type
, 0, class, lastseen
, hitssaved
))
343 dbquery("DELETE FROM regexgline.glines WHERE id = %lu", id
);
349 static void dbloadfini(DBConn
*dbconn
, void *arg
) {
353 char helpbuf
[8192 * 2], allclasses
[8192];
355 sbinit(&b
, (char *)allclasses
, sizeof(allclasses
));
356 for(p
=classes
;*p
;p
++) {
357 sbaddstr(&b
, (char *)*p
);
362 snprintf(helpbuf
, sizeof(helpbuf
),
363 "Usage: regexgline <regex> <duration> <type> <class> <reason>\n"
364 "Adds a new regular expression pattern.\n"
365 "Duration is represented as 3d, 3M etc.\n"
366 "Class is one of the following: %s\n"
367 "Type is an integer which represents the following:\n"
368 "1 - Instant USER@IP GLINE (igu)\n"
369 "2 - Instant *@IP GLINE (igh)\n"
370 "3 - Instant KILL (ik)\n"
371 "4 - Delayed USER@IP GLINE (dgu)\n"
372 "5 - Delayed *@IP GLINE (dgh)\n"
373 "6 - Delayed KILL (dk)\n"
374 "Note that some classes may have additional side effects (e.g. 'reservednick' also sets nick style glines).",
377 registercontrolhelpcmd("regexgline", NO_OPER
, 5, &rg_gline
, helpbuf
);
378 registercontrolhelpcmd("regexdelgline", NO_OPER
, 1, &rg_delgline
, "Usage: regexdelgline <pattern>\nDeletes a regular expression pattern.");
379 registercontrolhelpcmd("regexglist", NO_OPER
, 1, &rg_glist
, "Usage: regexglist <pattern>\nLists regular expression patterns.");
380 registercontrolhelpcmd("regexspew", NO_OPER
, 1, &rg_spew
, "Usage: regexspew <pattern>\nLists users currently on the network which match the given pattern.");
381 registercontrolhelpcmd("regexidlookup", NO_OPER
, 1, &rg_idlist
, "Usage: regexidlookup <id>\nFinds a regular expression pattern by it's ID number.");
383 registerhook(HOOK_NICK_NEWNICK
, &rg_nick
);
384 registerhook(HOOK_NICK_RENAME
, &rg_nick
);
385 registerhook(HOOK_NICK_LOSTNICK
, &rg_lostnick
);
388 rg_schedule
= schedulerecurring(time(NULL
) + 1, 0, 1, rg_checkexpiry
, NULL
);
389 schedulerecurring(time(NULL
) + 60, 0, 60, rg_flush_schedule
, NULL
);
392 void rg_dbload(void) {
393 dbattach("regexgline");
394 dbcreatequery("CREATE TABLE regexgline.glines (id INT NOT NULL PRIMARY KEY, gline TEXT NOT NULL, setby VARCHAR(%d) NOT NULL, reason VARCHAR(%d) NOT NULL, expires INT NOT NULL, type INT NOT NULL DEFAULT 1, class TEXT NOT NULL, lastseen INT DEFAULT 0, hits INT DEFAULT 0)", ACCOUNTLEN
, RG_REASON_MAX
);
395 dbcreatequery("CREATE TABLE regexgline.clog (host VARCHAR(%d) NOT NULL, account VARCHAR(%d) NOT NULL, event TEXT NOT NULL, arg TEXT NOT NULL, ts TIMESTAMP)", RG_MASKLEN
- 1, ACCOUNTLEN
);
396 dbcreatequery("CREATE TABLE regexgline.glog (glineid INT NOT NULL, ts TIMESTAMP, nickname VARCHAR(%d) NOT NULL, username VARCHAR(%d) NOT NULL, hostname VARCHAR(%d) NOT NULL, realname VARCHAR(%d))", NICKLEN
, USERLEN
, HOSTLEN
, REALLEN
);
398 dbloadtable("regexgline.glines", NULL
, dbloaddata
, dbloadfini
);
401 void rg_nick(int hooknum
, void *arg
) {
402 nick
*np
= (nick
*)arg
;
403 struct rg_struct
*rp
;
404 char hostname
[RG_MASKLEN
];
406 struct rg_glinelist gll
;
408 rg_initglinelist(&gll
);
410 hostlen
= RGBuildHostname(hostname
, np
);
412 if(ignorable_nick(np
))
415 for(rp
=rg_list
;rp
;rp
=rp
->next
) {
416 if(pcre_exec(rp
->regex
, rp
->hint
, hostname
, hostlen
, 0, 0, NULL
, 0) >= 0) {
417 rg_dogline(&gll
, np
, rp
, hostname
);
422 rg_flushglines(&gll
);
425 void rg_lostnick(int hooknum
, void *arg
) {
426 nick
*np
= (nick
*)arg
;
429 /* Cleanup the delays */
430 for(delay
=rg_delays
;delay
;delay
=delay
->next
)
435 int rg_gline(void *source
, int cargc
, char **cargv
) {
436 nick
*np
= (nick
*)source
, *tnp
;
438 const char *expirybuf
;
439 int expiry
, count
, j
, hostlen
;
440 struct rg_struct
*rp
;
441 struct rg_glinelist gll
;
444 char eemask
[RG_QUERY_BUF_SIZE
], eesetby
[RG_QUERY_BUF_SIZE
], eereason
[RG_QUERY_BUF_SIZE
], eeclass
[RG_QUERY_BUF_SIZE
];
445 char hostname
[RG_MASKLEN
], *class, *reason
, *regex
, type
;
451 if ((strlen(cargv
[2]) != 1) || ((type
!= '1') && (type
!= '2') && (type
!= '3') && (type
!= '4') && (type
!= '5') && (type
!= '6'))) {
452 controlreply(np
, "Invalid type specified!");
460 for(p
=classes
;*p
;p
++)
461 if(!strcasecmp(class, *p
))
465 controlreply(np
, "Bad class supplied.");
469 if (!(expiry
= durationtolong(cargv
[1]))) {
470 controlreply(np
, "Invalid duration specified!");
474 for(rp
=rg_list
;rp
;rp
=rp
->next
) {
475 if (RGMasksEqual(rp
->mask
->content
, regex
)) {
476 controlreply(np
, "That regexgline already exists!");
481 if (rg_sanitycheck(regex
, &count
)) {
482 controlreply(np
, "Error in expression.");
484 } else if (count
< 0) {
485 controlreply(np
, "That expression would hit too many users (%d)!", -count
);
489 realexpiry
= expiry
+ time(NULL
);
491 dbescapestring(eemask
, regex
, strlen(regex
));
492 dbescapestring(eesetby
, np
->nick
, strlen(np
->nick
));
493 dbescapestring(eeclass
, class, strlen(class));
494 dbescapestring(eereason
, reason
, strlen(reason
));
496 highestid
= highestid
+ 1;
497 dbquery("INSERT INTO regexgline.glines (id, gline, setby, reason, expires, type, class, lastseen, hits) VALUES (%lu, '%s', '%s', '%s', %lu, %c, '%s', 0, 0)", highestid
, eemask
, eesetby
, eereason
, realexpiry
, type
, eeclass
);
498 rp
= rg_newsstruct(highestid
, regex
, np
->nick
, reason
, "", cargv
[2], realexpiry
, class, 0, 0);
500 rg_initglinelist(&gll
);
502 for(j
=0;j
<NICKHASHSIZE
;j
++) {
503 for(tnp
=nicktable
[j
];tnp
;tnp
=tnp
->next
) {
504 if(ignorable_nick(tnp
))
507 hostlen
= RGBuildHostname(hostname
, tnp
);
508 if(pcre_exec(rp
->regex
, rp
->hint
, hostname
, hostlen
, 0, 0, NULL
, 0) >= 0)
509 rg_dogline(&gll
, tnp
, rp
, hostname
);
513 rg_flushglines(&gll
);
515 expirybuf
= longtoduration(expiry
, 0);
517 rg_logevent(np
, "regexgline", "%s %d %d %s %s", regex
, expiry
, count
, class, reason
);
518 controlreply(np
, "Added regexgline: %s (class: %s, expires in: %s, hit %d user%s): %s", regex
, class, expirybuf
, count
, (count
!=1)?"s":"", reason
);
519 /* If we are using NO, can we safely assume the user is authed here and use ->authname? */
520 controlwall(NO_OPER
, NL_GLINES
, "%s!%s@%s/%s added regexgline: %s (class: %s, expires in: %s, hit %d user%s): %s", np
->nick
, np
->ident
, np
->host
->name
->content
, np
->authname
, regex
, class, expirybuf
, count
, (count
!=1)?"s":"", reason
);
525 int rg_sanitycheck(char *mask
, int *count
) {
527 char hostname
[RG_MASKLEN
];
528 int erroroffset
, hostlen
, j
, masklen
= strlen(mask
);
533 if((masklen
< RG_MIN_MASK_LEN
) || (masklen
> RG_REGEXGLINE_MAX
))
536 if(!(regex
= pcre_compile(mask
, RG_PCREFLAGS
, &error
, &erroroffset
, NULL
))) {
537 Error("regexgline", ERR_WARNING
, "Error compiling expression %s at offset %d: %s", mask
, erroroffset
, error
);
540 hint
= pcre_study(regex
, 0, &error
);
542 Error("regexgline", ERR_WARNING
, "Error studying expression %s: %s", mask
, error
);
549 for(j
=0;j
<NICKHASHSIZE
;j
++) {
550 for(np
=nicktable
[j
];np
;np
=np
->next
) {
551 hostlen
= RGBuildHostname(hostname
, np
);
552 if(pcre_exec(regex
, hint
, hostname
, hostlen
, 0, 0, NULL
, 0) >= 0) {
562 if(*count
>= rg_max_casualties
)
568 int rg_delgline(void *source
, int cargc
, char **cargv
) {
569 nick
*np
= (nick
*)source
;
571 struct rg_struct
*rp
= rg_list
, *last
= NULL
;
577 rg_logevent(np
, "regexdelgline", "%s", cargv
[0]);
579 if(RGMasksEqual(rp
->mask
->content
, cargv
[0])) {
582 /* Cleanup the delays */
583 for(delay
=rg_delays
;delay
;delay
=delay
->next
)
584 if(delay
->reason
==rp
)
585 delay
->reason
= NULL
;
587 dbquery("DELETE FROM regexgline.glines WHERE id = %d", rp
->id
);
589 last
->next
= rp
->next
;
603 controlreply(np
, "Deleted (matched: %d).", count
);
604 /* If we are using NO, can we safely assume the user is authed here and use ->authname? */
605 controlwall(NO_OPER
, NL_GLINES
, "%s!%s@%s/%s removed regexgline: %s", np
->nick
, np
->ident
, np
->host
->name
->content
, np
->authname
, cargv
[0]);
607 controlreply(np
, "No glines matched: %s", cargv
[0]);
612 int rg_idlist(void *source
, int cargc
, char **cargv
) {
613 nick
*np
= (nick
*)source
;
617 } else if (strlen(cargv
[0]) != 8) {
618 controlreply(np
, "Invalid gline id!");
621 struct rg_struct
*rp
;
622 unsigned long id
= 0;
627 if(0xff == rc_hexlookup
[(int)cargv
[0][i
]]) {
628 controlreply(np
, "Invalid gline id!");
631 id
= (id
<< 4) | rc_hexlookup
[(int)cargv
[0][i
]];
636 controlreply(np
, GLINE_HEADER
);
637 for(rp
=rg_list
;rp
;rp
=rp
->next
) {
638 if(id
== rp
->glineid
) {
640 if(rp
->mask
->length
> longest
)
641 longest
= rp
->mask
->length
;
645 for(rp
=rg_list
;rp
;rp
=rp
->next
)
647 rg_displaygline(np
, rp
, longest
);
648 controlreply(np
, "Done.");
654 int rg_glist(void *source
, int cargc
, char **cargv
) {
655 nick
*np
= (nick
*)source
;
656 struct rg_struct
*rp
;
666 if(!(regex
= pcre_compile(cargv
[0], RG_PCREFLAGS
, &error
, &erroroffset
, NULL
))) {
667 controlreply(np
, "Error compiling expression %s at offset %d: %s", cargv
[0], erroroffset
, error
);
670 hint
= pcre_study(regex
, 0, &error
);
672 controlreply(np
, "Error studying expression %s: %s", cargv
[0], error
);
679 rg_logevent(np
, "regexglist", "%s", cargv
[0]);
680 controlreply(np
, GLINE_HEADER
);
681 for(rp
=rg_list
;rp
;rp
=rp
->next
) {
682 if(pcre_exec(regex
, hint
, rp
->mask
->content
, rp
->mask
->length
, 0, 0, NULL
, 0) >= 0) {
684 if(rp
->mask
->length
> longest
)
685 longest
= rp
->mask
->length
;
689 for(rp
=rg_list
;rp
;rp
=rp
->next
)
691 rg_displaygline(np
, rp
, longest
);
698 rg_logevent(np
, "regexglist", "%s", "");
699 controlreply(np
, GLINE_HEADER
);
700 for(rp
=rg_list
;rp
;rp
=rp
->next
)
701 if(rp
->mask
->length
> longest
)
702 longest
= rp
->mask
->length
;
704 for(rp
=rg_list
;rp
;rp
=rp
->next
)
705 rg_displaygline(np
, rp
, longest
);
708 controlreply(np
, "Done.");
712 char *displaytype(int type
) {
714 static char ctypebuf
[10];
739 snprintf(ctypebuf
, sizeof(ctype
), "%1d:%s", type
, ctype
);
743 char *getsep(int longest
) {
744 static int lastlongest
= -1;
745 static char lenbuf
[1024];
752 if(longest >= sizeof(lenbuf) - 20)
753 longest = sizeof(lenbuf) - 20;
756 if(lastlongest
== -1) {
759 for(i
=0;i
<sizeof(lenbuf
)-1;i
++)
761 lenbuf
[sizeof(lenbuf
)-1] = '\0';
765 if(lastlongest
!= longest
) {
766 lenbuf
[lastlongest
] = '-';
767 lenbuf
[longest
] = '\0';
768 lastlongest
= longest
;
774 void rg_displaygline(nick
*np
, struct rg_struct
*rp
, int longest
) { /* could be a macro? I'll assume the C compiler inlines it */
775 char *sep
= getsep(longest
);
776 /* 12345678 12345678901234567890 123456789012345 12345678 12345 12345678901234567890 1234567 1234567 123456
777 ID Expires Set by Class Type Last seen (ago) Hits(s) Hits Reason
781 time_t t
= time(NULL
);
783 if(rp
->lastseen
== 0) {
784 strlcpy(d
, "(never)", sizeof(d
));
786 strlcpy(d
, longtoduration(t
- rp
->lastseen
, 2), sizeof(d
));
789 controlreply(np
, "%s", rp
->mask
->content
);
790 controlreply(np
, " %08lx %-20s %-15s %-8s %-5s %-20s %-7lu %-7lu %s", rp
->glineid
, longtoduration(rp
->expires
- t
, 2), rp
->setby
->content
, rp
->class, displaytype(rp
->type
), d
, rp
->hitssaved
, rp
->hits
, rp
->reason
->content
);
791 controlreply(np
, "%s", sep
);
794 int rg_spew(void *source
, int cargc
, char **cargv
) {
795 nick
*np
= (nick
*)source
, *tnp
;
796 int counter
= 0, erroroffset
, hostlen
, j
;
800 char hostname
[RG_MASKLEN
];
807 if(!(regex
= pcre_compile(cargv
[0], RG_PCREFLAGS
, &error
, &erroroffset
, NULL
))) {
808 controlreply(np
, "Error compiling expression %s at offset %d: %s", cargv
[0], erroroffset
, error
);
811 hint
= pcre_study(regex
, 0, &error
);
813 controlreply(np
, "Error studying expression %s: %s", cargv
[0], error
);
819 rg_logevent(np
, "regexspew", "%s", cargv
[0]);
821 for(j
=0;j
<NICKHASHSIZE
;j
++) {
822 for(tnp
=nicktable
[j
];tnp
;tnp
=tnp
->next
) {
823 hostlen
= RGBuildHostname(hostname
, tnp
);
824 pcreret
= pcre_exec(regex
, hint
, hostname
, hostlen
, 0, 0, ovector
, sizeof(ovector
) / sizeof(int));
826 if(counter
== rg_max_spew
) {
827 controlreply(np
, "Reached maximum spew count (%d) - aborting display.", rg_max_spew
);
828 } else if (counter
< rg_max_spew
) {
829 /* 15 should be number of bolds */
830 char boldbuf
[RG_MASKLEN
+ 15], *tp
, *fp
, *realname
= NULL
;
832 for(tp
=hostname
,fp
=boldbuf
;*tp
;) {
833 if(tp
- hostname
== ovector
[0]) {
837 if(tp
- hostname
== ovector
[1]) {
856 controlreply(np
, "%s (%s) (%dc)", boldbuf
, realname
, tnp
->channels
->cursi
);
862 controlreply(np
, "Done - %d matches.", counter
);
871 void rg_startup(void) {
874 struct rg_struct
*rp
;
875 struct rg_glinelist gll
;
876 char hostname
[RG_MASKLEN
];
878 rg_initglinelist(&gll
);
880 for(j
=0;j
<NICKHASHSIZE
;j
++) {
881 for(np
=nicktable
[j
];np
;np
=np
->next
) {
882 if(ignorable_nick(np
))
884 hostlen
= RGBuildHostname(hostname
, np
);
885 for(rp
=rg_list
;rp
;rp
=rp
->next
) {
886 if(pcre_exec(rp
->regex
, rp
->hint
, hostname
, hostlen
, 0, 0, NULL
, 0) >= 0) {
887 rg_dogline(&gll
, np
, rp
, hostname
);
894 rg_flushglines(&gll
);
897 void rg_freestruct(struct rg_struct
*rp
) {
898 freesstring(rp
->mask
);
899 freesstring(rp
->setby
);
900 freesstring(rp
->reason
);
901 pcre_free(rp
->regex
);
907 struct rg_struct
*rg_newstruct(time_t expires
) {
908 struct rg_struct
*rp
;
910 if (time(NULL
) >= expires
)
913 rp
= (struct rg_struct
*)malloc(sizeof(struct rg_struct
));
915 struct rg_struct
*tp
, *lp
;
917 memset(rp
, 0, sizeof(rg_struct
));
918 rp
->expires
= expires
;
920 for(lp
=NULL
,tp
=rg_list
;tp
;lp
=tp
,tp
=tp
->next
) {
921 if (expires
<= tp
->expires
) { /* <= possible, slight speed increase */
944 struct rg_struct
*rg_newsstruct(unsigned long id
, char *mask
, char *setby
, char *reason
, char *expires
, char *type
, time_t iexpires
, char *class, time_t lastseen
, unsigned int hitssaved
) {
945 struct rg_struct
*newrow
, *lp
, *cp
;
947 char glineiddata
[1024];
952 if(!protectedatoi(expires
, &qexpires
))
954 rexpires
= (time_t)qexpires
;
959 newrow
= rg_newstruct(rexpires
);
965 for(p
=classes
;*p
;p
++) {
966 if(!strcasecmp(class, *p
)) {
973 newrow
->class = "unknown";
975 if(!(newrow
->regex
= pcre_compile(mask
, RG_PCREFLAGS
, &error
, &erroroffset
, NULL
))) {
976 Error("regexgline", ERR_WARNING
, "Error compiling expression %s at offset %d: %s", mask
, erroroffset
, error
);
979 newrow
->hint
= pcre_study(newrow
->regex
, 0, &error
);
981 Error("regexgline", ERR_WARNING
, "Error studying expression %s: %s", mask
, error
);
982 pcre_free(newrow
->regex
);
988 newrow
->hitssaved
= hitssaved
;
989 newrow
->lastseen
= lastseen
;
991 newrow
->mask
= getsstring(mask
, RG_REGEXGLINE_MAX
);
993 Error("regexgline", ERR_WARNING
, "Error allocating memory for mask!");
997 newrow
->setby
= getsstring(setby
, ACCOUNTLEN
);
999 Error("regexgline", ERR_WARNING
, "Error allocating memory for setby!");
1003 newrow
->reason
= getsstring(reason
, RG_REASON_MAX
);
1004 if(!newrow
->reason
) {
1005 Error("regexgline", ERR_WARNING
, "Error allocating memory for reason!");
1009 if(!protectedatoi(type
, &newrow
->type
))
1010 newrow
->type
= 0; /* just in case */
1012 snprintf(glineiddata
, sizeof(glineiddata
), "%s regexgline %s %s %s %d %d", mynumeric
->content
, mask
, setby
, reason
, (int)iexpires
, newrow
->type
);
1013 newrow
->glineid
= crc32(glineiddata
);
1020 freesstring(newrow
->mask
);
1022 freesstring(newrow
->setby
);
1024 freesstring(newrow
->reason
);
1025 pcre_free(newrow
->regex
);
1027 pcre_free(newrow
->hint
);
1030 for(lp
=NULL
,cp
=rg_list
;cp
;lp
=cp
,cp
=cp
->next
) {
1033 lp
->next
= cp
->next
;
1044 int __rg_dogline(struct rg_glinelist
*gll
, nick
*np
, struct rg_struct
*rp
, char *matched
) { /* PPA: if multiple users match the same user@host or *@host it'll send multiple glines?! */
1045 char hostname
[RG_MASKLEN
];
1049 rg_loggline(rp
, np
);
1051 if (rp
->type
== INSTANT_HOST_GLINE
) {
1052 usercount
= np
->host
->clonecount
;
1053 snprintf(hostname
, sizeof(hostname
), "*@%s", IPtostr(np
->p_ipaddr
));
1056 if ((rp
->type
== INSTANT_IDENT_GLINE
) || (usercount
> rg_max_per_gline
)) {
1059 for(usercount
=0,tnp
=np
->host
->nicks
;tnp
;tnp
=tnp
->nextbyhost
)
1060 if(!ircd_strcmp(np
->ident
, tnp
->ident
))
1063 snprintf(hostname
, sizeof(hostname
), "%s@%s", np
->ident
, IPtostr(np
->p_ipaddr
));
1066 if(!strcmp(rp
->class, RESERVED_NICK_CLASS
))
1067 irc_send("%s GL * +%s!*@* %d %jd :AUTO: %s (ID: %08lx)\r\n", mynumeric
->content
, np
->nick
, RESERVED_NICK_GLINE_DURATION
, (intmax_t)time(NULL
), rp
->reason
->content
, rp
->glineid
);
1069 validdelay
= (rp
->type
== INSTANT_KILL
) || (rp
->type
== DELAYED_IDENT_GLINE
) || (rp
->type
== DELAYED_HOST_GLINE
) || (rp
->type
== DELAYED_KILL
);
1070 if (validdelay
|| (usercount
> rg_max_per_gline
)) {
1071 struct rg_glinenode
*nn
= (struct rg_glinenode
*)malloc(sizeof(struct rg_glinenode
));
1075 gll
->end
->next
= nn
;
1085 nn
->punish
= INSTANT_KILL
;
1087 nn
->punish
= rp
->type
;
1093 if (rp
->type
== INSTANT_IDENT_GLINE
) {
1094 controlwall(NO_OPER
, NL_HITS
, "%s matched user@host gline regex %08lx (class: %s, hit %d user%s)", gvhost(np
), rp
->glineid
, rp
->class, usercount
, (usercount
!=1)?"s":"");
1095 } else if(rp
->type
== INSTANT_HOST_GLINE
) {
1096 controlwall(NO_OPER
, NL_HITS
, "%s matched *@host gline regex %08lx (class: %s, hit %d user%s)", gvhost(np
), rp
->glineid
, rp
->class, usercount
, (usercount
!=1)?"s":"");
1101 rg_shadowserver(np
, rp
, rp
->type
);
1102 irc_send("%s GL * +%s %d %jd :AUTO: %s (ID: %08lx)\r\n", mynumeric
->content
, hostname
, rg_expiry_time
, (intmax_t)time(NULL
), rp
->reason
->content
, rp
->glineid
);
1106 static char *gvhost(nick
*np
) {
1107 static char buf
[NICKLEN
+1+USERLEN
+1+HOSTLEN
+1+ACCOUNTLEN
+4+REALLEN
+1+10];
1110 snprintf(buf
, sizeof(buf
), "%s!%s@%s/%s r(%s)", np
->nick
, np
->ident
, np
->host
->name
->content
, np
->authname
, np
->realname
->name
->content
);
1112 snprintf(buf
, sizeof(buf
), "%s!%s@%s r(%s)", np
->nick
, np
->ident
, np
->host
->name
->content
, np
->realname
->name
->content
);
1118 static int floodprotection
= 0;
1119 static int lastfloodspam
= 0;
1121 void rg_dogline(struct rg_glinelist
*gll
, nick
*np
, struct rg_struct
*rp
, char *matched
) {
1124 if(t
> floodprotection
) {
1125 floodprotection
= t
;
1126 } else if((floodprotection
- t
) / 8 > RG_NETWORK_WIDE_MAX_GLINES_PER_8_SEC
) {
1127 if(t
> lastfloodspam
+ 3600) {
1128 channel
*cp
= findchannel("#twilightzone");
1130 controlchanmsg(cp
, "WARNING! REGEXGLINE DISABLED FOR AN HOUR DUE TO NETWORK WIDE LOOKING GLINE!: %d exceeded %d", (floodprotection
- t
) / 8, RG_NETWORK_WIDE_MAX_GLINES_PER_8_SEC
);
1131 controlwall(NO_OPER
, NL_MANAGEMENT
, "WARNING! REGEXGLINE DISABLED FOR AN HOUR DUE TO NETWORK WIDE LOOKING GLINE!");
1133 floodprotection
= t
+ RG_NETWORK_WIDE_MAX_GLINES_PER_8_SEC
* 3600 * 8;
1138 floodprotection
+=__rg_dogline(gll
, np
, rp
, matched
);
1141 void rg_logevent(nick
*np
, char *event
, char *details
, ...) {
1142 char eeevent
[RG_QUERY_BUF_SIZE
], eedetails
[RG_QUERY_BUF_SIZE
], eemask
[RG_QUERY_BUF_SIZE
], eeaccount
[RG_QUERY_BUF_SIZE
];
1143 char buf
[513], account
[ACCOUNTLEN
+ 1], mask
[RG_MASKLEN
];
1149 va_start(va
, details
);
1150 vsnprintf(buf
, sizeof(buf
), details
, va
);
1157 if (IsAccount(np
)) {
1158 strncpy(account
, np
->authname
, sizeof(account
) - 1);
1159 account
[sizeof(account
) - 1] = '\0';
1163 masklen
= RGBuildHostname(mask
, np
);
1169 dbescapestring(eeevent
, event
, strlen(event
));
1170 dbescapestring(eedetails
, buf
, strlen(buf
));
1171 dbescapestring(eeaccount
, account
, strlen(account
));
1172 dbescapestring(eemask
, mask
, masklen
);
1174 dbquery("INSERT INTO regexgline.clog (host, account, event, arg, ts) VALUES ('%s', '%s', '%s', '%s', NOW())", eemask
, eeaccount
, eeevent
, eedetails
);
1177 void rg_loggline(struct rg_struct
*rg
, nick
*np
) {
1178 char eenick
[RG_QUERY_BUF_SIZE
], eeuser
[RG_QUERY_BUF_SIZE
], eehost
[RG_QUERY_BUF_SIZE
], eereal
[RG_QUERY_BUF_SIZE
];
1182 rg
->lastseen
= time(NULL
);
1185 /* @paul: disabled */
1188 dbescapestring(eenick
, np
->nick
, strlen(np
->nick
));
1189 dbescapestring(eeuser
, np
->ident
, strlen(np
->ident
));
1190 dbescapestring(eehost
, np
->host
->name
->content
, strlen(np
->host
->name
->content
));
1191 dbescapestring(eereal
, np
->realname
->name
->content
, strlen(np
->realname
->name
->content
));
1193 dbquery("INSERT INTO regexgline.glog (glineid, nickname, username, hostname, realname, ts) VALUES (%d, '%s', '%s', '%s', '%s', NOW())", rg
->id
, eenick
, eeuser
, eehost
, eereal
);
1196 static unsigned int getrgmarker(void) {
1197 static unsigned int marker
= 0;
1201 struct rg_struct
*l
;
1203 /* If we wrapped to zero, zap the marker on all hosts */
1204 for(l
=rg_list
;l
;l
=l
->next
)
1212 void rg_flush_schedule(void *arg
) {
1213 struct rg_struct
*l
;
1215 for(l
=rg_list
;l
;l
=l
->next
) {
1219 dbquery("UPDATE regexgline.glines SET lastseen = %jd, hits = %lu WHERE id = %d", (intmax_t)l
->lastseen
, l
->hitssaved
, l
->id
);