Damien Miller | e929a43 | 2015-10-14 08:25:55 +1100 | [diff] [blame^] | 1 | /* $OpenBSD: realpath.c,v 1.16 2013/04/05 12:59:54 kurt 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 | |
Damien Miller | e929a43 | 2015-10-14 08:25:55 +1100 | [diff] [blame^] | 46 | /* A slightly modified copy of this file exists in libexec/ld.so */ |
| 47 | |
Ben Lindstrom | b4df15d | 2000-10-15 00:17:36 +0000 | [diff] [blame] | 48 | /* |
Darren Tucker | 73f671a | 2005-08-10 21:52:36 +1000 | [diff] [blame] | 49 | * char *realpath(const char *path, char resolved[PATH_MAX]); |
Ben Lindstrom | b4df15d | 2000-10-15 00:17:36 +0000 | [diff] [blame] | 50 | * |
| 51 | * Find the real name of path, by removing all ".", ".." and symlink |
| 52 | * components. Returns (resolved) on success, or (NULL) on failure, |
| 53 | * in which case the path which caused trouble is left in (resolved). |
| 54 | */ |
| 55 | char * |
Damien Miller | 7365fe5 | 2015-10-14 08:25:09 +1100 | [diff] [blame] | 56 | realpath(const char *path, char *resolved) |
Ben Lindstrom | b4df15d | 2000-10-15 00:17:36 +0000 | [diff] [blame] | 57 | { |
| 58 | struct stat sb; |
Darren Tucker | 73f671a | 2005-08-10 21:52:36 +1000 | [diff] [blame] | 59 | char *p, *q, *s; |
| 60 | size_t left_len, resolved_len; |
| 61 | unsigned symlinks; |
Damien Miller | 7365fe5 | 2015-10-14 08:25:09 +1100 | [diff] [blame] | 62 | int serrno, slen, mem_allocated; |
Darren Tucker | 73f671a | 2005-08-10 21:52:36 +1000 | [diff] [blame] | 63 | char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX]; |
Ben Lindstrom | b4df15d | 2000-10-15 00:17:36 +0000 | [diff] [blame] | 64 | |
Damien Miller | 7365fe5 | 2015-10-14 08:25:09 +1100 | [diff] [blame] | 65 | if (path[0] == '\0') { |
| 66 | errno = ENOENT; |
| 67 | return (NULL); |
| 68 | } |
| 69 | |
Darren Tucker | 73f671a | 2005-08-10 21:52:36 +1000 | [diff] [blame] | 70 | serrno = errno; |
Damien Miller | 7365fe5 | 2015-10-14 08:25:09 +1100 | [diff] [blame] | 71 | |
| 72 | if (resolved == NULL) { |
| 73 | resolved = malloc(PATH_MAX); |
| 74 | if (resolved == NULL) |
| 75 | return (NULL); |
| 76 | mem_allocated = 1; |
| 77 | } else |
| 78 | mem_allocated = 0; |
| 79 | |
Darren Tucker | 73f671a | 2005-08-10 21:52:36 +1000 | [diff] [blame] | 80 | symlinks = 0; |
| 81 | if (path[0] == '/') { |
| 82 | resolved[0] = '/'; |
Darren Tucker | ad7646a | 2005-02-02 10:43:59 +1100 | [diff] [blame] | 83 | resolved[1] = '\0'; |
Darren Tucker | 73f671a | 2005-08-10 21:52:36 +1000 | [diff] [blame] | 84 | if (path[1] == '\0') |
| 85 | return (resolved); |
| 86 | resolved_len = 1; |
| 87 | left_len = strlcpy(left, path + 1, sizeof(left)); |
| 88 | } else { |
| 89 | if (getcwd(resolved, PATH_MAX) == NULL) { |
Damien Miller | 7365fe5 | 2015-10-14 08:25:09 +1100 | [diff] [blame] | 90 | if (mem_allocated) |
| 91 | free(resolved); |
| 92 | else |
| 93 | strlcpy(resolved, ".", PATH_MAX); |
Darren Tucker | 73f671a | 2005-08-10 21:52:36 +1000 | [diff] [blame] | 94 | return (NULL); |
Ben Lindstrom | b4df15d | 2000-10-15 00:17:36 +0000 | [diff] [blame] | 95 | } |
Darren Tucker | 73f671a | 2005-08-10 21:52:36 +1000 | [diff] [blame] | 96 | resolved_len = strlen(resolved); |
| 97 | left_len = strlcpy(left, path, sizeof(left)); |
Ben Lindstrom | b4df15d | 2000-10-15 00:17:36 +0000 | [diff] [blame] | 98 | } |
Darren Tucker | 73f671a | 2005-08-10 21:52:36 +1000 | [diff] [blame] | 99 | if (left_len >= sizeof(left) || resolved_len >= PATH_MAX) { |
Darren Tucker | ad7646a | 2005-02-02 10:43:59 +1100 | [diff] [blame] | 100 | errno = ENAMETOOLONG; |
Damien Miller | 7365fe5 | 2015-10-14 08:25:09 +1100 | [diff] [blame] | 101 | goto err; |
Darren Tucker | ad7646a | 2005-02-02 10:43:59 +1100 | [diff] [blame] | 102 | } |
Ben Lindstrom | b4df15d | 2000-10-15 00:17:36 +0000 | [diff] [blame] | 103 | |
| 104 | /* |
Darren Tucker | 73f671a | 2005-08-10 21:52:36 +1000 | [diff] [blame] | 105 | * Iterate over path components in `left'. |
Ben Lindstrom | b4df15d | 2000-10-15 00:17:36 +0000 | [diff] [blame] | 106 | */ |
Darren Tucker | 73f671a | 2005-08-10 21:52:36 +1000 | [diff] [blame] | 107 | while (left_len != 0) { |
| 108 | /* |
| 109 | * Extract the next path component and adjust `left' |
| 110 | * and its length. |
| 111 | */ |
| 112 | p = strchr(left, '/'); |
| 113 | s = p ? p : left + left_len; |
Damien Miller | d56fd18 | 2015-07-20 11:19:51 +1000 | [diff] [blame] | 114 | if (s - left >= (ptrdiff_t)sizeof(next_token)) { |
Darren Tucker | ad7646a | 2005-02-02 10:43:59 +1100 | [diff] [blame] | 115 | errno = ENAMETOOLONG; |
Damien Miller | 7365fe5 | 2015-10-14 08:25:09 +1100 | [diff] [blame] | 116 | goto err; |
Ben Lindstrom | b4df15d | 2000-10-15 00:17:36 +0000 | [diff] [blame] | 117 | } |
Darren Tucker | 73f671a | 2005-08-10 21:52:36 +1000 | [diff] [blame] | 118 | memcpy(next_token, left, s - left); |
| 119 | next_token[s - left] = '\0'; |
| 120 | left_len -= s - left; |
| 121 | if (p != NULL) |
| 122 | memmove(left, s + 1, left_len + 1); |
| 123 | if (resolved[resolved_len - 1] != '/') { |
| 124 | if (resolved_len + 1 >= PATH_MAX) { |
Darren Tucker | ad7646a | 2005-02-02 10:43:59 +1100 | [diff] [blame] | 125 | errno = ENAMETOOLONG; |
Damien Miller | 7365fe5 | 2015-10-14 08:25:09 +1100 | [diff] [blame] | 126 | goto err; |
Darren Tucker | ad7646a | 2005-02-02 10:43:59 +1100 | [diff] [blame] | 127 | } |
Darren Tucker | 73f671a | 2005-08-10 21:52:36 +1000 | [diff] [blame] | 128 | resolved[resolved_len++] = '/'; |
| 129 | resolved[resolved_len] = '\0'; |
Darren Tucker | ad7646a | 2005-02-02 10:43:59 +1100 | [diff] [blame] | 130 | } |
Darren Tucker | 73f671a | 2005-08-10 21:52:36 +1000 | [diff] [blame] | 131 | if (next_token[0] == '\0') |
| 132 | continue; |
| 133 | else if (strcmp(next_token, ".") == 0) |
| 134 | continue; |
| 135 | else if (strcmp(next_token, "..") == 0) { |
| 136 | /* |
| 137 | * Strip the last path component except when we have |
| 138 | * single "/" |
| 139 | */ |
| 140 | if (resolved_len > 1) { |
| 141 | resolved[resolved_len - 1] = '\0'; |
| 142 | q = strrchr(resolved, '/') + 1; |
| 143 | *q = '\0'; |
| 144 | resolved_len = q - resolved; |
| 145 | } |
| 146 | continue; |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Append the next path component and lstat() it. If |
| 151 | * lstat() fails we still can return successfully if |
| 152 | * there are no more path components left. |
| 153 | */ |
| 154 | resolved_len = strlcat(resolved, next_token, PATH_MAX); |
| 155 | if (resolved_len >= PATH_MAX) { |
Darren Tucker | ad7646a | 2005-02-02 10:43:59 +1100 | [diff] [blame] | 156 | errno = ENAMETOOLONG; |
Damien Miller | 7365fe5 | 2015-10-14 08:25:09 +1100 | [diff] [blame] | 157 | goto err; |
Darren Tucker | 73f671a | 2005-08-10 21:52:36 +1000 | [diff] [blame] | 158 | } |
| 159 | if (lstat(resolved, &sb) != 0) { |
| 160 | if (errno == ENOENT && p == NULL) { |
| 161 | errno = serrno; |
| 162 | return (resolved); |
| 163 | } |
Damien Miller | 7365fe5 | 2015-10-14 08:25:09 +1100 | [diff] [blame] | 164 | goto err; |
Darren Tucker | 73f671a | 2005-08-10 21:52:36 +1000 | [diff] [blame] | 165 | } |
| 166 | if (S_ISLNK(sb.st_mode)) { |
| 167 | if (symlinks++ > MAXSYMLINKS) { |
| 168 | errno = ELOOP; |
Damien Miller | 7365fe5 | 2015-10-14 08:25:09 +1100 | [diff] [blame] | 169 | goto err; |
Darren Tucker | 73f671a | 2005-08-10 21:52:36 +1000 | [diff] [blame] | 170 | } |
| 171 | slen = readlink(resolved, symlink, sizeof(symlink) - 1); |
| 172 | if (slen < 0) |
Damien Miller | 7365fe5 | 2015-10-14 08:25:09 +1100 | [diff] [blame] | 173 | goto err; |
Darren Tucker | 73f671a | 2005-08-10 21:52:36 +1000 | [diff] [blame] | 174 | symlink[slen] = '\0'; |
| 175 | if (symlink[0] == '/') { |
| 176 | resolved[1] = 0; |
| 177 | resolved_len = 1; |
| 178 | } else if (resolved_len > 1) { |
| 179 | /* Strip the last path component. */ |
| 180 | resolved[resolved_len - 1] = '\0'; |
| 181 | q = strrchr(resolved, '/') + 1; |
| 182 | *q = '\0'; |
| 183 | resolved_len = q - resolved; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * If there are any path components left, then |
| 188 | * append them to symlink. The result is placed |
| 189 | * in `left'. |
| 190 | */ |
| 191 | if (p != NULL) { |
| 192 | if (symlink[slen - 1] != '/') { |
Damien Miller | d56fd18 | 2015-07-20 11:19:51 +1000 | [diff] [blame] | 193 | if (slen + 1 >= |
| 194 | (ptrdiff_t)sizeof(symlink)) { |
Darren Tucker | 73f671a | 2005-08-10 21:52:36 +1000 | [diff] [blame] | 195 | errno = ENAMETOOLONG; |
Damien Miller | 7365fe5 | 2015-10-14 08:25:09 +1100 | [diff] [blame] | 196 | goto err; |
Darren Tucker | 73f671a | 2005-08-10 21:52:36 +1000 | [diff] [blame] | 197 | } |
| 198 | symlink[slen] = '/'; |
| 199 | symlink[slen + 1] = 0; |
| 200 | } |
Damien Miller | 5225db6 | 2015-10-14 08:25:32 +1100 | [diff] [blame] | 201 | left_len = strlcat(symlink, left, sizeof(symlink)); |
Darren Tucker | 73f671a | 2005-08-10 21:52:36 +1000 | [diff] [blame] | 202 | if (left_len >= sizeof(left)) { |
| 203 | errno = ENAMETOOLONG; |
Damien Miller | 7365fe5 | 2015-10-14 08:25:09 +1100 | [diff] [blame] | 204 | goto err; |
Darren Tucker | 73f671a | 2005-08-10 21:52:36 +1000 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | left_len = strlcpy(left, symlink, sizeof(left)); |
Darren Tucker | ad7646a | 2005-02-02 10:43:59 +1100 | [diff] [blame] | 208 | } |
Ben Lindstrom | b4df15d | 2000-10-15 00:17:36 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Darren Tucker | 73f671a | 2005-08-10 21:52:36 +1000 | [diff] [blame] | 211 | /* |
| 212 | * Remove trailing slash except when the resolved pathname |
| 213 | * is a single "/". |
| 214 | */ |
| 215 | if (resolved_len > 1 && resolved[resolved_len - 1] == '/') |
| 216 | resolved[resolved_len - 1] = '\0'; |
Ben Lindstrom | b4df15d | 2000-10-15 00:17:36 +0000 | [diff] [blame] | 217 | return (resolved); |
Damien Miller | 7365fe5 | 2015-10-14 08:25:09 +1100 | [diff] [blame] | 218 | |
| 219 | err: |
| 220 | if (mem_allocated) |
| 221 | free(resolved); |
| 222 | return (NULL); |
Ben Lindstrom | b4df15d | 2000-10-15 00:17:36 +0000 | [diff] [blame] | 223 | } |
| 224 | #endif /* !defined(HAVE_REALPATH) || defined(BROKEN_REALPATH) */ |