2 * chroot_realpath.c -- resolve 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 */
35 #include <sys/stat.h> /* for S_IFLNK */
38 #define PATH_MAX _POSIX_PATH_MAX
41 #define MAX_READLINKS 32
43 char *chroot_realpath(const char *chroot, const char *path,
46 char copy_path[PATH_MAX];
47 char link_path[PATH_MAX];
48 char got_path[PATH_MAX];
49 char *got_path_root = got_path;
50 char *new_path = got_path;
57 if (chroot == NULL || *chroot == '\0' ||
58 (*chroot == '/' && chroot[1] == '\0')) {
59 strcpy(resolved_path, path);
63 chroot_len = strlen(chroot);
65 if (chroot_len + strlen(path) >= PATH_MAX - 3) {
70 /* Make a copy of the source path since we may need to modify it. */
71 strcpy(copy_path, path);
73 max_path = copy_path + PATH_MAX - chroot_len - 3;
75 /* Start with the chroot path. */
76 strcpy(new_path, chroot);
77 new_path += chroot_len;
78 while (*new_path == '/' && new_path > got_path)
80 got_path_root = new_path;
83 /* Expand each slash-separated pathname component. */
84 while (*path != '\0') {
85 /* Ignore stray "/". */
92 if (path[1] == '\0' || path[1] == '/') {
97 if (path[2] == '\0' || path[2] == '/') {
99 /* Ignore ".." at root. */
100 if (new_path == got_path_root + 1)
102 /* Handle ".." by backing up. */
103 while ((--new_path)[-1] != '/') ;
108 /* Safely copy the next pathname component. */
109 while (*path != '\0' && *path != '/') {
110 if (path > max_path) {
111 errno = ENAMETOOLONG;
114 *new_path++ = *path++;
117 /* Don't follow symlink for last pathname component. */
120 /* Protect against infinite loops. */
121 if (readlinks++ > MAX_READLINKS) {
125 /* See if latest pathname component is a symlink. */
127 n = readlink(got_path, link_path, PATH_MAX - 1);
129 /* EINVAL means the file exists but isn't a symlink. */
130 if (errno != EINVAL) {
131 /* Make sure it's null terminated. */
133 strcpy(resolved_path, got_path);
137 /* Note: readlink doesn't add the null byte. */
139 if (*link_path == '/')
140 /* Start over for an absolute symlink. */
141 new_path = got_path_root;
143 /* Otherwise back up over this component. */
144 while (*(--new_path) != '/') ;
145 /* Safe sex check. */
146 if (strlen(path) + n >= PATH_MAX - 2) {
147 errno = ENAMETOOLONG;
150 /* Insert symlink contents into path. */
151 strcat(link_path, path);
152 strcpy(copy_path, link_path);
158 /* Delete trailing slash but don't whomp a lone slash. */
159 if (new_path != got_path + 1 && new_path[-1] == '/')
161 /* Make sure it's null terminated. */
163 strcpy(resolved_path, got_path);
164 return resolved_path;