2000-11-03 05:37:44 +00:00
|
|
|
/*
|
2009-05-25 12:02:42 +00:00
|
|
|
* Copyright (c) 1994-1996,1998-2009 Todd C. Miller <Todd.Miller@courtesan.com>
|
2000-11-03 05:37:44 +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.
|
2000-11-03 05:37:44 +00:00
|
|
|
*
|
2004-02-13 21:36:43 +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.
|
2000-11-03 05:37:44 +00:00
|
|
|
*/
|
|
|
|
|
2004-11-19 18:39:14 +00:00
|
|
|
#include <config.h>
|
2000-11-03 05:37:44 +00:00
|
|
|
|
2001-12-14 19:52:54 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/stat.h>
|
2000-11-03 05:37:44 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#ifdef STDC_HEADERS
|
2001-12-14 19:52:54 +00:00
|
|
|
# include <stdlib.h>
|
|
|
|
# include <stddef.h>
|
|
|
|
#else
|
|
|
|
# ifdef HAVE_STDLIB_H
|
|
|
|
# include <stdlib.h>
|
|
|
|
# endif
|
2000-11-03 05:37:44 +00:00
|
|
|
#endif /* STDC_HEADERS */
|
|
|
|
#ifdef HAVE_STRING_H
|
2001-12-14 19:52:54 +00:00
|
|
|
# include <string.h>
|
|
|
|
#else
|
|
|
|
# ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
# endif
|
2000-11-03 05:37:44 +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 */
|
2000-11-03 05:37:44 +00:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#ifdef HAVE_LOGIN_CAP_H
|
|
|
|
# include <login_cap.h>
|
|
|
|
#endif
|
|
|
|
|
2010-03-14 19:58:47 -04:00
|
|
|
#include "sudoers.h"
|
2000-11-03 05:37:44 +00:00
|
|
|
|
2004-05-17 20:08:46 +00:00
|
|
|
#ifdef __TANDEM
|
|
|
|
# define ROOT_UID 65535
|
|
|
|
#else
|
|
|
|
# define ROOT_UID 0
|
|
|
|
#endif
|
|
|
|
|
2000-11-03 05:37:44 +00:00
|
|
|
/*
|
2000-12-30 03:29:47 +00:00
|
|
|
* Prototypes
|
2000-11-03 05:37:44 +00:00
|
|
|
*/
|
2010-03-27 20:19:40 -04:00
|
|
|
#if 0
|
2010-03-17 19:56:27 -04:00
|
|
|
static void runas_setup(void);
|
2010-03-27 20:19:40 -04:00
|
|
|
#endif
|
2010-03-17 19:56:27 -04:00
|
|
|
static void runas_setgroups(void);
|
|
|
|
static void restore_groups(void);
|
2007-11-25 13:07:21 +00:00
|
|
|
|
|
|
|
static int current_perm = -1;
|
2000-11-03 05:37:44 +00:00
|
|
|
|
2002-11-22 19:09:49 +00:00
|
|
|
#ifdef HAVE_SETRESUID
|
|
|
|
/*
|
|
|
|
* Set real and effective and saved uids and gids based on perm.
|
|
|
|
* We always retain a saved uid of 0 unless we are headed for an exec().
|
|
|
|
* We only flip the effective gid since it only changes for PERM_SUDOERS.
|
|
|
|
* This version of set_perms() works fine with the "stay_setuid" option.
|
|
|
|
*/
|
2009-05-10 11:52:13 +00:00
|
|
|
int
|
2004-10-13 16:46:19 +00:00
|
|
|
set_perms(perm)
|
2002-11-22 19:09:49 +00:00
|
|
|
int perm;
|
|
|
|
{
|
2008-01-27 21:37:54 +00:00
|
|
|
const char *errstr;
|
2009-05-10 11:52:13 +00:00
|
|
|
int noexit;
|
|
|
|
|
|
|
|
noexit = ISSET(perm, PERM_NOEXIT);
|
|
|
|
CLR(perm, PERM_MASK);
|
2008-01-27 21:37:54 +00:00
|
|
|
|
2007-11-25 13:07:21 +00:00
|
|
|
if (perm == current_perm)
|
2009-05-10 11:52:13 +00:00
|
|
|
return(1);
|
2007-11-25 13:07:21 +00:00
|
|
|
|
2002-11-22 19:09:49 +00:00
|
|
|
switch (perm) {
|
|
|
|
case PERM_ROOT:
|
2008-01-27 21:37:54 +00:00
|
|
|
if (setresuid(ROOT_UID, ROOT_UID, ROOT_UID)) {
|
|
|
|
errstr = "setresuid(ROOT_UID, ROOT_UID, ROOT_UID)";
|
|
|
|
goto bad;
|
|
|
|
}
|
2007-07-06 14:14:12 +00:00
|
|
|
(void) setresgid(-1, user_gid, -1);
|
2007-11-25 13:07:21 +00:00
|
|
|
if (current_perm == PERM_RUNAS)
|
|
|
|
restore_groups();
|
2002-11-22 19:09:49 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PERM_USER:
|
2002-12-15 16:24:24 +00:00
|
|
|
(void) setresgid(-1, user_gid, -1);
|
2008-01-27 21:37:54 +00:00
|
|
|
if (setresuid(user_uid, user_uid, ROOT_UID)) {
|
|
|
|
errstr = "setresuid(user_uid, user_uid, ROOT_UID)";
|
|
|
|
goto bad;
|
|
|
|
}
|
2002-11-22 19:09:49 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PERM_FULL_USER:
|
|
|
|
/* headed for exec() */
|
|
|
|
(void) setgid(user_gid);
|
2008-01-27 21:37:54 +00:00
|
|
|
if (setresuid(user_uid, user_uid, user_uid)) {
|
|
|
|
errstr = "setresuid(user_uid, user_uid, user_uid)";
|
|
|
|
goto bad;
|
|
|
|
}
|
2002-11-22 19:09:49 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PERM_RUNAS:
|
2007-11-26 00:26:42 +00:00
|
|
|
runas_setgroups();
|
2007-11-21 20:12:00 +00:00
|
|
|
(void) setresgid(-1, runas_gr ?
|
|
|
|
runas_gr->gr_gid : runas_pw->pw_gid, -1);
|
2008-01-27 21:37:54 +00:00
|
|
|
if (setresuid(-1, runas_pw ? runas_pw->pw_uid :
|
|
|
|
user_uid, -1)) {
|
|
|
|
errstr = "unable to change to runas uid";
|
|
|
|
goto bad;
|
|
|
|
}
|
2004-01-16 23:05:47 +00:00
|
|
|
break;
|
|
|
|
|
2010-03-27 09:12:48 -04:00
|
|
|
#if 0
|
2004-01-16 23:05:47 +00:00
|
|
|
case PERM_FULL_RUNAS:
|
2004-05-17 20:08:46 +00:00
|
|
|
/* headed for exec(), assume euid == ROOT_UID */
|
2002-11-22 19:09:49 +00:00
|
|
|
runas_setup();
|
2005-02-10 05:03:58 +00:00
|
|
|
if (setresuid(def_stay_setuid ?
|
2002-11-22 19:09:49 +00:00
|
|
|
user_uid : runas_pw->pw_uid,
|
2008-01-27 21:37:54 +00:00
|
|
|
runas_pw->pw_uid, runas_pw->pw_uid)) {
|
|
|
|
errstr = "unable to change to runas uid";
|
|
|
|
goto bad;
|
|
|
|
}
|
2002-11-22 19:09:49 +00:00
|
|
|
break;
|
2010-03-27 09:12:48 -04:00
|
|
|
#endif
|
2002-11-22 19:09:49 +00:00
|
|
|
|
|
|
|
case PERM_SUDOERS:
|
2004-05-17 20:08:46 +00:00
|
|
|
/* assume euid == ROOT_UID, ruid == user */
|
2002-12-15 16:24:24 +00:00
|
|
|
if (setresgid(-1, SUDOERS_GID, -1))
|
2005-02-10 05:03:58 +00:00
|
|
|
error(1, "unable to change to sudoers gid");
|
2002-11-22 19:09:49 +00:00
|
|
|
|
|
|
|
/*
|
2004-05-17 20:08:46 +00:00
|
|
|
* If SUDOERS_UID == ROOT_UID and SUDOERS_MODE
|
2002-11-22 19:09:49 +00:00
|
|
|
* is group readable we use a non-zero
|
|
|
|
* uid in order to avoid NFS lossage.
|
|
|
|
* Using uid 1 is a bit bogus but should
|
|
|
|
* work on all OS's.
|
|
|
|
*/
|
2004-05-17 20:08:46 +00:00
|
|
|
if (SUDOERS_UID == ROOT_UID) {
|
2008-01-27 21:37:54 +00:00
|
|
|
if ((SUDOERS_MODE & 040) && setresuid(ROOT_UID, 1, ROOT_UID)) {
|
|
|
|
errstr = "setresuid(ROOT_UID, 1, ROOT_UID)";
|
|
|
|
goto bad;
|
|
|
|
}
|
2002-11-22 19:09:49 +00:00
|
|
|
} else {
|
2008-01-27 21:37:54 +00:00
|
|
|
if (setresuid(ROOT_UID, SUDOERS_UID, ROOT_UID)) {
|
|
|
|
errstr = "setresuid(ROOT_UID, SUDOERS_UID, ROOT_UID)";
|
|
|
|
goto bad;
|
|
|
|
}
|
2002-11-22 19:09:49 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PERM_TIMESTAMP:
|
2008-01-27 21:37:54 +00:00
|
|
|
if (setresuid(ROOT_UID, timestamp_uid, ROOT_UID)) {
|
|
|
|
errstr = "setresuid(ROOT_UID, timestamp_uid, ROOT_UID)";
|
|
|
|
goto bad;
|
|
|
|
}
|
2002-11-22 19:09:49 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-11-25 13:07:21 +00:00
|
|
|
|
|
|
|
current_perm = perm;
|
2009-05-10 11:52:13 +00:00
|
|
|
return(1);
|
2008-01-27 21:37:54 +00:00
|
|
|
bad:
|
2009-05-10 11:52:13 +00:00
|
|
|
warningx("%s: %s", errstr,
|
2008-01-27 21:37:54 +00:00
|
|
|
errno == EAGAIN ? "too many processes" : strerror(errno));
|
2009-05-10 11:52:13 +00:00
|
|
|
if (noexit)
|
|
|
|
return(0);
|
|
|
|
exit(1);
|
2002-11-22 19:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
# ifdef HAVE_SETREUID
|
|
|
|
|
2000-12-30 03:29:47 +00:00
|
|
|
/*
|
|
|
|
* Set real and effective uids and gids based on perm.
|
2004-05-17 20:08:46 +00:00
|
|
|
* We always retain a real or effective uid of ROOT_UID unless
|
2000-12-30 03:29:47 +00:00
|
|
|
* we are headed for an exec().
|
2002-11-22 19:09:49 +00:00
|
|
|
* This version of set_perms() works fine with the "stay_setuid" option.
|
2000-12-30 03:29:47 +00:00
|
|
|
*/
|
2009-05-10 11:52:13 +00:00
|
|
|
int
|
2004-10-13 16:46:19 +00:00
|
|
|
set_perms(perm)
|
2000-12-30 03:29:47 +00:00
|
|
|
int perm;
|
|
|
|
{
|
2008-01-27 21:37:54 +00:00
|
|
|
const char *errstr;
|
2009-05-10 11:52:13 +00:00
|
|
|
int noexit;
|
|
|
|
|
|
|
|
noexit = ISSET(perm, PERM_NOEXIT);
|
|
|
|
CLR(perm, PERM_MASK);
|
2008-01-27 21:37:54 +00:00
|
|
|
|
2007-11-25 13:07:21 +00:00
|
|
|
if (perm == current_perm)
|
2009-05-10 11:52:13 +00:00
|
|
|
return(1);
|
2007-11-25 13:07:21 +00:00
|
|
|
|
2000-11-03 05:37:44 +00:00
|
|
|
switch (perm) {
|
|
|
|
case PERM_ROOT:
|
2008-01-27 21:37:54 +00:00
|
|
|
if (setreuid(-1, ROOT_UID)) {
|
|
|
|
errstr = "setreuid(-1, ROOT_UID)";
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
if (setuid(ROOT_UID)) {
|
|
|
|
errstr = "setuid(ROOT_UID)";
|
|
|
|
goto bad;
|
|
|
|
}
|
2007-07-06 14:14:12 +00:00
|
|
|
(void) setregid(-1, user_gid);
|
2007-11-25 13:07:21 +00:00
|
|
|
if (current_perm == PERM_RUNAS)
|
|
|
|
restore_groups();
|
2000-11-03 05:37:44 +00:00
|
|
|
break;
|
2002-01-13 18:28:09 +00:00
|
|
|
|
2000-11-03 05:37:44 +00:00
|
|
|
case PERM_USER:
|
2002-12-15 16:24:24 +00:00
|
|
|
(void) setregid(-1, user_gid);
|
2008-01-27 21:37:54 +00:00
|
|
|
if (setreuid(ROOT_UID, user_uid)) {
|
|
|
|
errstr = "setreuid(ROOT_UID, user_uid)";
|
|
|
|
goto bad;
|
|
|
|
}
|
2000-11-03 05:37:44 +00:00
|
|
|
break;
|
|
|
|
|
2002-01-16 21:27:09 +00:00
|
|
|
case PERM_FULL_USER:
|
|
|
|
/* headed for exec() */
|
|
|
|
(void) setgid(user_gid);
|
2008-01-27 21:37:54 +00:00
|
|
|
if (setreuid(user_uid, user_uid)) {
|
|
|
|
errstr = "setreuid(user_uid, user_uid)";
|
|
|
|
goto bad;
|
|
|
|
}
|
2002-01-16 21:27:09 +00:00
|
|
|
break;
|
|
|
|
|
2000-11-03 05:37:44 +00:00
|
|
|
case PERM_RUNAS:
|
2007-11-26 00:26:42 +00:00
|
|
|
runas_setgroups();
|
2007-11-21 20:12:00 +00:00
|
|
|
(void) setregid(-1, runas_gr ?
|
|
|
|
runas_gr->gr_gid : runas_pw->pw_gid);
|
|
|
|
if (setreuid(-1,
|
2008-01-27 21:37:54 +00:00
|
|
|
runas_pw ? runas_pw->pw_uid : user_uid)) {
|
|
|
|
errstr = "unable to change to runas uid";
|
|
|
|
goto bad;
|
|
|
|
}
|
2004-01-16 23:05:47 +00:00
|
|
|
break;
|
|
|
|
|
2010-03-27 09:12:48 -04:00
|
|
|
#if 0
|
2004-01-16 23:05:47 +00:00
|
|
|
case PERM_FULL_RUNAS:
|
2004-05-17 20:08:46 +00:00
|
|
|
/* headed for exec(), assume euid == ROOT_UID */
|
2000-12-30 03:29:47 +00:00
|
|
|
runas_setup();
|
2005-02-10 05:03:58 +00:00
|
|
|
if (setreuid(def_stay_setuid ? user_uid :
|
2008-01-27 21:37:54 +00:00
|
|
|
runas_pw->pw_uid, runas_pw->pw_uid)) {
|
|
|
|
errstr = "unable to change to runas uid";
|
|
|
|
goto bad;
|
|
|
|
}
|
2000-11-03 05:37:44 +00:00
|
|
|
break;
|
2010-03-27 09:12:48 -04:00
|
|
|
#endif
|
2000-11-03 05:37:44 +00:00
|
|
|
|
|
|
|
case PERM_SUDOERS:
|
2004-05-17 20:08:46 +00:00
|
|
|
/* assume euid == ROOT_UID, ruid == user */
|
2002-12-15 16:24:24 +00:00
|
|
|
if (setregid(-1, SUDOERS_GID))
|
2005-02-10 05:03:58 +00:00
|
|
|
error(1, "unable to change to sudoers gid");
|
2000-11-03 05:37:44 +00:00
|
|
|
|
|
|
|
/*
|
2004-05-17 20:08:46 +00:00
|
|
|
* If SUDOERS_UID == ROOT_UID and SUDOERS_MODE
|
2000-11-03 05:37:44 +00:00
|
|
|
* is group readable we use a non-zero
|
|
|
|
* uid in order to avoid NFS lossage.
|
|
|
|
* Using uid 1 is a bit bogus but should
|
|
|
|
* work on all OS's.
|
|
|
|
*/
|
2004-05-17 20:08:46 +00:00
|
|
|
if (SUDOERS_UID == ROOT_UID) {
|
2008-01-27 21:37:54 +00:00
|
|
|
if ((SUDOERS_MODE & 040) && setreuid(ROOT_UID, 1)) {
|
|
|
|
errstr = "setreuid(ROOT_UID, 1)";
|
|
|
|
goto bad;
|
|
|
|
}
|
2000-11-03 05:37:44 +00:00
|
|
|
} else {
|
2008-01-27 21:37:54 +00:00
|
|
|
if (setreuid(ROOT_UID, SUDOERS_UID)) {
|
|
|
|
errstr = "setreuid(ROOT_UID, SUDOERS_UID)";
|
|
|
|
goto bad;
|
|
|
|
}
|
2000-11-03 05:37:44 +00:00
|
|
|
}
|
2000-12-30 03:29:47 +00:00
|
|
|
break;
|
2002-05-03 22:48:17 +00:00
|
|
|
case PERM_TIMESTAMP:
|
2008-01-27 21:37:54 +00:00
|
|
|
if (setreuid(ROOT_UID, timestamp_uid)) {
|
|
|
|
errstr = "setreuid(ROOT_UID, timestamp_uid)";
|
|
|
|
goto bad;
|
|
|
|
}
|
2002-05-03 22:48:17 +00:00
|
|
|
break;
|
2000-12-30 03:29:47 +00:00
|
|
|
}
|
2007-11-25 13:07:21 +00:00
|
|
|
|
|
|
|
current_perm = perm;
|
2009-05-10 11:52:13 +00:00
|
|
|
return(1);
|
2008-01-27 21:37:54 +00:00
|
|
|
bad:
|
2009-05-10 11:52:13 +00:00
|
|
|
warningx("%s: %s", errstr,
|
2008-01-27 21:37:54 +00:00
|
|
|
errno == EAGAIN ? "too many processes" : strerror(errno));
|
2009-05-10 11:52:13 +00:00
|
|
|
if (noexit)
|
|
|
|
return(0);
|
|
|
|
exit(1);
|
2000-12-30 03:29:47 +00:00
|
|
|
}
|
|
|
|
|
2004-10-13 16:46:19 +00:00
|
|
|
# else /* !HAVE_SETRESUID && !HAVE_SETREUID */
|
2006-07-31 17:50:06 +00:00
|
|
|
# ifdef HAVE_SETEUID
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set real and effective uids and gids based on perm.
|
|
|
|
* NOTE: does not support the "stay_setuid" option.
|
|
|
|
*/
|
2009-05-10 11:52:13 +00:00
|
|
|
int
|
2006-07-31 17:50:06 +00:00
|
|
|
set_perms(perm)
|
|
|
|
int perm;
|
|
|
|
{
|
2008-01-27 21:37:54 +00:00
|
|
|
const char *errstr;
|
2009-05-10 11:52:13 +00:00
|
|
|
int noexit;
|
|
|
|
|
|
|
|
noexit = ISSET(perm, PERM_NOEXIT);
|
|
|
|
CLR(perm, PERM_MASK);
|
2008-01-27 21:37:54 +00:00
|
|
|
|
2007-11-25 13:07:21 +00:00
|
|
|
if (perm == current_perm)
|
2009-05-10 11:52:13 +00:00
|
|
|
return(1);
|
2007-11-25 13:07:21 +00:00
|
|
|
|
2006-07-31 17:50:06 +00:00
|
|
|
/*
|
|
|
|
* Since we only have setuid() and seteuid() and semantics
|
|
|
|
* for these calls differ on various systems, we set
|
|
|
|
* real and effective uids to ROOT_UID initially to be safe.
|
|
|
|
*/
|
2008-01-27 21:37:54 +00:00
|
|
|
if (seteuid(ROOT_UID)) {
|
|
|
|
errstr = "seteuid(ROOT_UID)";
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
if (setuid(ROOT_UID)) {
|
|
|
|
errstr = "setuid(ROOT_UID)";
|
|
|
|
goto bad;
|
|
|
|
}
|
2006-07-31 17:50:06 +00:00
|
|
|
|
|
|
|
switch (perm) {
|
|
|
|
case PERM_ROOT:
|
2007-07-06 14:14:12 +00:00
|
|
|
/* uid set above */
|
|
|
|
(void) setegid(user_gid);
|
2007-11-25 13:07:21 +00:00
|
|
|
if (current_perm == PERM_RUNAS)
|
|
|
|
restore_groups();
|
2006-07-31 17:50:06 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PERM_USER:
|
|
|
|
(void) setegid(user_gid);
|
2008-01-27 21:37:54 +00:00
|
|
|
if (seteuid(user_uid)) {
|
|
|
|
errstr = "seteuid(user_uid)";
|
|
|
|
goto bad;
|
|
|
|
}
|
2006-07-31 17:50:06 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PERM_FULL_USER:
|
|
|
|
/* headed for exec() */
|
|
|
|
(void) setgid(user_gid);
|
2008-01-27 21:37:54 +00:00
|
|
|
if (setuid(user_uid)) {
|
|
|
|
errstr = "setuid(user_uid)";
|
|
|
|
goto bad;
|
|
|
|
}
|
2006-07-31 17:50:06 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PERM_RUNAS:
|
2007-11-26 00:26:42 +00:00
|
|
|
runas_setgroups();
|
2007-11-21 20:12:00 +00:00
|
|
|
(void) setegid(runas_gr ?
|
|
|
|
runas_gr->gr_gid : runas_pw->pw_gid);
|
2008-01-27 21:37:54 +00:00
|
|
|
if (seteuid(runas_pw ? runas_pw->pw_uid : user_uid)) {
|
|
|
|
errstr = "unable to change to runas uid";
|
|
|
|
goto bad;
|
|
|
|
}
|
2006-07-31 17:50:06 +00:00
|
|
|
break;
|
|
|
|
|
2010-03-27 09:12:48 -04:00
|
|
|
#if 0
|
2006-07-31 17:50:06 +00:00
|
|
|
case PERM_FULL_RUNAS:
|
|
|
|
/* headed for exec() */
|
|
|
|
runas_setup();
|
2008-01-27 21:37:54 +00:00
|
|
|
if (setuid(runas_pw->pw_uid)) {
|
|
|
|
errstr = "unable to change to runas uid";
|
|
|
|
goto bad;
|
|
|
|
}
|
2006-07-31 17:50:06 +00:00
|
|
|
break;
|
2010-03-27 09:12:48 -04:00
|
|
|
#endif
|
2006-07-31 17:50:06 +00:00
|
|
|
|
|
|
|
case PERM_SUDOERS:
|
|
|
|
if (setegid(SUDOERS_GID))
|
|
|
|
error(1, "unable to change to sudoers gid");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If SUDOERS_UID == ROOT_UID and SUDOERS_MODE
|
|
|
|
* is group readable we use a non-zero
|
|
|
|
* uid in order to avoid NFS lossage.
|
|
|
|
* Using uid 1 is a bit bogus but should
|
|
|
|
* work on all OS's.
|
|
|
|
*/
|
|
|
|
if (SUDOERS_UID == ROOT_UID) {
|
2008-01-27 21:37:54 +00:00
|
|
|
if ((SUDOERS_MODE & 040) && seteuid(1)) {
|
|
|
|
errstr = "seteuid(1)";
|
|
|
|
goto bad;
|
|
|
|
}
|
2006-07-31 17:50:06 +00:00
|
|
|
} else {
|
2008-01-27 21:37:54 +00:00
|
|
|
if (seteuid(SUDOERS_UID)) {
|
|
|
|
errstr = "seteuid(SUDOERS_UID)";
|
|
|
|
goto bad;
|
|
|
|
}
|
2006-07-31 17:50:06 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PERM_TIMESTAMP:
|
2008-01-27 21:37:54 +00:00
|
|
|
if (seteuid(timestamp_uid)) {
|
|
|
|
errstr = "seteuid(timestamp_uid)";
|
|
|
|
goto bad;
|
|
|
|
}
|
2006-07-31 17:50:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-11-25 13:07:21 +00:00
|
|
|
|
|
|
|
current_perm = perm;
|
2009-05-10 11:52:13 +00:00
|
|
|
return(1);
|
2008-01-27 21:37:54 +00:00
|
|
|
bad:
|
2009-05-10 11:52:13 +00:00
|
|
|
warningx("%s: %s", errstr,
|
2008-01-27 21:37:54 +00:00
|
|
|
errno == EAGAIN ? "too many processes" : strerror(errno));
|
2009-05-10 11:52:13 +00:00
|
|
|
if (noexit)
|
|
|
|
return(0);
|
|
|
|
exit(1);
|
2006-07-31 17:50:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# else /* !HAVE_SETRESUID && !HAVE_SETREUID && !HAVE_SETEUID */
|
2004-05-27 23:12:02 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set uids and gids based on perm via setuid() and setgid().
|
|
|
|
* NOTE: does not support the "stay_setuid" or timestampowner options.
|
|
|
|
* Also, SUDOERS_UID and SUDOERS_GID are not used.
|
|
|
|
*/
|
2009-05-10 11:52:13 +00:00
|
|
|
int
|
2004-10-13 16:46:19 +00:00
|
|
|
set_perms(perm)
|
2004-05-27 23:12:02 +00:00
|
|
|
int perm;
|
|
|
|
{
|
2008-01-27 21:37:54 +00:00
|
|
|
const char *errstr;
|
2009-05-10 11:52:13 +00:00
|
|
|
int noexit;
|
|
|
|
|
|
|
|
noexit = ISSET(perm, PERM_NOEXIT);
|
|
|
|
CLR(perm, PERM_MASK);
|
2008-01-27 21:37:54 +00:00
|
|
|
|
2007-11-25 13:07:21 +00:00
|
|
|
if (perm == current_perm)
|
2009-05-10 11:52:13 +00:00
|
|
|
return(1);
|
2004-05-27 23:12:02 +00:00
|
|
|
|
|
|
|
switch (perm) {
|
|
|
|
case PERM_ROOT:
|
2008-01-27 21:37:54 +00:00
|
|
|
if (setuid(ROOT_UID)) {
|
|
|
|
errstr = "setuid(ROOT_UID)";
|
|
|
|
goto bad;
|
|
|
|
}
|
2007-11-25 13:07:21 +00:00
|
|
|
if (current_perm == PERM_RUNAS)
|
|
|
|
restore_groups();
|
2004-05-27 23:12:02 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PERM_FULL_USER:
|
|
|
|
(void) setgid(user_gid);
|
2008-01-27 21:37:54 +00:00
|
|
|
if (setuid(user_uid)) {
|
|
|
|
errstr = "setuid(user_uid)";
|
|
|
|
goto bad;
|
|
|
|
}
|
2004-05-27 23:12:02 +00:00
|
|
|
break;
|
|
|
|
|
2010-03-27 09:12:48 -04:00
|
|
|
#if 0
|
2004-05-27 23:12:02 +00:00
|
|
|
case PERM_FULL_RUNAS:
|
|
|
|
runas_setup();
|
2008-01-27 21:37:54 +00:00
|
|
|
if (setuid(runas_pw->pw_uid)) {
|
|
|
|
errstr = "unable to change to runas uid";
|
|
|
|
goto bad;
|
|
|
|
}
|
2004-05-27 23:12:02 +00:00
|
|
|
break;
|
2010-03-27 09:12:48 -04:00
|
|
|
#endif
|
2004-05-27 23:12:02 +00:00
|
|
|
|
|
|
|
case PERM_USER:
|
|
|
|
case PERM_SUDOERS:
|
|
|
|
case PERM_RUNAS:
|
|
|
|
case PERM_TIMESTAMP:
|
|
|
|
/* Unsupported since we can't set euid. */
|
|
|
|
break;
|
|
|
|
}
|
2007-11-25 13:07:21 +00:00
|
|
|
|
|
|
|
current_perm = perm;
|
2009-05-10 11:52:13 +00:00
|
|
|
return(1);
|
2008-01-27 21:37:54 +00:00
|
|
|
bad:
|
2009-05-10 11:52:13 +00:00
|
|
|
warningx("%s: %s", errstr,
|
2008-01-27 21:37:54 +00:00
|
|
|
errno == EAGAIN ? "too many processes" : strerror(errno));
|
2009-05-10 11:52:13 +00:00
|
|
|
if (noexit)
|
|
|
|
return(0);
|
|
|
|
exit(1);
|
2004-05-27 23:12:02 +00:00
|
|
|
}
|
2006-07-31 17:50:06 +00:00
|
|
|
# endif /* HAVE_SETEUID */
|
2002-11-22 19:09:49 +00:00
|
|
|
# endif /* HAVE_SETREUID */
|
|
|
|
#endif /* HAVE_SETRESUID */
|
2000-12-30 03:29:47 +00:00
|
|
|
|
2007-11-25 13:07:21 +00:00
|
|
|
#ifdef HAVE_INITGROUPS
|
|
|
|
static void
|
2007-11-26 00:26:42 +00:00
|
|
|
runas_setgroups()
|
2007-11-25 13:07:21 +00:00
|
|
|
{
|
|
|
|
static int ngroups = -1;
|
2009-05-22 10:37:29 +00:00
|
|
|
#ifdef HAVE_GETGROUPS
|
2007-11-27 23:40:50 +00:00
|
|
|
static GETGROUPS_T *groups;
|
2009-05-22 10:37:29 +00:00
|
|
|
#endif
|
2007-11-26 00:26:42 +00:00
|
|
|
struct passwd *pw;
|
2007-11-25 13:07:21 +00:00
|
|
|
|
|
|
|
if (def_preserve_groups)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Use stashed copy of runas groups if available, else initgroups and stash.
|
|
|
|
*/
|
|
|
|
if (ngroups == -1) {
|
2007-11-26 00:26:42 +00:00
|
|
|
pw = runas_pw ? runas_pw : sudo_user.pw;
|
|
|
|
if (initgroups(pw->pw_name, pw->pw_gid) < 0)
|
2007-11-25 13:07:21 +00:00
|
|
|
log_error(USE_ERRNO|MSG_ONLY, "can't set runas group vector");
|
2009-05-22 10:37:29 +00:00
|
|
|
#ifdef HAVE_GETGROUPS
|
|
|
|
if ((ngroups = getgroups(0, NULL)) > 0) {
|
|
|
|
groups = emalloc2(ngroups, sizeof(GETGROUPS_T));
|
|
|
|
if (getgroups(ngroups, groups) < 0)
|
|
|
|
log_error(USE_ERRNO|MSG_ONLY, "can't get runas group vector");
|
|
|
|
}
|
2007-11-25 13:07:21 +00:00
|
|
|
} else {
|
|
|
|
if (setgroups(ngroups, groups) < 0)
|
|
|
|
log_error(USE_ERRNO|MSG_ONLY, "can't set runas group vector");
|
2009-05-22 10:37:29 +00:00
|
|
|
#endif /* HAVE_GETGROUPS */
|
2007-11-25 13:07:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
restore_groups()
|
|
|
|
{
|
2009-12-13 22:24:34 +00:00
|
|
|
if (user_groups >= 0 && setgroups(user_ngroups, user_groups) < 0)
|
2007-11-25 13:07:21 +00:00
|
|
|
log_error(USE_ERRNO|MSG_ONLY, "can't reset user group vector");
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
static void
|
2007-11-26 00:26:42 +00:00
|
|
|
runas_setgroups()
|
2007-11-25 13:07:21 +00:00
|
|
|
{
|
|
|
|
/* STUB */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
restore_groups()
|
|
|
|
{
|
|
|
|
/* STUB */
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_INITGROUPS */
|
|
|
|
|
2010-03-27 09:12:48 -04:00
|
|
|
#if 0
|
2000-12-30 03:29:47 +00:00
|
|
|
static void
|
|
|
|
runas_setup()
|
|
|
|
{
|
2007-11-21 20:12:00 +00:00
|
|
|
gid_t gid;
|
2000-12-30 03:29:47 +00:00
|
|
|
#ifdef HAVE_LOGIN_CAP_H
|
2005-02-10 05:03:58 +00:00
|
|
|
int flags;
|
2000-12-30 03:29:47 +00:00
|
|
|
extern login_cap_t *lc;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (runas_pw->pw_name != NULL) {
|
2007-11-21 20:12:00 +00:00
|
|
|
gid = runas_gr ? runas_gr->gr_gid : runas_pw->pw_gid;
|
2008-03-06 17:19:57 +00:00
|
|
|
#ifdef HAVE_GETUSERATTR
|
|
|
|
aix_setlimits(runas_pw->pw_name);
|
|
|
|
#endif
|
2001-12-31 17:18:05 +00:00
|
|
|
#ifdef HAVE_PAM
|
|
|
|
pam_prep_user(runas_pw);
|
|
|
|
#endif /* HAVE_PAM */
|
|
|
|
|
2000-12-30 03:29:47 +00:00
|
|
|
#ifdef HAVE_LOGIN_CAP_H
|
2003-12-30 22:20:21 +00:00
|
|
|
if (def_use_loginclass) {
|
2000-12-30 03:29:47 +00:00
|
|
|
/*
|
2009-05-18 10:33:33 +00:00
|
|
|
* We only use setusercontext() to set the nice value and rlimits.
|
2000-12-30 03:29:47 +00:00
|
|
|
*/
|
2001-12-15 00:24:27 +00:00
|
|
|
flags = LOGIN_SETRESOURCES|LOGIN_SETPRIORITY;
|
2005-02-10 05:03:58 +00:00
|
|
|
if (setusercontext(lc, runas_pw, runas_pw->pw_uid, flags)) {
|
2004-05-17 20:08:46 +00:00
|
|
|
if (runas_pw->pw_uid != ROOT_UID)
|
2005-02-10 05:03:58 +00:00
|
|
|
error(1, "unable to set user context");
|
2002-07-20 12:30:45 +00:00
|
|
|
else
|
2005-02-10 05:03:58 +00:00
|
|
|
warning("unable to set user context");
|
2002-07-20 12:30:45 +00:00
|
|
|
}
|
2000-12-30 03:29:47 +00:00
|
|
|
}
|
2007-11-25 13:07:21 +00:00
|
|
|
#endif /* HAVE_LOGIN_CAP_H */
|
|
|
|
/*
|
|
|
|
* Initialize group vector
|
|
|
|
*/
|
2007-11-26 00:26:42 +00:00
|
|
|
runas_setgroups();
|
2009-06-25 12:44:33 +00:00
|
|
|
#ifdef HAVE_SETEUID
|
|
|
|
if (setegid(gid))
|
|
|
|
warning("cannot set egid to runas gid");
|
|
|
|
#endif
|
|
|
|
if (setgid(gid))
|
2009-05-18 10:33:33 +00:00
|
|
|
warning("cannot set gid to runas gid");
|
2000-12-30 03:29:47 +00:00
|
|
|
}
|
|
|
|
}
|
2010-03-27 09:12:48 -04:00
|
|
|
#endif
|