]> jfr.im git - solanum.git/blobdiff - ircd/match.c
Add general::tls_ciphers_oper_only
[solanum.git] / ircd / match.c
index 63a025ea549f25ce3db50aa087fc3d848cd54f49..a8165ffb22c0b2c08f42202f94be192ca413c942 100644 (file)
@@ -18,7 +18,7 @@
  *
  */
 #include "stdinc.h"
-#include "config.h"
+#include "defaults.h"
 #include "client.h"
 #include "ircd.h"
 #include "match.h"
@@ -88,14 +88,14 @@ int match(const char *mask, const char *name)
                                  else
                                  {
                                          m_tmp = m;
-                                         for (n_tmp = n; *n && ToLower(*n) != ToLower(*m); n++);
+                                         for (n_tmp = n; *n && irctolower(*n) != irctolower(*m); n++);
                                  }
                          }
                          /* and fall through */
                  default:
                          if (!*n)
                                  return (*m != '\0' ? 0 : 1);
-                         if (ToLower(*m) != ToLower(*n))
+                         if (irctolower(*m) != irctolower(*n))
                                  goto backtrack;
                          m++;
                          n++;
@@ -160,14 +160,14 @@ int mask_match(const char *mask, const char *name)
                                  else
                                  {
                                          m_tmp = m;
-                                         for (n_tmp = n; *n && ToLower(*n) != ToLower(*m); n++);
+                                         for (n_tmp = n; *n && irctolower(*n) != irctolower(*m); n++);
                                  }
                          }
                          /* and fall through */
                  default:
                          if (!*n)
                                  return (*m != '\0' ? 0 : 1);
-                         if (ToLower(*m) != ToLower(*n))
+                         if (irctolower(*m) != irctolower(*n))
                                  goto backtrack;
                          m++;
                          n++;
@@ -281,7 +281,7 @@ match_esc(const char *mask, const char *name)
                }
 
                if(quote)
-                       match1 = *m == 's' ? *n == ' ' : ToLower(*m) == ToLower(*n);
+                       match1 = *m == 's' ? *n == ' ' : irctolower(*m) == irctolower(*n);
                else if(*m == '?')
                        match1 = 1;
                else if(*m == '@')
@@ -289,7 +289,7 @@ match_esc(const char *mask, const char *name)
                else if(*m == '#')
                        match1 = IsDigit(*n);
                else
-                       match1 = ToLower(*m) == ToLower(*n);
+                       match1 = irctolower(*m) == irctolower(*n);
                if(match1)
                {
                        if(*m)
@@ -308,13 +308,13 @@ match_esc(const char *mask, const char *name)
        return 0;
 }
 
-int comp_with_mask(void *addr, void *dest, u_int mask)
+int comp_with_mask(void *addr, void *dest, unsigned int mask)
 {
        if (memcmp(addr, dest, mask / 8) == 0)
        {
                int n = mask / 8;
-               int m = ((-1) << (8 - (mask % 8)));
-               if (mask % 8 == 0 || (((u_char *) addr)[n] & m) == (((u_char *) dest)[n] & m))
+               unsigned char m = (0xFF << (8 - (mask % 8)));
+               if (mask % 8 == 0 || (((unsigned char *) addr)[n] & m) == (((unsigned char *) dest)[n] & m))
                {
                        return (1);
                }
@@ -322,7 +322,7 @@ int comp_with_mask(void *addr, void *dest, u_int mask)
        return (0);
 }
 
-int comp_with_mask_sock(struct sockaddr *addr, struct sockaddr *dest, u_int mask)
+int comp_with_mask_sock(struct sockaddr *addr, struct sockaddr *dest, unsigned int mask)
 {
        void *iaddr = NULL;
        void *idest = NULL;
@@ -332,14 +332,12 @@ int comp_with_mask_sock(struct sockaddr *addr, struct sockaddr *dest, u_int mask
                iaddr = &((struct sockaddr_in *)(void *)addr)->sin_addr;
                idest = &((struct sockaddr_in *)(void *)dest)->sin_addr;
        }
-#ifdef RB_IPV6
        else
        {
                iaddr = &((struct sockaddr_in6 *)(void *)addr)->sin6_addr;
                idest = &((struct sockaddr_in6 *)(void *)dest)->sin6_addr;
 
        }
-#endif
 
        return (comp_with_mask(iaddr, idest, mask));
 }
@@ -358,8 +356,8 @@ int match_ips(const char *s1, const char *s2)
        void *ipptr, *maskptr;
        int cidrlen, aftype;
 
-       strcpy(mask, s1);
-       strcpy(address, s2);
+       rb_strlcpy(mask, s1, sizeof(mask));
+       rb_strlcpy(address, s2, sizeof(address));
 
        len = strrchr(mask, '/');
        if (len == NULL)
@@ -371,7 +369,6 @@ int match_ips(const char *s1, const char *s2)
        if (cidrlen <= 0)
                return 0;
 
-#ifdef RB_IPV6
        if (strchr(mask, ':') && strchr(address, ':'))
        {
                if (cidrlen > 128)
@@ -381,9 +378,7 @@ int match_ips(const char *s1, const char *s2)
                ipptr = &((struct sockaddr_in6 *)&ipaddr)->sin6_addr;
                maskptr = &((struct sockaddr_in6 *)&maskaddr)->sin6_addr;
        }
-       else
-#endif
-       if (!strchr(mask, ':') && !strchr(address, ':'))
+       else if (!strchr(mask, ':') && !strchr(address, ':'))
        {
                if (cidrlen > 32)
                        return 0;
@@ -422,8 +417,8 @@ int match_cidr(const char *s1, const char *s2)
        void *ipptr, *maskptr;
        int cidrlen, aftype;
 
-       strcpy(mask, s1);
-       strcpy(address, s2);
+       rb_strlcpy(mask, s1, sizeof(mask));
+       rb_strlcpy(address, s2, sizeof(address));
 
        ipmask = strrchr(mask, '@');
        if (ipmask == NULL)
@@ -447,7 +442,6 @@ int match_cidr(const char *s1, const char *s2)
        if (cidrlen <= 0)
                return 0;
 
-#ifdef RB_IPV6
        if (strchr(ip, ':') && strchr(ipmask, ':'))
        {
                if (cidrlen > 128)
@@ -457,9 +451,7 @@ int match_cidr(const char *s1, const char *s2)
                ipptr = &((struct sockaddr_in6 *)&ipaddr)->sin6_addr;
                maskptr = &((struct sockaddr_in6 *)&maskaddr)->sin6_addr;
        }
-       else
-#endif
-       if (!strchr(ip, ':') && !strchr(ipmask, ':'))
+       else if (!strchr(ip, ':') && !strchr(ipmask, ':'))
        {
                if (cidrlen > 32)
                        return 0;
@@ -565,7 +557,7 @@ int irccmp(const char *s1, const char *s2)
        s_assert(s1 != NULL);
        s_assert(s2 != NULL);
 
-       while ((res = ToUpper(*str1) - ToUpper(*str2)) == 0)
+       while ((res = irctoupper(*str1) - irctoupper(*str2)) == 0)
        {
                if (*str1 == '\0')
                        return 0;
@@ -583,7 +575,7 @@ int ircncmp(const char *s1, const char *s2, int n)
        s_assert(s1 != NULL);
        s_assert(s2 != NULL);
 
-       while ((res = ToUpper(*str1) - ToUpper(*str2)) == 0)
+       while ((res = irctoupper(*str1) - irctoupper(*str2)) == 0)
        {
                str1++;
                str2++;
@@ -594,7 +586,81 @@ int ircncmp(const char *s1, const char *s2, int n)
        return (res);
 }
 
-const unsigned char ToLowerTab[] = {
+void matchset_for_client(struct Client *who, struct matchset *m)
+{
+       unsigned hostn = 0;
+       unsigned ipn = 0;
+
+       struct sockaddr_in ip4;
+
+       sprintf(m->host[hostn++], "%s!%s@%s", who->name, who->username, who->host);
+
+       if (!IsIPSpoof(who))
+       {
+               sprintf(m->ip[ipn++], "%s!%s@%s", who->name, who->username, who->sockhost);
+       }
+
+       if (who->localClient->mangledhost != NULL)
+       {
+               /* if host mangling mode enabled, also check their real host */
+               if (!strcmp(who->host, who->localClient->mangledhost))
+               {
+                       sprintf(m->host[hostn++], "%s!%s@%s", who->name, who->username, who->orighost);
+               }
+               /* if host mangling mode not enabled and no other spoof,
+                * also check the mangled form of their host */
+               else if (!IsDynSpoof(who))
+               {
+                       sprintf(m->host[hostn++], "%s!%s@%s", who->name, who->username, who->localClient->mangledhost);
+               }
+       }
+       if (!IsIPSpoof(who) && GET_SS_FAMILY(&who->localClient->ip) == AF_INET6 &&
+                       rb_ipv4_from_ipv6((const struct sockaddr_in6 *)&who->localClient->ip, &ip4))
+       {
+               int n = sprintf(m->ip[ipn++], "%s!%s@", who->name, who->username);
+               rb_inet_ntop_sock((struct sockaddr *)&ip4,
+                               m->ip[ipn] + n, sizeof m->ip[ipn] - n);
+       }
+
+       for (int i = hostn; i < ARRAY_SIZE(m->host); i++)
+       {
+               m->host[i][0] = '\0';
+       }
+       for (int i = ipn; i < ARRAY_SIZE(m->ip); i++)
+       {
+               m->ip[i][0] = '\0';
+       }
+}
+
+bool client_matches_mask(struct Client *who, const char *mask)
+{
+       static struct matchset ms;
+       matchset_for_client(who, &ms);
+       return matches_mask(&ms, mask);
+}
+
+bool matches_mask(const struct matchset *m, const char *mask)
+{
+       for (int i = 0; i < ARRAY_SIZE(m->host); i++)
+       {
+               if (m->host[i][0] == '\0')
+                       break;
+               if (match(mask, m->host[i]))
+                       return true;
+       }
+       for (int i = 0; i < ARRAY_SIZE(m->ip); i++)
+       {
+               if (m->ip[i][0] == '\0')
+                       break;
+               if (match(mask, m->ip[i]))
+                       return true;
+               if (match_cidr(mask, m->ip[i]))
+                       return true;
+       }
+       return false;
+}
+
+const unsigned char irctolower_tab[] = {
        0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa,
        0xb, 0xc, 0xd, 0xe, 0xf, 0x10, 0x11, 0x12, 0x13, 0x14,
        0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
@@ -629,7 +695,7 @@ const unsigned char ToLowerTab[] = {
        0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
 };
 
-const unsigned char ToUpperTab[] = {
+const unsigned char irctoupper_tab[] = {
        0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa,
        0xb, 0xc, 0xd, 0xe, 0xf, 0x10, 0x11, 0x12, 0x13, 0x14,
        0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
@@ -670,7 +736,7 @@ const unsigned char ToUpperTab[] = {
  * NOTE: RFC 1459 sez: anything but a ^G, comma, or space is allowed
  * for channel names
  */
-const unsigned int CharAttrs[] = {
+unsigned int CharAttrs[] = {
 /* 0  */ CNTRL_C,
 /* 1  */ CNTRL_C | CHAN_C | NONEOS_C,
 /* 2  */ CNTRL_C | CHAN_C | FCHAN_C | NONEOS_C,