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