]>
Commit | Line | Data |
---|---|---|
c270684a RC |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2010 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
20 | #include <unistd.h> | |
21 | ||
22 | #include "alloc-util.h" | |
c270684a | 23 | #include "fd-util.h" |
d944dc95 | 24 | #include "fileio.h" |
c270684a RC |
25 | #include "fs-util.h" |
26 | #include "macro.h" | |
27 | #include "mkdir.h" | |
d944dc95 | 28 | #include "path-util.h" |
c270684a RC |
29 | #include "rm-rf.h" |
30 | #include "string-util.h" | |
31 | #include "strv.h" | |
32 | #include "util.h" | |
33 | ||
d944dc95 LP |
34 | static void test_chase_symlinks(void) { |
35 | _cleanup_free_ char *result = NULL; | |
36 | char temp[] = "/tmp/test-chase.XXXXXX"; | |
37 | const char *top, *p, *q; | |
38 | int r; | |
39 | ||
40 | assert_se(mkdtemp(temp)); | |
41 | ||
42 | top = strjoina(temp, "/top"); | |
43 | assert_se(mkdir(top, 0700) >= 0); | |
44 | ||
45 | p = strjoina(top, "/dot"); | |
46 | assert_se(symlink(".", p) >= 0); | |
47 | ||
48 | p = strjoina(top, "/dotdot"); | |
49 | assert_se(symlink("..", p) >= 0); | |
50 | ||
51 | p = strjoina(top, "/dotdota"); | |
52 | assert_se(symlink("../a", p) >= 0); | |
53 | ||
54 | p = strjoina(temp, "/a"); | |
55 | assert_se(symlink("b", p) >= 0); | |
56 | ||
57 | p = strjoina(temp, "/b"); | |
58 | assert_se(symlink("/usr", p) >= 0); | |
59 | ||
60 | p = strjoina(temp, "/start"); | |
61 | assert_se(symlink("top/dot/dotdota", p) >= 0); | |
62 | ||
df878e68 ZJS |
63 | /* Paths that use symlinks underneath the "root" */ |
64 | ||
c4f4fce7 | 65 | r = chase_symlinks(p, NULL, 0, &result); |
a9fb0867 | 66 | assert_se(r > 0); |
d944dc95 LP |
67 | assert_se(path_equal(result, "/usr")); |
68 | ||
69 | result = mfree(result); | |
c4f4fce7 | 70 | r = chase_symlinks(p, temp, 0, &result); |
d944dc95 LP |
71 | assert_se(r == -ENOENT); |
72 | ||
73 | q = strjoina(temp, "/usr"); | |
a9fb0867 | 74 | |
cb638b5e | 75 | r = chase_symlinks(p, temp, CHASE_NONEXISTENT, &result); |
a9fb0867 LP |
76 | assert_se(r == 0); |
77 | assert_se(path_equal(result, q)); | |
78 | ||
d944dc95 LP |
79 | assert_se(mkdir(q, 0700) >= 0); |
80 | ||
f4b85a0f | 81 | result = mfree(result); |
c4f4fce7 | 82 | r = chase_symlinks(p, temp, 0, &result); |
a9fb0867 | 83 | assert_se(r > 0); |
d944dc95 LP |
84 | assert_se(path_equal(result, q)); |
85 | ||
86 | p = strjoina(temp, "/slash"); | |
87 | assert_se(symlink("/", p) >= 0); | |
88 | ||
89 | result = mfree(result); | |
c4f4fce7 | 90 | r = chase_symlinks(p, NULL, 0, &result); |
a9fb0867 | 91 | assert_se(r > 0); |
d944dc95 LP |
92 | assert_se(path_equal(result, "/")); |
93 | ||
94 | result = mfree(result); | |
c4f4fce7 | 95 | r = chase_symlinks(p, temp, 0, &result); |
a9fb0867 | 96 | assert_se(r > 0); |
d944dc95 LP |
97 | assert_se(path_equal(result, temp)); |
98 | ||
df878e68 ZJS |
99 | /* Paths that would "escape" outside of the "root" */ |
100 | ||
101 | p = strjoina(temp, "/6dots"); | |
102 | assert_se(symlink("../../..", p) >= 0); | |
103 | ||
104 | result = mfree(result); | |
c4f4fce7 | 105 | r = chase_symlinks(p, temp, 0, &result); |
a9fb0867 | 106 | assert_se(r > 0 && path_equal(result, temp)); |
df878e68 ZJS |
107 | |
108 | p = strjoina(temp, "/6dotsusr"); | |
109 | assert_se(symlink("../../../usr", p) >= 0); | |
110 | ||
111 | result = mfree(result); | |
c4f4fce7 | 112 | r = chase_symlinks(p, temp, 0, &result); |
a9fb0867 | 113 | assert_se(r > 0 && path_equal(result, q)); |
df878e68 ZJS |
114 | |
115 | p = strjoina(temp, "/top/8dotsusr"); | |
116 | assert_se(symlink("../../../../usr", p) >= 0); | |
117 | ||
118 | result = mfree(result); | |
c4f4fce7 | 119 | r = chase_symlinks(p, temp, 0, &result); |
a9fb0867 | 120 | assert_se(r > 0 && path_equal(result, q)); |
df878e68 ZJS |
121 | |
122 | /* Paths that contain repeated slashes */ | |
123 | ||
d944dc95 LP |
124 | p = strjoina(temp, "/slashslash"); |
125 | assert_se(symlink("///usr///", p) >= 0); | |
126 | ||
127 | result = mfree(result); | |
c4f4fce7 | 128 | r = chase_symlinks(p, NULL, 0, &result); |
a9fb0867 | 129 | assert_se(r > 0); |
d944dc95 LP |
130 | assert_se(path_equal(result, "/usr")); |
131 | ||
132 | result = mfree(result); | |
c4f4fce7 | 133 | r = chase_symlinks(p, temp, 0, &result); |
a9fb0867 | 134 | assert_se(r > 0); |
d944dc95 LP |
135 | assert_se(path_equal(result, q)); |
136 | ||
df878e68 ZJS |
137 | /* Paths using . */ |
138 | ||
d944dc95 | 139 | result = mfree(result); |
c4f4fce7 | 140 | r = chase_symlinks("/etc/./.././", NULL, 0, &result); |
a9fb0867 | 141 | assert_se(r > 0); |
d944dc95 LP |
142 | assert_se(path_equal(result, "/")); |
143 | ||
144 | result = mfree(result); | |
c4f4fce7 | 145 | r = chase_symlinks("/etc/./.././", "/etc", 0, &result); |
a9fb0867 | 146 | assert_se(r > 0 && path_equal(result, "/etc")); |
d944dc95 LP |
147 | |
148 | result = mfree(result); | |
c4f4fce7 | 149 | r = chase_symlinks("/etc/machine-id/foo", NULL, 0, &result); |
d944dc95 LP |
150 | assert_se(r == -ENOTDIR); |
151 | ||
df878e68 ZJS |
152 | /* Path that loops back to self */ |
153 | ||
d944dc95 LP |
154 | result = mfree(result); |
155 | p = strjoina(temp, "/recursive-symlink"); | |
156 | assert_se(symlink("recursive-symlink", p) >= 0); | |
c4f4fce7 | 157 | r = chase_symlinks(p, NULL, 0, &result); |
d944dc95 LP |
158 | assert_se(r == -ELOOP); |
159 | ||
a9fb0867 LP |
160 | /* Path which doesn't exist */ |
161 | ||
162 | p = strjoina(temp, "/idontexist"); | |
163 | r = chase_symlinks(p, NULL, 0, &result); | |
164 | assert_se(r == -ENOENT); | |
165 | ||
cb638b5e | 166 | r = chase_symlinks(p, NULL, CHASE_NONEXISTENT, &result); |
a9fb0867 LP |
167 | assert_se(r == 0); |
168 | assert_se(path_equal(result, p)); | |
169 | result = mfree(result); | |
170 | ||
171 | p = strjoina(temp, "/idontexist/meneither"); | |
172 | r = chase_symlinks(p, NULL, 0, &result); | |
173 | assert_se(r == -ENOENT); | |
174 | ||
cb638b5e | 175 | r = chase_symlinks(p, NULL, CHASE_NONEXISTENT, &result); |
a9fb0867 LP |
176 | assert_se(r == 0); |
177 | assert_se(path_equal(result, p)); | |
178 | result = mfree(result); | |
179 | ||
180 | /* Path which doesn't exist, but contains weird stuff */ | |
181 | ||
182 | p = strjoina(temp, "/idontexist/.."); | |
183 | r = chase_symlinks(p, NULL, 0, &result); | |
184 | assert_se(r == -ENOENT); | |
185 | ||
cb638b5e | 186 | r = chase_symlinks(p, NULL, CHASE_NONEXISTENT, &result); |
a9fb0867 LP |
187 | assert_se(r == -ENOENT); |
188 | ||
d944dc95 LP |
189 | assert_se(rm_rf(temp, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0); |
190 | } | |
191 | ||
c270684a RC |
192 | static void test_unlink_noerrno(void) { |
193 | char name[] = "/tmp/test-close_nointr.XXXXXX"; | |
194 | int fd; | |
195 | ||
646853bd | 196 | fd = mkostemp_safe(name); |
c270684a RC |
197 | assert_se(fd >= 0); |
198 | assert_se(close_nointr(fd) >= 0); | |
199 | ||
200 | { | |
201 | PROTECT_ERRNO; | |
202 | errno = -42; | |
203 | assert_se(unlink_noerrno(name) >= 0); | |
204 | assert_se(errno == -42); | |
205 | assert_se(unlink_noerrno(name) < 0); | |
206 | assert_se(errno == -42); | |
207 | } | |
208 | } | |
209 | ||
210 | static void test_readlink_and_make_absolute(void) { | |
211 | char tempdir[] = "/tmp/test-readlink_and_make_absolute"; | |
212 | char name[] = "/tmp/test-readlink_and_make_absolute/original"; | |
213 | char name2[] = "test-readlink_and_make_absolute/original"; | |
214 | char name_alias[] = "/tmp/test-readlink_and_make_absolute-alias"; | |
215 | char *r = NULL; | |
216 | ||
217 | assert_se(mkdir_safe(tempdir, 0755, getuid(), getgid()) >= 0); | |
218 | assert_se(touch(name) >= 0); | |
219 | ||
220 | assert_se(symlink(name, name_alias) >= 0); | |
221 | assert_se(readlink_and_make_absolute(name_alias, &r) >= 0); | |
222 | assert_se(streq(r, name)); | |
223 | free(r); | |
224 | assert_se(unlink(name_alias) >= 0); | |
225 | ||
226 | assert_se(chdir(tempdir) >= 0); | |
227 | assert_se(symlink(name2, name_alias) >= 0); | |
228 | assert_se(readlink_and_make_absolute(name_alias, &r) >= 0); | |
229 | assert_se(streq(r, name)); | |
230 | free(r); | |
231 | assert_se(unlink(name_alias) >= 0); | |
232 | ||
233 | assert_se(rm_rf(tempdir, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0); | |
234 | } | |
235 | ||
236 | static void test_get_files_in_directory(void) { | |
237 | _cleanup_strv_free_ char **l = NULL, **t = NULL; | |
238 | ||
239 | assert_se(get_files_in_directory("/tmp", &l) >= 0); | |
240 | assert_se(get_files_in_directory(".", &t) >= 0); | |
241 | assert_se(get_files_in_directory(".", NULL) >= 0); | |
242 | } | |
243 | ||
34a8f081 | 244 | static void test_var_tmp(void) { |
4245eb50 | 245 | _cleanup_free_ char *tmpdir_backup = NULL, *temp_backup = NULL, *tmp_backup = NULL; |
992e8f22 | 246 | const char *tmp_dir = NULL, *t; |
34a8f081 | 247 | |
992e8f22 LP |
248 | t = getenv("TMPDIR"); |
249 | if (t) { | |
250 | tmpdir_backup = strdup(t); | |
251 | assert_se(tmpdir_backup); | |
252 | } | |
34a8f081 | 253 | |
4245eb50 MAP |
254 | t = getenv("TEMP"); |
255 | if (t) { | |
256 | temp_backup = strdup(t); | |
257 | assert_se(temp_backup); | |
258 | } | |
259 | ||
260 | t = getenv("TMP"); | |
261 | if (t) { | |
262 | tmp_backup = strdup(t); | |
263 | assert_se(tmp_backup); | |
264 | } | |
265 | ||
992e8f22 | 266 | assert(unsetenv("TMPDIR") >= 0); |
4245eb50 MAP |
267 | assert(unsetenv("TEMP") >= 0); |
268 | assert(unsetenv("TMP") >= 0); | |
34a8f081 | 269 | |
992e8f22 LP |
270 | assert_se(var_tmp_dir(&tmp_dir) >= 0); |
271 | assert_se(streq(tmp_dir, "/var/tmp")); | |
34a8f081 | 272 | |
992e8f22 LP |
273 | assert_se(setenv("TMPDIR", "/tmp", true) >= 0); |
274 | assert_se(streq(getenv("TMPDIR"), "/tmp")); | |
34a8f081 | 275 | |
992e8f22 LP |
276 | assert_se(var_tmp_dir(&tmp_dir) >= 0); |
277 | assert_se(streq(tmp_dir, "/tmp")); | |
34a8f081 | 278 | |
992e8f22 LP |
279 | assert_se(setenv("TMPDIR", "/88_does_not_exist_88", true) >= 0); |
280 | assert_se(streq(getenv("TMPDIR"), "/88_does_not_exist_88")); | |
34a8f081 | 281 | |
992e8f22 LP |
282 | assert_se(var_tmp_dir(&tmp_dir) >= 0); |
283 | assert_se(streq(tmp_dir, "/var/tmp")); | |
34a8f081 | 284 | |
992e8f22 LP |
285 | if (tmpdir_backup) { |
286 | assert_se(setenv("TMPDIR", tmpdir_backup, true) >= 0); | |
287 | assert_se(streq(getenv("TMPDIR"), tmpdir_backup)); | |
34a8f081 | 288 | } |
4245eb50 MAP |
289 | |
290 | if (temp_backup) { | |
291 | assert_se(setenv("TEMP", temp_backup, true) >= 0); | |
292 | assert_se(streq(getenv("TEMP"), temp_backup)); | |
293 | } | |
294 | ||
295 | if (tmp_backup) { | |
296 | assert_se(setenv("TMP", tmp_backup, true) >= 0); | |
297 | assert_se(streq(getenv("TMP"), tmp_backup)); | |
298 | } | |
34a8f081 OW |
299 | } |
300 | ||
c270684a RC |
301 | int main(int argc, char *argv[]) { |
302 | test_unlink_noerrno(); | |
303 | test_readlink_and_make_absolute(); | |
304 | test_get_files_in_directory(); | |
34a8f081 | 305 | test_var_tmp(); |
d944dc95 | 306 | test_chase_symlinks(); |
c270684a RC |
307 | |
308 | return 0; | |
309 | } |