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>
32 const char *formerpath;
35 RB_ENTRY(envnode) node;
41 RB_HEAD(envtree, envnode) root;
45 static void fillenv(struct env *env, const char **envlist);
48 envcmp(struct envnode *a, struct envnode *b)
50 return strcmp(a->key, b->key);
52 RB_GENERATE_STATIC(envtree, envnode, node, envcmp)
54 static struct envnode *
55 createnode(const char *key, const char *value)
59 node = malloc(sizeof(*node));
62 node->key = strdup(key);
63 node->value = strdup(value);
64 if (!node->key || !node->value)
70 freenode(struct envnode *node)
72 free((char *)node->key);
73 free((char *)node->value);
78 addnode(struct env *env, const char *key, const char *value)
82 node = createnode(key, value);
83 RB_INSERT(envtree, &env->root, node);
88 createenv(const struct rule *rule, const struct passwd *mypw,
89 const struct passwd *targpw)
91 static const char *copyset[] = {
98 env = malloc(sizeof(*env));
104 addnode(env, "DOAS_USER", mypw->pw_name);
105 addnode(env, "HOME", targpw->pw_dir);
106 addnode(env, "LOGNAME", targpw->pw_name);
107 addnode(env, "PATH", getenv("PATH"));
108 addnode(env, "SHELL", targpw->pw_shell);
109 addnode(env, "USER", targpw->pw_name);
111 fillenv(env, copyset);
113 if (rule->options & KEEPENV) {
114 extern char **environ;
116 for (i = 0; environ[i] != NULL; i++) {
117 struct envnode *node;
124 /* ignore invalid or overlong names */
125 if ((eq = strchr(e, '=')) == NULL || eq == e)
128 if (len > sizeof(keybuf) - 1)
130 memcpy(keybuf, e, len);
133 node = createnode(keybuf, eq + 1);
134 if (RB_INSERT(envtree, &env->root, node)) {
135 /* ignore any later duplicates */
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 if (strcmp(val + 1, "PATH") == 0)
208 val = getenv(val + 1);
213 /* at last, we have something to insert */
215 node = createnode(name, val);
216 RB_INSERT(envtree, &env->root, node);
223 prepenv(const struct rule *rule, const struct passwd *mypw,
224 const struct passwd *targpw)
228 env = createenv(rule, mypw, targpw);
230 fillenv(env, rule->envlist);
232 return flattenenv(env);