2 * Copyright (c) 1996, 1998-2005, 2007-2011
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 */
41 #endif /* HAVE_STRING_H */
44 #endif /* HAVE_STRINGS_H */
47 #endif /* HAVE_UNISTD_H */
50 #endif /* HAVE_FNMATCH */
51 #ifdef HAVE_EXTENDED_GLOB
53 #endif /* HAVE_EXTENDED_GLOB */
54 #ifdef HAVE_NETGROUP_H
55 # include <netgroup.h>
56 #endif /* HAVE_NETGROUP_H */
60 #include <netinet/in.h>
61 #include <arpa/inet.h>
65 # define NAMLEN(dirent) strlen((dirent)->d_name)
67 # define dirent direct
68 # define NAMLEN(dirent) (dirent)->d_namlen
69 # ifdef HAVE_SYS_NDIR_H
70 # include <sys/ndir.h>
72 # ifdef HAVE_SYS_DIR_H
81 #include "interfaces.h"
86 # include "compat/fnmatch.h"
87 #endif /* HAVE_FNMATCH */
88 #ifndef HAVE_EXTENDED_GLOB
89 # include "compat/glob.h"
90 #endif /* HAVE_EXTENDED_GLOB */
92 static struct member_list empty;
94 static int command_matches_dir(char *, size_t);
95 static int command_matches_glob(char *, char *);
96 static int command_matches_fnmatch(char *, char *);
97 static int command_matches_normal(char *, char *);
100 * Returns TRUE if string 's' contains meta characters.
102 #define has_meta(s) (strpbrk(s, "\\?*[]") != NULL)
105 * Check for user described by pw in a list of members.
106 * Returns ALLOW, DENY or UNSPEC.
109 _userlist_matches(struct passwd *pw, struct member_list *list)
113 int rval, matched = UNSPEC;
115 tq_foreach_rev(list, m) {
118 matched = !m->negated;
121 if (netgr_matches(m->name, NULL, NULL, pw->pw_name))
122 matched = !m->negated;
125 if (usergr_matches(m->name, pw->pw_name, pw))
126 matched = !m->negated;
129 if ((a = alias_find(m->name, USERALIAS)) != NULL) {
130 rval = _userlist_matches(pw, &a->members);
132 matched = m->negated ? !rval : rval;
137 if (userpw_matches(m->name, pw->pw_name, pw))
138 matched = !m->negated;
141 if (matched != UNSPEC)
148 userlist_matches(struct passwd *pw, struct member_list *list)
151 return _userlist_matches(pw, list);
155 * Check for user described by pw in a list of members.
156 * If both lists are empty compare against def_runas_default.
157 * Returns ALLOW, DENY or UNSPEC.
160 _runaslist_matches(struct member_list *user_list, struct member_list *group_list)
165 int user_matched = UNSPEC;
166 int group_matched = UNSPEC;
168 if (runas_pw != NULL) {
169 /* If no runas user or runas group listed in sudoers, use default. */
170 if (tq_empty(user_list) && tq_empty(group_list))
171 return userpw_matches(def_runas_default, runas_pw->pw_name, runas_pw);
173 tq_foreach_rev(user_list, m) {
176 user_matched = !m->negated;
179 if (netgr_matches(m->name, NULL, NULL, runas_pw->pw_name))
180 user_matched = !m->negated;
183 if (usergr_matches(m->name, runas_pw->pw_name, runas_pw))
184 user_matched = !m->negated;
187 if ((a = alias_find(m->name, RUNASALIAS)) != NULL) {
188 rval = _runaslist_matches(&a->members, &empty);
190 user_matched = m->negated ? !rval : rval;
195 if (userpw_matches(m->name, runas_pw->pw_name, runas_pw))
196 user_matched = !m->negated;
199 if (user_matched != UNSPEC)
204 if (runas_gr != NULL) {
205 if (user_matched == UNSPEC) {
206 if (runas_pw == NULL || strcmp(runas_pw->pw_name, user_name) == 0)
207 user_matched = ALLOW; /* only changing group */
209 tq_foreach_rev(group_list, m) {
212 group_matched = !m->negated;
215 if ((a = alias_find(m->name, RUNASALIAS)) != NULL) {
216 rval = _runaslist_matches(&a->members, &empty);
218 group_matched = m->negated ? !rval : rval;
223 if (group_matches(m->name, runas_gr))
224 group_matched = !m->negated;
227 if (group_matched != UNSPEC)
230 if (group_matched == UNSPEC) {
231 if (runas_pw != NULL && runas_pw->pw_gid == runas_gr->gr_gid)
232 group_matched = ALLOW; /* runas group matches passwd db */
236 if (user_matched == DENY || group_matched == DENY)
238 if (user_matched == group_matched || runas_gr == NULL)
244 runaslist_matches(struct member_list *user_list, struct member_list *group_list)
247 return _runaslist_matches(user_list ? user_list : &empty,
248 group_list ? group_list : &empty);
252 * Check for host and shost in a list of members.
253 * Returns ALLOW, DENY or UNSPEC.
256 _hostlist_matches(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 = alias_find(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(struct member_list *list)
298 return _hostlist_matches(list);
302 * Check for cmnd and args in a list of members.
303 * Returns ALLOW, DENY or UNSPEC.
306 _cmndlist_matches(struct member_list *list)
309 int matched = UNSPEC;
311 tq_foreach_rev(list, m) {
312 matched = cmnd_matches(m);
313 if (matched != UNSPEC)
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;
339 matched = !m->negated;
343 if ((a = alias_find(m->name, CMNDALIAS)) != NULL) {
344 rval = _cmndlist_matches(&a->members);
346 matched = m->negated ? !rval : rval;
350 c = (struct sudo_command *)m->name;
351 if (command_matches(c->cmnd, c->args))
352 matched = !m->negated;
359 command_args_match(sudoers_cmnd, sudoers_args)
366 * If no args specified in sudoers, any user args are allowed.
367 * If the empty string is specified in sudoers, no user args are allowed.
370 (!user_args && sudoers_args && !strcmp("\"\"", sudoers_args)))
373 * If args are specified in sudoers, they must match the user args.
374 * If running as sudoedit, all args are assumed to be paths.
377 /* For sudoedit, all args are assumed to be pathnames. */
378 if (strcmp(sudoers_cmnd, "sudoedit") == 0)
379 flags = FNM_PATHNAME;
380 if (fnmatch(sudoers_args, user_args ? user_args : "", flags) == 0)
387 * If path doesn't end in /, return TRUE iff cmnd & path name the same inode;
388 * otherwise, return TRUE if user_cmnd names one of the inodes in path.
391 command_matches(char *sudoers_cmnd, char *sudoers_args)
393 /* Check for pseudo-commands */
394 if (sudoers_cmnd[0] != '/') {
396 * Return true if both sudoers_cmnd and user_cmnd are "sudoedit" AND
397 * a) there are no args in sudoers OR
398 * b) there are no args on command line and none req by sudoers OR
399 * c) there are args in sudoers and on command line and they match
401 if (strcmp(sudoers_cmnd, "sudoedit") != 0 ||
402 strcmp(user_cmnd, "sudoedit") != 0)
404 if (command_args_match(sudoers_cmnd, sudoers_args)) {
406 safe_cmnd = estrdup(sudoers_cmnd);
412 if (has_meta(sudoers_cmnd)) {
414 * If sudoers_cmnd has meta characters in it, we need to
415 * use glob(3) and/or fnmatch(3) to do the matching.
418 return command_matches_fnmatch(sudoers_cmnd, sudoers_args);
419 return command_matches_glob(sudoers_cmnd, sudoers_args);
421 return command_matches_normal(sudoers_cmnd, sudoers_args);
425 command_matches_fnmatch(char *sudoers_cmnd, char *sudoers_args)
428 * Return true if fnmatch(3) succeeds AND
429 * a) there are no args in sudoers OR
430 * b) there are no args on command line and none required by sudoers OR
431 * c) there are args in sudoers and on command line and they match
434 if (fnmatch(sudoers_cmnd, user_cmnd, FNM_PATHNAME) != 0)
436 if (command_args_match(sudoers_cmnd, sudoers_args)) {
439 safe_cmnd = estrdup(user_cmnd);
446 command_matches_glob(char *sudoers_cmnd, char *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 || gl.gl_pathc == 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);
508 if (command_args_match(sudoers_cmnd, sudoers_args)) {
510 safe_cmnd = estrdup(user_cmnd);
517 command_matches_normal(char *sudoers_cmnd, char *sudoers_args)
519 struct stat sudoers_stat;
523 /* If it ends in '/' it is a directory spec. */
524 dlen = strlen(sudoers_cmnd);
525 if (sudoers_cmnd[dlen - 1] == '/')
526 return command_matches_dir(sudoers_cmnd, dlen);
528 /* Only proceed if user_base and basename(sudoers_cmnd) match */
529 if ((base = strrchr(sudoers_cmnd, '/')) == NULL)
533 if (strcmp(user_base, base) != 0 ||
534 stat(sudoers_cmnd, &sudoers_stat) == -1)
538 * Return true if inode/device matches AND
539 * a) there are no args in sudoers OR
540 * b) there are no args on command line and none req by sudoers OR
541 * c) there are args in sudoers and on command line and they match
543 if (user_stat != NULL &&
544 (user_stat->st_dev != sudoers_stat.st_dev ||
545 user_stat->st_ino != sudoers_stat.st_ino))
547 if (command_args_match(sudoers_cmnd, sudoers_args)) {
549 safe_cmnd = estrdup(sudoers_cmnd);
556 * Return TRUE if user_cmnd names one of the inodes in dir, else FALSE.
559 command_matches_dir(char *sudoers_dir, size_t dlen)
561 struct stat sudoers_stat;
567 * Grot through directory entries, looking for user_base.
569 dirp = opendir(sudoers_dir);
573 if (strlcpy(buf, sudoers_dir, sizeof(buf)) >= sizeof(buf)) {
577 while ((dent = readdir(dirp)) != NULL) {
578 /* ignore paths > PATH_MAX (XXX - log) */
580 if (strlcat(buf, dent->d_name, sizeof(buf)) >= sizeof(buf))
583 /* only stat if basenames are the same */
584 if (strcmp(user_base, dent->d_name) != 0 ||
585 stat(buf, &sudoers_stat) == -1)
587 if (user_stat == NULL ||
588 (user_stat->st_dev == sudoers_stat.st_dev &&
589 user_stat->st_ino == sudoers_stat.st_ino)) {
591 safe_cmnd = estrdup(buf);
601 addr_matches_if(char *n)
603 union sudo_in_addr_un addr;
604 struct interface *ifp;
611 if (inet_pton(AF_INET6, n, &addr.ip6) > 0) {
617 addr.ip4.s_addr = inet_addr(n);
620 for (ifp = interfaces; ifp != NULL; ifp = ifp->next) {
621 if (ifp->family != family)
625 if (ifp->addr.ip4.s_addr == addr.ip4.s_addr ||
626 (ifp->addr.ip4.s_addr & ifp->netmask.ip4.s_addr)
632 if (memcmp(ifp->addr.ip6.s6_addr, addr.ip6.s6_addr,
633 sizeof(addr.ip6.s6_addr)) == 0)
635 for (j = 0; j < sizeof(addr.ip6.s6_addr); j++) {
636 if ((ifp->addr.ip6.s6_addr[j] & ifp->netmask.ip6.s6_addr[j]) != addr.ip6.s6_addr[j])
639 if (j == sizeof(addr.ip6.s6_addr))
649 addr_matches_if_netmask(char *n, char *m)
652 union sudo_in_addr_un addr, mask;
653 struct interface *ifp;
660 if (inet_pton(AF_INET6, n, &addr.ip6) > 0)
666 addr.ip4.s_addr = inet_addr(n);
669 if (family == AF_INET) {
671 mask.ip4.s_addr = inet_addr(m);
674 mask.ip4.s_addr = 0xffffffff;
675 mask.ip4.s_addr >>= i;
676 mask.ip4.s_addr <<= i;
677 mask.ip4.s_addr = htonl(mask.ip4.s_addr);
682 if (inet_pton(AF_INET6, m, &mask.ip6) <= 0) {
684 for (i = 0; i < 16; i++) {
686 mask.ip6.s6_addr[i] = 0;
687 else if (i * 8 + 8 <= j)
688 mask.ip6.s6_addr[i] = 0xff;
690 mask.ip6.s6_addr[i] = 0xff00 >> (j - i * 8);
694 #endif /* HAVE_IN6_ADDR */
696 for (ifp = interfaces; ifp != NULL; ifp = ifp->next) {
697 if (ifp->family != family)
701 if ((ifp->addr.ip4.s_addr & mask.ip4.s_addr) == addr.ip4.s_addr)
705 for (j = 0; j < sizeof(addr.ip6.s6_addr); j++) {
706 if ((ifp->addr.ip6.s6_addr[j] & mask.ip6.s6_addr[j]) != addr.ip6.s6_addr[j])
709 if (j == sizeof(addr.ip6.s6_addr))
711 #endif /* HAVE_IN6_ADDR */
719 * Returns TRUE if "n" is one of our ip addresses or if
720 * "n" is a network that we are on, else returns FALSE.
723 addr_matches(char *n)
728 /* If there's an explicit netmask, use it. */
729 if ((m = strchr(n, '/'))) {
731 retval = addr_matches_if_netmask(n, m);
734 retval = addr_matches_if(n);
740 * Returns TRUE if the hostname matches the pattern, else FALSE
743 hostname_matches(char *shost, char *lhost, char *pattern)
745 if (has_meta(pattern)) {
746 if (strchr(pattern, '.'))
747 return !fnmatch(pattern, lhost, FNM_CASEFOLD);
749 return !fnmatch(pattern, shost, FNM_CASEFOLD);
751 if (strchr(pattern, '.'))
752 return !strcasecmp(lhost, pattern);
754 return !strcasecmp(shost, pattern);
759 * Returns TRUE if the user/uid from sudoers matches the specified user/uid,
760 * else returns FALSE.
763 userpw_matches(char *sudoers_user, char *user, struct passwd *pw)
765 if (pw != NULL && *sudoers_user == '#') {
766 uid_t uid = (uid_t) atoi(sudoers_user + 1);
767 if (uid == pw->pw_uid)
770 return strcmp(sudoers_user, user) == 0;
774 * Returns TRUE if the group/gid from sudoers matches the specified group/gid,
775 * else returns FALSE.
778 group_matches(char *sudoers_group, struct group *gr)
780 if (*sudoers_group == '#') {
781 gid_t gid = (gid_t) atoi(sudoers_group + 1);
782 if (gid == gr->gr_gid)
785 return strcmp(gr->gr_name, sudoers_group) == 0;
789 * Returns TRUE if the given user belongs to the named group,
790 * else returns FALSE.
793 usergr_matches(char *group, char *user, struct passwd *pw)
796 struct passwd *pw0 = NULL;
798 /* make sure we have a valid usergroup, sudo style */
802 if (*group == ':' && def_group_plugin) {
803 matched = group_plugin_query(user, group + 1, pw);
807 /* look up user's primary gid in the passwd file */
809 if ((pw0 = sudo_getpwnam(user)) == NULL)
814 if (user_in_group(pw, group)) {
819 /* not a Unix group, could be an external group */
820 if (def_group_plugin && group_plugin_query(user, group, pw)) {
833 * Returns TRUE if "host" and "user" belong to the netgroup "netgr",
834 * else return FALSE. Either of "host", "shost" or "user" may be NULL
835 * in which case that argument is not checked...
837 * XXX - swap order of host & shost
840 netgr_matches(char *netgr, char *lhost, char *shost, char *user)
843 #ifdef HAVE_GETDOMAINNAME
844 static int initialized;
847 /* make sure we have a valid netgroup, sudo style */
851 #ifdef HAVE_GETDOMAINNAME
852 /* get the domain name (if any) */
854 domain = (char *) emalloc(MAXHOSTNAMELEN + 1);
855 if (getdomainname(domain, MAXHOSTNAMELEN + 1) == -1 || *domain == '\0') {
861 #endif /* HAVE_GETDOMAINNAME */
864 if (innetgr(netgr, lhost, user, domain))
866 else if (lhost != shost && innetgr(netgr, shost, user, domain))
868 #endif /* HAVE_INNETGR */