Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 1 | /* $OpenBSD: auth2-chall.c,v 1.41 2014/02/02 03:44:31 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" |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 44 | #include "misc.h" |
Darren Tucker | 3c66080 | 2005-01-20 22:20:50 +1100 | [diff] [blame] | 45 | #include "servconf.h" |
| 46 | |
| 47 | /* import */ |
| 48 | extern ServerOptions options; |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 49 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 50 | static int auth2_challenge_start(Authctxt *); |
| 51 | static int send_userauth_info_request(Authctxt *); |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 52 | static void input_userauth_info_response(int, u_int32_t, void *); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 53 | |
| 54 | #ifdef BSD_AUTH |
| 55 | extern KbdintDevice bsdauth_device; |
| 56 | #else |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 57 | #ifdef USE_PAM |
| 58 | extern KbdintDevice sshpam_device; |
| 59 | #endif |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 60 | #ifdef SKEY |
| 61 | extern KbdintDevice skey_device; |
| 62 | #endif |
| 63 | #endif |
| 64 | |
| 65 | KbdintDevice *devices[] = { |
| 66 | #ifdef BSD_AUTH |
| 67 | &bsdauth_device, |
| 68 | #else |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 69 | #ifdef USE_PAM |
| 70 | &sshpam_device, |
| 71 | #endif |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 72 | #ifdef SKEY |
| 73 | &skey_device, |
| 74 | #endif |
| 75 | #endif |
| 76 | NULL |
| 77 | }; |
| 78 | |
| 79 | typedef struct KbdintAuthctxt KbdintAuthctxt; |
| 80 | struct KbdintAuthctxt |
| 81 | { |
| 82 | char *devices; |
| 83 | void *ctxt; |
| 84 | KbdintDevice *device; |
Damien Miller | fb7fd95 | 2002-06-26 23:58:39 +1000 | [diff] [blame] | 85 | u_int nreq; |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
Darren Tucker | 3c66080 | 2005-01-20 22:20:50 +1100 | [diff] [blame] | 88 | #ifdef USE_PAM |
| 89 | void |
| 90 | remove_kbdint_device(const char *devname) |
| 91 | { |
| 92 | int i, j; |
| 93 | |
| 94 | for (i = 0; devices[i] != NULL; i++) |
| 95 | if (strcmp(devices[i]->name, devname) == 0) { |
| 96 | for (j = i; devices[j] != NULL; j++) |
| 97 | devices[j] = devices[j+1]; |
| 98 | i--; |
| 99 | } |
| 100 | } |
| 101 | #endif |
| 102 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 103 | static KbdintAuthctxt * |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 104 | kbdint_alloc(const char *devs) |
| 105 | { |
| 106 | KbdintAuthctxt *kbdintctxt; |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 107 | Buffer b; |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 108 | int i; |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 109 | |
Darren Tucker | 3c66080 | 2005-01-20 22:20:50 +1100 | [diff] [blame] | 110 | #ifdef USE_PAM |
| 111 | if (!options.use_pam) |
| 112 | remove_kbdint_device("pam"); |
| 113 | #endif |
| 114 | |
Damien Miller | 6c81fee | 2013-11-08 12:19:55 +1100 | [diff] [blame] | 115 | kbdintctxt = xcalloc(1, sizeof(KbdintAuthctxt)); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 116 | if (strcmp(devs, "") == 0) { |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 117 | buffer_init(&b); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 118 | for (i = 0; devices[i]; i++) { |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 119 | if (buffer_len(&b) > 0) |
| 120 | buffer_append(&b, ",", 1); |
| 121 | buffer_append(&b, devices[i]->name, |
| 122 | strlen(devices[i]->name)); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 123 | } |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 124 | buffer_append(&b, "\0", 1); |
| 125 | kbdintctxt->devices = xstrdup(buffer_ptr(&b)); |
| 126 | buffer_free(&b); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 127 | } else { |
| 128 | kbdintctxt->devices = xstrdup(devs); |
| 129 | } |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 130 | debug("kbdint_alloc: devices '%s'", kbdintctxt->devices); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 131 | kbdintctxt->ctxt = NULL; |
| 132 | kbdintctxt->device = NULL; |
Damien Miller | fb7fd95 | 2002-06-26 23:58:39 +1000 | [diff] [blame] | 133 | kbdintctxt->nreq = 0; |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 134 | |
| 135 | return kbdintctxt; |
| 136 | } |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 137 | static void |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 138 | kbdint_reset_device(KbdintAuthctxt *kbdintctxt) |
| 139 | { |
| 140 | if (kbdintctxt->ctxt) { |
| 141 | kbdintctxt->device->free_ctx(kbdintctxt->ctxt); |
| 142 | kbdintctxt->ctxt = NULL; |
| 143 | } |
| 144 | kbdintctxt->device = NULL; |
| 145 | } |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 146 | static void |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 147 | kbdint_free(KbdintAuthctxt *kbdintctxt) |
| 148 | { |
| 149 | if (kbdintctxt->device) |
| 150 | kbdint_reset_device(kbdintctxt); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 151 | free(kbdintctxt->devices); |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 152 | explicit_bzero(kbdintctxt, sizeof(*kbdintctxt)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 153 | free(kbdintctxt); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 154 | } |
| 155 | /* get next device */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 156 | static int |
Damien Miller | 91a55f2 | 2013-04-23 15:18:10 +1000 | [diff] [blame] | 157 | kbdint_next_device(Authctxt *authctxt, KbdintAuthctxt *kbdintctxt) |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 158 | { |
| 159 | size_t len; |
| 160 | char *t; |
| 161 | int i; |
| 162 | |
| 163 | if (kbdintctxt->device) |
| 164 | kbdint_reset_device(kbdintctxt); |
| 165 | do { |
| 166 | len = kbdintctxt->devices ? |
| 167 | strcspn(kbdintctxt->devices, ",") : 0; |
| 168 | |
| 169 | if (len == 0) |
| 170 | break; |
Damien Miller | 91a55f2 | 2013-04-23 15:18:10 +1000 | [diff] [blame] | 171 | for (i = 0; devices[i]; i++) { |
| 172 | if (!auth2_method_allowed(authctxt, |
| 173 | "keyboard-interactive", devices[i]->name)) |
| 174 | continue; |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 175 | if (strncmp(kbdintctxt->devices, devices[i]->name, len) == 0) |
| 176 | kbdintctxt->device = devices[i]; |
Damien Miller | 91a55f2 | 2013-04-23 15:18:10 +1000 | [diff] [blame] | 177 | } |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 178 | t = kbdintctxt->devices; |
| 179 | kbdintctxt->devices = t[len] ? xstrdup(t+len+1) : NULL; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 180 | free(t); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 181 | debug2("kbdint_next_device: devices %s", kbdintctxt->devices ? |
Damien Miller | 0dc1bef | 2005-07-17 17:22:45 +1000 | [diff] [blame] | 182 | kbdintctxt->devices : "<empty>"); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 183 | } while (kbdintctxt->devices && !kbdintctxt->device); |
| 184 | |
| 185 | return kbdintctxt->device ? 1 : 0; |
| 186 | } |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 187 | |
| 188 | /* |
Ben Lindstrom | bdfb4df | 2001-10-03 17:12:43 +0000 | [diff] [blame] | 189 | * try challenge-response, set authctxt->postponed if we have to |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 190 | * wait for the response. |
| 191 | */ |
| 192 | int |
| 193 | auth2_challenge(Authctxt *authctxt, char *devs) |
| 194 | { |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 195 | debug("auth2_challenge: user=%s devs=%s", |
| 196 | authctxt->user ? authctxt->user : "<nouser>", |
| 197 | devs ? devs : "<no devs>"); |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 198 | |
Ben Lindstrom | 742e89e | 2001-06-09 01:17:23 +0000 | [diff] [blame] | 199 | if (authctxt->user == NULL || !devs) |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 200 | return 0; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 201 | if (authctxt->kbdintctxt == NULL) |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 202 | authctxt->kbdintctxt = kbdint_alloc(devs); |
| 203 | return auth2_challenge_start(authctxt); |
| 204 | } |
| 205 | |
Damien Miller | ee11625 | 2001-12-21 12:42:34 +1100 | [diff] [blame] | 206 | /* unregister kbd-int callbacks and context */ |
| 207 | void |
| 208 | auth2_challenge_stop(Authctxt *authctxt) |
| 209 | { |
| 210 | /* unregister callback */ |
| 211 | dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL); |
Damien Miller | 8016390 | 2007-01-05 16:30:16 +1100 | [diff] [blame] | 212 | if (authctxt->kbdintctxt != NULL) { |
Damien Miller | ee11625 | 2001-12-21 12:42:34 +1100 | [diff] [blame] | 213 | kbdint_free(authctxt->kbdintctxt); |
| 214 | authctxt->kbdintctxt = NULL; |
| 215 | } |
| 216 | } |
| 217 | |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 218 | /* side effect: sets authctxt->postponed if a reply was sent*/ |
| 219 | static int |
| 220 | auth2_challenge_start(Authctxt *authctxt) |
| 221 | { |
| 222 | KbdintAuthctxt *kbdintctxt = authctxt->kbdintctxt; |
| 223 | |
| 224 | debug2("auth2_challenge_start: devices %s", |
| 225 | kbdintctxt->devices ? kbdintctxt->devices : "<empty>"); |
| 226 | |
Damien Miller | 91a55f2 | 2013-04-23 15:18:10 +1000 | [diff] [blame] | 227 | if (kbdint_next_device(authctxt, kbdintctxt) == 0) { |
Damien Miller | ee11625 | 2001-12-21 12:42:34 +1100 | [diff] [blame] | 228 | auth2_challenge_stop(authctxt); |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 229 | return 0; |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 230 | } |
| 231 | debug("auth2_challenge_start: trying authentication method '%s'", |
| 232 | kbdintctxt->device->name); |
| 233 | |
| 234 | if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) { |
Damien Miller | ee11625 | 2001-12-21 12:42:34 +1100 | [diff] [blame] | 235 | auth2_challenge_stop(authctxt); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 236 | return 0; |
| 237 | } |
| 238 | if (send_userauth_info_request(authctxt) == 0) { |
Damien Miller | ee11625 | 2001-12-21 12:42:34 +1100 | [diff] [blame] | 239 | auth2_challenge_stop(authctxt); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 240 | return 0; |
| 241 | } |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 242 | dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, |
| 243 | &input_userauth_info_response); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 244 | |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 245 | authctxt->postponed = 1; |
| 246 | return 0; |
| 247 | } |
| 248 | |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 249 | static int |
| 250 | send_userauth_info_request(Authctxt *authctxt) |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 251 | { |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 252 | KbdintAuthctxt *kbdintctxt; |
| 253 | char *name, *instr, **prompts; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 254 | u_int i, *echo_on; |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 255 | |
| 256 | kbdintctxt = authctxt->kbdintctxt; |
| 257 | if (kbdintctxt->device->query(kbdintctxt->ctxt, |
Damien Miller | fb7fd95 | 2002-06-26 23:58:39 +1000 | [diff] [blame] | 258 | &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on)) |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 259 | return 0; |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 260 | |
| 261 | packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 262 | packet_put_cstring(name); |
| 263 | packet_put_cstring(instr); |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 264 | packet_put_cstring(""); /* language not used */ |
Damien Miller | fb7fd95 | 2002-06-26 23:58:39 +1000 | [diff] [blame] | 265 | packet_put_int(kbdintctxt->nreq); |
| 266 | for (i = 0; i < kbdintctxt->nreq; i++) { |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 267 | packet_put_cstring(prompts[i]); |
| 268 | packet_put_char(echo_on[i]); |
| 269 | } |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 270 | packet_send(); |
| 271 | packet_write_wait(); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 272 | |
Damien Miller | fb7fd95 | 2002-06-26 23:58:39 +1000 | [diff] [blame] | 273 | for (i = 0; i < kbdintctxt->nreq; i++) |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 274 | free(prompts[i]); |
| 275 | free(prompts); |
| 276 | free(echo_on); |
| 277 | free(name); |
| 278 | free(instr); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 279 | return 1; |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 282 | static void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 283 | input_userauth_info_response(int type, u_int32_t seq, void *ctxt) |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 284 | { |
| 285 | Authctxt *authctxt = ctxt; |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 286 | KbdintAuthctxt *kbdintctxt; |
Damien Miller | c30def9 | 2009-01-28 16:13:39 +1100 | [diff] [blame] | 287 | int authenticated = 0, res; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 288 | u_int i, nresp; |
Damien Miller | 55aca02 | 2012-12-03 11:25:30 +1100 | [diff] [blame] | 289 | const char *devicename = NULL; |
| 290 | char **response = NULL; |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 291 | |
| 292 | if (authctxt == NULL) |
| 293 | fatal("input_userauth_info_response: no authctxt"); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 294 | kbdintctxt = authctxt->kbdintctxt; |
| 295 | if (kbdintctxt == NULL || kbdintctxt->ctxt == NULL) |
| 296 | fatal("input_userauth_info_response: no kbdintctxt"); |
| 297 | if (kbdintctxt->device == NULL) |
| 298 | fatal("input_userauth_info_response: no device"); |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 299 | |
| 300 | authctxt->postponed = 0; /* reset */ |
| 301 | nresp = packet_get_int(); |
Damien Miller | fb7fd95 | 2002-06-26 23:58:39 +1000 | [diff] [blame] | 302 | if (nresp != kbdintctxt->nreq) |
| 303 | fatal("input_userauth_info_response: wrong number of replies"); |
| 304 | if (nresp > 100) |
| 305 | fatal("input_userauth_info_response: too many replies"); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 306 | if (nresp > 0) { |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 307 | response = xcalloc(nresp, sizeof(char *)); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 308 | for (i = 0; i < nresp; i++) |
| 309 | response[i] = packet_get_string(NULL); |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 310 | } |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 311 | packet_check_eom(); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 312 | |
Darren Tucker | 611649e | 2005-01-20 11:05:34 +1100 | [diff] [blame] | 313 | res = kbdintctxt->device->respond(kbdintctxt->ctxt, nresp, response); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 314 | |
| 315 | for (i = 0; i < nresp; i++) { |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 316 | explicit_bzero(response[i], strlen(response[i])); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 317 | free(response[i]); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 318 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 319 | free(response); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 320 | |
| 321 | switch (res) { |
| 322 | case 0: |
| 323 | /* Success! */ |
Darren Tucker | 611649e | 2005-01-20 11:05:34 +1100 | [diff] [blame] | 324 | authenticated = authctxt->valid ? 1 : 0; |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 325 | break; |
| 326 | case 1: |
| 327 | /* Authentication needs further interaction */ |
Damien Miller | ee11625 | 2001-12-21 12:42:34 +1100 | [diff] [blame] | 328 | if (send_userauth_info_request(authctxt) == 1) |
| 329 | authctxt->postponed = 1; |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 330 | break; |
| 331 | default: |
| 332 | /* Failure! */ |
| 333 | break; |
| 334 | } |
Damien Miller | 15b05cf | 2012-12-03 09:53:20 +1100 | [diff] [blame] | 335 | devicename = kbdintctxt->device->name; |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 336 | if (!authctxt->postponed) { |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 337 | if (authenticated) { |
Damien Miller | ee11625 | 2001-12-21 12:42:34 +1100 | [diff] [blame] | 338 | auth2_challenge_stop(authctxt); |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 339 | } else { |
| 340 | /* start next device */ |
| 341 | /* may set authctxt->postponed */ |
| 342 | auth2_challenge_start(authctxt); |
| 343 | } |
| 344 | } |
Damien Miller | 15b05cf | 2012-12-03 09:53:20 +1100 | [diff] [blame] | 345 | userauth_finish(authctxt, authenticated, "keyboard-interactive", |
| 346 | devicename); |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 347 | } |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 348 | |
| 349 | void |
| 350 | privsep_challenge_enable(void) |
| 351 | { |
Damien Miller | a6a7c19 | 2003-05-26 21:36:13 +1000 | [diff] [blame] | 352 | #if defined(BSD_AUTH) || defined(USE_PAM) || defined(SKEY) |
| 353 | int n = 0; |
| 354 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 355 | #ifdef BSD_AUTH |
| 356 | extern KbdintDevice mm_bsdauth_device; |
| 357 | #endif |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 358 | #ifdef USE_PAM |
| 359 | extern KbdintDevice mm_sshpam_device; |
| 360 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 361 | #ifdef SKEY |
| 362 | extern KbdintDevice mm_skey_device; |
| 363 | #endif |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 364 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 365 | #ifdef BSD_AUTH |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 366 | devices[n++] = &mm_bsdauth_device; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 367 | #else |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 368 | #ifdef USE_PAM |
| 369 | devices[n++] = &mm_sshpam_device; |
| 370 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 371 | #ifdef SKEY |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 372 | devices[n++] = &mm_skey_device; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 373 | #endif |
| 374 | #endif |
| 375 | } |