[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.17

1.1       millert     1: /*
1.16      millert     2:  * Copyright (c) 1996, 1998-2005, 2007-2008
                      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.17    ! millert    74: __unused static const char rcsid[] = "$Sudo: testsudoers.c,v 1.128 2008/11/19 17:01:20 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 *
                    376: open_sudoers(path, keepopen)
                    377:     const char *path;
                    378:     int *keepopen;
1.11      millert   379: {
1.16      millert   380:     return(fopen(path, "r"));
1.11      millert   381: }
                    382:
1.5       millert   383: void
                    384: init_envtables()
1.1       millert   385: {
                    386:     return;
                    387: }
                    388:
1.16      millert   389: void
                    390: set_perms(perm)
                    391:     int perm;
1.1       millert   392: {
1.16      millert   393:     return;
                    394: }
1.1       millert   395:
1.16      millert   396: void
                    397: cleanup(gotsignal)
                    398:     int gotsignal;
                    399: {
                    400:     if (!gotsignal) {
                    401:        sudo_endpwent();
                    402:        sudo_endgrent();
                    403:     }
                    404: }
1.1       millert   405:
1.16      millert   406: void
                    407: print_member(m)
                    408:     struct member *m;
                    409: {
                    410:     struct sudo_command *c;
1.1       millert   411:
1.16      millert   412:     if (m->negated)
                    413:        putchar('!');
                    414:     if (m->name == NULL)
                    415:        fputs("ALL", stdout);
                    416:     else if (m->type != COMMAND)
                    417:        fputs(m->name, stdout);
                    418:     else {
                    419:        c = (struct sudo_command *) m->name;
                    420:        printf("%s%s%s", c->cmnd, c->args ? " " : "",
                    421:            c->args ? c->args : "");
1.1       millert   422:     }
1.16      millert   423: }
1.1       millert   424:
1.16      millert   425: void
                    426: print_defaults()
                    427: {
                    428:     struct defaults *d;
                    429:     struct member *m;
1.1       millert   430:
1.16      millert   431:     tq_foreach_fwd(&defaults, d) {
                    432:        (void) fputs("Defaults", stdout);
                    433:        switch (d->type) {
                    434:            case DEFAULTS_HOST:
                    435:                putchar('@');
                    436:                break;
                    437:            case DEFAULTS_USER:
                    438:                putchar(':');
                    439:                break;
                    440:            case DEFAULTS_RUNAS:
                    441:                putchar('>');
                    442:                break;
                    443:            case DEFAULTS_CMND:
                    444:                putchar('!');
                    445:                break;
                    446:        }
                    447:        tq_foreach_fwd(&d->binding, m) {
                    448:            if (m != tq_first(&d->binding))
                    449:                putchar(',');
                    450:            print_member(m);
                    451:        }
                    452:        printf("\t%s%s", d->op == FALSE ? "!" : "", d->var);
                    453:        if (d->val != NULL) {
                    454:            printf("%c%s", d->op == TRUE ? '=' : d->op, d->val);
                    455:        }
                    456:        putchar('\n');
1.1       millert   457:     }
1.16      millert   458: }
1.1       millert   459:
1.16      millert   460: int
                    461: print_alias(v1, v2)
                    462:     void *v1, *v2;
                    463: {
                    464:     struct alias *a = (struct alias *)v1;
                    465:     struct member *m;
                    466:     struct sudo_command *c;
                    467:
                    468:     switch (a->type) {
                    469:        case HOSTALIAS:
                    470:            (void) printf("Host_Alias\t%s = ", a->name);
                    471:            break;
                    472:        case CMNDALIAS:
                    473:            (void) printf("Cmnd_Alias\t%s = ", a->name);
                    474:            break;
                    475:        case USERALIAS:
                    476:            (void) printf("User_Alias\t%s = ", a->name);
                    477:            break;
                    478:        case RUNASALIAS:
                    479:            (void) printf("Runas_Alias\t%s = ", a->name);
                    480:            break;
                    481:     }
                    482:     tq_foreach_fwd(&a->members, m) {
                    483:        if (m != tq_first(&a->members))
                    484:            fputs(", ", stdout);
                    485:        if (m->type == COMMAND) {
                    486:            c = (struct sudo_command *) m->name;
                    487:            printf("%s%s%s", c->cmnd, c->args ? " " : "",
                    488:                c->args ? c->args : "");
                    489:        } else
                    490:            fputs(m->name, stdout);
                    491:     }
                    492:     putchar('\n');
                    493:     return(0);
                    494: }
1.1       millert   495:
1.16      millert   496: void
                    497: print_privilege(priv)
                    498:     struct privilege *priv;
                    499: {
                    500:     struct cmndspec *cs;
                    501:     struct member *m;
                    502:     struct privilege *p;
                    503:     struct cmndtag tags;
                    504:
                    505:     for (p = priv; p != NULL; p = p->next) {
                    506:        if (p != priv)
                    507:            fputs(" : ", stdout);
                    508:        tq_foreach_fwd(&p->hostlist, m) {
                    509:            if (m != tq_first(&p->hostlist))
                    510:                fputs(", ", stdout);
                    511:            print_member(m);
                    512:        }
                    513:        fputs(" = ", stdout);
                    514:        tags.nopasswd = tags.noexec = UNSPEC;
                    515:        tq_foreach_fwd(&p->cmndlist, cs) {
                    516:            if (cs != tq_first(&p->cmndlist))
                    517:                fputs(", ", stdout);
                    518:            /* XXX - runasgrouplist too */
                    519:            if (!tq_empty(&cs->runasuserlist)) {
                    520:                fputs("(", stdout);
                    521:                tq_foreach_fwd(&cs->runasuserlist, m) {
                    522:                    if (m != tq_first(&cs->runasuserlist))
                    523:                        fputs(", ", stdout);
                    524:                    print_member(m);
                    525:                }
                    526:                fputs(") ", stdout);
                    527:            }
                    528: #ifdef HAVE_SELINUX
                    529:            if (cs->role)
                    530:                printf("ROLE=%s ", cs->role);
                    531:            if (cs->type)
                    532:                printf("TYPE=%s ", cs->type);
                    533: #endif /* HAVE_SELINUX */
                    534:            if (cs->tags.nopasswd != UNSPEC && cs->tags.nopasswd != tags.nopasswd)
                    535:                printf("%sPASSWD: ", cs->tags.nopasswd ? "NO" : "");
                    536:            if (cs->tags.noexec != UNSPEC && cs->tags.noexec != tags.noexec)
                    537:                printf("%sEXEC: ", cs->tags.noexec ? "NO" : "");
                    538:            print_member(cs->cmnd);
                    539:            memcpy(&tags, &cs->tags, sizeof(tags));
1.1       millert   540:        }
                    541:     }
1.16      millert   542: }
1.1       millert   543:
1.16      millert   544: void
                    545: print_userspecs()
                    546: {
                    547:     struct member *m;
                    548:     struct userspec *us;
1.1       millert   549:
1.16      millert   550:     tq_foreach_fwd(&userspecs, us) {
                    551:        tq_foreach_fwd(&us->users, m) {
                    552:            if (m != tq_first(&us->users))
                    553:                fputs(", ", stdout);
                    554:            print_member(m);
                    555:        }
                    556:        putchar('\t');
                    557:        print_privilege(us->privileges.first); /* XXX */
                    558:        putchar('\n');
                    559:     }
                    560: }
1.1       millert   561:
1.16      millert   562: void
                    563: dump_sudoers()
                    564: {
                    565:     print_defaults();
1.1       millert   566:
1.16      millert   567:     putchar('\n');
                    568:     alias_apply(print_alias, NULL);
1.1       millert   569:
1.16      millert   570:     putchar('\n');
                    571:     print_userspecs();
                    572: }
1.1       millert   573:
1.16      millert   574: void
                    575: usage()
                    576: {
                    577:     (void) fprintf(stderr, "usage: %s [-d] [-G grfile] [-g group] [-h host] [-p pwfile] [-u user] <user> <command> [args]\n", getprogname());
                    578:     exit(1);
1.1       millert   579: }