Annotation of src/usr.bin/sudo/alias.c, Revision 1.4
1.1 millert 1: /*
1.4 ! millert 2: * Copyright (c) 2004-2005m, 2007-2009
1.1 millert 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: #else
36: # ifdef HAVE_STRINGS_H
37: # include <strings.h>
38: # endif
39: #endif /* HAVE_STRING_H */
40: #ifdef HAVE_UNISTD_H
41: # include <unistd.h>
42: #endif /* HAVE_UNISTD_H */
43:
44: #include "sudo.h"
45: #include "parse.h"
46: #include "redblack.h"
47: #include <gram.h>
48:
49: #ifndef lint
1.4 ! millert 50: __unused static const char rcsid[] = "$Sudo: alias.c,v 1.18 2009/05/25 12:02:41 millert Exp $";
1.1 millert 51: #endif /* lint */
52:
53: /*
54: * Globals
55: */
56: struct rbtree *aliases;
57: unsigned int alias_seqno;
58:
59: /*
60: * Comparison function for the red-black tree.
61: * Aliases are sorted by name with the type used as a tie-breaker.
62: */
1.3 millert 63: int
1.1 millert 64: alias_compare(v1, v2)
65: const void *v1, *v2;
66: {
67: const struct alias *a1 = (const struct alias *)v1;
68: const struct alias *a2 = (const struct alias *)v2;
69: int res;
70:
71: if (v1 == NULL)
72: res = -1;
73: else if (v2 == NULL)
74: res = 1;
75: else if ((res = strcmp(a1->name, a2->name)) == 0)
76: res = a1->type - a2->type;
77: return(res);
78: }
79:
80: /*
81: * Search the tree for an alias with the specified name and type.
82: * Returns a pointer to the alias structure or NULL if not found.
83: */
84: struct alias *
1.3 millert 85: alias_find(name, type)
1.1 millert 86: char *name;
87: int type;
88: {
89: struct alias key;
90: struct rbnode *node;
91: struct alias *a = NULL;
92:
93: key.name = name;
94: key.type = type;
95: if ((node = rbfind(aliases, &key)) != NULL) {
96: /*
97: * Compare the global sequence number with the one stored
98: * in the alias. If they match then we've seen this alias
99: * before and found a loop.
100: */
101: a = node->data;
102: if (a->seqno == alias_seqno)
103: return(NULL);
104: a->seqno = alias_seqno;
105: }
106: return(a);
107: }
108:
109: /*
110: * Add an alias to the aliases redblack tree.
111: * Returns NULL on success and an error string on failure.
112: */
113: char *
114: alias_add(name, type, members)
115: char *name;
116: int type;
117: struct member *members;
118: {
119: static char errbuf[512];
120: struct alias *a;
121:
122: a = emalloc(sizeof(*a));
123: a->name = name;
124: a->type = type;
125: a->seqno = 0;
126: list2tq(&a->members, members);
127: if (rbinsert(aliases, a)) {
128: alias_free(a);
129: snprintf(errbuf, sizeof(errbuf), "Alias `%s' already defined", name);
130: return(errbuf);
131: }
132: return(NULL);
133: }
134:
135: /*
136: * Apply a function to each alias entry and pass in a cookie.
137: */
138: void
139: alias_apply(func, cookie)
140: int (*func) __P((void *, void *));
141: void *cookie;
142: {
143: rbapply(aliases, func, cookie, inorder);
144: }
145:
146: /*
147: * Returns TRUE if there are no aliases, else FALSE.
148: */
149: int
150: no_aliases()
151: {
152: return(rbisempty(aliases));
153: }
154:
155: /*
156: * Free memory used by an alias struct and its members.
157: */
1.3 millert 158: void
1.1 millert 159: alias_free(v)
160: void *v;
161: {
162: struct alias *a = (struct alias *)v;
163: struct member *m;
164: struct sudo_command *c;
165: void *next;
166:
167: efree(a->name);
168: for (m = a->members.first; m != NULL; m = next) {
169: next = m->next;
170: if (m->type == COMMAND) {
171: c = (struct sudo_command *) m->name;
172: efree(c->cmnd);
173: efree(c->args);
174: }
175: efree(m->name);
176: efree(m);
177: }
178: efree(a);
179: }
180:
181: /*
1.3 millert 182: * Find the named alias, remove it from the tree and return it.
1.1 millert 183: */
1.3 millert 184: struct alias *
1.1 millert 185: alias_remove(name, type)
186: char *name;
187: int type;
188: {
189: struct rbnode *node;
1.2 millert 190: struct alias key, *a;
1.1 millert 191:
192: key.name = name;
193: key.type = type;
194: if ((node = rbfind(aliases, &key)) == NULL)
1.3 millert 195: return(NULL);
1.2 millert 196: a = rbdelete(aliases, node);
1.3 millert 197: return(a);
1.1 millert 198: }
199:
200: void
201: init_aliases()
202: {
203: if (aliases != NULL)
204: rbdestroy(aliases, alias_free);
205: aliases = rbcreate(alias_compare);
206: }