Debian Codegraph @

src/basic/env-file.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/stat.h>
4 #include <unistd.h>
5
6 #include "alloc-util.h"
7 #include "env-file.h"
8 #include "env-util.h"
9 #include "escape.h"
10 #include "fd-util.h"
11 #include "fileio.h"
12 #include "fs-util.h"
13 #include "label-util.h"
14 #include "log.h"
15 #include "string-util.h"
16 #include "strv.h"
17 #include "tmpfile-util.h"
18 #include "utf8.h"
19
20 typedef int (*push_env_func_t)(
21 const char *filename,
22 unsigned line,
23 const char *key,
24 char *value,
25 void *userdata);
26
27 static int parse_env_file_internal(
28 FILE *f,
29 const char *fname,
30 push_env_func_t push,
31 void *userdata) {
32
33 size_t n_key = 0, n_value = 0, last_value_whitespace = SIZE_MAX, last_key_whitespace = SIZE_MAX;
34 _cleanup_free_ char *contents = NULL, *key = NULL, *value = NULL;
35 unsigned line = 1;
36 int r;
37
38 enum {
39 PRE_KEY,
40 KEY,
41 PRE_VALUE,
42 VALUE,
43 VALUE_ESCAPE,
44 SINGLE_QUOTE_VALUE,
45 DOUBLE_QUOTE_VALUE,
46 DOUBLE_QUOTE_VALUE_ESCAPE,
47 COMMENT,
48 COMMENT_ESCAPE
49 } state = PRE_KEY;
50
51 assert(f || fname);
52 assert(push);
53
54 if (f)
55 r = read_full_stream(f, &contents, NULL);
56 else
57 r = read_full_file(fname, &contents, NULL);
58 if (r < 0)
59 return r;
60
61 for (char *p = contents; *p; p++) {
62 char c = *p;
63
64 switch (state) {
65
66 case PRE_KEY:
67 if (strchr(COMMENTS, c))
68 state = COMMENT;
69 else if (!strchr(WHITESPACE, c)) {
70 state = KEY;
71 last_key_whitespace = SIZE_MAX;
72
73 if (!GREEDY_REALLOC(key, n_key+2))
74 return -ENOMEM;
75
76 key[n_key++] = c;
77 }
78 break;
79
80 case KEY:
81 if (strchr(NEWLINE, c)) {
82 state = PRE_KEY;
83 line++;
84 n_key = 0;
85 } else if (c == '=') {
86 state = PRE_VALUE;
87 last_value_whitespace = SIZE_MAX;
88 } else {
89 if (!strchr(WHITESPACE, c))
90 last_key_whitespace = SIZE_MAX;
91 else if (last_key_whitespace == SIZE_MAX)
92 last_key_whitespace = n_key;
93
94 if (!GREEDY_REALLOC(key, n_key+2))
95 return -ENOMEM;
96
97 key[n_key++] = c;
98 }
99
100 break;
101
102 case PRE_VALUE:
103 if (strchr(NEWLINE, c)) {
104 state = PRE_KEY;
105 line++;
106 key[n_key] = 0;
107
108 if (value)
109 value[n_value] = 0;
110
111 /* strip trailing whitespace from key */
112 if (last_key_whitespace != SIZE_MAX)
113 key[last_key_whitespace] = 0;
114
115 r = push(fname, line, key, value, userdata);
116 if (r < 0)
117 return r;
118
119 n_key = 0;
120 value = NULL;
121 n_value = 0;
122
123 } else if (c == '\'')
124 state = SINGLE_QUOTE_VALUE;
125 else if (c == '"')
126 state = DOUBLE_QUOTE_VALUE;
127 else if (c == '\\')
128 state = VALUE_ESCAPE;
129 else if (!strchr(WHITESPACE, c)) {
130 state = VALUE;
131
132 if (!GREEDY_REALLOC(value, n_value+2))
133 return -ENOMEM;
134
135 value[n_value++] = c;
136 }
137
138 break;
139
140 case VALUE:
141 if (strchr(NEWLINE, c)) {
142 state = PRE_KEY;
143 line++;
144
145 key[n_key] = 0;
146
147 if (value)
148 value[n_value] = 0;
149
150 /* Chomp off trailing whitespace from value */
151 if (last_value_whitespace != SIZE_MAX)
152 value[last_value_whitespace] = 0;
153
154 /* strip trailing whitespace from key */
155 if (last_key_whitespace != SIZE_MAX)
156 key[last_key_whitespace] = 0;
157
158 r = push(fname, line, key, value, userdata);
159 if (r < 0)
160 return r;
161
162 n_key = 0;
163 value = NULL;
164 n_value = 0;
165
166 } else if (c == '\\') {
167 state = VALUE_ESCAPE;
168 last_value_whitespace = SIZE_MAX;
169 } else {
170 if (!strchr(WHITESPACE, c))
171 last_value_whitespace = SIZE_MAX;
172 else if (last_value_whitespace == SIZE_MAX)
173 last_value_whitespace = n_value;
174
175 if (!GREEDY_REALLOC(value, n_value+2))
176 return -ENOMEM;
177
178 value[n_value++] = c;
179 }
180
181 break;
182
183 case VALUE_ESCAPE:
184 state = VALUE;
185
186 if (!strchr(NEWLINE, c)) {
187 /* Escaped newlines we eat up entirely */
188 if (!GREEDY_REALLOC(value, n_value+2))
189 return -ENOMEM;
190
191 value[n_value++] = c;
192 }
193 break;
194
195 case SINGLE_QUOTE_VALUE:
196 if (c == '\'')
197 state = PRE_VALUE;
198 else {
199 if (!GREEDY_REALLOC(value, n_value+2))
200 return -ENOMEM;
201
202 value[n_value++] = c;
203 }
204
205 break;
206
207 case DOUBLE_QUOTE_VALUE:
208 if (c == '"')
209 state = PRE_VALUE;
210 else if (c == '\\')
211 state = DOUBLE_QUOTE_VALUE_ESCAPE;
212 else {
213 if (!GREEDY_REALLOC(value, n_value+2))
214 return -ENOMEM;
215
216 value[n_value++] = c;
217 }
218
219 break;
220
221 case DOUBLE_QUOTE_VALUE_ESCAPE:
222 state = DOUBLE_QUOTE_VALUE;
223
224 if (strchr(SHELL_NEED_ESCAPE, c)) {
225 /* If this is a char that needs escaping, just unescape it. */
226 if (!GREEDY_REALLOC(value, n_value+2))
227 return -ENOMEM;
228 value[n_value++] = c;
229 } else if (c != '\n') {
230 /* If other char than what needs escaping, keep the "\" in place, like the
231 * real shell does. */
232 if (!GREEDY_REALLOC(value, n_value+3))
233 return -ENOMEM;
234 value[n_value++] = '\\';
235 value[n_value++] = c;
236 }
237
238 /* Escaped newlines (aka "continuation lines") are eaten up entirely */
239 break;
240
241 case COMMENT:
242 if (c == '\\')
243 state = COMMENT_ESCAPE;
244 else if (strchr(NEWLINE, c)) {
245 state = PRE_KEY;
246 line++;
247 }
248 break;
249
250 case COMMENT_ESCAPE:
251 log_debug("The line which doesn't begin with \";\" or \"#\", but follows a comment" \
252 " line trailing with escape is now treated as a non comment line since v254.");
253 if (strchr(NEWLINE, c)) {
254 state = PRE_KEY;
255 line++;
256 } else
257 state = COMMENT;
258 break;
259 }
260 }
261
262 if (IN_SET(state,
263 PRE_VALUE,
264 VALUE,
265 VALUE_ESCAPE,
266 SINGLE_QUOTE_VALUE,
267 DOUBLE_QUOTE_VALUE,
268 DOUBLE_QUOTE_VALUE_ESCAPE)) {
269
270 key[n_key] = 0;
271
272 if (value)
273 value[n_value] = 0;
274
275 if (state == VALUE)
276 if (last_value_whitespace != SIZE_MAX)
277 value[last_value_whitespace] = 0;
278
279 /* strip trailing whitespace from key */
280 if (last_key_whitespace != SIZE_MAX)
281 key[last_key_whitespace] = 0;
282
283 r = push(fname, line, key, value, userdata);
284 if (r < 0)
285 return r;
286
287 value = NULL;
288 }
289
290 return 0;
291 }
292
293 static int check_utf8ness_and_warn(
294 const char *filename, unsigned line,
295 const char *key, char *value) {
296
297 assert(key);
298
299 if (!utf8_is_valid(key)) {
300 _cleanup_free_ char *p = NULL;
301
302 p = utf8_escape_invalid(key);
303 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
304 "%s:%u: invalid UTF-8 in key '%s', ignoring.",
305 strna(filename), line, p);
306 }
307
308 if (value && !utf8_is_valid(value)) {
309 _cleanup_free_ char *p = NULL;
310
311 p = utf8_escape_invalid(value);
312 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
313 "%s:%u: invalid UTF-8 value for key %s: '%s', ignoring.",
314 strna(filename), line, key, p);
315 }
316
317 return 0;
318 }
319
320 static int parse_env_file_push(
321 const char *filename, unsigned line,
322 const char *key, char *value,
323 void *userdata) {
324
325 const char *k;
326 va_list aq, *ap = userdata;
327 int r;
328
329 assert(key);
330
331 r = check_utf8ness_and_warn(filename, line, key, value);
332 if (r < 0)
333 return r;
334
335 va_copy(aq, *ap);
336
337 while ((k = va_arg(aq, const char *))) {
338 char **v;
339
340 v = va_arg(aq, char **);
341
342 if (streq(key, k)) {
343 va_end(aq);
344 free_and_replace(*v, value);
345
346 return 1;
347 }
348 }
349
350 va_end(aq);
351 free(value);
352
353 return 0;
354 }
355
356 int parse_env_filev(
357 FILE *f,
358 const char *fname,
359 va_list ap) {
360
361 int r;
362 va_list aq;
363
364 assert(f || fname);
365
366 va_copy(aq, ap);
367 r = parse_env_file_internal(f, fname, parse_env_file_push, &aq);
368 va_end(aq);
369 return r;
370 }
371
372 int parse_env_file_fdv(int fd, const char *fname, va_list ap) {
373 _cleanup_fclose_ FILE *f = NULL;
374 va_list aq;
375 int r;
376
377 assert(fd >= 0);
378
379 r = fdopen_independent(fd, "re", &f);
380 if (r < 0)
381 return r;
382
383 va_copy(aq, ap);
384 r = parse_env_file_internal(f, fname, parse_env_file_push, &aq);
385 va_end(aq);
386 return r;
387 }
388
389 int parse_env_file_sentinel(
390 FILE *f,
391 const char *fname,
392 ...) {
393
394 va_list ap;
395 int r;
396
397 assert(f || fname);
398
399 va_start(ap, fname);
400 r = parse_env_filev(f, fname, ap);
401 va_end(ap);
402
403 return r;
404 }
405
406 int parse_env_file_fd_sentinel(
407 int fd,
408 const char *fname, /* only used for logging */
409 ...) {
410
411 va_list ap;
412 int r;
413
414 assert(fd >= 0);
415
416 va_start(ap, fname);
417 r = parse_env_file_fdv(fd, fname, ap);
418 va_end(ap);
419
420 return r;
421 }
422
423 int parse_env_datav(
424 const char *data,
425 size_t size,
426 const char *fname, /* only used for logging */
427 va_list ap) {
428
429 assert(data);
430
431 if (size == SIZE_MAX)
432 size = strlen_ptr(data);
433
434 _cleanup_fclose_ FILE *f = fmemopen_unlocked((void*) data, size, "r");
435 if (!f)
436 return -ENOMEM;
437
438 return parse_env_filev(f, fname, ap);
439 }
440
441 int parse_env_data_sentinel(
442 const char *data,
443 size_t size,
444 const char *fname, /* only used for logging */
445 ...) {
446
447 va_list ap;
448 int r;
449
450 va_start(ap, fname);
451 r = parse_env_datav(data, size, fname, ap);
452 va_end(ap);
453
454 return r;
455 }
456
457 static int load_env_file_push(
458 const char *filename, unsigned line,
459 const char *key, char *value,
460 void *userdata) {
461
462 char ***m = userdata;
463 char *p;
464 int r;
465
466 assert(key);
467
468 r = check_utf8ness_and_warn(filename, line, key, value);
469 if (r < 0)
470 return r;
471
472 p = strjoin(key, "=", value);
473 if (!p)
474 return -ENOMEM;
475
476 r = strv_env_replace_consume(m, p);
477 if (r < 0)
478 return r;
479
480 free(value);
481 return 0;
482 }
483
484 int load_env_file(FILE *f, const char *fname, char ***ret) {
485 _cleanup_strv_free_ char **m = NULL;
486 int r;
487
488 assert(f || fname);
489 assert(ret);
490
491 r = parse_env_file_internal(f, fname, load_env_file_push, &m);
492 if (r < 0)
493 return r;
494
495 *ret = TAKE_PTR(m);
496 return 0;
497 }
498
499 static int load_env_file_push_pairs(
500 const char *filename, unsigned line,
501 const char *key, char *value,
502 void *userdata) {
503
504 char ***m = ASSERT_PTR(userdata);
505 int r;
506
507 assert(key);
508
509 r = check_utf8ness_and_warn(filename, line, key, value);
510 if (r < 0)
511 return r;
512
513 /* Check if the key is present */
514 for (char **t = *m; t && *t; t += 2)
515 if (streq(t[0], key)) {
516 if (value)
517 return free_and_replace(t[1], value);
518 else
519 return free_and_strdup(t+1, "");
520 }
521
522 r = strv_extend(m, key);
523 if (r < 0)
524 return r;
525
526 if (value)
527 return strv_push(m, value);
528 else
529 return strv_extend(m, "");
530 }
531
532 int load_env_file_pairs(FILE *f, const char *fname, char ***ret) {
533 _cleanup_strv_free_ char **m = NULL;
534 int r;
535
536 assert(f || fname);
537 assert(ret);
538
539 r = parse_env_file_internal(f, fname, load_env_file_push_pairs, &m);
540 if (r < 0)
541 return r;
542
543 *ret = TAKE_PTR(m);
544 return 0;
545 }
546
547 int load_env_file_pairs_fd(int fd, const char *fname, char ***ret) {
548 _cleanup_fclose_ FILE *f = NULL;
549 int r;
550
551 assert(fd >= 0);
552
553 r = fdopen_independent(fd, "re", &f);
554 if (r < 0)
555 return r;
556
557 return load_env_file_pairs(f, fname, ret);
558 }
559
560 static int merge_env_file_push(
561 const char *filename, unsigned line,
562 const char *key, char *value,
563 void *userdata) {
564
565 char ***env = ASSERT_PTR(userdata);
566 char *expanded_value;
567 int r;
568
569 assert(key);
570
571 if (!value) {
572 log_error("%s:%u: invalid syntax (around \"%s\"), ignoring.", strna(filename), line, key);
573 return 0;
574 }
575
576 if (!env_name_is_valid(key)) {
577 log_error("%s:%u: invalid variable name \"%s\", ignoring.", strna(filename), line, key);
578 free(value);
579 return 0;
580 }
581
582 r = replace_env(value,
583 *env,
584 REPLACE_ENV_USE_ENVIRONMENT|REPLACE_ENV_ALLOW_BRACELESS|REPLACE_ENV_ALLOW_EXTENDED,
585 &expanded_value);
586 if (r < 0)
587 return log_error_errno(r, "%s:%u: Failed to expand variable '%s': %m", strna(filename), line, value);
588
589 free_and_replace(value, expanded_value);
590
591 log_debug("%s:%u: setting %s=%s", filename, line, key, value);
592
593 return load_env_file_push(filename, line, key, value, env);
594 }
595
596 int merge_env_file(
597 char ***env,
598 FILE *f,
599 const char *fname) {
600
601 assert(env);
602 assert(f || fname);
603
604 /* NOTE: this function supports braceful and braceless variable expansions,
605 * plus "extended" substitutions, unlike other exported parsing functions.
606 */
607
608 return parse_env_file_internal(f, fname, merge_env_file_push, env);
609 }
610
611 static void env_file_fputs_escaped(FILE *f, const char *p) {
612 assert(f);
613 assert(p);
614
615 flockfile(f);
616
617 if (string_has_cc(p, NULL) || chars_intersect(p, WHITESPACE SHELL_NEED_QUOTES)) {
618 fputc_unlocked('"', f);
619
620 for (; *p; p++) {
621 if (strchr(SHELL_NEED_ESCAPE, *p))
622 fputc_unlocked('\\', f);
623
624 fputc_unlocked(*p, f);
625 }
626
627 fputc_unlocked('"', f);
628 } else
629 fputs_unlocked(p, f);
630
631 funlockfile(f);
632 }
633
634 void env_file_fputs_assignment(FILE *f, const char *k, const char *v) {
635 assert(f);
636 assert(k);
637
638 if (!v)
639 return;
640
641 fputs(k, f);
642 env_file_fputs_escaped(f, v);
643 fputc('\n', f);
644 }
645
646 static void write_env_var(FILE *f, const char *v) {
647 const char *p;
648
649 assert(f);
650 assert(v);
651
652 p = strchr(v, '=');
653 if (!p) {
654 /* Fallback */
655 fputs_unlocked(v, f);
656 fputc_unlocked('\n', f);
657 return;
658 }
659
660 p++;
661 fwrite_unlocked(v, 1, p-v, f);
662
663 env_file_fputs_escaped(f, p);
664
665 fputc_unlocked('\n', f);
666 }
667
668 int write_env_file(int dir_fd, const char *fname, char **headers, char **l, WriteEnvFileFlags flags) {
669 _cleanup_fclose_ FILE *f = NULL;
670 _cleanup_free_ char *p = NULL;
671 int r;
672
673 assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
674 assert(fname);
675
676 bool call_label_ops_post = false;
677 if (FLAGS_SET(flags, WRITE_ENV_FILE_LABEL)) {
678 r = label_ops_pre(dir_fd, fname, S_IFREG);
679 if (r < 0)
680 return r;
681
682 call_label_ops_post = true;
683 }
684
685 r = fopen_tmpfile_linkable_at(dir_fd, fname, O_WRONLY|O_CLOEXEC, &p, &f);
686 int k = call_label_ops_post ? label_ops_post(f ? fileno(f) : dir_fd, f ? NULL : fname, /* created= */ !!f) : 0;
687 if (r < 0)
688 return r;
689 CLEANUP_TMPFILE_AT(dir_fd, p);
690 if (k < 0)
691 return k;
692
693 r = fchmod_umask(fileno(f), 0644);
694 if (r < 0)
695 return r;
696
697 STRV_FOREACH(i, headers) {
698 assert(isempty(*i) || startswith(*i, "#"));
699 fputs_unlocked(*i, f);
700 fputc_unlocked('\n', f);
701 }
702
703 STRV_FOREACH(i, l)
704 write_env_var(f, *i);
705
706 r = flink_tmpfile_at(f, dir_fd, p, fname, LINK_TMPFILE_REPLACE|LINK_TMPFILE_SYNC);
707 if (r < 0)
708 return r;
709
710 p = mfree(p); /* disarm CLEANUP_TMPFILE_AT() */
711
712 return 0;
713 }
714
715 int write_vconsole_conf(int dir_fd, const char *fname, char **l) {
716 char **headers = STRV_MAKE(
717 "# Written by systemd-localed(8) or systemd-firstboot(1), read by systemd-localed",
718 "# and systemd-vconsole-setup(8). Use localectl(1) to update this file.");
719
720 return write_env_file(dir_fd, fname, headers, l, WRITE_ENV_FILE_LABEL);
721 }