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