]>
jfr.im git - solanum.git/blob - src/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
25 * $Id: hostmask.c 2757 2006-11-10 22:58:15Z jilles $
29 #include "ircd_defs.h"
34 #include "irc_string.h"
37 static unsigned long hash_ipv6(struct sockaddr
*, int);
39 static unsigned long hash_ipv4(struct sockaddr
*, int);
42 /* int parse_netmask(const char *, struct rb_sockaddr_storage *, int *);
43 * Input: A hostmask, or an IPV4/6 address.
44 * Output: An integer describing whether it is an IPV4, IPV6 address or a
45 * hostmask, an address(if it is an IP mask),
46 * a bitlength(if it is IP mask).
50 parse_netmask(const char *text
, struct sockaddr
*naddr
, int *nb
)
52 char *ip
= LOCAL_COPY(text
);
54 struct rb_sockaddr_storage
*addr
, xaddr
;
62 addr
= (struct rb_sockaddr_storage
*)&xaddr
;
64 addr
= (struct rb_sockaddr_storage
*)naddr
;
69 if((ptr
= strchr(ip
, '/')))
78 if(inetpton_sock(ip
, (struct sockaddr
*)addr
) > 0)
86 if((ptr
= strchr(ip
, '/')))
95 if(inetpton_sock(ip
, (struct sockaddr
*)addr
) > 0)
103 /* Hashtable stuff...now external as its used in m_stats.c */
104 struct AddressRec
*atable
[ATABLE_SIZE
];
109 memset(&atable
, 0, sizeof(atable
));
112 /* unsigned long hash_ipv4(struct rb_sockaddr_storage*)
113 * Input: An IP address.
114 * Output: A hash value of the IP address.
118 hash_ipv4(struct sockaddr
*saddr
, int bits
)
120 struct sockaddr_in
*addr
= (struct sockaddr_in
*) saddr
;
124 unsigned long av
= ntohl(addr
->sin_addr
.s_addr
) & ~((1 << (32 - bits
)) - 1);
125 return (av
^ (av
>> 12) ^ (av
>> 24)) & (ATABLE_SIZE
- 1);
131 /* unsigned long hash_ipv6(struct rb_sockaddr_storage*)
132 * Input: An IP address.
133 * Output: A hash value of the IP address.
138 hash_ipv6(struct sockaddr
*saddr
, int bits
)
140 struct sockaddr_in6
*addr
= (struct sockaddr_in6
*) saddr
;
141 unsigned long v
= 0, n
;
142 for (n
= 0; n
< 16; n
++)
146 v
^= addr
->sin6_addr
.s6_addr
[n
];
151 v
^= addr
->sin6_addr
.s6_addr
[n
] & ~((1 << (8 - bits
)) - 1);
152 return v
& (ATABLE_SIZE
- 1);
155 return v
& (ATABLE_SIZE
- 1);
157 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) ToLower(*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
)
213 unsigned long hprecv
= 0;
214 struct ConfItem
*hprec
= NULL
;
215 struct AddressRec
*arec
;
223 /* Check for IPV6 matches... */
228 for (b
= 128; b
>= 0; b
-= 16)
230 for (arec
= atable
[hash_ipv6(addr
, b
)]; arec
; arec
= arec
->next
)
231 if(arec
->type
== (type
& ~0x1) &&
232 arec
->masktype
== HM_IPV6
&&
233 comp_with_mask_sock(addr
, (struct sockaddr
*)&arec
->Mask
.ipa
.addr
,
234 arec
->Mask
.ipa
.bits
) && (type
& 0x1
239 && arec
->precedence
> hprecv
)
241 hprecv
= arec
->precedence
;
250 for (b
= 32; b
>= 0; b
-= 8)
252 for (arec
= atable
[hash_ipv4(addr
, b
)]; arec
; arec
= arec
->next
)
253 if(arec
->type
== (type
& ~0x1) &&
254 arec
->masktype
== HM_IPV4
&&
255 arec
->precedence
> hprecv
&&
256 comp_with_mask_sock(addr
, (struct sockaddr
*)&arec
->Mask
.ipa
.addr
,
257 arec
->Mask
.ipa
.bits
) &&
258 (type
& 0x1 || match(arec
->username
, username
)))
260 hprecv
= arec
->precedence
;
271 for (p
= orighost
; p
!= NULL
;)
273 for (arec
= atable
[hash_text(p
)]; arec
; arec
= arec
->next
)
275 if((arec
->type
== (type
& ~0x1)) &&
276 (arec
->masktype
== HM_HOST
) &&
277 arec
->precedence
> hprecv
&&
278 match(arec
->Mask
.hostname
, orighost
) &&
279 (type
& 0x1 || match(arec
->username
, username
)))
281 hprecv
= arec
->precedence
;
290 for (arec
= atable
[0]; arec
; arec
= arec
->next
)
292 if(arec
->type
== (type
& ~0x1) &&
293 arec
->masktype
== HM_HOST
&&
294 arec
->precedence
> hprecv
&&
295 (match(arec
->Mask
.hostname
, orighost
) ||
296 (sockhost
&& match(arec
->Mask
.hostname
, sockhost
))) &&
297 (type
& 0x1 || match(arec
->username
, username
)))
299 hprecv
= arec
->precedence
;
308 /* And yes - we have to check p after strchr and p after increment for
310 for (p
= name
; p
!= NULL
;)
312 for (arec
= atable
[hash_text(p
)]; arec
; arec
= arec
->next
)
313 if((arec
->type
== (type
& ~0x1)) &&
314 (arec
->masktype
== HM_HOST
) &&
315 arec
->precedence
> hprecv
&&
316 match(arec
->Mask
.hostname
, name
) &&
317 (type
& 0x1 || match(arec
->username
, username
)))
319 hprecv
= arec
->precedence
;
328 for (arec
= atable
[0]; arec
; arec
= arec
->next
)
330 if(arec
->type
== (type
& ~0x1) &&
331 arec
->masktype
== HM_HOST
&&
332 arec
->precedence
> hprecv
&&
333 (match(arec
->Mask
.hostname
, name
) ||
334 (sockhost
&& match(arec
->Mask
.hostname
, sockhost
))) &&
335 (type
& 0x1 || match(arec
->username
, username
)))
337 hprecv
= arec
->precedence
;
345 /* struct ConfItem* find_address_conf(const char*, const char*,
346 * struct rb_sockaddr_storage*, int);
347 * Input: The hostname, username, address, address family.
348 * Output: The applicable ConfItem.
352 find_address_conf(const char *host
, const char *sockhost
, const char *user
,
353 const char *notildeuser
, struct sockaddr
*ip
, int aftype
)
355 struct ConfItem
*iconf
, *kconf
;
358 /* Find the best I-line... If none, return NULL -A1kmm */
359 if(!(iconf
= find_conf_by_address(host
, sockhost
, NULL
, ip
, CONF_CLIENT
, aftype
, user
)))
361 /* Find what their visible username will be.
362 * Note that the username without tilde may contain one char more.
364 vuser
= IsNoTilde(iconf
) ? notildeuser
: user
;
366 /* If they are exempt from K-lines, return the best I-line. -A1kmm */
367 if(IsConfExemptKline(iconf
))
370 /* Find the best K-line... -A1kmm */
371 kconf
= find_conf_by_address(host
, sockhost
, NULL
, ip
, CONF_KILL
, aftype
, user
);
373 /* If they are K-lined, return the K-line */
377 /* if theres a spoof, check it against klines.. */
378 if(IsConfDoSpoofIp(iconf
))
380 char *p
= strchr(iconf
->name
, '@');
382 /* note, we dont need to pass sockhost here, as its
383 * guaranteed to not match by whats above.. --anfl
388 kconf
= find_conf_by_address(p
+1, NULL
, NULL
, ip
, CONF_KILL
, aftype
, iconf
->name
);
392 kconf
= find_conf_by_address(iconf
->name
, NULL
, NULL
, ip
, CONF_KILL
, aftype
, vuser
);
398 /* if no_tilde, check the username without tilde against klines too
402 kconf
= find_conf_by_address(host
, sockhost
, NULL
, ip
, CONF_KILL
, aftype
, vuser
);
407 /* hunt for a gline */
408 if(ConfigFileEntry
.glines
)
410 kconf
= find_conf_by_address(host
, sockhost
, NULL
, ip
, CONF_GLINE
, aftype
, user
);
412 if((kconf
!= NULL
) && !IsConfExemptGline(iconf
))
419 /* struct ConfItem* find_dline(struct rb_sockaddr_storage*, int)
420 * Input: An address, an address family.
421 * Output: The best matching D-line or exempt line.
422 * Side effects: None.
425 find_dline(struct sockaddr
*addr
, int aftype
)
427 struct ConfItem
*eline
;
428 eline
= find_conf_by_address(NULL
, NULL
, NULL
, addr
, CONF_EXEMPTDLINE
| 1, aftype
, NULL
);
431 return find_conf_by_address(NULL
, NULL
, NULL
, addr
, CONF_DLINE
| 1, aftype
, NULL
);
434 /* void find_exact_conf_by_address(const char*, int, const char *)
436 * Output: ConfItem if found
440 find_exact_conf_by_address(const char *address
, int type
, const char *username
)
444 struct AddressRec
*arec
;
445 struct rb_sockaddr_storage addr
;
448 address
= "/NOMATCH!/";
449 arec
= rb_malloc(sizeof(struct AddressRec
));
450 masktype
= parse_netmask(address
, (struct sockaddr
*)&addr
, &bits
);
452 if(masktype
== HM_IPV6
)
454 /* We have to do this, since we do not re-hash for every bit -A1kmm. */
455 hv
= hash_ipv6((struct sockaddr
*)&addr
, bits
- bits
% 16);
459 if(masktype
== HM_IPV4
)
461 /* We have to do this, since we do not re-hash for every bit -A1kmm. */
462 hv
= hash_ipv4((struct sockaddr
*)&addr
, bits
- bits
% 8);
466 hv
= get_mask_hash(address
);
468 for (arec
= atable
[hv
]; arec
; arec
= arec
->next
)
470 if (arec
->type
== type
&&
471 arec
->masktype
== masktype
&&
472 (arec
->username
== NULL
|| username
== NULL
? arec
->username
== username
: !irccmp(arec
->username
, username
)))
474 if (masktype
== HM_HOST
)
476 if (!irccmp(arec
->Mask
.hostname
, address
))
481 if (arec
->Mask
.ipa
.bits
== bits
&&
482 comp_with_mask_sock((struct sockaddr
*)&arec
->Mask
.ipa
.addr
, (struct sockaddr
*)&addr
, bits
))
490 /* void add_conf_by_address(const char*, int, const char *,
491 * struct ConfItem *aconf)
494 * Side-effects: Adds this entry to the hash table.
497 add_conf_by_address(const char *address
, int type
, const char *username
, struct ConfItem
*aconf
)
499 static unsigned long prec_value
= 0xFFFFFFFF;
502 struct AddressRec
*arec
;
505 address
= "/NOMATCH!/";
506 arec
= rb_malloc(sizeof(struct AddressRec
));
507 masktype
= parse_netmask(address
, (struct sockaddr
*)&arec
->Mask
.ipa
.addr
, &bits
);
508 arec
->Mask
.ipa
.bits
= bits
;
509 arec
->masktype
= masktype
;
511 if(masktype
== HM_IPV6
)
513 /* We have to do this, since we do not re-hash for every bit -A1kmm. */
515 arec
->next
= atable
[(hv
= hash_ipv6((struct sockaddr
*)&arec
->Mask
.ipa
.addr
, bits
))];
520 if(masktype
== HM_IPV4
)
522 /* We have to do this, since we do not re-hash for every bit -A1kmm. */
524 arec
->next
= atable
[(hv
= hash_ipv4((struct sockaddr
*)&arec
->Mask
.ipa
.addr
, bits
))];
529 arec
->Mask
.hostname
= address
;
530 arec
->next
= atable
[(hv
= get_mask_hash(address
))];
533 arec
->username
= username
;
535 arec
->precedence
= prec_value
--;
539 /* void delete_one_address(const char*, struct ConfItem*)
540 * Input: An address string, the associated ConfItem.
542 * Side effects: Deletes an address record. Frees the ConfItem if there
543 * is nothing referencing it, sets it as illegal otherwise.
546 delete_one_address_conf(const char *address
, struct ConfItem
*aconf
)
550 struct AddressRec
*arec
, *arecl
= NULL
;
551 struct rb_sockaddr_storage addr
;
552 masktype
= parse_netmask(address
, (struct sockaddr
*)&addr
, &bits
);
554 if(masktype
== HM_IPV6
)
556 /* We have to do this, since we do not re-hash for every bit -A1kmm. */
558 hv
= hash_ipv6((struct sockaddr
*)&addr
, bits
);
562 if(masktype
== HM_IPV4
)
564 /* We have to do this, since we do not re-hash for every bit -A1kmm. */
566 hv
= hash_ipv4((struct sockaddr
*)&addr
, bits
);
569 hv
= get_mask_hash(address
);
570 for (arec
= atable
[hv
]; arec
; arec
= arec
->next
)
572 if(arec
->aconf
== aconf
)
575 arecl
->next
= arec
->next
;
577 atable
[hv
] = arec
->next
;
578 aconf
->status
|= CONF_ILLEGAL
;
588 /* void clear_out_address_conf(void)
591 * Side effects: Clears out all address records in the hash table,
592 * frees them, and frees the ConfItems if nothing references
593 * them, otherwise sets them as illegal.
596 clear_out_address_conf(void)
599 struct AddressRec
**store_next
;
600 struct AddressRec
*arec
, *arecn
;
602 for (i
= 0; i
< ATABLE_SIZE
; i
++)
604 store_next
= &atable
[i
];
605 for (arec
= atable
[i
]; arec
; arec
= arecn
)
608 /* We keep the temporary K-lines and destroy the
609 * permanent ones, just to be confusing :) -A1kmm */
610 if(arec
->aconf
->flags
& CONF_FLAGS_TEMPORARY
||
611 (arec
->type
!= CONF_CLIENT
&& arec
->type
!= CONF_EXEMPTDLINE
))
614 store_next
= &arec
->next
;
618 arec
->aconf
->status
|= CONF_ILLEGAL
;
619 if(!arec
->aconf
->clients
)
620 free_conf(arec
->aconf
);
629 clear_out_address_conf_bans(void)
632 struct AddressRec
**store_next
;
633 struct AddressRec
*arec
, *arecn
;
635 for (i
= 0; i
< ATABLE_SIZE
; i
++)
637 store_next
= &atable
[i
];
638 for (arec
= atable
[i
]; arec
; arec
= arecn
)
641 /* We keep the temporary K-lines and destroy the
642 * permanent ones, just to be confusing :) -A1kmm */
643 if(arec
->aconf
->flags
& CONF_FLAGS_TEMPORARY
||
644 (arec
->type
== CONF_CLIENT
|| arec
->type
== CONF_EXEMPTDLINE
))
647 store_next
= &arec
->next
;
651 arec
->aconf
->status
|= CONF_ILLEGAL
;
652 if(!arec
->aconf
->clients
)
653 free_conf(arec
->aconf
);
663 * show_iline_prefix()
665 * inputs - pointer to struct Client requesting output
666 * - pointer to struct ConfItem
667 * - name to which iline prefix will be prefixed to
668 * output - pointer to static string with prefixes listed in ascii form
669 * side effects - NONE
672 show_iline_prefix(struct Client
*sptr
, struct ConfItem
*aconf
, char *name
)
674 static char prefix_of_host
[USERLEN
+ 15];
677 prefix_ptr
= prefix_of_host
;
680 if(IsNeedIdentd(aconf
))
682 if(IsConfDoSpoofIp(aconf
))
684 if(IsOper(sptr
) && IsConfExemptFlood(aconf
))
686 if(IsOper(sptr
) && IsConfExemptGline(aconf
) && !IsConfExemptKline(aconf
))
688 if(IsOper(sptr
) && IsConfExemptDNSBL(aconf
) && !IsConfExemptKline(aconf
))
690 if(IsOper(sptr
) && IsConfExemptKline(aconf
))
692 if(IsOper(sptr
) && IsConfExemptLimits(aconf
))
695 strncpy(prefix_ptr
, name
, USERLEN
);
696 return (prefix_of_host
);
701 * Inputs: pointer to client to report to
703 * Side effects: Reports configured auth{} blocks to client_p
706 report_auth(struct Client
*client_p
)
708 char *name
, *host
, *pass
, *user
, *classname
;
709 struct AddressRec
*arec
;
710 struct ConfItem
*aconf
;
713 for (i
= 0; i
< ATABLE_SIZE
; i
++)
714 for (arec
= atable
[i
]; arec
; arec
= arec
->next
)
715 if(arec
->type
== CONF_CLIENT
)
719 if(!IsOper(client_p
) && IsConfDoSpoofIp(aconf
))
722 get_printable_conf(aconf
, &name
, &host
, &pass
, &user
, &port
,
725 sendto_one_numeric(client_p
, RPL_STATSILINE
,
726 form_str(RPL_STATSILINE
),
727 name
, show_iline_prefix(client_p
, aconf
, user
),
728 show_ip_conf(aconf
, client_p
) ? host
: "255.255.255.255",
735 * inputs - Client to report to, mask
737 * side effects - Reports configured K-lines to client_p.
740 report_Klines(struct Client
*source_p
)
742 char *host
, *pass
, *user
, *oper_reason
;
743 struct AddressRec
*arec
;
744 struct ConfItem
*aconf
= NULL
;
747 for (i
= 0; i
< ATABLE_SIZE
; i
++)
749 for (arec
= atable
[i
]; arec
; arec
= arec
->next
)
751 if(arec
->type
== CONF_KILL
)
755 /* its a tempkline, theyre reported elsewhere */
756 if(aconf
->flags
& CONF_FLAGS_TEMPORARY
)
759 get_printable_kline(source_p
, aconf
, &host
, &pass
, &user
, &oper_reason
);
760 sendto_one_numeric(source_p
, RPL_STATSKLINE
,
761 form_str(RPL_STATSKLINE
),
762 'K', host
, user
, pass
,
763 oper_reason
? "|" : "",
764 oper_reason
? oper_reason
: "");