1995-04-13 16:34:54 +00:00
|
|
|
/*
|
2019-04-29 07:21:51 -06:00
|
|
|
* SPDX-License-Identifier: ISC
|
|
|
|
*
|
2018-03-06 15:59:31 -07:00
|
|
|
* Copyright (c) 1996, 1998-2005, 2007-2018
|
2017-12-03 17:53:40 -07:00
|
|
|
* Todd C. Miller <Todd.Miller@sudo.ws>
|
1995-04-13 16:34:54 +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.
|
|
|
|
*
|
|
|
|
* 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.
|
1995-04-13 16:34:54 +00:00
|
|
|
*/
|
|
|
|
|
2018-10-26 08:39:09 -06:00
|
|
|
/*
|
|
|
|
* This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
|
|
|
* PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
|
|
|
*/
|
2018-10-21 08:46:05 -06:00
|
|
|
|
2004-11-19 18:39:14 +00:00
|
|
|
#include <config.h>
|
1995-04-13 16:34:54 +00:00
|
|
|
|
2001-12-14 19:52:54 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2020-05-18 06:47:04 -06:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
1995-04-13 16:34:54 +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>
|
2010-06-29 13:08:05 -04:00
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif /* HAVE_STRINGS_H */
|
2015-07-02 09:08:28 -06:00
|
|
|
#include <unistd.h>
|
2010-05-28 12:01:06 -04:00
|
|
|
#include <errno.h>
|
1995-04-13 16:34:54 +00:00
|
|
|
|
2010-11-20 10:33:23 -05:00
|
|
|
#include "tsgetgrpw.h"
|
2010-03-14 19:58:47 -04:00
|
|
|
#include "sudoers.h"
|
1999-05-17 01:36:30 +00:00
|
|
|
#include "interfaces.h"
|
2012-01-08 12:11:14 -05:00
|
|
|
#include "sudo_conf.h"
|
2018-03-06 15:09:21 -07:00
|
|
|
#include "sudo_lbuf.h"
|
2011-11-12 12:18:44 -05:00
|
|
|
#include <gram.h>
|
1995-04-13 16:34:54 +00:00
|
|
|
|
2015-02-27 07:10:20 -07:00
|
|
|
#ifndef YYDEBUG
|
|
|
|
# define YYDEBUG 0
|
|
|
|
#endif
|
|
|
|
|
2018-08-27 05:21:04 -06:00
|
|
|
enum sudoers_formats {
|
|
|
|
format_ldif,
|
|
|
|
format_sudoers
|
|
|
|
};
|
|
|
|
|
1995-04-13 16:34:54 +00:00
|
|
|
/*
|
2010-05-28 12:01:06 -04:00
|
|
|
* Function Prototypes
|
1995-04-13 16:34:54 +00:00
|
|
|
*/
|
2018-03-06 15:09:21 -07:00
|
|
|
static void dump_sudoers(struct sudo_lbuf *lbuf);
|
|
|
|
static void usage(void) __attribute__((__noreturn__));
|
2011-08-10 09:02:37 -04:00
|
|
|
static void set_runaspw(const char *);
|
|
|
|
static void set_runasgr(const char *);
|
2016-07-18 12:11:25 -06:00
|
|
|
static bool cb_runas_default(const union sudo_defs_val *);
|
2018-03-06 15:09:21 -07:00
|
|
|
static int testsudoers_error(const char *msg);
|
|
|
|
static int testsudoers_output(const char *buf);
|
1995-04-13 16:34:54 +00:00
|
|
|
|
2018-03-06 15:59:31 -07:00
|
|
|
/* tsgetgrpw.c */
|
2010-03-17 19:56:27 -04:00
|
|
|
extern void setgrfile(const char *);
|
|
|
|
extern void setgrent(void);
|
|
|
|
extern void endgrent(void);
|
|
|
|
extern struct group *getgrent(void);
|
|
|
|
extern struct group *getgrnam(const char *);
|
|
|
|
extern struct group *getgrgid(gid_t);
|
|
|
|
extern void setpwfile(const char *);
|
|
|
|
extern void setpwent(void);
|
|
|
|
extern void endpwent(void);
|
|
|
|
extern struct passwd *getpwent(void);
|
|
|
|
extern struct passwd *getpwnam(const char *);
|
|
|
|
extern struct passwd *getpwuid(uid_t);
|
2005-01-05 19:50:49 +00:00
|
|
|
|
2018-03-06 15:59:31 -07:00
|
|
|
/* gram.y */
|
2011-03-20 21:25:57 -04:00
|
|
|
extern int (*trace_print)(const char *msg);
|
|
|
|
|
2010-05-28 12:01:06 -04:00
|
|
|
/*
|
|
|
|
* Globals
|
|
|
|
*/
|
|
|
|
struct sudo_user sudo_user;
|
|
|
|
struct passwd *list_pw;
|
2011-08-10 09:02:37 -04:00
|
|
|
static char *runas_group, *runas_user;
|
2010-05-28 12:01:06 -04:00
|
|
|
|
|
|
|
#if defined(SUDO_DEVEL) && defined(__OpenBSD__)
|
|
|
|
extern char *malloc_options;
|
|
|
|
#endif
|
2015-02-27 07:10:20 -07:00
|
|
|
#if YYDEBUG
|
2012-09-14 16:19:25 -04:00
|
|
|
extern int sudoersdebug;
|
2010-05-28 12:01:06 -04:00
|
|
|
#endif
|
|
|
|
|
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[])
|
1995-04-13 16:34:54 +00:00
|
|
|
{
|
2018-08-27 05:21:04 -06:00
|
|
|
enum sudoers_formats input_format = format_sudoers;
|
2004-10-26 22:25:34 +00:00
|
|
|
struct cmndspec *cs;
|
|
|
|
struct privilege *priv;
|
|
|
|
struct userspec *us;
|
2011-08-10 09:02:37 -04:00
|
|
|
char *p, *grfile, *pwfile;
|
2013-12-05 15:51:56 -07:00
|
|
|
const char *errstr;
|
2011-03-29 14:40:07 -04:00
|
|
|
int match, host_match, runas_match, cmnd_match;
|
2018-01-29 14:02:21 -07:00
|
|
|
int ch, dflag, exitcode = EXIT_FAILURE;
|
2018-03-06 15:09:21 -07:00
|
|
|
struct sudo_lbuf lbuf;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(main, SUDOERS_DEBUG_MAIN);
|
2008-11-19 17:01:20 +00:00
|
|
|
|
|
|
|
#if defined(SUDO_DEVEL) && defined(__OpenBSD__)
|
2016-11-25 09:04:00 -07:00
|
|
|
malloc_options = "S";
|
2008-11-19 17:01:20 +00:00
|
|
|
#endif
|
2015-02-27 07:10:20 -07:00
|
|
|
#if YYDEBUG
|
2012-09-14 16:19:25 -04:00
|
|
|
sudoersdebug = 1;
|
2004-10-26 22:25:34 +00:00
|
|
|
#endif
|
1996-08-17 23:17:23 +00:00
|
|
|
|
2013-12-01 19:12:21 -07:00
|
|
|
initprogname(argc > 0 ? argv[0] : "testsudoers");
|
1995-04-13 16:34:54 +00:00
|
|
|
|
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 19:58:42 -06:00
|
|
|
sudo_warn_set_locale_func(sudoers_warn_setlocale);
|
2012-01-08 12:11:14 -05:00
|
|
|
bindtextdomain("sudoers", LOCALEDIR); /* XXX - should have own domain */
|
|
|
|
textdomain("sudoers");
|
|
|
|
|
2018-03-06 15:59:31 -07:00
|
|
|
/* No word wrap on output. */
|
2018-03-06 15:09:21 -07:00
|
|
|
sudo_lbuf_init(&lbuf, testsudoers_output, 0, NULL, 0);
|
|
|
|
|
2014-10-26 08:33:08 -06:00
|
|
|
/* Initialize the debug subsystem. */
|
2015-07-07 13:17:50 -06:00
|
|
|
if (sudo_conf_read(NULL, SUDO_CONF_DEBUG) == -1)
|
2018-01-29 14:02:21 -07:00
|
|
|
goto done;
|
|
|
|
if (!sudoers_debug_register(getprogname(), sudo_conf_debug_files(getprogname())))
|
|
|
|
goto done;
|
2014-10-23 09:40:36 -06:00
|
|
|
|
2004-10-26 22:25:34 +00:00
|
|
|
dflag = 0;
|
2011-08-10 09:02:37 -04:00
|
|
|
grfile = pwfile = NULL;
|
2018-08-27 05:21:04 -06:00
|
|
|
while ((ch = getopt(argc, argv, "dg:G:h:i:P:p:tu:U:")) != -1) {
|
2004-10-26 22:25:34 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'd':
|
|
|
|
dflag = 1;
|
|
|
|
break;
|
2008-01-15 14:40:48 +00:00
|
|
|
case 'G':
|
2019-10-20 10:21:29 -06:00
|
|
|
sudoers_gid = (gid_t)sudo_strtoid(optarg, &errstr);
|
2013-12-05 15:51:56 -07:00
|
|
|
if (errstr != NULL)
|
2019-10-19 14:26:41 -06:00
|
|
|
sudo_fatalx("group-ID %s: %s", optarg, errstr);
|
2005-01-05 19:50:49 +00:00
|
|
|
break;
|
2008-01-15 14:40:48 +00:00
|
|
|
case 'g':
|
|
|
|
runas_group = optarg;
|
2016-09-15 13:16:38 -06:00
|
|
|
SET(sudo_user.flags, RUNAS_GROUP_SPECIFIED);
|
2008-01-15 14:40:48 +00:00
|
|
|
break;
|
2018-08-27 05:21:04 -06:00
|
|
|
case 'h':
|
|
|
|
user_host = optarg;
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
if (strcasecmp(optarg, "ldif") == 0) {
|
|
|
|
input_format = format_ldif;
|
|
|
|
} else if (strcasecmp(optarg, "sudoers") == 0) {
|
|
|
|
input_format = format_sudoers;
|
|
|
|
} else {
|
|
|
|
sudo_warnx(U_("unsupported input format %s"), optarg);
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
break;
|
2005-01-05 19:50:49 +00:00
|
|
|
case 'p':
|
|
|
|
pwfile = optarg;
|
|
|
|
break;
|
2012-05-21 15:39:24 -04:00
|
|
|
case 'P':
|
|
|
|
grfile = optarg;
|
|
|
|
break;
|
2011-03-20 21:25:57 -04:00
|
|
|
case 't':
|
2018-03-06 15:09:21 -07:00
|
|
|
trace_print = testsudoers_error;
|
2011-03-20 21:25:57 -04:00
|
|
|
break;
|
2012-05-18 14:29:29 -04:00
|
|
|
case 'U':
|
2019-10-20 10:21:29 -06:00
|
|
|
sudoers_uid = (uid_t)sudo_strtoid(optarg, &errstr);
|
2013-12-05 15:51:56 -07:00
|
|
|
if (errstr != NULL)
|
2019-10-19 14:26:41 -06:00
|
|
|
sudo_fatalx("user-ID %s: %s", optarg, errstr);
|
2012-05-18 14:29:29 -04:00
|
|
|
break;
|
2004-10-26 22:25:34 +00:00
|
|
|
case 'u':
|
2007-11-21 20:12:00 +00:00
|
|
|
runas_user = optarg;
|
2016-09-15 13:16:38 -06:00
|
|
|
SET(sudo_user.flags, RUNAS_USER_SPECIFIED);
|
2004-10-26 22:25:34 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
break;
|
|
|
|
}
|
1996-08-17 23:17:23 +00:00
|
|
|
}
|
2004-10-26 22:25:34 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
2005-01-05 19:50:49 +00:00
|
|
|
|
|
|
|
/* Set group/passwd file and init the cache. */
|
2009-09-19 19:13:10 +00:00
|
|
|
if (grfile)
|
|
|
|
setgrfile(grfile);
|
|
|
|
if (pwfile)
|
|
|
|
setpwfile(pwfile);
|
2005-01-05 19:50:49 +00:00
|
|
|
|
2004-10-26 22:25:34 +00:00
|
|
|
if (argc < 2) {
|
|
|
|
if (!dflag)
|
|
|
|
usage();
|
2011-11-07 08:44:05 -05:00
|
|
|
user_name = argc ? *argv++ : "root";
|
2004-11-16 04:24:11 +00:00
|
|
|
user_cmnd = user_base = "true";
|
2011-11-07 08:44:05 -05:00
|
|
|
argc = 0;
|
2000-03-23 04:38:22 +00:00
|
|
|
} else {
|
2011-11-07 08:44:05 -05:00
|
|
|
user_name = *argv++;
|
|
|
|
user_cmnd = *argv++;
|
2004-11-16 04:24:11 +00:00
|
|
|
if ((p = strrchr(user_cmnd, '/')) != NULL)
|
|
|
|
user_base = p + 1;
|
|
|
|
else
|
|
|
|
user_base = user_cmnd;
|
2011-03-31 14:06:11 -04:00
|
|
|
argc -= 2;
|
2000-03-23 04:38:22 +00:00
|
|
|
}
|
2011-01-07 11:56:15 -05:00
|
|
|
if ((sudo_user.pw = sudo_getpwnam(user_name)) == NULL)
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_fatalx(U_("unknown user: %s"), user_name);
|
2000-03-23 04:09:53 +00:00
|
|
|
|
2004-10-26 22:25:34 +00:00
|
|
|
if (user_host == NULL) {
|
2015-02-19 20:28:02 -07:00
|
|
|
if ((user_host = sudo_gethostname()) == NULL)
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_fatal("gethostname");
|
2004-10-26 22:25:34 +00:00
|
|
|
}
|
|
|
|
if ((p = strchr(user_host, '.'))) {
|
|
|
|
*p = '\0';
|
2015-06-17 06:49:59 -06:00
|
|
|
if ((user_shost = strdup(user_host)) == NULL)
|
2015-06-19 14:51:17 -06:00
|
|
|
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
|
2004-10-26 22:25:34 +00:00
|
|
|
*p = '.';
|
|
|
|
} else {
|
|
|
|
user_shost = user_host;
|
2004-03-24 23:06:34 +00:00
|
|
|
}
|
2013-08-15 14:26:51 -06:00
|
|
|
user_runhost = user_host;
|
|
|
|
user_srunhost = user_shost;
|
1995-12-18 03:04:55 +00:00
|
|
|
|
2011-03-31 14:06:11 -04:00
|
|
|
/* Fill in user_args from argv. */
|
|
|
|
if (argc > 0) {
|
2004-10-26 22:25:34 +00:00
|
|
|
char *to, **from;
|
|
|
|
size_t size, n;
|
1995-12-18 03:04:55 +00:00
|
|
|
|
2011-11-07 08:44:05 -05:00
|
|
|
for (size = 0, from = argv; *from; from++)
|
2008-01-15 14:40:48 +00:00
|
|
|
size += strlen(*from) + 1;
|
|
|
|
|
2015-06-17 06:49:59 -06:00
|
|
|
if ((user_args = malloc(size)) == NULL)
|
2015-06-19 14:51:17 -06:00
|
|
|
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
|
2011-11-07 08:44:05 -05:00
|
|
|
for (to = user_args, from = argv; *from; from++) {
|
2004-10-26 22:25:34 +00:00
|
|
|
n = strlcpy(to, *from, size - (to - user_args));
|
|
|
|
if (n >= size - (to - user_args))
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_fatalx(U_("internal error, %s overflow"), getprogname());
|
2004-10-26 22:25:34 +00:00
|
|
|
to += n;
|
|
|
|
*to++ = ' ';
|
|
|
|
}
|
|
|
|
*--to = '\0';
|
1995-12-18 03:04:55 +00:00
|
|
|
}
|
|
|
|
|
2004-10-26 22:25:34 +00:00
|
|
|
/* Initialize default values. */
|
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"));
|
1995-07-27 02:00:21 +00:00
|
|
|
|
2016-08-09 13:14:31 -06:00
|
|
|
/* Set group_plugin callback. */
|
|
|
|
sudo_defs_table[I_GROUP_PLUGIN].callback = cb_group_plugin;
|
|
|
|
|
2011-08-09 14:54:42 -04:00
|
|
|
/* Set runas callback. */
|
2011-08-10 09:02:37 -04:00
|
|
|
sudo_defs_table[I_RUNAS_DEFAULT].callback = cb_runas_default;
|
2011-08-09 14:54:42 -04:00
|
|
|
|
2016-07-20 14:16:00 -06:00
|
|
|
/* Set locale callback. */
|
|
|
|
sudo_defs_table[I_SUDOERS_LOCALE].callback = sudoers_locale_callback;
|
|
|
|
|
2004-10-26 22:25:34 +00:00
|
|
|
/* Load ip addr/mask for each interface. */
|
2015-06-17 06:49:59 -06:00
|
|
|
if (get_net_ifs(&p) > 0) {
|
|
|
|
if (!set_interfaces(p))
|
2020-08-12 13:45:09 -06:00
|
|
|
sudo_fatal("%s", U_("unable to parse network address list"));
|
2015-06-17 06:49:59 -06:00
|
|
|
}
|
1995-07-27 02:00:21 +00:00
|
|
|
|
2004-10-26 22:25:34 +00:00
|
|
|
/* Allocate space for data structures in the parser. */
|
2019-11-05 15:18:34 -07:00
|
|
|
init_parser("sudoers", false, true);
|
2004-10-26 22:25:34 +00:00
|
|
|
|
2016-08-10 10:56:05 -06:00
|
|
|
/*
|
|
|
|
* Set runas passwd/group entries based on command line or sudoers.
|
|
|
|
* Note that if runas_group was specified without runas_user we
|
|
|
|
* run the command as the invoking user.
|
|
|
|
*/
|
|
|
|
if (runas_group != NULL) {
|
|
|
|
set_runasgr(runas_group);
|
|
|
|
set_runaspw(runas_user ? runas_user : user_name);
|
|
|
|
} else
|
|
|
|
set_runaspw(runas_user ? runas_user : def_runas_default);
|
|
|
|
|
2018-08-27 05:21:04 -06:00
|
|
|
/* Parse the policy file. */
|
2016-07-20 19:58:42 -06:00
|
|
|
sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, NULL);
|
2018-08-27 05:21:04 -06:00
|
|
|
switch (input_format) {
|
|
|
|
case format_ldif:
|
2020-08-07 14:20:45 -06:00
|
|
|
if (!sudoers_parse_ldif(&parsed_policy, stdin, NULL, true)) {
|
|
|
|
(void) puts("Parse error in LDIF");
|
|
|
|
parse_error = true;
|
|
|
|
}
|
2018-08-27 05:21:04 -06:00
|
|
|
break;
|
|
|
|
case format_sudoers:
|
2020-08-07 14:20:45 -06:00
|
|
|
if (sudoersparse() != 0 || parse_error)
|
2018-08-27 05:21:04 -06:00
|
|
|
parse_error = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
sudo_fatalx("error: unhandled input %d", input_format);
|
2011-03-29 13:09:58 -04:00
|
|
|
}
|
2020-08-07 14:20:45 -06:00
|
|
|
if (!parse_error)
|
|
|
|
(void) puts("Parses OK");
|
2004-10-26 22:25:34 +00:00
|
|
|
|
2018-08-02 14:06:36 -06:00
|
|
|
if (!update_defaults(&parsed_policy, NULL, SETDEF_ALL, false))
|
2020-08-07 14:20:45 -06:00
|
|
|
(void) puts("Problem with defaults entries");
|
2004-10-26 22:25:34 +00:00
|
|
|
|
|
|
|
if (dflag) {
|
|
|
|
(void) putchar('\n');
|
2018-03-06 15:09:21 -07:00
|
|
|
dump_sudoers(&lbuf);
|
2012-01-08 12:11:14 -05:00
|
|
|
if (argc < 2) {
|
|
|
|
exitcode = parse_error ? 1 : 0;
|
|
|
|
goto done;
|
|
|
|
}
|
2004-10-26 22:25:34 +00:00
|
|
|
}
|
1995-07-27 02:00:21 +00:00
|
|
|
|
2011-03-29 14:40:07 -04:00
|
|
|
/* This loop must match the one in sudo_file_lookup() */
|
2004-10-26 22:25:34 +00:00
|
|
|
printf("\nEntries for user %s:\n", user_name);
|
2011-03-29 14:40:07 -04:00
|
|
|
match = UNSPEC;
|
2018-07-26 15:12:33 -06:00
|
|
|
TAILQ_FOREACH_REVERSE(us, &parsed_policy.userspecs, userspec_list, entries) {
|
|
|
|
if (userlist_matches(&parsed_policy, sudo_user.pw, &us->users) != ALLOW)
|
2007-07-06 00:21:16 +00:00
|
|
|
continue;
|
2013-10-22 09:08:38 -06:00
|
|
|
TAILQ_FOREACH_REVERSE(priv, &us->privileges, privilege_list, entries) {
|
2018-03-06 15:09:21 -07:00
|
|
|
sudo_lbuf_append(&lbuf, "\n");
|
2018-07-26 15:12:33 -06:00
|
|
|
sudoers_format_privilege(&lbuf, &parsed_policy, priv, false);
|
2018-03-06 15:09:21 -07:00
|
|
|
sudo_lbuf_print(&lbuf);
|
2018-07-26 15:12:33 -06:00
|
|
|
host_match = hostlist_matches(&parsed_policy, sudo_user.pw,
|
|
|
|
&priv->hostlist);
|
2011-03-29 14:40:07 -04:00
|
|
|
if (host_match == ALLOW) {
|
2007-07-06 00:21:16 +00:00
|
|
|
puts("\thost matched");
|
2013-10-22 09:08:38 -06:00
|
|
|
TAILQ_FOREACH_REVERSE(cs, &priv->cmndlist, cmndspec_list, entries) {
|
2018-07-26 15:12:33 -06:00
|
|
|
runas_match = runaslist_matches(&parsed_policy,
|
|
|
|
cs->runasuserlist, cs->runasgrouplist, NULL, NULL);
|
2011-03-29 14:40:07 -04:00
|
|
|
if (runas_match == ALLOW) {
|
2007-07-06 00:21:16 +00:00
|
|
|
puts("\trunas matched");
|
2020-09-09 15:26:44 -06:00
|
|
|
cmnd_match = cmnd_matches(&parsed_policy, cs->cmnd,
|
2020-09-09 15:26:45 -06:00
|
|
|
cs->runchroot, NULL);
|
2011-03-29 14:40:07 -04:00
|
|
|
if (cmnd_match != UNSPEC)
|
|
|
|
match = cmnd_match;
|
|
|
|
printf("\tcmnd %s\n", match == ALLOW ? "allowed" :
|
|
|
|
match == DENY ? "denied" : "unmatched");
|
2004-10-26 22:25:34 +00:00
|
|
|
}
|
2007-07-06 00:21:16 +00:00
|
|
|
}
|
2007-08-31 01:21:26 +00:00
|
|
|
} else
|
2016-07-20 19:58:42 -06:00
|
|
|
puts(U_("\thost unmatched"));
|
1995-07-27 02:00:21 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-20 19:58:42 -06:00
|
|
|
puts(match == ALLOW ? U_("\nCommand allowed") :
|
|
|
|
match == DENY ? U_("\nCommand denied") : U_("\nCommand unmatched"));
|
2000-01-11 18:20:41 +00:00
|
|
|
|
2011-03-29 13:09:58 -04:00
|
|
|
/*
|
|
|
|
* Exit codes:
|
|
|
|
* 0 - parsed OK and command matched.
|
|
|
|
* 1 - parse error
|
|
|
|
* 2 - command not matched
|
|
|
|
* 3 - command denied
|
|
|
|
*/
|
2012-01-08 12:11:14 -05:00
|
|
|
exitcode = parse_error ? 1 : (match == ALLOW ? 0 : match + 3);
|
|
|
|
done:
|
2018-03-06 15:09:21 -07:00
|
|
|
sudo_lbuf_destroy(&lbuf);
|
2016-05-11 07:06:45 -06:00
|
|
|
sudo_freepwcache();
|
|
|
|
sudo_freegrcache();
|
2012-01-08 12:11:14 -05:00
|
|
|
sudo_debug_exit_int(__func__, __FILE__, __LINE__, sudo_debug_subsys, exitcode);
|
|
|
|
exit(exitcode);
|
1995-07-27 02:00:21 +00:00
|
|
|
}
|
|
|
|
|
2011-08-10 09:02:37 -04:00
|
|
|
static void
|
2011-08-09 14:54:42 -04:00
|
|
|
set_runaspw(const char *user)
|
2008-01-15 14:40:48 +00:00
|
|
|
{
|
2013-12-05 15:51:56 -07:00
|
|
|
struct passwd *pw = NULL;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(set_runaspw, SUDOERS_DEBUG_UTIL);
|
2012-01-08 12:11:14 -05:00
|
|
|
|
2008-01-15 14:40:48 +00:00
|
|
|
if (*user == '#') {
|
2013-12-05 15:51:56 -07:00
|
|
|
const char *errstr;
|
2019-10-20 10:21:29 -06:00
|
|
|
uid_t uid = sudo_strtoid(user + 1, &errstr);
|
2013-12-05 15:51:56 -07:00
|
|
|
if (errstr == NULL) {
|
|
|
|
if ((pw = sudo_getpwuid(uid)) == NULL)
|
2015-04-16 13:19:04 -06:00
|
|
|
pw = sudo_fakepwnam(user, user_gid);
|
2013-12-05 15:51:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pw == NULL) {
|
|
|
|
if ((pw = sudo_getpwnam(user)) == NULL)
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_fatalx(U_("unknown user: %s"), user);
|
2008-01-15 14:40:48 +00:00
|
|
|
}
|
2013-12-05 15:51:56 -07:00
|
|
|
if (runas_pw != NULL)
|
|
|
|
sudo_pw_delref(runas_pw);
|
|
|
|
runas_pw = pw;
|
2012-01-08 12:11:14 -05:00
|
|
|
debug_return;
|
2008-01-15 14:40:48 +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)
|
2008-01-15 14:40:48 +00:00
|
|
|
{
|
2013-12-05 15:51:56 -07:00
|
|
|
struct group *gr = NULL;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(set_runasgr, SUDOERS_DEBUG_UTIL);
|
2012-01-08 12:11:14 -05:00
|
|
|
|
2008-01-15 14:40:48 +00:00
|
|
|
if (*group == '#') {
|
2013-12-05 15:51:56 -07:00
|
|
|
const char *errstr;
|
2019-10-20 10:21:29 -06:00
|
|
|
gid_t gid = sudo_strtoid(group + 1, &errstr);
|
2013-12-05 15:51:56 -07:00
|
|
|
if (errstr == NULL) {
|
|
|
|
if ((gr = sudo_getgrgid(gid)) == NULL)
|
|
|
|
gr = sudo_fakegrnam(group);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (gr == NULL) {
|
|
|
|
if ((gr = sudo_getgrnam(group)) == NULL)
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_fatalx(U_("unknown group: %s"), group);
|
2008-01-15 14:40:48 +00:00
|
|
|
}
|
2013-12-05 15:51:56 -07:00
|
|
|
if (runas_gr != NULL)
|
|
|
|
sudo_gr_delref(runas_gr);
|
|
|
|
runas_gr = gr;
|
2012-01-08 12:11:14 -05: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
|
2016-07-18 12:11:25 -06:00
|
|
|
cb_runas_default(const union sudo_defs_val *sd_un)
|
2011-08-10 09:02:37 -04:00
|
|
|
{
|
|
|
|
/* Only reset runaspw if user didn't specify one. */
|
|
|
|
if (!runas_user && !runas_group)
|
2016-07-18 12:11:25 -06:00
|
|
|
set_runaspw(sd_un->str);
|
2011-12-02 11:27:33 -05:00
|
|
|
return true;
|
2008-01-15 14:40:48 +00:00
|
|
|
}
|
|
|
|
|
2005-01-05 19:50:49 +00:00
|
|
|
void
|
2010-04-22 18:09:53 -04:00
|
|
|
sudo_setspent(void)
|
2005-01-05 19:50:49 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-04-22 18:09:53 -04:00
|
|
|
sudo_endspent(void)
|
2005-01-05 19:50:49 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-09-27 16:02:10 +00:00
|
|
|
FILE *
|
2020-09-25 15:09:45 -06:00
|
|
|
open_sudoers(const char *file, bool doedit, bool *keepopen)
|
2004-09-27 16:02:10 +00:00
|
|
|
{
|
2012-05-21 15:39:24 -04:00
|
|
|
struct stat sb;
|
|
|
|
FILE *fp = NULL;
|
2020-09-25 15:09:45 -06:00
|
|
|
const char *base;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(open_sudoers, SUDOERS_DEBUG_UTIL);
|
2012-01-08 12:11:14 -05:00
|
|
|
|
2020-09-25 15:09:45 -06:00
|
|
|
base = strrchr(file, '/');
|
|
|
|
if (base != NULL)
|
|
|
|
base++;
|
2020-05-20 13:10:53 -06:00
|
|
|
else
|
2020-09-25 15:09:45 -06:00
|
|
|
base = file;
|
2012-05-21 15:39:24 -04:00
|
|
|
|
2020-09-25 15:09:45 -06:00
|
|
|
switch (sudo_secure_file(file, sudoers_uid, sudoers_gid, &sb)) {
|
2012-05-21 15:39:24 -04:00
|
|
|
case SUDO_PATH_SECURE:
|
2020-09-25 15:09:45 -06:00
|
|
|
fp = fopen(file, "r");
|
2012-05-21 15:39:24 -04:00
|
|
|
break;
|
|
|
|
case SUDO_PATH_MISSING:
|
2020-09-25 15:09:45 -06:00
|
|
|
sudo_warn("unable to stat %s", base);
|
2012-05-21 15:39:24 -04:00
|
|
|
break;
|
|
|
|
case SUDO_PATH_BAD_TYPE:
|
2020-09-25 15:09:45 -06:00
|
|
|
sudo_warnx("%s is not a regular file", base);
|
2012-05-21 15:39:24 -04:00
|
|
|
break;
|
|
|
|
case SUDO_PATH_WRONG_OWNER:
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_warnx("%s should be owned by uid %u",
|
2020-09-25 15:09:45 -06:00
|
|
|
base, (unsigned int) sudoers_uid);
|
2012-05-21 15:39:24 -04:00
|
|
|
break;
|
|
|
|
case SUDO_PATH_WORLD_WRITABLE:
|
2020-09-25 15:09:45 -06:00
|
|
|
sudo_warnx("%s is world writable", base);
|
2012-05-21 15:39:24 -04:00
|
|
|
break;
|
|
|
|
case SUDO_PATH_GROUP_WRITABLE:
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_warnx("%s should be owned by gid %u",
|
2020-09-25 15:09:45 -06:00
|
|
|
base, (unsigned int) sudoers_gid);
|
2012-05-21 15:39:24 -04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* NOTREACHED */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug_return_ptr(fp);
|
2004-09-27 16:02:10 +00:00
|
|
|
}
|
|
|
|
|
2015-06-17 06:49:59 -06:00
|
|
|
bool
|
2010-04-22 18:09:53 -04:00
|
|
|
init_envtables(void)
|
2001-12-11 22:55:23 +00:00
|
|
|
{
|
2015-06-17 06:49:59 -06:00
|
|
|
return(true);
|
2001-12-11 22:55:23 +00:00
|
|
|
}
|
|
|
|
|
2014-04-10 16:11:47 -06:00
|
|
|
bool
|
2010-04-20 17:00:31 -04:00
|
|
|
set_perms(int perm)
|
|
|
|
{
|
2014-04-10 16:11:47 -06:00
|
|
|
return true;
|
2010-04-20 17:00:31 -04:00
|
|
|
}
|
|
|
|
|
2014-04-10 16:11:47 -06:00
|
|
|
bool
|
2010-04-20 17:00:31 -04:00
|
|
|
restore_perms(void)
|
1995-04-13 16:34:54 +00:00
|
|
|
{
|
2014-04-10 16:11:47 -06:00
|
|
|
return true;
|
2004-10-26 22:25:34 +00:00
|
|
|
}
|
1995-04-13 16:34:54 +00:00
|
|
|
|
2020-10-26 16:10:40 -06:00
|
|
|
void
|
|
|
|
update_eventlog_config(void)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-09 15:26:44 -06:00
|
|
|
int
|
|
|
|
set_cmnd_path(const char *runchroot)
|
|
|
|
{
|
|
|
|
return FOUND;
|
|
|
|
}
|
|
|
|
|
2018-03-06 15:09:21 -07:00
|
|
|
static bool
|
|
|
|
print_defaults(struct sudo_lbuf *lbuf)
|
2004-10-26 22:25:34 +00:00
|
|
|
{
|
2018-03-06 15:09:21 -07:00
|
|
|
struct defaults *def, *next;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(print_defaults, SUDOERS_DEBUG_UTIL);
|
2004-10-26 22:25:34 +00:00
|
|
|
|
2018-07-26 15:12:33 -06:00
|
|
|
TAILQ_FOREACH_SAFE(def, &parsed_policy.defaults, entries, next)
|
|
|
|
sudoers_format_default_line(lbuf, &parsed_policy, def, &next, false);
|
2012-01-08 12:11:14 -05:00
|
|
|
|
2018-03-06 15:09:21 -07:00
|
|
|
debug_return_bool(!sudo_lbuf_error(lbuf));
|
2004-10-26 22:25:34 +00:00
|
|
|
}
|
1995-04-13 16:34:54 +00:00
|
|
|
|
2018-03-06 15:09:21 -07:00
|
|
|
static int
|
2018-08-24 09:52:53 -06:00
|
|
|
print_alias(struct sudoers_parse_tree *parse_tree, struct alias *a, void *v)
|
2004-10-26 22:25:34 +00:00
|
|
|
{
|
2018-08-24 09:52:53 -06:00
|
|
|
struct sudo_lbuf *lbuf = v;
|
2004-10-26 22:25:34 +00:00
|
|
|
struct member *m;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(print_alias, SUDOERS_DEBUG_UTIL);
|
2004-10-26 22:25:34 +00:00
|
|
|
|
2018-03-06 15:09:21 -07:00
|
|
|
sudo_lbuf_append(lbuf, "%s %s = ", alias_type_to_string(a->type),
|
|
|
|
a->name);
|
2013-10-22 09:08:38 -06:00
|
|
|
TAILQ_FOREACH(m, &a->members, entries) {
|
|
|
|
if (m != TAILQ_FIRST(&a->members))
|
2018-03-06 15:09:21 -07:00
|
|
|
sudo_lbuf_append(lbuf, ", ");
|
2018-08-24 09:52:53 -06:00
|
|
|
sudoers_format_member(lbuf, parse_tree, m, NULL, UNSPEC);
|
1996-07-23 05:18:10 +00:00
|
|
|
}
|
2018-03-06 15:09:21 -07:00
|
|
|
sudo_lbuf_append(lbuf, "\n");
|
|
|
|
|
|
|
|
debug_return_int(sudo_lbuf_error(lbuf) ? -1 : 0);
|
2004-10-26 22:25:34 +00:00
|
|
|
}
|
1996-07-23 05:18:10 +00:00
|
|
|
|
2018-03-06 15:09:21 -07:00
|
|
|
static bool
|
|
|
|
print_aliases(struct sudo_lbuf *lbuf)
|
|
|
|
{
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(print_aliases, SUDOERS_DEBUG_UTIL);
|
2018-03-06 15:09:21 -07:00
|
|
|
|
2018-07-26 15:12:33 -06:00
|
|
|
alias_apply(&parsed_policy, print_alias, lbuf);
|
2015-02-19 10:02:20 -07:00
|
|
|
|
2018-03-06 15:09:21 -07:00
|
|
|
debug_return_bool(!sudo_lbuf_error(lbuf));
|
|
|
|
}
|
2015-02-19 10:02:20 -07:00
|
|
|
|
2018-03-06 15:09:21 -07:00
|
|
|
static void
|
|
|
|
dump_sudoers(struct sudo_lbuf *lbuf)
|
2004-10-26 22:25:34 +00:00
|
|
|
{
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(dump_sudoers, SUDOERS_DEBUG_UTIL);
|
2004-10-26 22:25:34 +00:00
|
|
|
|
2018-03-06 15:09:21 -07:00
|
|
|
/* Print Defaults */
|
|
|
|
if (!print_defaults(lbuf))
|
|
|
|
goto done;
|
|
|
|
if (lbuf->len > 0) {
|
|
|
|
sudo_lbuf_print(lbuf);
|
|
|
|
sudo_lbuf_append(lbuf, "\n");
|
2013-10-22 09:08:38 -06:00
|
|
|
}
|
2018-03-06 15:09:21 -07:00
|
|
|
|
|
|
|
/* Print Aliases */
|
|
|
|
if (!print_aliases(lbuf))
|
|
|
|
goto done;
|
|
|
|
if (lbuf->len > 1) {
|
|
|
|
sudo_lbuf_print(lbuf);
|
|
|
|
sudo_lbuf_append(lbuf, "\n");
|
2004-10-26 22:25:34 +00:00
|
|
|
}
|
1996-08-17 23:17:23 +00:00
|
|
|
|
2018-03-06 15:09:21 -07:00
|
|
|
/* Print User_Specs */
|
2018-07-26 15:12:33 -06:00
|
|
|
if (!sudoers_format_userspecs(lbuf, &parsed_policy, NULL, false, true))
|
2018-03-06 15:09:21 -07:00
|
|
|
goto done;
|
|
|
|
if (lbuf->len > 1) {
|
|
|
|
sudo_lbuf_print(lbuf);
|
|
|
|
}
|
2004-10-26 22:25:34 +00:00
|
|
|
|
2018-03-06 15:09:21 -07:00
|
|
|
done:
|
|
|
|
if (sudo_lbuf_error(lbuf)) {
|
|
|
|
if (errno == ENOMEM)
|
|
|
|
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
|
1995-04-13 16:34:54 +00:00
|
|
|
}
|
2018-03-06 15:09:21 -07:00
|
|
|
|
2012-01-08 12:11:14 -05:00
|
|
|
debug_return;
|
2004-10-26 22:25:34 +00:00
|
|
|
}
|
1995-04-13 16:34:54 +00:00
|
|
|
|
2018-03-06 15:09:21 -07:00
|
|
|
static int
|
|
|
|
testsudoers_output(const char *buf)
|
2004-10-26 22:25:34 +00:00
|
|
|
{
|
2018-03-06 15:09:21 -07:00
|
|
|
return fputs(buf, stdout);
|
2004-10-26 22:25:34 +00:00
|
|
|
}
|
|
|
|
|
2018-03-06 15:09:21 -07:00
|
|
|
static int
|
|
|
|
testsudoers_error(const char *buf)
|
2011-03-20 21:25:57 -04:00
|
|
|
{
|
2018-03-06 15:09:21 -07:00
|
|
|
return fputs(buf, stderr);
|
2011-03-20 21:25:57 -04:00
|
|
|
}
|
|
|
|
|
2018-03-06 15:09:21 -07:00
|
|
|
static void
|
2010-04-22 18:09:53 -04:00
|
|
|
usage(void)
|
2004-10-26 22:25:34 +00:00
|
|
|
{
|
2018-08-27 05:21:04 -06:00
|
|
|
(void) fprintf(stderr, "usage: %s [-dt] [-G sudoers_gid] [-g group] [-h host] [-i input_format] [-P grfile] [-p pwfile] [-U sudoers_uid] [-u user] <user> <command> [args]\n", getprogname());
|
2020-02-08 12:43:00 -07:00
|
|
|
exit(EXIT_FAILURE);
|
1995-04-13 16:34:54 +00:00
|
|
|
}
|