1993-11-27 23:46:33 +00:00
|
|
|
/*
|
2019-04-29 07:21:51 -06:00
|
|
|
* SPDX-License-Identifier: ISC
|
|
|
|
*
|
2023-03-20 18:27:27 -06:00
|
|
|
* Copyright (c) 1996, 1998-2005, 2007-2023
|
2017-12-03 17:53:40 -07:00
|
|
|
* Todd C. Miller <Todd.Miller@sudo.ws>
|
1993-03-02 16:35:47 +00:00
|
|
|
*
|
2004-02-13 21:36:44 +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.
|
1999-07-31 16:19:50 +00:00
|
|
|
*
|
2004-02-13 21:36:44 +00:00
|
|
|
* 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.
|
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.
|
1999-07-22 12:57:47 +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
|
|
|
|
1999-07-22 12:57:47 +00:00
|
|
|
/*
|
|
|
|
* Lock the sudoers file for safe editing (ala vipw) and check for parse errors.
|
1993-10-04 19:23:33 +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:39:35 +00:00
|
|
|
|
2001-12-14 19:52:54 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2012-11-25 09:34:40 -05:00
|
|
|
#include <sys/uio.h>
|
2004-05-17 20:08:46 +00:00
|
|
|
#ifndef __TANDEM
|
|
|
|
# include <sys/file.h>
|
|
|
|
#endif
|
2001-12-14 19:52:54 +00:00
|
|
|
#include <sys/wait.h>
|
1993-03-02 16:35:47 +00:00
|
|
|
#include <stdio.h>
|
2015-06-19 14:29:27 -06:00
|
|
|
#include <stdlib.h>
|
2020-05-18 07:59:24 -06:00
|
|
|
#include <string.h>
|
1994-03-09 22:36:55 +00:00
|
|
|
#include <unistd.h>
|
2011-05-17 16:38:40 -04:00
|
|
|
#include <stdarg.h>
|
1995-03-28 05:49:00 +00:00
|
|
|
#include <ctype.h>
|
1994-11-10 00:46:21 +00:00
|
|
|
#include <pwd.h>
|
2004-11-16 04:24:11 +00:00
|
|
|
#include <grp.h>
|
1995-03-27 22:04:19 +00:00
|
|
|
#include <signal.h>
|
1993-03-02 16:35:47 +00:00
|
|
|
#include <errno.h>
|
1995-03-31 22:27:59 +00:00
|
|
|
#include <fcntl.h>
|
2018-01-17 09:52:15 -07:00
|
|
|
#include <time.h>
|
2004-10-26 22:26:28 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2020-08-12 10:07:07 -06:00
|
|
|
#ifdef HAVE_GETOPT_LONG
|
|
|
|
# include <getopt.h>
|
|
|
|
# else
|
|
|
|
# include "compat/getopt.h"
|
|
|
|
#endif /* HAVE_GETOPT_LONG */
|
1993-03-02 16:35:47 +00:00
|
|
|
|
2010-03-14 19:58:47 -04:00
|
|
|
#include "sudoers.h"
|
2017-01-07 19:50:05 -07:00
|
|
|
#include "interfaces.h"
|
2009-04-05 16:25:04 +00:00
|
|
|
#include "redblack.h"
|
2011-04-05 11:47:31 -04:00
|
|
|
#include "sudoers_version.h"
|
2012-01-06 10:58:13 -05:00
|
|
|
#include "sudo_conf.h"
|
2011-11-12 12:18:44 -05:00
|
|
|
#include <gram.h>
|
1993-03-02 16:35:47 +00:00
|
|
|
|
2004-09-29 18:41:38 +00:00
|
|
|
struct sudoersfile {
|
2013-10-22 09:00:37 -06:00
|
|
|
TAILQ_ENTRY(sudoersfile) entries;
|
2023-05-02 10:45:56 -06:00
|
|
|
char *opath; /* original path we opened */
|
|
|
|
char *dpath; /* destination path to write to */
|
|
|
|
char *tpath; /* editor temporary file path */
|
|
|
|
bool modified; /* true if the user modified the file */
|
|
|
|
bool doedit; /* true when editing (not just checking) sudoers */
|
|
|
|
int fd; /* fd of the original file (if it exists) */
|
|
|
|
int errorline; /* line number when there is a syntax error */
|
2004-09-29 18:41:38 +00:00
|
|
|
};
|
2013-10-22 09:00:37 -06:00
|
|
|
TAILQ_HEAD(sudoersfile_list, sudoersfile);
|
2004-09-29 18:41:38 +00:00
|
|
|
|
1995-03-27 18:26:36 +00:00
|
|
|
/*
|
|
|
|
* Function prototypes
|
|
|
|
*/
|
2010-05-21 18:40:04 -04:00
|
|
|
static void quit(int);
|
2012-01-06 10:58:13 -05:00
|
|
|
static int whatnow(void);
|
2015-06-18 09:51:36 -06:00
|
|
|
static char *get_editor(int *editor_argc, char ***editor_argv);
|
2021-11-06 12:37:13 -06:00
|
|
|
static bool check_syntax(const char *, bool, bool, bool, bool);
|
2015-06-18 09:51:36 -06:00
|
|
|
static bool edit_sudoers(struct sudoersfile *, char *, int, char **, int);
|
2021-11-06 12:37:13 -06:00
|
|
|
static bool install_sudoers(struct sudoersfile *, bool, bool);
|
2022-09-07 07:48:31 -06:00
|
|
|
static bool visudo_track_error(const char *file, int line, int column, const char *fmt, va_list args) sudo_printf0like(4, 0);
|
2018-08-24 09:52:53 -06:00
|
|
|
static int print_unused(struct sudoers_parse_tree *, struct alias *, void *);
|
2015-06-18 09:51:36 -06:00
|
|
|
static bool reparse_sudoers(char *, int, char **, bool, bool);
|
2023-02-21 16:14:14 -07:00
|
|
|
static int run_command(const char *, char *const *, bool);
|
2018-01-28 16:11:02 -07:00
|
|
|
static void parse_sudoers_options(void);
|
2010-03-17 19:56:27 -04:00
|
|
|
static void setup_signals(void);
|
2012-11-25 09:34:40 -05:00
|
|
|
static void visudo_cleanup(void);
|
2023-05-02 10:37:38 -06:00
|
|
|
sudo_noreturn static void export_sudoers(const char *infile, const char *outfile);
|
2022-12-01 12:54:53 -07:00
|
|
|
sudo_noreturn static void help(void);
|
|
|
|
sudo_noreturn static void usage(void);
|
2010-11-19 12:58:03 -05:00
|
|
|
|
2018-01-26 12:51:24 -07:00
|
|
|
extern void get_hostname(void);
|
1995-03-27 18:26:36 +00:00
|
|
|
|
1994-05-24 16:02:23 +00:00
|
|
|
/*
|
|
|
|
* Globals
|
|
|
|
*/
|
1999-07-22 12:57:47 +00:00
|
|
|
struct sudo_user sudo_user;
|
2005-01-01 17:41:21 +00:00
|
|
|
struct passwd *list_pw;
|
2023-05-02 10:47:47 -06:00
|
|
|
static const char *path_sudoers = _PATH_SUDOERS;
|
2013-10-22 09:00:37 -06:00
|
|
|
static struct sudoersfile_list sudoerslist = TAILQ_HEAD_INITIALIZER(sudoerslist);
|
2023-05-09 07:29:06 -06:00
|
|
|
static struct sudoers_parser_config sudoers_conf = SUDOERS_PARSER_CONFIG_INITIALIZER;
|
2012-01-06 10:54:30 -05:00
|
|
|
static bool checkonly;
|
2022-10-04 16:11:45 -06:00
|
|
|
static bool edit_includes = true;
|
2022-03-10 13:30:56 -07:00
|
|
|
static unsigned int errors;
|
2022-10-04 16:11:45 -06:00
|
|
|
static const char short_opts[] = "cf:hIOPqsVx:";
|
2013-07-19 09:42:25 -06:00
|
|
|
static struct option long_opts[] = {
|
|
|
|
{ "check", no_argument, NULL, 'c' },
|
2013-12-16 14:32:42 -07:00
|
|
|
{ "export", required_argument, NULL, 'x' },
|
2013-07-19 09:42:25 -06:00
|
|
|
{ "file", required_argument, NULL, 'f' },
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
2022-10-04 16:11:45 -06:00
|
|
|
{ "no-includes", no_argument, NULL, 'I' },
|
2021-11-06 12:37:13 -06:00
|
|
|
{ "owner", no_argument, NULL, 'O' },
|
|
|
|
{ "perms", no_argument, NULL, 'P' },
|
2013-07-19 09:42:25 -06:00
|
|
|
{ "quiet", no_argument, NULL, 'q' },
|
|
|
|
{ "strict", no_argument, NULL, 's' },
|
|
|
|
{ "version", no_argument, NULL, 'V' },
|
|
|
|
{ NULL, no_argument, NULL, '\0' },
|
|
|
|
};
|
2004-09-28 18:33:06 +00:00
|
|
|
|
2020-08-12 09:57:42 -06:00
|
|
|
sudo_dso_public int main(int argc, char *argv[]);
|
2012-10-02 15:08:02 -04:00
|
|
|
|
1999-04-10 04:49:03 +00:00
|
|
|
int
|
2010-04-22 18:09:53 -04:00
|
|
|
main(int argc, char *argv[])
|
1993-03-02 16:35:47 +00:00
|
|
|
{
|
2004-09-29 18:41:38 +00:00
|
|
|
struct sudoersfile *sp;
|
2016-07-31 18:47:36 -06:00
|
|
|
char *editor, **editor_argv;
|
2018-01-28 16:34:16 -07:00
|
|
|
const char *export_path = NULL;
|
2016-07-20 16:41:23 -06:00
|
|
|
int ch, oldlocale, editor_argc, exitcode = 0;
|
2021-11-06 12:37:13 -06:00
|
|
|
bool use_perms, use_owner, quiet, strict, fflag;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(main, SUDOERS_DEBUG_MAIN);
|
2012-01-06 10:58:13 -05:00
|
|
|
|
2008-11-19 12:37:05 +00:00
|
|
|
#if defined(SUDO_DEVEL) && defined(__OpenBSD__)
|
2012-01-06 10:58:13 -05:00
|
|
|
{
|
|
|
|
extern char *malloc_options;
|
2016-11-25 09:04:00 -07:00
|
|
|
malloc_options = "S";
|
2012-01-06 10:58:13 -05:00
|
|
|
}
|
2008-11-19 12:37:05 +00:00
|
|
|
#endif
|
1999-08-17 15:29:47 +00:00
|
|
|
|
2013-12-01 19:12:21 -07:00
|
|
|
initprogname(argc > 0 ? argv[0] : "visudo");
|
2015-06-17 06:49:59 -06:00
|
|
|
if (!sudoers_initlocale(setlocale(LC_ALL, ""), def_sudoers_locale))
|
2015-06-19 14:51:17 -06:00
|
|
|
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
|
2016-07-20 16:41:23 -06:00
|
|
|
sudo_warn_set_locale_func(sudoers_warn_setlocale);
|
2011-05-17 16:38:40 -04:00
|
|
|
bindtextdomain("sudoers", LOCALEDIR); /* XXX - should have visudo domain */
|
|
|
|
textdomain("sudoers");
|
|
|
|
|
2010-05-30 06:35:21 -04:00
|
|
|
if (argc < 1)
|
2022-09-07 07:48:31 -06:00
|
|
|
usage();
|
1993-10-04 19:23:33 +00:00
|
|
|
|
2013-04-26 16:06:05 -04:00
|
|
|
/* Register fatal/fatalx callback. */
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_fatal_callback_register(visudo_cleanup);
|
2012-11-25 09:34:40 -05:00
|
|
|
|
2016-07-20 14:16:00 -06:00
|
|
|
/* Set sudoers locale callback. */
|
|
|
|
sudo_defs_table[I_SUDOERS_LOCALE].callback = sudoers_locale_callback;
|
|
|
|
|
2014-11-10 20:12:47 -07:00
|
|
|
/* Read debug and plugin sections of sudo.conf. */
|
2015-07-07 13:17:50 -06:00
|
|
|
if (sudo_conf_read(NULL, SUDO_CONF_DEBUG|SUDO_CONF_PLUGINS) == -1)
|
|
|
|
exit(EXIT_FAILURE);
|
2014-11-10 20:12:47 -07:00
|
|
|
|
2014-10-26 08:33:08 -06:00
|
|
|
/* Initialize the debug subsystem. */
|
2016-11-21 06:37:23 -10:00
|
|
|
if (!sudoers_debug_register(getprogname(), sudo_conf_debug_files(getprogname())))
|
|
|
|
exit(EXIT_FAILURE);
|
2014-10-23 09:40:36 -06:00
|
|
|
|
2014-11-10 20:12:47 -07:00
|
|
|
/* Parse sudoers plugin options, if any. */
|
|
|
|
parse_sudoers_options();
|
|
|
|
|
1993-10-04 19:23:33 +00:00
|
|
|
/*
|
1999-08-17 15:29:47 +00:00
|
|
|
* Arg handling.
|
1993-10-04 19:23:33 +00:00
|
|
|
*/
|
2021-11-06 12:37:13 -06:00
|
|
|
fflag = quiet = strict = use_owner = use_perms = false;
|
2013-07-19 09:42:25 -06:00
|
|
|
while ((ch = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
|
2001-12-12 00:41:33 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'V':
|
2013-07-19 09:42:25 -06:00
|
|
|
(void) printf(_("%s version %s\n"), getprogname(),
|
|
|
|
PACKAGE_VERSION);
|
|
|
|
(void) printf(_("%s grammar version %d\n"), getprogname(),
|
|
|
|
SUDOERS_GRAMMAR_VERSION);
|
2012-01-06 10:58:13 -05:00
|
|
|
goto done;
|
2001-12-12 00:41:33 +00:00
|
|
|
case 'c':
|
2012-08-02 15:40:11 -04:00
|
|
|
checkonly = true; /* check mode */
|
2001-12-12 00:41:33 +00:00
|
|
|
break;
|
|
|
|
case 'f':
|
2023-05-02 10:47:47 -06:00
|
|
|
path_sudoers = optarg;
|
2018-12-24 08:26:18 -07:00
|
|
|
fflag = true;
|
2001-12-12 00:41:33 +00:00
|
|
|
break;
|
2011-02-21 11:33:36 -05:00
|
|
|
case 'h':
|
|
|
|
help();
|
|
|
|
break;
|
2022-10-04 16:11:45 -06:00
|
|
|
case 'I':
|
|
|
|
edit_includes = false;
|
|
|
|
break;
|
2021-11-06 12:37:13 -06:00
|
|
|
case 'O':
|
|
|
|
use_owner = true; /* check/set owner */
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
use_perms = true; /* check/set perms */
|
|
|
|
break;
|
2001-12-12 00:41:33 +00:00
|
|
|
case 's':
|
2012-08-02 15:40:11 -04:00
|
|
|
strict = true; /* strict mode */
|
2001-12-12 00:41:33 +00:00
|
|
|
break;
|
|
|
|
case 'q':
|
2013-08-26 14:40:25 -06:00
|
|
|
quiet = true; /* quiet mode */
|
2001-12-12 00:41:33 +00:00
|
|
|
break;
|
2013-11-15 15:11:55 -07:00
|
|
|
case 'x':
|
2018-01-28 16:34:16 -07:00
|
|
|
export_path = optarg;
|
|
|
|
break;
|
2001-12-12 00:41:33 +00:00
|
|
|
default:
|
2022-09-07 07:48:31 -06:00
|
|
|
usage();
|
1995-03-27 18:26:36 +00:00
|
|
|
}
|
1998-11-18 03:51:10 +00:00
|
|
|
}
|
2018-12-24 08:26:18 -07:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
/* Check for optional sudoers file argument. */
|
|
|
|
switch (argc) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
/* Only accept sudoers file if no -f was specified. */
|
|
|
|
if (!fflag) {
|
2023-05-02 10:47:47 -06:00
|
|
|
path_sudoers = *argv;
|
2018-12-24 08:26:18 -07:00
|
|
|
fflag = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2022-09-07 07:48:31 -06:00
|
|
|
usage();
|
2018-12-24 08:26:18 -07:00
|
|
|
}
|
1993-10-04 19:23:33 +00:00
|
|
|
|
2022-03-11 10:44:17 -07:00
|
|
|
if (fflag) {
|
|
|
|
/* Looser owner/permission checks for an uninstalled sudoers file. */
|
|
|
|
if (!use_owner) {
|
2023-05-09 07:29:06 -06:00
|
|
|
sudoers_conf.sudoers_uid = (uid_t)-1;
|
|
|
|
sudoers_conf.sudoers_gid = (gid_t)-1;
|
2022-03-11 10:44:17 -07:00
|
|
|
}
|
|
|
|
if (!use_perms)
|
2023-05-09 07:29:06 -06:00
|
|
|
sudoers_conf.sudoers_mode |= S_IWUSR;
|
2022-03-11 10:44:17 -07:00
|
|
|
} else {
|
|
|
|
/* Check/set owner and mode for installed sudoers file. */
|
2021-11-06 12:37:13 -06:00
|
|
|
use_owner = true;
|
|
|
|
use_perms = true;
|
|
|
|
}
|
|
|
|
|
2018-01-28 16:34:16 -07:00
|
|
|
if (export_path != NULL) {
|
2020-10-30 10:15:30 -06:00
|
|
|
/* Backward compatibility for the time being. */
|
2023-05-02 10:47:47 -06:00
|
|
|
export_sudoers(path_sudoers, export_path);
|
2018-01-28 16:34:16 -07:00
|
|
|
}
|
|
|
|
|
1999-07-22 12:57:47 +00:00
|
|
|
/* Mock up a fake sudo_user struct. */
|
2023-02-21 20:01:13 -07:00
|
|
|
user_cmnd = user_base = strdup("true");
|
|
|
|
if (user_cmnd == NULL)
|
|
|
|
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
|
2016-07-31 18:47:36 -06:00
|
|
|
if (geteuid() == 0) {
|
|
|
|
const char *user = getenv("SUDO_USER");
|
|
|
|
if (user != NULL && *user != '\0')
|
|
|
|
sudo_user.pw = sudo_getpwnam(user);
|
|
|
|
}
|
2016-07-30 15:27:36 -06:00
|
|
|
if (sudo_user.pw == NULL) {
|
|
|
|
if ((sudo_user.pw = sudo_getpwuid(getuid())) == NULL)
|
|
|
|
sudo_fatalx(U_("you do not exist in the %s database"), "passwd");
|
|
|
|
}
|
2009-02-21 21:49:19 +00:00
|
|
|
get_hostname();
|
1996-04-28 01:03:30 +00:00
|
|
|
|
2022-03-10 13:30:56 -07:00
|
|
|
/* Hook the sudoers parser to track files with parse errors. */
|
|
|
|
sudoers_error_hook = visudo_track_error;
|
|
|
|
|
2000-02-16 00:03:44 +00:00
|
|
|
/* Setup defaults data structures. */
|
2015-06-17 06:49:59 -06:00
|
|
|
if (!init_defaults())
|
2020-08-12 13:45:09 -06:00
|
|
|
sudo_fatalx("%s", U_("unable to initialize sudoers default values"));
|
1993-12-03 02:35:54 +00:00
|
|
|
|
2012-01-06 10:58:13 -05:00
|
|
|
if (checkonly) {
|
2023-05-02 10:47:47 -06:00
|
|
|
exitcode = check_syntax(path_sudoers, quiet, strict, use_owner,
|
2021-11-06 12:37:13 -06:00
|
|
|
use_perms) ? 0 : 1;
|
2012-01-06 10:58:13 -05:00
|
|
|
goto done;
|
|
|
|
}
|
2000-04-01 21:23:28 +00:00
|
|
|
|
|
|
|
/*
|
2013-04-01 11:23:29 -04:00
|
|
|
* Parse the existing sudoers file(s) to highlight any existing
|
|
|
|
* errors and to pull in editor and env_editor conf values.
|
2000-04-01 21:23:28 +00:00
|
|
|
*/
|
2023-05-09 07:29:06 -06:00
|
|
|
sudoers_conf.strict = true;
|
|
|
|
sudoers_conf.verbose = quiet ? 0 : 2;
|
|
|
|
sudoers_conf.sudoers_path = path_sudoers;
|
|
|
|
init_parser(NULL, &sudoers_conf);
|
2023-05-02 10:47:47 -06:00
|
|
|
if ((sudoersin = open_sudoers(path_sudoers, &sudoers, true, NULL)) == NULL)
|
2020-02-08 12:43:00 -07:00
|
|
|
exit(EXIT_FAILURE);
|
2016-07-20 16:41:23 -06:00
|
|
|
sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale);
|
2016-05-07 04:52:21 -06:00
|
|
|
(void) sudoersparse();
|
2018-08-02 14:06:36 -06:00
|
|
|
(void) update_defaults(&parsed_policy, NULL,
|
2018-07-26 15:12:33 -06:00
|
|
|
SETDEF_GENERIC|SETDEF_HOST|SETDEF_USER, quiet);
|
2016-07-20 16:41:23 -06:00
|
|
|
sudoers_setlocale(oldlocale, NULL);
|
2000-04-01 21:23:28 +00:00
|
|
|
|
2015-06-18 09:51:36 -06:00
|
|
|
editor = get_editor(&editor_argc, &editor_argv);
|
2000-02-16 00:03:44 +00:00
|
|
|
|
2004-09-29 18:41:38 +00:00
|
|
|
/* Install signal handlers to clean up temp files if we are killed. */
|
2004-09-28 18:36:29 +00:00
|
|
|
setup_signals();
|
|
|
|
|
2004-09-29 18:41:38 +00:00
|
|
|
/* Edit the sudoers file(s) */
|
2013-10-22 09:00:37 -06:00
|
|
|
TAILQ_FOREACH(sp, &sudoerslist, entries) {
|
2009-04-18 23:25:08 +00:00
|
|
|
if (!sp->doedit)
|
|
|
|
continue;
|
2013-10-22 09:00:37 -06:00
|
|
|
if (sp != TAILQ_FIRST(&sudoerslist)) {
|
2023-05-02 10:45:56 -06:00
|
|
|
printf(_("press return to edit %s: "), sp->opath);
|
2023-05-11 18:12:54 -06:00
|
|
|
while ((ch = getchar()) != EOF && ch != '\r' && ch != '\n')
|
2004-09-29 18:41:38 +00:00
|
|
|
continue;
|
|
|
|
}
|
2015-06-18 09:51:36 -06:00
|
|
|
edit_sudoers(sp, editor, editor_argc, editor_argv, -1);
|
2004-09-28 18:33:06 +00:00
|
|
|
}
|
|
|
|
|
2013-04-24 11:55:21 -04:00
|
|
|
/*
|
|
|
|
* Check edited files for a parse error, re-edit any that fail
|
|
|
|
* and install the edited files as needed.
|
|
|
|
*/
|
2015-06-18 09:51:36 -06:00
|
|
|
if (reparse_sudoers(editor, editor_argc, editor_argv, strict, quiet)) {
|
2013-10-22 09:00:37 -06:00
|
|
|
TAILQ_FOREACH(sp, &sudoerslist, entries) {
|
2021-11-06 12:37:13 -06:00
|
|
|
if (!install_sudoers(sp, use_owner, use_perms)) {
|
2021-11-09 16:09:48 -07:00
|
|
|
if (sp->tpath != NULL) {
|
|
|
|
sudo_warnx(U_("contents of edit session left in %s"),
|
|
|
|
sp->tpath);
|
|
|
|
free(sp->tpath);
|
|
|
|
sp->tpath = NULL;
|
|
|
|
}
|
2021-11-06 12:37:13 -06:00
|
|
|
exitcode = 1;
|
|
|
|
}
|
2013-04-24 11:55:21 -04:00
|
|
|
}
|
2004-09-30 17:22:37 +00:00
|
|
|
}
|
2016-01-28 15:07:54 -07:00
|
|
|
free(editor);
|
2004-09-30 17:22:37 +00:00
|
|
|
|
2012-01-06 10:58:13 -05:00
|
|
|
done:
|
2013-11-15 15:11:55 -07:00
|
|
|
sudo_debug_exit_int(__func__, __FILE__, __LINE__, sudo_debug_subsys, exitcode);
|
2012-01-06 10:58:13 -05:00
|
|
|
exit(exitcode);
|
2004-09-28 18:33:06 +00:00
|
|
|
}
|
|
|
|
|
2022-03-10 13:30:56 -07:00
|
|
|
static bool
|
|
|
|
visudo_track_error(const char *file, int line, int column, const char *fmt,
|
|
|
|
va_list args)
|
|
|
|
{
|
|
|
|
struct sudoersfile *sp;
|
|
|
|
debug_decl(visudo_track_error, SUDOERS_DEBUG_UTIL);
|
|
|
|
|
|
|
|
TAILQ_FOREACH(sp, &sudoerslist, entries) {
|
|
|
|
if (sp->errorline > 0)
|
|
|
|
continue; /* preserve the first error */
|
|
|
|
|
2023-05-02 10:45:56 -06:00
|
|
|
if (strcmp(file, sp->opath) == 0 ||
|
2022-03-10 13:30:56 -07:00
|
|
|
(sp->tpath != NULL && strcmp(file, sp->tpath) == 0)) {
|
|
|
|
sp->errorline = line;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
errors++;
|
|
|
|
|
|
|
|
debug_return_bool(true);
|
|
|
|
}
|
|
|
|
|
2015-06-18 09:51:36 -06:00
|
|
|
static char *
|
|
|
|
get_editor(int *editor_argc, char ***editor_argv)
|
|
|
|
{
|
2020-10-30 10:15:30 -06:00
|
|
|
char *editor_path = NULL, **allowlist = NULL;
|
2023-01-12 15:55:27 -07:00
|
|
|
const char *env_editor = NULL;
|
2022-06-28 16:33:15 -06:00
|
|
|
static const char *files[] = { "+1", "sudoers" };
|
2020-10-30 10:15:30 -06:00
|
|
|
unsigned int allowlist_len = 0;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(get_editor, SUDOERS_DEBUG_UTIL);
|
2015-06-18 09:51:36 -06:00
|
|
|
|
2020-10-30 10:15:30 -06:00
|
|
|
/* Build up editor allowlist from def_editor unless env_editor is set. */
|
2015-06-18 09:51:36 -06:00
|
|
|
if (!def_env_editor) {
|
|
|
|
const char *cp, *ep;
|
|
|
|
const char *def_editor_end = def_editor + strlen(def_editor);
|
|
|
|
|
2020-10-30 10:15:30 -06:00
|
|
|
/* Count number of entries in allowlist and split into a list. */
|
2015-06-18 09:51:36 -06:00
|
|
|
for (cp = sudo_strsplit(def_editor, def_editor_end, ":", &ep);
|
|
|
|
cp != NULL; cp = sudo_strsplit(NULL, def_editor_end, ":", &ep)) {
|
2020-10-30 10:15:30 -06:00
|
|
|
allowlist_len++;
|
2015-06-18 09:51:36 -06:00
|
|
|
}
|
2020-10-30 10:15:30 -06:00
|
|
|
allowlist = reallocarray(NULL, allowlist_len + 1, sizeof(char *));
|
|
|
|
if (allowlist == NULL)
|
2015-06-19 14:51:17 -06:00
|
|
|
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
|
2020-10-30 10:15:30 -06:00
|
|
|
allowlist_len = 0;
|
2015-06-18 09:51:36 -06:00
|
|
|
for (cp = sudo_strsplit(def_editor, def_editor_end, ":", &ep);
|
|
|
|
cp != NULL; cp = sudo_strsplit(NULL, def_editor_end, ":", &ep)) {
|
2020-10-30 10:15:30 -06:00
|
|
|
allowlist[allowlist_len] = strndup(cp, (size_t)(ep - cp));
|
|
|
|
if (allowlist[allowlist_len] == NULL)
|
2015-06-19 14:51:17 -06:00
|
|
|
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
|
2020-10-30 10:15:30 -06:00
|
|
|
allowlist_len++;
|
2015-06-18 09:51:36 -06:00
|
|
|
}
|
2020-10-30 10:15:30 -06:00
|
|
|
allowlist[allowlist_len] = NULL;
|
2015-06-18 09:51:36 -06:00
|
|
|
}
|
|
|
|
|
2022-06-28 16:33:15 -06:00
|
|
|
editor_path = find_editor(2, (char **)files, editor_argc, editor_argv,
|
|
|
|
allowlist, &env_editor);
|
2015-06-18 09:51:36 -06:00
|
|
|
if (editor_path == NULL) {
|
2017-12-22 10:22:33 -07:00
|
|
|
if (def_env_editor && env_editor != NULL) {
|
|
|
|
/* We are honoring $EDITOR so this is a fatal error. */
|
2023-01-12 15:55:27 -07:00
|
|
|
if (errno == ENOENT) {
|
|
|
|
sudo_warnx(U_("specified editor (%s) doesn't exist"),
|
|
|
|
env_editor);
|
|
|
|
}
|
|
|
|
exit(EXIT_FAILURE);
|
2015-08-18 08:34:10 -06:00
|
|
|
}
|
2015-06-18 09:51:36 -06:00
|
|
|
sudo_fatalx(U_("no editor found (editor path = %s)"), def_editor);
|
2017-12-22 10:22:33 -07:00
|
|
|
}
|
2015-06-18 09:51:36 -06:00
|
|
|
|
2020-10-30 10:15:30 -06:00
|
|
|
if (allowlist != NULL) {
|
|
|
|
while (allowlist_len--)
|
|
|
|
free(allowlist[allowlist_len]);
|
|
|
|
free(allowlist);
|
2015-06-18 09:51:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
debug_return_str(editor_path);
|
|
|
|
}
|
|
|
|
|
2011-08-26 14:03:49 -04:00
|
|
|
/*
|
|
|
|
* List of editors that support the "+lineno" command line syntax.
|
|
|
|
* If an entry starts with '*' the tail end of the string is matched.
|
|
|
|
* No other wild cards are supported.
|
|
|
|
*/
|
2022-06-28 16:33:15 -06:00
|
|
|
static const char *lineno_editors[] = {
|
2011-08-26 14:03:49 -04:00
|
|
|
"ex",
|
|
|
|
"nex",
|
|
|
|
"vi",
|
|
|
|
"nvi",
|
|
|
|
"vim",
|
2022-07-31 17:20:50 +01:00
|
|
|
"nvim",
|
2011-08-26 14:03:49 -04:00
|
|
|
"elvis",
|
|
|
|
"*macs",
|
|
|
|
"mg",
|
|
|
|
"vile",
|
|
|
|
"jove",
|
|
|
|
"pico",
|
|
|
|
"nano",
|
|
|
|
"ee",
|
|
|
|
"joe",
|
|
|
|
"zile",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2015-06-18 09:51:36 -06:00
|
|
|
/*
|
|
|
|
* Check whether or not the specified editor matched lineno_editors[].
|
|
|
|
* Returns true if yes, false if no.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
editor_supports_plus(const char *editor)
|
|
|
|
{
|
2021-02-10 15:14:08 -07:00
|
|
|
const char *cp, *editor_base;
|
2022-06-28 16:33:15 -06:00
|
|
|
const char **av;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(editor_supports_plus, SUDOERS_DEBUG_UTIL);
|
2015-06-18 09:51:36 -06:00
|
|
|
|
2021-02-10 15:14:08 -07:00
|
|
|
editor_base = sudo_basename(editor);
|
2015-06-18 09:51:36 -06:00
|
|
|
if (*editor_base == 'r')
|
|
|
|
editor_base++;
|
|
|
|
|
|
|
|
for (av = lineno_editors; (cp = *av) != NULL; av++) {
|
|
|
|
/* We only handle a leading '*' wildcard. */
|
|
|
|
if (*cp == '*') {
|
|
|
|
size_t blen = strlen(editor_base);
|
|
|
|
size_t clen = strlen(++cp);
|
|
|
|
if (blen >= clen) {
|
|
|
|
if (strcmp(cp, editor_base + blen - clen) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (strcmp(cp, editor_base) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
debug_return_bool(cp != NULL);
|
|
|
|
}
|
|
|
|
|
2004-09-29 18:41:38 +00:00
|
|
|
/*
|
|
|
|
* Edit each sudoers file.
|
2011-12-02 11:27:33 -05:00
|
|
|
* Returns true on success, else false.
|
2004-09-29 18:41:38 +00:00
|
|
|
*/
|
2011-12-02 11:27:33 -05:00
|
|
|
static bool
|
2015-06-18 09:51:36 -06:00
|
|
|
edit_sudoers(struct sudoersfile *sp, char *editor, int editor_argc,
|
|
|
|
char **editor_argv, int lineno)
|
2004-09-28 18:33:06 +00:00
|
|
|
{
|
2004-09-29 18:41:38 +00:00
|
|
|
int tfd; /* sudoers temp file descriptor */
|
2011-12-02 11:27:33 -05:00
|
|
|
bool modified; /* was the file modified? */
|
2004-11-18 20:28:53 +00:00
|
|
|
int ac; /* argument count */
|
2004-09-29 18:41:38 +00:00
|
|
|
char linestr[64]; /* string version of lineno */
|
2015-03-02 13:58:50 -07:00
|
|
|
struct timespec ts, times[2]; /* time before and after edit */
|
|
|
|
struct timespec orig_mtim; /* starting mtime of sudoers file */
|
2004-09-29 18:41:38 +00:00
|
|
|
off_t orig_size; /* starting size of sudoers file */
|
2004-09-28 18:33:06 +00:00
|
|
|
struct stat sb; /* stat buffer */
|
2016-09-08 16:38:08 -06:00
|
|
|
bool ret = false; /* return value */
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(edit_sudoers, SUDOERS_DEBUG_UTIL);
|
2004-09-28 18:33:06 +00:00
|
|
|
|
2004-09-29 18:41:38 +00:00
|
|
|
if (fstat(sp->fd, &sb) == -1)
|
2023-05-02 10:45:56 -06:00
|
|
|
sudo_fatal(U_("unable to stat %s"), sp->opath);
|
2004-09-29 18:41:38 +00:00
|
|
|
orig_size = sb.st_size;
|
2015-03-02 13:58:50 -07:00
|
|
|
mtim_get(&sb, orig_mtim);
|
2004-09-29 18:41:38 +00:00
|
|
|
|
|
|
|
/* Create the temp file if needed and set timestamp. */
|
|
|
|
if (sp->tpath == NULL) {
|
2023-05-02 10:45:56 -06:00
|
|
|
if (asprintf(&sp->tpath, "%s.tmp", sp->dpath) == -1)
|
2015-06-19 14:51:17 -06:00
|
|
|
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
|
2016-11-07 13:36:05 -07:00
|
|
|
tfd = open(sp->tpath, O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU|S_IRUSR);
|
2004-09-29 18:41:38 +00:00
|
|
|
if (tfd < 0)
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_fatal("%s", sp->tpath);
|
2004-09-29 18:41:38 +00:00
|
|
|
|
2023-05-02 10:45:56 -06:00
|
|
|
/* Copy sp->opath -> sp->tpath and reset the mtime. */
|
2004-09-29 18:41:38 +00:00
|
|
|
if (orig_size != 0) {
|
2015-08-18 08:57:53 -06:00
|
|
|
char buf[4096], lastch = '\0';
|
|
|
|
ssize_t nread;
|
|
|
|
|
2004-09-29 18:41:38 +00:00
|
|
|
(void) lseek(sp->fd, (off_t)0, SEEK_SET);
|
2015-08-18 08:57:53 -06:00
|
|
|
while ((nread = read(sp->fd, buf, sizeof(buf))) > 0) {
|
2004-11-18 20:28:53 +00:00
|
|
|
if (write(tfd, buf, nread) != nread)
|
2020-08-12 13:45:09 -06:00
|
|
|
sudo_fatal("%s", U_("write error"));
|
2015-08-18 08:57:53 -06:00
|
|
|
lastch = buf[nread - 1];
|
|
|
|
}
|
2004-09-29 18:41:38 +00:00
|
|
|
|
|
|
|
/* Add missing newline at EOF if needed. */
|
2015-08-18 08:57:53 -06:00
|
|
|
if (lastch != '\n') {
|
|
|
|
lastch = '\n';
|
|
|
|
if (write(tfd, &lastch, 1) != 1)
|
2020-08-12 13:45:09 -06:00
|
|
|
sudo_fatal("%s", U_("write error"));
|
2004-09-29 18:41:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
(void) close(tfd);
|
|
|
|
}
|
2014-06-26 15:51:15 -06:00
|
|
|
times[0].tv_sec = times[1].tv_sec = orig_mtim.tv_sec;
|
2015-03-02 13:58:50 -07:00
|
|
|
times[0].tv_nsec = times[1].tv_nsec = orig_mtim.tv_nsec;
|
|
|
|
(void) utimensat(AT_FDCWD, sp->tpath, times, 0);
|
2004-09-29 18:41:38 +00:00
|
|
|
|
2015-06-18 09:51:36 -06:00
|
|
|
/* Disable +lineno if editor doesn't support it. */
|
|
|
|
if (lineno > 0 && !editor_supports_plus(editor))
|
2015-08-18 08:57:53 -06:00
|
|
|
lineno = -1;
|
2004-11-18 20:28:53 +00:00
|
|
|
|
2015-06-18 09:51:36 -06:00
|
|
|
/*
|
2015-09-15 09:29:40 -06:00
|
|
|
* The last 3 slots in the editor argv are: "-- +1 sudoers"
|
2015-06-18 09:51:36 -06:00
|
|
|
* Replace those placeholders with the real values.
|
|
|
|
*/
|
2015-09-15 09:29:40 -06:00
|
|
|
ac = editor_argc - 3;
|
2004-09-29 18:41:38 +00:00
|
|
|
if (lineno > 0) {
|
2015-06-18 09:51:36 -06:00
|
|
|
(void)snprintf(linestr, sizeof(linestr), "+%d", lineno);
|
2021-01-06 14:27:09 -07:00
|
|
|
editor_argv[ac++] = linestr; // -V507
|
2004-09-29 18:41:38 +00:00
|
|
|
}
|
2022-06-28 16:33:15 -06:00
|
|
|
editor_argv[ac++] = (char *)"--";
|
2015-06-18 09:51:36 -06:00
|
|
|
editor_argv[ac++] = sp->tpath;
|
2022-11-22 11:18:24 -07:00
|
|
|
editor_argv[ac] = NULL;
|
2004-09-28 18:33:06 +00:00
|
|
|
|
|
|
|
/*
|
2004-09-29 18:41:38 +00:00
|
|
|
* Do the edit:
|
|
|
|
* We cannot check the editor's exit value against 0 since
|
|
|
|
* XPG4 specifies that vi's exit value is a function of the
|
|
|
|
* number of errors during editing (?!?!).
|
2004-09-28 18:33:06 +00:00
|
|
|
*/
|
2015-03-02 13:58:50 -07:00
|
|
|
if (sudo_gettime_real(×[0]) == -1) {
|
2020-08-12 13:45:09 -06:00
|
|
|
sudo_warn("%s", U_("unable to read the clock"));
|
2015-02-25 07:10:25 -07:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2023-02-21 16:14:14 -07:00
|
|
|
if (run_command(editor, editor_argv, true) != -1) {
|
2015-03-02 13:58:50 -07:00
|
|
|
if (sudo_gettime_real(×[1]) == -1) {
|
2020-08-12 13:45:09 -06:00
|
|
|
sudo_warn("%s", U_("unable to read the clock"));
|
2015-02-25 07:10:25 -07:00
|
|
|
goto done;
|
|
|
|
}
|
2004-09-29 18:41:38 +00:00
|
|
|
/*
|
2020-10-30 10:15:30 -06:00
|
|
|
* Check for zero length sudoers file.
|
2004-09-29 18:41:38 +00:00
|
|
|
*/
|
2021-11-06 12:37:13 -06:00
|
|
|
if (stat(sp->tpath, &sb) == -1) {
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_warnx(U_("unable to stat temporary file (%s), %s unchanged"),
|
2023-05-02 10:45:56 -06:00
|
|
|
sp->tpath, sp->opath);
|
2012-01-06 10:58:13 -05:00
|
|
|
goto done;
|
2004-09-28 18:33:06 +00:00
|
|
|
}
|
2004-09-29 18:41:38 +00:00
|
|
|
if (sb.st_size == 0 && orig_size != 0) {
|
2015-05-27 13:34:28 -06:00
|
|
|
/* Avoid accidental zeroing of main sudoers file. */
|
|
|
|
if (sp == TAILQ_FIRST(&sudoerslist)) {
|
|
|
|
sudo_warnx(U_("zero length temporary file (%s), %s unchanged"),
|
2023-05-02 10:45:56 -06:00
|
|
|
sp->tpath, sp->opath);
|
2015-05-27 13:34:28 -06:00
|
|
|
goto done;
|
|
|
|
}
|
2004-09-28 18:33:06 +00:00
|
|
|
}
|
2004-09-29 18:41:38 +00:00
|
|
|
} else {
|
2023-05-02 10:45:56 -06:00
|
|
|
sudo_warnx(U_("editor (%s) failed, %s unchanged"), editor, sp->opath);
|
2012-01-06 10:58:13 -05:00
|
|
|
goto done;
|
2004-09-29 18:41:38 +00:00
|
|
|
}
|
|
|
|
|
2014-01-30 15:51:59 -07:00
|
|
|
/* Set modified bit if the user changed the file. */
|
2011-12-02 11:27:33 -05:00
|
|
|
modified = true;
|
2015-03-02 13:58:50 -07:00
|
|
|
mtim_get(&sb, ts);
|
|
|
|
if (orig_size == sb.st_size && sudo_timespeccmp(&orig_mtim, &ts, ==)) {
|
2004-09-29 18:41:38 +00:00
|
|
|
/*
|
|
|
|
* If mtime and size match but the user spent no measurable
|
|
|
|
* time in the editor we can't tell if the file was changed.
|
|
|
|
*/
|
2015-03-02 13:58:50 -07:00
|
|
|
if (sudo_timespeccmp(×[0], ×[1], !=))
|
2011-12-02 11:27:33 -05:00
|
|
|
modified = false;
|
2004-09-29 18:41:38 +00:00
|
|
|
}
|
2004-09-28 18:33:06 +00:00
|
|
|
|
1995-03-27 18:26:36 +00:00
|
|
|
/*
|
2004-09-29 18:41:38 +00:00
|
|
|
* If modified in this edit session, mark as modified.
|
1995-03-27 18:26:36 +00:00
|
|
|
*/
|
2004-09-29 18:41:38 +00:00
|
|
|
if (modified)
|
|
|
|
sp->modified = modified;
|
|
|
|
else
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_warnx(U_("%s unchanged"), sp->tpath);
|
2000-04-01 22:25:41 +00:00
|
|
|
|
2016-09-08 16:38:08 -06:00
|
|
|
ret = true;
|
2012-01-06 10:58:13 -05:00
|
|
|
done:
|
2016-09-08 16:38:08 -06:00
|
|
|
debug_return_bool(ret);
|
2004-09-29 18:41:38 +00:00
|
|
|
}
|
1993-10-04 19:23:33 +00:00
|
|
|
|
2016-11-09 16:00:12 -07:00
|
|
|
/*
|
|
|
|
* Check Defaults and Alias entries.
|
2022-03-10 13:30:56 -07:00
|
|
|
* On error, visudo_track_error() will set the line number in sudoerslist.
|
2016-11-09 16:00:12 -07:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
check_defaults_and_aliases(bool strict, bool quiet)
|
|
|
|
{
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(check_defaults_and_aliases, SUDOERS_DEBUG_UTIL);
|
2016-11-09 16:00:12 -07:00
|
|
|
|
2018-07-26 15:12:33 -06:00
|
|
|
if (!check_defaults(&parsed_policy, quiet)) {
|
2016-11-09 16:00:12 -07:00
|
|
|
parse_error = true;
|
2020-08-27 16:06:45 -06:00
|
|
|
}
|
2021-02-23 18:42:37 -07:00
|
|
|
if (check_aliases(&parsed_policy, strict, quiet, print_unused) != 0) {
|
2016-11-09 16:00:12 -07:00
|
|
|
parse_error = true;
|
|
|
|
}
|
|
|
|
debug_return;
|
|
|
|
}
|
|
|
|
|
2004-09-29 18:41:38 +00:00
|
|
|
/*
|
|
|
|
* Parse sudoers after editing and re-edit any ones that caused a parse error.
|
|
|
|
*/
|
2013-04-24 11:55:21 -04:00
|
|
|
static bool
|
2015-06-18 09:51:36 -06:00
|
|
|
reparse_sudoers(char *editor, int editor_argc, char **editor_argv,
|
|
|
|
bool strict, bool quiet)
|
2004-09-29 18:41:38 +00:00
|
|
|
{
|
|
|
|
struct sudoersfile *sp, *last;
|
|
|
|
FILE *fp;
|
2016-07-20 16:41:23 -06:00
|
|
|
int ch, oldlocale;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(reparse_sudoers, SUDOERS_DEBUG_UTIL);
|
1993-10-04 19:23:33 +00:00
|
|
|
|
2004-09-29 18:41:38 +00:00
|
|
|
/*
|
2020-10-30 10:15:30 -06:00
|
|
|
* Parse the edited sudoers files.
|
2004-09-29 18:41:38 +00:00
|
|
|
*/
|
2022-03-10 13:30:56 -07:00
|
|
|
errors = 0;
|
2013-10-22 09:00:37 -06:00
|
|
|
while ((sp = TAILQ_FIRST(&sudoerslist)) != NULL) {
|
|
|
|
last = TAILQ_LAST(&sudoerslist, sudoersfile_list);
|
2004-09-29 18:41:38 +00:00
|
|
|
fp = fopen(sp->tpath, "r+");
|
2004-11-15 15:53:53 +00:00
|
|
|
if (fp == NULL)
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_fatalx(U_("unable to re-open temporary file (%s), %s unchanged."),
|
2023-05-02 10:45:56 -06:00
|
|
|
sp->tpath, sp->opath);
|
1995-03-24 19:19:14 +00:00
|
|
|
|
2004-09-29 18:41:38 +00:00
|
|
|
/* Clean slate for each parse */
|
2015-06-17 06:49:59 -06:00
|
|
|
if (!init_defaults())
|
2020-08-12 13:45:09 -06:00
|
|
|
sudo_fatalx("%s", U_("unable to initialize sudoers default values"));
|
2023-05-09 07:29:06 -06:00
|
|
|
init_parser(sp->opath, &sudoers_conf);
|
2022-03-10 13:30:56 -07:00
|
|
|
sp->errorline = -1;
|
2004-09-29 18:41:38 +00:00
|
|
|
|
2013-04-24 11:55:21 -04:00
|
|
|
/* Parse the sudoers temp file(s) */
|
2012-09-14 16:19:25 -04:00
|
|
|
sudoersrestart(fp);
|
2016-07-20 16:41:23 -06:00
|
|
|
sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale);
|
2012-09-14 16:19:25 -04:00
|
|
|
if (sudoersparse() && !parse_error) {
|
2020-05-17 00:38:21 +08:00
|
|
|
sudo_warnx(U_("unable to parse temporary file (%s), unknown error"),
|
2004-09-29 18:41:38 +00:00
|
|
|
sp->tpath);
|
2011-12-02 11:27:33 -05:00
|
|
|
parse_error = true;
|
1993-10-04 19:23:33 +00:00
|
|
|
}
|
2012-09-14 16:19:25 -04:00
|
|
|
fclose(sudoersin);
|
2010-07-19 12:53:45 -04:00
|
|
|
if (!parse_error) {
|
2022-07-08 14:01:15 -06:00
|
|
|
parse_error = !update_defaults(&parsed_policy, NULL,
|
2018-05-14 09:05:03 -06:00
|
|
|
SETDEF_GENERIC|SETDEF_HOST|SETDEF_USER, true);
|
2016-11-09 16:00:12 -07:00
|
|
|
check_defaults_and_aliases(strict, quiet);
|
2010-04-14 06:24:42 -04:00
|
|
|
}
|
2016-07-23 09:10:48 -06:00
|
|
|
sudoers_setlocale(oldlocale, NULL);
|
1995-03-27 18:26:36 +00:00
|
|
|
|
1995-03-27 19:54:53 +00:00
|
|
|
/*
|
2013-04-24 11:55:21 -04:00
|
|
|
* Got an error, prompt the user for what to do now.
|
1995-03-27 19:54:53 +00:00
|
|
|
*/
|
2004-09-29 18:41:38 +00:00
|
|
|
if (parse_error) {
|
1995-03-27 19:54:53 +00:00
|
|
|
switch (whatnow()) {
|
2013-04-24 11:55:21 -04:00
|
|
|
case 'Q':
|
|
|
|
parse_error = false; /* ignore parse error */
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
visudo_cleanup(); /* discard changes */
|
|
|
|
debug_return_bool(false);
|
|
|
|
case 'e':
|
|
|
|
default:
|
|
|
|
/* Edit file with the parse error */
|
2013-10-22 09:00:37 -06:00
|
|
|
TAILQ_FOREACH(sp, &sudoerslist, entries) {
|
2022-03-10 13:30:56 -07:00
|
|
|
if (errors == 0 || sp->errorline > 0) {
|
2015-06-18 09:51:36 -06:00
|
|
|
edit_sudoers(sp, editor, editor_argc, editor_argv,
|
2022-03-10 13:30:56 -07:00
|
|
|
sp->errorline);
|
2013-04-24 11:55:21 -04:00
|
|
|
}
|
2004-09-29 18:41:38 +00:00
|
|
|
}
|
2013-04-24 11:55:21 -04:00
|
|
|
break;
|
2011-05-18 12:36:26 -04:00
|
|
|
}
|
2004-09-29 18:41:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If any new #include directives were added, edit them too. */
|
2017-07-17 09:26:00 -06:00
|
|
|
if ((sp = TAILQ_NEXT(last, entries)) != NULL) {
|
|
|
|
bool modified = false;
|
|
|
|
do {
|
2023-05-02 10:45:56 -06:00
|
|
|
printf(_("press return to edit %s: "), sp->opath);
|
2023-05-11 18:12:54 -06:00
|
|
|
while ((ch = getchar()) != EOF && ch != '\r' && ch != '\n')
|
2017-07-17 09:26:00 -06:00
|
|
|
continue;
|
|
|
|
edit_sudoers(sp, editor, editor_argc, editor_argv, -1);
|
|
|
|
if (sp->modified)
|
|
|
|
modified = true;
|
|
|
|
} while ((sp = TAILQ_NEXT(sp, entries)) != NULL);
|
|
|
|
|
|
|
|
/* Reparse sudoers if newly added includes were modified. */
|
|
|
|
if (modified)
|
|
|
|
continue;
|
2004-09-29 18:41:38 +00:00
|
|
|
}
|
|
|
|
|
2013-04-24 11:55:21 -04:00
|
|
|
/* If all sudoers files parsed OK we are done. */
|
|
|
|
if (!parse_error)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug_return_bool(true);
|
2004-09-30 17:22:37 +00:00
|
|
|
}
|
1993-03-02 16:35:47 +00:00
|
|
|
|
2004-09-30 17:22:37 +00:00
|
|
|
/*
|
|
|
|
* Set the owner and mode on a sudoers temp file and
|
2011-12-02 11:27:33 -05:00
|
|
|
* move it into place. Returns true on success, else false.
|
2004-09-30 17:22:37 +00:00
|
|
|
*/
|
2011-12-02 11:27:33 -05:00
|
|
|
static bool
|
2021-11-09 13:41:27 -07:00
|
|
|
install_sudoers(struct sudoersfile *sp, bool set_owner, bool set_mode)
|
2004-09-30 17:22:37 +00:00
|
|
|
{
|
2007-09-01 13:34:41 +00:00
|
|
|
struct stat sb;
|
2016-09-08 16:38:08 -06:00
|
|
|
bool ret = false;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(install_sudoers, SUDOERS_DEBUG_UTIL);
|
2007-09-01 13:34:41 +00:00
|
|
|
|
2021-11-09 16:09:48 -07:00
|
|
|
if (sp->tpath == NULL) {
|
|
|
|
ret = true;
|
2016-10-31 15:57:05 -06:00
|
|
|
goto done;
|
2021-11-09 16:09:48 -07:00
|
|
|
}
|
2016-10-31 15:57:05 -06:00
|
|
|
|
2011-11-30 15:33:26 -05:00
|
|
|
if (!sp->modified) {
|
|
|
|
/*
|
|
|
|
* No changes but fix owner/mode if needed.
|
|
|
|
*/
|
|
|
|
(void) unlink(sp->tpath);
|
2021-11-06 12:37:13 -06:00
|
|
|
if (fstat(sp->fd, &sb) == 0) {
|
|
|
|
if (set_owner) {
|
2023-05-08 17:03:31 -06:00
|
|
|
if (sb.st_uid != sudoers_file_uid() || sb.st_gid != sudoers_file_gid()) {
|
|
|
|
if (chown(sp->opath, sudoers_file_uid(), sudoers_file_gid()) != 0) {
|
2021-11-06 12:37:13 -06:00
|
|
|
sudo_warn(U_("unable to set (uid, gid) of %s to (%u, %u)"),
|
2023-05-08 17:03:31 -06:00
|
|
|
sp->opath, (unsigned int)sudoers_file_uid(),
|
|
|
|
(unsigned int)sudoers_file_gid());
|
2021-11-06 12:37:13 -06:00
|
|
|
}
|
2017-05-12 10:02:18 -06:00
|
|
|
}
|
|
|
|
}
|
2021-11-09 13:41:27 -07:00
|
|
|
if (set_mode) {
|
2023-05-08 17:03:31 -06:00
|
|
|
if ((sb.st_mode & ACCESSPERMS) != sudoers_file_mode()) {
|
|
|
|
if (chmod(sp->opath, sudoers_file_mode()) != 0) {
|
2021-11-06 12:37:13 -06:00
|
|
|
sudo_warn(U_("unable to change mode of %s to 0%o"),
|
2023-05-08 17:03:31 -06:00
|
|
|
sp->opath, (unsigned int)sudoers_file_mode());
|
2021-11-06 12:37:13 -06:00
|
|
|
}
|
2017-05-12 10:02:18 -06:00
|
|
|
}
|
|
|
|
}
|
2011-11-30 15:33:26 -05:00
|
|
|
}
|
2016-09-08 16:38:08 -06:00
|
|
|
ret = true;
|
2012-01-06 10:58:13 -05:00
|
|
|
goto done;
|
2011-11-30 15:33:26 -05:00
|
|
|
}
|
|
|
|
|
2004-09-30 17:22:37 +00:00
|
|
|
/*
|
2023-05-02 10:45:56 -06:00
|
|
|
* Change mode and ownership of temp file before moving it into place
|
|
|
|
* to avoid a race condition.
|
2004-09-30 17:22:37 +00:00
|
|
|
*/
|
2021-11-09 13:41:27 -07:00
|
|
|
if (!set_owner || !set_mode) {
|
2021-11-06 12:37:13 -06:00
|
|
|
/* Preserve owner/perms of the existing file. */
|
2007-09-01 13:34:41 +00:00
|
|
|
if (fstat(sp->fd, &sb) == -1)
|
2023-05-02 10:45:56 -06:00
|
|
|
sudo_fatal(U_("unable to stat %s"), sp->opath);
|
2021-11-06 12:37:13 -06:00
|
|
|
}
|
|
|
|
if (set_owner) {
|
2023-05-08 17:03:31 -06:00
|
|
|
if (chown(sp->tpath, sudoers_file_uid(), sudoers_file_gid()) != 0) {
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_warn(U_("unable to set (uid, gid) of %s to (%u, %u)"),
|
2023-05-08 17:03:31 -06:00
|
|
|
sp->tpath, (unsigned int)sudoers_file_uid(),
|
|
|
|
(unsigned int)sudoers_file_gid());
|
2012-01-06 10:58:13 -05:00
|
|
|
goto done;
|
2007-09-01 13:34:41 +00:00
|
|
|
}
|
2021-11-06 12:37:13 -06:00
|
|
|
} else {
|
|
|
|
if (chown(sp->tpath, sb.st_uid, sb.st_gid) != 0) {
|
|
|
|
sudo_warn(U_("unable to set (uid, gid) of %s to (%u, %u)"),
|
|
|
|
sp->tpath, (unsigned int)sb.st_uid, (unsigned int)sb.st_gid);
|
|
|
|
}
|
|
|
|
}
|
2021-11-09 13:41:27 -07:00
|
|
|
if (set_mode) {
|
2023-05-08 17:03:31 -06:00
|
|
|
if (chmod(sp->tpath, sudoers_file_mode()) != 0) {
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_warn(U_("unable to change mode of %s to 0%o"), sp->tpath,
|
2023-05-08 17:03:31 -06:00
|
|
|
(unsigned int)sudoers_file_mode());
|
2012-01-06 10:58:13 -05:00
|
|
|
goto done;
|
2007-09-01 13:34:41 +00:00
|
|
|
}
|
2021-11-06 12:37:13 -06:00
|
|
|
} else {
|
|
|
|
if (chmod(sp->tpath, sb.st_mode & ACCESSPERMS) != 0) {
|
|
|
|
sudo_warn(U_("unable to change mode of %s to 0%o"), sp->tpath,
|
|
|
|
(unsigned int)(sb.st_mode & ACCESSPERMS));
|
|
|
|
}
|
2004-09-30 17:22:37 +00:00
|
|
|
}
|
1996-08-22 18:00:46 +00:00
|
|
|
|
2004-09-30 17:22:37 +00:00
|
|
|
/*
|
2020-10-30 10:15:30 -06:00
|
|
|
* Now that we know sp->tpath parses correctly, it needs to be
|
2023-05-02 10:45:56 -06:00
|
|
|
* rename(2)'d to sp->dpath. If the rename(2) fails we try using
|
|
|
|
* mv(1) in case sp->tpath and sp->dpath are on different file systems.
|
2004-09-30 17:22:37 +00:00
|
|
|
*/
|
2023-05-02 10:45:56 -06:00
|
|
|
if (rename(sp->tpath, sp->dpath) == 0) {
|
2015-06-17 06:49:59 -06:00
|
|
|
free(sp->tpath);
|
2004-09-30 17:22:37 +00:00
|
|
|
sp->tpath = NULL;
|
|
|
|
} else {
|
|
|
|
if (errno == EXDEV) {
|
|
|
|
char *av[4];
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_warnx(U_("%s and %s not on the same file system, using mv to rename"),
|
2023-05-02 10:45:56 -06:00
|
|
|
sp->tpath, sp->dpath);
|
2004-09-30 17:22:37 +00:00
|
|
|
|
|
|
|
/* Build up argument vector for the command */
|
2021-02-10 15:14:08 -07:00
|
|
|
av[0] = sudo_basename(_PATH_MV);
|
2004-09-30 17:22:37 +00:00
|
|
|
av[1] = sp->tpath;
|
2023-05-02 10:45:56 -06:00
|
|
|
av[2] = sp->dpath;
|
2004-09-30 17:22:37 +00:00
|
|
|
av[3] = NULL;
|
|
|
|
|
|
|
|
/* And run it... */
|
2023-02-21 16:14:14 -07:00
|
|
|
if (run_command(_PATH_MV, av, false) != 0) {
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_warnx(U_("command failed: '%s %s %s', %s unchanged"),
|
2023-05-02 10:45:56 -06:00
|
|
|
_PATH_MV, sp->tpath, sp->dpath, sp->opath);
|
2012-01-06 10:58:13 -05:00
|
|
|
goto done;
|
2004-09-29 18:41:38 +00:00
|
|
|
}
|
2015-06-17 06:49:59 -06:00
|
|
|
free(sp->tpath);
|
2004-09-30 17:22:37 +00:00
|
|
|
sp->tpath = NULL;
|
|
|
|
} else {
|
2023-05-02 10:45:56 -06:00
|
|
|
sudo_warn(U_("error renaming %s, %s unchanged"), sp->tpath,
|
|
|
|
sp->opath);
|
2012-01-06 10:58:13 -05:00
|
|
|
goto done;
|
1994-12-16 02:06:56 +00:00
|
|
|
}
|
1998-11-18 03:51:10 +00:00
|
|
|
}
|
2016-09-08 16:38:08 -06:00
|
|
|
ret = true;
|
2012-01-06 10:58:13 -05:00
|
|
|
done:
|
2016-09-08 16:38:08 -06:00
|
|
|
debug_return_bool(ret);
|
1993-03-02 16:35:47 +00:00
|
|
|
}
|
1994-05-25 03:08:45 +00:00
|
|
|
|
1999-07-12 10:53:09 +00:00
|
|
|
/*
|
|
|
|
* Assuming a parse error occurred, prompt the user for what they want
|
|
|
|
* to do now. Returns the first letter of their choice.
|
1995-03-27 19:54:53 +00:00
|
|
|
*/
|
2012-01-06 10:58:13 -05:00
|
|
|
static int
|
2010-04-22 18:09:53 -04:00
|
|
|
whatnow(void)
|
1995-03-27 19:54:53 +00:00
|
|
|
{
|
1999-07-12 10:53:09 +00:00
|
|
|
int choice, c;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(whatnow, SUDOERS_DEBUG_UTIL);
|
1995-03-27 19:54:53 +00:00
|
|
|
|
1999-07-12 10:53:09 +00:00
|
|
|
for (;;) {
|
2011-05-19 16:07:14 -04:00
|
|
|
(void) fputs(_("What now? "), stdout);
|
1999-07-12 10:53:09 +00:00
|
|
|
choice = getchar();
|
2023-05-11 18:12:54 -06:00
|
|
|
for (c = choice; c != '\r' && c != '\n' && c != EOF;)
|
1999-07-12 10:53:09 +00:00
|
|
|
c = getchar();
|
1995-03-27 19:54:53 +00:00
|
|
|
|
1999-12-16 18:02:14 +00:00
|
|
|
switch (choice) {
|
|
|
|
case EOF:
|
|
|
|
choice = 'x';
|
2020-08-01 13:10:50 -06:00
|
|
|
FALLTHROUGH;
|
1999-12-16 18:02:14 +00:00
|
|
|
case 'e':
|
|
|
|
case 'x':
|
|
|
|
case 'Q':
|
2012-01-06 10:58:13 -05:00
|
|
|
debug_return_int(choice);
|
1999-12-16 18:02:14 +00:00
|
|
|
default:
|
2011-05-19 16:07:14 -04:00
|
|
|
(void) puts(_("Options are:\n"
|
|
|
|
" (e)dit sudoers file again\n"
|
|
|
|
" e(x)it without saving changes to sudoers file\n"
|
|
|
|
" (Q)uit and save changes to sudoers file (DANGER!)\n"));
|
1999-07-12 10:53:09 +00:00
|
|
|
}
|
|
|
|
}
|
1995-03-27 19:54:53 +00:00
|
|
|
}
|
|
|
|
|
1999-07-12 10:53:09 +00:00
|
|
|
/*
|
|
|
|
* Install signal handlers for visudo.
|
1995-05-02 03:34:07 +00:00
|
|
|
*/
|
1999-04-10 04:49:03 +00:00
|
|
|
static void
|
2010-04-22 18:09:53 -04:00
|
|
|
setup_signals(void)
|
1995-05-02 03:34:07 +00:00
|
|
|
{
|
2017-05-12 10:02:18 -06:00
|
|
|
struct sigaction sa;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(setup_signals, SUDOERS_DEBUG_UTIL);
|
1995-05-02 03:34:07 +00:00
|
|
|
|
2012-01-06 10:58:13 -05:00
|
|
|
/*
|
|
|
|
* Setup signal handlers to cleanup nicely.
|
|
|
|
*/
|
2013-08-03 08:30:06 -06:00
|
|
|
memset(&sa, 0, sizeof(sa));
|
2012-01-06 10:58:13 -05:00
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sa.sa_flags = SA_RESTART;
|
|
|
|
sa.sa_handler = quit;
|
|
|
|
(void) sigaction(SIGTERM, &sa, NULL);
|
|
|
|
(void) sigaction(SIGHUP, &sa, NULL);
|
|
|
|
(void) sigaction(SIGINT, &sa, NULL);
|
|
|
|
(void) sigaction(SIGQUIT, &sa, NULL);
|
|
|
|
|
|
|
|
debug_return;
|
1995-05-02 03:34:07 +00:00
|
|
|
}
|
1999-07-12 10:53:09 +00:00
|
|
|
|
2000-04-01 22:25:41 +00:00
|
|
|
static int
|
2023-02-21 16:14:14 -07:00
|
|
|
run_command(const char *path, char *const *argv, bool foreground)
|
2000-04-01 22:25:41 +00:00
|
|
|
{
|
2023-03-22 13:52:43 -06:00
|
|
|
int status, ttyfd = -1;
|
|
|
|
pid_t pid, saved_pgrp = -1;
|
2023-03-21 09:22:53 -06:00
|
|
|
int rv = -1;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(run_command, SUDOERS_DEBUG_UTIL);
|
2000-04-01 22:25:41 +00:00
|
|
|
|
2023-03-21 09:22:53 -06:00
|
|
|
if (foreground) {
|
|
|
|
/* Save original foreground process group id. */
|
|
|
|
ttyfd = open(_PATH_TTY, O_RDWR);
|
|
|
|
if (ttyfd != -1) {
|
|
|
|
saved_pgrp = tcgetpgrp(ttyfd);
|
|
|
|
if (saved_pgrp == -1) {
|
|
|
|
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
|
|
|
|
"%s: unable to get foreground pgrp", __func__);
|
|
|
|
close(ttyfd);
|
|
|
|
ttyfd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ttyfd == -1)
|
|
|
|
foreground = false;
|
|
|
|
}
|
|
|
|
|
2012-04-06 15:20:16 -04:00
|
|
|
switch (pid = sudo_debug_fork()) {
|
2000-04-01 22:25:41 +00:00
|
|
|
case -1:
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_fatal(U_("unable to execute %s"), path);
|
2000-04-01 22:25:41 +00:00
|
|
|
break; /* NOTREACHED */
|
|
|
|
case 0:
|
2023-02-21 16:14:14 -07:00
|
|
|
/* Run command as the foreground process of a new process group. */
|
|
|
|
if (foreground) {
|
|
|
|
pid = getpid();
|
2023-03-21 09:22:53 -06:00
|
|
|
if (setpgid(0, pid) == -1) {
|
|
|
|
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
|
|
|
|
"%s: unable to set pgrp to %d (editor)",
|
|
|
|
__func__, (int)pid);
|
|
|
|
} else {
|
|
|
|
/* Wait for parent to grant us the controlling tty. */
|
|
|
|
struct timespec ts = { 0, 1000 }; /* 1us */
|
|
|
|
sudo_debug_printf(SUDO_DEBUG_DEBUG,
|
|
|
|
"%s: waiting for controlling tty", __func__);
|
|
|
|
while (tcgetpgrp(ttyfd) != pid)
|
|
|
|
nanosleep(&ts, NULL);
|
|
|
|
sudo_debug_printf(SUDO_DEBUG_DEBUG,
|
|
|
|
"%s: got controlling tty", __func__);
|
2023-02-21 16:34:12 -07:00
|
|
|
}
|
2023-03-21 09:22:53 -06:00
|
|
|
close(ttyfd);
|
2023-02-21 16:14:14 -07:00
|
|
|
}
|
2005-11-11 22:19:53 +00:00
|
|
|
closefrom(STDERR_FILENO + 1);
|
2000-04-01 22:25:41 +00:00
|
|
|
execv(path, argv);
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_warn(U_("unable to run %s"), path);
|
2000-11-03 14:36:32 +00:00
|
|
|
_exit(127);
|
2000-04-01 22:25:41 +00:00
|
|
|
break; /* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
2023-03-21 09:22:53 -06:00
|
|
|
/* Set child process group in both parent and child to avoid a race. */
|
|
|
|
if (foreground) {
|
|
|
|
if (setpgid(pid, pid) == -1) {
|
|
|
|
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
|
|
|
|
"%s: unable to set pgrp to %d (editor)",
|
|
|
|
__func__, (int)pid);
|
|
|
|
} else if (tcsetpgrp(ttyfd, pid) == -1) {
|
|
|
|
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
|
|
|
|
"%s: unable to set foreground pgrp to %d (editor)",
|
|
|
|
__func__, (int)pid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-02 11:24:01 -06:00
|
|
|
for (;;) {
|
2023-03-21 09:22:53 -06:00
|
|
|
if (waitpid(pid, &status, WUNTRACED) == -1) {
|
2023-02-21 16:14:14 -07:00
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
2015-10-02 11:24:01 -06:00
|
|
|
break;
|
2023-02-21 16:14:14 -07:00
|
|
|
}
|
2023-03-21 09:22:53 -06:00
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
rv = WEXITSTATUS(status);
|
|
|
|
sudo_debug_printf(SUDO_DEBUG_DIAG, "%s: %d: exited %d",
|
2023-03-22 14:48:49 -06:00
|
|
|
__func__, (int)pid, rv);
|
2015-10-02 11:24:01 -06:00
|
|
|
break;
|
2023-03-21 09:22:53 -06:00
|
|
|
} else if (WIFSIGNALED(status)) {
|
|
|
|
sudo_debug_printf(SUDO_DEBUG_DIAG, "%s: %d: killed by signal %d",
|
2023-03-22 14:48:49 -06:00
|
|
|
__func__, (int)pid, WTERMSIG(status));
|
2023-03-21 09:22:53 -06:00
|
|
|
break;
|
|
|
|
} else if (WIFSTOPPED(status)) {
|
|
|
|
const int signo = WSTOPSIG(status);
|
|
|
|
sudo_debug_printf(SUDO_DEBUG_DIAG, "%s: %d: suspended by signal %d",
|
2023-03-22 14:48:49 -06:00
|
|
|
__func__, (int)pid, signo);
|
2023-03-21 09:22:53 -06:00
|
|
|
if (foreground) {
|
|
|
|
sudo_suspend_parent(signo, getpid(), getpgrp(), pid,
|
|
|
|
NULL, NULL);
|
|
|
|
killpg(pid, SIGCONT);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sudo_debug_printf(SUDO_DEBUG_ERROR,
|
2023-03-22 14:48:49 -06:00
|
|
|
"%d: unknown status 0x%x", (int)pid, status);
|
2023-03-21 09:22:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (foreground) {
|
|
|
|
/* Restore the original foreground process group. */
|
|
|
|
sigset_t mask, omask;
|
|
|
|
sigemptyset(&mask);
|
|
|
|
sigaddset(&mask, SIGTTOU);
|
|
|
|
sigprocmask(SIG_BLOCK, &mask, &omask);
|
|
|
|
sudo_debug_printf(SUDO_DEBUG_DEBUG,
|
|
|
|
"%s: restoring foreground pgrp to %d (visudo)",
|
|
|
|
__func__, (int)saved_pgrp);
|
|
|
|
if (tcsetpgrp(ttyfd, saved_pgrp) == -1) {
|
|
|
|
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
|
|
|
|
"%s: unable to restore foreground pgrp to %d (visudo)",
|
|
|
|
__func__, (int)saved_pgrp);
|
2023-02-21 16:14:14 -07:00
|
|
|
}
|
2023-03-21 09:22:53 -06:00
|
|
|
close(ttyfd);
|
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
2015-10-02 11:24:01 -06:00
|
|
|
}
|
2000-04-01 22:25:41 +00:00
|
|
|
|
2012-01-06 10:58:13 -05:00
|
|
|
debug_return_int(rv);
|
2000-04-01 22:25:41 +00:00
|
|
|
}
|
|
|
|
|
2012-01-04 15:45:27 -05:00
|
|
|
static bool
|
2021-11-09 13:41:27 -07:00
|
|
|
check_file(const char *path, bool quiet, bool check_owner, bool check_mode)
|
2001-12-11 22:51:17 +00:00
|
|
|
{
|
2008-11-09 20:19:19 +00:00
|
|
|
struct stat sb;
|
2012-02-28 14:16:39 -05:00
|
|
|
bool ok = true;
|
2021-11-06 12:37:13 -06:00
|
|
|
debug_decl(check_file, SUDOERS_DEBUG_UTIL);
|
2012-02-28 14:16:39 -05:00
|
|
|
|
|
|
|
if (stat(path, &sb) == 0) {
|
2021-11-06 12:37:13 -06:00
|
|
|
if (check_owner) {
|
2023-05-08 17:03:31 -06:00
|
|
|
if (sb.st_uid != sudoers_file_uid() || sb.st_gid != sudoers_file_gid()) {
|
2021-11-06 12:37:13 -06:00
|
|
|
ok = false;
|
|
|
|
if (!quiet) {
|
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: wrong owner (uid, gid) should be (%u, %u)\n"),
|
2023-05-08 17:03:31 -06:00
|
|
|
path, (unsigned int)sudoers_file_uid(),
|
|
|
|
(unsigned int)sudoers_file_gid());
|
2012-02-28 14:16:39 -05:00
|
|
|
}
|
2021-11-06 12:37:13 -06:00
|
|
|
}
|
2012-02-28 14:16:39 -05:00
|
|
|
}
|
2021-11-09 13:41:27 -07:00
|
|
|
if (check_mode) {
|
2023-05-08 17:03:31 -06:00
|
|
|
if ((sb.st_mode & ALLPERMS) != sudoers_file_mode()) {
|
2021-11-06 12:37:13 -06:00
|
|
|
ok = false;
|
|
|
|
if (!quiet) {
|
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: bad permissions, should be mode 0%o\n"),
|
2023-05-08 17:03:31 -06:00
|
|
|
path, (unsigned int)sudoers_file_mode());
|
2021-11-06 12:37:13 -06:00
|
|
|
}
|
2012-02-28 14:16:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug_return_bool(ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2023-05-02 10:37:38 -06:00
|
|
|
check_syntax(const char *path, bool quiet, bool strict, bool check_owner,
|
2021-11-09 13:41:27 -07:00
|
|
|
bool check_mode)
|
2012-02-28 14:16:39 -05:00
|
|
|
{
|
2012-01-06 10:58:13 -05:00
|
|
|
bool ok = false;
|
2023-05-02 10:37:38 -06:00
|
|
|
int fd, oldlocale;
|
|
|
|
char fname[PATH_MAX];
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(check_syntax, SUDOERS_DEBUG_UTIL);
|
2001-12-11 23:22:54 +00:00
|
|
|
|
2023-05-02 10:37:38 -06:00
|
|
|
if (strcmp(path, "-") == 0) {
|
2012-09-14 16:19:25 -04:00
|
|
|
sudoersin = stdin;
|
2023-05-02 10:37:38 -06:00
|
|
|
(void)strlcpy(fname, "stdin", sizeof(fname));
|
|
|
|
} else {
|
|
|
|
fd = sudo_open_conf_path(path, fname, sizeof(fname), NULL);
|
|
|
|
if (fd == -1 || (sudoersin = fdopen(fd, "r")) == NULL) {
|
|
|
|
if (!quiet)
|
|
|
|
sudo_warn(U_("unable to open %s"), fname);
|
2023-02-08 16:47:18 -07:00
|
|
|
if (fd != -1)
|
2023-05-02 10:37:38 -06:00
|
|
|
close(fd);
|
|
|
|
goto done;
|
|
|
|
}
|
2001-12-11 22:51:17 +00:00
|
|
|
}
|
2023-05-09 07:29:06 -06:00
|
|
|
init_parser(fname, &sudoers_conf);
|
2016-07-22 10:41:56 -06:00
|
|
|
sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale);
|
2012-09-14 16:19:25 -04:00
|
|
|
if (sudoersparse() && !parse_error) {
|
2001-12-11 22:51:17 +00:00
|
|
|
if (!quiet)
|
2023-05-02 10:37:38 -06:00
|
|
|
sudo_warnx(U_("failed to parse %s file, unknown error"), fname);
|
2011-12-02 11:27:33 -05:00
|
|
|
parse_error = true;
|
2001-12-11 22:51:17 +00:00
|
|
|
}
|
2012-08-14 10:45:55 -04:00
|
|
|
if (!parse_error) {
|
2022-07-08 14:01:15 -06:00
|
|
|
parse_error = !update_defaults(&parsed_policy, NULL,
|
2018-05-14 09:05:03 -06:00
|
|
|
SETDEF_GENERIC|SETDEF_HOST|SETDEF_USER, true);
|
2016-11-09 16:00:12 -07:00
|
|
|
check_defaults_and_aliases(strict, quiet);
|
2009-03-28 13:30:15 +00:00
|
|
|
}
|
2016-07-22 10:41:56 -06:00
|
|
|
sudoers_setlocale(oldlocale, NULL);
|
2012-01-06 10:58:13 -05:00
|
|
|
ok = !parse_error;
|
2012-02-28 14:16:39 -05:00
|
|
|
|
2020-08-27 16:08:18 -06:00
|
|
|
if (!parse_error) {
|
2012-02-28 14:16:39 -05:00
|
|
|
struct sudoersfile *sp;
|
|
|
|
|
|
|
|
/* Parsed OK, check mode and owner. */
|
2023-05-02 10:37:38 -06:00
|
|
|
if (check_file(fname, quiet, check_owner, check_mode)) {
|
2013-08-26 14:40:25 -06:00
|
|
|
if (!quiet)
|
2023-05-02 10:37:38 -06:00
|
|
|
(void) printf(_("%s: parsed OK\n"), fname);
|
2013-08-26 14:40:25 -06:00
|
|
|
} else {
|
2012-01-06 10:58:13 -05:00
|
|
|
ok = false;
|
2013-08-26 14:40:25 -06:00
|
|
|
}
|
2013-10-22 09:00:37 -06:00
|
|
|
TAILQ_FOREACH(sp, &sudoerslist, entries) {
|
2023-05-02 10:45:56 -06:00
|
|
|
if (check_file(sp->opath, quiet, check_owner, check_mode)) {
|
2013-08-26 14:40:25 -06:00
|
|
|
if (!quiet)
|
2023-05-02 10:45:56 -06:00
|
|
|
(void) printf(_("%s: parsed OK\n"), sp->opath);
|
2013-08-26 14:40:25 -06:00
|
|
|
} else {
|
2012-02-28 14:16:39 -05:00
|
|
|
ok = false;
|
2013-08-26 14:40:25 -06:00
|
|
|
}
|
2008-11-09 20:19:19 +00:00
|
|
|
}
|
2001-12-11 22:51:17 +00:00
|
|
|
}
|
|
|
|
|
2012-01-06 10:58:13 -05:00
|
|
|
done:
|
|
|
|
debug_return_bool(ok);
|
2001-12-11 22:51:17 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 08:08:45 -06:00
|
|
|
static bool
|
|
|
|
lock_sudoers(struct sudoersfile *entry)
|
|
|
|
{
|
|
|
|
int ch;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(lock_sudoers, SUDOERS_DEBUG_UTIL);
|
2018-08-31 08:08:45 -06:00
|
|
|
|
|
|
|
if (!sudo_lock_file(entry->fd, SUDO_TLOCK)) {
|
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
2023-05-02 10:45:56 -06:00
|
|
|
sudo_warnx(U_("%s busy, try again later"), entry->opath);
|
2018-08-31 08:08:45 -06:00
|
|
|
debug_return_bool(false);
|
|
|
|
}
|
2023-05-02 10:45:56 -06:00
|
|
|
sudo_warn(U_("unable to lock %s"), entry->opath);
|
2018-08-31 08:08:45 -06:00
|
|
|
(void) fputs(_("Edit anyway? [y/N]"), stdout);
|
|
|
|
ch = getchar();
|
|
|
|
if (tolower(ch) != 'y')
|
|
|
|
debug_return_bool(false);
|
|
|
|
}
|
|
|
|
debug_return_bool(true);
|
|
|
|
}
|
|
|
|
|
2007-06-16 11:31:56 +00:00
|
|
|
/*
|
2020-09-03 13:07:38 -06:00
|
|
|
* Open (and lock) a new sudoers file.
|
|
|
|
* Returns a new struct sudoersfile on success or NULL on failure.
|
2007-06-16 11:31:56 +00:00
|
|
|
*/
|
2020-09-03 13:07:38 -06:00
|
|
|
static struct sudoersfile *
|
|
|
|
new_sudoers(const char *path, bool doedit)
|
2004-09-27 16:02:10 +00:00
|
|
|
{
|
2023-05-02 10:37:38 -06:00
|
|
|
const char *cp, *ep, *path_end;
|
2004-09-29 18:41:38 +00:00
|
|
|
struct sudoersfile *entry;
|
2020-05-19 10:09:56 -06:00
|
|
|
struct stat sb;
|
2023-05-02 10:37:38 -06:00
|
|
|
size_t len;
|
|
|
|
int fd = -1;
|
2020-09-03 13:07:38 -06:00
|
|
|
debug_decl(new_sudoersfile, SUDOERS_DEBUG_UTIL);
|
2012-01-06 10:54:30 -05:00
|
|
|
|
2023-05-02 10:45:56 -06:00
|
|
|
/* We always write to the first file in the colon-separated path. */
|
|
|
|
len = strcspn(path, ":");
|
|
|
|
entry = calloc(1, sizeof(*entry));
|
|
|
|
if (entry == NULL || (entry->dpath = strndup(path, len)) == NULL)
|
|
|
|
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
|
|
|
|
|
2023-05-02 10:37:38 -06:00
|
|
|
/* Open the first file found in the colon-separated path. */
|
|
|
|
path_end = path + strlen(path);
|
|
|
|
for (cp = sudo_strsplit(path, path_end, ":", &ep);
|
|
|
|
cp != NULL; cp = sudo_strsplit(NULL, path_end, ":", &ep)) {
|
|
|
|
|
|
|
|
char fname[PATH_MAX];
|
|
|
|
len = ep - cp;
|
|
|
|
if (len >= sizeof(fname)) {
|
|
|
|
errno = ENAMETOOLONG;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
memcpy(fname, cp, len);
|
|
|
|
fname[len] = '\0';
|
|
|
|
|
|
|
|
/* Open in write mode for file locking. */
|
|
|
|
fd = open(fname, checkonly ? O_RDONLY : O_RDWR);
|
2023-05-02 10:45:56 -06:00
|
|
|
if (fd != -1) {
|
|
|
|
/* Store the path we actually opened. */
|
|
|
|
if ((entry->opath = strdup(fname)) == NULL) {
|
|
|
|
sudo_fatalx(U_("%s: %s"), __func__,
|
|
|
|
U_("unable to allocate memory"));
|
|
|
|
}
|
2023-05-02 10:37:38 -06:00
|
|
|
break;
|
2023-05-02 10:45:56 -06:00
|
|
|
}
|
2004-09-28 18:33:06 +00:00
|
|
|
|
2023-05-02 10:45:56 -06:00
|
|
|
/* If the file exists but we can't open it, that is a fatal error. */
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
sudo_warn("%s", fname);
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fd == -1) {
|
2023-05-02 10:37:38 -06:00
|
|
|
if (!checkonly) {
|
2023-05-02 10:45:56 -06:00
|
|
|
/* No sudoers file, create the destination file for editing. */
|
2023-05-08 17:03:31 -06:00
|
|
|
fd = open(entry->dpath, O_RDWR|O_CREAT, sudoers_file_mode());
|
2023-05-02 10:45:56 -06:00
|
|
|
}
|
|
|
|
if (fd == -1) {
|
|
|
|
sudo_warn("%s", entry->dpath);
|
|
|
|
goto bad;
|
2023-05-02 10:37:38 -06:00
|
|
|
}
|
2023-05-02 10:45:56 -06:00
|
|
|
entry->opath = entry->dpath;
|
2023-05-02 10:37:38 -06:00
|
|
|
}
|
2023-05-02 10:45:56 -06:00
|
|
|
if (fstat(fd, &sb) == -1) {
|
|
|
|
sudo_warn("%s", entry->opath);
|
2020-09-03 13:07:38 -06:00
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
if (!S_ISREG(sb.st_mode)) {
|
2023-05-02 10:45:56 -06:00
|
|
|
sudo_warnx(U_("%s is not a regular file"), entry->opath);
|
2020-09-03 13:07:38 -06:00
|
|
|
goto bad;
|
|
|
|
}
|
2023-05-02 10:37:38 -06:00
|
|
|
entry->fd = fd;
|
|
|
|
/* entry->tpath = NULL; */
|
|
|
|
/* entry->modified = false; */
|
|
|
|
entry->doedit = doedit;
|
2020-09-03 13:07:38 -06:00
|
|
|
if (!checkonly && !lock_sudoers(entry))
|
|
|
|
goto bad;
|
|
|
|
debug_return_ptr(entry);
|
|
|
|
bad:
|
2023-05-02 10:37:38 -06:00
|
|
|
if (fd != -1)
|
|
|
|
close(fd);
|
2023-05-02 10:45:56 -06:00
|
|
|
if (entry->opath != entry->dpath)
|
|
|
|
free(entry->opath);
|
|
|
|
free(entry->dpath);
|
2020-09-03 13:07:38 -06:00
|
|
|
free(entry);
|
|
|
|
debug_return_ptr(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Used to open (and lock) the initial sudoers file and to also open
|
|
|
|
* any subsequent files #included via a callback from the parser.
|
|
|
|
*/
|
|
|
|
FILE *
|
2023-05-02 10:37:38 -06:00
|
|
|
open_sudoers(const char *path, char **outfile, bool doedit, bool *keepopen)
|
2020-09-03 13:07:38 -06:00
|
|
|
{
|
|
|
|
struct sudoersfile *entry;
|
2023-05-02 10:37:38 -06:00
|
|
|
size_t len;
|
2020-09-03 13:07:38 -06:00
|
|
|
FILE *fp;
|
|
|
|
debug_decl(open_sudoers, SUDOERS_DEBUG_UTIL);
|
|
|
|
|
2023-05-02 10:37:38 -06:00
|
|
|
/* Check for existing entry using the first file in path. */
|
|
|
|
len = strcspn(path, ":");
|
2013-10-22 09:00:37 -06:00
|
|
|
TAILQ_FOREACH(entry, &sudoerslist, entries) {
|
2023-05-02 10:47:11 -06:00
|
|
|
if (strncmp(path, entry->dpath, len) == 0 && entry->dpath[len] == '\0')
|
2004-09-29 18:41:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (entry == NULL) {
|
2022-10-04 16:11:45 -06:00
|
|
|
if (doedit && !edit_includes) {
|
|
|
|
/* Only edit the main sudoers file. */
|
2023-05-02 10:47:47 -06:00
|
|
|
if (strncmp(path, path_sudoers, len) != 0 ||
|
|
|
|
(path_sudoers[len] != '\0' && path_sudoers[len] != ':'))
|
2022-10-04 16:11:45 -06:00
|
|
|
doedit = false;
|
|
|
|
}
|
2020-09-03 13:07:38 -06:00
|
|
|
if ((entry = new_sudoers(path, doedit)) == NULL)
|
2018-08-31 08:08:45 -06:00
|
|
|
debug_return_ptr(NULL);
|
2004-09-29 18:41:38 +00:00
|
|
|
if ((fp = fdopen(entry->fd, "r")) == NULL)
|
2023-05-02 10:45:56 -06:00
|
|
|
sudo_fatal("%s", entry->opath);
|
2013-10-22 09:00:37 -06:00
|
|
|
TAILQ_INSERT_TAIL(&sudoerslist, entry, entries);
|
2004-09-29 18:41:38 +00:00
|
|
|
} else {
|
|
|
|
/* Already exists, open .tmp version if there is one. */
|
|
|
|
if (entry->tpath != NULL) {
|
|
|
|
if ((fp = fopen(entry->tpath, "r")) == NULL)
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_fatal("%s", entry->tpath);
|
2004-09-29 18:41:38 +00:00
|
|
|
} else {
|
|
|
|
if ((fp = fdopen(entry->fd, "r")) == NULL)
|
2023-05-02 10:45:56 -06:00
|
|
|
sudo_fatal("%s", entry->opath);
|
2009-04-16 12:22:04 +00:00
|
|
|
rewind(fp);
|
2004-09-29 18:41:38 +00:00
|
|
|
}
|
2004-09-28 18:33:06 +00:00
|
|
|
}
|
2009-04-16 12:22:04 +00:00
|
|
|
if (keepopen != NULL)
|
2011-12-02 11:27:33 -05:00
|
|
|
*keepopen = true;
|
2023-05-02 10:37:38 -06:00
|
|
|
if (outfile != NULL) {
|
2023-05-02 10:45:56 -06:00
|
|
|
*outfile = sudo_rcstr_dup(entry->opath);
|
2023-05-02 10:37:38 -06:00
|
|
|
if (*outfile == NULL)
|
|
|
|
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
|
|
|
|
}
|
2012-01-06 10:58:13 -05:00
|
|
|
debug_return_ptr(fp);
|
2004-09-27 16:02:10 +00:00
|
|
|
}
|
|
|
|
|
2023-05-02 10:37:38 -06:00
|
|
|
/*
|
|
|
|
* Support "visudo -x" for backwards compatibility.
|
|
|
|
* To be removed in a future version of sudo.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
export_sudoers(const char *infile, const char *outfile)
|
|
|
|
{
|
|
|
|
char pathbuf[PATH_MAX];
|
|
|
|
int fd;
|
|
|
|
debug_decl(export_sudoers, SUDOERS_DEBUG_UTIL);
|
|
|
|
|
|
|
|
sudo_warnx("%s",
|
|
|
|
U_("the -x option will be removed in a future release"));
|
|
|
|
sudo_warnx("%s",
|
|
|
|
U_("please consider using the cvtsudoers utility instead"));
|
|
|
|
|
|
|
|
/* Export the first sudoers file that exists in the path. */
|
|
|
|
fd = sudo_open_conf_path(infile, pathbuf, sizeof(pathbuf), NULL);
|
|
|
|
if (fd != -1) {
|
|
|
|
close(fd);
|
|
|
|
infile = pathbuf;
|
|
|
|
}
|
|
|
|
execlp("cvtsudoers", "cvtsudoers", "-f", "json", "-o", outfile,
|
|
|
|
infile, (char *)0);
|
|
|
|
sudo_fatal(U_("unable to execute %s"), "cvtsudoers");
|
|
|
|
}
|
|
|
|
|
2021-02-23 18:42:37 -07:00
|
|
|
/* Display unused aliases from check_aliases(). */
|
2011-05-25 13:04:13 -04:00
|
|
|
static int
|
2021-02-23 18:42:37 -07:00
|
|
|
print_unused(struct sudoers_parse_tree *parse_tree, struct alias *a, void *v)
|
2011-05-25 13:04:13 -04:00
|
|
|
{
|
2021-02-23 18:42:37 -07:00
|
|
|
const bool quiet = *((bool *)v);
|
2011-05-25 13:04:13 -04:00
|
|
|
|
2021-02-23 18:42:37 -07:00
|
|
|
if (!quiet) {
|
|
|
|
fprintf(stderr, U_("Warning: %s:%d:%d: unused %s \"%s\""), a->file,
|
|
|
|
a->line, a->column, alias_type_to_string(a->type), a->name);
|
|
|
|
fputc('\n', stderr);
|
2015-05-27 09:51:54 -06:00
|
|
|
}
|
2011-01-24 15:15:18 -05:00
|
|
|
return 0;
|
2004-10-26 22:26:28 +00:00
|
|
|
}
|
|
|
|
|
2018-01-28 16:11:02 -07:00
|
|
|
static void
|
|
|
|
parse_sudoers_options(void)
|
|
|
|
{
|
|
|
|
struct plugin_info_list *plugins;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(parse_sudoers_options, SUDOERS_DEBUG_UTIL);
|
2018-01-28 16:11:02 -07:00
|
|
|
|
|
|
|
plugins = sudo_conf_plugins();
|
|
|
|
if (plugins) {
|
|
|
|
struct plugin_info *info;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(info, plugins, entries) {
|
|
|
|
if (strcmp(info->symbol_name, "sudoers_policy") == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (info != NULL && info->options != NULL) {
|
|
|
|
char * const *cur;
|
|
|
|
|
|
|
|
#define MATCHES(s, v) \
|
|
|
|
(strncmp((s), (v), sizeof(v) - 1) == 0 && (s)[sizeof(v) - 1] != '\0')
|
|
|
|
|
|
|
|
for (cur = info->options; *cur != NULL; cur++) {
|
|
|
|
const char *errstr, *p;
|
|
|
|
id_t id;
|
2023-05-08 17:03:31 -06:00
|
|
|
mode_t mode;
|
2018-01-28 16:11:02 -07:00
|
|
|
|
|
|
|
if (MATCHES(*cur, "sudoers_file=")) {
|
2023-05-02 10:47:47 -06:00
|
|
|
path_sudoers = *cur + sizeof("sudoers_file=") - 1;
|
2018-01-28 16:11:02 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (MATCHES(*cur, "sudoers_uid=")) {
|
|
|
|
p = *cur + sizeof("sudoers_uid=") - 1;
|
2019-10-20 10:21:29 -06:00
|
|
|
id = sudo_strtoid(p, &errstr);
|
2018-01-28 16:11:02 -07:00
|
|
|
if (errstr == NULL)
|
2023-05-09 07:29:06 -06:00
|
|
|
sudoers_conf.sudoers_uid = (uid_t)id;
|
2018-01-28 16:11:02 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (MATCHES(*cur, "sudoers_gid=")) {
|
|
|
|
p = *cur + sizeof("sudoers_gid=") - 1;
|
2019-10-20 10:21:29 -06:00
|
|
|
id = sudo_strtoid(p, &errstr);
|
2018-01-28 16:11:02 -07:00
|
|
|
if (errstr == NULL)
|
2023-05-09 07:29:06 -06:00
|
|
|
sudoers_conf.sudoers_gid = (gid_t)id;
|
2018-01-28 16:11:02 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (MATCHES(*cur, "sudoers_mode=")) {
|
|
|
|
p = *cur + sizeof("sudoers_mode=") - 1;
|
2023-05-08 17:03:31 -06:00
|
|
|
mode = sudo_strtomode(p, &errstr);
|
2018-01-28 16:11:02 -07:00
|
|
|
if (errstr == NULL)
|
2023-05-09 07:29:06 -06:00
|
|
|
sudoers_conf.sudoers_mode = mode;
|
2018-01-28 16:11:02 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#undef MATCHES
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug_return;
|
|
|
|
}
|
|
|
|
|
1999-07-12 10:53:09 +00:00
|
|
|
/*
|
2004-11-15 15:53:53 +00:00
|
|
|
* Unlink any sudoers temp files that remain.
|
1999-07-12 10:53:09 +00:00
|
|
|
*/
|
2012-11-25 09:34:40 -05:00
|
|
|
static void
|
|
|
|
visudo_cleanup(void)
|
1999-07-12 10:53:09 +00:00
|
|
|
{
|
2004-09-29 18:41:38 +00:00
|
|
|
struct sudoersfile *sp;
|
2001-01-17 16:34:36 +00:00
|
|
|
|
2013-10-22 09:00:37 -06:00
|
|
|
TAILQ_FOREACH(sp, &sudoerslist, entries) {
|
2004-09-29 18:41:38 +00:00
|
|
|
if (sp->tpath != NULL)
|
|
|
|
(void) unlink(sp->tpath);
|
|
|
|
}
|
1999-07-12 10:53:09 +00:00
|
|
|
}
|
|
|
|
|
2004-09-30 17:22:37 +00:00
|
|
|
/*
|
2004-11-15 15:53:53 +00:00
|
|
|
* Unlink sudoers temp files (if any) and exit.
|
2004-09-30 17:22:37 +00:00
|
|
|
*/
|
2010-05-21 18:40:04 -04:00
|
|
|
static void
|
2010-04-22 18:09:53 -04:00
|
|
|
quit(int signo)
|
2004-09-30 17:22:37 +00:00
|
|
|
{
|
2012-11-25 09:34:40 -05:00
|
|
|
struct iovec iov[4];
|
|
|
|
|
2022-03-10 13:30:56 -07:00
|
|
|
visudo_cleanup();
|
2011-03-04 16:12:40 -05:00
|
|
|
|
|
|
|
#define emsg " exiting due to signal: "
|
2012-11-25 09:34:40 -05:00
|
|
|
iov[0].iov_base = (char *)getprogname();
|
|
|
|
iov[0].iov_len = strlen(iov[0].iov_base);
|
2022-06-28 16:33:15 -06:00
|
|
|
iov[1].iov_base = (char *)emsg;
|
2012-11-25 09:34:40 -05:00
|
|
|
iov[1].iov_len = sizeof(emsg) - 1;
|
|
|
|
iov[2].iov_base = strsignal(signo);
|
|
|
|
iov[2].iov_len = strlen(iov[2].iov_base);
|
2022-06-28 16:33:15 -06:00
|
|
|
iov[3].iov_base = (char *)"\n";
|
2012-11-25 09:34:40 -05:00
|
|
|
iov[3].iov_len = 1;
|
|
|
|
ignore_result(writev(STDERR_FILENO, iov, 4));
|
2004-11-15 15:53:53 +00:00
|
|
|
_exit(signo);
|
2004-09-30 17:22:37 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 07:48:31 -06:00
|
|
|
#define VISUDO_USAGE "usage: %s [-chqsV] [[-f] sudoers ]\n"
|
|
|
|
|
1999-07-12 10:53:09 +00:00
|
|
|
static void
|
2022-09-07 07:48:31 -06:00
|
|
|
usage(void)
|
1999-07-12 10:53:09 +00:00
|
|
|
{
|
2022-09-07 07:48:31 -06:00
|
|
|
(void) fprintf(stderr, VISUDO_USAGE, getprogname());
|
|
|
|
exit(EXIT_FAILURE);
|
2011-02-21 11:33:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
help(void)
|
|
|
|
{
|
2011-05-19 16:07:14 -04:00
|
|
|
(void) printf(_("%s - safely edit the sudoers file\n\n"), getprogname());
|
2022-09-07 07:48:31 -06:00
|
|
|
(void) printf(VISUDO_USAGE, getprogname());
|
2011-05-19 16:07:14 -04:00
|
|
|
(void) puts(_("\nOptions:\n"
|
2014-05-13 15:00:43 -06:00
|
|
|
" -c, --check check-only mode\n"
|
|
|
|
" -f, --file=sudoers specify sudoers file location\n"
|
|
|
|
" -h, --help display help message and exit\n"
|
2022-10-04 16:11:45 -06:00
|
|
|
" -I, --no-includes do not edit include files\n"
|
2014-05-13 15:00:43 -06:00
|
|
|
" -q, --quiet less verbose (quiet) syntax error messages\n"
|
|
|
|
" -s, --strict strict syntax checking\n"
|
2018-01-26 11:20:37 -07:00
|
|
|
" -V, --version display version information and exit\n"));
|
2020-02-08 12:43:00 -07:00
|
|
|
exit(EXIT_SUCCESS);
|
1999-07-12 10:53:09 +00:00
|
|
|
}
|