11 #include <security/pam_appl.h>
14 int finished, num_received, num_expected;
16 struct pam_response *responses;
17 } context_pam2 = {0, 0, 0, NULL};
19 static int do_conversation2(int num_msg, const struct pam_message **msg,
20 struct pam_response **resp, void *appdata_ptr);
22 static struct pam_conv
28 void input_userauth_info_response_pam(int type, int plen, void *ctxt);
31 auth2_pam(Authctxt *authctxt)
36 if (authctxt->user == NULL)
37 fatal("auth2_pam: internal error: no user");
39 if (authctxt->valid) {
40 conv2.appdata_ptr = authctxt;
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);
49 #if 0 /* ISSUE: No longer valid, but should this still be
51 userauth_log(authctxt, retval, method);
57 do_conversation2(int num_msg, const struct pam_message **msg,
58 struct pam_response **resp, void *appdata_ptr)
60 int echo = 0, i = 0, j = 0, done = 0;
61 char *tmp = NULL, *text = NULL;
63 context_pam2.finished = 0;
64 context_pam2.num_received = 0;
65 context_pam2.num_expected = 0;
66 context_pam2.prompts = xmalloc(sizeof(int) * num_msg);
67 context_pam2.responses = xmalloc(sizeof(struct pam_response) * num_msg);
68 memset(context_pam2.responses, 0, sizeof(struct pam_response) * num_msg);
70 packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST);
71 packet_put_cstring(""); /* Name */
72 packet_put_cstring(""); /* Instructions */
73 packet_put_cstring(""); /* Language */
74 for (i = 0, j = 0; i < num_msg; i++) {
75 if((PAM_MSG_MEMBER(msg, i, msg_style) == PAM_PROMPT_ECHO_ON) ||
76 (PAM_MSG_MEMBER(msg, i, msg_style) == PAM_PROMPT_ECHO_OFF) ||
81 packet_put_int(j); /* Number of prompts. */
82 context_pam2.num_expected = j;
83 for (i = 0, j = 0; i < num_msg; i++) {
84 switch(PAM_MSG_MEMBER(msg, i, msg_style)) {
85 case PAM_PROMPT_ECHO_ON:
88 case PAM_PROMPT_ECHO_OFF:
96 tmp = xmalloc(strlen(text) + strlen(PAM_MSG_MEMBER(msg, i, msg)) + 2);
99 strcat(tmp, PAM_MSG_MEMBER(msg, i, msg));
104 text = xstrdup(PAM_MSG_MEMBER(msg, i, msg));
106 if((PAM_MSG_MEMBER(msg, i, msg_style) == PAM_PROMPT_ECHO_ON) ||
107 (PAM_MSG_MEMBER(msg, i, msg_style) == PAM_PROMPT_ECHO_OFF) ||
108 (i == num_msg - 1)) {
109 debug("sending prompt ssh-%d(pam-%d) = \"%s\"",
111 context_pam2.prompts[j++] = i;
112 packet_put_cstring(text);
113 packet_put_char(echo);
121 /* Grabbing control of execution and spinning until we get what
122 * we want is probably rude, but it seems to work properly, and
123 * the client *should* be in lock-step with us, so the loop should
124 * only be traversed once. */
125 while(context_pam2.finished == 0) {
127 dispatch_run(DISPATCH_BLOCK, &done, appdata_ptr);
128 if(context_pam2.finished == 0) {
129 debug("extra packet during conversation");
133 if(context_pam2.num_received == context_pam2.num_expected) {
134 *resp = context_pam2.responses;
142 input_userauth_info_response_pam(int type, int plen, void *ctxt)
144 Authctxt *authctxt = ctxt;
145 unsigned int nresp = 0, rlen = 0, i = 0;
148 if (authctxt == NULL)
149 fatal("input_userauth_info_response_pam: no authentication context");
151 if (authctxt->attempt++ >= AUTH_FAIL_MAX)
152 packet_disconnect("too many failed userauth_requests");
154 nresp = packet_get_int(); /* Number of responses. */
155 debug("got %d responses", nresp);
157 for (i = 0; i < nresp; i++) {
158 int j = context_pam2.prompts[i];
159 resp = packet_get_string(&rlen);
160 debug("response ssh-%d(pam-%d) = \"%s\"", i, j, resp);
161 context_pam2.responses[j].resp_retcode = PAM_SUCCESS;
162 context_pam2.responses[j].resp = xstrdup(resp);
164 context_pam2.num_received++;
167 context_pam2.finished = 1;