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-2006 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 62 2006-09-22 23:51:46Z spb $
30 #include "ircd_defs.h"
35 #include "irc_string.h"
38 static unsigned long hash_ipv6(struct sockaddr
*, int);
40 static unsigned long hash_ipv4(struct sockaddr
*, int);
43 /* int parse_netmask(const char *, struct irc_sockaddr_storage *, int *);
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).
51 parse_netmask(const char *text
, struct sockaddr
*naddr
, int *nb
)
53 char *ip
= LOCAL_COPY(text
);
55 struct irc_sockaddr_storage
*addr
, xaddr
;
63 addr
= (struct irc_sockaddr_storage
*)&xaddr
;
65 addr
= (struct irc_sockaddr_storage
*)naddr
;
70 if((ptr
= strchr(ip
, '/')))
79 if(inetpton_sock(ip
, (struct sockaddr
*)addr
) > 0)
87 if((ptr
= strchr(ip
, '/')))
96 if(inetpton_sock(ip
, (struct sockaddr
*)addr
) > 0)
104 /* Hashtable stuff...now external as its used in m_stats.c */
105 struct AddressRec
*atable
[ATABLE_SIZE
];
110 memset(&atable
, 0, sizeof(atable
));
113 /* unsigned long hash_ipv4(struct irc_sockaddr_storage*)
114 * Input: An IP address.
115 * Output: A hash value of the IP address.
119 hash_ipv4(struct sockaddr
*saddr
, int bits
)
121 struct sockaddr_in
*addr
= (struct sockaddr_in
*) saddr
;
125 unsigned long av
= ntohl(addr
->sin_addr
.s_addr
) & ~((1 << (32 - bits
)) - 1);
126 return (av
^ (av
>> 12) ^ (av
>> 24)) & (ATABLE_SIZE
- 1);
132 /* unsigned long hash_ipv6(struct irc_sockaddr_storage*)
133 * Input: An IP address.
134 * Output: A hash value of the IP address.
139 hash_ipv6(struct sockaddr
*saddr
, int bits
)
141 struct sockaddr_in6
*addr
= (struct sockaddr_in6
*) 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);
162 /* int hash_text(const char *start)
163 * Input: The start of the text to hash.
164 * Output: The hash of the string between 1 and (TH_MAX-1)
165 * Side-effects: None.
168 hash_text(const char *start
)
170 const char *p
= start
;
175 h
= (h
<< 4) - (h
+ (unsigned char) ToLower(*p
++));
178 return (h
& (ATABLE_SIZE
- 1));
181 /* unsigned long get_hash_mask(const char *)
182 * Input: The text to hash.
183 * Output: The hash of the string right of the first '.' past the last
184 * wildcard in the string.
185 * Side-effects: None.
188 get_mask_hash(const char *text
)
190 const char *hp
= "", *p
;
192 for (p
= text
+ strlen(text
) - 1; p
>= text
; p
--)
193 if(*p
== '*' || *p
== '?')
194 return hash_text(hp
);
197 return hash_text(text
);
200 /* struct ConfItem* find_conf_by_address(const char*, struct irc_sockaddr_storage*,
201 * int type, int fam, const char *username)
202 * Input: The hostname, the address, the type of mask to find, the address
203 * family, the username.
204 * Output: The matching value with the highest precedence.
206 * Note: Setting bit 0 of the type means that the username is ignored.
209 find_conf_by_address(const char *name
, const char *sockhost
,
210 const char *orighost
,
211 struct sockaddr
*addr
, int type
, int fam
,
212 const char *username
)
214 unsigned long hprecv
= 0;
215 struct ConfItem
*hprec
= NULL
;
216 struct AddressRec
*arec
;
224 /* Check for IPV6 matches... */
229 for (b
= 128; b
>= 0; b
-= 16)
231 for (arec
= atable
[hash_ipv6(addr
, b
)]; arec
; arec
= arec
->next
)
232 if(arec
->type
== (type
& ~0x1) &&
233 arec
->masktype
== HM_IPV6
&&
234 comp_with_mask_sock(addr
, (struct sockaddr
*)&arec
->Mask
.ipa
.addr
,
235 arec
->Mask
.ipa
.bits
) && (type
& 0x1
240 && arec
->precedence
> hprecv
)
242 hprecv
= arec
->precedence
;
251 for (b
= 32; b
>= 0; b
-= 8)
253 for (arec
= atable
[hash_ipv4(addr
, b
)]; arec
; arec
= arec
->next
)
254 if(arec
->type
== (type
& ~0x1) &&
255 arec
->masktype
== HM_IPV4
&&
256 arec
->precedence
> hprecv
&&
257 comp_with_mask_sock(addr
, (struct sockaddr
*)&arec
->Mask
.ipa
.addr
,
258 arec
->Mask
.ipa
.bits
) &&
259 (type
& 0x1 || match(arec
->username
, username
)))
261 hprecv
= arec
->precedence
;
272 for (p
= orighost
; p
!= NULL
;)
274 for (arec
= atable
[hash_text(p
)]; arec
; arec
= arec
->next
)
276 if((arec
->type
== (type
& ~0x1)) &&
277 (arec
->masktype
== HM_HOST
) &&
278 arec
->precedence
> hprecv
&&
279 match(arec
->Mask
.hostname
, orighost
) &&
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
& 0x1 || match(arec
->username
, username
)))
300 hprecv
= arec
->precedence
;
309 /* And yes - we have to check p after strchr and p after increment for
311 for (p
= name
; p
!= NULL
;)
313 for (arec
= atable
[hash_text(p
)]; arec
; arec
= arec
->next
)
314 if((arec
->type
== (type
& ~0x1)) &&
315 (arec
->masktype
== HM_HOST
) &&
316 arec
->precedence
> hprecv
&&
317 match(arec
->Mask
.hostname
, name
) &&
318 (type
& 0x1 || match(arec
->username
, username
)))
320 hprecv
= arec
->precedence
;
329 for (arec
= atable
[0]; arec
; arec
= arec
->next
)
331 if(arec
->type
== (type
& ~0x1) &&
332 arec
->masktype
== HM_HOST
&&
333 arec
->precedence
> hprecv
&&
334 (match(arec
->Mask
.hostname
, name
) ||
335 (sockhost
&& match(arec
->Mask
.hostname
, sockhost
))) &&
336 (type
& 0x1 || match(arec
->username
, username
)))
338 hprecv
= arec
->precedence
;
346 /* struct ConfItem* find_address_conf(const char*, const char*,
347 * struct irc_sockaddr_storage*, int);
348 * Input: The hostname, username, address, address family.
349 * Output: The applicable ConfItem.
353 find_address_conf(const char *host
, const char *sockhost
, const char *user
,
354 struct sockaddr
*ip
, int aftype
)
356 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
)))
362 /* If they are exempt from K-lines, return the best I-line. -A1kmm */
363 if(IsConfExemptKline(iconf
))
366 /* Find the best K-line... -A1kmm */
367 kconf
= find_conf_by_address(host
, sockhost
, NULL
, ip
, CONF_KILL
, aftype
, user
);
369 /* If they are K-lined, return the K-line */
373 /* if theres a spoof, check it against klines.. */
374 if(IsConfDoSpoofIp(iconf
))
376 char *p
= strchr(iconf
->name
, '@');
378 /* note, we dont need to pass sockhost here, as its
379 * guaranteed to not match by whats above.. --anfl
384 kconf
= find_conf_by_address(p
+1, NULL
, NULL
, ip
, CONF_KILL
, aftype
, iconf
->name
);
388 kconf
= find_conf_by_address(iconf
->name
, NULL
, NULL
, ip
, CONF_KILL
, aftype
, user
);
397 /* struct ConfItem* find_dline(struct irc_sockaddr_storage*, int)
398 * Input: An address, an address family.
399 * Output: The best matching D-line or exempt line.
400 * Side effects: None.
403 find_dline(struct sockaddr
*addr
, int aftype
)
405 struct ConfItem
*eline
;
406 eline
= find_conf_by_address(NULL
, NULL
, NULL
, addr
, CONF_EXEMPTDLINE
| 1, aftype
, NULL
);
409 return find_conf_by_address(NULL
, NULL
, NULL
, addr
, CONF_DLINE
| 1, aftype
, NULL
);
412 /* void add_conf_by_address(const char*, int, const char *,
413 * struct ConfItem *aconf)
416 * Side-effects: Adds this entry to the hash table.
419 add_conf_by_address(const char *address
, int type
, const char *username
, struct ConfItem
*aconf
)
421 static unsigned long prec_value
= 0xFFFFFFFF;
424 struct AddressRec
*arec
;
427 address
= "/NOMATCH!/";
428 arec
= MyMalloc(sizeof(struct AddressRec
));
429 masktype
= parse_netmask(address
, (struct sockaddr
*)&arec
->Mask
.ipa
.addr
, &bits
);
430 arec
->Mask
.ipa
.bits
= bits
;
431 arec
->masktype
= masktype
;
433 if(masktype
== HM_IPV6
)
435 /* We have to do this, since we do not re-hash for every bit -A1kmm. */
437 arec
->next
= atable
[(hv
= hash_ipv6((struct sockaddr
*)&arec
->Mask
.ipa
.addr
, bits
))];
442 if(masktype
== HM_IPV4
)
444 /* We have to do this, since we do not re-hash for every bit -A1kmm. */
446 arec
->next
= atable
[(hv
= hash_ipv4((struct sockaddr
*)&arec
->Mask
.ipa
.addr
, bits
))];
451 arec
->Mask
.hostname
= address
;
452 arec
->next
= atable
[(hv
= get_mask_hash(address
))];
455 arec
->username
= username
;
457 arec
->precedence
= prec_value
--;
461 /* void delete_one_address(const char*, struct ConfItem*)
462 * Input: An address string, the associated ConfItem.
464 * Side effects: Deletes an address record. Frees the ConfItem if there
465 * is nothing referencing it, sets it as illegal otherwise.
468 delete_one_address_conf(const char *address
, struct ConfItem
*aconf
)
472 struct AddressRec
*arec
, *arecl
= NULL
;
473 struct irc_sockaddr_storage addr
;
474 masktype
= parse_netmask(address
, (struct sockaddr
*)&addr
, &bits
);
476 if(masktype
== HM_IPV6
)
478 /* We have to do this, since we do not re-hash for every bit -A1kmm. */
480 hv
= hash_ipv6((struct sockaddr
*)&addr
, bits
);
484 if(masktype
== HM_IPV4
)
486 /* We have to do this, since we do not re-hash for every bit -A1kmm. */
488 hv
= hash_ipv4((struct sockaddr
*)&addr
, bits
);
491 hv
= get_mask_hash(address
);
492 for (arec
= atable
[hv
]; arec
; arec
= arec
->next
)
494 if(arec
->aconf
== aconf
)
497 arecl
->next
= arec
->next
;
499 atable
[hv
] = arec
->next
;
500 aconf
->status
|= CONF_ILLEGAL
;
510 /* void clear_out_address_conf(void)
513 * Side effects: Clears out all address records in the hash table,
514 * frees them, and frees the ConfItems if nothing references
515 * them, otherwise sets them as illegal.
518 clear_out_address_conf(void)
521 struct AddressRec
**store_next
;
522 struct AddressRec
*arec
, *arecn
;
524 for (i
= 0; i
< ATABLE_SIZE
; i
++)
526 store_next
= &atable
[i
];
527 for (arec
= atable
[i
]; arec
; arec
= arecn
)
530 /* We keep the temporary K-lines and destroy the
531 * permanent ones, just to be confusing :) -A1kmm */
532 if(arec
->aconf
->flags
& CONF_FLAGS_TEMPORARY
||
533 (arec
->type
!= CONF_CLIENT
&& arec
->type
!= CONF_EXEMPTDLINE
))
536 store_next
= &arec
->next
;
540 arec
->aconf
->status
|= CONF_ILLEGAL
;
541 if(!arec
->aconf
->clients
)
542 free_conf(arec
->aconf
);
551 clear_out_address_conf_bans(void)
554 struct AddressRec
**store_next
;
555 struct AddressRec
*arec
, *arecn
;
557 for (i
= 0; i
< ATABLE_SIZE
; i
++)
559 store_next
= &atable
[i
];
560 for (arec
= atable
[i
]; arec
; arec
= arecn
)
563 /* We keep the temporary K-lines and destroy the
564 * permanent ones, just to be confusing :) -A1kmm */
565 if(arec
->aconf
->flags
& CONF_FLAGS_TEMPORARY
||
566 (arec
->type
== CONF_CLIENT
|| arec
->type
== CONF_EXEMPTDLINE
))
569 store_next
= &arec
->next
;
573 arec
->aconf
->status
|= CONF_ILLEGAL
;
574 if(!arec
->aconf
->clients
)
575 free_conf(arec
->aconf
);
585 * show_iline_prefix()
587 * inputs - pointer to struct Client requesting output
588 * - pointer to struct ConfItem
589 * - name to which iline prefix will be prefixed to
590 * output - pointer to static string with prefixes listed in ascii form
591 * side effects - NONE
594 show_iline_prefix(struct Client
*sptr
, struct ConfItem
*aconf
, char *name
)
596 static char prefix_of_host
[USERLEN
+ 15];
599 prefix_ptr
= prefix_of_host
;
604 if(IsNeedIdentd(aconf
))
606 if(IsPassIdentd(aconf
))
608 if(IsNoMatchIp(aconf
))
610 if(IsConfDoSpoofIp(aconf
))
612 if(MyOper(sptr
) && IsConfExemptKline(aconf
))
614 if(MyOper(sptr
) && IsConfExemptLimits(aconf
))
616 if(MyOper(sptr
) && IsConfIdlelined(aconf
))
619 strncpy(prefix_ptr
, name
, USERLEN
);
620 return (prefix_of_host
);
625 * Inputs: pointer to client to report to
627 * Side effects: Reports configured auth{} blocks to client_p
630 report_auth(struct Client
*client_p
)
632 char *name
, *host
, *pass
, *user
, *classname
;
633 struct AddressRec
*arec
;
634 struct ConfItem
*aconf
;
637 for (i
= 0; i
< ATABLE_SIZE
; i
++)
638 for (arec
= atable
[i
]; arec
; arec
= arec
->next
)
639 if(arec
->type
== CONF_CLIENT
)
643 if(!MyOper(client_p
) && IsConfDoSpoofIp(aconf
))
646 get_printable_conf(aconf
, &name
, &host
, &pass
, &user
, &port
,
649 sendto_one_numeric(client_p
, RPL_STATSILINE
,
650 form_str(RPL_STATSILINE
),
651 name
, show_iline_prefix(client_p
, aconf
, user
),
652 show_ip_conf(aconf
, client_p
) ? host
: "255.255.255.255",
659 * inputs - Client to report to, mask
661 * side effects - Reports configured K-lines to client_p.
664 report_Klines(struct Client
*source_p
)
666 char *host
, *pass
, *user
, *oper_reason
;
667 struct AddressRec
*arec
;
668 struct ConfItem
*aconf
= NULL
;
671 for (i
= 0; i
< ATABLE_SIZE
; i
++)
673 for (arec
= atable
[i
]; arec
; arec
= arec
->next
)
675 if(arec
->type
== CONF_KILL
)
679 /* its a tempkline, theyre reported elsewhere */
680 if(aconf
->flags
& CONF_FLAGS_TEMPORARY
)
683 get_printable_kline(source_p
, aconf
, &host
, &pass
, &user
, &oper_reason
);
684 sendto_one_numeric(source_p
, RPL_STATSKLINE
,
685 form_str(RPL_STATSKLINE
),
686 'K', host
, user
, pass
,
687 oper_reason
? "|" : "",
688 oper_reason
? oper_reason
: "");