blob: 80f212287f5011e4806295f2fde51eb7b8e1a42d [file] [log] [blame]
markus@openbsd.org5f4082d2017-05-30 14:18:15 +00001/* $OpenBSD: auth2-chall.c,v 1.46 2017/05/30 14:18:15 markus Exp $ */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002/*
Ben Lindstrom92a2e382001-03-05 06:59:27 +00003 * Copyright (c) 2001 Markus Friedl. All rights reserved.
Ben Lindstrom551ea372001-06-05 18:56:16 +00004 * Copyright (c) 2001 Per Allansson. All rights reserved.
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00005 *
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 Millerd7834352006-08-05 12:39:39 +100026
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000027#include "includes.h"
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000028
Damien Millerd7834352006-08-05 12:39:39 +100029#include <sys/types.h>
30
Damien Millerded319c2006-09-01 15:38:36 +100031#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100032#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100033#include <string.h>
34
Damien Millerd7834352006-08-05 12:39:39 +100035#include "xmalloc.h"
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000036#include "ssh2.h"
Damien Millerd7834352006-08-05 12:39:39 +100037#include "key.h"
38#include "hostfile.h"
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000039#include "auth.h"
Damien Miller0e3b8722002-01-22 23:26:38 +110040#include "buffer.h"
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000041#include "packet.h"
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000042#include "dispatch.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000043#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100044#include "misc.h"
Darren Tucker3c660802005-01-20 22:20:50 +110045#include "servconf.h"
46
47/* import */
48extern ServerOptions options;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000049
Ben Lindstrombba81212001-06-25 05:01:22 +000050static int auth2_challenge_start(Authctxt *);
51static int send_userauth_info_request(Authctxt *);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +000052static int input_userauth_info_response(int, u_int32_t, void *);
Ben Lindstrom551ea372001-06-05 18:56:16 +000053
54#ifdef BSD_AUTH
55extern KbdintDevice bsdauth_device;
56#else
Damien Miller4f9f42a2003-05-10 19:28:02 +100057#ifdef USE_PAM
58extern KbdintDevice sshpam_device;
59#endif
Ben Lindstrom551ea372001-06-05 18:56:16 +000060#ifdef SKEY
61extern KbdintDevice skey_device;
62#endif
63#endif
64
65KbdintDevice *devices[] = {
66#ifdef BSD_AUTH
67 &bsdauth_device,
68#else
Damien Miller4f9f42a2003-05-10 19:28:02 +100069#ifdef USE_PAM
70 &sshpam_device,
71#endif
Ben Lindstrom551ea372001-06-05 18:56:16 +000072#ifdef SKEY
73 &skey_device,
74#endif
75#endif
76 NULL
77};
78
79typedef struct KbdintAuthctxt KbdintAuthctxt;
80struct KbdintAuthctxt
81{
82 char *devices;
83 void *ctxt;
84 KbdintDevice *device;
Damien Millerfb7fd952002-06-26 23:58:39 +100085 u_int nreq;
djm@openbsd.org5b64f852015-07-18 07:57:14 +000086 u_int devices_done;
Ben Lindstrom551ea372001-06-05 18:56:16 +000087};
88
Darren Tucker3c660802005-01-20 22:20:50 +110089#ifdef USE_PAM
90void
91remove_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 Lindstrombba81212001-06-25 05:01:22 +0000104static KbdintAuthctxt *
Ben Lindstrom551ea372001-06-05 18:56:16 +0000105kbdint_alloc(const char *devs)
106{
107 KbdintAuthctxt *kbdintctxt;
Damien Miller0e3b8722002-01-22 23:26:38 +1100108 Buffer b;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000109 int i;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000110
Darren Tucker3c660802005-01-20 22:20:50 +1100111#ifdef USE_PAM
112 if (!options.use_pam)
113 remove_kbdint_device("pam");
114#endif
115
Damien Miller6c81fee2013-11-08 12:19:55 +1100116 kbdintctxt = xcalloc(1, sizeof(KbdintAuthctxt));
Ben Lindstrom551ea372001-06-05 18:56:16 +0000117 if (strcmp(devs, "") == 0) {
Damien Miller0e3b8722002-01-22 23:26:38 +1100118 buffer_init(&b);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000119 for (i = 0; devices[i]; i++) {
Damien Miller0e3b8722002-01-22 23:26:38 +1100120 if (buffer_len(&b) > 0)
121 buffer_append(&b, ",", 1);
122 buffer_append(&b, devices[i]->name,
123 strlen(devices[i]->name));
Ben Lindstrom551ea372001-06-05 18:56:16 +0000124 }
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000125 if ((kbdintctxt->devices = sshbuf_dup_string(&b)) == NULL)
126 fatal("%s: sshbuf_dup_string failed", __func__);
Damien Miller0e3b8722002-01-22 23:26:38 +1100127 buffer_free(&b);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000128 } else {
129 kbdintctxt->devices = xstrdup(devs);
130 }
Damien Miller0e3b8722002-01-22 23:26:38 +1100131 debug("kbdint_alloc: devices '%s'", kbdintctxt->devices);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000132 kbdintctxt->ctxt = NULL;
133 kbdintctxt->device = NULL;
Damien Millerfb7fd952002-06-26 23:58:39 +1000134 kbdintctxt->nreq = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000135
136 return kbdintctxt;
137}
Ben Lindstrombba81212001-06-25 05:01:22 +0000138static void
Ben Lindstrom551ea372001-06-05 18:56:16 +0000139kbdint_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 Lindstrombba81212001-06-25 05:01:22 +0000147static void
Ben Lindstrom551ea372001-06-05 18:56:16 +0000148kbdint_free(KbdintAuthctxt *kbdintctxt)
149{
150 if (kbdintctxt->device)
151 kbdint_reset_device(kbdintctxt);
Darren Tuckera627d422013-06-02 07:31:17 +1000152 free(kbdintctxt->devices);
Damien Miller1d2c4562014-02-04 11:18:20 +1100153 explicit_bzero(kbdintctxt, sizeof(*kbdintctxt));
Darren Tuckera627d422013-06-02 07:31:17 +1000154 free(kbdintctxt);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000155}
156/* get next device */
Ben Lindstrombba81212001-06-25 05:01:22 +0000157static int
Damien Miller91a55f22013-04-23 15:18:10 +1000158kbdint_next_device(Authctxt *authctxt, KbdintAuthctxt *kbdintctxt)
Ben Lindstrom551ea372001-06-05 18:56:16 +0000159{
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 Miller91a55f22013-04-23 15:18:10 +1000172 for (i = 0; devices[i]; i++) {
djm@openbsd.org5b64f852015-07-18 07:57:14 +0000173 if ((kbdintctxt->devices_done & (1 << i)) != 0 ||
174 !auth2_method_allowed(authctxt,
Damien Miller91a55f22013-04-23 15:18:10 +1000175 "keyboard-interactive", devices[i]->name))
176 continue;
djm@openbsd.org5b64f852015-07-18 07:57:14 +0000177 if (strncmp(kbdintctxt->devices, devices[i]->name,
178 len) == 0) {
Ben Lindstrom551ea372001-06-05 18:56:16 +0000179 kbdintctxt->device = devices[i];
djm@openbsd.org5b64f852015-07-18 07:57:14 +0000180 kbdintctxt->devices_done |= 1 << i;
181 }
Damien Miller91a55f22013-04-23 15:18:10 +1000182 }
Ben Lindstrom551ea372001-06-05 18:56:16 +0000183 t = kbdintctxt->devices;
184 kbdintctxt->devices = t[len] ? xstrdup(t+len+1) : NULL;
Darren Tuckera627d422013-06-02 07:31:17 +1000185 free(t);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000186 debug2("kbdint_next_device: devices %s", kbdintctxt->devices ?
Damien Miller0dc1bef2005-07-17 17:22:45 +1000187 kbdintctxt->devices : "<empty>");
Ben Lindstrom551ea372001-06-05 18:56:16 +0000188 } while (kbdintctxt->devices && !kbdintctxt->device);
189
190 return kbdintctxt->device ? 1 : 0;
191}
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000192
193/*
Ben Lindstrombdfb4df2001-10-03 17:12:43 +0000194 * try challenge-response, set authctxt->postponed if we have to
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000195 * wait for the response.
196 */
197int
198auth2_challenge(Authctxt *authctxt, char *devs)
199{
Ben Lindstrom551ea372001-06-05 18:56:16 +0000200 debug("auth2_challenge: user=%s devs=%s",
201 authctxt->user ? authctxt->user : "<nouser>",
202 devs ? devs : "<no devs>");
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000203
Ben Lindstrom742e89e2001-06-09 01:17:23 +0000204 if (authctxt->user == NULL || !devs)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000205 return 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100206 if (authctxt->kbdintctxt == NULL)
Ben Lindstrom551ea372001-06-05 18:56:16 +0000207 authctxt->kbdintctxt = kbdint_alloc(devs);
208 return auth2_challenge_start(authctxt);
209}
210
Damien Milleree116252001-12-21 12:42:34 +1100211/* unregister kbd-int callbacks and context */
212void
213auth2_challenge_stop(Authctxt *authctxt)
214{
215 /* unregister callback */
216 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL);
Damien Miller80163902007-01-05 16:30:16 +1100217 if (authctxt->kbdintctxt != NULL) {
Damien Milleree116252001-12-21 12:42:34 +1100218 kbdint_free(authctxt->kbdintctxt);
219 authctxt->kbdintctxt = NULL;
220 }
221}
222
Ben Lindstrom551ea372001-06-05 18:56:16 +0000223/* side effect: sets authctxt->postponed if a reply was sent*/
224static int
225auth2_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 Miller91a55f22013-04-23 15:18:10 +1000232 if (kbdint_next_device(authctxt, kbdintctxt) == 0) {
Damien Milleree116252001-12-21 12:42:34 +1100233 auth2_challenge_stop(authctxt);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000234 return 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000235 }
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 Milleree116252001-12-21 12:42:34 +1100240 auth2_challenge_stop(authctxt);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000241 return 0;
242 }
243 if (send_userauth_info_request(authctxt) == 0) {
Damien Milleree116252001-12-21 12:42:34 +1100244 auth2_challenge_stop(authctxt);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000245 return 0;
246 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000247 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE,
248 &input_userauth_info_response);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000249
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000250 authctxt->postponed = 1;
251 return 0;
252}
253
Ben Lindstrom551ea372001-06-05 18:56:16 +0000254static int
255send_userauth_info_request(Authctxt *authctxt)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000256{
Ben Lindstrom551ea372001-06-05 18:56:16 +0000257 KbdintAuthctxt *kbdintctxt;
258 char *name, *instr, **prompts;
Damien Millereccb9de2005-06-17 12:59:34 +1000259 u_int i, *echo_on;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000260
261 kbdintctxt = authctxt->kbdintctxt;
262 if (kbdintctxt->device->query(kbdintctxt->ctxt,
Damien Millerfb7fd952002-06-26 23:58:39 +1000263 &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on))
Ben Lindstrom551ea372001-06-05 18:56:16 +0000264 return 0;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000265
266 packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000267 packet_put_cstring(name);
268 packet_put_cstring(instr);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000269 packet_put_cstring(""); /* language not used */
Damien Millerfb7fd952002-06-26 23:58:39 +1000270 packet_put_int(kbdintctxt->nreq);
271 for (i = 0; i < kbdintctxt->nreq; i++) {
Ben Lindstrom551ea372001-06-05 18:56:16 +0000272 packet_put_cstring(prompts[i]);
273 packet_put_char(echo_on[i]);
274 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000275 packet_send();
276 packet_write_wait();
Ben Lindstrom551ea372001-06-05 18:56:16 +0000277
Damien Millerfb7fd952002-06-26 23:58:39 +1000278 for (i = 0; i < kbdintctxt->nreq; i++)
Darren Tuckera627d422013-06-02 07:31:17 +1000279 free(prompts[i]);
280 free(prompts);
281 free(echo_on);
282 free(name);
283 free(instr);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000284 return 1;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000285}
286
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000287static int
Damien Miller630d6f42002-01-22 23:17:30 +1100288input_userauth_info_response(int type, u_int32_t seq, void *ctxt)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000289{
markus@openbsd.org5f4082d2017-05-30 14:18:15 +0000290 struct ssh *ssh = ctxt;
291 Authctxt *authctxt = ssh->authctxt;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000292 KbdintAuthctxt *kbdintctxt;
Damien Millerc30def92009-01-28 16:13:39 +1100293 int authenticated = 0, res;
Damien Millereccb9de2005-06-17 12:59:34 +1000294 u_int i, nresp;
Damien Miller55aca022012-12-03 11:25:30 +1100295 const char *devicename = NULL;
296 char **response = NULL;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000297
298 if (authctxt == NULL)
299 fatal("input_userauth_info_response: no authctxt");
Ben Lindstrom551ea372001-06-05 18:56:16 +0000300 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 Lindstromdb65e8f2001-01-19 04:26:52 +0000305
306 authctxt->postponed = 0; /* reset */
307 nresp = packet_get_int();
Damien Millerfb7fd952002-06-26 23:58:39 +1000308 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 Lindstrom551ea372001-06-05 18:56:16 +0000312 if (nresp > 0) {
Damien Miller07d86be2006-03-26 14:19:21 +1100313 response = xcalloc(nresp, sizeof(char *));
Ben Lindstrom551ea372001-06-05 18:56:16 +0000314 for (i = 0; i < nresp; i++)
315 response[i] = packet_get_string(NULL);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000316 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100317 packet_check_eom();
Ben Lindstrom551ea372001-06-05 18:56:16 +0000318
Darren Tucker611649e2005-01-20 11:05:34 +1100319 res = kbdintctxt->device->respond(kbdintctxt->ctxt, nresp, response);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000320
321 for (i = 0; i < nresp; i++) {
Damien Millera5103f42014-02-04 11:20:14 +1100322 explicit_bzero(response[i], strlen(response[i]));
Darren Tuckera627d422013-06-02 07:31:17 +1000323 free(response[i]);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000324 }
Darren Tuckera627d422013-06-02 07:31:17 +1000325 free(response);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000326
327 switch (res) {
328 case 0:
329 /* Success! */
Darren Tucker611649e2005-01-20 11:05:34 +1100330 authenticated = authctxt->valid ? 1 : 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000331 break;
332 case 1:
333 /* Authentication needs further interaction */
Damien Milleree116252001-12-21 12:42:34 +1100334 if (send_userauth_info_request(authctxt) == 1)
335 authctxt->postponed = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000336 break;
337 default:
338 /* Failure! */
339 break;
340 }
Damien Miller15b05cf2012-12-03 09:53:20 +1100341 devicename = kbdintctxt->device->name;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000342 if (!authctxt->postponed) {
Ben Lindstrom551ea372001-06-05 18:56:16 +0000343 if (authenticated) {
Damien Milleree116252001-12-21 12:42:34 +1100344 auth2_challenge_stop(authctxt);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000345 } else {
346 /* start next device */
347 /* may set authctxt->postponed */
348 auth2_challenge_start(authctxt);
349 }
350 }
Damien Miller15b05cf2012-12-03 09:53:20 +1100351 userauth_finish(authctxt, authenticated, "keyboard-interactive",
352 devicename);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000353 return 0;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000354}
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000355
356void
357privsep_challenge_enable(void)
358{
Damien Millera6a7c192003-05-26 21:36:13 +1000359#if defined(BSD_AUTH) || defined(USE_PAM) || defined(SKEY)
360 int n = 0;
361#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000362#ifdef BSD_AUTH
363 extern KbdintDevice mm_bsdauth_device;
364#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000365#ifdef USE_PAM
366 extern KbdintDevice mm_sshpam_device;
367#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000368#ifdef SKEY
369 extern KbdintDevice mm_skey_device;
370#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000371
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000372#ifdef BSD_AUTH
Damien Miller4f9f42a2003-05-10 19:28:02 +1000373 devices[n++] = &mm_bsdauth_device;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000374#else
Damien Miller4f9f42a2003-05-10 19:28:02 +1000375#ifdef USE_PAM
376 devices[n++] = &mm_sshpam_device;
377#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000378#ifdef SKEY
Damien Miller4f9f42a2003-05-10 19:28:02 +1000379 devices[n++] = &mm_skey_device;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000380#endif
381#endif
382}