]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/env-util.c
tree-wide: use mfree more
[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 "extract-word.h"
30 #include "macro.h"
31 #include "parse-util.h"
32 #include "string-util.h"
33 #include "strv.h"
34 #include "utf8.h"
35
36 #define VALID_CHARS_ENV_NAME \
37 DIGITS LETTERS \
38 "_"
39
40 #ifndef ARG_MAX
41 #define ARG_MAX ((size_t) sysconf(_SC_ARG_MAX))
42 #endif
43
44 static bool env_name_is_valid_n(const char *e, size_t n) {
45 const char *p;
46
47 if (!e)
48 return false;
49
50 if (n <= 0)
51 return false;
52
53 if (e[0] >= '0' && e[0] <= '9')
54 return false;
55
56 /* POSIX says the overall size of the environment block cannot
57 * be > ARG_MAX, an individual assignment hence cannot be
58 * either. Discounting the equal sign and trailing NUL this
59 * hence leaves ARG_MAX-2 as longest possible variable
60 * name. */
61 if (n > ARG_MAX - 2)
62 return false;
63
64 for (p = e; p < e + n; p++)
65 if (!strchr(VALID_CHARS_ENV_NAME, *p))
66 return false;
67
68 return true;
69 }
70
71 bool env_name_is_valid(const char *e) {
72 if (!e)
73 return false;
74
75 return env_name_is_valid_n(e, strlen(e));
76 }
77
78 bool env_value_is_valid(const char *e) {
79 if (!e)
80 return false;
81
82 if (!utf8_is_valid(e))
83 return false;
84
85 /* bash allows tabs in environment variables, and so should
86 * we */
87 if (string_has_cc(e, "\t"))
88 return false;
89
90 /* POSIX says the overall size of the environment block cannot
91 * be > ARG_MAX, an individual assignment hence cannot be
92 * either. Discounting the shortest possible variable name of
93 * length 1, the equal sign and trailing NUL this hence leaves
94 * ARG_MAX-3 as longest possible variable value. */
95 if (strlen(e) > ARG_MAX - 3)
96 return false;
97
98 return true;
99 }
100
101 bool env_assignment_is_valid(const char *e) {
102 const char *eq;
103
104 eq = strchr(e, '=');
105 if (!eq)
106 return false;
107
108 if (!env_name_is_valid_n(e, eq - e))
109 return false;
110
111 if (!env_value_is_valid(eq + 1))
112 return false;
113
114 /* POSIX says the overall size of the environment block cannot
115 * be > ARG_MAX, hence the individual variable assignments
116 * cannot be either, but let's leave room for one trailing NUL
117 * byte. */
118 if (strlen(e) > ARG_MAX - 1)
119 return false;
120
121 return true;
122 }
123
124 bool strv_env_is_valid(char **e) {
125 char **p, **q;
126
127 STRV_FOREACH(p, e) {
128 size_t k;
129
130 if (!env_assignment_is_valid(*p))
131 return false;
132
133 /* Check if there are duplicate assginments */
134 k = strcspn(*p, "=");
135 STRV_FOREACH(q, p + 1)
136 if (strneq(*p, *q, k) && (*q)[k] == '=')
137 return false;
138 }
139
140 return true;
141 }
142
143 bool strv_env_name_is_valid(char **l) {
144 char **p, **q;
145
146 STRV_FOREACH(p, l) {
147 if (!env_name_is_valid(*p))
148 return false;
149
150 STRV_FOREACH(q, p + 1)
151 if (streq(*p, *q))
152 return false;
153 }
154
155 return true;
156 }
157
158 bool strv_env_name_or_assignment_is_valid(char **l) {
159 char **p, **q;
160
161 STRV_FOREACH(p, l) {
162 if (!env_assignment_is_valid(*p) && !env_name_is_valid(*p))
163 return false;
164
165 STRV_FOREACH(q, p + 1)
166 if (streq(*p, *q))
167 return false;
168 }
169
170 return true;
171 }
172
173 static int env_append(char **r, char ***k, char **a) {
174 assert(r);
175 assert(k);
176
177 if (!a)
178 return 0;
179
180 /* Add the entries of a to *k unless they already exist in *r
181 * in which case they are overridden instead. This assumes
182 * there is enough space in the r array. */
183
184 for (; *a; a++) {
185 char **j;
186 size_t n;
187
188 n = strcspn(*a, "=");
189
190 if ((*a)[n] == '=')
191 n++;
192
193 for (j = r; j < *k; j++)
194 if (strneq(*j, *a, n))
195 break;
196
197 if (j >= *k)
198 (*k)++;
199 else
200 free(*j);
201
202 *j = strdup(*a);
203 if (!*j)
204 return -ENOMEM;
205 }
206
207 return 0;
208 }
209
210 char **strv_env_merge(unsigned n_lists, ...) {
211 size_t n = 0;
212 char **l, **k, **r;
213 va_list ap;
214 unsigned i;
215
216 /* Merges an arbitrary number of environment sets */
217
218 va_start(ap, n_lists);
219 for (i = 0; i < n_lists; i++) {
220 l = va_arg(ap, char**);
221 n += strv_length(l);
222 }
223 va_end(ap);
224
225 r = new(char*, n+1);
226 if (!r)
227 return NULL;
228
229 k = r;
230
231 va_start(ap, n_lists);
232 for (i = 0; i < n_lists; i++) {
233 l = va_arg(ap, char**);
234 if (env_append(r, &k, l) < 0)
235 goto fail;
236 }
237 va_end(ap);
238
239 *k = NULL;
240
241 return r;
242
243 fail:
244 va_end(ap);
245 strv_free(r);
246
247 return NULL;
248 }
249
250 _pure_ static bool env_match(const char *t, const char *pattern) {
251 assert(t);
252 assert(pattern);
253
254 /* pattern a matches string a
255 * a matches a=
256 * a matches a=b
257 * a= matches a=
258 * a=b matches a=b
259 * a= does not match a
260 * a=b does not match a=
261 * a=b does not match a
262 * a=b does not match a=c */
263
264 if (streq(t, pattern))
265 return true;
266
267 if (!strchr(pattern, '=')) {
268 size_t l = strlen(pattern);
269
270 return strneq(t, pattern, l) && t[l] == '=';
271 }
272
273 return false;
274 }
275
276 char **strv_env_delete(char **x, unsigned n_lists, ...) {
277 size_t n, i = 0;
278 char **k, **r;
279 va_list ap;
280
281 /* Deletes every entry from x that is mentioned in the other
282 * string lists */
283
284 n = strv_length(x);
285
286 r = new(char*, n+1);
287 if (!r)
288 return NULL;
289
290 STRV_FOREACH(k, x) {
291 unsigned v;
292
293 va_start(ap, n_lists);
294 for (v = 0; v < n_lists; v++) {
295 char **l, **j;
296
297 l = va_arg(ap, char**);
298 STRV_FOREACH(j, l)
299 if (env_match(*k, *j))
300 goto skip;
301 }
302 va_end(ap);
303
304 r[i] = strdup(*k);
305 if (!r[i]) {
306 strv_free(r);
307 return NULL;
308 }
309
310 i++;
311 continue;
312
313 skip:
314 va_end(ap);
315 }
316
317 r[i] = NULL;
318
319 assert(i <= n);
320
321 return r;
322 }
323
324 char **strv_env_unset(char **l, const char *p) {
325
326 char **f, **t;
327
328 if (!l)
329 return NULL;
330
331 assert(p);
332
333 /* Drops every occurrence of the env var setting p in the
334 * string list. Edits in-place. */
335
336 for (f = t = l; *f; f++) {
337
338 if (env_match(*f, p)) {
339 free(*f);
340 continue;
341 }
342
343 *(t++) = *f;
344 }
345
346 *t = NULL;
347 return l;
348 }
349
350 char **strv_env_unset_many(char **l, ...) {
351
352 char **f, **t;
353
354 if (!l)
355 return NULL;
356
357 /* Like strv_env_unset() but applies many at once. Edits in-place. */
358
359 for (f = t = l; *f; f++) {
360 bool found = false;
361 const char *p;
362 va_list ap;
363
364 va_start(ap, l);
365
366 while ((p = va_arg(ap, const char*))) {
367 if (env_match(*f, p)) {
368 found = true;
369 break;
370 }
371 }
372
373 va_end(ap);
374
375 if (found) {
376 free(*f);
377 continue;
378 }
379
380 *(t++) = *f;
381 }
382
383 *t = NULL;
384 return l;
385 }
386
387 char **strv_env_set(char **x, const char *p) {
388
389 char **k, **r;
390 char* m[2] = { (char*) p, NULL };
391
392 /* Overrides the env var setting of p, returns a new copy */
393
394 r = new(char*, strv_length(x)+2);
395 if (!r)
396 return NULL;
397
398 k = r;
399 if (env_append(r, &k, x) < 0)
400 goto fail;
401
402 if (env_append(r, &k, m) < 0)
403 goto fail;
404
405 *k = NULL;
406
407 return r;
408
409 fail:
410 strv_free(r);
411 return NULL;
412 }
413
414 char *strv_env_get_n(char **l, const char *name, size_t k) {
415 char **i;
416
417 assert(name);
418
419 if (k <= 0)
420 return NULL;
421
422 STRV_FOREACH(i, l)
423 if (strneq(*i, name, k) &&
424 (*i)[k] == '=')
425 return *i + k + 1;
426
427 return NULL;
428 }
429
430 char *strv_env_get(char **l, const char *name) {
431 assert(name);
432
433 return strv_env_get_n(l, name, strlen(name));
434 }
435
436 char **strv_env_clean_with_callback(char **e, void (*invalid_callback)(const char *p, void *userdata), void *userdata) {
437 char **p, **q;
438 int k = 0;
439
440 STRV_FOREACH(p, e) {
441 size_t n;
442 bool duplicate = false;
443
444 if (!env_assignment_is_valid(*p)) {
445 if (invalid_callback)
446 invalid_callback(*p, userdata);
447 free(*p);
448 continue;
449 }
450
451 n = strcspn(*p, "=");
452 STRV_FOREACH(q, p + 1)
453 if (strneq(*p, *q, n) && (*q)[n] == '=') {
454 duplicate = true;
455 break;
456 }
457
458 if (duplicate) {
459 free(*p);
460 continue;
461 }
462
463 e[k++] = *p;
464 }
465
466 if (e)
467 e[k] = NULL;
468
469 return e;
470 }
471
472 char *replace_env(const char *format, char **env) {
473 enum {
474 WORD,
475 CURLY,
476 VARIABLE
477 } state = WORD;
478
479 const char *e, *word = format;
480 char *r = NULL, *k;
481
482 assert(format);
483
484 for (e = format; *e; e ++) {
485
486 switch (state) {
487
488 case WORD:
489 if (*e == '$')
490 state = CURLY;
491 break;
492
493 case CURLY:
494 if (*e == '{') {
495 k = strnappend(r, word, e-word-1);
496 if (!k)
497 goto fail;
498
499 free(r);
500 r = k;
501
502 word = e-1;
503 state = VARIABLE;
504
505 } else if (*e == '$') {
506 k = strnappend(r, word, e-word);
507 if (!k)
508 goto fail;
509
510 free(r);
511 r = k;
512
513 word = e+1;
514 state = WORD;
515 } else
516 state = WORD;
517 break;
518
519 case VARIABLE:
520 if (*e == '}') {
521 const char *t;
522
523 t = strempty(strv_env_get_n(env, word+2, e-word-2));
524
525 k = strappend(r, t);
526 if (!k)
527 goto fail;
528
529 free(r);
530 r = k;
531
532 word = e+1;
533 state = WORD;
534 }
535 break;
536 }
537 }
538
539 k = strnappend(r, word, e-word);
540 if (!k)
541 goto fail;
542
543 free(r);
544 return k;
545
546 fail:
547 return mfree(r);
548 }
549
550 char **replace_env_argv(char **argv, char **env) {
551 char **ret, **i;
552 unsigned k = 0, l = 0;
553
554 l = strv_length(argv);
555
556 ret = new(char*, l+1);
557 if (!ret)
558 return NULL;
559
560 STRV_FOREACH(i, argv) {
561
562 /* If $FOO appears as single word, replace it by the split up variable */
563 if ((*i)[0] == '$' && (*i)[1] != '{' && (*i)[1] != '$') {
564 char *e;
565 char **w, **m = NULL;
566 unsigned q;
567
568 e = strv_env_get(env, *i+1);
569 if (e) {
570 int r;
571
572 r = strv_split_extract(&m, e, WHITESPACE, EXTRACT_RELAX|EXTRACT_QUOTES);
573 if (r < 0) {
574 ret[k] = NULL;
575 strv_free(ret);
576 return NULL;
577 }
578 } else
579 m = NULL;
580
581 q = strv_length(m);
582 l = l + q - 1;
583
584 w = realloc(ret, sizeof(char*) * (l+1));
585 if (!w) {
586 ret[k] = NULL;
587 strv_free(ret);
588 strv_free(m);
589 return NULL;
590 }
591
592 ret = w;
593 if (m) {
594 memcpy(ret + k, m, q * sizeof(char*));
595 free(m);
596 }
597
598 k += q;
599 continue;
600 }
601
602 /* If ${FOO} appears as part of a word, replace it by the variable as-is */
603 ret[k] = replace_env(*i, env);
604 if (!ret[k]) {
605 strv_free(ret);
606 return NULL;
607 }
608 k++;
609 }
610
611 ret[k] = NULL;
612 return ret;
613 }
614
615 int getenv_bool(const char *p) {
616 const char *e;
617
618 e = getenv(p);
619 if (!e)
620 return -ENXIO;
621
622 return parse_boolean(e);
623 }