2 * Copyright (c) 1996, 1998-2005, 2007-2009
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 */
92 #ifdef USING_NONUNIX_GROUPS
94 #endif /* USING_NONUNIX_GROUPS */
97 __unused static const char rcsid[] = "$Sudo: match.c,v 1.46 2009/05/27 00:49:07 millert Exp $";
100 static struct member_list empty;
102 static int command_matches_dir __P((char *, size_t));
103 static int command_matches_glob __P((char *, char *));
104 static int command_matches_fnmatch __P((char *, char *));
105 static int command_matches_normal __P((char *, char *));
108 * Returns TRUE if string 's' contains meta characters.
110 #define has_meta(s) (strpbrk(s, "\\?*[]") != NULL)
113 * Check for user described by pw in a list of members.
114 * Returns ALLOW, DENY or UNSPEC.
117 _userlist_matches(pw, list)
119 struct member_list *list;
123 int rval, matched = UNSPEC;
125 tq_foreach_rev(list, m) {
128 matched = !m->negated;
131 if (netgr_matches(m->name, NULL, NULL, pw->pw_name))
132 matched = !m->negated;
135 if (usergr_matches(m->name, pw->pw_name, pw))
136 matched = !m->negated;
139 if ((a = alias_find(m->name, USERALIAS)) != NULL) {
140 rval = _userlist_matches(pw, &a->members);
142 matched = m->negated ? !rval : rval;
147 if (userpw_matches(m->name, pw->pw_name, pw))
148 matched = !m->negated;
151 if (matched != UNSPEC)
158 userlist_matches(pw, list)
160 struct member_list *list;
163 return(_userlist_matches(pw, list));
167 * Check for user described by pw in a list of members.
168 * If both lists are empty compare against def_runas_default.
169 * Returns ALLOW, DENY or UNSPEC.
172 _runaslist_matches(user_list, group_list)
173 struct member_list *user_list;
174 struct member_list *group_list;
178 int rval, matched = UNSPEC;
180 if (runas_gr != NULL) {
181 if (tq_empty(group_list))
182 return(DENY); /* group was specified but none in sudoers */
183 if (runas_pw != NULL && strcmp(runas_pw->pw_name, user_name) &&
185 return(DENY); /* user was specified but none in sudoers */
188 if (tq_empty(user_list) && tq_empty(group_list))
189 return(userpw_matches(def_runas_default, runas_pw->pw_name, runas_pw));
191 if (runas_pw != NULL) {
192 tq_foreach_rev(user_list, m) {
195 matched = !m->negated;
198 if (netgr_matches(m->name, NULL, NULL, runas_pw->pw_name))
199 matched = !m->negated;
202 if (usergr_matches(m->name, runas_pw->pw_name, runas_pw))
203 matched = !m->negated;
206 if ((a = alias_find(m->name, RUNASALIAS)) != NULL) {
207 rval = _runaslist_matches(&a->members, &empty);
209 matched = m->negated ? !rval : rval;
214 if (userpw_matches(m->name, runas_pw->pw_name, runas_pw))
215 matched = !m->negated;
218 if (matched != UNSPEC)
223 if (runas_gr != NULL) {
224 tq_foreach_rev(group_list, m) {
227 matched = !m->negated;
230 if ((a = alias_find(m->name, RUNASALIAS)) != NULL) {
231 rval = _runaslist_matches(&a->members, &empty);
233 matched = m->negated ? !rval : rval;
238 if (group_matches(m->name, runas_gr))
239 matched = !m->negated;
242 if (matched != UNSPEC)
251 runaslist_matches(user_list, group_list)
252 struct member_list *user_list;
253 struct member_list *group_list;
256 return(_runaslist_matches(user_list ? user_list : &empty,
257 group_list ? group_list : &empty));
261 * Check for host and shost in a list of members.
262 * Returns ALLOW, DENY or UNSPEC.
265 _hostlist_matches(list)
266 struct member_list *list;
270 int rval, matched = UNSPEC;
272 tq_foreach_rev(list, m) {
275 matched = !m->negated;
278 if (netgr_matches(m->name, user_host, user_shost, NULL))
279 matched = !m->negated;
282 if (addr_matches(m->name))
283 matched = !m->negated;
286 if ((a = alias_find(m->name, HOSTALIAS)) != NULL) {
287 rval = _hostlist_matches(&a->members);
289 matched = m->negated ? !rval : rval;
294 if (hostname_matches(user_shost, user_host, m->name))
295 matched = !m->negated;
298 if (matched != UNSPEC)
305 hostlist_matches(list)
306 struct member_list *list;
309 return(_hostlist_matches(list));
313 * Check for cmnd and args in a list of members.
314 * Returns ALLOW, DENY or UNSPEC.
317 _cmndlist_matches(list)
318 struct member_list *list;
321 int rval, matched = UNSPEC;
323 tq_foreach_rev(list, m) {
324 rval = cmnd_matches(m);
325 if (rval != UNSPEC) {
326 matched = m->negated ? !rval : rval;
334 cmndlist_matches(list)
335 struct member_list *list;
338 return(_cmndlist_matches(list));
342 * Check cmnd and args.
343 * Returns ALLOW, DENY or UNSPEC.
350 struct sudo_command *c;
351 int rval, matched = UNSPEC;
355 matched = !m->negated;
359 if ((a = alias_find(m->name, CMNDALIAS)) != NULL) {
360 rval = _cmndlist_matches(&a->members);
362 matched = m->negated ? !rval : rval;
366 c = (struct sudo_command *)m->name;
367 if (command_matches(c->cmnd, c->args))
368 matched = !m->negated;
375 * If path doesn't end in /, return TRUE iff cmnd & path name the same inode;
376 * otherwise, return TRUE if user_cmnd names one of the inodes in path.
379 command_matches(sudoers_cmnd, sudoers_args)
383 /* Check for pseudo-commands */
384 if (strchr(user_cmnd, '/') == NULL) {
386 * Return true if both sudoers_cmnd and user_cmnd are "sudoedit" AND
387 * a) there are no args in sudoers OR
388 * b) there are no args on command line and none req by sudoers OR
389 * c) there are args in sudoers and on command line and they match
391 if (strcmp(sudoers_cmnd, "sudoedit") != 0 ||
392 strcmp(user_cmnd, "sudoedit") != 0)
395 (!user_args && sudoers_args && !strcmp("\"\"", sudoers_args)) ||
397 fnmatch(sudoers_args, user_args ? user_args : "", 0) == 0)) {
399 safe_cmnd = estrdup(sudoers_cmnd);
405 if (has_meta(sudoers_cmnd)) {
407 * If sudoers_cmnd has meta characters in it, we need to
408 * use glob(3) and/or fnmatch(3) to do the matching.
411 return(command_matches_fnmatch(sudoers_cmnd, sudoers_args));
412 return(command_matches_glob(sudoers_cmnd, sudoers_args));
414 return(command_matches_normal(sudoers_cmnd, sudoers_args));
418 command_matches_fnmatch(sudoers_cmnd, sudoers_args)
423 * Return true if fnmatch(3) succeeds AND
424 * a) there are no args in sudoers OR
425 * b) there are no args on command line and none required by sudoers OR
426 * c) there are args in sudoers and on command line and they match
429 if (fnmatch(sudoers_cmnd, user_cmnd, FNM_PATHNAME) != 0)
432 (!user_args && sudoers_args && !strcmp("\"\"", sudoers_args)) ||
434 fnmatch(sudoers_args, user_args ? user_args : "", 0) == 0)) {
437 safe_cmnd = estrdup(user_cmnd);
444 command_matches_glob(sudoers_cmnd, sudoers_args)
448 struct stat sudoers_stat;
450 char **ap, *base, *cp;
454 * First check to see if we can avoid the call to glob(3).
455 * Short circuit if there are no meta chars in the command itself
456 * and user_base and basename(sudoers_cmnd) don't match.
458 dlen = strlen(sudoers_cmnd);
459 if (sudoers_cmnd[dlen - 1] != '/') {
460 if ((base = strrchr(sudoers_cmnd, '/')) != NULL) {
462 if (!has_meta(base) && strcmp(user_base, base) != 0)
467 * Return true if we find a match in the glob(3) results AND
468 * a) there are no args in sudoers OR
469 * b) there are no args on command line and none required by sudoers OR
470 * c) there are args in sudoers and on command line and they match
473 #define GLOB_FLAGS (GLOB_NOSORT | GLOB_MARK | GLOB_BRACE | GLOB_TILDE)
474 if (glob(sudoers_cmnd, GLOB_FLAGS, NULL, &gl) != 0) {
478 /* For each glob match, compare basename, st_dev and st_ino. */
479 for (ap = gl.gl_pathv; (cp = *ap) != NULL; ap++) {
480 /* If it ends in '/' it is a directory spec. */
482 if (cp[dlen - 1] == '/') {
483 if (command_matches_dir(cp, dlen))
488 /* Only proceed if user_base and basename(cp) match */
489 if ((base = strrchr(cp, '/')) != NULL)
493 if (strcmp(user_base, base) != 0 ||
494 stat(cp, &sudoers_stat) == -1)
496 if (user_stat == NULL ||
497 (user_stat->st_dev == sudoers_stat.st_dev &&
498 user_stat->st_ino == sudoers_stat.st_ino)) {
500 safe_cmnd = estrdup(cp);
509 (!user_args && sudoers_args && !strcmp("\"\"", sudoers_args)) ||
511 fnmatch(sudoers_args, user_args ? user_args : "", 0) == 0)) {
513 safe_cmnd = estrdup(user_cmnd);
520 command_matches_normal(sudoers_cmnd, sudoers_args)
524 struct stat sudoers_stat;
528 /* If it ends in '/' it is a directory spec. */
529 dlen = strlen(sudoers_cmnd);
530 if (sudoers_cmnd[dlen - 1] == '/')
531 return(command_matches_dir(sudoers_cmnd, dlen));
533 /* Only proceed if user_base and basename(sudoers_cmnd) match */
534 if ((base = strrchr(sudoers_cmnd, '/')) == NULL)
538 if (strcmp(user_base, base) != 0 ||
539 stat(sudoers_cmnd, &sudoers_stat) == -1)
543 * Return true if inode/device matches AND
544 * a) there are no args in sudoers OR
545 * b) there are no args on command line and none req by sudoers OR
546 * c) there are args in sudoers and on command line and they match
548 if (user_stat != NULL &&
549 (user_stat->st_dev != sudoers_stat.st_dev ||
550 user_stat->st_ino != sudoers_stat.st_ino))
553 (!user_args && sudoers_args && !strcmp("\"\"", sudoers_args)) ||
555 fnmatch(sudoers_args, user_args ? user_args : "", 0) == 0)) {
557 safe_cmnd = estrdup(sudoers_cmnd);
564 * Return TRUE if user_cmnd names one of the inodes in dir, else FALSE.
567 command_matches_dir(sudoers_dir, dlen)
571 struct stat sudoers_stat;
577 * Grot through directory entries, looking for user_base.
579 dirp = opendir(sudoers_dir);
583 if (strlcpy(buf, sudoers_dir, sizeof(buf)) >= sizeof(buf))
585 while ((dent = readdir(dirp)) != NULL) {
586 /* ignore paths > PATH_MAX (XXX - log) */
588 if (strlcat(buf, dent->d_name, sizeof(buf)) >= sizeof(buf))
591 /* only stat if basenames are the same */
592 if (strcmp(user_base, dent->d_name) != 0 ||
593 stat(buf, &sudoers_stat) == -1)
595 if (user_stat->st_dev == sudoers_stat.st_dev &&
596 user_stat->st_ino == sudoers_stat.st_ino) {
598 safe_cmnd = estrdup(buf);
604 return(dent != NULL);
613 struct interface *ifp;
615 struct in6_addr addr6;
621 if (inet_pton(AF_INET6, n, &addr6) > 0) {
627 addr.s_addr = inet_addr(n);
630 for (i = 0; i < num_interfaces; i++) {
631 ifp = &interfaces[i];
632 if (ifp->family != family)
636 if (ifp->addr.ip4.s_addr == addr.s_addr ||
637 (ifp->addr.ip4.s_addr & ifp->netmask.ip4.s_addr)
643 if (memcmp(ifp->addr.ip6.s6_addr, addr6.s6_addr,
644 sizeof(addr6.s6_addr)) == 0)
646 for (j = 0; j < sizeof(addr6.s6_addr); j++) {
647 if ((ifp->addr.ip6.s6_addr[j] & ifp->netmask.ip6.s6_addr[j]) != addr6.s6_addr[j])
650 if (j == sizeof(addr6.s6_addr))
660 addr_matches_if_netmask(n, m)
665 struct in_addr addr, mask;
666 struct interface *ifp;
668 struct in6_addr addr6, mask6;
674 if (inet_pton(AF_INET6, n, &addr6) > 0)
680 addr.s_addr = inet_addr(n);
683 if (family == AF_INET) {
685 mask.s_addr = inet_addr(m);
688 mask.s_addr = 0xffffffff;
691 mask.s_addr = htonl(mask.s_addr);
696 if (inet_pton(AF_INET6, m, &mask6) <= 0) {
698 for (i = 0; i < 16; i++) {
700 mask6.s6_addr[i] = 0;
701 else if (i * 8 + 8 <= j)
702 mask6.s6_addr[i] = 0xff;
704 mask6.s6_addr[i] = 0xff00 >> (j - i * 8);
708 #endif /* HAVE_IN6_ADDR */
710 for (i = 0; i < num_interfaces; i++) {
711 ifp = &interfaces[i];
712 if (ifp->family != family)
716 if ((ifp->addr.ip4.s_addr & mask.s_addr) == addr.s_addr)
720 for (j = 0; j < sizeof(addr6.s6_addr); j++) {
721 if ((ifp->addr.ip6.s6_addr[j] & mask6.s6_addr[j]) != addr6.s6_addr[j])
724 if (j == sizeof(addr6.s6_addr))
726 #endif /* HAVE_IN6_ADDR */
734 * Returns TRUE if "n" is one of our ip addresses or if
735 * "n" is a network that we are on, else returns FALSE.
744 /* If there's an explicit netmask, use it. */
745 if ((m = strchr(n, '/'))) {
747 retval = addr_matches_if_netmask(n, m);
750 retval = addr_matches_if(n);
756 * Returns TRUE if the hostname matches the pattern, else FALSE
759 hostname_matches(shost, lhost, pattern)
764 if (has_meta(pattern)) {
765 if (strchr(pattern, '.'))
766 return(!fnmatch(pattern, lhost, FNM_CASEFOLD));
768 return(!fnmatch(pattern, shost, FNM_CASEFOLD));
770 if (strchr(pattern, '.'))
771 return(!strcasecmp(lhost, pattern));
773 return(!strcasecmp(shost, pattern));
778 * Returns TRUE if the user/uid from sudoers matches the specified user/uid,
779 * else returns FALSE.
782 userpw_matches(sudoers_user, user, pw)
787 if (pw != NULL && *sudoers_user == '#') {
788 uid_t uid = (uid_t) atoi(sudoers_user + 1);
789 if (uid == pw->pw_uid)
792 return(strcmp(sudoers_user, user) == 0);
796 * Returns TRUE if the group/gid from sudoers matches the specified group/gid,
797 * else returns FALSE.
800 group_matches(sudoers_group, gr)
804 if (*sudoers_group == '#') {
805 gid_t gid = (gid_t) atoi(sudoers_group + 1);
806 if (gid == gr->gr_gid)
809 return(strcmp(gr->gr_name, sudoers_group) == 0);
813 * Returns TRUE if the given user belongs to the named group,
814 * else returns FALSE.
817 usergr_matches(group, user, pw)
822 struct group *grp = NULL;
826 /* make sure we have a valid usergroup, sudo style */
830 #ifdef USING_NONUNIX_GROUPS
832 return(sudo_nonunix_groupcheck(++group, user, pw));
833 #endif /* USING_NONUNIX_GROUPS */
835 /* look up user's primary gid in the passwd file */
836 if (pw == NULL && (pw = sudo_getpwnam(user)) == NULL)
837 goto try_supplementary;
839 /* check against user's primary (passwd file) gid */
840 if ((grp = sudo_getgrnam(group)) == NULL)
841 goto try_supplementary;
842 if (grp->gr_gid == pw->pw_gid)
846 * If we are matching the invoking or list user and that user has a
847 * supplementary group vector, check it first.
849 if (strcmp(user, list_pw ? list_pw->pw_name : user_name) == 0) {
850 for (i = 0; i < user_ngroups; i++)
851 if (grp->gr_gid == user_groups[i])
856 if (grp != NULL && grp->gr_mem != NULL) {
857 for (cur = grp->gr_mem; *cur; cur++)
858 if (strcmp(*cur, user) == 0)
862 #ifdef USING_NONUNIX_GROUPS
863 /* not a Unix group, could be an AD group */
864 if (sudo_nonunix_groupcheck_available() &&
865 sudo_nonunix_groupcheck(group, user, pw))
867 #endif /* USING_NONUNIX_GROUPS */
873 * Returns TRUE if "host" and "user" belong to the netgroup "netgr",
874 * else return FALSE. Either of "host", "shost" or "user" may be NULL
875 * in which case that argument is not checked...
877 * XXX - swap order of host & shost
880 netgr_matches(netgr, lhost, shost, user)
887 #ifdef HAVE_GETDOMAINNAME
888 static int initialized;
891 /* make sure we have a valid netgroup, sudo style */
895 #ifdef HAVE_GETDOMAINNAME
896 /* get the domain name (if any) */
898 domain = (char *) emalloc(MAXHOSTNAMELEN + 1);
899 if (getdomainname(domain, MAXHOSTNAMELEN + 1) == -1 || *domain == '\0') {
905 #endif /* HAVE_GETDOMAINNAME */
908 if (innetgr(netgr, lhost, user, domain))
910 else if (lhost != shost && innetgr(netgr, shost, user, domain))
912 #endif /* HAVE_INNETGR */