]> jfr.im git - solanum.git/blobdiff - ircd/hostmask.c
Support more human friendly k/d/x-line duration format
[solanum.git] / ircd / hostmask.c
index 922a8d44b5b3c32bd6523c0235b7bd3ce3ab8455..6b50b5e56b44d5f260e955df37b69bf12dff048f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  charybdis: an advanced internet relay chat daemon (ircd).
+ *  Solanum: a slightly advanced ircd
  *  hostmask.c: Code to efficiently find IP & hostmask based configs.
  *
  *  Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
@@ -36,18 +36,12 @@ static unsigned long hash_ipv6(struct sockaddr *, int);
 static unsigned long hash_ipv4(struct sockaddr *, int);
 
 
-/* int parse_netmask(const char *, struct rb_sockaddr_storage *, int *);
- * Input: A hostmask, or an IPV4/6 address.
- * Output: An integer describing whether it is an IPV4, IPV6 address or a
- *         hostmask, an address(if it is an IP mask),
- *         a bitlength(if it is IP mask).
- * Side effects: None
- */
-int
-parse_netmask(const char *text, struct rb_sockaddr_storage *naddr, int *nb)
+static int
+_parse_netmask(const char *text, struct rb_sockaddr_storage *naddr, int *nb, bool strict)
 {
        char *ip = LOCAL_COPY(text);
        char *ptr;
+       char *endp;
        struct rb_sockaddr_storage *addr, xaddr;
        int *b, xb;
        if(nb == NULL)
@@ -70,11 +64,17 @@ parse_netmask(const char *text, struct rb_sockaddr_storage *naddr, int *nb)
                {
                        *ptr = '\0';
                        ptr++;
-                       *b = atoi(ptr);
-                       if(*b > 128)
-                               *b = 128;
-                       else if(*b < 0)
+                       long n = strtol(ptr, &endp, 10);
+                       if (endp == ptr || n < 0)
                                return HM_HOST;
+                       if (n > 128 || *endp != '\0')
+                       {
+                               if (strict)
+                                       return HM_ERROR;
+                               else
+                                       n = 128;
+                       }
+                       *b = n;
                } else
                        *b = 128;
                if(rb_inet_pton_sock(ip, addr) > 0)
@@ -88,11 +88,17 @@ parse_netmask(const char *text, struct rb_sockaddr_storage *naddr, int *nb)
                {
                        *ptr = '\0';
                        ptr++;
-                       *b = atoi(ptr);
-                       if(*b > 32)
-                               *b = 32;
-                       else if(*b < 0)
+                       long n = strtol(ptr, &endp, 10);
+                       if (endp == ptr || n < 0)
                                return HM_HOST;
+                       if (n > 32 || *endp != '\0')
+                       {
+                               if (strict)
+                                       return HM_ERROR;
+                               else
+                                       n = 32;
+                       }
+                       *b = n;
                } else
                        *b = 32;
                if(rb_inet_pton_sock(ip, addr) > 0)
@@ -103,6 +109,23 @@ parse_netmask(const char *text, struct rb_sockaddr_storage *naddr, int *nb)
        return HM_HOST;
 }
 
+/* int parse_netmask(const char *, struct rb_sockaddr_storage *, int *);
+ * Input: A hostmask, or an IPV4/6 address.
+ * Output: An integer describing whether it is an IPV4, IPV6 address or a
+ *         hostmask, an address(if it is an IP mask),
+ *         a bitlength(if it is IP mask).
+ * Side effects: None
+ */
+int parse_netmask(const char *mask, struct rb_sockaddr_storage *addr, int *blen)
+{
+       return _parse_netmask(mask, addr, blen, false);
+}
+
+int parse_netmask_strict(const char *mask, struct rb_sockaddr_storage *addr, int *blen)
+{
+       return _parse_netmask(mask, addr, blen, true);
+}
+
 /* Hashtable stuff...now external as its used in m_stats.c */
 struct AddressRec *atable[ATABLE_SIZE];
 
@@ -455,13 +478,8 @@ find_dline(struct sockaddr *addr, int aftype)
        return NULL;
 }
 
-/* void find_exact_conf_by_address(const char*, int, const char *)
- * Input:
- * Output: ConfItem if found
- * Side-effects: None
- */
 struct ConfItem *
-find_exact_conf_by_address(const char *address, int type, const char *username)
+find_exact_conf_by_address_filtered(const char *address, int type, const char *username, bool (*filter)(struct ConfItem *))
 {
        int masktype, bits;
        unsigned long hv;
@@ -491,6 +509,9 @@ find_exact_conf_by_address(const char *address, int type, const char *username)
                                arec->masktype == masktype &&
                                (arec->username == NULL || username == NULL ? arec->username == username : !irccmp(arec->username, username)))
                {
+                       if (filter && !filter(arec->aconf))
+                               continue;
+
                        if (masktype == HM_HOST)
                        {
                                if (!irccmp(arec->Mask.hostname, address))
@@ -507,6 +528,17 @@ find_exact_conf_by_address(const char *address, int type, const char *username)
        return NULL;
 }
 
+/* void find_exact_conf_by_address(const char*, int, const char *)
+ * Input:
+ * Output: ConfItem if found
+ * Side-effects: None
+ */
+struct ConfItem *
+find_exact_conf_by_address(const char *address, int type, const char *username)
+{
+       return find_exact_conf_by_address_filtered(address, type, username, NULL);
+}
+
 /* void add_conf_by_address(const char*, int, const char *,
  *         struct ConfItem *aconf)
  * Input:
@@ -608,7 +640,7 @@ delete_one_address_conf(const char *address, struct ConfItem *aconf)
  *               them, otherwise sets them as illegal.
  */
 void
-clear_out_address_conf(void)
+clear_out_address_conf(enum aconf_category clear_type)
 {
        int i;
        struct AddressRec **store_next;
@@ -619,44 +651,17 @@ clear_out_address_conf(void)
                store_next = &atable[i];
                for (arec = atable[i]; arec; arec = arecn)
                {
+                       enum aconf_category cur_type;
                        arecn = arec->next;
-                       /* We keep the temporary K-lines and destroy the
-                        * permanent ones, just to be confusing :) -A1kmm */
-                       if(arec->aconf->flags & CONF_FLAGS_TEMPORARY ||
-                          (arec->type != CONF_CLIENT && arec->type != CONF_EXEMPTDLINE))
-                       {
-                               *store_next = arec;
-                               store_next = &arec->next;
-                       }
-                       else
-                       {
-                               arec->aconf->status |= CONF_ILLEGAL;
-                               if(!arec->aconf->clients)
-                                       free_conf(arec->aconf);
-                               rb_free(arec);
-                       }
-               }
-               *store_next = NULL;
-       }
-}
 
-void
-clear_out_address_conf_bans(void)
-{
-       int i;
-       struct AddressRec **store_next;
-       struct AddressRec *arec, *arecn;
+                       if (arec->type == CONF_CLIENT || arec->type == CONF_EXEMPTDLINE || arec->type == CONF_SECURE)
+                               cur_type = AC_CONFIG;
+                       else
+                               cur_type = AC_BANDB;
 
-       for (i = 0; i < ATABLE_SIZE; i++)
-       {
-               store_next = &atable[i];
-               for (arec = atable[i]; arec; arec = arecn)
-               {
-                       arecn = arec->next;
                        /* We keep the temporary K-lines and destroy the
                         * permanent ones, just to be confusing :) -A1kmm */
-                       if(arec->aconf->flags & CONF_FLAGS_TEMPORARY ||
-                          (arec->type == CONF_CLIENT || arec->type == CONF_EXEMPTDLINE))
+                       if (arec->aconf->flags & CONF_FLAGS_TEMPORARY || cur_type != clear_type)
                        {
                                *store_next = arec;
                                store_next = &arec->next;
@@ -673,7 +678,6 @@ clear_out_address_conf_bans(void)
        }
 }
 
-
 /*
  * show_iline_prefix()
  *
@@ -696,6 +700,8 @@ show_iline_prefix(struct Client *sptr, struct ConfItem *aconf, char *name)
                *prefix_ptr++ = '+';
        if(IsConfDoSpoofIp(aconf))
                *prefix_ptr++ = '=';
+       if(IsNeedSasl(aconf))
+               *prefix_ptr++ = '%';
        if(IsOper(sptr) && IsConfExemptFlood(aconf))
                *prefix_ptr++ = '|';
        if(IsOper(sptr) && IsConfExemptDNSBL(aconf) && !IsConfExemptKline(aconf))
@@ -704,8 +710,7 @@ show_iline_prefix(struct Client *sptr, struct ConfItem *aconf, char *name)
                *prefix_ptr++ = '^';
        if(IsOper(sptr) && IsConfExemptLimits(aconf))
                *prefix_ptr++ = '>';
-       *prefix_ptr = '\0';
-       strncpy(prefix_ptr, name, USERLEN);
+       rb_strlcpy(prefix_ptr, name, USERLEN + 1);
        return (prefix_of_host);
 }