]>
Commit | Line | Data |
---|---|---|
212380e3 AC |
1 | /* |
2 | * charybdis: an advanced internet relay chat daemon (ircd). | |
3 | * hostmask.c: Code to efficiently find IP & hostmask based configs. | |
4 | * | |
5 | * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center | |
6 | * Copyright (C) 1996-2002 Hybrid Development Team | |
7 | * Copyright (C) 2002-2005 ircd-ratbox development team | |
05ee179e | 8 | * Copyright (C) 2005-2008 charybdis development team |
212380e3 AC |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; either version 2 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program; if not, write to the Free Software | |
22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
23 | * USA | |
24 | * | |
25 | * $Id: hostmask.c 2757 2006-11-10 22:58:15Z jilles $ | |
26 | */ | |
27 | ||
28 | #include "stdinc.h" | |
212380e3 AC |
29 | #include "ircd_defs.h" |
30 | #include "s_conf.h" | |
31 | #include "hostmask.h" | |
32 | #include "numeric.h" | |
33 | #include "send.h" | |
4562c604 | 34 | #include "match.h" |
212380e3 | 35 | |
ccda6e3f | 36 | #ifdef RB_IPV6 |
212380e3 AC |
37 | static unsigned long hash_ipv6(struct sockaddr *, int); |
38 | #endif | |
39 | static unsigned long hash_ipv4(struct sockaddr *, int); | |
40 | ||
41 | ||
e7046ee5 | 42 | /* int parse_netmask(const char *, struct rb_sockaddr_storage *, int *); |
212380e3 AC |
43 | * Input: A hostmask, or an IPV4/6 address. |
44 | * Output: An integer describing whether it is an IPV4, IPV6 address or a | |
45 | * hostmask, an address(if it is an IP mask), | |
46 | * a bitlength(if it is IP mask). | |
47 | * Side effects: None | |
48 | */ | |
49 | int | |
50 | parse_netmask(const char *text, struct sockaddr *naddr, int *nb) | |
51 | { | |
52 | char *ip = LOCAL_COPY(text); | |
53 | char *ptr; | |
e7046ee5 | 54 | struct rb_sockaddr_storage *addr, xaddr; |
212380e3 AC |
55 | int *b, xb; |
56 | if(nb == NULL) | |
57 | b = &xb; | |
58 | else | |
59 | b = nb; | |
60 | ||
61 | if(naddr == NULL) | |
e7046ee5 | 62 | addr = (struct rb_sockaddr_storage *)&xaddr; |
212380e3 | 63 | else |
e7046ee5 | 64 | addr = (struct rb_sockaddr_storage *)naddr; |
17e4b48b JT |
65 | |
66 | if(strpbrk(ip, "*?") != NULL) | |
67 | { | |
68 | return HM_HOST; | |
69 | } | |
ccda6e3f | 70 | #ifdef RB_IPV6 |
212380e3 AC |
71 | if(strchr(ip, ':')) |
72 | { | |
73 | if((ptr = strchr(ip, '/'))) | |
74 | { | |
75 | *ptr = '\0'; | |
76 | ptr++; | |
77 | *b = atoi(ptr); | |
78 | if(*b > 128) | |
79 | *b = 128; | |
80 | } else | |
81 | *b = 128; | |
caa4d9d2 | 82 | if(rb_inet_pton_sock(ip, (struct sockaddr *)addr) > 0) |
212380e3 AC |
83 | return HM_IPV6; |
84 | else | |
85 | return HM_HOST; | |
86 | } else | |
87 | #endif | |
88 | if(strchr(text, '.')) | |
89 | { | |
90 | if((ptr = strchr(ip, '/'))) | |
91 | { | |
92 | *ptr = '\0'; | |
93 | ptr++; | |
94 | *b = atoi(ptr); | |
95 | if(*b > 32) | |
96 | *b = 32; | |
97 | } else | |
98 | *b = 32; | |
caa4d9d2 | 99 | if(rb_inet_pton_sock(ip, (struct sockaddr *)addr) > 0) |
212380e3 AC |
100 | return HM_IPV4; |
101 | else | |
102 | return HM_HOST; | |
103 | } | |
104 | return HM_HOST; | |
105 | } | |
106 | ||
107 | /* Hashtable stuff...now external as its used in m_stats.c */ | |
108 | struct AddressRec *atable[ATABLE_SIZE]; | |
109 | ||
110 | void | |
111 | init_host_hash(void) | |
112 | { | |
113 | memset(&atable, 0, sizeof(atable)); | |
114 | } | |
115 | ||
e7046ee5 | 116 | /* unsigned long hash_ipv4(struct rb_sockaddr_storage*) |
212380e3 AC |
117 | * Input: An IP address. |
118 | * Output: A hash value of the IP address. | |
119 | * Side effects: None | |
120 | */ | |
121 | static unsigned long | |
122 | hash_ipv4(struct sockaddr *saddr, int bits) | |
123 | { | |
124 | struct sockaddr_in *addr = (struct sockaddr_in *) saddr; | |
125 | ||
126 | if(bits != 0) | |
127 | { | |
128 | unsigned long av = ntohl(addr->sin_addr.s_addr) & ~((1 << (32 - bits)) - 1); | |
129 | return (av ^ (av >> 12) ^ (av >> 24)) & (ATABLE_SIZE - 1); | |
130 | } | |
131 | ||
132 | return 0; | |
133 | } | |
134 | ||
e7046ee5 | 135 | /* unsigned long hash_ipv6(struct rb_sockaddr_storage*) |
212380e3 AC |
136 | * Input: An IP address. |
137 | * Output: A hash value of the IP address. | |
138 | * Side effects: None | |
139 | */ | |
ccda6e3f | 140 | #ifdef RB_IPV6 |
212380e3 AC |
141 | static unsigned long |
142 | hash_ipv6(struct sockaddr *saddr, int bits) | |
143 | { | |
144 | struct sockaddr_in6 *addr = (struct sockaddr_in6 *) saddr; | |
145 | unsigned long v = 0, n; | |
146 | for (n = 0; n < 16; n++) | |
147 | { | |
148 | if(bits >= 8) | |
149 | { | |
150 | v ^= addr->sin6_addr.s6_addr[n]; | |
151 | bits -= 8; | |
152 | } | |
153 | else if(bits) | |
154 | { | |
155 | v ^= addr->sin6_addr.s6_addr[n] & ~((1 << (8 - bits)) - 1); | |
156 | return v & (ATABLE_SIZE - 1); | |
157 | } | |
158 | else | |
159 | return v & (ATABLE_SIZE - 1); | |
160 | } | |
161 | return v & (ATABLE_SIZE - 1); | |
162 | } | |
163 | #endif | |
164 | ||
165 | /* int hash_text(const char *start) | |
166 | * Input: The start of the text to hash. | |
167 | * Output: The hash of the string between 1 and (TH_MAX-1) | |
168 | * Side-effects: None. | |
169 | */ | |
170 | static int | |
171 | hash_text(const char *start) | |
172 | { | |
173 | const char *p = start; | |
174 | unsigned long h = 0; | |
175 | ||
176 | while(*p) | |
177 | { | |
178 | h = (h << 4) - (h + (unsigned char) ToLower(*p++)); | |
179 | } | |
180 | ||
181 | return (h & (ATABLE_SIZE - 1)); | |
182 | } | |
183 | ||
184 | /* unsigned long get_hash_mask(const char *) | |
185 | * Input: The text to hash. | |
186 | * Output: The hash of the string right of the first '.' past the last | |
187 | * wildcard in the string. | |
188 | * Side-effects: None. | |
189 | */ | |
190 | static unsigned long | |
191 | get_mask_hash(const char *text) | |
192 | { | |
193 | const char *hp = "", *p; | |
194 | ||
195 | for (p = text + strlen(text) - 1; p >= text; p--) | |
196 | if(*p == '*' || *p == '?') | |
197 | return hash_text(hp); | |
198 | else if(*p == '.') | |
199 | hp = p + 1; | |
200 | return hash_text(text); | |
201 | } | |
202 | ||
e7046ee5 | 203 | /* struct ConfItem* find_conf_by_address(const char*, struct rb_sockaddr_storage*, |
212380e3 AC |
204 | * int type, int fam, const char *username) |
205 | * Input: The hostname, the address, the type of mask to find, the address | |
206 | * family, the username. | |
207 | * Output: The matching value with the highest precedence. | |
208 | * Side-effects: None | |
209 | * Note: Setting bit 0 of the type means that the username is ignored. | |
210 | */ | |
211 | struct ConfItem * | |
212 | find_conf_by_address(const char *name, const char *sockhost, | |
213 | const char *orighost, | |
214 | struct sockaddr *addr, int type, int fam, | |
40c1fd47 | 215 | const char *username, const char *auth_user) |
212380e3 AC |
216 | { |
217 | unsigned long hprecv = 0; | |
218 | struct ConfItem *hprec = NULL; | |
219 | struct AddressRec *arec; | |
220 | int b; | |
221 | ||
222 | if(username == NULL) | |
223 | username = ""; | |
224 | ||
225 | if(addr) | |
226 | { | |
227 | /* Check for IPV6 matches... */ | |
ccda6e3f | 228 | #ifdef RB_IPV6 |
212380e3 AC |
229 | if(fam == AF_INET6) |
230 | { | |
231 | ||
232 | for (b = 128; b >= 0; b -= 16) | |
233 | { | |
234 | for (arec = atable[hash_ipv6(addr, b)]; arec; arec = arec->next) | |
235 | if(arec->type == (type & ~0x1) && | |
236 | arec->masktype == HM_IPV6 && | |
237 | comp_with_mask_sock(addr, (struct sockaddr *)&arec->Mask.ipa.addr, | |
40c1fd47 VY |
238 | arec->Mask.ipa.bits) && |
239 | (type & 0x1 || match(arec-> username, username)) && | |
240 | (type != CONF_CLIENT || !arec->auth_user || | |
241 | (auth_user && match(arec->auth_user, auth_user))) && | |
242 | arec->precedence > hprecv) | |
212380e3 AC |
243 | { |
244 | hprecv = arec->precedence; | |
245 | hprec = arec->aconf; | |
246 | } | |
247 | } | |
248 | } | |
249 | else | |
250 | #endif | |
251 | if(fam == AF_INET) | |
252 | { | |
253 | for (b = 32; b >= 0; b -= 8) | |
254 | { | |
255 | for (arec = atable[hash_ipv4(addr, b)]; arec; arec = arec->next) | |
256 | if(arec->type == (type & ~0x1) && | |
257 | arec->masktype == HM_IPV4 && | |
212380e3 AC |
258 | comp_with_mask_sock(addr, (struct sockaddr *)&arec->Mask.ipa.addr, |
259 | arec->Mask.ipa.bits) && | |
40c1fd47 VY |
260 | (type & 0x1 || match(arec->username, username)) && |
261 | (type != CONF_CLIENT || !arec->auth_user || | |
262 | (auth_user && match(arec->auth_user, auth_user))) && | |
263 | arec->precedence > hprecv) | |
212380e3 AC |
264 | { |
265 | hprecv = arec->precedence; | |
266 | hprec = arec->aconf; | |
267 | } | |
268 | } | |
269 | } | |
270 | } | |
271 | ||
272 | if(orighost != NULL) | |
273 | { | |
274 | const char *p; | |
275 | ||
276 | for (p = orighost; p != NULL;) | |
277 | { | |
278 | for (arec = atable[hash_text(p)]; arec; arec = arec->next) | |
279 | ||
280 | if((arec->type == (type & ~0x1)) && | |
281 | (arec->masktype == HM_HOST) && | |
282 | arec->precedence > hprecv && | |
283 | match(arec->Mask.hostname, orighost) && | |
40c1fd47 VY |
284 | (type != CONF_CLIENT || !arec->auth_user || |
285 | (auth_user && match(arec->auth_user, auth_user))) && | |
212380e3 AC |
286 | (type & 0x1 || match(arec->username, username))) |
287 | { | |
288 | hprecv = arec->precedence; | |
289 | hprec = arec->aconf; | |
290 | } | |
291 | p = strchr(p, '.'); | |
292 | if(p != NULL) | |
293 | p++; | |
294 | else | |
295 | break; | |
296 | } | |
297 | for (arec = atable[0]; arec; arec = arec->next) | |
298 | { | |
299 | if(arec->type == (type & ~0x1) && | |
300 | arec->masktype == HM_HOST && | |
301 | arec->precedence > hprecv && | |
302 | (match(arec->Mask.hostname, orighost) || | |
303 | (sockhost && match(arec->Mask.hostname, sockhost))) && | |
40c1fd47 VY |
304 | (type != CONF_CLIENT || !arec->auth_user || |
305 | (auth_user && match(arec->auth_user, auth_user))) && | |
212380e3 AC |
306 | (type & 0x1 || match(arec->username, username))) |
307 | { | |
308 | hprecv = arec->precedence; | |
309 | hprec = arec->aconf; | |
310 | } | |
311 | } | |
312 | } | |
313 | ||
314 | if(name != NULL) | |
315 | { | |
316 | const char *p; | |
317 | /* And yes - we have to check p after strchr and p after increment for | |
318 | * NULL -kre */ | |
319 | for (p = name; p != NULL;) | |
320 | { | |
321 | for (arec = atable[hash_text(p)]; arec; arec = arec->next) | |
322 | if((arec->type == (type & ~0x1)) && | |
323 | (arec->masktype == HM_HOST) && | |
324 | arec->precedence > hprecv && | |
325 | match(arec->Mask.hostname, name) && | |
40c1fd47 VY |
326 | (type != CONF_CLIENT || !arec->auth_user || |
327 | (auth_user && match(arec->auth_user, auth_user))) && | |
212380e3 AC |
328 | (type & 0x1 || match(arec->username, username))) |
329 | { | |
330 | hprecv = arec->precedence; | |
331 | hprec = arec->aconf; | |
332 | } | |
333 | p = strchr(p, '.'); | |
334 | if(p != NULL) | |
335 | p++; | |
336 | else | |
337 | break; | |
338 | } | |
339 | for (arec = atable[0]; arec; arec = arec->next) | |
340 | { | |
341 | if(arec->type == (type & ~0x1) && | |
342 | arec->masktype == HM_HOST && | |
343 | arec->precedence > hprecv && | |
344 | (match(arec->Mask.hostname, name) || | |
345 | (sockhost && match(arec->Mask.hostname, sockhost))) && | |
40c1fd47 VY |
346 | (type != CONF_CLIENT || !arec->auth_user || |
347 | (auth_user && match(arec->auth_user, auth_user))) && | |
212380e3 AC |
348 | (type & 0x1 || match(arec->username, username))) |
349 | { | |
350 | hprecv = arec->precedence; | |
351 | hprec = arec->aconf; | |
352 | } | |
353 | } | |
354 | } | |
355 | return hprec; | |
356 | } | |
357 | ||
358 | /* struct ConfItem* find_address_conf(const char*, const char*, | |
e7046ee5 | 359 | * struct rb_sockaddr_storage*, int); |
212380e3 AC |
360 | * Input: The hostname, username, address, address family. |
361 | * Output: The applicable ConfItem. | |
362 | * Side-effects: None | |
363 | */ | |
364 | struct ConfItem * | |
365 | find_address_conf(const char *host, const char *sockhost, const char *user, | |
40c1fd47 | 366 | const char *notildeuser, struct sockaddr *ip, int aftype, char *auth_user) |
212380e3 AC |
367 | { |
368 | struct ConfItem *iconf, *kconf; | |
369 | const char *vuser; | |
370 | ||
371 | /* Find the best I-line... If none, return NULL -A1kmm */ | |
40c1fd47 | 372 | if(!(iconf = find_conf_by_address(host, sockhost, NULL, ip, CONF_CLIENT, aftype, user, auth_user))) |
212380e3 AC |
373 | return NULL; |
374 | /* Find what their visible username will be. | |
375 | * Note that the username without tilde may contain one char more. | |
376 | * -- jilles */ | |
377 | vuser = IsNoTilde(iconf) ? notildeuser : user; | |
378 | ||
379 | /* If they are exempt from K-lines, return the best I-line. -A1kmm */ | |
380 | if(IsConfExemptKline(iconf)) | |
381 | return iconf; | |
382 | ||
383 | /* Find the best K-line... -A1kmm */ | |
40c1fd47 | 384 | kconf = find_conf_by_address(host, sockhost, NULL, ip, CONF_KILL, aftype, user, NULL); |
212380e3 AC |
385 | |
386 | /* If they are K-lined, return the K-line */ | |
387 | if(kconf) | |
388 | return kconf; | |
389 | ||
390 | /* if theres a spoof, check it against klines.. */ | |
391 | if(IsConfDoSpoofIp(iconf)) | |
392 | { | |
27f616dd | 393 | char *p = strchr(iconf->info.name, '@'); |
212380e3 AC |
394 | |
395 | /* note, we dont need to pass sockhost here, as its | |
396 | * guaranteed to not match by whats above.. --anfl | |
397 | */ | |
398 | if(p) | |
399 | { | |
400 | *p = '\0'; | |
27f616dd | 401 | kconf = find_conf_by_address(p+1, NULL, NULL, ip, CONF_KILL, aftype, iconf->info.name, NULL); |
212380e3 AC |
402 | *p = '@'; |
403 | } | |
404 | else | |
27f616dd | 405 | kconf = find_conf_by_address(iconf->info.name, NULL, NULL, ip, CONF_KILL, aftype, vuser, NULL); |
212380e3 AC |
406 | |
407 | if(kconf) | |
408 | return kconf; | |
409 | } | |
410 | ||
411 | /* if no_tilde, check the username without tilde against klines too | |
412 | * -- jilles */ | |
413 | if(user != vuser) | |
414 | { | |
40c1fd47 | 415 | kconf = find_conf_by_address(host, sockhost, NULL, ip, CONF_KILL, aftype, vuser, NULL); |
212380e3 AC |
416 | if(kconf) |
417 | return kconf; | |
418 | } | |
419 | ||
212380e3 AC |
420 | return iconf; |
421 | } | |
422 | ||
e7046ee5 | 423 | /* struct ConfItem* find_dline(struct rb_sockaddr_storage*, int) |
54ac8b60 VY |
424 | * Input: An address, an address family. |
425 | * Output: The best matching D-line or exempt line. | |
426 | * Side effects: None. | |
427 | */ | |
428 | struct ConfItem * | |
429 | find_dline(struct sockaddr *addr, int aftype) | |
430 | { | |
431 | struct ConfItem *eline; | |
40c1fd47 | 432 | eline = find_conf_by_address(NULL, NULL, NULL, addr, CONF_EXEMPTDLINE | 1, aftype, NULL, NULL); |
54ac8b60 VY |
433 | if(eline) |
434 | return eline; | |
40c1fd47 | 435 | return find_conf_by_address(NULL, NULL, NULL, addr, CONF_DLINE | 1, aftype, NULL, NULL); |
54ac8b60 VY |
436 | } |
437 | ||
6f3a09ff | 438 | /* void find_exact_conf_by_address(const char*, int, const char *) |
4e0f14a0 JT |
439 | * Input: |
440 | * Output: ConfItem if found | |
441 | * Side-effects: None | |
442 | */ | |
443 | struct ConfItem * | |
444 | find_exact_conf_by_address(const char *address, int type, const char *username) | |
445 | { | |
446 | int masktype, bits; | |
447 | unsigned long hv; | |
448 | struct AddressRec *arec; | |
e7046ee5 | 449 | struct rb_sockaddr_storage addr; |
4e0f14a0 JT |
450 | |
451 | if(address == NULL) | |
452 | address = "/NOMATCH!/"; | |
4e0f14a0 | 453 | masktype = parse_netmask(address, (struct sockaddr *)&addr, &bits); |
ccda6e3f | 454 | #ifdef RB_IPV6 |
4e0f14a0 JT |
455 | if(masktype == HM_IPV6) |
456 | { | |
457 | /* We have to do this, since we do not re-hash for every bit -A1kmm. */ | |
458 | hv = hash_ipv6((struct sockaddr *)&addr, bits - bits % 16); | |
459 | } | |
460 | else | |
461 | #endif | |
462 | if(masktype == HM_IPV4) | |
463 | { | |
464 | /* We have to do this, since we do not re-hash for every bit -A1kmm. */ | |
465 | hv = hash_ipv4((struct sockaddr *)&addr, bits - bits % 8); | |
466 | } | |
467 | else | |
468 | { | |
469 | hv = get_mask_hash(address); | |
470 | } | |
471 | for (arec = atable[hv]; arec; arec = arec->next) | |
472 | { | |
473 | if (arec->type == type && | |
474 | arec->masktype == masktype && | |
6f3a09ff | 475 | (arec->username == NULL || username == NULL ? arec->username == username : !irccmp(arec->username, username))) |
4e0f14a0 JT |
476 | { |
477 | if (masktype == HM_HOST) | |
478 | { | |
479 | if (!irccmp(arec->Mask.hostname, address)) | |
480 | return arec->aconf; | |
481 | } | |
482 | else | |
483 | { | |
484 | if (arec->Mask.ipa.bits == bits && | |
485 | comp_with_mask_sock((struct sockaddr *)&arec->Mask.ipa.addr, (struct sockaddr *)&addr, bits)) | |
486 | return arec->aconf; | |
487 | } | |
488 | } | |
489 | } | |
490 | return NULL; | |
491 | } | |
492 | ||
212380e3 AC |
493 | /* void add_conf_by_address(const char*, int, const char *, |
494 | * struct ConfItem *aconf) | |
495 | * Input: | |
496 | * Output: None | |
497 | * Side-effects: Adds this entry to the hash table. | |
498 | */ | |
499 | void | |
40c1fd47 | 500 | add_conf_by_address(const char *address, int type, const char *username, const char *auth_user, struct ConfItem *aconf) |
212380e3 AC |
501 | { |
502 | static unsigned long prec_value = 0xFFFFFFFF; | |
503 | int masktype, bits; | |
504 | unsigned long hv; | |
505 | struct AddressRec *arec; | |
506 | ||
507 | if(address == NULL) | |
508 | address = "/NOMATCH!/"; | |
eddc2ab6 | 509 | arec = rb_malloc(sizeof(struct AddressRec)); |
212380e3 AC |
510 | masktype = parse_netmask(address, (struct sockaddr *)&arec->Mask.ipa.addr, &bits); |
511 | arec->Mask.ipa.bits = bits; | |
512 | arec->masktype = masktype; | |
ccda6e3f | 513 | #ifdef RB_IPV6 |
212380e3 AC |
514 | if(masktype == HM_IPV6) |
515 | { | |
516 | /* We have to do this, since we do not re-hash for every bit -A1kmm. */ | |
517 | bits -= bits % 16; | |
518 | arec->next = atable[(hv = hash_ipv6((struct sockaddr *)&arec->Mask.ipa.addr, bits))]; | |
519 | atable[hv] = arec; | |
520 | } | |
521 | else | |
522 | #endif | |
523 | if(masktype == HM_IPV4) | |
524 | { | |
525 | /* We have to do this, since we do not re-hash for every bit -A1kmm. */ | |
526 | bits -= bits % 8; | |
527 | arec->next = atable[(hv = hash_ipv4((struct sockaddr *)&arec->Mask.ipa.addr, bits))]; | |
528 | atable[hv] = arec; | |
529 | } | |
530 | else | |
531 | { | |
532 | arec->Mask.hostname = address; | |
533 | arec->next = atable[(hv = get_mask_hash(address))]; | |
534 | atable[hv] = arec; | |
535 | } | |
536 | arec->username = username; | |
40c1fd47 | 537 | arec->auth_user = auth_user; |
212380e3 AC |
538 | arec->aconf = aconf; |
539 | arec->precedence = prec_value--; | |
540 | arec->type = type; | |
541 | } | |
542 | ||
543 | /* void delete_one_address(const char*, struct ConfItem*) | |
544 | * Input: An address string, the associated ConfItem. | |
545 | * Output: None | |
546 | * Side effects: Deletes an address record. Frees the ConfItem if there | |
547 | * is nothing referencing it, sets it as illegal otherwise. | |
548 | */ | |
549 | void | |
550 | delete_one_address_conf(const char *address, struct ConfItem *aconf) | |
551 | { | |
552 | int masktype, bits; | |
553 | unsigned long hv; | |
554 | struct AddressRec *arec, *arecl = NULL; | |
e7046ee5 | 555 | struct rb_sockaddr_storage addr; |
212380e3 | 556 | masktype = parse_netmask(address, (struct sockaddr *)&addr, &bits); |
ccda6e3f | 557 | #ifdef RB_IPV6 |
212380e3 AC |
558 | if(masktype == HM_IPV6) |
559 | { | |
560 | /* We have to do this, since we do not re-hash for every bit -A1kmm. */ | |
561 | bits -= bits % 16; | |
562 | hv = hash_ipv6((struct sockaddr *)&addr, bits); | |
563 | } | |
564 | else | |
565 | #endif | |
566 | if(masktype == HM_IPV4) | |
567 | { | |
568 | /* We have to do this, since we do not re-hash for every bit -A1kmm. */ | |
569 | bits -= bits % 8; | |
570 | hv = hash_ipv4((struct sockaddr *)&addr, bits); | |
571 | } | |
572 | else | |
573 | hv = get_mask_hash(address); | |
574 | for (arec = atable[hv]; arec; arec = arec->next) | |
575 | { | |
576 | if(arec->aconf == aconf) | |
577 | { | |
578 | if(arecl) | |
579 | arecl->next = arec->next; | |
580 | else | |
581 | atable[hv] = arec->next; | |
582 | aconf->status |= CONF_ILLEGAL; | |
583 | if(!aconf->clients) | |
584 | free_conf(aconf); | |
637c4932 | 585 | rb_free(arec); |
212380e3 AC |
586 | return; |
587 | } | |
588 | arecl = arec; | |
589 | } | |
590 | } | |
591 | ||
592 | /* void clear_out_address_conf(void) | |
593 | * Input: None | |
594 | * Output: None | |
595 | * Side effects: Clears out all address records in the hash table, | |
596 | * frees them, and frees the ConfItems if nothing references | |
597 | * them, otherwise sets them as illegal. | |
598 | */ | |
599 | void | |
600 | clear_out_address_conf(void) | |
601 | { | |
602 | int i; | |
603 | struct AddressRec **store_next; | |
604 | struct AddressRec *arec, *arecn; | |
605 | ||
606 | for (i = 0; i < ATABLE_SIZE; i++) | |
607 | { | |
608 | store_next = &atable[i]; | |
609 | for (arec = atable[i]; arec; arec = arecn) | |
610 | { | |
611 | arecn = arec->next; | |
612 | /* We keep the temporary K-lines and destroy the | |
613 | * permanent ones, just to be confusing :) -A1kmm */ | |
614 | if(arec->aconf->flags & CONF_FLAGS_TEMPORARY || | |
615 | (arec->type != CONF_CLIENT && arec->type != CONF_EXEMPTDLINE)) | |
616 | { | |
617 | *store_next = arec; | |
618 | store_next = &arec->next; | |
619 | } | |
620 | else | |
621 | { | |
622 | arec->aconf->status |= CONF_ILLEGAL; | |
623 | if(!arec->aconf->clients) | |
624 | free_conf(arec->aconf); | |
637c4932 | 625 | rb_free(arec); |
212380e3 AC |
626 | } |
627 | } | |
628 | *store_next = NULL; | |
629 | } | |
630 | } | |
631 | ||
632 | void | |
633 | clear_out_address_conf_bans(void) | |
634 | { | |
635 | int i; | |
636 | struct AddressRec **store_next; | |
637 | struct AddressRec *arec, *arecn; | |
638 | ||
639 | for (i = 0; i < ATABLE_SIZE; i++) | |
640 | { | |
641 | store_next = &atable[i]; | |
642 | for (arec = atable[i]; arec; arec = arecn) | |
643 | { | |
644 | arecn = arec->next; | |
645 | /* We keep the temporary K-lines and destroy the | |
646 | * permanent ones, just to be confusing :) -A1kmm */ | |
647 | if(arec->aconf->flags & CONF_FLAGS_TEMPORARY || | |
648 | (arec->type == CONF_CLIENT || arec->type == CONF_EXEMPTDLINE)) | |
649 | { | |
650 | *store_next = arec; | |
651 | store_next = &arec->next; | |
652 | } | |
653 | else | |
654 | { | |
655 | arec->aconf->status |= CONF_ILLEGAL; | |
656 | if(!arec->aconf->clients) | |
657 | free_conf(arec->aconf); | |
637c4932 | 658 | rb_free(arec); |
212380e3 AC |
659 | } |
660 | } | |
661 | *store_next = NULL; | |
662 | } | |
663 | } | |
664 | ||
665 | ||
666 | /* | |
667 | * show_iline_prefix() | |
668 | * | |
669 | * inputs - pointer to struct Client requesting output | |
670 | * - pointer to struct ConfItem | |
671 | * - name to which iline prefix will be prefixed to | |
672 | * output - pointer to static string with prefixes listed in ascii form | |
673 | * side effects - NONE | |
674 | */ | |
675 | char * | |
676 | show_iline_prefix(struct Client *sptr, struct ConfItem *aconf, char *name) | |
677 | { | |
678 | static char prefix_of_host[USERLEN + 15]; | |
679 | char *prefix_ptr; | |
680 | ||
681 | prefix_ptr = prefix_of_host; | |
682 | if(IsNoTilde(aconf)) | |
683 | *prefix_ptr++ = '-'; | |
212380e3 AC |
684 | if(IsNeedIdentd(aconf)) |
685 | *prefix_ptr++ = '+'; | |
212380e3 AC |
686 | if(IsConfDoSpoofIp(aconf)) |
687 | *prefix_ptr++ = '='; | |
c4f13a64 JT |
688 | if(IsOper(sptr) && IsConfExemptFlood(aconf)) |
689 | *prefix_ptr++ = '|'; | |
c4f13a64 JT |
690 | if(IsOper(sptr) && IsConfExemptDNSBL(aconf) && !IsConfExemptKline(aconf)) |
691 | *prefix_ptr++ = '$'; | |
a63a1eab | 692 | if(IsOper(sptr) && IsConfExemptKline(aconf)) |
212380e3 | 693 | *prefix_ptr++ = '^'; |
a63a1eab | 694 | if(IsOper(sptr) && IsConfExemptLimits(aconf)) |
212380e3 | 695 | *prefix_ptr++ = '>'; |
212380e3 AC |
696 | *prefix_ptr = '\0'; |
697 | strncpy(prefix_ptr, name, USERLEN); | |
698 | return (prefix_of_host); | |
699 | } | |
700 | ||
701 | /* report_auth() | |
702 | * | |
703 | * Inputs: pointer to client to report to | |
704 | * Output: None | |
705 | * Side effects: Reports configured auth{} blocks to client_p | |
706 | */ | |
707 | void | |
708 | report_auth(struct Client *client_p) | |
709 | { | |
54e3b765 | 710 | char *name, *host, *pass, *user, *classname; |
212380e3 AC |
711 | struct AddressRec *arec; |
712 | struct ConfItem *aconf; | |
713 | int i, port; | |
714 | ||
715 | for (i = 0; i < ATABLE_SIZE; i++) | |
716 | for (arec = atable[i]; arec; arec = arec->next) | |
717 | if(arec->type == CONF_CLIENT) | |
718 | { | |
719 | aconf = arec->aconf; | |
720 | ||
a63a1eab | 721 | if(!IsOper(client_p) && IsConfDoSpoofIp(aconf)) |
212380e3 AC |
722 | continue; |
723 | ||
724 | get_printable_conf(aconf, &name, &host, &pass, &user, &port, | |
725 | &classname); | |
40c1fd47 VY |
726 | |
727 | if(!EmptyString(aconf->spasswd)) | |
728 | pass = aconf->spasswd; | |
212380e3 AC |
729 | |
730 | sendto_one_numeric(client_p, RPL_STATSILINE, | |
731 | form_str(RPL_STATSILINE), | |
40c1fd47 | 732 | name, pass, show_iline_prefix(client_p, aconf, user), |
212380e3 AC |
733 | show_ip_conf(aconf, client_p) ? host : "255.255.255.255", |
734 | port, classname); | |
735 | } | |
736 | } | |
737 | ||
738 | /* report_Klines() | |
739 | * | |
740 | * inputs - Client to report to, mask | |
741 | * outputs - | |
742 | * side effects - Reports configured K-lines to client_p. | |
743 | */ | |
744 | void | |
745 | report_Klines(struct Client *source_p) | |
746 | { | |
747 | char *host, *pass, *user, *oper_reason; | |
748 | struct AddressRec *arec; | |
749 | struct ConfItem *aconf = NULL; | |
750 | int i; | |
751 | ||
752 | for (i = 0; i < ATABLE_SIZE; i++) | |
753 | { | |
754 | for (arec = atable[i]; arec; arec = arec->next) | |
755 | { | |
756 | if(arec->type == CONF_KILL) | |
757 | { | |
758 | aconf = arec->aconf; | |
759 | ||
760 | /* its a tempkline, theyre reported elsewhere */ | |
761 | if(aconf->flags & CONF_FLAGS_TEMPORARY) | |
762 | continue; | |
763 | ||
764 | get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason); | |
765 | sendto_one_numeric(source_p, RPL_STATSKLINE, | |
766 | form_str(RPL_STATSKLINE), | |
767 | 'K', host, user, pass, | |
768 | oper_reason ? "|" : "", | |
769 | oper_reason ? oper_reason : ""); | |
770 | } | |
771 | } | |
772 | } | |
773 | } |