ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/hostmask.c
Revision: 3235
Committed: Sat Mar 29 19:34:16 2014 UTC (11 years, 4 months ago) by michael
Content type: text/x-csrc
File size: 21059 byte(s)
Log Message:
- Style corrections all over the place

File Contents

# User Rev Content
1 adx 30 /*
2 michael 2916 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 adx 30 *
4 michael 2916 * Copyright (c) 2001-2014 ircd-hybrid development team
5 adx 30 *
6     * This program is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19     * USA
20     */
21    
22 michael 2916 /*! \file hostmask.c
23     * \brief Code to efficiently find IP & hostmask based configs.
24     * \version $Id$
25     */
26    
27 adx 30 #include "stdinc.h"
28     #include "memory.h"
29     #include "ircd_defs.h"
30 michael 1011 #include "list.h"
31 michael 1309 #include "conf.h"
32 adx 30 #include "hostmask.h"
33     #include "send.h"
34     #include "irc_string.h"
35 michael 1369 #include "ircd.h"
36 adx 30
37    
38     #define DigitParse(ch) do { \
39     if (ch >= '0' && ch <= '9') \
40     ch = ch - '0'; \
41     else if (ch >= 'A' && ch <= 'F') \
42     ch = ch - 'A' + 10; \
43     else if (ch >= 'a' && ch <= 'f') \
44     ch = ch - 'a' + 10; \
45 michael 1298 } while (0);
46 adx 30
47     /* The mask parser/type determination code... */
48    
49     /* int try_parse_v6_netmask(const char *, struct irc_ssaddr *, int *);
50     * Input: A possible IPV6 address as a string.
51     * Output: An integer describing whether it is an IPV6 or hostmask,
52     * an address(if it is IPV6), a bitlength(if it is IPV6).
53     * Side effects: None
54     * Comments: Called from parse_netmask
55     */
56 michael 2916 /* Fixed so ::/0 (any IPv6 address) is valid
57 adx 30 Also a bug in DigitParse above.
58     -Gozem 2002-07-19 gozem@linux.nu
59     */
60     #ifdef IPV6
61     static int
62     try_parse_v6_netmask(const char *text, struct irc_ssaddr *addr, int *b)
63     {
64     const char *p;
65     char c;
66     int d[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
67     int dp = 0;
68     int nyble = 4;
69     int finsert = -1;
70     int bits = 128;
71     int deficit = 0;
72     short dc[8];
73 michael 1298 struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)addr;
74 adx 30
75 michael 3215 for (p = text; (c = *p); ++p)
76 michael 2894 {
77 adx 30 if (IsXDigit(c))
78     {
79     if (nyble == 0)
80     return HM_HOST;
81     DigitParse(c);
82     d[dp] |= c << (4 * --nyble);
83     }
84     else if (c == ':')
85     {
86     if (p > text && *(p - 1) == ':')
87     {
88     if (finsert >= 0)
89     return HM_HOST;
90     finsert = dp;
91     }
92     else
93     {
94     /* If there were less than 4 hex digits, e.g. :ABC: shift right
95     * so we don't interpret it as ABC0 -A1kmm */
96     d[dp] = d[dp] >> 4 * nyble;
97     nyble = 4;
98     if (++dp >= 8)
99     return HM_HOST;
100     }
101     }
102     else if (c == '*')
103     {
104     /* * must be last, and * is ambiguous if there is a ::... -A1kmm */
105     if (finsert >= 0 || *(p + 1) || dp == 0 || *(p - 1) != ':')
106     return HM_HOST;
107     bits = dp * 16;
108     }
109     else if (c == '/')
110     {
111     char *after;
112    
113     d[dp] = d[dp] >> 4 * nyble;
114 michael 3215 ++dp;
115 adx 30 bits = strtoul(p + 1, &after, 10);
116 michael 1298
117 adx 30 if (bits < 0 || *after)
118     return HM_HOST;
119     if (bits > dp * 4 && !(finsert >= 0 && bits <= 128))
120     return HM_HOST;
121     break;
122     }
123     else
124     return HM_HOST;
125 michael 2894 }
126 adx 30
127     d[dp] = d[dp] >> 4 * nyble;
128 michael 1298
129 adx 30 if (c == 0)
130 michael 3215 ++dp;
131 adx 30 if (finsert < 0 && bits == 0)
132     bits = dp * 16;
133 michael 1298
134 adx 30 /* How many words are missing? -A1kmm */
135     deficit = bits / 16 + ((bits % 16) ? 1 : 0) - dp;
136 michael 1298
137 adx 30 /* Now fill in the gaps(from ::) in the copied table... -A1kmm */
138 michael 3215 for (dp = 0, nyble = 0; dp < 8; ++dp)
139 adx 30 {
140     if (nyble == finsert && deficit)
141     {
142     dc[dp] = 0;
143     deficit--;
144     }
145     else
146     dc[dp] = d[nyble++];
147     }
148 michael 1298
149 adx 30 /* Set unused bits to 0... -A1kmm */
150     if (bits < 128 && (bits % 16 != 0))
151     dc[bits / 16] &= ~((1 << (15 - bits % 16)) - 1);
152 michael 3215 for (dp = bits / 16 + (bits % 16 ? 1 : 0); dp < 8; ++dp)
153 adx 30 dc[dp] = 0;
154 michael 1298
155 adx 30 /* And assign... -A1kmm */
156     if (addr)
157 michael 3215 for (dp = 0; dp < 8; ++dp)
158 adx 30 /* The cast is a kludge to make netbsd work. */
159     ((unsigned short *)&v6->sin6_addr)[dp] = htons(dc[dp]);
160 michael 1298
161 michael 3215 if (b)
162 adx 30 *b = bits;
163     return HM_IPV6;
164     }
165     #endif
166    
167     /* int try_parse_v4_netmask(const char *, struct irc_ssaddr *, int *);
168     * Input: A possible IPV4 address as a string.
169     * Output: An integer describing whether it is an IPV4 or hostmask,
170     * an address(if it is IPV4), a bitlength(if it is IPV4).
171     * Side effects: None
172     * Comments: Called from parse_netmask
173     */
174     static int
175     try_parse_v4_netmask(const char *text, struct irc_ssaddr *addr, int *b)
176     {
177     const char *p;
178     const char *digits[4];
179     unsigned char addb[4];
180     int n = 0, bits = 0;
181     char c;
182 michael 1298 struct sockaddr_in *v4 = (struct sockaddr_in *)addr;
183 adx 30
184     digits[n++] = text;
185    
186 michael 3215 for (p = text; (c = *p); ++p)
187 michael 2894 {
188 adx 30 if (c >= '0' && c <= '9') /* empty */
189     ;
190     else if (c == '.')
191     {
192     if (n >= 4)
193     return HM_HOST;
194 michael 1298
195 adx 30 digits[n++] = p + 1;
196     }
197     else if (c == '*')
198     {
199     if (*(p + 1) || n == 0 || *(p - 1) != '.')
200     return HM_HOST;
201 michael 1298
202 adx 30 bits = (n - 1) * 8;
203     break;
204     }
205     else if (c == '/')
206     {
207     char *after;
208     bits = strtoul(p + 1, &after, 10);
209 michael 1298
210 michael 1786 if (bits < 0 || *after)
211 adx 30 return HM_HOST;
212     if (bits > n * 8)
213     return HM_HOST;
214 michael 1298
215 adx 30 break;
216     }
217     else
218     return HM_HOST;
219 michael 2894 }
220 adx 30
221     if (n < 4 && bits == 0)
222     bits = n * 8;
223     if (bits)
224     while (n < 4)
225     digits[n++] = "0";
226 michael 1298
227 michael 3215 for (n = 0; n < 4; ++n)
228 adx 30 addb[n] = strtoul(digits[n], NULL, 10);
229 michael 1298
230 adx 30 if (bits == 0)
231     bits = 32;
232 michael 1298
233 adx 30 /* Set unused bits to 0... -A1kmm */
234     if (bits < 32 && bits % 8)
235     addb[bits / 8] &= ~((1 << (8 - bits % 8)) - 1);
236 michael 3215 for (n = bits / 8 + (bits % 8 ? 1 : 0); n < 4; ++n)
237 adx 30 addb[n] = 0;
238     if (addr)
239     v4->sin_addr.s_addr =
240     htonl(addb[0] << 24 | addb[1] << 16 | addb[2] << 8 | addb[3]);
241 michael 3215 if (b)
242 adx 30 *b = bits;
243     return HM_IPV4;
244     }
245    
246     /* int parse_netmask(const char *, struct irc_ssaddr *, int *);
247     * Input: A hostmask, or an IPV4/6 address.
248     * Output: An integer describing whether it is an IPV4, IPV6 address or a
249     * hostmask, an address(if it is an IP mask),
250     * a bitlength(if it is IP mask).
251     * Side effects: None
252     */
253     int
254     parse_netmask(const char *text, struct irc_ssaddr *addr, int *b)
255     {
256 michael 2814 if (strchr(text, '.'))
257     return try_parse_v4_netmask(text, addr, b);
258 adx 30 #ifdef IPV6
259 michael 1298 if (strchr(text, ':'))
260 adx 30 return try_parse_v6_netmask(text, addr, b);
261     #endif
262     return HM_HOST;
263     }
264    
265     /* The address matching stuff... */
266     /* int match_ipv6(struct irc_ssaddr *, struct irc_ssaddr *, int)
267     * Input: An IP address, an IP mask, the number of bits in the mask.
268     * Output: if match, -1 else 0
269     * Side effects: None
270     */
271     #ifdef IPV6
272     int
273 michael 1455 match_ipv6(const struct irc_ssaddr *addr, const struct irc_ssaddr *mask, int bits)
274 adx 30 {
275     int i, m, n = bits / 8;
276 michael 1455 const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
277     const struct sockaddr_in6 *v6mask = (const struct sockaddr_in6 *)mask;
278 adx 30
279 michael 3215 for (i = 0; i < n; ++i)
280 adx 30 if (v6->sin6_addr.s6_addr[i] != v6mask->sin6_addr.s6_addr[i])
281     return 0;
282 michael 3215
283 adx 30 if ((m = bits % 8) == 0)
284     return -1;
285     if ((v6->sin6_addr.s6_addr[n] & ~((1 << (8 - m)) - 1)) ==
286     v6mask->sin6_addr.s6_addr[n])
287     return -1;
288     return 0;
289     }
290     #endif
291 michael 1372
292 adx 30 /* int match_ipv4(struct irc_ssaddr *, struct irc_ssaddr *, int)
293     * Input: An IP address, an IP mask, the number of bits in the mask.
294     * Output: if match, -1 else 0
295     * Side Effects: None
296     */
297     int
298 michael 1455 match_ipv4(const struct irc_ssaddr *addr, const struct irc_ssaddr *mask, int bits)
299 adx 30 {
300 michael 1455 const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
301     const struct sockaddr_in *v4mask = (const struct sockaddr_in *)mask;
302 michael 1298
303 adx 30 if ((ntohl(v4->sin_addr.s_addr) & ~((1 << (32 - bits)) - 1)) !=
304     ntohl(v4mask->sin_addr.s_addr))
305     return 0;
306     return -1;
307     }
308    
309     /*
310     * mask_addr
311     *
312     * inputs - pointer to the ip to mask
313     * - bitlen
314     * output - NONE
315     * side effects -
316     */
317     void
318     mask_addr(struct irc_ssaddr *ip, int bits)
319     {
320     int mask;
321     #ifdef IPV6
322     struct sockaddr_in6 *v6_base_ip;
323     int i, m, n;
324     #endif
325     struct sockaddr_in *v4_base_ip;
326    
327     #ifdef IPV6
328     if (ip->ss.ss_family != AF_INET6)
329     #endif
330     {
331 michael 1298 v4_base_ip = (struct sockaddr_in *)ip;
332    
333 adx 30 mask = ~((1 << (32 - bits)) - 1);
334 michael 1298 v4_base_ip->sin_addr.s_addr = htonl(ntohl(v4_base_ip->sin_addr.s_addr) & mask);
335 adx 30 }
336     #ifdef IPV6
337     else
338     {
339     n = bits / 8;
340     m = bits % 8;
341 michael 1298 v6_base_ip = (struct sockaddr_in6 *)ip;
342 adx 30
343     mask = ~((1 << (8 - m)) -1 );
344     v6_base_ip->sin6_addr.s6_addr[n] = v6_base_ip->sin6_addr.s6_addr[n] & mask;
345 michael 1298
346 db 300 for (i = n + 1; i < 16; i++)
347     v6_base_ip->sin6_addr.s6_addr[i] = 0;
348 adx 30 }
349     #endif
350     }
351    
352     /* Hashtable stuff...now external as its used in m_stats.c */
353 michael 1367 dlink_list atable[ATABLE_SIZE];
354 adx 30
355     void
356     init_host_hash(void)
357     {
358     memset(&atable, 0, sizeof(atable));
359     }
360    
361     /* unsigned long hash_ipv4(struct irc_ssaddr*)
362     * Input: An IP address.
363     * Output: A hash value of the IP address.
364     * Side effects: None
365     */
366 michael 1343 static uint32_t
367 michael 2898 hash_ipv4(const struct irc_ssaddr *addr, int bits)
368 adx 30 {
369     if (bits != 0)
370     {
371 michael 2898 const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
372 michael 1343 uint32_t av = ntohl(v4->sin_addr.s_addr) & ~((1 << (32 - bits)) - 1);
373 michael 1298
374     return (av ^ (av >> 12) ^ (av >> 24)) & (ATABLE_SIZE - 1);
375 adx 30 }
376    
377 michael 1298 return 0;
378 adx 30 }
379    
380     /* unsigned long hash_ipv6(struct irc_ssaddr*)
381     * Input: An IP address.
382     * Output: A hash value of the IP address.
383     * Side effects: None
384     */
385     #ifdef IPV6
386 michael 1343 static uint32_t
387 michael 2898 hash_ipv6(const struct irc_ssaddr *addr, int bits)
388 adx 30 {
389 michael 1343 uint32_t v = 0, n;
390 michael 2898 const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
391 michael 1298
392 michael 3215 for (n = 0; n < 16; ++n)
393 adx 30 {
394     if (bits >= 8)
395     {
396     v ^= v6->sin6_addr.s6_addr[n];
397     bits -= 8;
398     }
399     else if (bits)
400     {
401     v ^= v6->sin6_addr.s6_addr[n] & ~((1 << (8 - bits)) - 1);
402     return v & (ATABLE_SIZE - 1);
403     }
404     else
405     return v & (ATABLE_SIZE - 1);
406     }
407     return v & (ATABLE_SIZE - 1);
408     }
409     #endif
410    
411     /* int hash_text(const char *start)
412     * Input: The start of the text to hash.
413     * Output: The hash of the string between 1 and (TH_MAX-1)
414     * Side-effects: None.
415     */
416 michael 1343 static uint32_t
417 adx 30 hash_text(const char *start)
418     {
419     const char *p = start;
420 michael 1343 uint32_t h = 0;
421 adx 30
422 michael 1367 for (; *p; ++p)
423 michael 2331 h = (h << 4) - (h + ToLower(*p));
424 michael 1298
425     return h & (ATABLE_SIZE - 1);
426 adx 30 }
427    
428     /* unsigned long get_hash_mask(const char *)
429     * Input: The text to hash.
430     * Output: The hash of the string right of the first '.' past the last
431     * wildcard in the string.
432     * Side-effects: None.
433     */
434 michael 1343 static uint32_t
435 adx 30 get_mask_hash(const char *text)
436     {
437     const char *hp = "", *p;
438    
439 michael 3215 for (p = text + strlen(text) - 1; p >= text; --p)
440 adx 723 if (IsMWildChar(*p))
441 adx 30 return hash_text(hp);
442     else if (*p == '.')
443     hp = p + 1;
444     return hash_text(text);
445     }
446    
447 michael 1632 /* struct MaskItem *find_conf_by_address(const char *, struct irc_ssaddr *,
448 adx 30 * int type, int fam, const char *username)
449     * Input: The hostname, the address, the type of mask to find, the address
450     * family, the username.
451     * Output: The matching value with the highest precedence.
452     * Side-effects: None
453     * Note: Setting bit 0 of the type means that the username is ignored.
454     * Warning: IsNeedPassword for everything that is not an auth{} entry
455 michael 1632 * should always be true (i.e. conf->flags & CONF_FLAGS_NEED_PASSWORD == 0)
456 adx 30 */
457 michael 1632 struct MaskItem *
458 michael 1369 find_conf_by_address(const char *name, struct irc_ssaddr *addr, unsigned int type,
459 michael 1371 int fam, const char *username, const char *password, int do_match)
460 adx 30 {
461 michael 1343 unsigned int hprecv = 0;
462 michael 1367 dlink_node *ptr = NULL;
463 michael 1632 struct MaskItem *hprec = NULL;
464     struct AddressRec *arec = NULL;
465 adx 30 int b;
466 michael 1371 int (*cmpfunc)(const char *, const char *) = do_match ? match : irccmp;
467 adx 30
468     if (addr)
469     {
470     /* Check for IPV6 matches... */
471     #ifdef IPV6
472     if (fam == AF_INET6)
473     {
474     for (b = 128; b >= 0; b -= 16)
475     {
476 michael 1367 DLINK_FOREACH(ptr, atable[hash_ipv6(addr, b)].head)
477     {
478 michael 2916 arec = ptr->data;
479 michael 1367
480 michael 2995 if ((arec->type == type) &&
481 adx 30 arec->precedence > hprecv &&
482     arec->masktype == HM_IPV6 &&
483     match_ipv6(addr, &arec->Mask.ipa.addr,
484     arec->Mask.ipa.bits) &&
485 michael 2995 (!username || !cmpfunc(arec->username, username)) &&
486 michael 2916 (IsNeedPassword(arec->conf) || arec->conf->passwd == NULL ||
487     match_conf_password(password, arec->conf)))
488 adx 30 {
489     hprecv = arec->precedence;
490 michael 1632 hprec = arec->conf;
491 adx 30 }
492 michael 1367 }
493 adx 30 }
494     }
495     else
496     #endif
497     if (fam == AF_INET)
498     {
499     for (b = 32; b >= 0; b -= 8)
500     {
501 michael 1367 DLINK_FOREACH(ptr, atable[hash_ipv4(addr, b)].head)
502     {
503     arec = ptr->data;
504    
505 michael 2995 if ((arec->type == type) &&
506 adx 30 arec->precedence > hprecv &&
507     arec->masktype == HM_IPV4 &&
508     match_ipv4(addr, &arec->Mask.ipa.addr,
509     arec->Mask.ipa.bits) &&
510 michael 2995 (!username || !cmpfunc(arec->username, username)) &&
511 michael 2916 (IsNeedPassword(arec->conf) || arec->conf->passwd == NULL ||
512     match_conf_password(password, arec->conf)))
513 adx 30 {
514     hprecv = arec->precedence;
515 michael 1632 hprec = arec->conf;
516 adx 30 }
517 michael 1367 }
518 adx 30 }
519     }
520     }
521    
522 michael 3215 if (name)
523 adx 30 {
524     const char *p = name;
525    
526     while (1)
527     {
528 michael 1367 DLINK_FOREACH(ptr, atable[hash_text(p)].head)
529     {
530     arec = ptr->data;
531 michael 2995 if ((arec->type == type) &&
532 adx 30 arec->precedence > hprecv &&
533     (arec->masktype == HM_HOST) &&
534 michael 1652 !cmpfunc(arec->Mask.hostname, name) &&
535 michael 2995 (!username || !cmpfunc(arec->username, username)) &&
536 michael 1632 (IsNeedPassword(arec->conf) || arec->conf->passwd == NULL ||
537     match_conf_password(password, arec->conf)))
538 adx 30 {
539     hprecv = arec->precedence;
540 michael 1632 hprec = arec->conf;
541 adx 30 }
542 michael 1367 }
543 michael 3215
544     if ((p = strchr(p, '.')) == NULL)
545 adx 30 break;
546 michael 3215 ++p;
547 adx 30 }
548 michael 1367
549     DLINK_FOREACH(ptr, atable[0].head)
550     {
551     arec = ptr->data;
552    
553 michael 2995 if (arec->type == type &&
554 adx 30 arec->precedence > hprecv &&
555     arec->masktype == HM_HOST &&
556 michael 1652 !cmpfunc(arec->Mask.hostname, name) &&
557 michael 2995 (!username || !cmpfunc(arec->username, username)) &&
558 michael 1632 (IsNeedPassword(arec->conf) || arec->conf->passwd == NULL ||
559     match_conf_password(password, arec->conf)))
560 adx 30 {
561     hprecv = arec->precedence;
562 michael 1632 hprec = arec->conf;
563 adx 30 }
564 michael 1367 }
565 adx 30 }
566    
567     return hprec;
568     }
569    
570 michael 1632 /* struct MaskItem* find_address_conf(const char*, const char*,
571 adx 30 * struct irc_ssaddr*, int, char *);
572     * Input: The hostname, username, address, address family.
573 michael 1632 * Output: The applicable MaskItem.
574 adx 30 * Side-effects: None
575     */
576 michael 1632 struct MaskItem *
577 adx 30 find_address_conf(const char *host, const char *user,
578     struct irc_ssaddr *ip, int aftype, char *password)
579     {
580 michael 1632 struct MaskItem *authcnf = NULL, *killcnf = NULL;
581 adx 30
582 michael 1426 /* Find the best auth{} block... If none, return NULL -A1kmm */
583 michael 1632 if ((authcnf = find_conf_by_address(host, ip, CONF_CLIENT, aftype, user,
584     password, 1)) == NULL)
585 michael 1298 return NULL;
586 adx 30
587 michael 1426 /* If they are exempt from K-lines, return the best auth{} block. -A1kmm */
588 michael 1632 if (IsConfExemptKline(authcnf))
589     return authcnf;
590 adx 30
591     /* Find the best K-line... -A1kmm */
592 michael 1632 killcnf = find_conf_by_address(host, ip, CONF_KLINE, aftype, user, NULL, 1);
593 adx 30
594 michael 1426 /*
595     * If they are K-lined, return the K-line. Otherwise, return the
596     * auth{} block. -A1kmm
597     */
598 michael 3215 if (killcnf)
599 michael 1632 return killcnf;
600 adx 30
601 michael 1632 if (IsConfExemptGline(authcnf))
602     return authcnf;
603 adx 30
604 michael 1632 killcnf = find_conf_by_address(host, ip, CONF_GLINE, aftype, user, NULL, 1);
605 michael 3215 if (killcnf)
606 michael 1632 return killcnf;
607    
608     return authcnf;
609 adx 30 }
610    
611 michael 1632 /* struct MaskItem* find_dline_conf(struct irc_ssaddr*, int)
612 adx 30 *
613     * Input: An address, an address family.
614     * Output: The best matching D-line or exempt line.
615     * Side effects: None.
616     */
617 michael 1632 struct MaskItem *
618 adx 30 find_dline_conf(struct irc_ssaddr *addr, int aftype)
619     {
620 michael 1632 struct MaskItem *eline;
621 adx 30
622 michael 2995 eline = find_conf_by_address(NULL, addr, CONF_EXEMPT, aftype, NULL, NULL, 1);
623 michael 3215 if (eline)
624 michael 1298 return eline;
625    
626 michael 2995 return find_conf_by_address(NULL, addr, CONF_DLINE, aftype, NULL, NULL, 1);
627 adx 30 }
628    
629 michael 1632 /* void add_conf_by_address(int, struct MaskItem *aconf)
630 michael 2916 * Input:
631 adx 30 * Output: None
632     * Side-effects: Adds this entry to the hash table.
633     */
634 michael 2811 struct AddressRec *
635 michael 1632 add_conf_by_address(const unsigned int type, struct MaskItem *conf)
636 adx 30 {
637 michael 3001 const char *hostname = conf->host;
638     const char *username = conf->user;
639 michael 1343 static unsigned int prec_value = 0xFFFFFFFF;
640 michael 1367 int bits = 0;
641 michael 3001 struct AddressRec *arec = NULL;
642 adx 30
643 michael 3003 assert(type && !EmptyString(hostname));
644 adx 30
645     arec = MyMalloc(sizeof(struct AddressRec));
646 michael 3001 arec->masktype = parse_netmask(hostname, &arec->Mask.ipa.addr, &bits);
647 adx 30 arec->Mask.ipa.bits = bits;
648 michael 1367 arec->username = username;
649 michael 1632 arec->conf = conf;
650 michael 1367 arec->precedence = prec_value--;
651     arec->type = type;
652 michael 1298
653 michael 1367 switch (arec->masktype)
654     {
655     case HM_IPV4:
656     /* We have to do this, since we do not re-hash for every bit -A1kmm. */
657     bits -= bits % 8;
658     dlinkAdd(arec, &arec->node, &atable[hash_ipv4(&arec->Mask.ipa.addr, bits)]);
659     break;
660 adx 30 #ifdef IPV6
661 michael 1367 case HM_IPV6:
662     /* We have to do this, since we do not re-hash for every bit -A1kmm. */
663     bits -= bits % 16;
664     dlinkAdd(arec, &arec->node, &atable[hash_ipv6(&arec->Mask.ipa.addr, bits)]);
665     break;
666 adx 30 #endif
667 michael 1367 default: /* HM_HOST */
668 michael 3001 arec->Mask.hostname = hostname;
669     dlinkAdd(arec, &arec->node, &atable[get_mask_hash(hostname)]);
670 michael 1367 break;
671 adx 30 }
672 michael 2811
673     return arec;
674 adx 30 }
675    
676 michael 1632 /* void delete_one_address(const char*, struct MaskItem*)
677     * Input: An address string, the associated MaskItem.
678 adx 30 * Output: None
679 michael 1632 * Side effects: Deletes an address record. Frees the MaskItem if there
680 adx 30 * is nothing referencing it, sets it as illegal otherwise.
681     */
682     void
683 michael 1632 delete_one_address_conf(const char *address, struct MaskItem *conf)
684 adx 30 {
685 michael 1367 int bits = 0;
686     uint32_t hv = 0;
687     dlink_node *ptr = NULL, *ptr_next = NULL;
688 adx 30 struct irc_ssaddr addr;
689 michael 1298
690 michael 1367 switch (parse_netmask(address, &addr, &bits))
691     {
692     case HM_IPV4:
693     /* We have to do this, since we do not re-hash for every bit -A1kmm. */
694     bits -= bits % 8;
695     hv = hash_ipv4(&addr, bits);
696     break;
697 adx 30 #ifdef IPV6
698 michael 1367 case HM_IPV6:
699     /* We have to do this, since we do not re-hash for every bit -A1kmm. */
700     bits -= bits % 16;
701     hv = hash_ipv6(&addr, bits);
702     break;
703 adx 30 #endif
704 michael 1367 default: /* HM_HOST */
705     hv = get_mask_hash(address);
706     break;
707 adx 30 }
708 michael 1298
709 michael 1367 DLINK_FOREACH_SAFE(ptr, ptr_next, atable[hv].head)
710 adx 30 {
711 michael 1367 struct AddressRec *arec = ptr->data;
712    
713 michael 1632 if (arec->conf == conf)
714 adx 30 {
715 michael 1367 dlinkDelete(&arec->node, &atable[hv]);
716 michael 1298
717 michael 1644 if (!conf->ref_count)
718 michael 1632 conf_free(conf);
719 michael 1298
720 adx 30 MyFree(arec);
721     return;
722     }
723     }
724     }
725    
726     /* void clear_out_address_conf(void)
727     * Input: None
728     * Output: None
729     * Side effects: Clears out all address records in the hash table,
730 michael 1632 * frees them, and frees the MaskItems if nothing references
731 michael 2916 * them, otherwise sets them as illegal.
732 adx 30 */
733     void
734     clear_out_address_conf(void)
735     {
736 michael 1367 dlink_node *ptr = NULL, *ptr_next = NULL;
737 michael 2916
738 michael 3235 for (unsigned int i = 0; i < ATABLE_SIZE; ++i)
739 adx 30 {
740 michael 1367 DLINK_FOREACH_SAFE(ptr, ptr_next, atable[i].head)
741 adx 30 {
742 michael 1367 struct AddressRec *arec = ptr->data;
743    
744 michael 1628 /*
745     * We keep the temporary K-lines and destroy the permanent ones,
746     * just to be confusing :) -A1kmm
747     */
748 michael 1649 if (arec->conf->until || IsConfDatabase(arec->conf))
749 michael 1628 continue;
750 michael 1622
751 michael 1628 dlinkDelete(&arec->node, &atable[i]);
752 michael 1298
753 michael 1644 if (!arec->conf->ref_count)
754 michael 1632 conf_free(arec->conf);
755 michael 1628 MyFree(arec);
756 adx 30 }
757     }
758     }
759    
760 michael 1369 static void
761     hostmask_send_expiration(struct AddressRec *arec)
762     {
763     char ban_type = '\0';
764    
765     if (!ConfigFileEntry.tkline_expire_notices)
766     return;
767    
768     switch (arec->type)
769     {
770     case CONF_KLINE:
771     ban_type = 'K';
772     break;
773     case CONF_DLINE:
774     ban_type = 'D';
775     break;
776     case CONF_GLINE:
777     ban_type = 'G';
778     break;
779 michael 1644 default: break;
780 michael 1369 }
781 michael 2916
782 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
783 michael 1369 "Temporary %c-line for [%s@%s] expired", ban_type,
784 michael 1632 (arec->conf->user) ? arec->conf->user : "*",
785     (arec->conf->host) ? arec->conf->host : "*");
786 michael 1369 }
787    
788     void
789     hostmask_expire_temporary(void)
790     {
791     dlink_node *ptr = NULL, *ptr_next = NULL;
792    
793 michael 3235 for (unsigned int i = 0; i < ATABLE_SIZE; ++i)
794 michael 1369 {
795     DLINK_FOREACH_SAFE(ptr, ptr_next, atable[i].head)
796     {
797     struct AddressRec *arec = ptr->data;
798    
799 michael 1649 if (!arec->conf->until || arec->conf->until > CurrentTime)
800 michael 1369 continue;
801    
802     switch (arec->type)
803     {
804     case CONF_KLINE:
805     case CONF_DLINE:
806     case CONF_GLINE:
807     hostmask_send_expiration(arec);
808    
809     dlinkDelete(&arec->node, &atable[i]);
810 michael 1632 conf_free(arec->conf);
811 michael 1369 MyFree(arec);
812     break;
813 michael 1644 default: break;
814 michael 1369 }
815     }
816     }
817     }

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision