build: update gnulib submodule to latest
[libguestfs.git] / daemon / realpath.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2009 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <limits.h>
27 #include <sys/types.h>
28 #include <dirent.h>
29
30 #include "openat.h"
31
32 #include "daemon.h"
33 #include "actions.h"
34
35 /* On Windows, NAME_MAX is not defined. */
36 #ifndef NAME_MAX
37 #define NAME_MAX FILENAME_MAX
38 #endif
39
40 char *
41 do_realpath (const char *path)
42 {
43   char *ret;
44
45   CHROOT_IN;
46   ret = realpath (path, NULL);
47   CHROOT_OUT;
48   if (ret == NULL) {
49     reply_with_perror ("realpath");
50     return NULL;
51   }
52
53   return ret;                   /* caller frees */
54 }
55
56 char *
57 do_case_sensitive_path (const char *path)
58 {
59   char ret[PATH_MAX+1] = "/";
60   size_t next = 1;
61   int fd_cwd;
62
63   /* 'fd_cwd' here is a surrogate for the current working directory, so
64    * that we don't have to actually call chdir(2).
65    */
66   fd_cwd = open (sysroot, O_RDONLY | O_DIRECTORY);
67   if (fd_cwd == -1) {
68     reply_with_perror ("%s", sysroot);
69     return NULL;
70   }
71
72   /* First character is a '/'.  Take each subsequent path element
73    * and follow it.
74    */
75   while (*path) {
76     size_t i = strcspn (path, "/");
77     if (i == 0) {
78       path++;
79       continue;
80     }
81
82     if (verbose)
83       fprintf (stderr, "case_sensitive_path: path = %s, next = %zu, i = %zu\n",
84                path, next, i);
85
86     if ((i == 1 && path[0] == '.') ||
87         (i == 2 && path[0] == '.' && path[1] == '.')) {
88       reply_with_error ("case_sensitive_path: path contained . or .. elements");
89       goto error;
90     }
91     if (i > NAME_MAX) {
92       reply_with_error ("case_sensitive_path: path element too long");
93       goto error;
94     }
95
96     char name[NAME_MAX+1];
97     memcpy (name, path, i);
98     name[i] = '\0';
99
100     /* Skip to next element in path (for the next loop iteration). */
101     path += i;
102
103     /* Read the current directory looking (case insensitively) for
104      * this element of the path.
105      */
106     int fd2 = dup (fd_cwd); /* because closedir will close it */
107     if (fd2 == -1) {
108       reply_with_perror ("dup");
109       goto error;
110     }
111     DIR *dir = fdopendir (fd2);
112     if (dir == NULL) {
113       reply_with_perror ("opendir");
114       goto error;
115     }
116
117     struct dirent *d = NULL;
118
119     errno = 0;
120     while ((d = readdir (dir)) != NULL) {
121       if (STRCASEEQ (d->d_name, name))
122         break;
123     }
124
125     if (d == NULL && errno != 0) {
126       reply_with_perror ("readdir");
127       goto error;
128     }
129
130     if (closedir (dir) == -1) {
131       reply_with_perror ("closedir");
132       goto error;
133     }
134
135     if (d == NULL) {
136       reply_with_error ("%s: no file or directory found with this name", name);
137       goto error;
138     }
139
140     /* Add the real name of this path element to the return value. */
141     if (next > 1)
142       ret[next++] = '/';
143
144     i = strlen (d->d_name);
145     if (next + i >= PATH_MAX) {
146       reply_with_error ("final path too long");
147       goto error;
148     }
149
150     strcpy (&ret[next], d->d_name);
151     next += i;
152
153     /* Is it a directory?  Try going into it. */
154     fd2 = openat (fd_cwd, d->d_name, O_RDONLY | O_DIRECTORY);
155     int err = errno;
156     close (fd_cwd);
157     fd_cwd = fd2;
158     errno = err;
159     if (fd_cwd == -1) {
160       /* ENOTDIR is OK provided we've reached the end of the path. */
161       if (errno != ENOTDIR) {
162         reply_with_perror ("openat: %s", d->d_name);
163         goto error;
164       }
165
166       if (*path) {
167         reply_with_error ("%s: non-directory element in path", d->d_name);
168         goto error;
169       }
170     }
171   }
172
173   close (fd_cwd);
174
175   ret[next] = '\0';
176   char *retp = strdup (ret);
177   if (retp == NULL) {
178     reply_with_perror ("strdup");
179     return NULL;
180   }
181   return retp;                  /* caller frees */
182
183  error:
184   close (fd_cwd);
185   return NULL;
186 }