2 * Copyright (c) 2004-2005m, 2007-2008
3 * Todd C. Miller <Todd.Miller@courtesan.com>
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.
16 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
17 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 #include <sys/types.h>
23 #include <sys/param.h>
32 #endif /* STDC_HEADERS */
36 # ifdef HAVE_STRINGS_H
39 #endif /* HAVE_STRING_H */
42 #endif /* HAVE_UNISTD_H */
50 __unused static const char rcsid[] = "$Sudo: alias.c,v 1.14 2008/11/18 13:29:58 millert Exp $";
56 struct rbtree *aliases;
57 unsigned int alias_seqno;
62 static int alias_compare __P((const void *, const void *));
63 static void alias_free __P((void *));
66 * Comparison function for the red-black tree.
67 * Aliases are sorted by name with the type used as a tie-breaker.
73 const struct alias *a1 = (const struct alias *)v1;
74 const struct alias *a2 = (const struct alias *)v2;
81 else if ((res = strcmp(a1->name, a2->name)) == 0)
82 res = a1->type - a2->type;
87 * Search the tree for an alias with the specified name and type.
88 * Returns a pointer to the alias structure or NULL if not found.
91 find_alias(name, type)
97 struct alias *a = NULL;
101 if ((node = rbfind(aliases, &key)) != NULL) {
103 * Compare the global sequence number with the one stored
104 * in the alias. If they match then we've seen this alias
105 * before and found a loop.
108 if (a->seqno == alias_seqno)
110 a->seqno = alias_seqno;
116 * Add an alias to the aliases redblack tree.
117 * Returns NULL on success and an error string on failure.
120 alias_add(name, type, members)
123 struct member *members;
125 static char errbuf[512];
128 a = emalloc(sizeof(*a));
132 list2tq(&a->members, members);
133 if (rbinsert(aliases, a)) {
135 snprintf(errbuf, sizeof(errbuf), "Alias `%s' already defined", name);
142 * Apply a function to each alias entry and pass in a cookie.
145 alias_apply(func, cookie)
146 int (*func) __P((void *, void *));
149 rbapply(aliases, func, cookie, inorder);
153 * Returns TRUE if there are no aliases, else FALSE.
158 return(rbisempty(aliases));
162 * Free memory used by an alias struct and its members.
168 struct alias *a = (struct alias *)v;
170 struct sudo_command *c;
174 for (m = a->members.first; m != NULL; m = next) {
176 if (m->type == COMMAND) {
177 c = (struct sudo_command *) m->name;
188 * Find the named alias, delete it from the tree and recover its resources.
191 alias_remove(name, type)
196 struct alias key, *a;
200 if ((node = rbfind(aliases, &key)) == NULL)
202 a = rbdelete(aliases, node);
211 rbdestroy(aliases, alias_free);
212 aliases = rbcreate(alias_compare);