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