Fix CVE-2016-1531
[exim.git] / src / src / environment.c
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
12 /* The cleanup_environment() function is used during the startup phase
13 of the Exim process, right after reading the configurations main
14 part, before any expansions take place. It retains the environment
15 variables we trust (via the keep_environment option) and allows to
16 set additional variables (via add_environment).
17
18 Returns:    TRUE if successful
19             FALSE otherwise
20 */
21
22 BOOL
23 cleanup_environment()
24 {
25 if (!keep_environment || *keep_environment == '\0')
26   clearenv();
27 else if (Ustrcmp(keep_environment, "*") != 0)
28   {
29   uschar **p;
30   if (environ) for (p = USS environ; *p; /* see below */)
31     {
32     uschar *name = string_copyn(*p, US Ustrchr(*p, '=') - *p);
33
34     if (OK != match_isinlist(name, CUSS &keep_environment,
35         0, NULL, NULL, MCL_NOEXPAND, FALSE, NULL))
36       if (unsetenv(CS name) < 0) return FALSE;
37       else /* nothing */;
38     else
39       p++;
40
41     store_reset(name);
42     }
43   }
44 if (add_environment)
45   {
46     uschar *p;
47     int sep = 0;
48     const uschar* envlist = add_environment;
49     while ((p = string_nextinlist(&envlist, &sep, NULL, 0)))
50         putenv(CS p);
51   }
52
53   return TRUE;
54 }