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