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