274b446e1a19efe0d086042ff09ddc1fe32abc46
[debian/sudo] / plugins / sudoers / alias.c
1 /*
2  * Copyright (c) 2004-2005, 2007-2011
3  *      Todd C. Miller <Todd.Miller@courtesan.com>
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  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
17  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
18  */
19
20 #include <config.h>
21
22 #include <sys/types.h>
23 #include <sys/param.h>
24 #include <stdio.h>
25 #ifdef STDC_HEADERS
26 # include <stdlib.h>
27 # include <stddef.h>
28 #else
29 # ifdef HAVE_STDLIB_H
30 #  include <stdlib.h>
31 # endif
32 #endif /* STDC_HEADERS */
33 #ifdef HAVE_STRING_H
34 # include <string.h>
35 #endif /* HAVE_STRING_H */
36 #ifdef HAVE_STRINGS_H
37 # include <strings.h>
38 #endif /* HAVE_STRING_H */
39 #ifdef HAVE_UNISTD_H
40 # include <unistd.h>
41 #endif /* HAVE_UNISTD_H */
42
43 #include "sudoers.h"
44 #include "parse.h"
45 #include "redblack.h"
46 #include <gram.h>
47
48 /*
49  * Globals
50  */
51 struct rbtree *aliases;
52 unsigned int alias_seqno;
53
54 /*
55  * Comparison function for the red-black tree.
56  * Aliases are sorted by name with the type used as a tie-breaker.
57  */
58 int
59 alias_compare(const void *v1, const void *v2)
60 {
61     const struct alias *a1 = (const struct alias *)v1;
62     const struct alias *a2 = (const struct alias *)v2;
63     int res;
64
65     if (v1 == NULL)
66         res = -1;
67     else if (v2 == NULL)
68         res = 1;
69     else if ((res = strcmp(a1->name, a2->name)) == 0)
70         res = a1->type - a2->type;
71     return res;
72 }
73
74 /*
75  * Search the tree for an alias with the specified name and type.
76  * Returns a pointer to the alias structure or NULL if not found.
77  */
78 struct alias *
79 alias_find(char *name, int type)
80 {
81     struct alias key;
82     struct rbnode *node;
83     struct alias *a = NULL;
84
85     key.name = name;
86     key.type = type;
87     if ((node = rbfind(aliases, &key)) != NULL) {
88             /*
89              * Compare the global sequence number with the one stored
90              * in the alias.  If they match then we've seen this alias
91              * before and found a loop.
92              */
93             a = node->data;
94             if (a->seqno == alias_seqno)
95                 return NULL;
96             a->seqno = alias_seqno;
97     }
98     return a;
99 }
100
101 /*
102  * Add an alias to the aliases redblack tree.
103  * Returns NULL on success and an error string on failure.
104  */
105 char *
106 alias_add(char *name, int type, struct member *members)
107 {
108     static char errbuf[512];
109     struct alias *a;
110
111     a = emalloc(sizeof(*a));
112     a->name = name;
113     a->type = type;
114     a->seqno = 0;
115     list2tq(&a->members, members);
116     if (rbinsert(aliases, a)) {
117         snprintf(errbuf, sizeof(errbuf), "Alias `%s' already defined", name);
118         alias_free(a);
119         return errbuf;
120     }
121     return NULL;
122 }
123
124 /*
125  * Apply a function to each alias entry and pass in a cookie.
126  */
127 void
128 alias_apply(int (*func)(void *, void *), void *cookie)
129 {
130     rbapply(aliases, func, cookie, inorder);
131 }
132
133 /*
134  * Returns TRUE if there are no aliases, else FALSE.
135  */
136 int
137 no_aliases(void)
138 {
139     return rbisempty(aliases);
140 }
141
142 /*
143  * Free memory used by an alias struct and its members.
144  */
145 void
146 alias_free(void *v)
147 {
148     struct alias *a = (struct alias *)v;
149     struct member *m;
150     struct sudo_command *c;
151     void *next;
152
153     efree(a->name);
154     for (m = a->members.first; m != NULL; m = next) {
155         next = m->next;
156         if (m->type == COMMAND) {
157                 c = (struct sudo_command *) m->name;
158                 efree(c->cmnd);
159                 efree(c->args);
160         }
161         efree(m->name);
162         efree(m);
163     }
164     efree(a);
165 }
166
167 /*
168  * Find the named alias, remove it from the tree and return it.
169  */
170 struct alias *
171 alias_remove(char *name, int type)
172 {
173     struct rbnode *node;
174     struct alias key;
175
176     key.name = name;
177     key.type = type;
178     if ((node = rbfind(aliases, &key)) == NULL)
179         return NULL;
180     return rbdelete(aliases, node);
181 }
182
183 void
184 init_aliases(void)
185 {
186     if (aliases != NULL)
187         rbdestroy(aliases, alias_free);
188     aliases = rbcreate(alias_compare);
189 }