Debian Codegraph @

src/realpath.c

raw
1 /*
2 libfakechroot -- fake chroot environment
3 Copyright (c) 2010, 2013 Piotr Roszatycki <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20
21 #include <config.h>
22
23 #ifdef HAVE___LXSTAT64
24 # define _LARGEFILE64_SOURCE
25 #endif
26
27 #include <stddef.h>
28 #include <sys/stat.h>
29 #ifdef HAVE_ALLOCA_H
30 # include <alloca.h>
31 #endif
32 #include <errno.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35
36 #include "libfakechroot.h"
37 #include "readlink.h"
38
39 #ifdef HAVE___LXSTAT64
40 # define _LARGEFILE64_SOURCE
41 # include "__lxstat64.h"
42 # define STAT_T stat64
43 # define LSTAT_REL(rpath, st) __lxstat64_rel(_STAT_VER, rpath, st)
44 #else
45 # include "lstat.h"
46 # define STAT_T stat
47 # define LSTAT_REL(rpath, st) lstat_rel(rpath, st)
48 #endif
49
50
51 /*
52 realpath function taken from Gnulib.
53 Copyright (C) 1996-2010 Free Software Foundation, Inc.
54 */
55
56 #ifndef MAXSYMLINKS
57 # ifdef SYMLOOP_MAX
58 # define MAXSYMLINKS SYMLOOP_MAX
59 # else
60 # define MAXSYMLINKS 20
61 # endif
62 #endif
63
64 #ifndef DOUBLE_SLASH_IS_DISTINCT_ROOT
65 # define DOUBLE_SLASH_IS_DISTINCT_ROOT 0
66 #endif
67
68 wrapper(realpath, char *, (const char * name, char * resolved))
69 {
70 char *rpath, *dest, *extra_buf = NULL;
71 const char *start, *end, *rpath_limit;
72 long int path_max;
73 int num_links = 0;
74
75 debug("realpath(\"%s\", &resolved)", name);
76 if (name == NULL) {
77 /* As per Single Unix Specification V2 we must return an error if
78 either parameter is a null pointer. We extend this to allow
79 the RESOLVED parameter to be NULL in case the we are expected to
80 allocate the room for the return value. */
81 __set_errno(EINVAL);
82 return NULL;
83 }
84
85 if (name[0] == '\0') {
86 /* As per Single Unix Specification V2 we must return an error if
87 the name argument points to an empty string. */
88 __set_errno(ENOENT);
89 return NULL;
90 }
91
92 path_max = FAKECHROOT_PATH_MAX;
93
94 if (resolved == NULL) {
95 rpath = malloc(path_max);
96 if (rpath == NULL) {
97 /* It's easier to set errno to ENOMEM than to rely on the
98 'malloc-posix' gnulib module. */
99 __set_errno(ENOMEM);
100 return NULL;
101 }
102 } else
103 rpath = resolved;
104 rpath_limit = rpath + path_max;
105
106 if (name[0] != '/') {
107 if (!getcwd(rpath, path_max)) {
108 rpath[0] = '\0';
109 goto error;
110 }
111 dest = strchr(rpath, '\0');
112 } else {
113 rpath[0] = '/';
114 dest = rpath + 1;
115 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && name[1] == '/')
116 *dest++ = '/';
117 }
118
119 for (start = end = name; *start; start = end) {
120 struct STAT_T st;
121 int n;
122
123 /* Skip sequence of multiple path-separators. */
124 while (*start == '/')
125 ++start;
126
127 /* Find end of path component. */
128 for (end = start; *end && *end != '/'; ++end)
129 /* Nothing. */;
130
131 if (end - start == 0)
132 break;
133 else if (end - start == 1 && start[0] == '.') {
134 /* nothing */
135 } else if (end - start == 2 && start[0] == '.' && start[1] == '.') {
136 /* Back up to previous component, ignore if at root already. */
137 if (dest > rpath + 1)
138 while ((--dest)[-1] != '/');
139 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1
140 && *dest == '/')
141 dest++;
142 } else {
143 size_t new_size;
144
145 if (dest[-1] != '/')
146 *dest++ = '/';
147
148 if (dest + (end - start) >= rpath_limit) {
149 ptrdiff_t dest_offset = dest - rpath;
150 char *new_rpath;
151
152 if (resolved) {
153 __set_errno(ENAMETOOLONG);
154 if (dest > rpath + 1)
155 dest--;
156 *dest = '\0';
157 goto error;
158 }
159 new_size = rpath_limit - rpath;
160 if (end - start + 1 > path_max)
161 new_size += end - start + 1;
162 else
163 new_size += path_max;
164 new_rpath = (char *) realloc(rpath, new_size);
165 if (new_rpath == NULL) {
166 /* It's easier to set errno to ENOMEM than to rely on the
167 'realloc-posix' gnulib module. */
168 __set_errno(ENOMEM);
169 goto error;
170 }
171 rpath = new_rpath;
172 rpath_limit = rpath + new_size;
173
174 dest = rpath + dest_offset;
175 }
176
177 memcpy(dest, start, end - start);
178 dest += end - start;
179 *dest = '\0';
180
181 if (LSTAT_REL (rpath, &st) < 0)
182 goto error;
183
184 if (S_ISLNK (st.st_mode)) {
185 char *buf;
186 size_t len;
187
188 if (++num_links > MAXSYMLINKS) {
189 __set_errno(ELOOP);
190 goto error;
191 }
192
193 buf = alloca(path_max);
194 if (!buf) {
195 __set_errno(ENOMEM);
196 goto error;
197 }
198
199 n = readlink(rpath, buf, path_max - 1);
200 if (n < 0) {
201 int saved_errno = errno;
202 __set_errno(saved_errno);
203 goto error;
204 }
205 buf[n] = '\0';
206
207 if (!extra_buf) {
208 extra_buf = alloca(path_max);
209 if (!extra_buf) {
210 __set_errno(ENOMEM);
211 goto error;
212 }
213 }
214
215 len = strlen(end);
216 if ((long int) (n + len) >= path_max) {
217 __set_errno(ENAMETOOLONG);
218 goto error;
219 }
220
221 /* Careful here, end may be a pointer into extra_buf... */
222 memmove(&extra_buf[n], end, len + 1);
223 name = end = memcpy(extra_buf, buf, n);
224
225 if (buf[0] == '/') {
226 dest = rpath + 1; /* It's an absolute symlink */
227 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && buf[1] == '/')
228 *dest++ = '/';
229 } else {
230 /* Back up to previous component, ignore if at root
231 already: */
232 if (dest > rpath + 1)
233 while ((--dest)[-1] != '/');
234 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1
235 && *dest == '/')
236 dest++;
237 }
238 } else if (!S_ISDIR (st.st_mode) && *end != '\0') {
239 __set_errno(ENOTDIR);
240 goto error;
241 }
242 }
243 }
244 if (dest > rpath + 1 && dest[-1] == '/')
245 --dest;
246 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1 && *dest == '/')
247 dest++;
248 *dest = '\0';
249
250 return rpath;
251
252 error: {
253 int saved_errno = errno;
254 if (resolved == NULL)
255 free(rpath);
256 __set_errno(saved_errno);
257 }
258 return NULL;
259 }