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"
27 #include "signal-util.h"
28 #include "socket-util.h"
29 #include "string-util.h"
31 #include "syslog-util.h"
32 #include "time-util.h"
35 int config_item_table_lookup(
39 ConfigParserCallback
*func
,
44 const ConfigTableItem
*t
;
52 for (t
= table
; t
->lvalue
; t
++) {
54 if (!streq(lvalue
, t
->lvalue
))
57 if (!streq_ptr(section
, t
->section
))
69 int config_item_perf_lookup(
73 ConfigParserCallback
*func
,
78 ConfigPerfItemLookup lookup
= (ConfigPerfItemLookup
) table
;
79 const ConfigPerfItem
*p
;
90 key
= strjoina(section
, ".", lvalue
);
91 p
= lookup(key
, strlen(key
));
93 p
= lookup(lvalue
, strlen(lvalue
));
99 *data
= (uint8_t*) userdata
+ p
->offset
;
103 /* Run the user supplied parser for an assignment */
104 static int next_assignment(
106 const char *filename
,
108 ConfigItemLookup lookup
,
111 unsigned section_line
,
114 ConfigParseFlags flags
,
117 ConfigParserCallback func
= NULL
;
128 r
= lookup(table
, section
, lvalue
, &func
, <ype
, &data
, userdata
);
133 return func(unit
, filename
, line
, section
, section_line
,
134 lvalue
, ltype
, rvalue
, data
, userdata
);
139 /* Warn about unknown non-extension fields. */
140 if (!(flags
& CONFIG_PARSE_RELAXED
) && !startswith(lvalue
, "X-"))
141 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
142 "Unknown key name '%s' in section '%s', ignoring.", lvalue
, section
);
147 /* Parse a single logical line */
148 static int parse_line(
150 const char *filename
,
152 const char *sections
,
153 ConfigItemLookup lookup
,
155 ConfigParseFlags flags
,
157 unsigned *section_line
,
158 bool *section_ignored
,
176 include
= first_word(l
, ".include");
178 _cleanup_free_
char *fn
= NULL
;
180 /* .includes are a bad idea, we only support them here
181 * for historical reasons. They create cyclic include
182 * problems and make it difficult to detect
183 * configuration file changes with an easy
184 * stat(). Better approaches, such as .d/ drop-in
187 * Support for them should be eventually removed. */
189 if (!(flags
& CONFIG_PARSE_ALLOW_INCLUDE
)) {
190 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, ".include not allowed here. Ignoring.");
194 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
195 ".include directives are deprecated, and support for them will be removed in a future version of systemd. "
196 "Please use drop-in files instead.");
198 fn
= file_in_same_dir(filename
, strstrip(include
));
202 return config_parse(unit
, fn
, NULL
, sections
, lookup
, table
, flags
, userdata
);
205 if (!utf8_is_valid(l
))
206 return log_syntax_invalid_utf8(unit
, LOG_WARNING
, filename
, line
, l
);
216 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid section header '%s'", l
);
220 n
= strndup(l
+1, k
-2);
224 if (sections
&& !nulstr_contains(sections
, n
)) {
225 bool ignore
= flags
& CONFIG_PARSE_RELAXED
;
228 ignore
= ignore
|| startswith(n
, "X-");
231 NULSTR_FOREACH(t
, sections
)
232 if (streq_ptr(n
, startswith(t
, "-"))) {
238 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Unknown section '%s'. Ignoring.", n
);
241 *section
= mfree(*section
);
243 *section_ignored
= true;
245 free_and_replace(*section
, n
);
246 *section_line
= line
;
247 *section_ignored
= false;
253 if (sections
&& !*section
) {
254 if (!(flags
& CONFIG_PARSE_RELAXED
) && !*section_ignored
)
255 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Assignment outside of section. Ignoring.");
262 return log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
263 "Missing '=', ignoring line.");
265 return log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
266 "Missing key name before '=', ignoring line.");
271 return next_assignment(unit
,
284 /* Go through the file and parse each line */
285 int config_parse(const char *unit
,
286 const char *filename
,
288 const char *sections
,
289 ConfigItemLookup lookup
,
291 ConfigParseFlags flags
,
294 _cleanup_free_
char *section
= NULL
, *continuation
= NULL
;
295 _cleanup_fclose_
FILE *ours
= NULL
;
296 unsigned line
= 0, section_line
= 0;
297 bool section_ignored
= false, bom_seen
= false;
304 f
= ours
= fopen(filename
, "re");
306 /* Only log on request, except for ENOENT,
307 * since we return 0 to the caller. */
308 if ((flags
& CONFIG_PARSE_WARN
) || errno
== ENOENT
)
309 log_full_errno(errno
== ENOENT
? LOG_DEBUG
: LOG_ERR
, errno
,
310 "Failed to open configuration file '%s': %m", filename
);
311 return errno
== ENOENT
? 0 : -errno
;
316 if (fd
>= 0) /* stream might not have an fd, let's be careful hence */
317 fd_warn_permissions(filename
, fd
);
320 _cleanup_free_
char *buf
= NULL
;
321 bool escaped
= false;
324 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
328 if (flags
& CONFIG_PARSE_WARN
)
329 log_error_errno(r
, "%s:%u: Line too long", filename
, line
);
334 if (CONFIG_PARSE_WARN
)
335 log_error_errno(r
, "%s:%u: Error while reading configuration file: %m", filename
, line
);
342 l
= skip_leading_chars(buf
, WHITESPACE
);
343 if (*l
!= '\0' && strchr(COMMENTS
, *l
))
350 q
= startswith(buf
, UTF8_BYTE_ORDER_MARK
);
358 if (strlen(continuation
) + strlen(l
) > LONG_LINE_MAX
) {
359 if (flags
& CONFIG_PARSE_WARN
)
360 log_error("%s:%u: Continuation line too long", filename
, line
);
364 if (!strextend(&continuation
, l
, NULL
)) {
365 if (flags
& CONFIG_PARSE_WARN
)
374 for (e
= p
; *e
; e
++) {
385 continuation
= strdup(l
);
387 if (flags
& CONFIG_PARSE_WARN
)
409 if (flags
& CONFIG_PARSE_WARN
)
410 log_warning_errno(r
, "%s:%u: Failed to parse file: %m", filename
, line
);
414 continuation
= mfree(continuation
);
431 if (flags
& CONFIG_PARSE_WARN
)
432 log_warning_errno(r
, "%s:%u: Failed to parse file: %m", filename
, line
);
440 static int config_parse_many_files(
441 const char *conf_file
,
443 const char *sections
,
444 ConfigItemLookup lookup
,
446 ConfigParseFlags flags
,
453 r
= config_parse(NULL
, conf_file
, NULL
, sections
, lookup
, table
, flags
, userdata
);
458 STRV_FOREACH(fn
, files
) {
459 r
= config_parse(NULL
, *fn
, NULL
, sections
, lookup
, table
, flags
, userdata
);
467 /* Parse each config file in the directories specified as nulstr. */
468 int config_parse_many_nulstr(
469 const char *conf_file
,
470 const char *conf_file_dirs
,
471 const char *sections
,
472 ConfigItemLookup lookup
,
474 ConfigParseFlags flags
,
477 _cleanup_strv_free_
char **files
= NULL
;
480 r
= conf_files_list_nulstr(&files
, ".conf", NULL
, 0, conf_file_dirs
);
484 return config_parse_many_files(conf_file
, files
, sections
, lookup
, table
, flags
, userdata
);
487 /* Parse each config file in the directories specified as strv. */
488 int config_parse_many(
489 const char *conf_file
,
490 const char* const* conf_file_dirs
,
491 const char *dropin_dirname
,
492 const char *sections
,
493 ConfigItemLookup lookup
,
495 ConfigParseFlags flags
,
497 char ***ret_dropins
) {
499 _cleanup_strv_free_
char **dropin_dirs
= NULL
;
500 _cleanup_strv_free_
char **files
= NULL
;
504 suffix
= strjoina("/", dropin_dirname
);
505 r
= strv_extend_strv_concat(&dropin_dirs
, (char**) conf_file_dirs
, suffix
);
509 r
= conf_files_list_strv(&files
, ".conf", NULL
, 0, (const char* const*) dropin_dirs
);
513 r
= config_parse_many_files(conf_file
, files
, sections
, lookup
, table
, flags
, userdata
);
518 *ret_dropins
= TAKE_PTR(files
);
523 #define DEFINE_PARSER(type, vartype, conv_func) \
524 DEFINE_CONFIG_PARSE_PTR(config_parse_##type, conv_func, vartype, "Failed to parse " #type " value")
526 DEFINE_PARSER(int, int, safe_atoi
);
527 DEFINE_PARSER(long, long, safe_atoli
);
528 DEFINE_PARSER(uint8
, uint8_t, safe_atou8
);
529 DEFINE_PARSER(uint16
, uint16_t, safe_atou16
);
530 DEFINE_PARSER(uint32
, uint32_t, safe_atou32
);
531 DEFINE_PARSER(int32
, int32_t, safe_atoi32
);
532 DEFINE_PARSER(uint64
, uint64_t, safe_atou64
);
533 DEFINE_PARSER(unsigned, unsigned, safe_atou
);
534 DEFINE_PARSER(double, double, safe_atod
);
535 DEFINE_PARSER(nsec
, nsec_t
, parse_nsec
);
536 DEFINE_PARSER(sec
, usec_t
, parse_sec
);
537 DEFINE_PARSER(sec_def_infinity
, usec_t
, parse_sec_def_infinity
);
538 DEFINE_PARSER(mode
, mode_t
, parse_mode
);
540 int config_parse_iec_size(const char* unit
,
541 const char *filename
,
544 unsigned section_line
,
560 r
= parse_size(rvalue
, 1024, &v
);
561 if (r
>= 0 && (uint64_t) (size_t) v
!= v
)
564 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value '%s', ignoring: %m", rvalue
);
572 int config_parse_si_uint64(
574 const char *filename
,
577 unsigned section_line
,
592 r
= parse_size(rvalue
, 1000, sz
);
594 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value '%s', ignoring: %m", rvalue
);
601 int config_parse_iec_uint64(
603 const char *filename
,
606 unsigned section_line
,
613 uint64_t *bytes
= data
;
621 r
= parse_size(rvalue
, 1024, bytes
);
623 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value, ignoring: %s", rvalue
);
628 int config_parse_bool(const char* unit
,
629 const char *filename
,
632 unsigned section_line
,
648 k
= parse_boolean(rvalue
);
650 log_syntax(unit
, LOG_ERR
, filename
, line
, k
,
651 "Failed to parse boolean value%s: %s",
652 fatal
? "" : ", ignoring", rvalue
);
653 return fatal
? -ENOEXEC
: 0;
660 int config_parse_id128(
662 const char *filename
,
665 unsigned section_line
,
672 sd_id128_t t
, *result
= data
;
679 r
= sd_id128_from_string(rvalue
, &t
);
681 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse 128bit ID/UUID, ignoring: %s", rvalue
);
682 else if (sd_id128_is_null(t
))
683 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "128bit ID/UUID is all 0, ignoring: %s", rvalue
);
689 int config_parse_tristate(
691 const char *filename
,
694 unsigned section_line
,
708 /* A tristate is pretty much a boolean, except that it can
709 * also take the special value -1, indicating "uninitialized",
710 * much like NULL is for a pointer type. */
712 k
= parse_boolean(rvalue
);
714 log_syntax(unit
, LOG_ERR
, filename
, line
, k
, "Failed to parse boolean value, ignoring: %s", rvalue
);
722 int config_parse_string(
724 const char *filename
,
727 unsigned section_line
,
741 if (free_and_strdup(s
, empty_to_null(rvalue
)) < 0)
747 int config_parse_path(
749 const char *filename
,
752 unsigned section_line
,
759 _cleanup_free_
char *n
= NULL
;
776 r
= path_simplify_and_warn(n
, PATH_CHECK_ABSOLUTE
| (fatal
? PATH_CHECK_FATAL
: 0), unit
, filename
, line
, lvalue
);
778 return fatal
? -ENOEXEC
: 0;
781 return free_and_replace(*s
, n
);
784 int config_parse_strv(
786 const char *filename
,
789 unsigned section_line
,
804 if (isempty(rvalue
)) {
805 *sv
= strv_free(*sv
);
812 r
= extract_first_word(&rvalue
, &word
, NULL
, EXTRACT_UNQUOTE
|EXTRACT_RETAIN_ESCAPE
);
818 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
822 r
= strv_consume(sv
, word
);
830 int config_parse_warn_compat(
832 const char *filename
,
835 unsigned section_line
,
842 Disabled reason
= ltype
;
846 case DISABLED_CONFIGURATION
:
847 log_syntax(unit
, LOG_DEBUG
, filename
, line
, 0,
848 "Support for option %s= has been disabled at compile time and it is ignored", lvalue
);
851 case DISABLED_LEGACY
:
852 log_syntax(unit
, LOG_INFO
, filename
, line
, 0,
853 "Support for option %s= has been removed and it is ignored", lvalue
);
856 case DISABLED_EXPERIMENTAL
:
857 log_syntax(unit
, LOG_INFO
, filename
, line
, 0,
858 "Support for option %s= has not yet been enabled and it is ignored", lvalue
);
865 int config_parse_log_facility(
867 const char *filename
,
870 unsigned section_line
,
884 x
= log_facility_unshifted_from_string(rvalue
);
886 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse log facility, ignoring: %s", rvalue
);
890 *o
= (x
<< 3) | LOG_PRI(*o
);
895 int config_parse_log_level(
897 const char *filename
,
900 unsigned section_line
,
914 x
= log_level_from_string(rvalue
);
916 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse log level, ignoring: %s", rvalue
);
920 if (*o
< 0) /* if it wasn't initialized so far, assume zero facility */
923 *o
= (*o
& LOG_FACMASK
) | x
;
928 int config_parse_signal(
930 const char *filename
,
933 unsigned section_line
,
947 r
= signal_from_string(rvalue
);
949 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse signal name, ignoring: %s", rvalue
);
957 int config_parse_personality(
959 const char *filename
,
962 unsigned section_line
,
969 unsigned long *personality
= data
, p
;
977 p
= PERSONALITY_INVALID
;
979 p
= personality_from_string(rvalue
);
980 if (p
== PERSONALITY_INVALID
) {
981 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse personality, ignoring: %s", rvalue
);
990 int config_parse_ifname(
992 const char *filename
,
995 unsigned section_line
,
1010 if (isempty(rvalue
)) {
1015 if (!ifname_valid(rvalue
)) {
1016 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue
);
1020 r
= free_and_strdup(s
, rvalue
);
1027 int config_parse_ifnames(
1029 const char *filename
,
1031 const char *section
,
1032 unsigned section_line
,
1039 _cleanup_strv_free_
char **names
= NULL
;
1049 if (isempty(rvalue
)) {
1056 _cleanup_free_
char *word
= NULL
;
1058 r
= extract_first_word(&p
, &word
, NULL
, 0);
1060 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1061 "Failed to extract interface name, ignoring assignment: %s",
1068 if (!ifname_valid_full(word
, ltype
)) {
1069 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1070 "Interface name is not valid or too long, ignoring assignment: %s",
1075 r
= strv_consume(&names
, TAKE_PTR(word
));
1080 r
= strv_extend_strv(s
, names
, true);
1087 int config_parse_ip_port(
1089 const char *filename
,
1091 const char *section
,
1092 unsigned section_line
,
1108 if (isempty(rvalue
)) {
1113 r
= parse_ip_port(rvalue
, &port
);
1115 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse port '%s'.", rvalue
);
1124 int config_parse_mtu(
1126 const char *filename
,
1128 const char *section
,
1129 unsigned section_line
,
1136 uint32_t *mtu
= data
;
1142 r
= parse_mtu(ltype
, rvalue
, mtu
);
1144 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1145 "Maximum transfer unit (MTU) value out of range. Permitted range is %" PRIu32
"…%" PRIu32
", ignoring: %s",
1146 (uint32_t) (ltype
== AF_INET6
? IPV6_MIN_MTU
: IPV4_MIN_MTU
), (uint32_t) UINT32_MAX
,
1151 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1152 "Failed to parse MTU value '%s', ignoring: %m", rvalue
);
1159 int config_parse_rlimit(
1161 const char *filename
,
1163 const char *section
,
1164 unsigned section_line
,
1171 struct rlimit
**rl
= data
, d
= {};
1177 r
= rlimit_parse(ltype
, rvalue
, &d
);
1179 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Soft resource limit chosen higher than hard limit, ignoring: %s", rvalue
);
1183 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse resource value, ignoring: %s", rvalue
);
1190 rl
[ltype
] = newdup(struct rlimit
, &d
, 1);
1198 int config_parse_permille(const char* unit
,
1199 const char *filename
,
1201 const char *section
,
1202 unsigned section_line
,
1209 unsigned *permille
= data
;
1217 r
= parse_permille(rvalue
);
1219 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1220 "Failed to parse permille value, ignoring: %s", rvalue
);
1224 *permille
= (unsigned) r
;
1229 int config_parse_vlanprotocol(const char* unit
,
1230 const char *filename
,
1232 const char *section
,
1233 unsigned section_line
,
1239 int *vlan_protocol
= data
;
1243 if (isempty(rvalue
)) {
1244 *vlan_protocol
= -1;
1248 if (STR_IN_SET(rvalue
, "802.1ad", "802.1AD"))
1249 *vlan_protocol
= ETH_P_8021AD
;
1250 else if (STR_IN_SET(rvalue
, "802.1q", "802.1Q"))
1251 *vlan_protocol
= ETH_P_8021Q
;
1253 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1254 "Failed to parse VLAN protocol value, ignoring: %s", rvalue
);