blob: 89b261e61f7a1ce10da777998b9e7095cd3298df [file] [log] [blame]
Damien Millera7a73ee2006-08-05 11:37:59 +10001/* $OpenBSD: auth2-chall.c,v 1.29 2006/08/01 23:22:47 stevesk 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 */
26#include "includes.h"
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000027
Damien Millera7a73ee2006-08-05 11:37:59 +100028#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100029#include <string.h>
30
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000031#include "ssh2.h"
32#include "auth.h"
Damien Miller0e3b8722002-01-22 23:26:38 +110033#include "buffer.h"
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000034#include "packet.h"
35#include "xmalloc.h"
36#include "dispatch.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000037#include "log.h"
Darren Tucker3c660802005-01-20 22:20:50 +110038#include "servconf.h"
39
40/* import */
41extern ServerOptions options;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000042
Ben Lindstrombba81212001-06-25 05:01:22 +000043static int auth2_challenge_start(Authctxt *);
44static int send_userauth_info_request(Authctxt *);
Damien Miller630d6f42002-01-22 23:17:30 +110045static void input_userauth_info_response(int, u_int32_t, void *);
Ben Lindstrom551ea372001-06-05 18:56:16 +000046
47#ifdef BSD_AUTH
48extern KbdintDevice bsdauth_device;
49#else
Damien Miller4f9f42a2003-05-10 19:28:02 +100050#ifdef USE_PAM
51extern KbdintDevice sshpam_device;
52#endif
Ben Lindstrom551ea372001-06-05 18:56:16 +000053#ifdef SKEY
54extern KbdintDevice skey_device;
55#endif
56#endif
57
58KbdintDevice *devices[] = {
59#ifdef BSD_AUTH
60 &bsdauth_device,
61#else
Damien Miller4f9f42a2003-05-10 19:28:02 +100062#ifdef USE_PAM
63 &sshpam_device,
64#endif
Ben Lindstrom551ea372001-06-05 18:56:16 +000065#ifdef SKEY
66 &skey_device,
67#endif
68#endif
69 NULL
70};
71
72typedef struct KbdintAuthctxt KbdintAuthctxt;
73struct KbdintAuthctxt
74{
75 char *devices;
76 void *ctxt;
77 KbdintDevice *device;
Damien Millerfb7fd952002-06-26 23:58:39 +100078 u_int nreq;
Ben Lindstrom551ea372001-06-05 18:56:16 +000079};
80
Darren Tucker3c660802005-01-20 22:20:50 +110081#ifdef USE_PAM
82void
83remove_kbdint_device(const char *devname)
84{
85 int i, j;
86
87 for (i = 0; devices[i] != NULL; i++)
88 if (strcmp(devices[i]->name, devname) == 0) {
89 for (j = i; devices[j] != NULL; j++)
90 devices[j] = devices[j+1];
91 i--;
92 }
93}
94#endif
95
Ben Lindstrombba81212001-06-25 05:01:22 +000096static KbdintAuthctxt *
Ben Lindstrom551ea372001-06-05 18:56:16 +000097kbdint_alloc(const char *devs)
98{
99 KbdintAuthctxt *kbdintctxt;
Damien Miller0e3b8722002-01-22 23:26:38 +1100100 Buffer b;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000101 int i;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000102
Darren Tucker3c660802005-01-20 22:20:50 +1100103#ifdef USE_PAM
104 if (!options.use_pam)
105 remove_kbdint_device("pam");
106#endif
107
Ben Lindstrom551ea372001-06-05 18:56:16 +0000108 kbdintctxt = xmalloc(sizeof(KbdintAuthctxt));
109 if (strcmp(devs, "") == 0) {
Damien Miller0e3b8722002-01-22 23:26:38 +1100110 buffer_init(&b);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000111 for (i = 0; devices[i]; i++) {
Damien Miller0e3b8722002-01-22 23:26:38 +1100112 if (buffer_len(&b) > 0)
113 buffer_append(&b, ",", 1);
114 buffer_append(&b, devices[i]->name,
115 strlen(devices[i]->name));
Ben Lindstrom551ea372001-06-05 18:56:16 +0000116 }
Damien Miller0e3b8722002-01-22 23:26:38 +1100117 buffer_append(&b, "\0", 1);
118 kbdintctxt->devices = xstrdup(buffer_ptr(&b));
119 buffer_free(&b);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000120 } else {
121 kbdintctxt->devices = xstrdup(devs);
122 }
Damien Miller0e3b8722002-01-22 23:26:38 +1100123 debug("kbdint_alloc: devices '%s'", kbdintctxt->devices);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000124 kbdintctxt->ctxt = NULL;
125 kbdintctxt->device = NULL;
Damien Millerfb7fd952002-06-26 23:58:39 +1000126 kbdintctxt->nreq = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000127
128 return kbdintctxt;
129}
Ben Lindstrombba81212001-06-25 05:01:22 +0000130static void
Ben Lindstrom551ea372001-06-05 18:56:16 +0000131kbdint_reset_device(KbdintAuthctxt *kbdintctxt)
132{
133 if (kbdintctxt->ctxt) {
134 kbdintctxt->device->free_ctx(kbdintctxt->ctxt);
135 kbdintctxt->ctxt = NULL;
136 }
137 kbdintctxt->device = NULL;
138}
Ben Lindstrombba81212001-06-25 05:01:22 +0000139static void
Ben Lindstrom551ea372001-06-05 18:56:16 +0000140kbdint_free(KbdintAuthctxt *kbdintctxt)
141{
142 if (kbdintctxt->device)
143 kbdint_reset_device(kbdintctxt);
144 if (kbdintctxt->devices) {
145 xfree(kbdintctxt->devices);
146 kbdintctxt->devices = NULL;
147 }
148 xfree(kbdintctxt);
149}
150/* get next device */
Ben Lindstrombba81212001-06-25 05:01:22 +0000151static int
Ben Lindstrom551ea372001-06-05 18:56:16 +0000152kbdint_next_device(KbdintAuthctxt *kbdintctxt)
153{
154 size_t len;
155 char *t;
156 int i;
157
158 if (kbdintctxt->device)
159 kbdint_reset_device(kbdintctxt);
160 do {
161 len = kbdintctxt->devices ?
162 strcspn(kbdintctxt->devices, ",") : 0;
163
164 if (len == 0)
165 break;
166 for (i = 0; devices[i]; i++)
167 if (strncmp(kbdintctxt->devices, devices[i]->name, len) == 0)
168 kbdintctxt->device = devices[i];
169 t = kbdintctxt->devices;
170 kbdintctxt->devices = t[len] ? xstrdup(t+len+1) : NULL;
171 xfree(t);
172 debug2("kbdint_next_device: devices %s", kbdintctxt->devices ?
Damien Miller0dc1bef2005-07-17 17:22:45 +1000173 kbdintctxt->devices : "<empty>");
Ben Lindstrom551ea372001-06-05 18:56:16 +0000174 } while (kbdintctxt->devices && !kbdintctxt->device);
175
176 return kbdintctxt->device ? 1 : 0;
177}
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000178
179/*
Ben Lindstrombdfb4df2001-10-03 17:12:43 +0000180 * try challenge-response, set authctxt->postponed if we have to
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000181 * wait for the response.
182 */
183int
184auth2_challenge(Authctxt *authctxt, char *devs)
185{
Ben Lindstrom551ea372001-06-05 18:56:16 +0000186 debug("auth2_challenge: user=%s devs=%s",
187 authctxt->user ? authctxt->user : "<nouser>",
188 devs ? devs : "<no devs>");
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000189
Ben Lindstrom742e89e2001-06-09 01:17:23 +0000190 if (authctxt->user == NULL || !devs)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000191 return 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100192 if (authctxt->kbdintctxt == NULL)
Ben Lindstrom551ea372001-06-05 18:56:16 +0000193 authctxt->kbdintctxt = kbdint_alloc(devs);
194 return auth2_challenge_start(authctxt);
195}
196
Damien Milleree116252001-12-21 12:42:34 +1100197/* unregister kbd-int callbacks and context */
198void
199auth2_challenge_stop(Authctxt *authctxt)
200{
201 /* unregister callback */
202 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL);
203 if (authctxt->kbdintctxt != NULL) {
204 kbdint_free(authctxt->kbdintctxt);
205 authctxt->kbdintctxt = NULL;
206 }
207}
208
Ben Lindstrom551ea372001-06-05 18:56:16 +0000209/* side effect: sets authctxt->postponed if a reply was sent*/
210static int
211auth2_challenge_start(Authctxt *authctxt)
212{
213 KbdintAuthctxt *kbdintctxt = authctxt->kbdintctxt;
214
215 debug2("auth2_challenge_start: devices %s",
216 kbdintctxt->devices ? kbdintctxt->devices : "<empty>");
217
218 if (kbdint_next_device(kbdintctxt) == 0) {
Damien Milleree116252001-12-21 12:42:34 +1100219 auth2_challenge_stop(authctxt);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000220 return 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000221 }
222 debug("auth2_challenge_start: trying authentication method '%s'",
223 kbdintctxt->device->name);
224
225 if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) {
Damien Milleree116252001-12-21 12:42:34 +1100226 auth2_challenge_stop(authctxt);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000227 return 0;
228 }
229 if (send_userauth_info_request(authctxt) == 0) {
Damien Milleree116252001-12-21 12:42:34 +1100230 auth2_challenge_stop(authctxt);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000231 return 0;
232 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000233 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE,
234 &input_userauth_info_response);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000235
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000236 authctxt->postponed = 1;
237 return 0;
238}
239
Ben Lindstrom551ea372001-06-05 18:56:16 +0000240static int
241send_userauth_info_request(Authctxt *authctxt)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000242{
Ben Lindstrom551ea372001-06-05 18:56:16 +0000243 KbdintAuthctxt *kbdintctxt;
244 char *name, *instr, **prompts;
Damien Millereccb9de2005-06-17 12:59:34 +1000245 u_int i, *echo_on;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000246
247 kbdintctxt = authctxt->kbdintctxt;
248 if (kbdintctxt->device->query(kbdintctxt->ctxt,
Damien Millerfb7fd952002-06-26 23:58:39 +1000249 &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on))
Ben Lindstrom551ea372001-06-05 18:56:16 +0000250 return 0;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000251
252 packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000253 packet_put_cstring(name);
254 packet_put_cstring(instr);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000255 packet_put_cstring(""); /* language not used */
Damien Millerfb7fd952002-06-26 23:58:39 +1000256 packet_put_int(kbdintctxt->nreq);
257 for (i = 0; i < kbdintctxt->nreq; i++) {
Ben Lindstrom551ea372001-06-05 18:56:16 +0000258 packet_put_cstring(prompts[i]);
259 packet_put_char(echo_on[i]);
260 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000261 packet_send();
262 packet_write_wait();
Ben Lindstrom551ea372001-06-05 18:56:16 +0000263
Damien Millerfb7fd952002-06-26 23:58:39 +1000264 for (i = 0; i < kbdintctxt->nreq; i++)
Ben Lindstrom551ea372001-06-05 18:56:16 +0000265 xfree(prompts[i]);
266 xfree(prompts);
267 xfree(echo_on);
268 xfree(name);
269 xfree(instr);
270 return 1;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000271}
272
Ben Lindstrom551ea372001-06-05 18:56:16 +0000273static void
Damien Miller630d6f42002-01-22 23:17:30 +1100274input_userauth_info_response(int type, u_int32_t seq, void *ctxt)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000275{
276 Authctxt *authctxt = ctxt;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000277 KbdintAuthctxt *kbdintctxt;
Damien Millereccb9de2005-06-17 12:59:34 +1000278 int authenticated = 0, res, len;
279 u_int i, nresp;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000280 char **response = NULL, *method;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000281
282 if (authctxt == NULL)
283 fatal("input_userauth_info_response: no authctxt");
Ben Lindstrom551ea372001-06-05 18:56:16 +0000284 kbdintctxt = authctxt->kbdintctxt;
285 if (kbdintctxt == NULL || kbdintctxt->ctxt == NULL)
286 fatal("input_userauth_info_response: no kbdintctxt");
287 if (kbdintctxt->device == NULL)
288 fatal("input_userauth_info_response: no device");
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000289
290 authctxt->postponed = 0; /* reset */
291 nresp = packet_get_int();
Damien Millerfb7fd952002-06-26 23:58:39 +1000292 if (nresp != kbdintctxt->nreq)
293 fatal("input_userauth_info_response: wrong number of replies");
294 if (nresp > 100)
295 fatal("input_userauth_info_response: too many replies");
Ben Lindstrom551ea372001-06-05 18:56:16 +0000296 if (nresp > 0) {
Damien Miller07d86be2006-03-26 14:19:21 +1100297 response = xcalloc(nresp, sizeof(char *));
Ben Lindstrom551ea372001-06-05 18:56:16 +0000298 for (i = 0; i < nresp; i++)
299 response[i] = packet_get_string(NULL);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000300 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100301 packet_check_eom();
Ben Lindstrom551ea372001-06-05 18:56:16 +0000302
Darren Tucker611649e2005-01-20 11:05:34 +1100303 res = kbdintctxt->device->respond(kbdintctxt->ctxt, nresp, response);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000304
305 for (i = 0; i < nresp; i++) {
306 memset(response[i], 'r', strlen(response[i]));
307 xfree(response[i]);
308 }
309 if (response)
310 xfree(response);
311
312 switch (res) {
313 case 0:
314 /* Success! */
Darren Tucker611649e2005-01-20 11:05:34 +1100315 authenticated = authctxt->valid ? 1 : 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000316 break;
317 case 1:
318 /* Authentication needs further interaction */
Damien Milleree116252001-12-21 12:42:34 +1100319 if (send_userauth_info_request(authctxt) == 1)
320 authctxt->postponed = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000321 break;
322 default:
323 /* Failure! */
324 break;
325 }
326
327 len = strlen("keyboard-interactive") + 2 +
328 strlen(kbdintctxt->device->name);
329 method = xmalloc(len);
Damien Miller209ee4e2002-01-22 23:25:08 +1100330 snprintf(method, len, "keyboard-interactive/%s",
331 kbdintctxt->device->name);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000332
333 if (!authctxt->postponed) {
Ben Lindstrom551ea372001-06-05 18:56:16 +0000334 if (authenticated) {
Damien Milleree116252001-12-21 12:42:34 +1100335 auth2_challenge_stop(authctxt);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000336 } else {
337 /* start next device */
338 /* may set authctxt->postponed */
339 auth2_challenge_start(authctxt);
340 }
341 }
Damien Miller5d57e502001-03-30 10:48:31 +1000342 userauth_finish(authctxt, authenticated, method);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000343 xfree(method);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000344}
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000345
346void
347privsep_challenge_enable(void)
348{
Damien Millera6a7c192003-05-26 21:36:13 +1000349#if defined(BSD_AUTH) || defined(USE_PAM) || defined(SKEY)
350 int n = 0;
351#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000352#ifdef BSD_AUTH
353 extern KbdintDevice mm_bsdauth_device;
354#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000355#ifdef USE_PAM
356 extern KbdintDevice mm_sshpam_device;
357#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000358#ifdef SKEY
359 extern KbdintDevice mm_skey_device;
360#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000361
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000362#ifdef BSD_AUTH
Damien Miller4f9f42a2003-05-10 19:28:02 +1000363 devices[n++] = &mm_bsdauth_device;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000364#else
Damien Miller4f9f42a2003-05-10 19:28:02 +1000365#ifdef USE_PAM
366 devices[n++] = &mm_sshpam_device;
367#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000368#ifdef SKEY
Damien Miller4f9f42a2003-05-10 19:28:02 +1000369 devices[n++] = &mm_skey_device;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000370#endif
371#endif
372}