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 */
96 static struct member_list empty;
98 static int command_matches_dir __P((char *, size_t));
99 static int command_matches_glob __P((char *, char *));
100 static int command_matches_fnmatch __P((char *, char *));
101 static int command_matches_normal __P((char *, char *));
104 * Returns TRUE if string 's' contains meta characters.
106 #define has_meta(s) (strpbrk(s, "\\?*[]") != NULL)
109 * Check for user described by pw in a list of members.
110 * Returns ALLOW, DENY or UNSPEC.
113 _userlist_matches(pw, list)
115 struct member_list *list;
119 int rval, matched = UNSPEC;
121 tq_foreach_rev(list, m) {
124 matched = !m->negated;
127 if (netgr_matches(m->name, NULL, NULL, pw->pw_name))
128 matched = !m->negated;
131 if (usergr_matches(m->name, pw->pw_name, pw))
132 matched = !m->negated;
135 if ((a = alias_find(m->name, USERALIAS)) != NULL) {
136 rval = _userlist_matches(pw, &a->members);
138 matched = m->negated ? !rval : rval;
143 if (userpw_matches(m->name, pw->pw_name, pw))
144 matched = !m->negated;
147 if (matched != UNSPEC)
154 userlist_matches(pw, list)
156 struct member_list *list;
159 return(_userlist_matches(pw, list));
163 * Check for user described by pw in a list of members.
164 * If both lists are empty compare against def_runas_default.
165 * Returns ALLOW, DENY or UNSPEC.
168 _runaslist_matches(user_list, group_list)
169 struct member_list *user_list;
170 struct member_list *group_list;
174 int rval, matched = UNSPEC;
176 if (runas_gr != NULL) {
177 if (tq_empty(group_list))
178 return(DENY); /* group was specified but none in sudoers */
179 if (runas_pw != NULL && strcmp(runas_pw->pw_name, user_name) &&
181 return(DENY); /* user was specified but none in sudoers */
184 if (tq_empty(user_list) && tq_empty(group_list))
185 return(userpw_matches(def_runas_default, runas_pw->pw_name, runas_pw));
187 if (runas_pw != NULL) {
188 tq_foreach_rev(user_list, m) {
191 matched = !m->negated;
194 if (netgr_matches(m->name, NULL, NULL, runas_pw->pw_name))
195 matched = !m->negated;
198 if (usergr_matches(m->name, runas_pw->pw_name, runas_pw))
199 matched = !m->negated;
202 if ((a = alias_find(m->name, RUNASALIAS)) != NULL) {
203 rval = _runaslist_matches(&a->members, &empty);
205 matched = m->negated ? !rval : rval;
210 if (userpw_matches(m->name, runas_pw->pw_name, runas_pw))
211 matched = !m->negated;
214 if (matched != UNSPEC)
219 if (runas_gr != NULL) {
220 tq_foreach_rev(group_list, m) {
223 matched = !m->negated;
226 if ((a = alias_find(m->name, RUNASALIAS)) != NULL) {
227 rval = _runaslist_matches(&a->members, &empty);
229 matched = m->negated ? !rval : rval;
234 if (group_matches(m->name, runas_gr))
235 matched = !m->negated;
238 if (matched != UNSPEC)
247 runaslist_matches(user_list, group_list)
248 struct member_list *user_list;
249 struct member_list *group_list;
252 return(_runaslist_matches(user_list ? user_list : &empty,
253 group_list ? group_list : &empty));
257 * Check for host and shost in a list of members.
258 * Returns ALLOW, DENY or UNSPEC.
261 _hostlist_matches(list)
262 struct member_list *list;
266 int rval, matched = UNSPEC;
268 tq_foreach_rev(list, m) {
271 matched = !m->negated;
274 if (netgr_matches(m->name, user_host, user_shost, NULL))
275 matched = !m->negated;
278 if (addr_matches(m->name))
279 matched = !m->negated;
282 if ((a = alias_find(m->name, HOSTALIAS)) != NULL) {
283 rval = _hostlist_matches(&a->members);
285 matched = m->negated ? !rval : rval;
290 if (hostname_matches(user_shost, user_host, m->name))
291 matched = !m->negated;
294 if (matched != UNSPEC)
301 hostlist_matches(list)
302 struct member_list *list;
305 return(_hostlist_matches(list));
309 * Check for cmnd and args in a list of members.
310 * Returns ALLOW, DENY or UNSPEC.
313 _cmndlist_matches(list)
314 struct member_list *list;
317 int matched = UNSPEC;
319 tq_foreach_rev(list, m) {
320 matched = cmnd_matches(m);
321 if (matched != UNSPEC)
328 cmndlist_matches(list)
329 struct member_list *list;
332 return(_cmndlist_matches(list));
336 * Check cmnd and args.
337 * Returns ALLOW, DENY or UNSPEC.
344 struct sudo_command *c;
345 int rval, matched = UNSPEC;
349 matched = !m->negated;
353 if ((a = alias_find(m->name, CMNDALIAS)) != NULL) {
354 rval = _cmndlist_matches(&a->members);
356 matched = m->negated ? !rval : rval;
360 c = (struct sudo_command *)m->name;
361 if (command_matches(c->cmnd, c->args))
362 matched = !m->negated;
369 * If path doesn't end in /, return TRUE iff cmnd & path name the same inode;
370 * otherwise, return TRUE if user_cmnd names one of the inodes in path.
373 command_matches(sudoers_cmnd, sudoers_args)
377 /* Check for pseudo-commands */
378 if (sudoers_cmnd[0] != '/') {
380 * Return true if both sudoers_cmnd and user_cmnd are "sudoedit" AND
381 * a) there are no args in sudoers OR
382 * b) there are no args on command line and none req by sudoers OR
383 * c) there are args in sudoers and on command line and they match
385 if (strcmp(sudoers_cmnd, "sudoedit") != 0 ||
386 strcmp(user_cmnd, "sudoedit") != 0)
389 (!user_args && sudoers_args && !strcmp("\"\"", sudoers_args)) ||
391 fnmatch(sudoers_args, user_args ? user_args : "", 0) == 0)) {
393 safe_cmnd = estrdup(sudoers_cmnd);
399 if (has_meta(sudoers_cmnd)) {
401 * If sudoers_cmnd has meta characters in it, we need to
402 * use glob(3) and/or fnmatch(3) to do the matching.
405 return(command_matches_fnmatch(sudoers_cmnd, sudoers_args));
406 return(command_matches_glob(sudoers_cmnd, sudoers_args));
408 return(command_matches_normal(sudoers_cmnd, sudoers_args));
412 command_matches_fnmatch(sudoers_cmnd, sudoers_args)
417 * Return true if fnmatch(3) succeeds AND
418 * a) there are no args in sudoers OR
419 * b) there are no args on command line and none required by sudoers OR
420 * c) there are args in sudoers and on command line and they match
423 if (fnmatch(sudoers_cmnd, user_cmnd, FNM_PATHNAME) != 0)
426 (!user_args && sudoers_args && !strcmp("\"\"", sudoers_args)) ||
428 fnmatch(sudoers_args, user_args ? user_args : "", 0) == 0)) {
431 safe_cmnd = estrdup(user_cmnd);
438 command_matches_glob(sudoers_cmnd, sudoers_args)
442 struct stat sudoers_stat;
444 char **ap, *base, *cp;
448 * First check to see if we can avoid the call to glob(3).
449 * Short circuit if there are no meta chars in the command itself
450 * and user_base and basename(sudoers_cmnd) don't match.
452 dlen = strlen(sudoers_cmnd);
453 if (sudoers_cmnd[dlen - 1] != '/') {
454 if ((base = strrchr(sudoers_cmnd, '/')) != NULL) {
456 if (!has_meta(base) && strcmp(user_base, base) != 0)
461 * Return true if we find a match in the glob(3) results AND
462 * a) there are no args in sudoers OR
463 * b) there are no args on command line and none required by sudoers OR
464 * c) there are args in sudoers and on command line and they match
467 #define GLOB_FLAGS (GLOB_NOSORT | GLOB_MARK | GLOB_BRACE | GLOB_TILDE)
468 if (glob(sudoers_cmnd, GLOB_FLAGS, NULL, &gl) != 0 || gl.gl_pathc == 0) {
472 /* For each glob match, compare basename, st_dev and st_ino. */
473 for (ap = gl.gl_pathv; (cp = *ap) != NULL; ap++) {
474 /* If it ends in '/' it is a directory spec. */
476 if (cp[dlen - 1] == '/') {
477 if (command_matches_dir(cp, dlen))
482 /* Only proceed if user_base and basename(cp) match */
483 if ((base = strrchr(cp, '/')) != NULL)
487 if (strcmp(user_base, base) != 0 ||
488 stat(cp, &sudoers_stat) == -1)
490 if (user_stat == NULL ||
491 (user_stat->st_dev == sudoers_stat.st_dev &&
492 user_stat->st_ino == sudoers_stat.st_ino)) {
494 safe_cmnd = estrdup(cp);
503 (!user_args && sudoers_args && !strcmp("\"\"", sudoers_args)) ||
505 fnmatch(sudoers_args, user_args ? user_args : "", 0) == 0)) {
507 safe_cmnd = estrdup(user_cmnd);
514 command_matches_normal(sudoers_cmnd, sudoers_args)
518 struct stat sudoers_stat;
522 /* If it ends in '/' it is a directory spec. */
523 dlen = strlen(sudoers_cmnd);
524 if (sudoers_cmnd[dlen - 1] == '/')
525 return(command_matches_dir(sudoers_cmnd, dlen));
527 /* Only proceed if user_base and basename(sudoers_cmnd) match */
528 if ((base = strrchr(sudoers_cmnd, '/')) == NULL)
532 if (strcmp(user_base, base) != 0 ||
533 stat(sudoers_cmnd, &sudoers_stat) == -1)
537 * Return true if inode/device matches AND
538 * a) there are no args in sudoers OR
539 * b) there are no args on command line and none req by sudoers OR
540 * c) there are args in sudoers and on command line and they match
542 if (user_stat != NULL &&
543 (user_stat->st_dev != sudoers_stat.st_dev ||
544 user_stat->st_ino != sudoers_stat.st_ino))
547 (!user_args && sudoers_args && !strcmp("\"\"", sudoers_args)) ||
549 fnmatch(sudoers_args, user_args ? user_args : "", 0) == 0)) {
551 safe_cmnd = estrdup(sudoers_cmnd);
558 * Return TRUE if user_cmnd names one of the inodes in dir, else FALSE.
561 command_matches_dir(sudoers_dir, dlen)
565 struct stat sudoers_stat;
571 * Grot through directory entries, looking for user_base.
573 dirp = opendir(sudoers_dir);
577 if (strlcpy(buf, sudoers_dir, sizeof(buf)) >= sizeof(buf)) {
581 while ((dent = readdir(dirp)) != NULL) {
582 /* ignore paths > PATH_MAX (XXX - log) */
584 if (strlcat(buf, dent->d_name, sizeof(buf)) >= sizeof(buf))
587 /* only stat if basenames are the same */
588 if (strcmp(user_base, dent->d_name) != 0 ||
589 stat(buf, &sudoers_stat) == -1)
591 if (user_stat->st_dev == sudoers_stat.st_dev &&
592 user_stat->st_ino == sudoers_stat.st_ino) {
594 safe_cmnd = estrdup(buf);
600 return(dent != NULL);
609 struct interface *ifp;
611 struct in6_addr addr6;
617 if (inet_pton(AF_INET6, n, &addr6) > 0) {
623 addr.s_addr = inet_addr(n);
626 for (i = 0; i < num_interfaces; i++) {
627 ifp = &interfaces[i];
628 if (ifp->family != family)
632 if (ifp->addr.ip4.s_addr == addr.s_addr ||
633 (ifp->addr.ip4.s_addr & ifp->netmask.ip4.s_addr)
639 if (memcmp(ifp->addr.ip6.s6_addr, addr6.s6_addr,
640 sizeof(addr6.s6_addr)) == 0)
642 for (j = 0; j < sizeof(addr6.s6_addr); j++) {
643 if ((ifp->addr.ip6.s6_addr[j] & ifp->netmask.ip6.s6_addr[j]) != addr6.s6_addr[j])
646 if (j == sizeof(addr6.s6_addr))
656 addr_matches_if_netmask(n, m)
661 struct in_addr addr, mask;
662 struct interface *ifp;
664 struct in6_addr addr6, mask6;
670 if (inet_pton(AF_INET6, n, &addr6) > 0)
676 addr.s_addr = inet_addr(n);
679 if (family == AF_INET) {
681 mask.s_addr = inet_addr(m);
684 mask.s_addr = 0xffffffff;
687 mask.s_addr = htonl(mask.s_addr);
692 if (inet_pton(AF_INET6, m, &mask6) <= 0) {
694 for (i = 0; i < 16; i++) {
696 mask6.s6_addr[i] = 0;
697 else if (i * 8 + 8 <= j)
698 mask6.s6_addr[i] = 0xff;
700 mask6.s6_addr[i] = 0xff00 >> (j - i * 8);
704 #endif /* HAVE_IN6_ADDR */
706 for (i = 0; i < num_interfaces; i++) {
707 ifp = &interfaces[i];
708 if (ifp->family != family)
712 if ((ifp->addr.ip4.s_addr & mask.s_addr) == addr.s_addr)
716 for (j = 0; j < sizeof(addr6.s6_addr); j++) {
717 if ((ifp->addr.ip6.s6_addr[j] & mask6.s6_addr[j]) != addr6.s6_addr[j])
720 if (j == sizeof(addr6.s6_addr))
722 #endif /* HAVE_IN6_ADDR */
730 * Returns TRUE if "n" is one of our ip addresses or if
731 * "n" is a network that we are on, else returns FALSE.
740 /* If there's an explicit netmask, use it. */
741 if ((m = strchr(n, '/'))) {
743 retval = addr_matches_if_netmask(n, m);
746 retval = addr_matches_if(n);
752 * Returns TRUE if the hostname matches the pattern, else FALSE
755 hostname_matches(shost, lhost, pattern)
760 if (has_meta(pattern)) {
761 if (strchr(pattern, '.'))
762 return(!fnmatch(pattern, lhost, FNM_CASEFOLD));
764 return(!fnmatch(pattern, shost, FNM_CASEFOLD));
766 if (strchr(pattern, '.'))
767 return(!strcasecmp(lhost, pattern));
769 return(!strcasecmp(shost, pattern));
774 * Returns TRUE if the user/uid from sudoers matches the specified user/uid,
775 * else returns FALSE.
778 userpw_matches(sudoers_user, user, pw)
783 if (pw != NULL && *sudoers_user == '#') {
784 uid_t uid = (uid_t) atoi(sudoers_user + 1);
785 if (uid == pw->pw_uid)
788 return(strcmp(sudoers_user, user) == 0);
792 * Returns TRUE if the group/gid from sudoers matches the specified group/gid,
793 * else returns FALSE.
796 group_matches(sudoers_group, gr)
800 if (*sudoers_group == '#') {
801 gid_t gid = (gid_t) atoi(sudoers_group + 1);
802 if (gid == gr->gr_gid)
805 return(strcmp(gr->gr_name, sudoers_group) == 0);
809 * Returns TRUE if the given user belongs to the named group,
810 * else returns FALSE.
813 usergr_matches(group, user, pw)
818 struct group *grp = NULL;
822 /* make sure we have a valid usergroup, sudo style */
826 #ifdef USING_NONUNIX_GROUPS
828 return(sudo_nonunix_groupcheck(++group, user, pw));
829 #endif /* USING_NONUNIX_GROUPS */
831 /* look up user's primary gid in the passwd file */
832 if (pw == NULL && (pw = sudo_getpwnam(user)) == NULL)
833 goto try_supplementary;
835 /* check against user's primary (passwd file) gid */
836 if ((grp = sudo_getgrnam(group)) == NULL)
837 goto try_supplementary;
838 if (grp->gr_gid == pw->pw_gid)
842 * If we are matching the invoking or list user and that user has a
843 * supplementary group vector, check it first.
845 if (strcmp(user, list_pw ? list_pw->pw_name : user_name) == 0) {
846 for (i = 0; i < user_ngroups; i++)
847 if (grp->gr_gid == user_groups[i])
852 if (grp != NULL && grp->gr_mem != NULL) {
853 for (cur = grp->gr_mem; *cur; cur++)
854 if (strcmp(*cur, user) == 0)
858 #ifdef USING_NONUNIX_GROUPS
859 /* not a Unix group, could be an AD group */
860 if (sudo_nonunix_groupcheck_available() &&
861 sudo_nonunix_groupcheck(group, user, pw))
863 #endif /* USING_NONUNIX_GROUPS */
869 * Returns TRUE if "host" and "user" belong to the netgroup "netgr",
870 * else return FALSE. Either of "host", "shost" or "user" may be NULL
871 * in which case that argument is not checked...
873 * XXX - swap order of host & shost
876 netgr_matches(netgr, lhost, shost, user)
883 #ifdef HAVE_GETDOMAINNAME
884 static int initialized;
887 /* make sure we have a valid netgroup, sudo style */
891 #ifdef HAVE_GETDOMAINNAME
892 /* get the domain name (if any) */
894 domain = (char *) emalloc(MAXHOSTNAMELEN + 1);
895 if (getdomainname(domain, MAXHOSTNAMELEN + 1) == -1 || *domain == '\0') {
901 #endif /* HAVE_GETDOMAINNAME */
904 if (innetgr(netgr, lhost, user, domain))
906 else if (lhost != shost && innetgr(netgr, shost, user, domain))
908 #endif /* HAVE_INNETGR */