]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/env-util.c
basic: add new merge_env_file function
[thirdparty/systemd.git] / src / basic / env-util.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2012 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <limits.h>
22 #include <stdarg.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include "alloc-util.h"
28 #include "env-util.h"
29 #include "escape.h"
30 #include "extract-word.h"
31 #include "macro.h"
32 #include "parse-util.h"
33 #include "string-util.h"
34 #include "strv.h"
35 #include "utf8.h"
36
37 #define VALID_CHARS_ENV_NAME \
38 DIGITS LETTERS \
39 "_"
40
41 #ifndef ARG_MAX
42 #define ARG_MAX ((size_t) sysconf(_SC_ARG_MAX))
43 #endif
44
45 static bool env_name_is_valid_n(const char *e, size_t n) {
46 const char *p;
47
48 if (!e)
49 return false;
50
51 if (n <= 0)
52 return false;
53
54 if (e[0] >= '0' && e[0] <= '9')
55 return false;
56
57 /* POSIX says the overall size of the environment block cannot
58 * be > ARG_MAX, an individual assignment hence cannot be
59 * either. Discounting the equal sign and trailing NUL this
60 * hence leaves ARG_MAX-2 as longest possible variable
61 * name. */
62 if (n > ARG_MAX - 2)
63 return false;
64
65 for (p = e; p < e + n; p++)
66 if (!strchr(VALID_CHARS_ENV_NAME, *p))
67 return false;
68
69 return true;
70 }
71
72 bool env_name_is_valid(const char *e) {
73 if (!e)
74 return false;
75
76 return env_name_is_valid_n(e, strlen(e));
77 }
78
79 bool env_value_is_valid(const char *e) {
80 if (!e)
81 return false;
82
83 if (!utf8_is_valid(e))
84 return false;
85
86 /* bash allows tabs in environment variables, and so should
87 * we */
88 if (string_has_cc(e, "\t"))
89 return false;
90
91 /* POSIX says the overall size of the environment block cannot
92 * be > ARG_MAX, an individual assignment hence cannot be
93 * either. Discounting the shortest possible variable name of
94 * length 1, the equal sign and trailing NUL this hence leaves
95 * ARG_MAX-3 as longest possible variable value. */
96 if (strlen(e) > ARG_MAX - 3)
97 return false;
98
99 return true;
100 }
101
102 bool env_assignment_is_valid(const char *e) {
103 const char *eq;
104
105 eq = strchr(e, '=');
106 if (!eq)
107 return false;
108
109 if (!env_name_is_valid_n(e, eq - e))
110 return false;
111
112 if (!env_value_is_valid(eq + 1))
113 return false;
114
115 /* POSIX says the overall size of the environment block cannot
116 * be > ARG_MAX, hence the individual variable assignments
117 * cannot be either, but let's leave room for one trailing NUL
118 * byte. */
119 if (strlen(e) > ARG_MAX - 1)
120 return false;
121
122 return true;
123 }
124
125 bool strv_env_is_valid(char **e) {
126 char **p, **q;
127
128 STRV_FOREACH(p, e) {
129 size_t k;
130
131 if (!env_assignment_is_valid(*p))
132 return false;
133
134 /* Check if there are duplicate assginments */
135 k = strcspn(*p, "=");
136 STRV_FOREACH(q, p + 1)
137 if (strneq(*p, *q, k) && (*q)[k] == '=')
138 return false;
139 }
140
141 return true;
142 }
143
144 bool strv_env_name_is_valid(char **l) {
145 char **p, **q;
146
147 STRV_FOREACH(p, l) {
148 if (!env_name_is_valid(*p))
149 return false;
150
151 STRV_FOREACH(q, p + 1)
152 if (streq(*p, *q))
153 return false;
154 }
155
156 return true;
157 }
158
159 bool strv_env_name_or_assignment_is_valid(char **l) {
160 char **p, **q;
161
162 STRV_FOREACH(p, l) {
163 if (!env_assignment_is_valid(*p) && !env_name_is_valid(*p))
164 return false;
165
166 STRV_FOREACH(q, p + 1)
167 if (streq(*p, *q))
168 return false;
169 }
170
171 return true;
172 }
173
174 static int env_append(char **r, char ***k, char **a) {
175 assert(r);
176 assert(k);
177
178 if (!a)
179 return 0;
180
181 /* Add the entries of a to *k unless they already exist in *r
182 * in which case they are overridden instead. This assumes
183 * there is enough space in the r array. */
184
185 for (; *a; a++) {
186 char **j;
187 size_t n;
188
189 n = strcspn(*a, "=");
190
191 if ((*a)[n] == '=')
192 n++;
193
194 for (j = r; j < *k; j++)
195 if (strneq(*j, *a, n))
196 break;
197
198 if (j >= *k)
199 (*k)++;
200 else
201 free(*j);
202
203 *j = strdup(*a);
204 if (!*j)
205 return -ENOMEM;
206 }
207
208 return 0;
209 }
210
211 char **strv_env_merge(unsigned n_lists, ...) {
212 size_t n = 0;
213 char **l, **k, **r;
214 va_list ap;
215 unsigned i;
216
217 /* Merges an arbitrary number of environment sets */
218
219 va_start(ap, n_lists);
220 for (i = 0; i < n_lists; i++) {
221 l = va_arg(ap, char**);
222 n += strv_length(l);
223 }
224 va_end(ap);
225
226 r = new(char*, n+1);
227 if (!r)
228 return NULL;
229
230 k = r;
231
232 va_start(ap, n_lists);
233 for (i = 0; i < n_lists; i++) {
234 l = va_arg(ap, char**);
235 if (env_append(r, &k, l) < 0)
236 goto fail;
237 }
238 va_end(ap);
239
240 *k = NULL;
241
242 return r;
243
244 fail:
245 va_end(ap);
246 strv_free(r);
247
248 return NULL;
249 }
250
251 static bool env_match(const char *t, const char *pattern) {
252 assert(t);
253 assert(pattern);
254
255 /* pattern a matches string a
256 * a matches a=
257 * a matches a=b
258 * a= matches a=
259 * a=b matches a=b
260 * a= does not match a
261 * a=b does not match a=
262 * a=b does not match a
263 * a=b does not match a=c */
264
265 if (streq(t, pattern))
266 return true;
267
268 if (!strchr(pattern, '=')) {
269 size_t l = strlen(pattern);
270
271 return strneq(t, pattern, l) && t[l] == '=';
272 }
273
274 return false;
275 }
276
277 static bool env_entry_has_name(const char *entry, const char *name) {
278 const char *t;
279
280 assert(entry);
281 assert(name);
282
283 t = startswith(entry, name);
284 if (!t)
285 return false;
286
287 return *t == '=';
288 }
289
290 char **strv_env_delete(char **x, unsigned n_lists, ...) {
291 size_t n, i = 0;
292 char **k, **r;
293 va_list ap;
294
295 /* Deletes every entry from x that is mentioned in the other
296 * string lists */
297
298 n = strv_length(x);
299
300 r = new(char*, n+1);
301 if (!r)
302 return NULL;
303
304 STRV_FOREACH(k, x) {
305 unsigned v;
306
307 va_start(ap, n_lists);
308 for (v = 0; v < n_lists; v++) {
309 char **l, **j;
310
311 l = va_arg(ap, char**);
312 STRV_FOREACH(j, l)
313 if (env_match(*k, *j))
314 goto skip;
315 }
316 va_end(ap);
317
318 r[i] = strdup(*k);
319 if (!r[i]) {
320 strv_free(r);
321 return NULL;
322 }
323
324 i++;
325 continue;
326
327 skip:
328 va_end(ap);
329 }
330
331 r[i] = NULL;
332
333 assert(i <= n);
334
335 return r;
336 }
337
338 char **strv_env_unset(char **l, const char *p) {
339
340 char **f, **t;
341
342 if (!l)
343 return NULL;
344
345 assert(p);
346
347 /* Drops every occurrence of the env var setting p in the
348 * string list. Edits in-place. */
349
350 for (f = t = l; *f; f++) {
351
352 if (env_match(*f, p)) {
353 free(*f);
354 continue;
355 }
356
357 *(t++) = *f;
358 }
359
360 *t = NULL;
361 return l;
362 }
363
364 char **strv_env_unset_many(char **l, ...) {
365
366 char **f, **t;
367
368 if (!l)
369 return NULL;
370
371 /* Like strv_env_unset() but applies many at once. Edits in-place. */
372
373 for (f = t = l; *f; f++) {
374 bool found = false;
375 const char *p;
376 va_list ap;
377
378 va_start(ap, l);
379
380 while ((p = va_arg(ap, const char*))) {
381 if (env_match(*f, p)) {
382 found = true;
383 break;
384 }
385 }
386
387 va_end(ap);
388
389 if (found) {
390 free(*f);
391 continue;
392 }
393
394 *(t++) = *f;
395 }
396
397 *t = NULL;
398 return l;
399 }
400
401 int strv_env_replace(char ***l, char *p) {
402 char **f;
403 const char *t, *name;
404
405 assert(p);
406
407 /* Replace first occurrence of the env var or add a new one in the
408 * string list. Drop other occurences. Edits in-place. Does not copy p.
409 * p must be a valid key=value assignment.
410 */
411
412 t = strchr(p, '=');
413 assert(t);
414
415 name = strndupa(p, t - p);
416
417 for (f = *l; f && *f; f++)
418 if (env_entry_has_name(*f, name)) {
419 free_and_replace(*f, p);
420 strv_env_unset(f + 1, *f);
421 return 0;
422 }
423
424 /* We didn't find a match, we need to append p or create a new strv */
425 if (strv_push(l, p) < 0)
426 return -ENOMEM;
427 return 1;
428 }
429
430 char **strv_env_set(char **x, const char *p) {
431
432 char **k, **r;
433 char* m[2] = { (char*) p, NULL };
434
435 /* Overrides the env var setting of p, returns a new copy */
436
437 r = new(char*, strv_length(x)+2);
438 if (!r)
439 return NULL;
440
441 k = r;
442 if (env_append(r, &k, x) < 0)
443 goto fail;
444
445 if (env_append(r, &k, m) < 0)
446 goto fail;
447
448 *k = NULL;
449
450 return r;
451
452 fail:
453 strv_free(r);
454 return NULL;
455 }
456
457 char *strv_env_get_n(char **l, const char *name, size_t k, unsigned flags) {
458 char **i;
459
460 assert(name);
461
462 if (k <= 0)
463 return NULL;
464
465 STRV_FOREACH_BACKWARDS(i, l)
466 if (strneq(*i, name, k) &&
467 (*i)[k] == '=')
468 return *i + k + 1;
469
470 if (flags & REPLACE_ENV_USE_ENVIRONMENT) {
471 const char *t;
472
473 t = strndupa(name, k);
474 return getenv(t);
475 };
476
477 return NULL;
478 }
479
480 char *strv_env_get(char **l, const char *name) {
481 assert(name);
482
483 return strv_env_get_n(l, name, strlen(name), 0);
484 }
485
486 char **strv_env_clean_with_callback(char **e, void (*invalid_callback)(const char *p, void *userdata), void *userdata) {
487 char **p, **q;
488 int k = 0;
489
490 STRV_FOREACH(p, e) {
491 size_t n;
492 bool duplicate = false;
493
494 if (!env_assignment_is_valid(*p)) {
495 if (invalid_callback)
496 invalid_callback(*p, userdata);
497 free(*p);
498 continue;
499 }
500
501 n = strcspn(*p, "=");
502 STRV_FOREACH(q, p + 1)
503 if (strneq(*p, *q, n) && (*q)[n] == '=') {
504 duplicate = true;
505 break;
506 }
507
508 if (duplicate) {
509 free(*p);
510 continue;
511 }
512
513 e[k++] = *p;
514 }
515
516 if (e)
517 e[k] = NULL;
518
519 return e;
520 }
521
522 char *replace_env(const char *format, char **env, unsigned flags) {
523 enum {
524 WORD,
525 CURLY,
526 VARIABLE
527 } state = WORD;
528
529 const char *e, *word = format;
530 char *r = NULL, *k;
531
532 assert(format);
533
534 for (e = format; *e; e ++) {
535
536 switch (state) {
537
538 case WORD:
539 if (*e == '$')
540 state = CURLY;
541 break;
542
543 case CURLY:
544 if (*e == '{') {
545 k = strnappend(r, word, e-word-1);
546 if (!k)
547 goto fail;
548
549 free(r);
550 r = k;
551
552 word = e-1;
553 state = VARIABLE;
554
555 } else if (*e == '$') {
556 k = strnappend(r, word, e-word);
557 if (!k)
558 goto fail;
559
560 free(r);
561 r = k;
562
563 word = e+1;
564 state = WORD;
565 } else
566 state = WORD;
567 break;
568
569 case VARIABLE:
570 if (*e == '}') {
571 const char *t;
572
573 t = strv_env_get_n(env, word+2, e-word-2, flags);
574
575 k = strappend(r, t);
576 if (!k)
577 goto fail;
578
579 free(r);
580 r = k;
581
582 word = e+1;
583 state = WORD;
584 }
585 break;
586 }
587 }
588
589 k = strnappend(r, word, e-word);
590 if (!k)
591 goto fail;
592
593 free(r);
594 return k;
595
596 fail:
597 return mfree(r);
598 }
599
600 char **replace_env_argv(char **argv, char **env) {
601 char **ret, **i;
602 unsigned k = 0, l = 0;
603
604 l = strv_length(argv);
605
606 ret = new(char*, l+1);
607 if (!ret)
608 return NULL;
609
610 STRV_FOREACH(i, argv) {
611
612 /* If $FOO appears as single word, replace it by the split up variable */
613 if ((*i)[0] == '$' && (*i)[1] != '{' && (*i)[1] != '$') {
614 char *e;
615 char **w, **m = NULL;
616 unsigned q;
617
618 e = strv_env_get(env, *i+1);
619 if (e) {
620 int r;
621
622 r = strv_split_extract(&m, e, WHITESPACE, EXTRACT_RELAX|EXTRACT_QUOTES);
623 if (r < 0) {
624 ret[k] = NULL;
625 strv_free(ret);
626 return NULL;
627 }
628 } else
629 m = NULL;
630
631 q = strv_length(m);
632 l = l + q - 1;
633
634 w = realloc(ret, sizeof(char*) * (l+1));
635 if (!w) {
636 ret[k] = NULL;
637 strv_free(ret);
638 strv_free(m);
639 return NULL;
640 }
641
642 ret = w;
643 if (m) {
644 memcpy(ret + k, m, q * sizeof(char*));
645 free(m);
646 }
647
648 k += q;
649 continue;
650 }
651
652 /* If ${FOO} appears as part of a word, replace it by the variable as-is */
653 ret[k] = replace_env(*i, env, 0);
654 if (!ret[k]) {
655 strv_free(ret);
656 return NULL;
657 }
658 k++;
659 }
660
661 ret[k] = NULL;
662 return ret;
663 }
664
665 int getenv_bool(const char *p) {
666 const char *e;
667
668 e = getenv(p);
669 if (!e)
670 return -ENXIO;
671
672 return parse_boolean(e);
673 }
674
675 int serialize_environment(FILE *f, char **environment) {
676 char **e;
677
678 STRV_FOREACH(e, environment) {
679 _cleanup_free_ char *ce;
680
681 ce = cescape(*e);
682 if (!ce)
683 return -ENOMEM;
684
685 fprintf(f, "env=%s\n", *e);
686 }
687
688 /* caller should call ferror() */
689
690 return 0;
691 }
692
693 int deserialize_environment(char ***environment, const char *line) {
694 char *uce = NULL;
695 int r;
696
697 assert(line);
698 assert(environment);
699
700 assert(startswith(line, "env="));
701 r = cunescape(line + 4, UNESCAPE_RELAX, &uce);
702 if (r < 0)
703 return r;
704
705 if (!env_assignment_is_valid(uce))
706 return -EINVAL;
707
708 return strv_env_replace(environment, uce);
709 }