Annotation of embedaddon/sudo/plugins/sudoers/find_path.c, revision 1.1.1.2

1.1       misho       1: /*
                      2:  * Copyright (c) 1996, 1998-2005, 2010-2011
                      3:  *     Todd C. Miller <Todd.Miller@courtesan.com>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  *
                     17:  * Sponsored in part by the Defense Advanced Research Projects
                     18:  * Agency (DARPA) and Air Force Research Laboratory, Air Force
                     19:  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
                     20:  */
                     21: 
                     22: #include <config.h>
                     23: 
                     24: #include <sys/types.h>
                     25: #include <sys/param.h>
                     26: #include <sys/stat.h>
                     27: #include <stdio.h>
                     28: #ifdef STDC_HEADERS
                     29: # include <stdlib.h>
                     30: # include <stddef.h>
                     31: #else
                     32: # ifdef HAVE_STDLIB_H
                     33: #  include <stdlib.h>
                     34: # endif
                     35: #endif /* STDC_HEADERS */
                     36: #ifdef HAVE_STRING_H
                     37: # include <string.h>
                     38: #endif /* HAVE_STRING_H */
                     39: #ifdef HAVE_STRINGS_H
                     40: # include <strings.h>
                     41: #endif /* HAVE_STRINGS_H */
                     42: #ifdef HAVE_UNISTD_H
                     43: # include <unistd.h>
                     44: #endif /* HAVE_UNISTD_H */
                     45: #include <errno.h>
                     46: 
                     47: #include "sudoers.h"
                     48: 
                     49: /*
                     50:  * This function finds the full pathname for a command and
                     51:  * stores it in a statically allocated array, filling in a pointer
                     52:  * to the array.  Returns FOUND if the command was found, NOT_FOUND
                     53:  * if it was not found, or NOT_FOUND_DOT if it would have been found
                     54:  * but it is in '.' and IGNORE_DOT is set.
                     55:  */
                     56: int
                     57: find_path(char *infile, char **outfile, struct stat *sbp, char *path,
                     58:     int ignore_dot)
                     59: {
                     60:     static char command[PATH_MAX]; /* qualified filename */
                     61:     char *n;                   /* for traversing path */
                     62:     char *origpath;            /* so we can free path later */
1.1.1.2 ! misho      63:     bool found = false;                /* did we find the command? */
        !            64:     bool checkdot = false;     /* check current dir? */
1.1       misho      65:     int len;                   /* length parameter */
1.1.1.2 ! misho      66:     debug_decl(find_path, SUDO_DEBUG_UTIL)
1.1       misho      67: 
                     68:     if (strlen(infile) >= PATH_MAX)
                     69:        errorx(1, _("%s: %s"), infile, strerror(ENAMETOOLONG));
                     70: 
                     71:     /*
                     72:      * If we were given a fully qualified or relative path
                     73:      * there is no need to look at $PATH.
                     74:      */
                     75:     if (strchr(infile, '/')) {
                     76:        strlcpy(command, infile, sizeof(command));      /* paranoia */
                     77:        if (sudo_goodpath(command, sbp)) {
                     78:            *outfile = command;
1.1.1.2 ! misho      79:            debug_return_int(FOUND);
1.1       misho      80:        } else
1.1.1.2 ! misho      81:            debug_return_int(NOT_FOUND);
1.1       misho      82:     }
                     83: 
                     84:     if (path == NULL)
1.1.1.2 ! misho      85:        debug_return_int(NOT_FOUND);
1.1       misho      86:     path = estrdup(path);
                     87:     origpath = path;
                     88: 
                     89:     do {
                     90:        if ((n = strchr(path, ':')))
                     91:            *n = '\0';
                     92: 
                     93:        /*
                     94:         * Search current dir last if it is in PATH This will miss sneaky
                     95:         * things like using './' or './/'
                     96:         */
                     97:        if (*path == '\0' || (*path == '.' && *(path + 1) == '\0')) {
                     98:            checkdot = 1;
                     99:            path = n + 1;
                    100:            continue;
                    101:        }
                    102: 
                    103:        /*
                    104:         * Resolve the path and exit the loop if found.
                    105:         */
                    106:        len = snprintf(command, sizeof(command), "%s/%s", path, infile);
                    107:        if (len <= 0 || len >= sizeof(command))
                    108:            errorx(1, _("%s: %s"), infile, strerror(ENAMETOOLONG));
1.1.1.2 ! misho     109:        if ((found = sudo_goodpath(command, sbp)))
1.1       misho     110:            break;
                    111: 
                    112:        path = n + 1;
                    113: 
                    114:     } while (n);
                    115:     efree(origpath);
                    116: 
                    117:     /*
                    118:      * Check current dir if dot was in the PATH
                    119:      */
1.1.1.2 ! misho     120:     if (!found && checkdot) {
1.1       misho     121:        len = snprintf(command, sizeof(command), "./%s", infile);
                    122:        if (len <= 0 || len >= sizeof(command))
                    123:            errorx(1, _("%s: %s"), infile, strerror(ENAMETOOLONG));
1.1.1.2 ! misho     124:        found = sudo_goodpath(command, sbp);
        !           125:        if (found && ignore_dot)
        !           126:            debug_return_int(NOT_FOUND_DOT);
1.1       misho     127:     }
                    128: 
1.1.1.2 ! misho     129:     if (found) {
        !           130:        *outfile = command;
        !           131:        debug_return_int(FOUND);
1.1       misho     132:     } else
1.1.1.2 ! misho     133:        debug_return_int(NOT_FOUND);
1.1       misho     134: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>