1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include "alloc-util.h"
11 #include "conf-files.h"
12 #include "conf-parser.h"
14 #include "extract-word.h"
20 #include "missing_network.h"
21 #include "nulstr-util.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,
141 "Unknown key name '%s' in section '%s', ignoring.", lvalue
, section
);
146 /* Parse a single logical line */
147 static int parse_line(
149 const char *filename
,
151 const char *sections
,
152 ConfigItemLookup lookup
,
154 ConfigParseFlags flags
,
156 unsigned *section_line
,
157 bool *section_ignored
,
175 include
= first_word(l
, ".include");
177 _cleanup_free_
char *fn
= NULL
;
179 /* .includes are a bad idea, we only support them here
180 * for historical reasons. They create cyclic include
181 * problems and make it difficult to detect
182 * configuration file changes with an easy
183 * stat(). Better approaches, such as .d/ drop-in
186 * Support for them should be eventually removed. */
188 if (!(flags
& CONFIG_PARSE_ALLOW_INCLUDE
)) {
189 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, ".include not allowed here. Ignoring.");
193 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
194 ".include directives are deprecated, and support for them will be removed in a future version of systemd. "
195 "Please use drop-in files instead.");
197 fn
= file_in_same_dir(filename
, strstrip(include
));
201 return config_parse(unit
, fn
, NULL
, sections
, lookup
, table
, flags
, userdata
);
204 if (!utf8_is_valid(l
))
205 return log_syntax_invalid_utf8(unit
, LOG_WARNING
, filename
, line
, l
);
215 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid section header '%s'", l
);
219 n
= strndup(l
+1, k
-2);
223 if (sections
&& !nulstr_contains(sections
, n
)) {
224 bool ignore
= flags
& CONFIG_PARSE_RELAXED
;
227 ignore
= ignore
|| startswith(n
, "X-");
230 NULSTR_FOREACH(t
, sections
)
231 if (streq_ptr(n
, startswith(t
, "-"))) {
237 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Unknown section '%s'. Ignoring.", n
);
240 *section
= mfree(*section
);
242 *section_ignored
= true;
244 free_and_replace(*section
, n
);
245 *section_line
= line
;
246 *section_ignored
= false;
252 if (sections
&& !*section
) {
253 if (!(flags
& CONFIG_PARSE_RELAXED
) && !*section_ignored
)
254 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Assignment outside of section. Ignoring.");
261 return log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
262 "Missing '=', ignoring line.");
264 return log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
265 "Missing key name before '=', ignoring line.");
270 return next_assignment(unit
,
283 /* Go through the file and parse each line */
284 int config_parse(const char *unit
,
285 const char *filename
,
287 const char *sections
,
288 ConfigItemLookup lookup
,
290 ConfigParseFlags flags
,
293 _cleanup_free_
char *section
= NULL
, *continuation
= NULL
;
294 _cleanup_fclose_
FILE *ours
= NULL
;
295 unsigned line
= 0, section_line
= 0;
296 bool section_ignored
= false, bom_seen
= false;
303 f
= ours
= fopen(filename
, "re");
305 /* Only log on request, except for ENOENT,
306 * since we return 0 to the caller. */
307 if ((flags
& CONFIG_PARSE_WARN
) || errno
== ENOENT
)
308 log_full_errno(errno
== ENOENT
? LOG_DEBUG
: LOG_ERR
, errno
,
309 "Failed to open configuration file '%s': %m", filename
);
310 return errno
== ENOENT
? 0 : -errno
;
315 if (fd
>= 0) /* stream might not have an fd, let's be careful hence */
316 fd_warn_permissions(filename
, fd
);
319 _cleanup_free_
char *buf
= NULL
;
320 bool escaped
= false;
323 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
327 if (flags
& CONFIG_PARSE_WARN
)
328 log_error_errno(r
, "%s:%u: Line too long", filename
, line
);
333 if (CONFIG_PARSE_WARN
)
334 log_error_errno(r
, "%s:%u: Error while reading configuration file: %m", filename
, line
);
341 l
= skip_leading_chars(buf
, WHITESPACE
);
342 if (*l
!= '\0' && strchr(COMMENTS
, *l
))
349 q
= startswith(buf
, UTF8_BYTE_ORDER_MARK
);
357 if (strlen(continuation
) + strlen(l
) > LONG_LINE_MAX
) {
358 if (flags
& CONFIG_PARSE_WARN
)
359 log_error("%s:%u: Continuation line too long", filename
, line
);
363 if (!strextend(&continuation
, l
, NULL
)) {
364 if (flags
& CONFIG_PARSE_WARN
)
373 for (e
= p
; *e
; e
++) {
384 continuation
= strdup(l
);
386 if (flags
& CONFIG_PARSE_WARN
)
408 if (flags
& CONFIG_PARSE_WARN
)
409 log_warning_errno(r
, "%s:%u: Failed to parse file: %m", filename
, line
);
413 continuation
= mfree(continuation
);
430 if (flags
& CONFIG_PARSE_WARN
)
431 log_warning_errno(r
, "%s:%u: Failed to parse file: %m", filename
, line
);
439 static int config_parse_many_files(
440 const char *conf_file
,
442 const char *sections
,
443 ConfigItemLookup lookup
,
445 ConfigParseFlags flags
,
452 r
= config_parse(NULL
, conf_file
, NULL
, sections
, lookup
, table
, flags
, userdata
);
457 STRV_FOREACH(fn
, files
) {
458 r
= config_parse(NULL
, *fn
, NULL
, sections
, lookup
, table
, flags
, userdata
);
466 /* Parse each config file in the directories specified as nulstr. */
467 int config_parse_many_nulstr(
468 const char *conf_file
,
469 const char *conf_file_dirs
,
470 const char *sections
,
471 ConfigItemLookup lookup
,
473 ConfigParseFlags flags
,
476 _cleanup_strv_free_
char **files
= NULL
;
479 r
= conf_files_list_nulstr(&files
, ".conf", NULL
, 0, conf_file_dirs
);
483 return config_parse_many_files(conf_file
, files
, sections
, lookup
, table
, flags
, userdata
);
486 /* Parse each config file in the directories specified as strv. */
487 int config_parse_many(
488 const char *conf_file
,
489 const char* const* conf_file_dirs
,
490 const char *dropin_dirname
,
491 const char *sections
,
492 ConfigItemLookup lookup
,
494 ConfigParseFlags flags
,
497 _cleanup_strv_free_
char **dropin_dirs
= NULL
;
498 _cleanup_strv_free_
char **files
= NULL
;
502 suffix
= strjoina("/", dropin_dirname
);
503 r
= strv_extend_strv_concat(&dropin_dirs
, (char**) conf_file_dirs
, suffix
);
507 r
= conf_files_list_strv(&files
, ".conf", NULL
, 0, (const char* const*) dropin_dirs
);
511 return config_parse_many_files(conf_file
, files
, sections
, lookup
, table
, flags
, userdata
);
514 #define DEFINE_PARSER(type, vartype, conv_func) \
515 DEFINE_CONFIG_PARSE_PTR(config_parse_##type, conv_func, vartype, "Failed to parse " #type " value")
517 DEFINE_PARSER(int, int, safe_atoi
);
518 DEFINE_PARSER(long, long, safe_atoli
);
519 DEFINE_PARSER(uint8
, uint8_t, safe_atou8
);
520 DEFINE_PARSER(uint16
, uint16_t, safe_atou16
);
521 DEFINE_PARSER(uint32
, uint32_t, safe_atou32
);
522 DEFINE_PARSER(int32
, int32_t, safe_atoi32
);
523 DEFINE_PARSER(uint64
, uint64_t, safe_atou64
);
524 DEFINE_PARSER(unsigned, unsigned, safe_atou
);
525 DEFINE_PARSER(double, double, safe_atod
);
526 DEFINE_PARSER(nsec
, nsec_t
, parse_nsec
);
527 DEFINE_PARSER(sec
, usec_t
, parse_sec
);
528 DEFINE_PARSER(sec_def_infinity
, usec_t
, parse_sec_def_infinity
);
529 DEFINE_PARSER(mode
, mode_t
, parse_mode
);
531 int config_parse_iec_size(const char* unit
,
532 const char *filename
,
535 unsigned section_line
,
551 r
= parse_size(rvalue
, 1024, &v
);
552 if (r
>= 0 && (uint64_t) (size_t) v
!= v
)
555 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value '%s', ignoring: %m", rvalue
);
563 int config_parse_si_uint64(
565 const char *filename
,
568 unsigned section_line
,
583 r
= parse_size(rvalue
, 1000, sz
);
585 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value '%s', ignoring: %m", rvalue
);
592 int config_parse_iec_uint64(
594 const char *filename
,
597 unsigned section_line
,
604 uint64_t *bytes
= data
;
612 r
= parse_size(rvalue
, 1024, bytes
);
614 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value, ignoring: %s", rvalue
);
619 int config_parse_bool(const char* unit
,
620 const char *filename
,
623 unsigned section_line
,
639 k
= parse_boolean(rvalue
);
641 log_syntax(unit
, LOG_ERR
, filename
, line
, k
,
642 "Failed to parse boolean value%s: %s",
643 fatal
? "" : ", ignoring", rvalue
);
644 return fatal
? -ENOEXEC
: 0;
651 int config_parse_tristate(
653 const char *filename
,
656 unsigned section_line
,
670 /* A tristate is pretty much a boolean, except that it can
671 * also take the special value -1, indicating "uninitialized",
672 * much like NULL is for a pointer type. */
674 k
= parse_boolean(rvalue
);
676 log_syntax(unit
, LOG_ERR
, filename
, line
, k
, "Failed to parse boolean value, ignoring: %s", rvalue
);
684 int config_parse_string(
686 const char *filename
,
689 unsigned section_line
,
703 if (free_and_strdup(s
, empty_to_null(rvalue
)) < 0)
709 int config_parse_path(
711 const char *filename
,
714 unsigned section_line
,
721 _cleanup_free_
char *n
= NULL
;
738 r
= path_simplify_and_warn(n
, PATH_CHECK_ABSOLUTE
| (fatal
? PATH_CHECK_FATAL
: 0), unit
, filename
, line
, lvalue
);
740 return fatal
? -ENOEXEC
: 0;
743 return free_and_replace(*s
, n
);
746 int config_parse_strv(
748 const char *filename
,
751 unsigned section_line
,
766 if (isempty(rvalue
)) {
767 *sv
= strv_free(*sv
);
774 r
= extract_first_word(&rvalue
, &word
, NULL
, EXTRACT_UNQUOTE
|EXTRACT_RETAIN_ESCAPE
);
780 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
784 r
= strv_consume(sv
, word
);
792 int config_parse_warn_compat(
794 const char *filename
,
797 unsigned section_line
,
804 Disabled reason
= ltype
;
808 case DISABLED_CONFIGURATION
:
809 log_syntax(unit
, LOG_DEBUG
, filename
, line
, 0,
810 "Support for option %s= has been disabled at compile time and it is ignored", lvalue
);
813 case DISABLED_LEGACY
:
814 log_syntax(unit
, LOG_INFO
, filename
, line
, 0,
815 "Support for option %s= has been removed and it is ignored", lvalue
);
818 case DISABLED_EXPERIMENTAL
:
819 log_syntax(unit
, LOG_INFO
, filename
, line
, 0,
820 "Support for option %s= has not yet been enabled and it is ignored", lvalue
);
827 int config_parse_log_facility(
829 const char *filename
,
832 unsigned section_line
,
846 x
= log_facility_unshifted_from_string(rvalue
);
848 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse log facility, ignoring: %s", rvalue
);
852 *o
= (x
<< 3) | LOG_PRI(*o
);
857 int config_parse_log_level(
859 const char *filename
,
862 unsigned section_line
,
876 x
= log_level_from_string(rvalue
);
878 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse log level, ignoring: %s", rvalue
);
882 if (*o
< 0) /* if it wasn't initialized so far, assume zero facility */
885 *o
= (*o
& LOG_FACMASK
) | x
;
890 int config_parse_signal(
892 const char *filename
,
895 unsigned section_line
,
909 r
= signal_from_string(rvalue
);
911 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse signal name, ignoring: %s", rvalue
);
919 int config_parse_personality(
921 const char *filename
,
924 unsigned section_line
,
931 unsigned long *personality
= data
, p
;
939 p
= PERSONALITY_INVALID
;
941 p
= personality_from_string(rvalue
);
942 if (p
== PERSONALITY_INVALID
) {
943 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse personality, ignoring: %s", rvalue
);
952 int config_parse_ifname(
954 const char *filename
,
957 unsigned section_line
,
972 if (isempty(rvalue
)) {
977 if (!ifname_valid(rvalue
)) {
978 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue
);
982 r
= free_and_strdup(s
, rvalue
);
989 int config_parse_ifnames(
991 const char *filename
,
994 unsigned section_line
,
1001 _cleanup_strv_free_
char **names
= NULL
;
1011 if (isempty(rvalue
)) {
1018 _cleanup_free_
char *word
= NULL
;
1020 r
= extract_first_word(&p
, &word
, NULL
, 0);
1022 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1023 "Failed to extract interface name, ignoring assignment: %s",
1030 if (!ifname_valid_full(word
, ltype
)) {
1031 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1032 "Interface name is not valid or too long, ignoring assignment: %s",
1037 r
= strv_consume(&names
, TAKE_PTR(word
));
1042 r
= strv_extend_strv(s
, names
, true);
1049 int config_parse_ip_port(
1051 const char *filename
,
1053 const char *section
,
1054 unsigned section_line
,
1070 if (isempty(rvalue
)) {
1075 r
= parse_ip_port(rvalue
, &port
);
1077 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse port '%s'.", rvalue
);
1086 int config_parse_mtu(
1088 const char *filename
,
1090 const char *section
,
1091 unsigned section_line
,
1098 uint32_t *mtu
= data
;
1104 r
= parse_mtu(ltype
, rvalue
, mtu
);
1106 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1107 "Maximum transfer unit (MTU) value out of range. Permitted range is %" PRIu32
"…%" PRIu32
", ignoring: %s",
1108 (uint32_t) (ltype
== AF_INET6
? IPV6_MIN_MTU
: IPV4_MIN_MTU
), (uint32_t) UINT32_MAX
,
1113 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1114 "Failed to parse MTU value '%s', ignoring: %m", rvalue
);
1121 int config_parse_rlimit(
1123 const char *filename
,
1125 const char *section
,
1126 unsigned section_line
,
1133 struct rlimit
**rl
= data
, d
= {};
1139 r
= rlimit_parse(ltype
, rvalue
, &d
);
1141 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Soft resource limit chosen higher than hard limit, ignoring: %s", rvalue
);
1145 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse resource value, ignoring: %s", rvalue
);
1152 rl
[ltype
] = newdup(struct rlimit
, &d
, 1);
1160 int config_parse_permille(const char* unit
,
1161 const char *filename
,
1163 const char *section
,
1164 unsigned section_line
,
1171 unsigned *permille
= data
;
1179 r
= parse_permille(rvalue
);
1181 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1182 "Failed to parse permille value, ignoring: %s", rvalue
);
1186 *permille
= (unsigned) r
;
1191 int config_parse_vlanprotocol(const char* unit
,
1192 const char *filename
,
1194 const char *section
,
1195 unsigned section_line
,
1201 int *vlan_protocol
= data
;
1205 if (isempty(rvalue
)) {
1206 *vlan_protocol
= -1;
1210 if (STR_IN_SET(rvalue
, "802.1ad", "802.1AD"))
1211 *vlan_protocol
= ETH_P_8021AD
;
1212 else if (STR_IN_SET(rvalue
, "802.1q", "802.1Q"))
1213 *vlan_protocol
= ETH_P_8021Q
;
1215 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1216 "Failed to parse VLAN protocol value, ignoring: %s", rvalue
);