-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.c
286 lines (244 loc) · 5.8 KB
/
env.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* Copyright 2024 Gaël PORTAY
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
/*
* GNU Bash supplies its own version of the environment functions getenv(),
* _getenv(), putenv(), setenv(), and unsetenv(); the function clearenv() is
* not supplied.
*
* As they said:
*
* We supply our own version of getenv () because we want library
* routines to get the changed values of exported variables.
*
* The three environment functions putenv(), setenv() and unsetenv() do nothing
* in the execve() context call: the two global variables environ and __environ
* remain unchanged. However, the function clearenv() clears the environ and
* set the two globals to NULL.
*
* All the environment functions are internaly supplied for the needs modifying
* the environment, including LD_PRELOAD/LD_LIBRARY_PATH if the dynamic loader
* do not support the options --preload/--library-path.
*
* It fixes the following error as LD_PRELOAD is STILL set to preload the GNU
* libc in version 2.29 in the chroot environment, but exec.sh is run via the
* /usr/bin/env linked against the GNU libc in version 2.38 in the host system:
*
* /usr/bin/env: /usr/lib64/libc-2.29.so: version `GLIBC_2.38' not found (required by /usr/bin/env)
* /usr/bin/env: /usr/lib64/libc-2.29.so: version `GLIBC_2.34' not found (required by /usr/bin/env)
* /usr/bin/env: /usr/lib64/libc-2.29.so: version `GLIBC_ABI_DT_RELR' not found (required by /usr/lib/libpthread.so.0)
*/
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include "iamroot.h"
int clearenv()
{
int ret;
ret = _clearenv();
__debug("%s() -> %i\n", __func__, ret);
return ret;
}
char *getenv(const char *name)
{
char *ret;
ret = _getenv(name);
__debug("%s(name: '%s') -> '%s'\n", __func__, name, ret);
return ret;
}
int putenv(char *string)
{
int ret;
ret = _putenv(string);
__debug("%s(string: '%s') -> %i\n", __func__, string, ret);
return ret;
}
int setenv(const char *name, const char *value, int overwrite)
{
int ret;
ret = _setenv(name, value, overwrite);
__debug("%s(name: '%s', value: '%s', overwrite: %i) -> %i\n", __func__,
name, value, overwrite, ret);
return ret;
}
int unsetenv(const char *name)
{
int ret;
ret = _unsetenv(name);
__debug("%s(name: '%s') -> %i\n", __func__, name, ret);
return ret;
}
#define clearenv _clearenv
#define getenv _getenv
#define putenv _putenv
#define setenv _setenv
#define unsetenv _unsetenv
void __env_rm_add(char *old, char *new);
/*
* Stolen from musl (src/env/clearenv.c)
*
* SPDX-FileCopyrightText: The musl Contributors
*
* SPDX-License-Identifier: MIT
*/
hidden int clearenv()
{
char **e = __environ;
__environ = 0;
if (e) while (*e) __env_rm_add(*e++, 0);
return 0;
}
/*
* Stolen from musl (src/env/getenv.c)
*
* SPDX-FileCopyrightText: The musl Contributors
*
* SPDX-License-Identifier: MIT
*/
hidden char *getenv(const char *name)
{
size_t l = __strchrnul(name, '=') - name;
if (l && !name[l] && __environ)
for (char **e = __environ; *e; e++)
if (!strncmp(name, *e, l) && l[*e] == '=')
return *e + l+1;
return 0;
}
/*
* Stolen from musl (src/env/putenv.c)
*
* SPDX-FileCopyrightText: The musl Contributors
*
* SPDX-License-Identifier: MIT
*/
hidden int __putenv(char *s, size_t l, char *r)
{
size_t i=0;
if (__environ) {
for (char **e = __environ; *e; e++, i++)
if (!strncmp(s, *e, l+1)) {
char *tmp = *e;
*e = s;
__env_rm_add(tmp, r);
return 0;
}
}
static char **oldenv;
char **newenv;
if (__environ == oldenv) {
newenv = realloc(oldenv, sizeof *newenv * (i+2));
if (!newenv) goto oom;
} else {
newenv = malloc(sizeof *newenv * (i+2));
if (!newenv) goto oom;
if (i) memcpy(newenv, __environ, sizeof *newenv * i);
free(oldenv);
}
newenv[i] = s;
newenv[i+1] = 0;
__environ = oldenv = newenv;
if (r) __env_rm_add(0, r);
return 0;
oom:
free(r);
return -1;
}
hidden int putenv(char *s)
{
size_t l = __strchrnul(s, '=') - s;
if (!l || !s[l]) return unsetenv(s);
return __putenv(s, l, 0);
}
/*
* Stolen from musl (src/env/setenv.c)
*
* SPDX-FileCopyrightText: The musl Contributors
*
* SPDX-License-Identifier: MIT
*/
hidden void __env_rm_add(char *old, char *new)
{
static char **env_alloced;
static size_t env_alloced_n;
for (size_t i=0; i < env_alloced_n; i++)
if (env_alloced[i] == old) {
env_alloced[i] = new;
free(old);
return;
} else if (!env_alloced[i] && new) {
env_alloced[i] = new;
new = 0;
}
if (!new) return;
char **t = realloc(env_alloced, sizeof *t * (env_alloced_n+1));
if (!t) return;
(env_alloced = t)[env_alloced_n++] = new;
}
hidden int setenv(const char *var, const char *value, int overwrite)
{
char *s;
size_t l1, l2;
if (!var || !(l1 = __strchrnul(var, '=') - var) || var[l1]) {
errno = EINVAL;
return -1;
}
if (!overwrite && getenv(var)) return 0;
l2 = strlen(value);
s = malloc(l1+l2+2);
if (!s) return -1;
memcpy(s, var, l1);
s[l1] = '=';
memcpy(s+l1+1, value, l2+1);
return __putenv(s, l1, s);
}
/*
* Stolen from musl (src/env/unsetenv.c)
*
* SPDX-FileCopyrightText: The musl Contributors
*
* SPDX-License-Identifier: MIT
*/
hidden int unsetenv(const char *name)
{
size_t l = __strchrnul(name, '=') - name;
if (!l || name[l]) {
errno = EINVAL;
return -1;
}
if (__environ) {
char **e = __environ, **eo = e;
for (; *e; e++)
if (!strncmp(name, *e, l) && l[*e] == '=')
__env_rm_add(*e, 0);
else if (eo != e)
*eo++ = *e;
else
eo++;
if (eo != e) *eo = 0;
}
return 0;
}
#undef clearenv
#undef getenv
#undef putenv
#undef setenv
#undef unsetenv
hidden char **_resetenv(char **envp)
{
char **env;
if (!__environ)
goto envp;
for (env = __environ; *env; env++)
if (!__strneq(*env, "IAMROOT"))
__env_rm_add(*env, NULL);
envp:
if (!envp)
goto exit;
for (env = envp; *env; env++)
_putenv(*env);
exit:
return __environ;
}