1993-11-27 23:42:49 +00:00
|
|
|
/*
|
2007-06-23 23:58:54 +00:00
|
|
|
* Copyright (c) 2004-2005, 2007 Todd C. Miller <Todd.Miller@courtesan.com>
|
1993-06-11 22:03:45 +00:00
|
|
|
*
|
2004-02-13 21:36:43 +00:00
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
1999-07-22 12:51:18 +00:00
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
1993-10-04 19:10:33 +00:00
|
|
|
*/
|
|
|
|
|
2004-11-19 18:39:14 +00:00
|
|
|
#include <config.h>
|
1994-03-12 18:36:53 +00:00
|
|
|
|
2001-12-14 19:52:54 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/param.h>
|
1993-06-11 22:03:45 +00:00
|
|
|
#include <stdio.h>
|
1995-09-23 20:47:54 +00:00
|
|
|
#ifdef STDC_HEADERS
|
1999-08-28 10:00:22 +00:00
|
|
|
# include <stdlib.h>
|
2001-12-14 19:52:54 +00:00
|
|
|
# include <stddef.h>
|
|
|
|
#else
|
|
|
|
# ifdef HAVE_STDLIB_H
|
|
|
|
# include <stdlib.h>
|
|
|
|
# endif
|
1995-09-23 20:47:54 +00:00
|
|
|
#endif /* STDC_HEADERS */
|
1994-03-12 18:36:53 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
1999-08-28 10:00:22 +00:00
|
|
|
# include <string.h>
|
2001-12-14 19:52:54 +00:00
|
|
|
#else
|
|
|
|
# ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
# endif
|
1994-03-12 18:36:53 +00:00
|
|
|
#endif /* HAVE_STRING_H */
|
2001-12-14 19:52:54 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif /* HAVE_UNISTD_H */
|
1993-06-11 22:03:45 +00:00
|
|
|
#include <ctype.h>
|
1996-05-28 02:42:57 +00:00
|
|
|
#include <pwd.h>
|
2005-04-24 23:22:19 +00:00
|
|
|
#include <grp.h>
|
1993-06-11 22:03:45 +00:00
|
|
|
|
|
|
|
#include "sudo.h"
|
1999-07-22 12:51:18 +00:00
|
|
|
#include "parse.h"
|
2007-08-19 20:48:09 +00:00
|
|
|
#include "lbuf.h"
|
2004-11-19 18:39:14 +00:00
|
|
|
#include <gram.h>
|
1999-08-28 10:00:22 +00:00
|
|
|
|
1998-11-18 04:16:13 +00:00
|
|
|
#ifndef lint
|
2005-01-27 15:42:30 +00:00
|
|
|
__unused static const char rcsid[] = "$Sudo$";
|
1998-11-18 04:16:13 +00:00
|
|
|
#endif /* lint */
|
|
|
|
|
2007-08-22 22:32:00 +00:00
|
|
|
/* Characters that must be quoted in sudoers */
|
2007-11-08 12:24:41 +00:00
|
|
|
#define SUDOERS_QUOTED ":\\,=#\""
|
2007-08-22 22:32:00 +00:00
|
|
|
|
2007-12-31 12:39:52 +00:00
|
|
|
/* sudoers nsswitch routines */
|
|
|
|
struct sudo_nss sudo_nss_file = {
|
|
|
|
&sudo_nss_file,
|
|
|
|
NULL,
|
|
|
|
sudo_file_open,
|
|
|
|
sudo_file_close,
|
|
|
|
sudo_file_parse,
|
|
|
|
sudo_file_setdefs,
|
2007-12-31 15:08:30 +00:00
|
|
|
sudo_file_lookup,
|
|
|
|
sudo_file_display_privs,
|
|
|
|
sudo_file_display_cmnd
|
2007-12-31 12:39:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parser externs.
|
|
|
|
*/
|
|
|
|
extern FILE *yyin;
|
|
|
|
extern char *errorfile;
|
|
|
|
extern int errorlineno, parse_error;
|
|
|
|
|
2004-11-15 18:00:29 +00:00
|
|
|
/*
|
|
|
|
* Local prototypes.
|
|
|
|
*/
|
2007-08-19 20:48:09 +00:00
|
|
|
static void print_member __P((struct lbuf *, char *, int, int, int));
|
2005-01-02 00:31:08 +00:00
|
|
|
static void display_defaults __P((struct passwd *));
|
|
|
|
static void display_bound_defaults __P((int));
|
2004-11-15 18:00:29 +00:00
|
|
|
|
2007-12-31 12:39:52 +00:00
|
|
|
int
|
|
|
|
sudo_file_open(nss)
|
|
|
|
struct sudo_nss *nss;
|
|
|
|
{
|
|
|
|
nss->handle = open_sudoers(_PATH_SUDOERS, NULL);
|
|
|
|
return(nss->handle ? 0 : -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sudo_file_close(nss)
|
|
|
|
struct sudo_nss *nss;
|
|
|
|
{
|
|
|
|
/* XXX - free up data structures */
|
|
|
|
if (nss->handle != NULL) {
|
|
|
|
fclose(nss->handle);
|
|
|
|
nss->handle = NULL;
|
|
|
|
yyin = NULL;
|
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
1996-02-01 01:59:18 +00:00
|
|
|
/*
|
2004-10-26 22:10:55 +00:00
|
|
|
* Parse the specified sudoers file.
|
1996-02-01 01:59:18 +00:00
|
|
|
*/
|
2004-10-26 22:10:55 +00:00
|
|
|
int
|
2007-12-31 12:39:52 +00:00
|
|
|
sudo_file_parse(nss)
|
|
|
|
struct sudo_nss *nss;
|
2004-10-26 22:10:55 +00:00
|
|
|
{
|
2007-12-31 12:39:52 +00:00
|
|
|
if (nss->handle == NULL)
|
|
|
|
return(-1);
|
2004-10-26 22:10:55 +00:00
|
|
|
|
|
|
|
init_parser(_PATH_SUDOERS, 0);
|
2007-12-31 12:39:52 +00:00
|
|
|
yyin = nss->handle;
|
|
|
|
if (yyparse() != 0 || parse_error) {
|
2007-12-31 19:26:52 +00:00
|
|
|
log_error(NO_EXIT, "parse error in %s near line %d",
|
|
|
|
errorfile, errorlineno);
|
2007-12-31 12:39:52 +00:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2007-12-31 12:54:47 +00:00
|
|
|
/*
|
|
|
|
* Update the defaults based on what was set by sudoers.
|
|
|
|
* If skip_cmnd is 1, skip DEFAULTS_CMND, if it is 0, skip all others.
|
|
|
|
* Returns TRUE on success and FALSE on failure.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
update_defaults(skip_cmnd)
|
|
|
|
int skip_cmnd;
|
|
|
|
{
|
|
|
|
struct defaults *def;
|
|
|
|
|
|
|
|
tq_foreach_fwd(&defaults, def) {
|
|
|
|
if (skip_cmnd == (def->type == DEFAULTS_CMND))
|
|
|
|
continue;
|
|
|
|
switch (def->type) {
|
|
|
|
case DEFAULTS:
|
|
|
|
if (!set_default(def->var, def->val, def->op))
|
|
|
|
return(FALSE);
|
|
|
|
case DEFAULTS_USER:
|
|
|
|
if (userlist_matches(sudo_user.pw, &def->binding) &&
|
|
|
|
!set_default(def->var, def->val, def->op))
|
|
|
|
return(FALSE);
|
|
|
|
break;
|
|
|
|
case DEFAULTS_RUNAS:
|
|
|
|
if (runaslist_matches(&def->binding, NULL) &&
|
|
|
|
!set_default(def->var, def->val, def->op))
|
|
|
|
return(FALSE);
|
|
|
|
break;
|
|
|
|
case DEFAULTS_HOST:
|
|
|
|
if (hostlist_matches(&def->binding) &&
|
|
|
|
!set_default(def->var, def->val, def->op))
|
|
|
|
return(FALSE);
|
|
|
|
break;
|
|
|
|
case DEFAULTS_CMND:
|
|
|
|
if (cmndlist_matches(&def->binding) &&
|
|
|
|
!set_default(def->var, def->val, def->op))
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wrapper around update_defaults() for nsswitch code.
|
|
|
|
*/
|
2007-12-31 12:39:52 +00:00
|
|
|
int
|
|
|
|
sudo_file_setdefs(nss)
|
|
|
|
struct sudo_nss *nss;
|
|
|
|
{
|
|
|
|
if (nss->handle == NULL)
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
if (!update_defaults(SKIP_CMND))
|
|
|
|
return(-1);
|
|
|
|
return(0);
|
2004-10-26 22:10:55 +00:00
|
|
|
}
|
1996-02-01 01:59:18 +00:00
|
|
|
|
1993-10-04 19:10:33 +00:00
|
|
|
/*
|
2004-10-26 22:10:55 +00:00
|
|
|
* Look up the user in the parsed sudoers file and check to see if they are
|
1999-08-01 15:42:12 +00:00
|
|
|
* allowed to run the specified command on this host as the target user.
|
1993-10-04 19:10:33 +00:00
|
|
|
*/
|
1999-04-10 04:49:03 +00:00
|
|
|
int
|
2007-12-31 12:39:52 +00:00
|
|
|
sudo_file_lookup(nss, pwflag)
|
|
|
|
struct sudo_nss *nss;
|
2000-08-12 20:48:29 +00:00
|
|
|
int pwflag;
|
1993-06-11 22:03:45 +00:00
|
|
|
{
|
2007-07-08 13:43:07 +00:00
|
|
|
int validated, match, host_match, runas_match, cmnd_match;
|
2004-10-26 22:10:55 +00:00
|
|
|
struct cmndspec *cs;
|
|
|
|
struct cmndtag *tags = NULL;
|
|
|
|
struct privilege *priv;
|
|
|
|
struct userspec *us;
|
1995-03-24 03:32:53 +00:00
|
|
|
|
2004-10-26 22:10:55 +00:00
|
|
|
/* Assume the worst. */
|
|
|
|
validated = VALIDATE_NOT_OK | FLAG_NO_HOST | FLAG_NO_USER;
|
1995-03-24 03:32:53 +00:00
|
|
|
|
2007-12-31 19:26:52 +00:00
|
|
|
if (nss->handle == NULL)
|
|
|
|
return(validated);
|
|
|
|
|
1995-03-28 20:26:55 +00:00
|
|
|
/*
|
2003-12-30 22:31:30 +00:00
|
|
|
* Only check the actual command if pwflag is not set.
|
2000-01-03 04:43:33 +00:00
|
|
|
* It is set for the "validate", "list" and "kill" pseudo-commands.
|
1999-08-20 20:37:16 +00:00
|
|
|
* Always check the host and user.
|
1995-03-28 20:26:55 +00:00
|
|
|
*/
|
2003-12-30 22:31:30 +00:00
|
|
|
if (pwflag) {
|
2004-10-26 22:10:55 +00:00
|
|
|
int nopass = UNSPEC;
|
2007-07-06 19:51:03 +00:00
|
|
|
enum def_tupple pwcheck;
|
|
|
|
|
|
|
|
pwcheck = (pwflag == -1) ? never : sudo_defs_table[pwflag].sd_un.tuple;
|
2004-10-26 22:10:55 +00:00
|
|
|
|
2007-07-08 13:30:07 +00:00
|
|
|
if (list_pw == NULL)
|
|
|
|
SET(validated, FLAG_NO_CHECK);
|
2004-10-26 22:10:55 +00:00
|
|
|
CLR(validated, FLAG_NO_USER);
|
|
|
|
CLR(validated, FLAG_NO_HOST);
|
2007-07-08 13:43:07 +00:00
|
|
|
match = DENY;
|
2007-09-11 19:42:48 +00:00
|
|
|
tq_foreach_rev(&userspecs, us) {
|
2007-08-30 17:26:35 +00:00
|
|
|
if (userlist_matches(sudo_user.pw, &us->users) != ALLOW)
|
2007-07-06 00:20:51 +00:00
|
|
|
continue;
|
2007-09-11 19:42:48 +00:00
|
|
|
tq_foreach_rev(&us->privileges, priv) {
|
2007-08-30 17:26:35 +00:00
|
|
|
if (hostlist_matches(&priv->hostlist) != ALLOW)
|
2007-07-06 00:20:51 +00:00
|
|
|
continue;
|
2007-09-11 19:42:48 +00:00
|
|
|
tq_foreach_rev(&priv->cmndlist, cs) {
|
2007-07-06 00:20:51 +00:00
|
|
|
/* Only check the command when listing another user. */
|
|
|
|
if (user_uid == 0 || list_pw == NULL ||
|
|
|
|
user_uid == list_pw->pw_uid ||
|
2007-07-08 13:43:07 +00:00
|
|
|
cmnd_matches(cs->cmnd) == ALLOW)
|
|
|
|
match = ALLOW;
|
2007-07-06 00:20:51 +00:00
|
|
|
if ((pwcheck == any && nopass != TRUE) ||
|
|
|
|
(pwcheck == all && nopass == TRUE))
|
|
|
|
nopass = cs->tags.nopasswd;
|
2007-08-31 01:21:26 +00:00
|
|
|
if (match == ALLOW)
|
|
|
|
goto matched_pseudo;
|
1996-07-26 17:23:40 +00:00
|
|
|
}
|
|
|
|
}
|
1995-03-28 20:26:55 +00:00
|
|
|
}
|
2007-08-31 01:21:26 +00:00
|
|
|
matched_pseudo:
|
2007-07-08 13:43:07 +00:00
|
|
|
if (match == ALLOW || user_uid == 0) {
|
2004-10-26 22:10:55 +00:00
|
|
|
/* User has an entry for this host. */
|
|
|
|
CLR(validated, VALIDATE_NOT_OK);
|
|
|
|
SET(validated, VALIDATE_OK);
|
1996-05-27 23:38:46 +00:00
|
|
|
}
|
2007-07-06 19:51:03 +00:00
|
|
|
if (pwcheck == always && def_authenticate)
|
|
|
|
SET(validated, FLAG_CHECK_USER);
|
|
|
|
else if (pwcheck == never || nopass == TRUE)
|
|
|
|
def_authenticate = FALSE;
|
2004-10-26 22:10:55 +00:00
|
|
|
return(validated);
|
|
|
|
}
|
1995-08-14 04:06:27 +00:00
|
|
|
|
2004-10-26 22:10:55 +00:00
|
|
|
/* Need to be runas user while stat'ing things. */
|
|
|
|
set_perms(PERM_RUNAS);
|
1998-10-15 03:57:14 +00:00
|
|
|
|
2007-07-08 13:43:07 +00:00
|
|
|
match = UNSPEC;
|
2007-09-11 19:42:48 +00:00
|
|
|
tq_foreach_rev(&userspecs, us) {
|
2007-08-30 17:26:35 +00:00
|
|
|
if (userlist_matches(sudo_user.pw, &us->users) != ALLOW)
|
2007-07-06 00:20:51 +00:00
|
|
|
continue;
|
|
|
|
CLR(validated, FLAG_NO_USER);
|
2007-09-11 19:42:48 +00:00
|
|
|
tq_foreach_rev(&us->privileges, priv) {
|
2007-08-30 17:26:35 +00:00
|
|
|
host_match = hostlist_matches(&priv->hostlist);
|
2007-07-08 13:43:07 +00:00
|
|
|
if (host_match == ALLOW)
|
2007-07-06 00:20:51 +00:00
|
|
|
CLR(validated, FLAG_NO_HOST);
|
2007-08-31 01:21:26 +00:00
|
|
|
else
|
|
|
|
continue;
|
2007-09-11 19:42:48 +00:00
|
|
|
tq_foreach_rev(&priv->cmndlist, cs) {
|
2007-11-21 20:12:00 +00:00
|
|
|
runas_match = runaslist_matches(&cs->runasuserlist,
|
|
|
|
&cs->runasgrouplist);
|
2007-08-31 01:21:26 +00:00
|
|
|
if (runas_match == ALLOW) {
|
2007-07-08 13:43:07 +00:00
|
|
|
cmnd_match = cmnd_matches(cs->cmnd);
|
2007-08-31 01:21:26 +00:00
|
|
|
if (cmnd_match != UNSPEC) {
|
|
|
|
match = cmnd_match;
|
2007-07-06 00:20:51 +00:00
|
|
|
tags = &cs->tags;
|
2007-08-31 01:21:26 +00:00
|
|
|
goto matched2;
|
|
|
|
}
|
2004-10-26 22:10:55 +00:00
|
|
|
}
|
1999-04-10 04:10:01 +00:00
|
|
|
}
|
1996-05-27 23:38:46 +00:00
|
|
|
}
|
|
|
|
}
|
2007-08-31 01:21:26 +00:00
|
|
|
matched2:
|
2007-07-08 13:43:07 +00:00
|
|
|
if (match == ALLOW) {
|
2004-10-26 22:10:55 +00:00
|
|
|
CLR(validated, VALIDATE_NOT_OK);
|
|
|
|
SET(validated, VALIDATE_OK);
|
|
|
|
if (tags != NULL) {
|
2004-11-19 23:00:28 +00:00
|
|
|
if (tags->nopasswd != UNSPEC)
|
|
|
|
def_authenticate = !tags->nopasswd;
|
|
|
|
if (tags->noexec != UNSPEC)
|
|
|
|
def_noexec = tags->noexec;
|
2007-06-23 23:58:54 +00:00
|
|
|
if (tags->setenv != UNSPEC)
|
|
|
|
def_setenv = tags->setenv;
|
2001-12-13 01:07:25 +00:00
|
|
|
}
|
2004-03-24 23:06:34 +00:00
|
|
|
}
|
2004-10-26 22:10:55 +00:00
|
|
|
set_perms(PERM_ROOT);
|
|
|
|
return(validated);
|
2004-03-24 23:06:34 +00:00
|
|
|
}
|
|
|
|
|
2007-11-21 16:05:31 +00:00
|
|
|
#define TAG_CHANGED(t) \
|
|
|
|
(cs->tags.t != UNSPEC && cs->tags.t != IMPLIED && cs->tags.t != tags.t)
|
2005-04-10 21:44:27 +00:00
|
|
|
|
2007-12-31 21:36:53 +00:00
|
|
|
/* Reset user_groups based on passwd entry. */
|
|
|
|
static void
|
|
|
|
reset_groups(pw)
|
2004-03-24 23:06:34 +00:00
|
|
|
struct passwd *pw;
|
1995-12-18 02:51:30 +00:00
|
|
|
{
|
2005-03-29 03:33:05 +00:00
|
|
|
#if defined(HAVE_INITGROUPS) && defined(HAVE_GETGROUPS)
|
|
|
|
if (pw != sudo_user.pw) {
|
2005-04-12 01:37:08 +00:00
|
|
|
(void) initgroups(pw->pw_name, pw->pw_gid);
|
|
|
|
if ((user_ngroups = getgroups(0, NULL)) > 0) {
|
2007-08-14 19:27:27 +00:00
|
|
|
user_groups = erealloc3(user_groups, user_ngroups,
|
2007-11-27 23:40:50 +00:00
|
|
|
sizeof(GETGROUPS_T));
|
2005-04-12 01:37:08 +00:00
|
|
|
if (getgroups(user_ngroups, user_groups) < 0)
|
|
|
|
log_error(USE_ERRNO|MSG_ONLY, "can't get group vector");
|
2007-12-31 21:23:46 +00:00
|
|
|
} else {
|
2005-04-12 01:37:08 +00:00
|
|
|
user_ngroups = 0;
|
2007-12-31 21:23:46 +00:00
|
|
|
efree(user_groups);
|
|
|
|
}
|
2005-03-29 03:33:05 +00:00
|
|
|
}
|
|
|
|
#endif
|
2007-12-31 21:36:53 +00:00
|
|
|
}
|
2005-03-29 03:33:05 +00:00
|
|
|
|
2007-12-31 21:36:53 +00:00
|
|
|
/*
|
|
|
|
* Print out privileges for the specified user.
|
|
|
|
* XXX - move out of parse.c
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
display_privs(snl, pw)
|
|
|
|
struct sudo_nss_list *snl;
|
|
|
|
struct passwd *pw;
|
|
|
|
{
|
|
|
|
struct sudo_nss *nss;
|
|
|
|
|
|
|
|
/* Reset group vector so group matching works correctly. */
|
|
|
|
reset_groups(pw);
|
|
|
|
|
|
|
|
/* Display privileges from all sources. */
|
|
|
|
tq_foreach_fwd(snl, nss) {
|
|
|
|
if (nss != tq_first(snl))
|
|
|
|
putchar('\n');
|
2007-12-31 15:08:30 +00:00
|
|
|
nss->display_privs(nss, pw);
|
2007-12-31 21:36:53 +00:00
|
|
|
}
|
2007-12-31 15:08:30 +00:00
|
|
|
}
|
2004-10-26 22:10:55 +00:00
|
|
|
|
2007-12-31 15:08:30 +00:00
|
|
|
void
|
|
|
|
sudo_file_display_privs(nss, pw)
|
|
|
|
struct sudo_nss *nss;
|
|
|
|
struct passwd *pw;
|
|
|
|
{
|
|
|
|
struct lbuf lbuf;
|
|
|
|
struct cmndspec *cs;
|
|
|
|
struct member *m;
|
|
|
|
struct privilege *priv;
|
|
|
|
struct userspec *us;
|
|
|
|
struct cmndtag tags;
|
2004-10-26 22:10:55 +00:00
|
|
|
|
2007-12-31 15:08:30 +00:00
|
|
|
display_defaults(pw);
|
2004-10-26 22:10:55 +00:00
|
|
|
|
2007-12-31 15:08:30 +00:00
|
|
|
lbuf_init(&lbuf, NULL, 8, '\\');
|
|
|
|
printf("User %s may run the following commands on this host:\n",
|
|
|
|
pw->pw_name);
|
|
|
|
|
|
|
|
tq_foreach_fwd(&userspecs, us) {
|
|
|
|
/* XXX - why only check the first privilege here? */
|
|
|
|
if (userlist_matches(pw, &us->users) != ALLOW ||
|
|
|
|
hostlist_matches(&us->privileges.first->hostlist) != ALLOW)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
tq_foreach_fwd(&us->privileges, priv) {
|
|
|
|
tags.noexec = def_noexec;
|
|
|
|
tags.setenv = def_setenv;
|
|
|
|
tags.nopasswd = !def_authenticate;
|
|
|
|
lbuf_append(&lbuf, " ", NULL);
|
|
|
|
tq_foreach_fwd(&priv->cmndlist, cs) {
|
|
|
|
if (cs != tq_first(&priv->cmndlist))
|
|
|
|
lbuf_append(&lbuf, ", ", NULL);
|
|
|
|
lbuf_append(&lbuf, "(", NULL);
|
|
|
|
if (!tq_empty(&cs->runasuserlist)) {
|
|
|
|
tq_foreach_fwd(&cs->runasuserlist, m) {
|
|
|
|
if (m != tq_first(&cs->runasuserlist))
|
|
|
|
lbuf_append(&lbuf, ", ", NULL);
|
|
|
|
print_member(&lbuf, m->name, m->type, m->negated,
|
|
|
|
RUNASALIAS);
|
2005-04-12 01:37:08 +00:00
|
|
|
}
|
2007-12-31 15:08:30 +00:00
|
|
|
} else {
|
|
|
|
lbuf_append(&lbuf, def_runas_default, NULL);
|
|
|
|
}
|
|
|
|
if (!tq_empty(&cs->runasgrouplist)) {
|
|
|
|
lbuf_append(&lbuf, " : ", NULL);
|
|
|
|
tq_foreach_fwd(&cs->runasgrouplist, m) {
|
|
|
|
if (m != tq_first(&cs->runasgrouplist))
|
|
|
|
lbuf_append(&lbuf, ", ", NULL);
|
|
|
|
print_member(&lbuf, m->name, m->type, m->negated,
|
|
|
|
RUNASALIAS);
|
2005-04-12 01:37:08 +00:00
|
|
|
}
|
2004-10-26 22:10:55 +00:00
|
|
|
}
|
2007-12-31 15:08:30 +00:00
|
|
|
lbuf_append(&lbuf, ") ", NULL);
|
|
|
|
if (TAG_CHANGED(setenv)) {
|
|
|
|
lbuf_append(&lbuf, cs->tags.setenv ? "SETENV: " :
|
|
|
|
"NOSETENV: ", NULL);
|
|
|
|
tags.setenv = cs->tags.setenv;
|
|
|
|
}
|
|
|
|
if (TAG_CHANGED(noexec)) {
|
|
|
|
lbuf_append(&lbuf, cs->tags.noexec ? "NOEXEC: " :
|
|
|
|
"EXEC: ", NULL);
|
|
|
|
tags.noexec = cs->tags.noexec;
|
|
|
|
}
|
|
|
|
if (TAG_CHANGED(nopasswd)) {
|
|
|
|
lbuf_append(&lbuf, cs->tags.nopasswd ? "NOPASSWD: " :
|
|
|
|
"PASSWD: ", NULL);
|
|
|
|
tags.nopasswd = cs->tags.nopasswd;
|
|
|
|
}
|
|
|
|
m = cs->cmnd;
|
|
|
|
print_member(&lbuf, m->name, m->type, m->negated,
|
|
|
|
CMNDALIAS);
|
2004-10-26 22:10:55 +00:00
|
|
|
}
|
2007-12-31 15:08:30 +00:00
|
|
|
lbuf_print(&lbuf);
|
2005-01-02 00:31:08 +00:00
|
|
|
}
|
|
|
|
}
|
2007-12-31 15:08:30 +00:00
|
|
|
lbuf_destroy(&lbuf);
|
2005-01-02 00:31:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Display matching Defaults entries for the given user on this host.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
display_defaults(pw)
|
|
|
|
struct passwd *pw;
|
|
|
|
{
|
|
|
|
struct defaults *d;
|
2007-08-19 20:48:09 +00:00
|
|
|
struct lbuf lbuf;
|
2007-08-30 17:26:35 +00:00
|
|
|
char *prefix = NULL;
|
2005-01-02 00:31:08 +00:00
|
|
|
int per_runas = 0, per_cmnd = 0;
|
|
|
|
|
2007-08-19 20:48:09 +00:00
|
|
|
lbuf_init(&lbuf, NULL, 4, 0);
|
|
|
|
|
2007-09-11 19:42:48 +00:00
|
|
|
tq_foreach_fwd(&defaults, d) {
|
2005-01-02 00:31:08 +00:00
|
|
|
switch (d->type) {
|
|
|
|
case DEFAULTS_HOST:
|
2007-08-30 17:26:35 +00:00
|
|
|
if (hostlist_matches(&d->binding) != ALLOW)
|
2005-01-02 00:31:08 +00:00
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
case DEFAULTS_USER:
|
2007-08-30 17:26:35 +00:00
|
|
|
if (userlist_matches(pw, &d->binding) != ALLOW)
|
2005-01-02 00:31:08 +00:00
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
case DEFAULTS_RUNAS:
|
|
|
|
per_runas = 1;
|
|
|
|
continue;
|
|
|
|
case DEFAULTS_CMND:
|
|
|
|
per_cmnd = 1;
|
|
|
|
continue;
|
1995-07-27 01:29:59 +00:00
|
|
|
}
|
2005-04-10 21:40:41 +00:00
|
|
|
if (prefix == NULL) {
|
2007-08-19 20:48:09 +00:00
|
|
|
printf("Matching Defaults entries for %s on this host:\n",
|
|
|
|
pw->pw_name);
|
|
|
|
prefix = " ";
|
2005-04-10 21:40:41 +00:00
|
|
|
}
|
2007-08-19 20:48:09 +00:00
|
|
|
lbuf_append(&lbuf, prefix, NULL);
|
2005-01-02 00:31:08 +00:00
|
|
|
if (d->val != NULL) {
|
2007-08-20 23:24:31 +00:00
|
|
|
lbuf_append(&lbuf, d->var, d->op == '+' ? " += " :
|
|
|
|
d->op == '-' ? " -= " : " = ", NULL);
|
2007-08-22 22:32:00 +00:00
|
|
|
if (strpbrk(d->val, " \t") != NULL) {
|
|
|
|
lbuf_append(&lbuf, "\"", NULL);
|
|
|
|
lbuf_append_quoted(&lbuf, "\"", d->val, NULL);
|
|
|
|
lbuf_append(&lbuf, "\"", NULL);
|
|
|
|
} else
|
|
|
|
lbuf_append_quoted(&lbuf, SUDOERS_QUOTED, d->val, NULL);
|
2005-01-02 00:31:08 +00:00
|
|
|
} else
|
2007-08-19 20:48:09 +00:00
|
|
|
lbuf_append(&lbuf, d->op == FALSE ? "!" : "", d->var, NULL);
|
2005-01-02 00:31:08 +00:00
|
|
|
prefix = ", ";
|
1995-07-25 00:01:47 +00:00
|
|
|
}
|
2007-08-19 20:48:09 +00:00
|
|
|
if (prefix) {
|
|
|
|
lbuf_print(&lbuf);
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
lbuf_destroy(&lbuf);
|
2005-01-02 00:31:08 +00:00
|
|
|
|
|
|
|
if (per_runas)
|
|
|
|
display_bound_defaults(DEFAULTS_RUNAS);
|
|
|
|
if (per_cmnd)
|
|
|
|
display_bound_defaults(DEFAULTS_CMND);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Display Defaults entries of the given type.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
display_bound_defaults(dtype)
|
|
|
|
int dtype;
|
|
|
|
{
|
2007-08-19 20:48:09 +00:00
|
|
|
struct lbuf lbuf;
|
2005-01-02 00:31:08 +00:00
|
|
|
struct defaults *d;
|
2007-08-30 17:26:35 +00:00
|
|
|
struct member *m, *binding = NULL;
|
2005-04-23 19:10:16 +00:00
|
|
|
char *dname, *dsep;
|
2005-01-02 00:31:08 +00:00
|
|
|
int atype;
|
|
|
|
|
|
|
|
switch (dtype) {
|
|
|
|
case DEFAULTS_HOST:
|
|
|
|
atype = HOSTALIAS;
|
|
|
|
dname = "host";
|
|
|
|
dsep = "@";
|
|
|
|
break;
|
|
|
|
case DEFAULTS_USER:
|
|
|
|
atype = USERALIAS;
|
|
|
|
dname = "user";
|
|
|
|
dsep = ":";
|
|
|
|
break;
|
|
|
|
case DEFAULTS_RUNAS:
|
|
|
|
atype = RUNASALIAS;
|
|
|
|
dname = "runas";
|
|
|
|
dsep = ">";
|
|
|
|
break;
|
|
|
|
case DEFAULTS_CMND:
|
|
|
|
atype = CMNDALIAS;
|
|
|
|
dname = "cmnd";
|
|
|
|
dsep = "!";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2007-08-19 20:48:09 +00:00
|
|
|
lbuf_init(&lbuf, NULL, 4, 0);
|
|
|
|
printf("Per-%s Defaults entries:\n", dname);
|
2007-09-11 19:42:48 +00:00
|
|
|
tq_foreach_fwd(&defaults, d) {
|
2005-01-02 00:31:08 +00:00
|
|
|
if (d->type != dtype)
|
|
|
|
continue;
|
|
|
|
|
2007-09-11 19:42:48 +00:00
|
|
|
if (binding != tq_first(&d->binding)) {
|
|
|
|
binding = tq_first(&d->binding);
|
2007-08-19 20:48:09 +00:00
|
|
|
lbuf_append(&lbuf, " Defaults", dsep, NULL);
|
2005-01-02 00:31:08 +00:00
|
|
|
for (m = binding; m != NULL; m = m->next) {
|
|
|
|
if (m != binding)
|
2007-08-19 20:48:09 +00:00
|
|
|
lbuf_append(&lbuf, ",", NULL);
|
|
|
|
print_member(&lbuf, m->name, m->type, m->negated, atype);
|
|
|
|
lbuf_append(&lbuf, " ", NULL);
|
2005-01-02 00:31:08 +00:00
|
|
|
}
|
|
|
|
} else
|
2007-08-19 20:48:09 +00:00
|
|
|
lbuf_append(&lbuf, ", ", NULL);
|
2005-01-02 00:31:08 +00:00
|
|
|
if (d->val != NULL) {
|
2007-08-19 20:48:09 +00:00
|
|
|
lbuf_append(&lbuf, d->var, d->op == '+' ? "+=" :
|
|
|
|
d->op == '-' ? "-=" : "=", d->val, NULL);
|
2005-01-02 00:31:08 +00:00
|
|
|
} else
|
2007-08-19 20:48:09 +00:00
|
|
|
lbuf_append(&lbuf, d->op == FALSE ? "!" : "", d->var, NULL);
|
2005-01-02 00:31:08 +00:00
|
|
|
}
|
2007-08-19 20:48:09 +00:00
|
|
|
lbuf_print(&lbuf);
|
|
|
|
lbuf_destroy(&lbuf);
|
|
|
|
putchar('\n');
|
1995-07-25 00:01:47 +00:00
|
|
|
}
|
1996-02-01 01:59:18 +00:00
|
|
|
|
2004-11-24 21:31:51 +00:00
|
|
|
/*
|
|
|
|
* Check user_cmnd against sudoers and print the matching entry if the
|
|
|
|
* command is allowed.
|
2007-12-31 15:08:30 +00:00
|
|
|
* XXX - move out of parse.c
|
2004-11-24 21:31:51 +00:00
|
|
|
*/
|
|
|
|
int
|
2007-12-31 15:08:30 +00:00
|
|
|
display_cmnd(snl, pw)
|
|
|
|
struct sudo_nss_list *snl;
|
|
|
|
struct passwd *pw;
|
|
|
|
{
|
|
|
|
struct sudo_nss *nss;
|
|
|
|
|
2007-12-31 21:36:53 +00:00
|
|
|
/* Reset group vector so group matching works correctly. */
|
|
|
|
reset_groups(pw);
|
2007-12-31 15:08:30 +00:00
|
|
|
|
|
|
|
tq_foreach_fwd(snl, nss) {
|
|
|
|
if (nss->display_cmnd(nss, pw) == 0)
|
2007-12-31 21:36:53 +00:00
|
|
|
return(0);
|
2007-12-31 15:08:30 +00:00
|
|
|
}
|
2007-12-31 21:36:53 +00:00
|
|
|
return(1);
|
2007-12-31 15:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sudo_file_display_cmnd(nss, pw)
|
|
|
|
struct sudo_nss *nss;
|
2004-11-24 21:31:51 +00:00
|
|
|
struct passwd *pw;
|
|
|
|
{
|
|
|
|
struct cmndspec *cs;
|
2007-08-30 17:26:35 +00:00
|
|
|
struct member *match;
|
2004-11-24 21:31:51 +00:00
|
|
|
struct privilege *priv;
|
|
|
|
struct userspec *us;
|
2005-04-12 01:37:08 +00:00
|
|
|
int rval = 1;
|
2007-07-08 13:43:07 +00:00
|
|
|
int host_match, runas_match, cmnd_match;
|
2004-11-24 21:31:51 +00:00
|
|
|
|
2007-12-31 15:08:30 +00:00
|
|
|
if (nss->handle == NULL)
|
|
|
|
return(rval);
|
2004-11-24 21:31:51 +00:00
|
|
|
|
2007-12-31 15:08:30 +00:00
|
|
|
match = NULL;
|
|
|
|
tq_foreach_rev(&userspecs, us) {
|
|
|
|
if (userlist_matches(pw, &us->users) != ALLOW)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
tq_foreach_rev(&us->privileges, priv) {
|
|
|
|
host_match = hostlist_matches(&priv->hostlist);
|
|
|
|
if (host_match != ALLOW)
|
|
|
|
continue;
|
|
|
|
tq_foreach_rev(&priv->cmndlist, cs) {
|
|
|
|
runas_match = runaslist_matches(&cs->runasuserlist,
|
|
|
|
&cs->runasgrouplist);
|
|
|
|
if (runas_match == ALLOW) {
|
|
|
|
cmnd_match = cmnd_matches(cs->cmnd);
|
|
|
|
if (cmnd_match != UNSPEC) {
|
|
|
|
match = host_match && runas_match ?
|
|
|
|
cs->cmnd : NULL;
|
|
|
|
goto matched;
|
2007-07-06 00:20:51 +00:00
|
|
|
}
|
2005-04-12 01:37:08 +00:00
|
|
|
}
|
2004-11-24 21:31:51 +00:00
|
|
|
}
|
|
|
|
}
|
2007-12-31 15:08:30 +00:00
|
|
|
}
|
|
|
|
matched:
|
|
|
|
if (match != NULL && !match->negated) {
|
|
|
|
printf("%s%s%s\n", safe_cmnd, user_args ? " " : "",
|
|
|
|
user_args ? user_args : "");
|
|
|
|
rval = 0;
|
2004-11-24 21:31:51 +00:00
|
|
|
}
|
2005-04-12 01:37:08 +00:00
|
|
|
return(rval);
|
2004-11-24 21:31:51 +00:00
|
|
|
}
|
|
|
|
|
1996-02-01 01:59:18 +00:00
|
|
|
/*
|
2004-10-26 22:10:55 +00:00
|
|
|
* Print the contents of a struct member to stdout
|
1996-02-01 01:59:18 +00:00
|
|
|
*/
|
2004-11-15 18:00:29 +00:00
|
|
|
static void
|
2007-09-15 11:24:54 +00:00
|
|
|
_print_member(lbuf, name, type, negated, alias_type)
|
2007-08-19 20:48:09 +00:00
|
|
|
struct lbuf *lbuf;
|
2004-11-15 18:00:29 +00:00
|
|
|
char *name;
|
|
|
|
int type, negated, alias_type;
|
1996-02-01 01:59:18 +00:00
|
|
|
{
|
2004-11-15 18:00:29 +00:00
|
|
|
struct alias *a;
|
|
|
|
struct member *m;
|
2004-10-26 22:10:55 +00:00
|
|
|
struct sudo_command *c;
|
|
|
|
|
2004-11-15 18:00:29 +00:00
|
|
|
switch (type) {
|
|
|
|
case ALL:
|
2007-08-19 20:48:09 +00:00
|
|
|
lbuf_append(lbuf, negated ? "!ALL" : "ALL", NULL);
|
2004-11-15 18:00:29 +00:00
|
|
|
break;
|
|
|
|
case COMMAND:
|
|
|
|
c = (struct sudo_command *) name;
|
2007-08-22 22:32:00 +00:00
|
|
|
if (negated)
|
2007-08-30 17:12:00 +00:00
|
|
|
lbuf_append(lbuf, "!", NULL);
|
2007-08-22 22:32:00 +00:00
|
|
|
lbuf_append_quoted(lbuf, SUDOERS_QUOTED, c->cmnd, NULL);
|
|
|
|
if (c->args) {
|
|
|
|
lbuf_append(lbuf, " ", NULL);
|
|
|
|
lbuf_append_quoted(lbuf, SUDOERS_QUOTED, c->args, NULL);
|
|
|
|
}
|
2004-11-15 18:00:29 +00:00
|
|
|
break;
|
|
|
|
case ALIAS:
|
|
|
|
if ((a = find_alias(name, alias_type)) != NULL) {
|
2007-09-11 19:42:48 +00:00
|
|
|
tq_foreach_fwd(&a->members, m) {
|
|
|
|
if (m != tq_first(&a->members))
|
2007-08-19 20:48:09 +00:00
|
|
|
lbuf_append(lbuf, ", ", NULL);
|
2007-09-15 11:24:54 +00:00
|
|
|
_print_member(lbuf, m->name, m->type,
|
2004-11-15 18:00:29 +00:00
|
|
|
negated ? !m->negated : m->negated, alias_type);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
default:
|
2007-08-19 20:48:09 +00:00
|
|
|
lbuf_append(lbuf, negated ? "!" : "", name, NULL);
|
2004-11-15 18:00:29 +00:00
|
|
|
break;
|
1996-02-01 01:59:18 +00:00
|
|
|
}
|
|
|
|
}
|
2007-09-15 11:24:54 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
print_member(lbuf, name, type, negated, alias_type)
|
|
|
|
struct lbuf *lbuf;
|
|
|
|
char *name;
|
|
|
|
int type, negated, alias_type;
|
|
|
|
{
|
|
|
|
alias_seqno++;
|
|
|
|
_print_member(lbuf, name, type, negated, alias_type);
|
|
|
|
}
|