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"
18 #define INSTANT_IDENT_GLINE 1
19 #define INSTANT_HOST_GLINE 2
20 #define INSTANT_KILL 3
21 #define DELAYED_IDENT_GLINE 4
22 #define DELAYED_HOST_GLINE 5
23 #define DELAYED_KILL 6
25 MODULE_VERSION("1.41");
27 typedef struct rg_glinenode
{
29 struct rg_struct
*reason
;
31 struct rg_glinenode
*next
;
34 typedef struct rg_glinelist
{
35 struct rg_glinenode
*start
;
36 struct rg_glinenode
*end
;
39 typedef struct rg_delay
{
42 struct rg_struct
*reason
;
44 struct rg_delay
*next
;
49 void rg_setdelay(nick
*np
, struct rg_struct
*reason
, short punish
);
50 void rg_deletedelay(rg_delay
*delay
);
51 void rg_dodelay(void *arg
);
53 void rg_dogline(struct rg_glinelist
*gll
, nick
*np
, struct rg_struct
*rp
, char *matched
);
55 static DBModuleIdentifier dbid
;
56 static unsigned long highestid
= 0;
57 static int attached
= 0, started
= 0;
59 /* shadowserver only reports classes[0] */
60 static const char *classes
[] = { "drone", "proxy", "spam", "fakeauth", "other", (char *)0 };
63 sstring
*max_casualties
, *max_spew
, *expiry_time
, *max_per_gline
;
65 max_casualties
= getcopyconfigitem("regexgline", "maxcasualties", RGStringise(RG_MAX_CASUALTIES_DEFAULT
), 8);
66 if(!protectedatoi(max_casualties
->content
, &rg_max_casualties
))
67 rg_max_casualties
= RG_MAX_CASUALTIES_DEFAULT
;
69 freesstring(max_casualties
);
71 max_spew
= getcopyconfigitem("regexgline", "maxspew", RGStringise(RG_MAX_SPEW_DEFAULT
), 8);
72 if(!protectedatoi(max_spew
->content
, &rg_max_spew
))
73 rg_max_spew
= RG_MAX_SPEW_DEFAULT
;
75 freesstring(max_spew
);
77 expiry_time
= getcopyconfigitem("regexgline", "expirytime", RGStringise(RG_EXPIRY_TIME_DEFAULT
), 8);
78 if(!protectedatoi(expiry_time
->content
, &rg_expiry_time
))
79 rg_expiry_time
= RG_EXPIRY_TIME_DEFAULT
;
81 freesstring(expiry_time
);
83 max_per_gline
= getcopyconfigitem("regexgline", "maxpergline", RGStringise(RG_MAX_PER_GLINE_DEFAULT
), 8);
84 if(!protectedatoi(max_per_gline
->content
, &rg_max_per_gline
))
85 rg_max_per_gline
= RG_MAX_PER_GLINE_DEFAULT
;
87 freesstring(max_per_gline
);
96 Error("regexgline", ERR_STOP
, "Could not connect to database.");
101 struct rg_struct
*gp
= rg_list
, *oldgp
;
102 rg_delay
*delay
, *delaynext
;
105 deregisterhook(HOOK_NICK_NEWNICK
, &rg_nick
);
106 deregisterhook(HOOK_NICK_RENAME
, &rg_nick
);
107 deregisterhook(HOOK_NICK_LOSTNICK
, &rg_lostnick
);
108 deregistercontrolcmd("regexspew", rg_spew
);
109 deregistercontrolcmd("regexglist", rg_glist
);
110 deregistercontrolcmd("regexdelgline", rg_delgline
);
111 deregistercontrolcmd("regexgline", rg_gline
);
112 deregistercontrolcmd("regexidlookup", rg_idlist
);
116 for(delay
=rg_delays
;delay
;delay
=delaynext
) {
117 delaynext
=delay
->next
;
118 deleteschedule(delay
->sch
, rg_dodelay
, delay
);
124 deleteschedule(rg_schedule
, &rg_checkexpiry
, NULL
);
128 for(gp
=rg_list
;gp
;) {
131 rg_freestruct(oldgp
);
135 dbdetach("regexgline");
140 void rg_checkexpiry(void *arg
) {
141 struct rg_struct
*rp
= rg_list
, *lp
= NULL
;
142 time_t current
= time(NULL
);
145 if (current
>= rp
->expires
) {
162 void rg_setdelay(nick
*np
, rg_struct
*reason
, short punish
) {
164 delay
= (rg_delay
*)malloc(sizeof(rg_delay
));
168 killuser(NULL
, np
, "%s (ID: %08lx)", reason
->reason
->content
, reason
->glineid
);
173 delay
->reason
= reason
;
174 delay
->punish
= punish
;
175 delay
->next
= rg_delays
;
178 delay
->sch
= scheduleoneshot(time(NULL
) + (RG_MINIMUM_DELAY_TIME
+ (rand() % RG_MAXIMUM_RAND_TIME
)), rg_dodelay
, delay
);
181 static void rg_shadowserver(nick
*np
, struct rg_struct
*reason
, int type
) {
184 if(reason
->class != classes
[0]) /* drone */
187 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
);
189 triggerhook(HOOK_SHADOW_SERVER
, (void *)buf
);
192 void rg_deletedelay(rg_delay
*delay
) {
193 rg_delay
*temp
, *prev
;
195 for (temp
=rg_delays
;temp
;temp
=temp
->next
) {
198 rg_delays
= temp
->next
;
200 prev
->next
= temp
->next
;
210 void rg_dodelay(void *arg
) {
211 rg_delay
*delay
= (rg_delay
*)arg
;
212 char hostname
[RG_MASKLEN
];
213 int hostlen
, usercount
= 0;
215 /* User or regex gline no longer exists */
216 if((!delay
->np
) || (!delay
->reason
)) {
217 rg_deletedelay(delay
);
221 hostlen
= RGBuildHostname(hostname
, delay
->np
);
223 /* User has wisely changed nicknames */
224 if(pcre_exec(delay
->reason
->regex
, delay
->reason
->hint
, hostname
, hostlen
, 0, 0, NULL
, 0) < 0) {
225 rg_deletedelay(delay
);
229 if (delay
->reason
->type
== DELAYED_HOST_GLINE
) {
230 usercount
= delay
->np
->host
->clonecount
;
231 snprintf(hostname
, sizeof(hostname
), "*@%s", IPtostr(delay
->np
->p_ipaddr
));
234 if((delay
->reason
->type
== DELAYED_IDENT_GLINE
) || (usercount
> rg_max_per_gline
)) {
237 for(usercount
=0,tnp
=delay
->np
->host
->nicks
;tnp
;tnp
=tnp
->nextbyhost
)
238 if(!ircd_strcmp(delay
->np
->ident
, tnp
->ident
))
241 snprintf(hostname
, sizeof(hostname
), "%s@%s", delay
->np
->ident
, IPtostr(delay
->np
->p_ipaddr
));
244 if ((delay
->reason
->type
== DELAYED_KILL
) || (usercount
> rg_max_per_gline
)) {
245 if (IsAccount(delay
->np
)) {
246 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);
248 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);
251 rg_shadowserver(delay
->np
, delay
->reason
, DELAYED_KILL
);
252 killuser(NULL
, delay
->np
, "%s (ID: %08lx)", delay
->reason
->reason
->content
, delay
->reason
->glineid
);
256 if (delay
->reason
->type
== DELAYED_IDENT_GLINE
) {
257 if (IsAccount(delay
->np
)) {
258 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":"");
260 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":"");
262 } else if (delay
->reason
->type
== DELAYED_HOST_GLINE
) {
263 if (IsAccount(delay
->np
)) {
264 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":"");
266 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":"");
272 rg_shadowserver(delay
->np
, delay
->reason
, delay
->reason
->type
);
273 irc_send("%s GL * +%s %d %d :AUTO: %s (ID: %08lx)\r\n", mynumeric
->content
, hostname
, rg_expiry_time
, time(NULL
), delay
->reason
->reason
->content
, delay
->reason
->glineid
);
274 rg_deletedelay(delay
);
277 void rg_initglinelist(struct rg_glinelist
*gll
) {
282 void rg_flushglines(struct rg_glinelist
*gll
) {
283 struct rg_glinenode
*nn
, *pn
;
284 for(nn
=gll
->start
;nn
;nn
=pn
) {
286 if(nn
->punish
== INSTANT_KILL
) {
287 if ( IsAccount(nn
->np
) ) {
288 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);
290 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);
293 rg_shadowserver(nn
->np
, nn
->reason
, nn
->punish
);
294 killuser(NULL
, nn
->np
, "%s (ID: %08lx)", nn
->reason
->reason
->content
, nn
->reason
->glineid
);
295 } else if ((nn
->punish
== DELAYED_IDENT_GLINE
) || (nn
->punish
== DELAYED_HOST_GLINE
) || (nn
->punish
== DELAYED_KILL
)) {
296 rg_setdelay(nn
->np
, nn
->reason
, nn
->punish
);
301 rg_initglinelist(gll
);
304 static void dbloaddata(DBConn
*dbconn
, void *arg
) {
305 DBResult
*dbres
= dbgetresult(dbconn
);
307 if(!dbquerysuccessful(dbres
)) {
308 Error("chanserv", ERR_ERROR
, "Error loading DB");
312 if (dbnumfields(dbres
) != 7) {
313 Error("regexgline", ERR_ERROR
, "DB format error");
317 while(dbfetchrow(dbres
)) {
319 char *gline
, *setby
, *reason
, *expires
, *type
, *class;
321 id
= strtoul(dbgetvalue(dbres
, 0), NULL
, 10);
325 gline
= dbgetvalue(dbres
, 1);
326 setby
= dbgetvalue(dbres
, 2);
327 reason
= dbgetvalue(dbres
, 3);
328 expires
= dbgetvalue(dbres
, 4);
329 type
= dbgetvalue(dbres
, 5);
330 class = dbgetvalue(dbres
, 6);
332 if (!rg_newsstruct(id
, gline
, setby
, reason
, expires
, type
, 0, class))
333 dbquery("DELETE FROM regexgline.glines WHERE id = %u", id
);
339 static void dbloadfini(DBConn
*dbconn
, void *arg
) {
343 char helpbuf
[8192 * 2], allclasses
[8192];
345 sbinit(&b
, (char *)allclasses
, sizeof(allclasses
));
346 for(p
=classes
;*p
;p
++) {
347 sbaddstr(&b
, (char *)*p
);
352 snprintf(helpbuf
, sizeof(helpbuf
),
353 "Usage: regexgline <regex> <duration> <type> <class> <reason>\n"
354 "Adds a new regular expression pattern.\n"
355 "Duration is represented as 3d, 3M etc.\n"
356 "Class is one of the following: %s\n"
357 "Type is an integer which represents the following:\n"
358 "1 - Instant USER@IP GLINE (igu)\n"
359 "2 - Instant *@IP GLINE (igh)\n"
360 "3 - Instant KILL (ik)\n"
361 "4 - Delayed USER@IP GLINE (dgu)\n"
362 "5 - Delayed *@IP GLINE (dgh)\n"
363 "6 - Delayed KILL (dk)",
366 registercontrolhelpcmd("regexgline", NO_OPER
, 5, &rg_gline
, helpbuf
);
367 registercontrolhelpcmd("regexdelgline", NO_OPER
, 1, &rg_delgline
, "Usage: regexdelgline <pattern>\nDeletes a regular expression pattern.");
368 registercontrolhelpcmd("regexglist", NO_OPER
, 1, &rg_glist
, "Usage: regexglist <pattern>\nLists regular expression patterns.");
369 registercontrolhelpcmd("regexspew", NO_OPER
, 1, &rg_spew
, "Usage: regexspew <pattern>\nLists users currently on the network which match the given pattern.");
370 registercontrolhelpcmd("regexidlookup", NO_OPER
, 1, &rg_idlist
, "Usage: regexidlookup <id>\nFinds a regular expression pattern by it's ID number.");
372 registerhook(HOOK_NICK_NEWNICK
, &rg_nick
);
373 registerhook(HOOK_NICK_RENAME
, &rg_nick
);
374 registerhook(HOOK_NICK_LOSTNICK
, &rg_lostnick
);
377 rg_schedule
= schedulerecurring(time(NULL
) + 1, 0, 1, rg_checkexpiry
, NULL
);
380 void rg_dbload(void) {
381 dbattach("regexgline");
382 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)", ACCOUNTLEN
, RG_REASON_MAX
);
383 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
);
384 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
);
386 dbloadtable("regexgline.glines", NULL
, dbloaddata
, dbloadfini
);
389 void rg_nick(int hooknum
, void *arg
) {
390 nick
*np
= (nick
*)arg
;
391 struct rg_struct
*rp
;
392 char hostname
[RG_MASKLEN
];
394 struct rg_glinelist gll
;
396 rg_initglinelist(&gll
);
398 hostlen
= RGBuildHostname(hostname
, np
);
400 if(IsOper(np
) || IsService(np
) || IsXOper(np
))
403 for(rp
=rg_list
;rp
;rp
=rp
->next
) {
404 if(pcre_exec(rp
->regex
, rp
->hint
, hostname
, hostlen
, 0, 0, NULL
, 0) >= 0) {
405 rg_dogline(&gll
, np
, rp
, hostname
);
410 rg_flushglines(&gll
);
413 void rg_lostnick(int hooknum
, void *arg
) {
414 nick
*np
= (nick
*)arg
;
417 /* Cleanup the delays */
418 for(delay
=rg_delays
;delay
;delay
=delay
->next
)
423 int rg_gline(void *source
, int cargc
, char **cargv
) {
424 nick
*np
= (nick
*)source
, *tnp
;
426 const char *expirybuf
;
427 int expiry
, count
, j
, hostlen
;
428 struct rg_struct
*rp
;
429 struct rg_glinelist gll
;
432 char eemask
[RG_QUERY_BUF_SIZE
], eesetby
[RG_QUERY_BUF_SIZE
], eereason
[RG_QUERY_BUF_SIZE
], eeclass
[RG_QUERY_BUF_SIZE
];
433 char hostname
[RG_MASKLEN
], *class, *reason
, *regex
, type
;
439 if ((strlen(cargv
[2]) != 1) || ((type
!= '1') && (type
!= '2') && (type
!= '3') && (type
!= '4') && (type
!= '5') && (type
!= '6'))) {
440 controlreply(np
, "Invalid type specified!");
448 for(p
=classes
;*p
;p
++)
449 if(!strcasecmp(class, *p
))
453 controlreply(np
, "Bad class supplied.");
457 if (!(expiry
= durationtolong(cargv
[1]))) {
458 controlreply(np
, "Invalid duration specified!");
462 for(rp
=rg_list
;rp
;rp
=rp
->next
) {
463 if (RGMasksEqual(rp
->mask
->content
, regex
)) {
464 controlreply(np
, "That regexgline already exists!");
469 if (rg_sanitycheck(regex
, &count
)) {
470 controlreply(np
, "Error in expression.");
472 } else if (count
< 0) {
473 controlreply(np
, "That expression would hit too many users (%d)!", -count
);
477 realexpiry
= expiry
+ time(NULL
);
479 dbescapestring(eemask
, regex
, strlen(regex
));
480 dbescapestring(eesetby
, np
->nick
, strlen(np
->nick
));
481 dbescapestring(eeclass
, class, strlen(class));
482 dbescapestring(eereason
, reason
, strlen(reason
));
484 highestid
= highestid
+ 1;
485 dbquery("INSERT INTO regexgline.glines (id, gline, setby, reason, expires, type, class) VALUES (%d, '%s', '%s', '%s', %d, %c, '%s')", highestid
, eemask
, eesetby
, eereason
, realexpiry
, type
, eeclass
);
486 rp
= rg_newsstruct(highestid
, regex
, np
->nick
, reason
, "", cargv
[2], realexpiry
, class);
488 rg_initglinelist(&gll
);
490 for(j
=0;j
<NICKHASHSIZE
;j
++) {
491 for(tnp
=nicktable
[j
];tnp
;tnp
=tnp
->next
) {
492 if(IsOper(tnp
) || IsService(tnp
) || IsXOper(tnp
))
495 hostlen
= RGBuildHostname(hostname
, tnp
);
496 if(pcre_exec(rp
->regex
, rp
->hint
, hostname
, hostlen
, 0, 0, NULL
, 0) >= 0)
497 rg_dogline(&gll
, tnp
, rp
, hostname
);
501 rg_flushglines(&gll
);
503 expirybuf
= longtoduration(expiry
, 0);
505 rg_logevent(np
, "regexgline", "%s %d %d %s %s", regex
, expiry
, count
, class, reason
);
506 controlreply(np
, "Added regexgline: %s (class: %s, expires in: %s, hit %d user%s): %s", regex
, class, expirybuf
, count
, (count
!=1)?"s":"", reason
);
507 /* If we are using NO, can we safely assume the user is authed here and use ->authname? */
508 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
);
513 int rg_sanitycheck(char *mask
, int *count
) {
515 char hostname
[RG_MASKLEN
];
516 int erroroffset
, hostlen
, j
, masklen
= strlen(mask
);
521 if((masklen
< RG_MIN_MASK_LEN
) || (masklen
> RG_REGEXGLINE_MAX
))
524 if(!(regex
= pcre_compile(mask
, RG_PCREFLAGS
, &error
, &erroroffset
, NULL
))) {
525 Error("regexgline", ERR_WARNING
, "Error compiling expression %s at offset %d: %s", mask
, erroroffset
, error
);
528 hint
= pcre_study(regex
, 0, &error
);
530 Error("regexgline", ERR_WARNING
, "Error studying expression %s: %s", mask
, error
);
537 for(j
=0;j
<NICKHASHSIZE
;j
++) {
538 for(np
=nicktable
[j
];np
;np
=np
->next
) {
539 hostlen
= RGBuildHostname(hostname
, np
);
540 if(pcre_exec(regex
, hint
, hostname
, hostlen
, 0, 0, NULL
, 0) >= 0) {
550 if(*count
>= rg_max_casualties
)
556 int rg_delgline(void *source
, int cargc
, char **cargv
) {
557 nick
*np
= (nick
*)source
;
559 struct rg_struct
*rp
= rg_list
, *last
= NULL
;
565 rg_logevent(np
, "regexdelgline", "%s", cargv
[0]);
567 if(RGMasksEqual(rp
->mask
->content
, cargv
[0])) {
570 /* Cleanup the delays */
571 for(delay
=rg_delays
;delay
;delay
=delay
->next
)
572 if(delay
->reason
==rp
)
573 delay
->reason
= NULL
;
575 dbquery("DELETE FROM regexgline.glines WHERE id = %d", rp
->id
);
577 last
->next
= rp
->next
;
591 controlreply(np
, "Deleted (matched: %d).", count
);
592 /* If we are using NO, can we safely assume the user is authed here and use ->authname? */
593 controlwall(NO_OPER
, NL_GLINES
, "%s!%s@%s/%s removed regexgline: %s", np
->nick
, np
->ident
, np
->host
->name
->content
, np
->authname
, cargv
[0]);
595 controlreply(np
, "No glines matched: %s", cargv
[0]);
600 int rg_idlist(void *source
, int cargc
, char **cargv
) {
601 nick
*np
= (nick
*)source
;
605 } else if (strlen(cargv
[0]) != 8) {
606 controlreply(np
, "Invalid gline id!");
609 struct rg_struct
*rp
;
610 unsigned long id
= 0;
614 if(0xff == rc_hexlookup
[(int)cargv
[0][i
]]) {
615 controlreply(np
, "Invalid gline id!");
618 id
= (id
<< 4) | rc_hexlookup
[(int)cargv
[0][i
]];
622 controlreply(np
, "Mask Expires Set by Type Reason");
623 for(rp
=rg_list
;rp
;rp
=rp
->next
)
624 if(id
== rp
->glineid
)
625 rg_displaygline(np
, rp
);
626 controlreply(np
, "Done.");
632 int rg_glist(void *source
, int cargc
, char **cargv
) {
633 nick
*np
= (nick
*)source
;
634 struct rg_struct
*rp
;
642 if(!(regex
= pcre_compile(cargv
[0], RG_PCREFLAGS
, &error
, &erroroffset
, NULL
))) {
643 controlreply(np
, "Error compiling expression %s at offset %d: %s", cargv
[0], erroroffset
, error
);
646 hint
= pcre_study(regex
, 0, &error
);
648 controlreply(np
, "Error studying expression %s: %s", cargv
[0], error
);
654 rg_logevent(np
, "regexglist", "%s", cargv
[0]);
655 controlreply(np
, "Mask Expires Set by Class Type Hits Reason");
656 for(rp
=rg_list
;rp
;rp
=rp
->next
)
657 if(pcre_exec(regex
, hint
, rp
->mask
->content
, rp
->mask
->length
, 0, 0, NULL
, 0) >= 0)
658 rg_displaygline(np
, rp
);
665 rg_logevent(np
, "regexglist", "");
666 controlreply(np
, "Mask Expires Set by Class Type Hits Reason");
667 for(rp
=rg_list
;rp
;rp
=rp
->next
)
668 rg_displaygline(np
, rp
);
671 controlreply(np
, "Done.");
675 char *displaytype(int type
) {
677 static char ctypebuf
[10];
702 snprintf(ctypebuf
, sizeof(ctype
), "%1d:%s", type
, ctype
);
706 void rg_displaygline(nick
*np
, struct rg_struct
*rp
) { /* could be a macro? I'll assume the C compiler inlines it */
707 controlreply(np
, " %-25s %-20s %-15s %-8s %-5s %-5lu %s", rp
->mask
->content
, longtoduration(rp
->expires
- time(NULL
), 0), rp
->setby
->content
, rp
->class, displaytype(rp
->type
), rp
->hits
, rp
->reason
->content
);
710 int rg_spew(void *source
, int cargc
, char **cargv
) {
711 nick
*np
= (nick
*)source
, *tnp
;
712 int counter
= 0, erroroffset
, hostlen
, j
;
716 char hostname
[RG_MASKLEN
];
723 if(!(regex
= pcre_compile(cargv
[0], RG_PCREFLAGS
, &error
, &erroroffset
, NULL
))) {
724 controlreply(np
, "Error compiling expression %s at offset %d: %s", cargv
[0], erroroffset
, error
);
727 hint
= pcre_study(regex
, 0, &error
);
729 controlreply(np
, "Error studying expression %s: %s", cargv
[0], error
);
735 rg_logevent(np
, "regexspew", "%s", cargv
[0]);
737 for(j
=0;j
<NICKHASHSIZE
;j
++) {
738 for(tnp
=nicktable
[j
];tnp
;tnp
=tnp
->next
) {
739 hostlen
= RGBuildHostname(hostname
, tnp
);
740 pcreret
= pcre_exec(regex
, hint
, hostname
, hostlen
, 0, 0, ovector
, sizeof(ovector
) / sizeof(int));
742 if(counter
== rg_max_spew
) {
743 controlreply(np
, "Reached maximum spew count (%d) - aborting display.", rg_max_spew
);
744 } else if (counter
< rg_max_spew
) {
745 /* 15 should be number of bolds */
746 char boldbuf
[RG_MASKLEN
+ 15], *tp
, *fp
, *realname
= NULL
;
748 for(tp
=hostname
,fp
=boldbuf
;*tp
;) {
749 if(tp
- hostname
== ovector
[0]) {
753 if(tp
- hostname
== ovector
[1]) {
772 controlreply(np
, "%s (%s) (%dc)", boldbuf
, realname
, tnp
->channels
->cursi
);
778 controlreply(np
, "Done - %d matches.", counter
);
787 void rg_startup(void) {
790 struct rg_struct
*rp
;
791 struct rg_glinelist gll
;
792 char hostname
[RG_MASKLEN
];
794 rg_initglinelist(&gll
);
796 for(j
=0;j
<NICKHASHSIZE
;j
++) {
797 for(np
=nicktable
[j
];np
;np
=np
->next
) {
798 if(IsOper(np
) || IsService(np
) || IsXOper(np
))
800 hostlen
= RGBuildHostname(hostname
, np
);
801 for(rp
=rg_list
;rp
;rp
=rp
->next
) {
802 if(pcre_exec(rp
->regex
, rp
->hint
, hostname
, hostlen
, 0, 0, NULL
, 0) >= 0) {
803 rg_dogline(&gll
, np
, rp
, hostname
);
810 rg_flushglines(&gll
);
813 void rg_freestruct(struct rg_struct
*rp
) {
814 freesstring(rp
->mask
);
815 freesstring(rp
->setby
);
816 freesstring(rp
->reason
);
817 pcre_free(rp
->regex
);
823 struct rg_struct
*rg_newstruct(time_t expires
) {
824 struct rg_struct
*rp
;
826 if (time(NULL
) >= expires
)
829 rp
= (struct rg_struct
*)malloc(sizeof(struct rg_struct
));
831 struct rg_struct
*tp
, *lp
;
833 memset(rp
, 0, sizeof(rp
));
834 rp
->expires
= expires
;
836 for(lp
=NULL
,tp
=rg_list
;tp
;lp
=tp
,tp
=tp
->next
) {
837 if (expires
<= tp
->expires
) { /* <= possible, slight speed increase */
860 struct rg_struct
*rg_newsstruct(unsigned long id
, char *mask
, char *setby
, char *reason
, char *expires
, char *type
, time_t iexpires
, char *class) {
861 struct rg_struct
*newrow
, *lp
, *cp
;
863 char glineiddata
[1024];
868 if(!protectedatoi(expires
, &qexpires
))
870 rexpires
= (time_t)qexpires
;
875 newrow
= rg_newstruct(rexpires
);
881 for(p
=classes
;*p
;p
++) {
882 if(!strcasecmp(class, *p
)) {
889 newrow
->class = "unknown";
891 if(!(newrow
->regex
= pcre_compile(mask
, RG_PCREFLAGS
, &error
, &erroroffset
, NULL
))) {
892 Error("regexgline", ERR_WARNING
, "Error compiling expression %s at offset %d: %s", mask
, erroroffset
, error
);
895 newrow
->hint
= pcre_study(newrow
->regex
, 0, &error
);
897 Error("regexgline", ERR_WARNING
, "Error studying expression %s: %s", mask
, error
);
898 pcre_free(newrow
->regex
);
905 newrow
->mask
= getsstring(mask
, RG_REGEXGLINE_MAX
);
907 Error("regexgline", ERR_WARNING
, "Error allocating memory for mask!");
911 newrow
->setby
= getsstring(setby
, ACCOUNTLEN
);
913 Error("regexgline", ERR_WARNING
, "Error allocating memory for setby!");
917 newrow
->reason
= getsstring(reason
, RG_REASON_MAX
);
918 if(!newrow
->reason
) {
919 Error("regexgline", ERR_WARNING
, "Error allocating memory for reason!");
923 if(!protectedatoi(type
, &newrow
->type
))
924 newrow
->type
= 0; /* just in case */
926 snprintf(glineiddata
, sizeof(glineiddata
), "%s regexgline %s %s %s %d %d", mynumeric
->content
, mask
, setby
, reason
, (int)iexpires
, newrow
->type
);
927 newrow
->glineid
= crc32(glineiddata
);
934 freesstring(newrow
->mask
);
936 freesstring(newrow
->setby
);
938 freesstring(newrow
->reason
);
939 pcre_free(newrow
->regex
);
941 pcre_free(newrow
->hint
);
944 for(lp
=NULL
,cp
=rg_list
;cp
;lp
=cp
,cp
=cp
->next
) {
958 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?! */
959 char hostname
[RG_MASKLEN
];
965 if (rp
->type
== INSTANT_HOST_GLINE
) {
966 usercount
= np
->host
->clonecount
;
967 snprintf(hostname
, sizeof(hostname
), "*@%s", IPtostr(np
->p_ipaddr
));
970 if ((rp
->type
== INSTANT_IDENT_GLINE
) || (usercount
> rg_max_per_gline
)) {
973 for(usercount
=0,tnp
=np
->host
->nicks
;tnp
;tnp
=tnp
->nextbyhost
)
974 if(!ircd_strcmp(np
->ident
, tnp
->ident
))
977 snprintf(hostname
, sizeof(hostname
), "%s@%s", np
->ident
, IPtostr(np
->p_ipaddr
));
980 validdelay
= (rp
->type
== INSTANT_KILL
) || (rp
->type
== DELAYED_IDENT_GLINE
) || (rp
->type
== DELAYED_HOST_GLINE
) || (rp
->type
== DELAYED_KILL
);
981 if (validdelay
|| (usercount
> rg_max_per_gline
)) {
982 struct rg_glinenode
*nn
= (struct rg_glinenode
*)malloc(sizeof(struct rg_glinenode
));
996 nn
->punish
= INSTANT_KILL
;
998 nn
->punish
= rp
->type
;
1004 if (rp
->type
== INSTANT_IDENT_GLINE
) {
1005 if (IsAccount(np
)) {
1006 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":"");
1008 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":"");
1010 } else if(rp
->type
== INSTANT_HOST_GLINE
) {
1011 if (IsAccount(np
)) {
1012 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":"");
1014 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":"");
1020 rg_shadowserver(np
, rp
, rp
->type
);
1021 irc_send("%s GL * +%s %d %d :AUTO: %s (ID: %08lx)\r\n", mynumeric
->content
, hostname
, rg_expiry_time
, time(NULL
), rp
->reason
->content
, rp
->glineid
);
1025 static int floodprotection
= 0;
1026 static int lastfloodspam
= 0;
1028 void rg_dogline(struct rg_glinelist
*gll
, nick
*np
, struct rg_struct
*rp
, char *matched
) {
1031 if(t
> floodprotection
) {
1032 floodprotection
= t
;
1033 } else if((floodprotection
- t
) / 8 > RG_NETWORK_WIDE_MAX_GLINES_PER_8_SEC
) {
1034 if(t
> lastfloodspam
+ 3600) {
1035 channel
*cp
= findchannel("#twilightzone");
1037 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
);
1038 controlwall(NO_OPER
, NL_MANAGEMENT
, "WARNING! REGEXGLINE DISABLED FOR AN HOUR DUE TO NETWORK WIDE LOOKING GLINE!");
1040 floodprotection
= t
+ RG_NETWORK_WIDE_MAX_GLINES_PER_8_SEC
* 3600 * 8;
1045 floodprotection
+=__rg_dogline(gll
, np
, rp
, matched
);
1048 void rg_logevent(nick
*np
, char *event
, char *details
, ...) {
1049 char eeevent
[RG_QUERY_BUF_SIZE
], eedetails
[RG_QUERY_BUF_SIZE
], eemask
[RG_QUERY_BUF_SIZE
], eeaccount
[RG_QUERY_BUF_SIZE
];
1050 char buf
[513], account
[ACCOUNTLEN
+ 1], mask
[RG_MASKLEN
];
1055 va_start(va
, details
);
1056 vsnprintf(buf
, sizeof(buf
), details
, va
);
1060 if (IsAccount(np
)) {
1061 strncpy(account
, np
->authname
, sizeof(account
) - 1);
1062 account
[sizeof(account
) - 1] = '\0';
1066 masklen
= RGBuildHostname(mask
, np
);
1072 dbescapestring(eeevent
, event
, strlen(event
));
1073 dbescapestring(eedetails
, buf
, strlen(buf
));
1074 dbescapestring(eeaccount
, account
, strlen(account
));
1075 dbescapestring(eemask
, mask
, masklen
);
1077 dbquery("INSERT INTO regexgline.clog (host, account, event, arg, ts) VALUES ('%s', '%s', '%s', '%s', NOW())", eemask
, eeaccount
, eeevent
, eedetails
);
1080 void rg_loggline(struct rg_struct
*rg
, nick
*np
) {
1081 char eenick
[RG_QUERY_BUF_SIZE
], eeuser
[RG_QUERY_BUF_SIZE
], eehost
[RG_QUERY_BUF_SIZE
], eereal
[RG_QUERY_BUF_SIZE
];
1085 /* @paul: disabled */
1088 dbescapestring(eenick
, np
->nick
, strlen(np
->nick
));
1089 dbescapestring(eeuser
, np
->ident
, strlen(np
->ident
));
1090 dbescapestring(eehost
, np
->host
->name
->content
, strlen(np
->host
->name
->content
));
1091 dbescapestring(eereal
, np
->realname
->name
->content
, strlen(np
->realname
->name
->content
));
1093 dbquery("INSERT INTO regexgline.glog (glineid, nickname, username, hostname, realname, ts) VALUES (%d, '%s', '%s', '%s', '%s', NOW())", rg
->id
, eenick
, eeuser
, eehost
, eereal
);