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