2 * Copyright (c) 1996, 1998-2002 Todd C. Miller <Todd.Miller@courtesan.com>
5 * This code is derived from software contributed by Chris Jepeway.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * 4. Products derived from this software may not be called "Sudo" nor
22 * may "Sudo" appear in their names without specific prior written
23 * permission from the author.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
28 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
31 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
33 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <sys/types.h>
40 #include <sys/param.h>
50 #endif /* STDC_HEADERS */
54 # ifdef HAVE_STRINGS_H
57 #endif /* HAVE_STRING_H */
60 #endif /* HAVE_UNISTD_H */
63 #endif /* HAVE_FNMATCH_H */
64 #ifdef HAVE_NETGROUP_H
65 # include <netgroup.h>
66 #endif /* HAVE_NETGROUP_H */
70 #include <netinet/in.h>
71 #include <arpa/inet.h>
75 # define NAMLEN(dirent) strlen((dirent)->d_name)
77 # define dirent direct
78 # define NAMLEN(dirent) (dirent)->d_namlen
79 # ifdef HAVE_SYS_NDIR_H
80 # include <sys/ndir.h>
82 # ifdef HAVE_SYS_DIR_H
92 #include "interfaces.h"
95 # include "emul/fnmatch.h"
96 #endif /* HAVE_FNMATCH */
99 static const char rcsid[] = "$Sudo: parse.c,v 1.137 2002/03/16 00:44:47 millert Exp $";
105 int parse_error = FALSE;
107 extern FILE *yyin, *yyout;
112 static int has_meta __P((char *));
113 void init_parser __P((void));
116 * Look up the user in the sudoers file and check to see if they are
117 * allowed to run the specified command on this host as the target user.
120 sudoers_lookup(pwflag)
127 /* Become sudoers file owner */
128 set_perms(PERM_SUDOERS, 0);
130 /* We opened _PATH_SUDOERS in check_sudoers() so just rewind it. */
135 /* Allocate space for data structures in the parser. */
138 /* If pwcheck *could* be PWCHECK_ALL or PWCHECK_ANY, keep more state. */
142 /* Need to be root while stat'ing things in the parser. */
143 set_perms(PERM_ROOT, 0);
146 /* Close the sudoers file now that we are done with it. */
147 (void) fclose(sudoers_fp);
150 if (error || parse_error)
151 return(VALIDATE_ERROR);
154 * The pw options may have changed during sudoers parse so we
155 * wait until now to set this.
158 pwcheck = (pwflag == -1) ? PWCHECK_NEVER : def_ival(pwflag);
163 * Assume the worst. If the stack is empty the user was
164 * not mentioned at all.
166 if (def_flag(I_AUTHENTICATE))
167 error = VALIDATE_NOT_OK;
169 error = VALIDATE_NOT_OK | FLAG_NOPASS;
171 error |= FLAG_NO_CHECK;
173 error |= FLAG_NO_HOST;
175 error |= FLAG_NO_USER;
179 * Only check the actual command if pwcheck flag is not set.
180 * It is set for the "validate", "list" and "kill" pseudo-commands.
181 * Always check the host and user.
187 if (pwcheck == PWCHECK_NEVER || !def_flag(I_AUTHENTICATE))
188 nopass = FLAG_NOPASS;
191 if (host_matches == TRUE) {
193 if (pwcheck == PWCHECK_ANY && no_passwd == TRUE)
194 nopass = FLAG_NOPASS;
195 else if (pwcheck == PWCHECK_ALL && nopass != 0)
196 nopass = (no_passwd == TRUE) ? FLAG_NOPASS : 0;
203 return(VALIDATE_OK | nopass);
207 if (host_matches == TRUE) {
208 error &= ~FLAG_NO_HOST;
209 if (runas_matches == TRUE) {
210 if (cmnd_matches == TRUE) {
212 * User was granted access to cmnd on host.
213 * If no passwd required return as such.
215 if (no_passwd == TRUE)
216 return(VALIDATE_OK | FLAG_NOPASS);
219 } else if (cmnd_matches == FALSE) {
221 * User was explicitly denied access to cmnd on host.
223 if (no_passwd == TRUE)
224 return(VALIDATE_NOT_OK | FLAG_NOPASS);
226 return(VALIDATE_NOT_OK);
235 * The user was not explicitly granted nor denied access.
239 return(error | nopass);
243 * If path doesn't end in /, return TRUE iff cmnd & path name the same inode;
244 * otherwise, return TRUE if cmnd names one of the inodes in path.
247 command_matches(cmnd, cmnd_args, path, sudoers_args)
254 static struct stat cst;
258 char buf[MAXPATHLEN];
259 static char *cmnd_base;
261 /* Don't bother with pseudo commands like "validate" */
262 if (strchr(cmnd, '/') == NULL)
267 /* Only need to stat cmnd once since it never changes */
268 if (cst.st_dev == 0) {
269 if (stat(cmnd, &cst) == -1)
271 if ((cmnd_base = strrchr(cmnd, '/')) == NULL)
278 * If the pathname has meta characters in it use fnmatch(3)
281 if (has_meta(path)) {
283 * Return true if fnmatch(3) succeeds AND
284 * a) there are no args in sudoers OR
285 * b) there are no args on command line and none required by sudoers OR
286 * c) there are args in sudoers and on command line and they match
289 if (fnmatch(path, cmnd, FNM_PATHNAME) != 0)
292 (!cmnd_args && sudoers_args && !strcmp("\"\"", sudoers_args)) ||
293 (sudoers_args && fnmatch(sudoers_args, cmnd_args ? cmnd_args : "",
297 safe_cmnd = estrdup(user_cmnd);
304 * Check to make sure this is not a directory spec (doesn't end in '/')
306 if (path[plen - 1] != '/') {
309 /* Only proceed if the basenames of cmnd and path are the same */
310 if ((p = strrchr(path, '/')) == NULL)
314 if (strcmp(cmnd_base, p) != 0 || stat(path, &pst) == -1)
318 * Return true if inode/device matches AND
319 * a) there are no args in sudoers OR
320 * b) there are no args on command line and none req by sudoers OR
321 * c) there are args in sudoers and on command line and they match
323 if (cst.st_dev != pst.st_dev || cst.st_ino != pst.st_ino)
326 (!cmnd_args && sudoers_args && !strcmp("\"\"", sudoers_args)) ||
328 fnmatch(sudoers_args, cmnd_args ? cmnd_args : "", 0) == 0)) {
331 safe_cmnd = estrdup(path);
338 * Grot through path's directory entries, looking for cmnd.
340 dirp = opendir(path);
344 while ((dent = readdir(dirp)) != NULL) {
345 /* ignore paths > MAXPATHLEN (XXX - log) */
346 if (plen + NAMLEN(dent) >= sizeof(buf))
349 strcat(buf, dent->d_name);
351 /* only stat if basenames are the same */
352 if (strcmp(cmnd_base, dent->d_name) != 0 || stat(buf, &pst) == -1)
354 if (cst.st_dev == pst.st_dev && cst.st_ino == pst.st_ino) {
357 safe_cmnd = estrdup(buf);
363 return(dent != NULL);
368 * Returns TRUE if "n" is one of our ip addresses or if
369 * "n" is a network that we are on, else returns FALSE.
377 struct in_addr addr, mask;
379 /* If there's an explicit netmask, use it. */
380 if ((m = strchr(n, '/'))) {
382 addr.s_addr = inet_addr(n);
384 mask.s_addr = inet_addr(m);
387 mask.s_addr = 0xffffffff;
390 mask.s_addr = htonl(mask.s_addr);
394 for (i = 0; i < num_interfaces; i++)
395 if ((interfaces[i].addr.s_addr & mask.s_addr) == addr.s_addr)
398 addr.s_addr = inet_addr(n);
400 for (i = 0; i < num_interfaces; i++)
401 if (interfaces[i].addr.s_addr == addr.s_addr ||
402 (interfaces[i].addr.s_addr & interfaces[i].netmask.s_addr)
411 * Returns 0 if the hostname matches the pattern and non-zero otherwise.
414 hostname_matches(shost, lhost, pattern)
419 if (has_meta(pattern)) {
420 if (strchr(pattern, '.'))
421 return(fnmatch(pattern, lhost, FNM_CASEFOLD));
423 return(fnmatch(pattern, shost, FNM_CASEFOLD));
425 if (strchr(pattern, '.'))
426 return(strcasecmp(lhost, pattern));
428 return(strcasecmp(shost, pattern));
433 * Returns TRUE if the given user belongs to the named group,
434 * else returns FALSE.
437 usergr_matches(group, user)
445 /* make sure we have a valid usergroup, sudo style */
449 if ((grp = getgrnam(group)) == NULL)
453 * Check against user's real gid as well as group's user list
455 if ((pw = getpwnam(user)) == NULL)
458 if (grp->gr_gid == pw->pw_gid)
461 for (cur=grp->gr_mem; *cur; cur++) {
462 if (strcmp(*cur, user) == 0)
470 * Returns TRUE if "host" and "user" belong to the netgroup "netgr",
471 * else return FALSE. Either of "host", "shost" or "user" may be NULL
472 * in which case that argument is not checked...
475 netgr_matches(netgr, host, shost, user)
481 #ifdef HAVE_GETDOMAINNAME
482 static char *domain = (char *) -1;
484 static char *domain = NULL;
485 #endif /* HAVE_GETDOMAINNAME */
487 /* make sure we have a valid netgroup, sudo style */
491 #ifdef HAVE_GETDOMAINNAME
492 /* get the domain name (if any) */
493 if (domain == (char *) -1) {
494 domain = (char *) emalloc(MAXHOSTNAMELEN);
495 if (getdomainname(domain, MAXHOSTNAMELEN) == -1 || *domain == '\0') {
500 #endif /* HAVE_GETDOMAINNAME */
503 if (innetgr(netgr, host, user, domain))
505 else if (host != shost && innetgr(netgr, shost, user, domain))
507 #endif /* HAVE_INNETGR */
513 * Returns TRUE if "s" has shell meta characters in it,
514 * else returns FALSE.
522 for (t = s; *t; t++) {
523 if (*t == '\\' || *t == '?' || *t == '*' || *t == '[' || *t == ']')