3 * Copyright (c) 2016 Ted Unangst <tedu@openbsd.org>
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.
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.
18 #include <sys/types.h>
33 RB_ENTRY(envnode) node;
39 RB_HEAD(envtree, envnode) root;
43 static void fillenv(struct env *env, const char **envlist);
46 envcmp(struct envnode *a, struct envnode *b)
48 return strcmp(a->key, b->key);
50 RB_GENERATE_STATIC(envtree, envnode, node, envcmp)
52 static struct envnode *
53 createnode(const char *key, const char *value)
57 node = malloc(sizeof(*node));
60 node->key = strdup(key);
61 node->value = strdup(value);
62 if (!node->key || !node->value)
68 freenode(struct envnode *node)
70 free((char *)node->key);
71 free((char *)node->value);
76 addnode(struct env *env, const char *key, const char *value)
80 node = createnode(key, value);
81 RB_INSERT(envtree, &env->root, node);
86 createenv(const struct rule *rule, const struct passwd *mypw,
87 const struct passwd *targpw)
92 env = malloc(sizeof(*env));
98 addnode(env, "DOAS_USER", mypw->pw_name);
100 if (rule->options & KEEPENV) {
101 extern char **environ;
103 for (i = 0; environ[i] != NULL; i++) {
104 struct envnode *node;
111 /* ignore invalid or overlong names */
112 if ((eq = strchr(e, '=')) == NULL || eq == e)
115 if (len > sizeof(keybuf) - 1)
117 memcpy(keybuf, e, len);
120 node = createnode(keybuf, eq + 1);
121 if (RB_INSERT(envtree, &env->root, node)) {
122 /* ignore any later duplicates */
129 static const char *copyset[] = {
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);
140 fillenv(env, copyset);
147 flattenenv(struct env *env)
150 struct envnode *node;
153 envp = reallocarray(NULL, env->count + 1, sizeof(char *));
157 RB_FOREACH(node, envtree, &env->root) {
158 if (asprintf(&envp[i], "%s=%s", node->key, node->value) == -1)
167 fillenv(struct env *env, const char **envlist)
169 struct envnode *node, key;
176 for (i = 0; envlist[i]; i++) {
179 /* parse out env name */
180 if ((eq = strchr(e, '=')) == NULL)
184 if (len > sizeof(name) - 1)
186 memcpy(name, e, len);
189 /* delete previous copies */
193 if ((node = RB_FIND(envtree, &env->root, &key))) {
194 RB_REMOVE(envtree, &env->root, node);
201 /* assign value or inherit from environ */
205 val = getenv(val + 1);
209 /* at last, we have something to insert */
211 node = createnode(name, val);
212 RB_INSERT(envtree, &env->root, node);
219 prepenv(const struct rule *rule, const struct passwd *mypw,
220 const struct passwd *targpw)
224 env = createenv(rule, mypw, targpw);
226 fillenv(env, rule->envlist);
228 return flattenenv(env);