OSDN Git Service

cleanup style; no functional changes
[uclinux-h8/uClibc.git] / utils / chroot_realpath.c
1 /*
2  * chroot_realpath.c -- reslove 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 #ifndef __set_errno
36 #define __set_errno(val) ((errno) = (val))
37 #endif
38
39 #include <sys/stat.h>           /* for S_IFLNK */
40
41 #ifndef PATH_MAX
42 #define PATH_MAX _POSIX_PATH_MAX
43 #endif
44
45 #define MAX_READLINKS 32
46
47 char *chroot_realpath(const char *chroot, const char *path,
48                       char resolved_path[])
49 {
50         char copy_path[PATH_MAX];
51         char link_path[PATH_MAX];
52         char got_path[PATH_MAX];
53         char *got_path_root = got_path;
54         char *new_path = got_path;
55         char *max_path;
56         int readlinks = 0;
57         int n;
58         int chroot_len;
59
60         /* Trivial case. */
61         if (chroot == NULL || *chroot == '\0' ||
62             (*chroot == '/' && chroot[1] == '\0')) {
63                 strcpy(resolved_path, path);
64                 return resolved_path;
65         }
66
67         chroot_len = strlen(chroot);
68
69         if (chroot_len + strlen(path) >= PATH_MAX - 3) {
70                 __set_errno(ENAMETOOLONG);
71                 return NULL;
72         }
73
74         /* Make a copy of the source path since we may need to modify it. */
75         strcpy(copy_path, path);
76         path = copy_path;
77         max_path = copy_path + PATH_MAX - chroot_len - 3;
78
79         /* Start with the chroot path. */
80         strcpy(new_path, chroot);
81         new_path += chroot_len;
82         while (*new_path == '/' && new_path > got_path)
83                 new_path--;
84         got_path_root = new_path;
85         *new_path++ = '/';
86
87         /* Expand each slash-separated pathname component. */
88         while (*path != '\0') {
89                 /* Ignore stray "/". */
90                 if (*path == '/') {
91                         path++;
92                         continue;
93                 }
94                 if (*path == '.') {
95                         /* Ignore ".". */
96                         if (path[1] == '\0' || path[1] == '/') {
97                                 path++;
98                                 continue;
99                         }
100                         if (path[1] == '.') {
101                                 if (path[2] == '\0' || path[2] == '/') {
102                                         path += 2;
103                                         /* Ignore ".." at root. */
104                                         if (new_path == got_path_root + 1)
105                                                 continue;
106                                         /* Handle ".." by backing up. */
107                                         while ((--new_path)[-1] != '/') ;
108                                         continue;
109                                 }
110                         }
111                 }
112                 /* Safely copy the next pathname component. */
113                 while (*path != '\0' && *path != '/') {
114                         if (path > max_path) {
115                                 __set_errno(ENAMETOOLONG);
116                                 return NULL;
117                         }
118                         *new_path++ = *path++;
119                 }
120                 if (*path == '\0')
121                         /* Don't follow symlink for last pathname component. */
122                         break;
123 #ifdef S_IFLNK
124                 /* Protect against infinite loops. */
125                 if (readlinks++ > MAX_READLINKS) {
126                         __set_errno(ELOOP);
127                         return NULL;
128                 }
129                 /* See if latest pathname component is a symlink. */
130                 *new_path = '\0';
131                 n = readlink(got_path, link_path, PATH_MAX - 1);
132                 if (n < 0) {
133                         /* EINVAL means the file exists but isn't a symlink. */
134                         if (errno != EINVAL) {
135                                 /* Make sure it's null terminated. */
136                                 *new_path = '\0';
137                                 strcpy(resolved_path, got_path);
138                                 return NULL;
139                         }
140                 } else {
141                         /* Note: readlink doesn't add the null byte. */
142                         link_path[n] = '\0';
143                         if (*link_path == '/')
144                                 /* Start over for an absolute symlink. */
145                                 new_path = got_path_root;
146                         else
147                                 /* Otherwise back up over this component. */
148                                 while (*(--new_path) != '/') ;
149                         /* Safe sex check. */
150                         if (strlen(path) + n >= PATH_MAX - 2) {
151                                 __set_errno(ENAMETOOLONG);
152                                 return NULL;
153                         }
154                         /* Insert symlink contents into path. */
155                         strcat(link_path, path);
156                         strcpy(copy_path, link_path);
157                         path = copy_path;
158                 }
159 #endif                          /* S_IFLNK */
160                 *new_path++ = '/';
161         }
162         /* Delete trailing slash but don't whomp a lone slash. */
163         if (new_path != got_path + 1 && new_path[-1] == '/')
164                 new_path--;
165         /* Make sure it's null terminated. */
166         *new_path = '\0';
167         strcpy(resolved_path, got_path);
168         return resolved_path;
169 }