OSDN Git Service

- tidy up utils_install
[uclinux-h8/uClibc.git] / utils / chroot_realpath.c
1 /*
2  * chroot_realpath.c -- resolve pathname as if inside chroot
3  * Based on realpath.c Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
4  *
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.
9  *
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.
14  *
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.
19  *
20  * 2005/09/12: Dan Howell (modified from realpath.c to emulate chroot)
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <sys/types.h>
28 #include <unistd.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <strings.h>
32 #include <limits.h>             /* for PATH_MAX */
33 #include <sys/param.h>          /* for MAXPATHLEN */
34 #include <errno.h>
35 #include <sys/stat.h>           /* for S_IFLNK */
36
37 #ifndef PATH_MAX
38 #define PATH_MAX _POSIX_PATH_MAX
39 #endif
40
41 #define MAX_READLINKS 32
42
43 char *chroot_realpath(const char *chroot, const char *path,
44                       char resolved_path[])
45 {
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;
51         char *max_path;
52         int readlinks = 0;
53         int n;
54         int chroot_len;
55
56         /* Trivial case. */
57         if (chroot == NULL || *chroot == '\0' ||
58             (*chroot == '/' && chroot[1] == '\0')) {
59                 strcpy(resolved_path, path);
60                 return resolved_path;
61         }
62
63         chroot_len = strlen(chroot);
64
65         if (chroot_len + strlen(path) >= PATH_MAX - 3) {
66                 errno = ENAMETOOLONG;
67                 return NULL;
68         }
69
70         /* Make a copy of the source path since we may need to modify it. */
71         strcpy(copy_path, path);
72         path = copy_path;
73         max_path = copy_path + PATH_MAX - chroot_len - 3;
74
75         /* Start with the chroot path. */
76         strcpy(new_path, chroot);
77         new_path += chroot_len;
78         while (*new_path == '/' && new_path > got_path)
79                 new_path--;
80         got_path_root = new_path;
81         *new_path++ = '/';
82
83         /* Expand each slash-separated pathname component. */
84         while (*path != '\0') {
85                 /* Ignore stray "/". */
86                 if (*path == '/') {
87                         path++;
88                         continue;
89                 }
90                 if (*path == '.') {
91                         /* Ignore ".". */
92                         if (path[1] == '\0' || path[1] == '/') {
93                                 path++;
94                                 continue;
95                         }
96                         if (path[1] == '.') {
97                                 if (path[2] == '\0' || path[2] == '/') {
98                                         path += 2;
99                                         /* Ignore ".." at root. */
100                                         if (new_path == got_path_root + 1)
101                                                 continue;
102                                         /* Handle ".." by backing up. */
103                                         while ((--new_path)[-1] != '/') ;
104                                         continue;
105                                 }
106                         }
107                 }
108                 /* Safely copy the next pathname component. */
109                 while (*path != '\0' && *path != '/') {
110                         if (path > max_path) {
111                                 errno = ENAMETOOLONG;
112                                 return NULL;
113                         }
114                         *new_path++ = *path++;
115                 }
116                 if (*path == '\0')
117                         /* Don't follow symlink for last pathname component. */
118                         break;
119 #ifdef S_IFLNK
120                 /* Protect against infinite loops. */
121                 if (readlinks++ > MAX_READLINKS) {
122                         errno = ELOOP;
123                         return NULL;
124                 }
125                 /* See if latest pathname component is a symlink. */
126                 *new_path = '\0';
127                 n = readlink(got_path, link_path, PATH_MAX - 1);
128                 if (n < 0) {
129                         /* EINVAL means the file exists but isn't a symlink. */
130                         if (errno != EINVAL) {
131                                 /* Make sure it's null terminated. */
132                                 *new_path = '\0';
133                                 strcpy(resolved_path, got_path);
134                                 return NULL;
135                         }
136                 } else {
137                         /* Note: readlink doesn't add the null byte. */
138                         link_path[n] = '\0';
139                         if (*link_path == '/')
140                                 /* Start over for an absolute symlink. */
141                                 new_path = got_path_root;
142                         else
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;
148                                 return NULL;
149                         }
150                         /* Insert symlink contents into path. */
151                         strcat(link_path, path);
152                         strcpy(copy_path, link_path);
153                         path = copy_path;
154                 }
155 #endif                          /* S_IFLNK */
156                 *new_path++ = '/';
157         }
158         /* Delete trailing slash but don't whomp a lone slash. */
159         if (new_path != got_path + 1 && new_path[-1] == '/')
160                 new_path--;
161         /* Make sure it's null terminated. */
162         *new_path = '\0';
163         strcpy(resolved_path, got_path);
164         return resolved_path;
165 }