2 * chroot_realpath.c -- reslove pathname as if inside chroot
3 * Based on realpath.c Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; see the file COPYING.LIB. If not,
17 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * 2005/09/12: Dan Howell (modified from realpath.c to emulate chroot)
27 #include <sys/types.h>
32 #include <limits.h> /* for PATH_MAX */
33 #include <sys/param.h> /* for MAXPATHLEN */
36 #define __set_errno(val) ((errno) = (val))
39 #include <sys/stat.h> /* for S_IFLNK */
42 #define PATH_MAX _POSIX_PATH_MAX
45 #define MAX_READLINKS 32
47 char *chroot_realpath(const char *chroot, const char *path,
50 char copy_path[PATH_MAX];
51 char link_path[PATH_MAX];
52 char got_path[PATH_MAX];
53 char *got_path_root = got_path;
54 char *new_path = got_path;
61 if (chroot == NULL || *chroot == '\0' ||
62 (*chroot == '/' && chroot[1] == '\0')) {
63 strcpy(resolved_path, path);
67 chroot_len = strlen(chroot);
69 if (chroot_len + strlen(path) >= PATH_MAX - 3) {
70 __set_errno(ENAMETOOLONG);
74 /* Make a copy of the source path since we may need to modify it. */
75 strcpy(copy_path, path);
77 max_path = copy_path + PATH_MAX - chroot_len - 3;
79 /* Start with the chroot path. */
80 strcpy(new_path, chroot);
81 new_path += chroot_len;
82 while (*new_path == '/' && new_path > got_path)
84 got_path_root = new_path;
87 /* Expand each slash-separated pathname component. */
88 while (*path != '\0') {
89 /* Ignore stray "/". */
96 if (path[1] == '\0' || path[1] == '/') {
100 if (path[1] == '.') {
101 if (path[2] == '\0' || path[2] == '/') {
103 /* Ignore ".." at root. */
104 if (new_path == got_path_root + 1)
106 /* Handle ".." by backing up. */
107 while ((--new_path)[-1] != '/') ;
112 /* Safely copy the next pathname component. */
113 while (*path != '\0' && *path != '/') {
114 if (path > max_path) {
115 __set_errno(ENAMETOOLONG);
118 *new_path++ = *path++;
121 /* Don't follow symlink for last pathname component. */
124 /* Protect against infinite loops. */
125 if (readlinks++ > MAX_READLINKS) {
129 /* See if latest pathname component is a symlink. */
131 n = readlink(got_path, link_path, PATH_MAX - 1);
133 /* EINVAL means the file exists but isn't a symlink. */
134 if (errno != EINVAL) {
135 /* Make sure it's null terminated. */
137 strcpy(resolved_path, got_path);
141 /* Note: readlink doesn't add the null byte. */
143 if (*link_path == '/')
144 /* Start over for an absolute symlink. */
145 new_path = got_path_root;
147 /* Otherwise back up over this component. */
148 while (*(--new_path) != '/') ;
149 /* Safe sex check. */
150 if (strlen(path) + n >= PATH_MAX - 2) {
151 __set_errno(ENAMETOOLONG);
154 /* Insert symlink contents into path. */
155 strcat(link_path, path);
156 strcpy(copy_path, link_path);
162 /* Delete trailing slash but don't whomp a lone slash. */
163 if (new_path != got_path + 1 && new_path[-1] == '/')
165 /* Make sure it's null terminated. */
167 strcpy(resolved_path, got_path);
168 return resolved_path;