2 * Copyright (c) 1996, 1998-2004 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
16 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
18 * Sponsored in part by the Defense Advanced Research Projects
19 * Agency (DARPA) and Air Force Research Laboratory, Air Force
20 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
25 #include <sys/types.h>
26 #include <sys/param.h>
36 #endif /* STDC_HEADERS */
40 # ifdef HAVE_STRINGS_H
43 #endif /* HAVE_STRING_H */
46 #endif /* HAVE_UNISTD_H */
49 #endif /* HAVE_FNMATCH */
50 #ifdef HAVE_NETGROUP_H
51 # include <netgroup.h>
52 #endif /* HAVE_NETGROUP_H */
56 #include <netinet/in.h>
57 #include <arpa/inet.h>
61 # define NAMLEN(dirent) strlen((dirent)->d_name)
63 # define dirent direct
64 # define NAMLEN(dirent) (dirent)->d_namlen
65 # ifdef HAVE_SYS_NDIR_H
66 # include <sys/ndir.h>
68 # ifdef HAVE_SYS_DIR_H
78 #include "interfaces.h"
81 # include "emul/fnmatch.h"
82 #endif /* HAVE_FNMATCH */
85 static const char rcsid[] = "$Sudo: parse.c,v 1.161 2004/08/24 18:01:13 millert Exp $";
91 int parse_error = FALSE;
93 extern FILE *yyin, *yyout;
98 static int has_meta __P((char *));
99 void init_parser __P((void));
102 * Look up the user in the sudoers file and check to see if they are
103 * allowed to run the specified command on this host as the target user.
106 sudoers_lookup(pwflag)
110 enum def_tupple pwcheck;
112 /* We opened _PATH_SUDOERS in check_sudoers() so just rewind it. */
117 /* Allocate space for data structures in the parser. */
120 /* If pwcheck *could* be "all" or "any", keep more state. */
124 /* Need to be runas user while stat'ing things in the parser. */
125 set_perms(PERM_RUNAS);
128 /* Close the sudoers file now that we are done with it. */
129 (void) fclose(sudoers_fp);
132 if (error || parse_error) {
133 set_perms(PERM_ROOT);
134 return(VALIDATE_ERROR);
138 * The pw options may have changed during sudoers parse so we
139 * wait until now to set this.
142 pwcheck = (pwflag == -1) ? never : sudo_defs_table[pwflag].sd_un.tuple;
147 * Assume the worst. If the stack is empty the user was
148 * not mentioned at all.
150 if (def_authenticate)
151 error = VALIDATE_NOT_OK;
153 error = VALIDATE_NOT_OK | FLAG_NOPASS;
155 SET(error, FLAG_NO_CHECK);
157 SET(error, FLAG_NO_HOST);
159 SET(error, FLAG_NO_USER);
163 * Only check the actual command if pwflag is not set.
164 * It is set for the "validate", "list" and "kill" pseudo-commands.
165 * Always check the host and user.
171 if (pwcheck == always && def_authenticate)
172 nopass = FLAG_CHECK_USER;
173 else if (pwcheck == never || !def_authenticate)
174 nopass = FLAG_NOPASS;
177 if (host_matches == TRUE) {
179 if (pwcheck == any && no_passwd == TRUE)
180 nopass = FLAG_NOPASS;
181 else if (pwcheck == all && nopass != 0)
182 nopass = (no_passwd == TRUE) ? FLAG_NOPASS : 0;
187 set_perms(PERM_ROOT);
190 return(VALIDATE_OK | nopass);
194 if (host_matches == TRUE) {
195 CLR(error, FLAG_NO_HOST);
196 if (runas_matches == TRUE && cmnd_matches == TRUE) {
198 * User was granted access to cmnd on host as user.
200 set_perms(PERM_ROOT);
202 (no_passwd == TRUE ? FLAG_NOPASS : 0) |
203 (no_execve == TRUE ? FLAG_NOEXEC : 0));
204 } else if ((runas_matches == TRUE && cmnd_matches == FALSE) ||
205 (runas_matches == FALSE && cmnd_matches == TRUE)) {
207 * User was explicitly denied access to cmnd on host.
209 set_perms(PERM_ROOT);
210 return(VALIDATE_NOT_OK |
211 (no_passwd == TRUE ? FLAG_NOPASS : 0) |
212 (no_execve == TRUE ? FLAG_NOEXEC : 0));
218 set_perms(PERM_ROOT);
221 * The user was neither explicitly granted nor denied access.
225 return(error | nopass);
229 * If path doesn't end in /, return TRUE iff cmnd & path name the same inode;
230 * otherwise, return TRUE if user_cmnd names one of the inodes in path.
233 command_matches(sudoers_cmnd, sudoers_args)
237 struct stat sudoers_stat;
242 /* Check for pseudo-commands */
243 if (strchr(user_cmnd, '/') == NULL) {
245 * Return true if both sudoers_cmnd and user_cmnd are "sudoedit" AND
246 * a) there are no args in sudoers OR
247 * b) there are no args on command line and none req by sudoers OR
248 * c) there are args in sudoers and on command line and they match
250 if (strcmp(sudoers_cmnd, "sudoedit") != 0 ||
251 strcmp(user_cmnd, "sudoedit") != 0)
254 (!user_args && sudoers_args && !strcmp("\"\"", sudoers_args)) ||
256 fnmatch(sudoers_args, user_args ? user_args : "", 0) == 0)) {
259 safe_cmnd = estrdup(sudoers_cmnd);
266 * If sudoers_cmnd has meta characters in it, use fnmatch(3)
267 * to do the matching.
269 if (has_meta(sudoers_cmnd)) {
271 * Return true if fnmatch(3) succeeds AND
272 * a) there are no args in sudoers OR
273 * b) there are no args on command line and none required by sudoers OR
274 * c) there are args in sudoers and on command line and they match
277 if (fnmatch(sudoers_cmnd, user_cmnd, FNM_PATHNAME) != 0)
280 (!user_args && sudoers_args && !strcmp("\"\"", sudoers_args)) ||
282 fnmatch(sudoers_args, user_args ? user_args : "", 0) == 0)) {
285 safe_cmnd = estrdup(user_cmnd);
290 size_t dlen = strlen(sudoers_cmnd);
294 * Check to make sure this is not a directory spec (doesn't end in '/')
296 if (sudoers_cmnd[dlen - 1] != '/') {
299 /* Only proceed if user_base and basename(sudoers_cmnd) match */
300 if ((base = strrchr(sudoers_cmnd, '/')) == NULL)
304 if (strcmp(user_base, base) != 0 ||
305 stat(sudoers_cmnd, &sudoers_stat) == -1)
309 * Return true if inode/device matches AND
310 * a) there are no args in sudoers OR
311 * b) there are no args on command line and none req by sudoers OR
312 * c) there are args in sudoers and on command line and they match
314 if (user_stat->st_dev != sudoers_stat.st_dev ||
315 user_stat->st_ino != sudoers_stat.st_ino)
318 (!user_args && sudoers_args && !strcmp("\"\"", sudoers_args)) ||
320 fnmatch(sudoers_args, user_args ? user_args : "", 0) == 0)) {
323 safe_cmnd = estrdup(sudoers_cmnd);
330 * Grot through sudoers_cmnd's directory entries, looking for user_base.
332 dirp = opendir(sudoers_cmnd);
336 if (strlcpy(buf, sudoers_cmnd, sizeof(buf)) >= sizeof(buf))
338 while ((dent = readdir(dirp)) != NULL) {
339 /* ignore paths > PATH_MAX (XXX - log) */
341 if (strlcat(buf, dent->d_name, sizeof(buf)) >= sizeof(buf))
344 /* only stat if basenames are the same */
345 if (strcmp(user_base, dent->d_name) != 0 ||
346 stat(buf, &sudoers_stat) == -1)
348 if (user_stat->st_dev == sudoers_stat.st_dev &&
349 user_stat->st_ino == sudoers_stat.st_ino) {
352 safe_cmnd = estrdup(buf);
358 return(dent != NULL);
363 * Returns TRUE if "n" is one of our ip addresses or if
364 * "n" is a network that we are on, else returns FALSE.
372 struct in_addr addr, mask;
374 /* If there's an explicit netmask, use it. */
375 if ((m = strchr(n, '/'))) {
377 addr.s_addr = inet_addr(n);
379 mask.s_addr = inet_addr(m);
382 mask.s_addr = 0xffffffff;
385 mask.s_addr = htonl(mask.s_addr);
389 for (i = 0; i < num_interfaces; i++)
390 if ((interfaces[i].addr.s_addr & mask.s_addr) == addr.s_addr)
393 addr.s_addr = inet_addr(n);
395 for (i = 0; i < num_interfaces; i++)
396 if (interfaces[i].addr.s_addr == addr.s_addr ||
397 (interfaces[i].addr.s_addr & interfaces[i].netmask.s_addr)
406 * Returns 0 if the hostname matches the pattern and non-zero otherwise.
409 hostname_matches(shost, lhost, pattern)
414 if (has_meta(pattern)) {
415 if (strchr(pattern, '.'))
416 return(fnmatch(pattern, lhost, FNM_CASEFOLD));
418 return(fnmatch(pattern, shost, FNM_CASEFOLD));
420 if (strchr(pattern, '.'))
421 return(strcasecmp(lhost, pattern));
423 return(strcasecmp(shost, pattern));
428 * Returns TRUE if the user/uid from sudoers matches the specified user/uid,
429 * else returns FALSE.
432 userpw_matches(sudoers_user, user, pw)
437 if (pw != NULL && *sudoers_user == '#') {
438 uid_t uid = atoi(sudoers_user + 1);
439 if (uid == pw->pw_uid)
442 return(strcmp(sudoers_user, user) == 0);
446 * Returns TRUE if the given user belongs to the named group,
447 * else returns FALSE.
448 * XXX - reduce the number of passwd/group lookups
451 usergr_matches(group, user, pw)
460 /* make sure we have a valid usergroup, sudo style */
464 /* look up user's primary gid in the passwd file */
465 if (pw == NULL && (pw = getpwnam(user)) == NULL)
469 if ((grp = getgrnam(group)) == NULL)
472 /* check against user's primary (passwd file) gid */
473 if (grp->gr_gid == pw_gid)
476 /* check to see if user is explicitly listed in the group */
477 for (cur = grp->gr_mem; *cur; cur++) {
478 if (strcmp(*cur, user) == 0)
486 * Returns TRUE if "host" and "user" belong to the netgroup "netgr",
487 * else return FALSE. Either of "host", "shost" or "user" may be NULL
488 * in which case that argument is not checked...
491 netgr_matches(netgr, host, shost, user)
497 #ifdef HAVE_GETDOMAINNAME
498 static char *domain = (char *) -1;
500 static char *domain = NULL;
501 #endif /* HAVE_GETDOMAINNAME */
503 /* make sure we have a valid netgroup, sudo style */
507 #ifdef HAVE_GETDOMAINNAME
508 /* get the domain name (if any) */
509 if (domain == (char *) -1) {
510 domain = (char *) emalloc(MAXHOSTNAMELEN);
511 if (getdomainname(domain, MAXHOSTNAMELEN) == -1 || *domain == '\0') {
516 #endif /* HAVE_GETDOMAINNAME */
519 if (innetgr(netgr, host, user, domain))
521 else if (host != shost && innetgr(netgr, shost, user, domain))
523 #endif /* HAVE_INNETGR */
529 * Returns TRUE if "s" has shell meta characters in it,
530 * else returns FALSE.
538 for (t = s; *t; t++) {
539 if (*t == '\\' || *t == '?' || *t == '*' || *t == '[' || *t == ']')