2 * Copyright (c) 1996, 1998-2005, 2007
3 * Todd C. Miller <Todd.Miller@courtesan.com>
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.
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.
16 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
17 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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.
26 #include <sys/types.h>
27 #include <sys/param.h>
28 #include <sys/socket.h>
38 #endif /* STDC_HEADERS */
42 # ifdef HAVE_STRINGS_H
45 #endif /* HAVE_STRING_H */
48 #endif /* HAVE_UNISTD_H */
51 #endif /* HAVE_FNMATCH */
52 #ifdef HAVE_EXTENDED_GLOB
54 #endif /* HAVE_EXTENDED_GLOB */
55 #ifdef HAVE_NETGROUP_H
56 # include <netgroup.h>
57 #endif /* HAVE_NETGROUP_H */
61 #include <netinet/in.h>
62 #include <arpa/inet.h>
66 # define NAMLEN(dirent) strlen((dirent)->d_name)
68 # define dirent direct
69 # define NAMLEN(dirent) (dirent)->d_namlen
70 # ifdef HAVE_SYS_NDIR_H
71 # include <sys/ndir.h>
73 # ifdef HAVE_SYS_DIR_H
83 #include "interfaces.h"
86 # include "emul/fnmatch.h"
87 #endif /* HAVE_FNMATCH */
88 #ifndef HAVE_EXTENDED_GLOB
89 # include "emul/glob.h"
90 #endif /* HAVE_EXTENDED_GLOB */
93 __unused static const char rcsid[] = "$Sudo: parse.c,v 1.160.2.16 2008/02/09 14:44:48 millert Exp $";
99 int parse_error = FALSE;
101 extern FILE *yyin, *yyout;
106 static int has_meta __P((char *));
107 void init_parser __P((void));
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.
114 sudoers_lookup(pwflag)
119 /* We opened _PATH_SUDOERS in check_sudoers() so just rewind it. */
124 /* Allocate space for data structures in the parser. */
127 /* Keep more state for pseudo-commands so that listpw and verifypw work */
131 /* Need to be runas user while stat'ing things in the parser. */
132 set_perms(PERM_RUNAS);
135 /* Close the sudoers file now that we are done with it. */
136 (void) fclose(sudoers_fp);
139 if (error || parse_error) {
140 set_perms(PERM_ROOT);
141 return(VALIDATE_ERROR);
145 * Assume the worst. If the stack is empty the user was
146 * not mentioned at all.
148 if (def_authenticate)
149 error = VALIDATE_NOT_OK;
151 error = VALIDATE_NOT_OK | FLAG_NOPASS;
153 SET(error, FLAG_NO_CHECK);
155 SET(error, FLAG_NO_HOST);
157 SET(error, FLAG_NO_USER);
161 * Only check the actual command if pwflag is not set.
162 * It is set for the "validate", "list" and "kill" pseudo-commands.
163 * Always check the host and user.
168 enum def_tupple pwcheck;
170 pwcheck = (pwflag == -1) ? never : sudo_defs_table[pwflag].sd_un.tuple;
172 if (pwcheck == always && def_authenticate)
173 nopass = FLAG_CHECK_USER;
174 else if (pwcheck == never || !def_authenticate)
175 nopass = FLAG_NOPASS;
178 if (host_matches == TRUE) {
180 if (pwcheck == any && no_passwd == TRUE)
181 nopass = FLAG_NOPASS;
182 else if (pwcheck == all && nopass != 0)
183 nopass = (no_passwd == TRUE) ? FLAG_NOPASS : 0;
188 set_perms(PERM_ROOT);
191 return(VALIDATE_OK | nopass);
195 if (host_matches == TRUE) {
196 CLR(error, FLAG_NO_HOST);
197 if (runas_matches == TRUE && cmnd_matches == TRUE) {
199 * User was granted access to cmnd on host as user.
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;
207 user_role = def_role;
209 if (user_type == NULL) {
210 if (match[top-1].type != NULL)
211 user_type = match[top-1].type;
213 user_type = def_type;
216 set_perms(PERM_ROOT);
218 (no_passwd == TRUE ? FLAG_NOPASS : 0) |
219 (no_execve == TRUE ? FLAG_NOEXEC : 0) |
220 (setenv_ok >= TRUE ? FLAG_SETENV : 0));
221 } else if ((runas_matches == TRUE && cmnd_matches == FALSE) ||
222 (runas_matches == FALSE && cmnd_matches == TRUE)) {
224 * User was explicitly denied access to cmnd on host.
226 set_perms(PERM_ROOT);
227 return(VALIDATE_NOT_OK |
228 (no_passwd == TRUE ? FLAG_NOPASS : 0) |
229 (no_execve == TRUE ? FLAG_NOEXEC : 0) |
230 (setenv_ok >= TRUE ? FLAG_SETENV : 0));
236 set_perms(PERM_ROOT);
239 * The user was neither explicitly granted nor denied access.
243 return(error | nopass);
247 * If path doesn't end in /, return TRUE iff cmnd & path name the same inode;
248 * otherwise, return TRUE if user_cmnd names one of the inodes in path.
251 command_matches(sudoers_cmnd, sudoers_args)
255 struct stat sudoers_stat;
257 char **ap, *base, buf[PATH_MAX];
261 /* Check for pseudo-commands */
262 if (strchr(user_cmnd, '/') == NULL) {
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
269 if (strcmp(sudoers_cmnd, "sudoedit") != 0 ||
270 strcmp(user_cmnd, "sudoedit") != 0)
273 (!user_args && sudoers_args && !strcmp("\"\"", sudoers_args)) ||
275 fnmatch(sudoers_args, user_args ? user_args : "", 0) == 0)) {
277 safe_cmnd = estrdup(sudoers_cmnd);
284 * If sudoers_cmnd has meta characters in it, use fnmatch(3)
285 * to do the matching.
287 if (has_meta(sudoers_cmnd)) {
289 * Return true if we find a match in the glob(3) results AND
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
295 * Could optimize patterns ending in "/*" to "/user_base"
297 #define GLOB_FLAGS (GLOB_NOSORT | GLOB_MARK | GLOB_BRACE | GLOB_TILDE)
298 if (glob(sudoers_cmnd, GLOB_FLAGS, NULL, &gl) != 0) {
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)
309 if (strcmp(user_base, base) != 0 ||
310 stat(*ap, &sudoers_stat) == -1)
312 if (user_stat->st_dev == sudoers_stat.st_dev &&
313 user_stat->st_ino == sudoers_stat.st_ino) {
315 safe_cmnd = estrdup(*ap);
324 (!user_args && sudoers_args && !strcmp("\"\"", sudoers_args)) ||
326 fnmatch(sudoers_args, user_args ? user_args : "", 0) == 0)) {
328 safe_cmnd = estrdup(user_cmnd);
333 size_t dlen = strlen(sudoers_cmnd);
337 * Check to make sure this is not a directory spec (doesn't end in '/')
339 if (sudoers_cmnd[dlen - 1] != '/') {
340 /* Only proceed if user_base and basename(sudoers_cmnd) match */
341 if ((base = strrchr(sudoers_cmnd, '/')) == NULL)
345 if (strcmp(user_base, base) != 0 ||
346 stat(sudoers_cmnd, &sudoers_stat) == -1)
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
355 if (user_stat->st_dev != sudoers_stat.st_dev ||
356 user_stat->st_ino != sudoers_stat.st_ino)
359 (!user_args && sudoers_args && !strcmp("\"\"", sudoers_args)) ||
361 fnmatch(sudoers_args, user_args ? user_args : "", 0) == 0)) {
363 safe_cmnd = estrdup(sudoers_cmnd);
370 * Grot through sudoers_cmnd's directory entries, looking for user_base.
372 dirp = opendir(sudoers_cmnd);
376 if (strlcpy(buf, sudoers_cmnd, sizeof(buf)) >= sizeof(buf))
378 while ((dent = readdir(dirp)) != NULL) {
379 /* ignore paths > PATH_MAX (XXX - log) */
381 if (strlcat(buf, dent->d_name, sizeof(buf)) >= sizeof(buf))
384 /* only stat if basenames are the same */
385 if (strcmp(user_base, dent->d_name) != 0 ||
386 stat(buf, &sudoers_stat) == -1)
388 if (user_stat->st_dev == sudoers_stat.st_dev &&
389 user_stat->st_ino == sudoers_stat.st_ino) {
391 safe_cmnd = estrdup(buf);
397 return(dent != NULL);
407 struct interface *ifp;
409 struct in6_addr addr6;
415 if (inet_pton(AF_INET6, n, &addr6) > 0) {
421 addr.s_addr = inet_addr(n);
424 for (i = 0; i < num_interfaces; i++) {
425 ifp = &interfaces[i];
426 if (ifp->family != family)
430 if (ifp->addr.ip4.s_addr == addr.s_addr ||
431 (ifp->addr.ip4.s_addr & ifp->netmask.ip4.s_addr)
437 if (memcmp(ifp->addr.ip6.s6_addr, addr6.s6_addr,
438 sizeof(addr6.s6_addr)) == 0)
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])
444 if (j == sizeof(addr6.s6_addr))
446 #endif /* HAVE_IN6_ADDR */
454 addr_matches_if_netmask(n, m)
459 struct in_addr addr, mask;
460 struct interface *ifp;
462 struct in6_addr addr6, mask6;
468 if (inet_pton(AF_INET6, n, &addr6) > 0)
474 addr.s_addr = inet_addr(n);
477 if (family == AF_INET) {
479 mask.s_addr = inet_addr(m);
482 mask.s_addr = 0xffffffff;
485 mask.s_addr = htonl(mask.s_addr);
490 if (inet_pton(AF_INET6, m, &mask6) <= 0) {
492 for (i = 0; i < 16; i++) {
494 mask6.s6_addr[i] = 0;
495 else if (i * 8 + 8 <= j)
496 mask6.s6_addr[i] = 0xff;
498 mask6.s6_addr[i] = 0xff00 >> (j - i * 8);
502 #endif /* HAVE_IN6_ADDR */
504 for (i = 0; i < num_interfaces; i++) {
505 ifp = &interfaces[i];
506 if (ifp->family != family)
510 if ((ifp->addr.ip4.s_addr & mask.s_addr) == addr.s_addr)
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])
518 if (j == sizeof(addr6.s6_addr))
520 #endif /* HAVE_IN6_ADDR */
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.
538 /* If there's an explicit netmask, use it. */
539 if ((m = strchr(n, '/'))) {
541 retval = addr_matches_if_netmask(n, m);
544 retval = addr_matches_if(n);
550 * Returns 0 if the hostname matches the pattern and non-zero otherwise.
553 hostname_matches(shost, lhost, pattern)
558 if (has_meta(pattern)) {
559 if (strchr(pattern, '.'))
560 return(fnmatch(pattern, lhost, FNM_CASEFOLD));
562 return(fnmatch(pattern, shost, FNM_CASEFOLD));
564 if (strchr(pattern, '.'))
565 return(strcasecmp(lhost, pattern));
567 return(strcasecmp(shost, pattern));
572 * Returns TRUE if the user/uid from sudoers matches the specified user/uid,
573 * else returns FALSE.
576 userpw_matches(sudoers_user, user, pw)
581 if (pw != NULL && *sudoers_user == '#') {
582 uid_t uid = atoi(sudoers_user + 1);
583 if (uid == pw->pw_uid)
586 return(strcmp(sudoers_user, user) == 0);
590 * Returns TRUE if the given user belongs to the named group,
591 * else returns FALSE.
592 * XXX - reduce the number of passwd/group lookups
595 usergr_matches(group, user, pw)
605 /* make sure we have a valid usergroup, sudo style */
609 /* look up user's primary gid in the passwd file */
610 if (pw == NULL && (pw = getpwnam(user)) == NULL)
614 if ((grp = getgrnam(group)) == NULL)
617 /* check against user's primary (passwd file) gid */
618 if (grp->gr_gid == pw_gid)
622 * If the user has a supplementary group vector, check it first.
624 for (i = 0; i < user_ngroups; i++) {
625 if (grp->gr_gid == user_groups[i])
628 if (grp->gr_mem != NULL) {
629 for (cur = grp->gr_mem; *cur; cur++) {
630 if (strcmp(*cur, user) == 0)
639 * Returns TRUE if "host" and "user" belong to the netgroup "netgr",
640 * else return FALSE. Either of "host", "shost" or "user" may be NULL
641 * in which case that argument is not checked...
644 netgr_matches(netgr, host, shost, user)
651 #ifdef HAVE_GETDOMAINNAME
652 static int initialized;
655 /* make sure we have a valid netgroup, sudo style */
659 #ifdef HAVE_GETDOMAINNAME
660 /* get the domain name (if any) */
662 domain = (char *) emalloc(MAXHOSTNAMELEN);
663 if (getdomainname(domain, MAXHOSTNAMELEN) == -1 || *domain == '\0') {
669 #endif /* HAVE_GETDOMAINNAME */
672 if (innetgr(netgr, host, user, domain))
674 else if (host != shost && innetgr(netgr, shost, user, domain))
676 #endif /* HAVE_INNETGR */
682 * Returns TRUE if "s" has shell meta characters in it,
683 * else returns FALSE.
691 for (t = s; *t; t++) {
692 if (*t == '\\' || *t == '?' || *t == '*' || *t == '[' || *t == ']')