blob: 85a2d6c2f5e0b41d5c4d3fb2f6cca355c7cb9360 [file] [log] [blame]
Lennart Poettering5008d582010-09-28 02:34:02 +02001/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02006 Copyright 2010 Lennart Poettering, Kay Sievers
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05007 Copyright 2015 Zbigniew Jędrzejewski-Szmek
Lennart Poettering5008d582010-09-28 02:34:02 +02008
9 systemd is free software; you can redistribute it and/or modify it
Lennart Poettering5430f7f2012-04-12 00:20:58 +020010 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
Lennart Poettering5008d582010-09-28 02:34:02 +020012 (at your option) any later version.
13
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lennart Poettering5430f7f2012-04-12 00:20:58 +020017 Lesser General Public License for more details.
Lennart Poettering5008d582010-09-28 02:34:02 +020018
Lennart Poettering5430f7f2012-04-12 00:20:58 +020019 You should have received a copy of the GNU Lesser General Public License
Lennart Poettering5008d582010-09-28 02:34:02 +020020 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21***/
22
Lennart Poettering5008d582010-09-28 02:34:02 +020023#include <dirent.h>
Lennart Poettering3f6fd1b2015-09-23 03:01:06 +020024#include <errno.h>
25#include <fcntl.h>
26#include <fnmatch.h>
27#include <getopt.h>
28#include <glob.h>
29#include <limits.h>
30#include <linux/fs.h>
31#include <stdbool.h>
32#include <stddef.h>
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020033#include <stdio.h>
34#include <stdlib.h>
Lennart Poettering3f6fd1b2015-09-23 03:01:06 +020035#include <string.h>
Zbigniew Jędrzejewski-Szmek505ef0e2015-01-09 01:10:02 -050036#include <sys/stat.h>
Maciej Wereskiebf4e802014-12-04 10:32:10 +010037#include <sys/xattr.h>
Lennart Poettering3f6fd1b2015-09-23 03:01:06 +020038#include <time.h>
39#include <unistd.h>
Lennart Poettering5008d582010-09-28 02:34:02 +020040
Lennart Poettering3f6fd1b2015-09-23 03:01:06 +020041#include "acl-util.h"
42#include "btrfs-util.h"
43#include "capability.h"
44#include "conf-files.h"
45#include "copy.h"
Lennart Poettering4f5dd392015-10-23 18:52:53 +020046#include "escape.h"
Lennart Poettering3ffd4af2015-10-25 13:14:12 +010047#include "fd-util.h"
Lennart Poettering0d39fa92015-10-26 18:05:03 +010048#include "fileio.h"
Lennart Poettering3f6fd1b2015-09-23 03:01:06 +020049#include "formats-util.h"
Lennart Poetteringc0044932015-10-25 14:08:25 +010050#include "io-util.h"
Lennart Poettering3f6fd1b2015-09-23 03:01:06 +020051#include "label.h"
Lennart Poettering5008d582010-09-28 02:34:02 +020052#include "log.h"
Dave Reisner54693d92012-09-15 12:58:49 -040053#include "macro.h"
Zbigniew Jędrzejewski-Szmekd39efe72013-03-13 20:20:54 -040054#include "missing.h"
Kay Sievers49e942b2012-04-10 21:54:31 +020055#include "mkdir.h"
Lennart Poettering6bedfcb2015-10-26 16:18:16 +010056#include "parse-util.h"
Kay Sievers9eb977d2012-05-07 21:36:12 +020057#include "path-util.h"
Lennart Poetteringc6878632015-04-04 11:52:57 +020058#include "rm-rf.h"
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +010059#include "selinux-util.h"
Lennart Poettering3f6fd1b2015-09-23 03:01:06 +020060#include "set.h"
61#include "specifier.h"
Lennart Poettering07630ce2015-10-24 22:58:24 +020062#include "string-util.h"
Lennart Poettering3f6fd1b2015-09-23 03:01:06 +020063#include "strv.h"
Lennart Poetteringb1d4f8e2015-10-25 22:32:30 +010064#include "user-util.h"
Lennart Poettering3f6fd1b2015-09-23 03:01:06 +020065#include "util.h"
Lennart Poettering5008d582010-09-28 02:34:02 +020066
Andreas Jaeger01000472010-09-29 10:08:24 +020067/* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates
Lennart Poettering5008d582010-09-28 02:34:02 +020068 * them in the file system. This is intended to be used to create
Kay Sieversdb019b82011-04-04 15:33:00 +020069 * properly owned directories beneath /tmp, /var/tmp, /run, which are
70 * volatile and hence need to be recreated on bootup. */
Lennart Poettering5008d582010-09-28 02:34:02 +020071
Michal Schmidt66ccd032011-12-15 21:31:14 +010072typedef enum ItemType {
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010073 /* These ones take file names */
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020074 CREATE_FILE = 'f',
75 TRUNCATE_FILE = 'F',
76 CREATE_DIRECTORY = 'd',
77 TRUNCATE_DIRECTORY = 'D',
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +010078 CREATE_SUBVOLUME = 'v',
Lennart Poettering5fb13eb2015-10-21 19:46:23 +020079 CREATE_SUBVOLUME_INHERIT_QUOTA = 'q',
80 CREATE_SUBVOLUME_NEW_QUOTA = 'Q',
Lennart Poetteringee17ee72011-07-12 03:56:56 +020081 CREATE_FIFO = 'p',
Lennart Poettering468d7262012-01-17 15:04:12 +010082 CREATE_SYMLINK = 'L',
83 CREATE_CHAR_DEVICE = 'c',
84 CREATE_BLOCK_DEVICE = 'b',
Lennart Poettering849958d2014-06-10 23:02:40 +020085 COPY_FILES = 'C',
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +010086
87 /* These ones take globs */
Lennart Poettering17493fa2015-04-10 16:22:22 +020088 WRITE_FILE = 'w',
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -050089 SET_XATTR = 't',
90 RECURSIVE_SET_XATTR = 'T',
91 SET_ACL = 'a',
92 RECURSIVE_SET_ACL = 'A',
Lennart Poettering17493fa2015-04-10 16:22:22 +020093 SET_ATTRIBUTE = 'h',
94 RECURSIVE_SET_ATTRIBUTE = 'H',
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020095 IGNORE_PATH = 'x',
Michal Sekletar78a92a52013-01-18 16:13:08 +010096 IGNORE_DIRECTORY_PATH = 'X',
Lennart Poettering3b63d2d2010-10-18 22:38:41 +020097 REMOVE_PATH = 'r',
Michal Schmidta8d88782011-12-15 23:11:07 +010098 RECURSIVE_REMOVE_PATH = 'R',
Michal Schmidt777b87e2011-12-16 18:27:35 +010099 RELABEL_PATH = 'z',
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200100 RECURSIVE_RELABEL_PATH = 'Z',
Lennart Poettering17493fa2015-04-10 16:22:22 +0200101 ADJUST_MODE = 'm', /* legacy, 'z' is identical to this */
Michal Schmidt66ccd032011-12-15 21:31:14 +0100102} ItemType;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200103
104typedef struct Item {
Michal Schmidt66ccd032011-12-15 21:31:14 +0100105 ItemType type;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200106
107 char *path;
Lennart Poettering468d7262012-01-17 15:04:12 +0100108 char *argument;
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100109 char **xattrs;
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -0500110#ifdef HAVE_ACL
111 acl_t acl_access;
112 acl_t acl_default;
113#endif
Lennart Poettering5008d582010-09-28 02:34:02 +0200114 uid_t uid;
115 gid_t gid;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200116 mode_t mode;
117 usec_t age;
118
Lennart Poettering468d7262012-01-17 15:04:12 +0100119 dev_t major_minor;
Lennart Poettering88ec4df2015-04-08 22:35:52 +0200120 unsigned attribute_value;
121 unsigned attribute_mask;
Lennart Poettering468d7262012-01-17 15:04:12 +0100122
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200123 bool uid_set:1;
124 bool gid_set:1;
125 bool mode_set:1;
126 bool age_set:1;
Lennart Poetteringabef3f92014-06-11 10:14:07 +0200127 bool mask_perms:1;
Lennart Poettering88ec4df2015-04-08 22:35:52 +0200128 bool attribute_set:1;
Lennart Poettering24f3a372012-06-20 09:05:50 +0200129
130 bool keep_first_level:1;
Lennart Poettering1910cd02014-06-11 01:37:35 +0200131
Lennart Poettering2e78fa72014-06-16 13:21:07 +0200132 bool force:1;
133
Lennart Poettering1910cd02014-06-11 01:37:35 +0200134 bool done:1;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200135} Item;
136
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -0500137typedef struct ItemArray {
138 Item *items;
139 size_t count;
140 size_t size;
141} ItemArray;
142
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200143static bool arg_create = false;
144static bool arg_clean = false;
145static bool arg_remove = false;
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -0500146static bool arg_boot = false;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200147
Lennart Poettering7bc040f2014-06-11 01:26:28 +0200148static char **arg_include_prefixes = NULL;
149static char **arg_exclude_prefixes = NULL;
Michael Marineaucf9a4ab2014-03-13 21:32:13 -0700150static char *arg_root = NULL;
Lennart Poetteringfba6e682011-02-13 14:00:54 +0100151
Josh Triplett7f0a55d2014-10-29 05:02:56 -0700152static const char conf_file_dirs[] = CONF_DIRS_NULSTR("tmpfiles");
Dave Reisner91256702012-06-08 22:31:19 -0400153
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200154#define MAX_DEPTH 256
155
Lennart Poetteringef43a392015-04-22 18:18:56 +0200156static OrderedHashmap *items = NULL, *globs = NULL;
Lennart Poettering7bc040f2014-06-11 01:26:28 +0200157static Set *unix_sockets = NULL;
158
Lennart Poetteringbd550f72015-04-10 16:03:24 +0200159static const Specifier specifier_table[] = {
160 { 'm', specifier_machine_id, NULL },
161 { 'b', specifier_boot_id, NULL },
162 { 'H', specifier_host_name, NULL },
163 { 'v', specifier_kernel_release, NULL },
164 {}
165};
166
Michal Schmidt66ccd032011-12-15 21:31:14 +0100167static bool needs_glob(ItemType t) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200168 return IN_SET(t,
169 WRITE_FILE,
170 IGNORE_PATH,
171 IGNORE_DIRECTORY_PATH,
172 REMOVE_PATH,
173 RECURSIVE_REMOVE_PATH,
Lennart Poetteringe73a03e2014-06-10 23:42:16 +0200174 ADJUST_MODE,
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200175 RELABEL_PATH,
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -0500176 RECURSIVE_RELABEL_PATH,
177 SET_XATTR,
178 RECURSIVE_SET_XATTR,
179 SET_ACL,
Lennart Poettering34f64532015-04-09 13:12:06 +0200180 RECURSIVE_SET_ACL,
181 SET_ATTRIBUTE,
182 RECURSIVE_SET_ATTRIBUTE);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100183}
184
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -0500185static bool takes_ownership(ItemType t) {
186 return IN_SET(t,
187 CREATE_FILE,
188 TRUNCATE_FILE,
189 CREATE_DIRECTORY,
190 TRUNCATE_DIRECTORY,
191 CREATE_SUBVOLUME,
Lennart Poettering5fb13eb2015-10-21 19:46:23 +0200192 CREATE_SUBVOLUME_INHERIT_QUOTA,
193 CREATE_SUBVOLUME_NEW_QUOTA,
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -0500194 CREATE_FIFO,
195 CREATE_SYMLINK,
196 CREATE_CHAR_DEVICE,
197 CREATE_BLOCK_DEVICE,
198 COPY_FILES,
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -0500199 WRITE_FILE,
200 IGNORE_PATH,
201 IGNORE_DIRECTORY_PATH,
202 REMOVE_PATH,
203 RECURSIVE_REMOVE_PATH);
204}
205
Lennart Poetteringef43a392015-04-22 18:18:56 +0200206static struct Item* find_glob(OrderedHashmap *h, const char *match) {
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -0500207 ItemArray *j;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100208 Iterator i;
209
Lennart Poetteringef43a392015-04-22 18:18:56 +0200210 ORDERED_HASHMAP_FOREACH(j, h, i) {
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -0500211 unsigned n;
212
213 for (n = 0; n < j->count; n++) {
214 Item *item = j->items + n;
215
216 if (fnmatch(item->path, match, FNM_PATHNAME|FNM_PERIOD) == 0)
217 return item;
218 }
219 }
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100220
221 return NULL;
222}
223
Lennart Poettering17b90522011-02-14 21:55:06 +0100224static void load_unix_sockets(void) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200225 _cleanup_fclose_ FILE *f = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +0100226 char line[LINE_MAX];
227
228 if (unix_sockets)
229 return;
230
231 /* We maintain a cache of the sockets we found in
232 * /proc/net/unix to speed things up a little. */
233
Michal Schmidtd5099ef2014-08-13 01:00:18 +0200234 unix_sockets = set_new(&string_hash_ops);
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100235 if (!unix_sockets)
Lennart Poettering17b90522011-02-14 21:55:06 +0100236 return;
237
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100238 f = fopen("/proc/net/unix", "re");
239 if (!f)
Lennart Poettering17b90522011-02-14 21:55:06 +0100240 return;
241
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100242 /* Skip header */
243 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100244 goto fail;
245
246 for (;;) {
247 char *p, *s;
248 int k;
249
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100250 if (!fgets(line, sizeof(line), f))
Lennart Poettering17b90522011-02-14 21:55:06 +0100251 break;
252
253 truncate_nl(line);
254
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100255 p = strchr(line, ':');
256 if (!p)
Lennart Poettering17b90522011-02-14 21:55:06 +0100257 continue;
258
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100259 if (strlen(p) < 37)
260 continue;
261
262 p += 37;
Lennart Poettering17b90522011-02-14 21:55:06 +0100263 p += strspn(p, WHITESPACE);
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100264 p += strcspn(p, WHITESPACE); /* skip one more word */
Lennart Poettering17b90522011-02-14 21:55:06 +0100265 p += strspn(p, WHITESPACE);
266
267 if (*p != '/')
268 continue;
269
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +0100270 s = strdup(p);
271 if (!s)
Lennart Poettering17b90522011-02-14 21:55:06 +0100272 goto fail;
273
Lennart Poettering4ff21d82011-02-17 13:13:34 +0100274 path_kill_slashes(s);
275
Zbigniew Jędrzejewski-Szmekef422022013-04-22 23:12:15 -0400276 k = set_consume(unix_sockets, s);
277 if (k < 0 && k != -EEXIST)
278 goto fail;
Lennart Poettering17b90522011-02-14 21:55:06 +0100279 }
280
281 return;
282
283fail:
284 set_free_free(unix_sockets);
285 unix_sockets = NULL;
Lennart Poettering17b90522011-02-14 21:55:06 +0100286}
287
288static bool unix_socket_alive(const char *fn) {
289 assert(fn);
290
291 load_unix_sockets();
292
293 if (unix_sockets)
294 return !!set_get(unix_sockets, (char*) fn);
295
296 /* We don't know, so assume yes */
297 return true;
298}
299
Kay Sievers99d680a2013-03-13 13:12:36 +0100300static int dir_is_mount_point(DIR *d, const char *subdir) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200301
Zbigniew Jędrzejewski-Szmek2695c5c2015-01-07 10:28:04 -0500302 union file_handle_union h = FILE_HANDLE_INIT;
Kay Sievers99d680a2013-03-13 13:12:36 +0100303 int mount_id_parent, mount_id;
304 int r_p, r;
305
Dave Reisner370c8602014-04-19 13:22:35 -0400306 r_p = name_to_handle_at(dirfd(d), ".", &h.handle, &mount_id_parent, 0);
Kay Sievers99d680a2013-03-13 13:12:36 +0100307 if (r_p < 0)
308 r_p = -errno;
309
Dave Reisner370c8602014-04-19 13:22:35 -0400310 h.handle.handle_bytes = MAX_HANDLE_SZ;
311 r = name_to_handle_at(dirfd(d), subdir, &h.handle, &mount_id, 0);
Kay Sievers99d680a2013-03-13 13:12:36 +0100312 if (r < 0)
313 r = -errno;
314
315 /* got no handle; make no assumptions, return error */
316 if (r_p < 0 && r < 0)
317 return r_p;
318
319 /* got both handles; if they differ, it is a mount point */
320 if (r_p >= 0 && r >= 0)
321 return mount_id_parent != mount_id;
322
323 /* got only one handle; assume different mount points if one
324 * of both queries was not supported by the filesystem */
Dave Reisnere7aab542014-10-11 20:35:06 -0400325 if (r_p == -ENOSYS || r_p == -EOPNOTSUPP || r == -ENOSYS || r == -EOPNOTSUPP)
Kay Sievers99d680a2013-03-13 13:12:36 +0100326 return true;
327
328 /* return error */
329 if (r_p < 0)
330 return r_p;
331 return r;
332}
333
Zbigniew Jędrzejewski-Szmekdf99a9e2014-01-02 00:02:31 -0500334static DIR* xopendirat_nomod(int dirfd, const char *path) {
335 DIR *dir;
336
337 dir = xopendirat(dirfd, path, O_NOFOLLOW|O_NOATIME);
Lennart Poettering15322272015-03-23 18:54:31 +0700338 if (dir)
339 return dir;
340
341 log_debug_errno(errno, "Cannot open %sdirectory \"%s\": %m", dirfd == AT_FDCWD ? "" : "sub", path);
342 if (errno != EPERM)
343 return NULL;
344
345 dir = xopendirat(dirfd, path, O_NOFOLLOW);
346 if (!dir)
347 log_debug_errno(errno, "Cannot open %sdirectory \"%s\": %m", dirfd == AT_FDCWD ? "" : "sub", path);
Zbigniew Jędrzejewski-Szmekdf99a9e2014-01-02 00:02:31 -0500348
349 return dir;
350}
351
352static DIR* opendir_nomod(const char *path) {
353 return xopendirat_nomod(AT_FDCWD, path);
354}
355
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200356static int dir_cleanup(
Michal Sekletar78a92a52013-01-18 16:13:08 +0100357 Item *i,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200358 const char *p,
359 DIR *d,
360 const struct stat *ds,
361 usec_t cutoff,
362 dev_t rootdev,
363 bool mountpoint,
Lennart Poettering24f3a372012-06-20 09:05:50 +0200364 int maxdepth,
Lennart Poettering265ffa12013-09-17 16:33:30 -0500365 bool keep_this_level) {
366
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200367 struct dirent *dent;
368 struct timespec times[2];
369 bool deleted = false;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200370 int r = 0;
371
372 while ((dent = readdir(d))) {
373 struct stat s;
374 usec_t age;
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200375 _cleanup_free_ char *sub_path = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200376
Zbigniew Jędrzejewski-Szmek7fcb4b92015-01-22 23:35:34 -0500377 if (STR_IN_SET(dent->d_name, ".", ".."))
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200378 continue;
379
380 if (fstatat(dirfd(d), dent->d_name, &s, AT_SYMLINK_NOFOLLOW) < 0) {
Kay Sieversca2f4172013-10-17 03:20:46 +0200381 if (errno == ENOENT)
382 continue;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200383
Kay Sieversca2f4172013-10-17 03:20:46 +0200384 /* FUSE, NFS mounts, SELinux might return EACCES */
385 if (errno == EACCES)
Michal Schmidt56f64d92014-11-28 19:29:59 +0100386 log_debug_errno(errno, "stat(%s/%s) failed: %m", p, dent->d_name);
Kay Sieversca2f4172013-10-17 03:20:46 +0200387 else
Michal Schmidt56f64d92014-11-28 19:29:59 +0100388 log_error_errno(errno, "stat(%s/%s) failed: %m", p, dent->d_name);
Kay Sieversca2f4172013-10-17 03:20:46 +0200389 r = -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200390 continue;
391 }
392
393 /* Stay on the same filesystem */
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500394 if (s.st_dev != rootdev) {
395 log_debug("Ignoring \"%s/%s\": different filesystem.", p, dent->d_name);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200396 continue;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500397 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200398
Kay Sievers99d680a2013-03-13 13:12:36 +0100399 /* Try to detect bind mounts of the same filesystem instance; they
400 * do not differ in device major/minors. This type of query is not
401 * supported on all kernels or filesystem types though. */
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500402 if (S_ISDIR(s.st_mode) && dir_is_mount_point(d, dent->d_name) > 0) {
403 log_debug("Ignoring \"%s/%s\": different mount of the same filesystem.",
404 p, dent->d_name);
Kay Sievers99d680a2013-03-13 13:12:36 +0100405 continue;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500406 }
Kay Sievers99d680a2013-03-13 13:12:36 +0100407
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200408 /* Do not delete read-only files owned by root */
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500409 if (s.st_uid == 0 && !(s.st_mode & S_IWUSR)) {
410 log_debug("Ignoring \"%s/%s\": read-only and owner by root.", p, dent->d_name);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200411 continue;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500412 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200413
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200414 sub_path = strjoin(p, "/", dent->d_name, NULL);
415 if (!sub_path) {
Shawn Landden0d0f0c52012-07-25 14:55:59 -0700416 r = log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200417 goto finish;
418 }
419
420 /* Is there an item configured for this path? */
Lennart Poetteringef43a392015-04-22 18:18:56 +0200421 if (ordered_hashmap_get(items, sub_path)) {
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500422 log_debug("Ignoring \"%s\": a separate entry exists.", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200423 continue;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500424 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200425
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500426 if (find_glob(globs, sub_path)) {
427 log_debug("Ignoring \"%s\": a separate glob exists.", sub_path);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100428 continue;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500429 }
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +0100430
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200431 if (S_ISDIR(s.st_mode)) {
432
433 if (mountpoint &&
434 streq(dent->d_name, "lost+found") &&
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500435 s.st_uid == 0) {
436 log_debug("Ignoring \"%s\".", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200437 continue;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500438 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200439
440 if (maxdepth <= 0)
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500441 log_warning("Reached max depth on \"%s\".", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200442 else {
Harald Hoyer7fd1b192013-04-18 09:11:22 +0200443 _cleanup_closedir_ DIR *sub_dir;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200444 int q;
445
Zbigniew Jędrzejewski-Szmekdf99a9e2014-01-02 00:02:31 -0500446 sub_dir = xopendirat_nomod(dirfd(d), dent->d_name);
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200447 if (!sub_dir) {
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500448 if (errno != ENOENT)
449 r = log_error_errno(errno, "opendir(%s) failed: %m", sub_path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200450
451 continue;
452 }
453
Michal Sekletar78a92a52013-01-18 16:13:08 +0100454 q = dir_cleanup(i, sub_path, sub_dir, &s, cutoff, rootdev, false, maxdepth-1, false);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200455 if (q < 0)
456 r = q;
457 }
458
Lennart Poettering24f3a372012-06-20 09:05:50 +0200459 /* Note: if you are wondering why we don't
460 * support the sticky bit for excluding
461 * directories from cleaning like we do it for
462 * other file system objects: well, the sticky
463 * bit already has a meaning for directories,
464 * so we don't want to overload that. */
465
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500466 if (keep_this_level) {
467 log_debug("Keeping \"%s\".", sub_path);
Lennart Poettering24f3a372012-06-20 09:05:50 +0200468 continue;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500469 }
Lennart Poettering24f3a372012-06-20 09:05:50 +0200470
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200471 /* Ignore ctime, we change it when deleting */
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500472 age = timespec_load(&s.st_mtim);
473 if (age >= cutoff) {
474 char a[FORMAT_TIMESTAMP_MAX];
475 /* Follows spelling in stat(1). */
476 log_debug("Directory \"%s\": modify time %s is too new.",
477 sub_path,
478 format_timestamp_us(a, sizeof(a), age));
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200479 continue;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500480 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200481
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500482 age = timespec_load(&s.st_atim);
483 if (age >= cutoff) {
484 char a[FORMAT_TIMESTAMP_MAX];
485 log_debug("Directory \"%s\": access time %s is too new.",
486 sub_path,
487 format_timestamp_us(a, sizeof(a), age));
488 continue;
489 }
490
Zbigniew Jędrzejewski-Szmek61253222015-01-31 01:03:09 -0500491 log_debug("Removing directory \"%s\".", sub_path);
492 if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0)
493 if (errno != ENOENT && errno != ENOTEMPTY) {
494 log_error_errno(errno, "rmdir(%s): %m", sub_path);
495 r = -errno;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200496 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200497
498 } else {
Lennart Poettering9c737362010-11-14 20:12:51 +0100499 /* Skip files for which the sticky bit is
500 * set. These are semantics we define, and are
501 * unknown elsewhere. See XDG_RUNTIME_DIR
502 * specification for details. */
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500503 if (s.st_mode & S_ISVTX) {
504 log_debug("Skipping \"%s\": sticky bit set.", sub_path);
Lennart Poettering9c737362010-11-14 20:12:51 +0100505 continue;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200506 }
507
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500508 if (mountpoint && S_ISREG(s.st_mode))
Zbigniew Jędrzejewski-Szmek1542c012015-06-13 13:14:37 -0400509 if (s.st_uid == 0 && STR_IN_SET(dent->d_name,
510 ".journal",
511 "aquota.user",
512 "aquota.group")) {
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500513 log_debug("Skipping \"%s\".", sub_path);
514 continue;
515 }
516
Lennart Poettering17b90522011-02-14 21:55:06 +0100517 /* Ignore sockets that are listed in /proc/net/unix */
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500518 if (S_ISSOCK(s.st_mode) && unix_socket_alive(sub_path)) {
519 log_debug("Skipping \"%s\": live socket.", sub_path);
Lennart Poettering17b90522011-02-14 21:55:06 +0100520 continue;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500521 }
Lennart Poettering17b90522011-02-14 21:55:06 +0100522
Lennart Poettering78ab08e2011-02-19 14:20:16 +0100523 /* Ignore device nodes */
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500524 if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode)) {
525 log_debug("Skipping \"%s\": a device.", sub_path);
Lennart Poettering78ab08e2011-02-19 14:20:16 +0100526 continue;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500527 }
Lennart Poettering78ab08e2011-02-19 14:20:16 +0100528
Lennart Poettering24f3a372012-06-20 09:05:50 +0200529 /* Keep files on this level around if this is
530 * requested */
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500531 if (keep_this_level) {
532 log_debug("Keeping \"%s\".", sub_path);
Lennart Poettering24f3a372012-06-20 09:05:50 +0200533 continue;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200534 }
535
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500536 age = timespec_load(&s.st_mtim);
537 if (age >= cutoff) {
538 char a[FORMAT_TIMESTAMP_MAX];
539 /* Follows spelling in stat(1). */
540 log_debug("File \"%s\": modify time %s is too new.",
541 sub_path,
542 format_timestamp_us(a, sizeof(a), age));
543 continue;
544 }
545
546 age = timespec_load(&s.st_atim);
547 if (age >= cutoff) {
548 char a[FORMAT_TIMESTAMP_MAX];
549 log_debug("File \"%s\": access time %s is too new.",
550 sub_path,
551 format_timestamp_us(a, sizeof(a), age));
552 continue;
553 }
554
555 age = timespec_load(&s.st_ctim);
556 if (age >= cutoff) {
557 char a[FORMAT_TIMESTAMP_MAX];
558 log_debug("File \"%s\": change time %s is too new.",
559 sub_path,
560 format_timestamp_us(a, sizeof(a), age));
561 continue;
562 }
563
564 log_debug("unlink \"%s\"", sub_path);
565
566 if (unlinkat(dirfd(d), dent->d_name, 0) < 0)
567 if (errno != ENOENT)
568 r = log_error_errno(errno, "unlink(%s): %m", sub_path);
569
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200570 deleted = true;
571 }
572 }
573
574finish:
575 if (deleted) {
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500576 usec_t age1, age2;
577 char a[FORMAT_TIMESTAMP_MAX], b[FORMAT_TIMESTAMP_MAX];
578
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200579 /* Restore original directory timestamps */
580 times[0] = ds->st_atim;
581 times[1] = ds->st_mtim;
582
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500583 age1 = timespec_load(&ds->st_atim);
584 age2 = timespec_load(&ds->st_mtim);
585 log_debug("Restoring access and modification time on \"%s\": %s, %s",
586 p,
587 format_timestamp_us(a, sizeof(a), age1),
588 format_timestamp_us(b, sizeof(b), age2));
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200589 if (futimens(dirfd(d), times) < 0)
Michal Schmidt56f64d92014-11-28 19:29:59 +0100590 log_error_errno(errno, "utimensat(%s): %m", p);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200591 }
592
Lennart Poettering3b63d2d2010-10-18 22:38:41 +0200593 return r;
594}
595
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -0500596static int path_set_perms(Item *i, const char *path) {
Lennart Poettering48b8aaa2015-04-13 15:16:54 +0200597 _cleanup_close_ int fd = -1;
Michael Olbrich1924a972014-08-17 09:45:00 +0200598 struct stat st;
Michael Olbrich1924a972014-08-17 09:45:00 +0200599
Lennart Poetteringcde684a2014-06-10 22:50:46 +0200600 assert(i);
601 assert(path);
602
Lennart Poettering48b8aaa2015-04-13 15:16:54 +0200603 /* We open the file with O_PATH here, to make the operation
604 * somewhat atomic. Also there's unfortunately no fchmodat()
605 * with AT_SYMLINK_NOFOLLOW, hence we emulate it here via
606 * O_PATH. */
Michael Olbrich1924a972014-08-17 09:45:00 +0200607
Lennart Poettering48b8aaa2015-04-13 15:16:54 +0200608 fd = open(path, O_RDONLY|O_NOFOLLOW|O_CLOEXEC|O_PATH|O_NOATIME);
609 if (fd < 0)
610 return log_error_errno(errno, "Adjusting owner and mode for %s failed: %m", path);
Lennart Poetteringabef3f92014-06-11 10:14:07 +0200611
Lennart Poettering48b8aaa2015-04-13 15:16:54 +0200612 if (fstatat(fd, "", &st, AT_EMPTY_PATH) < 0)
613 return log_error_errno(errno, "Failed to fstat() file %s: %m", path);
614
615 if (S_ISLNK(st.st_mode))
616 log_debug("Skipping mode an owner fix for symlink %s.", path);
617 else {
618 char fn[strlen("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
619 xsprintf(fn, "/proc/self/fd/%i", fd);
620
621 /* not using i->path directly because it may be a glob */
622 if (i->mode_set) {
623 mode_t m = i->mode;
624
625 if (i->mask_perms) {
626 if (!(st.st_mode & 0111))
627 m &= ~0111;
628 if (!(st.st_mode & 0222))
Thomas Hindoe Paaboel Andersen7d6884b2015-08-06 00:31:09 +0200629 m &= ~0222;
Lennart Poettering48b8aaa2015-04-13 15:16:54 +0200630 if (!(st.st_mode & 0444))
631 m &= ~0444;
632 if (!S_ISDIR(st.st_mode))
633 m &= ~07000; /* remove sticky/sgid/suid bit, unless directory */
634 }
635
636 if (m == (st.st_mode & 07777))
637 log_debug("\"%s\" has right mode %o", path, st.st_mode);
638 else {
639 log_debug("chmod \"%s\" to mode %o", path, m);
640 if (chmod(fn, m) < 0)
641 return log_error_errno(errno, "chmod(%s) failed: %m", path);
642 }
Lennart Poetteringabef3f92014-06-11 10:14:07 +0200643 }
644
Lennart Poettering48b8aaa2015-04-13 15:16:54 +0200645 if ((i->uid != st.st_uid || i->gid != st.st_gid) &&
646 (i->uid_set || i->gid_set)) {
647 log_debug("chown \"%s\" to "UID_FMT"."GID_FMT,
648 path,
649 i->uid_set ? i->uid : UID_INVALID,
650 i->gid_set ? i->gid : GID_INVALID);
651 if (chown(fn,
652 i->uid_set ? i->uid : UID_INVALID,
653 i->gid_set ? i->gid : GID_INVALID) < 0)
Thomas Hindoe Paaboel Andersen7d6884b2015-08-06 00:31:09 +0200654 return log_error_errno(errno, "chown(%s) failed: %m", path);
Lennart Poettering48b8aaa2015-04-13 15:16:54 +0200655 }
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500656 }
Michal Schmidt062e01b2011-12-16 18:00:11 +0100657
Lennart Poettering48b8aaa2015-04-13 15:16:54 +0200658 fd = safe_close(fd);
659
Lennart Poettering9855d6c2014-06-11 09:19:57 +0200660 return label_fix(path, false, false);
Michal Schmidt062e01b2011-12-16 18:00:11 +0100661}
662
Lennart Poetteringbd550f72015-04-10 16:03:24 +0200663static int parse_xattrs_from_arg(Item *i) {
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100664 const char *p;
665 int r;
666
667 assert(i);
Zbigniew Jędrzejewski-Szmek505ef0e2015-01-09 01:10:02 -0500668 assert(i->argument);
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100669
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100670 p = i->argument;
671
Lennart Poettering4034a062015-03-23 18:55:36 +0700672 for (;;) {
Lennart Poetteringbd550f72015-04-10 16:03:24 +0200673 _cleanup_free_ char *name = NULL, *value = NULL, *xattr = NULL, *xattr_replaced = NULL;
Lennart Poettering4034a062015-03-23 18:55:36 +0700674
Richard Maw12ba2c42015-06-23 16:26:49 +0000675 r = extract_first_word(&p, &xattr, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE);
Lennart Poettering4034a062015-03-23 18:55:36 +0700676 if (r < 0)
Lennart Poetteringbd550f72015-04-10 16:03:24 +0200677 log_warning_errno(r, "Failed to parse extended attribute '%s', ignoring: %m", p);
Lennart Poettering4034a062015-03-23 18:55:36 +0700678 if (r <= 0)
679 break;
Zbigniew Jędrzejewski-Szmek505ef0e2015-01-09 01:10:02 -0500680
Lennart Poetteringbd550f72015-04-10 16:03:24 +0200681 r = specifier_printf(xattr, specifier_table, NULL, &xattr_replaced);
682 if (r < 0)
683 return log_error_errno(r, "Failed to replace specifiers in extended attribute '%s': %m", xattr);
684
685 r = split_pair(xattr_replaced, "=", &name, &value);
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100686 if (r < 0) {
Lennart Poettering4034a062015-03-23 18:55:36 +0700687 log_warning_errno(r, "Failed to parse extended attribute, ignoring: %s", xattr);
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100688 continue;
689 }
Zbigniew Jędrzejewski-Szmek505ef0e2015-01-09 01:10:02 -0500690
Lennart Poettering4034a062015-03-23 18:55:36 +0700691 if (isempty(name) || isempty(value)) {
Lennart Poetteringbd550f72015-04-10 16:03:24 +0200692 log_warning("Malformed extended attribute found, ignoring: %s", xattr);
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100693 continue;
694 }
Zbigniew Jędrzejewski-Szmek505ef0e2015-01-09 01:10:02 -0500695
Lennart Poettering4034a062015-03-23 18:55:36 +0700696 if (strv_push_pair(&i->xattrs, name, value) < 0)
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100697 return log_oom();
Zbigniew Jędrzejewski-Szmek505ef0e2015-01-09 01:10:02 -0500698
Lennart Poettering4034a062015-03-23 18:55:36 +0700699 name = value = NULL;
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100700 }
701
Lennart Poettering4034a062015-03-23 18:55:36 +0700702 return 0;
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100703}
704
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -0500705static int path_set_xattrs(Item *i, const char *path) {
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100706 char **name, **value;
707
708 assert(i);
709 assert(path);
710
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100711 STRV_FOREACH_PAIR(name, value, i->xattrs) {
712 int n;
Zbigniew Jędrzejewski-Szmek505ef0e2015-01-09 01:10:02 -0500713
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100714 n = strlen(*value);
Lennart Poetteringbd550f72015-04-10 16:03:24 +0200715 log_debug("Setting extended attribute '%s=%s' on %s.", *name, *value, path);
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100716 if (lsetxattr(path, *name, *value, n, 0) < 0) {
Lennart Poetteringbd550f72015-04-10 16:03:24 +0200717 log_error("Setting extended attribute %s=%s on %s failed: %m", *name, *value, path);
Maciej Wereskiebf4e802014-12-04 10:32:10 +0100718 return -errno;
719 }
720 }
721 return 0;
722}
723
Lennart Poetteringbd550f72015-04-10 16:03:24 +0200724static int parse_acls_from_arg(Item *item) {
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -0500725#ifdef HAVE_ACL
726 int r;
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -0500727
728 assert(item);
729
Zbigniew Jędrzejewski-Szmek50d9e462015-01-18 05:02:47 -0500730 /* If force (= modify) is set, we will not modify the acl
731 * afterwards, so the mask can be added now if necessary. */
Lennart Poetteringbd550f72015-04-10 16:03:24 +0200732
Zbigniew Jędrzejewski-Szmek50d9e462015-01-18 05:02:47 -0500733 r = parse_acl(item->argument, &item->acl_access, &item->acl_default, !item->force);
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -0500734 if (r < 0)
Lennart Poettering4034a062015-03-23 18:55:36 +0700735 log_warning_errno(r, "Failed to parse ACL \"%s\": %m. Ignoring", item->argument);
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -0500736#else
737 log_warning_errno(ENOSYS, "ACLs are not supported. Ignoring");
738#endif
739
740 return 0;
741}
742
Zbigniew Jędrzejewski-Szmek35888b62015-02-02 20:28:39 -0500743#ifdef HAVE_ACL
Lennart Poettering48b8aaa2015-04-13 15:16:54 +0200744static int path_set_acl(const char *path, const char *pretty, acl_type_t type, acl_t acl, bool modify) {
745 _cleanup_(acl_free_charpp) char *t = NULL;
Zbigniew Jędrzejewski-Szmekdd4105b2015-01-18 18:22:27 -0500746 _cleanup_(acl_freep) acl_t dup = NULL;
Zbigniew Jędrzejewski-Szmek50d9e462015-01-18 05:02:47 -0500747 int r;
748
Hans-Peter Deifeld873e872015-03-03 00:35:08 +0100749 /* Returns 0 for success, positive error if already warned,
750 * negative error otherwise. */
751
Zbigniew Jędrzejewski-Szmek50d9e462015-01-18 05:02:47 -0500752 if (modify) {
Zbigniew Jędrzejewski-Szmekdd4105b2015-01-18 18:22:27 -0500753 r = acls_for_file(path, type, acl, &dup);
Zbigniew Jędrzejewski-Szmek50d9e462015-01-18 05:02:47 -0500754 if (r < 0)
755 return r;
Zbigniew Jędrzejewski-Szmek50d9e462015-01-18 05:02:47 -0500756
Zbigniew Jędrzejewski-Szmekdd4105b2015-01-18 18:22:27 -0500757 r = calc_acl_mask_if_needed(&dup);
758 if (r < 0)
759 return r;
760 } else {
761 dup = acl_dup(acl);
762 if (!dup)
763 return -errno;
764
765 /* the mask was already added earlier if needed */
766 }
767
768 r = add_base_acls_if_needed(&dup, path);
769 if (r < 0)
770 return r;
771
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500772 t = acl_to_any_text(dup, NULL, ',', TEXT_ABBREVIATE);
Lennart Poettering48b8aaa2015-04-13 15:16:54 +0200773 log_debug("Setting %s ACL %s on %s.",
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500774 type == ACL_TYPE_ACCESS ? "access" : "default",
Lennart Poettering48b8aaa2015-04-13 15:16:54 +0200775 strna(t), pretty);
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500776
Zbigniew Jędrzejewski-Szmekdd4105b2015-01-18 18:22:27 -0500777 r = acl_set_file(path, type, dup);
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500778 if (r < 0)
Lennart Poettering3ea40b72015-04-10 14:44:52 +0200779 /* Return positive to indicate we already warned */
780 return -log_error_errno(errno,
781 "Setting %s ACL \"%s\" on %s failed: %m",
782 type == ACL_TYPE_ACCESS ? "access" : "default",
Lennart Poettering48b8aaa2015-04-13 15:16:54 +0200783 strna(t), pretty);
Hans-Peter Deifeld873e872015-03-03 00:35:08 +0100784
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500785 return 0;
Zbigniew Jędrzejewski-Szmek50d9e462015-01-18 05:02:47 -0500786}
Zbigniew Jędrzejewski-Szmek35888b62015-02-02 20:28:39 -0500787#endif
Zbigniew Jędrzejewski-Szmek50d9e462015-01-18 05:02:47 -0500788
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -0500789static int path_set_acls(Item *item, const char *path) {
Hans-Peter Deifeld873e872015-03-03 00:35:08 +0100790 int r = 0;
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -0500791#ifdef HAVE_ACL
Lennart Poettering48b8aaa2015-04-13 15:16:54 +0200792 char fn[strlen("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
793 _cleanup_close_ int fd = -1;
794 struct stat st;
795
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -0500796 assert(item);
797 assert(path);
798
Lennart Poettering48b8aaa2015-04-13 15:16:54 +0200799 fd = open(path, O_RDONLY|O_NOFOLLOW|O_CLOEXEC|O_PATH|O_NOATIME);
800 if (fd < 0)
801 return log_error_errno(errno, "Adjusting ACL of %s failed: %m", path);
802
803 if (fstatat(fd, "", &st, AT_EMPTY_PATH) < 0)
804 return log_error_errno(errno, "Failed to fstat() file %s: %m", path);
805
806 if (S_ISLNK(st.st_mode)) {
807 log_debug("Skipping ACL fix for symlink %s.", path);
808 return 0;
809 }
810
811 xsprintf(fn, "/proc/self/fd/%i", fd);
812
Hans-Peter Deifeld873e872015-03-03 00:35:08 +0100813 if (item->acl_access)
Lennart Poettering48b8aaa2015-04-13 15:16:54 +0200814 r = path_set_acl(fn, path, ACL_TYPE_ACCESS, item->acl_access, item->force);
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -0500815
Hans-Peter Deifeld873e872015-03-03 00:35:08 +0100816 if (r == 0 && item->acl_default)
Lennart Poettering48b8aaa2015-04-13 15:16:54 +0200817 r = path_set_acl(fn, path, ACL_TYPE_DEFAULT, item->acl_default, item->force);
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -0500818
Hans-Peter Deifeld873e872015-03-03 00:35:08 +0100819 if (r > 0)
820 return -r; /* already warned */
David Herrmann15411c02015-03-13 14:08:00 +0100821 else if (r == -EOPNOTSUPP) {
Hans-Peter Deifeld873e872015-03-03 00:35:08 +0100822 log_debug_errno(r, "ACLs not supported by file system at %s", path);
823 return 0;
824 } else if (r < 0)
825 log_error_errno(r, "ACL operation on \"%s\" failed: %m", path);
826#endif
827 return r;
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -0500828}
829
Lennart Poettering88ec4df2015-04-08 22:35:52 +0200830#define ATTRIBUTES_ALL \
831 (FS_NOATIME_FL | \
832 FS_SYNC_FL | \
833 FS_DIRSYNC_FL | \
834 FS_APPEND_FL | \
835 FS_COMPR_FL | \
836 FS_NODUMP_FL | \
837 FS_EXTENT_FL | \
838 FS_IMMUTABLE_FL | \
839 FS_JOURNAL_DATA_FL | \
840 FS_SECRM_FL | \
841 FS_UNRM_FL | \
842 FS_NOTAIL_FL | \
843 FS_TOPDIR_FL | \
844 FS_NOCOW_FL)
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +0100845
Lennart Poetteringbd550f72015-04-10 16:03:24 +0200846static int parse_attribute_from_arg(Item *item) {
Lennart Poettering88ec4df2015-04-08 22:35:52 +0200847
848 static const struct {
849 char character;
850 unsigned value;
851 } attributes[] = {
852 { 'A', FS_NOATIME_FL }, /* do not update atime */
853 { 'S', FS_SYNC_FL }, /* Synchronous updates */
854 { 'D', FS_DIRSYNC_FL }, /* dirsync behaviour (directories only) */
855 { 'a', FS_APPEND_FL }, /* writes to file may only append */
856 { 'c', FS_COMPR_FL }, /* Compress file */
857 { 'd', FS_NODUMP_FL }, /* do not dump file */
858 { 'e', FS_EXTENT_FL }, /* Top of directory hierarchies*/
859 { 'i', FS_IMMUTABLE_FL }, /* Immutable file */
860 { 'j', FS_JOURNAL_DATA_FL }, /* Reserved for ext3 */
861 { 's', FS_SECRM_FL }, /* Secure deletion */
862 { 'u', FS_UNRM_FL }, /* Undelete */
863 { 't', FS_NOTAIL_FL }, /* file tail should not be merged */
864 { 'T', FS_TOPDIR_FL }, /* Top of directory hierarchies*/
865 { 'C', FS_NOCOW_FL }, /* Do not cow file */
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +0100866 };
Lennart Poettering88ec4df2015-04-08 22:35:52 +0200867
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +0100868 enum {
869 MODE_ADD,
870 MODE_DEL,
871 MODE_SET
872 } mode = MODE_ADD;
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +0100873
Lennart Poettering88ec4df2015-04-08 22:35:52 +0200874 unsigned value = 0, mask = 0;
875 const char *p;
876
877 assert(item);
878
879 p = item->argument;
880 if (p) {
881 if (*p == '+') {
882 mode = MODE_ADD;
883 p++;
884 } else if (*p == '-') {
885 mode = MODE_DEL;
886 p++;
887 } else if (*p == '=') {
888 mode = MODE_SET;
889 p++;
890 }
891 }
892
893 if (isempty(p) && mode != MODE_SET) {
894 log_error("Setting file attribute on '%s' needs an attribute specification.", item->path);
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +0100895 return -EINVAL;
896 }
897
Lennart Poettering88ec4df2015-04-08 22:35:52 +0200898 for (; p && *p ; p++) {
899 unsigned i, v;
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +0100900
Lennart Poettering88ec4df2015-04-08 22:35:52 +0200901 for (i = 0; i < ELEMENTSOF(attributes); i++)
902 if (*p == attributes[i].character)
903 break;
904
905 if (i >= ELEMENTSOF(attributes)) {
906 log_error("Unknown file attribute '%c' on '%s'.", *p, item->path);
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +0100907 return -EINVAL;
908 }
Lennart Poettering88ec4df2015-04-08 22:35:52 +0200909
910 v = attributes[i].value;
911
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +0100912 if (mode == MODE_ADD || mode == MODE_SET)
Lennart Poettering88ec4df2015-04-08 22:35:52 +0200913 value |= v;
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +0100914 else
Lennart Poettering88ec4df2015-04-08 22:35:52 +0200915 value &= ~v;
916
917 mask |= v;
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +0100918 }
919
920 if (mode == MODE_SET)
Lennart Poettering88ec4df2015-04-08 22:35:52 +0200921 mask |= ATTRIBUTES_ALL;
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +0100922
Lennart Poettering88ec4df2015-04-08 22:35:52 +0200923 assert(mask != 0);
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +0100924
Lennart Poettering88ec4df2015-04-08 22:35:52 +0200925 item->attribute_mask = mask;
926 item->attribute_value = value;
927 item->attribute_set = true;
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +0100928
929 return 0;
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +0100930}
931
Lennart Poettering88ec4df2015-04-08 22:35:52 +0200932static int path_set_attribute(Item *item, const char *path) {
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +0100933 _cleanup_close_ int fd = -1;
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +0100934 struct stat st;
Lennart Poettering88ec4df2015-04-08 22:35:52 +0200935 unsigned f;
936 int r;
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +0100937
Lennart Poettering88ec4df2015-04-08 22:35:52 +0200938 if (!item->attribute_set || item->attribute_mask == 0)
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +0100939 return 0;
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +0100940
Lennart Poettering48b8aaa2015-04-13 15:16:54 +0200941 fd = open(path, O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_NOATIME|O_NOFOLLOW);
942 if (fd < 0) {
943 if (errno == ELOOP)
944 return log_error_errno(errno, "Skipping file attributes adjustment on symlink %s.", path);
945
Lennart Poettering88ec4df2015-04-08 22:35:52 +0200946 return log_error_errno(errno, "Cannot open '%s': %m", path);
Lennart Poettering48b8aaa2015-04-13 15:16:54 +0200947 }
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +0100948
Lennart Poettering88ec4df2015-04-08 22:35:52 +0200949 if (fstat(fd, &st) < 0)
950 return log_error_errno(errno, "Cannot stat '%s': %m", path);
951
952 /* Issuing the file attribute ioctls on device nodes is not
953 * safe, as that will be delivered to the drivers, not the
954 * file system containing the device node. */
955 if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode)) {
956 log_error("Setting file flags is only supported on regular files and directories, cannot set on '%s'.", path);
957 return -EINVAL;
958 }
959
960 f = item->attribute_value & item->attribute_mask;
961
962 /* Mask away directory-specific flags */
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +0100963 if (!S_ISDIR(st.st_mode))
964 f &= ~FS_DIRSYNC_FL;
Lennart Poettering88ec4df2015-04-08 22:35:52 +0200965
966 r = chattr_fd(fd, f, item->attribute_mask);
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +0100967 if (r < 0)
Lennart Poetteringad75a972015-07-22 22:02:14 +0200968 log_full_errno(r == -ENOTTY ? LOG_DEBUG : LOG_WARNING,
969 r,
970 "Cannot set file attribute for '%s', value=0x%08x, mask=0x%08x: %m",
971 path, item->attribute_value, item->attribute_mask);
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +0100972
973 return 0;
974}
975
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400976static int write_one_file(Item *i, const char *path) {
Lennart Poettering43ad6e32014-06-18 00:01:39 +0200977 _cleanup_close_ int fd = -1;
978 int flags, r = 0;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400979 struct stat st;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400980
Lennart Poettering874f1942014-06-10 22:48:56 +0200981 assert(i);
982 assert(path);
983
Lennart Poettering43ad6e32014-06-18 00:01:39 +0200984 flags = i->type == CREATE_FILE ? O_CREAT|O_APPEND|O_NOFOLLOW :
985 i->type == TRUNCATE_FILE ? O_CREAT|O_TRUNC|O_NOFOLLOW : 0;
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400986
Lennart Poettering43ad6e32014-06-18 00:01:39 +0200987 RUN_WITH_UMASK(0000) {
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200988 mac_selinux_create_file_prepare(path, S_IFREG);
Lennart Poettering43ad6e32014-06-18 00:01:39 +0200989 fd = open(path, flags|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY, i->mode);
Lennart Poetteringecabcf82014-10-23 19:41:27 +0200990 mac_selinux_create_file_clear();
Lennart Poettering5c0d3982013-04-04 03:39:39 +0200991 }
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400992
993 if (fd < 0) {
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500994 if (i->type == WRITE_FILE && errno == ENOENT) {
995 log_debug_errno(errno, "Not writing \"%s\": %m", path);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400996 return 0;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -0500997 }
Dave Reisnerd4e9eb92012-09-03 17:13:18 -0400998
Michael Olbrichf44b28f2015-04-30 20:50:38 +0200999 r = -errno;
1000 if (!i->argument && errno == EROFS && stat(path, &st) == 0 &&
1001 (i->type == CREATE_FILE || st.st_size == 0))
1002 goto check_mode;
1003
1004 return log_error_errno(r, "Failed to create file %s: %m", path);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -04001005 }
1006
1007 if (i->argument) {
Lennart Poetteringbd550f72015-04-10 16:03:24 +02001008 _cleanup_free_ char *unescaped = NULL, *replaced = NULL;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001009
Lennart Poettering4034a062015-03-23 18:55:36 +07001010 log_debug("%s to \"%s\".", i->type == CREATE_FILE ? "Appending" : "Writing", path);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -04001011
Lennart Poettering527b7a42015-04-06 20:11:41 +02001012 r = cunescape(i->argument, 0, &unescaped);
1013 if (r < 0)
1014 return log_error_errno(r, "Failed to unescape parameter to write: %s", i->argument);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -04001015
Lennart Poetteringbd550f72015-04-10 16:03:24 +02001016 r = specifier_printf(unescaped, specifier_table, NULL, &replaced);
1017 if (r < 0)
1018 return log_error_errno(r, "Failed to replace specifiers in parameter to write '%s': %m", unescaped);
1019
1020 r = loop_write(fd, replaced, strlen(replaced), false);
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001021 if (r < 0)
1022 return log_error_errno(r, "Failed to write file \"%s\": %m", path);
1023 } else
1024 log_debug("\"%s\" has been created.", path);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -04001025
Lennart Poettering43ad6e32014-06-18 00:01:39 +02001026 fd = safe_close(fd);
Dave Reisner3612fbc2012-09-12 16:21:00 -04001027
Michal Schmidt4a62c712014-11-28 19:57:32 +01001028 if (stat(path, &st) < 0)
1029 return log_error_errno(errno, "stat(%s) failed: %m", path);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -04001030
Michael Olbrichf44b28f2015-04-30 20:50:38 +02001031 check_mode:
Dave Reisnerd4e9eb92012-09-03 17:13:18 -04001032 if (!S_ISREG(st.st_mode)) {
1033 log_error("%s is not a file.", path);
1034 return -EEXIST;
1035 }
1036
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -05001037 r = path_set_perms(i, path);
Dave Reisnerd4e9eb92012-09-03 17:13:18 -04001038 if (r < 0)
1039 return r;
1040
1041 return 0;
1042}
1043
Zbigniew Jędrzejewski-Szmek081043c2015-01-18 01:33:39 -05001044typedef int (*action_t)(Item *, const char *);
1045
1046static int item_do_children(Item *i, const char *path, action_t action) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +02001047 _cleanup_closedir_ DIR *d;
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001048 int r = 0;
1049
1050 assert(i);
1051 assert(path);
Michal Schmidta8d88782011-12-15 23:11:07 +01001052
1053 /* This returns the first error we run into, but nevertheless
1054 * tries to go on */
1055
Zbigniew Jędrzejewski-Szmekdf99a9e2014-01-02 00:02:31 -05001056 d = opendir_nomod(path);
1057 if (!d)
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001058 return errno == ENOENT || errno == ENOTDIR ? 0 : -errno;
Michal Schmidta8d88782011-12-15 23:11:07 +01001059
1060 for (;;) {
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001061 _cleanup_free_ char *p = NULL;
Lennart Poettering7d5e9c02012-09-19 22:21:09 +02001062 struct dirent *de;
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001063 int q;
Michal Schmidta8d88782011-12-15 23:11:07 +01001064
Florian Weimerd78096b2013-12-19 12:26:07 +01001065 errno = 0;
1066 de = readdir(d);
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001067 if (!de) {
1068 if (errno != 0 && r == 0)
1069 r = -errno;
1070
Michal Schmidta8d88782011-12-15 23:11:07 +01001071 break;
1072 }
1073
Zbigniew Jędrzejewski-Szmek7fcb4b92015-01-22 23:35:34 -05001074 if (STR_IN_SET(de->d_name, ".", ".."))
Michal Schmidta8d88782011-12-15 23:11:07 +01001075 continue;
1076
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001077 p = strjoin(path, "/", de->d_name, NULL);
1078 if (!p)
1079 return -ENOMEM;
Michal Schmidta8d88782011-12-15 23:11:07 +01001080
Zbigniew Jędrzejewski-Szmek081043c2015-01-18 01:33:39 -05001081 q = action(i, p);
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001082 if (q < 0 && q != -ENOENT && r == 0)
1083 r = q;
Michal Schmidta8d88782011-12-15 23:11:07 +01001084
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001085 if (IN_SET(de->d_type, DT_UNKNOWN, DT_DIR)) {
Zbigniew Jędrzejewski-Szmek081043c2015-01-18 01:33:39 -05001086 q = item_do_children(i, p, action);
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001087 if (q < 0 && r == 0)
1088 r = q;
Michal Schmidta8d88782011-12-15 23:11:07 +01001089 }
Michal Schmidta8d88782011-12-15 23:11:07 +01001090 }
1091
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001092 return r;
Michal Schmidta8d88782011-12-15 23:11:07 +01001093}
1094
Zbigniew Jędrzejewski-Szmek081043c2015-01-18 01:33:39 -05001095static int glob_item(Item *i, action_t action, bool recursive) {
Zbigniew Jędrzejewski-Szmekdf99a9e2014-01-02 00:02:31 -05001096 _cleanup_globfree_ glob_t g = {
Zbigniew Jędrzejewski-Szmekebf31a12015-01-26 10:39:03 -05001097 .gl_closedir = (void (*)(void *)) closedir,
1098 .gl_readdir = (struct dirent *(*)(void *)) readdir,
1099 .gl_opendir = (void *(*)(const char *)) opendir_nomod,
Zbigniew Jędrzejewski-Szmekdf99a9e2014-01-02 00:02:31 -05001100 .gl_lstat = lstat,
1101 .gl_stat = stat,
1102 };
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001103 int r = 0, k;
Michal Schmidt99e68c02011-12-15 23:45:26 +01001104 char **fn;
1105
Michal Schmidt99e68c02011-12-15 23:45:26 +01001106 errno = 0;
Zbigniew Jędrzejewski-Szmekdf99a9e2014-01-02 00:02:31 -05001107 k = glob(i->path, GLOB_NOSORT|GLOB_BRACE|GLOB_ALTDIRFUNC, NULL, &g);
Zbigniew Jędrzejewski-Szmek081043c2015-01-18 01:33:39 -05001108 if (k != 0 && k != GLOB_NOMATCH)
1109 return log_error_errno(errno ?: EIO, "glob(%s) failed: %m", i->path);
Zbigniew Jędrzejewski-Szmekc84a9482013-03-24 19:09:19 -04001110
1111 STRV_FOREACH(fn, g.gl_pathv) {
1112 k = action(i, *fn);
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001113 if (k < 0 && r == 0)
Zbigniew Jędrzejewski-Szmekc84a9482013-03-24 19:09:19 -04001114 r = k;
Zbigniew Jędrzejewski-Szmek081043c2015-01-18 01:33:39 -05001115
1116 if (recursive) {
1117 k = item_do_children(i, *fn, action);
1118 if (k < 0 && r == 0)
1119 r = k;
1120 }
Michal Schmidt99e68c02011-12-15 23:45:26 +01001121 }
1122
Michal Schmidt99e68c02011-12-15 23:45:26 +01001123 return r;
1124}
1125
Zbigniew Jędrzejewski-Szmek294929f2015-02-01 12:29:27 -05001126typedef enum {
1127 CREATION_NORMAL,
1128 CREATION_EXISTING,
1129 CREATION_FORCE,
Lennart Poettering7a7d5db2015-02-02 21:34:09 +01001130 _CREATION_MODE_MAX,
1131 _CREATION_MODE_INVALID = -1
Zbigniew Jędrzejewski-Szmek294929f2015-02-01 12:29:27 -05001132} CreationMode;
1133
Lennart Poettering7a7d5db2015-02-02 21:34:09 +01001134static const char *creation_mode_verb_table[_CREATION_MODE_MAX] = {
1135 [CREATION_NORMAL] = "Created",
1136 [CREATION_EXISTING] = "Found existing",
1137 [CREATION_FORCE] = "Created replacement",
1138};
1139
1140DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(creation_mode_verb, CreationMode);
Zbigniew Jędrzejewski-Szmek294929f2015-02-01 12:29:27 -05001141
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001142static int create_item(Item *i) {
Thomas Hindoe Paaboel Andersen75c2a9f2015-04-10 23:26:21 +02001143 _cleanup_free_ char *resolved = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001144 struct stat st;
Kay Sieversdf28bc02013-10-21 15:24:18 +02001145 int r = 0;
Zbigniew Jędrzejewski-Szmek294929f2015-02-01 12:29:27 -05001146 CreationMode creation;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001147
1148 assert(i);
1149
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001150 log_debug("Running create action for entry %c %s", (char) i->type, i->path);
1151
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001152 switch (i->type) {
1153
1154 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +01001155 case IGNORE_DIRECTORY_PATH:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001156 case REMOVE_PATH:
1157 case RECURSIVE_REMOVE_PATH:
1158 return 0;
1159
1160 case CREATE_FILE:
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001161 case TRUNCATE_FILE:
Dave Reisner1845fdd2012-09-27 20:48:13 -04001162 r = write_one_file(i, i->path);
1163 if (r < 0)
1164 return r;
1165 break;
Lennart Poettering265ffa12013-09-17 16:33:30 -05001166
Lennart Poettering5c5ccf12015-04-10 18:07:04 +02001167 case COPY_FILES: {
Lennart Poettering5c5ccf12015-04-10 18:07:04 +02001168 r = specifier_printf(i->argument, specifier_table, NULL, &resolved);
1169 if (r < 0)
1170 return log_error_errno(r, "Failed to substitute specifiers in copy source %s: %m", i->argument);
1171
1172 log_debug("Copying tree \"%s\" to \"%s\".", resolved, i->path);
1173 r = copy_tree(resolved, i->path, false);
Michael Olbrichf44b28f2015-04-30 20:50:38 +02001174
1175 if (r == -EROFS && stat(i->path, &st) == 0)
1176 r = -EEXIST;
1177
Lennart Poettering849958d2014-06-10 23:02:40 +02001178 if (r < 0) {
Lennart Poetteringe1563472014-06-19 19:36:08 +02001179 struct stat a, b;
1180
Michal Schmidt8d3d7072014-11-28 19:13:53 +01001181 if (r != -EEXIST)
1182 return log_error_errno(r, "Failed to copy files to %s: %m", i->path);
Lennart Poetteringe1563472014-06-19 19:36:08 +02001183
Lennart Poettering5c5ccf12015-04-10 18:07:04 +02001184 if (stat(resolved, &a) < 0)
1185 return log_error_errno(errno, "stat(%s) failed: %m", resolved);
Lennart Poetteringe1563472014-06-19 19:36:08 +02001186
Michal Schmidt4a62c712014-11-28 19:57:32 +01001187 if (stat(i->path, &b) < 0)
1188 return log_error_errno(errno, "stat(%s) failed: %m", i->path);
Lennart Poetteringe1563472014-06-19 19:36:08 +02001189
1190 if ((a.st_mode ^ b.st_mode) & S_IFMT) {
1191 log_debug("Can't copy to %s, file exists already and is of different type", i->path);
1192 return 0;
1193 }
Lennart Poettering849958d2014-06-10 23:02:40 +02001194 }
1195
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -05001196 r = path_set_perms(i, i->path);
Lennart Poettering849958d2014-06-10 23:02:40 +02001197 if (r < 0)
1198 return r;
1199
1200 break;
1201
Dave Reisnerd4e9eb92012-09-03 17:13:18 -04001202 case WRITE_FILE:
Zbigniew Jędrzejewski-Szmek081043c2015-01-18 01:33:39 -05001203 r = glob_item(i, write_one_file, false);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +01001204 if (r < 0)
1205 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001206
1207 break;
1208
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001209 case CREATE_DIRECTORY:
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +01001210 case TRUNCATE_DIRECTORY:
1211 case CREATE_SUBVOLUME:
Lennart Poettering5fb13eb2015-10-21 19:46:23 +02001212 case CREATE_SUBVOLUME_INHERIT_QUOTA:
1213 case CREATE_SUBVOLUME_NEW_QUOTA:
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001214
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +01001215 RUN_WITH_UMASK(0000)
Lennart Poettering5c0d3982013-04-04 03:39:39 +02001216 mkdir_parents_label(i->path, 0755);
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +01001217
Lennart Poettering5fb13eb2015-10-21 19:46:23 +02001218 if (IN_SET(i->type, CREATE_SUBVOLUME, CREATE_SUBVOLUME_INHERIT_QUOTA, CREATE_SUBVOLUME_NEW_QUOTA)) {
1219 RUN_WITH_UMASK((~i->mode) & 0777)
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +01001220 r = btrfs_subvol_make(i->path);
Lennart Poettering5fb13eb2015-10-21 19:46:23 +02001221 } else
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +01001222 r = 0;
1223
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001224 if (IN_SET(i->type, CREATE_DIRECTORY, TRUNCATE_DIRECTORY) || r == -ENOTTY)
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +01001225 RUN_WITH_UMASK(0000)
1226 r = mkdir_label(i->path, i->mode);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001227
Lennart Poetteringe1563472014-06-19 19:36:08 +02001228 if (r < 0) {
Lennart Poettering7b135a72015-05-15 21:47:22 +02001229 int k;
1230
1231 if (r != -EEXIST && r != -EROFS)
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001232 return log_error_errno(r, "Failed to create directory or subvolume \"%s\": %m", i->path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001233
Lennart Poettering7b135a72015-05-15 21:47:22 +02001234 k = is_dir(i->path, false);
1235 if (k == -ENOENT && r == -EROFS)
1236 return log_error_errno(r, "%s does not exist and cannot be created as the file system is read-only.", i->path);
1237 if (k < 0)
1238 return log_error_errno(k, "Failed to check if %s exists: %m", i->path);
1239 if (!k) {
1240 log_warning("\"%s\" already exists and is not a directory.", i->path);
Lennart Poetteringe1563472014-06-19 19:36:08 +02001241 return 0;
1242 }
Zbigniew Jędrzejewski-Szmek294929f2015-02-01 12:29:27 -05001243
1244 creation = CREATION_EXISTING;
1245 } else
1246 creation = CREATION_NORMAL;
Lennart Poettering7b135a72015-05-15 21:47:22 +02001247
Lennart Poettering7a7d5db2015-02-02 21:34:09 +01001248 log_debug("%s directory \"%s\".", creation_mode_verb_to_string(creation), i->path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001249
Lennart Poettering5fb13eb2015-10-21 19:46:23 +02001250 if (IN_SET(i->type, CREATE_SUBVOLUME_NEW_QUOTA, CREATE_SUBVOLUME_INHERIT_QUOTA)) {
1251 r = btrfs_subvol_auto_qgroup(i->path, 0, i->type == CREATE_SUBVOLUME_NEW_QUOTA);
1252 if (r == -ENOTTY) {
1253 log_debug_errno(r, "Couldn't adjust quota for subvolume \"%s\" because of unsupported file system or because directory is not a subvolume: %m", i->path);
1254 return 0;
1255 }
1256 if (r == -EROFS) {
1257 log_debug_errno(r, "Couldn't adjust quota for subvolume \"%s\" because of read-only file system: %m", i->path);
1258 return 0;
1259 }
1260 if (r < 0)
1261 return log_error_errno(r, "Failed to adjust quota for subvolume \"%s\": %m", i->path);
1262 if (r > 0)
1263 log_debug("Adjusted quota for subvolume \"%s\".", i->path);
1264 if (r == 0)
1265 log_debug("Quota for subvolume \"%s\" already in place, no change made.", i->path);
1266 }
1267
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -05001268 r = path_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +01001269 if (r < 0)
1270 return r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001271
1272 break;
Lennart Poetteringee17ee72011-07-12 03:56:56 +02001273
1274 case CREATE_FIFO:
1275
Lennart Poettering5c0d3982013-04-04 03:39:39 +02001276 RUN_WITH_UMASK(0000) {
Lennart Poetteringecabcf82014-10-23 19:41:27 +02001277 mac_selinux_create_file_prepare(i->path, S_IFIFO);
Lennart Poettering5c0d3982013-04-04 03:39:39 +02001278 r = mkfifo(i->path, i->mode);
Lennart Poetteringecabcf82014-10-23 19:41:27 +02001279 mac_selinux_create_file_clear();
Lennart Poetteringee17ee72011-07-12 03:56:56 +02001280 }
1281
Lennart Poettering1554afa2014-06-17 23:50:22 +02001282 if (r < 0) {
Michal Schmidt4a62c712014-11-28 19:57:32 +01001283 if (errno != EEXIST)
1284 return log_error_errno(errno, "Failed to create fifo %s: %m", i->path);
Lennart Poetteringee17ee72011-07-12 03:56:56 +02001285
Lennart Poetteringa542c4d2015-05-15 21:48:20 +02001286 if (lstat(i->path, &st) < 0)
Michal Schmidt4a62c712014-11-28 19:57:32 +01001287 return log_error_errno(errno, "stat(%s) failed: %m", i->path);
Lennart Poettering1554afa2014-06-17 23:50:22 +02001288
1289 if (!S_ISFIFO(st.st_mode)) {
1290
1291 if (i->force) {
Lennart Poettering1554afa2014-06-17 23:50:22 +02001292 RUN_WITH_UMASK(0000) {
Lennart Poetteringecabcf82014-10-23 19:41:27 +02001293 mac_selinux_create_file_prepare(i->path, S_IFIFO);
Lennart Poettering1554afa2014-06-17 23:50:22 +02001294 r = mkfifo_atomic(i->path, i->mode);
Lennart Poetteringecabcf82014-10-23 19:41:27 +02001295 mac_selinux_create_file_clear();
Lennart Poettering1554afa2014-06-17 23:50:22 +02001296 }
1297
Michal Schmidtf6479622014-11-28 18:50:43 +01001298 if (r < 0)
1299 return log_error_errno(r, "Failed to create fifo %s: %m", i->path);
Zbigniew Jędrzejewski-Szmek294929f2015-02-01 12:29:27 -05001300 creation = CREATION_FORCE;
Lennart Poettering1554afa2014-06-17 23:50:22 +02001301 } else {
Lennart Poetteringa542c4d2015-05-15 21:48:20 +02001302 log_warning("\"%s\" already exists and is not a fifo.", i->path);
Lennart Poettering1554afa2014-06-17 23:50:22 +02001303 return 0;
1304 }
Zbigniew Jędrzejewski-Szmek294929f2015-02-01 12:29:27 -05001305 } else
1306 creation = CREATION_EXISTING;
1307 } else
1308 creation = CREATION_NORMAL;
Lennart Poettering7a7d5db2015-02-02 21:34:09 +01001309 log_debug("%s fifo \"%s\".", creation_mode_verb_to_string(creation), i->path);
Lennart Poetteringee17ee72011-07-12 03:56:56 +02001310
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -05001311 r = path_set_perms(i, i->path);
Michal Schmidtf05bc3f2011-12-15 23:44:23 +01001312 if (r < 0)
1313 return r;
Lennart Poetteringee17ee72011-07-12 03:56:56 +02001314
1315 break;
Lennart Poettering5c5ccf12015-04-10 18:07:04 +02001316 }
Michal Schmidta8d88782011-12-15 23:11:07 +01001317
Lennart Poettering5c5ccf12015-04-10 18:07:04 +02001318 case CREATE_SYMLINK: {
Lennart Poettering5c5ccf12015-04-10 18:07:04 +02001319 r = specifier_printf(i->argument, specifier_table, NULL, &resolved);
1320 if (r < 0)
1321 return log_error_errno(r, "Failed to substitute specifiers in symlink target %s: %m", i->argument);
Lennart Poettering468d7262012-01-17 15:04:12 +01001322
Lennart Poetteringecabcf82014-10-23 19:41:27 +02001323 mac_selinux_create_file_prepare(i->path, S_IFLNK);
Lennart Poettering5c5ccf12015-04-10 18:07:04 +02001324 r = symlink(resolved, i->path);
Lennart Poetteringecabcf82014-10-23 19:41:27 +02001325 mac_selinux_create_file_clear();
Kay Sieverse9a5ef72012-04-17 16:05:03 +02001326
Lennart Poettering468d7262012-01-17 15:04:12 +01001327 if (r < 0) {
Lennart Poettering2e78fa72014-06-16 13:21:07 +02001328 _cleanup_free_ char *x = NULL;
Lennart Poettering468d7262012-01-17 15:04:12 +01001329
Michal Schmidt4a62c712014-11-28 19:57:32 +01001330 if (errno != EEXIST)
Lennart Poettering5c5ccf12015-04-10 18:07:04 +02001331 return log_error_errno(errno, "symlink(%s, %s) failed: %m", resolved, i->path);
Lennart Poettering2e78fa72014-06-16 13:21:07 +02001332
1333 r = readlink_malloc(i->path, &x);
Lennart Poettering5c5ccf12015-04-10 18:07:04 +02001334 if (r < 0 || !streq(resolved, x)) {
Lennart Poettering2e78fa72014-06-16 13:21:07 +02001335
1336 if (i->force) {
Lennart Poetteringecabcf82014-10-23 19:41:27 +02001337 mac_selinux_create_file_prepare(i->path, S_IFLNK);
Lennart Poettering5c5ccf12015-04-10 18:07:04 +02001338 r = symlink_atomic(resolved, i->path);
Lennart Poetteringecabcf82014-10-23 19:41:27 +02001339 mac_selinux_create_file_clear();
Lennart Poettering2e78fa72014-06-16 13:21:07 +02001340
Michal Schmidtf6479622014-11-28 18:50:43 +01001341 if (r < 0)
Lennart Poettering5c5ccf12015-04-10 18:07:04 +02001342 return log_error_errno(r, "symlink(%s, %s) failed: %m", resolved, i->path);
Lennart Poetteringa542c4d2015-05-15 21:48:20 +02001343
Zbigniew Jędrzejewski-Szmek294929f2015-02-01 12:29:27 -05001344 creation = CREATION_FORCE;
Lennart Poettering1554afa2014-06-17 23:50:22 +02001345 } else {
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001346 log_debug("\"%s\" is not a symlink or does not point to the correct path.", i->path);
Lennart Poettering1554afa2014-06-17 23:50:22 +02001347 return 0;
1348 }
Zbigniew Jędrzejewski-Szmek294929f2015-02-01 12:29:27 -05001349 } else
1350 creation = CREATION_EXISTING;
1351 } else
Lennart Poetteringa542c4d2015-05-15 21:48:20 +02001352
Zbigniew Jędrzejewski-Szmek294929f2015-02-01 12:29:27 -05001353 creation = CREATION_NORMAL;
Lennart Poettering7a7d5db2015-02-02 21:34:09 +01001354 log_debug("%s symlink \"%s\".", creation_mode_verb_to_string(creation), i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +01001355 break;
Lennart Poettering5c5ccf12015-04-10 18:07:04 +02001356 }
Lennart Poettering468d7262012-01-17 15:04:12 +01001357
1358 case CREATE_BLOCK_DEVICE:
1359 case CREATE_CHAR_DEVICE: {
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -07001360 mode_t file_type;
1361
1362 if (have_effective_cap(CAP_MKNOD) == 0) {
1363 /* In a container we lack CAP_MKNOD. We
Anatol Pomozovab06eef2013-04-14 19:37:54 -07001364 shouldn't attempt to create the device node in
Lennart Poetteringcb7ed9d2012-09-05 23:39:55 -07001365 that case to avoid noise, and we don't support
1366 virtualized devices in containers anyway. */
1367
1368 log_debug("We lack CAP_MKNOD, skipping creation of device node %s.", i->path);
1369 return 0;
1370 }
1371
Lennart Poettering1554afa2014-06-17 23:50:22 +02001372 file_type = i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR;
Lennart Poettering468d7262012-01-17 15:04:12 +01001373
Lennart Poettering5c0d3982013-04-04 03:39:39 +02001374 RUN_WITH_UMASK(0000) {
Lennart Poetteringecabcf82014-10-23 19:41:27 +02001375 mac_selinux_create_file_prepare(i->path, file_type);
Lennart Poettering5c0d3982013-04-04 03:39:39 +02001376 r = mknod(i->path, i->mode | file_type, i->major_minor);
Lennart Poetteringecabcf82014-10-23 19:41:27 +02001377 mac_selinux_create_file_clear();
Lennart Poettering5c0d3982013-04-04 03:39:39 +02001378 }
Lennart Poettering468d7262012-01-17 15:04:12 +01001379
Kay Sievers6555ad82014-06-13 04:11:11 +02001380 if (r < 0) {
1381 if (errno == EPERM) {
1382 log_debug("We lack permissions, possibly because of cgroup configuration; "
1383 "skipping creation of device node %s.", i->path);
1384 return 0;
1385 }
1386
Michal Schmidt4a62c712014-11-28 19:57:32 +01001387 if (errno != EEXIST)
1388 return log_error_errno(errno, "Failed to create device node %s: %m", i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +01001389
Lennart Poetteringa542c4d2015-05-15 21:48:20 +02001390 if (lstat(i->path, &st) < 0)
Michal Schmidt4a62c712014-11-28 19:57:32 +01001391 return log_error_errno(errno, "stat(%s) failed: %m", i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +01001392
Lennart Poettering1554afa2014-06-17 23:50:22 +02001393 if ((st.st_mode & S_IFMT) != file_type) {
1394
1395 if (i->force) {
1396
1397 RUN_WITH_UMASK(0000) {
Lennart Poetteringecabcf82014-10-23 19:41:27 +02001398 mac_selinux_create_file_prepare(i->path, file_type);
Lennart Poettering1554afa2014-06-17 23:50:22 +02001399 r = mknod_atomic(i->path, i->mode | file_type, i->major_minor);
Lennart Poetteringecabcf82014-10-23 19:41:27 +02001400 mac_selinux_create_file_clear();
Lennart Poettering1554afa2014-06-17 23:50:22 +02001401 }
1402
Michal Schmidtf6479622014-11-28 18:50:43 +01001403 if (r < 0)
Zbigniew Jędrzejewski-Szmek294929f2015-02-01 12:29:27 -05001404 return log_error_errno(r, "Failed to create device node \"%s\": %m", i->path);
1405 creation = CREATION_FORCE;
Lennart Poettering1554afa2014-06-17 23:50:22 +02001406 } else {
1407 log_debug("%s is not a device node.", i->path);
1408 return 0;
1409 }
Zbigniew Jędrzejewski-Szmek294929f2015-02-01 12:29:27 -05001410 } else
1411 creation = CREATION_EXISTING;
1412 } else
1413 creation = CREATION_NORMAL;
Lennart Poetteringa542c4d2015-05-15 21:48:20 +02001414
Zbigniew Jędrzejewski-Szmek294929f2015-02-01 12:29:27 -05001415 log_debug("%s %s device node \"%s\" %u:%u.",
Lennart Poettering7a7d5db2015-02-02 21:34:09 +01001416 creation_mode_verb_to_string(creation),
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001417 i->type == CREATE_BLOCK_DEVICE ? "block" : "char",
1418 i->path, major(i->mode), minor(i->mode));
Lennart Poettering468d7262012-01-17 15:04:12 +01001419
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -05001420 r = path_set_perms(i, i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +01001421 if (r < 0)
1422 return r;
1423
1424 break;
1425 }
1426
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001427 case ADJUST_MODE:
Michal Schmidt777b87e2011-12-16 18:27:35 +01001428 case RELABEL_PATH:
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -05001429 r = glob_item(i, path_set_perms, false);
Michal Schmidt777b87e2011-12-16 18:27:35 +01001430 if (r < 0)
Lennart Poettering96ca8192013-06-21 15:57:42 +02001431 return r;
Michal Schmidt777b87e2011-12-16 18:27:35 +01001432 break;
1433
Michal Schmidta8d88782011-12-15 23:11:07 +01001434 case RECURSIVE_RELABEL_PATH:
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -05001435 r = glob_item(i, path_set_perms, true);
Michal Schmidta8d88782011-12-15 23:11:07 +01001436 if (r < 0)
1437 return r;
Maciej Wereskiebf4e802014-12-04 10:32:10 +01001438 break;
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001439
Maciej Wereskiebf4e802014-12-04 10:32:10 +01001440 case SET_XATTR:
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -05001441 r = glob_item(i, path_set_xattrs, false);
1442 if (r < 0)
1443 return r;
1444 break;
1445
1446 case RECURSIVE_SET_XATTR:
1447 r = glob_item(i, path_set_xattrs, true);
Maciej Wereskiebf4e802014-12-04 10:32:10 +01001448 if (r < 0)
1449 return r;
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001450 break;
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -05001451
1452 case SET_ACL:
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -05001453 r = glob_item(i, path_set_acls, false);
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -05001454 if (r < 0)
1455 return r;
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -05001456 break;
1457
1458 case RECURSIVE_SET_ACL:
1459 r = glob_item(i, path_set_acls, true);
1460 if (r < 0)
1461 return r;
1462 break;
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001463
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001464 case SET_ATTRIBUTE:
1465 r = glob_item(i, path_set_attribute, false);
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001466 if (r < 0)
1467 return r;
1468 break;
1469
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001470 case RECURSIVE_SET_ATTRIBUTE:
1471 r = glob_item(i, path_set_attribute, true);
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001472 if (r < 0)
1473 return r;
1474 break;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001475 }
1476
Michal Schmidtf05bc3f2011-12-15 23:44:23 +01001477 return 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001478}
1479
Michal Schmidta0896122011-12-15 21:32:50 +01001480static int remove_item_instance(Item *i, const char *instance) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001481 int r;
1482
1483 assert(i);
1484
1485 switch (i->type) {
1486
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001487 case REMOVE_PATH:
Michal Schmidt4a62c712014-11-28 19:57:32 +01001488 if (remove(instance) < 0 && errno != ENOENT)
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001489 return log_error_errno(errno, "rm(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001490
1491 break;
1492
1493 case TRUNCATE_DIRECTORY:
1494 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringd139b242012-06-20 14:31:00 +02001495 /* FIXME: we probably should use dir_cleanup() here
1496 * instead of rm_rf() so that 'x' is honoured. */
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001497 log_debug("rm -rf \"%s\"", instance);
Lennart Poettering1b26f092015-06-15 19:11:15 +02001498 r = rm_rf(instance, (i->type == RECURSIVE_REMOVE_PATH ? REMOVE_ROOT|REMOVE_SUBVOLUME : 0) | REMOVE_PHYSICAL);
Michal Schmidtf6479622014-11-28 18:50:43 +01001499 if (r < 0 && r != -ENOENT)
1500 return log_error_errno(r, "rm_rf(%s): %m", instance);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001501
1502 break;
Zbigniew Jędrzejewski-Szmek7fcb4b92015-01-22 23:35:34 -05001503
1504 default:
1505 assert_not_reached("wut?");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001506 }
1507
1508 return 0;
1509}
1510
Michal Schmidta0896122011-12-15 21:32:50 +01001511static int remove_item(Item *i) {
Michal Schmidt99e68c02011-12-15 23:45:26 +01001512 int r = 0;
1513
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001514 assert(i);
1515
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001516 log_debug("Running remove action for entry %c %s", (char) i->type, i->path);
1517
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001518 switch (i->type) {
1519
1520 case CREATE_FILE:
1521 case TRUNCATE_FILE:
1522 case CREATE_DIRECTORY:
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +01001523 case CREATE_SUBVOLUME:
Lennart Poettering5fb13eb2015-10-21 19:46:23 +02001524 case CREATE_SUBVOLUME_INHERIT_QUOTA:
1525 case CREATE_SUBVOLUME_NEW_QUOTA:
Lennart Poetteringee17ee72011-07-12 03:56:56 +02001526 case CREATE_FIFO:
Lennart Poettering468d7262012-01-17 15:04:12 +01001527 case CREATE_SYMLINK:
1528 case CREATE_CHAR_DEVICE:
1529 case CREATE_BLOCK_DEVICE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001530 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +01001531 case IGNORE_DIRECTORY_PATH:
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001532 case ADJUST_MODE:
Michal Schmidt777b87e2011-12-16 18:27:35 +01001533 case RELABEL_PATH:
Michal Schmidta8d88782011-12-15 23:11:07 +01001534 case RECURSIVE_RELABEL_PATH:
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001535 case WRITE_FILE:
Lennart Poettering849958d2014-06-10 23:02:40 +02001536 case COPY_FILES:
Maciej Wereskiebf4e802014-12-04 10:32:10 +01001537 case SET_XATTR:
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -05001538 case RECURSIVE_SET_XATTR:
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -05001539 case SET_ACL:
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -05001540 case RECURSIVE_SET_ACL:
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001541 case SET_ATTRIBUTE:
1542 case RECURSIVE_SET_ATTRIBUTE:
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001543 break;
1544
1545 case REMOVE_PATH:
1546 case TRUNCATE_DIRECTORY:
Michal Schmidt99e68c02011-12-15 23:45:26 +01001547 case RECURSIVE_REMOVE_PATH:
Zbigniew Jędrzejewski-Szmek081043c2015-01-18 01:33:39 -05001548 r = glob_item(i, remove_item_instance, false);
Michal Schmidt99e68c02011-12-15 23:45:26 +01001549 break;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001550 }
1551
Michal Schmidt99e68c02011-12-15 23:45:26 +01001552 return r;
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01001553}
1554
Michal Sekletar78a92a52013-01-18 16:13:08 +01001555static int clean_item_instance(Item *i, const char* instance) {
Harald Hoyer7fd1b192013-04-18 09:11:22 +02001556 _cleanup_closedir_ DIR *d = NULL;
Michal Sekletar78a92a52013-01-18 16:13:08 +01001557 struct stat s, ps;
1558 bool mountpoint;
Michal Sekletar78a92a52013-01-18 16:13:08 +01001559 usec_t cutoff, n;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001560 char timestamp[FORMAT_TIMESTAMP_MAX];
Michal Sekletar78a92a52013-01-18 16:13:08 +01001561
1562 assert(i);
1563
1564 if (!i->age_set)
1565 return 0;
1566
1567 n = now(CLOCK_REALTIME);
1568 if (n < i->age)
1569 return 0;
1570
1571 cutoff = n - i->age;
1572
Zbigniew Jędrzejewski-Szmekdf99a9e2014-01-02 00:02:31 -05001573 d = opendir_nomod(instance);
Michal Sekletar78a92a52013-01-18 16:13:08 +01001574 if (!d) {
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001575 if (errno == ENOENT || errno == ENOTDIR) {
1576 log_debug_errno(errno, "Directory \"%s\": %m", instance);
Michal Sekletar78a92a52013-01-18 16:13:08 +01001577 return 0;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001578 }
Michal Sekletar78a92a52013-01-18 16:13:08 +01001579
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001580 log_error_errno(errno, "Failed to open directory %s: %m", instance);
Michal Sekletar78a92a52013-01-18 16:13:08 +01001581 return -errno;
1582 }
1583
Michal Schmidt4a62c712014-11-28 19:57:32 +01001584 if (fstat(dirfd(d), &s) < 0)
1585 return log_error_errno(errno, "stat(%s) failed: %m", i->path);
Michal Sekletar78a92a52013-01-18 16:13:08 +01001586
1587 if (!S_ISDIR(s.st_mode)) {
1588 log_error("%s is not a directory.", i->path);
Zbigniew Jędrzejewski-Szmek19fbec12013-03-03 18:42:52 -05001589 return -ENOTDIR;
Michal Sekletar78a92a52013-01-18 16:13:08 +01001590 }
1591
Michal Schmidt4a62c712014-11-28 19:57:32 +01001592 if (fstatat(dirfd(d), "..", &ps, AT_SYMLINK_NOFOLLOW) != 0)
1593 return log_error_errno(errno, "stat(%s/..) failed: %m", i->path);
Michal Sekletar78a92a52013-01-18 16:13:08 +01001594
Thomas Hindoe Paaboel Andersen2bb158c2015-10-23 20:15:17 +02001595 mountpoint = s.st_dev != ps.st_dev || s.st_ino == ps.st_ino;
Michal Sekletar78a92a52013-01-18 16:13:08 +01001596
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001597 log_debug("Cleanup threshold for %s \"%s\" is %s",
1598 mountpoint ? "mount point" : "directory",
1599 instance,
1600 format_timestamp_us(timestamp, sizeof(timestamp), cutoff));
1601
1602 return dir_cleanup(i, instance, d, &s, cutoff, s.st_dev, mountpoint,
1603 MAX_DEPTH, i->keep_first_level);
Michal Sekletar78a92a52013-01-18 16:13:08 +01001604}
1605
1606static int clean_item(Item *i) {
1607 int r = 0;
1608
1609 assert(i);
1610
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001611 log_debug("Running clean action for entry %c %s", (char) i->type, i->path);
1612
Michal Sekletar78a92a52013-01-18 16:13:08 +01001613 switch (i->type) {
1614 case CREATE_DIRECTORY:
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +01001615 case CREATE_SUBVOLUME:
Lennart Poettering5fb13eb2015-10-21 19:46:23 +02001616 case CREATE_SUBVOLUME_INHERIT_QUOTA:
1617 case CREATE_SUBVOLUME_NEW_QUOTA:
Michal Sekletar78a92a52013-01-18 16:13:08 +01001618 case TRUNCATE_DIRECTORY:
1619 case IGNORE_PATH:
Lennart Poettering849958d2014-06-10 23:02:40 +02001620 case COPY_FILES:
Michal Sekletar78a92a52013-01-18 16:13:08 +01001621 clean_item_instance(i, i->path);
1622 break;
1623 case IGNORE_DIRECTORY_PATH:
Zbigniew Jędrzejewski-Szmek081043c2015-01-18 01:33:39 -05001624 r = glob_item(i, clean_item_instance, false);
Michal Sekletar78a92a52013-01-18 16:13:08 +01001625 break;
1626 default:
1627 break;
1628 }
1629
1630 return r;
1631}
1632
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001633static int process_item_array(ItemArray *array);
1634
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001635static int process_item(Item *i) {
Lennart Poettering1e958932015-01-06 20:33:46 +01001636 int r, q, p, t = 0;
Zbigniew Jędrzejewski-Szmek9348f0e2014-10-01 07:33:22 -05001637 _cleanup_free_ char *prefix = NULL;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001638
1639 assert(i);
1640
Lennart Poettering1910cd02014-06-11 01:37:35 +02001641 if (i->done)
1642 return 0;
1643
1644 i->done = true;
1645
Zbigniew Jędrzejewski-Szmek9348f0e2014-10-01 07:33:22 -05001646 prefix = malloc(strlen(i->path) + 1);
1647 if (!prefix)
1648 return log_oom();
1649
Lennart Poettering1910cd02014-06-11 01:37:35 +02001650 PATH_FOREACH_PREFIX(prefix, i->path) {
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001651 ItemArray *j;
Lennart Poettering1910cd02014-06-11 01:37:35 +02001652
Lennart Poetteringef43a392015-04-22 18:18:56 +02001653 j = ordered_hashmap_get(items, prefix);
Zbigniew Jędrzejewski-Szmek1db50422015-01-06 09:53:12 -05001654 if (j) {
1655 int s;
1656
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001657 s = process_item_array(j);
Zbigniew Jędrzejewski-Szmek1db50422015-01-06 09:53:12 -05001658 if (s < 0 && t == 0)
1659 t = s;
1660 }
Lennart Poettering1910cd02014-06-11 01:37:35 +02001661 }
1662
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001663 r = arg_create ? create_item(i) : 0;
Michal Schmidta0896122011-12-15 21:32:50 +01001664 q = arg_remove ? remove_item(i) : 0;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001665 p = arg_clean ? clean_item(i) : 0;
1666
Zbigniew Jędrzejewski-Szmek1db50422015-01-06 09:53:12 -05001667 return t < 0 ? t :
1668 r < 0 ? r :
1669 q < 0 ? q :
1670 p;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001671}
1672
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001673static int process_item_array(ItemArray *array) {
1674 unsigned n;
1675 int r = 0, k;
Lennart Poettering753615e2014-06-12 23:07:17 +02001676
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001677 assert(array);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001678
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001679 for (n = 0; n < array->count; n++) {
1680 k = process_item(array->items + n);
1681 if (k < 0 && r == 0)
1682 r = k;
1683 }
1684
1685 return r;
1686}
1687
1688static void item_free_contents(Item *i) {
1689 assert(i);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001690 free(i->path);
Lennart Poettering468d7262012-01-17 15:04:12 +01001691 free(i->argument);
Maciej Wereskiebf4e802014-12-04 10:32:10 +01001692 strv_free(i->xattrs);
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -05001693
1694#ifdef HAVE_ACL
1695 acl_free(i->acl_access);
1696 acl_free(i->acl_default);
1697#endif
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001698}
1699
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001700static void item_array_free(ItemArray *a) {
1701 unsigned n;
Maciej Wereskie2f2fb72013-07-19 15:43:12 +02001702
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001703 if (!a)
1704 return;
1705
1706 for (n = 0; n < a->count; n++)
1707 item_free_contents(a->items + n);
1708 free(a->items);
1709 free(a);
1710}
1711
Lennart Poettering17493fa2015-04-10 16:22:22 +02001712static int item_compare(const void *a, const void *b) {
1713 const Item *x = a, *y = b;
1714
1715 /* Make sure that the ownership taking item is put first, so
1716 * that we first create the node, and then can adjust it */
1717
1718 if (takes_ownership(x->type) && !takes_ownership(y->type))
1719 return -1;
1720 if (!takes_ownership(x->type) && takes_ownership(y->type))
1721 return 1;
1722
1723 return (int) x->type - (int) y->type;
1724}
1725
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001726static bool item_compatible(Item *a, Item *b) {
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001727 assert(a);
1728 assert(b);
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001729 assert(streq(a->path, b->path));
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001730
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001731 if (takes_ownership(a->type) && takes_ownership(b->type))
1732 /* check if the items are the same */
1733 return streq_ptr(a->argument, b->argument) &&
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001734
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001735 a->uid_set == b->uid_set &&
1736 a->uid == b->uid &&
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001737
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001738 a->gid_set == b->gid_set &&
1739 a->gid == b->gid &&
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001740
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001741 a->mode_set == b->mode_set &&
1742 a->mode == b->mode &&
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001743
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001744 a->age_set == b->age_set &&
1745 a->age == b->age &&
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001746
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001747 a->mask_perms == b->mask_perms &&
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001748
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001749 a->keep_first_level == b->keep_first_level &&
Lennart Poettering468d7262012-01-17 15:04:12 +01001750
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001751 a->major_minor == b->major_minor;
Lennart Poettering468d7262012-01-17 15:04:12 +01001752
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02001753 return true;
1754}
1755
Dave Reisnera2aced42013-07-24 11:10:05 -04001756static bool should_include_path(const char *path) {
1757 char **prefix;
1758
Lennart Poetteringabef3f92014-06-11 10:14:07 +02001759 STRV_FOREACH(prefix, arg_exclude_prefixes)
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001760 if (path_startswith(path, *prefix)) {
1761 log_debug("Entry \"%s\" matches exclude prefix \"%s\", skipping.",
1762 path, *prefix);
Dave Reisner5c795112013-07-24 11:19:24 -04001763 return false;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001764 }
Dave Reisnera2aced42013-07-24 11:10:05 -04001765
Lennart Poetteringabef3f92014-06-11 10:14:07 +02001766 STRV_FOREACH(prefix, arg_include_prefixes)
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001767 if (path_startswith(path, *prefix)) {
1768 log_debug("Entry \"%s\" matches include prefix \"%s\".", path, *prefix);
Dave Reisnera2aced42013-07-24 11:10:05 -04001769 return true;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001770 }
Dave Reisnera2aced42013-07-24 11:10:05 -04001771
Dave Reisner5c795112013-07-24 11:19:24 -04001772 /* no matches, so we should include this path only if we
1773 * have no whitelist at all */
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001774 if (strv_length(arg_include_prefixes) == 0)
1775 return true;
1776
1777 log_debug("Entry \"%s\" does not match any include prefix, skipping.", path);
1778 return false;
Dave Reisnera2aced42013-07-24 11:10:05 -04001779}
1780
Lennart Poetteringfba6e682011-02-13 14:00:54 +01001781static int parse_line(const char *fname, unsigned line, const char *buffer) {
Lennart Poettering1731e342013-09-17 11:02:02 -05001782
Lennart Poetteringcde684a2014-06-10 22:50:46 +02001783 _cleanup_free_ char *action = NULL, *mode = NULL, *user = NULL, *group = NULL, *age = NULL, *path = NULL;
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001784 _cleanup_(item_free_contents) Item i = {};
1785 ItemArray *existing;
Lennart Poetteringef43a392015-04-22 18:18:56 +02001786 OrderedHashmap *h;
daurnimator657cf7f2015-03-09 15:11:44 -04001787 int r, pos;
Zbigniew Jędrzejewski-Szmek5f255142015-01-09 01:11:01 -05001788 bool force = false, boot = false;
Lennart Poettering5008d582010-09-28 02:34:02 +02001789
1790 assert(fname);
1791 assert(line >= 1);
1792 assert(buffer);
1793
Richard Maw68685602015-06-23 16:20:53 +00001794 r = extract_many_words(
Lennart Poettering4034a062015-03-23 18:55:36 +07001795 &buffer,
Richard Maw68685602015-06-23 16:20:53 +00001796 NULL,
Richard Maw12ba2c42015-06-23 16:26:49 +00001797 EXTRACT_QUOTES,
Lennart Poettering4034a062015-03-23 18:55:36 +07001798 &action,
1799 &path,
1800 &mode,
1801 &user,
1802 &group,
1803 &age,
1804 NULL);
daurnimator657cf7f2015-03-09 15:11:44 -04001805 if (r < 0)
1806 return log_error_errno(r, "[%s:%u] Failed to parse line: %m", fname, line);
1807 else if (r < 2) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001808 log_error("[%s:%u] Syntax error.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001809 return -EIO;
Lennart Poettering5008d582010-09-28 02:34:02 +02001810 }
1811
Lennart Poetteringaa5f6812015-04-21 01:10:19 +02001812 if (!isempty(buffer) && !streq(buffer, "-")) {
Lennart Poettering4034a062015-03-23 18:55:36 +07001813 i.argument = strdup(buffer);
1814 if (!i.argument)
1815 return log_oom();
1816 }
1817
Lennart Poettering2e78fa72014-06-16 13:21:07 +02001818 if (isempty(action)) {
1819 log_error("[%s:%u] Command too short '%s'.", fname, line, action);
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001820 return -EINVAL;
Lennart Poettering2e78fa72014-06-16 13:21:07 +02001821 }
1822
Zbigniew Jędrzejewski-Szmek5f255142015-01-09 01:11:01 -05001823 for (pos = 1; action[pos]; pos++) {
1824 if (action[pos] == '!' && !boot)
1825 boot = true;
1826 else if (action[pos] == '+' && !force)
1827 force = true;
1828 else {
1829 log_error("[%s:%u] Unknown modifiers in command '%s'",
1830 fname, line, action);
1831 return -EINVAL;
1832 }
Lennart Poettering2e78fa72014-06-16 13:21:07 +02001833 }
1834
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001835 if (boot && !arg_boot) {
1836 log_debug("Ignoring entry %s \"%s\" because --boot is not specified.",
1837 action, path);
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001838 return 0;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001839 }
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05001840
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001841 i.type = action[0];
1842 i.force = force;
Lennart Poettering1731e342013-09-17 11:02:02 -05001843
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001844 r = specifier_printf(path, specifier_table, NULL, &i.path);
Lennart Poettering1731e342013-09-17 11:02:02 -05001845 if (r < 0) {
1846 log_error("[%s:%u] Failed to replace specifiers: %s", fname, line, path);
1847 return r;
1848 }
1849
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001850 switch (i.type) {
Lennart Poettering468d7262012-01-17 15:04:12 +01001851
Michal Schmidt777b87e2011-12-16 18:27:35 +01001852 case CREATE_DIRECTORY:
Lennart Poetteringd7b8eec2014-12-27 18:46:36 +01001853 case CREATE_SUBVOLUME:
Lennart Poettering5fb13eb2015-10-21 19:46:23 +02001854 case CREATE_SUBVOLUME_INHERIT_QUOTA:
1855 case CREATE_SUBVOLUME_NEW_QUOTA:
Michal Schmidt777b87e2011-12-16 18:27:35 +01001856 case TRUNCATE_DIRECTORY:
1857 case CREATE_FIFO:
1858 case IGNORE_PATH:
Michal Sekletar78a92a52013-01-18 16:13:08 +01001859 case IGNORE_DIRECTORY_PATH:
Michal Schmidt777b87e2011-12-16 18:27:35 +01001860 case REMOVE_PATH:
1861 case RECURSIVE_REMOVE_PATH:
Lennart Poetteringe73a03e2014-06-10 23:42:16 +02001862 case ADJUST_MODE:
Michal Schmidt777b87e2011-12-16 18:27:35 +01001863 case RELABEL_PATH:
1864 case RECURSIVE_RELABEL_PATH:
Lennart Poetteringc82500c2015-04-10 14:46:05 +02001865 if (i.argument)
Lennart Poetteringbd550f72015-04-10 16:03:24 +02001866 log_warning("[%s:%u] %c lines don't take argument fields, ignoring.", fname, line, i.type);
Lennart Poetteringc82500c2015-04-10 14:46:05 +02001867
1868 break;
1869
1870 case CREATE_FILE:
1871 case TRUNCATE_FILE:
Michal Schmidt777b87e2011-12-16 18:27:35 +01001872 break;
Lennart Poettering468d7262012-01-17 15:04:12 +01001873
1874 case CREATE_SYMLINK:
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001875 if (!i.argument) {
1876 i.argument = strappend("/usr/share/factory/", i.path);
1877 if (!i.argument)
Kay Sievers2f3b8732014-06-20 15:57:43 +02001878 return log_oom();
Lennart Poettering468d7262012-01-17 15:04:12 +01001879 }
1880 break;
1881
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001882 case WRITE_FILE:
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001883 if (!i.argument) {
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001884 log_error("[%s:%u] Write file requires argument.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001885 return -EBADMSG;
Lennart Poettering31ed59c2012-01-18 16:39:04 +01001886 }
1887 break;
1888
Lennart Poettering849958d2014-06-10 23:02:40 +02001889 case COPY_FILES:
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001890 if (!i.argument) {
1891 i.argument = strappend("/usr/share/factory/", i.path);
1892 if (!i.argument)
Kay Sievers2f3b8732014-06-20 15:57:43 +02001893 return log_oom();
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001894 } else if (!path_is_absolute(i.argument)) {
Lennart Poettering849958d2014-06-10 23:02:40 +02001895 log_error("[%s:%u] Source path is not absolute.", fname, line);
1896 return -EBADMSG;
1897 }
1898
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001899 path_kill_slashes(i.argument);
Lennart Poettering849958d2014-06-10 23:02:40 +02001900 break;
1901
Lennart Poettering468d7262012-01-17 15:04:12 +01001902 case CREATE_CHAR_DEVICE:
1903 case CREATE_BLOCK_DEVICE: {
1904 unsigned major, minor;
1905
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001906 if (!i.argument) {
Lennart Poettering468d7262012-01-17 15:04:12 +01001907 log_error("[%s:%u] Device file requires argument.", fname, line);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001908 return -EBADMSG;
Lennart Poettering468d7262012-01-17 15:04:12 +01001909 }
1910
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001911 if (sscanf(i.argument, "%u:%u", &major, &minor) != 2) {
1912 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 -04001913 return -EBADMSG;
Lennart Poettering468d7262012-01-17 15:04:12 +01001914 }
1915
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001916 i.major_minor = makedev(major, minor);
Lennart Poettering468d7262012-01-17 15:04:12 +01001917 break;
1918 }
1919
Maciej Wereskiebf4e802014-12-04 10:32:10 +01001920 case SET_XATTR:
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -05001921 case RECURSIVE_SET_XATTR:
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001922 if (!i.argument) {
Maciej Wereskiebf4e802014-12-04 10:32:10 +01001923 log_error("[%s:%u] Set extended attribute requires argument.", fname, line);
1924 return -EBADMSG;
1925 }
Lennart Poetteringbd550f72015-04-10 16:03:24 +02001926 r = parse_xattrs_from_arg(&i);
Maciej Wereskiebf4e802014-12-04 10:32:10 +01001927 if (r < 0)
1928 return r;
1929 break;
1930
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -05001931 case SET_ACL:
Zbigniew Jędrzejewski-Szmekb705ab62015-01-18 02:10:00 -05001932 case RECURSIVE_SET_ACL:
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -05001933 if (!i.argument) {
1934 log_error("[%s:%u] Set ACLs requires argument.", fname, line);
1935 return -EBADMSG;
1936 }
Lennart Poetteringbd550f72015-04-10 16:03:24 +02001937 r = parse_acls_from_arg(&i);
Zbigniew Jędrzejewski-Szmekf8eeeaf2015-01-17 23:27:39 -05001938 if (r < 0)
1939 return r;
1940 break;
1941
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001942 case SET_ATTRIBUTE:
1943 case RECURSIVE_SET_ATTRIBUTE:
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001944 if (!i.argument) {
Lennart Poettering88ec4df2015-04-08 22:35:52 +02001945 log_error("[%s:%u] Set file attribute requires argument.", fname, line);
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001946 return -EBADMSG;
1947 }
Lennart Poetteringbd550f72015-04-10 16:03:24 +02001948 r = parse_attribute_from_arg(&i);
Goffredo Baroncelli22c3a6c2015-03-16 20:33:50 +01001949 if (r < 0)
1950 return r;
1951 break;
1952
Michal Schmidt777b87e2011-12-16 18:27:35 +01001953 default:
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05001954 log_error("[%s:%u] Unknown command type '%c'.", fname, line, (char) i.type);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001955 return -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02001956 }
Lennart Poettering468d7262012-01-17 15:04:12 +01001957
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001958 if (!path_is_absolute(i.path)) {
1959 log_error("[%s:%u] Path '%s' not absolute.", fname, line, i.path);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001960 return -EBADMSG;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001961 }
1962
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001963 path_kill_slashes(i.path);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02001964
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001965 if (!should_include_path(i.path))
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001966 return 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02001967
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001968 if (arg_root) {
Lennart Poetteringcde684a2014-06-10 22:50:46 +02001969 char *p;
1970
Lennart Poettering1d13f642015-05-13 17:42:10 +02001971 p = prefix_root(arg_root, i.path);
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001972 if (!p)
1973 return log_oom();
1974
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001975 free(i.path);
1976 i.path = p;
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07001977 }
1978
Lennart Poettering90937fe2015-04-10 16:04:16 +02001979 if (!isempty(user) && !streq(user, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001980 const char *u = user;
Lennart Poettering5008d582010-09-28 02:34:02 +02001981
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001982 r = get_user_creds(&u, &i.uid, NULL, NULL, NULL);
Lennart Poettering4b678342011-07-23 01:17:59 +02001983 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001984 log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001985 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001986 }
1987
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001988 i.uid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02001989 }
1990
Lennart Poettering90937fe2015-04-10 16:04:16 +02001991 if (!isempty(group) && !streq(group, "-")) {
Lennart Poettering4b678342011-07-23 01:17:59 +02001992 const char *g = group;
Lennart Poettering5008d582010-09-28 02:34:02 +02001993
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05001994 r = get_group_creds(&g, &i.gid);
Lennart Poettering4b678342011-07-23 01:17:59 +02001995 if (r < 0) {
Lennart Poettering5008d582010-09-28 02:34:02 +02001996 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04001997 return r;
Lennart Poettering5008d582010-09-28 02:34:02 +02001998 }
1999
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002000 i.gid_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02002001 }
2002
Lennart Poettering90937fe2015-04-10 16:04:16 +02002003 if (!isempty(mode) && !streq(mode, "-")) {
Lennart Poetteringabef3f92014-06-11 10:14:07 +02002004 const char *mm = mode;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002005 unsigned m;
Lennart Poettering5008d582010-09-28 02:34:02 +02002006
Lennart Poetteringabef3f92014-06-11 10:14:07 +02002007 if (*mm == '~') {
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002008 i.mask_perms = true;
Lennart Poetteringabef3f92014-06-11 10:14:07 +02002009 mm++;
2010 }
2011
Lennart Poettering2ff7b0a2015-04-10 14:43:06 +02002012 if (parse_mode(mm, &m) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002013 log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
Lennart Poettering2ff7b0a2015-04-10 14:43:06 +02002014 return -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02002015 }
2016
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002017 i.mode = m;
2018 i.mode_set = true;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002019 } else
Lennart Poettering5fb13eb2015-10-21 19:46:23 +02002020 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 +02002021
Lennart Poettering90937fe2015-04-10 16:04:16 +02002022 if (!isempty(age) && !streq(age, "-")) {
Lennart Poettering24f3a372012-06-20 09:05:50 +02002023 const char *a = age;
2024
2025 if (*a == '~') {
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002026 i.keep_first_level = true;
Lennart Poettering24f3a372012-06-20 09:05:50 +02002027 a++;
2028 }
2029
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002030 if (parse_sec(a, &i.age) < 0) {
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002031 log_error("[%s:%u] Invalid age '%s'.", fname, line, age);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04002032 return -EBADMSG;
Lennart Poettering5008d582010-09-28 02:34:02 +02002033 }
2034
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002035 i.age_set = true;
Lennart Poettering5008d582010-09-28 02:34:02 +02002036 }
2037
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002038 h = needs_glob(i.type) ? globs : items;
Lennart Poettering022707d2011-01-05 16:11:15 +01002039
Lennart Poetteringef43a392015-04-22 18:18:56 +02002040 existing = ordered_hashmap_get(h, i.path);
Lennart Poettering468d7262012-01-17 15:04:12 +01002041 if (existing) {
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002042 unsigned n;
2043
2044 for (n = 0; n < existing->count; n++) {
Martin Pitt6487ada2015-03-05 14:58:56 +01002045 if (!item_compatible(existing->items + n, &i)) {
Zbigniew Jędrzejewski-Szmek505ef0e2015-01-09 01:10:02 -05002046 log_warning("[%s:%u] Duplicate line for path \"%s\", ignoring.",
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002047 fname, line, i.path);
Martin Pitt6487ada2015-03-05 14:58:56 +01002048 return 0;
2049 }
Maciej Wereskiebf4e802014-12-04 10:32:10 +01002050 }
2051 } else {
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002052 existing = new0(ItemArray, 1);
Lennart Poetteringef43a392015-04-22 18:18:56 +02002053 r = ordered_hashmap_put(h, i.path, existing);
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002054 if (r < 0)
2055 return log_oom();
Lennart Poetteringbfe95f32011-04-08 04:49:43 +02002056 }
2057
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002058 if (!GREEDY_REALLOC(existing->items, existing->size, existing->count + 1))
2059 return log_oom();
Lennart Poettering5008d582010-09-28 02:34:02 +02002060
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002061 memcpy(existing->items + existing->count++, &i, sizeof(i));
Zbigniew Jędrzejewski-Szmekdd449ac2015-04-10 18:57:05 -04002062
2063 /* Sort item array, to enforce stable ordering of application */
2064 qsort_safe(existing->items, existing->count, sizeof(Item), item_compare);
Lennart Poettering17493fa2015-04-10 16:22:22 +02002065
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002066 zero(i);
Zbigniew Jędrzejewski-Szmek7f2c1f42013-03-31 16:29:46 -04002067 return 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02002068}
2069
Zbigniew Jędrzejewski-Szmek601185b2014-08-02 11:12:21 -04002070static void help(void) {
Lennart Poettering522d4a42011-02-13 15:08:15 +01002071 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
2072 "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
Dave Reisner5c795112013-07-24 11:19:24 -04002073 " -h --help Show this help\n"
Lennart Poetteringeb9da372013-11-06 18:28:39 +01002074 " --version Show package version\n"
Dave Reisner5c795112013-07-24 11:19:24 -04002075 " --create Create marked files/directories\n"
2076 " --clean Clean up marked directories\n"
2077 " --remove Remove marked files/directories\n"
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05002078 " --boot Execute actions only safe at boot\n"
Zbigniew Jędrzejewski-Szmek79ca8882015-01-27 21:22:08 -05002079 " --prefix=PATH Only apply rules with the specified prefix\n"
2080 " --exclude-prefix=PATH Ignore rules with the specified prefix\n"
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07002081 " --root=PATH Operate on an alternate filesystem root\n",
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002082 program_invocation_short_name);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002083}
2084
2085static int parse_argv(int argc, char *argv[]) {
2086
2087 enum {
Lennart Poetteringeb9da372013-11-06 18:28:39 +01002088 ARG_VERSION = 0x100,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002089 ARG_CREATE,
2090 ARG_CLEAN,
Lennart Poetteringfba6e682011-02-13 14:00:54 +01002091 ARG_REMOVE,
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05002092 ARG_BOOT,
Dave Reisner5c795112013-07-24 11:19:24 -04002093 ARG_PREFIX,
2094 ARG_EXCLUDE_PREFIX,
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07002095 ARG_ROOT,
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002096 };
2097
2098 static const struct option options[] = {
Dave Reisner5c795112013-07-24 11:19:24 -04002099 { "help", no_argument, NULL, 'h' },
Lennart Poetteringeb9da372013-11-06 18:28:39 +01002100 { "version", no_argument, NULL, ARG_VERSION },
Dave Reisner5c795112013-07-24 11:19:24 -04002101 { "create", no_argument, NULL, ARG_CREATE },
2102 { "clean", no_argument, NULL, ARG_CLEAN },
2103 { "remove", no_argument, NULL, ARG_REMOVE },
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05002104 { "boot", no_argument, NULL, ARG_BOOT },
Dave Reisner5c795112013-07-24 11:19:24 -04002105 { "prefix", required_argument, NULL, ARG_PREFIX },
2106 { "exclude-prefix", required_argument, NULL, ARG_EXCLUDE_PREFIX },
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07002107 { "root", required_argument, NULL, ARG_ROOT },
Lennart Poetteringeb9da372013-11-06 18:28:39 +01002108 {}
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002109 };
2110
Lennart Poettering0f474362015-10-22 19:28:31 +02002111 int c, r;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002112
2113 assert(argc >= 0);
2114 assert(argv);
2115
Zbigniew Jędrzejewski-Szmek601185b2014-08-02 11:12:21 -04002116 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002117
2118 switch (c) {
2119
2120 case 'h':
Zbigniew Jędrzejewski-Szmek601185b2014-08-02 11:12:21 -04002121 help();
2122 return 0;
Lennart Poetteringeb9da372013-11-06 18:28:39 +01002123
2124 case ARG_VERSION:
Lennart Poettering3f6fd1b2015-09-23 03:01:06 +02002125 return version();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002126
2127 case ARG_CREATE:
2128 arg_create = true;
2129 break;
2130
2131 case ARG_CLEAN:
2132 arg_clean = true;
2133 break;
2134
2135 case ARG_REMOVE:
2136 arg_remove = true;
2137 break;
2138
Zbigniew Jędrzejewski-Szmek81815652013-12-30 13:00:38 -05002139 case ARG_BOOT:
2140 arg_boot = true;
Zbigniew Jędrzejewski-Szmekc4708f12013-12-20 20:25:39 -05002141 break;
2142
Lennart Poetteringfba6e682011-02-13 14:00:54 +01002143 case ARG_PREFIX:
Lennart Poettering7bc040f2014-06-11 01:26:28 +02002144 if (strv_push(&arg_include_prefixes, optarg) < 0)
Dave Reisnera2aced42013-07-24 11:10:05 -04002145 return log_oom();
Lennart Poetteringfba6e682011-02-13 14:00:54 +01002146 break;
2147
Dave Reisner5c795112013-07-24 11:19:24 -04002148 case ARG_EXCLUDE_PREFIX:
Lennart Poettering7bc040f2014-06-11 01:26:28 +02002149 if (strv_push(&arg_exclude_prefixes, optarg) < 0)
Dave Reisner5c795112013-07-24 11:19:24 -04002150 return log_oom();
2151 break;
2152
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07002153 case ARG_ROOT:
Lennart Poettering0f03c2a2015-10-22 19:54:29 +02002154 r = parse_path_argument_and_warn(optarg, true, &arg_root);
Lennart Poettering0f474362015-10-22 19:28:31 +02002155 if (r < 0)
Lennart Poettering0f03c2a2015-10-22 19:54:29 +02002156 return r;
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07002157 break;
2158
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002159 case '?':
2160 return -EINVAL;
2161
2162 default:
Lennart Poetteringeb9da372013-11-06 18:28:39 +01002163 assert_not_reached("Unhandled option");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002164 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002165
2166 if (!arg_clean && !arg_create && !arg_remove) {
Harald Hoyer35b8ca32011-02-21 15:32:17 +01002167 log_error("You need to specify at least one of --clean, --create or --remove.");
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002168 return -EINVAL;
2169 }
2170
2171 return 1;
2172}
2173
Lennart Poetteringfba6e682011-02-13 14:00:54 +01002174static int read_config_file(const char *fn, bool ignore_enoent) {
Lennart Poettering1731e342013-09-17 11:02:02 -05002175 _cleanup_fclose_ FILE *f = NULL;
2176 char line[LINE_MAX];
Michal Sekletar78a92a52013-01-18 16:13:08 +01002177 Iterator iterator;
Lennart Poettering1731e342013-09-17 11:02:02 -05002178 unsigned v = 0;
Michal Sekletar78a92a52013-01-18 16:13:08 +01002179 Item *i;
Lennart Poettering1731e342013-09-17 11:02:02 -05002180 int r;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01002181
2182 assert(fn);
2183
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07002184 r = search_and_fopen_nulstr(fn, "re", arg_root, conf_file_dirs, &f);
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01002185 if (r < 0) {
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002186 if (ignore_enoent && r == -ENOENT) {
2187 log_debug_errno(r, "Failed to open \"%s\": %m", fn);
Lennart Poetteringfba6e682011-02-13 14:00:54 +01002188 return 0;
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002189 }
Lennart Poetteringfba6e682011-02-13 14:00:54 +01002190
Michal Schmidt8d3d7072014-11-28 19:13:53 +01002191 return log_error_errno(r, "Failed to open '%s', ignoring: %m", fn);
Lennart Poetteringfba6e682011-02-13 14:00:54 +01002192 }
Zbigniew Jędrzejewski-Szmek582deb82015-01-24 01:54:05 -05002193 log_debug("Reading config file \"%s\".", fn);
Lennart Poetteringfba6e682011-02-13 14:00:54 +01002194
Lennart Poettering1731e342013-09-17 11:02:02 -05002195 FOREACH_LINE(line, f, break) {
2196 char *l;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01002197 int k;
2198
Lennart Poetteringfba6e682011-02-13 14:00:54 +01002199 v++;
2200
2201 l = strstrip(line);
2202 if (*l == '#' || *l == 0)
2203 continue;
2204
Lennart Poettering1731e342013-09-17 11:02:02 -05002205 k = parse_line(fn, v, l);
2206 if (k < 0 && r == 0)
2207 r = k;
Lennart Poetteringfba6e682011-02-13 14:00:54 +01002208 }
2209
Michal Sekletar78a92a52013-01-18 16:13:08 +01002210 /* we have to determine age parameter for each entry of type X */
Lennart Poetteringef43a392015-04-22 18:18:56 +02002211 ORDERED_HASHMAP_FOREACH(i, globs, iterator) {
Michal Sekletar78a92a52013-01-18 16:13:08 +01002212 Iterator iter;
2213 Item *j, *candidate_item = NULL;
2214
2215 if (i->type != IGNORE_DIRECTORY_PATH)
2216 continue;
2217
Lennart Poetteringef43a392015-04-22 18:18:56 +02002218 ORDERED_HASHMAP_FOREACH(j, items, iter) {
Lennart Poettering5fb13eb2015-10-21 19:46:23 +02002219 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 +01002220 continue;
2221
2222 if (path_equal(j->path, i->path)) {
2223 candidate_item = j;
2224 break;
2225 }
2226
2227 if ((!candidate_item && path_startswith(i->path, j->path)) ||
2228 (candidate_item && path_startswith(j->path, candidate_item->path) && (fnmatch(i->path, j->path, FNM_PATHNAME | FNM_PERIOD) == 0)))
2229 candidate_item = j;
2230 }
2231
Richard Weinberger9ed2a352014-09-09 11:09:37 +02002232 if (candidate_item && candidate_item->age_set) {
Michal Sekletar78a92a52013-01-18 16:13:08 +01002233 i->age = candidate_item->age;
2234 i->age_set = true;
2235 }
2236 }
2237
Lennart Poetteringfba6e682011-02-13 14:00:54 +01002238 if (ferror(f)) {
Michal Schmidt56f64d92014-11-28 19:29:59 +01002239 log_error_errno(errno, "Failed to read from file %s: %m", fn);
Lennart Poetteringfba6e682011-02-13 14:00:54 +01002240 if (r == 0)
2241 r = -EIO;
2242 }
2243
Lennart Poetteringfba6e682011-02-13 14:00:54 +01002244 return r;
2245}
2246
Lennart Poettering5008d582010-09-28 02:34:02 +02002247int main(int argc, char *argv[]) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01002248 int r, k;
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002249 ItemArray *a;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002250 Iterator iterator;
Lennart Poettering5008d582010-09-28 02:34:02 +02002251
Lennart Poetteringfdcad0c2012-01-11 22:07:35 +01002252 r = parse_argv(argc, argv);
2253 if (r <= 0)
Lennart Poettering753615e2014-06-12 23:07:17 +02002254 goto finish;
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002255
Lennart Poetteringeb0ca9e2011-02-12 09:31:38 +01002256 log_set_target(LOG_TARGET_AUTO);
Lennart Poettering5008d582010-09-28 02:34:02 +02002257 log_parse_environment();
2258 log_open();
2259
Lennart Poettering4c126262011-08-01 20:52:18 +02002260 umask(0022);
2261
WaLyong Chocc56faf2014-10-23 17:23:46 +09002262 mac_selinux_init(NULL);
Lennart Poettering5008d582010-09-28 02:34:02 +02002263
Lennart Poetteringef43a392015-04-22 18:18:56 +02002264 items = ordered_hashmap_new(&string_hash_ops);
2265 globs = ordered_hashmap_new(&string_hash_ops);
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01002266
2267 if (!items || !globs) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01002268 r = log_oom();
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002269 goto finish;
2270 }
2271
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01002272 r = 0;
Lennart Poettering5008d582010-09-28 02:34:02 +02002273
Lennart Poetteringfba6e682011-02-13 14:00:54 +01002274 if (optind < argc) {
2275 int j;
Lennart Poettering5008d582010-09-28 02:34:02 +02002276
Dave Reisner91256702012-06-08 22:31:19 -04002277 for (j = optind; j < argc; j++) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01002278 k = read_config_file(argv[j], false);
2279 if (k < 0 && r == 0)
2280 r = k;
Dave Reisner91256702012-06-08 22:31:19 -04002281 }
Lennart Poettering5008d582010-09-28 02:34:02 +02002282
Lennart Poetteringfba6e682011-02-13 14:00:54 +01002283 } else {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01002284 _cleanup_strv_free_ char **files = NULL;
2285 char **f;
Lennart Poettering5008d582010-09-28 02:34:02 +02002286
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07002287 r = conf_files_list_nulstr(&files, ".conf", arg_root, conf_file_dirs);
Kay Sievers44143302011-04-28 23:51:24 +02002288 if (r < 0) {
Michal Schmidtda927ba2014-11-28 13:19:16 +01002289 log_error_errno(r, "Failed to enumerate tmpfiles.d files: %m");
Kay Sievers44143302011-04-28 23:51:24 +02002290 goto finish;
2291 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002292
Kay Sievers772f8372011-04-25 21:38:21 +02002293 STRV_FOREACH(f, files) {
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01002294 k = read_config_file(*f, true);
2295 if (k < 0 && r == 0)
2296 r = k;
Lennart Poettering5008d582010-09-28 02:34:02 +02002297 }
Lennart Poettering5008d582010-09-28 02:34:02 +02002298 }
2299
Lennart Poettering17493fa2015-04-10 16:22:22 +02002300 /* The non-globbing ones usually create things, hence we apply
2301 * them first */
Lennart Poetteringef43a392015-04-22 18:18:56 +02002302 ORDERED_HASHMAP_FOREACH(a, items, iterator) {
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002303 k = process_item_array(a);
Zbigniew Jędrzejewski-Szmek1db50422015-01-06 09:53:12 -05002304 if (k < 0 && r == 0)
2305 r = k;
2306 }
Lennart Poetteringb8bb3e82011-02-12 09:31:25 +01002307
Lennart Poettering17493fa2015-04-10 16:22:22 +02002308 /* The globbing ones usually alter things, hence we apply them
2309 * second. */
Lennart Poetteringef43a392015-04-22 18:18:56 +02002310 ORDERED_HASHMAP_FOREACH(a, globs, iterator) {
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002311 k = process_item_array(a);
Zbigniew Jędrzejewski-Szmek1db50422015-01-06 09:53:12 -05002312 if (k < 0 && r == 0)
2313 r = k;
2314 }
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002315
Lennart Poettering5008d582010-09-28 02:34:02 +02002316finish:
Lennart Poetteringef43a392015-04-22 18:18:56 +02002317 while ((a = ordered_hashmap_steal_first(items)))
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002318 item_array_free(a);
Lennart Poettering3b63d2d2010-10-18 22:38:41 +02002319
Lennart Poetteringef43a392015-04-22 18:18:56 +02002320 while ((a = ordered_hashmap_steal_first(globs)))
Zbigniew Jędrzejewski-Szmek3f93da92015-01-09 02:00:37 -05002321 item_array_free(a);
Lennart Poettering17b90522011-02-14 21:55:06 +01002322
Lennart Poetteringef43a392015-04-22 18:18:56 +02002323 ordered_hashmap_free(items);
2324 ordered_hashmap_free(globs);
Lennart Poettering5008d582010-09-28 02:34:02 +02002325
Lennart Poettering7bc040f2014-06-11 01:26:28 +02002326 free(arg_include_prefixes);
2327 free(arg_exclude_prefixes);
Michael Marineaucf9a4ab2014-03-13 21:32:13 -07002328 free(arg_root);
Dave Reisnera2aced42013-07-24 11:10:05 -04002329
Lennart Poettering17b90522011-02-14 21:55:06 +01002330 set_free_free(unix_sockets);
2331
WaLyong Chocc56faf2014-10-23 17:23:46 +09002332 mac_selinux_finish();
Lennart Poettering29003cf2010-10-19 19:36:45 +02002333
Lennart Poetteringfabe5c02013-02-11 23:48:36 +01002334 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
Lennart Poettering5008d582010-09-28 02:34:02 +02002335}