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"
21 #include "parse-util.h"
22 #include "path-util.h"
23 #include "process-util.h"
24 #include "signal-util.h"
25 #include "socket-util.h"
26 #include "string-util.h"
28 #include "syslog-util.h"
29 #include "time-util.h"
31 #include "rlimit-util.h"
33 int config_item_table_lookup(
37 ConfigParserCallback
*func
,
42 const ConfigTableItem
*t
;
50 for (t
= table
; t
->lvalue
; t
++) {
52 if (!streq(lvalue
, t
->lvalue
))
55 if (!streq_ptr(section
, t
->section
))
67 int config_item_perf_lookup(
71 ConfigParserCallback
*func
,
76 ConfigPerfItemLookup lookup
= (ConfigPerfItemLookup
) table
;
77 const ConfigPerfItem
*p
;
86 p
= lookup(lvalue
, strlen(lvalue
));
90 key
= strjoin(section
, ".", lvalue
);
94 p
= lookup(key
, strlen(key
));
103 *data
= (uint8_t*) userdata
+ p
->offset
;
107 /* Run the user supplied parser for an assignment */
108 static int next_assignment(
110 const char *filename
,
112 ConfigItemLookup lookup
,
115 unsigned section_line
,
118 ConfigParseFlags flags
,
121 ConfigParserCallback func
= NULL
;
132 r
= lookup(table
, section
, lvalue
, &func
, <ype
, &data
, userdata
);
138 return func(unit
, filename
, line
, section
, section_line
,
139 lvalue
, ltype
, rvalue
, data
, userdata
);
144 /* Warn about unknown non-extension fields. */
145 if (!(flags
& CONFIG_PARSE_RELAXED
) && !startswith(lvalue
, "X-"))
146 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Unknown lvalue '%s' in section '%s'", lvalue
, section
);
151 /* Parse a single logical line */
152 static int parse_line(
154 const char *filename
,
156 const char *sections
,
157 ConfigItemLookup lookup
,
159 ConfigParseFlags flags
,
161 unsigned *section_line
,
162 bool *section_ignored
,
177 if (strchr(COMMENTS
"\n", *l
))
180 include
= first_word(l
, ".include");
182 _cleanup_free_
char *fn
= NULL
;
184 /* .includes are a bad idea, we only support them here
185 * for historical reasons. They create cyclic include
186 * problems and make it difficult to detect
187 * configuration file changes with an easy
188 * stat(). Better approaches, such as .d/ drop-in
191 * Support for them should be eventually removed. */
193 if (!(flags
& CONFIG_PARSE_ALLOW_INCLUDE
)) {
194 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, ".include not allowed here. Ignoring.");
198 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
199 ".include directives are deprecated, and support for them will be removed in a future version of systemd. "
200 "Please use drop-in files instead.");
202 fn
= file_in_same_dir(filename
, strstrip(include
));
206 return config_parse(unit
, fn
, NULL
, sections
, lookup
, table
, flags
, userdata
);
209 if (!utf8_is_valid(l
))
210 return log_syntax_invalid_utf8(unit
, LOG_WARNING
, filename
, line
, l
);
220 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid section header '%s'", l
);
224 n
= strndup(l
+1, k
-2);
228 if (sections
&& !nulstr_contains(sections
, n
)) {
230 if (!(flags
& CONFIG_PARSE_RELAXED
) && !startswith(n
, "X-"))
231 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Unknown section '%s'. Ignoring.", n
);
234 *section
= mfree(*section
);
236 *section_ignored
= true;
238 free_and_replace(*section
, n
);
239 *section_line
= line
;
240 *section_ignored
= false;
246 if (sections
&& !*section
) {
248 if (!(flags
& CONFIG_PARSE_RELAXED
) && !*section_ignored
)
249 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Assignment outside of section. Ignoring.");
256 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Missing '='.");
263 return next_assignment(unit
,
276 /* Go through the file and parse each line */
277 int config_parse(const char *unit
,
278 const char *filename
,
280 const char *sections
,
281 ConfigItemLookup lookup
,
283 ConfigParseFlags flags
,
286 _cleanup_free_
char *section
= NULL
, *continuation
= NULL
;
287 _cleanup_fclose_
FILE *ours
= NULL
;
288 unsigned line
= 0, section_line
= 0;
289 bool section_ignored
= false;
296 f
= ours
= fopen(filename
, "re");
298 /* Only log on request, except for ENOENT,
299 * since we return 0 to the caller. */
300 if ((flags
& CONFIG_PARSE_WARN
) || errno
== ENOENT
)
301 log_full_errno(errno
== ENOENT
? LOG_DEBUG
: LOG_ERR
, errno
,
302 "Failed to open configuration file '%s': %m", filename
);
303 return errno
== ENOENT
? 0 : -errno
;
307 fd_warn_permissions(filename
, fileno(f
));
310 _cleanup_free_
char *buf
= NULL
;
311 bool escaped
= false;
314 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
318 if (flags
& CONFIG_PARSE_WARN
)
319 log_error_errno(r
, "%s:%u: Line too long", filename
, line
);
324 if (CONFIG_PARSE_WARN
)
325 log_error_errno(r
, "%s:%u: Error while reading configuration file: %m", filename
, line
);
331 if (!(flags
& CONFIG_PARSE_REFUSE_BOM
)) {
334 q
= startswith(buf
, UTF8_BYTE_ORDER_MARK
);
337 flags
|= CONFIG_PARSE_REFUSE_BOM
;
342 if (strlen(continuation
) + strlen(l
) > LONG_LINE_MAX
) {
343 if (flags
& CONFIG_PARSE_WARN
)
344 log_error("%s:%u: Continuation line too long", filename
, line
);
348 if (!strextend(&continuation
, l
, NULL
)) {
349 if (flags
& CONFIG_PARSE_WARN
)
358 for (e
= p
; *e
; e
++) {
369 continuation
= strdup(l
);
371 if (flags
& CONFIG_PARSE_WARN
)
393 if (flags
& CONFIG_PARSE_WARN
)
394 log_warning_errno(r
, "%s:%u: Failed to parse file: %m", filename
, line
);
398 continuation
= mfree(continuation
);
415 if (flags
& CONFIG_PARSE_WARN
)
416 log_warning_errno(r
, "%s:%u: Failed to parse file: %m", filename
, line
);
424 static int config_parse_many_files(
425 const char *conf_file
,
427 const char *sections
,
428 ConfigItemLookup lookup
,
430 ConfigParseFlags flags
,
437 r
= config_parse(NULL
, conf_file
, NULL
, sections
, lookup
, table
, flags
, userdata
);
442 STRV_FOREACH(fn
, files
) {
443 r
= config_parse(NULL
, *fn
, NULL
, sections
, lookup
, table
, flags
, userdata
);
451 /* Parse each config file in the directories specified as nulstr. */
452 int config_parse_many_nulstr(
453 const char *conf_file
,
454 const char *conf_file_dirs
,
455 const char *sections
,
456 ConfigItemLookup lookup
,
458 ConfigParseFlags flags
,
461 _cleanup_strv_free_
char **files
= NULL
;
464 r
= conf_files_list_nulstr(&files
, ".conf", NULL
, 0, conf_file_dirs
);
468 return config_parse_many_files(conf_file
, files
, sections
, lookup
, table
, flags
, userdata
);
471 /* Parse each config file in the directories specified as strv. */
472 int config_parse_many(
473 const char *conf_file
,
474 const char* const* conf_file_dirs
,
475 const char *dropin_dirname
,
476 const char *sections
,
477 ConfigItemLookup lookup
,
479 ConfigParseFlags flags
,
482 _cleanup_strv_free_
char **dropin_dirs
= NULL
;
483 _cleanup_strv_free_
char **files
= NULL
;
487 suffix
= strjoina("/", dropin_dirname
);
488 r
= strv_extend_strv_concat(&dropin_dirs
, (char**) conf_file_dirs
, suffix
);
492 r
= conf_files_list_strv(&files
, ".conf", NULL
, 0, (const char* const*) dropin_dirs
);
496 return config_parse_many_files(conf_file
, files
, sections
, lookup
, table
, flags
, userdata
);
499 #define DEFINE_PARSER(type, vartype, conv_func) \
500 DEFINE_CONFIG_PARSE_PTR(config_parse_##type, conv_func, vartype, "Failed to parse " #type " value")
502 DEFINE_PARSER(int, int, safe_atoi
);
503 DEFINE_PARSER(long, long, safe_atoli
);
504 DEFINE_PARSER(uint8
, uint8_t, safe_atou8
);
505 DEFINE_PARSER(uint16
, uint16_t, safe_atou16
);
506 DEFINE_PARSER(uint32
, uint32_t, safe_atou32
);
507 DEFINE_PARSER(uint64
, uint64_t, safe_atou64
);
508 DEFINE_PARSER(unsigned, unsigned, safe_atou
);
509 DEFINE_PARSER(double, double, safe_atod
);
510 DEFINE_PARSER(nsec
, nsec_t
, parse_nsec
);
511 DEFINE_PARSER(sec
, usec_t
, parse_sec
);
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_QUOTES
|EXTRACT_RETAIN_ESCAPE
);
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_join_controllers(
1015 const char *filename
,
1017 const char *section
,
1018 unsigned section_line
,
1025 char ****ret
= data
;
1026 const char *whole_rvalue
= rvalue
;
1028 _cleanup_(strv_free_freep
) char ***controllers
= NULL
;
1036 _cleanup_free_
char *word
= NULL
;
1040 r
= extract_first_word(&rvalue
, &word
, NULL
, EXTRACT_QUOTES
);
1042 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid value for %s: %s", lvalue
, whole_rvalue
);
1048 l
= strv_split(word
, ",");
1053 if (strv_length(l
) <= 1) {
1059 controllers
= new(char**, 2);
1066 controllers
[1] = NULL
;
1073 t
= new0(char**, n
+2);
1081 for (a
= controllers
; *a
; a
++)
1082 if (strv_overlap(*a
, l
)) {
1083 if (strv_extend_strv(&l
, *a
, false) < 0) {
1102 t
[n
++] = strv_uniq(l
);
1104 strv_free_free(controllers
);
1108 if (!isempty(rvalue
))
1109 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Trailing garbage, ignoring.");
1111 /* As a special case, return a single empty strv, to override the default */
1113 controllers
= new(char**, 2);
1116 controllers
[0] = strv_new(NULL
);
1117 if (!controllers
[0])
1119 controllers
[1] = NULL
;
1122 strv_free_free(*ret
);
1123 *ret
= TAKE_PTR(controllers
);
1128 int config_parse_mtu(
1130 const char *filename
,
1132 const char *section
,
1133 unsigned section_line
,
1140 uint32_t *mtu
= data
;
1146 r
= parse_mtu(ltype
, rvalue
, mtu
);
1148 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1149 "Maximum transfer unit (MTU) value out of range. Permitted range is %" PRIu32
"…%" PRIu32
", ignoring: %s",
1150 (uint32_t) (ltype
== AF_INET6
? IPV6_MIN_MTU
: IPV4_MIN_MTU
), (uint32_t) UINT32_MAX
,
1155 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1156 "Failed to parse MTU value '%s', ignoring: %m", rvalue
);
1163 int config_parse_rlimit(
1165 const char *filename
,
1167 const char *section
,
1168 unsigned section_line
,
1175 struct rlimit
**rl
= data
, d
= {};
1181 r
= rlimit_parse(ltype
, rvalue
, &d
);
1183 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Soft resource limit chosen higher than hard limit, ignoring: %s", rvalue
);
1187 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse resource value, ignoring: %s", rvalue
);
1194 rl
[ltype
] = newdup(struct rlimit
, &d
, 1);
1202 int config_parse_permille(const char* unit
,
1203 const char *filename
,
1205 const char *section
,
1206 unsigned section_line
,
1213 unsigned *permille
= data
;
1221 r
= parse_permille(rvalue
);
1223 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1224 "Failed to parse permille value, ignoring: %s", rvalue
);
1228 *permille
= (unsigned) r
;