Files
sudo/interfaces.c

286 lines
7.3 KiB
C

/*
* CU sudo version 1.3.1
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 1, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Please send bugs, changes, problems to sudo-bugs@cs.colorado.edu
*
*******************************************************************
*
* This module contains load_interfaces() a function that
* fills the interfaces global with a list of active ip
* addresses and their associated netmasks.
*
* Todd C. Miller Mon May 1 20:48:43 MDT 1995
*/
#ifndef lint
static char rcsid[] = "$Id$";
#endif /* lint */
#include "config.h"
#include <stdio.h>
#ifdef STDC_HEADERS
#include <stdlib.h>
#endif /* STDC_HEADERS */
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#ifdef HAVE_STRING_H
#include <string.h>
#endif /* HAVE_STRING_H */
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif /* HAVE_STRINGS_H */
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif /* HAVE_MALLOC_H */
#include <netdb.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/param.h>
#ifdef HAVE_SYS_SOCKIO_H
#include <sys/sockio.h>
#else
#include <sys/ioctl.h>
#endif /* HAVE_SYS_SOCKIO_H */
#ifdef _ISC
#include <sys/stream.h>
#include <sys/sioctl.h>
#include <sys/stropts.h>
#include <net/errno.h>
#define STRSET(cmd, param, len) {strioctl.ic_cmd=(cmd);\
strioctl.ic_dp=(param);\
strioctl.ic_timout=0;\
strioctl.ic_len=(len);}
#endif /* _ISC */
#ifdef _MIPS
#include <net/soioctl.h>
#endif /* _MIPS */
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <net/if.h>
#include "sudo.h"
#include "options.h"
#include "version.h"
#if !defined(STDC_HEADERS) && !defined(__GNUC__)
extern char *malloc __P((size_t));
#endif /* !STDC_HEADERS && !__GNUC__ */
/*
* Globals
*/
struct interface *interfaces;
int num_interfaces = 0;
extern int Argc;
extern char **Argv;
#ifdef SIOCGIFCONF
/**********************************************************************
*
* load_interfaces()
*
* This function sets the interfaces global variable
* and sets the constituent ip addrs and netmasks.
*/
void load_interfaces()
{
unsigned long localhost_mask;
struct ifconf *ifconf;
char ifconf_buf[sizeof(struct ifconf) + BUFSIZ];
struct ifreq ifreq;
struct sockaddr_in *sin;
char buf[BUFSIZ];
int sock, n, i;
#ifdef _ISC
struct strioctl strioctl;
#endif /* _ISC */
/* so we can skip localhost and its ilk */
localhost_mask = inet_addr("127.0.0.0");
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
perror("socket");
exit(1);
}
/*
* get interface configuration or return (leaving interfaces NULL)
*/
ifconf = (struct ifconf *) ifconf_buf;
ifconf->ifc_buf = (caddr_t) (ifconf_buf + sizeof(struct ifconf));
ifconf->ifc_len = sizeof(ifconf_buf) - sizeof(struct ifconf);
#ifdef _ISC
STRSET(SIOCGIFCONF, (caddr_t) ifconf, sizeof(ifconf_buf));
if (ioctl(sock, I_STR, (caddr_t) &strioctl) < 0) {
/* networking probably not installed in kernel */
return;
}
#else
if (ioctl(sock, SIOCGIFCONF, (caddr_t) ifconf) < 0) {
/* networking probably not installed in kernel */
return;
}
#endif /* _ISC */
/*
* get the maximum number of interfaces that *could* exist.
*/
n = ifconf->ifc_len / sizeof(struct ifreq);
/*
* malloc() space for interfaces array
*/
interfaces = (struct interface *) malloc(sizeof(struct interface) * n);
if (interfaces == NULL) {
perror("malloc");
(void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]);
exit(1);
}
/*
* for each interface, get the ip address and netmask
*/
for (i = 0; i < ifconf->ifc_len; ) {
/* setup ifreq struct */
ifreq = *((struct ifreq *) &ifconf->ifc_buf[i]);
/* set i to the subscript of the next interface */
#ifdef HAVE_SA_LEN
if (ifreq.ifr_addr.sa_len > sizeof(ifreq.ifr_addr))
i += sizeof(ifreq.ifr_name) + ifreq.ifr_addr.sa_len;
else
#endif /* HAVE_SA_LEN */
i += sizeof(ifreq);
/* get the ip address */
#ifdef _ISC
STRSET(SIOCGIFADDR, (caddr_t) &ifreq, sizeof(ifreq));
if (ioctl(sock, I_STR, (caddr_t) &strioctl) < 0) {
#else
if (ioctl(sock, SIOCGIFADDR, (caddr_t) &ifreq)) {
#endif /* _ISC */
/* non-fatal error if interface is down or not supported */
if (errno == EADDRNOTAVAIL || errno == ENXIO || errno == EAFNOSUPPORT)
continue;
(void) fprintf(stderr, "%s: Error, ioctl: SIOCGIFADDR ", Argv[0]);
perror("");
exit(1);
}
sin = (struct sockaddr_in *) &ifreq.ifr_addr;
/* make sure we don't have a dup (usually consecutive) */
if (num_interfaces && interfaces[num_interfaces - 1].addr.s_addr ==
sin->sin_addr.s_addr)
continue;
/* store the ip address */
interfaces[num_interfaces].addr.s_addr = sin->sin_addr.s_addr;
/* get the netmask */
#ifdef SIOCGIFNETMASK
#ifdef _ISC
STRSET(SIOCGIFNETMASK, (caddr_t) &ifreq, sizeof(ifreq));
if (ioctl(sock, I_STR, (caddr_t) &strioctl) == 0) {
#else
if (ioctl(sock, SIOCGIFNETMASK, (caddr_t) &ifreq) == 0) {
#endif /* _ISC */
/* store the netmask */
interfaces[num_interfaces].netmask.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);
else
interfaces[num_interfaces].netmask.s_addr = htonl(IN_CLASSA_NET);
}
/* avoid localhost and friends */
if ((interfaces[num_interfaces].addr.s_addr &
interfaces[num_interfaces].netmask.s_addr) == localhost_mask)
continue;
num_interfaces++;
}
/* if there were bogus entries, realloc the array */
if (n != num_interfaces) {
interfaces = (struct interface *) realloc(interfaces,
sizeof(struct interface) * num_interfaces);
if (interfaces == NULL) {
perror("realloc");
(void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]);
exit(1);
}
}
}
/**********************************************************************
*
* next_if()
*
* This function returns a pointer to the next struct ifreq *
* in the list.
*/
static struct ifreq *next_if(cur)
struct ifreq *cur;
{
struct ifreq *next;
u_char sa_len;
#ifdef HAVE_SA_LEN
sa_len = cur->ifr_addr.sa_len;
if (sa_len > sizeof(cur->ifr_ifru))
next = (struct ifreq *) ((caddr_t) cur + sizeof(cur->ifr_name) + sa_len);
else
#endif /* HAVE_SA_LEN */
next = (struct ifreq *) ((caddr_t) cur + sizeof(cur->ifr_name) + sizeof(cur->ifr_ifru));
return(next);
}
#else /* SIOCGIFCONF */
/**********************************************************************
*
* load_interfaces()
*
* Stub function for those without SIOCGIFCONF
*/
void load_interfaces()
{
return;
}
#endif /* SIOCGIFCONF */