/*
* Copyright (c) 1988 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. 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.
*/
#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1988\
The Regents of the University of California. All rights reserved.");
#endif /* not lint */
/*
* Authorization
*/
switch(pam_err = pam_acct_mgmt(pamh, 0)) {
case PAM_NEW_AUTHTOK_REQD:
pam_err = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK);
if (pam_err != PAM_SUCCESS)
PAM_END("pam_chauthok");
break;
case PAM_SUCCESS:
break;
default:
PAM_END("pam_acct_mgmt");
break;
}
/*
* pam_authenticate might have changed the target user.
* refresh pwd and user
*/
pam_err = pam_get_item(pamh, PAM_USER, &newuser);
if (pam_err != PAM_SUCCESS) {
syslog(LOG_WARNING,
"pam_get_item(PAM_USER): %s", safe_pam_strerror(pamh, pam_err));
} else {
user = (char *)__UNCONST(newuser);
if (getpwnam_r(user, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0 ||
pwd == NULL) {
(void)pam_end(pamh, pam_err);
syslog(LOG_ERR, "unknown login: %s", username);
errx(EXIT_FAILURE, "unknown login: %s", username);
}
}
#define ERRX_PAM_END(args) do { \
(void)pam_end(pamh, pam_err); \
errx args; \
} while (0)
#define ERR_PAM_END(args) do { \
(void)pam_end(pamh, pam_err); \
err args; \
} while (0)
/* force the usage of specified class */
if (class) {
if (ruid)
ERRX_PAM_END((EXIT_FAILURE, "Only root may use -c"));
pwd->pw_class = class;
}
if ((lc = login_getclass(pwd->pw_class)) == NULL)
ERRX_PAM_END((EXIT_FAILURE,
"Unknown class %s\n", pwd->pw_class));
if (asme) {
/* if asme and non-standard target shell, must be root */
if (chshell(pwd->pw_shell) == 0 && ruid)
ERRX_PAM_END((EXIT_FAILURE,
"permission denied (shell)."));
} else if (pwd->pw_shell && *pwd->pw_shell) {
shell = pwd->pw_shell;
iscsh = UNSET;
} else {
shell = _PATH_BSHELL;
iscsh = NO;
}
if ((p = strrchr(shell, '/')) != NULL)
avshell = p + 1;
else
avshell = shell;
/* if we're forking a csh, we want to slightly muck the args */
if (iscsh == UNSET)
iscsh = strstr(avshell, "csh") ? YES : NO;
/*
* Initialize the supplemental groups before pam gets to them,
* so that other pam modules get a chance to add more when
* we do setcred. Note, we don't relinquish our set-userid yet
*/
/* if we aren't changing users, keep the current group members */
if (ruid != pwd->pw_uid &&
setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) == -1)
ERR_PAM_END((EXIT_FAILURE, "setting user context"));
/*
* Manage session.
*/
if (asthem) {
pid_t pid, xpid;
int status = 1;
struct sigaction sa, sa_int, sa_pipe, sa_quit;
int fds[2];
if ((pam_err = pam_open_session(pamh, 0)) != PAM_SUCCESS)
PAM_END("pam_open_session");
/*
* In order to call pam_close_session after the
* command terminates, we need to fork.
*/
sa.sa_flags = SA_RESTART;
sa.sa_handler = SIG_IGN;
(void)sigemptyset(&sa.sa_mask);
(void)sigaction(SIGINT, &sa, &sa_int);
(void)sigaction(SIGQUIT, &sa, &sa_quit);
(void)sigaction(SIGPIPE, &sa, &sa_pipe);
sa.sa_handler = SIG_DFL;
(void)sigaction(SIGTSTP, &sa, NULL);
/*
* Use a pipe to guarantee the order of execution of
* the parent and the child.
*/
if (pipe(fds) == -1) {
warn("pipe failed");
goto out;
}
/*
* The child: starting here, we don't have to care about
* handling PAM issues if we exit, the parent will do the
* job when we exit.
*/
#undef PAM_END
#undef ERR_PAM_END
#undef ERRX_PAM_END
if (!asme) {
if (asthem) {
char **pamenv;
p = getenv("TERM");
/*
* Create an empty environment
*/
environ = emalloc(sizeof(char *));
environ[0] = NULL;
/*
* Add PAM environement, before the LOGIN_CAP stuff:
* if the login class is unspecified, we'll get the
* same data from PAM, if -c was used, the specified
* class must override PAM.
*/
if ((pamenv = pam_getenvlist(pamh)) != NULL) {
char **envitem;
/*
* XXX Here FreeBSD filters out
* SHELL, LOGNAME, MAIL, CDPATH, IFS, PATH
* how could we get untrusted data here?
*/
for (envitem = pamenv; *envitem; envitem++) {
if (putenv(*envitem) == -1)
free(*envitem);
}
free(pamenv);
}
if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETPATH |
LOGIN_SETENV | LOGIN_SETUMASK) == -1)
err(EXIT_FAILURE, "setting user context");
if (p)
(void)setenv("TERM", p, 1);
}
if (ruid != 0)
syslog(LOG_NOTICE, "%s to %s%s",
username, pwd->pw_name, ontty());
/* Raise our priority back to what we had before */
(void)setpriority(PRIO_PROCESS, 0, prio);
/*
* Set user context, except for umask, and the stuff
* we have done before.
*/
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) == -1)
err(EXIT_FAILURE, "setusercontext");
if (!asme) {
if (asthem) {
if (gohome && chdir(pwd->pw_dir) == -1)
errx(EXIT_FAILURE, "no directory");
}
}