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