[BACK]Return to testsudoers.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / sudo

Annotation of src/usr.bin/sudo/testsudoers.c, Revision 1.18

1.1       millert     1: /*
1.18    ! millert     2:  * Copyright (c) 1996, 1998-2005, 2007-2009
1.16      millert     3:  *     Todd C. Miller <Todd.Miller@courtesan.com>
1.1       millert     4:  *
1.11      millert     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.
1.1       millert     8:  *
1.11      millert     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.
1.1       millert    16:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
                     17:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.10      millert    18:  *
                     19:  * Sponsored in part by the Defense Advanced Research Projects
                     20:  * Agency (DARPA) and Air Force Research Laboratory, Air Force
                     21:  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
1.1       millert    22:  */
                     23:
1.9       millert    24: #define _SUDO_MAIN
                     25:
1.12      millert    26: #include <config.h>
1.1       millert    27:
1.5       millert    28: #include <sys/param.h>
                     29: #include <sys/types.h>
                     30: #include <sys/stat.h>
                     31: #include <sys/socket.h>
1.1       millert    32: #include <stdio.h>
                     33: #ifdef STDC_HEADERS
                     34: # include <stdlib.h>
1.5       millert    35: # include <stddef.h>
                     36: #else
                     37: # ifdef HAVE_STDLIB_H
                     38: #  include <stdlib.h>
                     39: # endif
1.1       millert    40: #endif /* STDC_HEADERS */
1.5       millert    41: #ifdef HAVE_STRING_H
                     42: # include <string.h>
                     43: #else
                     44: # ifdef HAVE_STRINGS_H
                     45: #  include <strings.h>
                     46: # endif
                     47: #endif /* HAVE_STRING_H */
1.1       millert    48: #ifdef HAVE_UNISTD_H
                     49: # include <unistd.h>
                     50: #endif /* HAVE_UNISTD_H */
1.2       millert    51: #ifdef HAVE_FNMATCH
1.1       millert    52: # include <fnmatch.h>
1.9       millert    53: #endif /* HAVE_FNMATCH */
1.1       millert    54: #ifdef HAVE_NETGROUP_H
                     55: # include <netgroup.h>
                     56: #endif /* HAVE_NETGROUP_H */
                     57: #include <ctype.h>
                     58: #include <pwd.h>
                     59: #include <grp.h>
                     60: #include <netinet/in.h>
                     61: #include <arpa/inet.h>
                     62: #include <netdb.h>
                     63:
                     64: #include "sudo.h"
1.16      millert    65: #include "interfaces.h"
1.1       millert    66: #include "parse.h"
1.16      millert    67: #include <gram.h>
1.1       millert    68:
                     69: #ifndef HAVE_FNMATCH
                     70: # include "emul/fnmatch.h"
                     71: #endif /* HAVE_FNMATCH */
                     72:
                     73: #ifndef lint
1.18    ! millert    74: __unused static const char rcsid[] = "$Sudo: testsudoers.c,v 1.131 2009/05/25 12:02:42 millert Exp $";
1.1       millert    75: #endif /* lint */
                     76:
1.5       millert    77:
                     78: /*
1.1       millert    79:  * Globals
                     80:  */
1.9       millert    81: int  Argc, NewArgc;
1.1       millert    82: char **Argv, **NewArgv;
                     83: int num_interfaces;
                     84: struct interface *interfaces;
                     85: struct sudo_user sudo_user;
1.16      millert    86: struct passwd *list_pw;
                     87: extern int parse_error;
                     88:
                     89: /* passwd/group redirection for pwutil.c */
                     90: void (*my_setgrent) __P((void)) = setgrent;
                     91: void (*my_endgrent) __P((void)) = endgrent;
                     92: struct group *(*my_getgrnam) __P((const char *)) = getgrnam;
                     93: struct group *(*my_getgrgid) __P((gid_t)) = getgrgid;
                     94: void (*my_setpwent) __P((void)) = setpwent;
                     95: void (*my_endpwent) __P((void)) = endpwent;
                     96: struct passwd *(*my_getpwnam) __P((const char *)) = getpwnam;
                     97: struct passwd *(*my_getpwuid) __P((uid_t)) = getpwuid;
                     98:
                     99: /* For getopt(3) */
                    100: extern char *optarg;
                    101: extern int optind;
                    102:
1.17      millert   103: #if defined(SUDO_DEVEL) && defined(__OpenBSD__)
                    104: extern char *malloc_options;
                    105: #endif
                    106: #ifdef YYDEBUG
                    107: extern int yydebug;
                    108: #endif
                    109:
1.16      millert   110: int  print_alias __P((void *, void *));
                    111: void dump_sudoers __P((void));
                    112: void print_defaults __P((void));
                    113: void print_privilege __P((struct privilege *));
                    114: void print_userspecs __P((void));
                    115: void usage __P((void)) __attribute__((__noreturn__));
                    116: void set_runasgr __P((char *));
                    117: void set_runaspw __P((char *));
                    118:
                    119: extern void ts_setgrfile __P((const char *));
                    120: extern void ts_setgrent __P((void));
                    121: extern void ts_endgrent __P((void));
                    122: extern struct group *ts_getgrent __P((void));
                    123: extern struct group *ts_getgrnam __P((const char *));
                    124: extern struct group *ts_getgrgid __P((gid_t));
                    125: extern void ts_setpwfile __P((const char *));
                    126: extern void ts_setpwent __P((void));
                    127: extern void ts_endpwent __P((void));
                    128: extern struct passwd *ts_getpwent __P((void));
                    129: extern struct passwd *ts_getpwnam __P((const char *));
                    130: extern struct passwd *ts_getpwuid __P((uid_t));
1.1       millert   131:
                    132: int
1.16      millert   133: main(argc, argv)
                    134:     int argc;
                    135:     char **argv;
1.1       millert   136: {
1.16      millert   137:     struct cmndspec *cs;
                    138:     struct privilege *priv;
                    139:     struct userspec *us;
                    140:     char *p, *grfile, *pwfile, *runas_group, *runas_user;
                    141:     char hbuf[MAXHOSTNAMELEN + 1];
                    142:     int ch, dflag, rval, matched;
1.17      millert   143:
                    144: #if defined(SUDO_DEVEL) && defined(__OpenBSD__)
                    145:     malloc_options = "AFGJPR";
                    146: #endif
                    147: #ifdef YYDEBUG
1.16      millert   148:     yydebug = 1;
                    149: #endif
1.11      millert   150:
1.16      millert   151:     Argv = argv;
                    152:     Argc = argc;
                    153:
                    154:     dflag = 0;
                    155:     grfile = pwfile = runas_group = runas_user = NULL;
                    156:     while ((ch = getopt(argc, argv, "dg:G:h:p:u:")) != -1) {
                    157:        switch (ch) {
                    158:            case 'd':
                    159:                dflag = 1;
                    160:                break;
                    161:            case 'h':
                    162:                user_host = optarg;
                    163:                break;
                    164:            case 'G':
                    165:                grfile = optarg;
                    166:                break;
                    167:            case 'g':
                    168:                runas_group = optarg;
                    169:                break;
                    170:            case 'p':
                    171:                pwfile = optarg;
                    172:                break;
                    173:            case 'u':
                    174:                runas_user = optarg;
                    175:                break;
                    176:            default:
                    177:                usage();
                    178:                break;
                    179:        }
                    180:     }
                    181:     argc -= optind;
                    182:     argv += optind;
                    183:     NewArgc = argc;
                    184:     NewArgv = argv;
                    185:
                    186:     /* Set group/passwd file and init the cache. */
                    187:     if (grfile) {
                    188:        my_setgrent = ts_setgrent;
                    189:        my_endgrent = ts_endgrent;
                    190:        my_getgrnam = ts_getgrnam;
                    191:        my_getgrgid = ts_getgrgid;
                    192:        ts_setgrfile(grfile);
                    193:     }
                    194:     if (pwfile) {
                    195:        my_setpwent = ts_setpwent;
                    196:        my_endpwent = ts_endpwent;
                    197:        my_getpwnam = ts_getpwnam;
                    198:        my_getpwuid = ts_getpwuid;
                    199:        ts_setpwfile(pwfile);
                    200:     }
                    201:     sudo_setpwent();
                    202:     sudo_setgrent();
                    203:
                    204:     if (argc < 2) {
                    205:        if (!dflag)
                    206:            usage();
                    207:        if ((sudo_user.pw = sudo_getpwnam("nobody")) == NULL)
                    208:             errorx(1, "no passwd entry for nobody!");
                    209:        user_cmnd = user_base = "true";
                    210:     } else {
                    211:        if ((sudo_user.pw = sudo_getpwnam(*argv)) == NULL)
                    212:             errorx(1, "no passwd entry for %s!", *argv);
                    213:        user_cmnd = *++argv;
                    214:        if ((p = strrchr(user_cmnd, '/')) != NULL)
                    215:            user_base = p + 1;
                    216:        else
                    217:            user_base = user_cmnd;
                    218:        NewArgc -= 2;
1.1       millert   219:     }
                    220:
1.16      millert   221:     if (user_host == NULL) {
                    222:        if (gethostname(hbuf, sizeof(hbuf)) != 0)
                    223:            error(1, "gethostname");
                    224:        hbuf[sizeof(hbuf) - 1] = '\0';
                    225:        user_host = hbuf;
                    226:     }
                    227:     if ((p = strchr(user_host, '.'))) {
                    228:        *p = '\0';
                    229:        user_shost = estrdup(user_host);
                    230:        *p = '.';
1.1       millert   231:     } else {
1.16      millert   232:        user_shost = user_host;
1.1       millert   233:     }
                    234:
1.16      millert   235:     /* Fill in user_args from NewArgv. */
                    236:     if (NewArgc > 1) {
                    237:        char *to, **from;
                    238:        size_t size, n;
1.13      millert   239:
1.16      millert   240:        for (size = 0, from = NewArgv + 1; *from; from++)
                    241:            size += strlen(*from) + 1;
1.13      millert   242:
1.16      millert   243:        user_args = (char *) emalloc(size);
                    244:        for (to = user_args, from = NewArgv + 1; *from; from++) {
                    245:            n = strlcpy(to, *from, size - (to - user_args));
                    246:            if (n >= size - (to - user_args))
                    247:                    errorx(1, "internal error, init_vars() overflow");
                    248:            to += n;
                    249:            *to++ = ' ';
1.13      millert   250:        }
1.16      millert   251:        *--to = '\0';
1.13      millert   252:     }
                    253:
1.16      millert   254:     /* Initialize default values. */
                    255:     init_defaults();
                    256:
                    257:     /* Load ip addr/mask for each interface. */
                    258:     load_interfaces();
1.13      millert   259:
1.16      millert   260:     /* Allocate space for data structures in the parser. */
                    261:     init_parser("sudoers", 0);
1.13      millert   262:
1.16      millert   263:     if (yyparse() != 0 || parse_error)
                    264:        (void) fputs("Does not parse", stdout);
1.13      millert   265:     else
1.16      millert   266:        (void) fputs("Parses OK", stdout);
                    267:
                    268:     if (!update_defaults(SETDEF_ALL))
                    269:        (void) fputs(" (problem with defaults entries)", stdout);
                    270:     puts(".");
                    271:
                    272:     /*
                    273:      * Set runas passwd/group entries based on command line or sudoers.
                    274:      * Note that if runas_group was specified without runas_user we
                    275:      * defer setting runas_pw so the match routines know to ignore it.
                    276:      */
                    277:     if (runas_group != NULL) {
                    278:         set_runasgr(runas_group);
                    279:         if (runas_user != NULL)
                    280:             set_runaspw(runas_user);
                    281:     } else
                    282:         set_runaspw(runas_user ? runas_user : def_runas_default);
1.1       millert   283:
1.16      millert   284:     if (dflag) {
                    285:        (void) putchar('\n');
                    286:        dump_sudoers();
                    287:        if (argc < 2)
                    288:            exit(0);
1.13      millert   289:     }
1.1       millert   290:
1.16      millert   291:     /* This loop must match the one in sudoers_lookup() */
                    292:     printf("\nEntries for user %s:\n", user_name);
                    293:     matched = UNSPEC;
                    294:     tq_foreach_rev(&userspecs, us) {
                    295:        if (userlist_matches(sudo_user.pw, &us->users) != ALLOW)
1.13      millert   296:            continue;
1.16      millert   297:        tq_foreach_rev(&us->privileges, priv) {
                    298:            putchar('\n');
                    299:            print_privilege(priv); /* XXX */
                    300:            putchar('\n');
                    301:            if (hostlist_matches(&priv->hostlist) == ALLOW) {
                    302:                puts("\thost  matched");
                    303:                tq_foreach_rev(&priv->cmndlist, cs) {
                    304:                    if (runaslist_matches(&cs->runasuserlist,
                    305:                        &cs->runasgrouplist) == ALLOW) {
                    306:                        puts("\trunas matched");
                    307:                        rval = cmnd_matches(cs->cmnd);
                    308:                        if (rval != UNSPEC)
                    309:                            matched = rval;
                    310:                        printf("\tcmnd  %s\n", rval == ALLOW ? "allowed" :
                    311:                            rval == DENY ? "denied" : "unmatched");
                    312:                    }
1.13      millert   313:                }
1.16      millert   314:            } else
                    315:                puts("\thost  unmatched");
1.13      millert   316:        }
1.1       millert   317:     }
1.16      millert   318:     printf("\nCommand %s\n", matched == ALLOW ? "allowed" :
                    319:        matched == DENY ? "denied" : "unmatched");
1.1       millert   320:
1.16      millert   321:     exit(0);
1.13      millert   322: }
                    323:
1.16      millert   324: void
                    325: set_runaspw(user)
                    326:     char *user;
1.13      millert   327: {
1.16      millert   328:     if (*user == '#') {
                    329:        if ((runas_pw = sudo_getpwuid(atoi(user + 1))) == NULL)
                    330:            runas_pw = sudo_fakepwnam(user, runas_gr ? runas_gr->gr_gid : 0);
1.4       millert   331:     } else {
1.16      millert   332:        if ((runas_pw = sudo_getpwnam(user)) == NULL)
                    333:            errorx(1, "unknown user: %s", user);
1.4       millert   334:     }
1.1       millert   335: }
                    336:
1.16      millert   337: void
                    338: set_runasgr(group)
                    339:     char *group;
1.11      millert   340: {
1.16      millert   341:     if (*group == '#') {
                    342:        if ((runas_gr = sudo_getgrgid(atoi(group + 1))) == NULL)
                    343:            runas_gr = sudo_fakegrnam(group);
                    344:     } else {
                    345:        if ((runas_gr = sudo_getgrnam(group)) == NULL)
                    346:            errorx(1, "unknown group: %s", group);
1.11      millert   347:     }
                    348: }
                    349:
1.16      millert   350: void
                    351: sudo_setspent()
1.1       millert   352: {
1.16      millert   353:     return;
1.1       millert   354: }
                    355:
1.16      millert   356: void
                    357: sudo_endspent()
1.1       millert   358: {
1.16      millert   359:     return;
1.1       millert   360: }
                    361:
1.16      millert   362: char *
                    363: sudo_getepw(pw)
                    364:     const struct passwd *pw;
1.2       millert   365: {
1.16      millert   366:     return (pw->pw_passwd);
1.2       millert   367: }
                    368:
                    369: void
                    370: set_fqdn()
1.5       millert   371: {
                    372:     return;
                    373: }
                    374:
1.16      millert   375: FILE *
1.18    ! millert   376: open_sudoers(path, isdir, keepopen)
1.16      millert   377:     const char *path;
1.18    ! millert   378:     int isdir;
1.16      millert   379:     int *keepopen;
1.11      millert   380: {
1.16      millert   381:     return(fopen(path, "r"));
1.11      millert   382: }
                    383:
1.5       millert   384: void
                    385: init_envtables()
1.1       millert   386: {
                    387:     return;
                    388: }
                    389:
1.18    ! millert   390: int
1.16      millert   391: set_perms(perm)
                    392:     int perm;
1.1       millert   393: {
1.18    ! millert   394:     return(1);
1.16      millert   395: }
1.1       millert   396:
1.16      millert   397: void
                    398: cleanup(gotsignal)
                    399:     int gotsignal;
                    400: {
                    401:     if (!gotsignal) {
                    402:        sudo_endpwent();
                    403:        sudo_endgrent();
                    404:     }
                    405: }
1.1       millert   406:
1.16      millert   407: void
                    408: print_member(m)
                    409:     struct member *m;
                    410: {
                    411:     struct sudo_command *c;
1.1       millert   412:
1.16      millert   413:     if (m->negated)
                    414:        putchar('!');
                    415:     if (m->name == NULL)
                    416:        fputs("ALL", stdout);
                    417:     else if (m->type != COMMAND)
                    418:        fputs(m->name, stdout);
                    419:     else {
                    420:        c = (struct sudo_command *) m->name;
                    421:        printf("%s%s%s", c->cmnd, c->args ? " " : "",
                    422:            c->args ? c->args : "");
1.1       millert   423:     }
1.16      millert   424: }
1.1       millert   425:
1.16      millert   426: void
                    427: print_defaults()
                    428: {
                    429:     struct defaults *d;
                    430:     struct member *m;
1.1       millert   431:
1.16      millert   432:     tq_foreach_fwd(&defaults, d) {
                    433:        (void) fputs("Defaults", stdout);
                    434:        switch (d->type) {
                    435:            case DEFAULTS_HOST:
                    436:                putchar('@');
                    437:                break;
                    438:            case DEFAULTS_USER:
                    439:                putchar(':');
                    440:                break;
                    441:            case DEFAULTS_RUNAS:
                    442:                putchar('>');
                    443:                break;
                    444:            case DEFAULTS_CMND:
                    445:                putchar('!');
                    446:                break;
                    447:        }
                    448:        tq_foreach_fwd(&d->binding, m) {
                    449:            if (m != tq_first(&d->binding))
                    450:                putchar(',');
                    451:            print_member(m);
                    452:        }
                    453:        printf("\t%s%s", d->op == FALSE ? "!" : "", d->var);
                    454:        if (d->val != NULL) {
                    455:            printf("%c%s", d->op == TRUE ? '=' : d->op, d->val);
                    456:        }
                    457:        putchar('\n');
1.1       millert   458:     }
1.16      millert   459: }
1.1       millert   460:
1.16      millert   461: int
                    462: print_alias(v1, v2)
                    463:     void *v1, *v2;
                    464: {
                    465:     struct alias *a = (struct alias *)v1;
                    466:     struct member *m;
                    467:     struct sudo_command *c;
                    468:
                    469:     switch (a->type) {
                    470:        case HOSTALIAS:
                    471:            (void) printf("Host_Alias\t%s = ", a->name);
                    472:            break;
                    473:        case CMNDALIAS:
                    474:            (void) printf("Cmnd_Alias\t%s = ", a->name);
                    475:            break;
                    476:        case USERALIAS:
                    477:            (void) printf("User_Alias\t%s = ", a->name);
                    478:            break;
                    479:        case RUNASALIAS:
                    480:            (void) printf("Runas_Alias\t%s = ", a->name);
                    481:            break;
                    482:     }
                    483:     tq_foreach_fwd(&a->members, m) {
                    484:        if (m != tq_first(&a->members))
                    485:            fputs(", ", stdout);
                    486:        if (m->type == COMMAND) {
                    487:            c = (struct sudo_command *) m->name;
                    488:            printf("%s%s%s", c->cmnd, c->args ? " " : "",
                    489:                c->args ? c->args : "");
                    490:        } else
                    491:            fputs(m->name, stdout);
                    492:     }
                    493:     putchar('\n');
                    494:     return(0);
                    495: }
1.1       millert   496:
1.16      millert   497: void
                    498: print_privilege(priv)
                    499:     struct privilege *priv;
                    500: {
                    501:     struct cmndspec *cs;
                    502:     struct member *m;
                    503:     struct privilege *p;
                    504:     struct cmndtag tags;
                    505:
                    506:     for (p = priv; p != NULL; p = p->next) {
                    507:        if (p != priv)
                    508:            fputs(" : ", stdout);
                    509:        tq_foreach_fwd(&p->hostlist, m) {
                    510:            if (m != tq_first(&p->hostlist))
                    511:                fputs(", ", stdout);
                    512:            print_member(m);
                    513:        }
                    514:        fputs(" = ", stdout);
                    515:        tags.nopasswd = tags.noexec = UNSPEC;
                    516:        tq_foreach_fwd(&p->cmndlist, cs) {
                    517:            if (cs != tq_first(&p->cmndlist))
                    518:                fputs(", ", stdout);
                    519:            /* XXX - runasgrouplist too */
                    520:            if (!tq_empty(&cs->runasuserlist)) {
                    521:                fputs("(", stdout);
                    522:                tq_foreach_fwd(&cs->runasuserlist, m) {
                    523:                    if (m != tq_first(&cs->runasuserlist))
                    524:                        fputs(", ", stdout);
                    525:                    print_member(m);
                    526:                }
                    527:                fputs(") ", stdout);
                    528:            }
                    529: #ifdef HAVE_SELINUX
                    530:            if (cs->role)
                    531:                printf("ROLE=%s ", cs->role);
                    532:            if (cs->type)
                    533:                printf("TYPE=%s ", cs->type);
                    534: #endif /* HAVE_SELINUX */
                    535:            if (cs->tags.nopasswd != UNSPEC && cs->tags.nopasswd != tags.nopasswd)
                    536:                printf("%sPASSWD: ", cs->tags.nopasswd ? "NO" : "");
                    537:            if (cs->tags.noexec != UNSPEC && cs->tags.noexec != tags.noexec)
                    538:                printf("%sEXEC: ", cs->tags.noexec ? "NO" : "");
                    539:            print_member(cs->cmnd);
                    540:            memcpy(&tags, &cs->tags, sizeof(tags));
1.1       millert   541:        }
                    542:     }
1.16      millert   543: }
1.1       millert   544:
1.16      millert   545: void
                    546: print_userspecs()
                    547: {
                    548:     struct member *m;
                    549:     struct userspec *us;
1.1       millert   550:
1.16      millert   551:     tq_foreach_fwd(&userspecs, us) {
                    552:        tq_foreach_fwd(&us->users, m) {
                    553:            if (m != tq_first(&us->users))
                    554:                fputs(", ", stdout);
                    555:            print_member(m);
                    556:        }
                    557:        putchar('\t');
                    558:        print_privilege(us->privileges.first); /* XXX */
                    559:        putchar('\n');
                    560:     }
                    561: }
1.1       millert   562:
1.16      millert   563: void
                    564: dump_sudoers()
                    565: {
                    566:     print_defaults();
1.1       millert   567:
1.16      millert   568:     putchar('\n');
                    569:     alias_apply(print_alias, NULL);
1.1       millert   570:
1.16      millert   571:     putchar('\n');
                    572:     print_userspecs();
                    573: }
1.1       millert   574:
1.16      millert   575: void
                    576: usage()
                    577: {
                    578:     (void) fprintf(stderr, "usage: %s [-d] [-G grfile] [-g group] [-h host] [-p pwfile] [-u user] <user> <command> [args]\n", getprogname());
                    579:     exit(1);
1.1       millert   580: }