5 #include <security/pam_appl.h>
16 int finished, num_received, num_expected;
18 struct pam_response *responses;
19 } context_pam2 = {0, 0, 0, NULL};
21 static int do_conversation2(int num_msg, const struct pam_message **msg,
22 struct pam_response **resp, void *appdata_ptr);
24 static struct pam_conv
30 void input_userauth_info_response_pam(int type, int plen, void *ctxt);
33 auth2_pam(Authctxt *authctxt)
37 if (authctxt->user == NULL)
38 fatal("auth2_pam: internal error: no user");
40 conv2.appdata_ptr = authctxt;
43 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE,
44 &input_userauth_info_response_pam);
45 retval = (do_pam_authenticate(0) == PAM_SUCCESS);
46 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL);
52 do_conversation2(int num_msg, const struct pam_message **msg,
53 struct pam_response **resp, void *appdata_ptr)
55 int echo = 0, i = 0, j = 0, done = 0;
56 char *tmp = NULL, *text = NULL;
58 context_pam2.finished = 0;
59 context_pam2.num_received = 0;
60 context_pam2.num_expected = 0;
61 context_pam2.prompts = xmalloc(sizeof(int) * num_msg);
62 context_pam2.responses = xmalloc(sizeof(struct pam_response) * num_msg);
63 memset(context_pam2.responses, 0, sizeof(struct pam_response) * num_msg);
65 packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST);
66 packet_put_cstring(""); /* Name */
67 packet_put_cstring(""); /* Instructions */
68 packet_put_cstring(""); /* Language */
69 for (i = 0, j = 0; i < num_msg; i++) {
70 if((PAM_MSG_MEMBER(msg, i, msg_style) == PAM_PROMPT_ECHO_ON) ||
71 (PAM_MSG_MEMBER(msg, i, msg_style) == PAM_PROMPT_ECHO_OFF) ||
76 packet_put_int(j); /* Number of prompts. */
77 context_pam2.num_expected = j;
78 for (i = 0, j = 0; i < num_msg; i++) {
79 switch(PAM_MSG_MEMBER(msg, i, msg_style)) {
80 case PAM_PROMPT_ECHO_ON:
83 case PAM_PROMPT_ECHO_OFF:
91 tmp = xmalloc(strlen(text) + strlen(PAM_MSG_MEMBER(msg, i, msg)) + 2);
94 strcat(tmp, PAM_MSG_MEMBER(msg, i, msg));
99 text = xstrdup(PAM_MSG_MEMBER(msg, i, msg));
101 if((PAM_MSG_MEMBER(msg, i, msg_style) == PAM_PROMPT_ECHO_ON) ||
102 (PAM_MSG_MEMBER(msg, i, msg_style) == PAM_PROMPT_ECHO_OFF) ||
103 (i == num_msg - 1)) {
104 debug("sending prompt ssh-%d(pam-%d) = \"%s\"",
106 context_pam2.prompts[j++] = i;
107 packet_put_cstring(text);
108 packet_put_char(echo);
116 /* Grabbing control of execution and spinning until we get what
117 * we want is probably rude, but it seems to work properly, and
118 * the client *should* be in lock-step with us, so the loop should
119 * only be traversed once. */
120 while(context_pam2.finished == 0) {
122 dispatch_run(DISPATCH_BLOCK, &done, appdata_ptr);
123 if(context_pam2.finished == 0) {
124 debug("extra packet during conversation");
128 if(context_pam2.num_received == context_pam2.num_expected) {
129 *resp = context_pam2.responses;
137 input_userauth_info_response_pam(int type, int plen, void *ctxt)
139 Authctxt *authctxt = ctxt;
140 unsigned int nresp = 0, rlen = 0, i = 0;
143 if (authctxt == NULL)
144 fatal("input_userauth_info_response_pam: no authentication context");
146 if (authctxt->attempt++ >= AUTH_FAIL_MAX)
147 packet_disconnect("too many failed userauth_requests");
149 nresp = packet_get_int(); /* Number of responses. */
150 debug("got %d responses", nresp);
152 for (i = 0; i < nresp; i++) {
153 int j = context_pam2.prompts[i];
154 resp = packet_get_string(&rlen);
155 debug("response ssh-%d(pam-%d) = \"%s\"", i, j, resp);
156 context_pam2.responses[j].resp_retcode = PAM_SUCCESS;
157 context_pam2.responses[j].resp = xstrdup(resp);
159 context_pam2.num_received++;
162 context_pam2.finished = 1;