Initial commit for Tizen
[profile/extras/shadow-utils.git] / libmisc / env.c
1 /*
2  * Copyright (c) 1989 - 1992, Julianne Frances Haugh
3  * Copyright (c) 1996 - 1999, Marek Michałkiewicz
4  * Copyright (c) 2003 - 2005, Tomasz Kłoczko
5  * Copyright (c) 2008 - 2009, Nicolas François
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the copyright holders or contributors may not be used to
17  *    endorse or promote products derived from this software without
18  *    specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
24  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <config.h>
34
35 #ident "$Id: env.c 2818 2009-04-27 20:07:59Z nekral-guest $"
36
37 #include <assert.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include "prototypes.h"
42 #include "defines.h"
43 /*
44  * NEWENVP_STEP must be a power of two.  This is the number
45  * of (char *) pointers to allocate at a time, to avoid using
46  * realloc() too often.
47  */
48 #define NEWENVP_STEP 16
49 size_t newenvc = 0;
50 /*@null@*/char **newenvp = NULL;
51 extern char **environ;
52
53 static const char *forbid[] = {
54         "_RLD_=",
55         "BASH_ENV=",            /* GNU creeping featurism strikes again... */
56         "ENV=",
57         "HOME=",
58         "IFS=",
59         "KRB_CONF=",
60         "LD_",                  /* anything with the LD_ prefix */
61         "LIBPATH=",
62         "MAIL=",
63         "NLSPATH=",
64         "PATH=",
65         "SHELL=",
66         "SHLIB_PATH=",
67         (char *) 0
68 };
69
70 /* these are allowed, but with no slashes inside
71    (to work around security problems in GNU gettext) */
72 static const char *noslash[] = {
73         "LANG=",
74         "LANGUAGE=",
75         "LC_",                  /* anything with the LC_ prefix */
76         (char *) 0
77 };
78
79 /*
80  * initenv() must be called once before using addenv().
81  */
82 void initenv (void)
83 {
84         newenvp = (char **) xmalloc (NEWENVP_STEP * sizeof (char *));
85         *newenvp = NULL;
86 }
87
88
89 void addenv (const char *string, /*@null@*/const char *value)
90 {
91         char *cp, *newstring;
92         size_t i;
93         size_t n;
94
95         if (NULL != value) {
96                 size_t len = strlen (string) + strlen (value) + 2;
97                 int wlen;
98                 newstring = xmalloc (len);
99                 wlen = snprintf (newstring, len, "%s=%s", string, value);
100                 assert (wlen == (int) len -1);
101         } else {
102                 newstring = xstrdup (string);
103         }
104
105         /*
106          * Search for a '=' character within the string and if none is found
107          * just ignore the whole string.
108          */
109
110         cp = strchr (newstring, '=');
111         if (NULL == cp) {
112                 free (newstring);
113                 return;
114         }
115
116         n = (size_t) (cp - newstring);
117
118         for (i = 0; i < newenvc; i++) {
119                 if (   (strncmp (newstring, newenvp[i], n) == 0)
120                     && (('=' == newenvp[i][n]) || ('\0' == newenvp[i][n]))) {
121                         break;
122                 }
123         }
124
125         if (i < newenvc) {
126                 free (newenvp[i]);
127                 newenvp[i] = newstring;
128                 return;
129         }
130
131         newenvp[newenvc++] = newstring;
132
133         /*
134          * Check whether newenvc is a multiple of NEWENVP_STEP.
135          * If so we have to resize the vector.
136          * the expression (newenvc & (NEWENVP_STEP - 1)) == 0
137          * is equal to    (newenvc %  NEWENVP_STEP) == 0
138          * as long as NEWENVP_STEP is a power of 2.
139          */
140
141         if ((newenvc & (NEWENVP_STEP - 1)) == 0) {
142                 char **__newenvp;
143                 size_t newsize;
144
145                 /*
146                  * If the resize operation succeds we can
147                  * happily go on, else print a message.
148                  */
149
150                 newsize = (newenvc + NEWENVP_STEP) * sizeof (char *);
151                 __newenvp = (char **) realloc (newenvp, newsize);
152
153                 if (__newenvp) {
154                         /*
155                          * If this is our current environment, update
156                          * environ so that it doesn't point to some
157                          * free memory area (realloc() could move it).
158                          */
159                         if (environ == newenvp) {
160                                 environ = __newenvp;
161                         }
162                         newenvp = __newenvp;
163                 } else {
164                         (void) fputs (_("Environment overflow\n"), stderr);
165                         newenvc--;
166                         free (newenvp[newenvc]);
167                 }
168         }
169
170         /*
171          * The last entry of newenvp must be NULL
172          */
173
174         newenvp[newenvc] = NULL;
175 }
176
177
178 /*
179  * set_env - copy command line arguments into the environment
180  */
181 void set_env (int argc, char *const *argv)
182 {
183         int noname = 1;
184         char variable[1024];
185         char *cp;
186
187         for (; argc > 0; argc--, argv++) {
188                 if (strlen (*argv) >= sizeof variable) {
189                         continue;       /* ignore long entries */
190                 }
191
192                 cp = strchr (*argv, '=');
193                 if (NULL == cp) {
194                         int wlen;
195                         wlen = snprintf (variable, sizeof variable, "L%d", noname);
196                         assert (wlen < (int) sizeof(variable));
197                         noname++;
198                         addenv (variable, *argv);
199                 } else {
200                         const char **p;
201
202                         for (p = forbid; NULL != *p; p++) {
203                                 if (strncmp (*argv, *p, strlen (*p)) == 0) {
204                                         break;
205                                 }
206                         }
207
208                         if (NULL != *p) {
209                                 strncpy (variable, *argv, (size_t)(cp - *argv));
210                                 variable[cp - *argv] = '\0';
211                                 printf (_("You may not change $%s\n"),
212                                         variable);
213                                 continue;
214                         }
215
216                         addenv (*argv, NULL);
217                 }
218         }
219 }
220
221 /*
222  * sanitize_env - remove some nasty environment variables
223  * If you fall into a total paranoia, you should call this
224  * function for any root-setuid program or anything the user
225  * might change the environment with. 99% useless as almost
226  * all modern Unixes will handle setuid executables properly,
227  * but... I feel better with that silly precaution. -j.
228  */
229
230 void sanitize_env (void)
231 {
232         char **envp = environ;
233         const char **bad;
234         char **cur;
235         char **move;
236
237         for (cur = envp; NULL != *cur; cur++) {
238                 for (bad = forbid; NULL != *bad; bad++) {
239                         if (strncmp (*cur, *bad, strlen (*bad)) == 0) {
240                                 for (move = cur; NULL != *move; move++) {
241                                         *move = *(move + 1);
242                                 }
243                                 cur--;
244                                 break;
245                         }
246                 }
247         }
248
249         for (cur = envp; NULL != *cur; cur++) {
250                 for (bad = noslash; NULL != *bad; bad++) {
251                         if (strncmp (*cur, *bad, strlen (*bad)) != 0) {
252                                 continue;
253                         }
254                         if (strchr (*cur, '/') != NULL) {
255                                 continue;       /* OK */
256                         }
257                         for (move = cur; NULL != *move; move++) {
258                                 *move = *(move + 1);
259                         }
260                         cur--;
261                         break;
262                 }
263         }
264 }
265