Annotation of embedaddon/sudo/plugins/sudoers/alias.c, revision 1.1.1.2

1.1       misho       1: /*
                      2:  * Copyright (c) 2004-2005, 2007-2011
                      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: #endif /* HAVE_STRING_H */
                     36: #ifdef HAVE_STRINGS_H
                     37: # include <strings.h>
                     38: #endif /* HAVE_STRING_H */
                     39: #ifdef HAVE_UNISTD_H
                     40: # include <unistd.h>
                     41: #endif /* HAVE_UNISTD_H */
                     42: #include <errno.h>
                     43: 
                     44: #include "sudoers.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:  */
                     59: int
                     60: alias_compare(const void *v1, const void *v2)
                     61: {
                     62:     const struct alias *a1 = (const struct alias *)v1;
                     63:     const struct alias *a2 = (const struct alias *)v2;
                     64:     int res;
1.1.1.2 ! misho      65:     debug_decl(alias_compare, SUDO_DEBUG_ALIAS)
1.1       misho      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;
1.1.1.2 ! misho      73:     debug_return_int(res);
1.1       misho      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 *
                     81: alias_find(char *name, int type)
                     82: {
                     83:     struct alias key;
                     84:     struct rbnode *node;
                     85:     struct alias *a = NULL;
1.1.1.2 ! misho      86:     debug_decl(alias_find, SUDO_DEBUG_ALIAS)
1.1       misho      87: 
                     88:     key.name = name;
                     89:     key.type = type;
                     90:     if ((node = rbfind(aliases, &key)) != NULL) {
                     91:        /*
                     92:         * Compare the global sequence number with the one stored
                     93:         * in the alias.  If they match then we've seen this alias
                     94:         * before and found a loop.
                     95:         */
                     96:        a = node->data;
                     97:        if (a->seqno == alias_seqno) {
                     98:            errno = ELOOP;
1.1.1.2 ! misho      99:            debug_return_ptr(NULL);
1.1       misho     100:        }
                    101:        a->seqno = alias_seqno;
                    102:     } else {
                    103:        errno = ENOENT;
                    104:     }
1.1.1.2 ! misho     105:     debug_return_ptr(a);
1.1       misho     106: }
                    107: 
                    108: /*
                    109:  * Add an alias to the aliases redblack tree.
                    110:  * Returns NULL on success and an error string on failure.
                    111:  */
                    112: char *
                    113: alias_add(char *name, int type, struct member *members)
                    114: {
                    115:     static char errbuf[512];
                    116:     struct alias *a;
1.1.1.2 ! misho     117:     debug_decl(alias_add, SUDO_DEBUG_ALIAS)
1.1       misho     118: 
1.1.1.2 ! misho     119:     a = ecalloc(1, sizeof(*a));
1.1       misho     120:     a->name = name;
                    121:     a->type = type;
1.1.1.2 ! misho     122:     /* a->seqno = 0; */
1.1       misho     123:     list2tq(&a->members, members);
                    124:     if (rbinsert(aliases, a)) {
                    125:        snprintf(errbuf, sizeof(errbuf), _("Alias `%s' already defined"), name);
                    126:        alias_free(a);
1.1.1.2 ! misho     127:        debug_return_str(errbuf);
1.1       misho     128:     }
1.1.1.2 ! misho     129:     debug_return_str(NULL);
1.1       misho     130: }
                    131: 
                    132: /*
                    133:  * Apply a function to each alias entry and pass in a cookie.
                    134:  */
                    135: void
                    136: alias_apply(int (*func)(void *, void *), void *cookie)
                    137: {
1.1.1.2 ! misho     138:     debug_decl(alias_apply, SUDO_DEBUG_ALIAS)
        !           139: 
1.1       misho     140:     rbapply(aliases, func, cookie, inorder);
1.1.1.2 ! misho     141: 
        !           142:     debug_return;
1.1       misho     143: }
                    144: 
                    145: /*
1.1.1.2 ! misho     146:  * Returns true if there are no aliases, else false.
1.1       misho     147:  */
1.1.1.2 ! misho     148: bool
1.1       misho     149: no_aliases(void)
                    150: {
1.1.1.2 ! misho     151:     debug_decl(no_aliases, SUDO_DEBUG_ALIAS)
        !           152:     debug_return_bool(rbisempty(aliases));
1.1       misho     153: }
                    154: 
                    155: /*
                    156:  * Free memory used by an alias struct and its members.
                    157:  */
                    158: void
                    159: alias_free(void *v)
                    160: {
                    161:     struct alias *a = (struct alias *)v;
                    162:     struct member *m;
                    163:     struct sudo_command *c;
                    164:     void *next;
1.1.1.2 ! misho     165:     debug_decl(alias_free, SUDO_DEBUG_ALIAS)
1.1       misho     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);
1.1.1.2 ! misho     179: 
        !           180:     debug_return;
1.1       misho     181: }
                    182: 
                    183: /*
                    184:  * Find the named alias, remove it from the tree and return it.
                    185:  */
                    186: struct alias *
                    187: alias_remove(char *name, int type)
                    188: {
                    189:     struct rbnode *node;
                    190:     struct alias key;
1.1.1.2 ! misho     191:     debug_decl(alias_remove, SUDO_DEBUG_ALIAS)
1.1       misho     192: 
                    193:     key.name = name;
                    194:     key.type = type;
                    195:     if ((node = rbfind(aliases, &key)) == NULL) {
                    196:        errno = ENOENT;
                    197:        return NULL;
                    198:     }
1.1.1.2 ! misho     199:     debug_return_ptr(rbdelete(aliases, node));
1.1       misho     200: }
                    201: 
                    202: void
                    203: init_aliases(void)
                    204: {
1.1.1.2 ! misho     205:     debug_decl(init_aliases, SUDO_DEBUG_ALIAS)
        !           206: 
1.1       misho     207:     if (aliases != NULL)
                    208:        rbdestroy(aliases, alias_free);
                    209:     aliases = rbcreate(alias_compare);
1.1.1.2 ! misho     210: 
        !           211:     debug_return;
1.1       misho     212: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>