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