1 /* Return the canonical absolute name of a given file inside chroot.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2004, 2005
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License version 2 as
8 published by the Free Software Foundation.
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 #include <sys/param.h>
35 /* Return the canonical absolute name of file NAME as if chroot(CHROOT) was
36 done first. A canonical name does not contain any `.', `..' components
37 nor any repeated path separators ('/') or symlinks. All path components
38 must exist and NAME must be absolute filename. The result is malloc'd.
39 The returned name includes the CHROOT prefix. */
42 chroot_canon (const char *chroot
, const char *name
)
46 char *extra_buf
= NULL
;
50 const char *rpath_limit
;
52 size_t chroot_len
= strlen (chroot
);
60 rpath
= malloc (chroot_len
+ PATH_MAX
);
64 rpath_limit
= rpath
+ chroot_len
+ PATH_MAX
;
66 rpath_root
= (char *) mempcpy (rpath
, chroot
, chroot_len
) - 1;
67 if (*rpath_root
!= '/')
69 dest
= rpath_root
+ 1;
71 for (start
= end
= name
; *start
; start
= end
)
76 /* Skip sequence of multiple path-separators. */
80 /* Find end of path component. */
81 for (end
= start
; *end
&& *end
!= '/'; ++end
)
86 else if (end
- start
== 1 && start
[0] == '.')
88 else if (end
- start
== 2 && start
[0] == '.' && start
[1] == '.')
90 /* Back up to previous component, ignore if at root already. */
91 if (dest
> rpath_root
+ 1)
92 while ((--dest
)[-1] != '/');
101 if (dest
+ (end
- start
) >= rpath_limit
)
103 ptrdiff_t dest_offset
= dest
- rpath
;
106 new_size
= rpath_limit
- rpath
;
107 if (end
- start
+ 1 > PATH_MAX
)
108 new_size
+= end
- start
+ 1;
110 new_size
+= PATH_MAX
;
111 new_rpath
= (char *) realloc (rpath
, new_size
);
112 if (new_rpath
== NULL
)
115 rpath_limit
= rpath
+ new_size
;
117 dest
= rpath
+ dest_offset
;
120 dest
= mempcpy (dest
, start
, end
- start
);
123 if (lstat64 (rpath
, &st
) < 0)
130 if (S_ISLNK (st
.st_mode
))
132 char *buf
= alloca (PATH_MAX
);
135 if (++num_links
> MAXSYMLINKS
)
141 n
= readlink (rpath
, buf
, PATH_MAX
);
151 extra_buf
= alloca (PATH_MAX
);
154 if ((long int) (n
+ len
) >= PATH_MAX
)
156 __set_errno (ENAMETOOLONG
);
160 /* Careful here, end may be a pointer into extra_buf... */
161 memmove (&extra_buf
[n
], end
, len
+ 1);
162 name
= end
= memcpy (extra_buf
, buf
, n
);
165 dest
= rpath_root
+ 1; /* It's an absolute symlink */
167 /* Back up to previous component, ignore if at root already: */
168 if (dest
> rpath_root
+ 1)
169 while ((--dest
)[-1] != '/');
174 if (dest
> rpath_root
+ 1 && dest
[-1] == '/')