]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/conf-parser.c
ffe888a4396b759c522325918d56895883541836
[thirdparty/systemd.git] / src / shared / conf-parser.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <linux/ipv6.h>
4 #include <stdio.h>
5
6 #include "sd-id128.h"
7
8 #include "alloc-util.h"
9 #include "calendarspec.h"
10 #include "chase.h"
11 #include "conf-files.h"
12 #include "conf-parser.h"
13 #include "constants.h"
14 #include "dns-domain.h"
15 #include "escape.h"
16 #include "ether-addr-util.h"
17 #include "extract-word.h"
18 #include "fd-util.h"
19 #include "fileio.h"
20 #include "fs-util.h"
21 #include "hash-funcs.h"
22 #include "hostname-util.h"
23 #include "id128-util.h"
24 #include "in-addr-prefix-util.h"
25 #include "ip-protocol-list.h"
26 #include "log.h"
27 #include "missing-network.h"
28 #include "nulstr-util.h"
29 #include "parse-helpers.h"
30 #include "parse-util.h"
31 #include "path-util.h"
32 #include "percent-util.h"
33 #include "process-util.h"
34 #include "rlimit-util.h"
35 #include "set.h"
36 #include "signal-util.h"
37 #include "siphash24.h"
38 #include "socket-util.h"
39 #include "stat-util.h"
40 #include "string-util.h"
41 #include "strv.h"
42 #include "syslog-util.h"
43 #include "time-util.h"
44 #include "utf8.h"
45
46 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(config_file_hash_ops_fclose,
47 char, path_hash_func, path_compare,
48 FILE, safe_fclose);
49
50 int config_item_table_lookup(
51 const void *table,
52 const char *section,
53 const char *lvalue,
54 ConfigParserCallback *ret_func,
55 int *ret_ltype,
56 void **ret_data,
57 void *userdata) {
58
59 assert(table);
60 assert(lvalue);
61 assert(ret_func);
62 assert(ret_ltype);
63 assert(ret_data);
64
65 for (const ConfigTableItem *t = table; t->lvalue; t++) {
66
67 if (!streq(lvalue, t->lvalue))
68 continue;
69
70 if (!streq_ptr(section, t->section))
71 continue;
72
73 *ret_func = t->parse;
74 *ret_ltype = t->ltype;
75 *ret_data = t->data;
76 return 1;
77 }
78
79 *ret_func = NULL;
80 *ret_ltype = 0;
81 *ret_data = NULL;
82 return 0;
83 }
84
85 int config_item_perf_lookup(
86 const void *table,
87 const char *section,
88 const char *lvalue,
89 ConfigParserCallback *ret_func,
90 int *ret_ltype,
91 void **ret_data,
92 void *userdata) {
93
94 ConfigPerfItemLookup lookup = (ConfigPerfItemLookup) table;
95 const ConfigPerfItem *p;
96
97 assert(table);
98 assert(lvalue);
99 assert(ret_func);
100 assert(ret_ltype);
101 assert(ret_data);
102
103 if (section) {
104 const char *key;
105
106 key = strjoina(section, ".", lvalue);
107 p = lookup(key, strlen(key));
108 } else
109 p = lookup(lvalue, strlen(lvalue));
110 if (!p) {
111 *ret_func = NULL;
112 *ret_ltype = 0;
113 *ret_data = NULL;
114 return 0;
115 }
116
117 *ret_func = p->parse;
118 *ret_ltype = p->ltype;
119 *ret_data = (uint8_t*) userdata + p->offset;
120 return 1;
121 }
122
123 /* Run the user supplied parser for an assignment */
124 static int next_assignment(
125 const char *unit,
126 const char *filename,
127 unsigned line,
128 ConfigItemLookup lookup,
129 const void *table,
130 const char *section,
131 unsigned section_line,
132 const char *lvalue,
133 const char *rvalue,
134 ConfigParseFlags flags,
135 void *userdata) {
136
137 ConfigParserCallback func = NULL;
138 int ltype = 0;
139 void *data = NULL;
140 int r;
141
142 assert(filename);
143 assert(line > 0);
144 assert(lookup);
145 assert(lvalue);
146 assert(rvalue);
147
148 r = lookup(table, section, lvalue, &func, &ltype, &data, userdata);
149 if (r < 0)
150 return r;
151 if (r > 0) {
152 if (!func)
153 return 0;
154
155 return func(unit, filename, line, section, section_line,
156 lvalue, ltype, rvalue, data, userdata);
157 }
158
159 /* Warn about unknown non-extension fields. */
160 if (!(flags & CONFIG_PARSE_RELAXED) && !startswith(lvalue, "X-"))
161 log_syntax(unit, LOG_WARNING, filename, line, 0,
162 "Unknown key '%s'%s%s%s, ignoring.",
163 lvalue,
164 section ? " in section [" : "",
165 strempty(section),
166 section ? "]" : "");
167
168 return 0;
169 }
170
171 /* Parse a single logical line */
172 static int parse_line(
173 const char *unit,
174 const char *filename,
175 unsigned line,
176 const char *sections,
177 ConfigItemLookup lookup,
178 const void *table,
179 ConfigParseFlags flags,
180 char **section,
181 unsigned *section_line,
182 bool *section_ignored,
183 char *l, /* is modified */
184 void *userdata) {
185
186 char *e;
187
188 assert(filename);
189 assert(line > 0);
190 assert(lookup);
191 assert(l);
192 assert(section);
193 assert(section_line);
194 assert(section_ignored);
195
196 l = strstrip(l);
197 if (isempty(l))
198 return 0;
199
200 if (l[0] == '\n')
201 return 0;
202
203 if (!utf8_is_valid(l))
204 return log_syntax_invalid_utf8(unit, LOG_WARNING, filename, line, l);
205
206 if (l[0] == '[') {
207 _cleanup_free_ char *n = NULL;
208 size_t k;
209
210 k = strlen(l);
211 assert(k > 0);
212
213 if (l[k-1] != ']')
214 return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EBADMSG), "Invalid section header '%s'", l);
215
216 n = strndup(l+1, k-2);
217 if (!n)
218 return log_oom();
219
220 if (!string_is_safe(n, /* flags= */ 0))
221 return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EBADMSG), "Section header invalid '%s'", l);
222
223 if (sections && !nulstr_contains(sections, n)) {
224 bool ignore;
225
226 ignore = (flags & CONFIG_PARSE_RELAXED) || startswith(n, "X-");
227
228 if (!ignore)
229 NULSTR_FOREACH(t, sections)
230 if (streq_ptr(n, startswith(t, "-"))) { /* Ignore sections prefixed with "-" in valid section list */
231 ignore = true;
232 break;
233 }
234
235 if (!ignore)
236 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown section '%s'. Ignoring.", n);
237
238 *section = mfree(*section);
239 *section_line = 0;
240 *section_ignored = true;
241 } else {
242 free_and_replace(*section, n);
243 *section_line = line;
244 *section_ignored = false;
245 }
246
247 return 0;
248 }
249
250 if (sections && !*section) {
251 if (!(flags & CONFIG_PARSE_RELAXED) && !*section_ignored)
252 log_syntax(unit, LOG_WARNING, filename, line, 0, "Assignment outside of section. Ignoring.");
253
254 return 0;
255 }
256
257 e = strchr(l, '=');
258 if (!e)
259 return log_syntax(unit, LOG_WARNING, filename, line, 0,
260 "Missing '=', ignoring line.");
261 if (e == l)
262 return log_syntax(unit, LOG_WARNING, filename, line, 0,
263 "Missing key name before '=', ignoring line.");
264
265 *e = 0;
266 e++;
267
268 return next_assignment(unit,
269 filename,
270 line,
271 lookup,
272 table,
273 *section,
274 *section_line,
275 strstrip(l),
276 strstrip(e),
277 flags,
278 userdata);
279 }
280
281 /* Go through the file and parse each line */
282 int config_parse(
283 const char *unit,
284 const char *filename,
285 FILE *f,
286 const char *sections,
287 ConfigItemLookup lookup,
288 const void *table,
289 ConfigParseFlags flags,
290 void *userdata,
291 struct stat *ret_stat) {
292
293 _cleanup_free_ char *section = NULL, *continuation = NULL;
294 _cleanup_fclose_ FILE *ours = NULL;
295 unsigned line = 0, section_line = 0;
296 bool section_ignored = false, bom_seen = false;
297 struct stat st;
298 int r, fd;
299
300 assert(filename);
301 assert(lookup);
302
303 if (!f) {
304 f = ours = fopen(filename, "re");
305 if (!f) {
306 /* Only log on request, except for ENOENT,
307 * since we return 0 to the caller. */
308 if ((flags & CONFIG_PARSE_WARN) || errno == ENOENT)
309 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_ERR, errno,
310 "Failed to open configuration file '%s': %m", filename);
311
312 if (errno == ENOENT) {
313 if (ret_stat)
314 *ret_stat = (struct stat) {};
315
316 return 0;
317 }
318
319 return -errno;
320 }
321 }
322
323 fd = fileno(f);
324 if (fd >= 0) { /* stream might not have an fd, let's be careful hence */
325
326 if (fstat(fd, &st) < 0)
327 return log_full_errno(FLAGS_SET(flags, CONFIG_PARSE_WARN) ? LOG_ERR : LOG_DEBUG, errno,
328 "Failed to fstat(%s): %m", filename);
329
330 (void) stat_warn_permissions(filename, &st);
331 } else
332 st = (struct stat) {};
333
334 for (;;) {
335 _cleanup_free_ char *buf = NULL;
336 bool escaped = false;
337 char *l, *p, *e;
338
339 r = read_line(f, LONG_LINE_MAX, &buf);
340 if (r == 0)
341 break;
342 if (r == -ENOBUFS) {
343 if (flags & CONFIG_PARSE_WARN)
344 log_error_errno(r, "%s:%u: Line too long", filename, line);
345
346 return r;
347 }
348 if (r < 0) {
349 if (FLAGS_SET(flags, CONFIG_PARSE_WARN))
350 log_error_errno(r, "%s:%u: Error while reading configuration file: %m", filename, line);
351
352 return r;
353 }
354
355 line++;
356
357 l = skip_leading_chars(buf, WHITESPACE);
358 if (*l != '\0' && strchr(COMMENTS, *l))
359 continue;
360
361 l = buf;
362 if (!bom_seen) {
363 char *q;
364
365 q = startswith(buf, UTF8_BYTE_ORDER_MARK);
366 if (q) {
367 l = q;
368 bom_seen = true;
369 }
370 }
371
372 if (continuation) {
373 if (strlen(continuation) + strlen(l) > LONG_LINE_MAX) {
374 if (flags & CONFIG_PARSE_WARN)
375 log_error("%s:%u: Continuation line too long", filename, line);
376 return -ENOBUFS;
377 }
378
379 if (!strextend(&continuation, l)) {
380 if (flags & CONFIG_PARSE_WARN)
381 log_oom();
382 return -ENOMEM;
383 }
384
385 p = continuation;
386 } else
387 p = l;
388
389 for (e = p; *e; e++) {
390 if (escaped)
391 escaped = false;
392 else if (*e == '\\')
393 escaped = true;
394 }
395
396 if (escaped) {
397 *(e-1) = ' ';
398
399 if (!continuation) {
400 continuation = strdup(l);
401 if (!continuation) {
402 if (flags & CONFIG_PARSE_WARN)
403 log_oom();
404 return -ENOMEM;
405 }
406 }
407
408 continue;
409 }
410
411 r = parse_line(unit,
412 filename,
413 line,
414 sections,
415 lookup,
416 table,
417 flags,
418 &section,
419 &section_line,
420 &section_ignored,
421 p,
422 userdata);
423 if (r < 0) {
424 if (flags & CONFIG_PARSE_WARN)
425 log_warning_errno(r, "%s:%u: Failed to parse file: %m", filename, line);
426 return r;
427 }
428
429 continuation = mfree(continuation);
430 }
431
432 if (continuation) {
433 r = parse_line(unit,
434 filename,
435 ++line,
436 sections,
437 lookup,
438 table,
439 flags,
440 &section,
441 &section_line,
442 &section_ignored,
443 continuation,
444 userdata);
445 if (r < 0) {
446 if (flags & CONFIG_PARSE_WARN)
447 log_warning_errno(r, "%s:%u: Failed to parse file: %m", filename, line);
448 return r;
449 }
450 }
451
452 if (ret_stat)
453 *ret_stat = st;
454
455 return 1;
456 }
457
458 int hashmap_put_stats_by_path(Hashmap **stats_by_path, const char *path, const struct stat *st) {
459 _cleanup_free_ struct stat *st_copy = NULL;
460 _cleanup_free_ char *path_copy = NULL;
461 int r;
462
463 assert(stats_by_path);
464 assert(path);
465 assert(st);
466
467 st_copy = newdup(struct stat, st, 1);
468 if (!st_copy)
469 return -ENOMEM;
470
471 path_copy = strdup(path);
472 if (!path_copy)
473 return -ENOMEM;
474
475 r = hashmap_ensure_put(stats_by_path, &path_hash_ops_free_free, path_copy, st_copy);
476 if (r < 0)
477 return r;
478
479 assert(r > 0);
480 TAKE_PTR(path_copy);
481 TAKE_PTR(st_copy);
482 return 0;
483 }
484
485 static int config_parse_many_files(
486 const char *root,
487 int root_fd,
488 const char* const* conf_files,
489 char **files,
490 const char *sections,
491 ConfigItemLookup lookup,
492 const void *table,
493 ConfigParseFlags flags,
494 void *userdata,
495 Hashmap **ret_stats_by_path) {
496
497 _cleanup_hashmap_free_ Hashmap *stats_by_path = NULL;
498 _cleanup_ordered_hashmap_free_ OrderedHashmap *dropins = NULL;
499 _cleanup_set_free_ Set *inodes = NULL;
500 struct stat st;
501 int r, level = FLAGS_SET(flags, CONFIG_PARSE_WARN) ? LOG_WARNING : LOG_DEBUG;
502
503 if (ret_stats_by_path) {
504 stats_by_path = hashmap_new(&path_hash_ops_free_free);
505 if (!stats_by_path)
506 return log_oom_full(level);
507 }
508
509 /* Pin and stat() all dropins */
510 STRV_FOREACH(fn, files) {
511 _cleanup_fclose_ FILE *f = NULL;
512 r = chase_and_fopenat_unlocked(root_fd, root_fd, *fn, CHASE_MUST_BE_REGULAR, "re", /* ret_path= */ NULL, &f);
513 if (r == -ENOENT)
514 continue;
515 if (r < 0)
516 return log_full_errno(level, r, "Failed to open %s: %m", *fn);
517
518 int fd = fileno(f);
519 assert(fd >= 0);
520
521 r = ordered_hashmap_ensure_put(&dropins, &config_file_hash_ops_fclose, *fn, f);
522 if (r < 0) {
523 assert(r == -ENOMEM);
524 return log_oom_full(level);
525 }
526 assert(r > 0);
527 TAKE_PTR(f);
528
529 /* Get inodes for all drop-ins. Later we'll verify if main config is a symlink to or is
530 * symlinked as one of them. If so, we skip reading main config file directly. */
531
532 _cleanup_free_ struct stat *st_dropin = new(struct stat, 1);
533 if (!st_dropin)
534 return log_oom_full(level);
535
536 if (fstat(fd, st_dropin) < 0)
537 return log_full_errno(level, errno, "Failed to stat %s: %m", *fn);
538
539 r = set_ensure_consume(&inodes, &inode_hash_ops, TAKE_PTR(st_dropin));
540 if (r < 0)
541 return log_oom_full(level);
542 }
543
544 /* First process the first found main config file. */
545 STRV_FOREACH(fn, conf_files) {
546 _cleanup_fclose_ FILE *f = NULL;
547 r = chase_and_fopenat_unlocked(root_fd, root_fd, *fn, CHASE_MUST_BE_REGULAR, "re", /* ret_path= */ NULL, &f);
548 if (r == -ENOENT)
549 continue;
550 if (r < 0)
551 return log_full_errno(level, r, "Failed to open %s: %m", *fn);
552
553 if (inodes) {
554 if (fstat(fileno(f), &st) < 0)
555 return log_full_errno(level, errno, "Failed to stat %s: %m", *fn);
556
557 if (set_contains(inodes, &st)) {
558 log_debug("%s: symlink to/symlinked as drop-in, will be read later.", *fn);
559 break;
560 }
561 }
562
563 r = config_parse(/* unit= */ NULL, *fn, f, sections, lookup, table, flags, userdata, &st);
564 if (r < 0)
565 return r; /* config_parse() logs internally. */
566 assert(r > 0);
567
568 if (ret_stats_by_path) {
569 r = hashmap_put_stats_by_path(&stats_by_path, *fn, &st);
570 if (r < 0)
571 return log_full_errno(level, r, "Failed to save stats of %s: %m", *fn);
572 }
573
574 break;
575 }
576
577 /* Then read all the drop-ins. */
578 const char *path_dropin;
579 FILE *f_dropin;
580 ORDERED_HASHMAP_FOREACH_KEY(f_dropin, path_dropin, dropins) {
581 r = config_parse(/* unit= */ NULL, path_dropin, f_dropin, sections, lookup, table, flags, userdata, &st);
582 if (r < 0)
583 return r; /* config_parse() logs internally. */
584 assert(r > 0);
585
586 if (ret_stats_by_path) {
587 r = hashmap_put_stats_by_path(&stats_by_path, path_dropin, &st);
588 if (r < 0)
589 return log_full_errno(level, r, "Failed to save stats of %s: %m", path_dropin);
590 }
591 }
592
593 if (ret_stats_by_path)
594 *ret_stats_by_path = TAKE_PTR(stats_by_path);
595
596 return 0;
597 }
598
599 static int normalize_root_fd(const char *root, int *root_fd, int *ret_opened_fd) {
600 assert(root_fd);
601 assert(ret_opened_fd);
602
603 /* Normalizes a root dir specification: if root_fd is already valid, keep it. Otherwise, we open the
604 * specified dir */
605
606 if (wildcard_fd_is_valid(*root_fd)) {
607 *ret_opened_fd = -EBADF;
608 return 0;
609 }
610
611 if (empty_or_root(root)) {
612 *root_fd = XAT_FDROOT;
613 *ret_opened_fd = -EBADF;
614 return 0;
615 }
616
617 int fd = open(root, O_CLOEXEC|O_PATH|O_DIRECTORY);
618 if (fd < 0)
619 return log_error_errno(errno, "Failed to open root directory '%s': %m", root);
620
621 *ret_opened_fd = *root_fd = fd;
622 return 0;
623 }
624
625 /* Parse each config file in the directories specified as strv. */
626 int config_parse_many_full(
627 const char* const* conf_files,
628 const char* const* conf_file_dirs,
629 const char *dropin_dirname,
630 const char *root,
631 int root_fd,
632 const char *sections,
633 ConfigItemLookup lookup,
634 const void *table,
635 ConfigParseFlags flags,
636 void *userdata,
637 Hashmap **ret_stats_by_path,
638 char ***ret_dropin_files) {
639
640 _cleanup_strv_free_ char **files = NULL;
641 int r;
642
643 assert(conf_file_dirs);
644 assert(dropin_dirname);
645 assert(table);
646
647 _cleanup_close_ int opened_root_fd = -EBADF;
648 r = normalize_root_fd(root, &root_fd, &opened_root_fd);
649 if (r < 0)
650 return r;
651
652 r = conf_files_list_dropins(
653 &files,
654 dropin_dirname,
655 root,
656 root_fd,
657 FLAGS_SET(flags, CONFIG_PARSE_WARN) ? CONF_FILES_WARN : 0,
658 conf_file_dirs);
659 if (r < 0)
660 return log_full_errno(FLAGS_SET(flags, CONFIG_PARSE_WARN) ? LOG_WARNING : LOG_DEBUG, r,
661 "Failed to list drop-ins in %s: %m", dropin_dirname);
662
663 r = config_parse_many_files(
664 root,
665 root_fd,
666 conf_files,
667 files,
668 sections,
669 lookup,
670 table,
671 flags,
672 userdata,
673 ret_stats_by_path);
674 if (r < 0)
675 return r; /* config_parse_many_files() logs internally. */
676
677 if (ret_dropin_files)
678 *ret_dropin_files = TAKE_PTR(files);
679
680 return 0;
681 }
682
683 int config_parse_standard_file_with_dropins_full(
684 const char *root,
685 int root_fd,
686 const char *main_file, /* A path like "systemd/frobnicator.conf" */
687 const char *sections,
688 ConfigItemLookup lookup,
689 const void *table,
690 ConfigParseFlags flags,
691 void *userdata,
692 Hashmap **ret_stats_by_path,
693 char ***ret_dropin_files) {
694
695 const char* const *conf_file_dirs = (const char* const*) CONF_PATHS_STRV("");
696 _cleanup_strv_free_ char **conf_files = NULL;
697 int r, level = FLAGS_SET(flags, CONFIG_PARSE_WARN) ? LOG_WARNING : LOG_DEBUG;
698
699 /* Build the list of main config files */
700 r = strv_extend_strv_concat(
701 &conf_files,
702 conf_file_dirs,
703 main_file);
704 if (r < 0)
705 return log_oom_full(level);
706
707 _cleanup_free_ char *dropin_dirname = strjoin(main_file, ".d");
708 if (!dropin_dirname)
709 return log_oom_full(level);
710
711 return config_parse_many_full(
712 (const char* const*) conf_files,
713 conf_file_dirs,
714 dropin_dirname,
715 root,
716 root_fd,
717 sections,
718 lookup,
719 table,
720 flags,
721 userdata,
722 ret_stats_by_path,
723 ret_dropin_files);
724 }
725
726 static int dropins_get_stats_by_path(
727 const char* conf_file,
728 const char* const* conf_file_dirs,
729 Hashmap **stats_by_path) {
730
731 _cleanup_strv_free_ char **files = NULL;
732 _cleanup_free_ char *dropin_dirname = NULL;
733 int r;
734
735 assert(conf_file);
736 assert(conf_file_dirs);
737 assert(stats_by_path);
738
739 r = path_extract_filename(conf_file, &dropin_dirname);
740 if (r < 0)
741 return r;
742 if (r == O_DIRECTORY)
743 return -EINVAL;
744
745 if (!strextend(&dropin_dirname, ".d"))
746 return -ENOMEM;
747
748 r = conf_files_list_dropins(
749 &files,
750 dropin_dirname,
751 /* root= */ NULL,
752 /* root_fd= */ XAT_FDROOT,
753 /* flags= */ 0,
754 conf_file_dirs);
755 if (r < 0)
756 return r;
757
758 STRV_FOREACH(fn, files) {
759 struct stat st;
760
761 if (stat(*fn, &st) < 0) {
762 if (errno == ENOENT)
763 continue;
764
765 return -errno;
766 }
767
768 r = hashmap_put_stats_by_path(stats_by_path, *fn, &st);
769 if (r < 0)
770 return r;
771 }
772
773 return 0;
774 }
775
776 int config_get_stats_by_path(
777 const char *suffix,
778 const char *root,
779 ConfFilesFlags flags,
780 const char* const* dirs,
781 bool check_dropins,
782 Hashmap **ret) {
783
784 _cleanup_hashmap_free_ Hashmap *stats_by_path = NULL;
785 _cleanup_strv_free_ char **files = NULL;
786 int r;
787
788 assert(suffix);
789 assert(dirs);
790 assert(ret);
791
792 /* Unlike config_parse(), this does not support stream. */
793
794 r = conf_files_list_strv(&files, suffix, root, flags, dirs);
795 if (r < 0)
796 return r;
797
798 STRV_FOREACH(f, files) {
799 struct stat st;
800
801 /* First read the main config file. */
802 if (stat(*f, &st) < 0) {
803 if (errno == ENOENT)
804 continue;
805
806 return -errno;
807 }
808
809 /* Skipping an empty file. */
810 if (null_or_empty(&st))
811 continue;
812
813 r = hashmap_put_stats_by_path(&stats_by_path, *f, &st);
814 if (r < 0)
815 return r;
816
817 if (!check_dropins)
818 continue;
819
820 /* Then read all the drop-ins if requested. */
821 r = dropins_get_stats_by_path(*f, dirs, &stats_by_path);
822 if (r < 0)
823 return r;
824 }
825
826 *ret = TAKE_PTR(stats_by_path);
827 return 0;
828 }
829
830 bool stats_by_path_equal(Hashmap *a, Hashmap *b) {
831 struct stat *st_a, *st_b;
832 const char *path;
833
834 if (hashmap_size(a) != hashmap_size(b))
835 return false;
836
837 HASHMAP_FOREACH_KEY(st_a, path, a) {
838 st_b = hashmap_get(b, path);
839 if (!st_b)
840 return false;
841
842 if (!stat_inode_unmodified(st_a, st_b))
843 return false;
844 }
845
846 return true;
847 }
848
849 int config_section_parse(
850 const ConfigSectionParser *parsers,
851 size_t n_parsers,
852 const char *unit,
853 const char *filename,
854 unsigned line,
855 const char *section,
856 unsigned section_line,
857 const char *lvalue,
858 int ltype,
859 const char *rvalue,
860 void *userdata) {
861
862 assert(parsers);
863 assert(n_parsers > 0);
864 assert(ltype >= 0);
865 assert((size_t) ltype < n_parsers);
866 assert(userdata);
867
868 const ConfigSectionParser *e = parsers + ltype;
869 assert(e->parser);
870
871 /* This is used when a object is dynamically allocated per [SECTION] in a config parser, e.g.
872 * [Address] for systemd.network. Takes the allocated object as 'userdata', then it is passed to
873 * config parsers in the table. The 'data' field points to an element of the passed object, where
874 * its offset is given by the table. */
875
876 return e->parser(unit, filename, line, section, section_line, lvalue, e->ltype, rvalue,
877 (uint8_t*) userdata + e->offset, userdata);
878 }
879
880 void config_section_hash_func(const ConfigSection *c, struct siphash *state) {
881 siphash24_compress_string(c->filename, state);
882 siphash24_compress_typesafe(c->line, state);
883 }
884
885 int config_section_compare_func(const ConfigSection *x, const ConfigSection *y) {
886 int r;
887
888 r = strcmp(x->filename, y->filename);
889 if (r != 0)
890 return r;
891
892 return CMP(x->line, y->line);
893 }
894
895 DEFINE_HASH_OPS(config_section_hash_ops, ConfigSection, config_section_hash_func, config_section_compare_func);
896
897 int config_section_new(const char *filename, unsigned line, ConfigSection **ret) {
898 ConfigSection *cs;
899
900 assert(filename);
901 assert(line > 0);
902 assert(ret);
903
904 cs = malloc0(offsetof(ConfigSection, filename) + strlen(filename) + 1);
905 if (!cs)
906 return -ENOMEM;
907
908 strcpy(cs->filename, filename);
909 cs->line = line;
910
911 *ret = TAKE_PTR(cs);
912 return 0;
913 }
914
915 static int _hashmap_by_section_find_unused_line(
916 HashmapBase *entries_by_section,
917 const char *filename,
918 unsigned *ret) {
919
920 ConfigSection *cs;
921 unsigned n = 0;
922 void *entry;
923
924 assert(ret);
925
926 HASHMAP_BASE_FOREACH_KEY(entry, cs, entries_by_section) {
927 if (filename && !streq(cs->filename, filename))
928 continue;
929 n = MAX(n, cs->line);
930 }
931
932 /* overflow? */
933 if (n >= UINT_MAX)
934 return -EFBIG;
935
936 *ret = n + 1;
937 return 0;
938 }
939
940 int hashmap_by_section_find_unused_line(
941 Hashmap *entries_by_section,
942 const char *filename,
943 unsigned *ret) {
944
945 return _hashmap_by_section_find_unused_line(HASHMAP_BASE(entries_by_section), filename, ret);
946 }
947
948 int ordered_hashmap_by_section_find_unused_line(
949 OrderedHashmap *entries_by_section,
950 const char *filename,
951 unsigned *ret) {
952
953 return _hashmap_by_section_find_unused_line(HASHMAP_BASE(entries_by_section), filename, ret);
954 }
955
956 #define DEFINE_PARSER(type, vartype, conv_func) \
957 DEFINE_CONFIG_PARSE_PTR(config_parse_##type, conv_func, vartype)
958
959 DEFINE_PARSER(int, int, safe_atoi);
960 DEFINE_PARSER(long, long, safe_atoli);
961 DEFINE_PARSER(uint8, uint8_t, safe_atou8);
962 DEFINE_PARSER(uint16, uint16_t, safe_atou16);
963 DEFINE_PARSER(uint32, uint32_t, safe_atou32);
964 DEFINE_PARSER(int32, int32_t, safe_atoi32);
965 DEFINE_PARSER(uint64, uint64_t, safe_atou64);
966 DEFINE_PARSER(unsigned, unsigned, safe_atou);
967 DEFINE_PARSER(double, double, safe_atod);
968 DEFINE_PARSER(nsec, nsec_t, parse_nsec);
969 DEFINE_PARSER(sec, usec_t, parse_sec);
970 DEFINE_PARSER(sec_def_infinity, usec_t, parse_sec_def_infinity);
971 DEFINE_PARSER(mode, mode_t, parse_mode);
972 DEFINE_PARSER(pid, pid_t, parse_pid);
973
974 int config_parse_iec_size(
975 const char *unit,
976 const char *filename,
977 unsigned line,
978 const char *section,
979 unsigned section_line,
980 const char *lvalue,
981 int ltype,
982 const char *rvalue,
983 void *data,
984 void *userdata) {
985
986 size_t *sz = ASSERT_PTR(data);
987 uint64_t v;
988 int r;
989
990 assert(filename);
991 assert(lvalue);
992 assert(rvalue);
993
994 r = parse_size(rvalue, 1024, &v);
995 if (r >= 0 && (uint64_t) (size_t) v != v)
996 r = -ERANGE;
997 if (r < 0)
998 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
999
1000 *sz = (size_t) v;
1001 return 1;
1002 }
1003
1004 int config_parse_iec_size_long(
1005 const char *unit,
1006 const char *filename,
1007 unsigned line,
1008 const char *section,
1009 unsigned section_line,
1010 const char *lvalue,
1011 int ltype,
1012 const char *rvalue,
1013 void *data,
1014 void *userdata) {
1015
1016 long *sz = ASSERT_PTR(data);
1017 uint64_t v;
1018 int r;
1019
1020 assert(filename);
1021 assert(lvalue);
1022 assert(rvalue);
1023
1024 r = parse_size(rvalue, 1024, &v);
1025 if (r >= 0 && v > LONG_MAX)
1026 r = -ERANGE;
1027 if (r < 0)
1028 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
1029
1030 *sz = (long) v;
1031 return 1;
1032 }
1033
1034 int config_parse_si_uint64(
1035 const char *unit,
1036 const char *filename,
1037 unsigned line,
1038 const char *section,
1039 unsigned section_line,
1040 const char *lvalue,
1041 int ltype,
1042 const char *rvalue,
1043 void *data,
1044 void *userdata) {
1045
1046 uint64_t *sz = ASSERT_PTR(data);
1047 int r;
1048
1049 assert(filename);
1050 assert(lvalue);
1051 assert(rvalue);
1052
1053 r = parse_size(rvalue, 1000, sz);
1054 if (r < 0)
1055 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
1056
1057 return 1;
1058 }
1059
1060 int config_parse_iec_uint64(
1061 const char *unit,
1062 const char *filename,
1063 unsigned line,
1064 const char *section,
1065 unsigned section_line,
1066 const char *lvalue,
1067 int ltype,
1068 const char *rvalue,
1069 void *data,
1070 void *userdata) {
1071
1072 uint64_t *bytes = ASSERT_PTR(data);
1073 int r;
1074
1075 assert(filename);
1076 assert(lvalue);
1077 assert(rvalue);
1078
1079 r = parse_size(rvalue, 1024, bytes);
1080 if (r < 0)
1081 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
1082
1083 return 1;
1084 }
1085
1086 int config_parse_iec_uint64_infinity(
1087 const char *unit,
1088 const char *filename,
1089 unsigned line,
1090 const char *section,
1091 unsigned section_line,
1092 const char *lvalue,
1093 int ltype,
1094 const char *rvalue,
1095 void *data,
1096 void *userdata) {
1097
1098 uint64_t *bytes = ASSERT_PTR(data);
1099
1100 assert(rvalue);
1101
1102 if (streq(rvalue, "infinity")) {
1103 *bytes = UINT64_MAX;
1104 return 1;
1105 }
1106
1107 return config_parse_iec_uint64(unit, filename, line, section, section_line, lvalue, ltype, rvalue, data, userdata);
1108 }
1109
1110 int config_parse_bool(
1111 const char *unit,
1112 const char *filename,
1113 unsigned line,
1114 const char *section,
1115 unsigned section_line,
1116 const char *lvalue,
1117 int ltype,
1118 const char *rvalue,
1119 void *data,
1120 void *userdata) {
1121
1122 bool *b = ASSERT_PTR(data);
1123 bool fatal = ltype;
1124 int r;
1125
1126 assert(filename);
1127 assert(lvalue);
1128 assert(rvalue);
1129
1130 r = parse_boolean(rvalue);
1131 if (r < 0) {
1132 log_syntax_parse_error_full(unit, filename, line, r, fatal, lvalue, rvalue);
1133 return fatal ? -ENOEXEC : 0;
1134 }
1135
1136 *b = r;
1137 return 1; /* set */
1138 }
1139
1140 int config_parse_uint32_flag(
1141 const char *unit,
1142 const char *filename,
1143 unsigned line,
1144 const char *section,
1145 unsigned section_line,
1146 const char *lvalue,
1147 int ltype,
1148 const char *rvalue,
1149 void *data,
1150 void *userdata) {
1151
1152 uint32_t *flags = ASSERT_PTR(data);
1153 int r;
1154
1155 assert(ltype != 0);
1156
1157 r = isempty(rvalue) ? 0 : parse_boolean(rvalue);
1158 if (r < 0)
1159 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
1160
1161 SET_FLAG(*flags, ltype, r);
1162 return 1;
1163 }
1164
1165 int config_parse_uint32_invert_flag(
1166 const char *unit,
1167 const char *filename,
1168 unsigned line,
1169 const char *section,
1170 unsigned section_line,
1171 const char *lvalue,
1172 int ltype,
1173 const char *rvalue,
1174 void *data,
1175 void *userdata) {
1176
1177 uint32_t *flags = ASSERT_PTR(data);
1178 int r;
1179
1180 assert(ltype != 0);
1181
1182 r = isempty(rvalue) ? 0 : parse_boolean(rvalue);
1183 if (r < 0)
1184 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
1185
1186 SET_FLAG(*flags, ltype, !r);
1187 return 1;
1188 }
1189
1190 int config_parse_id128(
1191 const char *unit,
1192 const char *filename,
1193 unsigned line,
1194 const char *section,
1195 unsigned section_line,
1196 const char *lvalue,
1197 int ltype,
1198 const char *rvalue,
1199 void *data,
1200 void *userdata) {
1201
1202 sd_id128_t *result = data;
1203 int r;
1204
1205 assert(filename);
1206 assert(lvalue);
1207 assert(rvalue);
1208
1209 r = id128_from_string_nonzero(rvalue, result);
1210 if (r == -ENXIO) {
1211 log_syntax(unit, LOG_WARNING, filename, line, r, "128-bit ID/UUID is all 0, ignoring: %s", rvalue);
1212 return 0;
1213 }
1214 if (r < 0)
1215 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
1216
1217 return 1;
1218 }
1219
1220 int config_parse_tristate(
1221 const char *unit,
1222 const char *filename,
1223 unsigned line,
1224 const char *section,
1225 unsigned section_line,
1226 const char *lvalue,
1227 int ltype,
1228 const char *rvalue,
1229 void *data,
1230 void *userdata) {
1231
1232 int r, *t = ASSERT_PTR(data);
1233
1234 assert(filename);
1235 assert(lvalue);
1236 assert(rvalue);
1237
1238 /* A tristate is pretty much a boolean, except that it can also take an empty string,
1239 * indicating "uninitialized", much like NULL is for a pointer type. */
1240
1241 if (isempty(rvalue)) {
1242 *t = -1;
1243 return 1;
1244 }
1245
1246 r = parse_tristate(rvalue, t);
1247 if (r < 0)
1248 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
1249
1250 return 1;
1251 }
1252
1253 int config_parse_string(
1254 const char *unit,
1255 const char *filename,
1256 unsigned line,
1257 const char *section,
1258 unsigned section_line,
1259 const char *lvalue,
1260 int ltype,
1261 const char *rvalue,
1262 void *data,
1263 void *userdata) {
1264
1265 char **s = ASSERT_PTR(data);
1266 int r;
1267
1268 assert(filename);
1269 assert(lvalue);
1270 assert(rvalue);
1271
1272 if (isempty(rvalue)) {
1273 *s = mfree(*s);
1274 return 1;
1275 }
1276
1277 if (FLAGS_SET(ltype, CONFIG_PARSE_STRING_SAFE) && !string_is_safe(rvalue, STRING_ALLOW_GLOBS)) {
1278 _cleanup_free_ char *escaped = NULL;
1279
1280 escaped = cescape(rvalue);
1281 log_syntax(unit, LOG_WARNING, filename, line, 0,
1282 "Specified string contains unsafe characters, ignoring: %s", strna(escaped));
1283 return 0;
1284 }
1285
1286 if (FLAGS_SET(ltype, CONFIG_PARSE_STRING_ASCII) && !ascii_is_valid(rvalue)) {
1287 _cleanup_free_ char *escaped = NULL;
1288
1289 escaped = cescape(rvalue);
1290 log_syntax(unit, LOG_WARNING, filename, line, 0,
1291 "Specified string contains invalid ASCII characters, ignoring: %s", strna(escaped));
1292 return 0;
1293 }
1294
1295 r = free_and_strdup_warn(s, rvalue);
1296 if (r < 0)
1297 return r;
1298
1299 return 1;
1300 }
1301
1302 int config_parse_dns_name(
1303 const char *unit,
1304 const char *filename,
1305 unsigned line,
1306 const char *section,
1307 unsigned section_line,
1308 const char *lvalue,
1309 int ltype,
1310 const char *rvalue,
1311 void *data,
1312 void *userdata) {
1313
1314 char **hostname = ASSERT_PTR(data);
1315 int r;
1316
1317 assert(filename);
1318 assert(lvalue);
1319 assert(rvalue);
1320
1321 if (isempty(rvalue)) {
1322 *hostname = mfree(*hostname);
1323 return 1;
1324 }
1325
1326 r = dns_name_is_valid(rvalue);
1327 if (r < 0) {
1328 log_syntax(unit, LOG_WARNING, filename, line, r,
1329 "Failed to check validity of DNS domain name '%s', ignoring assignment: %m", rvalue);
1330 return 0;
1331 }
1332 if (r == 0) {
1333 log_syntax(unit, LOG_WARNING, filename, line, 0,
1334 "Specified invalid DNS domain name, ignoring assignment: %s", rvalue);
1335 return 0;
1336 }
1337
1338 r = free_and_strdup_warn(hostname, rvalue);
1339 if (r < 0)
1340 return r;
1341
1342 return 1;
1343 }
1344
1345 int config_parse_hostname(
1346 const char *unit,
1347 const char *filename,
1348 unsigned line,
1349 const char *section,
1350 unsigned section_line,
1351 const char *lvalue,
1352 int ltype,
1353 const char *rvalue,
1354 void *data,
1355 void *userdata) {
1356
1357 char **hostname = ASSERT_PTR(data);
1358
1359 assert(filename);
1360 assert(lvalue);
1361 assert(rvalue);
1362
1363 if (isempty(rvalue)) {
1364 *hostname = mfree(*hostname);
1365 return 1;
1366 }
1367
1368 if (!hostname_is_valid(rvalue, 0)) {
1369 log_syntax(unit, LOG_WARNING, filename, line, 0,
1370 "Specified invalid hostname, ignoring assignment: %s", rvalue);
1371 return 0;
1372 }
1373
1374 return config_parse_dns_name(unit, filename, line, section, section_line,
1375 lvalue, ltype, rvalue, data, userdata);
1376 }
1377
1378 int config_parse_path(
1379 const char *unit,
1380 const char *filename,
1381 unsigned line,
1382 const char *section,
1383 unsigned section_line,
1384 const char *lvalue,
1385 int ltype,
1386 const char *rvalue,
1387 void *data,
1388 void *userdata) {
1389
1390 _cleanup_free_ char *n = NULL;
1391 bool fatal = ltype;
1392 char **s = ASSERT_PTR(data);
1393 int r;
1394
1395 assert(filename);
1396 assert(lvalue);
1397 assert(rvalue);
1398
1399 if (isempty(rvalue)) {
1400 *s = mfree(*s);
1401 return 1;
1402 }
1403
1404 n = strdup(rvalue);
1405 if (!n)
1406 return log_oom();
1407
1408 r = path_simplify_and_warn(n, PATH_CHECK_ABSOLUTE | (fatal ? PATH_CHECK_FATAL : 0), unit, filename, line, lvalue);
1409 if (r < 0)
1410 return fatal ? -ENOEXEC : 0;
1411
1412 free_and_replace(*s, n);
1413 return 1;
1414 }
1415
1416 int config_parse_strv(
1417 const char *unit,
1418 const char *filename,
1419 unsigned line,
1420 const char *section,
1421 unsigned section_line,
1422 const char *lvalue,
1423 int ltype, /* When true, duplicated entries will be filtered. */
1424 const char *rvalue,
1425 void *data,
1426 void *userdata) {
1427
1428 char ***sv = ASSERT_PTR(data);
1429 int r;
1430
1431 assert(filename);
1432 assert(lvalue);
1433 assert(rvalue);
1434
1435 if (isempty(rvalue)) {
1436 *sv = strv_free(*sv);
1437 return 1;
1438 }
1439
1440 _cleanup_strv_free_ char **strv = NULL;
1441 for (const char *p = rvalue;;) {
1442 char *word;
1443
1444 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE|EXTRACT_RETAIN_ESCAPE);
1445 if (r < 0)
1446 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
1447 if (r == 0)
1448 break;
1449
1450 r = strv_consume(&strv, word);
1451 if (r < 0)
1452 return log_oom();
1453 }
1454
1455 r = strv_extend_strv_consume(sv, TAKE_PTR(strv), /* filter_duplicates= */ ltype);
1456 if (r < 0)
1457 return log_oom();
1458
1459 return 1;
1460 }
1461
1462 int config_parse_warn_compat(
1463 const char *unit,
1464 const char *filename,
1465 unsigned line,
1466 const char *section,
1467 unsigned section_line,
1468 const char *lvalue,
1469 int ltype,
1470 const char *rvalue,
1471 void *data,
1472 void *userdata) {
1473
1474 Disabled reason = ltype;
1475
1476 switch (reason) {
1477
1478 case DISABLED_CONFIGURATION:
1479 log_syntax(unit, LOG_DEBUG, filename, line, 0,
1480 "Support for option %s= has been disabled at compile time and it is ignored", lvalue);
1481 break;
1482
1483 case DISABLED_LEGACY:
1484 log_syntax(unit, LOG_INFO, filename, line, 0,
1485 "Support for option %s= has been removed and it is ignored", lvalue);
1486 break;
1487
1488 case DISABLED_EXPERIMENTAL:
1489 log_syntax(unit, LOG_INFO, filename, line, 0,
1490 "Support for option %s= has not yet been enabled and it is ignored", lvalue);
1491 break;
1492 }
1493
1494 return 0;
1495 }
1496
1497 int config_parse_log_facility(
1498 const char *unit,
1499 const char *filename,
1500 unsigned line,
1501 const char *section,
1502 unsigned section_line,
1503 const char *lvalue,
1504 int ltype,
1505 const char *rvalue,
1506 void *data,
1507 void *userdata) {
1508
1509 int *o = data, x;
1510
1511 assert(filename);
1512 assert(lvalue);
1513 assert(rvalue);
1514 assert(data);
1515
1516 x = log_facility_unshifted_from_string(rvalue);
1517 if (x < 0)
1518 return log_syntax_parse_error(unit, filename, line, x, lvalue, rvalue);
1519
1520 *o = (x << 3) | LOG_PRI(*o);
1521
1522 return 1;
1523 }
1524
1525 int config_parse_log_level(
1526 const char *unit,
1527 const char *filename,
1528 unsigned line,
1529 const char *section,
1530 unsigned section_line,
1531 const char *lvalue,
1532 int ltype,
1533 const char *rvalue,
1534 void *data,
1535 void *userdata) {
1536
1537 int *o = data, x;
1538
1539 assert(filename);
1540 assert(lvalue);
1541 assert(rvalue);
1542 assert(data);
1543
1544 x = log_level_from_string(rvalue);
1545 if (x < 0)
1546 return log_syntax_parse_error(unit, filename, line, x, lvalue, rvalue);
1547
1548 if (*o < 0) /* if it wasn't initialized so far, assume zero facility */
1549 *o = x;
1550 else
1551 *o = (*o & LOG_FACMASK) | x;
1552
1553 return 1;
1554 }
1555
1556 int config_parse_signal(
1557 const char *unit,
1558 const char *filename,
1559 unsigned line,
1560 const char *section,
1561 unsigned section_line,
1562 const char *lvalue,
1563 int ltype,
1564 const char *rvalue,
1565 void *data,
1566 void *userdata) {
1567
1568 int *sig = data, r;
1569
1570 assert(filename);
1571 assert(lvalue);
1572 assert(rvalue);
1573 assert(sig);
1574
1575 r = signal_from_string(rvalue);
1576 if (r <= 0)
1577 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
1578
1579 *sig = r;
1580 return 1;
1581 }
1582
1583 int config_parse_personality(
1584 const char *unit,
1585 const char *filename,
1586 unsigned line,
1587 const char *section,
1588 unsigned section_line,
1589 const char *lvalue,
1590 int ltype,
1591 const char *rvalue,
1592 void *data,
1593 void *userdata) {
1594
1595 unsigned long *personality = data, p;
1596
1597 assert(filename);
1598 assert(lvalue);
1599 assert(rvalue);
1600 assert(personality);
1601
1602 if (isempty(rvalue)) {
1603 *personality = PERSONALITY_INVALID;
1604 return 1;
1605 }
1606
1607 p = personality_from_string(rvalue);
1608 if (p == PERSONALITY_INVALID)
1609 return log_syntax_parse_error(unit, filename, line, 0, lvalue, rvalue);
1610
1611 *personality = p;
1612 return 1;
1613 }
1614
1615 int config_parse_ifname(
1616 const char *unit,
1617 const char *filename,
1618 unsigned line,
1619 const char *section,
1620 unsigned section_line,
1621 const char *lvalue,
1622 int ltype,
1623 const char *rvalue,
1624 void *data,
1625 void *userdata) {
1626
1627 char **s = ASSERT_PTR(data);
1628 int r;
1629
1630 assert(filename);
1631 assert(lvalue);
1632 assert(rvalue);
1633
1634 if (isempty(rvalue)) {
1635 *s = mfree(*s);
1636 return 1;
1637 }
1638
1639 if (!ifname_valid(rvalue)) {
1640 log_syntax(unit, LOG_WARNING, filename, line, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue);
1641 return 0;
1642 }
1643
1644 r = free_and_strdup_warn(s, rvalue);
1645 if (r < 0)
1646 return r;
1647
1648 return 1;
1649 }
1650
1651 int config_parse_ifnames(
1652 const char *unit,
1653 const char *filename,
1654 unsigned line,
1655 const char *section,
1656 unsigned section_line,
1657 const char *lvalue,
1658 int ltype,
1659 const char *rvalue,
1660 void *data,
1661 void *userdata) {
1662
1663 _cleanup_strv_free_ char **names = NULL;
1664 char ***s = ASSERT_PTR(data);
1665 int r;
1666
1667 assert(filename);
1668 assert(lvalue);
1669 assert(rvalue);
1670
1671 if (isempty(rvalue)) {
1672 *s = strv_free(*s);
1673 return 1;
1674 }
1675
1676 for (const char *p = rvalue;;) {
1677 _cleanup_free_ char *word = NULL;
1678
1679 r = extract_first_word(&p, &word, NULL, 0);
1680 if (r < 0)
1681 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
1682 if (r == 0)
1683 break;
1684
1685 if (!ifname_valid_full(word, ltype)) {
1686 log_syntax(unit, LOG_WARNING, filename, line, 0,
1687 "Interface name is not valid or too long, ignoring assignment: %s",
1688 word);
1689 continue;
1690 }
1691
1692 r = strv_consume(&names, TAKE_PTR(word));
1693 if (r < 0)
1694 return log_oom();
1695 }
1696
1697 r = strv_extend_strv(s, names, true);
1698 if (r < 0)
1699 return log_oom();
1700
1701 return 1;
1702 }
1703
1704 int config_parse_ip_port(
1705 const char *unit,
1706 const char *filename,
1707 unsigned line,
1708 const char *section,
1709 unsigned section_line,
1710 const char *lvalue,
1711 int ltype,
1712 const char *rvalue,
1713 void *data,
1714 void *userdata) {
1715
1716 uint16_t *s = ASSERT_PTR(data);
1717 uint16_t port;
1718 int r;
1719
1720 assert(filename);
1721 assert(lvalue);
1722 assert(rvalue);
1723
1724 if (isempty(rvalue)) {
1725 *s = 0;
1726 return 1;
1727 }
1728
1729 r = parse_ip_port(rvalue, &port);
1730 if (r < 0)
1731 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
1732
1733 *s = port;
1734 return 1;
1735 }
1736
1737 int config_parse_mtu(
1738 const char *unit,
1739 const char *filename,
1740 unsigned line,
1741 const char *section,
1742 unsigned section_line,
1743 const char *lvalue,
1744 int ltype,
1745 const char *rvalue,
1746 void *data,
1747 void *userdata) {
1748
1749 uint32_t *mtu = ASSERT_PTR(data);
1750 int r;
1751
1752 assert(rvalue);
1753
1754 r = parse_mtu(ltype, rvalue, mtu);
1755 if (r == -ERANGE) {
1756 log_syntax(unit, LOG_WARNING, filename, line, r,
1757 "Maximum transfer unit (MTU) value out of range. Permitted range is %" PRIu32 "…%" PRIu32 ", ignoring: %s",
1758 (uint32_t) (ltype == AF_INET6 ? IPV6_MIN_MTU : IPV4_MIN_MTU), (uint32_t) UINT32_MAX,
1759 rvalue);
1760 return 0;
1761 }
1762 if (r < 0)
1763 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
1764
1765 return 1;
1766 }
1767
1768 int config_parse_rlimit(
1769 const char *unit,
1770 const char *filename,
1771 unsigned line,
1772 const char *section,
1773 unsigned section_line,
1774 const char *lvalue,
1775 int ltype,
1776 const char *rvalue,
1777 void *data,
1778 void *userdata) {
1779
1780 struct rlimit **rl = data, d = {};
1781 int r;
1782
1783 assert(rvalue);
1784 assert(rl);
1785
1786 r = rlimit_parse(ltype, rvalue, &d);
1787 if (r == -EILSEQ) {
1788 log_syntax(unit, LOG_WARNING, filename, line, r, "Soft resource limit chosen higher than hard limit, ignoring: %s", rvalue);
1789 return 0;
1790 }
1791 if (r < 0)
1792 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
1793
1794 if (rl[ltype])
1795 *rl[ltype] = d;
1796 else {
1797 rl[ltype] = newdup(struct rlimit, &d, 1);
1798 if (!rl[ltype])
1799 return log_oom();
1800 }
1801
1802 return 1;
1803 }
1804
1805 int config_parse_permille(
1806 const char *unit,
1807 const char *filename,
1808 unsigned line,
1809 const char *section,
1810 unsigned section_line,
1811 const char *lvalue,
1812 int ltype,
1813 const char *rvalue,
1814 void *data,
1815 void *userdata) {
1816
1817 unsigned *permille = ASSERT_PTR(data);
1818 int r;
1819
1820 assert(filename);
1821 assert(lvalue);
1822 assert(rvalue);
1823
1824 r = parse_permille(rvalue);
1825 if (r < 0)
1826 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
1827
1828 *permille = (unsigned) r;
1829 return 1;
1830 }
1831
1832 int config_parse_vlanprotocol(
1833 const char *unit,
1834 const char *filename,
1835 unsigned line,
1836 const char *section,
1837 unsigned section_line,
1838 const char *lvalue,
1839 int ltype,
1840 const char *rvalue,
1841 void *data,
1842 void *userdata) {
1843
1844 int *vlan_protocol = data;
1845
1846 assert(filename);
1847 assert(lvalue);
1848
1849 if (isempty(rvalue)) {
1850 *vlan_protocol = -1;
1851 return 1;
1852 }
1853
1854 if (STR_IN_SET(rvalue, "802.1ad", "802.1AD"))
1855 *vlan_protocol = ETH_P_8021AD;
1856 else if (STR_IN_SET(rvalue, "802.1q", "802.1Q"))
1857 *vlan_protocol = ETH_P_8021Q;
1858 else
1859 return log_syntax_parse_error(unit, filename, line, 0, lvalue, rvalue);
1860
1861 return 1;
1862 }
1863
1864 int config_parse_hw_addr(
1865 const char *unit,
1866 const char *filename,
1867 unsigned line,
1868 const char *section,
1869 unsigned section_line,
1870 const char *lvalue,
1871 int ltype,
1872 const char *rvalue,
1873 void *data,
1874 void *userdata) {
1875
1876 struct hw_addr_data *hwaddr = ASSERT_PTR(data);
1877 int r;
1878
1879 assert(filename);
1880 assert(lvalue);
1881 assert(rvalue);
1882
1883 if (isempty(rvalue)) {
1884 *hwaddr = HW_ADDR_NULL;
1885 return 1;
1886 }
1887
1888 r = parse_hw_addr_full(rvalue, ltype, hwaddr);
1889 if (r < 0)
1890 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
1891
1892 return 1;
1893 }
1894
1895 int config_parse_hw_addrs(
1896 const char *unit,
1897 const char *filename,
1898 unsigned line,
1899 const char *section,
1900 unsigned section_line,
1901 const char *lvalue,
1902 int ltype,
1903 const char *rvalue,
1904 void *data,
1905 void *userdata) {
1906
1907 Set **hwaddrs = ASSERT_PTR(data);
1908 int r;
1909
1910 assert(filename);
1911 assert(lvalue);
1912 assert(rvalue);
1913
1914 if (isempty(rvalue)) {
1915 /* Empty assignment resets the list */
1916 *hwaddrs = set_free(*hwaddrs);
1917 return 1;
1918 }
1919
1920 for (const char *p = rvalue;;) {
1921 _cleanup_free_ char *word = NULL;
1922 _cleanup_free_ struct hw_addr_data *n = NULL;
1923
1924 r = extract_first_word(&p, &word, NULL, 0);
1925 if (r < 0)
1926 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
1927 if (r == 0)
1928 return 1;
1929
1930 n = new(struct hw_addr_data, 1);
1931 if (!n)
1932 return log_oom();
1933
1934 r = parse_hw_addr_full(word, ltype, n);
1935 if (r < 0) {
1936 log_syntax(unit, LOG_WARNING, filename, line, r,
1937 "Not a valid hardware address, ignoring: %s", word);
1938 continue;
1939 }
1940
1941 r = set_ensure_consume(hwaddrs, &hw_addr_hash_ops_free, TAKE_PTR(n));
1942 if (r < 0)
1943 return log_oom();
1944 }
1945 }
1946
1947 int config_parse_ether_addr(
1948 const char *unit,
1949 const char *filename,
1950 unsigned line,
1951 const char *section,
1952 unsigned section_line,
1953 const char *lvalue,
1954 int ltype,
1955 const char *rvalue,
1956 void *data,
1957 void *userdata) {
1958
1959 _cleanup_free_ struct ether_addr *n = NULL;
1960 struct ether_addr **hwaddr = ASSERT_PTR(data);
1961 int r;
1962
1963 assert(filename);
1964 assert(lvalue);
1965 assert(rvalue);
1966
1967 if (isempty(rvalue)) {
1968 *hwaddr = mfree(*hwaddr);
1969 return 1;
1970 }
1971
1972 n = new0(struct ether_addr, 1);
1973 if (!n)
1974 return log_oom();
1975
1976 r = parse_ether_addr(rvalue, n);
1977 if (r < 0)
1978 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
1979
1980 free_and_replace(*hwaddr, n);
1981 return 1;
1982 }
1983
1984 int config_parse_ether_addrs(
1985 const char *unit,
1986 const char *filename,
1987 unsigned line,
1988 const char *section,
1989 unsigned section_line,
1990 const char *lvalue,
1991 int ltype,
1992 const char *rvalue,
1993 void *data,
1994 void *userdata) {
1995
1996 Set **hwaddrs = ASSERT_PTR(data);
1997 int r;
1998
1999 assert(filename);
2000 assert(lvalue);
2001 assert(rvalue);
2002
2003 if (isempty(rvalue)) {
2004 /* Empty assignment resets the list */
2005 *hwaddrs = set_free(*hwaddrs);
2006 return 1;
2007 }
2008
2009 for (const char *p = rvalue;;) {
2010 _cleanup_free_ char *word = NULL;
2011 _cleanup_free_ struct ether_addr *n = NULL;
2012
2013 r = extract_first_word(&p, &word, NULL, 0);
2014 if (r < 0)
2015 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
2016 if (r == 0)
2017 return 1;
2018
2019 n = new(struct ether_addr, 1);
2020 if (!n)
2021 return log_oom();
2022
2023 r = parse_ether_addr(word, n);
2024 if (r < 0) {
2025 log_syntax(unit, LOG_WARNING, filename, line, r,
2026 "Not a valid MAC address, ignoring: %s", word);
2027 continue;
2028 }
2029
2030 r = set_ensure_consume(hwaddrs, &ether_addr_hash_ops_free, TAKE_PTR(n));
2031 if (r < 0)
2032 return log_oom();
2033 }
2034 }
2035
2036 int config_parse_in_addr_non_null(
2037 const char *unit,
2038 const char *filename,
2039 unsigned line,
2040 const char *section,
2041 unsigned section_line,
2042 const char *lvalue,
2043 int ltype,
2044 const char *rvalue,
2045 void *data,
2046 void *userdata) {
2047
2048 /* data must be a pointer to struct in_addr or in6_addr, and the type is determined by ltype. */
2049 struct in_addr *ipv4 = ASSERT_PTR(data);
2050 struct in6_addr *ipv6 = ASSERT_PTR(data);
2051 union in_addr_union a;
2052 int r;
2053
2054 assert(filename);
2055 assert(lvalue);
2056 assert(rvalue);
2057 assert(IN_SET(ltype, AF_INET, AF_INET6));
2058
2059 if (isempty(rvalue)) {
2060 if (ltype == AF_INET)
2061 *ipv4 = (struct in_addr) {};
2062 else
2063 *ipv6 = (struct in6_addr) {};
2064 return 1;
2065 }
2066
2067 r = in_addr_from_string(ltype, rvalue, &a);
2068 if (r < 0)
2069 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
2070
2071 if (!in_addr_is_set(ltype, &a)) {
2072 log_syntax(unit, LOG_WARNING, filename, line, 0,
2073 "%s= cannot be the ANY address, ignoring: %s", lvalue, rvalue);
2074 return 0;
2075 }
2076
2077 if (ltype == AF_INET)
2078 *ipv4 = a.in;
2079 else
2080 *ipv6 = a.in6;
2081 return 1;
2082 }
2083
2084 int config_parse_in_addr_data(
2085 const char *unit,
2086 const char *filename,
2087 unsigned line,
2088 const char *section,
2089 unsigned section_line,
2090 const char *lvalue,
2091 int ltype,
2092 const char *rvalue,
2093 void *data,
2094 void *userdata) {
2095
2096 struct in_addr_data *p = ASSERT_PTR(data);
2097 int r;
2098
2099 assert(filename);
2100 assert(lvalue);
2101
2102 if (isempty(rvalue)) {
2103 *p = (struct in_addr_data) {};
2104 return 1;
2105 }
2106
2107 r = in_addr_from_string_auto(rvalue, &p->family, &p->address);
2108 if (r < 0)
2109 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
2110
2111 return 1;
2112 }
2113
2114 int config_parse_in_addr_prefix(
2115 const char *unit,
2116 const char *filename,
2117 unsigned line,
2118 const char *section,
2119 unsigned section_line,
2120 const char *lvalue,
2121 int ltype, /* takes boolean, whether we warn about missing prefixlen */
2122 const char *rvalue,
2123 void *data,
2124 void *userdata) {
2125
2126 struct in_addr_prefix *p = ASSERT_PTR(data);
2127 int r;
2128
2129 assert(filename);
2130 assert(lvalue);
2131
2132 if (isempty(rvalue)) {
2133 *p = (struct in_addr_prefix) {};
2134 return 1;
2135 }
2136
2137 r = in_addr_prefix_from_string_auto_full(rvalue, ltype ? PREFIXLEN_REFUSE : PREFIXLEN_FULL, &p->family, &p->address, &p->prefixlen);
2138 if (r == -ENOANO) {
2139 r = in_addr_prefix_from_string_auto(rvalue, &p->family, &p->address, &p->prefixlen);
2140 if (r >= 0)
2141 log_syntax(unit, LOG_WARNING, filename, line, r,
2142 "%s=%s is specified without prefix length. Assuming the prefix length is %u. "
2143 "Please specify the prefix length explicitly.", lvalue, rvalue, p->prefixlen);
2144 }
2145 if (r < 0)
2146 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
2147
2148 return 1;
2149 }
2150
2151 int config_parse_unsigned_bounded(
2152 const char *unit,
2153 const char *filename,
2154 unsigned line,
2155 const char *section,
2156 unsigned section_line,
2157 const char *lvalue,
2158 const char *rvalue,
2159 unsigned min,
2160 unsigned max,
2161 bool ignoring,
2162 unsigned *ret) {
2163
2164 int r;
2165
2166 assert(filename);
2167 assert(lvalue);
2168 assert(rvalue);
2169 assert(ret);
2170
2171 r = safe_atou_bounded(rvalue, min, max, ret);
2172 if (r == -ERANGE) {
2173 log_syntax(unit, LOG_WARNING, filename, line, r,
2174 "Invalid '%s=%s', allowed range is %u..%u%s.",
2175 lvalue, rvalue, min, max, ignoring ? ", ignoring" : "");
2176 return ignoring ? 0 : r;
2177 }
2178 if (r < 0)
2179 return log_syntax_parse_error_full(unit, filename, line, r, /* critical= */ !ignoring, lvalue, rvalue);
2180
2181 return 1; /* Return 1 if something was set */
2182 }
2183
2184 int config_parse_calendar(
2185 const char *unit,
2186 const char *filename,
2187 unsigned line,
2188 const char *section,
2189 unsigned section_line,
2190 const char *lvalue,
2191 int ltype,
2192 const char *rvalue,
2193 void *data,
2194 void *userdata) {
2195
2196 CalendarSpec **cr = data;
2197 _cleanup_(calendar_spec_freep) CalendarSpec *c = NULL;
2198 int r;
2199
2200 assert(filename);
2201 assert(lvalue);
2202 assert(rvalue);
2203 assert(data);
2204
2205 if (isempty(rvalue)) {
2206 *cr = calendar_spec_free(*cr);
2207 return 1;
2208 }
2209
2210 r = calendar_spec_from_string(rvalue, &c);
2211 if (r < 0)
2212 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
2213
2214 free_and_replace_full(*cr, c, calendar_spec_free);
2215 return 1;
2216 }
2217
2218 DEFINE_CONFIG_PARSE(config_parse_percent, parse_percent);
2219 DEFINE_CONFIG_PARSE(config_parse_permyriad, parse_permyriad);
2220 DEFINE_CONFIG_PARSE_PTR(config_parse_sec_fix_0, parse_sec_fix_0, usec_t);
2221
2222 int config_parse_timezone(
2223 const char *unit,
2224 const char *filename,
2225 unsigned line,
2226 const char *section,
2227 unsigned section_line,
2228 const char *lvalue,
2229 int ltype,
2230 const char *rvalue,
2231 void *data,
2232 void *userdata) {
2233
2234 char **tz = ASSERT_PTR(data);
2235 int r;
2236
2237 assert(filename);
2238 assert(lvalue);
2239 assert(rvalue);
2240
2241 if (isempty(rvalue)) {
2242 *tz = mfree(*tz);
2243 return 1;
2244 }
2245
2246 r = verify_timezone(rvalue, LOG_WARNING);
2247 if (r < 0)
2248 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
2249
2250 r = free_and_strdup_warn(tz, rvalue);
2251 if (r < 0)
2252 return r;
2253
2254 return 1;
2255 }
2256
2257 int config_parse_ip_protocol(
2258 const char *unit,
2259 const char *filename,
2260 unsigned line,
2261 const char *section,
2262 unsigned section_line,
2263 const char *lvalue,
2264 int ltype,
2265 const char *rvalue,
2266 void *data,
2267 void *userdata) {
2268
2269 uint8_t *proto = ASSERT_PTR(data);
2270 int r;
2271
2272 r = isempty(rvalue) ? 0 : parse_ip_protocol_full(rvalue, /* relaxed= */ ltype);
2273 if (r < 0)
2274 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
2275
2276 if (r > UINT8_MAX) {
2277 /* linux/fib_rules.h and linux/fou.h define the netlink field as one byte, so we need to
2278 * reject protocols numbers that don't fit in one byte. */
2279 log_syntax(unit, LOG_WARNING, filename, line, r,
2280 "Invalid '%s=%s', allowed range is 0..255, ignoring.",
2281 lvalue, rvalue);
2282 return 0;
2283 }
2284
2285 *proto = r;
2286 return 1; /* done. */
2287 }
2288
2289 int config_parse_loadavg(
2290 const char *unit,
2291 const char *filename,
2292 unsigned line,
2293 const char *section,
2294 unsigned section_line,
2295 const char *lvalue,
2296 int ltype,
2297 const char *rvalue,
2298 void *data,
2299 void *userdata) {
2300
2301 loadavg_t *i = ASSERT_PTR(data);
2302 int r;
2303
2304 assert(rvalue);
2305
2306 r = parse_permyriad(rvalue);
2307 if (r < 0)
2308 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
2309
2310 r = store_loadavg_fixed_point(r / 100, r % 100, i);
2311 if (r < 0)
2312 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
2313
2314 return 1; /* done. */
2315 }