]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/conf-parser.c
tree-wide: rename config_parse_many to …_nulstr
[thirdparty/systemd.git] / src / shared / conf-parser.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <limits.h>
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/types.h>
27
28 #include "alloc-util.h"
29 #include "conf-files.h"
30 #include "conf-parser.h"
31 #include "extract-word.h"
32 #include "fd-util.h"
33 #include "fs-util.h"
34 #include "log.h"
35 #include "macro.h"
36 #include "parse-util.h"
37 #include "path-util.h"
38 #include "process-util.h"
39 #include "signal-util.h"
40 #include "socket-util.h"
41 #include "string-util.h"
42 #include "strv.h"
43 #include "syslog-util.h"
44 #include "time-util.h"
45 #include "utf8.h"
46
47 int config_item_table_lookup(
48 const void *table,
49 const char *section,
50 const char *lvalue,
51 ConfigParserCallback *func,
52 int *ltype,
53 void **data,
54 void *userdata) {
55
56 const ConfigTableItem *t;
57
58 assert(table);
59 assert(lvalue);
60 assert(func);
61 assert(ltype);
62 assert(data);
63
64 for (t = table; t->lvalue; t++) {
65
66 if (!streq(lvalue, t->lvalue))
67 continue;
68
69 if (!streq_ptr(section, t->section))
70 continue;
71
72 *func = t->parse;
73 *ltype = t->ltype;
74 *data = t->data;
75 return 1;
76 }
77
78 return 0;
79 }
80
81 int config_item_perf_lookup(
82 const void *table,
83 const char *section,
84 const char *lvalue,
85 ConfigParserCallback *func,
86 int *ltype,
87 void **data,
88 void *userdata) {
89
90 ConfigPerfItemLookup lookup = (ConfigPerfItemLookup) table;
91 const ConfigPerfItem *p;
92
93 assert(table);
94 assert(lvalue);
95 assert(func);
96 assert(ltype);
97 assert(data);
98
99 if (!section)
100 p = lookup(lvalue, strlen(lvalue));
101 else {
102 char *key;
103
104 key = strjoin(section, ".", lvalue, NULL);
105 if (!key)
106 return -ENOMEM;
107
108 p = lookup(key, strlen(key));
109 free(key);
110 }
111
112 if (!p)
113 return 0;
114
115 *func = p->parse;
116 *ltype = p->ltype;
117 *data = (uint8_t*) userdata + p->offset;
118 return 1;
119 }
120
121 /* Run the user supplied parser for an assignment */
122 static int next_assignment(const char *unit,
123 const char *filename,
124 unsigned line,
125 ConfigItemLookup lookup,
126 const void *table,
127 const char *section,
128 unsigned section_line,
129 const char *lvalue,
130 const char *rvalue,
131 bool relaxed,
132 void *userdata) {
133
134 ConfigParserCallback func = NULL;
135 int ltype = 0;
136 void *data = NULL;
137 int r;
138
139 assert(filename);
140 assert(line > 0);
141 assert(lookup);
142 assert(lvalue);
143 assert(rvalue);
144
145 r = lookup(table, section, lvalue, &func, &ltype, &data, userdata);
146 if (r < 0)
147 return r;
148
149 if (r > 0) {
150 if (func)
151 return func(unit, filename, line, section, section_line,
152 lvalue, ltype, rvalue, data, userdata);
153
154 return 0;
155 }
156
157 /* Warn about unknown non-extension fields. */
158 if (!relaxed && !startswith(lvalue, "X-"))
159 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown lvalue '%s' in section '%s'", lvalue, section);
160
161 return 0;
162 }
163
164 /* Parse a variable assignment line */
165 static int parse_line(const char* unit,
166 const char *filename,
167 unsigned line,
168 const char *sections,
169 ConfigItemLookup lookup,
170 const void *table,
171 bool relaxed,
172 bool allow_include,
173 char **section,
174 unsigned *section_line,
175 bool *section_ignored,
176 char *l,
177 void *userdata) {
178
179 char *e;
180
181 assert(filename);
182 assert(line > 0);
183 assert(lookup);
184 assert(l);
185
186 l = strstrip(l);
187
188 if (!*l)
189 return 0;
190
191 if (strchr(COMMENTS "\n", *l))
192 return 0;
193
194 if (startswith(l, ".include ")) {
195 _cleanup_free_ char *fn = NULL;
196
197 /* .includes are a bad idea, we only support them here
198 * for historical reasons. They create cyclic include
199 * problems and make it difficult to detect
200 * configuration file changes with an easy
201 * stat(). Better approaches, such as .d/ drop-in
202 * snippets exist.
203 *
204 * Support for them should be eventually removed. */
205
206 if (!allow_include) {
207 log_syntax(unit, LOG_ERR, filename, line, 0, ".include not allowed here. Ignoring.");
208 return 0;
209 }
210
211 fn = file_in_same_dir(filename, strstrip(l+9));
212 if (!fn)
213 return -ENOMEM;
214
215 return config_parse(unit, fn, NULL, sections, lookup, table, relaxed, false, false, userdata);
216 }
217
218 if (*l == '[') {
219 size_t k;
220 char *n;
221
222 k = strlen(l);
223 assert(k > 0);
224
225 if (l[k-1] != ']') {
226 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid section header '%s'", l);
227 return -EBADMSG;
228 }
229
230 n = strndup(l+1, k-2);
231 if (!n)
232 return -ENOMEM;
233
234 if (sections && !nulstr_contains(sections, n)) {
235
236 if (!relaxed && !startswith(n, "X-"))
237 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown section '%s'. Ignoring.", n);
238
239 free(n);
240 *section = mfree(*section);
241 *section_line = 0;
242 *section_ignored = true;
243 } else {
244 free(*section);
245 *section = n;
246 *section_line = line;
247 *section_ignored = false;
248 }
249
250 return 0;
251 }
252
253 if (sections && !*section) {
254
255 if (!relaxed && !*section_ignored)
256 log_syntax(unit, LOG_WARNING, filename, line, 0, "Assignment outside of section. Ignoring.");
257
258 return 0;
259 }
260
261 e = strchr(l, '=');
262 if (!e) {
263 log_syntax(unit, LOG_WARNING, filename, line, 0, "Missing '='.");
264 return -EINVAL;
265 }
266
267 *e = 0;
268 e++;
269
270 return next_assignment(unit,
271 filename,
272 line,
273 lookup,
274 table,
275 *section,
276 *section_line,
277 strstrip(l),
278 strstrip(e),
279 relaxed,
280 userdata);
281 }
282
283 /* Go through the file and parse each line */
284 int config_parse(const char *unit,
285 const char *filename,
286 FILE *f,
287 const char *sections,
288 ConfigItemLookup lookup,
289 const void *table,
290 bool relaxed,
291 bool allow_include,
292 bool warn,
293 void *userdata) {
294
295 _cleanup_free_ char *section = NULL, *continuation = NULL;
296 _cleanup_fclose_ FILE *ours = NULL;
297 unsigned line = 0, section_line = 0;
298 bool section_ignored = false, allow_bom = true;
299 int r;
300
301 assert(filename);
302 assert(lookup);
303
304 if (!f) {
305 f = ours = fopen(filename, "re");
306 if (!f) {
307 /* Only log on request, except for ENOENT,
308 * since we return 0 to the caller. */
309 if (warn || errno == ENOENT)
310 log_full(errno == ENOENT ? LOG_DEBUG : LOG_ERR,
311 "Failed to open configuration file '%s': %m", filename);
312 return errno == ENOENT ? 0 : -errno;
313 }
314 }
315
316 fd_warn_permissions(filename, fileno(f));
317
318 for (;;) {
319 char buf[LINE_MAX], *l, *p, *c = NULL, *e;
320 bool escaped = false;
321
322 if (!fgets(buf, sizeof buf, f)) {
323 if (feof(f))
324 break;
325
326 return log_error_errno(errno, "Failed to read configuration file '%s': %m", filename);
327 }
328
329 l = buf;
330 if (allow_bom && startswith(l, UTF8_BYTE_ORDER_MARK))
331 l += strlen(UTF8_BYTE_ORDER_MARK);
332 allow_bom = false;
333
334 truncate_nl(l);
335
336 if (continuation) {
337 c = strappend(continuation, l);
338 if (!c) {
339 if (warn)
340 log_oom();
341 return -ENOMEM;
342 }
343
344 continuation = mfree(continuation);
345 p = c;
346 } else
347 p = l;
348
349 for (e = p; *e; e++) {
350 if (escaped)
351 escaped = false;
352 else if (*e == '\\')
353 escaped = true;
354 }
355
356 if (escaped) {
357 *(e-1) = ' ';
358
359 if (c)
360 continuation = c;
361 else {
362 continuation = strdup(l);
363 if (!continuation) {
364 if (warn)
365 log_oom();
366 return -ENOMEM;
367 }
368 }
369
370 continue;
371 }
372
373 r = parse_line(unit,
374 filename,
375 ++line,
376 sections,
377 lookup,
378 table,
379 relaxed,
380 allow_include,
381 &section,
382 &section_line,
383 &section_ignored,
384 p,
385 userdata);
386 free(c);
387
388 if (r < 0) {
389 if (warn)
390 log_warning_errno(r, "Failed to parse file '%s': %m",
391 filename);
392 return r;
393 }
394 }
395
396 return 0;
397 }
398
399 /* Parse each config file in the specified directories. */
400 int config_parse_many_nulstr(
401 const char *conf_file,
402 const char *conf_file_dirs,
403 const char *sections,
404 ConfigItemLookup lookup,
405 const void *table,
406 bool relaxed,
407 void *userdata) {
408
409 _cleanup_strv_free_ char **files = NULL;
410 char **fn;
411 int r;
412
413 r = conf_files_list_nulstr(&files, ".conf", NULL, conf_file_dirs);
414 if (r < 0)
415 return r;
416
417 if (conf_file) {
418 r = config_parse(NULL, conf_file, NULL, sections, lookup, table, relaxed, false, true, userdata);
419 if (r < 0)
420 return r;
421 }
422
423 STRV_FOREACH(fn, files) {
424 r = config_parse(NULL, *fn, NULL, sections, lookup, table, relaxed, false, true, userdata);
425 if (r < 0)
426 return r;
427 }
428
429 return 0;
430 }
431
432 #define DEFINE_PARSER(type, vartype, conv_func) \
433 int config_parse_##type( \
434 const char *unit, \
435 const char *filename, \
436 unsigned line, \
437 const char *section, \
438 unsigned section_line, \
439 const char *lvalue, \
440 int ltype, \
441 const char *rvalue, \
442 void *data, \
443 void *userdata) { \
444 \
445 vartype *i = data; \
446 int r; \
447 \
448 assert(filename); \
449 assert(lvalue); \
450 assert(rvalue); \
451 assert(data); \
452 \
453 r = conv_func(rvalue, i); \
454 if (r < 0) \
455 log_syntax(unit, LOG_ERR, filename, line, r, \
456 "Failed to parse %s value, ignoring: %s", \
457 #type, rvalue); \
458 \
459 return 0; \
460 } \
461 struct __useless_struct_to_allow_trailing_semicolon__
462
463 DEFINE_PARSER(int, int, safe_atoi);
464 DEFINE_PARSER(long, long, safe_atoli);
465 DEFINE_PARSER(uint16, uint16_t, safe_atou16);
466 DEFINE_PARSER(uint32, uint32_t, safe_atou32);
467 DEFINE_PARSER(uint64, uint64_t, safe_atou64);
468 DEFINE_PARSER(unsigned, unsigned, safe_atou);
469 DEFINE_PARSER(double, double, safe_atod);
470 DEFINE_PARSER(nsec, nsec_t, parse_nsec);
471 DEFINE_PARSER(sec, usec_t, parse_sec);
472 DEFINE_PARSER(mode, mode_t, parse_mode);
473
474 int config_parse_iec_size(const char* unit,
475 const char *filename,
476 unsigned line,
477 const char *section,
478 unsigned section_line,
479 const char *lvalue,
480 int ltype,
481 const char *rvalue,
482 void *data,
483 void *userdata) {
484
485 size_t *sz = data;
486 uint64_t v;
487 int r;
488
489 assert(filename);
490 assert(lvalue);
491 assert(rvalue);
492 assert(data);
493
494 r = parse_size(rvalue, 1024, &v);
495 if (r < 0 || (uint64_t) (size_t) v != v) {
496 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
497 return 0;
498 }
499
500 *sz = (size_t) v;
501 return 0;
502 }
503
504 int config_parse_si_size(const char* unit,
505 const char *filename,
506 unsigned line,
507 const char *section,
508 unsigned section_line,
509 const char *lvalue,
510 int ltype,
511 const char *rvalue,
512 void *data,
513 void *userdata) {
514
515 size_t *sz = data;
516 uint64_t v;
517 int r;
518
519 assert(filename);
520 assert(lvalue);
521 assert(rvalue);
522 assert(data);
523
524 r = parse_size(rvalue, 1000, &v);
525 if (r < 0 || (uint64_t) (size_t) v != v) {
526 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
527 return 0;
528 }
529
530 *sz = (size_t) v;
531 return 0;
532 }
533
534 int config_parse_iec_uint64(const char* unit,
535 const char *filename,
536 unsigned line,
537 const char *section,
538 unsigned section_line,
539 const char *lvalue,
540 int ltype,
541 const char *rvalue,
542 void *data,
543 void *userdata) {
544
545 uint64_t *bytes = data;
546 int r;
547
548 assert(filename);
549 assert(lvalue);
550 assert(rvalue);
551 assert(data);
552
553 r = parse_size(rvalue, 1024, bytes);
554 if (r < 0)
555 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
556
557 return 0;
558 }
559
560 int config_parse_bool(const char* unit,
561 const char *filename,
562 unsigned line,
563 const char *section,
564 unsigned section_line,
565 const char *lvalue,
566 int ltype,
567 const char *rvalue,
568 void *data,
569 void *userdata) {
570
571 int k;
572 bool *b = data;
573
574 assert(filename);
575 assert(lvalue);
576 assert(rvalue);
577 assert(data);
578
579 k = parse_boolean(rvalue);
580 if (k < 0) {
581 log_syntax(unit, LOG_ERR, filename, line, k, "Failed to parse boolean value, ignoring: %s", rvalue);
582 return 0;
583 }
584
585 *b = !!k;
586 return 0;
587 }
588
589 int config_parse_tristate(
590 const char* unit,
591 const char *filename,
592 unsigned line,
593 const char *section,
594 unsigned section_line,
595 const char *lvalue,
596 int ltype,
597 const char *rvalue,
598 void *data,
599 void *userdata) {
600
601 int k, *t = data;
602
603 assert(filename);
604 assert(lvalue);
605 assert(rvalue);
606 assert(data);
607
608 /* A tristate is pretty much a boolean, except that it can
609 * also take the special value -1, indicating "uninitialized",
610 * much like NULL is for a pointer type. */
611
612 k = parse_boolean(rvalue);
613 if (k < 0) {
614 log_syntax(unit, LOG_ERR, filename, line, k, "Failed to parse boolean value, ignoring: %s", rvalue);
615 return 0;
616 }
617
618 *t = !!k;
619 return 0;
620 }
621
622 int config_parse_string(
623 const char *unit,
624 const char *filename,
625 unsigned line,
626 const char *section,
627 unsigned section_line,
628 const char *lvalue,
629 int ltype,
630 const char *rvalue,
631 void *data,
632 void *userdata) {
633
634 char **s = data, *n;
635
636 assert(filename);
637 assert(lvalue);
638 assert(rvalue);
639 assert(data);
640
641 if (!utf8_is_valid(rvalue)) {
642 log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, rvalue);
643 return 0;
644 }
645
646 if (isempty(rvalue))
647 n = NULL;
648 else {
649 n = strdup(rvalue);
650 if (!n)
651 return log_oom();
652 }
653
654 free(*s);
655 *s = n;
656
657 return 0;
658 }
659
660 int config_parse_path(
661 const char *unit,
662 const char *filename,
663 unsigned line,
664 const char *section,
665 unsigned section_line,
666 const char *lvalue,
667 int ltype,
668 const char *rvalue,
669 void *data,
670 void *userdata) {
671
672 char **s = data, *n;
673
674 assert(filename);
675 assert(lvalue);
676 assert(rvalue);
677 assert(data);
678
679 if (!utf8_is_valid(rvalue)) {
680 log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, rvalue);
681 return 0;
682 }
683
684 if (!path_is_absolute(rvalue)) {
685 log_syntax(unit, LOG_ERR, filename, line, 0, "Not an absolute path, ignoring: %s", rvalue);
686 return 0;
687 }
688
689 n = strdup(rvalue);
690 if (!n)
691 return log_oom();
692
693 path_kill_slashes(n);
694
695 free(*s);
696 *s = n;
697
698 return 0;
699 }
700
701 int config_parse_strv(const char *unit,
702 const char *filename,
703 unsigned line,
704 const char *section,
705 unsigned section_line,
706 const char *lvalue,
707 int ltype,
708 const char *rvalue,
709 void *data,
710 void *userdata) {
711
712 char ***sv = data;
713 int r;
714
715 assert(filename);
716 assert(lvalue);
717 assert(rvalue);
718 assert(data);
719
720 if (isempty(rvalue)) {
721 char **empty;
722
723 /* Empty assignment resets the list. As a special rule
724 * we actually fill in a real empty array here rather
725 * than NULL, since some code wants to know if
726 * something was set at all... */
727 empty = new0(char*, 1);
728 if (!empty)
729 return log_oom();
730
731 strv_free(*sv);
732 *sv = empty;
733
734 return 0;
735 }
736
737 for (;;) {
738 char *word = NULL;
739
740 r = extract_first_word(&rvalue, &word, WHITESPACE, EXTRACT_QUOTES|EXTRACT_RETAIN_ESCAPE);
741 if (r == 0)
742 break;
743 if (r == -ENOMEM)
744 return log_oom();
745 if (r < 0) {
746 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
747 break;
748 }
749
750 if (!utf8_is_valid(word)) {
751 log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, rvalue);
752 free(word);
753 continue;
754 }
755 r = strv_consume(sv, word);
756 if (r < 0)
757 return log_oom();
758 }
759
760 return 0;
761 }
762
763 int config_parse_log_facility(
764 const char *unit,
765 const char *filename,
766 unsigned line,
767 const char *section,
768 unsigned section_line,
769 const char *lvalue,
770 int ltype,
771 const char *rvalue,
772 void *data,
773 void *userdata) {
774
775
776 int *o = data, x;
777
778 assert(filename);
779 assert(lvalue);
780 assert(rvalue);
781 assert(data);
782
783 x = log_facility_unshifted_from_string(rvalue);
784 if (x < 0) {
785 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse log facility, ignoring: %s", rvalue);
786 return 0;
787 }
788
789 *o = (x << 3) | LOG_PRI(*o);
790
791 return 0;
792 }
793
794 int config_parse_log_level(
795 const char *unit,
796 const char *filename,
797 unsigned line,
798 const char *section,
799 unsigned section_line,
800 const char *lvalue,
801 int ltype,
802 const char *rvalue,
803 void *data,
804 void *userdata) {
805
806
807 int *o = data, x;
808
809 assert(filename);
810 assert(lvalue);
811 assert(rvalue);
812 assert(data);
813
814 x = log_level_from_string(rvalue);
815 if (x < 0) {
816 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse log level, ignoring: %s", rvalue);
817 return 0;
818 }
819
820 *o = (*o & LOG_FACMASK) | x;
821 return 0;
822 }
823
824 int config_parse_signal(
825 const char *unit,
826 const char *filename,
827 unsigned line,
828 const char *section,
829 unsigned section_line,
830 const char *lvalue,
831 int ltype,
832 const char *rvalue,
833 void *data,
834 void *userdata) {
835
836 int *sig = data, r;
837
838 assert(filename);
839 assert(lvalue);
840 assert(rvalue);
841 assert(sig);
842
843 r = signal_from_string_try_harder(rvalue);
844 if (r <= 0) {
845 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse signal name, ignoring: %s", rvalue);
846 return 0;
847 }
848
849 *sig = r;
850 return 0;
851 }
852
853 int config_parse_personality(
854 const char *unit,
855 const char *filename,
856 unsigned line,
857 const char *section,
858 unsigned section_line,
859 const char *lvalue,
860 int ltype,
861 const char *rvalue,
862 void *data,
863 void *userdata) {
864
865 unsigned long *personality = data, p;
866
867 assert(filename);
868 assert(lvalue);
869 assert(rvalue);
870 assert(personality);
871
872 p = personality_from_string(rvalue);
873 if (p == PERSONALITY_INVALID) {
874 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse personality, ignoring: %s", rvalue);
875 return 0;
876 }
877
878 *personality = p;
879 return 0;
880 }
881
882 int config_parse_ifname(
883 const char *unit,
884 const char *filename,
885 unsigned line,
886 const char *section,
887 unsigned section_line,
888 const char *lvalue,
889 int ltype,
890 const char *rvalue,
891 void *data,
892 void *userdata) {
893
894 char **s = data;
895 int r;
896
897 assert(filename);
898 assert(lvalue);
899 assert(rvalue);
900 assert(data);
901
902 if (isempty(rvalue)) {
903 *s = mfree(*s);
904 return 0;
905 }
906
907 if (!ifname_valid(rvalue)) {
908 log_syntax(unit, LOG_ERR, filename, line, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue);
909 return 0;
910 }
911
912 r = free_and_strdup(s, rvalue);
913 if (r < 0)
914 return log_oom();
915
916 return 0;
917 }