From: Todd C. Miller Date: Sun, 4 Jun 2000 23:57:22 +0000 (+0000) Subject: Add support for using getifaddrs() to get the list of ip addr / netmask X-Git-Tag: SUDO_1_6_4~269 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d7050d5615b14abcc16888d5050101dfc3ba7b2e;p=sudo Add support for using getifaddrs() to get the list of ip addr / netmask pairs. Currently IPv4-only. --- diff --git a/config.h.in b/config.h.in index 1c8d7a738..5c5004ecc 100644 --- a/config.h.in +++ b/config.h.in @@ -272,6 +272,9 @@ /* Define if you have vasprintf(3). */ #undef HAVE_VASPRINTF +/* Define if you have getifaddrs(3). */ +#undef HAVE_GETIFADDRS + /* Define if you have the header file. */ #undef HAVE_MALLOC_H diff --git a/interfaces.c b/interfaces.c index 7a5bf9b7a..b58a654d9 100644 --- a/interfaces.c +++ b/interfaces.c @@ -82,6 +82,9 @@ struct rtentry; #include #include #include +#ifdef HAVE_GETIFADDRS +#include +#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)); +} diff --git a/interfaces.h b/interfaces.h index f6453eed6..f6cd892f9 100644 --- a/interfaces.h +++ b/interfaces.h @@ -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 903e742c2..4233d6de1 100644 --- 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.