]>
jfr.im git - solanum.git/blob - ircd/hostmask.c
2 * charybdis: an advanced internet relay chat daemon (ircd).
3 * hostmask.c: Code to efficiently find IP & hostmask based configs.
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
8 * Copyright (C) 2005-2008 charybdis development team
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.
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.
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
27 #include "ircd_defs.h"
34 static unsigned long hash_ipv6(struct sockaddr
*, int);
35 static unsigned long hash_ipv4(struct sockaddr
*, int);
38 /* int parse_netmask(const char *, struct rb_sockaddr_storage *, int *);
39 * Input: A hostmask, or an IPV4/6 address.
40 * Output: An integer describing whether it is an IPV4, IPV6 address or a
41 * hostmask, an address(if it is an IP mask),
42 * a bitlength(if it is IP mask).
46 parse_netmask(const char *text
, struct rb_sockaddr_storage
*naddr
, int *nb
)
48 char *ip
= LOCAL_COPY(text
);
50 struct rb_sockaddr_storage
*addr
, xaddr
;
62 if(strpbrk(ip
, "*?") != NULL
)
68 if((ptr
= strchr(ip
, '/')))
79 if(rb_inet_pton_sock(ip
, (struct sockaddr
*)addr
) > 0)
86 if((ptr
= strchr(ip
, '/')))
97 if(rb_inet_pton_sock(ip
, (struct sockaddr
*)addr
) > 0)
105 /* Hashtable stuff...now external as its used in m_stats.c */
106 struct AddressRec
*atable
[ATABLE_SIZE
];
111 memset(&atable
, 0, sizeof(atable
));
114 /* unsigned long hash_ipv4(struct rb_sockaddr_storage*)
115 * Input: An IP address.
116 * Output: A hash value of the IP address.
120 hash_ipv4(struct sockaddr
*saddr
, int bits
)
122 struct sockaddr_in
*addr
= (struct sockaddr_in
*)(void *)saddr
;
126 unsigned long av
= ntohl(addr
->sin_addr
.s_addr
) & ~((1 << (32 - bits
)) - 1);
127 return (av
^ (av
>> 12) ^ (av
>> 24)) & (ATABLE_SIZE
- 1);
133 /* unsigned long hash_ipv6(struct rb_sockaddr_storage*)
134 * Input: An IP address.
135 * Output: A hash value of the IP address.
139 hash_ipv6(struct sockaddr
*saddr
, int bits
)
141 struct sockaddr_in6
*addr
= (struct sockaddr_in6
*)(void *)saddr
;
142 unsigned long v
= 0, n
;
143 for (n
= 0; n
< 16; n
++)
147 v
^= addr
->sin6_addr
.s6_addr
[n
];
152 v
^= addr
->sin6_addr
.s6_addr
[n
] & ~((1 << (8 - bits
)) - 1);
153 return v
& (ATABLE_SIZE
- 1);
156 return v
& (ATABLE_SIZE
- 1);
158 return v
& (ATABLE_SIZE
- 1);
161 /* int hash_text(const char *start)
162 * Input: The start of the text to hash.
163 * Output: The hash of the string between 1 and (TH_MAX-1)
164 * Side-effects: None.
167 hash_text(const char *start
)
169 const char *p
= start
;
174 h
= (h
<< 4) - (h
+ (unsigned char) irctolower(*p
++));
177 return (h
& (ATABLE_SIZE
- 1));
180 /* unsigned long get_hash_mask(const char *)
181 * Input: The text to hash.
182 * Output: The hash of the string right of the first '.' past the last
183 * wildcard in the string.
184 * Side-effects: None.
187 get_mask_hash(const char *text
)
189 const char *hp
= "", *p
;
191 for (p
= text
+ strlen(text
) - 1; p
>= text
; p
--)
192 if(*p
== '*' || *p
== '?')
193 return hash_text(hp
);
196 return hash_text(text
);
199 /* struct ConfItem* find_conf_by_address(const char*, struct rb_sockaddr_storage*,
200 * int type, int fam, const char *username)
201 * Input: The hostname, the address, the type of mask to find, the address
202 * family, the username.
203 * Output: The matching value with the highest precedence.
205 * Note: Setting bit 0 of the type means that the username is ignored.
208 find_conf_by_address(const char *name
, const char *sockhost
,
209 const char *orighost
,
210 struct sockaddr
*addr
, int type
, int fam
,
211 const char *username
, const char *auth_user
)
213 unsigned long hprecv
= 0;
214 struct ConfItem
*hprec
= NULL
;
215 struct AddressRec
*arec
;
223 /* Check for IPV6 matches... */
227 for (b
= 128; b
>= 0; b
-= 16)
229 for (arec
= atable
[hash_ipv6(addr
, b
)]; arec
; arec
= arec
->next
)
230 if(arec
->type
== (type
& ~0x1) &&
231 arec
->masktype
== HM_IPV6
&&
232 comp_with_mask_sock(addr
, (struct sockaddr
*)&arec
->Mask
.ipa
.addr
,
233 arec
->Mask
.ipa
.bits
) &&
234 (type
& 0x1 || match(arec
-> username
, username
)) &&
235 (type
!= CONF_CLIENT
|| !arec
->auth_user
||
236 (auth_user
&& match(arec
->auth_user
, auth_user
))) &&
237 arec
->precedence
> hprecv
)
239 hprecv
= arec
->precedence
;
247 for (b
= 32; b
>= 0; b
-= 8)
249 for (arec
= atable
[hash_ipv4(addr
, b
)]; arec
; arec
= arec
->next
)
250 if(arec
->type
== (type
& ~0x1) &&
251 arec
->masktype
== HM_IPV4
&&
252 comp_with_mask_sock(addr
, (struct sockaddr
*)&arec
->Mask
.ipa
.addr
,
253 arec
->Mask
.ipa
.bits
) &&
254 (type
& 0x1 || match(arec
->username
, username
)) &&
255 (type
!= CONF_CLIENT
|| !arec
->auth_user
||
256 (auth_user
&& match(arec
->auth_user
, auth_user
))) &&
257 arec
->precedence
> hprecv
)
259 hprecv
= arec
->precedence
;
270 for (p
= orighost
; p
!= NULL
;)
272 for (arec
= atable
[hash_text(p
)]; arec
; arec
= arec
->next
)
274 if((arec
->type
== (type
& ~0x1)) &&
275 (arec
->masktype
== HM_HOST
) &&
276 arec
->precedence
> hprecv
&&
277 match(arec
->Mask
.hostname
, orighost
) &&
278 (type
!= CONF_CLIENT
|| !arec
->auth_user
||
279 (auth_user
&& match(arec
->auth_user
, auth_user
))) &&
280 (type
& 0x1 || match(arec
->username
, username
)))
282 hprecv
= arec
->precedence
;
291 for (arec
= atable
[0]; arec
; arec
= arec
->next
)
293 if(arec
->type
== (type
& ~0x1) &&
294 arec
->masktype
== HM_HOST
&&
295 arec
->precedence
> hprecv
&&
296 (match(arec
->Mask
.hostname
, orighost
) ||
297 (sockhost
&& match(arec
->Mask
.hostname
, sockhost
))) &&
298 (type
!= CONF_CLIENT
|| !arec
->auth_user
||
299 (auth_user
&& match(arec
->auth_user
, auth_user
))) &&
300 (type
& 0x1 || match(arec
->username
, username
)))
302 hprecv
= arec
->precedence
;
311 /* And yes - we have to check p after strchr and p after increment for
313 for (p
= name
; p
!= NULL
;)
315 for (arec
= atable
[hash_text(p
)]; arec
; arec
= arec
->next
)
316 if((arec
->type
== (type
& ~0x1)) &&
317 (arec
->masktype
== HM_HOST
) &&
318 arec
->precedence
> hprecv
&&
319 match(arec
->Mask
.hostname
, name
) &&
320 (type
!= CONF_CLIENT
|| !arec
->auth_user
||
321 (auth_user
&& match(arec
->auth_user
, auth_user
))) &&
322 (type
& 0x1 || match(arec
->username
, username
)))
324 hprecv
= arec
->precedence
;
333 for (arec
= atable
[0]; arec
; arec
= arec
->next
)
335 if(arec
->type
== (type
& ~0x1) &&
336 arec
->masktype
== HM_HOST
&&
337 arec
->precedence
> hprecv
&&
338 (match(arec
->Mask
.hostname
, name
) ||
339 (sockhost
&& match(arec
->Mask
.hostname
, sockhost
))) &&
340 (type
!= CONF_CLIENT
|| !arec
->auth_user
||
341 (auth_user
&& match(arec
->auth_user
, auth_user
))) &&
342 (type
& 0x1 || match(arec
->username
, username
)))
344 hprecv
= arec
->precedence
;
352 /* struct ConfItem* find_address_conf(const char*, const char*,
353 * struct rb_sockaddr_storage*, int);
354 * Input: The hostname, username, address, address family.
355 * Output: The applicable ConfItem.
359 find_address_conf(const char *host
, const char *sockhost
, const char *user
,
360 const char *notildeuser
, struct sockaddr
*ip
, int aftype
, char *auth_user
)
362 struct ConfItem
*iconf
, *kconf
;
364 struct sockaddr_in ip4
;
366 /* Find the best I-line... If none, return NULL -A1kmm */
367 if(!(iconf
= find_conf_by_address(host
, sockhost
, NULL
, ip
, CONF_CLIENT
, aftype
, user
, auth_user
)))
369 /* Find what their visible username will be.
370 * Note that the username without tilde may contain one char more.
372 vuser
= IsNoTilde(iconf
) ? notildeuser
: user
;
374 /* If they are exempt from K-lines, return the best I-line. -A1kmm */
375 if(IsConfExemptKline(iconf
))
378 /* Find the best K-line... -A1kmm */
379 kconf
= find_conf_by_address(host
, sockhost
, NULL
, ip
, CONF_KILL
, aftype
, user
, NULL
);
381 /* If they are K-lined, return the K-line */
385 /* if theres a spoof, check it against klines.. */
386 if(IsConfDoSpoofIp(iconf
))
388 char *p
= strchr(iconf
->info
.name
, '@');
390 /* note, we dont need to pass sockhost here, as its
391 * guaranteed to not match by whats above.. --anfl
396 kconf
= find_conf_by_address(p
+1, NULL
, NULL
, ip
, CONF_KILL
, aftype
, iconf
->info
.name
, NULL
);
400 kconf
= find_conf_by_address(iconf
->info
.name
, NULL
, NULL
, ip
, CONF_KILL
, aftype
, vuser
, NULL
);
406 /* if no_tilde, check the username without tilde against klines too
410 kconf
= find_conf_by_address(host
, sockhost
, NULL
, ip
, CONF_KILL
, aftype
, vuser
, NULL
);
415 if(ip
!= NULL
&& ip
->sa_family
== AF_INET6
&&
416 rb_ipv4_from_ipv6((const struct sockaddr_in6
*)(const void *)ip
, &ip4
))
418 kconf
= find_conf_by_address(NULL
, NULL
, NULL
, (struct sockaddr
*)&ip4
, CONF_KILL
, AF_INET
, vuser
, NULL
);
426 /* struct ConfItem* find_dline(struct rb_sockaddr_storage*, int)
427 * Input: An address, an address family.
428 * Output: The best matching D-line or exempt line.
429 * Side effects: None.
432 find_dline(struct sockaddr
*addr
, int aftype
)
434 struct ConfItem
*aconf
;
435 struct sockaddr_in addr2
;
437 aconf
= find_conf_by_address(NULL
, NULL
, NULL
, addr
, CONF_EXEMPTDLINE
| 1, aftype
, NULL
, NULL
);
440 aconf
= find_conf_by_address(NULL
, NULL
, NULL
, addr
, CONF_DLINE
| 1, aftype
, NULL
, NULL
);
443 if(addr
->sa_family
== AF_INET6
&&
444 rb_ipv4_from_ipv6((const struct sockaddr_in6
*)(const void *)addr
, &addr2
))
446 aconf
= find_conf_by_address(NULL
, NULL
, NULL
, (struct sockaddr
*)&addr2
, CONF_DLINE
| 1, AF_INET
, NULL
, NULL
);
453 /* void find_exact_conf_by_address(const char*, int, const char *)
455 * Output: ConfItem if found
459 find_exact_conf_by_address(const char *address
, int type
, const char *username
)
463 struct AddressRec
*arec
;
464 struct rb_sockaddr_storage addr
;
467 address
= "/NOMATCH!/";
468 masktype
= parse_netmask(address
, &addr
, &bits
);
469 if(masktype
== HM_IPV6
)
471 /* We have to do this, since we do not re-hash for every bit -A1kmm. */
472 hv
= hash_ipv6((struct sockaddr
*)&addr
, bits
- bits
% 16);
474 else if(masktype
== HM_IPV4
)
476 /* We have to do this, since we do not re-hash for every bit -A1kmm. */
477 hv
= hash_ipv4((struct sockaddr
*)&addr
, bits
- bits
% 8);
481 hv
= get_mask_hash(address
);
483 for (arec
= atable
[hv
]; arec
; arec
= arec
->next
)
485 if (arec
->type
== type
&&
486 arec
->masktype
== masktype
&&
487 (arec
->username
== NULL
|| username
== NULL
? arec
->username
== username
: !irccmp(arec
->username
, username
)))
489 if (masktype
== HM_HOST
)
491 if (!irccmp(arec
->Mask
.hostname
, address
))
496 if (arec
->Mask
.ipa
.bits
== bits
&&
497 comp_with_mask_sock((struct sockaddr
*)&arec
->Mask
.ipa
.addr
, (struct sockaddr
*)&addr
, bits
))
505 /* void add_conf_by_address(const char*, int, const char *,
506 * struct ConfItem *aconf)
509 * Side-effects: Adds this entry to the hash table.
512 add_conf_by_address(const char *address
, int type
, const char *username
, const char *auth_user
, struct ConfItem
*aconf
)
514 static unsigned long prec_value
= 0xFFFFFFFF;
517 struct AddressRec
*arec
;
520 address
= "/NOMATCH!/";
521 arec
= rb_malloc(sizeof(struct AddressRec
));
522 arec
->masktype
= parse_netmask(address
, &arec
->Mask
.ipa
.addr
, &bits
);
523 if(arec
->masktype
== HM_IPV6
)
525 arec
->Mask
.ipa
.bits
= bits
;
526 /* We have to do this, since we do not re-hash for every bit -A1kmm. */
528 arec
->next
= atable
[(hv
= hash_ipv6((struct sockaddr
*)&arec
->Mask
.ipa
.addr
, bits
))];
531 else if(arec
->masktype
== HM_IPV4
)
533 arec
->Mask
.ipa
.bits
= bits
;
534 /* We have to do this, since we do not re-hash for every bit -A1kmm. */
536 arec
->next
= atable
[(hv
= hash_ipv4((struct sockaddr
*)&arec
->Mask
.ipa
.addr
, bits
))];
541 arec
->Mask
.hostname
= address
;
542 arec
->next
= atable
[(hv
= get_mask_hash(address
))];
545 arec
->username
= username
;
546 arec
->auth_user
= auth_user
;
548 arec
->precedence
= prec_value
--;
552 /* void delete_one_address(const char*, struct ConfItem*)
553 * Input: An address string, the associated ConfItem.
555 * Side effects: Deletes an address record. Frees the ConfItem if there
556 * is nothing referencing it, sets it as illegal otherwise.
559 delete_one_address_conf(const char *address
, struct ConfItem
*aconf
)
563 struct AddressRec
*arec
, *arecl
= NULL
;
564 struct rb_sockaddr_storage addr
;
565 masktype
= parse_netmask(address
, &addr
, &bits
);
566 if(masktype
== HM_IPV6
)
568 /* We have to do this, since we do not re-hash for every bit -A1kmm. */
570 hv
= hash_ipv6((struct sockaddr
*)&addr
, bits
);
572 else if(masktype
== HM_IPV4
)
574 /* We have to do this, since we do not re-hash for every bit -A1kmm. */
576 hv
= hash_ipv4((struct sockaddr
*)&addr
, bits
);
579 hv
= get_mask_hash(address
);
580 for (arec
= atable
[hv
]; arec
; arec
= arec
->next
)
582 if(arec
->aconf
== aconf
)
585 arecl
->next
= arec
->next
;
587 atable
[hv
] = arec
->next
;
588 aconf
->status
|= CONF_ILLEGAL
;
598 /* void clear_out_address_conf(void)
601 * Side effects: Clears out all address records in the hash table,
602 * frees them, and frees the ConfItems if nothing references
603 * them, otherwise sets them as illegal.
606 clear_out_address_conf(void)
609 struct AddressRec
**store_next
;
610 struct AddressRec
*arec
, *arecn
;
612 for (i
= 0; i
< ATABLE_SIZE
; i
++)
614 store_next
= &atable
[i
];
615 for (arec
= atable
[i
]; arec
; arec
= arecn
)
618 /* We keep the temporary K-lines and destroy the
619 * permanent ones, just to be confusing :) -A1kmm */
620 if(arec
->aconf
->flags
& CONF_FLAGS_TEMPORARY
||
621 (arec
->type
!= CONF_CLIENT
&& arec
->type
!= CONF_EXEMPTDLINE
))
624 store_next
= &arec
->next
;
628 arec
->aconf
->status
|= CONF_ILLEGAL
;
629 if(!arec
->aconf
->clients
)
630 free_conf(arec
->aconf
);
639 clear_out_address_conf_bans(void)
642 struct AddressRec
**store_next
;
643 struct AddressRec
*arec
, *arecn
;
645 for (i
= 0; i
< ATABLE_SIZE
; i
++)
647 store_next
= &atable
[i
];
648 for (arec
= atable
[i
]; arec
; arec
= arecn
)
651 /* We keep the temporary K-lines and destroy the
652 * permanent ones, just to be confusing :) -A1kmm */
653 if(arec
->aconf
->flags
& CONF_FLAGS_TEMPORARY
||
654 (arec
->type
== CONF_CLIENT
|| arec
->type
== CONF_EXEMPTDLINE
))
657 store_next
= &arec
->next
;
661 arec
->aconf
->status
|= CONF_ILLEGAL
;
662 if(!arec
->aconf
->clients
)
663 free_conf(arec
->aconf
);
673 * show_iline_prefix()
675 * inputs - pointer to struct Client requesting output
676 * - pointer to struct ConfItem
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
682 show_iline_prefix(struct Client
*sptr
, struct ConfItem
*aconf
, char *name
)
684 static char prefix_of_host
[USERLEN
+ 15];
687 prefix_ptr
= prefix_of_host
;
690 if(IsNeedIdentd(aconf
))
692 if(IsConfDoSpoofIp(aconf
))
694 if(IsOper(sptr
) && IsConfExemptFlood(aconf
))
696 if(IsOper(sptr
) && IsConfExemptDNSBL(aconf
) && !IsConfExemptKline(aconf
))
698 if(IsOper(sptr
) && IsConfExemptKline(aconf
))
700 if(IsOper(sptr
) && IsConfExemptLimits(aconf
))
703 strncpy(prefix_ptr
, name
, USERLEN
);
704 return (prefix_of_host
);
709 * Inputs: pointer to client to report to
711 * Side effects: Reports configured auth{} blocks to client_p
714 report_auth(struct Client
*client_p
)
716 char *name
, *host
, *user
, *classname
;
718 struct AddressRec
*arec
;
719 struct ConfItem
*aconf
;
722 for (i
= 0; i
< ATABLE_SIZE
; i
++)
723 for (arec
= atable
[i
]; arec
; arec
= arec
->next
)
724 if(arec
->type
== CONF_CLIENT
)
728 if(!IsOper(client_p
) && IsConfDoSpoofIp(aconf
))
731 get_printable_conf(aconf
, &name
, &host
, &pass
, &user
, &port
,
734 if(!EmptyString(aconf
->spasswd
))
735 pass
= aconf
->spasswd
;
737 sendto_one_numeric(client_p
, RPL_STATSILINE
,
738 form_str(RPL_STATSILINE
),
739 name
, pass
, show_iline_prefix(client_p
, aconf
, user
),
740 show_ip_conf(aconf
, client_p
) ? host
: "255.255.255.255",