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