blob: ddabe1a90710a44d8f39770cd20e4cc78e21534e [file] [log] [blame]
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001/* $OpenBSD: auth2-chall.c,v 1.42 2015/01/19 20:07:45 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;
Ben Lindstrom551ea372001-06-05 18:56:16 +000086};
87
Darren Tucker3c660802005-01-20 22:20:50 +110088#ifdef USE_PAM
89void
90remove_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 Lindstrombba81212001-06-25 05:01:22 +0000103static KbdintAuthctxt *
Ben Lindstrom551ea372001-06-05 18:56:16 +0000104kbdint_alloc(const char *devs)
105{
106 KbdintAuthctxt *kbdintctxt;
Damien Miller0e3b8722002-01-22 23:26:38 +1100107 Buffer b;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000108 int i;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000109
Darren Tucker3c660802005-01-20 22:20:50 +1100110#ifdef USE_PAM
111 if (!options.use_pam)
112 remove_kbdint_device("pam");
113#endif
114
Damien Miller6c81fee2013-11-08 12:19:55 +1100115 kbdintctxt = xcalloc(1, sizeof(KbdintAuthctxt));
Ben Lindstrom551ea372001-06-05 18:56:16 +0000116 if (strcmp(devs, "") == 0) {
Damien Miller0e3b8722002-01-22 23:26:38 +1100117 buffer_init(&b);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000118 for (i = 0; devices[i]; i++) {
Damien Miller0e3b8722002-01-22 23:26:38 +1100119 if (buffer_len(&b) > 0)
120 buffer_append(&b, ",", 1);
121 buffer_append(&b, devices[i]->name,
122 strlen(devices[i]->name));
Ben Lindstrom551ea372001-06-05 18:56:16 +0000123 }
Damien Miller0e3b8722002-01-22 23:26:38 +1100124 buffer_append(&b, "\0", 1);
125 kbdintctxt->devices = xstrdup(buffer_ptr(&b));
126 buffer_free(&b);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000127 } else {
128 kbdintctxt->devices = xstrdup(devs);
129 }
Damien Miller0e3b8722002-01-22 23:26:38 +1100130 debug("kbdint_alloc: devices '%s'", kbdintctxt->devices);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000131 kbdintctxt->ctxt = NULL;
132 kbdintctxt->device = NULL;
Damien Millerfb7fd952002-06-26 23:58:39 +1000133 kbdintctxt->nreq = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000134
135 return kbdintctxt;
136}
Ben Lindstrombba81212001-06-25 05:01:22 +0000137static void
Ben Lindstrom551ea372001-06-05 18:56:16 +0000138kbdint_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 Lindstrombba81212001-06-25 05:01:22 +0000146static void
Ben Lindstrom551ea372001-06-05 18:56:16 +0000147kbdint_free(KbdintAuthctxt *kbdintctxt)
148{
149 if (kbdintctxt->device)
150 kbdint_reset_device(kbdintctxt);
Darren Tuckera627d422013-06-02 07:31:17 +1000151 free(kbdintctxt->devices);
Damien Miller1d2c4562014-02-04 11:18:20 +1100152 explicit_bzero(kbdintctxt, sizeof(*kbdintctxt));
Darren Tuckera627d422013-06-02 07:31:17 +1000153 free(kbdintctxt);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000154}
155/* get next device */
Ben Lindstrombba81212001-06-25 05:01:22 +0000156static int
Damien Miller91a55f22013-04-23 15:18:10 +1000157kbdint_next_device(Authctxt *authctxt, KbdintAuthctxt *kbdintctxt)
Ben Lindstrom551ea372001-06-05 18:56:16 +0000158{
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 Miller91a55f22013-04-23 15:18:10 +1000171 for (i = 0; devices[i]; i++) {
172 if (!auth2_method_allowed(authctxt,
173 "keyboard-interactive", devices[i]->name))
174 continue;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000175 if (strncmp(kbdintctxt->devices, devices[i]->name, len) == 0)
176 kbdintctxt->device = devices[i];
Damien Miller91a55f22013-04-23 15:18:10 +1000177 }
Ben Lindstrom551ea372001-06-05 18:56:16 +0000178 t = kbdintctxt->devices;
179 kbdintctxt->devices = t[len] ? xstrdup(t+len+1) : NULL;
Darren Tuckera627d422013-06-02 07:31:17 +1000180 free(t);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000181 debug2("kbdint_next_device: devices %s", kbdintctxt->devices ?
Damien Miller0dc1bef2005-07-17 17:22:45 +1000182 kbdintctxt->devices : "<empty>");
Ben Lindstrom551ea372001-06-05 18:56:16 +0000183 } while (kbdintctxt->devices && !kbdintctxt->device);
184
185 return kbdintctxt->device ? 1 : 0;
186}
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000187
188/*
Ben Lindstrombdfb4df2001-10-03 17:12:43 +0000189 * try challenge-response, set authctxt->postponed if we have to
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000190 * wait for the response.
191 */
192int
193auth2_challenge(Authctxt *authctxt, char *devs)
194{
Ben Lindstrom551ea372001-06-05 18:56:16 +0000195 debug("auth2_challenge: user=%s devs=%s",
196 authctxt->user ? authctxt->user : "<nouser>",
197 devs ? devs : "<no devs>");
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000198
Ben Lindstrom742e89e2001-06-09 01:17:23 +0000199 if (authctxt->user == NULL || !devs)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000200 return 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100201 if (authctxt->kbdintctxt == NULL)
Ben Lindstrom551ea372001-06-05 18:56:16 +0000202 authctxt->kbdintctxt = kbdint_alloc(devs);
203 return auth2_challenge_start(authctxt);
204}
205
Damien Milleree116252001-12-21 12:42:34 +1100206/* unregister kbd-int callbacks and context */
207void
208auth2_challenge_stop(Authctxt *authctxt)
209{
210 /* unregister callback */
211 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL);
Damien Miller80163902007-01-05 16:30:16 +1100212 if (authctxt->kbdintctxt != NULL) {
Damien Milleree116252001-12-21 12:42:34 +1100213 kbdint_free(authctxt->kbdintctxt);
214 authctxt->kbdintctxt = NULL;
215 }
216}
217
Ben Lindstrom551ea372001-06-05 18:56:16 +0000218/* side effect: sets authctxt->postponed if a reply was sent*/
219static int
220auth2_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 Miller91a55f22013-04-23 15:18:10 +1000227 if (kbdint_next_device(authctxt, kbdintctxt) == 0) {
Damien Milleree116252001-12-21 12:42:34 +1100228 auth2_challenge_stop(authctxt);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000229 return 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000230 }
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 Milleree116252001-12-21 12:42:34 +1100235 auth2_challenge_stop(authctxt);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000236 return 0;
237 }
238 if (send_userauth_info_request(authctxt) == 0) {
Damien Milleree116252001-12-21 12:42:34 +1100239 auth2_challenge_stop(authctxt);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000240 return 0;
241 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000242 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE,
243 &input_userauth_info_response);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000244
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000245 authctxt->postponed = 1;
246 return 0;
247}
248
Ben Lindstrom551ea372001-06-05 18:56:16 +0000249static int
250send_userauth_info_request(Authctxt *authctxt)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000251{
Ben Lindstrom551ea372001-06-05 18:56:16 +0000252 KbdintAuthctxt *kbdintctxt;
253 char *name, *instr, **prompts;
Damien Millereccb9de2005-06-17 12:59:34 +1000254 u_int i, *echo_on;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000255
256 kbdintctxt = authctxt->kbdintctxt;
257 if (kbdintctxt->device->query(kbdintctxt->ctxt,
Damien Millerfb7fd952002-06-26 23:58:39 +1000258 &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on))
Ben Lindstrom551ea372001-06-05 18:56:16 +0000259 return 0;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000260
261 packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000262 packet_put_cstring(name);
263 packet_put_cstring(instr);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000264 packet_put_cstring(""); /* language not used */
Damien Millerfb7fd952002-06-26 23:58:39 +1000265 packet_put_int(kbdintctxt->nreq);
266 for (i = 0; i < kbdintctxt->nreq; i++) {
Ben Lindstrom551ea372001-06-05 18:56:16 +0000267 packet_put_cstring(prompts[i]);
268 packet_put_char(echo_on[i]);
269 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000270 packet_send();
271 packet_write_wait();
Ben Lindstrom551ea372001-06-05 18:56:16 +0000272
Damien Millerfb7fd952002-06-26 23:58:39 +1000273 for (i = 0; i < kbdintctxt->nreq; i++)
Darren Tuckera627d422013-06-02 07:31:17 +1000274 free(prompts[i]);
275 free(prompts);
276 free(echo_on);
277 free(name);
278 free(instr);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000279 return 1;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000280}
281
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000282static int
Damien Miller630d6f42002-01-22 23:17:30 +1100283input_userauth_info_response(int type, u_int32_t seq, void *ctxt)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000284{
285 Authctxt *authctxt = ctxt;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000286 KbdintAuthctxt *kbdintctxt;
Damien Millerc30def92009-01-28 16:13:39 +1100287 int authenticated = 0, res;
Damien Millereccb9de2005-06-17 12:59:34 +1000288 u_int i, nresp;
Damien Miller55aca022012-12-03 11:25:30 +1100289 const char *devicename = NULL;
290 char **response = NULL;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000291
292 if (authctxt == NULL)
293 fatal("input_userauth_info_response: no authctxt");
Ben Lindstrom551ea372001-06-05 18:56:16 +0000294 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 Lindstromdb65e8f2001-01-19 04:26:52 +0000299
300 authctxt->postponed = 0; /* reset */
301 nresp = packet_get_int();
Damien Millerfb7fd952002-06-26 23:58:39 +1000302 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 Lindstrom551ea372001-06-05 18:56:16 +0000306 if (nresp > 0) {
Damien Miller07d86be2006-03-26 14:19:21 +1100307 response = xcalloc(nresp, sizeof(char *));
Ben Lindstrom551ea372001-06-05 18:56:16 +0000308 for (i = 0; i < nresp; i++)
309 response[i] = packet_get_string(NULL);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000310 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100311 packet_check_eom();
Ben Lindstrom551ea372001-06-05 18:56:16 +0000312
Darren Tucker611649e2005-01-20 11:05:34 +1100313 res = kbdintctxt->device->respond(kbdintctxt->ctxt, nresp, response);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000314
315 for (i = 0; i < nresp; i++) {
Damien Millera5103f42014-02-04 11:20:14 +1100316 explicit_bzero(response[i], strlen(response[i]));
Darren Tuckera627d422013-06-02 07:31:17 +1000317 free(response[i]);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000318 }
Darren Tuckera627d422013-06-02 07:31:17 +1000319 free(response);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000320
321 switch (res) {
322 case 0:
323 /* Success! */
Darren Tucker611649e2005-01-20 11:05:34 +1100324 authenticated = authctxt->valid ? 1 : 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000325 break;
326 case 1:
327 /* Authentication needs further interaction */
Damien Milleree116252001-12-21 12:42:34 +1100328 if (send_userauth_info_request(authctxt) == 1)
329 authctxt->postponed = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000330 break;
331 default:
332 /* Failure! */
333 break;
334 }
Damien Miller15b05cf2012-12-03 09:53:20 +1100335 devicename = kbdintctxt->device->name;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000336 if (!authctxt->postponed) {
Ben Lindstrom551ea372001-06-05 18:56:16 +0000337 if (authenticated) {
Damien Milleree116252001-12-21 12:42:34 +1100338 auth2_challenge_stop(authctxt);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000339 } else {
340 /* start next device */
341 /* may set authctxt->postponed */
342 auth2_challenge_start(authctxt);
343 }
344 }
Damien Miller15b05cf2012-12-03 09:53:20 +1100345 userauth_finish(authctxt, authenticated, "keyboard-interactive",
346 devicename);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000347 return 0;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000348}
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000349
350void
351privsep_challenge_enable(void)
352{
Damien Millera6a7c192003-05-26 21:36:13 +1000353#if defined(BSD_AUTH) || defined(USE_PAM) || defined(SKEY)
354 int n = 0;
355#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000356#ifdef BSD_AUTH
357 extern KbdintDevice mm_bsdauth_device;
358#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000359#ifdef USE_PAM
360 extern KbdintDevice mm_sshpam_device;
361#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000362#ifdef SKEY
363 extern KbdintDevice mm_skey_device;
364#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000365
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000366#ifdef BSD_AUTH
Damien Miller4f9f42a2003-05-10 19:28:02 +1000367 devices[n++] = &mm_bsdauth_device;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000368#else
Damien Miller4f9f42a2003-05-10 19:28:02 +1000369#ifdef USE_PAM
370 devices[n++] = &mm_sshpam_device;
371#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000372#ifdef SKEY
Damien Miller4f9f42a2003-05-10 19:28:02 +1000373 devices[n++] = &mm_skey_device;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000374#endif
375#endif
376}