1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "alloc-util.h"
10 #include "string-util.h"
12 #include "tmpfile-util.h"
15 static int parse_env_file_internal(
18 int (*push
) (const char *filename
, unsigned line
,
19 const char *key
, char *value
, void *userdata
, int *n_pushed
),
23 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;
24 _cleanup_free_
char *contents
= NULL
, *key
= NULL
, *value
= NULL
;
37 DOUBLE_QUOTE_VALUE_ESCAPE
,
43 r
= read_full_stream(f
, &contents
, NULL
);
45 r
= read_full_file(fname
, &contents
, NULL
);
49 for (p
= contents
; *p
; p
++) {
55 if (strchr(COMMENTS
, c
))
57 else if (!strchr(WHITESPACE
, c
)) {
59 last_key_whitespace
= (size_t) -1;
61 if (!GREEDY_REALLOC(key
, key_alloc
, n_key
+2))
69 if (strchr(NEWLINE
, c
)) {
73 } else if (c
== '=') {
75 last_value_whitespace
= (size_t) -1;
77 if (!strchr(WHITESPACE
, c
))
78 last_key_whitespace
= (size_t) -1;
79 else if (last_key_whitespace
== (size_t) -1)
80 last_key_whitespace
= n_key
;
82 if (!GREEDY_REALLOC(key
, key_alloc
, n_key
+2))
91 if (strchr(NEWLINE
, c
)) {
99 /* strip trailing whitespace from key */
100 if (last_key_whitespace
!= (size_t) -1)
101 key
[last_key_whitespace
] = 0;
103 r
= push(fname
, line
, key
, value
, userdata
, n_pushed
);
109 value_alloc
= n_value
= 0;
111 } else if (c
== '\'')
112 state
= SINGLE_QUOTE_VALUE
;
114 state
= DOUBLE_QUOTE_VALUE
;
116 state
= VALUE_ESCAPE
;
117 else if (!strchr(WHITESPACE
, c
)) {
120 if (!GREEDY_REALLOC(value
, value_alloc
, n_value
+2))
123 value
[n_value
++] = c
;
129 if (strchr(NEWLINE
, c
)) {
138 /* Chomp off trailing whitespace from value */
139 if (last_value_whitespace
!= (size_t) -1)
140 value
[last_value_whitespace
] = 0;
142 /* strip trailing whitespace from key */
143 if (last_key_whitespace
!= (size_t) -1)
144 key
[last_key_whitespace
] = 0;
146 r
= push(fname
, line
, key
, value
, userdata
, n_pushed
);
152 value_alloc
= n_value
= 0;
154 } else if (c
== '\\') {
155 state
= VALUE_ESCAPE
;
156 last_value_whitespace
= (size_t) -1;
158 if (!strchr(WHITESPACE
, c
))
159 last_value_whitespace
= (size_t) -1;
160 else if (last_value_whitespace
== (size_t) -1)
161 last_value_whitespace
= n_value
;
163 if (!GREEDY_REALLOC(value
, value_alloc
, n_value
+2))
166 value
[n_value
++] = c
;
174 if (!strchr(NEWLINE
, c
)) {
175 /* Escaped newlines we eat up entirely */
176 if (!GREEDY_REALLOC(value
, value_alloc
, n_value
+2))
179 value
[n_value
++] = c
;
183 case SINGLE_QUOTE_VALUE
:
187 if (!GREEDY_REALLOC(value
, value_alloc
, n_value
+2))
190 value
[n_value
++] = c
;
195 case DOUBLE_QUOTE_VALUE
:
199 state
= DOUBLE_QUOTE_VALUE_ESCAPE
;
201 if (!GREEDY_REALLOC(value
, value_alloc
, n_value
+2))
204 value
[n_value
++] = c
;
209 case DOUBLE_QUOTE_VALUE_ESCAPE
:
210 state
= DOUBLE_QUOTE_VALUE
;
213 if (!GREEDY_REALLOC(value
, value_alloc
, n_value
+2))
215 value
[n_value
++] = '"';
216 } else if (!strchr(NEWLINE
, c
)) {
217 if (!GREEDY_REALLOC(value
, value_alloc
, n_value
+3))
219 value
[n_value
++] = '\\';
220 value
[n_value
++] = c
;
227 state
= COMMENT_ESCAPE
;
228 else if (strchr(NEWLINE
, c
)) {
246 DOUBLE_QUOTE_VALUE_ESCAPE
)) {
254 if (last_value_whitespace
!= (size_t) -1)
255 value
[last_value_whitespace
] = 0;
257 /* strip trailing whitespace from key */
258 if (last_key_whitespace
!= (size_t) -1)
259 key
[last_key_whitespace
] = 0;
261 r
= push(fname
, line
, key
, value
, userdata
, n_pushed
);
271 static int check_utf8ness_and_warn(
272 const char *filename
, unsigned line
,
273 const char *key
, char *value
) {
275 if (!utf8_is_valid(key
)) {
276 _cleanup_free_
char *p
= NULL
;
278 p
= utf8_escape_invalid(key
);
279 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
280 "%s:%u: invalid UTF-8 in key '%s', ignoring.",
281 strna(filename
), line
, p
);
284 if (value
&& !utf8_is_valid(value
)) {
285 _cleanup_free_
char *p
= NULL
;
287 p
= utf8_escape_invalid(value
);
288 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
289 "%s:%u: invalid UTF-8 value for key %s: '%s', ignoring.",
290 strna(filename
), line
, key
, p
);
296 static int parse_env_file_push(
297 const char *filename
, unsigned line
,
298 const char *key
, char *value
,
303 va_list aq
, *ap
= userdata
;
306 r
= check_utf8ness_and_warn(filename
, line
, key
, value
);
312 while ((k
= va_arg(aq
, const char *))) {
315 v
= va_arg(aq
, char **);
344 r
= parse_env_file_internal(f
, fname
, parse_env_file_push
, &aq
, &n_pushed
);
352 int parse_env_file_sentinel(
361 r
= parse_env_filev(f
, fname
, ap
);
367 static int load_env_file_push(
368 const char *filename
, unsigned line
,
369 const char *key
, char *value
,
372 char ***m
= userdata
;
376 r
= check_utf8ness_and_warn(filename
, line
, key
, value
);
380 p
= strjoin(key
, "=", value
);
384 r
= strv_env_replace(m
, p
);
397 int load_env_file(FILE *f
, const char *fname
, char ***rl
) {
401 r
= parse_env_file_internal(f
, fname
, load_env_file_push
, &m
, NULL
);
411 static int load_env_file_push_pairs(
412 const char *filename
, unsigned line
,
413 const char *key
, char *value
,
416 char ***m
= userdata
;
419 r
= check_utf8ness_and_warn(filename
, line
, key
, value
);
423 r
= strv_extend(m
, key
);
428 r
= strv_extend(m
, "");
432 r
= strv_push(m
, value
);
443 int load_env_file_pairs(FILE *f
, const char *fname
, char ***rl
) {
447 r
= parse_env_file_internal(f
, fname
, load_env_file_push_pairs
, &m
, NULL
);
457 static int merge_env_file_push(
458 const char *filename
, unsigned line
,
459 const char *key
, char *value
,
463 char ***env
= userdata
;
464 char *expanded_value
;
469 log_error("%s:%u: invalid syntax (around \"%s\"), ignoring.", strna(filename
), line
, key
);
473 if (!env_name_is_valid(key
)) {
474 log_error("%s:%u: invalid variable name \"%s\", ignoring.", strna(filename
), line
, key
);
479 expanded_value
= replace_env(value
, *env
,
480 REPLACE_ENV_USE_ENVIRONMENT
|
481 REPLACE_ENV_ALLOW_BRACELESS
|
482 REPLACE_ENV_ALLOW_EXTENDED
);
486 free_and_replace(value
, expanded_value
);
488 log_debug("%s:%u: setting %s=%s", filename
, line
, key
, 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) fchmod_umask(fileno(f
), 0644);
551 write_env_var(f
, *i
);
553 r
= fflush_and_check(f
);
555 if (rename(p
, fname
) >= 0)