2 * Copyright (c) 1996, 1998-2005, 2007-2012
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>
37 #endif /* STDC_HEADERS */
40 #endif /* HAVE_STRING_H */
43 #endif /* HAVE_STRINGS_H */
46 #endif /* HAVE_UNISTD_H */
49 #endif /* HAVE_FNMATCH */
50 #ifdef HAVE_EXTENDED_GLOB
52 #endif /* HAVE_EXTENDED_GLOB */
53 #ifdef HAVE_NETGROUP_H
54 # include <netgroup.h>
55 #endif /* HAVE_NETGROUP_H */
62 # define NAMLEN(dirent) strlen((dirent)->d_name)
64 # define dirent direct
65 # define NAMLEN(dirent) (dirent)->d_namlen
66 # ifdef HAVE_SYS_NDIR_H
67 # include <sys/ndir.h>
69 # ifdef HAVE_SYS_DIR_H
82 # include "compat/fnmatch.h"
83 #endif /* HAVE_FNMATCH */
84 #ifndef HAVE_EXTENDED_GLOB
85 # include "compat/glob.h"
86 #endif /* HAVE_EXTENDED_GLOB */
88 static struct member_list empty;
90 static bool command_matches_dir(char *, size_t);
91 static bool command_matches_glob(char *, char *);
92 static bool command_matches_fnmatch(char *, char *);
93 static bool command_matches_normal(char *, char *);
96 * Returns true if string 's' contains meta characters.
98 #define has_meta(s) (strpbrk(s, "\\?*[]") != NULL)
101 * Check for user described by pw in a list of members.
102 * Returns ALLOW, DENY or UNSPEC.
105 _userlist_matches(struct passwd *pw, struct member_list *list)
109 int rval, matched = UNSPEC;
110 debug_decl(_userlist_matches, SUDO_DEBUG_MATCH)
112 tq_foreach_rev(list, m) {
115 matched = !m->negated;
118 if (netgr_matches(m->name, NULL, NULL, pw->pw_name))
119 matched = !m->negated;
122 if (usergr_matches(m->name, pw->pw_name, pw))
123 matched = !m->negated;
126 if ((a = alias_find(m->name, USERALIAS)) != NULL) {
127 rval = _userlist_matches(pw, &a->members);
129 matched = m->negated ? !rval : rval;
134 if (userpw_matches(m->name, pw->pw_name, pw))
135 matched = !m->negated;
138 if (matched != UNSPEC)
141 debug_return_bool(matched);
145 userlist_matches(struct passwd *pw, struct member_list *list)
148 return _userlist_matches(pw, list);
152 * Check for user described by pw in a list of members.
153 * If both lists are empty compare against def_runas_default.
154 * Returns ALLOW, DENY or UNSPEC.
157 _runaslist_matches(struct member_list *user_list, struct member_list *group_list)
162 int user_matched = UNSPEC;
163 int group_matched = UNSPEC;
164 debug_decl(_runaslist_matches, SUDO_DEBUG_MATCH)
166 if (runas_pw != NULL) {
167 /* If no runas user or runas group listed in sudoers, use default. */
168 if (tq_empty(user_list) && tq_empty(group_list))
169 debug_return_int(userpw_matches(def_runas_default, runas_pw->pw_name, runas_pw));
171 tq_foreach_rev(user_list, m) {
174 user_matched = !m->negated;
177 if (netgr_matches(m->name, NULL, NULL, runas_pw->pw_name))
178 user_matched = !m->negated;
181 if (usergr_matches(m->name, runas_pw->pw_name, runas_pw))
182 user_matched = !m->negated;
185 if ((a = alias_find(m->name, RUNASALIAS)) != NULL) {
186 rval = _runaslist_matches(&a->members, &empty);
188 user_matched = m->negated ? !rval : rval;
193 if (userpw_matches(m->name, runas_pw->pw_name, runas_pw))
194 user_matched = !m->negated;
197 if (user_matched != UNSPEC)
202 if (runas_gr != NULL) {
203 if (user_matched == UNSPEC) {
204 if (runas_pw == NULL || strcmp(runas_pw->pw_name, user_name) == 0)
205 user_matched = ALLOW; /* only changing group */
207 tq_foreach_rev(group_list, m) {
210 group_matched = !m->negated;
213 if ((a = alias_find(m->name, RUNASALIAS)) != NULL) {
214 rval = _runaslist_matches(&empty, &a->members);
216 group_matched = m->negated ? !rval : rval;
221 if (group_matches(m->name, runas_gr))
222 group_matched = !m->negated;
225 if (group_matched != UNSPEC)
228 if (group_matched == UNSPEC) {
229 if (runas_pw != NULL && runas_pw->pw_gid == runas_gr->gr_gid)
230 group_matched = ALLOW; /* runas group matches passwd db */
234 if (user_matched == DENY || group_matched == DENY)
235 debug_return_int(DENY);
236 if (user_matched == group_matched || runas_gr == NULL)
237 debug_return_int(user_matched);
238 debug_return_int(UNSPEC);
242 runaslist_matches(struct member_list *user_list, struct member_list *group_list)
245 return _runaslist_matches(user_list ? user_list : &empty,
246 group_list ? group_list : &empty);
250 * Check for host and shost in a list of members.
251 * Returns ALLOW, DENY or UNSPEC.
254 _hostlist_matches(struct member_list *list)
258 int rval, matched = UNSPEC;
259 debug_decl(_hostlist_matches, SUDO_DEBUG_MATCH)
261 tq_foreach_rev(list, m) {
264 matched = !m->negated;
267 if (netgr_matches(m->name, user_host, user_shost, NULL))
268 matched = !m->negated;
271 if (addr_matches(m->name))
272 matched = !m->negated;
275 if ((a = alias_find(m->name, HOSTALIAS)) != NULL) {
276 rval = _hostlist_matches(&a->members);
278 matched = m->negated ? !rval : rval;
283 if (hostname_matches(user_shost, user_host, m->name))
284 matched = !m->negated;
287 if (matched != UNSPEC)
290 debug_return_bool(matched);
294 hostlist_matches(struct member_list *list)
297 return _hostlist_matches(list);
301 * Check for cmnd and args in a list of members.
302 * Returns ALLOW, DENY or UNSPEC.
305 _cmndlist_matches(struct member_list *list)
308 int matched = UNSPEC;
309 debug_decl(_cmndlist_matches, SUDO_DEBUG_MATCH)
311 tq_foreach_rev(list, m) {
312 matched = cmnd_matches(m);
313 if (matched != UNSPEC)
316 debug_return_bool(matched);
320 cmndlist_matches(struct member_list *list)
323 return _cmndlist_matches(list);
327 * Check cmnd and args.
328 * Returns ALLOW, DENY or UNSPEC.
331 cmnd_matches(struct member *m)
334 struct sudo_command *c;
335 int rval, matched = UNSPEC;
336 debug_decl(cmnd_matches, SUDO_DEBUG_MATCH)
340 matched = !m->negated;
344 if ((a = alias_find(m->name, CMNDALIAS)) != NULL) {
345 rval = _cmndlist_matches(&a->members);
347 matched = m->negated ? !rval : rval;
351 c = (struct sudo_command *)m->name;
352 if (command_matches(c->cmnd, c->args))
353 matched = !m->negated;
356 debug_return_bool(matched);
360 command_args_match(sudoers_cmnd, sudoers_args)
365 debug_decl(command_args_match, SUDO_DEBUG_MATCH)
368 * If no args specified in sudoers, any user args are allowed.
369 * If the empty string is specified in sudoers, no user args are allowed.
372 (!user_args && sudoers_args && !strcmp("\"\"", sudoers_args)))
373 debug_return_bool(true);
375 * If args are specified in sudoers, they must match the user args.
376 * If running as sudoedit, all args are assumed to be paths.
379 /* For sudoedit, all args are assumed to be pathnames. */
380 if (strcmp(sudoers_cmnd, "sudoedit") == 0)
381 flags = FNM_PATHNAME;
382 if (fnmatch(sudoers_args, user_args ? user_args : "", flags) == 0)
383 debug_return_bool(true);
385 debug_return_bool(false);
389 * If path doesn't end in /, return true iff cmnd & path name the same inode;
390 * otherwise, return true if user_cmnd names one of the inodes in path.
393 command_matches(char *sudoers_cmnd, char *sudoers_args)
395 debug_decl(command_matches, SUDO_DEBUG_MATCH)
397 /* Check for pseudo-commands */
398 if (sudoers_cmnd[0] != '/') {
400 * Return true if both sudoers_cmnd and user_cmnd are "sudoedit" AND
401 * a) there are no args in sudoers OR
402 * b) there are no args on command line and none req by sudoers OR
403 * c) there are args in sudoers and on command line and they match
405 if (strcmp(sudoers_cmnd, "sudoedit") != 0 ||
406 strcmp(user_cmnd, "sudoedit") != 0)
407 debug_return_bool(false);
408 if (command_args_match(sudoers_cmnd, sudoers_args)) {
410 safe_cmnd = estrdup(sudoers_cmnd);
411 debug_return_bool(true);
413 debug_return_bool(false);
416 if (has_meta(sudoers_cmnd)) {
418 * If sudoers_cmnd has meta characters in it, we need to
419 * use glob(3) and/or fnmatch(3) to do the matching.
422 debug_return_bool(command_matches_fnmatch(sudoers_cmnd, sudoers_args));
423 debug_return_bool(command_matches_glob(sudoers_cmnd, sudoers_args));
425 debug_return_bool(command_matches_normal(sudoers_cmnd, sudoers_args));
429 command_matches_fnmatch(char *sudoers_cmnd, char *sudoers_args)
431 debug_decl(command_matches_fnmatch, SUDO_DEBUG_MATCH)
434 * Return true if fnmatch(3) succeeds AND
435 * a) there are no args in sudoers OR
436 * b) there are no args on command line and none required by sudoers OR
437 * c) there are args in sudoers and on command line and they match
440 if (fnmatch(sudoers_cmnd, user_cmnd, FNM_PATHNAME) != 0)
441 debug_return_bool(false);
442 if (command_args_match(sudoers_cmnd, sudoers_args)) {
445 safe_cmnd = estrdup(user_cmnd);
446 debug_return_bool(true);
448 debug_return_bool(false);
452 command_matches_glob(char *sudoers_cmnd, char *sudoers_args)
454 struct stat sudoers_stat;
456 char **ap, *base, *cp;
458 debug_decl(command_matches_glob, SUDO_DEBUG_MATCH)
461 * First check to see if we can avoid the call to glob(3).
462 * Short circuit if there are no meta chars in the command itself
463 * and user_base and basename(sudoers_cmnd) don't match.
465 dlen = strlen(sudoers_cmnd);
466 if (sudoers_cmnd[dlen - 1] != '/') {
467 if ((base = strrchr(sudoers_cmnd, '/')) != NULL) {
469 if (!has_meta(base) && strcmp(user_base, base) != 0)
470 debug_return_bool(false);
474 * Return true if we find a match in the glob(3) results AND
475 * a) there are no args in sudoers OR
476 * b) there are no args on command line and none required by sudoers OR
477 * c) there are args in sudoers and on command line and they match
480 #define GLOB_FLAGS (GLOB_NOSORT | GLOB_BRACE | GLOB_TILDE)
481 if (glob(sudoers_cmnd, GLOB_FLAGS, NULL, &gl) != 0 || gl.gl_pathc == 0) {
483 debug_return_bool(false);
485 /* For each glob match, compare basename, st_dev and st_ino. */
486 for (ap = gl.gl_pathv; (cp = *ap) != NULL; ap++) {
487 /* If it ends in '/' it is a directory spec. */
489 if (cp[dlen - 1] == '/') {
490 if (command_matches_dir(cp, dlen))
491 debug_return_bool(true);
495 /* Only proceed if user_base and basename(cp) match */
496 if ((base = strrchr(cp, '/')) != NULL)
500 if (strcmp(user_base, base) != 0 ||
501 stat(cp, &sudoers_stat) == -1)
503 if (user_stat == NULL ||
504 (user_stat->st_dev == sudoers_stat.st_dev &&
505 user_stat->st_ino == sudoers_stat.st_ino)) {
507 safe_cmnd = estrdup(cp);
513 debug_return_bool(false);
515 if (command_args_match(sudoers_cmnd, sudoers_args)) {
517 safe_cmnd = estrdup(user_cmnd);
518 debug_return_bool(true);
520 debug_return_bool(false);
524 command_matches_normal(char *sudoers_cmnd, char *sudoers_args)
526 struct stat sudoers_stat;
529 debug_decl(command_matches_normal, SUDO_DEBUG_MATCH)
531 /* If it ends in '/' it is a directory spec. */
532 dlen = strlen(sudoers_cmnd);
533 if (sudoers_cmnd[dlen - 1] == '/')
534 debug_return_bool(command_matches_dir(sudoers_cmnd, dlen));
536 /* Only proceed if user_base and basename(sudoers_cmnd) match */
537 if ((base = strrchr(sudoers_cmnd, '/')) == NULL)
541 if (strcmp(user_base, base) != 0 ||
542 stat(sudoers_cmnd, &sudoers_stat) == -1)
543 debug_return_bool(false);
546 * Return true if inode/device matches AND
547 * a) there are no args in sudoers OR
548 * b) there are no args on command line and none req by sudoers OR
549 * c) there are args in sudoers and on command line and they match
551 if (user_stat != NULL &&
552 (user_stat->st_dev != sudoers_stat.st_dev ||
553 user_stat->st_ino != sudoers_stat.st_ino))
554 debug_return_bool(false);
555 if (command_args_match(sudoers_cmnd, sudoers_args)) {
557 safe_cmnd = estrdup(sudoers_cmnd);
558 debug_return_bool(true);
560 debug_return_bool(false);
564 * Return true if user_cmnd names one of the inodes in dir, else false.
567 command_matches_dir(char *sudoers_dir, size_t dlen)
569 struct stat sudoers_stat;
573 debug_decl(command_matches_dir, SUDO_DEBUG_MATCH)
576 * Grot through directory entries, looking for user_base.
578 dirp = opendir(sudoers_dir);
580 debug_return_bool(false);
582 if (strlcpy(buf, sudoers_dir, sizeof(buf)) >= sizeof(buf)) {
584 debug_return_bool(false);
586 while ((dent = readdir(dirp)) != NULL) {
587 /* ignore paths > PATH_MAX (XXX - log) */
589 if (strlcat(buf, dent->d_name, sizeof(buf)) >= sizeof(buf))
592 /* only stat if basenames are the same */
593 if (strcmp(user_base, dent->d_name) != 0 ||
594 stat(buf, &sudoers_stat) == -1)
596 if (user_stat == NULL ||
597 (user_stat->st_dev == sudoers_stat.st_dev &&
598 user_stat->st_ino == sudoers_stat.st_ino)) {
600 safe_cmnd = estrdup(buf);
606 debug_return_bool(dent != NULL);
610 * Returns true if the hostname matches the pattern, else false
613 hostname_matches(char *shost, char *lhost, char *pattern)
615 debug_decl(hostname_matches, SUDO_DEBUG_MATCH)
617 if (has_meta(pattern)) {
618 if (strchr(pattern, '.'))
619 debug_return_bool(!fnmatch(pattern, lhost, FNM_CASEFOLD));
621 debug_return_bool(!fnmatch(pattern, shost, FNM_CASEFOLD));
623 if (strchr(pattern, '.'))
624 debug_return_bool(!strcasecmp(lhost, pattern));
626 debug_return_bool(!strcasecmp(shost, pattern));
631 * Returns true if the user/uid from sudoers matches the specified user/uid,
632 * else returns false.
635 userpw_matches(char *sudoers_user, char *user, struct passwd *pw)
637 debug_decl(userpw_matches, SUDO_DEBUG_MATCH)
639 if (pw != NULL && *sudoers_user == '#') {
640 uid_t uid = (uid_t) atoi(sudoers_user + 1);
641 if (uid == pw->pw_uid)
642 debug_return_bool(true);
644 debug_return_bool(strcmp(sudoers_user, user) == 0);
648 * Returns true if the group/gid from sudoers matches the specified group/gid,
649 * else returns false.
652 group_matches(char *sudoers_group, struct group *gr)
654 debug_decl(group_matches, SUDO_DEBUG_MATCH)
656 if (*sudoers_group == '#') {
657 gid_t gid = (gid_t) atoi(sudoers_group + 1);
658 if (gid == gr->gr_gid)
659 debug_return_bool(true);
661 debug_return_bool(strcmp(gr->gr_name, sudoers_group) == 0);
665 * Returns true if the given user belongs to the named group,
666 * else returns false.
669 usergr_matches(char *group, char *user, struct passwd *pw)
672 struct passwd *pw0 = NULL;
673 debug_decl(usergr_matches, SUDO_DEBUG_MATCH)
675 /* make sure we have a valid usergroup, sudo style */
679 if (*group == ':' && def_group_plugin) {
680 matched = group_plugin_query(user, group + 1, pw);
684 /* look up user's primary gid in the passwd file */
686 if ((pw0 = sudo_getpwnam(user)) == NULL)
691 if (user_in_group(pw, group)) {
696 /* not a Unix group, could be an external group */
697 if (def_group_plugin && group_plugin_query(user, group, pw)) {
706 debug_return_bool(matched);
710 * Returns true if "host" and "user" belong to the netgroup "netgr",
711 * else return false. Either of "host", "shost" or "user" may be NULL
712 * in which case that argument is not checked...
714 * XXX - swap order of host & shost
717 netgr_matches(char *netgr, char *lhost, char *shost, char *user)
720 #ifdef HAVE_GETDOMAINNAME
721 static int initialized;
723 debug_decl(netgr_matches, SUDO_DEBUG_MATCH)
725 /* make sure we have a valid netgroup, sudo style */
727 debug_return_bool(false);
729 #ifdef HAVE_GETDOMAINNAME
730 /* get the domain name (if any) */
732 domain = (char *) emalloc(MAXHOSTNAMELEN + 1);
733 if (getdomainname(domain, MAXHOSTNAMELEN + 1) == -1 || *domain == '\0') {
739 #endif /* HAVE_GETDOMAINNAME */
742 if (innetgr(netgr, lhost, user, domain))
743 debug_return_bool(true);
744 else if (lhost != shost && innetgr(netgr, shost, user, domain))
745 debug_return_bool(true);
746 #endif /* HAVE_INNETGR */
748 debug_return_bool(false);