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 * see <http://www.gnu.org/licenses/>.
19 * 2005/09/12: Dan Howell (modified from realpath.c to emulate chroot)
24 #define MAX_READLINKS 32
26 char *chroot_realpath(const char *root
, const char *path
,
27 char resolved_path
[]);
29 char *chroot_realpath(const char *root
, const char *path
,
32 char copy_path
[PATH_MAX
];
33 char link_path
[PATH_MAX
];
34 char got_path
[PATH_MAX
];
35 char *got_path_root
= got_path
;
36 char *new_path
= got_path
;
43 if (root
== NULL
|| *root
== '\0' ||
44 (*root
== '/' && root
[1] == '\0')) {
45 strcpy(resolved_path
, path
);
49 chroot_len
= strlen(root
);
51 if (chroot_len
+ strlen(path
) >= PATH_MAX
- 3) {
56 /* Make a copy of the source path since we may need to modify it. */
57 strcpy(copy_path
, path
);
59 max_path
= copy_path
+ PATH_MAX
- chroot_len
- 3;
61 /* Start with the chroot path. */
62 strcpy(new_path
, root
);
63 new_path
+= chroot_len
;
64 while (*new_path
== '/' && new_path
> got_path
)
66 got_path_root
= new_path
;
69 /* Expand each slash-separated pathname component. */
70 while (*path
!= '\0') {
71 /* Ignore stray "/". */
78 if (path
[1] == '\0' || path
[1] == '/') {
83 if (path
[2] == '\0' || path
[2] == '/') {
85 /* Ignore ".." at root. */
86 if (new_path
== got_path_root
+ 1)
88 /* Handle ".." by backing up. */
89 while ((--new_path
)[-1] != '/') ;
94 /* Safely copy the next pathname component. */
95 while (*path
!= '\0' && *path
!= '/') {
96 if (path
> max_path
) {
100 *new_path
++ = *path
++;
103 /* Don't follow symlink for last pathname component. */
106 /* Protect against infinite loops. */
107 if (readlinks
++ > MAX_READLINKS
) {
111 /* See if latest pathname component is a symlink. */
113 n
= readlink(got_path
, link_path
, PATH_MAX
- 1);
115 /* EINVAL means the file exists but isn't a symlink. */
116 if (errno
!= EINVAL
) {
117 /* Make sure it's null terminated. */
119 strcpy(resolved_path
, got_path
);
123 /* Note: readlink doesn't add the null byte. */
125 if (*link_path
== '/')
126 /* Start over for an absolute symlink. */
127 new_path
= got_path_root
;
129 /* Otherwise back up over this component. */
130 while (*(--new_path
) != '/') ;
131 /* Safe sex check. */
132 if (strlen(path
) + n
>= PATH_MAX
- 2) {
133 errno
= ENAMETOOLONG
;
136 /* Insert symlink contents into path. */
137 strcat(link_path
, path
);
138 strcpy(copy_path
, link_path
);
144 /* Delete trailing slash but don't whomp a lone slash. */
145 if (new_path
!= got_path
+ 1 && new_path
[-1] == '/')
147 /* Make sure it's null terminated. */
149 strcpy(resolved_path
, got_path
);
150 return resolved_path
;