2004-10-26 22:14:01 +00:00
|
|
|
/*
|
2019-04-29 07:21:51 -06:00
|
|
|
* SPDX-License-Identifier: ISC
|
|
|
|
*
|
2023-03-03 13:57:27 -07:00
|
|
|
* Copyright (c) 1996, 1998-2005, 2007-2023
|
2017-12-03 17:53:40 -07:00
|
|
|
* Todd C. Miller <Todd.Miller@sudo.ws>
|
2004-10-26 22:14:01 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
* Sponsored in part by the Defense Advanced Research Projects
|
|
|
|
* Agency (DARPA) and Air Force Research Laboratory, Air Force
|
|
|
|
* Materiel Command, USAF, under agreement number F39502-99-1-0512.
|
|
|
|
*/
|
|
|
|
|
2018-10-26 08:39:09 -06:00
|
|
|
/*
|
|
|
|
* This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
|
|
|
* PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
|
|
|
*/
|
2018-10-21 08:46:05 -06:00
|
|
|
|
2004-11-19 18:39:14 +00:00
|
|
|
#include <config.h>
|
2004-10-26 22:14:01 +00:00
|
|
|
|
|
|
|
#include <sys/stat.h>
|
2015-02-03 07:33:24 -07:00
|
|
|
#ifdef HAVE_SYS_SYSTEMINFO_H
|
|
|
|
# include <sys/systeminfo.h>
|
|
|
|
#endif
|
2004-10-26 22:14:01 +00:00
|
|
|
#include <stdio.h>
|
2015-06-19 14:29:27 -06:00
|
|
|
#include <stdlib.h>
|
2020-05-18 07:59:24 -06:00
|
|
|
#include <string.h>
|
2010-06-29 13:08:05 -04:00
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif /* HAVE_STRINGS_H */
|
2015-07-02 09:08:28 -06:00
|
|
|
#include <unistd.h>
|
2004-10-26 22:14:01 +00:00
|
|
|
#ifdef HAVE_NETGROUP_H
|
|
|
|
# include <netgroup.h>
|
2013-03-06 17:05:23 -05:00
|
|
|
#else
|
|
|
|
# include <netdb.h>
|
2004-10-26 22:14:01 +00:00
|
|
|
#endif /* HAVE_NETGROUP_H */
|
2015-07-02 09:24:48 -06:00
|
|
|
#include <dirent.h>
|
2016-01-04 10:35:18 -07:00
|
|
|
#include <fcntl.h>
|
2013-04-14 07:00:21 -04:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <errno.h>
|
2014-06-26 15:51:15 -06:00
|
|
|
#ifdef HAVE_FNMATCH
|
|
|
|
# include <fnmatch.h>
|
|
|
|
#else
|
|
|
|
# include "compat/fnmatch.h"
|
|
|
|
#endif /* HAVE_FNMATCH */
|
|
|
|
|
2020-08-12 10:07:07 -06:00
|
|
|
#include "sudoers.h"
|
|
|
|
#include <gram.h>
|
|
|
|
|
2018-04-04 09:51:05 -06:00
|
|
|
/*
|
|
|
|
* Check whether user described by pw matches member.
|
|
|
|
* Returns ALLOW, DENY or UNSPEC.
|
|
|
|
*/
|
|
|
|
int
|
2023-03-17 13:31:36 -06:00
|
|
|
user_matches(const struct sudoers_parse_tree *parse_tree,
|
|
|
|
const struct passwd *pw, const struct member *m)
|
2018-04-04 09:51:05 -06:00
|
|
|
{
|
2019-08-15 14:20:12 -06:00
|
|
|
const char *lhost = parse_tree->lhost ? parse_tree->lhost : user_runhost;
|
|
|
|
const char *shost = parse_tree->shost ? parse_tree->shost : user_srunhost;
|
2018-04-04 09:51:05 -06:00
|
|
|
int matched = UNSPEC;
|
2019-08-15 14:20:12 -06:00
|
|
|
struct alias *a;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(user_matches, SUDOERS_DEBUG_MATCH);
|
2018-04-04 09:51:05 -06:00
|
|
|
|
|
|
|
switch (m->type) {
|
|
|
|
case ALL:
|
|
|
|
matched = !m->negated;
|
|
|
|
break;
|
|
|
|
case NETGROUP:
|
2023-03-08 13:44:22 -07:00
|
|
|
if (netgr_matches(parse_tree->nss, m->name,
|
2019-08-15 14:20:12 -06:00
|
|
|
def_netgroup_tuple ? lhost : NULL,
|
|
|
|
def_netgroup_tuple ? shost : NULL, pw->pw_name))
|
2018-04-04 09:51:05 -06:00
|
|
|
matched = !m->negated;
|
|
|
|
break;
|
|
|
|
case USERGROUP:
|
|
|
|
if (usergr_matches(m->name, pw->pw_name, pw))
|
|
|
|
matched = !m->negated;
|
|
|
|
break;
|
|
|
|
case ALIAS:
|
2018-07-26 15:12:33 -06:00
|
|
|
if ((a = alias_get(parse_tree, m->name, USERALIAS)) != NULL) {
|
2018-04-15 08:14:46 -06:00
|
|
|
/* XXX */
|
2018-07-26 15:12:33 -06:00
|
|
|
int rc = userlist_matches(parse_tree, pw, &a->members);
|
2018-04-04 09:51:05 -06:00
|
|
|
if (rc != UNSPEC)
|
|
|
|
matched = m->negated ? !rc : rc;
|
|
|
|
alias_put(a);
|
|
|
|
break;
|
|
|
|
}
|
2020-08-01 13:10:50 -06:00
|
|
|
FALLTHROUGH;
|
2018-04-04 09:51:05 -06:00
|
|
|
case WORD:
|
|
|
|
if (userpw_matches(m->name, pw->pw_name, pw))
|
|
|
|
matched = !m->negated;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
debug_return_int(matched);
|
|
|
|
}
|
|
|
|
|
2004-10-26 22:14:01 +00:00
|
|
|
/*
|
|
|
|
* Check for user described by pw in a list of members.
|
|
|
|
* Returns ALLOW, DENY or UNSPEC.
|
|
|
|
*/
|
2013-05-22 11:32:08 -04:00
|
|
|
int
|
2023-03-17 13:31:36 -06:00
|
|
|
userlist_matches(const struct sudoers_parse_tree *parse_tree,
|
|
|
|
const struct passwd *pw, const struct member_list *list)
|
2004-10-26 22:14:01 +00:00
|
|
|
{
|
|
|
|
struct member *m;
|
2018-04-04 09:51:05 -06:00
|
|
|
int matched = UNSPEC;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(userlist_matches, SUDOERS_DEBUG_MATCH);
|
2004-10-26 22:14:01 +00:00
|
|
|
|
2013-10-22 09:08:38 -06:00
|
|
|
TAILQ_FOREACH_REVERSE(m, list, member_list, entries) {
|
2018-07-26 15:12:33 -06:00
|
|
|
if ((matched = user_matches(parse_tree, pw, m)) != UNSPEC)
|
2007-08-31 01:21:26 +00:00
|
|
|
break;
|
2004-10-26 22:14:01 +00:00
|
|
|
}
|
2015-05-07 10:33:23 -06:00
|
|
|
debug_return_int(matched);
|
2004-10-26 22:14:01 +00:00
|
|
|
}
|
|
|
|
|
2018-10-27 06:37:34 -06:00
|
|
|
struct gid_list *
|
|
|
|
runas_getgroups(void)
|
|
|
|
{
|
|
|
|
const struct passwd *pw;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(runas_getgroups, SUDOERS_DEBUG_MATCH);
|
2018-10-27 06:37:34 -06:00
|
|
|
|
|
|
|
if (def_preserve_groups) {
|
|
|
|
sudo_gidlist_addref(user_gid_list);
|
|
|
|
debug_return_ptr(user_gid_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Only use results from a group db query, not the front end. */
|
|
|
|
pw = runas_pw ? runas_pw : sudo_user.pw;
|
|
|
|
debug_return_ptr(sudo_get_gidlist(pw, ENTRY_TYPE_QUERIED));
|
|
|
|
}
|
|
|
|
|
2004-10-26 22:14:01 +00:00
|
|
|
/*
|
2023-06-13 20:13:24 -06:00
|
|
|
* Check whether the requested runas user matches user_list, the
|
|
|
|
* user portion of a sudoers runaslist. If user_list is NULL, a
|
|
|
|
* list containing runas_default is used.
|
2004-10-26 22:14:01 +00:00
|
|
|
* Returns ALLOW, DENY or UNSPEC.
|
|
|
|
*/
|
2023-06-13 20:13:24 -06:00
|
|
|
static int
|
|
|
|
runas_userlist_matches(const struct sudoers_parse_tree *parse_tree,
|
|
|
|
const struct member_list *user_list, struct member **matching_user)
|
2004-10-26 22:14:01 +00:00
|
|
|
{
|
2019-08-15 14:20:12 -06:00
|
|
|
const char *lhost = parse_tree->lhost ? parse_tree->lhost : user_runhost;
|
|
|
|
const char *shost = parse_tree->shost ? parse_tree->shost : user_srunhost;
|
|
|
|
int user_matched = UNSPEC;
|
2004-11-15 17:33:52 +00:00
|
|
|
struct member *m;
|
2004-11-16 16:10:09 +00:00
|
|
|
struct alias *a;
|
2023-06-13 20:13:24 -06:00
|
|
|
debug_decl(runas_userlist_matches, SUDOERS_DEBUG_MATCH);
|
2007-11-21 20:12:00 +00:00
|
|
|
|
2023-06-13 10:29:00 -06:00
|
|
|
TAILQ_FOREACH_REVERSE(m, user_list, member_list, entries) {
|
|
|
|
switch (m->type) {
|
|
|
|
case ALL:
|
|
|
|
user_matched = !m->negated;
|
|
|
|
break;
|
|
|
|
case NETGROUP:
|
|
|
|
if (netgr_matches(parse_tree->nss, m->name,
|
|
|
|
def_netgroup_tuple ? lhost : NULL,
|
|
|
|
def_netgroup_tuple ? shost : NULL,
|
|
|
|
runas_pw->pw_name))
|
2023-06-13 16:03:25 -06:00
|
|
|
user_matched = !m->negated;
|
2023-06-13 10:29:00 -06:00
|
|
|
break;
|
|
|
|
case USERGROUP:
|
|
|
|
if (usergr_matches(m->name, runas_pw->pw_name, runas_pw))
|
|
|
|
user_matched = !m->negated;
|
|
|
|
break;
|
|
|
|
case ALIAS:
|
|
|
|
a = alias_get(parse_tree, m->name, RUNASALIAS);
|
|
|
|
if (a != NULL) {
|
2023-06-13 20:13:24 -06:00
|
|
|
const int rc = runas_userlist_matches(parse_tree,
|
|
|
|
&a->members, matching_user);
|
2023-06-13 10:29:00 -06:00
|
|
|
if (rc != UNSPEC)
|
|
|
|
user_matched = m->negated ? !rc : rc;
|
|
|
|
alias_put(a);
|
2023-06-13 16:03:25 -06:00
|
|
|
break;
|
2023-06-13 10:29:00 -06:00
|
|
|
}
|
|
|
|
FALLTHROUGH;
|
|
|
|
case WORD:
|
|
|
|
if (userpw_matches(m->name, runas_pw->pw_name, runas_pw))
|
|
|
|
user_matched = !m->negated;
|
2023-06-13 16:03:25 -06:00
|
|
|
break;
|
2023-06-13 10:29:00 -06:00
|
|
|
case MYSELF:
|
2023-07-15 08:44:57 -06:00
|
|
|
/*
|
|
|
|
* Only match a rule with an empty runas user if a group
|
|
|
|
* was specified on the command line without a user _or_
|
|
|
|
* the user specified their own name on the command line.
|
|
|
|
*/
|
|
|
|
if ((!ISSET(sudo_user.flags, RUNAS_USER_SPECIFIED) &&
|
|
|
|
ISSET(sudo_user.flags, RUNAS_GROUP_SPECIFIED)) ||
|
|
|
|
strcmp(user_name, runas_pw->pw_name) == 0)
|
2023-06-13 10:29:00 -06:00
|
|
|
user_matched = !m->negated;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (user_matched != UNSPEC) {
|
|
|
|
if (matching_user != NULL && m->type != ALIAS)
|
|
|
|
*matching_user = m;
|
|
|
|
break;
|
2007-11-21 20:12:00 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-13 20:13:24 -06:00
|
|
|
debug_return_int(user_matched);
|
|
|
|
}
|
2007-11-21 20:12:00 +00:00
|
|
|
|
2023-06-13 20:13:24 -06:00
|
|
|
/*
|
|
|
|
* Check whether the requested runas group matches group_list, the
|
|
|
|
* group portion of a sudoers runaslist, or the runas user's groups.
|
|
|
|
* Returns ALLOW, DENY or UNSPEC.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
runas_grouplist_matches(const struct sudoers_parse_tree *parse_tree,
|
|
|
|
const struct member_list *group_list, struct member **matching_group)
|
|
|
|
{
|
|
|
|
int group_matched = UNSPEC;
|
|
|
|
struct member *m;
|
|
|
|
struct alias *a;
|
|
|
|
debug_decl(runas_grouplist_matches, SUDOERS_DEBUG_MATCH);
|
|
|
|
|
|
|
|
if (group_list != NULL) {
|
|
|
|
TAILQ_FOREACH_REVERSE(m, group_list, member_list, entries) {
|
|
|
|
switch (m->type) {
|
|
|
|
case ALL:
|
|
|
|
group_matched = !m->negated;
|
|
|
|
break;
|
|
|
|
case ALIAS:
|
|
|
|
a = alias_get(parse_tree, m->name, RUNASALIAS);
|
|
|
|
if (a != NULL) {
|
|
|
|
const int rc = runas_grouplist_matches(parse_tree,
|
|
|
|
&a->members, matching_group);
|
|
|
|
if (rc != UNSPEC)
|
|
|
|
group_matched = m->negated ? !rc : rc;
|
|
|
|
alias_put(a);
|
2013-10-22 09:08:38 -06:00
|
|
|
break;
|
2023-06-13 20:13:24 -06:00
|
|
|
}
|
|
|
|
FALLTHROUGH;
|
|
|
|
case WORD:
|
|
|
|
if (group_matches(m->name, runas_gr))
|
|
|
|
group_matched = !m->negated;
|
2007-11-21 20:12:00 +00:00
|
|
|
break;
|
2023-06-13 20:13:24 -06:00
|
|
|
}
|
|
|
|
if (group_matched != UNSPEC) {
|
|
|
|
if (matching_group != NULL && m->type != ALIAS)
|
|
|
|
*matching_group = m;
|
|
|
|
break;
|
2012-08-02 14:37:32 -04:00
|
|
|
}
|
2004-11-15 17:33:52 +00:00
|
|
|
}
|
2023-06-13 20:13:24 -06:00
|
|
|
}
|
|
|
|
if (group_matched == UNSPEC) {
|
|
|
|
struct gid_list *runas_groups;
|
|
|
|
/*
|
|
|
|
* The runas group was not explicitly allowed by sudoers.
|
|
|
|
* Check whether it is one of the target user's groups.
|
|
|
|
*/
|
|
|
|
if (runas_pw->pw_gid == runas_gr->gr_gid) {
|
|
|
|
group_matched = ALLOW; /* runas group matches passwd db */
|
|
|
|
} else if ((runas_groups = runas_getgroups()) != NULL) {
|
|
|
|
int i;
|
2018-10-27 06:37:34 -06:00
|
|
|
|
2023-06-13 20:13:24 -06:00
|
|
|
for (i = 0; i < runas_groups->ngids; i++) {
|
|
|
|
if (runas_groups->gids[i] == runas_gr->gr_gid) {
|
|
|
|
group_matched = ALLOW; /* matched aux group vector */
|
|
|
|
break;
|
2018-10-27 06:37:34 -06:00
|
|
|
}
|
|
|
|
}
|
2023-06-13 20:13:24 -06:00
|
|
|
sudo_gidlist_delref(runas_groups);
|
2011-05-23 14:00:54 -04:00
|
|
|
}
|
2004-11-15 17:33:52 +00:00
|
|
|
}
|
2007-11-21 20:12:00 +00:00
|
|
|
|
2023-06-13 20:13:24 -06:00
|
|
|
debug_return_int(group_matched);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check whether the sudoers runaslist, composed of user_list and
|
|
|
|
* group_list, matches the runas user/group requested by the user.
|
|
|
|
* Either (or both) user_list and group_list may be NULL.
|
|
|
|
* If user_list is NULL, a list containing runas_default is used.
|
|
|
|
* Returns ALLOW, DENY or UNSPEC.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
runaslist_matches(const struct sudoers_parse_tree *parse_tree,
|
|
|
|
const struct member_list *user_list, const struct member_list *group_list,
|
|
|
|
struct member **matching_user, struct member **matching_group)
|
|
|
|
{
|
|
|
|
struct member_list _user_list = TAILQ_HEAD_INITIALIZER(_user_list);
|
|
|
|
int user_matched, group_matched = UNSPEC;
|
|
|
|
struct member m_user;
|
|
|
|
debug_decl(runaslist_matches, SUDOERS_DEBUG_MATCH);
|
|
|
|
|
|
|
|
/* If no runas user listed in sudoers, use the default value. */
|
|
|
|
if (user_list == NULL) {
|
|
|
|
m_user.name = def_runas_default;
|
|
|
|
m_user.type = WORD;
|
|
|
|
m_user.negated = false;
|
|
|
|
TAILQ_INSERT_HEAD(&_user_list, &m_user, entries);
|
|
|
|
user_list = &_user_list;
|
|
|
|
matching_user = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
user_matched = runas_userlist_matches(parse_tree, user_list, matching_user);
|
|
|
|
if (ISSET(sudo_user.flags, RUNAS_GROUP_SPECIFIED)) {
|
|
|
|
group_matched = runas_grouplist_matches(parse_tree, group_list,
|
|
|
|
matching_group);
|
|
|
|
}
|
|
|
|
|
2010-09-06 07:56:15 -04:00
|
|
|
if (user_matched == DENY || group_matched == DENY)
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_return_int(DENY);
|
2010-09-06 07:56:15 -04:00
|
|
|
if (user_matched == group_matched || runas_gr == NULL)
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_return_int(user_matched);
|
|
|
|
debug_return_int(UNSPEC);
|
2004-10-26 22:14:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-04-04 09:51:05 -06:00
|
|
|
* Check for lhost and shost in a list of members.
|
2004-10-26 22:14:01 +00:00
|
|
|
* Returns ALLOW, DENY or UNSPEC.
|
|
|
|
*/
|
2018-04-04 09:51:05 -06:00
|
|
|
static int
|
2023-03-17 13:31:36 -06:00
|
|
|
hostlist_matches_int(const struct sudoers_parse_tree *parse_tree,
|
2018-07-26 15:12:33 -06:00
|
|
|
const struct passwd *pw, const char *lhost, const char *shost,
|
|
|
|
const struct member_list *list)
|
2004-10-26 22:14:01 +00:00
|
|
|
{
|
|
|
|
struct member *m;
|
2018-04-04 09:51:05 -06:00
|
|
|
int matched = UNSPEC;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(hostlist_matches, SUDOERS_DEBUG_MATCH);
|
2004-10-26 22:14:01 +00:00
|
|
|
|
2013-10-22 09:08:38 -06:00
|
|
|
TAILQ_FOREACH_REVERSE(m, list, member_list, entries) {
|
2018-07-26 15:12:33 -06:00
|
|
|
matched = host_matches(parse_tree, pw, lhost, shost, m);
|
2018-04-04 09:51:05 -06:00
|
|
|
if (matched != UNSPEC)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
debug_return_int(matched);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for user_runhost and user_srunhost in a list of members.
|
|
|
|
* Returns ALLOW, DENY or UNSPEC.
|
|
|
|
*/
|
|
|
|
int
|
2023-03-17 13:31:36 -06:00
|
|
|
hostlist_matches(const struct sudoers_parse_tree *parse_tree,
|
|
|
|
const struct passwd *pw, const struct member_list *list)
|
2018-04-04 09:51:05 -06:00
|
|
|
{
|
2019-08-15 14:20:12 -06:00
|
|
|
const char *lhost = parse_tree->lhost ? parse_tree->lhost : user_runhost;
|
|
|
|
const char *shost = parse_tree->shost ? parse_tree->shost : user_srunhost;
|
|
|
|
|
|
|
|
return hostlist_matches_int(parse_tree, pw, lhost, shost, list);
|
2018-04-04 09:51:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check whether host or shost matches member.
|
|
|
|
* Returns ALLOW, DENY or UNSPEC.
|
|
|
|
*/
|
|
|
|
int
|
2023-03-17 13:31:36 -06:00
|
|
|
host_matches(const struct sudoers_parse_tree *parse_tree,
|
|
|
|
const struct passwd *pw, const char *lhost, const char *shost,
|
|
|
|
const struct member *m)
|
2018-04-04 09:51:05 -06:00
|
|
|
{
|
|
|
|
struct alias *a;
|
|
|
|
int matched = UNSPEC;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(host_matches, SUDOERS_DEBUG_MATCH);
|
2018-04-04 09:51:05 -06:00
|
|
|
|
|
|
|
switch (m->type) {
|
|
|
|
case ALL:
|
|
|
|
matched = !m->negated;
|
|
|
|
break;
|
|
|
|
case NETGROUP:
|
2023-03-08 13:44:22 -07:00
|
|
|
if (netgr_matches(parse_tree->nss, m->name, lhost, shost,
|
2018-04-04 09:51:05 -06:00
|
|
|
def_netgroup_tuple ? pw->pw_name : NULL))
|
2004-10-26 22:14:01 +00:00
|
|
|
matched = !m->negated;
|
2018-04-04 09:51:05 -06:00
|
|
|
break;
|
|
|
|
case NTWKADDR:
|
|
|
|
if (addr_matches(m->name))
|
|
|
|
matched = !m->negated;
|
|
|
|
break;
|
|
|
|
case ALIAS:
|
2018-07-26 15:12:33 -06:00
|
|
|
a = alias_get(parse_tree, m->name, HOSTALIAS);
|
|
|
|
if (a != NULL) {
|
2018-04-15 08:14:46 -06:00
|
|
|
/* XXX */
|
2018-07-26 15:12:33 -06:00
|
|
|
int rc = hostlist_matches_int(parse_tree, pw, lhost, shost,
|
|
|
|
&a->members);
|
2018-04-04 09:51:05 -06:00
|
|
|
if (rc != UNSPEC)
|
|
|
|
matched = m->negated ? !rc : rc;
|
|
|
|
alias_put(a);
|
2004-10-26 22:14:01 +00:00
|
|
|
break;
|
2018-04-04 09:51:05 -06:00
|
|
|
}
|
2020-08-01 13:10:50 -06:00
|
|
|
FALLTHROUGH;
|
2018-04-04 09:51:05 -06:00
|
|
|
case WORD:
|
|
|
|
if (hostname_matches(shost, lhost, m->name))
|
|
|
|
matched = !m->negated;
|
2007-08-31 01:21:26 +00:00
|
|
|
break;
|
2004-10-26 22:14:01 +00:00
|
|
|
}
|
2022-10-29 11:39:05 -06:00
|
|
|
sudo_debug_printf(SUDO_DEBUG_DEBUG,
|
|
|
|
"host %s (%s) matches sudoers host %s%s: %s", lhost, shost,
|
|
|
|
m->negated ? "!" : "", m->name ? m->name : "ALL",
|
|
|
|
matched == true ? "true" : "false");
|
2015-05-07 10:33:23 -06:00
|
|
|
debug_return_int(matched);
|
2004-10-26 22:14:01 +00:00
|
|
|
}
|
|
|
|
|
2007-09-15 11:24:54 +00:00
|
|
|
/*
|
|
|
|
* Check for cmnd and args in a list of members.
|
|
|
|
* Returns ALLOW, DENY or UNSPEC.
|
|
|
|
*/
|
2013-05-22 11:32:08 -04:00
|
|
|
int
|
2023-03-17 13:31:36 -06:00
|
|
|
cmndlist_matches(const struct sudoers_parse_tree *parse_tree,
|
2020-09-09 15:26:45 -06:00
|
|
|
const struct member_list *list, const char *runchroot,
|
|
|
|
struct cmnd_info *info)
|
2007-09-15 11:24:54 +00:00
|
|
|
{
|
|
|
|
struct member *m;
|
2009-11-23 15:56:14 +00:00
|
|
|
int matched = UNSPEC;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(cmndlist_matches, SUDOERS_DEBUG_MATCH);
|
2007-09-15 11:24:54 +00:00
|
|
|
|
2013-10-22 09:08:38 -06:00
|
|
|
TAILQ_FOREACH_REVERSE(m, list, member_list, entries) {
|
2020-09-09 15:26:45 -06:00
|
|
|
matched = cmnd_matches(parse_tree, m, runchroot, info);
|
2009-11-23 15:56:14 +00:00
|
|
|
if (matched != UNSPEC)
|
2007-09-15 11:24:54 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-05-07 10:33:23 -06:00
|
|
|
debug_return_int(matched);
|
2007-09-15 11:24:54 +00:00
|
|
|
}
|
|
|
|
|
2004-10-26 22:14:01 +00:00
|
|
|
/*
|
2007-08-30 17:26:35 +00:00
|
|
|
* Check cmnd and args.
|
2004-10-26 22:14:01 +00:00
|
|
|
* Returns ALLOW, DENY or UNSPEC.
|
|
|
|
*/
|
|
|
|
int
|
2023-03-17 13:31:36 -06:00
|
|
|
cmnd_matches(const struct sudoers_parse_tree *parse_tree,
|
|
|
|
const struct member *m, const char *runchroot, struct cmnd_info *info)
|
2007-08-30 17:26:35 +00:00
|
|
|
{
|
2004-11-16 16:10:09 +00:00
|
|
|
struct alias *a;
|
2007-08-30 17:26:35 +00:00
|
|
|
struct sudo_command *c;
|
2016-09-08 16:38:08 -06:00
|
|
|
int rc, matched = UNSPEC;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(cmnd_matches, SUDOERS_DEBUG_MATCH);
|
2004-10-26 22:14:01 +00:00
|
|
|
|
2007-08-30 17:26:35 +00:00
|
|
|
switch (m->type) {
|
|
|
|
case ALL:
|
2020-03-11 11:19:37 -06:00
|
|
|
case COMMAND:
|
|
|
|
c = (struct sudo_command *)m->name;
|
2020-09-09 15:26:45 -06:00
|
|
|
if (command_matches(c->cmnd, c->args, runchroot, info, &c->digests))
|
2020-03-11 11:19:37 -06:00
|
|
|
matched = !m->negated;
|
2007-08-30 17:26:35 +00:00
|
|
|
break;
|
|
|
|
case ALIAS:
|
2018-07-26 15:12:33 -06:00
|
|
|
a = alias_get(parse_tree, m->name, CMNDALIAS);
|
|
|
|
if (a != NULL) {
|
2020-09-09 15:26:45 -06:00
|
|
|
rc = cmndlist_matches(parse_tree, &a->members, runchroot, info);
|
2016-09-08 16:38:08 -06:00
|
|
|
if (rc != UNSPEC)
|
|
|
|
matched = m->negated ? !rc : rc;
|
2013-05-22 11:32:08 -04:00
|
|
|
alias_put(a);
|
2007-08-30 17:26:35 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2015-05-07 10:33:23 -06:00
|
|
|
debug_return_int(matched);
|
2007-08-30 17:26:35 +00:00
|
|
|
}
|
|
|
|
|
2023-03-03 13:57:27 -07:00
|
|
|
/*
|
|
|
|
* Like cmnd_matches() but only matches against the ALL command.
|
|
|
|
* Returns ALLOW, DENY or UNSPEC.
|
|
|
|
*/
|
|
|
|
int
|
2023-03-17 13:31:36 -06:00
|
|
|
cmnd_matches_all(const struct sudoers_parse_tree *parse_tree,
|
|
|
|
const struct member *m, const char *runchroot, struct cmnd_info *info)
|
2023-03-03 13:57:27 -07:00
|
|
|
{
|
|
|
|
const bool negated = m->negated;
|
|
|
|
struct sudo_command *c;
|
|
|
|
int matched = UNSPEC;
|
|
|
|
struct alias *a;
|
|
|
|
debug_decl(cmnd_matches_all, SUDOERS_DEBUG_MATCH);
|
|
|
|
|
|
|
|
switch (m->type) {
|
|
|
|
case ALL:
|
|
|
|
c = (struct sudo_command *)m->name;
|
|
|
|
if (command_matches(c->cmnd, c->args, runchroot, info, &c->digests))
|
|
|
|
matched = !negated;
|
|
|
|
break;
|
|
|
|
case ALIAS:
|
|
|
|
a = alias_get(parse_tree, m->name, CMNDALIAS);
|
|
|
|
if (a != NULL) {
|
|
|
|
TAILQ_FOREACH_REVERSE(m, &a->members, member_list, entries) {
|
|
|
|
matched = cmnd_matches_all(parse_tree, m, runchroot, info);
|
|
|
|
if (matched != UNSPEC) {
|
|
|
|
if (negated)
|
|
|
|
matched = !matched;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
alias_put(a);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
debug_return_int(matched);
|
|
|
|
}
|
|
|
|
|
2004-10-26 22:14:01 +00:00
|
|
|
/*
|
2011-12-02 11:27:33 -05:00
|
|
|
* Returns true if the hostname matches the pattern, else false
|
2004-10-26 22:14:01 +00:00
|
|
|
*/
|
2011-12-02 11:27:33 -05:00
|
|
|
bool
|
2013-12-05 14:34:56 -07:00
|
|
|
hostname_matches(const char *shost, const char *lhost, const char *pattern)
|
2004-10-26 22:14:01 +00:00
|
|
|
{
|
2013-12-05 14:34:56 -07:00
|
|
|
const char *host;
|
|
|
|
bool rc;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(hostname_matches, SUDOERS_DEBUG_MATCH);
|
2011-10-22 14:40:21 -04:00
|
|
|
|
2013-12-05 14:34:56 -07:00
|
|
|
host = strchr(pattern, '.') != NULL ? lhost : shost;
|
2004-10-26 22:14:01 +00:00
|
|
|
if (has_meta(pattern)) {
|
2013-12-05 14:34:56 -07:00
|
|
|
rc = !fnmatch(pattern, host, FNM_CASEFOLD);
|
2004-10-26 22:14:01 +00:00
|
|
|
} else {
|
2013-12-05 14:34:56 -07:00
|
|
|
rc = !strcasecmp(host, pattern);
|
2004-10-26 22:14:01 +00:00
|
|
|
}
|
2013-12-05 14:34:56 -07:00
|
|
|
debug_return_bool(rc);
|
2004-10-26 22:14:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-12-05 14:34:56 -07:00
|
|
|
* Returns true if the user/uid from sudoers matches the specified user/uid,
|
|
|
|
* else returns false.
|
2004-10-26 22:14:01 +00:00
|
|
|
*/
|
2011-12-02 11:27:33 -05:00
|
|
|
bool
|
2013-12-05 14:34:56 -07:00
|
|
|
userpw_matches(const char *sudoers_user, const char *user, const struct passwd *pw)
|
2004-10-26 22:14:01 +00:00
|
|
|
{
|
2013-12-03 14:33:26 -07:00
|
|
|
const char *errstr;
|
|
|
|
uid_t uid;
|
2013-12-05 14:34:56 -07:00
|
|
|
bool rc;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(userpw_matches, SUDOERS_DEBUG_MATCH);
|
2011-10-22 14:40:21 -04:00
|
|
|
|
2004-10-26 22:14:01 +00:00
|
|
|
if (pw != NULL && *sudoers_user == '#') {
|
2019-10-20 10:21:29 -06:00
|
|
|
uid = (uid_t) sudo_strtoid(sudoers_user + 1, &errstr);
|
2014-04-09 10:22:09 -06:00
|
|
|
if (errstr == NULL && uid == pw->pw_uid) {
|
2013-12-05 14:34:56 -07:00
|
|
|
rc = true;
|
|
|
|
goto done;
|
|
|
|
}
|
2004-10-26 22:14:01 +00:00
|
|
|
}
|
2018-03-05 10:42:02 -07:00
|
|
|
if (def_case_insensitive_user)
|
|
|
|
rc = strcasecmp(sudoers_user, user) == 0;
|
|
|
|
else
|
|
|
|
rc = strcmp(sudoers_user, user) == 0;
|
2013-12-05 14:34:56 -07:00
|
|
|
done:
|
|
|
|
sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
|
|
|
|
"user %s matches sudoers user %s: %s",
|
|
|
|
user, sudoers_user, rc ? "true" : "false");
|
|
|
|
debug_return_bool(rc);
|
2004-10-26 22:14:01 +00:00
|
|
|
}
|
|
|
|
|
2007-11-21 20:12:00 +00:00
|
|
|
/*
|
2013-12-05 14:34:56 -07:00
|
|
|
* Returns true if the group/gid from sudoers matches the specified group/gid,
|
|
|
|
* else returns false.
|
2007-11-21 20:12:00 +00:00
|
|
|
*/
|
2011-12-02 11:27:33 -05:00
|
|
|
bool
|
2013-12-05 14:34:56 -07:00
|
|
|
group_matches(const char *sudoers_group, const struct group *gr)
|
2007-11-21 20:12:00 +00:00
|
|
|
{
|
2013-12-03 14:33:26 -07:00
|
|
|
const char *errstr;
|
|
|
|
gid_t gid;
|
2013-12-05 14:34:56 -07:00
|
|
|
bool rc;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(group_matches, SUDOERS_DEBUG_MATCH);
|
2011-10-22 14:40:21 -04:00
|
|
|
|
2007-11-21 20:12:00 +00:00
|
|
|
if (*sudoers_group == '#') {
|
2019-10-20 10:21:29 -06:00
|
|
|
gid = (gid_t) sudo_strtoid(sudoers_group + 1, &errstr);
|
2014-04-09 10:22:09 -06:00
|
|
|
if (errstr == NULL && gid == gr->gr_gid) {
|
2013-12-05 14:34:56 -07:00
|
|
|
rc = true;
|
|
|
|
goto done;
|
|
|
|
}
|
2007-11-21 20:12:00 +00:00
|
|
|
}
|
2018-03-05 10:42:02 -07:00
|
|
|
if (def_case_insensitive_group)
|
|
|
|
rc = strcasecmp(sudoers_group, gr->gr_name) == 0;
|
|
|
|
else
|
|
|
|
rc = strcmp(sudoers_group, gr->gr_name) == 0;
|
2013-12-05 14:34:56 -07:00
|
|
|
done:
|
|
|
|
sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
|
|
|
|
"group %s matches sudoers group %s: %s",
|
|
|
|
gr->gr_name, sudoers_group, rc ? "true" : "false");
|
|
|
|
debug_return_bool(rc);
|
2007-11-21 20:12:00 +00:00
|
|
|
}
|
|
|
|
|
2004-10-26 22:14:01 +00:00
|
|
|
/*
|
2013-12-05 14:34:56 -07:00
|
|
|
* Returns true if the given user belongs to the named group,
|
|
|
|
* else returns false.
|
2004-10-26 22:14:01 +00:00
|
|
|
*/
|
2011-12-02 11:27:33 -05:00
|
|
|
bool
|
2013-12-05 14:34:56 -07:00
|
|
|
usergr_matches(const char *group, const char *user, const struct passwd *pw)
|
2004-10-26 22:14:01 +00:00
|
|
|
{
|
2015-05-07 10:56:12 -06:00
|
|
|
bool matched = false;
|
2010-08-04 09:58:50 -04:00
|
|
|
struct passwd *pw0 = NULL;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(usergr_matches, SUDOERS_DEBUG_MATCH);
|
2010-08-04 09:58:50 -04:00
|
|
|
|
2015-10-24 05:43:07 -06:00
|
|
|
/* Make sure we have a valid usergroup, sudo style */
|
2013-12-05 14:34:56 -07:00
|
|
|
if (*group++ != '%') {
|
|
|
|
sudo_debug_printf(SUDO_DEBUG_DIAG, "user group %s has no leading '%%'",
|
|
|
|
group);
|
2010-08-04 09:58:50 -04:00
|
|
|
goto done;
|
2013-12-05 14:34:56 -07:00
|
|
|
}
|
2004-10-26 22:14:01 +00:00
|
|
|
|
2015-10-24 05:43:07 -06:00
|
|
|
/* Query group plugin for %:name groups. */
|
2010-08-04 09:58:50 -04:00
|
|
|
if (*group == ':' && def_group_plugin) {
|
2015-05-07 10:56:12 -06:00
|
|
|
if (group_plugin_query(user, group + 1, pw) == true)
|
|
|
|
matched = true;
|
2010-08-04 09:58:50 -04:00
|
|
|
goto done;
|
|
|
|
}
|
2009-05-17 22:19:38 +00:00
|
|
|
|
2015-10-24 05:43:07 -06:00
|
|
|
/* Look up user's primary gid in the passwd file. */
|
2010-08-04 09:58:50 -04:00
|
|
|
if (pw == NULL) {
|
2013-12-05 14:34:56 -07:00
|
|
|
if ((pw0 = sudo_getpwnam(user)) == NULL) {
|
|
|
|
sudo_debug_printf(SUDO_DEBUG_DIAG, "unable to find %s in passwd db",
|
|
|
|
user);
|
2010-08-04 09:58:50 -04:00
|
|
|
goto done;
|
2013-12-05 14:34:56 -07:00
|
|
|
}
|
2010-08-04 09:58:50 -04:00
|
|
|
pw = pw0;
|
|
|
|
}
|
2004-10-26 22:14:01 +00:00
|
|
|
|
2010-08-04 09:58:50 -04:00
|
|
|
if (user_in_group(pw, group)) {
|
2011-12-02 11:27:33 -05:00
|
|
|
matched = true;
|
2010-08-04 09:58:50 -04:00
|
|
|
goto done;
|
|
|
|
}
|
2004-10-26 22:14:01 +00:00
|
|
|
|
2015-10-24 05:43:07 -06:00
|
|
|
/* Query the group plugin for Unix groups too? */
|
|
|
|
if (def_group_plugin && def_always_query_group_plugin) {
|
|
|
|
if (group_plugin_query(user, group, pw) == true) {
|
|
|
|
matched = true;
|
|
|
|
goto done;
|
|
|
|
}
|
2010-08-04 09:58:50 -04:00
|
|
|
}
|
2009-05-27 00:49:07 +00:00
|
|
|
|
2010-08-04 09:58:50 -04:00
|
|
|
done:
|
|
|
|
if (pw0 != NULL)
|
2012-06-13 16:21:45 -04:00
|
|
|
sudo_pw_delref(pw0);
|
2010-08-04 09:58:50 -04:00
|
|
|
|
2013-12-05 14:34:56 -07:00
|
|
|
sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
|
|
|
|
"user %s matches group %s: %s", user, group, matched ? "true" : "false");
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_return_bool(matched);
|
2004-10-26 22:14:01 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 09:06:25 -06:00
|
|
|
#if defined(HAVE_GETDOMAINNAME) || defined(SI_SRPC_DOMAIN)
|
|
|
|
/*
|
|
|
|
* Check the domain for invalid characters.
|
|
|
|
* Linux getdomainname(2) returns (none) if no domain is set.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
valid_domain(const char *domain)
|
|
|
|
{
|
|
|
|
const char *cp;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(valid_domain, SUDOERS_DEBUG_MATCH);
|
2016-09-13 09:06:25 -06:00
|
|
|
|
|
|
|
for (cp = domain; *cp != '\0'; cp++) {
|
|
|
|
/* Check for illegal characters, Linux may use "(none)". */
|
|
|
|
if (*cp == '(' || *cp == ')' || *cp == ',' || *cp == ' ')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (cp == domain || *cp != '\0')
|
|
|
|
debug_return_bool(false);
|
|
|
|
debug_return_bool(true);
|
|
|
|
}
|
|
|
|
|
2013-04-01 13:56:42 -04:00
|
|
|
/*
|
2015-01-30 14:45:22 -07:00
|
|
|
* Get NIS-style domain name and copy from static storage or NULL if none.
|
2013-04-01 13:56:42 -04:00
|
|
|
*/
|
2015-01-30 14:45:22 -07:00
|
|
|
const char *
|
2013-04-01 13:56:42 -04:00
|
|
|
sudo_getdomainname(void)
|
|
|
|
{
|
2015-02-19 20:28:02 -07:00
|
|
|
static char *domain;
|
2015-01-30 14:45:22 -07:00
|
|
|
static bool initialized;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(sudo_getdomainname, SUDOERS_DEBUG_MATCH);
|
2015-01-30 14:45:22 -07:00
|
|
|
|
2015-02-02 15:38:03 -07:00
|
|
|
if (!initialized) {
|
2015-02-19 20:28:02 -07:00
|
|
|
size_t host_name_max;
|
2016-09-08 16:38:08 -06:00
|
|
|
int rc;
|
2015-02-03 07:33:24 -07:00
|
|
|
|
2015-02-19 20:28:02 -07:00
|
|
|
# ifdef _SC_HOST_NAME_MAX
|
|
|
|
host_name_max = (size_t)sysconf(_SC_HOST_NAME_MAX);
|
|
|
|
if (host_name_max == (size_t)-1)
|
|
|
|
# endif
|
|
|
|
host_name_max = 255; /* POSIX and historic BSD */
|
|
|
|
|
|
|
|
domain = malloc(host_name_max + 1);
|
|
|
|
if (domain != NULL) {
|
|
|
|
domain[0] = '\0';
|
2016-09-13 09:06:25 -06:00
|
|
|
# ifdef SI_SRPC_DOMAIN
|
2016-09-08 16:38:08 -06:00
|
|
|
rc = sysinfo(SI_SRPC_DOMAIN, domain, host_name_max + 1);
|
2015-02-03 07:33:24 -07:00
|
|
|
# else
|
2016-09-08 16:38:08 -06:00
|
|
|
rc = getdomainname(domain, host_name_max + 1);
|
2015-02-03 07:33:24 -07:00
|
|
|
# endif
|
2016-09-13 09:06:25 -06:00
|
|
|
if (rc == -1 || !valid_domain(domain)) {
|
|
|
|
/* Error or invalid domain name. */
|
|
|
|
free(domain);
|
|
|
|
domain = NULL;
|
2013-04-01 13:56:42 -04:00
|
|
|
}
|
2015-07-14 15:28:01 -06:00
|
|
|
} else {
|
|
|
|
/* XXX - want to pass error back to caller */
|
|
|
|
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
|
|
|
|
"unable to allocate memory");
|
2013-04-01 13:56:42 -04:00
|
|
|
}
|
2015-02-02 15:38:03 -07:00
|
|
|
initialized = true;
|
2013-04-01 13:56:42 -04:00
|
|
|
}
|
2015-02-19 20:28:02 -07:00
|
|
|
debug_return_str(domain);
|
2013-04-01 13:56:42 -04:00
|
|
|
}
|
2015-02-19 20:28:02 -07:00
|
|
|
#else
|
|
|
|
const char *
|
|
|
|
sudo_getdomainname(void)
|
|
|
|
{
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(sudo_getdomainname, SUDOERS_DEBUG_MATCH);
|
2015-02-19 20:28:02 -07:00
|
|
|
debug_return_ptr(NULL);
|
|
|
|
}
|
|
|
|
#endif /* HAVE_GETDOMAINNAME || SI_SRPC_DOMAIN */
|
2013-04-01 13:56:42 -04:00
|
|
|
|
2004-10-26 22:14:01 +00:00
|
|
|
/*
|
2011-12-02 11:27:33 -05:00
|
|
|
* Returns true if "host" and "user" belong to the netgroup "netgr",
|
2013-12-05 14:34:56 -07:00
|
|
|
* else return false. Either of "lhost", "shost" or "user" may be NULL
|
2004-10-26 22:14:01 +00:00
|
|
|
* in which case that argument is not checked...
|
|
|
|
*/
|
2011-12-02 11:27:33 -05:00
|
|
|
bool
|
2023-06-06 19:55:06 -06:00
|
|
|
netgr_matches(const struct sudo_nss *nss, const char *netgr,
|
2023-03-08 13:44:22 -07:00
|
|
|
const char *lhost, const char *shost, const char *user)
|
2004-10-26 22:14:01 +00:00
|
|
|
{
|
2015-01-30 14:45:22 -07:00
|
|
|
const char *domain;
|
2013-12-05 14:34:56 -07:00
|
|
|
bool rc = false;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(netgr_matches, SUDOERS_DEBUG_MATCH);
|
2004-10-26 22:14:01 +00:00
|
|
|
|
2014-02-07 14:58:48 -07:00
|
|
|
if (!def_use_netgroups) {
|
|
|
|
sudo_debug_printf(SUDO_DEBUG_INFO, "netgroups are disabled");
|
|
|
|
debug_return_bool(false);
|
|
|
|
}
|
|
|
|
|
2004-10-26 22:14:01 +00:00
|
|
|
/* make sure we have a valid netgroup, sudo style */
|
2013-12-05 14:34:56 -07:00
|
|
|
if (*netgr++ != '+') {
|
|
|
|
sudo_debug_printf(SUDO_DEBUG_DIAG, "netgroup %s has no leading '+'",
|
|
|
|
netgr);
|
2011-12-02 11:27:33 -05:00
|
|
|
debug_return_bool(false);
|
2013-12-05 14:34:56 -07:00
|
|
|
}
|
2004-10-26 22:14:01 +00:00
|
|
|
|
|
|
|
/* get the domain name (if any) */
|
2015-01-30 14:45:22 -07:00
|
|
|
domain = sudo_getdomainname();
|
2004-10-26 22:14:01 +00:00
|
|
|
|
2023-03-08 13:44:22 -07:00
|
|
|
/* Use nss-specific innetgr() function if available. */
|
|
|
|
if (nss != NULL && nss->innetgr != NULL) {
|
|
|
|
switch (nss->innetgr(nss, netgr, lhost, user, domain)) {
|
|
|
|
case 0:
|
|
|
|
if (lhost != shost) {
|
|
|
|
if (nss->innetgr(nss, netgr, shost, user, domain) == 1)
|
|
|
|
rc = true;
|
|
|
|
}
|
|
|
|
goto done;
|
|
|
|
case 1:
|
|
|
|
rc = true;
|
|
|
|
goto done;
|
|
|
|
default:
|
|
|
|
/* Not supported, use system innetgr(3). */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_INNETGR
|
|
|
|
/* Use system innetgr() function. */
|
|
|
|
if (innetgr(netgr, lhost, user, domain) == 1) {
|
2013-12-05 14:34:56 -07:00
|
|
|
rc = true;
|
2023-03-08 13:44:22 -07:00
|
|
|
} else if (lhost != shost) {
|
|
|
|
if (innetgr(netgr, shost, user, domain) == 1)
|
|
|
|
rc = true;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,
|
|
|
|
"%s: no system netgroup support", __func__);
|
|
|
|
#endif /* HAVE_INNETGR */
|
2004-10-26 22:14:01 +00:00
|
|
|
|
2023-03-08 13:44:22 -07:00
|
|
|
done:
|
2013-12-05 14:34:56 -07:00
|
|
|
sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
|
|
|
|
"netgroup %s matches (%s|%s, %s, %s): %s", netgr, lhost ? lhost : "",
|
|
|
|
shost ? shost : "", user ? user : "", domain ? domain : "",
|
|
|
|
rc ? "true" : "false");
|
|
|
|
|
|
|
|
debug_return_bool(rc);
|
2004-10-26 22:14:01 +00:00
|
|
|
}
|