blob: ed1acdd3b8b81a10fa06ca862084d6b42ed36da6 [file] [log] [blame]
Damien Miller91a55f22013-04-23 15:18:10 +10001/* $OpenBSD: auth2-chall.c,v 1.37 2013/03/07 19:27:25 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"
Darren Tucker3c660802005-01-20 22:20:50 +110044#include "servconf.h"
45
46/* import */
47extern ServerOptions options;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000048
Ben Lindstrombba81212001-06-25 05:01:22 +000049static int auth2_challenge_start(Authctxt *);
50static int send_userauth_info_request(Authctxt *);
Damien Miller630d6f42002-01-22 23:17:30 +110051static void input_userauth_info_response(int, u_int32_t, void *);
Ben Lindstrom551ea372001-06-05 18:56:16 +000052
53#ifdef BSD_AUTH
54extern KbdintDevice bsdauth_device;
55#else
Damien Miller4f9f42a2003-05-10 19:28:02 +100056#ifdef USE_PAM
57extern KbdintDevice sshpam_device;
58#endif
Ben Lindstrom551ea372001-06-05 18:56:16 +000059#ifdef SKEY
60extern KbdintDevice skey_device;
61#endif
62#endif
63
64KbdintDevice *devices[] = {
65#ifdef BSD_AUTH
66 &bsdauth_device,
67#else
Damien Miller4f9f42a2003-05-10 19:28:02 +100068#ifdef USE_PAM
69 &sshpam_device,
70#endif
Ben Lindstrom551ea372001-06-05 18:56:16 +000071#ifdef SKEY
72 &skey_device,
73#endif
74#endif
75 NULL
76};
77
78typedef struct KbdintAuthctxt KbdintAuthctxt;
79struct KbdintAuthctxt
80{
81 char *devices;
82 void *ctxt;
83 KbdintDevice *device;
Damien Millerfb7fd952002-06-26 23:58:39 +100084 u_int nreq;
Ben Lindstrom551ea372001-06-05 18:56:16 +000085};
86
Darren Tucker3c660802005-01-20 22:20:50 +110087#ifdef USE_PAM
88void
89remove_kbdint_device(const char *devname)
90{
91 int i, j;
92
93 for (i = 0; devices[i] != NULL; i++)
94 if (strcmp(devices[i]->name, devname) == 0) {
95 for (j = i; devices[j] != NULL; j++)
96 devices[j] = devices[j+1];
97 i--;
98 }
99}
100#endif
101
Ben Lindstrombba81212001-06-25 05:01:22 +0000102static KbdintAuthctxt *
Ben Lindstrom551ea372001-06-05 18:56:16 +0000103kbdint_alloc(const char *devs)
104{
105 KbdintAuthctxt *kbdintctxt;
Damien Miller0e3b8722002-01-22 23:26:38 +1100106 Buffer b;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000107 int i;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000108
Darren Tucker3c660802005-01-20 22:20:50 +1100109#ifdef USE_PAM
110 if (!options.use_pam)
111 remove_kbdint_device("pam");
112#endif
113
Ben Lindstrom551ea372001-06-05 18:56:16 +0000114 kbdintctxt = xmalloc(sizeof(KbdintAuthctxt));
115 if (strcmp(devs, "") == 0) {
Damien Miller0e3b8722002-01-22 23:26:38 +1100116 buffer_init(&b);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000117 for (i = 0; devices[i]; i++) {
Damien Miller0e3b8722002-01-22 23:26:38 +1100118 if (buffer_len(&b) > 0)
119 buffer_append(&b, ",", 1);
120 buffer_append(&b, devices[i]->name,
121 strlen(devices[i]->name));
Ben Lindstrom551ea372001-06-05 18:56:16 +0000122 }
Damien Miller0e3b8722002-01-22 23:26:38 +1100123 buffer_append(&b, "\0", 1);
124 kbdintctxt->devices = xstrdup(buffer_ptr(&b));
125 buffer_free(&b);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000126 } else {
127 kbdintctxt->devices = xstrdup(devs);
128 }
Damien Miller0e3b8722002-01-22 23:26:38 +1100129 debug("kbdint_alloc: devices '%s'", kbdintctxt->devices);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000130 kbdintctxt->ctxt = NULL;
131 kbdintctxt->device = NULL;
Damien Millerfb7fd952002-06-26 23:58:39 +1000132 kbdintctxt->nreq = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000133
134 return kbdintctxt;
135}
Ben Lindstrombba81212001-06-25 05:01:22 +0000136static void
Ben Lindstrom551ea372001-06-05 18:56:16 +0000137kbdint_reset_device(KbdintAuthctxt *kbdintctxt)
138{
139 if (kbdintctxt->ctxt) {
140 kbdintctxt->device->free_ctx(kbdintctxt->ctxt);
141 kbdintctxt->ctxt = NULL;
142 }
143 kbdintctxt->device = NULL;
144}
Ben Lindstrombba81212001-06-25 05:01:22 +0000145static void
Ben Lindstrom551ea372001-06-05 18:56:16 +0000146kbdint_free(KbdintAuthctxt *kbdintctxt)
147{
148 if (kbdintctxt->device)
149 kbdint_reset_device(kbdintctxt);
150 if (kbdintctxt->devices) {
151 xfree(kbdintctxt->devices);
152 kbdintctxt->devices = NULL;
153 }
154 xfree(kbdintctxt);
155}
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++) {
173 if (!auth2_method_allowed(authctxt,
174 "keyboard-interactive", devices[i]->name))
175 continue;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000176 if (strncmp(kbdintctxt->devices, devices[i]->name, len) == 0)
177 kbdintctxt->device = devices[i];
Damien Miller91a55f22013-04-23 15:18:10 +1000178 }
Ben Lindstrom551ea372001-06-05 18:56:16 +0000179 t = kbdintctxt->devices;
180 kbdintctxt->devices = t[len] ? xstrdup(t+len+1) : NULL;
181 xfree(t);
182 debug2("kbdint_next_device: devices %s", kbdintctxt->devices ?
Damien Miller0dc1bef2005-07-17 17:22:45 +1000183 kbdintctxt->devices : "<empty>");
Ben Lindstrom551ea372001-06-05 18:56:16 +0000184 } while (kbdintctxt->devices && !kbdintctxt->device);
185
186 return kbdintctxt->device ? 1 : 0;
187}
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000188
189/*
Ben Lindstrombdfb4df2001-10-03 17:12:43 +0000190 * try challenge-response, set authctxt->postponed if we have to
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000191 * wait for the response.
192 */
193int
194auth2_challenge(Authctxt *authctxt, char *devs)
195{
Ben Lindstrom551ea372001-06-05 18:56:16 +0000196 debug("auth2_challenge: user=%s devs=%s",
197 authctxt->user ? authctxt->user : "<nouser>",
198 devs ? devs : "<no devs>");
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000199
Ben Lindstrom742e89e2001-06-09 01:17:23 +0000200 if (authctxt->user == NULL || !devs)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000201 return 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100202 if (authctxt->kbdintctxt == NULL)
Ben Lindstrom551ea372001-06-05 18:56:16 +0000203 authctxt->kbdintctxt = kbdint_alloc(devs);
204 return auth2_challenge_start(authctxt);
205}
206
Damien Milleree116252001-12-21 12:42:34 +1100207/* unregister kbd-int callbacks and context */
208void
209auth2_challenge_stop(Authctxt *authctxt)
210{
211 /* unregister callback */
212 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL);
Damien Miller80163902007-01-05 16:30:16 +1100213 if (authctxt->kbdintctxt != NULL) {
Damien Milleree116252001-12-21 12:42:34 +1100214 kbdint_free(authctxt->kbdintctxt);
215 authctxt->kbdintctxt = NULL;
216 }
217}
218
Ben Lindstrom551ea372001-06-05 18:56:16 +0000219/* side effect: sets authctxt->postponed if a reply was sent*/
220static int
221auth2_challenge_start(Authctxt *authctxt)
222{
223 KbdintAuthctxt *kbdintctxt = authctxt->kbdintctxt;
224
225 debug2("auth2_challenge_start: devices %s",
226 kbdintctxt->devices ? kbdintctxt->devices : "<empty>");
227
Damien Miller91a55f22013-04-23 15:18:10 +1000228 if (kbdint_next_device(authctxt, kbdintctxt) == 0) {
Damien Milleree116252001-12-21 12:42:34 +1100229 auth2_challenge_stop(authctxt);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000230 return 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000231 }
232 debug("auth2_challenge_start: trying authentication method '%s'",
233 kbdintctxt->device->name);
234
235 if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) {
Damien Milleree116252001-12-21 12:42:34 +1100236 auth2_challenge_stop(authctxt);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000237 return 0;
238 }
239 if (send_userauth_info_request(authctxt) == 0) {
Damien Milleree116252001-12-21 12:42:34 +1100240 auth2_challenge_stop(authctxt);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000241 return 0;
242 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000243 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE,
244 &input_userauth_info_response);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000245
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000246 authctxt->postponed = 1;
247 return 0;
248}
249
Ben Lindstrom551ea372001-06-05 18:56:16 +0000250static int
251send_userauth_info_request(Authctxt *authctxt)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000252{
Ben Lindstrom551ea372001-06-05 18:56:16 +0000253 KbdintAuthctxt *kbdintctxt;
254 char *name, *instr, **prompts;
Damien Millereccb9de2005-06-17 12:59:34 +1000255 u_int i, *echo_on;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000256
257 kbdintctxt = authctxt->kbdintctxt;
258 if (kbdintctxt->device->query(kbdintctxt->ctxt,
Damien Millerfb7fd952002-06-26 23:58:39 +1000259 &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on))
Ben Lindstrom551ea372001-06-05 18:56:16 +0000260 return 0;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000261
262 packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000263 packet_put_cstring(name);
264 packet_put_cstring(instr);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000265 packet_put_cstring(""); /* language not used */
Damien Millerfb7fd952002-06-26 23:58:39 +1000266 packet_put_int(kbdintctxt->nreq);
267 for (i = 0; i < kbdintctxt->nreq; i++) {
Ben Lindstrom551ea372001-06-05 18:56:16 +0000268 packet_put_cstring(prompts[i]);
269 packet_put_char(echo_on[i]);
270 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000271 packet_send();
272 packet_write_wait();
Ben Lindstrom551ea372001-06-05 18:56:16 +0000273
Damien Millerfb7fd952002-06-26 23:58:39 +1000274 for (i = 0; i < kbdintctxt->nreq; i++)
Ben Lindstrom551ea372001-06-05 18:56:16 +0000275 xfree(prompts[i]);
276 xfree(prompts);
277 xfree(echo_on);
278 xfree(name);
279 xfree(instr);
280 return 1;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000281}
282
Ben Lindstrom551ea372001-06-05 18:56:16 +0000283static void
Damien Miller630d6f42002-01-22 23:17:30 +1100284input_userauth_info_response(int type, u_int32_t seq, void *ctxt)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000285{
286 Authctxt *authctxt = ctxt;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000287 KbdintAuthctxt *kbdintctxt;
Damien Millerc30def92009-01-28 16:13:39 +1100288 int authenticated = 0, res;
Damien Millereccb9de2005-06-17 12:59:34 +1000289 u_int i, nresp;
Damien Miller55aca022012-12-03 11:25:30 +1100290 const char *devicename = NULL;
291 char **response = NULL;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000292
293 if (authctxt == NULL)
294 fatal("input_userauth_info_response: no authctxt");
Ben Lindstrom551ea372001-06-05 18:56:16 +0000295 kbdintctxt = authctxt->kbdintctxt;
296 if (kbdintctxt == NULL || kbdintctxt->ctxt == NULL)
297 fatal("input_userauth_info_response: no kbdintctxt");
298 if (kbdintctxt->device == NULL)
299 fatal("input_userauth_info_response: no device");
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000300
301 authctxt->postponed = 0; /* reset */
302 nresp = packet_get_int();
Damien Millerfb7fd952002-06-26 23:58:39 +1000303 if (nresp != kbdintctxt->nreq)
304 fatal("input_userauth_info_response: wrong number of replies");
305 if (nresp > 100)
306 fatal("input_userauth_info_response: too many replies");
Ben Lindstrom551ea372001-06-05 18:56:16 +0000307 if (nresp > 0) {
Damien Miller07d86be2006-03-26 14:19:21 +1100308 response = xcalloc(nresp, sizeof(char *));
Ben Lindstrom551ea372001-06-05 18:56:16 +0000309 for (i = 0; i < nresp; i++)
310 response[i] = packet_get_string(NULL);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000311 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100312 packet_check_eom();
Ben Lindstrom551ea372001-06-05 18:56:16 +0000313
Darren Tucker611649e2005-01-20 11:05:34 +1100314 res = kbdintctxt->device->respond(kbdintctxt->ctxt, nresp, response);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000315
316 for (i = 0; i < nresp; i++) {
317 memset(response[i], 'r', strlen(response[i]));
318 xfree(response[i]);
319 }
320 if (response)
321 xfree(response);
322
323 switch (res) {
324 case 0:
325 /* Success! */
Darren Tucker611649e2005-01-20 11:05:34 +1100326 authenticated = authctxt->valid ? 1 : 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000327 break;
328 case 1:
329 /* Authentication needs further interaction */
Damien Milleree116252001-12-21 12:42:34 +1100330 if (send_userauth_info_request(authctxt) == 1)
331 authctxt->postponed = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000332 break;
333 default:
334 /* Failure! */
335 break;
336 }
Damien Miller15b05cf2012-12-03 09:53:20 +1100337 devicename = kbdintctxt->device->name;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000338 if (!authctxt->postponed) {
Ben Lindstrom551ea372001-06-05 18:56:16 +0000339 if (authenticated) {
Damien Milleree116252001-12-21 12:42:34 +1100340 auth2_challenge_stop(authctxt);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000341 } else {
342 /* start next device */
343 /* may set authctxt->postponed */
344 auth2_challenge_start(authctxt);
345 }
346 }
Damien Miller15b05cf2012-12-03 09:53:20 +1100347 userauth_finish(authctxt, authenticated, "keyboard-interactive",
348 devicename);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000349}
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000350
351void
352privsep_challenge_enable(void)
353{
Damien Millera6a7c192003-05-26 21:36:13 +1000354#if defined(BSD_AUTH) || defined(USE_PAM) || defined(SKEY)
355 int n = 0;
356#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000357#ifdef BSD_AUTH
358 extern KbdintDevice mm_bsdauth_device;
359#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000360#ifdef USE_PAM
361 extern KbdintDevice mm_sshpam_device;
362#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000363#ifdef SKEY
364 extern KbdintDevice mm_skey_device;
365#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000366
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000367#ifdef BSD_AUTH
Damien Miller4f9f42a2003-05-10 19:28:02 +1000368 devices[n++] = &mm_bsdauth_device;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000369#else
Damien Miller4f9f42a2003-05-10 19:28:02 +1000370#ifdef USE_PAM
371 devices[n++] = &mm_sshpam_device;
372#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000373#ifdef SKEY
Damien Miller4f9f42a2003-05-10 19:28:02 +1000374 devices[n++] = &mm_skey_device;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000375#endif
376#endif
377}