1 /* Return the canonical absolute name of a given file inside chroot.
2 Copyright (C) 1996,1997,1998,1999,2000,2001 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library 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 The GNU C 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 the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 #include <sys/param.h>
36 /* Return the canonical absolute name of file NAME as if chroot(CHROOT) was
37 done first. A canonical name does not contain any `.', `..' components
38 nor any repeated path separators ('/') or symlinks. All path components
39 must exist and NAME must be absolute filename. The result is malloc'd.
40 The returned name includes the CHROOT prefix. */
43 chroot_canon (const char *chroot
, const char *name
)
47 char *extra_buf
= NULL
;
51 const char *rpath_limit
;
53 size_t chroot_len
= strlen (chroot
);
61 rpath
= malloc (chroot_len
+ PATH_MAX
);
62 rpath_limit
= rpath
+ chroot_len
+ PATH_MAX
;
64 rpath_root
= (char *) mempcpy (rpath
, chroot
, chroot_len
) - 1;
65 if (*rpath_root
!= '/')
67 dest
= rpath_root
+ 1;
69 for (start
= end
= name
; *start
; start
= end
)
74 /* Skip sequence of multiple path-separators. */
78 /* Find end of path component. */
79 for (end
= start
; *end
&& *end
!= '/'; ++end
)
84 else if (end
- start
== 1 && start
[0] == '.')
86 else if (end
- start
== 2 && start
[0] == '.' && start
[1] == '.')
88 /* Back up to previous component, ignore if at root already. */
89 if (dest
> rpath_root
+ 1)
90 while ((--dest
)[-1] != '/');
99 if (dest
+ (end
- start
) >= rpath_limit
)
101 ptrdiff_t dest_offset
= dest
- rpath
;
104 new_size
= rpath_limit
- rpath
;
105 if (end
- start
+ 1 > PATH_MAX
)
106 new_size
+= end
- start
+ 1;
108 new_size
+= PATH_MAX
;
109 new_rpath
= (char *) realloc (rpath
, new_size
);
110 if (new_rpath
== NULL
)
113 rpath_limit
= rpath
+ new_size
;
115 dest
= rpath
+ dest_offset
;
118 dest
= mempcpy (dest
, start
, end
- start
);
121 if (lstat64 (rpath
, &st
) < 0)
128 if (S_ISLNK (st
.st_mode
))
130 char *buf
= alloca (PATH_MAX
);
133 if (++num_links
> MAXSYMLINKS
)
139 n
= readlink (rpath
, buf
, PATH_MAX
);
149 extra_buf
= alloca (PATH_MAX
);
152 if ((long int) (n
+ len
) >= PATH_MAX
)
154 __set_errno (ENAMETOOLONG
);
158 /* Careful here, end may be a pointer into extra_buf... */
159 memmove (&extra_buf
[n
], end
, len
+ 1);
160 name
= end
= memcpy (extra_buf
, buf
, n
);
163 dest
= rpath_root
+ 1; /* It's an absolute symlink */
165 /* Back up to previous component, ignore if at root already: */
166 if (dest
> rpath_root
+ 1)
167 while ((--dest
)[-1] != '/');
172 if (dest
> rpath_root
+ 1 && dest
[-1] == '/')