mirror of
https://github.com/sudo-project/sudo.git
synced 2025-08-31 06:15:37 +00:00
BSD-style copyright, cosmetic changes
This commit is contained in:
86
find_path.c
86
find_path.c
@@ -1,32 +1,28 @@
|
||||
/*
|
||||
* CU sudo version 1.6
|
||||
* Copyright (c) 1996, 1998, 1999 Todd C. Miller <Todd.Miller@courtesan.com>
|
||||
* Copyright (c) 1996, 1998, 1999 Todd C. Miller <Todd.Miller@courtesan.com>
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* 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@courtesan.com
|
||||
*
|
||||
*******************************************************************
|
||||
*
|
||||
* This module contains the find_path() function that returns
|
||||
* TRUE if the command was found and FALSE if not.
|
||||
* If find_path() returns TRUE, the copyin paramters command and
|
||||
* ocommand contain the resolved and unresolved pathnames respectively.
|
||||
* NOTE: if "." or "" exists in PATH it will be searched last.
|
||||
*
|
||||
* Todd C. Miller <Todd.Miller@courtesan.com> Sat Mar 25 21:50:36 MST 1995
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
@@ -48,6 +44,7 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/param.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "sudo.h"
|
||||
|
||||
#ifndef STDC_HEADERS
|
||||
@@ -59,51 +56,37 @@ extern int stat __P((const char *, struct stat *));
|
||||
extern int lstat __P((const char *, struct stat *));
|
||||
#endif /* !STDC_HEADERS */
|
||||
|
||||
#ifndef _S_IFMT
|
||||
#define _S_IFMT S_IFMT
|
||||
#endif /* _S_IFMT */
|
||||
#ifndef _S_IFLNK
|
||||
#define _S_IFLNK S_IFLNK
|
||||
#endif /* _S_IFLNK */
|
||||
|
||||
#ifndef lint
|
||||
static const char rcsid[] = "$Sudo$";
|
||||
#endif /* lint */
|
||||
|
||||
/*******************************************************************
|
||||
*
|
||||
* find_path()
|
||||
*
|
||||
* this function finds the full pathname for a command and
|
||||
* stores it in a statically allocated array, filling in a pointer
|
||||
* to the array. Returns FOUND if the command was found, NOT_FOUND
|
||||
* if it was not found, or NOT_FOUND_DOT if it would have been found
|
||||
* but it is in '.' and IGNORE_DOT_PATH is in effect.
|
||||
/*
|
||||
* This function finds the full pathname for a command and
|
||||
* stores it in a statically allocated array, filling in a pointer
|
||||
* to the array. Returns FOUND if the command was found, NOT_FOUND
|
||||
* if it was not found, or NOT_FOUND_DOT if it would have been found
|
||||
* but it is in '.' and IGNORE_DOT_PATH is in effect.
|
||||
*/
|
||||
|
||||
int
|
||||
find_path(infile, outfile)
|
||||
char *infile; /* file to find */
|
||||
char **outfile; /* result parameter */
|
||||
{
|
||||
static char command[MAXPATHLEN]; /* qualified filename */
|
||||
register char *n; /* for traversing path */
|
||||
char *n; /* for traversing path */
|
||||
char *path = NULL; /* contents of PATH env var */
|
||||
char *origpath; /* so we can free path later */
|
||||
char *result = NULL; /* result of path/file lookup */
|
||||
int checkdot = 0; /* check current dir? */
|
||||
|
||||
command[0] = '\0';
|
||||
|
||||
if (strlen(infile) >= MAXPATHLEN) {
|
||||
errno = ENAMETOOLONG;
|
||||
(void) fprintf(stderr, "%s: path too long: %s\n", Argv[0], infile);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/*
|
||||
* If we were given a fully qualified or relative path
|
||||
* there is no need to look at PATH.
|
||||
* there is no need to look at $PATH.
|
||||
*/
|
||||
if (strchr(infile, '/')) {
|
||||
(void) strcpy(command, infile);
|
||||
@@ -115,11 +98,10 @@ find_path(infile, outfile)
|
||||
}
|
||||
|
||||
/*
|
||||
* grab PATH out of environment and make a local copy
|
||||
* Grab PATH out of environment and make a local copy
|
||||
*/
|
||||
if ((path = getenv("PATH")) == NULL)
|
||||
return(NOT_FOUND);
|
||||
|
||||
path = estrdup(path);
|
||||
origpath = path;
|
||||
|
||||
@@ -129,7 +111,7 @@ find_path(infile, outfile)
|
||||
*n = '\0';
|
||||
|
||||
/*
|
||||
* search current dir last if it is in PATH This will miss sneaky
|
||||
* Search current dir last if it is in PATH This will miss sneaky
|
||||
* things like using './' or './/'
|
||||
*/
|
||||
if (*path == '\0' || (*path == '.' && *(path + 1) == '\0')) {
|
||||
@@ -139,7 +121,7 @@ find_path(infile, outfile)
|
||||
}
|
||||
|
||||
/*
|
||||
* resolve the path and exit the loop if found
|
||||
* Resolve the path and exit the loop if found.
|
||||
*/
|
||||
if (strlen(path) + strlen(infile) + 1 >= MAXPATHLEN) {
|
||||
(void) fprintf(stderr, "%s: path too long: %s\n", Argv[0], infile);
|
||||
|
88
getspwuid.c
88
getspwuid.c
@@ -1,31 +1,28 @@
|
||||
/*
|
||||
* CU sudo version 1.6
|
||||
* Copyright (c) 1996, 1998, 1999 Todd C. Miller <Todd.Miller@courtesan.com>
|
||||
* Copyright (c) 1996, 1998, 1999 Todd C. Miller <Todd.Miller@courtesan.com>
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* 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@courtesan.com
|
||||
*
|
||||
*******************************************************************
|
||||
*
|
||||
* This module contains sudo_getpwuid(), a function that
|
||||
* Makes a dynamic copy of the struct passwd returned by
|
||||
* getpwuid() and substitutes the shadow password if
|
||||
* necessary.
|
||||
*
|
||||
* Todd C. Miller Mon Nov 20 13:53:06 MST 1995
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
@@ -89,15 +86,10 @@ static char *sudo_getshell __P((struct passwd *));
|
||||
static char *sudo_getepw __P((struct passwd *));
|
||||
|
||||
|
||||
|
||||
/**********************************************************************
|
||||
*
|
||||
* sudo_getshell()
|
||||
*
|
||||
* This function returns the user's shell based on either the
|
||||
* SHELL evariable or the passwd(5) entry (in that order).
|
||||
/*
|
||||
* Return the user's shell based on either the SHELL
|
||||
* environment variable or the passwd(5) entry (in that order).
|
||||
*/
|
||||
|
||||
static char *
|
||||
sudo_getshell(pw)
|
||||
struct passwd *pw;
|
||||
@@ -116,22 +108,16 @@ sudo_getshell(pw)
|
||||
return(pw_shell);
|
||||
}
|
||||
|
||||
|
||||
/**********************************************************************
|
||||
*
|
||||
* sudo_getepw()
|
||||
*
|
||||
* This function returns the encrypted password for the user described
|
||||
* by pw. If there is a shadow password it is returned, else the
|
||||
* normal UN*X password is returned instead.
|
||||
/*
|
||||
* Return the encrypted password for the user described by pw. If shadow
|
||||
* passwords are in use, look in the shadow file.
|
||||
*/
|
||||
|
||||
static char *
|
||||
sudo_getepw(pw)
|
||||
struct passwd *pw;
|
||||
{
|
||||
|
||||
/* if there is a function to check for shadow enabled, use it... */
|
||||
/* If there is a function to check for shadow enabled, use it... */
|
||||
#ifdef HAVE_ISCOMSEC
|
||||
if (!iscomsec())
|
||||
return(pw->pw_passwd);
|
||||
@@ -187,20 +173,14 @@ sudo_getepw(pw)
|
||||
}
|
||||
#endif /* HAVE_GETAUTHUID */
|
||||
|
||||
/* Fall back on normal passwd */
|
||||
/* Fall back on normal password. */
|
||||
return(pw->pw_passwd);
|
||||
}
|
||||
|
||||
|
||||
/**********************************************************************
|
||||
*
|
||||
* sudo_getpwuid()
|
||||
*
|
||||
* This function dynamically allocates space for a struct password
|
||||
* and the constituent parts that we care about. If shadow passwords
|
||||
* are in use, it substitutes the shadow password for pw_passwd.
|
||||
/*
|
||||
* Dynamically allocate space for a struct password and the constituent parts
|
||||
* that we care about. Fills in pw_passwd from shadow file if necessary.
|
||||
*/
|
||||
|
||||
struct passwd *
|
||||
sudo_getpwuid(uid)
|
||||
uid_t uid;
|
||||
@@ -210,7 +190,7 @@ sudo_getpwuid(uid)
|
||||
if ((pw = getpwuid(uid)) == NULL)
|
||||
return(NULL);
|
||||
|
||||
/* allocate space for a local copy of pw */
|
||||
/* Allocate space for a local copy of pw. */
|
||||
local_pw = (struct passwd *) emalloc(sizeof(struct passwd));
|
||||
|
||||
/*
|
||||
|
83
goodpath.c
83
goodpath.c
@@ -1,32 +1,28 @@
|
||||
/*
|
||||
* CU sudo version 1.6
|
||||
* Copyright (c) 1996, 1998, 1999 Todd C. Miller <Todd.Miller@courtesan.com>
|
||||
* Copyright (c) 1996, 1998, 1999 Todd C. Miller <Todd.Miller@courtesan.com>
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* 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@courtesan.com
|
||||
*
|
||||
*******************************************************************
|
||||
*
|
||||
* This module contains sudo_goodpath(3)
|
||||
*
|
||||
* sudo_goodpath(3) takes a path to check and returns its argument
|
||||
* if the path is stat(2)'able, a regular file, and executable by
|
||||
* root. The string's size should be <= MAXPATHLEN.
|
||||
*
|
||||
* Todd C. Miller <Todd.Miller@courtesan.com> Sat Mar 25 21:58:17 MST 1995
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
@@ -56,43 +52,34 @@ extern int stat __P((const char *, struct stat *));
|
||||
static const char rcsid[] = "$Sudo$";
|
||||
#endif /* lint */
|
||||
|
||||
/******************************************************************
|
||||
*
|
||||
* sudo_goodpath()
|
||||
*
|
||||
* this function takes a path and makes sure it describes a a file
|
||||
* that is a normal file and executable by root.
|
||||
/*
|
||||
* Verify that path is a normal file and executable by root.
|
||||
*/
|
||||
|
||||
char *
|
||||
sudo_goodpath(path)
|
||||
const char * path;
|
||||
const char *path;
|
||||
{
|
||||
struct stat statbuf; /* for stat(2) */
|
||||
int err; /* if stat(2) got an error */
|
||||
struct stat sb;
|
||||
int err;
|
||||
|
||||
/* check for brain damage */
|
||||
/* Check for brain damage */
|
||||
if (path == NULL || path[0] == '\0')
|
||||
return(NULL);
|
||||
|
||||
/* we need to be root for the stat */
|
||||
/* Do the stat() as root. */
|
||||
set_perms(PERM_ROOT, 0);
|
||||
|
||||
err = stat(path, &statbuf);
|
||||
|
||||
/* discard root perms */
|
||||
err = stat(path, &sb);
|
||||
set_perms(PERM_USER, 0);
|
||||
|
||||
/* stat(3) failed */
|
||||
/* stat() failed */
|
||||
if (err)
|
||||
return(NULL);
|
||||
|
||||
/* make sure path describes an executable regular file */
|
||||
if (S_ISREG(statbuf.st_mode) && (statbuf.st_mode & 0000111)) {
|
||||
return((char *)path);
|
||||
} else {
|
||||
/* file is not executable/regular */
|
||||
/* Make sure path describes an executable regular file. */
|
||||
if (!S_ISREG(sb.st_mode) || !(sb.st_mode & 0000111)) {
|
||||
errno = EACCES;
|
||||
return(NULL);
|
||||
}
|
||||
|
||||
return((char *)path);
|
||||
}
|
||||
|
108
interfaces.c
108
interfaces.c
@@ -1,30 +1,28 @@
|
||||
/*
|
||||
* CU sudo version 1.6
|
||||
* Copyright (c) 1996, 1998, 1999 Todd C. Miller <Todd.Miller@courtesan.com>
|
||||
* Copyright (c) 1996, 1998, 1999 Todd C. Miller <Todd.Miller@courtesan.com>
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* 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@courtesan.com
|
||||
*
|
||||
*******************************************************************
|
||||
*
|
||||
* 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
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
@@ -78,14 +76,10 @@ static const char rcsid[] = "$Sudo$";
|
||||
|
||||
|
||||
#if defined(SIOCGIFCONF) && !defined(STUB_LOAD_INTERFACES)
|
||||
/**********************************************************************
|
||||
*
|
||||
* load_interfaces()
|
||||
*
|
||||
* This function sets the interfaces global variable
|
||||
* and sets the constituent ip addrs and netmasks.
|
||||
/*
|
||||
* Allocate and fill in the interfaces global variable with the
|
||||
* machine's ip addresses and netmasks.
|
||||
*/
|
||||
|
||||
void
|
||||
load_interfaces()
|
||||
{
|
||||
@@ -101,12 +95,13 @@ load_interfaces()
|
||||
|
||||
sock = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (sock < 0) {
|
||||
perror("socket");
|
||||
(void) fprintf(stderr, "%s: cannot open socket: %s\n",
|
||||
Argv[0], strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/*
|
||||
* get interface configuration or return (leaving interfaces NULL)
|
||||
* Get interface configuration or return (leaving num_interfaces 0)
|
||||
*/
|
||||
for (;;) {
|
||||
ifconf_buf = erealloc(ifconf_buf, len);
|
||||
@@ -114,7 +109,7 @@ 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 */
|
||||
/* Networking may not be installed in kernel... */
|
||||
#ifdef _ISC
|
||||
STRSET(SIOCGIFCONF, (caddr_t) ifconf, len);
|
||||
if (ioctl(sock, I_STR, (caddr_t) &strioctl) < 0) {
|
||||
@@ -126,49 +121,37 @@ load_interfaces()
|
||||
return;
|
||||
}
|
||||
|
||||
/* break out of loop if we have a big enough buffer */
|
||||
/* Break out of loop if we have a big enough buffer. */
|
||||
if (ifconf->ifc_len + sizeof(struct ifreq) < len)
|
||||
break;
|
||||
len += BUFSIZ;
|
||||
}
|
||||
|
||||
/*
|
||||
* get the maximum number of interfaces that *could* exist.
|
||||
*/
|
||||
/* Allocate space for the maximum number of interfaces that could exist. */
|
||||
n = ifconf->ifc_len / sizeof(struct ifreq);
|
||||
|
||||
/*
|
||||
* allocate space for interfaces array
|
||||
*/
|
||||
interfaces = (struct interface *) emalloc(sizeof(struct interface) * n);
|
||||
|
||||
/*
|
||||
* for each interface, store the ip address and netmask
|
||||
*/
|
||||
/* For each interface, store the ip address and netmask. */
|
||||
for (i = 0; i < ifconf->ifc_len; ) {
|
||||
/* get a pointer to the current interface */
|
||||
/* Get a pointer to the current interface. */
|
||||
ifr = (struct ifreq *) &ifconf->ifc_buf[i];
|
||||
|
||||
/* set i to the subscript of the next interface */
|
||||
/* Set i to the subscript of the next interface. */
|
||||
i += sizeof(struct ifreq);
|
||||
#ifdef HAVE_SA_LEN
|
||||
if (ifr->ifr_addr.sa_len > sizeof(ifr->ifr_addr))
|
||||
i += ifr->ifr_addr.sa_len - sizeof(struct sockaddr);
|
||||
#endif /* HAVE_SA_LEN */
|
||||
|
||||
/* skip duplicates and interfaces with NULL addresses */
|
||||
/* Skip duplicates and interfaces with NULL addresses. */
|
||||
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;
|
||||
|
||||
/* skip non-ip things */
|
||||
if (ifr->ifr_addr.sa_family != AF_INET)
|
||||
continue;
|
||||
|
||||
/*
|
||||
* make sure the interface is up, skip if not.
|
||||
*/
|
||||
#ifdef SIOCGIFFLAGS
|
||||
memset(&ifr_tmp, 0, sizeof(ifr_tmp));
|
||||
strncpy(ifr_tmp.ifr_name, ifr->ifr_name, sizeof(ifr_tmp.ifr_name) - 1);
|
||||
@@ -176,18 +159,17 @@ load_interfaces()
|
||||
#endif
|
||||
ifr_tmp = *ifr;
|
||||
|
||||
/* skip interfaces marked "down" and "loopback" */
|
||||
/* Skip interfaces marked "down" and "loopback". */
|
||||
if (!(ifr_tmp.ifr_flags & IFF_UP) || (ifr_tmp.ifr_flags & IFF_LOOPBACK))
|
||||
continue;
|
||||
|
||||
/* store the ip address */
|
||||
sin = (struct sockaddr_in *) &ifr->ifr_addr;
|
||||
interfaces[num_interfaces].addr.s_addr = sin->sin_addr.s_addr;
|
||||
|
||||
/* stash the name of the interface we saved */
|
||||
/* Stash the name of the interface we saved. */
|
||||
previfname = ifr->ifr_name;
|
||||
|
||||
/* get the netmask */
|
||||
/* Get the netmask. */
|
||||
(void) memset(&ifr_tmp, 0, sizeof(ifr_tmp));
|
||||
strncpy(ifr_tmp.ifr_name, ifr->ifr_name, sizeof(ifr_tmp.ifr_name) - 1);
|
||||
#ifdef SIOCGIFNETMASK
|
||||
@@ -199,7 +181,6 @@ load_interfaces()
|
||||
#endif /* _ISC */
|
||||
sin = (struct sockaddr_in *) &ifr_tmp.ifr_addr;
|
||||
|
||||
/* store the netmask */
|
||||
interfaces[num_interfaces].netmask.s_addr = sin->sin_addr.s_addr;
|
||||
} else {
|
||||
#else
|
||||
@@ -213,13 +194,12 @@ load_interfaces()
|
||||
interfaces[num_interfaces].netmask.s_addr = htonl(IN_CLASSA_NET);
|
||||
}
|
||||
|
||||
/* only now can we be sure it was a good/interesting interface */
|
||||
/* Only now can we be sure it was a good/interesting interface. */
|
||||
num_interfaces++;
|
||||
}
|
||||
|
||||
/* if there were bogus entries, realloc the array */
|
||||
/* If the expected size < real size, realloc the array. */
|
||||
if (n != num_interfaces) {
|
||||
/* it is unlikely that num_interfaces will be 0 but who knows... */
|
||||
if (num_interfaces != 0)
|
||||
interfaces = (struct interface *) erealloc(interfaces,
|
||||
sizeof(struct interface) * num_interfaces);
|
||||
@@ -232,13 +212,9 @@ load_interfaces()
|
||||
|
||||
#else /* !SIOCGIFCONF || STUB_LOAD_INTERFACES */
|
||||
|
||||
/**********************************************************************
|
||||
*
|
||||
* load_interfaces()
|
||||
*
|
||||
* Stub function for those without SIOCGIFCONF
|
||||
/*
|
||||
* Stub function for those without SIOCGIFCONF
|
||||
*/
|
||||
|
||||
void
|
||||
load_interfaces()
|
||||
{
|
||||
|
Reference in New Issue
Block a user