2002-03-21 12:18:27 +00:00
|
|
|
#include "includes.h"
|
2003-04-23 17:13:13 +00:00
|
|
|
RCSID("$Id: auth2-pam.c,v 1.15 2003/01/08 01:37:03 djm Exp $");
|
2002-03-21 12:18:27 +00:00
|
|
|
|
2002-06-27 22:42:11 +00:00
|
|
|
#ifdef USE_PAM
|
2002-03-21 12:18:27 +00:00
|
|
|
#include <security/pam_appl.h>
|
|
|
|
|
2002-06-27 22:42:11 +00:00
|
|
|
#include "ssh.h"
|
|
|
|
#include "ssh2.h"
|
2002-03-21 12:18:27 +00:00
|
|
|
#include "auth.h"
|
2002-06-27 22:42:11 +00:00
|
|
|
#include "auth-pam.h"
|
|
|
|
#include "packet.h"
|
2002-03-21 12:18:27 +00:00
|
|
|
#include "xmalloc.h"
|
2002-06-27 22:42:11 +00:00
|
|
|
#include "dispatch.h"
|
|
|
|
#include "log.h"
|
2002-03-21 12:18:27 +00:00
|
|
|
|
2002-06-27 22:42:11 +00:00
|
|
|
static int do_pam_conversation_kbd_int(int num_msg,
|
|
|
|
const struct pam_message **msg, struct pam_response **resp,
|
|
|
|
void *appdata_ptr);
|
|
|
|
void input_userauth_info_response_pam(int type, u_int32_t seqnr, void *ctxt);
|
2002-03-21 12:18:27 +00:00
|
|
|
|
2002-06-27 22:42:11 +00:00
|
|
|
struct {
|
|
|
|
int finished, num_received, num_expected;
|
|
|
|
int *prompts;
|
|
|
|
struct pam_response *responses;
|
|
|
|
} context_pam2 = {0, 0, 0, NULL};
|
2002-03-21 12:18:27 +00:00
|
|
|
|
2002-06-27 22:42:11 +00:00
|
|
|
static struct pam_conv conv2 = {
|
|
|
|
do_pam_conversation_kbd_int,
|
|
|
|
NULL,
|
|
|
|
};
|
2002-03-21 12:18:27 +00:00
|
|
|
|
2002-06-27 22:42:11 +00:00
|
|
|
int
|
|
|
|
auth2_pam(Authctxt *authctxt)
|
2002-03-21 12:18:27 +00:00
|
|
|
{
|
2002-06-27 22:42:11 +00:00
|
|
|
int retval = -1;
|
2002-03-21 12:18:27 +00:00
|
|
|
|
2002-06-27 22:42:11 +00:00
|
|
|
if (authctxt->user == NULL)
|
|
|
|
fatal("auth2_pam: internal error: no user");
|
2002-03-21 12:18:27 +00:00
|
|
|
|
2002-06-27 22:42:11 +00:00
|
|
|
conv2.appdata_ptr = authctxt;
|
|
|
|
do_pam_set_conv(&conv2);
|
2002-03-21 12:18:27 +00:00
|
|
|
|
2002-06-27 22:42:11 +00:00
|
|
|
dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE,
|
|
|
|
&input_userauth_info_response_pam);
|
|
|
|
retval = (do_pam_authenticate(0) == PAM_SUCCESS);
|
|
|
|
dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL);
|
|
|
|
|
|
|
|
return retval;
|
2002-03-21 12:18:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2002-06-27 22:42:11 +00:00
|
|
|
do_pam_conversation_kbd_int(int num_msg, const struct pam_message **msg,
|
|
|
|
struct pam_response **resp, void *appdata_ptr)
|
2002-03-21 12:18:27 +00:00
|
|
|
{
|
2002-06-27 22:42:11 +00:00
|
|
|
int i, j, done;
|
|
|
|
char *text;
|
|
|
|
|
|
|
|
context_pam2.finished = 0;
|
|
|
|
context_pam2.num_received = 0;
|
|
|
|
context_pam2.num_expected = 0;
|
|
|
|
context_pam2.prompts = xmalloc(sizeof(int) * num_msg);
|
|
|
|
context_pam2.responses = xmalloc(sizeof(struct pam_response) * num_msg);
|
|
|
|
memset(context_pam2.responses, 0, sizeof(struct pam_response) * num_msg);
|
|
|
|
|
|
|
|
text = NULL;
|
|
|
|
for (i = 0, context_pam2.num_expected = 0; i < num_msg; i++) {
|
|
|
|
int style = PAM_MSG_MEMBER(msg, i, msg_style);
|
|
|
|
switch (style) {
|
2002-03-21 12:18:27 +00:00
|
|
|
case PAM_PROMPT_ECHO_ON:
|
2002-06-27 22:42:11 +00:00
|
|
|
case PAM_PROMPT_ECHO_OFF:
|
|
|
|
context_pam2.num_expected++;
|
2002-03-21 12:18:27 +00:00
|
|
|
break;
|
|
|
|
case PAM_TEXT_INFO:
|
2002-06-27 22:42:11 +00:00
|
|
|
case PAM_ERROR_MSG:
|
2002-03-21 12:18:27 +00:00
|
|
|
default:
|
2002-06-27 22:42:11 +00:00
|
|
|
/* Capture all these messages to be sent at once */
|
|
|
|
message_cat(&text, PAM_MSG_MEMBER(msg, i, msg));
|
|
|
|
break;
|
2002-03-21 12:18:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-27 22:42:11 +00:00
|
|
|
if (context_pam2.num_expected == 0)
|
|
|
|
return PAM_SUCCESS;
|
|
|
|
|
|
|
|
packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST);
|
|
|
|
packet_put_cstring(""); /* Name */
|
|
|
|
packet_put_cstring(""); /* Instructions */
|
|
|
|
packet_put_cstring(""); /* Language */
|
|
|
|
packet_put_int(context_pam2.num_expected);
|
|
|
|
|
|
|
|
for (i = 0, j = 0; i < num_msg; i++) {
|
|
|
|
int style = PAM_MSG_MEMBER(msg, i, msg_style);
|
|
|
|
|
|
|
|
/* Skip messages which don't need a reply */
|
|
|
|
if (style != PAM_PROMPT_ECHO_ON && style != PAM_PROMPT_ECHO_OFF)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
context_pam2.prompts[j++] = i;
|
|
|
|
if (text) {
|
|
|
|
message_cat(&text, PAM_MSG_MEMBER(msg, i, msg));
|
|
|
|
packet_put_cstring(text);
|
|
|
|
text = NULL;
|
|
|
|
} else
|
|
|
|
packet_put_cstring(PAM_MSG_MEMBER(msg, i, msg));
|
|
|
|
packet_put_char(style == PAM_PROMPT_ECHO_ON);
|
2002-03-21 12:18:27 +00:00
|
|
|
}
|
2002-06-27 22:42:11 +00:00
|
|
|
packet_send();
|
|
|
|
packet_write_wait();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Grabbing control of execution and spinning until we get what
|
|
|
|
* we want is probably rude, but it seems to work properly, and
|
|
|
|
* the client *should* be in lock-step with us, so the loop should
|
|
|
|
* only be traversed once.
|
|
|
|
*/
|
|
|
|
while(context_pam2.finished == 0) {
|
|
|
|
done = 1;
|
|
|
|
dispatch_run(DISPATCH_BLOCK, &done, appdata_ptr);
|
2002-10-29 10:16:02 +00:00
|
|
|
if (context_pam2.finished == 0)
|
2002-06-27 22:42:11 +00:00
|
|
|
debug("extra packet during conversation");
|
2002-03-21 12:18:27 +00:00
|
|
|
}
|
2002-06-27 22:42:11 +00:00
|
|
|
|
2002-10-29 10:16:02 +00:00
|
|
|
if (context_pam2.num_received == context_pam2.num_expected) {
|
2002-06-27 22:42:11 +00:00
|
|
|
*resp = context_pam2.responses;
|
|
|
|
return PAM_SUCCESS;
|
|
|
|
} else
|
|
|
|
return PAM_CONV_ERR;
|
2002-03-21 12:18:27 +00:00
|
|
|
}
|
|
|
|
|
2002-06-27 22:42:11 +00:00
|
|
|
void
|
|
|
|
input_userauth_info_response_pam(int type, u_int32_t seqnr, void *ctxt)
|
2002-03-21 12:18:27 +00:00
|
|
|
{
|
2002-06-27 22:42:11 +00:00
|
|
|
Authctxt *authctxt = ctxt;
|
|
|
|
unsigned int nresp = 0, rlen = 0, i = 0;
|
|
|
|
char *resp;
|
2002-03-21 12:18:27 +00:00
|
|
|
|
2002-06-27 22:42:11 +00:00
|
|
|
if (authctxt == NULL)
|
|
|
|
fatal("input_userauth_info_response_pam: no authentication context");
|
2002-03-21 12:18:27 +00:00
|
|
|
|
2002-06-27 22:42:11 +00:00
|
|
|
nresp = packet_get_int(); /* Number of responses. */
|
|
|
|
debug("got %d responses", nresp);
|
2002-03-21 12:18:27 +00:00
|
|
|
|
2002-06-29 11:48:59 +00:00
|
|
|
|
|
|
|
if (nresp != context_pam2.num_expected)
|
|
|
|
fatal("%s: Received incorrect number of responses "
|
2002-10-29 10:16:02 +00:00
|
|
|
"(expected %d, received %u)", __func__,
|
|
|
|
context_pam2.num_expected, nresp);
|
2002-06-29 11:48:59 +00:00
|
|
|
|
|
|
|
if (nresp > 100)
|
|
|
|
fatal("%s: too many replies", __func__);
|
|
|
|
|
2002-06-27 22:42:11 +00:00
|
|
|
for (i = 0; i < nresp; i++) {
|
|
|
|
int j = context_pam2.prompts[i];
|
|
|
|
|
|
|
|
resp = packet_get_string(&rlen);
|
|
|
|
context_pam2.responses[j].resp_retcode = PAM_SUCCESS;
|
2003-04-23 17:13:13 +00:00
|
|
|
context_pam2.responses[j].resp = resp;
|
2002-06-27 22:42:11 +00:00
|
|
|
context_pam2.num_received++;
|
2002-03-21 12:18:27 +00:00
|
|
|
}
|
|
|
|
|
2002-06-27 22:42:11 +00:00
|
|
|
context_pam2.finished = 1;
|
2002-03-21 12:18:27 +00:00
|
|
|
|
2002-06-27 22:42:11 +00:00
|
|
|
packet_check_eom();
|
2002-03-21 12:18:27 +00:00
|
|
|
}
|
2002-06-27 22:42:11 +00:00
|
|
|
#endif
|