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