man pages get built in the build- directories
[debian/sudo] / alias.c
1 /*
2  * Copyright (c) 2004-2005m, 2007-2008
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
50 __unused static const char rcsid[] = "$Sudo: alias.c,v 1.14 2008/11/18 13:29:58 millert Exp $";
51 #endif /* lint */
52
53 /*
54  * Globals
55  */
56 struct rbtree *aliases;
57 unsigned int alias_seqno;
58
59 /*
60  * Local protoypes
61  */
62 static int   alias_compare      __P((const void *, const void *));
63 static void  alias_free         __P((void *));
64
65 /*
66  * Comparison function for the red-black tree.
67  * Aliases are sorted by name with the type used as a tie-breaker.
68  */
69 static int
70 alias_compare(v1, v2)
71     const void *v1, *v2;
72 {
73     const struct alias *a1 = (const struct alias *)v1;
74     const struct alias *a2 = (const struct alias *)v2;
75     int res;
76
77     if (v1 == NULL)
78         res = -1;
79     else if (v2 == NULL)
80         res = 1;
81     else if ((res = strcmp(a1->name, a2->name)) == 0)
82         res = a1->type - a2->type;
83     return(res);
84 }
85
86 /*
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.
89  */
90 struct alias *
91 find_alias(name, type)
92     char *name;
93     int type;
94 {
95     struct alias key;
96     struct rbnode *node;
97     struct alias *a = NULL;
98
99     key.name = name;
100     key.type = type;
101     if ((node = rbfind(aliases, &key)) != NULL) {
102             /*
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.
106              */
107             a = node->data;
108             if (a->seqno == alias_seqno)
109                 return(NULL);
110             a->seqno = alias_seqno;
111     }
112     return(a);
113 }
114
115 /*
116  * Add an alias to the aliases redblack tree.
117  * Returns NULL on success and an error string on failure.
118  */
119 char *
120 alias_add(name, type, members)
121     char *name;
122     int type;
123     struct member *members;
124 {
125     static char errbuf[512];
126     struct alias *a;
127
128     a = emalloc(sizeof(*a));
129     a->name = name;
130     a->type = type;
131     a->seqno = 0;
132     list2tq(&a->members, members);
133     if (rbinsert(aliases, a)) {
134         alias_free(a);
135         snprintf(errbuf, sizeof(errbuf), "Alias `%s' already defined", name);
136         return(errbuf);
137     }
138     return(NULL);
139 }
140
141 /*
142  * Apply a function to each alias entry and pass in a cookie.
143  */
144 void
145 alias_apply(func, cookie)
146     int (*func) __P((void *, void *));
147     void *cookie;
148 {
149     rbapply(aliases, func, cookie, inorder);
150 }
151
152 /*
153  * Returns TRUE if there are no aliases, else FALSE.
154  */
155 int
156 no_aliases()
157 {
158     return(rbisempty(aliases));
159 }
160
161 /*
162  * Free memory used by an alias struct and its members.
163  */
164 static void
165 alias_free(v)
166     void *v;
167 {
168     struct alias *a = (struct alias *)v;
169     struct member *m;
170     struct sudo_command *c;
171     void *next;
172
173     efree(a->name);
174     for (m = a->members.first; m != NULL; m = next) {
175         next = m->next;
176         if (m->type == COMMAND) {
177                 c = (struct sudo_command *) m->name;
178                 efree(c->cmnd);
179                 efree(c->args);
180         }
181         efree(m->name);
182         efree(m);
183     }
184     efree(a);
185 }
186
187 /*
188  * Find the named alias, delete it from the tree and recover its resources.
189  */
190 int
191 alias_remove(name, type)
192     char *name;
193     int type;
194 {
195     struct rbnode *node;
196     struct alias key, *a;
197
198     key.name = name;
199     key.type = type;
200     if ((node = rbfind(aliases, &key)) == NULL)
201         return(FALSE);
202     a = rbdelete(aliases, node);
203     alias_free(a);
204     return(TRUE);
205 }
206
207 void
208 init_aliases()
209 {
210     if (aliases != NULL)
211         rbdestroy(aliases, alias_free);
212     aliases = rbcreate(alias_compare);
213 }