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