Annotation of src/usr.bin/sudo/parse.c, Revision 1.15
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.15 ! millert 93: __unused static const char rcsid[] = "$Sudo: parse.c,v 1.160.2.12 2007/08/13 16:30:02 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: */
201: set_perms(PERM_ROOT);
202: return(VALIDATE_OK |
203: (no_passwd == TRUE ? FLAG_NOPASS : 0) |
1.13 millert 204: (no_execve == TRUE ? FLAG_NOEXEC : 0) |
205: (setenv_ok == TRUE ? FLAG_SETENV : 0));
1.12 millert 206: } else if ((runas_matches == TRUE && cmnd_matches == FALSE) ||
207: (runas_matches == FALSE && cmnd_matches == TRUE)) {
208: /*
209: * User was explicitly denied access to cmnd on host.
210: */
211: set_perms(PERM_ROOT);
212: return(VALIDATE_NOT_OK |
213: (no_passwd == TRUE ? FLAG_NOPASS : 0) |
1.13 millert 214: (no_execve == TRUE ? FLAG_NOEXEC : 0) |
215: (setenv_ok == TRUE ? FLAG_SETENV : 0));
1.1 millert 216: }
217: }
218: top--;
219: }
1.3 millert 220: }
1.12 millert 221: set_perms(PERM_ROOT);
1.1 millert 222:
223: /*
1.12 millert 224: * The user was neither explicitly granted nor denied access.
1.1 millert 225: */
1.6 millert 226: if (nopass == -1)
227: nopass = 0;
228: return(error | nopass);
1.1 millert 229: }
230:
231: /*
232: * If path doesn't end in /, return TRUE iff cmnd & path name the same inode;
1.12 millert 233: * otherwise, return TRUE if user_cmnd names one of the inodes in path.
1.1 millert 234: */
235: int
1.12 millert 236: command_matches(sudoers_cmnd, sudoers_args)
237: char *sudoers_cmnd;
1.1 millert 238: char *sudoers_args;
239: {
1.12 millert 240: struct stat sudoers_stat;
241: struct dirent *dent;
1.13 millert 242: char **ap, *base, buf[PATH_MAX];
243: glob_t gl;
1.1 millert 244: DIR *dirp;
245:
1.12 millert 246: /* Check for pseudo-commands */
247: if (strchr(user_cmnd, '/') == NULL) {
248: /*
249: * Return true if both sudoers_cmnd and user_cmnd are "sudoedit" AND
250: * a) there are no args in sudoers OR
251: * b) there are no args on command line and none req by sudoers OR
252: * c) there are args in sudoers and on command line and they match
253: */
254: if (strcmp(sudoers_cmnd, "sudoedit") != 0 ||
255: strcmp(user_cmnd, "sudoedit") != 0)
256: return(FALSE);
257: if (!sudoers_args ||
258: (!user_args && sudoers_args && !strcmp("\"\"", sudoers_args)) ||
259: (sudoers_args &&
260: fnmatch(sudoers_args, user_args ? user_args : "", 0) == 0)) {
1.13 millert 261: efree(safe_cmnd);
1.12 millert 262: safe_cmnd = estrdup(sudoers_cmnd);
263: return(TRUE);
264: } else
1.1 millert 265: return(FALSE);
266: }
267:
268: /*
1.12 millert 269: * If sudoers_cmnd has meta characters in it, use fnmatch(3)
270: * to do the matching.
1.1 millert 271: */
1.12 millert 272: if (has_meta(sudoers_cmnd)) {
1.1 millert 273: /*
1.13 millert 274: * Return true if we find a match in the glob(3) results AND
1.1 millert 275: * a) there are no args in sudoers OR
276: * b) there are no args on command line and none required by sudoers OR
277: * c) there are args in sudoers and on command line and they match
278: * else return false.
1.13 millert 279: *
280: * Could optimize patterns ending in "/*" to "/user_base"
1.1 millert 281: */
1.13 millert 282: #define GLOB_FLAGS (GLOB_NOSORT | GLOB_MARK | GLOB_BRACE | GLOB_TILDE)
283: if (glob(sudoers_cmnd, GLOB_FLAGS, NULL, &gl) != 0) {
284: globfree(&gl);
1.1 millert 285: return(FALSE);
1.13 millert 286: }
287: /* For each glob match, compare basename, st_dev and st_ino. */
288: for (ap = gl.gl_pathv; *ap != NULL; ap++) {
289: /* only stat if basenames are the same */
290: if ((base = strrchr(*ap, '/')) != NULL)
291: base++;
292: else
293: base = *ap;
294: if (strcmp(user_base, base) != 0 ||
295: stat(*ap, &sudoers_stat) == -1)
296: continue;
297: if (user_stat->st_dev == sudoers_stat.st_dev &&
298: user_stat->st_ino == sudoers_stat.st_ino) {
299: efree(safe_cmnd);
300: safe_cmnd = estrdup(*ap);
301: break;
302: }
303: }
304: globfree(&gl);
305: if (*ap == NULL)
306: return(FALSE);
307:
1.1 millert 308: if (!sudoers_args ||
1.12 millert 309: (!user_args && sudoers_args && !strcmp("\"\"", sudoers_args)) ||
310: (sudoers_args &&
311: fnmatch(sudoers_args, user_args ? user_args : "", 0) == 0)) {
1.13 millert 312: efree(safe_cmnd);
1.1 millert 313: safe_cmnd = estrdup(user_cmnd);
314: return(TRUE);
315: } else
316: return(FALSE);
317: } else {
1.12 millert 318: size_t dlen = strlen(sudoers_cmnd);
319:
1.1 millert 320: /*
321: * No meta characters
322: * Check to make sure this is not a directory spec (doesn't end in '/')
323: */
1.12 millert 324: if (sudoers_cmnd[dlen - 1] != '/') {
325: /* Only proceed if user_base and basename(sudoers_cmnd) match */
326: if ((base = strrchr(sudoers_cmnd, '/')) == NULL)
327: base = sudoers_cmnd;
1.1 millert 328: else
1.12 millert 329: base++;
330: if (strcmp(user_base, base) != 0 ||
331: stat(sudoers_cmnd, &sudoers_stat) == -1)
1.1 millert 332: return(FALSE);
333:
334: /*
335: * Return true if inode/device matches AND
336: * a) there are no args in sudoers OR
337: * b) there are no args on command line and none req by sudoers OR
338: * c) there are args in sudoers and on command line and they match
339: */
1.12 millert 340: if (user_stat->st_dev != sudoers_stat.st_dev ||
341: user_stat->st_ino != sudoers_stat.st_ino)
1.1 millert 342: return(FALSE);
343: if (!sudoers_args ||
1.12 millert 344: (!user_args && sudoers_args && !strcmp("\"\"", sudoers_args)) ||
1.1 millert 345: (sudoers_args &&
1.12 millert 346: fnmatch(sudoers_args, user_args ? user_args : "", 0) == 0)) {
1.13 millert 347: efree(safe_cmnd);
1.12 millert 348: safe_cmnd = estrdup(sudoers_cmnd);
1.1 millert 349: return(TRUE);
350: } else
351: return(FALSE);
352: }
353:
354: /*
1.12 millert 355: * Grot through sudoers_cmnd's directory entries, looking for user_base.
1.1 millert 356: */
1.12 millert 357: dirp = opendir(sudoers_cmnd);
1.1 millert 358: if (dirp == NULL)
359: return(FALSE);
360:
1.12 millert 361: if (strlcpy(buf, sudoers_cmnd, sizeof(buf)) >= sizeof(buf))
362: return(FALSE);
1.1 millert 363: while ((dent = readdir(dirp)) != NULL) {
1.12 millert 364: /* ignore paths > PATH_MAX (XXX - log) */
365: buf[dlen] = '\0';
366: if (strlcat(buf, dent->d_name, sizeof(buf)) >= sizeof(buf))
1.1 millert 367: continue;
368:
369: /* only stat if basenames are the same */
1.12 millert 370: if (strcmp(user_base, dent->d_name) != 0 ||
371: stat(buf, &sudoers_stat) == -1)
1.1 millert 372: continue;
1.12 millert 373: if (user_stat->st_dev == sudoers_stat.st_dev &&
374: user_stat->st_ino == sudoers_stat.st_ino) {
1.13 millert 375: efree(safe_cmnd);
1.1 millert 376: safe_cmnd = estrdup(buf);
377: break;
378: }
379: }
380:
381: closedir(dirp);
382: return(dent != NULL);
383: }
384: }
385:
1.15 ! millert 386: static int
! 387: addr_matches_if(n)
1.1 millert 388: char *n;
389: {
390: int i;
1.15 ! millert 391: struct in_addr addr;
! 392: struct interface *ifp;
! 393: #ifdef AF_INET6
! 394: struct in6_addr addr6;
! 395: int j;
! 396: #endif
! 397: int family = AF_UNSPEC;
! 398:
! 399: #ifdef AF_INET6
! 400: if (inet_pton(AF_INET6, n, &addr6) > 0) {
! 401: family = AF_INET6;
! 402: } else
! 403: #else
! 404: {
! 405: family = AF_INET;
! 406: addr.s_addr = inet_addr(n);
! 407: }
! 408: #endif
! 409:
! 410: for (i = 0; i < num_interfaces; i++) {
! 411: ifp = &interfaces[i];
! 412: if (ifp->family != family)
! 413: continue;
! 414: switch(family) {
! 415: case AF_INET:
! 416: if (ifp->addr.ip4.s_addr == addr.s_addr ||
! 417: (ifp->addr.ip4.s_addr & ifp->netmask.ip4.s_addr)
! 418: == addr.s_addr)
! 419: return(TRUE);
! 420: break;
! 421: #ifdef AF_INET6
! 422: case AF_INET6:
! 423: if (memcmp(ifp->addr.ip6.s6_addr, addr6.s6_addr,
! 424: sizeof(addr6.s6_addr)) == 0)
! 425: return(TRUE);
! 426: for (j = 0; j < sizeof(addr6.s6_addr); j++) {
! 427: if ((ifp->addr.ip6.s6_addr[j] & ifp->netmask.ip6.s6_addr[j]) != addr6.s6_addr[j])
! 428: break;
! 429: }
! 430: if (j == sizeof(addr6.s6_addr))
! 431: return(TRUE);
! 432: #endif /* AF_INET6 */
! 433: }
! 434: }
! 435:
! 436: return(FALSE);
! 437: }
! 438:
! 439: static int
! 440: addr_matches_if_netmask(n, m)
! 441: char *n;
1.1 millert 442: char *m;
1.15 ! millert 443: {
! 444: int i;
1.1 millert 445: struct in_addr addr, mask;
1.15 ! millert 446: struct interface *ifp;
! 447: #ifdef AF_INET6
! 448: struct in6_addr addr6, mask6;
! 449: int j;
! 450: #endif
! 451: int family = AF_UNSPEC;
1.1 millert 452:
1.15 ! millert 453: #ifdef AF_INET6
! 454: if (inet_pton(AF_INET6, n, &addr6) > 0)
! 455: family = AF_INET6;
! 456: else
! 457: #else
! 458: {
! 459: family = AF_INET;
1.1 millert 460: addr.s_addr = inet_addr(n);
1.15 ! millert 461: }
! 462: #endif
! 463:
! 464: if (family == AF_INET) {
1.1 millert 465: if (strchr(m, '.'))
466: mask.s_addr = inet_addr(m);
1.6 millert 467: else {
468: i = 32 - atoi(m);
469: mask.s_addr = 0xffffffff;
470: mask.s_addr >>= i;
471: mask.s_addr <<= i;
472: mask.s_addr = htonl(mask.s_addr);
473: }
1.15 ! millert 474: }
! 475: #ifdef AF_INET6
! 476: else {
! 477: if (inet_pton(AF_INET6, m, &mask6) <= 0) {
! 478: j = atoi(m);
! 479: for (i = 0; i < 16; i++) {
! 480: if (j < i * 8)
! 481: mask6.s6_addr[i] = 0;
! 482: else if (i * 8 + 8 <= j)
! 483: mask6.s6_addr[i] = 0xff;
! 484: else
! 485: mask6.s6_addr[i] = 0xff00 >> (j - i * 8);
! 486: }
! 487: }
! 488: }
! 489: #endif /* AF_INET6 */
1.1 millert 490:
1.15 ! millert 491: for (i = 0; i < num_interfaces; i++) {
! 492: ifp = &interfaces[i];
! 493: if (ifp->family != family)
! 494: continue;
! 495: switch(family) {
! 496: case AF_INET:
! 497: if ((ifp->addr.ip4.s_addr & mask.s_addr) == addr.s_addr)
! 498: return(TRUE);
! 499: #ifdef AF_INET6
! 500: case AF_INET6:
! 501: for (j = 0; j < sizeof(addr6.s6_addr); j++) {
! 502: if ((ifp->addr.ip6.s6_addr[j] & mask6.s6_addr[j]) != addr6.s6_addr[j])
! 503: break;
! 504: }
! 505: if (j == sizeof(addr6.s6_addr))
! 506: return(TRUE);
! 507: #endif /* AF_INET6 */
! 508: }
1.1 millert 509: }
510:
511: return(FALSE);
1.15 ! millert 512: }
! 513:
! 514: /*
! 515: * Returns TRUE if "n" is one of our ip addresses or if
! 516: * "n" is a network that we are on, else returns FALSE.
! 517: */
! 518: int
! 519: addr_matches(n)
! 520: char *n;
! 521: {
! 522: char *m;
! 523: int retval;
! 524:
! 525: /* If there's an explicit netmask, use it. */
! 526: if ((m = strchr(n, '/'))) {
! 527: *m++ = '\0';
! 528: retval = addr_matches_if_netmask(n, m);
! 529: *(m - 1) = '/';
! 530: } else
! 531: retval = addr_matches_if(n);
! 532:
! 533: return(retval);
1.1 millert 534: }
535:
536: /*
1.4 millert 537: * Returns 0 if the hostname matches the pattern and non-zero otherwise.
538: */
539: int
540: hostname_matches(shost, lhost, pattern)
541: char *shost;
542: char *lhost;
543: char *pattern;
544: {
545: if (has_meta(pattern)) {
546: if (strchr(pattern, '.'))
547: return(fnmatch(pattern, lhost, FNM_CASEFOLD));
548: else
549: return(fnmatch(pattern, shost, FNM_CASEFOLD));
550: } else {
551: if (strchr(pattern, '.'))
552: return(strcasecmp(lhost, pattern));
553: else
554: return(strcasecmp(shost, pattern));
555: }
556: }
557:
558: /*
1.12 millert 559: * Returns TRUE if the user/uid from sudoers matches the specified user/uid,
560: * else returns FALSE.
561: */
562: int
563: userpw_matches(sudoers_user, user, pw)
564: char *sudoers_user;
565: char *user;
566: struct passwd *pw;
567: {
568: if (pw != NULL && *sudoers_user == '#') {
569: uid_t uid = atoi(sudoers_user + 1);
570: if (uid == pw->pw_uid)
571: return(1);
572: }
573: return(strcmp(sudoers_user, user) == 0);
574: }
575:
576: /*
1.1 millert 577: * Returns TRUE if the given user belongs to the named group,
578: * else returns FALSE.
1.12 millert 579: * XXX - reduce the number of passwd/group lookups
1.1 millert 580: */
581: int
1.12 millert 582: usergr_matches(group, user, pw)
1.1 millert 583: char *group;
584: char *user;
1.12 millert 585: struct passwd *pw;
1.1 millert 586: {
587: struct group *grp;
1.12 millert 588: gid_t pw_gid;
1.1 millert 589: char **cur;
1.14 millert 590: int i;
1.1 millert 591:
592: /* make sure we have a valid usergroup, sudo style */
593: if (*group++ != '%')
594: return(FALSE);
595:
1.12 millert 596: /* look up user's primary gid in the passwd file */
597: if (pw == NULL && (pw = getpwnam(user)) == NULL)
1.1 millert 598: return(FALSE);
1.12 millert 599: pw_gid = pw->pw_gid;
1.1 millert 600:
1.12 millert 601: if ((grp = getgrnam(group)) == NULL)
1.1 millert 602: return(FALSE);
603:
1.12 millert 604: /* check against user's primary (passwd file) gid */
605: if (grp->gr_gid == pw_gid)
1.1 millert 606: return(TRUE);
607:
1.13 millert 608: /*
609: * If the user has a supplementary group vector, check it first.
610: */
1.14 millert 611: for (i = 0; i < user_ngroups; i++) {
612: if (grp->gr_gid == user_groups[i])
1.1 millert 613: return(TRUE);
614: }
1.13 millert 615: if (grp->gr_mem != NULL) {
616: for (cur = grp->gr_mem; *cur; cur++) {
617: if (strcmp(*cur, user) == 0)
618: return(TRUE);
619: }
620: }
1.1 millert 621:
622: return(FALSE);
623: }
624:
625: /*
626: * Returns TRUE if "host" and "user" belong to the netgroup "netgr",
1.3 millert 627: * else return FALSE. Either of "host", "shost" or "user" may be NULL
1.1 millert 628: * in which case that argument is not checked...
629: */
630: int
1.3 millert 631: netgr_matches(netgr, host, shost, user)
1.1 millert 632: char *netgr;
633: char *host;
1.3 millert 634: char *shost;
1.1 millert 635: char *user;
636: {
1.13 millert 637: static char *domain;
1.1 millert 638: #ifdef HAVE_GETDOMAINNAME
1.13 millert 639: static int initialized;
640: #endif
1.1 millert 641:
642: /* make sure we have a valid netgroup, sudo style */
643: if (*netgr++ != '+')
644: return(FALSE);
645:
646: #ifdef HAVE_GETDOMAINNAME
647: /* get the domain name (if any) */
1.13 millert 648: if (!initialized) {
1.1 millert 649: domain = (char *) emalloc(MAXHOSTNAMELEN);
650: if (getdomainname(domain, MAXHOSTNAMELEN) == -1 || *domain == '\0') {
1.13 millert 651: efree(domain);
1.1 millert 652: domain = NULL;
653: }
1.13 millert 654: initialized = 1;
1.1 millert 655: }
656: #endif /* HAVE_GETDOMAINNAME */
657:
658: #ifdef HAVE_INNETGR
1.3 millert 659: if (innetgr(netgr, host, user, domain))
660: return(TRUE);
661: else if (host != shost && innetgr(netgr, shost, user, domain))
662: return(TRUE);
663: #endif /* HAVE_INNETGR */
664:
1.1 millert 665: return(FALSE);
666: }
667:
668: /*
669: * Returns TRUE if "s" has shell meta characters in it,
670: * else returns FALSE.
671: */
672: static int
673: has_meta(s)
674: char *s;
675: {
1.4 millert 676: char *t;
1.12 millert 677:
1.1 millert 678: for (t = s; *t; t++) {
679: if (*t == '\\' || *t == '?' || *t == '*' || *t == '[' || *t == ']')
680: return(TRUE);
681: }
682: return(FALSE);
683: }