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)
89 static const char *copyset[] = {
96 env = malloc(sizeof(*env));
102 addnode(env, "DOAS_USER", mypw->pw_name);
103 addnode(env, "HOME", targpw->pw_dir);
104 addnode(env, "LOGNAME", targpw->pw_name);
105 addnode(env, "PATH", getenv("PATH"));
106 addnode(env, "SHELL", targpw->pw_shell);
107 addnode(env, "USER", targpw->pw_name);
109 fillenv(env, copyset);
111 if (rule->options & KEEPENV) {
112 extern char **environ;
114 for (i = 0; environ[i] != NULL; i++) {
115 struct envnode *node;
122 /* ignore invalid or overlong names */
123 if ((eq = strchr(e, '=')) == NULL || eq == e)
126 if (len > sizeof(keybuf) - 1)
128 memcpy(keybuf, e, len);
131 node = createnode(keybuf, eq + 1);
132 if (RB_INSERT(envtree, &env->root, node)) {
133 /* ignore any later duplicates */
145 flattenenv(struct env *env)
148 struct envnode *node;
151 envp = reallocarray(NULL, env->count + 1, sizeof(char *));
155 RB_FOREACH(node, envtree, &env->root) {
156 if (asprintf(&envp[i], "%s=%s", node->key, node->value) == -1)
165 fillenv(struct env *env, const char **envlist)
167 struct envnode *node, key;
174 for (i = 0; envlist[i]; i++) {
177 /* parse out env name */
178 if ((eq = strchr(e, '=')) == NULL)
182 if (len > sizeof(name) - 1)
184 memcpy(name, e, len);
187 /* delete previous copies */
191 if ((node = RB_FIND(envtree, &env->root, &key))) {
192 RB_REMOVE(envtree, &env->root, node);
199 /* assign value or inherit from environ */
203 val = getenv(val + 1);
207 /* at last, we have something to insert */
209 node = createnode(name, val);
210 RB_INSERT(envtree, &env->root, node);
217 prepenv(const struct rule *rule, const struct passwd *mypw,
218 const struct passwd *targpw)
222 env = createenv(rule, mypw, targpw);
224 fillenv(env, rule->envlist);
226 return flattenenv(env);