xref: /netbsd-src/external/ibm-public/postfix/dist/src/util/clean_env.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: clean_env.c,v 1.2 2020/03/18 19:05:21 christos Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	clean_env 3
6 /* SUMMARY
7 /*	clean up the environment
8 /* SYNOPSIS
9 /*	#include <clean_env.h>
10 /*
11 /*	void	clean_env(preserve_list)
12 /*	const char **preserve_list;
13 /*
14 /*	void	update_env(preserve_list)
15 /*	const char **preserve_list;
16 /* DESCRIPTION
17 /*	clean_env() reduces the process environment to the bare minimum.
18 /*	The function takes a null-terminated list of arguments.
19 /*	Each argument specifies the name of an environment variable
20 /*	that should be preserved, or specifies a name=value that should
21 /*	be entered into the new environment.
22 /*
23 /*	update_env() applies name=value settings, but otherwise does not
24 /*	change the process environment.
25 /* DIAGNOSTICS
26 /*	Fatal error: out of memory.
27 /* SEE ALSO
28 /*	safe_getenv(3), guarded getenv()
29 /* LICENSE
30 /* .ad
31 /* .fi
32 /*	The Secure Mailer license must be distributed with this software.
33 /* AUTHOR(S)
34 /*	Wietse Venema
35 /*	IBM T.J. Watson Research
36 /*	P.O. Box 704
37 /*	Yorktown Heights, NY 10598, USA
38 /*
39 /*	Wietse Venema
40 /*	Google, Inc.
41 /*	111 8th Avenue
42 /*	New York, NY 10011, USA
43 /*--*/
44 
45 /* System library. */
46 
47 #include <sys_defs.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50 #include <string.h>
51 
52 /* Utility library. */
53 
54 #include <msg.h>
55 #include <argv.h>
56 #include <safe.h>
57 #include <clean_env.h>
58 
59 /* clean_env - clean up the environment */
60 
61 void    clean_env(char **preserve_list)
62 {
63     extern char **environ;
64     ARGV   *save_list;
65     char   *value;
66     char  **cpp;
67     char   *eq;
68 
69     /*
70      * Preserve or specify selected environment variables.
71      */
72 #define STRING_AND_LENGTH(x, y) (x), (ssize_t) (y)
73 
74     save_list = argv_alloc(10);
75     for (cpp = preserve_list; *cpp; cpp++)
76 	if ((eq = strchr(*cpp, '=')) != 0)
77 	    argv_addn(save_list, STRING_AND_LENGTH(*cpp, eq - *cpp),
78 		      STRING_AND_LENGTH(eq + 1, strlen(eq + 1)), (char *) 0);
79 	else if ((value = safe_getenv(*cpp)) != 0)
80 	    argv_add(save_list, *cpp, value, (char *) 0);
81 
82     /*
83      * Truncate the process environment, if available. On some systems
84      * (Ultrix!), environ can be a null pointer.
85      */
86     if (environ)
87 	environ[0] = 0;
88 
89     /*
90      * Restore preserved environment variables.
91      */
92     for (cpp = save_list->argv; *cpp; cpp += 2)
93 	if (setenv(cpp[0], cpp[1], 1))
94 	    msg_fatal("setenv(%s, %s): %m", cpp[0], cpp[1]);
95 
96     /*
97      * Cleanup.
98      */
99     argv_free(save_list);
100 }
101 
102 /* update_env - apply name=value settings only */
103 
104 void    update_env(char **preserve_list)
105 {
106     char  **cpp;
107     ARGV   *save_list;
108     char   *eq;
109 
110     /*
111      * Extract name=value settings.
112      */
113     save_list = argv_alloc(10);
114     for (cpp = preserve_list; *cpp; cpp++)
115 	if ((eq = strchr(*cpp, '=')) != 0)
116 	    argv_addn(save_list, STRING_AND_LENGTH(*cpp, eq - *cpp),
117 		      STRING_AND_LENGTH(eq + 1, strlen(eq + 1)), (char *) 0);
118 
119     /*
120      * Apply name=value settings.
121      */
122     for (cpp = save_list->argv; *cpp; cpp += 2)
123 	if (setenv(cpp[0], cpp[1], 1))
124 	    msg_fatal("setenv(%s, %s): %m", cpp[0], cpp[1]);
125 
126     /*
127      * Cleanup.
128      */
129     argv_free(save_list);
130 }
131