2 * Copyright (c) 2004-2005, 2007-2013
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>
31 #endif /* STDC_HEADERS */
34 #endif /* HAVE_STRING_H */
37 #endif /* HAVE_STRING_H */
40 #endif /* HAVE_UNISTD_H */
51 struct rbtree *aliases;
54 * Comparison function for the red-black tree.
55 * Aliases are sorted by name with the type used as a tie-breaker.
58 alias_compare(const void *v1, const void *v2)
60 const struct alias *a1 = (const struct alias *)v1;
61 const struct alias *a2 = (const struct alias *)v2;
63 debug_decl(alias_compare, SUDO_DEBUG_ALIAS)
69 else if ((res = strcmp(a1->name, a2->name)) == 0)
70 res = a1->type - a2->type;
71 debug_return_int(res);
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 * Caller is responsible for calling alias_put() on the returned
78 * alias to mark it as unused.
81 alias_get(char *name, int type)
85 struct alias *a = NULL;
86 debug_decl(alias_get, SUDO_DEBUG_ALIAS)
90 if ((node = rbfind(aliases, &key)) != NULL) {
92 * Check whether this alias is already in use.
93 * If so, we've detected a loop. If not, set the flag,
94 * which the caller should clear with a call to alias_put().
99 debug_return_ptr(NULL);
109 * Clear the "used" flag in an alias once the caller is done with it.
112 alias_put(struct alias *a)
114 debug_decl(alias_put, SUDO_DEBUG_ALIAS)
120 * Add an alias to the aliases redblack tree.
121 * Returns NULL on success and an error string on failure.
124 alias_add(char *name, int type, struct member *members)
126 static char errbuf[512];
128 debug_decl(alias_add, SUDO_DEBUG_ALIAS)
130 a = ecalloc(1, sizeof(*a));
133 /* a->used = false; */
134 list2tq(&a->members, members);
135 if (rbinsert(aliases, a)) {
136 snprintf(errbuf, sizeof(errbuf), N_("Alias `%s' already defined"), name);
138 debug_return_str(errbuf);
140 debug_return_str(NULL);
144 * Apply a function to each alias entry and pass in a cookie.
147 alias_apply(int (*func)(void *, void *), void *cookie)
149 debug_decl(alias_apply, SUDO_DEBUG_ALIAS)
151 rbapply(aliases, func, cookie, inorder);
157 * Returns true if there are no aliases, else false.
162 debug_decl(no_aliases, SUDO_DEBUG_ALIAS)
163 debug_return_bool(rbisempty(aliases));
167 * Free memory used by an alias struct and its members.
172 struct alias *a = (struct alias *)v;
174 struct sudo_command *c;
176 debug_decl(alias_free, SUDO_DEBUG_ALIAS)
179 for (m = a->members.first; m != NULL; m = next) {
181 if (m->type == COMMAND) {
182 c = (struct sudo_command *) m->name;
195 * Find the named alias, remove it from the tree and return it.
198 alias_remove(char *name, int type)
202 debug_decl(alias_remove, SUDO_DEBUG_ALIAS)
206 if ((node = rbfind(aliases, &key)) == NULL) {
210 debug_return_ptr(rbdelete(aliases, node));
216 debug_decl(init_aliases, SUDO_DEBUG_ALIAS)
219 rbdestroy(aliases, alias_free);
220 aliases = rbcreate(alias_compare);