Merge commit 'upstream/1.7.4p6'
[debian/sudo] / alias.c
1 /*
2  * Copyright (c) 2004-2005, 2007-2010
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 "sudo.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(v1, v2)
60     const void *v1, *v2;
61 {
62     const struct alias *a1 = (const struct alias *)v1;
63     const struct alias *a2 = (const struct alias *)v2;
64     int res;
65
66     if (v1 == NULL)
67         res = -1;
68     else if (v2 == NULL)
69         res = 1;
70     else if ((res = strcmp(a1->name, a2->name)) == 0)
71         res = a1->type - a2->type;
72     return(res);
73 }
74
75 /*
76  * Search the tree for an alias with the specified name and type.
77  * Returns a pointer to the alias structure or NULL if not found.
78  */
79 struct alias *
80 alias_find(name, type)
81     char *name;
82     int type;
83 {
84     struct alias key;
85     struct rbnode *node;
86     struct alias *a = NULL;
87
88     key.name = name;
89     key.type = type;
90     if ((node = rbfind(aliases, &key)) != NULL) {
91             /*
92              * Compare the global sequence number with the one stored
93              * in the alias.  If they match then we've seen this alias
94              * before and found a loop.
95              */
96             a = node->data;
97             if (a->seqno == alias_seqno)
98                 return(NULL);
99             a->seqno = alias_seqno;
100     }
101     return(a);
102 }
103
104 /*
105  * Add an alias to the aliases redblack tree.
106  * Returns NULL on success and an error string on failure.
107  */
108 char *
109 alias_add(name, type, members)
110     char *name;
111     int type;
112     struct member *members;
113 {
114     static char errbuf[512];
115     struct alias *a;
116
117     a = emalloc(sizeof(*a));
118     a->name = name;
119     a->type = type;
120     a->seqno = 0;
121     list2tq(&a->members, members);
122     if (rbinsert(aliases, a)) {
123         snprintf(errbuf, sizeof(errbuf), "Alias `%s' already defined", name);
124         alias_free(a);
125         return(errbuf);
126     }
127     return(NULL);
128 }
129
130 /*
131  * Apply a function to each alias entry and pass in a cookie.
132  */
133 void
134 alias_apply(func, cookie)
135     int (*func) __P((void *, void *));
136     void *cookie;
137 {
138     rbapply(aliases, func, cookie, inorder);
139 }
140
141 /*
142  * Returns TRUE if there are no aliases, else FALSE.
143  */
144 int
145 no_aliases()
146 {
147     return(rbisempty(aliases));
148 }
149
150 /*
151  * Free memory used by an alias struct and its members.
152  */
153 void
154 alias_free(v)
155     void *v;
156 {
157     struct alias *a = (struct alias *)v;
158     struct member *m;
159     struct sudo_command *c;
160     void *next;
161
162     efree(a->name);
163     for (m = a->members.first; m != NULL; m = next) {
164         next = m->next;
165         if (m->type == COMMAND) {
166                 c = (struct sudo_command *) m->name;
167                 efree(c->cmnd);
168                 efree(c->args);
169         }
170         efree(m->name);
171         efree(m);
172     }
173     efree(a);
174 }
175
176 /*
177  * Find the named alias, remove it from the tree and return it.
178  */
179 struct alias *
180 alias_remove(name, type)
181     char *name;
182     int type;
183 {
184     struct rbnode *node;
185     struct alias key, *a;
186
187     key.name = name;
188     key.type = type;
189     if ((node = rbfind(aliases, &key)) == NULL)
190         return(NULL);
191     a = rbdelete(aliases, node);
192     return(a);
193 }
194
195 void
196 init_aliases()
197 {
198     if (aliases != NULL)
199         rbdestroy(aliases, alias_free);
200     aliases = rbcreate(alias_compare);
201 }