]> git.armaanb.net Git - opendoas.git/blob - env.c
redo the environment inheritance to not inherit. it was intended to make life easier...
[opendoas.git] / env.c
1 /* $OpenBSD$ */
2 /*
3  * Copyright (c) 2016 Ted Unangst <tedu@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 #include <sys/types.h>
19 #include "sys-tree.h"
20
21 #include <string.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <err.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <pwd.h>
28
29 #include "doas.h"
30 #include "includes.h"
31
32 struct envnode {
33         RB_ENTRY(envnode) node;
34         const char *key;
35         const char *value;
36 };
37
38 struct env {
39         RB_HEAD(envtree, envnode) root;
40         u_int count;
41 };
42
43 static void fillenv(struct env *env, const char **envlist);
44
45 static int
46 envcmp(struct envnode *a, struct envnode *b)
47 {
48         return strcmp(a->key, b->key);
49 }
50 RB_GENERATE_STATIC(envtree, envnode, node, envcmp)
51
52 static struct envnode *
53 createnode(const char *key, const char *value)
54 {
55         struct envnode *node;
56
57         node = malloc(sizeof(*node));
58         if (!node)
59                 err(1, NULL);
60         node->key = strdup(key);
61         node->value = strdup(value);
62         if (!node->key || !node->value)
63                 err(1, NULL);
64         return node;
65 }
66
67 static void
68 freenode(struct envnode *node)
69 {
70         free((char *)node->key);
71         free((char *)node->value);
72         free(node);
73 }
74
75 static void
76 addnode(struct env *env, const char *key, const char *value)
77 {
78         struct envnode *node;
79
80         node = createnode(key, value);
81         RB_INSERT(envtree, &env->root, node);
82         env->count++;
83 }
84
85 static struct env *
86 createenv(const struct rule *rule, const struct passwd *mypw,
87     const struct passwd *targpw)
88 {
89         struct env *env;
90         u_int i;
91
92         env = malloc(sizeof(*env));
93         if (!env)
94                 err(1, NULL);
95         RB_INIT(&env->root);
96         env->count = 0;
97
98         addnode(env, "DOAS_USER", mypw->pw_name);
99
100         if (rule->options & KEEPENV) {
101                 extern char **environ;
102
103                 for (i = 0; environ[i] != NULL; i++) {
104                         struct envnode *node;
105                         const char *e, *eq;
106                         size_t len;
107                         char keybuf[1024];
108
109                         e = environ[i];
110
111                         /* ignore invalid or overlong names */
112                         if ((eq = strchr(e, '=')) == NULL || eq == e)
113                                 continue;
114                         len = eq - e;
115                         if (len > sizeof(keybuf) - 1)
116                                 continue;
117                         memcpy(keybuf, e, len);
118                         keybuf[len] = '\0';
119
120                         node = createnode(keybuf, eq + 1);
121                         if (RB_INSERT(envtree, &env->root, node)) {
122                                 /* ignore any later duplicates */
123                                 freenode(node);
124                         } else {
125                                 env->count++;
126                         }
127                 }
128         } else {
129                 static const char *copyset[] = {
130                         "DISPLAY", "TERM",
131                         NULL
132                 };
133
134                 addnode(env, "HOME", targpw->pw_dir);
135                 addnode(env, "LOGNAME", targpw->pw_name);
136                 addnode(env, "PATH", getenv("PATH"));
137                 addnode(env, "SHELL", targpw->pw_shell);
138                 addnode(env, "USER", targpw->pw_name);
139
140                 fillenv(env, copyset);
141         }
142
143         return env;
144 }
145
146 static char **
147 flattenenv(struct env *env)
148 {
149         char **envp;
150         struct envnode *node;
151         u_int i;
152
153         envp = reallocarray(NULL, env->count + 1, sizeof(char *));
154         if (!envp)
155                 err(1, NULL);
156         i = 0;
157         RB_FOREACH(node, envtree, &env->root) {
158                 if (asprintf(&envp[i], "%s=%s", node->key, node->value) == -1)
159                         err(1, NULL);
160                 i++;
161         }
162         envp[i] = NULL;
163         return envp;
164 }
165
166 static void
167 fillenv(struct env *env, const char **envlist)
168 {
169         struct envnode *node, key;
170         const char *e, *eq;
171         const char *val;
172         char name[1024];
173         u_int i;
174         size_t len;
175
176         for (i = 0; envlist[i]; i++) {
177                 e = envlist[i];
178
179                 /* parse out env name */
180                 if ((eq = strchr(e, '=')) == NULL)
181                         len = strlen(e);
182                 else
183                         len = eq - e;
184                 if (len > sizeof(name) - 1)
185                         continue;
186                 memcpy(name, e, len);
187                 name[len] = '\0';
188
189                 /* delete previous copies */
190                 key.key = name;
191                 if (*name == '-')
192                         key.key = name + 1;
193                 if ((node = RB_FIND(envtree, &env->root, &key))) {
194                         RB_REMOVE(envtree, &env->root, node);
195                         freenode(node);
196                         env->count--;
197                 }
198                 if (*name == '-')
199                         continue;
200
201                 /* assign value or inherit from environ */
202                 if (eq) {
203                         val = eq + 1;
204                         if (*val == '$')
205                                 val = getenv(val + 1);
206                 } else {
207                         val = getenv(name);
208                 }
209                 /* at last, we have something to insert */
210                 if (val) {
211                         node = createnode(name, val);
212                         RB_INSERT(envtree, &env->root, node);
213                         env->count++;
214                 }
215         }
216 }
217
218 char **
219 prepenv(const struct rule *rule, const struct passwd *mypw,
220     const struct passwd *targpw)
221 {
222         struct env *env;
223
224         env = createenv(rule, mypw, targpw);
225         if (rule->envlist)
226                 fillenv(env, rule->envlist);
227
228         return flattenenv(env);
229 }