1994-05-27 12:33:43 +00:00
|
|
|
/*
|
2005-01-17 19:57:59 +00:00
|
|
|
* Copyright (c) 2002, 2005 Networks Associates Technologies, Inc.
|
2002-05-28 06:47:32 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Portions of this software were developed for the FreeBSD Project by
|
|
|
|
* ThinkSec AS and NAI Labs, the Security Research Division of Network
|
|
|
|
* Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
|
|
|
|
* ("CBOSS"), as part of the DARPA CHATS research program.
|
1994-05-27 12:33:43 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2005-01-17 19:57:59 +00:00
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1988, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
1994-05-27 12:33:43 +00:00
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1996-03-09 14:57:43 +00:00
|
|
|
static const char copyright[] =
|
1994-05-27 12:33:43 +00:00
|
|
|
"@(#) Copyright (c) 1988, 1993, 1994\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
1997-08-12 06:45:43 +00:00
|
|
|
#if 0
|
2004-04-04 18:56:53 +00:00
|
|
|
#ifndef lint
|
1994-05-27 12:33:43 +00:00
|
|
|
static char sccsid[] = "@(#)su.c 8.3 (Berkeley) 4/2/94";
|
|
|
|
#endif /* not lint */
|
2004-04-04 18:56:53 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
2001-05-26 09:52:36 +00:00
|
|
|
#include <sys/wait.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2006-09-01 13:39:02 +00:00
|
|
|
#ifdef USE_BSM_AUDIT
|
|
|
|
#include <bsm/libbsm.h>
|
|
|
|
#include <bsm/audit_uevents.h>
|
|
|
|
#endif
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <grp.h>
|
2001-05-26 09:52:36 +00:00
|
|
|
#include <login_cap.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <paths.h>
|
|
|
|
#include <pwd.h>
|
2001-05-26 09:52:36 +00:00
|
|
|
#include <signal.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <unistd.h>
|
2006-09-01 13:39:02 +00:00
|
|
|
#include <stdarg.h>
|
1997-01-13 06:39:19 +00:00
|
|
|
|
2001-03-27 19:40:51 +00:00
|
|
|
#include <security/pam_appl.h>
|
2002-03-06 12:46:56 +00:00
|
|
|
#include <security/openpam.h>
|
2001-03-27 19:40:51 +00:00
|
|
|
|
2003-04-08 16:59:53 +00:00
|
|
|
#define PAM_END() do { \
|
|
|
|
int local_ret; \
|
|
|
|
if (pamh != NULL) { \
|
|
|
|
local_ret = pam_setcred(pamh, PAM_DELETE_CRED); \
|
|
|
|
if (local_ret != PAM_SUCCESS) \
|
|
|
|
syslog(LOG_ERR, "pam_setcred: %s", \
|
|
|
|
pam_strerror(pamh, local_ret)); \
|
|
|
|
if (asthem) { \
|
|
|
|
local_ret = pam_close_session(pamh, 0); \
|
|
|
|
if (local_ret != PAM_SUCCESS) \
|
|
|
|
syslog(LOG_ERR, "pam_close_session: %s",\
|
|
|
|
pam_strerror(pamh, local_ret)); \
|
|
|
|
} \
|
|
|
|
local_ret = pam_end(pamh, local_ret); \
|
|
|
|
if (local_ret != PAM_SUCCESS) \
|
|
|
|
syslog(LOG_ERR, "pam_end: %s", \
|
|
|
|
pam_strerror(pamh, local_ret)); \
|
|
|
|
} \
|
2001-05-26 09:52:36 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2003-04-08 16:59:53 +00:00
|
|
|
#define PAM_SET_ITEM(what, item) do { \
|
|
|
|
int local_ret; \
|
|
|
|
local_ret = pam_set_item(pamh, what, item); \
|
|
|
|
if (local_ret != PAM_SUCCESS) { \
|
|
|
|
syslog(LOG_ERR, "pam_set_item(" #what "): %s", \
|
|
|
|
pam_strerror(pamh, local_ret)); \
|
|
|
|
errx(1, "pam_set_item(" #what "): %s", \
|
|
|
|
pam_strerror(pamh, local_ret)); \
|
2004-06-13 11:21:06 +00:00
|
|
|
/* NOTREACHED */ \
|
2003-04-08 16:59:53 +00:00
|
|
|
} \
|
2001-05-26 09:52:36 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
enum tristate { UNSET, YES, NO };
|
2001-03-27 19:40:51 +00:00
|
|
|
|
|
|
|
static pam_handle_t *pamh = NULL;
|
2001-05-26 09:52:36 +00:00
|
|
|
static char **environ_pam;
|
1994-10-19 00:03:45 +00:00
|
|
|
|
2001-05-26 09:52:36 +00:00
|
|
|
static char *ontty(void);
|
2004-06-13 11:21:06 +00:00
|
|
|
static int chshell(const char *);
|
|
|
|
static void usage(void) __dead2;
|
|
|
|
static void export_pam_environment(void);
|
2001-05-26 09:52:36 +00:00
|
|
|
static int ok_to_export(const char *);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2001-05-26 09:52:36 +00:00
|
|
|
extern char **environ;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
int
|
2001-05-26 09:52:36 +00:00
|
|
|
main(int argc, char *argv[])
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2004-06-13 11:21:06 +00:00
|
|
|
static char *cleanenv;
|
2001-05-26 09:52:36 +00:00
|
|
|
struct passwd *pwd;
|
2002-03-06 12:46:56 +00:00
|
|
|
struct pam_conv conv = { openpam_ttyconv, NULL };
|
2001-05-26 09:52:36 +00:00
|
|
|
enum tristate iscsh;
|
|
|
|
login_cap_t *lc;
|
2001-09-12 19:15:02 +00:00
|
|
|
union {
|
|
|
|
const char **a;
|
|
|
|
char * const *b;
|
2002-05-28 06:47:32 +00:00
|
|
|
} np;
|
2001-05-26 09:52:36 +00:00
|
|
|
uid_t ruid;
|
2006-01-03 09:17:04 +00:00
|
|
|
pid_t child_pid, child_pgrp, pid;
|
2004-06-13 11:21:06 +00:00
|
|
|
int asme, ch, asthem, fastlogin, prio, i, retcode,
|
2003-04-08 16:59:53 +00:00
|
|
|
statusp, setmaclabel;
|
2004-06-13 11:21:06 +00:00
|
|
|
u_int setwhat;
|
|
|
|
char *username, *class, shellbuf[MAXPATHLEN];
|
2001-09-12 19:15:02 +00:00
|
|
|
const char *p, *user, *shell, *mytty, **nargv;
|
2003-03-27 01:32:51 +00:00
|
|
|
struct sigaction sa, sa_int, sa_quit, sa_pipe;
|
|
|
|
int temp, fds[2];
|
2006-09-01 13:39:02 +00:00
|
|
|
#ifdef USE_BSM_AUDIT
|
|
|
|
const char *aerr;
|
|
|
|
au_id_t auid;
|
|
|
|
#endif
|
2002-06-26 00:42:40 +00:00
|
|
|
|
2001-05-26 09:52:36 +00:00
|
|
|
shell = class = cleanenv = NULL;
|
|
|
|
asme = asthem = fastlogin = statusp = 0;
|
1995-09-06 12:38:53 +00:00
|
|
|
user = "root";
|
2001-05-26 09:52:36 +00:00
|
|
|
iscsh = UNSET;
|
2002-10-23 03:19:34 +00:00
|
|
|
setmaclabel = 0;
|
2001-05-26 09:52:36 +00:00
|
|
|
|
2002-10-23 03:19:34 +00:00
|
|
|
while ((ch = getopt(argc, argv, "-flmsc:")) != -1)
|
2001-05-26 09:52:36 +00:00
|
|
|
switch ((char)ch) {
|
1994-05-27 12:33:43 +00:00
|
|
|
case 'f':
|
|
|
|
fastlogin = 1;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
case 'l':
|
|
|
|
asme = 0;
|
|
|
|
asthem = 1;
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
asme = 1;
|
|
|
|
asthem = 0;
|
|
|
|
break;
|
2002-10-23 03:19:34 +00:00
|
|
|
case 's':
|
|
|
|
setmaclabel = 1;
|
|
|
|
break;
|
1997-10-27 22:05:12 +00:00
|
|
|
case 'c':
|
|
|
|
class = optarg;
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case '?':
|
|
|
|
default:
|
1997-08-12 06:45:43 +00:00
|
|
|
usage();
|
2004-06-13 11:21:06 +00:00
|
|
|
/* NOTREACHED */
|
1997-08-12 06:45:43 +00:00
|
|
|
}
|
1998-09-21 07:47:53 +00:00
|
|
|
|
|
|
|
if (optind < argc)
|
1995-09-06 12:38:53 +00:00
|
|
|
user = argv[optind++];
|
|
|
|
|
1997-08-23 14:09:36 +00:00
|
|
|
if (user == NULL)
|
|
|
|
usage();
|
2004-06-13 11:21:06 +00:00
|
|
|
/* NOTREACHED */
|
1997-08-23 14:09:36 +00:00
|
|
|
|
2005-01-17 19:57:59 +00:00
|
|
|
/*
|
|
|
|
* Try to provide more helpful debugging output if su(1) is running
|
|
|
|
* non-setuid, or was run from a file system not mounted setuid.
|
|
|
|
*/
|
|
|
|
if (geteuid() != 0)
|
|
|
|
errx(1, "not running setuid");
|
|
|
|
|
2006-09-01 13:39:02 +00:00
|
|
|
#ifdef USE_BSM_AUDIT
|
|
|
|
if (getauid(&auid) < 0 && errno != ENOSYS) {
|
|
|
|
syslog(LOG_AUTH | LOG_ERR, "getauid: %s", strerror(errno));
|
|
|
|
errx(1, "Permission denied");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (strlen(user) > MAXLOGNAME - 1) {
|
|
|
|
#ifdef USE_BSM_AUDIT
|
|
|
|
if (audit_submit(AUE_su, auid,
|
|
|
|
1, EPERM, "username too long: '%s'", user))
|
|
|
|
errx(1, "Permission denied");
|
|
|
|
#endif
|
|
|
|
errx(1, "username too long");
|
|
|
|
}
|
|
|
|
|
2004-06-13 11:21:06 +00:00
|
|
|
nargv = malloc(sizeof(char *) * (size_t)(argc + 4));
|
2001-05-26 09:52:36 +00:00
|
|
|
if (nargv == NULL)
|
|
|
|
errx(1, "malloc failure");
|
1995-09-06 12:38:53 +00:00
|
|
|
|
|
|
|
nargv[argc + 3] = NULL;
|
|
|
|
for (i = argc; i >= optind; i--)
|
2001-05-26 09:52:36 +00:00
|
|
|
nargv[i + 3] = argv[i];
|
2001-09-12 19:15:02 +00:00
|
|
|
np.a = &nargv[i + 3];
|
1995-09-06 12:38:53 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
prio = getpriority(PRIO_PROCESS, 0);
|
|
|
|
if (errno)
|
|
|
|
prio = 0;
|
2001-05-26 09:52:36 +00:00
|
|
|
|
|
|
|
setpriority(PRIO_PROCESS, 0, -2);
|
2001-03-27 19:40:51 +00:00
|
|
|
openlog("su", LOG_CONS, LOG_AUTH);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2001-05-26 09:52:36 +00:00
|
|
|
/* get current login name, real uid and shell */
|
1994-05-27 12:33:43 +00:00
|
|
|
ruid = getuid();
|
|
|
|
username = getlogin();
|
2001-05-26 09:52:36 +00:00
|
|
|
pwd = getpwnam(username);
|
|
|
|
if (username == NULL || pwd == NULL || pwd->pw_uid != ruid)
|
1994-05-27 12:33:43 +00:00
|
|
|
pwd = getpwuid(ruid);
|
2006-09-01 13:39:02 +00:00
|
|
|
if (pwd == NULL) {
|
|
|
|
#ifdef USE_BSM_AUDIT
|
|
|
|
if (audit_submit(AUE_su, auid, 1, EPERM,
|
|
|
|
"unable to determine invoking subject: '%s'", username))
|
|
|
|
errx(1, "Permission denied");
|
|
|
|
#endif
|
1994-05-27 12:33:43 +00:00
|
|
|
errx(1, "who are you?");
|
2006-09-01 13:39:02 +00:00
|
|
|
}
|
2001-05-26 09:52:36 +00:00
|
|
|
|
|
|
|
username = strdup(pwd->pw_name);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (username == NULL)
|
2001-05-26 09:52:36 +00:00
|
|
|
err(1, "strdup failure");
|
|
|
|
|
1997-01-13 06:39:19 +00:00
|
|
|
if (asme) {
|
|
|
|
if (pwd->pw_shell != NULL && *pwd->pw_shell != '\0') {
|
2001-05-26 09:52:36 +00:00
|
|
|
/* must copy - pwd memory is recycled */
|
|
|
|
shell = strncpy(shellbuf, pwd->pw_shell,
|
|
|
|
sizeof(shellbuf));
|
|
|
|
shellbuf[sizeof(shellbuf) - 1] = '\0';
|
|
|
|
}
|
|
|
|
else {
|
1994-05-27 12:33:43 +00:00
|
|
|
shell = _PATH_BSHELL;
|
|
|
|
iscsh = NO;
|
|
|
|
}
|
1997-01-13 06:39:19 +00:00
|
|
|
}
|
|
|
|
|
2001-05-26 09:52:36 +00:00
|
|
|
/* Do the whole PAM startup thing */
|
2001-03-27 19:40:51 +00:00
|
|
|
retcode = pam_start("su", user, &conv, &pamh);
|
|
|
|
if (retcode != PAM_SUCCESS) {
|
|
|
|
syslog(LOG_ERR, "pam_start: %s", pam_strerror(pamh, retcode));
|
|
|
|
errx(1, "pam_start: %s", pam_strerror(pamh, retcode));
|
|
|
|
}
|
|
|
|
|
2003-02-06 14:29:28 +00:00
|
|
|
PAM_SET_ITEM(PAM_RUSER, username);
|
2001-08-11 14:24:13 +00:00
|
|
|
|
2001-03-27 19:40:51 +00:00
|
|
|
mytty = ttyname(STDERR_FILENO);
|
|
|
|
if (!mytty)
|
|
|
|
mytty = "tty";
|
2001-05-26 09:52:36 +00:00
|
|
|
PAM_SET_ITEM(PAM_TTY, mytty);
|
|
|
|
|
|
|
|
retcode = pam_authenticate(pamh, 0);
|
2001-03-27 19:40:51 +00:00
|
|
|
if (retcode != PAM_SUCCESS) {
|
2006-09-01 13:39:02 +00:00
|
|
|
#ifdef USE_BSM_AUDIT
|
|
|
|
if (audit_submit(AUE_su, auid, 1, EPERM, "bad su %s to %s on %s",
|
|
|
|
username, user, mytty))
|
|
|
|
errx(1, "Permission denied");
|
|
|
|
#endif
|
2002-10-18 08:23:24 +00:00
|
|
|
syslog(LOG_AUTH|LOG_WARNING, "BAD SU %s to %s on %s",
|
|
|
|
username, user, mytty);
|
2001-05-26 09:52:36 +00:00
|
|
|
errx(1, "Sorry");
|
2001-03-27 19:40:51 +00:00
|
|
|
}
|
2006-09-01 13:39:02 +00:00
|
|
|
#ifdef USE_BSM_AUDIT
|
|
|
|
if (audit_submit(AUE_su, auid, 0, 0, "successful authentication"))
|
|
|
|
errx(1, "Permission denied");
|
|
|
|
#endif
|
2001-05-26 09:52:36 +00:00
|
|
|
retcode = pam_get_item(pamh, PAM_USER, (const void **)&p);
|
|
|
|
if (retcode == PAM_SUCCESS)
|
|
|
|
user = p;
|
|
|
|
else
|
|
|
|
syslog(LOG_ERR, "pam_get_item(PAM_USER): %s",
|
|
|
|
pam_strerror(pamh, retcode));
|
2004-01-06 09:47:24 +00:00
|
|
|
pwd = getpwnam(user);
|
2006-09-01 13:39:02 +00:00
|
|
|
if (pwd == NULL) {
|
|
|
|
#ifdef USE_BSM_AUDIT
|
|
|
|
if (audit_submit(AUE_su, auid, 1, EPERM,
|
|
|
|
"unknown subject: %s", user))
|
|
|
|
errx(1, "Permission denied");
|
|
|
|
#endif
|
2004-01-06 09:47:24 +00:00
|
|
|
errx(1, "unknown login: %s", user);
|
2006-09-01 13:39:02 +00:00
|
|
|
}
|
2001-05-26 09:52:36 +00:00
|
|
|
|
|
|
|
retcode = pam_acct_mgmt(pamh, 0);
|
|
|
|
if (retcode == PAM_NEW_AUTHTOK_REQD) {
|
|
|
|
retcode = pam_chauthtok(pamh,
|
|
|
|
PAM_CHANGE_EXPIRED_AUTHTOK);
|
2001-03-27 19:40:51 +00:00
|
|
|
if (retcode != PAM_SUCCESS) {
|
2006-09-01 13:39:02 +00:00
|
|
|
#ifdef USE_BSM_AUDIT
|
|
|
|
aerr = pam_strerror(pamh, retcode);
|
|
|
|
if (aerr == NULL)
|
|
|
|
aerr = "Unknown PAM error";
|
|
|
|
if (audit_submit(AUE_su, auid, 1, EPERM,
|
|
|
|
"pam_chauthtok: %s", aerr))
|
|
|
|
errx(1, "Permission denied");
|
|
|
|
#endif
|
2001-05-26 09:52:36 +00:00
|
|
|
syslog(LOG_ERR, "pam_chauthtok: %s",
|
|
|
|
pam_strerror(pamh, retcode));
|
2001-03-27 19:40:51 +00:00
|
|
|
errx(1, "Sorry");
|
|
|
|
}
|
|
|
|
}
|
2001-05-26 09:52:36 +00:00
|
|
|
if (retcode != PAM_SUCCESS) {
|
2006-09-01 13:39:02 +00:00
|
|
|
#ifdef USE_BSM_AUDIT
|
|
|
|
if (audit_submit(AUE_su, auid, 1, EPERM, "pam_acct_mgmt: %s",
|
|
|
|
pam_strerror(pamh, retcode)))
|
|
|
|
errx(1, "Permission denied");
|
|
|
|
#endif
|
2001-05-26 09:52:36 +00:00
|
|
|
syslog(LOG_ERR, "pam_acct_mgmt: %s",
|
|
|
|
pam_strerror(pamh, retcode));
|
|
|
|
errx(1, "Sorry");
|
|
|
|
}
|
2001-03-27 19:40:51 +00:00
|
|
|
|
2004-01-06 09:47:24 +00:00
|
|
|
/* get target login information */
|
2001-05-26 09:52:36 +00:00
|
|
|
if (class == NULL)
|
1997-10-27 22:05:12 +00:00
|
|
|
lc = login_getpwclass(pwd);
|
2001-05-26 09:52:36 +00:00
|
|
|
else {
|
2006-09-01 13:39:02 +00:00
|
|
|
if (ruid != 0) {
|
|
|
|
#ifdef USE_BSM_AUDIT
|
|
|
|
if (audit_submit(AUE_su, auid, 1, EPERM,
|
|
|
|
"only root may use -c"))
|
|
|
|
errx(1, "Permission denied");
|
|
|
|
#endif
|
1997-10-27 22:05:12 +00:00
|
|
|
errx(1, "only root may use -c");
|
2006-09-01 13:39:02 +00:00
|
|
|
}
|
1997-10-27 22:05:12 +00:00
|
|
|
lc = login_getclass(class);
|
|
|
|
if (lc == NULL)
|
|
|
|
errx(1, "unknown class: %s", class);
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2001-05-26 09:52:36 +00:00
|
|
|
/* if asme and non-standard target shell, must be root */
|
1994-05-27 12:33:43 +00:00
|
|
|
if (asme) {
|
2001-05-26 09:52:36 +00:00
|
|
|
if (ruid != 0 && !chshell(pwd->pw_shell))
|
2004-04-04 18:56:53 +00:00
|
|
|
errx(1, "permission denied (shell)");
|
2001-05-26 09:52:36 +00:00
|
|
|
}
|
|
|
|
else if (pwd->pw_shell && *pwd->pw_shell) {
|
1994-05-27 12:33:43 +00:00
|
|
|
shell = pwd->pw_shell;
|
|
|
|
iscsh = UNSET;
|
2001-05-26 09:52:36 +00:00
|
|
|
}
|
|
|
|
else {
|
1994-05-27 12:33:43 +00:00
|
|
|
shell = _PATH_BSHELL;
|
|
|
|
iscsh = NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if we're forking a csh, we want to slightly muck the args */
|
|
|
|
if (iscsh == UNSET) {
|
1996-03-09 14:57:43 +00:00
|
|
|
p = strrchr(shell, '/');
|
|
|
|
if (p)
|
1994-05-27 12:33:43 +00:00
|
|
|
++p;
|
|
|
|
else
|
|
|
|
p = shell;
|
2001-05-26 09:52:36 +00:00
|
|
|
iscsh = strcmp(p, "csh") ? (strcmp(p, "tcsh") ? NO : YES) : YES;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2001-05-26 09:52:36 +00:00
|
|
|
setpriority(PRIO_PROCESS, 0, prio);
|
1997-01-13 06:39:19 +00:00
|
|
|
|
2001-03-27 19:40:51 +00:00
|
|
|
/*
|
2001-05-26 09:52:36 +00:00
|
|
|
* PAM modules might add supplementary groups in pam_setcred(), so
|
|
|
|
* initialize them first.
|
2001-03-27 19:40:51 +00:00
|
|
|
*/
|
|
|
|
if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) < 0)
|
|
|
|
err(1, "setusercontext");
|
|
|
|
|
|
|
|
retcode = pam_setcred(pamh, PAM_ESTABLISH_CRED);
|
2003-04-08 16:59:53 +00:00
|
|
|
if (retcode != PAM_SUCCESS) {
|
|
|
|
syslog(LOG_ERR, "pam_setcred: %s",
|
2001-05-26 09:52:36 +00:00
|
|
|
pam_strerror(pamh, retcode));
|
2003-04-08 16:59:53 +00:00
|
|
|
errx(1, "failed to establish credentials.");
|
|
|
|
}
|
|
|
|
if (asthem) {
|
|
|
|
retcode = pam_open_session(pamh, 0);
|
|
|
|
if (retcode != PAM_SUCCESS) {
|
|
|
|
syslog(LOG_ERR, "pam_open_session: %s",
|
|
|
|
pam_strerror(pamh, retcode));
|
|
|
|
errx(1, "failed to open session.");
|
|
|
|
}
|
|
|
|
}
|
2001-03-27 19:40:51 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We must fork() before setuid() because we need to call
|
|
|
|
* pam_setcred(pamh, PAM_DELETE_CRED) as root.
|
|
|
|
*/
|
2002-06-26 00:42:40 +00:00
|
|
|
sa.sa_flags = SA_RESTART;
|
2002-10-17 23:32:44 +00:00
|
|
|
sa.sa_handler = SIG_IGN;
|
2002-06-26 00:42:40 +00:00
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sigaction(SIGINT, &sa, &sa_int);
|
|
|
|
sigaction(SIGQUIT, &sa, &sa_quit);
|
2003-03-27 01:32:51 +00:00
|
|
|
sigaction(SIGPIPE, &sa, &sa_pipe);
|
2003-03-11 09:16:51 +00:00
|
|
|
sa.sa_handler = SIG_DFL;
|
|
|
|
sigaction(SIGTSTP, &sa, NULL);
|
2001-03-27 19:40:51 +00:00
|
|
|
statusp = 1;
|
2003-03-27 01:32:51 +00:00
|
|
|
if (pipe(fds) == -1) {
|
|
|
|
PAM_END();
|
2004-06-13 11:21:06 +00:00
|
|
|
err(1, "pipe");
|
2003-03-27 01:32:51 +00:00
|
|
|
}
|
2001-05-26 09:52:36 +00:00
|
|
|
child_pid = fork();
|
|
|
|
switch (child_pid) {
|
2001-03-27 19:40:51 +00:00
|
|
|
default:
|
2003-11-03 23:54:55 +00:00
|
|
|
sa.sa_handler = SIG_IGN;
|
|
|
|
sigaction(SIGTTOU, &sa, NULL);
|
2003-03-27 01:32:51 +00:00
|
|
|
close(fds[0]);
|
2006-01-03 09:17:04 +00:00
|
|
|
setpgid(child_pid, child_pid);
|
|
|
|
if (tcgetpgrp(STDERR_FILENO) == getpgrp())
|
|
|
|
tcsetpgrp(STDERR_FILENO, child_pid);
|
2003-03-27 01:32:51 +00:00
|
|
|
close(fds[1]);
|
|
|
|
sigaction(SIGPIPE, &sa_pipe, NULL);
|
2003-04-08 16:59:53 +00:00
|
|
|
while ((pid = waitpid(child_pid, &statusp, WUNTRACED)) != -1) {
|
2001-05-26 09:52:36 +00:00
|
|
|
if (WIFSTOPPED(statusp)) {
|
2006-01-03 09:17:04 +00:00
|
|
|
child_pgrp = getpgid(child_pid);
|
|
|
|
if (tcgetpgrp(STDERR_FILENO) == child_pgrp)
|
|
|
|
tcsetpgrp(STDERR_FILENO, getpgrp());
|
2006-01-02 09:46:38 +00:00
|
|
|
kill(getpid(), SIGSTOP);
|
2006-01-03 09:17:04 +00:00
|
|
|
if (tcgetpgrp(STDERR_FILENO) == getpgrp()) {
|
|
|
|
child_pgrp = getpgid(child_pid);
|
|
|
|
tcsetpgrp(STDERR_FILENO, child_pgrp);
|
|
|
|
}
|
2006-01-02 09:46:38 +00:00
|
|
|
kill(child_pid, SIGCONT);
|
2001-05-26 09:52:36 +00:00
|
|
|
statusp = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
2001-03-27 19:40:51 +00:00
|
|
|
}
|
2006-01-03 09:17:04 +00:00
|
|
|
child_pgrp = getpgid(child_pid);
|
|
|
|
if (tcgetpgrp(STDERR_FILENO) == child_pgrp)
|
|
|
|
tcsetpgrp(STDERR_FILENO, getpgrp());
|
2003-04-08 16:59:53 +00:00
|
|
|
if (pid == -1)
|
2001-05-26 09:52:36 +00:00
|
|
|
err(1, "waitpid");
|
2001-08-11 14:22:32 +00:00
|
|
|
PAM_END();
|
2004-06-13 11:21:06 +00:00
|
|
|
exit(WEXITSTATUS(statusp));
|
2001-03-27 19:40:51 +00:00
|
|
|
case -1:
|
2001-08-11 14:22:32 +00:00
|
|
|
PAM_END();
|
2004-06-13 11:21:06 +00:00
|
|
|
err(1, "fork");
|
2001-03-27 19:40:51 +00:00
|
|
|
case 0:
|
2003-03-27 01:32:51 +00:00
|
|
|
close(fds[1]);
|
|
|
|
read(fds[0], &temp, 1);
|
|
|
|
close(fds[0]);
|
|
|
|
sigaction(SIGPIPE, &sa_pipe, NULL);
|
2002-06-26 00:42:40 +00:00
|
|
|
sigaction(SIGINT, &sa_int, NULL);
|
|
|
|
sigaction(SIGQUIT, &sa_quit, NULL);
|
2003-03-27 01:32:51 +00:00
|
|
|
|
2001-05-26 09:52:36 +00:00
|
|
|
/*
|
|
|
|
* Set all user context except for: Environmental variables
|
|
|
|
* Umask Login records (wtmp, etc) Path
|
|
|
|
*/
|
|
|
|
setwhat = LOGIN_SETALL & ~(LOGIN_SETENV | LOGIN_SETUMASK |
|
2002-10-23 03:19:34 +00:00
|
|
|
LOGIN_SETLOGIN | LOGIN_SETPATH | LOGIN_SETGROUP |
|
|
|
|
LOGIN_SETMAC);
|
|
|
|
/*
|
|
|
|
* If -s is present, also set the MAC label.
|
|
|
|
*/
|
|
|
|
if (setmaclabel)
|
|
|
|
setwhat |= LOGIN_SETMAC;
|
2001-05-26 09:52:36 +00:00
|
|
|
/*
|
|
|
|
* Don't touch resource/priority settings if -m has been used
|
|
|
|
* or -l and -c hasn't, and we're not su'ing to root.
|
|
|
|
*/
|
|
|
|
if ((asme || (!asthem && class == NULL)) && pwd->pw_uid)
|
|
|
|
setwhat &= ~(LOGIN_SETPRIORITY | LOGIN_SETRESOURCES);
|
|
|
|
if (setusercontext(lc, pwd, pwd->pw_uid, setwhat) < 0)
|
|
|
|
err(1, "setusercontext");
|
|
|
|
|
|
|
|
if (!asme) {
|
|
|
|
if (asthem) {
|
|
|
|
p = getenv("TERM");
|
|
|
|
environ = &cleanenv;
|
2003-04-08 16:59:53 +00:00
|
|
|
}
|
2001-05-26 09:52:36 +00:00
|
|
|
|
2003-04-08 16:59:53 +00:00
|
|
|
if (asthem || pwd->pw_uid)
|
|
|
|
setenv("USER", pwd->pw_name, 1);
|
|
|
|
setenv("HOME", pwd->pw_dir, 1);
|
|
|
|
setenv("SHELL", shell, 1);
|
|
|
|
|
|
|
|
if (asthem) {
|
2001-05-26 09:52:36 +00:00
|
|
|
/*
|
|
|
|
* Add any environmental variables that the
|
|
|
|
* PAM modules may have set.
|
|
|
|
*/
|
|
|
|
environ_pam = pam_getenvlist(pamh);
|
|
|
|
if (environ_pam)
|
|
|
|
export_pam_environment();
|
|
|
|
|
|
|
|
/* set the su'd user's environment & umask */
|
|
|
|
setusercontext(lc, pwd, pwd->pw_uid,
|
|
|
|
LOGIN_SETPATH | LOGIN_SETUMASK |
|
|
|
|
LOGIN_SETENV);
|
|
|
|
if (p)
|
|
|
|
setenv("TERM", p, 1);
|
2006-09-29 04:41:37 +00:00
|
|
|
|
|
|
|
p = pam_getenv(pamh, "HOME");
|
|
|
|
if (chdir(p ? p : pwd->pw_dir) < 0)
|
|
|
|
errx(1, "no directory");
|
2001-05-26 09:52:36 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2001-05-26 09:52:36 +00:00
|
|
|
login_close(lc);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2001-05-26 09:52:36 +00:00
|
|
|
if (iscsh == YES) {
|
|
|
|
if (fastlogin)
|
2001-09-12 19:15:02 +00:00
|
|
|
*np.a-- = "-f";
|
2001-05-26 09:52:36 +00:00
|
|
|
if (asme)
|
2001-09-12 19:15:02 +00:00
|
|
|
*np.a-- = "-m";
|
2001-05-26 09:52:36 +00:00
|
|
|
}
|
|
|
|
/* csh strips the first character... */
|
2001-09-12 19:15:02 +00:00
|
|
|
*np.a = asthem ? "-su" : iscsh == YES ? "_su" : "su";
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2001-05-26 09:52:36 +00:00
|
|
|
if (ruid != 0)
|
|
|
|
syslog(LOG_NOTICE, "%s to %s%s", username, user,
|
|
|
|
ontty());
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2001-09-12 19:15:02 +00:00
|
|
|
execv(shell, np.b);
|
2001-05-26 09:52:36 +00:00
|
|
|
err(1, "%s", shell);
|
2001-03-27 19:40:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-06-13 11:21:06 +00:00
|
|
|
static void
|
2001-05-26 09:52:36 +00:00
|
|
|
export_pam_environment(void)
|
2001-03-27 19:40:51 +00:00
|
|
|
{
|
|
|
|
char **pp;
|
|
|
|
|
|
|
|
for (pp = environ_pam; *pp != NULL; pp++) {
|
|
|
|
if (ok_to_export(*pp))
|
2001-05-26 09:52:36 +00:00
|
|
|
putenv(*pp);
|
2001-03-27 19:40:51 +00:00
|
|
|
free(*pp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sanity checks on PAM environmental variables:
|
|
|
|
* - Make sure there is an '=' in the string.
|
|
|
|
* - Make sure the string doesn't run on too long.
|
|
|
|
* - Do not export certain variables. This list was taken from the
|
|
|
|
* Solaris pam_putenv(3) man page.
|
2003-04-08 16:59:53 +00:00
|
|
|
* Note that if the user is chrooted, PAM may have a better idea than we
|
|
|
|
* do of where her home directory is.
|
2001-03-27 19:40:51 +00:00
|
|
|
*/
|
|
|
|
static int
|
2001-05-26 09:52:36 +00:00
|
|
|
ok_to_export(const char *s)
|
2001-03-27 19:40:51 +00:00
|
|
|
{
|
|
|
|
static const char *noexport[] = {
|
2003-04-08 16:59:53 +00:00
|
|
|
"SHELL", /* "HOME", */ "LOGNAME", "MAIL", "CDPATH",
|
2001-03-27 19:40:51 +00:00
|
|
|
"IFS", "PATH", NULL
|
|
|
|
};
|
|
|
|
const char **pp;
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
if (strlen(s) > 1024 || strchr(s, '=') == NULL)
|
|
|
|
return 0;
|
|
|
|
if (strncmp(s, "LD_", 3) == 0)
|
|
|
|
return 0;
|
|
|
|
for (pp = noexport; *pp != NULL; pp++) {
|
|
|
|
n = strlen(*pp);
|
|
|
|
if (s[n] == '=' && strncmp(s, *pp, n) == 0)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
1997-08-12 06:45:43 +00:00
|
|
|
static void
|
2001-05-26 09:52:36 +00:00
|
|
|
usage(void)
|
1997-08-12 06:45:43 +00:00
|
|
|
{
|
2001-08-15 15:11:52 +00:00
|
|
|
|
2002-10-23 03:19:34 +00:00
|
|
|
fprintf(stderr, "usage: su [-] [-flms] [-c class] [login [args]]\n");
|
2001-08-15 15:11:52 +00:00
|
|
|
exit(1);
|
2004-06-13 11:21:06 +00:00
|
|
|
/* NOTREACHED */
|
1997-08-12 06:45:43 +00:00
|
|
|
}
|
|
|
|
|
2001-05-26 09:52:36 +00:00
|
|
|
static int
|
2004-06-13 11:21:06 +00:00
|
|
|
chshell(const char *sh)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2001-05-26 09:52:36 +00:00
|
|
|
int r;
|
1994-05-27 12:33:43 +00:00
|
|
|
char *cp;
|
|
|
|
|
2001-05-26 09:52:36 +00:00
|
|
|
r = 0;
|
1997-01-13 06:39:19 +00:00
|
|
|
setusershell();
|
2003-10-19 02:09:36 +00:00
|
|
|
while ((cp = getusershell()) != NULL && !r)
|
|
|
|
r = (strcmp(cp, sh) == 0);
|
1997-01-13 06:39:19 +00:00
|
|
|
endusershell();
|
|
|
|
return r;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2001-05-26 09:52:36 +00:00
|
|
|
static char *
|
|
|
|
ontty(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
static char buf[MAXPATHLEN + 4];
|
|
|
|
|
|
|
|
buf[0] = 0;
|
1996-03-09 14:57:43 +00:00
|
|
|
p = ttyname(STDERR_FILENO);
|
|
|
|
if (p)
|
1994-05-27 12:33:43 +00:00
|
|
|
snprintf(buf, sizeof(buf), " on %s", p);
|
2001-05-26 09:52:36 +00:00
|
|
|
return buf;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|