#if defined(__osf__) && !defined(__cplusplus)
struct mbuf;
struct rtentry;
#endif
#include <config.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#if defined(HAVE_SYS_SOCKIO_H) && !defined(SIOCGIFCONF)
# include <sys/sockio.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#ifdef HAVE_STDBOOL_H
# include <stdbool.h>
#else
# include "compat/stdbool.h"
#endif
#include <unistd.h>
#include <netdb.h>
#include <errno.h>
#ifdef _ISC
# include <sys/stream.h>
# include <sys/sioctl.h>
# include <sys/stropts.h>
# define STRSET(cmd, param, len) {strioctl.ic_cmd=(cmd);\
strioctl.ic_dp=(param);\
strioctl.ic_timout=0;\
strioctl.ic_len=(len);}
#endif
#ifdef _MIPS
# include <net/soioctl.h>
#endif
#include <netinet/in.h>
#include <arpa/inet.h>
#ifdef NEED_RESOLV_H
# include <arpa/nameser.h>
# include <resolv.h>
#endif
#include <net/if.h>
#ifdef HAVE_GETIFADDRS
# include <ifaddrs.h>
#endif
#define SUDO_NET_IFS_C
#define DEFAULT_TEXT_DOMAIN "sudo"
#include "sudo_gettext.h"
#include "sudo_compat.h"
#include "sudo_fatal.h"
#include "sudo_conf.h"
#include "sudo_debug.h"
#ifndef IFF_LOOPBACK
# define IFF_LOOPBACK 0
#endif
#ifndef INET_ADDRSTRLEN
# define INET_ADDRSTRLEN 16
#endif
#ifndef INET6_ADDRSTRLEN
# define INET6_ADDRSTRLEN 46
#endif
#ifdef HAVE_GETIFADDRS
int
get_net_ifs(char **addrinfo)
{
struct ifaddrs *ifa, *ifaddrs;
struct sockaddr_in *sin;
#ifdef HAVE_STRUCT_IN6_ADDR
struct sockaddr_in6 *sin6;
char addrstr[INET6_ADDRSTRLEN], maskstr[INET6_ADDRSTRLEN];
#else
char addrstr[INET_ADDRSTRLEN], maskstr[INET_ADDRSTRLEN];
#endif
int ailen, len, num_interfaces = 0;
char *cp;
debug_decl(get_net_ifs, SUDO_DEBUG_NETIF)
if (!sudo_conf_probe_interfaces())
debug_return_int(0);
if (getifaddrs(&ifaddrs) == -1)
debug_return_int(-1);
for (ifa = ifaddrs; ifa != NULL; ifa = ifa -> ifa_next) {
if (ifa->ifa_addr == NULL || ifa->ifa_netmask == NULL ||
!ISSET(ifa->ifa_flags, IFF_UP) || ISSET(ifa->ifa_flags, IFF_LOOPBACK))
continue;
switch (ifa->ifa_addr->sa_family) {
case AF_INET:
#ifdef HAVE_STRUCT_IN6_ADDR
case AF_INET6:
#endif
num_interfaces++;
break;
}
}
if (num_interfaces == 0)
debug_return_int(0);
ailen = num_interfaces * 2 * INET6_ADDRSTRLEN;
if ((cp = malloc(ailen)) == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
debug_return_int(-1);
}
*addrinfo = cp;
for (ifa = ifaddrs; ifa != NULL; ifa = ifa -> ifa_next) {
if (ifa->ifa_addr == NULL || ifa->ifa_netmask == NULL ||
!ISSET(ifa->ifa_flags, IFF_UP) || ISSET(ifa->ifa_flags, IFF_LOOPBACK))
continue;
switch (ifa->ifa_addr->sa_family) {
case AF_INET:
sin = (struct sockaddr_in *)ifa->ifa_addr;
if (inet_ntop(AF_INET, &sin->sin_addr, addrstr, sizeof(addrstr)) == NULL)
continue;
sin = (struct sockaddr_in *)ifa->ifa_netmask;
if (inet_ntop(AF_INET, &sin->sin_addr, maskstr, sizeof(maskstr)) == NULL)
continue;
len = snprintf(cp, ailen - (*addrinfo - cp),
"%s%s/%s", cp == *addrinfo ? "" : " ", addrstr, maskstr);
if (len <= 0 || len >= ailen - (*addrinfo - cp)) {
sudo_warnx(U_("internal error, %s overflow"), __func__);
goto done;
}
cp += len;
break;
#ifdef HAVE_STRUCT_IN6_ADDR
case AF_INET6:
sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
if (inet_ntop(AF_INET6, &sin6->sin6_addr, addrstr, sizeof(addrstr)) == NULL)
continue;
sin6 = (struct sockaddr_in6 *)ifa->ifa_netmask;
if (inet_ntop(AF_INET6, &sin6->sin6_addr, maskstr, sizeof(maskstr)) == NULL)
continue;
len = snprintf(cp, ailen - (*addrinfo - cp),
"%s%s/%s", cp == *addrinfo ? "" : " ", addrstr, maskstr);
if (len <= 0 || len >= ailen - (*addrinfo - cp)) {
sudo_warnx(U_("internal error, %s overflow"), __func__);
goto done;
}
cp += len;
break;
#endif
}
}
done:
#ifdef HAVE_FREEIFADDRS
freeifaddrs(ifaddrs);
#else
free(ifaddrs);
#endif
debug_return_int(num_interfaces);
}
#elif defined(SIOCGIFCONF) && !defined(STUB_LOAD_INTERFACES)
int
get_net_ifs(char **addrinfo)
{
char ifr_tmpbuf[sizeof(struct ifreq)];
struct ifreq *ifr, *ifr_tmp = (struct ifreq *)ifr_tmpbuf;
struct ifconf *ifconf;
struct sockaddr_in *sin;
int ailen, i, len, n, sock, num_interfaces = 0;
size_t buflen = sizeof(struct ifconf) + BUFSIZ;
char *cp, *previfname = "", *ifconf_buf = NULL;
char addrstr[INET_ADDRSTRLEN], maskstr[INET_ADDRSTRLEN];
#ifdef _ISC
struct strioctl strioctl;
#endif
debug_decl(get_net_ifs, SUDO_DEBUG_NETIF)
if (!sudo_conf_probe_interfaces())
debug_return_int(0);
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0)
debug_return_int(-1);
for (;;) {
if ((ifconf_buf = malloc(buflen)) == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
num_interfaces = -1;
goto done;
}
ifconf = (struct ifconf *) ifconf_buf;
ifconf->ifc_len = buflen - sizeof(struct ifconf);
ifconf->ifc_buf = (caddr_t) (ifconf_buf + sizeof(struct ifconf));
#ifdef _ISC
STRSET(SIOCGIFCONF, (caddr_t) ifconf, buflen);
if (ioctl(sock, I_STR, (caddr_t) &strioctl) < 0)
#else
if (ioctl(sock, SIOCGIFCONF, (caddr_t) ifconf) < 0 && errno != EINVAL)
#endif
goto done;
if (ifconf->ifc_len + sizeof(struct ifreq) < buflen)
break;
buflen += BUFSIZ;
free(ifconf_buf);
}
if ((n = ifconf->ifc_len / sizeof(struct ifreq)) == 0)
goto done;
ailen = n * 2 * INET6_ADDRSTRLEN;
if ((cp = malloc(ailen)) == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
num_interfaces = -1;
goto done;
}
*addrinfo = cp;
for (i = 0; i < ifconf->ifc_len; ) {
ifr = (struct ifreq *) &ifconf->ifc_buf[i];
i += sizeof(struct ifreq);
#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
if (ifr->ifr_addr.sa_len > sizeof(ifr->ifr_addr))
i += ifr->ifr_addr.sa_len - sizeof(struct sockaddr);
#endif
sin = (struct sockaddr_in *) &ifr->ifr_addr;
if (sin->sin_addr.s_addr == 0 ||
strncmp(previfname, ifr->ifr_name, sizeof(ifr->ifr_name) - 1) == 0)
continue;
if (ifr->ifr_addr.sa_family != AF_INET)
continue;
#ifdef SIOCGIFFLAGS
memset(ifr_tmp, 0, 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
memcpy(ifr_tmp, ifr, sizeof(*ifr_tmp));
if (!ISSET(ifr_tmp->ifr_flags, IFF_UP) ||
ISSET(ifr_tmp->ifr_flags, IFF_LOOPBACK))
continue;
memset(ifr_tmp, 0, sizeof(*ifr_tmp));
strncpy(ifr_tmp->ifr_name, ifr->ifr_name, sizeof(ifr_tmp->ifr_name) - 1);
sin = (struct sockaddr_in *) &ifr_tmp->ifr_addr;
#ifdef _ISC
STRSET(SIOCGIFNETMASK, (caddr_t) ifr_tmp, sizeof(*ifr_tmp));
if (ioctl(sock, I_STR, (caddr_t) &strioctl) < 0)
#else
if (ioctl(sock, SIOCGIFNETMASK, (caddr_t) ifr_tmp) < 0)
#endif
sin->sin_addr.s_addr = htonl(IN_CLASSC_NET);
sin = (struct sockaddr_in *) &ifr->ifr_addr;
if (inet_ntop(AF_INET, &sin->sin_addr, addrstr, sizeof(addrstr)) == NULL)
continue;
sin = (struct sockaddr_in *) &ifr_tmp->ifr_addr;
if (inet_ntop(AF_INET, &sin->sin_addr, maskstr, sizeof(maskstr)) == NULL)
continue;
len = snprintf(cp, ailen - (*addrinfo - cp),
"%s%s/%s", cp == *addrinfo ? "" : " ", addrstr, maskstr);
if (len <= 0 || len >= ailen - (*addrinfo - cp)) {
sudo_warnx(U_("internal error, %s overflow"), __func__);
goto done;
}
cp += len;
previfname = ifr->ifr_name;
num_interfaces++;
}
done:
free(ifconf_buf);
(void) close(sock);
debug_return_int(num_interfaces);
}
#else
int
get_net_ifs(char **addrinfo)
{
debug_decl(get_net_ifs, SUDO_DEBUG_NETIF)
debug_return_int(0);
}
#endif