2
0
mirror of https://github.com/sudo-project/sudo.git synced 2025-09-04 00:05:11 +00:00

ENOTDIR is ok now too (in case part of the path is bogus)

This commit is contained in:
Todd C. Miller
1993-09-04 18:08:15 +00:00
parent 223b37b147
commit 51deb28dbb

View File

@@ -34,217 +34,198 @@
* Jeff Nieusma Thu Mar 21 23:11:23 MST 1991 * Jeff Nieusma Thu Mar 21 23:11:23 MST 1991
*/ */
/*
* Most of this code has been rewritten to fix bugs and bears little
* resemblence to the original. As such, this file conforms to my
* personal coding style.
*
* Todd C. Miller (millert@colorado.edu) Sat Sep 4 12:22:04 MDT 1993
*/
#include <stdio.h> #include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <string.h>
#include <strings.h> #include <strings.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include "sudo.h"
extern char *malloc(); extern char *malloc();
extern char *getenv(); extern char *getenv();
extern char *strcpy();
extern char **Argv; extern int fprintf();
char *find_path(); extern int readlink();
static char *do_stat(); extern int stat();
static char *check_link(); extern int lstat();
char *strdup(); #ifdef USE_CWD
extern char *getcwd();
#else
extern char *getwd();
#endif
/******************************************************************* /*******************************************************************
* *
* find_path() * find_path()
* *
* this function finds the full pathname for a command * this function finds the full pathname for a command
*/ */
char *find_path(file) char *find_path(file)
char *file; char *file;
{ {
register char *n; register char *n; /* for traversing path */
char *path=NULL; char *path = NULL; /* contents of PATH env var */
char *cmd; char fn[MAXPATHLEN+1]; /* filename (path + file) */
struct stat statbuf; /* for stat() */
char *qualify();
if ( strlen ( file ) > MAXPATHLEN ) { if (strlen(file) > MAXPATHLEN) {
fprintf ( stderr, "%s: path too long: %s\n", Argv[0], file ); fprintf(stderr, "%s: path too long: %s\n", Argv[0], file);
exit (1); exit(1);
} }
if ( *file == '.' && *(file+1) == '/' || *file == '/' )
return ( do_stat ( NULL, file ) );
if ( ( path=getenv("PATH") ) == NULL ) return ( NULL ) ;
if ( ( path=strdup(path) ) == NULL ) {
perror ( "find_path: malloc" );
exit (1);
}
while ( n = index ( path, ':' ) ) {
*n='\0';
if ( cmd = do_stat ( path, file ) ) return ( cmd );
path=n+1;
}
if ( cmd = do_stat ( path, file ) )
return ( cmd );
else
return ( NULL );
} /* do we need to search the path? */
if (index(file, '/'))
return (qualify(file));
/* grab PATH out of environment and make a local copy */
if ((path = getenv("PATH") ) == NULL)
return (NULL);
if ((path = strdup(path)) == NULL) {
perror("find_path: malloc");
/********************************************************************** exit(1);
*
* check_link()
*
* this function makes sure the argument is not a symbolic link.
* it returns the pathname of the binary or NULL
*/
static char *check_link(path)
char *path;
{
char buf1[MAXPATHLEN+1]; /* is the link */
char *s, *buf;
register int rtn;
/* the recursive buck stops here */
if ( path == NULL ) return NULL ;
/* I'd rather play with pointers than arrays... */
buf = buf1;
/* If this is NOT a sym link, return */
if ( ( rtn=readlink(path, buf, MAXPATHLEN)) < 0 )
return (path);
/* if it is a sym link, NULL terminate the string */
buf[rtn]='\0';
/* if the link points to an absolute path, start again... */
if ( *buf == '/' ) return ( do_stat( NULL, buf ) );
/* if the link points to ./something or something/ we need to
* strip off the filename portion of the current path */
if ( ( s=rindex(path,'/') ) == NULL ) {
fprintf( stderr, "check_link: This path is very wierd: %s \n", path );
exit (1);
} }
else
*s='\0';
/* as long as the link has ./ or ../ in it, get rid of it... */ while ((n = index(path, ':'))) {
while ( *buf == '.' ) { *n='\0';
strcpy(fn, path);
strcat(fn, "/");
strcat(fn, file);
if ( strncmp(buf, "../", 3) == 0 ) { /* stat the file to make sure it exists and is executable */
if ( ( s=rindex(path, '/')) ) { if (!stat(fn, &statbuf) && (statbuf.st_mode & 0000111))
*s='\0'; return (qualify(fn));
if ( *path == '\0' ) strcpy ( path, "/" ); else if (errno == ENOENT || errno == ENOTDIR)
} path=n+1;
buf += 3; else {
continue; perror("find_path: stat");
exit(1);
} }
else if ( strncmp(buf, "./", 2) == 0 ) {
buf += 2;
continue;
}
else
break;
} }
return(NULL);
/* we have to copy the path buffer since do_stat() will bzero() it */
if ( ( s = strdup ( path ) ) == NULL ) {
perror ( "check_link: malloc" );
exit (1);
}
return ( do_stat ( s, buf ) );
} }
/****************************************************************** /******************************************************************
* *
* do_stat() * qualify()
* *
* This function takes a path and a file and stat()s the file * this function takes a path and makes it fully qualified and resolves
* If the file exists and is executable, the full path to that * all symbolic links, returning the fully qualfied path.
* file is returned otherwise NULL is returned.
*/ */
static char *do_stat( path, file ) char *qualify(n)
char *path, *file; char *n; /* name to make fully qualified */
{ {
static char buf[MAXPATHLEN+1]; char *beg = NULL; /* begining of a path component */
struct stat s; char *end; /* end of a path component */
register char type; static char full[MAXPATHLEN+1]; /* the fully qualified name */
char name[MAXPATHLEN+1]; /* local copy of n */
struct stat statbuf; /* for lstat() */
char *tmp; /* temporary pointer */
/* is it a bogus path? */
if (stat(n, &statbuf)) {
if (errno == ENOENT)
return(NULL);
else {
perror("qualify: stat");
exit(1);
}
}
if (index(file, '/') && *file != '/' && strncmp(file, "./", 2) /* if n is relative, fill full with working dir */
&& strncmp(file, "../", 3)) if (*n != '/') {
type=3; #ifdef USE_CWD
else if ( *file == '.' && *(file+1) == '/' ) if (!getcwd(full, (size_t)(MAXPATHLEN+1))) {
type=1;
else if ( *file == '/' )
type=2;
else if ( path == NULL )
type=2;
else if ( *path == '.' && *(path+1) == (char)NULL )
type=3;
else
type=0;
switch ( type ) {
case 1:
file += 2;
case 3:
if ( (path=(char *)malloc(MAXPATHLEN+1)) == NULL ) {
perror ("do_stat: malloc");
exit (1);
}
#ifdef hpux
if ( ! getcwd ( path, (size_t)(MAXPATHLEN+1) ) ) {
perror ("do_stat: getcwd");
exit (1);
}
#else #else
if ( ! getwd ( path ) ) { if (!getwd(full)) {
perror ("do_stat: getwd");
exit (1);
}
#endif #endif
break; fprintf(stderr, "%s: Can't get working directory!\n", Argv[0]);
case 2: exit(1);
default: }
break; } else
} full[0] = '\0';
if ( ( ( path? strlen(path) : 0 ) + strlen (file) ) > MAXPATHLEN - 1 ) {
fprintf ( stderr, "%s: path too long: %s/%s\n", Argv[0], path, file );
exit (1);
}
bzero ( buf, MAXPATHLEN+1 ); (void)strcpy(name, n); /* working copy... */
if ( path ) strcat ( buf, path );
if ( *file != '/' && path [strlen(path)-1] != '/' ) strcat ( buf, "/" );
strcat ( buf, file );
/* make sure file exists and is executable */ do { /* while (end) */
if ( ! stat ( buf, &s ) && (s.st_mode & 0000111) ) if (beg)
return ( check_link ( buf ) ); beg = end + 1; /* skip past the NULL */
else else
return ( NULL ); beg = name; /* just starting out... */
/* find and terminate end of path component */
if ((end = index(beg, '/')))
*end = '\0';
if (beg == end)
continue;
else if (!strcmp(beg, "."))
; /* ignore "." */
else if (!strcmp(beg, "..")) {
tmp = rindex(full, '/');
if (tmp && tmp != &full[0])
*tmp = '\0';
} else {
strcat(full, "/");
strcat(full, beg); /* copy in new component */
}
/* check for symbolic links */
if (lstat(full, &statbuf)) {
perror("qualify: lstat");
exit(1);
}
if ((statbuf.st_mode & S_IFMT) == S_IFLNK) {
int linklen; /* length of link contents */
char newname[MAXPATHLEN+1]; /* temp storage to build new name */
linklen = readlink(full, newname, sizeof(newname));
newname[linklen] = '\0';
/* check to make sure we don't go past MAXPATHLEN */
++end;
if (end != (char *)1) {
if (linklen + strlen(end) >= MAXPATHLEN) {
fprintf(stderr, "%s: path too long: %s/%s\n", Argv[0],
newname, end);
exit(1);
}
strcat(newname, "/");
strcat(newname, end); /* copy what's left of end */
}
if (newname[0] == '/') /* reset full if necesary */
full[0] = '\0';
else
if ((tmp = rindex(full, '/'))) /* remove component from full */
*tmp = '\0';
strcpy(name, newname); /* reset name with new path */
beg = NULL; /* since we have a new name */
}
} while (end);
return((char *)full);
} }
#ifdef NEED_STRDUP
/****************************************************************** /******************************************************************
* *
* strdup() * strdup()
@@ -254,11 +235,14 @@ else
*/ */
char *strdup(s1) char *strdup(s1)
char *s1; char *s1;
{ {
char *s; char *s;
if ( ( s=(char *)malloc(strlen(s1)+1)) == NULL )
return (NULL); if ((s = (char *) malloc(strlen(s1) + 1)) == NULL)
strcpy(s,s1); return (NULL);
return (s);
(void)strcpy(s, s1);
return(s);
} }
#endif