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