]>
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
25 * $Id: hostmask.c 2757 2006-11-10 22:58:15Z jilles $
29 #include "ircd_defs.h"
35 #include "ipv4_from_ipv6.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 rb_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 rb_sockaddr_storage
*naddr
, int *nb
)
53 char *ip
= LOCAL_COPY(text
);
55 struct rb_sockaddr_storage
*addr
, xaddr
;
67 if(strpbrk(ip
, "*?") != NULL
)
74 if((ptr
= strchr(ip
, '/')))
85 if(rb_inet_pton_sock(ip
, (struct sockaddr
*)addr
) > 0)
93 if((ptr
= strchr(ip
, '/')))
104 if(rb_inet_pton_sock(ip
, (struct sockaddr
*)addr
) > 0)
112 /* Hashtable stuff...now external as its used in m_stats.c */
113 struct AddressRec
*atable
[ATABLE_SIZE
];
118 memset(&atable
, 0, sizeof(atable
));
121 /* unsigned long hash_ipv4(struct rb_sockaddr_storage*)
122 * Input: An IP address.
123 * Output: A hash value of the IP address.
127 hash_ipv4(struct sockaddr
*saddr
, int bits
)
129 struct sockaddr_in
*addr
= (struct sockaddr_in
*)(void *)saddr
;
133 unsigned long av
= ntohl(addr
->sin_addr
.s_addr
) & ~((1 << (32 - bits
)) - 1);
134 return (av
^ (av
>> 12) ^ (av
>> 24)) & (ATABLE_SIZE
- 1);
140 /* unsigned long hash_ipv6(struct rb_sockaddr_storage*)
141 * Input: An IP address.
142 * Output: A hash value of the IP address.
147 hash_ipv6(struct sockaddr
*saddr
, int bits
)
149 struct sockaddr_in6
*addr
= (struct sockaddr_in6
*)(void *)saddr
;
150 unsigned long v
= 0, n
;
151 for (n
= 0; n
< 16; n
++)
155 v
^= addr
->sin6_addr
.s6_addr
[n
];
160 v
^= addr
->sin6_addr
.s6_addr
[n
] & ~((1 << (8 - bits
)) - 1);
161 return v
& (ATABLE_SIZE
- 1);
164 return v
& (ATABLE_SIZE
- 1);
166 return v
& (ATABLE_SIZE
- 1);
170 /* int hash_text(const char *start)
171 * Input: The start of the text to hash.
172 * Output: The hash of the string between 1 and (TH_MAX-1)
173 * Side-effects: None.
176 hash_text(const char *start
)
178 const char *p
= start
;
183 h
= (h
<< 4) - (h
+ (unsigned char) ToLower(*p
++));
186 return (h
& (ATABLE_SIZE
- 1));
189 /* unsigned long get_hash_mask(const char *)
190 * Input: The text to hash.
191 * Output: The hash of the string right of the first '.' past the last
192 * wildcard in the string.
193 * Side-effects: None.
196 get_mask_hash(const char *text
)
198 const char *hp
= "", *p
;
200 for (p
= text
+ strlen(text
) - 1; p
>= text
; p
--)
201 if(*p
== '*' || *p
== '?')
202 return hash_text(hp
);
205 return hash_text(text
);
208 /* struct ConfItem* find_conf_by_address(const char*, struct rb_sockaddr_storage*,
209 * int type, int fam, const char *username)
210 * Input: The hostname, the address, the type of mask to find, the address
211 * family, the username.
212 * Output: The matching value with the highest precedence.
214 * Note: Setting bit 0 of the type means that the username is ignored.
217 find_conf_by_address(const char *name
, const char *sockhost
,
218 const char *orighost
,
219 struct sockaddr
*addr
, int type
, int fam
,
220 const char *username
, const char *auth_user
)
222 unsigned long hprecv
= 0;
223 struct ConfItem
*hprec
= NULL
;
224 struct AddressRec
*arec
;
232 /* Check for IPV6 matches... */
237 for (b
= 128; b
>= 0; b
-= 16)
239 for (arec
= atable
[hash_ipv6(addr
, b
)]; arec
; arec
= arec
->next
)
240 if(arec
->type
== (type
& ~0x1) &&
241 arec
->masktype
== HM_IPV6
&&
242 comp_with_mask_sock(addr
, (struct sockaddr
*)&arec
->Mask
.ipa
.addr
,
243 arec
->Mask
.ipa
.bits
) &&
244 (type
& 0x1 || match(arec
-> username
, username
)) &&
245 (type
!= CONF_CLIENT
|| !arec
->auth_user
||
246 (auth_user
&& match(arec
->auth_user
, auth_user
))) &&
247 arec
->precedence
> hprecv
)
249 hprecv
= arec
->precedence
;
258 for (b
= 32; b
>= 0; b
-= 8)
260 for (arec
= atable
[hash_ipv4(addr
, b
)]; arec
; arec
= arec
->next
)
261 if(arec
->type
== (type
& ~0x1) &&
262 arec
->masktype
== HM_IPV4
&&
263 comp_with_mask_sock(addr
, (struct sockaddr
*)&arec
->Mask
.ipa
.addr
,
264 arec
->Mask
.ipa
.bits
) &&
265 (type
& 0x1 || match(arec
->username
, username
)) &&
266 (type
!= CONF_CLIENT
|| !arec
->auth_user
||
267 (auth_user
&& match(arec
->auth_user
, auth_user
))) &&
268 arec
->precedence
> hprecv
)
270 hprecv
= arec
->precedence
;
281 for (p
= orighost
; p
!= NULL
;)
283 for (arec
= atable
[hash_text(p
)]; arec
; arec
= arec
->next
)
285 if((arec
->type
== (type
& ~0x1)) &&
286 (arec
->masktype
== HM_HOST
) &&
287 arec
->precedence
> hprecv
&&
288 match(arec
->Mask
.hostname
, orighost
) &&
289 (type
!= CONF_CLIENT
|| !arec
->auth_user
||
290 (auth_user
&& match(arec
->auth_user
, auth_user
))) &&
291 (type
& 0x1 || match(arec
->username
, username
)))
293 hprecv
= arec
->precedence
;
302 for (arec
= atable
[0]; arec
; arec
= arec
->next
)
304 if(arec
->type
== (type
& ~0x1) &&
305 arec
->masktype
== HM_HOST
&&
306 arec
->precedence
> hprecv
&&
307 (match(arec
->Mask
.hostname
, orighost
) ||
308 (sockhost
&& match(arec
->Mask
.hostname
, sockhost
))) &&
309 (type
!= CONF_CLIENT
|| !arec
->auth_user
||
310 (auth_user
&& match(arec
->auth_user
, auth_user
))) &&
311 (type
& 0x1 || match(arec
->username
, username
)))
313 hprecv
= arec
->precedence
;
322 /* And yes - we have to check p after strchr and p after increment for
324 for (p
= name
; p
!= NULL
;)
326 for (arec
= atable
[hash_text(p
)]; arec
; arec
= arec
->next
)
327 if((arec
->type
== (type
& ~0x1)) &&
328 (arec
->masktype
== HM_HOST
) &&
329 arec
->precedence
> hprecv
&&
330 match(arec
->Mask
.hostname
, name
) &&
331 (type
!= CONF_CLIENT
|| !arec
->auth_user
||
332 (auth_user
&& match(arec
->auth_user
, auth_user
))) &&
333 (type
& 0x1 || match(arec
->username
, username
)))
335 hprecv
= arec
->precedence
;
344 for (arec
= atable
[0]; arec
; arec
= arec
->next
)
346 if(arec
->type
== (type
& ~0x1) &&
347 arec
->masktype
== HM_HOST
&&
348 arec
->precedence
> hprecv
&&
349 (match(arec
->Mask
.hostname
, name
) ||
350 (sockhost
&& match(arec
->Mask
.hostname
, sockhost
))) &&
351 (type
!= CONF_CLIENT
|| !arec
->auth_user
||
352 (auth_user
&& match(arec
->auth_user
, auth_user
))) &&
353 (type
& 0x1 || match(arec
->username
, username
)))
355 hprecv
= arec
->precedence
;
363 /* struct ConfItem* find_address_conf(const char*, const char*,
364 * struct rb_sockaddr_storage*, int);
365 * Input: The hostname, username, address, address family.
366 * Output: The applicable ConfItem.
370 find_address_conf(const char *host
, const char *sockhost
, const char *user
,
371 const char *notildeuser
, struct sockaddr
*ip
, int aftype
, char *auth_user
)
373 struct ConfItem
*iconf
, *kconf
;
376 struct sockaddr_in ip4
;
379 /* Find the best I-line... If none, return NULL -A1kmm */
380 if(!(iconf
= find_conf_by_address(host
, sockhost
, NULL
, ip
, CONF_CLIENT
, aftype
, user
, auth_user
)))
382 /* Find what their visible username will be.
383 * Note that the username without tilde may contain one char more.
385 vuser
= IsNoTilde(iconf
) ? notildeuser
: user
;
387 /* If they are exempt from K-lines, return the best I-line. -A1kmm */
388 if(IsConfExemptKline(iconf
))
391 /* Find the best K-line... -A1kmm */
392 kconf
= find_conf_by_address(host
, sockhost
, NULL
, ip
, CONF_KILL
, aftype
, user
, NULL
);
394 /* If they are K-lined, return the K-line */
398 /* if theres a spoof, check it against klines.. */
399 if(IsConfDoSpoofIp(iconf
))
401 char *p
= strchr(iconf
->info
.name
, '@');
403 /* note, we dont need to pass sockhost here, as its
404 * guaranteed to not match by whats above.. --anfl
409 kconf
= find_conf_by_address(p
+1, NULL
, NULL
, ip
, CONF_KILL
, aftype
, iconf
->info
.name
, NULL
);
413 kconf
= find_conf_by_address(iconf
->info
.name
, NULL
, NULL
, ip
, CONF_KILL
, aftype
, vuser
, NULL
);
419 /* if no_tilde, check the username without tilde against klines too
423 kconf
= find_conf_by_address(host
, sockhost
, NULL
, ip
, CONF_KILL
, aftype
, vuser
, NULL
);
429 if(ip
!= NULL
&& ip
->sa_family
== AF_INET6
&&
430 ipv4_from_ipv6((const struct sockaddr_in6
*)(const void *)ip
, &ip4
))
432 kconf
= find_conf_by_address(NULL
, NULL
, NULL
, (struct sockaddr
*)&ip4
, CONF_KILL
, AF_INET
, vuser
, NULL
);
441 /* struct ConfItem* find_dline(struct rb_sockaddr_storage*, int)
442 * Input: An address, an address family.
443 * Output: The best matching D-line or exempt line.
444 * Side effects: None.
447 find_dline(struct sockaddr
*addr
, int aftype
)
449 struct ConfItem
*aconf
;
451 struct sockaddr_in addr2
;
454 aconf
= find_conf_by_address(NULL
, NULL
, NULL
, addr
, CONF_EXEMPTDLINE
| 1, aftype
, NULL
, NULL
);
457 aconf
= find_conf_by_address(NULL
, NULL
, NULL
, addr
, CONF_DLINE
| 1, aftype
, NULL
, NULL
);
461 if(addr
->sa_family
== AF_INET6
&&
462 ipv4_from_ipv6((const struct sockaddr_in6
*)(const void *)addr
, &addr2
))
464 aconf
= find_conf_by_address(NULL
, NULL
, NULL
, (struct sockaddr
*)&addr2
, CONF_DLINE
| 1, AF_INET
, NULL
, NULL
);
472 /* void find_exact_conf_by_address(const char*, int, const char *)
474 * Output: ConfItem if found
478 find_exact_conf_by_address(const char *address
, int type
, const char *username
)
482 struct AddressRec
*arec
;
483 struct rb_sockaddr_storage addr
;
486 address
= "/NOMATCH!/";
487 masktype
= parse_netmask(address
, &addr
, &bits
);
489 if(masktype
== HM_IPV6
)
491 /* We have to do this, since we do not re-hash for every bit -A1kmm. */
492 hv
= hash_ipv6((struct sockaddr
*)&addr
, bits
- bits
% 16);
496 if(masktype
== HM_IPV4
)
498 /* We have to do this, since we do not re-hash for every bit -A1kmm. */
499 hv
= hash_ipv4((struct sockaddr
*)&addr
, bits
- bits
% 8);
503 hv
= get_mask_hash(address
);
505 for (arec
= atable
[hv
]; arec
; arec
= arec
->next
)
507 if (arec
->type
== type
&&
508 arec
->masktype
== masktype
&&
509 (arec
->username
== NULL
|| username
== NULL
? arec
->username
== username
: !irccmp(arec
->username
, username
)))
511 if (masktype
== HM_HOST
)
513 if (!irccmp(arec
->Mask
.hostname
, address
))
518 if (arec
->Mask
.ipa
.bits
== bits
&&
519 comp_with_mask_sock((struct sockaddr
*)&arec
->Mask
.ipa
.addr
, (struct sockaddr
*)&addr
, bits
))
527 /* void add_conf_by_address(const char*, int, const char *,
528 * struct ConfItem *aconf)
531 * Side-effects: Adds this entry to the hash table.
534 add_conf_by_address(const char *address
, int type
, const char *username
, const char *auth_user
, struct ConfItem
*aconf
)
536 static unsigned long prec_value
= 0xFFFFFFFF;
539 struct AddressRec
*arec
;
542 address
= "/NOMATCH!/";
543 arec
= rb_malloc(sizeof(struct AddressRec
));
544 masktype
= parse_netmask(address
, &arec
->Mask
.ipa
.addr
, &bits
);
545 arec
->Mask
.ipa
.bits
= bits
;
546 arec
->masktype
= masktype
;
548 if(masktype
== HM_IPV6
)
550 /* We have to do this, since we do not re-hash for every bit -A1kmm. */
552 arec
->next
= atable
[(hv
= hash_ipv6((struct sockaddr
*)&arec
->Mask
.ipa
.addr
, bits
))];
557 if(masktype
== HM_IPV4
)
559 /* We have to do this, since we do not re-hash for every bit -A1kmm. */
561 arec
->next
= atable
[(hv
= hash_ipv4((struct sockaddr
*)&arec
->Mask
.ipa
.addr
, bits
))];
566 arec
->Mask
.hostname
= address
;
567 arec
->next
= atable
[(hv
= get_mask_hash(address
))];
570 arec
->username
= username
;
571 arec
->auth_user
= auth_user
;
573 arec
->precedence
= prec_value
--;
577 /* void delete_one_address(const char*, struct ConfItem*)
578 * Input: An address string, the associated ConfItem.
580 * Side effects: Deletes an address record. Frees the ConfItem if there
581 * is nothing referencing it, sets it as illegal otherwise.
584 delete_one_address_conf(const char *address
, struct ConfItem
*aconf
)
588 struct AddressRec
*arec
, *arecl
= NULL
;
589 struct rb_sockaddr_storage addr
;
590 masktype
= parse_netmask(address
, &addr
, &bits
);
592 if(masktype
== HM_IPV6
)
594 /* We have to do this, since we do not re-hash for every bit -A1kmm. */
596 hv
= hash_ipv6((struct sockaddr
*)&addr
, bits
);
600 if(masktype
== HM_IPV4
)
602 /* We have to do this, since we do not re-hash for every bit -A1kmm. */
604 hv
= hash_ipv4((struct sockaddr
*)&addr
, bits
);
607 hv
= get_mask_hash(address
);
608 for (arec
= atable
[hv
]; arec
; arec
= arec
->next
)
610 if(arec
->aconf
== aconf
)
613 arecl
->next
= arec
->next
;
615 atable
[hv
] = arec
->next
;
616 aconf
->status
|= CONF_ILLEGAL
;
626 /* void clear_out_address_conf(void)
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.
634 clear_out_address_conf(void)
637 struct AddressRec
**store_next
;
638 struct AddressRec
*arec
, *arecn
;
640 for (i
= 0; i
< ATABLE_SIZE
; i
++)
642 store_next
= &atable
[i
];
643 for (arec
= atable
[i
]; arec
; arec
= arecn
)
646 /* We keep the temporary K-lines and destroy the
647 * permanent ones, just to be confusing :) -A1kmm */
648 if(arec
->aconf
->flags
& CONF_FLAGS_TEMPORARY
||
649 (arec
->type
!= CONF_CLIENT
&& arec
->type
!= CONF_EXEMPTDLINE
))
652 store_next
= &arec
->next
;
656 arec
->aconf
->status
|= CONF_ILLEGAL
;
657 if(!arec
->aconf
->clients
)
658 free_conf(arec
->aconf
);
667 clear_out_address_conf_bans(void)
670 struct AddressRec
**store_next
;
671 struct AddressRec
*arec
, *arecn
;
673 for (i
= 0; i
< ATABLE_SIZE
; i
++)
675 store_next
= &atable
[i
];
676 for (arec
= atable
[i
]; arec
; arec
= arecn
)
679 /* We keep the temporary K-lines and destroy the
680 * permanent ones, just to be confusing :) -A1kmm */
681 if(arec
->aconf
->flags
& CONF_FLAGS_TEMPORARY
||
682 (arec
->type
== CONF_CLIENT
|| arec
->type
== CONF_EXEMPTDLINE
))
685 store_next
= &arec
->next
;
689 arec
->aconf
->status
|= CONF_ILLEGAL
;
690 if(!arec
->aconf
->clients
)
691 free_conf(arec
->aconf
);
701 * show_iline_prefix()
703 * inputs - pointer to struct Client requesting output
704 * - pointer to struct ConfItem
705 * - name to which iline prefix will be prefixed to
706 * output - pointer to static string with prefixes listed in ascii form
707 * side effects - NONE
710 show_iline_prefix(struct Client
*sptr
, struct ConfItem
*aconf
, char *name
)
712 static char prefix_of_host
[USERLEN
+ 15];
715 prefix_ptr
= prefix_of_host
;
718 if(IsNeedIdentd(aconf
))
720 if(IsConfDoSpoofIp(aconf
))
722 if(IsOper(sptr
) && IsConfExemptFlood(aconf
))
724 if(IsOper(sptr
) && IsConfExemptDNSBL(aconf
) && !IsConfExemptKline(aconf
))
726 if(IsOper(sptr
) && IsConfExemptKline(aconf
))
728 if(IsOper(sptr
) && IsConfExemptLimits(aconf
))
731 strncpy(prefix_ptr
, name
, USERLEN
);
732 return (prefix_of_host
);
737 * Inputs: pointer to client to report to
739 * Side effects: Reports configured auth{} blocks to client_p
742 report_auth(struct Client
*client_p
)
744 char *name
, *host
, *user
, *classname
;
746 struct AddressRec
*arec
;
747 struct ConfItem
*aconf
;
750 for (i
= 0; i
< ATABLE_SIZE
; i
++)
751 for (arec
= atable
[i
]; arec
; arec
= arec
->next
)
752 if(arec
->type
== CONF_CLIENT
)
756 if(!IsOper(client_p
) && IsConfDoSpoofIp(aconf
))
759 get_printable_conf(aconf
, &name
, &host
, &pass
, &user
, &port
,
762 if(!EmptyString(aconf
->spasswd
))
763 pass
= aconf
->spasswd
;
765 sendto_one_numeric(client_p
, RPL_STATSILINE
,
766 form_str(RPL_STATSILINE
),
767 name
, pass
, show_iline_prefix(client_p
, aconf
, user
),
768 show_ip_conf(aconf
, client_p
) ? host
: "255.255.255.255",