1993-11-27 23:42:49 +00:00
|
|
|
/*
|
2019-04-29 07:21:51 -06:00
|
|
|
* SPDX-License-Identifier: ISC
|
|
|
|
*
|
2022-01-28 08:52:41 -07:00
|
|
|
* Copyright (c) 1993-1996, 1998-2022 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"
|
2018-05-14 09:05:03 -06:00
|
|
|
#include "parse.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
|
|
|
*/
|
2011-07-18 16:23:38 -04:00
|
|
|
static int set_cmnd(void);
|
2014-04-10 16:03:26 -06:00
|
|
|
static bool init_vars(char * const *);
|
|
|
|
static bool set_loginclass(struct passwd *);
|
|
|
|
static bool set_runasgr(const char *, bool);
|
|
|
|
static bool set_runaspw(const char *, bool);
|
2013-04-01 10:40:17 -04:00
|
|
|
static bool tty_present(void);
|
2020-10-26 16:10:42 -06:00
|
|
|
static void set_callbacks(void);
|
2010-03-14 19:58:47 -04:00
|
|
|
|
1994-05-14 21:27:05 +00:00
|
|
|
/*
|
|
|
|
* Globals
|
|
|
|
*/
|
1999-07-22 12:55:17 +00:00
|
|
|
struct sudo_user sudo_user;
|
2010-08-21 08:48:35 -04:00
|
|
|
struct passwd *list_pw;
|
2002-11-22 18:23:24 +00:00
|
|
|
uid_t timestamp_uid;
|
2017-03-20 12:59:28 -06:00
|
|
|
gid_t timestamp_gid;
|
2019-10-18 06:20:27 -06:00
|
|
|
bool force_umask;
|
2010-07-12 17:57:53 -04:00
|
|
|
int sudo_mode;
|
2010-03-18 06:42:17 -04:00
|
|
|
|
2011-02-23 13:38:52 -05:00
|
|
|
static char *prev_user;
|
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;
|
2020-09-09 19:18:24 -06:00
|
|
|
static int cmnd_status = -1;
|
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
|
|
|
|
|
2010-04-30 12:01:15 -04:00
|
|
|
/* XXX - must be extern for audit bits of sudo_auth.c */
|
|
|
|
int NewArgc;
|
|
|
|
char **NewArgv;
|
2010-03-14 19:58:47 -04:00
|
|
|
|
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.
|
|
|
|
* We do not send mail for errors when reinitializing, mail would have
|
|
|
|
* already been sent the first time.
|
|
|
|
* TODO: prevent Defaults error logging too
|
|
|
|
*/
|
2021-08-09 15:50:25 -06:00
|
|
|
static bool
|
|
|
|
sudoers_reinit_defaults(void)
|
|
|
|
{
|
|
|
|
struct sudo_nss *nss, *nss_next;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!update_defaults(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);
|
|
|
|
|
|
|
|
TAILQ_FOREACH_SAFE(nss, snl, entries, nss_next) {
|
2022-03-14 13:54:11 -06:00
|
|
|
/* Missing/invalid defaults is not a fatal error. */
|
2022-03-09 12:38:25 -07:00
|
|
|
if (nss->getdefs(nss) == -1) {
|
|
|
|
log_warningx(SLOG_SEND_MAIL|SLOG_NO_STDERR,
|
|
|
|
N_("unable to get defaults from %s"), nss->source);
|
2022-03-14 13:54:11 -06:00
|
|
|
} else {
|
|
|
|
(void)update_defaults(nss->parse_tree, NULL,
|
|
|
|
SETDEF_GENERIC|SETDEF_HOST|SETDEF_USER|SETDEF_RUNAS, false);
|
2022-03-09 12:38:25 -07:00
|
|
|
}
|
2021-08-09 15:50:25 -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. */
|
2021-08-09 15:50:25 -06:00
|
|
|
sudo_mode = sudoers_policy_deserialize_info(info, &initial_defaults);
|
2014-04-07 05:34:56 -06:00
|
|
|
if (ISSET(sudo_mode, MODE_ERROR))
|
2015-05-07 10:33:23 -06:00
|
|
|
debug_return_int(-1);
|
2010-03-14 19:58:47 -04:00
|
|
|
|
2014-04-10 16:03:26 -06:00
|
|
|
if (!init_vars(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. */
|
2014-04-10 16:11:47 -06:00
|
|
|
if (!set_perms(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. */
|
|
|
|
if (!update_defaults(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. */
|
2019-11-05 15:18:34 -07:00
|
|
|
init_parser(sudoers_file, false, false);
|
2013-10-22 09:00:37 -06:00
|
|
|
TAILQ_FOREACH_SAFE(nss, snl, entries, nss_next) {
|
2018-07-26 15:12:33 -06:00
|
|
|
if (nss->open(nss) == -1 || (nss->parse_tree = nss->parse(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. */
|
2022-03-09 12:38:25 -07:00
|
|
|
if (nss->getdefs(nss) == -1) {
|
|
|
|
log_warningx(SLOG_SEND_MAIL|SLOG_NO_STDERR,
|
|
|
|
N_("unable to get defaults from %s"), nss->source);
|
2022-03-14 13:54:11 -06:00
|
|
|
} else {
|
|
|
|
(void)update_defaults(nss->parse_tree, NULL,
|
|
|
|
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). */
|
2014-04-10 16:03:26 -06:00
|
|
|
if (set_loginclass(runas_pw ? runas_pw : sudo_user.pw))
|
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:
|
2022-03-14 13:54:11 -06:00
|
|
|
mail_parse_errors();
|
|
|
|
|
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=".
|
|
|
|
* Sets sudo_user.iolog_file as a side effect.
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
format_iolog_path(void)
|
|
|
|
{
|
|
|
|
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),
|
|
|
|
&sudoers_iolog_path_escapes[1], NULL);
|
|
|
|
if (ok) {
|
|
|
|
ok = expand_iolog_path(def_iolog_file, file, sizeof(file),
|
|
|
|
&sudoers_iolog_path_escapes[0], dir);
|
|
|
|
}
|
|
|
|
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. */
|
|
|
|
sudo_user.iolog_path = iolog_path + sizeof("iolog_path=") - 1;
|
|
|
|
sudo_user.iolog_file = sudo_user.iolog_path + 1 + strlen(dir);
|
2019-10-24 20:04:33 -06:00
|
|
|
|
|
|
|
done:
|
|
|
|
debug_return_str(iolog_path);
|
|
|
|
}
|
|
|
|
|
2020-09-09 15:26:44 -06:00
|
|
|
static int
|
2020-09-09 19:18:24 -06:00
|
|
|
check_user_runchroot(void)
|
2020-09-09 15:26:44 -06:00
|
|
|
{
|
2020-09-09 19:18:24 -06:00
|
|
|
debug_decl(check_user_runchroot, SUDOERS_DEBUG_PLUGIN);
|
2020-09-09 15:26:44 -06:00
|
|
|
|
|
|
|
if (user_runchroot == NULL)
|
|
|
|
debug_return_bool(true);
|
|
|
|
|
|
|
|
sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
|
|
|
|
"def_runchroot %s, user_runchroot %s",
|
2020-09-23 08:59:18 -06:00
|
|
|
def_runchroot ? def_runchroot : "none",
|
|
|
|
user_runchroot ? user_runchroot : "none");
|
2020-09-09 15:26:44 -06:00
|
|
|
|
|
|
|
if (def_runchroot == NULL || (strcmp(def_runchroot, "*") != 0 &&
|
|
|
|
strcmp(def_runchroot, user_runchroot) != 0)) {
|
2020-09-23 08:18:55 -06:00
|
|
|
log_warningx(SLOG_NO_STDERR|SLOG_AUDIT,
|
2020-09-23 08:02:43 -06:00
|
|
|
N_("user not allowed to change root directory to %s"),
|
|
|
|
user_runchroot);
|
2020-09-09 19:18:24 -06:00
|
|
|
sudo_warnx(U_("you are not permitted to use the -R option with %s"),
|
|
|
|
user_cmnd);
|
2020-09-09 15:26:44 -06:00
|
|
|
debug_return_bool(false);
|
|
|
|
}
|
|
|
|
free(def_runchroot);
|
|
|
|
if ((def_runchroot = strdup(user_runchroot)) == NULL) {
|
|
|
|
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
|
2021-01-04 14:48:43 -07:00
|
|
|
debug_return_int(-1);
|
2020-09-09 15:26:44 -06:00
|
|
|
}
|
|
|
|
debug_return_bool(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2020-09-09 19:18:24 -06:00
|
|
|
check_user_runcwd(void)
|
2020-09-09 15:26:44 -06:00
|
|
|
{
|
2020-09-09 19:18:24 -06:00
|
|
|
debug_decl(check_user_runcwd, SUDOERS_DEBUG_PLUGIN);
|
2020-09-09 15:26:44 -06:00
|
|
|
|
|
|
|
sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
|
|
|
|
"def_runcwd %s, user_runcwd %s, user_cwd %s",
|
2020-09-23 08:59:18 -06:00
|
|
|
def_runcwd ? def_runcwd : "none", user_runcwd ? user_runcwd : "none",
|
|
|
|
user_cwd ? user_cwd : "none");
|
2020-09-09 15:26:44 -06:00
|
|
|
|
|
|
|
if (strcmp(user_cwd, user_runcwd) != 0) {
|
|
|
|
if (def_runcwd == NULL || strcmp(def_runcwd, "*") != 0) {
|
2020-09-23 08:18:55 -06:00
|
|
|
log_warningx(SLOG_NO_STDERR|SLOG_AUDIT,
|
2020-09-23 08:02:43 -06:00
|
|
|
N_("user not allowed to change directory to %s"), user_runcwd);
|
2020-09-09 19:18:24 -06:00
|
|
|
sudo_warnx(U_("you are not permitted to use the -D option with %s"),
|
|
|
|
user_cmnd);
|
2020-09-09 15:26:44 -06:00
|
|
|
debug_return_bool(false);
|
|
|
|
}
|
|
|
|
free(def_runcwd);
|
|
|
|
if ((def_runcwd = strdup(user_runcwd)) == NULL) {
|
|
|
|
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
|
|
|
|
debug_return_int(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug_return_bool(true);
|
|
|
|
}
|
|
|
|
|
2021-08-09 15:50:25 -06:00
|
|
|
static bool need_reinit;
|
|
|
|
|
2012-10-25 16:58:31 -04:00
|
|
|
int
|
2010-04-26 21:53:59 -04:00
|
|
|
sudoers_policy_main(int argc, char * const argv[], int pwflag, char *env_add[],
|
2018-08-05 07:17:34 -06:00
|
|
|
bool verbose, void *closure)
|
2010-03-14 19:58:47 -04:00
|
|
|
{
|
2012-10-25 13:16:58 -04:00
|
|
|
char *iolog_path = NULL;
|
2016-11-07 13:36:05 -07:00
|
|
|
mode_t cmnd_umask = ACCESSPERMS;
|
2020-09-09 19:18:24 -06:00
|
|
|
int oldlocale, validated, ret = -1;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(sudoers_policy_main, SUDOERS_DEBUG_PLUGIN);
|
1996-02-02 03:34:21 +00:00
|
|
|
|
2015-05-11 14:51:32 -06:00
|
|
|
sudo_warn_set_locale_func(sudoers_warn_setlocale);
|
|
|
|
|
2021-02-16 09:32:34 -07:00
|
|
|
if (argc == 0) {
|
|
|
|
sudo_warnx("%s", U_("no command specified"));
|
|
|
|
debug_return_int(-1);
|
|
|
|
}
|
|
|
|
|
2021-08-09 15:50:26 -06:00
|
|
|
/* Was previous command was intercepted? */
|
|
|
|
if (def_intercept)
|
|
|
|
SET(sudo_mode, MODE_POLICY_INTERCEPTED);
|
|
|
|
|
|
|
|
/* Only certain mode flags are legal for intercepted commands. */
|
|
|
|
if (ISSET(sudo_mode, MODE_POLICY_INTERCEPTED))
|
|
|
|
sudo_mode &= MODE_INTERCEPT_MASK;
|
|
|
|
|
2021-08-09 15:50:25 -06:00
|
|
|
/* Re-initialize defaults if we are called multiple times. */
|
|
|
|
if (need_reinit) {
|
|
|
|
if (!sudoers_reinit_defaults())
|
|
|
|
debug_return_int(-1);
|
|
|
|
}
|
|
|
|
need_reinit = true;
|
|
|
|
|
2015-06-12 15:30:06 -06:00
|
|
|
unlimit_nproc();
|
|
|
|
|
2004-10-26 22:22:46 +00:00
|
|
|
/* Is root even allowed to run sudo? */
|
|
|
|
if (user_uid == 0 && !def_root_sudo) {
|
2020-01-30 13:25:36 -07:00
|
|
|
/* Not an audit event (should it be?). */
|
2020-08-12 13:45:09 -06:00
|
|
|
sudo_warnx("%s",
|
|
|
|
U_("sudoers specifies that root is not allowed to sudo"));
|
2018-07-26 15:12:33 -06:00
|
|
|
goto bad;
|
2019-12-09 19:29:45 -07:00
|
|
|
}
|
1999-12-05 02:54:20 +00:00
|
|
|
|
2014-04-10 16:11:47 -06:00
|
|
|
if (!set_perms(PERM_INITIAL))
|
2018-07-26 15:12:33 -06:00
|
|
|
goto bad;
|
2010-04-20 17:00:31 -04:00
|
|
|
|
2010-04-26 20:41:28 -04:00
|
|
|
/* Environment variables specified on the command line. */
|
2010-04-26 21:28:58 -04:00
|
|
|
if (env_add != NULL && env_add[0] != NULL)
|
2010-04-26 20:41:28 -04:00
|
|
|
sudo_user.env_vars = env_add;
|
|
|
|
|
2010-03-21 08:54:06 -04:00
|
|
|
/*
|
2021-08-09 15:50:26 -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.
|
2010-03-21 08:54:06 -04:00
|
|
|
*/
|
2021-08-09 15:50:26 -06:00
|
|
|
if (NewArgv != NULL) {
|
|
|
|
sudoers_gc_remove(GC_PTR, NewArgv);
|
|
|
|
free(NewArgv);
|
|
|
|
}
|
2021-02-16 09:32:34 -07:00
|
|
|
NewArgc = argc;
|
|
|
|
NewArgv = reallocarray(NULL, NewArgc + 2, sizeof(char *));
|
|
|
|
if (NewArgv == NULL) {
|
|
|
|
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
sudoers_gc_add(GC_PTR, NewArgv);
|
|
|
|
memcpy(NewArgv, argv, argc * sizeof(char *));
|
|
|
|
NewArgv[NewArgc] = NULL;
|
|
|
|
if (ISSET(sudo_mode, MODE_LOGIN_SHELL) && runas_pw != NULL) {
|
|
|
|
NewArgv[0] = strdup(runas_pw->pw_shell);
|
|
|
|
if (NewArgv[0] == 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;
|
|
|
|
}
|
2021-02-16 09:32:34 -07:00
|
|
|
sudoers_gc_add(GC_PTR, NewArgv[0]);
|
2010-09-07 17:59:10 -04:00
|
|
|
}
|
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. */
|
|
|
|
if (ISSET(sudo_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. */
|
2011-07-18 16:23:38 -04:00
|
|
|
cmnd_status = set_cmnd();
|
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
|
|
|
|
2013-08-15 09:56:17 -06:00
|
|
|
/* Check for -C overriding def_closefrom. */
|
|
|
|
if (user_closefrom >= 0 && user_closefrom != def_closefrom) {
|
|
|
|
if (!def_closefrom_override) {
|
2020-09-23 08:18:55 -06:00
|
|
|
log_warningx(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;
|
|
|
|
}
|
|
|
|
def_closefrom = user_closefrom;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2012-11-08 15:37:43 -05:00
|
|
|
sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale);
|
2020-09-09 15:26:45 -06:00
|
|
|
validated = sudoers_lookup(snl, sudo_user.pw, &cmnd_status, pwflag);
|
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
|
|
|
|
2012-11-08 15:37:43 -05:00
|
|
|
/* Restore user's locale. */
|
|
|
|
sudoers_setlocale(oldlocale, NULL);
|
|
|
|
|
2015-06-17 06:49:59 -06:00
|
|
|
if (safe_cmnd == NULL) {
|
|
|
|
if ((safe_cmnd = strdup(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) {
|
2021-08-19 09:44:11 -06:00
|
|
|
log_warningx(SLOG_AUDIT, N_("unknown user %s"), runas_pw->pw_name);
|
2019-12-09 17:14:06 -07:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (runas_gr != NULL) {
|
|
|
|
if (unknown_runas_gid && !def_runas_allow_unknown_id) {
|
2021-08-19 09:44:11 -06:00
|
|
|
log_warningx(SLOG_AUDIT, N_("unknown group %s"),
|
2020-09-23 08:18:55 -06:00
|
|
|
runas_gr->gr_name);
|
2019-12-09 17:14:06 -07:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-05-03 22:48:17 +00:00
|
|
|
/*
|
|
|
|
* Look up the timestamp dir owner if one is specified.
|
|
|
|
*/
|
2003-12-30 22:20:21 +00:00
|
|
|
if (def_timestampowner) {
|
2013-12-05 15:51:56 -07:00
|
|
|
struct passwd *pw = NULL;
|
2002-05-03 22:48:17 +00:00
|
|
|
|
2013-12-05 15:51:56 -07:00
|
|
|
if (*def_timestampowner == '#') {
|
|
|
|
const char *errstr;
|
2019-10-20 10:21:29 -06:00
|
|
|
uid_t uid = sudo_strtoid(def_timestampowner + 1, &errstr);
|
2013-12-05 15:51:56 -07:00
|
|
|
if (errstr == NULL)
|
|
|
|
pw = sudo_getpwuid(uid);
|
|
|
|
}
|
|
|
|
if (pw == NULL)
|
2004-11-16 04:24:11 +00:00
|
|
|
pw = sudo_getpwnam(def_timestampowner);
|
2012-03-26 11:02:06 -04:00
|
|
|
if (pw != NULL) {
|
|
|
|
timestamp_uid = pw->pw_uid;
|
2017-03-20 12:59:28 -06:00
|
|
|
timestamp_gid = pw->pw_gid;
|
2012-06-13 16:21:45 -04:00
|
|
|
sudo_pw_delref(pw);
|
2012-03-26 11:02:06 -04:00
|
|
|
} else {
|
2020-01-30 13:25:36 -07:00
|
|
|
/* XXX - audit too? */
|
2014-05-02 20:54:01 -06:00
|
|
|
log_warningx(SLOG_SEND_MAIL,
|
|
|
|
N_("timestamp owner (%s): No such user"), def_timestampowner);
|
2012-03-26 11:02:06 -04:00
|
|
|
timestamp_uid = ROOT_UID;
|
2017-03-20 12:59:28 -06:00
|
|
|
timestamp_gid = ROOT_GID;
|
2012-03-26 11:02:06 -04:00
|
|
|
}
|
2002-05-03 22:48:17 +00:00
|
|
|
}
|
|
|
|
|
2010-03-21 08:54:06 -04:00
|
|
|
/* If no command line args and "shell_noargs" is not set, error out. */
|
2010-03-14 19:58:47 -04:00
|
|
|
if (ISSET(sudo_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
|
|
|
|
1999-10-08 01:12:50 +00:00
|
|
|
/* Bail if a tty is required and we don't have one. */
|
2013-04-01 10:40:17 -04:00
|
|
|
if (def_requiretty && !tty_present()) {
|
2020-09-23 08:18:55 -06:00
|
|
|
log_warningx(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
|
|
|
}
|
|
|
|
|
2019-12-09 19:29:45 -07:00
|
|
|
/* Check runas user's shell. */
|
|
|
|
if (!check_user_shell(runas_pw)) {
|
2020-09-23 08:18:55 -06:00
|
|
|
log_warningx(SLOG_RAW_MSG|SLOG_AUDIT,
|
|
|
|
N_("invalid shell for user %s: %s"),
|
2019-12-09 19:29:45 -07:00
|
|
|
runas_pw->pw_name, runas_pw->pw_shell);
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
if (ISSET(sudo_mode, MODE_EDIT) ||
|
|
|
|
(ISSET(sudo_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. */
|
2014-04-30 16:57:12 -06:00
|
|
|
if (!rebuild_env())
|
|
|
|
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. */
|
2016-09-05 19:44:46 -06:00
|
|
|
switch (check_user(validated, sudo_mode)) {
|
|
|
|
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. */
|
|
|
|
if (!log_denial(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
|
|
|
|
2020-09-09 19:18:24 -06:00
|
|
|
/* Check whether user_runchroot is permitted (if specified). */
|
|
|
|
switch (check_user_runchroot()) {
|
|
|
|
case true:
|
|
|
|
break;
|
|
|
|
case false:
|
|
|
|
goto bad;
|
|
|
|
default:
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check whether user_runcwd is permitted (if specified). */
|
|
|
|
switch (check_user_runcwd()) {
|
|
|
|
case true:
|
|
|
|
break;
|
|
|
|
case false:
|
|
|
|
goto bad;
|
|
|
|
default:
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2004-01-29 21:15:27 +00:00
|
|
|
/* If run as root with SUDO_USER set, set sudo_user.pw to that user. */
|
2004-10-26 22:22:46 +00:00
|
|
|
/* XXX - causes confusion when root is not listed in sudoers */
|
2021-01-23 08:43:59 -07:00
|
|
|
if (ISSET(sudo_mode, MODE_RUN|MODE_EDIT) && prev_user != NULL) {
|
2004-10-27 16:16:23 +00:00
|
|
|
if (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) {
|
2010-08-04 09:58:50 -04:00
|
|
|
if (sudo_user.pw != NULL)
|
2012-06-13 16:21:45 -04:00
|
|
|
sudo_pw_delref(sudo_user.pw);
|
2004-11-15 14:53:05 +00:00
|
|
|
sudo_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. */
|
2015-06-25 11:12:36 -06:00
|
|
|
if (!log_failure(validated, cmnd_status))
|
|
|
|
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. */
|
2015-06-25 11:12:36 -06:00
|
|
|
if (create_admin_success_flag() == -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) {
|
2020-06-02 09:07:46 -06:00
|
|
|
audit_failure(NewArgv, N_("command in current directory"));
|
2016-11-02 17:10:17 -06:00
|
|
|
sudo_warnx(U_("ignoring \"%s\" found in '.'\nUse \"sudo ./%s\" if this is the \"%s\" you wish to run."), user_cmnd, user_cmnd, 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) {
|
2013-02-20 15:35:26 -05:00
|
|
|
if (ISSET(sudo_mode, MODE_CHECK)) {
|
2020-06-02 09:07:46 -06:00
|
|
|
audit_failure(NewArgv, N_("%s: command not found"),
|
2014-05-13 09:41:53 -06:00
|
|
|
NewArgv[0]);
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_warnx(U_("%s: command not found"), NewArgv[0]);
|
2013-02-20 15:35:26 -05:00
|
|
|
} else {
|
2020-06-02 09:07:46 -06:00
|
|
|
audit_failure(NewArgv, N_("%s: command not found"),
|
2014-05-13 09:41:53 -06:00
|
|
|
user_cmnd);
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_warnx(U_("%s: command not found"), user_cmnd);
|
2021-10-11 11:05:58 -06:00
|
|
|
if (strncmp(user_cmnd, "cd", 2) == 0 && (user_cmnd[2] == '\0' ||
|
|
|
|
isblank((unsigned char)user_cmnd[2]))) {
|
|
|
|
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. */
|
|
|
|
if (!def_user_command_timeouts && user_timeout > 0) {
|
2020-09-23 08:18:55 -06:00
|
|
|
log_warningx(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. */
|
|
|
|
if (ISSET(sudo_mode, MODE_RUN) && !def_setenv) {
|
|
|
|
if (ISSET(sudo_mode, MODE_PRESERVE_ENV)) {
|
2020-09-23 08:18:55 -06:00
|
|
|
log_warningx(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 {
|
|
|
|
if (!validate_env_vars(sudo_user.env_vars))
|
|
|
|
goto bad;
|
|
|
|
}
|
2010-03-14 19:58:47 -04:00
|
|
|
}
|
1999-08-19 16:30:09 +00:00
|
|
|
|
2020-02-26 13:17:40 -07:00
|
|
|
if (ISSET(sudo_mode, (MODE_RUN | MODE_EDIT)) && !remote_iologs) {
|
2012-10-25 13:16:58 -04:00
|
|
|
if ((def_log_input || def_log_output) && def_iolog_file && def_iolog_dir) {
|
2019-10-24 20:04:33 -06:00
|
|
|
if ((iolog_path = format_iolog_path()) == NULL) {
|
2017-01-13 15:45:59 -07:00
|
|
|
if (!def_ignore_iolog_errors)
|
|
|
|
goto done;
|
|
|
|
/* Unable to expand I/O log path, disable I/O logging. */
|
|
|
|
def_log_input = false;
|
|
|
|
def_log_output = false;
|
|
|
|
}
|
2010-12-27 12:18:32 -05:00
|
|
|
}
|
2010-12-20 16:28:20 -05:00
|
|
|
}
|
|
|
|
|
2015-06-25 11:12:36 -06:00
|
|
|
switch (sudo_mode & MODE_MASK) {
|
|
|
|
case MODE_CHECK:
|
2016-09-08 16:38:08 -06:00
|
|
|
ret = display_cmnd(snl, list_pw ? list_pw : sudo_user.pw);
|
2015-06-25 11:12:36 -06:00
|
|
|
break;
|
|
|
|
case MODE_LIST:
|
2018-08-05 07:17:34 -06:00
|
|
|
ret = display_privs(snl, list_pw ? list_pw : sudo_user.pw, verbose);
|
2015-06-25 11:12:36 -06:00
|
|
|
break;
|
|
|
|
case MODE_VALIDATE:
|
|
|
|
case MODE_RUN:
|
|
|
|
case MODE_EDIT:
|
2020-01-30 13:25:36 -07:00
|
|
|
/* ret may be overridden by "goto bad" later */
|
|
|
|
ret = true;
|
2015-06-25 11:12:36 -06:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Should not happen. */
|
|
|
|
sudo_warnx("internal error, unexpected sudo mode 0x%x", sudo_mode);
|
|
|
|
goto done;
|
|
|
|
}
|
2005-04-12 01:37:08 +00:00
|
|
|
|
2011-03-06 15:52:40 -05:00
|
|
|
if (ISSET(sudo_mode, (MODE_VALIDATE|MODE_CHECK|MODE_LIST))) {
|
2016-09-08 16:38:08 -06:00
|
|
|
/* ret already set appropriately */
|
2010-03-15 18:13:58 -04:00
|
|
|
goto done;
|
2011-03-06 15:52:40 -05:00
|
|
|
}
|
2010-03-15 18:13:58 -04: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)
|
|
|
|
cmnd_umask |= user_umask;
|
2010-03-14 19:58:47 -04:00
|
|
|
}
|
1999-09-08 08:06:28 +00:00
|
|
|
|
2010-03-14 19:58:47 -04:00
|
|
|
if (ISSET(sudo_mode, MODE_LOGIN_SHELL)) {
|
|
|
|
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 */
|
|
|
|
if ((p = strrchr(NewArgv[0], '/')) == NULL)
|
|
|
|
p = NewArgv[0];
|
|
|
|
*p = '-';
|
|
|
|
NewArgv[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
|
|
|
*/
|
|
|
|
if (NewArgc > 1 && strcmp(NewArgv[0], "-bash") == 0 &&
|
2011-07-17 10:37:15 -04:00
|
|
|
strcmp(NewArgv[1], "-c") == 0) {
|
2021-08-09 15:50:26 -06:00
|
|
|
/* We allocated extra space for the --login above. */
|
2021-09-19 13:58:56 -06:00
|
|
|
memmove(&NewArgv[2], &NewArgv[1], sizeof(char *) * NewArgc);
|
2011-07-17 10:37:15 -04:00
|
|
|
NewArgv[1] = "--login";
|
2021-08-09 15:50:26 -06:00
|
|
|
NewArgc++;
|
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. */
|
2017-03-22 13:39:25 -06:00
|
|
|
if (!read_env_file(_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. */
|
|
|
|
if (login_class) {
|
|
|
|
login_cap_t *lc = login_getclass(login_class);
|
|
|
|
if (lc != NULL) {
|
|
|
|
setusercontext(lc, runas_pw, runas_pw->pw_uid, LOGIN_SETPATH|LOGIN_SETENV);
|
|
|
|
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) {
|
2019-02-19 11:03:02 -07:00
|
|
|
if (!read_env_file(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) {
|
2017-03-22 13:39:25 -06:00
|
|
|
if (!read_env_file(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. */
|
2014-04-30 16:57:12 -06:00
|
|
|
if (!insert_env_vars(sudo_user.env_vars))
|
2015-06-25 11:12:36 -06:00
|
|
|
goto done;
|
2004-01-18 22:55:32 +00:00
|
|
|
|
2014-07-12 10:35:00 -06:00
|
|
|
/* Note: must call audit before uid change. */
|
2010-05-13 14:09:21 -04:00
|
|
|
if (ISSET(sudo_mode, MODE_EDIT)) {
|
2020-02-24 19:59:44 -07:00
|
|
|
char **edit_argv;
|
2014-07-12 10:35:00 -06:00
|
|
|
int edit_argc;
|
2017-12-22 10:22:33 -07:00
|
|
|
const char *env_editor;
|
2014-07-12 10:35:00 -06:00
|
|
|
|
2015-06-17 06:49:59 -06:00
|
|
|
free(safe_cmnd);
|
2015-06-18 09:51:36 -06:00
|
|
|
safe_cmnd = find_editor(NewArgc - 1, NewArgv + 1, &edit_argc,
|
2021-09-29 10:25:19 -06:00
|
|
|
&edit_argv, NULL, &env_editor);
|
2015-06-25 11:12:36 -06:00
|
|
|
if (safe_cmnd == NULL) {
|
|
|
|
if (errno != ENOENT)
|
|
|
|
goto done;
|
2020-06-02 09:07:46 -06:00
|
|
|
audit_failure(NewArgv, N_("%s: command not found"),
|
2017-12-22 10:22:33 -07:00
|
|
|
env_editor ? env_editor : def_editor);
|
|
|
|
sudo_warnx(U_("%s: command not found"),
|
|
|
|
env_editor ? env_editor : def_editor);
|
2014-07-12 10:35:00 -06:00
|
|
|
goto bad;
|
2015-06-25 11:12:36 -06:00
|
|
|
}
|
2021-08-10 12:58:18 -06:00
|
|
|
/* find_editor() already g/c'd edit_argv[] */
|
2021-08-09 15:50:26 -06:00
|
|
|
sudoers_gc_remove(GC_PTR, NewArgv);
|
|
|
|
free(NewArgv);
|
2020-02-24 19:59:44 -07:00
|
|
|
NewArgv = edit_argv;
|
|
|
|
NewArgc = 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
|
|
|
|
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;
|
2010-04-20 17:00:31 -04:00
|
|
|
|
2010-03-14 19:58:47 -04:00
|
|
|
done:
|
2022-03-14 13:54:11 -06:00
|
|
|
mail_parse_errors();
|
|
|
|
|
2021-02-12 19:52:11 -07:00
|
|
|
if (def_group_plugin)
|
|
|
|
group_plugin_unload();
|
|
|
|
init_parser(NULL, false, false);
|
|
|
|
|
2020-11-17 13:44:32 -07:00
|
|
|
if (ret == -1) {
|
|
|
|
/* Free stashed copy of the environment. */
|
|
|
|
(void)env_init(NULL);
|
|
|
|
} else {
|
|
|
|
/* Store settings to pass back to front-end. */
|
2020-10-16 05:56:03 -06:00
|
|
|
if (!sudoers_policy_store_result(ret, NewArgv, env_get(), cmnd_umask,
|
2020-02-24 19:59:44 -07:00
|
|
|
iolog_path, closure))
|
|
|
|
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
|
|
|
}
|
|
|
|
|
1999-07-22 12:55:17 +00:00
|
|
|
/*
|
2016-11-02 17:10:17 -06:00
|
|
|
* Initialize timezone and fill in sudo_user struct.
|
1993-02-16 17:07:38 +00:00
|
|
|
*/
|
2014-04-10 16:03:26 -06:00
|
|
|
static bool
|
2010-03-14 19:58:47 -04:00
|
|
|
init_vars(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="))
|
|
|
|
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="))
|
|
|
|
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" */
|
|
|
|
if (user_prompt == NULL)
|
|
|
|
user_prompt = *ep + sizeof("SUDO_PROMPT=") - 1;
|
|
|
|
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
|
|
|
|
1993-10-04 19:10:33 +00:00
|
|
|
/*
|
2016-08-10 10:56:05 -06:00
|
|
|
* Get a local copy of the user's passwd struct and group list if we
|
|
|
|
* don't already have them.
|
1993-10-04 19:10:33 +00:00
|
|
|
*/
|
2012-10-25 16:58:31 -04:00
|
|
|
if (sudo_user.pw == NULL) {
|
|
|
|
if ((sudo_user.pw = sudo_getpwnam(user_name)) == NULL) {
|
|
|
|
/*
|
|
|
|
* It is not unusual for users to place "sudo -k" in a .logout
|
|
|
|
* file which can cause sudo to be run during reboot after the
|
|
|
|
* YP/NIS/NIS+/LDAP/etc daemon has died.
|
|
|
|
*/
|
2014-04-10 16:03:26 -06:00
|
|
|
if (sudo_mode == MODE_KILL || sudo_mode == MODE_INVALIDATE) {
|
2021-08-19 09:44:11 -06:00
|
|
|
sudo_warnx(U_("unknown uid %u"), (unsigned int) user_uid);
|
2014-04-10 16:03:26 -06:00
|
|
|
debug_return_bool(false);
|
|
|
|
}
|
2011-09-25 06:35:40 -04:00
|
|
|
|
2014-05-02 16:40:30 -06:00
|
|
|
/* Need to make a fake struct passwd for the call to log_warningx(). */
|
2013-04-19 14:48:23 -04:00
|
|
|
sudo_user.pw = sudo_mkpwent(user_name, user_uid, user_gid, NULL, NULL);
|
2013-08-07 10:13:04 -06:00
|
|
|
unknown_user = true;
|
2012-10-25 16:58:31 -04:00
|
|
|
}
|
1994-05-29 22:36:44 +00:00
|
|
|
}
|
2016-08-13 16:27:44 -06:00
|
|
|
if (user_gid_list == NULL)
|
2017-11-28 09:48:43 -07:00
|
|
|
user_gid_list = sudo_get_gidlist(sudo_user.pw, ENTRY_TYPE_ANY);
|
2016-08-10 10:56:05 -06:00
|
|
|
|
|
|
|
/* Store initialize permissions so we can restore them later. */
|
2014-04-10 16:11:47 -06:00
|
|
|
if (!set_perms(PERM_INITIAL))
|
|
|
|
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) {
|
2021-08-19 09:44:11 -06:00
|
|
|
log_warningx(SLOG_SEND_MAIL, N_("unknown uid %u"),
|
2014-05-02 20:54:01 -06:00
|
|
|
(unsigned int) user_uid);
|
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.
|
|
|
|
*/
|
|
|
|
if (sudo_user.runas_group != NULL) {
|
|
|
|
if (!set_runasgr(sudo_user.runas_group, false))
|
|
|
|
debug_return_bool(false);
|
|
|
|
if (!set_runaspw(sudo_user.runas_user ?
|
|
|
|
sudo_user.runas_user : user_name, false))
|
|
|
|
debug_return_bool(false);
|
|
|
|
} else {
|
|
|
|
if (!set_runaspw(sudo_user.runas_user ?
|
|
|
|
sudo_user.runas_user : def_runas_default, false))
|
|
|
|
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
|
|
|
/*
|
|
|
|
* Fill in user_cmnd and user_stat variables.
|
|
|
|
* Does not fill in user_base.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
set_cmnd_path(const char *runchroot)
|
|
|
|
{
|
|
|
|
char *path = user_path;
|
|
|
|
int ret;
|
|
|
|
debug_decl(set_cmnd_path, SUDOERS_DEBUG_PLUGIN);
|
|
|
|
|
2021-08-09 15:50:25 -06:00
|
|
|
free(user_cmnd);
|
|
|
|
user_cmnd = NULL;
|
2020-09-09 15:26:44 -06:00
|
|
|
if (def_secure_path && !user_is_exempt())
|
|
|
|
path = def_secure_path;
|
|
|
|
if (!set_perms(PERM_RUNAS))
|
|
|
|
debug_return_int(NOT_FOUND_ERROR);
|
|
|
|
ret = find_path(NewArgv[0], &user_cmnd, user_stat, path,
|
|
|
|
runchroot, def_ignore_dot, NULL);
|
|
|
|
if (!restore_perms())
|
|
|
|
debug_return_int(NOT_FOUND_ERROR);
|
|
|
|
if (ret == NOT_FOUND) {
|
|
|
|
/* Failed as root, try as invoking user. */
|
|
|
|
if (!set_perms(PERM_USER))
|
|
|
|
debug_return_int(false);
|
|
|
|
ret = find_path(NewArgv[0], &user_cmnd, user_stat, path,
|
|
|
|
runchroot, def_ignore_dot, NULL);
|
|
|
|
if (!restore_perms())
|
|
|
|
debug_return_int(NOT_FOUND_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
debug_return_int(ret);
|
|
|
|
}
|
|
|
|
|
2004-10-26 22:22:46 +00:00
|
|
|
/*
|
2004-11-19 21:35:12 +00:00
|
|
|
* Fill in user_cmnd, user_args, user_base and user_stat variables
|
|
|
|
* and apply any command-specific defaults entries.
|
2004-10-26 22:22:46 +00:00
|
|
|
*/
|
|
|
|
static int
|
2011-07-18 16:23:38 -04:00
|
|
|
set_cmnd(void)
|
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
|
|
|
|
2014-04-10 16:03:26 -06:00
|
|
|
/* Allocate user_stat for find_path() and match functions. */
|
2021-08-09 15:50:25 -06:00
|
|
|
free(user_stat);
|
2015-06-17 06:49:59 -06:00
|
|
|
user_stat = calloc(1, sizeof(struct stat));
|
|
|
|
if (user_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. */
|
|
|
|
free(safe_cmnd);
|
|
|
|
safe_cmnd = NULL;
|
|
|
|
|
2021-01-23 08:43:59 -07:00
|
|
|
if (ISSET(sudo_mode, MODE_RUN|MODE_EDIT|MODE_CHECK)) {
|
|
|
|
if (!ISSET(sudo_mode, MODE_EDIT)) {
|
2020-09-09 15:26:44 -06:00
|
|
|
const char *runchroot = user_runchroot;
|
|
|
|
if (runchroot == NULL && def_runchroot != NULL &&
|
|
|
|
strcmp(def_runchroot, "*") != 0)
|
|
|
|
runchroot = def_runchroot;
|
|
|
|
|
|
|
|
ret = set_cmnd_path(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) {
|
2020-06-02 09:07:46 -06:00
|
|
|
audit_failure(NewArgv, N_("command too long"));
|
2020-01-30 13:25:36 -07:00
|
|
|
}
|
2014-05-02 20:54:01 -06:00
|
|
|
log_warning(0, "%s", NewArgv[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
|
|
|
|
|
|
|
/* set user_args */
|
2021-08-09 15:50:25 -06:00
|
|
|
free(user_args);
|
|
|
|
user_args = NULL;
|
2000-12-30 03:29:47 +00:00
|
|
|
if (NewArgc > 1) {
|
2021-01-23 08:43:59 -07:00
|
|
|
if (ISSET(sudo_mode, MODE_SHELL|MODE_LOGIN_SHELL) &&
|
|
|
|
ISSET(sudo_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.
|
|
|
|
*/
|
2021-01-27 21:27:14 -07:00
|
|
|
user_args = strvec_join(NewArgv + 1, ' ', strlcpy_unescape);
|
2011-07-29 10:10:40 -04:00
|
|
|
} else {
|
2021-01-28 09:15:36 -07:00
|
|
|
user_args = strvec_join(NewArgv + 1, ' ', NULL);
|
2000-12-30 03:29:47 +00:00
|
|
|
}
|
2021-01-27 21:27:14 -07:00
|
|
|
if (user_args == NULL)
|
|
|
|
debug_return_int(NOT_FOUND_ERROR);
|
2000-12-30 03:29:47 +00:00
|
|
|
}
|
2004-01-21 22:25:10 +00:00
|
|
|
}
|
2021-02-16 09:32:34 -07:00
|
|
|
if (user_cmnd == NULL) {
|
|
|
|
user_cmnd = strdup(NewArgv[0]);
|
|
|
|
if (user_cmnd == NULL)
|
|
|
|
debug_return_int(NOT_FOUND_ERROR);
|
|
|
|
}
|
2021-02-10 15:14:08 -07:00
|
|
|
user_base = sudo_basename(user_cmnd);
|
2000-12-30 03:29:47 +00:00
|
|
|
|
2019-11-05 15:18:34 -07:00
|
|
|
/* Convert "sudo sudoedit" -> "sudoedit" */
|
|
|
|
if (ISSET(sudo_mode, MODE_RUN) && strcmp(user_base, "sudoedit") == 0) {
|
2021-02-16 09:32:34 -07:00
|
|
|
char *new_cmnd;
|
|
|
|
|
2019-11-05 15:18:34 -07:00
|
|
|
CLR(sudo_mode, MODE_RUN);
|
|
|
|
SET(sudo_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);
|
|
|
|
}
|
|
|
|
free(user_cmnd);
|
|
|
|
user_base = 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. */
|
2022-03-10 13:30:56 -07:00
|
|
|
(void)update_defaults(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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
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 *
|
2020-09-25 15:09:45 -06:00
|
|
|
open_sudoers(const char *file, bool doedit, bool *keepopen)
|
1994-11-08 01:38:57 +00:00
|
|
|
{
|
2012-02-22 13:04:03 -05:00
|
|
|
struct stat sb;
|
2004-09-27 16:01:54 +00:00
|
|
|
FILE *fp = NULL;
|
2019-07-14 19:37:33 -06:00
|
|
|
bool perm_root = false;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(open_sudoers, SUDOERS_DEBUG_PLUGIN);
|
1994-11-10 00:49:53 +00:00
|
|
|
|
2014-04-10 16:11:47 -06:00
|
|
|
if (!set_perms(PERM_SUDOERS))
|
|
|
|
debug_return_ptr(NULL);
|
1996-07-08 02:30:25 +00:00
|
|
|
|
2019-07-14 18:51:50 -06:00
|
|
|
again:
|
2020-09-25 15:09:45 -06:00
|
|
|
switch (sudo_secure_file(file, sudoers_uid, sudoers_gid, &sb)) {
|
2012-02-22 13:04:03 -05:00
|
|
|
case SUDO_PATH_SECURE:
|
2012-05-21 13:59:02 -04:00
|
|
|
/*
|
2014-02-17 10:20:14 -07:00
|
|
|
* If we are expecting sudoers to be group readable by
|
|
|
|
* SUDOERS_GID but it is not, we must open the file as root,
|
|
|
|
* not uid 1.
|
2012-05-21 13:59:02 -04:00
|
|
|
*/
|
2014-02-17 10:20:14 -07:00
|
|
|
if (sudoers_uid == ROOT_UID && ISSET(sudoers_mode, S_IRGRP)) {
|
|
|
|
if (!ISSET(sb.st_mode, S_IRGRP) || sb.st_gid != SUDOERS_GID) {
|
2019-07-14 19:37:33 -06:00
|
|
|
if (!perm_root) {
|
|
|
|
if (!restore_perms() || !set_perms(PERM_ROOT))
|
|
|
|
debug_return_ptr(NULL);
|
|
|
|
}
|
2012-05-21 13:59:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
2020-09-25 15:09:45 -06:00
|
|
|
* Open file and make sure we can read it so we can present
|
2012-05-21 13:59:02 -04:00
|
|
|
* the user with a reasonable error message (unlike the lexer).
|
|
|
|
*/
|
2020-09-25 15:09:45 -06:00
|
|
|
if ((fp = fopen(file, "r")) == NULL) {
|
|
|
|
log_warning(SLOG_SEND_MAIL, N_("unable to open %s"), file);
|
2012-02-22 13:04:03 -05:00
|
|
|
} else {
|
|
|
|
if (sb.st_size != 0 && fgetc(fp) == EOF) {
|
2014-05-02 20:54:01 -06:00
|
|
|
log_warning(SLOG_SEND_MAIL,
|
2020-09-25 15:09:45 -06:00
|
|
|
N_("unable to read %s"), file);
|
2012-02-22 13:04:03 -05:00
|
|
|
fclose(fp);
|
|
|
|
fp = NULL;
|
|
|
|
} else {
|
|
|
|
/* Rewind fp and set close on exec flag. */
|
|
|
|
rewind(fp);
|
|
|
|
(void) fcntl(fileno(fp), F_SETFD, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SUDO_PATH_MISSING:
|
2019-07-14 18:51:50 -06:00
|
|
|
/*
|
|
|
|
* If we tried to stat() sudoers as non-root but got EACCES,
|
|
|
|
* try again as root.
|
|
|
|
*/
|
2019-07-14 19:37:33 -06:00
|
|
|
if (errno == EACCES && geteuid() != ROOT_UID) {
|
2019-07-14 18:51:50 -06:00
|
|
|
int serrno = errno;
|
|
|
|
if (restore_perms()) {
|
|
|
|
if (!set_perms(PERM_ROOT))
|
|
|
|
debug_return_ptr(NULL);
|
2019-07-14 19:37:33 -06:00
|
|
|
perm_root = true;
|
2019-07-14 18:51:50 -06:00
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
errno = serrno;
|
|
|
|
}
|
2020-09-25 15:09:45 -06:00
|
|
|
log_warning(SLOG_SEND_MAIL, N_("unable to stat %s"), file);
|
2012-02-22 13:04:03 -05:00
|
|
|
break;
|
|
|
|
case SUDO_PATH_BAD_TYPE:
|
2014-05-02 20:54:01 -06:00
|
|
|
log_warningx(SLOG_SEND_MAIL,
|
2020-09-25 15:09:45 -06:00
|
|
|
N_("%s is not a regular file"), file);
|
2012-02-22 13:04:03 -05:00
|
|
|
break;
|
|
|
|
case SUDO_PATH_WRONG_OWNER:
|
2014-05-02 20:54:01 -06:00
|
|
|
log_warningx(SLOG_SEND_MAIL,
|
2020-09-25 15:09:45 -06:00
|
|
|
N_("%s is owned by uid %u, should be %u"), file,
|
2014-05-02 20:54:01 -06:00
|
|
|
(unsigned int) sb.st_uid, (unsigned int) sudoers_uid);
|
2012-02-22 13:04:03 -05:00
|
|
|
break;
|
|
|
|
case SUDO_PATH_WORLD_WRITABLE:
|
2020-09-25 15:09:45 -06:00
|
|
|
log_warningx(SLOG_SEND_MAIL, N_("%s is world writable"), file);
|
2012-02-22 13:04:03 -05:00
|
|
|
break;
|
|
|
|
case SUDO_PATH_GROUP_WRITABLE:
|
2014-05-02 20:54:01 -06:00
|
|
|
log_warningx(SLOG_SEND_MAIL,
|
2020-09-25 15:09:45 -06:00
|
|
|
N_("%s is owned by gid %u, should be %u"), file,
|
2014-05-02 20:54:01 -06:00
|
|
|
(unsigned int) sb.st_gid, (unsigned int) sudoers_gid);
|
2012-02-22 13:04:03 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* NOTREACHED */
|
|
|
|
break;
|
1998-01-21 05:03:46 +00:00
|
|
|
}
|
1995-11-16 07:41:35 +00:00
|
|
|
|
2014-04-10 16:11:47 -06:00
|
|
|
if (!restore_perms()) {
|
|
|
|
/* unable to change back to root */
|
2016-05-05 15:01:22 -06:00
|
|
|
if (fp != NULL) {
|
|
|
|
fclose(fp);
|
|
|
|
fp = NULL;
|
|
|
|
}
|
2014-04-10 16:11:47 -06:00
|
|
|
}
|
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
|
2010-04-22 18:09:53 -04:00
|
|
|
set_loginclass(struct passwd *pw)
|
2000-03-07 04:29:46 +00:00
|
|
|
{
|
2014-05-02 20:54:01 -06:00
|
|
|
const 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
|
|
|
|
2000-03-07 04:29:46 +00:00
|
|
|
if (login_class && strcmp(login_class, "-") != 0) {
|
2014-04-10 16:03:26 -06:00
|
|
|
if (user_uid != 0 && pw->pw_uid != 0) {
|
2016-11-02 17:10:17 -06:00
|
|
|
sudo_warnx(U_("only root can use \"-c %s\""), login_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 {
|
|
|
|
login_class = pw->pw_class;
|
|
|
|
if (!login_class || !*login_class)
|
|
|
|
login_class =
|
|
|
|
(pw->pw_uid == 0) ? LOGIN_DEFROOTCLASS : LOGIN_DEFCLASS;
|
|
|
|
}
|
2000-03-07 04:29:46 +00:00
|
|
|
|
2011-12-02 14:29:54 -05:00
|
|
|
/* Make sure specified login class is valid. */
|
2000-03-24 20:13:12 +00:00
|
|
|
lc = login_getclass(login_class);
|
2001-08-23 21:43:38 +00:00
|
|
|
if (!lc || !lc->lc_class || strcmp(lc->lc_class, login_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.
|
|
|
|
*/
|
2021-08-19 09:44:11 -06:00
|
|
|
log_warningx(errflags, N_("unknown login class %s"), login_class);
|
2011-12-02 14:29:54 -05:00
|
|
|
def_use_loginclass = false;
|
2014-04-10 16:03:26 -06:00
|
|
|
if (login_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
|
2010-04-22 18:09:53 -04:00
|
|
|
set_loginclass(struct passwd *pw)
|
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
|
|
|
|
2012-08-15 09:52:26 -04:00
|
|
|
#ifndef AI_FQDN
|
|
|
|
# define AI_FQDN AI_CANONNAME
|
|
|
|
#endif
|
|
|
|
|
2015-06-17 06:49:59 -06:00
|
|
|
/*
|
|
|
|
* Look up the fully qualified domain name of host.
|
2015-08-04 16:15:11 -06:00
|
|
|
* Use AI_FQDN if available since "canonical" is not always the same as fqdn.
|
2021-01-29 06:24:31 -07:00
|
|
|
* Returns 0 on success, setting longp and shortp.
|
|
|
|
* Returns non-zero on failure, longp and shortp are unchanged.
|
|
|
|
* See gai_strerror() for the list of error return codes.
|
2015-06-17 06:49:59 -06:00
|
|
|
*/
|
2018-11-05 09:08:05 -07:00
|
|
|
static int
|
2015-06-17 06:49:59 -06:00
|
|
|
resolve_host(const char *host, char **longp, char **shortp)
|
|
|
|
{
|
|
|
|
struct addrinfo *res0, hint;
|
|
|
|
char *cp, *lname, *sname;
|
2018-11-05 09:08:05 -07:00
|
|
|
int ret;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(resolve_host, SUDOERS_DEBUG_PLUGIN);
|
2015-06-17 06:49:59 -06:00
|
|
|
|
|
|
|
memset(&hint, 0, sizeof(hint));
|
|
|
|
hint.ai_family = PF_UNSPEC;
|
|
|
|
hint.ai_flags = AI_FQDN;
|
|
|
|
|
2018-11-05 09:08:05 -07:00
|
|
|
if ((ret = getaddrinfo(host, NULL, &hint, &res0)) != 0)
|
|
|
|
debug_return_int(ret);
|
2015-06-17 06:49:59 -06:00
|
|
|
if ((lname = strdup(res0->ai_canonname)) == NULL) {
|
|
|
|
freeaddrinfo(res0);
|
2018-11-05 09:08:05 -07:00
|
|
|
debug_return_int(EAI_MEMORY);
|
2015-06-17 06:49:59 -06:00
|
|
|
}
|
|
|
|
if ((cp = strchr(lname, '.')) != NULL) {
|
|
|
|
sname = strndup(lname, (size_t)(cp - lname));
|
|
|
|
if (sname == NULL) {
|
|
|
|
free(lname);
|
|
|
|
freeaddrinfo(res0);
|
2018-11-05 09:08:05 -07:00
|
|
|
debug_return_int(EAI_MEMORY);
|
2015-06-17 06:49:59 -06:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sname = lname;
|
|
|
|
}
|
|
|
|
freeaddrinfo(res0);
|
|
|
|
*longp = lname;
|
|
|
|
*shortp = sname;
|
|
|
|
|
2018-12-24 20:31:26 -07:00
|
|
|
debug_return_int(0);
|
2015-06-17 06:49:59 -06:00
|
|
|
}
|
|
|
|
|
1999-12-02 20:31:25 +00:00
|
|
|
/*
|
2014-11-12 20:33:41 -07:00
|
|
|
* Look up the fully qualified domain name of user_host and user_runhost.
|
|
|
|
* Sets user_host, user_shost, user_runhost and user_srunhost.
|
1999-12-02 20:31:25 +00:00
|
|
|
*/
|
2015-06-17 06:49:59 -06:00
|
|
|
static bool
|
2022-03-10 13:35:58 -07:00
|
|
|
cb_fqdn(const char *file, int line, int column,
|
|
|
|
const union sudo_defs_val *sd_un, int op)
|
1999-12-02 20:31:25 +00:00
|
|
|
{
|
2014-11-12 20:33:41 -07:00
|
|
|
bool remote;
|
2020-01-28 13:00:54 -07:00
|
|
|
int rc;
|
2015-06-17 06:49:59 -06:00
|
|
|
char *lhost, *shost;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(cb_fqdn, SUDOERS_DEBUG_PLUGIN);
|
1999-12-02 20:31:25 +00:00
|
|
|
|
2016-07-19 14:52:33 -06:00
|
|
|
/* Nothing to do if fqdn flag is disabled. */
|
|
|
|
if (sd_un != NULL && !sd_un->flag)
|
|
|
|
debug_return_bool(true);
|
|
|
|
|
2014-11-12 20:33:41 -07:00
|
|
|
/* If the -h flag was given we need to resolve both host and runhost. */
|
|
|
|
remote = strcmp(user_runhost, user_host) != 0;
|
|
|
|
|
2015-06-17 06:49:59 -06:00
|
|
|
/* First resolve user_host, setting user_host and user_shost. */
|
2018-11-05 09:08:05 -07:00
|
|
|
if (resolve_host(user_host, &lhost, &shost) != 0) {
|
2020-01-28 13:00:54 -07:00
|
|
|
if ((rc = resolve_host(user_runhost, &lhost, &shost)) != 0) {
|
2018-11-05 09:08:05 -07:00
|
|
|
gai_log_warning(SLOG_SEND_MAIL|SLOG_RAW_MSG, rc,
|
2015-06-17 06:49:59 -06:00
|
|
|
N_("unable to resolve host %s"), user_host);
|
|
|
|
debug_return_bool(false);
|
|
|
|
}
|
1999-12-02 20:31:25 +00:00
|
|
|
}
|
2015-06-17 06:49:59 -06:00
|
|
|
if (user_shost != user_host)
|
|
|
|
free(user_shost);
|
|
|
|
free(user_host);
|
|
|
|
user_host = lhost;
|
|
|
|
user_shost = shost;
|
|
|
|
|
|
|
|
/* Next resolve user_runhost, setting user_runhost and user_srunhost. */
|
|
|
|
lhost = shost = NULL;
|
2014-11-12 20:33:41 -07:00
|
|
|
if (remote) {
|
2020-01-28 13:00:54 -07:00
|
|
|
if ((rc = resolve_host(user_runhost, &lhost, &shost)) != 0) {
|
|
|
|
gai_log_warning(SLOG_NO_LOG|SLOG_RAW_MSG, rc,
|
|
|
|
N_("unable to resolve host %s"), user_runhost);
|
|
|
|
debug_return_bool(false);
|
2017-08-01 13:45:32 -06:00
|
|
|
}
|
2014-11-12 20:33:41 -07:00
|
|
|
} else {
|
|
|
|
/* Not remote, just use user_host. */
|
2015-06-17 06:49:59 -06:00
|
|
|
if ((lhost = strdup(user_host)) != NULL) {
|
|
|
|
if (user_shost != user_host)
|
2016-09-09 16:26:22 -06:00
|
|
|
shost = strdup(user_shost);
|
2015-06-17 06:49:59 -06:00
|
|
|
else
|
|
|
|
shost = lhost;
|
2014-11-12 20:33:41 -07:00
|
|
|
}
|
2017-08-01 13:45:32 -06:00
|
|
|
if (lhost == NULL || shost == NULL) {
|
|
|
|
free(lhost);
|
|
|
|
if (lhost != shost)
|
|
|
|
free(shost);
|
|
|
|
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
|
|
|
|
debug_return_bool(false);
|
|
|
|
}
|
2014-11-12 20:33:41 -07:00
|
|
|
}
|
2017-08-01 13:45:32 -06:00
|
|
|
if (lhost != NULL && shost != NULL) {
|
|
|
|
if (user_srunhost != user_runhost)
|
|
|
|
free(user_srunhost);
|
|
|
|
free(user_runhost);
|
|
|
|
user_runhost = lhost;
|
|
|
|
user_srunhost = shost;
|
2015-06-17 06:49:59 -06:00
|
|
|
}
|
2014-11-12 20:33:41 -07:00
|
|
|
|
|
|
|
sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
|
|
|
|
"host %s, shost %s, runhost %s, srunhost %s",
|
|
|
|
user_host, user_shost, user_runhost, user_srunhost);
|
2015-06-17 06:49:59 -06:00
|
|
|
debug_return_bool(true);
|
1999-12-02 20:31:25 +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
|
|
|
|
* and store it in 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
|
|
|
|
set_runaspw(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;
|
2015-04-16 13:19:04 -06:00
|
|
|
pw = sudo_fakepwnam(user, 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)
|
2021-08-19 09:44:11 -06:00
|
|
|
log_warningx(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
|
|
|
}
|
2013-12-05 15:51:56 -07:00
|
|
|
if (runas_pw != NULL)
|
|
|
|
sudo_pw_delref(runas_pw);
|
|
|
|
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
|
|
|
|
* and store it in runas_gr.
|
2007-11-21 20:12:00 +00:00
|
|
|
*/
|
2014-04-10 16:03:26 -06:00
|
|
|
static bool
|
|
|
|
set_runasgr(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)
|
2021-08-19 09:44:11 -06:00
|
|
|
log_warningx(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
|
|
|
}
|
2013-12-05 15:51:56 -07:00
|
|
|
if (runas_gr != NULL)
|
|
|
|
sudo_gr_delref(runas_gr);
|
|
|
|
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.
|
|
|
|
*/
|
2014-04-04 15:30:12 -06:00
|
|
|
static bool
|
2022-03-10 13:35:58 -07:00
|
|
|
cb_runas_default(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. */
|
2020-12-05 09:05:42 -07:00
|
|
|
if (sudo_user.runas_user == NULL && sudo_user.runas_group == NULL)
|
2016-07-18 12:20:46 -06:00
|
|
|
debug_return_bool(set_runaspw(sd_un->str, true));
|
|
|
|
debug_return_bool(true);
|
2007-11-21 20:12:00 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 16:14:54 -06:00
|
|
|
/*
|
2019-10-18 06:20:27 -06:00
|
|
|
* Callback for tty_tickets sudoers setting.
|
2017-08-01 16:14:54 -06:00
|
|
|
*/
|
|
|
|
static bool
|
2022-03-10 13:35:58 -07:00
|
|
|
cb_tty_tickets(const char *file, int line, int column,
|
|
|
|
const union sudo_defs_val *sd_un, int op)
|
2017-08-01 16:14:54 -06:00
|
|
|
{
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(cb_tty_tickets, SUDOERS_DEBUG_PLUGIN);
|
2017-08-01 16:14:54 -06:00
|
|
|
|
|
|
|
/* Convert tty_tickets -> timestamp_type */
|
|
|
|
if (sd_un->flag)
|
|
|
|
def_timestamp_type = tty;
|
|
|
|
else
|
|
|
|
def_timestamp_type = global;
|
|
|
|
debug_return_bool(true);
|
|
|
|
}
|
|
|
|
|
2019-10-18 06:20:27 -06:00
|
|
|
/*
|
|
|
|
* Callback for umask sudoers setting.
|
|
|
|
*/
|
|
|
|
static bool
|
2022-03-10 13:35:58 -07:00
|
|
|
cb_umask(const char *file, int line, int column,
|
|
|
|
const union sudo_defs_val *sd_un, int op)
|
2019-10-18 06:20:27 -06:00
|
|
|
{
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(cb_umask, SUDOERS_DEBUG_PLUGIN);
|
2019-10-18 06:20:27 -06:00
|
|
|
|
|
|
|
/* Force umask if explicitly set in sudoers. */
|
|
|
|
force_umask = sd_un->mode != ACCESSPERMS;
|
|
|
|
|
|
|
|
debug_return_bool(true);
|
|
|
|
}
|
|
|
|
|
2020-09-09 19:18:24 -06:00
|
|
|
/*
|
|
|
|
* Callback for runchroot sudoers setting.
|
|
|
|
*/
|
|
|
|
static bool
|
2022-03-10 13:35:58 -07:00
|
|
|
cb_runchroot(const char *file, int line, int column,
|
|
|
|
const union sudo_defs_val *sd_un, int op)
|
2020-09-09 19:18:24 -06:00
|
|
|
{
|
|
|
|
debug_decl(cb_runchroot, SUDOERS_DEBUG_PLUGIN);
|
|
|
|
|
|
|
|
sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
|
|
|
|
"def_runchroot now %s", sd_un->str);
|
|
|
|
if (user_cmnd != NULL) {
|
|
|
|
/* Update user_cmnd based on the new chroot. */
|
|
|
|
cmnd_status = set_cmnd_path(sd_un->str);
|
|
|
|
sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
|
|
|
|
"user_cmnd now %s", user_cmnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
debug_return_bool(true);
|
|
|
|
}
|
|
|
|
|
2020-10-26 16:10:42 -06:00
|
|
|
static bool
|
2022-03-10 13:35:58 -07:00
|
|
|
cb_logfile(const char *file, int line, int column,
|
|
|
|
const union sudo_defs_val *sd_un, int op)
|
2020-10-26 16:10:42 -06:00
|
|
|
{
|
|
|
|
int logtype = def_syslog ? EVLOG_SYSLOG : EVLOG_NONE;
|
|
|
|
debug_decl(cb_logfile, SUDOERS_DEBUG_PLUGIN);
|
|
|
|
|
|
|
|
if (sd_un->str != NULL)
|
|
|
|
SET(logtype, EVLOG_FILE);
|
|
|
|
eventlog_set_type(logtype);
|
|
|
|
eventlog_set_logpath(sd_un->str);
|
|
|
|
|
|
|
|
debug_return_bool(true);
|
|
|
|
}
|
|
|
|
|
2020-10-27 15:26:02 -06:00
|
|
|
static bool
|
2022-03-10 13:35:58 -07:00
|
|
|
cb_log_format(const char *file, int line, int column,
|
|
|
|
const union sudo_defs_val *sd_un, int op)
|
2020-10-27 15:26:02 -06:00
|
|
|
{
|
|
|
|
debug_decl(cb_log_format, SUDOERS_DEBUG_PLUGIN);
|
|
|
|
|
|
|
|
eventlog_set_format(sd_un->tuple == sudo ? EVLOG_SUDO : EVLOG_JSON);
|
|
|
|
|
|
|
|
debug_return_bool(true);
|
|
|
|
}
|
|
|
|
|
2020-10-26 16:10:42 -06:00
|
|
|
static bool
|
2022-03-10 13:35:58 -07:00
|
|
|
cb_syslog(const char *file, int line, int column,
|
|
|
|
const union sudo_defs_val *sd_un, int op)
|
2020-10-26 16:10:42 -06:00
|
|
|
{
|
|
|
|
int logtype = def_logfile ? EVLOG_FILE : EVLOG_NONE;
|
|
|
|
debug_decl(cb_syslog, SUDOERS_DEBUG_PLUGIN);
|
|
|
|
|
|
|
|
if (sd_un->str != NULL)
|
|
|
|
SET(logtype, EVLOG_SYSLOG);
|
|
|
|
eventlog_set_type(logtype);
|
|
|
|
|
|
|
|
debug_return_bool(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2022-03-10 13:35:58 -07:00
|
|
|
cb_syslog_goodpri(const char *file, int line, int column,
|
|
|
|
const union sudo_defs_val *sd_un, int op)
|
2020-10-26 16:10:42 -06:00
|
|
|
{
|
|
|
|
debug_decl(cb_syslog_goodpri, SUDOERS_DEBUG_PLUGIN);
|
|
|
|
|
|
|
|
eventlog_set_syslog_acceptpri(sd_un->ival);
|
|
|
|
|
|
|
|
debug_return_bool(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2022-03-10 13:35:58 -07:00
|
|
|
cb_syslog_badpri(const char *file, int line, int column,
|
|
|
|
const union sudo_defs_val *sd_un, int op)
|
2020-10-26 16:10:42 -06:00
|
|
|
{
|
|
|
|
debug_decl(cb_syslog_badpri, SUDOERS_DEBUG_PLUGIN);
|
|
|
|
|
|
|
|
eventlog_set_syslog_rejectpri(sd_un->ival);
|
|
|
|
eventlog_set_syslog_alertpri(sd_un->ival);
|
|
|
|
|
|
|
|
debug_return_bool(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2022-03-10 13:35:58 -07:00
|
|
|
cb_syslog_maxlen(const char *file, int line, int column,
|
|
|
|
const union sudo_defs_val *sd_un, int op)
|
2020-10-26 16:10:42 -06:00
|
|
|
{
|
|
|
|
debug_decl(cb_syslog_maxlen, SUDOERS_DEBUG_PLUGIN);
|
|
|
|
|
|
|
|
eventlog_set_syslog_maxlen(sd_un->ival);
|
|
|
|
|
|
|
|
debug_return_bool(true);
|
|
|
|
}
|
|
|
|
|
2020-10-26 16:16:46 -06:00
|
|
|
static bool
|
2022-03-10 13:35:58 -07:00
|
|
|
cb_loglinelen(const char *file, int line, int column,
|
|
|
|
const union sudo_defs_val *sd_un, int op)
|
2020-10-26 16:16:46 -06:00
|
|
|
{
|
|
|
|
debug_decl(cb_loglinelen, SUDOERS_DEBUG_PLUGIN);
|
|
|
|
|
|
|
|
eventlog_set_file_maxlen(sd_un->ival);
|
|
|
|
|
|
|
|
debug_return_bool(true);
|
|
|
|
}
|
|
|
|
|
2020-10-26 16:10:42 -06:00
|
|
|
static bool
|
2022-03-10 13:35:58 -07:00
|
|
|
cb_log_year(const char *file, int line, int column,
|
|
|
|
const union sudo_defs_val *sd_un, int op)
|
2020-10-26 16:10:42 -06:00
|
|
|
{
|
|
|
|
debug_decl(cb_syslog_maxlen, SUDOERS_DEBUG_PLUGIN);
|
|
|
|
|
|
|
|
eventlog_set_time_fmt(sd_un->flag ? "%h %e %T %Y" : "%h %e %T");
|
|
|
|
|
|
|
|
debug_return_bool(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2022-03-10 13:35:58 -07:00
|
|
|
cb_log_host(const char *file, int line, int column,
|
|
|
|
const union sudo_defs_val *sd_un, int op)
|
2020-10-26 16:10:42 -06:00
|
|
|
{
|
|
|
|
debug_decl(cb_syslog_maxlen, SUDOERS_DEBUG_PLUGIN);
|
|
|
|
|
|
|
|
eventlog_set_omit_hostname(!sd_un->flag);
|
|
|
|
|
|
|
|
debug_return_bool(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2022-03-10 13:35:58 -07:00
|
|
|
cb_mailerpath(const char *file, int line, int column,
|
|
|
|
const union sudo_defs_val *sd_un, int op)
|
2020-10-26 16:10:42 -06:00
|
|
|
{
|
|
|
|
debug_decl(cb_mailerpath, SUDOERS_DEBUG_PLUGIN);
|
|
|
|
|
|
|
|
eventlog_set_mailerpath(sd_un->str);
|
|
|
|
|
|
|
|
debug_return_bool(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2022-03-10 13:35:58 -07:00
|
|
|
cb_mailerflags(const char *file, int line, int column,
|
|
|
|
const union sudo_defs_val *sd_un, int op)
|
2020-10-26 16:10:42 -06:00
|
|
|
{
|
|
|
|
debug_decl(cb_mailerflags, SUDOERS_DEBUG_PLUGIN);
|
|
|
|
|
|
|
|
eventlog_set_mailerflags(sd_un->str);
|
|
|
|
|
|
|
|
debug_return_bool(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2022-03-10 13:35:58 -07:00
|
|
|
cb_mailfrom(const char *file, int line, int column,
|
|
|
|
const union sudo_defs_val *sd_un, int op)
|
2020-10-26 16:10:42 -06:00
|
|
|
{
|
|
|
|
debug_decl(cb_mailfrom, SUDOERS_DEBUG_PLUGIN);
|
|
|
|
|
|
|
|
eventlog_set_mailfrom(sd_un->str);
|
|
|
|
|
|
|
|
debug_return_bool(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2022-03-10 13:35:58 -07:00
|
|
|
cb_mailto(const char *file, int line, int column,
|
|
|
|
const union sudo_defs_val *sd_un, int op)
|
2020-10-26 16:10:42 -06:00
|
|
|
{
|
|
|
|
debug_decl(cb_mailto, SUDOERS_DEBUG_PLUGIN);
|
|
|
|
|
|
|
|
eventlog_set_mailto(sd_un->str);
|
|
|
|
|
|
|
|
debug_return_bool(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2022-03-10 13:35:58 -07:00
|
|
|
cb_mailsub(const char *file, int line, int column,
|
|
|
|
const union sudo_defs_val *sd_un, int op)
|
2020-10-26 16:10:42 -06:00
|
|
|
{
|
|
|
|
debug_decl(cb_mailsub, SUDOERS_DEBUG_PLUGIN);
|
|
|
|
|
|
|
|
eventlog_set_mailsub(sd_un->str);
|
|
|
|
|
|
|
|
debug_return_bool(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set parse Defaults callbacks.
|
|
|
|
* We do this here instead in def_data.in so we don't have to
|
|
|
|
* stub out the callbacks for visudo and testsudoers.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
set_callbacks(void)
|
|
|
|
{
|
|
|
|
debug_decl(set_callbacks, SUDOERS_DEBUG_PLUGIN);
|
|
|
|
|
|
|
|
/* Set fqdn callback. */
|
|
|
|
sudo_defs_table[I_FQDN].callback = cb_fqdn;
|
|
|
|
|
|
|
|
/* Set group_plugin callback. */
|
|
|
|
sudo_defs_table[I_GROUP_PLUGIN].callback = cb_group_plugin;
|
|
|
|
|
|
|
|
/* Set runas callback. */
|
|
|
|
sudo_defs_table[I_RUNAS_DEFAULT].callback = cb_runas_default;
|
|
|
|
|
|
|
|
/* Set locale callback. */
|
|
|
|
sudo_defs_table[I_SUDOERS_LOCALE].callback = sudoers_locale_callback;
|
|
|
|
|
|
|
|
/* Set maxseq callback. */
|
|
|
|
sudo_defs_table[I_MAXSEQ].callback = cb_maxseq;
|
|
|
|
|
|
|
|
/* Set iolog_user callback. */
|
|
|
|
sudo_defs_table[I_IOLOG_USER].callback = cb_iolog_user;
|
|
|
|
|
|
|
|
/* Set iolog_group callback. */
|
|
|
|
sudo_defs_table[I_IOLOG_GROUP].callback = cb_iolog_group;
|
|
|
|
|
|
|
|
/* Set iolog_mode callback. */
|
|
|
|
sudo_defs_table[I_IOLOG_MODE].callback = cb_iolog_mode;
|
|
|
|
|
|
|
|
/* Set tty_tickets callback. */
|
|
|
|
sudo_defs_table[I_TTY_TICKETS].callback = cb_tty_tickets;
|
|
|
|
|
|
|
|
/* Set umask callback. */
|
|
|
|
sudo_defs_table[I_UMASK].callback = cb_umask;
|
|
|
|
|
|
|
|
/* Set runchroot callback. */
|
|
|
|
sudo_defs_table[I_RUNCHROOT].callback = cb_runchroot;
|
|
|
|
|
|
|
|
/* eventlog callbacks */
|
|
|
|
sudo_defs_table[I_SYSLOG].callback = cb_syslog;
|
|
|
|
sudo_defs_table[I_SYSLOG_GOODPRI].callback = cb_syslog_goodpri;
|
|
|
|
sudo_defs_table[I_SYSLOG_BADPRI].callback = cb_syslog_badpri;
|
|
|
|
sudo_defs_table[I_SYSLOG_MAXLEN].callback = cb_syslog_maxlen;
|
2020-10-26 16:16:46 -06:00
|
|
|
sudo_defs_table[I_LOGLINELEN].callback = cb_loglinelen;
|
2020-10-26 16:10:42 -06:00
|
|
|
sudo_defs_table[I_LOG_HOST].callback = cb_log_host;
|
|
|
|
sudo_defs_table[I_LOGFILE].callback = cb_logfile;
|
2020-10-27 15:26:02 -06:00
|
|
|
sudo_defs_table[I_LOG_FORMAT].callback = cb_log_format;
|
2020-10-26 16:10:42 -06:00
|
|
|
sudo_defs_table[I_LOG_YEAR].callback = cb_log_year;
|
|
|
|
sudo_defs_table[I_MAILERPATH].callback = cb_mailerpath;
|
|
|
|
sudo_defs_table[I_MAILERFLAGS].callback = cb_mailerflags;
|
|
|
|
sudo_defs_table[I_MAILFROM].callback = cb_mailfrom;
|
|
|
|
sudo_defs_table[I_MAILTO].callback = cb_mailto;
|
|
|
|
sudo_defs_table[I_MAILSUB].callback = cb_mailsub;
|
2022-01-28 08:52:41 -07:00
|
|
|
sudo_defs_table[I_PASSPROMPT_REGEX].callback = cb_passprompt_regex;
|
2020-10-26 16:10:42 -06:00
|
|
|
|
|
|
|
debug_return;
|
|
|
|
}
|
|
|
|
|
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) {
|
2012-11-25 09:34:40 -05:00
|
|
|
nss->close(nss);
|
2013-10-22 09:00:37 -06:00
|
|
|
}
|
2021-02-12 15:36:18 -07:00
|
|
|
snl = NULL;
|
2021-02-16 10:37:04 -07:00
|
|
|
init_parser(NULL, false, false);
|
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();
|
2021-02-15 08:29:47 -07:00
|
|
|
sudo_user_free();
|
2016-05-11 07:06:45 -06:00
|
|
|
sudo_freepwcache();
|
|
|
|
sudo_freegrcache();
|
2012-11-25 09:34:40 -05:00
|
|
|
|
2021-08-09 15:50:26 -06:00
|
|
|
/* Clear globals */
|
|
|
|
list_pw = NULL;
|
|
|
|
NewArgv = NULL;
|
|
|
|
NewArgc = 0;
|
|
|
|
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
|
|
|
|
tty_present(void)
|
|
|
|
{
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(tty_present, SUDOERS_DEBUG_PLUGIN);
|
2019-06-19 20:32:22 -06:00
|
|
|
|
|
|
|
if (user_ttypath == NULL) {
|
|
|
|
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
|
|
|
}
|
2021-02-15 08:29:47 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Free memory allocated for struct sudo_user.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sudo_user_free(void)
|
|
|
|
{
|
|
|
|
debug_decl(sudo_user_free, SUDOERS_DEBUG_PLUGIN);
|
|
|
|
|
|
|
|
/* Free remaining references to password and group entries. */
|
|
|
|
if (sudo_user.pw != NULL)
|
|
|
|
sudo_pw_delref(sudo_user.pw);
|
|
|
|
if (runas_pw != NULL)
|
|
|
|
sudo_pw_delref(runas_pw);
|
|
|
|
if (runas_gr != NULL)
|
|
|
|
sudo_gr_delref(runas_gr);
|
|
|
|
if (user_gid_list != NULL)
|
|
|
|
sudo_gidlist_delref(user_gid_list);
|
|
|
|
|
|
|
|
/* Free dynamic contents of sudo_user. */
|
|
|
|
free(user_cwd);
|
|
|
|
free(user_name);
|
|
|
|
free(user_gids);
|
|
|
|
if (user_ttypath != NULL)
|
|
|
|
free(user_ttypath);
|
|
|
|
else
|
|
|
|
free(user_tty);
|
|
|
|
if (user_shost != user_host)
|
|
|
|
free(user_shost);
|
|
|
|
free(user_host);
|
|
|
|
if (user_srunhost != user_runhost)
|
|
|
|
free(user_srunhost);
|
|
|
|
free(user_runhost);
|
|
|
|
free(user_cmnd);
|
|
|
|
free(user_args);
|
|
|
|
free(safe_cmnd);
|
|
|
|
free(user_stat);
|
|
|
|
#ifdef HAVE_SELINUX
|
|
|
|
free(user_role);
|
|
|
|
free(user_type);
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_PRIV_SET
|
2021-02-18 05:41:20 -07:00
|
|
|
free(runas_privs);
|
|
|
|
free(runas_limitprivs);
|
2021-02-15 08:29:47 -07:00
|
|
|
#endif
|
|
|
|
memset(&sudo_user, 0, sizeof(sudo_user));
|
|
|
|
|
|
|
|
debug_return;
|
|
|
|
}
|