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