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'", 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
, *buf
))
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(mode
, mode_t
, parse_mode
);
511 int config_parse_iec_size(const char* unit
,
512 const char *filename
,
515 unsigned section_line
,
531 r
= parse_size(rvalue
, 1024, &v
);
532 if (r
>= 0 && (uint64_t) (size_t) v
!= v
)
535 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value '%s', ignoring: %m", rvalue
);
543 int config_parse_si_size(
545 const char *filename
,
548 unsigned section_line
,
564 r
= parse_size(rvalue
, 1000, &v
);
565 if (r
>= 0 && (uint64_t) (size_t) v
!= v
)
568 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value '%s', ignoring: %m", rvalue
);
576 int config_parse_iec_uint64(
578 const char *filename
,
581 unsigned section_line
,
588 uint64_t *bytes
= data
;
596 r
= parse_size(rvalue
, 1024, bytes
);
598 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value, ignoring: %s", rvalue
);
603 int config_parse_bool(const char* unit
,
604 const char *filename
,
607 unsigned section_line
,
623 k
= parse_boolean(rvalue
);
625 log_syntax(unit
, LOG_ERR
, filename
, line
, k
,
626 "Failed to parse boolean value%s: %s",
627 fatal
? "" : ", ignoring", rvalue
);
628 return fatal
? -ENOEXEC
: 0;
635 int config_parse_tristate(
637 const char *filename
,
640 unsigned section_line
,
654 /* A tristate is pretty much a boolean, except that it can
655 * also take the special value -1, indicating "uninitialized",
656 * much like NULL is for a pointer type. */
658 k
= parse_boolean(rvalue
);
660 log_syntax(unit
, LOG_ERR
, filename
, line
, k
, "Failed to parse boolean value, ignoring: %s", rvalue
);
668 int config_parse_string(
670 const char *filename
,
673 unsigned section_line
,
687 if (free_and_strdup(s
, empty_to_null(rvalue
)) < 0)
693 int config_parse_path(
695 const char *filename
,
698 unsigned section_line
,
705 _cleanup_free_
char *n
= NULL
;
722 r
= path_simplify_and_warn(n
, PATH_CHECK_ABSOLUTE
| (fatal
? PATH_CHECK_FATAL
: 0), unit
, filename
, line
, lvalue
);
724 return fatal
? -ENOEXEC
: 0;
727 return free_and_replace(*s
, n
);
730 int config_parse_strv(
732 const char *filename
,
735 unsigned section_line
,
750 if (isempty(rvalue
)) {
751 *sv
= strv_free(*sv
);
758 r
= extract_first_word(&rvalue
, &word
, NULL
, EXTRACT_QUOTES
|EXTRACT_RETAIN_ESCAPE
);
764 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
768 r
= strv_consume(sv
, word
);
776 int config_parse_warn_compat(
778 const char *filename
,
781 unsigned section_line
,
788 Disabled reason
= ltype
;
792 case DISABLED_CONFIGURATION
:
793 log_syntax(unit
, LOG_DEBUG
, filename
, line
, 0,
794 "Support for option %s= has been disabled at compile time and it is ignored", lvalue
);
797 case DISABLED_LEGACY
:
798 log_syntax(unit
, LOG_INFO
, filename
, line
, 0,
799 "Support for option %s= has been removed and it is ignored", lvalue
);
802 case DISABLED_EXPERIMENTAL
:
803 log_syntax(unit
, LOG_INFO
, filename
, line
, 0,
804 "Support for option %s= has not yet been enabled and it is ignored", lvalue
);
811 int config_parse_log_facility(
813 const char *filename
,
816 unsigned section_line
,
830 x
= log_facility_unshifted_from_string(rvalue
);
832 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse log facility, ignoring: %s", rvalue
);
836 *o
= (x
<< 3) | LOG_PRI(*o
);
841 int config_parse_log_level(
843 const char *filename
,
846 unsigned section_line
,
860 x
= log_level_from_string(rvalue
);
862 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse log level, ignoring: %s", rvalue
);
866 if (*o
< 0) /* if it wasn't initialized so far, assume zero facility */
869 *o
= (*o
& LOG_FACMASK
) | x
;
874 int config_parse_signal(
876 const char *filename
,
879 unsigned section_line
,
893 r
= signal_from_string(rvalue
);
895 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse signal name, ignoring: %s", rvalue
);
903 int config_parse_personality(
905 const char *filename
,
908 unsigned section_line
,
915 unsigned long *personality
= data
, p
;
923 p
= PERSONALITY_INVALID
;
925 p
= personality_from_string(rvalue
);
926 if (p
== PERSONALITY_INVALID
) {
927 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse personality, ignoring: %s", rvalue
);
936 int config_parse_ifname(
938 const char *filename
,
941 unsigned section_line
,
956 if (isempty(rvalue
)) {
961 if (!ifname_valid(rvalue
)) {
962 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue
);
966 r
= free_and_strdup(s
, rvalue
);
973 int config_parse_ip_port(
975 const char *filename
,
978 unsigned section_line
,
994 if (isempty(rvalue
)) {
999 r
= parse_ip_port(rvalue
, &port
);
1001 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse port '%s'.", rvalue
);
1010 int config_parse_mtu(
1012 const char *filename
,
1014 const char *section
,
1015 unsigned section_line
,
1022 uint32_t *mtu
= data
;
1028 r
= parse_mtu(ltype
, rvalue
, mtu
);
1030 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1031 "Maximum transfer unit (MTU) value out of range. Permitted range is %" PRIu32
"…%" PRIu32
", ignoring: %s",
1032 (uint32_t) (ltype
== AF_INET6
? IPV6_MIN_MTU
: IPV4_MIN_MTU
), (uint32_t) UINT32_MAX
,
1037 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1038 "Failed to parse MTU value '%s', ignoring: %m", rvalue
);
1045 int config_parse_rlimit(
1047 const char *filename
,
1049 const char *section
,
1050 unsigned section_line
,
1057 struct rlimit
**rl
= data
, d
= {};
1063 r
= rlimit_parse(ltype
, rvalue
, &d
);
1065 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Soft resource limit chosen higher than hard limit, ignoring: %s", rvalue
);
1069 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse resource value, ignoring: %s", rvalue
);
1076 rl
[ltype
] = newdup(struct rlimit
, &d
, 1);
1084 int config_parse_permille(const char* unit
,
1085 const char *filename
,
1087 const char *section
,
1088 unsigned section_line
,
1095 unsigned *permille
= data
;
1103 r
= parse_permille(rvalue
);
1105 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1106 "Failed to parse permille value, ignoring: %s", rvalue
);
1110 *permille
= (unsigned) r
;