]>
Commit | Line | Data |
---|---|---|
224c7076 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> | |
38 | #include <sys/mount.h> | |
39 | ||
40 | #include <errno.h> | |
41 | #include <stdlib.h> | |
42 | #include <string.h> | |
43 | #include <unistd.h> | |
44 | #include <sys/attr.h> | |
45 | #include <sys/vnode.h> | |
46 | #include "un-namespace.h" | |
47 | ||
48 | struct attrs { | |
49 | u_int32_t len; | |
50 | attrreference_t name; | |
51 | dev_t dev; | |
52 | fsobj_type_t type; | |
53 | fsobj_id_t id; | |
54 | char buf[PATH_MAX]; | |
55 | }; | |
56 | ||
57 | #ifndef BUILDING_VARIANT | |
58 | __private_extern__ struct attrlist _rp_alist = { | |
59 | ATTR_BIT_MAP_COUNT, | |
60 | 0, | |
61 | ATTR_CMN_NAME | ATTR_CMN_DEVID | ATTR_CMN_OBJTYPE | ATTR_CMN_OBJID, | |
62 | 0, | |
63 | 0, | |
64 | 0, | |
65 | 0, | |
66 | }; | |
67 | #else /* BUILDING_VARIANT */ | |
68 | __private_extern__ struct attrlist _rp_alist; | |
69 | #endif /* BUILDING_VARIANT */ | |
70 | ||
71 | extern char * __private_getcwd(char *, size_t, int); | |
72 | ||
73 | /* | |
74 | * char *realpath(const char *path, char resolved[PATH_MAX]); | |
75 | * | |
76 | * Find the real name of path, by removing all ".", ".." and symlink | |
77 | * components. Returns (resolved) on success, or (NULL) on failure, | |
78 | * in which case the path which caused trouble is left in (resolved). | |
79 | */ | |
80 | char * | |
34e8f829 | 81 | realpath(const char *path, char inresolved[PATH_MAX]) |
224c7076 A |
82 | { |
83 | struct attrs attrs; | |
84 | struct stat sb; | |
85 | char *p, *q, *s; | |
86 | size_t left_len, resolved_len, save_resolved_len; | |
87 | unsigned symlinks; | |
88 | int serrno, slen, useattrs, islink; | |
89 | char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX]; | |
90 | dev_t dev, lastdev; | |
91 | struct statfs sfs; | |
92 | static dev_t rootdev; | |
93 | static int rootdev_inited = 0; | |
94 | ino_t inode; | |
34e8f829 | 95 | char *resolved; |
224c7076 A |
96 | |
97 | if (path == NULL) { | |
98 | errno = EINVAL; | |
99 | return (NULL); | |
100 | } | |
101 | #if __DARWIN_UNIX03 | |
102 | if (*path == 0) { | |
103 | errno = ENOENT; | |
104 | return (NULL); | |
105 | } | |
106 | #endif /* __DARWIN_UNIX03 */ | |
34e8f829 A |
107 | /* |
108 | * Extension to the standard; if inresolved == NULL, allocate memory | |
109 | * (first on the stack, then use strdup()) | |
110 | */ | |
111 | if (!inresolved) { | |
112 | if ((resolved = alloca(PATH_MAX)) == NULL) return (NULL); | |
113 | } else { | |
114 | resolved = inresolved; | |
115 | } | |
224c7076 A |
116 | if (!rootdev_inited) { |
117 | rootdev_inited = 1; | |
118 | if (stat("/", &sb) < 0) { | |
119 | return (NULL); | |
120 | } | |
121 | rootdev = sb.st_dev; | |
122 | } | |
123 | serrno = errno; | |
124 | symlinks = 0; | |
125 | if (path[0] == '/') { | |
126 | resolved[0] = '/'; | |
127 | resolved[1] = '\0'; | |
128 | if (path[1] == '\0') { | |
129 | return (resolved); | |
130 | } | |
131 | resolved_len = 1; | |
132 | left_len = strlcpy(left, path + 1, sizeof(left)); | |
133 | } else { | |
134 | #if !defined(VARIANT_DARWINEXTSN) && __DARWIN_UNIX03 | |
135 | /* 4447159: don't use GETPATH, so this will fail if */ | |
136 | /* if parent directories are not readable, as per POSIX */ | |
137 | if (__private_getcwd(resolved, PATH_MAX, 0) == NULL) | |
138 | #else /* VARIANT_DARWINEXTSN || !__DARWIN_UNIX03 */ | |
139 | if (__private_getcwd(resolved, PATH_MAX, 1) == NULL) | |
140 | #endif /* !VARIANT_DARWINEXTSN && __DARWIN_UNIX03 */ | |
141 | { | |
142 | strlcpy(resolved, ".", PATH_MAX); | |
143 | return (NULL); | |
144 | } | |
145 | resolved_len = strlen(resolved); | |
146 | left_len = strlcpy(left, path, sizeof(left)); | |
147 | } | |
148 | if (left_len >= sizeof(left) || resolved_len >= PATH_MAX) { | |
149 | errno = ENAMETOOLONG; | |
150 | return (NULL); | |
151 | } | |
152 | if (resolved_len > 1) { | |
153 | if (stat(resolved, &sb) < 0) { | |
154 | return (NULL); | |
155 | } | |
156 | lastdev = sb.st_dev; | |
157 | } else | |
158 | lastdev = rootdev; | |
159 | ||
160 | /* | |
161 | * Iterate over path components in `left'. | |
162 | */ | |
163 | while (left_len != 0) { | |
164 | /* | |
165 | * Extract the next path component and adjust `left' | |
166 | * and its length. | |
167 | */ | |
168 | p = strchr(left, '/'); | |
169 | s = p ? p : left + left_len; | |
170 | if (s - left >= sizeof(next_token)) { | |
171 | errno = ENAMETOOLONG; | |
172 | return (NULL); | |
173 | } | |
174 | memcpy(next_token, left, s - left); | |
175 | next_token[s - left] = '\0'; | |
176 | left_len -= s - left; | |
177 | if (p != NULL) | |
178 | memmove(left, s + 1, left_len + 1); | |
179 | if (resolved[resolved_len - 1] != '/') { | |
180 | if (resolved_len + 1 >= PATH_MAX) { | |
181 | errno = ENAMETOOLONG; | |
182 | return (NULL); | |
183 | } | |
184 | resolved[resolved_len++] = '/'; | |
185 | resolved[resolved_len] = '\0'; | |
186 | } | |
187 | if (next_token[0] == '\0') | |
188 | continue; | |
189 | else if (strcmp(next_token, ".") == 0) | |
190 | continue; | |
191 | else if (strcmp(next_token, "..") == 0) { | |
192 | /* | |
193 | * Strip the last path component except when we have | |
194 | * single "/" | |
195 | */ | |
196 | if (resolved_len > 1) { | |
197 | resolved[resolved_len - 1] = '\0'; | |
198 | q = strrchr(resolved, '/') + 1; | |
199 | *q = '\0'; | |
200 | resolved_len = q - resolved; | |
201 | } | |
202 | continue; | |
203 | } | |
204 | ||
205 | /* | |
206 | * Save resolved_len, so that we can later null out | |
207 | * the the appended next_token, and replace with the | |
208 | * real name (matters on case-insensitive filesystems). | |
209 | */ | |
210 | save_resolved_len = resolved_len; | |
211 | ||
212 | /* | |
213 | * Append the next path component and lstat() it. If | |
214 | * lstat() fails we still can return successfully if | |
215 | * there are no more path components left. | |
216 | */ | |
217 | resolved_len = strlcat(resolved, next_token, PATH_MAX); | |
218 | if (resolved_len >= PATH_MAX) { | |
219 | errno = ENAMETOOLONG; | |
220 | return (NULL); | |
221 | } | |
222 | if (getattrlist(resolved, &_rp_alist, &attrs, sizeof(attrs), FSOPT_NOFOLLOW) == 0) { | |
223 | useattrs = 1; | |
224 | islink = (attrs.type == VLNK); | |
225 | dev = attrs.dev; | |
226 | inode = attrs.id.fid_objno; | |
227 | } else if (errno == ENOTSUP || errno == EINVAL) { | |
228 | if ((useattrs = lstat(resolved, &sb)) == 0) { | |
229 | islink = S_ISLNK(sb.st_mode); | |
230 | dev = sb.st_dev; | |
231 | inode = sb.st_ino; | |
232 | } | |
233 | } else | |
234 | useattrs = -1; | |
235 | if (useattrs < 0) { | |
236 | #if !__DARWIN_UNIX03 | |
237 | if (errno == ENOENT && p == NULL) { | |
238 | errno = serrno; | |
239 | return (resolved); | |
240 | } | |
241 | #endif /* !__DARWIN_UNIX03 */ | |
242 | return (NULL); | |
243 | } | |
244 | if (dev != lastdev) { | |
245 | /* | |
246 | * We have crossed a mountpoint. For volumes like UDF | |
247 | * the getattrlist name may not match the actual | |
248 | * mountpoint, so we just copy the mountpoint directly. | |
249 | * (3703138). However, the mountpoint may not be | |
250 | * accessible, as when chroot-ed, so check first. | |
251 | * There may be a file on the chroot-ed volume with | |
252 | * the same name as the mountpoint, so compare device | |
253 | * and inode numbers. | |
254 | */ | |
255 | lastdev = dev; | |
256 | if (statfs(resolved, &sfs) == 0 && lstat(sfs.f_mntonname, &sb) == 0 && dev == sb.st_dev && inode == sb.st_ino) { | |
257 | /* | |
258 | * However, it's possible that the mountpoint | |
259 | * path matches, even though it isn't the real | |
260 | * path in the chroot-ed environment, so check | |
261 | * that each component of the mountpoint | |
262 | * is a directory (and not a symlink) | |
263 | */ | |
34e8f829 | 264 | char temp[sizeof(sfs.f_mntonname)]; |
224c7076 A |
265 | char *cp; |
266 | int ok = 1; | |
267 | ||
268 | strcpy(temp, sfs.f_mntonname); | |
269 | for(;;) { | |
270 | if ((cp = strrchr(temp, '/')) == NULL) { | |
271 | ok = 0; | |
272 | break; | |
273 | } | |
274 | if (cp <= temp) | |
275 | break; | |
276 | *cp = 0; | |
277 | if (lstat(temp, &sb) < 0 || (sb.st_mode & S_IFMT) != S_IFDIR) { | |
278 | ok = 0; | |
279 | break; | |
280 | } | |
281 | } | |
282 | if (ok) { | |
283 | resolved_len = strlcpy(resolved, sfs.f_mntonname, PATH_MAX); | |
284 | continue; | |
285 | } | |
286 | } | |
287 | /* if we fail, use the other methods. */ | |
288 | } | |
289 | if (islink) { | |
290 | if (symlinks++ > MAXSYMLINKS) { | |
291 | errno = ELOOP; | |
292 | return (NULL); | |
293 | } | |
294 | slen = readlink(resolved, symlink, sizeof(symlink) - 1); | |
295 | if (slen < 0) { | |
296 | return (NULL); | |
297 | } | |
298 | symlink[slen] = '\0'; | |
299 | if (symlink[0] == '/') { | |
300 | resolved[1] = 0; | |
301 | resolved_len = 1; | |
302 | lastdev = rootdev; | |
303 | } else if (resolved_len > 1) { | |
304 | /* Strip the last path component. */ | |
305 | resolved[resolved_len - 1] = '\0'; | |
306 | q = strrchr(resolved, '/') + 1; | |
307 | *q = '\0'; | |
308 | resolved_len = q - resolved; | |
309 | } | |
310 | ||
311 | /* | |
312 | * If there are any path components left, then | |
313 | * append them to symlink. The result is placed | |
314 | * in `left'. | |
315 | */ | |
316 | if (p != NULL) { | |
317 | if (symlink[slen - 1] != '/') { | |
318 | if (slen + 1 >= sizeof(symlink)) { | |
319 | errno = ENAMETOOLONG; | |
320 | return (NULL); | |
321 | } | |
322 | symlink[slen] = '/'; | |
323 | symlink[slen + 1] = 0; | |
324 | } | |
325 | left_len = strlcat(symlink, left, sizeof(left)); | |
326 | if (left_len >= sizeof(left)) { | |
327 | errno = ENAMETOOLONG; | |
328 | return (NULL); | |
329 | } | |
330 | } | |
331 | left_len = strlcpy(left, symlink, sizeof(left)); | |
332 | } else if (useattrs) { | |
333 | /* | |
334 | * attrs already has the real name. | |
335 | */ | |
336 | ||
337 | resolved[save_resolved_len] = '\0'; | |
338 | resolved_len = strlcat(resolved, (const char *)&attrs.name + attrs.name.attr_dataoffset, PATH_MAX); | |
339 | if (resolved_len >= PATH_MAX) { | |
340 | errno = ENAMETOOLONG; | |
341 | return (NULL); | |
342 | } | |
343 | } | |
344 | /* | |
345 | * For the case of useattrs == 0, we could scan the directory | |
346 | * and try to match the inode. There are many problems with | |
347 | * this: (1) the directory may not be readable, (2) for multiple | |
348 | * hard links, we would find the first, but not necessarily | |
349 | * the one specified in the path, (3) we can't try to do | |
350 | * a case-insensitive search to match the right one in (2), | |
351 | * because the underlying filesystem may do things like | |
352 | * decompose composed characters. For most cases, doing | |
353 | * nothing is the right thing when useattrs == 0, so we punt | |
354 | * for now. | |
355 | */ | |
356 | } | |
357 | ||
358 | /* | |
359 | * Remove trailing slash except when the resolved pathname | |
360 | * is a single "/". | |
361 | */ | |
362 | if (resolved_len > 1 && resolved[resolved_len - 1] == '/') | |
363 | resolved[resolved_len - 1] = '\0'; | |
34e8f829 | 364 | if (!inresolved) resolved = strdup(resolved); |
224c7076 A |
365 | return (resolved); |
366 | } |