]> granicus.if.org Git - sudo/commitdiff
Add support for using getifaddrs() to get the list of ip addr / netmask
authorTodd C. Miller <Todd.Miller@courtesan.com>
Sun, 4 Jun 2000 23:57:22 +0000 (23:57 +0000)
committerTodd C. Miller <Todd.Miller@courtesan.com>
Sun, 4 Jun 2000 23:57:22 +0000 (23:57 +0000)
pairs.  Currently IPv4-only.

config.h.in
interfaces.c
interfaces.h
sudo.c

index 1c8d7a738271b28179a7ed4baf0c76706ac429bb..5c5004ecc34af66dafb97334ae462bcc2b5da709 100644 (file)
 /* Define if you have vasprintf(3).  */
 #undef HAVE_VASPRINTF
 
+/* Define if you have getifaddrs(3).  */
+#undef HAVE_GETIFADDRS
+
 /* Define if you have the <malloc.h> header file.  */
 #undef HAVE_MALLOC_H
 
index 7a5bf9b7afeb1dce2eba303860710cb3d42cd4d5..b58a654d9b0808740d95678f2a7ff40e8091a532 100644 (file)
@@ -82,6 +82,9 @@ struct rtentry;
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <net/if.h>
+#ifdef HAVE_GETIFADDRS
+#include <ifaddrs.h>
+#endif
 
 #include "sudo.h"
 #include "interfaces.h"
@@ -91,7 +94,65 @@ static const char rcsid[] = "$Sudo$";
 #endif /* lint */
 
 
-#if defined(SIOCGIFCONF) && !defined(STUB_LOAD_INTERFACES)
+#ifdef HAVE_GETIFADDRS
+
+/*
+ * Allocate and fill in the interfaces global variable with the
+ * machine's ip addresses and netmasks.
+ */
+void
+load_interfaces()
+{
+    struct ifaddrs *ifa, *ifaddrs;
+    /* XXX - sockaddr_in6 sin6; */
+    struct sockaddr_in *sin;
+    int i;
+
+    if (getifaddrs(&ifaddrs))
+       return;
+
+    /* Allocate space for the interfaces list. */
+    for (ifa = ifaddrs; ifa -> ifa_next; ifa = ifa -> ifa_next) {
+       /* Skip interfaces marked "down" and "loopback". */
+       if (ifa->ifa_addr == NULL || !(ifa->ifa_flags & IFF_UP) ||
+           (ifa->ifa_flags & IFF_LOOPBACK))
+           continue;
+
+       switch(ifa->ifa_addr->sa_family) {
+           /* XXX - AF_INET6 */
+           case AF_INET:
+               num_interfaces++;
+               break;
+       }
+    }
+    interfaces =
+       (struct interface *) emalloc(sizeof(struct interface) * num_interfaces);
+
+    /* Store the ip addr / netmask pairs. */
+    for (ifa = ifaddrs, i = 0; ifa -> ifa_next; ifa = ifa -> ifa_next) {
+       /* Skip interfaces marked "down" and "loopback". */
+       if (ifa->ifa_addr == NULL || !(ifa->ifa_flags & IFF_UP) ||
+           (ifa->ifa_flags & IFF_LOOPBACK))
+               continue;
+
+       switch(ifa->ifa_addr->sa_family) {
+           /* XXX - AF_INET6 */
+           case AF_INET:
+               sin = (struct sockaddr_in *)ifa->ifa_addr;
+               memcpy(&interfaces[i].addr, &sin->sin_addr,
+                   sizeof(struct in_addr));
+               sin = (struct sockaddr_in *)ifa->ifa_netmask;
+               memcpy(&interfaces[i].netmask, &sin->sin_addr,
+                   sizeof(struct in_addr));
+               i++;
+               break;
+       }
+    }
+    freeifaddrs(ifaddrs);
+}
+
+#elif defined(SIOCGIFCONF) && !defined(STUB_LOAD_INTERFACES)
+
 /*
  * Allocate and fill in the interfaces global variable with the
  * machine's ip addresses and netmasks.
@@ -238,3 +299,14 @@ load_interfaces()
 }
 
 #endif /* SIOCGIFCONF && !STUB_LOAD_INTERFACES */
+
+void
+dump_interfaces()
+{
+    int i;
+
+    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),
+           ntohl(interfaces[i].netmask.s_addr));
+}
index f6453eed6972854377d084ce962fb4ccdd8f4db0..f6cd892f9a937fc556c2ff00b6c515dc61d12d65 100644 (file)
@@ -49,6 +49,7 @@ struct interface {
  * Prototypes for external functions.
  */
 void load_interfaces   __P((void));
+void dump_interfaces   __P((void));
 
 /*
  * Definitions for external variables.
diff --git a/sudo.c b/sudo.c
index 903e742c2da672655b9f5a2dcf2a42c186be8bcc..4233d6de1e4aeb904202b9dfc107d1b3f3e94e45 100644 (file)
--- a/sudo.c
+++ b/sudo.c
@@ -234,6 +234,9 @@ main(argc, argv)
     /* Setup defaults data structures. */
     init_defaults();
 
+    /* Load the list of local ip addresses and netmasks.  */
+    load_interfaces();
+
     sudoers_flags = 0;
     if (sudo_mode & MODE_SHELL)
        user_cmnd = "shell";
@@ -245,6 +248,7 @@ main(argc, argv)
                    putchar('\n');
                    dump_auth_methods();
                    dump_defaults();
+                   dump_interfaces();
                }
                exit(0);
                break;
@@ -521,12 +525,6 @@ init_vars(sudo_mode)
     } else
        set_perms(PERM_ROOT, sudo_mode);
 
-    /*
-     * Load the list of local ip addresses and netmasks into
-     * the interfaces array.
-     */
-    load_interfaces();
-
     /*
      * If we were given the '-s' option (run shell) we need to redo
      * NewArgv and NewArgc.