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