blob: 0d170930795e43fca61570c323f707d3c6ed4b10 [file] [log] [blame]
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001/*
Ben Lindstrom92a2e382001-03-05 06:59:27 +00002 * Copyright (c) 2001 Markus Friedl. All rights reserved.
Ben Lindstrom551ea372001-06-05 18:56:16 +00003 * Copyright (c) 2001 Per Allansson. All rights reserved.
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25#include "includes.h"
Ben Lindstroma962c2f2002-07-04 00:14:17 +000026RCSID("$OpenBSD: auth2-chall.c,v 1.20 2002/06/30 21:59:45 deraadt Exp $");
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000027
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000028#include "ssh2.h"
29#include "auth.h"
Damien Miller0e3b8722002-01-22 23:26:38 +110030#include "buffer.h"
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000031#include "packet.h"
32#include "xmalloc.h"
33#include "dispatch.h"
Ben Lindstrom551ea372001-06-05 18:56:16 +000034#include "auth.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000035#include "log.h"
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000036
Ben Lindstrombba81212001-06-25 05:01:22 +000037static int auth2_challenge_start(Authctxt *);
38static int send_userauth_info_request(Authctxt *);
Damien Miller630d6f42002-01-22 23:17:30 +110039static void input_userauth_info_response(int, u_int32_t, void *);
Ben Lindstrom551ea372001-06-05 18:56:16 +000040
41#ifdef BSD_AUTH
42extern KbdintDevice bsdauth_device;
43#else
44#ifdef SKEY
45extern KbdintDevice skey_device;
46#endif
47#endif
48
49KbdintDevice *devices[] = {
50#ifdef BSD_AUTH
51 &bsdauth_device,
52#else
53#ifdef SKEY
54 &skey_device,
55#endif
56#endif
57 NULL
58};
59
60typedef struct KbdintAuthctxt KbdintAuthctxt;
61struct KbdintAuthctxt
62{
63 char *devices;
64 void *ctxt;
65 KbdintDevice *device;
Damien Millerfb7fd952002-06-26 23:58:39 +100066 u_int nreq;
Ben Lindstrom551ea372001-06-05 18:56:16 +000067};
68
Ben Lindstrombba81212001-06-25 05:01:22 +000069static KbdintAuthctxt *
Ben Lindstrom551ea372001-06-05 18:56:16 +000070kbdint_alloc(const char *devs)
71{
72 KbdintAuthctxt *kbdintctxt;
Damien Miller0e3b8722002-01-22 23:26:38 +110073 Buffer b;
Ben Lindstrom551ea372001-06-05 18:56:16 +000074 int i;
Ben Lindstrom551ea372001-06-05 18:56:16 +000075
76 kbdintctxt = xmalloc(sizeof(KbdintAuthctxt));
77 if (strcmp(devs, "") == 0) {
Damien Miller0e3b8722002-01-22 23:26:38 +110078 buffer_init(&b);
Ben Lindstrom551ea372001-06-05 18:56:16 +000079 for (i = 0; devices[i]; i++) {
Damien Miller0e3b8722002-01-22 23:26:38 +110080 if (buffer_len(&b) > 0)
81 buffer_append(&b, ",", 1);
82 buffer_append(&b, devices[i]->name,
83 strlen(devices[i]->name));
Ben Lindstrom551ea372001-06-05 18:56:16 +000084 }
Damien Miller0e3b8722002-01-22 23:26:38 +110085 buffer_append(&b, "\0", 1);
86 kbdintctxt->devices = xstrdup(buffer_ptr(&b));
87 buffer_free(&b);
Ben Lindstrom551ea372001-06-05 18:56:16 +000088 } else {
89 kbdintctxt->devices = xstrdup(devs);
90 }
Damien Miller0e3b8722002-01-22 23:26:38 +110091 debug("kbdint_alloc: devices '%s'", kbdintctxt->devices);
Ben Lindstrom551ea372001-06-05 18:56:16 +000092 kbdintctxt->ctxt = NULL;
93 kbdintctxt->device = NULL;
Damien Millerfb7fd952002-06-26 23:58:39 +100094 kbdintctxt->nreq = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +000095
96 return kbdintctxt;
97}
Ben Lindstrombba81212001-06-25 05:01:22 +000098static void
Ben Lindstrom551ea372001-06-05 18:56:16 +000099kbdint_reset_device(KbdintAuthctxt *kbdintctxt)
100{
101 if (kbdintctxt->ctxt) {
102 kbdintctxt->device->free_ctx(kbdintctxt->ctxt);
103 kbdintctxt->ctxt = NULL;
104 }
105 kbdintctxt->device = NULL;
106}
Ben Lindstrombba81212001-06-25 05:01:22 +0000107static void
Ben Lindstrom551ea372001-06-05 18:56:16 +0000108kbdint_free(KbdintAuthctxt *kbdintctxt)
109{
110 if (kbdintctxt->device)
111 kbdint_reset_device(kbdintctxt);
112 if (kbdintctxt->devices) {
113 xfree(kbdintctxt->devices);
114 kbdintctxt->devices = NULL;
115 }
116 xfree(kbdintctxt);
117}
118/* get next device */
Ben Lindstrombba81212001-06-25 05:01:22 +0000119static int
Ben Lindstrom551ea372001-06-05 18:56:16 +0000120kbdint_next_device(KbdintAuthctxt *kbdintctxt)
121{
122 size_t len;
123 char *t;
124 int i;
125
126 if (kbdintctxt->device)
127 kbdint_reset_device(kbdintctxt);
128 do {
129 len = kbdintctxt->devices ?
130 strcspn(kbdintctxt->devices, ",") : 0;
131
132 if (len == 0)
133 break;
134 for (i = 0; devices[i]; i++)
135 if (strncmp(kbdintctxt->devices, devices[i]->name, len) == 0)
136 kbdintctxt->device = devices[i];
137 t = kbdintctxt->devices;
138 kbdintctxt->devices = t[len] ? xstrdup(t+len+1) : NULL;
139 xfree(t);
140 debug2("kbdint_next_device: devices %s", kbdintctxt->devices ?
141 kbdintctxt->devices : "<empty>");
142 } while (kbdintctxt->devices && !kbdintctxt->device);
143
144 return kbdintctxt->device ? 1 : 0;
145}
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000146
147/*
Ben Lindstrombdfb4df2001-10-03 17:12:43 +0000148 * try challenge-response, set authctxt->postponed if we have to
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000149 * wait for the response.
150 */
151int
152auth2_challenge(Authctxt *authctxt, char *devs)
153{
Ben Lindstrom551ea372001-06-05 18:56:16 +0000154 debug("auth2_challenge: user=%s devs=%s",
155 authctxt->user ? authctxt->user : "<nouser>",
156 devs ? devs : "<no devs>");
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000157
Ben Lindstrom742e89e2001-06-09 01:17:23 +0000158 if (authctxt->user == NULL || !devs)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000159 return 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100160 if (authctxt->kbdintctxt == NULL)
Ben Lindstrom551ea372001-06-05 18:56:16 +0000161 authctxt->kbdintctxt = kbdint_alloc(devs);
162 return auth2_challenge_start(authctxt);
163}
164
Damien Milleree116252001-12-21 12:42:34 +1100165/* unregister kbd-int callbacks and context */
166void
167auth2_challenge_stop(Authctxt *authctxt)
168{
169 /* unregister callback */
170 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL);
171 if (authctxt->kbdintctxt != NULL) {
172 kbdint_free(authctxt->kbdintctxt);
173 authctxt->kbdintctxt = NULL;
174 }
175}
176
Ben Lindstrom551ea372001-06-05 18:56:16 +0000177/* side effect: sets authctxt->postponed if a reply was sent*/
178static int
179auth2_challenge_start(Authctxt *authctxt)
180{
181 KbdintAuthctxt *kbdintctxt = authctxt->kbdintctxt;
182
183 debug2("auth2_challenge_start: devices %s",
184 kbdintctxt->devices ? kbdintctxt->devices : "<empty>");
185
186 if (kbdint_next_device(kbdintctxt) == 0) {
Damien Milleree116252001-12-21 12:42:34 +1100187 auth2_challenge_stop(authctxt);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000188 return 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000189 }
190 debug("auth2_challenge_start: trying authentication method '%s'",
191 kbdintctxt->device->name);
192
193 if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) {
Damien Milleree116252001-12-21 12:42:34 +1100194 auth2_challenge_stop(authctxt);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000195 return 0;
196 }
197 if (send_userauth_info_request(authctxt) == 0) {
Damien Milleree116252001-12-21 12:42:34 +1100198 auth2_challenge_stop(authctxt);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000199 return 0;
200 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000201 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE,
202 &input_userauth_info_response);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000203
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000204 authctxt->postponed = 1;
205 return 0;
206}
207
Ben Lindstrom551ea372001-06-05 18:56:16 +0000208static int
209send_userauth_info_request(Authctxt *authctxt)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000210{
Ben Lindstrom551ea372001-06-05 18:56:16 +0000211 KbdintAuthctxt *kbdintctxt;
212 char *name, *instr, **prompts;
213 int i;
Damien Millerfb7fd952002-06-26 23:58:39 +1000214 u_int *echo_on;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000215
216 kbdintctxt = authctxt->kbdintctxt;
217 if (kbdintctxt->device->query(kbdintctxt->ctxt,
Damien Millerfb7fd952002-06-26 23:58:39 +1000218 &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on))
Ben Lindstrom551ea372001-06-05 18:56:16 +0000219 return 0;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000220
221 packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000222 packet_put_cstring(name);
223 packet_put_cstring(instr);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000224 packet_put_cstring(""); /* language not used */
Damien Millerfb7fd952002-06-26 23:58:39 +1000225 packet_put_int(kbdintctxt->nreq);
226 for (i = 0; i < kbdintctxt->nreq; i++) {
Ben Lindstrom551ea372001-06-05 18:56:16 +0000227 packet_put_cstring(prompts[i]);
228 packet_put_char(echo_on[i]);
229 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000230 packet_send();
231 packet_write_wait();
Ben Lindstrom551ea372001-06-05 18:56:16 +0000232
Damien Millerfb7fd952002-06-26 23:58:39 +1000233 for (i = 0; i < kbdintctxt->nreq; i++)
Ben Lindstrom551ea372001-06-05 18:56:16 +0000234 xfree(prompts[i]);
235 xfree(prompts);
236 xfree(echo_on);
237 xfree(name);
238 xfree(instr);
239 return 1;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000240}
241
Ben Lindstrom551ea372001-06-05 18:56:16 +0000242static void
Damien Miller630d6f42002-01-22 23:17:30 +1100243input_userauth_info_response(int type, u_int32_t seq, void *ctxt)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000244{
245 Authctxt *authctxt = ctxt;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000246 KbdintAuthctxt *kbdintctxt;
247 int i, authenticated = 0, res, len;
248 u_int nresp;
249 char **response = NULL, *method;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000250
251 if (authctxt == NULL)
252 fatal("input_userauth_info_response: no authctxt");
Ben Lindstrom551ea372001-06-05 18:56:16 +0000253 kbdintctxt = authctxt->kbdintctxt;
254 if (kbdintctxt == NULL || kbdintctxt->ctxt == NULL)
255 fatal("input_userauth_info_response: no kbdintctxt");
256 if (kbdintctxt->device == NULL)
257 fatal("input_userauth_info_response: no device");
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000258
259 authctxt->postponed = 0; /* reset */
260 nresp = packet_get_int();
Damien Millerfb7fd952002-06-26 23:58:39 +1000261 if (nresp != kbdintctxt->nreq)
262 fatal("input_userauth_info_response: wrong number of replies");
263 if (nresp > 100)
264 fatal("input_userauth_info_response: too many replies");
Ben Lindstrom551ea372001-06-05 18:56:16 +0000265 if (nresp > 0) {
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000266 response = xmalloc(nresp * sizeof(char *));
Ben Lindstrom551ea372001-06-05 18:56:16 +0000267 for (i = 0; i < nresp; i++)
268 response[i] = packet_get_string(NULL);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000269 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100270 packet_check_eom();
Ben Lindstrom551ea372001-06-05 18:56:16 +0000271
272 if (authctxt->valid) {
273 res = kbdintctxt->device->respond(kbdintctxt->ctxt,
274 nresp, response);
275 } else {
276 res = -1;
277 }
278
279 for (i = 0; i < nresp; i++) {
280 memset(response[i], 'r', strlen(response[i]));
281 xfree(response[i]);
282 }
283 if (response)
284 xfree(response);
285
286 switch (res) {
287 case 0:
288 /* Success! */
289 authenticated = 1;
290 break;
291 case 1:
292 /* Authentication needs further interaction */
Damien Milleree116252001-12-21 12:42:34 +1100293 if (send_userauth_info_request(authctxt) == 1)
294 authctxt->postponed = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000295 break;
296 default:
297 /* Failure! */
298 break;
299 }
300
301 len = strlen("keyboard-interactive") + 2 +
302 strlen(kbdintctxt->device->name);
303 method = xmalloc(len);
Damien Miller209ee4e2002-01-22 23:25:08 +1100304 snprintf(method, len, "keyboard-interactive/%s",
305 kbdintctxt->device->name);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000306
307 if (!authctxt->postponed) {
Ben Lindstrom551ea372001-06-05 18:56:16 +0000308 if (authenticated) {
Damien Milleree116252001-12-21 12:42:34 +1100309 auth2_challenge_stop(authctxt);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000310 } else {
311 /* start next device */
312 /* may set authctxt->postponed */
313 auth2_challenge_start(authctxt);
314 }
315 }
Damien Miller5d57e502001-03-30 10:48:31 +1000316 userauth_finish(authctxt, authenticated, method);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000317 xfree(method);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000318}
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000319
320void
321privsep_challenge_enable(void)
322{
323#ifdef BSD_AUTH
324 extern KbdintDevice mm_bsdauth_device;
325#endif
326#ifdef SKEY
327 extern KbdintDevice mm_skey_device;
328#endif
329 /* As long as SSHv1 has devices[0] hard coded this is fine */
330#ifdef BSD_AUTH
331 devices[0] = &mm_bsdauth_device;
332#else
333#ifdef SKEY
334 devices[0] = &mm_skey_device;
335#endif
336#endif
337}