blob: 1b3cb5fbd3780bcfb0354a5eccc60682bc1b3789 [file] [log] [blame]
Zbigniew Jędrzejewski-Szmek53e1b682017-11-18 17:09:20 +01001/* SPDX-License-Identifier: LGPL-2.1+ */
Lennart Poettering5008d582010-09-28 02:34:02 +02002
Lennart Poettering3f6fd1b2015-09-23 03:01:06 +02003#include <errno.h>
4#include <fcntl.h>
5#include <fnmatch.h>
6#include <getopt.h>
7#include <glob.h>
8#include <limits.h>
9#include <linux/fs.h>
10#include <stdbool.h>
11#include <stddef.h>
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020012#include <stdio.h>
13#include <stdlib.h>
Lennart Poettering3f6fd1b2015-09-23 03:01:06 +020014#include <string.h>
Zbigniew Jędrzejewski-Szmek505ef0e2015-01-09 01:10:02 -050015#include <sys/stat.h>
Maciej Wereskiebf4e802014-12-04 10:32:10 +010016#include <sys/xattr.h>
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +010017#include <sysexits.h>
Lennart Poettering3f6fd1b2015-09-23 03:01:06 +020018#include <time.h>
19#include <unistd.h>
Lennart Poettering5008d582010-09-28 02:34:02 +020020
Zbigniew Jędrzejewski-Szmek5a8575e2017-11-23 13:56:32 +010021#include "sd-path.h"
22
Lennart Poettering3f6fd1b2015-09-23 03:01:06 +020023#include "acl-util.h"
Lennart Poetteringb5efdb82015-10-27 03:01:06 +010024#include "alloc-util.h"
Lennart Poettering3f6fd1b2015-09-23 03:01:06 +020025#include "btrfs-util.h"
Lennart Poettering430f0182015-10-26 23:32:16 +010026#include "capability-util.h"
Lennart Poetteringc8b30942015-10-26 20:39:23 +010027#include "chattr-util.h"
Lennart Poettering3f6fd1b2015-09-23 03:01:06 +020028#include "conf-files.h"
29#include "copy.h"
Lennart Poetteringa0f29c72015-11-03 12:26:12 +010030#include "def.h"
Reverend Homer8fb3f002016-12-09 12:04:30 +030031#include "dirent-util.h"
Lennart Poettering4f5dd392015-10-23 18:52:53 +020032#include "escape.h"
Lennart Poettering3ffd4af2015-10-25 13:14:12 +010033#include "fd-util.h"
Lennart Poettering0d39fa92015-10-26 18:05:03 +010034#include "fileio.h"
Zbigniew Jędrzejewski-Szmekf97b34a2016-11-07 10:14:59 -050035#include "format-util.h"
Lennart Poetteringf4f15632015-10-26 21:16:26 +010036#include "fs-util.h"
Lennart Poettering7d50b322015-10-27 01:48:17 +010037#include "glob-util.h"
Lennart Poetteringc0044932015-10-25 14:08:25 +010038#include "io-util.h"
Lennart Poettering3f6fd1b2015-09-23 03:01:06 +020039#include "label.h"
Lennart Poettering5008d582010-09-28 02:34:02 +020040#include "log.h"
Dave Reisner54693d92012-09-15 12:58:49 -040041#include "macro.h"
Zbigniew Jędrzejewski-Szmekd39efe72013-03-13 20:20:54 -040042#include "missing.h"
Kay Sievers49e942b2012-04-10 21:54:31 +020043#include "mkdir.h"
Lennart Poettering4e036b72015-10-27 13:45:00 +010044#include "mount-util.h"
Lennart Poetteringdcd5c892018-06-12 15:37:53 +020045#include "pager.h"
Lennart Poettering6bedfcb2015-10-26 16:18:16 +010046#include "parse-util.h"
Zbigniew Jędrzejewski-Szmekf2b5ca02017-11-23 11:20:29 +010047#include "path-lookup.h"
Kay Sievers9eb977d2012-05-07 21:36:12 +020048#include "path-util.h"
Lennart Poetteringc6878632015-04-04 11:52:57 +020049#include "rm-rf.h"
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +010050#include "selinux-util.h"
Lennart Poettering3f6fd1b2015-09-23 03:01:06 +020051#include "set.h"
52#include "specifier.h"
Lennart Poettering8fcde012015-10-26 22:01:44 +010053#include "stat-util.h"
Lennart Poettering15a5e952015-10-27 01:26:31 +010054#include "stdio-util.h"
Lennart Poettering8b434402015-10-26 22:31:05 +010055#include "string-table.h"
Lennart Poettering07630ce2015-10-24 22:58:24 +020056#include "string-util.h"
Lennart Poettering3f6fd1b2015-09-23 03:01:06 +020057#include "strv.h"
Zbigniew Jędrzejewski-Szmekceaaeb92018-04-26 19:07:54 +020058#include "terminal-util.h"
Lennart Poetteringaffb60b2015-10-26 23:20:41 +010059#include "umask-util.h"
Lennart Poetteringb1d4f8e2015-10-25 22:32:30 +010060#include "user-util.h"
Lennart Poettering3f6fd1b2015-09-23 03:01:06 +020061#include "util.h"
Lennart Poettering5008d582010-09-28 02:34:02 +020062
Andreas Jaeger01000472010-09-29 10:08:24 +020063/* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates
Lennart Poettering5008d582010-09-28 02:34:02 +020064 * them in the file system. This is intended to be used to create
Kay Sieversdb019b82011-04-04 15:33:00 +020065 * properly owned directories beneath /tmp, /var/tmp, /run, which are
66 * volatile and hence need to be recreated on bootup. */
Lennart Poettering5008d582010-09-28 02:34:02 +020067
Michal Schmidt66ccd032011-12-15 21:31:14 +010068typedef enum ItemType {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010069 /* These ones take file names */
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020070 CREATE_FILE = 'f',
71 TRUNCATE_FILE = 'F',
72 CREATE_DIRECTORY = 'd',
73 TRUNCATE_DIRECTORY = 'D',
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +010074 CREATE_SUBVOLUME = 'v',
Lennart Poettering5fb13eb2015-10-21 19:46:23 +020075 CREATE_SUBVOLUME_INHERIT_QUOTA = 'q',
76 CREATE_SUBVOLUME_NEW_QUOTA = 'Q',
Lennart Poetteringee17ee72011-07-12 03:56:56 +020077 CREATE_FIFO = 'p',
Lennart Poettering468d7262012-01-17 15:04:12 +010078 CREATE_SYMLINK = 'L',
79 CREATE_CHAR_DEVICE = 'c',
80 CREATE_BLOCK_DEVICE = 'b',
Lennart Poettering849958d2014-06-10 23:02:40 +020081 COPY_FILES = 'C',
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010082
83 /* These ones take globs */
Lennart Poettering17493fa2015-04-10 16:22:22 +020084 WRITE_FILE = 'w',
Zbigniew Jędrzejewski-Szmekdf8dee82016-04-20 00:06:25 -040085 EMPTY_DIRECTORY = 'e',
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -050086 SET_XATTR = 't',
87 RECURSIVE_SET_XATTR = 'T',
88 SET_ACL = 'a',
89 RECURSIVE_SET_ACL = 'A',
Lennart Poettering17493fa2015-04-10 16:22:22 +020090 SET_ATTRIBUTE = 'h',
91 RECURSIVE_SET_ATTRIBUTE = 'H',
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020092 IGNORE_PATH = 'x',
Michal Sekletar78a92a52013-01-18 16:13:08 +010093 IGNORE_DIRECTORY_PATH = 'X',
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020094 REMOVE_PATH = 'r',
Michal Schmidta8d88782011-12-15 23:11:07 +010095 RECURSIVE_REMOVE_PATH = 'R',
Michal Schmidt777b87e2011-12-16 18:27:35 +010096 RELABEL_PATH = 'z',
Lennart Poetteringe73a03e2014-06-10 23:42:16 +020097 RECURSIVE_RELABEL_PATH = 'Z',
Lennart Poettering17493fa2015-04-10 16:22:22 +020098 ADJUST_MODE = 'm', /* legacy, 'z' is identical to this */
Michal Schmidt66ccd032011-12-15 21:31:14 +010099} ItemType;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200100
101typedef struct Item {
Michal Schmidt66ccd032011-12-15 21:31:14 +0100102 ItemType type;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200103
104 char *path;
Lennart Poettering468d7262012-01-17 15:04:12 +0100105 char *argument;
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100106 char **xattrs;
Zbigniew Jędrzejewski-Szmek349cc4a2017-10-03 10:41:51 +0200107#if HAVE_ACL
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -0500108 acl_t acl_access;
109 acl_t acl_default;
110#endif
Lennart Poettering5008d582010-09-28 02:34:02 +0200111 uid_t uid;
112 gid_t gid;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200113 mode_t mode;
114 usec_t age;
115
Lennart Poettering468d7262012-01-17 15:04:12 +0100116 dev_t major_minor;
Lennart Poettering88ec4df2015-04-08 22:35:52 +0200117 unsigned attribute_value;
118 unsigned attribute_mask;
Lennart Poettering468d7262012-01-17 15:04:12 +0100119
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200120 bool uid_set:1;
121 bool gid_set:1;
122 bool mode_set:1;
123 bool age_set:1;
Lennart Poetteringabef3f92014-06-11 10:14:07 +0200124 bool mask_perms:1;
Lennart Poettering88ec4df2015-04-08 22:35:52 +0200125 bool attribute_set:1;
Lennart Poettering24f3a372012-06-20 09:05:50 +0200126
127 bool keep_first_level:1;
Lennart Poettering1910cd02014-06-11 01:37:35 +0200128
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200129 bool force:1;
130
William Douglas6d7b5432018-09-10 12:07:29 -0700131 bool allow_failure:1;
132
Lennart Poettering1910cd02014-06-11 01:37:35 +0200133 bool done:1;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200134} Item;
135
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -0500136typedef struct ItemArray {
137 Item *items;
138 size_t count;
139 size_t size;
140} ItemArray;
141
Zbigniew Jędrzejewski-Szmek5a8575e2017-11-23 13:56:32 +0100142typedef enum DirectoryType {
143 DIRECTORY_RUNTIME = 0,
144 DIRECTORY_STATE,
145 DIRECTORY_CACHE,
146 DIRECTORY_LOGS,
147 _DIRECTORY_TYPE_MAX,
148} DirectoryType;
149
Zbigniew Jędrzejewski-Szmekceaaeb92018-04-26 19:07:54 +0200150static bool arg_cat_config = false;
Zbigniew Jędrzejewski-Szmekf2b5ca02017-11-23 11:20:29 +0100151static bool arg_user = false;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200152static bool arg_create = false;
153static bool arg_clean = false;
154static bool arg_remove = false;
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -0500155static bool arg_boot = false;
Lennart Poetteringdcd5c892018-06-12 15:37:53 +0200156static bool arg_no_pager = false;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200157
Lennart Poettering7bc040f2014-06-11 01:26:28 +0200158static char **arg_include_prefixes = NULL;
159static char **arg_exclude_prefixes = NULL;
Michael Marineaucf9a4ab2014-03-13 21:32:13 -0700160static char *arg_root = NULL;
Zbigniew Jędrzejewski-Szmeka6d84742018-02-05 14:53:11 +0100161static char *arg_replace = NULL;
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100162
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200163#define MAX_DEPTH 256
164
Lennart Poetteringef43a392015-04-22 18:18:56 +0200165static OrderedHashmap *items = NULL, *globs = NULL;
Lennart Poettering7bc040f2014-06-11 01:26:28 +0200166static Set *unix_sockets = NULL;
167
Franck Bui4cef1922017-11-16 11:27:29 +0100168static int specifier_machine_id_safe(char specifier, void *data, void *userdata, char **ret);
Zbigniew Jędrzejewski-Szmek5a8575e2017-11-23 13:56:32 +0100169static int specifier_directory(char specifier, void *data, void *userdata, char **ret);
Franck Bui4cef1922017-11-16 11:27:29 +0100170
Lennart Poetteringbd550f72015-04-10 16:03:24 +0200171static const Specifier specifier_table[] = {
Franck Bui4cef1922017-11-16 11:27:29 +0100172 { 'm', specifier_machine_id_safe, NULL },
Zbigniew Jędrzejewski-Szmek5a8575e2017-11-23 13:56:32 +0100173 { 'b', specifier_boot_id, NULL },
174 { 'H', specifier_host_name, NULL },
175 { 'v', specifier_kernel_release, NULL },
ayekatca23eeb2017-11-24 12:44:08 +0100176
Zbigniew Jędrzejewski-Szmek5a8575e2017-11-23 13:56:32 +0100177 { 'U', specifier_user_id, NULL },
178 { 'u', specifier_user_name, NULL },
179 { 'h', specifier_user_home, NULL },
Lennart Poetteringb294e592018-05-29 11:25:26 +0200180
Zbigniew Jędrzejewski-Szmek5a8575e2017-11-23 13:56:32 +0100181 { 't', specifier_directory, UINT_TO_PTR(DIRECTORY_RUNTIME) },
182 { 'S', specifier_directory, UINT_TO_PTR(DIRECTORY_STATE) },
183 { 'C', specifier_directory, UINT_TO_PTR(DIRECTORY_CACHE) },
184 { 'L', specifier_directory, UINT_TO_PTR(DIRECTORY_LOGS) },
Lennart Poetteringb294e592018-05-29 11:25:26 +0200185 { 'T', specifier_tmp_dir, NULL },
186 { 'V', specifier_var_tmp_dir, NULL },
Lennart Poetteringbd550f72015-04-10 16:03:24 +0200187 {}
188};
189
Franck Bui4cef1922017-11-16 11:27:29 +0100190static int specifier_machine_id_safe(char specifier, void *data, void *userdata, char **ret) {
191 int r;
192
Franck Buid8dab752018-01-10 23:28:44 +0100193 /* If /etc/machine_id is missing or empty (e.g. in a chroot environment)
194 * return a recognizable error so that the caller can skip the rule
Franck Bui4cef1922017-11-16 11:27:29 +0100195 * gracefully. */
196
197 r = specifier_machine_id(specifier, data, userdata, ret);
Franck Buid8dab752018-01-10 23:28:44 +0100198 if (IN_SET(r, -ENOENT, -ENOMEDIUM))
Zbigniew Jędrzejewski-Szmek5a8575e2017-11-23 13:56:32 +0100199 return -ENXIO;
Franck Bui4cef1922017-11-16 11:27:29 +0100200
201 return r;
202}
203
Zbigniew Jędrzejewski-Szmek5a8575e2017-11-23 13:56:32 +0100204static int specifier_directory(char specifier, void *data, void *userdata, char **ret) {
205 struct table_entry {
206 uint64_t type;
207 const char *suffix;
208 };
209
210 static const struct table_entry paths_system[] = {
211 [DIRECTORY_RUNTIME] = { SD_PATH_SYSTEM_RUNTIME },
212 [DIRECTORY_STATE] = { SD_PATH_SYSTEM_STATE_PRIVATE },
213 [DIRECTORY_CACHE] = { SD_PATH_SYSTEM_STATE_CACHE },
214 [DIRECTORY_LOGS] = { SD_PATH_SYSTEM_STATE_LOGS },
215 };
216
217 static const struct table_entry paths_user[] = {
218 [DIRECTORY_RUNTIME] = { SD_PATH_USER_RUNTIME },
219 [DIRECTORY_STATE] = { SD_PATH_USER_CONFIGURATION },
220 [DIRECTORY_CACHE] = { SD_PATH_USER_STATE_CACHE },
221 [DIRECTORY_LOGS] = { SD_PATH_USER_CONFIGURATION, "log" },
222 };
223
224 unsigned i;
225 const struct table_entry *paths;
226
227 assert_cc(ELEMENTSOF(paths_system) == ELEMENTSOF(paths_user));
228 paths = arg_user ? paths_user : paths_system;
229
230 i = PTR_TO_UINT(data);
231 assert(i < ELEMENTSOF(paths_system));
232
233 return sd_path_home(paths[i].type, paths[i].suffix, ret);
234}
235
Franck Bui4cef1922017-11-16 11:27:29 +0100236static int log_unresolvable_specifier(const char *filename, unsigned line) {
237 static bool notified = false;
238
Zbigniew Jędrzejewski-Szmek5a8575e2017-11-23 13:56:32 +0100239 /* In system mode, this is called when /etc is not fully initialized (e.g.
240 * in a chroot environment) where some specifiers are unresolvable. In user
241 * mode, this is called when some variables are not defined. These cases are
242 * not considered as an error so log at LOG_NOTICE only for the first time
243 * and then downgrade this to LOG_DEBUG for the rest. */
Franck Bui4cef1922017-11-16 11:27:29 +0100244
245 log_full(notified ? LOG_DEBUG : LOG_NOTICE,
Zbigniew Jędrzejewski-Szmek5a8575e2017-11-23 13:56:32 +0100246 "[%s:%u] Failed to resolve specifier: %s, skipping",
247 filename, line,
248 arg_user ? "Required $XDG_... variable not defined" : "uninitialized /etc detected");
Franck Bui4cef1922017-11-16 11:27:29 +0100249
250 if (!notified)
251 log_notice("All rules containing unresolvable specifiers will be skipped.");
252
253 notified = true;
254 return 0;
255}
256
Zbigniew Jędrzejewski-Szmekf2b5ca02017-11-23 11:20:29 +0100257static int user_config_paths(char*** ret) {
258 _cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL;
259 _cleanup_free_ char *persistent_config = NULL, *runtime_config = NULL, *data_home = NULL;
260 _cleanup_strv_free_ char **res = NULL;
261 int r;
262
263 r = xdg_user_dirs(&config_dirs, &data_dirs);
264 if (r < 0)
265 return r;
266
267 r = xdg_user_config_dir(&persistent_config, "/user-tmpfiles.d");
268 if (r < 0 && r != -ENXIO)
269 return r;
270
271 r = xdg_user_runtime_dir(&runtime_config, "/user-tmpfiles.d");
272 if (r < 0 && r != -ENXIO)
273 return r;
274
275 r = xdg_user_data_dir(&data_home, "/user-tmpfiles.d");
276 if (r < 0 && r != -ENXIO)
277 return r;
278
279 r = strv_extend_strv_concat(&res, config_dirs, "/user-tmpfiles.d");
280 if (r < 0)
281 return r;
282
283 r = strv_extend(&res, persistent_config);
284 if (r < 0)
285 return r;
286
287 r = strv_extend(&res, runtime_config);
288 if (r < 0)
289 return r;
290
291 r = strv_extend(&res, data_home);
292 if (r < 0)
293 return r;
294
295 r = strv_extend_strv_concat(&res, data_dirs, "/user-tmpfiles.d");
296 if (r < 0)
297 return r;
298
299 r = path_strv_make_absolute_cwd(res);
300 if (r < 0)
301 return r;
302
Lennart Poetteringae2a15b2018-03-22 16:53:26 +0100303 *ret = TAKE_PTR(res);
Zbigniew Jędrzejewski-Szmekf2b5ca02017-11-23 11:20:29 +0100304 return 0;
305}
306
Michal Schmidt66ccd032011-12-15 21:31:14 +0100307static bool needs_glob(ItemType t) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200308 return IN_SET(t,
309 WRITE_FILE,
310 IGNORE_PATH,
311 IGNORE_DIRECTORY_PATH,
312 REMOVE_PATH,
313 RECURSIVE_REMOVE_PATH,
Zbigniew Jędrzejewski-Szmekdf8dee82016-04-20 00:06:25 -0400314 EMPTY_DIRECTORY,
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200315 ADJUST_MODE,
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200316 RELABEL_PATH,
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -0500317 RECURSIVE_RELABEL_PATH,
318 SET_XATTR,
319 RECURSIVE_SET_XATTR,
320 SET_ACL,
Lennart Poettering34f64532015-04-09 13:12:06 +0200321 RECURSIVE_SET_ACL,
322 SET_ATTRIBUTE,
323 RECURSIVE_SET_ATTRIBUTE);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100324}
325
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -0500326static bool takes_ownership(ItemType t) {
327 return IN_SET(t,
328 CREATE_FILE,
329 TRUNCATE_FILE,
330 CREATE_DIRECTORY,
Zbigniew Jędrzejewski-Szmekdf8dee82016-04-20 00:06:25 -0400331 EMPTY_DIRECTORY,
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -0500332 TRUNCATE_DIRECTORY,
333 CREATE_SUBVOLUME,
Lennart Poettering5fb13eb2015-10-21 19:46:23 +0200334 CREATE_SUBVOLUME_INHERIT_QUOTA,
335 CREATE_SUBVOLUME_NEW_QUOTA,
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -0500336 CREATE_FIFO,
337 CREATE_SYMLINK,
338 CREATE_CHAR_DEVICE,
339 CREATE_BLOCK_DEVICE,
340 COPY_FILES,
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -0500341 WRITE_FILE,
342 IGNORE_PATH,
343 IGNORE_DIRECTORY_PATH,
344 REMOVE_PATH,
345 RECURSIVE_REMOVE_PATH);
346}
347
Lennart Poetteringef43a392015-04-22 18:18:56 +0200348static struct Item* find_glob(OrderedHashmap *h, const char *match) {
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -0500349 ItemArray *j;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100350 Iterator i;
351
Lennart Poetteringef43a392015-04-22 18:18:56 +0200352 ORDERED_HASHMAP_FOREACH(j, h, i) {
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -0500353 unsigned n;
354
355 for (n = 0; n < j->count; n++) {
356 Item *item = j->items + n;
357
358 if (fnmatch(item->path, match, FNM_PATHNAME|FNM_PERIOD) == 0)
359 return item;
360 }
361 }
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100362
363 return NULL;
364}
365
Lennart Poettering17b90522011-02-14 21:55:06 +0100366static void load_unix_sockets(void) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200367 _cleanup_fclose_ FILE *f = NULL;
Lennart Poetteringf1ff7342018-01-10 17:28:03 +0100368 int r;
Lennart Poettering17b90522011-02-14 21:55:06 +0100369
370 if (unix_sockets)
371 return;
372
Lennart Poetteringf1ff7342018-01-10 17:28:03 +0100373 /* We maintain a cache of the sockets we found in /proc/net/unix to speed things up a little. */
Lennart Poettering17b90522011-02-14 21:55:06 +0100374
Lennart Poettering548f6932018-02-08 18:58:35 +0100375 unix_sockets = set_new(&path_hash_ops);
376 if (!unix_sockets) {
377 log_oom();
Lennart Poettering17b90522011-02-14 21:55:06 +0100378 return;
Lennart Poettering548f6932018-02-08 18:58:35 +0100379 }
Lennart Poettering17b90522011-02-14 21:55:06 +0100380
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100381 f = fopen("/proc/net/unix", "re");
Lennart Poetteringf1ff7342018-01-10 17:28:03 +0100382 if (!f) {
383 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno,
384 "Failed to open /proc/net/unix, ignoring: %m");
385 goto fail;
386 }
Lennart Poettering17b90522011-02-14 21:55:06 +0100387
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100388 /* Skip header */
Lennart Poetteringf1ff7342018-01-10 17:28:03 +0100389 r = read_line(f, LONG_LINE_MAX, NULL);
390 if (r < 0) {
391 log_warning_errno(r, "Failed to skip /proc/net/unix header line: %m");
Lennart Poettering17b90522011-02-14 21:55:06 +0100392 goto fail;
Lennart Poetteringf1ff7342018-01-10 17:28:03 +0100393 }
394 if (r == 0) {
395 log_warning("Premature end of file reading /proc/net/unix.");
396 goto fail;
397 }
Lennart Poettering17b90522011-02-14 21:55:06 +0100398
399 for (;;) {
Lennart Poetteringf1ff7342018-01-10 17:28:03 +0100400 _cleanup_free_ char *line = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +0100401 char *p, *s;
Lennart Poettering17b90522011-02-14 21:55:06 +0100402
Lennart Poetteringf1ff7342018-01-10 17:28:03 +0100403 r = read_line(f, LONG_LINE_MAX, &line);
404 if (r < 0) {
405 log_warning_errno(r, "Failed to read /proc/net/unix line, ignoring: %m");
406 goto fail;
407 }
408 if (r == 0) /* EOF */
Lennart Poettering17b90522011-02-14 21:55:06 +0100409 break;
410
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100411 p = strchr(line, ':');
412 if (!p)
Lennart Poettering17b90522011-02-14 21:55:06 +0100413 continue;
414
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100415 if (strlen(p) < 37)
416 continue;
417
418 p += 37;
Lennart Poettering17b90522011-02-14 21:55:06 +0100419 p += strspn(p, WHITESPACE);
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100420 p += strcspn(p, WHITESPACE); /* skip one more word */
Lennart Poettering17b90522011-02-14 21:55:06 +0100421 p += strspn(p, WHITESPACE);
422
423 if (*p != '/')
424 continue;
425
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100426 s = strdup(p);
Lennart Poetteringf1ff7342018-01-10 17:28:03 +0100427 if (!s) {
428 log_oom();
Lennart Poettering17b90522011-02-14 21:55:06 +0100429 goto fail;
Lennart Poetteringf1ff7342018-01-10 17:28:03 +0100430 }
Lennart Poettering17b90522011-02-14 21:55:06 +0100431
Yu Watanabe858d36c2018-05-31 23:39:31 +0900432 path_simplify(s, false);
Lennart Poettering4ff21d82011-02-17 13:13:34 +0100433
Lennart Poetteringf1ff7342018-01-10 17:28:03 +0100434 r = set_consume(unix_sockets, s);
435 if (r < 0 && r != -EEXIST) {
436 log_warning_errno(r, "Failed to add AF_UNIX socket to set, ignoring: %m");
Zbigniew Jędrzejewski-Szmekef422022013-04-22 23:12:15 -0400437 goto fail;
Lennart Poetteringf1ff7342018-01-10 17:28:03 +0100438 }
Lennart Poettering17b90522011-02-14 21:55:06 +0100439 }
440
441 return;
442
443fail:
Lennart Poetteringf1ff7342018-01-10 17:28:03 +0100444 unix_sockets = set_free_free(unix_sockets);
Lennart Poettering17b90522011-02-14 21:55:06 +0100445}
446
447static bool unix_socket_alive(const char *fn) {
448 assert(fn);
449
450 load_unix_sockets();
451
452 if (unix_sockets)
453 return !!set_get(unix_sockets, (char*) fn);
454
455 /* We don't know, so assume yes */
456 return true;
457}
458
Kay Sievers99d680a2013-03-13 13:12:36 +0100459static int dir_is_mount_point(DIR *d, const char *subdir) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200460
Kay Sievers99d680a2013-03-13 13:12:36 +0100461 int mount_id_parent, mount_id;
462 int r_p, r;
463
Lennart Poetteringcbfb8672017-11-20 15:29:53 +0100464 r_p = name_to_handle_at_loop(dirfd(d), ".", NULL, &mount_id_parent, 0);
Kay Sievers99d680a2013-03-13 13:12:36 +0100465 if (r_p < 0)
466 r_p = -errno;
467
Lennart Poetteringcbfb8672017-11-20 15:29:53 +0100468 r = name_to_handle_at_loop(dirfd(d), subdir, NULL, &mount_id, 0);
Kay Sievers99d680a2013-03-13 13:12:36 +0100469 if (r < 0)
470 r = -errno;
471
472 /* got no handle; make no assumptions, return error */
473 if (r_p < 0 && r < 0)
474 return r_p;
475
476 /* got both handles; if they differ, it is a mount point */
477 if (r_p >= 0 && r >= 0)
478 return mount_id_parent != mount_id;
479
480 /* got only one handle; assume different mount points if one
481 * of both queries was not supported by the filesystem */
Yu Watanabe4c701092017-10-04 23:01:32 +0900482 if (IN_SET(r_p, -ENOSYS, -EOPNOTSUPP) || IN_SET(r, -ENOSYS, -EOPNOTSUPP))
Kay Sievers99d680a2013-03-13 13:12:36 +0100483 return true;
484
485 /* return error */
486 if (r_p < 0)
487 return r_p;
488 return r;
489}
490
Zbigniew Jędrzejewski-Szmekdf99a9e2014-01-02 00:02:31 -0500491static DIR* xopendirat_nomod(int dirfd, const char *path) {
492 DIR *dir;
493
494 dir = xopendirat(dirfd, path, O_NOFOLLOW|O_NOATIME);
Lennart Poettering15322272015-03-23 18:54:31 +0700495 if (dir)
496 return dir;
497
498 log_debug_errno(errno, "Cannot open %sdirectory \"%s\": %m", dirfd == AT_FDCWD ? "" : "sub", path);
499 if (errno != EPERM)
500 return NULL;
501
502 dir = xopendirat(dirfd, path, O_NOFOLLOW);
503 if (!dir)
504 log_debug_errno(errno, "Cannot open %sdirectory \"%s\": %m", dirfd == AT_FDCWD ? "" : "sub", path);
Zbigniew Jędrzejewski-Szmekdf99a9e2014-01-02 00:02:31 -0500505
506 return dir;
507}
508
509static DIR* opendir_nomod(const char *path) {
510 return xopendirat_nomod(AT_FDCWD, path);
511}
512
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200513static int dir_cleanup(
Michal Sekletar78a92a52013-01-18 16:13:08 +0100514 Item *i,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200515 const char *p,
516 DIR *d,
517 const struct stat *ds,
518 usec_t cutoff,
519 dev_t rootdev,
520 bool mountpoint,
Lennart Poettering24f3a372012-06-20 09:05:50 +0200521 int maxdepth,
Lennart Poettering265ffa12013-09-17 16:33:30 -0500522 bool keep_this_level) {
523
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200524 struct dirent *dent;
525 struct timespec times[2];
526 bool deleted = false;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200527 int r = 0;
528
Reverend Homer8fb3f002016-12-09 12:04:30 +0300529 FOREACH_DIRENT_ALL(dent, d, break) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200530 struct stat s;
531 usec_t age;
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200532 _cleanup_free_ char *sub_path = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200533
Lennart Poettering49bfc872017-02-02 00:06:18 +0100534 if (dot_or_dot_dot(dent->d_name))
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200535 continue;
536
537 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
Kay Sieversca2f4172013-10-17 03:20:46 +0200538 if (errno == ENOENT)
539 continue;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200540
Kay Sieversca2f4172013-10-17 03:20:46 +0200541 /* FUSE, NFS mounts, SELinux might return EACCES */
Lennart Poetteringecc17092018-01-22 15:29:30 +0100542 r = log_full_errno(errno == EACCES ? LOG_DEBUG : LOG_ERR, errno,
543 "stat(%s/%s) failed: %m", p, dent->d_name);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200544 continue;
545 }
546
547 /* Stay on the same filesystem */
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500548 if (s.st_dev != rootdev) {
549 log_debug("Ignoring \"%s/%s\": different filesystem.", p, dent->d_name);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200550 continue;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500551 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200552
Kay Sievers99d680a2013-03-13 13:12:36 +0100553 /* Try to detect bind mounts of the same filesystem instance; they
554 * do not differ in device major/minors. This type of query is not
555 * supported on all kernels or filesystem types though. */
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500556 if (S_ISDIR(s.st_mode) && dir_is_mount_point(d, dent->d_name) > 0) {
557 log_debug("Ignoring \"%s/%s\": different mount of the same filesystem.",
558 p, dent->d_name);
Kay Sievers99d680a2013-03-13 13:12:36 +0100559 continue;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500560 }
Kay Sievers99d680a2013-03-13 13:12:36 +0100561
Zbigniew Jędrzejewski-Szmek605405c2016-10-23 11:43:27 -0400562 sub_path = strjoin(p, "/", dent->d_name);
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200563 if (!sub_path) {
Shawn Landden0d0f0c52012-07-25 14:55:59 -0700564 r = log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200565 goto finish;
566 }
567
568 /* Is there an item configured for this path? */
Lennart Poetteringef43a392015-04-22 18:18:56 +0200569 if (ordered_hashmap_get(items, sub_path)) {
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500570 log_debug("Ignoring \"%s\": a separate entry exists.", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200571 continue;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500572 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200573
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500574 if (find_glob(globs, sub_path)) {
575 log_debug("Ignoring \"%s\": a separate glob exists.", sub_path);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100576 continue;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500577 }
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100578
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200579 if (S_ISDIR(s.st_mode)) {
580
581 if (mountpoint &&
582 streq(dent->d_name, "lost+found") &&
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500583 s.st_uid == 0) {
584 log_debug("Ignoring \"%s\".", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200585 continue;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500586 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200587
588 if (maxdepth <= 0)
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500589 log_warning("Reached max depth on \"%s\".", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200590 else {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200591 _cleanup_closedir_ DIR *sub_dir;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200592 int q;
593
Zbigniew Jędrzejewski-Szmekdf99a9e2014-01-02 00:02:31 -0500594 sub_dir = xopendirat_nomod(dirfd(d), dent->d_name);
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200595 if (!sub_dir) {
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500596 if (errno != ENOENT)
597 r = log_error_errno(errno, "opendir(%s) failed: %m", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200598
599 continue;
600 }
601
Michal Sekletar78a92a52013-01-18 16:13:08 +0100602 q = dir_cleanup(i, sub_path, sub_dir, &s, cutoff, rootdev, false, maxdepth-1, false);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200603 if (q < 0)
604 r = q;
605 }
606
Lennart Poettering24f3a372012-06-20 09:05:50 +0200607 /* Note: if you are wondering why we don't
608 * support the sticky bit for excluding
609 * directories from cleaning like we do it for
610 * other file system objects: well, the sticky
611 * bit already has a meaning for directories,
612 * so we don't want to overload that. */
613
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500614 if (keep_this_level) {
615 log_debug("Keeping \"%s\".", sub_path);
Lennart Poettering24f3a372012-06-20 09:05:50 +0200616 continue;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500617 }
Lennart Poettering24f3a372012-06-20 09:05:50 +0200618
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200619 /* Ignore ctime, we change it when deleting */
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500620 age = timespec_load(&s.st_mtim);
621 if (age >= cutoff) {
622 char a[FORMAT_TIMESTAMP_MAX];
623 /* Follows spelling in stat(1). */
624 log_debug("Directory \"%s\": modify time %s is too new.",
625 sub_path,
626 format_timestamp_us(a, sizeof(a), age));
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200627 continue;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500628 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200629
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500630 age = timespec_load(&s.st_atim);
631 if (age >= cutoff) {
632 char a[FORMAT_TIMESTAMP_MAX];
633 log_debug("Directory \"%s\": access time %s is too new.",
634 sub_path,
635 format_timestamp_us(a, sizeof(a), age));
636 continue;
637 }
638
Zbigniew Jędrzejewski-Szmek61253222015-01-31 01:03:09 -0500639 log_debug("Removing directory \"%s\".", sub_path);
640 if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0)
Lennart Poettering920ce822018-01-22 15:31:01 +0100641 if (!IN_SET(errno, ENOENT, ENOTEMPTY))
642 r = log_error_errno(errno, "rmdir(%s): %m", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200643
644 } else {
Lennart Poettering9c737362010-11-14 20:12:51 +0100645 /* Skip files for which the sticky bit is
646 * set. These are semantics we define, and are
647 * unknown elsewhere. See XDG_RUNTIME_DIR
648 * specification for details. */
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500649 if (s.st_mode & S_ISVTX) {
650 log_debug("Skipping \"%s\": sticky bit set.", sub_path);
Lennart Poettering9c737362010-11-14 20:12:51 +0100651 continue;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200652 }
653
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500654 if (mountpoint && S_ISREG(s.st_mode))
Zbigniew Jędrzejewski-Szmek1542c012015-06-13 13:14:37 -0400655 if (s.st_uid == 0 && STR_IN_SET(dent->d_name,
656 ".journal",
657 "aquota.user",
658 "aquota.group")) {
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500659 log_debug("Skipping \"%s\".", sub_path);
660 continue;
661 }
662
Lennart Poettering17b90522011-02-14 21:55:06 +0100663 /* Ignore sockets that are listed in /proc/net/unix */
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500664 if (S_ISSOCK(s.st_mode) && unix_socket_alive(sub_path)) {
665 log_debug("Skipping \"%s\": live socket.", sub_path);
Lennart Poettering17b90522011-02-14 21:55:06 +0100666 continue;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500667 }
Lennart Poettering17b90522011-02-14 21:55:06 +0100668
Lennart Poettering78ab08e2011-02-19 14:20:16 +0100669 /* Ignore device nodes */
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500670 if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode)) {
671 log_debug("Skipping \"%s\": a device.", sub_path);
Lennart Poettering78ab08e2011-02-19 14:20:16 +0100672 continue;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500673 }
Lennart Poettering78ab08e2011-02-19 14:20:16 +0100674
Lennart Poettering24f3a372012-06-20 09:05:50 +0200675 /* Keep files on this level around if this is
676 * requested */
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500677 if (keep_this_level) {
678 log_debug("Keeping \"%s\".", sub_path);
Lennart Poettering24f3a372012-06-20 09:05:50 +0200679 continue;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200680 }
681
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500682 age = timespec_load(&s.st_mtim);
683 if (age >= cutoff) {
684 char a[FORMAT_TIMESTAMP_MAX];
685 /* Follows spelling in stat(1). */
686 log_debug("File \"%s\": modify time %s is too new.",
687 sub_path,
688 format_timestamp_us(a, sizeof(a), age));
689 continue;
690 }
691
692 age = timespec_load(&s.st_atim);
693 if (age >= cutoff) {
694 char a[FORMAT_TIMESTAMP_MAX];
695 log_debug("File \"%s\": access time %s is too new.",
696 sub_path,
697 format_timestamp_us(a, sizeof(a), age));
698 continue;
699 }
700
701 age = timespec_load(&s.st_ctim);
702 if (age >= cutoff) {
703 char a[FORMAT_TIMESTAMP_MAX];
704 log_debug("File \"%s\": change time %s is too new.",
705 sub_path,
706 format_timestamp_us(a, sizeof(a), age));
707 continue;
708 }
709
710 log_debug("unlink \"%s\"", sub_path);
711
712 if (unlinkat(dirfd(d), dent->d_name, 0) < 0)
713 if (errno != ENOENT)
714 r = log_error_errno(errno, "unlink(%s): %m", sub_path);
715
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200716 deleted = true;
717 }
718 }
719
720finish:
721 if (deleted) {
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500722 usec_t age1, age2;
723 char a[FORMAT_TIMESTAMP_MAX], b[FORMAT_TIMESTAMP_MAX];
724
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200725 /* Restore original directory timestamps */
726 times[0] = ds->st_atim;
727 times[1] = ds->st_mtim;
728
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500729 age1 = timespec_load(&ds->st_atim);
730 age2 = timespec_load(&ds->st_mtim);
731 log_debug("Restoring access and modification time on \"%s\": %s, %s",
732 p,
733 format_timestamp_us(a, sizeof(a), age1),
734 format_timestamp_us(b, sizeof(b), age2));
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200735 if (futimens(dirfd(d), times) < 0)
Michal Schmidt56f64d92014-11-28 19:29:59 +0100736 log_error_errno(errno, "utimensat(%s): %m", p);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200737 }
738
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200739 return r;
740}
741
Lennart Poettering5579f852018-01-23 14:03:34 +0100742static bool dangerous_hardlinks(void) {
743 _cleanup_free_ char *value = NULL;
744 static int cached = -1;
745 int r;
746
747 /* Check whether the fs.protected_hardlinks sysctl is on. If we can't determine it we assume its off, as that's
748 * what the upstream default is. */
749
750 if (cached >= 0)
751 return cached;
752
753 r = read_one_line_file("/proc/sys/fs/protected_hardlinks", &value);
754 if (r < 0) {
755 log_debug_errno(r, "Failed to read fs.protected_hardlinks sysctl: %m");
756 return true;
757 }
758
759 r = parse_boolean(value);
760 if (r < 0) {
761 log_debug_errno(r, "Failed to parse fs.protected_hardlinks sysctl: %m");
762 return true;
763 }
764
765 cached = r == 0;
766 return cached;
767}
768
Franck Bui774f79b2018-03-02 16:13:07 +0100769static bool hardlink_vulnerable(const struct stat *st) {
Lennart Poettering5579f852018-01-23 14:03:34 +0100770 assert(st);
771
772 return !S_ISDIR(st->st_mode) && st->st_nlink > 1 && dangerous_hardlinks();
773}
774
Lennart Poettering9d874ae2018-08-06 15:40:16 +0200775static int fd_set_perms(Item *i, int fd, const char *path, const struct stat *st) {
Franck Buib206ac82018-04-13 11:39:39 +0200776 struct stat stbuf;
Franck Bui936f6bd2018-03-02 17:19:32 +0100777
778 assert(i);
779 assert(fd);
Franck Bui4dc7bfd2018-08-20 17:23:12 +0200780 assert(path);
Franck Bui936f6bd2018-03-02 17:19:32 +0100781
782 if (!i->mode_set && !i->uid_set && !i->gid_set)
783 goto shortcut;
784
Franck Buib206ac82018-04-13 11:39:39 +0200785 if (!st) {
786 if (fstat(fd, &stbuf) < 0)
787 return log_error_errno(errno, "fstat(%s) failed: %m", path);
788 st = &stbuf;
789 }
790
Franck Bui936f6bd2018-03-02 17:19:32 +0100791 if (hardlink_vulnerable(st)) {
792 log_error("Refusing to set permissions on hardlinked file %s while the fs.protected_hardlinks sysctl is turned off.", path);
793 return -EPERM;
794 }
795
796 if (i->mode_set) {
797 if (S_ISLNK(st->st_mode))
798 log_debug("Skipping mode fix for symlink %s.", path);
799 else {
800 mode_t m = i->mode;
801
802 if (i->mask_perms) {
803 if (!(st->st_mode & 0111))
804 m &= ~0111;
805 if (!(st->st_mode & 0222))
806 m &= ~0222;
807 if (!(st->st_mode & 0444))
808 m &= ~0444;
809 if (!S_ISDIR(st->st_mode))
810 m &= ~07000; /* remove sticky/sgid/suid bit, unless directory */
811 }
812
813 if (m == (st->st_mode & 07777))
814 log_debug("\"%s\" has correct mode %o already.", path, st->st_mode);
815 else {
Franck Bui936f6bd2018-03-02 17:19:32 +0100816 log_debug("Changing \"%s\" to mode %o.", path, m);
Franck Bui4dfaa522018-04-11 16:58:49 +0200817 if (fchmod_opath(fd, m) < 0)
818 return log_error_errno(errno, "fchmod() of %s failed: %m", path);
Franck Bui936f6bd2018-03-02 17:19:32 +0100819 }
820 }
821 }
822
823 if ((i->uid_set && i->uid != st->st_uid) ||
824 (i->gid_set && i->gid != st->st_gid)) {
825 log_debug("Changing \"%s\" to owner "UID_FMT":"GID_FMT,
826 path,
827 i->uid_set ? i->uid : UID_INVALID,
828 i->gid_set ? i->gid : GID_INVALID);
829
830 if (fchownat(fd,
831 "",
832 i->uid_set ? i->uid : UID_INVALID,
833 i->gid_set ? i->gid : GID_INVALID,
834 AT_EMPTY_PATH) < 0)
835 return log_error_errno(errno, "fchownat() of %s failed: %m", path);
836 }
837
838shortcut:
Lennart Poettering08c84982018-03-27 07:38:26 +0200839 return label_fix(path, 0);
Franck Bui936f6bd2018-03-02 17:19:32 +0100840}
841
Franck Bui5ec9d062018-04-13 17:31:22 +0200842static int path_open_parent_safe(const char *path) {
843 _cleanup_free_ char *dn = NULL;
844 int fd;
845
846 if (path_equal(path, "/") || !path_is_normalized(path)) {
847 log_error("Failed to open parent of '%s': invalid path.", path);
848 return -EINVAL;
849 }
850
851 dn = dirname_malloc(path);
852 if (!dn)
853 return log_oom();
854
855 fd = chase_symlinks(dn, NULL, CHASE_OPEN|CHASE_SAFE, NULL);
856 if (fd == -EPERM)
857 return log_error_errno(fd, "Unsafe symlinks encountered in %s, refusing.", path);
858 if (fd < 0)
859 return log_error_errno(fd, "Failed to validate path %s: %m", path);
860
861 return fd;
862}
863
Franck Buiaddc3e32018-03-20 08:58:48 +0100864static int path_open_safe(const char *path) {
865 int fd;
866
867 /* path_open_safe() returns a file descriptor opened with O_PATH after
868 * verifying that the path doesn't contain unsafe transitions, except
869 * for its final component as the function does not follow symlink. */
870
871 assert(path);
872
873 if (!path_is_normalized(path)) {
874 log_error("Failed to open invalid path '%s'.", path);
875 return -EINVAL;
876 }
877
878 fd = chase_symlinks(path, NULL, CHASE_OPEN|CHASE_SAFE|CHASE_NOFOLLOW, NULL);
879 if (fd == -EPERM)
880 return log_error_errno(fd, "Unsafe symlinks encountered in %s, refusing.", path);
881 if (fd < 0)
882 return log_error_errno(fd, "Failed to validate path %s: %m", path);
883
884 return fd;
885}
886
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -0500887static int path_set_perms(Item *i, const char *path) {
Lennart Poettering48b8aaa2015-04-13 15:16:54 +0200888 _cleanup_close_ int fd = -1;
Michael Olbrich1924a972014-08-17 09:45:00 +0200889
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200890 assert(i);
891 assert(path);
892
Franck Buiaddc3e32018-03-20 08:58:48 +0100893 fd = path_open_safe(path);
Franck Bui54946022018-04-26 15:45:17 +0200894 if (fd < 0)
Franck Buiaddc3e32018-03-20 08:58:48 +0100895 return fd;
Michal Sekletar780e2ee2017-08-31 12:45:25 +0200896
Lennart Poettering9d874ae2018-08-06 15:40:16 +0200897 return fd_set_perms(i, fd, path, NULL);
Michal Schmidt062e01b2011-12-16 18:00:11 +0100898}
899
Lennart Poetteringbd550f72015-04-10 16:03:24 +0200900static int parse_xattrs_from_arg(Item *i) {
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100901 const char *p;
902 int r;
903
904 assert(i);
Zbigniew Jędrzejewski-Szmek505ef0e2015-01-09 01:10:02 -0500905 assert(i->argument);
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100906
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100907 p = i->argument;
908
Lennart Poettering4034a062015-03-23 18:55:36 +0700909 for (;;) {
Franck Bui4cef1922017-11-16 11:27:29 +0100910 _cleanup_free_ char *name = NULL, *value = NULL, *xattr = NULL;
Lennart Poettering4034a062015-03-23 18:55:36 +0700911
Richard Maw12ba2c42015-06-23 16:26:49 +0000912 r = extract_first_word(&p, &xattr, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE);
Lennart Poettering4034a062015-03-23 18:55:36 +0700913 if (r < 0)
Lennart Poetteringbd550f72015-04-10 16:03:24 +0200914 log_warning_errno(r, "Failed to parse extended attribute '%s', ignoring: %m", p);
Lennart Poettering4034a062015-03-23 18:55:36 +0700915 if (r <= 0)
916 break;
Zbigniew Jędrzejewski-Szmek505ef0e2015-01-09 01:10:02 -0500917
Franck Bui4cef1922017-11-16 11:27:29 +0100918 r = split_pair(xattr, "=", &name, &value);
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100919 if (r < 0) {
Lennart Poettering4034a062015-03-23 18:55:36 +0700920 log_warning_errno(r, "Failed to parse extended attribute, ignoring: %s", xattr);
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100921 continue;
922 }
Zbigniew Jędrzejewski-Szmek505ef0e2015-01-09 01:10:02 -0500923
Lennart Poettering4034a062015-03-23 18:55:36 +0700924 if (isempty(name) || isempty(value)) {
Lennart Poetteringbd550f72015-04-10 16:03:24 +0200925 log_warning("Malformed extended attribute found, ignoring: %s", xattr);
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100926 continue;
927 }
Zbigniew Jędrzejewski-Szmek505ef0e2015-01-09 01:10:02 -0500928
Lennart Poettering4034a062015-03-23 18:55:36 +0700929 if (strv_push_pair(&i->xattrs, name, value) < 0)
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100930 return log_oom();
Zbigniew Jędrzejewski-Szmek505ef0e2015-01-09 01:10:02 -0500931
Lennart Poettering4034a062015-03-23 18:55:36 +0700932 name = value = NULL;
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100933 }
934
Lennart Poettering4034a062015-03-23 18:55:36 +0700935 return 0;
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100936}
937
Lennart Poettering9d874ae2018-08-06 15:40:16 +0200938static int fd_set_xattrs(Item *i, int fd, const char *path, const struct stat *st) {
Lennart Poettering34d26772018-03-23 15:41:33 +0100939 char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100940 char **name, **value;
941
942 assert(i);
Franck Bui936f6bd2018-03-02 17:19:32 +0100943 assert(fd);
Franck Bui4dc7bfd2018-08-20 17:23:12 +0200944 assert(path);
Franck Bui936f6bd2018-03-02 17:19:32 +0100945
946 xsprintf(procfs_path, "/proc/self/fd/%i", fd);
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100947
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100948 STRV_FOREACH_PAIR(name, value, i->xattrs) {
Lennart Poetteringbd550f72015-04-10 16:03:24 +0200949 log_debug("Setting extended attribute '%s=%s' on %s.", *name, *value, path);
Franck Bui936f6bd2018-03-02 17:19:32 +0100950 if (setxattr(procfs_path, *name, *value, strlen(*value), 0) < 0)
Zbigniew Jędrzejewski-Szmek25f027c2017-05-13 11:26:55 -0400951 return log_error_errno(errno, "Setting extended attribute %s=%s on %s failed: %m",
952 *name, *value, path);
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100953 }
954 return 0;
955}
956
Franck Bui936f6bd2018-03-02 17:19:32 +0100957static int path_set_xattrs(Item *i, const char *path) {
958 _cleanup_close_ int fd = -1;
959
960 assert(i);
961 assert(path);
962
Franck Buiaddc3e32018-03-20 08:58:48 +0100963 fd = path_open_safe(path);
Franck Bui936f6bd2018-03-02 17:19:32 +0100964 if (fd < 0)
Franck Buiaddc3e32018-03-20 08:58:48 +0100965 return fd;
Franck Bui936f6bd2018-03-02 17:19:32 +0100966
Lennart Poettering9d874ae2018-08-06 15:40:16 +0200967 return fd_set_xattrs(i, fd, path, NULL);
Franck Bui936f6bd2018-03-02 17:19:32 +0100968}
969
Lennart Poetteringbd550f72015-04-10 16:03:24 +0200970static int parse_acls_from_arg(Item *item) {
Zbigniew Jędrzejewski-Szmek349cc4a2017-10-03 10:41:51 +0200971#if HAVE_ACL
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -0500972 int r;
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -0500973
974 assert(item);
975
Zbigniew Jędrzejewski-Szmek50d9e462015-01-18 05:02:47 -0500976 /* If force (= modify) is set, we will not modify the acl
977 * afterwards, so the mask can be added now if necessary. */
Lennart Poetteringbd550f72015-04-10 16:03:24 +0200978
Zbigniew Jędrzejewski-Szmek50d9e462015-01-18 05:02:47 -0500979 r = parse_acl(item->argument, &item->acl_access, &item->acl_default, !item->force);
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -0500980 if (r < 0)
Lennart Poettering4034a062015-03-23 18:55:36 +0700981 log_warning_errno(r, "Failed to parse ACL \"%s\": %m. Ignoring", item->argument);
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -0500982#else
983 log_warning_errno(ENOSYS, "ACLs are not supported. Ignoring");
984#endif
985
986 return 0;
987}
988
Zbigniew Jędrzejewski-Szmek349cc4a2017-10-03 10:41:51 +0200989#if HAVE_ACL
Lennart Poettering48b8aaa2015-04-13 15:16:54 +0200990static int path_set_acl(const char *path, const char *pretty, acl_type_t type, acl_t acl, bool modify) {
991 _cleanup_(acl_free_charpp) char *t = NULL;
Zbigniew Jędrzejewski-Szmekdd4105b2015-01-18 18:22:27 -0500992 _cleanup_(acl_freep) acl_t dup = NULL;
Zbigniew Jędrzejewski-Szmek50d9e462015-01-18 05:02:47 -0500993 int r;
994
Hans-Peter Deifeld873e872015-03-03 00:35:08 +0100995 /* Returns 0 for success, positive error if already warned,
996 * negative error otherwise. */
997
Zbigniew Jędrzejewski-Szmek50d9e462015-01-18 05:02:47 -0500998 if (modify) {
Zbigniew Jędrzejewski-Szmekdd4105b2015-01-18 18:22:27 -0500999 r = acls_for_file(path, type, acl, &dup);
Zbigniew Jędrzejewski-Szmek50d9e462015-01-18 05:02:47 -05001000 if (r < 0)
1001 return r;
Zbigniew Jędrzejewski-Szmek50d9e462015-01-18 05:02:47 -05001002
Zbigniew Jędrzejewski-Szmekdd4105b2015-01-18 18:22:27 -05001003 r = calc_acl_mask_if_needed(&dup);
1004 if (r < 0)
1005 return r;
1006 } else {
1007 dup = acl_dup(acl);
1008 if (!dup)
1009 return -errno;
1010
1011 /* the mask was already added earlier if needed */
1012 }
1013
1014 r = add_base_acls_if_needed(&dup, path);
1015 if (r < 0)
1016 return r;
1017
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001018 t = acl_to_any_text(dup, NULL, ',', TEXT_ABBREVIATE);
Lennart Poettering48b8aaa2015-04-13 15:16:54 +02001019 log_debug("Setting %s ACL %s on %s.",
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001020 type == ACL_TYPE_ACCESS ? "access" : "default",
Lennart Poettering48b8aaa2015-04-13 15:16:54 +02001021 strna(t), pretty);
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001022
Zbigniew Jędrzejewski-Szmekdd4105b2015-01-18 18:22:27 -05001023 r = acl_set_file(path, type, dup);
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001024 if (r < 0)
Lennart Poettering3ea40b72015-04-10 14:44:52 +02001025 /* Return positive to indicate we already warned */
1026 return -log_error_errno(errno,
1027 "Setting %s ACL \"%s\" on %s failed: %m",
1028 type == ACL_TYPE_ACCESS ? "access" : "default",
Lennart Poettering48b8aaa2015-04-13 15:16:54 +02001029 strna(t), pretty);
Hans-Peter Deifeld873e872015-03-03 00:35:08 +01001030
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001031 return 0;
Zbigniew Jędrzejewski-Szmek50d9e462015-01-18 05:02:47 -05001032}
Zbigniew Jędrzejewski-Szmek35888b62015-02-02 20:28:39 -05001033#endif
Zbigniew Jędrzejewski-Szmek50d9e462015-01-18 05:02:47 -05001034
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001035static int fd_set_acls(Item *item, int fd, const char *path, const struct stat *st) {
Hans-Peter Deifeld873e872015-03-03 00:35:08 +01001036 int r = 0;
Zbigniew Jędrzejewski-Szmek349cc4a2017-10-03 10:41:51 +02001037#if HAVE_ACL
Lennart Poettering34d26772018-03-23 15:41:33 +01001038 char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
Franck Buib206ac82018-04-13 11:39:39 +02001039 struct stat stbuf;
Franck Bui936f6bd2018-03-02 17:19:32 +01001040
1041 assert(item);
1042 assert(fd);
Franck Bui4dc7bfd2018-08-20 17:23:12 +02001043 assert(path);
Franck Bui936f6bd2018-03-02 17:19:32 +01001044
Franck Buib206ac82018-04-13 11:39:39 +02001045 if (!st) {
1046 if (fstat(fd, &stbuf) < 0)
1047 return log_error_errno(errno, "fstat(%s) failed: %m", path);
1048 st = &stbuf;
1049 }
1050
Franck Bui936f6bd2018-03-02 17:19:32 +01001051 if (hardlink_vulnerable(st)) {
1052 log_error("Refusing to set ACLs on hardlinked file %s while the fs.protected_hardlinks sysctl is turned off.", path);
1053 return -EPERM;
1054 }
1055
1056 if (S_ISLNK(st->st_mode)) {
1057 log_debug("Skipping ACL fix for symlink %s.", path);
1058 return 0;
1059 }
1060
1061 xsprintf(procfs_path, "/proc/self/fd/%i", fd);
1062
1063 if (item->acl_access)
1064 r = path_set_acl(procfs_path, path, ACL_TYPE_ACCESS, item->acl_access, item->force);
1065
remueller26673852018-07-16 16:56:01 +02001066 /* set only default acls to folders */
1067 if (r == 0 && item->acl_default && S_ISDIR(st->st_mode))
Franck Bui936f6bd2018-03-02 17:19:32 +01001068 r = path_set_acl(procfs_path, path, ACL_TYPE_DEFAULT, item->acl_default, item->force);
1069
1070 if (r > 0)
1071 return -r; /* already warned */
1072 if (r == -EOPNOTSUPP) {
1073 log_debug_errno(r, "ACLs not supported by file system at %s", path);
1074 return 0;
1075 }
1076 if (r < 0)
1077 return log_error_errno(r, "ACL operation on \"%s\" failed: %m", path);
1078#endif
1079 return r;
1080}
1081
1082static int path_set_acls(Item *item, const char *path) {
1083 int r = 0;
Yu Watanabe545cdb92018-06-29 16:26:49 +09001084#if HAVE_ACL
Lennart Poettering48b8aaa2015-04-13 15:16:54 +02001085 _cleanup_close_ int fd = -1;
Lennart Poettering48b8aaa2015-04-13 15:16:54 +02001086
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -05001087 assert(item);
1088 assert(path);
1089
Franck Buiaddc3e32018-03-20 08:58:48 +01001090 fd = path_open_safe(path);
Lennart Poettering48b8aaa2015-04-13 15:16:54 +02001091 if (fd < 0)
Franck Buiaddc3e32018-03-20 08:58:48 +01001092 return fd;
Lennart Poettering48b8aaa2015-04-13 15:16:54 +02001093
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001094 r = fd_set_acls(item, fd, path, NULL);
Yu Watanabe545cdb92018-06-29 16:26:49 +09001095#endif
1096 return r;
1097}
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -05001098
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001099#define ATTRIBUTES_ALL \
1100 (FS_NOATIME_FL | \
1101 FS_SYNC_FL | \
1102 FS_DIRSYNC_FL | \
1103 FS_APPEND_FL | \
1104 FS_COMPR_FL | \
1105 FS_NODUMP_FL | \
1106 FS_EXTENT_FL | \
1107 FS_IMMUTABLE_FL | \
1108 FS_JOURNAL_DATA_FL | \
1109 FS_SECRM_FL | \
1110 FS_UNRM_FL | \
1111 FS_NOTAIL_FL | \
1112 FS_TOPDIR_FL | \
1113 FS_NOCOW_FL)
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001114
Lennart Poetteringbd550f72015-04-10 16:03:24 +02001115static int parse_attribute_from_arg(Item *item) {
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001116
1117 static const struct {
1118 char character;
1119 unsigned value;
1120 } attributes[] = {
1121 { 'A', FS_NOATIME_FL }, /* do not update atime */
1122 { 'S', FS_SYNC_FL }, /* Synchronous updates */
1123 { 'D', FS_DIRSYNC_FL }, /* dirsync behaviour (directories only) */
1124 { 'a', FS_APPEND_FL }, /* writes to file may only append */
1125 { 'c', FS_COMPR_FL }, /* Compress file */
1126 { 'd', FS_NODUMP_FL }, /* do not dump file */
kpengboy8c35b2c2016-05-29 08:31:14 -07001127 { 'e', FS_EXTENT_FL }, /* Extents */
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001128 { 'i', FS_IMMUTABLE_FL }, /* Immutable file */
1129 { 'j', FS_JOURNAL_DATA_FL }, /* Reserved for ext3 */
1130 { 's', FS_SECRM_FL }, /* Secure deletion */
1131 { 'u', FS_UNRM_FL }, /* Undelete */
1132 { 't', FS_NOTAIL_FL }, /* file tail should not be merged */
AsciiWolf13e785f2017-02-24 18:14:02 +01001133 { 'T', FS_TOPDIR_FL }, /* Top of directory hierarchies */
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001134 { 'C', FS_NOCOW_FL }, /* Do not cow file */
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001135 };
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001136
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001137 enum {
1138 MODE_ADD,
1139 MODE_DEL,
1140 MODE_SET
1141 } mode = MODE_ADD;
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001142
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001143 unsigned value = 0, mask = 0;
1144 const char *p;
1145
1146 assert(item);
1147
1148 p = item->argument;
1149 if (p) {
1150 if (*p == '+') {
1151 mode = MODE_ADD;
1152 p++;
1153 } else if (*p == '-') {
1154 mode = MODE_DEL;
1155 p++;
1156 } else if (*p == '=') {
1157 mode = MODE_SET;
1158 p++;
1159 }
1160 }
1161
1162 if (isempty(p) && mode != MODE_SET) {
1163 log_error("Setting file attribute on '%s' needs an attribute specification.", item->path);
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001164 return -EINVAL;
1165 }
1166
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001167 for (; p && *p ; p++) {
1168 unsigned i, v;
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001169
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001170 for (i = 0; i < ELEMENTSOF(attributes); i++)
1171 if (*p == attributes[i].character)
1172 break;
1173
1174 if (i >= ELEMENTSOF(attributes)) {
1175 log_error("Unknown file attribute '%c' on '%s'.", *p, item->path);
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001176 return -EINVAL;
1177 }
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001178
1179 v = attributes[i].value;
1180
Andreas Rammhold37420952017-09-29 00:37:23 +02001181 SET_FLAG(value, v, IN_SET(mode, MODE_ADD, MODE_SET));
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001182
1183 mask |= v;
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001184 }
1185
1186 if (mode == MODE_SET)
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001187 mask |= ATTRIBUTES_ALL;
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001188
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001189 assert(mask != 0);
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001190
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001191 item->attribute_mask = mask;
1192 item->attribute_value = value;
1193 item->attribute_set = true;
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001194
1195 return 0;
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001196}
1197
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001198static int fd_set_attribute(Item *item, int fd, const char *path, const struct stat *st) {
Franck Bui936f6bd2018-03-02 17:19:32 +01001199 _cleanup_close_ int procfs_fd = -1;
Franck Buib206ac82018-04-13 11:39:39 +02001200 struct stat stbuf;
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001201 unsigned f;
1202 int r;
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001203
Franck Bui4dc7bfd2018-08-20 17:23:12 +02001204 assert(item);
1205 assert(fd);
1206 assert(path);
1207
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001208 if (!item->attribute_set || item->attribute_mask == 0)
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001209 return 0;
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001210
Franck Buib206ac82018-04-13 11:39:39 +02001211 if (!st) {
1212 if (fstat(fd, &stbuf) < 0)
1213 return log_error_errno(errno, "fstat(%s) failed: %m", path);
1214 st = &stbuf;
1215 }
1216
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001217 /* Issuing the file attribute ioctls on device nodes is not
1218 * safe, as that will be delivered to the drivers, not the
1219 * file system containing the device node. */
Franck Bui936f6bd2018-03-02 17:19:32 +01001220 if (!S_ISREG(st->st_mode) && !S_ISDIR(st->st_mode)) {
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001221 log_error("Setting file flags is only supported on regular files and directories, cannot set on '%s'.", path);
1222 return -EINVAL;
1223 }
1224
1225 f = item->attribute_value & item->attribute_mask;
1226
1227 /* Mask away directory-specific flags */
Franck Bui936f6bd2018-03-02 17:19:32 +01001228 if (!S_ISDIR(st->st_mode))
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001229 f &= ~FS_DIRSYNC_FL;
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001230
Lennart Poetteringf2324782018-03-26 13:25:51 +02001231 procfs_fd = fd_reopen(fd, O_RDONLY|O_CLOEXEC|O_NOATIME);
Franck Bui936f6bd2018-03-02 17:19:32 +01001232 if (procfs_fd < 0)
Lennart Poetteringc3e03772018-08-06 20:19:52 +02001233 return log_error_errno(procfs_fd, "Failed to re-open '%s': %m", path);
Franck Bui936f6bd2018-03-02 17:19:32 +01001234
Lennart Poetteringdb9a4252018-06-25 20:04:07 +02001235 r = chattr_fd(procfs_fd, f, item->attribute_mask, NULL);
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001236 if (r < 0)
Yu Watanabe4c701092017-10-04 23:01:32 +09001237 log_full_errno(IN_SET(r, -ENOTTY, -EOPNOTSUPP) ? LOG_DEBUG : LOG_WARNING,
Lennart Poetteringad75a972015-07-22 22:02:14 +02001238 r,
Lennart Poettering15b45fa2018-08-06 15:44:47 +02001239 "Cannot set file attribute for '%s', value=0x%08x, mask=0x%08x, ignoring: %m",
Lennart Poetteringad75a972015-07-22 22:02:14 +02001240 path, item->attribute_value, item->attribute_mask);
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001241
1242 return 0;
1243}
1244
Franck Bui936f6bd2018-03-02 17:19:32 +01001245static int path_set_attribute(Item *item, const char *path) {
1246 _cleanup_close_ int fd = -1;
Franck Bui936f6bd2018-03-02 17:19:32 +01001247
1248 if (!item->attribute_set || item->attribute_mask == 0)
1249 return 0;
1250
Franck Buiaddc3e32018-03-20 08:58:48 +01001251 fd = path_open_safe(path);
Franck Bui936f6bd2018-03-02 17:19:32 +01001252 if (fd < 0)
Franck Buiaddc3e32018-03-20 08:58:48 +01001253 return fd;
Franck Bui936f6bd2018-03-02 17:19:32 +01001254
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001255 return fd_set_attribute(item, fd, path, NULL);
Franck Bui936f6bd2018-03-02 17:19:32 +01001256}
1257
Dave Reisnerd4e9eb92012-09-03 17:13:18 -04001258static int write_one_file(Item *i, const char *path) {
Franck Bui551470e2018-04-13 17:54:09 +02001259 _cleanup_close_ int fd = -1, dir_fd = -1;
1260 char *bn;
Franck Bui31c84ff2018-04-13 09:19:24 +02001261 int r;
1262
1263 assert(i);
1264 assert(path);
1265 assert(i->argument);
1266 assert(i->type == WRITE_FILE);
1267
Franck Bui551470e2018-04-13 17:54:09 +02001268 /* Validate the path and keep the fd on the directory for opening the
1269 * file so we're sure that it can't be changed behind our back. */
1270 dir_fd = path_open_parent_safe(path);
1271 if (dir_fd < 0)
1272 return dir_fd;
1273
1274 bn = basename(path);
1275
Franck Bui31c84ff2018-04-13 09:19:24 +02001276 /* Follows symlinks */
Franck Bui551470e2018-04-13 17:54:09 +02001277 fd = openat(dir_fd, bn, O_NONBLOCK|O_CLOEXEC|O_WRONLY|O_NOCTTY, i->mode);
Franck Bui31c84ff2018-04-13 09:19:24 +02001278 if (fd < 0) {
1279 if (errno == ENOENT) {
1280 log_debug_errno(errno, "Not writing missing file \"%s\": %m", path);
1281 return 0;
1282 }
1283 return log_error_errno(errno, "Failed to open file \"%s\": %m", path);
1284 }
1285
1286 /* 'w' is allowed to write into any kind of files. */
1287 log_debug("Writing to \"%s\".", path);
1288
1289 r = loop_write(fd, i->argument, strlen(i->argument), false);
1290 if (r < 0)
1291 return log_error_errno(r, "Failed to write file \"%s\": %m", path);
1292
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001293 return fd_set_perms(i, fd, path, NULL);
Franck Bui31c84ff2018-04-13 09:19:24 +02001294}
1295
1296static int create_file(Item *i, const char *path) {
Franck Bui5ec9d062018-04-13 17:31:22 +02001297 _cleanup_close_ int fd = -1, dir_fd = -1;
1298 struct stat stbuf, *st = NULL;
1299 int r = 0;
1300 char *bn;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -04001301
Lennart Poettering874f1942014-06-10 22:48:56 +02001302 assert(i);
1303 assert(path);
Franck Bui5ec9d062018-04-13 17:31:22 +02001304 assert(i->type == CREATE_FILE);
Lennart Poettering874f1942014-06-10 22:48:56 +02001305
Franck Bui5ec9d062018-04-13 17:31:22 +02001306 /* 'f' operates on regular files exclusively. */
1307
1308 /* Validate the path and keep the fd on the directory for opening the
1309 * file so we're sure that it can't be changed behind our back. */
1310 dir_fd = path_open_parent_safe(path);
1311 if (dir_fd < 0)
1312 return dir_fd;
1313
1314 bn = basename(path);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -04001315
Lennart Poettering43ad6e32014-06-18 00:01:39 +02001316 RUN_WITH_UMASK(0000) {
Lennart Poetteringecabcf82014-10-23 19:41:27 +02001317 mac_selinux_create_file_prepare(path, S_IFREG);
Franck Bui5ec9d062018-04-13 17:31:22 +02001318 fd = openat(dir_fd, bn, O_CREAT|O_EXCL|O_NOFOLLOW|O_NONBLOCK|O_CLOEXEC|O_WRONLY|O_NOCTTY, i->mode);
Lennart Poetteringecabcf82014-10-23 19:41:27 +02001319 mac_selinux_create_file_clear();
Lennart Poettering5c0d3982013-04-04 03:39:39 +02001320 }
Dave Reisnerd4e9eb92012-09-03 17:13:18 -04001321
1322 if (fd < 0) {
Franck Bui5ec9d062018-04-13 17:31:22 +02001323 /* Even on a read-only filesystem, open(2) returns EEXIST if the
1324 * file already exists. It returns EROFS only if it needs to
1325 * create the file. */
1326 if (errno != EEXIST)
1327 return log_error_errno(errno, "Failed to create file %s: %m", path);
1328
1329 /* Re-open the file. At that point it must exist since open(2)
1330 * failed with EEXIST. We still need to check if the perms/mode
1331 * need to be changed. For read-only filesystems, we let
1332 * fd_set_perms() report the error if the perms need to be
1333 * modified. */
1334 fd = openat(dir_fd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH, i->mode);
1335 if (fd < 0)
1336 return log_error_errno(errno, "Failed to re-open file %s: %m", path);
1337
1338 if (fstat(fd, &stbuf) < 0)
1339 return log_error_errno(errno, "stat(%s) failed: %m", path);
1340
1341 if (!S_ISREG(stbuf.st_mode)) {
1342 log_error("%s exists and is not a regular file.", path);
1343 return -EEXIST;
Lennart Poettering49e87292018-01-24 10:54:10 +01001344 }
Dave Reisnerd4e9eb92012-09-03 17:13:18 -04001345
Franck Bui5ec9d062018-04-13 17:31:22 +02001346 st = &stbuf;
1347 } else {
Michael Olbrichf44b28f2015-04-30 20:50:38 +02001348
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001349 log_debug("\"%s\" has been created.", path);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -04001350
Franck Bui5ec9d062018-04-13 17:31:22 +02001351 if (i->argument) {
1352 log_debug("Writing to \"%s\".", path);
Dave Reisner3612fbc2012-09-12 16:21:00 -04001353
Franck Bui5ec9d062018-04-13 17:31:22 +02001354 r = loop_write(fd, i->argument, strlen(i->argument), false);
1355 if (r < 0)
1356 return log_error_errno(r, "Failed to write file \"%s\": %m", path);
1357 }
1358 }
1359
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001360 return fd_set_perms(i, fd, path, st);
Franck Bui5ec9d062018-04-13 17:31:22 +02001361}
1362
1363static int truncate_file(Item *i, const char *path) {
Franck Bui14ab8042018-04-13 17:50:49 +02001364 _cleanup_close_ int fd = -1, dir_fd = -1;
Franck Bui5ec9d062018-04-13 17:31:22 +02001365 struct stat stbuf, *st = NULL;
1366 bool erofs = false;
1367 int r = 0;
Franck Bui14ab8042018-04-13 17:50:49 +02001368 char *bn;
Franck Bui5ec9d062018-04-13 17:31:22 +02001369
1370 assert(i);
1371 assert(path);
1372 assert(i->type == TRUNCATE_FILE);
1373
1374 /* We want to operate on regular file exclusively especially since
1375 * O_TRUNC is unspecified if the file is neither a regular file nor a
1376 * fifo nor a terminal device. Therefore we first open the file and make
1377 * sure it's a regular one before truncating it. */
1378
Franck Bui14ab8042018-04-13 17:50:49 +02001379 /* Validate the path and keep the fd on the directory for opening the
1380 * file so we're sure that it can't be changed behind our back. */
1381 dir_fd = path_open_parent_safe(path);
1382 if (dir_fd < 0)
1383 return dir_fd;
1384
1385 bn = basename(path);
1386
Franck Bui5ec9d062018-04-13 17:31:22 +02001387 RUN_WITH_UMASK(0000) {
1388 mac_selinux_create_file_prepare(path, S_IFREG);
Franck Bui14ab8042018-04-13 17:50:49 +02001389 fd = openat(dir_fd, bn, O_CREAT|O_NOFOLLOW|O_NONBLOCK|O_CLOEXEC|O_WRONLY|O_NOCTTY, i->mode);
Franck Bui5ec9d062018-04-13 17:31:22 +02001390 mac_selinux_create_file_clear();
1391 }
1392
1393 if (fd < 0) {
1394 if (errno != EROFS)
1395 return log_error_errno(errno, "Failed to open/create file %s: %m", path);
1396
1397 /* On a read-only filesystem, we don't want to fail if the
1398 * target is already empty and the perms are set. So we still
1399 * proceed with the sanity checks and let the remaining
1400 * operations fail with EROFS if they try to modify the target
1401 * file. */
1402
Franck Bui14ab8042018-04-13 17:50:49 +02001403 fd = openat(dir_fd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH, i->mode);
Franck Bui5ec9d062018-04-13 17:31:22 +02001404 if (fd < 0) {
1405 if (errno == ENOENT) {
1406 log_error("Cannot create file %s on a read-only file system.", path);
1407 return -EROFS;
1408 }
1409
1410 return log_error_errno(errno, "Failed to re-open file %s: %m", path);
1411 }
1412
1413 erofs = true;
1414 }
1415
1416 if (fstat(fd, &stbuf) < 0)
Michal Schmidt4a62c712014-11-28 19:57:32 +01001417 return log_error_errno(errno, "stat(%s) failed: %m", path);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -04001418
Franck Bui5ec9d062018-04-13 17:31:22 +02001419 if (!S_ISREG(stbuf.st_mode)) {
1420 log_error("%s exists and is not a regular file.", path);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -04001421 return -EEXIST;
1422 }
1423
Franck Bui5ec9d062018-04-13 17:31:22 +02001424 if (stbuf.st_size > 0) {
1425 if (ftruncate(fd, 0) < 0) {
1426 r = erofs ? -EROFS : -errno;
1427 return log_error_errno(r, "Failed to truncate file %s: %m", path);
1428 }
1429 } else
1430 st = &stbuf;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -04001431
Franck Bui5ec9d062018-04-13 17:31:22 +02001432 log_debug("\"%s\" has been created.", path);
1433
1434 if (i->argument) {
1435 log_debug("Writing to \"%s\".", path);
1436
1437 r = loop_write(fd, i->argument, strlen(i->argument), false);
1438 if (r < 0) {
1439 r = erofs ? -EROFS : r;
1440 return log_error_errno(r, "Failed to write file %s: %m", path);
1441 }
1442 }
1443
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001444 return fd_set_perms(i, fd, path, st);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -04001445}
1446
Franck Buib1f7b172018-04-12 12:19:22 +02001447static int copy_files(Item *i) {
Franck Bui16ba55a2018-04-12 12:55:22 +02001448 _cleanup_close_ int dfd = -1, fd = -1;
1449 char *bn;
Franck Buib1f7b172018-04-12 12:19:22 +02001450 int r;
1451
1452 log_debug("Copying tree \"%s\" to \"%s\".", i->argument, i->path);
1453
Franck Bui16ba55a2018-04-12 12:55:22 +02001454 bn = basename(i->path);
Franck Buib1f7b172018-04-12 12:19:22 +02001455
Franck Bui16ba55a2018-04-12 12:55:22 +02001456 /* Validate the path and use the returned directory fd for copying the
1457 * target so we're sure that the path can't be changed behind our
1458 * back. */
1459 dfd = path_open_parent_safe(i->path);
1460 if (dfd < 0)
1461 return dfd;
Franck Buib1f7b172018-04-12 12:19:22 +02001462
Franck Bui16ba55a2018-04-12 12:55:22 +02001463 r = copy_tree_at(AT_FDCWD, i->argument,
1464 dfd, bn,
1465 i->uid_set ? i->uid : UID_INVALID,
1466 i->gid_set ? i->gid : GID_INVALID,
1467 COPY_REFLINK);
Franck Buib1f7b172018-04-12 12:19:22 +02001468 if (r < 0) {
1469 struct stat a, b;
1470
Franck Bui16ba55a2018-04-12 12:55:22 +02001471 /* If the target already exists on read-only filesystems, trying
1472 * to create the target will not fail with EEXIST but with
1473 * EROFS. */
1474 if (r == -EROFS && faccessat(dfd, bn, F_OK, AT_SYMLINK_NOFOLLOW) == 0)
1475 r = -EEXIST;
1476
Franck Buib1f7b172018-04-12 12:19:22 +02001477 if (r != -EEXIST)
1478 return log_error_errno(r, "Failed to copy files to %s: %m", i->path);
1479
1480 if (stat(i->argument, &a) < 0)
1481 return log_error_errno(errno, "stat(%s) failed: %m", i->argument);
1482
Franck Bui16ba55a2018-04-12 12:55:22 +02001483 if (fstatat(dfd, bn, &b, AT_SYMLINK_NOFOLLOW) < 0)
Franck Buib1f7b172018-04-12 12:19:22 +02001484 return log_error_errno(errno, "stat(%s) failed: %m", i->path);
1485
1486 if ((a.st_mode ^ b.st_mode) & S_IFMT) {
1487 log_debug("Can't copy to %s, file exists already and is of different type", i->path);
1488 return 0;
1489 }
1490 }
1491
Franck Bui16ba55a2018-04-12 12:55:22 +02001492 fd = openat(dfd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH);
1493 if (fd < 0)
1494 return log_error_errno(errno, "Failed to openat(%s): %m", i->path);
1495
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001496 return fd_set_perms(i, fd, i->path, NULL);
Franck Buib1f7b172018-04-12 12:19:22 +02001497}
1498
Franck Bui074bd732018-04-12 18:10:57 +02001499typedef enum {
1500 CREATION_NORMAL,
1501 CREATION_EXISTING,
1502 CREATION_FORCE,
1503 _CREATION_MODE_MAX,
1504 _CREATION_MODE_INVALID = -1
1505} CreationMode;
1506
1507static const char *creation_mode_verb_table[_CREATION_MODE_MAX] = {
1508 [CREATION_NORMAL] = "Created",
1509 [CREATION_EXISTING] = "Found existing",
1510 [CREATION_FORCE] = "Created replacement",
1511};
1512
1513DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(creation_mode_verb, CreationMode);
1514
Franck Buif17a8d62018-08-06 12:29:54 +02001515static int create_directory_or_subvolume(const char *path, mode_t mode, bool subvol, CreationMode *creation) {
Franck Bui4c39d892018-04-27 11:51:14 +02001516 _cleanup_close_ int pfd = -1;
Franck Buif17a8d62018-08-06 12:29:54 +02001517 CreationMode c;
Franck Bui4c39d892018-04-27 11:51:14 +02001518 int r;
Franck Bui4ad36842018-04-26 14:54:20 +02001519
Franck Bui4c39d892018-04-27 11:51:14 +02001520 assert(path);
Franck Bui4ad36842018-04-26 14:54:20 +02001521
Franck Buif17a8d62018-08-06 12:29:54 +02001522 if (!creation)
1523 creation = &c;
1524
Franck Bui4c39d892018-04-27 11:51:14 +02001525 pfd = path_open_parent_safe(path);
1526 if (pfd < 0)
1527 return pfd;
Franck Bui4ad36842018-04-26 14:54:20 +02001528
Franck Bui4c39d892018-04-27 11:51:14 +02001529 if (subvol) {
Franck Bui4ad36842018-04-26 14:54:20 +02001530 if (btrfs_is_subvol(empty_to_root(arg_root)) <= 0)
1531
1532 /* Don't create a subvolume unless the root directory is
1533 * one, too. We do this under the assumption that if the
1534 * root directory is just a plain directory (i.e. very
1535 * light-weight), we shouldn't try to split it up into
1536 * subvolumes (i.e. more heavy-weight). Thus, chroot()
1537 * environments and suchlike will get a full brtfs
1538 * subvolume set up below their tree only if they
1539 * specifically set up a btrfs subvolume for the root
1540 * dir too. */
1541
Franck Bui4c39d892018-04-27 11:51:14 +02001542 subvol = false;
Franck Bui4ad36842018-04-26 14:54:20 +02001543 else {
Franck Bui4c39d892018-04-27 11:51:14 +02001544 RUN_WITH_UMASK((~mode) & 0777)
1545 r = btrfs_subvol_make_fd(pfd, basename(path));
Franck Bui4ad36842018-04-26 14:54:20 +02001546 }
1547 } else
1548 r = 0;
1549
Franck Bui4c39d892018-04-27 11:51:14 +02001550 if (!subvol || r == -ENOTTY)
Franck Bui4ad36842018-04-26 14:54:20 +02001551 RUN_WITH_UMASK(0000)
Franck Bui4c39d892018-04-27 11:51:14 +02001552 r = mkdirat_label(pfd, basename(path), mode);
Franck Bui4ad36842018-04-26 14:54:20 +02001553
1554 if (r < 0) {
1555 int k;
1556
1557 if (!IN_SET(r, -EEXIST, -EROFS))
Franck Bui4c39d892018-04-27 11:51:14 +02001558 return log_error_errno(r, "Failed to create directory or subvolume \"%s\": %m", path);
Franck Bui4ad36842018-04-26 14:54:20 +02001559
Franck Bui4c39d892018-04-27 11:51:14 +02001560 k = is_dir_fd(pfd);
Franck Bui4ad36842018-04-26 14:54:20 +02001561 if (k == -ENOENT && r == -EROFS)
Franck Bui4c39d892018-04-27 11:51:14 +02001562 return log_error_errno(r, "%s does not exist and cannot be created as the file system is read-only.", path);
Franck Bui4ad36842018-04-26 14:54:20 +02001563 if (k < 0)
Franck Bui4c39d892018-04-27 11:51:14 +02001564 return log_error_errno(k, "Failed to check if %s exists: %m", path);
Franck Bui4ad36842018-04-26 14:54:20 +02001565 if (!k) {
Franck Bui4c39d892018-04-27 11:51:14 +02001566 log_warning("\"%s\" already exists and is not a directory.", path);
1567 return -EEXIST;
Franck Bui4ad36842018-04-26 14:54:20 +02001568 }
1569
Franck Buif17a8d62018-08-06 12:29:54 +02001570 *creation = CREATION_EXISTING;
Franck Bui4ad36842018-04-26 14:54:20 +02001571 } else
Franck Buif17a8d62018-08-06 12:29:54 +02001572 *creation = CREATION_NORMAL;
Franck Bui4ad36842018-04-26 14:54:20 +02001573
Franck Buif17a8d62018-08-06 12:29:54 +02001574 log_debug("%s directory \"%s\".", creation_mode_verb_to_string(*creation), path);
Franck Bui4c39d892018-04-27 11:51:14 +02001575
1576 r = openat(pfd, basename(path), O_NOCTTY|O_CLOEXEC|O_DIRECTORY);
1577 if (r < 0)
Lennart Poettering09f12792018-08-06 15:44:24 +02001578 return log_error_errno(errno, "Failed to open directory '%s': %m", basename(path));
1579
Franck Bui4c39d892018-04-27 11:51:14 +02001580 return r;
1581}
1582
1583static int create_directory(Item *i, const char *path) {
1584 _cleanup_close_ int fd = -1;
1585
1586 assert(i);
1587 assert(IN_SET(i->type, CREATE_DIRECTORY, TRUNCATE_DIRECTORY));
1588
Franck Buif17a8d62018-08-06 12:29:54 +02001589 fd = create_directory_or_subvolume(path, i->mode, false, NULL);
Franck Bui4c39d892018-04-27 11:51:14 +02001590 if (fd == -EEXIST)
1591 return 0;
1592 if (fd < 0)
1593 return fd;
1594
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001595 return fd_set_perms(i, fd, path, NULL);
Franck Bui4c39d892018-04-27 11:51:14 +02001596}
1597
1598static int create_subvolume(Item *i, const char *path) {
1599 _cleanup_close_ int fd = -1;
Franck Buif17a8d62018-08-06 12:29:54 +02001600 CreationMode creation;
Franck Bui4c39d892018-04-27 11:51:14 +02001601 int r, q = 0;
1602
1603 assert(i);
1604 assert(IN_SET(i->type, CREATE_SUBVOLUME, CREATE_SUBVOLUME_NEW_QUOTA, CREATE_SUBVOLUME_INHERIT_QUOTA));
1605
Franck Buif17a8d62018-08-06 12:29:54 +02001606 fd = create_directory_or_subvolume(path, i->mode, true, &creation);
Franck Bui4c39d892018-04-27 11:51:14 +02001607 if (fd == -EEXIST)
1608 return 0;
1609 if (fd < 0)
1610 return fd;
Franck Bui4ad36842018-04-26 14:54:20 +02001611
Franck Buif17a8d62018-08-06 12:29:54 +02001612 if (creation == CREATION_NORMAL &&
1613 IN_SET(i->type, CREATE_SUBVOLUME_NEW_QUOTA, CREATE_SUBVOLUME_INHERIT_QUOTA)) {
Franck Bui4c39d892018-04-27 11:51:14 +02001614 r = btrfs_subvol_auto_qgroup_fd(fd, 0, i->type == CREATE_SUBVOLUME_NEW_QUOTA);
Franck Bui4ad36842018-04-26 14:54:20 +02001615 if (r == -ENOTTY)
1616 log_debug_errno(r, "Couldn't adjust quota for subvolume \"%s\" (unsupported fs or dir not a subvolume): %m", i->path);
1617 else if (r == -EROFS)
1618 log_debug_errno(r, "Couldn't adjust quota for subvolume \"%s\" (fs is read-only).", i->path);
1619 else if (r == -ENOPROTOOPT)
1620 log_debug_errno(r, "Couldn't adjust quota for subvolume \"%s\" (quota support is disabled).", i->path);
1621 else if (r < 0)
1622 q = log_error_errno(r, "Failed to adjust quota for subvolume \"%s\": %m", i->path);
1623 else if (r > 0)
1624 log_debug("Adjusted quota for subvolume \"%s\".", i->path);
1625 else if (r == 0)
1626 log_debug("Quota for subvolume \"%s\" already in place, no change made.", i->path);
1627 }
1628
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001629 r = fd_set_perms(i, fd, path, NULL);
1630 if (q < 0) /* prefer the quota change error from above */
Franck Bui4ad36842018-04-26 14:54:20 +02001631 return q;
1632
1633 return r;
1634}
1635
Franck Bui54946022018-04-26 15:45:17 +02001636static int empty_directory(Item *i, const char *path) {
1637 int r;
1638
1639 assert(i);
1640 assert(i->type == EMPTY_DIRECTORY);
1641
1642 r = is_dir(path, false);
1643 if (r == -ENOENT) {
1644 /* Option "e" operates only on existing objects. Do not
1645 * print errors about non-existent files or directories */
1646 log_debug("Skipping missing directory: %s", path);
1647 return 0;
1648 }
1649 if (r < 0)
1650 return log_error_errno(r, "is_dir() failed on path %s: %m", path);
1651 if (r == 0) {
1652 log_error("'%s' already exists and is not a directory.", path);
1653 return -EEXIST;
1654 }
1655
1656 return path_set_perms(i, path);
1657}
1658
Franck Bui074bd732018-04-12 18:10:57 +02001659static int create_device(Item *i, mode_t file_type) {
Franck Buic7700a72018-04-12 18:33:54 +02001660 _cleanup_close_ int dfd = -1, fd = -1;
Franck Bui074bd732018-04-12 18:10:57 +02001661 CreationMode creation;
Franck Buic7700a72018-04-12 18:33:54 +02001662 char *bn;
Franck Bui074bd732018-04-12 18:10:57 +02001663 int r;
1664
1665 assert(i);
1666 assert(IN_SET(file_type, S_IFBLK, S_IFCHR));
1667
Franck Buic7700a72018-04-12 18:33:54 +02001668 bn = basename(i->path);
1669
1670 /* Validate the path and use the returned directory fd for copying the
1671 * target so we're sure that the path can't be changed behind our
1672 * back. */
1673 dfd = path_open_parent_safe(i->path);
1674 if (dfd < 0)
1675 return dfd;
1676
Franck Bui074bd732018-04-12 18:10:57 +02001677 RUN_WITH_UMASK(0000) {
1678 mac_selinux_create_file_prepare(i->path, file_type);
Franck Buic7700a72018-04-12 18:33:54 +02001679 r = mknodat(dfd, bn, i->mode | file_type, i->major_minor);
Franck Bui074bd732018-04-12 18:10:57 +02001680 mac_selinux_create_file_clear();
1681 }
1682
1683 if (r < 0) {
Franck Buic7700a72018-04-12 18:33:54 +02001684 struct stat st;
1685
Franck Bui074bd732018-04-12 18:10:57 +02001686 if (errno == EPERM) {
1687 log_debug("We lack permissions, possibly because of cgroup configuration; "
1688 "skipping creation of device node %s.", i->path);
1689 return 0;
1690 }
1691
1692 if (errno != EEXIST)
1693 return log_error_errno(errno, "Failed to create device node %s: %m", i->path);
1694
Franck Buic7700a72018-04-12 18:33:54 +02001695 if (fstatat(dfd, bn, &st, 0) < 0)
Franck Bui074bd732018-04-12 18:10:57 +02001696 return log_error_errno(errno, "stat(%s) failed: %m", i->path);
1697
1698 if ((st.st_mode & S_IFMT) != file_type) {
1699
1700 if (i->force) {
1701
1702 RUN_WITH_UMASK(0000) {
1703 mac_selinux_create_file_prepare(i->path, file_type);
Franck Buic7700a72018-04-12 18:33:54 +02001704 /* FIXME: need to introduce mknodat_atomic() */
Franck Bui074bd732018-04-12 18:10:57 +02001705 r = mknod_atomic(i->path, i->mode | file_type, i->major_minor);
1706 mac_selinux_create_file_clear();
1707 }
1708
1709 if (r < 0)
1710 return log_error_errno(r, "Failed to create device node \"%s\": %m", i->path);
1711 creation = CREATION_FORCE;
1712 } else {
1713 log_debug("%s is not a device node.", i->path);
1714 return 0;
1715 }
1716 } else
1717 creation = CREATION_EXISTING;
1718 } else
1719 creation = CREATION_NORMAL;
1720
1721 log_debug("%s %s device node \"%s\" %u:%u.",
1722 creation_mode_verb_to_string(creation),
1723 i->type == CREATE_BLOCK_DEVICE ? "block" : "char",
1724 i->path, major(i->mode), minor(i->mode));
1725
Franck Buic7700a72018-04-12 18:33:54 +02001726 fd = openat(dfd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH);
1727 if (fd < 0)
1728 return log_error_errno(errno, "Failed to openat(%s): %m", i->path);
1729
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001730 return fd_set_perms(i, fd, i->path, NULL);
Franck Bui074bd732018-04-12 18:10:57 +02001731}
1732
Franck Buia2fc2f82018-04-27 18:11:26 +02001733static int create_fifo(Item *i, const char *path) {
Franck Bui7ea5a872018-04-27 18:17:32 +02001734 _cleanup_close_ int pfd = -1, fd = -1;
Franck Buia2fc2f82018-04-27 18:11:26 +02001735 CreationMode creation;
1736 struct stat st;
Franck Bui7ea5a872018-04-27 18:17:32 +02001737 char *bn;
Franck Buia2fc2f82018-04-27 18:11:26 +02001738 int r;
1739
Franck Bui7ea5a872018-04-27 18:17:32 +02001740 pfd = path_open_parent_safe(path);
1741 if (pfd < 0)
1742 return pfd;
1743
1744 bn = basename(path);
1745
Franck Buia2fc2f82018-04-27 18:11:26 +02001746 RUN_WITH_UMASK(0000) {
1747 mac_selinux_create_file_prepare(path, S_IFIFO);
Franck Bui7ea5a872018-04-27 18:17:32 +02001748 r = mkfifoat(pfd, bn, i->mode);
Franck Buia2fc2f82018-04-27 18:11:26 +02001749 mac_selinux_create_file_clear();
1750 }
1751
1752 if (r < 0) {
1753 if (errno != EEXIST)
1754 return log_error_errno(errno, "Failed to create fifo %s: %m", path);
1755
Franck Bui7ea5a872018-04-27 18:17:32 +02001756 if (fstatat(pfd, bn, &st, AT_SYMLINK_NOFOLLOW) < 0)
Franck Buia2fc2f82018-04-27 18:11:26 +02001757 return log_error_errno(errno, "stat(%s) failed: %m", path);
1758
1759 if (!S_ISFIFO(st.st_mode)) {
1760
1761 if (i->force) {
1762 RUN_WITH_UMASK(0000) {
1763 mac_selinux_create_file_prepare(path, S_IFIFO);
Franck Bui7ea5a872018-04-27 18:17:32 +02001764 r = mkfifoat_atomic(pfd, bn, i->mode);
Franck Buia2fc2f82018-04-27 18:11:26 +02001765 mac_selinux_create_file_clear();
1766 }
1767
1768 if (r < 0)
1769 return log_error_errno(r, "Failed to create fifo %s: %m", path);
1770 creation = CREATION_FORCE;
1771 } else {
1772 log_warning("\"%s\" already exists and is not a fifo.", path);
1773 return 0;
1774 }
1775 } else
1776 creation = CREATION_EXISTING;
1777 } else
1778 creation = CREATION_NORMAL;
Franck Bui7ea5a872018-04-27 18:17:32 +02001779
Franck Buia2fc2f82018-04-27 18:11:26 +02001780 log_debug("%s fifo \"%s\".", creation_mode_verb_to_string(creation), path);
1781
Franck Bui7ea5a872018-04-27 18:17:32 +02001782 fd = openat(pfd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH);
1783 if (fd < 0)
Lennart Poetteringa5df05f2018-08-06 15:46:01 +02001784 return log_error_errno(errno, "Failed to openat(%s): %m", path);
Franck Bui7ea5a872018-04-27 18:17:32 +02001785
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001786 return fd_set_perms(i, fd, i->path, NULL);
Franck Buia2fc2f82018-04-27 18:11:26 +02001787}
1788
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001789typedef int (*action_t)(Item *i, const char *path);
1790typedef int (*fdaction_t)(Item *i, int fd, const char *path, const struct stat *st);
Zbigniew Jędrzejewski-Szmek081043c2015-01-18 01:33:39 -05001791
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001792static int item_do(Item *i, int fd, const char *path, fdaction_t action) {
Franck Bui14f34802018-05-24 14:17:07 +02001793 struct stat st;
Franck Bui936f6bd2018-03-02 17:19:32 +01001794 int r = 0, q;
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001795
1796 assert(i);
Franck Bui4dc7bfd2018-08-20 17:23:12 +02001797 assert(path);
Franck Bui936f6bd2018-03-02 17:19:32 +01001798 assert(fd >= 0);
Franck Bui14f34802018-05-24 14:17:07 +02001799
1800 if (fstat(fd, &st) < 0) {
Lennart Poettering09f12792018-08-06 15:44:24 +02001801 r = log_error_errno(errno, "fstat() on file failed: %m");
Franck Bui14f34802018-05-24 14:17:07 +02001802 goto finish;
1803 }
Michal Schmidta8d88782011-12-15 23:11:07 +01001804
1805 /* This returns the first error we run into, but nevertheless
1806 * tries to go on */
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001807 r = action(i, fd, path, &st);
Michal Schmidta8d88782011-12-15 23:11:07 +01001808
Franck Bui14f34802018-05-24 14:17:07 +02001809 if (S_ISDIR(st.st_mode)) {
Zbigniew Jędrzejewski-Szmek22dd8d32018-05-11 11:09:37 +02001810 char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
Franck Bui936f6bd2018-03-02 17:19:32 +01001811 _cleanup_closedir_ DIR *d = NULL;
1812 struct dirent *de;
Michal Schmidta8d88782011-12-15 23:11:07 +01001813
Franck Bui936f6bd2018-03-02 17:19:32 +01001814 /* The passed 'fd' was opened with O_PATH. We need to convert
1815 * it into a 'regular' fd before reading the directory content. */
1816 xsprintf(procfs_path, "/proc/self/fd/%i", fd);
Michal Schmidta8d88782011-12-15 23:11:07 +01001817
Franck Bui936f6bd2018-03-02 17:19:32 +01001818 d = opendir(procfs_path);
1819 if (!d) {
Lennart Poettering09f12792018-08-06 15:44:24 +02001820 log_error_errno(errno, "Failed to opendir() '%s': %m", procfs_path);
1821 if (r == 0)
1822 r = -errno;
Franck Bui936f6bd2018-03-02 17:19:32 +01001823 goto finish;
1824 }
Michal Schmidta8d88782011-12-15 23:11:07 +01001825
Franck Bui936f6bd2018-03-02 17:19:32 +01001826 FOREACH_DIRENT_ALL(de, d, q = -errno; goto finish) {
Franck Bui936f6bd2018-03-02 17:19:32 +01001827 int de_fd;
Michal Schmidta8d88782011-12-15 23:11:07 +01001828
Franck Bui936f6bd2018-03-02 17:19:32 +01001829 if (dot_or_dot_dot(de->d_name))
1830 continue;
Michal Schmidta8d88782011-12-15 23:11:07 +01001831
Franck Bui936f6bd2018-03-02 17:19:32 +01001832 de_fd = openat(fd, de->d_name, O_NOFOLLOW|O_CLOEXEC|O_PATH);
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001833 if (de_fd < 0)
1834 q = log_error_errno(errno, "Failed to open() file '%s': %m", de->d_name);
Franck Bui4dc7bfd2018-08-20 17:23:12 +02001835 else {
1836 _cleanup_free_ char *de_path = NULL;
1837
1838 de_path = path_join(NULL, path, de->d_name);
1839 if (!de_path)
1840 q = log_oom();
1841 else
1842 /* Pass ownership of dirent fd over */
1843 q = item_do(i, de_fd, de_path, action);
1844 }
Franck Bui936f6bd2018-03-02 17:19:32 +01001845
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001846 if (q < 0 && r == 0)
1847 r = q;
Michal Schmidta8d88782011-12-15 23:11:07 +01001848 }
Michal Schmidta8d88782011-12-15 23:11:07 +01001849 }
Franck Bui936f6bd2018-03-02 17:19:32 +01001850finish:
1851 safe_close(fd);
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001852 return r;
Michal Schmidta8d88782011-12-15 23:11:07 +01001853}
1854
Franck Bui936f6bd2018-03-02 17:19:32 +01001855static int glob_item(Item *i, action_t action) {
Zbigniew Jędrzejewski-Szmekdf99a9e2014-01-02 00:02:31 -05001856 _cleanup_globfree_ glob_t g = {
Zbigniew Jędrzejewski-Szmekebf31a12015-01-26 10:39:03 -05001857 .gl_opendir = (void *(*)(const char *)) opendir_nomod,
Zbigniew Jędrzejewski-Szmekdf99a9e2014-01-02 00:02:31 -05001858 };
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001859 int r = 0, k;
Michal Schmidt99e68c02011-12-15 23:45:26 +01001860 char **fn;
1861
Zbigniew Jędrzejewski-Szmek84e72b52017-04-25 23:50:35 -04001862 k = safe_glob(i->path, GLOB_NOSORT|GLOB_BRACE, &g);
1863 if (k < 0 && k != -ENOENT)
1864 return log_error_errno(k, "glob(%s) failed: %m", i->path);
Zbigniew Jędrzejewski-Szmekc84a9482013-03-24 19:09:19 -04001865
1866 STRV_FOREACH(fn, g.gl_pathv) {
1867 k = action(i, *fn);
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001868 if (k < 0 && r == 0)
Zbigniew Jędrzejewski-Szmekc84a9482013-03-24 19:09:19 -04001869 r = k;
Franck Bui936f6bd2018-03-02 17:19:32 +01001870 }
Zbigniew Jędrzejewski-Szmek081043c2015-01-18 01:33:39 -05001871
Franck Bui936f6bd2018-03-02 17:19:32 +01001872 return r;
1873}
1874
1875static int glob_item_recursively(Item *i, fdaction_t action) {
1876 _cleanup_globfree_ glob_t g = {
1877 .gl_opendir = (void *(*)(const char *)) opendir_nomod,
1878 };
1879 int r = 0, k;
1880 char **fn;
1881
1882 k = safe_glob(i->path, GLOB_NOSORT|GLOB_BRACE, &g);
1883 if (k < 0 && k != -ENOENT)
1884 return log_error_errno(k, "glob(%s) failed: %m", i->path);
1885
1886 STRV_FOREACH(fn, g.gl_pathv) {
1887 _cleanup_close_ int fd = -1;
Franck Bui936f6bd2018-03-02 17:19:32 +01001888
1889 /* Make sure we won't trigger/follow file object (such as
1890 * device nodes, automounts, ...) pointed out by 'fn' with
1891 * O_PATH. Note, when O_PATH is used, flags other than
1892 * O_CLOEXEC, O_DIRECTORY, and O_NOFOLLOW are ignored. */
1893
1894 fd = open(*fn, O_CLOEXEC|O_NOFOLLOW|O_PATH);
1895 if (fd < 0) {
Lennart Poettering09f12792018-08-06 15:44:24 +02001896 log_error_errno(errno, "Opening '%s' failed: %m", *fn);
1897 if (r == 0)
1898 r = -errno;
Franck Bui936f6bd2018-03-02 17:19:32 +01001899 continue;
Zbigniew Jędrzejewski-Szmek081043c2015-01-18 01:33:39 -05001900 }
Franck Bui936f6bd2018-03-02 17:19:32 +01001901
Lennart Poettering9d874ae2018-08-06 15:40:16 +02001902 k = item_do(i, fd, *fn, action);
Franck Bui936f6bd2018-03-02 17:19:32 +01001903 if (k < 0 && r == 0)
1904 r = k;
1905
1906 /* we passed fd ownership to the previous call */
1907 fd = -1;
Michal Schmidt99e68c02011-12-15 23:45:26 +01001908 }
1909
Michal Schmidt99e68c02011-12-15 23:45:26 +01001910 return r;
1911}
1912
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001913static int create_item(Item *i) {
Zbigniew Jędrzejewski-Szmek294929f2015-02-01 12:29:27 -05001914 CreationMode creation;
Franck Buia2fc2f82018-04-27 18:11:26 +02001915 int r = 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001916
1917 assert(i);
1918
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001919 log_debug("Running create action for entry %c %s", (char) i->type, i->path);
1920
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001921 switch (i->type) {
1922
1923 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +01001924 case IGNORE_DIRECTORY_PATH:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001925 case REMOVE_PATH:
1926 case RECURSIVE_REMOVE_PATH:
1927 return 0;
1928
1929 case CREATE_FILE:
Lennart Poettering7fa10742018-01-23 14:14:19 +01001930 RUN_WITH_UMASK(0000)
1931 (void) mkdir_parents_label(i->path, 0755);
1932
Franck Bui31c84ff2018-04-13 09:19:24 +02001933 r = create_file(i, i->path);
Dave Reisner1845fdd2012-09-27 20:48:13 -04001934 if (r < 0)
1935 return r;
1936 break;
Lennart Poettering265ffa12013-09-17 16:33:30 -05001937
Franck Bui5ec9d062018-04-13 17:31:22 +02001938 case TRUNCATE_FILE:
1939 RUN_WITH_UMASK(0000)
1940 (void) mkdir_parents_label(i->path, 0755);
1941
1942 r = truncate_file(i, i->path);
1943 if (r < 0)
1944 return r;
1945 break;
1946
Franck Bui7b887f22018-04-12 12:18:19 +02001947 case COPY_FILES:
Lennart Poettering7fa10742018-01-23 14:14:19 +01001948 RUN_WITH_UMASK(0000)
1949 (void) mkdir_parents_label(i->path, 0755);
1950
Franck Buib1f7b172018-04-12 12:19:22 +02001951 r = copy_files(i);
Lennart Poettering849958d2014-06-10 23:02:40 +02001952 if (r < 0)
1953 return r;
Lennart Poettering849958d2014-06-10 23:02:40 +02001954 break;
1955
Dave Reisnerd4e9eb92012-09-03 17:13:18 -04001956 case WRITE_FILE:
Franck Bui936f6bd2018-03-02 17:19:32 +01001957 r = glob_item(i, write_one_file);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +01001958 if (r < 0)
1959 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001960
1961 break;
1962
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001963 case CREATE_DIRECTORY:
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +01001964 case TRUNCATE_DIRECTORY:
Franck Bui4c39d892018-04-27 11:51:14 +02001965 RUN_WITH_UMASK(0000)
1966 (void) mkdir_parents_label(i->path, 0755);
1967
1968 r = create_directory(i, i->path);
1969 if (r < 0)
1970 return r;
1971 break;
1972
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +01001973 case CREATE_SUBVOLUME:
Lennart Poettering5fb13eb2015-10-21 19:46:23 +02001974 case CREATE_SUBVOLUME_INHERIT_QUOTA:
1975 case CREATE_SUBVOLUME_NEW_QUOTA:
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +01001976 RUN_WITH_UMASK(0000)
Lennart Poettering7fa10742018-01-23 14:14:19 +01001977 (void) mkdir_parents_label(i->path, 0755);
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +01001978
Franck Bui4c39d892018-04-27 11:51:14 +02001979 r = create_subvolume(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +01001980 if (r < 0)
1981 return r;
Franck Bui4ad36842018-04-26 14:54:20 +02001982 break;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001983
Franck Bui4ad36842018-04-26 14:54:20 +02001984 case EMPTY_DIRECTORY:
Franck Bui54946022018-04-26 15:45:17 +02001985 r = glob_item(i, empty_directory);
Franck Bui4ad36842018-04-26 14:54:20 +02001986 if (r < 0)
1987 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001988 break;
Lennart Poetteringee17ee72011-07-12 03:56:56 +02001989
1990 case CREATE_FIFO:
Franck Buia2fc2f82018-04-27 18:11:26 +02001991 RUN_WITH_UMASK(0000)
Lennart Poettering7fa10742018-01-23 14:14:19 +01001992 (void) mkdir_parents_label(i->path, 0755);
1993
Franck Buia2fc2f82018-04-27 18:11:26 +02001994 r = create_fifo(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +01001995 if (r < 0)
1996 return r;
Lennart Poetteringee17ee72011-07-12 03:56:56 +02001997 break;
Michal Schmidta8d88782011-12-15 23:11:07 +01001998
Lennart Poettering5c5ccf12015-04-10 18:07:04 +02001999 case CREATE_SYMLINK: {
Lennart Poettering7fa10742018-01-23 14:14:19 +01002000 RUN_WITH_UMASK(0000)
2001 (void) mkdir_parents_label(i->path, 0755);
2002
Lennart Poetteringecabcf82014-10-23 19:41:27 +02002003 mac_selinux_create_file_prepare(i->path, S_IFLNK);
Franck Bui4cef1922017-11-16 11:27:29 +01002004 r = symlink(i->argument, i->path);
Lennart Poetteringecabcf82014-10-23 19:41:27 +02002005 mac_selinux_create_file_clear();
Kay Sieverse9a5ef72012-04-17 16:05:03 +02002006
Lennart Poettering468d7262012-01-17 15:04:12 +01002007 if (r < 0) {
Lennart Poettering2e78fa72014-06-16 13:21:07 +02002008 _cleanup_free_ char *x = NULL;
Lennart Poettering468d7262012-01-17 15:04:12 +01002009
Michal Schmidt4a62c712014-11-28 19:57:32 +01002010 if (errno != EEXIST)
Franck Bui4cef1922017-11-16 11:27:29 +01002011 return log_error_errno(errno, "symlink(%s, %s) failed: %m", i->argument, i->path);
Lennart Poettering2e78fa72014-06-16 13:21:07 +02002012
2013 r = readlink_malloc(i->path, &x);
Franck Bui4cef1922017-11-16 11:27:29 +01002014 if (r < 0 || !streq(i->argument, x)) {
Lennart Poettering2e78fa72014-06-16 13:21:07 +02002015
2016 if (i->force) {
Lennart Poetteringecabcf82014-10-23 19:41:27 +02002017 mac_selinux_create_file_prepare(i->path, S_IFLNK);
Franck Bui4cef1922017-11-16 11:27:29 +01002018 r = symlink_atomic(i->argument, i->path);
Lennart Poetteringecabcf82014-10-23 19:41:27 +02002019 mac_selinux_create_file_clear();
Lennart Poettering2e78fa72014-06-16 13:21:07 +02002020
Ricardo Salveti de Araujo2ef5de12018-04-03 10:05:11 -03002021 if (IN_SET(r, -EISDIR, -EEXIST, -ENOTEMPTY)) {
William Douglasb3f58972017-08-09 08:53:03 -07002022 r = rm_rf(i->path, REMOVE_ROOT|REMOVE_PHYSICAL);
2023 if (r < 0)
2024 return log_error_errno(r, "rm -fr %s failed: %m", i->path);
2025
2026 mac_selinux_create_file_prepare(i->path, S_IFLNK);
Franck Bui4cef1922017-11-16 11:27:29 +01002027 r = symlink(i->argument, i->path) < 0 ? -errno : 0;
William Douglasb3f58972017-08-09 08:53:03 -07002028 mac_selinux_create_file_clear();
2029 }
Michal Schmidtf6479622014-11-28 18:50:43 +01002030 if (r < 0)
Franck Bui4cef1922017-11-16 11:27:29 +01002031 return log_error_errno(r, "symlink(%s, %s) failed: %m", i->argument, i->path);
Lennart Poetteringa542c4d2015-05-15 21:48:20 +02002032
Zbigniew Jędrzejewski-Szmek294929f2015-02-01 12:29:27 -05002033 creation = CREATION_FORCE;
Lennart Poettering1554afa2014-06-17 23:50:22 +02002034 } else {
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002035 log_debug("\"%s\" is not a symlink or does not point to the correct path.", i->path);
Lennart Poettering1554afa2014-06-17 23:50:22 +02002036 return 0;
2037 }
Zbigniew Jędrzejewski-Szmek294929f2015-02-01 12:29:27 -05002038 } else
2039 creation = CREATION_EXISTING;
2040 } else
Lennart Poetteringa542c4d2015-05-15 21:48:20 +02002041
Zbigniew Jędrzejewski-Szmek294929f2015-02-01 12:29:27 -05002042 creation = CREATION_NORMAL;
Lennart Poettering7a7d5db2015-02-02 21:34:09 +01002043 log_debug("%s symlink \"%s\".", creation_mode_verb_to_string(creation), i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +01002044 break;
Lennart Poettering5c5ccf12015-04-10 18:07:04 +02002045 }
Lennart Poettering468d7262012-01-17 15:04:12 +01002046
2047 case CREATE_BLOCK_DEVICE:
Franck Bui074bd732018-04-12 18:10:57 +02002048 case CREATE_CHAR_DEVICE:
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -07002049 if (have_effective_cap(CAP_MKNOD) == 0) {
Lennart Poettering713998c2018-08-06 15:46:32 +02002050 /* In a container we lack CAP_MKNOD. We shouldn't attempt to create the device node in that
2051 * case to avoid noise, and we don't support virtualized devices in containers anyway. */
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -07002052
2053 log_debug("We lack CAP_MKNOD, skipping creation of device node %s.", i->path);
2054 return 0;
2055 }
2056
Lennart Poettering7fa10742018-01-23 14:14:19 +01002057 RUN_WITH_UMASK(0000)
2058 (void) mkdir_parents_label(i->path, 0755);
2059
Franck Bui074bd732018-04-12 18:10:57 +02002060 r = create_device(i, i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR);
Lennart Poettering468d7262012-01-17 15:04:12 +01002061 if (r < 0)
2062 return r;
2063
2064 break;
Lennart Poettering468d7262012-01-17 15:04:12 +01002065
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02002066 case ADJUST_MODE:
Michal Schmidt777b87e2011-12-16 18:27:35 +01002067 case RELABEL_PATH:
Franck Bui936f6bd2018-03-02 17:19:32 +01002068 r = glob_item(i, path_set_perms);
Michal Schmidt777b87e2011-12-16 18:27:35 +01002069 if (r < 0)
Lennart Poettering96ca8192013-06-21 15:57:42 +02002070 return r;
Michal Schmidt777b87e2011-12-16 18:27:35 +01002071 break;
2072
Michal Schmidta8d88782011-12-15 23:11:07 +01002073 case RECURSIVE_RELABEL_PATH:
Franck Bui936f6bd2018-03-02 17:19:32 +01002074 r = glob_item_recursively(i, fd_set_perms);
Michal Schmidta8d88782011-12-15 23:11:07 +01002075 if (r < 0)
2076 return r;
Maciej Wereskiebf4e802014-12-04 10:32:10 +01002077 break;
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02002078
Maciej Wereskiebf4e802014-12-04 10:32:10 +01002079 case SET_XATTR:
Franck Bui936f6bd2018-03-02 17:19:32 +01002080 r = glob_item(i, path_set_xattrs);
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -05002081 if (r < 0)
2082 return r;
2083 break;
2084
2085 case RECURSIVE_SET_XATTR:
Franck Bui936f6bd2018-03-02 17:19:32 +01002086 r = glob_item_recursively(i, fd_set_xattrs);
Maciej Wereskiebf4e802014-12-04 10:32:10 +01002087 if (r < 0)
2088 return r;
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02002089 break;
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -05002090
2091 case SET_ACL:
Franck Bui936f6bd2018-03-02 17:19:32 +01002092 r = glob_item(i, path_set_acls);
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -05002093 if (r < 0)
2094 return r;
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -05002095 break;
2096
2097 case RECURSIVE_SET_ACL:
Franck Bui936f6bd2018-03-02 17:19:32 +01002098 r = glob_item_recursively(i, fd_set_acls);
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -05002099 if (r < 0)
2100 return r;
2101 break;
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01002102
Lennart Poettering88ec4df2015-04-08 22:35:52 +02002103 case SET_ATTRIBUTE:
Franck Bui936f6bd2018-03-02 17:19:32 +01002104 r = glob_item(i, path_set_attribute);
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01002105 if (r < 0)
2106 return r;
2107 break;
2108
Lennart Poettering88ec4df2015-04-08 22:35:52 +02002109 case RECURSIVE_SET_ATTRIBUTE:
Franck Bui936f6bd2018-03-02 17:19:32 +01002110 r = glob_item_recursively(i, fd_set_attribute);
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01002111 if (r < 0)
2112 return r;
2113 break;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002114 }
2115
Michal Schmidtf05bc3f2011-12-15 23:44:23 +01002116 return 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002117}
2118
Michal Schmidta0896122011-12-15 21:32:50 +01002119static int remove_item_instance(Item *i, const char *instance) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002120 int r;
2121
2122 assert(i);
2123
2124 switch (i->type) {
2125
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002126 case REMOVE_PATH:
Michal Schmidt4a62c712014-11-28 19:57:32 +01002127 if (remove(instance) < 0 && errno != ENOENT)
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002128 return log_error_errno(errno, "rm(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002129
2130 break;
2131
2132 case TRUNCATE_DIRECTORY:
2133 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringd139b242012-06-20 14:31:00 +02002134 /* FIXME: we probably should use dir_cleanup() here
2135 * instead of rm_rf() so that 'x' is honoured. */
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002136 log_debug("rm -rf \"%s\"", instance);
Lennart Poettering1b26f092015-06-15 19:11:15 +02002137 r = rm_rf(instance, (i->type == RECURSIVE_REMOVE_PATH ? REMOVE_ROOT|REMOVE_SUBVOLUME : 0) | REMOVE_PHYSICAL);
Michal Schmidtf6479622014-11-28 18:50:43 +01002138 if (r < 0 && r != -ENOENT)
2139 return log_error_errno(r, "rm_rf(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002140
2141 break;
Zbigniew Jędrzejewski-Szmek7fcb4b92015-01-22 23:35:34 -05002142
2143 default:
2144 assert_not_reached("wut?");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002145 }
2146
2147 return 0;
2148}
2149
Michal Schmidta0896122011-12-15 21:32:50 +01002150static int remove_item(Item *i) {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01002151 assert(i);
2152
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002153 log_debug("Running remove action for entry %c %s", (char) i->type, i->path);
2154
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01002155 switch (i->type) {
2156
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01002157 case REMOVE_PATH:
2158 case TRUNCATE_DIRECTORY:
Michal Schmidt99e68c02011-12-15 23:45:26 +01002159 case RECURSIVE_REMOVE_PATH:
Franck Bui936f6bd2018-03-02 17:19:32 +01002160 return glob_item(i, remove_item_instance);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01002161
Zbigniew Jędrzejewski-Szmekdf8dee82016-04-20 00:06:25 -04002162 default:
2163 return 0;
2164 }
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01002165}
2166
Michal Sekletar78a92a52013-01-18 16:13:08 +01002167static int clean_item_instance(Item *i, const char* instance) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +02002168 _cleanup_closedir_ DIR *d = NULL;
Michal Sekletar78a92a52013-01-18 16:13:08 +01002169 struct stat s, ps;
2170 bool mountpoint;
Michal Sekletar78a92a52013-01-18 16:13:08 +01002171 usec_t cutoff, n;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002172 char timestamp[FORMAT_TIMESTAMP_MAX];
Michal Sekletar78a92a52013-01-18 16:13:08 +01002173
2174 assert(i);
2175
2176 if (!i->age_set)
2177 return 0;
2178
2179 n = now(CLOCK_REALTIME);
2180 if (n < i->age)
2181 return 0;
2182
2183 cutoff = n - i->age;
2184
Zbigniew Jędrzejewski-Szmekdf99a9e2014-01-02 00:02:31 -05002185 d = opendir_nomod(instance);
Michal Sekletar78a92a52013-01-18 16:13:08 +01002186 if (!d) {
Zbigniew Jędrzejewski-Szmekd710aaf2016-07-22 20:27:45 -04002187 if (IN_SET(errno, ENOENT, ENOTDIR)) {
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002188 log_debug_errno(errno, "Directory \"%s\": %m", instance);
Michal Sekletar78a92a52013-01-18 16:13:08 +01002189 return 0;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002190 }
Michal Sekletar78a92a52013-01-18 16:13:08 +01002191
Zbigniew Jędrzejewski-Szmekd710aaf2016-07-22 20:27:45 -04002192 return log_error_errno(errno, "Failed to open directory %s: %m", instance);
Michal Sekletar78a92a52013-01-18 16:13:08 +01002193 }
2194
Michal Schmidt4a62c712014-11-28 19:57:32 +01002195 if (fstat(dirfd(d), &s) < 0)
2196 return log_error_errno(errno, "stat(%s) failed: %m", i->path);
Michal Sekletar78a92a52013-01-18 16:13:08 +01002197
2198 if (!S_ISDIR(s.st_mode)) {
2199 log_error("%s is not a directory.", i->path);
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05002200 return -ENOTDIR;
Michal Sekletar78a92a52013-01-18 16:13:08 +01002201 }
2202
Michal Schmidt4a62c712014-11-28 19:57:32 +01002203 if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0)
2204 return log_error_errno(errno, "stat(%s/..) failed: %m", i->path);
Michal Sekletar78a92a52013-01-18 16:13:08 +01002205
Thomas Hindoe Paaboel Andersen2bb158c2015-10-23 20:15:17 +02002206 mountpoint = s.st_dev != ps.st_dev || s.st_ino == ps.st_ino;
Michal Sekletar78a92a52013-01-18 16:13:08 +01002207
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002208 log_debug("Cleanup threshold for %s \"%s\" is %s",
2209 mountpoint ? "mount point" : "directory",
2210 instance,
2211 format_timestamp_us(timestamp, sizeof(timestamp), cutoff));
2212
2213 return dir_cleanup(i, instance, d, &s, cutoff, s.st_dev, mountpoint,
2214 MAX_DEPTH, i->keep_first_level);
Michal Sekletar78a92a52013-01-18 16:13:08 +01002215}
2216
2217static int clean_item(Item *i) {
Michal Sekletar78a92a52013-01-18 16:13:08 +01002218 assert(i);
2219
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002220 log_debug("Running clean action for entry %c %s", (char) i->type, i->path);
2221
Michal Sekletar78a92a52013-01-18 16:13:08 +01002222 switch (i->type) {
2223 case CREATE_DIRECTORY:
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +01002224 case CREATE_SUBVOLUME:
Lennart Poettering5fb13eb2015-10-21 19:46:23 +02002225 case CREATE_SUBVOLUME_INHERIT_QUOTA:
2226 case CREATE_SUBVOLUME_NEW_QUOTA:
Michal Sekletar78a92a52013-01-18 16:13:08 +01002227 case TRUNCATE_DIRECTORY:
2228 case IGNORE_PATH:
Lennart Poettering849958d2014-06-10 23:02:40 +02002229 case COPY_FILES:
Michal Sekletar78a92a52013-01-18 16:13:08 +01002230 clean_item_instance(i, i->path);
Zbigniew Jędrzejewski-Szmekdf8dee82016-04-20 00:06:25 -04002231 return 0;
Zbigniew Jędrzejewski-Szmek65241c12017-11-22 15:16:48 +01002232 case EMPTY_DIRECTORY:
Michal Sekletar78a92a52013-01-18 16:13:08 +01002233 case IGNORE_DIRECTORY_PATH:
Franck Bui936f6bd2018-03-02 17:19:32 +01002234 return glob_item(i, clean_item_instance);
Michal Sekletar78a92a52013-01-18 16:13:08 +01002235 default:
Zbigniew Jędrzejewski-Szmekdf8dee82016-04-20 00:06:25 -04002236 return 0;
Michal Sekletar78a92a52013-01-18 16:13:08 +01002237 }
Michal Sekletar78a92a52013-01-18 16:13:08 +01002238}
2239
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002240static int process_item_array(ItemArray *array);
2241
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002242static int process_item(Item *i) {
Lennart Poettering1e958932015-01-06 20:33:46 +01002243 int r, q, p, t = 0;
Zbigniew Jędrzejewski-Szmek9348f0e2014-10-01 07:33:22 -05002244 _cleanup_free_ char *prefix = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002245
2246 assert(i);
2247
Lennart Poettering1910cd02014-06-11 01:37:35 +02002248 if (i->done)
2249 return 0;
2250
2251 i->done = true;
2252
Zbigniew Jędrzejewski-Szmek9348f0e2014-10-01 07:33:22 -05002253 prefix = malloc(strlen(i->path) + 1);
2254 if (!prefix)
2255 return log_oom();
2256
Lennart Poettering1910cd02014-06-11 01:37:35 +02002257 PATH_FOREACH_PREFIX(prefix, i->path) {
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002258 ItemArray *j;
Lennart Poettering1910cd02014-06-11 01:37:35 +02002259
Lennart Poetteringef43a392015-04-22 18:18:56 +02002260 j = ordered_hashmap_get(items, prefix);
Zbigniew Jędrzejewski-Szmek1db50422015-01-06 09:53:12 -05002261 if (j) {
2262 int s;
2263
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002264 s = process_item_array(j);
Zbigniew Jędrzejewski-Szmek1db50422015-01-06 09:53:12 -05002265 if (s < 0 && t == 0)
2266 t = s;
2267 }
Lennart Poettering1910cd02014-06-11 01:37:35 +02002268 }
2269
NeilBrown655f2da2017-09-04 23:35:07 +10002270 if (chase_symlinks(i->path, NULL, CHASE_NO_AUTOFS, NULL) == -EREMOTE)
2271 return t;
2272
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002273 r = arg_create ? create_item(i) : 0;
Michal Schmidta0896122011-12-15 21:32:50 +01002274 q = arg_remove ? remove_item(i) : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002275 p = arg_clean ? clean_item(i) : 0;
William Douglas6d7b5432018-09-10 12:07:29 -07002276 /* Failure can only be tolerated for create */
2277 if (i->allow_failure)
2278 r = 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002279
Zbigniew Jędrzejewski-Szmek1db50422015-01-06 09:53:12 -05002280 return t < 0 ? t :
2281 r < 0 ? r :
2282 q < 0 ? q :
2283 p;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002284}
2285
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002286static int process_item_array(ItemArray *array) {
2287 unsigned n;
2288 int r = 0, k;
Lennart Poettering753615e2014-06-12 23:07:17 +02002289
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002290 assert(array);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002291
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002292 for (n = 0; n < array->count; n++) {
2293 k = process_item(array->items + n);
2294 if (k < 0 && r == 0)
2295 r = k;
2296 }
2297
2298 return r;
2299}
2300
2301static void item_free_contents(Item *i) {
2302 assert(i);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002303 free(i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +01002304 free(i->argument);
Maciej Wereskiebf4e802014-12-04 10:32:10 +01002305 strv_free(i->xattrs);
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -05002306
Zbigniew Jędrzejewski-Szmek349cc4a2017-10-03 10:41:51 +02002307#if HAVE_ACL
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -05002308 acl_free(i->acl_access);
2309 acl_free(i->acl_default);
2310#endif
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002311}
2312
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002313static void item_array_free(ItemArray *a) {
2314 unsigned n;
Maciej Wereskie2f2fb72013-07-19 15:43:12 +02002315
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002316 if (!a)
2317 return;
2318
2319 for (n = 0; n < a->count; n++)
2320 item_free_contents(a->items + n);
2321 free(a->items);
2322 free(a);
2323}
2324
Yu Watanabe93bab282018-09-18 08:39:24 +09002325static int item_compare(const Item *a, const Item *b) {
Lennart Poettering17493fa2015-04-10 16:22:22 +02002326 /* Make sure that the ownership taking item is put first, so
2327 * that we first create the node, and then can adjust it */
2328
Yu Watanabe93bab282018-09-18 08:39:24 +09002329 if (takes_ownership(a->type) && !takes_ownership(b->type))
Lennart Poettering17493fa2015-04-10 16:22:22 +02002330 return -1;
Yu Watanabe93bab282018-09-18 08:39:24 +09002331 if (!takes_ownership(a->type) && takes_ownership(b->type))
Lennart Poettering17493fa2015-04-10 16:22:22 +02002332 return 1;
2333
Yu Watanabe93bab282018-09-18 08:39:24 +09002334 return CMP(a->type, b->type);
Lennart Poettering17493fa2015-04-10 16:22:22 +02002335}
2336
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002337static bool item_compatible(Item *a, Item *b) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02002338 assert(a);
2339 assert(b);
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002340 assert(streq(a->path, b->path));
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02002341
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002342 if (takes_ownership(a->type) && takes_ownership(b->type))
2343 /* check if the items are the same */
2344 return streq_ptr(a->argument, b->argument) &&
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02002345
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002346 a->uid_set == b->uid_set &&
2347 a->uid == b->uid &&
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02002348
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002349 a->gid_set == b->gid_set &&
2350 a->gid == b->gid &&
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02002351
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002352 a->mode_set == b->mode_set &&
2353 a->mode == b->mode &&
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02002354
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002355 a->age_set == b->age_set &&
2356 a->age == b->age &&
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02002357
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002358 a->mask_perms == b->mask_perms &&
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02002359
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002360 a->keep_first_level == b->keep_first_level &&
Lennart Poettering468d7262012-01-17 15:04:12 +01002361
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002362 a->major_minor == b->major_minor;
Lennart Poettering468d7262012-01-17 15:04:12 +01002363
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02002364 return true;
2365}
2366
Dave Reisnera2aced42013-07-24 11:10:05 -04002367static bool should_include_path(const char *path) {
2368 char **prefix;
2369
Lennart Poetteringabef3f92014-06-11 10:14:07 +02002370 STRV_FOREACH(prefix, arg_exclude_prefixes)
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002371 if (path_startswith(path, *prefix)) {
2372 log_debug("Entry \"%s\" matches exclude prefix \"%s\", skipping.",
2373 path, *prefix);
Dave Reisner5c795112013-07-24 11:19:24 -04002374 return false;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002375 }
Dave Reisnera2aced42013-07-24 11:10:05 -04002376
Lennart Poetteringabef3f92014-06-11 10:14:07 +02002377 STRV_FOREACH(prefix, arg_include_prefixes)
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002378 if (path_startswith(path, *prefix)) {
2379 log_debug("Entry \"%s\" matches include prefix \"%s\".", path, *prefix);
Dave Reisnera2aced42013-07-24 11:10:05 -04002380 return true;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002381 }
Dave Reisnera2aced42013-07-24 11:10:05 -04002382
Dave Reisner5c795112013-07-24 11:19:24 -04002383 /* no matches, so we should include this path only if we
2384 * have no whitelist at all */
Lennart Poettering7b943bb2017-11-29 12:40:22 +01002385 if (strv_isempty(arg_include_prefixes))
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002386 return true;
2387
2388 log_debug("Entry \"%s\" does not match any include prefix, skipping.", path);
2389 return false;
Dave Reisnera2aced42013-07-24 11:10:05 -04002390}
2391
Franck Bui4cef1922017-11-16 11:27:29 +01002392static int specifier_expansion_from_arg(Item *i) {
2393 _cleanup_free_ char *unescaped = NULL, *resolved = NULL;
2394 char **xattr;
2395 int r;
2396
2397 assert(i);
2398
2399 if (i->argument == NULL)
2400 return 0;
2401
2402 switch (i->type) {
2403 case COPY_FILES:
2404 case CREATE_SYMLINK:
2405 case CREATE_FILE:
2406 case TRUNCATE_FILE:
2407 case WRITE_FILE:
2408 r = cunescape(i->argument, 0, &unescaped);
2409 if (r < 0)
2410 return log_error_errno(r, "Failed to unescape parameter to write: %s", i->argument);
2411
2412 r = specifier_printf(unescaped, specifier_table, NULL, &resolved);
2413 if (r < 0)
2414 return r;
2415
2416 free_and_replace(i->argument, resolved);
2417 break;
2418
2419 case SET_XATTR:
2420 case RECURSIVE_SET_XATTR:
2421 assert(i->xattrs);
2422
2423 STRV_FOREACH (xattr, i->xattrs) {
2424 r = specifier_printf(*xattr, specifier_table, NULL, &resolved);
2425 if (r < 0)
2426 return r;
2427
2428 free_and_replace(*xattr, resolved);
2429 }
2430 break;
2431
2432 default:
2433 break;
2434 }
2435 return 0;
2436}
2437
Lennart Poetteringa2d1fb82018-05-16 22:15:46 -04002438static int patch_var_run(const char *fname, unsigned line, char **path) {
2439 const char *k;
2440 char *n;
2441
2442 assert(path);
2443 assert(*path);
2444
2445 /* Optionally rewrites lines referencing /var/run/, to use /run/ instead. Why bother? tmpfiles merges lines in
2446 * some cases and detects conflicts in others. If files/directories are specified through two equivalent lines
2447 * this is problematic as neither case will be detected. Ideally we'd detect these cases by resolving symlinks
2448 * early, but that's precisely not what we can do here as this code very likely is running very early on, at a
2449 * time where the paths in question are not available yet, or even more importantly, our own tmpfiles rules
2450 * might create the paths that are intermediary to the listed paths. We can't really cover the generic case,
2451 * but the least we can do is cover the specific case of /var/run vs. /run, as /var/run is a legacy name for
2452 * /run only, and we explicitly document that and require that on systemd systems the former is a symlink to
2453 * the latter. Moreover files below this path are by far the primary usecase for tmpfiles.d/. */
2454
2455 k = path_startswith(*path, "/var/run/");
2456 if (isempty(k)) /* Don't complain about other paths than /var/run, and not about /var/run itself either. */
2457 return 0;
2458
2459 n = strjoin("/run/", k);
2460 if (!n)
2461 return log_oom();
2462
2463 /* Also log about this briefly. We do so at LOG_NOTICE level, as we fixed up the situation automatically, hence
2464 * there's no immediate need for action by the user. However, in the interest of making things less confusing
2465 * to the user, let's still inform the user that these snippets should really be updated. */
2466
2467 log_notice("[%s:%u] Line references path below legacy directory /var/run/, updating %s → %s; please update the tmpfiles.d/ drop-in file accordingly.", fname, line, *path, n);
2468
2469 free(*path);
2470 *path = n;
2471
2472 return 0;
2473}
2474
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002475static int parse_line(const char *fname, unsigned line, const char *buffer, bool *invalid_config) {
Lennart Poettering1731e342013-09-17 11:02:02 -05002476
Lennart Poetteringcde684a2014-06-10 22:50:46 +02002477 _cleanup_free_ char *action = NULL, *mode = NULL, *user = NULL, *group = NULL, *age = NULL, *path = NULL;
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002478 _cleanup_(item_free_contents) Item i = {};
2479 ItemArray *existing;
Lennart Poetteringef43a392015-04-22 18:18:56 +02002480 OrderedHashmap *h;
daurnimator657cf7f2015-03-09 15:11:44 -04002481 int r, pos;
William Douglas6d7b5432018-09-10 12:07:29 -07002482 bool force = false, boot = false, allow_failure = false;
Lennart Poettering5008d582010-09-28 02:34:02 +02002483
2484 assert(fname);
2485 assert(line >= 1);
2486 assert(buffer);
2487
Richard Maw68685602015-06-23 16:20:53 +00002488 r = extract_many_words(
Lennart Poettering4034a062015-03-23 18:55:36 +07002489 &buffer,
Richard Maw68685602015-06-23 16:20:53 +00002490 NULL,
Richard Maw12ba2c42015-06-23 16:26:49 +00002491 EXTRACT_QUOTES,
Lennart Poettering4034a062015-03-23 18:55:36 +07002492 &action,
2493 &path,
2494 &mode,
2495 &user,
2496 &group,
2497 &age,
2498 NULL);
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002499 if (r < 0) {
Zbigniew Jędrzejewski-Szmek751223f2017-11-24 12:19:40 +01002500 if (IN_SET(r, -EINVAL, -EBADSLT))
2501 /* invalid quoting and such or an unknown specifier */
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002502 *invalid_config = true;
daurnimator657cf7f2015-03-09 15:11:44 -04002503 return log_error_errno(r, "[%s:%u] Failed to parse line: %m", fname, line);
Lennart Poettering4b936992018-05-10 11:29:11 -07002504 } else if (r < 2) {
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002505 *invalid_config = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02002506 log_error("[%s:%u] Syntax error.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04002507 return -EIO;
Lennart Poettering5008d582010-09-28 02:34:02 +02002508 }
2509
Lennart Poetteringaa5f6812015-04-21 01:10:19 +02002510 if (!isempty(buffer) && !streq(buffer, "-")) {
Lennart Poettering4034a062015-03-23 18:55:36 +07002511 i.argument = strdup(buffer);
2512 if (!i.argument)
2513 return log_oom();
2514 }
2515
Lennart Poettering2e78fa72014-06-16 13:21:07 +02002516 if (isempty(action)) {
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002517 *invalid_config = true;
Lennart Poettering2e78fa72014-06-16 13:21:07 +02002518 log_error("[%s:%u] Command too short '%s'.", fname, line, action);
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05002519 return -EINVAL;
Lennart Poettering2e78fa72014-06-16 13:21:07 +02002520 }
2521
Zbigniew Jędrzejewski-Szmek5f255142015-01-09 01:11:01 -05002522 for (pos = 1; action[pos]; pos++) {
2523 if (action[pos] == '!' && !boot)
2524 boot = true;
2525 else if (action[pos] == '+' && !force)
2526 force = true;
William Douglas6d7b5432018-09-10 12:07:29 -07002527 else if (action[pos] == '-' && !allow_failure)
2528 allow_failure = true;
Zbigniew Jędrzejewski-Szmek5f255142015-01-09 01:11:01 -05002529 else {
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002530 *invalid_config = true;
Zbigniew Jędrzejewski-Szmek5f255142015-01-09 01:11:01 -05002531 log_error("[%s:%u] Unknown modifiers in command '%s'",
2532 fname, line, action);
2533 return -EINVAL;
2534 }
Lennart Poettering2e78fa72014-06-16 13:21:07 +02002535 }
2536
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002537 if (boot && !arg_boot) {
2538 log_debug("Ignoring entry %s \"%s\" because --boot is not specified.",
2539 action, path);
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05002540 return 0;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002541 }
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05002542
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002543 i.type = action[0];
2544 i.force = force;
William Douglas6d7b5432018-09-10 12:07:29 -07002545 i.allow_failure = allow_failure;
Lennart Poettering1731e342013-09-17 11:02:02 -05002546
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002547 r = specifier_printf(path, specifier_table, NULL, &i.path);
Zbigniew Jędrzejewski-Szmek5a8575e2017-11-23 13:56:32 +01002548 if (r == -ENXIO)
Franck Bui4cef1922017-11-16 11:27:29 +01002549 return log_unresolvable_specifier(fname, line);
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002550 if (r < 0) {
Zbigniew Jędrzejewski-Szmek751223f2017-11-24 12:19:40 +01002551 if (IN_SET(r, -EINVAL, -EBADSLT))
2552 *invalid_config = true;
Franck Bui4cef1922017-11-16 11:27:29 +01002553 return log_error_errno(r, "[%s:%u] Failed to replace specifiers: %s", fname, line, path);
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002554 }
Lennart Poettering1731e342013-09-17 11:02:02 -05002555
Lennart Poetteringa2d1fb82018-05-16 22:15:46 -04002556 r = patch_var_run(fname, line, &i.path);
2557 if (r < 0)
2558 return r;
2559
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002560 switch (i.type) {
Lennart Poettering468d7262012-01-17 15:04:12 +01002561
Michal Schmidt777b87e2011-12-16 18:27:35 +01002562 case CREATE_DIRECTORY:
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +01002563 case CREATE_SUBVOLUME:
Lennart Poettering5fb13eb2015-10-21 19:46:23 +02002564 case CREATE_SUBVOLUME_INHERIT_QUOTA:
2565 case CREATE_SUBVOLUME_NEW_QUOTA:
Zbigniew Jędrzejewski-Szmekdf8dee82016-04-20 00:06:25 -04002566 case EMPTY_DIRECTORY:
Michal Schmidt777b87e2011-12-16 18:27:35 +01002567 case TRUNCATE_DIRECTORY:
2568 case CREATE_FIFO:
2569 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +01002570 case IGNORE_DIRECTORY_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +01002571 case REMOVE_PATH:
2572 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02002573 case ADJUST_MODE:
Michal Schmidt777b87e2011-12-16 18:27:35 +01002574 case RELABEL_PATH:
2575 case RECURSIVE_RELABEL_PATH:
Lennart Poetteringc82500c2015-04-10 14:46:05 +02002576 if (i.argument)
Lennart Poetteringbd550f72015-04-10 16:03:24 +02002577 log_warning("[%s:%u] %c lines don't take argument fields, ignoring.", fname, line, i.type);
Lennart Poetteringc82500c2015-04-10 14:46:05 +02002578
2579 break;
2580
2581 case CREATE_FILE:
2582 case TRUNCATE_FILE:
Michal Schmidt777b87e2011-12-16 18:27:35 +01002583 break;
Lennart Poettering468d7262012-01-17 15:04:12 +01002584
2585 case CREATE_SYMLINK:
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002586 if (!i.argument) {
2587 i.argument = strappend("/usr/share/factory/", i.path);
2588 if (!i.argument)
Kay Sievers2f3b8732014-06-20 15:57:43 +02002589 return log_oom();
Lennart Poettering468d7262012-01-17 15:04:12 +01002590 }
2591 break;
2592
Lennart Poettering31ed59c2012-01-18 16:39:04 +01002593 case WRITE_FILE:
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002594 if (!i.argument) {
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002595 *invalid_config = true;
Lennart Poettering31ed59c2012-01-18 16:39:04 +01002596 log_error("[%s:%u] Write file requires argument.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04002597 return -EBADMSG;
Lennart Poettering31ed59c2012-01-18 16:39:04 +01002598 }
2599 break;
2600
Lennart Poettering849958d2014-06-10 23:02:40 +02002601 case COPY_FILES:
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002602 if (!i.argument) {
2603 i.argument = strappend("/usr/share/factory/", i.path);
2604 if (!i.argument)
Kay Sievers2f3b8732014-06-20 15:57:43 +02002605 return log_oom();
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002606 } else if (!path_is_absolute(i.argument)) {
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002607 *invalid_config = true;
Lennart Poettering849958d2014-06-10 23:02:40 +02002608 log_error("[%s:%u] Source path is not absolute.", fname, line);
2609 return -EBADMSG;
2610 }
2611
Yu Watanabe858d36c2018-05-31 23:39:31 +09002612 path_simplify(i.argument, false);
Lennart Poettering849958d2014-06-10 23:02:40 +02002613 break;
2614
Lennart Poettering468d7262012-01-17 15:04:12 +01002615 case CREATE_CHAR_DEVICE:
2616 case CREATE_BLOCK_DEVICE: {
2617 unsigned major, minor;
2618
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002619 if (!i.argument) {
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002620 *invalid_config = true;
Lennart Poettering468d7262012-01-17 15:04:12 +01002621 log_error("[%s:%u] Device file requires argument.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04002622 return -EBADMSG;
Lennart Poettering468d7262012-01-17 15:04:12 +01002623 }
2624
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002625 if (sscanf(i.argument, "%u:%u", &major, &minor) != 2) {
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002626 *invalid_config = true;
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002627 log_error("[%s:%u] Can't parse device file major/minor '%s'.", fname, line, i.argument);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04002628 return -EBADMSG;
Lennart Poettering468d7262012-01-17 15:04:12 +01002629 }
2630
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002631 i.major_minor = makedev(major, minor);
Lennart Poettering468d7262012-01-17 15:04:12 +01002632 break;
2633 }
2634
Maciej Wereskiebf4e802014-12-04 10:32:10 +01002635 case SET_XATTR:
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -05002636 case RECURSIVE_SET_XATTR:
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002637 if (!i.argument) {
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002638 *invalid_config = true;
Maciej Wereskiebf4e802014-12-04 10:32:10 +01002639 log_error("[%s:%u] Set extended attribute requires argument.", fname, line);
2640 return -EBADMSG;
2641 }
Lennart Poetteringbd550f72015-04-10 16:03:24 +02002642 r = parse_xattrs_from_arg(&i);
Maciej Wereskiebf4e802014-12-04 10:32:10 +01002643 if (r < 0)
2644 return r;
2645 break;
2646
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -05002647 case SET_ACL:
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -05002648 case RECURSIVE_SET_ACL:
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -05002649 if (!i.argument) {
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002650 *invalid_config = true;
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -05002651 log_error("[%s:%u] Set ACLs requires argument.", fname, line);
2652 return -EBADMSG;
2653 }
Lennart Poetteringbd550f72015-04-10 16:03:24 +02002654 r = parse_acls_from_arg(&i);
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -05002655 if (r < 0)
2656 return r;
2657 break;
2658
Lennart Poettering88ec4df2015-04-08 22:35:52 +02002659 case SET_ATTRIBUTE:
2660 case RECURSIVE_SET_ATTRIBUTE:
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01002661 if (!i.argument) {
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002662 *invalid_config = true;
Lennart Poettering88ec4df2015-04-08 22:35:52 +02002663 log_error("[%s:%u] Set file attribute requires argument.", fname, line);
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01002664 return -EBADMSG;
2665 }
Lennart Poetteringbd550f72015-04-10 16:03:24 +02002666 r = parse_attribute_from_arg(&i);
Zbigniew Jędrzejewski-Szmek751223f2017-11-24 12:19:40 +01002667 if (IN_SET(r, -EINVAL, -EBADSLT))
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002668 *invalid_config = true;
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01002669 if (r < 0)
2670 return r;
2671 break;
2672
Michal Schmidt777b87e2011-12-16 18:27:35 +01002673 default:
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002674 log_error("[%s:%u] Unknown command type '%c'.", fname, line, (char) i.type);
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002675 *invalid_config = true;
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04002676 return -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02002677 }
Lennart Poettering468d7262012-01-17 15:04:12 +01002678
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002679 if (!path_is_absolute(i.path)) {
2680 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i.path);
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002681 *invalid_config = true;
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04002682 return -EBADMSG;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002683 }
2684
Yu Watanabe858d36c2018-05-31 23:39:31 +09002685 path_simplify(i.path, false);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002686
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002687 if (!should_include_path(i.path))
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04002688 return 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02002689
Franck Bui4cef1922017-11-16 11:27:29 +01002690 r = specifier_expansion_from_arg(&i);
Zbigniew Jędrzejewski-Szmek5a8575e2017-11-23 13:56:32 +01002691 if (r == -ENXIO)
Franck Bui4cef1922017-11-16 11:27:29 +01002692 return log_unresolvable_specifier(fname, line);
Zbigniew Jędrzejewski-Szmek751223f2017-11-24 12:19:40 +01002693 if (r < 0) {
2694 if (IN_SET(r, -EINVAL, -EBADSLT))
2695 *invalid_config = true;
Franck Bui4cef1922017-11-16 11:27:29 +01002696 return log_error_errno(r, "[%s:%u] Failed to substitute specifiers in argument: %m",
2697 fname, line);
Zbigniew Jędrzejewski-Szmek751223f2017-11-24 12:19:40 +01002698 }
Franck Bui4cef1922017-11-16 11:27:29 +01002699
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07002700 if (arg_root) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +02002701 char *p;
2702
Lennart Poettering1d13f642015-05-13 17:42:10 +02002703 p = prefix_root(arg_root, i.path);
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07002704 if (!p)
2705 return log_oom();
2706
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002707 free(i.path);
2708 i.path = p;
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07002709 }
2710
Lennart Poettering90937fe2015-04-10 16:04:16 +02002711 if (!isempty(user) && !streq(user, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02002712 const char *u = user;
Lennart Poettering5008d582010-09-28 02:34:02 +02002713
Lennart Poetteringfafff8f2018-08-02 18:36:47 +02002714 r = get_user_creds(&u, &i.uid, NULL, NULL, NULL, USER_CREDS_ALLOW_MISSING);
Lennart Poettering4b678342011-07-23 01:17:59 +02002715 if (r < 0) {
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002716 *invalid_config = true;
2717 return log_error_errno(r, "[%s:%u] Unknown user '%s'.", fname, line, user);
Lennart Poettering5008d582010-09-28 02:34:02 +02002718 }
2719
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002720 i.uid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02002721 }
2722
Lennart Poettering90937fe2015-04-10 16:04:16 +02002723 if (!isempty(group) && !streq(group, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02002724 const char *g = group;
Lennart Poettering5008d582010-09-28 02:34:02 +02002725
Lennart Poetteringfafff8f2018-08-02 18:36:47 +02002726 r = get_group_creds(&g, &i.gid, USER_CREDS_ALLOW_MISSING);
Lennart Poettering4b678342011-07-23 01:17:59 +02002727 if (r < 0) {
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002728 *invalid_config = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02002729 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04002730 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02002731 }
2732
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002733 i.gid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02002734 }
2735
Lennart Poettering90937fe2015-04-10 16:04:16 +02002736 if (!isempty(mode) && !streq(mode, "-")) {
Lennart Poetteringabef3f92014-06-11 10:14:07 +02002737 const char *mm = mode;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002738 unsigned m;
Lennart Poettering5008d582010-09-28 02:34:02 +02002739
Lennart Poetteringabef3f92014-06-11 10:14:07 +02002740 if (*mm == '~') {
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002741 i.mask_perms = true;
Lennart Poetteringabef3f92014-06-11 10:14:07 +02002742 mm++;
2743 }
2744
Lennart Poettering2ff7b0a2015-04-10 14:43:06 +02002745 if (parse_mode(mm, &m) < 0) {
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002746 *invalid_config = true;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002747 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
Lennart Poettering2ff7b0a2015-04-10 14:43:06 +02002748 return -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02002749 }
2750
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002751 i.mode = m;
2752 i.mode_set = true;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002753 } else
Lennart Poettering5fb13eb2015-10-21 19:46:23 +02002754 i.mode = IN_SET(i.type, CREATE_DIRECTORY, TRUNCATE_DIRECTORY, CREATE_SUBVOLUME, CREATE_SUBVOLUME_INHERIT_QUOTA, CREATE_SUBVOLUME_NEW_QUOTA) ? 0755 : 0644;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002755
Lennart Poettering90937fe2015-04-10 16:04:16 +02002756 if (!isempty(age) && !streq(age, "-")) {
Lennart Poettering24f3a372012-06-20 09:05:50 +02002757 const char *a = age;
2758
2759 if (*a == '~') {
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002760 i.keep_first_level = true;
Lennart Poettering24f3a372012-06-20 09:05:50 +02002761 a++;
2762 }
2763
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002764 if (parse_sec(a, &i.age) < 0) {
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002765 *invalid_config = true;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002766 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04002767 return -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02002768 }
2769
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002770 i.age_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02002771 }
2772
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002773 h = needs_glob(i.type) ? globs : items;
Lennart Poettering022707d2011-01-05 16:11:15 +01002774
Lennart Poetteringef43a392015-04-22 18:18:56 +02002775 existing = ordered_hashmap_get(h, i.path);
Lennart Poettering468d7262012-01-17 15:04:12 +01002776 if (existing) {
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002777 unsigned n;
2778
2779 for (n = 0; n < existing->count; n++) {
Martin Pitt6487ada2015-03-05 14:58:56 +01002780 if (!item_compatible(existing->items + n, &i)) {
Zbigniew Jędrzejewski-Szmeke286dba2017-11-22 14:19:13 +01002781 log_notice("[%s:%u] Duplicate line for path \"%s\", ignoring.",
2782 fname, line, i.path);
Martin Pitt6487ada2015-03-05 14:58:56 +01002783 return 0;
2784 }
Maciej Wereskiebf4e802014-12-04 10:32:10 +01002785 }
2786 } else {
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002787 existing = new0(ItemArray, 1);
Lennart Poettering07982ed2018-01-22 15:32:57 +01002788 if (!existing)
2789 return log_oom();
2790
Lennart Poetteringef43a392015-04-22 18:18:56 +02002791 r = ordered_hashmap_put(h, i.path, existing);
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002792 if (r < 0)
2793 return log_oom();
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02002794 }
2795
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002796 if (!GREEDY_REALLOC(existing->items, existing->size, existing->count + 1))
2797 return log_oom();
Lennart Poettering5008d582010-09-28 02:34:02 +02002798
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002799 memcpy(existing->items + existing->count++, &i, sizeof(i));
Zbigniew Jędrzejewski-Szmekdd449ac2015-04-10 18:57:05 -04002800
2801 /* Sort item array, to enforce stable ordering of application */
Yu Watanabe93bab282018-09-18 08:39:24 +09002802 typesafe_qsort(existing->items, existing->count, item_compare);
Lennart Poettering17493fa2015-04-10 16:22:22 +02002803
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002804 zero(i);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04002805 return 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02002806}
2807
Zbigniew Jędrzejewski-Szmekceaaeb92018-04-26 19:07:54 +02002808static int cat_config(char **config_dirs, char **args) {
2809 _cleanup_strv_free_ char **files = NULL;
2810 int r;
2811
2812 r = conf_files_list_with_replacement(arg_root, config_dirs, arg_replace, &files, NULL);
2813 if (r < 0)
2814 return r;
2815
2816 return cat_files(NULL, files, 0);
2817}
2818
Lennart Poettering37ec0fd2018-08-09 10:32:31 +02002819static int help(void) {
2820 _cleanup_free_ char *link = NULL;
2821 int r;
2822
2823 r = terminal_urlify_man("systemd-tmpfiles", "8", &link);
2824 if (r < 0)
2825 return log_oom();
2826
Lennart Poettering522d4a42011-02-13 15:08:15 +01002827 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
2828 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
Dave Reisner5c795112013-07-24 11:19:24 -04002829 " -h --help Show this help\n"
Zbigniew Jędrzejewski-Szmekf2b5ca02017-11-23 11:20:29 +01002830 " --user Execute user configuration\n"
Lennart Poetteringeb9da372013-11-06 18:28:39 +01002831 " --version Show package version\n"
Zbigniew Jędrzejewski-Szmekceaaeb92018-04-26 19:07:54 +02002832 " --cat-config Show configuration files\n"
Dave Reisner5c795112013-07-24 11:19:24 -04002833 " --create Create marked files/directories\n"
2834 " --clean Clean up marked directories\n"
2835 " --remove Remove marked files/directories\n"
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05002836 " --boot Execute actions only safe at boot\n"
Zbigniew Jędrzejewski-Szmek79ca8882015-01-27 21:22:08 -05002837 " --prefix=PATH Only apply rules with the specified prefix\n"
2838 " --exclude-prefix=PATH Ignore rules with the specified prefix\n"
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01002839 " --root=PATH Operate on an alternate filesystem root\n"
Zbigniew Jędrzejewski-Szmeka6d84742018-02-05 14:53:11 +01002840 " --replace=PATH Treat arguments as replacement for PATH\n"
Lennart Poetteringdcd5c892018-06-12 15:37:53 +02002841 " --no-pager Do not pipe output into a pager\n"
Lennart Poettering37ec0fd2018-08-09 10:32:31 +02002842 "\nSee the %s for details.\n"
2843 , program_invocation_short_name
2844 , link
2845 );
2846
2847 return 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002848}
2849
2850static int parse_argv(int argc, char *argv[]) {
2851
2852 enum {
Lennart Poetteringeb9da372013-11-06 18:28:39 +01002853 ARG_VERSION = 0x100,
Zbigniew Jędrzejewski-Szmekceaaeb92018-04-26 19:07:54 +02002854 ARG_CAT_CONFIG,
Zbigniew Jędrzejewski-Szmekf2b5ca02017-11-23 11:20:29 +01002855 ARG_USER,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002856 ARG_CREATE,
2857 ARG_CLEAN,
Lennart Poetteringfba6e682011-02-13 14:00:54 +01002858 ARG_REMOVE,
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05002859 ARG_BOOT,
Dave Reisner5c795112013-07-24 11:19:24 -04002860 ARG_PREFIX,
2861 ARG_EXCLUDE_PREFIX,
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07002862 ARG_ROOT,
Zbigniew Jędrzejewski-Szmeka6d84742018-02-05 14:53:11 +01002863 ARG_REPLACE,
Lennart Poetteringdcd5c892018-06-12 15:37:53 +02002864 ARG_NO_PAGER,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002865 };
2866
2867 static const struct option options[] = {
Dave Reisner5c795112013-07-24 11:19:24 -04002868 { "help", no_argument, NULL, 'h' },
Zbigniew Jędrzejewski-Szmekf2b5ca02017-11-23 11:20:29 +01002869 { "user", no_argument, NULL, ARG_USER },
Lennart Poetteringeb9da372013-11-06 18:28:39 +01002870 { "version", no_argument, NULL, ARG_VERSION },
Zbigniew Jędrzejewski-Szmekceaaeb92018-04-26 19:07:54 +02002871 { "cat-config", no_argument, NULL, ARG_CAT_CONFIG },
Dave Reisner5c795112013-07-24 11:19:24 -04002872 { "create", no_argument, NULL, ARG_CREATE },
2873 { "clean", no_argument, NULL, ARG_CLEAN },
2874 { "remove", no_argument, NULL, ARG_REMOVE },
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05002875 { "boot", no_argument, NULL, ARG_BOOT },
Dave Reisner5c795112013-07-24 11:19:24 -04002876 { "prefix", required_argument, NULL, ARG_PREFIX },
2877 { "exclude-prefix", required_argument, NULL, ARG_EXCLUDE_PREFIX },
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07002878 { "root", required_argument, NULL, ARG_ROOT },
Zbigniew Jędrzejewski-Szmeka6d84742018-02-05 14:53:11 +01002879 { "replace", required_argument, NULL, ARG_REPLACE },
Lennart Poetteringdcd5c892018-06-12 15:37:53 +02002880 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
Lennart Poetteringeb9da372013-11-06 18:28:39 +01002881 {}
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002882 };
2883
Lennart Poettering0f474362015-10-22 19:28:31 +02002884 int c, r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002885
2886 assert(argc >= 0);
2887 assert(argv);
2888
Zbigniew Jędrzejewski-Szmek601185b2014-08-02 11:12:21 -04002889 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002890
2891 switch (c) {
2892
2893 case 'h':
Lennart Poettering37ec0fd2018-08-09 10:32:31 +02002894 return help();
Lennart Poetteringeb9da372013-11-06 18:28:39 +01002895
2896 case ARG_VERSION:
Lennart Poettering3f6fd1b2015-09-23 03:01:06 +02002897 return version();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002898
Zbigniew Jędrzejewski-Szmekceaaeb92018-04-26 19:07:54 +02002899 case ARG_CAT_CONFIG:
2900 arg_cat_config = true;
2901 break;
2902
Zbigniew Jędrzejewski-Szmekf2b5ca02017-11-23 11:20:29 +01002903 case ARG_USER:
2904 arg_user = true;
2905 break;
2906
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002907 case ARG_CREATE:
2908 arg_create = true;
2909 break;
2910
2911 case ARG_CLEAN:
2912 arg_clean = true;
2913 break;
2914
2915 case ARG_REMOVE:
2916 arg_remove = true;
2917 break;
2918
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05002919 case ARG_BOOT:
2920 arg_boot = true;
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05002921 break;
2922
Lennart Poetteringfba6e682011-02-13 14:00:54 +01002923 case ARG_PREFIX:
Lennart Poettering7bc040f2014-06-11 01:26:28 +02002924 if (strv_push(&arg_include_prefixes, optarg) < 0)
Dave Reisnera2aced42013-07-24 11:10:05 -04002925 return log_oom();
Lennart Poetteringfba6e682011-02-13 14:00:54 +01002926 break;
2927
Dave Reisner5c795112013-07-24 11:19:24 -04002928 case ARG_EXCLUDE_PREFIX:
Lennart Poettering7bc040f2014-06-11 01:26:28 +02002929 if (strv_push(&arg_exclude_prefixes, optarg) < 0)
Dave Reisner5c795112013-07-24 11:19:24 -04002930 return log_oom();
2931 break;
2932
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07002933 case ARG_ROOT:
Lennart Poettering0f03c2a2015-10-22 19:54:29 +02002934 r = parse_path_argument_and_warn(optarg, true, &arg_root);
Lennart Poettering0f474362015-10-22 19:28:31 +02002935 if (r < 0)
Lennart Poettering0f03c2a2015-10-22 19:54:29 +02002936 return r;
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07002937 break;
2938
Zbigniew Jędrzejewski-Szmeka6d84742018-02-05 14:53:11 +01002939 case ARG_REPLACE:
2940 if (!path_is_absolute(optarg) ||
2941 !endswith(optarg, ".conf")) {
2942 log_error("The argument to --replace= must an absolute path to a config file");
2943 return -EINVAL;
2944 }
2945
2946 arg_replace = optarg;
2947 break;
2948
Lennart Poetteringdcd5c892018-06-12 15:37:53 +02002949 case ARG_NO_PAGER:
2950 arg_no_pager = true;
2951 break;
2952
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002953 case '?':
2954 return -EINVAL;
2955
2956 default:
Lennart Poetteringeb9da372013-11-06 18:28:39 +01002957 assert_not_reached("Unhandled option");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002958 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002959
Zbigniew Jędrzejewski-Szmekceaaeb92018-04-26 19:07:54 +02002960 if (!arg_clean && !arg_create && !arg_remove && !arg_cat_config) {
Harald Hoyer35b8ca32011-02-21 15:32:17 +01002961 log_error("You need to specify at least one of --clean, --create or --remove.");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002962 return -EINVAL;
2963 }
2964
Zbigniew Jędrzejewski-Szmekceaaeb92018-04-26 19:07:54 +02002965 if (arg_replace && arg_cat_config) {
2966 log_error("Option --replace= is not supported with --cat-config");
2967 return -EINVAL;
2968 }
2969
Zbigniew Jędrzejewski-Szmeka6d84742018-02-05 14:53:11 +01002970 if (arg_replace && optind >= argc) {
2971 log_error("When --replace= is given, some configuration items must be specified");
2972 return -EINVAL;
2973 }
2974
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002975 return 1;
2976}
2977
Zbigniew Jędrzejewski-Szmeka6d84742018-02-05 14:53:11 +01002978static int read_config_file(char **config_dirs, const char *fn, bool ignore_enoent, bool *invalid_config) {
Zbigniew Jędrzejewski-Szmekf7ac1ed2016-04-19 22:34:04 -04002979 _cleanup_fclose_ FILE *_f = NULL;
2980 FILE *f;
Lennart Poettering1731e342013-09-17 11:02:02 -05002981 char line[LINE_MAX];
Michal Sekletar78a92a52013-01-18 16:13:08 +01002982 Iterator iterator;
Lennart Poettering1731e342013-09-17 11:02:02 -05002983 unsigned v = 0;
Michal Sekletar78a92a52013-01-18 16:13:08 +01002984 Item *i;
Lennart Poettering4e68ec12016-06-21 13:20:23 +02002985 int r = 0;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01002986
2987 assert(fn);
2988
Zbigniew Jędrzejewski-Szmekf7ac1ed2016-04-19 22:34:04 -04002989 if (streq(fn, "-")) {
Zbigniew Jędrzejewski-Szmeka6d84742018-02-05 14:53:11 +01002990 log_debug("Reading config from stdin…");
Zbigniew Jędrzejewski-Szmekf7ac1ed2016-04-19 22:34:04 -04002991 fn = "<stdin>";
2992 f = stdin;
2993 } else {
Zbigniew Jędrzejewski-Szmeka6d84742018-02-05 14:53:11 +01002994 r = search_and_fopen(fn, "re", arg_root, (const char**) config_dirs, &_f);
Zbigniew Jędrzejewski-Szmekf7ac1ed2016-04-19 22:34:04 -04002995 if (r < 0) {
2996 if (ignore_enoent && r == -ENOENT) {
2997 log_debug_errno(r, "Failed to open \"%s\", ignoring: %m", fn);
2998 return 0;
2999 }
Lennart Poetteringfba6e682011-02-13 14:00:54 +01003000
Zbigniew Jędrzejewski-Szmekf7ac1ed2016-04-19 22:34:04 -04003001 return log_error_errno(r, "Failed to open '%s': %m", fn);
3002 }
Zbigniew Jędrzejewski-Szmeka6d84742018-02-05 14:53:11 +01003003 log_debug("Reading config file \"%s\"…", fn);
Zbigniew Jędrzejewski-Szmekf7ac1ed2016-04-19 22:34:04 -04003004 f = _f;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01003005 }
3006
Lennart Poettering1731e342013-09-17 11:02:02 -05003007 FOREACH_LINE(line, f, break) {
3008 char *l;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01003009 int k;
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01003010 bool invalid_line = false;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01003011
Lennart Poetteringfba6e682011-02-13 14:00:54 +01003012 v++;
3013
3014 l = strstrip(line);
Yu Watanabe4c701092017-10-04 23:01:32 +09003015 if (IN_SET(*l, 0, '#'))
Lennart Poetteringfba6e682011-02-13 14:00:54 +01003016 continue;
3017
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01003018 k = parse_line(fn, v, l, &invalid_line);
3019 if (k < 0) {
3020 if (invalid_line)
3021 /* Allow reporting with a special code if the caller requested this */
3022 *invalid_config = true;
3023 else if (r == 0)
3024 /* The first error becomes our return value */
3025 r = k;
3026 }
Lennart Poetteringfba6e682011-02-13 14:00:54 +01003027 }
3028
Michal Sekletar78a92a52013-01-18 16:13:08 +01003029 /* we have to determine age parameter for each entry of type X */
Lennart Poetteringef43a392015-04-22 18:18:56 +02003030 ORDERED_HASHMAP_FOREACH(i, globs, iterator) {
Michal Sekletar78a92a52013-01-18 16:13:08 +01003031 Iterator iter;
3032 Item *j, *candidate_item = NULL;
3033
3034 if (i->type != IGNORE_DIRECTORY_PATH)
3035 continue;
3036
Lennart Poetteringef43a392015-04-22 18:18:56 +02003037 ORDERED_HASHMAP_FOREACH(j, items, iter) {
Lennart Poettering5fb13eb2015-10-21 19:46:23 +02003038 if (!IN_SET(j->type, CREATE_DIRECTORY, TRUNCATE_DIRECTORY, CREATE_SUBVOLUME, CREATE_SUBVOLUME_INHERIT_QUOTA, CREATE_SUBVOLUME_NEW_QUOTA))
Michal Sekletar78a92a52013-01-18 16:13:08 +01003039 continue;
3040
3041 if (path_equal(j->path, i->path)) {
3042 candidate_item = j;
3043 break;
3044 }
3045
3046 if ((!candidate_item && path_startswith(i->path, j->path)) ||
3047 (candidate_item && path_startswith(j->path, candidate_item->path) && (fnmatch(i->path, j->path, FNM_PATHNAME | FNM_PERIOD) == 0)))
3048 candidate_item = j;
3049 }
3050
Richard Weinberger9ed2a352014-09-09 11:09:37 +02003051 if (candidate_item && candidate_item->age_set) {
Michal Sekletar78a92a52013-01-18 16:13:08 +01003052 i->age = candidate_item->age;
3053 i->age_set = true;
3054 }
3055 }
3056
Lennart Poetteringfba6e682011-02-13 14:00:54 +01003057 if (ferror(f)) {
Michal Schmidt56f64d92014-11-28 19:29:59 +01003058 log_error_errno(errno, "Failed to read from file %s: %m", fn);
Lennart Poetteringfba6e682011-02-13 14:00:54 +01003059 if (r == 0)
3060 r = -EIO;
3061 }
3062
Lennart Poetteringfba6e682011-02-13 14:00:54 +01003063 return r;
3064}
3065
Zbigniew Jędrzejewski-Szmeka6d84742018-02-05 14:53:11 +01003066static int parse_arguments(char **config_dirs, char **args, bool *invalid_config) {
3067 char **arg;
3068 int r;
3069
3070 STRV_FOREACH(arg, args) {
3071 r = read_config_file(config_dirs, *arg, false, invalid_config);
3072 if (r < 0)
3073 return r;
3074 }
3075
3076 return 0;
3077}
3078
3079static int read_config_files(char **config_dirs, char **args, bool *invalid_config) {
3080 _cleanup_strv_free_ char **files = NULL;
3081 _cleanup_free_ char *p = NULL;
3082 char **f;
3083 int r;
3084
Zbigniew Jędrzejewski-Szmekceaaeb92018-04-26 19:07:54 +02003085 r = conf_files_list_with_replacement(arg_root, config_dirs, arg_replace, &files, &p);
Zbigniew Jędrzejewski-Szmeka6d84742018-02-05 14:53:11 +01003086 if (r < 0)
Zbigniew Jędrzejewski-Szmekceaaeb92018-04-26 19:07:54 +02003087 return r;
Zbigniew Jędrzejewski-Szmeka6d84742018-02-05 14:53:11 +01003088
3089 STRV_FOREACH(f, files)
3090 if (p && path_equal(*f, p)) {
3091 log_debug("Parsing arguments at position \"%s\"…", *f);
3092
3093 r = parse_arguments(config_dirs, args, invalid_config);
3094 if (r < 0)
3095 return r;
3096 } else
3097 /* Just warn, ignore result otherwise.
3098 * read_config_file() has some debug output, so no need to print anything. */
3099 (void) read_config_file(config_dirs, *f, true, invalid_config);
3100
3101 return 0;
3102}
3103
Lennart Poettering5008d582010-09-28 02:34:02 +02003104int main(int argc, char *argv[]) {
Zbigniew Jędrzejewski-Szmekbb9947b2018-03-29 16:19:33 +02003105 int r, k, r_process = 0;
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05003106 ItemArray *a;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02003107 Iterator iterator;
Zbigniew Jędrzejewski-Szmekf2b5ca02017-11-23 11:20:29 +01003108 _cleanup_strv_free_ char **config_dirs = NULL;
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01003109 bool invalid_config = false;
Lennart Poettering5008d582010-09-28 02:34:02 +02003110
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +01003111 r = parse_argv(argc, argv);
3112 if (r <= 0)
Lennart Poettering753615e2014-06-12 23:07:17 +02003113 goto finish;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02003114
Lennart Poetteringeb0ca9e2011-02-12 09:31:38 +01003115 log_set_target(LOG_TARGET_AUTO);
Lennart Poettering5008d582010-09-28 02:34:02 +02003116 log_parse_environment();
3117 log_open();
3118
Zbigniew Jędrzejewski-Szmekf2b5ca02017-11-23 11:20:29 +01003119 if (arg_user) {
3120 r = user_config_paths(&config_dirs);
3121 if (r < 0) {
3122 log_error_errno(r, "Failed to initialize configuration directory list: %m");
3123 goto finish;
3124 }
3125 } else {
3126 config_dirs = strv_split_nulstr(CONF_PATHS_NULSTR("tmpfiles.d"));
3127 if (!config_dirs) {
3128 r = log_oom();
3129 goto finish;
3130 }
3131 }
3132
Lennart Poettering79c91ce2018-01-22 15:33:13 +01003133 if (DEBUG_LOGGING) {
Zbigniew Jędrzejewski-Szmekf2b5ca02017-11-23 11:20:29 +01003134 _cleanup_free_ char *t = NULL;
3135
3136 t = strv_join(config_dirs, "\n\t");
3137 if (t)
Zbigniew Jędrzejewski-Szmekbb9947b2018-03-29 16:19:33 +02003138 log_debug("Looking for configuration files in (higher priority first):\n\t%s", t);
Zbigniew Jędrzejewski-Szmekf2b5ca02017-11-23 11:20:29 +01003139 }
3140
Zbigniew Jędrzejewski-Szmekceaaeb92018-04-26 19:07:54 +02003141 if (arg_cat_config) {
Lennart Poetteringdcd5c892018-06-12 15:37:53 +02003142 (void) pager_open(arg_no_pager, false);
3143
Zbigniew Jędrzejewski-Szmekceaaeb92018-04-26 19:07:54 +02003144 r = cat_config(config_dirs, argv + optind);
3145 goto finish;
3146 }
3147
3148 umask(0022);
3149
3150 mac_selinux_init();
3151
3152 items = ordered_hashmap_new(&string_hash_ops);
3153 globs = ordered_hashmap_new(&string_hash_ops);
3154
3155 if (!items || !globs) {
3156 r = log_oom();
3157 goto finish;
3158 }
3159
Zbigniew Jędrzejewski-Szmeka6d84742018-02-05 14:53:11 +01003160 /* If command line arguments are specified along with --replace, read all
3161 * configuration files and insert the positional arguments at the specified
3162 * place. Otherwise, if command line arguments are specified, execute just
3163 * them, and finally, without --replace= or any positional arguments, just
3164 * read configuration and execute it.
3165 */
3166 if (arg_replace || optind >= argc)
3167 r = read_config_files(config_dirs, argv + optind, &invalid_config);
3168 else
3169 r = parse_arguments(config_dirs, argv + optind, &invalid_config);
3170 if (r < 0)
3171 goto finish;
Lennart Poettering5008d582010-09-28 02:34:02 +02003172
Lennart Poettering17493fa2015-04-10 16:22:22 +02003173 /* The non-globbing ones usually create things, hence we apply
3174 * them first */
Lennart Poetteringef43a392015-04-22 18:18:56 +02003175 ORDERED_HASHMAP_FOREACH(a, items, iterator) {
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05003176 k = process_item_array(a);
Zbigniew Jędrzejewski-Szmekbb9947b2018-03-29 16:19:33 +02003177 if (k < 0 && r_process == 0)
3178 r_process = k;
Zbigniew Jędrzejewski-Szmek1db50422015-01-06 09:53:12 -05003179 }
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01003180
Lennart Poettering17493fa2015-04-10 16:22:22 +02003181 /* The globbing ones usually alter things, hence we apply them
3182 * second. */
Lennart Poetteringef43a392015-04-22 18:18:56 +02003183 ORDERED_HASHMAP_FOREACH(a, globs, iterator) {
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05003184 k = process_item_array(a);
Zbigniew Jędrzejewski-Szmekbb9947b2018-03-29 16:19:33 +02003185 if (k < 0 && r_process == 0)
3186 r_process = k;
Zbigniew Jędrzejewski-Szmek1db50422015-01-06 09:53:12 -05003187 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02003188
Lennart Poettering5008d582010-09-28 02:34:02 +02003189finish:
Lennart Poetteringdcd5c892018-06-12 15:37:53 +02003190 pager_close();
3191
Zbigniew Jędrzejewski-Szmek224b0e72017-11-28 12:35:49 +01003192 ordered_hashmap_free_with_destructor(items, item_array_free);
3193 ordered_hashmap_free_with_destructor(globs, item_array_free);
Lennart Poettering5008d582010-09-28 02:34:02 +02003194
Lennart Poettering7bc040f2014-06-11 01:26:28 +02003195 free(arg_include_prefixes);
3196 free(arg_exclude_prefixes);
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07003197 free(arg_root);
Dave Reisnera2aced42013-07-24 11:10:05 -04003198
Lennart Poettering17b90522011-02-14 21:55:06 +01003199 set_free_free(unix_sockets);
3200
WaLyong Chocc56faf2014-10-23 17:23:46 +09003201 mac_selinux_finish();
Lennart Poettering29003cf2010-10-19 19:36:45 +02003202
Zbigniew Jędrzejewski-Szmekbb9947b2018-03-29 16:19:33 +02003203 if (r < 0 || ERRNO_IS_RESOURCE(-r_process))
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01003204 return EXIT_FAILURE;
3205 else if (invalid_config)
3206 return EX_DATAERR;
Zbigniew Jędrzejewski-Szmekbb9947b2018-03-29 16:19:33 +02003207 else if (r_process < 0)
3208 return EX_CANTCREAT;
Zbigniew Jędrzejewski-Szmekd9daae52017-11-22 14:13:32 +01003209 else
3210 return EXIT_SUCCESS;
Lennart Poettering5008d582010-09-28 02:34:02 +02003211}