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

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