1993-11-27 23:42:49 +00:00
|
|
|
/*
|
2014-04-07 05:34:56 -06:00
|
|
|
* Copyright (c) 1993-1996, 1998-2014 Todd C. Miller <Todd.Miller@courtesan.com>
|
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
|
|
|
*/
|
|
|
|
|
2003-04-02 18:25:30 +00:00
|
|
|
#define _SUDO_MAIN
|
1993-02-16 17:07:38 +00: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>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/socket.h>
|
1993-02-16 17:07:38 +00:00
|
|
|
#include <stdio.h>
|
1994-03-12 18:36:53 +00:00
|
|
|
#ifdef STDC_HEADERS
|
2001-12-14 19:52:54 +00:00
|
|
|
# include <stdlib.h>
|
|
|
|
# include <stddef.h>
|
|
|
|
#else
|
|
|
|
# ifdef HAVE_STDLIB_H
|
|
|
|
# include <stdlib.h>
|
|
|
|
# endif
|
1994-03-12 18:36:53 +00:00
|
|
|
#endif /* STDC_HEADERS */
|
|
|
|
#ifdef HAVE_STRING_H
|
2001-12-14 19:52:54 +00:00
|
|
|
# if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
|
|
|
|
# include <memory.h>
|
|
|
|
# endif
|
|
|
|
# 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 */
|
2001-12-14 19:52:54 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif /* HAVE_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>
|
2011-12-02 17:28:50 -05: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"
|
|
|
|
#include "auth/sudo_auth.h"
|
2012-02-22 13:04:03 -05:00
|
|
|
#include "secure_path.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
|
|
|
*/
|
2012-10-25 16:58:31 -04:00
|
|
|
static char *find_editor(int nfiles, char **files, char ***argv_out);
|
2014-04-04 15:30:12 -06:00
|
|
|
static bool cb_runas_default(const char *);
|
|
|
|
static bool cb_sudoers_locale(const char *);
|
2011-07-18 16:23:38 -04:00
|
|
|
static int set_cmnd(void);
|
2012-10-25 16:58:31 -04:00
|
|
|
static void create_admin_success_flag(void);
|
|
|
|
static void init_vars(char * const *);
|
|
|
|
static void set_fqdn(void);
|
2010-03-17 19:56:27 -04:00
|
|
|
static void set_loginclass(struct passwd *);
|
2011-08-10 09:02:37 -04:00
|
|
|
static void set_runasgr(const char *);
|
2012-10-25 16:58:31 -04:00
|
|
|
static void set_runaspw(const char *);
|
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;
|
2008-02-08 13:18:12 +00:00
|
|
|
int long_list;
|
2002-11-22 18:23:24 +00:00
|
|
|
uid_t timestamp_uid;
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
{
|
2010-10-12 10:47:16 -04:00
|
|
|
volatile int sources = 0;
|
2012-10-25 16:58:31 -04:00
|
|
|
struct sudo_nss *nss, *nss_next;
|
|
|
|
debug_decl(sudoers_policy_init, SUDO_DEBUG_PLUGIN)
|
2010-03-14 19:58:47 -04:00
|
|
|
|
2011-05-17 16:38:40 -04:00
|
|
|
bindtextdomain("sudoers", LOCALEDIR);
|
|
|
|
|
2004-11-15 04:06:16 +00:00
|
|
|
sudo_setpwent();
|
2004-11-16 04:24:11 +00:00
|
|
|
sudo_setgrent();
|
1996-09-06 04:12:58 +00:00
|
|
|
|
2013-04-26 16:06:05 -04:00
|
|
|
/* Register fatal/fatalx callback. */
|
2013-04-18 14:07:59 -04:00
|
|
|
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). */
|
|
|
|
env_init(envp);
|
|
|
|
|
1999-09-08 08:06:28 +00:00
|
|
|
/* Setup defaults data structures. */
|
|
|
|
init_defaults();
|
|
|
|
|
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))
|
|
|
|
debug_return_bool(-1);
|
2010-03-14 19:58:47 -04:00
|
|
|
|
|
|
|
init_vars(envp); /* XXX - move this later? */
|
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. */
|
|
|
|
set_perms(PERM_ROOT);
|
2010-04-20 17:00:31 -04:00
|
|
|
|
2008-01-01 18:22:03 +00:00
|
|
|
/* Open and parse sudoers, set global defaults */
|
2013-10-22 09:00:37 -06:00
|
|
|
TAILQ_FOREACH_SAFE(nss, snl, entries, nss_next) {
|
2012-08-10 11:59:26 -04:00
|
|
|
if (nss->open(nss) == 0 && nss->parse(nss) == 0) {
|
|
|
|
sources++;
|
|
|
|
if (nss->setdefs(nss) != 0)
|
2013-04-18 14:14:03 -04:00
|
|
|
log_warning(NO_STDERR, N_("problem with defaults entries"));
|
2012-08-10 11:59:26 -04:00
|
|
|
} else {
|
2013-10-22 09:00:37 -06:00
|
|
|
TAILQ_REMOVE(snl, nss, entries);
|
2012-08-10 11:59:26 -04:00
|
|
|
}
|
2004-10-26 22:22:46 +00:00
|
|
|
}
|
2010-03-14 19:58:47 -04:00
|
|
|
if (sources == 0) {
|
2013-11-18 08:59:57 -07:00
|
|
|
warningx(U_("no valid sudoers sources found, quitting"));
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_return_bool(-1);
|
2010-03-14 19:58:47 -04:00
|
|
|
}
|
2004-02-13 02:08:27 +00:00
|
|
|
|
2007-12-02 00:51:32 +00:00
|
|
|
/* XXX - collect post-sudoers parse settings into a function */
|
|
|
|
|
2010-07-02 11:11:25 -04:00
|
|
|
/*
|
2011-09-24 10:24:40 -04:00
|
|
|
* Initialize external group plugin, if any.
|
2010-07-02 11:11:25 -04:00
|
|
|
*/
|
|
|
|
if (def_group_plugin) {
|
2011-12-02 11:27:33 -05:00
|
|
|
if (group_plugin_load(def_group_plugin) != true)
|
2010-07-02 11:11:25 -04:00
|
|
|
def_group_plugin = NULL;
|
|
|
|
}
|
|
|
|
|
2007-11-21 20:12:00 +00:00
|
|
|
/*
|
|
|
|
* Set runas passwd/group entries based on command line or sudoers.
|
|
|
|
* Note that if runas_group was specified without runas_user we
|
|
|
|
* defer setting runas_pw so the match routines know to ignore it.
|
|
|
|
*/
|
2012-10-25 16:58:31 -04:00
|
|
|
/* XXX - qpm4u does more here as it may have already set runas_pw */
|
2007-11-21 20:12:00 +00:00
|
|
|
if (runas_group != NULL) {
|
|
|
|
set_runasgr(runas_group);
|
|
|
|
if (runas_user != NULL)
|
|
|
|
set_runaspw(runas_user);
|
|
|
|
} else
|
|
|
|
set_runaspw(runas_user ? runas_user : def_runas_default);
|
|
|
|
|
2008-11-01 13:20:01 +00:00
|
|
|
if (!update_defaults(SETDEF_RUNAS))
|
2013-04-18 14:14:03 -04:00
|
|
|
log_warning(NO_STDERR, N_("problem with defaults entries"));
|
2008-11-01 13:20:01 +00:00
|
|
|
|
2010-06-18 17:47:55 -04:00
|
|
|
if (def_fqdn)
|
|
|
|
set_fqdn(); /* deferred until after sudoers is parsed */
|
|
|
|
|
2007-12-02 00:51:32 +00:00
|
|
|
/* Set login class if applicable. */
|
2011-12-02 14:29:54 -05:00
|
|
|
set_loginclass(runas_pw ? runas_pw : sudo_user.pw);
|
2007-12-02 00:51:32 +00:00
|
|
|
|
2010-04-20 17:00:31 -04:00
|
|
|
restore_perms();
|
|
|
|
|
2011-12-02 11:27:33 -05:00
|
|
|
debug_return_bool(true);
|
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[],
|
2012-10-25 16:58:31 -04:00
|
|
|
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;
|
|
|
|
mode_t cmnd_umask = 0777;
|
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;
|
2011-12-02 11:27:33 -05:00
|
|
|
volatile int rval = true;
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_decl(sudoers_policy_main, SUDO_DEBUG_PLUGIN)
|
1996-02-02 03:34:21 +00:00
|
|
|
|
2012-10-25 16:58:31 -04:00
|
|
|
/* XXX - would like to move this to policy.c but need the cleanup. */
|
2013-04-18 14:07:59 -04:00
|
|
|
if (fatal_setjmp() != 0) {
|
2013-04-26 16:06:05 -04:00
|
|
|
/* error recovery via fatal(), fatalx() or log_fatal() */
|
2011-03-08 09:38:21 -05:00
|
|
|
rval = -1;
|
|
|
|
goto done;
|
2010-12-28 11:02:12 -05:00
|
|
|
}
|
|
|
|
|
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. */
|
2013-11-18 08:59:57 -07:00
|
|
|
warningx(U_("sudoers specifies that root is not allowed to sudo"));
|
2011-03-08 09:38:21 -05:00
|
|
|
goto bad;
|
2010-03-14 19:58:47 -04:00
|
|
|
}
|
1999-12-05 02:54:20 +00:00
|
|
|
|
2010-04-20 17:00:31 -04:00
|
|
|
set_perms(PERM_INITIAL);
|
|
|
|
|
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;
|
|
|
|
NewArgv = emalloc2(NewArgc + 1, sizeof(char *));
|
|
|
|
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;
|
2011-07-17 10:37:15 -04:00
|
|
|
NewArgv = emalloc2(NewArgc + 2, sizeof(char *));
|
|
|
|
memcpy(++NewArgv, argv, argc * sizeof(char *));
|
2010-09-07 17:59:10 -04:00
|
|
|
NewArgv[NewArgc] = NULL;
|
2011-11-03 09:31:20 -04:00
|
|
|
if (ISSET(sudo_mode, MODE_LOGIN_SHELL) && runas_pw != NULL)
|
2010-09-14 11:30:28 -04:00
|
|
|
NewArgv[0] = estrdup(runas_pw->pw_shell);
|
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();
|
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? */
|
2013-11-18 08:59:57 -07:00
|
|
|
warningx(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);
|
2008-01-05 18:27:18 +00:00
|
|
|
validated = FLAG_NO_USER | FLAG_NO_HOST;
|
2013-10-22 09:00:37 -06:00
|
|
|
TAILQ_FOREACH(nss, snl, entries) {
|
2008-01-05 18:27:18 +00:00
|
|
|
validated = nss->lookup(nss, validated, pwflag);
|
2007-12-28 16:20:45 +00:00
|
|
|
|
2009-03-10 20:44:05 +00:00
|
|
|
if (ISSET(validated, VALIDATE_OK)) {
|
2012-09-04 09:22:10 -04:00
|
|
|
/* Handle [SUCCESS=return] */
|
2009-03-10 20:44:05 +00:00
|
|
|
if (nss->ret_if_found)
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
/* Handle [NOTFOUND=return] */
|
|
|
|
if (nss->ret_if_notfound)
|
|
|
|
break;
|
|
|
|
}
|
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);
|
|
|
|
|
2004-11-24 21:28:55 +00:00
|
|
|
if (safe_cmnd == NULL)
|
|
|
|
safe_cmnd = estrdup(user_cmnd);
|
2004-10-26 22:22:46 +00:00
|
|
|
|
2007-11-21 20:12:00 +00:00
|
|
|
/* If only a group was specified, set runas_pw based on invoking user. */
|
|
|
|
if (runas_pw == NULL)
|
|
|
|
set_runaspw(user_name);
|
|
|
|
|
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;
|
|
|
|
uid_t uid = atoid(def_timestampowner + 1, NULL, NULL, &errstr);
|
|
|
|
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;
|
2012-06-13 16:21:45 -04:00
|
|
|
sudo_pw_delref(pw);
|
2012-03-26 11:02:06 -04:00
|
|
|
} else {
|
2013-04-18 14:14:03 -04:00
|
|
|
log_warning(0, N_("timestamp owner (%s): No such user"),
|
2003-12-30 22:20:21 +00:00
|
|
|
def_timestampowner);
|
2012-03-26 11:02:06 -04:00
|
|
|
timestamp_uid = ROOT_UID;
|
|
|
|
}
|
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. */
|
2010-03-21 08:54:06 -04:00
|
|
|
rval = -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()) {
|
2013-03-28 15:46:52 -04:00
|
|
|
audit_failure(NewArgv, N_("no tty"));
|
2013-11-18 08:59:57 -07:00
|
|
|
warningx(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. */
|
2011-03-10 15:11:49 -05:00
|
|
|
rebuild_env();
|
2007-06-23 23:58:54 +00:00
|
|
|
|
2003-12-30 22:31:30 +00:00
|
|
|
/* Require a password if sudoers says so. */
|
2011-10-25 10:08:26 -04:00
|
|
|
rval = check_user(validated, sudo_mode);
|
2012-07-10 12:42:33 -04:00
|
|
|
if (rval != true) {
|
2014-03-25 16:46:00 -06:00
|
|
|
/* Note: log_denial() calls audit for us. */
|
2012-07-10 12:42:33 -04:00
|
|
|
if (!ISSET(validated, VALIDATE_OK))
|
2012-11-06 11:19:51 -05:00
|
|
|
log_denial(validated, false);
|
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. */
|
2010-03-14 19:58:47 -04:00
|
|
|
if (!ISSET(validated, VALIDATE_OK)) {
|
2014-03-25 16:46:00 -06:00
|
|
|
/* Note: log_failure() calls audit for us. */
|
2012-07-10 12:42:33 -04:00
|
|
|
log_failure(validated, cmnd_status);
|
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. */
|
|
|
|
create_admin_success_flag();
|
|
|
|
|
2010-03-14 19:58:47 -04:00
|
|
|
/* Finally tell the user if the command did not exist. */
|
|
|
|
if (cmnd_status == NOT_FOUND_DOT) {
|
2012-11-08 15:37:43 -05:00
|
|
|
audit_failure(NewArgv, N_("command in current directory"));
|
2013-11-18 08:59:57 -07:00
|
|
|
warningx(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)) {
|
|
|
|
audit_failure(NewArgv, N_("%s: command not found"), NewArgv[0]);
|
2013-11-18 08:59:57 -07:00
|
|
|
warningx(U_("%s: command not found"), NewArgv[0]);
|
2013-02-20 15:35:26 -05:00
|
|
|
} else {
|
|
|
|
audit_failure(NewArgv, N_("%s: command not found"), user_cmnd);
|
2013-11-18 08:59:57 -07:00
|
|
|
warningx(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
|
|
|
|
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)) {
|
2014-03-25 16:46:00 -06:00
|
|
|
/* XXX - audit? */
|
2013-11-18 08:59:57 -07:00
|
|
|
warningx(U_("sorry, you are not allowed to preserve the environment"));
|
2011-03-08 09:38:21 -05:00
|
|
|
goto bad;
|
2010-03-14 19:58:47 -04:00
|
|
|
} else
|
2010-04-26 20:41:28 -04:00
|
|
|
validate_env_vars(sudo_user.env_vars);
|
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);
|
2011-03-11 12:11:05 -05:00
|
|
|
sudo_user.iolog_file++;
|
2010-12-27 12:18:32 -05:00
|
|
|
}
|
2010-12-20 16:28:20 -05:00
|
|
|
}
|
|
|
|
|
2010-03-14 19:58:47 -04:00
|
|
|
log_allowed(validated);
|
2010-03-15 18:13:58 -04:00
|
|
|
if (ISSET(sudo_mode, MODE_CHECK))
|
|
|
|
rval = display_cmnd(snl, list_pw ? list_pw : sudo_user.pw);
|
|
|
|
else if (ISSET(sudo_mode, MODE_LIST))
|
|
|
|
display_privs(snl, list_pw ? list_pw : sudo_user.pw); /* XXX - return val */
|
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();
|
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))) {
|
2011-03-08 09:38:21 -05:00
|
|
|
/* rval 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.
|
|
|
|
*/
|
|
|
|
if (def_umask != 0777) {
|
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. */
|
2011-12-02 11:27:33 -05:00
|
|
|
read_env_file(_PATH_ENVIRONMENT, true);
|
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. */
|
2010-04-26 17:14:11 -04:00
|
|
|
if (def_env_file)
|
2011-12-02 11:27:33 -05:00
|
|
|
read_env_file(def_env_file, false);
|
2007-12-21 21:53:32 +00:00
|
|
|
|
2010-03-14 19:58:47 -04:00
|
|
|
/* Insert user-specified environment variables. */
|
2010-04-26 20:41:28 -04:00
|
|
|
insert_env_vars(sudo_user.env_vars);
|
2004-01-18 22:55:32 +00:00
|
|
|
|
2010-05-13 14:09:21 -04:00
|
|
|
if (ISSET(sudo_mode, MODE_EDIT)) {
|
2012-10-25 13:16:58 -04:00
|
|
|
efree(safe_cmnd);
|
|
|
|
safe_cmnd = find_editor(NewArgc - 1, NewArgv + 1, &edit_argv);
|
|
|
|
if (safe_cmnd == NULL)
|
2011-03-08 09:38:21 -05:00
|
|
|
goto bad;
|
2010-05-13 14:09:21 -04:00
|
|
|
}
|
2004-01-22 02:57:01 +00:00
|
|
|
|
2010-03-14 19:58:47 -04:00
|
|
|
/* Must audit before uid change. */
|
2014-03-26 13:00:56 -06:00
|
|
|
if (audit_success(NewArgv) != 0)
|
|
|
|
goto bad;
|
2008-05-03 00:53:21 +00:00
|
|
|
|
2012-10-25 16:58:31 -04:00
|
|
|
/* Setup execution environment to pass back to front-end. */
|
|
|
|
rval = sudoers_policy_exec_setup(edit_argv ? edit_argv : NewArgv,
|
|
|
|
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. */
|
2012-03-07 16:35:42 -05:00
|
|
|
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:
|
2011-12-02 11:27:33 -05:00
|
|
|
rval = false;
|
2010-04-20 17:00:31 -04:00
|
|
|
|
2010-03-14 19:58:47 -04:00
|
|
|
done:
|
2013-04-18 14:07:59 -04:00
|
|
|
fatal_disable_setjmp();
|
2011-03-08 09:38:21 -05:00
|
|
|
rewind_perms();
|
|
|
|
|
2010-08-06 12:07:03 -04:00
|
|
|
/* Close the password and group files and free up memory. */
|
|
|
|
sudo_endpwent();
|
|
|
|
sudo_endgrent();
|
|
|
|
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_return_bool(rval);
|
1993-02-16 17:07:38 +00:00
|
|
|
}
|
|
|
|
|
1999-07-22 12:55:17 +00:00
|
|
|
/*
|
2012-10-25 16:58:31 -04:00
|
|
|
* Initialize timezone and fill in ``sudo_user'' struct.
|
1993-02-16 17:07:38 +00:00
|
|
|
*/
|
2004-10-26 22:22:46 +00:00
|
|
|
static void
|
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;
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_decl(init_vars, SUDO_DEBUG_PLUGIN)
|
1999-01-17 22:08:55 +00:00
|
|
|
|
2012-11-25 09:33:52 -05:00
|
|
|
sudoers_initlocale(setlocale(LC_ALL, NULL), def_sudoers_locale);
|
2012-11-08 15:37:43 -05:00
|
|
|
|
2005-02-13 05:33:59 +00:00
|
|
|
for (ep = envp; *ep; ep++) {
|
2008-03-02 14:31:57 +00:00
|
|
|
/* XXX - don't fill in if empty string */
|
2005-02-13 05:33:59 +00:00
|
|
|
switch (**ep) {
|
2007-07-15 19:44:46 +00:00
|
|
|
case 'K':
|
|
|
|
if (strncmp("KRB5CCNAME=", *ep, 11) == 0)
|
|
|
|
user_ccname = *ep + 11;
|
|
|
|
break;
|
2005-02-13 05:33:59 +00:00
|
|
|
case 'P':
|
|
|
|
if (strncmp("PATH=", *ep, 5) == 0)
|
|
|
|
user_path = *ep + 5;
|
|
|
|
break;
|
|
|
|
case 'S':
|
2010-03-21 08:54:06 -04:00
|
|
|
if (!user_prompt && strncmp("SUDO_PROMPT=", *ep, 12) == 0)
|
2005-02-13 05:33:59 +00:00
|
|
|
user_prompt = *ep + 12;
|
|
|
|
else if (strncmp("SUDO_USER=", *ep, 10) == 0)
|
|
|
|
prev_user = *ep + 10;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1993-10-04 19:10:33 +00:00
|
|
|
/*
|
2012-10-25 16:58:31 -04:00
|
|
|
* Get a local copy of the user's struct passwd if we don't already
|
|
|
|
* have one.
|
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.
|
|
|
|
*/
|
|
|
|
if (sudo_mode == MODE_KILL || sudo_mode == MODE_INVALIDATE)
|
2013-11-18 08:59:57 -07:00
|
|
|
fatalx(U_("unknown uid: %u"), (unsigned int) user_uid);
|
2011-09-25 06:35:40 -04:00
|
|
|
|
2012-10-25 16:58:31 -04:00
|
|
|
/* Need to make a fake struct passwd for the call to log_fatal(). */
|
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
|
|
|
}
|
2011-07-20 11:58:45 -04:00
|
|
|
|
|
|
|
/*
|
2013-08-07 10:13:04 -06:00
|
|
|
* Get group list and store initialize permissions.
|
2011-07-20 11:58:45 -04:00
|
|
|
*/
|
|
|
|
if (user_group_list == NULL)
|
2012-06-13 16:21:45 -04:00
|
|
|
user_group_list = sudo_get_grlist(sudo_user.pw);
|
2013-08-07 10:13:04 -06:00
|
|
|
set_perms(PERM_INITIAL);
|
2011-07-20 11:58:45 -04:00
|
|
|
|
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. */
|
|
|
|
sudo_defs_table[I_SUDOERS_LOCALE].callback = cb_sudoers_locale;
|
|
|
|
|
2013-02-18 15:06:23 -05:00
|
|
|
/* Set maxseq callback. */
|
|
|
|
sudo_defs_table[I_MAXSEQ].callback = io_set_max_sessid;
|
|
|
|
|
2012-03-26 10:59:14 -04:00
|
|
|
/* It is now safe to use log_fatal() and set_perms() */
|
2013-08-07 10:13:04 -06:00
|
|
|
if (unknown_user)
|
|
|
|
log_fatal(0, N_("unknown uid: %u"), (unsigned int) user_uid);
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_return;
|
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
|
|
|
{
|
|
|
|
int rval;
|
2010-05-13 10:27:03 -04:00
|
|
|
char *path = user_path;
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_decl(set_cmnd, SUDO_DEBUG_PLUGIN)
|
2000-10-26 16:42:40 +00:00
|
|
|
|
1999-07-22 12:55:17 +00:00
|
|
|
/* Resolve the path and return. */
|
2004-01-21 22:25:10 +00:00
|
|
|
rval = FOUND;
|
2012-03-19 11:24:24 -04:00
|
|
|
user_stat = ecalloc(1, sizeof(struct stat));
|
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;
|
2004-01-21 22:25:10 +00:00
|
|
|
set_perms(PERM_RUNAS);
|
2010-05-13 10:27:03 -04:00
|
|
|
rval = find_path(NewArgv[0], &user_cmnd, user_stat, path,
|
|
|
|
def_ignore_dot);
|
2010-04-10 10:34:37 -04:00
|
|
|
restore_perms();
|
2014-03-26 14:15:15 -06:00
|
|
|
if (rval == NOT_FOUND) {
|
2004-01-21 22:25:10 +00:00
|
|
|
/* Failed as root, try as invoking user. */
|
|
|
|
set_perms(PERM_USER);
|
2010-05-13 10:27:03 -04:00
|
|
|
rval = find_path(NewArgv[0], &user_cmnd, user_stat, path,
|
|
|
|
def_ignore_dot);
|
2010-04-10 10:34:37 -04:00
|
|
|
restore_perms();
|
2004-01-21 22:25:10 +00:00
|
|
|
}
|
2014-03-26 14:15:15 -06:00
|
|
|
if (rval == NOT_FOUND_ERROR) {
|
|
|
|
if (errno == ENAMETOOLONG)
|
|
|
|
audit_failure(NewArgv, N_("command too long"));
|
|
|
|
log_fatal(NO_MAIL|USE_ERRNO, NewArgv[0]);
|
|
|
|
}
|
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;
|
2010-03-14 19:58:47 -04:00
|
|
|
user_args = emalloc(size);
|
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));
|
|
|
|
if (n >= size - (to - user_args))
|
2014-04-01 16:42:13 -06:00
|
|
|
fatalx(U_("internal error, %s overflow"), __func__);
|
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
|
|
|
|
2008-11-01 13:20:01 +00:00
|
|
|
if (!update_defaults(SETDEF_CMND))
|
2013-04-18 14:14:03 -04:00
|
|
|
log_warning(NO_STDERR, N_("problem with defaults entries"));
|
2004-11-19 21:35:12 +00:00
|
|
|
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_return_int(rval);
|
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;
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_decl(open_sudoers, SUDO_DEBUG_PLUGIN)
|
1994-11-10 00:49:53 +00:00
|
|
|
|
2002-05-05 00:43:38 +00:00
|
|
|
set_perms(PERM_SUDOERS);
|
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) {
|
2012-05-21 13:59:02 -04:00
|
|
|
restore_perms();
|
|
|
|
set_perms(PERM_ROOT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* 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) {
|
2013-04-18 14:14:03 -04:00
|
|
|
log_warning(USE_ERRNO, N_("unable to open %s"), sudoers);
|
2012-02-22 13:04:03 -05:00
|
|
|
} else {
|
|
|
|
if (sb.st_size != 0 && fgetc(fp) == EOF) {
|
2013-04-18 14:14:03 -04:00
|
|
|
log_warning(USE_ERRNO, N_("unable to read %s"),
|
2012-02-22 13:04:03 -05:00
|
|
|
sudoers);
|
|
|
|
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:
|
2013-04-18 14:14:03 -04:00
|
|
|
log_warning(USE_ERRNO, N_("unable to stat %s"), sudoers);
|
2012-02-22 13:04:03 -05:00
|
|
|
break;
|
|
|
|
case SUDO_PATH_BAD_TYPE:
|
2013-04-18 14:14:03 -04:00
|
|
|
log_warning(0, N_("%s is not a regular file"), sudoers);
|
2012-02-22 13:04:03 -05:00
|
|
|
break;
|
|
|
|
case SUDO_PATH_WRONG_OWNER:
|
2013-04-18 14:14:03 -04:00
|
|
|
log_warning(0, N_("%s is owned by uid %u, should be %u"),
|
2012-02-22 13:04:03 -05:00
|
|
|
sudoers, (unsigned int) sb.st_uid, (unsigned int) sudoers_uid);
|
|
|
|
break;
|
|
|
|
case SUDO_PATH_WORLD_WRITABLE:
|
2013-04-18 14:14:03 -04:00
|
|
|
log_warning(0, N_("%s is world writable"), sudoers);
|
2012-02-22 13:04:03 -05:00
|
|
|
break;
|
|
|
|
case SUDO_PATH_GROUP_WRITABLE:
|
2013-04-18 14:14:03 -04:00
|
|
|
log_warning(0, N_("%s is owned by gid %u, should be %u"),
|
2012-02-22 13:04:03 -05:00
|
|
|
sudoers, (unsigned int) sb.st_gid, (unsigned int) sudoers_gid);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* NOTREACHED */
|
|
|
|
break;
|
1998-01-21 05:03:46 +00:00
|
|
|
}
|
1995-11-16 07:41:35 +00:00
|
|
|
|
2010-04-10 10:34:37 -04:00
|
|
|
restore_perms(); /* change back to root */
|
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
|
|
|
|
static void
|
2010-04-22 18:09:53 -04:00
|
|
|
set_loginclass(struct passwd *pw)
|
2000-03-07 04:29:46 +00:00
|
|
|
{
|
2012-03-26 10:59:14 -04:00
|
|
|
const int errflags = NO_MAIL|MSG_ONLY;
|
2011-12-08 17:17:25 -05:00
|
|
|
login_cap_t *lc;
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_decl(set_loginclass, SUDO_DEBUG_PLUGIN)
|
2000-03-24 20:13:12 +00:00
|
|
|
|
2011-12-02 14:29:54 -05:00
|
|
|
if (!def_use_loginclass)
|
|
|
|
debug_return;
|
|
|
|
|
2000-03-07 04:29:46 +00:00
|
|
|
if (login_class && strcmp(login_class, "-") != 0) {
|
2013-12-07 09:17:54 -07:00
|
|
|
if (user_uid != 0 && pw->pw_uid != 0)
|
2013-11-18 08:59:57 -07:00
|
|
|
fatalx(U_("only root can use `-c %s'"), login_class);
|
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
|
|
|
/*
|
|
|
|
* Don't make it a fatal error if the user didn't specify the login
|
|
|
|
* class themselves. We do this because if login.conf gets
|
|
|
|
* corrupted we want the admin to be able to use sudo to fix it.
|
|
|
|
*/
|
|
|
|
if (login_class)
|
2012-11-08 15:37:44 -05:00
|
|
|
log_fatal(errflags, N_("unknown login class: %s"), login_class);
|
2012-03-26 10:59:14 -04:00
|
|
|
else
|
2013-04-18 14:14:03 -04:00
|
|
|
log_warning(errflags, N_("unknown login class: %s"), login_class);
|
2011-12-02 14:29:54 -05:00
|
|
|
def_use_loginclass = false;
|
2001-08-23 21:43:38 +00:00
|
|
|
}
|
2011-12-08 17:17:25 -05:00
|
|
|
login_close(lc);
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_return;
|
2000-03-07 04:29:46 +00:00
|
|
|
}
|
|
|
|
#else
|
2000-10-27 22:41:48 +00:00
|
|
|
static void
|
2010-04-22 18:09:53 -04:00
|
|
|
set_loginclass(struct passwd *pw)
|
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
|
|
|
|
|
1999-12-02 20:31:25 +00:00
|
|
|
/*
|
|
|
|
* Look up the fully qualified domain name and set user_host and user_shost.
|
2012-08-15 09:52:26 -04:00
|
|
|
* Use AI_FQDN if available since "canonical" is not always the same as fqdn.
|
1999-12-02 20:31:25 +00:00
|
|
|
*/
|
2012-10-25 16:58:31 -04:00
|
|
|
static void
|
2010-03-14 19:58:47 -04:00
|
|
|
set_fqdn(void)
|
1999-12-02 20:31:25 +00:00
|
|
|
{
|
2007-08-15 13:22:06 +00:00
|
|
|
struct addrinfo *res0, hint;
|
1999-12-02 20:31:25 +00:00
|
|
|
char *p;
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_decl(set_fqdn, SUDO_DEBUG_PLUGIN)
|
1999-12-02 20:31:25 +00:00
|
|
|
|
2013-08-03 08:30:06 -06:00
|
|
|
memset(&hint, 0, sizeof(hint));
|
2007-08-15 13:22:06 +00:00
|
|
|
hint.ai_family = PF_UNSPEC;
|
2012-08-15 09:52:26 -04:00
|
|
|
hint.ai_flags = AI_FQDN;
|
2007-08-15 13:22:06 +00:00
|
|
|
if (getaddrinfo(user_host, NULL, &hint, &res0) != 0) {
|
2013-04-18 14:14:03 -04:00
|
|
|
log_warning(MSG_ONLY, N_("unable to resolve host %s"), user_host);
|
2002-01-15 01:53:02 +00:00
|
|
|
} else {
|
|
|
|
if (user_shost != user_host)
|
2005-03-29 14:29:47 +00:00
|
|
|
efree(user_shost);
|
|
|
|
efree(user_host);
|
2007-08-15 13:22:06 +00:00
|
|
|
user_host = estrdup(res0->ai_canonname);
|
|
|
|
freeaddrinfo(res0);
|
2012-08-15 09:52:26 -04:00
|
|
|
if ((p = strchr(user_host, '.')) != NULL)
|
|
|
|
user_shost = estrndup(user_host, (size_t)(p - user_host));
|
|
|
|
else
|
|
|
|
user_shost = user_host;
|
1999-12-02 20:31:25 +00:00
|
|
|
}
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_return;
|
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
|
|
|
*/
|
2011-10-22 14:40:21 -04:00
|
|
|
static void
|
2011-08-09 14:54:42 -04:00
|
|
|
set_runaspw(const char *user)
|
2004-01-16 23:09:20 +00:00
|
|
|
{
|
2013-12-05 15:51:56 -07:00
|
|
|
struct passwd *pw = NULL;
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_decl(set_runaspw, SUDO_DEBUG_PLUGIN)
|
|
|
|
|
2004-01-16 23:09:20 +00:00
|
|
|
if (*user == '#') {
|
2013-12-05 15:51:56 -07:00
|
|
|
const char *errstr;
|
|
|
|
uid_t uid = atoid(user + 1, NULL, NULL, &errstr);
|
|
|
|
if (errstr == NULL) {
|
|
|
|
if ((pw = sudo_getpwuid(uid)) == NULL)
|
|
|
|
pw = sudo_fakepwnam(user, runas_gr ? runas_gr->gr_gid : 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pw == NULL) {
|
|
|
|
if ((pw = sudo_getpwnam(user)) == NULL)
|
2012-11-08 15:37:44 -05:00
|
|
|
log_fatal(NO_MAIL|MSG_ONLY, N_("unknown user: %s"), user);
|
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;
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_return;
|
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
|
|
|
*/
|
2011-08-10 09:02:37 -04:00
|
|
|
static void
|
2011-08-09 14:54:42 -04:00
|
|
|
set_runasgr(const char *group)
|
2007-11-21 20:12:00 +00:00
|
|
|
{
|
2013-12-05 15:51:56 -07:00
|
|
|
struct group *gr = NULL;
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_decl(set_runasgr, SUDO_DEBUG_PLUGIN)
|
|
|
|
|
2007-11-21 20:12:00 +00:00
|
|
|
if (*group == '#') {
|
2013-12-05 15:51:56 -07:00
|
|
|
const char *errstr;
|
|
|
|
gid_t gid = atoid(group + 1, NULL, NULL, &errstr);
|
|
|
|
if (errstr == NULL) {
|
|
|
|
if ((gr = sudo_getgrgid(gid)) == NULL)
|
|
|
|
gr = sudo_fakegrnam(group);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (gr == NULL) {
|
|
|
|
if ((gr = sudo_getgrnam(group)) == NULL)
|
2012-11-08 15:37:44 -05:00
|
|
|
log_fatal(NO_MAIL|MSG_ONLY, N_("unknown group: %s"), group);
|
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;
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_return;
|
2011-08-10 09:02:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Callback for runas_default sudoers setting.
|
|
|
|
*/
|
2014-04-04 15:30:12 -06:00
|
|
|
static bool
|
2011-08-10 09:02:37 -04:00
|
|
|
cb_runas_default(const char *user)
|
|
|
|
{
|
|
|
|
/* Only reset runaspw if user didn't specify one. */
|
|
|
|
if (!runas_user && !runas_group)
|
|
|
|
set_runaspw(user);
|
2011-12-02 11:27:33 -05:00
|
|
|
return true;
|
2007-11-21 20:12:00 +00:00
|
|
|
}
|
|
|
|
|
2012-11-25 09:33:52 -05:00
|
|
|
/*
|
|
|
|
* Callback for sudoers_locale sudoers setting.
|
|
|
|
*/
|
2014-04-04 15:30:12 -06:00
|
|
|
static bool
|
2012-11-25 09:33:52 -05:00
|
|
|
cb_sudoers_locale(const char *locale)
|
|
|
|
{
|
|
|
|
sudoers_initlocale(NULL, locale);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-11-15 15:53:53 +00:00
|
|
|
/*
|
2013-04-26 16:06:05 -04:00
|
|
|
* Cleanup hook for fatal()/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;
|
2012-11-25 09:34:40 -05:00
|
|
|
debug_decl(sudoers_cleanup, SUDO_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();
|
|
|
|
sudo_endpwent();
|
|
|
|
sudo_endgrent();
|
|
|
|
|
|
|
|
debug_return;
|
2004-11-15 15:53:53 +00:00
|
|
|
}
|
|
|
|
|
2010-05-13 14:09:21 -04:00
|
|
|
static char *
|
2012-10-24 14:24:36 -04:00
|
|
|
resolve_editor(const char *ed, size_t edlen, int nfiles, char **files, char ***argv_out)
|
2010-05-13 14:09:21 -04:00
|
|
|
{
|
2012-10-24 14:24:36 -04:00
|
|
|
char *cp, **nargv, *editor, *editor_path = NULL;
|
2011-12-02 11:27:33 -05:00
|
|
|
int ac, i, nargc;
|
|
|
|
bool wasblank;
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_decl(resolve_editor, SUDO_DEBUG_PLUGIN)
|
2010-05-13 14:09:21 -04:00
|
|
|
|
2012-10-24 14:24:36 -04:00
|
|
|
/* Note: editor becomes part of argv_out and is not freed. */
|
|
|
|
editor = emalloc(edlen + 1);
|
|
|
|
memcpy(editor, ed, edlen);
|
|
|
|
editor[edlen] = '\0';
|
2010-06-04 12:17:19 -04:00
|
|
|
|
2010-05-13 14:09:21 -04:00
|
|
|
/*
|
|
|
|
* Split editor into an argument vector; editor is reused (do not free).
|
|
|
|
* The EDITOR and VISUAL environment variables may contain command
|
|
|
|
* line args so look for those and alloc space for them too.
|
|
|
|
*/
|
|
|
|
nargc = 1;
|
2011-12-02 11:27:33 -05:00
|
|
|
for (wasblank = false, cp = editor; *cp != '\0'; cp++) {
|
2010-05-13 14:09:21 -04:00
|
|
|
if (isblank((unsigned char) *cp))
|
2011-12-02 11:27:33 -05:00
|
|
|
wasblank = true;
|
2010-05-13 14:09:21 -04:00
|
|
|
else if (wasblank) {
|
2011-12-02 11:27:33 -05:00
|
|
|
wasblank = false;
|
2010-05-13 14:09:21 -04:00
|
|
|
nargc++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* If we can't find the editor in the user's PATH, give up. */
|
|
|
|
cp = strtok(editor, " \t");
|
|
|
|
if (cp == NULL ||
|
|
|
|
find_path(cp, &editor_path, NULL, getenv("PATH"), 0) != FOUND) {
|
2010-06-04 12:17:19 -04:00
|
|
|
efree(editor);
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_return_str(NULL);
|
2010-05-13 14:09:21 -04:00
|
|
|
}
|
2010-05-13 17:11:31 -04:00
|
|
|
nargv = (char **) emalloc2(nargc + 1 + nfiles + 1, sizeof(char *));
|
2010-05-13 14:09:21 -04:00
|
|
|
for (ac = 0; cp != NULL && ac < nargc; ac++) {
|
|
|
|
nargv[ac] = cp;
|
|
|
|
cp = strtok(NULL, " \t");
|
|
|
|
}
|
2010-05-13 17:11:31 -04:00
|
|
|
nargv[ac++] = "--";
|
|
|
|
for (i = 0; i < nfiles; )
|
|
|
|
nargv[ac++] = files[i++];
|
2010-05-13 14:09:21 -04:00
|
|
|
nargv[ac] = NULL;
|
|
|
|
|
|
|
|
*argv_out = nargv;
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_return_str(editor_path);
|
2010-05-13 14:09:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine which editor to use. We don't need to worry about restricting
|
|
|
|
* this to a "safe" editor since it runs with the uid of the invoking user,
|
|
|
|
* not the runas (privileged) user.
|
|
|
|
*/
|
|
|
|
static char *
|
2010-05-13 17:11:31 -04:00
|
|
|
find_editor(int nfiles, char **files, char ***argv_out)
|
2010-05-13 14:09:21 -04:00
|
|
|
{
|
2012-10-24 14:24:36 -04:00
|
|
|
const char *cp, *ep, *editor;
|
|
|
|
char *editor_path = NULL, **ev, *ev0[4];
|
|
|
|
size_t len;
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_decl(find_editor, SUDO_DEBUG_PLUGIN)
|
2010-05-13 14:09:21 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If any of SUDO_EDITOR, VISUAL or EDITOR are set, choose the first one.
|
|
|
|
*/
|
|
|
|
ev0[0] = "SUDO_EDITOR";
|
|
|
|
ev0[1] = "VISUAL";
|
|
|
|
ev0[2] = "EDITOR";
|
|
|
|
ev0[3] = NULL;
|
2012-10-24 14:24:36 -04:00
|
|
|
for (ev = ev0; editor_path == NULL && *ev != NULL; ev++) {
|
2010-05-13 14:09:21 -04:00
|
|
|
if ((editor = getenv(*ev)) != NULL && *editor != '\0') {
|
2012-10-24 14:24:36 -04:00
|
|
|
editor_path = resolve_editor(editor, strlen(editor), nfiles,
|
|
|
|
files, argv_out);
|
2010-05-13 14:09:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (editor_path == NULL) {
|
2012-10-24 14:24:36 -04:00
|
|
|
/* def_editor could be a path, split it up, avoiding strtok() */
|
|
|
|
cp = editor = def_editor;
|
|
|
|
do {
|
|
|
|
if ((ep = strchr(cp, ':')) != NULL)
|
|
|
|
len = ep - cp;
|
|
|
|
else
|
|
|
|
len = strlen(cp);
|
|
|
|
editor_path = resolve_editor(cp, len, nfiles, files, argv_out);
|
|
|
|
cp = ep + 1;
|
|
|
|
} while (ep != NULL && editor_path == NULL);
|
2010-05-13 14:09:21 -04:00
|
|
|
}
|
|
|
|
if (!editor_path) {
|
2012-11-08 15:37:43 -05:00
|
|
|
audit_failure(NewArgv, N_("%s: command not found"), editor);
|
2013-11-18 08:59:57 -07:00
|
|
|
warningx(U_("%s: command not found"), editor);
|
2010-05-13 14:09:21 -04:00
|
|
|
}
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_return_str(editor_path);
|
2010-05-13 14:09:21 -04:00
|
|
|
}
|
|
|
|
|
2010-07-28 16:34:11 -04:00
|
|
|
#ifdef USE_ADMIN_FLAG
|
|
|
|
static void
|
|
|
|
create_admin_success_flag(void)
|
|
|
|
{
|
|
|
|
struct stat statbuf;
|
|
|
|
char flagfile[PATH_MAX];
|
|
|
|
int fd, n;
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_decl(create_admin_success_flag, SUDO_DEBUG_PLUGIN)
|
2010-07-28 16:34:11 -04:00
|
|
|
|
|
|
|
/* Check whether the user is in the admin group. */
|
|
|
|
if (!user_in_group(sudo_user.pw, "admin"))
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_return;
|
2010-07-28 16:34:11 -04:00
|
|
|
|
|
|
|
/* Build path to flag file. */
|
|
|
|
n = snprintf(flagfile, sizeof(flagfile), "%s/.sudo_as_admin_successful",
|
|
|
|
user_dir);
|
2013-10-23 15:19:41 -06:00
|
|
|
if (n <= 0 || (size_t)n >= sizeof(flagfile))
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_return;
|
2010-07-28 16:34:11 -04:00
|
|
|
|
|
|
|
/* Create admin flag file if it doesn't already exist. */
|
|
|
|
set_perms(PERM_USER);
|
2010-11-23 07:34:22 -05:00
|
|
|
if (stat(flagfile, &statbuf) != 0) {
|
|
|
|
fd = open(flagfile, O_CREAT|O_WRONLY|O_EXCL, 0644);
|
|
|
|
close(fd);
|
2010-07-28 16:34:11 -04:00
|
|
|
}
|
2010-11-23 07:34:22 -05:00
|
|
|
restore_perms();
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_return;
|
2010-07-28 16:34:11 -04:00
|
|
|
}
|
|
|
|
#else /* !USE_ADMIN_FLAG */
|
|
|
|
static void
|
|
|
|
create_admin_success_flag(void)
|
|
|
|
{
|
|
|
|
/* STUB */
|
|
|
|
}
|
|
|
|
#endif /* USE_ADMIN_FLAG */
|
2013-04-01 10:40:17 -04:00
|
|
|
|
|
|
|
static bool
|
|
|
|
tty_present(void)
|
|
|
|
{
|
|
|
|
#if defined(HAVE_STRUCT_KINFO_PROC2_P_TDEV) || defined(HAVE_STRUCT_KINFO_PROC_P_TDEV) || defined(HAVE_STRUCT_KINFO_PROC_KI_TDEV) || defined(HAVE_STRUCT_KINFO_PROC_KP_EPROC_E_TDEV) || defined(HAVE_STRUCT_PSINFO_PR_TTYDEV) || defined(HAVE_PSTAT_GETPROC) || defined(__linux__)
|
|
|
|
return user_ttypath != NULL;
|
|
|
|
#else
|
|
|
|
int fd = open(_PATH_TTY, O_RDWR|O_NOCTTY);
|
|
|
|
if (fd != -1)
|
|
|
|
close(fd);
|
|
|
|
return fd != -1;
|
|
|
|
#endif
|
|
|
|
}
|