mirror of
https://github.com/sudo-project/sudo.git
synced 2025-09-01 23:05:17 +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
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* it under the terms of the GNU General Public License as published by
|
* modification, are permitted provided that the following conditions
|
||||||
* the Free Software Foundation; either version 1, or (at your option)
|
* are met:
|
||||||
* any later version.
|
* 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,
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||||
* GNU General Public License for more details.
|
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
*
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
* You should have received a copy of the GNU General Public License
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||||
* along with this program; if not, write to the Free Software
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||||
*
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||||
* Please send bugs, changes, problems to sudo-bugs@courtesan.com
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*
|
|
||||||
*******************************************************************
|
|
||||||
*
|
|
||||||
* 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
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
@@ -48,6 +44,7 @@
|
|||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/param.h>
|
#include <sys/param.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
#include "sudo.h"
|
#include "sudo.h"
|
||||||
|
|
||||||
#ifndef STDC_HEADERS
|
#ifndef STDC_HEADERS
|
||||||
@@ -59,51 +56,37 @@ extern int stat __P((const char *, struct stat *));
|
|||||||
extern int lstat __P((const char *, struct stat *));
|
extern int lstat __P((const char *, struct stat *));
|
||||||
#endif /* !STDC_HEADERS */
|
#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
|
#ifndef lint
|
||||||
static const char rcsid[] = "$Sudo$";
|
static const char rcsid[] = "$Sudo$";
|
||||||
#endif /* lint */
|
#endif /* lint */
|
||||||
|
|
||||||
/*******************************************************************
|
/*
|
||||||
*
|
* This function finds the full pathname for a command and
|
||||||
* find_path()
|
* stores it in a statically allocated array, filling in a pointer
|
||||||
*
|
* to the array. Returns FOUND if the command was found, NOT_FOUND
|
||||||
* this function finds the full pathname for a command and
|
* if it was not found, or NOT_FOUND_DOT if it would have been found
|
||||||
* stores it in a statically allocated array, filling in a pointer
|
* but it is in '.' and IGNORE_DOT_PATH is in effect.
|
||||||
* 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
|
int
|
||||||
find_path(infile, outfile)
|
find_path(infile, outfile)
|
||||||
char *infile; /* file to find */
|
char *infile; /* file to find */
|
||||||
char **outfile; /* result parameter */
|
char **outfile; /* result parameter */
|
||||||
{
|
{
|
||||||
static char command[MAXPATHLEN]; /* qualified filename */
|
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 *path = NULL; /* contents of PATH env var */
|
||||||
char *origpath; /* so we can free path later */
|
char *origpath; /* so we can free path later */
|
||||||
char *result = NULL; /* result of path/file lookup */
|
char *result = NULL; /* result of path/file lookup */
|
||||||
int checkdot = 0; /* check current dir? */
|
int checkdot = 0; /* check current dir? */
|
||||||
|
|
||||||
command[0] = '\0';
|
|
||||||
|
|
||||||
if (strlen(infile) >= MAXPATHLEN) {
|
if (strlen(infile) >= MAXPATHLEN) {
|
||||||
errno = ENAMETOOLONG;
|
|
||||||
(void) fprintf(stderr, "%s: path too long: %s\n", Argv[0], infile);
|
(void) fprintf(stderr, "%s: path too long: %s\n", Argv[0], infile);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If we were given a fully qualified or relative path
|
* 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, '/')) {
|
if (strchr(infile, '/')) {
|
||||||
(void) strcpy(command, 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)
|
if ((path = getenv("PATH")) == NULL)
|
||||||
return(NOT_FOUND);
|
return(NOT_FOUND);
|
||||||
|
|
||||||
path = estrdup(path);
|
path = estrdup(path);
|
||||||
origpath = path;
|
origpath = path;
|
||||||
|
|
||||||
@@ -129,7 +111,7 @@ find_path(infile, outfile)
|
|||||||
*n = '\0';
|
*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 './/'
|
* things like using './' or './/'
|
||||||
*/
|
*/
|
||||||
if (*path == '\0' || (*path == '.' && *(path + 1) == '\0')) {
|
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) {
|
if (strlen(path) + strlen(infile) + 1 >= MAXPATHLEN) {
|
||||||
(void) fprintf(stderr, "%s: path too long: %s\n", Argv[0], infile);
|
(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
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* it under the terms of the GNU General Public License as published by
|
* modification, are permitted provided that the following conditions
|
||||||
* the Free Software Foundation; either version 1, or (at your option)
|
* are met:
|
||||||
* any later version.
|
* 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,
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||||
* GNU General Public License for more details.
|
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
*
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
* You should have received a copy of the GNU General Public License
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||||
* along with this program; if not, write to the Free Software
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||||
*
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||||
* Please send bugs, changes, problems to sudo-bugs@courtesan.com
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*
|
|
||||||
*******************************************************************
|
|
||||||
*
|
|
||||||
* 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
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
@@ -89,15 +86,10 @@ static char *sudo_getshell __P((struct passwd *));
|
|||||||
static char *sudo_getepw __P((struct passwd *));
|
static char *sudo_getepw __P((struct passwd *));
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
/**********************************************************************
|
* Return the user's shell based on either the SHELL
|
||||||
*
|
* environment variable or the passwd(5) entry (in that order).
|
||||||
* sudo_getshell()
|
|
||||||
*
|
|
||||||
* This function returns the user's shell based on either the
|
|
||||||
* SHELL evariable or the passwd(5) entry (in that order).
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static char *
|
static char *
|
||||||
sudo_getshell(pw)
|
sudo_getshell(pw)
|
||||||
struct passwd *pw;
|
struct passwd *pw;
|
||||||
@@ -116,22 +108,16 @@ sudo_getshell(pw)
|
|||||||
return(pw_shell);
|
return(pw_shell);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
/**********************************************************************
|
* Return the encrypted password for the user described by pw. If shadow
|
||||||
*
|
* passwords are in use, look in the shadow file.
|
||||||
* 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.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static char *
|
static char *
|
||||||
sudo_getepw(pw)
|
sudo_getepw(pw)
|
||||||
struct passwd *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
|
#ifdef HAVE_ISCOMSEC
|
||||||
if (!iscomsec())
|
if (!iscomsec())
|
||||||
return(pw->pw_passwd);
|
return(pw->pw_passwd);
|
||||||
@@ -187,20 +173,14 @@ sudo_getepw(pw)
|
|||||||
}
|
}
|
||||||
#endif /* HAVE_GETAUTHUID */
|
#endif /* HAVE_GETAUTHUID */
|
||||||
|
|
||||||
/* Fall back on normal passwd */
|
/* Fall back on normal password. */
|
||||||
return(pw->pw_passwd);
|
return(pw->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.
|
||||||
* 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.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
struct passwd *
|
struct passwd *
|
||||||
sudo_getpwuid(uid)
|
sudo_getpwuid(uid)
|
||||||
uid_t uid;
|
uid_t uid;
|
||||||
@@ -210,7 +190,7 @@ sudo_getpwuid(uid)
|
|||||||
if ((pw = getpwuid(uid)) == NULL)
|
if ((pw = getpwuid(uid)) == NULL)
|
||||||
return(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));
|
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
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* it under the terms of the GNU General Public License as published by
|
* modification, are permitted provided that the following conditions
|
||||||
* the Free Software Foundation; either version 1, or (at your option)
|
* are met:
|
||||||
* any later version.
|
* 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,
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||||
* GNU General Public License for more details.
|
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
*
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
* You should have received a copy of the GNU General Public License
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||||
* along with this program; if not, write to the Free Software
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||||
*
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||||
* Please send bugs, changes, problems to sudo-bugs@courtesan.com
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*
|
|
||||||
*******************************************************************
|
|
||||||
*
|
|
||||||
* 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
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
@@ -56,43 +52,34 @@ extern int stat __P((const char *, struct stat *));
|
|||||||
static const char rcsid[] = "$Sudo$";
|
static const char rcsid[] = "$Sudo$";
|
||||||
#endif /* lint */
|
#endif /* lint */
|
||||||
|
|
||||||
/******************************************************************
|
/*
|
||||||
*
|
* Verify that path is a normal file and executable by root.
|
||||||
* sudo_goodpath()
|
|
||||||
*
|
|
||||||
* this function takes a path and makes sure it describes a a file
|
|
||||||
* that is a normal file and executable by root.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
char *
|
char *
|
||||||
sudo_goodpath(path)
|
sudo_goodpath(path)
|
||||||
const char * path;
|
const char *path;
|
||||||
{
|
{
|
||||||
struct stat statbuf; /* for stat(2) */
|
struct stat sb;
|
||||||
int err; /* if stat(2) got an error */
|
int err;
|
||||||
|
|
||||||
/* check for brain damage */
|
/* Check for brain damage */
|
||||||
if (path == NULL || path[0] == '\0')
|
if (path == NULL || path[0] == '\0')
|
||||||
return(NULL);
|
return(NULL);
|
||||||
|
|
||||||
/* we need to be root for the stat */
|
/* Do the stat() as root. */
|
||||||
set_perms(PERM_ROOT, 0);
|
set_perms(PERM_ROOT, 0);
|
||||||
|
err = stat(path, &sb);
|
||||||
err = stat(path, &statbuf);
|
|
||||||
|
|
||||||
/* discard root perms */
|
|
||||||
set_perms(PERM_USER, 0);
|
set_perms(PERM_USER, 0);
|
||||||
|
|
||||||
/* stat(3) failed */
|
/* stat() failed */
|
||||||
if (err)
|
if (err)
|
||||||
return(NULL);
|
return(NULL);
|
||||||
|
|
||||||
/* make sure path describes an executable regular file */
|
/* Make sure path describes an executable regular file. */
|
||||||
if (S_ISREG(statbuf.st_mode) && (statbuf.st_mode & 0000111)) {
|
if (!S_ISREG(sb.st_mode) || !(sb.st_mode & 0000111)) {
|
||||||
return((char *)path);
|
|
||||||
} else {
|
|
||||||
/* file is not executable/regular */
|
|
||||||
errno = EACCES;
|
errno = EACCES;
|
||||||
return(NULL);
|
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
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* it under the terms of the GNU General Public License as published by
|
* modification, are permitted provided that the following conditions
|
||||||
* the Free Software Foundation; either version 1, or (at your option)
|
* are met:
|
||||||
* any later version.
|
* 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,
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||||
* GNU General Public License for more details.
|
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
*
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
* You should have received a copy of the GNU General Public License
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||||
* along with this program; if not, write to the Free Software
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||||
*
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||||
* Please send bugs, changes, problems to sudo-bugs@courtesan.com
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*
|
|
||||||
*******************************************************************
|
|
||||||
*
|
|
||||||
* 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
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
@@ -78,14 +76,10 @@ static const char rcsid[] = "$Sudo$";
|
|||||||
|
|
||||||
|
|
||||||
#if defined(SIOCGIFCONF) && !defined(STUB_LOAD_INTERFACES)
|
#if defined(SIOCGIFCONF) && !defined(STUB_LOAD_INTERFACES)
|
||||||
/**********************************************************************
|
/*
|
||||||
*
|
* Allocate and fill in the interfaces global variable with the
|
||||||
* load_interfaces()
|
* machine's ip addresses and netmasks.
|
||||||
*
|
|
||||||
* This function sets the interfaces global variable
|
|
||||||
* and sets the constituent ip addrs and netmasks.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void
|
void
|
||||||
load_interfaces()
|
load_interfaces()
|
||||||
{
|
{
|
||||||
@@ -101,12 +95,13 @@ load_interfaces()
|
|||||||
|
|
||||||
sock = socket(AF_INET, SOCK_DGRAM, 0);
|
sock = socket(AF_INET, SOCK_DGRAM, 0);
|
||||||
if (sock < 0) {
|
if (sock < 0) {
|
||||||
perror("socket");
|
(void) fprintf(stderr, "%s: cannot open socket: %s\n",
|
||||||
|
Argv[0], strerror(errno));
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* get interface configuration or return (leaving interfaces NULL)
|
* Get interface configuration or return (leaving num_interfaces 0)
|
||||||
*/
|
*/
|
||||||
for (;;) {
|
for (;;) {
|
||||||
ifconf_buf = erealloc(ifconf_buf, len);
|
ifconf_buf = erealloc(ifconf_buf, len);
|
||||||
@@ -114,7 +109,7 @@ load_interfaces()
|
|||||||
ifconf->ifc_len = len - sizeof(struct ifconf);
|
ifconf->ifc_len = len - sizeof(struct ifconf);
|
||||||
ifconf->ifc_buf = (caddr_t) (ifconf_buf + 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
|
#ifdef _ISC
|
||||||
STRSET(SIOCGIFCONF, (caddr_t) ifconf, len);
|
STRSET(SIOCGIFCONF, (caddr_t) ifconf, len);
|
||||||
if (ioctl(sock, I_STR, (caddr_t) &strioctl) < 0) {
|
if (ioctl(sock, I_STR, (caddr_t) &strioctl) < 0) {
|
||||||
@@ -126,49 +121,37 @@ load_interfaces()
|
|||||||
return;
|
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)
|
if (ifconf->ifc_len + sizeof(struct ifreq) < len)
|
||||||
break;
|
break;
|
||||||
len += BUFSIZ;
|
len += BUFSIZ;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/* Allocate space for the maximum number of interfaces that could exist. */
|
||||||
* get the maximum number of interfaces that *could* exist.
|
|
||||||
*/
|
|
||||||
n = ifconf->ifc_len / sizeof(struct ifreq);
|
n = ifconf->ifc_len / sizeof(struct ifreq);
|
||||||
|
|
||||||
/*
|
|
||||||
* allocate space for interfaces array
|
|
||||||
*/
|
|
||||||
interfaces = (struct interface *) emalloc(sizeof(struct interface) * n);
|
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; ) {
|
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];
|
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);
|
i += sizeof(struct ifreq);
|
||||||
#ifdef HAVE_SA_LEN
|
#ifdef HAVE_SA_LEN
|
||||||
if (ifr->ifr_addr.sa_len > sizeof(ifr->ifr_addr))
|
if (ifr->ifr_addr.sa_len > sizeof(ifr->ifr_addr))
|
||||||
i += ifr->ifr_addr.sa_len - sizeof(struct sockaddr);
|
i += ifr->ifr_addr.sa_len - sizeof(struct sockaddr);
|
||||||
#endif /* HAVE_SA_LEN */
|
#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;
|
sin = (struct sockaddr_in *) &ifr->ifr_addr;
|
||||||
if (sin->sin_addr.s_addr == 0 ||
|
if (sin->sin_addr.s_addr == 0 ||
|
||||||
strncmp(previfname, ifr->ifr_name, sizeof(ifr->ifr_name) - 1) == 0)
|
strncmp(previfname, ifr->ifr_name, sizeof(ifr->ifr_name) - 1) == 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
/* skip non-ip things */
|
|
||||||
if (ifr->ifr_addr.sa_family != AF_INET)
|
if (ifr->ifr_addr.sa_family != AF_INET)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
/*
|
|
||||||
* make sure the interface is up, skip if not.
|
|
||||||
*/
|
|
||||||
#ifdef SIOCGIFFLAGS
|
#ifdef SIOCGIFFLAGS
|
||||||
memset(&ifr_tmp, 0, sizeof(ifr_tmp));
|
memset(&ifr_tmp, 0, sizeof(ifr_tmp));
|
||||||
strncpy(ifr_tmp.ifr_name, ifr->ifr_name, sizeof(ifr_tmp.ifr_name) - 1);
|
strncpy(ifr_tmp.ifr_name, ifr->ifr_name, sizeof(ifr_tmp.ifr_name) - 1);
|
||||||
@@ -176,18 +159,17 @@ load_interfaces()
|
|||||||
#endif
|
#endif
|
||||||
ifr_tmp = *ifr;
|
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))
|
if (!(ifr_tmp.ifr_flags & IFF_UP) || (ifr_tmp.ifr_flags & IFF_LOOPBACK))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
/* store the ip address */
|
|
||||||
sin = (struct sockaddr_in *) &ifr->ifr_addr;
|
sin = (struct sockaddr_in *) &ifr->ifr_addr;
|
||||||
interfaces[num_interfaces].addr.s_addr = sin->sin_addr.s_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;
|
previfname = ifr->ifr_name;
|
||||||
|
|
||||||
/* get the netmask */
|
/* Get the netmask. */
|
||||||
(void) memset(&ifr_tmp, 0, sizeof(ifr_tmp));
|
(void) memset(&ifr_tmp, 0, sizeof(ifr_tmp));
|
||||||
strncpy(ifr_tmp.ifr_name, ifr->ifr_name, sizeof(ifr_tmp.ifr_name) - 1);
|
strncpy(ifr_tmp.ifr_name, ifr->ifr_name, sizeof(ifr_tmp.ifr_name) - 1);
|
||||||
#ifdef SIOCGIFNETMASK
|
#ifdef SIOCGIFNETMASK
|
||||||
@@ -199,7 +181,6 @@ load_interfaces()
|
|||||||
#endif /* _ISC */
|
#endif /* _ISC */
|
||||||
sin = (struct sockaddr_in *) &ifr_tmp.ifr_addr;
|
sin = (struct sockaddr_in *) &ifr_tmp.ifr_addr;
|
||||||
|
|
||||||
/* store the netmask */
|
|
||||||
interfaces[num_interfaces].netmask.s_addr = sin->sin_addr.s_addr;
|
interfaces[num_interfaces].netmask.s_addr = sin->sin_addr.s_addr;
|
||||||
} else {
|
} else {
|
||||||
#else
|
#else
|
||||||
@@ -213,13 +194,12 @@ load_interfaces()
|
|||||||
interfaces[num_interfaces].netmask.s_addr = htonl(IN_CLASSA_NET);
|
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++;
|
num_interfaces++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if there were bogus entries, realloc the array */
|
/* If the expected size < real size, realloc the array. */
|
||||||
if (n != num_interfaces) {
|
if (n != num_interfaces) {
|
||||||
/* it is unlikely that num_interfaces will be 0 but who knows... */
|
|
||||||
if (num_interfaces != 0)
|
if (num_interfaces != 0)
|
||||||
interfaces = (struct interface *) erealloc(interfaces,
|
interfaces = (struct interface *) erealloc(interfaces,
|
||||||
sizeof(struct interface) * num_interfaces);
|
sizeof(struct interface) * num_interfaces);
|
||||||
@@ -232,13 +212,9 @@ load_interfaces()
|
|||||||
|
|
||||||
#else /* !SIOCGIFCONF || STUB_LOAD_INTERFACES */
|
#else /* !SIOCGIFCONF || STUB_LOAD_INTERFACES */
|
||||||
|
|
||||||
/**********************************************************************
|
/*
|
||||||
*
|
* Stub function for those without SIOCGIFCONF
|
||||||
* load_interfaces()
|
|
||||||
*
|
|
||||||
* Stub function for those without SIOCGIFCONF
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void
|
void
|
||||||
load_interfaces()
|
load_interfaces()
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user