]>
Commit | Line | Data |
---|---|---|
9385eb3d A |
1 | /* |
2 | * Copyright (c) 2003 Constantin S. Svintsoff <kostik@iclub.nsu.ru> | |
3 | * | |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted provided that the following conditions | |
6 | * are met: | |
7 | * 1. Redistributions of source code must retain the above copyright | |
8 | * notice, this list of conditions and the following disclaimer. | |
9 | * 2. Redistributions in binary form must reproduce the above copyright | |
10 | * notice, this list of conditions and the following disclaimer in the | |
11 | * documentation and/or other materials provided with the distribution. | |
12 | * 3. The names of the authors may not be used to endorse or promote | |
13 | * products derived from this software without specific prior written | |
14 | * permission. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | |
17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
22 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
23 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
24 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
25 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
26 | * SUCH DAMAGE. | |
27 | */ | |
28 | ||
29 | #if defined(LIBC_SCCS) && !defined(lint) | |
30 | static char sccsid[] = "@(#)realpath.c 8.1 (Berkeley) 2/16/94"; | |
31 | #endif /* LIBC_SCCS and not lint */ | |
32 | #include <sys/cdefs.h> | |
33 | __FBSDID("$FreeBSD: src/lib/libc/stdlib/realpath.c,v 1.20 2003/05/28 08:23:01 fjoe Exp $"); | |
34 | ||
35 | #include "namespace.h" | |
36 | #include <sys/param.h> | |
37 | #include <sys/stat.h> | |
ad3c9f2a | 38 | #include <sys/mount.h> |
9385eb3d A |
39 | |
40 | #include <errno.h> | |
41 | #include <stdlib.h> | |
42 | #include <string.h> | |
43 | #include <unistd.h> | |
ad3c9f2a A |
44 | #include <sys/attr.h> |
45 | #include <sys/vnode.h> | |
9385eb3d A |
46 | #include "un-namespace.h" |
47 | ||
507116e3 | 48 | |
ad3c9f2a A |
49 | struct attrs { |
50 | u_int32_t len; | |
51 | attrreference_t name; | |
52 | dev_t dev; | |
53 | fsobj_type_t type; | |
54 | fsobj_id_t id; | |
55 | char buf[PATH_MAX]; | |
56 | }; | |
57 | ||
58 | #ifndef BUILDING_VARIANT | |
6465356a | 59 | __private_extern__ const struct attrlist _rp_alist = { |
ad3c9f2a A |
60 | ATTR_BIT_MAP_COUNT, |
61 | 0, | |
62 | ATTR_CMN_NAME | ATTR_CMN_DEVID | ATTR_CMN_OBJTYPE | ATTR_CMN_OBJID, | |
63 | 0, | |
64 | 0, | |
65 | 0, | |
66 | 0, | |
67 | }; | |
68 | #else /* BUILDING_VARIANT */ | |
b061a43b | 69 | extern const struct attrlist _rp_alist; |
ad3c9f2a A |
70 | #endif /* BUILDING_VARIANT */ |
71 | ||
72 | extern char * __private_getcwd(char *, size_t, int); | |
73 | ||
9385eb3d A |
74 | /* |
75 | * char *realpath(const char *path, char resolved[PATH_MAX]); | |
76 | * | |
77 | * Find the real name of path, by removing all ".", ".." and symlink | |
78 | * components. Returns (resolved) on success, or (NULL) on failure, | |
79 | * in which case the path which caused trouble is left in (resolved). | |
80 | */ | |
81 | char * | |
ad3c9f2a | 82 | realpath(const char *path, char inresolved[PATH_MAX]) |
9385eb3d | 83 | { |
ad3c9f2a | 84 | struct attrs attrs; |
9385eb3d | 85 | struct stat sb; |
b061a43b A |
86 | char *p, *q; |
87 | size_t left_len, resolved_len, save_resolved_len, next_token_len; | |
9385eb3d | 88 | unsigned symlinks; |
b061a43b A |
89 | int serrno, useattrs, islink; |
90 | ssize_t slen; | |
9385eb3d | 91 | char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX]; |
ad3c9f2a A |
92 | dev_t dev, lastdev; |
93 | struct statfs sfs; | |
94 | static dev_t rootdev; | |
95 | static int rootdev_inited = 0; | |
96 | ino_t inode; | |
97 | char *resolved; | |
9385eb3d | 98 | |
ad3c9f2a A |
99 | if (path == NULL) { |
100 | errno = EINVAL; | |
101 | return (NULL); | |
102 | } | |
103 | #if __DARWIN_UNIX03 | |
104 | if (*path == 0) { | |
105 | errno = ENOENT; | |
106 | return (NULL); | |
107 | } | |
108 | #endif /* __DARWIN_UNIX03 */ | |
507116e3 | 109 | |
ad3c9f2a A |
110 | /* |
111 | * Extension to the standard; if inresolved == NULL, allocate memory | |
112 | */ | |
113 | if (!inresolved) { | |
114 | if ((resolved = malloc(PATH_MAX)) == NULL) return (NULL); | |
115 | } else { | |
116 | resolved = inresolved; | |
117 | } | |
118 | if (!rootdev_inited) { | |
119 | rootdev_inited = 1; | |
120 | if (stat("/", &sb) < 0) { | |
121 | error_return: | |
122 | if (!inresolved) { | |
123 | int e = errno; | |
124 | free(resolved); | |
125 | errno = e; | |
126 | } | |
127 | return (NULL); | |
128 | } | |
129 | rootdev = sb.st_dev; | |
130 | } | |
9385eb3d A |
131 | serrno = errno; |
132 | symlinks = 0; | |
133 | if (path[0] == '/') { | |
134 | resolved[0] = '/'; | |
135 | resolved[1] = '\0'; | |
ad3c9f2a | 136 | if (path[1] == '\0') { |
9385eb3d | 137 | return (resolved); |
ad3c9f2a | 138 | } |
9385eb3d A |
139 | resolved_len = 1; |
140 | left_len = strlcpy(left, path + 1, sizeof(left)); | |
141 | } else { | |
ad3c9f2a A |
142 | #if !defined(VARIANT_DARWINEXTSN) && __DARWIN_UNIX03 |
143 | /* 4447159: don't use GETPATH, so this will fail if */ | |
144 | /* if parent directories are not readable, as per POSIX */ | |
145 | if (__private_getcwd(resolved, PATH_MAX, 0) == NULL) | |
146 | #else /* VARIANT_DARWINEXTSN || !__DARWIN_UNIX03 */ | |
147 | if (__private_getcwd(resolved, PATH_MAX, 1) == NULL) | |
148 | #endif /* !VARIANT_DARWINEXTSN && __DARWIN_UNIX03 */ | |
149 | { | |
9385eb3d | 150 | strlcpy(resolved, ".", PATH_MAX); |
ad3c9f2a | 151 | goto error_return; |
9385eb3d A |
152 | } |
153 | resolved_len = strlen(resolved); | |
154 | left_len = strlcpy(left, path, sizeof(left)); | |
155 | } | |
156 | if (left_len >= sizeof(left) || resolved_len >= PATH_MAX) { | |
157 | errno = ENAMETOOLONG; | |
ad3c9f2a | 158 | goto error_return; |
9385eb3d | 159 | } |
ad3c9f2a A |
160 | if (resolved_len > 1) { |
161 | if (stat(resolved, &sb) < 0) { | |
162 | goto error_return; | |
163 | } | |
164 | lastdev = sb.st_dev; | |
165 | } else | |
166 | lastdev = rootdev; | |
9385eb3d A |
167 | |
168 | /* | |
169 | * Iterate over path components in `left'. | |
170 | */ | |
171 | while (left_len != 0) { | |
172 | /* | |
173 | * Extract the next path component and adjust `left' | |
174 | * and its length. | |
175 | */ | |
176 | p = strchr(left, '/'); | |
b061a43b A |
177 | next_token_len = p ? p - left : left_len; |
178 | memcpy(next_token, left, next_token_len); | |
179 | next_token[next_token_len] = '\0'; | |
180 | ||
181 | if (p != NULL) { | |
182 | left_len -= next_token_len + 1; | |
183 | memmove(left, p + 1, left_len + 1); | |
184 | } else { | |
185 | left[0] = '\0'; | |
186 | left_len = 0; | |
9385eb3d | 187 | } |
b061a43b | 188 | |
9385eb3d A |
189 | if (resolved[resolved_len - 1] != '/') { |
190 | if (resolved_len + 1 >= PATH_MAX) { | |
191 | errno = ENAMETOOLONG; | |
ad3c9f2a | 192 | goto error_return; |
9385eb3d A |
193 | } |
194 | resolved[resolved_len++] = '/'; | |
195 | resolved[resolved_len] = '\0'; | |
196 | } | |
b061a43b A |
197 | if (next_token[0] == '\0') { |
198 | /* Handle consequential slashes. */ | |
9385eb3d | 199 | continue; |
b061a43b | 200 | } else if (strcmp(next_token, ".") == 0) { |
9385eb3d | 201 | continue; |
b061a43b | 202 | } else if (strcmp(next_token, "..") == 0) { |
9385eb3d A |
203 | /* |
204 | * Strip the last path component except when we have | |
205 | * single "/" | |
206 | */ | |
207 | if (resolved_len > 1) { | |
208 | resolved[resolved_len - 1] = '\0'; | |
209 | q = strrchr(resolved, '/') + 1; | |
210 | *q = '\0'; | |
211 | resolved_len = q - resolved; | |
212 | } | |
213 | continue; | |
214 | } | |
215 | ||
ad3c9f2a A |
216 | /* |
217 | * Save resolved_len, so that we can later null out | |
218 | * the the appended next_token, and replace with the | |
219 | * real name (matters on case-insensitive filesystems). | |
220 | */ | |
221 | save_resolved_len = resolved_len; | |
222 | ||
9385eb3d A |
223 | /* |
224 | * Append the next path component and lstat() it. If | |
225 | * lstat() fails we still can return successfully if | |
226 | * there are no more path components left. | |
227 | */ | |
228 | resolved_len = strlcat(resolved, next_token, PATH_MAX); | |
229 | if (resolved_len >= PATH_MAX) { | |
230 | errno = ENAMETOOLONG; | |
ad3c9f2a | 231 | goto error_return; |
9385eb3d | 232 | } |
6465356a | 233 | if (getattrlist(resolved, (void *)&_rp_alist, &attrs, sizeof(attrs), FSOPT_NOFOLLOW) == 0) { |
ad3c9f2a A |
234 | useattrs = 1; |
235 | islink = (attrs.type == VLNK); | |
236 | dev = attrs.dev; | |
237 | inode = attrs.id.fid_objno; | |
238 | } else if (errno == ENOTSUP || errno == EINVAL) { | |
239 | if ((useattrs = lstat(resolved, &sb)) == 0) { | |
240 | islink = S_ISLNK(sb.st_mode); | |
241 | dev = sb.st_dev; | |
242 | inode = sb.st_ino; | |
243 | } | |
244 | } else | |
245 | useattrs = -1; | |
246 | if (useattrs < 0) { | |
247 | #if !__DARWIN_UNIX03 | |
9385eb3d A |
248 | if (errno == ENOENT && p == NULL) { |
249 | errno = serrno; | |
250 | return (resolved); | |
251 | } | |
ad3c9f2a A |
252 | #endif /* !__DARWIN_UNIX03 */ |
253 | goto error_return; | |
9385eb3d | 254 | } |
ad3c9f2a A |
255 | if (dev != lastdev) { |
256 | /* | |
257 | * We have crossed a mountpoint. For volumes like UDF | |
258 | * the getattrlist name may not match the actual | |
259 | * mountpoint, so we just copy the mountpoint directly. | |
260 | * (3703138). However, the mountpoint may not be | |
261 | * accessible, as when chroot-ed, so check first. | |
262 | * There may be a file on the chroot-ed volume with | |
263 | * the same name as the mountpoint, so compare device | |
264 | * and inode numbers. | |
265 | */ | |
266 | lastdev = dev; | |
267 | if (statfs(resolved, &sfs) == 0 && lstat(sfs.f_mntonname, &sb) == 0 && dev == sb.st_dev && inode == sb.st_ino) { | |
268 | /* | |
269 | * However, it's possible that the mountpoint | |
270 | * path matches, even though it isn't the real | |
271 | * path in the chroot-ed environment, so check | |
272 | * that each component of the mountpoint | |
273 | * is a directory (and not a symlink) | |
274 | */ | |
275 | char temp[sizeof(sfs.f_mntonname)]; | |
276 | char *cp; | |
277 | int ok = 1; | |
278 | ||
279 | strcpy(temp, sfs.f_mntonname); | |
280 | for(;;) { | |
281 | if ((cp = strrchr(temp, '/')) == NULL) { | |
282 | ok = 0; | |
283 | break; | |
284 | } | |
285 | if (cp <= temp) | |
286 | break; | |
287 | *cp = 0; | |
288 | if (lstat(temp, &sb) < 0 || (sb.st_mode & S_IFMT) != S_IFDIR) { | |
289 | ok = 0; | |
290 | break; | |
291 | } | |
292 | } | |
293 | if (ok) { | |
294 | resolved_len = strlcpy(resolved, sfs.f_mntonname, PATH_MAX); | |
295 | continue; | |
296 | } | |
297 | } | |
298 | /* if we fail, use the other methods. */ | |
299 | } | |
300 | if (islink) { | |
9385eb3d A |
301 | if (symlinks++ > MAXSYMLINKS) { |
302 | errno = ELOOP; | |
ad3c9f2a | 303 | goto error_return; |
9385eb3d | 304 | } |
b061a43b A |
305 | slen = readlink(resolved, symlink, sizeof(symlink)); |
306 | if (slen <= 0 || slen >= sizeof(symlink)) { | |
307 | if (slen < 0) | |
308 | ; /* keep errno from readlink(2) call */ | |
309 | else if (slen == 0) | |
310 | errno = ENOENT; | |
311 | else | |
312 | errno = ENAMETOOLONG; | |
ad3c9f2a A |
313 | goto error_return; |
314 | } | |
9385eb3d A |
315 | symlink[slen] = '\0'; |
316 | if (symlink[0] == '/') { | |
317 | resolved[1] = 0; | |
318 | resolved_len = 1; | |
ad3c9f2a | 319 | lastdev = rootdev; |
b061a43b | 320 | } else { |
9385eb3d | 321 | /* Strip the last path component. */ |
9385eb3d A |
322 | q = strrchr(resolved, '/') + 1; |
323 | *q = '\0'; | |
324 | resolved_len = q - resolved; | |
325 | } | |
326 | ||
327 | /* | |
328 | * If there are any path components left, then | |
329 | * append them to symlink. The result is placed | |
330 | * in `left'. | |
331 | */ | |
332 | if (p != NULL) { | |
333 | if (symlink[slen - 1] != '/') { | |
334 | if (slen + 1 >= sizeof(symlink)) { | |
335 | errno = ENAMETOOLONG; | |
ad3c9f2a | 336 | goto error_return; |
9385eb3d A |
337 | } |
338 | symlink[slen] = '/'; | |
339 | symlink[slen + 1] = 0; | |
340 | } | |
6465356a | 341 | left_len = strlcat(symlink, left, sizeof(symlink)); |
b061a43b | 342 | if (left_len >= sizeof(symlink)) { |
9385eb3d | 343 | errno = ENAMETOOLONG; |
ad3c9f2a | 344 | goto error_return; |
9385eb3d A |
345 | } |
346 | } | |
347 | left_len = strlcpy(left, symlink, sizeof(left)); | |
ad3c9f2a A |
348 | } else if (useattrs) { |
349 | /* | |
350 | * attrs already has the real name. | |
351 | */ | |
352 | ||
353 | resolved[save_resolved_len] = '\0'; | |
354 | resolved_len = strlcat(resolved, (const char *)&attrs.name + attrs.name.attr_dataoffset, PATH_MAX); | |
355 | if (resolved_len >= PATH_MAX) { | |
356 | errno = ENAMETOOLONG; | |
357 | goto error_return; | |
358 | } | |
9385eb3d | 359 | } |
ad3c9f2a A |
360 | /* |
361 | * For the case of useattrs == 0, we could scan the directory | |
362 | * and try to match the inode. There are many problems with | |
363 | * this: (1) the directory may not be readable, (2) for multiple | |
364 | * hard links, we would find the first, but not necessarily | |
365 | * the one specified in the path, (3) we can't try to do | |
366 | * a case-insensitive search to match the right one in (2), | |
367 | * because the underlying filesystem may do things like | |
368 | * decompose composed characters. For most cases, doing | |
369 | * nothing is the right thing when useattrs == 0, so we punt | |
370 | * for now. | |
371 | */ | |
9385eb3d A |
372 | } |
373 | ||
374 | /* | |
375 | * Remove trailing slash except when the resolved pathname | |
376 | * is a single "/". | |
377 | */ | |
378 | if (resolved_len > 1 && resolved[resolved_len - 1] == '/') | |
379 | resolved[resolved_len - 1] = '\0'; | |
380 | return (resolved); | |
381 | } |