]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/conf-parser.c
Use "return log_error_errno" in more places"
[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(const char *conf_file,
401 const char *conf_file_dirs,
402 const char *sections,
403 ConfigItemLookup lookup,
404 const void *table,
405 bool relaxed,
406 void *userdata) {
407 _cleanup_strv_free_ char **files = NULL;
408 char **fn;
409 int r;
410
411 r = conf_files_list_nulstr(&files, ".conf", NULL, conf_file_dirs);
412 if (r < 0)
413 return r;
414
415 if (conf_file) {
416 r = config_parse(NULL, conf_file, NULL, sections, lookup, table, relaxed, false, true, userdata);
417 if (r < 0)
418 return r;
419 }
420
421 STRV_FOREACH(fn, files) {
422 r = config_parse(NULL, *fn, NULL, sections, lookup, table, relaxed, false, true, userdata);
423 if (r < 0)
424 return r;
425 }
426
427 return 0;
428 }
429
430 #define DEFINE_PARSER(type, vartype, conv_func) \
431 int config_parse_##type( \
432 const char *unit, \
433 const char *filename, \
434 unsigned line, \
435 const char *section, \
436 unsigned section_line, \
437 const char *lvalue, \
438 int ltype, \
439 const char *rvalue, \
440 void *data, \
441 void *userdata) { \
442 \
443 vartype *i = data; \
444 int r; \
445 \
446 assert(filename); \
447 assert(lvalue); \
448 assert(rvalue); \
449 assert(data); \
450 \
451 r = conv_func(rvalue, i); \
452 if (r < 0) \
453 log_syntax(unit, LOG_ERR, filename, line, r, \
454 "Failed to parse %s value, ignoring: %s", \
455 #type, rvalue); \
456 \
457 return 0; \
458 } \
459 struct __useless_struct_to_allow_trailing_semicolon__
460
461 DEFINE_PARSER(int, int, safe_atoi);
462 DEFINE_PARSER(long, long, safe_atoli);
463 DEFINE_PARSER(uint32, uint32_t, safe_atou32);
464 DEFINE_PARSER(uint64, uint64_t, safe_atou64);
465 DEFINE_PARSER(unsigned, unsigned, safe_atou);
466 DEFINE_PARSER(double, double, safe_atod);
467 DEFINE_PARSER(nsec, nsec_t, parse_nsec);
468 DEFINE_PARSER(sec, usec_t, parse_sec);
469 DEFINE_PARSER(mode, mode_t, parse_mode);
470
471 int config_parse_iec_size(const char* unit,
472 const char *filename,
473 unsigned line,
474 const char *section,
475 unsigned section_line,
476 const char *lvalue,
477 int ltype,
478 const char *rvalue,
479 void *data,
480 void *userdata) {
481
482 size_t *sz = data;
483 uint64_t v;
484 int r;
485
486 assert(filename);
487 assert(lvalue);
488 assert(rvalue);
489 assert(data);
490
491 r = parse_size(rvalue, 1024, &v);
492 if (r < 0 || (uint64_t) (size_t) v != v) {
493 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
494 return 0;
495 }
496
497 *sz = (size_t) v;
498 return 0;
499 }
500
501 int config_parse_si_size(const char* unit,
502 const char *filename,
503 unsigned line,
504 const char *section,
505 unsigned section_line,
506 const char *lvalue,
507 int ltype,
508 const char *rvalue,
509 void *data,
510 void *userdata) {
511
512 size_t *sz = data;
513 uint64_t v;
514 int r;
515
516 assert(filename);
517 assert(lvalue);
518 assert(rvalue);
519 assert(data);
520
521 r = parse_size(rvalue, 1000, &v);
522 if (r < 0 || (uint64_t) (size_t) v != v) {
523 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
524 return 0;
525 }
526
527 *sz = (size_t) v;
528 return 0;
529 }
530
531 int config_parse_iec_uint64(const char* unit,
532 const char *filename,
533 unsigned line,
534 const char *section,
535 unsigned section_line,
536 const char *lvalue,
537 int ltype,
538 const char *rvalue,
539 void *data,
540 void *userdata) {
541
542 uint64_t *bytes = data;
543 int r;
544
545 assert(filename);
546 assert(lvalue);
547 assert(rvalue);
548 assert(data);
549
550 r = parse_size(rvalue, 1024, bytes);
551 if (r < 0)
552 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
553
554 return 0;
555 }
556
557 int config_parse_bool(const char* unit,
558 const char *filename,
559 unsigned line,
560 const char *section,
561 unsigned section_line,
562 const char *lvalue,
563 int ltype,
564 const char *rvalue,
565 void *data,
566 void *userdata) {
567
568 int k;
569 bool *b = data;
570
571 assert(filename);
572 assert(lvalue);
573 assert(rvalue);
574 assert(data);
575
576 k = parse_boolean(rvalue);
577 if (k < 0) {
578 log_syntax(unit, LOG_ERR, filename, line, k, "Failed to parse boolean value, ignoring: %s", rvalue);
579 return 0;
580 }
581
582 *b = !!k;
583 return 0;
584 }
585
586 int config_parse_tristate(
587 const char* unit,
588 const char *filename,
589 unsigned line,
590 const char *section,
591 unsigned section_line,
592 const char *lvalue,
593 int ltype,
594 const char *rvalue,
595 void *data,
596 void *userdata) {
597
598 int k, *t = data;
599
600 assert(filename);
601 assert(lvalue);
602 assert(rvalue);
603 assert(data);
604
605 /* A tristate is pretty much a boolean, except that it can
606 * also take the special value -1, indicating "uninitialized",
607 * much like NULL is for a pointer type. */
608
609 k = parse_boolean(rvalue);
610 if (k < 0) {
611 log_syntax(unit, LOG_ERR, filename, line, k, "Failed to parse boolean value, ignoring: %s", rvalue);
612 return 0;
613 }
614
615 *t = !!k;
616 return 0;
617 }
618
619 int config_parse_string(
620 const char *unit,
621 const char *filename,
622 unsigned line,
623 const char *section,
624 unsigned section_line,
625 const char *lvalue,
626 int ltype,
627 const char *rvalue,
628 void *data,
629 void *userdata) {
630
631 char **s = data, *n;
632
633 assert(filename);
634 assert(lvalue);
635 assert(rvalue);
636 assert(data);
637
638 if (!utf8_is_valid(rvalue)) {
639 log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, rvalue);
640 return 0;
641 }
642
643 if (isempty(rvalue))
644 n = NULL;
645 else {
646 n = strdup(rvalue);
647 if (!n)
648 return log_oom();
649 }
650
651 free(*s);
652 *s = n;
653
654 return 0;
655 }
656
657 int config_parse_path(
658 const char *unit,
659 const char *filename,
660 unsigned line,
661 const char *section,
662 unsigned section_line,
663 const char *lvalue,
664 int ltype,
665 const char *rvalue,
666 void *data,
667 void *userdata) {
668
669 char **s = data, *n;
670
671 assert(filename);
672 assert(lvalue);
673 assert(rvalue);
674 assert(data);
675
676 if (!utf8_is_valid(rvalue)) {
677 log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, rvalue);
678 return 0;
679 }
680
681 if (!path_is_absolute(rvalue)) {
682 log_syntax(unit, LOG_ERR, filename, line, 0, "Not an absolute path, ignoring: %s", rvalue);
683 return 0;
684 }
685
686 n = strdup(rvalue);
687 if (!n)
688 return log_oom();
689
690 path_kill_slashes(n);
691
692 free(*s);
693 *s = n;
694
695 return 0;
696 }
697
698 int config_parse_strv(const char *unit,
699 const char *filename,
700 unsigned line,
701 const char *section,
702 unsigned section_line,
703 const char *lvalue,
704 int ltype,
705 const char *rvalue,
706 void *data,
707 void *userdata) {
708
709 char ***sv = data;
710 int r;
711
712 assert(filename);
713 assert(lvalue);
714 assert(rvalue);
715 assert(data);
716
717 if (isempty(rvalue)) {
718 char **empty;
719
720 /* Empty assignment resets the list. As a special rule
721 * we actually fill in a real empty array here rather
722 * than NULL, since some code wants to know if
723 * something was set at all... */
724 empty = new0(char*, 1);
725 if (!empty)
726 return log_oom();
727
728 strv_free(*sv);
729 *sv = empty;
730
731 return 0;
732 }
733
734 for (;;) {
735 char *word = NULL;
736
737 r = extract_first_word(&rvalue, &word, WHITESPACE, EXTRACT_QUOTES|EXTRACT_RETAIN_ESCAPE);
738 if (r == 0)
739 break;
740 if (r == -ENOMEM)
741 return log_oom();
742 if (r < 0) {
743 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
744 break;
745 }
746
747 if (!utf8_is_valid(word)) {
748 log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, rvalue);
749 free(word);
750 continue;
751 }
752 r = strv_consume(sv, word);
753 if (r < 0)
754 return log_oom();
755 }
756
757 return 0;
758 }
759
760 int config_parse_log_facility(
761 const char *unit,
762 const char *filename,
763 unsigned line,
764 const char *section,
765 unsigned section_line,
766 const char *lvalue,
767 int ltype,
768 const char *rvalue,
769 void *data,
770 void *userdata) {
771
772
773 int *o = data, x;
774
775 assert(filename);
776 assert(lvalue);
777 assert(rvalue);
778 assert(data);
779
780 x = log_facility_unshifted_from_string(rvalue);
781 if (x < 0) {
782 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse log facility, ignoring: %s", rvalue);
783 return 0;
784 }
785
786 *o = (x << 3) | LOG_PRI(*o);
787
788 return 0;
789 }
790
791 int config_parse_log_level(
792 const char *unit,
793 const char *filename,
794 unsigned line,
795 const char *section,
796 unsigned section_line,
797 const char *lvalue,
798 int ltype,
799 const char *rvalue,
800 void *data,
801 void *userdata) {
802
803
804 int *o = data, x;
805
806 assert(filename);
807 assert(lvalue);
808 assert(rvalue);
809 assert(data);
810
811 x = log_level_from_string(rvalue);
812 if (x < 0) {
813 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse log level, ignoring: %s", rvalue);
814 return 0;
815 }
816
817 *o = (*o & LOG_FACMASK) | x;
818 return 0;
819 }
820
821 int config_parse_signal(
822 const char *unit,
823 const char *filename,
824 unsigned line,
825 const char *section,
826 unsigned section_line,
827 const char *lvalue,
828 int ltype,
829 const char *rvalue,
830 void *data,
831 void *userdata) {
832
833 int *sig = data, r;
834
835 assert(filename);
836 assert(lvalue);
837 assert(rvalue);
838 assert(sig);
839
840 r = signal_from_string_try_harder(rvalue);
841 if (r <= 0) {
842 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse signal name, ignoring: %s", rvalue);
843 return 0;
844 }
845
846 *sig = r;
847 return 0;
848 }
849
850 int config_parse_personality(
851 const char *unit,
852 const char *filename,
853 unsigned line,
854 const char *section,
855 unsigned section_line,
856 const char *lvalue,
857 int ltype,
858 const char *rvalue,
859 void *data,
860 void *userdata) {
861
862 unsigned long *personality = data, p;
863
864 assert(filename);
865 assert(lvalue);
866 assert(rvalue);
867 assert(personality);
868
869 p = personality_from_string(rvalue);
870 if (p == PERSONALITY_INVALID) {
871 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse personality, ignoring: %s", rvalue);
872 return 0;
873 }
874
875 *personality = p;
876 return 0;
877 }
878
879 int config_parse_ifname(
880 const char *unit,
881 const char *filename,
882 unsigned line,
883 const char *section,
884 unsigned section_line,
885 const char *lvalue,
886 int ltype,
887 const char *rvalue,
888 void *data,
889 void *userdata) {
890
891 char **s = data;
892 int r;
893
894 assert(filename);
895 assert(lvalue);
896 assert(rvalue);
897 assert(data);
898
899 if (isempty(rvalue)) {
900 *s = mfree(*s);
901 return 0;
902 }
903
904 if (!ifname_valid(rvalue)) {
905 log_syntax(unit, LOG_ERR, filename, line, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue);
906 return 0;
907 }
908
909 r = free_and_strdup(s, rvalue);
910 if (r < 0)
911 return log_oom();
912
913 return 0;
914 }