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 MODULE_VERSION("1.43");
29 typedef struct rg_glinenode
{
31 struct rg_struct
*reason
;
33 struct rg_glinenode
*next
;
36 typedef struct rg_glinelist
{
37 struct rg_glinenode
*start
;
38 struct rg_glinenode
*end
;
41 typedef struct rg_delay
{
44 struct rg_struct
*reason
;
46 struct rg_delay
*next
;
49 #define GLINE_HEADER " ID Expires Set by Class Type Last seen (ago) Hits(p) Hits Reason"
53 void rg_setdelay(nick
*np
, struct rg_struct
*reason
, short punish
);
54 void rg_deletedelay(rg_delay
*delay
);
55 void rg_dodelay(void *arg
);
57 void rg_dogline(struct rg_glinelist
*gll
, nick
*np
, struct rg_struct
*rp
, char *matched
);
58 void rg_flush_schedule(void *arg
);
60 static DBModuleIdentifier dbid
;
61 static unsigned long highestid
= 0;
62 static int attached
= 0, started
= 0;
64 static unsigned int getrgmarker(void);
66 /* shadowserver only reports classes[0] */
67 static const char *classes
[] = { "drone", "proxy", "spam", "fakeauth", "other", (char *)0 };
70 sstring
*max_casualties
, *max_spew
, *expiry_time
, *max_per_gline
;
72 max_casualties
= getcopyconfigitem("regexgline", "maxcasualties", RGStringise(RG_MAX_CASUALTIES_DEFAULT
), 8);
73 if(!protectedatoi(max_casualties
->content
, &rg_max_casualties
))
74 rg_max_casualties
= RG_MAX_CASUALTIES_DEFAULT
;
76 freesstring(max_casualties
);
78 max_spew
= getcopyconfigitem("regexgline", "maxspew", RGStringise(RG_MAX_SPEW_DEFAULT
), 8);
79 if(!protectedatoi(max_spew
->content
, &rg_max_spew
))
80 rg_max_spew
= RG_MAX_SPEW_DEFAULT
;
82 freesstring(max_spew
);
84 expiry_time
= getcopyconfigitem("regexgline", "expirytime", RGStringise(RG_EXPIRY_TIME_DEFAULT
), 8);
85 if(!protectedatoi(expiry_time
->content
, &rg_expiry_time
))
86 rg_expiry_time
= RG_EXPIRY_TIME_DEFAULT
;
88 freesstring(expiry_time
);
90 max_per_gline
= getcopyconfigitem("regexgline", "maxpergline", RGStringise(RG_MAX_PER_GLINE_DEFAULT
), 8);
91 if(!protectedatoi(max_per_gline
->content
, &rg_max_per_gline
))
92 rg_max_per_gline
= RG_MAX_PER_GLINE_DEFAULT
;
94 freesstring(max_per_gline
);
103 Error("regexgline", ERR_STOP
, "Could not connect to database.");
108 struct rg_struct
*gp
= rg_list
, *oldgp
;
109 rg_delay
*delay
, *delaynext
;
112 deregisterhook(HOOK_NICK_NEWNICK
, &rg_nick
);
113 deregisterhook(HOOK_NICK_RENAME
, &rg_nick
);
114 deregisterhook(HOOK_NICK_LOSTNICK
, &rg_lostnick
);
115 deregistercontrolcmd("regexspew", rg_spew
);
116 deregistercontrolcmd("regexglist", rg_glist
);
117 deregistercontrolcmd("regexdelgline", rg_delgline
);
118 deregistercontrolcmd("regexgline", rg_gline
);
119 deregistercontrolcmd("regexidlookup", rg_idlist
);
123 for(delay
=rg_delays
;delay
;delay
=delaynext
) {
124 delaynext
=delay
->next
;
125 deleteschedule(delay
->sch
, rg_dodelay
, delay
);
131 deleteschedule(rg_schedule
, &rg_checkexpiry
, NULL
);
135 deleteallschedules(rg_flush_schedule
);
136 rg_flush_schedule(NULL
);
138 for(gp
=rg_list
;gp
;) {
141 rg_freestruct(oldgp
);
145 dbdetach("regexgline");
150 static int ignorable_nick(nick
*np
) {
151 if(IsOper(np
) || IsService(np
) || IsXOper(np
) || SIsService(&serverlist
[homeserver(np
->numeric
)]))
156 void rg_checkexpiry(void *arg
) {
157 struct rg_struct
*rp
= rg_list
, *lp
= NULL
;
158 time_t current
= time(NULL
);
161 if (current
>= rp
->expires
) {
162 dbquery("DELETE FROM regexglines WHERE id = %d", rp
->id
);
179 void rg_setdelay(nick
*np
, rg_struct
*reason
, short punish
) {
181 delay
= (rg_delay
*)malloc(sizeof(rg_delay
));
185 killuser(NULL
, np
, "%s (ID: %08lx)", reason
->reason
->content
, reason
->glineid
);
190 delay
->reason
= reason
;
191 delay
->punish
= punish
;
192 delay
->next
= rg_delays
;
195 delay
->sch
= scheduleoneshot(time(NULL
) + (RG_MINIMUM_DELAY_TIME
+ (rand() % RG_MAXIMUM_RAND_TIME
)), rg_dodelay
, delay
);
198 static void rg_shadowserver(nick
*np
, struct rg_struct
*reason
, int type
) {
201 if(reason
->class != classes
[0]) /* drone */
204 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
);
206 triggerhook(HOOK_SHADOW_SERVER
, (void *)buf
);
209 void rg_deletedelay(rg_delay
*delay
) {
210 rg_delay
*temp
, *prev
;
212 for (temp
=rg_delays
;temp
;temp
=temp
->next
) {
215 rg_delays
= temp
->next
;
217 prev
->next
= temp
->next
;
227 void rg_dodelay(void *arg
) {
228 rg_delay
*delay
= (rg_delay
*)arg
;
229 char hostname
[RG_MASKLEN
];
230 int hostlen
, usercount
= 0;
232 /* User or regex gline no longer exists */
233 if((!delay
->np
) || (!delay
->reason
)) {
234 rg_deletedelay(delay
);
238 hostlen
= RGBuildHostname(hostname
, delay
->np
);
240 /* User has wisely changed nicknames */
241 if(pcre_exec(delay
->reason
->regex
, delay
->reason
->hint
, hostname
, hostlen
, 0, 0, NULL
, 0) < 0) {
242 rg_deletedelay(delay
);
246 if (delay
->reason
->type
== DELAYED_HOST_GLINE
) {
247 usercount
= delay
->np
->host
->clonecount
;
248 snprintf(hostname
, sizeof(hostname
), "*@%s", IPtostr(delay
->np
->p_ipaddr
));
251 if((delay
->reason
->type
== DELAYED_IDENT_GLINE
) || (usercount
> rg_max_per_gline
)) {
254 for(usercount
=0,tnp
=delay
->np
->host
->nicks
;tnp
;tnp
=tnp
->nextbyhost
)
255 if(!ircd_strcmp(delay
->np
->ident
, tnp
->ident
))
258 snprintf(hostname
, sizeof(hostname
), "%s@%s", delay
->np
->ident
, IPtostr(delay
->np
->p_ipaddr
));
261 if ((delay
->reason
->type
== DELAYED_KILL
) || (usercount
> rg_max_per_gline
)) {
262 if (IsAccount(delay
->np
)) {
263 controlwall(NO_OPER
, NL_HITS
, "%s!%s@%s/%s matched delayed kill regex %08lx (class: %s)", delay
->np
->nick
, delay
->np
->ident
, delay
->np
->host
->name
->content
, delay
->np
->authname
, delay
->reason
->glineid
, delay
->reason
->class);
265 controlwall(NO_OPER
, NL_HITS
, "%s!%s@%s matched delayed kill regex %08lx (class: %s)", delay
->np
->nick
, delay
->np
->ident
, delay
->np
->host
->name
->content
, delay
->reason
->glineid
, delay
->reason
->class);
268 rg_shadowserver(delay
->np
, delay
->reason
, DELAYED_KILL
);
269 killuser(NULL
, delay
->np
, "%s (ID: %08lx)", delay
->reason
->reason
->content
, delay
->reason
->glineid
);
273 if (delay
->reason
->type
== DELAYED_IDENT_GLINE
) {
274 if (IsAccount(delay
->np
)) {
275 controlwall(NO_OPER
, NL_HITS
, "%s!%s@%s/%s matched delayed user@host gline regex %08lx (class: %s, hit %d user%s)", delay
->np
->nick
, delay
->np
->ident
, delay
->np
->host
->name
->content
, delay
->np
->authname
, delay
->reason
->glineid
, delay
->reason
->class, usercount
, (usercount
!=1)?"s":"");
277 controlwall(NO_OPER
, NL_HITS
, "%s!%s@%s matched delayed user@host gline regex %08lx (class: %s, hit %d user%s)", delay
->np
->nick
, delay
->np
->ident
, delay
->np
->host
->name
->content
, delay
->reason
->glineid
, delay
->reason
->class, usercount
, (usercount
!=1)?"s":"");
279 } else if (delay
->reason
->type
== DELAYED_HOST_GLINE
) {
280 if (IsAccount(delay
->np
)) {
281 controlwall(NO_OPER
, NL_HITS
, "%s!%s@%s/%s matched delayed *@host gline regex %08lx (class: %s, hit %d user%s)", delay
->np
->nick
, delay
->np
->ident
, delay
->np
->host
->name
->content
, delay
->np
->authname
, delay
->reason
->glineid
, delay
->reason
->class, usercount
, (usercount
!=1)?"s":"");
283 controlwall(NO_OPER
, NL_HITS
, "%s!%s@%s matched delayed *@host gline regex %08lx (class: %s, hit %d user%s)", delay
->np
->nick
, delay
->np
->ident
, delay
->np
->host
->name
->content
, delay
->reason
->glineid
, delay
->reason
->class, usercount
, (usercount
!=1)?"s":"");
289 rg_shadowserver(delay
->np
, delay
->reason
, delay
->reason
->type
);
290 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
);
291 rg_deletedelay(delay
);
294 void rg_initglinelist(struct rg_glinelist
*gll
) {
299 void rg_flushglines(struct rg_glinelist
*gll
) {
300 struct rg_glinenode
*nn
, *pn
;
301 for(nn
=gll
->start
;nn
;nn
=pn
) {
303 if(nn
->punish
== INSTANT_KILL
) {
304 if ( IsAccount(nn
->np
) ) {
305 controlwall(NO_OPER
, NL_HITS
, "%s!%s@%s/%s matched kill regex %08lx (class: %s)", nn
->np
->nick
, nn
->np
->ident
, nn
->np
->host
->name
->content
, nn
->np
->authname
, nn
->reason
->glineid
, nn
->reason
->class);
307 controlwall(NO_OPER
, NL_HITS
, "%s!%s@%s matched kill regex %08lx (class: %s)", nn
->np
->nick
, nn
->np
->ident
, nn
->np
->host
->name
->content
, nn
->reason
->glineid
, nn
->reason
->class);
310 rg_shadowserver(nn
->np
, nn
->reason
, nn
->punish
);
311 killuser(NULL
, nn
->np
, "%s (ID: %08lx)", nn
->reason
->reason
->content
, nn
->reason
->glineid
);
312 } else if ((nn
->punish
== DELAYED_IDENT_GLINE
) || (nn
->punish
== DELAYED_HOST_GLINE
) || (nn
->punish
== DELAYED_KILL
)) {
313 rg_setdelay(nn
->np
, nn
->reason
, nn
->punish
);
318 rg_initglinelist(gll
);
321 static void dbloaddata(DBConn
*dbconn
, void *arg
) {
322 DBResult
*dbres
= dbgetresult(dbconn
);
324 if(!dbquerysuccessful(dbres
)) {
325 Error("chanserv", ERR_ERROR
, "Error loading DB");
329 if (dbnumfields(dbres
) != 9) {
330 Error("regexgline", ERR_ERROR
, "DB format error");
334 while(dbfetchrow(dbres
)) {
335 unsigned long id
, hitssaved
;
337 char *gline
, *setby
, *reason
, *expires
, *type
, *class;
339 id
= strtoul(dbgetvalue(dbres
, 0), NULL
, 10);
343 gline
= dbgetvalue(dbres
, 1);
344 setby
= dbgetvalue(dbres
, 2);
345 reason
= dbgetvalue(dbres
, 3);
346 expires
= dbgetvalue(dbres
, 4);
347 type
= dbgetvalue(dbres
, 5);
348 class = dbgetvalue(dbres
, 6);
350 lastseen
= strtoul(dbgetvalue(dbres
, 7), NULL
, 10);
351 hitssaved
= strtoul(dbgetvalue(dbres
, 8), NULL
, 10);
353 if (!rg_newsstruct(id
, gline
, setby
, reason
, expires
, type
, 0, class, lastseen
, hitssaved
))
354 dbquery("DELETE FROM regexgline.glines WHERE id = %lu", id
);
360 static void dbloadfini(DBConn
*dbconn
, void *arg
) {
364 char helpbuf
[8192 * 2], allclasses
[8192];
366 sbinit(&b
, (char *)allclasses
, sizeof(allclasses
));
367 for(p
=classes
;*p
;p
++) {
368 sbaddstr(&b
, (char *)*p
);
373 snprintf(helpbuf
, sizeof(helpbuf
),
374 "Usage: regexgline <regex> <duration> <type> <class> <reason>\n"
375 "Adds a new regular expression pattern.\n"
376 "Duration is represented as 3d, 3M etc.\n"
377 "Class is one of the following: %s\n"
378 "Type is an integer which represents the following:\n"
379 "1 - Instant USER@IP GLINE (igu)\n"
380 "2 - Instant *@IP GLINE (igh)\n"
381 "3 - Instant KILL (ik)\n"
382 "4 - Delayed USER@IP GLINE (dgu)\n"
383 "5 - Delayed *@IP GLINE (dgh)\n"
384 "6 - Delayed KILL (dk)",
387 registercontrolhelpcmd("regexgline", NO_OPER
, 5, &rg_gline
, helpbuf
);
388 registercontrolhelpcmd("regexdelgline", NO_OPER
, 1, &rg_delgline
, "Usage: regexdelgline <pattern>\nDeletes a regular expression pattern.");
389 registercontrolhelpcmd("regexglist", NO_OPER
, 1, &rg_glist
, "Usage: regexglist <pattern>\nLists regular expression patterns.");
390 registercontrolhelpcmd("regexspew", NO_OPER
, 1, &rg_spew
, "Usage: regexspew <pattern>\nLists users currently on the network which match the given pattern.");
391 registercontrolhelpcmd("regexidlookup", NO_OPER
, 1, &rg_idlist
, "Usage: regexidlookup <id>\nFinds a regular expression pattern by it's ID number.");
393 registerhook(HOOK_NICK_NEWNICK
, &rg_nick
);
394 registerhook(HOOK_NICK_RENAME
, &rg_nick
);
395 registerhook(HOOK_NICK_LOSTNICK
, &rg_lostnick
);
398 rg_schedule
= schedulerecurring(time(NULL
) + 1, 0, 1, rg_checkexpiry
, NULL
);
399 schedulerecurring(time(NULL
) + 60, 0, 60, rg_flush_schedule
, NULL
);
402 void rg_dbload(void) {
403 dbattach("regexgline");
404 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
);
405 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
);
406 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
);
408 dbloadtable("regexgline.glines", NULL
, dbloaddata
, dbloadfini
);
411 void rg_nick(int hooknum
, void *arg
) {
412 nick
*np
= (nick
*)arg
;
413 struct rg_struct
*rp
;
414 char hostname
[RG_MASKLEN
];
416 struct rg_glinelist gll
;
418 rg_initglinelist(&gll
);
420 hostlen
= RGBuildHostname(hostname
, np
);
422 if(ignorable_nick(np
))
425 for(rp
=rg_list
;rp
;rp
=rp
->next
) {
426 if(pcre_exec(rp
->regex
, rp
->hint
, hostname
, hostlen
, 0, 0, NULL
, 0) >= 0) {
427 rg_dogline(&gll
, np
, rp
, hostname
);
432 rg_flushglines(&gll
);
435 void rg_lostnick(int hooknum
, void *arg
) {
436 nick
*np
= (nick
*)arg
;
439 /* Cleanup the delays */
440 for(delay
=rg_delays
;delay
;delay
=delay
->next
)
445 int rg_gline(void *source
, int cargc
, char **cargv
) {
446 nick
*np
= (nick
*)source
, *tnp
;
448 const char *expirybuf
;
449 int expiry
, count
, j
, hostlen
;
450 struct rg_struct
*rp
;
451 struct rg_glinelist gll
;
454 char eemask
[RG_QUERY_BUF_SIZE
], eesetby
[RG_QUERY_BUF_SIZE
], eereason
[RG_QUERY_BUF_SIZE
], eeclass
[RG_QUERY_BUF_SIZE
];
455 char hostname
[RG_MASKLEN
], *class, *reason
, *regex
, type
;
461 if ((strlen(cargv
[2]) != 1) || ((type
!= '1') && (type
!= '2') && (type
!= '3') && (type
!= '4') && (type
!= '5') && (type
!= '6'))) {
462 controlreply(np
, "Invalid type specified!");
470 for(p
=classes
;*p
;p
++)
471 if(!strcasecmp(class, *p
))
475 controlreply(np
, "Bad class supplied.");
479 if (!(expiry
= durationtolong(cargv
[1]))) {
480 controlreply(np
, "Invalid duration specified!");
484 for(rp
=rg_list
;rp
;rp
=rp
->next
) {
485 if (RGMasksEqual(rp
->mask
->content
, regex
)) {
486 controlreply(np
, "That regexgline already exists!");
491 if (rg_sanitycheck(regex
, &count
)) {
492 controlreply(np
, "Error in expression.");
494 } else if (count
< 0) {
495 controlreply(np
, "That expression would hit too many users (%d)!", -count
);
499 realexpiry
= expiry
+ time(NULL
);
501 dbescapestring(eemask
, regex
, strlen(regex
));
502 dbescapestring(eesetby
, np
->nick
, strlen(np
->nick
));
503 dbescapestring(eeclass
, class, strlen(class));
504 dbescapestring(eereason
, reason
, strlen(reason
));
506 highestid
= highestid
+ 1;
507 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
);
508 rp
= rg_newsstruct(highestid
, regex
, np
->nick
, reason
, "", cargv
[2], realexpiry
, class, 0, 0);
510 rg_initglinelist(&gll
);
512 for(j
=0;j
<NICKHASHSIZE
;j
++) {
513 for(tnp
=nicktable
[j
];tnp
;tnp
=tnp
->next
) {
514 if(ignorable_nick(tnp
))
517 hostlen
= RGBuildHostname(hostname
, tnp
);
518 if(pcre_exec(rp
->regex
, rp
->hint
, hostname
, hostlen
, 0, 0, NULL
, 0) >= 0)
519 rg_dogline(&gll
, tnp
, rp
, hostname
);
523 rg_flushglines(&gll
);
525 expirybuf
= longtoduration(expiry
, 0);
527 rg_logevent(np
, "regexgline", "%s %d %d %s %s", regex
, expiry
, count
, class, reason
);
528 controlreply(np
, "Added regexgline: %s (class: %s, expires in: %s, hit %d user%s): %s", regex
, class, expirybuf
, count
, (count
!=1)?"s":"", reason
);
529 /* If we are using NO, can we safely assume the user is authed here and use ->authname? */
530 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
);
535 int rg_sanitycheck(char *mask
, int *count
) {
537 char hostname
[RG_MASKLEN
];
538 int erroroffset
, hostlen
, j
, masklen
= strlen(mask
);
543 if((masklen
< RG_MIN_MASK_LEN
) || (masklen
> RG_REGEXGLINE_MAX
))
546 if(!(regex
= pcre_compile(mask
, RG_PCREFLAGS
, &error
, &erroroffset
, NULL
))) {
547 Error("regexgline", ERR_WARNING
, "Error compiling expression %s at offset %d: %s", mask
, erroroffset
, error
);
550 hint
= pcre_study(regex
, 0, &error
);
552 Error("regexgline", ERR_WARNING
, "Error studying expression %s: %s", mask
, error
);
559 for(j
=0;j
<NICKHASHSIZE
;j
++) {
560 for(np
=nicktable
[j
];np
;np
=np
->next
) {
561 hostlen
= RGBuildHostname(hostname
, np
);
562 if(pcre_exec(regex
, hint
, hostname
, hostlen
, 0, 0, NULL
, 0) >= 0) {
572 if(*count
>= rg_max_casualties
)
578 int rg_delgline(void *source
, int cargc
, char **cargv
) {
579 nick
*np
= (nick
*)source
;
581 struct rg_struct
*rp
= rg_list
, *last
= NULL
;
587 rg_logevent(np
, "regexdelgline", "%s", cargv
[0]);
589 if(RGMasksEqual(rp
->mask
->content
, cargv
[0])) {
592 /* Cleanup the delays */
593 for(delay
=rg_delays
;delay
;delay
=delay
->next
)
594 if(delay
->reason
==rp
)
595 delay
->reason
= NULL
;
597 dbquery("DELETE FROM regexgline.glines WHERE id = %d", rp
->id
);
599 last
->next
= rp
->next
;
613 controlreply(np
, "Deleted (matched: %d).", count
);
614 /* If we are using NO, can we safely assume the user is authed here and use ->authname? */
615 controlwall(NO_OPER
, NL_GLINES
, "%s!%s@%s/%s removed regexgline: %s", np
->nick
, np
->ident
, np
->host
->name
->content
, np
->authname
, cargv
[0]);
617 controlreply(np
, "No glines matched: %s", cargv
[0]);
622 int rg_idlist(void *source
, int cargc
, char **cargv
) {
623 nick
*np
= (nick
*)source
;
627 } else if (strlen(cargv
[0]) != 8) {
628 controlreply(np
, "Invalid gline id!");
631 struct rg_struct
*rp
;
632 unsigned long id
= 0;
637 if(0xff == rc_hexlookup
[(int)cargv
[0][i
]]) {
638 controlreply(np
, "Invalid gline id!");
641 id
= (id
<< 4) | rc_hexlookup
[(int)cargv
[0][i
]];
646 controlreply(np
, GLINE_HEADER
);
647 for(rp
=rg_list
;rp
;rp
=rp
->next
) {
648 if(id
== rp
->glineid
) {
650 if(rp
->mask
->length
> longest
)
651 longest
= rp
->mask
->length
;
655 for(rp
=rg_list
;rp
;rp
=rp
->next
)
657 rg_displaygline(np
, rp
, longest
);
658 controlreply(np
, "Done.");
664 int rg_glist(void *source
, int cargc
, char **cargv
) {
665 nick
*np
= (nick
*)source
;
666 struct rg_struct
*rp
;
676 if(!(regex
= pcre_compile(cargv
[0], RG_PCREFLAGS
, &error
, &erroroffset
, NULL
))) {
677 controlreply(np
, "Error compiling expression %s at offset %d: %s", cargv
[0], erroroffset
, error
);
680 hint
= pcre_study(regex
, 0, &error
);
682 controlreply(np
, "Error studying expression %s: %s", cargv
[0], error
);
689 rg_logevent(np
, "regexglist", "%s", cargv
[0]);
690 controlreply(np
, GLINE_HEADER
);
691 for(rp
=rg_list
;rp
;rp
=rp
->next
) {
692 if(pcre_exec(regex
, hint
, rp
->mask
->content
, rp
->mask
->length
, 0, 0, NULL
, 0) >= 0) {
694 if(rp
->mask
->length
> longest
)
695 longest
= rp
->mask
->length
;
699 for(rp
=rg_list
;rp
;rp
=rp
->next
)
701 rg_displaygline(np
, rp
, longest
);
708 rg_logevent(np
, "regexglist", "%s", "");
709 controlreply(np
, GLINE_HEADER
);
710 for(rp
=rg_list
;rp
;rp
=rp
->next
)
711 if(rp
->mask
->length
> longest
)
712 longest
= rp
->mask
->length
;
714 for(rp
=rg_list
;rp
;rp
=rp
->next
)
715 rg_displaygline(np
, rp
, longest
);
718 controlreply(np
, "Done.");
722 char *displaytype(int type
) {
724 static char ctypebuf
[10];
749 snprintf(ctypebuf
, sizeof(ctype
), "%1d:%s", type
, ctype
);
753 char *getsep(int longest
) {
754 static int lastlongest
= -1;
755 static char lenbuf
[1024];
762 if(longest >= sizeof(lenbuf) - 20)
763 longest = sizeof(lenbuf) - 20;
766 if(lastlongest
== -1) {
769 for(i
=0;i
<sizeof(lenbuf
)-1;i
++)
771 lenbuf
[sizeof(lenbuf
)-1] = '\0';
775 if(lastlongest
!= longest
) {
776 lenbuf
[lastlongest
] = '-';
777 lenbuf
[longest
] = '\0';
778 lastlongest
= longest
;
784 void rg_displaygline(nick
*np
, struct rg_struct
*rp
, int longest
) { /* could be a macro? I'll assume the C compiler inlines it */
785 char *sep
= getsep(longest
);
786 /* 12345678 12345678901234567890 123456789012345 12345678 12345 12345678901234567890 1234567 1234567 123456
787 ID Expires Set by Class Type Last seen (ago) Hits(s) Hits Reason
791 time_t t
= time(NULL
);
793 if(rp
->lastseen
== 0) {
794 strlcpy(d
, "(never)", sizeof(d
));
796 strlcpy(d
, longtoduration(t
- rp
->lastseen
, 2), sizeof(d
));
799 controlreply(np
, "%s", rp
->mask
->content
);
800 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
);
801 controlreply(np
, "%s", sep
);
804 int rg_spew(void *source
, int cargc
, char **cargv
) {
805 nick
*np
= (nick
*)source
, *tnp
;
806 int counter
= 0, erroroffset
, hostlen
, j
;
810 char hostname
[RG_MASKLEN
];
817 if(!(regex
= pcre_compile(cargv
[0], RG_PCREFLAGS
, &error
, &erroroffset
, NULL
))) {
818 controlreply(np
, "Error compiling expression %s at offset %d: %s", cargv
[0], erroroffset
, error
);
821 hint
= pcre_study(regex
, 0, &error
);
823 controlreply(np
, "Error studying expression %s: %s", cargv
[0], error
);
829 rg_logevent(np
, "regexspew", "%s", cargv
[0]);
831 for(j
=0;j
<NICKHASHSIZE
;j
++) {
832 for(tnp
=nicktable
[j
];tnp
;tnp
=tnp
->next
) {
833 hostlen
= RGBuildHostname(hostname
, tnp
);
834 pcreret
= pcre_exec(regex
, hint
, hostname
, hostlen
, 0, 0, ovector
, sizeof(ovector
) / sizeof(int));
836 if(counter
== rg_max_spew
) {
837 controlreply(np
, "Reached maximum spew count (%d) - aborting display.", rg_max_spew
);
838 } else if (counter
< rg_max_spew
) {
839 /* 15 should be number of bolds */
840 char boldbuf
[RG_MASKLEN
+ 15], *tp
, *fp
, *realname
= NULL
;
842 for(tp
=hostname
,fp
=boldbuf
;*tp
;) {
843 if(tp
- hostname
== ovector
[0]) {
847 if(tp
- hostname
== ovector
[1]) {
866 controlreply(np
, "%s (%s) (%dc)", boldbuf
, realname
, tnp
->channels
->cursi
);
872 controlreply(np
, "Done - %d matches.", counter
);
881 void rg_startup(void) {
884 struct rg_struct
*rp
;
885 struct rg_glinelist gll
;
886 char hostname
[RG_MASKLEN
];
888 rg_initglinelist(&gll
);
890 for(j
=0;j
<NICKHASHSIZE
;j
++) {
891 for(np
=nicktable
[j
];np
;np
=np
->next
) {
892 if(ignorable_nick(np
))
894 hostlen
= RGBuildHostname(hostname
, np
);
895 for(rp
=rg_list
;rp
;rp
=rp
->next
) {
896 if(pcre_exec(rp
->regex
, rp
->hint
, hostname
, hostlen
, 0, 0, NULL
, 0) >= 0) {
897 rg_dogline(&gll
, np
, rp
, hostname
);
904 rg_flushglines(&gll
);
907 void rg_freestruct(struct rg_struct
*rp
) {
908 freesstring(rp
->mask
);
909 freesstring(rp
->setby
);
910 freesstring(rp
->reason
);
911 pcre_free(rp
->regex
);
917 struct rg_struct
*rg_newstruct(time_t expires
) {
918 struct rg_struct
*rp
;
920 if (time(NULL
) >= expires
)
923 rp
= (struct rg_struct
*)malloc(sizeof(struct rg_struct
));
925 struct rg_struct
*tp
, *lp
;
927 memset(rp
, 0, sizeof(rg_struct
));
928 rp
->expires
= expires
;
930 for(lp
=NULL
,tp
=rg_list
;tp
;lp
=tp
,tp
=tp
->next
) {
931 if (expires
<= tp
->expires
) { /* <= possible, slight speed increase */
954 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
) {
955 struct rg_struct
*newrow
, *lp
, *cp
;
957 char glineiddata
[1024];
962 if(!protectedatoi(expires
, &qexpires
))
964 rexpires
= (time_t)qexpires
;
969 newrow
= rg_newstruct(rexpires
);
975 for(p
=classes
;*p
;p
++) {
976 if(!strcasecmp(class, *p
)) {
983 newrow
->class = "unknown";
985 if(!(newrow
->regex
= pcre_compile(mask
, RG_PCREFLAGS
, &error
, &erroroffset
, NULL
))) {
986 Error("regexgline", ERR_WARNING
, "Error compiling expression %s at offset %d: %s", mask
, erroroffset
, error
);
989 newrow
->hint
= pcre_study(newrow
->regex
, 0, &error
);
991 Error("regexgline", ERR_WARNING
, "Error studying expression %s: %s", mask
, error
);
992 pcre_free(newrow
->regex
);
998 newrow
->hitssaved
= hitssaved
;
999 newrow
->lastseen
= lastseen
;
1001 newrow
->mask
= getsstring(mask
, RG_REGEXGLINE_MAX
);
1003 Error("regexgline", ERR_WARNING
, "Error allocating memory for mask!");
1007 newrow
->setby
= getsstring(setby
, ACCOUNTLEN
);
1008 if(!newrow
->setby
) {
1009 Error("regexgline", ERR_WARNING
, "Error allocating memory for setby!");
1013 newrow
->reason
= getsstring(reason
, RG_REASON_MAX
);
1014 if(!newrow
->reason
) {
1015 Error("regexgline", ERR_WARNING
, "Error allocating memory for reason!");
1019 if(!protectedatoi(type
, &newrow
->type
))
1020 newrow
->type
= 0; /* just in case */
1022 snprintf(glineiddata
, sizeof(glineiddata
), "%s regexgline %s %s %s %d %d", mynumeric
->content
, mask
, setby
, reason
, (int)iexpires
, newrow
->type
);
1023 newrow
->glineid
= crc32(glineiddata
);
1030 freesstring(newrow
->mask
);
1032 freesstring(newrow
->setby
);
1034 freesstring(newrow
->reason
);
1035 pcre_free(newrow
->regex
);
1037 pcre_free(newrow
->hint
);
1040 for(lp
=NULL
,cp
=rg_list
;cp
;lp
=cp
,cp
=cp
->next
) {
1043 lp
->next
= cp
->next
;
1054 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?! */
1055 char hostname
[RG_MASKLEN
];
1059 rg_loggline(rp
, np
);
1061 if (rp
->type
== INSTANT_HOST_GLINE
) {
1062 usercount
= np
->host
->clonecount
;
1063 snprintf(hostname
, sizeof(hostname
), "*@%s", IPtostr(np
->p_ipaddr
));
1066 if ((rp
->type
== INSTANT_IDENT_GLINE
) || (usercount
> rg_max_per_gline
)) {
1069 for(usercount
=0,tnp
=np
->host
->nicks
;tnp
;tnp
=tnp
->nextbyhost
)
1070 if(!ircd_strcmp(np
->ident
, tnp
->ident
))
1073 snprintf(hostname
, sizeof(hostname
), "%s@%s", np
->ident
, IPtostr(np
->p_ipaddr
));
1076 validdelay
= (rp
->type
== INSTANT_KILL
) || (rp
->type
== DELAYED_IDENT_GLINE
) || (rp
->type
== DELAYED_HOST_GLINE
) || (rp
->type
== DELAYED_KILL
);
1077 if (validdelay
|| (usercount
> rg_max_per_gline
)) {
1078 struct rg_glinenode
*nn
= (struct rg_glinenode
*)malloc(sizeof(struct rg_glinenode
));
1082 gll
->end
->next
= nn
;
1092 nn
->punish
= INSTANT_KILL
;
1094 nn
->punish
= rp
->type
;
1100 if (rp
->type
== INSTANT_IDENT_GLINE
) {
1101 if (IsAccount(np
)) {
1102 controlwall(NO_OPER
, NL_HITS
, "%s!%s@%s/%s matched user@host gline regex %08lx (class: %s, hit %d user%s)", np
->nick
, np
->ident
, np
->host
->name
->content
, np
->authname
, rp
->glineid
, rp
->class, usercount
, (usercount
!=1)?"s":"");
1104 controlwall(NO_OPER
, NL_HITS
, "%s!%s@%s matched user@host gline regex %08lx (class: %s, hit %d user%s)", np
->nick
, np
->ident
, np
->host
->name
->content
, rp
->glineid
, rp
->class, usercount
, (usercount
!=1)?"s":"");
1106 } else if(rp
->type
== INSTANT_HOST_GLINE
) {
1107 if (IsAccount(np
)) {
1108 controlwall(NO_OPER
, NL_HITS
, "%s!%s@%s/%s matched *@host gline regex %08lx (class: %s, hit %d user%s)", np
->nick
, np
->ident
, np
->host
->name
->content
, np
->authname
, rp
->glineid
, rp
->class, usercount
, (usercount
!=1)?"s":"");
1110 controlwall(NO_OPER
, NL_HITS
, "%s!%s@%s matched *@host gline regex %08lx (class: %s, hit %d user%s)", np
->nick
, np
->ident
, np
->host
->name
->content
, rp
->glineid
, rp
->class, usercount
, (usercount
!=1)?"s":"");
1116 rg_shadowserver(np
, rp
, rp
->type
);
1117 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
);
1121 static int floodprotection
= 0;
1122 static int lastfloodspam
= 0;
1124 void rg_dogline(struct rg_glinelist
*gll
, nick
*np
, struct rg_struct
*rp
, char *matched
) {
1127 if(t
> floodprotection
) {
1128 floodprotection
= t
;
1129 } else if((floodprotection
- t
) / 8 > RG_NETWORK_WIDE_MAX_GLINES_PER_8_SEC
) {
1130 if(t
> lastfloodspam
+ 3600) {
1131 channel
*cp
= findchannel("#twilightzone");
1133 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
);
1134 controlwall(NO_OPER
, NL_MANAGEMENT
, "WARNING! REGEXGLINE DISABLED FOR AN HOUR DUE TO NETWORK WIDE LOOKING GLINE!");
1136 floodprotection
= t
+ RG_NETWORK_WIDE_MAX_GLINES_PER_8_SEC
* 3600 * 8;
1141 floodprotection
+=__rg_dogline(gll
, np
, rp
, matched
);
1144 void rg_logevent(nick
*np
, char *event
, char *details
, ...) {
1145 char eeevent
[RG_QUERY_BUF_SIZE
], eedetails
[RG_QUERY_BUF_SIZE
], eemask
[RG_QUERY_BUF_SIZE
], eeaccount
[RG_QUERY_BUF_SIZE
];
1146 char buf
[513], account
[ACCOUNTLEN
+ 1], mask
[RG_MASKLEN
];
1152 va_start(va
, details
);
1153 vsnprintf(buf
, sizeof(buf
), details
, va
);
1160 if (IsAccount(np
)) {
1161 strncpy(account
, np
->authname
, sizeof(account
) - 1);
1162 account
[sizeof(account
) - 1] = '\0';
1166 masklen
= RGBuildHostname(mask
, np
);
1172 dbescapestring(eeevent
, event
, strlen(event
));
1173 dbescapestring(eedetails
, buf
, strlen(buf
));
1174 dbescapestring(eeaccount
, account
, strlen(account
));
1175 dbescapestring(eemask
, mask
, masklen
);
1177 dbquery("INSERT INTO regexgline.clog (host, account, event, arg, ts) VALUES ('%s', '%s', '%s', '%s', NOW())", eemask
, eeaccount
, eeevent
, eedetails
);
1180 void rg_loggline(struct rg_struct
*rg
, nick
*np
) {
1181 char eenick
[RG_QUERY_BUF_SIZE
], eeuser
[RG_QUERY_BUF_SIZE
], eehost
[RG_QUERY_BUF_SIZE
], eereal
[RG_QUERY_BUF_SIZE
];
1185 rg
->lastseen
= time(NULL
);
1188 /* @paul: disabled */
1191 dbescapestring(eenick
, np
->nick
, strlen(np
->nick
));
1192 dbescapestring(eeuser
, np
->ident
, strlen(np
->ident
));
1193 dbescapestring(eehost
, np
->host
->name
->content
, strlen(np
->host
->name
->content
));
1194 dbescapestring(eereal
, np
->realname
->name
->content
, strlen(np
->realname
->name
->content
));
1196 dbquery("INSERT INTO regexgline.glog (glineid, nickname, username, hostname, realname, ts) VALUES (%d, '%s', '%s', '%s', '%s', NOW())", rg
->id
, eenick
, eeuser
, eehost
, eereal
);
1199 static unsigned int getrgmarker(void) {
1200 static unsigned int marker
= 0;
1204 struct rg_struct
*l
;
1206 /* If we wrapped to zero, zap the marker on all hosts */
1207 for(l
=rg_list
;l
;l
=l
->next
)
1215 void rg_flush_schedule(void *arg
) {
1216 struct rg_struct
*l
;
1218 for(l
=rg_list
;l
;l
=l
->next
) {
1222 dbquery("UPDATE regexgline.glines SET lastseen = %jd, hits = %lu WHERE id = %d", (intmax_t)l
->lastseen
, l
->hitssaved
, l
->id
);