Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1 | /* $OpenBSD: auth2-chall.c,v 1.38 2013/05/17 00:13:13 djm Exp $ */ |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 2 | /* |
Ben Lindstrom | 92a2e38 | 2001-03-05 06:59:27 +0000 | [diff] [blame] | 3 | * Copyright (c) 2001 Markus Friedl. All rights reserved. |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 4 | * Copyright (c) 2001 Per Allansson. All rights reserved. |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 16 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 17 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 18 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 19 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 20 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 21 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 22 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 26 | |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 27 | #include "includes.h" |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 28 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 29 | #include <sys/types.h> |
| 30 | |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 31 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 32 | #include <stdio.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 33 | #include <string.h> |
| 34 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 35 | #include "xmalloc.h" |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 36 | #include "ssh2.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 37 | #include "key.h" |
| 38 | #include "hostfile.h" |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 39 | #include "auth.h" |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 40 | #include "buffer.h" |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 41 | #include "packet.h" |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 42 | #include "dispatch.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 43 | #include "log.h" |
Darren Tucker | 3c66080 | 2005-01-20 22:20:50 +1100 | [diff] [blame] | 44 | #include "servconf.h" |
| 45 | |
| 46 | /* import */ |
| 47 | extern ServerOptions options; |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 48 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 49 | static int auth2_challenge_start(Authctxt *); |
| 50 | static int send_userauth_info_request(Authctxt *); |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 51 | static void input_userauth_info_response(int, u_int32_t, void *); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 52 | |
| 53 | #ifdef BSD_AUTH |
| 54 | extern KbdintDevice bsdauth_device; |
| 55 | #else |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 56 | #ifdef USE_PAM |
| 57 | extern KbdintDevice sshpam_device; |
| 58 | #endif |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 59 | #ifdef SKEY |
| 60 | extern KbdintDevice skey_device; |
| 61 | #endif |
| 62 | #endif |
| 63 | |
| 64 | KbdintDevice *devices[] = { |
| 65 | #ifdef BSD_AUTH |
| 66 | &bsdauth_device, |
| 67 | #else |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 68 | #ifdef USE_PAM |
| 69 | &sshpam_device, |
| 70 | #endif |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 71 | #ifdef SKEY |
| 72 | &skey_device, |
| 73 | #endif |
| 74 | #endif |
| 75 | NULL |
| 76 | }; |
| 77 | |
| 78 | typedef struct KbdintAuthctxt KbdintAuthctxt; |
| 79 | struct KbdintAuthctxt |
| 80 | { |
| 81 | char *devices; |
| 82 | void *ctxt; |
| 83 | KbdintDevice *device; |
Damien Miller | fb7fd95 | 2002-06-26 23:58:39 +1000 | [diff] [blame] | 84 | u_int nreq; |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
Darren Tucker | 3c66080 | 2005-01-20 22:20:50 +1100 | [diff] [blame] | 87 | #ifdef USE_PAM |
| 88 | void |
| 89 | remove_kbdint_device(const char *devname) |
| 90 | { |
| 91 | int i, j; |
| 92 | |
| 93 | for (i = 0; devices[i] != NULL; i++) |
| 94 | if (strcmp(devices[i]->name, devname) == 0) { |
| 95 | for (j = i; devices[j] != NULL; j++) |
| 96 | devices[j] = devices[j+1]; |
| 97 | i--; |
| 98 | } |
| 99 | } |
| 100 | #endif |
| 101 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 102 | static KbdintAuthctxt * |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 103 | kbdint_alloc(const char *devs) |
| 104 | { |
| 105 | KbdintAuthctxt *kbdintctxt; |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 106 | Buffer b; |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 107 | int i; |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 108 | |
Darren Tucker | 3c66080 | 2005-01-20 22:20:50 +1100 | [diff] [blame] | 109 | #ifdef USE_PAM |
| 110 | if (!options.use_pam) |
| 111 | remove_kbdint_device("pam"); |
| 112 | #endif |
| 113 | |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 114 | kbdintctxt = xmalloc(sizeof(KbdintAuthctxt)); |
| 115 | if (strcmp(devs, "") == 0) { |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 116 | buffer_init(&b); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 117 | for (i = 0; devices[i]; i++) { |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 118 | if (buffer_len(&b) > 0) |
| 119 | buffer_append(&b, ",", 1); |
| 120 | buffer_append(&b, devices[i]->name, |
| 121 | strlen(devices[i]->name)); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 122 | } |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 123 | buffer_append(&b, "\0", 1); |
| 124 | kbdintctxt->devices = xstrdup(buffer_ptr(&b)); |
| 125 | buffer_free(&b); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 126 | } else { |
| 127 | kbdintctxt->devices = xstrdup(devs); |
| 128 | } |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 129 | debug("kbdint_alloc: devices '%s'", kbdintctxt->devices); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 130 | kbdintctxt->ctxt = NULL; |
| 131 | kbdintctxt->device = NULL; |
Damien Miller | fb7fd95 | 2002-06-26 23:58:39 +1000 | [diff] [blame] | 132 | kbdintctxt->nreq = 0; |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 133 | |
| 134 | return kbdintctxt; |
| 135 | } |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 136 | static void |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 137 | kbdint_reset_device(KbdintAuthctxt *kbdintctxt) |
| 138 | { |
| 139 | if (kbdintctxt->ctxt) { |
| 140 | kbdintctxt->device->free_ctx(kbdintctxt->ctxt); |
| 141 | kbdintctxt->ctxt = NULL; |
| 142 | } |
| 143 | kbdintctxt->device = NULL; |
| 144 | } |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 145 | static void |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 146 | kbdint_free(KbdintAuthctxt *kbdintctxt) |
| 147 | { |
| 148 | if (kbdintctxt->device) |
| 149 | kbdint_reset_device(kbdintctxt); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 150 | free(kbdintctxt->devices); |
| 151 | bzero(kbdintctxt, sizeof(*kbdintctxt)); |
| 152 | free(kbdintctxt); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 153 | } |
| 154 | /* get next device */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 155 | static int |
Damien Miller | 91a55f2 | 2013-04-23 15:18:10 +1000 | [diff] [blame] | 156 | kbdint_next_device(Authctxt *authctxt, KbdintAuthctxt *kbdintctxt) |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 157 | { |
| 158 | size_t len; |
| 159 | char *t; |
| 160 | int i; |
| 161 | |
| 162 | if (kbdintctxt->device) |
| 163 | kbdint_reset_device(kbdintctxt); |
| 164 | do { |
| 165 | len = kbdintctxt->devices ? |
| 166 | strcspn(kbdintctxt->devices, ",") : 0; |
| 167 | |
| 168 | if (len == 0) |
| 169 | break; |
Damien Miller | 91a55f2 | 2013-04-23 15:18:10 +1000 | [diff] [blame] | 170 | for (i = 0; devices[i]; i++) { |
| 171 | if (!auth2_method_allowed(authctxt, |
| 172 | "keyboard-interactive", devices[i]->name)) |
| 173 | continue; |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 174 | if (strncmp(kbdintctxt->devices, devices[i]->name, len) == 0) |
| 175 | kbdintctxt->device = devices[i]; |
Damien Miller | 91a55f2 | 2013-04-23 15:18:10 +1000 | [diff] [blame] | 176 | } |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 177 | t = kbdintctxt->devices; |
| 178 | kbdintctxt->devices = t[len] ? xstrdup(t+len+1) : NULL; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 179 | free(t); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 180 | debug2("kbdint_next_device: devices %s", kbdintctxt->devices ? |
Damien Miller | 0dc1bef | 2005-07-17 17:22:45 +1000 | [diff] [blame] | 181 | kbdintctxt->devices : "<empty>"); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 182 | } while (kbdintctxt->devices && !kbdintctxt->device); |
| 183 | |
| 184 | return kbdintctxt->device ? 1 : 0; |
| 185 | } |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 186 | |
| 187 | /* |
Ben Lindstrom | bdfb4df | 2001-10-03 17:12:43 +0000 | [diff] [blame] | 188 | * try challenge-response, set authctxt->postponed if we have to |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 189 | * wait for the response. |
| 190 | */ |
| 191 | int |
| 192 | auth2_challenge(Authctxt *authctxt, char *devs) |
| 193 | { |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 194 | debug("auth2_challenge: user=%s devs=%s", |
| 195 | authctxt->user ? authctxt->user : "<nouser>", |
| 196 | devs ? devs : "<no devs>"); |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 197 | |
Ben Lindstrom | 742e89e | 2001-06-09 01:17:23 +0000 | [diff] [blame] | 198 | if (authctxt->user == NULL || !devs) |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 199 | return 0; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 200 | if (authctxt->kbdintctxt == NULL) |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 201 | authctxt->kbdintctxt = kbdint_alloc(devs); |
| 202 | return auth2_challenge_start(authctxt); |
| 203 | } |
| 204 | |
Damien Miller | ee11625 | 2001-12-21 12:42:34 +1100 | [diff] [blame] | 205 | /* unregister kbd-int callbacks and context */ |
| 206 | void |
| 207 | auth2_challenge_stop(Authctxt *authctxt) |
| 208 | { |
| 209 | /* unregister callback */ |
| 210 | dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL); |
Damien Miller | 8016390 | 2007-01-05 16:30:16 +1100 | [diff] [blame] | 211 | if (authctxt->kbdintctxt != NULL) { |
Damien Miller | ee11625 | 2001-12-21 12:42:34 +1100 | [diff] [blame] | 212 | kbdint_free(authctxt->kbdintctxt); |
| 213 | authctxt->kbdintctxt = NULL; |
| 214 | } |
| 215 | } |
| 216 | |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 217 | /* side effect: sets authctxt->postponed if a reply was sent*/ |
| 218 | static int |
| 219 | auth2_challenge_start(Authctxt *authctxt) |
| 220 | { |
| 221 | KbdintAuthctxt *kbdintctxt = authctxt->kbdintctxt; |
| 222 | |
| 223 | debug2("auth2_challenge_start: devices %s", |
| 224 | kbdintctxt->devices ? kbdintctxt->devices : "<empty>"); |
| 225 | |
Damien Miller | 91a55f2 | 2013-04-23 15:18:10 +1000 | [diff] [blame] | 226 | if (kbdint_next_device(authctxt, kbdintctxt) == 0) { |
Damien Miller | ee11625 | 2001-12-21 12:42:34 +1100 | [diff] [blame] | 227 | auth2_challenge_stop(authctxt); |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 228 | return 0; |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 229 | } |
| 230 | debug("auth2_challenge_start: trying authentication method '%s'", |
| 231 | kbdintctxt->device->name); |
| 232 | |
| 233 | if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) { |
Damien Miller | ee11625 | 2001-12-21 12:42:34 +1100 | [diff] [blame] | 234 | auth2_challenge_stop(authctxt); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 235 | return 0; |
| 236 | } |
| 237 | if (send_userauth_info_request(authctxt) == 0) { |
Damien Miller | ee11625 | 2001-12-21 12:42:34 +1100 | [diff] [blame] | 238 | auth2_challenge_stop(authctxt); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 239 | return 0; |
| 240 | } |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 241 | dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, |
| 242 | &input_userauth_info_response); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 243 | |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 244 | authctxt->postponed = 1; |
| 245 | return 0; |
| 246 | } |
| 247 | |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 248 | static int |
| 249 | send_userauth_info_request(Authctxt *authctxt) |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 250 | { |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 251 | KbdintAuthctxt *kbdintctxt; |
| 252 | char *name, *instr, **prompts; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 253 | u_int i, *echo_on; |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 254 | |
| 255 | kbdintctxt = authctxt->kbdintctxt; |
| 256 | if (kbdintctxt->device->query(kbdintctxt->ctxt, |
Damien Miller | fb7fd95 | 2002-06-26 23:58:39 +1000 | [diff] [blame] | 257 | &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on)) |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 258 | return 0; |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 259 | |
| 260 | packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 261 | packet_put_cstring(name); |
| 262 | packet_put_cstring(instr); |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 263 | packet_put_cstring(""); /* language not used */ |
Damien Miller | fb7fd95 | 2002-06-26 23:58:39 +1000 | [diff] [blame] | 264 | packet_put_int(kbdintctxt->nreq); |
| 265 | for (i = 0; i < kbdintctxt->nreq; i++) { |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 266 | packet_put_cstring(prompts[i]); |
| 267 | packet_put_char(echo_on[i]); |
| 268 | } |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 269 | packet_send(); |
| 270 | packet_write_wait(); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 271 | |
Damien Miller | fb7fd95 | 2002-06-26 23:58:39 +1000 | [diff] [blame] | 272 | for (i = 0; i < kbdintctxt->nreq; i++) |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 273 | free(prompts[i]); |
| 274 | free(prompts); |
| 275 | free(echo_on); |
| 276 | free(name); |
| 277 | free(instr); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 278 | return 1; |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 281 | static void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 282 | input_userauth_info_response(int type, u_int32_t seq, void *ctxt) |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 283 | { |
| 284 | Authctxt *authctxt = ctxt; |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 285 | KbdintAuthctxt *kbdintctxt; |
Damien Miller | c30def9 | 2009-01-28 16:13:39 +1100 | [diff] [blame] | 286 | int authenticated = 0, res; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 287 | u_int i, nresp; |
Damien Miller | 55aca02 | 2012-12-03 11:25:30 +1100 | [diff] [blame] | 288 | const char *devicename = NULL; |
| 289 | char **response = NULL; |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 290 | |
| 291 | if (authctxt == NULL) |
| 292 | fatal("input_userauth_info_response: no authctxt"); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 293 | kbdintctxt = authctxt->kbdintctxt; |
| 294 | if (kbdintctxt == NULL || kbdintctxt->ctxt == NULL) |
| 295 | fatal("input_userauth_info_response: no kbdintctxt"); |
| 296 | if (kbdintctxt->device == NULL) |
| 297 | fatal("input_userauth_info_response: no device"); |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 298 | |
| 299 | authctxt->postponed = 0; /* reset */ |
| 300 | nresp = packet_get_int(); |
Damien Miller | fb7fd95 | 2002-06-26 23:58:39 +1000 | [diff] [blame] | 301 | if (nresp != kbdintctxt->nreq) |
| 302 | fatal("input_userauth_info_response: wrong number of replies"); |
| 303 | if (nresp > 100) |
| 304 | fatal("input_userauth_info_response: too many replies"); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 305 | if (nresp > 0) { |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 306 | response = xcalloc(nresp, sizeof(char *)); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 307 | for (i = 0; i < nresp; i++) |
| 308 | response[i] = packet_get_string(NULL); |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 309 | } |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 310 | packet_check_eom(); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 311 | |
Darren Tucker | 611649e | 2005-01-20 11:05:34 +1100 | [diff] [blame] | 312 | res = kbdintctxt->device->respond(kbdintctxt->ctxt, nresp, response); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 313 | |
| 314 | for (i = 0; i < nresp; i++) { |
| 315 | memset(response[i], 'r', strlen(response[i])); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 316 | free(response[i]); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 317 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 318 | free(response); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 319 | |
| 320 | switch (res) { |
| 321 | case 0: |
| 322 | /* Success! */ |
Darren Tucker | 611649e | 2005-01-20 11:05:34 +1100 | [diff] [blame] | 323 | authenticated = authctxt->valid ? 1 : 0; |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 324 | break; |
| 325 | case 1: |
| 326 | /* Authentication needs further interaction */ |
Damien Miller | ee11625 | 2001-12-21 12:42:34 +1100 | [diff] [blame] | 327 | if (send_userauth_info_request(authctxt) == 1) |
| 328 | authctxt->postponed = 1; |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 329 | break; |
| 330 | default: |
| 331 | /* Failure! */ |
| 332 | break; |
| 333 | } |
Damien Miller | 15b05cf | 2012-12-03 09:53:20 +1100 | [diff] [blame] | 334 | devicename = kbdintctxt->device->name; |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 335 | if (!authctxt->postponed) { |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 336 | if (authenticated) { |
Damien Miller | ee11625 | 2001-12-21 12:42:34 +1100 | [diff] [blame] | 337 | auth2_challenge_stop(authctxt); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 338 | } else { |
| 339 | /* start next device */ |
| 340 | /* may set authctxt->postponed */ |
| 341 | auth2_challenge_start(authctxt); |
| 342 | } |
| 343 | } |
Damien Miller | 15b05cf | 2012-12-03 09:53:20 +1100 | [diff] [blame] | 344 | userauth_finish(authctxt, authenticated, "keyboard-interactive", |
| 345 | devicename); |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 346 | } |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 347 | |
| 348 | void |
| 349 | privsep_challenge_enable(void) |
| 350 | { |
Damien Miller | a6a7c19 | 2003-05-26 21:36:13 +1000 | [diff] [blame] | 351 | #if defined(BSD_AUTH) || defined(USE_PAM) || defined(SKEY) |
| 352 | int n = 0; |
| 353 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 354 | #ifdef BSD_AUTH |
| 355 | extern KbdintDevice mm_bsdauth_device; |
| 356 | #endif |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 357 | #ifdef USE_PAM |
| 358 | extern KbdintDevice mm_sshpam_device; |
| 359 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 360 | #ifdef SKEY |
| 361 | extern KbdintDevice mm_skey_device; |
| 362 | #endif |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 363 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 364 | #ifdef BSD_AUTH |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 365 | devices[n++] = &mm_bsdauth_device; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 366 | #else |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 367 | #ifdef USE_PAM |
| 368 | devices[n++] = &mm_sshpam_device; |
| 369 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 370 | #ifdef SKEY |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 371 | devices[n++] = &mm_skey_device; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 372 | #endif |
| 373 | #endif |
| 374 | } |