2 * Copyright (c) 2004-2005, 2007-2011
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 */
35 #endif /* HAVE_STRING_H */
38 #endif /* HAVE_STRING_H */
41 #endif /* HAVE_UNISTD_H */
52 struct rbtree *aliases;
53 unsigned int alias_seqno;
56 * Comparison function for the red-black tree.
57 * Aliases are sorted by name with the type used as a tie-breaker.
60 alias_compare(const void *v1, const void *v2)
62 const struct alias *a1 = (const struct alias *)v1;
63 const struct alias *a2 = (const struct alias *)v2;
65 debug_decl(alias_compare, SUDO_DEBUG_ALIAS)
71 else if ((res = strcmp(a1->name, a2->name)) == 0)
72 res = a1->type - a2->type;
73 debug_return_int(res);
77 * Search the tree for an alias with the specified name and type.
78 * Returns a pointer to the alias structure or NULL if not found.
81 alias_find(char *name, int type)
85 struct alias *a = NULL;
86 debug_decl(alias_find, SUDO_DEBUG_ALIAS)
90 if ((node = rbfind(aliases, &key)) != NULL) {
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.
97 if (a->seqno == alias_seqno) {
99 debug_return_ptr(NULL);
101 a->seqno = alias_seqno;
109 * Add an alias to the aliases redblack tree.
110 * Returns NULL on success and an error string on failure.
113 alias_add(char *name, int type, struct member *members)
115 static char errbuf[512];
117 debug_decl(alias_add, SUDO_DEBUG_ALIAS)
119 a = ecalloc(1, sizeof(*a));
123 list2tq(&a->members, members);
124 if (rbinsert(aliases, a)) {
125 snprintf(errbuf, sizeof(errbuf), _("Alias `%s' already defined"), name);
127 debug_return_str(errbuf);
129 debug_return_str(NULL);
133 * Apply a function to each alias entry and pass in a cookie.
136 alias_apply(int (*func)(void *, void *), void *cookie)
138 debug_decl(alias_apply, SUDO_DEBUG_ALIAS)
140 rbapply(aliases, func, cookie, inorder);
146 * Returns true if there are no aliases, else false.
151 debug_decl(no_aliases, SUDO_DEBUG_ALIAS)
152 debug_return_bool(rbisempty(aliases));
156 * Free memory used by an alias struct and its members.
161 struct alias *a = (struct alias *)v;
163 struct sudo_command *c;
165 debug_decl(alias_free, SUDO_DEBUG_ALIAS)
168 for (m = a->members.first; m != NULL; m = next) {
170 if (m->type == COMMAND) {
171 c = (struct sudo_command *) m->name;
184 * Find the named alias, remove it from the tree and return it.
187 alias_remove(char *name, int type)
191 debug_decl(alias_remove, SUDO_DEBUG_ALIAS)
195 if ((node = rbfind(aliases, &key)) == NULL) {
199 debug_return_ptr(rbdelete(aliases, node));
205 debug_decl(init_aliases, SUDO_DEBUG_ALIAS)
208 rbdestroy(aliases, alias_free);
209 aliases = rbcreate(alias_compare);