2 * Copyright (c) 1996, 1998-2005, 2007-2008
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
82 #include "interfaces.h"
87 # include "emul/fnmatch.h"
88 #endif /* HAVE_FNMATCH */
89 #ifndef HAVE_EXTENDED_GLOB
90 # include "emul/glob.h"
91 #endif /* HAVE_EXTENDED_GLOB */
94 __unused static const char rcsid[] = "$Sudo: match.c,v 1.38 2008/11/02 14:35:37 millert Exp $";
97 static struct member_list empty;
99 static int command_matches_dir __P((char *, size_t));
102 * Returns TRUE if string 's' contains meta characters.
104 #define has_meta(s) (strpbrk(s, "\\?*[]") != NULL)
107 * Check for user described by pw in a list of members.
108 * Returns ALLOW, DENY or UNSPEC.
111 _userlist_matches(pw, list)
113 struct member_list *list;
117 int rval, matched = UNSPEC;
119 tq_foreach_rev(list, m) {
122 matched = !m->negated;
125 if (netgr_matches(m->name, NULL, NULL, pw->pw_name))
126 matched = !m->negated;
129 if (usergr_matches(m->name, pw->pw_name, pw))
130 matched = !m->negated;
133 if ((a = find_alias(m->name, USERALIAS)) != NULL) {
134 rval = _userlist_matches(pw, &a->members);
136 matched = m->negated ? !rval : rval;
141 if (userpw_matches(m->name, pw->pw_name, pw))
142 matched = !m->negated;
145 if (matched != UNSPEC)
152 userlist_matches(pw, list)
154 struct member_list *list;
157 return(_userlist_matches(pw, list));
161 * Check for user described by pw in a list of members.
162 * If both lists are empty compare against def_runas_default.
163 * Returns ALLOW, DENY or UNSPEC.
166 _runaslist_matches(user_list, group_list)
167 struct member_list *user_list;
168 struct member_list *group_list;
172 int rval, matched = UNSPEC;
174 /* Deny if user specified a group but there is no group in sudoers */
175 if (runas_gr != NULL && tq_empty(group_list))
178 if (tq_empty(user_list) && tq_empty(group_list))
179 return(userpw_matches(def_runas_default, runas_pw->pw_name, runas_pw));
181 if (runas_pw != NULL) {
182 tq_foreach_rev(user_list, m) {
185 matched = !m->negated;
188 if (netgr_matches(m->name, NULL, NULL, runas_pw->pw_name))
189 matched = !m->negated;
192 if (usergr_matches(m->name, runas_pw->pw_name, runas_pw))
193 matched = !m->negated;
196 if ((a = find_alias(m->name, RUNASALIAS)) != NULL) {
197 rval = _runaslist_matches(&a->members, &empty);
199 matched = m->negated ? !rval : rval;
204 if (userpw_matches(m->name, runas_pw->pw_name, runas_pw))
205 matched = !m->negated;
208 if (matched != UNSPEC)
213 if (runas_gr != NULL) {
214 tq_foreach_rev(group_list, m) {
217 matched = !m->negated;
220 if ((a = find_alias(m->name, RUNASALIAS)) != NULL) {
221 rval = _runaslist_matches(&a->members, &empty);
223 matched = m->negated ? !rval : rval;
228 if (group_matches(m->name, runas_gr))
229 matched = !m->negated;
232 if (matched != UNSPEC)
241 runaslist_matches(user_list, group_list)
242 struct member_list *user_list;
243 struct member_list *group_list;
246 return(_runaslist_matches(user_list ? user_list : &empty,
247 group_list ? group_list : &empty));
251 * Check for host and shost in a list of members.
252 * Returns ALLOW, DENY or UNSPEC.
255 _hostlist_matches(list)
256 struct member_list *list;
260 int rval, matched = UNSPEC;
262 tq_foreach_rev(list, m) {
265 matched = !m->negated;
268 if (netgr_matches(m->name, user_host, user_shost, NULL))
269 matched = !m->negated;
272 if (addr_matches(m->name))
273 matched = !m->negated;
276 if ((a = find_alias(m->name, HOSTALIAS)) != NULL) {
277 rval = _hostlist_matches(&a->members);
279 matched = m->negated ? !rval : rval;
284 if (hostname_matches(user_shost, user_host, m->name))
285 matched = !m->negated;
288 if (matched != UNSPEC)
295 hostlist_matches(list)
296 struct member_list *list;
299 return(_hostlist_matches(list));
303 * Check for cmnd and args in a list of members.
304 * Returns ALLOW, DENY or UNSPEC.
307 _cmndlist_matches(list)
308 struct member_list *list;
311 int rval, matched = UNSPEC;
313 tq_foreach_rev(list, m) {
314 rval = cmnd_matches(m);
315 if (rval != UNSPEC) {
316 matched = m->negated ? !rval : rval;
324 cmndlist_matches(list)
325 struct member_list *list;
328 return(_cmndlist_matches(list));
332 * Check cmnd and args.
333 * Returns ALLOW, DENY or UNSPEC.
340 struct sudo_command *c;
341 int rval, matched = UNSPEC;
345 matched = !m->negated;
349 if ((a = find_alias(m->name, CMNDALIAS)) != NULL) {
350 rval = _cmndlist_matches(&a->members);
352 matched = m->negated ? !rval : rval;
356 c = (struct sudo_command *)m->name;
357 if (command_matches(c->cmnd, c->args))
358 matched = !m->negated;
365 * If path doesn't end in /, return TRUE iff cmnd & path name the same inode;
366 * otherwise, return TRUE if user_cmnd names one of the inodes in path.
369 command_matches(sudoers_cmnd, sudoers_args)
373 struct stat sudoers_stat;
374 char **ap, *base, *cp;
378 /* Check for pseudo-commands */
379 if (strchr(user_cmnd, '/') == NULL) {
381 * Return true if both sudoers_cmnd and user_cmnd are "sudoedit" AND
382 * a) there are no args in sudoers OR
383 * b) there are no args on command line and none req by sudoers OR
384 * c) there are args in sudoers and on command line and they match
386 if (strcmp(sudoers_cmnd, "sudoedit") != 0 ||
387 strcmp(user_cmnd, "sudoedit") != 0)
390 (!user_args && sudoers_args && !strcmp("\"\"", sudoers_args)) ||
392 fnmatch(sudoers_args, user_args ? user_args : "", 0) == 0)) {
394 safe_cmnd = estrdup(sudoers_cmnd);
399 dlen = strlen(sudoers_cmnd);
402 * If sudoers_cmnd has meta characters in it, we may need to
403 * use glob(3) and fnmatch(3) to do the matching.
405 if (has_meta(sudoers_cmnd)) {
407 * First check to see if we can avoid the call to glob(3).
408 * Short circuit if there are no meta chars in the command itself
409 * and user_base and basename(sudoers_cmnd) don't match.
411 if (sudoers_cmnd[dlen - 1] != '/') {
412 if ((base = strrchr(sudoers_cmnd, '/')) != NULL) {
414 if (!has_meta(base) && strcmp(user_base, base) != 0)
419 * Return true if we find a match in the glob(3) results AND
420 * a) there are no args in sudoers OR
421 * b) there are no args on command line and none required by sudoers OR
422 * c) there are args in sudoers and on command line and they match
425 #define GLOB_FLAGS (GLOB_NOSORT | GLOB_MARK | GLOB_BRACE | GLOB_TILDE)
426 if (glob(sudoers_cmnd, GLOB_FLAGS, NULL, &gl) != 0) {
430 /* For each glob match, compare basename, st_dev and st_ino. */
431 for (ap = gl.gl_pathv; (cp = *ap) != NULL; ap++) {
432 /* If it ends in '/' it is a directory spec. */
434 if (cp[dlen - 1] == '/') {
435 if (command_matches_dir(cp, dlen))
440 /* Only proceed if user_base and basename(cp) match */
441 if ((base = strrchr(cp, '/')) != NULL)
445 if (strcmp(user_base, base) != 0 ||
446 stat(cp, &sudoers_stat) == -1)
448 if (user_stat == NULL ||
449 (user_stat->st_dev == sudoers_stat.st_dev &&
450 user_stat->st_ino == sudoers_stat.st_ino)) {
452 safe_cmnd = estrdup(cp);
461 (!user_args && sudoers_args && !strcmp("\"\"", sudoers_args)) ||
463 fnmatch(sudoers_args, user_args ? user_args : "", 0) == 0)) {
465 safe_cmnd = estrdup(user_cmnd);
470 /* If it ends in '/' it is a directory spec. */
471 if (sudoers_cmnd[dlen - 1] == '/')
472 return(command_matches_dir(sudoers_cmnd, dlen));
474 /* Only proceed if user_base and basename(sudoers_cmnd) match */
475 if ((base = strrchr(sudoers_cmnd, '/')) == NULL)
479 if (strcmp(user_base, base) != 0 ||
480 stat(sudoers_cmnd, &sudoers_stat) == -1)
484 * Return true if inode/device matches AND
485 * a) there are no args in sudoers OR
486 * b) there are no args on command line and none req by sudoers OR
487 * c) there are args in sudoers and on command line and they match
489 if (user_stat != NULL &&
490 (user_stat->st_dev != sudoers_stat.st_dev ||
491 user_stat->st_ino != sudoers_stat.st_ino))
494 (!user_args && sudoers_args && !strcmp("\"\"", sudoers_args)) ||
496 fnmatch(sudoers_args, user_args ? user_args : "", 0) == 0)) {
498 safe_cmnd = estrdup(sudoers_cmnd);
506 * Return TRUE if user_cmnd names one of the inodes in dir, else FALSE.
509 command_matches_dir(sudoers_dir, dlen)
513 struct stat sudoers_stat;
519 * Grot through directory entries, looking for user_base.
521 dirp = opendir(sudoers_dir);
525 if (strlcpy(buf, sudoers_dir, sizeof(buf)) >= sizeof(buf))
527 while ((dent = readdir(dirp)) != NULL) {
528 /* ignore paths > PATH_MAX (XXX - log) */
530 if (strlcat(buf, dent->d_name, sizeof(buf)) >= sizeof(buf))
533 /* only stat if basenames are the same */
534 if (strcmp(user_base, dent->d_name) != 0 ||
535 stat(buf, &sudoers_stat) == -1)
537 if (user_stat->st_dev == sudoers_stat.st_dev &&
538 user_stat->st_ino == sudoers_stat.st_ino) {
540 safe_cmnd = estrdup(buf);
546 return(dent != NULL);
555 struct interface *ifp;
557 struct in6_addr addr6;
563 if (inet_pton(AF_INET6, n, &addr6) > 0) {
569 addr.s_addr = inet_addr(n);
572 for (i = 0; i < num_interfaces; i++) {
573 ifp = &interfaces[i];
574 if (ifp->family != family)
578 if (ifp->addr.ip4.s_addr == addr.s_addr ||
579 (ifp->addr.ip4.s_addr & ifp->netmask.ip4.s_addr)
585 if (memcmp(ifp->addr.ip6.s6_addr, addr6.s6_addr,
586 sizeof(addr6.s6_addr)) == 0)
588 for (j = 0; j < sizeof(addr6.s6_addr); j++) {
589 if ((ifp->addr.ip6.s6_addr[j] & ifp->netmask.ip6.s6_addr[j]) != addr6.s6_addr[j])
592 if (j == sizeof(addr6.s6_addr))
602 addr_matches_if_netmask(n, m)
607 struct in_addr addr, mask;
608 struct interface *ifp;
610 struct in6_addr addr6, mask6;
616 if (inet_pton(AF_INET6, n, &addr6) > 0)
622 addr.s_addr = inet_addr(n);
625 if (family == AF_INET) {
627 mask.s_addr = inet_addr(m);
630 mask.s_addr = 0xffffffff;
633 mask.s_addr = htonl(mask.s_addr);
638 if (inet_pton(AF_INET6, m, &mask6) <= 0) {
640 for (i = 0; i < 16; i++) {
642 mask6.s6_addr[i] = 0;
643 else if (i * 8 + 8 <= j)
644 mask6.s6_addr[i] = 0xff;
646 mask6.s6_addr[i] = 0xff00 >> (j - i * 8);
650 #endif /* HAVE_IN6_ADDR */
652 for (i = 0; i < num_interfaces; i++) {
653 ifp = &interfaces[i];
654 if (ifp->family != family)
658 if ((ifp->addr.ip4.s_addr & mask.s_addr) == addr.s_addr)
662 for (j = 0; j < sizeof(addr6.s6_addr); j++) {
663 if ((ifp->addr.ip6.s6_addr[j] & mask6.s6_addr[j]) != addr6.s6_addr[j])
666 if (j == sizeof(addr6.s6_addr))
668 #endif /* HAVE_IN6_ADDR */
676 * Returns TRUE if "n" is one of our ip addresses or if
677 * "n" is a network that we are on, else returns FALSE.
686 /* If there's an explicit netmask, use it. */
687 if ((m = strchr(n, '/'))) {
689 retval = addr_matches_if_netmask(n, m);
692 retval = addr_matches_if(n);
698 * Returns TRUE if the hostname matches the pattern, else FALSE
701 hostname_matches(shost, lhost, pattern)
706 if (has_meta(pattern)) {
707 if (strchr(pattern, '.'))
708 return(!fnmatch(pattern, lhost, FNM_CASEFOLD));
710 return(!fnmatch(pattern, shost, FNM_CASEFOLD));
712 if (strchr(pattern, '.'))
713 return(!strcasecmp(lhost, pattern));
715 return(!strcasecmp(shost, pattern));
720 * Returns TRUE if the user/uid from sudoers matches the specified user/uid,
721 * else returns FALSE.
724 userpw_matches(sudoers_user, user, pw)
729 if (pw != NULL && *sudoers_user == '#') {
730 uid_t uid = (uid_t) atoi(sudoers_user + 1);
731 if (uid == pw->pw_uid)
734 return(strcmp(sudoers_user, user) == 0);
738 * Returns TRUE if the group/gid from sudoers matches the specified group/gid,
739 * else returns FALSE.
742 group_matches(sudoers_group, gr)
746 if (*sudoers_group == '#') {
747 gid_t gid = (gid_t) atoi(sudoers_group + 1);
748 if (gid == gr->gr_gid)
751 return(strcmp(gr->gr_name, sudoers_group) == 0);
755 * Returns TRUE if the given user belongs to the named group,
756 * else returns FALSE.
757 * XXX - reduce the number of group lookups
760 usergr_matches(group, user, pw)
769 /* make sure we have a valid usergroup, sudo style */
773 /* look up user's primary gid in the passwd file */
774 if (pw == NULL && (pw = sudo_getpwnam(user)) == NULL)
777 if ((grp = sudo_getgrnam(group)) == NULL)
780 /* check against user's primary (passwd file) gid */
781 if (grp->gr_gid == pw->pw_gid)
785 * If we are matching the invoking or list user and that user has a
786 * supplementary group vector, check it first.
788 if (strcmp(user, list_pw ? list_pw->pw_name : user_name) == 0) {
789 for (i = 0; i < user_ngroups; i++)
790 if (grp->gr_gid == user_groups[i])
793 if (grp->gr_mem != NULL) {
794 for (cur = grp->gr_mem; *cur; cur++)
795 if (strcmp(*cur, user) == 0)
803 * Returns TRUE if "host" and "user" belong to the netgroup "netgr",
804 * else return FALSE. Either of "host", "shost" or "user" may be NULL
805 * in which case that argument is not checked...
807 * XXX - swap order of host & shost
810 netgr_matches(netgr, lhost, shost, user)
817 #ifdef HAVE_GETDOMAINNAME
818 static int initialized;
821 /* make sure we have a valid netgroup, sudo style */
825 #ifdef HAVE_GETDOMAINNAME
826 /* get the domain name (if any) */
828 domain = (char *) emalloc(MAXHOSTNAMELEN + 1);
829 if (getdomainname(domain, MAXHOSTNAMELEN + 1) == -1 || *domain == '\0') {
835 #endif /* HAVE_GETDOMAINNAME */
838 if (innetgr(netgr, lhost, user, domain))
840 else if (lhost != shost && innetgr(netgr, shost, user, domain))
842 #endif /* HAVE_INNETGR */