1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 2003 Constantin S. Svintsoff <[email protected]>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The names of the authors may not be used to endorse or promote
15 * products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #if defined(LIBC_SCCS) && !defined(lint)
32 static char sccsid[] = "@(#)realpath.c 8.1 (Berkeley) 2/16/94";
33 #endif /* LIBC_SCCS and not lint */
34 #include "namespace.h"
35 #include <sys/param.h>
36 #include <sys/stat.h>
37
38 #include <errno.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include <fcntl.h>
43 #include "un-namespace.h"
44 #include "libc_private.h"
45
46 extern int __realpathat(int fd, const char *path, char *buf, size_t size,
47 int flags);
48
49 /*
50 * Find the real name of path, by removing all ".", ".." and symlink
51 * components. Returns (resolved) on success, or (NULL) on failure,
52 * in which case the path which caused trouble is left in (resolved).
53 */
54 static char * __noinline
realpath1(const char * path,char * resolved)55 realpath1(const char *path, char *resolved)
56 {
57 struct stat sb;
58 char *p, *q;
59 size_t left_len, resolved_len, next_token_len;
60 unsigned symlinks;
61 ssize_t slen;
62 char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX];
63
64 symlinks = 0;
65 if (path[0] == '/') {
66 resolved[0] = '/';
67 resolved[1] = '\0';
68 if (path[1] == '\0')
69 return (resolved);
70 resolved_len = 1;
71 left_len = strlcpy(left, path + 1, sizeof(left));
72 } else {
73 if (getcwd(resolved, PATH_MAX) == NULL) {
74 resolved[0] = '.';
75 resolved[1] = '\0';
76 return (NULL);
77 }
78 resolved_len = strlen(resolved);
79 left_len = strlcpy(left, path, sizeof(left));
80 }
81 if (left_len >= sizeof(left) || resolved_len >= PATH_MAX) {
82 errno = ENAMETOOLONG;
83 return (NULL);
84 }
85
86 /*
87 * Iterate over path components in `left'.
88 */
89 while (left_len != 0) {
90 /*
91 * Extract the next path component and adjust `left'
92 * and its length.
93 */
94 p = strchr(left, '/');
95
96 next_token_len = p != NULL ? (size_t)(p - left) : left_len;
97 memcpy(next_token, left, next_token_len);
98 next_token[next_token_len] = '\0';
99
100 if (p != NULL) {
101 left_len -= next_token_len + 1;
102 memmove(left, p + 1, left_len + 1);
103 } else {
104 left[0] = '\0';
105 left_len = 0;
106 }
107
108 if (resolved[resolved_len - 1] != '/') {
109 if (resolved_len + 1 >= PATH_MAX) {
110 errno = ENAMETOOLONG;
111 return (NULL);
112 }
113 resolved[resolved_len++] = '/';
114 resolved[resolved_len] = '\0';
115 }
116 if (next_token[0] == '\0') {
117 /* Handle consequential slashes. */
118 continue;
119 } else if (strcmp(next_token, ".") == 0) {
120 continue;
121 } else if (strcmp(next_token, "..") == 0) {
122 /*
123 * Strip the last path component except when we have
124 * single "/"
125 */
126 if (resolved_len > 1) {
127 resolved[resolved_len - 1] = '\0';
128 q = strrchr(resolved, '/') + 1;
129 *q = '\0';
130 resolved_len = q - resolved;
131 }
132 continue;
133 }
134
135 /*
136 * Append the next path component and lstat() it.
137 */
138 resolved_len = strlcat(resolved, next_token, PATH_MAX);
139 if (resolved_len >= PATH_MAX) {
140 errno = ENAMETOOLONG;
141 return (NULL);
142 }
143 if (lstat(resolved, &sb) != 0)
144 return (NULL);
145 if (S_ISLNK(sb.st_mode)) {
146 if (symlinks++ > MAXSYMLINKS) {
147 errno = ELOOP;
148 return (NULL);
149 }
150 slen = readlink(resolved, symlink, sizeof(symlink));
151 if (slen <= 0 || slen >= (ssize_t)sizeof(symlink)) {
152 if (slen < 0)
153 ; /* keep errno from readlink(2) call */
154 else if (slen == 0)
155 errno = ENOENT;
156 else
157 errno = ENAMETOOLONG;
158 return (NULL);
159 }
160 symlink[slen] = '\0';
161 if (symlink[0] == '/') {
162 resolved[1] = 0;
163 resolved_len = 1;
164 } else {
165 /* Strip the last path component. */
166 q = strrchr(resolved, '/') + 1;
167 *q = '\0';
168 resolved_len = q - resolved;
169 }
170
171 /*
172 * If there are any path components left, then
173 * append them to symlink. The result is placed
174 * in `left'.
175 */
176 if (p != NULL) {
177 if (symlink[slen - 1] != '/') {
178 if (slen + 1 >= (ssize_t)sizeof(symlink)) {
179 errno = ENAMETOOLONG;
180 return (NULL);
181 }
182 symlink[slen] = '/';
183 symlink[slen + 1] = 0;
184 }
185 left_len = strlcat(symlink, left,
186 sizeof(symlink));
187 if (left_len >= sizeof(symlink)) {
188 errno = ENAMETOOLONG;
189 return (NULL);
190 }
191 }
192 left_len = strlcpy(left, symlink, sizeof(left));
193 } else if (!S_ISDIR(sb.st_mode) && p != NULL) {
194 errno = ENOTDIR;
195 return (NULL);
196 }
197 }
198
199 /*
200 * Remove trailing slash except when the resolved pathname
201 * is a single "/".
202 */
203 if (resolved_len > 1 && resolved[resolved_len - 1] == '/')
204 resolved[resolved_len - 1] = '\0';
205 return (resolved);
206 }
207
208 char *
realpath(const char * __restrict path,char * __restrict resolved)209 realpath(const char * __restrict path, char * __restrict resolved)
210 {
211 char *m, *res;
212
213 if (path == NULL) {
214 errno = EINVAL;
215 return (NULL);
216 }
217 if (path[0] == '\0') {
218 errno = ENOENT;
219 return (NULL);
220 }
221 if (resolved != NULL) {
222 m = NULL;
223 } else {
224 m = resolved = malloc(PATH_MAX);
225 if (resolved == NULL)
226 return (NULL);
227 }
228 if (__getosreldate() >= 1300080) {
229 if (__realpathat(AT_FDCWD, path, resolved, PATH_MAX, 0) == 0)
230 return (resolved);
231 }
232 res = realpath1(path, resolved);
233 if (res == NULL)
234 free(m);
235 return (res);
236 }
237