2002-02-23 01:22:51 +00:00
|
|
|
/*-
|
2003-06-01 12:54:03 +00:00
|
|
|
* Copyright (c) 2002-2003 Networks Associates Technology, Inc.
|
2002-02-23 01:22:51 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This software was developed for the FreeBSD Project by ThinkSec AS and
|
2002-06-30 21:30:05 +00:00
|
|
|
* Network Associates Laboratories, 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.
|
2002-02-23 01:22:51 +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.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior written
|
|
|
|
* permission.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2003-06-01 12:54:03 +00:00
|
|
|
* $P4: //depot/projects/openpam/bin/su/su.c#12 $
|
2002-02-23 01:22:51 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
2003-06-01 12:54:03 +00:00
|
|
|
#include <grp.h>
|
2002-02-23 01:22:51 +00:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdio.h>
|
2002-05-24 13:18:43 +00:00
|
|
|
#include <stdlib.h>
|
2002-06-30 21:30:05 +00:00
|
|
|
#include <string.h>
|
2002-02-23 01:22:51 +00:00
|
|
|
#include <syslog.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <security/pam_appl.h>
|
2002-06-30 21:30:05 +00:00
|
|
|
#include <security/openpam.h> /* for openpam_ttyconv() */
|
|
|
|
|
|
|
|
extern char **environ;
|
2002-02-23 01:22:51 +00:00
|
|
|
|
|
|
|
static pam_handle_t *pamh;
|
|
|
|
static struct pam_conv pamc;
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
fprintf(stderr, "Usage: su [login [args]]\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char hostname[MAXHOSTNAMELEN];
|
|
|
|
const char *user, *tty;
|
2002-06-30 21:30:05 +00:00
|
|
|
char **args, **pam_envlist, **pam_env;
|
2002-02-23 01:22:51 +00:00
|
|
|
struct passwd *pwd;
|
2002-06-30 21:30:05 +00:00
|
|
|
int o, pam_err, status;
|
2002-02-23 01:22:51 +00:00
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
while ((o = getopt(argc, argv, "h")) != -1)
|
|
|
|
switch (o) {
|
|
|
|
case 'h':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2003-06-01 12:54:03 +00:00
|
|
|
if (argc > 0) {
|
|
|
|
user = *argv;
|
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
} else {
|
|
|
|
user = "root";
|
|
|
|
}
|
|
|
|
|
2002-02-23 01:22:51 +00:00
|
|
|
/* initialize PAM */
|
|
|
|
pamc.conv = &openpam_ttyconv;
|
2003-06-01 12:54:03 +00:00
|
|
|
pam_start("su", user, &pamc, &pamh);
|
2002-02-23 01:22:51 +00:00
|
|
|
|
|
|
|
/* set some items */
|
|
|
|
gethostname(hostname, sizeof(hostname));
|
2002-06-30 21:30:05 +00:00
|
|
|
if ((pam_err = pam_set_item(pamh, PAM_RHOST, hostname)) != PAM_SUCCESS)
|
|
|
|
goto pamerr;
|
2002-02-23 01:22:51 +00:00
|
|
|
user = getlogin();
|
2002-06-30 21:30:05 +00:00
|
|
|
if ((pam_err = pam_set_item(pamh, PAM_RUSER, user)) != PAM_SUCCESS)
|
|
|
|
goto pamerr;
|
2002-02-23 01:22:51 +00:00
|
|
|
tty = ttyname(STDERR_FILENO);
|
2002-06-30 21:30:05 +00:00
|
|
|
if ((pam_err = pam_set_item(pamh, PAM_TTY, tty)) != PAM_SUCCESS)
|
|
|
|
goto pamerr;
|
2002-02-23 01:22:51 +00:00
|
|
|
|
|
|
|
/* authenticate the applicant */
|
2002-06-30 21:30:05 +00:00
|
|
|
if ((pam_err = pam_authenticate(pamh, 0)) != PAM_SUCCESS)
|
|
|
|
goto pamerr;
|
|
|
|
if ((pam_err = pam_acct_mgmt(pamh, 0)) == PAM_NEW_AUTHTOK_REQD)
|
|
|
|
pam_err = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK);
|
|
|
|
if (pam_err != PAM_SUCCESS)
|
|
|
|
goto pamerr;
|
2002-02-23 01:22:51 +00:00
|
|
|
|
|
|
|
/* establish the requested credentials */
|
2002-06-30 21:30:05 +00:00
|
|
|
if ((pam_err = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS)
|
|
|
|
goto pamerr;
|
2002-02-23 01:22:51 +00:00
|
|
|
|
|
|
|
/* authentication succeeded; open a session */
|
2002-06-30 21:30:05 +00:00
|
|
|
if ((pam_err = pam_open_session(pamh, 0)) != PAM_SUCCESS)
|
|
|
|
goto pamerr;
|
|
|
|
|
|
|
|
/* get mapped user name; PAM may have changed it */
|
|
|
|
pam_err = pam_get_item(pamh, PAM_USER, (const void **)&user);
|
|
|
|
if (pam_err != PAM_SUCCESS || (pwd = getpwnam(user)) == NULL)
|
|
|
|
goto pamerr;
|
|
|
|
|
|
|
|
/* export PAM environment */
|
|
|
|
if ((pam_envlist = pam_getenvlist(pamh)) != NULL) {
|
|
|
|
for (pam_env = pam_envlist; *pam_env != NULL; ++pam_env) {
|
|
|
|
putenv(*pam_env);
|
|
|
|
free(*pam_env);
|
|
|
|
}
|
|
|
|
free(pam_envlist);
|
|
|
|
}
|
2002-02-23 01:22:51 +00:00
|
|
|
|
2002-06-30 21:30:05 +00:00
|
|
|
/* build argument list */
|
|
|
|
if ((args = calloc(argc + 2, sizeof *args)) == NULL) {
|
|
|
|
warn("calloc()");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
*args = pwd->pw_shell;
|
|
|
|
memcpy(args + 1, argv, argc * sizeof *args);
|
2002-02-23 01:22:51 +00:00
|
|
|
|
2002-06-30 21:30:05 +00:00
|
|
|
/* fork and exec */
|
2002-02-23 01:22:51 +00:00
|
|
|
switch ((pid = fork())) {
|
|
|
|
case -1:
|
2002-06-30 21:30:05 +00:00
|
|
|
warn("fork()");
|
|
|
|
goto err;
|
2002-02-23 01:22:51 +00:00
|
|
|
case 0:
|
2003-06-01 12:54:03 +00:00
|
|
|
/* child: give up privs and start a shell */
|
|
|
|
|
|
|
|
/* set uid and groups */
|
|
|
|
if (initgroups(pwd->pw_name, pwd->pw_gid) == -1) {
|
|
|
|
warn("initgroups()");
|
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
if (setgid(pwd->pw_gid) == -1) {
|
|
|
|
warn("setgid()");
|
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
if (setuid(pwd->pw_uid) == -1) {
|
|
|
|
warn("setuid()");
|
|
|
|
_exit(1);
|
|
|
|
}
|
2002-06-30 21:30:05 +00:00
|
|
|
execve(*args, args, environ);
|
|
|
|
warn("execve()");
|
|
|
|
_exit(1);
|
2002-02-23 01:22:51 +00:00
|
|
|
default:
|
|
|
|
/* parent: wait for child to exit */
|
|
|
|
waitpid(pid, &status, 0);
|
|
|
|
|
2002-06-30 21:30:05 +00:00
|
|
|
/* close the session and release PAM resources */
|
|
|
|
pam_err = pam_close_session(pamh, 0);
|
|
|
|
pam_end(pamh, pam_err);
|
2002-02-23 01:22:51 +00:00
|
|
|
|
2002-06-30 21:30:05 +00:00
|
|
|
exit(WEXITSTATUS(status));
|
|
|
|
}
|
|
|
|
|
|
|
|
pamerr:
|
|
|
|
fprintf(stderr, "Sorry\n");
|
|
|
|
err:
|
|
|
|
pam_end(pamh, pam_err);
|
|
|
|
exit(1);
|
2002-02-23 01:22:51 +00:00
|
|
|
}
|