]>
Commit | Line | Data |
---|---|---|
a032b68d | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
663996b3 | 2 | |
663996b3 | 3 | #include <errno.h> |
6300502b MP |
4 | #include <fcntl.h> |
5 | #include <fnmatch.h> | |
6 | #include <getopt.h> | |
663996b3 | 7 | #include <limits.h> |
6300502b MP |
8 | #include <linux/fs.h> |
9 | #include <stdbool.h> | |
10 | #include <stddef.h> | |
663996b3 | 11 | #include <stdlib.h> |
bb4f798a | 12 | #include <sys/file.h> |
f47781d8 | 13 | #include <sys/xattr.h> |
52ad194e | 14 | #include <sysexits.h> |
6300502b MP |
15 | #include <time.h> |
16 | #include <unistd.h> | |
663996b3 | 17 | |
52ad194e MB |
18 | #include "sd-path.h" |
19 | ||
6300502b | 20 | #include "acl-util.h" |
db2df898 | 21 | #include "alloc-util.h" |
6300502b | 22 | #include "btrfs-util.h" |
db2df898 MP |
23 | #include "capability-util.h" |
24 | #include "chattr-util.h" | |
6300502b MP |
25 | #include "conf-files.h" |
26 | #include "copy.h" | |
db2df898 | 27 | #include "def.h" |
2897b343 | 28 | #include "dirent-util.h" |
a032b68d | 29 | #include "dissect-image.h" |
3a6ce677 | 30 | #include "env-util.h" |
db2df898 MP |
31 | #include "escape.h" |
32 | #include "fd-util.h" | |
33 | #include "fileio.h" | |
2897b343 | 34 | #include "format-util.h" |
db2df898 MP |
35 | #include "fs-util.h" |
36 | #include "glob-util.h" | |
37 | #include "io-util.h" | |
6300502b | 38 | #include "label.h" |
663996b3 | 39 | #include "log.h" |
663996b3 | 40 | #include "macro.h" |
6e866b33 | 41 | #include "main-func.h" |
a032b68d MB |
42 | #include "missing_stat.h" |
43 | #include "missing_syscall.h" | |
663996b3 | 44 | #include "mkdir.h" |
a032b68d | 45 | #include "mount-util.h" |
6e866b33 | 46 | #include "mountpoint-util.h" |
a10f5d05 | 47 | #include "offline-passwd.h" |
b012e921 | 48 | #include "pager.h" |
3a6ce677 | 49 | #include "parse-argument.h" |
db2df898 | 50 | #include "parse-util.h" |
52ad194e | 51 | #include "path-lookup.h" |
663996b3 | 52 | #include "path-util.h" |
6e866b33 | 53 | #include "pretty-print.h" |
bb4f798a | 54 | #include "rlimit-util.h" |
e3bff60a | 55 | #include "rm-rf.h" |
e735f4d4 | 56 | #include "selinux-util.h" |
6300502b | 57 | #include "set.h" |
bb4f798a | 58 | #include "sort-util.h" |
6300502b | 59 | #include "specifier.h" |
db2df898 MP |
60 | #include "stat-util.h" |
61 | #include "stdio-util.h" | |
62 | #include "string-table.h" | |
63 | #include "string-util.h" | |
6300502b | 64 | #include "strv.h" |
3a6ce677 | 65 | #include "terminal-util.h" |
db2df898 MP |
66 | #include "umask-util.h" |
67 | #include "user-util.h" | |
663996b3 MS |
68 | |
69 | /* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates | |
70 | * them in the file system. This is intended to be used to create | |
71 | * properly owned directories beneath /tmp, /var/tmp, /run, which are | |
72 | * volatile and hence need to be recreated on bootup. */ | |
73 | ||
6e866b33 MB |
74 | typedef enum OperationMask { |
75 | OPERATION_CREATE = 1 << 0, | |
76 | OPERATION_REMOVE = 1 << 1, | |
77 | OPERATION_CLEAN = 1 << 2, | |
78 | } OperationMask; | |
79 | ||
663996b3 MS |
80 | typedef enum ItemType { |
81 | /* These ones take file names */ | |
82 | CREATE_FILE = 'f', | |
e1f67bc7 | 83 | TRUNCATE_FILE = 'F', /* deprecated: use f+ */ |
663996b3 MS |
84 | CREATE_DIRECTORY = 'd', |
85 | TRUNCATE_DIRECTORY = 'D', | |
e735f4d4 | 86 | CREATE_SUBVOLUME = 'v', |
db2df898 MP |
87 | CREATE_SUBVOLUME_INHERIT_QUOTA = 'q', |
88 | CREATE_SUBVOLUME_NEW_QUOTA = 'Q', | |
663996b3 MS |
89 | CREATE_FIFO = 'p', |
90 | CREATE_SYMLINK = 'L', | |
91 | CREATE_CHAR_DEVICE = 'c', | |
92 | CREATE_BLOCK_DEVICE = 'b', | |
60f067b4 | 93 | COPY_FILES = 'C', |
663996b3 MS |
94 | |
95 | /* These ones take globs */ | |
e3bff60a | 96 | WRITE_FILE = 'w', |
aa27b158 | 97 | EMPTY_DIRECTORY = 'e', |
e735f4d4 MP |
98 | SET_XATTR = 't', |
99 | RECURSIVE_SET_XATTR = 'T', | |
100 | SET_ACL = 'a', | |
101 | RECURSIVE_SET_ACL = 'A', | |
e3bff60a MP |
102 | SET_ATTRIBUTE = 'h', |
103 | RECURSIVE_SET_ATTRIBUTE = 'H', | |
663996b3 MS |
104 | IGNORE_PATH = 'x', |
105 | IGNORE_DIRECTORY_PATH = 'X', | |
106 | REMOVE_PATH = 'r', | |
107 | RECURSIVE_REMOVE_PATH = 'R', | |
108 | RELABEL_PATH = 'z', | |
60f067b4 | 109 | RECURSIVE_RELABEL_PATH = 'Z', |
e3bff60a | 110 | ADJUST_MODE = 'm', /* legacy, 'z' is identical to this */ |
663996b3 MS |
111 | } ItemType; |
112 | ||
8b3d4ff0 MB |
113 | typedef enum AgeBy { |
114 | AGE_BY_ATIME = 1 << 0, | |
115 | AGE_BY_BTIME = 1 << 1, | |
116 | AGE_BY_CTIME = 1 << 2, | |
117 | AGE_BY_MTIME = 1 << 3, | |
118 | ||
119 | /* All file timestamp types are checked by default. */ | |
120 | AGE_BY_DEFAULT_FILE = AGE_BY_ATIME | AGE_BY_BTIME | AGE_BY_CTIME | AGE_BY_MTIME, | |
121 | AGE_BY_DEFAULT_DIR = AGE_BY_ATIME | AGE_BY_BTIME | AGE_BY_MTIME | |
122 | } AgeBy; | |
123 | ||
663996b3 MS |
124 | typedef struct Item { |
125 | ItemType type; | |
126 | ||
127 | char *path; | |
128 | char *argument; | |
f47781d8 | 129 | char **xattrs; |
f5e65279 | 130 | #if HAVE_ACL |
e735f4d4 MP |
131 | acl_t acl_access; |
132 | acl_t acl_default; | |
133 | #endif | |
663996b3 MS |
134 | uid_t uid; |
135 | gid_t gid; | |
136 | mode_t mode; | |
137 | usec_t age; | |
8b3d4ff0 | 138 | AgeBy age_by_file, age_by_dir; |
663996b3 MS |
139 | |
140 | dev_t major_minor; | |
e3bff60a MP |
141 | unsigned attribute_value; |
142 | unsigned attribute_mask; | |
663996b3 MS |
143 | |
144 | bool uid_set:1; | |
145 | bool gid_set:1; | |
146 | bool mode_set:1; | |
147 | bool age_set:1; | |
60f067b4 | 148 | bool mask_perms:1; |
e3bff60a | 149 | bool attribute_set:1; |
663996b3 MS |
150 | |
151 | bool keep_first_level:1; | |
663996b3 | 152 | |
e1f67bc7 | 153 | bool append_or_force:1; |
e842803a | 154 | |
6e866b33 MB |
155 | bool allow_failure:1; |
156 | ||
8b3d4ff0 MB |
157 | bool try_replace:1; |
158 | ||
6e866b33 | 159 | OperationMask done; |
60f067b4 | 160 | } Item; |
663996b3 | 161 | |
e735f4d4 MP |
162 | typedef struct ItemArray { |
163 | Item *items; | |
6e866b33 | 164 | size_t n_items; |
6e866b33 MB |
165 | |
166 | struct ItemArray *parent; | |
167 | Set *children; | |
e735f4d4 MP |
168 | } ItemArray; |
169 | ||
52ad194e | 170 | typedef enum DirectoryType { |
6e866b33 | 171 | DIRECTORY_RUNTIME, |
52ad194e MB |
172 | DIRECTORY_STATE, |
173 | DIRECTORY_CACHE, | |
174 | DIRECTORY_LOGS, | |
175 | _DIRECTORY_TYPE_MAX, | |
176 | } DirectoryType; | |
177 | ||
b012e921 | 178 | static bool arg_cat_config = false; |
52ad194e | 179 | static bool arg_user = false; |
6e866b33 | 180 | static OperationMask arg_operation = 0; |
60f067b4 | 181 | static bool arg_boot = false; |
6e866b33 | 182 | static PagerFlags arg_pager_flags = 0; |
663996b3 | 183 | |
60f067b4 JS |
184 | static char **arg_include_prefixes = NULL; |
185 | static char **arg_exclude_prefixes = NULL; | |
186 | static char *arg_root = NULL; | |
a032b68d | 187 | static char *arg_image = NULL; |
98393f85 | 188 | static char *arg_replace = NULL; |
663996b3 | 189 | |
663996b3 MS |
190 | #define MAX_DEPTH 256 |
191 | ||
e3bff60a | 192 | static OrderedHashmap *items = NULL, *globs = NULL; |
60f067b4 JS |
193 | static Set *unix_sockets = NULL; |
194 | ||
6e866b33 MB |
195 | STATIC_DESTRUCTOR_REGISTER(items, ordered_hashmap_freep); |
196 | STATIC_DESTRUCTOR_REGISTER(globs, ordered_hashmap_freep); | |
197 | STATIC_DESTRUCTOR_REGISTER(unix_sockets, set_free_freep); | |
198 | STATIC_DESTRUCTOR_REGISTER(arg_include_prefixes, freep); | |
199 | STATIC_DESTRUCTOR_REGISTER(arg_exclude_prefixes, freep); | |
200 | STATIC_DESTRUCTOR_REGISTER(arg_root, freep); | |
a032b68d | 201 | STATIC_DESTRUCTOR_REGISTER(arg_image, freep); |
6e866b33 MB |
202 | |
203 | static int specifier_machine_id_safe(char specifier, const void *data, const void *userdata, char **ret); | |
204 | static int specifier_directory(char specifier, const void *data, const void *userdata, char **ret); | |
52ad194e | 205 | |
e3bff60a | 206 | static const Specifier specifier_table[] = { |
9e294e28 | 207 | { 'a', specifier_architecture, NULL }, |
52ad194e | 208 | { 'b', specifier_boot_id, NULL }, |
9e294e28 | 209 | { 'B', specifier_os_build_id, NULL }, |
52ad194e | 210 | { 'H', specifier_host_name, NULL }, |
a10f5d05 | 211 | { 'l', specifier_short_host_name, NULL }, |
9e294e28 | 212 | { 'm', specifier_machine_id_safe, NULL }, |
a10f5d05 | 213 | { 'o', specifier_os_id, NULL }, |
9e294e28 | 214 | { 'v', specifier_kernel_release, NULL }, |
a10f5d05 | 215 | { 'w', specifier_os_version_id, NULL }, |
a10f5d05 | 216 | { 'W', specifier_os_variant_id, NULL }, |
52ad194e | 217 | |
52ad194e | 218 | { 'h', specifier_user_home, NULL }, |
b012e921 | 219 | |
52ad194e MB |
220 | { 'C', specifier_directory, UINT_TO_PTR(DIRECTORY_CACHE) }, |
221 | { 'L', specifier_directory, UINT_TO_PTR(DIRECTORY_LOGS) }, | |
9e294e28 MB |
222 | { 'S', specifier_directory, UINT_TO_PTR(DIRECTORY_STATE) }, |
223 | { 't', specifier_directory, UINT_TO_PTR(DIRECTORY_RUNTIME) }, | |
224 | ||
225 | COMMON_CREDS_SPECIFIERS, | |
226 | ||
227 | COMMON_TMP_SPECIFIERS, | |
e3bff60a MP |
228 | {} |
229 | }; | |
230 | ||
6e866b33 | 231 | static int specifier_machine_id_safe(char specifier, const void *data, const void *userdata, char **ret) { |
52ad194e MB |
232 | int r; |
233 | ||
1d42b86d MB |
234 | /* If /etc/machine_id is missing or empty (e.g. in a chroot environment) |
235 | * return a recognizable error so that the caller can skip the rule | |
52ad194e MB |
236 | * gracefully. */ |
237 | ||
238 | r = specifier_machine_id(specifier, data, userdata, ret); | |
1d42b86d | 239 | if (IN_SET(r, -ENOENT, -ENOMEDIUM)) |
52ad194e MB |
240 | return -ENXIO; |
241 | ||
242 | return r; | |
243 | } | |
244 | ||
6e866b33 | 245 | static int specifier_directory(char specifier, const void *data, const void *userdata, char **ret) { |
52ad194e MB |
246 | struct table_entry { |
247 | uint64_t type; | |
248 | const char *suffix; | |
249 | }; | |
250 | ||
251 | static const struct table_entry paths_system[] = { | |
252 | [DIRECTORY_RUNTIME] = { SD_PATH_SYSTEM_RUNTIME }, | |
253 | [DIRECTORY_STATE] = { SD_PATH_SYSTEM_STATE_PRIVATE }, | |
254 | [DIRECTORY_CACHE] = { SD_PATH_SYSTEM_STATE_CACHE }, | |
255 | [DIRECTORY_LOGS] = { SD_PATH_SYSTEM_STATE_LOGS }, | |
256 | }; | |
257 | ||
258 | static const struct table_entry paths_user[] = { | |
259 | [DIRECTORY_RUNTIME] = { SD_PATH_USER_RUNTIME }, | |
260 | [DIRECTORY_STATE] = { SD_PATH_USER_CONFIGURATION }, | |
261 | [DIRECTORY_CACHE] = { SD_PATH_USER_STATE_CACHE }, | |
262 | [DIRECTORY_LOGS] = { SD_PATH_USER_CONFIGURATION, "log" }, | |
263 | }; | |
264 | ||
265 | unsigned i; | |
266 | const struct table_entry *paths; | |
267 | ||
268 | assert_cc(ELEMENTSOF(paths_system) == ELEMENTSOF(paths_user)); | |
269 | paths = arg_user ? paths_user : paths_system; | |
270 | ||
271 | i = PTR_TO_UINT(data); | |
272 | assert(i < ELEMENTSOF(paths_system)); | |
273 | ||
a10f5d05 | 274 | return sd_path_lookup(paths[i].type, paths[i].suffix, ret); |
52ad194e MB |
275 | } |
276 | ||
277 | static int log_unresolvable_specifier(const char *filename, unsigned line) { | |
278 | static bool notified = false; | |
279 | ||
280 | /* In system mode, this is called when /etc is not fully initialized (e.g. | |
281 | * in a chroot environment) where some specifiers are unresolvable. In user | |
282 | * mode, this is called when some variables are not defined. These cases are | |
283 | * not considered as an error so log at LOG_NOTICE only for the first time | |
284 | * and then downgrade this to LOG_DEBUG for the rest. */ | |
285 | ||
a10f5d05 MB |
286 | log_syntax(NULL, |
287 | notified ? LOG_DEBUG : LOG_NOTICE, | |
288 | filename, line, 0, | |
289 | "Failed to resolve specifier: %s, skipping", | |
290 | arg_user ? "Required $XDG_... variable not defined" : "uninitialized /etc detected"); | |
52ad194e MB |
291 | |
292 | if (!notified) | |
293 | log_notice("All rules containing unresolvable specifiers will be skipped."); | |
294 | ||
295 | notified = true; | |
296 | return 0; | |
297 | } | |
298 | ||
299 | static int user_config_paths(char*** ret) { | |
300 | _cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL; | |
301 | _cleanup_free_ char *persistent_config = NULL, *runtime_config = NULL, *data_home = NULL; | |
302 | _cleanup_strv_free_ char **res = NULL; | |
303 | int r; | |
304 | ||
305 | r = xdg_user_dirs(&config_dirs, &data_dirs); | |
306 | if (r < 0) | |
307 | return r; | |
308 | ||
309 | r = xdg_user_config_dir(&persistent_config, "/user-tmpfiles.d"); | |
310 | if (r < 0 && r != -ENXIO) | |
311 | return r; | |
312 | ||
313 | r = xdg_user_runtime_dir(&runtime_config, "/user-tmpfiles.d"); | |
314 | if (r < 0 && r != -ENXIO) | |
315 | return r; | |
316 | ||
317 | r = xdg_user_data_dir(&data_home, "/user-tmpfiles.d"); | |
318 | if (r < 0 && r != -ENXIO) | |
319 | return r; | |
320 | ||
321 | r = strv_extend_strv_concat(&res, config_dirs, "/user-tmpfiles.d"); | |
322 | if (r < 0) | |
323 | return r; | |
324 | ||
325 | r = strv_extend(&res, persistent_config); | |
326 | if (r < 0) | |
327 | return r; | |
328 | ||
329 | r = strv_extend(&res, runtime_config); | |
330 | if (r < 0) | |
331 | return r; | |
332 | ||
333 | r = strv_extend(&res, data_home); | |
334 | if (r < 0) | |
335 | return r; | |
336 | ||
337 | r = strv_extend_strv_concat(&res, data_dirs, "/user-tmpfiles.d"); | |
338 | if (r < 0) | |
339 | return r; | |
340 | ||
341 | r = path_strv_make_absolute_cwd(res); | |
342 | if (r < 0) | |
343 | return r; | |
344 | ||
b012e921 | 345 | *ret = TAKE_PTR(res); |
52ad194e MB |
346 | return 0; |
347 | } | |
348 | ||
663996b3 | 349 | static bool needs_glob(ItemType t) { |
60f067b4 JS |
350 | return IN_SET(t, |
351 | WRITE_FILE, | |
352 | IGNORE_PATH, | |
353 | IGNORE_DIRECTORY_PATH, | |
354 | REMOVE_PATH, | |
355 | RECURSIVE_REMOVE_PATH, | |
aa27b158 | 356 | EMPTY_DIRECTORY, |
60f067b4 JS |
357 | ADJUST_MODE, |
358 | RELABEL_PATH, | |
e735f4d4 MP |
359 | RECURSIVE_RELABEL_PATH, |
360 | SET_XATTR, | |
361 | RECURSIVE_SET_XATTR, | |
362 | SET_ACL, | |
e3bff60a MP |
363 | RECURSIVE_SET_ACL, |
364 | SET_ATTRIBUTE, | |
365 | RECURSIVE_SET_ATTRIBUTE); | |
e735f4d4 MP |
366 | } |
367 | ||
368 | static bool takes_ownership(ItemType t) { | |
369 | return IN_SET(t, | |
370 | CREATE_FILE, | |
371 | TRUNCATE_FILE, | |
372 | CREATE_DIRECTORY, | |
aa27b158 | 373 | EMPTY_DIRECTORY, |
e735f4d4 MP |
374 | TRUNCATE_DIRECTORY, |
375 | CREATE_SUBVOLUME, | |
db2df898 MP |
376 | CREATE_SUBVOLUME_INHERIT_QUOTA, |
377 | CREATE_SUBVOLUME_NEW_QUOTA, | |
e735f4d4 MP |
378 | CREATE_FIFO, |
379 | CREATE_SYMLINK, | |
380 | CREATE_CHAR_DEVICE, | |
381 | CREATE_BLOCK_DEVICE, | |
382 | COPY_FILES, | |
e735f4d4 MP |
383 | WRITE_FILE, |
384 | IGNORE_PATH, | |
385 | IGNORE_DIRECTORY_PATH, | |
386 | REMOVE_PATH, | |
387 | RECURSIVE_REMOVE_PATH); | |
663996b3 MS |
388 | } |
389 | ||
e3bff60a | 390 | static struct Item* find_glob(OrderedHashmap *h, const char *match) { |
e735f4d4 | 391 | ItemArray *j; |
663996b3 | 392 | |
a032b68d | 393 | ORDERED_HASHMAP_FOREACH(j, h) { |
6e866b33 | 394 | size_t n; |
e735f4d4 | 395 | |
6e866b33 | 396 | for (n = 0; n < j->n_items; n++) { |
e735f4d4 MP |
397 | Item *item = j->items + n; |
398 | ||
399 | if (fnmatch(item->path, match, FNM_PATHNAME|FNM_PERIOD) == 0) | |
400 | return item; | |
401 | } | |
402 | } | |
663996b3 MS |
403 | |
404 | return NULL; | |
405 | } | |
406 | ||
6e866b33 MB |
407 | static int load_unix_sockets(void) { |
408 | _cleanup_set_free_free_ Set *sockets = NULL; | |
663996b3 | 409 | _cleanup_fclose_ FILE *f = NULL; |
1d42b86d | 410 | int r; |
663996b3 MS |
411 | |
412 | if (unix_sockets) | |
6e866b33 | 413 | return 0; |
663996b3 | 414 | |
1d42b86d | 415 | /* We maintain a cache of the sockets we found in /proc/net/unix to speed things up a little. */ |
663996b3 | 416 | |
6e866b33 MB |
417 | sockets = set_new(&path_hash_ops); |
418 | if (!sockets) | |
419 | return log_oom(); | |
663996b3 MS |
420 | |
421 | f = fopen("/proc/net/unix", "re"); | |
6e866b33 MB |
422 | if (!f) |
423 | return log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno, | |
424 | "Failed to open /proc/net/unix, ignoring: %m"); | |
663996b3 MS |
425 | |
426 | /* Skip header */ | |
1d42b86d | 427 | r = read_line(f, LONG_LINE_MAX, NULL); |
6e866b33 MB |
428 | if (r < 0) |
429 | return log_warning_errno(r, "Failed to skip /proc/net/unix header line: %m"); | |
430 | if (r == 0) | |
431 | return log_warning_errno(SYNTHETIC_ERRNO(EIO), "Premature end of file reading /proc/net/unix."); | |
663996b3 MS |
432 | |
433 | for (;;) { | |
6e866b33 MB |
434 | _cleanup_free_ char *line = NULL, *s = NULL; |
435 | char *p; | |
663996b3 | 436 | |
1d42b86d | 437 | r = read_line(f, LONG_LINE_MAX, &line); |
6e866b33 MB |
438 | if (r < 0) |
439 | return log_warning_errno(r, "Failed to read /proc/net/unix line, ignoring: %m"); | |
1d42b86d | 440 | if (r == 0) /* EOF */ |
663996b3 MS |
441 | break; |
442 | ||
663996b3 MS |
443 | p = strchr(line, ':'); |
444 | if (!p) | |
445 | continue; | |
446 | ||
447 | if (strlen(p) < 37) | |
448 | continue; | |
449 | ||
450 | p += 37; | |
451 | p += strspn(p, WHITESPACE); | |
452 | p += strcspn(p, WHITESPACE); /* skip one more word */ | |
453 | p += strspn(p, WHITESPACE); | |
454 | ||
455 | if (*p != '/') | |
456 | continue; | |
457 | ||
458 | s = strdup(p); | |
6e866b33 MB |
459 | if (!s) |
460 | return log_oom(); | |
663996b3 | 461 | |
8b3d4ff0 | 462 | path_simplify(s); |
663996b3 | 463 | |
6e866b33 MB |
464 | r = set_consume(sockets, s); |
465 | if (r == -EEXIST) | |
466 | continue; | |
467 | if (r < 0) | |
468 | return log_warning_errno(r, "Failed to add AF_UNIX socket to set, ignoring: %m"); | |
663996b3 | 469 | |
6e866b33 MB |
470 | TAKE_PTR(s); |
471 | } | |
663996b3 | 472 | |
6e866b33 MB |
473 | unix_sockets = TAKE_PTR(sockets); |
474 | return 1; | |
663996b3 MS |
475 | } |
476 | ||
477 | static bool unix_socket_alive(const char *fn) { | |
478 | assert(fn); | |
479 | ||
6e866b33 MB |
480 | if (load_unix_sockets() < 0) |
481 | return true; /* We don't know, so assume yes */ | |
663996b3 | 482 | |
6e866b33 | 483 | return !!set_get(unix_sockets, (char*) fn); |
663996b3 MS |
484 | } |
485 | ||
e735f4d4 MP |
486 | static DIR* xopendirat_nomod(int dirfd, const char *path) { |
487 | DIR *dir; | |
488 | ||
489 | dir = xopendirat(dirfd, path, O_NOFOLLOW|O_NOATIME); | |
e3bff60a MP |
490 | if (dir) |
491 | return dir; | |
492 | ||
493 | log_debug_errno(errno, "Cannot open %sdirectory \"%s\": %m", dirfd == AT_FDCWD ? "" : "sub", path); | |
494 | if (errno != EPERM) | |
495 | return NULL; | |
496 | ||
497 | dir = xopendirat(dirfd, path, O_NOFOLLOW); | |
498 | if (!dir) | |
499 | log_debug_errno(errno, "Cannot open %sdirectory \"%s\": %m", dirfd == AT_FDCWD ? "" : "sub", path); | |
e735f4d4 MP |
500 | |
501 | return dir; | |
502 | } | |
503 | ||
504 | static DIR* opendir_nomod(const char *path) { | |
505 | return xopendirat_nomod(AT_FDCWD, path); | |
506 | } | |
507 | ||
a032b68d MB |
508 | static inline nsec_t load_statx_timestamp_nsec(const struct statx_timestamp *ts) { |
509 | assert(ts); | |
510 | ||
511 | if (ts->tv_sec < 0) | |
512 | return NSEC_INFINITY; | |
513 | ||
514 | if ((nsec_t) ts->tv_sec >= (UINT64_MAX - ts->tv_nsec) / NSEC_PER_SEC) | |
515 | return NSEC_INFINITY; | |
516 | ||
517 | return ts->tv_sec * NSEC_PER_SEC + ts->tv_nsec; | |
518 | } | |
519 | ||
8b3d4ff0 MB |
520 | static bool needs_cleanup( |
521 | nsec_t atime, | |
522 | nsec_t btime, | |
523 | nsec_t ctime, | |
524 | nsec_t mtime, | |
525 | nsec_t cutoff, | |
526 | const char *sub_path, | |
527 | AgeBy age_by, | |
528 | bool is_dir) { | |
529 | ||
530 | if (FLAGS_SET(age_by, AGE_BY_MTIME) && mtime != NSEC_INFINITY && mtime >= cutoff) { | |
531 | char a[FORMAT_TIMESTAMP_MAX]; | |
532 | /* Follows spelling in stat(1). */ | |
533 | log_debug("%s \"%s\": modify time %s is too new.", | |
534 | is_dir ? "Directory" : "File", | |
535 | sub_path, | |
536 | format_timestamp_style(a, sizeof(a), mtime / NSEC_PER_USEC, TIMESTAMP_US)); | |
537 | ||
538 | return false; | |
539 | } | |
540 | ||
541 | if (FLAGS_SET(age_by, AGE_BY_ATIME) && atime != NSEC_INFINITY && atime >= cutoff) { | |
542 | char a[FORMAT_TIMESTAMP_MAX]; | |
543 | log_debug("%s \"%s\": access time %s is too new.", | |
544 | is_dir ? "Directory" : "File", | |
545 | sub_path, | |
546 | format_timestamp_style(a, sizeof(a), atime / NSEC_PER_USEC, TIMESTAMP_US)); | |
547 | ||
548 | return false; | |
549 | } | |
550 | ||
551 | /* | |
552 | * Note: Unless explicitly specified by the user, "ctime" is ignored | |
553 | * by default for directories, because we change it when deleting. | |
554 | */ | |
555 | if (FLAGS_SET(age_by, AGE_BY_CTIME) && ctime != NSEC_INFINITY && ctime >= cutoff) { | |
556 | char a[FORMAT_TIMESTAMP_MAX]; | |
557 | log_debug("%s \"%s\": change time %s is too new.", | |
558 | is_dir ? "Directory" : "File", | |
559 | sub_path, | |
560 | format_timestamp_style(a, sizeof(a), ctime / NSEC_PER_USEC, TIMESTAMP_US)); | |
561 | ||
562 | return false; | |
563 | } | |
564 | ||
565 | if (FLAGS_SET(age_by, AGE_BY_BTIME) && btime != NSEC_INFINITY && btime >= cutoff) { | |
566 | char a[FORMAT_TIMESTAMP_MAX]; | |
567 | log_debug("%s \"%s\": birth time %s is too new.", | |
568 | is_dir ? "Directory" : "File", | |
569 | sub_path, | |
570 | format_timestamp_style(a, sizeof(a), btime / NSEC_PER_USEC, TIMESTAMP_US)); | |
571 | ||
572 | return false; | |
573 | } | |
574 | ||
575 | return true; | |
576 | } | |
577 | ||
663996b3 MS |
578 | static int dir_cleanup( |
579 | Item *i, | |
580 | const char *p, | |
581 | DIR *d, | |
a032b68d MB |
582 | nsec_t self_atime_nsec, |
583 | nsec_t self_mtime_nsec, | |
584 | nsec_t cutoff_nsec, | |
585 | dev_t rootdev_major, | |
586 | dev_t rootdev_minor, | |
663996b3 MS |
587 | bool mountpoint, |
588 | int maxdepth, | |
8b3d4ff0 MB |
589 | bool keep_this_level, |
590 | AgeBy age_by_file, | |
591 | AgeBy age_by_dir) { | |
14228c0d | 592 | |
663996b3 | 593 | bool deleted = false; |
a032b68d | 594 | struct dirent *dent; |
663996b3 MS |
595 | int r = 0; |
596 | ||
2897b343 | 597 | FOREACH_DIRENT_ALL(dent, d, break) { |
663996b3 | 598 | _cleanup_free_ char *sub_path = NULL; |
a032b68d | 599 | nsec_t atime_nsec, mtime_nsec, ctime_nsec, btime_nsec; |
663996b3 | 600 | |
2897b343 | 601 | if (dot_or_dot_dot(dent->d_name)) |
663996b3 MS |
602 | continue; |
603 | ||
a032b68d MB |
604 | /* If statx() is supported, use it. It's preferable over fstatat() since it tells us |
605 | * explicitly where we are looking at a mount point, for free as side information. Determining | |
606 | * the same information without statx() is hard, see the complexity of path_is_mount_point(), | |
607 | * and also much slower as it requires a number of syscalls instead of just one. Hence, when | |
608 | * we have modern statx() we use it instead of fstat() and do proper mount point checks, | |
609 | * while on older kernels's well do traditional st_dev based detection of mount points. | |
610 | * | |
611 | * Using statx() for detecting mount points also has the benfit that we handle weird file | |
612 | * systems such as overlayfs better where each file is originating from a different | |
613 | * st_dev. */ | |
614 | ||
615 | STRUCT_STATX_DEFINE(sx); | |
616 | ||
617 | r = statx_fallback( | |
618 | dirfd(d), dent->d_name, | |
619 | AT_SYMLINK_NOFOLLOW|AT_NO_AUTOMOUNT, | |
620 | STATX_TYPE|STATX_MODE|STATX_UID|STATX_ATIME|STATX_MTIME|STATX_CTIME|STATX_BTIME, | |
621 | &sx); | |
622 | if (r == -ENOENT) | |
623 | continue; | |
624 | if (r < 0) { | |
60f067b4 | 625 | /* FUSE, NFS mounts, SELinux might return EACCES */ |
1d42b86d | 626 | r = log_full_errno(errno == EACCES ? LOG_DEBUG : LOG_ERR, errno, |
a032b68d | 627 | "statx(%s/%s) failed: %m", p, dent->d_name); |
663996b3 MS |
628 | continue; |
629 | } | |
630 | ||
a032b68d MB |
631 | if (FLAGS_SET(sx.stx_attributes_mask, STATX_ATTR_MOUNT_ROOT)) { |
632 | /* Yay, we have the mount point API, use it */ | |
633 | if (FLAGS_SET(sx.stx_attributes, STATX_ATTR_MOUNT_ROOT)) { | |
634 | log_debug("Ignoring \"%s/%s\": different mount points.", p, dent->d_name); | |
635 | continue; | |
636 | } | |
637 | } else { | |
638 | /* So we might have statx() but the STATX_ATTR_MOUNT_ROOT flag is not supported, fall | |
639 | * back to traditional stx_dev checking. */ | |
640 | if (sx.stx_dev_major != rootdev_major || | |
641 | sx.stx_dev_minor != rootdev_minor) { | |
642 | log_debug("Ignoring \"%s/%s\": different filesystem.", p, dent->d_name); | |
f2dec872 BR |
643 | continue; |
644 | } | |
a032b68d MB |
645 | |
646 | /* Try to detect bind mounts of the same filesystem instance; they do not differ in device | |
647 | * major/minors. This type of query is not supported on all kernels or filesystem types | |
648 | * though. */ | |
649 | if (S_ISDIR(sx.stx_mode)) { | |
650 | int q; | |
651 | ||
652 | q = fd_is_mount_point(dirfd(d), dent->d_name, 0); | |
653 | if (q < 0) | |
654 | log_debug_errno(q, "Failed to determine whether \"%s/%s\" is a mount point, ignoring: %m", p, dent->d_name); | |
655 | else if (q > 0) { | |
656 | log_debug("Ignoring \"%s/%s\": different mount of the same filesystem.", p, dent->d_name); | |
657 | continue; | |
658 | } | |
659 | } | |
e735f4d4 | 660 | } |
663996b3 | 661 | |
a032b68d MB |
662 | atime_nsec = FLAGS_SET(sx.stx_mask, STATX_ATIME) ? load_statx_timestamp_nsec(&sx.stx_atime) : 0; |
663 | mtime_nsec = FLAGS_SET(sx.stx_mask, STATX_MTIME) ? load_statx_timestamp_nsec(&sx.stx_mtime) : 0; | |
664 | ctime_nsec = FLAGS_SET(sx.stx_mask, STATX_CTIME) ? load_statx_timestamp_nsec(&sx.stx_ctime) : 0; | |
665 | btime_nsec = FLAGS_SET(sx.stx_mask, STATX_BTIME) ? load_statx_timestamp_nsec(&sx.stx_btime) : 0; | |
666 | ||
f2dec872 | 667 | sub_path = path_join(p, dent->d_name); |
60f067b4 | 668 | if (!sub_path) { |
663996b3 MS |
669 | r = log_oom(); |
670 | goto finish; | |
671 | } | |
672 | ||
673 | /* Is there an item configured for this path? */ | |
e3bff60a | 674 | if (ordered_hashmap_get(items, sub_path)) { |
e735f4d4 | 675 | log_debug("Ignoring \"%s\": a separate entry exists.", sub_path); |
663996b3 | 676 | continue; |
e735f4d4 | 677 | } |
663996b3 | 678 | |
e735f4d4 MP |
679 | if (find_glob(globs, sub_path)) { |
680 | log_debug("Ignoring \"%s\": a separate glob exists.", sub_path); | |
663996b3 | 681 | continue; |
e735f4d4 | 682 | } |
663996b3 | 683 | |
a032b68d | 684 | if (S_ISDIR(sx.stx_mode)) { |
bb4f798a | 685 | _cleanup_closedir_ DIR *sub_dir = NULL; |
663996b3 MS |
686 | |
687 | if (mountpoint && | |
688 | streq(dent->d_name, "lost+found") && | |
a032b68d | 689 | sx.stx_uid == 0) { |
bb4f798a | 690 | log_debug("Ignoring directory \"%s\".", sub_path); |
663996b3 | 691 | continue; |
e735f4d4 | 692 | } |
663996b3 MS |
693 | |
694 | if (maxdepth <= 0) | |
e735f4d4 | 695 | log_warning("Reached max depth on \"%s\".", sub_path); |
663996b3 | 696 | else { |
663996b3 MS |
697 | int q; |
698 | ||
e735f4d4 | 699 | sub_dir = xopendirat_nomod(dirfd(d), dent->d_name); |
60f067b4 | 700 | if (!sub_dir) { |
e735f4d4 | 701 | if (errno != ENOENT) |
bb4f798a | 702 | r = log_warning_errno(errno, "Opening directory \"%s\" failed, ignoring: %m", sub_path); |
663996b3 MS |
703 | |
704 | continue; | |
705 | } | |
706 | ||
bb4f798a MB |
707 | if (flock(dirfd(sub_dir), LOCK_EX|LOCK_NB) < 0) { |
708 | log_debug_errno(errno, "Couldn't acquire shared BSD lock on directory \"%s\", skipping: %m", p); | |
709 | continue; | |
710 | } | |
711 | ||
a032b68d MB |
712 | q = dir_cleanup(i, |
713 | sub_path, sub_dir, | |
714 | atime_nsec, mtime_nsec, cutoff_nsec, | |
715 | rootdev_major, rootdev_minor, | |
8b3d4ff0 MB |
716 | false, maxdepth-1, false, |
717 | age_by_file, age_by_dir); | |
663996b3 MS |
718 | if (q < 0) |
719 | r = q; | |
720 | } | |
721 | ||
bb4f798a MB |
722 | /* Note: if you are wondering why we don't support the sticky bit for excluding |
723 | * directories from cleaning like we do it for other file system objects: well, the | |
724 | * sticky bit already has a meaning for directories, so we don't want to overload | |
725 | * that. */ | |
663996b3 | 726 | |
e735f4d4 | 727 | if (keep_this_level) { |
bb4f798a | 728 | log_debug("Keeping directory \"%s\".", sub_path); |
663996b3 | 729 | continue; |
e735f4d4 | 730 | } |
663996b3 | 731 | |
8b3d4ff0 MB |
732 | /* |
733 | * Check the file timestamps of an entry against the | |
734 | * given cutoff time; delete if it is older. | |
735 | */ | |
736 | if (!needs_cleanup(atime_nsec, btime_nsec, ctime_nsec, mtime_nsec, | |
737 | cutoff_nsec, sub_path, age_by_dir, true)) | |
663996b3 | 738 | continue; |
663996b3 | 739 | |
e735f4d4 MP |
740 | log_debug("Removing directory \"%s\".", sub_path); |
741 | if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0) | |
1d42b86d | 742 | if (!IN_SET(errno, ENOENT, ENOTEMPTY)) |
bb4f798a | 743 | r = log_warning_errno(errno, "Failed to remove directory \"%s\", ignoring: %m", sub_path); |
663996b3 MS |
744 | |
745 | } else { | |
bb4f798a MB |
746 | /* Skip files for which the sticky bit is set. These are semantics we define, and are |
747 | * unknown elsewhere. See XDG_RUNTIME_DIR specification for details. */ | |
a032b68d | 748 | if (sx.stx_mode & S_ISVTX) { |
e735f4d4 | 749 | log_debug("Skipping \"%s\": sticky bit set.", sub_path); |
663996b3 | 750 | continue; |
e735f4d4 | 751 | } |
663996b3 | 752 | |
f2dec872 | 753 | if (mountpoint && |
a032b68d MB |
754 | S_ISREG(sx.stx_mode) && |
755 | sx.stx_uid == 0 && | |
f2dec872 BR |
756 | STR_IN_SET(dent->d_name, |
757 | ".journal", | |
758 | "aquota.user", | |
759 | "aquota.group")) { | |
760 | log_debug("Skipping \"%s\".", sub_path); | |
761 | continue; | |
762 | } | |
663996b3 MS |
763 | |
764 | /* Ignore sockets that are listed in /proc/net/unix */ | |
a032b68d | 765 | if (S_ISSOCK(sx.stx_mode) && unix_socket_alive(sub_path)) { |
e735f4d4 | 766 | log_debug("Skipping \"%s\": live socket.", sub_path); |
663996b3 | 767 | continue; |
e735f4d4 | 768 | } |
663996b3 MS |
769 | |
770 | /* Ignore device nodes */ | |
a032b68d | 771 | if (S_ISCHR(sx.stx_mode) || S_ISBLK(sx.stx_mode)) { |
e735f4d4 | 772 | log_debug("Skipping \"%s\": a device.", sub_path); |
663996b3 | 773 | continue; |
e735f4d4 | 774 | } |
663996b3 | 775 | |
bb4f798a | 776 | /* Keep files on this level around if this is requested */ |
e735f4d4 MP |
777 | if (keep_this_level) { |
778 | log_debug("Keeping \"%s\".", sub_path); | |
663996b3 | 779 | continue; |
e735f4d4 | 780 | } |
663996b3 | 781 | |
8b3d4ff0 MB |
782 | if (!needs_cleanup(atime_nsec, btime_nsec, ctime_nsec, mtime_nsec, |
783 | cutoff_nsec, sub_path, age_by_file, false)) | |
e735f4d4 | 784 | continue; |
663996b3 | 785 | |
bb4f798a | 786 | log_debug("Removing \"%s\".", sub_path); |
e735f4d4 MP |
787 | if (unlinkat(dirfd(d), dent->d_name, 0) < 0) |
788 | if (errno != ENOENT) | |
bb4f798a | 789 | r = log_warning_errno(errno, "Failed to remove \"%s\", ignoring: %m", sub_path); |
e735f4d4 | 790 | |
663996b3 MS |
791 | deleted = true; |
792 | } | |
793 | } | |
794 | ||
795 | finish: | |
796 | if (deleted) { | |
a032b68d MB |
797 | char a[FORMAT_TIMESTAMP_MAX], m[FORMAT_TIMESTAMP_MAX]; |
798 | struct timespec ts[2]; | |
bb4f798a | 799 | |
e735f4d4 MP |
800 | log_debug("Restoring access and modification time on \"%s\": %s, %s", |
801 | p, | |
a032b68d MB |
802 | format_timestamp_style(a, sizeof(a), self_atime_nsec / NSEC_PER_USEC, TIMESTAMP_US), |
803 | format_timestamp_style(m, sizeof(m), self_mtime_nsec / NSEC_PER_USEC, TIMESTAMP_US)); | |
804 | ||
805 | timespec_store_nsec(ts + 0, self_atime_nsec); | |
806 | timespec_store_nsec(ts + 1, self_mtime_nsec); | |
bb4f798a MB |
807 | |
808 | /* Restore original directory timestamps */ | |
a032b68d | 809 | if (futimens(dirfd(d), ts) < 0) |
bb4f798a | 810 | log_warning_errno(errno, "Failed to revert timestamps of '%s', ignoring: %m", p); |
663996b3 MS |
811 | } |
812 | ||
813 | return r; | |
814 | } | |
815 | ||
1d42b86d MB |
816 | static bool dangerous_hardlinks(void) { |
817 | _cleanup_free_ char *value = NULL; | |
818 | static int cached = -1; | |
819 | int r; | |
820 | ||
821 | /* Check whether the fs.protected_hardlinks sysctl is on. If we can't determine it we assume its off, as that's | |
822 | * what the upstream default is. */ | |
823 | ||
824 | if (cached >= 0) | |
825 | return cached; | |
826 | ||
827 | r = read_one_line_file("/proc/sys/fs/protected_hardlinks", &value); | |
828 | if (r < 0) { | |
829 | log_debug_errno(r, "Failed to read fs.protected_hardlinks sysctl: %m"); | |
830 | return true; | |
831 | } | |
832 | ||
833 | r = parse_boolean(value); | |
834 | if (r < 0) { | |
835 | log_debug_errno(r, "Failed to parse fs.protected_hardlinks sysctl: %m"); | |
836 | return true; | |
837 | } | |
838 | ||
839 | cached = r == 0; | |
840 | return cached; | |
841 | } | |
842 | ||
98393f85 | 843 | static bool hardlink_vulnerable(const struct stat *st) { |
1d42b86d MB |
844 | assert(st); |
845 | ||
846 | return !S_ISDIR(st->st_mode) && st->st_nlink > 1 && dangerous_hardlinks(); | |
847 | } | |
848 | ||
f2dec872 BR |
849 | static mode_t process_mask_perms(mode_t mode, mode_t current) { |
850 | ||
851 | if ((current & 0111) == 0) | |
852 | mode &= ~0111; | |
853 | if ((current & 0222) == 0) | |
854 | mode &= ~0222; | |
855 | if ((current & 0444) == 0) | |
856 | mode &= ~0444; | |
857 | if (!S_ISDIR(current)) | |
858 | mode &= ~07000; /* remove sticky/sgid/suid bit, unless directory */ | |
859 | ||
860 | return mode; | |
861 | } | |
862 | ||
6e866b33 MB |
863 | static int fd_set_perms(Item *i, int fd, const char *path, const struct stat *st) { |
864 | struct stat stbuf; | |
f2dec872 BR |
865 | mode_t new_mode; |
866 | bool do_chown; | |
7de6915e | 867 | int r; |
5eef597e | 868 | |
60f067b4 | 869 | assert(i); |
98393f85 | 870 | assert(fd); |
6e866b33 | 871 | assert(path); |
60f067b4 | 872 | |
98393f85 MB |
873 | if (!i->mode_set && !i->uid_set && !i->gid_set) |
874 | goto shortcut; | |
e3bff60a | 875 | |
6e866b33 MB |
876 | if (!st) { |
877 | if (fstat(fd, &stbuf) < 0) | |
878 | return log_error_errno(errno, "fstat(%s) failed: %m", path); | |
879 | st = &stbuf; | |
1d42b86d | 880 | } |
e3bff60a | 881 | |
6e866b33 MB |
882 | if (hardlink_vulnerable(st)) |
883 | return log_error_errno(SYNTHETIC_ERRNO(EPERM), | |
884 | "Refusing to set permissions on hardlinked file %s while the fs.protected_hardlinks sysctl is turned off.", | |
885 | path); | |
886 | ||
f2dec872 BR |
887 | /* Do we need a chown()? */ |
888 | do_chown = | |
889 | (i->uid_set && i->uid != st->st_uid) || | |
890 | (i->gid_set && i->gid != st->st_gid); | |
891 | ||
892 | /* Calculate the mode to apply */ | |
893 | new_mode = i->mode_set ? (i->mask_perms ? | |
894 | process_mask_perms(i->mode, st->st_mode) : | |
895 | i->mode) : | |
896 | (st->st_mode & 07777); | |
897 | ||
898 | if (i->mode_set && do_chown) { | |
899 | /* Before we issue the chmod() let's reduce the access mode to the common bits of the old and | |
900 | * the new mode. That way there's no time window where the file exists under the old owner | |
901 | * with more than the old access modes — and not under the new owner with more than the new | |
902 | * access modes either. */ | |
903 | ||
98393f85 | 904 | if (S_ISLNK(st->st_mode)) |
f2dec872 | 905 | log_debug("Skipping temporary mode fix for symlink %s.", path); |
1d42b86d | 906 | else { |
f2dec872 | 907 | mode_t m = new_mode & st->st_mode; /* Mask new mode by old mode */ |
663996b3 | 908 | |
f2dec872 BR |
909 | if (((m ^ st->st_mode) & 07777) == 0) |
910 | log_debug("\"%s\" matches temporary mode %o already.", path, m); | |
e3bff60a | 911 | else { |
f2dec872 | 912 | log_debug("Temporarily changing \"%s\" to mode %o.", path, m); |
7de6915e MB |
913 | r = fchmod_opath(fd, m); |
914 | if (r < 0) | |
915 | return log_error_errno(r, "fchmod() of %s failed: %m", path); | |
e3bff60a | 916 | } |
60f067b4 | 917 | } |
1d42b86d | 918 | } |
663996b3 | 919 | |
f2dec872 | 920 | if (do_chown) { |
1d42b86d MB |
921 | log_debug("Changing \"%s\" to owner "UID_FMT":"GID_FMT, |
922 | path, | |
923 | i->uid_set ? i->uid : UID_INVALID, | |
924 | i->gid_set ? i->gid : GID_INVALID); | |
925 | ||
98393f85 MB |
926 | if (fchownat(fd, |
927 | "", | |
928 | i->uid_set ? i->uid : UID_INVALID, | |
929 | i->gid_set ? i->gid : GID_INVALID, | |
930 | AT_EMPTY_PATH) < 0) | |
931 | return log_error_errno(errno, "fchownat() of %s failed: %m", path); | |
e735f4d4 | 932 | } |
663996b3 | 933 | |
f2dec872 BR |
934 | /* Now, apply the final mode. We do this in two cases: when the user set a mode explicitly, or after a |
935 | * chown(), since chown()'s mangle the access mode in regards to sgid/suid in some conditions. */ | |
936 | if (i->mode_set || do_chown) { | |
937 | if (S_ISLNK(st->st_mode)) | |
938 | log_debug("Skipping mode fix for symlink %s.", path); | |
939 | else { | |
940 | /* Check if the chmod() is unnecessary. Note that if we did a chown() before we always | |
941 | * chmod() here again, since it might have mangled the bits. */ | |
942 | if (!do_chown && ((new_mode ^ st->st_mode) & 07777) == 0) | |
943 | log_debug("\"%s\" matches mode %o already.", path, new_mode); | |
944 | else { | |
945 | log_debug("Changing \"%s\" to mode %o.", path, new_mode); | |
7de6915e MB |
946 | r = fchmod_opath(fd, new_mode); |
947 | if (r < 0) | |
948 | return log_error_errno(r, "fchmod() of %s failed: %m", path); | |
f2dec872 BR |
949 | } |
950 | } | |
951 | } | |
952 | ||
1d42b86d | 953 | shortcut: |
b012e921 | 954 | return label_fix(path, 0); |
663996b3 MS |
955 | } |
956 | ||
6e866b33 MB |
957 | static int path_open_parent_safe(const char *path) { |
958 | _cleanup_free_ char *dn = NULL; | |
e1f67bc7 | 959 | int r, fd; |
6e866b33 MB |
960 | |
961 | if (path_equal(path, "/") || !path_is_normalized(path)) | |
962 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
963 | "Failed to open parent of '%s': invalid path.", | |
964 | path); | |
965 | ||
966 | dn = dirname_malloc(path); | |
967 | if (!dn) | |
968 | return log_oom(); | |
969 | ||
e1f67bc7 MB |
970 | r = chase_symlinks(dn, arg_root, CHASE_SAFE|CHASE_WARN, NULL, &fd); |
971 | if (r < 0 && r != -ENOLINK) | |
972 | return log_error_errno(r, "Failed to validate path %s: %m", path); | |
6e866b33 | 973 | |
e1f67bc7 | 974 | return r < 0 ? r : fd; |
6e866b33 MB |
975 | } |
976 | ||
977 | static int path_open_safe(const char *path) { | |
e1f67bc7 | 978 | int r, fd; |
6e866b33 MB |
979 | |
980 | /* path_open_safe() returns a file descriptor opened with O_PATH after | |
981 | * verifying that the path doesn't contain unsafe transitions, except | |
982 | * for its final component as the function does not follow symlink. */ | |
98393f85 | 983 | |
98393f85 MB |
984 | assert(path); |
985 | ||
6e866b33 MB |
986 | if (!path_is_normalized(path)) |
987 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
988 | "Failed to open invalid path '%s'.", | |
989 | path); | |
98393f85 | 990 | |
e1f67bc7 MB |
991 | r = chase_symlinks(path, arg_root, CHASE_SAFE|CHASE_WARN|CHASE_NOFOLLOW, NULL, &fd); |
992 | if (r < 0 && r != -ENOLINK) | |
993 | return log_error_errno(r, "Failed to validate path %s: %m", path); | |
98393f85 | 994 | |
e1f67bc7 | 995 | return r < 0 ? r : fd; |
6e866b33 | 996 | } |
98393f85 | 997 | |
6e866b33 MB |
998 | static int path_set_perms(Item *i, const char *path) { |
999 | _cleanup_close_ int fd = -1; | |
1000 | ||
1001 | assert(i); | |
1002 | assert(path); | |
98393f85 | 1003 | |
6e866b33 MB |
1004 | fd = path_open_safe(path); |
1005 | if (fd < 0) | |
1006 | return fd; | |
b012e921 | 1007 | |
6e866b33 | 1008 | return fd_set_perms(i, fd, path, NULL); |
98393f85 MB |
1009 | } |
1010 | ||
e3bff60a | 1011 | static int parse_xattrs_from_arg(Item *i) { |
f47781d8 MP |
1012 | const char *p; |
1013 | int r; | |
1014 | ||
1015 | assert(i); | |
e735f4d4 | 1016 | assert(i->argument); |
f47781d8 | 1017 | |
f47781d8 MP |
1018 | p = i->argument; |
1019 | ||
e3bff60a | 1020 | for (;;) { |
52ad194e | 1021 | _cleanup_free_ char *name = NULL, *value = NULL, *xattr = NULL; |
e3bff60a | 1022 | |
f2dec872 | 1023 | r = extract_first_word(&p, &xattr, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE); |
e3bff60a MP |
1024 | if (r < 0) |
1025 | log_warning_errno(r, "Failed to parse extended attribute '%s', ignoring: %m", p); | |
1026 | if (r <= 0) | |
1027 | break; | |
e735f4d4 | 1028 | |
52ad194e | 1029 | r = split_pair(xattr, "=", &name, &value); |
f47781d8 | 1030 | if (r < 0) { |
e3bff60a | 1031 | log_warning_errno(r, "Failed to parse extended attribute, ignoring: %s", xattr); |
f47781d8 MP |
1032 | continue; |
1033 | } | |
e735f4d4 | 1034 | |
e3bff60a MP |
1035 | if (isempty(name) || isempty(value)) { |
1036 | log_warning("Malformed extended attribute found, ignoring: %s", xattr); | |
f47781d8 MP |
1037 | continue; |
1038 | } | |
e735f4d4 | 1039 | |
e3bff60a | 1040 | if (strv_push_pair(&i->xattrs, name, value) < 0) |
f47781d8 | 1041 | return log_oom(); |
e735f4d4 | 1042 | |
e3bff60a | 1043 | name = value = NULL; |
f47781d8 MP |
1044 | } |
1045 | ||
e3bff60a | 1046 | return 0; |
f47781d8 MP |
1047 | } |
1048 | ||
6e866b33 | 1049 | static int fd_set_xattrs(Item *i, int fd, const char *path, const struct stat *st) { |
b012e921 | 1050 | char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)]; |
f47781d8 MP |
1051 | char **name, **value; |
1052 | ||
1053 | assert(i); | |
98393f85 | 1054 | assert(fd); |
6e866b33 | 1055 | assert(path); |
98393f85 MB |
1056 | |
1057 | xsprintf(procfs_path, "/proc/self/fd/%i", fd); | |
f47781d8 | 1058 | |
f47781d8 | 1059 | STRV_FOREACH_PAIR(name, value, i->xattrs) { |
e3bff60a | 1060 | log_debug("Setting extended attribute '%s=%s' on %s.", *name, *value, path); |
98393f85 | 1061 | if (setxattr(procfs_path, *name, *value, strlen(*value), 0) < 0) |
81c58355 MB |
1062 | return log_error_errno(errno, "Setting extended attribute %s=%s on %s failed: %m", |
1063 | *name, *value, path); | |
f47781d8 MP |
1064 | } |
1065 | return 0; | |
1066 | } | |
1067 | ||
98393f85 MB |
1068 | static int path_set_xattrs(Item *i, const char *path) { |
1069 | _cleanup_close_ int fd = -1; | |
1070 | ||
1071 | assert(i); | |
1072 | assert(path); | |
1073 | ||
6e866b33 | 1074 | fd = path_open_safe(path); |
98393f85 | 1075 | if (fd < 0) |
6e866b33 | 1076 | return fd; |
98393f85 | 1077 | |
6e866b33 | 1078 | return fd_set_xattrs(i, fd, path, NULL); |
98393f85 MB |
1079 | } |
1080 | ||
e3bff60a | 1081 | static int parse_acls_from_arg(Item *item) { |
f5e65279 | 1082 | #if HAVE_ACL |
e735f4d4 MP |
1083 | int r; |
1084 | ||
1085 | assert(item); | |
1086 | ||
e1f67bc7 | 1087 | /* If append_or_force (= modify) is set, we will not modify the acl |
e735f4d4 | 1088 | * afterwards, so the mask can be added now if necessary. */ |
e3bff60a | 1089 | |
e1f67bc7 | 1090 | r = parse_acl(item->argument, &item->acl_access, &item->acl_default, !item->append_or_force); |
e735f4d4 | 1091 | if (r < 0) |
e3bff60a | 1092 | log_warning_errno(r, "Failed to parse ACL \"%s\": %m. Ignoring", item->argument); |
e735f4d4 | 1093 | #else |
a032b68d | 1094 | log_warning("ACLs are not supported. Ignoring."); |
e735f4d4 MP |
1095 | #endif |
1096 | ||
1097 | return 0; | |
1098 | } | |
1099 | ||
f5e65279 | 1100 | #if HAVE_ACL |
e3bff60a MP |
1101 | static int path_set_acl(const char *path, const char *pretty, acl_type_t type, acl_t acl, bool modify) { |
1102 | _cleanup_(acl_free_charpp) char *t = NULL; | |
e735f4d4 MP |
1103 | _cleanup_(acl_freep) acl_t dup = NULL; |
1104 | int r; | |
e3bff60a MP |
1105 | |
1106 | /* Returns 0 for success, positive error if already warned, | |
1107 | * negative error otherwise. */ | |
e735f4d4 MP |
1108 | |
1109 | if (modify) { | |
1110 | r = acls_for_file(path, type, acl, &dup); | |
1111 | if (r < 0) | |
1112 | return r; | |
1113 | ||
1114 | r = calc_acl_mask_if_needed(&dup); | |
1115 | if (r < 0) | |
1116 | return r; | |
1117 | } else { | |
1118 | dup = acl_dup(acl); | |
1119 | if (!dup) | |
1120 | return -errno; | |
1121 | ||
1122 | /* the mask was already added earlier if needed */ | |
1123 | } | |
1124 | ||
1125 | r = add_base_acls_if_needed(&dup, path); | |
1126 | if (r < 0) | |
1127 | return r; | |
1128 | ||
1129 | t = acl_to_any_text(dup, NULL, ',', TEXT_ABBREVIATE); | |
e3bff60a | 1130 | log_debug("Setting %s ACL %s on %s.", |
e735f4d4 | 1131 | type == ACL_TYPE_ACCESS ? "access" : "default", |
e3bff60a | 1132 | strna(t), pretty); |
e735f4d4 MP |
1133 | |
1134 | r = acl_set_file(path, type, dup); | |
a032b68d MB |
1135 | if (r < 0) { |
1136 | if (ERRNO_IS_NOT_SUPPORTED(errno)) | |
1137 | /* No error if filesystem doesn't support ACLs. Return negative. */ | |
1138 | return -errno; | |
1139 | else | |
1140 | /* Return positive to indicate we already warned */ | |
1141 | return -log_error_errno(errno, | |
1142 | "Setting %s ACL \"%s\" on %s failed: %m", | |
1143 | type == ACL_TYPE_ACCESS ? "access" : "default", | |
1144 | strna(t), pretty); | |
1145 | } | |
e735f4d4 MP |
1146 | return 0; |
1147 | } | |
1148 | #endif | |
1149 | ||
6e866b33 | 1150 | static int fd_set_acls(Item *item, int fd, const char *path, const struct stat *st) { |
e3bff60a | 1151 | int r = 0; |
f5e65279 | 1152 | #if HAVE_ACL |
b012e921 | 1153 | char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)]; |
6e866b33 | 1154 | struct stat stbuf; |
e735f4d4 MP |
1155 | |
1156 | assert(item); | |
98393f85 | 1157 | assert(fd); |
6e866b33 | 1158 | assert(path); |
e3bff60a | 1159 | |
6e866b33 MB |
1160 | if (!st) { |
1161 | if (fstat(fd, &stbuf) < 0) | |
1162 | return log_error_errno(errno, "fstat(%s) failed: %m", path); | |
1163 | st = &stbuf; | |
1d42b86d MB |
1164 | } |
1165 | ||
6e866b33 MB |
1166 | if (hardlink_vulnerable(st)) |
1167 | return log_error_errno(SYNTHETIC_ERRNO(EPERM), | |
1168 | "Refusing to set ACLs on hardlinked file %s while the fs.protected_hardlinks sysctl is turned off.", | |
1169 | path); | |
1170 | ||
98393f85 | 1171 | if (S_ISLNK(st->st_mode)) { |
e3bff60a MP |
1172 | log_debug("Skipping ACL fix for symlink %s.", path); |
1173 | return 0; | |
e735f4d4 | 1174 | } |
e3bff60a | 1175 | |
98393f85 | 1176 | xsprintf(procfs_path, "/proc/self/fd/%i", fd); |
e3bff60a MP |
1177 | |
1178 | if (item->acl_access) | |
e1f67bc7 | 1179 | r = path_set_acl(procfs_path, path, ACL_TYPE_ACCESS, item->acl_access, item->append_or_force); |
e3bff60a | 1180 | |
6e866b33 MB |
1181 | /* set only default acls to folders */ |
1182 | if (r == 0 && item->acl_default && S_ISDIR(st->st_mode)) | |
e1f67bc7 | 1183 | r = path_set_acl(procfs_path, path, ACL_TYPE_DEFAULT, item->acl_default, item->append_or_force); |
e3bff60a | 1184 | |
a032b68d MB |
1185 | if (ERRNO_IS_NOT_SUPPORTED(r)) { |
1186 | log_debug_errno(r, "ACLs not supported by file system at %s", path); | |
1187 | return 0; | |
1188 | } | |
1189 | ||
e3bff60a MP |
1190 | if (r > 0) |
1191 | return -r; /* already warned */ | |
a10f5d05 MB |
1192 | |
1193 | /* The above procfs paths don't work if /proc is not mounted. */ | |
1194 | if (r == -ENOENT && proc_mounted() == 0) | |
1195 | r = -ENOSYS; | |
1196 | ||
98393f85 MB |
1197 | if (r < 0) |
1198 | return log_error_errno(r, "ACL operation on \"%s\" failed: %m", path); | |
e735f4d4 | 1199 | #endif |
e3bff60a MP |
1200 | return r; |
1201 | } | |
1202 | ||
98393f85 MB |
1203 | static int path_set_acls(Item *item, const char *path) { |
1204 | int r = 0; | |
6e866b33 | 1205 | #if HAVE_ACL |
98393f85 | 1206 | _cleanup_close_ int fd = -1; |
98393f85 MB |
1207 | |
1208 | assert(item); | |
1209 | assert(path); | |
1210 | ||
6e866b33 | 1211 | fd = path_open_safe(path); |
98393f85 | 1212 | if (fd < 0) |
6e866b33 | 1213 | return fd; |
98393f85 | 1214 | |
6e866b33 MB |
1215 | r = fd_set_acls(item, fd, path, NULL); |
1216 | #endif | |
1217 | return r; | |
1218 | } | |
98393f85 | 1219 | |
e3bff60a MP |
1220 | static int parse_attribute_from_arg(Item *item) { |
1221 | ||
1222 | static const struct { | |
1223 | char character; | |
1224 | unsigned value; | |
1225 | } attributes[] = { | |
1226 | { 'A', FS_NOATIME_FL }, /* do not update atime */ | |
1227 | { 'S', FS_SYNC_FL }, /* Synchronous updates */ | |
1228 | { 'D', FS_DIRSYNC_FL }, /* dirsync behaviour (directories only) */ | |
1229 | { 'a', FS_APPEND_FL }, /* writes to file may only append */ | |
1230 | { 'c', FS_COMPR_FL }, /* Compress file */ | |
1231 | { 'd', FS_NODUMP_FL }, /* do not dump file */ | |
5a920b42 | 1232 | { 'e', FS_EXTENT_FL }, /* Extents */ |
e3bff60a MP |
1233 | { 'i', FS_IMMUTABLE_FL }, /* Immutable file */ |
1234 | { 'j', FS_JOURNAL_DATA_FL }, /* Reserved for ext3 */ | |
1235 | { 's', FS_SECRM_FL }, /* Secure deletion */ | |
1236 | { 'u', FS_UNRM_FL }, /* Undelete */ | |
1237 | { 't', FS_NOTAIL_FL }, /* file tail should not be merged */ | |
2897b343 | 1238 | { 'T', FS_TOPDIR_FL }, /* Top of directory hierarchies */ |
e3bff60a | 1239 | { 'C', FS_NOCOW_FL }, /* Do not cow file */ |
bb4f798a | 1240 | { 'P', FS_PROJINHERIT_FL }, /* Inherit the quota project ID */ |
e3bff60a MP |
1241 | }; |
1242 | ||
1243 | enum { | |
1244 | MODE_ADD, | |
1245 | MODE_DEL, | |
1246 | MODE_SET | |
1247 | } mode = MODE_ADD; | |
1248 | ||
1249 | unsigned value = 0, mask = 0; | |
1250 | const char *p; | |
1251 | ||
1252 | assert(item); | |
1253 | ||
1254 | p = item->argument; | |
1255 | if (p) { | |
1256 | if (*p == '+') { | |
1257 | mode = MODE_ADD; | |
1258 | p++; | |
1259 | } else if (*p == '-') { | |
1260 | mode = MODE_DEL; | |
1261 | p++; | |
1262 | } else if (*p == '=') { | |
1263 | mode = MODE_SET; | |
1264 | p++; | |
1265 | } | |
1266 | } | |
1267 | ||
6e866b33 MB |
1268 | if (isempty(p) && mode != MODE_SET) |
1269 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1270 | "Setting file attribute on '%s' needs an attribute specification.", | |
1271 | item->path); | |
e3bff60a MP |
1272 | |
1273 | for (; p && *p ; p++) { | |
1274 | unsigned i, v; | |
1275 | ||
1276 | for (i = 0; i < ELEMENTSOF(attributes); i++) | |
1277 | if (*p == attributes[i].character) | |
1278 | break; | |
1279 | ||
6e866b33 MB |
1280 | if (i >= ELEMENTSOF(attributes)) |
1281 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1282 | "Unknown file attribute '%c' on '%s'.", | |
1283 | *p, item->path); | |
e3bff60a MP |
1284 | |
1285 | v = attributes[i].value; | |
1286 | ||
f5e65279 | 1287 | SET_FLAG(value, v, IN_SET(mode, MODE_ADD, MODE_SET)); |
e3bff60a MP |
1288 | |
1289 | mask |= v; | |
1290 | } | |
1291 | ||
1292 | if (mode == MODE_SET) | |
bb4f798a | 1293 | mask |= CHATTR_ALL_FL; |
e3bff60a MP |
1294 | |
1295 | assert(mask != 0); | |
1296 | ||
1297 | item->attribute_mask = mask; | |
1298 | item->attribute_value = value; | |
1299 | item->attribute_set = true; | |
1300 | ||
1301 | return 0; | |
1302 | } | |
1303 | ||
6e866b33 | 1304 | static int fd_set_attribute(Item *item, int fd, const char *path, const struct stat *st) { |
98393f85 | 1305 | _cleanup_close_ int procfs_fd = -1; |
6e866b33 | 1306 | struct stat stbuf; |
e3bff60a MP |
1307 | unsigned f; |
1308 | int r; | |
1309 | ||
6e866b33 MB |
1310 | assert(item); |
1311 | assert(fd); | |
1312 | assert(path); | |
1313 | ||
e3bff60a MP |
1314 | if (!item->attribute_set || item->attribute_mask == 0) |
1315 | return 0; | |
1316 | ||
6e866b33 MB |
1317 | if (!st) { |
1318 | if (fstat(fd, &stbuf) < 0) | |
1319 | return log_error_errno(errno, "fstat(%s) failed: %m", path); | |
1320 | st = &stbuf; | |
1321 | } | |
e3bff60a MP |
1322 | |
1323 | /* Issuing the file attribute ioctls on device nodes is not | |
1324 | * safe, as that will be delivered to the drivers, not the | |
1325 | * file system containing the device node. */ | |
6e866b33 MB |
1326 | if (!S_ISREG(st->st_mode) && !S_ISDIR(st->st_mode)) |
1327 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1328 | "Setting file flags is only supported on regular files and directories, cannot set on '%s'.", | |
1329 | path); | |
e3bff60a MP |
1330 | |
1331 | f = item->attribute_value & item->attribute_mask; | |
1332 | ||
1333 | /* Mask away directory-specific flags */ | |
98393f85 | 1334 | if (!S_ISDIR(st->st_mode)) |
e3bff60a MP |
1335 | f &= ~FS_DIRSYNC_FL; |
1336 | ||
b012e921 | 1337 | procfs_fd = fd_reopen(fd, O_RDONLY|O_CLOEXEC|O_NOATIME); |
98393f85 | 1338 | if (procfs_fd < 0) |
6e866b33 | 1339 | return log_error_errno(procfs_fd, "Failed to re-open '%s': %m", path); |
98393f85 | 1340 | |
3a6ce677 BR |
1341 | unsigned previous, current; |
1342 | r = chattr_full(NULL, procfs_fd, f, item->attribute_mask, &previous, ¤t, true); | |
1343 | if (r == -ENOANO) | |
1344 | log_warning("Cannot set file attributes for '%s', maybe due to incompatibility in specified attributes, " | |
1345 | "previous=0x%08x, current=0x%08x, expected=0x%08x, ignoring.", | |
1346 | path, previous, current, (previous & ~item->attribute_mask) | (f & item->attribute_mask)); | |
1347 | else if (r < 0) | |
1348 | log_full_errno(ERRNO_IS_NOT_SUPPORTED(r) ? LOG_DEBUG : LOG_WARNING, r, | |
1349 | "Cannot set file attributes for '%s', value=0x%08x, mask=0x%08x, ignoring: %m", | |
7035cd9e | 1350 | path, item->attribute_value, item->attribute_mask); |
e735f4d4 MP |
1351 | |
1352 | return 0; | |
1353 | } | |
1354 | ||
98393f85 MB |
1355 | static int path_set_attribute(Item *item, const char *path) { |
1356 | _cleanup_close_ int fd = -1; | |
98393f85 MB |
1357 | |
1358 | if (!item->attribute_set || item->attribute_mask == 0) | |
1359 | return 0; | |
1360 | ||
6e866b33 | 1361 | fd = path_open_safe(path); |
98393f85 | 1362 | if (fd < 0) |
6e866b33 | 1363 | return fd; |
98393f85 | 1364 | |
6e866b33 | 1365 | return fd_set_attribute(item, fd, path, NULL); |
98393f85 MB |
1366 | } |
1367 | ||
663996b3 | 1368 | static int write_one_file(Item *i, const char *path) { |
6e866b33 MB |
1369 | _cleanup_close_ int fd = -1, dir_fd = -1; |
1370 | char *bn; | |
a10f5d05 | 1371 | int r; |
60f067b4 JS |
1372 | |
1373 | assert(i); | |
1374 | assert(path); | |
6e866b33 MB |
1375 | assert(i->argument); |
1376 | assert(i->type == WRITE_FILE); | |
1377 | ||
1378 | /* Validate the path and keep the fd on the directory for opening the | |
1379 | * file so we're sure that it can't be changed behind our back. */ | |
1380 | dir_fd = path_open_parent_safe(path); | |
1381 | if (dir_fd < 0) | |
1382 | return dir_fd; | |
1383 | ||
1384 | bn = basename(path); | |
1385 | ||
1386 | /* Follows symlinks */ | |
a10f5d05 MB |
1387 | fd = openat(dir_fd, bn, |
1388 | O_NONBLOCK|O_CLOEXEC|O_WRONLY|O_NOCTTY|(i->append_or_force ? O_APPEND : 0), | |
1389 | i->mode); | |
6e866b33 MB |
1390 | if (fd < 0) { |
1391 | if (errno == ENOENT) { | |
1392 | log_debug_errno(errno, "Not writing missing file \"%s\": %m", path); | |
1393 | return 0; | |
1394 | } | |
a10f5d05 MB |
1395 | |
1396 | if (i->allow_failure) | |
1397 | return log_debug_errno(errno, "Failed to open file \"%s\", ignoring: %m", path); | |
1398 | ||
6e866b33 MB |
1399 | return log_error_errno(errno, "Failed to open file \"%s\": %m", path); |
1400 | } | |
663996b3 | 1401 | |
6e866b33 MB |
1402 | /* 'w' is allowed to write into any kind of files. */ |
1403 | log_debug("Writing to \"%s\".", path); | |
1404 | ||
1405 | r = loop_write(fd, i->argument, strlen(i->argument), false); | |
1406 | if (r < 0) | |
1407 | return log_error_errno(r, "Failed to write file \"%s\": %m", path); | |
1408 | ||
1409 | return fd_set_perms(i, fd, path, NULL); | |
1410 | } | |
1411 | ||
1412 | static int create_file(Item *i, const char *path) { | |
1413 | _cleanup_close_ int fd = -1, dir_fd = -1; | |
1414 | struct stat stbuf, *st = NULL; | |
1415 | int r = 0; | |
1416 | char *bn; | |
1417 | ||
1418 | assert(i); | |
1419 | assert(path); | |
1420 | assert(i->type == CREATE_FILE); | |
1421 | ||
1422 | /* 'f' operates on regular files exclusively. */ | |
1423 | ||
1424 | /* Validate the path and keep the fd on the directory for opening the | |
1425 | * file so we're sure that it can't be changed behind our back. */ | |
1426 | dir_fd = path_open_parent_safe(path); | |
1427 | if (dir_fd < 0) | |
1428 | return dir_fd; | |
1429 | ||
1430 | bn = basename(path); | |
663996b3 | 1431 | |
e842803a | 1432 | RUN_WITH_UMASK(0000) { |
5eef597e | 1433 | mac_selinux_create_file_prepare(path, S_IFREG); |
6e866b33 | 1434 | fd = openat(dir_fd, bn, O_CREAT|O_EXCL|O_NOFOLLOW|O_NONBLOCK|O_CLOEXEC|O_WRONLY|O_NOCTTY, i->mode); |
5eef597e | 1435 | mac_selinux_create_file_clear(); |
663996b3 MS |
1436 | } |
1437 | ||
1438 | if (fd < 0) { | |
6e866b33 MB |
1439 | /* Even on a read-only filesystem, open(2) returns EEXIST if the |
1440 | * file already exists. It returns EROFS only if it needs to | |
1441 | * create the file. */ | |
1442 | if (errno != EEXIST) | |
1443 | return log_error_errno(errno, "Failed to create file %s: %m", path); | |
1444 | ||
1445 | /* Re-open the file. At that point it must exist since open(2) | |
1446 | * failed with EEXIST. We still need to check if the perms/mode | |
1447 | * need to be changed. For read-only filesystems, we let | |
1448 | * fd_set_perms() report the error if the perms need to be | |
1449 | * modified. */ | |
1450 | fd = openat(dir_fd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH, i->mode); | |
1451 | if (fd < 0) | |
1452 | return log_error_errno(errno, "Failed to re-open file %s: %m", path); | |
1453 | ||
1454 | if (fstat(fd, &stbuf) < 0) | |
1455 | return log_error_errno(errno, "stat(%s) failed: %m", path); | |
1456 | ||
a032b68d MB |
1457 | if (!S_ISREG(stbuf.st_mode)) |
1458 | return log_error_errno(SYNTHETIC_ERRNO(EEXIST), | |
1459 | "%s exists and is not a regular file.", | |
1460 | path); | |
6e866b33 MB |
1461 | |
1462 | st = &stbuf; | |
1463 | } else { | |
1464 | ||
1465 | log_debug("\"%s\" has been created.", path); | |
1466 | ||
1467 | if (i->argument) { | |
1468 | log_debug("Writing to \"%s\".", path); | |
1469 | ||
1470 | r = loop_write(fd, i->argument, strlen(i->argument), false); | |
1471 | if (r < 0) | |
1472 | return log_error_errno(r, "Failed to write file \"%s\": %m", path); | |
1d42b86d | 1473 | } |
6e866b33 | 1474 | } |
663996b3 | 1475 | |
6e866b33 MB |
1476 | return fd_set_perms(i, fd, path, st); |
1477 | } | |
1478 | ||
1479 | static int truncate_file(Item *i, const char *path) { | |
1480 | _cleanup_close_ int fd = -1, dir_fd = -1; | |
1481 | struct stat stbuf, *st = NULL; | |
1482 | bool erofs = false; | |
1483 | int r = 0; | |
1484 | char *bn; | |
1485 | ||
1486 | assert(i); | |
1487 | assert(path); | |
e1f67bc7 | 1488 | assert(i->type == TRUNCATE_FILE || (i->type == CREATE_FILE && i->append_or_force)); |
6e866b33 MB |
1489 | |
1490 | /* We want to operate on regular file exclusively especially since | |
1491 | * O_TRUNC is unspecified if the file is neither a regular file nor a | |
1492 | * fifo nor a terminal device. Therefore we first open the file and make | |
1493 | * sure it's a regular one before truncating it. */ | |
e3bff60a | 1494 | |
6e866b33 MB |
1495 | /* Validate the path and keep the fd on the directory for opening the |
1496 | * file so we're sure that it can't be changed behind our back. */ | |
1497 | dir_fd = path_open_parent_safe(path); | |
1498 | if (dir_fd < 0) | |
1499 | return dir_fd; | |
1500 | ||
1501 | bn = basename(path); | |
1502 | ||
1503 | RUN_WITH_UMASK(0000) { | |
1504 | mac_selinux_create_file_prepare(path, S_IFREG); | |
1505 | fd = openat(dir_fd, bn, O_CREAT|O_NOFOLLOW|O_NONBLOCK|O_CLOEXEC|O_WRONLY|O_NOCTTY, i->mode); | |
1506 | mac_selinux_create_file_clear(); | |
663996b3 MS |
1507 | } |
1508 | ||
6e866b33 MB |
1509 | if (fd < 0) { |
1510 | if (errno != EROFS) | |
1511 | return log_error_errno(errno, "Failed to open/create file %s: %m", path); | |
663996b3 | 1512 | |
6e866b33 MB |
1513 | /* On a read-only filesystem, we don't want to fail if the |
1514 | * target is already empty and the perms are set. So we still | |
1515 | * proceed with the sanity checks and let the remaining | |
1516 | * operations fail with EROFS if they try to modify the target | |
1517 | * file. */ | |
1518 | ||
1519 | fd = openat(dir_fd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH, i->mode); | |
1520 | if (fd < 0) { | |
a032b68d MB |
1521 | if (errno == ENOENT) |
1522 | return log_error_errno(SYNTHETIC_ERRNO(EROFS), | |
1523 | "Cannot create file %s on a read-only file system.", | |
1524 | path); | |
6e866b33 MB |
1525 | |
1526 | return log_error_errno(errno, "Failed to re-open file %s: %m", path); | |
1527 | } | |
663996b3 | 1528 | |
6e866b33 MB |
1529 | erofs = true; |
1530 | } | |
663996b3 | 1531 | |
6e866b33 | 1532 | if (fstat(fd, &stbuf) < 0) |
f47781d8 | 1533 | return log_error_errno(errno, "stat(%s) failed: %m", path); |
663996b3 | 1534 | |
a032b68d MB |
1535 | if (!S_ISREG(stbuf.st_mode)) |
1536 | return log_error_errno(SYNTHETIC_ERRNO(EEXIST), | |
1537 | "%s exists and is not a regular file.", | |
1538 | path); | |
663996b3 | 1539 | |
6e866b33 MB |
1540 | if (stbuf.st_size > 0) { |
1541 | if (ftruncate(fd, 0) < 0) { | |
1542 | r = erofs ? -EROFS : -errno; | |
1543 | return log_error_errno(r, "Failed to truncate file %s: %m", path); | |
1544 | } | |
1545 | } else | |
1546 | st = &stbuf; | |
1547 | ||
1548 | log_debug("\"%s\" has been created.", path); | |
1549 | ||
1550 | if (i->argument) { | |
1551 | log_debug("Writing to \"%s\".", path); | |
1552 | ||
1553 | r = loop_write(fd, i->argument, strlen(i->argument), false); | |
1554 | if (r < 0) { | |
1555 | r = erofs ? -EROFS : r; | |
1556 | return log_error_errno(r, "Failed to write file %s: %m", path); | |
1557 | } | |
1558 | } | |
1559 | ||
1560 | return fd_set_perms(i, fd, path, st); | |
1561 | } | |
1562 | ||
1563 | static int copy_files(Item *i) { | |
1564 | _cleanup_close_ int dfd = -1, fd = -1; | |
1565 | char *bn; | |
1566 | int r; | |
1567 | ||
1568 | log_debug("Copying tree \"%s\" to \"%s\".", i->argument, i->path); | |
1569 | ||
1570 | bn = basename(i->path); | |
1571 | ||
1572 | /* Validate the path and use the returned directory fd for copying the | |
1573 | * target so we're sure that the path can't be changed behind our | |
1574 | * back. */ | |
1575 | dfd = path_open_parent_safe(i->path); | |
1576 | if (dfd < 0) | |
1577 | return dfd; | |
1578 | ||
1579 | r = copy_tree_at(AT_FDCWD, i->argument, | |
1580 | dfd, bn, | |
1581 | i->uid_set ? i->uid : UID_INVALID, | |
1582 | i->gid_set ? i->gid : GID_INVALID, | |
a032b68d | 1583 | COPY_REFLINK | COPY_MERGE_EMPTY | COPY_MAC_CREATE | COPY_HARDLINKS); |
6e866b33 MB |
1584 | if (r < 0) { |
1585 | struct stat a, b; | |
1586 | ||
1587 | /* If the target already exists on read-only filesystems, trying | |
1588 | * to create the target will not fail with EEXIST but with | |
1589 | * EROFS. */ | |
1590 | if (r == -EROFS && faccessat(dfd, bn, F_OK, AT_SYMLINK_NOFOLLOW) == 0) | |
1591 | r = -EEXIST; | |
1592 | ||
1593 | if (r != -EEXIST) | |
1594 | return log_error_errno(r, "Failed to copy files to %s: %m", i->path); | |
1595 | ||
1596 | if (stat(i->argument, &a) < 0) | |
1597 | return log_error_errno(errno, "stat(%s) failed: %m", i->argument); | |
1598 | ||
1599 | if (fstatat(dfd, bn, &b, AT_SYMLINK_NOFOLLOW) < 0) | |
1600 | return log_error_errno(errno, "stat(%s) failed: %m", i->path); | |
1601 | ||
1602 | if ((a.st_mode ^ b.st_mode) & S_IFMT) { | |
1603 | log_debug("Can't copy to %s, file exists already and is of different type", i->path); | |
1604 | return 0; | |
1605 | } | |
1606 | } | |
1607 | ||
1608 | fd = openat(dfd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH); | |
1609 | if (fd < 0) | |
1610 | return log_error_errno(errno, "Failed to openat(%s): %m", i->path); | |
1611 | ||
1612 | return fd_set_perms(i, fd, i->path, NULL); | |
1613 | } | |
1614 | ||
1615 | typedef enum { | |
1616 | CREATION_NORMAL, | |
1617 | CREATION_EXISTING, | |
1618 | CREATION_FORCE, | |
1619 | _CREATION_MODE_MAX, | |
3a6ce677 | 1620 | _CREATION_MODE_INVALID = -EINVAL, |
6e866b33 MB |
1621 | } CreationMode; |
1622 | ||
bb4f798a | 1623 | static const char *const creation_mode_verb_table[_CREATION_MODE_MAX] = { |
6e866b33 MB |
1624 | [CREATION_NORMAL] = "Created", |
1625 | [CREATION_EXISTING] = "Found existing", | |
1626 | [CREATION_FORCE] = "Created replacement", | |
1627 | }; | |
1628 | ||
1629 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(creation_mode_verb, CreationMode); | |
1630 | ||
1631 | static int create_directory_or_subvolume(const char *path, mode_t mode, bool subvol, CreationMode *creation) { | |
1632 | _cleanup_close_ int pfd = -1; | |
1633 | CreationMode c; | |
1634 | int r; | |
1635 | ||
1636 | assert(path); | |
1637 | ||
1638 | if (!creation) | |
1639 | creation = &c; | |
1640 | ||
1641 | pfd = path_open_parent_safe(path); | |
1642 | if (pfd < 0) | |
1643 | return pfd; | |
1644 | ||
1645 | if (subvol) { | |
3a6ce677 BR |
1646 | r = getenv_bool("SYSTEMD_TMPFILES_FORCE_SUBVOL"); |
1647 | if (r < 0) { | |
1648 | if (r != -ENXIO) /* env var is unset */ | |
1649 | log_warning_errno(r, "Cannot parse value of $SYSTEMD_TMPFILES_FORCE_SUBVOL, ignoring."); | |
1650 | r = btrfs_is_subvol(empty_to_root(arg_root)) > 0; | |
1651 | } | |
1652 | if (!r) | |
6e866b33 MB |
1653 | /* Don't create a subvolume unless the root directory is |
1654 | * one, too. We do this under the assumption that if the | |
1655 | * root directory is just a plain directory (i.e. very | |
1656 | * light-weight), we shouldn't try to split it up into | |
1657 | * subvolumes (i.e. more heavy-weight). Thus, chroot() | |
1658 | * environments and suchlike will get a full brtfs | |
1659 | * subvolume set up below their tree only if they | |
1660 | * specifically set up a btrfs subvolume for the root | |
1661 | * dir too. */ | |
1662 | ||
1663 | subvol = false; | |
1664 | else { | |
1665 | RUN_WITH_UMASK((~mode) & 0777) | |
1666 | r = btrfs_subvol_make_fd(pfd, basename(path)); | |
1667 | } | |
1668 | } else | |
1669 | r = 0; | |
1670 | ||
1671 | if (!subvol || r == -ENOTTY) | |
1672 | RUN_WITH_UMASK(0000) | |
1673 | r = mkdirat_label(pfd, basename(path), mode); | |
1674 | ||
1675 | if (r < 0) { | |
1676 | int k; | |
1677 | ||
1678 | if (!IN_SET(r, -EEXIST, -EROFS)) | |
1679 | return log_error_errno(r, "Failed to create directory or subvolume \"%s\": %m", path); | |
1680 | ||
1681 | k = is_dir_fd(pfd); | |
1682 | if (k == -ENOENT && r == -EROFS) | |
1683 | return log_error_errno(r, "%s does not exist and cannot be created as the file system is read-only.", path); | |
1684 | if (k < 0) | |
1685 | return log_error_errno(k, "Failed to check if %s exists: %m", path); | |
5b5a102a MB |
1686 | if (!k) |
1687 | return log_warning_errno(SYNTHETIC_ERRNO(EEXIST), | |
1688 | "\"%s\" already exists and is not a directory.", path); | |
6e866b33 MB |
1689 | |
1690 | *creation = CREATION_EXISTING; | |
1691 | } else | |
1692 | *creation = CREATION_NORMAL; | |
1693 | ||
1694 | log_debug("%s directory \"%s\".", creation_mode_verb_to_string(*creation), path); | |
1695 | ||
1696 | r = openat(pfd, basename(path), O_NOCTTY|O_CLOEXEC|O_DIRECTORY); | |
f47781d8 | 1697 | if (r < 0) |
6e866b33 | 1698 | return log_error_errno(errno, "Failed to open directory '%s': %m", basename(path)); |
f47781d8 | 1699 | |
6e866b33 | 1700 | return r; |
663996b3 MS |
1701 | } |
1702 | ||
6e866b33 MB |
1703 | static int create_directory(Item *i, const char *path) { |
1704 | _cleanup_close_ int fd = -1; | |
1705 | ||
1706 | assert(i); | |
1707 | assert(IN_SET(i->type, CREATE_DIRECTORY, TRUNCATE_DIRECTORY)); | |
1708 | ||
1709 | fd = create_directory_or_subvolume(path, i->mode, false, NULL); | |
1710 | if (fd == -EEXIST) | |
1711 | return 0; | |
1712 | if (fd < 0) | |
1713 | return fd; | |
1714 | ||
1715 | return fd_set_perms(i, fd, path, NULL); | |
1716 | } | |
1717 | ||
1718 | static int create_subvolume(Item *i, const char *path) { | |
1719 | _cleanup_close_ int fd = -1; | |
1720 | CreationMode creation; | |
1721 | int r, q = 0; | |
1722 | ||
1723 | assert(i); | |
1724 | assert(IN_SET(i->type, CREATE_SUBVOLUME, CREATE_SUBVOLUME_NEW_QUOTA, CREATE_SUBVOLUME_INHERIT_QUOTA)); | |
1725 | ||
1726 | fd = create_directory_or_subvolume(path, i->mode, true, &creation); | |
1727 | if (fd == -EEXIST) | |
1728 | return 0; | |
1729 | if (fd < 0) | |
1730 | return fd; | |
1731 | ||
1732 | if (creation == CREATION_NORMAL && | |
1733 | IN_SET(i->type, CREATE_SUBVOLUME_NEW_QUOTA, CREATE_SUBVOLUME_INHERIT_QUOTA)) { | |
1734 | r = btrfs_subvol_auto_qgroup_fd(fd, 0, i->type == CREATE_SUBVOLUME_NEW_QUOTA); | |
1735 | if (r == -ENOTTY) | |
1736 | log_debug_errno(r, "Couldn't adjust quota for subvolume \"%s\" (unsupported fs or dir not a subvolume): %m", i->path); | |
1737 | else if (r == -EROFS) | |
1738 | log_debug_errno(r, "Couldn't adjust quota for subvolume \"%s\" (fs is read-only).", i->path); | |
a10f5d05 | 1739 | else if (r == -ENOTCONN) |
6e866b33 MB |
1740 | log_debug_errno(r, "Couldn't adjust quota for subvolume \"%s\" (quota support is disabled).", i->path); |
1741 | else if (r < 0) | |
1742 | q = log_error_errno(r, "Failed to adjust quota for subvolume \"%s\": %m", i->path); | |
1743 | else if (r > 0) | |
1744 | log_debug("Adjusted quota for subvolume \"%s\".", i->path); | |
1745 | else if (r == 0) | |
1746 | log_debug("Quota for subvolume \"%s\" already in place, no change made.", i->path); | |
1747 | } | |
1748 | ||
1749 | r = fd_set_perms(i, fd, path, NULL); | |
1750 | if (q < 0) /* prefer the quota change error from above */ | |
1751 | return q; | |
1752 | ||
1753 | return r; | |
1754 | } | |
1755 | ||
1756 | static int empty_directory(Item *i, const char *path) { | |
1757 | int r; | |
e735f4d4 | 1758 | |
6e866b33 MB |
1759 | assert(i); |
1760 | assert(i->type == EMPTY_DIRECTORY); | |
1761 | ||
1762 | r = is_dir(path, false); | |
1763 | if (r == -ENOENT) { | |
1764 | /* Option "e" operates only on existing objects. Do not | |
1765 | * print errors about non-existent files or directories */ | |
1766 | log_debug("Skipping missing directory: %s", path); | |
1767 | return 0; | |
1768 | } | |
1769 | if (r < 0) | |
1770 | return log_error_errno(r, "is_dir() failed on path %s: %m", path); | |
5b5a102a MB |
1771 | if (r == 0) { |
1772 | log_warning("\"%s\" already exists and is not a directory.", path); | |
1773 | return 0; | |
1774 | } | |
6e866b33 MB |
1775 | |
1776 | return path_set_perms(i, path); | |
1777 | } | |
1778 | ||
1779 | static int create_device(Item *i, mode_t file_type) { | |
1780 | _cleanup_close_ int dfd = -1, fd = -1; | |
1781 | CreationMode creation; | |
1782 | char *bn; | |
1783 | int r; | |
1784 | ||
1785 | assert(i); | |
1786 | assert(IN_SET(file_type, S_IFBLK, S_IFCHR)); | |
1787 | ||
1788 | bn = basename(i->path); | |
1789 | ||
1790 | /* Validate the path and use the returned directory fd for copying the | |
1791 | * target so we're sure that the path can't be changed behind our | |
1792 | * back. */ | |
1793 | dfd = path_open_parent_safe(i->path); | |
1794 | if (dfd < 0) | |
1795 | return dfd; | |
1796 | ||
1797 | RUN_WITH_UMASK(0000) { | |
1798 | mac_selinux_create_file_prepare(i->path, file_type); | |
1799 | r = mknodat(dfd, bn, i->mode | file_type, i->major_minor); | |
1800 | mac_selinux_create_file_clear(); | |
1801 | } | |
1802 | ||
1803 | if (r < 0) { | |
1804 | struct stat st; | |
1805 | ||
1806 | if (errno == EPERM) { | |
1807 | log_debug("We lack permissions, possibly because of cgroup configuration; " | |
1808 | "skipping creation of device node %s.", i->path); | |
1809 | return 0; | |
1810 | } | |
1811 | ||
1812 | if (errno != EEXIST) | |
1813 | return log_error_errno(errno, "Failed to create device node %s: %m", i->path); | |
1814 | ||
1815 | if (fstatat(dfd, bn, &st, 0) < 0) | |
1816 | return log_error_errno(errno, "stat(%s) failed: %m", i->path); | |
1817 | ||
1818 | if ((st.st_mode & S_IFMT) != file_type) { | |
1819 | ||
e1f67bc7 | 1820 | if (i->append_or_force) { |
6e866b33 MB |
1821 | |
1822 | RUN_WITH_UMASK(0000) { | |
1823 | mac_selinux_create_file_prepare(i->path, file_type); | |
1824 | /* FIXME: need to introduce mknodat_atomic() */ | |
1825 | r = mknod_atomic(i->path, i->mode | file_type, i->major_minor); | |
1826 | mac_selinux_create_file_clear(); | |
1827 | } | |
1828 | ||
1829 | if (r < 0) | |
1830 | return log_error_errno(r, "Failed to create device node \"%s\": %m", i->path); | |
1831 | creation = CREATION_FORCE; | |
1832 | } else { | |
5b5a102a | 1833 | log_warning("\"%s\" already exists is not a device node.", i->path); |
6e866b33 MB |
1834 | return 0; |
1835 | } | |
1836 | } else | |
1837 | creation = CREATION_EXISTING; | |
1838 | } else | |
1839 | creation = CREATION_NORMAL; | |
1840 | ||
1841 | log_debug("%s %s device node \"%s\" %u:%u.", | |
1842 | creation_mode_verb_to_string(creation), | |
1843 | i->type == CREATE_BLOCK_DEVICE ? "block" : "char", | |
1844 | i->path, major(i->mode), minor(i->mode)); | |
1845 | ||
1846 | fd = openat(dfd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH); | |
1847 | if (fd < 0) | |
1848 | return log_error_errno(errno, "Failed to openat(%s): %m", i->path); | |
1849 | ||
1850 | return fd_set_perms(i, fd, i->path, NULL); | |
1851 | } | |
1852 | ||
1853 | static int create_fifo(Item *i, const char *path) { | |
1854 | _cleanup_close_ int pfd = -1, fd = -1; | |
1855 | CreationMode creation; | |
1856 | struct stat st; | |
1857 | char *bn; | |
1858 | int r; | |
1859 | ||
1860 | pfd = path_open_parent_safe(path); | |
1861 | if (pfd < 0) | |
1862 | return pfd; | |
1863 | ||
1864 | bn = basename(path); | |
1865 | ||
1866 | RUN_WITH_UMASK(0000) { | |
1867 | mac_selinux_create_file_prepare(path, S_IFIFO); | |
1868 | r = mkfifoat(pfd, bn, i->mode); | |
1869 | mac_selinux_create_file_clear(); | |
1870 | } | |
1871 | ||
1872 | if (r < 0) { | |
1873 | if (errno != EEXIST) | |
1874 | return log_error_errno(errno, "Failed to create fifo %s: %m", path); | |
1875 | ||
1876 | if (fstatat(pfd, bn, &st, AT_SYMLINK_NOFOLLOW) < 0) | |
1877 | return log_error_errno(errno, "stat(%s) failed: %m", path); | |
1878 | ||
1879 | if (!S_ISFIFO(st.st_mode)) { | |
1880 | ||
e1f67bc7 | 1881 | if (i->append_or_force) { |
6e866b33 MB |
1882 | RUN_WITH_UMASK(0000) { |
1883 | mac_selinux_create_file_prepare(path, S_IFIFO); | |
1884 | r = mkfifoat_atomic(pfd, bn, i->mode); | |
1885 | mac_selinux_create_file_clear(); | |
1886 | } | |
1887 | ||
1888 | if (r < 0) | |
1889 | return log_error_errno(r, "Failed to create fifo %s: %m", path); | |
1890 | creation = CREATION_FORCE; | |
1891 | } else { | |
1892 | log_warning("\"%s\" already exists and is not a fifo.", path); | |
1893 | return 0; | |
1894 | } | |
1895 | } else | |
1896 | creation = CREATION_EXISTING; | |
1897 | } else | |
1898 | creation = CREATION_NORMAL; | |
1899 | ||
1900 | log_debug("%s fifo \"%s\".", creation_mode_verb_to_string(creation), path); | |
1901 | ||
1902 | fd = openat(pfd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH); | |
1903 | if (fd < 0) | |
1904 | return log_error_errno(errno, "Failed to openat(%s): %m", path); | |
1905 | ||
1906 | return fd_set_perms(i, fd, i->path, NULL); | |
1907 | } | |
1908 | ||
1909 | typedef int (*action_t)(Item *i, const char *path); | |
1910 | typedef int (*fdaction_t)(Item *i, int fd, const char *path, const struct stat *st); | |
1911 | ||
1912 | static int item_do(Item *i, int fd, const char *path, fdaction_t action) { | |
1913 | struct stat st; | |
98393f85 | 1914 | int r = 0, q; |
60f067b4 JS |
1915 | |
1916 | assert(i); | |
6e866b33 | 1917 | assert(path); |
98393f85 | 1918 | assert(fd >= 0); |
6e866b33 MB |
1919 | |
1920 | if (fstat(fd, &st) < 0) { | |
1921 | r = log_error_errno(errno, "fstat() on file failed: %m"); | |
1922 | goto finish; | |
1923 | } | |
663996b3 MS |
1924 | |
1925 | /* This returns the first error we run into, but nevertheless | |
1926 | * tries to go on */ | |
6e866b33 | 1927 | r = action(i, fd, path, &st); |
663996b3 | 1928 | |
6e866b33 | 1929 | if (S_ISDIR(st.st_mode)) { |
b012e921 | 1930 | char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)]; |
98393f85 MB |
1931 | _cleanup_closedir_ DIR *d = NULL; |
1932 | struct dirent *de; | |
663996b3 | 1933 | |
98393f85 MB |
1934 | /* The passed 'fd' was opened with O_PATH. We need to convert |
1935 | * it into a 'regular' fd before reading the directory content. */ | |
1936 | xsprintf(procfs_path, "/proc/self/fd/%i", fd); | |
60f067b4 | 1937 | |
98393f85 MB |
1938 | d = opendir(procfs_path); |
1939 | if (!d) { | |
6e866b33 MB |
1940 | log_error_errno(errno, "Failed to opendir() '%s': %m", procfs_path); |
1941 | if (r == 0) | |
1942 | r = -errno; | |
98393f85 MB |
1943 | goto finish; |
1944 | } | |
663996b3 | 1945 | |
98393f85 | 1946 | FOREACH_DIRENT_ALL(de, d, q = -errno; goto finish) { |
98393f85 MB |
1947 | int de_fd; |
1948 | ||
1949 | if (dot_or_dot_dot(de->d_name)) | |
1950 | continue; | |
663996b3 | 1951 | |
98393f85 | 1952 | de_fd = openat(fd, de->d_name, O_NOFOLLOW|O_CLOEXEC|O_PATH); |
6e866b33 MB |
1953 | if (de_fd < 0) |
1954 | q = log_error_errno(errno, "Failed to open() file '%s': %m", de->d_name); | |
1955 | else { | |
1956 | _cleanup_free_ char *de_path = NULL; | |
1957 | ||
1958 | de_path = path_join(path, de->d_name); | |
1959 | if (!de_path) | |
1960 | q = log_oom(); | |
1961 | else | |
1962 | /* Pass ownership of dirent fd over */ | |
1963 | q = item_do(i, de_fd, de_path, action); | |
1964 | } | |
663996b3 | 1965 | |
60f067b4 JS |
1966 | if (q < 0 && r == 0) |
1967 | r = q; | |
663996b3 MS |
1968 | } |
1969 | } | |
98393f85 MB |
1970 | finish: |
1971 | safe_close(fd); | |
60f067b4 | 1972 | return r; |
663996b3 MS |
1973 | } |
1974 | ||
98393f85 | 1975 | static int glob_item(Item *i, action_t action) { |
e735f4d4 | 1976 | _cleanup_globfree_ glob_t g = { |
e735f4d4 | 1977 | .gl_opendir = (void *(*)(const char *)) opendir_nomod, |
e735f4d4 | 1978 | }; |
60f067b4 | 1979 | int r = 0, k; |
663996b3 MS |
1980 | char **fn; |
1981 | ||
81c58355 MB |
1982 | k = safe_glob(i->path, GLOB_NOSORT|GLOB_BRACE, &g); |
1983 | if (k < 0 && k != -ENOENT) | |
1984 | return log_error_errno(k, "glob(%s) failed: %m", i->path); | |
663996b3 MS |
1985 | |
1986 | STRV_FOREACH(fn, g.gl_pathv) { | |
1987 | k = action(i, *fn); | |
60f067b4 | 1988 | if (k < 0 && r == 0) |
663996b3 | 1989 | r = k; |
98393f85 | 1990 | } |
e735f4d4 | 1991 | |
98393f85 MB |
1992 | return r; |
1993 | } | |
1994 | ||
1995 | static int glob_item_recursively(Item *i, fdaction_t action) { | |
1996 | _cleanup_globfree_ glob_t g = { | |
1997 | .gl_opendir = (void *(*)(const char *)) opendir_nomod, | |
1998 | }; | |
1999 | int r = 0, k; | |
2000 | char **fn; | |
2001 | ||
2002 | k = safe_glob(i->path, GLOB_NOSORT|GLOB_BRACE, &g); | |
2003 | if (k < 0 && k != -ENOENT) | |
2004 | return log_error_errno(k, "glob(%s) failed: %m", i->path); | |
2005 | ||
2006 | STRV_FOREACH(fn, g.gl_pathv) { | |
2007 | _cleanup_close_ int fd = -1; | |
98393f85 MB |
2008 | |
2009 | /* Make sure we won't trigger/follow file object (such as | |
2010 | * device nodes, automounts, ...) pointed out by 'fn' with | |
2011 | * O_PATH. Note, when O_PATH is used, flags other than | |
2012 | * O_CLOEXEC, O_DIRECTORY, and O_NOFOLLOW are ignored. */ | |
2013 | ||
2014 | fd = open(*fn, O_CLOEXEC|O_NOFOLLOW|O_PATH); | |
2015 | if (fd < 0) { | |
6e866b33 MB |
2016 | log_error_errno(errno, "Opening '%s' failed: %m", *fn); |
2017 | if (r == 0) | |
2018 | r = -errno; | |
98393f85 MB |
2019 | continue; |
2020 | } | |
2021 | ||
6e866b33 | 2022 | k = item_do(i, fd, *fn, action); |
98393f85 MB |
2023 | if (k < 0 && r == 0) |
2024 | r = k; | |
2025 | ||
2026 | /* we passed fd ownership to the previous call */ | |
2027 | fd = -1; | |
663996b3 MS |
2028 | } |
2029 | ||
2030 | return r; | |
2031 | } | |
2032 | ||
8b3d4ff0 MB |
2033 | static int rm_if_wrong_type_safe( |
2034 | mode_t mode, | |
2035 | int parent_fd, | |
2036 | const struct stat *parent_st, /* Only used if follow_links below is true. */ | |
2037 | const char *name, | |
2038 | int flags) { | |
2039 | _cleanup_free_ char *parent_name = NULL; | |
2040 | bool follow_links = !FLAGS_SET(flags, AT_SYMLINK_NOFOLLOW); | |
2041 | struct stat st; | |
2042 | int r; | |
2043 | ||
2044 | assert(name); | |
2045 | assert((mode & ~S_IFMT) == 0); | |
2046 | assert(!follow_links || parent_st); | |
2047 | assert((flags & ~AT_SYMLINK_NOFOLLOW) == 0); | |
2048 | ||
2049 | if (!filename_is_valid(name)) | |
2050 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "\"%s\" is not a valid filename.", name); | |
2051 | ||
2052 | r = fstatat_harder(parent_fd, name, &st, flags, REMOVE_CHMOD | REMOVE_CHMOD_RESTORE); | |
2053 | if (r < 0) { | |
2054 | (void) fd_get_path(parent_fd, &parent_name); | |
2055 | return log_full_errno(r == -ENOENT? LOG_DEBUG : LOG_ERR, r, | |
2056 | "Failed to stat \"%s\" at \"%s\": %m", name, strna(parent_name)); | |
2057 | } | |
2058 | ||
2059 | /* Fail before removing anything if this is an unsafe transition. */ | |
2060 | if (follow_links && unsafe_transition(parent_st, &st)) { | |
2061 | (void) fd_get_path(parent_fd, &parent_name); | |
2062 | return log_error_errno(SYNTHETIC_ERRNO(ENOLINK), | |
2063 | "Unsafe transition from \"%s\" to \"%s\".", parent_name, name); | |
2064 | } | |
2065 | ||
2066 | if ((st.st_mode & S_IFMT) == mode) | |
2067 | return 0; | |
2068 | ||
2069 | (void) fd_get_path(parent_fd, &parent_name); | |
2070 | log_notice("Wrong file type 0x%x; rm -rf \"%s/%s\"", st.st_mode & S_IFMT, strna(parent_name), name); | |
2071 | ||
2072 | /* If the target of the symlink was the wrong type, the link needs to be removed instead of the | |
2073 | * target, so make sure it is identified as a link and not a directory. */ | |
2074 | if (follow_links) { | |
2075 | r = fstatat_harder(parent_fd, name, &st, AT_SYMLINK_NOFOLLOW, REMOVE_CHMOD | REMOVE_CHMOD_RESTORE); | |
2076 | if (r < 0) | |
2077 | return log_error_errno(r, "Failed to stat \"%s\" at \"%s\": %m", name, strna(parent_name)); | |
2078 | } | |
2079 | ||
2080 | /* Do not remove mount points. */ | |
2081 | r = fd_is_mount_point(parent_fd, name, follow_links ? AT_SYMLINK_FOLLOW : 0); | |
2082 | if (r < 0) | |
2083 | (void) log_warning_errno(r, "Failed to check if \"%s/%s\" is a mount point: %m; Continuing", | |
2084 | strna(parent_name), name); | |
2085 | else if (r > 0) | |
2086 | return log_error_errno(SYNTHETIC_ERRNO(EBUSY), | |
2087 | "Not removing \"%s/%s\" because it is a mount point.", strna(parent_name), name); | |
2088 | ||
2089 | if ((st.st_mode & S_IFMT) == S_IFDIR) { | |
2090 | _cleanup_close_ int child_fd = -1; | |
2091 | ||
2092 | child_fd = openat(parent_fd, name, O_NOCTTY | O_CLOEXEC | O_DIRECTORY); | |
2093 | if (child_fd < 0) | |
2094 | return log_error_errno(errno, "Failed to open \"%s\" at \"%s\": %m", name, strna(parent_name)); | |
2095 | ||
2096 | r = rm_rf_children(TAKE_FD(child_fd), REMOVE_ROOT|REMOVE_SUBVOLUME|REMOVE_PHYSICAL, &st); | |
2097 | if (r < 0) | |
2098 | return log_error_errno(r, "Failed to remove contents of \"%s\" at \"%s\": %m", name, strna(parent_name)); | |
2099 | ||
2100 | r = unlinkat_harder(parent_fd, name, AT_REMOVEDIR, REMOVE_CHMOD | REMOVE_CHMOD_RESTORE); | |
2101 | } else | |
2102 | r = unlinkat_harder(parent_fd, name, 0, REMOVE_CHMOD | REMOVE_CHMOD_RESTORE); | |
2103 | if (r < 0) | |
2104 | return log_error_errno(r, "Failed to remove \"%s\" at \"%s\": %m", name, strna(parent_name)); | |
2105 | ||
2106 | /* This is covered by the log_notice "Wrong file type..." It is logged earlier because it gives | |
2107 | * context to other error messages that might follow. */ | |
2108 | return -ENOENT; | |
2109 | } | |
2110 | ||
2111 | /* If child_mode is non-zero, rm_if_wrong_type_safe will be executed for the last path component. */ | |
2112 | static int mkdir_parents_rm_if_wrong_type(mode_t child_mode, const char *path) { | |
2113 | _cleanup_close_ int parent_fd = -1; | |
2114 | struct stat parent_st; | |
2115 | size_t path_len; | |
2116 | int r; | |
2117 | ||
2118 | assert(path); | |
2119 | assert((child_mode & ~S_IFMT) == 0); | |
2120 | ||
2121 | path_len = strlen(path); | |
2122 | ||
2123 | if (!is_path(path)) | |
2124 | /* rm_if_wrong_type_safe already logs errors. */ | |
2125 | return child_mode != 0 ? rm_if_wrong_type_safe(child_mode, AT_FDCWD, NULL, path, AT_SYMLINK_NOFOLLOW) : 0; | |
2126 | ||
2127 | if (child_mode != 0 && endswith(path, "/")) | |
2128 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
2129 | "Trailing path separators are only allowed if child_mode is not set; got \"%s\"", path); | |
2130 | ||
2131 | /* Get the parent_fd and stat. */ | |
2132 | parent_fd = openat(AT_FDCWD, path_is_absolute(path) ? "/" : ".", O_NOCTTY | O_CLOEXEC | O_DIRECTORY); | |
2133 | if (parent_fd < 0) | |
2134 | return log_error_errno(errno, "Failed to open root: %m"); | |
2135 | ||
2136 | if (fstat(parent_fd, &parent_st) < 0) | |
2137 | return log_error_errno(errno, "Failed to stat root: %m"); | |
2138 | ||
2139 | /* Check every parent directory in the path, except the last component */ | |
2140 | for (const char *e = path;;) { | |
2141 | _cleanup_close_ int next_fd = -1; | |
2142 | char t[path_len + 1]; | |
2143 | const char *s; | |
2144 | ||
2145 | /* Find the start of the next path component. */ | |
2146 | s = e + strspn(e, "/"); | |
2147 | /* Find the end of the next path component. */ | |
2148 | e = s + strcspn(s, "/"); | |
2149 | ||
2150 | /* Copy the path component to t so it can be a null terminated string. */ | |
2151 | *((char*) mempcpy(t, s, e - s)) = 0; | |
2152 | ||
2153 | /* Is this the last component? If so, then check the type */ | |
2154 | if (*e == 0) | |
2155 | return child_mode != 0 ? rm_if_wrong_type_safe(child_mode, parent_fd, &parent_st, t, AT_SYMLINK_NOFOLLOW) : 0; | |
2156 | ||
2157 | r = rm_if_wrong_type_safe(S_IFDIR, parent_fd, &parent_st, t, 0); | |
2158 | /* Remove dangling symlinks. */ | |
2159 | if (r == -ENOENT) | |
2160 | r = rm_if_wrong_type_safe(S_IFDIR, parent_fd, &parent_st, t, AT_SYMLINK_NOFOLLOW); | |
2161 | if (r == -ENOENT) { | |
2162 | RUN_WITH_UMASK(0000) | |
2163 | r = mkdirat_label(parent_fd, t, 0755); | |
2164 | if (r < 0) { | |
2165 | _cleanup_free_ char *parent_name = NULL; | |
2166 | ||
2167 | (void) fd_get_path(parent_fd, &parent_name); | |
2168 | return log_error_errno(r, "Failed to mkdir \"%s\" at \"%s\": %m", t, strnull(parent_name)); | |
2169 | } | |
2170 | } else if (r < 0) | |
2171 | /* rm_if_wrong_type_safe already logs errors. */ | |
2172 | return r; | |
2173 | ||
2174 | next_fd = openat(parent_fd, t, O_NOCTTY | O_CLOEXEC | O_DIRECTORY); | |
2175 | if (next_fd < 0) { | |
2176 | _cleanup_free_ char *parent_name = NULL; | |
2177 | ||
2178 | r = -errno; | |
2179 | (void) fd_get_path(parent_fd, &parent_name); | |
2180 | return log_error_errno(r, "Failed to open \"%s\" at \"%s\": %m", t, strnull(parent_name)); | |
2181 | } | |
2182 | if (fstat(next_fd, &parent_st) < 0) { | |
2183 | _cleanup_free_ char *parent_name = NULL; | |
2184 | ||
2185 | r = -errno; | |
2186 | (void) fd_get_path(parent_fd, &parent_name); | |
2187 | return log_error_errno(r, "Failed to stat \"%s\" at \"%s\": %m", t, strnull(parent_name)); | |
2188 | } | |
2189 | ||
2190 | CLOSE_AND_REPLACE(parent_fd, next_fd); | |
2191 | } | |
2192 | } | |
2193 | ||
2194 | static int mkdir_parents_item(Item *i, mode_t child_mode) { | |
2195 | int r; | |
2196 | if (i->try_replace) { | |
2197 | r = mkdir_parents_rm_if_wrong_type(child_mode, i->path); | |
2198 | if (r < 0 && r != -ENOENT) | |
2199 | return r; | |
2200 | } else | |
2201 | RUN_WITH_UMASK(0000) | |
2202 | (void) mkdir_parents_label(i->path, 0755); | |
2203 | ||
2204 | return 0; | |
2205 | } | |
2206 | ||
663996b3 | 2207 | static int create_item(Item *i) { |
e735f4d4 | 2208 | CreationMode creation; |
6e866b33 | 2209 | int r = 0; |
663996b3 MS |
2210 | |
2211 | assert(i); | |
2212 | ||
e735f4d4 MP |
2213 | log_debug("Running create action for entry %c %s", (char) i->type, i->path); |
2214 | ||
663996b3 MS |
2215 | switch (i->type) { |
2216 | ||
2217 | case IGNORE_PATH: | |
2218 | case IGNORE_DIRECTORY_PATH: | |
2219 | case REMOVE_PATH: | |
2220 | case RECURSIVE_REMOVE_PATH: | |
2221 | return 0; | |
2222 | ||
e1f67bc7 | 2223 | case TRUNCATE_FILE: |
663996b3 | 2224 | case CREATE_FILE: |
8b3d4ff0 MB |
2225 | r = mkdir_parents_item(i, S_IFREG); |
2226 | if (r < 0) | |
2227 | return r; | |
1d42b86d | 2228 | |
e1f67bc7 MB |
2229 | if ((i->type == CREATE_FILE && i->append_or_force) || i->type == TRUNCATE_FILE) |
2230 | r = truncate_file(i, i->path); | |
2231 | else | |
2232 | r = create_file(i, i->path); | |
1d42b86d | 2233 | |
6e866b33 MB |
2234 | if (r < 0) |
2235 | return r; | |
2236 | break; | |
e842803a | 2237 | |
6e866b33 | 2238 | case COPY_FILES: |
8b3d4ff0 MB |
2239 | r = mkdir_parents_item(i, 0); |
2240 | if (r < 0) | |
2241 | return r; | |
60f067b4 | 2242 | |
6e866b33 | 2243 | r = copy_files(i); |
663996b3 MS |
2244 | if (r < 0) |
2245 | return r; | |
663996b3 MS |
2246 | break; |
2247 | ||
60f067b4 | 2248 | case WRITE_FILE: |
98393f85 | 2249 | r = glob_item(i, write_one_file); |
14228c0d MB |
2250 | if (r < 0) |
2251 | return r; | |
2252 | ||
2253 | break; | |
2254 | ||
663996b3 | 2255 | case CREATE_DIRECTORY: |
e735f4d4 | 2256 | case TRUNCATE_DIRECTORY: |
8b3d4ff0 MB |
2257 | r = mkdir_parents_item(i, S_IFDIR); |
2258 | if (r < 0) | |
2259 | return r; | |
6e866b33 MB |
2260 | |
2261 | r = create_directory(i, i->path); | |
2262 | if (r < 0) | |
2263 | return r; | |
2264 | break; | |
2265 | ||
e735f4d4 | 2266 | case CREATE_SUBVOLUME: |
db2df898 MP |
2267 | case CREATE_SUBVOLUME_INHERIT_QUOTA: |
2268 | case CREATE_SUBVOLUME_NEW_QUOTA: | |
8b3d4ff0 MB |
2269 | r = mkdir_parents_item(i, S_IFDIR); |
2270 | if (r < 0) | |
2271 | return r; | |
e735f4d4 | 2272 | |
6e866b33 MB |
2273 | r = create_subvolume(i, i->path); |
2274 | if (r < 0) | |
2275 | return r; | |
2276 | break; | |
db2df898 | 2277 | |
aa27b158 | 2278 | case EMPTY_DIRECTORY: |
6e866b33 | 2279 | r = glob_item(i, empty_directory); |
f47781d8 MP |
2280 | if (r < 0) |
2281 | return r; | |
663996b3 MS |
2282 | break; |
2283 | ||
2284 | case CREATE_FIFO: | |
8b3d4ff0 MB |
2285 | r = mkdir_parents_item(i, S_IFIFO); |
2286 | if (r < 0) | |
2287 | return r; | |
1d42b86d | 2288 | |
6e866b33 | 2289 | r = create_fifo(i, i->path); |
f47781d8 MP |
2290 | if (r < 0) |
2291 | return r; | |
663996b3 MS |
2292 | break; |
2293 | ||
e3bff60a | 2294 | case CREATE_SYMLINK: { |
8b3d4ff0 MB |
2295 | r = mkdir_parents_item(i, S_IFLNK); |
2296 | if (r < 0) | |
2297 | return r; | |
1d42b86d | 2298 | |
5eef597e | 2299 | mac_selinux_create_file_prepare(i->path, S_IFLNK); |
52ad194e | 2300 | r = symlink(i->argument, i->path); |
5eef597e | 2301 | mac_selinux_create_file_clear(); |
663996b3 | 2302 | |
663996b3 | 2303 | if (r < 0) { |
e842803a MB |
2304 | _cleanup_free_ char *x = NULL; |
2305 | ||
f47781d8 | 2306 | if (errno != EEXIST) |
52ad194e | 2307 | return log_error_errno(errno, "symlink(%s, %s) failed: %m", i->argument, i->path); |
e842803a MB |
2308 | |
2309 | r = readlink_malloc(i->path, &x); | |
52ad194e | 2310 | if (r < 0 || !streq(i->argument, x)) { |
e842803a | 2311 | |
e1f67bc7 | 2312 | if (i->append_or_force) { |
5eef597e | 2313 | mac_selinux_create_file_prepare(i->path, S_IFLNK); |
52ad194e | 2314 | r = symlink_atomic(i->argument, i->path); |
5eef597e | 2315 | mac_selinux_create_file_clear(); |
663996b3 | 2316 | |
b012e921 | 2317 | if (IN_SET(r, -EISDIR, -EEXIST, -ENOTEMPTY)) { |
f5e65279 MB |
2318 | r = rm_rf(i->path, REMOVE_ROOT|REMOVE_PHYSICAL); |
2319 | if (r < 0) | |
2320 | return log_error_errno(r, "rm -fr %s failed: %m", i->path); | |
2321 | ||
2322 | mac_selinux_create_file_prepare(i->path, S_IFLNK); | |
52ad194e | 2323 | r = symlink(i->argument, i->path) < 0 ? -errno : 0; |
f5e65279 MB |
2324 | mac_selinux_create_file_clear(); |
2325 | } | |
f47781d8 | 2326 | if (r < 0) |
52ad194e | 2327 | return log_error_errno(r, "symlink(%s, %s) failed: %m", i->argument, i->path); |
e3bff60a | 2328 | |
e735f4d4 | 2329 | creation = CREATION_FORCE; |
e842803a | 2330 | } else { |
e735f4d4 | 2331 | log_debug("\"%s\" is not a symlink or does not point to the correct path.", i->path); |
e842803a MB |
2332 | return 0; |
2333 | } | |
e735f4d4 MP |
2334 | } else |
2335 | creation = CREATION_EXISTING; | |
2336 | } else | |
e3bff60a | 2337 | |
e735f4d4 MP |
2338 | creation = CREATION_NORMAL; |
2339 | log_debug("%s symlink \"%s\".", creation_mode_verb_to_string(creation), i->path); | |
663996b3 | 2340 | break; |
e3bff60a | 2341 | } |
663996b3 MS |
2342 | |
2343 | case CREATE_BLOCK_DEVICE: | |
6e866b33 | 2344 | case CREATE_CHAR_DEVICE: |
663996b3 | 2345 | if (have_effective_cap(CAP_MKNOD) == 0) { |
6e866b33 MB |
2346 | /* In a container we lack CAP_MKNOD. We shouldn't attempt to create the device node in that |
2347 | * case to avoid noise, and we don't support virtualized devices in containers anyway. */ | |
663996b3 MS |
2348 | |
2349 | log_debug("We lack CAP_MKNOD, skipping creation of device node %s.", i->path); | |
2350 | return 0; | |
2351 | } | |
2352 | ||
8b3d4ff0 MB |
2353 | r = mkdir_parents_item(i, i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR); |
2354 | if (r < 0) | |
2355 | return r; | |
1d42b86d | 2356 | |
6e866b33 | 2357 | r = create_device(i, i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR); |
f47781d8 MP |
2358 | if (r < 0) |
2359 | return r; | |
2360 | ||
663996b3 | 2361 | break; |
663996b3 | 2362 | |
60f067b4 | 2363 | case ADJUST_MODE: |
663996b3 | 2364 | case RELABEL_PATH: |
98393f85 | 2365 | r = glob_item(i, path_set_perms); |
663996b3 | 2366 | if (r < 0) |
14228c0d | 2367 | return r; |
663996b3 MS |
2368 | break; |
2369 | ||
2370 | case RECURSIVE_RELABEL_PATH: | |
98393f85 | 2371 | r = glob_item_recursively(i, fd_set_perms); |
663996b3 MS |
2372 | if (r < 0) |
2373 | return r; | |
f47781d8 | 2374 | break; |
60f067b4 | 2375 | |
f47781d8 | 2376 | case SET_XATTR: |
98393f85 | 2377 | r = glob_item(i, path_set_xattrs); |
e735f4d4 MP |
2378 | if (r < 0) |
2379 | return r; | |
2380 | break; | |
2381 | ||
2382 | case RECURSIVE_SET_XATTR: | |
98393f85 | 2383 | r = glob_item_recursively(i, fd_set_xattrs); |
e735f4d4 MP |
2384 | if (r < 0) |
2385 | return r; | |
2386 | break; | |
2387 | ||
2388 | case SET_ACL: | |
98393f85 | 2389 | r = glob_item(i, path_set_acls); |
e735f4d4 MP |
2390 | if (r < 0) |
2391 | return r; | |
2392 | break; | |
2393 | ||
2394 | case RECURSIVE_SET_ACL: | |
98393f85 | 2395 | r = glob_item_recursively(i, fd_set_acls); |
f47781d8 MP |
2396 | if (r < 0) |
2397 | return r; | |
60f067b4 | 2398 | break; |
663996b3 | 2399 | |
e3bff60a | 2400 | case SET_ATTRIBUTE: |
98393f85 | 2401 | r = glob_item(i, path_set_attribute); |
e3bff60a MP |
2402 | if (r < 0) |
2403 | return r; | |
2404 | break; | |
2405 | ||
2406 | case RECURSIVE_SET_ATTRIBUTE: | |
98393f85 | 2407 | r = glob_item_recursively(i, fd_set_attribute); |
e3bff60a MP |
2408 | if (r < 0) |
2409 | return r; | |
2410 | break; | |
2411 | } | |
663996b3 MS |
2412 | |
2413 | return 0; | |
2414 | } | |
2415 | ||
2416 | static int remove_item_instance(Item *i, const char *instance) { | |
2417 | int r; | |
2418 | ||
2419 | assert(i); | |
2420 | ||
2421 | switch (i->type) { | |
2422 | ||
663996b3 | 2423 | case REMOVE_PATH: |
f47781d8 | 2424 | if (remove(instance) < 0 && errno != ENOENT) |
e735f4d4 | 2425 | return log_error_errno(errno, "rm(%s): %m", instance); |
663996b3 MS |
2426 | |
2427 | break; | |
2428 | ||
663996b3 | 2429 | case RECURSIVE_REMOVE_PATH: |
6e866b33 | 2430 | /* FIXME: we probably should use dir_cleanup() here instead of rm_rf() so that 'x' is honoured. */ |
e735f4d4 | 2431 | log_debug("rm -rf \"%s\"", instance); |
6e866b33 | 2432 | r = rm_rf(instance, REMOVE_ROOT|REMOVE_SUBVOLUME|REMOVE_PHYSICAL); |
f47781d8 MP |
2433 | if (r < 0 && r != -ENOENT) |
2434 | return log_error_errno(r, "rm_rf(%s): %m", instance); | |
663996b3 MS |
2435 | |
2436 | break; | |
e735f4d4 MP |
2437 | |
2438 | default: | |
2439 | assert_not_reached("wut?"); | |
663996b3 MS |
2440 | } |
2441 | ||
2442 | return 0; | |
2443 | } | |
2444 | ||
2445 | static int remove_item(Item *i) { | |
6e866b33 MB |
2446 | int r; |
2447 | ||
663996b3 MS |
2448 | assert(i); |
2449 | ||
e735f4d4 MP |
2450 | log_debug("Running remove action for entry %c %s", (char) i->type, i->path); |
2451 | ||
663996b3 MS |
2452 | switch (i->type) { |
2453 | ||
663996b3 | 2454 | case TRUNCATE_DIRECTORY: |
6e866b33 MB |
2455 | /* FIXME: we probably should use dir_cleanup() here instead of rm_rf() so that 'x' is honoured. */ |
2456 | log_debug("rm -rf \"%s\"", i->path); | |
2457 | r = rm_rf(i->path, REMOVE_PHYSICAL); | |
2458 | if (r < 0 && r != -ENOENT) | |
2459 | return log_error_errno(r, "rm_rf(%s): %m", i->path); | |
2460 | ||
2461 | return 0; | |
2462 | ||
2463 | case REMOVE_PATH: | |
663996b3 | 2464 | case RECURSIVE_REMOVE_PATH: |
98393f85 | 2465 | return glob_item(i, remove_item_instance); |
663996b3 | 2466 | |
aa27b158 MP |
2467 | default: |
2468 | return 0; | |
2469 | } | |
663996b3 MS |
2470 | } |
2471 | ||
8b3d4ff0 MB |
2472 | static char *age_by_to_string(AgeBy ab, bool is_dir) { |
2473 | static const char ab_map[] = { 'a', 'b', 'c', 'm' }; | |
2474 | size_t j = 0; | |
2475 | char *ret; | |
2476 | ||
2477 | ret = new(char, ELEMENTSOF(ab_map) + 1); | |
2478 | if (!ret) | |
2479 | return NULL; | |
2480 | ||
2481 | for (size_t i = 0; i < ELEMENTSOF(ab_map); i++) | |
2482 | if (FLAGS_SET(ab, 1U << i)) | |
2483 | ret[j++] = is_dir ? ascii_toupper(ab_map[i]) : ab_map[i]; | |
2484 | ||
2485 | ret[j] = 0; | |
2486 | return ret; | |
2487 | } | |
2488 | ||
663996b3 | 2489 | static int clean_item_instance(Item *i, const char* instance) { |
a032b68d | 2490 | char timestamp[FORMAT_TIMESTAMP_MAX]; |
663996b3 | 2491 | _cleanup_closedir_ DIR *d = NULL; |
a032b68d MB |
2492 | STRUCT_STATX_DEFINE(sx); |
2493 | int mountpoint, r; | |
663996b3 MS |
2494 | usec_t cutoff, n; |
2495 | ||
2496 | assert(i); | |
2497 | ||
2498 | if (!i->age_set) | |
2499 | return 0; | |
2500 | ||
2501 | n = now(CLOCK_REALTIME); | |
2502 | if (n < i->age) | |
2503 | return 0; | |
2504 | ||
2505 | cutoff = n - i->age; | |
2506 | ||
e735f4d4 | 2507 | d = opendir_nomod(instance); |
663996b3 | 2508 | if (!d) { |
5a920b42 | 2509 | if (IN_SET(errno, ENOENT, ENOTDIR)) { |
e735f4d4 | 2510 | log_debug_errno(errno, "Directory \"%s\": %m", instance); |
663996b3 | 2511 | return 0; |
e735f4d4 | 2512 | } |
663996b3 | 2513 | |
5a920b42 | 2514 | return log_error_errno(errno, "Failed to open directory %s: %m", instance); |
663996b3 MS |
2515 | } |
2516 | ||
a032b68d MB |
2517 | r = statx_fallback(dirfd(d), "", AT_EMPTY_PATH, STATX_MODE|STATX_INO|STATX_ATIME|STATX_MTIME, &sx); |
2518 | if (r < 0) | |
2519 | return log_error_errno(r, "statx(%s) failed: %m", instance); | |
663996b3 | 2520 | |
a032b68d MB |
2521 | if (FLAGS_SET(sx.stx_attributes_mask, STATX_ATTR_MOUNT_ROOT)) |
2522 | mountpoint = FLAGS_SET(sx.stx_attributes, STATX_ATTR_MOUNT_ROOT); | |
2523 | else { | |
2524 | struct stat ps; | |
663996b3 | 2525 | |
a032b68d MB |
2526 | if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0) |
2527 | return log_error_errno(errno, "stat(%s/..) failed: %m", i->path); | |
663996b3 | 2528 | |
a032b68d MB |
2529 | mountpoint = |
2530 | sx.stx_dev_major != major(ps.st_dev) || | |
2531 | sx.stx_dev_minor != minor(ps.st_dev) || | |
2532 | sx.stx_ino != ps.st_ino; | |
2533 | } | |
663996b3 | 2534 | |
8b3d4ff0 MB |
2535 | if (DEBUG_LOGGING) { |
2536 | _cleanup_free_ char *ab_f = NULL, *ab_d = NULL; | |
2537 | ||
2538 | ab_f = age_by_to_string(i->age_by_file, false); | |
2539 | if (!ab_f) | |
2540 | return log_oom(); | |
2541 | ||
2542 | ab_d = age_by_to_string(i->age_by_dir, true); | |
2543 | if (!ab_d) | |
2544 | return log_oom(); | |
2545 | ||
2546 | log_debug("Cleanup threshold for %s \"%s\" is %s; age-by: %s%s", | |
2547 | mountpoint ? "mount point" : "directory", | |
2548 | instance, | |
2549 | format_timestamp_style(timestamp, sizeof(timestamp), cutoff, TIMESTAMP_US), | |
2550 | ab_f, ab_d); | |
2551 | } | |
e735f4d4 | 2552 | |
a032b68d MB |
2553 | return dir_cleanup(i, instance, d, |
2554 | load_statx_timestamp_nsec(&sx.stx_atime), | |
2555 | load_statx_timestamp_nsec(&sx.stx_mtime), | |
2556 | cutoff * NSEC_PER_USEC, | |
2557 | sx.stx_dev_major, sx.stx_dev_minor, mountpoint, | |
8b3d4ff0 MB |
2558 | MAX_DEPTH, i->keep_first_level, |
2559 | i->age_by_file, i->age_by_dir); | |
663996b3 MS |
2560 | } |
2561 | ||
2562 | static int clean_item(Item *i) { | |
663996b3 MS |
2563 | assert(i); |
2564 | ||
e735f4d4 MP |
2565 | log_debug("Running clean action for entry %c %s", (char) i->type, i->path); |
2566 | ||
663996b3 MS |
2567 | switch (i->type) { |
2568 | case CREATE_DIRECTORY: | |
e735f4d4 | 2569 | case CREATE_SUBVOLUME: |
db2df898 MP |
2570 | case CREATE_SUBVOLUME_INHERIT_QUOTA: |
2571 | case CREATE_SUBVOLUME_NEW_QUOTA: | |
663996b3 MS |
2572 | case TRUNCATE_DIRECTORY: |
2573 | case IGNORE_PATH: | |
60f067b4 | 2574 | case COPY_FILES: |
663996b3 | 2575 | clean_item_instance(i, i->path); |
aa27b158 | 2576 | return 0; |
52ad194e | 2577 | case EMPTY_DIRECTORY: |
663996b3 | 2578 | case IGNORE_DIRECTORY_PATH: |
98393f85 | 2579 | return glob_item(i, clean_item_instance); |
663996b3 | 2580 | default: |
aa27b158 | 2581 | return 0; |
663996b3 | 2582 | } |
663996b3 MS |
2583 | } |
2584 | ||
6e866b33 MB |
2585 | static int process_item(Item *i, OperationMask operation) { |
2586 | OperationMask todo; | |
8b3d4ff0 MB |
2587 | _cleanup_free_ char *_path = NULL; |
2588 | const char *path; | |
6e866b33 | 2589 | int r, q, p; |
663996b3 MS |
2590 | |
2591 | assert(i); | |
2592 | ||
6e866b33 MB |
2593 | todo = operation & ~i->done; |
2594 | if (todo == 0) /* Everything already done? */ | |
60f067b4 JS |
2595 | return 0; |
2596 | ||
6e866b33 | 2597 | i->done |= operation; |
e735f4d4 | 2598 | |
8b3d4ff0 MB |
2599 | path = i->path; |
2600 | if (string_is_glob(path)) { | |
2601 | /* We can't easily check whether a glob matches any autofs path, so let's do the check only | |
2602 | * for the non-glob part. */ | |
2603 | ||
2604 | r = glob_non_glob_prefix(path, &_path); | |
2605 | if (r < 0 && r != -ENOENT) | |
2606 | return log_debug_errno(r, "Failed to deglob path: %m"); | |
2607 | if (r >= 0) | |
2608 | path = _path; | |
2609 | } | |
2610 | ||
2611 | r = chase_symlinks(path, arg_root, CHASE_NO_AUTOFS|CHASE_NONEXISTENT|CHASE_WARN, NULL, NULL); | |
6e866b33 | 2612 | if (r == -EREMOTE) { |
8b3d4ff0 | 2613 | log_notice_errno(r, "Skipping %s", i->path); /* We log the configured path, to not confuse the user. */ |
6e866b33 | 2614 | return 0; |
60f067b4 | 2615 | } |
6e866b33 MB |
2616 | if (r < 0) |
2617 | log_debug_errno(r, "Failed to determine whether '%s' is below autofs, ignoring: %m", i->path); | |
60f067b4 | 2618 | |
6e866b33 MB |
2619 | r = FLAGS_SET(operation, OPERATION_CREATE) ? create_item(i) : 0; |
2620 | /* Failure can only be tolerated for create */ | |
2621 | if (i->allow_failure) | |
2622 | r = 0; | |
f5e65279 | 2623 | |
6e866b33 MB |
2624 | q = FLAGS_SET(operation, OPERATION_REMOVE) ? remove_item(i) : 0; |
2625 | p = FLAGS_SET(operation, OPERATION_CLEAN) ? clean_item(i) : 0; | |
663996b3 | 2626 | |
6e866b33 | 2627 | return r < 0 ? r : |
e735f4d4 MP |
2628 | q < 0 ? q : |
2629 | p; | |
2630 | } | |
663996b3 | 2631 | |
6e866b33 MB |
2632 | static int process_item_array(ItemArray *array, OperationMask operation) { |
2633 | int r = 0; | |
2634 | size_t n; | |
663996b3 | 2635 | |
e735f4d4 | 2636 | assert(array); |
663996b3 | 2637 | |
6e866b33 MB |
2638 | /* Create any parent first. */ |
2639 | if (FLAGS_SET(operation, OPERATION_CREATE) && array->parent) | |
2640 | r = process_item_array(array->parent, operation & OPERATION_CREATE); | |
2641 | ||
2642 | /* Clean up all children first */ | |
2643 | if ((operation & (OPERATION_REMOVE|OPERATION_CLEAN)) && !set_isempty(array->children)) { | |
6e866b33 MB |
2644 | ItemArray *c; |
2645 | ||
a032b68d | 2646 | SET_FOREACH(c, array->children) { |
6e866b33 MB |
2647 | int k; |
2648 | ||
2649 | k = process_item_array(c, operation & (OPERATION_REMOVE|OPERATION_CLEAN)); | |
2650 | if (k < 0 && r == 0) | |
2651 | r = k; | |
2652 | } | |
2653 | } | |
2654 | ||
2655 | for (n = 0; n < array->n_items; n++) { | |
2656 | int k; | |
2657 | ||
2658 | k = process_item(array->items + n, operation); | |
e735f4d4 MP |
2659 | if (k < 0 && r == 0) |
2660 | r = k; | |
2661 | } | |
e842803a | 2662 | |
e735f4d4 MP |
2663 | return r; |
2664 | } | |
663996b3 | 2665 | |
e735f4d4 MP |
2666 | static void item_free_contents(Item *i) { |
2667 | assert(i); | |
663996b3 MS |
2668 | free(i->path); |
2669 | free(i->argument); | |
f47781d8 | 2670 | strv_free(i->xattrs); |
e735f4d4 | 2671 | |
f5e65279 | 2672 | #if HAVE_ACL |
e735f4d4 MP |
2673 | acl_free(i->acl_access); |
2674 | acl_free(i->acl_default); | |
2675 | #endif | |
663996b3 MS |
2676 | } |
2677 | ||
6e866b33 MB |
2678 | static ItemArray* item_array_free(ItemArray *a) { |
2679 | size_t n; | |
e735f4d4 MP |
2680 | |
2681 | if (!a) | |
6e866b33 | 2682 | return NULL; |
14228c0d | 2683 | |
6e866b33 | 2684 | for (n = 0; n < a->n_items; n++) |
e735f4d4 | 2685 | item_free_contents(a->items + n); |
6e866b33 MB |
2686 | |
2687 | set_free(a->children); | |
e735f4d4 | 2688 | free(a->items); |
6e866b33 | 2689 | return mfree(a); |
e735f4d4 MP |
2690 | } |
2691 | ||
6e866b33 | 2692 | static int item_compare(const Item *a, const Item *b) { |
e3bff60a MP |
2693 | /* Make sure that the ownership taking item is put first, so |
2694 | * that we first create the node, and then can adjust it */ | |
2695 | ||
6e866b33 | 2696 | if (takes_ownership(a->type) && !takes_ownership(b->type)) |
e3bff60a | 2697 | return -1; |
6e866b33 | 2698 | if (!takes_ownership(a->type) && takes_ownership(b->type)) |
e3bff60a MP |
2699 | return 1; |
2700 | ||
6e866b33 | 2701 | return CMP(a->type, b->type); |
e3bff60a MP |
2702 | } |
2703 | ||
e735f4d4 | 2704 | static bool item_compatible(Item *a, Item *b) { |
663996b3 MS |
2705 | assert(a); |
2706 | assert(b); | |
e735f4d4 | 2707 | assert(streq(a->path, b->path)); |
663996b3 | 2708 | |
e735f4d4 MP |
2709 | if (takes_ownership(a->type) && takes_ownership(b->type)) |
2710 | /* check if the items are the same */ | |
2711 | return streq_ptr(a->argument, b->argument) && | |
663996b3 | 2712 | |
e735f4d4 MP |
2713 | a->uid_set == b->uid_set && |
2714 | a->uid == b->uid && | |
663996b3 | 2715 | |
e735f4d4 MP |
2716 | a->gid_set == b->gid_set && |
2717 | a->gid == b->gid && | |
663996b3 | 2718 | |
e735f4d4 MP |
2719 | a->mode_set == b->mode_set && |
2720 | a->mode == b->mode && | |
663996b3 | 2721 | |
e735f4d4 MP |
2722 | a->age_set == b->age_set && |
2723 | a->age == b->age && | |
663996b3 | 2724 | |
8b3d4ff0 MB |
2725 | a->age_by_file == b->age_by_file && |
2726 | a->age_by_dir == b->age_by_dir && | |
2727 | ||
e735f4d4 | 2728 | a->mask_perms == b->mask_perms && |
663996b3 | 2729 | |
e735f4d4 | 2730 | a->keep_first_level == b->keep_first_level && |
663996b3 | 2731 | |
e735f4d4 | 2732 | a->major_minor == b->major_minor; |
663996b3 MS |
2733 | |
2734 | return true; | |
2735 | } | |
2736 | ||
14228c0d MB |
2737 | static bool should_include_path(const char *path) { |
2738 | char **prefix; | |
2739 | ||
60f067b4 | 2740 | STRV_FOREACH(prefix, arg_exclude_prefixes) |
e735f4d4 MP |
2741 | if (path_startswith(path, *prefix)) { |
2742 | log_debug("Entry \"%s\" matches exclude prefix \"%s\", skipping.", | |
2743 | path, *prefix); | |
14228c0d | 2744 | return false; |
e735f4d4 | 2745 | } |
14228c0d | 2746 | |
60f067b4 | 2747 | STRV_FOREACH(prefix, arg_include_prefixes) |
e735f4d4 MP |
2748 | if (path_startswith(path, *prefix)) { |
2749 | log_debug("Entry \"%s\" matches include prefix \"%s\".", path, *prefix); | |
14228c0d | 2750 | return true; |
e735f4d4 | 2751 | } |
14228c0d | 2752 | |
a10f5d05 | 2753 | /* no matches, so we should include this path only if we have no allow list at all */ |
52ad194e | 2754 | if (strv_isempty(arg_include_prefixes)) |
e735f4d4 MP |
2755 | return true; |
2756 | ||
2757 | log_debug("Entry \"%s\" does not match any include prefix, skipping.", path); | |
2758 | return false; | |
14228c0d MB |
2759 | } |
2760 | ||
52ad194e MB |
2761 | static int specifier_expansion_from_arg(Item *i) { |
2762 | _cleanup_free_ char *unescaped = NULL, *resolved = NULL; | |
2763 | char **xattr; | |
2764 | int r; | |
2765 | ||
2766 | assert(i); | |
2767 | ||
f2dec872 | 2768 | if (!i->argument) |
52ad194e MB |
2769 | return 0; |
2770 | ||
2771 | switch (i->type) { | |
2772 | case COPY_FILES: | |
2773 | case CREATE_SYMLINK: | |
2774 | case CREATE_FILE: | |
2775 | case TRUNCATE_FILE: | |
2776 | case WRITE_FILE: | |
2777 | r = cunescape(i->argument, 0, &unescaped); | |
2778 | if (r < 0) | |
2779 | return log_error_errno(r, "Failed to unescape parameter to write: %s", i->argument); | |
2780 | ||
8b3d4ff0 | 2781 | r = specifier_printf(unescaped, PATH_MAX-1, specifier_table, NULL, &resolved); |
52ad194e MB |
2782 | if (r < 0) |
2783 | return r; | |
2784 | ||
2785 | free_and_replace(i->argument, resolved); | |
2786 | break; | |
2787 | ||
2788 | case SET_XATTR: | |
2789 | case RECURSIVE_SET_XATTR: | |
a10f5d05 | 2790 | STRV_FOREACH(xattr, i->xattrs) { |
8b3d4ff0 | 2791 | r = specifier_printf(*xattr, SIZE_MAX, specifier_table, NULL, &resolved); |
52ad194e MB |
2792 | if (r < 0) |
2793 | return r; | |
2794 | ||
2795 | free_and_replace(*xattr, resolved); | |
2796 | } | |
2797 | break; | |
2798 | ||
2799 | default: | |
2800 | break; | |
2801 | } | |
2802 | return 0; | |
2803 | } | |
2804 | ||
b012e921 MB |
2805 | static int patch_var_run(const char *fname, unsigned line, char **path) { |
2806 | const char *k; | |
2807 | char *n; | |
2808 | ||
2809 | assert(path); | |
2810 | assert(*path); | |
2811 | ||
2812 | /* Optionally rewrites lines referencing /var/run/, to use /run/ instead. Why bother? tmpfiles merges lines in | |
2813 | * some cases and detects conflicts in others. If files/directories are specified through two equivalent lines | |
2814 | * this is problematic as neither case will be detected. Ideally we'd detect these cases by resolving symlinks | |
2815 | * early, but that's precisely not what we can do here as this code very likely is running very early on, at a | |
2816 | * time where the paths in question are not available yet, or even more importantly, our own tmpfiles rules | |
2817 | * might create the paths that are intermediary to the listed paths. We can't really cover the generic case, | |
2818 | * but the least we can do is cover the specific case of /var/run vs. /run, as /var/run is a legacy name for | |
2819 | * /run only, and we explicitly document that and require that on systemd systems the former is a symlink to | |
2820 | * the latter. Moreover files below this path are by far the primary usecase for tmpfiles.d/. */ | |
2821 | ||
2822 | k = path_startswith(*path, "/var/run/"); | |
2823 | if (isempty(k)) /* Don't complain about other paths than /var/run, and not about /var/run itself either. */ | |
2824 | return 0; | |
2825 | ||
f2dec872 | 2826 | n = path_join("/run", k); |
b012e921 MB |
2827 | if (!n) |
2828 | return log_oom(); | |
2829 | ||
2830 | /* Also log about this briefly. We do so at LOG_NOTICE level, as we fixed up the situation automatically, hence | |
2831 | * there's no immediate need for action by the user. However, in the interest of making things less confusing | |
2832 | * to the user, let's still inform the user that these snippets should really be updated. */ | |
5b5a102a MB |
2833 | log_syntax(NULL, LOG_NOTICE, fname, line, 0, |
2834 | "Line references path below legacy directory /var/run/, updating %s → %s; please update the tmpfiles.d/ drop-in file accordingly.", | |
2835 | *path, n); | |
b012e921 | 2836 | |
6e866b33 | 2837 | free_and_replace(*path, n); |
b012e921 MB |
2838 | |
2839 | return 0; | |
2840 | } | |
2841 | ||
a10f5d05 MB |
2842 | static int find_uid(const char *user, uid_t *ret_uid, Hashmap **cache) { |
2843 | int r; | |
2844 | ||
2845 | assert(user); | |
2846 | assert(ret_uid); | |
2847 | ||
2848 | /* First: parse as numeric UID string */ | |
2849 | r = parse_uid(user, ret_uid); | |
2850 | if (r >= 0) | |
2851 | return r; | |
2852 | ||
2853 | /* Second: pass to NSS if we are running "online" */ | |
2854 | if (!arg_root) | |
2855 | return get_user_creds(&user, ret_uid, NULL, NULL, NULL, 0); | |
2856 | ||
2857 | /* Third, synthesize "root" unconditionally */ | |
2858 | if (streq(user, "root")) { | |
2859 | *ret_uid = 0; | |
2860 | return 0; | |
2861 | } | |
2862 | ||
2863 | /* Fourth: use fgetpwent() to read /etc/passwd directly, if we are "offline" */ | |
2864 | return name_to_uid_offline(arg_root, user, ret_uid, cache); | |
2865 | } | |
2866 | ||
2867 | static int find_gid(const char *group, gid_t *ret_gid, Hashmap **cache) { | |
2868 | int r; | |
2869 | ||
2870 | assert(group); | |
2871 | assert(ret_gid); | |
2872 | ||
2873 | /* First: parse as numeric GID string */ | |
2874 | r = parse_gid(group, ret_gid); | |
2875 | if (r >= 0) | |
2876 | return r; | |
2877 | ||
2878 | /* Second: pass to NSS if we are running "online" */ | |
2879 | if (!arg_root) | |
2880 | return get_group_creds(&group, ret_gid, 0); | |
2881 | ||
2882 | /* Third, synthesize "root" unconditionally */ | |
2883 | if (streq(group, "root")) { | |
2884 | *ret_gid = 0; | |
2885 | return 0; | |
2886 | } | |
2887 | ||
2888 | /* Fourth: use fgetgrent() to read /etc/group directly, if we are "offline" */ | |
2889 | return name_to_gid_offline(arg_root, group, ret_gid, cache); | |
2890 | } | |
2891 | ||
8b3d4ff0 MB |
2892 | static int parse_age_by_from_arg(const char *age_by_str, Item *item) { |
2893 | AgeBy ab_f = 0, ab_d = 0; | |
2894 | ||
2895 | static const struct { | |
2896 | char age_by_chr; | |
2897 | AgeBy age_by_flag; | |
2898 | } age_by_types[] = { | |
2899 | { 'a', AGE_BY_ATIME }, | |
2900 | { 'b', AGE_BY_BTIME }, | |
2901 | { 'c', AGE_BY_CTIME }, | |
2902 | { 'm', AGE_BY_MTIME }, | |
2903 | }; | |
2904 | ||
2905 | assert(age_by_str); | |
2906 | assert(item); | |
2907 | ||
2908 | if (isempty(age_by_str)) | |
2909 | return -EINVAL; | |
2910 | ||
2911 | for (const char *s = age_by_str; *s != 0; s++) { | |
2912 | size_t i; | |
2913 | ||
2914 | /* Ignore whitespace. */ | |
2915 | if (strchr(WHITESPACE, *s)) | |
2916 | continue; | |
2917 | ||
2918 | for (i = 0; i < ELEMENTSOF(age_by_types); i++) { | |
2919 | /* Check lower-case for files, upper-case for directories. */ | |
2920 | if (*s == age_by_types[i].age_by_chr) { | |
2921 | ab_f |= age_by_types[i].age_by_flag; | |
2922 | break; | |
2923 | } else if (*s == ascii_toupper(age_by_types[i].age_by_chr)) { | |
2924 | ab_d |= age_by_types[i].age_by_flag; | |
2925 | break; | |
2926 | } | |
2927 | } | |
2928 | ||
2929 | /* Invalid character. */ | |
2930 | if (i >= ELEMENTSOF(age_by_types)) | |
2931 | return -EINVAL; | |
2932 | } | |
2933 | ||
2934 | /* No match. */ | |
2935 | if (ab_f == 0 && ab_d == 0) | |
2936 | return -EINVAL; | |
2937 | ||
2938 | item->age_by_file = ab_f > 0 ? ab_f : AGE_BY_DEFAULT_FILE; | |
2939 | item->age_by_dir = ab_d > 0 ? ab_d : AGE_BY_DEFAULT_DIR; | |
2940 | ||
2941 | return 0; | |
2942 | } | |
2943 | ||
a10f5d05 MB |
2944 | static int parse_line( |
2945 | const char *fname, | |
2946 | unsigned line, | |
2947 | const char *buffer, | |
2948 | bool *invalid_config, | |
2949 | Hashmap **uid_cache, | |
2950 | Hashmap **gid_cache) { | |
14228c0d | 2951 | |
60f067b4 | 2952 | _cleanup_free_ char *action = NULL, *mode = NULL, *user = NULL, *group = NULL, *age = NULL, *path = NULL; |
8b3d4ff0 MB |
2953 | _cleanup_(item_free_contents) Item i = { |
2954 | /* The "age-by" argument considers all file timestamp types by default. */ | |
2955 | .age_by_file = AGE_BY_DEFAULT_FILE, | |
2956 | .age_by_dir = AGE_BY_DEFAULT_DIR, | |
2957 | }; | |
e735f4d4 | 2958 | ItemArray *existing; |
e3bff60a MP |
2959 | OrderedHashmap *h; |
2960 | int r, pos; | |
8b3d4ff0 | 2961 | bool append_or_force = false, boot = false, allow_failure = false, try_replace = false; |
663996b3 MS |
2962 | |
2963 | assert(fname); | |
2964 | assert(line >= 1); | |
2965 | assert(buffer); | |
2966 | ||
13d276d0 | 2967 | r = extract_many_words( |
e3bff60a | 2968 | &buffer, |
13d276d0 | 2969 | NULL, |
f2dec872 | 2970 | EXTRACT_UNQUOTE, |
e3bff60a MP |
2971 | &action, |
2972 | &path, | |
2973 | &mode, | |
2974 | &user, | |
2975 | &group, | |
2976 | &age, | |
2977 | NULL); | |
52ad194e MB |
2978 | if (r < 0) { |
2979 | if (IN_SET(r, -EINVAL, -EBADSLT)) | |
2980 | /* invalid quoting and such or an unknown specifier */ | |
2981 | *invalid_config = true; | |
a10f5d05 | 2982 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Failed to parse line: %m"); |
b012e921 | 2983 | } else if (r < 2) { |
52ad194e | 2984 | *invalid_config = true; |
a10f5d05 | 2985 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "Syntax error."); |
663996b3 MS |
2986 | } |
2987 | ||
bb4f798a | 2988 | if (!empty_or_dash(buffer)) { |
e3bff60a MP |
2989 | i.argument = strdup(buffer); |
2990 | if (!i.argument) | |
2991 | return log_oom(); | |
2992 | } | |
2993 | ||
e842803a | 2994 | if (isempty(action)) { |
52ad194e | 2995 | *invalid_config = true; |
a10f5d05 | 2996 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "Command too short '%s'.", action); |
e842803a MB |
2997 | } |
2998 | ||
e735f4d4 MP |
2999 | for (pos = 1; action[pos]; pos++) { |
3000 | if (action[pos] == '!' && !boot) | |
3001 | boot = true; | |
e1f67bc7 MB |
3002 | else if (action[pos] == '+' && !append_or_force) |
3003 | append_or_force = true; | |
6e866b33 MB |
3004 | else if (action[pos] == '-' && !allow_failure) |
3005 | allow_failure = true; | |
8b3d4ff0 MB |
3006 | else if (action[pos] == '=' && !try_replace) |
3007 | try_replace = true; | |
e735f4d4 | 3008 | else { |
52ad194e | 3009 | *invalid_config = true; |
a10f5d05 | 3010 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "Unknown modifiers in command '%s'", action); |
e735f4d4 | 3011 | } |
e842803a MB |
3012 | } |
3013 | ||
e735f4d4 | 3014 | if (boot && !arg_boot) { |
a10f5d05 | 3015 | log_syntax(NULL, LOG_DEBUG, fname, line, 0, "Ignoring entry %s \"%s\" because --boot is not specified.", action, path); |
60f067b4 | 3016 | return 0; |
e735f4d4 | 3017 | } |
60f067b4 | 3018 | |
e735f4d4 | 3019 | i.type = action[0]; |
e1f67bc7 | 3020 | i.append_or_force = append_or_force; |
6e866b33 | 3021 | i.allow_failure = allow_failure; |
8b3d4ff0 | 3022 | i.try_replace = try_replace; |
e842803a | 3023 | |
8b3d4ff0 | 3024 | r = specifier_printf(path, PATH_MAX-1, specifier_table, NULL, &i.path); |
52ad194e MB |
3025 | if (r == -ENXIO) |
3026 | return log_unresolvable_specifier(fname, line); | |
14228c0d | 3027 | if (r < 0) { |
52ad194e MB |
3028 | if (IN_SET(r, -EINVAL, -EBADSLT)) |
3029 | *invalid_config = true; | |
a10f5d05 | 3030 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Failed to replace specifiers in '%s': %m", path); |
14228c0d MB |
3031 | } |
3032 | ||
b012e921 MB |
3033 | r = patch_var_run(fname, line, &i.path); |
3034 | if (r < 0) | |
3035 | return r; | |
3036 | ||
e735f4d4 | 3037 | switch (i.type) { |
663996b3 | 3038 | |
663996b3 | 3039 | case CREATE_DIRECTORY: |
e735f4d4 | 3040 | case CREATE_SUBVOLUME: |
db2df898 MP |
3041 | case CREATE_SUBVOLUME_INHERIT_QUOTA: |
3042 | case CREATE_SUBVOLUME_NEW_QUOTA: | |
aa27b158 | 3043 | case EMPTY_DIRECTORY: |
663996b3 MS |
3044 | case TRUNCATE_DIRECTORY: |
3045 | case CREATE_FIFO: | |
3046 | case IGNORE_PATH: | |
3047 | case IGNORE_DIRECTORY_PATH: | |
3048 | case REMOVE_PATH: | |
3049 | case RECURSIVE_REMOVE_PATH: | |
60f067b4 | 3050 | case ADJUST_MODE: |
663996b3 MS |
3051 | case RELABEL_PATH: |
3052 | case RECURSIVE_RELABEL_PATH: | |
e3bff60a | 3053 | if (i.argument) |
a10f5d05 | 3054 | log_syntax(NULL, LOG_WARNING, fname, line, 0, "%c lines don't take argument fields, ignoring.", i.type); |
e3bff60a MP |
3055 | |
3056 | break; | |
3057 | ||
3058 | case CREATE_FILE: | |
3059 | case TRUNCATE_FILE: | |
663996b3 MS |
3060 | break; |
3061 | ||
3062 | case CREATE_SYMLINK: | |
e735f4d4 | 3063 | if (!i.argument) { |
f2dec872 | 3064 | i.argument = path_join("/usr/share/factory", i.path); |
e735f4d4 | 3065 | if (!i.argument) |
e842803a | 3066 | return log_oom(); |
663996b3 MS |
3067 | } |
3068 | break; | |
3069 | ||
3070 | case WRITE_FILE: | |
e735f4d4 | 3071 | if (!i.argument) { |
52ad194e | 3072 | *invalid_config = true; |
a10f5d05 | 3073 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "Write file requires argument."); |
663996b3 MS |
3074 | } |
3075 | break; | |
3076 | ||
60f067b4 | 3077 | case COPY_FILES: |
e735f4d4 | 3078 | if (!i.argument) { |
a10f5d05 | 3079 | i.argument = path_join("/usr/share/factory", i.path); |
e735f4d4 | 3080 | if (!i.argument) |
e842803a | 3081 | return log_oom(); |
f2dec872 | 3082 | |
e735f4d4 | 3083 | } else if (!path_is_absolute(i.argument)) { |
52ad194e | 3084 | *invalid_config = true; |
a10f5d05 MB |
3085 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "Source path '%s' is not absolute.", i.argument); |
3086 | ||
3087 | } | |
f2dec872 | 3088 | |
a10f5d05 | 3089 | if (!empty_or_root(arg_root)) { |
f2dec872 BR |
3090 | char *p; |
3091 | ||
3092 | p = path_join(arg_root, i.argument); | |
3093 | if (!p) | |
3094 | return log_oom(); | |
3095 | free_and_replace(i.argument, p); | |
60f067b4 JS |
3096 | } |
3097 | ||
8b3d4ff0 | 3098 | path_simplify(i.argument); |
60f067b4 JS |
3099 | break; |
3100 | ||
663996b3 | 3101 | case CREATE_CHAR_DEVICE: |
6e866b33 | 3102 | case CREATE_BLOCK_DEVICE: |
e735f4d4 | 3103 | if (!i.argument) { |
52ad194e | 3104 | *invalid_config = true; |
a10f5d05 | 3105 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "Device file requires argument."); |
663996b3 MS |
3106 | } |
3107 | ||
6e866b33 MB |
3108 | r = parse_dev(i.argument, &i.major_minor); |
3109 | if (r < 0) { | |
52ad194e | 3110 | *invalid_config = true; |
a10f5d05 | 3111 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Can't parse device file major/minor '%s'.", i.argument); |
663996b3 MS |
3112 | } |
3113 | ||
663996b3 | 3114 | break; |
663996b3 | 3115 | |
f47781d8 | 3116 | case SET_XATTR: |
e735f4d4 MP |
3117 | case RECURSIVE_SET_XATTR: |
3118 | if (!i.argument) { | |
52ad194e | 3119 | *invalid_config = true; |
a10f5d05 MB |
3120 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), |
3121 | "Set extended attribute requires argument."); | |
f47781d8 | 3122 | } |
e3bff60a | 3123 | r = parse_xattrs_from_arg(&i); |
e735f4d4 MP |
3124 | if (r < 0) |
3125 | return r; | |
3126 | break; | |
3127 | ||
3128 | case SET_ACL: | |
3129 | case RECURSIVE_SET_ACL: | |
3130 | if (!i.argument) { | |
52ad194e | 3131 | *invalid_config = true; |
a10f5d05 MB |
3132 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), |
3133 | "Set ACLs requires argument."); | |
e735f4d4 | 3134 | } |
e3bff60a MP |
3135 | r = parse_acls_from_arg(&i); |
3136 | if (r < 0) | |
3137 | return r; | |
3138 | break; | |
3139 | ||
3140 | case SET_ATTRIBUTE: | |
3141 | case RECURSIVE_SET_ATTRIBUTE: | |
3142 | if (!i.argument) { | |
52ad194e | 3143 | *invalid_config = true; |
a10f5d05 MB |
3144 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), |
3145 | "Set file attribute requires argument."); | |
e3bff60a MP |
3146 | } |
3147 | r = parse_attribute_from_arg(&i); | |
52ad194e MB |
3148 | if (IN_SET(r, -EINVAL, -EBADSLT)) |
3149 | *invalid_config = true; | |
f47781d8 MP |
3150 | if (r < 0) |
3151 | return r; | |
3152 | break; | |
3153 | ||
663996b3 | 3154 | default: |
52ad194e | 3155 | *invalid_config = true; |
a10f5d05 MB |
3156 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), |
3157 | "Unknown command type '%c'.", (char) i.type); | |
663996b3 MS |
3158 | } |
3159 | ||
e735f4d4 | 3160 | if (!path_is_absolute(i.path)) { |
52ad194e | 3161 | *invalid_config = true; |
a10f5d05 MB |
3162 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), |
3163 | "Path '%s' not absolute.", i.path); | |
663996b3 MS |
3164 | } |
3165 | ||
8b3d4ff0 | 3166 | path_simplify(i.path); |
663996b3 | 3167 | |
e735f4d4 | 3168 | if (!should_include_path(i.path)) |
663996b3 MS |
3169 | return 0; |
3170 | ||
52ad194e MB |
3171 | r = specifier_expansion_from_arg(&i); |
3172 | if (r == -ENXIO) | |
3173 | return log_unresolvable_specifier(fname, line); | |
3174 | if (r < 0) { | |
3175 | if (IN_SET(r, -EINVAL, -EBADSLT)) | |
3176 | *invalid_config = true; | |
a10f5d05 | 3177 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Failed to substitute specifiers in argument: %m"); |
52ad194e MB |
3178 | } |
3179 | ||
a10f5d05 | 3180 | if (!empty_or_root(arg_root)) { |
60f067b4 JS |
3181 | char *p; |
3182 | ||
f2dec872 | 3183 | p = path_join(arg_root, i.path); |
60f067b4 JS |
3184 | if (!p) |
3185 | return log_oom(); | |
6e866b33 | 3186 | free_and_replace(i.path, p); |
60f067b4 JS |
3187 | } |
3188 | ||
bb4f798a | 3189 | if (!empty_or_dash(user)) { |
a10f5d05 | 3190 | r = find_uid(user, &i.uid, uid_cache); |
663996b3 | 3191 | if (r < 0) { |
52ad194e | 3192 | *invalid_config = true; |
a10f5d05 | 3193 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Failed to resolve user '%s': %m", user); |
663996b3 MS |
3194 | } |
3195 | ||
e735f4d4 | 3196 | i.uid_set = true; |
663996b3 MS |
3197 | } |
3198 | ||
bb4f798a | 3199 | if (!empty_or_dash(group)) { |
a10f5d05 | 3200 | r = find_gid(group, &i.gid, gid_cache); |
663996b3 | 3201 | if (r < 0) { |
52ad194e | 3202 | *invalid_config = true; |
a10f5d05 | 3203 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Failed to resolve group '%s'.", group); |
663996b3 MS |
3204 | } |
3205 | ||
e735f4d4 | 3206 | i.gid_set = true; |
663996b3 MS |
3207 | } |
3208 | ||
bb4f798a | 3209 | if (!empty_or_dash(mode)) { |
60f067b4 | 3210 | const char *mm = mode; |
663996b3 MS |
3211 | unsigned m; |
3212 | ||
60f067b4 | 3213 | if (*mm == '~') { |
e735f4d4 | 3214 | i.mask_perms = true; |
60f067b4 JS |
3215 | mm++; |
3216 | } | |
3217 | ||
a10f5d05 MB |
3218 | r = parse_mode(mm, &m); |
3219 | if (r < 0) { | |
52ad194e | 3220 | *invalid_config = true; |
a10f5d05 | 3221 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Invalid mode '%s'.", mode); |
663996b3 MS |
3222 | } |
3223 | ||
e735f4d4 MP |
3224 | i.mode = m; |
3225 | i.mode_set = true; | |
663996b3 | 3226 | } else |
db2df898 | 3227 | i.mode = IN_SET(i.type, CREATE_DIRECTORY, TRUNCATE_DIRECTORY, CREATE_SUBVOLUME, CREATE_SUBVOLUME_INHERIT_QUOTA, CREATE_SUBVOLUME_NEW_QUOTA) ? 0755 : 0644; |
663996b3 | 3228 | |
bb4f798a | 3229 | if (!empty_or_dash(age)) { |
663996b3 | 3230 | const char *a = age; |
8b3d4ff0 | 3231 | _cleanup_free_ char *seconds = NULL, *age_by = NULL; |
663996b3 MS |
3232 | |
3233 | if (*a == '~') { | |
e735f4d4 | 3234 | i.keep_first_level = true; |
663996b3 MS |
3235 | a++; |
3236 | } | |
3237 | ||
8b3d4ff0 MB |
3238 | /* Format: "age-by:age"; where age-by is "[abcmABCM]+". */ |
3239 | r = split_pair(a, ":", &age_by, &seconds); | |
3240 | if (r == -ENOMEM) | |
3241 | return log_oom(); | |
3242 | if (r < 0 && r != -EINVAL) | |
3243 | return log_error_errno(r, "Failed to parse age-by for '%s': %m", age); | |
3244 | if (r >= 0) { | |
3245 | /* We found a ":", parse the "age-by" part. */ | |
3246 | r = parse_age_by_from_arg(age_by, &i); | |
3247 | if (r == -ENOMEM) | |
3248 | return log_oom(); | |
3249 | if (r < 0) { | |
3250 | *invalid_config = true; | |
3251 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Invalid age-by '%s'.", age_by); | |
3252 | } | |
3253 | ||
3254 | /* For parsing the "age" part, after the ":". */ | |
3255 | a = seconds; | |
3256 | } | |
3257 | ||
a10f5d05 MB |
3258 | r = parse_sec(a, &i.age); |
3259 | if (r < 0) { | |
52ad194e | 3260 | *invalid_config = true; |
8b3d4ff0 | 3261 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Invalid age '%s'.", a); |
663996b3 MS |
3262 | } |
3263 | ||
e735f4d4 | 3264 | i.age_set = true; |
663996b3 MS |
3265 | } |
3266 | ||
e735f4d4 | 3267 | h = needs_glob(i.type) ? globs : items; |
663996b3 | 3268 | |
e3bff60a | 3269 | existing = ordered_hashmap_get(h, i.path); |
663996b3 | 3270 | if (existing) { |
6e866b33 | 3271 | size_t n; |
e735f4d4 | 3272 | |
6e866b33 | 3273 | for (n = 0; n < existing->n_items; n++) { |
e1f67bc7 | 3274 | if (!item_compatible(existing->items + n, &i) && !i.append_or_force) { |
a10f5d05 | 3275 | log_syntax(NULL, LOG_NOTICE, fname, line, 0, "Duplicate line for path \"%s\", ignoring.", i.path); |
e3bff60a MP |
3276 | return 0; |
3277 | } | |
f47781d8 MP |
3278 | } |
3279 | } else { | |
e735f4d4 | 3280 | existing = new0(ItemArray, 1); |
1d42b86d MB |
3281 | if (!existing) |
3282 | return log_oom(); | |
3283 | ||
e3bff60a | 3284 | r = ordered_hashmap_put(h, i.path, existing); |
6e866b33 MB |
3285 | if (r < 0) { |
3286 | free(existing); | |
e735f4d4 | 3287 | return log_oom(); |
6e866b33 | 3288 | } |
663996b3 MS |
3289 | } |
3290 | ||
8b3d4ff0 | 3291 | if (!GREEDY_REALLOC(existing->items, existing->n_items + 1)) |
e735f4d4 | 3292 | return log_oom(); |
663996b3 | 3293 | |
6e866b33 MB |
3294 | existing->items[existing->n_items++] = i; |
3295 | i = (struct Item) {}; | |
e3bff60a MP |
3296 | |
3297 | /* Sort item array, to enforce stable ordering of application */ | |
6e866b33 | 3298 | typesafe_qsort(existing->items, existing->n_items, item_compare); |
e3bff60a | 3299 | |
663996b3 MS |
3300 | return 0; |
3301 | } | |
3302 | ||
b012e921 MB |
3303 | static int cat_config(char **config_dirs, char **args) { |
3304 | _cleanup_strv_free_ char **files = NULL; | |
3305 | int r; | |
3306 | ||
3307 | r = conf_files_list_with_replacement(arg_root, config_dirs, arg_replace, &files, NULL); | |
3308 | if (r < 0) | |
3309 | return r; | |
3310 | ||
3311 | return cat_files(NULL, files, 0); | |
3312 | } | |
3313 | ||
a032b68d MB |
3314 | static int exclude_default_prefixes(void) { |
3315 | int r; | |
3316 | ||
3317 | /* Provide an easy way to exclude virtual/memory file systems from what we do here. Useful in | |
3318 | * combination with --root= where we probably don't want to apply stuff to these dirs as they are | |
3319 | * likely over-mounted if the root directory is actually used, and it wouldbe less than ideal to have | |
3320 | * all kinds of files created/adjusted underneath these mount points. */ | |
3321 | ||
3322 | r = strv_extend_strv( | |
3323 | &arg_exclude_prefixes, | |
3324 | STRV_MAKE("/dev", | |
3325 | "/proc", | |
3326 | "/run", | |
3327 | "/sys"), | |
3328 | true); | |
3329 | if (r < 0) | |
3330 | return log_oom(); | |
3331 | ||
3332 | return 0; | |
3333 | } | |
3334 | ||
6e866b33 MB |
3335 | static int help(void) { |
3336 | _cleanup_free_ char *link = NULL; | |
3337 | int r; | |
3338 | ||
3339 | r = terminal_urlify_man("systemd-tmpfiles", "8", &link); | |
3340 | if (r < 0) | |
3341 | return log_oom(); | |
3342 | ||
3a6ce677 BR |
3343 | printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n" |
3344 | "\n%sCreates, deletes and cleans up volatile and temporary files and directories.%s\n\n" | |
14228c0d | 3345 | " -h --help Show this help\n" |
52ad194e | 3346 | " --user Execute user configuration\n" |
60f067b4 | 3347 | " --version Show package version\n" |
b012e921 | 3348 | " --cat-config Show configuration files\n" |
14228c0d MB |
3349 | " --create Create marked files/directories\n" |
3350 | " --clean Clean up marked directories\n" | |
3351 | " --remove Remove marked files/directories\n" | |
60f067b4 | 3352 | " --boot Execute actions only safe at boot\n" |
e735f4d4 MP |
3353 | " --prefix=PATH Only apply rules with the specified prefix\n" |
3354 | " --exclude-prefix=PATH Ignore rules with the specified prefix\n" | |
a032b68d | 3355 | " -E Ignore rules prefixed with /dev, /proc, /run, /sys\n" |
52ad194e | 3356 | " --root=PATH Operate on an alternate filesystem root\n" |
a032b68d | 3357 | " --image=PATH Operate on disk image as filesystem root\n" |
98393f85 | 3358 | " --replace=PATH Treat arguments as replacement for PATH\n" |
b012e921 | 3359 | " --no-pager Do not pipe output into a pager\n" |
3a6ce677 BR |
3360 | "\nSee the %s for details.\n", |
3361 | program_invocation_short_name, | |
3362 | ansi_highlight(), | |
3363 | ansi_normal(), | |
3364 | link); | |
6e866b33 MB |
3365 | |
3366 | return 0; | |
663996b3 MS |
3367 | } |
3368 | ||
3369 | static int parse_argv(int argc, char *argv[]) { | |
3370 | ||
3371 | enum { | |
60f067b4 | 3372 | ARG_VERSION = 0x100, |
b012e921 | 3373 | ARG_CAT_CONFIG, |
52ad194e | 3374 | ARG_USER, |
663996b3 MS |
3375 | ARG_CREATE, |
3376 | ARG_CLEAN, | |
3377 | ARG_REMOVE, | |
60f067b4 | 3378 | ARG_BOOT, |
14228c0d MB |
3379 | ARG_PREFIX, |
3380 | ARG_EXCLUDE_PREFIX, | |
60f067b4 | 3381 | ARG_ROOT, |
a032b68d | 3382 | ARG_IMAGE, |
98393f85 | 3383 | ARG_REPLACE, |
b012e921 | 3384 | ARG_NO_PAGER, |
663996b3 MS |
3385 | }; |
3386 | ||
3387 | static const struct option options[] = { | |
14228c0d | 3388 | { "help", no_argument, NULL, 'h' }, |
52ad194e | 3389 | { "user", no_argument, NULL, ARG_USER }, |
60f067b4 | 3390 | { "version", no_argument, NULL, ARG_VERSION }, |
b012e921 | 3391 | { "cat-config", no_argument, NULL, ARG_CAT_CONFIG }, |
14228c0d MB |
3392 | { "create", no_argument, NULL, ARG_CREATE }, |
3393 | { "clean", no_argument, NULL, ARG_CLEAN }, | |
3394 | { "remove", no_argument, NULL, ARG_REMOVE }, | |
60f067b4 | 3395 | { "boot", no_argument, NULL, ARG_BOOT }, |
14228c0d MB |
3396 | { "prefix", required_argument, NULL, ARG_PREFIX }, |
3397 | { "exclude-prefix", required_argument, NULL, ARG_EXCLUDE_PREFIX }, | |
60f067b4 | 3398 | { "root", required_argument, NULL, ARG_ROOT }, |
a032b68d | 3399 | { "image", required_argument, NULL, ARG_IMAGE }, |
98393f85 | 3400 | { "replace", required_argument, NULL, ARG_REPLACE }, |
b012e921 | 3401 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, |
60f067b4 | 3402 | {} |
663996b3 MS |
3403 | }; |
3404 | ||
db2df898 | 3405 | int c, r; |
663996b3 MS |
3406 | |
3407 | assert(argc >= 0); | |
3408 | assert(argv); | |
3409 | ||
a032b68d | 3410 | while ((c = getopt_long(argc, argv, "hE", options, NULL)) >= 0) |
663996b3 MS |
3411 | |
3412 | switch (c) { | |
3413 | ||
3414 | case 'h': | |
6e866b33 | 3415 | return help(); |
60f067b4 JS |
3416 | |
3417 | case ARG_VERSION: | |
6300502b | 3418 | return version(); |
663996b3 | 3419 | |
b012e921 MB |
3420 | case ARG_CAT_CONFIG: |
3421 | arg_cat_config = true; | |
3422 | break; | |
3423 | ||
52ad194e MB |
3424 | case ARG_USER: |
3425 | arg_user = true; | |
3426 | break; | |
3427 | ||
663996b3 | 3428 | case ARG_CREATE: |
6e866b33 | 3429 | arg_operation |= OPERATION_CREATE; |
663996b3 MS |
3430 | break; |
3431 | ||
3432 | case ARG_CLEAN: | |
6e866b33 | 3433 | arg_operation |= OPERATION_CLEAN; |
663996b3 MS |
3434 | break; |
3435 | ||
3436 | case ARG_REMOVE: | |
6e866b33 | 3437 | arg_operation |= OPERATION_REMOVE; |
663996b3 MS |
3438 | break; |
3439 | ||
60f067b4 JS |
3440 | case ARG_BOOT: |
3441 | arg_boot = true; | |
3442 | break; | |
3443 | ||
663996b3 | 3444 | case ARG_PREFIX: |
60f067b4 | 3445 | if (strv_push(&arg_include_prefixes, optarg) < 0) |
14228c0d MB |
3446 | return log_oom(); |
3447 | break; | |
3448 | ||
3449 | case ARG_EXCLUDE_PREFIX: | |
60f067b4 | 3450 | if (strv_push(&arg_exclude_prefixes, optarg) < 0) |
14228c0d | 3451 | return log_oom(); |
663996b3 MS |
3452 | break; |
3453 | ||
60f067b4 | 3454 | case ARG_ROOT: |
3a6ce677 | 3455 | r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_root); |
db2df898 MP |
3456 | if (r < 0) |
3457 | return r; | |
60f067b4 JS |
3458 | break; |
3459 | ||
a032b68d MB |
3460 | case ARG_IMAGE: |
3461 | #ifdef STANDALONE | |
3462 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), | |
3463 | "This systemd-tmpfiles version is compiled without support for --image=."); | |
3464 | #else | |
3a6ce677 | 3465 | r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_image); |
a032b68d MB |
3466 | if (r < 0) |
3467 | return r; | |
3468 | #endif | |
3469 | /* Imply -E here since it makes little sense to create files persistently in the /run mountpoint of a disk image */ | |
3470 | _fallthrough_; | |
3471 | ||
3472 | case 'E': | |
3473 | r = exclude_default_prefixes(); | |
3474 | if (r < 0) | |
3475 | return r; | |
3476 | ||
3477 | break; | |
3478 | ||
98393f85 MB |
3479 | case ARG_REPLACE: |
3480 | if (!path_is_absolute(optarg) || | |
6e866b33 MB |
3481 | !endswith(optarg, ".conf")) |
3482 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3483 | "The argument to --replace= must an absolute path to a config file"); | |
98393f85 MB |
3484 | |
3485 | arg_replace = optarg; | |
3486 | break; | |
3487 | ||
b012e921 | 3488 | case ARG_NO_PAGER: |
6e866b33 | 3489 | arg_pager_flags |= PAGER_DISABLE; |
b012e921 MB |
3490 | break; |
3491 | ||
663996b3 MS |
3492 | case '?': |
3493 | return -EINVAL; | |
3494 | ||
3495 | default: | |
60f067b4 | 3496 | assert_not_reached("Unhandled option"); |
663996b3 | 3497 | } |
663996b3 | 3498 | |
6e866b33 MB |
3499 | if (arg_operation == 0 && !arg_cat_config) |
3500 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3501 | "You need to specify at least one of --clean, --create or --remove."); | |
663996b3 | 3502 | |
6e866b33 MB |
3503 | if (arg_replace && arg_cat_config) |
3504 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3505 | "Option --replace= is not supported with --cat-config"); | |
b012e921 | 3506 | |
6e866b33 MB |
3507 | if (arg_replace && optind >= argc) |
3508 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3509 | "When --replace= is given, some configuration items must be specified"); | |
98393f85 | 3510 | |
a032b68d MB |
3511 | if (arg_root && arg_user) |
3512 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3513 | "Combination of --user and --root= is not supported."); | |
3514 | ||
3515 | if (arg_image && arg_root) | |
3516 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported."); | |
3517 | ||
663996b3 MS |
3518 | return 1; |
3519 | } | |
3520 | ||
98393f85 | 3521 | static int read_config_file(char **config_dirs, const char *fn, bool ignore_enoent, bool *invalid_config) { |
a10f5d05 | 3522 | _cleanup_(hashmap_freep) Hashmap *uid_cache = NULL, *gid_cache = NULL; |
aa27b158 | 3523 | _cleanup_fclose_ FILE *_f = NULL; |
8b3d4ff0 | 3524 | _cleanup_free_ char *pp = NULL; |
14228c0d | 3525 | unsigned v = 0; |
6e866b33 | 3526 | FILE *f; |
5b5a102a | 3527 | ItemArray *ia; |
5a920b42 | 3528 | int r = 0; |
663996b3 MS |
3529 | |
3530 | assert(fn); | |
3531 | ||
aa27b158 | 3532 | if (streq(fn, "-")) { |
98393f85 | 3533 | log_debug("Reading config from stdin…"); |
aa27b158 MP |
3534 | fn = "<stdin>"; |
3535 | f = stdin; | |
3536 | } else { | |
8b3d4ff0 | 3537 | r = search_and_fopen(fn, "re", arg_root, (const char**) config_dirs, &_f, &pp); |
aa27b158 MP |
3538 | if (r < 0) { |
3539 | if (ignore_enoent && r == -ENOENT) { | |
3540 | log_debug_errno(r, "Failed to open \"%s\", ignoring: %m", fn); | |
3541 | return 0; | |
3542 | } | |
663996b3 | 3543 | |
aa27b158 MP |
3544 | return log_error_errno(r, "Failed to open '%s': %m", fn); |
3545 | } | |
8b3d4ff0 MB |
3546 | |
3547 | log_debug("Reading config file \"%s\"…", pp); | |
3548 | fn = pp; | |
aa27b158 | 3549 | f = _f; |
663996b3 MS |
3550 | } |
3551 | ||
6e866b33 MB |
3552 | for (;;) { |
3553 | _cleanup_free_ char *line = NULL; | |
3554 | bool invalid_line = false; | |
14228c0d | 3555 | char *l; |
663996b3 | 3556 | int k; |
6e866b33 MB |
3557 | |
3558 | k = read_line(f, LONG_LINE_MAX, &line); | |
3559 | if (k < 0) | |
3560 | return log_error_errno(k, "Failed to read '%s': %m", fn); | |
3561 | if (k == 0) | |
3562 | break; | |
663996b3 | 3563 | |
663996b3 MS |
3564 | v++; |
3565 | ||
3566 | l = strstrip(line); | |
f5e65279 | 3567 | if (IN_SET(*l, 0, '#')) |
663996b3 MS |
3568 | continue; |
3569 | ||
a10f5d05 | 3570 | k = parse_line(fn, v, l, &invalid_line, &uid_cache, &gid_cache); |
52ad194e MB |
3571 | if (k < 0) { |
3572 | if (invalid_line) | |
3573 | /* Allow reporting with a special code if the caller requested this */ | |
3574 | *invalid_config = true; | |
3575 | else if (r == 0) | |
3576 | /* The first error becomes our return value */ | |
3577 | r = k; | |
3578 | } | |
663996b3 MS |
3579 | } |
3580 | ||
3581 | /* we have to determine age parameter for each entry of type X */ | |
5b5a102a MB |
3582 | ORDERED_HASHMAP_FOREACH(ia, globs) |
3583 | for (size_t ni = 0; ni < ia->n_items; ni++) { | |
3584 | ItemArray *ja; | |
3585 | Item *i = ia->items + ni, *candidate_item = NULL; | |
663996b3 | 3586 | |
5b5a102a | 3587 | if (i->type != IGNORE_DIRECTORY_PATH) |
663996b3 MS |
3588 | continue; |
3589 | ||
5b5a102a MB |
3590 | ORDERED_HASHMAP_FOREACH(ja, items) |
3591 | for (size_t nj = 0; nj < ja->n_items; nj++) { | |
3592 | Item *j = ja->items + nj; | |
663996b3 | 3593 | |
8b3d4ff0 MB |
3594 | if (!IN_SET(j->type, CREATE_DIRECTORY, |
3595 | TRUNCATE_DIRECTORY, | |
3596 | CREATE_SUBVOLUME, | |
3597 | CREATE_SUBVOLUME_INHERIT_QUOTA, | |
3598 | CREATE_SUBVOLUME_NEW_QUOTA)) | |
5b5a102a MB |
3599 | continue; |
3600 | ||
3601 | if (path_equal(j->path, i->path)) { | |
3602 | candidate_item = j; | |
3603 | break; | |
3604 | } | |
663996b3 | 3605 | |
8b3d4ff0 MB |
3606 | if (candidate_item |
3607 | ? (path_startswith(j->path, candidate_item->path) && fnmatch(i->path, j->path, FNM_PATHNAME | FNM_PERIOD) == 0) | |
3608 | : path_startswith(i->path, j->path) != NULL) | |
5b5a102a MB |
3609 | candidate_item = j; |
3610 | } | |
3611 | ||
3612 | if (candidate_item && candidate_item->age_set) { | |
3613 | i->age = candidate_item->age; | |
3614 | i->age_set = true; | |
3615 | } | |
663996b3 | 3616 | } |
663996b3 MS |
3617 | |
3618 | if (ferror(f)) { | |
f47781d8 | 3619 | log_error_errno(errno, "Failed to read from file %s: %m", fn); |
663996b3 MS |
3620 | if (r == 0) |
3621 | r = -EIO; | |
3622 | } | |
3623 | ||
663996b3 MS |
3624 | return r; |
3625 | } | |
3626 | ||
98393f85 MB |
3627 | static int parse_arguments(char **config_dirs, char **args, bool *invalid_config) { |
3628 | char **arg; | |
3629 | int r; | |
3630 | ||
3631 | STRV_FOREACH(arg, args) { | |
3632 | r = read_config_file(config_dirs, *arg, false, invalid_config); | |
3633 | if (r < 0) | |
3634 | return r; | |
3635 | } | |
3636 | ||
3637 | return 0; | |
3638 | } | |
3639 | ||
3640 | static int read_config_files(char **config_dirs, char **args, bool *invalid_config) { | |
3641 | _cleanup_strv_free_ char **files = NULL; | |
3642 | _cleanup_free_ char *p = NULL; | |
3643 | char **f; | |
3644 | int r; | |
3645 | ||
b012e921 | 3646 | r = conf_files_list_with_replacement(arg_root, config_dirs, arg_replace, &files, &p); |
98393f85 | 3647 | if (r < 0) |
b012e921 | 3648 | return r; |
98393f85 MB |
3649 | |
3650 | STRV_FOREACH(f, files) | |
3651 | if (p && path_equal(*f, p)) { | |
3652 | log_debug("Parsing arguments at position \"%s\"…", *f); | |
3653 | ||
3654 | r = parse_arguments(config_dirs, args, invalid_config); | |
3655 | if (r < 0) | |
3656 | return r; | |
3657 | } else | |
3658 | /* Just warn, ignore result otherwise. | |
3659 | * read_config_file() has some debug output, so no need to print anything. */ | |
3660 | (void) read_config_file(config_dirs, *f, true, invalid_config); | |
3661 | ||
3662 | return 0; | |
3663 | } | |
3664 | ||
6e866b33 MB |
3665 | static int link_parent(ItemArray *a) { |
3666 | const char *path; | |
3667 | char *prefix; | |
3668 | int r; | |
3669 | ||
3670 | assert(a); | |
3671 | ||
3672 | /* Finds the closest "parent" item array for the specified item array. Then registers the specified item array | |
3673 | * as child of it, and fills the parent in, linking them both ways. This allows us to later create parents | |
3674 | * before their children, and clean up/remove children before their parents. */ | |
3675 | ||
3676 | if (a->n_items <= 0) | |
3677 | return 0; | |
3678 | ||
3679 | path = a->items[0].path; | |
7c20daf6 | 3680 | prefix = newa(char, strlen(path) + 1); |
6e866b33 MB |
3681 | PATH_FOREACH_PREFIX(prefix, path) { |
3682 | ItemArray *j; | |
3683 | ||
3684 | j = ordered_hashmap_get(items, prefix); | |
3685 | if (!j) | |
3686 | j = ordered_hashmap_get(globs, prefix); | |
3687 | if (j) { | |
a10f5d05 | 3688 | r = set_ensure_put(&j->children, NULL, a); |
6e866b33 MB |
3689 | if (r < 0) |
3690 | return log_oom(); | |
3691 | ||
3692 | a->parent = j; | |
3693 | return 1; | |
3694 | } | |
3695 | } | |
3696 | ||
3697 | return 0; | |
3698 | } | |
3699 | ||
3700 | DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(item_array_hash_ops, char, string_hash_func, string_compare_func, | |
3701 | ItemArray, item_array_free); | |
3702 | ||
3703 | static int run(int argc, char *argv[]) { | |
a032b68d MB |
3704 | #ifndef STANDALONE |
3705 | _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL; | |
3706 | _cleanup_(decrypted_image_unrefp) DecryptedImage *decrypted_image = NULL; | |
3707 | _cleanup_(umount_and_rmdir_and_freep) char *unlink_dir = NULL; | |
3708 | #endif | |
52ad194e MB |
3709 | _cleanup_strv_free_ char **config_dirs = NULL; |
3710 | bool invalid_config = false; | |
6e866b33 MB |
3711 | ItemArray *a; |
3712 | enum { | |
3713 | PHASE_REMOVE_AND_CLEAN, | |
3714 | PHASE_CREATE, | |
3715 | _PHASE_MAX | |
3716 | } phase; | |
3717 | int r, k; | |
663996b3 MS |
3718 | |
3719 | r = parse_argv(argc, argv); | |
3720 | if (r <= 0) | |
6e866b33 | 3721 | return r; |
663996b3 | 3722 | |
3a6ce677 BR |
3723 | log_setup(); |
3724 | ||
3725 | /* We require /proc/ for a lot of our operations, i.e. for adjusting access modes, for anything | |
3726 | * SELinux related, for recursive operation, for xattr, acl and chattr handling, for btrfs stuff and | |
3727 | * a lot more. It's probably the majority of invocations where /proc/ is required. Since people | |
3728 | * apparently invoke it without anyway and are surprised about the failures, let's catch this early | |
3729 | * and output a nice and friendly warning. */ | |
3730 | if (proc_mounted() == 0) | |
3731 | return log_error_errno(SYNTHETIC_ERRNO(ENOSYS), | |
3732 | "/proc/ is not mounted, but required for successful operation of systemd-tmpfiles. " | |
3733 | "Please mount /proc/. Alternatively, consider using the --root= or --image= switches."); | |
663996b3 | 3734 | |
bb4f798a MB |
3735 | /* Descending down file system trees might take a lot of fds */ |
3736 | (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE); | |
3737 | ||
52ad194e MB |
3738 | if (arg_user) { |
3739 | r = user_config_paths(&config_dirs); | |
6e866b33 MB |
3740 | if (r < 0) |
3741 | return log_error_errno(r, "Failed to initialize configuration directory list: %m"); | |
52ad194e MB |
3742 | } else { |
3743 | config_dirs = strv_split_nulstr(CONF_PATHS_NULSTR("tmpfiles.d")); | |
6e866b33 MB |
3744 | if (!config_dirs) |
3745 | return log_oom(); | |
52ad194e MB |
3746 | } |
3747 | ||
1d42b86d | 3748 | if (DEBUG_LOGGING) { |
52ad194e | 3749 | _cleanup_free_ char *t = NULL; |
a032b68d MB |
3750 | char **i; |
3751 | ||
3752 | STRV_FOREACH(i, config_dirs) { | |
3753 | _cleanup_free_ char *j = NULL; | |
3754 | ||
3755 | j = path_join(arg_root, *i); | |
3756 | if (!j) | |
3757 | return log_oom(); | |
52ad194e | 3758 | |
3a6ce677 | 3759 | if (!strextend(&t, "\n\t", j)) |
a032b68d MB |
3760 | return log_oom(); |
3761 | } | |
3762 | ||
3763 | log_debug("Looking for configuration files in (higher priority first):%s", t); | |
b012e921 MB |
3764 | } |
3765 | ||
3766 | if (arg_cat_config) { | |
6e866b33 | 3767 | (void) pager_open(arg_pager_flags); |
b012e921 | 3768 | |
6e866b33 | 3769 | return cat_config(config_dirs, argv + optind); |
b012e921 MB |
3770 | } |
3771 | ||
3772 | umask(0022); | |
3773 | ||
a10f5d05 MB |
3774 | r = mac_selinux_init(); |
3775 | if (r < 0) | |
3776 | return r; | |
b012e921 | 3777 | |
a032b68d MB |
3778 | #ifndef STANDALONE |
3779 | if (arg_image) { | |
3780 | assert(!arg_root); | |
3781 | ||
3782 | r = mount_image_privately_interactively( | |
3783 | arg_image, | |
8b3d4ff0 MB |
3784 | DISSECT_IMAGE_GENERIC_ROOT | |
3785 | DISSECT_IMAGE_REQUIRE_ROOT | | |
3786 | DISSECT_IMAGE_VALIDATE_OS | | |
3787 | DISSECT_IMAGE_RELAX_VAR_CHECK | | |
3788 | DISSECT_IMAGE_FSCK | | |
3789 | DISSECT_IMAGE_GROWFS, | |
a032b68d MB |
3790 | &unlink_dir, |
3791 | &loop_device, | |
3792 | &decrypted_image); | |
3793 | if (r < 0) | |
3794 | return r; | |
3795 | ||
3796 | arg_root = strdup(unlink_dir); | |
3797 | if (!arg_root) | |
3798 | return log_oom(); | |
3799 | } | |
3800 | #else | |
3801 | assert(!arg_image); | |
3802 | #endif | |
3803 | ||
6e866b33 MB |
3804 | items = ordered_hashmap_new(&item_array_hash_ops); |
3805 | globs = ordered_hashmap_new(&item_array_hash_ops); | |
3806 | if (!items || !globs) | |
3807 | return log_oom(); | |
52ad194e | 3808 | |
98393f85 MB |
3809 | /* If command line arguments are specified along with --replace, read all |
3810 | * configuration files and insert the positional arguments at the specified | |
3811 | * place. Otherwise, if command line arguments are specified, execute just | |
3812 | * them, and finally, without --replace= or any positional arguments, just | |
3813 | * read configuration and execute it. | |
3814 | */ | |
3815 | if (arg_replace || optind >= argc) | |
3816 | r = read_config_files(config_dirs, argv + optind, &invalid_config); | |
3817 | else | |
3818 | r = parse_arguments(config_dirs, argv + optind, &invalid_config); | |
3819 | if (r < 0) | |
6e866b33 | 3820 | return r; |
663996b3 | 3821 | |
6e866b33 | 3822 | /* Let's now link up all child/parent relationships */ |
a032b68d | 3823 | ORDERED_HASHMAP_FOREACH(a, items) { |
6e866b33 MB |
3824 | r = link_parent(a); |
3825 | if (r < 0) | |
3826 | return r; | |
e735f4d4 | 3827 | } |
a032b68d | 3828 | ORDERED_HASHMAP_FOREACH(a, globs) { |
6e866b33 MB |
3829 | r = link_parent(a); |
3830 | if (r < 0) | |
3831 | return r; | |
e735f4d4 | 3832 | } |
663996b3 | 3833 | |
6e866b33 MB |
3834 | /* If multiple operations are requested, let's first run the remove/clean operations, and only then the create |
3835 | * operations. i.e. that we first clean out the platform we then build on. */ | |
3836 | for (phase = 0; phase < _PHASE_MAX; phase++) { | |
3837 | OperationMask op; | |
b012e921 | 3838 | |
6e866b33 MB |
3839 | if (phase == PHASE_REMOVE_AND_CLEAN) |
3840 | op = arg_operation & (OPERATION_REMOVE|OPERATION_CLEAN); | |
3841 | else if (phase == PHASE_CREATE) | |
3842 | op = arg_operation & OPERATION_CREATE; | |
3843 | else | |
3844 | assert_not_reached("unexpected phase"); | |
663996b3 | 3845 | |
6e866b33 MB |
3846 | if (op == 0) /* Nothing requested in this phase */ |
3847 | continue; | |
14228c0d | 3848 | |
6e866b33 | 3849 | /* The non-globbing ones usually create things, hence we apply them first */ |
a032b68d | 3850 | ORDERED_HASHMAP_FOREACH(a, items) { |
6e866b33 MB |
3851 | k = process_item_array(a, op); |
3852 | if (k < 0 && r >= 0) | |
3853 | r = k; | |
3854 | } | |
663996b3 | 3855 | |
6e866b33 | 3856 | /* The globbing ones usually alter things, hence we apply them second. */ |
a032b68d | 3857 | ORDERED_HASHMAP_FOREACH(a, globs) { |
6e866b33 MB |
3858 | k = process_item_array(a, op); |
3859 | if (k < 0 && r >= 0) | |
3860 | r = k; | |
3861 | } | |
3862 | } | |
663996b3 | 3863 | |
a032b68d | 3864 | if (ERRNO_IS_RESOURCE(r)) |
6e866b33 MB |
3865 | return r; |
3866 | if (invalid_config) | |
52ad194e | 3867 | return EX_DATAERR; |
6e866b33 | 3868 | if (r < 0) |
b012e921 | 3869 | return EX_CANTCREAT; |
6e866b33 | 3870 | return 0; |
663996b3 | 3871 | } |
6e866b33 MB |
3872 | |
3873 | DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); |