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 as published
8 by the Free Software Foundation; version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
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
);
65 rpath_limit
= rpath
+ chroot_len
+ PATH_MAX
;
67 rpath_root
= (char *) mempcpy (rpath
, chroot
, chroot_len
) - 1;
68 if (*rpath_root
!= '/')
70 dest
= rpath_root
+ 1;
72 for (start
= end
= name
; *start
; start
= end
)
77 /* Skip sequence of multiple path-separators. */
81 /* Find end of path component. */
82 for (end
= start
; *end
&& *end
!= '/'; ++end
)
87 else if (end
- start
== 1 && start
[0] == '.')
89 else if (end
- start
== 2 && start
[0] == '.' && start
[1] == '.')
91 /* Back up to previous component, ignore if at root already. */
92 if (dest
> rpath_root
+ 1)
93 while ((--dest
)[-1] != '/');
102 if (dest
+ (end
- start
) >= rpath_limit
)
104 ptrdiff_t dest_offset
= dest
- rpath
;
107 new_size
= rpath_limit
- rpath
;
108 if (end
- start
+ 1 > PATH_MAX
)
109 new_size
+= end
- start
+ 1;
111 new_size
+= PATH_MAX
;
112 new_rpath
= (char *) realloc (rpath
, new_size
);
113 if (new_rpath
== NULL
)
116 rpath_limit
= rpath
+ new_size
;
118 dest
= rpath
+ dest_offset
;
121 dest
= mempcpy (dest
, start
, end
- start
);
124 if (lstat64 (rpath
, &st
) < 0)
131 if (S_ISLNK (st
.st_mode
))
133 char *buf
= alloca (PATH_MAX
);
136 if (++num_links
> MAXSYMLINKS
)
142 n
= readlink (rpath
, buf
, PATH_MAX
);
152 extra_buf
= alloca (PATH_MAX
);
155 if ((long int) (n
+ len
) >= PATH_MAX
)
157 __set_errno (ENAMETOOLONG
);
161 /* Careful here, end may be a pointer into extra_buf... */
162 memmove (&extra_buf
[n
], end
, len
+ 1);
163 name
= end
= memcpy (extra_buf
, buf
, n
);
166 dest
= rpath_root
+ 1; /* It's an absolute symlink */
168 /* Back up to previous component, ignore if at root already: */
169 if (dest
> rpath_root
+ 1)
170 while ((--dest
)[-1] != '/');
175 if (dest
> rpath_root
+ 1 && dest
[-1] == '/')