]> jfr.im git - irc/quakenet/newserv.git/blob - glines/glines_buf.c
CHANSERV: fix issue where chanserv_relay doesn't wait for db to be loaded before...
[irc/quakenet/newserv.git] / glines / glines_buf.c
1 #include <stdarg.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <assert.h>
5 #include "../lib/array.h"
6 #include "../lib/irc_string.h"
7 #include "../irc/irc.h"
8 #include "../control/control.h"
9 #include "../trusts/trusts.h"
10 #include "glines.h"
11
12 static int nextglinebufid = 1;
13 glinebuf *glinebuflog[MAXGLINELOG] = {};
14 int glinebuflogoffset = 0;
15
16 void glinebufinit(glinebuf *gbuf, int id) {
17 gbuf->id = id;
18 gbuf->comment = NULL;
19 gbuf->commit = 0;
20 gbuf->amend = 0;
21 gbuf->glines = NULL;
22 gbuf->hitsvalid = 1;
23 gbuf->userhits = 0;
24 gbuf->channelhits = 0;
25 array_init(&gbuf->hits, sizeof(sstring *));
26 }
27
28 gline *glinebufadd(glinebuf *gbuf, const char *mask, const char *creator, const char *reason, time_t expire, time_t lastmod, time_t lifetime) {
29 gline *gl;
30
31 gl = makegline(mask); /* sets up nick,user,host,node and flags */
32
33 if (!gl) {
34 /* couldn't process gline mask */
35 Error("gline", ERR_WARNING, "Tried to add malformed G-Line %s!", mask);
36 return 0;
37 }
38
39 gl->creator = getsstring(creator, 255);
40
41 /* it's not unreasonable to assume gline is active, if we're adding a deactivated gline, we can remove this later */
42 gl->flags |= GLINE_ACTIVE;
43
44 gl->reason = getsstring(reason, 255);
45 gl->expire = expire;
46 gl->lastmod = lastmod;
47 gl->lifetime = lifetime;
48
49 gl->next = gbuf->glines;
50 gbuf->glines = gl;
51
52 gbuf->hitsvalid = 0;
53
54 return gl;
55 }
56
57 void glinebufaddbyip(glinebuf *gbuf, const char *user, struct irc_in_addr *ip, unsigned char bits, int flags, const char *creator, const char *reason, time_t expire, time_t lastmod, time_t lifetime) {
58 trusthost *th, *oth;
59 char mask[512];
60 unsigned char nodebits;
61
62 nodebits = getnodebits(ip);
63
64 if (!(flags & GLINE_IGNORE_TRUST)) {
65 th = th_getbyhost(ip);
66
67 if (th && (th->group->flags & TRUST_RELIABLE_USERNAME)) { /* Trust with reliable usernames */
68 for (oth = th->group->hosts; oth; oth = oth->next)
69 glinebufaddbyip(gbuf, user, &oth->ip, oth->bits, flags | GLINE_ALWAYS_USER | GLINE_IGNORE_TRUST, creator, reason, expire, lastmod, lifetime);
70
71 return;
72 }
73 }
74
75 if (!(flags & GLINE_ALWAYS_USER))
76 user = "*";
77
78 /* Widen gline to match the node mask. */
79 if (nodebits < bits)
80 bits = nodebits;
81
82 snprintf(mask, sizeof(mask), "%s@%s", user, (bits == 128) ? IPtostr(*ip) : CIDRtostr(*ip, bits));
83
84 glinebufadd(gbuf, mask, creator, reason, expire, lastmod, lifetime);
85 }
86
87 void glinebufaddbynick(glinebuf *gbuf, nick *np, int flags, const char *creator, const char *reason, time_t expire, time_t lastmod, time_t lifetime) {
88 if (flags & GLINE_ALWAYS_NICK) {
89 char mask[512];
90 snprintf(mask, sizeof(mask), "%s!*@*", np->nick);
91 glinebufadd(gbuf, mask, creator, reason, expire, lastmod, lifetime);
92 } else {
93 glinebufaddbyip(gbuf, np->ident, &np->ipaddress, 128, flags, creator, reason, expire, lastmod, lifetime);
94 }
95 }
96
97 void glinebufaddbywhowas(glinebuf *gbuf, whowas *ww, int flags, const char *creator, const char *reason, time_t expire, time_t lastmod, time_t lifetime) {
98 nick *np = &ww->nick;
99
100 if (flags & GLINE_ALWAYS_NICK) {
101 char mask[512];
102 snprintf(mask, sizeof(mask), "%s!*@*", np->nick);
103 glinebufadd(gbuf, mask, creator, reason, expire, lastmod, lifetime);
104 } else {
105 glinebufaddbyip(gbuf, np->ident, &np->ipaddress, 128, flags, creator, reason, expire, lastmod, lifetime);
106 }
107 }
108
109 void glinebufcounthits(glinebuf *gbuf, int *users, int *channels) {
110 gline *gl;
111 int i, hit, slot;
112 chanindex *cip;
113 channel *cp;
114 nick *np;
115 char uhmask[512];
116
117 #if 0 /* Let's just do a new hit check anyway. */
118 if (gbuf->hitsvalid)
119 return;
120 #endif
121
122 gbuf->userhits = 0;
123 gbuf->channelhits = 0;
124
125 for (i = 0; i < gbuf->hits.cursi; i++)
126 freesstring(((sstring **)gbuf->hits.content)[i]);
127
128 array_free(&gbuf->hits);
129 array_init(&gbuf->hits, sizeof(sstring *));
130
131 for (i = 0; i<CHANNELHASHSIZE; i++) {
132 for (cip = chantable[i]; cip; cip = cip->next) {
133 cp = cip->channel;
134
135 if (!cp)
136 continue;
137
138 hit = 0;
139
140 for (gl = gbuf->glines; gl; gl = gl->next) {
141 if (gline_match_channel(gl, cp)) {
142 hit = 1;
143 break;
144 }
145 }
146
147 if (hit) {
148 snprintf(uhmask, sizeof(uhmask), "channel: %s", cip->name->content);
149
150 gbuf->channelhits++;
151
152 slot = array_getfreeslot(&gbuf->hits);
153 ((sstring **)gbuf->hits.content)[slot] = getsstring(uhmask, 512);
154 }
155 }
156 }
157
158 for (i = 0; i < NICKHASHSIZE; i++) {
159 for (np = nicktable[i]; np; np = np->next) {
160 hit = 0;
161
162 for (gl = gbuf->glines; gl; gl = gl->next) {
163 if (gline_match_nick(gl, np)) {
164 hit = 1;
165 break;
166 }
167 }
168
169 if (hit) {
170 snprintf(uhmask, sizeof(uhmask), "user: %s!%s@%s%s%s r(%s)", np->nick, np->ident, np->host->name->content,
171 (np->auth) ? "/" : "", (np->auth) ? np->authname : "", np->realname->name->content);
172
173 gbuf->userhits++;
174
175 slot = array_getfreeslot(&gbuf->hits);
176 ((sstring **)gbuf->hits.content)[slot] = getsstring(uhmask, 512);
177 }
178 }
179 }
180
181 gbuf->hitsvalid = 1;
182
183 if (users)
184 *users = gbuf->userhits;
185
186 if (channels)
187 *channels = gbuf->channelhits;
188 }
189
190 int glinebufchecksane(glinebuf *gbuf, nick *spewto, int overridesanity, int overridelimit) {
191 gline *gl;
192 int users, channels, good;
193 const char *hint;
194
195 glinebufcounthits(gbuf, &users, &channels);
196
197 if (!overridelimit) {
198 if (channels > MAXUSERGLINECHANNELHITS) {
199 controlreply(spewto, "G-Lines would hit %d channels. Limit is %d. Not setting G-Lines.", channels, MAXUSERGLINECHANNELHITS);
200 return 0;
201 } else if (users > MAXUSERGLINEUSERHITS) {
202 controlreply(spewto, "G-Lines would hit %d users. Limit is %d. Not setting G-Lines.", users, MAXUSERGLINEUSERHITS);
203 return 0;
204 }
205 }
206
207 good = 1;
208
209 if (!overridesanity) {
210 /* Remove glines that fail the sanity check */
211 for (gl = gbuf->glines; gl; gl = gl->next) {
212 if (!isglinesane(gl, &hint)) {
213 controlreply(spewto, "Sanity check failed for G-Line on '%s' - Skipping: %s",
214 glinetostring(gl), hint);
215 good = 0;
216 }
217 }
218 }
219
220 return good;
221 }
222
223 void glinebufspew(glinebuf *gbuf, nick *spewto) {
224 gline *gl;
225 int i;
226 char timebuf[30], lastmod[30];
227
228 if (!gbuf->hitsvalid)
229 glinebufcounthits(gbuf, NULL, NULL);
230
231 if (gbuf->id == 0)
232 controlreply(spewto, "Uncommitted G-Line transaction.");
233 else
234 controlreply(spewto, "G-Line transaction ID %d", gbuf->id);
235
236 controlreply(spewto, "Comment: %s", (gbuf->comment) ? gbuf->comment->content : "(no comment)");
237
238 if (gbuf->commit) {
239 strftime(timebuf, sizeof(timebuf), "%d/%m/%y %H:%M:%S", localtime(&gbuf->commit));
240 controlreply(spewto, "Committed at: %s", timebuf);
241 }
242
243 if (gbuf->amend) {
244 strftime(timebuf, sizeof(timebuf), "%d/%m/%y %H:%M:%S", localtime(&gbuf->amend));
245 controlreply(spewto, "Amended at: %s", timebuf);
246 }
247
248 controlreply(spewto, "Mask Expiry Last modified Creator Reason");
249
250 for (gl = gbuf->glines; gl; gl = gl->next) {
251 strftime(timebuf, sizeof(timebuf), "%d/%m/%y %H:%M:%S", localtime(&gl->expire));
252
253 if (gl->lastmod == 0)
254 strncpy(lastmod, "<ulined>", sizeof(lastmod));
255 else
256 strftime(lastmod, sizeof(lastmod), "%d/%m/%y %H:%M:%S", localtime(&gl->lastmod));
257
258 controlreply(spewto, "%-40s %-20s %-20s %-25s %s", glinetostring(gl), timebuf, lastmod, gl->creator->content, gl->reason ? gl->reason->content : "");
259 }
260
261 controlreply(spewto, "Hits");
262
263 for (i = 0; i < gbuf->hits.cursi; i++) {
264 controlreply(spewto, "%s", ((sstring **)gbuf->hits.content)[i]->content);
265
266 if (i >= 500) {
267 controlreply(spewto, "More than 500 hits, list truncated.");
268 break;
269 }
270 }
271
272 if (i == 0)
273 controlreply(spewto, "(no hits)");
274 }
275
276 void glinebufmerge(glinebuf *gbuf) {
277 /* TODO: reimplement */
278
279 /*
280 if (gbuf->merge) {
281 /-* Check if an existing gline supercedes this mask *-/
282 for (sgl = gbuf->glines; sgl; sgl = sgl->next) {
283 if (gline_match_mask(sgl, gl)) {
284 freegline(gl);
285 return sgl;
286 }
287 }
288
289 /-* Remove older glines this gline matches *-/
290 for (pnext = &gbuf->glines; *pnext; pnext = &((*pnext)->next)) {
291 sgl = *pnext;
292
293 if (gline_match_mask(gl, sgl)) {
294 *pnext = sgl->next;
295 freegline(sgl);
296
297 if (!*pnext)
298 break;
299 }
300 }
301 }
302 */
303 }
304
305 int glinebufcommit(glinebuf *gbuf, int propagate) {
306 gline *gl, *next, *sgl;
307 int users, channels, id;
308
309 /* Sanity check */
310 glinebufcounthits(gbuf, &users, &channels);
311
312 if (propagate && (users > MAXGLINEUSERHITS || channels > MAXGLINECHANNELHITS)) {
313 controlwall(NO_OPER, NL_GLINES_AUTO, "G-Line buffer would hit %d users/%d channels. Not setting G-Lines.");
314 glinebufabort(gbuf);
315 return 0;
316 }
317
318 /* Record the commit time */
319 time(&gbuf->commit);
320
321 id = glinebufwritelog(gbuf, propagate);
322
323 /* Move glines to the global gline list */
324 for (gl = gbuf->glines; gl; gl = next) {
325 next = gl->next;
326
327 sgl = findgline(glinetostring(gl));
328
329 if (sgl) {
330 /* existing gline
331 * in 1.4, can update expire, reason etc
332 * in 1.3 can only activate/deactivate an existing gline */
333
334 if (gl->flags & GLINE_ACTIVE && !(sgl->flags & GLINE_ACTIVE))
335 gline_activate(sgl, 0, 0);
336 else if (!(gl->flags & GLINE_ACTIVE) && sgl->flags & GLINE_ACTIVE)
337 gline_deactivate(sgl, 0, 0);
338
339 #if SNIRCD_VERSION >= 140
340 sgl->expire = gl->expire;
341
342 if (gl->lifetime > sgl->lifetime)
343 sgl->lifetime = gl->lifetime;
344
345 freesstring(sgl->reason);
346 sgl->reason = getsstring(gl->reason, 512);
347 #endif
348
349 freegline(gl);
350 gl = sgl;
351 } else {
352 gl->next = glinelist;
353 glinelist = gl;
354 }
355
356 gl->glinebufid = id;
357
358 if (propagate)
359 gline_propagate(gl);
360 }
361
362 /* We've moved all glines to the global gline list. Clear glines link in the glinebuf. */
363 gbuf->glines = NULL;
364
365 glinebufabort(gbuf);
366
367 return id;
368 }
369
370 void glinebufabort(glinebuf *gbuf) {
371 gline *gl, *next;
372 int i;
373
374 for (gl = gbuf->glines; gl; gl = next) {
375 next = gl->next;
376
377 freegline(gl);
378 }
379
380 freesstring(gbuf->comment);
381
382 for (i = 0; i < gbuf->hits.cursi; i++)
383 freesstring(((sstring **)gbuf->hits.content)[i]);
384
385 array_free(&gbuf->hits);
386 }
387
388 int glinebufundo(int id) {
389 glinebuf *gbl;
390 gline *gl, *sgl;
391 int i;
392
393 for (i = 0; i < MAXGLINELOG; i++) {
394 gbl = glinebuflog[i];
395
396 if (!gbl || gbl->id != id)
397 continue;
398
399 for (gl = gbl->glines; gl; gl = gl->next) {
400 sgl = findgline(glinetostring(gl));
401
402 if (!sgl)
403 continue;
404
405 sgl->glinebufid = 0;
406
407 gline_deactivate(sgl, 0, 1);
408 }
409
410 glinebufabort(gbl);
411 glinebuflog[i] = NULL;
412
413 return 1;
414 }
415
416 return 0;
417 }
418
419 void glinebufcommentf(glinebuf *gbuf, const char *format, ...) {
420 char comment[512];
421 va_list va;
422
423 va_start(va, format);
424 vsnprintf(comment, 511, format, va);
425 comment[511] = '\0';
426 va_end(va);
427
428 gbuf->comment = getsstring(comment, 512);
429 }
430
431 void glinebufcommentv(glinebuf *gbuf, const char *prefix, int cargc, char **cargv) {
432 char comment[512];
433 int i;
434
435 if (prefix)
436 strncpy(comment, prefix, sizeof(comment));
437 else
438 comment[0] = '\0';
439
440 for (i = 0; i < cargc; i++) {
441 if (comment[0])
442 strncat(comment, " ", sizeof(comment));
443
444 strncat(comment, cargv[i], sizeof(comment));
445 }
446
447 comment[511] = '\0';
448
449 gbuf->comment = getsstring(comment, 512);
450 }
451
452 int glinebufwritelog(glinebuf *gbuf, int propagating) {
453 int i, slot;
454 gline *gl, *sgl;
455 glinebuf *gbl;
456
457 if (!gbuf->glines)
458 return 0; /* Don't waste log IDs on empty gline buffers */
459
460 gbl = NULL;
461
462 /* Find an existing log buffer with the same id */
463 if (gbuf->id != 0) {
464 for (i = 0; i < MAXGLINELOG; i++) {
465 if (!glinebuflog[i])
466 continue;
467
468 if (glinebuflog[i]->id == gbuf->id) {
469 gbl = glinebuflog[i];
470 gbl->amend = gbuf->commit;
471
472 /* We're going to re-insert this glinebuf, so remove it for now */
473 glinebuflog[i] = NULL;
474
475 break;
476 }
477 }
478 }
479
480 /* Find a recent glinebuf that's a close match */
481 if (!gbl && !propagating) {
482 for (i = 0; i < MAXGLINELOG; i++) {
483 if (!glinebuflog[i])
484 continue;
485
486 if (glinebuflog[i]->commit < getnettime() - 5 && glinebuflog[i]->amend < getnettime() - 5)
487 continue;
488
489 assert(glinebuflog[i]->glines);
490
491 if (strcmp(glinebuflog[i]->glines->creator->content, gbuf->glines->creator->content) != 0)
492 continue;
493
494 gbl = glinebuflog[i];
495 gbl->amend = gbuf->commit;
496
497 /* We're going to re-insert this glinebuf, so remove it for now */
498 glinebuflog[i] = NULL;
499
500 break;
501 }
502 }
503
504 /* Make a new glinebuf for the log */
505 if (!gbl && gbuf->id == 0) {
506 gbl = malloc(sizeof(glinebuf));
507 glinebufinit(gbl, (gbuf->id == 0) ? nextglinebufid++ : gbuf->id);
508
509 assert(gbl->hitsvalid);
510
511 if (gbuf->comment)
512 glinebufcommentf(gbl, "%s", gbuf->comment->content);
513 else if (!propagating)
514 glinebufcommentf(gbl, "G-Lines set by %s", gbuf->glines->creator->content);
515
516 gbl->commit = gbuf->commit;
517 }
518
519 /* Save a duplicate of the glines in the log buffer */
520 for (gl = gbuf->glines; gl; gl = gl->next) {
521 sgl = glinedup(gl);
522 sgl->next = gbl->glines;
523 gbl->glines = sgl;
524 }
525
526 gbl->userhits += gbuf->userhits;
527 gbl->channelhits += gbuf->channelhits;
528
529 assert(gbuf->userhits + gbuf->channelhits == gbuf->hits.cursi);
530
531 for (i = 0; i < gbuf->hits.cursi; i++) {
532 slot = array_getfreeslot(&gbl->hits);
533 ((sstring **)gbl->hits.content)[slot] = getsstring(((sstring **)gbuf->hits.content)[i]->content, 512);
534 }
535
536 /* Log the transaction */
537 glinebuflogoffset++;
538
539 if (glinebuflogoffset >= MAXGLINELOG)
540 glinebuflogoffset = 0;
541
542 if (glinebuflog[glinebuflogoffset])
543 glinebufabort(glinebuflog[glinebuflogoffset]);
544
545 glinebuflog[glinebuflogoffset] = gbl;
546
547 return gbl->id;
548 }