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