Damien Miller | b848158 | 2000-12-03 11:51:51 +1100 | [diff] [blame] | 1 | #include "includes.h" |
Damien Miller | 22e22bf | 2001-01-19 15:46:38 +1100 | [diff] [blame^] | 2 | RCSID("$Id: auth2-pam.c,v 1.4 2001/01/19 04:46:38 djm Exp $"); |
Damien Miller | b848158 | 2000-12-03 11:51:51 +1100 | [diff] [blame] | 3 | |
| 4 | #ifdef USE_PAM |
| 5 | #include "ssh.h" |
| 6 | #include "ssh2.h" |
| 7 | #include "auth.h" |
| 8 | #include "packet.h" |
| 9 | #include "xmalloc.h" |
| 10 | #include "dispatch.h" |
| 11 | #include <security/pam_appl.h> |
| 12 | |
| 13 | struct { |
| 14 | int finished, num_received, num_expected; |
| 15 | int *prompts; |
| 16 | struct pam_response *responses; |
| 17 | } context_pam2 = {0, 0, 0, NULL}; |
| 18 | |
| 19 | static int do_conversation2(int num_msg, const struct pam_message **msg, |
| 20 | struct pam_response **resp, void *appdata_ptr); |
| 21 | |
| 22 | static struct pam_conv |
| 23 | conv2 = { |
| 24 | do_conversation2, |
| 25 | NULL, |
| 26 | }; |
| 27 | |
| 28 | void input_userauth_info_response_pam(int type, int plen, void *ctxt); |
| 29 | |
| 30 | int |
| 31 | auth2_pam(Authctxt *authctxt) |
| 32 | { |
| 33 | int retval = -1; |
| 34 | char *method = "PAM"; |
| 35 | |
| 36 | if (authctxt->user == NULL) |
| 37 | fatal("auth2_pam: internal error: no user"); |
| 38 | |
Damien Miller | 22e22bf | 2001-01-19 15:46:38 +1100 | [diff] [blame^] | 39 | conv2.appdata_ptr = authctxt; |
| 40 | pam_set_conv(&conv2); |
Damien Miller | b848158 | 2000-12-03 11:51:51 +1100 | [diff] [blame] | 41 | |
| 42 | dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, |
| 43 | &input_userauth_info_response_pam); |
| 44 | retval = (do_pam_authenticate(0) == PAM_SUCCESS); |
| 45 | dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL); |
| 46 | |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 47 | #if 0 /* ISSUE: No longer valid, but should this still be |
| 48 | handled?? */ |
Damien Miller | b848158 | 2000-12-03 11:51:51 +1100 | [diff] [blame] | 49 | userauth_log(authctxt, retval, method); |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 50 | #endif |
Damien Miller | b848158 | 2000-12-03 11:51:51 +1100 | [diff] [blame] | 51 | return retval; |
| 52 | } |
| 53 | |
| 54 | static int |
| 55 | do_conversation2(int num_msg, const struct pam_message **msg, |
| 56 | struct pam_response **resp, void *appdata_ptr) |
| 57 | { |
| 58 | int echo = 0, i = 0, j = 0, done = 0; |
| 59 | char *tmp = NULL, *text = NULL; |
| 60 | |
| 61 | context_pam2.finished = 0; |
| 62 | context_pam2.num_received = 0; |
| 63 | context_pam2.num_expected = 0; |
| 64 | context_pam2.prompts = xmalloc(sizeof(int) * num_msg); |
| 65 | context_pam2.responses = xmalloc(sizeof(struct pam_response) * num_msg); |
| 66 | memset(context_pam2.responses, 0, sizeof(struct pam_response) * num_msg); |
| 67 | |
| 68 | packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST); |
| 69 | packet_put_cstring(""); /* Name */ |
| 70 | packet_put_cstring(""); /* Instructions */ |
| 71 | packet_put_cstring(""); /* Language */ |
| 72 | for (i = 0, j = 0; i < num_msg; i++) { |
Damien Miller | 82cf0ce | 2000-12-20 13:34:48 +1100 | [diff] [blame] | 73 | if((PAM_MSG_MEMBER(msg, i, msg_style) == PAM_PROMPT_ECHO_ON) || |
| 74 | (PAM_MSG_MEMBER(msg, i, msg_style) == PAM_PROMPT_ECHO_OFF) || |
Damien Miller | b848158 | 2000-12-03 11:51:51 +1100 | [diff] [blame] | 75 | (i == num_msg - 1)) { |
| 76 | j++; |
| 77 | } |
| 78 | } |
| 79 | packet_put_int(j); /* Number of prompts. */ |
| 80 | context_pam2.num_expected = j; |
| 81 | for (i = 0, j = 0; i < num_msg; i++) { |
Damien Miller | 82cf0ce | 2000-12-20 13:34:48 +1100 | [diff] [blame] | 82 | switch(PAM_MSG_MEMBER(msg, i, msg_style)) { |
Damien Miller | b848158 | 2000-12-03 11:51:51 +1100 | [diff] [blame] | 83 | case PAM_PROMPT_ECHO_ON: |
| 84 | echo = 1; |
| 85 | break; |
| 86 | case PAM_PROMPT_ECHO_OFF: |
| 87 | echo = 0; |
| 88 | break; |
| 89 | default: |
| 90 | echo = 0; |
| 91 | break; |
| 92 | } |
| 93 | if(text) { |
Damien Miller | 82cf0ce | 2000-12-20 13:34:48 +1100 | [diff] [blame] | 94 | tmp = xmalloc(strlen(text) + strlen(PAM_MSG_MEMBER(msg, i, msg)) + 2); |
Damien Miller | b848158 | 2000-12-03 11:51:51 +1100 | [diff] [blame] | 95 | strcpy(tmp, text); |
| 96 | strcat(tmp, "\n"); |
Damien Miller | 82cf0ce | 2000-12-20 13:34:48 +1100 | [diff] [blame] | 97 | strcat(tmp, PAM_MSG_MEMBER(msg, i, msg)); |
Damien Miller | b848158 | 2000-12-03 11:51:51 +1100 | [diff] [blame] | 98 | xfree(text); |
| 99 | text = tmp; |
| 100 | tmp = NULL; |
| 101 | } else { |
Damien Miller | 82cf0ce | 2000-12-20 13:34:48 +1100 | [diff] [blame] | 102 | text = xstrdup(PAM_MSG_MEMBER(msg, i, msg)); |
Damien Miller | b848158 | 2000-12-03 11:51:51 +1100 | [diff] [blame] | 103 | } |
Damien Miller | 82cf0ce | 2000-12-20 13:34:48 +1100 | [diff] [blame] | 104 | if((PAM_MSG_MEMBER(msg, i, msg_style) == PAM_PROMPT_ECHO_ON) || |
| 105 | (PAM_MSG_MEMBER(msg, i, msg_style) == PAM_PROMPT_ECHO_OFF) || |
Damien Miller | b848158 | 2000-12-03 11:51:51 +1100 | [diff] [blame] | 106 | (i == num_msg - 1)) { |
| 107 | debug("sending prompt ssh-%d(pam-%d) = \"%s\"", |
| 108 | j, i, text); |
| 109 | context_pam2.prompts[j++] = i; |
| 110 | packet_put_cstring(text); |
| 111 | packet_put_char(echo); |
| 112 | xfree(text); |
| 113 | text = NULL; |
| 114 | } |
| 115 | } |
| 116 | packet_send(); |
| 117 | packet_write_wait(); |
| 118 | |
| 119 | /* Grabbing control of execution and spinning until we get what |
| 120 | * we want is probably rude, but it seems to work properly, and |
| 121 | * the client *should* be in lock-step with us, so the loop should |
| 122 | * only be traversed once. */ |
| 123 | while(context_pam2.finished == 0) { |
| 124 | done = 1; |
| 125 | dispatch_run(DISPATCH_BLOCK, &done, appdata_ptr); |
| 126 | if(context_pam2.finished == 0) { |
| 127 | debug("extra packet during conversation"); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if(context_pam2.num_received == context_pam2.num_expected) { |
| 132 | *resp = context_pam2.responses; |
| 133 | return PAM_SUCCESS; |
| 134 | } else { |
| 135 | return PAM_CONV_ERR; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | void |
| 140 | input_userauth_info_response_pam(int type, int plen, void *ctxt) |
| 141 | { |
| 142 | Authctxt *authctxt = ctxt; |
| 143 | unsigned int nresp = 0, rlen = 0, i = 0; |
| 144 | char *resp; |
| 145 | |
| 146 | if (authctxt == NULL) |
| 147 | fatal("input_userauth_info_response_pam: no authentication context"); |
| 148 | |
| 149 | if (authctxt->attempt++ >= AUTH_FAIL_MAX) |
| 150 | packet_disconnect("too many failed userauth_requests"); |
| 151 | |
| 152 | nresp = packet_get_int(); /* Number of responses. */ |
| 153 | debug("got %d responses", nresp); |
| 154 | |
| 155 | for (i = 0; i < nresp; i++) { |
| 156 | int j = context_pam2.prompts[i]; |
| 157 | resp = packet_get_string(&rlen); |
| 158 | debug("response ssh-%d(pam-%d) = \"%s\"", i, j, resp); |
| 159 | context_pam2.responses[j].resp_retcode = PAM_SUCCESS; |
| 160 | context_pam2.responses[j].resp = xstrdup(resp); |
| 161 | xfree(resp); |
| 162 | context_pam2.num_received++; |
| 163 | } |
| 164 | |
| 165 | context_pam2.finished = 1; |
| 166 | |
| 167 | packet_done(); |
| 168 | } |
| 169 | |
| 170 | #endif |