1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
/*
* Copyright (c) 2004-2005, 2007-2010
* Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <config.h>
#include <sys/types.h>
#include <sys/param.h>
#include <stdio.h>
#ifdef STDC_HEADERS
# include <stdlib.h>
# include <stddef.h>
#else
# ifdef HAVE_STDLIB_H
# include <stdlib.h>
# endif
#endif /* STDC_HEADERS */
#ifdef HAVE_STRING_H
# include <string.h>
#endif /* HAVE_STRING_H */
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif /* HAVE_STRING_H */
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif /* HAVE_UNISTD_H */
#include "sudo.h"
#include "parse.h"
#include "redblack.h"
#include <gram.h>
/*
* Globals
*/
struct rbtree *aliases;
unsigned int alias_seqno;
/*
* Comparison function for the red-black tree.
* Aliases are sorted by name with the type used as a tie-breaker.
*/
int
alias_compare(v1, v2)
const void *v1, *v2;
{
const struct alias *a1 = (const struct alias *)v1;
const struct alias *a2 = (const struct alias *)v2;
int res;
if (v1 == NULL)
res = -1;
else if (v2 == NULL)
res = 1;
else if ((res = strcmp(a1->name, a2->name)) == 0)
res = a1->type - a2->type;
return(res);
}
/*
* Search the tree for an alias with the specified name and type.
* Returns a pointer to the alias structure or NULL if not found.
*/
struct alias *
alias_find(name, type)
char *name;
int type;
{
struct alias key;
struct rbnode *node;
struct alias *a = NULL;
key.name = name;
key.type = type;
if ((node = rbfind(aliases, &key)) != NULL) {
/*
* Compare the global sequence number with the one stored
* in the alias. If they match then we've seen this alias
* before and found a loop.
*/
a = node->data;
if (a->seqno == alias_seqno)
return(NULL);
a->seqno = alias_seqno;
}
return(a);
}
/*
* Add an alias to the aliases redblack tree.
* Returns NULL on success and an error string on failure.
*/
char *
alias_add(name, type, members)
char *name;
int type;
struct member *members;
{
static char errbuf[512];
struct alias *a;
a = emalloc(sizeof(*a));
a->name = name;
a->type = type;
a->seqno = 0;
list2tq(&a->members, members);
if (rbinsert(aliases, a)) {
snprintf(errbuf, sizeof(errbuf), "Alias `%s' already defined", name);
alias_free(a);
return(errbuf);
}
return(NULL);
}
/*
* Apply a function to each alias entry and pass in a cookie.
*/
void
alias_apply(func, cookie)
int (*func) __P((void *, void *));
void *cookie;
{
rbapply(aliases, func, cookie, inorder);
}
/*
* Returns TRUE if there are no aliases, else FALSE.
*/
int
no_aliases()
{
return(rbisempty(aliases));
}
/*
* Free memory used by an alias struct and its members.
*/
void
alias_free(v)
void *v;
{
struct alias *a = (struct alias *)v;
struct member *m;
struct sudo_command *c;
void *next;
efree(a->name);
for (m = a->members.first; m != NULL; m = next) {
next = m->next;
if (m->type == COMMAND) {
c = (struct sudo_command *) m->name;
efree(c->cmnd);
efree(c->args);
}
efree(m->name);
efree(m);
}
efree(a);
}
/*
* Find the named alias, remove it from the tree and return it.
*/
struct alias *
alias_remove(name, type)
char *name;
int type;
{
struct rbnode *node;
struct alias key, *a;
key.name = name;
key.type = type;
if ((node = rbfind(aliases, &key)) == NULL)
return(NULL);
a = rbdelete(aliases, node);
return(a);
}
void
init_aliases()
{
if (aliases != NULL)
rbdestroy(aliases, alias_free);
aliases = rbcreate(alias_compare);
}
|