Use C99 initialisations for iterators
[exim.git] / src / src / environment.c
CommitLineData
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 12extern char **environ;
2478dbdf 13
bc3c7bb7
HSHR
14/* The cleanup_environment() function is used during the startup phase
15of the Exim process, right after reading the configurations main
16part, before any expansions take place. It retains the environment
17variables we trust (via the keep_environment option) and allows to
18set additional variables (via add_environment).
19
20Returns: TRUE if successful
21 FALSE otherwise
22*/
23
24BOOL
25cleanup_environment()
26{
27if (!keep_environment || *keep_environment == '\0')
2478dbdf
HSHR
28 {
29 /* From: https://github.com/dovecot/core/blob/master/src/lib/env-util.c#L55
30 Try to clear the environment.
31 a) environ = NULL crashes on OS X.
32 b) *environ = NULL doesn't work on FreeBSD 7.0.
33 c) environ = emptyenv doesn't work on Haiku OS
34 d) environ = calloc() should work everywhere */
35
36 if (environ) *environ = NULL;
37
38 }
bc3c7bb7 39else if (Ustrcmp(keep_environment, "*") != 0)
d7978c0f 40 if (environ) for (uschar ** p = USS environ; *p; /* see below */)
bc3c7bb7 41 {
2478dbdf
HSHR
42 /* It's considered broken if we do not find the '=', according to
43 Florian Weimer. For now we ignore such strings. unsetenv() would complain,
44 getenv() would complain. */
93a680e4 45 uschar * eqp = Ustrchr(*p, '=');
bc3c7bb7 46
2478dbdf
HSHR
47 if (eqp)
48 {
93a680e4
JH
49 uschar * name = string_copyn(*p, eqp - *p);
50
2478dbdf
HSHR
51 if (OK != match_isinlist(name, CUSS &keep_environment,
52 0, NULL, NULL, MCL_NOEXPAND, FALSE, NULL))
271019bd 53 if (os_unsetenv(name) < 0) return FALSE;
2478dbdf
HSHR
54 else p = USS environ; /* RESTART from the beginning */
55 else p++;
56 store_reset(name);
57 }
bc3c7bb7 58 }
bc3c7bb7
HSHR
59if (add_environment)
60 {
93a680e4 61 uschar * p;
bc3c7bb7 62 int sep = 0;
93a680e4
JH
63 const uschar * envlist = add_environment;
64
65 while ((p = string_nextinlist(&envlist, &sep, NULL, 0))) putenv(CS p);
bc3c7bb7
HSHR
66 }
67
68 return TRUE;
69}