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 void rg_checkexpiry(void *arg
) {
151 struct rg_struct
*rp
= rg_list
, *lp
= NULL
;
152 time_t current
= time(NULL
);
155 if (current
>= rp
->expires
) {
172 void rg_setdelay(nick
*np
, rg_struct
*reason
, short punish
) {
174 delay
= (rg_delay
*)malloc(sizeof(rg_delay
));
178 killuser(NULL
, np
, "%s (ID: %08lx)", reason
->reason
->content
, reason
->glineid
);
183 delay
->reason
= reason
;
184 delay
->punish
= punish
;
185 delay
->next
= rg_delays
;
188 delay
->sch
= scheduleoneshot(time(NULL
) + (RG_MINIMUM_DELAY_TIME
+ (rand() % RG_MAXIMUM_RAND_TIME
)), rg_dodelay
, delay
);
191 static void rg_shadowserver(nick
*np
, struct rg_struct
*reason
, int type
) {
194 if(reason
->class != classes
[0]) /* drone */
197 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
);
199 triggerhook(HOOK_SHADOW_SERVER
, (void *)buf
);
202 void rg_deletedelay(rg_delay
*delay
) {
203 rg_delay
*temp
, *prev
;
205 for (temp
=rg_delays
;temp
;temp
=temp
->next
) {
208 rg_delays
= temp
->next
;
210 prev
->next
= temp
->next
;
220 void rg_dodelay(void *arg
) {
221 rg_delay
*delay
= (rg_delay
*)arg
;
222 char hostname
[RG_MASKLEN
];
223 int hostlen
, usercount
= 0;
225 /* User or regex gline no longer exists */
226 if((!delay
->np
) || (!delay
->reason
)) {
227 rg_deletedelay(delay
);
231 hostlen
= RGBuildHostname(hostname
, delay
->np
);
233 /* User has wisely changed nicknames */
234 if(pcre_exec(delay
->reason
->regex
, delay
->reason
->hint
, hostname
, hostlen
, 0, 0, NULL
, 0) < 0) {
235 rg_deletedelay(delay
);
239 if (delay
->reason
->type
== DELAYED_HOST_GLINE
) {
240 usercount
= delay
->np
->host
->clonecount
;
241 snprintf(hostname
, sizeof(hostname
), "*@%s", IPtostr(delay
->np
->p_ipaddr
));
244 if((delay
->reason
->type
== DELAYED_IDENT_GLINE
) || (usercount
> rg_max_per_gline
)) {
247 for(usercount
=0,tnp
=delay
->np
->host
->nicks
;tnp
;tnp
=tnp
->nextbyhost
)
248 if(!ircd_strcmp(delay
->np
->ident
, tnp
->ident
))
251 snprintf(hostname
, sizeof(hostname
), "%s@%s", delay
->np
->ident
, IPtostr(delay
->np
->p_ipaddr
));
254 if ((delay
->reason
->type
== DELAYED_KILL
) || (usercount
> rg_max_per_gline
)) {
255 if (IsAccount(delay
->np
)) {
256 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);
258 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);
261 rg_shadowserver(delay
->np
, delay
->reason
, DELAYED_KILL
);
262 killuser(NULL
, delay
->np
, "%s (ID: %08lx)", delay
->reason
->reason
->content
, delay
->reason
->glineid
);
266 if (delay
->reason
->type
== DELAYED_IDENT_GLINE
) {
267 if (IsAccount(delay
->np
)) {
268 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":"");
270 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":"");
272 } else if (delay
->reason
->type
== DELAYED_HOST_GLINE
) {
273 if (IsAccount(delay
->np
)) {
274 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":"");
276 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":"");
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 if ( IsAccount(nn
->np
) ) {
298 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);
300 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);
303 rg_shadowserver(nn
->np
, nn
->reason
, nn
->punish
);
304 killuser(NULL
, nn
->np
, "%s (ID: %08lx)", nn
->reason
->reason
->content
, nn
->reason
->glineid
);
305 } else if ((nn
->punish
== DELAYED_IDENT_GLINE
) || (nn
->punish
== DELAYED_HOST_GLINE
) || (nn
->punish
== DELAYED_KILL
)) {
306 rg_setdelay(nn
->np
, nn
->reason
, nn
->punish
);
311 rg_initglinelist(gll
);
314 static void dbloaddata(DBConn
*dbconn
, void *arg
) {
315 DBResult
*dbres
= dbgetresult(dbconn
);
317 if(!dbquerysuccessful(dbres
)) {
318 Error("chanserv", ERR_ERROR
, "Error loading DB");
322 if (dbnumfields(dbres
) != 9) {
323 Error("regexgline", ERR_ERROR
, "DB format error");
327 while(dbfetchrow(dbres
)) {
328 unsigned long id
, hitssaved
;
330 char *gline
, *setby
, *reason
, *expires
, *type
, *class;
332 id
= strtoul(dbgetvalue(dbres
, 0), NULL
, 10);
336 gline
= dbgetvalue(dbres
, 1);
337 setby
= dbgetvalue(dbres
, 2);
338 reason
= dbgetvalue(dbres
, 3);
339 expires
= dbgetvalue(dbres
, 4);
340 type
= dbgetvalue(dbres
, 5);
341 class = dbgetvalue(dbres
, 6);
343 lastseen
= strtoul(dbgetvalue(dbres
, 7), NULL
, 10);
344 hitssaved
= strtoul(dbgetvalue(dbres
, 8), NULL
, 10);
346 if (!rg_newsstruct(id
, gline
, setby
, reason
, expires
, type
, 0, class, lastseen
, hitssaved
))
347 dbquery("DELETE FROM regexgline.glines WHERE id = %lu", id
);
353 static void dbloadfini(DBConn
*dbconn
, void *arg
) {
357 char helpbuf
[8192 * 2], allclasses
[8192];
359 sbinit(&b
, (char *)allclasses
, sizeof(allclasses
));
360 for(p
=classes
;*p
;p
++) {
361 sbaddstr(&b
, (char *)*p
);
366 snprintf(helpbuf
, sizeof(helpbuf
),
367 "Usage: regexgline <regex> <duration> <type> <class> <reason>\n"
368 "Adds a new regular expression pattern.\n"
369 "Duration is represented as 3d, 3M etc.\n"
370 "Class is one of the following: %s\n"
371 "Type is an integer which represents the following:\n"
372 "1 - Instant USER@IP GLINE (igu)\n"
373 "2 - Instant *@IP GLINE (igh)\n"
374 "3 - Instant KILL (ik)\n"
375 "4 - Delayed USER@IP GLINE (dgu)\n"
376 "5 - Delayed *@IP GLINE (dgh)\n"
377 "6 - Delayed KILL (dk)",
380 registercontrolhelpcmd("regexgline", NO_OPER
, 5, &rg_gline
, helpbuf
);
381 registercontrolhelpcmd("regexdelgline", NO_OPER
, 1, &rg_delgline
, "Usage: regexdelgline <pattern>\nDeletes a regular expression pattern.");
382 registercontrolhelpcmd("regexglist", NO_OPER
, 1, &rg_glist
, "Usage: regexglist <pattern>\nLists regular expression patterns.");
383 registercontrolhelpcmd("regexspew", NO_OPER
, 1, &rg_spew
, "Usage: regexspew <pattern>\nLists users currently on the network which match the given pattern.");
384 registercontrolhelpcmd("regexidlookup", NO_OPER
, 1, &rg_idlist
, "Usage: regexidlookup <id>\nFinds a regular expression pattern by it's ID number.");
386 registerhook(HOOK_NICK_NEWNICK
, &rg_nick
);
387 registerhook(HOOK_NICK_RENAME
, &rg_nick
);
388 registerhook(HOOK_NICK_LOSTNICK
, &rg_lostnick
);
391 rg_schedule
= schedulerecurring(time(NULL
) + 1, 0, 1, rg_checkexpiry
, NULL
);
392 schedulerecurring(time(NULL
) + 60, 0, 60, rg_flush_schedule
, NULL
);
395 void rg_dbload(void) {
396 dbattach("regexgline");
397 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
);
398 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
);
399 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
);
401 dbloadtable("regexgline.glines", NULL
, dbloaddata
, dbloadfini
);
404 void rg_nick(int hooknum
, void *arg
) {
405 nick
*np
= (nick
*)arg
;
406 struct rg_struct
*rp
;
407 char hostname
[RG_MASKLEN
];
409 struct rg_glinelist gll
;
411 rg_initglinelist(&gll
);
413 hostlen
= RGBuildHostname(hostname
, np
);
415 if(IsOper(np
) || IsService(np
) || IsXOper(np
))
418 for(rp
=rg_list
;rp
;rp
=rp
->next
) {
419 if(pcre_exec(rp
->regex
, rp
->hint
, hostname
, hostlen
, 0, 0, NULL
, 0) >= 0) {
420 rg_dogline(&gll
, np
, rp
, hostname
);
425 rg_flushglines(&gll
);
428 void rg_lostnick(int hooknum
, void *arg
) {
429 nick
*np
= (nick
*)arg
;
432 /* Cleanup the delays */
433 for(delay
=rg_delays
;delay
;delay
=delay
->next
)
438 int rg_gline(void *source
, int cargc
, char **cargv
) {
439 nick
*np
= (nick
*)source
, *tnp
;
441 const char *expirybuf
;
442 int expiry
, count
, j
, hostlen
;
443 struct rg_struct
*rp
;
444 struct rg_glinelist gll
;
447 char eemask
[RG_QUERY_BUF_SIZE
], eesetby
[RG_QUERY_BUF_SIZE
], eereason
[RG_QUERY_BUF_SIZE
], eeclass
[RG_QUERY_BUF_SIZE
];
448 char hostname
[RG_MASKLEN
], *class, *reason
, *regex
, type
;
454 if ((strlen(cargv
[2]) != 1) || ((type
!= '1') && (type
!= '2') && (type
!= '3') && (type
!= '4') && (type
!= '5') && (type
!= '6'))) {
455 controlreply(np
, "Invalid type specified!");
463 for(p
=classes
;*p
;p
++)
464 if(!strcasecmp(class, *p
))
468 controlreply(np
, "Bad class supplied.");
472 if (!(expiry
= durationtolong(cargv
[1]))) {
473 controlreply(np
, "Invalid duration specified!");
477 for(rp
=rg_list
;rp
;rp
=rp
->next
) {
478 if (RGMasksEqual(rp
->mask
->content
, regex
)) {
479 controlreply(np
, "That regexgline already exists!");
484 if (rg_sanitycheck(regex
, &count
)) {
485 controlreply(np
, "Error in expression.");
487 } else if (count
< 0) {
488 controlreply(np
, "That expression would hit too many users (%d)!", -count
);
492 realexpiry
= expiry
+ time(NULL
);
494 dbescapestring(eemask
, regex
, strlen(regex
));
495 dbescapestring(eesetby
, np
->nick
, strlen(np
->nick
));
496 dbescapestring(eeclass
, class, strlen(class));
497 dbescapestring(eereason
, reason
, strlen(reason
));
499 highestid
= highestid
+ 1;
500 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
);
501 rp
= rg_newsstruct(highestid
, regex
, np
->nick
, reason
, "", cargv
[2], realexpiry
, class, 0, 0);
503 rg_initglinelist(&gll
);
505 for(j
=0;j
<NICKHASHSIZE
;j
++) {
506 for(tnp
=nicktable
[j
];tnp
;tnp
=tnp
->next
) {
507 if(IsOper(tnp
) || IsService(tnp
) || IsXOper(tnp
))
510 hostlen
= RGBuildHostname(hostname
, tnp
);
511 if(pcre_exec(rp
->regex
, rp
->hint
, hostname
, hostlen
, 0, 0, NULL
, 0) >= 0)
512 rg_dogline(&gll
, tnp
, rp
, hostname
);
516 rg_flushglines(&gll
);
518 expirybuf
= longtoduration(expiry
, 0);
520 rg_logevent(np
, "regexgline", "%s %d %d %s %s", regex
, expiry
, count
, class, reason
);
521 controlreply(np
, "Added regexgline: %s (class: %s, expires in: %s, hit %d user%s): %s", regex
, class, expirybuf
, count
, (count
!=1)?"s":"", reason
);
522 /* If we are using NO, can we safely assume the user is authed here and use ->authname? */
523 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
);
528 int rg_sanitycheck(char *mask
, int *count
) {
530 char hostname
[RG_MASKLEN
];
531 int erroroffset
, hostlen
, j
, masklen
= strlen(mask
);
536 if((masklen
< RG_MIN_MASK_LEN
) || (masklen
> RG_REGEXGLINE_MAX
))
539 if(!(regex
= pcre_compile(mask
, RG_PCREFLAGS
, &error
, &erroroffset
, NULL
))) {
540 Error("regexgline", ERR_WARNING
, "Error compiling expression %s at offset %d: %s", mask
, erroroffset
, error
);
543 hint
= pcre_study(regex
, 0, &error
);
545 Error("regexgline", ERR_WARNING
, "Error studying expression %s: %s", mask
, error
);
552 for(j
=0;j
<NICKHASHSIZE
;j
++) {
553 for(np
=nicktable
[j
];np
;np
=np
->next
) {
554 hostlen
= RGBuildHostname(hostname
, np
);
555 if(pcre_exec(regex
, hint
, hostname
, hostlen
, 0, 0, NULL
, 0) >= 0) {
565 if(*count
>= rg_max_casualties
)
571 int rg_delgline(void *source
, int cargc
, char **cargv
) {
572 nick
*np
= (nick
*)source
;
574 struct rg_struct
*rp
= rg_list
, *last
= NULL
;
580 rg_logevent(np
, "regexdelgline", "%s", cargv
[0]);
582 if(RGMasksEqual(rp
->mask
->content
, cargv
[0])) {
585 /* Cleanup the delays */
586 for(delay
=rg_delays
;delay
;delay
=delay
->next
)
587 if(delay
->reason
==rp
)
588 delay
->reason
= NULL
;
590 dbquery("DELETE FROM regexgline.glines WHERE id = %d", rp
->id
);
592 last
->next
= rp
->next
;
606 controlreply(np
, "Deleted (matched: %d).", count
);
607 /* If we are using NO, can we safely assume the user is authed here and use ->authname? */
608 controlwall(NO_OPER
, NL_GLINES
, "%s!%s@%s/%s removed regexgline: %s", np
->nick
, np
->ident
, np
->host
->name
->content
, np
->authname
, cargv
[0]);
610 controlreply(np
, "No glines matched: %s", cargv
[0]);
615 int rg_idlist(void *source
, int cargc
, char **cargv
) {
616 nick
*np
= (nick
*)source
;
620 } else if (strlen(cargv
[0]) != 8) {
621 controlreply(np
, "Invalid gline id!");
624 struct rg_struct
*rp
;
625 unsigned long id
= 0;
630 if(0xff == rc_hexlookup
[(int)cargv
[0][i
]]) {
631 controlreply(np
, "Invalid gline id!");
634 id
= (id
<< 4) | rc_hexlookup
[(int)cargv
[0][i
]];
639 controlreply(np
, GLINE_HEADER
);
640 for(rp
=rg_list
;rp
;rp
=rp
->next
) {
641 if(id
== rp
->glineid
) {
643 if(rp
->mask
->length
> longest
)
644 longest
= rp
->mask
->length
;
648 for(rp
=rg_list
;rp
;rp
=rp
->next
)
650 rg_displaygline(np
, rp
, longest
);
651 controlreply(np
, "Done.");
657 int rg_glist(void *source
, int cargc
, char **cargv
) {
658 nick
*np
= (nick
*)source
;
659 struct rg_struct
*rp
;
669 if(!(regex
= pcre_compile(cargv
[0], RG_PCREFLAGS
, &error
, &erroroffset
, NULL
))) {
670 controlreply(np
, "Error compiling expression %s at offset %d: %s", cargv
[0], erroroffset
, error
);
673 hint
= pcre_study(regex
, 0, &error
);
675 controlreply(np
, "Error studying expression %s: %s", cargv
[0], error
);
682 rg_logevent(np
, "regexglist", "%s", cargv
[0]);
683 controlreply(np
, GLINE_HEADER
);
684 for(rp
=rg_list
;rp
;rp
=rp
->next
) {
685 if(pcre_exec(regex
, hint
, rp
->mask
->content
, rp
->mask
->length
, 0, 0, NULL
, 0) >= 0) {
687 if(rp
->mask
->length
> longest
)
688 longest
= rp
->mask
->length
;
692 for(rp
=rg_list
;rp
;rp
=rp
->next
)
694 rg_displaygline(np
, rp
, longest
);
701 rg_logevent(np
, "regexglist", "%s", "");
702 controlreply(np
, GLINE_HEADER
);
703 for(rp
=rg_list
;rp
;rp
=rp
->next
)
704 if(rp
->mask
->length
> longest
)
705 longest
= rp
->mask
->length
;
707 for(rp
=rg_list
;rp
;rp
=rp
->next
)
708 rg_displaygline(np
, rp
, longest
);
711 controlreply(np
, "Done.");
715 char *displaytype(int type
) {
717 static char ctypebuf
[10];
742 snprintf(ctypebuf
, sizeof(ctype
), "%1d:%s", type
, ctype
);
746 char *getsep(int longest
) {
747 static int lastlongest
= -1;
748 static char lenbuf
[1024];
755 if(longest >= sizeof(lenbuf) - 20)
756 longest = sizeof(lenbuf) - 20;
759 if(lastlongest
== -1) {
762 for(i
=0;i
<sizeof(lenbuf
)-1;i
++)
764 lenbuf
[sizeof(lenbuf
)-1] = '\0';
768 if(lastlongest
!= longest
) {
769 lenbuf
[lastlongest
] = '-';
770 lenbuf
[longest
] = '\0';
771 lastlongest
= longest
;
777 void rg_displaygline(nick
*np
, struct rg_struct
*rp
, int longest
) { /* could be a macro? I'll assume the C compiler inlines it */
778 char *sep
= getsep(longest
);
779 /* 12345678 12345678901234567890 123456789012345 12345678 12345 12345678901234567890 1234567 1234567 123456
780 ID Expires Set by Class Type Last seen (ago) Hits(s) Hits Reason
784 time_t t
= time(NULL
);
786 if(rp
->lastseen
== 0) {
787 strlcpy(d
, "(never)", sizeof(d
));
789 strlcpy(d
, longtoduration(t
- rp
->lastseen
, 2), sizeof(d
));
792 controlreply(np
, "%s", rp
->mask
->content
);
793 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
);
794 controlreply(np
, "%s", sep
);
797 int rg_spew(void *source
, int cargc
, char **cargv
) {
798 nick
*np
= (nick
*)source
, *tnp
;
799 int counter
= 0, erroroffset
, hostlen
, j
;
803 char hostname
[RG_MASKLEN
];
810 if(!(regex
= pcre_compile(cargv
[0], RG_PCREFLAGS
, &error
, &erroroffset
, NULL
))) {
811 controlreply(np
, "Error compiling expression %s at offset %d: %s", cargv
[0], erroroffset
, error
);
814 hint
= pcre_study(regex
, 0, &error
);
816 controlreply(np
, "Error studying expression %s: %s", cargv
[0], error
);
822 rg_logevent(np
, "regexspew", "%s", cargv
[0]);
824 for(j
=0;j
<NICKHASHSIZE
;j
++) {
825 for(tnp
=nicktable
[j
];tnp
;tnp
=tnp
->next
) {
826 hostlen
= RGBuildHostname(hostname
, tnp
);
827 pcreret
= pcre_exec(regex
, hint
, hostname
, hostlen
, 0, 0, ovector
, sizeof(ovector
) / sizeof(int));
829 if(counter
== rg_max_spew
) {
830 controlreply(np
, "Reached maximum spew count (%d) - aborting display.", rg_max_spew
);
831 } else if (counter
< rg_max_spew
) {
832 /* 15 should be number of bolds */
833 char boldbuf
[RG_MASKLEN
+ 15], *tp
, *fp
, *realname
= NULL
;
835 for(tp
=hostname
,fp
=boldbuf
;*tp
;) {
836 if(tp
- hostname
== ovector
[0]) {
840 if(tp
- hostname
== ovector
[1]) {
859 controlreply(np
, "%s (%s) (%dc)", boldbuf
, realname
, tnp
->channels
->cursi
);
865 controlreply(np
, "Done - %d matches.", counter
);
874 void rg_startup(void) {
877 struct rg_struct
*rp
;
878 struct rg_glinelist gll
;
879 char hostname
[RG_MASKLEN
];
881 rg_initglinelist(&gll
);
883 for(j
=0;j
<NICKHASHSIZE
;j
++) {
884 for(np
=nicktable
[j
];np
;np
=np
->next
) {
885 if(IsOper(np
) || IsService(np
) || IsXOper(np
))
887 hostlen
= RGBuildHostname(hostname
, np
);
888 for(rp
=rg_list
;rp
;rp
=rp
->next
) {
889 if(pcre_exec(rp
->regex
, rp
->hint
, hostname
, hostlen
, 0, 0, NULL
, 0) >= 0) {
890 rg_dogline(&gll
, np
, rp
, hostname
);
897 rg_flushglines(&gll
);
900 void rg_freestruct(struct rg_struct
*rp
) {
901 freesstring(rp
->mask
);
902 freesstring(rp
->setby
);
903 freesstring(rp
->reason
);
904 pcre_free(rp
->regex
);
910 struct rg_struct
*rg_newstruct(time_t expires
) {
911 struct rg_struct
*rp
;
913 if (time(NULL
) >= expires
)
916 rp
= (struct rg_struct
*)malloc(sizeof(struct rg_struct
));
918 struct rg_struct
*tp
, *lp
;
920 memset(rp
, 0, sizeof(rg_struct
));
921 rp
->expires
= expires
;
923 for(lp
=NULL
,tp
=rg_list
;tp
;lp
=tp
,tp
=tp
->next
) {
924 if (expires
<= tp
->expires
) { /* <= possible, slight speed increase */
947 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
) {
948 struct rg_struct
*newrow
, *lp
, *cp
;
950 char glineiddata
[1024];
955 if(!protectedatoi(expires
, &qexpires
))
957 rexpires
= (time_t)qexpires
;
962 newrow
= rg_newstruct(rexpires
);
968 for(p
=classes
;*p
;p
++) {
969 if(!strcasecmp(class, *p
)) {
976 newrow
->class = "unknown";
978 if(!(newrow
->regex
= pcre_compile(mask
, RG_PCREFLAGS
, &error
, &erroroffset
, NULL
))) {
979 Error("regexgline", ERR_WARNING
, "Error compiling expression %s at offset %d: %s", mask
, erroroffset
, error
);
982 newrow
->hint
= pcre_study(newrow
->regex
, 0, &error
);
984 Error("regexgline", ERR_WARNING
, "Error studying expression %s: %s", mask
, error
);
985 pcre_free(newrow
->regex
);
991 newrow
->hitssaved
= hitssaved
;
992 newrow
->lastseen
= lastseen
;
994 newrow
->mask
= getsstring(mask
, RG_REGEXGLINE_MAX
);
996 Error("regexgline", ERR_WARNING
, "Error allocating memory for mask!");
1000 newrow
->setby
= getsstring(setby
, ACCOUNTLEN
);
1001 if(!newrow
->setby
) {
1002 Error("regexgline", ERR_WARNING
, "Error allocating memory for setby!");
1006 newrow
->reason
= getsstring(reason
, RG_REASON_MAX
);
1007 if(!newrow
->reason
) {
1008 Error("regexgline", ERR_WARNING
, "Error allocating memory for reason!");
1012 if(!protectedatoi(type
, &newrow
->type
))
1013 newrow
->type
= 0; /* just in case */
1015 snprintf(glineiddata
, sizeof(glineiddata
), "%s regexgline %s %s %s %d %d", mynumeric
->content
, mask
, setby
, reason
, (int)iexpires
, newrow
->type
);
1016 newrow
->glineid
= crc32(glineiddata
);
1023 freesstring(newrow
->mask
);
1025 freesstring(newrow
->setby
);
1027 freesstring(newrow
->reason
);
1028 pcre_free(newrow
->regex
);
1030 pcre_free(newrow
->hint
);
1033 for(lp
=NULL
,cp
=rg_list
;cp
;lp
=cp
,cp
=cp
->next
) {
1036 lp
->next
= cp
->next
;
1047 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?! */
1048 char hostname
[RG_MASKLEN
];
1052 rg_loggline(rp
, np
);
1054 if (rp
->type
== INSTANT_HOST_GLINE
) {
1055 usercount
= np
->host
->clonecount
;
1056 snprintf(hostname
, sizeof(hostname
), "*@%s", IPtostr(np
->p_ipaddr
));
1059 if ((rp
->type
== INSTANT_IDENT_GLINE
) || (usercount
> rg_max_per_gline
)) {
1062 for(usercount
=0,tnp
=np
->host
->nicks
;tnp
;tnp
=tnp
->nextbyhost
)
1063 if(!ircd_strcmp(np
->ident
, tnp
->ident
))
1066 snprintf(hostname
, sizeof(hostname
), "%s@%s", np
->ident
, IPtostr(np
->p_ipaddr
));
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 if (IsAccount(np
)) {
1095 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":"");
1097 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":"");
1099 } else if(rp
->type
== INSTANT_HOST_GLINE
) {
1100 if (IsAccount(np
)) {
1101 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":"");
1103 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":"");
1109 rg_shadowserver(np
, rp
, rp
->type
);
1110 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
);
1114 static int floodprotection
= 0;
1115 static int lastfloodspam
= 0;
1117 void rg_dogline(struct rg_glinelist
*gll
, nick
*np
, struct rg_struct
*rp
, char *matched
) {
1120 if(t
> floodprotection
) {
1121 floodprotection
= t
;
1122 } else if((floodprotection
- t
) / 8 > RG_NETWORK_WIDE_MAX_GLINES_PER_8_SEC
) {
1123 if(t
> lastfloodspam
+ 3600) {
1124 channel
*cp
= findchannel("#twilightzone");
1126 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
);
1127 controlwall(NO_OPER
, NL_MANAGEMENT
, "WARNING! REGEXGLINE DISABLED FOR AN HOUR DUE TO NETWORK WIDE LOOKING GLINE!");
1129 floodprotection
= t
+ RG_NETWORK_WIDE_MAX_GLINES_PER_8_SEC
* 3600 * 8;
1134 floodprotection
+=__rg_dogline(gll
, np
, rp
, matched
);
1137 void rg_logevent(nick
*np
, char *event
, char *details
, ...) {
1138 char eeevent
[RG_QUERY_BUF_SIZE
], eedetails
[RG_QUERY_BUF_SIZE
], eemask
[RG_QUERY_BUF_SIZE
], eeaccount
[RG_QUERY_BUF_SIZE
];
1139 char buf
[513], account
[ACCOUNTLEN
+ 1], mask
[RG_MASKLEN
];
1145 va_start(va
, details
);
1146 vsnprintf(buf
, sizeof(buf
), details
, va
);
1153 if (IsAccount(np
)) {
1154 strncpy(account
, np
->authname
, sizeof(account
) - 1);
1155 account
[sizeof(account
) - 1] = '\0';
1159 masklen
= RGBuildHostname(mask
, np
);
1165 dbescapestring(eeevent
, event
, strlen(event
));
1166 dbescapestring(eedetails
, buf
, strlen(buf
));
1167 dbescapestring(eeaccount
, account
, strlen(account
));
1168 dbescapestring(eemask
, mask
, masklen
);
1170 dbquery("INSERT INTO regexgline.clog (host, account, event, arg, ts) VALUES ('%s', '%s', '%s', '%s', NOW())", eemask
, eeaccount
, eeevent
, eedetails
);
1173 void rg_loggline(struct rg_struct
*rg
, nick
*np
) {
1174 char eenick
[RG_QUERY_BUF_SIZE
], eeuser
[RG_QUERY_BUF_SIZE
], eehost
[RG_QUERY_BUF_SIZE
], eereal
[RG_QUERY_BUF_SIZE
];
1178 rg
->lastseen
= time(NULL
);
1181 /* @paul: disabled */
1184 dbescapestring(eenick
, np
->nick
, strlen(np
->nick
));
1185 dbescapestring(eeuser
, np
->ident
, strlen(np
->ident
));
1186 dbescapestring(eehost
, np
->host
->name
->content
, strlen(np
->host
->name
->content
));
1187 dbescapestring(eereal
, np
->realname
->name
->content
, strlen(np
->realname
->name
->content
));
1189 dbquery("INSERT INTO regexgline.glog (glineid, nickname, username, hostname, realname, ts) VALUES (%d, '%s', '%s', '%s', '%s', NOW())", rg
->id
, eenick
, eeuser
, eehost
, eereal
);
1192 static unsigned int getrgmarker(void) {
1193 static unsigned int marker
= 0;
1197 struct rg_struct
*l
;
1199 /* If we wrapped to zero, zap the marker on all hosts */
1200 for(l
=rg_list
;l
;l
=l
->next
)
1208 void rg_flush_schedule(void *arg
) {
1209 struct rg_struct
*l
;
1211 for(l
=rg_list
;l
;l
=l
->next
) {
1215 dbquery("UPDATE regexgline.glines SET lastseen = %jd, hits = %lu WHERE id = %d", (intmax_t)l
->lastseen
, l
->hitssaved
, l
->id
);