[BACK]Return to find_path.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / sudo

Annotation of src/usr.bin/sudo/find_path.c, Revision 1.5

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 */
                     59: #include <errno.h>
                     60:
                     61: #include "sudo.h"
                     62:
                     63: #ifndef lint
1.5     ! millert    64: static const char rcsid[] = "$Sudo: find_path.c,v 1.101 2003/03/15 20:31:02 millert Exp $";
1.1       millert    65: #endif /* lint */
                     66:
                     67: /*
                     68:  * This function finds the full pathname for a command and
                     69:  * stores it in a statically allocated array, filling in a pointer
                     70:  * to the array.  Returns FOUND if the command was found, NOT_FOUND
                     71:  * if it was not found, or NOT_FOUND_DOT if it would have been found
                     72:  * but it is in '.' and IGNORE_DOT is set.
                     73:  */
                     74: int
1.3       millert    75: find_path(infile, outfile, path)
1.1       millert    76:     char *infile;              /* file to find */
                     77:     char **outfile;            /* result parameter */
1.3       millert    78:     char *path;                        /* path to search */
1.1       millert    79: {
                     80:     static char command[MAXPATHLEN]; /* qualified filename */
                     81:     char *n;                   /* for traversing path */
                     82:     char *origpath;            /* so we can free path later */
                     83:     char *result = NULL;       /* result of path/file lookup */
                     84:     int checkdot = 0;          /* check current dir? */
1.5     ! millert    85:     int len;                   /* length parameter */
1.1       millert    86:
                     87:     if (strlen(infile) >= MAXPATHLEN) {
                     88:        (void) fprintf(stderr, "%s: path too long: %s\n", Argv[0], infile);
                     89:        exit(1);
                     90:     }
                     91:
                     92:     /*
                     93:      * If we were given a fully qualified or relative path
                     94:      * there is no need to look at $PATH.
                     95:      */
                     96:     if (strchr(infile, '/')) {
1.5     ! millert    97:        strlcpy(command, infile, sizeof(command));      /* paranoia */
1.1       millert    98:        if (sudo_goodpath(command)) {
                     99:            *outfile = command;
                    100:            return(FOUND);
                    101:        } else
                    102:            return(NOT_FOUND);
                    103:     }
                    104:
1.3       millert   105:     /* Use PATH passed in unless SECURE_PATH is in effect.  */
                    106: #ifdef SECURE_PATH
                    107:     if (!user_is_exempt())
                    108:        path = SECURE_PATH;
                    109: #endif /* SECURE_PATH */
                    110:     if (path == NULL)
1.1       millert   111:        return(NOT_FOUND);
                    112:     path = estrdup(path);
                    113:     origpath = path;
                    114:
                    115:     do {
                    116:        if ((n = strchr(path, ':')))
                    117:            *n = '\0';
                    118:
                    119:        /*
                    120:         * Search current dir last if it is in PATH This will miss sneaky
                    121:         * things like using './' or './/'
                    122:         */
                    123:        if (*path == '\0' || (*path == '.' && *(path + 1) == '\0')) {
                    124:            checkdot = 1;
                    125:            path = n + 1;
                    126:            continue;
                    127:        }
                    128:
                    129:        /*
                    130:         * Resolve the path and exit the loop if found.
                    131:         */
1.5     ! millert   132:        len = snprintf(command, sizeof(command), "%s/%s", path, infile);
        !           133:        if (len <= 0 || len >= sizeof(command)) {
1.1       millert   134:            (void) fprintf(stderr, "%s: path too long: %s\n", Argv[0], infile);
                    135:            exit(1);
                    136:        }
                    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: }