blob: ad6b7233fe9309796427666b8a1293ee199c40bd [file] [log] [blame]
Damien Millere3476ed2006-07-24 14:13:33 +10001/* $OpenBSD: auth2-chall.c,v 1.28 2006/07/22 20:48:22 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 Millere3476ed2006-07-24 14:13:33 +100028#include <string.h>
29
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000030#include "ssh2.h"
31#include "auth.h"
Damien Miller0e3b8722002-01-22 23:26:38 +110032#include "buffer.h"
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000033#include "packet.h"
34#include "xmalloc.h"
35#include "dispatch.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000036#include "log.h"
Darren Tucker3c660802005-01-20 22:20:50 +110037#include "servconf.h"
38
39/* import */
40extern ServerOptions options;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000041
Ben Lindstrombba81212001-06-25 05:01:22 +000042static int auth2_challenge_start(Authctxt *);
43static int send_userauth_info_request(Authctxt *);
Damien Miller630d6f42002-01-22 23:17:30 +110044static void input_userauth_info_response(int, u_int32_t, void *);
Ben Lindstrom551ea372001-06-05 18:56:16 +000045
46#ifdef BSD_AUTH
47extern KbdintDevice bsdauth_device;
48#else
Damien Miller4f9f42a2003-05-10 19:28:02 +100049#ifdef USE_PAM
50extern KbdintDevice sshpam_device;
51#endif
Ben Lindstrom551ea372001-06-05 18:56:16 +000052#ifdef SKEY
53extern KbdintDevice skey_device;
54#endif
55#endif
56
57KbdintDevice *devices[] = {
58#ifdef BSD_AUTH
59 &bsdauth_device,
60#else
Damien Miller4f9f42a2003-05-10 19:28:02 +100061#ifdef USE_PAM
62 &sshpam_device,
63#endif
Ben Lindstrom551ea372001-06-05 18:56:16 +000064#ifdef SKEY
65 &skey_device,
66#endif
67#endif
68 NULL
69};
70
71typedef struct KbdintAuthctxt KbdintAuthctxt;
72struct KbdintAuthctxt
73{
74 char *devices;
75 void *ctxt;
76 KbdintDevice *device;
Damien Millerfb7fd952002-06-26 23:58:39 +100077 u_int nreq;
Ben Lindstrom551ea372001-06-05 18:56:16 +000078};
79
Darren Tucker3c660802005-01-20 22:20:50 +110080#ifdef USE_PAM
81void
82remove_kbdint_device(const char *devname)
83{
84 int i, j;
85
86 for (i = 0; devices[i] != NULL; i++)
87 if (strcmp(devices[i]->name, devname) == 0) {
88 for (j = i; devices[j] != NULL; j++)
89 devices[j] = devices[j+1];
90 i--;
91 }
92}
93#endif
94
Ben Lindstrombba81212001-06-25 05:01:22 +000095static KbdintAuthctxt *
Ben Lindstrom551ea372001-06-05 18:56:16 +000096kbdint_alloc(const char *devs)
97{
98 KbdintAuthctxt *kbdintctxt;
Damien Miller0e3b8722002-01-22 23:26:38 +110099 Buffer b;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000100 int i;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000101
Darren Tucker3c660802005-01-20 22:20:50 +1100102#ifdef USE_PAM
103 if (!options.use_pam)
104 remove_kbdint_device("pam");
105#endif
106
Ben Lindstrom551ea372001-06-05 18:56:16 +0000107 kbdintctxt = xmalloc(sizeof(KbdintAuthctxt));
108 if (strcmp(devs, "") == 0) {
Damien Miller0e3b8722002-01-22 23:26:38 +1100109 buffer_init(&b);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000110 for (i = 0; devices[i]; i++) {
Damien Miller0e3b8722002-01-22 23:26:38 +1100111 if (buffer_len(&b) > 0)
112 buffer_append(&b, ",", 1);
113 buffer_append(&b, devices[i]->name,
114 strlen(devices[i]->name));
Ben Lindstrom551ea372001-06-05 18:56:16 +0000115 }
Damien Miller0e3b8722002-01-22 23:26:38 +1100116 buffer_append(&b, "\0", 1);
117 kbdintctxt->devices = xstrdup(buffer_ptr(&b));
118 buffer_free(&b);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000119 } else {
120 kbdintctxt->devices = xstrdup(devs);
121 }
Damien Miller0e3b8722002-01-22 23:26:38 +1100122 debug("kbdint_alloc: devices '%s'", kbdintctxt->devices);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000123 kbdintctxt->ctxt = NULL;
124 kbdintctxt->device = NULL;
Damien Millerfb7fd952002-06-26 23:58:39 +1000125 kbdintctxt->nreq = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000126
127 return kbdintctxt;
128}
Ben Lindstrombba81212001-06-25 05:01:22 +0000129static void
Ben Lindstrom551ea372001-06-05 18:56:16 +0000130kbdint_reset_device(KbdintAuthctxt *kbdintctxt)
131{
132 if (kbdintctxt->ctxt) {
133 kbdintctxt->device->free_ctx(kbdintctxt->ctxt);
134 kbdintctxt->ctxt = NULL;
135 }
136 kbdintctxt->device = NULL;
137}
Ben Lindstrombba81212001-06-25 05:01:22 +0000138static void
Ben Lindstrom551ea372001-06-05 18:56:16 +0000139kbdint_free(KbdintAuthctxt *kbdintctxt)
140{
141 if (kbdintctxt->device)
142 kbdint_reset_device(kbdintctxt);
143 if (kbdintctxt->devices) {
144 xfree(kbdintctxt->devices);
145 kbdintctxt->devices = NULL;
146 }
147 xfree(kbdintctxt);
148}
149/* get next device */
Ben Lindstrombba81212001-06-25 05:01:22 +0000150static int
Ben Lindstrom551ea372001-06-05 18:56:16 +0000151kbdint_next_device(KbdintAuthctxt *kbdintctxt)
152{
153 size_t len;
154 char *t;
155 int i;
156
157 if (kbdintctxt->device)
158 kbdint_reset_device(kbdintctxt);
159 do {
160 len = kbdintctxt->devices ?
161 strcspn(kbdintctxt->devices, ",") : 0;
162
163 if (len == 0)
164 break;
165 for (i = 0; devices[i]; i++)
166 if (strncmp(kbdintctxt->devices, devices[i]->name, len) == 0)
167 kbdintctxt->device = devices[i];
168 t = kbdintctxt->devices;
169 kbdintctxt->devices = t[len] ? xstrdup(t+len+1) : NULL;
170 xfree(t);
171 debug2("kbdint_next_device: devices %s", kbdintctxt->devices ?
Damien Miller0dc1bef2005-07-17 17:22:45 +1000172 kbdintctxt->devices : "<empty>");
Ben Lindstrom551ea372001-06-05 18:56:16 +0000173 } while (kbdintctxt->devices && !kbdintctxt->device);
174
175 return kbdintctxt->device ? 1 : 0;
176}
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000177
178/*
Ben Lindstrombdfb4df2001-10-03 17:12:43 +0000179 * try challenge-response, set authctxt->postponed if we have to
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000180 * wait for the response.
181 */
182int
183auth2_challenge(Authctxt *authctxt, char *devs)
184{
Ben Lindstrom551ea372001-06-05 18:56:16 +0000185 debug("auth2_challenge: user=%s devs=%s",
186 authctxt->user ? authctxt->user : "<nouser>",
187 devs ? devs : "<no devs>");
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000188
Ben Lindstrom742e89e2001-06-09 01:17:23 +0000189 if (authctxt->user == NULL || !devs)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000190 return 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100191 if (authctxt->kbdintctxt == NULL)
Ben Lindstrom551ea372001-06-05 18:56:16 +0000192 authctxt->kbdintctxt = kbdint_alloc(devs);
193 return auth2_challenge_start(authctxt);
194}
195
Damien Milleree116252001-12-21 12:42:34 +1100196/* unregister kbd-int callbacks and context */
197void
198auth2_challenge_stop(Authctxt *authctxt)
199{
200 /* unregister callback */
201 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL);
202 if (authctxt->kbdintctxt != NULL) {
203 kbdint_free(authctxt->kbdintctxt);
204 authctxt->kbdintctxt = NULL;
205 }
206}
207
Ben Lindstrom551ea372001-06-05 18:56:16 +0000208/* side effect: sets authctxt->postponed if a reply was sent*/
209static int
210auth2_challenge_start(Authctxt *authctxt)
211{
212 KbdintAuthctxt *kbdintctxt = authctxt->kbdintctxt;
213
214 debug2("auth2_challenge_start: devices %s",
215 kbdintctxt->devices ? kbdintctxt->devices : "<empty>");
216
217 if (kbdint_next_device(kbdintctxt) == 0) {
Damien Milleree116252001-12-21 12:42:34 +1100218 auth2_challenge_stop(authctxt);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000219 return 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000220 }
221 debug("auth2_challenge_start: trying authentication method '%s'",
222 kbdintctxt->device->name);
223
224 if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) {
Damien Milleree116252001-12-21 12:42:34 +1100225 auth2_challenge_stop(authctxt);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000226 return 0;
227 }
228 if (send_userauth_info_request(authctxt) == 0) {
Damien Milleree116252001-12-21 12:42:34 +1100229 auth2_challenge_stop(authctxt);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000230 return 0;
231 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000232 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE,
233 &input_userauth_info_response);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000234
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000235 authctxt->postponed = 1;
236 return 0;
237}
238
Ben Lindstrom551ea372001-06-05 18:56:16 +0000239static int
240send_userauth_info_request(Authctxt *authctxt)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000241{
Ben Lindstrom551ea372001-06-05 18:56:16 +0000242 KbdintAuthctxt *kbdintctxt;
243 char *name, *instr, **prompts;
Damien Millereccb9de2005-06-17 12:59:34 +1000244 u_int i, *echo_on;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000245
246 kbdintctxt = authctxt->kbdintctxt;
247 if (kbdintctxt->device->query(kbdintctxt->ctxt,
Damien Millerfb7fd952002-06-26 23:58:39 +1000248 &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on))
Ben Lindstrom551ea372001-06-05 18:56:16 +0000249 return 0;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000250
251 packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000252 packet_put_cstring(name);
253 packet_put_cstring(instr);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000254 packet_put_cstring(""); /* language not used */
Damien Millerfb7fd952002-06-26 23:58:39 +1000255 packet_put_int(kbdintctxt->nreq);
256 for (i = 0; i < kbdintctxt->nreq; i++) {
Ben Lindstrom551ea372001-06-05 18:56:16 +0000257 packet_put_cstring(prompts[i]);
258 packet_put_char(echo_on[i]);
259 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000260 packet_send();
261 packet_write_wait();
Ben Lindstrom551ea372001-06-05 18:56:16 +0000262
Damien Millerfb7fd952002-06-26 23:58:39 +1000263 for (i = 0; i < kbdintctxt->nreq; i++)
Ben Lindstrom551ea372001-06-05 18:56:16 +0000264 xfree(prompts[i]);
265 xfree(prompts);
266 xfree(echo_on);
267 xfree(name);
268 xfree(instr);
269 return 1;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000270}
271
Ben Lindstrom551ea372001-06-05 18:56:16 +0000272static void
Damien Miller630d6f42002-01-22 23:17:30 +1100273input_userauth_info_response(int type, u_int32_t seq, void *ctxt)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000274{
275 Authctxt *authctxt = ctxt;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000276 KbdintAuthctxt *kbdintctxt;
Damien Millereccb9de2005-06-17 12:59:34 +1000277 int authenticated = 0, res, len;
278 u_int i, nresp;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000279 char **response = NULL, *method;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000280
281 if (authctxt == NULL)
282 fatal("input_userauth_info_response: no authctxt");
Ben Lindstrom551ea372001-06-05 18:56:16 +0000283 kbdintctxt = authctxt->kbdintctxt;
284 if (kbdintctxt == NULL || kbdintctxt->ctxt == NULL)
285 fatal("input_userauth_info_response: no kbdintctxt");
286 if (kbdintctxt->device == NULL)
287 fatal("input_userauth_info_response: no device");
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000288
289 authctxt->postponed = 0; /* reset */
290 nresp = packet_get_int();
Damien Millerfb7fd952002-06-26 23:58:39 +1000291 if (nresp != kbdintctxt->nreq)
292 fatal("input_userauth_info_response: wrong number of replies");
293 if (nresp > 100)
294 fatal("input_userauth_info_response: too many replies");
Ben Lindstrom551ea372001-06-05 18:56:16 +0000295 if (nresp > 0) {
Damien Miller07d86be2006-03-26 14:19:21 +1100296 response = xcalloc(nresp, sizeof(char *));
Ben Lindstrom551ea372001-06-05 18:56:16 +0000297 for (i = 0; i < nresp; i++)
298 response[i] = packet_get_string(NULL);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000299 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100300 packet_check_eom();
Ben Lindstrom551ea372001-06-05 18:56:16 +0000301
Darren Tucker611649e2005-01-20 11:05:34 +1100302 res = kbdintctxt->device->respond(kbdintctxt->ctxt, nresp, response);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000303
304 for (i = 0; i < nresp; i++) {
305 memset(response[i], 'r', strlen(response[i]));
306 xfree(response[i]);
307 }
308 if (response)
309 xfree(response);
310
311 switch (res) {
312 case 0:
313 /* Success! */
Darren Tucker611649e2005-01-20 11:05:34 +1100314 authenticated = authctxt->valid ? 1 : 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000315 break;
316 case 1:
317 /* Authentication needs further interaction */
Damien Milleree116252001-12-21 12:42:34 +1100318 if (send_userauth_info_request(authctxt) == 1)
319 authctxt->postponed = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000320 break;
321 default:
322 /* Failure! */
323 break;
324 }
325
326 len = strlen("keyboard-interactive") + 2 +
327 strlen(kbdintctxt->device->name);
328 method = xmalloc(len);
Damien Miller209ee4e2002-01-22 23:25:08 +1100329 snprintf(method, len, "keyboard-interactive/%s",
330 kbdintctxt->device->name);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000331
332 if (!authctxt->postponed) {
Ben Lindstrom551ea372001-06-05 18:56:16 +0000333 if (authenticated) {
Damien Milleree116252001-12-21 12:42:34 +1100334 auth2_challenge_stop(authctxt);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000335 } else {
336 /* start next device */
337 /* may set authctxt->postponed */
338 auth2_challenge_start(authctxt);
339 }
340 }
Damien Miller5d57e502001-03-30 10:48:31 +1000341 userauth_finish(authctxt, authenticated, method);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000342 xfree(method);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000343}
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000344
345void
346privsep_challenge_enable(void)
347{
Damien Millera6a7c192003-05-26 21:36:13 +1000348#if defined(BSD_AUTH) || defined(USE_PAM) || defined(SKEY)
349 int n = 0;
350#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000351#ifdef BSD_AUTH
352 extern KbdintDevice mm_bsdauth_device;
353#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000354#ifdef USE_PAM
355 extern KbdintDevice mm_sshpam_device;
356#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000357#ifdef SKEY
358 extern KbdintDevice mm_skey_device;
359#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000360
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000361#ifdef BSD_AUTH
Damien Miller4f9f42a2003-05-10 19:28:02 +1000362 devices[n++] = &mm_bsdauth_device;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000363#else
Damien Miller4f9f42a2003-05-10 19:28:02 +1000364#ifdef USE_PAM
365 devices[n++] = &mm_sshpam_device;
366#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000367#ifdef SKEY
Damien Miller4f9f42a2003-05-10 19:28:02 +1000368 devices[n++] = &mm_skey_device;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000369#endif
370#endif
371}