Commit | Line | Data |
---|---|---|
bc3c7bb7 HSHR |
1 | /************************************************* |
2 | * Exim - an Internet mail transport agent * | |
3 | *************************************************/ | |
4 | ||
5 | /* Copyright (c) Heiko Schlittermann 2016 | |
6 | * hs@schlittermann.de | |
7 | * See the file NOTICE for conditions of use and distribution. | |
8 | */ | |
9 | ||
10 | #include "exim.h" | |
11 | ||
2478dbdf | 12 | extern char **environ; |
2478dbdf | 13 | |
bc3c7bb7 HSHR |
14 | /* The cleanup_environment() function is used during the startup phase |
15 | of the Exim process, right after reading the configurations main | |
16 | part, before any expansions take place. It retains the environment | |
17 | variables we trust (via the keep_environment option) and allows to | |
18 | set additional variables (via add_environment). | |
19 | ||
20 | Returns: TRUE if successful | |
21 | FALSE otherwise | |
22 | */ | |
23 | ||
24 | BOOL | |
25 | cleanup_environment() | |
26 | { | |
2e5d9e71 JH |
27 | int old_pool = store_pool; |
28 | store_pool = POOL_PERM; /* Need perm memory for any created env vars */ | |
29 | ||
bc3c7bb7 | 30 | if (!keep_environment || *keep_environment == '\0') |
2478dbdf HSHR |
31 | { |
32 | /* From: https://github.com/dovecot/core/blob/master/src/lib/env-util.c#L55 | |
33 | Try to clear the environment. | |
34 | a) environ = NULL crashes on OS X. | |
35 | b) *environ = NULL doesn't work on FreeBSD 7.0. | |
36 | c) environ = emptyenv doesn't work on Haiku OS | |
37 | d) environ = calloc() should work everywhere */ | |
38 | ||
39 | if (environ) *environ = NULL; | |
40 | ||
41 | } | |
bc3c7bb7 | 42 | else if (Ustrcmp(keep_environment, "*") != 0) |
f3ebb786 JH |
43 | { |
44 | rmark reset_point = store_mark(); | |
d7978c0f | 45 | if (environ) for (uschar ** p = USS environ; *p; /* see below */) |
bc3c7bb7 | 46 | { |
2478dbdf HSHR |
47 | /* It's considered broken if we do not find the '=', according to |
48 | Florian Weimer. For now we ignore such strings. unsetenv() would complain, | |
49 | getenv() would complain. */ | |
93a680e4 | 50 | uschar * eqp = Ustrchr(*p, '='); |
bc3c7bb7 | 51 | |
2478dbdf HSHR |
52 | if (eqp) |
53 | { | |
93a680e4 JH |
54 | uschar * name = string_copyn(*p, eqp - *p); |
55 | ||
2478dbdf HSHR |
56 | if (OK != match_isinlist(name, CUSS &keep_environment, |
57 | 0, NULL, NULL, MCL_NOEXPAND, FALSE, NULL)) | |
271019bd | 58 | if (os_unsetenv(name) < 0) return FALSE; |
2478dbdf HSHR |
59 | else p = USS environ; /* RESTART from the beginning */ |
60 | else p++; | |
2478dbdf | 61 | } |
bc3c7bb7 | 62 | } |
f3ebb786 JH |
63 | store_reset(reset_point); |
64 | } | |
bc3c7bb7 HSHR |
65 | if (add_environment) |
66 | { | |
f3ebb786 JH |
67 | uschar * p; |
68 | int sep = 0; | |
69 | const uschar * envlist = add_environment; | |
93a680e4 | 70 | |
2e5d9e71 JH |
71 | while ((p = string_nextinlist(&envlist, &sep, NULL, 0))) |
72 | { | |
73 | DEBUG(D_expand) debug_printf("adding %s\n", p); | |
74 | putenv(CS p); | |
75 | } | |
bc3c7bb7 | 76 | } |
2e5d9e71 JH |
77 | #ifndef DISABLE_TLS |
78 | tls_clean_env(); | |
79 | #endif | |
bc3c7bb7 | 80 | |
2e5d9e71 | 81 | store_pool = old_pool; |
f3ebb786 | 82 | return TRUE; |
bc3c7bb7 | 83 | } |