1993-11-27 23:42:49 +00:00
|
|
|
/*
|
2019-04-29 07:21:51 -06:00
|
|
|
* SPDX-License-Identifier: ISC
|
|
|
|
*
|
2023-04-01 10:22:07 -06:00
|
|
|
* Copyright (c) 1993-1996, 1998-2023 Todd C. Miller <Todd.Miller@sudo.ws>
|
1993-11-27 23:42:49 +00:00
|
|
|
*
|
2004-02-13 21:36:43 +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.
|
1993-02-16 17:07:38 +00:00
|
|
|
*
|
2003-04-16 00:42:10 +00:00
|
|
|
* 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.
|
1993-02-16 17:07:38 +00:00
|
|
|
*/
|
|
|
|
|
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-05-17 20:08:46 +00:00
|
|
|
#ifdef __TANDEM
|
|
|
|
# include <floss.h>
|
|
|
|
#endif
|
|
|
|
|
2004-11-19 18:39:14 +00:00
|
|
|
#include <config.h>
|
1994-03-12 18:36:53 +00:00
|
|
|
|
2001-12-14 19:52:54 +00:00
|
|
|
#include <sys/types.h>
|
2015-06-12 15:30:06 -06:00
|
|
|
#include <sys/resource.h>
|
2001-12-14 19:52:54 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/socket.h>
|
1993-02-16 17:07:38 +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>
|
2015-07-02 09:08:28 -06:00
|
|
|
#include <unistd.h>
|
1993-02-16 17:07:38 +00:00
|
|
|
#include <pwd.h>
|
1995-03-26 06:19:20 +00:00
|
|
|
#include <errno.h>
|
1995-11-16 07:41:35 +00:00
|
|
|
#include <fcntl.h>
|
1999-07-05 20:11:50 +00:00
|
|
|
#include <grp.h>
|
1995-09-01 04:23:19 +00:00
|
|
|
#include <netdb.h>
|
2000-10-26 16:42:40 +00:00
|
|
|
#ifdef HAVE_LOGIN_CAP_H
|
2000-03-07 04:29:46 +00:00
|
|
|
# include <login_cap.h>
|
2000-03-24 20:13:12 +00:00
|
|
|
# ifndef LOGIN_DEFROOTCLASS
|
|
|
|
# define LOGIN_DEFROOTCLASS "daemon"
|
|
|
|
# endif
|
2012-03-07 16:38:57 -05:00
|
|
|
# ifndef LOGIN_SETENV
|
|
|
|
# define LOGIN_SETENV 0
|
|
|
|
# endif
|
2000-03-07 04:29:46 +00:00
|
|
|
#endif
|
2008-02-09 14:30:07 +00:00
|
|
|
#ifdef HAVE_SELINUX
|
|
|
|
# include <selinux/selinux.h>
|
|
|
|
#endif
|
2010-05-13 14:09:21 -04:00
|
|
|
#include <ctype.h>
|
2020-08-12 10:07:07 -06:00
|
|
|
#ifndef HAVE_GETADDRINFO
|
|
|
|
# include "compat/getaddrinfo.h"
|
|
|
|
#endif
|
1994-05-25 03:08:10 +00:00
|
|
|
|
2010-03-14 19:58:47 -04:00
|
|
|
#include "sudoers.h"
|
2021-02-13 15:48:21 -07:00
|
|
|
#include "check.h"
|
2010-03-14 19:58:47 -04:00
|
|
|
#include "auth/sudo_auth.h"
|
2019-10-24 20:04:31 -06:00
|
|
|
#include "sudo_iolog.h"
|
1994-03-12 18:36:53 +00:00
|
|
|
|
1994-05-28 19:13:27 +00:00
|
|
|
/*
|
1995-11-25 18:23:40 +00:00
|
|
|
* Prototypes
|
1994-05-28 19:13:27 +00:00
|
|
|
*/
|
2023-08-21 09:21:49 -06:00
|
|
|
static int set_cmnd(struct sudoers_context *ctx);
|
|
|
|
static bool init_vars(struct sudoers_context *ctx, char * const *);
|
|
|
|
static bool set_loginclass(struct sudoers_context *);
|
|
|
|
static bool set_runaspw(struct sudoers_context *ctx, const char *, bool);
|
|
|
|
static bool set_runasgr(struct sudoers_context *ctx, const char *, bool);
|
|
|
|
static bool tty_present(struct sudoers_context *ctx);
|
2010-03-14 19:58:47 -04:00
|
|
|
|
1994-05-14 21:27:05 +00:00
|
|
|
/*
|
|
|
|
* Globals
|
|
|
|
*/
|
2011-02-23 13:38:52 -05:00
|
|
|
static char *prev_user;
|
2023-08-21 09:21:54 -06:00
|
|
|
static struct sudoers_context sudoers_ctx = SUDOERS_CONTEXT_INITIALIZER;
|
2007-12-31 12:39:52 +00:00
|
|
|
static struct sudo_nss_list *snl;
|
2019-12-09 17:14:06 -07:00
|
|
|
static bool unknown_runas_uid;
|
|
|
|
static bool unknown_runas_gid;
|
2023-07-10 11:06:23 -06:00
|
|
|
static int cmnd_status = NOT_FOUND_ERROR;
|
2021-08-09 15:50:25 -06:00
|
|
|
static struct defaults_list initial_defaults = TAILQ_HEAD_INITIALIZER(initial_defaults);
|
1993-02-16 17:07:38 +00:00
|
|
|
|
2015-06-12 15:30:06 -06:00
|
|
|
#ifdef __linux__
|
|
|
|
static struct rlimit nproclimit;
|
|
|
|
#endif
|
|
|
|
|
2020-11-09 17:13:04 -07:00
|
|
|
#ifdef SUDOERS_LOG_CLIENT
|
2020-02-26 13:17:40 -07:00
|
|
|
# define remote_iologs (!SLIST_EMPTY(&def_log_servers))
|
|
|
|
#else
|
|
|
|
# define remote_iologs 0
|
|
|
|
#endif
|
|
|
|
|
2015-06-12 15:30:06 -06:00
|
|
|
/*
|
|
|
|
* Unlimit the number of processes since Linux's setuid() will
|
|
|
|
* apply resource limits when changing uid and return EAGAIN if
|
|
|
|
* nproc would be exceeded by the uid switch.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
unlimit_nproc(void)
|
|
|
|
{
|
|
|
|
#ifdef __linux__
|
|
|
|
struct rlimit rl;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(unlimit_nproc, SUDOERS_DEBUG_UTIL);
|
2015-06-12 15:30:06 -06:00
|
|
|
|
|
|
|
if (getrlimit(RLIMIT_NPROC, &nproclimit) != 0)
|
2019-12-18 09:42:14 -07:00
|
|
|
sudo_warn("getrlimit(RLIMIT_NPROC)");
|
2015-06-12 15:30:06 -06:00
|
|
|
rl.rlim_cur = rl.rlim_max = RLIM_INFINITY;
|
|
|
|
if (setrlimit(RLIMIT_NPROC, &rl) != 0) {
|
|
|
|
rl.rlim_cur = rl.rlim_max = nproclimit.rlim_max;
|
|
|
|
if (setrlimit(RLIMIT_NPROC, &rl) != 0)
|
2019-12-18 09:42:14 -07:00
|
|
|
sudo_warn("setrlimit(RLIMIT_NPROC)");
|
2015-06-12 15:30:06 -06:00
|
|
|
}
|
|
|
|
debug_return;
|
|
|
|
#endif /* __linux__ */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Restore saved value of RLIMIT_NPROC.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
restore_nproc(void)
|
|
|
|
{
|
|
|
|
#ifdef __linux__
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(restore_nproc, SUDOERS_DEBUG_UTIL);
|
2015-06-12 15:30:06 -06:00
|
|
|
|
|
|
|
if (setrlimit(RLIMIT_NPROC, &nproclimit) != 0)
|
2019-12-18 09:42:14 -07:00
|
|
|
sudo_warn("setrlimit(RLIMIT_NPROC)");
|
2015-06-12 15:30:06 -06:00
|
|
|
|
|
|
|
debug_return;
|
|
|
|
#endif /* __linux__ */
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:54:11 -06:00
|
|
|
/*
|
|
|
|
* Re-initialize Defaults settings.
|
2022-03-14 13:54:12 -06:00
|
|
|
* We do not warn, log or send mail for errors when reinitializing,
|
|
|
|
* this would have already been done the first time through.
|
2022-03-14 13:54:11 -06:00
|
|
|
*/
|
2021-08-09 15:50:25 -06:00
|
|
|
static bool
|
2023-08-21 09:21:49 -06:00
|
|
|
sudoers_reinit_defaults(struct sudoers_context *ctx)
|
2021-08-09 15:50:25 -06:00
|
|
|
{
|
|
|
|
struct sudo_nss *nss, *nss_next;
|
2022-03-14 13:54:12 -06:00
|
|
|
sudoers_logger_t logger = sudoers_error_hook;
|
2021-08-09 15:50:25 -06:00
|
|
|
debug_decl(sudoers_reinit_defaults, SUDOERS_DEBUG_PLUGIN);
|
|
|
|
|
|
|
|
if (!init_defaults()) {
|
|
|
|
sudo_warnx("%s", U_("unable to initialize sudoers default values"));
|
|
|
|
debug_return_bool(false);
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:54:12 -06:00
|
|
|
/* It should not be possible for the initial defaults to fail to apply. */
|
2023-08-21 09:21:49 -06:00
|
|
|
if (!update_defaults(ctx, NULL, &initial_defaults,
|
2022-03-14 13:54:11 -06:00
|
|
|
SETDEF_GENERIC|SETDEF_HOST|SETDEF_USER|SETDEF_RUNAS, false))
|
2021-08-09 15:50:25 -06:00
|
|
|
debug_return_bool(false);
|
|
|
|
|
2022-03-14 13:54:12 -06:00
|
|
|
/* Disable error logging while re-processing defaults. */
|
|
|
|
sudoers_error_hook = NULL;
|
|
|
|
|
2021-08-09 15:50:25 -06:00
|
|
|
TAILQ_FOREACH_SAFE(nss, snl, entries, nss_next) {
|
2022-03-14 13:54:11 -06:00
|
|
|
/* Missing/invalid defaults is not a fatal error. */
|
2023-08-21 09:21:49 -06:00
|
|
|
if (nss->getdefs(ctx, nss) != -1) {
|
|
|
|
(void)update_defaults(ctx, nss->parse_tree, NULL,
|
2022-03-14 13:54:12 -06:00
|
|
|
SETDEF_GENERIC|SETDEF_HOST|SETDEF_USER|SETDEF_RUNAS, true);
|
2022-03-09 12:38:25 -07:00
|
|
|
}
|
2021-08-09 15:50:25 -06:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:54:12 -06:00
|
|
|
/* Restore error logging. */
|
|
|
|
sudoers_error_hook = logger;
|
|
|
|
|
2022-09-02 14:45:57 -06:00
|
|
|
/* No need to check the admin flag file multiple times. */
|
2023-08-21 10:47:23 -06:00
|
|
|
if (ISSET(ctx->mode, MODE_POLICY_INTERCEPTED)) {
|
2023-03-10 15:00:20 -07:00
|
|
|
free(def_admin_flag);
|
2022-10-20 13:45:36 -06:00
|
|
|
def_admin_flag = NULL;
|
2023-03-10 15:00:20 -07:00
|
|
|
}
|
2022-09-02 14:45:57 -06:00
|
|
|
|
2022-03-14 13:54:11 -06:00
|
|
|
debug_return_bool(true);
|
2021-08-09 15:50:25 -06:00
|
|
|
}
|
|
|
|
|
2012-10-25 16:58:31 -04:00
|
|
|
int
|
2022-03-10 13:30:56 -07:00
|
|
|
sudoers_init(void *info, sudoers_logger_t logger, char * const envp[])
|
1993-02-16 17:07:38 +00:00
|
|
|
{
|
2012-10-25 16:58:31 -04:00
|
|
|
struct sudo_nss *nss, *nss_next;
|
2016-06-30 12:40:19 -06:00
|
|
|
int oldlocale, sources = 0;
|
2020-06-02 09:07:46 -06:00
|
|
|
static int ret = -1;
|
|
|
|
debug_decl(sudoers_init, SUDOERS_DEBUG_PLUGIN);
|
|
|
|
|
2021-02-12 15:36:18 -07:00
|
|
|
/* Only initialize once. */
|
|
|
|
if (snl != NULL)
|
|
|
|
debug_return_int(ret);
|
2010-03-14 19:58:47 -04:00
|
|
|
|
2011-05-17 16:38:40 -04:00
|
|
|
bindtextdomain("sudoers", LOCALEDIR);
|
|
|
|
|
2022-03-10 13:30:56 -07:00
|
|
|
/* Hook up logging function for parse errors. */
|
|
|
|
sudoers_error_hook = logger;
|
|
|
|
|
2013-04-26 16:06:05 -04:00
|
|
|
/* Register fatal/fatalx callback. */
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_fatal_callback_register(sudoers_cleanup);
|
2012-11-25 09:34:40 -05:00
|
|
|
|
2010-05-12 08:32:12 -04:00
|
|
|
/* Initialize environment functions (including replacements). */
|
2015-06-17 06:49:59 -06:00
|
|
|
if (!env_init(envp))
|
|
|
|
debug_return_int(-1);
|
2010-05-12 08:32:12 -04:00
|
|
|
|
1999-09-08 08:06:28 +00:00
|
|
|
/* Setup defaults data structures. */
|
2015-06-17 06:49:59 -06:00
|
|
|
if (!init_defaults()) {
|
2020-08-12 13:45:09 -06:00
|
|
|
sudo_warnx("%s", U_("unable to initialize sudoers default values"));
|
2015-06-17 06:49:59 -06:00
|
|
|
debug_return_int(-1);
|
|
|
|
}
|
1999-09-08 08:06:28 +00:00
|
|
|
|
2012-10-25 16:58:31 -04:00
|
|
|
/* Parse info from front-end. */
|
2023-08-21 10:47:23 -06:00
|
|
|
sudoers_ctx.mode = sudoers_policy_deserialize_info(&sudoers_ctx, info,
|
2023-08-21 09:21:49 -06:00
|
|
|
&initial_defaults);
|
2023-08-21 10:47:23 -06:00
|
|
|
if (ISSET(sudoers_ctx.mode, MODE_ERROR))
|
2015-05-07 10:33:23 -06:00
|
|
|
debug_return_int(-1);
|
2010-03-14 19:58:47 -04:00
|
|
|
|
2023-08-21 09:21:49 -06:00
|
|
|
if (!init_vars(&sudoers_ctx, envp))
|
2015-05-07 10:33:23 -06:00
|
|
|
debug_return_int(-1);
|
1994-05-24 21:44:50 +00:00
|
|
|
|
2007-12-28 16:20:45 +00:00
|
|
|
/* Parse nsswitch.conf for sudoers order. */
|
2008-01-01 18:22:03 +00:00
|
|
|
snl = sudo_read_nss();
|
2004-02-13 02:08:27 +00:00
|
|
|
|
2011-10-04 11:15:06 -04:00
|
|
|
/* LDAP or NSS may modify the euid so we need to be root for the open. */
|
2023-08-21 09:21:49 -06:00
|
|
|
if (!set_perms(NULL, PERM_ROOT))
|
2015-05-07 10:33:23 -06:00
|
|
|
debug_return_int(-1);
|
2010-04-20 17:00:31 -04:00
|
|
|
|
2022-03-14 13:54:11 -06:00
|
|
|
/* Use the C locale unless another is specified in sudoers. */
|
|
|
|
sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale);
|
|
|
|
sudo_warn_set_locale_func(sudoers_warn_setlocale);
|
|
|
|
|
2021-08-09 15:50:25 -06:00
|
|
|
/* Update defaults set by front-end. */
|
2023-08-21 09:21:49 -06:00
|
|
|
if (!update_defaults(&sudoers_ctx, NULL, &initial_defaults,
|
2022-03-09 12:38:25 -07:00
|
|
|
SETDEF_GENERIC|SETDEF_HOST|SETDEF_USER|SETDEF_RUNAS, false)) {
|
2022-03-14 13:54:11 -06:00
|
|
|
goto cleanup;
|
2021-08-09 15:50:25 -06:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:54:11 -06:00
|
|
|
/* Open and parse sudoers, set global defaults. */
|
2013-10-22 09:00:37 -06:00
|
|
|
TAILQ_FOREACH_SAFE(nss, snl, entries, nss_next) {
|
2023-08-21 09:21:49 -06:00
|
|
|
if (nss->open(&sudoers_ctx, nss) == -1 || (nss->parse_tree = nss->parse(&sudoers_ctx, nss)) == NULL) {
|
2013-10-22 09:00:37 -06:00
|
|
|
TAILQ_REMOVE(snl, nss, entries);
|
2018-07-26 15:12:33 -06:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
sources++;
|
2022-03-14 13:54:11 -06:00
|
|
|
|
|
|
|
/* Missing/invalid defaults is not a fatal error. */
|
2023-08-21 09:21:49 -06:00
|
|
|
if (nss->getdefs(&sudoers_ctx, nss) == -1) {
|
|
|
|
log_warningx(&sudoers_ctx, SLOG_PARSE_ERROR|SLOG_NO_STDERR,
|
2022-03-09 12:38:25 -07:00
|
|
|
N_("unable to get defaults from %s"), nss->source);
|
2022-03-14 13:54:11 -06:00
|
|
|
} else {
|
2023-08-21 09:21:49 -06:00
|
|
|
(void)update_defaults(&sudoers_ctx, nss->parse_tree, NULL,
|
2022-03-14 13:54:11 -06:00
|
|
|
SETDEF_GENERIC|SETDEF_HOST|SETDEF_USER|SETDEF_RUNAS, false);
|
2022-03-09 12:38:25 -07:00
|
|
|
}
|
2004-10-26 22:22:46 +00:00
|
|
|
}
|
2010-03-14 19:58:47 -04:00
|
|
|
if (sources == 0) {
|
2020-08-12 13:45:09 -06:00
|
|
|
sudo_warnx("%s", U_("no valid sudoers sources found, quitting"));
|
2014-04-10 16:03:26 -06:00
|
|
|
goto cleanup;
|
2010-03-14 19:58:47 -04:00
|
|
|
}
|
2004-02-13 02:08:27 +00:00
|
|
|
|
2016-08-10 10:56:05 -06:00
|
|
|
/* Set login class if applicable (after sudoers is parsed). */
|
2023-08-21 09:21:49 -06:00
|
|
|
if (set_loginclass(&sudoers_ctx))
|
2016-09-08 16:38:08 -06:00
|
|
|
ret = true;
|
2007-12-02 00:51:32 +00:00
|
|
|
|
2014-04-10 16:03:26 -06:00
|
|
|
cleanup:
|
2023-08-21 09:21:49 -06:00
|
|
|
mail_parse_errors(&sudoers_ctx);
|
2022-03-14 13:54:11 -06:00
|
|
|
|
2014-04-10 16:11:47 -06:00
|
|
|
if (!restore_perms())
|
2016-09-08 16:38:08 -06:00
|
|
|
ret = -1;
|
2010-04-20 17:00:31 -04:00
|
|
|
|
2016-06-30 12:40:19 -06:00
|
|
|
/* Restore user's locale. */
|
2016-07-20 15:52:32 -06:00
|
|
|
sudo_warn_set_locale_func(NULL);
|
2016-06-30 12:40:19 -06:00
|
|
|
sudoers_setlocale(oldlocale, NULL);
|
|
|
|
|
2016-09-08 16:38:08 -06:00
|
|
|
debug_return_int(ret);
|
2010-03-14 19:58:47 -04:00
|
|
|
}
|
|
|
|
|
2019-10-24 20:04:33 -06:00
|
|
|
/*
|
|
|
|
* Expand I/O log dir and file into a full path.
|
|
|
|
* Returns the full I/O log path prefixed with "iolog_path=".
|
2023-08-21 09:21:49 -06:00
|
|
|
* Sets ctx->user.iolog_file as a side effect.
|
2019-10-24 20:04:33 -06:00
|
|
|
*/
|
|
|
|
static char *
|
2023-08-21 09:21:49 -06:00
|
|
|
format_iolog_path(struct sudoers_context *ctx)
|
2019-10-24 20:04:33 -06:00
|
|
|
{
|
|
|
|
char dir[PATH_MAX], file[PATH_MAX];
|
|
|
|
char *iolog_path = NULL;
|
|
|
|
int oldlocale;
|
|
|
|
bool ok;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(format_iolog_path, SUDOERS_DEBUG_PLUGIN);
|
2019-10-24 20:04:33 -06:00
|
|
|
|
|
|
|
/* Use sudoers locale for strftime() */
|
|
|
|
sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale);
|
|
|
|
ok = expand_iolog_path(def_iolog_dir, dir, sizeof(dir),
|
2023-08-21 09:21:49 -06:00
|
|
|
&sudoers_iolog_path_escapes[1], ctx);
|
2019-10-24 20:04:33 -06:00
|
|
|
if (ok) {
|
2023-08-21 09:21:49 -06:00
|
|
|
ctx->user.iolog_dir = dir;
|
2019-10-24 20:04:33 -06:00
|
|
|
ok = expand_iolog_path(def_iolog_file, file, sizeof(file),
|
2023-08-21 09:21:49 -06:00
|
|
|
&sudoers_iolog_path_escapes[0], ctx);
|
|
|
|
ctx->user.iolog_dir = NULL;
|
2019-10-24 20:04:33 -06:00
|
|
|
}
|
|
|
|
sudoers_setlocale(oldlocale, NULL);
|
|
|
|
if (!ok)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
if (asprintf(&iolog_path, "iolog_path=%s/%s", dir, file) == -1) {
|
|
|
|
iolog_path = NULL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2020-12-05 08:31:46 -07:00
|
|
|
/* Stash pointer to the I/O log for the event log. */
|
2023-08-21 09:21:49 -06:00
|
|
|
ctx->user.iolog_path = iolog_path + sizeof("iolog_path=") - 1;
|
|
|
|
ctx->user.iolog_file = ctx->user.iolog_path + 1 + strlen(dir);
|
2019-10-24 20:04:33 -06:00
|
|
|
|
|
|
|
done:
|
|
|
|
debug_return_str(iolog_path);
|
|
|
|
}
|
|
|
|
|
2023-08-08 09:57:09 -06:00
|
|
|
static void
|
2023-08-09 11:19:17 -06:00
|
|
|
cb_lookup(const struct sudoers_parse_tree *parse_tree,
|
|
|
|
const struct userspec *us, int user_match, const struct privilege *priv,
|
|
|
|
int host_match, const struct cmndspec *cs, int date_match, int runas_match,
|
|
|
|
int cmnd_match, void *closure)
|
2023-08-08 09:57:09 -06:00
|
|
|
{
|
|
|
|
struct sudoers_match_info *info = closure;
|
|
|
|
|
|
|
|
if (cmnd_match != UNSPEC) {
|
|
|
|
info->us = us;
|
|
|
|
info->priv = priv;
|
|
|
|
info->cs = cs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-05 13:35:09 -06:00
|
|
|
/*
|
|
|
|
* Find the command, perform a sudoers lookup, ask for a password as
|
|
|
|
* needed, and perform post-lokup checks. Logs success/failure.
|
|
|
|
* This is used by the check, list and validate plugin methods.
|
|
|
|
*
|
|
|
|
* Returns true if allowed, false if denied, -1 on error and
|
|
|
|
* -2 for usage error.
|
|
|
|
*/
|
|
|
|
static int
|
2023-08-21 09:21:49 -06:00
|
|
|
sudoers_check_common(struct sudoers_context *ctx, int pwflag)
|
2010-03-14 19:58:47 -04:00
|
|
|
{
|
2023-08-08 09:57:09 -06:00
|
|
|
struct sudoers_match_info match_info = { NULL };
|
2023-07-10 11:06:23 -06:00
|
|
|
int oldlocale, ret = -1;
|
|
|
|
unsigned int validated;
|
2023-06-29 17:30:39 -06:00
|
|
|
time_t now;
|
2023-04-05 13:35:09 -06:00
|
|
|
debug_decl(sudoers_check_common, SUDOERS_DEBUG_PLUGIN);
|
2010-03-14 19:58:47 -04:00
|
|
|
|
2011-05-26 12:52:59 -04:00
|
|
|
/* If given the -P option, set the "preserve_groups" flag. */
|
2023-08-21 10:47:23 -06:00
|
|
|
if (ISSET(ctx->mode, MODE_PRESERVE_GROUPS))
|
2011-12-02 11:27:33 -05:00
|
|
|
def_preserve_groups = true;
|
2011-05-26 12:52:59 -04:00
|
|
|
|
2013-08-15 09:56:17 -06:00
|
|
|
/* Find command in path and apply per-command Defaults. */
|
2023-08-21 09:21:49 -06:00
|
|
|
cmnd_status = set_cmnd(ctx);
|
2015-06-25 11:12:36 -06:00
|
|
|
if (cmnd_status == NOT_FOUND_ERROR)
|
2014-04-10 16:03:26 -06:00
|
|
|
goto done;
|
2004-10-26 22:22:46 +00:00
|
|
|
|
2023-04-05 13:25:32 -06:00
|
|
|
/* Is root even allowed to run sudo? */
|
2023-08-21 09:21:49 -06:00
|
|
|
if (ctx->user.uid == 0 && !def_root_sudo) {
|
2023-04-05 13:25:32 -06:00
|
|
|
/* Not an audit event (should it be?). */
|
|
|
|
sudo_warnx("%s",
|
|
|
|
U_("sudoers specifies that root is not allowed to sudo"));
|
2023-04-05 13:35:09 -06:00
|
|
|
ret = false;
|
|
|
|
goto done;
|
2023-04-05 13:25:32 -06:00
|
|
|
}
|
|
|
|
|
2013-08-15 09:56:17 -06:00
|
|
|
/* Check for -C overriding def_closefrom. */
|
2023-08-21 09:21:49 -06:00
|
|
|
if (ctx->user.closefrom >= 0 && ctx->user.closefrom != def_closefrom) {
|
2013-08-15 09:56:17 -06:00
|
|
|
if (!def_closefrom_override) {
|
2023-08-21 09:21:49 -06:00
|
|
|
log_warningx(ctx, SLOG_NO_STDERR|SLOG_AUDIT,
|
2020-09-23 08:02:43 -06:00
|
|
|
N_("user not allowed to override closefrom limit"));
|
2020-08-12 13:45:09 -06:00
|
|
|
sudo_warnx("%s", U_("you are not permitted to use the -C option"));
|
2013-08-15 09:56:17 -06:00
|
|
|
goto bad;
|
|
|
|
}
|
2023-08-21 09:21:49 -06:00
|
|
|
def_closefrom = ctx->user.closefrom;
|
2013-08-15 09:56:17 -06:00
|
|
|
}
|
|
|
|
|
2010-03-14 19:58:47 -04:00
|
|
|
/*
|
2012-11-08 15:37:43 -05:00
|
|
|
* Check sudoers sources, using the locale specified in sudoers.
|
2010-03-14 19:58:47 -04:00
|
|
|
*/
|
2023-06-29 17:30:39 -06:00
|
|
|
time(&now);
|
2012-11-08 15:37:43 -05:00
|
|
|
sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale);
|
2023-08-21 09:21:49 -06:00
|
|
|
validated = sudoers_lookup(snl, ctx, now, cb_lookup, &match_info,
|
2023-08-07 15:06:19 -06:00
|
|
|
&cmnd_status, pwflag);
|
2023-04-01 10:22:07 -06:00
|
|
|
sudoers_setlocale(oldlocale, NULL);
|
2018-05-14 09:05:03 -06:00
|
|
|
if (ISSET(validated, VALIDATE_ERROR)) {
|
|
|
|
/* The lookup function should have printed an error. */
|
|
|
|
goto done;
|
2007-12-28 16:20:45 +00:00
|
|
|
}
|
2009-05-17 22:19:38 +00:00
|
|
|
|
2023-08-08 09:57:09 -06:00
|
|
|
if (match_info.us != NULL && match_info.us->file != NULL) {
|
2023-08-21 09:21:49 -06:00
|
|
|
free(ctx->user.source);
|
2023-08-08 09:57:09 -06:00
|
|
|
if (match_info.us->line != 0) {
|
2023-08-21 09:21:49 -06:00
|
|
|
if (asprintf(&ctx->user.source, "%s:%d:%d", match_info.us->file,
|
2023-08-08 09:57:09 -06:00
|
|
|
match_info.us->line, match_info.us->column) == -1)
|
2023-08-21 09:21:49 -06:00
|
|
|
ctx->user.source = NULL;
|
2023-08-08 09:57:09 -06:00
|
|
|
} else {
|
2023-08-21 09:21:49 -06:00
|
|
|
ctx->user.source = strdup(match_info.us->file);
|
2023-08-08 09:57:09 -06:00
|
|
|
}
|
2023-08-21 09:21:49 -06:00
|
|
|
if (ctx->user.source == NULL) {
|
2023-08-08 09:57:09 -06:00
|
|
|
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-21 09:21:49 -06:00
|
|
|
if (ctx->runas.cmnd == NULL) {
|
|
|
|
if ((ctx->runas.cmnd = strdup(ctx->user.cmnd)) == NULL) {
|
2015-06-19 14:51:17 -06:00
|
|
|
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
|
2015-06-17 06:49:59 -06:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
2004-10-26 22:22:46 +00:00
|
|
|
|
2019-12-09 17:14:06 -07:00
|
|
|
/* Defer uid/gid checks until after defaults have been updated. */
|
|
|
|
if (unknown_runas_uid && !def_runas_allow_unknown_id) {
|
2023-08-21 09:21:49 -06:00
|
|
|
log_warningx(ctx, SLOG_AUDIT, N_("unknown user %s"),
|
|
|
|
ctx->runas.pw->pw_name);
|
2019-12-09 17:14:06 -07:00
|
|
|
goto done;
|
|
|
|
}
|
2023-08-21 09:21:49 -06:00
|
|
|
if (ctx->runas.gr != NULL) {
|
2019-12-09 17:14:06 -07:00
|
|
|
if (unknown_runas_gid && !def_runas_allow_unknown_id) {
|
2023-08-21 09:21:49 -06:00
|
|
|
log_warningx(ctx, SLOG_AUDIT, N_("unknown group %s"),
|
|
|
|
ctx->runas.gr->gr_name);
|
2019-12-09 17:14:06 -07:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-21 08:54:06 -04:00
|
|
|
/* If no command line args and "shell_noargs" is not set, error out. */
|
2023-08-21 10:47:23 -06:00
|
|
|
if (ISSET(ctx->mode, MODE_IMPLIED_SHELL) && !def_shell_noargs) {
|
2014-03-25 16:46:00 -06:00
|
|
|
/* Not an audit event. */
|
2016-09-08 16:38:08 -06:00
|
|
|
ret = -2; /* usage error */
|
2010-03-14 19:58:47 -04:00
|
|
|
goto done;
|
|
|
|
}
|
2000-01-17 17:25:10 +00:00
|
|
|
|
2023-04-05 13:35:09 -06:00
|
|
|
/* Bail if a tty is required and we don't have one. */
|
2023-08-21 09:21:49 -06:00
|
|
|
if (def_requiretty && !tty_present(ctx)) {
|
|
|
|
log_warningx(ctx, SLOG_NO_STDERR|SLOG_AUDIT, N_("no tty"));
|
2020-08-12 13:45:09 -06:00
|
|
|
sudo_warnx("%s", U_("sorry, you must have a tty to run sudo"));
|
2013-03-28 15:46:52 -04:00
|
|
|
goto bad;
|
1999-10-08 01:12:50 +00:00
|
|
|
}
|
|
|
|
|
2023-04-05 13:35:09 -06:00
|
|
|
/* Check runas user's shell if running (or checking) a command. */
|
2023-08-21 10:47:23 -06:00
|
|
|
if (ISSET(ctx->mode, MODE_RUN|MODE_CHECK)) {
|
2023-08-21 09:21:49 -06:00
|
|
|
if (!check_user_shell(ctx->runas.pw)) {
|
|
|
|
log_warningx(ctx, SLOG_RAW_MSG|SLOG_AUDIT,
|
2023-04-05 13:35:09 -06:00
|
|
|
N_("invalid shell for user %s: %s"),
|
2023-08-21 09:21:49 -06:00
|
|
|
ctx->runas.pw->pw_name, ctx->runas.pw->pw_shell);
|
2023-04-05 13:35:09 -06:00
|
|
|
goto bad;
|
|
|
|
}
|
2019-12-09 19:29:45 -07:00
|
|
|
}
|
|
|
|
|
2010-05-13 14:09:21 -04:00
|
|
|
/*
|
|
|
|
* We don't reset the environment for sudoedit or if the user
|
|
|
|
* specified the -E command line flag and they have setenv privs.
|
|
|
|
*/
|
2023-08-21 10:47:23 -06:00
|
|
|
if (ISSET(ctx->mode, MODE_EDIT) ||
|
|
|
|
(ISSET(ctx->mode, MODE_PRESERVE_ENV) && def_setenv))
|
2011-12-02 11:27:33 -05:00
|
|
|
def_env_reset = false;
|
2007-07-08 18:44:28 +00:00
|
|
|
|
|
|
|
/* Build a new environment that avoids any nasty bits. */
|
2023-08-21 09:21:49 -06:00
|
|
|
if (!rebuild_env(ctx))
|
2014-04-30 16:57:12 -06:00
|
|
|
goto bad;
|
2007-06-23 23:58:54 +00:00
|
|
|
|
2003-12-30 22:31:30 +00:00
|
|
|
/* Require a password if sudoers says so. */
|
2023-08-21 10:47:23 -06:00
|
|
|
switch (check_user(ctx, validated, ctx->mode)) {
|
2016-09-05 19:44:46 -06:00
|
|
|
case true:
|
|
|
|
/* user authenticated successfully. */
|
|
|
|
break;
|
|
|
|
case false:
|
2014-03-25 16:46:00 -06:00
|
|
|
/* Note: log_denial() calls audit for us. */
|
2015-06-25 11:12:36 -06:00
|
|
|
if (!ISSET(validated, VALIDATE_SUCCESS)) {
|
2017-11-14 13:58:35 -07:00
|
|
|
/* Only display a denial message if no password was read. */
|
2023-08-21 09:21:49 -06:00
|
|
|
if (!log_denial(ctx, validated, def_passwd_tries <= 0))
|
2016-09-05 19:44:46 -06:00
|
|
|
goto done;
|
2015-06-25 11:12:36 -06:00
|
|
|
}
|
2016-09-05 19:44:46 -06:00
|
|
|
goto bad;
|
|
|
|
default:
|
2016-09-08 16:38:08 -06:00
|
|
|
/* some other error, ret is -1. */
|
2011-10-25 10:08:26 -04:00
|
|
|
goto done;
|
2012-07-10 12:42:33 -04:00
|
|
|
}
|
1999-08-19 16:30:09 +00:00
|
|
|
|
2023-08-21 09:21:49 -06:00
|
|
|
/* Check whether ctx->runas.chroot is permitted (if specified). */
|
|
|
|
switch (check_user_runchroot(ctx->runas.chroot)) {
|
2020-09-09 19:18:24 -06:00
|
|
|
case true:
|
|
|
|
break;
|
|
|
|
case false:
|
2023-08-21 09:21:49 -06:00
|
|
|
log_warningx(ctx, SLOG_NO_STDERR|SLOG_AUDIT,
|
2023-06-30 10:43:18 -06:00
|
|
|
N_("user not allowed to change root directory to %s"),
|
2023-08-21 09:21:49 -06:00
|
|
|
ctx->runas.chroot);
|
2023-06-30 10:43:18 -06:00
|
|
|
sudo_warnx(U_("you are not permitted to use the -R option with %s"),
|
2023-08-21 09:21:49 -06:00
|
|
|
ctx->user.cmnd);
|
2020-09-09 19:18:24 -06:00
|
|
|
goto bad;
|
|
|
|
default:
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2023-08-21 09:21:49 -06:00
|
|
|
/* Check whether ctx->runas.cwd is permitted (if specified). */
|
|
|
|
switch (check_user_runcwd(ctx->runas.cwd)) {
|
2020-09-09 19:18:24 -06:00
|
|
|
case true:
|
|
|
|
break;
|
|
|
|
case false:
|
2023-08-21 09:21:49 -06:00
|
|
|
log_warningx(ctx, SLOG_NO_STDERR|SLOG_AUDIT,
|
|
|
|
N_("user not allowed to change directory to %s"), ctx->runas.cwd);
|
2023-06-30 10:43:18 -06:00
|
|
|
sudo_warnx(U_("you are not permitted to use the -D option with %s"),
|
2023-08-21 09:21:49 -06:00
|
|
|
ctx->user.cmnd);
|
2020-09-09 19:18:24 -06:00
|
|
|
goto bad;
|
|
|
|
default:
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2023-08-21 09:21:49 -06:00
|
|
|
/* If run as root with SUDO_USER set, set ctx->user.pw to that user. */
|
2004-10-26 22:22:46 +00:00
|
|
|
/* XXX - causes confusion when root is not listed in sudoers */
|
2023-08-21 10:47:23 -06:00
|
|
|
if (ISSET(ctx->mode, MODE_RUN|MODE_EDIT) && prev_user != NULL) {
|
2023-08-21 09:21:49 -06:00
|
|
|
if (ctx->user.uid == 0 && strcmp(prev_user, "root") != 0) {
|
2004-11-15 14:53:05 +00:00
|
|
|
struct passwd *pw;
|
2004-01-29 21:15:27 +00:00
|
|
|
|
2009-12-12 15:37:52 +00:00
|
|
|
if ((pw = sudo_getpwnam(prev_user)) != NULL) {
|
2023-08-21 09:21:49 -06:00
|
|
|
if (ctx->user.pw != NULL)
|
|
|
|
sudo_pw_delref(ctx->user.pw);
|
|
|
|
ctx->user.pw = pw;
|
2009-12-12 15:37:52 +00:00
|
|
|
}
|
2004-10-27 16:16:23 +00:00
|
|
|
}
|
2004-01-29 21:15:27 +00:00
|
|
|
}
|
|
|
|
|
2010-04-26 22:10:34 -04:00
|
|
|
/* If the user was not allowed to run the command we are done. */
|
2015-01-21 11:03:48 -07:00
|
|
|
if (!ISSET(validated, VALIDATE_SUCCESS)) {
|
2014-03-25 16:46:00 -06:00
|
|
|
/* Note: log_failure() calls audit for us. */
|
2023-08-21 09:21:49 -06:00
|
|
|
if (!log_failure(ctx, validated, cmnd_status))
|
2015-06-25 11:12:36 -06:00
|
|
|
goto done;
|
2011-03-08 09:38:21 -05:00
|
|
|
goto bad;
|
2010-03-14 19:58:47 -04:00
|
|
|
}
|
2007-06-23 23:58:54 +00:00
|
|
|
|
2010-07-28 16:34:11 -04:00
|
|
|
/* Create Ubuntu-style dot file to indicate sudo was successful. */
|
2023-08-21 09:21:49 -06:00
|
|
|
if (create_admin_success_flag(ctx) == -1)
|
2015-06-25 11:12:36 -06:00
|
|
|
goto done;
|
2010-07-28 16:34:11 -04:00
|
|
|
|
2010-03-14 19:58:47 -04:00
|
|
|
/* Finally tell the user if the command did not exist. */
|
|
|
|
if (cmnd_status == NOT_FOUND_DOT) {
|
2023-08-21 09:22:24 -06:00
|
|
|
audit_failure(ctx, ctx->runas.argv, N_("command in current directory"));
|
2023-08-21 09:21:49 -06:00
|
|
|
sudo_warnx(U_("ignoring \"%s\" found in '.'\nUse \"sudo ./%s\" if this is the \"%s\" you wish to run."), ctx->user.cmnd, ctx->user.cmnd, ctx->user.cmnd);
|
2011-03-08 09:38:21 -05:00
|
|
|
goto bad;
|
2010-03-14 19:58:47 -04:00
|
|
|
} else if (cmnd_status == NOT_FOUND) {
|
2023-08-21 10:47:23 -06:00
|
|
|
if (ISSET(ctx->mode, MODE_CHECK)) {
|
2023-08-21 09:22:24 -06:00
|
|
|
audit_failure(ctx, ctx->runas.argv, N_("%s: command not found"),
|
|
|
|
ctx->runas.argv[1]);
|
|
|
|
sudo_warnx(U_("%s: command not found"), ctx->runas.argv[1]);
|
2013-02-20 15:35:26 -05:00
|
|
|
} else {
|
2023-08-21 09:22:24 -06:00
|
|
|
audit_failure(ctx, ctx->runas.argv, N_("%s: command not found"),
|
|
|
|
ctx->user.cmnd);
|
2023-08-21 09:21:49 -06:00
|
|
|
sudo_warnx(U_("%s: command not found"), ctx->user.cmnd);
|
|
|
|
if (strncmp(ctx->user.cmnd, "cd", 2) == 0 && (ctx->user.cmnd[2] == '\0' ||
|
|
|
|
isblank((unsigned char)ctx->user.cmnd[2]))) {
|
2021-10-11 11:05:58 -06:00
|
|
|
sudo_warnx("%s",
|
|
|
|
U_("\"cd\" is a shell built-in command, it cannot be run directly."));
|
|
|
|
sudo_warnx("%s",
|
|
|
|
U_("the -s option may be used to run a privileged shell."));
|
|
|
|
sudo_warnx("%s",
|
|
|
|
U_("the -D option may be used to run a command in a specific directory."));
|
|
|
|
}
|
2013-02-20 15:35:26 -05:00
|
|
|
}
|
2011-03-08 09:38:21 -05:00
|
|
|
goto bad;
|
2010-03-14 19:58:47 -04:00
|
|
|
}
|
2009-08-30 15:18:50 +00:00
|
|
|
|
2017-02-16 09:58:18 -07:00
|
|
|
/* If user specified a timeout make sure sudoers allows it. */
|
2023-08-21 09:21:49 -06:00
|
|
|
if (!def_user_command_timeouts && ctx->user.timeout > 0) {
|
|
|
|
log_warningx(ctx, SLOG_NO_STDERR|SLOG_AUDIT,
|
2020-09-23 08:02:43 -06:00
|
|
|
N_("user not allowed to set a command timeout"));
|
2020-08-12 13:45:09 -06:00
|
|
|
sudo_warnx("%s",
|
|
|
|
U_("sorry, you are not allowed set a command timeout"));
|
2017-02-16 09:58:18 -07:00
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
2010-03-14 19:58:47 -04:00
|
|
|
/* If user specified env vars make sure sudoers allows it. */
|
2023-08-21 10:47:23 -06:00
|
|
|
if (ISSET(ctx->mode, MODE_RUN) && !def_setenv) {
|
|
|
|
if (ISSET(ctx->mode, MODE_PRESERVE_ENV)) {
|
2023-08-21 09:21:49 -06:00
|
|
|
log_warningx(ctx, SLOG_NO_STDERR|SLOG_AUDIT,
|
2020-09-23 08:02:43 -06:00
|
|
|
N_("user not allowed to preserve the environment"));
|
2020-08-12 13:45:09 -06:00
|
|
|
sudo_warnx("%s",
|
|
|
|
U_("sorry, you are not allowed to preserve the environment"));
|
2011-03-08 09:38:21 -05:00
|
|
|
goto bad;
|
2014-04-30 16:57:12 -06:00
|
|
|
} else {
|
2023-08-21 09:21:49 -06:00
|
|
|
if (!validate_env_vars(ctx, ctx->user.env_vars))
|
2014-04-30 16:57:12 -06:00
|
|
|
goto bad;
|
|
|
|
}
|
2010-03-14 19:58:47 -04:00
|
|
|
}
|
1999-08-19 16:30:09 +00:00
|
|
|
|
2023-04-05 13:35:09 -06:00
|
|
|
ret = true;
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
bad:
|
|
|
|
ret = false;
|
|
|
|
done:
|
|
|
|
debug_return_int(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool need_reinit;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check whether the user is allowed to run the specified command.
|
|
|
|
* Returns true if allowed, false if denied, -1 on error and
|
|
|
|
* -2 for usage error.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sudoers_check_cmnd(int argc, char * const argv[], char *env_add[],
|
|
|
|
void *closure)
|
|
|
|
{
|
|
|
|
char *iolog_path = NULL;
|
|
|
|
mode_t cmnd_umask = ACCESSPERMS;
|
|
|
|
int ret = -1;
|
|
|
|
debug_decl(sudoers_check_cmnd, SUDOERS_DEBUG_PLUGIN);
|
|
|
|
|
|
|
|
sudo_warn_set_locale_func(sudoers_warn_setlocale);
|
|
|
|
|
|
|
|
if (argc == 0) {
|
|
|
|
sudo_warnx("%s", U_("no command specified"));
|
|
|
|
debug_return_int(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (need_reinit) {
|
|
|
|
/* Was previous command intercepted? */
|
2023-08-21 10:47:23 -06:00
|
|
|
if (ISSET(sudoers_ctx.mode, MODE_RUN) && def_intercept)
|
|
|
|
SET(sudoers_ctx.mode, MODE_POLICY_INTERCEPTED);
|
2023-04-05 13:35:09 -06:00
|
|
|
|
|
|
|
/* Only certain mode flags are legal for intercepted commands. */
|
2023-08-21 10:47:23 -06:00
|
|
|
if (ISSET(sudoers_ctx.mode, MODE_POLICY_INTERCEPTED))
|
|
|
|
sudoers_ctx.mode &= MODE_INTERCEPT_MASK;
|
2023-04-05 13:35:09 -06:00
|
|
|
|
|
|
|
/* Re-initialize defaults if we are called multiple times. */
|
2023-08-21 09:21:49 -06:00
|
|
|
if (!sudoers_reinit_defaults(&sudoers_ctx))
|
2023-04-05 13:35:09 -06:00
|
|
|
debug_return_int(-1);
|
|
|
|
}
|
|
|
|
need_reinit = true;
|
|
|
|
|
|
|
|
unlimit_nproc();
|
|
|
|
|
2023-08-21 09:21:49 -06:00
|
|
|
if (!set_perms(&sudoers_ctx, PERM_INITIAL))
|
2023-04-05 13:35:09 -06:00
|
|
|
goto bad;
|
|
|
|
|
|
|
|
/* Environment variables specified on the command line. */
|
|
|
|
if (env_add != NULL && env_add[0] != NULL)
|
2023-08-21 09:21:49 -06:00
|
|
|
sudoers_ctx.user.env_vars = env_add;
|
2023-04-05 13:35:09 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Make a local copy of argc/argv, with special handling for the
|
|
|
|
* '-i' option. We also allocate an extra slot for bash's --login.
|
|
|
|
*/
|
2023-08-21 09:22:24 -06:00
|
|
|
if (sudoers_ctx.runas.argv != NULL && sudoers_ctx.runas.argv != sudoers_ctx.runas.argv_saved) {
|
|
|
|
sudoers_gc_remove(GC_PTR, sudoers_ctx.runas.argv);
|
|
|
|
free(sudoers_ctx.runas.argv);
|
2023-04-05 13:35:09 -06:00
|
|
|
}
|
2023-08-21 09:22:24 -06:00
|
|
|
sudoers_ctx.runas.argv = reallocarray(NULL, (size_t)argc + 2, sizeof(char *));
|
|
|
|
if (sudoers_ctx.runas.argv == NULL) {
|
2023-04-05 13:35:09 -06:00
|
|
|
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
|
|
|
|
goto error;
|
|
|
|
}
|
2023-08-21 09:22:24 -06:00
|
|
|
sudoers_gc_add(GC_PTR, sudoers_ctx.runas.argv);
|
|
|
|
memcpy(sudoers_ctx.runas.argv, argv, (size_t)argc * sizeof(char *));
|
|
|
|
sudoers_ctx.runas.argc = argc;
|
|
|
|
sudoers_ctx.runas.argv[sudoers_ctx.runas.argc] = NULL;
|
2023-08-21 10:47:23 -06:00
|
|
|
if (ISSET(sudoers_ctx.mode, MODE_LOGIN_SHELL) && sudoers_ctx.runas.pw != NULL) {
|
2023-08-21 09:22:24 -06:00
|
|
|
sudoers_ctx.runas.argv[0] = strdup(sudoers_ctx.runas.pw->pw_shell);
|
|
|
|
if (sudoers_ctx.runas.argv[0] == NULL) {
|
2023-04-05 13:35:09 -06:00
|
|
|
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
|
|
|
|
goto error;
|
|
|
|
}
|
2023-08-21 09:22:24 -06:00
|
|
|
sudoers_gc_add(GC_PTR, sudoers_ctx.runas.argv[0]);
|
2023-04-05 13:35:09 -06:00
|
|
|
}
|
|
|
|
|
2023-08-21 09:21:49 -06:00
|
|
|
ret = sudoers_check_common(&sudoers_ctx, 0);
|
2023-04-05 13:35:09 -06:00
|
|
|
if (ret != true)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
if (!remote_iologs) {
|
2022-09-20 14:35:12 -06:00
|
|
|
if (iolog_enabled && def_iolog_file && def_iolog_dir) {
|
2023-08-21 09:21:49 -06:00
|
|
|
if ((iolog_path = format_iolog_path(&sudoers_ctx)) == NULL) {
|
2017-01-13 15:45:59 -07:00
|
|
|
if (!def_ignore_iolog_errors)
|
2023-04-05 13:35:09 -06:00
|
|
|
goto error;
|
2017-01-13 15:45:59 -07:00
|
|
|
/* Unable to expand I/O log path, disable I/O logging. */
|
|
|
|
def_log_input = false;
|
|
|
|
def_log_output = false;
|
2022-09-20 14:35:12 -06:00
|
|
|
def_log_stdin = false;
|
|
|
|
def_log_stdout = false;
|
|
|
|
def_log_stderr = false;
|
|
|
|
def_log_ttyin = false;
|
|
|
|
def_log_ttyout = false;
|
2017-01-13 15:45:59 -07:00
|
|
|
}
|
2010-12-27 12:18:32 -05:00
|
|
|
}
|
2010-12-20 16:28:20 -05:00
|
|
|
}
|
|
|
|
|
2010-03-14 19:58:47 -04:00
|
|
|
/*
|
|
|
|
* Set umask based on sudoers.
|
|
|
|
* If user's umask is more restrictive, OR in those bits too
|
|
|
|
* unless umask_override is set.
|
|
|
|
*/
|
2016-11-07 13:36:05 -07:00
|
|
|
if (def_umask != ACCESSPERMS) {
|
2012-10-25 13:16:58 -04:00
|
|
|
cmnd_umask = def_umask;
|
2013-03-28 15:40:32 -04:00
|
|
|
if (!def_umask_override)
|
2023-08-21 09:21:49 -06:00
|
|
|
cmnd_umask |= sudoers_ctx.user.umask;
|
2010-03-14 19:58:47 -04:00
|
|
|
}
|
1999-09-08 08:06:28 +00:00
|
|
|
|
2023-08-21 10:47:23 -06:00
|
|
|
if (ISSET(sudoers_ctx.mode, MODE_LOGIN_SHELL)) {
|
2010-03-14 19:58:47 -04:00
|
|
|
char *p;
|
2009-09-03 10:21:18 +00:00
|
|
|
|
2010-03-14 19:58:47 -04:00
|
|
|
/* Convert /bin/sh -> -sh so shell knows it is a login shell */
|
2023-08-21 09:22:24 -06:00
|
|
|
if ((p = strrchr(sudoers_ctx.runas.argv[0], '/')) == NULL)
|
|
|
|
p = sudoers_ctx.runas.argv[0];
|
2010-03-14 19:58:47 -04:00
|
|
|
*p = '-';
|
2023-08-21 09:22:24 -06:00
|
|
|
sudoers_ctx.runas.argv[0] = p;
|
2000-02-28 23:58:17 +00:00
|
|
|
|
2011-06-26 18:02:09 -04:00
|
|
|
/*
|
2011-07-17 10:37:15 -04:00
|
|
|
* Newer versions of bash require the --login option to be used
|
|
|
|
* in conjunction with the -c option even if the shell name starts
|
|
|
|
* with a '-'. Unfortunately, bash 1.x uses -login, not --login
|
|
|
|
* so this will cause an error for that.
|
2011-06-26 18:02:09 -04:00
|
|
|
*/
|
2023-08-21 09:22:24 -06:00
|
|
|
if (sudoers_ctx.runas.argc > 1 && strcmp(sudoers_ctx.runas.argv[0], "-bash") == 0 &&
|
|
|
|
strcmp(sudoers_ctx.runas.argv[1], "-c") == 0) {
|
2021-08-09 15:50:26 -06:00
|
|
|
/* We allocated extra space for the --login above. */
|
2023-08-21 09:22:24 -06:00
|
|
|
memmove(&sudoers_ctx.runas.argv[2], &sudoers_ctx.runas.argv[1],
|
|
|
|
(size_t)sudoers_ctx.runas.argc * sizeof(char *));
|
|
|
|
sudoers_ctx.runas.argv[1] = (char *)"--login";
|
|
|
|
sudoers_ctx.runas.argc++;
|
2011-07-17 10:37:15 -04:00
|
|
|
}
|
2011-06-26 18:02:09 -04:00
|
|
|
|
2012-03-08 14:51:03 -05:00
|
|
|
#if defined(_AIX) || (defined(__linux__) && !defined(HAVE_PAM))
|
2010-03-14 19:58:47 -04:00
|
|
|
/* Insert system-wide environment variables. */
|
2023-08-21 10:47:23 -06:00
|
|
|
if (!read_env_file(&sudoers_ctx, _PATH_ENVIRONMENT, true, false))
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_warn("%s", _PATH_ENVIRONMENT);
|
2010-03-14 19:58:47 -04:00
|
|
|
#endif
|
2012-03-07 16:38:57 -05:00
|
|
|
#ifdef HAVE_LOGIN_CAP_H
|
|
|
|
/* Set environment based on login class. */
|
2023-08-21 09:21:49 -06:00
|
|
|
if (sudoers_ctx.runas.class) {
|
|
|
|
login_cap_t *lc = login_getclass(sudoers_ctx.runas.class);
|
2012-03-07 16:38:57 -05:00
|
|
|
if (lc != NULL) {
|
2023-08-21 09:21:49 -06:00
|
|
|
setusercontext(lc, sudoers_ctx.runas.pw,
|
|
|
|
sudoers_ctx.runas.pw->pw_uid, LOGIN_SETPATH|LOGIN_SETENV);
|
2012-03-07 16:38:57 -05:00
|
|
|
login_close(lc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* HAVE_LOGIN_CAP_H */
|
2010-03-14 19:58:47 -04:00
|
|
|
}
|
2004-01-18 22:55:32 +00:00
|
|
|
|
2010-03-14 19:58:47 -04:00
|
|
|
/* Insert system-wide environment variables. */
|
2017-03-22 13:39:25 -06:00
|
|
|
if (def_restricted_env_file) {
|
2023-08-21 10:47:23 -06:00
|
|
|
if (!read_env_file(&sudoers_ctx, def_restricted_env_file, false, true))
|
2017-03-22 13:39:25 -06:00
|
|
|
sudo_warn("%s", def_restricted_env_file);
|
|
|
|
}
|
2014-04-30 16:57:12 -06:00
|
|
|
if (def_env_file) {
|
2023-08-21 10:47:23 -06:00
|
|
|
if (!read_env_file(&sudoers_ctx, def_env_file, false, false))
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_warn("%s", def_env_file);
|
2014-04-30 16:57:12 -06:00
|
|
|
}
|
2007-12-21 21:53:32 +00:00
|
|
|
|
2010-03-14 19:58:47 -04:00
|
|
|
/* Insert user-specified environment variables. */
|
2023-08-21 09:21:49 -06:00
|
|
|
if (!insert_env_vars(sudoers_ctx.user.env_vars)) {
|
2022-12-28 14:07:43 -07:00
|
|
|
sudo_warnx("%s",
|
|
|
|
U_("error setting user-specified environment variables"));
|
2023-04-05 13:35:09 -06:00
|
|
|
goto error;
|
2022-12-28 14:07:43 -07:00
|
|
|
}
|
2004-01-18 22:55:32 +00:00
|
|
|
|
2014-07-12 10:35:00 -06:00
|
|
|
/* Note: must call audit before uid change. */
|
2023-08-21 10:47:23 -06:00
|
|
|
if (ISSET(sudoers_ctx.mode, MODE_EDIT)) {
|
2023-01-12 15:55:27 -07:00
|
|
|
const char *env_editor = NULL;
|
2020-02-24 19:59:44 -07:00
|
|
|
char **edit_argv;
|
2014-07-12 10:35:00 -06:00
|
|
|
int edit_argc;
|
|
|
|
|
2023-08-21 10:47:25 -06:00
|
|
|
sudoers_ctx.sudoedit_nfiles = sudoers_ctx.runas.argc - 1;
|
2023-08-21 09:21:49 -06:00
|
|
|
free(sudoers_ctx.runas.cmnd);
|
2023-08-21 10:47:25 -06:00
|
|
|
sudoers_ctx.runas.cmnd = find_editor(sudoers_ctx.sudoedit_nfiles,
|
2023-08-21 09:22:24 -06:00
|
|
|
sudoers_ctx.runas.argv + 1, &edit_argc, &edit_argv, NULL, &env_editor);
|
2023-08-21 09:21:49 -06:00
|
|
|
if (sudoers_ctx.runas.cmnd == NULL) {
|
2023-01-12 15:55:27 -07:00
|
|
|
switch (errno) {
|
|
|
|
case ENOENT:
|
2023-08-21 09:22:24 -06:00
|
|
|
audit_failure(&sudoers_ctx, sudoers_ctx.runas.argv,
|
2023-08-21 09:21:49 -06:00
|
|
|
N_("%s: command not found"),
|
2023-01-12 15:55:27 -07:00
|
|
|
env_editor ? env_editor : def_editor);
|
|
|
|
sudo_warnx(U_("%s: command not found"),
|
|
|
|
env_editor ? env_editor : def_editor);
|
2023-04-05 13:35:09 -06:00
|
|
|
goto error;
|
2023-01-12 15:55:27 -07:00
|
|
|
case EINVAL:
|
|
|
|
if (def_env_editor && env_editor != NULL) {
|
|
|
|
/* User tried to do something funny with the editor. */
|
2023-08-21 09:21:49 -06:00
|
|
|
log_warningx(&sudoers_ctx,
|
|
|
|
SLOG_NO_STDERR|SLOG_AUDIT|SLOG_SEND_MAIL,
|
2023-01-12 15:55:27 -07:00
|
|
|
"invalid user-specified editor: %s", env_editor);
|
2023-04-05 13:35:09 -06:00
|
|
|
goto error;
|
2023-01-12 15:55:27 -07:00
|
|
|
}
|
|
|
|
FALLTHROUGH;
|
|
|
|
default:
|
2023-04-05 13:35:09 -06:00
|
|
|
goto error;
|
2023-01-12 15:55:27 -07:00
|
|
|
}
|
2015-06-25 11:12:36 -06:00
|
|
|
}
|
2021-08-10 12:58:18 -06:00
|
|
|
/* find_editor() already g/c'd edit_argv[] */
|
2023-08-21 09:22:24 -06:00
|
|
|
if (sudoers_ctx.runas.argv != sudoers_ctx.runas.argv_saved) {
|
|
|
|
sudoers_gc_remove(GC_PTR, sudoers_ctx.runas.argv);
|
|
|
|
free(sudoers_ctx.runas.argv);
|
2022-06-30 11:10:43 -06:00
|
|
|
}
|
2023-08-21 09:22:24 -06:00
|
|
|
sudoers_ctx.runas.argv = edit_argv;
|
|
|
|
sudoers_ctx.runas.argc = edit_argc;
|
2015-03-16 20:19:24 -06:00
|
|
|
|
|
|
|
/* We want to run the editor with the unmodified environment. */
|
|
|
|
env_swap_old();
|
2010-05-13 14:09:21 -04:00
|
|
|
}
|
2004-01-22 02:57:01 +00:00
|
|
|
|
2022-06-30 11:10:43 -06:00
|
|
|
/* Save the initial command and argv so we have it for exit logging. */
|
2023-08-21 09:22:24 -06:00
|
|
|
if (sudoers_ctx.runas.cmnd_saved == NULL) {
|
|
|
|
sudoers_ctx.runas.cmnd_saved = strdup(sudoers_ctx.runas.cmnd);
|
|
|
|
if (sudoers_ctx.runas.cmnd_saved == NULL) {
|
2022-06-30 11:10:43 -06:00
|
|
|
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
|
2023-04-05 13:35:09 -06:00
|
|
|
goto error;
|
2022-06-30 11:10:43 -06:00
|
|
|
}
|
2023-08-21 09:22:24 -06:00
|
|
|
sudoers_ctx.runas.argv_saved = sudoers_ctx.runas.argv;
|
2022-06-30 11:10:43 -06:00
|
|
|
}
|
|
|
|
|
2022-06-30 13:35:04 -06:00
|
|
|
ret = true;
|
2011-03-08 09:38:21 -05:00
|
|
|
goto done;
|
2005-11-11 22:23:08 +00:00
|
|
|
|
2011-03-08 09:38:21 -05:00
|
|
|
bad:
|
2016-09-08 16:38:08 -06:00
|
|
|
ret = false;
|
2023-04-05 13:35:09 -06:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
error:
|
|
|
|
ret = -1;
|
2010-04-20 17:00:31 -04:00
|
|
|
|
2010-03-14 19:58:47 -04:00
|
|
|
done:
|
2023-08-21 09:21:49 -06:00
|
|
|
mail_parse_errors(&sudoers_ctx);
|
2022-03-14 13:54:11 -06:00
|
|
|
|
2021-02-12 19:52:11 -07:00
|
|
|
if (def_group_plugin)
|
|
|
|
group_plugin_unload();
|
2023-05-02 13:27:17 -06:00
|
|
|
reset_parser();
|
2021-02-12 19:52:11 -07:00
|
|
|
|
2020-11-17 13:44:32 -07:00
|
|
|
if (ret == -1) {
|
|
|
|
/* Free stashed copy of the environment. */
|
|
|
|
(void)env_init(NULL);
|
2022-12-28 10:50:51 -07:00
|
|
|
|
|
|
|
/* Free locally-allocated strings. */
|
|
|
|
free(iolog_path);
|
2020-11-17 13:44:32 -07:00
|
|
|
} else {
|
|
|
|
/* Store settings to pass back to front-end. */
|
2023-08-21 09:22:24 -06:00
|
|
|
if (!sudoers_policy_store_result(&sudoers_ctx, ret,
|
|
|
|
sudoers_ctx.runas.argv, env_get(), cmnd_umask, iolog_path, closure))
|
2020-02-24 19:59:44 -07:00
|
|
|
ret = -1;
|
|
|
|
}
|
2020-01-30 13:25:36 -07:00
|
|
|
|
2014-04-10 16:11:47 -06:00
|
|
|
if (!rewind_perms())
|
2016-09-08 16:38:08 -06:00
|
|
|
ret = -1;
|
2011-03-08 09:38:21 -05:00
|
|
|
|
2015-06-12 15:30:06 -06:00
|
|
|
restore_nproc();
|
|
|
|
|
2015-05-11 14:51:32 -06:00
|
|
|
sudo_warn_set_locale_func(NULL);
|
|
|
|
|
2016-09-08 16:38:08 -06:00
|
|
|
debug_return_int(ret);
|
1993-02-16 17:07:38 +00:00
|
|
|
}
|
|
|
|
|
2023-04-05 13:35:09 -06:00
|
|
|
/*
|
|
|
|
* Validate the user and update their timestamp file entry.
|
|
|
|
* Returns true if allowed, false if denied, -1 on error and
|
|
|
|
* -2 for usage error.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sudoers_validate_user(void)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
debug_decl(sudoers_validate_user, SUDOERS_DEBUG_PLUGIN);
|
|
|
|
|
|
|
|
sudo_warn_set_locale_func(sudoers_warn_setlocale);
|
|
|
|
|
|
|
|
unlimit_nproc();
|
|
|
|
|
2023-08-21 09:21:49 -06:00
|
|
|
if (!set_perms(&sudoers_ctx, PERM_INITIAL))
|
2023-04-05 13:35:09 -06:00
|
|
|
goto done;
|
|
|
|
|
2023-08-21 09:22:24 -06:00
|
|
|
sudoers_ctx.runas.argv = reallocarray(NULL, 2, sizeof(char *));
|
|
|
|
if (sudoers_ctx.runas.argv == NULL) {
|
2023-04-05 13:35:09 -06:00
|
|
|
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
|
|
|
|
goto done;
|
|
|
|
}
|
2023-08-21 09:22:24 -06:00
|
|
|
sudoers_gc_add(GC_PTR, sudoers_ctx.runas.argv);
|
|
|
|
sudoers_ctx.runas.argv[0] = (char *)"validate";
|
|
|
|
sudoers_ctx.runas.argv[1] = NULL;
|
|
|
|
sudoers_ctx.runas.argc = 2;
|
2023-04-05 13:35:09 -06:00
|
|
|
|
2023-08-21 09:21:49 -06:00
|
|
|
ret = sudoers_check_common(&sudoers_ctx, I_VERIFYPW);
|
2023-04-05 13:35:09 -06:00
|
|
|
|
|
|
|
done:
|
2023-08-21 09:21:49 -06:00
|
|
|
mail_parse_errors(&sudoers_ctx);
|
2023-04-05 13:35:09 -06:00
|
|
|
|
|
|
|
if (def_group_plugin)
|
|
|
|
group_plugin_unload();
|
2023-05-02 13:27:17 -06:00
|
|
|
reset_parser();
|
2023-04-05 13:35:09 -06:00
|
|
|
env_init(NULL);
|
|
|
|
|
|
|
|
if (!rewind_perms())
|
|
|
|
ret = -1;
|
|
|
|
|
|
|
|
restore_nproc();
|
|
|
|
|
|
|
|
sudo_warn_set_locale_func(NULL);
|
|
|
|
|
|
|
|
debug_return_int(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* List a user's privileges or check whether a specific command may be run.
|
|
|
|
* Returns true if allowed, false if denied, -1 on error and
|
|
|
|
* -2 for usage error.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sudoers_list(int argc, char * const argv[], const char *list_user, bool verbose)
|
|
|
|
{
|
2023-08-13 09:34:57 -06:00
|
|
|
struct passwd *pw;
|
2023-04-05 13:35:09 -06:00
|
|
|
int ret = -1;
|
|
|
|
debug_decl(sudoers_list, SUDOERS_DEBUG_PLUGIN);
|
|
|
|
|
|
|
|
sudo_warn_set_locale_func(sudoers_warn_setlocale);
|
|
|
|
|
|
|
|
unlimit_nproc();
|
|
|
|
|
2023-08-21 09:21:49 -06:00
|
|
|
if (!set_perms(&sudoers_ctx, PERM_INITIAL))
|
2023-04-05 13:35:09 -06:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
if (list_user) {
|
2023-08-21 09:21:49 -06:00
|
|
|
if (sudoers_ctx.runas.list_pw != NULL)
|
|
|
|
sudo_pw_delref(sudoers_ctx.runas.list_pw);
|
|
|
|
sudoers_ctx.runas.list_pw = sudo_getpwnam(list_user);
|
|
|
|
if (sudoers_ctx.runas.list_pw == NULL) {
|
2023-04-05 13:35:09 -06:00
|
|
|
sudo_warnx(U_("unknown user %s"), list_user);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-21 09:22:24 -06:00
|
|
|
sudoers_ctx.runas.argv = reallocarray(NULL, (size_t)argc + 2, sizeof(char *));
|
|
|
|
if (sudoers_ctx.runas.argv == NULL) {
|
2023-04-05 13:35:09 -06:00
|
|
|
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
|
|
|
|
goto done;
|
|
|
|
}
|
2023-08-21 09:22:24 -06:00
|
|
|
sudoers_gc_add(GC_PTR, sudoers_ctx.runas.argv);
|
|
|
|
sudoers_ctx.runas.argv[0] = (char *)"list";
|
2023-04-05 13:35:09 -06:00
|
|
|
if (argc != 0)
|
2023-08-21 09:22:24 -06:00
|
|
|
memcpy(sudoers_ctx.runas.argv + 1, argv, (size_t)argc * sizeof(char *));
|
|
|
|
sudoers_ctx.runas.argc = argc + 1;
|
|
|
|
sudoers_ctx.runas.argv[sudoers_ctx.runas.argc] = NULL;
|
2023-04-05 13:35:09 -06:00
|
|
|
|
2023-08-21 09:21:49 -06:00
|
|
|
ret = sudoers_check_common(&sudoers_ctx, I_LISTPW);
|
2023-04-05 13:35:09 -06:00
|
|
|
if (ret != true)
|
|
|
|
goto done;
|
|
|
|
|
2023-08-21 09:21:49 -06:00
|
|
|
pw = sudoers_ctx.runas.list_pw ? sudoers_ctx.runas.list_pw : sudoers_ctx.user.pw;
|
2023-08-21 10:47:23 -06:00
|
|
|
if (ISSET(sudoers_ctx.mode, MODE_CHECK))
|
2023-08-21 09:21:49 -06:00
|
|
|
ret = display_cmnd(&sudoers_ctx, snl, pw, verbose);
|
2023-04-05 13:35:09 -06:00
|
|
|
else
|
2023-08-21 09:21:49 -06:00
|
|
|
ret = display_privs(&sudoers_ctx, snl, pw, verbose);
|
2023-04-05 13:35:09 -06:00
|
|
|
|
|
|
|
done:
|
2023-08-21 09:21:49 -06:00
|
|
|
mail_parse_errors(&sudoers_ctx);
|
2023-04-05 13:35:09 -06:00
|
|
|
|
|
|
|
if (def_group_plugin)
|
|
|
|
group_plugin_unload();
|
2023-05-02 13:27:17 -06:00
|
|
|
reset_parser();
|
2023-04-05 13:35:09 -06:00
|
|
|
env_init(NULL);
|
|
|
|
|
|
|
|
if (!rewind_perms())
|
|
|
|
ret = -1;
|
|
|
|
|
|
|
|
restore_nproc();
|
|
|
|
|
|
|
|
sudo_warn_set_locale_func(NULL);
|
|
|
|
|
|
|
|
debug_return_int(ret);
|
|
|
|
}
|
|
|
|
|
1999-07-22 12:55:17 +00:00
|
|
|
/*
|
2023-08-21 09:21:49 -06:00
|
|
|
* Initialize timezone and fill in ctx->user.
|
1993-02-16 17:07:38 +00:00
|
|
|
*/
|
2014-04-10 16:03:26 -06:00
|
|
|
static bool
|
2023-08-21 09:21:49 -06:00
|
|
|
init_vars(struct sudoers_context *ctx, char * const envp[])
|
1993-02-16 17:07:38 +00:00
|
|
|
{
|
2010-03-14 19:58:47 -04:00
|
|
|
char * const * ep;
|
2013-08-07 10:13:04 -06:00
|
|
|
bool unknown_user = false;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(init_vars, SUDOERS_DEBUG_PLUGIN);
|
1999-01-17 22:08:55 +00:00
|
|
|
|
2015-06-17 06:49:59 -06:00
|
|
|
if (!sudoers_initlocale(setlocale(LC_ALL, NULL), def_sudoers_locale)) {
|
2015-06-19 14:51:17 -06:00
|
|
|
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
|
2015-06-17 06:49:59 -06:00
|
|
|
debug_return_bool(false);
|
|
|
|
}
|
2012-11-08 15:37:43 -05:00
|
|
|
|
2017-07-20 12:02:22 -06:00
|
|
|
#define MATCHES(s, v) \
|
|
|
|
(strncmp((s), (v), sizeof(v) - 1) == 0 && (s)[sizeof(v) - 1] != '\0')
|
|
|
|
|
2005-02-13 05:33:59 +00:00
|
|
|
for (ep = envp; *ep; ep++) {
|
|
|
|
switch (**ep) {
|
2007-07-15 19:44:46 +00:00
|
|
|
case 'K':
|
2017-07-20 12:02:22 -06:00
|
|
|
if (MATCHES(*ep, "KRB5CCNAME="))
|
2023-08-21 09:21:49 -06:00
|
|
|
ctx->user.ccname = *ep + sizeof("KRB5CCNAME=") - 1;
|
2007-07-15 19:44:46 +00:00
|
|
|
break;
|
2005-02-13 05:33:59 +00:00
|
|
|
case 'P':
|
2017-07-20 12:02:22 -06:00
|
|
|
if (MATCHES(*ep, "PATH="))
|
2023-08-21 09:21:49 -06:00
|
|
|
ctx->user.path = *ep + sizeof("PATH=") - 1;
|
2005-02-13 05:33:59 +00:00
|
|
|
break;
|
|
|
|
case 'S':
|
2017-07-21 09:07:00 -06:00
|
|
|
if (MATCHES(*ep, "SUDO_PROMPT=")) {
|
|
|
|
/* Don't override "sudo -p prompt" */
|
2023-08-21 09:21:49 -06:00
|
|
|
if (ctx->user.prompt == NULL)
|
|
|
|
ctx->user.prompt = *ep + sizeof("SUDO_PROMPT=") - 1;
|
2017-07-21 09:07:00 -06:00
|
|
|
break;
|
2017-07-20 11:40:49 -06:00
|
|
|
}
|
2017-07-21 09:07:00 -06:00
|
|
|
if (MATCHES(*ep, "SUDO_USER="))
|
|
|
|
prev_user = *ep + sizeof("SUDO_USER=") - 1;
|
2005-02-13 05:33:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-07-20 12:02:22 -06:00
|
|
|
#undef MATCHES
|
2005-02-13 05:33:59 +00:00
|
|
|
|
2023-08-21 09:21:49 -06:00
|
|
|
if (ctx->user.pw == NULL) {
|
|
|
|
/* Fake a struct passwd for the call to log_warningx(). */
|
|
|
|
ctx->user.pw = sudo_mkpwent(ctx->user.name, ctx->user.uid,
|
|
|
|
ctx->user.gid, NULL, NULL);
|
|
|
|
unknown_user = true;
|
1994-05-29 22:36:44 +00:00
|
|
|
}
|
2023-08-21 09:21:49 -06:00
|
|
|
if (ctx->user.gid_list == NULL)
|
|
|
|
ctx->user.gid_list = sudo_get_gidlist(ctx->user.pw, ENTRY_TYPE_ANY);
|
2016-08-10 10:56:05 -06:00
|
|
|
|
|
|
|
/* Store initialize permissions so we can restore them later. */
|
2023-08-21 09:21:49 -06:00
|
|
|
if (!set_perms(ctx, PERM_INITIAL))
|
2014-04-10 16:11:47 -06:00
|
|
|
debug_return_bool(false);
|
2011-07-20 11:58:45 -04:00
|
|
|
|
2020-10-26 16:10:42 -06:00
|
|
|
/* Set parse callbacks */
|
|
|
|
set_callbacks();
|
2020-09-09 19:18:24 -06:00
|
|
|
|
2014-05-02 16:40:30 -06:00
|
|
|
/* It is now safe to use log_warningx() and set_perms() */
|
2014-04-10 16:03:26 -06:00
|
|
|
if (unknown_user) {
|
2023-08-21 09:21:49 -06:00
|
|
|
log_warningx(ctx, SLOG_SEND_MAIL, N_("unknown user %s"), ctx->user.name);
|
2014-04-10 16:03:26 -06:00
|
|
|
debug_return_bool(false);
|
|
|
|
}
|
2016-07-18 12:11:25 -06:00
|
|
|
|
2020-12-20 08:43:34 -07:00
|
|
|
/*
|
|
|
|
* Set runas passwd/group entries based on command line or sudoers.
|
|
|
|
* Note that if runas_group was specified without runas_user we
|
|
|
|
* run the command as the invoking user.
|
|
|
|
*/
|
2023-08-21 09:21:49 -06:00
|
|
|
if (ctx->runas.group != NULL) {
|
|
|
|
if (!set_runasgr(ctx, ctx->runas.group, false))
|
2020-12-20 08:43:34 -07:00
|
|
|
debug_return_bool(false);
|
2023-08-21 09:21:49 -06:00
|
|
|
if (!set_runaspw(ctx, ctx->runas.user ?
|
|
|
|
ctx->runas.user : ctx->user.name, false))
|
2020-12-20 08:43:34 -07:00
|
|
|
debug_return_bool(false);
|
|
|
|
} else {
|
2023-08-21 09:21:49 -06:00
|
|
|
if (!set_runaspw(ctx, ctx->runas.user ?
|
|
|
|
ctx->runas.user : def_runas_default, false))
|
2020-12-20 08:43:34 -07:00
|
|
|
debug_return_bool(false);
|
|
|
|
}
|
|
|
|
|
2014-04-10 16:03:26 -06:00
|
|
|
debug_return_bool(true);
|
2004-10-26 22:22:46 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 15:26:44 -06:00
|
|
|
/*
|
2023-08-21 09:21:49 -06:00
|
|
|
* Fill in ctx->user.cmnd and ctx->user.cmnd_stat variables.
|
|
|
|
* Does not fill in ctx->user.cmnd_base.
|
2020-09-09 15:26:44 -06:00
|
|
|
*/
|
|
|
|
int
|
2023-08-21 09:21:49 -06:00
|
|
|
set_cmnd_path(struct sudoers_context *ctx, const char *runchroot)
|
2020-09-09 15:26:44 -06:00
|
|
|
{
|
2022-12-11 13:46:00 -07:00
|
|
|
const char *cmnd_in;
|
|
|
|
char *cmnd_out = NULL;
|
2023-08-21 09:21:49 -06:00
|
|
|
char *path = ctx->user.path;
|
2023-02-21 13:24:33 -07:00
|
|
|
int ret, pivot_fds[2];
|
2020-09-09 15:26:44 -06:00
|
|
|
debug_decl(set_cmnd_path, SUDOERS_DEBUG_PLUGIN);
|
|
|
|
|
2023-08-21 10:47:23 -06:00
|
|
|
cmnd_in = ISSET(ctx->mode, MODE_CHECK) ?
|
2023-08-21 09:22:24 -06:00
|
|
|
ctx->runas.argv[1] : ctx->runas.argv[0];
|
2022-12-11 13:46:00 -07:00
|
|
|
|
2023-08-21 09:21:49 -06:00
|
|
|
free(ctx->user.cmnd_list);
|
|
|
|
ctx->user.cmnd_list = NULL;
|
|
|
|
free(ctx->user.cmnd);
|
|
|
|
ctx->user.cmnd = NULL;
|
|
|
|
canon_path_free(ctx->user.cmnd_dir);
|
|
|
|
ctx->user.cmnd_dir = NULL;
|
|
|
|
if (def_secure_path && !user_is_exempt(ctx))
|
2020-09-09 15:26:44 -06:00
|
|
|
path = def_secure_path;
|
2023-02-21 13:24:33 -07:00
|
|
|
|
|
|
|
/* Pivot root. */
|
|
|
|
if (runchroot != NULL) {
|
|
|
|
if (!pivot_root(runchroot, pivot_fds))
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2023-08-21 09:21:49 -06:00
|
|
|
if (!set_perms(ctx, PERM_RUNAS))
|
2022-12-11 14:29:14 -07:00
|
|
|
goto error;
|
2023-08-21 09:21:49 -06:00
|
|
|
ret = find_path(cmnd_in, &cmnd_out, ctx->user.cmnd_stat, path,
|
2023-08-12 10:39:59 -06:00
|
|
|
def_ignore_dot, NULL);
|
2020-09-09 15:26:44 -06:00
|
|
|
if (!restore_perms())
|
2022-12-11 14:29:14 -07:00
|
|
|
goto error;
|
2020-09-09 15:26:44 -06:00
|
|
|
if (ret == NOT_FOUND) {
|
|
|
|
/* Failed as root, try as invoking user. */
|
2023-08-21 09:21:49 -06:00
|
|
|
if (!set_perms(ctx, PERM_USER))
|
2022-12-11 14:29:14 -07:00
|
|
|
goto error;
|
2023-08-21 09:21:49 -06:00
|
|
|
ret = find_path(cmnd_in, &cmnd_out, ctx->user.cmnd_stat, path,
|
2023-02-21 13:24:33 -07:00
|
|
|
def_ignore_dot, NULL);
|
2020-09-09 15:26:44 -06:00
|
|
|
if (!restore_perms())
|
2022-12-11 14:29:14 -07:00
|
|
|
goto error;
|
2020-09-09 15:26:44 -06:00
|
|
|
}
|
|
|
|
|
2023-02-21 13:24:33 -07:00
|
|
|
if (cmnd_out != NULL) {
|
|
|
|
char *slash = strrchr(cmnd_out, '/');
|
|
|
|
if (slash != NULL) {
|
|
|
|
*slash = '\0';
|
2023-08-21 09:21:49 -06:00
|
|
|
ctx->user.cmnd_dir = canon_path(cmnd_out);
|
|
|
|
if (ctx->user.cmnd_dir == NULL && errno == ENOMEM)
|
2023-02-21 13:24:33 -07:00
|
|
|
goto error;
|
|
|
|
*slash = '/';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-21 10:47:23 -06:00
|
|
|
if (ISSET(ctx->mode, MODE_CHECK))
|
2023-08-21 09:21:49 -06:00
|
|
|
ctx->user.cmnd_list = cmnd_out;
|
2022-12-11 13:46:00 -07:00
|
|
|
else
|
2023-08-21 09:21:49 -06:00
|
|
|
ctx->user.cmnd = cmnd_out;
|
2022-12-11 13:46:00 -07:00
|
|
|
|
2023-02-21 13:24:33 -07:00
|
|
|
/* Restore root. */
|
|
|
|
if (runchroot != NULL)
|
|
|
|
(void)unpivot_root(pivot_fds);
|
|
|
|
|
2020-09-09 15:26:44 -06:00
|
|
|
debug_return_int(ret);
|
2022-12-11 14:29:14 -07:00
|
|
|
error:
|
2023-02-21 13:24:33 -07:00
|
|
|
if (runchroot != NULL)
|
|
|
|
(void)unpivot_root(pivot_fds);
|
2022-12-11 14:29:14 -07:00
|
|
|
free(cmnd_out);
|
|
|
|
debug_return_int(NOT_FOUND_ERROR);
|
2020-09-09 15:26:44 -06:00
|
|
|
}
|
|
|
|
|
2023-08-10 15:49:30 -06:00
|
|
|
/*
|
2023-08-21 09:21:49 -06:00
|
|
|
* Fill in ctx->user.cmnd, ctx->user.cmnd_stat and cmnd_status variables.
|
|
|
|
* Does not fill in ctx->user.cmnd_base.
|
2023-08-10 15:49:30 -06:00
|
|
|
*/
|
|
|
|
void
|
2023-08-21 09:21:49 -06:00
|
|
|
set_cmnd_status(struct sudoers_context *ctx, const char *runchroot)
|
2023-08-10 15:49:30 -06:00
|
|
|
{
|
2023-08-21 09:21:49 -06:00
|
|
|
cmnd_status = set_cmnd_path(ctx, runchroot);
|
2023-08-10 15:49:30 -06:00
|
|
|
}
|
|
|
|
|
2004-10-26 22:22:46 +00:00
|
|
|
/*
|
2023-08-21 09:21:49 -06:00
|
|
|
* Fill in ctx->user.cmnd, ctx->user.cmnd_args, ctx->user.cmnd_base and
|
|
|
|
* ctx->user.cmnd_stat variables and apply any command-specific defaults entries.
|
2004-10-26 22:22:46 +00:00
|
|
|
*/
|
|
|
|
static int
|
2023-08-21 09:21:49 -06:00
|
|
|
set_cmnd(struct sudoers_context *ctx)
|
2004-10-26 22:22:46 +00:00
|
|
|
{
|
2018-05-14 09:05:03 -06:00
|
|
|
struct sudo_nss *nss;
|
|
|
|
int ret = FOUND;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(set_cmnd, SUDOERS_DEBUG_PLUGIN);
|
2000-10-26 16:42:40 +00:00
|
|
|
|
2023-08-21 09:21:49 -06:00
|
|
|
/* Allocate ctx->user.cmnd_stat for find_path() and match functions. */
|
|
|
|
free(ctx->user.cmnd_stat);
|
|
|
|
ctx->user.cmnd_stat = calloc(1, sizeof(struct stat));
|
|
|
|
if (ctx->user.cmnd_stat == NULL) {
|
2015-06-19 14:51:17 -06:00
|
|
|
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
|
2015-06-17 06:49:59 -06:00
|
|
|
debug_return_int(NOT_FOUND_ERROR);
|
|
|
|
}
|
2010-03-14 19:58:47 -04:00
|
|
|
|
2021-08-09 15:50:25 -06:00
|
|
|
/* Re-initialize for when we are called multiple times. */
|
2023-08-21 09:21:49 -06:00
|
|
|
free(ctx->runas.cmnd);
|
|
|
|
ctx->runas.cmnd = NULL;
|
2021-08-09 15:50:25 -06:00
|
|
|
|
2023-08-21 10:47:23 -06:00
|
|
|
if (ISSET(ctx->mode, MODE_RUN|MODE_EDIT|MODE_CHECK)) {
|
|
|
|
if (!ISSET(ctx->mode, MODE_EDIT)) {
|
2023-08-21 09:21:49 -06:00
|
|
|
const char *runchroot = ctx->runas.chroot;
|
2020-09-09 15:26:44 -06:00
|
|
|
if (runchroot == NULL && def_runchroot != NULL &&
|
|
|
|
strcmp(def_runchroot, "*") != 0)
|
|
|
|
runchroot = def_runchroot;
|
|
|
|
|
2023-08-21 09:21:49 -06:00
|
|
|
ret = set_cmnd_path(ctx, runchroot);
|
2016-09-08 16:38:08 -06:00
|
|
|
if (ret == NOT_FOUND_ERROR) {
|
2020-01-30 13:25:36 -07:00
|
|
|
if (errno == ENAMETOOLONG) {
|
2023-08-21 09:22:24 -06:00
|
|
|
audit_failure(ctx, ctx->runas.argv, N_("command too long"));
|
2020-01-30 13:25:36 -07:00
|
|
|
}
|
2023-08-21 09:22:24 -06:00
|
|
|
log_warning(ctx, 0, "%s", ctx->runas.argv[0]);
|
2016-09-08 16:38:08 -06:00
|
|
|
debug_return_int(ret);
|
2014-03-26 14:15:15 -06:00
|
|
|
}
|
2001-12-15 00:38:06 +00:00
|
|
|
}
|
2000-12-30 03:29:47 +00:00
|
|
|
|
2023-08-21 09:21:49 -06:00
|
|
|
/* set ctx->user.cmnd_args */
|
|
|
|
free(ctx->user.cmnd_args);
|
|
|
|
ctx->user.cmnd_args = NULL;
|
2023-08-21 10:47:23 -06:00
|
|
|
if (ISSET(ctx->mode, MODE_CHECK)) {
|
2023-08-21 09:22:24 -06:00
|
|
|
if (ctx->runas.argc > 2) {
|
|
|
|
/* Skip the command being listed in ctx->runas.argv[1]. */
|
|
|
|
ctx->user.cmnd_args = strvec_join(ctx->runas.argv + 2, ' ', NULL);
|
2023-08-21 09:21:49 -06:00
|
|
|
if (ctx->user.cmnd_args == NULL)
|
2023-03-03 11:12:18 -07:00
|
|
|
debug_return_int(NOT_FOUND_ERROR);
|
|
|
|
}
|
2023-08-21 09:22:24 -06:00
|
|
|
} else if (ctx->runas.argc > 1) {
|
2023-08-21 10:47:23 -06:00
|
|
|
if (ISSET(ctx->mode, MODE_SHELL|MODE_LOGIN_SHELL) &&
|
|
|
|
ISSET(ctx->mode, MODE_RUN)) {
|
2011-07-29 10:10:40 -04:00
|
|
|
/*
|
|
|
|
* When running a command via a shell, the sudo front-end
|
|
|
|
* escapes potential meta chars. We unescape non-spaces
|
|
|
|
* for sudoers matching and logging purposes.
|
2022-09-29 13:43:20 -06:00
|
|
|
* TODO: move escaping to the policy plugin instead
|
2011-07-29 10:10:40 -04:00
|
|
|
*/
|
2023-08-21 09:22:24 -06:00
|
|
|
ctx->user.cmnd_args = strvec_join(ctx->runas.argv + 1, ' ',
|
|
|
|
strlcpy_unescape);
|
2011-07-29 10:10:40 -04:00
|
|
|
} else {
|
2023-08-21 09:22:24 -06:00
|
|
|
ctx->user.cmnd_args = strvec_join(ctx->runas.argv + 1, ' ',
|
|
|
|
NULL);
|
2000-12-30 03:29:47 +00:00
|
|
|
}
|
2023-08-21 09:21:49 -06:00
|
|
|
if (ctx->user.cmnd_args == NULL)
|
2021-01-27 21:27:14 -07:00
|
|
|
debug_return_int(NOT_FOUND_ERROR);
|
2000-12-30 03:29:47 +00:00
|
|
|
}
|
2004-01-21 22:25:10 +00:00
|
|
|
}
|
2023-08-21 09:21:49 -06:00
|
|
|
if (ctx->user.cmnd == NULL) {
|
2023-08-21 09:22:24 -06:00
|
|
|
ctx->user.cmnd = strdup(ctx->runas.argv[0]);
|
2023-08-21 09:21:49 -06:00
|
|
|
if (ctx->user.cmnd == NULL)
|
2021-02-16 09:32:34 -07:00
|
|
|
debug_return_int(NOT_FOUND_ERROR);
|
|
|
|
}
|
2023-08-21 09:21:49 -06:00
|
|
|
ctx->user.cmnd_base = sudo_basename(ctx->user.cmnd);
|
2000-12-30 03:29:47 +00:00
|
|
|
|
2019-11-05 15:18:34 -07:00
|
|
|
/* Convert "sudo sudoedit" -> "sudoedit" */
|
2023-08-21 10:47:23 -06:00
|
|
|
if (ISSET(ctx->mode, MODE_RUN) && strcmp(ctx->user.cmnd_base, "sudoedit") == 0) {
|
2021-02-16 09:32:34 -07:00
|
|
|
char *new_cmnd;
|
|
|
|
|
2023-08-21 10:47:23 -06:00
|
|
|
CLR(ctx->mode, MODE_RUN);
|
|
|
|
SET(ctx->mode, MODE_EDIT);
|
2020-08-12 13:45:09 -06:00
|
|
|
sudo_warnx("%s", U_("sudoedit doesn't need to be run via sudo"));
|
2021-02-16 09:32:34 -07:00
|
|
|
if ((new_cmnd = strdup("sudoedit")) == NULL) {
|
|
|
|
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
|
|
|
|
debug_return_int(NOT_FOUND_ERROR);
|
|
|
|
}
|
2023-08-21 09:21:49 -06:00
|
|
|
free(ctx->user.cmnd);
|
|
|
|
ctx->user.cmnd_base = ctx->user.cmnd = new_cmnd;
|
2019-11-05 15:18:34 -07:00
|
|
|
}
|
|
|
|
|
2018-05-14 09:05:03 -06:00
|
|
|
TAILQ_FOREACH(nss, snl, entries) {
|
2022-03-14 13:54:11 -06:00
|
|
|
/* Missing/invalid defaults is not a fatal error. */
|
2023-08-21 09:21:49 -06:00
|
|
|
(void)update_defaults(ctx, nss->parse_tree, NULL, SETDEF_CMND, false);
|
2014-05-02 20:54:01 -06:00
|
|
|
}
|
2004-11-19 21:35:12 +00:00
|
|
|
|
2016-09-08 16:38:08 -06:00
|
|
|
debug_return_int(ret);
|
1999-07-22 12:55:17 +00:00
|
|
|
}
|
|
|
|
|
2023-05-02 10:37:38 -06:00
|
|
|
static int
|
|
|
|
open_file(const char *path, int flags)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
debug_decl(open_file, SUDOERS_DEBUG_PLUGIN);
|
|
|
|
|
2023-08-21 09:21:49 -06:00
|
|
|
if (!set_perms(NULL, PERM_SUDOERS))
|
2023-05-02 10:37:38 -06:00
|
|
|
debug_return_int(-1);
|
|
|
|
|
|
|
|
fd = open(path, flags);
|
|
|
|
if (fd == -1 && errno == EACCES && geteuid() != ROOT_UID) {
|
|
|
|
/*
|
|
|
|
* If we tried to open sudoers as non-root but got EACCES,
|
|
|
|
* try again as root.
|
|
|
|
*/
|
|
|
|
int serrno = errno;
|
2023-08-21 09:21:49 -06:00
|
|
|
if (restore_perms() && set_perms(NULL, PERM_ROOT))
|
2023-05-02 10:37:38 -06:00
|
|
|
fd = open(path, flags);
|
|
|
|
errno = serrno;
|
|
|
|
}
|
|
|
|
if (!restore_perms()) {
|
|
|
|
/* unable to change back to root */
|
|
|
|
if (fd != -1) {
|
|
|
|
close(fd);
|
|
|
|
fd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
debug_return_int(fd);
|
|
|
|
}
|
|
|
|
|
1999-07-22 12:55:17 +00:00
|
|
|
/*
|
2020-10-30 10:15:30 -06:00
|
|
|
* Open sudoers file and check mode/owner/type.
|
2007-12-31 19:26:52 +00:00
|
|
|
* Returns a handle to the sudoers file or NULL on error.
|
1994-11-08 01:38:57 +00:00
|
|
|
*/
|
2004-09-27 16:01:54 +00:00
|
|
|
FILE *
|
2023-05-02 10:37:38 -06:00
|
|
|
open_sudoers(const char *path, char **outfile, bool doedit, bool *keepopen)
|
1994-11-08 01:38:57 +00:00
|
|
|
{
|
2023-05-02 10:37:38 -06:00
|
|
|
char fname[PATH_MAX];
|
2004-09-27 16:01:54 +00:00
|
|
|
FILE *fp = NULL;
|
2022-09-21 19:08:10 -06:00
|
|
|
struct stat sb;
|
|
|
|
int error, fd;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(open_sudoers, SUDOERS_DEBUG_PLUGIN);
|
1994-11-10 00:49:53 +00:00
|
|
|
|
2023-05-02 10:37:38 -06:00
|
|
|
fd = sudo_open_conf_path(path, fname, sizeof(fname), open_file);
|
2023-05-08 17:03:31 -06:00
|
|
|
error = sudo_secure_fd(fd, S_IFREG, sudoers_file_uid(), sudoers_file_gid(),
|
|
|
|
&sb);
|
2023-05-02 10:37:38 -06:00
|
|
|
switch (error) {
|
|
|
|
case SUDO_PATH_SECURE:
|
2022-10-04 09:33:44 -06:00
|
|
|
/*
|
|
|
|
* Make sure we can read the file so we can present the
|
|
|
|
* user with a reasonable error message (unlike the lexer).
|
|
|
|
*/
|
|
|
|
if ((fp = fdopen(fd, "r")) == NULL) {
|
2023-08-21 09:21:49 -06:00
|
|
|
log_warning(&sudoers_ctx, SLOG_PARSE_ERROR,
|
|
|
|
N_("unable to open %s"), fname);
|
2022-10-04 09:33:44 -06:00
|
|
|
} else {
|
2023-05-02 10:37:38 -06:00
|
|
|
fd = -1;
|
2022-10-04 09:33:44 -06:00
|
|
|
if (sb.st_size != 0 && fgetc(fp) == EOF) {
|
2023-08-21 09:21:49 -06:00
|
|
|
log_warning(&sudoers_ctx, SLOG_PARSE_ERROR,
|
|
|
|
N_("unable to read %s"), fname);
|
2022-10-04 09:33:44 -06:00
|
|
|
fclose(fp);
|
|
|
|
fp = NULL;
|
2012-02-22 13:04:03 -05:00
|
|
|
} else {
|
2022-10-04 09:33:44 -06:00
|
|
|
/* Rewind fp and set close on exec flag. */
|
|
|
|
rewind(fp);
|
2023-05-02 10:37:38 -06:00
|
|
|
(void)fcntl(fileno(fp), F_SETFD, 1);
|
2023-05-02 10:37:38 -06:00
|
|
|
if (outfile != NULL) {
|
|
|
|
*outfile = sudo_rcstr_dup(fname);
|
|
|
|
if (*outfile == NULL) {
|
|
|
|
sudo_warnx(U_("%s: %s"), __func__,
|
|
|
|
U_("unable to allocate memory"));
|
|
|
|
fclose(fp);
|
|
|
|
fp = NULL;
|
|
|
|
}
|
|
|
|
}
|
2012-02-22 13:04:03 -05:00
|
|
|
}
|
2022-10-04 09:33:44 -06:00
|
|
|
}
|
2023-05-02 10:37:38 -06:00
|
|
|
break;
|
|
|
|
case SUDO_PATH_MISSING:
|
2023-08-21 09:21:49 -06:00
|
|
|
log_warning(&sudoers_ctx, SLOG_PARSE_ERROR,
|
|
|
|
N_("unable to open %s"), path);
|
2023-05-02 10:37:38 -06:00
|
|
|
break;
|
|
|
|
case SUDO_PATH_BAD_TYPE:
|
2023-08-21 09:21:49 -06:00
|
|
|
log_warningx(&sudoers_ctx, SLOG_PARSE_ERROR,
|
|
|
|
N_("%s is not a regular file"), fname);
|
2023-05-02 10:37:38 -06:00
|
|
|
break;
|
|
|
|
case SUDO_PATH_WRONG_OWNER:
|
2023-08-21 09:21:49 -06:00
|
|
|
log_warningx(&sudoers_ctx, SLOG_PARSE_ERROR,
|
2023-05-02 10:37:38 -06:00
|
|
|
N_("%s is owned by uid %u, should be %u"), fname,
|
2023-05-08 17:03:31 -06:00
|
|
|
(unsigned int)sb.st_uid, (unsigned int)sudoers_file_uid());
|
2023-05-02 10:37:38 -06:00
|
|
|
break;
|
|
|
|
case SUDO_PATH_WORLD_WRITABLE:
|
2023-08-21 09:21:49 -06:00
|
|
|
log_warningx(&sudoers_ctx, SLOG_PARSE_ERROR,
|
|
|
|
N_("%s is world writable"), fname);
|
2023-05-02 10:37:38 -06:00
|
|
|
break;
|
|
|
|
case SUDO_PATH_GROUP_WRITABLE:
|
2023-08-21 09:21:49 -06:00
|
|
|
log_warningx(&sudoers_ctx, SLOG_PARSE_ERROR,
|
2023-05-02 10:37:38 -06:00
|
|
|
N_("%s is owned by gid %u, should be %u"), fname,
|
2023-05-08 17:03:31 -06:00
|
|
|
(unsigned int)sb.st_gid, (unsigned int)sudoers_file_gid());
|
2023-05-02 10:37:38 -06:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
sudo_warnx("%s: internal error, unexpected error %d", __func__, error);
|
|
|
|
break;
|
1998-01-21 05:03:46 +00:00
|
|
|
}
|
1995-11-16 07:41:35 +00:00
|
|
|
|
2023-05-02 10:37:38 -06:00
|
|
|
if (fp == NULL && fd != -1)
|
|
|
|
close(fd);
|
2012-02-22 13:04:03 -05:00
|
|
|
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_return_ptr(fp);
|
1994-11-08 01:38:57 +00:00
|
|
|
}
|
|
|
|
|
2000-10-26 16:42:40 +00:00
|
|
|
#ifdef HAVE_LOGIN_CAP_H
|
2014-04-10 16:03:26 -06:00
|
|
|
static bool
|
2023-08-21 09:21:49 -06:00
|
|
|
set_loginclass(struct sudoers_context *ctx)
|
2000-03-07 04:29:46 +00:00
|
|
|
{
|
2023-08-21 09:21:49 -06:00
|
|
|
const struct passwd *pw = ctx->runas.pw ? ctx->runas.pw : ctx->user.pw;
|
2023-07-10 11:06:23 -06:00
|
|
|
const unsigned int errflags = SLOG_RAW_MSG;
|
2011-12-08 17:17:25 -05:00
|
|
|
login_cap_t *lc;
|
2016-09-08 16:38:08 -06:00
|
|
|
bool ret = true;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(set_loginclass, SUDOERS_DEBUG_PLUGIN);
|
2000-03-24 20:13:12 +00:00
|
|
|
|
2011-12-02 14:29:54 -05:00
|
|
|
if (!def_use_loginclass)
|
2014-04-10 16:03:26 -06:00
|
|
|
goto done;
|
2011-12-02 14:29:54 -05:00
|
|
|
|
2023-08-21 09:21:49 -06:00
|
|
|
if (ctx->runas.class && strcmp(ctx->runas.class, "-") != 0) {
|
|
|
|
if (ctx->user.uid != 0 && pw->pw_uid != 0) {
|
|
|
|
sudo_warnx(U_("only root can use \"-c %s\""), ctx->runas.class);
|
2016-09-08 16:38:08 -06:00
|
|
|
ret = false;
|
2014-04-10 16:03:26 -06:00
|
|
|
goto done;
|
|
|
|
}
|
2000-03-24 20:13:12 +00:00
|
|
|
} else {
|
2023-08-21 09:21:49 -06:00
|
|
|
ctx->runas.class = pw->pw_class;
|
|
|
|
if (!ctx->runas.class || !*ctx->runas.class)
|
|
|
|
ctx->runas.class = (char *)
|
2022-06-28 16:33:15 -06:00
|
|
|
((pw->pw_uid == 0) ? LOGIN_DEFROOTCLASS : LOGIN_DEFCLASS);
|
2000-03-24 20:13:12 +00:00
|
|
|
}
|
2000-03-07 04:29:46 +00:00
|
|
|
|
2011-12-02 14:29:54 -05:00
|
|
|
/* Make sure specified login class is valid. */
|
2023-08-21 09:21:49 -06:00
|
|
|
lc = login_getclass(ctx->runas.class);
|
|
|
|
if (!lc || !lc->lc_class || strcmp(lc->lc_class, ctx->runas.class) != 0) {
|
2012-03-26 10:59:14 -04:00
|
|
|
/*
|
2014-04-10 16:03:26 -06:00
|
|
|
* Don't make it an error if the user didn't specify the login
|
2012-03-26 10:59:14 -04:00
|
|
|
* class themselves. We do this because if login.conf gets
|
|
|
|
* corrupted we want the admin to be able to use sudo to fix it.
|
|
|
|
*/
|
2023-08-21 09:21:49 -06:00
|
|
|
log_warningx(ctx, errflags, N_("unknown login class %s"),
|
|
|
|
ctx->runas.class);
|
2011-12-02 14:29:54 -05:00
|
|
|
def_use_loginclass = false;
|
2023-08-21 09:21:49 -06:00
|
|
|
if (ctx->runas.class)
|
2016-09-08 16:38:08 -06:00
|
|
|
ret = false;
|
2001-08-23 21:43:38 +00:00
|
|
|
}
|
2011-12-08 17:17:25 -05:00
|
|
|
login_close(lc);
|
2014-04-10 16:03:26 -06:00
|
|
|
done:
|
2016-09-08 16:38:08 -06:00
|
|
|
debug_return_bool(ret);
|
2000-03-07 04:29:46 +00:00
|
|
|
}
|
|
|
|
#else
|
2014-04-10 16:03:26 -06:00
|
|
|
static bool
|
2023-08-21 09:21:49 -06:00
|
|
|
set_loginclass(struct sudoers_context *ctx)
|
2000-03-07 04:29:46 +00:00
|
|
|
{
|
2014-04-10 16:03:26 -06:00
|
|
|
return true;
|
2000-03-07 04:29:46 +00:00
|
|
|
}
|
2000-10-26 16:42:40 +00:00
|
|
|
#endif /* HAVE_LOGIN_CAP_H */
|
2000-03-07 04:29:46 +00:00
|
|
|
|
2004-01-16 23:09:20 +00:00
|
|
|
/*
|
2010-08-04 09:58:50 -04:00
|
|
|
* Get passwd entry for the user we are going to run commands as
|
2023-08-21 09:21:49 -06:00
|
|
|
* and store it in ctx->runas.pw. By default, commands run as "root".
|
2004-01-16 23:09:20 +00:00
|
|
|
*/
|
2014-04-10 16:03:26 -06:00
|
|
|
static bool
|
2023-08-21 09:21:49 -06:00
|
|
|
set_runaspw(struct sudoers_context *ctx, const char *user, bool quiet)
|
2004-01-16 23:09:20 +00:00
|
|
|
{
|
2013-12-05 15:51:56 -07:00
|
|
|
struct passwd *pw = NULL;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(set_runaspw, SUDOERS_DEBUG_PLUGIN);
|
2011-10-22 14:40:21 -04:00
|
|
|
|
2019-12-09 17:14:06 -07:00
|
|
|
unknown_runas_uid = false;
|
2004-01-16 23:09:20 +00:00
|
|
|
if (*user == '#') {
|
2013-12-05 15:51:56 -07:00
|
|
|
const char *errstr;
|
2019-10-20 10:21:29 -06:00
|
|
|
uid_t uid = sudo_strtoid(user + 1, &errstr);
|
2013-12-05 15:51:56 -07:00
|
|
|
if (errstr == NULL) {
|
2019-12-09 17:14:06 -07:00
|
|
|
if ((pw = sudo_getpwuid(uid)) == NULL) {
|
|
|
|
unknown_runas_uid = true;
|
2023-08-21 09:21:49 -06:00
|
|
|
pw = sudo_fakepwnam(user, ctx->user.gid);
|
2019-12-09 17:14:06 -07:00
|
|
|
}
|
2013-12-05 15:51:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pw == NULL) {
|
2014-04-10 16:03:26 -06:00
|
|
|
if ((pw = sudo_getpwnam(user)) == NULL) {
|
|
|
|
if (!quiet)
|
2023-08-21 09:21:49 -06:00
|
|
|
log_warningx(ctx, SLOG_AUDIT, N_("unknown user %s"), user);
|
2014-04-10 16:03:26 -06:00
|
|
|
debug_return_bool(false);
|
|
|
|
}
|
2004-01-16 23:09:20 +00:00
|
|
|
}
|
2023-08-21 09:21:49 -06:00
|
|
|
if (ctx->runas.pw != NULL)
|
|
|
|
sudo_pw_delref(ctx->runas.pw);
|
|
|
|
ctx->runas.pw = pw;
|
2014-04-10 16:03:26 -06:00
|
|
|
debug_return_bool(true);
|
2004-01-16 23:09:20 +00:00
|
|
|
}
|
|
|
|
|
2007-11-21 20:12:00 +00:00
|
|
|
/*
|
2010-08-04 09:58:50 -04:00
|
|
|
* Get group entry for the group we are going to run commands as
|
2023-08-21 09:21:49 -06:00
|
|
|
* and store it in ctx->runas.gr.
|
2007-11-21 20:12:00 +00:00
|
|
|
*/
|
2014-04-10 16:03:26 -06:00
|
|
|
static bool
|
2023-08-21 09:21:49 -06:00
|
|
|
set_runasgr(struct sudoers_context *ctx, const char *group, bool quiet)
|
2007-11-21 20:12:00 +00:00
|
|
|
{
|
2013-12-05 15:51:56 -07:00
|
|
|
struct group *gr = NULL;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(set_runasgr, SUDOERS_DEBUG_PLUGIN);
|
2011-10-22 14:40:21 -04:00
|
|
|
|
2019-12-09 17:14:06 -07:00
|
|
|
unknown_runas_gid = false;
|
2007-11-21 20:12:00 +00:00
|
|
|
if (*group == '#') {
|
2013-12-05 15:51:56 -07:00
|
|
|
const char *errstr;
|
2019-10-20 10:21:29 -06:00
|
|
|
gid_t gid = sudo_strtoid(group + 1, &errstr);
|
2013-12-05 15:51:56 -07:00
|
|
|
if (errstr == NULL) {
|
2019-12-09 17:14:06 -07:00
|
|
|
if ((gr = sudo_getgrgid(gid)) == NULL) {
|
|
|
|
unknown_runas_gid = true;
|
2013-12-05 15:51:56 -07:00
|
|
|
gr = sudo_fakegrnam(group);
|
2019-12-09 17:14:06 -07:00
|
|
|
}
|
2013-12-05 15:51:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (gr == NULL) {
|
2014-04-10 16:03:26 -06:00
|
|
|
if ((gr = sudo_getgrnam(group)) == NULL) {
|
|
|
|
if (!quiet)
|
2023-08-21 09:21:49 -06:00
|
|
|
log_warningx(ctx, SLOG_AUDIT, N_("unknown group %s"), group);
|
2014-04-10 16:03:26 -06:00
|
|
|
debug_return_bool(false);
|
|
|
|
}
|
2007-11-21 20:12:00 +00:00
|
|
|
}
|
2023-08-21 09:21:49 -06:00
|
|
|
if (ctx->runas.gr != NULL)
|
|
|
|
sudo_gr_delref(ctx->runas.gr);
|
|
|
|
ctx->runas.gr = gr;
|
2014-04-10 16:03:26 -06:00
|
|
|
debug_return_bool(true);
|
2011-08-10 09:02:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Callback for runas_default sudoers setting.
|
|
|
|
*/
|
2023-08-10 15:49:30 -06:00
|
|
|
bool
|
2023-08-21 09:21:49 -06:00
|
|
|
cb_runas_default(struct sudoers_context *ctx, const char *file, int line,
|
|
|
|
int column, const union sudo_defs_val *sd_un, int op)
|
2011-08-10 09:02:37 -04:00
|
|
|
{
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(cb_runas_default, SUDOERS_DEBUG_PLUGIN);
|
2016-07-18 12:20:46 -06:00
|
|
|
|
2011-08-10 09:02:37 -04:00
|
|
|
/* Only reset runaspw if user didn't specify one. */
|
2023-08-21 09:21:49 -06:00
|
|
|
if (ctx->runas.user == NULL && ctx->runas.group == NULL)
|
|
|
|
debug_return_bool(set_runaspw(ctx, sd_un->str, true));
|
2016-07-18 12:20:46 -06:00
|
|
|
debug_return_bool(true);
|
2007-11-21 20:12:00 +00:00
|
|
|
}
|
|
|
|
|
2004-11-15 15:53:53 +00:00
|
|
|
/*
|
2014-06-27 09:30:52 -06:00
|
|
|
* Cleanup hook for sudo_fatal()/sudo_fatalx()
|
2021-08-09 15:50:25 -06:00
|
|
|
* Also called at policy close time.
|
2004-11-15 15:53:53 +00:00
|
|
|
*/
|
|
|
|
void
|
2012-11-25 09:34:40 -05:00
|
|
|
sudoers_cleanup(void)
|
2004-11-15 15:53:53 +00:00
|
|
|
{
|
2007-12-31 12:39:52 +00:00
|
|
|
struct sudo_nss *nss;
|
2021-08-09 15:50:25 -06:00
|
|
|
struct defaults *def;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(sudoers_cleanup, SUDOERS_DEBUG_PLUGIN);
|
2007-12-31 12:39:52 +00:00
|
|
|
|
2012-11-25 09:34:40 -05:00
|
|
|
if (snl != NULL) {
|
2013-10-22 09:00:37 -06:00
|
|
|
TAILQ_FOREACH(nss, snl, entries) {
|
2023-08-21 09:21:49 -06:00
|
|
|
nss->close(&sudoers_ctx, nss);
|
2013-10-22 09:00:37 -06:00
|
|
|
}
|
2021-02-12 15:36:18 -07:00
|
|
|
snl = NULL;
|
2023-05-02 13:27:17 -06:00
|
|
|
reset_parser();
|
2005-11-18 01:39:59 +00:00
|
|
|
}
|
2021-08-09 15:50:25 -06:00
|
|
|
while ((def = TAILQ_FIRST(&initial_defaults)) != NULL) {
|
|
|
|
TAILQ_REMOVE(&initial_defaults, def, entries);
|
|
|
|
free(def->var);
|
|
|
|
free(def->val);
|
|
|
|
free(def);
|
|
|
|
}
|
|
|
|
need_reinit = false;
|
2012-11-25 09:34:40 -05:00
|
|
|
if (def_group_plugin)
|
|
|
|
group_plugin_unload();
|
2023-08-21 09:21:52 -06:00
|
|
|
sudoers_ctx_free(&sudoers_ctx);
|
2016-05-11 07:06:45 -06:00
|
|
|
sudo_freepwcache();
|
|
|
|
sudo_freegrcache();
|
2023-02-21 13:24:33 -07:00
|
|
|
canon_path_free_cache();
|
2012-11-25 09:34:40 -05:00
|
|
|
|
2023-03-10 15:23:48 -07:00
|
|
|
/* We must free the cached environment before running g/c. */
|
|
|
|
env_init(NULL);
|
|
|
|
|
2023-03-10 15:03:44 -07:00
|
|
|
/* Run garbage collector. */
|
|
|
|
sudoers_gc_run();
|
|
|
|
|
2021-08-09 15:50:26 -06:00
|
|
|
/* Clear globals */
|
|
|
|
prev_user = NULL;
|
|
|
|
|
2012-11-25 09:34:40 -05:00
|
|
|
debug_return;
|
2004-11-15 15:53:53 +00:00
|
|
|
}
|
|
|
|
|
2013-04-01 10:40:17 -04:00
|
|
|
static bool
|
2023-08-21 09:21:49 -06:00
|
|
|
tty_present(struct sudoers_context *ctx)
|
2013-04-01 10:40:17 -04:00
|
|
|
{
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(tty_present, SUDOERS_DEBUG_PLUGIN);
|
2019-06-19 20:32:22 -06:00
|
|
|
|
2023-08-21 09:21:49 -06:00
|
|
|
if (ctx->user.tcpgid == 0 && ctx->user.ttypath == NULL) {
|
2022-09-15 16:34:49 -06:00
|
|
|
/* No job control or terminal, check /dev/tty. */
|
2019-06-19 20:32:22 -06:00
|
|
|
int fd = open(_PATH_TTY, O_RDWR);
|
|
|
|
if (fd == -1)
|
|
|
|
debug_return_bool(false);
|
2013-04-01 10:40:17 -04:00
|
|
|
close(fd);
|
2019-06-19 20:32:22 -06:00
|
|
|
}
|
|
|
|
debug_return_bool(true);
|
2013-04-01 10:40:17 -04:00
|
|
|
}
|
2023-08-21 09:21:49 -06:00
|
|
|
|
2023-08-21 10:47:23 -06:00
|
|
|
bool
|
|
|
|
sudoers_set_mode(unsigned int flags, unsigned int mask)
|
|
|
|
{
|
|
|
|
SET(sudoers_ctx.mode, flags);
|
|
|
|
return ((sudoers_ctx.mode & mask) == sudoers_ctx.mode);
|
|
|
|
}
|
|
|
|
|
2023-08-21 09:21:49 -06:00
|
|
|
const struct sudoers_context *
|
|
|
|
sudoers_get_context(void)
|
|
|
|
{
|
|
|
|
return &sudoers_ctx;
|
|
|
|
}
|