1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include "alloc-util.h"
12 #include "conf-files.h"
13 #include "conf-parser.h"
15 #include "extract-word.h"
22 #include "parse-util.h"
23 #include "path-util.h"
24 #include "process-util.h"
25 #include "rlimit-util.h"
26 #include "signal-util.h"
27 #include "socket-util.h"
28 #include "string-util.h"
30 #include "syslog-util.h"
31 #include "time-util.h"
34 int config_item_table_lookup(
38 ConfigParserCallback
*func
,
43 const ConfigTableItem
*t
;
51 for (t
= table
; t
->lvalue
; t
++) {
53 if (!streq(lvalue
, t
->lvalue
))
56 if (!streq_ptr(section
, t
->section
))
68 int config_item_perf_lookup(
72 ConfigParserCallback
*func
,
77 ConfigPerfItemLookup lookup
= (ConfigPerfItemLookup
) table
;
78 const ConfigPerfItem
*p
;
89 key
= strjoina(section
, ".", lvalue
);
90 p
= lookup(key
, strlen(key
));
92 p
= lookup(lvalue
, strlen(lvalue
));
98 *data
= (uint8_t*) userdata
+ p
->offset
;
102 /* Run the user supplied parser for an assignment */
103 static int next_assignment(
105 const char *filename
,
107 ConfigItemLookup lookup
,
110 unsigned section_line
,
113 ConfigParseFlags flags
,
116 ConfigParserCallback func
= NULL
;
127 r
= lookup(table
, section
, lvalue
, &func
, <ype
, &data
, userdata
);
132 return func(unit
, filename
, line
, section
, section_line
,
133 lvalue
, ltype
, rvalue
, data
, userdata
);
138 /* Warn about unknown non-extension fields. */
139 if (!(flags
& CONFIG_PARSE_RELAXED
) && !startswith(lvalue
, "X-"))
140 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Unknown lvalue '%s' in section '%s', ignoring", lvalue
, section
);
145 /* Parse a single logical line */
146 static int parse_line(
148 const char *filename
,
150 const char *sections
,
151 ConfigItemLookup lookup
,
153 ConfigParseFlags flags
,
155 unsigned *section_line
,
156 bool *section_ignored
,
174 include
= first_word(l
, ".include");
176 _cleanup_free_
char *fn
= NULL
;
178 /* .includes are a bad idea, we only support them here
179 * for historical reasons. They create cyclic include
180 * problems and make it difficult to detect
181 * configuration file changes with an easy
182 * stat(). Better approaches, such as .d/ drop-in
185 * Support for them should be eventually removed. */
187 if (!(flags
& CONFIG_PARSE_ALLOW_INCLUDE
)) {
188 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, ".include not allowed here. Ignoring.");
192 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
193 ".include directives are deprecated, and support for them will be removed in a future version of systemd. "
194 "Please use drop-in files instead.");
196 fn
= file_in_same_dir(filename
, strstrip(include
));
200 return config_parse(unit
, fn
, NULL
, sections
, lookup
, table
, flags
, userdata
);
203 if (!utf8_is_valid(l
))
204 return log_syntax_invalid_utf8(unit
, LOG_WARNING
, filename
, line
, l
);
214 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid section header '%s'", l
);
218 n
= strndup(l
+1, k
-2);
222 if (sections
&& !nulstr_contains(sections
, n
)) {
224 if (!(flags
& CONFIG_PARSE_RELAXED
) && !startswith(n
, "X-"))
225 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Unknown section '%s'. Ignoring.", n
);
228 *section
= mfree(*section
);
230 *section_ignored
= true;
232 free_and_replace(*section
, n
);
233 *section_line
= line
;
234 *section_ignored
= false;
240 if (sections
&& !*section
) {
242 if (!(flags
& CONFIG_PARSE_RELAXED
) && !*section_ignored
)
243 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Assignment outside of section. Ignoring.");
250 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Missing '='.");
257 return next_assignment(unit
,
270 /* Go through the file and parse each line */
271 int config_parse(const char *unit
,
272 const char *filename
,
274 const char *sections
,
275 ConfigItemLookup lookup
,
277 ConfigParseFlags flags
,
280 _cleanup_free_
char *section
= NULL
, *continuation
= NULL
;
281 _cleanup_fclose_
FILE *ours
= NULL
;
282 unsigned line
= 0, section_line
= 0;
283 bool section_ignored
= false;
290 f
= ours
= fopen(filename
, "re");
292 /* Only log on request, except for ENOENT,
293 * since we return 0 to the caller. */
294 if ((flags
& CONFIG_PARSE_WARN
) || errno
== ENOENT
)
295 log_full_errno(errno
== ENOENT
? LOG_DEBUG
: LOG_ERR
, errno
,
296 "Failed to open configuration file '%s': %m", filename
);
297 return errno
== ENOENT
? 0 : -errno
;
301 fd_warn_permissions(filename
, fileno(f
));
304 _cleanup_free_
char *buf
= NULL
;
305 bool escaped
= false;
308 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
312 if (flags
& CONFIG_PARSE_WARN
)
313 log_error_errno(r
, "%s:%u: Line too long", filename
, line
);
318 if (CONFIG_PARSE_WARN
)
319 log_error_errno(r
, "%s:%u: Error while reading configuration file: %m", filename
, line
);
324 if (strchr(COMMENTS
, *skip_leading_chars(buf
, WHITESPACE
)))
328 if (!(flags
& CONFIG_PARSE_REFUSE_BOM
)) {
331 q
= startswith(buf
, UTF8_BYTE_ORDER_MARK
);
334 flags
|= CONFIG_PARSE_REFUSE_BOM
;
339 if (strlen(continuation
) + strlen(l
) > LONG_LINE_MAX
) {
340 if (flags
& CONFIG_PARSE_WARN
)
341 log_error("%s:%u: Continuation line too long", filename
, line
);
345 if (!strextend(&continuation
, l
, NULL
)) {
346 if (flags
& CONFIG_PARSE_WARN
)
355 for (e
= p
; *e
; e
++) {
366 continuation
= strdup(l
);
368 if (flags
& CONFIG_PARSE_WARN
)
390 if (flags
& CONFIG_PARSE_WARN
)
391 log_warning_errno(r
, "%s:%u: Failed to parse file: %m", filename
, line
);
395 continuation
= mfree(continuation
);
412 if (flags
& CONFIG_PARSE_WARN
)
413 log_warning_errno(r
, "%s:%u: Failed to parse file: %m", filename
, line
);
421 static int config_parse_many_files(
422 const char *conf_file
,
424 const char *sections
,
425 ConfigItemLookup lookup
,
427 ConfigParseFlags flags
,
434 r
= config_parse(NULL
, conf_file
, NULL
, sections
, lookup
, table
, flags
, userdata
);
439 STRV_FOREACH(fn
, files
) {
440 r
= config_parse(NULL
, *fn
, NULL
, sections
, lookup
, table
, flags
, userdata
);
448 /* Parse each config file in the directories specified as nulstr. */
449 int config_parse_many_nulstr(
450 const char *conf_file
,
451 const char *conf_file_dirs
,
452 const char *sections
,
453 ConfigItemLookup lookup
,
455 ConfigParseFlags flags
,
458 _cleanup_strv_free_
char **files
= NULL
;
461 r
= conf_files_list_nulstr(&files
, ".conf", NULL
, 0, conf_file_dirs
);
465 return config_parse_many_files(conf_file
, files
, sections
, lookup
, table
, flags
, userdata
);
468 /* Parse each config file in the directories specified as strv. */
469 int config_parse_many(
470 const char *conf_file
,
471 const char* const* conf_file_dirs
,
472 const char *dropin_dirname
,
473 const char *sections
,
474 ConfigItemLookup lookup
,
476 ConfigParseFlags flags
,
479 _cleanup_strv_free_
char **dropin_dirs
= NULL
;
480 _cleanup_strv_free_
char **files
= NULL
;
484 suffix
= strjoina("/", dropin_dirname
);
485 r
= strv_extend_strv_concat(&dropin_dirs
, (char**) conf_file_dirs
, suffix
);
489 r
= conf_files_list_strv(&files
, ".conf", NULL
, 0, (const char* const*) dropin_dirs
);
493 return config_parse_many_files(conf_file
, files
, sections
, lookup
, table
, flags
, userdata
);
496 #define DEFINE_PARSER(type, vartype, conv_func) \
497 DEFINE_CONFIG_PARSE_PTR(config_parse_##type, conv_func, vartype, "Failed to parse " #type " value")
499 DEFINE_PARSER(int, int, safe_atoi
);
500 DEFINE_PARSER(long, long, safe_atoli
);
501 DEFINE_PARSER(uint8
, uint8_t, safe_atou8
);
502 DEFINE_PARSER(uint16
, uint16_t, safe_atou16
);
503 DEFINE_PARSER(uint32
, uint32_t, safe_atou32
);
504 DEFINE_PARSER(uint64
, uint64_t, safe_atou64
);
505 DEFINE_PARSER(unsigned, unsigned, safe_atou
);
506 DEFINE_PARSER(double, double, safe_atod
);
507 DEFINE_PARSER(nsec
, nsec_t
, parse_nsec
);
508 DEFINE_PARSER(sec
, usec_t
, parse_sec
);
509 DEFINE_PARSER(sec_def_infinity
, usec_t
, parse_sec_def_infinity
);
510 DEFINE_PARSER(mode
, mode_t
, parse_mode
);
512 int config_parse_iec_size(const char* unit
,
513 const char *filename
,
516 unsigned section_line
,
532 r
= parse_size(rvalue
, 1024, &v
);
533 if (r
>= 0 && (uint64_t) (size_t) v
!= v
)
536 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value '%s', ignoring: %m", rvalue
);
544 int config_parse_si_size(
546 const char *filename
,
549 unsigned section_line
,
565 r
= parse_size(rvalue
, 1000, &v
);
566 if (r
>= 0 && (uint64_t) (size_t) v
!= v
)
569 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value '%s', ignoring: %m", rvalue
);
577 int config_parse_iec_uint64(
579 const char *filename
,
582 unsigned section_line
,
589 uint64_t *bytes
= data
;
597 r
= parse_size(rvalue
, 1024, bytes
);
599 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value, ignoring: %s", rvalue
);
604 int config_parse_bool(const char* unit
,
605 const char *filename
,
608 unsigned section_line
,
624 k
= parse_boolean(rvalue
);
626 log_syntax(unit
, LOG_ERR
, filename
, line
, k
,
627 "Failed to parse boolean value%s: %s",
628 fatal
? "" : ", ignoring", rvalue
);
629 return fatal
? -ENOEXEC
: 0;
636 int config_parse_tristate(
638 const char *filename
,
641 unsigned section_line
,
655 /* A tristate is pretty much a boolean, except that it can
656 * also take the special value -1, indicating "uninitialized",
657 * much like NULL is for a pointer type. */
659 k
= parse_boolean(rvalue
);
661 log_syntax(unit
, LOG_ERR
, filename
, line
, k
, "Failed to parse boolean value, ignoring: %s", rvalue
);
669 int config_parse_string(
671 const char *filename
,
674 unsigned section_line
,
688 if (free_and_strdup(s
, empty_to_null(rvalue
)) < 0)
694 int config_parse_path(
696 const char *filename
,
699 unsigned section_line
,
706 _cleanup_free_
char *n
= NULL
;
723 r
= path_simplify_and_warn(n
, PATH_CHECK_ABSOLUTE
| (fatal
? PATH_CHECK_FATAL
: 0), unit
, filename
, line
, lvalue
);
725 return fatal
? -ENOEXEC
: 0;
728 return free_and_replace(*s
, n
);
731 int config_parse_strv(
733 const char *filename
,
736 unsigned section_line
,
751 if (isempty(rvalue
)) {
752 *sv
= strv_free(*sv
);
759 r
= extract_first_word(&rvalue
, &word
, NULL
, EXTRACT_QUOTES
|EXTRACT_RETAIN_ESCAPE
);
765 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
769 r
= strv_consume(sv
, word
);
777 int config_parse_warn_compat(
779 const char *filename
,
782 unsigned section_line
,
789 Disabled reason
= ltype
;
793 case DISABLED_CONFIGURATION
:
794 log_syntax(unit
, LOG_DEBUG
, filename
, line
, 0,
795 "Support for option %s= has been disabled at compile time and it is ignored", lvalue
);
798 case DISABLED_LEGACY
:
799 log_syntax(unit
, LOG_INFO
, filename
, line
, 0,
800 "Support for option %s= has been removed and it is ignored", lvalue
);
803 case DISABLED_EXPERIMENTAL
:
804 log_syntax(unit
, LOG_INFO
, filename
, line
, 0,
805 "Support for option %s= has not yet been enabled and it is ignored", lvalue
);
812 int config_parse_log_facility(
814 const char *filename
,
817 unsigned section_line
,
831 x
= log_facility_unshifted_from_string(rvalue
);
833 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse log facility, ignoring: %s", rvalue
);
837 *o
= (x
<< 3) | LOG_PRI(*o
);
842 int config_parse_log_level(
844 const char *filename
,
847 unsigned section_line
,
861 x
= log_level_from_string(rvalue
);
863 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse log level, ignoring: %s", rvalue
);
867 if (*o
< 0) /* if it wasn't initialized so far, assume zero facility */
870 *o
= (*o
& LOG_FACMASK
) | x
;
875 int config_parse_signal(
877 const char *filename
,
880 unsigned section_line
,
894 r
= signal_from_string(rvalue
);
896 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse signal name, ignoring: %s", rvalue
);
904 int config_parse_personality(
906 const char *filename
,
909 unsigned section_line
,
916 unsigned long *personality
= data
, p
;
924 p
= PERSONALITY_INVALID
;
926 p
= personality_from_string(rvalue
);
927 if (p
== PERSONALITY_INVALID
) {
928 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse personality, ignoring: %s", rvalue
);
937 int config_parse_ifname(
939 const char *filename
,
942 unsigned section_line
,
957 if (isempty(rvalue
)) {
962 if (!ifname_valid(rvalue
)) {
963 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue
);
967 r
= free_and_strdup(s
, rvalue
);
974 int config_parse_ip_port(
976 const char *filename
,
979 unsigned section_line
,
995 if (isempty(rvalue
)) {
1000 r
= parse_ip_port(rvalue
, &port
);
1002 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse port '%s'.", rvalue
);
1011 int config_parse_mtu(
1013 const char *filename
,
1015 const char *section
,
1016 unsigned section_line
,
1023 uint32_t *mtu
= data
;
1029 r
= parse_mtu(ltype
, rvalue
, mtu
);
1031 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1032 "Maximum transfer unit (MTU) value out of range. Permitted range is %" PRIu32
"…%" PRIu32
", ignoring: %s",
1033 (uint32_t) (ltype
== AF_INET6
? IPV6_MIN_MTU
: IPV4_MIN_MTU
), (uint32_t) UINT32_MAX
,
1038 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1039 "Failed to parse MTU value '%s', ignoring: %m", rvalue
);
1046 int config_parse_rlimit(
1048 const char *filename
,
1050 const char *section
,
1051 unsigned section_line
,
1058 struct rlimit
**rl
= data
, d
= {};
1064 r
= rlimit_parse(ltype
, rvalue
, &d
);
1066 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Soft resource limit chosen higher than hard limit, ignoring: %s", rvalue
);
1070 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse resource value, ignoring: %s", rvalue
);
1077 rl
[ltype
] = newdup(struct rlimit
, &d
, 1);
1085 int config_parse_permille(const char* unit
,
1086 const char *filename
,
1088 const char *section
,
1089 unsigned section_line
,
1096 unsigned *permille
= data
;
1104 r
= parse_permille(rvalue
);
1106 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1107 "Failed to parse permille value, ignoring: %s", rvalue
);
1111 *permille
= (unsigned) r
;