]>
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"
35 static unsigned long hash_ipv6(struct sockaddr
*, int);
37 static unsigned long hash_ipv4(struct sockaddr
*, int);
40 /* int parse_netmask(const char *, struct rb_sockaddr_storage *, int *);
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).
48 parse_netmask(const char *text
, struct rb_sockaddr_storage
*naddr
, int *nb
)
50 char *ip
= LOCAL_COPY(text
);
52 struct rb_sockaddr_storage
*addr
, xaddr
;
64 if(strpbrk(ip
, "*?") != NULL
)
71 if((ptr
= strchr(ip
, '/')))
82 if(rb_inet_pton_sock(ip
, (struct sockaddr
*)addr
) > 0)
90 if((ptr
= strchr(ip
, '/')))
101 if(rb_inet_pton_sock(ip
, (struct sockaddr
*)addr
) > 0)
109 /* Hashtable stuff...now external as its used in m_stats.c */
110 struct AddressRec
*atable
[ATABLE_SIZE
];
115 memset(&atable
, 0, sizeof(atable
));
118 /* unsigned long hash_ipv4(struct rb_sockaddr_storage*)
119 * Input: An IP address.
120 * Output: A hash value of the IP address.
124 hash_ipv4(struct sockaddr
*saddr
, int bits
)
126 struct sockaddr_in
*addr
= (struct sockaddr_in
*)(void *)saddr
;
130 unsigned long av
= ntohl(addr
->sin_addr
.s_addr
) & ~((1 << (32 - bits
)) - 1);
131 return (av
^ (av
>> 12) ^ (av
>> 24)) & (ATABLE_SIZE
- 1);
137 /* unsigned long hash_ipv6(struct rb_sockaddr_storage*)
138 * Input: An IP address.
139 * Output: A hash value of the IP address.
144 hash_ipv6(struct sockaddr
*saddr
, int bits
)
146 struct sockaddr_in6
*addr
= (struct sockaddr_in6
*)(void *)saddr
;
147 unsigned long v
= 0, n
;
148 for (n
= 0; n
< 16; n
++)
152 v
^= addr
->sin6_addr
.s6_addr
[n
];
157 v
^= addr
->sin6_addr
.s6_addr
[n
] & ~((1 << (8 - bits
)) - 1);
158 return v
& (ATABLE_SIZE
- 1);
161 return v
& (ATABLE_SIZE
- 1);
163 return v
& (ATABLE_SIZE
- 1);
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.
173 hash_text(const char *start
)
175 const char *p
= start
;
180 h
= (h
<< 4) - (h
+ (unsigned char) irctolower(*p
++));
183 return (h
& (ATABLE_SIZE
- 1));
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.
193 get_mask_hash(const char *text
)
195 const char *hp
= "", *p
;
197 for (p
= text
+ strlen(text
) - 1; p
>= text
; p
--)
198 if(*p
== '*' || *p
== '?')
199 return hash_text(hp
);
202 return hash_text(text
);
205 /* struct ConfItem* find_conf_by_address(const char*, struct rb_sockaddr_storage*,
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.
211 * Note: Setting bit 0 of the type means that the username is ignored.
214 find_conf_by_address(const char *name
, const char *sockhost
,
215 const char *orighost
,
216 struct sockaddr
*addr
, int type
, int fam
,
217 const char *username
, const char *auth_user
)
219 unsigned long hprecv
= 0;
220 struct ConfItem
*hprec
= NULL
;
221 struct AddressRec
*arec
;
229 /* Check for IPV6 matches... */
234 for (b
= 128; b
>= 0; b
-= 16)
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
,
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
)
246 hprecv
= arec
->precedence
;
255 for (b
= 32; b
>= 0; b
-= 8)
257 for (arec
= atable
[hash_ipv4(addr
, b
)]; arec
; arec
= arec
->next
)
258 if(arec
->type
== (type
& ~0x1) &&
259 arec
->masktype
== HM_IPV4
&&
260 comp_with_mask_sock(addr
, (struct sockaddr
*)&arec
->Mask
.ipa
.addr
,
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
)
267 hprecv
= arec
->precedence
;
278 for (p
= orighost
; p
!= NULL
;)
280 for (arec
= atable
[hash_text(p
)]; arec
; arec
= arec
->next
)
282 if((arec
->type
== (type
& ~0x1)) &&
283 (arec
->masktype
== HM_HOST
) &&
284 arec
->precedence
> hprecv
&&
285 match(arec
->Mask
.hostname
, orighost
) &&
286 (type
!= CONF_CLIENT
|| !arec
->auth_user
||
287 (auth_user
&& match(arec
->auth_user
, auth_user
))) &&
288 (type
& 0x1 || match(arec
->username
, username
)))
290 hprecv
= arec
->precedence
;
299 for (arec
= atable
[0]; arec
; arec
= arec
->next
)
301 if(arec
->type
== (type
& ~0x1) &&
302 arec
->masktype
== HM_HOST
&&
303 arec
->precedence
> hprecv
&&
304 (match(arec
->Mask
.hostname
, orighost
) ||
305 (sockhost
&& match(arec
->Mask
.hostname
, sockhost
))) &&
306 (type
!= CONF_CLIENT
|| !arec
->auth_user
||
307 (auth_user
&& match(arec
->auth_user
, auth_user
))) &&
308 (type
& 0x1 || match(arec
->username
, username
)))
310 hprecv
= arec
->precedence
;
319 /* And yes - we have to check p after strchr and p after increment for
321 for (p
= name
; p
!= NULL
;)
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
) &&
328 (type
!= CONF_CLIENT
|| !arec
->auth_user
||
329 (auth_user
&& match(arec
->auth_user
, auth_user
))) &&
330 (type
& 0x1 || match(arec
->username
, username
)))
332 hprecv
= arec
->precedence
;
341 for (arec
= atable
[0]; arec
; arec
= arec
->next
)
343 if(arec
->type
== (type
& ~0x1) &&
344 arec
->masktype
== HM_HOST
&&
345 arec
->precedence
> hprecv
&&
346 (match(arec
->Mask
.hostname
, name
) ||
347 (sockhost
&& match(arec
->Mask
.hostname
, sockhost
))) &&
348 (type
!= CONF_CLIENT
|| !arec
->auth_user
||
349 (auth_user
&& match(arec
->auth_user
, auth_user
))) &&
350 (type
& 0x1 || match(arec
->username
, username
)))
352 hprecv
= arec
->precedence
;
360 /* struct ConfItem* find_address_conf(const char*, const char*,
361 * struct rb_sockaddr_storage*, int);
362 * Input: The hostname, username, address, address family.
363 * Output: The applicable ConfItem.
367 find_address_conf(const char *host
, const char *sockhost
, const char *user
,
368 const char *notildeuser
, struct sockaddr
*ip
, int aftype
, char *auth_user
)
370 struct ConfItem
*iconf
, *kconf
;
373 struct sockaddr_in ip4
;
376 /* Find the best I-line... If none, return NULL -A1kmm */
377 if(!(iconf
= find_conf_by_address(host
, sockhost
, NULL
, ip
, CONF_CLIENT
, aftype
, user
, auth_user
)))
379 /* Find what their visible username will be.
380 * Note that the username without tilde may contain one char more.
382 vuser
= IsNoTilde(iconf
) ? notildeuser
: user
;
384 /* If they are exempt from K-lines, return the best I-line. -A1kmm */
385 if(IsConfExemptKline(iconf
))
388 /* Find the best K-line... -A1kmm */
389 kconf
= find_conf_by_address(host
, sockhost
, NULL
, ip
, CONF_KILL
, aftype
, user
, NULL
);
391 /* If they are K-lined, return the K-line */
395 /* if theres a spoof, check it against klines.. */
396 if(IsConfDoSpoofIp(iconf
))
398 char *p
= strchr(iconf
->info
.name
, '@');
400 /* note, we dont need to pass sockhost here, as its
401 * guaranteed to not match by whats above.. --anfl
406 kconf
= find_conf_by_address(p
+1, NULL
, NULL
, ip
, CONF_KILL
, aftype
, iconf
->info
.name
, NULL
);
410 kconf
= find_conf_by_address(iconf
->info
.name
, NULL
, NULL
, ip
, CONF_KILL
, aftype
, vuser
, NULL
);
416 /* if no_tilde, check the username without tilde against klines too
420 kconf
= find_conf_by_address(host
, sockhost
, NULL
, ip
, CONF_KILL
, aftype
, vuser
, NULL
);
426 if(ip
!= NULL
&& ip
->sa_family
== AF_INET6
&&
427 rb_ipv4_from_ipv6((const struct sockaddr_in6
*)(const void *)ip
, &ip4
))
429 kconf
= find_conf_by_address(NULL
, NULL
, NULL
, (struct sockaddr
*)&ip4
, CONF_KILL
, AF_INET
, vuser
, NULL
);
438 /* struct ConfItem* find_dline(struct rb_sockaddr_storage*, int)
439 * Input: An address, an address family.
440 * Output: The best matching D-line or exempt line.
441 * Side effects: None.
444 find_dline(struct sockaddr
*addr
, int aftype
)
446 struct ConfItem
*aconf
;
448 struct sockaddr_in addr2
;
451 aconf
= find_conf_by_address(NULL
, NULL
, NULL
, addr
, CONF_EXEMPTDLINE
| 1, aftype
, NULL
, NULL
);
454 aconf
= find_conf_by_address(NULL
, NULL
, NULL
, addr
, CONF_DLINE
| 1, aftype
, NULL
, NULL
);
458 if(addr
->sa_family
== AF_INET6
&&
459 rb_ipv4_from_ipv6((const struct sockaddr_in6
*)(const void *)addr
, &addr2
))
461 aconf
= find_conf_by_address(NULL
, NULL
, NULL
, (struct sockaddr
*)&addr2
, CONF_DLINE
| 1, AF_INET
, NULL
, NULL
);
469 /* void find_exact_conf_by_address(const char*, int, const char *)
471 * Output: ConfItem if found
475 find_exact_conf_by_address(const char *address
, int type
, const char *username
)
479 struct AddressRec
*arec
;
480 struct rb_sockaddr_storage addr
;
483 address
= "/NOMATCH!/";
484 masktype
= parse_netmask(address
, &addr
, &bits
);
486 if(masktype
== HM_IPV6
)
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);
493 if(masktype
== HM_IPV4
)
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);
500 hv
= get_mask_hash(address
);
502 for (arec
= atable
[hv
]; arec
; arec
= arec
->next
)
504 if (arec
->type
== type
&&
505 arec
->masktype
== masktype
&&
506 (arec
->username
== NULL
|| username
== NULL
? arec
->username
== username
: !irccmp(arec
->username
, username
)))
508 if (masktype
== HM_HOST
)
510 if (!irccmp(arec
->Mask
.hostname
, address
))
515 if (arec
->Mask
.ipa
.bits
== bits
&&
516 comp_with_mask_sock((struct sockaddr
*)&arec
->Mask
.ipa
.addr
, (struct sockaddr
*)&addr
, bits
))
524 /* void add_conf_by_address(const char*, int, const char *,
525 * struct ConfItem *aconf)
528 * Side-effects: Adds this entry to the hash table.
531 add_conf_by_address(const char *address
, int type
, const char *username
, const char *auth_user
, struct ConfItem
*aconf
)
533 static unsigned long prec_value
= 0xFFFFFFFF;
536 struct AddressRec
*arec
;
539 address
= "/NOMATCH!/";
540 arec
= rb_malloc(sizeof(struct AddressRec
));
541 masktype
= parse_netmask(address
, &arec
->Mask
.ipa
.addr
, &bits
);
542 arec
->Mask
.ipa
.bits
= bits
;
543 arec
->masktype
= masktype
;
545 if(masktype
== HM_IPV6
)
547 /* We have to do this, since we do not re-hash for every bit -A1kmm. */
549 arec
->next
= atable
[(hv
= hash_ipv6((struct sockaddr
*)&arec
->Mask
.ipa
.addr
, bits
))];
554 if(masktype
== HM_IPV4
)
556 /* We have to do this, since we do not re-hash for every bit -A1kmm. */
558 arec
->next
= atable
[(hv
= hash_ipv4((struct sockaddr
*)&arec
->Mask
.ipa
.addr
, bits
))];
563 arec
->Mask
.hostname
= address
;
564 arec
->next
= atable
[(hv
= get_mask_hash(address
))];
567 arec
->username
= username
;
568 arec
->auth_user
= auth_user
;
570 arec
->precedence
= prec_value
--;
574 /* void delete_one_address(const char*, struct ConfItem*)
575 * Input: An address string, the associated ConfItem.
577 * Side effects: Deletes an address record. Frees the ConfItem if there
578 * is nothing referencing it, sets it as illegal otherwise.
581 delete_one_address_conf(const char *address
, struct ConfItem
*aconf
)
585 struct AddressRec
*arec
, *arecl
= NULL
;
586 struct rb_sockaddr_storage addr
;
587 masktype
= parse_netmask(address
, &addr
, &bits
);
589 if(masktype
== HM_IPV6
)
591 /* We have to do this, since we do not re-hash for every bit -A1kmm. */
593 hv
= hash_ipv6((struct sockaddr
*)&addr
, bits
);
597 if(masktype
== HM_IPV4
)
599 /* We have to do this, since we do not re-hash for every bit -A1kmm. */
601 hv
= hash_ipv4((struct sockaddr
*)&addr
, bits
);
604 hv
= get_mask_hash(address
);
605 for (arec
= atable
[hv
]; arec
; arec
= arec
->next
)
607 if(arec
->aconf
== aconf
)
610 arecl
->next
= arec
->next
;
612 atable
[hv
] = arec
->next
;
613 aconf
->status
|= CONF_ILLEGAL
;
623 /* void clear_out_address_conf(void)
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.
631 clear_out_address_conf(void)
634 struct AddressRec
**store_next
;
635 struct AddressRec
*arec
, *arecn
;
637 for (i
= 0; i
< ATABLE_SIZE
; i
++)
639 store_next
= &atable
[i
];
640 for (arec
= atable
[i
]; arec
; arec
= arecn
)
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
))
649 store_next
= &arec
->next
;
653 arec
->aconf
->status
|= CONF_ILLEGAL
;
654 if(!arec
->aconf
->clients
)
655 free_conf(arec
->aconf
);
664 clear_out_address_conf_bans(void)
667 struct AddressRec
**store_next
;
668 struct AddressRec
*arec
, *arecn
;
670 for (i
= 0; i
< ATABLE_SIZE
; i
++)
672 store_next
= &atable
[i
];
673 for (arec
= atable
[i
]; arec
; arec
= arecn
)
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
))
682 store_next
= &arec
->next
;
686 arec
->aconf
->status
|= CONF_ILLEGAL
;
687 if(!arec
->aconf
->clients
)
688 free_conf(arec
->aconf
);
698 * show_iline_prefix()
700 * inputs - pointer to struct Client requesting output
701 * - pointer to struct ConfItem
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
707 show_iline_prefix(struct Client
*sptr
, struct ConfItem
*aconf
, char *name
)
709 static char prefix_of_host
[USERLEN
+ 15];
712 prefix_ptr
= prefix_of_host
;
715 if(IsNeedIdentd(aconf
))
717 if(IsConfDoSpoofIp(aconf
))
719 if(IsOper(sptr
) && IsConfExemptFlood(aconf
))
721 if(IsOper(sptr
) && IsConfExemptDNSBL(aconf
) && !IsConfExemptKline(aconf
))
723 if(IsOper(sptr
) && IsConfExemptKline(aconf
))
725 if(IsOper(sptr
) && IsConfExemptLimits(aconf
))
728 strncpy(prefix_ptr
, name
, USERLEN
);
729 return (prefix_of_host
);
734 * Inputs: pointer to client to report to
736 * Side effects: Reports configured auth{} blocks to client_p
739 report_auth(struct Client
*client_p
)
741 char *name
, *host
, *user
, *classname
;
743 struct AddressRec
*arec
;
744 struct ConfItem
*aconf
;
747 for (i
= 0; i
< ATABLE_SIZE
; i
++)
748 for (arec
= atable
[i
]; arec
; arec
= arec
->next
)
749 if(arec
->type
== CONF_CLIENT
)
753 if(!IsOper(client_p
) && IsConfDoSpoofIp(aconf
))
756 get_printable_conf(aconf
, &name
, &host
, &pass
, &user
, &port
,
759 if(!EmptyString(aconf
->spasswd
))
760 pass
= aconf
->spasswd
;
762 sendto_one_numeric(client_p
, RPL_STATSILINE
,
763 form_str(RPL_STATSILINE
),
764 name
, pass
, show_iline_prefix(client_p
, aconf
, user
),
765 show_ip_conf(aconf
, client_p
) ? host
: "255.255.255.255",