added F_MAXFD
[freeradius.git] / src / lib / misc.c
index e5a7144..f062ecf 100644 (file)
@@ -80,6 +80,28 @@ int fr_set_signal(int sig, sig_t func)
        return 0;
 }
 
+/** Uninstall a signal for a specific handler
+ *
+ * man sigaction says these are fine to call from a signal handler.
+ *
+ * @param sig SIGNAL
+ */
+int fr_unset_signal(int sig)
+{
+#ifdef HAVE_SIGACTION
+        struct sigaction act;
+
+        memset(&act, 0, sizeof(act));
+        act.sa_flags = 0;
+        sigemptyset(&act.sa_mask);
+        act.sa_handler = SIG_DFL;
+
+        return sigaction(sig, &act, NULL);
+#else
+        return signal(sig, SIG_DFL);
+#endif
+}
+
 static int _fr_trigger_talloc_ctx_free(fr_talloc_link_t *trigger)
 {
        if (trigger->armed) talloc_free(trigger->child);
@@ -198,6 +220,66 @@ char const *ip_ntoa(char *buffer, uint32_t ipaddr)
        return buffer;
 }
 
+/*
+ *     Parse decimal digits until we run out of decimal digits.
+ */
+static int ip_octet_from_str(char const *str, uint32_t *poctet)
+{
+       uint32_t octet;
+       char const *p = str;
+
+       if ((*p < '0') || (*p > '9')) {
+               return -1;
+       }
+
+       octet = 0;
+
+       while ((*p >= '0') && (*p <= '9')) {
+               octet *= 10;
+               octet += *p - '0';
+               p++;
+
+               if (octet > 255) return -1;
+       }
+
+
+       *poctet = octet;
+       return p - str;
+}
+
+static int ip_prefix_from_str(char const *str, uint32_t *paddr)
+{
+       int shift, length;
+       uint32_t octet;
+       uint32_t addr;
+       char const *p = str;
+
+       addr = 0;
+
+       for (shift = 24; shift >= 0; shift -= 8) {
+               length = ip_octet_from_str(p, &octet);
+               if (length <= 0) return -1;
+
+               addr |= octet << shift;
+               p += length;
+
+               /*
+                *      EOS or / means we're done.
+                */
+               if (!*p || (*p == '/')) break;
+
+               /*
+                *      We require dots between octets.
+                */
+               if (*p != '.') return -1;
+               p++;
+       }
+
+       *paddr = htonl(addr);
+       return p - str;
+}
+
+
 /** Parse an IPv4 address or IPv4 prefix in presentation format (and others)
  *
  * @param out Where to write the ip address value.
@@ -210,7 +292,7 @@ char const *ip_ntoa(char *buffer, uint32_t ipaddr)
 int fr_pton4(fr_ipaddr_t *out, char const *value, ssize_t inlen, bool resolve, bool fallback)
 {
        char *p;
-       unsigned int prefix;
+       unsigned int mask;
        char *eptr;
 
        /* Dotted quad + / + [0-9]{1,2} */
@@ -230,6 +312,7 @@ int fr_pton4(fr_ipaddr_t *out, char const *value, ssize_t inlen, bool resolve, b
        }
 
        p = strchr(value, '/');
+
        /*
         *      192.0.2.2 is parsed as if it was /32
         */
@@ -242,6 +325,7 @@ int fr_pton4(fr_ipaddr_t *out, char const *value, ssize_t inlen, bool resolve, b
                 */
                if ((value[0] == '*') && (value[1] == '\0')) {
                        out->ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
+
                /*
                 *      Convert things which are obviously integers to IP addresses
                 *
@@ -250,9 +334,10 @@ int fr_pton4(fr_ipaddr_t *out, char const *value, ssize_t inlen, bool resolve, b
                 */
                } else if (is_integer(value) || ((value[0] == '0') && (value[1] == 'x'))) {
                        out->ipaddr.ip4addr.s_addr = htonl(strtoul(value, NULL, 0));
+
                } else if (!resolve) {
                        if (inet_pton(AF_INET, value, &out->ipaddr.ip4addr.s_addr) <= 0) {
-                               fr_strerror_printf("Failed to parse IPv4 address string \"%s\"", value);
+                               fr_strerror_printf("Failed to parse IPv4 addreess string \"%s\"", value);
                                return -1;
                        }
                } else if (ip_hton(out, AF_INET, value, fallback) < 0) return -1;
@@ -261,42 +346,33 @@ int fr_pton4(fr_ipaddr_t *out, char const *value, ssize_t inlen, bool resolve, b
        }
 
        /*
-        *      Otherwise parse the prefix
-        */
-       if ((size_t)(p - value) >= INET_ADDRSTRLEN) {
-               fr_strerror_printf("Invalid IPv4 address string \"%s\"", value);
-               return -1;
-       }
-
-       /*
         *      Copy the IP portion into a temporary buffer if we haven't already.
         */
        if (inlen < 0) memcpy(buffer, value, p - value);
        buffer[p - value] = '\0';
 
-       if (!resolve) {
-               if (inet_pton(AF_INET, buffer, &out->ipaddr.ip4addr.s_addr) <= 0) {
-                       fr_strerror_printf("Failed to parse IPv4 address string \"%s\"", value);
-                       return -1;
-               }
-       } else if (ip_hton(out, AF_INET, buffer, fallback) < 0) return -1;
+       if (ip_prefix_from_str(buffer, &out->ipaddr.ip4addr.s_addr) <= 0) {
+               fr_strerror_printf("Failed to parse IPv4 address string \"%s\"", value);
+               return -1;
+       }
 
-       prefix = strtoul(p + 1, &eptr, 10);
-       if (prefix > 32) {
+       mask = strtoul(p + 1, &eptr, 10);
+       if (mask > 32) {
                fr_strerror_printf("Invalid IPv4 mask length \"%s\".  Should be between 0-32", p);
                return -1;
        }
+
        if (eptr[0] != '\0') {
                fr_strerror_printf("Failed to parse IPv4 address string \"%s\", "
                                   "got garbage after mask length \"%s\"", value, eptr);
                return -1;
        }
 
-       if (prefix < 32) {
-               out->ipaddr.ip4addr = fr_inaddr_mask(&out->ipaddr.ip4addr, prefix);
+       if (mask < 32) {
+               out->ipaddr.ip4addr = fr_inaddr_mask(&out->ipaddr.ip4addr, mask);
        }
 
-       out->prefix = (uint8_t) prefix;
+       out->prefix = (uint8_t) mask;
        out->af = AF_INET;
 
        return 0;
@@ -511,12 +587,13 @@ int fr_pton_port(fr_ipaddr_t *out, uint16_t *port_out, char const *value, ssize_
         *      Host, IPv4 or IPv6 with no port
         */
        q = memchr(p, ':', len);
-       if (!q || !memchr(p, '.', len)) return fr_pton(out, p, len, af, resolve);
+       if (!q) return fr_pton(out, p, len, af, resolve);
 
        /*
         *      IPv4 or host, with port
         */
        if (fr_pton(out, p, (q - p), af, resolve) < 0) return -1;
+
 do_port:
        /*
         *      Valid ports are a maximum of 5 digits, so if the
@@ -1240,7 +1317,7 @@ bool is_whitespace(char const *value)
        size_t  i;
 
        for (i = 0; i < len; i++) {
-               clen = fr_utf8_char(p);
+               clen = fr_utf8_char(p, len - i);
                if (clen == 0) return false;
                i += (size_t)clen;
                p += clen;
@@ -1283,19 +1360,32 @@ int closefrom(int fd)
        int i;
        int maxfd = 256;
 
+#ifdef F_CLOSEM
+       if (fcntl(fd, F_CLOSEM) == 0) {
+               return 0;
+       }
+#endif
+
+#ifdef F_MAXFD
+       maxfd = fcntl(fd, F_F_MAXFD);
+       if (maxfd >= 0) goto do_close;
+#endif
+
 #ifdef _SC_OPEN_MAX
        maxfd = sysconf(_SC_OPEN_MAX);
        if (maxfd < 0) {
-         maxfd = 256;
+               maxfd = 256;
        }
 #endif
 
+#ifdef F_MAXFD
+do_close:
+#endif
+
        if (fd > maxfd) return 0;
 
        /*
         *      FIXME: return EINTR?
-        *
-        *      Use F_CLOSEM?
         */
        for (i = fd; i < maxfd; i++) {
                close(i);