chiark / gitweb /
30a490326a48dabe72b8d0e8cda41373ebe2e8b5
[elogind.git] / src / basic / conf-files.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   Copyright 2010 Lennart Poettering
4 ***/
5
6 #include <dirent.h>
7 #include <errno.h>
8 #include <stdarg.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 #include "conf-files.h"
14 //#include "def.h"
15 #include "dirent-util.h"
16 #include "fd-util.h"
17 #include "hashmap.h"
18 #include "log.h"
19 #include "macro.h"
20 #include "missing.h"
21 #include "path-util.h"
22 //#include "set.h"
23 #include "stat-util.h"
24 #include "string-util.h"
25 #include "strv.h"
26 //#include "terminal-util.h"
27 #include "util.h"
28
29 static int files_add(
30                 Hashmap *h,
31                 Set *masked,
32                 const char *suffix,
33                 const char *root,
34                 unsigned flags,
35                 const char *path) {
36
37         _cleanup_closedir_ DIR *dir = NULL;
38         const char *dirpath;
39         struct dirent *de;
40         int r;
41
42         assert(h);
43         assert((flags & CONF_FILES_FILTER_MASKED) == 0 || masked);
44         assert(path);
45
46         dirpath = prefix_roota(root, path);
47
48         dir = opendir(dirpath);
49         if (!dir) {
50                 if (errno == ENOENT)
51                         return 0;
52
53                 return log_debug_errno(errno, "Failed to open directory '%s': %m", dirpath);
54         }
55
56         FOREACH_DIRENT(de, dir, return -errno) {
57                 struct stat st;
58                 char *p, *key;
59
60                 /* Does this match the suffix? */
61                 if (suffix && !endswith(de->d_name, suffix))
62                         continue;
63
64                 /* Has this file already been found in an earlier directory? */
65                 if (hashmap_contains(h, de->d_name)) {
66                         log_debug("Skipping overridden file '%s/%s'.", dirpath, de->d_name);
67                         continue;
68                 }
69
70                 /* Has this been masked in an earlier directory? */
71                 if ((flags & CONF_FILES_FILTER_MASKED) && set_contains(masked, de->d_name)) {
72                         log_debug("File '%s/%s' is masked by previous entry.", dirpath, de->d_name);
73                         continue;
74                 }
75
76                 /* Read file metadata if we shall validate the check for file masks, for node types or whether the node is marked executable. */
77                 if (flags & (CONF_FILES_FILTER_MASKED|CONF_FILES_REGULAR|CONF_FILES_DIRECTORY|CONF_FILES_EXECUTABLE))
78                         if (fstatat(dirfd(dir), de->d_name, &st, 0) < 0) {
79                                 log_debug_errno(errno, "Failed to stat '%s/%s', ignoring: %m", dirpath, de->d_name);
80                                 continue;
81                         }
82
83                 /* Is this a masking entry? */
84                 if ((flags & CONF_FILES_FILTER_MASKED))
85                         if (null_or_empty(&st)) {
86                                 /* Mark this one as masked */
87                                 r = set_put_strdup(masked, de->d_name);
88                                 if (r < 0)
89                                         return r;
90
91                                 log_debug("File '%s/%s' is a mask.", dirpath, de->d_name);
92                                 continue;
93                         }
94
95                 /* Does this node have the right type? */
96                 if (flags & (CONF_FILES_REGULAR|CONF_FILES_DIRECTORY))
97                         if (!((flags & CONF_FILES_DIRECTORY) && S_ISDIR(st.st_mode)) &&
98                             !((flags & CONF_FILES_REGULAR) && S_ISREG(st.st_mode))) {
99                                 log_debug("Ignoring '%s/%s', as it is not a of the right type.", dirpath, de->d_name);
100                                 continue;
101                         }
102
103                 /* Does this node have the executable bit set? */
104                 if (flags & CONF_FILES_EXECUTABLE)
105                         /* As requested: check if the file is marked exectuable. Note that we don't check access(X_OK)
106                          * here, as we care about whether the file is marked executable at all, and not whether it is
107                          * executable for us, because if so, such errors are stuff we should log about. */
108
109                         if ((st.st_mode & 0111) == 0) { /* not executable */
110                                 log_debug("Ignoring '%s/%s', as it is not marked executable.", dirpath, de->d_name);
111                                 continue;
112                         }
113
114                 if (flags & CONF_FILES_BASENAME) {
115                         p = strdup(de->d_name);
116                         if (!p)
117                                 return -ENOMEM;
118
119                         key = p;
120                 } else {
121                         p = strjoin(dirpath, "/", de->d_name);
122                         if (!p)
123                                 return -ENOMEM;
124
125                         key = basename(p);
126                 }
127
128                 r = hashmap_put(h, key, p);
129                 if (r < 0) {
130                         free(p);
131                         return log_debug_errno(r, "Failed to add item to hashmap: %m");
132                 }
133
134                 assert(r > 0);
135         }
136
137         return 0;
138 }
139
140 static int base_cmp(const void *a, const void *b) {
141         const char *s1, *s2;
142
143         s1 = *(char * const *)a;
144         s2 = *(char * const *)b;
145         return strcmp(basename(s1), basename(s2));
146 }
147
148 static int conf_files_list_strv_internal(char ***strv, const char *suffix, const char *root, unsigned flags, char **dirs) {
149         _cleanup_hashmap_free_ Hashmap *fh = NULL;
150         _cleanup_set_free_free_ Set *masked = NULL;
151         char **files, **p;
152         int r;
153
154         assert(strv);
155
156         /* This alters the dirs string array */
157         if (!path_strv_resolve_uniq(dirs, root))
158                 return -ENOMEM;
159
160         fh = hashmap_new(&path_hash_ops);
161         if (!fh)
162                 return -ENOMEM;
163
164         if (flags & CONF_FILES_FILTER_MASKED) {
165                 masked = set_new(&path_hash_ops);
166                 if (!masked)
167                         return -ENOMEM;
168         }
169
170         STRV_FOREACH(p, dirs) {
171                 r = files_add(fh, masked, suffix, root, flags, *p);
172                 if (r == -ENOMEM)
173                         return r;
174                 if (r < 0)
175                         log_debug_errno(r, "Failed to search for files in %s, ignoring: %m", *p);
176         }
177
178         files = hashmap_get_strv(fh);
179         if (!files)
180                 return -ENOMEM;
181
182         qsort_safe(files, hashmap_size(fh), sizeof(char *), base_cmp);
183         *strv = files;
184
185         return 0;
186 }
187
188 int conf_files_insert(char ***strv, const char *root, char **dirs, const char *path) {
189         /* Insert a path into strv, at the place honouring the usual sorting rules:
190          * - we first compare by the basename
191          * - and then we compare by dirname, allowing just one file with the given
192          *   basename.
193          * This means that we will
194          * - add a new entry if basename(path) was not on the list,
195          * - do nothing if an entry with higher priority was already present,
196          * - do nothing if our new entry matches the existing entry,
197          * - replace the existing entry if our new entry has higher priority.
198          */
199         size_t i;
200         char *t;
201         int r;
202
203         for (i = 0; i < strv_length(*strv); i++) {
204                 int c;
205
206                 c = base_cmp(*strv + i, &path);
207                 if (c == 0) {
208                         char **dir;
209
210                         /* Oh, we found our spot and it already contains something. */
211                         STRV_FOREACH(dir, dirs) {
212                                 char *p1, *p2;
213
214                                 p1 = path_startswith((*strv)[i], root);
215                                 if (p1)
216                                         /* Skip "/" in *dir, because p1 is without "/" too */
217                                         p1 = path_startswith(p1, *dir + 1);
218                                 if (p1)
219                                         /* Existing entry with higher priority
220                                          * or same priority, no need to do anything. */
221                                         return 0;
222
223                                 p2 = path_startswith(path, *dir);
224                                 if (p2) {
225                                         /* Our new entry has higher priority */
226                                         t = path_join(root, path, NULL);
227                                         if (!t)
228                                                 return log_oom();
229
230                                         return free_and_replace((*strv)[i], t);
231                                 }
232                         }
233
234                 } else if (c > 0)
235                         /* Following files have lower priority, let's go insert our
236                          * new entry. */
237                         break;
238
239                 /* … we are not there yet, let's continue */
240         }
241
242         t = path_join(root, path, NULL);
243         if (!t)
244                 return log_oom();
245
246         r = strv_insert(strv, i, t);
247         if (r < 0)
248                 free(t);
249         return r;
250 }
251
252 int conf_files_insert_nulstr(char ***strv, const char *root, const char *dirs, const char *path) {
253         _cleanup_strv_free_ char **d = NULL;
254
255         assert(strv);
256
257         d = strv_split_nulstr(dirs);
258         if (!d)
259                 return -ENOMEM;
260
261         return conf_files_insert(strv, root, d, path);
262 }
263
264 int conf_files_list_strv(char ***strv, const char *suffix, const char *root, unsigned flags, const char* const* dirs) {
265         _cleanup_strv_free_ char **copy = NULL;
266
267         assert(strv);
268
269         copy = strv_copy((char**) dirs);
270         if (!copy)
271                 return -ENOMEM;
272
273         return conf_files_list_strv_internal(strv, suffix, root, flags, copy);
274 }
275
276 int conf_files_list(char ***strv, const char *suffix, const char *root, unsigned flags, const char *dir, ...) {
277         _cleanup_strv_free_ char **dirs = NULL;
278         va_list ap;
279
280         assert(strv);
281
282         va_start(ap, dir);
283         dirs = strv_new_ap(dir, ap);
284         va_end(ap);
285
286         if (!dirs)
287                 return -ENOMEM;
288
289         return conf_files_list_strv_internal(strv, suffix, root, flags, dirs);
290 }
291
292 int conf_files_list_nulstr(char ***strv, const char *suffix, const char *root, unsigned flags, const char *dirs) {
293         _cleanup_strv_free_ char **d = NULL;
294
295         assert(strv);
296
297         d = strv_split_nulstr(dirs);
298         if (!d)
299                 return -ENOMEM;
300
301         return conf_files_list_strv_internal(strv, suffix, root, flags, d);
302 }
303
304 int conf_files_list_with_replacement(
305                 const char *root,
306                 char **config_dirs,
307                 const char *replacement,
308                 char ***files,
309                 char **replace_file) {
310
311         _cleanup_strv_free_ char **f = NULL;
312         _cleanup_free_ char *p = NULL;
313         int r;
314
315         assert(config_dirs);
316         assert(files);
317         assert(replace_file || !replacement);
318
319         r = conf_files_list_strv(&f, ".conf", root, 0, (const char* const*) config_dirs);
320         if (r < 0)
321                 return log_error_errno(r, "Failed to enumerate config files: %m");
322
323         if (replacement) {
324                 r = conf_files_insert(&f, root, config_dirs, replacement);
325                 if (r < 0)
326                         return log_error_errno(r, "Failed to extend config file list: %m");
327
328                 p = path_join(root, replacement, NULL);
329                 if (!p)
330                         return log_oom();
331         }
332
333         *files = TAKE_PTR(f);
334         if (replace_file)
335                 *replace_file = TAKE_PTR(p);
336         return 0;
337 }
338
339 int conf_files_cat(const char *root, const char *name) {
340         _cleanup_strv_free_ char **dirs = NULL, **files = NULL;
341         _cleanup_free_ char *path = NULL;
342         const char *dir;
343         char **t;
344         int r;
345
346         NULSTR_FOREACH(dir, CONF_PATHS_NULSTR("")) {
347                 assert(endswith(dir, "/"));
348                 r = strv_extendf(&dirs, "%s%s.d", dir, name);
349                 if (r < 0)
350                         return log_error_errno(r, "Failed to build directory list: %m");
351         }
352
353         r = conf_files_list_strv(&files, ".conf", root, 0, (const char* const*) dirs);
354         if (r < 0)
355                 return log_error_errno(r, "Failed to query file list: %m");
356
357         path = path_join(root, "/etc", name);
358         if (!path)
359                 return log_oom();
360
361         if (DEBUG_LOGGING) {
362                 log_debug("Looking for configuration in:");
363                 log_debug("   %s", path);
364                 STRV_FOREACH(t, dirs)
365                         log_debug("   %s/*.conf", *t);
366         }
367
368         /* show */
369         return cat_files(path, files, CAT_FLAGS_MAIN_FILE_OPTIONAL);
370 }