]>
Commit | Line | Data |
---|---|---|
1e0c4014 CP |
1 | /* regexgline.c */ |
2 | ||
3 | /* TODO: | |
4 | ||
5 | FUTURE: natural (sort of) language parsing | |
6 | ^^ CIDR | |
7 | ||
8 | PPA: if multiple users match the same user@host or *@host it'll send multiple glines?! | |
9 | */ | |
10 | ||
11 | #include "regexgline.h" | |
87698d77 | 12 | #include "../lib/version.h" |
eba15ac4 | 13 | #include "../dbapi/dbapi.h" |
86178656 | 14 | #include "../lib/stringbuf.h" |
36c1eaf1 CP |
15 | #include "../core/hooks.h" |
16 | #include "../server/server.h" | |
c4d6944b | 17 | #include "../lib/strlfunc.h" |
a473a1be | 18 | #include "../glines/glines.h" |
8bab42e7 | 19 | #include <stdint.h> |
87698d77 | 20 | |
444d2433 CP |
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 | |
27 | ||
e2fa40b2 CP |
28 | #define RESERVED_NICK_GLINE_DURATION 3600 /* 1h */ |
29 | ||
30 | MODULE_VERSION("1.44"); | |
1e0c4014 CP |
31 | |
32 | typedef struct rg_glinenode { | |
33 | nick *np; | |
34 | struct rg_struct *reason; | |
20c491ca | 35 | short punish; |
1e0c4014 CP |
36 | struct rg_glinenode *next; |
37 | } rg_glinenode; | |
38 | ||
39 | typedef struct rg_glinelist { | |
40 | struct rg_glinenode *start; | |
41 | struct rg_glinenode *end; | |
42 | } rg_glinelist; | |
43 | ||
5f3eb6fa CP |
44 | typedef struct rg_delay { |
45 | schedule *sch; | |
46 | nick *np; | |
47 | struct rg_struct *reason; | |
48 | short punish; | |
49 | struct rg_delay *next; | |
50 | } rg_delay; | |
51 | ||
c4d6944b CP |
52 | #define GLINE_HEADER " ID Expires Set by Class Type Last seen (ago) Hits(p) Hits Reason" |
53 | ||
5f3eb6fa CP |
54 | rg_delay *rg_delays; |
55 | ||
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); | |
59 | ||
1e0c4014 | 60 | void rg_dogline(struct rg_glinelist *gll, nick *np, struct rg_struct *rp, char *matched); |
c4d6944b | 61 | void rg_flush_schedule(void *arg); |
1e0c4014 | 62 | |
fad97385 | 63 | static char *gvhost(nick *np); |
24072adc CP |
64 | typedef void (scannick_fn)(struct rg_struct *, nick *, char *, void *); |
65 | static void rg_scannick(nick *np, scannick_fn *fn, void *arg); | |
fcec2be0 | 66 | static void rg_gline_match(struct rg_struct *rp, nick *np, char *hostname, void *arg); |
fad97385 | 67 | |
eba15ac4 CP |
68 | static DBModuleIdentifier dbid; |
69 | static unsigned long highestid = 0; | |
70 | static int attached = 0, started = 0; | |
71 | ||
c4d6944b CP |
72 | static unsigned int getrgmarker(void); |
73 | ||
e2fa40b2 | 74 | #define RESERVED_NICK_CLASS "reservednick" |
d73fc7e5 | 75 | /* shadowserver only reports classes[0] */ |
e2fa40b2 | 76 | static const char *classes[] = { "drone", "proxy", "spam", "other", RESERVED_NICK_CLASS, (char *)0 }; |
86178656 | 77 | |
24072adc CP |
78 | void rg_initglinelist(struct rg_glinelist *gll); |
79 | void rg_flushglines(struct rg_glinelist *gll); | |
80 | ||
1e0c4014 CP |
81 | void _init(void) { |
82 | sstring *max_casualties, *max_spew, *expiry_time, *max_per_gline; | |
83 | ||
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; | |
87 | ||
88 | freesstring(max_casualties); | |
89 | ||
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; | |
93 | ||
94 | freesstring(max_spew); | |
95 | ||
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; | |
99 | ||
100 | freesstring(expiry_time); | |
101 | ||
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; | |
105 | ||
106 | freesstring(max_per_gline); | |
5f3eb6fa CP |
107 | |
108 | rg_delays = NULL; | |
1e0c4014 | 109 | |
eba15ac4 CP |
110 | if(dbconnected()) { |
111 | attached = 1; | |
112 | dbid = dbgetid(); | |
1e0c4014 | 113 | rg_dbload(); |
eba15ac4 CP |
114 | } else { |
115 | Error("regexgline", ERR_STOP, "Could not connect to database."); | |
1e0c4014 CP |
116 | } |
117 | } | |
24072adc CP |
118 | |
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]; | |
122 | ||
123 | (*count)++; | |
124 | } | |
125 | ||
126 | static void rg_gline_reply_match(struct rg_struct *rp, nick *np, char *hostname, void *arg) { | |
127 | void **varg = (void **)arg; | |
128 | ||
129 | rg_count_match(rp, np, hostname, arg); | |
130 | rg_gline_match(rp, np, hostname, varg[1]); | |
131 | } | |
132 | ||
133 | int rg_rescan(void *source, int cargc, char **cargv) { | |
134 | int gline = 0; | |
135 | int j; | |
136 | nick *np = (nick *)source, *tnp; | |
137 | void *arg[2]; | |
138 | int count = 0; | |
139 | struct rg_glinelist gll; | |
140 | scannick_fn *fn; | |
141 | ||
142 | if(cargc > 0) | |
143 | gline = !strcmp(cargv[0], "-g"); | |
144 | ||
145 | arg[0] = &count; | |
146 | ||
147 | if(gline == 0) { | |
148 | fn = rg_count_match; | |
149 | } else { | |
150 | controlreply(np, "G-line mode activated."); | |
151 | ||
152 | rg_initglinelist(&gll); | |
153 | arg[1] = &gll; | |
154 | ||
155 | fn = rg_gline_reply_match; | |
156 | } | |
157 | ||
158 | controlreply(np, "Beginning scan, this may take a while..."); | |
159 | ||
160 | for(j=0;j<NICKHASHSIZE;j++) | |
161 | for(tnp=nicktable[j];tnp;tnp=tnp->next) | |
162 | rg_scannick(tnp, fn, arg); | |
163 | ||
164 | controlreply(np, "Scan completed, %d hits.", count); | |
165 | ||
166 | if(gline) | |
167 | rg_flushglines(&gll); | |
168 | ||
169 | return CMD_OK; | |
170 | } | |
171 | ||
1e0c4014 | 172 | void _fini(void) { |
8ef5efbf | 173 | struct rg_struct *gp, *oldgp; |
5f3eb6fa | 174 | rg_delay *delay, *delaynext; |
1e0c4014 | 175 | |
eba15ac4 CP |
176 | if(started) { |
177 | deregisterhook(HOOK_NICK_NEWNICK, &rg_nick); | |
2fdef282 | 178 | deregisterhook(HOOK_NICK_RENAME, &rg_rename); |
eba15ac4 CP |
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); | |
24072adc | 185 | deregistercontrolcmd("regexrescan", rg_rescan); |
eba15ac4 CP |
186 | } |
187 | ||
5f3eb6fa CP |
188 | if(rg_delays) { |
189 | for(delay=rg_delays;delay;delay=delaynext) { | |
190 | delaynext=delay->next; | |
191 | deleteschedule(delay->sch, rg_dodelay, delay); | |
192 | free(delay); | |
193 | } | |
194 | } | |
1e0c4014 CP |
195 | |
196 | if(rg_schedule) { | |
197 | deleteschedule(rg_schedule, &rg_checkexpiry, NULL); | |
198 | rg_schedule = NULL; | |
199 | } | |
c4d6944b CP |
200 | |
201 | deleteallschedules(rg_flush_schedule); | |
202 | rg_flush_schedule(NULL); | |
203 | ||
1e0c4014 CP |
204 | for(gp=rg_list;gp;) { |
205 | oldgp = gp; | |
206 | gp = gp->next; | |
207 | rg_freestruct(oldgp); | |
208 | } | |
eba15ac4 CP |
209 | |
210 | if(attached) { | |
211 | dbdetach("regexgline"); | |
212 | dbfreeid(dbid); | |
213 | } | |
1e0c4014 CP |
214 | } |
215 | ||
1b02955c CP |
216 | static int ignorable_nick(nick *np) { |
217 | if(IsOper(np) || IsService(np) || IsXOper(np) || SIsService(&serverlist[homeserver(np->numeric)])) | |
218 | return 1; | |
219 | return 0; | |
220 | } | |
221 | ||
1e0c4014 CP |
222 | void rg_checkexpiry(void *arg) { |
223 | struct rg_struct *rp = rg_list, *lp = NULL; | |
224 | time_t current = time(NULL); | |
225 | ||
226 | while(rp) { | |
227 | if (current >= rp->expires) { | |
f2a9a545 | 228 | dbquery("DELETE FROM regexglines WHERE id = %d", rp->id); |
1e0c4014 CP |
229 | if (lp) { |
230 | lp->next = rp->next; | |
231 | rg_freestruct(rp); | |
232 | rp = lp->next; | |
233 | } else { | |
234 | rg_list = rp->next; | |
235 | rg_freestruct(rp); | |
236 | rp = rg_list; | |
237 | } | |
238 | } else { | |
239 | lp = rp; | |
240 | rp = rp->next; | |
241 | } | |
242 | } | |
243 | } | |
244 | ||
5f3eb6fa CP |
245 | void rg_setdelay(nick *np, rg_struct *reason, short punish) { |
246 | rg_delay *delay; | |
247 | delay = (rg_delay *)malloc(sizeof(rg_delay)); | |
248 | ||
249 | /* Just incase */ | |
250 | if(!delay) { | |
251 | killuser(NULL, np, "%s (ID: %08lx)", reason->reason->content, reason->glineid); | |
252 | return; | |
253 | } | |
254 | ||
255 | delay->np = np; | |
256 | delay->reason = reason; | |
257 | delay->punish = punish; | |
258 | delay->next = rg_delays; | |
259 | rg_delays = delay; | |
260 | ||
261 | delay->sch = scheduleoneshot(time(NULL) + (RG_MINIMUM_DELAY_TIME + (rand() % RG_MAXIMUM_RAND_TIME)), rg_dodelay, delay); | |
262 | } | |
263 | ||
36c1eaf1 CP |
264 | static void rg_shadowserver(nick *np, struct rg_struct *reason, int type) { |
265 | char buf[1024]; | |
d73fc7e5 CP |
266 | |
267 | if(reason->class != classes[0]) /* drone */ | |
268 | return; | |
269 | ||
36c1eaf1 CP |
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); |
271 | ||
272 | triggerhook(HOOK_SHADOW_SERVER, (void *)buf); | |
273 | } | |
274 | ||
5f3eb6fa CP |
275 | void rg_deletedelay(rg_delay *delay) { |
276 | rg_delay *temp, *prev; | |
277 | prev = NULL; | |
278 | for (temp=rg_delays;temp;temp=temp->next) { | |
279 | if (temp==delay) { | |
280 | if (temp==rg_delays) | |
281 | rg_delays = temp->next; | |
282 | else | |
283 | prev->next = temp->next; | |
284 | ||
285 | free(temp); | |
286 | return; | |
287 | } | |
288 | ||
289 | prev = temp; | |
290 | } | |
291 | } | |
292 | ||
293 | void rg_dodelay(void *arg) { | |
294 | rg_delay *delay = (rg_delay *)arg; | |
295 | char hostname[RG_MASKLEN]; | |
296 | int hostlen, usercount = 0; | |
a473a1be | 297 | char reason[200]; |
2d7e1ef2 | 298 | int glineflags = 0; |
5f3eb6fa CP |
299 | |
300 | /* User or regex gline no longer exists */ | |
301 | if((!delay->np) || (!delay->reason)) { | |
302 | rg_deletedelay(delay); | |
303 | return; | |
304 | } | |
305 | ||
306 | hostlen = RGBuildHostname(hostname, delay->np); | |
307 | ||
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); | |
311 | return; | |
312 | } | |
313 | ||
444d2433 | 314 | if (delay->reason->type == DELAYED_HOST_GLINE) { |
5f3eb6fa | 315 | usercount = delay->np->host->clonecount; |
5f3eb6fa CP |
316 | } |
317 | ||
444d2433 | 318 | if((delay->reason->type == DELAYED_IDENT_GLINE) || (usercount > rg_max_per_gline)) { |
5f3eb6fa CP |
319 | nick *tnp; |
320 | ||
321 | for(usercount=0,tnp=delay->np->host->nicks;tnp;tnp=tnp->nextbyhost) | |
322 | if(!ircd_strcmp(delay->np->ident, tnp->ident)) | |
323 | usercount++; | |
324 | ||
2d7e1ef2 | 325 | glineflags = GLINE_ALWAYS_USER; |
5f3eb6fa CP |
326 | } |
327 | ||
444d2433 | 328 | if ((delay->reason->type == DELAYED_KILL) || (usercount > rg_max_per_gline)) { |
fad97385 | 329 | controlwall(NO_OPER, NL_HITS, "%s matched delayed kill regex %08lx (class: %s)", gvhost(delay->np), delay->reason->glineid, delay->reason->class); |
36c1eaf1 CP |
330 | |
331 | rg_shadowserver(delay->np, delay->reason, DELAYED_KILL); | |
5f3eb6fa CP |
332 | killuser(NULL, delay->np, "%s (ID: %08lx)", delay->reason->reason->content, delay->reason->glineid); |
333 | return; | |
334 | } | |
335 | ||
444d2433 | 336 | if (delay->reason->type == DELAYED_IDENT_GLINE) { |
fad97385 | 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":""); |
444d2433 | 338 | } else if (delay->reason->type == DELAYED_HOST_GLINE) { |
fad97385 | 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":""); |
444d2433 CP |
340 | } else { |
341 | return; | |
5f3eb6fa CP |
342 | } |
343 | ||
36c1eaf1 | 344 | rg_shadowserver(delay->np, delay->reason, delay->reason->type); |
a473a1be | 345 | snprintf(reason, sizeof(reason), "AUTO: %s (ID: %08lx)", delay->reason->reason->content, delay->reason->glineid); |
3c5c26a8 | 346 | glinebynick(delay->np, rg_expiry_time, reason, glineflags, "regexgline"); |
5f3eb6fa CP |
347 | rg_deletedelay(delay); |
348 | } | |
349 | ||
1e0c4014 CP |
350 | void rg_initglinelist(struct rg_glinelist *gll) { |
351 | gll->start = NULL; | |
352 | gll->end = NULL; | |
353 | } | |
354 | ||
355 | void rg_flushglines(struct rg_glinelist *gll) { | |
356 | struct rg_glinenode *nn, *pn; | |
357 | for(nn=gll->start;nn;nn=pn) { | |
358 | pn = nn->next; | |
36c1eaf1 | 359 | if(nn->punish == INSTANT_KILL) { |
fad97385 | 360 | controlwall(NO_OPER, NL_HITS, "%s matched kill regex %08lx (class: %s)", gvhost(nn->np), nn->reason->glineid, nn->reason->class); |
36c1eaf1 CP |
361 | |
362 | rg_shadowserver(nn->np, nn->reason, nn->punish); | |
1e0c4014 | 363 | killuser(NULL, nn->np, "%s (ID: %08lx)", nn->reason->reason->content, nn->reason->glineid); |
36c1eaf1 | 364 | } else if ((nn->punish == DELAYED_IDENT_GLINE) || (nn->punish == DELAYED_HOST_GLINE) || (nn->punish == DELAYED_KILL)) { |
5f3eb6fa CP |
365 | rg_setdelay(nn->np, nn->reason, nn->punish); |
366 | } | |
1e0c4014 CP |
367 | free(nn); |
368 | } | |
369 | ||
370 | rg_initglinelist(gll); | |
371 | } | |
372 | ||
eba15ac4 CP |
373 | static void dbloaddata(DBConn *dbconn, void *arg) { |
374 | DBResult *dbres = dbgetresult(dbconn); | |
375 | ||
376 | if(!dbquerysuccessful(dbres)) { | |
377 | Error("chanserv", ERR_ERROR, "Error loading DB"); | |
378 | return; | |
1e0c4014 | 379 | } |
1e0c4014 | 380 | |
c4d6944b | 381 | if (dbnumfields(dbres) != 9) { |
eba15ac4 CP |
382 | Error("regexgline", ERR_ERROR, "DB format error"); |
383 | return; | |
384 | } | |
385 | ||
386 | while(dbfetchrow(dbres)) { | |
c4d6944b CP |
387 | unsigned long id, hitssaved; |
388 | time_t lastseen; | |
86178656 | 389 | char *gline, *setby, *reason, *expires, *type, *class; |
eba15ac4 CP |
390 | |
391 | id = strtoul(dbgetvalue(dbres, 0), NULL, 10); | |
392 | if(id > highestid) | |
393 | highestid = id; | |
394 | ||
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); | |
86178656 | 400 | class = dbgetvalue(dbres, 6); |
eba15ac4 | 401 | |
c4d6944b CP |
402 | lastseen = strtoul(dbgetvalue(dbres, 7), NULL, 10); |
403 | hitssaved = strtoul(dbgetvalue(dbres, 8), NULL, 10); | |
404 | ||
405 | if (!rg_newsstruct(id, gline, setby, reason, expires, type, 0, class, lastseen, hitssaved)) | |
16739dbe | 406 | dbquery("DELETE FROM regexgline.glines WHERE id = %lu", id); |
eba15ac4 CP |
407 | } |
408 | ||
409 | dbclear(dbres); | |
410 | } | |
411 | ||
412 | static void dbloadfini(DBConn *dbconn, void *arg) { | |
413 | started = 1; | |
86178656 CP |
414 | StringBuf b; |
415 | const char **p; | |
416 | char helpbuf[8192 * 2], allclasses[8192]; | |
417 | ||
418 | sbinit(&b, (char *)allclasses, sizeof(allclasses)); | |
419 | for(p=classes;*p;p++) { | |
420 | sbaddstr(&b, (char *)*p); | |
421 | sbaddchar(&b, ' '); | |
422 | } | |
423 | sbterminate(&b); | |
232eadea | 424 | |
86178656 CP |
425 | snprintf(helpbuf, sizeof(helpbuf), |
426 | "Usage: regexgline <regex> <duration> <type> <class> <reason>\n" | |
232eadea CP |
427 | "Adds a new regular expression pattern.\n" |
428 | "Duration is represented as 3d, 3M etc.\n" | |
86178656 | 429 | "Class is one of the following: %s\n" |
232eadea | 430 | "Type is an integer which represents the following:\n" |
01d1f71c CP |
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" | |
e2fa40b2 CP |
436 | "6 - Delayed KILL (dk)\n" |
437 | "Note that some classes may have additional side effects (e.g. 'reservednick' also sets nick style glines).", | |
86178656 CP |
438 | allclasses); |
439 | ||
440 | registercontrolhelpcmd("regexgline", NO_OPER, 5, &rg_gline, helpbuf); | |
eba15ac4 CP |
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."); | |
24072adc | 445 | registercontrolhelpcmd("regexrescan", NO_OPER, 1, &rg_rescan, "Usage: regexrescan ?-g?\nRescans the net for missed clients, optionally glining matches (used for debugging)."); |
232eadea | 446 | |
eba15ac4 | 447 | registerhook(HOOK_NICK_NEWNICK, &rg_nick); |
2fdef282 | 448 | registerhook(HOOK_NICK_RENAME, &rg_rename); |
eba15ac4 CP |
449 | registerhook(HOOK_NICK_LOSTNICK, &rg_lostnick); |
450 | rg_startup(); | |
232eadea | 451 | |
eba15ac4 | 452 | rg_schedule = schedulerecurring(time(NULL) + 1, 0, 1, rg_checkexpiry, NULL); |
c4d6944b | 453 | schedulerecurring(time(NULL) + 60, 0, 60, rg_flush_schedule, NULL); |
1e0c4014 CP |
454 | } |
455 | ||
eba15ac4 CP |
456 | void rg_dbload(void) { |
457 | dbattach("regexgline"); | |
c4d6944b | 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); |
63c6ac26 CP |
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); | |
eba15ac4 | 461 | |
63c6ac26 | 462 | dbloadtable("regexgline.glines", NULL, dbloaddata, dbloadfini); |
1e0c4014 CP |
463 | } |
464 | ||
24072adc | 465 | static void rg_scannick(nick *np, scannick_fn *fn, void *arg) { |
1e0c4014 CP |
466 | struct rg_struct *rp; |
467 | char hostname[RG_MASKLEN]; | |
468 | int hostlen; | |
c1c1cc5b | 469 | |
1b02955c | 470 | if(ignorable_nick(np)) |
c1c1cc5b CP |
471 | return; |
472 | ||
fcec2be0 CP |
473 | hostlen = RGBuildHostname(hostname, np); |
474 | ||
1e0c4014 CP |
475 | for(rp=rg_list;rp;rp=rp->next) { |
476 | if(pcre_exec(rp->regex, rp->hint, hostname, hostlen, 0, 0, NULL, 0) >= 0) { | |
fcec2be0 | 477 | fn(rp, np, hostname, arg); |
1e0c4014 CP |
478 | break; |
479 | } | |
480 | } | |
fcec2be0 CP |
481 | } |
482 | ||
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; | |
485 | ||
486 | rg_dogline(gll, np, rp, hostname); | |
487 | } | |
488 | ||
2fdef282 CP |
489 | void rg_rename(int hooknum, void *arg) { |
490 | void **harg = (void **)arg; | |
491 | rg_nick(hooknum, harg[0]); | |
492 | } | |
493 | ||
fcec2be0 CP |
494 | void rg_nick(int hooknum, void *arg) { |
495 | nick *np = (nick *)arg; | |
496 | struct rg_glinelist gll; | |
497 | ||
498 | rg_initglinelist(&gll); | |
499 | ||
500 | rg_scannick(np, rg_gline_match, &gll); | |
1e0c4014 CP |
501 | |
502 | rg_flushglines(&gll); | |
503 | } | |
504 | ||
5f3eb6fa CP |
505 | void rg_lostnick(int hooknum, void *arg) { |
506 | nick *np = (nick *)arg; | |
507 | rg_delay *delay; | |
508 | ||
509 | /* Cleanup the delays */ | |
510 | for(delay=rg_delays;delay;delay=delay->next) | |
511 | if(delay->np==np) | |
512 | delay->np = NULL; | |
513 | } | |
514 | ||
1e0c4014 CP |
515 | int rg_gline(void *source, int cargc, char **cargv) { |
516 | nick *np = (nick *)source, *tnp; | |
517 | time_t realexpiry; | |
518 | const char *expirybuf; | |
519 | int expiry, count, j, hostlen; | |
520 | struct rg_struct *rp; | |
521 | struct rg_glinelist gll; | |
86178656 | 522 | const char **p; |
1e0c4014 | 523 | |
86178656 CP |
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; | |
1e0c4014 | 526 | |
86178656 | 527 | if(cargc < 5) |
5f3eb6fa | 528 | return CMD_USAGE; |
86178656 CP |
529 | |
530 | type = cargv[2][0]; | |
531 | if ((strlen(cargv[2]) != 1) || ((type != '1') && (type != '2') && (type != '3') && (type != '4') && (type != '5') && (type != '6'))) { | |
1e0c4014 | 532 | controlreply(np, "Invalid type specified!"); |
5f3eb6fa | 533 | return CMD_USAGE; |
1e0c4014 | 534 | } |
5f3eb6fa | 535 | |
86178656 CP |
536 | regex = cargv[0]; |
537 | class = cargv[3]; | |
538 | reason = cargv[4]; | |
539 | ||
540 | for(p=classes;*p;p++) | |
541 | if(!strcasecmp(class, *p)) | |
542 | break; | |
543 | ||
544 | if(!*p) { | |
545 | controlreply(np, "Bad class supplied."); | |
546 | return CMD_USAGE; | |
547 | } | |
548 | ||
1e0c4014 CP |
549 | if (!(expiry = durationtolong(cargv[1]))) { |
550 | controlreply(np, "Invalid duration specified!"); | |
5f3eb6fa | 551 | return CMD_USAGE; |
1e0c4014 CP |
552 | } |
553 | ||
554 | for(rp=rg_list;rp;rp=rp->next) { | |
86178656 CP |
555 | if (RGMasksEqual(rp->mask->content, regex)) { |
556 | controlreply(np, "That regexgline already exists!"); | |
1e0c4014 CP |
557 | return CMD_ERROR; |
558 | } | |
559 | } | |
560 | ||
86178656 | 561 | if (rg_sanitycheck(regex, &count)) { |
1e0c4014 CP |
562 | controlreply(np, "Error in expression."); |
563 | return CMD_ERROR; | |
564 | } else if (count < 0) { | |
565 | controlreply(np, "That expression would hit too many users (%d)!", -count); | |
566 | return CMD_ERROR; | |
567 | } | |
568 | ||
569 | realexpiry = expiry + time(NULL); | |
570 | ||
86178656 | 571 | dbescapestring(eemask, regex, strlen(regex)); |
eba15ac4 | 572 | dbescapestring(eesetby, np->nick, strlen(np->nick)); |
86178656 CP |
573 | dbescapestring(eeclass, class, strlen(class)); |
574 | dbescapestring(eereason, reason, strlen(reason)); | |
1e0c4014 | 575 | |
eba15ac4 | 576 | highestid = highestid + 1; |
16739dbe | 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); |
c4d6944b | 578 | rp = rg_newsstruct(highestid, regex, np->nick, reason, "", cargv[2], realexpiry, class, 0, 0); |
1e0c4014 CP |
579 | |
580 | rg_initglinelist(&gll); | |
581 | ||
582 | for(j=0;j<NICKHASHSIZE;j++) { | |
583 | for(tnp=nicktable[j];tnp;tnp=tnp->next) { | |
1b02955c | 584 | if(ignorable_nick(tnp)) |
c1c1cc5b CP |
585 | continue; |
586 | ||
d28e0741 | 587 | hostlen = RGBuildHostname(hostname, tnp); |
1e0c4014 CP |
588 | if(pcre_exec(rp->regex, rp->hint, hostname, hostlen, 0, 0, NULL, 0) >= 0) |
589 | rg_dogline(&gll, tnp, rp, hostname); | |
590 | } | |
591 | } | |
592 | ||
593 | rg_flushglines(&gll); | |
594 | ||
595 | expirybuf = longtoduration(expiry, 0); | |
596 | ||
86178656 CP |
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); | |
5f3eb6fa | 599 | /* If we are using NO, can we safely assume the user is authed here and use ->authname? */ |
86178656 | 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); |
1e0c4014 CP |
601 | |
602 | return CMD_OK; | |
603 | } | |
604 | ||
605 | int rg_sanitycheck(char *mask, int *count) { | |
606 | const char *error; | |
607 | char hostname[RG_MASKLEN]; | |
608 | int erroroffset, hostlen, j, masklen = strlen(mask); | |
609 | pcre *regex; | |
610 | pcre_extra *hint; | |
611 | nick *np; | |
612 | ||
613 | if((masklen < RG_MIN_MASK_LEN) || (masklen > RG_REGEXGLINE_MAX)) | |
614 | return 1; | |
615 | ||
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); | |
618 | return 2; | |
619 | } else { | |
620 | hint = pcre_study(regex, 0, &error); | |
621 | if(error) { | |
622 | Error("regexgline", ERR_WARNING, "Error studying expression %s: %s", mask, error); | |
623 | pcre_free(regex); | |
624 | return 3; | |
625 | } | |
626 | } | |
627 | ||
628 | *count = 0; | |
629 | for(j=0;j<NICKHASHSIZE;j++) { | |
630 | for(np=nicktable[j];np;np=np->next) { | |
d28e0741 | 631 | hostlen = RGBuildHostname(hostname, np); |
1e0c4014 CP |
632 | if(pcre_exec(regex, hint, hostname, hostlen, 0, 0, NULL, 0) >= 0) { |
633 | (*count)++; | |
634 | } | |
635 | } | |
636 | } | |
637 | ||
638 | pcre_free(regex); | |
639 | if(hint) | |
640 | pcre_free(hint); | |
641 | ||
642 | if(*count >= rg_max_casualties) | |
643 | *count = -(*count); | |
644 | ||
645 | return 0; | |
646 | } | |
647 | ||
648 | int rg_delgline(void *source, int cargc, char **cargv) { | |
649 | nick *np = (nick *)source; | |
5f3eb6fa | 650 | rg_delay *delay; |
1e0c4014 CP |
651 | struct rg_struct *rp = rg_list, *last = NULL; |
652 | int count = 0; | |
653 | ||
5f3eb6fa CP |
654 | if(cargc < 1) |
655 | return CMD_USAGE; | |
1e0c4014 CP |
656 | |
657 | rg_logevent(np, "regexdelgline", "%s", cargv[0]); | |
658 | while(rp) { | |
659 | if(RGMasksEqual(rp->mask->content, cargv[0])) { | |
660 | count++; | |
5f3eb6fa CP |
661 | |
662 | /* Cleanup the delays */ | |
663 | for(delay=rg_delays;delay;delay=delay->next) | |
664 | if(delay->reason==rp) | |
665 | delay->reason = NULL; | |
666 | ||
eba15ac4 | 667 | dbquery("DELETE FROM regexgline.glines WHERE id = %d", rp->id); |
1e0c4014 CP |
668 | if(last) { |
669 | last->next = rp->next; | |
670 | rg_freestruct(rp); | |
671 | rp = last->next; | |
672 | } else { | |
673 | rg_list = rp->next; | |
674 | rg_freestruct(rp); | |
675 | rp = rg_list; | |
676 | } | |
677 | } else { | |
678 | last = rp; | |
679 | rp = rp->next; | |
680 | } | |
681 | } | |
682 | if (count > 0) { | |
683 | controlreply(np, "Deleted (matched: %d).", count); | |
5f3eb6fa CP |
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]); | |
1e0c4014 CP |
686 | } else { |
687 | controlreply(np, "No glines matched: %s", cargv[0]); | |
688 | } | |
689 | return CMD_OK; | |
690 | } | |
691 | ||
692 | int rg_idlist(void *source, int cargc, char **cargv) { | |
693 | nick *np = (nick *)source; | |
694 | ||
695 | if(cargc < 1) { | |
5f3eb6fa | 696 | return CMD_USAGE; |
1e0c4014 CP |
697 | } else if (strlen(cargv[0]) != 8) { |
698 | controlreply(np, "Invalid gline id!"); | |
699 | return CMD_ERROR; | |
700 | } else { | |
701 | struct rg_struct *rp; | |
702 | unsigned long id = 0; | |
c4d6944b CP |
703 | int i, longest = 0; |
704 | unsigned int m; | |
1e0c4014 CP |
705 | |
706 | for(i=0;i<8;i++) { | |
707 | if(0xff == rc_hexlookup[(int)cargv[0][i]]) { | |
708 | controlreply(np, "Invalid gline id!"); | |
709 | return CMD_ERROR; | |
710 | } else { | |
711 | id = (id << 4) | rc_hexlookup[(int)cargv[0][i]]; | |
712 | } | |
713 | } | |
714 | ||
c4d6944b CP |
715 | m = getrgmarker(); |
716 | controlreply(np, GLINE_HEADER); | |
717 | for(rp=rg_list;rp;rp=rp->next) { | |
718 | if(id == rp->glineid) { | |
719 | rp->marker = m; | |
720 | if(rp->mask->length > longest) | |
721 | longest = rp->mask->length; | |
722 | } | |
723 | } | |
724 | ||
1e0c4014 | 725 | for(rp=rg_list;rp;rp=rp->next) |
c4d6944b CP |
726 | if(rp->marker == m) |
727 | rg_displaygline(np, rp, longest); | |
1e0c4014 CP |
728 | controlreply(np, "Done."); |
729 | ||
730 | return CMD_OK; | |
731 | } | |
732 | } | |
733 | ||
734 | int rg_glist(void *source, int cargc, char **cargv) { | |
735 | nick *np = (nick *)source; | |
736 | struct rg_struct *rp; | |
c4d6944b | 737 | int longest = 0; |
1e0c4014 CP |
738 | |
739 | if(cargc) { | |
740 | int erroroffset; | |
741 | pcre *regex; | |
742 | pcre_extra *hint; | |
743 | const char *error; | |
c4d6944b CP |
744 | unsigned int m; |
745 | ||
1e0c4014 CP |
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); | |
748 | return CMD_ERROR; | |
749 | } else { | |
750 | hint = pcre_study(regex, 0, &error); | |
751 | if(error) { | |
752 | controlreply(np, "Error studying expression %s: %s", cargv[0], error); | |
753 | pcre_free(regex); | |
754 | return CMD_ERROR; | |
755 | } | |
756 | } | |
c4d6944b CP |
757 | |
758 | m = getrgmarker(); | |
1e0c4014 | 759 | rg_logevent(np, "regexglist", "%s", cargv[0]); |
c4d6944b CP |
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) { | |
763 | rp->marker = m; | |
764 | if(rp->mask->length > longest) | |
765 | longest = rp->mask->length; | |
766 | } | |
767 | } | |
768 | ||
1e0c4014 | 769 | for(rp=rg_list;rp;rp=rp->next) |
c4d6944b CP |
770 | if(rp->marker == m) |
771 | rg_displaygline(np, rp, longest); | |
772 | ||
1e0c4014 CP |
773 | pcre_free(regex); |
774 | if(hint) | |
775 | pcre_free(hint); | |
776 | ||
777 | } else { | |
579d4f01 | 778 | rg_logevent(np, "regexglist", "%s", ""); |
c4d6944b | 779 | controlreply(np, GLINE_HEADER); |
1e0c4014 | 780 | for(rp=rg_list;rp;rp=rp->next) |
c4d6944b CP |
781 | if(rp->mask->length > longest) |
782 | longest = rp->mask->length; | |
783 | ||
784 | for(rp=rg_list;rp;rp=rp->next) | |
785 | rg_displaygline(np, rp, longest); | |
1e0c4014 CP |
786 | } |
787 | ||
788 | controlreply(np, "Done."); | |
789 | return CMD_OK; | |
790 | } | |
791 | ||
01d1f71c CP |
792 | char *displaytype(int type) { |
793 | char *ctype; | |
794 | static char ctypebuf[10]; | |
795 | ||
796 | switch(type) { | |
797 | case 1: | |
798 | ctype = "igu"; | |
799 | break; | |
800 | case 2: | |
801 | ctype = "igh"; | |
802 | break; | |
803 | case 3: | |
804 | ctype = "ik"; | |
805 | break; | |
806 | case 4: | |
807 | ctype = "dgu"; | |
808 | break; | |
809 | case 5: | |
810 | ctype = "dgh"; | |
811 | break; | |
812 | case 6: | |
813 | ctype = "dk"; | |
814 | break; | |
815 | default: | |
816 | ctype = "??"; | |
817 | } | |
818 | ||
819 | snprintf(ctypebuf, sizeof(ctype), "%1d:%s", type, ctype); | |
820 | return ctypebuf; | |
821 | } | |
822 | ||
c4d6944b CP |
823 | char *getsep(int longest) { |
824 | static int lastlongest = -1; | |
825 | static char lenbuf[1024]; | |
826 | ||
827 | longest = 125; | |
828 | /* | |
829 | if(longest < 100) | |
830 | longest = 100; | |
831 | ||
832 | if(longest >= sizeof(lenbuf) - 20) | |
833 | longest = sizeof(lenbuf) - 20; | |
834 | */ | |
835 | longest+=4; | |
836 | if(lastlongest == -1) { | |
837 | int i; | |
838 | ||
839 | for(i=0;i<sizeof(lenbuf)-1;i++) | |
840 | lenbuf[i] = '-'; | |
841 | lenbuf[sizeof(lenbuf)-1] = '\0'; | |
842 | lastlongest = 0; | |
843 | } | |
844 | ||
845 | if(lastlongest != longest) { | |
846 | lenbuf[lastlongest] = '-'; | |
847 | lenbuf[longest] = '\0'; | |
848 | lastlongest = longest; | |
849 | } | |
850 | ||
851 | return lenbuf; | |
852 | } | |
853 | ||
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 | |
858 | */ | |
859 | ||
860 | char d[512]; | |
861 | time_t t = time(NULL); | |
862 | ||
863 | if(rp->lastseen == 0) { | |
864 | strlcpy(d, "(never)", sizeof(d)); | |
865 | } else { | |
866 | strlcpy(d, longtoduration(t - rp->lastseen, 2), sizeof(d)); | |
867 | } | |
868 | ||
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); | |
1e0c4014 CP |
872 | } |
873 | ||
874 | int rg_spew(void *source, int cargc, char **cargv) { | |
875 | nick *np = (nick *)source, *tnp; | |
876 | int counter = 0, erroroffset, hostlen, j; | |
877 | pcre *regex; | |
878 | pcre_extra *hint; | |
879 | const char *error; | |
880 | char hostname[RG_MASKLEN]; | |
d28e0741 CP |
881 | int ovector[30]; |
882 | int pcreret; | |
883 | ||
5f3eb6fa CP |
884 | if(cargc < 1) |
885 | return CMD_USAGE; | |
1e0c4014 CP |
886 | |
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); | |
889 | return CMD_ERROR; | |
890 | } else { | |
891 | hint = pcre_study(regex, 0, &error); | |
892 | if(error) { | |
893 | controlreply(np, "Error studying expression %s: %s", cargv[0], error); | |
894 | pcre_free(regex); | |
895 | return CMD_ERROR; | |
896 | } | |
897 | } | |
898 | ||
899 | rg_logevent(np, "regexspew", "%s", cargv[0]); | |
900 | ||
901 | for(j=0;j<NICKHASHSIZE;j++) { | |
902 | for(tnp=nicktable[j];tnp;tnp=tnp->next) { | |
903 | hostlen = RGBuildHostname(hostname, tnp); | |
d28e0741 CP |
904 | pcreret = pcre_exec(regex, hint, hostname, hostlen, 0, 0, ovector, sizeof(ovector) / sizeof(int)); |
905 | if(pcreret >= 0) { | |
1e0c4014 CP |
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) { | |
d28e0741 CP |
909 | /* 15 should be number of bolds */ |
910 | char boldbuf[RG_MASKLEN + 15], *tp, *fp, *realname = NULL; | |
911 | int boldon = 0; | |
912 | for(tp=hostname,fp=boldbuf;*tp;) { | |
913 | if(tp - hostname == ovector[0]) { | |
914 | *fp++ = '\002'; | |
915 | boldon = 1; | |
916 | } | |
917 | if(tp - hostname == ovector[1]) { | |
918 | *fp++ = '\002'; | |
919 | boldon = 0; | |
920 | } | |
921 | if(*tp == '\r') { | |
922 | if(boldon) | |
923 | *fp++ = '\002'; | |
924 | *fp++ = '\0'; | |
925 | realname = fp; | |
926 | if(boldon) | |
927 | *fp++ = '\002'; | |
928 | tp++; | |
929 | } else { | |
930 | *fp++ = *tp++; | |
931 | } | |
932 | } | |
933 | if(boldon) | |
934 | *fp++ = '\002'; | |
935 | *fp++ = '\0'; | |
767ee88c | 936 | controlreply(np, "%s (%s) (%dc)", boldbuf, realname, tnp->channels->cursi); |
1e0c4014 CP |
937 | } |
938 | counter++; | |
939 | } | |
940 | } | |
941 | } | |
942 | controlreply(np, "Done - %d matches.", counter); | |
943 | ||
944 | pcre_free(regex); | |
945 | if(hint) | |
946 | pcre_free(hint); | |
947 | ||
948 | return CMD_OK; | |
949 | } | |
950 | ||
1e0c4014 | 951 | void rg_startup(void) { |
fcec2be0 | 952 | int j; |
1e0c4014 | 953 | nick *np; |
1e0c4014 | 954 | struct rg_glinelist gll; |
1e0c4014 CP |
955 | |
956 | rg_initglinelist(&gll); | |
957 | ||
fcec2be0 CP |
958 | for(j=0;j<NICKHASHSIZE;j++) |
959 | for(np=nicktable[j];np;np=np->next) | |
960 | rg_scannick(np, rg_gline_match, &gll); | |
1e0c4014 CP |
961 | |
962 | rg_flushglines(&gll); | |
963 | } | |
964 | ||
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); | |
970 | if(rp->hint) | |
971 | pcre_free(rp->hint); | |
972 | free(rp); | |
973 | } | |
974 | ||
975 | struct rg_struct *rg_newstruct(time_t expires) { | |
976 | struct rg_struct *rp; | |
977 | ||
978 | if (time(NULL) >= expires) | |
979 | return NULL; | |
980 | ||
981 | rp = (struct rg_struct*)malloc(sizeof(struct rg_struct)); | |
982 | if(rp) { | |
983 | struct rg_struct *tp, *lp; | |
86178656 | 984 | |
c4d6944b | 985 | memset(rp, 0, sizeof(rg_struct)); |
1e0c4014 | 986 | rp->expires = expires; |
1e0c4014 CP |
987 | |
988 | for(lp=NULL,tp=rg_list;tp;lp=tp,tp=tp->next) { | |
989 | if (expires <= tp->expires) { /* <= possible, slight speed increase */ | |
990 | rp->next = tp; | |
991 | if (lp) { | |
992 | lp->next = rp; | |
993 | } else { | |
994 | rg_list = rp; | |
995 | } | |
996 | break; | |
997 | } | |
998 | } | |
999 | if (!tp) { | |
1000 | rp->next = NULL; | |
1001 | if (lp) { | |
1002 | lp->next = rp; | |
1003 | } else { | |
1004 | rg_list = rp; | |
1005 | } | |
1006 | } | |
1007 | ||
1008 | } | |
1009 | return rp; | |
1010 | } | |
1011 | ||
c4d6944b | 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) { |
1e0c4014 CP |
1013 | struct rg_struct *newrow, *lp, *cp; |
1014 | time_t rexpires; | |
1015 | char glineiddata[1024]; | |
86178656 CP |
1016 | const char **p; |
1017 | ||
1e0c4014 | 1018 | if (iexpires == 0) { |
5f3eb6fa CP |
1019 | int qexpires; |
1020 | if(!protectedatoi(expires, &qexpires)) | |
1e0c4014 | 1021 | return NULL; |
5f3eb6fa | 1022 | rexpires = (time_t)qexpires; |
1e0c4014 CP |
1023 | } else { |
1024 | rexpires = iexpires; | |
1025 | } | |
1026 | ||
1027 | newrow = rg_newstruct(rexpires); | |
1028 | ||
1029 | if(newrow) { | |
1030 | const char *error; | |
1031 | int erroroffset; | |
1032 | ||
86178656 CP |
1033 | for(p=classes;*p;p++) { |
1034 | if(!strcasecmp(class, *p)) { | |
1035 | newrow->class = *p; | |
1036 | break; | |
1037 | } | |
1038 | } | |
1039 | ||
1040 | if(!*p) | |
1041 | newrow->class = "unknown"; | |
1042 | ||
1e0c4014 CP |
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); | |
1045 | goto dispose; | |
1046 | } else { | |
1047 | newrow->hint = pcre_study(newrow->regex, 0, &error); | |
1048 | if(error) { | |
1049 | Error("regexgline", ERR_WARNING, "Error studying expression %s: %s", mask, error); | |
1050 | pcre_free(newrow->regex); | |
1051 | goto dispose; | |
1052 | } | |
1053 | } | |
1054 | ||
eba15ac4 | 1055 | newrow->id = id; |
c4d6944b CP |
1056 | newrow->hitssaved = hitssaved; |
1057 | newrow->lastseen = lastseen; | |
86178656 | 1058 | |
1e0c4014 CP |
1059 | newrow->mask = getsstring(mask, RG_REGEXGLINE_MAX); |
1060 | if(!newrow->mask) { | |
1061 | Error("regexgline", ERR_WARNING, "Error allocating memory for mask!"); | |
1062 | goto dispose2; | |
1063 | } | |
1064 | ||
1065 | newrow->setby = getsstring(setby, ACCOUNTLEN); | |
1066 | if(!newrow->setby) { | |
1067 | Error("regexgline", ERR_WARNING, "Error allocating memory for setby!"); | |
1068 | goto dispose2; | |
1069 | } | |
1070 | ||
1071 | newrow->reason = getsstring(reason, RG_REASON_MAX); | |
1072 | if(!newrow->reason) { | |
1073 | Error("regexgline", ERR_WARNING, "Error allocating memory for reason!"); | |
1074 | goto dispose2; | |
1075 | } | |
1076 | ||
1077 | if(!protectedatoi(type, &newrow->type)) | |
1078 | newrow->type = 0; /* just in case */ | |
1079 | ||
5f3eb6fa | 1080 | snprintf(glineiddata, sizeof(glineiddata), "%s regexgline %s %s %s %d %d", mynumeric->content, mask, setby, reason, (int)iexpires, newrow->type); |
1e0c4014 CP |
1081 | newrow->glineid = crc32(glineiddata); |
1082 | } | |
1083 | ||
1084 | return newrow; | |
1085 | ||
1086 | dispose2: | |
1087 | if(newrow->mask) | |
1088 | freesstring(newrow->mask); | |
1089 | if(newrow->setby) | |
1090 | freesstring(newrow->setby); | |
1091 | if(newrow->reason) | |
1092 | freesstring(newrow->reason); | |
1093 | pcre_free(newrow->regex); | |
1094 | if(newrow->hint) | |
1095 | pcre_free(newrow->hint); | |
1096 | ||
1097 | dispose: | |
1098 | for(lp=NULL,cp=rg_list;cp;lp=cp,cp=cp->next) { | |
1099 | if(newrow == cp) { | |
1100 | if(lp) { | |
1101 | lp->next = cp->next; | |
1102 | } else { | |
1103 | rg_list = cp->next; | |
1104 | } | |
1105 | free(newrow); | |
1106 | break; | |
1107 | } | |
1108 | } | |
1109 | return NULL; | |
1110 | } | |
1111 | ||
de2bb012 | 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?! */ |
1fbb1306 | 1113 | int usercount = 0; |
444d2433 | 1114 | int validdelay; |
a473a1be | 1115 | char reason[200]; |
2d7e1ef2 | 1116 | int glineflags = 0; |
1e0c4014 | 1117 | |
d28e0741 | 1118 | rg_loggline(rp, np); |
1e0c4014 | 1119 | |
444d2433 | 1120 | if (rp->type == INSTANT_HOST_GLINE) { |
5f3eb6fa | 1121 | usercount = np->host->clonecount; |
5f3eb6fa CP |
1122 | } |
1123 | ||
444d2433 | 1124 | if ((rp->type == INSTANT_IDENT_GLINE) || (usercount > rg_max_per_gline)) { |
1e0c4014 CP |
1125 | nick *tnp; |
1126 | ||
5f3eb6fa | 1127 | for(usercount=0,tnp=np->host->nicks;tnp;tnp=tnp->nextbyhost) |
1e0c4014 CP |
1128 | if(!ircd_strcmp(np->ident, tnp->ident)) |
1129 | usercount++; | |
1130 | ||
2d7e1ef2 | 1131 | glineflags = GLINE_ALWAYS_USER; |
0c4a5a3e CP |
1132 | } |
1133 | ||
46b488b3 | 1134 | if(!strcmp(rp->class, RESERVED_NICK_CLASS)) { |
2e5173ff | 1135 | char reason[512]; |
46b488b3 | 1136 | snprintf(reason, sizeof(reason), "AUTO %s (ID: %08lx)", rp->reason->content, rp->glineid); |
3c5c26a8 | 1137 | glinebynick(np, RESERVED_NICK_GLINE_DURATION, reason, GLINE_ALWAYS_NICK, "regexgline"); |
46b488b3 | 1138 | } |
e2fa40b2 | 1139 | |
444d2433 CP |
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)) { | |
1e0c4014 CP |
1142 | struct rg_glinenode *nn = (struct rg_glinenode *)malloc(sizeof(struct rg_glinenode)); |
1143 | if(nn) { | |
1144 | nn->next = NULL; | |
1145 | if(gll->end) { | |
1146 | gll->end->next = nn; | |
1147 | gll->end = nn; | |
1148 | } else { | |
1149 | gll->start = nn; | |
1150 | gll->end = nn; | |
1151 | } | |
444d2433 | 1152 | |
1e0c4014 CP |
1153 | nn->np = np; |
1154 | nn->reason = rp; | |
444d2433 CP |
1155 | if(!validdelay) { |
1156 | nn->punish = INSTANT_KILL; | |
5f3eb6fa CP |
1157 | } else { |
1158 | nn->punish = rp->type; | |
1159 | } | |
1e0c4014 | 1160 | } |
de2bb012 | 1161 | return usercount; |
1e0c4014 CP |
1162 | } |
1163 | ||
444d2433 | 1164 | if (rp->type == INSTANT_IDENT_GLINE) { |
fad97385 | 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":""); |
444d2433 | 1166 | } else if(rp->type == INSTANT_HOST_GLINE) { |
fad97385 | 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":""); |
444d2433 CP |
1168 | } else { |
1169 | return 0; | |
5f3eb6fa CP |
1170 | } |
1171 | ||
36c1eaf1 | 1172 | rg_shadowserver(np, rp, rp->type); |
a473a1be | 1173 | snprintf(reason, sizeof(reason), "AUTO: %s (ID: %08lx)", rp->reason->content, rp->glineid); |
3c5c26a8 | 1174 | glinebynick(np, rg_expiry_time, reason, glineflags, "regexgline"); |
de2bb012 CP |
1175 | return usercount; |
1176 | } | |
1177 | ||
fad97385 CP |
1178 | static char *gvhost(nick *np) { |
1179 | static char buf[NICKLEN+1+USERLEN+1+HOSTLEN+1+ACCOUNTLEN+4+REALLEN+1+10]; | |
1180 | ||
1181 | if(IsAccount(np)) { | |
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); | |
1183 | } else { | |
1184 | snprintf(buf, sizeof(buf), "%s!%s@%s r(%s)", np->nick, np->ident, np->host->name->content, np->realname->name->content); | |
1185 | } | |
1186 | ||
1187 | return buf; | |
1188 | } | |
1189 | ||
481738a4 CP |
1190 | static int floodprotection = 0; |
1191 | static int lastfloodspam = 0; | |
de2bb012 CP |
1192 | |
1193 | void rg_dogline(struct rg_glinelist *gll, nick *np, struct rg_struct *rp, char *matched) { | |
1194 | int t = time(NULL); | |
1195 | ||
1196 | if(t > floodprotection) { | |
1197 | floodprotection = t; | |
1198 | } else if((floodprotection - t) / 8 > RG_NETWORK_WIDE_MAX_GLINES_PER_8_SEC) { | |
b88719fa | 1199 | if(t > lastfloodspam + 3600) { |
481738a4 CP |
1200 | channel *cp = findchannel("#twilightzone"); |
1201 | if(cp) | |
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!"); | |
1204 | lastfloodspam = t; | |
b88719fa | 1205 | floodprotection = t + RG_NETWORK_WIDE_MAX_GLINES_PER_8_SEC * 3600 * 8; |
481738a4 | 1206 | } |
b88719fa | 1207 | return; |
de2bb012 CP |
1208 | } |
1209 | ||
1210 | floodprotection+=__rg_dogline(gll, np, rp, matched); | |
1e0c4014 CP |
1211 | } |
1212 | ||
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]; | |
1216 | int masklen; | |
526e7c1d | 1217 | |
1e0c4014 | 1218 | va_list va; |
16739dbe CP |
1219 | |
1220 | if(details) { | |
1221 | va_start(va, details); | |
1222 | vsnprintf(buf, sizeof(buf), details, va); | |
1223 | va_end(va); | |
1224 | } else { | |
1225 | buf[0] = '\0'; | |
1226 | } | |
1227 | ||
1e0c4014 CP |
1228 | if(np) { |
1229 | if (IsAccount(np)) { | |
1230 | strncpy(account, np->authname, sizeof(account) - 1); | |
1231 | account[sizeof(account) - 1] = '\0'; | |
1232 | } else { | |
1233 | account[0] = '\0'; | |
1234 | } | |
d28e0741 | 1235 | masklen = RGBuildHostname(mask, np); |
1e0c4014 CP |
1236 | } else { |
1237 | mask[0] = '\0'; | |
1238 | masklen = 0; | |
1239 | } | |
1240 | ||
eba15ac4 CP |
1241 | dbescapestring(eeevent, event, strlen(event)); |
1242 | dbescapestring(eedetails, buf, strlen(buf)); | |
63c6ac26 | 1243 | dbescapestring(eeaccount, account, strlen(account)); |
eba15ac4 | 1244 | dbescapestring(eemask, mask, masklen); |
1e0c4014 | 1245 | |
63c6ac26 | 1246 | dbquery("INSERT INTO regexgline.clog (host, account, event, arg, ts) VALUES ('%s', '%s', '%s', '%s', NOW())", eemask, eeaccount, eeevent, eedetails); |
1e0c4014 CP |
1247 | } |
1248 | ||
d28e0741 CP |
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]; | |
1e0c4014 | 1251 | |
01d1f71c | 1252 | rg->hits++; |
c4d6944b CP |
1253 | rg->hitssaved++; |
1254 | rg->lastseen = time(NULL); | |
1255 | rg->dirty = 1; | |
01d1f71c | 1256 | |
eba15ac4 CP |
1257 | /* @paul: disabled */ |
1258 | ||
526e7c1d | 1259 | return; |
eba15ac4 CP |
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)); | |
1e0c4014 | 1264 | |
63c6ac26 | 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); |
1e0c4014 | 1266 | } |
c4d6944b CP |
1267 | |
1268 | static unsigned int getrgmarker(void) { | |
1269 | static unsigned int marker = 0; | |
1270 | ||
1271 | marker++; | |
1272 | if(!marker) { | |
1273 | struct rg_struct *l; | |
1274 | ||
1275 | /* If we wrapped to zero, zap the marker on all hosts */ | |
1276 | for(l=rg_list;l;l=l->next) | |
1277 | l->marker=0; | |
1278 | marker++; | |
1279 | } | |
1280 | ||
1281 | return marker; | |
1282 | } | |
1283 | ||
1284 | void rg_flush_schedule(void *arg) { | |
1285 | struct rg_struct *l; | |
1286 | ||
1287 | for(l=rg_list;l;l=l->next) { | |
1288 | if(!l->dirty) | |
1289 | continue; | |
1290 | ||
58a4da4a | 1291 | dbquery("UPDATE regexgline.glines SET lastseen = %jd, hits = %lu WHERE id = %d", (intmax_t)l->lastseen, l->hitssaved, l->id); |
c4d6944b CP |
1292 | |
1293 | l->dirty = 0; | |
1294 | } | |
1295 | } | |
1296 |