daemon: debug segv correct use of dereferencing NULL.
[libguestfs.git] / daemon / realpath.c
index bfe8e67..ea936c6 100644 (file)
@@ -1,5 +1,5 @@
 /* libguestfs - the guestfsd daemon
- * Copyright (C) 2009 Red Hat Inc.
+ * Copyright (C) 2009-2011 Red Hat Inc.
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -13,7 +13,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 #include <config.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <fcntl.h>
 #include <limits.h>
 #include <sys/types.h>
 #include <dirent.h>
 
-#include "ignore-value.h"
-
 #include "daemon.h"
+#include "optgroups.h"
 #include "actions.h"
 
+/* On Windows, NAME_MAX is not defined. */
+#ifndef NAME_MAX
+#define NAME_MAX FILENAME_MAX
+#endif
+
+int
+optgroup_realpath_available (void)
+{
+#ifdef HAVE_REALPATH
+  return 1;
+#else
+  return 0;
+#endif
+}
+
 char *
 do_realpath (const char *path)
 {
+#ifdef HAVE_REALPATH
   char *ret;
 
   CHROOT_IN;
   ret = realpath (path, NULL);
   CHROOT_OUT;
   if (ret == NULL) {
-    reply_with_perror ("realpath");
+    reply_with_perror ("%s", path);
     return NULL;
   }
 
   return ret;                  /* caller frees */
+#else
+  NOT_AVAILABLE (NULL);
+#endif
 }
 
 char *
@@ -52,9 +71,13 @@ do_case_sensitive_path (const char *path)
 {
   char ret[PATH_MAX+1] = "/";
   size_t next = 1;
+  int fd_cwd;
 
-  /* MUST chdir ("/") before leaving this function. */
-  if (chdir (sysroot) == -1) {
+  /* 'fd_cwd' here is a surrogate for the current working directory, so
+   * that we don't have to actually call chdir(2).
+   */
+  fd_cwd = open (sysroot, O_RDONLY | O_DIRECTORY);
+  if (fd_cwd == -1) {
     reply_with_perror ("%s", sysroot);
     return NULL;
   }
@@ -69,17 +92,13 @@ do_case_sensitive_path (const char *path)
       continue;
     }
 
-    if (verbose)
-      fprintf (stderr, "case_sensitive_path: path = %s, next = %zu, i = %zu\n",
-               path, next, i);
-
     if ((i == 1 && path[0] == '.') ||
         (i == 2 && path[0] == '.' && path[1] == '.')) {
-      reply_with_error ("case_sensitive_path: path contained . or .. elements");
+      reply_with_error ("path contained . or .. elements");
       goto error;
     }
     if (i > NAME_MAX) {
-      reply_with_error ("case_sensitive_path: path element too long");
+      reply_with_error ("path element too long");
       goto error;
     }
 
@@ -93,7 +112,12 @@ do_case_sensitive_path (const char *path)
     /* Read the current directory looking (case insensitively) for
      * this element of the path.
      */
-    DIR *dir = opendir (".");
+    int fd2 = dup (fd_cwd); /* because closedir will close it */
+    if (fd2 == -1) {
+      reply_with_perror ("dup");
+      goto error;
+    }
+    DIR *dir = fdopendir (fd2);
     if (dir == NULL) {
       reply_with_perror ("opendir");
       goto error;
@@ -103,7 +127,7 @@ do_case_sensitive_path (const char *path)
 
     errno = 0;
     while ((d = readdir (dir)) != NULL) {
-      if (strcasecmp (d->d_name, name) == 0)
+      if (STRCASEEQ (d->d_name, name))
         break;
     }
 
@@ -136,10 +160,15 @@ do_case_sensitive_path (const char *path)
     next += i;
 
     /* Is it a directory?  Try going into it. */
-    if (chdir (d->d_name) == -1) {
+    fd2 = openat (fd_cwd, d->d_name, O_RDONLY | O_DIRECTORY);
+    int err = errno;
+    close (fd_cwd);
+    fd_cwd = fd2;
+    errno = err;
+    if (fd_cwd == -1) {
       /* ENOTDIR is OK provided we've reached the end of the path. */
       if (errno != ENOTDIR) {
-        reply_with_perror ("chdir: %s", d->d_name);
+        reply_with_perror ("openat: %s", d->d_name);
         goto error;
       }
 
@@ -150,7 +179,8 @@ do_case_sensitive_path (const char *path)
     }
   }
 
-  ignore_value (chdir ("/"));
+  if (fd_cwd >= 0)
+    close (fd_cwd);
 
   ret[next] = '\0';
   char *retp = strdup (ret);
@@ -161,6 +191,8 @@ do_case_sensitive_path (const char *path)
   return retp;                  /* caller frees */
 
  error:
-  ignore_value (chdir ("/"));
+  if (fd_cwd >= 0)
+    close (fd_cwd);
+
   return NULL;
 }