2
0
mirror of https://github.com/sudo-project/sudo.git synced 2025-08-22 18:08:23 +00:00
sudo/find_path.c

187 lines
5.1 KiB
C
Raw Normal View History

/*
1998-09-17 16:27:15 +00:00
* CU sudo version 1.5.7
1993-02-16 03:01:06 +00:00
*
* 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.
*
1996-09-08 00:21:42 +00:00
* Please send bugs, changes, problems to sudo-bugs@courtesan.com
*
1993-02-16 03:01:06 +00:00
*******************************************************************
*
* 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.
*
1995-03-26 06:16:43 +00:00
* Todd C. Miller (millert@colorado.edu) Sat Mar 25 21:50:36 MST 1995
*/
#ifndef lint
1998-11-18 03:51:10 +00:00
static const char rcsid[] = "$Id$";
#endif /* lint */
#include "config.h"
1993-02-16 03:01:06 +00:00
#include <stdio.h>
#ifdef STDC_HEADERS
1993-11-30 00:14:02 +00:00
#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
1993-12-06 06:12:34 +00:00
#include <strings.h>
#endif /* HAVE_STRINGS_H */
#if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
#include <malloc.h>
#endif /* HAVE_MALLOC_H && !STDC_HEADERS */
#include <errno.h>
1993-02-16 03:01:06 +00:00
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
1994-08-12 01:58:03 +00:00
#include <netinet/in.h>
#include "sudo.h"
#ifndef STDC_HEADERS
1994-08-08 17:05:22 +00:00
#ifndef __GNUC__ /* gcc has its own malloc */
extern char *malloc __P((size_t));
1994-08-08 17:05:22 +00:00
#endif /* __GNUC__ */
extern char *getenv __P((const char *));
extern char *strcpy __P((char *, const char *));
extern int fprintf __P((FILE *, const char *, ...));
1995-07-18 17:35:56 +00:00
extern ssize_t readlink __P((const char *, VOID *, size_t));
extern int stat __P((const char *, struct stat *));
extern int lstat __P((const char *, struct stat *));
#ifdef HAVE_STRDUP
extern char *strdup __P((const char *));
#endif /* HAVE_STRDUP */
#endif /* !STDC_HEADERS */
1993-02-16 03:01:06 +00:00
#ifndef _S_IFMT
#define _S_IFMT S_IFMT
#endif /* _S_IFMT */
#ifndef _S_IFLNK
#define _S_IFLNK S_IFLNK
#endif /* _S_IFLNK */
1993-02-16 03:01:06 +00:00
/*******************************************************************
*
* find_path()
1993-02-16 03:01:06 +00:00
*
1995-03-26 06:16:43 +00:00
* 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.
1993-02-16 03:01:06 +00:00
*/
int find_path(infile, outfile)
char *infile; /* file to find */
char **outfile; /* result parameter */
1993-02-16 03:01:06 +00:00
{
1998-04-06 03:15:39 +00:00
static char command[MAXPATHLEN]; /* qualified filename */
1993-10-04 19:10:33 +00:00
register 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 */
1993-10-04 19:10:33 +00:00
int checkdot = 0; /* check current dir? */
1995-03-29 20:58:57 +00:00
command[0] = '\0';
1995-03-26 06:16:43 +00:00
if (strlen(infile) >= MAXPATHLEN) {
1995-03-26 06:16:43 +00:00
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.
1993-10-04 19:10:33 +00:00
*/
if (strchr(infile, '/')) {
(void) strcpy(command, infile);
if (sudo_goodpath(command)) {
*outfile = command;
return(FOUND);
} else
return(NOT_FOUND);
}
1993-02-16 03:01:06 +00:00
1993-10-04 19:10:33 +00:00
/*
* grab PATH out of environment and make a local copy
*/
if ((path = getenv("PATH")) == NULL)
return(NOT_FOUND);
1993-02-16 03:01:06 +00:00
if ((path = (char *) strdup(path)) == NULL) {
(void) fprintf(stderr, "%s: out of memory!\n", Argv[0]);
exit(1);
1993-02-16 03:01:06 +00:00
}
origpath = path;
1993-02-16 03:01:06 +00:00
1995-03-26 06:16:43 +00:00
/* XXX use strtok() */
do {
if ((n = strchr(path, ':')))
*n = '\0';
/*
1993-10-04 19:10:33 +00:00
* search current dir last if it is in PATH This will miss sneaky
* things like using './' or './/'
*/
1993-11-30 00:14:02 +00:00
if (*path == '\0' || (*path == '.' && *(path + 1) == '\0')) {
checkdot = 1;
path = n + 1;
continue;
}
1993-10-04 19:10:33 +00:00
/*
* resolve the path and exit the loop if found
1993-10-04 19:10:33 +00:00
*/
if (strlen(path) + strlen(infile) + 1 >= MAXPATHLEN) {
(void) fprintf(stderr, "%s: path too long: %s\n", Argv[0], infile);
1995-03-26 06:16:43 +00:00
exit(1);
}
(void) sprintf(command, "%s/%s", path, infile);
1995-03-26 06:16:43 +00:00
if ((result = sudo_goodpath(command)))
break;
path = n + 1;
} while (n);
(void) free(origpath);
1993-10-04 19:10:33 +00:00
/*
* Check current dir if dot was in the PATH
1993-10-04 19:10:33 +00:00
*/
if (!result && checkdot) {
result = sudo_goodpath(infile);
#ifdef IGNORE_DOT_PATH
if (result)
return(NOT_FOUND_DOT);
1996-03-19 00:37:24 +00:00
#endif /* IGNORE_DOT_PATH */
}
if (result) {
*outfile = result;
return(FOUND);
} else
return(NOT_FOUND);
1995-03-26 01:39:14 +00:00
}