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