]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/conf-files.c
Merge pull request #28301 from berrange/cvm-lockdown
[thirdparty/systemd.git] / src / basic / conf-files.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2c21044f 2
2c21044f 3#include <errno.h>
11c3a366 4#include <stdarg.h>
2c21044f 5#include <stdio.h>
07630cea 6#include <stdlib.h>
2c21044f 7
f461a28d 8#include "chase.h"
3ffd4af2 9#include "conf-files.h"
28db6fbf 10#include "constants.h"
a0956174 11#include "dirent-util.h"
3ffd4af2 12#include "fd-util.h"
07630cea
LP
13#include "hashmap.h"
14#include "log.h"
2c21044f 15#include "macro.h"
08af3cc5 16#include "nulstr-util.h"
9eb977db 17#include "path-util.h"
3e36211b 18#include "set.h"
760877e9 19#include "sort-util.h"
b5084605 20#include "stat-util.h"
07630cea
LP
21#include "string-util.h"
22#include "strv.h"
854a42fb 23#include "terminal-util.h"
2c21044f 24
3e36211b 25static int files_add(
a5af5f80
YW
26 DIR *dir,
27 const char *dirpath,
28 Hashmap **files,
9d0d39ee 29 Set **masked,
3e36211b 30 const char *suffix,
a5af5f80 31 unsigned flags) {
3e36211b 32
1d13f648 33 int r;
e02caf30 34
a5af5f80
YW
35 assert(dir);
36 assert(dirpath);
37 assert(files);
9d0d39ee 38 assert(masked);
2c21044f 39
31d5192d 40 FOREACH_DIRENT(de, dir, return -errno) {
9d0d39ee 41 _cleanup_free_ char *n = NULL, *p = NULL;
3e36211b 42 struct stat st;
3e36211b
LP
43
44 /* Does this match the suffix? */
45 if (suffix && !endswith(de->d_name, suffix))
46 continue;
2c21044f 47
3e36211b 48 /* Has this file already been found in an earlier directory? */
a5af5f80 49 if (hashmap_contains(*files, de->d_name)) {
3e36211b 50 log_debug("Skipping overridden file '%s/%s'.", dirpath, de->d_name);
2c21044f 51 continue;
e34aa8ed 52 }
2c21044f 53
3e36211b 54 /* Has this been masked in an earlier directory? */
9d0d39ee 55 if ((flags & CONF_FILES_FILTER_MASKED) && set_contains(*masked, de->d_name)) {
3e36211b
LP
56 log_debug("File '%s/%s' is masked by previous entry.", dirpath, de->d_name);
57 continue;
58 }
59
60 /* Read file metadata if we shall validate the check for file masks, for node types or whether the node is marked executable. */
61 if (flags & (CONF_FILES_FILTER_MASKED|CONF_FILES_REGULAR|CONF_FILES_DIRECTORY|CONF_FILES_EXECUTABLE))
62 if (fstatat(dirfd(dir), de->d_name, &st, 0) < 0) {
63 log_debug_errno(errno, "Failed to stat '%s/%s', ignoring: %m", dirpath, de->d_name);
64 continue;
65 }
66
67 /* Is this a masking entry? */
68 if ((flags & CONF_FILES_FILTER_MASKED))
69 if (null_or_empty(&st)) {
70 /* Mark this one as masked */
9d0d39ee 71 r = set_put_strdup(masked, de->d_name);
3e36211b
LP
72 if (r < 0)
73 return r;
74
75 log_debug("File '%s/%s' is a mask.", dirpath, de->d_name);
76 continue;
77 }
78
79 /* Does this node have the right type? */
80 if (flags & (CONF_FILES_REGULAR|CONF_FILES_DIRECTORY))
81 if (!((flags & CONF_FILES_DIRECTORY) && S_ISDIR(st.st_mode)) &&
82 !((flags & CONF_FILES_REGULAR) && S_ISREG(st.st_mode))) {
387f6955 83 log_debug("Ignoring '%s/%s', as it does not have the right type.", dirpath, de->d_name);
3e36211b
LP
84 continue;
85 }
b5084605 86
3e36211b
LP
87 /* Does this node have the executable bit set? */
88 if (flags & CONF_FILES_EXECUTABLE)
5238e957 89 /* As requested: check if the file is marked executable. Note that we don't check access(X_OK)
b5084605 90 * here, as we care about whether the file is marked executable at all, and not whether it is
3e36211b 91 * executable for us, because if so, such errors are stuff we should log about. */
b5084605 92
3e36211b
LP
93 if ((st.st_mode & 0111) == 0) { /* not executable */
94 log_debug("Ignoring '%s/%s', as it is not marked executable.", dirpath, de->d_name);
b5084605
LP
95 continue;
96 }
97
9d0d39ee
YW
98 n = strdup(de->d_name);
99 if (!n)
100 return -ENOMEM;
b5084605 101
9d0d39ee 102 if ((flags & CONF_FILES_BASENAME))
a5af5f80 103 r = hashmap_ensure_put(files, &string_hash_ops_free, n, n);
9d0d39ee 104 else {
657ee2d8 105 p = path_join(dirpath, de->d_name);
3e36211b
LP
106 if (!p)
107 return -ENOMEM;
2c21044f 108
a5af5f80 109 r = hashmap_ensure_put(files, &string_hash_ops_free_free, n, p);
3e36211b 110 }
9d0d39ee
YW
111 if (r < 0)
112 return r;
3e36211b 113 assert(r > 0);
9d0d39ee
YW
114
115 TAKE_PTR(n);
116 TAKE_PTR(p);
2c21044f
KS
117 }
118
e02caf30 119 return 0;
2c21044f
KS
120}
121
93bab288 122static int base_cmp(char * const *a, char * const *b) {
27342675
YW
123 assert(a);
124 assert(b);
125 return path_compare_filename(*a, *b);
2c21044f
KS
126}
127
1a39bddf
YW
128static int copy_and_sort_files_from_hashmap(Hashmap *fh, char ***ret) {
129 _cleanup_free_ char **sv = NULL;
130 char **files;
131
132 assert(ret);
133
134 sv = hashmap_get_strv(fh);
135 if (!sv)
136 return -ENOMEM;
137
138 /* The entries in the array given by hashmap_get_strv() are still owned by the hashmap. */
139 files = strv_copy(sv);
140 if (!files)
141 return -ENOMEM;
142
143 typesafe_qsort(files, strv_length(files), base_cmp);
144
145 *ret = files;
146 return 0;
147}
148
a5af5f80 149int conf_files_list_strv(
94916255
LP
150 char ***ret,
151 const char *suffix,
152 const char *root,
153 unsigned flags,
a5af5f80 154 const char * const *dirs) {
94916255 155
e1d75803 156 _cleanup_hashmap_free_ Hashmap *fh = NULL;
9d0d39ee 157 _cleanup_set_free_ Set *masked = NULL;
578ac060 158 int r;
2c21044f 159
94916255 160 assert(ret);
fabe5c0e 161
2c21044f 162 STRV_FOREACH(p, dirs) {
a5af5f80
YW
163 _cleanup_closedir_ DIR *dir = NULL;
164 _cleanup_free_ char *path = NULL;
165
166 r = chase_and_opendir(*p, root, CHASE_PREFIX_ROOT, &path, &dir);
167 if (r < 0) {
168 if (r != -ENOENT)
169 log_debug_errno(r, "Failed to chase and open directory '%s', ignoring: %m", *p);
170 continue;
171 }
172
173 r = files_add(dir, path, &fh, &masked, suffix, flags);
31d5192d 174 if (r == -ENOMEM)
fabe5c0e 175 return r;
31d5192d 176 if (r < 0)
a5af5f80 177 log_debug_errno(r, "Failed to search for files in '%s', ignoring: %m", path);
2c21044f
KS
178 }
179
1a39bddf 180 return copy_and_sort_files_from_hashmap(fh, ret);
fabe5c0e
LP
181}
182
b1229544
YW
183int conf_files_list_strv_at(
184 char ***ret,
185 const char *suffix,
186 int rfd,
187 unsigned flags,
188 const char * const *dirs) {
189
190 _cleanup_hashmap_free_ Hashmap *fh = NULL;
191 _cleanup_set_free_ Set *masked = NULL;
192 int r;
193
194 assert(rfd >= 0 || rfd == AT_FDCWD);
195 assert(ret);
196
197 STRV_FOREACH(p, dirs) {
198 _cleanup_closedir_ DIR *dir = NULL;
199 _cleanup_free_ char *path = NULL;
200
201 r = chase_and_opendirat(rfd, *p, CHASE_AT_RESOLVE_IN_ROOT, &path, &dir);
202 if (r < 0) {
203 if (r != -ENOENT)
204 log_debug_errno(r, "Failed to chase and open directory '%s', ignoring: %m", *p);
205 continue;
206 }
207
208 r = files_add(dir, path, &fh, &masked, suffix, flags);
209 if (r == -ENOMEM)
210 return r;
211 if (r < 0)
212 log_debug_errno(r, "Failed to search for files in '%s', ignoring: %m", path);
213 }
214
215 return copy_and_sort_files_from_hashmap(fh, ret);
216}
217
a6d8474f 218int conf_files_insert(char ***strv, const char *root, char **dirs, const char *path) {
d16a1c1b
ZJS
219 /* Insert a path into strv, at the place honouring the usual sorting rules:
220 * - we first compare by the basename
221 * - and then we compare by dirname, allowing just one file with the given
222 * basename.
223 * This means that we will
224 * - add a new entry if basename(path) was not on the list,
225 * - do nothing if an entry with higher priority was already present,
226 * - do nothing if our new entry matches the existing entry,
227 * - replace the existing entry if our new entry has higher priority.
228 */
243dd6ae 229 size_t i, n;
d16a1c1b 230 char *t;
d16a1c1b
ZJS
231 int r;
232
243dd6ae
LP
233 n = strv_length(*strv);
234 for (i = 0; i < n; i++) {
d16a1c1b
ZJS
235 int c;
236
93bab288 237 c = base_cmp((char* const*) *strv + i, (char* const*) &path);
de010b0b 238 if (c == 0)
082bb1c5 239 /* Oh, there already is an entry with a matching name (the last component). */
a6d8474f 240 STRV_FOREACH(dir, dirs) {
082bb1c5 241 _cleanup_free_ char *rdir = NULL;
d16a1c1b
ZJS
242 char *p1, *p2;
243
c6134d3e 244 rdir = path_join(root, *dir);
082bb1c5
ZJS
245 if (!rdir)
246 return -ENOMEM;
247
248 p1 = path_startswith((*strv)[i], rdir);
d16a1c1b
ZJS
249 if (p1)
250 /* Existing entry with higher priority
251 * or same priority, no need to do anything. */
252 return 0;
253
a6d8474f 254 p2 = path_startswith(path, *dir);
d16a1c1b
ZJS
255 if (p2) {
256 /* Our new entry has higher priority */
082bb1c5 257
c6134d3e 258 t = path_join(root, path);
d16a1c1b
ZJS
259 if (!t)
260 return log_oom();
261
262 return free_and_replace((*strv)[i], t);
263 }
264 }
265
de010b0b 266 else if (c > 0)
d16a1c1b
ZJS
267 /* Following files have lower priority, let's go insert our
268 * new entry. */
269 break;
270
271 /* … we are not there yet, let's continue */
272 }
273
082bb1c5 274 /* The new file has lower priority than all the existing entries */
c6134d3e 275 t = path_join(root, path);
d16a1c1b 276 if (!t)
a7181c67 277 return -ENOMEM;
d16a1c1b
ZJS
278
279 r = strv_insert(strv, i, t);
280 if (r < 0)
281 free(t);
d16a1c1b 282
380b82d6 283 return r;
a6d8474f
ZJS
284}
285
94916255 286int conf_files_list(char ***ret, const char *suffix, const char *root, unsigned flags, const char *dir) {
a5af5f80 287 return conf_files_list_strv(ret, suffix, root, flags, STRV_MAKE_CONST(dir));
fabe5c0e 288}
2c21044f 289
b1229544
YW
290int conf_files_list_at(char ***ret, const char *suffix, int rfd, unsigned flags, const char *dir) {
291 return conf_files_list_strv_at(ret, suffix, rfd, flags, STRV_MAKE_CONST(dir));
292}
293
94916255 294int conf_files_list_nulstr(char ***ret, const char *suffix, const char *root, unsigned flags, const char *dirs) {
a6d8474f 295 _cleanup_strv_free_ char **d = NULL;
fabe5c0e 296
94916255 297 assert(ret);
fabe5c0e 298
a6d8474f
ZJS
299 d = strv_split_nulstr(dirs);
300 if (!d)
fabe5c0e 301 return -ENOMEM;
2c21044f 302
a5af5f80 303 return conf_files_list_strv(ret, suffix, root, flags, (const char**) d);
2c21044f 304}
854a42fb 305
b1229544
YW
306int conf_files_list_nulstr_at(char ***ret, const char *suffix, int rfd, unsigned flags, const char *dirs) {
307 _cleanup_strv_free_ char **d = NULL;
308
309 assert(ret);
310
311 d = strv_split_nulstr(dirs);
312 if (!d)
313 return -ENOMEM;
314
315 return conf_files_list_strv_at(ret, suffix, rfd, flags, (const char**) d);
316}
317
ceaaeb9b
ZJS
318int conf_files_list_with_replacement(
319 const char *root,
320 char **config_dirs,
321 const char *replacement,
94916255
LP
322 char ***ret_files,
323 char **ret_replace_file) {
ceaaeb9b
ZJS
324
325 _cleanup_strv_free_ char **f = NULL;
326 _cleanup_free_ char *p = NULL;
327 int r;
328
329 assert(config_dirs);
94916255
LP
330 assert(ret_files);
331 assert(ret_replace_file || !replacement);
ceaaeb9b
ZJS
332
333 r = conf_files_list_strv(&f, ".conf", root, 0, (const char* const*) config_dirs);
334 if (r < 0)
335 return log_error_errno(r, "Failed to enumerate config files: %m");
336
337 if (replacement) {
338 r = conf_files_insert(&f, root, config_dirs, replacement);
339 if (r < 0)
340 return log_error_errno(r, "Failed to extend config file list: %m");
341
c6134d3e 342 p = path_join(root, replacement);
ceaaeb9b
ZJS
343 if (!p)
344 return log_oom();
345 }
346
94916255
LP
347 *ret_files = TAKE_PTR(f);
348 if (ret_replace_file)
349 *ret_replace_file = TAKE_PTR(p);
350
ceaaeb9b
ZJS
351 return 0;
352}
35c0e344
MY
353
354int conf_files_list_dropins(
355 char ***ret,
356 const char *dropin_dirname,
357 const char *root,
358 const char * const *dirs) {
359
360 _cleanup_strv_free_ char **dropin_dirs = NULL;
361 const char *suffix;
362 int r;
363
364 assert(ret);
365 assert(dropin_dirname);
366 assert(dirs);
367
368 suffix = strjoina("/", dropin_dirname);
369 r = strv_extend_strv_concat(&dropin_dirs, (char**) dirs, suffix);
370 if (r < 0)
371 return r;
372
373 return conf_files_list_strv(ret, ".conf", root, 0, (const char* const*) dropin_dirs);
374}