]> jfr.im git - irc/quakenet/newserv.git/blob - trusts/data.c
th_updatechildren: Fix bug when iterating over empty TGs.
[irc/quakenet/newserv.git] / trusts / data.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <stdio.h>
4
5 #include "../lib/sstring.h"
6 #include "../core/hooks.h"
7 #include "../core/nsmalloc.h"
8 #include "../lib/irc_string.h"
9 #include "../irc/irc.h"
10 #include "trusts.h"
11
12 trustgroup *tglist;
13
14 void th_dbupdatecounts(trusthost *);
15 void tg_dbupdatecounts(trustgroup *);
16
17 static trusthost *th_getnextchildbyhost(trusthost *, trusthost *);
18
19 void trusts_freeall(void) {
20 trustgroup *tg, *ntg;
21 trusthost *th, *nth;
22
23 for(tg=tglist;tg;tg=ntg) {
24 ntg = tg->next;
25 for(th=tg->hosts;th;th=nth) {
26 nth = th->next;
27
28 th_free(th);
29 }
30
31 tg_free(tg, 1);
32 }
33
34 tglist = NULL;
35 }
36
37 trustgroup *tg_getbyid(unsigned int id) {
38 trustgroup *tg;
39
40 for(tg=tglist;tg;tg=tg->next)
41 if(tg->id == id)
42 return tg;
43
44 return NULL;
45 }
46
47 void th_free(trusthost *th) {
48 triggerhook(HOOK_TRUSTS_LOSTHOST, th);
49
50 nsfree(POOL_TRUSTS, th);
51 }
52
53 static void th_updatechildren(trusthost *th) {
54 trusthost *nth = NULL;
55
56 th->children = NULL;
57
58 for(;;) {
59 nth = th_getnextchildbyhost(th, nth);
60 if(!nth)
61 break;
62
63 nth->nextbychild = th->children;
64 th->children = nth;
65 }
66 }
67
68 void th_linktree(void) {
69 trustgroup *tg;
70 trusthost *th;
71
72 /* ugh */
73 for(tg=tglist;tg;tg=tg->next)
74 for(th=tg->hosts;th;th=th->next)
75 th->parent = th_getsmallestsupersetbyhost(&th->ip, th->bits);
76
77 for(tg=tglist;tg;tg=tg->next)
78 for(th=tg->hosts;th;th=th->next)
79 if(th->parent)
80 th_updatechildren(th->parent);
81 }
82
83 trusthost *th_add(trusthost *ith) {
84 trusthost *th;
85
86 th = nsmalloc(POOL_TRUSTS, sizeof(trusthost));
87 if(!th)
88 return NULL;
89
90 memcpy(th, ith, sizeof(trusthost));
91
92 th->users = NULL;
93 th->count = 0;
94
95 th->parent = NULL;
96 th->children = NULL;
97
98 th->marker = 0;
99
100 th->next = th->group->hosts;
101 th->group->hosts = th;
102
103 return th;
104 }
105
106 void tg_free(trustgroup *tg, int created) {
107 if(created)
108 triggerhook(HOOK_TRUSTS_LOSTGROUP, tg);
109
110 freesstring(tg->name);
111 freesstring(tg->createdby);
112 freesstring(tg->contact);
113 freesstring(tg->comment);
114 nsfree(POOL_TRUSTS, tg);
115 }
116
117 trustgroup *tg_add(trustgroup *itg) {
118 trustgroup *tg = nsmalloc(POOL_TRUSTS, sizeof(trustgroup));
119 if(!tg)
120 return NULL;
121
122 memcpy(tg, itg, sizeof(trustgroup));
123
124 tg->name = getsstring(tg->name->content, TRUSTNAMELEN);
125 tg->createdby = getsstring(tg->createdby->content, CREATEDBYLEN);
126 tg->contact = getsstring(tg->contact->content, CONTACTLEN);
127 tg->comment = getsstring(tg->comment->content, COMMENTLEN);
128 if(!tg->name || !tg->createdby || !tg->contact || !tg->comment) {
129 tg_free(tg, 0);
130 return NULL;
131 }
132
133 tg->hosts = NULL;
134 tg->marker = 0;
135 tg->count = 0;
136
137 memset(tg->exts, 0, sizeof(tg->exts));
138
139 tg->next = tglist;
140 tglist = tg;
141
142 triggerhook(HOOK_TRUSTS_NEWGROUP, tg);
143
144 return tg;
145 }
146
147 trusthost *th_getbyhost(struct irc_in_addr *ip) {
148 trustgroup *tg;
149 trusthost *th, *result = NULL;
150 uint32_t bits;
151
152 for(tg=tglist;tg;tg=tg->next) {
153 for(th=tg->hosts;th;th=th->next) {
154 if(ipmask_check(ip, &th->ip, th->bits)) {
155 if(!result || (th->bits > bits)) {
156 bits = th->bits;
157 result = th;
158 }
159 }
160 }
161 }
162
163 return result;
164 }
165
166 trusthost *th_getbyhostandmask(struct irc_in_addr *ip, uint32_t bits) {
167 trustgroup *tg;
168 trusthost *th;
169
170 for(tg=tglist;tg;tg=tg->next)
171 for(th=tg->hosts;th;th=th->next)
172 if(ipmask_check(ip, &th->ip, 128) && th->bits == bits)
173 return th;
174
175 return NULL;
176 }
177
178 /* returns the ip with the smallest prefix that is still a superset of the given host */
179 trusthost *th_getsmallestsupersetbyhost(struct irc_in_addr *ip, uint32_t bits) {
180 trustgroup *tg;
181 trusthost *th, *result = NULL;
182 uint32_t sbits;
183
184 for(tg=tglist;tg;tg=tg->next) {
185 for(th=tg->hosts;th;th=th->next) {
186 if(ipmask_check(ip, &th->ip, th->bits)) {
187 if((th->bits < bits) && (!result || (th->bits > sbits))) {
188 sbits = th->bits;
189 result = th;
190 }
191 }
192 }
193 }
194
195 return result;
196 }
197
198 /* returns the first ip that is a subset it comes across */
199 trusthost *th_getsubsetbyhost(struct irc_in_addr *ip, uint32_t bits) {
200 trustgroup *tg;
201 trusthost *th;
202
203 for(tg=tglist;tg;tg=tg->next)
204 for(th=tg->hosts;th;th=th->next)
205 if(ipmask_check(ip, &th->ip, th->bits))
206 if(th->bits > bits)
207 return th;
208
209 return NULL;
210 }
211
212 /* NOT reentrant obviously */
213 static trusthost *th_getnextchildbyhost(trusthost *orig, trusthost *th) {
214 if(!th) {
215 trustgroup *tg;
216
217 tg = tglist;
218 for(tg=tglist;tg;tg=tg->next) {
219 th = tg->hosts;
220 if(th)
221 break;
222 }
223
224 /* INVARIANT: tg => th */
225 if(!tg)
226 return NULL;
227
228 if(th->parent == orig)
229 return th;
230 }
231
232 for(;;) {
233 if(th->next) {
234 th = th->next;
235 } else {
236 trustgroup *tg = th->group;
237
238 do {
239 tg = tg->next;
240 } while (tg && !tg->hosts);
241
242 if(!tg)
243 return NULL;
244
245 th = tg->hosts;
246 }
247
248 if(th->parent == orig)
249 return th;
250 }
251 }
252
253 void th_getsuperandsubsets(struct irc_in_addr *ip, uint32_t bits, trusthost **superset, trusthost **subset) {
254 *superset = th_getsmallestsupersetbyhost(ip, bits);
255 *subset = th_getsubsetbyhost(ip, bits);
256 }
257
258 void trusts_flush(void (*thflush)(trusthost *), void (*tgflush)(trustgroup *)) {
259 trustgroup *tg;
260 trusthost *th;
261 time_t t = getnettime();
262
263 for(tg=tglist;tg;tg=tg->next) {
264 if(tg->count > 0)
265 tg->lastseen = t;
266
267 tgflush(tg);
268
269 for(th=tg->hosts;th;th=th->next) {
270 if(th->count > 0)
271 th->lastseen = t;
272
273 thflush(th);
274 }
275 }
276 }
277
278 trustgroup *tg_strtotg(char *name) {
279 unsigned long id;
280 trustgroup *tg;
281
282 /* legacy format */
283 if(name[0] == '#') {
284 char *endp;
285 id = strtoul(&name[1], &endp, 10);
286 if(!id || *endp)
287 return NULL;
288
289 return tg_getbyid(id);
290 }
291
292 for(tg=tglist;tg;tg=tg->next)
293 if(!strcmp(name, tg->name->content))
294 return tg;
295
296 return NULL;
297 }
298
299 void th_adjusthosts(trusthost *th, trusthost *superset, trusthost *subset) {
300 /*
301 * First and foremost, CIDR doesn't allow hosts to cross boundaries, i.e. everything with a smaller prefix
302 * is entirely contained with the prefix that is one smaller.
303 * e.g. 0.0.0.0/23, 0.0.0.128/23, you can't have a single prefix for 0.0.0.64-0.0.0.192, instead
304 * you have two, 0.0.0.64/26 and 0.0.0.128/26.
305 *
306 * This makes the code MUCH easier as the entire thing is one huge set/tree.
307 *
308 * Four cases here:
309 * 1: host isn't covered by any existing hosts.
310 * 2: host is covered by a less specific one only, e.g. adding 0.0.0.1/32, while 0.0.0.0/24 already exists.
311 * 3: host is covered by a more specific one only, e.g. adding 0.0.0.0/24 while 0.0.0.1/32 already exists
312 * (note there might be more than one more specific host, e.g. 0.0.0.1/32 and 0.0.0.2/32).
313 * 4: covered by more and less specific cases, e.g. adding 0.0.0.0/24 to: { 0.0.0.1/32, 0.0.0.2/32, 0.0.0.0/16 }.
314 *
315 * CASE 1
316 * ------
317 *
318 * !superset && !subset
319 *
320 * Scan through the host hash and add any clients which match our host, this is exactly the same as case 3
321 * but without needing to check (though checking doesn't hurt), so we'll just use the code for that.
322 *
323 * CASE 2
324 * ------
325 *
326 * superset && !subset
327 *
328 * We have the less specific host in 'superset', we know it is the only one so pull out clients in it's
329 * ->users list matching our new host.
330 * No need to look for extra hosts in the main nick hash as they're all covered already.
331 *
332 * CASE 3
333 * ------
334 *
335 * !superset && subset
336 *
337 * We have one host in 'subset', but there might be more than one, we don't care though!
338 * We can scan the entire host hash and pull out any hosts that match us and don't have
339 * a trust group already, this ignores any with a more specific prefix.
340 *
341 * CASE 4
342 * ------
343 *
344 * superset && subset
345 *
346 * Here we first fix up the ones less specific then us, so we just perform what we did for case 2,
347 * then we perform what we did for case 3.
348 *
349 * So in summary:
350 * CASE 1: DO 3
351 * CASE 2: (work)
352 * CASE 3: (work)
353 * CASE 4: DO 2; DO 3
354 * Or:
355 * if(2 || 4) : DO 2
356 * if(1 || 3 || 4): DO 3
357 */
358
359 /* we let the compiler do the boolean minimisation for clarity reasons */
360
361 if((superset && !subset) || (superset && subset)) { /* cases 2 and 4 */
362 nick *np, *nnp;
363 for(np=superset->users;np;np=nnp) {
364 nnp = nextbytrust(np);
365 if(ipmask_check(&np->p_nodeaddr, &th->ip, th->bits)) {
366 trusts_lostnick(np, 1);
367 trusts_newnick(np, 1);
368 }
369 }
370 }
371
372 if((!superset && !subset) || (!superset && subset) || (superset && subset)) { /* cases 1, 3 and 4 */
373 nick *np;
374 int i;
375
376 for(i=0;i<NICKHASHSIZE;i++)
377 for(np=nicktable[i];np;np=np->next)
378 if(!gettrusthost(np) && ipmask_check(&np->p_nodeaddr, &th->ip, th->bits))
379 trusts_newnick(np, 1);
380 }
381 }
382
383 unsigned int nexttgmarker(void) {
384 static unsigned int tgmarker = 0;
385 trustgroup *tg;
386
387 tgmarker++;
388 if(!tgmarker) {
389 /* If we wrapped to zero, zap the marker on all groups */
390 for(tg=tglist;tg;tg=tg->next)
391 tg->marker=0;
392
393 tgmarker++;
394 }
395
396 return tgmarker;
397 }
398
399 unsigned int nextthmarker(void) {
400 static unsigned int thmarker = 0;
401 trustgroup *tg;
402 trusthost *th;
403
404 thmarker++;
405 if(!thmarker) {
406 /* If we wrapped to zero, zap the marker on all hosts */
407 for(tg=tglist;tg;tg=tg->next)
408 for(th=tg->hosts;th;th=th->next)
409 th->marker=0;
410
411 thmarker++;
412 }
413
414 return thmarker;
415 }
416
417 trusthost *th_getbyid(unsigned int id) {
418 trustgroup *tg;
419 trusthost *th;
420
421 for(tg=tglist;tg;tg=tg->next)
422 for(th=tg->hosts;th;th=th->next)
423 if(th->id == id)
424 return th;
425
426 return NULL;
427 }
428
429 int tg_modify(trustgroup *oldtg, trustgroup *newtg) {
430 trustgroup vnewtg;
431
432 memcpy(&vnewtg, oldtg, sizeof(trustgroup));
433
434 /* unfortunately we can't just memcpy the new one over */
435
436 vnewtg.name = getsstring(newtg->name->content, TRUSTNAMELEN);
437 vnewtg.createdby = getsstring(newtg->createdby->content, CREATEDBYLEN);
438 vnewtg.contact = getsstring(newtg->contact->content, CONTACTLEN);
439 vnewtg.comment = getsstring(newtg->comment->content, COMMENTLEN);
440 if(!vnewtg.name || !vnewtg.createdby || !vnewtg.contact || !vnewtg.comment) {
441 freesstring(vnewtg.name);
442 freesstring(vnewtg.createdby);
443 freesstring(vnewtg.contact);
444 freesstring(vnewtg.comment);
445 return 0;
446 }
447
448 /* id remains the same, count/hosts/marker/next/exts are ignored */
449 vnewtg.trustedfor = newtg->trustedfor;
450 vnewtg.mode = newtg->mode;
451 vnewtg.maxperident = newtg->maxperident;
452 vnewtg.maxusage = newtg->maxusage;
453 vnewtg.expires = newtg->expires;
454 vnewtg.lastseen = newtg->lastseen;
455 vnewtg.lastmaxusereset = newtg->lastmaxusereset;
456
457 memcpy(oldtg, &vnewtg, sizeof(trustgroup));
458
459 return 1;
460 }
461
462 int th_modify(trusthost *oldth, trusthost *newth) {
463 oldth->maxpernode = newth->maxpernode;
464 oldth->nodebits = newth->nodebits;
465
466 return 1;
467 }
468