2 * Copyright (c) 1996, 1998-2005, 2007-2010
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 "emul/fnmatch.h"
87 #endif /* HAVE_FNMATCH */
88 #ifndef HAVE_EXTENDED_GLOB
89 # include "emul/glob.h"
90 #endif /* HAVE_EXTENDED_GLOB */
91 #ifdef USING_NONUNIX_GROUPS
93 #endif /* USING_NONUNIX_GROUPS */
95 static struct member_list empty;
97 static int command_matches_dir __P((char *, size_t));
98 static int command_matches_glob __P((char *, char *));
99 static int command_matches_fnmatch __P((char *, char *));
100 static int command_matches_normal __P((char *, char *));
103 * Returns TRUE if string 's' contains meta characters.
105 #define has_meta(s) (strpbrk(s, "\\?*[]") != NULL)
108 * Check for user described by pw in a list of members.
109 * Returns ALLOW, DENY or UNSPEC.
112 _userlist_matches(pw, list)
114 struct member_list *list;
118 int rval, matched = UNSPEC;
120 tq_foreach_rev(list, m) {
123 matched = !m->negated;
126 if (netgr_matches(m->name, NULL, NULL, pw->pw_name))
127 matched = !m->negated;
130 if (usergr_matches(m->name, pw->pw_name, pw))
131 matched = !m->negated;
134 if ((a = alias_find(m->name, USERALIAS)) != NULL) {
135 rval = _userlist_matches(pw, &a->members);
137 matched = m->negated ? !rval : rval;
142 if (userpw_matches(m->name, pw->pw_name, pw))
143 matched = !m->negated;
146 if (matched != UNSPEC)
153 userlist_matches(pw, list)
155 struct member_list *list;
158 return(_userlist_matches(pw, list));
162 * Check for user described by pw in a list of members.
163 * If both lists are empty compare against def_runas_default.
164 * Returns ALLOW, DENY or UNSPEC.
167 _runaslist_matches(user_list, group_list)
168 struct member_list *user_list;
169 struct member_list *group_list;
174 int user_matched = UNSPEC;
175 int group_matched = UNSPEC;
177 if (tq_empty(user_list) && tq_empty(group_list))
178 return(userpw_matches(def_runas_default, runas_pw->pw_name, runas_pw));
180 if (runas_pw != NULL) {
181 tq_foreach_rev(user_list, m) {
184 user_matched = !m->negated;
187 if (netgr_matches(m->name, NULL, NULL, runas_pw->pw_name))
188 user_matched = !m->negated;
191 if (usergr_matches(m->name, runas_pw->pw_name, runas_pw))
192 user_matched = !m->negated;
195 if ((a = alias_find(m->name, RUNASALIAS)) != NULL) {
196 rval = _runaslist_matches(&a->members, &empty);
198 user_matched = m->negated ? !rval : rval;
203 if (userpw_matches(m->name, runas_pw->pw_name, runas_pw))
204 user_matched = !m->negated;
207 if (user_matched != UNSPEC)
212 if (runas_gr != NULL) {
213 if (user_matched == UNSPEC) {
214 if (runas_pw == NULL || strcmp(runas_pw->pw_name, user_name) == 0)
215 user_matched = ALLOW; /* only changing group */
217 tq_foreach_rev(group_list, m) {
220 group_matched = !m->negated;
223 if ((a = alias_find(m->name, RUNASALIAS)) != NULL) {
224 rval = _runaslist_matches(&a->members, &empty);
226 group_matched = m->negated ? !rval : rval;
231 if (group_matches(m->name, runas_gr))
232 group_matched = !m->negated;
235 if (group_matched != UNSPEC)
240 if (user_matched == DENY || group_matched == DENY)
242 if (user_matched == group_matched || runas_gr == NULL)
243 return(user_matched);
248 runaslist_matches(user_list, group_list)
249 struct member_list *user_list;
250 struct member_list *group_list;
253 return(_runaslist_matches(user_list ? user_list : &empty,
254 group_list ? group_list : &empty));
258 * Check for host and shost in a list of members.
259 * Returns ALLOW, DENY or UNSPEC.
262 _hostlist_matches(list)
263 struct member_list *list;
267 int rval, matched = UNSPEC;
269 tq_foreach_rev(list, m) {
272 matched = !m->negated;
275 if (netgr_matches(m->name, user_host, user_shost, NULL))
276 matched = !m->negated;
279 if (addr_matches(m->name))
280 matched = !m->negated;
283 if ((a = alias_find(m->name, HOSTALIAS)) != NULL) {
284 rval = _hostlist_matches(&a->members);
286 matched = m->negated ? !rval : rval;
291 if (hostname_matches(user_shost, user_host, m->name))
292 matched = !m->negated;
295 if (matched != UNSPEC)
302 hostlist_matches(list)
303 struct member_list *list;
306 return(_hostlist_matches(list));
310 * Check for cmnd and args in a list of members.
311 * Returns ALLOW, DENY or UNSPEC.
314 _cmndlist_matches(list)
315 struct member_list *list;
318 int matched = UNSPEC;
320 tq_foreach_rev(list, m) {
321 matched = cmnd_matches(m);
322 if (matched != UNSPEC)
329 cmndlist_matches(list)
330 struct member_list *list;
333 return(_cmndlist_matches(list));
337 * Check cmnd and args.
338 * Returns ALLOW, DENY or UNSPEC.
345 struct sudo_command *c;
346 int rval, matched = UNSPEC;
350 matched = !m->negated;
354 if ((a = alias_find(m->name, CMNDALIAS)) != NULL) {
355 rval = _cmndlist_matches(&a->members);
357 matched = m->negated ? !rval : rval;
361 c = (struct sudo_command *)m->name;
362 if (command_matches(c->cmnd, c->args))
363 matched = !m->negated;
370 * If path doesn't end in /, return TRUE iff cmnd & path name the same inode;
371 * otherwise, return TRUE if user_cmnd names one of the inodes in path.
374 command_matches(sudoers_cmnd, sudoers_args)
378 /* Check for pseudo-commands */
379 if (sudoers_cmnd[0] != '/') {
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);
400 if (has_meta(sudoers_cmnd)) {
402 * If sudoers_cmnd has meta characters in it, we need to
403 * use glob(3) and/or fnmatch(3) to do the matching.
406 return(command_matches_fnmatch(sudoers_cmnd, sudoers_args));
407 return(command_matches_glob(sudoers_cmnd, sudoers_args));
409 return(command_matches_normal(sudoers_cmnd, sudoers_args));
413 command_matches_fnmatch(sudoers_cmnd, sudoers_args)
418 * Return true if fnmatch(3) succeeds AND
419 * a) there are no args in sudoers OR
420 * b) there are no args on command line and none required by sudoers OR
421 * c) there are args in sudoers and on command line and they match
424 if (fnmatch(sudoers_cmnd, user_cmnd, FNM_PATHNAME) != 0)
427 (!user_args && sudoers_args && !strcmp("\"\"", sudoers_args)) ||
429 fnmatch(sudoers_args, user_args ? user_args : "", 0) == 0)) {
432 safe_cmnd = estrdup(user_cmnd);
439 command_matches_glob(sudoers_cmnd, sudoers_args)
443 struct stat sudoers_stat;
445 char **ap, *base, *cp;
449 * First check to see if we can avoid the call to glob(3).
450 * Short circuit if there are no meta chars in the command itself
451 * and user_base and basename(sudoers_cmnd) don't match.
453 dlen = strlen(sudoers_cmnd);
454 if (sudoers_cmnd[dlen - 1] != '/') {
455 if ((base = strrchr(sudoers_cmnd, '/')) != NULL) {
457 if (!has_meta(base) && strcmp(user_base, base) != 0)
462 * Return true if we find a match in the glob(3) results AND
463 * a) there are no args in sudoers OR
464 * b) there are no args on command line and none required by sudoers OR
465 * c) there are args in sudoers and on command line and they match
468 #define GLOB_FLAGS (GLOB_NOSORT | GLOB_MARK | GLOB_BRACE | GLOB_TILDE)
469 if (glob(sudoers_cmnd, GLOB_FLAGS, NULL, &gl) != 0 || gl.gl_pathc == 0) {
473 /* For each glob match, compare basename, st_dev and st_ino. */
474 for (ap = gl.gl_pathv; (cp = *ap) != NULL; ap++) {
475 /* If it ends in '/' it is a directory spec. */
477 if (cp[dlen - 1] == '/') {
478 if (command_matches_dir(cp, dlen))
483 /* Only proceed if user_base and basename(cp) match */
484 if ((base = strrchr(cp, '/')) != NULL)
488 if (strcmp(user_base, base) != 0 ||
489 stat(cp, &sudoers_stat) == -1)
491 if (user_stat == NULL ||
492 (user_stat->st_dev == sudoers_stat.st_dev &&
493 user_stat->st_ino == sudoers_stat.st_ino)) {
495 safe_cmnd = estrdup(cp);
504 (!user_args && sudoers_args && !strcmp("\"\"", sudoers_args)) ||
506 fnmatch(sudoers_args, user_args ? user_args : "", 0) == 0)) {
508 safe_cmnd = estrdup(user_cmnd);
515 command_matches_normal(sudoers_cmnd, 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))
548 (!user_args && sudoers_args && !strcmp("\"\"", sudoers_args)) ||
550 fnmatch(sudoers_args, user_args ? user_args : "", 0) == 0)) {
552 safe_cmnd = estrdup(sudoers_cmnd);
559 * Return TRUE if user_cmnd names one of the inodes in dir, else FALSE.
562 command_matches_dir(sudoers_dir, dlen)
566 struct stat sudoers_stat;
572 * Grot through directory entries, looking for user_base.
574 dirp = opendir(sudoers_dir);
578 if (strlcpy(buf, sudoers_dir, sizeof(buf)) >= sizeof(buf)) {
582 while ((dent = readdir(dirp)) != NULL) {
583 /* ignore paths > PATH_MAX (XXX - log) */
585 if (strlcat(buf, dent->d_name, sizeof(buf)) >= sizeof(buf))
588 /* only stat if basenames are the same */
589 if (strcmp(user_base, dent->d_name) != 0 ||
590 stat(buf, &sudoers_stat) == -1)
592 if (user_stat->st_dev == sudoers_stat.st_dev &&
593 user_stat->st_ino == sudoers_stat.st_ino) {
595 safe_cmnd = estrdup(buf);
601 return(dent != NULL);
609 union sudo_in_addr_un addr;
610 struct interface *ifp;
617 if (inet_pton(AF_INET6, n, &addr.ip6) > 0) {
623 addr.ip4.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.ip4.s_addr ||
633 (ifp->addr.ip4.s_addr & ifp->netmask.ip4.s_addr)
639 if (memcmp(ifp->addr.ip6.s6_addr, addr.ip6.s6_addr,
640 sizeof(addr.ip6.s6_addr)) == 0)
642 for (j = 0; j < sizeof(addr.ip6.s6_addr); j++) {
643 if ((ifp->addr.ip6.s6_addr[j] & ifp->netmask.ip6.s6_addr[j]) != addr.ip6.s6_addr[j])
646 if (j == sizeof(addr.ip6.s6_addr))
656 addr_matches_if_netmask(n, m)
661 union sudo_in_addr_un addr, mask;
662 struct interface *ifp;
669 if (inet_pton(AF_INET6, n, &addr.ip6) > 0)
675 addr.ip4.s_addr = inet_addr(n);
678 if (family == AF_INET) {
680 mask.ip4.s_addr = inet_addr(m);
683 mask.ip4.s_addr = 0xffffffff;
684 mask.ip4.s_addr >>= i;
685 mask.ip4.s_addr <<= i;
686 mask.ip4.s_addr = htonl(mask.ip4.s_addr);
691 if (inet_pton(AF_INET6, m, &mask.ip6) <= 0) {
693 for (i = 0; i < 16; i++) {
695 mask.ip6.s6_addr[i] = 0;
696 else if (i * 8 + 8 <= j)
697 mask.ip6.s6_addr[i] = 0xff;
699 mask.ip6.s6_addr[i] = 0xff00 >> (j - i * 8);
703 #endif /* HAVE_IN6_ADDR */
705 for (i = 0; i < num_interfaces; i++) {
706 ifp = &interfaces[i];
707 if (ifp->family != family)
711 if ((ifp->addr.ip4.s_addr & mask.ip4.s_addr) == addr.ip4.s_addr)
715 for (j = 0; j < sizeof(addr.ip6.s6_addr); j++) {
716 if ((ifp->addr.ip6.s6_addr[j] & mask.ip6.s6_addr[j]) != addr.ip6.s6_addr[j])
719 if (j == sizeof(addr.ip6.s6_addr))
721 #endif /* HAVE_IN6_ADDR */
729 * Returns TRUE if "n" is one of our ip addresses or if
730 * "n" is a network that we are on, else returns FALSE.
739 /* If there's an explicit netmask, use it. */
740 if ((m = strchr(n, '/'))) {
742 retval = addr_matches_if_netmask(n, m);
745 retval = addr_matches_if(n);
751 * Returns TRUE if the hostname matches the pattern, else FALSE
754 hostname_matches(shost, lhost, pattern)
759 if (has_meta(pattern)) {
760 if (strchr(pattern, '.'))
761 return(!fnmatch(pattern, lhost, FNM_CASEFOLD));
763 return(!fnmatch(pattern, shost, FNM_CASEFOLD));
765 if (strchr(pattern, '.'))
766 return(!strcasecmp(lhost, pattern));
768 return(!strcasecmp(shost, pattern));
773 * Returns TRUE if the user/uid from sudoers matches the specified user/uid,
774 * else returns FALSE.
777 userpw_matches(sudoers_user, user, pw)
782 if (pw != NULL && *sudoers_user == '#') {
783 uid_t uid = (uid_t) atoi(sudoers_user + 1);
784 if (uid == pw->pw_uid)
787 return(strcmp(sudoers_user, user) == 0);
791 * Returns TRUE if the group/gid from sudoers matches the specified group/gid,
792 * else returns FALSE.
795 group_matches(sudoers_group, gr)
799 if (*sudoers_group == '#') {
800 gid_t gid = (gid_t) atoi(sudoers_group + 1);
801 if (gid == gr->gr_gid)
804 return(strcmp(gr->gr_name, sudoers_group) == 0);
808 * Returns TRUE if the given user belongs to the named group,
809 * else returns FALSE.
812 usergr_matches(group, user, pw)
817 /* make sure we have a valid usergroup, sudo style */
821 #ifdef USING_NONUNIX_GROUPS
823 return(sudo_nonunix_groupcheck(++group, user, pw));
824 #endif /* USING_NONUNIX_GROUPS */
826 /* look up user's primary gid in the passwd file */
827 if (pw == NULL && (pw = sudo_getpwnam(user)) == NULL)
830 if (user_in_group(pw, group))
833 #ifdef USING_NONUNIX_GROUPS
834 /* not a Unix group, could be an AD group */
835 if (sudo_nonunix_groupcheck_available() &&
836 sudo_nonunix_groupcheck(group, user, pw))
838 #endif /* USING_NONUNIX_GROUPS */
844 * Returns TRUE if "host" and "user" belong to the netgroup "netgr",
845 * else return FALSE. Either of "host", "shost" or "user" may be NULL
846 * in which case that argument is not checked...
848 * XXX - swap order of host & shost
851 netgr_matches(netgr, lhost, shost, user)
858 #ifdef HAVE_GETDOMAINNAME
859 static int initialized;
862 /* make sure we have a valid netgroup, sudo style */
866 #ifdef HAVE_GETDOMAINNAME
867 /* get the domain name (if any) */
869 domain = (char *) emalloc(MAXHOSTNAMELEN + 1);
870 if (getdomainname(domain, MAXHOSTNAMELEN + 1) == -1 || *domain == '\0') {
876 #endif /* HAVE_GETDOMAINNAME */
879 if (innetgr(netgr, lhost, user, domain))
881 else if (lhost != shost && innetgr(netgr, shost, user, domain))
883 #endif /* HAVE_INNETGR */