2 RCSID("$Id: auth2-pam.c,v 1.14 2002/06/28 16:48:12 mouring Exp $");
3 RCSID("$FreeBSD: src/crypto/openssh/auth2-pam.c,v 1.4.2.2 2003/02/03 17:31:06 des Exp $");
6 #include <security/pam_appl.h>
17 static int do_pam_conversation_kbd_int(int num_msg,
18 const struct pam_message **msg, struct pam_response **resp,
20 void input_userauth_info_response_pam(int type, u_int32_t seqnr, void *ctxt);
23 int finished, num_received, num_expected;
25 struct pam_response *responses;
26 } context_pam2 = {0, 0, 0, NULL};
28 static struct pam_conv conv2 = {
29 do_pam_conversation_kbd_int,
34 auth2_pam(Authctxt *authctxt)
38 if (authctxt->user == NULL)
39 fatal("auth2_pam: internal error: no user");
41 conv2.appdata_ptr = authctxt;
42 do_pam_set_conv(&conv2);
44 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE,
45 &input_userauth_info_response_pam);
46 retval = (do_pam_authenticate(0) == PAM_SUCCESS);
47 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL);
53 do_pam_conversation_kbd_int(int num_msg, const struct pam_message **msg,
54 struct pam_response **resp, void *appdata_ptr)
59 context_pam2.finished = 0;
60 context_pam2.num_received = 0;
61 context_pam2.num_expected = 0;
62 context_pam2.prompts = xmalloc(sizeof(int) * num_msg);
63 context_pam2.responses = xmalloc(sizeof(struct pam_response) * num_msg);
64 memset(context_pam2.responses, 0, sizeof(struct pam_response) * num_msg);
67 for (i = 0, context_pam2.num_expected = 0; i < num_msg; i++) {
68 int style = PAM_MSG_MEMBER(msg, i, msg_style);
70 case PAM_PROMPT_ECHO_ON:
71 case PAM_PROMPT_ECHO_OFF:
72 context_pam2.num_expected++;
77 /* Capture all these messages to be sent at once */
78 message_cat(&text, PAM_MSG_MEMBER(msg, i, msg));
83 if (context_pam2.num_expected == 0)
86 packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST);
87 packet_put_cstring(""); /* Name */
88 packet_put_cstring(""); /* Instructions */
89 packet_put_cstring(""); /* Language */
90 packet_put_int(context_pam2.num_expected);
92 for (i = 0, j = 0; i < num_msg; i++) {
93 int style = PAM_MSG_MEMBER(msg, i, msg_style);
95 /* Skip messages which don't need a reply */
96 if (style != PAM_PROMPT_ECHO_ON && style != PAM_PROMPT_ECHO_OFF)
99 context_pam2.prompts[j++] = i;
101 message_cat(&text, PAM_MSG_MEMBER(msg, i, msg));
102 packet_put_cstring(text);
105 packet_put_cstring(PAM_MSG_MEMBER(msg, i, msg));
106 packet_put_char(style == PAM_PROMPT_ECHO_ON);
112 * Grabbing control of execution and spinning until we get what
113 * we want is probably rude, but it seems to work properly, and
114 * the client *should* be in lock-step with us, so the loop should
115 * only be traversed once.
117 while(context_pam2.finished == 0) {
119 dispatch_run(DISPATCH_BLOCK, &done, appdata_ptr);
120 if (context_pam2.finished == 0)
121 debug("extra packet during conversation");
124 if (context_pam2.num_received == context_pam2.num_expected) {
125 *resp = context_pam2.responses;
132 input_userauth_info_response_pam(int type, u_int32_t seqnr, void *ctxt)
134 Authctxt *authctxt = ctxt;
135 unsigned int nresp = 0, rlen = 0, i = 0;
138 if (authctxt == NULL)
139 fatal("input_userauth_info_response_pam: no authentication context");
141 nresp = packet_get_int(); /* Number of responses. */
142 debug("got %d responses", nresp);
145 if (nresp != context_pam2.num_expected)
146 fatal("%s: Received incorrect number of responses "
147 "(expected %d, received %u)", __func__,
148 context_pam2.num_expected, nresp);
151 fatal("%s: too many replies", __func__);
153 for (i = 0; i < nresp; i++) {
154 int j = context_pam2.prompts[i];
156 resp = packet_get_string(&rlen);
157 context_pam2.responses[j].resp_retcode = PAM_SUCCESS;
158 context_pam2.responses[j].resp = xstrdup(resp);
160 context_pam2.num_received++;
163 context_pam2.finished = 1;