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"
18 #include "../glines/glines.h"
21 #define INSTANT_IDENT_GLINE 1
22 #define INSTANT_HOST_GLINE 2
23 #define INSTANT_KILL 3
24 #define DELAYED_IDENT_GLINE 4
25 #define DELAYED_HOST_GLINE 5
26 #define DELAYED_KILL 6
28 #define RESERVED_NICK_GLINE_DURATION 3600 /* 1h */
30 MODULE_VERSION("1.44");
32 typedef struct rg_glinenode
{
34 struct rg_struct
*reason
;
36 struct rg_glinenode
*next
;
39 typedef struct rg_glinelist
{
40 struct rg_glinenode
*start
;
41 struct rg_glinenode
*end
;
44 typedef struct rg_delay
{
47 struct rg_struct
*reason
;
49 struct rg_delay
*next
;
52 #define GLINE_HEADER " ID Expires Set by Class Type Last seen (ago) Hits(p) Hits Reason"
56 void rg_setdelay(nick
*np
, struct rg_struct
*reason
, short punish
);
57 void rg_deletedelay(rg_delay
*delay
);
58 void rg_dodelay(void *arg
);
60 void rg_dogline(struct rg_glinelist
*gll
, nick
*np
, struct rg_struct
*rp
, char *matched
);
61 void rg_flush_schedule(void *arg
);
63 static char *gvhost(nick
*np
);
64 typedef void (scannick_fn
)(struct rg_struct
*, nick
*, char *, void *);
65 static void rg_scannick(nick
*np
, scannick_fn
*fn
, void *arg
);
66 static void rg_gline_match(struct rg_struct
*rp
, nick
*np
, char *hostname
, void *arg
);
68 static DBModuleIdentifier dbid
;
69 static unsigned long highestid
= 0;
70 static int attached
= 0, started
= 0;
72 static unsigned int getrgmarker(void);
74 #define RESERVED_NICK_CLASS "reservednick"
75 /* shadowserver only reports classes[0] */
76 static const char *classes
[] = { "drone", "proxy", "spam", "other", RESERVED_NICK_CLASS
, (char *)0 };
78 void rg_initglinelist(struct rg_glinelist
*gll
);
79 void rg_flushglines(struct rg_glinelist
*gll
);
82 sstring
*max_casualties
, *max_spew
, *expiry_time
, *max_per_gline
;
84 max_casualties
= getcopyconfigitem("regexgline", "maxcasualties", RGStringise(RG_MAX_CASUALTIES_DEFAULT
), 8);
85 if(!protectedatoi(max_casualties
->content
, &rg_max_casualties
))
86 rg_max_casualties
= RG_MAX_CASUALTIES_DEFAULT
;
88 freesstring(max_casualties
);
90 max_spew
= getcopyconfigitem("regexgline", "maxspew", RGStringise(RG_MAX_SPEW_DEFAULT
), 8);
91 if(!protectedatoi(max_spew
->content
, &rg_max_spew
))
92 rg_max_spew
= RG_MAX_SPEW_DEFAULT
;
94 freesstring(max_spew
);
96 expiry_time
= getcopyconfigitem("regexgline", "expirytime", RGStringise(RG_EXPIRY_TIME_DEFAULT
), 8);
97 if(!protectedatoi(expiry_time
->content
, &rg_expiry_time
))
98 rg_expiry_time
= RG_EXPIRY_TIME_DEFAULT
;
100 freesstring(expiry_time
);
102 max_per_gline
= getcopyconfigitem("regexgline", "maxpergline", RGStringise(RG_MAX_PER_GLINE_DEFAULT
), 8);
103 if(!protectedatoi(max_per_gline
->content
, &rg_max_per_gline
))
104 rg_max_per_gline
= RG_MAX_PER_GLINE_DEFAULT
;
106 freesstring(max_per_gline
);
115 Error("regexgline", ERR_STOP
, "Could not connect to database.");
119 static void rg_count_match(struct rg_struct
*rp
, nick
*np
, char *hostname
, void *arg
) {
120 void **varg
= (void **)arg
;
121 int *count
= (int *)varg
[0];
126 static void rg_gline_reply_match(struct rg_struct
*rp
, nick
*np
, char *hostname
, void *arg
) {
127 void **varg
= (void **)arg
;
129 rg_count_match(rp
, np
, hostname
, arg
);
130 rg_gline_match(rp
, np
, hostname
, varg
[1]);
133 int rg_rescan(void *source
, int cargc
, char **cargv
) {
136 nick
*np
= (nick
*)source
, *tnp
;
139 struct rg_glinelist gll
;
143 gline
= !strcmp(cargv
[0], "-g");
150 controlreply(np
, "G-line mode activated.");
152 rg_initglinelist(&gll
);
155 fn
= rg_gline_reply_match
;
158 controlreply(np
, "Beginning scan, this may take a while...");
160 for(j
=0;j
<NICKHASHSIZE
;j
++)
161 for(tnp
=nicktable
[j
];tnp
;tnp
=tnp
->next
)
162 rg_scannick(tnp
, fn
, arg
);
164 controlreply(np
, "Scan completed, %d hits.", count
);
167 rg_flushglines(&gll
);
173 struct rg_struct
*gp
, *oldgp
;
174 rg_delay
*delay
, *delaynext
;
177 deregisterhook(HOOK_NICK_NEWNICK
, &rg_nick
);
178 deregisterhook(HOOK_NICK_RENAME
, &rg_rename
);
179 deregisterhook(HOOK_NICK_LOSTNICK
, &rg_lostnick
);
180 deregistercontrolcmd("regexspew", rg_spew
);
181 deregistercontrolcmd("regexglist", rg_glist
);
182 deregistercontrolcmd("regexdelgline", rg_delgline
);
183 deregistercontrolcmd("regexgline", rg_gline
);
184 deregistercontrolcmd("regexidlookup", rg_idlist
);
185 deregistercontrolcmd("regexrescan", rg_rescan
);
189 for(delay
=rg_delays
;delay
;delay
=delaynext
) {
190 delaynext
=delay
->next
;
191 deleteschedule(delay
->sch
, rg_dodelay
, delay
);
197 deleteschedule(rg_schedule
, &rg_checkexpiry
, NULL
);
201 deleteallschedules(rg_flush_schedule
);
202 rg_flush_schedule(NULL
);
204 for(gp
=rg_list
;gp
;) {
207 rg_freestruct(oldgp
);
211 dbdetach("regexgline");
216 static int ignorable_nick(nick
*np
) {
217 if(IsOper(np
) || IsService(np
) || IsXOper(np
) || SIsService(&serverlist
[homeserver(np
->numeric
)]))
222 void rg_checkexpiry(void *arg
) {
223 struct rg_struct
*rp
= rg_list
, *lp
= NULL
;
224 time_t current
= time(NULL
);
227 if (current
>= rp
->expires
) {
228 dbquery("DELETE FROM regexglines WHERE id = %d", rp
->id
);
245 void rg_setdelay(nick
*np
, rg_struct
*reason
, short punish
) {
247 delay
= (rg_delay
*)malloc(sizeof(rg_delay
));
251 killuser(NULL
, np
, "%s (ID: %08lx)", reason
->reason
->content
, reason
->glineid
);
256 delay
->reason
= reason
;
257 delay
->punish
= punish
;
258 delay
->next
= rg_delays
;
261 delay
->sch
= scheduleoneshot(time(NULL
) + (RG_MINIMUM_DELAY_TIME
+ (rand() % RG_MAXIMUM_RAND_TIME
)), rg_dodelay
, delay
);
264 static void rg_shadowserver(nick
*np
, struct rg_struct
*reason
, int type
) {
267 if(reason
->class != classes
[0]) /* drone */
270 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
);
272 triggerhook(HOOK_SHADOW_SERVER
, (void *)buf
);
275 void rg_deletedelay(rg_delay
*delay
) {
276 rg_delay
*temp
, *prev
;
278 for (temp
=rg_delays
;temp
;temp
=temp
->next
) {
281 rg_delays
= temp
->next
;
283 prev
->next
= temp
->next
;
293 void rg_dodelay(void *arg
) {
294 rg_delay
*delay
= (rg_delay
*)arg
;
295 char hostname
[RG_MASKLEN
];
296 int hostlen
, usercount
= 0;
300 /* User or regex gline no longer exists */
301 if((!delay
->np
) || (!delay
->reason
)) {
302 rg_deletedelay(delay
);
306 hostlen
= RGBuildHostname(hostname
, delay
->np
);
308 /* User has wisely changed nicknames */
309 if(pcre_exec(delay
->reason
->regex
, delay
->reason
->hint
, hostname
, hostlen
, 0, 0, NULL
, 0) < 0) {
310 rg_deletedelay(delay
);
314 if (delay
->reason
->type
== DELAYED_HOST_GLINE
) {
315 usercount
= delay
->np
->host
->clonecount
;
318 if((delay
->reason
->type
== DELAYED_IDENT_GLINE
) || (usercount
> rg_max_per_gline
)) {
321 for(usercount
=0,tnp
=delay
->np
->host
->nicks
;tnp
;tnp
=tnp
->nextbyhost
)
322 if(!ircd_strcmp(delay
->np
->ident
, tnp
->ident
))
325 glineflags
= GLINE_ALWAYS_USER
;
328 if ((delay
->reason
->type
== DELAYED_KILL
) || (usercount
> rg_max_per_gline
)) {
329 controlwall(NO_OPER
, NL_HITS
, "%s matched delayed kill regex %08lx (class: %s)", gvhost(delay
->np
), delay
->reason
->glineid
, delay
->reason
->class);
331 rg_shadowserver(delay
->np
, delay
->reason
, DELAYED_KILL
);
332 killuser(NULL
, delay
->np
, "%s (ID: %08lx)", delay
->reason
->reason
->content
, delay
->reason
->glineid
);
336 if (delay
->reason
->type
== DELAYED_IDENT_GLINE
) {
337 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":"");
338 } else if (delay
->reason
->type
== DELAYED_HOST_GLINE
) {
339 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":"");
344 rg_shadowserver(delay
->np
, delay
->reason
, delay
->reason
->type
);
345 snprintf(reason
, sizeof(reason
), "AUTO: %s (ID: %08lx)", delay
->reason
->reason
->content
, delay
->reason
->glineid
);
346 glinebynick(delay
->np
, rg_expiry_time
, reason
, glineflags
, "regexgline");
347 rg_deletedelay(delay
);
350 void rg_initglinelist(struct rg_glinelist
*gll
) {
355 void rg_flushglines(struct rg_glinelist
*gll
) {
356 struct rg_glinenode
*nn
, *pn
;
357 for(nn
=gll
->start
;nn
;nn
=pn
) {
359 if(nn
->punish
== INSTANT_KILL
) {
360 controlwall(NO_OPER
, NL_HITS
, "%s matched kill regex %08lx (class: %s)", gvhost(nn
->np
), nn
->reason
->glineid
, nn
->reason
->class);
362 rg_shadowserver(nn
->np
, nn
->reason
, nn
->punish
);
363 killuser(NULL
, nn
->np
, "%s (ID: %08lx)", nn
->reason
->reason
->content
, nn
->reason
->glineid
);
364 } else if ((nn
->punish
== DELAYED_IDENT_GLINE
) || (nn
->punish
== DELAYED_HOST_GLINE
) || (nn
->punish
== DELAYED_KILL
)) {
365 rg_setdelay(nn
->np
, nn
->reason
, nn
->punish
);
370 rg_initglinelist(gll
);
373 static void dbloaddata(DBConn
*dbconn
, void *arg
) {
374 DBResult
*dbres
= dbgetresult(dbconn
);
376 if(!dbquerysuccessful(dbres
)) {
377 Error("chanserv", ERR_ERROR
, "Error loading DB");
381 if (dbnumfields(dbres
) != 9) {
382 Error("regexgline", ERR_ERROR
, "DB format error");
386 while(dbfetchrow(dbres
)) {
387 unsigned long id
, hitssaved
;
389 char *gline
, *setby
, *reason
, *expires
, *type
, *class;
391 id
= strtoul(dbgetvalue(dbres
, 0), NULL
, 10);
395 gline
= dbgetvalue(dbres
, 1);
396 setby
= dbgetvalue(dbres
, 2);
397 reason
= dbgetvalue(dbres
, 3);
398 expires
= dbgetvalue(dbres
, 4);
399 type
= dbgetvalue(dbres
, 5);
400 class = dbgetvalue(dbres
, 6);
402 lastseen
= strtoul(dbgetvalue(dbres
, 7), NULL
, 10);
403 hitssaved
= strtoul(dbgetvalue(dbres
, 8), NULL
, 10);
405 if (!rg_newsstruct(id
, gline
, setby
, reason
, expires
, type
, 0, class, lastseen
, hitssaved
))
406 dbquery("DELETE FROM regexgline.glines WHERE id = %lu", id
);
412 static void dbloadfini(DBConn
*dbconn
, void *arg
) {
416 char helpbuf
[8192 * 2], allclasses
[8192];
418 sbinit(&b
, (char *)allclasses
, sizeof(allclasses
));
419 for(p
=classes
;*p
;p
++) {
420 sbaddstr(&b
, (char *)*p
);
425 snprintf(helpbuf
, sizeof(helpbuf
),
426 "Usage: regexgline <regex> <duration> <type> <class> <reason>\n"
427 "Adds a new regular expression pattern.\n"
428 "Duration is represented as 3d, 3M etc.\n"
429 "Class is one of the following: %s\n"
430 "Type is an integer which represents the following:\n"
431 "1 - Instant USER@IP GLINE (igu)\n"
432 "2 - Instant *@IP GLINE (igh)\n"
433 "3 - Instant KILL (ik)\n"
434 "4 - Delayed USER@IP GLINE (dgu)\n"
435 "5 - Delayed *@IP GLINE (dgh)\n"
436 "6 - Delayed KILL (dk)\n"
437 "Note that some classes may have additional side effects (e.g. 'reservednick' also sets nick style glines).",
440 registercontrolhelpcmd("regexgline", NO_OPER
, 5, &rg_gline
, helpbuf
);
441 registercontrolhelpcmd("regexdelgline", NO_OPER
, 1, &rg_delgline
, "Usage: regexdelgline <pattern>\nDeletes a regular expression pattern.");
442 registercontrolhelpcmd("regexglist", NO_OPER
, 1, &rg_glist
, "Usage: regexglist <pattern>\nLists regular expression patterns.");
443 registercontrolhelpcmd("regexspew", NO_OPER
, 1, &rg_spew
, "Usage: regexspew <pattern>\nLists users currently on the network which match the given pattern.");
444 registercontrolhelpcmd("regexidlookup", NO_OPER
, 1, &rg_idlist
, "Usage: regexidlookup <id>\nFinds a regular expression pattern by it's ID number.");
445 registercontrolhelpcmd("regexrescan", NO_OPER
, 1, &rg_rescan
, "Usage: regexrescan ?-g?\nRescans the net for missed clients, optionally glining matches (used for debugging).");
447 registerhook(HOOK_NICK_NEWNICK
, &rg_nick
);
448 registerhook(HOOK_NICK_RENAME
, &rg_rename
);
449 registerhook(HOOK_NICK_LOSTNICK
, &rg_lostnick
);
452 rg_schedule
= schedulerecurring(time(NULL
) + 1, 0, 1, rg_checkexpiry
, NULL
);
453 schedulerecurring(time(NULL
) + 60, 0, 60, rg_flush_schedule
, NULL
);
456 void rg_dbload(void) {
457 dbattach("regexgline");
458 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
);
459 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
);
460 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
);
462 dbloadtable("regexgline.glines", NULL
, dbloaddata
, dbloadfini
);
465 static void rg_scannick(nick
*np
, scannick_fn
*fn
, void *arg
) {
466 struct rg_struct
*rp
;
467 char hostname
[RG_MASKLEN
];
470 if(ignorable_nick(np
))
473 hostlen
= RGBuildHostname(hostname
, np
);
475 for(rp
=rg_list
;rp
;rp
=rp
->next
) {
476 if(pcre_exec(rp
->regex
, rp
->hint
, hostname
, hostlen
, 0, 0, NULL
, 0) >= 0) {
477 fn(rp
, np
, hostname
, arg
);
483 static void rg_gline_match(struct rg_struct
*rp
, nick
*np
, char *hostname
, void *arg
) {
484 struct rg_glinelist
*gll
= (struct rg_glinelist
*)arg
;
486 rg_dogline(gll
, np
, rp
, hostname
);
489 void rg_rename(int hooknum
, void *arg
) {
490 void **harg
= (void **)arg
;
491 rg_nick(hooknum
, harg
[0]);
494 void rg_nick(int hooknum
, void *arg
) {
495 nick
*np
= (nick
*)arg
;
496 struct rg_glinelist gll
;
498 rg_initglinelist(&gll
);
500 rg_scannick(np
, rg_gline_match
, &gll
);
502 rg_flushglines(&gll
);
505 void rg_lostnick(int hooknum
, void *arg
) {
506 nick
*np
= (nick
*)arg
;
509 /* Cleanup the delays */
510 for(delay
=rg_delays
;delay
;delay
=delay
->next
)
515 int rg_gline(void *source
, int cargc
, char **cargv
) {
516 nick
*np
= (nick
*)source
, *tnp
;
518 const char *expirybuf
;
519 int expiry
, count
, j
, hostlen
;
520 struct rg_struct
*rp
;
521 struct rg_glinelist gll
;
524 char eemask
[RG_QUERY_BUF_SIZE
], eesetby
[RG_QUERY_BUF_SIZE
], eereason
[RG_QUERY_BUF_SIZE
], eeclass
[RG_QUERY_BUF_SIZE
];
525 char hostname
[RG_MASKLEN
], *class, *reason
, *regex
, type
;
531 if ((strlen(cargv
[2]) != 1) || ((type
!= '1') && (type
!= '2') && (type
!= '3') && (type
!= '4') && (type
!= '5') && (type
!= '6'))) {
532 controlreply(np
, "Invalid type specified!");
540 for(p
=classes
;*p
;p
++)
541 if(!strcasecmp(class, *p
))
545 controlreply(np
, "Bad class supplied.");
549 if (!(expiry
= durationtolong(cargv
[1]))) {
550 controlreply(np
, "Invalid duration specified!");
554 for(rp
=rg_list
;rp
;rp
=rp
->next
) {
555 if (RGMasksEqual(rp
->mask
->content
, regex
)) {
556 controlreply(np
, "That regexgline already exists!");
561 if (rg_sanitycheck(regex
, &count
)) {
562 controlreply(np
, "Error in expression.");
564 } else if (count
< 0) {
565 controlreply(np
, "That expression would hit too many users (%d)!", -count
);
569 realexpiry
= expiry
+ time(NULL
);
571 dbescapestring(eemask
, regex
, strlen(regex
));
572 dbescapestring(eesetby
, np
->nick
, strlen(np
->nick
));
573 dbescapestring(eeclass
, class, strlen(class));
574 dbescapestring(eereason
, reason
, strlen(reason
));
576 highestid
= highestid
+ 1;
577 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
);
578 rp
= rg_newsstruct(highestid
, regex
, np
->nick
, reason
, "", cargv
[2], realexpiry
, class, 0, 0);
580 rg_initglinelist(&gll
);
582 for(j
=0;j
<NICKHASHSIZE
;j
++) {
583 for(tnp
=nicktable
[j
];tnp
;tnp
=tnp
->next
) {
584 if(ignorable_nick(tnp
))
587 hostlen
= RGBuildHostname(hostname
, tnp
);
588 if(pcre_exec(rp
->regex
, rp
->hint
, hostname
, hostlen
, 0, 0, NULL
, 0) >= 0)
589 rg_dogline(&gll
, tnp
, rp
, hostname
);
593 rg_flushglines(&gll
);
595 expirybuf
= longtoduration(expiry
, 0);
597 rg_logevent(np
, "regexgline", "%s %d %d %s %s", regex
, expiry
, count
, class, reason
);
598 controlreply(np
, "Added regexgline: %s (class: %s, expires in: %s, hit %d user%s): %s", regex
, class, expirybuf
, count
, (count
!=1)?"s":"", reason
);
599 /* If we are using NO, can we safely assume the user is authed here and use ->authname? */
600 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
);
605 int rg_sanitycheck(char *mask
, int *count
) {
607 char hostname
[RG_MASKLEN
];
608 int erroroffset
, hostlen
, j
, masklen
= strlen(mask
);
613 if((masklen
< RG_MIN_MASK_LEN
) || (masklen
> RG_REGEXGLINE_MAX
))
616 if(!(regex
= pcre_compile(mask
, RG_PCREFLAGS
, &error
, &erroroffset
, NULL
))) {
617 Error("regexgline", ERR_WARNING
, "Error compiling expression %s at offset %d: %s", mask
, erroroffset
, error
);
620 hint
= pcre_study(regex
, 0, &error
);
622 Error("regexgline", ERR_WARNING
, "Error studying expression %s: %s", mask
, error
);
629 for(j
=0;j
<NICKHASHSIZE
;j
++) {
630 for(np
=nicktable
[j
];np
;np
=np
->next
) {
631 hostlen
= RGBuildHostname(hostname
, np
);
632 if(pcre_exec(regex
, hint
, hostname
, hostlen
, 0, 0, NULL
, 0) >= 0) {
642 if(*count
>= rg_max_casualties
)
648 int rg_delgline(void *source
, int cargc
, char **cargv
) {
649 nick
*np
= (nick
*)source
;
651 struct rg_struct
*rp
= rg_list
, *last
= NULL
;
657 rg_logevent(np
, "regexdelgline", "%s", cargv
[0]);
659 if(RGMasksEqual(rp
->mask
->content
, cargv
[0])) {
662 /* Cleanup the delays */
663 for(delay
=rg_delays
;delay
;delay
=delay
->next
)
664 if(delay
->reason
==rp
)
665 delay
->reason
= NULL
;
667 dbquery("DELETE FROM regexgline.glines WHERE id = %d", rp
->id
);
669 last
->next
= rp
->next
;
683 controlreply(np
, "Deleted (matched: %d).", count
);
684 /* If we are using NO, can we safely assume the user is authed here and use ->authname? */
685 controlwall(NO_OPER
, NL_GLINES
, "%s!%s@%s/%s removed regexgline: %s", np
->nick
, np
->ident
, np
->host
->name
->content
, np
->authname
, cargv
[0]);
687 controlreply(np
, "No glines matched: %s", cargv
[0]);
692 int rg_idlist(void *source
, int cargc
, char **cargv
) {
693 nick
*np
= (nick
*)source
;
697 } else if (strlen(cargv
[0]) != 8) {
698 controlreply(np
, "Invalid gline id!");
701 struct rg_struct
*rp
;
702 unsigned long id
= 0;
707 if(0xff == rc_hexlookup
[(int)cargv
[0][i
]]) {
708 controlreply(np
, "Invalid gline id!");
711 id
= (id
<< 4) | rc_hexlookup
[(int)cargv
[0][i
]];
716 controlreply(np
, GLINE_HEADER
);
717 for(rp
=rg_list
;rp
;rp
=rp
->next
) {
718 if(id
== rp
->glineid
) {
720 if(rp
->mask
->length
> longest
)
721 longest
= rp
->mask
->length
;
725 for(rp
=rg_list
;rp
;rp
=rp
->next
)
727 rg_displaygline(np
, rp
, longest
);
728 controlreply(np
, "Done.");
734 int rg_glist(void *source
, int cargc
, char **cargv
) {
735 nick
*np
= (nick
*)source
;
736 struct rg_struct
*rp
;
746 if(!(regex
= pcre_compile(cargv
[0], RG_PCREFLAGS
, &error
, &erroroffset
, NULL
))) {
747 controlreply(np
, "Error compiling expression %s at offset %d: %s", cargv
[0], erroroffset
, error
);
750 hint
= pcre_study(regex
, 0, &error
);
752 controlreply(np
, "Error studying expression %s: %s", cargv
[0], error
);
759 rg_logevent(np
, "regexglist", "%s", cargv
[0]);
760 controlreply(np
, GLINE_HEADER
);
761 for(rp
=rg_list
;rp
;rp
=rp
->next
) {
762 if(pcre_exec(regex
, hint
, rp
->mask
->content
, rp
->mask
->length
, 0, 0, NULL
, 0) >= 0) {
764 if(rp
->mask
->length
> longest
)
765 longest
= rp
->mask
->length
;
769 for(rp
=rg_list
;rp
;rp
=rp
->next
)
771 rg_displaygline(np
, rp
, longest
);
778 rg_logevent(np
, "regexglist", "%s", "");
779 controlreply(np
, GLINE_HEADER
);
780 for(rp
=rg_list
;rp
;rp
=rp
->next
)
781 if(rp
->mask
->length
> longest
)
782 longest
= rp
->mask
->length
;
784 for(rp
=rg_list
;rp
;rp
=rp
->next
)
785 rg_displaygline(np
, rp
, longest
);
788 controlreply(np
, "Done.");
792 char *displaytype(int type
) {
794 static char ctypebuf
[10];
819 snprintf(ctypebuf
, sizeof(ctype
), "%1d:%s", type
, ctype
);
823 char *getsep(int longest
) {
824 static int lastlongest
= -1;
825 static char lenbuf
[1024];
832 if(longest >= sizeof(lenbuf) - 20)
833 longest = sizeof(lenbuf) - 20;
836 if(lastlongest
== -1) {
839 for(i
=0;i
<sizeof(lenbuf
)-1;i
++)
841 lenbuf
[sizeof(lenbuf
)-1] = '\0';
845 if(lastlongest
!= longest
) {
846 lenbuf
[lastlongest
] = '-';
847 lenbuf
[longest
] = '\0';
848 lastlongest
= longest
;
854 void rg_displaygline(nick
*np
, struct rg_struct
*rp
, int longest
) { /* could be a macro? I'll assume the C compiler inlines it */
855 char *sep
= getsep(longest
);
856 /* 12345678 12345678901234567890 123456789012345 12345678 12345 12345678901234567890 1234567 1234567 123456
857 ID Expires Set by Class Type Last seen (ago) Hits(s) Hits Reason
861 time_t t
= time(NULL
);
863 if(rp
->lastseen
== 0) {
864 strlcpy(d
, "(never)", sizeof(d
));
866 strlcpy(d
, longtoduration(t
- rp
->lastseen
, 2), sizeof(d
));
869 controlreply(np
, "%s", rp
->mask
->content
);
870 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
);
871 controlreply(np
, "%s", sep
);
874 int rg_spew(void *source
, int cargc
, char **cargv
) {
875 nick
*np
= (nick
*)source
, *tnp
;
876 int counter
= 0, erroroffset
, hostlen
, j
;
880 char hostname
[RG_MASKLEN
];
887 if(!(regex
= pcre_compile(cargv
[0], RG_PCREFLAGS
, &error
, &erroroffset
, NULL
))) {
888 controlreply(np
, "Error compiling expression %s at offset %d: %s", cargv
[0], erroroffset
, error
);
891 hint
= pcre_study(regex
, 0, &error
);
893 controlreply(np
, "Error studying expression %s: %s", cargv
[0], error
);
899 rg_logevent(np
, "regexspew", "%s", cargv
[0]);
901 for(j
=0;j
<NICKHASHSIZE
;j
++) {
902 for(tnp
=nicktable
[j
];tnp
;tnp
=tnp
->next
) {
903 hostlen
= RGBuildHostname(hostname
, tnp
);
904 pcreret
= pcre_exec(regex
, hint
, hostname
, hostlen
, 0, 0, ovector
, sizeof(ovector
) / sizeof(int));
906 if(counter
== rg_max_spew
) {
907 controlreply(np
, "Reached maximum spew count (%d) - aborting display.", rg_max_spew
);
908 } else if (counter
< rg_max_spew
) {
909 /* 15 should be number of bolds */
910 char boldbuf
[RG_MASKLEN
+ 15], *tp
, *fp
, *realname
= NULL
;
912 for(tp
=hostname
,fp
=boldbuf
;*tp
;) {
913 if(tp
- hostname
== ovector
[0]) {
917 if(tp
- hostname
== ovector
[1]) {
936 controlreply(np
, "%s (%s) (%dc)", boldbuf
, realname
, tnp
->channels
->cursi
);
942 controlreply(np
, "Done - %d matches.", counter
);
951 void rg_startup(void) {
954 struct rg_glinelist gll
;
956 rg_initglinelist(&gll
);
958 for(j
=0;j
<NICKHASHSIZE
;j
++)
959 for(np
=nicktable
[j
];np
;np
=np
->next
)
960 rg_scannick(np
, rg_gline_match
, &gll
);
962 rg_flushglines(&gll
);
965 void rg_freestruct(struct rg_struct
*rp
) {
966 freesstring(rp
->mask
);
967 freesstring(rp
->setby
);
968 freesstring(rp
->reason
);
969 pcre_free(rp
->regex
);
975 struct rg_struct
*rg_newstruct(time_t expires
) {
976 struct rg_struct
*rp
;
978 if (time(NULL
) >= expires
)
981 rp
= (struct rg_struct
*)malloc(sizeof(struct rg_struct
));
983 struct rg_struct
*tp
, *lp
;
985 memset(rp
, 0, sizeof(rg_struct
));
986 rp
->expires
= expires
;
988 for(lp
=NULL
,tp
=rg_list
;tp
;lp
=tp
,tp
=tp
->next
) {
989 if (expires
<= tp
->expires
) { /* <= possible, slight speed increase */
1012 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
) {
1013 struct rg_struct
*newrow
, *lp
, *cp
;
1015 char glineiddata
[1024];
1018 if (iexpires
== 0) {
1020 if(!protectedatoi(expires
, &qexpires
))
1022 rexpires
= (time_t)qexpires
;
1024 rexpires
= iexpires
;
1027 newrow
= rg_newstruct(rexpires
);
1033 for(p
=classes
;*p
;p
++) {
1034 if(!strcasecmp(class, *p
)) {
1041 newrow
->class = "unknown";
1043 if(!(newrow
->regex
= pcre_compile(mask
, RG_PCREFLAGS
, &error
, &erroroffset
, NULL
))) {
1044 Error("regexgline", ERR_WARNING
, "Error compiling expression %s at offset %d: %s", mask
, erroroffset
, error
);
1047 newrow
->hint
= pcre_study(newrow
->regex
, 0, &error
);
1049 Error("regexgline", ERR_WARNING
, "Error studying expression %s: %s", mask
, error
);
1050 pcre_free(newrow
->regex
);
1056 newrow
->hitssaved
= hitssaved
;
1057 newrow
->lastseen
= lastseen
;
1059 newrow
->mask
= getsstring(mask
, RG_REGEXGLINE_MAX
);
1061 Error("regexgline", ERR_WARNING
, "Error allocating memory for mask!");
1065 newrow
->setby
= getsstring(setby
, ACCOUNTLEN
);
1066 if(!newrow
->setby
) {
1067 Error("regexgline", ERR_WARNING
, "Error allocating memory for setby!");
1071 newrow
->reason
= getsstring(reason
, RG_REASON_MAX
);
1072 if(!newrow
->reason
) {
1073 Error("regexgline", ERR_WARNING
, "Error allocating memory for reason!");
1077 if(!protectedatoi(type
, &newrow
->type
))
1078 newrow
->type
= 0; /* just in case */
1080 snprintf(glineiddata
, sizeof(glineiddata
), "%s regexgline %s %s %s %d %d", mynumeric
->content
, mask
, setby
, reason
, (int)iexpires
, newrow
->type
);
1081 newrow
->glineid
= crc32(glineiddata
);
1088 freesstring(newrow
->mask
);
1090 freesstring(newrow
->setby
);
1092 freesstring(newrow
->reason
);
1093 pcre_free(newrow
->regex
);
1095 pcre_free(newrow
->hint
);
1098 for(lp
=NULL
,cp
=rg_list
;cp
;lp
=cp
,cp
=cp
->next
) {
1101 lp
->next
= cp
->next
;
1112 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?! */
1118 rg_loggline(rp
, np
);
1120 if (rp
->type
== INSTANT_HOST_GLINE
) {
1121 usercount
= np
->host
->clonecount
;
1124 if ((rp
->type
== INSTANT_IDENT_GLINE
) || (usercount
> rg_max_per_gline
)) {
1127 for(usercount
=0,tnp
=np
->host
->nicks
;tnp
;tnp
=tnp
->nextbyhost
)
1128 if(!ircd_strcmp(np
->ident
, tnp
->ident
))
1131 glineflags
= GLINE_ALWAYS_USER
;
1134 if(!strcmp(rp
->class, RESERVED_NICK_CLASS
)) {
1136 snprintf(reason
, sizeof(reason
), "AUTO %s (ID: %08lx)", rp
->reason
->content
, rp
->glineid
);
1137 glinebynick(np
, RESERVED_NICK_GLINE_DURATION
, reason
, GLINE_ALWAYS_NICK
, "regexgline");
1140 validdelay
= (rp
->type
== INSTANT_KILL
) || (rp
->type
== DELAYED_IDENT_GLINE
) || (rp
->type
== DELAYED_HOST_GLINE
) || (rp
->type
== DELAYED_KILL
);
1141 if (validdelay
|| (usercount
> rg_max_per_gline
)) {
1142 struct rg_glinenode
*nn
= (struct rg_glinenode
*)malloc(sizeof(struct rg_glinenode
));
1146 gll
->end
->next
= nn
;
1156 nn
->punish
= INSTANT_KILL
;
1158 nn
->punish
= rp
->type
;
1164 if (rp
->type
== INSTANT_IDENT_GLINE
) {
1165 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":"");
1166 } else if(rp
->type
== INSTANT_HOST_GLINE
) {
1167 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":"");
1172 rg_shadowserver(np
, rp
, rp
->type
);
1173 snprintf(reason
, sizeof(reason
), "AUTO: %s (ID: %08lx)", rp
->reason
->content
, rp
->glineid
);
1174 glinebynick(np
, rg_expiry_time
, reason
, glineflags
, "regexgline");
1178 static char *gvhost(nick
*np
) {
1179 static char buf
[NICKLEN
+1+USERLEN
+1+HOSTLEN
+1+ACCOUNTLEN
+4+REALLEN
+1+10];
1182 snprintf(buf
, sizeof(buf
), "%s!%s@%s/%s r(%s)", np
->nick
, np
->ident
, np
->host
->name
->content
, np
->authname
, np
->realname
->name
->content
);
1184 snprintf(buf
, sizeof(buf
), "%s!%s@%s r(%s)", np
->nick
, np
->ident
, np
->host
->name
->content
, np
->realname
->name
->content
);
1190 static int floodprotection
= 0;
1191 static int lastfloodspam
= 0;
1193 void rg_dogline(struct rg_glinelist
*gll
, nick
*np
, struct rg_struct
*rp
, char *matched
) {
1196 if(t
> floodprotection
) {
1197 floodprotection
= t
;
1198 } else if((floodprotection
- t
) / 8 > RG_NETWORK_WIDE_MAX_GLINES_PER_8_SEC
) {
1199 if(t
> lastfloodspam
+ 3600) {
1200 channel
*cp
= findchannel("#twilightzone");
1202 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
);
1203 controlwall(NO_OPER
, NL_MANAGEMENT
, "WARNING! REGEXGLINE DISABLED FOR AN HOUR DUE TO NETWORK WIDE LOOKING GLINE!");
1205 floodprotection
= t
+ RG_NETWORK_WIDE_MAX_GLINES_PER_8_SEC
* 3600 * 8;
1210 floodprotection
+=__rg_dogline(gll
, np
, rp
, matched
);
1213 void rg_logevent(nick
*np
, char *event
, char *details
, ...) {
1214 char eeevent
[RG_QUERY_BUF_SIZE
], eedetails
[RG_QUERY_BUF_SIZE
], eemask
[RG_QUERY_BUF_SIZE
], eeaccount
[RG_QUERY_BUF_SIZE
];
1215 char buf
[513], account
[ACCOUNTLEN
+ 1], mask
[RG_MASKLEN
];
1221 va_start(va
, details
);
1222 vsnprintf(buf
, sizeof(buf
), details
, va
);
1229 if (IsAccount(np
)) {
1230 strncpy(account
, np
->authname
, sizeof(account
) - 1);
1231 account
[sizeof(account
) - 1] = '\0';
1235 masklen
= RGBuildHostname(mask
, np
);
1241 dbescapestring(eeevent
, event
, strlen(event
));
1242 dbescapestring(eedetails
, buf
, strlen(buf
));
1243 dbescapestring(eeaccount
, account
, strlen(account
));
1244 dbescapestring(eemask
, mask
, masklen
);
1246 dbquery("INSERT INTO regexgline.clog (host, account, event, arg, ts) VALUES ('%s', '%s', '%s', '%s', NOW())", eemask
, eeaccount
, eeevent
, eedetails
);
1249 void rg_loggline(struct rg_struct
*rg
, nick
*np
) {
1250 char eenick
[RG_QUERY_BUF_SIZE
], eeuser
[RG_QUERY_BUF_SIZE
], eehost
[RG_QUERY_BUF_SIZE
], eereal
[RG_QUERY_BUF_SIZE
];
1254 rg
->lastseen
= time(NULL
);
1257 /* @paul: disabled */
1260 dbescapestring(eenick
, np
->nick
, strlen(np
->nick
));
1261 dbescapestring(eeuser
, np
->ident
, strlen(np
->ident
));
1262 dbescapestring(eehost
, np
->host
->name
->content
, strlen(np
->host
->name
->content
));
1263 dbescapestring(eereal
, np
->realname
->name
->content
, strlen(np
->realname
->name
->content
));
1265 dbquery("INSERT INTO regexgline.glog (glineid, nickname, username, hostname, realname, ts) VALUES (%d, '%s', '%s', '%s', '%s', NOW())", rg
->id
, eenick
, eeuser
, eehost
, eereal
);
1268 static unsigned int getrgmarker(void) {
1269 static unsigned int marker
= 0;
1273 struct rg_struct
*l
;
1275 /* If we wrapped to zero, zap the marker on all hosts */
1276 for(l
=rg_list
;l
;l
=l
->next
)
1284 void rg_flush_schedule(void *arg
) {
1285 struct rg_struct
*l
;
1287 for(l
=rg_list
;l
;l
=l
->next
) {
1291 dbquery("UPDATE regexgline.glines SET lastseen = %jd, hits = %lu WHERE id = %d", (intmax_t)l
->lastseen
, l
->hitssaved
, l
->id
);