daemon: Use gnulib futimens module.
[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 "daemon.h"
31 #include "actions.h"
32
33 /* On Windows, NAME_MAX is not defined. */
34 #ifndef NAME_MAX
35 #define NAME_MAX FILENAME_MAX
36 #endif
37
38 char *
39 do_realpath (const char *path)
40 {
41   char *ret;
42
43   CHROOT_IN;
44   ret = realpath (path, NULL);
45   CHROOT_OUT;
46   if (ret == NULL) {
47     reply_with_perror ("realpath");
48     return NULL;
49   }
50
51   return ret;                   /* caller frees */
52 }
53
54 char *
55 do_case_sensitive_path (const char *path)
56 {
57   char ret[PATH_MAX+1] = "/";
58   size_t next = 1;
59   int fd_cwd;
60
61   /* 'fd_cwd' here is a surrogate for the current working directory, so
62    * that we don't have to actually call chdir(2).
63    */
64   fd_cwd = open (sysroot, O_RDONLY | O_DIRECTORY);
65   if (fd_cwd == -1) {
66     reply_with_perror ("%s", sysroot);
67     return NULL;
68   }
69
70   /* First character is a '/'.  Take each subsequent path element
71    * and follow it.
72    */
73   while (*path) {
74     size_t i = strcspn (path, "/");
75     if (i == 0) {
76       path++;
77       continue;
78     }
79
80     if (verbose)
81       fprintf (stderr, "case_sensitive_path: path = %s, next = %zu, i = %zu\n",
82                path, next, i);
83
84     if ((i == 1 && path[0] == '.') ||
85         (i == 2 && path[0] == '.' && path[1] == '.')) {
86       reply_with_error ("case_sensitive_path: path contained . or .. elements");
87       goto error;
88     }
89     if (i > NAME_MAX) {
90       reply_with_error ("case_sensitive_path: path element too long");
91       goto error;
92     }
93
94     char name[NAME_MAX+1];
95     memcpy (name, path, i);
96     name[i] = '\0';
97
98     /* Skip to next element in path (for the next loop iteration). */
99     path += i;
100
101     /* Read the current directory looking (case insensitively) for
102      * this element of the path.
103      */
104     int fd2 = dup (fd_cwd); /* because closedir will close it */
105     if (fd2 == -1) {
106       reply_with_perror ("dup");
107       goto error;
108     }
109     DIR *dir = fdopendir (fd2);
110     if (dir == NULL) {
111       reply_with_perror ("opendir");
112       goto error;
113     }
114
115     struct dirent *d = NULL;
116
117     errno = 0;
118     while ((d = readdir (dir)) != NULL) {
119       if (STRCASEEQ (d->d_name, name))
120         break;
121     }
122
123     if (d == NULL && errno != 0) {
124       reply_with_perror ("readdir");
125       goto error;
126     }
127
128     if (closedir (dir) == -1) {
129       reply_with_perror ("closedir");
130       goto error;
131     }
132
133     if (d == NULL) {
134       reply_with_error ("%s: no file or directory found with this name", name);
135       goto error;
136     }
137
138     /* Add the real name of this path element to the return value. */
139     if (next > 1)
140       ret[next++] = '/';
141
142     i = strlen (d->d_name);
143     if (next + i >= PATH_MAX) {
144       reply_with_error ("final path too long");
145       goto error;
146     }
147
148     strcpy (&ret[next], d->d_name);
149     next += i;
150
151     /* Is it a directory?  Try going into it. */
152     fd2 = openat (fd_cwd, d->d_name, O_RDONLY | O_DIRECTORY);
153     int err = errno;
154     close (fd_cwd);
155     fd_cwd = fd2;
156     errno = err;
157     if (fd_cwd == -1) {
158       /* ENOTDIR is OK provided we've reached the end of the path. */
159       if (errno != ENOTDIR) {
160         reply_with_perror ("openat: %s", d->d_name);
161         goto error;
162       }
163
164       if (*path) {
165         reply_with_error ("%s: non-directory element in path", d->d_name);
166         goto error;
167       }
168     }
169   }
170
171   close (fd_cwd);
172
173   ret[next] = '\0';
174   char *retp = strdup (ret);
175   if (retp == NULL) {
176     reply_with_perror ("strdup");
177     return NULL;
178   }
179   return retp;                  /* caller frees */
180
181  error:
182   close (fd_cwd);
183   return NULL;
184 }