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