]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
5008d581 | 2 | |
5008d581 | 3 | #include <errno.h> |
3f6fd1ba LP |
4 | #include <fcntl.h> |
5 | #include <fnmatch.h> | |
6 | #include <getopt.h> | |
5008d581 | 7 | #include <limits.h> |
3f6fd1ba LP |
8 | #include <linux/fs.h> |
9 | #include <stdbool.h> | |
10 | #include <stddef.h> | |
3b63d2d3 | 11 | #include <stdlib.h> |
52b32b2a | 12 | #include <sys/file.h> |
ebf4e801 | 13 | #include <sys/xattr.h> |
d9daae55 | 14 | #include <sysexits.h> |
3f6fd1ba LP |
15 | #include <time.h> |
16 | #include <unistd.h> | |
5008d581 | 17 | |
5a8575ef ZJS |
18 | #include "sd-path.h" |
19 | ||
3f6fd1ba | 20 | #include "acl-util.h" |
b5efdb8a | 21 | #include "alloc-util.h" |
3f6fd1ba | 22 | #include "btrfs-util.h" |
d6b4d1c7 | 23 | #include "build.h" |
430f0182 | 24 | #include "capability-util.h" |
f461a28d | 25 | #include "chase.h" |
c8b3094d | 26 | #include "chattr-util.h" |
3f6fd1ba | 27 | #include "conf-files.h" |
28db6fbf | 28 | #include "constants.h" |
3f6fd1ba | 29 | #include "copy.h" |
1d77721f | 30 | #include "creds-util.h" |
7176f06c | 31 | #include "devnum-util.h" |
8fb3f009 | 32 | #include "dirent-util.h" |
63d3d0a5 | 33 | #include "dissect-image.h" |
4368c60c | 34 | #include "env-util.h" |
8f6fb95c | 35 | #include "errno-util.h" |
4f5dd394 | 36 | #include "escape.h" |
3ffd4af2 | 37 | #include "fd-util.h" |
0d39fa9c | 38 | #include "fileio.h" |
f97b34a6 | 39 | #include "format-util.h" |
f4f15635 | 40 | #include "fs-util.h" |
7d50b32a | 41 | #include "glob-util.h" |
708daf42 | 42 | #include "hexdecoct.h" |
c004493c | 43 | #include "io-util.h" |
0690160e | 44 | #include "label-util.h" |
5008d581 | 45 | #include "log.h" |
54693d9b | 46 | #include "macro.h" |
87938c3b | 47 | #include "main-func.h" |
ddb439b8 | 48 | #include "missing_syscall.h" |
35cd0ba5 | 49 | #include "mkdir-label.h" |
63d3d0a5 | 50 | #include "mount-util.h" |
049af8ad | 51 | #include "mountpoint-util.h" |
08af3cc5 | 52 | #include "nulstr-util.h" |
a3451c2c | 53 | #include "offline-passwd.h" |
dcd5c891 | 54 | #include "pager.h" |
614b022c | 55 | #include "parse-argument.h" |
6bedfcbb | 56 | #include "parse-util.h" |
f2b5ca0e | 57 | #include "path-lookup.h" |
9eb977db | 58 | #include "path-util.h" |
294bf0c3 | 59 | #include "pretty-print.h" |
e5358401 | 60 | #include "rlimit-util.h" |
c6878637 | 61 | #include "rm-rf.h" |
d7b8eec7 | 62 | #include "selinux-util.h" |
3f6fd1ba | 63 | #include "set.h" |
760877e9 | 64 | #include "sort-util.h" |
3f6fd1ba | 65 | #include "specifier.h" |
41fea218 | 66 | #include "stat-util.h" |
15a5e950 | 67 | #include "stdio-util.h" |
8b43440b | 68 | #include "string-table.h" |
07630cea | 69 | #include "string-util.h" |
3f6fd1ba | 70 | #include "strv.h" |
3f532a59 | 71 | #include "terminal-util.h" |
affb60b1 | 72 | #include "umask-util.h" |
b1d4f8e1 | 73 | #include "user-util.h" |
1401882f | 74 | #include "virt.h" |
5008d581 | 75 | |
01000479 | 76 | /* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates |
5008d581 | 77 | * them in the file system. This is intended to be used to create |
db019b8d KS |
78 | * properly owned directories beneath /tmp, /var/tmp, /run, which are |
79 | * volatile and hence need to be recreated on bootup. */ | |
5008d581 | 80 | |
1a967b6b LP |
81 | typedef enum OperationMask { |
82 | OPERATION_CREATE = 1 << 0, | |
83 | OPERATION_REMOVE = 1 << 1, | |
84 | OPERATION_CLEAN = 1 << 2, | |
81a18380 | 85 | OPERATION_PURGE = 1 << 3, |
1a967b6b LP |
86 | } OperationMask; |
87 | ||
66ccd038 | 88 | typedef enum ItemType { |
b8bb3e8f | 89 | /* These ones take file names */ |
2223a025 ZJS |
90 | CREATE_FILE = 'f', |
91 | TRUNCATE_FILE = 'F', /* deprecated: use f+ */ | |
92 | CREATE_DIRECTORY = 'd', | |
93 | TRUNCATE_DIRECTORY = 'D', | |
94 | CREATE_SUBVOLUME = 'v', | |
5fb13eb5 | 95 | CREATE_SUBVOLUME_INHERIT_QUOTA = 'q', |
2223a025 ZJS |
96 | CREATE_SUBVOLUME_NEW_QUOTA = 'Q', |
97 | CREATE_FIFO = 'p', | |
98 | CREATE_SYMLINK = 'L', | |
99 | CREATE_CHAR_DEVICE = 'c', | |
100 | CREATE_BLOCK_DEVICE = 'b', | |
101 | COPY_FILES = 'C', | |
b8bb3e8f LP |
102 | |
103 | /* These ones take globs */ | |
2223a025 ZJS |
104 | WRITE_FILE = 'w', |
105 | EMPTY_DIRECTORY = 'e', | |
106 | SET_XATTR = 't', | |
107 | RECURSIVE_SET_XATTR = 'T', | |
108 | SET_ACL = 'a', | |
109 | RECURSIVE_SET_ACL = 'A', | |
110 | SET_ATTRIBUTE = 'h', | |
111 | RECURSIVE_SET_ATTRIBUTE = 'H', | |
112 | IGNORE_PATH = 'x', | |
113 | IGNORE_DIRECTORY_PATH = 'X', | |
114 | REMOVE_PATH = 'r', | |
115 | RECURSIVE_REMOVE_PATH = 'R', | |
116 | RELABEL_PATH = 'z', | |
117 | RECURSIVE_RELABEL_PATH = 'Z', | |
118 | ADJUST_MODE = 'm', /* legacy, 'z' is identical to this */ | |
66ccd038 | 119 | } ItemType; |
3b63d2d3 | 120 | |
7f7a50dd SK |
121 | typedef enum AgeBy { |
122 | AGE_BY_ATIME = 1 << 0, | |
123 | AGE_BY_BTIME = 1 << 1, | |
124 | AGE_BY_CTIME = 1 << 2, | |
125 | AGE_BY_MTIME = 1 << 3, | |
126 | ||
127 | /* All file timestamp types are checked by default. */ | |
128 | AGE_BY_DEFAULT_FILE = AGE_BY_ATIME | AGE_BY_BTIME | AGE_BY_CTIME | AGE_BY_MTIME, | |
2223a025 | 129 | AGE_BY_DEFAULT_DIR = AGE_BY_ATIME | AGE_BY_BTIME | AGE_BY_MTIME, |
7f7a50dd SK |
130 | } AgeBy; |
131 | ||
3b63d2d3 | 132 | typedef struct Item { |
66ccd038 | 133 | ItemType type; |
3b63d2d3 LP |
134 | |
135 | char *path; | |
468d726b | 136 | char *argument; |
708daf42 LP |
137 | void *binary_argument; /* set if binary data, in which case it takes precedence over 'argument' */ |
138 | size_t binary_argument_size; | |
ebf4e801 | 139 | char **xattrs; |
349cc4a5 | 140 | #if HAVE_ACL |
f8eeeaf9 | 141 | acl_t acl_access; |
26d98cdd | 142 | acl_t acl_access_exec; |
f8eeeaf9 ZJS |
143 | acl_t acl_default; |
144 | #endif | |
5008d581 LP |
145 | uid_t uid; |
146 | gid_t gid; | |
3b63d2d3 LP |
147 | mode_t mode; |
148 | usec_t age; | |
7f7a50dd | 149 | AgeBy age_by_file, age_by_dir; |
3b63d2d3 | 150 | |
468d726b | 151 | dev_t major_minor; |
88ec4dfa LP |
152 | unsigned attribute_value; |
153 | unsigned attribute_mask; | |
468d726b | 154 | |
3b63d2d3 LP |
155 | bool uid_set:1; |
156 | bool gid_set:1; | |
157 | bool mode_set:1; | |
cc43328c LP |
158 | bool uid_only_create:1; |
159 | bool gid_only_create:1; | |
160 | bool mode_only_create:1; | |
3b63d2d3 | 161 | bool age_set:1; |
abef3f91 | 162 | bool mask_perms:1; |
88ec4dfa | 163 | bool attribute_set:1; |
24f3a374 LP |
164 | |
165 | bool keep_first_level:1; | |
1910cd0e | 166 | |
c55ac248 | 167 | bool append_or_force:1; |
2e78fa79 | 168 | |
6d7b5433 WD |
169 | bool allow_failure:1; |
170 | ||
c46c3233 AW |
171 | bool try_replace:1; |
172 | ||
811a1587 | 173 | OperationMask done; |
3b63d2d3 LP |
174 | } Item; |
175 | ||
3f93da98 ZJS |
176 | typedef struct ItemArray { |
177 | Item *items; | |
96d10d78 | 178 | size_t n_items; |
811a1587 LP |
179 | |
180 | struct ItemArray *parent; | |
181 | Set *children; | |
3f93da98 ZJS |
182 | } ItemArray; |
183 | ||
5a8575ef | 184 | typedef enum DirectoryType { |
133bbca4 | 185 | DIRECTORY_RUNTIME, |
5a8575ef ZJS |
186 | DIRECTORY_STATE, |
187 | DIRECTORY_CACHE, | |
188 | DIRECTORY_LOGS, | |
189 | _DIRECTORY_TYPE_MAX, | |
190 | } DirectoryType; | |
191 | ||
a9bc518c LP |
192 | typedef enum { |
193 | CREATION_NORMAL, | |
194 | CREATION_EXISTING, | |
195 | CREATION_FORCE, | |
196 | _CREATION_MODE_MAX, | |
197 | _CREATION_MODE_INVALID = -EINVAL, | |
198 | } CreationMode; | |
199 | ||
1be6a5db | 200 | static CatFlags arg_cat_flags = CAT_CONFIG_OFF; |
4870133b | 201 | static RuntimeScope arg_runtime_scope = RUNTIME_SCOPE_SYSTEM; |
1a967b6b | 202 | static OperationMask arg_operation = 0; |
81815651 | 203 | static bool arg_boot = false; |
e0ea6af3 | 204 | static bool arg_graceful = false; |
0221d68a | 205 | static PagerFlags arg_pager_flags = 0; |
3b63d2d3 | 206 | |
7bc040fa LP |
207 | static char **arg_include_prefixes = NULL; |
208 | static char **arg_exclude_prefixes = NULL; | |
cf9a4abd | 209 | static char *arg_root = NULL; |
63d3d0a5 | 210 | static char *arg_image = NULL; |
a6d8474f | 211 | static char *arg_replace = NULL; |
84be0c71 | 212 | static ImagePolicy *arg_image_policy = NULL; |
fba6e687 | 213 | |
3b63d2d3 LP |
214 | #define MAX_DEPTH 256 |
215 | ||
a5e4f3ab | 216 | typedef struct Context { |
376d0495 ZJS |
217 | OrderedHashmap *items; |
218 | OrderedHashmap *globs; | |
a5e4f3ab | 219 | Set *unix_sockets; |
376d0495 ZJS |
220 | Hashmap *uid_cache; |
221 | Hashmap *gid_cache; | |
a5e4f3ab | 222 | } Context; |
7bc040fa | 223 | |
7f13af72 AAF |
224 | STATIC_DESTRUCTOR_REGISTER(arg_include_prefixes, strv_freep); |
225 | STATIC_DESTRUCTOR_REGISTER(arg_exclude_prefixes, strv_freep); | |
87938c3b | 226 | STATIC_DESTRUCTOR_REGISTER(arg_root, freep); |
63d3d0a5 | 227 | STATIC_DESTRUCTOR_REGISTER(arg_image, freep); |
84be0c71 | 228 | STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep); |
87938c3b | 229 | |
a9bc518c LP |
230 | static const char *const creation_mode_verb_table[_CREATION_MODE_MAX] = { |
231 | [CREATION_NORMAL] = "Created", | |
232 | [CREATION_EXISTING] = "Found existing", | |
233 | [CREATION_FORCE] = "Created replacement", | |
234 | }; | |
235 | ||
236 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(creation_mode_verb, CreationMode); | |
237 | ||
a5e4f3ab LP |
238 | static void context_done(Context *c) { |
239 | assert(c); | |
240 | ||
241 | ordered_hashmap_free(c->items); | |
242 | ordered_hashmap_free(c->globs); | |
243 | ||
244 | set_free(c->unix_sockets); | |
376d0495 ZJS |
245 | |
246 | hashmap_free(c->uid_cache); | |
247 | hashmap_free(c->gid_cache); | |
a5e4f3ab LP |
248 | } |
249 | ||
6db98098 | 250 | /* Different kinds of errors that mean that information is not available in the environment. */ |
cf1ab844 | 251 | static bool ERRNO_IS_NOINFO(int r) { |
6db98098 ZJS |
252 | return IN_SET(abs(r), |
253 | EUNATCH, /* os-release or machine-id missing */ | |
254 | ENOMEDIUM, /* machine-id or another file empty */ | |
5dd814d7 | 255 | ENOPKG, /* machine-id is uninitialized */ |
6db98098 | 256 | ENXIO); /* env var is unset */ |
4cef1923 FB |
257 | } |
258 | ||
5ac41a16 ZJS |
259 | static int specifier_directory( |
260 | char specifier, | |
261 | const void *data, | |
262 | const char *root, | |
263 | const void *userdata, | |
264 | char **ret) { | |
265 | ||
5a8575ef ZJS |
266 | struct table_entry { |
267 | uint64_t type; | |
268 | const char *suffix; | |
269 | }; | |
270 | ||
271 | static const struct table_entry paths_system[] = { | |
272 | [DIRECTORY_RUNTIME] = { SD_PATH_SYSTEM_RUNTIME }, | |
273 | [DIRECTORY_STATE] = { SD_PATH_SYSTEM_STATE_PRIVATE }, | |
274 | [DIRECTORY_CACHE] = { SD_PATH_SYSTEM_STATE_CACHE }, | |
275 | [DIRECTORY_LOGS] = { SD_PATH_SYSTEM_STATE_LOGS }, | |
276 | }; | |
277 | ||
278 | static const struct table_entry paths_user[] = { | |
279 | [DIRECTORY_RUNTIME] = { SD_PATH_USER_RUNTIME }, | |
b50aadaf | 280 | [DIRECTORY_STATE] = { SD_PATH_USER_STATE_PRIVATE }, |
5a8575ef | 281 | [DIRECTORY_CACHE] = { SD_PATH_USER_STATE_CACHE }, |
b50aadaf | 282 | [DIRECTORY_LOGS] = { SD_PATH_USER_STATE_PRIVATE, "log" }, |
5a8575ef ZJS |
283 | }; |
284 | ||
5a8575ef | 285 | const struct table_entry *paths; |
de61a04b LP |
286 | _cleanup_free_ char *p = NULL; |
287 | unsigned i; | |
288 | int r; | |
5a8575ef ZJS |
289 | |
290 | assert_cc(ELEMENTSOF(paths_system) == ELEMENTSOF(paths_user)); | |
4870133b | 291 | paths = arg_runtime_scope == RUNTIME_SCOPE_USER ? paths_user : paths_system; |
5a8575ef ZJS |
292 | |
293 | i = PTR_TO_UINT(data); | |
294 | assert(i < ELEMENTSOF(paths_system)); | |
295 | ||
de61a04b LP |
296 | r = sd_path_lookup(paths[i].type, paths[i].suffix, &p); |
297 | if (r < 0) | |
298 | return r; | |
299 | ||
300 | if (arg_root) { | |
301 | _cleanup_free_ char *j = NULL; | |
302 | ||
303 | j = path_join(arg_root, p); | |
304 | if (!j) | |
305 | return -ENOMEM; | |
306 | ||
307 | *ret = TAKE_PTR(j); | |
308 | } else | |
309 | *ret = TAKE_PTR(p); | |
310 | ||
311 | return 0; | |
5a8575ef ZJS |
312 | } |
313 | ||
4cef1923 FB |
314 | static int log_unresolvable_specifier(const char *filename, unsigned line) { |
315 | static bool notified = false; | |
316 | ||
1401882f ZJS |
317 | /* In system mode, this is called when /etc is not fully initialized and some specifiers are |
318 | * unresolvable. In user mode, this is called when some variables are not defined. These cases are | |
319 | * not considered a fatal error, so log at LOG_NOTICE only for the first time and then downgrade this | |
320 | * to LOG_DEBUG for the rest. | |
321 | * | |
322 | * If we're running in a chroot (--root was used or sd_booted() reports that systemd is not running), | |
323 | * always use LOG_DEBUG. We may be called to initialize a chroot before booting and there is no | |
324 | * expectation that machine-id and other files will be populated. | |
325 | */ | |
326 | ||
327 | int log_level = notified || arg_root || running_in_chroot() > 0 ? | |
328 | LOG_DEBUG : LOG_NOTICE; | |
4cef1923 | 329 | |
d02933fd | 330 | log_syntax(NULL, |
1401882f | 331 | log_level, |
d02933fd | 332 | filename, line, 0, |
1401882f | 333 | "Failed to resolve specifier: %s, skipping.", |
4870133b | 334 | arg_runtime_scope == RUNTIME_SCOPE_USER ? "Required $XDG_... variable not defined" : "uninitialized /etc/ detected"); |
4cef1923 FB |
335 | |
336 | if (!notified) | |
1401882f ZJS |
337 | log_full(log_level, |
338 | "All rules containing unresolvable specifiers will be skipped."); | |
4cef1923 FB |
339 | |
340 | notified = true; | |
341 | return 0; | |
342 | } | |
343 | ||
f2b5ca0e ZJS |
344 | static int user_config_paths(char*** ret) { |
345 | _cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL; | |
346 | _cleanup_free_ char *persistent_config = NULL, *runtime_config = NULL, *data_home = NULL; | |
347 | _cleanup_strv_free_ char **res = NULL; | |
348 | int r; | |
349 | ||
350 | r = xdg_user_dirs(&config_dirs, &data_dirs); | |
351 | if (r < 0) | |
352 | return r; | |
353 | ||
354 | r = xdg_user_config_dir(&persistent_config, "/user-tmpfiles.d"); | |
6db98098 | 355 | if (r < 0 && !ERRNO_IS_NOINFO(r)) |
f2b5ca0e ZJS |
356 | return r; |
357 | ||
358 | r = xdg_user_runtime_dir(&runtime_config, "/user-tmpfiles.d"); | |
6db98098 | 359 | if (r < 0 && !ERRNO_IS_NOINFO(r)) |
f2b5ca0e ZJS |
360 | return r; |
361 | ||
362 | r = xdg_user_data_dir(&data_home, "/user-tmpfiles.d"); | |
6db98098 | 363 | if (r < 0 && !ERRNO_IS_NOINFO(r)) |
f2b5ca0e ZJS |
364 | return r; |
365 | ||
366 | r = strv_extend_strv_concat(&res, config_dirs, "/user-tmpfiles.d"); | |
367 | if (r < 0) | |
368 | return r; | |
369 | ||
69f3c619 LP |
370 | r = strv_extend_many( |
371 | &res, | |
372 | persistent_config, | |
373 | runtime_config, | |
374 | data_home); | |
f2b5ca0e ZJS |
375 | if (r < 0) |
376 | return r; | |
377 | ||
378 | r = strv_extend_strv_concat(&res, data_dirs, "/user-tmpfiles.d"); | |
379 | if (r < 0) | |
380 | return r; | |
381 | ||
382 | r = path_strv_make_absolute_cwd(res); | |
383 | if (r < 0) | |
384 | return r; | |
385 | ||
ae2a15bc | 386 | *ret = TAKE_PTR(res); |
f2b5ca0e ZJS |
387 | return 0; |
388 | } | |
389 | ||
81a18380 LB |
390 | static bool needs_purge(ItemType t) { |
391 | return IN_SET(t, | |
392 | COPY_FILES, | |
393 | TRUNCATE_FILE, | |
394 | CREATE_FILE, | |
395 | WRITE_FILE, | |
396 | EMPTY_DIRECTORY, | |
397 | CREATE_SUBVOLUME, | |
398 | CREATE_SUBVOLUME_INHERIT_QUOTA, | |
399 | CREATE_SUBVOLUME_NEW_QUOTA, | |
400 | CREATE_CHAR_DEVICE, | |
401 | CREATE_BLOCK_DEVICE, | |
402 | CREATE_SYMLINK, | |
403 | CREATE_FIFO, | |
404 | CREATE_DIRECTORY, | |
405 | TRUNCATE_DIRECTORY); | |
406 | } | |
407 | ||
66ccd038 | 408 | static bool needs_glob(ItemType t) { |
cde684a2 LP |
409 | return IN_SET(t, |
410 | WRITE_FILE, | |
df8dee85 | 411 | EMPTY_DIRECTORY, |
b705ab6a ZJS |
412 | SET_XATTR, |
413 | RECURSIVE_SET_XATTR, | |
414 | SET_ACL, | |
34f64536 LP |
415 | RECURSIVE_SET_ACL, |
416 | SET_ATTRIBUTE, | |
d88b65f6 LP |
417 | RECURSIVE_SET_ATTRIBUTE, |
418 | IGNORE_PATH, | |
419 | IGNORE_DIRECTORY_PATH, | |
420 | REMOVE_PATH, | |
421 | RECURSIVE_REMOVE_PATH, | |
422 | RELABEL_PATH, | |
423 | RECURSIVE_RELABEL_PATH, | |
424 | ADJUST_MODE); | |
b8bb3e8f LP |
425 | } |
426 | ||
3f93da98 ZJS |
427 | static bool takes_ownership(ItemType t) { |
428 | return IN_SET(t, | |
429 | CREATE_FILE, | |
430 | TRUNCATE_FILE, | |
431 | CREATE_DIRECTORY, | |
432 | TRUNCATE_DIRECTORY, | |
433 | CREATE_SUBVOLUME, | |
5fb13eb5 LP |
434 | CREATE_SUBVOLUME_INHERIT_QUOTA, |
435 | CREATE_SUBVOLUME_NEW_QUOTA, | |
3f93da98 ZJS |
436 | CREATE_FIFO, |
437 | CREATE_SYMLINK, | |
438 | CREATE_CHAR_DEVICE, | |
439 | CREATE_BLOCK_DEVICE, | |
440 | COPY_FILES, | |
3f93da98 | 441 | WRITE_FILE, |
d88b65f6 | 442 | EMPTY_DIRECTORY, |
3f93da98 ZJS |
443 | IGNORE_PATH, |
444 | IGNORE_DIRECTORY_PATH, | |
445 | REMOVE_PATH, | |
446 | RECURSIVE_REMOVE_PATH); | |
447 | } | |
448 | ||
ef43a391 | 449 | static struct Item* find_glob(OrderedHashmap *h, const char *match) { |
3f93da98 | 450 | ItemArray *j; |
b8bb3e8f | 451 | |
90e74a66 | 452 | ORDERED_HASHMAP_FOREACH(j, h) { |
96d10d78 | 453 | size_t n; |
3f93da98 | 454 | |
96d10d78 | 455 | for (n = 0; n < j->n_items; n++) { |
3f93da98 ZJS |
456 | Item *item = j->items + n; |
457 | ||
458 | if (fnmatch(item->path, match, FNM_PATHNAME|FNM_PERIOD) == 0) | |
459 | return item; | |
460 | } | |
461 | } | |
b8bb3e8f LP |
462 | |
463 | return NULL; | |
464 | } | |
465 | ||
a5e4f3ab | 466 | static int load_unix_sockets(Context *c) { |
fe2f05b7 | 467 | _cleanup_set_free_ Set *sockets = NULL; |
7fd1b19b | 468 | _cleanup_fclose_ FILE *f = NULL; |
f1ff734f | 469 | int r; |
17b90525 | 470 | |
a5e4f3ab | 471 | if (c->unix_sockets) |
71a0be76 | 472 | return 0; |
17b90525 | 473 | |
f1ff734f | 474 | /* We maintain a cache of the sockets we found in /proc/net/unix to speed things up a little. */ |
17b90525 | 475 | |
fdcad0c2 | 476 | f = fopen("/proc/net/unix", "re"); |
71a0be76 YW |
477 | if (!f) |
478 | return log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno, | |
479 | "Failed to open /proc/net/unix, ignoring: %m"); | |
17b90525 | 480 | |
fdcad0c2 | 481 | /* Skip header */ |
f1ff734f | 482 | r = read_line(f, LONG_LINE_MAX, NULL); |
71a0be76 YW |
483 | if (r < 0) |
484 | return log_warning_errno(r, "Failed to skip /proc/net/unix header line: %m"); | |
485 | if (r == 0) | |
486 | return log_warning_errno(SYNTHETIC_ERRNO(EIO), "Premature end of file reading /proc/net/unix."); | |
17b90525 LP |
487 | |
488 | for (;;) { | |
fe2f05b7 | 489 | _cleanup_free_ char *line = NULL; |
71a0be76 | 490 | char *p; |
17b90525 | 491 | |
f1ff734f | 492 | r = read_line(f, LONG_LINE_MAX, &line); |
71a0be76 YW |
493 | if (r < 0) |
494 | return log_warning_errno(r, "Failed to read /proc/net/unix line, ignoring: %m"); | |
f1ff734f | 495 | if (r == 0) /* EOF */ |
17b90525 LP |
496 | break; |
497 | ||
fdcad0c2 LP |
498 | p = strchr(line, ':'); |
499 | if (!p) | |
500 | continue; | |
501 | ||
502 | if (strlen(p) < 37) | |
17b90525 LP |
503 | continue; |
504 | ||
fdcad0c2 | 505 | p += 37; |
17b90525 | 506 | p += strspn(p, WHITESPACE); |
fdcad0c2 | 507 | p += strcspn(p, WHITESPACE); /* skip one more word */ |
17b90525 LP |
508 | p += strspn(p, WHITESPACE); |
509 | ||
fe2f05b7 | 510 | if (!path_is_absolute(p)) |
17b90525 LP |
511 | continue; |
512 | ||
fe2f05b7 | 513 | r = set_put_strdup_full(&sockets, &path_hash_ops_free, p); |
71a0be76 YW |
514 | if (r < 0) |
515 | return log_warning_errno(r, "Failed to add AF_UNIX socket to set, ignoring: %m"); | |
71a0be76 | 516 | } |
17b90525 | 517 | |
a5e4f3ab | 518 | c->unix_sockets = TAKE_PTR(sockets); |
71a0be76 | 519 | return 1; |
17b90525 LP |
520 | } |
521 | ||
a5e4f3ab LP |
522 | static bool unix_socket_alive(Context *c, const char *fn) { |
523 | assert(c); | |
17b90525 LP |
524 | assert(fn); |
525 | ||
a5e4f3ab | 526 | if (load_unix_sockets(c) < 0) |
71a0be76 | 527 | return true; /* We don't know, so assume yes */ |
17b90525 | 528 | |
a5e4f3ab | 529 | return set_contains(c->unix_sockets, fn); |
17b90525 LP |
530 | } |
531 | ||
708daf42 LP |
532 | /* Accessors for the argument in binary format */ |
533 | static const void* item_binary_argument(const Item *i) { | |
534 | assert(i); | |
535 | return i->binary_argument ?: i->argument; | |
536 | } | |
537 | ||
538 | static size_t item_binary_argument_size(const Item *i) { | |
539 | assert(i); | |
540 | return i->binary_argument ? i->binary_argument_size : strlen_ptr(i->argument); | |
541 | } | |
542 | ||
df99a9ef ZJS |
543 | static DIR* xopendirat_nomod(int dirfd, const char *path) { |
544 | DIR *dir; | |
545 | ||
546 | dir = xopendirat(dirfd, path, O_NOFOLLOW|O_NOATIME); | |
1532227a LP |
547 | if (dir) |
548 | return dir; | |
549 | ||
08c25eee DDM |
550 | if (!IN_SET(errno, ENOENT, ELOOP)) |
551 | log_debug_errno(errno, "Cannot open %sdirectory \"%s\": %m", dirfd == AT_FDCWD ? "" : "sub", path); | |
552 | ||
1532227a LP |
553 | if (errno != EPERM) |
554 | return NULL; | |
555 | ||
556 | dir = xopendirat(dirfd, path, O_NOFOLLOW); | |
557 | if (!dir) | |
558 | log_debug_errno(errno, "Cannot open %sdirectory \"%s\": %m", dirfd == AT_FDCWD ? "" : "sub", path); | |
df99a9ef ZJS |
559 | |
560 | return dir; | |
561 | } | |
562 | ||
563 | static DIR* opendir_nomod(const char *path) { | |
564 | return xopendirat_nomod(AT_FDCWD, path); | |
565 | } | |
566 | ||
7f7a50dd SK |
567 | static bool needs_cleanup( |
568 | nsec_t atime, | |
569 | nsec_t btime, | |
570 | nsec_t ctime, | |
571 | nsec_t mtime, | |
572 | nsec_t cutoff, | |
573 | const char *sub_path, | |
574 | AgeBy age_by, | |
575 | bool is_dir) { | |
576 | ||
577 | if (FLAGS_SET(age_by, AGE_BY_MTIME) && mtime != NSEC_INFINITY && mtime >= cutoff) { | |
7f7a50dd SK |
578 | /* Follows spelling in stat(1). */ |
579 | log_debug("%s \"%s\": modify time %s is too new.", | |
580 | is_dir ? "Directory" : "File", | |
581 | sub_path, | |
0086ef19 | 582 | FORMAT_TIMESTAMP_STYLE(mtime / NSEC_PER_USEC, TIMESTAMP_US)); |
7f7a50dd SK |
583 | |
584 | return false; | |
585 | } | |
586 | ||
587 | if (FLAGS_SET(age_by, AGE_BY_ATIME) && atime != NSEC_INFINITY && atime >= cutoff) { | |
7f7a50dd SK |
588 | log_debug("%s \"%s\": access time %s is too new.", |
589 | is_dir ? "Directory" : "File", | |
590 | sub_path, | |
0086ef19 | 591 | FORMAT_TIMESTAMP_STYLE(atime / NSEC_PER_USEC, TIMESTAMP_US)); |
7f7a50dd SK |
592 | |
593 | return false; | |
594 | } | |
595 | ||
596 | /* | |
597 | * Note: Unless explicitly specified by the user, "ctime" is ignored | |
598 | * by default for directories, because we change it when deleting. | |
599 | */ | |
600 | if (FLAGS_SET(age_by, AGE_BY_CTIME) && ctime != NSEC_INFINITY && ctime >= cutoff) { | |
7f7a50dd SK |
601 | log_debug("%s \"%s\": change time %s is too new.", |
602 | is_dir ? "Directory" : "File", | |
603 | sub_path, | |
0086ef19 | 604 | FORMAT_TIMESTAMP_STYLE(ctime / NSEC_PER_USEC, TIMESTAMP_US)); |
7f7a50dd SK |
605 | |
606 | return false; | |
607 | } | |
608 | ||
609 | if (FLAGS_SET(age_by, AGE_BY_BTIME) && btime != NSEC_INFINITY && btime >= cutoff) { | |
7f7a50dd SK |
610 | log_debug("%s \"%s\": birth time %s is too new.", |
611 | is_dir ? "Directory" : "File", | |
612 | sub_path, | |
0086ef19 | 613 | FORMAT_TIMESTAMP_STYLE(btime / NSEC_PER_USEC, TIMESTAMP_US)); |
7f7a50dd SK |
614 | |
615 | return false; | |
616 | } | |
617 | ||
618 | return true; | |
619 | } | |
620 | ||
3b63d2d3 | 621 | static int dir_cleanup( |
a5e4f3ab | 622 | Context *c, |
78a92a5a | 623 | Item *i, |
3b63d2d3 LP |
624 | const char *p, |
625 | DIR *d, | |
ddb439b8 LP |
626 | nsec_t self_atime_nsec, |
627 | nsec_t self_mtime_nsec, | |
628 | nsec_t cutoff_nsec, | |
629 | dev_t rootdev_major, | |
630 | dev_t rootdev_minor, | |
3b63d2d3 | 631 | bool mountpoint, |
24f3a374 | 632 | int maxdepth, |
7f7a50dd SK |
633 | bool keep_this_level, |
634 | AgeBy age_by_file, | |
635 | AgeBy age_by_dir) { | |
265ffa1e | 636 | |
3b63d2d3 | 637 | bool deleted = false; |
3b63d2d3 LP |
638 | int r = 0; |
639 | ||
a5e4f3ab LP |
640 | assert(c); |
641 | assert(i); | |
642 | assert(d); | |
643 | ||
c7f0d9e5 | 644 | FOREACH_DIRENT_ALL(de, d, break) { |
7fd1b19b | 645 | _cleanup_free_ char *sub_path = NULL; |
ddb439b8 | 646 | nsec_t atime_nsec, mtime_nsec, ctime_nsec, btime_nsec; |
3b63d2d3 | 647 | |
c7f0d9e5 | 648 | if (dot_or_dot_dot(de->d_name)) |
3b63d2d3 | 649 | continue; |
5008d581 | 650 | |
ca194a2a | 651 | /* If statx() is supported, use it. It's preferable over fstatat() since it tells us |
69e3234d | 652 | * explicitly where we are looking at a mount point, for free as side information. Determining |
ca194a2a LP |
653 | * the same information without statx() is hard, see the complexity of path_is_mount_point(), |
654 | * and also much slower as it requires a number of syscalls instead of just one. Hence, when | |
655 | * we have modern statx() we use it instead of fstat() and do proper mount point checks, | |
656 | * while on older kernels's well do traditional st_dev based detection of mount points. | |
657 | * | |
1bd0cefc | 658 | * Using statx() for detecting mount points also has the benefit that we handle weird file |
ca194a2a LP |
659 | * systems such as overlayfs better where each file is originating from a different |
660 | * st_dev. */ | |
661 | ||
662 | STRUCT_STATX_DEFINE(sx); | |
663 | ||
664 | r = statx_fallback( | |
c7f0d9e5 | 665 | dirfd(d), de->d_name, |
ca194a2a LP |
666 | AT_SYMLINK_NOFOLLOW|AT_NO_AUTOMOUNT, |
667 | STATX_TYPE|STATX_MODE|STATX_UID|STATX_ATIME|STATX_MTIME|STATX_CTIME|STATX_BTIME, | |
668 | &sx); | |
669 | if (r == -ENOENT) | |
670 | continue; | |
671 | if (r < 0) { | |
672 | /* FUSE, NFS mounts, SELinux might return EACCES */ | |
149e0ca6 | 673 | r = log_full_errno(r == -EACCES ? LOG_DEBUG : LOG_ERR, r, |
c7f0d9e5 | 674 | "statx(%s/%s) failed: %m", p, de->d_name); |
ca194a2a | 675 | continue; |
3b63d2d3 LP |
676 | } |
677 | ||
ca194a2a LP |
678 | if (FLAGS_SET(sx.stx_attributes_mask, STATX_ATTR_MOUNT_ROOT)) { |
679 | /* Yay, we have the mount point API, use it */ | |
680 | if (FLAGS_SET(sx.stx_attributes, STATX_ATTR_MOUNT_ROOT)) { | |
c7f0d9e5 | 681 | log_debug("Ignoring \"%s/%s\": different mount points.", p, de->d_name); |
ddb439b8 LP |
682 | continue; |
683 | } | |
ca194a2a LP |
684 | } else { |
685 | /* So we might have statx() but the STATX_ATTR_MOUNT_ROOT flag is not supported, fall | |
686 | * back to traditional stx_dev checking. */ | |
687 | if (sx.stx_dev_major != rootdev_major || | |
688 | sx.stx_dev_minor != rootdev_minor) { | |
c7f0d9e5 | 689 | log_debug("Ignoring \"%s/%s\": different filesystem.", p, de->d_name); |
ddb439b8 LP |
690 | continue; |
691 | } | |
692 | ||
ca194a2a LP |
693 | /* Try to detect bind mounts of the same filesystem instance; they do not differ in device |
694 | * major/minors. This type of query is not supported on all kernels or filesystem types | |
695 | * though. */ | |
696 | if (S_ISDIR(sx.stx_mode)) { | |
697 | int q; | |
3b63d2d3 | 698 | |
c7f0d9e5 | 699 | q = fd_is_mount_point(dirfd(d), de->d_name, 0); |
ca194a2a | 700 | if (q < 0) |
c7f0d9e5 | 701 | log_debug_errno(q, "Failed to determine whether \"%s/%s\" is a mount point, ignoring: %m", p, de->d_name); |
ca194a2a | 702 | else if (q > 0) { |
c7f0d9e5 | 703 | log_debug("Ignoring \"%s/%s\": different mount of the same filesystem.", p, de->d_name); |
ca194a2a LP |
704 | continue; |
705 | } | |
60bdc0ca | 706 | } |
582deb84 | 707 | } |
99d680ac | 708 | |
41fea218 AV |
709 | atime_nsec = FLAGS_SET(sx.stx_mask, STATX_ATIME) ? statx_timestamp_load_nsec(&sx.stx_atime) : 0; |
710 | mtime_nsec = FLAGS_SET(sx.stx_mask, STATX_MTIME) ? statx_timestamp_load_nsec(&sx.stx_mtime) : 0; | |
711 | ctime_nsec = FLAGS_SET(sx.stx_mask, STATX_CTIME) ? statx_timestamp_load_nsec(&sx.stx_ctime) : 0; | |
712 | btime_nsec = FLAGS_SET(sx.stx_mask, STATX_BTIME) ? statx_timestamp_load_nsec(&sx.stx_btime) : 0; | |
ca194a2a | 713 | |
c7f0d9e5 | 714 | sub_path = path_join(p, de->d_name); |
cde684a2 | 715 | if (!sub_path) { |
0d0f0c50 | 716 | r = log_oom(); |
3b63d2d3 LP |
717 | goto finish; |
718 | } | |
719 | ||
720 | /* Is there an item configured for this path? */ | |
a5e4f3ab | 721 | if (ordered_hashmap_get(c->items, sub_path)) { |
582deb84 | 722 | log_debug("Ignoring \"%s\": a separate entry exists.", sub_path); |
3b63d2d3 | 723 | continue; |
582deb84 | 724 | } |
3b63d2d3 | 725 | |
a5e4f3ab | 726 | if (find_glob(c->globs, sub_path)) { |
582deb84 | 727 | log_debug("Ignoring \"%s\": a separate glob exists.", sub_path); |
b8bb3e8f | 728 | continue; |
582deb84 | 729 | } |
b8bb3e8f | 730 | |
ca194a2a | 731 | if (S_ISDIR(sx.stx_mode)) { |
52b32b2a | 732 | _cleanup_closedir_ DIR *sub_dir = NULL; |
3b63d2d3 LP |
733 | |
734 | if (mountpoint && | |
c7f0d9e5 | 735 | streq(de->d_name, "lost+found") && |
ca194a2a | 736 | sx.stx_uid == 0) { |
781bc44e | 737 | log_debug("Ignoring directory \"%s\".", sub_path); |
3b63d2d3 | 738 | continue; |
582deb84 | 739 | } |
3b63d2d3 LP |
740 | |
741 | if (maxdepth <= 0) | |
582deb84 | 742 | log_warning("Reached max depth on \"%s\".", sub_path); |
3b63d2d3 | 743 | else { |
3b63d2d3 LP |
744 | int q; |
745 | ||
c7f0d9e5 | 746 | sub_dir = xopendirat_nomod(dirfd(d), de->d_name); |
cde684a2 | 747 | if (!sub_dir) { |
582deb84 | 748 | if (errno != ENOENT) |
781bc44e | 749 | r = log_warning_errno(errno, "Opening directory \"%s\" failed, ignoring: %m", sub_path); |
3b63d2d3 LP |
750 | |
751 | continue; | |
752 | } | |
753 | ||
52b32b2a | 754 | if (flock(dirfd(sub_dir), LOCK_EX|LOCK_NB) < 0) { |
b572e8da | 755 | log_debug_errno(errno, "Couldn't acquire shared BSD lock on directory \"%s\", skipping: %m", sub_path); |
52b32b2a LP |
756 | continue; |
757 | } | |
758 | ||
a5e4f3ab | 759 | q = dir_cleanup(c, i, |
ddb439b8 LP |
760 | sub_path, sub_dir, |
761 | atime_nsec, mtime_nsec, cutoff_nsec, | |
762 | rootdev_major, rootdev_minor, | |
7f7a50dd SK |
763 | false, maxdepth-1, false, |
764 | age_by_file, age_by_dir); | |
3b63d2d3 LP |
765 | if (q < 0) |
766 | r = q; | |
767 | } | |
768 | ||
a23fabc6 LP |
769 | /* Note: if you are wondering why we don't support the sticky bit for excluding |
770 | * directories from cleaning like we do it for other file system objects: well, the | |
771 | * sticky bit already has a meaning for directories, so we don't want to overload | |
772 | * that. */ | |
24f3a374 | 773 | |
582deb84 | 774 | if (keep_this_level) { |
781bc44e | 775 | log_debug("Keeping directory \"%s\".", sub_path); |
24f3a374 | 776 | continue; |
582deb84 | 777 | } |
24f3a374 | 778 | |
7f7a50dd SK |
779 | /* |
780 | * Check the file timestamps of an entry against the | |
781 | * given cutoff time; delete if it is older. | |
782 | */ | |
783 | if (!needs_cleanup(atime_nsec, btime_nsec, ctime_nsec, mtime_nsec, | |
784 | cutoff_nsec, sub_path, age_by_dir, true)) | |
3b63d2d3 LP |
785 | continue; |
786 | ||
61253220 | 787 | log_debug("Removing directory \"%s\".", sub_path); |
c7f0d9e5 | 788 | if (unlinkat(dirfd(d), de->d_name, AT_REMOVEDIR) < 0) |
920ce828 | 789 | if (!IN_SET(errno, ENOENT, ENOTEMPTY)) |
781bc44e | 790 | r = log_warning_errno(errno, "Failed to remove directory \"%s\", ignoring: %m", sub_path); |
3b63d2d3 LP |
791 | |
792 | } else { | |
65e179a1 DDM |
793 | _cleanup_close_ int fd = -EBADF; |
794 | ||
a23fabc6 LP |
795 | /* Skip files for which the sticky bit is set. These are semantics we define, and are |
796 | * unknown elsewhere. See XDG_RUNTIME_DIR specification for details. */ | |
ca194a2a | 797 | if (sx.stx_mode & S_ISVTX) { |
582deb84 | 798 | log_debug("Skipping \"%s\": sticky bit set.", sub_path); |
9c73736d | 799 | continue; |
582deb84 | 800 | } |
9c73736d | 801 | |
20b6bb95 | 802 | if (mountpoint && |
ca194a2a LP |
803 | S_ISREG(sx.stx_mode) && |
804 | sx.stx_uid == 0 && | |
c7f0d9e5 | 805 | STR_IN_SET(de->d_name, |
20b6bb95 LP |
806 | ".journal", |
807 | "aquota.user", | |
808 | "aquota.group")) { | |
809 | log_debug("Skipping \"%s\".", sub_path); | |
810 | continue; | |
811 | } | |
3b63d2d3 | 812 | |
17b90525 | 813 | /* Ignore sockets that are listed in /proc/net/unix */ |
a5e4f3ab | 814 | if (S_ISSOCK(sx.stx_mode) && unix_socket_alive(c, sub_path)) { |
582deb84 | 815 | log_debug("Skipping \"%s\": live socket.", sub_path); |
17b90525 | 816 | continue; |
582deb84 | 817 | } |
17b90525 | 818 | |
78ab08eb | 819 | /* Ignore device nodes */ |
ca194a2a | 820 | if (S_ISCHR(sx.stx_mode) || S_ISBLK(sx.stx_mode)) { |
582deb84 | 821 | log_debug("Skipping \"%s\": a device.", sub_path); |
78ab08eb | 822 | continue; |
582deb84 | 823 | } |
78ab08eb | 824 | |
a23fabc6 | 825 | /* Keep files on this level around if this is requested */ |
582deb84 ZJS |
826 | if (keep_this_level) { |
827 | log_debug("Keeping \"%s\".", sub_path); | |
24f3a374 | 828 | continue; |
582deb84 | 829 | } |
24f3a374 | 830 | |
7f7a50dd SK |
831 | if (!needs_cleanup(atime_nsec, btime_nsec, ctime_nsec, mtime_nsec, |
832 | cutoff_nsec, sub_path, age_by_file, false)) | |
3b63d2d3 | 833 | continue; |
3b63d2d3 | 834 | |
420d2e31 DDM |
835 | fd = xopenat(dirfd(d), |
836 | de->d_name, | |
25e6ce1c | 837 | O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME|O_NONBLOCK, |
420d2e31 DDM |
838 | /* xopen_flags = */ 0, |
839 | /* mode = */ 0); | |
08c25eee | 840 | if (fd < 0 && !IN_SET(fd, -ENOENT, -ELOOP)) |
65e179a1 DDM |
841 | log_warning_errno(fd, "Opening file \"%s\" failed, ignoring: %m", sub_path); |
842 | if (fd >= 0 && flock(fd, LOCK_EX|LOCK_NB) < 0 && errno == EAGAIN) { | |
b572e8da | 843 | log_debug_errno(errno, "Couldn't acquire shared BSD lock on file \"%s\", skipping: %m", sub_path); |
65e179a1 DDM |
844 | continue; |
845 | } | |
846 | ||
781bc44e | 847 | log_debug("Removing \"%s\".", sub_path); |
c7f0d9e5 | 848 | if (unlinkat(dirfd(d), de->d_name, 0) < 0) |
582deb84 | 849 | if (errno != ENOENT) |
781bc44e | 850 | r = log_warning_errno(errno, "Failed to remove \"%s\", ignoring: %m", sub_path); |
582deb84 | 851 | |
3b63d2d3 LP |
852 | deleted = true; |
853 | } | |
854 | } | |
855 | ||
856 | finish: | |
857 | if (deleted) { | |
ddb439b8 | 858 | struct timespec ts[2]; |
76e15a9c | 859 | |
582deb84 ZJS |
860 | log_debug("Restoring access and modification time on \"%s\": %s, %s", |
861 | p, | |
0086ef19 ZJS |
862 | FORMAT_TIMESTAMP_STYLE(self_atime_nsec / NSEC_PER_USEC, TIMESTAMP_US), |
863 | FORMAT_TIMESTAMP_STYLE(self_mtime_nsec / NSEC_PER_USEC, TIMESTAMP_US)); | |
ddb439b8 LP |
864 | |
865 | timespec_store_nsec(ts + 0, self_atime_nsec); | |
866 | timespec_store_nsec(ts + 1, self_mtime_nsec); | |
76e15a9c LP |
867 | |
868 | /* Restore original directory timestamps */ | |
ddb439b8 | 869 | if (futimens(dirfd(d), ts) < 0) |
76e15a9c | 870 | log_warning_errno(errno, "Failed to revert timestamps of '%s', ignoring: %m", p); |
3b63d2d3 LP |
871 | } |
872 | ||
3b63d2d3 LP |
873 | return r; |
874 | } | |
875 | ||
5579f856 LP |
876 | static bool dangerous_hardlinks(void) { |
877 | _cleanup_free_ char *value = NULL; | |
878 | static int cached = -1; | |
879 | int r; | |
880 | ||
881 | /* Check whether the fs.protected_hardlinks sysctl is on. If we can't determine it we assume its off, as that's | |
882 | * what the upstream default is. */ | |
883 | ||
884 | if (cached >= 0) | |
885 | return cached; | |
886 | ||
887 | r = read_one_line_file("/proc/sys/fs/protected_hardlinks", &value); | |
888 | if (r < 0) { | |
889 | log_debug_errno(r, "Failed to read fs.protected_hardlinks sysctl: %m"); | |
890 | return true; | |
891 | } | |
892 | ||
893 | r = parse_boolean(value); | |
894 | if (r < 0) { | |
895 | log_debug_errno(r, "Failed to parse fs.protected_hardlinks sysctl: %m"); | |
896 | return true; | |
897 | } | |
898 | ||
899 | cached = r == 0; | |
900 | return cached; | |
901 | } | |
902 | ||
774f79b5 | 903 | static bool hardlink_vulnerable(const struct stat *st) { |
5579f856 LP |
904 | assert(st); |
905 | ||
906 | return !S_ISDIR(st->st_mode) && st->st_nlink > 1 && dangerous_hardlinks(); | |
907 | } | |
908 | ||
8b364a38 LP |
909 | static mode_t process_mask_perms(mode_t mode, mode_t current) { |
910 | ||
911 | if ((current & 0111) == 0) | |
912 | mode &= ~0111; | |
913 | if ((current & 0222) == 0) | |
914 | mode &= ~0222; | |
915 | if ((current & 0444) == 0) | |
916 | mode &= ~0444; | |
917 | if (!S_ISDIR(current)) | |
918 | mode &= ~07000; /* remove sticky/sgid/suid bit, unless directory */ | |
919 | ||
920 | return mode; | |
921 | } | |
922 | ||
a9bc518c | 923 | static int fd_set_perms( |
a5e4f3ab | 924 | Context *c, |
a9bc518c LP |
925 | Item *i, |
926 | int fd, | |
927 | const char *path, | |
928 | const struct stat *st, | |
929 | CreationMode creation) { | |
930 | ||
cc43328c | 931 | bool do_chown, do_chmod; |
b206ac8e | 932 | struct stat stbuf; |
a9f55d0e | 933 | mode_t new_mode; |
cc43328c LP |
934 | uid_t new_uid; |
935 | gid_t new_gid; | |
dee00c19 | 936 | int r; |
1924a97d | 937 | |
a5e4f3ab | 938 | assert(c); |
cde684a2 | 939 | assert(i); |
b4b0f87c | 940 | assert(fd >= 0); |
4dc7bfdf | 941 | assert(path); |
48b8aaa8 | 942 | |
936f6bdb FB |
943 | if (!i->mode_set && !i->uid_set && !i->gid_set) |
944 | goto shortcut; | |
1924a97d | 945 | |
b206ac8e FB |
946 | if (!st) { |
947 | if (fstat(fd, &stbuf) < 0) | |
948 | return log_error_errno(errno, "fstat(%s) failed: %m", path); | |
949 | st = &stbuf; | |
950 | } | |
951 | ||
baaa35ad ZJS |
952 | if (hardlink_vulnerable(st)) |
953 | return log_error_errno(SYNTHETIC_ERRNO(EPERM), | |
954 | "Refusing to set permissions on hardlinked file %s while the fs.protected_hardlinks sysctl is turned off.", | |
955 | path); | |
cc43328c LP |
956 | new_uid = i->uid_set && (creation != CREATION_EXISTING || !i->uid_only_create) ? i->uid : st->st_uid; |
957 | new_gid = i->gid_set && (creation != CREATION_EXISTING || !i->gid_only_create) ? i->gid : st->st_gid; | |
5579f856 | 958 | |
a9f55d0e | 959 | /* Do we need a chown()? */ |
cc43328c | 960 | do_chown = (new_uid != st->st_uid) || (new_gid != st->st_gid); |
8b364a38 | 961 | |
a9f55d0e | 962 | /* Calculate the mode to apply */ |
cc43328c LP |
963 | new_mode = i->mode_set && (creation != CREATION_EXISTING || !i->mode_only_create) ? |
964 | (i->mask_perms ? process_mask_perms(i->mode, st->st_mode) : i->mode) : | |
965 | (st->st_mode & 07777); | |
a9f55d0e | 966 | |
cc43328c LP |
967 | do_chmod = ((new_mode ^ st->st_mode) & 07777) != 0; |
968 | ||
969 | if (do_chmod && do_chown) { | |
a9f55d0e LP |
970 | /* Before we issue the chmod() let's reduce the access mode to the common bits of the old and |
971 | * the new mode. That way there's no time window where the file exists under the old owner | |
972 | * with more than the old access modes — and not under the new owner with more than the new | |
973 | * access modes either. */ | |
8b364a38 | 974 | |
936f6bdb | 975 | if (S_ISLNK(st->st_mode)) |
a9f55d0e | 976 | log_debug("Skipping temporary mode fix for symlink %s.", path); |
51207ca1 | 977 | else { |
a9f55d0e | 978 | mode_t m = new_mode & st->st_mode; /* Mask new mode by old mode */ |
abef3f91 | 979 | |
a9f55d0e LP |
980 | if (((m ^ st->st_mode) & 07777) == 0) |
981 | log_debug("\"%s\" matches temporary mode %o already.", path, m); | |
48b8aaa8 | 982 | else { |
a9f55d0e | 983 | log_debug("Temporarily changing \"%s\" to mode %o.", path, m); |
dee00c19 LP |
984 | r = fchmod_opath(fd, m); |
985 | if (r < 0) | |
986 | return log_error_errno(r, "fchmod() of %s failed: %m", path); | |
48b8aaa8 | 987 | } |
062e01bb | 988 | } |
51207ca1 | 989 | } |
062e01bb | 990 | |
a9f55d0e | 991 | if (do_chown) { |
cc43328c LP |
992 | log_debug("Changing \"%s\" to owner "UID_FMT":"GID_FMT, path, new_uid, new_gid); |
993 | ||
994 | if (fchownat(fd, "", | |
995 | new_uid != st->st_uid ? new_uid : UID_INVALID, | |
996 | new_gid != st->st_gid ? new_gid : GID_INVALID, | |
936f6bdb FB |
997 | AT_EMPTY_PATH) < 0) |
998 | return log_error_errno(errno, "fchownat() of %s failed: %m", path); | |
582deb84 | 999 | } |
062e01bb | 1000 | |
a9f55d0e LP |
1001 | /* Now, apply the final mode. We do this in two cases: when the user set a mode explicitly, or after a |
1002 | * chown(), since chown()'s mangle the access mode in regards to sgid/suid in some conditions. */ | |
cc43328c | 1003 | if (do_chmod || do_chown) { |
a9f55d0e LP |
1004 | if (S_ISLNK(st->st_mode)) |
1005 | log_debug("Skipping mode fix for symlink %s.", path); | |
1006 | else { | |
cc43328c LP |
1007 | log_debug("Changing \"%s\" to mode %o.", path, new_mode); |
1008 | r = fchmod_opath(fd, new_mode); | |
1009 | if (r < 0) | |
1010 | return log_error_errno(r, "fchmod() of %s failed: %m", path); | |
a9f55d0e LP |
1011 | } |
1012 | } | |
1013 | ||
59793c8f | 1014 | shortcut: |
03bc11d1 | 1015 | return label_fix_full(fd, /* inode_path= */ NULL, /* label_path= */ path, 0); |
062e01bb MS |
1016 | } |
1017 | ||
244c2a83 | 1018 | static int path_open_parent_safe(const char *path, bool allow_failure) { |
5ec9d065 | 1019 | _cleanup_free_ char *dn = NULL; |
a5648b80 | 1020 | int r, fd; |
5ec9d065 | 1021 | |
4609d3bb | 1022 | if (!path_is_normalized(path)) |
244c2a83 LB |
1023 | return log_full_errno(allow_failure ? LOG_INFO : LOG_ERR, |
1024 | SYNTHETIC_ERRNO(EINVAL), | |
1025 | "Failed to open parent of '%s': path not normalized%s.", | |
1026 | path, | |
1027 | allow_failure ? ", ignoring" : ""); | |
5ec9d065 | 1028 | |
4609d3bb LP |
1029 | r = path_extract_directory(path, &dn); |
1030 | if (r < 0) | |
244c2a83 LB |
1031 | return log_full_errno(allow_failure ? LOG_INFO : LOG_ERR, |
1032 | r, | |
1033 | "Unable to determine parent directory of '%s'%s: %m", | |
1034 | path, | |
1035 | allow_failure ? ", ignoring" : ""); | |
5ec9d065 | 1036 | |
f461a28d | 1037 | r = chase(dn, arg_root, allow_failure ? CHASE_SAFE : CHASE_SAFE|CHASE_WARN, NULL, &fd); |
4609d3bb LP |
1038 | if (r == -ENOLINK) /* Unsafe symlink: already covered by CHASE_WARN */ |
1039 | return r; | |
1040 | if (r < 0) | |
244c2a83 LB |
1041 | return log_full_errno(allow_failure ? LOG_INFO : LOG_ERR, |
1042 | r, | |
1043 | "Failed to open path '%s'%s: %m", | |
1044 | dn, | |
1045 | allow_failure ? ", ignoring" : ""); | |
5ec9d065 | 1046 | |
4609d3bb | 1047 | return fd; |
5ec9d065 FB |
1048 | } |
1049 | ||
addc3e30 | 1050 | static int path_open_safe(const char *path) { |
a5648b80 | 1051 | int r, fd; |
addc3e30 FB |
1052 | |
1053 | /* path_open_safe() returns a file descriptor opened with O_PATH after | |
1054 | * verifying that the path doesn't contain unsafe transitions, except | |
1055 | * for its final component as the function does not follow symlink. */ | |
1056 | ||
1057 | assert(path); | |
1058 | ||
baaa35ad | 1059 | if (!path_is_normalized(path)) |
4609d3bb | 1060 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to open invalid path '%s'.", path); |
addc3e30 | 1061 | |
f461a28d | 1062 | r = chase(path, arg_root, CHASE_SAFE|CHASE_WARN|CHASE_NOFOLLOW, NULL, &fd); |
4609d3bb LP |
1063 | if (r == -ENOLINK) |
1064 | return r; /* Unsafe symlink: already covered by CHASE_WARN */ | |
1065 | if (r < 0) | |
1066 | return log_error_errno(r, "Failed to open path %s: %m", path); | |
addc3e30 | 1067 | |
4609d3bb | 1068 | return fd; |
addc3e30 FB |
1069 | } |
1070 | ||
a9bc518c | 1071 | static int path_set_perms( |
a5e4f3ab | 1072 | Context *c, |
a9bc518c LP |
1073 | Item *i, |
1074 | const char *path, | |
1075 | CreationMode creation) { | |
1076 | ||
254d1313 | 1077 | _cleanup_close_ int fd = -EBADF; |
936f6bdb | 1078 | |
a5e4f3ab | 1079 | assert(c); |
936f6bdb FB |
1080 | assert(i); |
1081 | assert(path); | |
1082 | ||
addc3e30 | 1083 | fd = path_open_safe(path); |
54946021 | 1084 | if (fd < 0) |
addc3e30 | 1085 | return fd; |
d460ba18 | 1086 | |
a5e4f3ab | 1087 | return fd_set_perms(c, i, fd, path, /* st= */ NULL, creation); |
936f6bdb FB |
1088 | } |
1089 | ||
bd550f78 | 1090 | static int parse_xattrs_from_arg(Item *i) { |
ebf4e801 MW |
1091 | const char *p; |
1092 | int r; | |
1093 | ||
1094 | assert(i); | |
ebf4e801 | 1095 | |
3a632fc1 | 1096 | assert_se(p = i->argument); |
4034a06d | 1097 | for (;;) { |
4cef1923 | 1098 | _cleanup_free_ char *name = NULL, *value = NULL, *xattr = NULL; |
4034a06d | 1099 | |
4ec85141 | 1100 | r = extract_first_word(&p, &xattr, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE); |
4034a06d | 1101 | if (r < 0) |
bd550f78 | 1102 | log_warning_errno(r, "Failed to parse extended attribute '%s', ignoring: %m", p); |
4034a06d LP |
1103 | if (r <= 0) |
1104 | break; | |
505ef0e3 | 1105 | |
4cef1923 | 1106 | r = split_pair(xattr, "=", &name, &value); |
ebf4e801 | 1107 | if (r < 0) { |
4034a06d | 1108 | log_warning_errno(r, "Failed to parse extended attribute, ignoring: %s", xattr); |
ebf4e801 MW |
1109 | continue; |
1110 | } | |
505ef0e3 | 1111 | |
4034a06d | 1112 | if (isempty(name) || isempty(value)) { |
bd550f78 | 1113 | log_warning("Malformed extended attribute found, ignoring: %s", xattr); |
ebf4e801 MW |
1114 | continue; |
1115 | } | |
505ef0e3 | 1116 | |
4034a06d | 1117 | if (strv_push_pair(&i->xattrs, name, value) < 0) |
ebf4e801 | 1118 | return log_oom(); |
505ef0e3 | 1119 | |
4034a06d | 1120 | name = value = NULL; |
ebf4e801 MW |
1121 | } |
1122 | ||
4034a06d | 1123 | return 0; |
ebf4e801 MW |
1124 | } |
1125 | ||
a9bc518c | 1126 | static int fd_set_xattrs( |
a5e4f3ab | 1127 | Context *c, |
a9bc518c LP |
1128 | Item *i, |
1129 | int fd, | |
1130 | const char *path, | |
1131 | const struct stat *st, | |
1132 | CreationMode creation) { | |
1133 | ||
a5e4f3ab | 1134 | assert(c); |
ebf4e801 | 1135 | assert(i); |
b4b0f87c | 1136 | assert(fd >= 0); |
4dc7bfdf | 1137 | assert(path); |
936f6bdb | 1138 | |
ebf4e801 | 1139 | STRV_FOREACH_PAIR(name, value, i->xattrs) { |
bd550f78 | 1140 | log_debug("Setting extended attribute '%s=%s' on %s.", *name, *value, path); |
ddb6eeaf | 1141 | if (setxattr(FORMAT_PROC_FD_PATH(fd), *name, *value, strlen(*value), 0) < 0) |
25f027c5 ZJS |
1142 | return log_error_errno(errno, "Setting extended attribute %s=%s on %s failed: %m", |
1143 | *name, *value, path); | |
ebf4e801 MW |
1144 | } |
1145 | return 0; | |
1146 | } | |
1147 | ||
a9bc518c | 1148 | static int path_set_xattrs( |
a5e4f3ab | 1149 | Context *c, |
a9bc518c LP |
1150 | Item *i, |
1151 | const char *path, | |
1152 | CreationMode creation) { | |
1153 | ||
254d1313 | 1154 | _cleanup_close_ int fd = -EBADF; |
936f6bdb | 1155 | |
a5e4f3ab | 1156 | assert(c); |
936f6bdb FB |
1157 | assert(i); |
1158 | assert(path); | |
1159 | ||
addc3e30 | 1160 | fd = path_open_safe(path); |
936f6bdb | 1161 | if (fd < 0) |
addc3e30 | 1162 | return fd; |
936f6bdb | 1163 | |
a5e4f3ab | 1164 | return fd_set_xattrs(c, i, fd, path, /* st = */ NULL, creation); |
936f6bdb FB |
1165 | } |
1166 | ||
bd550f78 | 1167 | static int parse_acls_from_arg(Item *item) { |
349cc4a5 | 1168 | #if HAVE_ACL |
f8eeeaf9 | 1169 | int r; |
f8eeeaf9 ZJS |
1170 | |
1171 | assert(item); | |
1172 | ||
c55ac248 | 1173 | /* If append_or_force (= modify) is set, we will not modify the acl |
50d9e46d | 1174 | * afterwards, so the mask can be added now if necessary. */ |
bd550f78 | 1175 | |
26d98cdd MY |
1176 | r = parse_acl(item->argument, &item->acl_access, &item->acl_access_exec, |
1177 | &item->acl_default, !item->append_or_force); | |
f8eeeaf9 | 1178 | if (r < 0) |
f939a403 | 1179 | log_full_errno(arg_graceful && IN_SET(r, -EINVAL, -ENOENT, -ESRCH) ? LOG_DEBUG : LOG_WARNING, |
e0ea6af3 | 1180 | r, "Failed to parse ACL \"%s\", ignoring: %m", item->argument); |
f8eeeaf9 | 1181 | #else |
26d98cdd | 1182 | log_warning("ACLs are not supported, ignoring."); |
f8eeeaf9 ZJS |
1183 | #endif |
1184 | ||
1185 | return 0; | |
1186 | } | |
1187 | ||
349cc4a5 | 1188 | #if HAVE_ACL |
26d98cdd MY |
1189 | static int parse_acl_cond_exec( |
1190 | const char *path, | |
1191 | acl_t access, /* could be empty (NULL) */ | |
1192 | acl_t cond_exec, | |
1193 | const struct stat *st, | |
1194 | bool append, | |
1195 | acl_t *ret) { | |
1196 | ||
1197 | _cleanup_(acl_freep) acl_t parsed = NULL; | |
1198 | acl_entry_t entry; | |
1199 | acl_permset_t permset; | |
1200 | bool has_exec; | |
1201 | int r; | |
1202 | ||
1203 | assert(path); | |
1204 | assert(ret); | |
1205 | assert(st); | |
1206 | ||
1207 | parsed = access ? acl_dup(access) : acl_init(0); | |
1208 | if (!parsed) | |
1209 | return -errno; | |
1210 | ||
1211 | /* Since we substitute 'X' with 'x' in parse_acl(), we just need to copy the entries over | |
1212 | * for directories */ | |
1213 | if (S_ISDIR(st->st_mode)) { | |
1214 | for (r = acl_get_entry(cond_exec, ACL_FIRST_ENTRY, &entry); | |
1215 | r > 0; | |
1216 | r = acl_get_entry(cond_exec, ACL_NEXT_ENTRY, &entry)) { | |
1217 | ||
1218 | acl_entry_t parsed_entry; | |
1219 | ||
1220 | if (acl_create_entry(&parsed, &parsed_entry) < 0) | |
1221 | return -errno; | |
1222 | ||
1223 | if (acl_copy_entry(parsed_entry, entry) < 0) | |
1224 | return -errno; | |
1225 | } | |
1226 | if (r < 0) | |
1227 | return -errno; | |
1228 | ||
1229 | goto finish; | |
1230 | } | |
1231 | ||
1232 | has_exec = st->st_mode & S_IXUSR; | |
1233 | ||
1234 | if (!has_exec && append) { | |
1235 | _cleanup_(acl_freep) acl_t old = NULL; | |
1236 | ||
1237 | old = acl_get_file(path, ACL_TYPE_ACCESS); | |
1238 | if (!old) | |
1239 | return -errno; | |
1240 | ||
1241 | for (r = acl_get_entry(old, ACL_FIRST_ENTRY, &entry); | |
1242 | r > 0; | |
1243 | r = acl_get_entry(old, ACL_NEXT_ENTRY, &entry)) { | |
1244 | ||
1245 | if (acl_get_permset(entry, &permset) < 0) | |
1246 | return -errno; | |
1247 | ||
1248 | r = acl_get_perm(permset, ACL_EXECUTE); | |
1249 | if (r < 0) | |
1250 | return -errno; | |
1251 | if (r > 0) { | |
1252 | has_exec = true; | |
1253 | break; | |
1254 | } | |
1255 | } | |
1256 | if (r < 0) | |
1257 | return -errno; | |
1258 | } | |
1259 | ||
1260 | /* Check if we're about to set the execute bit in acl_access */ | |
1261 | if (!has_exec && access) { | |
1262 | for (r = acl_get_entry(access, ACL_FIRST_ENTRY, &entry); | |
1263 | r > 0; | |
1264 | r = acl_get_entry(access, ACL_NEXT_ENTRY, &entry)) { | |
1265 | ||
1266 | if (acl_get_permset(entry, &permset) < 0) | |
1267 | return -errno; | |
1268 | ||
1269 | r = acl_get_perm(permset, ACL_EXECUTE); | |
1270 | if (r < 0) | |
1271 | return -errno; | |
1272 | if (r > 0) { | |
1273 | has_exec = true; | |
1274 | break; | |
1275 | } | |
1276 | } | |
1277 | if (r < 0) | |
1278 | return -errno; | |
1279 | } | |
1280 | ||
1281 | for (r = acl_get_entry(cond_exec, ACL_FIRST_ENTRY, &entry); | |
1282 | r > 0; | |
1283 | r = acl_get_entry(cond_exec, ACL_NEXT_ENTRY, &entry)) { | |
1284 | ||
1285 | acl_entry_t parsed_entry; | |
1286 | ||
1287 | if (acl_create_entry(&parsed, &parsed_entry) < 0) | |
1288 | return -errno; | |
1289 | ||
1290 | if (acl_copy_entry(parsed_entry, entry) < 0) | |
1291 | return -errno; | |
1292 | ||
1293 | if (!has_exec) { | |
1294 | if (acl_get_permset(parsed_entry, &permset) < 0) | |
1295 | return -errno; | |
1296 | ||
1297 | if (acl_delete_perm(permset, ACL_EXECUTE) < 0) | |
1298 | return -errno; | |
1299 | } | |
1300 | } | |
1301 | if (r < 0) | |
1302 | return -errno; | |
1303 | ||
1304 | finish: | |
1305 | if (!append) { /* want_mask = true */ | |
1306 | r = calc_acl_mask_if_needed(&parsed); | |
1307 | if (r < 0) | |
1308 | return r; | |
1309 | } | |
1310 | ||
1311 | *ret = TAKE_PTR(parsed); | |
1312 | ||
1313 | return 0; | |
1314 | } | |
1315 | ||
a9bc518c | 1316 | static int path_set_acl( |
a5e4f3ab | 1317 | Context *c, |
a9bc518c LP |
1318 | const char *path, |
1319 | const char *pretty, | |
1320 | acl_type_t type, | |
1321 | acl_t acl, | |
1322 | bool modify) { | |
1323 | ||
48b8aaa8 | 1324 | _cleanup_(acl_free_charpp) char *t = NULL; |
dd4105b0 | 1325 | _cleanup_(acl_freep) acl_t dup = NULL; |
50d9e46d ZJS |
1326 | int r; |
1327 | ||
a5e4f3ab LP |
1328 | assert(c); |
1329 | ||
1330 | /* Returns 0 for success, positive error if already warned, negative error otherwise. */ | |
d873e877 | 1331 | |
50d9e46d | 1332 | if (modify) { |
dd4105b0 | 1333 | r = acls_for_file(path, type, acl, &dup); |
50d9e46d ZJS |
1334 | if (r < 0) |
1335 | return r; | |
50d9e46d | 1336 | |
dd4105b0 ZJS |
1337 | r = calc_acl_mask_if_needed(&dup); |
1338 | if (r < 0) | |
1339 | return r; | |
1340 | } else { | |
1341 | dup = acl_dup(acl); | |
1342 | if (!dup) | |
1343 | return -errno; | |
1344 | ||
1345 | /* the mask was already added earlier if needed */ | |
1346 | } | |
1347 | ||
1348 | r = add_base_acls_if_needed(&dup, path); | |
1349 | if (r < 0) | |
1350 | return r; | |
1351 | ||
582deb84 | 1352 | t = acl_to_any_text(dup, NULL, ',', TEXT_ABBREVIATE); |
48b8aaa8 | 1353 | log_debug("Setting %s ACL %s on %s.", |
582deb84 | 1354 | type == ACL_TYPE_ACCESS ? "access" : "default", |
48b8aaa8 | 1355 | strna(t), pretty); |
50d9e46d | 1356 | |
582deb84 | 1357 | r = acl_set_file(path, type, dup); |
3045c416 PC |
1358 | if (r < 0) { |
1359 | if (ERRNO_IS_NOT_SUPPORTED(errno)) | |
1360 | /* No error if filesystem doesn't support ACLs. Return negative. */ | |
1361 | return -errno; | |
1362 | else | |
1363 | /* Return positive to indicate we already warned */ | |
1364 | return -log_error_errno(errno, | |
1365 | "Setting %s ACL \"%s\" on %s failed: %m", | |
1366 | type == ACL_TYPE_ACCESS ? "access" : "default", | |
1367 | strna(t), pretty); | |
1368 | } | |
582deb84 | 1369 | return 0; |
50d9e46d | 1370 | } |
35888b67 | 1371 | #endif |
50d9e46d | 1372 | |
a9bc518c | 1373 | static int fd_set_acls( |
a5e4f3ab | 1374 | Context *c, |
a9bc518c LP |
1375 | Item *item, |
1376 | int fd, | |
1377 | const char *path, | |
1378 | const struct stat *st, | |
1379 | CreationMode creation) { | |
1380 | ||
d873e877 | 1381 | int r = 0; |
349cc4a5 | 1382 | #if HAVE_ACL |
26d98cdd | 1383 | _cleanup_(acl_freep) acl_t access_with_exec_parsed = NULL; |
b206ac8e | 1384 | struct stat stbuf; |
48b8aaa8 | 1385 | |
a5e4f3ab | 1386 | assert(c); |
f8eeeaf9 | 1387 | assert(item); |
b4b0f87c | 1388 | assert(fd >= 0); |
4dc7bfdf | 1389 | assert(path); |
48b8aaa8 | 1390 | |
b206ac8e FB |
1391 | if (!st) { |
1392 | if (fstat(fd, &stbuf) < 0) | |
1393 | return log_error_errno(errno, "fstat(%s) failed: %m", path); | |
1394 | st = &stbuf; | |
1395 | } | |
1396 | ||
baaa35ad ZJS |
1397 | if (hardlink_vulnerable(st)) |
1398 | return log_error_errno(SYNTHETIC_ERRNO(EPERM), | |
1399 | "Refusing to set ACLs on hardlinked file %s while the fs.protected_hardlinks sysctl is turned off.", | |
1400 | path); | |
5579f856 | 1401 | |
936f6bdb | 1402 | if (S_ISLNK(st->st_mode)) { |
48b8aaa8 LP |
1403 | log_debug("Skipping ACL fix for symlink %s.", path); |
1404 | return 0; | |
1405 | } | |
1406 | ||
26d98cdd MY |
1407 | if (item->acl_access_exec) { |
1408 | r = parse_acl_cond_exec(FORMAT_PROC_FD_PATH(fd), | |
1409 | item->acl_access, | |
1410 | item->acl_access_exec, | |
1411 | st, | |
1412 | item->append_or_force, | |
1413 | &access_with_exec_parsed); | |
1414 | if (r < 0) | |
1415 | return log_error_errno(r, "Failed to parse conditionalized execute bit for \"%s\": %m", path); | |
1416 | ||
a5e4f3ab | 1417 | r = path_set_acl(c, FORMAT_PROC_FD_PATH(fd), path, ACL_TYPE_ACCESS, access_with_exec_parsed, item->append_or_force); |
26d98cdd | 1418 | } else if (item->acl_access) |
a5e4f3ab | 1419 | r = path_set_acl(c, FORMAT_PROC_FD_PATH(fd), path, ACL_TYPE_ACCESS, item->acl_access, item->append_or_force); |
f8eeeaf9 | 1420 | |
2667385b RM |
1421 | /* set only default acls to folders */ |
1422 | if (r == 0 && item->acl_default && S_ISDIR(st->st_mode)) | |
a5e4f3ab | 1423 | r = path_set_acl(c, FORMAT_PROC_FD_PATH(fd), path, ACL_TYPE_DEFAULT, item->acl_default, item->append_or_force); |
f8eeeaf9 | 1424 | |
3045c416 PC |
1425 | if (ERRNO_IS_NOT_SUPPORTED(r)) { |
1426 | log_debug_errno(r, "ACLs not supported by file system at %s", path); | |
1427 | return 0; | |
1428 | } | |
1429 | ||
d873e877 | 1430 | if (r > 0) |
26d98cdd | 1431 | return -r; /* already warned in path_set_acl */ |
f8606626 LP |
1432 | |
1433 | /* The above procfs paths don't work if /proc is not mounted. */ | |
1434 | if (r == -ENOENT && proc_mounted() == 0) | |
1435 | r = -ENOSYS; | |
1436 | ||
936f6bdb FB |
1437 | if (r < 0) |
1438 | return log_error_errno(r, "ACL operation on \"%s\" failed: %m", path); | |
d873e877 HPD |
1439 | #endif |
1440 | return r; | |
f8eeeaf9 ZJS |
1441 | } |
1442 | ||
a5e4f3ab LP |
1443 | static int path_set_acls( |
1444 | Context *c, | |
1445 | Item *item, | |
1446 | const char *path, | |
1447 | CreationMode creation) { | |
1448 | ||
936f6bdb | 1449 | int r = 0; |
545cdb90 | 1450 | #if HAVE_ACL |
254d1313 | 1451 | _cleanup_close_ int fd = -EBADF; |
936f6bdb | 1452 | |
a5e4f3ab | 1453 | assert(c); |
936f6bdb FB |
1454 | assert(item); |
1455 | assert(path); | |
1456 | ||
addc3e30 | 1457 | fd = path_open_safe(path); |
936f6bdb | 1458 | if (fd < 0) |
addc3e30 | 1459 | return fd; |
936f6bdb | 1460 | |
a5e4f3ab | 1461 | r = fd_set_acls(c, item, fd, path, /* st= */ NULL, creation); |
545cdb90 YW |
1462 | #endif |
1463 | return r; | |
1464 | } | |
936f6bdb | 1465 | |
bd550f78 | 1466 | static int parse_attribute_from_arg(Item *item) { |
88ec4dfa LP |
1467 | static const struct { |
1468 | char character; | |
1469 | unsigned value; | |
1470 | } attributes[] = { | |
1471 | { 'A', FS_NOATIME_FL }, /* do not update atime */ | |
1472 | { 'S', FS_SYNC_FL }, /* Synchronous updates */ | |
1473 | { 'D', FS_DIRSYNC_FL }, /* dirsync behaviour (directories only) */ | |
1474 | { 'a', FS_APPEND_FL }, /* writes to file may only append */ | |
1475 | { 'c', FS_COMPR_FL }, /* Compress file */ | |
1476 | { 'd', FS_NODUMP_FL }, /* do not dump file */ | |
8c35b2ca | 1477 | { 'e', FS_EXTENT_FL }, /* Extents */ |
88ec4dfa LP |
1478 | { 'i', FS_IMMUTABLE_FL }, /* Immutable file */ |
1479 | { 'j', FS_JOURNAL_DATA_FL }, /* Reserved for ext3 */ | |
1480 | { 's', FS_SECRM_FL }, /* Secure deletion */ | |
1481 | { 'u', FS_UNRM_FL }, /* Undelete */ | |
1482 | { 't', FS_NOTAIL_FL }, /* file tail should not be merged */ | |
13e785f7 | 1483 | { 'T', FS_TOPDIR_FL }, /* Top of directory hierarchies */ |
88ec4dfa | 1484 | { 'C', FS_NOCOW_FL }, /* Do not cow file */ |
75006470 | 1485 | { 'P', FS_PROJINHERIT_FL }, /* Inherit the quota project ID */ |
22c3a6ca | 1486 | }; |
88ec4dfa | 1487 | |
22c3a6ca GB |
1488 | enum { |
1489 | MODE_ADD, | |
1490 | MODE_DEL, | |
1491 | MODE_SET | |
1492 | } mode = MODE_ADD; | |
22c3a6ca | 1493 | |
88ec4dfa LP |
1494 | unsigned value = 0, mask = 0; |
1495 | const char *p; | |
22c3a6ca | 1496 | |
88ec4dfa LP |
1497 | assert(item); |
1498 | ||
1499 | p = item->argument; | |
1500 | if (p) { | |
1501 | if (*p == '+') { | |
1502 | mode = MODE_ADD; | |
1503 | p++; | |
1504 | } else if (*p == '-') { | |
1505 | mode = MODE_DEL; | |
1506 | p++; | |
1507 | } else if (*p == '=') { | |
1508 | mode = MODE_SET; | |
1509 | p++; | |
1510 | } | |
22c3a6ca GB |
1511 | } |
1512 | ||
baaa35ad ZJS |
1513 | if (isempty(p) && mode != MODE_SET) |
1514 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1515 | "Setting file attribute on '%s' needs an attribute specification.", | |
1516 | item->path); | |
88ec4dfa LP |
1517 | |
1518 | for (; p && *p ; p++) { | |
1519 | unsigned i, v; | |
1520 | ||
1521 | for (i = 0; i < ELEMENTSOF(attributes); i++) | |
1522 | if (*p == attributes[i].character) | |
1523 | break; | |
1524 | ||
baaa35ad ZJS |
1525 | if (i >= ELEMENTSOF(attributes)) |
1526 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1527 | "Unknown file attribute '%c' on '%s'.", | |
1528 | *p, item->path); | |
88ec4dfa LP |
1529 | |
1530 | v = attributes[i].value; | |
1531 | ||
3742095b | 1532 | SET_FLAG(value, v, IN_SET(mode, MODE_ADD, MODE_SET)); |
88ec4dfa LP |
1533 | |
1534 | mask |= v; | |
22c3a6ca GB |
1535 | } |
1536 | ||
1537 | if (mode == MODE_SET) | |
d629ba70 | 1538 | mask |= CHATTR_ALL_FL; |
22c3a6ca | 1539 | |
88ec4dfa | 1540 | assert(mask != 0); |
22c3a6ca | 1541 | |
88ec4dfa LP |
1542 | item->attribute_mask = mask; |
1543 | item->attribute_value = value; | |
1544 | item->attribute_set = true; | |
22c3a6ca GB |
1545 | |
1546 | return 0; | |
22c3a6ca GB |
1547 | } |
1548 | ||
a9bc518c | 1549 | static int fd_set_attribute( |
a5e4f3ab | 1550 | Context *c, |
a9bc518c LP |
1551 | Item *item, |
1552 | int fd, | |
1553 | const char *path, | |
1554 | const struct stat *st, | |
1555 | CreationMode creation) { | |
1556 | ||
254d1313 | 1557 | _cleanup_close_ int procfs_fd = -EBADF; |
b206ac8e | 1558 | struct stat stbuf; |
88ec4dfa LP |
1559 | unsigned f; |
1560 | int r; | |
22c3a6ca | 1561 | |
a5e4f3ab | 1562 | assert(c); |
4dc7bfdf | 1563 | assert(item); |
b4b0f87c | 1564 | assert(fd >= 0); |
4dc7bfdf FB |
1565 | assert(path); |
1566 | ||
88ec4dfa | 1567 | if (!item->attribute_set || item->attribute_mask == 0) |
22c3a6ca | 1568 | return 0; |
22c3a6ca | 1569 | |
b206ac8e FB |
1570 | if (!st) { |
1571 | if (fstat(fd, &stbuf) < 0) | |
1572 | return log_error_errno(errno, "fstat(%s) failed: %m", path); | |
1573 | st = &stbuf; | |
1574 | } | |
1575 | ||
c5d554aa LP |
1576 | /* Issuing the file attribute ioctls on device nodes is not safe, as that will be delivered to the |
1577 | * drivers, not the file system containing the device node. */ | |
baaa35ad ZJS |
1578 | if (!S_ISREG(st->st_mode) && !S_ISDIR(st->st_mode)) |
1579 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1580 | "Setting file flags is only supported on regular files and directories, cannot set on '%s'.", | |
1581 | path); | |
88ec4dfa LP |
1582 | |
1583 | f = item->attribute_value & item->attribute_mask; | |
1584 | ||
1585 | /* Mask away directory-specific flags */ | |
936f6bdb | 1586 | if (!S_ISDIR(st->st_mode)) |
22c3a6ca | 1587 | f &= ~FS_DIRSYNC_FL; |
88ec4dfa | 1588 | |
f2324783 | 1589 | procfs_fd = fd_reopen(fd, O_RDONLY|O_CLOEXEC|O_NOATIME); |
936f6bdb | 1590 | if (procfs_fd < 0) |
c3e03778 | 1591 | return log_error_errno(procfs_fd, "Failed to re-open '%s': %m", path); |
936f6bdb | 1592 | |
94566540 | 1593 | unsigned previous, current; |
cf91b915 | 1594 | r = chattr_full(procfs_fd, NULL, f, item->attribute_mask, &previous, ¤t, CHATTR_FALLBACK_BITWISE); |
94566540 | 1595 | if (r == -ENOANO) |
d1a61dfa | 1596 | log_warning("Cannot set file attributes for '%s', maybe due to incompatibility in specified attributes, " |
94566540 YW |
1597 | "previous=0x%08x, current=0x%08x, expected=0x%08x, ignoring.", |
1598 | path, previous, current, (previous & ~item->attribute_mask) | (f & item->attribute_mask)); | |
1599 | else if (r < 0) | |
1600 | log_full_errno(ERRNO_IS_NOT_SUPPORTED(r) ? LOG_DEBUG : LOG_WARNING, r, | |
1601 | "Cannot set file attributes for '%s', value=0x%08x, mask=0x%08x, ignoring: %m", | |
ad75a97f | 1602 | path, item->attribute_value, item->attribute_mask); |
22c3a6ca GB |
1603 | |
1604 | return 0; | |
1605 | } | |
1606 | ||
a5e4f3ab LP |
1607 | static int path_set_attribute( |
1608 | Context *c, | |
1609 | Item *item, | |
1610 | const char *path, | |
1611 | CreationMode creation) { | |
1612 | ||
254d1313 | 1613 | _cleanup_close_ int fd = -EBADF; |
936f6bdb | 1614 | |
a5e4f3ab LP |
1615 | assert(c); |
1616 | assert(item); | |
1617 | ||
936f6bdb FB |
1618 | if (!item->attribute_set || item->attribute_mask == 0) |
1619 | return 0; | |
1620 | ||
addc3e30 | 1621 | fd = path_open_safe(path); |
936f6bdb | 1622 | if (fd < 0) |
addc3e30 | 1623 | return fd; |
936f6bdb | 1624 | |
a5e4f3ab | 1625 | return fd_set_attribute(c, item, fd, path, /* st= */ NULL, creation); |
936f6bdb FB |
1626 | } |
1627 | ||
708daf42 LP |
1628 | static int write_argument_data(Item *i, int fd, const char *path) { |
1629 | int r; | |
1630 | ||
1631 | assert(i); | |
1632 | assert(fd >= 0); | |
1633 | assert(path); | |
1634 | ||
1635 | if (item_binary_argument_size(i) == 0) | |
1636 | return 0; | |
1637 | ||
1638 | assert(item_binary_argument(i)); | |
1639 | ||
1640 | log_debug("Writing to \"%s\".", path); | |
1641 | ||
e22c60a9 | 1642 | r = loop_write(fd, item_binary_argument(i), item_binary_argument_size(i)); |
708daf42 LP |
1643 | if (r < 0) |
1644 | return log_error_errno(r, "Failed to write file \"%s\": %m", path); | |
1645 | ||
1646 | return 0; | |
1647 | } | |
1648 | ||
a5e4f3ab | 1649 | static int write_one_file(Context *c, Item *i, const char *path, CreationMode creation) { |
254d1313 | 1650 | _cleanup_close_ int fd = -EBADF, dir_fd = -EBADF; |
4609d3bb | 1651 | _cleanup_free_ char *bn = NULL; |
1baaf8aa | 1652 | int r; |
31c84ff1 | 1653 | |
a5e4f3ab | 1654 | assert(c); |
31c84ff1 FB |
1655 | assert(i); |
1656 | assert(path); | |
31c84ff1 FB |
1657 | assert(i->type == WRITE_FILE); |
1658 | ||
4609d3bb LP |
1659 | r = path_extract_filename(path, &bn); |
1660 | if (r < 0) | |
1661 | return log_error_errno(r, "Failed to extract filename from path '%s': %m", path); | |
1662 | if (r == O_DIRECTORY) | |
1663 | return log_error_errno(SYNTHETIC_ERRNO(EISDIR), "Cannot open path '%s' for writing, is a directory.", path); | |
1664 | ||
1665 | /* Validate the path and keep the fd on the directory for opening the file so we're sure that it | |
1666 | * can't be changed behind our back. */ | |
244c2a83 | 1667 | dir_fd = path_open_parent_safe(path, i->allow_failure); |
551470ec FB |
1668 | if (dir_fd < 0) |
1669 | return dir_fd; | |
1670 | ||
31c84ff1 | 1671 | /* Follows symlinks */ |
1baaf8aa LP |
1672 | fd = openat(dir_fd, bn, |
1673 | O_NONBLOCK|O_CLOEXEC|O_WRONLY|O_NOCTTY|(i->append_or_force ? O_APPEND : 0), | |
1674 | i->mode); | |
31c84ff1 FB |
1675 | if (fd < 0) { |
1676 | if (errno == ENOENT) { | |
1677 | log_debug_errno(errno, "Not writing missing file \"%s\": %m", path); | |
1678 | return 0; | |
1679 | } | |
1baaf8aa LP |
1680 | |
1681 | if (i->allow_failure) | |
1682 | return log_debug_errno(errno, "Failed to open file \"%s\", ignoring: %m", path); | |
1683 | ||
31c84ff1 FB |
1684 | return log_error_errno(errno, "Failed to open file \"%s\": %m", path); |
1685 | } | |
1686 | ||
1687 | /* 'w' is allowed to write into any kind of files. */ | |
31c84ff1 | 1688 | |
708daf42 | 1689 | r = write_argument_data(i, fd, path); |
31c84ff1 | 1690 | if (r < 0) |
708daf42 | 1691 | return r; |
31c84ff1 | 1692 | |
a5e4f3ab | 1693 | return fd_set_perms(c, i, fd, path, NULL, creation); |
31c84ff1 FB |
1694 | } |
1695 | ||
a5e4f3ab LP |
1696 | static int create_file( |
1697 | Context *c, | |
1698 | Item *i, | |
1699 | const char *path) { | |
1700 | ||
254d1313 | 1701 | _cleanup_close_ int fd = -EBADF, dir_fd = -EBADF; |
4609d3bb | 1702 | _cleanup_free_ char *bn = NULL; |
5ec9d065 | 1703 | struct stat stbuf, *st = NULL; |
a9bc518c | 1704 | CreationMode creation; |
5ec9d065 | 1705 | int r = 0; |
d4e9eb91 | 1706 | |
a5e4f3ab | 1707 | assert(c); |
874f1947 LP |
1708 | assert(i); |
1709 | assert(path); | |
5ec9d065 FB |
1710 | assert(i->type == CREATE_FILE); |
1711 | ||
1712 | /* 'f' operates on regular files exclusively. */ | |
1713 | ||
4609d3bb LP |
1714 | r = path_extract_filename(path, &bn); |
1715 | if (r < 0) | |
1716 | return log_error_errno(r, "Failed to extract filename from path '%s': %m", path); | |
1717 | if (r == O_DIRECTORY) | |
1718 | return log_error_errno(SYNTHETIC_ERRNO(EISDIR), "Cannot open path '%s' for writing, is a directory.", path); | |
1719 | ||
1720 | /* Validate the path and keep the fd on the directory for opening the file so we're sure that it | |
1721 | * can't be changed behind our back. */ | |
244c2a83 | 1722 | dir_fd = path_open_parent_safe(path, i->allow_failure); |
5ec9d065 FB |
1723 | if (dir_fd < 0) |
1724 | return dir_fd; | |
874f1947 | 1725 | |
2053593f | 1726 | WITH_UMASK(0000) { |
ecabcf8b | 1727 | mac_selinux_create_file_prepare(path, S_IFREG); |
05c4affe | 1728 | fd = RET_NERRNO(openat(dir_fd, bn, O_CREAT|O_EXCL|O_NOFOLLOW|O_NONBLOCK|O_CLOEXEC|O_WRONLY|O_NOCTTY, i->mode)); |
ecabcf8b | 1729 | mac_selinux_create_file_clear(); |
5c0d398d | 1730 | } |
d4e9eb91 DR |
1731 | |
1732 | if (fd < 0) { | |
c5d554aa LP |
1733 | /* Even on a read-only filesystem, open(2) returns EEXIST if the file already exists. It |
1734 | * returns EROFS only if it needs to create the file. */ | |
05c4affe LP |
1735 | if (fd != -EEXIST) |
1736 | return log_error_errno(fd, "Failed to create file %s: %m", path); | |
5ec9d065 | 1737 | |
c5d554aa LP |
1738 | /* Re-open the file. At that point it must exist since open(2) failed with EEXIST. We still |
1739 | * need to check if the perms/mode need to be changed. For read-only filesystems, we let | |
1740 | * fd_set_perms() report the error if the perms need to be modified. */ | |
5ec9d065 FB |
1741 | fd = openat(dir_fd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH, i->mode); |
1742 | if (fd < 0) | |
1743 | return log_error_errno(errno, "Failed to re-open file %s: %m", path); | |
1744 | ||
1745 | if (fstat(fd, &stbuf) < 0) | |
1746 | return log_error_errno(errno, "stat(%s) failed: %m", path); | |
1747 | ||
d7a0f1f4 FS |
1748 | if (!S_ISREG(stbuf.st_mode)) |
1749 | return log_error_errno(SYNTHETIC_ERRNO(EEXIST), | |
1750 | "%s exists and is not a regular file.", | |
1751 | path); | |
d4e9eb91 | 1752 | |
5ec9d065 | 1753 | st = &stbuf; |
a9bc518c LP |
1754 | creation = CREATION_EXISTING; |
1755 | } else { | |
8f6fb95c LP |
1756 | r = write_argument_data(i, fd, path); |
1757 | if (r < 0) | |
1758 | return r; | |
a9bc518c LP |
1759 | |
1760 | creation = CREATION_NORMAL; | |
d4e9eb91 DR |
1761 | } |
1762 | ||
a5e4f3ab | 1763 | return fd_set_perms(c, i, fd, path, st, creation); |
5ec9d065 | 1764 | } |
d4e9eb91 | 1765 | |
a5e4f3ab LP |
1766 | static int truncate_file( |
1767 | Context *c, | |
1768 | Item *i, | |
1769 | const char *path) { | |
1770 | ||
254d1313 | 1771 | _cleanup_close_ int fd = -EBADF, dir_fd = -EBADF; |
4609d3bb | 1772 | _cleanup_free_ char *bn = NULL; |
5ec9d065 | 1773 | struct stat stbuf, *st = NULL; |
a9bc518c | 1774 | CreationMode creation; |
5ec9d065 FB |
1775 | bool erofs = false; |
1776 | int r = 0; | |
1777 | ||
a5e4f3ab | 1778 | assert(c); |
5ec9d065 FB |
1779 | assert(i); |
1780 | assert(path); | |
eccebf4b | 1781 | assert(i->type == TRUNCATE_FILE || (i->type == CREATE_FILE && i->append_or_force)); |
d4e9eb91 | 1782 | |
4609d3bb LP |
1783 | /* We want to operate on regular file exclusively especially since O_TRUNC is unspecified if the file |
1784 | * is neither a regular file nor a fifo nor a terminal device. Therefore we first open the file and | |
1785 | * make sure it's a regular one before truncating it. */ | |
1786 | ||
1787 | r = path_extract_filename(path, &bn); | |
1788 | if (r < 0) | |
1789 | return log_error_errno(r, "Failed to extract filename from path '%s': %m", path); | |
1790 | if (r == O_DIRECTORY) | |
1791 | return log_error_errno(SYNTHETIC_ERRNO(EISDIR), "Cannot open path '%s' for truncation, is a directory.", path); | |
3612fbc1 | 1792 | |
4609d3bb LP |
1793 | /* Validate the path and keep the fd on the directory for opening the file so we're sure that it |
1794 | * can't be changed behind our back. */ | |
244c2a83 | 1795 | dir_fd = path_open_parent_safe(path, i->allow_failure); |
14ab804e FB |
1796 | if (dir_fd < 0) |
1797 | return dir_fd; | |
1798 | ||
a9bc518c LP |
1799 | creation = CREATION_EXISTING; |
1800 | fd = RET_NERRNO(openat(dir_fd, bn, O_NOFOLLOW|O_NONBLOCK|O_CLOEXEC|O_WRONLY|O_NOCTTY, i->mode)); | |
1801 | if (fd == -ENOENT) { | |
1802 | creation = CREATION_NORMAL; /* Didn't work without O_CREATE, try again with */ | |
1803 | ||
2053593f | 1804 | WITH_UMASK(0000) { |
a9bc518c LP |
1805 | mac_selinux_create_file_prepare(path, S_IFREG); |
1806 | fd = RET_NERRNO(openat(dir_fd, bn, O_CREAT|O_NOFOLLOW|O_NONBLOCK|O_CLOEXEC|O_WRONLY|O_NOCTTY, i->mode)); | |
1807 | mac_selinux_create_file_clear(); | |
1808 | } | |
5ec9d065 FB |
1809 | } |
1810 | ||
1811 | if (fd < 0) { | |
05c4affe LP |
1812 | if (fd != -EROFS) |
1813 | return log_error_errno(fd, "Failed to open/create file %s: %m", path); | |
5ec9d065 | 1814 | |
c5d554aa LP |
1815 | /* On a read-only filesystem, we don't want to fail if the target is already empty and the |
1816 | * perms are set. So we still proceed with the sanity checks and let the remaining operations | |
1817 | * fail with EROFS if they try to modify the target file. */ | |
5ec9d065 | 1818 | |
14ab804e | 1819 | fd = openat(dir_fd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH, i->mode); |
5ec9d065 | 1820 | if (fd < 0) { |
d7a0f1f4 FS |
1821 | if (errno == ENOENT) |
1822 | return log_error_errno(SYNTHETIC_ERRNO(EROFS), | |
1823 | "Cannot create file %s on a read-only file system.", | |
1824 | path); | |
5ec9d065 FB |
1825 | |
1826 | return log_error_errno(errno, "Failed to re-open file %s: %m", path); | |
1827 | } | |
1828 | ||
1829 | erofs = true; | |
a9bc518c | 1830 | creation = CREATION_EXISTING; |
5ec9d065 FB |
1831 | } |
1832 | ||
1833 | if (fstat(fd, &stbuf) < 0) | |
4a62c710 | 1834 | return log_error_errno(errno, "stat(%s) failed: %m", path); |
d4e9eb91 | 1835 | |
d7a0f1f4 FS |
1836 | if (!S_ISREG(stbuf.st_mode)) |
1837 | return log_error_errno(SYNTHETIC_ERRNO(EEXIST), | |
1838 | "%s exists and is not a regular file.", | |
1839 | path); | |
d4e9eb91 | 1840 | |
5ec9d065 FB |
1841 | if (stbuf.st_size > 0) { |
1842 | if (ftruncate(fd, 0) < 0) { | |
1843 | r = erofs ? -EROFS : -errno; | |
1844 | return log_error_errno(r, "Failed to truncate file %s: %m", path); | |
1845 | } | |
1846 | } else | |
1847 | st = &stbuf; | |
d4e9eb91 | 1848 | |
5ec9d065 FB |
1849 | log_debug("\"%s\" has been created.", path); |
1850 | ||
708daf42 LP |
1851 | if (item_binary_argument(i)) { |
1852 | r = write_argument_data(i, fd, path); | |
2f3745dd | 1853 | if (r < 0) |
708daf42 | 1854 | return r; |
5ec9d065 FB |
1855 | } |
1856 | ||
a5e4f3ab | 1857 | return fd_set_perms(c, i, fd, path, st, creation); |
d4e9eb91 DR |
1858 | } |
1859 | ||
a5e4f3ab | 1860 | static int copy_files(Context *c, Item *i) { |
254d1313 | 1861 | _cleanup_close_ int dfd = -EBADF, fd = -EBADF; |
4609d3bb | 1862 | _cleanup_free_ char *bn = NULL; |
8f6fb95c | 1863 | struct stat st, a; |
b1f7b17f FB |
1864 | int r; |
1865 | ||
1866 | log_debug("Copying tree \"%s\" to \"%s\".", i->argument, i->path); | |
1867 | ||
4609d3bb LP |
1868 | r = path_extract_filename(i->path, &bn); |
1869 | if (r < 0) | |
1870 | return log_error_errno(r, "Failed to extract filename from path '%s': %m", i->path); | |
b1f7b17f | 1871 | |
4609d3bb LP |
1872 | /* Validate the path and use the returned directory fd for copying the target so we're sure that the |
1873 | * path can't be changed behind our back. */ | |
244c2a83 | 1874 | dfd = path_open_parent_safe(i->path, i->allow_failure); |
16ba55ad FB |
1875 | if (dfd < 0) |
1876 | return dfd; | |
b1f7b17f | 1877 | |
16ba55ad FB |
1878 | r = copy_tree_at(AT_FDCWD, i->argument, |
1879 | dfd, bn, | |
1880 | i->uid_set ? i->uid : UID_INVALID, | |
1881 | i->gid_set ? i->gid : GID_INVALID, | |
1fd5ec56 | 1882 | COPY_REFLINK | ((i->append_or_force) ? COPY_MERGE : COPY_MERGE_EMPTY) | COPY_MAC_CREATE | COPY_HARDLINKS, |
ad6fae7f | 1883 | NULL, NULL); |
b1f7b17f | 1884 | |
8f6fb95c LP |
1885 | fd = openat(dfd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH); |
1886 | if (fd < 0) { | |
1887 | if (r < 0) /* Look at original error first */ | |
b1f7b17f FB |
1888 | return log_error_errno(r, "Failed to copy files to %s: %m", i->path); |
1889 | ||
8f6fb95c LP |
1890 | return log_error_errno(errno, "Failed to openat(%s): %m", i->path); |
1891 | } | |
1892 | ||
1893 | if (fstat(fd, &st) < 0) | |
1894 | return log_error_errno(errno, "Failed to fstat(%s): %m", i->path); | |
b1f7b17f | 1895 | |
8f6fb95c LP |
1896 | if (stat(i->argument, &a) < 0) |
1897 | return log_error_errno(errno, "Failed to stat(%s): %m", i->argument); | |
b1f7b17f | 1898 | |
8f6fb95c LP |
1899 | if (((st.st_mode ^ a.st_mode) & S_IFMT) != 0) { |
1900 | log_debug("Can't copy to %s, file exists already and is of different type", i->path); | |
1901 | return 0; | |
b1f7b17f FB |
1902 | } |
1903 | ||
a5e4f3ab | 1904 | return fd_set_perms(c, i, fd, i->path, &st, _CREATION_MODE_INVALID); |
b1f7b17f FB |
1905 | } |
1906 | ||
a9bc518c LP |
1907 | static int create_directory_or_subvolume( |
1908 | const char *path, | |
1909 | mode_t mode, | |
1910 | bool subvol, | |
244c2a83 | 1911 | bool allow_failure, |
8f6fb95c | 1912 | struct stat *ret_st, |
a9bc518c | 1913 | CreationMode *ret_creation) { |
074bd73f | 1914 | |
4609d3bb | 1915 | _cleanup_free_ char *bn = NULL; |
254d1313 | 1916 | _cleanup_close_ int pfd = -EBADF; |
8f6fb95c LP |
1917 | CreationMode creation; |
1918 | struct stat st; | |
a9bc518c | 1919 | int r, fd; |
4ad36844 | 1920 | |
4c39d899 | 1921 | assert(path); |
4ad36844 | 1922 | |
4609d3bb LP |
1923 | r = path_extract_filename(path, &bn); |
1924 | if (r < 0) | |
1925 | return log_error_errno(r, "Failed to extract filename from path '%s': %m", path); | |
1926 | ||
244c2a83 | 1927 | pfd = path_open_parent_safe(path, allow_failure); |
4c39d899 FB |
1928 | if (pfd < 0) |
1929 | return pfd; | |
4ad36844 | 1930 | |
4c39d899 | 1931 | if (subvol) { |
4368c60c AV |
1932 | r = getenv_bool("SYSTEMD_TMPFILES_FORCE_SUBVOL"); |
1933 | if (r < 0) { | |
1934 | if (r != -ENXIO) /* env var is unset */ | |
1935 | log_warning_errno(r, "Cannot parse value of $SYSTEMD_TMPFILES_FORCE_SUBVOL, ignoring."); | |
1936 | r = btrfs_is_subvol(empty_to_root(arg_root)) > 0; | |
1937 | } | |
8f6fb95c | 1938 | if (r == 0) |
92631578 LP |
1939 | /* Don't create a subvolume unless the root directory is one, too. We do this under |
1940 | * the assumption that if the root directory is just a plain directory (i.e. very | |
1941 | * light-weight), we shouldn't try to split it up into subvolumes (i.e. more | |
1942 | * heavy-weight). Thus, chroot() environments and suchlike will get a full brtfs | |
1943 | * subvolume set up below their tree only if they specifically set up a btrfs | |
1944 | * subvolume for the root dir too. */ | |
4ad36844 | 1945 | |
4c39d899 | 1946 | subvol = false; |
4ad36844 | 1947 | else { |
2053593f | 1948 | WITH_UMASK((~mode) & 0777) |
e54c79cc | 1949 | r = btrfs_subvol_make(pfd, bn); |
4ad36844 FB |
1950 | } |
1951 | } else | |
1952 | r = 0; | |
1953 | ||
13d84288 | 1954 | if (!subvol || ERRNO_IS_NEG_NOT_SUPPORTED(r)) |
2053593f | 1955 | WITH_UMASK(0000) |
4609d3bb | 1956 | r = mkdirat_label(pfd, bn, mode); |
4ad36844 | 1957 | |
8f6fb95c | 1958 | creation = r >= 0 ? CREATION_NORMAL : CREATION_EXISTING; |
4ad36844 | 1959 | |
8f6fb95c LP |
1960 | fd = openat(pfd, bn, O_NOFOLLOW|O_CLOEXEC|O_DIRECTORY|O_PATH); |
1961 | if (fd < 0) { | |
1962 | /* We couldn't open it because it is not actually a directory? */ | |
1963 | if (errno == ENOTDIR) | |
1964 | return log_error_errno(SYNTHETIC_ERRNO(EEXIST), "\"%s\" already exists and is not a directory.", path); | |
1965 | ||
1966 | /* Then look at the original error */ | |
1967 | if (r < 0) | |
244c2a83 LB |
1968 | return log_full_errno(allow_failure ? LOG_INFO : LOG_ERR, |
1969 | r, | |
1970 | "Failed to create directory or subvolume \"%s\"%s: %m", | |
1971 | path, | |
1972 | allow_failure ? ", ignoring" : ""); | |
4ad36844 | 1973 | |
8f6fb95c LP |
1974 | return log_error_errno(errno, "Failed to open directory/subvolume we just created '%s': %m", path); |
1975 | } | |
4ad36844 | 1976 | |
8f6fb95c LP |
1977 | if (fstat(fd, &st) < 0) |
1978 | return log_error_errno(errno, "Failed to fstat(%s): %m", path); | |
4ad36844 | 1979 | |
8f6fb95c | 1980 | assert(S_ISDIR(st.st_mode)); /* we used O_DIRECTORY above */ |
4c39d899 | 1981 | |
8f6fb95c | 1982 | log_debug("%s directory \"%s\".", creation_mode_verb_to_string(creation), path); |
09f12798 | 1983 | |
8f6fb95c LP |
1984 | if (ret_st) |
1985 | *ret_st = st; | |
a9bc518c | 1986 | if (ret_creation) |
8f6fb95c | 1987 | *ret_creation = creation; |
a9bc518c LP |
1988 | |
1989 | return fd; | |
4c39d899 FB |
1990 | } |
1991 | ||
a5e4f3ab LP |
1992 | static int create_directory( |
1993 | Context *c, | |
1994 | Item *i, | |
1995 | const char *path) { | |
1996 | ||
254d1313 | 1997 | _cleanup_close_ int fd = -EBADF; |
a9bc518c | 1998 | CreationMode creation; |
8f6fb95c | 1999 | struct stat st; |
4c39d899 | 2000 | |
a5e4f3ab | 2001 | assert(c); |
4c39d899 FB |
2002 | assert(i); |
2003 | assert(IN_SET(i->type, CREATE_DIRECTORY, TRUNCATE_DIRECTORY)); | |
2004 | ||
244c2a83 | 2005 | fd = create_directory_or_subvolume(path, i->mode, /* subvol= */ false, i->allow_failure, &st, &creation); |
4c39d899 FB |
2006 | if (fd == -EEXIST) |
2007 | return 0; | |
2008 | if (fd < 0) | |
2009 | return fd; | |
2010 | ||
a5e4f3ab | 2011 | return fd_set_perms(c, i, fd, path, &st, creation); |
4c39d899 FB |
2012 | } |
2013 | ||
a5e4f3ab LP |
2014 | static int create_subvolume( |
2015 | Context *c, | |
2016 | Item *i, | |
2017 | const char *path) { | |
2018 | ||
254d1313 | 2019 | _cleanup_close_ int fd = -EBADF; |
f17a8d61 | 2020 | CreationMode creation; |
8f6fb95c | 2021 | struct stat st; |
4c39d899 FB |
2022 | int r, q = 0; |
2023 | ||
a5e4f3ab | 2024 | assert(c); |
4c39d899 FB |
2025 | assert(i); |
2026 | assert(IN_SET(i->type, CREATE_SUBVOLUME, CREATE_SUBVOLUME_NEW_QUOTA, CREATE_SUBVOLUME_INHERIT_QUOTA)); | |
2027 | ||
244c2a83 | 2028 | fd = create_directory_or_subvolume(path, i->mode, /* subvol = */ true, i->allow_failure, &st, &creation); |
4c39d899 FB |
2029 | if (fd == -EEXIST) |
2030 | return 0; | |
2031 | if (fd < 0) | |
2032 | return fd; | |
4ad36844 | 2033 | |
f17a8d61 FB |
2034 | if (creation == CREATION_NORMAL && |
2035 | IN_SET(i->type, CREATE_SUBVOLUME_NEW_QUOTA, CREATE_SUBVOLUME_INHERIT_QUOTA)) { | |
4c39d899 | 2036 | r = btrfs_subvol_auto_qgroup_fd(fd, 0, i->type == CREATE_SUBVOLUME_NEW_QUOTA); |
4ad36844 FB |
2037 | if (r == -ENOTTY) |
2038 | log_debug_errno(r, "Couldn't adjust quota for subvolume \"%s\" (unsupported fs or dir not a subvolume): %m", i->path); | |
2039 | else if (r == -EROFS) | |
2040 | log_debug_errno(r, "Couldn't adjust quota for subvolume \"%s\" (fs is read-only).", i->path); | |
4b019d2f | 2041 | else if (r == -ENOTCONN) |
4ad36844 FB |
2042 | log_debug_errno(r, "Couldn't adjust quota for subvolume \"%s\" (quota support is disabled).", i->path); |
2043 | else if (r < 0) | |
2044 | q = log_error_errno(r, "Failed to adjust quota for subvolume \"%s\": %m", i->path); | |
2045 | else if (r > 0) | |
2046 | log_debug("Adjusted quota for subvolume \"%s\".", i->path); | |
2047 | else if (r == 0) | |
2048 | log_debug("Quota for subvolume \"%s\" already in place, no change made.", i->path); | |
2049 | } | |
2050 | ||
a5e4f3ab | 2051 | r = fd_set_perms(c, i, fd, path, &st, creation); |
9d874aec | 2052 | if (q < 0) /* prefer the quota change error from above */ |
4ad36844 FB |
2053 | return q; |
2054 | ||
2055 | return r; | |
2056 | } | |
2057 | ||
a5e4f3ab LP |
2058 | static int empty_directory( |
2059 | Context *c, | |
2060 | Item *i, | |
2061 | const char *path, | |
2062 | CreationMode creation) { | |
2063 | ||
254d1313 | 2064 | _cleanup_close_ int fd = -EBADF; |
27f6aa0b | 2065 | struct stat st; |
54946021 FB |
2066 | int r; |
2067 | ||
a5e4f3ab | 2068 | assert(c); |
54946021 FB |
2069 | assert(i); |
2070 | assert(i->type == EMPTY_DIRECTORY); | |
2071 | ||
f461a28d | 2072 | r = chase(path, arg_root, CHASE_SAFE|CHASE_WARN, NULL, &fd); |
27f6aa0b | 2073 | if (r == -ENOLINK) /* Unsafe symlink: already covered by CHASE_WARN */ |
e56074a2 | 2074 | return r; |
54946021 | 2075 | if (r == -ENOENT) { |
27f6aa0b LP |
2076 | /* Option "e" operates only on existing objects. Do not print errors about non-existent files |
2077 | * or directories */ | |
2078 | log_debug_errno(r, "Skipping missing directory: %s", path); | |
54946021 FB |
2079 | return 0; |
2080 | } | |
2081 | if (r < 0) | |
27f6aa0b LP |
2082 | return log_error_errno(r, "Failed to open directory '%s': %m", path); |
2083 | ||
2084 | if (fstat(fd, &st) < 0) | |
2085 | return log_error_errno(errno, "Failed to fstat(%s): %m", path); | |
2086 | if (!S_ISDIR(st.st_mode)) { | |
2087 | log_warning("'%s' already exists and is not a directory.", path); | |
b88ba6c7 ZJS |
2088 | return 0; |
2089 | } | |
54946021 | 2090 | |
a5e4f3ab | 2091 | return fd_set_perms(c, i, fd, path, &st, creation); |
54946021 FB |
2092 | } |
2093 | ||
a5e4f3ab LP |
2094 | static int create_device( |
2095 | Context *c, | |
2096 | Item *i, | |
2097 | mode_t file_type) { | |
2098 | ||
254d1313 | 2099 | _cleanup_close_ int dfd = -EBADF, fd = -EBADF; |
4609d3bb | 2100 | _cleanup_free_ char *bn = NULL; |
074bd73f | 2101 | CreationMode creation; |
8f6fb95c | 2102 | struct stat st; |
074bd73f FB |
2103 | int r; |
2104 | ||
a5e4f3ab | 2105 | assert(c); |
074bd73f | 2106 | assert(i); |
a8ec0abe | 2107 | assert(IN_SET(i->type, CREATE_BLOCK_DEVICE, CREATE_CHAR_DEVICE)); |
074bd73f FB |
2108 | assert(IN_SET(file_type, S_IFBLK, S_IFCHR)); |
2109 | ||
4609d3bb LP |
2110 | r = path_extract_filename(i->path, &bn); |
2111 | if (r < 0) | |
2112 | return log_error_errno(r, "Failed to extract filename from path '%s': %m", i->path); | |
2113 | if (r == O_DIRECTORY) | |
2114 | return log_error_errno(SYNTHETIC_ERRNO(EISDIR), "Cannot open path '%s' for creating device node, is a directory.", i->path); | |
c7700a77 | 2115 | |
4609d3bb LP |
2116 | /* Validate the path and use the returned directory fd for copying the target so we're sure that the |
2117 | * path can't be changed behind our back. */ | |
244c2a83 | 2118 | dfd = path_open_parent_safe(i->path, i->allow_failure); |
c7700a77 FB |
2119 | if (dfd < 0) |
2120 | return dfd; | |
2121 | ||
2053593f | 2122 | WITH_UMASK(0000) { |
074bd73f | 2123 | mac_selinux_create_file_prepare(i->path, file_type); |
05c4affe | 2124 | r = RET_NERRNO(mknodat(dfd, bn, i->mode | file_type, i->major_minor)); |
074bd73f FB |
2125 | mac_selinux_create_file_clear(); |
2126 | } | |
8f6fb95c | 2127 | creation = r >= 0 ? CREATION_NORMAL : CREATION_EXISTING; |
074bd73f | 2128 | |
8f6fb95c LP |
2129 | /* Try to open the inode via O_PATH, regardless if we could create it or not. Maybe everything is in |
2130 | * order anyway and we hence can ignore the error to create the device node */ | |
2131 | fd = openat(dfd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH); | |
2132 | if (fd < 0) { | |
2133 | /* OK, so opening the inode failed, let's look at the original error then. */ | |
c7700a77 | 2134 | |
8f6fb95c LP |
2135 | if (r < 0) { |
2136 | if (ERRNO_IS_PRIVILEGE(r)) | |
2137 | goto handle_privilege; | |
074bd73f | 2138 | |
8f6fb95c LP |
2139 | return log_error_errno(r, "Failed to create device node '%s': %m", i->path); |
2140 | } | |
074bd73f | 2141 | |
8f6fb95c LP |
2142 | return log_error_errno(errno, "Failed to open device node '%s' we just created: %m", i->path); |
2143 | } | |
074bd73f | 2144 | |
8f6fb95c LP |
2145 | if (fstat(fd, &st) < 0) |
2146 | return log_error_errno(errno, "Failed to fstat(%s): %m", i->path); | |
074bd73f | 2147 | |
8f6fb95c | 2148 | if (((st.st_mode ^ file_type) & S_IFMT) != 0) { |
074bd73f | 2149 | |
8f6fb95c LP |
2150 | if (i->append_or_force) { |
2151 | fd = safe_close(fd); | |
074bd73f | 2152 | |
2053593f | 2153 | WITH_UMASK(0000) { |
8f6fb95c LP |
2154 | mac_selinux_create_file_prepare(i->path, file_type); |
2155 | r = mknodat_atomic(dfd, bn, i->mode | file_type, i->major_minor); | |
2156 | mac_selinux_create_file_clear(); | |
2157 | } | |
2158 | if (ERRNO_IS_PRIVILEGE(r)) | |
2159 | goto handle_privilege; | |
2160 | if (IN_SET(r, -EISDIR, -EEXIST, -ENOTEMPTY)) { | |
2161 | r = rm_rf_child(dfd, bn, REMOVE_PHYSICAL); | |
074bd73f | 2162 | if (r < 0) |
8f6fb95c LP |
2163 | return log_error_errno(r, "rm -rf %s failed: %m", i->path); |
2164 | ||
2165 | mac_selinux_create_file_prepare(i->path, file_type); | |
2166 | r = RET_NERRNO(mknodat(dfd, bn, i->mode | file_type, i->major_minor)); | |
2167 | mac_selinux_create_file_clear(); | |
074bd73f | 2168 | } |
8f6fb95c LP |
2169 | if (r < 0) |
2170 | return log_error_errno(r, "Failed to create device node '%s': %m", i->path); | |
2171 | ||
2172 | fd = openat(dfd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH); | |
2173 | if (fd < 0) | |
2174 | return log_error_errno(errno, "Failed to open device node we just created '%s': %m", i->path); | |
2175 | ||
2176 | /* Validate type before change ownership below */ | |
2177 | if (fstat(fd, &st) < 0) | |
2178 | return log_error_errno(errno, "Failed to fstat(%s): %m", i->path); | |
2179 | ||
2180 | if (((st.st_mode ^ file_type) & S_IFMT) != 0) | |
2181 | return log_error_errno(SYNTHETIC_ERRNO(EBADF), "Device node we just created is not a device node, refusing."); | |
2182 | ||
2183 | creation = CREATION_FORCE; | |
2184 | } else { | |
2185 | log_warning("\"%s\" already exists and is not a device node.", i->path); | |
2186 | return 0; | |
2187 | } | |
2188 | } | |
074bd73f FB |
2189 | |
2190 | log_debug("%s %s device node \"%s\" %u:%u.", | |
2191 | creation_mode_verb_to_string(creation), | |
2192 | i->type == CREATE_BLOCK_DEVICE ? "block" : "char", | |
2193 | i->path, major(i->mode), minor(i->mode)); | |
2194 | ||
a5e4f3ab | 2195 | return fd_set_perms(c, i, fd, i->path, &st, creation); |
c7700a77 | 2196 | |
8f6fb95c LP |
2197 | handle_privilege: |
2198 | log_debug_errno(r, | |
2199 | "We lack permissions, possibly because of cgroup configuration; " | |
2200 | "skipping creation of device node '%s'.", i->path); | |
2201 | return 0; | |
074bd73f FB |
2202 | } |
2203 | ||
a5e4f3ab | 2204 | static int create_fifo(Context *c, Item *i) { |
254d1313 | 2205 | _cleanup_close_ int pfd = -EBADF, fd = -EBADF; |
4609d3bb | 2206 | _cleanup_free_ char *bn = NULL; |
a2fc2f8d FB |
2207 | CreationMode creation; |
2208 | struct stat st; | |
2209 | int r; | |
2210 | ||
a5e4f3ab | 2211 | assert(c); |
8f6fb95c LP |
2212 | assert(i); |
2213 | assert(i->type == CREATE_FIFO); | |
2214 | ||
4609d3bb LP |
2215 | r = path_extract_filename(i->path, &bn); |
2216 | if (r < 0) | |
8f6fb95c | 2217 | return log_error_errno(r, "Failed to extract filename from path '%s': %m", i->path); |
4609d3bb | 2218 | if (r == O_DIRECTORY) |
8f6fb95c | 2219 | return log_error_errno(SYNTHETIC_ERRNO(EISDIR), "Cannot open path '%s' for creating FIFO, is a directory.", i->path); |
4609d3bb | 2220 | |
244c2a83 | 2221 | pfd = path_open_parent_safe(i->path, i->allow_failure); |
7ea5a87f FB |
2222 | if (pfd < 0) |
2223 | return pfd; | |
2224 | ||
2053593f | 2225 | WITH_UMASK(0000) { |
8f6fb95c | 2226 | mac_selinux_create_file_prepare(i->path, S_IFIFO); |
05c4affe | 2227 | r = RET_NERRNO(mkfifoat(pfd, bn, i->mode)); |
a2fc2f8d FB |
2228 | mac_selinux_create_file_clear(); |
2229 | } | |
2230 | ||
8f6fb95c | 2231 | creation = r >= 0 ? CREATION_NORMAL : CREATION_EXISTING; |
a2fc2f8d | 2232 | |
1751bdde | 2233 | /* Open the inode via O_PATH, regardless if we managed to create it or not. Maybe it is already the FIFO we want */ |
8f6fb95c LP |
2234 | fd = openat(pfd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH); |
2235 | if (fd < 0) { | |
2236 | if (r < 0) | |
2237 | return log_error_errno(r, "Failed to create FIFO %s: %m", i->path); /* original error! */ | |
a2fc2f8d | 2238 | |
8f6fb95c LP |
2239 | return log_error_errno(errno, "Failed to open FIFO we just created %s: %m", i->path); |
2240 | } | |
a2fc2f8d | 2241 | |
8f6fb95c LP |
2242 | if (fstat(fd, &st) < 0) |
2243 | return log_error_errno(errno, "Failed to fstat(%s): %m", i->path); | |
2244 | ||
2245 | if (!S_ISFIFO(st.st_mode)) { | |
a2fc2f8d | 2246 | |
8f6fb95c LP |
2247 | if (i->append_or_force) { |
2248 | fd = safe_close(fd); | |
2249 | ||
2053593f | 2250 | WITH_UMASK(0000) { |
8f6fb95c LP |
2251 | mac_selinux_create_file_prepare(i->path, S_IFIFO); |
2252 | r = mkfifoat_atomic(pfd, bn, i->mode); | |
2253 | mac_selinux_create_file_clear(); | |
2254 | } | |
2255 | if (IN_SET(r, -EISDIR, -EEXIST, -ENOTEMPTY)) { | |
2256 | r = rm_rf_child(pfd, bn, REMOVE_PHYSICAL); | |
a2fc2f8d | 2257 | if (r < 0) |
8f6fb95c LP |
2258 | return log_error_errno(r, "rm -rf %s failed: %m", i->path); |
2259 | ||
2260 | mac_selinux_create_file_prepare(i->path, S_IFIFO); | |
2261 | r = RET_NERRNO(mkfifoat(pfd, bn, i->mode)); | |
2262 | mac_selinux_create_file_clear(); | |
a2fc2f8d | 2263 | } |
8f6fb95c LP |
2264 | if (r < 0) |
2265 | return log_error_errno(r, "Failed to create FIFO %s: %m", i->path); | |
7ea5a87f | 2266 | |
8f6fb95c LP |
2267 | fd = openat(pfd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH); |
2268 | if (fd < 0) | |
2269 | return log_error_errno(errno, "Failed to open FIFO we just created '%s': %m", i->path); | |
a2fc2f8d | 2270 | |
8f6fb95c LP |
2271 | /* Validate type before change ownership below */ |
2272 | if (fstat(fd, &st) < 0) | |
2273 | return log_error_errno(errno, "Failed to fstat(%s): %m", i->path); | |
2274 | ||
2275 | if (!S_ISFIFO(st.st_mode)) | |
2276 | return log_error_errno(SYNTHETIC_ERRNO(EBADF), "FIFO inode we just created is not a FIFO, refusing."); | |
2277 | ||
2278 | creation = CREATION_FORCE; | |
2279 | } else { | |
2280 | log_warning("\"%s\" already exists and is not a FIFO.", i->path); | |
2281 | return 0; | |
2282 | } | |
2283 | } | |
2284 | ||
2285 | log_debug("%s fifo \"%s\".", creation_mode_verb_to_string(creation), i->path); | |
7ea5a87f | 2286 | |
a5e4f3ab | 2287 | return fd_set_perms(c, i, fd, i->path, &st, creation); |
a2fc2f8d FB |
2288 | } |
2289 | ||
a5e4f3ab | 2290 | static int create_symlink(Context *c, Item *i) { |
254d1313 | 2291 | _cleanup_close_ int pfd = -EBADF, fd = -EBADF; |
9e430ce3 LP |
2292 | _cleanup_free_ char *bn = NULL; |
2293 | CreationMode creation; | |
2294 | struct stat st; | |
2295 | bool good = false; | |
2296 | int r; | |
2297 | ||
a5e4f3ab | 2298 | assert(c); |
9e430ce3 LP |
2299 | assert(i); |
2300 | ||
2301 | r = path_extract_filename(i->path, &bn); | |
2302 | if (r < 0) | |
2303 | return log_error_errno(r, "Failed to extract filename from path '%s': %m", i->path); | |
2304 | if (r == O_DIRECTORY) | |
2305 | return log_error_errno(SYNTHETIC_ERRNO(EISDIR), "Cannot open path '%s' for creating FIFO, is a directory.", i->path); | |
2306 | ||
244c2a83 | 2307 | pfd = path_open_parent_safe(i->path, i->allow_failure); |
9e430ce3 LP |
2308 | if (pfd < 0) |
2309 | return pfd; | |
2310 | ||
2311 | mac_selinux_create_file_prepare(i->path, S_IFLNK); | |
2312 | r = RET_NERRNO(symlinkat(i->argument, pfd, bn)); | |
2313 | mac_selinux_create_file_clear(); | |
2314 | ||
2315 | creation = r >= 0 ? CREATION_NORMAL : CREATION_EXISTING; | |
2316 | ||
2317 | fd = openat(pfd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH); | |
2318 | if (fd < 0) { | |
2319 | if (r < 0) | |
2320 | return log_error_errno(r, "Failed to create symlink '%s': %m", i->path); /* original error! */ | |
2321 | ||
2322 | return log_error_errno(errno, "Failed to open symlink we just created '%s': %m", i->path); | |
2323 | } | |
2324 | ||
2325 | if (fstat(fd, &st) < 0) | |
2326 | return log_error_errno(errno, "Failed to fstat(%s): %m", i->path); | |
2327 | ||
2328 | if (S_ISLNK(st.st_mode)) { | |
2329 | _cleanup_free_ char *x = NULL; | |
2330 | ||
2331 | r = readlinkat_malloc(fd, "", &x); | |
2332 | if (r < 0) | |
2333 | return log_error_errno(r, "readlinkat(%s) failed: %m", i->path); | |
2334 | ||
2335 | good = streq(x, i->argument); | |
2336 | } else | |
2337 | good = false; | |
2338 | ||
2339 | if (!good) { | |
2340 | if (!i->append_or_force) { | |
2341 | log_debug("\"%s\" is not a symlink or does not point to the correct path.", i->path); | |
2342 | return 0; | |
2343 | } | |
2344 | ||
2345 | fd = safe_close(fd); | |
2346 | ||
2347 | mac_selinux_create_file_prepare(i->path, S_IFLNK); | |
2348 | r = symlinkat_atomic_full(i->argument, pfd, bn, /* make_relative= */ false); | |
2349 | mac_selinux_create_file_clear(); | |
2350 | if (IN_SET(r, -EISDIR, -EEXIST, -ENOTEMPTY)) { | |
2351 | r = rm_rf_child(pfd, bn, REMOVE_PHYSICAL); | |
2352 | if (r < 0) | |
2353 | return log_error_errno(r, "rm -rf %s failed: %m", i->path); | |
2354 | ||
2355 | mac_selinux_create_file_prepare(i->path, S_IFLNK); | |
2356 | r = RET_NERRNO(symlinkat(i->argument, pfd, i->path)); | |
2357 | mac_selinux_create_file_clear(); | |
2358 | } | |
2359 | if (r < 0) | |
2360 | return log_error_errno(r, "symlink(%s, %s) failed: %m", i->argument, i->path); | |
2361 | ||
2362 | fd = openat(pfd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH); | |
2363 | if (fd < 0) | |
2364 | return log_error_errno(errno, "Failed to open symlink we just created '%s': %m", i->path); | |
2365 | ||
2366 | /* Validate type before change ownership below */ | |
2367 | if (fstat(fd, &st) < 0) | |
2368 | return log_error_errno(errno, "Failed to fstat(%s): %m", i->path); | |
2369 | ||
2370 | if (!S_ISLNK(st.st_mode)) | |
2371 | return log_error_errno(SYNTHETIC_ERRNO(EBADF), "Symlink we just created is not a symlink, refusing."); | |
2372 | ||
2373 | creation = CREATION_FORCE; | |
2374 | } | |
2375 | ||
2376 | log_debug("%s symlink \"%s\".", creation_mode_verb_to_string(creation), i->path); | |
a5e4f3ab | 2377 | return fd_set_perms(c, i, fd, i->path, &st, creation); |
9e430ce3 LP |
2378 | } |
2379 | ||
a5e4f3ab LP |
2380 | typedef int (*action_t)(Context *c, Item *i, const char *path, CreationMode creation); |
2381 | typedef int (*fdaction_t)(Context *c, Item *i, int fd, const char *path, const struct stat *st, CreationMode creation); | |
a9bc518c LP |
2382 | |
2383 | static int item_do( | |
a5e4f3ab | 2384 | Context *c, |
a9bc518c LP |
2385 | Item *i, |
2386 | int fd, | |
2387 | const char *path, | |
2388 | CreationMode creation, | |
2389 | fdaction_t action) { | |
081043cf | 2390 | |
14f3480a | 2391 | struct stat st; |
936f6bdb | 2392 | int r = 0, q; |
e73a03e0 | 2393 | |
a5e4f3ab | 2394 | assert(c); |
e73a03e0 | 2395 | assert(i); |
4dc7bfdf | 2396 | assert(path); |
936f6bdb | 2397 | assert(fd >= 0); |
14f3480a FB |
2398 | |
2399 | if (fstat(fd, &st) < 0) { | |
09f12798 | 2400 | r = log_error_errno(errno, "fstat() on file failed: %m"); |
14f3480a FB |
2401 | goto finish; |
2402 | } | |
a8d88783 | 2403 | |
a9bc518c | 2404 | /* This returns the first error we run into, but nevertheless tries to go on */ |
a5e4f3ab | 2405 | r = action(c, i, fd, path, &st, creation); |
a8d88783 | 2406 | |
14f3480a | 2407 | if (S_ISDIR(st.st_mode)) { |
936f6bdb | 2408 | _cleanup_closedir_ DIR *d = NULL; |
a8d88783 | 2409 | |
ddb6eeaf LP |
2410 | /* The passed 'fd' was opened with O_PATH. We need to convert it into a 'regular' fd before |
2411 | * reading the directory content. */ | |
2412 | d = opendir(FORMAT_PROC_FD_PATH(fd)); | |
936f6bdb | 2413 | if (!d) { |
ddb6eeaf | 2414 | log_error_errno(errno, "Failed to opendir() '%s': %m", FORMAT_PROC_FD_PATH(fd)); |
09f12798 LP |
2415 | if (r == 0) |
2416 | r = -errno; | |
936f6bdb FB |
2417 | goto finish; |
2418 | } | |
a8d88783 | 2419 | |
936f6bdb | 2420 | FOREACH_DIRENT_ALL(de, d, q = -errno; goto finish) { |
936f6bdb | 2421 | int de_fd; |
a8d88783 | 2422 | |
936f6bdb FB |
2423 | if (dot_or_dot_dot(de->d_name)) |
2424 | continue; | |
2425 | ||
2426 | de_fd = openat(fd, de->d_name, O_NOFOLLOW|O_CLOEXEC|O_PATH); | |
9d874aec LP |
2427 | if (de_fd < 0) |
2428 | q = log_error_errno(errno, "Failed to open() file '%s': %m", de->d_name); | |
4dc7bfdf FB |
2429 | else { |
2430 | _cleanup_free_ char *de_path = NULL; | |
2431 | ||
62a85ee0 | 2432 | de_path = path_join(path, de->d_name); |
4dc7bfdf FB |
2433 | if (!de_path) |
2434 | q = log_oom(); | |
2435 | else | |
2436 | /* Pass ownership of dirent fd over */ | |
a5e4f3ab | 2437 | q = item_do(c, i, de_fd, de_path, CREATION_EXISTING, action); |
4dc7bfdf | 2438 | } |
a8d88783 | 2439 | |
e73a03e0 LP |
2440 | if (q < 0 && r == 0) |
2441 | r = q; | |
a8d88783 | 2442 | } |
a8d88783 | 2443 | } |
936f6bdb FB |
2444 | finish: |
2445 | safe_close(fd); | |
e73a03e0 | 2446 | return r; |
a8d88783 MS |
2447 | } |
2448 | ||
a5e4f3ab | 2449 | static int glob_item(Context *c, Item *i, action_t action) { |
df99a9ef | 2450 | _cleanup_globfree_ glob_t g = { |
ebf31a1f | 2451 | .gl_opendir = (void *(*)(const char *)) opendir_nomod, |
df99a9ef | 2452 | }; |
e73a03e0 | 2453 | int r = 0, k; |
99e68c0b | 2454 | |
a5e4f3ab LP |
2455 | assert(c); |
2456 | assert(i); | |
2457 | ||
84e72b5e ZJS |
2458 | k = safe_glob(i->path, GLOB_NOSORT|GLOB_BRACE, &g); |
2459 | if (k < 0 && k != -ENOENT) | |
2460 | return log_error_errno(k, "glob(%s) failed: %m", i->path); | |
99e68c0b | 2461 | |
c84a9488 | 2462 | STRV_FOREACH(fn, g.gl_pathv) { |
a9bc518c | 2463 | /* We pass CREATION_EXISTING here, since if we are globbing for it, it always has to exist */ |
a5e4f3ab | 2464 | k = action(c, i, *fn, CREATION_EXISTING); |
e73a03e0 | 2465 | if (k < 0 && r == 0) |
99e68c0b | 2466 | r = k; |
936f6bdb FB |
2467 | } |
2468 | ||
2469 | return r; | |
2470 | } | |
081043cf | 2471 | |
a5e4f3ab LP |
2472 | static int glob_item_recursively( |
2473 | Context *c, | |
2474 | Item *i, | |
2475 | fdaction_t action) { | |
2476 | ||
936f6bdb FB |
2477 | _cleanup_globfree_ glob_t g = { |
2478 | .gl_opendir = (void *(*)(const char *)) opendir_nomod, | |
2479 | }; | |
2480 | int r = 0, k; | |
936f6bdb FB |
2481 | |
2482 | k = safe_glob(i->path, GLOB_NOSORT|GLOB_BRACE, &g); | |
2483 | if (k < 0 && k != -ENOENT) | |
2484 | return log_error_errno(k, "glob(%s) failed: %m", i->path); | |
2485 | ||
2486 | STRV_FOREACH(fn, g.gl_pathv) { | |
254d1313 | 2487 | _cleanup_close_ int fd = -EBADF; |
936f6bdb FB |
2488 | |
2489 | /* Make sure we won't trigger/follow file object (such as | |
2490 | * device nodes, automounts, ...) pointed out by 'fn' with | |
2491 | * O_PATH. Note, when O_PATH is used, flags other than | |
2492 | * O_CLOEXEC, O_DIRECTORY, and O_NOFOLLOW are ignored. */ | |
2493 | ||
2494 | fd = open(*fn, O_CLOEXEC|O_NOFOLLOW|O_PATH); | |
2495 | if (fd < 0) { | |
09f12798 LP |
2496 | log_error_errno(errno, "Opening '%s' failed: %m", *fn); |
2497 | if (r == 0) | |
2498 | r = -errno; | |
936f6bdb FB |
2499 | continue; |
2500 | } | |
2501 | ||
a5e4f3ab | 2502 | k = item_do(c, i, fd, *fn, CREATION_EXISTING, action); |
936f6bdb FB |
2503 | if (k < 0 && r == 0) |
2504 | r = k; | |
2505 | ||
2506 | /* we passed fd ownership to the previous call */ | |
254d1313 | 2507 | fd = -EBADF; |
c84a9488 | 2508 | } |
99e68c0b | 2509 | |
99e68c0b MS |
2510 | return r; |
2511 | } | |
2512 | ||
c46c3233 AW |
2513 | static int rm_if_wrong_type_safe( |
2514 | mode_t mode, | |
2515 | int parent_fd, | |
21e43a7c | 2516 | const struct stat *parent_st, /* Only used if follow_links below is true. */ |
c46c3233 AW |
2517 | const char *name, |
2518 | int flags) { | |
2519 | _cleanup_free_ char *parent_name = NULL; | |
2520 | bool follow_links = !FLAGS_SET(flags, AT_SYMLINK_NOFOLLOW); | |
2521 | struct stat st; | |
2522 | int r; | |
2523 | ||
2524 | assert(name); | |
2525 | assert((mode & ~S_IFMT) == 0); | |
2526 | assert(!follow_links || parent_st); | |
2527 | assert((flags & ~AT_SYMLINK_NOFOLLOW) == 0); | |
2528 | ||
2529 | if (!filename_is_valid(name)) | |
2530 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "\"%s\" is not a valid filename.", name); | |
2531 | ||
2532 | r = fstatat_harder(parent_fd, name, &st, flags, REMOVE_CHMOD | REMOVE_CHMOD_RESTORE); | |
2533 | if (r < 0) { | |
2534 | (void) fd_get_path(parent_fd, &parent_name); | |
2535 | return log_full_errno(r == -ENOENT? LOG_DEBUG : LOG_ERR, r, | |
2536 | "Failed to stat \"%s\" at \"%s\": %m", name, strna(parent_name)); | |
2537 | } | |
2538 | ||
2539 | /* Fail before removing anything if this is an unsafe transition. */ | |
2540 | if (follow_links && unsafe_transition(parent_st, &st)) { | |
2541 | (void) fd_get_path(parent_fd, &parent_name); | |
2542 | return log_error_errno(SYNTHETIC_ERRNO(ENOLINK), | |
2543 | "Unsafe transition from \"%s\" to \"%s\".", parent_name, name); | |
2544 | } | |
2545 | ||
2546 | if ((st.st_mode & S_IFMT) == mode) | |
2547 | return 0; | |
2548 | ||
2549 | (void) fd_get_path(parent_fd, &parent_name); | |
056ff0b4 | 2550 | log_notice("Wrong file type 0o%o; rm -rf \"%s/%s\"", st.st_mode & S_IFMT, strna(parent_name), name); |
c46c3233 AW |
2551 | |
2552 | /* If the target of the symlink was the wrong type, the link needs to be removed instead of the | |
2553 | * target, so make sure it is identified as a link and not a directory. */ | |
2554 | if (follow_links) { | |
2555 | r = fstatat_harder(parent_fd, name, &st, AT_SYMLINK_NOFOLLOW, REMOVE_CHMOD | REMOVE_CHMOD_RESTORE); | |
2556 | if (r < 0) | |
2557 | return log_error_errno(r, "Failed to stat \"%s\" at \"%s\": %m", name, strna(parent_name)); | |
2558 | } | |
2559 | ||
2560 | /* Do not remove mount points. */ | |
2561 | r = fd_is_mount_point(parent_fd, name, follow_links ? AT_SYMLINK_FOLLOW : 0); | |
2562 | if (r < 0) | |
2563 | (void) log_warning_errno(r, "Failed to check if \"%s/%s\" is a mount point: %m; Continuing", | |
2564 | strna(parent_name), name); | |
2565 | else if (r > 0) | |
2566 | return log_error_errno(SYNTHETIC_ERRNO(EBUSY), | |
2567 | "Not removing \"%s/%s\" because it is a mount point.", strna(parent_name), name); | |
2568 | ||
2569 | if ((st.st_mode & S_IFMT) == S_IFDIR) { | |
254d1313 | 2570 | _cleanup_close_ int child_fd = -EBADF; |
c46c3233 AW |
2571 | |
2572 | child_fd = openat(parent_fd, name, O_NOCTTY | O_CLOEXEC | O_DIRECTORY); | |
2573 | if (child_fd < 0) | |
2574 | return log_error_errno(errno, "Failed to open \"%s\" at \"%s\": %m", name, strna(parent_name)); | |
2575 | ||
2576 | r = rm_rf_children(TAKE_FD(child_fd), REMOVE_ROOT|REMOVE_SUBVOLUME|REMOVE_PHYSICAL, &st); | |
2577 | if (r < 0) | |
2578 | return log_error_errno(r, "Failed to remove contents of \"%s\" at \"%s\": %m", name, strna(parent_name)); | |
2579 | ||
2580 | r = unlinkat_harder(parent_fd, name, AT_REMOVEDIR, REMOVE_CHMOD | REMOVE_CHMOD_RESTORE); | |
2581 | } else | |
2582 | r = unlinkat_harder(parent_fd, name, 0, REMOVE_CHMOD | REMOVE_CHMOD_RESTORE); | |
2583 | if (r < 0) | |
2584 | return log_error_errno(r, "Failed to remove \"%s\" at \"%s\": %m", name, strna(parent_name)); | |
2585 | ||
2586 | /* This is covered by the log_notice "Wrong file type..." It is logged earlier because it gives | |
2587 | * context to other error messages that might follow. */ | |
2588 | return -ENOENT; | |
2589 | } | |
2590 | ||
2591 | /* If child_mode is non-zero, rm_if_wrong_type_safe will be executed for the last path component. */ | |
2592 | static int mkdir_parents_rm_if_wrong_type(mode_t child_mode, const char *path) { | |
254d1313 | 2593 | _cleanup_close_ int parent_fd = -EBADF; |
c46c3233 | 2594 | struct stat parent_st; |
c46c3233 | 2595 | size_t path_len; |
21e43a7c | 2596 | int r; |
c46c3233 AW |
2597 | |
2598 | assert(path); | |
2599 | assert((child_mode & ~S_IFMT) == 0); | |
2600 | ||
2601 | path_len = strlen(path); | |
2602 | ||
2603 | if (!is_path(path)) | |
2604 | /* rm_if_wrong_type_safe already logs errors. */ | |
2605 | return child_mode != 0 ? rm_if_wrong_type_safe(child_mode, AT_FDCWD, NULL, path, AT_SYMLINK_NOFOLLOW) : 0; | |
2606 | ||
2607 | if (child_mode != 0 && endswith(path, "/")) | |
2608 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
2609 | "Trailing path separators are only allowed if child_mode is not set; got \"%s\"", path); | |
2610 | ||
2611 | /* Get the parent_fd and stat. */ | |
c68cafba YW |
2612 | parent_fd = openat(AT_FDCWD, path_is_absolute(path) ? "/" : ".", O_NOCTTY | O_CLOEXEC | O_DIRECTORY); |
2613 | if (parent_fd < 0) | |
2614 | return log_error_errno(errno, "Failed to open root: %m"); | |
2615 | ||
c46c3233 AW |
2616 | if (fstat(parent_fd, &parent_st) < 0) |
2617 | return log_error_errno(errno, "Failed to stat root: %m"); | |
2618 | ||
2619 | /* Check every parent directory in the path, except the last component */ | |
21e43a7c | 2620 | for (const char *e = path;;) { |
254d1313 | 2621 | _cleanup_close_ int next_fd = -EBADF; |
c46c3233 | 2622 | char t[path_len + 1]; |
21e43a7c | 2623 | const char *s; |
c46c3233 AW |
2624 | |
2625 | /* Find the start of the next path component. */ | |
2626 | s = e + strspn(e, "/"); | |
2627 | /* Find the end of the next path component. */ | |
2628 | e = s + strcspn(s, "/"); | |
2629 | ||
2630 | /* Copy the path component to t so it can be a null terminated string. */ | |
2631 | *((char*) mempcpy(t, s, e - s)) = 0; | |
2632 | ||
2633 | /* Is this the last component? If so, then check the type */ | |
2634 | if (*e == 0) | |
2635 | return child_mode != 0 ? rm_if_wrong_type_safe(child_mode, parent_fd, &parent_st, t, AT_SYMLINK_NOFOLLOW) : 0; | |
c46c3233 | 2636 | |
21e43a7c YW |
2637 | r = rm_if_wrong_type_safe(S_IFDIR, parent_fd, &parent_st, t, 0); |
2638 | /* Remove dangling symlinks. */ | |
2639 | if (r == -ENOENT) | |
2640 | r = rm_if_wrong_type_safe(S_IFDIR, parent_fd, &parent_st, t, AT_SYMLINK_NOFOLLOW); | |
c46c3233 | 2641 | if (r == -ENOENT) { |
2053593f | 2642 | WITH_UMASK(0000) |
c46c3233 AW |
2643 | r = mkdirat_label(parent_fd, t, 0755); |
2644 | if (r < 0) { | |
21e43a7c YW |
2645 | _cleanup_free_ char *parent_name = NULL; |
2646 | ||
c46c3233 | 2647 | (void) fd_get_path(parent_fd, &parent_name); |
21e43a7c | 2648 | return log_error_errno(r, "Failed to mkdir \"%s\" at \"%s\": %m", t, strnull(parent_name)); |
c46c3233 AW |
2649 | } |
2650 | } else if (r < 0) | |
2651 | /* rm_if_wrong_type_safe already logs errors. */ | |
2652 | return r; | |
2653 | ||
7c248223 | 2654 | next_fd = RET_NERRNO(openat(parent_fd, t, O_NOCTTY | O_CLOEXEC | O_DIRECTORY)); |
c46c3233 | 2655 | if (next_fd < 0) { |
21e43a7c YW |
2656 | _cleanup_free_ char *parent_name = NULL; |
2657 | ||
c46c3233 | 2658 | (void) fd_get_path(parent_fd, &parent_name); |
7c248223 | 2659 | return log_error_errno(next_fd, "Failed to open \"%s\" at \"%s\": %m", t, strnull(parent_name)); |
c46c3233 | 2660 | } |
7c248223 LP |
2661 | r = RET_NERRNO(fstat(next_fd, &parent_st)); |
2662 | if (r < 0) { | |
21e43a7c YW |
2663 | _cleanup_free_ char *parent_name = NULL; |
2664 | ||
c46c3233 | 2665 | (void) fd_get_path(parent_fd, &parent_name); |
21e43a7c | 2666 | return log_error_errno(r, "Failed to stat \"%s\" at \"%s\": %m", t, strnull(parent_name)); |
c46c3233 AW |
2667 | } |
2668 | ||
ee3455cf | 2669 | close_and_replace(parent_fd, next_fd); |
c46c3233 AW |
2670 | } |
2671 | } | |
2672 | ||
2673 | static int mkdir_parents_item(Item *i, mode_t child_mode) { | |
2674 | int r; | |
2675 | if (i->try_replace) { | |
2676 | r = mkdir_parents_rm_if_wrong_type(child_mode, i->path); | |
2677 | if (r < 0 && r != -ENOENT) | |
2678 | return r; | |
2679 | } else | |
2053593f | 2680 | WITH_UMASK(0000) |
c46c3233 AW |
2681 | (void) mkdir_parents_label(i->path, 0755); |
2682 | ||
2683 | return 0; | |
2684 | } | |
2685 | ||
a5e4f3ab | 2686 | static int create_item(Context *c, Item *i) { |
9e430ce3 | 2687 | int r; |
5008d581 | 2688 | |
a5e4f3ab | 2689 | assert(c); |
3b63d2d3 | 2690 | assert(i); |
5008d581 | 2691 | |
582deb84 ZJS |
2692 | log_debug("Running create action for entry %c %s", (char) i->type, i->path); |
2693 | ||
3b63d2d3 LP |
2694 | switch (i->type) { |
2695 | ||
2696 | case IGNORE_PATH: | |
78a92a5a | 2697 | case IGNORE_DIRECTORY_PATH: |
3b63d2d3 LP |
2698 | case REMOVE_PATH: |
2699 | case RECURSIVE_REMOVE_PATH: | |
2700 | return 0; | |
5008d581 | 2701 | |
eccebf4b | 2702 | case TRUNCATE_FILE: |
3b63d2d3 | 2703 | case CREATE_FILE: |
c46c3233 AW |
2704 | r = mkdir_parents_item(i, S_IFREG); |
2705 | if (r < 0) | |
2706 | return r; | |
7fa10748 | 2707 | |
eccebf4b | 2708 | if ((i->type == CREATE_FILE && i->append_or_force) || i->type == TRUNCATE_FILE) |
a5e4f3ab | 2709 | r = truncate_file(c, i, i->path); |
eccebf4b | 2710 | else |
a5e4f3ab | 2711 | r = create_file(c, i, i->path); |
5ec9d065 FB |
2712 | if (r < 0) |
2713 | return r; | |
2714 | break; | |
2715 | ||
7b887f22 | 2716 | case COPY_FILES: |
c46c3233 AW |
2717 | r = mkdir_parents_item(i, 0); |
2718 | if (r < 0) | |
2719 | return r; | |
7fa10748 | 2720 | |
a5e4f3ab | 2721 | r = copy_files(c, i); |
849958d1 LP |
2722 | if (r < 0) |
2723 | return r; | |
849958d1 LP |
2724 | break; |
2725 | ||
d4e9eb91 | 2726 | case WRITE_FILE: |
a5e4f3ab | 2727 | r = glob_item(c, i, write_one_file); |
f05bc3f7 MS |
2728 | if (r < 0) |
2729 | return r; | |
5008d581 | 2730 | |
3b63d2d3 LP |
2731 | break; |
2732 | ||
3b63d2d3 | 2733 | case CREATE_DIRECTORY: |
d7b8eec7 | 2734 | case TRUNCATE_DIRECTORY: |
c46c3233 AW |
2735 | r = mkdir_parents_item(i, S_IFDIR); |
2736 | if (r < 0) | |
2737 | return r; | |
4c39d899 | 2738 | |
a5e4f3ab | 2739 | r = create_directory(c, i, i->path); |
4c39d899 FB |
2740 | if (r < 0) |
2741 | return r; | |
2742 | break; | |
2743 | ||
d7b8eec7 | 2744 | case CREATE_SUBVOLUME: |
5fb13eb5 LP |
2745 | case CREATE_SUBVOLUME_INHERIT_QUOTA: |
2746 | case CREATE_SUBVOLUME_NEW_QUOTA: | |
c46c3233 AW |
2747 | r = mkdir_parents_item(i, S_IFDIR); |
2748 | if (r < 0) | |
2749 | return r; | |
d7b8eec7 | 2750 | |
a5e4f3ab | 2751 | r = create_subvolume(c, i, i->path); |
4ad36844 FB |
2752 | if (r < 0) |
2753 | return r; | |
2754 | break; | |
5fb13eb5 | 2755 | |
df8dee85 | 2756 | case EMPTY_DIRECTORY: |
a5e4f3ab | 2757 | r = glob_item(c, i, empty_directory); |
f05bc3f7 MS |
2758 | if (r < 0) |
2759 | return r; | |
3b63d2d3 | 2760 | break; |
ee17ee7c LP |
2761 | |
2762 | case CREATE_FIFO: | |
c46c3233 AW |
2763 | r = mkdir_parents_item(i, S_IFIFO); |
2764 | if (r < 0) | |
2765 | return r; | |
7fa10748 | 2766 | |
a5e4f3ab | 2767 | r = create_fifo(c, i); |
f05bc3f7 MS |
2768 | if (r < 0) |
2769 | return r; | |
ee17ee7c | 2770 | break; |
a8d88783 | 2771 | |
9e430ce3 | 2772 | case CREATE_SYMLINK: |
c46c3233 AW |
2773 | r = mkdir_parents_item(i, S_IFLNK); |
2774 | if (r < 0) | |
2775 | return r; | |
7fa10748 | 2776 | |
a5e4f3ab | 2777 | r = create_symlink(c, i); |
9e430ce3 LP |
2778 | if (r < 0) |
2779 | return r; | |
a542c4dc | 2780 | |
468d726b | 2781 | break; |
468d726b LP |
2782 | |
2783 | case CREATE_BLOCK_DEVICE: | |
074bd73f | 2784 | case CREATE_CHAR_DEVICE: |
26c45a6c | 2785 | if (have_effective_cap(CAP_MKNOD) <= 0) { |
713998cf LP |
2786 | /* In a container we lack CAP_MKNOD. We shouldn't attempt to create the device node in that |
2787 | * case to avoid noise, and we don't support virtualized devices in containers anyway. */ | |
cb7ed9df LP |
2788 | |
2789 | log_debug("We lack CAP_MKNOD, skipping creation of device node %s.", i->path); | |
2790 | return 0; | |
2791 | } | |
2792 | ||
c46c3233 AW |
2793 | r = mkdir_parents_item(i, i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR); |
2794 | if (r < 0) | |
2795 | return r; | |
7fa10748 | 2796 | |
a5e4f3ab | 2797 | r = create_device(c, i, i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR); |
468d726b LP |
2798 | if (r < 0) |
2799 | return r; | |
2800 | ||
2801 | break; | |
468d726b | 2802 | |
e73a03e0 | 2803 | case ADJUST_MODE: |
777b87e7 | 2804 | case RELABEL_PATH: |
a5e4f3ab | 2805 | r = glob_item(c, i, path_set_perms); |
777b87e7 | 2806 | if (r < 0) |
96ca8194 | 2807 | return r; |
777b87e7 MS |
2808 | break; |
2809 | ||
a8d88783 | 2810 | case RECURSIVE_RELABEL_PATH: |
a5e4f3ab | 2811 | r = glob_item_recursively(c, i, fd_set_perms); |
a8d88783 MS |
2812 | if (r < 0) |
2813 | return r; | |
ebf4e801 | 2814 | break; |
e73a03e0 | 2815 | |
ebf4e801 | 2816 | case SET_XATTR: |
a5e4f3ab | 2817 | r = glob_item(c, i, path_set_xattrs); |
b705ab6a ZJS |
2818 | if (r < 0) |
2819 | return r; | |
2820 | break; | |
2821 | ||
2822 | case RECURSIVE_SET_XATTR: | |
a5e4f3ab | 2823 | r = glob_item_recursively(c, i, fd_set_xattrs); |
ebf4e801 MW |
2824 | if (r < 0) |
2825 | return r; | |
e73a03e0 | 2826 | break; |
f8eeeaf9 ZJS |
2827 | |
2828 | case SET_ACL: | |
a5e4f3ab | 2829 | r = glob_item(c, i, path_set_acls); |
f8eeeaf9 ZJS |
2830 | if (r < 0) |
2831 | return r; | |
b705ab6a ZJS |
2832 | break; |
2833 | ||
2834 | case RECURSIVE_SET_ACL: | |
a5e4f3ab | 2835 | r = glob_item_recursively(c, i, fd_set_acls); |
b705ab6a ZJS |
2836 | if (r < 0) |
2837 | return r; | |
2838 | break; | |
22c3a6ca | 2839 | |
88ec4dfa | 2840 | case SET_ATTRIBUTE: |
a5e4f3ab | 2841 | r = glob_item(c, i, path_set_attribute); |
22c3a6ca GB |
2842 | if (r < 0) |
2843 | return r; | |
2844 | break; | |
2845 | ||
88ec4dfa | 2846 | case RECURSIVE_SET_ATTRIBUTE: |
a5e4f3ab | 2847 | r = glob_item_recursively(c, i, fd_set_attribute); |
22c3a6ca GB |
2848 | if (r < 0) |
2849 | return r; | |
2850 | break; | |
3b63d2d3 LP |
2851 | } |
2852 | ||
f05bc3f7 | 2853 | return 0; |
3b63d2d3 LP |
2854 | } |
2855 | ||
81a18380 LB |
2856 | static int purge_item_instance(Context *c, Item *i, const char *instance, CreationMode creation) { |
2857 | int r; | |
2858 | ||
2859 | /* FIXME: we probably should use dir_cleanup() here instead of rm_rf() so that 'x' is honoured. */ | |
2860 | log_debug("rm -rf \"%s\"", instance); | |
2861 | r = rm_rf(instance, REMOVE_ROOT|REMOVE_SUBVOLUME|REMOVE_PHYSICAL); | |
2862 | if (r < 0 && r != -ENOENT) | |
2863 | return log_error_errno(r, "rm_rf(%s): %m", instance); | |
2864 | ||
2865 | return 0; | |
2866 | } | |
2867 | ||
2868 | static int purge_item(Context *c, Item *i) { | |
2869 | ||
2870 | assert(i); | |
2871 | ||
2872 | if (!needs_purge(i->type)) | |
2873 | return 0; | |
2874 | ||
2875 | log_debug("Running purge owned action for entry %c %s", (char) i->type, i->path); | |
2876 | ||
2877 | if (needs_glob(i->type)) | |
2878 | return glob_item(c, i, purge_item_instance); | |
2879 | ||
2880 | return purge_item_instance(c, i, i->path, CREATION_EXISTING); | |
2881 | } | |
2882 | ||
a5e4f3ab LP |
2883 | static int remove_item_instance( |
2884 | Context *c, | |
2885 | Item *i, | |
2886 | const char *instance, | |
2887 | CreationMode creation) { | |
2888 | ||
3b63d2d3 LP |
2889 | int r; |
2890 | ||
a5e4f3ab | 2891 | assert(c); |
3b63d2d3 LP |
2892 | assert(i); |
2893 | ||
2894 | switch (i->type) { | |
2895 | ||
3b63d2d3 | 2896 | case REMOVE_PATH: |
4a62c710 | 2897 | if (remove(instance) < 0 && errno != ENOENT) |
3f93da98 | 2898 | return log_error_errno(errno, "rm(%s): %m", instance); |
3b63d2d3 LP |
2899 | |
2900 | break; | |
2901 | ||
3b63d2d3 | 2902 | case RECURSIVE_REMOVE_PATH: |
2c575977 | 2903 | /* FIXME: we probably should use dir_cleanup() here instead of rm_rf() so that 'x' is honoured. */ |
582deb84 | 2904 | log_debug("rm -rf \"%s\"", instance); |
2c575977 | 2905 | r = rm_rf(instance, REMOVE_ROOT|REMOVE_SUBVOLUME|REMOVE_PHYSICAL); |
f647962d MS |
2906 | if (r < 0 && r != -ENOENT) |
2907 | return log_error_errno(r, "rm_rf(%s): %m", instance); | |
3b63d2d3 LP |
2908 | |
2909 | break; | |
7fcb4b9b ZJS |
2910 | |
2911 | default: | |
04499a70 | 2912 | assert_not_reached(); |
3b63d2d3 LP |
2913 | } |
2914 | ||
2915 | return 0; | |
2916 | } | |
2917 | ||
a5e4f3ab | 2918 | static int remove_item(Context *c, Item *i) { |
2c575977 LP |
2919 | int r; |
2920 | ||
a5e4f3ab | 2921 | assert(c); |
b8bb3e8f LP |
2922 | assert(i); |
2923 | ||
582deb84 ZJS |
2924 | log_debug("Running remove action for entry %c %s", (char) i->type, i->path); |
2925 | ||
b8bb3e8f LP |
2926 | switch (i->type) { |
2927 | ||
b8bb3e8f | 2928 | case TRUNCATE_DIRECTORY: |
2c575977 LP |
2929 | /* FIXME: we probably should use dir_cleanup() here instead of rm_rf() so that 'x' is honoured. */ |
2930 | log_debug("rm -rf \"%s\"", i->path); | |
2931 | r = rm_rf(i->path, REMOVE_PHYSICAL); | |
2932 | if (r < 0 && r != -ENOENT) | |
2933 | return log_error_errno(r, "rm_rf(%s): %m", i->path); | |
2934 | ||
2935 | return 0; | |
2936 | ||
2937 | case REMOVE_PATH: | |
99e68c0b | 2938 | case RECURSIVE_REMOVE_PATH: |
a5e4f3ab | 2939 | return glob_item(c, i, remove_item_instance); |
b8bb3e8f | 2940 | |
df8dee85 ZJS |
2941 | default: |
2942 | return 0; | |
2943 | } | |
b8bb3e8f LP |
2944 | } |
2945 | ||
7f7a50dd SK |
2946 | static char *age_by_to_string(AgeBy ab, bool is_dir) { |
2947 | static const char ab_map[] = { 'a', 'b', 'c', 'm' }; | |
2948 | size_t j = 0; | |
2949 | char *ret; | |
2950 | ||
2951 | ret = new(char, ELEMENTSOF(ab_map) + 1); | |
2952 | if (!ret) | |
2953 | return NULL; | |
2954 | ||
2955 | for (size_t i = 0; i < ELEMENTSOF(ab_map); i++) | |
2956 | if (FLAGS_SET(ab, 1U << i)) | |
2957 | ret[j++] = is_dir ? ascii_toupper(ab_map[i]) : ab_map[i]; | |
2958 | ||
2959 | ret[j] = 0; | |
2960 | return ret; | |
2961 | } | |
2962 | ||
a9bc518c | 2963 | static int clean_item_instance( |
a5e4f3ab | 2964 | Context *c, |
a9bc518c LP |
2965 | Item *i, |
2966 | const char* instance, | |
2967 | CreationMode creation) { | |
2968 | ||
7fd1b19b | 2969 | _cleanup_closedir_ DIR *d = NULL; |
7cd296c2 | 2970 | STRUCT_STATX_DEFINE(sx); |
ca194a2a | 2971 | int mountpoint, r; |
78a92a5a | 2972 | usec_t cutoff, n; |
ddb439b8 | 2973 | |
78a92a5a MS |
2974 | assert(i); |
2975 | ||
2976 | if (!i->age_set) | |
2977 | return 0; | |
2978 | ||
2979 | n = now(CLOCK_REALTIME); | |
2980 | if (n < i->age) | |
2981 | return 0; | |
2982 | ||
2983 | cutoff = n - i->age; | |
2984 | ||
df99a9ef | 2985 | d = opendir_nomod(instance); |
78a92a5a | 2986 | if (!d) { |
d710aaf7 | 2987 | if (IN_SET(errno, ENOENT, ENOTDIR)) { |
582deb84 | 2988 | log_debug_errno(errno, "Directory \"%s\": %m", instance); |
78a92a5a | 2989 | return 0; |
582deb84 | 2990 | } |
78a92a5a | 2991 | |
d710aaf7 | 2992 | return log_error_errno(errno, "Failed to open directory %s: %m", instance); |
78a92a5a MS |
2993 | } |
2994 | ||
ca194a2a LP |
2995 | r = statx_fallback(dirfd(d), "", AT_EMPTY_PATH, STATX_MODE|STATX_INO|STATX_ATIME|STATX_MTIME, &sx); |
2996 | if (r < 0) | |
2997 | return log_error_errno(r, "statx(%s) failed: %m", instance); | |
ddb439b8 | 2998 | |
ca194a2a LP |
2999 | if (FLAGS_SET(sx.stx_attributes_mask, STATX_ATTR_MOUNT_ROOT)) |
3000 | mountpoint = FLAGS_SET(sx.stx_attributes, STATX_ATTR_MOUNT_ROOT); | |
3001 | else { | |
ddb439b8 | 3002 | struct stat ps; |
78a92a5a | 3003 | |
ddb439b8 LP |
3004 | if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0) |
3005 | return log_error_errno(errno, "stat(%s/..) failed: %m", i->path); | |
78a92a5a | 3006 | |
ddb439b8 | 3007 | mountpoint = |
ca194a2a LP |
3008 | sx.stx_dev_major != major(ps.st_dev) || |
3009 | sx.stx_dev_minor != minor(ps.st_dev) || | |
3010 | sx.stx_ino != ps.st_ino; | |
ddb439b8 | 3011 | } |
78a92a5a | 3012 | |
7f7a50dd SK |
3013 | if (DEBUG_LOGGING) { |
3014 | _cleanup_free_ char *ab_f = NULL, *ab_d = NULL; | |
3015 | ||
3016 | ab_f = age_by_to_string(i->age_by_file, false); | |
3017 | if (!ab_f) | |
3018 | return log_oom(); | |
3019 | ||
3020 | ab_d = age_by_to_string(i->age_by_dir, true); | |
3021 | if (!ab_d) | |
3022 | return log_oom(); | |
3023 | ||
3024 | log_debug("Cleanup threshold for %s \"%s\" is %s; age-by: %s%s", | |
3025 | mountpoint ? "mount point" : "directory", | |
3026 | instance, | |
0086ef19 | 3027 | FORMAT_TIMESTAMP_STYLE(cutoff, TIMESTAMP_US), |
7f7a50dd SK |
3028 | ab_f, ab_d); |
3029 | } | |
582deb84 | 3030 | |
a5e4f3ab | 3031 | return dir_cleanup(c, i, instance, d, |
41fea218 AV |
3032 | statx_timestamp_load_nsec(&sx.stx_atime), |
3033 | statx_timestamp_load_nsec(&sx.stx_mtime), | |
ca194a2a LP |
3034 | cutoff * NSEC_PER_USEC, |
3035 | sx.stx_dev_major, sx.stx_dev_minor, mountpoint, | |
7f7a50dd SK |
3036 | MAX_DEPTH, i->keep_first_level, |
3037 | i->age_by_file, i->age_by_dir); | |
78a92a5a MS |
3038 | } |
3039 | ||
a5e4f3ab LP |
3040 | static int clean_item(Context *c, Item *i) { |
3041 | assert(c); | |
78a92a5a MS |
3042 | assert(i); |
3043 | ||
582deb84 ZJS |
3044 | log_debug("Running clean action for entry %c %s", (char) i->type, i->path); |
3045 | ||
78a92a5a | 3046 | switch (i->type) { |
a5e4f3ab | 3047 | |
78a92a5a | 3048 | case CREATE_DIRECTORY: |
9cb3204f | 3049 | case TRUNCATE_DIRECTORY: |
d7b8eec7 | 3050 | case CREATE_SUBVOLUME: |
5fb13eb5 LP |
3051 | case CREATE_SUBVOLUME_INHERIT_QUOTA: |
3052 | case CREATE_SUBVOLUME_NEW_QUOTA: | |
849958d1 | 3053 | case COPY_FILES: |
a5e4f3ab | 3054 | clean_item_instance(c, i, i->path, CREATION_EXISTING); |
df8dee85 | 3055 | return 0; |
a5e4f3ab | 3056 | |
65241c14 | 3057 | case EMPTY_DIRECTORY: |
9cb3204f | 3058 | case IGNORE_PATH: |
78a92a5a | 3059 | case IGNORE_DIRECTORY_PATH: |
a5e4f3ab LP |
3060 | return glob_item(c, i, clean_item_instance); |
3061 | ||
78a92a5a | 3062 | default: |
df8dee85 | 3063 | return 0; |
78a92a5a | 3064 | } |
78a92a5a MS |
3065 | } |
3066 | ||
a5e4f3ab LP |
3067 | static int process_item( |
3068 | Context *c, | |
3069 | Item *i, | |
3070 | OperationMask operation) { | |
3071 | ||
811a1587 | 3072 | OperationMask todo; |
bd6d28f2 ZJS |
3073 | _cleanup_free_ char *_path = NULL; |
3074 | const char *path; | |
81a18380 | 3075 | int r; |
3b63d2d3 | 3076 | |
a5e4f3ab | 3077 | assert(c); |
3b63d2d3 LP |
3078 | assert(i); |
3079 | ||
811a1587 LP |
3080 | todo = operation & ~i->done; |
3081 | if (todo == 0) /* Everything already done? */ | |
1910cd0e LP |
3082 | return 0; |
3083 | ||
811a1587 | 3084 | i->done |= operation; |
1910cd0e | 3085 | |
bd6d28f2 ZJS |
3086 | path = i->path; |
3087 | if (string_is_glob(path)) { | |
3088 | /* We can't easily check whether a glob matches any autofs path, so let's do the check only | |
3089 | * for the non-glob part. */ | |
3090 | ||
3091 | r = glob_non_glob_prefix(path, &_path); | |
3092 | if (r < 0 && r != -ENOENT) | |
3093 | return log_debug_errno(r, "Failed to deglob path: %m"); | |
3094 | if (r >= 0) | |
3095 | path = _path; | |
3096 | } | |
3097 | ||
f461a28d | 3098 | r = chase(path, arg_root, CHASE_NO_AUTOFS|CHASE_NONEXISTENT|CHASE_WARN, NULL, NULL); |
21af3386 | 3099 | if (r == -EREMOTE) { |
bd6d28f2 | 3100 | log_notice_errno(r, "Skipping %s", i->path); /* We log the configured path, to not confuse the user. */ |
811a1587 | 3101 | return 0; |
145b8d0f FB |
3102 | } |
3103 | if (r < 0) | |
48d96904 | 3104 | log_debug_errno(r, "Failed to determine whether '%s' is below autofs, ignoring: %m", i->path); |
655f2da0 | 3105 | |
a5e4f3ab | 3106 | r = FLAGS_SET(operation, OPERATION_CREATE) ? create_item(c, i) : 0; |
6d7b5433 WD |
3107 | /* Failure can only be tolerated for create */ |
3108 | if (i->allow_failure) | |
3109 | r = 0; | |
3b63d2d3 | 3110 | |
81a18380 LB |
3111 | RET_GATHER(r, FLAGS_SET(operation, OPERATION_REMOVE) ? remove_item(c, i) : 0); |
3112 | RET_GATHER(r, FLAGS_SET(operation, OPERATION_CLEAN) ? clean_item(c, i) : 0); | |
3113 | RET_GATHER(r, FLAGS_SET(operation, OPERATION_PURGE) ? purge_item(c, i) : 0); | |
1a967b6b | 3114 | |
81a18380 | 3115 | return r; |
3b63d2d3 LP |
3116 | } |
3117 | ||
a5e4f3ab LP |
3118 | static int process_item_array( |
3119 | Context *c, | |
3120 | ItemArray *array, | |
3121 | OperationMask operation) { | |
3122 | ||
96d10d78 LP |
3123 | int r = 0; |
3124 | size_t n; | |
753615e8 | 3125 | |
a5e4f3ab | 3126 | assert(c); |
3f93da98 ZJS |
3127 | assert(array); |
3128 | ||
811a1587 LP |
3129 | /* Create any parent first. */ |
3130 | if (FLAGS_SET(operation, OPERATION_CREATE) && array->parent) | |
a5e4f3ab | 3131 | r = process_item_array(c, array->parent, operation & OPERATION_CREATE); |
811a1587 LP |
3132 | |
3133 | /* Clean up all children first */ | |
81a18380 | 3134 | if ((operation & (OPERATION_REMOVE|OPERATION_CLEAN|OPERATION_PURGE)) && !set_isempty(array->children)) { |
a5e4f3ab | 3135 | ItemArray *cc; |
811a1587 | 3136 | |
a5e4f3ab | 3137 | SET_FOREACH(cc, array->children) { |
811a1587 LP |
3138 | int k; |
3139 | ||
81a18380 | 3140 | k = process_item_array(c, cc, operation & (OPERATION_REMOVE|OPERATION_CLEAN|OPERATION_PURGE)); |
811a1587 LP |
3141 | if (k < 0 && r == 0) |
3142 | r = k; | |
3143 | } | |
3144 | } | |
3145 | ||
96d10d78 LP |
3146 | for (n = 0; n < array->n_items; n++) { |
3147 | int k; | |
3148 | ||
a5e4f3ab | 3149 | k = process_item(c, array->items + n, operation); |
3f93da98 ZJS |
3150 | if (k < 0 && r == 0) |
3151 | r = k; | |
3152 | } | |
3153 | ||
3154 | return r; | |
3155 | } | |
3b63d2d3 | 3156 | |
3f93da98 ZJS |
3157 | static void item_free_contents(Item *i) { |
3158 | assert(i); | |
3b63d2d3 | 3159 | free(i->path); |
468d726b | 3160 | free(i->argument); |
708daf42 | 3161 | free(i->binary_argument); |
ebf4e801 | 3162 | strv_free(i->xattrs); |
f8eeeaf9 | 3163 | |
349cc4a5 | 3164 | #if HAVE_ACL |
9f804ab0 SJ |
3165 | if (i->acl_access) |
3166 | acl_free(i->acl_access); | |
3167 | ||
26d98cdd MY |
3168 | if (i->acl_access_exec) |
3169 | acl_free(i->acl_access_exec); | |
3170 | ||
9f804ab0 SJ |
3171 | if (i->acl_default) |
3172 | acl_free(i->acl_default); | |
f8eeeaf9 | 3173 | #endif |
3b63d2d3 LP |
3174 | } |
3175 | ||
87938c3b | 3176 | static ItemArray* item_array_free(ItemArray *a) { |
96d10d78 | 3177 | size_t n; |
3f93da98 ZJS |
3178 | |
3179 | if (!a) | |
87938c3b | 3180 | return NULL; |
3f93da98 | 3181 | |
96d10d78 | 3182 | for (n = 0; n < a->n_items; n++) |
3f93da98 | 3183 | item_free_contents(a->items + n); |
96d10d78 | 3184 | |
811a1587 | 3185 | set_free(a->children); |
3f93da98 | 3186 | free(a->items); |
87938c3b | 3187 | return mfree(a); |
3f93da98 | 3188 | } |
e2f2fb78 | 3189 | |
93bab288 | 3190 | static int item_compare(const Item *a, const Item *b) { |
17493fa5 LP |
3191 | /* Make sure that the ownership taking item is put first, so |
3192 | * that we first create the node, and then can adjust it */ | |
3193 | ||
93bab288 | 3194 | if (takes_ownership(a->type) && !takes_ownership(b->type)) |
17493fa5 | 3195 | return -1; |
93bab288 | 3196 | if (!takes_ownership(a->type) && takes_ownership(b->type)) |
17493fa5 LP |
3197 | return 1; |
3198 | ||
93bab288 | 3199 | return CMP(a->type, b->type); |
17493fa5 LP |
3200 | } |
3201 | ||
a6aafd6a | 3202 | static bool item_compatible(const Item *a, const Item *b) { |
bfe95f35 LP |
3203 | assert(a); |
3204 | assert(b); | |
3f93da98 | 3205 | assert(streq(a->path, b->path)); |
bfe95f35 | 3206 | |
3f93da98 ZJS |
3207 | if (takes_ownership(a->type) && takes_ownership(b->type)) |
3208 | /* check if the items are the same */ | |
708daf42 LP |
3209 | return memcmp_nn(item_binary_argument(a), item_binary_argument_size(a), |
3210 | item_binary_argument(b), item_binary_argument_size(b)) == 0 && | |
bfe95f35 | 3211 | |
3f93da98 ZJS |
3212 | a->uid_set == b->uid_set && |
3213 | a->uid == b->uid && | |
cc43328c | 3214 | a->uid_only_create == b->uid_only_create && |
bfe95f35 | 3215 | |
3f93da98 ZJS |
3216 | a->gid_set == b->gid_set && |
3217 | a->gid == b->gid && | |
cc43328c | 3218 | a->gid_only_create == b->gid_only_create && |
bfe95f35 | 3219 | |
3f93da98 ZJS |
3220 | a->mode_set == b->mode_set && |
3221 | a->mode == b->mode && | |
cc43328c | 3222 | a->mode_only_create == b->mode_only_create && |
bfe95f35 | 3223 | |
3f93da98 ZJS |
3224 | a->age_set == b->age_set && |
3225 | a->age == b->age && | |
bfe95f35 | 3226 | |
7f7a50dd SK |
3227 | a->age_by_file == b->age_by_file && |
3228 | a->age_by_dir == b->age_by_dir && | |
3229 | ||
3f93da98 | 3230 | a->mask_perms == b->mask_perms && |
bfe95f35 | 3231 | |
3f93da98 | 3232 | a->keep_first_level == b->keep_first_level && |
468d726b | 3233 | |
3f93da98 | 3234 | a->major_minor == b->major_minor; |
468d726b | 3235 | |
bfe95f35 LP |
3236 | return true; |
3237 | } | |
3238 | ||
a2aced4a | 3239 | static bool should_include_path(const char *path) { |
abef3f91 | 3240 | STRV_FOREACH(prefix, arg_exclude_prefixes) |
582deb84 ZJS |
3241 | if (path_startswith(path, *prefix)) { |
3242 | log_debug("Entry \"%s\" matches exclude prefix \"%s\", skipping.", | |
3243 | path, *prefix); | |
5c795114 | 3244 | return false; |
582deb84 | 3245 | } |
a2aced4a | 3246 | |
abef3f91 | 3247 | STRV_FOREACH(prefix, arg_include_prefixes) |
582deb84 ZJS |
3248 | if (path_startswith(path, *prefix)) { |
3249 | log_debug("Entry \"%s\" matches include prefix \"%s\".", path, *prefix); | |
a2aced4a | 3250 | return true; |
582deb84 | 3251 | } |
a2aced4a | 3252 | |
6b000af4 | 3253 | /* no matches, so we should include this path only if we have no allow list at all */ |
7b943bb7 | 3254 | if (strv_isempty(arg_include_prefixes)) |
582deb84 ZJS |
3255 | return true; |
3256 | ||
3257 | log_debug("Entry \"%s\" does not match any include prefix, skipping.", path); | |
3258 | return false; | |
a2aced4a DR |
3259 | } |
3260 | ||
172e9cc3 | 3261 | static int specifier_expansion_from_arg(const Specifier *specifier_table, Item *i) { |
4cef1923 FB |
3262 | int r; |
3263 | ||
3264 | assert(i); | |
3265 | ||
4e361acc | 3266 | if (!i->argument) |
4cef1923 FB |
3267 | return 0; |
3268 | ||
3269 | switch (i->type) { | |
3270 | case COPY_FILES: | |
3271 | case CREATE_SYMLINK: | |
3272 | case CREATE_FILE: | |
3273 | case TRUNCATE_FILE: | |
e437538f ZJS |
3274 | case WRITE_FILE: { |
3275 | _cleanup_free_ char *unescaped = NULL, *resolved = NULL; | |
3276 | ssize_t l; | |
3277 | ||
3278 | l = cunescape(i->argument, 0, &unescaped); | |
3279 | if (l < 0) | |
3280 | return log_error_errno(l, "Failed to unescape parameter to write: %s", i->argument); | |
4cef1923 | 3281 | |
de61a04b | 3282 | r = specifier_printf(unescaped, PATH_MAX-1, specifier_table, arg_root, NULL, &resolved); |
4cef1923 FB |
3283 | if (r < 0) |
3284 | return r; | |
3285 | ||
e437538f ZJS |
3286 | return free_and_replace(i->argument, resolved); |
3287 | } | |
4cef1923 | 3288 | case SET_XATTR: |
de010b0b | 3289 | case RECURSIVE_SET_XATTR: |
dfe01841 | 3290 | STRV_FOREACH(xattr, i->xattrs) { |
e437538f ZJS |
3291 | _cleanup_free_ char *resolved = NULL; |
3292 | ||
de61a04b | 3293 | r = specifier_printf(*xattr, SIZE_MAX, specifier_table, arg_root, NULL, &resolved); |
4cef1923 FB |
3294 | if (r < 0) |
3295 | return r; | |
3296 | ||
3297 | free_and_replace(*xattr, resolved); | |
3298 | } | |
e437538f | 3299 | return 0; |
de010b0b | 3300 | |
4cef1923 | 3301 | default: |
e437538f | 3302 | return 0; |
4cef1923 | 3303 | } |
4cef1923 FB |
3304 | } |
3305 | ||
a2d1fb88 LP |
3306 | static int patch_var_run(const char *fname, unsigned line, char **path) { |
3307 | const char *k; | |
3308 | char *n; | |
3309 | ||
3310 | assert(path); | |
3311 | assert(*path); | |
3312 | ||
3313 | /* Optionally rewrites lines referencing /var/run/, to use /run/ instead. Why bother? tmpfiles merges lines in | |
3314 | * some cases and detects conflicts in others. If files/directories are specified through two equivalent lines | |
3315 | * this is problematic as neither case will be detected. Ideally we'd detect these cases by resolving symlinks | |
3316 | * early, but that's precisely not what we can do here as this code very likely is running very early on, at a | |
3317 | * time where the paths in question are not available yet, or even more importantly, our own tmpfiles rules | |
3318 | * might create the paths that are intermediary to the listed paths. We can't really cover the generic case, | |
3319 | * but the least we can do is cover the specific case of /var/run vs. /run, as /var/run is a legacy name for | |
3320 | * /run only, and we explicitly document that and require that on systemd systems the former is a symlink to | |
7227dd81 | 3321 | * the latter. Moreover files below this path are by far the primary use case for tmpfiles.d/. */ |
a2d1fb88 LP |
3322 | |
3323 | k = path_startswith(*path, "/var/run/"); | |
3324 | if (isempty(k)) /* Don't complain about other paths than /var/run, and not about /var/run itself either. */ | |
3325 | return 0; | |
3326 | ||
2d9b74ba | 3327 | n = path_join("/run", k); |
a2d1fb88 LP |
3328 | if (!n) |
3329 | return log_oom(); | |
3330 | ||
3331 | /* Also log about this briefly. We do so at LOG_NOTICE level, as we fixed up the situation automatically, hence | |
3332 | * there's no immediate need for action by the user. However, in the interest of making things less confusing | |
3333 | * to the user, let's still inform the user that these snippets should really be updated. */ | |
b88ba6c7 ZJS |
3334 | log_syntax(NULL, LOG_NOTICE, fname, line, 0, |
3335 | "Line references path below legacy directory /var/run/, updating %s → %s; please update the tmpfiles.d/ drop-in file accordingly.", | |
3336 | *path, n); | |
a2d1fb88 | 3337 | |
81fa4479 | 3338 | free_and_replace(*path, n); |
a2d1fb88 LP |
3339 | |
3340 | return 0; | |
3341 | } | |
3342 | ||
a3451c2c LP |
3343 | static int find_uid(const char *user, uid_t *ret_uid, Hashmap **cache) { |
3344 | int r; | |
3345 | ||
3346 | assert(user); | |
3347 | assert(ret_uid); | |
3348 | ||
3349 | /* First: parse as numeric UID string */ | |
3350 | r = parse_uid(user, ret_uid); | |
3351 | if (r >= 0) | |
3352 | return r; | |
3353 | ||
3354 | /* Second: pass to NSS if we are running "online" */ | |
3355 | if (!arg_root) | |
3356 | return get_user_creds(&user, ret_uid, NULL, NULL, NULL, 0); | |
3357 | ||
3358 | /* Third, synthesize "root" unconditionally */ | |
3359 | if (streq(user, "root")) { | |
3360 | *ret_uid = 0; | |
3361 | return 0; | |
3362 | } | |
3363 | ||
3364 | /* Fourth: use fgetpwent() to read /etc/passwd directly, if we are "offline" */ | |
3365 | return name_to_uid_offline(arg_root, user, ret_uid, cache); | |
3366 | } | |
3367 | ||
3368 | static int find_gid(const char *group, gid_t *ret_gid, Hashmap **cache) { | |
3369 | int r; | |
3370 | ||
3371 | assert(group); | |
3372 | assert(ret_gid); | |
3373 | ||
3374 | /* First: parse as numeric GID string */ | |
3375 | r = parse_gid(group, ret_gid); | |
3376 | if (r >= 0) | |
3377 | return r; | |
3378 | ||
3379 | /* Second: pass to NSS if we are running "online" */ | |
3380 | if (!arg_root) | |
3381 | return get_group_creds(&group, ret_gid, 0); | |
3382 | ||
3383 | /* Third, synthesize "root" unconditionally */ | |
3384 | if (streq(group, "root")) { | |
3385 | *ret_gid = 0; | |
3386 | return 0; | |
3387 | } | |
3388 | ||
3389 | /* Fourth: use fgetgrent() to read /etc/group directly, if we are "offline" */ | |
3390 | return name_to_gid_offline(arg_root, group, ret_gid, cache); | |
3391 | } | |
3392 | ||
7f7a50dd SK |
3393 | static int parse_age_by_from_arg(const char *age_by_str, Item *item) { |
3394 | AgeBy ab_f = 0, ab_d = 0; | |
3395 | ||
3396 | static const struct { | |
3397 | char age_by_chr; | |
3398 | AgeBy age_by_flag; | |
3399 | } age_by_types[] = { | |
3400 | { 'a', AGE_BY_ATIME }, | |
3401 | { 'b', AGE_BY_BTIME }, | |
3402 | { 'c', AGE_BY_CTIME }, | |
3403 | { 'm', AGE_BY_MTIME }, | |
3404 | }; | |
3405 | ||
3406 | assert(age_by_str); | |
3407 | assert(item); | |
3408 | ||
3409 | if (isempty(age_by_str)) | |
3410 | return -EINVAL; | |
3411 | ||
3412 | for (const char *s = age_by_str; *s != 0; s++) { | |
3413 | size_t i; | |
3414 | ||
3415 | /* Ignore whitespace. */ | |
3416 | if (strchr(WHITESPACE, *s)) | |
3417 | continue; | |
3418 | ||
3419 | for (i = 0; i < ELEMENTSOF(age_by_types); i++) { | |
3420 | /* Check lower-case for files, upper-case for directories. */ | |
3421 | if (*s == age_by_types[i].age_by_chr) { | |
3422 | ab_f |= age_by_types[i].age_by_flag; | |
3423 | break; | |
3424 | } else if (*s == ascii_toupper(age_by_types[i].age_by_chr)) { | |
3425 | ab_d |= age_by_types[i].age_by_flag; | |
3426 | break; | |
3427 | } | |
3428 | } | |
3429 | ||
3430 | /* Invalid character. */ | |
3431 | if (i >= ELEMENTSOF(age_by_types)) | |
3432 | return -EINVAL; | |
3433 | } | |
3434 | ||
3435 | /* No match. */ | |
3436 | if (ab_f == 0 && ab_d == 0) | |
3437 | return -EINVAL; | |
3438 | ||
3439 | item->age_by_file = ab_f > 0 ? ab_f : AGE_BY_DEFAULT_FILE; | |
3440 | item->age_by_dir = ab_d > 0 ? ab_d : AGE_BY_DEFAULT_DIR; | |
3441 | ||
3442 | return 0; | |
3443 | } | |
3444 | ||
a6aafd6a | 3445 | static bool is_duplicated_item(ItemArray *existing, const Item *i) { |
9af74e0f FB |
3446 | |
3447 | assert(existing); | |
3448 | assert(i); | |
3449 | ||
3450 | for (size_t n = 0; n < existing->n_items; n++) { | |
a6aafd6a | 3451 | const Item *e = existing->items + n; |
9af74e0f FB |
3452 | |
3453 | if (item_compatible(e, i)) | |
3454 | continue; | |
3455 | ||
3456 | /* Only multiple 'w+' lines for the same path are allowed. */ | |
3457 | if (e->type != WRITE_FILE || !e->append_or_force || | |
3458 | i->type != WRITE_FILE || !i->append_or_force) | |
3459 | return true; | |
3460 | } | |
3461 | ||
3462 | return false; | |
3463 | } | |
3464 | ||
a3451c2c | 3465 | static int parse_line( |
a5e4f3ab | 3466 | Context *c, |
a3451c2c LP |
3467 | const char *fname, |
3468 | unsigned line, | |
3469 | const char *buffer, | |
376d0495 | 3470 | bool *invalid_config) { |
1731e34a | 3471 | |
cde684a2 | 3472 | _cleanup_free_ char *action = NULL, *mode = NULL, *user = NULL, *group = NULL, *age = NULL, *path = NULL; |
7f7a50dd SK |
3473 | _cleanup_(item_free_contents) Item i = { |
3474 | /* The "age-by" argument considers all file timestamp types by default. */ | |
3475 | .age_by_file = AGE_BY_DEFAULT_FILE, | |
3476 | .age_by_dir = AGE_BY_DEFAULT_DIR, | |
3477 | }; | |
3f93da98 | 3478 | ItemArray *existing; |
ef43a391 | 3479 | OrderedHashmap *h; |
657cf7f4 | 3480 | int r, pos; |
e0ea6af3 ZJS |
3481 | bool append_or_force = false, boot = false, allow_failure = false, try_replace = false, |
3482 | unbase64 = false, from_cred = false, missing_user_or_group = false; | |
3b63d2d3 | 3483 | |
a5e4f3ab | 3484 | assert(c); |
3b63d2d3 LP |
3485 | assert(fname); |
3486 | assert(line >= 1); | |
3487 | assert(buffer); | |
3488 | ||
172e9cc3 ZJS |
3489 | const Specifier specifier_table[] = { |
3490 | { 'a', specifier_architecture, NULL }, | |
3491 | { 'b', specifier_boot_id, NULL }, | |
3492 | { 'B', specifier_os_build_id, NULL }, | |
9a5893e9 ZJS |
3493 | { 'H', specifier_hostname, NULL }, |
3494 | { 'l', specifier_short_hostname, NULL }, | |
6db98098 | 3495 | { 'm', specifier_machine_id, NULL }, |
172e9cc3 ZJS |
3496 | { 'o', specifier_os_id, NULL }, |
3497 | { 'v', specifier_kernel_release, NULL }, | |
3498 | { 'w', specifier_os_version_id, NULL }, | |
3499 | { 'W', specifier_os_variant_id, NULL }, | |
3500 | ||
3501 | { 'h', specifier_user_home, NULL }, | |
3502 | ||
3503 | { 'C', specifier_directory, UINT_TO_PTR(DIRECTORY_CACHE) }, | |
3504 | { 'L', specifier_directory, UINT_TO_PTR(DIRECTORY_LOGS) }, | |
3505 | { 'S', specifier_directory, UINT_TO_PTR(DIRECTORY_STATE) }, | |
3506 | { 't', specifier_directory, UINT_TO_PTR(DIRECTORY_RUNTIME) }, | |
3507 | ||
4870133b | 3508 | COMMON_CREDS_SPECIFIERS(arg_runtime_scope), |
172e9cc3 ZJS |
3509 | COMMON_TMP_SPECIFIERS, |
3510 | {} | |
3511 | }; | |
3512 | ||
68685607 | 3513 | r = extract_many_words( |
4034a06d | 3514 | &buffer, |
68685607 | 3515 | NULL, |
4b6621fe | 3516 | EXTRACT_UNQUOTE | EXTRACT_CUNESCAPE, |
4034a06d LP |
3517 | &action, |
3518 | &path, | |
3519 | &mode, | |
3520 | &user, | |
3521 | &group, | |
3522 | &age, | |
3523 | NULL); | |
d9daae55 | 3524 | if (r < 0) { |
751223fe ZJS |
3525 | if (IN_SET(r, -EINVAL, -EBADSLT)) |
3526 | /* invalid quoting and such or an unknown specifier */ | |
d9daae55 | 3527 | *invalid_config = true; |
d02933fd | 3528 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Failed to parse line: %m"); |
4b93699d | 3529 | } else if (r < 2) { |
d9daae55 | 3530 | *invalid_config = true; |
d02933fd | 3531 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "Syntax error."); |
5008d581 LP |
3532 | } |
3533 | ||
e7b88b7b | 3534 | if (!empty_or_dash(buffer)) { |
4034a06d LP |
3535 | i.argument = strdup(buffer); |
3536 | if (!i.argument) | |
3537 | return log_oom(); | |
3538 | } | |
3539 | ||
2e78fa79 | 3540 | if (isempty(action)) { |
d9daae55 | 3541 | *invalid_config = true; |
d02933fd | 3542 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "Command too short '%s'.", action); |
2e78fa79 LP |
3543 | } |
3544 | ||
5f255144 ZJS |
3545 | for (pos = 1; action[pos]; pos++) { |
3546 | if (action[pos] == '!' && !boot) | |
3547 | boot = true; | |
c55ac248 ZS |
3548 | else if (action[pos] == '+' && !append_or_force) |
3549 | append_or_force = true; | |
6d7b5433 WD |
3550 | else if (action[pos] == '-' && !allow_failure) |
3551 | allow_failure = true; | |
c46c3233 AW |
3552 | else if (action[pos] == '=' && !try_replace) |
3553 | try_replace = true; | |
708daf42 LP |
3554 | else if (action[pos] == '~' && !unbase64) |
3555 | unbase64 = true; | |
e52f6f63 LP |
3556 | else if (action[pos] == '^' && !from_cred) |
3557 | from_cred = true; | |
5f255144 | 3558 | else { |
d9daae55 | 3559 | *invalid_config = true; |
d02933fd | 3560 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "Unknown modifiers in command '%s'", action); |
5f255144 | 3561 | } |
2e78fa79 LP |
3562 | } |
3563 | ||
582deb84 | 3564 | if (boot && !arg_boot) { |
d02933fd | 3565 | log_syntax(NULL, LOG_DEBUG, fname, line, 0, "Ignoring entry %s \"%s\" because --boot is not specified.", action, path); |
c4708f13 | 3566 | return 0; |
582deb84 | 3567 | } |
c4708f13 | 3568 | |
3f93da98 | 3569 | i.type = action[0]; |
c55ac248 | 3570 | i.append_or_force = append_or_force; |
6d7b5433 | 3571 | i.allow_failure = allow_failure; |
c46c3233 | 3572 | i.try_replace = try_replace; |
2e78fa79 | 3573 | |
de61a04b | 3574 | r = specifier_printf(path, PATH_MAX-1, specifier_table, arg_root, NULL, &i.path); |
6db98098 | 3575 | if (ERRNO_IS_NOINFO(r)) |
4cef1923 | 3576 | return log_unresolvable_specifier(fname, line); |
d9daae55 | 3577 | if (r < 0) { |
751223fe ZJS |
3578 | if (IN_SET(r, -EINVAL, -EBADSLT)) |
3579 | *invalid_config = true; | |
d02933fd | 3580 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Failed to replace specifiers in '%s': %m", path); |
d9daae55 | 3581 | } |
1731e34a | 3582 | |
a2d1fb88 LP |
3583 | r = patch_var_run(fname, line, &i.path); |
3584 | if (r < 0) | |
3585 | return r; | |
3586 | ||
35c18a51 LP |
3587 | if (!path_is_absolute(i.path)) { |
3588 | *invalid_config = true; | |
3589 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), | |
3590 | "Path '%s' not absolute.", i.path); | |
3591 | } | |
3592 | ||
3593 | path_simplify(i.path); | |
3594 | ||
3f93da98 | 3595 | switch (i.type) { |
468d726b | 3596 | |
777b87e7 | 3597 | case CREATE_DIRECTORY: |
d7b8eec7 | 3598 | case CREATE_SUBVOLUME: |
5fb13eb5 LP |
3599 | case CREATE_SUBVOLUME_INHERIT_QUOTA: |
3600 | case CREATE_SUBVOLUME_NEW_QUOTA: | |
df8dee85 | 3601 | case EMPTY_DIRECTORY: |
777b87e7 MS |
3602 | case TRUNCATE_DIRECTORY: |
3603 | case CREATE_FIFO: | |
3604 | case IGNORE_PATH: | |
78a92a5a | 3605 | case IGNORE_DIRECTORY_PATH: |
777b87e7 MS |
3606 | case REMOVE_PATH: |
3607 | case RECURSIVE_REMOVE_PATH: | |
e73a03e0 | 3608 | case ADJUST_MODE: |
777b87e7 MS |
3609 | case RELABEL_PATH: |
3610 | case RECURSIVE_RELABEL_PATH: | |
c82500c6 | 3611 | if (i.argument) |
2676befc JJ |
3612 | log_syntax(NULL, |
3613 | LOG_WARNING, | |
3614 | fname, | |
3615 | line, | |
3616 | 0, | |
3617 | "%c lines don't take argument fields, ignoring.", | |
3618 | (char) i.type); | |
c82500c6 LP |
3619 | |
3620 | break; | |
3621 | ||
3622 | case CREATE_FILE: | |
3623 | case TRUNCATE_FILE: | |
777b87e7 | 3624 | break; |
468d726b LP |
3625 | |
3626 | case CREATE_SYMLINK: | |
708daf42 LP |
3627 | if (unbase64) { |
3628 | *invalid_config = true; | |
3629 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "base64 decoding not supported for symlink targets."); | |
3630 | } | |
468d726b LP |
3631 | break; |
3632 | ||
31ed59c5 | 3633 | case WRITE_FILE: |
3f93da98 | 3634 | if (!i.argument) { |
d9daae55 | 3635 | *invalid_config = true; |
d02933fd | 3636 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "Write file requires argument."); |
31ed59c5 LP |
3637 | } |
3638 | break; | |
3639 | ||
849958d1 | 3640 | case COPY_FILES: |
708daf42 LP |
3641 | if (unbase64) { |
3642 | *invalid_config = true; | |
3643 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "base64 decoding not supported for copy sources."); | |
3644 | } | |
849958d1 LP |
3645 | break; |
3646 | ||
468d726b | 3647 | case CREATE_CHAR_DEVICE: |
3a47c40d | 3648 | case CREATE_BLOCK_DEVICE: |
708daf42 LP |
3649 | if (unbase64) { |
3650 | *invalid_config = true; | |
3651 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "base64 decoding not supported for device node creation."); | |
3652 | } | |
3653 | ||
3f93da98 | 3654 | if (!i.argument) { |
d9daae55 | 3655 | *invalid_config = true; |
d02933fd | 3656 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "Device file requires argument."); |
468d726b LP |
3657 | } |
3658 | ||
7176f06c | 3659 | r = parse_devnum(i.argument, &i.major_minor); |
3a47c40d | 3660 | if (r < 0) { |
d9daae55 | 3661 | *invalid_config = true; |
d02933fd | 3662 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Can't parse device file major/minor '%s'.", i.argument); |
468d726b LP |
3663 | } |
3664 | ||
468d726b | 3665 | break; |
468d726b | 3666 | |
ebf4e801 | 3667 | case SET_XATTR: |
b705ab6a | 3668 | case RECURSIVE_SET_XATTR: |
708daf42 LP |
3669 | if (unbase64) { |
3670 | *invalid_config = true; | |
3671 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "base64 decoding not supported for extended attributes."); | |
3672 | } | |
3f93da98 | 3673 | if (!i.argument) { |
d9daae55 | 3674 | *invalid_config = true; |
d02933fd LP |
3675 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), |
3676 | "Set extended attribute requires argument."); | |
ebf4e801 | 3677 | } |
bd550f78 | 3678 | r = parse_xattrs_from_arg(&i); |
ebf4e801 MW |
3679 | if (r < 0) |
3680 | return r; | |
3681 | break; | |
3682 | ||
f8eeeaf9 | 3683 | case SET_ACL: |
b705ab6a | 3684 | case RECURSIVE_SET_ACL: |
708daf42 LP |
3685 | if (unbase64) { |
3686 | *invalid_config = true; | |
3687 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "base64 decoding not supported for ACLs."); | |
3688 | } | |
f8eeeaf9 | 3689 | if (!i.argument) { |
d9daae55 | 3690 | *invalid_config = true; |
d02933fd LP |
3691 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), |
3692 | "Set ACLs requires argument."); | |
f8eeeaf9 | 3693 | } |
bd550f78 | 3694 | r = parse_acls_from_arg(&i); |
f8eeeaf9 ZJS |
3695 | if (r < 0) |
3696 | return r; | |
3697 | break; | |
3698 | ||
88ec4dfa LP |
3699 | case SET_ATTRIBUTE: |
3700 | case RECURSIVE_SET_ATTRIBUTE: | |
708daf42 LP |
3701 | if (unbase64) { |
3702 | *invalid_config = true; | |
3703 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "base64 decoding not supported for file attributes."); | |
3704 | } | |
22c3a6ca | 3705 | if (!i.argument) { |
d9daae55 | 3706 | *invalid_config = true; |
d02933fd LP |
3707 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), |
3708 | "Set file attribute requires argument."); | |
22c3a6ca | 3709 | } |
bd550f78 | 3710 | r = parse_attribute_from_arg(&i); |
751223fe | 3711 | if (IN_SET(r, -EINVAL, -EBADSLT)) |
d9daae55 | 3712 | *invalid_config = true; |
22c3a6ca GB |
3713 | if (r < 0) |
3714 | return r; | |
3715 | break; | |
3716 | ||
777b87e7 | 3717 | default: |
d9daae55 | 3718 | *invalid_config = true; |
d02933fd LP |
3719 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), |
3720 | "Unknown command type '%c'.", (char) i.type); | |
3b63d2d3 | 3721 | } |
468d726b | 3722 | |
3f93da98 | 3723 | if (!should_include_path(i.path)) |
7f2c1f4d | 3724 | return 0; |
5008d581 | 3725 | |
e52f6f63 LP |
3726 | if (!unbase64) { |
3727 | /* Do specifier expansion except if base64 mode is enabled */ | |
708daf42 | 3728 | r = specifier_expansion_from_arg(specifier_table, &i); |
6db98098 | 3729 | if (ERRNO_IS_NOINFO(r)) |
708daf42 LP |
3730 | return log_unresolvable_specifier(fname, line); |
3731 | if (r < 0) { | |
3732 | if (IN_SET(r, -EINVAL, -EBADSLT)) | |
3733 | *invalid_config = true; | |
3734 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Failed to substitute specifiers in argument: %m"); | |
3735 | } | |
751223fe | 3736 | } |
4cef1923 | 3737 | |
1ed8887e | 3738 | switch (i.type) { |
05381430 DL |
3739 | case CREATE_SYMLINK: |
3740 | if (!i.argument) { | |
3741 | i.argument = path_join("/usr/share/factory", i.path); | |
3742 | if (!i.argument) | |
3743 | return log_oom(); | |
3744 | } | |
3745 | break; | |
3746 | ||
1ed8887e DL |
3747 | case COPY_FILES: |
3748 | if (!i.argument) { | |
3749 | i.argument = path_join("/usr/share/factory", i.path); | |
3750 | if (!i.argument) | |
3751 | return log_oom(); | |
3752 | } else if (!path_is_absolute(i.argument)) { | |
3753 | *invalid_config = true; | |
3754 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "Source path '%s' is not absolute.", i.argument); | |
3755 | ||
3756 | } | |
3757 | ||
3758 | if (!empty_or_root(arg_root)) { | |
3759 | char *p; | |
3760 | ||
3761 | p = path_join(arg_root, i.argument); | |
3762 | if (!p) | |
3763 | return log_oom(); | |
3764 | free_and_replace(i.argument, p); | |
3765 | } | |
3766 | ||
3767 | path_simplify(i.argument); | |
3768 | ||
3769 | if (laccess(i.argument, F_OK) == -ENOENT) { | |
3770 | /* Silently skip over lines where the source file is missing. */ | |
3771 | log_syntax(NULL, LOG_DEBUG, fname, line, 0, "Copy source path '%s' does not exist, skipping line.", i.argument); | |
3772 | return 0; | |
3773 | } | |
3774 | ||
3775 | break; | |
3776 | ||
3777 | default: | |
3778 | break; | |
3779 | } | |
3780 | ||
e52f6f63 LP |
3781 | if (from_cred) { |
3782 | if (!i.argument) | |
3783 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EINVAL), "Reading from credential requested, but no credential name specified."); | |
3784 | if (!credential_name_valid(i.argument)) | |
3785 | return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EINVAL), "Credential name not valid: %s", i.argument); | |
3786 | ||
3787 | r = read_credential(i.argument, &i.binary_argument, &i.binary_argument_size); | |
3788 | if (IN_SET(r, -ENXIO, -ENOENT)) { | |
3789 | /* Silently skip over lines that have no credentials passed */ | |
9109009d | 3790 | log_syntax(NULL, LOG_DEBUG, fname, line, 0, |
6db98098 | 3791 | "Credential '%s' not specified, skipping line.", i.argument); |
e52f6f63 LP |
3792 | return 0; |
3793 | } | |
3794 | if (r < 0) | |
3795 | return log_error_errno(r, "Failed to read credential '%s': %m", i.argument); | |
3796 | } | |
3797 | ||
3798 | /* If base64 decoding is requested, do so now */ | |
3799 | if (unbase64 && item_binary_argument(&i)) { | |
3800 | _cleanup_free_ void *data = NULL; | |
3801 | size_t data_size = 0; | |
3802 | ||
bdd2036e MY |
3803 | r = unbase64mem_full(item_binary_argument(&i), item_binary_argument_size(&i), /* secure = */ false, |
3804 | &data, &data_size); | |
e52f6f63 LP |
3805 | if (r < 0) |
3806 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Failed to base64 decode specified argument '%s': %m", i.argument); | |
3807 | ||
3808 | free_and_replace(i.binary_argument, data); | |
3809 | i.binary_argument_size = data_size; | |
3810 | } | |
3811 | ||
7dc6477d | 3812 | if (!empty_or_root(arg_root)) { |
cde684a2 LP |
3813 | char *p; |
3814 | ||
c6134d3e | 3815 | p = path_join(arg_root, i.path); |
cf9a4abd MM |
3816 | if (!p) |
3817 | return log_oom(); | |
81fa4479 | 3818 | free_and_replace(i.path, p); |
cf9a4abd MM |
3819 | } |
3820 | ||
e7b88b7b | 3821 | if (!empty_or_dash(user)) { |
cc43328c LP |
3822 | const char *u; |
3823 | ||
3824 | u = startswith(user, ":"); | |
3825 | if (u) | |
3826 | i.uid_only_create = true; | |
3827 | else | |
3828 | u = user; | |
3829 | ||
376d0495 | 3830 | r = find_uid(u, &i.uid, &c->uid_cache); |
e0ea6af3 ZJS |
3831 | if (r == -ESRCH && arg_graceful) { |
3832 | log_syntax(NULL, LOG_DEBUG, fname, line, r, | |
3833 | "%s: user '%s' not found, not adjusting ownership.", i.path, u); | |
3834 | missing_user_or_group = true; | |
3835 | } else if (r < 0) { | |
d9daae55 | 3836 | *invalid_config = true; |
cc43328c | 3837 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Failed to resolve user '%s': %m", u); |
e0ea6af3 ZJS |
3838 | } else |
3839 | i.uid_set = true; | |
3b63d2d3 LP |
3840 | } |
3841 | ||
e7b88b7b | 3842 | if (!empty_or_dash(group)) { |
cc43328c LP |
3843 | const char *g; |
3844 | ||
3845 | g = startswith(group, ":"); | |
3846 | if (g) | |
3847 | i.gid_only_create = true; | |
3848 | else | |
3849 | g = group; | |
3850 | ||
376d0495 | 3851 | r = find_gid(g, &i.gid, &c->gid_cache); |
e0ea6af3 ZJS |
3852 | if (r == -ESRCH && arg_graceful) { |
3853 | log_syntax(NULL, LOG_DEBUG, fname, line, r, | |
3854 | "%s: group '%s' not found, not adjusting ownership.", i.path, g); | |
3855 | missing_user_or_group = true; | |
3856 | } else if (r < 0) { | |
d9daae55 | 3857 | *invalid_config = true; |
e0ea6af3 ZJS |
3858 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Failed to resolve group '%s': %m", g); |
3859 | } else | |
3860 | i.gid_set = true; | |
3b63d2d3 LP |
3861 | } |
3862 | ||
e7b88b7b | 3863 | if (!empty_or_dash(mode)) { |
cc43328c | 3864 | const char *mm; |
3b63d2d3 LP |
3865 | unsigned m; |
3866 | ||
cc43328c LP |
3867 | for (mm = mode;; mm++) { |
3868 | if (*mm == '~') | |
3869 | i.mask_perms = true; | |
3870 | else if (*mm == ':') | |
3871 | i.mode_only_create = true; | |
3872 | else | |
3873 | break; | |
abef3f91 LP |
3874 | } |
3875 | ||
d02933fd LP |
3876 | r = parse_mode(mm, &m); |
3877 | if (r < 0) { | |
d9daae55 | 3878 | *invalid_config = true; |
d02933fd | 3879 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Invalid mode '%s'.", mode); |
3b63d2d3 LP |
3880 | } |
3881 | ||
3f93da98 ZJS |
3882 | i.mode = m; |
3883 | i.mode_set = true; | |
3b63d2d3 | 3884 | } else |
2223a025 ZJS |
3885 | i.mode = IN_SET(i.type, |
3886 | CREATE_DIRECTORY, | |
3887 | TRUNCATE_DIRECTORY, | |
3888 | CREATE_SUBVOLUME, | |
3889 | CREATE_SUBVOLUME_INHERIT_QUOTA, | |
3890 | CREATE_SUBVOLUME_NEW_QUOTA) ? 0755 : 0644; | |
3b63d2d3 | 3891 | |
e0ea6af3 ZJS |
3892 | if (missing_user_or_group && (i.mode & ~0777) != 0) { |
3893 | /* Refuse any special bits for nodes where we couldn't resolve the ownership properly. */ | |
3894 | mode_t adjusted = i.mode & 0777; | |
3895 | log_syntax(NULL, LOG_INFO, fname, line, 0, | |
3896 | "Changing mode 0%o to 0%o because of changed ownership.", i.mode, adjusted); | |
3897 | i.mode = adjusted; | |
3898 | } | |
3899 | ||
e7b88b7b | 3900 | if (!empty_or_dash(age)) { |
24f3a374 | 3901 | const char *a = age; |
7f7a50dd | 3902 | _cleanup_free_ char *seconds = NULL, *age_by = NULL; |
24f3a374 LP |
3903 | |
3904 | if (*a == '~') { | |
3f93da98 | 3905 | i.keep_first_level = true; |
24f3a374 LP |
3906 | a++; |
3907 | } | |
3908 | ||
7f7a50dd SK |
3909 | /* Format: "age-by:age"; where age-by is "[abcmABCM]+". */ |
3910 | r = split_pair(a, ":", &age_by, &seconds); | |
3911 | if (r == -ENOMEM) | |
3912 | return log_oom(); | |
3913 | if (r < 0 && r != -EINVAL) | |
3914 | return log_error_errno(r, "Failed to parse age-by for '%s': %m", age); | |
3915 | if (r >= 0) { | |
3916 | /* We found a ":", parse the "age-by" part. */ | |
3917 | r = parse_age_by_from_arg(age_by, &i); | |
3918 | if (r == -ENOMEM) | |
3919 | return log_oom(); | |
3920 | if (r < 0) { | |
3921 | *invalid_config = true; | |
3922 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Invalid age-by '%s'.", age_by); | |
3923 | } | |
3924 | ||
3925 | /* For parsing the "age" part, after the ":". */ | |
3926 | a = seconds; | |
3927 | } | |
3928 | ||
d02933fd LP |
3929 | r = parse_sec(a, &i.age); |
3930 | if (r < 0) { | |
d9daae55 | 3931 | *invalid_config = true; |
7f7a50dd | 3932 | return log_syntax(NULL, LOG_ERR, fname, line, r, "Invalid age '%s'.", a); |
3b63d2d3 LP |
3933 | } |
3934 | ||
3f93da98 | 3935 | i.age_set = true; |
3b63d2d3 LP |
3936 | } |
3937 | ||
a5e4f3ab | 3938 | h = needs_glob(i.type) ? c->globs : c->items; |
bfe95f35 | 3939 | |
ef43a391 | 3940 | existing = ordered_hashmap_get(h, i.path); |
468d726b | 3941 | if (existing) { |
9af74e0f FB |
3942 | if (is_duplicated_item(existing, &i)) { |
3943 | log_syntax(NULL, LOG_NOTICE, fname, line, 0, | |
3944 | "Duplicate line for path \"%s\", ignoring.", i.path); | |
3945 | return 0; | |
ebf4e801 MW |
3946 | } |
3947 | } else { | |
3f93da98 | 3948 | existing = new0(ItemArray, 1); |
07982ed1 LP |
3949 | if (!existing) |
3950 | return log_oom(); | |
3951 | ||
ef43a391 | 3952 | r = ordered_hashmap_put(h, i.path, existing); |
ccd114f0 LP |
3953 | if (r < 0) { |
3954 | free(existing); | |
3f93da98 | 3955 | return log_oom(); |
ccd114f0 | 3956 | } |
bfe95f35 LP |
3957 | } |
3958 | ||
319a4f4b | 3959 | if (!GREEDY_REALLOC(existing->items, existing->n_items + 1)) |
3f93da98 | 3960 | return log_oom(); |
5008d581 | 3961 | |
088d71f8 | 3962 | existing->items[existing->n_items++] = TAKE_STRUCT(i); |
dd449aca ZJS |
3963 | |
3964 | /* Sort item array, to enforce stable ordering of application */ | |
96d10d78 | 3965 | typesafe_qsort(existing->items, existing->n_items, item_compare); |
17493fa5 | 3966 | |
7f2c1f4d | 3967 | return 0; |
5008d581 LP |
3968 | } |
3969 | ||
ceaaeb9b ZJS |
3970 | static int cat_config(char **config_dirs, char **args) { |
3971 | _cleanup_strv_free_ char **files = NULL; | |
3972 | int r; | |
3973 | ||
3974 | r = conf_files_list_with_replacement(arg_root, config_dirs, arg_replace, &files, NULL); | |
3975 | if (r < 0) | |
3976 | return r; | |
3977 | ||
1be6a5db ZJS |
3978 | pager_open(arg_pager_flags); |
3979 | ||
3980 | return cat_files(NULL, files, arg_cat_flags); | |
ceaaeb9b ZJS |
3981 | } |
3982 | ||
dd04fb32 LP |
3983 | static int exclude_default_prefixes(void) { |
3984 | int r; | |
3985 | ||
3986 | /* Provide an easy way to exclude virtual/memory file systems from what we do here. Useful in | |
3987 | * combination with --root= where we probably don't want to apply stuff to these dirs as they are | |
3988 | * likely over-mounted if the root directory is actually used, and it wouldbe less than ideal to have | |
3989 | * all kinds of files created/adjusted underneath these mount points. */ | |
3990 | ||
69f3c619 | 3991 | r = strv_extend_many( |
dd04fb32 | 3992 | &arg_exclude_prefixes, |
69f3c619 LP |
3993 | "/dev", |
3994 | "/proc", | |
3995 | "/run", | |
3996 | "/sys"); | |
dd04fb32 LP |
3997 | if (r < 0) |
3998 | return log_oom(); | |
3999 | ||
69f3c619 | 4000 | strv_uniq(arg_exclude_prefixes); |
dd04fb32 LP |
4001 | return 0; |
4002 | } | |
4003 | ||
37ec0fdd LP |
4004 | static int help(void) { |
4005 | _cleanup_free_ char *link = NULL; | |
4006 | int r; | |
4007 | ||
4008 | r = terminal_urlify_man("systemd-tmpfiles", "8", &link); | |
4009 | if (r < 0) | |
4010 | return log_oom(); | |
4011 | ||
3f532a59 LP |
4012 | printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n" |
4013 | "\n%sCreates, deletes and cleans up volatile and temporary files and directories.%s\n\n" | |
5c795114 | 4014 | " -h --help Show this help\n" |
f2b5ca0e | 4015 | " --user Execute user configuration\n" |
eb9da376 | 4016 | " --version Show package version\n" |
ceaaeb9b | 4017 | " --cat-config Show configuration files\n" |
1be6a5db | 4018 | " --tldr Show non-comment parts of configuration\n" |
5c795114 DR |
4019 | " --create Create marked files/directories\n" |
4020 | " --clean Clean up marked directories\n" | |
4021 | " --remove Remove marked files/directories\n" | |
81815651 | 4022 | " --boot Execute actions only safe at boot\n" |
9a27ef09 | 4023 | " --graceful Quietly ignore unknown users or groups\n" |
81a18380 | 4024 | " --purge Delete all files owned by the configuration files\n" |
79ca888f ZJS |
4025 | " --prefix=PATH Only apply rules with the specified prefix\n" |
4026 | " --exclude-prefix=PATH Ignore rules with the specified prefix\n" | |
dd04fb32 | 4027 | " -E Ignore rules prefixed with /dev, /proc, /run, /sys\n" |
d9daae55 | 4028 | " --root=PATH Operate on an alternate filesystem root\n" |
63d3d0a5 | 4029 | " --image=PATH Operate on disk image as filesystem root\n" |
84be0c71 | 4030 | " --image-policy=POLICY Specify disk image dissection policy\n" |
a6d8474f | 4031 | " --replace=PATH Treat arguments as replacement for PATH\n" |
dcd5c891 | 4032 | " --no-pager Do not pipe output into a pager\n" |
bc556335 DDM |
4033 | "\nSee the %s for details.\n", |
4034 | program_invocation_short_name, | |
4035 | ansi_highlight(), | |
4036 | ansi_normal(), | |
4037 | link); | |
37ec0fdd LP |
4038 | |
4039 | return 0; | |
3b63d2d3 LP |
4040 | } |
4041 | ||
4042 | static int parse_argv(int argc, char *argv[]) { | |
3b63d2d3 | 4043 | enum { |
eb9da376 | 4044 | ARG_VERSION = 0x100, |
ceaaeb9b | 4045 | ARG_CAT_CONFIG, |
1be6a5db | 4046 | ARG_TLDR, |
f2b5ca0e | 4047 | ARG_USER, |
3b63d2d3 LP |
4048 | ARG_CREATE, |
4049 | ARG_CLEAN, | |
fba6e687 | 4050 | ARG_REMOVE, |
81a18380 | 4051 | ARG_PURGE, |
81815651 | 4052 | ARG_BOOT, |
e0ea6af3 | 4053 | ARG_GRACEFUL, |
5c795114 DR |
4054 | ARG_PREFIX, |
4055 | ARG_EXCLUDE_PREFIX, | |
cf9a4abd | 4056 | ARG_ROOT, |
63d3d0a5 | 4057 | ARG_IMAGE, |
06e78680 | 4058 | ARG_IMAGE_POLICY, |
a6d8474f | 4059 | ARG_REPLACE, |
dcd5c891 | 4060 | ARG_NO_PAGER, |
3b63d2d3 LP |
4061 | }; |
4062 | ||
4063 | static const struct option options[] = { | |
5c795114 | 4064 | { "help", no_argument, NULL, 'h' }, |
f2b5ca0e | 4065 | { "user", no_argument, NULL, ARG_USER }, |
eb9da376 | 4066 | { "version", no_argument, NULL, ARG_VERSION }, |
ceaaeb9b | 4067 | { "cat-config", no_argument, NULL, ARG_CAT_CONFIG }, |
1be6a5db | 4068 | { "tldr", no_argument, NULL, ARG_TLDR }, |
5c795114 DR |
4069 | { "create", no_argument, NULL, ARG_CREATE }, |
4070 | { "clean", no_argument, NULL, ARG_CLEAN }, | |
4071 | { "remove", no_argument, NULL, ARG_REMOVE }, | |
81a18380 | 4072 | { "purge", no_argument, NULL, ARG_PURGE }, |
81815651 | 4073 | { "boot", no_argument, NULL, ARG_BOOT }, |
e0ea6af3 | 4074 | { "graceful", no_argument, NULL, ARG_GRACEFUL }, |
5c795114 DR |
4075 | { "prefix", required_argument, NULL, ARG_PREFIX }, |
4076 | { "exclude-prefix", required_argument, NULL, ARG_EXCLUDE_PREFIX }, | |
cf9a4abd | 4077 | { "root", required_argument, NULL, ARG_ROOT }, |
63d3d0a5 | 4078 | { "image", required_argument, NULL, ARG_IMAGE }, |
06e78680 | 4079 | { "image-policy", required_argument, NULL, ARG_IMAGE_POLICY }, |
a6d8474f | 4080 | { "replace", required_argument, NULL, ARG_REPLACE }, |
dcd5c891 | 4081 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, |
eb9da376 | 4082 | {} |
3b63d2d3 LP |
4083 | }; |
4084 | ||
0f474365 | 4085 | int c, r; |
3b63d2d3 LP |
4086 | |
4087 | assert(argc >= 0); | |
4088 | assert(argv); | |
4089 | ||
dd04fb32 | 4090 | while ((c = getopt_long(argc, argv, "hE", options, NULL)) >= 0) |
3b63d2d3 LP |
4091 | |
4092 | switch (c) { | |
4093 | ||
4094 | case 'h': | |
37ec0fdd | 4095 | return help(); |
eb9da376 LP |
4096 | |
4097 | case ARG_VERSION: | |
3f6fd1ba | 4098 | return version(); |
3b63d2d3 | 4099 | |
ceaaeb9b | 4100 | case ARG_CAT_CONFIG: |
1be6a5db ZJS |
4101 | arg_cat_flags = CAT_CONFIG_ON; |
4102 | break; | |
4103 | ||
4104 | case ARG_TLDR: | |
4105 | arg_cat_flags = CAT_TLDR; | |
ceaaeb9b ZJS |
4106 | break; |
4107 | ||
f2b5ca0e | 4108 | case ARG_USER: |
4870133b | 4109 | arg_runtime_scope = RUNTIME_SCOPE_USER; |
f2b5ca0e ZJS |
4110 | break; |
4111 | ||
3b63d2d3 | 4112 | case ARG_CREATE: |
1a967b6b | 4113 | arg_operation |= OPERATION_CREATE; |
3b63d2d3 LP |
4114 | break; |
4115 | ||
4116 | case ARG_CLEAN: | |
1a967b6b | 4117 | arg_operation |= OPERATION_CLEAN; |
3b63d2d3 LP |
4118 | break; |
4119 | ||
4120 | case ARG_REMOVE: | |
1a967b6b | 4121 | arg_operation |= OPERATION_REMOVE; |
3b63d2d3 LP |
4122 | break; |
4123 | ||
81815651 ZJS |
4124 | case ARG_BOOT: |
4125 | arg_boot = true; | |
c4708f13 ZJS |
4126 | break; |
4127 | ||
81a18380 LB |
4128 | case ARG_PURGE: |
4129 | arg_operation |= OPERATION_PURGE; | |
4130 | break; | |
4131 | ||
e0ea6af3 ZJS |
4132 | case ARG_GRACEFUL: |
4133 | arg_graceful = true; | |
4134 | break; | |
4135 | ||
fba6e687 | 4136 | case ARG_PREFIX: |
7f13af72 | 4137 | if (strv_extend(&arg_include_prefixes, optarg) < 0) |
a2aced4a | 4138 | return log_oom(); |
fba6e687 LP |
4139 | break; |
4140 | ||
5c795114 | 4141 | case ARG_EXCLUDE_PREFIX: |
7f13af72 | 4142 | if (strv_extend(&arg_exclude_prefixes, optarg) < 0) |
5c795114 DR |
4143 | return log_oom(); |
4144 | break; | |
4145 | ||
63d3d0a5 | 4146 | case ARG_ROOT: |
614b022c | 4147 | r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_root); |
dd04fb32 LP |
4148 | if (r < 0) |
4149 | return r; | |
dd04fb32 LP |
4150 | break; |
4151 | ||
63d3d0a5 | 4152 | case ARG_IMAGE: |
3537577c ZJS |
4153 | #ifdef STANDALONE |
4154 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), | |
4155 | "This systemd-tmpfiles version is compiled without support for --image=."); | |
4156 | #else | |
614b022c | 4157 | r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_image); |
63d3d0a5 LP |
4158 | if (r < 0) |
4159 | return r; | |
3537577c ZJS |
4160 | #endif |
4161 | /* Imply -E here since it makes little sense to create files persistently in the /run mountpoint of a disk image */ | |
63d3d0a5 LP |
4162 | _fallthrough_; |
4163 | ||
4164 | case 'E': | |
4165 | r = exclude_default_prefixes(); | |
0f474365 | 4166 | if (r < 0) |
0f03c2a4 | 4167 | return r; |
63d3d0a5 | 4168 | |
cf9a4abd MM |
4169 | break; |
4170 | ||
06e78680 YW |
4171 | case ARG_IMAGE_POLICY: |
4172 | r = parse_image_policy_argument(optarg, &arg_image_policy); | |
4173 | if (r < 0) | |
4174 | return r; | |
4175 | break; | |
4176 | ||
a6d8474f | 4177 | case ARG_REPLACE: |
37ee46f7 | 4178 | if (!path_is_absolute(optarg)) |
baaa35ad | 4179 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
37ee46f7 ZJS |
4180 | "The argument to --replace= must be an absolute path."); |
4181 | if (!endswith(optarg, ".conf")) | |
4182 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
4183 | "The argument to --replace= must have the extension '.conf'."); | |
a6d8474f ZJS |
4184 | |
4185 | arg_replace = optarg; | |
4186 | break; | |
4187 | ||
dcd5c891 | 4188 | case ARG_NO_PAGER: |
0221d68a | 4189 | arg_pager_flags |= PAGER_DISABLE; |
dcd5c891 LP |
4190 | break; |
4191 | ||
3b63d2d3 LP |
4192 | case '?': |
4193 | return -EINVAL; | |
4194 | ||
4195 | default: | |
04499a70 | 4196 | assert_not_reached(); |
3b63d2d3 | 4197 | } |
3b63d2d3 | 4198 | |
1be6a5db | 4199 | if (arg_operation == 0 && arg_cat_flags == CAT_CONFIG_OFF) |
baaa35ad | 4200 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
81a18380 | 4201 | "You need to specify at least one of --clean, --create, --remove, or --purge."); |
3b63d2d3 | 4202 | |
1be6a5db | 4203 | if (arg_replace && arg_cat_flags != CAT_CONFIG_OFF) |
baaa35ad | 4204 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
1be6a5db | 4205 | "Option --replace= is not supported with --cat-config/--tldr."); |
ceaaeb9b | 4206 | |
baaa35ad ZJS |
4207 | if (arg_replace && optind >= argc) |
4208 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1be6a5db | 4209 | "When --replace= is given, some configuration items must be specified."); |
a6d8474f | 4210 | |
4870133b | 4211 | if (arg_root && arg_runtime_scope == RUNTIME_SCOPE_USER) |
b63aacaa LP |
4212 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
4213 | "Combination of --user and --root= is not supported."); | |
4214 | ||
63d3d0a5 LP |
4215 | if (arg_image && arg_root) |
4216 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported."); | |
4217 | ||
3b63d2d3 LP |
4218 | return 1; |
4219 | } | |
4220 | ||
1d77721f | 4221 | static int read_config_file( |
a5e4f3ab | 4222 | Context *c, |
1d77721f LP |
4223 | char **config_dirs, |
4224 | const char *fn, | |
4225 | bool ignore_enoent, | |
4226 | bool *invalid_config) { | |
4227 | ||
f7ac1ed2 | 4228 | _cleanup_fclose_ FILE *_f = NULL; |
2708160c | 4229 | _cleanup_free_ char *pp = NULL; |
1731e34a | 4230 | unsigned v = 0; |
2a98ae4a | 4231 | FILE *f; |
bec890e3 | 4232 | ItemArray *ia; |
4e68ec18 | 4233 | int r = 0; |
fba6e687 | 4234 | |
a5e4f3ab | 4235 | assert(c); |
fba6e687 LP |
4236 | assert(fn); |
4237 | ||
f7ac1ed2 | 4238 | if (streq(fn, "-")) { |
28e5e1e9 | 4239 | log_debug("Reading config from stdin%s", special_glyph(SPECIAL_GLYPH_ELLIPSIS)); |
f7ac1ed2 ZJS |
4240 | fn = "<stdin>"; |
4241 | f = stdin; | |
4242 | } else { | |
2708160c | 4243 | r = search_and_fopen(fn, "re", arg_root, (const char**) config_dirs, &_f, &pp); |
f7ac1ed2 ZJS |
4244 | if (r < 0) { |
4245 | if (ignore_enoent && r == -ENOENT) { | |
4246 | log_debug_errno(r, "Failed to open \"%s\", ignoring: %m", fn); | |
4247 | return 0; | |
4248 | } | |
fba6e687 | 4249 | |
f7ac1ed2 ZJS |
4250 | return log_error_errno(r, "Failed to open '%s': %m", fn); |
4251 | } | |
2708160c | 4252 | |
28e5e1e9 | 4253 | log_debug("Reading config file \"%s\"%s", pp, special_glyph(SPECIAL_GLYPH_ELLIPSIS)); |
2708160c | 4254 | fn = pp; |
f7ac1ed2 | 4255 | f = _f; |
fba6e687 LP |
4256 | } |
4257 | ||
2a98ae4a LP |
4258 | for (;;) { |
4259 | _cleanup_free_ char *line = NULL; | |
4260 | bool invalid_line = false; | |
fba6e687 | 4261 | int k; |
2a98ae4a | 4262 | |
0ff6ff2b | 4263 | k = read_stripped_line(f, LONG_LINE_MAX, &line); |
2a98ae4a LP |
4264 | if (k < 0) |
4265 | return log_error_errno(k, "Failed to read '%s': %m", fn); | |
4266 | if (k == 0) | |
4267 | break; | |
fba6e687 | 4268 | |
fba6e687 LP |
4269 | v++; |
4270 | ||
0ff6ff2b | 4271 | if (IN_SET(line[0], 0, '#')) |
fba6e687 LP |
4272 | continue; |
4273 | ||
376d0495 | 4274 | k = parse_line(c, fn, v, line, &invalid_line); |
d9daae55 ZJS |
4275 | if (k < 0) { |
4276 | if (invalid_line) | |
4277 | /* Allow reporting with a special code if the caller requested this */ | |
4278 | *invalid_config = true; | |
4279 | else if (r == 0) | |
4280 | /* The first error becomes our return value */ | |
4281 | r = k; | |
4282 | } | |
fba6e687 LP |
4283 | } |
4284 | ||
78a92a5a | 4285 | /* we have to determine age parameter for each entry of type X */ |
a5e4f3ab | 4286 | ORDERED_HASHMAP_FOREACH(ia, c->globs) |
bec890e3 MM |
4287 | for (size_t ni = 0; ni < ia->n_items; ni++) { |
4288 | ItemArray *ja; | |
4289 | Item *i = ia->items + ni, *candidate_item = NULL; | |
78a92a5a | 4290 | |
bec890e3 | 4291 | if (i->type != IGNORE_DIRECTORY_PATH) |
78a92a5a MS |
4292 | continue; |
4293 | ||
a5e4f3ab | 4294 | ORDERED_HASHMAP_FOREACH(ja, c->items) |
bec890e3 MM |
4295 | for (size_t nj = 0; nj < ja->n_items; nj++) { |
4296 | Item *j = ja->items + nj; | |
78a92a5a | 4297 | |
875e7b25 ZJS |
4298 | if (!IN_SET(j->type, CREATE_DIRECTORY, |
4299 | TRUNCATE_DIRECTORY, | |
4300 | CREATE_SUBVOLUME, | |
4301 | CREATE_SUBVOLUME_INHERIT_QUOTA, | |
4302 | CREATE_SUBVOLUME_NEW_QUOTA)) | |
bec890e3 | 4303 | continue; |
78a92a5a | 4304 | |
bec890e3 MM |
4305 | if (path_equal(j->path, i->path)) { |
4306 | candidate_item = j; | |
4307 | break; | |
4308 | } | |
4309 | ||
875e7b25 ZJS |
4310 | if (candidate_item |
4311 | ? (path_startswith(j->path, candidate_item->path) && fnmatch(i->path, j->path, FNM_PATHNAME | FNM_PERIOD) == 0) | |
4312 | : path_startswith(i->path, j->path) != NULL) | |
bec890e3 MM |
4313 | candidate_item = j; |
4314 | } | |
4315 | ||
4316 | if (candidate_item && candidate_item->age_set) { | |
4317 | i->age = candidate_item->age; | |
4318 | i->age_set = true; | |
4319 | } | |
78a92a5a | 4320 | } |
78a92a5a | 4321 | |
fba6e687 | 4322 | if (ferror(f)) { |
56f64d95 | 4323 | log_error_errno(errno, "Failed to read from file %s: %m", fn); |
fba6e687 LP |
4324 | if (r == 0) |
4325 | r = -EIO; | |
4326 | } | |
4327 | ||
fba6e687 LP |
4328 | return r; |
4329 | } | |
4330 | ||
a5e4f3ab LP |
4331 | static int parse_arguments( |
4332 | Context *c, | |
4333 | char **config_dirs, | |
4334 | char **args, | |
4335 | bool *invalid_config) { | |
a6d8474f ZJS |
4336 | int r; |
4337 | ||
a5e4f3ab LP |
4338 | assert(c); |
4339 | ||
a6d8474f | 4340 | STRV_FOREACH(arg, args) { |
a5e4f3ab | 4341 | r = read_config_file(c, config_dirs, *arg, false, invalid_config); |
a6d8474f ZJS |
4342 | if (r < 0) |
4343 | return r; | |
4344 | } | |
4345 | ||
4346 | return 0; | |
4347 | } | |
4348 | ||
a5e4f3ab LP |
4349 | static int read_config_files( |
4350 | Context *c, | |
4351 | char **config_dirs, | |
4352 | char **args, | |
4353 | bool *invalid_config) { | |
4354 | ||
a6d8474f ZJS |
4355 | _cleanup_strv_free_ char **files = NULL; |
4356 | _cleanup_free_ char *p = NULL; | |
a6d8474f ZJS |
4357 | int r; |
4358 | ||
a5e4f3ab LP |
4359 | assert(c); |
4360 | ||
ceaaeb9b | 4361 | r = conf_files_list_with_replacement(arg_root, config_dirs, arg_replace, &files, &p); |
a6d8474f | 4362 | if (r < 0) |
ceaaeb9b | 4363 | return r; |
a6d8474f ZJS |
4364 | |
4365 | STRV_FOREACH(f, files) | |
4366 | if (p && path_equal(*f, p)) { | |
28e5e1e9 | 4367 | log_debug("Parsing arguments at position \"%s\"%s", *f, special_glyph(SPECIAL_GLYPH_ELLIPSIS)); |
a6d8474f | 4368 | |
a5e4f3ab | 4369 | r = parse_arguments(c, config_dirs, args, invalid_config); |
a6d8474f ZJS |
4370 | if (r < 0) |
4371 | return r; | |
4372 | } else | |
4373 | /* Just warn, ignore result otherwise. | |
4374 | * read_config_file() has some debug output, so no need to print anything. */ | |
a5e4f3ab | 4375 | (void) read_config_file(c, config_dirs, *f, true, invalid_config); |
a6d8474f ZJS |
4376 | |
4377 | return 0; | |
4378 | } | |
4379 | ||
a5e4f3ab | 4380 | static int read_credential_lines(Context *c, bool *invalid_config) { |
1d77721f LP |
4381 | _cleanup_free_ char *j = NULL; |
4382 | const char *d; | |
4383 | int r; | |
4384 | ||
a5e4f3ab LP |
4385 | assert(c); |
4386 | ||
1d77721f LP |
4387 | r = get_credentials_dir(&d); |
4388 | if (r == -ENXIO) | |
4389 | return 0; | |
4390 | if (r < 0) | |
4391 | return log_error_errno(r, "Failed to get credentials directory: %m"); | |
4392 | ||
4393 | j = path_join(d, "tmpfiles.extra"); | |
4394 | if (!j) | |
4395 | return log_oom(); | |
4396 | ||
a5e4f3ab | 4397 | (void) read_config_file(c, /* config_dirs= */ NULL, j, /* ignore_enoent= */ true, invalid_config); |
1d77721f LP |
4398 | return 0; |
4399 | } | |
4400 | ||
a5e4f3ab | 4401 | static int link_parent(Context *c, ItemArray *a) { |
811a1587 LP |
4402 | const char *path; |
4403 | char *prefix; | |
4404 | int r; | |
4405 | ||
a5e4f3ab | 4406 | assert(c); |
811a1587 LP |
4407 | assert(a); |
4408 | ||
09f467ac | 4409 | /* Finds the closest "parent" item array for the specified item array. Then registers the specified item array |
811a1587 LP |
4410 | * as child of it, and fills the parent in, linking them both ways. This allows us to later create parents |
4411 | * before their children, and clean up/remove children before their parents. */ | |
4412 | ||
4413 | if (a->n_items <= 0) | |
4414 | return 0; | |
4415 | ||
4416 | path = a->items[0].path; | |
6e9417f5 | 4417 | prefix = newa(char, strlen(path) + 1); |
811a1587 LP |
4418 | PATH_FOREACH_PREFIX(prefix, path) { |
4419 | ItemArray *j; | |
4420 | ||
a5e4f3ab | 4421 | j = ordered_hashmap_get(c->items, prefix); |
bd0ce244 | 4422 | if (!j) |
a5e4f3ab | 4423 | j = ordered_hashmap_get(c->globs, prefix); |
811a1587 | 4424 | if (j) { |
de7fef4b | 4425 | r = set_ensure_put(&j->children, NULL, a); |
811a1587 LP |
4426 | if (r < 0) |
4427 | return log_oom(); | |
4428 | ||
4429 | a->parent = j; | |
4430 | return 1; | |
4431 | } | |
4432 | } | |
4433 | ||
4434 | return 0; | |
4435 | } | |
4436 | ||
87938c3b YW |
4437 | DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(item_array_hash_ops, char, string_hash_func, string_compare_func, |
4438 | ItemArray, item_array_free); | |
4439 | ||
4440 | static int run(int argc, char *argv[]) { | |
3537577c | 4441 | #ifndef STANDALONE |
63d3d0a5 | 4442 | _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL; |
a4b3e942 | 4443 | _cleanup_(umount_and_freep) char *mounted_dir = NULL; |
3537577c | 4444 | #endif |
f2b5ca0e | 4445 | _cleanup_strv_free_ char **config_dirs = NULL; |
a5e4f3ab | 4446 | _cleanup_(context_done) Context c = {}; |
d9daae55 | 4447 | bool invalid_config = false; |
64adb379 | 4448 | ItemArray *a; |
44ac4f88 | 4449 | enum { |
81a18380 | 4450 | PHASE_PURGE, |
44ac4f88 LP |
4451 | PHASE_REMOVE_AND_CLEAN, |
4452 | PHASE_CREATE, | |
4453 | _PHASE_MAX | |
4454 | } phase; | |
87938c3b | 4455 | int r, k; |
3b63d2d3 | 4456 | |
fdcad0c2 LP |
4457 | r = parse_argv(argc, argv); |
4458 | if (r <= 0) | |
87938c3b | 4459 | return r; |
5008d581 | 4460 | |
d2acb93d | 4461 | log_setup(); |
5008d581 | 4462 | |
01131684 LP |
4463 | /* We require /proc/ for a lot of our operations, i.e. for adjusting access modes, for anything |
4464 | * SELinux related, for recursive operation, for xattr, acl and chattr handling, for btrfs stuff and | |
4465 | * a lot more. It's probably the majority of invocations where /proc/ is required. Since people | |
4466 | * apparently invoke it without anyway and are surprised about the failures, let's catch this early | |
4467 | * and output a nice and friendly warning. */ | |
4468 | if (proc_mounted() == 0) | |
4469 | return log_error_errno(SYNTHETIC_ERRNO(ENOSYS), | |
4470 | "/proc/ is not mounted, but required for successful operation of systemd-tmpfiles. " | |
4471 | "Please mount /proc/. Alternatively, consider using the --root= or --image= switches."); | |
4472 | ||
e5358401 LP |
4473 | /* Descending down file system trees might take a lot of fds */ |
4474 | (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE); | |
4475 | ||
4870133b LP |
4476 | switch (arg_runtime_scope) { |
4477 | ||
4478 | case RUNTIME_SCOPE_USER: | |
f2b5ca0e | 4479 | r = user_config_paths(&config_dirs); |
87938c3b YW |
4480 | if (r < 0) |
4481 | return log_error_errno(r, "Failed to initialize configuration directory list: %m"); | |
4870133b LP |
4482 | break; |
4483 | ||
4484 | case RUNTIME_SCOPE_SYSTEM: | |
f2b5ca0e | 4485 | config_dirs = strv_split_nulstr(CONF_PATHS_NULSTR("tmpfiles.d")); |
87938c3b YW |
4486 | if (!config_dirs) |
4487 | return log_oom(); | |
4870133b LP |
4488 | break; |
4489 | ||
4490 | default: | |
4491 | assert_not_reached(); | |
f2b5ca0e ZJS |
4492 | } |
4493 | ||
79c91cec | 4494 | if (DEBUG_LOGGING) { |
f2b5ca0e ZJS |
4495 | _cleanup_free_ char *t = NULL; |
4496 | ||
b8e35011 LP |
4497 | STRV_FOREACH(i, config_dirs) { |
4498 | _cleanup_free_ char *j = NULL; | |
4499 | ||
4500 | j = path_join(arg_root, *i); | |
4501 | if (!j) | |
4502 | return log_oom(); | |
4503 | ||
c2bc710b | 4504 | if (!strextend(&t, "\n\t", j)) |
b8e35011 LP |
4505 | return log_oom(); |
4506 | } | |
4507 | ||
4508 | log_debug("Looking for configuration files in (higher priority first):%s", t); | |
f2b5ca0e ZJS |
4509 | } |
4510 | ||
1be6a5db | 4511 | if (arg_cat_flags != CAT_CONFIG_OFF) |
87938c3b | 4512 | return cat_config(config_dirs, argv + optind); |
ceaaeb9b ZJS |
4513 | |
4514 | umask(0022); | |
4515 | ||
a452c807 | 4516 | r = mac_init(); |
a9ba0e32 CG |
4517 | if (r < 0) |
4518 | return r; | |
ceaaeb9b | 4519 | |
3537577c | 4520 | #ifndef STANDALONE |
63d3d0a5 LP |
4521 | if (arg_image) { |
4522 | assert(!arg_root); | |
4523 | ||
4524 | r = mount_image_privately_interactively( | |
4525 | arg_image, | |
84be0c71 | 4526 | arg_image_policy, |
4b5de5dd LP |
4527 | DISSECT_IMAGE_GENERIC_ROOT | |
4528 | DISSECT_IMAGE_REQUIRE_ROOT | | |
4529 | DISSECT_IMAGE_VALIDATE_OS | | |
4530 | DISSECT_IMAGE_RELAX_VAR_CHECK | | |
c65f854a LP |
4531 | DISSECT_IMAGE_FSCK | |
4532 | DISSECT_IMAGE_GROWFS, | |
a4b3e942 | 4533 | &mounted_dir, |
a133d2c3 | 4534 | /* ret_dir_fd= */ NULL, |
e330f97a | 4535 | &loop_device); |
63d3d0a5 LP |
4536 | if (r < 0) |
4537 | return r; | |
4538 | ||
a4b3e942 | 4539 | arg_root = strdup(mounted_dir); |
63d3d0a5 LP |
4540 | if (!arg_root) |
4541 | return log_oom(); | |
4542 | } | |
3537577c ZJS |
4543 | #else |
4544 | assert(!arg_image); | |
4545 | #endif | |
63d3d0a5 | 4546 | |
a5e4f3ab LP |
4547 | c.items = ordered_hashmap_new(&item_array_hash_ops); |
4548 | c.globs = ordered_hashmap_new(&item_array_hash_ops); | |
4549 | if (!c.items || !c.globs) | |
87938c3b | 4550 | return log_oom(); |
ceaaeb9b | 4551 | |
a6d8474f ZJS |
4552 | /* If command line arguments are specified along with --replace, read all |
4553 | * configuration files and insert the positional arguments at the specified | |
4554 | * place. Otherwise, if command line arguments are specified, execute just | |
4555 | * them, and finally, without --replace= or any positional arguments, just | |
4556 | * read configuration and execute it. | |
4557 | */ | |
4558 | if (arg_replace || optind >= argc) | |
a5e4f3ab | 4559 | r = read_config_files(&c, config_dirs, argv + optind, &invalid_config); |
a6d8474f | 4560 | else |
a5e4f3ab | 4561 | r = parse_arguments(&c, config_dirs, argv + optind, &invalid_config); |
a6d8474f | 4562 | if (r < 0) |
87938c3b | 4563 | return r; |
5008d581 | 4564 | |
a5e4f3ab | 4565 | r = read_credential_lines(&c, &invalid_config); |
1d77721f LP |
4566 | if (r < 0) |
4567 | return r; | |
4568 | ||
811a1587 | 4569 | /* Let's now link up all child/parent relationships */ |
a5e4f3ab LP |
4570 | ORDERED_HASHMAP_FOREACH(a, c.items) { |
4571 | r = link_parent(&c, a); | |
811a1587 | 4572 | if (r < 0) |
87938c3b | 4573 | return r; |
1db50423 | 4574 | } |
a5e4f3ab LP |
4575 | ORDERED_HASHMAP_FOREACH(a, c.globs) { |
4576 | r = link_parent(&c, a); | |
811a1587 | 4577 | if (r < 0) |
87938c3b | 4578 | return r; |
811a1587 LP |
4579 | } |
4580 | ||
64adb379 LP |
4581 | /* If multiple operations are requested, let's first run the remove/clean operations, and only then the create |
4582 | * operations. i.e. that we first clean out the platform we then build on. */ | |
44ac4f88 | 4583 | for (phase = 0; phase < _PHASE_MAX; phase++) { |
64adb379 | 4584 | OperationMask op; |
b8bb3e8f | 4585 | |
81a18380 LB |
4586 | if (phase == PHASE_PURGE) |
4587 | op = arg_operation & OPERATION_PURGE; | |
4588 | else if (phase == PHASE_REMOVE_AND_CLEAN) | |
64adb379 | 4589 | op = arg_operation & (OPERATION_REMOVE|OPERATION_CLEAN); |
44ac4f88 | 4590 | else if (phase == PHASE_CREATE) |
64adb379 LP |
4591 | op = arg_operation & OPERATION_CREATE; |
4592 | else | |
04499a70 | 4593 | assert_not_reached(); |
64adb379 LP |
4594 | |
4595 | if (op == 0) /* Nothing requested in this phase */ | |
4596 | continue; | |
4597 | ||
4598 | /* The non-globbing ones usually create things, hence we apply them first */ | |
a5e4f3ab LP |
4599 | ORDERED_HASHMAP_FOREACH(a, c.items) { |
4600 | k = process_item_array(&c, a, op); | |
87938c3b YW |
4601 | if (k < 0 && r >= 0) |
4602 | r = k; | |
64adb379 LP |
4603 | } |
4604 | ||
4605 | /* The globbing ones usually alter things, hence we apply them second. */ | |
a5e4f3ab LP |
4606 | ORDERED_HASHMAP_FOREACH(a, c.globs) { |
4607 | k = process_item_array(&c, a, op); | |
87938c3b YW |
4608 | if (k < 0 && r >= 0) |
4609 | r = k; | |
64adb379 | 4610 | } |
1db50423 | 4611 | } |
3b63d2d3 | 4612 | |
95d2155a | 4613 | if (ERRNO_IS_RESOURCE(r)) |
87938c3b YW |
4614 | return r; |
4615 | if (invalid_config) | |
d9daae55 | 4616 | return EX_DATAERR; |
87938c3b | 4617 | if (r < 0) |
bb9947be | 4618 | return EX_CANTCREAT; |
87938c3b | 4619 | return 0; |
5008d581 | 4620 | } |
87938c3b YW |
4621 | |
4622 | DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); |