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

Annotation of src/usr.bin/sudo/parse.c, Revision 1.19

1.1       millert     1: /*
1.13      millert     2:  * Copyright (c) 1996, 1998-2005, 2007
                      3:  *     Todd C. Miller <Todd.Miller@courtesan.com>
1.1       millert     4:  *
1.12      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.12      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.11      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.13      millert    24: #include <config.h>
1.1       millert    25:
1.6       millert    26: #include <sys/types.h>
                     27: #include <sys/param.h>
1.15      millert    28: #include <sys/socket.h>
1.6       millert    29: #include <sys/stat.h>
1.1       millert    30: #include <stdio.h>
                     31: #ifdef STDC_HEADERS
                     32: # include <stdlib.h>
1.6       millert    33: # include <stddef.h>
                     34: #else
                     35: # ifdef HAVE_STDLIB_H
                     36: #  include <stdlib.h>
                     37: # endif
1.1       millert    38: #endif /* STDC_HEADERS */
1.6       millert    39: #ifdef HAVE_STRING_H
                     40: # include <string.h>
                     41: #else
                     42: # ifdef HAVE_STRINGS_H
                     43: #  include <strings.h>
                     44: # endif
                     45: #endif /* HAVE_STRING_H */
1.1       millert    46: #ifdef HAVE_UNISTD_H
                     47: # include <unistd.h>
                     48: #endif /* HAVE_UNISTD_H */
1.2       millert    49: #ifdef HAVE_FNMATCH
1.1       millert    50: # include <fnmatch.h>
1.10      millert    51: #endif /* HAVE_FNMATCH */
1.13      millert    52: #ifdef HAVE_EXTENDED_GLOB
                     53: # include <glob.h>
                     54: #endif /* HAVE_EXTENDED_GLOB */
1.1       millert    55: #ifdef HAVE_NETGROUP_H
                     56: # include <netgroup.h>
                     57: #endif /* HAVE_NETGROUP_H */
                     58: #include <ctype.h>
                     59: #include <pwd.h>
                     60: #include <grp.h>
                     61: #include <netinet/in.h>
                     62: #include <arpa/inet.h>
                     63: #include <netdb.h>
1.6       millert    64: #ifdef HAVE_DIRENT_H
1.1       millert    65: # include <dirent.h>
                     66: # define NAMLEN(dirent) strlen((dirent)->d_name)
                     67: #else
                     68: # define dirent direct
                     69: # define NAMLEN(dirent) (dirent)->d_namlen
1.6       millert    70: # ifdef HAVE_SYS_NDIR_H
1.1       millert    71: #  include <sys/ndir.h>
                     72: # endif
1.6       millert    73: # ifdef HAVE_SYS_DIR_H
1.1       millert    74: #  include <sys/dir.h>
                     75: # endif
1.6       millert    76: # ifdef HAVE_NDIR_H
1.1       millert    77: #  include <ndir.h>
                     78: # endif
                     79: #endif
                     80:
                     81: #include "sudo.h"
                     82: #include "parse.h"
                     83: #include "interfaces.h"
                     84:
                     85: #ifndef HAVE_FNMATCH
                     86: # include "emul/fnmatch.h"
                     87: #endif /* HAVE_FNMATCH */
1.13      millert    88: #ifndef HAVE_EXTENDED_GLOB
                     89: # include "emul/glob.h"
                     90: #endif /* HAVE_EXTENDED_GLOB */
1.1       millert    91:
                     92: #ifndef lint
1.19    ! millert    93: __unused static const char rcsid[] = "$Sudo: parse.c,v 1.160.2.16 2008/02/09 14:44:48 millert Exp $";
1.1       millert    94: #endif /* lint */
                     95:
                     96: /*
                     97:  * Globals
                     98:  */
                     99: int parse_error = FALSE;
1.3       millert   100: extern int keepall;
1.1       millert   101: extern FILE *yyin, *yyout;
                    102:
                    103: /*
                    104:  * Prototypes
                    105:  */
                    106: static int has_meta    __P((char *));
                    107:        void init_parser        __P((void));
                    108:
                    109: /*
                    110:  * Look up the user in the sudoers file and check to see if they are
                    111:  * allowed to run the specified command on this host as the target user.
                    112:  */
                    113: int
1.6       millert   114: sudoers_lookup(pwflag)
                    115:     int pwflag;
1.1       millert   116: {
1.12      millert   117:     int error, nopass;
1.1       millert   118:
                    119:     /* We opened _PATH_SUDOERS in check_sudoers() so just rewind it. */
                    120:     rewind(sudoers_fp);
                    121:     yyin = sudoers_fp;
                    122:     yyout = stdout;
                    123:
                    124:     /* Allocate space for data structures in the parser. */
                    125:     init_parser();
                    126:
1.13      millert   127:     /* Keep more state for pseudo-commands so that listpw and verifypw work */
1.6       millert   128:     if (pwflag > 0)
1.3       millert   129:        keepall = TRUE;
                    130:
1.12      millert   131:     /* Need to be runas user while stat'ing things in the parser. */
                    132:     set_perms(PERM_RUNAS);
1.1       millert   133:     error = yyparse();
                    134:
                    135:     /* Close the sudoers file now that we are done with it. */
                    136:     (void) fclose(sudoers_fp);
                    137:     sudoers_fp = NULL;
                    138:
1.12      millert   139:     if (error || parse_error) {
                    140:        set_perms(PERM_ROOT);
1.1       millert   141:        return(VALIDATE_ERROR);
1.12      millert   142:     }
1.1       millert   143:
                    144:     /*
                    145:      * Assume the worst.  If the stack is empty the user was
                    146:      * not mentioned at all.
                    147:      */
1.12      millert   148:     if (def_authenticate)
1.2       millert   149:        error = VALIDATE_NOT_OK;
                    150:     else
                    151:        error = VALIDATE_NOT_OK | FLAG_NOPASS;
1.13      millert   152:     if (pwflag) {
1.12      millert   153:        SET(error, FLAG_NO_CHECK);
1.3       millert   154:     } else {
1.12      millert   155:        SET(error, FLAG_NO_HOST);
1.1       millert   156:        if (!top)
1.12      millert   157:            SET(error, FLAG_NO_USER);
1.3       millert   158:     }
1.1       millert   159:
                    160:     /*
1.12      millert   161:      * Only check the actual command if pwflag is not set.
1.3       millert   162:      * It is set for the "validate", "list" and "kill" pseudo-commands.
1.1       millert   163:      * Always check the host and user.
                    164:      */
1.6       millert   165:     nopass = -1;
1.12      millert   166:     if (pwflag) {
1.6       millert   167:        int found;
1.13      millert   168:        enum def_tupple pwcheck;
                    169:
                    170:        pwcheck = (pwflag == -1) ? never : sudo_defs_table[pwflag].sd_un.tuple;
1.3       millert   171:
1.12      millert   172:        if (pwcheck == always && def_authenticate)
                    173:            nopass = FLAG_CHECK_USER;
                    174:        else if (pwcheck == never || !def_authenticate)
1.3       millert   175:            nopass = FLAG_NOPASS;
                    176:        found = 0;
1.1       millert   177:        while (top) {
                    178:            if (host_matches == TRUE) {
1.3       millert   179:                found = 1;
1.12      millert   180:                if (pwcheck == any && no_passwd == TRUE)
1.3       millert   181:                    nopass = FLAG_NOPASS;
1.12      millert   182:                else if (pwcheck == all && nopass != 0)
1.3       millert   183:                    nopass = (no_passwd == TRUE) ? FLAG_NOPASS : 0;
1.1       millert   184:            }
                    185:            top--;
                    186:        }
1.3       millert   187:        if (found) {
1.12      millert   188:            set_perms(PERM_ROOT);
1.3       millert   189:            if (nopass == -1)
                    190:                nopass = 0;
                    191:            return(VALIDATE_OK | nopass);
                    192:        }
                    193:     } else {
1.1       millert   194:        while (top) {
                    195:            if (host_matches == TRUE) {
1.12      millert   196:                CLR(error, FLAG_NO_HOST);
                    197:                if (runas_matches == TRUE && cmnd_matches == TRUE) {
                    198:                    /*
                    199:                     * User was granted access to cmnd on host as user.
                    200:                     */
1.19    ! millert   201: #ifdef HAVE_SELINUX
        !           202:                    /* Set role and type if not specified on command line. */
        !           203:                    if (user_role == NULL) {
        !           204:                        if (match[top-1].role != NULL)
        !           205:                            user_role = match[top-1].role;
        !           206:                        else
        !           207:                            user_role = def_role;
        !           208:                    }
        !           209:                    if (user_type == NULL) {
        !           210:                        if (match[top-1].type != NULL)
        !           211:                            user_type = match[top-1].type;
        !           212:                        else
        !           213:                            user_type = def_type;
        !           214:                    }
        !           215: #endif
1.12      millert   216:                    set_perms(PERM_ROOT);
                    217:                    return(VALIDATE_OK |
                    218:                        (no_passwd == TRUE ? FLAG_NOPASS : 0) |
1.13      millert   219:                        (no_execve == TRUE ? FLAG_NOEXEC : 0) |
1.18      millert   220:                        (setenv_ok >= TRUE ? FLAG_SETENV : 0));
1.12      millert   221:                } else if ((runas_matches == TRUE && cmnd_matches == FALSE) ||
                    222:                    (runas_matches == FALSE && cmnd_matches == TRUE)) {
                    223:                    /*
                    224:                     * User was explicitly denied access to cmnd on host.
                    225:                     */
                    226:                    set_perms(PERM_ROOT);
                    227:                    return(VALIDATE_NOT_OK |
                    228:                        (no_passwd == TRUE ? FLAG_NOPASS : 0) |
1.13      millert   229:                        (no_execve == TRUE ? FLAG_NOEXEC : 0) |
1.18      millert   230:                        (setenv_ok >= TRUE ? FLAG_SETENV : 0));
1.1       millert   231:                }
                    232:            }
                    233:            top--;
                    234:        }
1.3       millert   235:     }
1.12      millert   236:     set_perms(PERM_ROOT);
1.1       millert   237:
                    238:     /*
1.12      millert   239:      * The user was neither explicitly granted nor denied access.
1.1       millert   240:      */
1.6       millert   241:     if (nopass == -1)
                    242:        nopass = 0;
                    243:     return(error | nopass);
1.1       millert   244: }
                    245:
                    246: /*
                    247:  * If path doesn't end in /, return TRUE iff cmnd & path name the same inode;
1.12      millert   248:  * otherwise, return TRUE if user_cmnd names one of the inodes in path.
1.1       millert   249:  */
                    250: int
1.12      millert   251: command_matches(sudoers_cmnd, sudoers_args)
                    252:     char *sudoers_cmnd;
1.1       millert   253:     char *sudoers_args;
                    254: {
1.12      millert   255:     struct stat sudoers_stat;
                    256:     struct dirent *dent;
1.13      millert   257:     char **ap, *base, buf[PATH_MAX];
                    258:     glob_t gl;
1.1       millert   259:     DIR *dirp;
                    260:
1.12      millert   261:     /* Check for pseudo-commands */
                    262:     if (strchr(user_cmnd, '/') == NULL) {
                    263:        /*
                    264:         * Return true if both sudoers_cmnd and user_cmnd are "sudoedit" AND
                    265:         *  a) there are no args in sudoers OR
                    266:         *  b) there are no args on command line and none req by sudoers OR
                    267:         *  c) there are args in sudoers and on command line and they match
                    268:         */
                    269:        if (strcmp(sudoers_cmnd, "sudoedit") != 0 ||
                    270:            strcmp(user_cmnd, "sudoedit") != 0)
                    271:            return(FALSE);
                    272:        if (!sudoers_args ||
                    273:            (!user_args && sudoers_args && !strcmp("\"\"", sudoers_args)) ||
                    274:            (sudoers_args &&
                    275:             fnmatch(sudoers_args, user_args ? user_args : "", 0) == 0)) {
1.13      millert   276:            efree(safe_cmnd);
1.12      millert   277:            safe_cmnd = estrdup(sudoers_cmnd);
                    278:            return(TRUE);
                    279:        } else
1.1       millert   280:            return(FALSE);
                    281:     }
                    282:
                    283:     /*
1.12      millert   284:      * If sudoers_cmnd has meta characters in it, use fnmatch(3)
                    285:      * to do the matching.
1.1       millert   286:      */
1.12      millert   287:     if (has_meta(sudoers_cmnd)) {
1.1       millert   288:        /*
1.13      millert   289:         * Return true if we find a match in the glob(3) results AND
1.1       millert   290:         *  a) there are no args in sudoers OR
                    291:         *  b) there are no args on command line and none required by sudoers OR
                    292:         *  c) there are args in sudoers and on command line and they match
                    293:         * else return false.
1.13      millert   294:         *
                    295:         * Could optimize patterns ending in "/*" to "/user_base"
1.1       millert   296:         */
1.13      millert   297: #define GLOB_FLAGS     (GLOB_NOSORT | GLOB_MARK | GLOB_BRACE | GLOB_TILDE)
                    298:        if (glob(sudoers_cmnd, GLOB_FLAGS, NULL, &gl) != 0) {
                    299:            globfree(&gl);
1.1       millert   300:            return(FALSE);
1.13      millert   301:        }
                    302:        /* For each glob match, compare basename, st_dev and st_ino. */
                    303:        for (ap = gl.gl_pathv; *ap != NULL; ap++) {
                    304:            /* only stat if basenames are the same */
                    305:            if ((base = strrchr(*ap, '/')) != NULL)
                    306:                base++;
                    307:            else
                    308:                base = *ap;
                    309:            if (strcmp(user_base, base) != 0 ||
                    310:                stat(*ap, &sudoers_stat) == -1)
                    311:                continue;
                    312:            if (user_stat->st_dev == sudoers_stat.st_dev &&
                    313:                user_stat->st_ino == sudoers_stat.st_ino) {
                    314:                efree(safe_cmnd);
                    315:                safe_cmnd = estrdup(*ap);
                    316:                break;
                    317:            }
                    318:        }
                    319:        globfree(&gl);
                    320:        if (*ap == NULL)
                    321:            return(FALSE);
                    322:
1.1       millert   323:        if (!sudoers_args ||
1.12      millert   324:            (!user_args && sudoers_args && !strcmp("\"\"", sudoers_args)) ||
                    325:            (sudoers_args &&
                    326:             fnmatch(sudoers_args, user_args ? user_args : "", 0) == 0)) {
1.13      millert   327:            efree(safe_cmnd);
1.1       millert   328:            safe_cmnd = estrdup(user_cmnd);
                    329:            return(TRUE);
                    330:        } else
                    331:            return(FALSE);
                    332:     } else {
1.12      millert   333:        size_t dlen = strlen(sudoers_cmnd);
                    334:
1.1       millert   335:        /*
                    336:         * No meta characters
                    337:         * Check to make sure this is not a directory spec (doesn't end in '/')
                    338:         */
1.12      millert   339:        if (sudoers_cmnd[dlen - 1] != '/') {
                    340:            /* Only proceed if user_base and basename(sudoers_cmnd) match */
                    341:            if ((base = strrchr(sudoers_cmnd, '/')) == NULL)
                    342:                base = sudoers_cmnd;
1.1       millert   343:            else
1.12      millert   344:                base++;
                    345:            if (strcmp(user_base, base) != 0 ||
                    346:                stat(sudoers_cmnd, &sudoers_stat) == -1)
1.1       millert   347:                return(FALSE);
                    348:
                    349:            /*
                    350:             * Return true if inode/device matches AND
                    351:             *  a) there are no args in sudoers OR
                    352:             *  b) there are no args on command line and none req by sudoers OR
                    353:             *  c) there are args in sudoers and on command line and they match
                    354:             */
1.12      millert   355:            if (user_stat->st_dev != sudoers_stat.st_dev ||
                    356:                user_stat->st_ino != sudoers_stat.st_ino)
1.1       millert   357:                return(FALSE);
                    358:            if (!sudoers_args ||
1.12      millert   359:                (!user_args && sudoers_args && !strcmp("\"\"", sudoers_args)) ||
1.1       millert   360:                (sudoers_args &&
1.12      millert   361:                 fnmatch(sudoers_args, user_args ? user_args : "", 0) == 0)) {
1.13      millert   362:                efree(safe_cmnd);
1.12      millert   363:                safe_cmnd = estrdup(sudoers_cmnd);
1.1       millert   364:                return(TRUE);
                    365:            } else
                    366:                return(FALSE);
                    367:        }
                    368:
                    369:        /*
1.12      millert   370:         * Grot through sudoers_cmnd's directory entries, looking for user_base.
1.1       millert   371:         */
1.12      millert   372:        dirp = opendir(sudoers_cmnd);
1.1       millert   373:        if (dirp == NULL)
                    374:            return(FALSE);
                    375:
1.12      millert   376:        if (strlcpy(buf, sudoers_cmnd, sizeof(buf)) >= sizeof(buf))
                    377:            return(FALSE);
1.1       millert   378:        while ((dent = readdir(dirp)) != NULL) {
1.12      millert   379:            /* ignore paths > PATH_MAX (XXX - log) */
                    380:            buf[dlen] = '\0';
                    381:            if (strlcat(buf, dent->d_name, sizeof(buf)) >= sizeof(buf))
1.1       millert   382:                continue;
                    383:
                    384:            /* only stat if basenames are the same */
1.12      millert   385:            if (strcmp(user_base, dent->d_name) != 0 ||
                    386:                stat(buf, &sudoers_stat) == -1)
1.1       millert   387:                continue;
1.12      millert   388:            if (user_stat->st_dev == sudoers_stat.st_dev &&
                    389:                user_stat->st_ino == sudoers_stat.st_ino) {
1.13      millert   390:                efree(safe_cmnd);
1.1       millert   391:                safe_cmnd = estrdup(buf);
                    392:                break;
                    393:            }
                    394:        }
                    395:
                    396:        closedir(dirp);
                    397:        return(dent != NULL);
                    398:     }
                    399: }
                    400:
1.15      millert   401: static int
                    402: addr_matches_if(n)
1.1       millert   403:     char *n;
                    404: {
                    405:     int i;
1.15      millert   406:     struct in_addr addr;
                    407:     struct interface *ifp;
1.17      millert   408: #ifdef HAVE_IN6_ADDR
1.15      millert   409:     struct in6_addr addr6;
                    410:     int j;
                    411: #endif
1.16      millert   412:     int family;
1.15      millert   413:
1.17      millert   414: #ifdef HAVE_IN6_ADDR
1.15      millert   415:     if (inet_pton(AF_INET6, n, &addr6) > 0) {
                    416:        family = AF_INET6;
                    417:     } else
1.16      millert   418: #endif
1.15      millert   419:     {
                    420:        family = AF_INET;
                    421:        addr.s_addr = inet_addr(n);
                    422:     }
                    423:
                    424:     for (i = 0; i < num_interfaces; i++) {
                    425:        ifp = &interfaces[i];
                    426:        if (ifp->family != family)
                    427:            continue;
                    428:        switch(family) {
                    429:            case AF_INET:
                    430:                if (ifp->addr.ip4.s_addr == addr.s_addr ||
                    431:                    (ifp->addr.ip4.s_addr & ifp->netmask.ip4.s_addr)
                    432:                    == addr.s_addr)
                    433:                    return(TRUE);
                    434:                break;
1.17      millert   435: #ifdef HAVE_IN6_ADDR
1.15      millert   436:            case AF_INET6:
                    437:                if (memcmp(ifp->addr.ip6.s6_addr, addr6.s6_addr,
                    438:                    sizeof(addr6.s6_addr)) == 0)
                    439:                    return(TRUE);
                    440:                for (j = 0; j < sizeof(addr6.s6_addr); j++) {
                    441:                    if ((ifp->addr.ip6.s6_addr[j] & ifp->netmask.ip6.s6_addr[j]) != addr6.s6_addr[j])
                    442:                        break;
                    443:                }
                    444:                if (j == sizeof(addr6.s6_addr))
                    445:                    return(TRUE);
1.17      millert   446: #endif /* HAVE_IN6_ADDR */
1.15      millert   447:        }
                    448:     }
                    449:
                    450:     return(FALSE);
                    451: }
                    452:
                    453: static int
                    454: addr_matches_if_netmask(n, m)
                    455:     char *n;
1.1       millert   456:     char *m;
1.15      millert   457: {
                    458:     int i;
1.1       millert   459:     struct in_addr addr, mask;
1.15      millert   460:     struct interface *ifp;
1.17      millert   461: #ifdef HAVE_IN6_ADDR
1.15      millert   462:     struct in6_addr addr6, mask6;
                    463:     int j;
                    464: #endif
1.16      millert   465:     int family;
1.1       millert   466:
1.17      millert   467: #ifdef HAVE_IN6_ADDR
1.15      millert   468:     if (inet_pton(AF_INET6, n, &addr6) > 0)
                    469:        family = AF_INET6;
                    470:     else
1.16      millert   471: #endif
1.15      millert   472:     {
                    473:        family = AF_INET;
1.1       millert   474:        addr.s_addr = inet_addr(n);
1.15      millert   475:     }
                    476:
                    477:     if (family == AF_INET) {
1.1       millert   478:        if (strchr(m, '.'))
                    479:            mask.s_addr = inet_addr(m);
1.6       millert   480:        else {
                    481:            i = 32 - atoi(m);
                    482:            mask.s_addr = 0xffffffff;
                    483:            mask.s_addr >>= i;
                    484:            mask.s_addr <<= i;
                    485:            mask.s_addr = htonl(mask.s_addr);
                    486:        }
1.15      millert   487:     }
1.17      millert   488: #ifdef HAVE_IN6_ADDR
1.15      millert   489:     else {
                    490:        if (inet_pton(AF_INET6, m, &mask6) <= 0) {
                    491:            j = atoi(m);
                    492:            for (i = 0; i < 16; i++) {
                    493:                if (j < i * 8)
                    494:                    mask6.s6_addr[i] = 0;
                    495:                else if (i * 8 + 8 <= j)
                    496:                    mask6.s6_addr[i] = 0xff;
                    497:                else
                    498:                    mask6.s6_addr[i] = 0xff00 >> (j - i * 8);
                    499:            }
                    500:        }
                    501:     }
1.17      millert   502: #endif /* HAVE_IN6_ADDR */
1.1       millert   503:
1.15      millert   504:     for (i = 0; i < num_interfaces; i++) {
                    505:        ifp = &interfaces[i];
                    506:        if (ifp->family != family)
                    507:            continue;
                    508:        switch(family) {
                    509:            case AF_INET:
                    510:                if ((ifp->addr.ip4.s_addr & mask.s_addr) == addr.s_addr)
                    511:                    return(TRUE);
1.17      millert   512: #ifdef HAVE_IN6_ADDR
1.15      millert   513:            case AF_INET6:
                    514:                for (j = 0; j < sizeof(addr6.s6_addr); j++) {
                    515:                    if ((ifp->addr.ip6.s6_addr[j] & mask6.s6_addr[j]) != addr6.s6_addr[j])
                    516:                        break;
                    517:                }
                    518:                if (j == sizeof(addr6.s6_addr))
                    519:                    return(TRUE);
1.17      millert   520: #endif /* HAVE_IN6_ADDR */
1.15      millert   521:        }
1.1       millert   522:     }
                    523:
                    524:     return(FALSE);
1.15      millert   525: }
                    526:
                    527: /*
                    528:  * Returns TRUE if "n" is one of our ip addresses or if
                    529:  * "n" is a network that we are on, else returns FALSE.
                    530:  */
                    531: int
                    532: addr_matches(n)
                    533:     char *n;
                    534: {
                    535:     char *m;
                    536:     int retval;
                    537:
                    538:     /* If there's an explicit netmask, use it. */
                    539:     if ((m = strchr(n, '/'))) {
                    540:        *m++ = '\0';
                    541:        retval = addr_matches_if_netmask(n, m);
                    542:        *(m - 1) = '/';
                    543:     } else
                    544:        retval = addr_matches_if(n);
                    545:
                    546:     return(retval);
1.1       millert   547: }
                    548:
                    549: /*
1.4       millert   550:  * Returns 0 if the hostname matches the pattern and non-zero otherwise.
                    551:  */
                    552: int
                    553: hostname_matches(shost, lhost, pattern)
                    554:     char *shost;
                    555:     char *lhost;
                    556:     char *pattern;
                    557: {
                    558:     if (has_meta(pattern)) {
                    559:        if (strchr(pattern, '.'))
                    560:            return(fnmatch(pattern, lhost, FNM_CASEFOLD));
                    561:        else
                    562:            return(fnmatch(pattern, shost, FNM_CASEFOLD));
                    563:     } else {
                    564:        if (strchr(pattern, '.'))
                    565:            return(strcasecmp(lhost, pattern));
                    566:        else
                    567:            return(strcasecmp(shost, pattern));
                    568:     }
                    569: }
                    570:
                    571: /*
1.12      millert   572:  *  Returns TRUE if the user/uid from sudoers matches the specified user/uid,
                    573:  *  else returns FALSE.
                    574:  */
                    575: int
                    576: userpw_matches(sudoers_user, user, pw)
                    577:     char *sudoers_user;
                    578:     char *user;
                    579:     struct passwd *pw;
                    580: {
                    581:     if (pw != NULL && *sudoers_user == '#') {
                    582:        uid_t uid = atoi(sudoers_user + 1);
                    583:        if (uid == pw->pw_uid)
                    584:            return(1);
                    585:     }
                    586:     return(strcmp(sudoers_user, user) == 0);
                    587: }
                    588:
                    589: /*
1.1       millert   590:  *  Returns TRUE if the given user belongs to the named group,
                    591:  *  else returns FALSE.
1.12      millert   592:  *  XXX - reduce the number of passwd/group lookups
1.1       millert   593:  */
                    594: int
1.12      millert   595: usergr_matches(group, user, pw)
1.1       millert   596:     char *group;
                    597:     char *user;
1.12      millert   598:     struct passwd *pw;
1.1       millert   599: {
                    600:     struct group *grp;
1.12      millert   601:     gid_t pw_gid;
1.1       millert   602:     char **cur;
1.14      millert   603:     int i;
1.1       millert   604:
                    605:     /* make sure we have a valid usergroup, sudo style */
                    606:     if (*group++ != '%')
                    607:        return(FALSE);
                    608:
1.12      millert   609:     /* look up user's primary gid in the passwd file */
                    610:     if (pw == NULL && (pw = getpwnam(user)) == NULL)
1.1       millert   611:        return(FALSE);
1.12      millert   612:     pw_gid = pw->pw_gid;
1.1       millert   613:
1.12      millert   614:     if ((grp = getgrnam(group)) == NULL)
1.1       millert   615:        return(FALSE);
                    616:
1.12      millert   617:     /* check against user's primary (passwd file) gid */
                    618:     if (grp->gr_gid == pw_gid)
1.1       millert   619:        return(TRUE);
                    620:
1.13      millert   621:     /*
                    622:      * If the user has a supplementary group vector, check it first.
                    623:      */
1.14      millert   624:     for (i = 0; i < user_ngroups; i++) {
                    625:        if (grp->gr_gid == user_groups[i])
1.1       millert   626:            return(TRUE);
                    627:     }
1.13      millert   628:     if (grp->gr_mem != NULL) {
                    629:        for (cur = grp->gr_mem; *cur; cur++) {
                    630:            if (strcmp(*cur, user) == 0)
                    631:                return(TRUE);
                    632:        }
                    633:     }
1.1       millert   634:
                    635:     return(FALSE);
                    636: }
                    637:
                    638: /*
                    639:  * Returns TRUE if "host" and "user" belong to the netgroup "netgr",
1.3       millert   640:  * else return FALSE.  Either of "host", "shost" or "user" may be NULL
1.1       millert   641:  * in which case that argument is not checked...
                    642:  */
                    643: int
1.3       millert   644: netgr_matches(netgr, host, shost, user)
1.1       millert   645:     char *netgr;
                    646:     char *host;
1.3       millert   647:     char *shost;
1.1       millert   648:     char *user;
                    649: {
1.13      millert   650:     static char *domain;
1.1       millert   651: #ifdef HAVE_GETDOMAINNAME
1.13      millert   652:     static int initialized;
                    653: #endif
1.1       millert   654:
                    655:     /* make sure we have a valid netgroup, sudo style */
                    656:     if (*netgr++ != '+')
                    657:        return(FALSE);
                    658:
                    659: #ifdef HAVE_GETDOMAINNAME
                    660:     /* get the domain name (if any) */
1.13      millert   661:     if (!initialized) {
1.1       millert   662:        domain = (char *) emalloc(MAXHOSTNAMELEN);
                    663:        if (getdomainname(domain, MAXHOSTNAMELEN) == -1 || *domain == '\0') {
1.13      millert   664:            efree(domain);
1.1       millert   665:            domain = NULL;
                    666:        }
1.13      millert   667:        initialized = 1;
1.1       millert   668:     }
                    669: #endif /* HAVE_GETDOMAINNAME */
                    670:
                    671: #ifdef HAVE_INNETGR
1.3       millert   672:     if (innetgr(netgr, host, user, domain))
                    673:        return(TRUE);
                    674:     else if (host != shost && innetgr(netgr, shost, user, domain))
                    675:        return(TRUE);
                    676: #endif /* HAVE_INNETGR */
                    677:
1.1       millert   678:     return(FALSE);
                    679: }
                    680:
                    681: /*
                    682:  * Returns TRUE if "s" has shell meta characters in it,
                    683:  * else returns FALSE.
                    684:  */
                    685: static int
                    686: has_meta(s)
                    687:     char *s;
                    688: {
1.4       millert   689:     char *t;
1.12      millert   690:
1.1       millert   691:     for (t = s; *t; t++) {
                    692:        if (*t == '\\' || *t == '?' || *t == '*' || *t == '[' || *t == ']')
                    693:            return(TRUE);
                    694:     }
                    695:     return(FALSE);
                    696: }