1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
* 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 */
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-08-12 06:45:43 +00:00
|
|
|
#if 0
|
1994-05-27 12:33:43 +00:00
|
|
|
static char sccsid[] = "@(#)su.c 8.3 (Berkeley) 4/2/94";
|
1997-08-12 06:45:43 +00:00
|
|
|
#endif
|
1996-03-09 14:57:43 +00:00
|
|
|
static const char rcsid[] =
|
1999-08-28 01:08:13 +00:00
|
|
|
"$FreeBSD$";
|
1994-05-27 12:33:43 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <grp.h>
|
2001-05-26 09:52:36 +00:00
|
|
|
#include <libutil.h>
|
|
|
|
#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>
|
1997-01-13 06:39:19 +00:00
|
|
|
|
2001-03-27 19:40:51 +00:00
|
|
|
#include <security/pam_appl.h>
|
|
|
|
#include <security/pam_misc.h>
|
|
|
|
|
2001-05-26 09:52:36 +00:00
|
|
|
#define PAM_END do { \
|
|
|
|
int local_ret; \
|
|
|
|
if (pamh != NULL && creds_set) { \
|
|
|
|
local_ret = pam_setcred(pamh, PAM_DELETE_CRED); \
|
|
|
|
if (local_ret != PAM_SUCCESS) \
|
|
|
|
syslog(LOG_ERR, "pam_setcred: %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)); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
|
|
|
#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)); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define ARGSTR "-flmc:"
|
|
|
|
|
|
|
|
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 int creds_set = 0;
|
|
|
|
static char **environ_pam;
|
1994-10-19 00:03:45 +00:00
|
|
|
|
2001-05-26 09:52:36 +00:00
|
|
|
static char *ontty(void);
|
|
|
|
static int chshell(char *);
|
|
|
|
static void usage(void);
|
|
|
|
static int export_pam_environment(void);
|
|
|
|
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
|
|
|
{
|
2001-05-26 09:52:36 +00:00
|
|
|
struct passwd *pwd;
|
|
|
|
struct pam_conv conv = {misc_conv, NULL};
|
|
|
|
enum tristate iscsh;
|
|
|
|
login_cap_t *lc;
|
|
|
|
uid_t ruid;
|
|
|
|
gid_t gid;
|
|
|
|
int asme, ch, asthem, fastlogin, prio, i, setwhat, retcode,
|
|
|
|
statusp, child_pid, child_pgrp, ret_pid;
|
|
|
|
char *p, *user, *shell, *username, *cleanenv, **nargv, **np,
|
|
|
|
*class, *mytty, shellbuf[MAXPATHLEN],
|
|
|
|
myhost[MAXHOSTNAMELEN + 1];
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
while ((ch = getopt(argc, argv, ARGSTR)) != -1)
|
|
|
|
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;
|
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();
|
|
|
|
}
|
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();
|
|
|
|
|
2001-05-26 09:52:36 +00:00
|
|
|
if (strlen(user) > MAXLOGNAME - 1)
|
|
|
|
errx(1, "username too long");
|
|
|
|
|
|
|
|
nargv = malloc(sizeof(char *) * (argc + 4));
|
|
|
|
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];
|
1995-09-06 12:38:53 +00:00
|
|
|
np = &nargv[i + 3];
|
|
|
|
|
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);
|
|
|
|
if (pwd == NULL)
|
|
|
|
errx(1, "who are you?");
|
1998-05-25 03:34:52 +00:00
|
|
|
gid = pwd->pw_gid;
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
gethostname(myhost, sizeof(myhost));
|
2001-05-26 09:52:36 +00:00
|
|
|
PAM_SET_ITEM(PAM_RHOST, myhost);
|
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) {
|
2001-05-26 09:52:36 +00:00
|
|
|
syslog(LOG_ERR, "pam_authenticate: %s",
|
|
|
|
pam_strerror(pamh, retcode));
|
|
|
|
errx(1, "Sorry");
|
2001-03-27 19:40:51 +00:00
|
|
|
}
|
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));
|
|
|
|
|
|
|
|
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) {
|
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) {
|
|
|
|
syslog(LOG_ERR, "pam_acct_mgmt: %s",
|
|
|
|
pam_strerror(pamh, retcode));
|
|
|
|
errx(1, "Sorry");
|
|
|
|
}
|
2001-03-27 19:40:51 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/* get target login information, default to root */
|
2001-05-26 09:52:36 +00:00
|
|
|
pwd = getpwnam(user);
|
|
|
|
if (pwd == NULL)
|
1995-07-12 20:11:19 +00:00
|
|
|
errx(1, "unknown login: %s", user);
|
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 {
|
|
|
|
if (ruid != 0)
|
1997-10-27 22:05:12 +00:00
|
|
|
errx(1, "only root may use -c");
|
|
|
|
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))
|
1994-05-27 12:33:43 +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);
|
2001-05-26 09:52:36 +00:00
|
|
|
if (retcode != PAM_SUCCESS)
|
|
|
|
syslog(LOG_ERR, "pam_setcred(pamh, PAM_ESTABLISH_CRED): %s",
|
|
|
|
pam_strerror(pamh, retcode));
|
|
|
|
else
|
|
|
|
creds_set = 1;
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
statusp = 1;
|
2001-05-26 09:52:36 +00:00
|
|
|
child_pid = fork();
|
|
|
|
switch (child_pid) {
|
2001-03-27 19:40:51 +00:00
|
|
|
default:
|
2001-05-26 09:52:36 +00:00
|
|
|
while ((ret_pid = waitpid(child_pid, &statusp, WUNTRACED)) != -1) {
|
|
|
|
if (WIFSTOPPED(statusp)) {
|
|
|
|
child_pgrp = tcgetpgrp(1);
|
|
|
|
kill(getpid(), SIGSTOP);
|
|
|
|
tcsetpgrp(1, child_pgrp);
|
|
|
|
kill(child_pid, SIGCONT);
|
|
|
|
statusp = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
2001-03-27 19:40:51 +00:00
|
|
|
}
|
2001-05-26 09:52:36 +00:00
|
|
|
if (ret_pid == -1)
|
|
|
|
err(1, "waitpid");
|
|
|
|
PAM_END;
|
|
|
|
exit(statusp);
|
2001-03-27 19:40:51 +00:00
|
|
|
case -1:
|
2001-05-26 09:52:36 +00:00
|
|
|
err(1, "fork");
|
|
|
|
PAM_END;
|
|
|
|
exit(1);
|
2001-03-27 19:40:51 +00:00
|
|
|
case 0:
|
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 |
|
|
|
|
LOGIN_SETLOGIN | LOGIN_SETPATH | LOGIN_SETGROUP);
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
if (chdir(pwd->pw_dir) < 0)
|
|
|
|
errx(1, "no directory");
|
|
|
|
}
|
|
|
|
if (asthem || pwd->pw_uid)
|
|
|
|
setenv("USER", pwd->pw_name, 1);
|
|
|
|
setenv("HOME", pwd->pw_dir, 1);
|
|
|
|
setenv("SHELL", shell, 1);
|
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)
|
|
|
|
*np-- = "-f";
|
|
|
|
if (asme)
|
|
|
|
*np-- = "-m";
|
|
|
|
}
|
|
|
|
/* csh strips the first character... */
|
|
|
|
*np = 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-05-26 09:52:36 +00:00
|
|
|
execv(shell, np);
|
|
|
|
err(1, "%s", shell);
|
2001-03-27 19:40:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
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);
|
|
|
|
}
|
|
|
|
return PAM_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
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[] = {
|
|
|
|
"SHELL", "HOME", "LOGNAME", "MAIL", "CDPATH",
|
|
|
|
"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-03-27 19:40:51 +00:00
|
|
|
errx(1, "usage: su [%s] [login [args]]", ARGSTR);
|
1997-08-12 06:45:43 +00:00
|
|
|
}
|
|
|
|
|
2001-05-26 09:52:36 +00:00
|
|
|
static int
|
|
|
|
chshell(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();
|
2001-05-26 09:52:36 +00:00
|
|
|
do {
|
|
|
|
cp = getusershell();
|
|
|
|
r = strcmp(cp, sh);
|
|
|
|
} while (!r && cp != NULL);
|
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
|
|
|
}
|