]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/conf-parser.c
Merge pull request #10682 from yuwata/fix-oss-fuzz-network-issues
[thirdparty/systemd.git] / src / shared / conf-parser.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
a7334b09 2
ed5bcfbe 3#include <errno.h>
a8fbdf54
TA
4#include <limits.h>
5#include <stdint.h>
07630cea 6#include <stdio.h>
ed5bcfbe 7#include <stdlib.h>
07630cea 8#include <string.h>
a8fbdf54 9#include <sys/types.h>
07630cea 10
b5efdb8a 11#include "alloc-util.h"
e8461023 12#include "conf-files.h"
6bedfcbb 13#include "conf-parser.h"
e6dde451 14#include "def.h"
a8fbdf54 15#include "extract-word.h"
6bedfcbb 16#include "fd-util.h"
e6dde451 17#include "fileio.h"
f4f15635 18#include "fs-util.h"
16354eff 19#include "log.h"
07630cea 20#include "macro.h"
6bedfcbb 21#include "parse-util.h"
9eb977db 22#include "path-util.h"
7b3e062c 23#include "process-util.h"
f757855e 24#include "signal-util.h"
d31645ad 25#include "socket-util.h"
07630cea
LP
26#include "string-util.h"
27#include "strv.h"
7b3e062c 28#include "syslog-util.h"
a8fbdf54 29#include "time-util.h"
07630cea 30#include "utf8.h"
4f424df7 31#include "rlimit-util.h"
e8e581bf 32
f975e971 33int config_item_table_lookup(
e9f3d2d5 34 const void *table,
ed5bcfbe 35 const char *section,
ed5bcfbe 36 const char *lvalue,
f975e971
LP
37 ConfigParserCallback *func,
38 int *ltype,
39 void **data,
ed5bcfbe
LP
40 void *userdata) {
41
e9f3d2d5 42 const ConfigTableItem *t;
f975e971
LP
43
44 assert(table);
ed5bcfbe 45 assert(lvalue);
f975e971
LP
46 assert(func);
47 assert(ltype);
48 assert(data);
ed5bcfbe 49
f975e971 50 for (t = table; t->lvalue; t++) {
ed5bcfbe 51
f975e971 52 if (!streq(lvalue, t->lvalue))
ed5bcfbe
LP
53 continue;
54
f975e971 55 if (!streq_ptr(section, t->section))
ed5bcfbe
LP
56 continue;
57
f975e971
LP
58 *func = t->parse;
59 *ltype = t->ltype;
60 *data = t->data;
61 return 1;
62 }
ed5bcfbe 63
f975e971
LP
64 return 0;
65}
10e87ee7 66
f975e971 67int config_item_perf_lookup(
e9f3d2d5 68 const void *table,
f975e971
LP
69 const char *section,
70 const char *lvalue,
71 ConfigParserCallback *func,
72 int *ltype,
73 void **data,
74 void *userdata) {
75
76 ConfigPerfItemLookup lookup = (ConfigPerfItemLookup) table;
77 const ConfigPerfItem *p;
78
79 assert(table);
80 assert(lvalue);
81 assert(func);
82 assert(ltype);
83 assert(data);
84
85 if (!section)
86 p = lookup(lvalue, strlen(lvalue));
87 else {
88 char *key;
89
605405c6 90 key = strjoin(section, ".", lvalue);
44d91056 91 if (!key)
f975e971
LP
92 return -ENOMEM;
93
94 p = lookup(key, strlen(key));
95 free(key);
ed5bcfbe
LP
96 }
97
f975e971
LP
98 if (!p)
99 return 0;
100
101 *func = p->parse;
102 *ltype = p->ltype;
103 *data = (uint8_t*) userdata + p->offset;
104 return 1;
105}
106
107/* Run the user supplied parser for an assignment */
bcde742e
LP
108static int next_assignment(
109 const char *unit,
110 const char *filename,
111 unsigned line,
112 ConfigItemLookup lookup,
113 const void *table,
114 const char *section,
115 unsigned section_line,
116 const char *lvalue,
117 const char *rvalue,
118 ConfigParseFlags flags,
119 void *userdata) {
f975e971
LP
120
121 ConfigParserCallback func = NULL;
122 int ltype = 0;
123 void *data = NULL;
124 int r;
125
126 assert(filename);
127 assert(line > 0);
128 assert(lookup);
129 assert(lvalue);
130 assert(rvalue);
131
132 r = lookup(table, section, lvalue, &func, &ltype, &data, userdata);
133 if (r < 0)
134 return r;
135
d937fbbd
LP
136 if (r > 0) {
137 if (func)
71a61510
TG
138 return func(unit, filename, line, section, section_line,
139 lvalue, ltype, rvalue, data, userdata);
d937fbbd
LP
140
141 return 0;
142 }
f975e971 143
46205bb6 144 /* Warn about unknown non-extension fields. */
bcde742e 145 if (!(flags & CONFIG_PARSE_RELAXED) && !startswith(lvalue, "X-"))
12ca818f 146 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown lvalue '%s' in section '%s'", lvalue, section);
46205bb6 147
f1857be0 148 return 0;
ed5bcfbe
LP
149}
150
8a37ce65 151/* Parse a single logical line */
bcde742e
LP
152static int parse_line(
153 const char* unit,
154 const char *filename,
155 unsigned line,
156 const char *sections,
157 ConfigItemLookup lookup,
158 const void *table,
159 ConfigParseFlags flags,
160 char **section,
161 unsigned *section_line,
162 bool *section_ignored,
163 char *l,
164 void *userdata) {
f975e971 165
bdc8e623 166 char *e, *include;
ed5bcfbe 167
f975e971
LP
168 assert(filename);
169 assert(line > 0);
170 assert(lookup);
171 assert(l);
172
b2aa81ef 173 l = strstrip(l);
b2aa81ef 174 if (!*l)
ed5bcfbe 175 return 0;
1ea86b18 176
d3b6d0c2 177 if (strchr(COMMENTS "\n", *l))
1ea86b18 178 return 0;
ed5bcfbe 179
bdc8e623
LP
180 include = first_word(l, ".include");
181 if (include) {
db5c0122
LP
182 _cleanup_free_ char *fn = NULL;
183
b8e7a47b
LP
184 /* .includes are a bad idea, we only support them here
185 * for historical reasons. They create cyclic include
186 * problems and make it difficult to detect
187 * configuration file changes with an easy
188 * stat(). Better approaches, such as .d/ drop-in
189 * snippets exist.
190 *
191 * Support for them should be eventually removed. */
192
bcde742e 193 if (!(flags & CONFIG_PARSE_ALLOW_INCLUDE)) {
12ca818f 194 log_syntax(unit, LOG_ERR, filename, line, 0, ".include not allowed here. Ignoring.");
db5c0122
LP
195 return 0;
196 }
ed5bcfbe 197
41b283d0
LP
198 log_syntax(unit, LOG_WARNING, filename, line, 0,
199 ".include directives are deprecated, and support for them will be removed in a future version of systemd. "
200 "Please use drop-in files instead.");
201
bdc8e623 202 fn = file_in_same_dir(filename, strstrip(include));
f975e971 203 if (!fn)
b2aa81ef 204 return -ENOMEM;
ed5bcfbe 205
bcde742e 206 return config_parse(unit, fn, NULL, sections, lookup, table, flags, userdata);
ed5bcfbe
LP
207 }
208
78d17fa0
YW
209 if (!utf8_is_valid(l))
210 return log_syntax_invalid_utf8(unit, LOG_WARNING, filename, line, l);
211
b2aa81ef 212 if (*l == '[') {
ed5bcfbe
LP
213 size_t k;
214 char *n;
215
b2aa81ef 216 k = strlen(l);
ed5bcfbe
LP
217 assert(k > 0);
218
b2aa81ef 219 if (l[k-1] != ']') {
12ca818f 220 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid section header '%s'", l);
ed5bcfbe
LP
221 return -EBADMSG;
222 }
223
f975e971
LP
224 n = strndup(l+1, k-2);
225 if (!n)
ed5bcfbe
LP
226 return -ENOMEM;
227
f975e971 228 if (sections && !nulstr_contains(sections, n)) {
42f4e3c4 229
bcde742e 230 if (!(flags & CONFIG_PARSE_RELAXED) && !startswith(n, "X-"))
12ca818f 231 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown section '%s'. Ignoring.", n);
f975e971
LP
232
233 free(n);
a1e58e8e 234 *section = mfree(*section);
71a61510 235 *section_line = 0;
342aea19 236 *section_ignored = true;
f975e971 237 } else {
97b9c506 238 free_and_replace(*section, n);
71a61510 239 *section_line = line;
342aea19 240 *section_ignored = false;
f975e971 241 }
ed5bcfbe
LP
242
243 return 0;
244 }
245
62f168a0
LP
246 if (sections && !*section) {
247
bcde742e 248 if (!(flags & CONFIG_PARSE_RELAXED) && !*section_ignored)
12ca818f 249 log_syntax(unit, LOG_WARNING, filename, line, 0, "Assignment outside of section. Ignoring.");
62f168a0 250
10e87ee7 251 return 0;
62f168a0 252 }
10e87ee7 253
f975e971
LP
254 e = strchr(l, '=');
255 if (!e) {
12ca818f
LP
256 log_syntax(unit, LOG_WARNING, filename, line, 0, "Missing '='.");
257 return -EINVAL;
ed5bcfbe
LP
258 }
259
260 *e = 0;
261 e++;
262
e8e581bf
ZJS
263 return next_assignment(unit,
264 filename,
265 line,
266 lookup,
267 table,
268 *section,
71a61510 269 *section_line,
e8e581bf
ZJS
270 strstrip(l),
271 strstrip(e),
bcde742e 272 flags,
e8e581bf 273 userdata);
ed5bcfbe
LP
274}
275
276/* Go through the file and parse each line */
e8e581bf
ZJS
277int config_parse(const char *unit,
278 const char *filename,
279 FILE *f,
280 const char *sections,
281 ConfigItemLookup lookup,
e9f3d2d5 282 const void *table,
bcde742e 283 ConfigParseFlags flags,
e8e581bf 284 void *userdata) {
f975e971 285
7fd1b19b
HH
286 _cleanup_free_ char *section = NULL, *continuation = NULL;
287 _cleanup_fclose_ FILE *ours = NULL;
71a61510 288 unsigned line = 0, section_line = 0;
bcde742e 289 bool section_ignored = false;
ed5bcfbe
LP
290 int r;
291
292 assert(filename);
f975e971 293 assert(lookup);
ed5bcfbe 294
87f0e418 295 if (!f) {
245802dd 296 f = ours = fopen(filename, "re");
f975e971 297 if (!f) {
36f822c4
ZJS
298 /* Only log on request, except for ENOENT,
299 * since we return 0 to the caller. */
bcde742e 300 if ((flags & CONFIG_PARSE_WARN) || errno == ENOENT)
b8b846d7
LP
301 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_ERR, errno,
302 "Failed to open configuration file '%s': %m", filename);
9f43a07f 303 return errno == ENOENT ? 0 : -errno;
87f0e418 304 }
ed5bcfbe
LP
305 }
306
fdb9161c
LP
307 fd_warn_permissions(filename, fileno(f));
308
9dd7ea9a 309 for (;;) {
e6dde451 310 _cleanup_free_ char *buf = NULL;
3dab2943 311 bool escaped = false;
92b5e605 312 char *l, *p, *e;
ed5bcfbe 313
e6dde451
LP
314 r = read_line(f, LONG_LINE_MAX, &buf);
315 if (r == 0)
316 break;
317 if (r == -ENOBUFS) {
bcde742e 318 if (flags & CONFIG_PARSE_WARN)
e6dde451
LP
319 log_error_errno(r, "%s:%u: Line too long", filename, line);
320
321 return r;
322 }
323 if (r < 0) {
bcde742e 324 if (CONFIG_PARSE_WARN)
e6dde451 325 log_error_errno(r, "%s:%u: Error while reading configuration file: %m", filename, line);
ed5bcfbe 326
e6dde451 327 return r;
ed5bcfbe
LP
328 }
329
9dd7ea9a 330 l = buf;
bcde742e 331 if (!(flags & CONFIG_PARSE_REFUSE_BOM)) {
e6dde451 332 char *q;
9dd7ea9a 333
e6dde451
LP
334 q = startswith(buf, UTF8_BYTE_ORDER_MARK);
335 if (q) {
336 l = q;
bcde742e 337 flags |= CONFIG_PARSE_REFUSE_BOM;
e6dde451
LP
338 }
339 }
3dab2943
LP
340
341 if (continuation) {
e6dde451 342 if (strlen(continuation) + strlen(l) > LONG_LINE_MAX) {
bcde742e 343 if (flags & CONFIG_PARSE_WARN)
e6dde451
LP
344 log_error("%s:%u: Continuation line too long", filename, line);
345 return -ENOBUFS;
346 }
347
92b5e605 348 if (!strextend(&continuation, l, NULL)) {
bcde742e 349 if (flags & CONFIG_PARSE_WARN)
36f822c4 350 log_oom();
245802dd 351 return -ENOMEM;
36f822c4 352 }
3dab2943 353
92b5e605 354 p = continuation;
3dab2943
LP
355 } else
356 p = l;
357
358 for (e = p; *e; e++) {
359 if (escaped)
360 escaped = false;
361 else if (*e == '\\')
362 escaped = true;
363 }
364
365 if (escaped) {
366 *(e-1) = ' ';
367
92b5e605 368 if (!continuation) {
f975e971 369 continuation = strdup(l);
36f822c4 370 if (!continuation) {
bcde742e 371 if (flags & CONFIG_PARSE_WARN)
36f822c4 372 log_oom();
245802dd 373 return -ENOMEM;
36f822c4 374 }
3dab2943
LP
375 }
376
377 continue;
378 }
379
e8e581bf
ZJS
380 r = parse_line(unit,
381 filename,
382 ++line,
383 sections,
384 lookup,
385 table,
bcde742e 386 flags,
e8e581bf 387 &section,
71a61510 388 &section_line,
342aea19 389 &section_ignored,
e8e581bf
ZJS
390 p,
391 userdata);
36f822c4 392 if (r < 0) {
bcde742e 393 if (flags & CONFIG_PARSE_WARN)
e6dde451 394 log_warning_errno(r, "%s:%u: Failed to parse file: %m", filename, line);
245802dd 395 return r;
36f822c4 396 }
92b5e605
LP
397
398 continuation = mfree(continuation);
ed5bcfbe
LP
399 }
400
4f29e0db
FB
401 if (continuation) {
402 r = parse_line(unit,
403 filename,
404 ++line,
405 sections,
406 lookup,
407 table,
408 flags,
409 &section,
410 &section_line,
411 &section_ignored,
412 continuation,
413 userdata);
414 if (r < 0) {
415 if (flags & CONFIG_PARSE_WARN)
416 log_warning_errno(r, "%s:%u: Failed to parse file: %m", filename, line);
417 return r;
4f29e0db
FB
418 }
419 }
420
245802dd 421 return 0;
ed5bcfbe
LP
422}
423
23bb31aa 424static int config_parse_many_files(
43688c49 425 const char *conf_file,
23bb31aa 426 char **files,
43688c49
ZJS
427 const char *sections,
428 ConfigItemLookup lookup,
429 const void *table,
bcde742e 430 ConfigParseFlags flags,
43688c49
ZJS
431 void *userdata) {
432
e8461023
JT
433 char **fn;
434 int r;
435
e8461023 436 if (conf_file) {
bcde742e 437 r = config_parse(NULL, conf_file, NULL, sections, lookup, table, flags, userdata);
e8461023
JT
438 if (r < 0)
439 return r;
440 }
441
442 STRV_FOREACH(fn, files) {
bcde742e 443 r = config_parse(NULL, *fn, NULL, sections, lookup, table, flags, userdata);
e8461023
JT
444 if (r < 0)
445 return r;
446 }
447
448 return 0;
449}
450
23bb31aa
ZJS
451/* Parse each config file in the directories specified as nulstr. */
452int config_parse_many_nulstr(
453 const char *conf_file,
454 const char *conf_file_dirs,
455 const char *sections,
456 ConfigItemLookup lookup,
457 const void *table,
bcde742e 458 ConfigParseFlags flags,
23bb31aa
ZJS
459 void *userdata) {
460
461 _cleanup_strv_free_ char **files = NULL;
462 int r;
463
b5084605 464 r = conf_files_list_nulstr(&files, ".conf", NULL, 0, conf_file_dirs);
23bb31aa
ZJS
465 if (r < 0)
466 return r;
467
bcde742e 468 return config_parse_many_files(conf_file, files, sections, lookup, table, flags, userdata);
23bb31aa
ZJS
469}
470
471/* Parse each config file in the directories specified as strv. */
472int config_parse_many(
473 const char *conf_file,
474 const char* const* conf_file_dirs,
475 const char *dropin_dirname,
476 const char *sections,
477 ConfigItemLookup lookup,
478 const void *table,
bcde742e 479 ConfigParseFlags flags,
23bb31aa
ZJS
480 void *userdata) {
481
482 _cleanup_strv_free_ char **dropin_dirs = NULL;
483 _cleanup_strv_free_ char **files = NULL;
484 const char *suffix;
485 int r;
486
487 suffix = strjoina("/", dropin_dirname);
488 r = strv_extend_strv_concat(&dropin_dirs, (char**) conf_file_dirs, suffix);
489 if (r < 0)
490 return r;
491
b5084605 492 r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char* const*) dropin_dirs);
23bb31aa
ZJS
493 if (r < 0)
494 return r;
495
bcde742e 496 return config_parse_many_files(conf_file, files, sections, lookup, table, flags, userdata);
23bb31aa
ZJS
497}
498
eb3491d9 499#define DEFINE_PARSER(type, vartype, conv_func) \
2d1729ca 500 DEFINE_CONFIG_PARSE_PTR(config_parse_##type, conv_func, vartype, "Failed to parse " #type " value")
94d75d64
LP
501
502DEFINE_PARSER(int, int, safe_atoi);
503DEFINE_PARSER(long, long, safe_atoli);
134e24e1 504DEFINE_PARSER(uint8, uint8_t, safe_atou8);
c7440e74 505DEFINE_PARSER(uint16, uint16_t, safe_atou16);
94d75d64
LP
506DEFINE_PARSER(uint32, uint32_t, safe_atou32);
507DEFINE_PARSER(uint64, uint64_t, safe_atou64);
508DEFINE_PARSER(unsigned, unsigned, safe_atou);
509DEFINE_PARSER(double, double, safe_atod);
510DEFINE_PARSER(nsec, nsec_t, parse_nsec);
511DEFINE_PARSER(sec, usec_t, parse_sec);
512DEFINE_PARSER(mode, mode_t, parse_mode);
ed5bcfbe 513
5556b5fe 514int config_parse_iec_size(const char* unit,
e8e581bf
ZJS
515 const char *filename,
516 unsigned line,
517 const char *section,
71a61510 518 unsigned section_line,
e8e581bf
ZJS
519 const char *lvalue,
520 int ltype,
521 const char *rvalue,
522 void *data,
523 void *userdata) {
ed5bcfbe
LP
524
525 size_t *sz = data;
59f448cf 526 uint64_t v;
e8e581bf 527 int r;
ed5bcfbe
LP
528
529 assert(filename);
530 assert(lvalue);
531 assert(rvalue);
532 assert(data);
533
59f448cf 534 r = parse_size(rvalue, 1024, &v);
1e5f4e8b
YW
535 if (r >= 0 && (uint64_t) (size_t) v != v)
536 r = -ERANGE;
537 if (r < 0) {
538 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value '%s', ignoring: %m", rvalue);
9ba1a159
LP
539 return 0;
540 }
541
59f448cf 542 *sz = (size_t) v;
9ba1a159
LP
543 return 0;
544}
545
78f3c4bc
LP
546int config_parse_si_size(
547 const char* unit,
548 const char *filename,
549 unsigned line,
550 const char *section,
551 unsigned section_line,
552 const char *lvalue,
553 int ltype,
554 const char *rvalue,
555 void *data,
556 void *userdata) {
5556b5fe
LP
557
558 size_t *sz = data;
59f448cf 559 uint64_t v;
5556b5fe
LP
560 int r;
561
562 assert(filename);
563 assert(lvalue);
564 assert(rvalue);
565 assert(data);
566
59f448cf 567 r = parse_size(rvalue, 1000, &v);
1e5f4e8b
YW
568 if (r >= 0 && (uint64_t) (size_t) v != v)
569 r = -ERANGE;
570 if (r < 0) {
571 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value '%s', ignoring: %m", rvalue);
5556b5fe
LP
572 return 0;
573 }
574
59f448cf 575 *sz = (size_t) v;
5556b5fe
LP
576 return 0;
577}
9ba1a159 578
78f3c4bc
LP
579int config_parse_iec_uint64(
580 const char* unit,
581 const char *filename,
582 unsigned line,
583 const char *section,
584 unsigned section_line,
585 const char *lvalue,
586 int ltype,
587 const char *rvalue,
588 void *data,
589 void *userdata) {
9ba1a159 590
59f448cf 591 uint64_t *bytes = data;
e8e581bf 592 int r;
9ba1a159
LP
593
594 assert(filename);
595 assert(lvalue);
596 assert(rvalue);
597 assert(data);
598
5556b5fe 599 r = parse_size(rvalue, 1024, bytes);
e8e581bf 600 if (r < 0)
59f448cf 601 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
ed5bcfbe 602
ed5bcfbe
LP
603 return 0;
604}
605
e8e581bf
ZJS
606int config_parse_bool(const char* unit,
607 const char *filename,
608 unsigned line,
609 const char *section,
71a61510 610 unsigned section_line,
e8e581bf
ZJS
611 const char *lvalue,
612 int ltype,
613 const char *rvalue,
614 void *data,
615 void *userdata) {
ed5bcfbe
LP
616
617 int k;
618 bool *b = data;
2c75fb73 619 bool fatal = ltype;
ed5bcfbe
LP
620
621 assert(filename);
622 assert(lvalue);
623 assert(rvalue);
624 assert(data);
625
e8e581bf
ZJS
626 k = parse_boolean(rvalue);
627 if (k < 0) {
2c75fb73
ZJS
628 log_syntax(unit, LOG_ERR, filename, line, k,
629 "Failed to parse boolean value%s: %s",
630 fatal ? "" : ", ignoring", rvalue);
631 return fatal ? -ENOEXEC : 0;
ed5bcfbe
LP
632 }
633
5d904a6a 634 *b = k;
ed5bcfbe
LP
635 return 0;
636}
637
f757855e
LP
638int config_parse_tristate(
639 const char* unit,
640 const char *filename,
641 unsigned line,
642 const char *section,
643 unsigned section_line,
644 const char *lvalue,
645 int ltype,
646 const char *rvalue,
647 void *data,
648 void *userdata) {
649
650 int k, *t = data;
651
652 assert(filename);
653 assert(lvalue);
654 assert(rvalue);
655 assert(data);
656
657 /* A tristate is pretty much a boolean, except that it can
658 * also take the special value -1, indicating "uninitialized",
659 * much like NULL is for a pointer type. */
660
661 k = parse_boolean(rvalue);
662 if (k < 0) {
663 log_syntax(unit, LOG_ERR, filename, line, k, "Failed to parse boolean value, ignoring: %s", rvalue);
664 return 0;
665 }
666
667 *t = !!k;
668 return 0;
669}
670
8f2665a4
LP
671int config_parse_string(
672 const char *unit,
673 const char *filename,
674 unsigned line,
675 const char *section,
676 unsigned section_line,
677 const char *lvalue,
678 int ltype,
679 const char *rvalue,
680 void *data,
681 void *userdata) {
682
5a4ff988 683 char **s = data;
ed5bcfbe
LP
684
685 assert(filename);
686 assert(lvalue);
687 assert(rvalue);
688 assert(data);
689
5a4ff988
LP
690 if (free_and_strdup(s, empty_to_null(rvalue)) < 0)
691 return log_oom();
8f2665a4 692
ed5bcfbe
LP
693 return 0;
694}
57d42a5f 695
8f2665a4
LP
696int config_parse_path(
697 const char *unit,
698 const char *filename,
699 unsigned line,
700 const char *section,
701 unsigned section_line,
702 const char *lvalue,
703 int ltype,
704 const char *rvalue,
705 void *data,
706 void *userdata) {
034c6ed7 707
c0d72c43 708 _cleanup_free_ char *n = NULL;
2c75fb73 709 bool fatal = ltype;
c0d72c43 710 char **s = data;
cd4f53c5 711 int r;
034c6ed7
LP
712
713 assert(filename);
714 assert(lvalue);
715 assert(rvalue);
716 assert(data);
717
8e7b5bd0 718 if (isempty(rvalue))
0fe50629 719 goto finalize;
0fe50629 720
7f110ff9
LP
721 n = strdup(rvalue);
722 if (!n)
74051b9b 723 return log_oom();
034c6ed7 724
cd4f53c5
YW
725 r = path_simplify_and_warn(n, PATH_CHECK_ABSOLUTE | (fatal ? PATH_CHECK_FATAL : 0), unit, filename, line, lvalue);
726 if (r < 0)
727 return fatal ? -ENOEXEC : 0;
01f78473 728
0fe50629 729finalize:
8e7b5bd0 730 return free_and_replace(*s, n);
034c6ed7 731}
57d42a5f 732
8249bb72
LP
733int config_parse_strv(
734 const char *unit,
735 const char *filename,
736 unsigned line,
737 const char *section,
738 unsigned section_line,
739 const char *lvalue,
740 int ltype,
741 const char *rvalue,
742 void *data,
743 void *userdata) {
57d42a5f 744
a2a5291b 745 char ***sv = data;
00d0fd06 746 int r;
57d42a5f
LP
747
748 assert(filename);
749 assert(lvalue);
750 assert(rvalue);
751 assert(data);
752
74051b9b 753 if (isempty(rvalue)) {
8249bb72 754 *sv = strv_free(*sv);
853b8397 755 return 0;
74051b9b
LP
756 }
757
34f253f0
DR
758 for (;;) {
759 char *word = NULL;
00d0fd06 760
9a82ab95 761 r = extract_first_word(&rvalue, &word, NULL, EXTRACT_QUOTES|EXTRACT_RETAIN_ESCAPE);
34f253f0
DR
762 if (r == 0)
763 break;
764 if (r == -ENOMEM)
853b8397 765 return log_oom();
34f253f0
DR
766 if (r < 0) {
767 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
768 break;
769 }
853b8397 770
34f253f0 771 r = strv_consume(sv, word);
853b8397
LP
772 if (r < 0)
773 return log_oom();
7f110ff9 774 }
57d42a5f 775
57d42a5f 776 return 0;
57d42a5f 777}
15ae422b 778
1e35c5ab
RP
779int config_parse_warn_compat(
780 const char *unit,
781 const char *filename,
782 unsigned line,
783 const char *section,
784 unsigned section_line,
785 const char *lvalue,
786 int ltype,
787 const char *rvalue,
788 void *data,
789 void *userdata) {
7ef7e15b 790
1e35c5ab
RP
791 Disabled reason = ltype;
792
793 switch(reason) {
7ef7e15b 794
1e35c5ab
RP
795 case DISABLED_CONFIGURATION:
796 log_syntax(unit, LOG_DEBUG, filename, line, 0,
797 "Support for option %s= has been disabled at compile time and it is ignored", lvalue);
798 break;
7ef7e15b 799
1e35c5ab
RP
800 case DISABLED_LEGACY:
801 log_syntax(unit, LOG_INFO, filename, line, 0,
802 "Support for option %s= has been removed and it is ignored", lvalue);
803 break;
7ef7e15b 804
1e35c5ab
RP
805 case DISABLED_EXPERIMENTAL:
806 log_syntax(unit, LOG_INFO, filename, line, 0,
807 "Support for option %s= has not yet been enabled and it is ignored", lvalue);
808 break;
7ef7e15b 809 }
1e35c5ab
RP
810
811 return 0;
812}
813
ca37242e
LP
814int config_parse_log_facility(
815 const char *unit,
816 const char *filename,
817 unsigned line,
818 const char *section,
819 unsigned section_line,
820 const char *lvalue,
821 int ltype,
822 const char *rvalue,
823 void *data,
824 void *userdata) {
213ba152 825
213ba152
LP
826 int *o = data, x;
827
828 assert(filename);
829 assert(lvalue);
830 assert(rvalue);
831 assert(data);
832
833 x = log_facility_unshifted_from_string(rvalue);
834 if (x < 0) {
12ca818f 835 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse log facility, ignoring: %s", rvalue);
213ba152
LP
836 return 0;
837 }
838
839 *o = (x << 3) | LOG_PRI(*o);
840
841 return 0;
842}
843
ca37242e
LP
844int config_parse_log_level(
845 const char *unit,
846 const char *filename,
847 unsigned line,
848 const char *section,
849 unsigned section_line,
850 const char *lvalue,
851 int ltype,
852 const char *rvalue,
853 void *data,
854 void *userdata) {
213ba152 855
213ba152
LP
856 int *o = data, x;
857
858 assert(filename);
859 assert(lvalue);
860 assert(rvalue);
861 assert(data);
862
863 x = log_level_from_string(rvalue);
864 if (x < 0) {
12ca818f 865 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse log level, ignoring: %s", rvalue);
213ba152
LP
866 return 0;
867 }
868
d3070fbd
LP
869 if (*o < 0) /* if it wasn't initialized so far, assume zero facility */
870 *o = x;
871 else
872 *o = (*o & LOG_FACMASK) | x;
873
213ba152
LP
874 return 0;
875}
f757855e
LP
876
877int config_parse_signal(
878 const char *unit,
879 const char *filename,
880 unsigned line,
881 const char *section,
882 unsigned section_line,
883 const char *lvalue,
884 int ltype,
885 const char *rvalue,
886 void *data,
887 void *userdata) {
888
889 int *sig = data, r;
890
891 assert(filename);
892 assert(lvalue);
893 assert(rvalue);
894 assert(sig);
895
29a3db75 896 r = signal_from_string(rvalue);
f757855e 897 if (r <= 0) {
12ca818f 898 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse signal name, ignoring: %s", rvalue);
f757855e
LP
899 return 0;
900 }
901
902 *sig = r;
903 return 0;
904}
905
906int config_parse_personality(
907 const char *unit,
908 const char *filename,
909 unsigned line,
910 const char *section,
911 unsigned section_line,
912 const char *lvalue,
913 int ltype,
914 const char *rvalue,
915 void *data,
916 void *userdata) {
917
918 unsigned long *personality = data, p;
919
920 assert(filename);
921 assert(lvalue);
922 assert(rvalue);
923 assert(personality);
924
40fdd636
LP
925 if (isempty(rvalue))
926 p = PERSONALITY_INVALID;
927 else {
928 p = personality_from_string(rvalue);
929 if (p == PERSONALITY_INVALID) {
930 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse personality, ignoring: %s", rvalue);
931 return 0;
932 }
f757855e
LP
933 }
934
935 *personality = p;
936 return 0;
937}
d31645ad
LP
938
939int config_parse_ifname(
940 const char *unit,
941 const char *filename,
942 unsigned line,
943 const char *section,
944 unsigned section_line,
945 const char *lvalue,
946 int ltype,
947 const char *rvalue,
948 void *data,
949 void *userdata) {
950
951 char **s = data;
952 int r;
953
954 assert(filename);
955 assert(lvalue);
956 assert(rvalue);
957 assert(data);
958
959 if (isempty(rvalue)) {
960 *s = mfree(*s);
961 return 0;
962 }
963
964 if (!ifname_valid(rvalue)) {
965 log_syntax(unit, LOG_ERR, filename, line, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue);
966 return 0;
967 }
968
969 r = free_and_strdup(s, rvalue);
970 if (r < 0)
971 return log_oom();
972
973 return 0;
974}
177d0b20
SS
975
976int config_parse_ip_port(
977 const char *unit,
978 const char *filename,
979 unsigned line,
980 const char *section,
981 unsigned section_line,
982 const char *lvalue,
983 int ltype,
984 const char *rvalue,
985 void *data,
986 void *userdata) {
987
988 uint16_t *s = data;
989 uint16_t port;
990 int r;
991
992 assert(filename);
993 assert(lvalue);
994 assert(rvalue);
995 assert(data);
996
997 if (isempty(rvalue)) {
998 *s = 0;
999 return 0;
1000 }
1001
1002 r = parse_ip_port(rvalue, &port);
1003 if (r < 0) {
1004 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse port '%s'.", rvalue);
1005 return 0;
1006 }
1007
1008 *s = port;
1009
1010 return 0;
1011}
9ecdba8c
ZJS
1012
1013int config_parse_join_controllers(
1014 const char *unit,
1015 const char *filename,
1016 unsigned line,
1017 const char *section,
1018 unsigned section_line,
1019 const char *lvalue,
1020 int ltype,
1021 const char *rvalue,
1022 void *data,
1023 void *userdata) {
1024
1025 char ****ret = data;
1026 const char *whole_rvalue = rvalue;
1027 unsigned n = 0;
1028 _cleanup_(strv_free_freep) char ***controllers = NULL;
1029
1030 assert(filename);
1031 assert(lvalue);
1032 assert(rvalue);
1033 assert(ret);
1034
1035 for (;;) {
1036 _cleanup_free_ char *word = NULL;
1037 char **l;
1038 int r;
1039
1040 r = extract_first_word(&rvalue, &word, NULL, EXTRACT_QUOTES);
1041 if (r < 0) {
1042 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid value for %s: %s", lvalue, whole_rvalue);
1043 return r;
1044 }
1045 if (r == 0)
1046 break;
1047
1048 l = strv_split(word, ",");
1049 if (!l)
1050 return log_oom();
1051 strv_uniq(l);
1052
1053 if (strv_length(l) <= 1) {
1054 strv_free(l);
1055 continue;
1056 }
1057
1058 if (!controllers) {
1059 controllers = new(char**, 2);
1060 if (!controllers) {
1061 strv_free(l);
1062 return log_oom();
1063 }
1064
1065 controllers[0] = l;
1066 controllers[1] = NULL;
1067
1068 n = 1;
1069 } else {
1070 char ***a;
1071 char ***t;
1072
1073 t = new0(char**, n+2);
1074 if (!t) {
1075 strv_free(l);
1076 return log_oom();
1077 }
1078
1079 n = 0;
1080
1081 for (a = controllers; *a; a++)
1082 if (strv_overlap(*a, l)) {
1083 if (strv_extend_strv(&l, *a, false) < 0) {
1084 strv_free(l);
1085 strv_free_free(t);
1086 return log_oom();
1087 }
1088
1089 } else {
1090 char **c;
1091
1092 c = strv_copy(*a);
1093 if (!c) {
1094 strv_free(l);
1095 strv_free_free(t);
1096 return log_oom();
1097 }
1098
1099 t[n++] = c;
1100 }
1101
1102 t[n++] = strv_uniq(l);
1103
1104 strv_free_free(controllers);
1105 controllers = t;
1106 }
1107 }
1108 if (!isempty(rvalue))
1109 log_syntax(unit, LOG_ERR, filename, line, 0, "Trailing garbage, ignoring.");
1110
56c8d744
ZJS
1111 /* As a special case, return a single empty strv, to override the default */
1112 if (!controllers) {
1113 controllers = new(char**, 2);
1114 if (!controllers)
1115 return log_oom();
bea1a013 1116 controllers[0] = strv_new(NULL);
56c8d744
ZJS
1117 if (!controllers[0])
1118 return log_oom();
1119 controllers[1] = NULL;
1120 }
1121
9ecdba8c 1122 strv_free_free(*ret);
1cc6c93a 1123 *ret = TAKE_PTR(controllers);
9ecdba8c
ZJS
1124
1125 return 0;
1126}
79138a38
LP
1127
1128int config_parse_mtu(
1129 const char *unit,
1130 const char *filename,
1131 unsigned line,
1132 const char *section,
1133 unsigned section_line,
1134 const char *lvalue,
1135 int ltype,
1136 const char *rvalue,
1137 void *data,
1138 void *userdata) {
1139
1140 uint32_t *mtu = data;
1141 int r;
1142
1143 assert(rvalue);
1144 assert(mtu);
1145
1146 r = parse_mtu(ltype, rvalue, mtu);
1147 if (r == -ERANGE) {
1148 log_syntax(unit, LOG_ERR, filename, line, r,
1149 "Maximum transfer unit (MTU) value out of range. Permitted range is %" PRIu32 "…%" PRIu32 ", ignoring: %s",
1150 (uint32_t) (ltype == AF_INET6 ? IPV6_MIN_MTU : IPV4_MIN_MTU), (uint32_t) UINT32_MAX,
1151 rvalue);
1152 return 0;
1153 }
1154 if (r < 0) {
1155 log_syntax(unit, LOG_ERR, filename, line, r,
1156 "Failed to parse MTU value '%s', ignoring: %m", rvalue);
1157 return 0;
1158 }
1159
1160 return 0;
1161}
4f424df7
LP
1162
1163int config_parse_rlimit(
1164 const char *unit,
1165 const char *filename,
1166 unsigned line,
1167 const char *section,
1168 unsigned section_line,
1169 const char *lvalue,
1170 int ltype,
1171 const char *rvalue,
1172 void *data,
1173 void *userdata) {
1174
1175 struct rlimit **rl = data, d = {};
1176 int r;
1177
1178 assert(rvalue);
1179 assert(rl);
1180
1181 r = rlimit_parse(ltype, rvalue, &d);
1182 if (r == -EILSEQ) {
1183 log_syntax(unit, LOG_WARNING, filename, line, r, "Soft resource limit chosen higher than hard limit, ignoring: %s", rvalue);
1184 return 0;
1185 }
1186 if (r < 0) {
1187 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse resource value, ignoring: %s", rvalue);
1188 return 0;
1189 }
1190
1191 if (rl[ltype])
1192 *rl[ltype] = d;
1193 else {
1194 rl[ltype] = newdup(struct rlimit, &d, 1);
1195 if (!rl[ltype])
1196 return log_oom();
1197 }
1198
1199 return 0;
1200}
c07b23ca
MKB
1201
1202int config_parse_permille(const char* unit,
1203 const char *filename,
1204 unsigned line,
1205 const char *section,
1206 unsigned section_line,
1207 const char *lvalue,
1208 int ltype,
1209 const char *rvalue,
1210 void *data,
1211 void *userdata) {
1212
1213 unsigned *permille = data;
1214 int r;
1215
1216 assert(filename);
1217 assert(lvalue);
1218 assert(rvalue);
1219 assert(permille);
1220
1221 r = parse_permille(rvalue);
1222 if (r < 0) {
1223 log_syntax(unit, LOG_ERR, filename, line, r,
1224 "Failed to parse permille value, ignoring: %s", rvalue);
1225 return 0;
1226 }
1227
1228 *permille = (unsigned) r;
1229
1230 return 0;
1231}