#include <config.h>
#ifdef HAVE_PAM
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pwd.h>
#include <errno.h>
#ifdef HAVE_PAM_PAM_APPL_H
# include <pam/pam_appl.h>
#else
# include <security/pam_appl.h>
#endif
#ifdef __hpux
# include <nl_types.h>
#endif
#ifdef HAVE_LIBINTL_H
# if defined(__LINUX_PAM__)
# define PAM_TEXT_DOMAIN "Linux-PAM"
# elif defined(__sun__)
# define PAM_TEXT_DOMAIN "SUNW_OST_SYSOSPAM"
# endif
#endif
#ifdef PAM_TEXT_DOMAIN
# define dgt(d, t) dgettext(d, t)
#endif
#include <sudoers.h>
#include "sudo_auth.h"
#ifdef PAM_SUN_CODEBASE
# define PAM_CONST
#else
# define PAM_CONST const
#endif
#ifdef PAM_SUN_CODEBASE
# define PAM_MSG_GET(msg, n) (*(msg) + (n))
#else
# define PAM_MSG_GET(msg, n) ((msg)[(n)])
#endif
#ifndef PAM_DATA_SILENT
#define PAM_DATA_SILENT 0
#endif
struct sudo_pam_closure {
const struct sudoers_context *ctx;
struct sudo_conv_callback *callback;
};
struct conv_filter {
char *msg;
size_t msglen;
};
static int converse(int, PAM_CONST struct pam_message **,
struct pam_response **, void *);
static struct sudo_pam_closure pam_closure;
static struct pam_conv pam_conv = { converse, &pam_closure };
static const char *def_prompt = PASSPROMPT;
static bool getpass_error;
static bool noninteractive;
static pam_handle_t *pamh;
static struct conv_filter *conv_filter;
static void
conv_filter_init(const struct sudoers_context *ctx)
{
debug_decl(conv_filter_init, SUDOERS_DEBUG_AUTH);
#ifdef __hpux
if (!ISSET(ctx->mode, MODE_SHELL|MODE_LOGIN_SHELL)) {
int i, nfilt = 0, maxfilters = 0;
struct conv_filter *newfilt;
nl_catd catd;
char *msg;
if ((catd = catopen("pam_comsec", NL_CAT_LOCALE)) != -1) {
maxfilters += 4;
newfilt = reallocarray(conv_filter, maxfilters + 1,
sizeof(*conv_filter));
if (newfilt != NULL) {
conv_filter = newfilt;
for (i = 1; i < 5; i++) {
if ((msg = catgets(catd, 1, i, NULL)) == NULL)
break;
sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
"adding \"%s\" to conversation filter", msg);
if ((conv_filter[nfilt].msg = strdup(msg)) == NULL)
break;
conv_filter[nfilt].msglen = strcspn(msg, "%");
nfilt++;
}
}
}
if ((catd = catopen("pam_hpsec", NL_CAT_LOCALE)) != -1) {
maxfilters += 2;
newfilt = reallocarray(conv_filter, maxfilters + 1,
sizeof(*conv_filter));
if (newfilt != NULL) {
conv_filter = newfilt;
for (i = 3; i < 5; i++) {
if ((msg = catgets(catd, 1, i, NULL)) == NULL)
break;
sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
"adding \"%s\" to conversation filter", msg);
if ((conv_filter[nfilt].msg = strdup(msg)) == NULL)
break;
conv_filter[nfilt].msglen = strcspn(msg, "%");
nfilt++;
}
}
}
if (conv_filter != NULL) {
conv_filter[nfilt].msg = NULL;
conv_filter[nfilt].msglen = 0;
}
}
#endif
debug_return;
}
static const char *
sudo_pam_strerror(pam_handle_t *handle, int errnum)
{
const char *errstr;
static char errbuf[32];
if (errnum == PAM_SYSTEM_ERR)
return strerror(errno);
if ((errstr = pam_strerror(handle, errnum)) == NULL)
(void)snprintf(errbuf, sizeof(errbuf), "PAM error %d", errnum);
return errstr;
}
static int
sudo_pam_init2(const struct sudoers_context *ctx, struct passwd *pw,
sudo_auth *auth, bool quiet)
{
static int pam_status = PAM_SUCCESS;
const char *ttypath = ctx->user.ttypath;
const char *errstr, *pam_service;
int rc;
debug_decl(sudo_pam_init, SUDOERS_DEBUG_AUTH);
auth->data = &pam_status;
if (pamh != NULL) {
debug_return_int(AUTH_SUCCESS);
}
noninteractive = IS_NONINTERACTIVE(auth);
pam_closure.ctx = ctx;
if (ISSET(ctx->mode, MODE_ASKPASS) && def_pam_askpass_service != NULL) {
pam_service = def_pam_askpass_service;
} else {
pam_service = ISSET(ctx->mode, MODE_LOGIN_SHELL) ?
def_pam_login_service : def_pam_service;
}
pam_status = pam_start(pam_service, pw->pw_name, &pam_conv, &pamh);
if (pam_status != PAM_SUCCESS) {
errstr = sudo_pam_strerror(NULL, pam_status);
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"pam_start(%s, %s, %p, %p): %s", pam_service, pw->pw_name,
&pam_conv, &pamh, errstr);
if (!quiet)
log_warningx(ctx, 0, N_("unable to initialize PAM: %s"), errstr);
debug_return_int(AUTH_ERROR);
}
conv_filter_init(ctx);
if (def_pam_ruser) {
rc = pam_set_item(pamh, PAM_RUSER, ctx->user.name);
if (rc != PAM_SUCCESS) {
errstr = sudo_pam_strerror(pamh, rc);
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"pam_set_item(pamh, PAM_RUSER, %s): %s", ctx->user.name, errstr);
}
}
if (def_pam_rhost) {
rc = pam_set_item(pamh, PAM_RHOST, ctx->user.host);
if (rc != PAM_SUCCESS) {
errstr = sudo_pam_strerror(pamh, rc);
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"pam_set_item(pamh, PAM_RHOST, %s): %s", ctx->user.host, errstr);
}
}
if (ttypath != NULL) {
rc = pam_set_item(pamh, PAM_TTY, ttypath);
if (rc != PAM_SUCCESS) {
errstr = sudo_pam_strerror(pamh, rc);
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"pam_set_item(pamh, PAM_TTY, %s): %s", ttypath, errstr);
}
}
if (!def_pam_session && !def_pam_setcred)
auth->end_session = NULL;
debug_return_int(AUTH_SUCCESS);
}
int
sudo_pam_init(const struct sudoers_context *ctx, struct passwd *pw,
sudo_auth *auth)
{
return sudo_pam_init2(ctx, pw, auth, false);
}
#ifdef _AIX
int
sudo_pam_init_quiet(const struct sudoers_context *ctx, struct passwd *pw,
sudo_auth *auth)
{
return sudo_pam_init2(ctx, pw, auth, true);
}
#endif
int
sudo_pam_verify(const struct sudoers_context *ctx, struct passwd *pw,
const char *prompt, sudo_auth *auth, struct sudo_conv_callback *callback)
{
const char *envccname, *pam_user;
int rc, *pam_status = (int *)auth->data;
debug_decl(sudo_pam_verify, SUDOERS_DEBUG_AUTH);
def_prompt = prompt;
getpass_error = false;
pam_closure.callback = callback;
envccname = sudo_getenv("KRB5CCNAME");
if (envccname == NULL && ctx->user.ccname != NULL) {
if (sudo_setenv("KRB5CCNAME", ctx->user.ccname, true) != 0) {
sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,
"unable to set KRB5CCNAME");
debug_return_int(AUTH_FAILURE);
}
}
*pam_status = pam_authenticate(pamh, def_pam_silent ? PAM_SILENT : 0);
def_prompt = PASSPROMPT;
if (envccname == NULL && sudo_unsetenv("KRB5CCNAME") != 0) {
sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,
"unable to restore KRB5CCNAME");
debug_return_int(AUTH_FAILURE);
}
if (getpass_error) {
debug_return_int(noninteractive ? AUTH_NONINTERACTIVE : AUTH_INTR);
}
switch (*pam_status) {
case PAM_SUCCESS:
rc = pam_get_item(pamh, PAM_USER, (PAM_CONST void **)&pam_user);
if (rc == PAM_SUCCESS &&
(pam_user == NULL || strcmp(pam_user, pw->pw_name) != 0)) {
sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,
"unable to authenticate '%s' as user '%s'",
pw->pw_name, pam_user);
debug_return_int(AUTH_FAILURE);
}
debug_return_int(AUTH_SUCCESS);
case PAM_AUTH_ERR:
case PAM_AUTHINFO_UNAVAIL:
case PAM_MAXTRIES:
case PAM_PERM_DENIED:
sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,
"pam_authenticate: %d", *pam_status);
debug_return_int(AUTH_FAILURE);
default:
log_warningx(ctx, 0, N_("PAM authentication error: %s"),
sudo_pam_strerror(pamh, *pam_status));
debug_return_int(AUTH_ERROR);
}
}
int
sudo_pam_approval(const struct sudoers_context *ctx, struct passwd *pw,
sudo_auth *auth, bool exempt)
{
const char *s;
int rc, status = AUTH_SUCCESS;
int *pam_status = (int *) auth->data;
debug_decl(sudo_pam_approval, SUDOERS_DEBUG_AUTH);
if (def_pam_acct_mgmt) {
rc = pam_acct_mgmt(pamh, PAM_SILENT);
switch (rc) {
case PAM_SUCCESS:
break;
case PAM_AUTH_ERR:
log_warningx(ctx, 0, N_("account validation failure, "
"is your account locked?"));
status = AUTH_ERROR;
break;
case PAM_NEW_AUTHTOK_REQD:
if (exempt) {
rc = *pam_status;
break;
}
log_warningx(ctx, 0, N_("Account or password is "
"expired, reset your password and try again"));
rc = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK);
if (rc == PAM_SUCCESS)
break;
s = pam_strerror(pamh, rc);
log_warningx(ctx, 0,
N_("unable to change expired password: %s"), s);
status = AUTH_FAILURE;
break;
case PAM_AUTHTOK_EXPIRED:
if (exempt) {
rc = *pam_status;
break;
}
log_warningx(ctx, 0,
N_("Password expired, contact your system administrator"));
status = AUTH_ERROR;
break;
case PAM_ACCT_EXPIRED:
log_warningx(ctx, 0,
N_("Account expired or PAM config lacks an \"account\" "
"section for sudo, contact your system administrator"));
status = AUTH_ERROR;
break;
case PAM_AUTHINFO_UNAVAIL:
case PAM_MAXTRIES:
case PAM_PERM_DENIED:
s = sudo_pam_strerror(pamh, rc);
log_warningx(ctx, 0, N_("PAM account management error: %s"), s);
status = AUTH_FAILURE;
break;
default:
s = sudo_pam_strerror(pamh, rc);
log_warningx(ctx, 0, N_("PAM account management error: %s"), s);
status = AUTH_ERROR;
break;
}
*pam_status = rc;
}
debug_return_int(status);
}
int
sudo_pam_cleanup(const struct sudoers_context *ctx, struct passwd *pw,
sudo_auth *auth, bool force)
{
int *pam_status = (int *) auth->data;
debug_decl(sudo_pam_cleanup, SUDOERS_DEBUG_AUTH);
if (force || *pam_status != PAM_SUCCESS || auth->end_session == NULL) {
*pam_status = pam_end(pamh, *pam_status | PAM_DATA_SILENT);
pamh = NULL;
}
debug_return_int(*pam_status == PAM_SUCCESS ? AUTH_SUCCESS : AUTH_FAILURE);
}
int
sudo_pam_begin_session(const struct sudoers_context *ctx, struct passwd *pw,
char **user_envp[], sudo_auth *auth)
{
int rc, status = AUTH_SUCCESS;
int *pam_status = (int *) auth->data;
const char *errstr;
debug_decl(sudo_pam_begin_session, SUDOERS_DEBUG_AUTH);
if (pw == NULL) {
if (pamh != NULL) {
rc = pam_end(pamh, PAM_SUCCESS | PAM_DATA_SILENT);
if (rc != PAM_SUCCESS) {
errstr = sudo_pam_strerror(pamh, rc);
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"pam_end: %s", errstr);
}
pamh = NULL;
}
goto done;
}
rc = pam_set_item(pamh, PAM_USER, pw->pw_name);
if (rc != PAM_SUCCESS) {
errstr = sudo_pam_strerror(pamh, rc);
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"pam_set_item(pamh, PAM_USER, %s): %s", pw->pw_name, errstr);
}
if (def_pam_setcred) {
rc = pam_setcred(pamh, PAM_REINITIALIZE_CRED);
if (rc != PAM_SUCCESS) {
errstr = sudo_pam_strerror(pamh, rc);
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"pam_setcred: %s", errstr);
def_pam_setcred = false;
}
}
if (def_pam_session) {
const bool silent = !ISSET(ctx->mode, MODE_SHELL|MODE_LOGIN_SHELL);
rc = pam_open_session(pamh, silent ? PAM_SILENT : 0);
switch (rc) {
case PAM_SUCCESS:
break;
case PAM_SESSION_ERR:
errstr = sudo_pam_strerror(pamh, rc);
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"pam_open_session: %s", errstr);
def_pam_session = false;
break;
default:
*pam_status = rc;
errstr = sudo_pam_strerror(pamh, rc);
log_warningx(ctx, 0, N_("%s: %s"), "pam_open_session", errstr);
rc = pam_end(pamh, *pam_status | PAM_DATA_SILENT);
if (rc != PAM_SUCCESS) {
errstr = sudo_pam_strerror(pamh, rc);
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"pam_end: %s", errstr);
}
pamh = NULL;
status = AUTH_ERROR;
goto done;
}
}
#ifdef HAVE_PAM_GETENVLIST
if (user_envp != NULL) {
char **pam_envp = pam_getenvlist(pamh);
if (pam_envp != NULL) {
if (!env_init(*user_envp) || !env_merge(ctx, pam_envp))
status = AUTH_ERROR;
*user_envp = env_get();
free(pam_envp);
}
}
#endif
done:
debug_return_int(status);
}
int
sudo_pam_end_session(sudo_auth *auth)
{
int rc, status = AUTH_SUCCESS;
const char *errstr;
debug_decl(sudo_pam_end_session, SUDOERS_DEBUG_AUTH);
if (pamh != NULL) {
if (def_pam_session) {
rc = pam_close_session(pamh, PAM_SILENT);
if (rc != PAM_SUCCESS) {
errstr = sudo_pam_strerror(pamh, rc);
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"pam_close_session: %s", errstr);
}
}
if (def_pam_setcred) {
rc = pam_setcred(pamh, PAM_DELETE_CRED | PAM_SILENT);
if (rc != PAM_SUCCESS) {
errstr = sudo_pam_strerror(pamh, rc);
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"pam_setcred: %s", errstr);
}
}
rc = pam_end(pamh, PAM_SUCCESS | PAM_DATA_SILENT);
if (rc != PAM_SUCCESS) {
errstr = sudo_pam_strerror(pamh, rc);
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"pam_end: %s", errstr);
status = AUTH_ERROR;
}
pamh = NULL;
}
debug_return_int(status);
}
#define PROMPT_IS_PASSWORD(_p) \
(strncmp((_p), "Password:", 9) == 0 && \
((_p)[9] == '\0' || ((_p)[9] == ' ' && (_p)[10] == '\0')))
#ifdef PAM_TEXT_DOMAIN
# define PAM_PROMPT_IS_PASSWORD(_p) \
(strcmp((_p), dgt(PAM_TEXT_DOMAIN, "Password:")) == 0 || \
strcmp((_p), dgt(PAM_TEXT_DOMAIN, "Password: ")) == 0 || \
PROMPT_IS_PASSWORD(_p))
#else
# define PAM_PROMPT_IS_PASSWORD(_p) PROMPT_IS_PASSWORD(_p)
#endif
static bool
use_pam_prompt(const char *pam_prompt)
{
size_t user_len;
debug_decl(use_pam_prompt, SUDOERS_DEBUG_AUTH);
if (def_passprompt_override)
debug_return_bool(false);
if (PROMPT_IS_PASSWORD(def_prompt))
debug_return_bool(true);
if (PAM_PROMPT_IS_PASSWORD(pam_prompt))
debug_return_bool(false);
if (pam_closure.ctx != NULL) {
const char *user_name = pam_closure.ctx->user.name;
user_len = strlen(user_name);
if (strncmp(pam_prompt, user_name, user_len) == 0) {
const char *cp = pam_prompt + user_len;
if (strncmp(cp, "'s Password:", 12) == 0 &&
(cp[12] == '\0' || (cp[12] == ' ' && cp[13] == '\0')))
debug_return_bool(false);
}
}
debug_return_bool(true);
}
static bool
is_filtered(const char *msg)
{
bool filtered = false;
if (conv_filter != NULL) {
struct conv_filter *filt = conv_filter;
while (filt->msg != NULL) {
if (strncmp(msg, filt->msg, filt->msglen) == 0) {
filtered = true;
break;
}
filt++;
}
}
return filtered;
}
static int
converse(int num_msg, PAM_CONST struct pam_message **msg,
struct pam_response **reply_out, void *appdata_ptr)
{
struct sudo_conv_callback *callback = NULL;
struct sudo_pam_closure *closure = appdata_ptr;
struct pam_response *reply;
const char *prompt;
char *pass;
int n, type;
debug_decl(converse, SUDOERS_DEBUG_AUTH);
if (num_msg <= 0 || num_msg > PAM_MAX_NUM_MSG) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"invalid number of PAM messages: %d", num_msg);
debug_return_int(PAM_CONV_ERR);
}
sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
"number of PAM messages: %d", num_msg);
reply = calloc((size_t)num_msg, sizeof(struct pam_response));
if (reply == NULL) {
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
debug_return_int(PAM_BUF_ERR);
}
if (closure != NULL)
callback = closure->callback;
for (n = 0; n < num_msg; n++) {
PAM_CONST struct pam_message *pm = PAM_MSG_GET(msg, n);
type = SUDO_CONV_PROMPT_ECHO_OFF;
switch (pm->msg_style) {
case PAM_PROMPT_ECHO_ON:
type = SUDO_CONV_PROMPT_ECHO_ON;
FALLTHROUGH;
case PAM_PROMPT_ECHO_OFF:
if (getpass_error)
goto bad;
if (noninteractive) {
getpass_error = true;
goto bad;
}
prompt = use_pam_prompt(pm->msg) ? pm->msg : def_prompt;
pass = auth_getpass(prompt, type, callback);
if (pass == NULL) {
getpass_error = true;
goto bad;
}
if (strlen(pass) >= PAM_MAX_RESP_SIZE) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"password longer than %d", PAM_MAX_RESP_SIZE);
freezero(pass, strlen(pass));
pass = NULL;
goto bad;
}
reply[n].resp = pass;
break;
case PAM_TEXT_INFO:
if (pm->msg != NULL && !is_filtered(pm->msg))
sudo_printf(SUDO_CONV_INFO_MSG|SUDO_CONV_PREFER_TTY,
"%s\n", pm->msg);
break;
case PAM_ERROR_MSG:
if (pm->msg != NULL)
sudo_printf(SUDO_CONV_ERROR_MSG|SUDO_CONV_PREFER_TTY,
"%s\n", pm->msg);
break;
default:
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unsupported message style: %d", pm->msg_style);
goto bad;
}
}
*reply_out = reply;
debug_return_int(PAM_SUCCESS);
bad:
for (n = 0; n < num_msg; n++) {
struct pam_response *pr = &reply[n];
if (pr->resp != NULL) {
freezero(pr->resp, strlen(pr->resp));
pr->resp = NULL;
}
}
free(reply);
debug_return_int(PAM_CONV_ERR);
}
#endif