]>
jfr.im git - irc/rqf/shadowircd.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
28 #include "ircd_defs.h"
36 static unsigned long hash_ipv6(struct sockaddr
*, int);
38 static unsigned long hash_ipv4(struct sockaddr
*, int);
41 /* int parse_netmask(const char *, struct rb_sockaddr_storage *, int *);
42 * Input: A hostmask, or an IPV4/6 address.
43 * Output: An integer describing whether it is an IPV4, IPV6 address or a
44 * hostmask, an address(if it is an IP mask),
45 * a bitlength(if it is IP mask).
49 parse_netmask(const char *text
, struct sockaddr
*naddr
, int *nb
)
51 char *ip
= LOCAL_COPY(text
);
53 struct rb_sockaddr_storage
*addr
, xaddr
;
61 addr
= (struct rb_sockaddr_storage
*)&xaddr
;
63 addr
= (struct rb_sockaddr_storage
*)naddr
;
65 if(strpbrk(ip
, "*?") != NULL
)
72 if((ptr
= strchr(ip
, '/')))
81 if(rb_inet_pton_sock(ip
, (struct sockaddr
*)addr
) > 0)
89 if((ptr
= strchr(ip
, '/')))
98 if(rb_inet_pton_sock(ip
, (struct sockaddr
*)addr
) > 0)
106 /* Hashtable stuff...now external as its used in m_stats.c */
107 struct AddressRec
*atable
[ATABLE_SIZE
];
112 memset(&atable
, 0, sizeof(atable
));
115 /* unsigned long hash_ipv4(struct rb_sockaddr_storage*)
116 * Input: An IP address.
117 * Output: A hash value of the IP address.
121 hash_ipv4(struct sockaddr
*saddr
, int bits
)
123 struct sockaddr_in
*addr
= (struct sockaddr_in
*) saddr
;
127 unsigned long av
= ntohl(addr
->sin_addr
.s_addr
) & ~((1 << (32 - bits
)) - 1);
128 return (av
^ (av
>> 12) ^ (av
>> 24)) & (ATABLE_SIZE
- 1);
134 /* unsigned long hash_ipv6(struct rb_sockaddr_storage*)
135 * Input: An IP address.
136 * Output: A hash value of the IP address.
141 hash_ipv6(struct sockaddr
*saddr
, int bits
)
143 struct sockaddr_in6
*addr
= (struct sockaddr_in6
*) saddr
;
144 unsigned long v
= 0, n
;
145 for (n
= 0; n
< 16; n
++)
149 v
^= addr
->sin6_addr
.s6_addr
[n
];
154 v
^= addr
->sin6_addr
.s6_addr
[n
] & ~((1 << (8 - bits
)) - 1);
155 return v
& (ATABLE_SIZE
- 1);
158 return v
& (ATABLE_SIZE
- 1);
160 return v
& (ATABLE_SIZE
- 1);
164 /* int hash_text(const char *start)
165 * Input: The start of the text to hash.
166 * Output: The hash of the string between 1 and (TH_MAX-1)
167 * Side-effects: None.
170 hash_text(const char *start
)
172 const char *p
= start
;
177 h
= (h
<< 4) - (h
+ (unsigned char) ToLower(*p
++));
180 return (h
& (ATABLE_SIZE
- 1));
183 /* unsigned long get_hash_mask(const char *)
184 * Input: The text to hash.
185 * Output: The hash of the string right of the first '.' past the last
186 * wildcard in the string.
187 * Side-effects: None.
190 get_mask_hash(const char *text
)
192 const char *hp
= "", *p
;
194 for (p
= text
+ strlen(text
) - 1; p
>= text
; p
--)
195 if(*p
== '*' || *p
== '?')
196 return hash_text(hp
);
199 return hash_text(text
);
202 /* struct ConfItem* find_conf_by_address(const char*, struct rb_sockaddr_storage*,
203 * int type, int fam, const char *username)
204 * Input: The hostname, the address, the type of mask to find, the address
205 * family, the username.
206 * Output: The matching value with the highest precedence.
208 * Note: Setting bit 0 of the type means that the username is ignored.
211 find_conf_by_address(const char *name
, const char *sockhost
,
212 const char *orighost
,
213 struct sockaddr
*addr
, int type
, int fam
,
214 const char *username
, const char *auth_user
)
216 unsigned long hprecv
= 0;
217 struct ConfItem
*hprec
= NULL
;
218 struct AddressRec
*arec
;
226 /* Check for IPV6 matches... */
231 for (b
= 128; b
>= 0; b
-= 16)
233 for (arec
= atable
[hash_ipv6(addr
, b
)]; arec
; arec
= arec
->next
)
234 if(arec
->type
== (type
& ~0x1) &&
235 arec
->masktype
== HM_IPV6
&&
236 comp_with_mask_sock(addr
, (struct sockaddr
*)&arec
->Mask
.ipa
.addr
,
237 arec
->Mask
.ipa
.bits
) &&
238 (type
& 0x1 || match(arec
-> username
, username
)) &&
239 (type
!= CONF_CLIENT
|| !arec
->auth_user
||
240 (auth_user
&& match(arec
->auth_user
, auth_user
))) &&
241 arec
->precedence
> hprecv
)
243 hprecv
= arec
->precedence
;
252 for (b
= 32; b
>= 0; b
-= 8)
254 for (arec
= atable
[hash_ipv4(addr
, b
)]; arec
; arec
= arec
->next
)
255 if(arec
->type
== (type
& ~0x1) &&
256 arec
->masktype
== HM_IPV4
&&
257 comp_with_mask_sock(addr
, (struct sockaddr
*)&arec
->Mask
.ipa
.addr
,
258 arec
->Mask
.ipa
.bits
) &&
259 (type
& 0x1 || match(arec
->username
, username
)) &&
260 (type
!= CONF_CLIENT
|| !arec
->auth_user
||
261 (auth_user
&& match(arec
->auth_user
, auth_user
))) &&
262 arec
->precedence
> hprecv
)
264 hprecv
= arec
->precedence
;
275 for (p
= orighost
; p
!= NULL
;)
277 for (arec
= atable
[hash_text(p
)]; arec
; arec
= arec
->next
)
279 if((arec
->type
== (type
& ~0x1)) &&
280 (arec
->masktype
== HM_HOST
) &&
281 arec
->precedence
> hprecv
&&
282 match(arec
->Mask
.hostname
, orighost
) &&
283 (type
!= CONF_CLIENT
|| !arec
->auth_user
||
284 (auth_user
&& match(arec
->auth_user
, auth_user
))) &&
285 (type
& 0x1 || match(arec
->username
, username
)))
287 hprecv
= arec
->precedence
;
296 for (arec
= atable
[0]; arec
; arec
= arec
->next
)
298 if(arec
->type
== (type
& ~0x1) &&
299 arec
->masktype
== HM_HOST
&&
300 arec
->precedence
> hprecv
&&
301 (match(arec
->Mask
.hostname
, orighost
) ||
302 (sockhost
&& match(arec
->Mask
.hostname
, sockhost
))) &&
303 (type
!= CONF_CLIENT
|| !arec
->auth_user
||
304 (auth_user
&& match(arec
->auth_user
, auth_user
))) &&
305 (type
& 0x1 || match(arec
->username
, username
)))
307 hprecv
= arec
->precedence
;
316 /* And yes - we have to check p after strchr and p after increment for
318 for (p
= name
; p
!= NULL
;)
320 for (arec
= atable
[hash_text(p
)]; arec
; arec
= arec
->next
)
321 if((arec
->type
== (type
& ~0x1)) &&
322 (arec
->masktype
== HM_HOST
) &&
323 arec
->precedence
> hprecv
&&
324 match(arec
->Mask
.hostname
, name
) &&
325 (type
!= CONF_CLIENT
|| !arec
->auth_user
||
326 (auth_user
&& match(arec
->auth_user
, auth_user
))) &&
327 (type
& 0x1 || match(arec
->username
, username
)))
329 hprecv
= arec
->precedence
;
338 for (arec
= atable
[0]; arec
; arec
= arec
->next
)
340 if(arec
->type
== (type
& ~0x1) &&
341 arec
->masktype
== HM_HOST
&&
342 arec
->precedence
> hprecv
&&
343 (match(arec
->Mask
.hostname
, name
) ||
344 (sockhost
&& match(arec
->Mask
.hostname
, sockhost
))) &&
345 (type
!= CONF_CLIENT
|| !arec
->auth_user
||
346 (auth_user
&& match(arec
->auth_user
, auth_user
))) &&
347 (type
& 0x1 || match(arec
->username
, username
)))
349 hprecv
= arec
->precedence
;
357 /* struct ConfItem* find_address_conf(const char*, const char*,
358 * struct rb_sockaddr_storage*, int);
359 * Input: The hostname, username, address, address family.
360 * Output: The applicable ConfItem.
364 find_address_conf(const char *host
, const char *sockhost
, const char *user
,
365 const char *notildeuser
, struct sockaddr
*ip
, int aftype
, char *auth_user
)
367 struct ConfItem
*iconf
, *kconf
;
370 /* Find the best I-line... If none, return NULL -A1kmm */
371 if(!(iconf
= find_conf_by_address(host
, sockhost
, NULL
, ip
, CONF_CLIENT
, aftype
, user
, auth_user
)))
373 /* Find what their visible username will be.
374 * Note that the username without tilde may contain one char more.
376 vuser
= IsNoTilde(iconf
) ? notildeuser
: user
;
378 /* If they are exempt from K-lines, return the best I-line. -A1kmm */
379 if(IsConfExemptKline(iconf
))
382 /* Find the best K-line... -A1kmm */
383 kconf
= find_conf_by_address(host
, sockhost
, NULL
, ip
, CONF_KILL
, aftype
, user
, NULL
);
385 /* If they are K-lined, return the K-line */
389 /* if theres a spoof, check it against klines.. */
390 if(IsConfDoSpoofIp(iconf
))
392 char *p
= strchr(iconf
->info
.name
, '@');
394 /* note, we dont need to pass sockhost here, as its
395 * guaranteed to not match by whats above.. --anfl
400 kconf
= find_conf_by_address(p
+1, NULL
, NULL
, ip
, CONF_KILL
, aftype
, iconf
->info
.name
, NULL
);
404 kconf
= find_conf_by_address(iconf
->info
.name
, NULL
, NULL
, ip
, CONF_KILL
, aftype
, vuser
, NULL
);
410 /* if no_tilde, check the username without tilde against klines too
414 kconf
= find_conf_by_address(host
, sockhost
, NULL
, ip
, CONF_KILL
, aftype
, vuser
, NULL
);
422 /* struct ConfItem* find_dline(struct rb_sockaddr_storage*, int)
423 * Input: An address, an address family.
424 * Output: The best matching D-line or exempt line.
425 * Side effects: None.
428 find_dline(struct sockaddr
*addr
, int aftype
)
430 struct ConfItem
*eline
;
431 eline
= find_conf_by_address(NULL
, NULL
, NULL
, addr
, CONF_EXEMPTDLINE
| 1, aftype
, NULL
, NULL
);
434 return find_conf_by_address(NULL
, NULL
, NULL
, addr
, CONF_DLINE
| 1, aftype
, NULL
, NULL
);
437 /* void find_exact_conf_by_address(const char*, int, const char *)
439 * Output: ConfItem if found
443 find_exact_conf_by_address(const char *address
, int type
, const char *username
)
447 struct AddressRec
*arec
;
448 struct rb_sockaddr_storage addr
;
451 address
= "/NOMATCH!/";
452 masktype
= parse_netmask(address
, (struct sockaddr
*)&addr
, &bits
);
454 if(masktype
== HM_IPV6
)
456 /* We have to do this, since we do not re-hash for every bit -A1kmm. */
457 hv
= hash_ipv6((struct sockaddr
*)&addr
, bits
- bits
% 16);
461 if(masktype
== HM_IPV4
)
463 /* We have to do this, since we do not re-hash for every bit -A1kmm. */
464 hv
= hash_ipv4((struct sockaddr
*)&addr
, bits
- bits
% 8);
468 hv
= get_mask_hash(address
);
470 for (arec
= atable
[hv
]; arec
; arec
= arec
->next
)
472 if (arec
->type
== type
&&
473 arec
->masktype
== masktype
&&
474 (arec
->username
== NULL
|| username
== NULL
? arec
->username
== username
: !irccmp(arec
->username
, username
)))
476 if (masktype
== HM_HOST
)
478 if (!irccmp(arec
->Mask
.hostname
, address
))
483 if (arec
->Mask
.ipa
.bits
== bits
&&
484 comp_with_mask_sock((struct sockaddr
*)&arec
->Mask
.ipa
.addr
, (struct sockaddr
*)&addr
, bits
))
492 /* void add_conf_by_address(const char*, int, const char *,
493 * struct ConfItem *aconf)
496 * Side-effects: Adds this entry to the hash table.
499 add_conf_by_address(const char *address
, int type
, const char *username
, const char *auth_user
, struct ConfItem
*aconf
)
501 static unsigned long prec_value
= 0xFFFFFFFF;
504 struct AddressRec
*arec
;
507 address
= "/NOMATCH!/";
508 arec
= rb_malloc(sizeof(struct AddressRec
));
509 masktype
= parse_netmask(address
, (struct sockaddr
*)&arec
->Mask
.ipa
.addr
, &bits
);
510 arec
->Mask
.ipa
.bits
= bits
;
511 arec
->masktype
= masktype
;
513 if(masktype
== HM_IPV6
)
515 /* We have to do this, since we do not re-hash for every bit -A1kmm. */
517 arec
->next
= atable
[(hv
= hash_ipv6((struct sockaddr
*)&arec
->Mask
.ipa
.addr
, bits
))];
522 if(masktype
== HM_IPV4
)
524 /* We have to do this, since we do not re-hash for every bit -A1kmm. */
526 arec
->next
= atable
[(hv
= hash_ipv4((struct sockaddr
*)&arec
->Mask
.ipa
.addr
, bits
))];
531 arec
->Mask
.hostname
= address
;
532 arec
->next
= atable
[(hv
= get_mask_hash(address
))];
535 arec
->username
= username
;
536 arec
->auth_user
= auth_user
;
538 arec
->precedence
= prec_value
--;
542 /* void delete_one_address(const char*, struct ConfItem*)
543 * Input: An address string, the associated ConfItem.
545 * Side effects: Deletes an address record. Frees the ConfItem if there
546 * is nothing referencing it, sets it as illegal otherwise.
549 delete_one_address_conf(const char *address
, struct ConfItem
*aconf
)
553 struct AddressRec
*arec
, *arecl
= NULL
;
554 struct rb_sockaddr_storage addr
;
555 masktype
= parse_netmask(address
, (struct sockaddr
*)&addr
, &bits
);
557 if(masktype
== HM_IPV6
)
559 /* We have to do this, since we do not re-hash for every bit -A1kmm. */
561 hv
= hash_ipv6((struct sockaddr
*)&addr
, bits
);
565 if(masktype
== HM_IPV4
)
567 /* We have to do this, since we do not re-hash for every bit -A1kmm. */
569 hv
= hash_ipv4((struct sockaddr
*)&addr
, bits
);
572 hv
= get_mask_hash(address
);
573 for (arec
= atable
[hv
]; arec
; arec
= arec
->next
)
575 if(arec
->aconf
== aconf
)
578 arecl
->next
= arec
->next
;
580 atable
[hv
] = arec
->next
;
581 aconf
->status
|= CONF_ILLEGAL
;
591 /* void clear_out_address_conf(void)
594 * Side effects: Clears out all address records in the hash table,
595 * frees them, and frees the ConfItems if nothing references
596 * them, otherwise sets them as illegal.
599 clear_out_address_conf(void)
602 struct AddressRec
**store_next
;
603 struct AddressRec
*arec
, *arecn
;
605 for (i
= 0; i
< ATABLE_SIZE
; i
++)
607 store_next
= &atable
[i
];
608 for (arec
= atable
[i
]; arec
; arec
= arecn
)
611 /* We keep the temporary K-lines and destroy the
612 * permanent ones, just to be confusing :) -A1kmm */
613 if(arec
->aconf
->flags
& CONF_FLAGS_TEMPORARY
||
614 (arec
->type
!= CONF_CLIENT
&& arec
->type
!= CONF_EXEMPTDLINE
))
617 store_next
= &arec
->next
;
621 arec
->aconf
->status
|= CONF_ILLEGAL
;
622 if(!arec
->aconf
->clients
)
623 free_conf(arec
->aconf
);
632 clear_out_address_conf_bans(void)
635 struct AddressRec
**store_next
;
636 struct AddressRec
*arec
, *arecn
;
638 for (i
= 0; i
< ATABLE_SIZE
; i
++)
640 store_next
= &atable
[i
];
641 for (arec
= atable
[i
]; arec
; arec
= arecn
)
644 /* We keep the temporary K-lines and destroy the
645 * permanent ones, just to be confusing :) -A1kmm */
646 if(arec
->aconf
->flags
& CONF_FLAGS_TEMPORARY
||
647 (arec
->type
== CONF_CLIENT
|| arec
->type
== CONF_EXEMPTDLINE
))
650 store_next
= &arec
->next
;
654 arec
->aconf
->status
|= CONF_ILLEGAL
;
655 if(!arec
->aconf
->clients
)
656 free_conf(arec
->aconf
);
666 * show_iline_prefix()
668 * inputs - pointer to struct Client requesting output
669 * - pointer to struct ConfItem
670 * - name to which iline prefix will be prefixed to
671 * output - pointer to static string with prefixes listed in ascii form
672 * side effects - NONE
675 show_iline_prefix(struct Client
*sptr
, struct ConfItem
*aconf
, char *name
)
677 static char prefix_of_host
[USERLEN
+ 15];
680 prefix_ptr
= prefix_of_host
;
683 if(IsNeedIdentd(aconf
))
685 if(IsConfDoSpoofIp(aconf
))
687 if(IsOper(sptr
) && IsConfExemptFlood(aconf
))
689 if(IsOper(sptr
) && IsConfExemptDNSBL(aconf
) && !IsConfExemptKline(aconf
))
691 if(IsOper(sptr
) && IsConfExemptKline(aconf
))
693 if(IsOper(sptr
) && IsConfExemptLimits(aconf
))
696 strncpy(prefix_ptr
, name
, USERLEN
);
697 return (prefix_of_host
);
702 * Inputs: pointer to client to report to
704 * Side effects: Reports configured auth{} blocks to client_p
707 report_auth(struct Client
*client_p
)
709 char *name
, *host
, *pass
, *user
, *classname
;
710 struct AddressRec
*arec
;
711 struct ConfItem
*aconf
;
714 for (i
= 0; i
< ATABLE_SIZE
; i
++)
715 for (arec
= atable
[i
]; arec
; arec
= arec
->next
)
716 if(arec
->type
== CONF_CLIENT
)
720 if(!IsOper(client_p
) && IsConfDoSpoofIp(aconf
))
723 get_printable_conf(aconf
, &name
, &host
, &pass
, &user
, &port
,
726 if(!EmptyString(aconf
->spasswd
))
727 pass
= aconf
->spasswd
;
729 sendto_one_numeric(client_p
, RPL_STATSILINE
,
730 form_str(RPL_STATSILINE
),
731 name
, pass
, show_iline_prefix(client_p
, aconf
, user
),
732 show_ip_conf(aconf
, client_p
) ? host
: "255.255.255.255",
739 * inputs - Client to report to, mask
741 * side effects - Reports configured K-lines to client_p.
744 report_Klines(struct Client
*source_p
)
746 char *host
, *pass
, *user
, *oper_reason
;
747 struct AddressRec
*arec
;
748 struct ConfItem
*aconf
= NULL
;
751 for (i
= 0; i
< ATABLE_SIZE
; i
++)
753 for (arec
= atable
[i
]; arec
; arec
= arec
->next
)
755 if(arec
->type
== CONF_KILL
)
759 /* its a tempkline, theyre reported elsewhere */
760 if(aconf
->flags
& CONF_FLAGS_TEMPORARY
)
763 get_printable_kline(source_p
, aconf
, &host
, &pass
, &user
, &oper_reason
);
764 sendto_one_numeric(source_p
, RPL_STATSKLINE
,
765 form_str(RPL_STATSKLINE
),
766 'K', host
, user
, pass
,
767 oper_reason
? "|" : "",
768 oper_reason
? oper_reason
: "");