1 /* $OpenBSD: realpath.c,v 1.20 2015/10/13 20:55:37 millert Exp $ */ 2 /* 3 * Copyright (c) 2003 Constantin S. Svintsoff <kostik@iclub.nsu.ru> 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. 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. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 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 30 #include <sys/stat.h> 31 32 #include <errno.h> 33 #include <stdlib.h> 34 #include <string.h> 35 #include <unistd.h> 36 #include <limits.h> 37 38 /* A slightly modified copy of this file exists in libexec/ld.so */ 39 40 /* 41 * char *realpath(const char *path, char resolved[PATH_MAX]); 42 * 43 * Find the real name of path, by removing all ".", ".." and symlink 44 * components. Returns (resolved) on success, or (NULL) on failure, 45 * in which case the path which caused trouble is left in (resolved). 46 */ 47 char * 48 realpath(const char *path, char *resolved) 49 { 50 struct stat sb; 51 char *p, *q, *s; 52 size_t left_len, resolved_len; 53 unsigned symlinks; 54 int serrno, slen, mem_allocated; 55 char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX]; 56 57 if (path[0] == '\0') { 58 errno = ENOENT; 59 return (NULL); 60 } 61 62 serrno = errno; 63 64 if (resolved == NULL) { 65 resolved = malloc(PATH_MAX); 66 if (resolved == NULL) 67 return (NULL); 68 mem_allocated = 1; 69 } else 70 mem_allocated = 0; 71 72 symlinks = 0; 73 if (path[0] == '/') { 74 resolved[0] = '/'; 75 resolved[1] = '\0'; 76 if (path[1] == '\0') 77 return (resolved); 78 resolved_len = 1; 79 left_len = strlcpy(left, path + 1, sizeof(left)); 80 } else { 81 if (getcwd(resolved, PATH_MAX) == NULL) { 82 if (mem_allocated) 83 free(resolved); 84 else 85 strlcpy(resolved, ".", PATH_MAX); 86 return (NULL); 87 } 88 resolved_len = strlen(resolved); 89 left_len = strlcpy(left, path, sizeof(left)); 90 } 91 if (left_len >= sizeof(left) || resolved_len >= PATH_MAX) { 92 errno = ENAMETOOLONG; 93 goto err; 94 } 95 96 /* 97 * Iterate over path components in `left'. 98 */ 99 while (left_len != 0) { 100 /* 101 * Extract the next path component and adjust `left' 102 * and its length. 103 */ 104 p = strchr(left, '/'); 105 s = p ? p : left + left_len; 106 if (s - left >= sizeof(next_token)) { 107 errno = ENAMETOOLONG; 108 goto err; 109 } 110 memcpy(next_token, left, s - left); 111 next_token[s - left] = '\0'; 112 left_len -= s - left; 113 if (p != NULL) 114 memmove(left, s + 1, left_len + 1); 115 if (resolved[resolved_len - 1] != '/') { 116 if (resolved_len + 1 >= PATH_MAX) { 117 errno = ENAMETOOLONG; 118 goto err; 119 } 120 resolved[resolved_len++] = '/'; 121 resolved[resolved_len] = '\0'; 122 } 123 if (next_token[0] == '\0') 124 continue; 125 else if (strcmp(next_token, ".") == 0) 126 continue; 127 else if (strcmp(next_token, "..") == 0) { 128 /* 129 * Strip the last path component except when we have 130 * single "/" 131 */ 132 if (resolved_len > 1) { 133 resolved[resolved_len - 1] = '\0'; 134 q = strrchr(resolved, '/') + 1; 135 *q = '\0'; 136 resolved_len = q - resolved; 137 } 138 continue; 139 } 140 141 /* 142 * Append the next path component and lstat() it. If 143 * lstat() fails we still can return successfully if 144 * there are no more path components left. 145 */ 146 resolved_len = strlcat(resolved, next_token, PATH_MAX); 147 if (resolved_len >= PATH_MAX) { 148 errno = ENAMETOOLONG; 149 goto err; 150 } 151 if (lstat(resolved, &sb) != 0) { 152 if (errno == ENOENT && p == NULL) { 153 errno = serrno; 154 return (resolved); 155 } 156 goto err; 157 } 158 if (S_ISLNK(sb.st_mode)) { 159 if (symlinks++ > SYMLOOP_MAX) { 160 errno = ELOOP; 161 goto err; 162 } 163 slen = readlink(resolved, symlink, sizeof(symlink) - 1); 164 if (slen < 0) 165 goto err; 166 symlink[slen] = '\0'; 167 if (symlink[0] == '/') { 168 resolved[1] = 0; 169 resolved_len = 1; 170 } else if (resolved_len > 1) { 171 /* Strip the last path component. */ 172 resolved[resolved_len - 1] = '\0'; 173 q = strrchr(resolved, '/') + 1; 174 *q = '\0'; 175 resolved_len = q - resolved; 176 } 177 178 /* 179 * If there are any path components left, then 180 * append them to symlink. The result is placed 181 * in `left'. 182 */ 183 if (p != NULL) { 184 if (symlink[slen - 1] != '/') { 185 if (slen + 1 >= sizeof(symlink)) { 186 errno = ENAMETOOLONG; 187 goto err; 188 } 189 symlink[slen] = '/'; 190 symlink[slen + 1] = 0; 191 } 192 left_len = strlcat(symlink, left, sizeof(symlink)); 193 if (left_len >= sizeof(symlink)) { 194 errno = ENAMETOOLONG; 195 goto err; 196 } 197 } 198 left_len = strlcpy(left, symlink, sizeof(left)); 199 } 200 } 201 202 /* 203 * Remove trailing slash except when the resolved pathname 204 * is a single "/". 205 */ 206 if (resolved_len > 1 && resolved[resolved_len - 1] == '/') 207 resolved[resolved_len - 1] = '\0'; 208 return (resolved); 209 210 err: 211 if (mem_allocated) 212 free(resolved); 213 return (NULL); 214 } 215