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 "nulstr-util.h"
23 #include "parse-util.h"
24 #include "path-util.h"
25 #include "process-util.h"
26 #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, "Unknown lvalue '%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
)) {
225 if (!(flags
& CONFIG_PARSE_RELAXED
) && !startswith(n
, "X-"))
226 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Unknown section '%s'. Ignoring.", n
);
229 *section
= mfree(*section
);
231 *section_ignored
= true;
233 free_and_replace(*section
, n
);
234 *section_line
= line
;
235 *section_ignored
= false;
241 if (sections
&& !*section
) {
243 if (!(flags
& CONFIG_PARSE_RELAXED
) && !*section_ignored
)
244 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Assignment outside of section. Ignoring.");
251 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Missing '='.");
258 return next_assignment(unit
,
271 /* Go through the file and parse each line */
272 int config_parse(const char *unit
,
273 const char *filename
,
275 const char *sections
,
276 ConfigItemLookup lookup
,
278 ConfigParseFlags flags
,
281 _cleanup_free_
char *section
= NULL
, *continuation
= NULL
;
282 _cleanup_fclose_
FILE *ours
= NULL
;
283 unsigned line
= 0, section_line
= 0;
284 bool section_ignored
= false;
291 f
= ours
= fopen(filename
, "re");
293 /* Only log on request, except for ENOENT,
294 * since we return 0 to the caller. */
295 if ((flags
& CONFIG_PARSE_WARN
) || errno
== ENOENT
)
296 log_full_errno(errno
== ENOENT
? LOG_DEBUG
: LOG_ERR
, errno
,
297 "Failed to open configuration file '%s': %m", filename
);
298 return errno
== ENOENT
? 0 : -errno
;
302 fd_warn_permissions(filename
, fileno(f
));
305 _cleanup_free_
char *buf
= NULL
;
306 bool escaped
= false;
309 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
313 if (flags
& CONFIG_PARSE_WARN
)
314 log_error_errno(r
, "%s:%u: Line too long", filename
, line
);
319 if (CONFIG_PARSE_WARN
)
320 log_error_errno(r
, "%s:%u: Error while reading configuration file: %m", filename
, line
);
325 l
= skip_leading_chars(buf
, WHITESPACE
);
326 if (*l
!= '\0' && strchr(COMMENTS
, *l
))
330 if (!(flags
& CONFIG_PARSE_REFUSE_BOM
)) {
333 q
= startswith(buf
, UTF8_BYTE_ORDER_MARK
);
336 flags
|= CONFIG_PARSE_REFUSE_BOM
;
341 if (strlen(continuation
) + strlen(l
) > LONG_LINE_MAX
) {
342 if (flags
& CONFIG_PARSE_WARN
)
343 log_error("%s:%u: Continuation line too long", filename
, line
);
347 if (!strextend(&continuation
, l
, NULL
)) {
348 if (flags
& CONFIG_PARSE_WARN
)
357 for (e
= p
; *e
; e
++) {
368 continuation
= strdup(l
);
370 if (flags
& CONFIG_PARSE_WARN
)
392 if (flags
& CONFIG_PARSE_WARN
)
393 log_warning_errno(r
, "%s:%u: Failed to parse file: %m", filename
, line
);
397 continuation
= mfree(continuation
);
414 if (flags
& CONFIG_PARSE_WARN
)
415 log_warning_errno(r
, "%s:%u: Failed to parse file: %m", filename
, line
);
423 static int config_parse_many_files(
424 const char *conf_file
,
426 const char *sections
,
427 ConfigItemLookup lookup
,
429 ConfigParseFlags flags
,
436 r
= config_parse(NULL
, conf_file
, NULL
, sections
, lookup
, table
, flags
, userdata
);
441 STRV_FOREACH(fn
, files
) {
442 r
= config_parse(NULL
, *fn
, NULL
, sections
, lookup
, table
, flags
, userdata
);
450 /* Parse each config file in the directories specified as nulstr. */
451 int config_parse_many_nulstr(
452 const char *conf_file
,
453 const char *conf_file_dirs
,
454 const char *sections
,
455 ConfigItemLookup lookup
,
457 ConfigParseFlags flags
,
460 _cleanup_strv_free_
char **files
= NULL
;
463 r
= conf_files_list_nulstr(&files
, ".conf", NULL
, 0, conf_file_dirs
);
467 return config_parse_many_files(conf_file
, files
, sections
, lookup
, table
, flags
, userdata
);
470 /* Parse each config file in the directories specified as strv. */
471 int config_parse_many(
472 const char *conf_file
,
473 const char* const* conf_file_dirs
,
474 const char *dropin_dirname
,
475 const char *sections
,
476 ConfigItemLookup lookup
,
478 ConfigParseFlags flags
,
481 _cleanup_strv_free_
char **dropin_dirs
= NULL
;
482 _cleanup_strv_free_
char **files
= NULL
;
486 suffix
= strjoina("/", dropin_dirname
);
487 r
= strv_extend_strv_concat(&dropin_dirs
, (char**) conf_file_dirs
, suffix
);
491 r
= conf_files_list_strv(&files
, ".conf", NULL
, 0, (const char* const*) dropin_dirs
);
495 return config_parse_many_files(conf_file
, files
, sections
, lookup
, table
, flags
, userdata
);
498 #define DEFINE_PARSER(type, vartype, conv_func) \
499 DEFINE_CONFIG_PARSE_PTR(config_parse_##type, conv_func, vartype, "Failed to parse " #type " value")
501 DEFINE_PARSER(int, int, safe_atoi
);
502 DEFINE_PARSER(long, long, safe_atoli
);
503 DEFINE_PARSER(uint8
, uint8_t, safe_atou8
);
504 DEFINE_PARSER(uint16
, uint16_t, safe_atou16
);
505 DEFINE_PARSER(uint32
, uint32_t, safe_atou32
);
506 DEFINE_PARSER(uint64
, uint64_t, safe_atou64
);
507 DEFINE_PARSER(unsigned, unsigned, safe_atou
);
508 DEFINE_PARSER(double, double, safe_atod
);
509 DEFINE_PARSER(nsec
, nsec_t
, parse_nsec
);
510 DEFINE_PARSER(sec
, usec_t
, parse_sec
);
511 DEFINE_PARSER(sec_def_infinity
, usec_t
, parse_sec_def_infinity
);
512 DEFINE_PARSER(mode
, mode_t
, parse_mode
);
514 int config_parse_iec_size(const char* unit
,
515 const char *filename
,
518 unsigned section_line
,
534 r
= parse_size(rvalue
, 1024, &v
);
535 if (r
>= 0 && (uint64_t) (size_t) v
!= v
)
538 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value '%s', ignoring: %m", rvalue
);
546 int config_parse_si_size(
548 const char *filename
,
551 unsigned section_line
,
567 r
= parse_size(rvalue
, 1000, &v
);
568 if (r
>= 0 && (uint64_t) (size_t) v
!= v
)
571 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value '%s', ignoring: %m", rvalue
);
579 int config_parse_iec_uint64(
581 const char *filename
,
584 unsigned section_line
,
591 uint64_t *bytes
= data
;
599 r
= parse_size(rvalue
, 1024, bytes
);
601 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value, ignoring: %s", rvalue
);
606 int config_parse_bool(const char* unit
,
607 const char *filename
,
610 unsigned section_line
,
626 k
= parse_boolean(rvalue
);
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;
638 int config_parse_tristate(
640 const char *filename
,
643 unsigned section_line
,
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. */
661 k
= parse_boolean(rvalue
);
663 log_syntax(unit
, LOG_ERR
, filename
, line
, k
, "Failed to parse boolean value, ignoring: %s", rvalue
);
671 int config_parse_string(
673 const char *filename
,
676 unsigned section_line
,
690 if (free_and_strdup(s
, empty_to_null(rvalue
)) < 0)
696 int config_parse_path(
698 const char *filename
,
701 unsigned section_line
,
708 _cleanup_free_
char *n
= NULL
;
725 r
= path_simplify_and_warn(n
, PATH_CHECK_ABSOLUTE
| (fatal
? PATH_CHECK_FATAL
: 0), unit
, filename
, line
, lvalue
);
727 return fatal
? -ENOEXEC
: 0;
730 return free_and_replace(*s
, n
);
733 int config_parse_strv(
735 const char *filename
,
738 unsigned section_line
,
753 if (isempty(rvalue
)) {
754 *sv
= strv_free(*sv
);
761 r
= extract_first_word(&rvalue
, &word
, NULL
, EXTRACT_UNQUOTE
);
767 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
771 r
= strv_consume(sv
, word
);
779 int config_parse_warn_compat(
781 const char *filename
,
784 unsigned section_line
,
791 Disabled reason
= ltype
;
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
);
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
);
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
);
814 int config_parse_log_facility(
816 const char *filename
,
819 unsigned section_line
,
833 x
= log_facility_unshifted_from_string(rvalue
);
835 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse log facility, ignoring: %s", rvalue
);
839 *o
= (x
<< 3) | LOG_PRI(*o
);
844 int config_parse_log_level(
846 const char *filename
,
849 unsigned section_line
,
863 x
= log_level_from_string(rvalue
);
865 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse log level, ignoring: %s", rvalue
);
869 if (*o
< 0) /* if it wasn't initialized so far, assume zero facility */
872 *o
= (*o
& LOG_FACMASK
) | x
;
877 int config_parse_signal(
879 const char *filename
,
882 unsigned section_line
,
896 r
= signal_from_string(rvalue
);
898 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse signal name, ignoring: %s", rvalue
);
906 int config_parse_personality(
908 const char *filename
,
911 unsigned section_line
,
918 unsigned long *personality
= data
, p
;
926 p
= PERSONALITY_INVALID
;
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
);
939 int config_parse_ifname(
941 const char *filename
,
944 unsigned section_line
,
959 if (isempty(rvalue
)) {
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
);
969 r
= free_and_strdup(s
, rvalue
);
976 int config_parse_ip_port(
978 const char *filename
,
981 unsigned section_line
,
997 if (isempty(rvalue
)) {
1002 r
= parse_ip_port(rvalue
, &port
);
1004 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse port '%s'.", rvalue
);
1013 int config_parse_mtu(
1015 const char *filename
,
1017 const char *section
,
1018 unsigned section_line
,
1025 uint32_t *mtu
= data
;
1031 r
= parse_mtu(ltype
, rvalue
, mtu
);
1033 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1034 "Maximum transfer unit (MTU) value out of range. Permitted range is %" PRIu32
"…%" PRIu32
", ignoring: %s",
1035 (uint32_t) (ltype
== AF_INET6
? IPV6_MIN_MTU
: IPV4_MIN_MTU
), (uint32_t) UINT32_MAX
,
1040 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1041 "Failed to parse MTU value '%s', ignoring: %m", rvalue
);
1048 int config_parse_rlimit(
1050 const char *filename
,
1052 const char *section
,
1053 unsigned section_line
,
1060 struct rlimit
**rl
= data
, d
= {};
1066 r
= rlimit_parse(ltype
, rvalue
, &d
);
1068 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Soft resource limit chosen higher than hard limit, ignoring: %s", rvalue
);
1072 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse resource value, ignoring: %s", rvalue
);
1079 rl
[ltype
] = newdup(struct rlimit
, &d
, 1);
1087 int config_parse_permille(const char* unit
,
1088 const char *filename
,
1090 const char *section
,
1091 unsigned section_line
,
1098 unsigned *permille
= data
;
1106 r
= parse_permille(rvalue
);
1108 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1109 "Failed to parse permille value, ignoring: %s", rvalue
);
1113 *permille
= (unsigned) r
;