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