another obsolete patch
[debian/sudo] / interfaces.c
index 1f1aeae7182b6890d8e1ab8a761d5807ef3a9010..502cb94ae5e4afb39b399d1056fd2a6edfd6844a 100644 (file)
@@ -1,5 +1,6 @@
 /*
- * Copyright (c) 1996, 1998-2003 Todd C. Miller <Todd.Miller@courtesan.com>
+ * Copyright (c) 1996, 1998-2005, 2007-2010
+ *     Todd C. Miller <Todd.Miller@courtesan.com>
  *
  * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
@@ -19,7 +20,7 @@
  */
 
 /*
- * Supress a warning w/ gcc on Digital UN*X.
+ * Suppress a warning w/ gcc on Digital UN*X.
  * The system headers should really do this....
  */
 #if defined(__osf__) && !defined(__cplusplus)
@@ -27,7 +28,7 @@ struct mbuf;
 struct rtentry;
 #endif
 
-#include "config.h"
+#include <config.h>
 
 #include <sys/types.h>
 #include <sys/socket.h>
@@ -51,20 +52,15 @@ struct rtentry;
 #  include <memory.h>
 # endif
 # include <string.h>
-#else
-# ifdef HAVE_STRINGS_H
-#  include <strings.h>
-# endif
 #endif /* HAVE_STRING_H */
+#ifdef HAVE_STRINGS_H
+# include <strings.h>
+#endif /* HAVE_STRINGS_H */
 #ifdef HAVE_UNISTD_H
 # include <unistd.h>
 #endif /* HAVE_UNISTD_H */
-#ifdef HAVE_ERR_H
-# include <err.h>
-#else
-# include "emul/err.h"
-#endif /* HAVE_ERR_H */
 #include <netdb.h>
+#include <errno.h>
 #ifdef _ISC
 # include <sys/stream.h>
 # include <sys/sioctl.h>
@@ -87,10 +83,10 @@ struct rtentry;
 #include "sudo.h"
 #include "interfaces.h"
 
-#ifndef lint
-static const char rcsid[] = "$Sudo: interfaces.c,v 1.72 2004/02/13 21:36:43 millert Exp $";
-#endif /* lint */
-
+/* Minix apparently lacks IFF_LOOPBACK */
+#ifndef IFF_LOOPBACK
+# define IFF_LOOPBACK  0
+#endif
 
 #ifdef HAVE_GETIFADDRS
 
@@ -102,8 +98,10 @@ void
 load_interfaces()
 {
     struct ifaddrs *ifa, *ifaddrs;
-    /* XXX - sockaddr_in6 sin6; */
     struct sockaddr_in *sin;
+#ifdef HAVE_IN6_ADDR
+    struct sockaddr_in6 *sin6;
+#endif
     int i;
 
     if (getifaddrs(&ifaddrs))
@@ -117,8 +115,10 @@ load_interfaces()
            continue;
 
        switch(ifa->ifa_addr->sa_family) {
-           /* XXX - AF_INET6 */
            case AF_INET:
+#ifdef HAVE_IN6_ADDR
+           case AF_INET6:
+#endif
                num_interfaces++;
                break;
        }
@@ -136,22 +136,42 @@ load_interfaces()
                continue;
 
        switch(ifa->ifa_addr->sa_family) {
-           /* XXX - AF_INET6 */
            case AF_INET:
                sin = (struct sockaddr_in *)ifa->ifa_addr;
+               if (sin == NULL)
+                   continue;
                memcpy(&interfaces[i].addr, &sin->sin_addr,
                    sizeof(struct in_addr));
                sin = (struct sockaddr_in *)ifa->ifa_netmask;
+               if (sin == NULL)
+                   continue;
                memcpy(&interfaces[i].netmask, &sin->sin_addr,
                    sizeof(struct in_addr));
+               interfaces[i].family = AF_INET;
                i++;
                break;
+#ifdef HAVE_IN6_ADDR
+           case AF_INET6:
+               sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
+               if (sin6 == NULL)
+                   continue;
+               memcpy(&interfaces[i].addr, &sin6->sin6_addr,
+                   sizeof(struct in6_addr));
+               sin6 = (struct sockaddr_in6 *)ifa->ifa_netmask;
+               if (sin6 == NULL)
+                   continue;
+               memcpy(&interfaces[i].netmask, &sin6->sin6_addr,
+                   sizeof(struct in6_addr));
+               interfaces[i].family = AF_INET6;
+               i++;
+               break;
+#endif /* HAVE_IN6_ADDR */
        }
     }
 #ifdef HAVE_FREEIFADDRS
     freeifaddrs(ifaddrs);
 #else
-    free(ifaddrs);
+    efree(ifaddrs);
 #endif
 }
 
@@ -176,7 +196,7 @@ load_interfaces()
 
     sock = socket(AF_INET, SOCK_DGRAM, 0);
     if (sock < 0)
-       err(1, "cannot open socket");
+       error(1, "cannot open socket");
 
     /*
      * Get interface configuration or return (leaving num_interfaces == 0)
@@ -187,14 +207,14 @@ load_interfaces()
        ifconf->ifc_len = len - sizeof(struct ifconf);
        ifconf->ifc_buf = (caddr_t) (ifconf_buf + sizeof(struct ifconf));
 
-       /* Networking may not be installed in kernel... */
 #ifdef _ISC
        STRSET(SIOCGIFCONF, (caddr_t) ifconf, len);
        if (ioctl(sock, I_STR, (caddr_t) &strioctl) < 0) {
 #else
-       if (ioctl(sock, SIOCGIFCONF, (caddr_t) ifconf) < 0) {
+       /* Note that some kernels return EINVAL if the buffer is too small */
+       if (ioctl(sock, SIOCGIFCONF, (caddr_t) ifconf) < 0 && errno != EINVAL) {
 #endif /* _ISC */
-           free(ifconf_buf);
+           efree(ifconf_buf);
            (void) close(sock);
            return;
        }
@@ -232,7 +252,7 @@ load_interfaces()
                continue;
 
 #ifdef SIOCGIFFLAGS
-       memset(&ifr_tmp, 0, sizeof(ifr_tmp));
+       zero_bytes(&ifr_tmp, sizeof(ifr_tmp));
        strncpy(ifr_tmp.ifr_name, ifr->ifr_name, sizeof(ifr_tmp.ifr_name) - 1);
        if (ioctl(sock, SIOCGIFFLAGS, (caddr_t) &ifr_tmp) < 0)
 #endif
@@ -244,13 +264,13 @@ load_interfaces()
                continue;
 
        sin = (struct sockaddr_in *) &ifr->ifr_addr;
-       interfaces[num_interfaces].addr.s_addr = sin->sin_addr.s_addr;
+       interfaces[num_interfaces].addr.ip4.s_addr = sin->sin_addr.s_addr;
 
        /* Stash the name of the interface we saved. */
        previfname = ifr->ifr_name;
 
        /* Get the netmask. */
-       (void) memset(&ifr_tmp, 0, sizeof(ifr_tmp));
+       zero_bytes(&ifr_tmp, sizeof(ifr_tmp));
        strncpy(ifr_tmp.ifr_name, ifr->ifr_name, sizeof(ifr_tmp.ifr_name) - 1);
 #ifdef SIOCGIFNETMASK
 #ifdef _ISC
@@ -261,20 +281,21 @@ load_interfaces()
 #endif /* _ISC */
            sin = (struct sockaddr_in *) &ifr_tmp.ifr_addr;
 
-           interfaces[num_interfaces].netmask.s_addr = sin->sin_addr.s_addr;
+           interfaces[num_interfaces].netmask.ip4.s_addr = sin->sin_addr.s_addr;
        } else {
 #else
        {
 #endif /* SIOCGIFNETMASK */
-           if (IN_CLASSC(interfaces[num_interfaces].addr.s_addr))
-               interfaces[num_interfaces].netmask.s_addr = htonl(IN_CLASSC_NET);
-           else if (IN_CLASSB(interfaces[num_interfaces].addr.s_addr))
-               interfaces[num_interfaces].netmask.s_addr = htonl(IN_CLASSB_NET);
+           if (IN_CLASSC(interfaces[num_interfaces].addr.ip4.s_addr))
+               interfaces[num_interfaces].netmask.ip4.s_addr = htonl(IN_CLASSC_NET);
+           else if (IN_CLASSB(interfaces[num_interfaces].addr.ip4.s_addr))
+               interfaces[num_interfaces].netmask.ip4.s_addr = htonl(IN_CLASSB_NET);
            else
-               interfaces[num_interfaces].netmask.s_addr = htonl(IN_CLASSA_NET);
+               interfaces[num_interfaces].netmask.ip4.s_addr = htonl(IN_CLASSA_NET);
        }
 
        /* Only now can we be sure it was a good/interesting interface. */
+       interfaces[num_interfaces].family = AF_INET;
        num_interfaces++;
     }
 
@@ -284,9 +305,9 @@ load_interfaces()
            interfaces = (struct interface *) erealloc3(interfaces,
                num_interfaces, sizeof(struct interface));
        else
-           free(interfaces);
+           efree(interfaces);
     }
-    free(ifconf_buf);
+    efree(ifconf_buf);
     (void) close(sock);
 }
 
@@ -307,9 +328,26 @@ void
 dump_interfaces()
 {
     int i;
+#ifdef HAVE_IN6_ADDR
+    char addrbuf[INET6_ADDRSTRLEN], maskbuf[INET6_ADDRSTRLEN];
+#endif
 
     puts("Local IP address and netmask pairs:");
-    for (i = 0; i < num_interfaces; i++)
-       printf("\t%s / 0x%x\n", inet_ntoa(interfaces[i].addr),
-           (unsigned int)ntohl(interfaces[i].netmask.s_addr));
+    for (i = 0; i < num_interfaces; i++) {
+       switch(interfaces[i].family) {
+           case AF_INET:
+               printf("\t%s / ", inet_ntoa(interfaces[i].addr.ip4));
+               puts(inet_ntoa(interfaces[i].netmask.ip4));
+               break;
+#ifdef HAVE_IN6_ADDR
+           case AF_INET6:
+               inet_ntop(AF_INET6, &interfaces[i].addr.ip6,
+                   addrbuf, sizeof(addrbuf));
+               inet_ntop(AF_INET6, &interfaces[i].netmask.ip6,
+                   maskbuf, sizeof(maskbuf));
+               printf("\t%s / %s\n", addrbuf, maskbuf);
+               break;
+#endif /* HAVE_IN6_ADDR */
+       }
+    }
 }