1993-11-27 23:42:49 +00:00
|
|
|
/*
|
2018-05-14 09:05:03 -06:00
|
|
|
* Copyright (c) 1993-1996, 1998-2018 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>
|
1994-03-12 18:36:53 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
2001-12-14 19:52:54 +00:00
|
|
|
# include <string.h>
|
1994-03-12 18:36:53 +00:00
|
|
|
#endif /* HAVE_STRING_H */
|
2010-06-29 13:08:05 -04:00
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif /* HAVE_STRINGS_H */
|
2015-07-02 09:08:28 -06:00
|
|
|
#include <unistd.h>
|
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-22 12:55:17 +00:00
|
|
|
#include <signal.h>
|
1999-07-05 20:11:50 +00:00
|
|
|
#include <grp.h>
|
2010-08-10 13:44:05 -04:00
|
|
|
#include <time.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>
|
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"
|
2010-03-14 19:58:47 -04:00
|
|
|
#include "auth/sudo_auth.h"
|
1994-03-12 18:36:53 +00:00
|
|
|
|
2014-06-26 15:51:15 -06:00
|
|
|
#ifndef HAVE_GETADDRINFO
|
|
|
|
# include "compat/getaddrinfo.h"
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
*/
|
2016-07-18 12:19:07 -06:00
|
|
|
static bool cb_fqdn(const union sudo_defs_val *);
|
2016-07-18 12:11:25 -06:00
|
|
|
static bool cb_runas_default(const union sudo_defs_val *);
|
2017-08-01 16:14:54 -06:00
|
|
|
static bool cb_tty_tickets(const union sudo_defs_val *);
|
2011-07-18 16:23:38 -04:00
|
|
|
static int set_cmnd(void);
|
2015-06-25 11:12:36 -06:00
|
|
|
static int create_admin_success_flag(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);
|
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;
|
2000-10-26 16:42:40 +00:00
|
|
|
#ifdef HAVE_BSD_AUTH_H
|
|
|
|
char *login_style;
|
|
|
|
#endif /* HAVE_BSD_AUTH_H */
|
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-11-21 20:12:00 +00:00
|
|
|
static char *runas_user;
|
|
|
|
static char *runas_group;
|
2007-12-31 12:39:52 +00:00
|
|
|
static struct sudo_nss_list *snl;
|
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
|
|
|
|
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;
|
2016-01-22 11:22:58 -07:00
|
|
|
debug_decl(unlimit_nproc, SUDOERS_DEBUG_UTIL)
|
2015-06-12 15:30:06 -06:00
|
|
|
|
|
|
|
if (getrlimit(RLIMIT_NPROC, &nproclimit) != 0)
|
|
|
|
sudo_warn("getrlimit");
|
|
|
|
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)
|
|
|
|
sudo_warn("setrlimit");
|
|
|
|
}
|
|
|
|
debug_return;
|
|
|
|
#endif /* __linux__ */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Restore saved value of RLIMIT_NPROC.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
restore_nproc(void)
|
|
|
|
{
|
|
|
|
#ifdef __linux__
|
2016-01-22 11:22:58 -07:00
|
|
|
debug_decl(restore_nproc, SUDOERS_DEBUG_UTIL)
|
2015-06-12 15:30:06 -06:00
|
|
|
|
|
|
|
if (setrlimit(RLIMIT_NPROC, &nproclimit) != 0)
|
|
|
|
sudo_warn("setrlimit");
|
|
|
|
|
|
|
|
debug_return;
|
|
|
|
#endif /* __linux__ */
|
|
|
|
}
|
|
|
|
|
2012-10-25 16:58:31 -04:00
|
|
|
int
|
|
|
|
sudoers_policy_init(void *info, 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;
|
2016-09-08 16:38:08 -06:00
|
|
|
int ret = -1;
|
2015-02-01 08:24:49 -07:00
|
|
|
debug_decl(sudoers_policy_init, SUDOERS_DEBUG_PLUGIN)
|
2010-03-14 19:58:47 -04:00
|
|
|
|
2011-05-17 16:38:40 -04:00
|
|
|
bindtextdomain("sudoers", LOCALEDIR);
|
|
|
|
|
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()) {
|
|
|
|
sudo_warnx(U_("unable to initialize sudoers default values"));
|
|
|
|
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. */
|
|
|
|
sudo_mode = sudoers_policy_deserialize_info(info, &runas_user, &runas_group);
|
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
|
|
|
|
2016-06-30 12:40:19 -06:00
|
|
|
/*
|
|
|
|
* Open and parse sudoers, set global defaults.
|
|
|
|
* Uses the C locale unless another is specified in sudoers.
|
|
|
|
*/
|
|
|
|
sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale);
|
2016-07-20 15:52:32 -06:00
|
|
|
sudo_warn_set_locale_func(sudoers_warn_setlocale);
|
2018-05-14 09:05:03 -06:00
|
|
|
init_parser(sudoers_file, 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++;
|
2018-08-02 14:06:36 -06:00
|
|
|
if (nss->getdefs(nss) == -1 || !update_defaults(nss->parse_tree, NULL,
|
2018-07-26 15:12:33 -06:00
|
|
|
SETDEF_GENERIC|SETDEF_HOST|SETDEF_USER|SETDEF_RUNAS, false)) {
|
|
|
|
log_warningx(SLOG_SEND_MAIL|SLOG_NO_STDERR,
|
|
|
|
N_("problem with defaults entries"));
|
|
|
|
}
|
2004-10-26 22:22:46 +00:00
|
|
|
}
|
2010-03-14 19:58:47 -04:00
|
|
|
if (sources == 0) {
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_warnx(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:
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2010-05-13 14:09:21 -04:00
|
|
|
char **edit_argv = NULL;
|
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;
|
2010-03-14 19:58:47 -04:00
|
|
|
struct sudo_nss *nss;
|
2012-11-08 15:37:43 -05:00
|
|
|
int cmnd_status = -1, oldlocale, validated;
|
2016-09-08 16:38:08 -06:00
|
|
|
int ret = -1;
|
2015-02-01 08:24:49 -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);
|
|
|
|
|
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) {
|
2014-03-25 16:46:00 -06:00
|
|
|
/* Not an audit event. */
|
2018-07-26 15:12:33 -06:00
|
|
|
sudo_warnx(U_("sudoers specifies that root is not allowed to sudo"));
|
|
|
|
goto bad;
|
2010-03-14 19:58:47 -04: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
|
|
|
/*
|
|
|
|
* Make a local copy of argc/argv, with special handling
|
2010-09-07 17:59:10 -04:00
|
|
|
* for pseudo-commands and the '-i' option.
|
2010-03-21 08:54:06 -04:00
|
|
|
*/
|
2010-09-07 17:59:10 -04:00
|
|
|
if (argc == 0) {
|
|
|
|
NewArgc = 1;
|
2015-06-17 06:49:59 -06:00
|
|
|
NewArgv = reallocarray(NULL, NewArgc + 1, sizeof(char *));
|
|
|
|
if (NewArgv == 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;
|
|
|
|
}
|
2010-09-07 17:59:10 -04:00
|
|
|
NewArgv[0] = user_cmnd;
|
|
|
|
NewArgv[1] = NULL;
|
|
|
|
} else {
|
2011-07-17 10:37:15 -04:00
|
|
|
/* Must leave an extra slot before NewArgv for bash's --login */
|
2010-09-07 17:59:10 -04:00
|
|
|
NewArgc = argc;
|
2015-06-17 06:49:59 -06:00
|
|
|
NewArgv = reallocarray(NULL, NewArgc + 2, sizeof(char *));
|
|
|
|
if (NewArgv == 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;
|
|
|
|
}
|
2018-10-16 12:50:43 -06:00
|
|
|
NewArgv++; /* reserve an extra slot for --login */
|
|
|
|
memcpy(NewArgv, argv, argc * sizeof(char *));
|
2010-09-07 17:59:10 -04:00
|
|
|
NewArgv[NewArgc] = NULL;
|
2015-06-17 06:49:59 -06:00
|
|
|
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
|
|
|
free(NewArgv);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
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) {
|
2014-03-25 16:46:00 -06:00
|
|
|
/* XXX - audit? */
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_warnx(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);
|
2018-05-14 09:05:03 -06:00
|
|
|
validated = sudoers_lookup(snl, sudo_user.pw, FLAG_NO_USER | FLAG_NO_HOST,
|
|
|
|
pwflag);
|
|
|
|
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
|
|
|
|
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;
|
2014-06-27 14:53:54 -06:00
|
|
|
uid_t uid = sudo_strtoid(def_timestampowner + 1, NULL, NULL, &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 {
|
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()) {
|
2014-05-13 09:41:53 -06:00
|
|
|
audit_failure(NewArgc, NewArgv, N_("no tty"));
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_warnx(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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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 */
|
2004-10-27 16:16:23 +00:00
|
|
|
if (sudo_mode & (MODE_RUN | MODE_EDIT) && prev_user != NULL) {
|
|
|
|
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) {
|
2014-05-13 09:41:53 -06:00
|
|
|
audit_failure(NewArgc, 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)) {
|
2014-05-13 09:41:53 -06:00
|
|
|
audit_failure(NewArgc, NewArgv, N_("%s: command not found"),
|
|
|
|
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 {
|
2014-05-13 09:41:53 -06:00
|
|
|
audit_failure(NewArgc, NewArgv, N_("%s: command not found"),
|
|
|
|
user_cmnd);
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_warnx(U_("%s: command not found"), user_cmnd);
|
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) {
|
|
|
|
/* XXX - audit/log? */
|
|
|
|
sudo_warnx(U_("sorry, you are not allowed set a command timeout"));
|
|
|
|
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)) {
|
2017-02-16 09:58:18 -07:00
|
|
|
/* XXX - audit/log? */
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_warnx(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
|
|
|
|
2012-10-25 13:16:58 -04:00
|
|
|
if (ISSET(sudo_mode, (MODE_RUN | MODE_EDIT))) {
|
|
|
|
if ((def_log_input || def_log_output) && def_iolog_file && def_iolog_dir) {
|
2013-03-28 14:25:24 -04:00
|
|
|
const char prefix[] = "iolog_path=";
|
|
|
|
iolog_path = expand_iolog_path(prefix, def_iolog_dir,
|
2012-10-25 13:16:58 -04:00
|
|
|
def_iolog_file, &sudo_user.iolog_file);
|
2017-01-13 15:45:59 -07:00
|
|
|
if (iolog_path == NULL) {
|
|
|
|
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;
|
|
|
|
} else {
|
|
|
|
sudo_user.iolog_file++;
|
|
|
|
}
|
2010-12-27 12:18:32 -05:00
|
|
|
}
|
2010-12-20 16:28:20 -05:00
|
|
|
}
|
|
|
|
|
2016-08-17 07:22:51 -06:00
|
|
|
if (!log_allowed(validated) && !def_ignore_logfile_errors)
|
2015-05-27 10:55:27 -06:00
|
|
|
goto bad;
|
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:
|
|
|
|
/* Nothing to do. */
|
2016-09-08 16:38:08 -06:00
|
|
|
ret = true;
|
2015-06-25 11:12:36 -06:00
|
|
|
break;
|
|
|
|
case MODE_RUN:
|
|
|
|
case MODE_EDIT:
|
2016-09-08 16:38:08 -06:00
|
|
|
/* ret set by sudoers_policy_exec_setup() below. */
|
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
|
|
|
|
2010-03-14 19:58:47 -04:00
|
|
|
/* Cleanup sudoers sources */
|
2013-10-22 09:00:37 -06:00
|
|
|
TAILQ_FOREACH(nss, snl, entries) {
|
2010-03-14 19:58:47 -04:00
|
|
|
nss->close(nss);
|
|
|
|
}
|
2010-07-02 11:11:25 -04:00
|
|
|
if (def_group_plugin)
|
|
|
|
group_plugin_unload();
|
2018-05-14 09:05:03 -06:00
|
|
|
init_parser(NULL, false);
|
2010-06-25 10:05:51 -04: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) {
|
|
|
|
/* Use the extra slot before NewArgv so we can store --login. */
|
|
|
|
NewArgv--;
|
|
|
|
NewArgc++;
|
|
|
|
NewArgv[0] = NewArgv[1];
|
|
|
|
NewArgv[1] = "--login";
|
|
|
|
}
|
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) {
|
|
|
|
if (!read_env_file(def_env_file, false, true))
|
|
|
|
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)) {
|
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,
|
2017-12-22 10:22:33 -07:00
|
|
|
&edit_argv, NULL, &env_editor, false);
|
2015-06-25 11:12:36 -06:00
|
|
|
if (safe_cmnd == NULL) {
|
|
|
|
if (errno != ENOENT)
|
|
|
|
goto done;
|
2017-12-22 10:22:33 -07:00
|
|
|
audit_failure(NewArgc, NewArgv, N_("%s: command not found"),
|
|
|
|
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
|
|
|
}
|
2016-08-17 07:22:51 -06:00
|
|
|
if (audit_success(edit_argc, edit_argv) != 0 && !def_ignore_audit_errors)
|
2015-06-25 11:12:36 -06:00
|
|
|
goto done;
|
2015-03-16 20:19:24 -06:00
|
|
|
|
|
|
|
/* We want to run the editor with the unmodified environment. */
|
|
|
|
env_swap_old();
|
2014-07-12 10:35:00 -06:00
|
|
|
} else {
|
2016-08-17 07:22:51 -06:00
|
|
|
if (audit_success(NewArgc, NewArgv) != 0 && !def_ignore_audit_errors)
|
2015-06-25 11:12:36 -06:00
|
|
|
goto done;
|
2010-05-13 14:09:21 -04:00
|
|
|
}
|
2004-01-22 02:57:01 +00:00
|
|
|
|
2012-10-25 16:58:31 -04:00
|
|
|
/* Setup execution environment to pass back to front-end. */
|
2016-09-08 16:38:08 -06:00
|
|
|
ret = sudoers_policy_exec_setup(edit_argv ? edit_argv : NewArgv,
|
2012-10-25 16:58:31 -04:00
|
|
|
env_get(), cmnd_umask, iolog_path, closure);
|
2012-03-07 16:35:42 -05:00
|
|
|
|
2012-10-25 16:58:31 -04:00
|
|
|
/* Zero out stashed copy of environment, it is owned by the front-end. */
|
2015-06-17 06:49:59 -06:00
|
|
|
(void)env_init(NULL);
|
2002-11-22 18:33:47 +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:
|
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();
|
|
|
|
|
2016-05-11 07:06:45 -06:00
|
|
|
/* Destroy the password and group caches and free the contents. */
|
|
|
|
sudo_freepwcache();
|
|
|
|
sudo_freegrcache();
|
2010-08-06 12:07:03 -04:00
|
|
|
|
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;
|
2015-02-01 08:24:49 -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) {
|
2014-06-27 09:30:52 -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
|
|
|
|
2016-07-18 12:19:07 -06:00
|
|
|
/* Set fqdn callback. */
|
|
|
|
sudo_defs_table[I_FQDN].callback = cb_fqdn;
|
|
|
|
|
2016-08-09 13:14:31 -06:00
|
|
|
/* Set group_plugin callback. */
|
|
|
|
sudo_defs_table[I_GROUP_PLUGIN].callback = cb_group_plugin;
|
|
|
|
|
2011-08-09 14:54:42 -04:00
|
|
|
/* Set runas callback. */
|
2011-08-10 09:02:37 -04:00
|
|
|
sudo_defs_table[I_RUNAS_DEFAULT].callback = cb_runas_default;
|
2011-08-09 14:54:42 -04:00
|
|
|
|
2012-11-25 09:33:52 -05:00
|
|
|
/* Set locale callback. */
|
2016-07-20 14:16:00 -06:00
|
|
|
sudo_defs_table[I_SUDOERS_LOCALE].callback = sudoers_locale_callback;
|
2012-11-25 09:33:52 -05:00
|
|
|
|
2013-02-18 15:06:23 -05:00
|
|
|
/* Set maxseq callback. */
|
2016-07-18 12:11:25 -06:00
|
|
|
sudo_defs_table[I_MAXSEQ].callback = cb_maxseq;
|
2013-02-18 15:06:23 -05:00
|
|
|
|
2016-10-29 12:45:55 -06:00
|
|
|
/* 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;
|
|
|
|
|
2017-08-01 16:14:54 -06:00
|
|
|
/* Set tty_tickets callback. */
|
|
|
|
sudo_defs_table[I_TTY_TICKETS].callback = cb_tty_tickets;
|
|
|
|
|
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) {
|
2014-05-02 20:54:01 -06:00
|
|
|
log_warningx(SLOG_SEND_MAIL, N_("unknown uid: %u"),
|
|
|
|
(unsigned int) user_uid);
|
2014-04-10 16:03:26 -06:00
|
|
|
debug_return_bool(false);
|
|
|
|
}
|
2016-07-18 12:11:25 -06:00
|
|
|
|
2016-08-10 10:56:05 -06: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 (runas_group != NULL) {
|
|
|
|
if (!set_runasgr(runas_group, false))
|
|
|
|
debug_return_bool(false);
|
|
|
|
if (!set_runaspw(runas_user ? runas_user : user_name, false))
|
|
|
|
debug_return_bool(false);
|
|
|
|
} else {
|
|
|
|
if (!set_runaspw(runas_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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
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;
|
2010-05-13 10:27:03 -04:00
|
|
|
char *path = user_path;
|
2018-05-14 09:05:03 -06:00
|
|
|
int ret = FOUND;
|
2015-02-01 08:24:49 -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. */
|
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
|
|
|
|
2010-03-21 08:54:06 -04:00
|
|
|
/* Default value for cmnd, overridden below. */
|
|
|
|
if (user_cmnd == NULL)
|
|
|
|
user_cmnd = NewArgv[0];
|
|
|
|
|
2004-11-24 21:31:51 +00:00
|
|
|
if (sudo_mode & (MODE_RUN | MODE_EDIT | MODE_CHECK)) {
|
|
|
|
if (ISSET(sudo_mode, MODE_RUN | MODE_CHECK)) {
|
2010-05-13 10:27:03 -04:00
|
|
|
if (def_secure_path && !user_is_exempt())
|
|
|
|
path = def_secure_path;
|
2014-04-10 16:11:47 -06:00
|
|
|
if (!set_perms(PERM_RUNAS))
|
|
|
|
debug_return_int(-1);
|
2016-09-08 16:38:08 -06:00
|
|
|
ret = find_path(NewArgv[0], &user_cmnd, user_stat, path,
|
2015-06-18 09:51:36 -06:00
|
|
|
def_ignore_dot, NULL);
|
2014-04-10 16:11:47 -06:00
|
|
|
if (!restore_perms())
|
|
|
|
debug_return_int(-1);
|
2016-09-08 16:38:08 -06:00
|
|
|
if (ret == NOT_FOUND) {
|
2004-01-21 22:25:10 +00:00
|
|
|
/* Failed as root, try as invoking user. */
|
2014-04-10 16:11:47 -06:00
|
|
|
if (!set_perms(PERM_USER))
|
|
|
|
debug_return_int(-1);
|
2016-09-08 16:38:08 -06:00
|
|
|
ret = find_path(NewArgv[0], &user_cmnd, user_stat, path,
|
2015-06-18 09:51:36 -06:00
|
|
|
def_ignore_dot, NULL);
|
2014-04-10 16:11:47 -06:00
|
|
|
if (!restore_perms())
|
|
|
|
debug_return_int(-1);
|
2004-01-21 22:25:10 +00:00
|
|
|
}
|
2016-09-08 16:38:08 -06:00
|
|
|
if (ret == NOT_FOUND_ERROR) {
|
2014-03-26 14:15:15 -06:00
|
|
|
if (errno == ENAMETOOLONG)
|
2014-05-13 09:41:53 -06:00
|
|
|
audit_failure(NewArgc, NewArgv, N_("command too long"));
|
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 */
|
|
|
|
if (NewArgc > 1) {
|
2011-07-29 10:10:40 -04:00
|
|
|
char *to, *from, **av;
|
2003-03-13 20:00:45 +00:00
|
|
|
size_t size, n;
|
2000-12-30 03:29:47 +00:00
|
|
|
|
2004-01-21 22:25:10 +00:00
|
|
|
/* Alloc and build up user_args. */
|
2011-07-29 10:10:40 -04:00
|
|
|
for (size = 0, av = NewArgv + 1; *av; av++)
|
|
|
|
size += strlen(*av) + 1;
|
2015-06-17 06:49:59 -06:00
|
|
|
if (size == 0 || (user_args = malloc(size)) == 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(-1);
|
|
|
|
}
|
2011-07-29 10:10:40 -04:00
|
|
|
if (ISSET(sudo_mode, MODE_SHELL|MODE_LOGIN_SHELL)) {
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
for (to = user_args, av = NewArgv + 1; (from = *av); av++) {
|
|
|
|
while (*from) {
|
|
|
|
if (from[0] == '\\' && !isspace((unsigned char)from[1]))
|
|
|
|
from++;
|
|
|
|
*to++ = *from++;
|
|
|
|
}
|
|
|
|
*to++ = ' ';
|
|
|
|
}
|
|
|
|
*--to = '\0';
|
|
|
|
} else {
|
|
|
|
for (to = user_args, av = NewArgv + 1; *av; av++) {
|
|
|
|
n = strlcpy(to, *av, size - (to - user_args));
|
2014-04-10 16:03:26 -06:00
|
|
|
if (n >= size - (to - user_args)) {
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_warnx(U_("internal error, %s overflow"), __func__);
|
2014-04-10 16:03:26 -06:00
|
|
|
debug_return_int(-1);
|
|
|
|
}
|
2011-07-29 10:10:40 -04:00
|
|
|
to += n;
|
|
|
|
*to++ = ' ';
|
|
|
|
}
|
|
|
|
*--to = '\0';
|
2000-12-30 03:29:47 +00:00
|
|
|
}
|
|
|
|
}
|
2004-01-21 22:25:10 +00:00
|
|
|
}
|
2010-05-26 16:33:35 -04:00
|
|
|
|
2004-08-24 18:01:14 +00:00
|
|
|
if ((user_base = strrchr(user_cmnd, '/')) != NULL)
|
|
|
|
user_base++;
|
|
|
|
else
|
|
|
|
user_base = user_cmnd;
|
2000-12-30 03:29:47 +00:00
|
|
|
|
2018-05-14 09:05:03 -06:00
|
|
|
TAILQ_FOREACH(nss, snl, entries) {
|
2018-08-02 14:06:36 -06:00
|
|
|
if (!update_defaults(nss->parse_tree, NULL, SETDEF_CMND, false)) {
|
2018-05-14 09:05:03 -06:00
|
|
|
log_warningx(SLOG_SEND_MAIL|SLOG_NO_STDERR,
|
|
|
|
N_("problem with defaults entries"));
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-09-28 17:52:59 +00:00
|
|
|
* Open sudoers and sanity 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 *
|
2011-12-02 11:27:33 -05:00
|
|
|
open_sudoers(const char *sudoers, 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;
|
2015-02-01 08:24:49 -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
|
|
|
|
2012-02-29 15:50:48 -05:00
|
|
|
switch (sudo_secure_file(sudoers, 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) {
|
2014-04-10 16:11:47 -06:00
|
|
|
if (!restore_perms() || !set_perms(PERM_ROOT))
|
|
|
|
debug_return_ptr(NULL);
|
2012-05-21 13:59:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Open sudoers and make sure we can read it so we can present
|
|
|
|
* the user with a reasonable error message (unlike the lexer).
|
|
|
|
*/
|
2012-02-22 13:04:03 -05:00
|
|
|
if ((fp = fopen(sudoers, "r")) == NULL) {
|
2014-05-02 20:54:01 -06:00
|
|
|
log_warning(SLOG_SEND_MAIL, N_("unable to open %s"), sudoers);
|
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,
|
|
|
|
N_("unable to read %s"), sudoers);
|
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:
|
2014-05-02 20:54:01 -06:00
|
|
|
log_warning(SLOG_SEND_MAIL, N_("unable to stat %s"), sudoers);
|
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,
|
|
|
|
N_("%s is not a regular file"), sudoers);
|
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,
|
|
|
|
N_("%s is owned by uid %u, should be %u"), sudoers,
|
|
|
|
(unsigned int) sb.st_uid, (unsigned int) sudoers_uid);
|
2012-02-22 13:04:03 -05:00
|
|
|
break;
|
|
|
|
case SUDO_PATH_WORLD_WRITABLE:
|
2014-05-02 20:54:01 -06:00
|
|
|
log_warningx(SLOG_SEND_MAIL, N_("%s is world writable"), sudoers);
|
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,
|
|
|
|
N_("%s is owned by gid %u, should be %u"), sudoers,
|
|
|
|
(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;
|
2015-02-01 08:24:49 -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.
|
|
|
|
*/
|
2014-05-02 16:40:30 -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.
|
2015-06-17 06:49:59 -06:00
|
|
|
* Returns true on success, setting longp and shortp.
|
|
|
|
* Returns false on failure, longp and shortp are unchanged.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
resolve_host(const char *host, char **longp, char **shortp)
|
|
|
|
{
|
|
|
|
struct addrinfo *res0, hint;
|
|
|
|
char *cp, *lname, *sname;
|
|
|
|
debug_decl(resolve_host, SUDOERS_DEBUG_PLUGIN)
|
|
|
|
|
|
|
|
memset(&hint, 0, sizeof(hint));
|
|
|
|
hint.ai_family = PF_UNSPEC;
|
|
|
|
hint.ai_flags = AI_FQDN;
|
|
|
|
|
|
|
|
if (getaddrinfo(host, NULL, &hint, &res0) != 0)
|
|
|
|
debug_return_bool(false);
|
|
|
|
if ((lname = strdup(res0->ai_canonname)) == NULL) {
|
|
|
|
freeaddrinfo(res0);
|
|
|
|
debug_return_bool(false);
|
|
|
|
}
|
|
|
|
if ((cp = strchr(lname, '.')) != NULL) {
|
|
|
|
sname = strndup(lname, (size_t)(cp - lname));
|
|
|
|
if (sname == NULL) {
|
|
|
|
free(lname);
|
|
|
|
freeaddrinfo(res0);
|
|
|
|
debug_return_bool(false);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sname = lname;
|
|
|
|
}
|
|
|
|
freeaddrinfo(res0);
|
|
|
|
*longp = lname;
|
|
|
|
*shortp = sname;
|
|
|
|
|
|
|
|
debug_return_bool(true);
|
|
|
|
}
|
|
|
|
|
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
|
2016-07-19 14:52:33 -06:00
|
|
|
cb_fqdn(const union sudo_defs_val *sd_un)
|
1999-12-02 20:31:25 +00:00
|
|
|
{
|
2014-11-12 20:33:41 -07:00
|
|
|
bool remote;
|
2015-06-17 06:49:59 -06:00
|
|
|
char *lhost, *shost;
|
2016-07-18 12:19:07 -06: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. */
|
|
|
|
if (!resolve_host(user_host, &lhost, &shost)) {
|
|
|
|
if (!resolve_host(user_runhost, &lhost, &shost)) {
|
|
|
|
log_warning(SLOG_SEND_MAIL|SLOG_RAW_MSG,
|
|
|
|
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) {
|
2017-08-01 13:45:32 -06:00
|
|
|
if (!resolve_host(user_runhost, &lhost, &shost)) {
|
|
|
|
sudo_warnx(U_("unable to resolve host %s"), user_runhost);
|
|
|
|
}
|
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;
|
2015-02-01 08:24:49 -07:00
|
|
|
debug_decl(set_runaspw, SUDOERS_DEBUG_PLUGIN)
|
2011-10-22 14:40:21 -04:00
|
|
|
|
2004-01-16 23:09:20 +00:00
|
|
|
if (*user == '#') {
|
2013-12-05 15:51:56 -07:00
|
|
|
const char *errstr;
|
2014-06-27 14:53:54 -06:00
|
|
|
uid_t uid = sudo_strtoid(user + 1, NULL, NULL, &errstr);
|
2013-12-05 15:51:56 -07:00
|
|
|
if (errstr == NULL) {
|
|
|
|
if ((pw = sudo_getpwuid(uid)) == NULL)
|
2015-04-16 13:19:04 -06:00
|
|
|
pw = sudo_fakepwnam(user, user_gid);
|
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)
|
2014-05-02 20:54:01 -06:00
|
|
|
log_warningx(SLOG_RAW_MSG, 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;
|
2015-02-01 08:24:49 -07:00
|
|
|
debug_decl(set_runasgr, SUDOERS_DEBUG_PLUGIN)
|
2011-10-22 14:40:21 -04:00
|
|
|
|
2007-11-21 20:12:00 +00:00
|
|
|
if (*group == '#') {
|
2013-12-05 15:51:56 -07:00
|
|
|
const char *errstr;
|
2014-06-27 14:53:54 -06:00
|
|
|
gid_t gid = sudo_strtoid(group + 1, NULL, NULL, &errstr);
|
2013-12-05 15:51:56 -07:00
|
|
|
if (errstr == NULL) {
|
|
|
|
if ((gr = sudo_getgrgid(gid)) == NULL)
|
|
|
|
gr = sudo_fakegrnam(group);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (gr == NULL) {
|
2014-04-10 16:03:26 -06:00
|
|
|
if ((gr = sudo_getgrnam(group)) == NULL) {
|
|
|
|
if (!quiet)
|
2014-05-02 20:54:01 -06:00
|
|
|
log_warningx(SLOG_RAW_MSG, 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
|
2016-07-18 12:11:25 -06:00
|
|
|
cb_runas_default(const union sudo_defs_val *sd_un)
|
2011-08-10 09:02:37 -04:00
|
|
|
{
|
2016-07-18 12:20:46 -06:00
|
|
|
debug_decl(cb_runas_default, SUDOERS_DEBUG_PLUGIN)
|
|
|
|
|
2011-08-10 09:02:37 -04:00
|
|
|
/* Only reset runaspw if user didn't specify one. */
|
|
|
|
if (!runas_user && !runas_group)
|
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
|
|
|
/*
|
|
|
|
* Callback for runas_default sudoers setting.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
cb_tty_tickets(const union sudo_defs_val *sd_un)
|
|
|
|
{
|
|
|
|
debug_decl(cb_tty_tickets, SUDOERS_DEBUG_PLUGIN)
|
|
|
|
|
|
|
|
/* Convert tty_tickets -> timestamp_type */
|
|
|
|
if (sd_un->flag)
|
|
|
|
def_timestamp_type = tty;
|
|
|
|
else
|
|
|
|
def_timestamp_type = global;
|
|
|
|
debug_return_bool(true);
|
|
|
|
}
|
|
|
|
|
2004-11-15 15:53:53 +00:00
|
|
|
/*
|
2014-06-27 09:30:52 -06:00
|
|
|
* Cleanup hook for sudo_fatal()/sudo_fatalx()
|
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;
|
2015-02-01 08:24:49 -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
|
|
|
}
|
2005-11-18 01:39:59 +00:00
|
|
|
}
|
2012-11-25 09:34:40 -05:00
|
|
|
if (def_group_plugin)
|
|
|
|
group_plugin_unload();
|
2016-05-11 07:06:45 -06:00
|
|
|
sudo_freepwcache();
|
|
|
|
sudo_freegrcache();
|
2012-11-25 09:34:40 -05:00
|
|
|
|
|
|
|
debug_return;
|
2004-11-15 15:53:53 +00:00
|
|
|
}
|
|
|
|
|
2010-07-28 16:34:11 -04:00
|
|
|
#ifdef USE_ADMIN_FLAG
|
2015-06-25 11:12:36 -06:00
|
|
|
static int
|
2010-07-28 16:34:11 -04:00
|
|
|
create_admin_success_flag(void)
|
|
|
|
{
|
|
|
|
char flagfile[PATH_MAX];
|
2016-09-08 16:38:08 -06:00
|
|
|
int len, ret = -1;
|
2015-02-01 08:24:49 -07:00
|
|
|
debug_decl(create_admin_success_flag, SUDOERS_DEBUG_PLUGIN)
|
2010-07-28 16:34:11 -04:00
|
|
|
|
2016-05-06 14:30:46 -06:00
|
|
|
/* Check whether the user is in the sudo or admin group. */
|
|
|
|
if (!user_in_group(sudo_user.pw, "sudo") &&
|
|
|
|
!user_in_group(sudo_user.pw, "admin"))
|
2015-06-25 11:12:36 -06:00
|
|
|
debug_return_int(true);
|
2010-07-28 16:34:11 -04:00
|
|
|
|
|
|
|
/* Build path to flag file. */
|
2015-06-25 11:12:36 -06:00
|
|
|
len = snprintf(flagfile, sizeof(flagfile), "%s/.sudo_as_admin_successful",
|
2010-07-28 16:34:11 -04:00
|
|
|
user_dir);
|
2015-06-25 11:12:36 -06:00
|
|
|
if (len <= 0 || (size_t)len >= sizeof(flagfile))
|
|
|
|
debug_return_int(false);
|
2010-07-28 16:34:11 -04:00
|
|
|
|
|
|
|
/* Create admin flag file if it doesn't already exist. */
|
2014-04-10 16:11:47 -06:00
|
|
|
if (set_perms(PERM_USER)) {
|
2016-05-06 14:12:08 -06:00
|
|
|
int fd = open(flagfile, O_CREAT|O_WRONLY|O_NONBLOCK|O_EXCL, 0644);
|
2016-09-08 16:38:08 -06:00
|
|
|
ret = fd != -1 || errno == EEXIST;
|
2016-05-06 14:12:08 -06:00
|
|
|
if (fd != -1)
|
|
|
|
close(fd);
|
2015-06-25 11:12:36 -06:00
|
|
|
if (!restore_perms())
|
2016-09-08 16:38:08 -06:00
|
|
|
ret = -1;
|
2010-07-28 16:34:11 -04:00
|
|
|
}
|
2016-09-08 16:38:08 -06:00
|
|
|
debug_return_int(ret);
|
2010-07-28 16:34:11 -04:00
|
|
|
}
|
|
|
|
#else /* !USE_ADMIN_FLAG */
|
2015-06-25 11:12:36 -06:00
|
|
|
static int
|
2010-07-28 16:34:11 -04:00
|
|
|
create_admin_success_flag(void)
|
|
|
|
{
|
|
|
|
/* STUB */
|
2015-06-25 11:12:36 -06:00
|
|
|
return true;
|
2010-07-28 16:34:11 -04:00
|
|
|
}
|
|
|
|
#endif /* USE_ADMIN_FLAG */
|
2013-04-01 10:40:17 -04:00
|
|
|
|
|
|
|
static bool
|
|
|
|
tty_present(void)
|
|
|
|
{
|
2018-03-05 17:35:02 -07:00
|
|
|
#if defined(HAVE_KINFO_PROC2_NETBSD) || defined(HAVE_KINFO_PROC_OPENBSD) || defined(HAVE_KINFO_PROC_FREEBSD) || defined(HAVE_KINFO_PROC_44BSD) || defined(HAVE_STRUCT_PSINFO_PR_TTYDEV) || defined(HAVE_PSTAT_GETPROC) || defined(__linux__)
|
2013-04-01 10:40:17 -04:00
|
|
|
return user_ttypath != NULL;
|
|
|
|
#else
|
2016-05-16 11:17:20 -06:00
|
|
|
int fd = open(_PATH_TTY, O_RDWR);
|
2013-04-01 10:40:17 -04:00
|
|
|
if (fd != -1)
|
|
|
|
close(fd);
|
|
|
|
return fd != -1;
|
|
|
|
#endif
|
|
|
|
}
|