Annotation of src/usr.bin/sudo/find_path.c, Revision 1.6
1.1 millert 1: /*
1.5 millert 2: * Copyright (c) 1996, 1998-2003 Todd C. Miller <Todd.Miller@courtesan.com>
1.1 millert 3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
8: *
9: * 1. Redistributions of source code must retain the above copyright
10: * notice, this list of conditions and the following disclaimer.
11: *
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: *
16: * 3. The name of the author may not be used to endorse or promote products
17: * derived from this software without specific prior written permission.
18: *
19: * 4. Products derived from this software may not be called "Sudo" nor
20: * may "Sudo" appear in their names without specific prior written
21: * permission from the author.
22: *
23: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
26: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33: */
34:
35: #include "config.h"
36:
1.3 millert 37: #include <sys/types.h>
38: #include <sys/param.h>
39: #include <sys/stat.h>
1.1 millert 40: #include <stdio.h>
41: #ifdef STDC_HEADERS
1.3 millert 42: # include <stdlib.h>
43: # include <stddef.h>
44: #else
45: # ifdef HAVE_STDLIB_H
46: # include <stdlib.h>
47: # endif
1.1 millert 48: #endif /* STDC_HEADERS */
1.3 millert 49: #ifdef HAVE_STRING_H
50: # include <string.h>
51: #else
52: # ifdef HAVE_STRINGS_H
53: # include <strings.h>
54: # endif
55: #endif /* HAVE_STRING_H */
1.1 millert 56: #ifdef HAVE_UNISTD_H
1.3 millert 57: # include <unistd.h>
1.1 millert 58: #endif /* HAVE_UNISTD_H */
1.6 ! millert 59: #ifdef HAVE_ERR_H
! 60: # include <err.h>
! 61: #else
! 62: # include "emul/err.h"
! 63: #endif /* HAVE_ERR_H */
1.1 millert 64:
65: #include "sudo.h"
66:
67: #ifndef lint
1.6 ! millert 68: static const char rcsid[] = "$Sudo: find_path.c,v 1.102 2003/04/02 18:25:19 millert Exp $";
1.1 millert 69: #endif /* lint */
70:
71: /*
72: * This function finds the full pathname for a command and
73: * stores it in a statically allocated array, filling in a pointer
74: * to the array. Returns FOUND if the command was found, NOT_FOUND
75: * if it was not found, or NOT_FOUND_DOT if it would have been found
76: * but it is in '.' and IGNORE_DOT is set.
77: */
78: int
1.3 millert 79: find_path(infile, outfile, path)
1.1 millert 80: char *infile; /* file to find */
81: char **outfile; /* result parameter */
1.3 millert 82: char *path; /* path to search */
1.1 millert 83: {
84: static char command[MAXPATHLEN]; /* qualified filename */
85: char *n; /* for traversing path */
86: char *origpath; /* so we can free path later */
87: char *result = NULL; /* result of path/file lookup */
88: int checkdot = 0; /* check current dir? */
1.5 millert 89: int len; /* length parameter */
1.1 millert 90:
1.6 ! millert 91: if (strlen(infile) >= MAXPATHLEN)
! 92: errx(1, "%s: File name too long", infile);
1.1 millert 93:
94: /*
95: * If we were given a fully qualified or relative path
96: * there is no need to look at $PATH.
97: */
98: if (strchr(infile, '/')) {
1.5 millert 99: strlcpy(command, infile, sizeof(command)); /* paranoia */
1.1 millert 100: if (sudo_goodpath(command)) {
101: *outfile = command;
102: return(FOUND);
103: } else
104: return(NOT_FOUND);
105: }
106:
1.3 millert 107: /* Use PATH passed in unless SECURE_PATH is in effect. */
108: #ifdef SECURE_PATH
109: if (!user_is_exempt())
110: path = SECURE_PATH;
111: #endif /* SECURE_PATH */
112: if (path == NULL)
1.1 millert 113: return(NOT_FOUND);
114: path = estrdup(path);
115: origpath = path;
116:
117: do {
118: if ((n = strchr(path, ':')))
119: *n = '\0';
120:
121: /*
122: * Search current dir last if it is in PATH This will miss sneaky
123: * things like using './' or './/'
124: */
125: if (*path == '\0' || (*path == '.' && *(path + 1) == '\0')) {
126: checkdot = 1;
127: path = n + 1;
128: continue;
129: }
130:
131: /*
132: * Resolve the path and exit the loop if found.
133: */
1.5 millert 134: len = snprintf(command, sizeof(command), "%s/%s", path, infile);
1.6 ! millert 135: if (len <= 0 || len >= sizeof(command))
! 136: errx(1, "%s: File name too long", infile);
1.1 millert 137: if ((result = sudo_goodpath(command)))
138: break;
139:
140: path = n + 1;
141:
142: } while (n);
143: free(origpath);
144:
145: /*
146: * Check current dir if dot was in the PATH
147: */
148: if (!result && checkdot) {
149: result = sudo_goodpath(infile);
150: if (result && def_flag(I_IGNORE_DOT))
151: return(NOT_FOUND_DOT);
152: }
153:
154: if (result) {
155: *outfile = result;
156: return(FOUND);
157: } else
158: return(NOT_FOUND);
159: }