ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/hostmask.c
(Generate patch)

Comparing ircd-hybrid/trunk/src/hostmask.c (file contents):
Revision 3469 by michael, Sat May 3 17:55:53 2014 UTC vs.
Revision 6868 by michael, Mon Nov 30 20:03:49 2015 UTC

# Line 1 | Line 1
1   /*
2   *  ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3   *
4 < *  Copyright (c) 2001-2014 ircd-hybrid development team
4 > *  Copyright (c) 2001-2015 ircd-hybrid development team
5   *
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
# Line 15 | Line 15
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
18 > *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19   *  USA
20   */
21  
# Line 44 | Line 44
44                           ch = ch - 'a' + 10; \
45                         } while (0);
46  
47 + /* Hashtable stuff...now external as it's used in m_stats.c */
48 + dlink_list atable[ATABLE_SIZE];
49 +
50   /* The mask parser/type determination code... */
51  
52   /* int try_parse_v6_netmask(const char *, struct irc_ssaddr *, int *);
# Line 53 | Line 56
56   * Side effects: None
57   * Comments: Called from parse_netmask
58   */
56 /* Fixed so ::/0 (any IPv6 address) is valid
57   Also a bug in DigitParse above.
58   -Gozem 2002-07-19 gozem@linux.nu
59 */
60 #ifdef IPV6
59   static int
60   try_parse_v6_netmask(const char *text, struct irc_ssaddr *addr, int *b)
61   {
# Line 69 | Line 67 | try_parse_v6_netmask(const char *text, s
67    int bits = 128;
68    int deficit = 0;
69    short dc[8];
70 <  struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)addr;
70 >  struct sockaddr_in6 *const v6 = (struct sockaddr_in6 *)addr;
71  
72    for (const char *p = text; (c = *p); ++p)
73    {
# Line 90 | Line 88 | try_parse_v6_netmask(const char *text, s
88        }
89        else
90        {
91 <        /* If there were less than 4 hex digits, e.g. :ABC: shift right
92 <         * so we don't interpret it as ABC0 -A1kmm */
91 >        /*
92 >         * If there were less than 4 hex digits, e.g. :ABC: shift right
93 >         * so we don't interpret it as ABC0 -A1kmm
94 >         */
95          d[dp] = d[dp] >> 4 * nyble;
96          nyble = 4;
97 +
98          if (++dp >= 8)
99            return HM_HOST;
100        }
# Line 161 | Line 162 | try_parse_v6_netmask(const char *text, s
162      *b = bits;
163    return HM_IPV6;
164   }
164 #endif
165  
166   /* int try_parse_v4_netmask(const char *, struct irc_ssaddr *, int *);
167   * Input: A possible IPV4 address as a string.
# Line 177 | Line 177 | try_parse_v4_netmask(const char *text, s
177    unsigned char addb[4];
178    int n = 0, bits = 0;
179    char c;
180 <  struct sockaddr_in *v4 = (struct sockaddr_in *)addr;
180 >  struct sockaddr_in *const v4 = (struct sockaddr_in *)addr;
181  
182    digits[n++] = text;
183  
# Line 194 | Line 194 | try_parse_v4_netmask(const char *text, s
194      }
195      else if (c == '*')
196      {
197 <      if (*(p + 1) || n == 0 || *(p - 1) != '.')
197 >      if (*(p + 1) || n == 1 || *(p - 1) != '.')
198          return HM_HOST;
199  
200        bits = (n - 1) * 8;
# Line 253 | Line 253 | parse_netmask(const char *text, struct i
253   {
254    if (strchr(text, '.'))
255      return try_parse_v4_netmask(text, addr, b);
256 #ifdef IPV6
256    if (strchr(text, ':'))
257      return try_parse_v6_netmask(text, addr, b);
258 < #endif
258 >
259    return HM_HOST;
260   }
261  
# Line 266 | Line 265 | parse_netmask(const char *text, struct i
265   * Output: if match, -1 else 0
266   * Side effects: None
267   */
269 #ifdef IPV6
268   int
269   match_ipv6(const struct irc_ssaddr *addr, const struct irc_ssaddr *mask, int bits)
270   {
271    int i, m, n = bits / 8;
272 <  const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
273 <  const struct sockaddr_in6 *v6mask = (const struct sockaddr_in6 *)mask;
272 >  const struct sockaddr_in6 *const v6 = (const struct sockaddr_in6 *)addr;
273 >  const struct sockaddr_in6 *const v6mask = (const struct sockaddr_in6 *)mask;
274  
275    for (i = 0; i < n; ++i)
276      if (v6->sin6_addr.s6_addr[i] != v6mask->sin6_addr.s6_addr[i])
# Line 285 | Line 283 | match_ipv6(const struct irc_ssaddr *addr
283      return -1;
284    return 0;
285   }
288 #endif
286  
287   /* int match_ipv4(struct irc_ssaddr *, struct irc_ssaddr *, int)
288   * Input: An IP address, an IP mask, the number of bits in the mask.
# Line 295 | Line 292 | match_ipv6(const struct irc_ssaddr *addr
292   int
293   match_ipv4(const struct irc_ssaddr *addr, const struct irc_ssaddr *mask, int bits)
294   {
295 <  const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
296 <  const struct sockaddr_in *v4mask = (const struct sockaddr_in *)mask;
295 >  const struct sockaddr_in *const v4 = (const struct sockaddr_in *)addr;
296 >  const struct sockaddr_in *const v4mask = (const struct sockaddr_in *)mask;
297  
298    if ((ntohl(v4->sin_addr.s_addr) & ~((1 << (32 - bits)) - 1)) !=
299        ntohl(v4mask->sin_addr.s_addr))
# Line 316 | Line 313 | void
313   mask_addr(struct irc_ssaddr *ip, int bits)
314   {
315    int mask;
319 #ifdef IPV6
316    struct sockaddr_in6 *v6_base_ip;
317    int i, m, n;
322 #endif
318    struct sockaddr_in *v4_base_ip;
319  
320 < #ifdef IPV6
326 <  if (ip->ss.ss_family != AF_INET6)
327 < #endif
320 >  if (ip->ss.ss_family == AF_INET)
321    {
322 +    uint32_t tmp = 0;
323      v4_base_ip = (struct sockaddr_in *)ip;
324  
325      mask = ~((1 << (32 - bits)) - 1);
326 <    v4_base_ip->sin_addr.s_addr = htonl(ntohl(v4_base_ip->sin_addr.s_addr) & mask);
326 >    tmp = ntohl(v4_base_ip->sin_addr.s_addr);
327 >    v4_base_ip->sin_addr.s_addr = htonl(tmp & mask);
328    }
334 #ifdef IPV6
329    else
330    {
331      n = bits / 8;
332      m = bits % 8;
333      v6_base_ip = (struct sockaddr_in6 *)ip;
334  
335 <    mask = ~((1 << (8 - m)) -1 );
335 >    mask = ~((1 << (8 - m)) - 1);
336      v6_base_ip->sin6_addr.s6_addr[n] = v6_base_ip->sin6_addr.s6_addr[n] & mask;
337  
338      for (i = n + 1; i < 16; i++)
339        v6_base_ip->sin6_addr.s6_addr[i] = 0;
340    }
347 #endif
348 }
349
350 /* Hashtable stuff...now external as its used in m_stats.c */
351 dlink_list atable[ATABLE_SIZE];
352
353 void
354 init_host_hash(void)
355 {
356  memset(&atable, 0, sizeof(atable));
341   }
342  
343   /* unsigned long hash_ipv4(struct irc_ssaddr*)
# Line 366 | Line 350 | hash_ipv4(const struct irc_ssaddr *addr,
350   {
351    if (bits != 0)
352    {
353 <    const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
353 >    const struct sockaddr_in *const v4 = (const struct sockaddr_in *)addr;
354      uint32_t av = ntohl(v4->sin_addr.s_addr) & ~((1 << (32 - bits)) - 1);
355  
356      return (av ^ (av >> 12) ^ (av >> 24)) & (ATABLE_SIZE - 1);
# Line 380 | Line 364 | hash_ipv4(const struct irc_ssaddr *addr,
364   * Output: A hash value of the IP address.
365   * Side effects: None
366   */
383 #ifdef IPV6
367   static uint32_t
368   hash_ipv6(const struct irc_ssaddr *addr, int bits)
369   {
370    uint32_t v = 0, n;
371 <  const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
371 >  const struct sockaddr_in6 *const v6 = (const struct sockaddr_in6 *)addr;
372  
373    for (n = 0; n < 16; ++n)
374    {
# Line 402 | Line 385 | hash_ipv6(const struct irc_ssaddr *addr,
385      else
386        return v & (ATABLE_SIZE - 1);
387    }
388 +
389    return v & (ATABLE_SIZE - 1);
390   }
407 #endif
391  
392   /* int hash_text(const char *start)
393   * Input: The start of the text to hash.
# Line 452 | Line 435 | get_mask_hash(const char *text)
435   * should always be true (i.e. conf->flags & CONF_FLAGS_NEED_PASSWORD == 0)
436   */
437   struct MaskItem *
438 < find_conf_by_address(const char *name, struct irc_ssaddr *addr, unsigned int type,
438 > find_conf_by_address(const char *name, const struct irc_ssaddr *addr, unsigned int type,
439                       int fam, const char *username, const char *password, int do_match)
440   {
441    unsigned int hprecv = 0;
442 <  dlink_node *ptr = NULL;
442 >  dlink_node *node = NULL;
443    struct MaskItem *hprec = NULL;
444    struct AddressRec *arec = NULL;
445    int b;
# Line 465 | Line 448 | find_conf_by_address(const char *name, s
448    if (addr)
449    {
450      /* Check for IPV6 matches... */
468 #ifdef IPV6
451      if (fam == AF_INET6)
452      {
453        for (b = 128; b >= 0; b -= 16)
454        {
455 <        DLINK_FOREACH(ptr, atable[hash_ipv6(addr, b)].head)
455 >        DLINK_FOREACH(node, atable[hash_ipv6(addr, b)].head)
456          {
457 <          arec = ptr->data;
457 >          arec = node->data;
458  
459            if ((arec->type == type) &&
460                arec->precedence > hprecv &&
# Line 489 | Line 471 | find_conf_by_address(const char *name, s
471          }
472        }
473      }
474 <    else
493 < #endif
494 <    if (fam == AF_INET)
474 >    else if (fam == AF_INET)
475      {
476        for (b = 32; b >= 0; b -= 8)
477        {
478 <        DLINK_FOREACH(ptr, atable[hash_ipv4(addr, b)].head)
478 >        DLINK_FOREACH(node, atable[hash_ipv4(addr, b)].head)
479          {
480 <          arec = ptr->data;
480 >          arec = node->data;
481  
482            if ((arec->type == type) &&
483                arec->precedence > hprecv &&
# Line 522 | Line 502 | find_conf_by_address(const char *name, s
502  
503      while (1)
504      {
505 <        DLINK_FOREACH(ptr, atable[hash_text(p)].head)
505 >        DLINK_FOREACH(node, atable[hash_text(p)].head)
506          {
507 <          arec = ptr->data;
507 >          arec = node->data;
508            if ((arec->type == type) &&
509              arec->precedence > hprecv &&
510              (arec->masktype == HM_HOST) &&
# Line 543 | Line 523 | find_conf_by_address(const char *name, s
523        ++p;
524      }
525  
526 <    DLINK_FOREACH(ptr, atable[0].head)
526 >    DLINK_FOREACH(node, atable[0].head)
527      {
528 <      arec = ptr->data;
528 >      arec = node->data;
529  
530        if (arec->type == type &&
531            arec->precedence > hprecv &&
# Line 571 | Line 551 | find_conf_by_address(const char *name, s
551   * Side-effects: None
552   */
553   struct MaskItem *
554 < find_address_conf(const char *host, const char *user,
555 <                  struct irc_ssaddr *ip, int aftype, char *password)
554 > find_address_conf(const char *host, const char *user, const struct irc_ssaddr *ip,
555 >                  int aftype, const char *password)
556   {
557    struct MaskItem *authcnf = NULL, *killcnf = NULL;
558  
# Line 590 | Line 570 | find_address_conf(const char *host, cons
570  
571    /*
572     * If they are K-lined, return the K-line. Otherwise, return the
573 <   * auth{} block. -A1kmm
573 >   * auth {} block. -A1kmm
574     */
575    if (killcnf)
576      return killcnf;
577  
598  if (IsConfExemptGline(authcnf))
599    return authcnf;
600
601  killcnf = find_conf_by_address(host, ip, CONF_GLINE, aftype, user, NULL, 1);
602  if (killcnf)
603    return killcnf;
604
578    return authcnf;
579   }
580  
# Line 612 | Line 585 | find_address_conf(const char *host, cons
585   * Side effects: None.
586   */
587   struct MaskItem *
588 < find_dline_conf(struct irc_ssaddr *addr, int aftype)
588 > find_dline_conf(const struct irc_ssaddr *addr, int aftype)
589   {
590    struct MaskItem *eline;
591  
# Line 631 | Line 604 | find_dline_conf(struct irc_ssaddr *addr,
604   struct AddressRec *
605   add_conf_by_address(const unsigned int type, struct MaskItem *conf)
606   {
634  const char *hostname = conf->host;
635  const char *username = conf->user;
636  static unsigned int prec_value = 0xFFFFFFFF;
637  int bits = 0;
607    struct AddressRec *arec = NULL;
608 +  const char *const hostname = conf->host;
609 +  const char *const username = conf->user;
610 +  static unsigned int prec_value = UINT_MAX;
611 +  int bits = 0;
612  
613    assert(type && !EmptyString(hostname));
614  
615 <  arec = MyMalloc(sizeof(struct AddressRec));
615 >  arec = MyCalloc(sizeof(struct AddressRec));
616    arec->masktype = parse_netmask(hostname, &arec->Mask.ipa.addr, &bits);
617    arec->Mask.ipa.bits = bits;
618    arec->username = username;
# Line 654 | Line 627 | add_conf_by_address(const unsigned int t
627        bits -= bits % 8;
628        dlinkAdd(arec, &arec->node, &atable[hash_ipv4(&arec->Mask.ipa.addr, bits)]);
629        break;
657 #ifdef IPV6
630      case HM_IPV6:
631        /* We have to do this, since we do not re-hash for every bit -A1kmm. */
632        bits -= bits % 16;
633        dlinkAdd(arec, &arec->node, &atable[hash_ipv6(&arec->Mask.ipa.addr, bits)]);
634        break;
663 #endif
635      default: /* HM_HOST */
636        arec->Mask.hostname = hostname;
637        dlinkAdd(arec, &arec->node, &atable[get_mask_hash(hostname)]);
# Line 681 | Line 652 | delete_one_address_conf(const char *addr
652   {
653    int bits = 0;
654    uint32_t hv = 0;
655 <  dlink_node *ptr = NULL, *ptr_next = NULL;
655 >  dlink_node *node = NULL;
656    struct irc_ssaddr addr;
657  
658    switch (parse_netmask(address, &addr, &bits))
# Line 691 | Line 662 | delete_one_address_conf(const char *addr
662        bits -= bits % 8;
663        hv = hash_ipv4(&addr, bits);
664        break;
694 #ifdef IPV6
665      case HM_IPV6:
666        /* We have to do this, since we do not re-hash for every bit -A1kmm. */
667        bits -= bits % 16;
668        hv = hash_ipv6(&addr, bits);
669        break;
700 #endif
670      default: /* HM_HOST */
671        hv = get_mask_hash(address);
672        break;
673    }
674  
675 <  DLINK_FOREACH_SAFE(ptr, ptr_next, atable[hv].head)
675 >  DLINK_FOREACH(node, atable[hv].head)
676    {
677 <    struct AddressRec *arec = ptr->data;
677 >    struct AddressRec *arec = node->data;
678  
679      if (arec->conf == conf)
680      {
# Line 730 | Line 699 | delete_one_address_conf(const char *addr
699   void
700   clear_out_address_conf(void)
701   {
702 <  dlink_node *ptr = NULL, *ptr_next = NULL;
702 >  dlink_node *node = NULL, *node_next = NULL;
703  
704    for (unsigned int i = 0; i < ATABLE_SIZE; ++i)
705    {
706 <    DLINK_FOREACH_SAFE(ptr, ptr_next, atable[i].head)
706 >    DLINK_FOREACH_SAFE(node, node_next, atable[i].head)
707      {
708 <      struct AddressRec *arec = ptr->data;
708 >      struct AddressRec *arec = node->data;
709  
710        /*
711 <       * We keep the temporary K-lines and destroy the permanent ones,
743 <       * just to be confusing :) -A1kmm
711 >       * Destroy the ircd.conf items and keep those that are in the databases
712         */
713 <      if (arec->conf->until || IsConfDatabase(arec->conf))
713 >      if (IsConfDatabase(arec->conf))
714          continue;
715  
716        dlinkDelete(&arec->node, &atable[i]);
717 +      arec->conf->active = 0;
718  
719        if (!arec->conf->ref_count)
720          conf_free(arec->conf);
# Line 755 | Line 724 | clear_out_address_conf(void)
724   }
725  
726   static void
727 < hostmask_send_expiration(struct AddressRec *arec)
727 > hostmask_send_expiration(const struct AddressRec *const arec)
728   {
729    char ban_type = '\0';
730  
731 <  if (!ConfigFileEntry.tkline_expire_notices)
731 >  if (!ConfigGeneral.tkline_expire_notices)
732      return;
733  
734    switch (arec->type)
# Line 770 | Line 739 | hostmask_send_expiration(struct AddressR
739      case CONF_DLINE:
740        ban_type = 'D';
741        break;
773    case CONF_GLINE:
774      ban_type = 'G';
775      break;
742      default: break;
743    }
744  
745 <  sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
745 >  sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
746                         "Temporary %c-line for [%s@%s] expired", ban_type,
747                         (arec->conf->user) ? arec->conf->user : "*",
748                         (arec->conf->host) ? arec->conf->host : "*");
# Line 785 | Line 751 | hostmask_send_expiration(struct AddressR
751   void
752   hostmask_expire_temporary(void)
753   {
754 <  dlink_node *ptr = NULL, *ptr_next = NULL;
754 >  dlink_node *node = NULL, *node_next = NULL;
755  
756    for (unsigned int i = 0; i < ATABLE_SIZE; ++i)
757    {
758 <    DLINK_FOREACH_SAFE(ptr, ptr_next, atable[i].head)
758 >    DLINK_FOREACH_SAFE(node, node_next, atable[i].head)
759      {
760 <      struct AddressRec *arec = ptr->data;
760 >      struct AddressRec *arec = node->data;
761  
762        if (!arec->conf->until || arec->conf->until > CurrentTime)
763          continue;
# Line 800 | Line 766 | hostmask_expire_temporary(void)
766        {
767          case CONF_KLINE:
768          case CONF_DLINE:
803        case CONF_GLINE:
769            hostmask_send_expiration(arec);
770  
771            dlinkDelete(&arec->node, &atable[i]);

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)