blob: 8358140b922f4ce1cb3e6ad7c4636bbfde52bcf3 [file] [log] [blame]
Damien Millerd7834352006-08-05 12:39:39 +10001/* $OpenBSD: auth2-chall.c,v 1.30 2006/08/03 03:34:41 deraadt 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 Millera7a73ee2006-08-05 11:37:59 +100031#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100032#include <string.h>
33
Damien Millerd7834352006-08-05 12:39:39 +100034#include "xmalloc.h"
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000035#include "ssh2.h"
Damien Millerd7834352006-08-05 12:39:39 +100036#include "key.h"
37#include "hostfile.h"
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000038#include "auth.h"
Damien Miller0e3b8722002-01-22 23:26:38 +110039#include "buffer.h"
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000040#include "packet.h"
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000041#include "dispatch.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000042#include "log.h"
Darren Tucker3c660802005-01-20 22:20:50 +110043#include "servconf.h"
44
45/* import */
46extern ServerOptions options;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000047
Ben Lindstrombba81212001-06-25 05:01:22 +000048static int auth2_challenge_start(Authctxt *);
49static int send_userauth_info_request(Authctxt *);
Damien Miller630d6f42002-01-22 23:17:30 +110050static void input_userauth_info_response(int, u_int32_t, void *);
Ben Lindstrom551ea372001-06-05 18:56:16 +000051
52#ifdef BSD_AUTH
53extern KbdintDevice bsdauth_device;
54#else
Damien Miller4f9f42a2003-05-10 19:28:02 +100055#ifdef USE_PAM
56extern KbdintDevice sshpam_device;
57#endif
Ben Lindstrom551ea372001-06-05 18:56:16 +000058#ifdef SKEY
59extern KbdintDevice skey_device;
60#endif
61#endif
62
63KbdintDevice *devices[] = {
64#ifdef BSD_AUTH
65 &bsdauth_device,
66#else
Damien Miller4f9f42a2003-05-10 19:28:02 +100067#ifdef USE_PAM
68 &sshpam_device,
69#endif
Ben Lindstrom551ea372001-06-05 18:56:16 +000070#ifdef SKEY
71 &skey_device,
72#endif
73#endif
74 NULL
75};
76
77typedef struct KbdintAuthctxt KbdintAuthctxt;
78struct KbdintAuthctxt
79{
80 char *devices;
81 void *ctxt;
82 KbdintDevice *device;
Damien Millerfb7fd952002-06-26 23:58:39 +100083 u_int nreq;
Ben Lindstrom551ea372001-06-05 18:56:16 +000084};
85
Darren Tucker3c660802005-01-20 22:20:50 +110086#ifdef USE_PAM
87void
88remove_kbdint_device(const char *devname)
89{
90 int i, j;
91
92 for (i = 0; devices[i] != NULL; i++)
93 if (strcmp(devices[i]->name, devname) == 0) {
94 for (j = i; devices[j] != NULL; j++)
95 devices[j] = devices[j+1];
96 i--;
97 }
98}
99#endif
100
Ben Lindstrombba81212001-06-25 05:01:22 +0000101static KbdintAuthctxt *
Ben Lindstrom551ea372001-06-05 18:56:16 +0000102kbdint_alloc(const char *devs)
103{
104 KbdintAuthctxt *kbdintctxt;
Damien Miller0e3b8722002-01-22 23:26:38 +1100105 Buffer b;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000106 int i;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000107
Darren Tucker3c660802005-01-20 22:20:50 +1100108#ifdef USE_PAM
109 if (!options.use_pam)
110 remove_kbdint_device("pam");
111#endif
112
Ben Lindstrom551ea372001-06-05 18:56:16 +0000113 kbdintctxt = xmalloc(sizeof(KbdintAuthctxt));
114 if (strcmp(devs, "") == 0) {
Damien Miller0e3b8722002-01-22 23:26:38 +1100115 buffer_init(&b);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000116 for (i = 0; devices[i]; i++) {
Damien Miller0e3b8722002-01-22 23:26:38 +1100117 if (buffer_len(&b) > 0)
118 buffer_append(&b, ",", 1);
119 buffer_append(&b, devices[i]->name,
120 strlen(devices[i]->name));
Ben Lindstrom551ea372001-06-05 18:56:16 +0000121 }
Damien Miller0e3b8722002-01-22 23:26:38 +1100122 buffer_append(&b, "\0", 1);
123 kbdintctxt->devices = xstrdup(buffer_ptr(&b));
124 buffer_free(&b);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000125 } else {
126 kbdintctxt->devices = xstrdup(devs);
127 }
Damien Miller0e3b8722002-01-22 23:26:38 +1100128 debug("kbdint_alloc: devices '%s'", kbdintctxt->devices);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000129 kbdintctxt->ctxt = NULL;
130 kbdintctxt->device = NULL;
Damien Millerfb7fd952002-06-26 23:58:39 +1000131 kbdintctxt->nreq = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000132
133 return kbdintctxt;
134}
Ben Lindstrombba81212001-06-25 05:01:22 +0000135static void
Ben Lindstrom551ea372001-06-05 18:56:16 +0000136kbdint_reset_device(KbdintAuthctxt *kbdintctxt)
137{
138 if (kbdintctxt->ctxt) {
139 kbdintctxt->device->free_ctx(kbdintctxt->ctxt);
140 kbdintctxt->ctxt = NULL;
141 }
142 kbdintctxt->device = NULL;
143}
Ben Lindstrombba81212001-06-25 05:01:22 +0000144static void
Ben Lindstrom551ea372001-06-05 18:56:16 +0000145kbdint_free(KbdintAuthctxt *kbdintctxt)
146{
147 if (kbdintctxt->device)
148 kbdint_reset_device(kbdintctxt);
149 if (kbdintctxt->devices) {
150 xfree(kbdintctxt->devices);
151 kbdintctxt->devices = NULL;
152 }
153 xfree(kbdintctxt);
154}
155/* get next device */
Ben Lindstrombba81212001-06-25 05:01:22 +0000156static int
Ben Lindstrom551ea372001-06-05 18:56:16 +0000157kbdint_next_device(KbdintAuthctxt *kbdintctxt)
158{
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;
171 for (i = 0; devices[i]; i++)
172 if (strncmp(kbdintctxt->devices, devices[i]->name, len) == 0)
173 kbdintctxt->device = devices[i];
174 t = kbdintctxt->devices;
175 kbdintctxt->devices = t[len] ? xstrdup(t+len+1) : NULL;
176 xfree(t);
177 debug2("kbdint_next_device: devices %s", kbdintctxt->devices ?
Damien Miller0dc1bef2005-07-17 17:22:45 +1000178 kbdintctxt->devices : "<empty>");
Ben Lindstrom551ea372001-06-05 18:56:16 +0000179 } while (kbdintctxt->devices && !kbdintctxt->device);
180
181 return kbdintctxt->device ? 1 : 0;
182}
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000183
184/*
Ben Lindstrombdfb4df2001-10-03 17:12:43 +0000185 * try challenge-response, set authctxt->postponed if we have to
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000186 * wait for the response.
187 */
188int
189auth2_challenge(Authctxt *authctxt, char *devs)
190{
Ben Lindstrom551ea372001-06-05 18:56:16 +0000191 debug("auth2_challenge: user=%s devs=%s",
192 authctxt->user ? authctxt->user : "<nouser>",
193 devs ? devs : "<no devs>");
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000194
Ben Lindstrom742e89e2001-06-09 01:17:23 +0000195 if (authctxt->user == NULL || !devs)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000196 return 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100197 if (authctxt->kbdintctxt == NULL)
Ben Lindstrom551ea372001-06-05 18:56:16 +0000198 authctxt->kbdintctxt = kbdint_alloc(devs);
199 return auth2_challenge_start(authctxt);
200}
201
Damien Milleree116252001-12-21 12:42:34 +1100202/* unregister kbd-int callbacks and context */
203void
204auth2_challenge_stop(Authctxt *authctxt)
205{
206 /* unregister callback */
207 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL);
208 if (authctxt->kbdintctxt != NULL) {
209 kbdint_free(authctxt->kbdintctxt);
210 authctxt->kbdintctxt = NULL;
211 }
212}
213
Ben Lindstrom551ea372001-06-05 18:56:16 +0000214/* side effect: sets authctxt->postponed if a reply was sent*/
215static int
216auth2_challenge_start(Authctxt *authctxt)
217{
218 KbdintAuthctxt *kbdintctxt = authctxt->kbdintctxt;
219
220 debug2("auth2_challenge_start: devices %s",
221 kbdintctxt->devices ? kbdintctxt->devices : "<empty>");
222
223 if (kbdint_next_device(kbdintctxt) == 0) {
Damien Milleree116252001-12-21 12:42:34 +1100224 auth2_challenge_stop(authctxt);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000225 return 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000226 }
227 debug("auth2_challenge_start: trying authentication method '%s'",
228 kbdintctxt->device->name);
229
230 if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) {
Damien Milleree116252001-12-21 12:42:34 +1100231 auth2_challenge_stop(authctxt);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000232 return 0;
233 }
234 if (send_userauth_info_request(authctxt) == 0) {
Damien Milleree116252001-12-21 12:42:34 +1100235 auth2_challenge_stop(authctxt);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000236 return 0;
237 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000238 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE,
239 &input_userauth_info_response);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000240
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000241 authctxt->postponed = 1;
242 return 0;
243}
244
Ben Lindstrom551ea372001-06-05 18:56:16 +0000245static int
246send_userauth_info_request(Authctxt *authctxt)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000247{
Ben Lindstrom551ea372001-06-05 18:56:16 +0000248 KbdintAuthctxt *kbdintctxt;
249 char *name, *instr, **prompts;
Damien Millereccb9de2005-06-17 12:59:34 +1000250 u_int i, *echo_on;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000251
252 kbdintctxt = authctxt->kbdintctxt;
253 if (kbdintctxt->device->query(kbdintctxt->ctxt,
Damien Millerfb7fd952002-06-26 23:58:39 +1000254 &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on))
Ben Lindstrom551ea372001-06-05 18:56:16 +0000255 return 0;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000256
257 packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000258 packet_put_cstring(name);
259 packet_put_cstring(instr);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000260 packet_put_cstring(""); /* language not used */
Damien Millerfb7fd952002-06-26 23:58:39 +1000261 packet_put_int(kbdintctxt->nreq);
262 for (i = 0; i < kbdintctxt->nreq; i++) {
Ben Lindstrom551ea372001-06-05 18:56:16 +0000263 packet_put_cstring(prompts[i]);
264 packet_put_char(echo_on[i]);
265 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000266 packet_send();
267 packet_write_wait();
Ben Lindstrom551ea372001-06-05 18:56:16 +0000268
Damien Millerfb7fd952002-06-26 23:58:39 +1000269 for (i = 0; i < kbdintctxt->nreq; i++)
Ben Lindstrom551ea372001-06-05 18:56:16 +0000270 xfree(prompts[i]);
271 xfree(prompts);
272 xfree(echo_on);
273 xfree(name);
274 xfree(instr);
275 return 1;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000276}
277
Ben Lindstrom551ea372001-06-05 18:56:16 +0000278static void
Damien Miller630d6f42002-01-22 23:17:30 +1100279input_userauth_info_response(int type, u_int32_t seq, void *ctxt)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000280{
281 Authctxt *authctxt = ctxt;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000282 KbdintAuthctxt *kbdintctxt;
Damien Millereccb9de2005-06-17 12:59:34 +1000283 int authenticated = 0, res, len;
284 u_int i, nresp;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000285 char **response = NULL, *method;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000286
287 if (authctxt == NULL)
288 fatal("input_userauth_info_response: no authctxt");
Ben Lindstrom551ea372001-06-05 18:56:16 +0000289 kbdintctxt = authctxt->kbdintctxt;
290 if (kbdintctxt == NULL || kbdintctxt->ctxt == NULL)
291 fatal("input_userauth_info_response: no kbdintctxt");
292 if (kbdintctxt->device == NULL)
293 fatal("input_userauth_info_response: no device");
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000294
295 authctxt->postponed = 0; /* reset */
296 nresp = packet_get_int();
Damien Millerfb7fd952002-06-26 23:58:39 +1000297 if (nresp != kbdintctxt->nreq)
298 fatal("input_userauth_info_response: wrong number of replies");
299 if (nresp > 100)
300 fatal("input_userauth_info_response: too many replies");
Ben Lindstrom551ea372001-06-05 18:56:16 +0000301 if (nresp > 0) {
Damien Miller07d86be2006-03-26 14:19:21 +1100302 response = xcalloc(nresp, sizeof(char *));
Ben Lindstrom551ea372001-06-05 18:56:16 +0000303 for (i = 0; i < nresp; i++)
304 response[i] = packet_get_string(NULL);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000305 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100306 packet_check_eom();
Ben Lindstrom551ea372001-06-05 18:56:16 +0000307
Darren Tucker611649e2005-01-20 11:05:34 +1100308 res = kbdintctxt->device->respond(kbdintctxt->ctxt, nresp, response);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000309
310 for (i = 0; i < nresp; i++) {
311 memset(response[i], 'r', strlen(response[i]));
312 xfree(response[i]);
313 }
314 if (response)
315 xfree(response);
316
317 switch (res) {
318 case 0:
319 /* Success! */
Darren Tucker611649e2005-01-20 11:05:34 +1100320 authenticated = authctxt->valid ? 1 : 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000321 break;
322 case 1:
323 /* Authentication needs further interaction */
Damien Milleree116252001-12-21 12:42:34 +1100324 if (send_userauth_info_request(authctxt) == 1)
325 authctxt->postponed = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000326 break;
327 default:
328 /* Failure! */
329 break;
330 }
331
332 len = strlen("keyboard-interactive") + 2 +
333 strlen(kbdintctxt->device->name);
334 method = xmalloc(len);
Damien Miller209ee4e2002-01-22 23:25:08 +1100335 snprintf(method, len, "keyboard-interactive/%s",
336 kbdintctxt->device->name);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000337
338 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 Miller5d57e502001-03-30 10:48:31 +1000347 userauth_finish(authctxt, authenticated, method);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000348 xfree(method);
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}