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