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