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 if (strchr(COMMENTS
, *skip_leading_chars(buf
, WHITESPACE
)))
329 if (!(flags
& CONFIG_PARSE_REFUSE_BOM
)) {
332 q
= startswith(buf
, UTF8_BYTE_ORDER_MARK
);
335 flags
|= CONFIG_PARSE_REFUSE_BOM
;
340 if (strlen(continuation
) + strlen(l
) > LONG_LINE_MAX
) {
341 if (flags
& CONFIG_PARSE_WARN
)
342 log_error("%s:%u: Continuation line too long", filename
, line
);
346 if (!strextend(&continuation
, l
, NULL
)) {
347 if (flags
& CONFIG_PARSE_WARN
)
356 for (e
= p
; *e
; e
++) {
367 continuation
= strdup(l
);
369 if (flags
& CONFIG_PARSE_WARN
)
391 if (flags
& CONFIG_PARSE_WARN
)
392 log_warning_errno(r
, "%s:%u: Failed to parse file: %m", filename
, line
);
396 continuation
= mfree(continuation
);
413 if (flags
& CONFIG_PARSE_WARN
)
414 log_warning_errno(r
, "%s:%u: Failed to parse file: %m", filename
, line
);
422 static int config_parse_many_files(
423 const char *conf_file
,
425 const char *sections
,
426 ConfigItemLookup lookup
,
428 ConfigParseFlags flags
,
435 r
= config_parse(NULL
, conf_file
, NULL
, sections
, lookup
, table
, flags
, userdata
);
440 STRV_FOREACH(fn
, files
) {
441 r
= config_parse(NULL
, *fn
, NULL
, sections
, lookup
, table
, flags
, userdata
);
449 /* Parse each config file in the directories specified as nulstr. */
450 int config_parse_many_nulstr(
451 const char *conf_file
,
452 const char *conf_file_dirs
,
453 const char *sections
,
454 ConfigItemLookup lookup
,
456 ConfigParseFlags flags
,
459 _cleanup_strv_free_
char **files
= NULL
;
462 r
= conf_files_list_nulstr(&files
, ".conf", NULL
, 0, conf_file_dirs
);
466 return config_parse_many_files(conf_file
, files
, sections
, lookup
, table
, flags
, userdata
);
469 /* Parse each config file in the directories specified as strv. */
470 int config_parse_many(
471 const char *conf_file
,
472 const char* const* conf_file_dirs
,
473 const char *dropin_dirname
,
474 const char *sections
,
475 ConfigItemLookup lookup
,
477 ConfigParseFlags flags
,
480 _cleanup_strv_free_
char **dropin_dirs
= NULL
;
481 _cleanup_strv_free_
char **files
= NULL
;
485 suffix
= strjoina("/", dropin_dirname
);
486 r
= strv_extend_strv_concat(&dropin_dirs
, (char**) conf_file_dirs
, suffix
);
490 r
= conf_files_list_strv(&files
, ".conf", NULL
, 0, (const char* const*) dropin_dirs
);
494 return config_parse_many_files(conf_file
, files
, sections
, lookup
, table
, flags
, userdata
);
497 #define DEFINE_PARSER(type, vartype, conv_func) \
498 DEFINE_CONFIG_PARSE_PTR(config_parse_##type, conv_func, vartype, "Failed to parse " #type " value")
500 DEFINE_PARSER(int, int, safe_atoi
);
501 DEFINE_PARSER(long, long, safe_atoli
);
502 DEFINE_PARSER(uint8
, uint8_t, safe_atou8
);
503 DEFINE_PARSER(uint16
, uint16_t, safe_atou16
);
504 DEFINE_PARSER(uint32
, uint32_t, safe_atou32
);
505 DEFINE_PARSER(uint64
, uint64_t, safe_atou64
);
506 DEFINE_PARSER(unsigned, unsigned, safe_atou
);
507 DEFINE_PARSER(double, double, safe_atod
);
508 DEFINE_PARSER(nsec
, nsec_t
, parse_nsec
);
509 DEFINE_PARSER(sec
, usec_t
, parse_sec
);
510 DEFINE_PARSER(sec_def_infinity
, usec_t
, parse_sec_def_infinity
);
511 DEFINE_PARSER(mode
, mode_t
, parse_mode
);
513 int config_parse_iec_size(const char* unit
,
514 const char *filename
,
517 unsigned section_line
,
533 r
= parse_size(rvalue
, 1024, &v
);
534 if (r
>= 0 && (uint64_t) (size_t) v
!= v
)
537 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value '%s', ignoring: %m", rvalue
);
545 int config_parse_si_size(
547 const char *filename
,
550 unsigned section_line
,
566 r
= parse_size(rvalue
, 1000, &v
);
567 if (r
>= 0 && (uint64_t) (size_t) v
!= v
)
570 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value '%s', ignoring: %m", rvalue
);
578 int config_parse_iec_uint64(
580 const char *filename
,
583 unsigned section_line
,
590 uint64_t *bytes
= data
;
598 r
= parse_size(rvalue
, 1024, bytes
);
600 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value, ignoring: %s", rvalue
);
605 int config_parse_bool(const char* unit
,
606 const char *filename
,
609 unsigned section_line
,
625 k
= parse_boolean(rvalue
);
627 log_syntax(unit
, LOG_ERR
, filename
, line
, k
,
628 "Failed to parse boolean value%s: %s",
629 fatal
? "" : ", ignoring", rvalue
);
630 return fatal
? -ENOEXEC
: 0;
637 int config_parse_tristate(
639 const char *filename
,
642 unsigned section_line
,
656 /* A tristate is pretty much a boolean, except that it can
657 * also take the special value -1, indicating "uninitialized",
658 * much like NULL is for a pointer type. */
660 k
= parse_boolean(rvalue
);
662 log_syntax(unit
, LOG_ERR
, filename
, line
, k
, "Failed to parse boolean value, ignoring: %s", rvalue
);
670 int config_parse_string(
672 const char *filename
,
675 unsigned section_line
,
689 if (free_and_strdup(s
, empty_to_null(rvalue
)) < 0)
695 int config_parse_path(
697 const char *filename
,
700 unsigned section_line
,
707 _cleanup_free_
char *n
= NULL
;
724 r
= path_simplify_and_warn(n
, PATH_CHECK_ABSOLUTE
| (fatal
? PATH_CHECK_FATAL
: 0), unit
, filename
, line
, lvalue
);
726 return fatal
? -ENOEXEC
: 0;
729 return free_and_replace(*s
, n
);
732 int config_parse_strv(
734 const char *filename
,
737 unsigned section_line
,
752 if (isempty(rvalue
)) {
753 *sv
= strv_free(*sv
);
760 r
= extract_first_word(&rvalue
, &word
, NULL
, EXTRACT_QUOTES
|EXTRACT_RETAIN_ESCAPE
);
766 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
770 r
= strv_consume(sv
, word
);
778 int config_parse_warn_compat(
780 const char *filename
,
783 unsigned section_line
,
790 Disabled reason
= ltype
;
794 case DISABLED_CONFIGURATION
:
795 log_syntax(unit
, LOG_DEBUG
, filename
, line
, 0,
796 "Support for option %s= has been disabled at compile time and it is ignored", lvalue
);
799 case DISABLED_LEGACY
:
800 log_syntax(unit
, LOG_INFO
, filename
, line
, 0,
801 "Support for option %s= has been removed and it is ignored", lvalue
);
804 case DISABLED_EXPERIMENTAL
:
805 log_syntax(unit
, LOG_INFO
, filename
, line
, 0,
806 "Support for option %s= has not yet been enabled and it is ignored", lvalue
);
813 int config_parse_log_facility(
815 const char *filename
,
818 unsigned section_line
,
832 x
= log_facility_unshifted_from_string(rvalue
);
834 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse log facility, ignoring: %s", rvalue
);
838 *o
= (x
<< 3) | LOG_PRI(*o
);
843 int config_parse_log_level(
845 const char *filename
,
848 unsigned section_line
,
862 x
= log_level_from_string(rvalue
);
864 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse log level, ignoring: %s", rvalue
);
868 if (*o
< 0) /* if it wasn't initialized so far, assume zero facility */
871 *o
= (*o
& LOG_FACMASK
) | x
;
876 int config_parse_signal(
878 const char *filename
,
881 unsigned section_line
,
895 r
= signal_from_string(rvalue
);
897 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse signal name, ignoring: %s", rvalue
);
905 int config_parse_personality(
907 const char *filename
,
910 unsigned section_line
,
917 unsigned long *personality
= data
, p
;
925 p
= PERSONALITY_INVALID
;
927 p
= personality_from_string(rvalue
);
928 if (p
== PERSONALITY_INVALID
) {
929 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse personality, ignoring: %s", rvalue
);
938 int config_parse_ifname(
940 const char *filename
,
943 unsigned section_line
,
958 if (isempty(rvalue
)) {
963 if (!ifname_valid(rvalue
)) {
964 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue
);
968 r
= free_and_strdup(s
, rvalue
);
975 int config_parse_ip_port(
977 const char *filename
,
980 unsigned section_line
,
996 if (isempty(rvalue
)) {
1001 r
= parse_ip_port(rvalue
, &port
);
1003 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse port '%s'.", rvalue
);
1012 int config_parse_mtu(
1014 const char *filename
,
1016 const char *section
,
1017 unsigned section_line
,
1024 uint32_t *mtu
= data
;
1030 r
= parse_mtu(ltype
, rvalue
, mtu
);
1032 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1033 "Maximum transfer unit (MTU) value out of range. Permitted range is %" PRIu32
"…%" PRIu32
", ignoring: %s",
1034 (uint32_t) (ltype
== AF_INET6
? IPV6_MIN_MTU
: IPV4_MIN_MTU
), (uint32_t) UINT32_MAX
,
1039 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1040 "Failed to parse MTU value '%s', ignoring: %m", rvalue
);
1047 int config_parse_rlimit(
1049 const char *filename
,
1051 const char *section
,
1052 unsigned section_line
,
1059 struct rlimit
**rl
= data
, d
= {};
1065 r
= rlimit_parse(ltype
, rvalue
, &d
);
1067 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Soft resource limit chosen higher than hard limit, ignoring: %s", rvalue
);
1071 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse resource value, ignoring: %s", rvalue
);
1078 rl
[ltype
] = newdup(struct rlimit
, &d
, 1);
1086 int config_parse_permille(const char* unit
,
1087 const char *filename
,
1089 const char *section
,
1090 unsigned section_line
,
1097 unsigned *permille
= data
;
1105 r
= parse_permille(rvalue
);
1107 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1108 "Failed to parse permille value, ignoring: %s", rvalue
);
1112 *permille
= (unsigned) r
;