1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
12 #include "string-util.h"
14 #include "tmpfile-util.h"
17 static int parse_env_file_internal(
20 int (*push
) (const char *filename
, unsigned line
,
21 const char *key
, char *value
, void *userdata
, int *n_pushed
),
25 size_t key_alloc
= 0, n_key
= 0, value_alloc
= 0, n_value
= 0, last_value_whitespace
= (size_t) -1, last_key_whitespace
= (size_t) -1;
26 _cleanup_free_
char *contents
= NULL
, *key
= NULL
, *value
= NULL
;
39 DOUBLE_QUOTE_VALUE_ESCAPE
,
45 r
= read_full_stream(f
, &contents
, NULL
);
47 r
= read_full_file(fname
, &contents
, NULL
);
51 for (p
= contents
; *p
; p
++) {
57 if (strchr(COMMENTS
, c
))
59 else if (!strchr(WHITESPACE
, c
)) {
61 last_key_whitespace
= (size_t) -1;
63 if (!GREEDY_REALLOC(key
, key_alloc
, n_key
+2))
71 if (strchr(NEWLINE
, c
)) {
75 } else if (c
== '=') {
77 last_value_whitespace
= (size_t) -1;
79 if (!strchr(WHITESPACE
, c
))
80 last_key_whitespace
= (size_t) -1;
81 else if (last_key_whitespace
== (size_t) -1)
82 last_key_whitespace
= n_key
;
84 if (!GREEDY_REALLOC(key
, key_alloc
, n_key
+2))
93 if (strchr(NEWLINE
, c
)) {
101 /* strip trailing whitespace from key */
102 if (last_key_whitespace
!= (size_t) -1)
103 key
[last_key_whitespace
] = 0;
105 r
= push(fname
, line
, key
, value
, userdata
, n_pushed
);
111 value_alloc
= n_value
= 0;
113 } else if (c
== '\'')
114 state
= SINGLE_QUOTE_VALUE
;
116 state
= DOUBLE_QUOTE_VALUE
;
118 state
= VALUE_ESCAPE
;
119 else if (!strchr(WHITESPACE
, c
)) {
122 if (!GREEDY_REALLOC(value
, value_alloc
, n_value
+2))
125 value
[n_value
++] = c
;
131 if (strchr(NEWLINE
, c
)) {
140 /* Chomp off trailing whitespace from value */
141 if (last_value_whitespace
!= (size_t) -1)
142 value
[last_value_whitespace
] = 0;
144 /* strip trailing whitespace from key */
145 if (last_key_whitespace
!= (size_t) -1)
146 key
[last_key_whitespace
] = 0;
148 r
= push(fname
, line
, key
, value
, userdata
, n_pushed
);
154 value_alloc
= n_value
= 0;
156 } else if (c
== '\\') {
157 state
= VALUE_ESCAPE
;
158 last_value_whitespace
= (size_t) -1;
160 if (!strchr(WHITESPACE
, c
))
161 last_value_whitespace
= (size_t) -1;
162 else if (last_value_whitespace
== (size_t) -1)
163 last_value_whitespace
= n_value
;
165 if (!GREEDY_REALLOC(value
, value_alloc
, n_value
+2))
168 value
[n_value
++] = c
;
176 if (!strchr(NEWLINE
, c
)) {
177 /* Escaped newlines we eat up entirely */
178 if (!GREEDY_REALLOC(value
, value_alloc
, n_value
+2))
181 value
[n_value
++] = c
;
185 case SINGLE_QUOTE_VALUE
:
189 if (!GREEDY_REALLOC(value
, value_alloc
, n_value
+2))
192 value
[n_value
++] = c
;
197 case DOUBLE_QUOTE_VALUE
:
201 state
= DOUBLE_QUOTE_VALUE_ESCAPE
;
203 if (!GREEDY_REALLOC(value
, value_alloc
, n_value
+2))
206 value
[n_value
++] = c
;
211 case DOUBLE_QUOTE_VALUE_ESCAPE
:
212 state
= DOUBLE_QUOTE_VALUE
;
215 if (!GREEDY_REALLOC(value
, value_alloc
, n_value
+2))
217 value
[n_value
++] = '"';
218 } else if (!strchr(NEWLINE
, c
)) {
219 if (!GREEDY_REALLOC(value
, value_alloc
, n_value
+3))
221 value
[n_value
++] = '\\';
222 value
[n_value
++] = c
;
229 state
= COMMENT_ESCAPE
;
230 else if (strchr(NEWLINE
, c
)) {
248 DOUBLE_QUOTE_VALUE_ESCAPE
)) {
256 if (last_value_whitespace
!= (size_t) -1)
257 value
[last_value_whitespace
] = 0;
259 /* strip trailing whitespace from key */
260 if (last_key_whitespace
!= (size_t) -1)
261 key
[last_key_whitespace
] = 0;
263 r
= push(fname
, line
, key
, value
, userdata
, n_pushed
);
273 static int check_utf8ness_and_warn(
274 const char *filename
, unsigned line
,
275 const char *key
, char *value
) {
277 if (!utf8_is_valid(key
)) {
278 _cleanup_free_
char *p
= NULL
;
280 p
= utf8_escape_invalid(key
);
281 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
282 "%s:%u: invalid UTF-8 in key '%s', ignoring.",
283 strna(filename
), line
, p
);
286 if (value
&& !utf8_is_valid(value
)) {
287 _cleanup_free_
char *p
= NULL
;
289 p
= utf8_escape_invalid(value
);
290 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
291 "%s:%u: invalid UTF-8 value for key %s: '%s', ignoring.",
292 strna(filename
), line
, key
, p
);
298 static int parse_env_file_push(
299 const char *filename
, unsigned line
,
300 const char *key
, char *value
,
305 va_list aq
, *ap
= userdata
;
308 r
= check_utf8ness_and_warn(filename
, line
, key
, value
);
314 while ((k
= va_arg(aq
, const char *))) {
317 v
= va_arg(aq
, char **);
346 r
= parse_env_file_internal(f
, fname
, parse_env_file_push
, &aq
, &n_pushed
);
354 int parse_env_file_sentinel(
363 r
= parse_env_filev(f
, fname
, ap
);
369 static int load_env_file_push(
370 const char *filename
, unsigned line
,
371 const char *key
, char *value
,
374 char ***m
= userdata
;
378 r
= check_utf8ness_and_warn(filename
, line
, key
, value
);
382 p
= strjoin(key
, "=", value
);
386 r
= strv_env_replace(m
, p
);
399 int load_env_file(FILE *f
, const char *fname
, char ***rl
) {
403 r
= parse_env_file_internal(f
, fname
, load_env_file_push
, &m
, NULL
);
413 static int load_env_file_push_pairs(
414 const char *filename
, unsigned line
,
415 const char *key
, char *value
,
418 char ***m
= userdata
;
421 r
= check_utf8ness_and_warn(filename
, line
, key
, value
);
425 r
= strv_extend(m
, key
);
430 r
= strv_extend(m
, "");
434 r
= strv_push(m
, value
);
445 int load_env_file_pairs(FILE *f
, const char *fname
, char ***rl
) {
449 r
= parse_env_file_internal(f
, fname
, load_env_file_push_pairs
, &m
, NULL
);
459 static int merge_env_file_push(
460 const char *filename
, unsigned line
,
461 const char *key
, char *value
,
465 char ***env
= userdata
;
466 char *expanded_value
;
471 log_error("%s:%u: invalid syntax (around \"%s\"), ignoring.", strna(filename
), line
, key
);
475 if (!env_name_is_valid(key
)) {
476 log_error("%s:%u: invalid variable name \"%s\", ignoring.", strna(filename
), line
, key
);
481 expanded_value
= replace_env(value
, *env
,
482 REPLACE_ENV_USE_ENVIRONMENT
|
483 REPLACE_ENV_ALLOW_BRACELESS
|
484 REPLACE_ENV_ALLOW_EXTENDED
);
488 free_and_replace(value
, expanded_value
);
490 return load_env_file_push(filename
, line
, key
, value
, env
, n_pushed
);
498 /* NOTE: this function supports braceful and braceless variable expansions,
499 * plus "extended" substitutions, unlike other exported parsing functions.
502 return parse_env_file_internal(f
, fname
, merge_env_file_push
, env
, NULL
);
505 static void write_env_var(FILE *f
, const char *v
) {
511 fputs_unlocked(v
, f
);
512 fputc_unlocked('\n', f
);
517 fwrite_unlocked(v
, 1, p
-v
, f
);
519 if (string_has_cc(p
, NULL
) || chars_intersect(p
, WHITESPACE SHELL_NEED_QUOTES
)) {
520 fputc_unlocked('\"', f
);
523 if (strchr(SHELL_NEED_ESCAPE
, *p
))
524 fputc_unlocked('\\', f
);
526 fputc_unlocked(*p
, f
);
529 fputc_unlocked('\"', f
);
531 fputs_unlocked(p
, f
);
533 fputc_unlocked('\n', f
);
536 int write_env_file(const char *fname
, char **l
) {
537 _cleanup_fclose_
FILE *f
= NULL
;
538 _cleanup_free_
char *p
= NULL
;
544 r
= fopen_temporary(fname
, &f
, &p
);
548 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
549 (void) fchmod_umask(fileno(f
), 0644);
552 write_env_var(f
, *i
);
554 r
= fflush_and_check(f
);
556 if (rename(p
, fname
) >= 0)