Debian Codegraph @

src/dedotdot.c

raw
1 /*
2 libfakechroot -- fake chroot environment
3 Copyright (c) 2013-2015 Piotr Roszatycki <[email protected]>
4
5 This 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.
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; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20
21 #include <config.h>
22
23 #include "libfakechroot.h"
24 #include "strlcpy.h"
25
26 /* mini_httpd - small HTTP server
27 **
28 ** Copyright © 1999,2000 by Jef Poskanzer <[email protected]>.
29 ** All rights reserved.
30 **
31 ** Redistribution and use in source and binary forms, with or without
32 ** modification, are permitted provided that the following conditions
33 ** are met:
34 ** 1. Redistributions of source code must retain the above copyright
35 ** notice, this list of conditions and the following disclaimer.
36 ** 2. Redistributions in binary form must reproduce the above copyright
37 ** notice, this list of conditions and the following disclaimer in the
38 ** documentation and/or other materials provided with the distribution.
39 **
40 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
41 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 ** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 ** SUCH DAMAGE.
51 */
52
53 #include <string.h>
54
55 LOCAL void dedotdot(char * file)
56 {
57 char c, *cp, *cp2;
58 int l;
59
60 if (!file || !*file)
61 return;
62
63 /* Collapse any multiple / sequences. */
64 while ((cp = strstr(file, "//")) != (char*) 0) {
65 for (cp2 = cp + 2; *cp2 == '/'; ++cp2)
66 continue;
67 (void) strlcpy(cp + 1, cp2, strlen(cp2) + 1);
68 }
69
70 /* Remove leading ./ and any /./ sequences. */
71 while (strncmp(file, "./", 2) == 0)
72 (void) strlcpy(file, file + 2, strlen(file) - 1);
73 while ((cp = strstr(file, "/./")) != (char*) 0)
74 (void) strlcpy(cp, cp + 2, strlen(cp) - 1);
75
76 /* Alternate between removing leading ../ and removing foo/../ */
77 for (;;) {
78 while (strncmp(file, "/../", 4) == 0)
79 (void) strlcpy(file, file + 3, strlen(file) - 2);
80 cp = strstr(file, "/../");
81 if (cp == (char*) 0 || strncmp(file, "../", 3) == 0)
82 break;
83 for (cp2 = cp - 1; cp2 >= file && *cp2 != '/'; --cp2)
84 continue;
85 (void) strlcpy(cp2 + 1, cp + 4, strlen(cp) - 3);
86 }
87
88 /* Also elide any foo/.. at the end. */
89 while (strncmp(file, "../", 3) != 0 && (l = strlen(file)) > 3
90 && strcmp((cp = file + l - 3), "/..") == 0) {
91
92 for (cp2 = cp - 1; cp2 > file && *cp2 != '/'; --cp2)
93 continue;
94
95 if (cp2 < file)
96 break;
97 if (strncmp(cp2, "../", 3) == 0)
98 break;
99
100 c = *cp2;
101 *cp2 = '\0';
102
103 if (file == cp2 && c == '/') {
104 strcpy(file, "/");
105 }
106 }
107
108 /* Correct some paths */
109 if (*file == '\0') {
110 strcpy(file, ".");
111 }
112 else if (strcmp(file, "/.") == 0 || strcmp(file, "/..") == 0) {
113 strcpy(file, "/");
114 }
115
116 /* Any /. and the end */
117 for (l = strlen(file); l > 3 && strcmp((cp = file + l - 2), "/.") == 0; l -= 2) {
118 *cp = '\0';
119 }
120 }