Imported Upstream version 1.8.7
[debian/sudo] / plugins / sudoers / find_path.c
index 2566733638e1182e554cda8eac5d96b697bcb262..2d5261372975649325fa0e7f61cc6840996a970d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1996, 1998-2005, 2010-2011
+ * Copyright (c) 1996, 1998-2005, 2010-2013
  *     Todd C. Miller <Todd.Miller@courtesan.com>
  *
  * Permission to use, copy, modify, and distribute this software for any
@@ -22,7 +22,6 @@
 #include <config.h>
 
 #include <sys/types.h>
-#include <sys/param.h>
 #include <sys/stat.h>
 #include <stdio.h>
 #ifdef STDC_HEADERS
@@ -42,6 +41,7 @@
 #ifdef HAVE_UNISTD_H
 # include <unistd.h>
 #endif /* HAVE_UNISTD_H */
+#include <errno.h>
 
 #include "sudoers.h"
 
@@ -59,12 +59,15 @@ find_path(char *infile, char **outfile, struct stat *sbp, char *path,
     static char command[PATH_MAX]; /* qualified filename */
     char *n;                   /* for traversing path */
     char *origpath;            /* so we can free path later */
-    char *result = NULL;       /* result of path/file lookup */
-    int checkdot = 0;          /* check current dir? */
+    bool found = false;                /* did we find the command? */
+    bool checkdot = false;     /* check current dir? */
     int len;                   /* length parameter */
+    debug_decl(find_path, SUDO_DEBUG_UTIL)
 
-    if (strlen(infile) >= PATH_MAX)
-       errorx(1, "%s: File name too long", infile);
+    if (strlen(infile) >= PATH_MAX) {
+       errno = ENAMETOOLONG;
+       fatal("%s", infile);
+    }
 
     /*
      * If we were given a fully qualified or relative path
@@ -74,13 +77,13 @@ find_path(char *infile, char **outfile, struct stat *sbp, char *path,
        strlcpy(command, infile, sizeof(command));      /* paranoia */
        if (sudo_goodpath(command, sbp)) {
            *outfile = command;
-           return FOUND;
+           debug_return_int(FOUND);
        } else
-           return NOT_FOUND;
+           debug_return_int(NOT_FOUND);
     }
 
     if (path == NULL)
-       return NOT_FOUND;
+       debug_return_int(NOT_FOUND);
     path = estrdup(path);
     origpath = path;
 
@@ -102,9 +105,11 @@ find_path(char *infile, char **outfile, struct stat *sbp, char *path,
         * Resolve the path and exit the loop if found.
         */
        len = snprintf(command, sizeof(command), "%s/%s", path, infile);
-       if (len <= 0 || len >= sizeof(command))
-           errorx(1, "%s: File name too long", infile);
-       if ((result = sudo_goodpath(command, sbp)))
+       if (len <= 0 || len >= sizeof(command)) {
+           errno = ENAMETOOLONG;
+           fatal("%s", infile);
+       }
+       if ((found = sudo_goodpath(command, sbp)))
            break;
 
        path = n + 1;
@@ -115,18 +120,20 @@ find_path(char *infile, char **outfile, struct stat *sbp, char *path,
     /*
      * Check current dir if dot was in the PATH
      */
-    if (!result && checkdot) {
+    if (!found && checkdot) {
        len = snprintf(command, sizeof(command), "./%s", infile);
-       if (len <= 0 || len >= sizeof(command))
-           errorx(1, "%s: File name too long", infile);
-       result = sudo_goodpath(command, sbp);
-       if (result && ignore_dot)
-           return NOT_FOUND_DOT;
+       if (len <= 0 || len >= sizeof(command)) {
+           errno = ENAMETOOLONG;
+           fatal("%s", infile);
+       }
+       found = sudo_goodpath(command, sbp);
+       if (found && ignore_dot)
+           debug_return_int(NOT_FOUND_DOT);
     }
 
-    if (result) {
-       *outfile = result;
-       return FOUND;
+    if (found) {
+       *outfile = command;
+       debug_return_int(FOUND);
     } else
-       return NOT_FOUND;
+       debug_return_int(NOT_FOUND);
 }