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