blob: aacbf0bccebb19509760f91c4998f4a034fe9860 [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
Damien Miller4f9f42a2003-05-10 19:28:02 +100044#ifdef USE_PAM
45extern KbdintDevice sshpam_device;
46#endif
Ben Lindstrom551ea372001-06-05 18:56:16 +000047#ifdef SKEY
48extern KbdintDevice skey_device;
49#endif
50#endif
51
52KbdintDevice *devices[] = {
53#ifdef BSD_AUTH
54 &bsdauth_device,
55#else
Damien Miller4f9f42a2003-05-10 19:28:02 +100056#ifdef USE_PAM
57 &sshpam_device,
58#endif
Ben Lindstrom551ea372001-06-05 18:56:16 +000059#ifdef SKEY
60 &skey_device,
61#endif
62#endif
63 NULL
64};
65
66typedef struct KbdintAuthctxt KbdintAuthctxt;
67struct KbdintAuthctxt
68{
69 char *devices;
70 void *ctxt;
71 KbdintDevice *device;
Damien Millerfb7fd952002-06-26 23:58:39 +100072 u_int nreq;
Ben Lindstrom551ea372001-06-05 18:56:16 +000073};
74
Ben Lindstrombba81212001-06-25 05:01:22 +000075static KbdintAuthctxt *
Ben Lindstrom551ea372001-06-05 18:56:16 +000076kbdint_alloc(const char *devs)
77{
78 KbdintAuthctxt *kbdintctxt;
Damien Miller0e3b8722002-01-22 23:26:38 +110079 Buffer b;
Ben Lindstrom551ea372001-06-05 18:56:16 +000080 int i;
Ben Lindstrom551ea372001-06-05 18:56:16 +000081
82 kbdintctxt = xmalloc(sizeof(KbdintAuthctxt));
83 if (strcmp(devs, "") == 0) {
Damien Miller0e3b8722002-01-22 23:26:38 +110084 buffer_init(&b);
Ben Lindstrom551ea372001-06-05 18:56:16 +000085 for (i = 0; devices[i]; i++) {
Damien Miller0e3b8722002-01-22 23:26:38 +110086 if (buffer_len(&b) > 0)
87 buffer_append(&b, ",", 1);
88 buffer_append(&b, devices[i]->name,
89 strlen(devices[i]->name));
Ben Lindstrom551ea372001-06-05 18:56:16 +000090 }
Damien Miller0e3b8722002-01-22 23:26:38 +110091 buffer_append(&b, "\0", 1);
92 kbdintctxt->devices = xstrdup(buffer_ptr(&b));
93 buffer_free(&b);
Ben Lindstrom551ea372001-06-05 18:56:16 +000094 } else {
95 kbdintctxt->devices = xstrdup(devs);
96 }
Damien Miller0e3b8722002-01-22 23:26:38 +110097 debug("kbdint_alloc: devices '%s'", kbdintctxt->devices);
Ben Lindstrom551ea372001-06-05 18:56:16 +000098 kbdintctxt->ctxt = NULL;
99 kbdintctxt->device = NULL;
Damien Millerfb7fd952002-06-26 23:58:39 +1000100 kbdintctxt->nreq = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000101
102 return kbdintctxt;
103}
Ben Lindstrombba81212001-06-25 05:01:22 +0000104static void
Ben Lindstrom551ea372001-06-05 18:56:16 +0000105kbdint_reset_device(KbdintAuthctxt *kbdintctxt)
106{
107 if (kbdintctxt->ctxt) {
108 kbdintctxt->device->free_ctx(kbdintctxt->ctxt);
109 kbdintctxt->ctxt = NULL;
110 }
111 kbdintctxt->device = NULL;
112}
Ben Lindstrombba81212001-06-25 05:01:22 +0000113static void
Ben Lindstrom551ea372001-06-05 18:56:16 +0000114kbdint_free(KbdintAuthctxt *kbdintctxt)
115{
116 if (kbdintctxt->device)
117 kbdint_reset_device(kbdintctxt);
118 if (kbdintctxt->devices) {
119 xfree(kbdintctxt->devices);
120 kbdintctxt->devices = NULL;
121 }
122 xfree(kbdintctxt);
123}
124/* get next device */
Ben Lindstrombba81212001-06-25 05:01:22 +0000125static int
Ben Lindstrom551ea372001-06-05 18:56:16 +0000126kbdint_next_device(KbdintAuthctxt *kbdintctxt)
127{
128 size_t len;
129 char *t;
130 int i;
131
132 if (kbdintctxt->device)
133 kbdint_reset_device(kbdintctxt);
134 do {
135 len = kbdintctxt->devices ?
136 strcspn(kbdintctxt->devices, ",") : 0;
137
138 if (len == 0)
139 break;
140 for (i = 0; devices[i]; i++)
141 if (strncmp(kbdintctxt->devices, devices[i]->name, len) == 0)
142 kbdintctxt->device = devices[i];
143 t = kbdintctxt->devices;
144 kbdintctxt->devices = t[len] ? xstrdup(t+len+1) : NULL;
145 xfree(t);
146 debug2("kbdint_next_device: devices %s", kbdintctxt->devices ?
147 kbdintctxt->devices : "<empty>");
148 } while (kbdintctxt->devices && !kbdintctxt->device);
149
150 return kbdintctxt->device ? 1 : 0;
151}
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000152
153/*
Ben Lindstrombdfb4df2001-10-03 17:12:43 +0000154 * try challenge-response, set authctxt->postponed if we have to
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000155 * wait for the response.
156 */
157int
158auth2_challenge(Authctxt *authctxt, char *devs)
159{
Ben Lindstrom551ea372001-06-05 18:56:16 +0000160 debug("auth2_challenge: user=%s devs=%s",
161 authctxt->user ? authctxt->user : "<nouser>",
162 devs ? devs : "<no devs>");
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000163
Ben Lindstrom742e89e2001-06-09 01:17:23 +0000164 if (authctxt->user == NULL || !devs)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000165 return 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100166 if (authctxt->kbdintctxt == NULL)
Ben Lindstrom551ea372001-06-05 18:56:16 +0000167 authctxt->kbdintctxt = kbdint_alloc(devs);
168 return auth2_challenge_start(authctxt);
169}
170
Damien Milleree116252001-12-21 12:42:34 +1100171/* unregister kbd-int callbacks and context */
172void
173auth2_challenge_stop(Authctxt *authctxt)
174{
175 /* unregister callback */
176 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL);
177 if (authctxt->kbdintctxt != NULL) {
178 kbdint_free(authctxt->kbdintctxt);
179 authctxt->kbdintctxt = NULL;
180 }
181}
182
Ben Lindstrom551ea372001-06-05 18:56:16 +0000183/* side effect: sets authctxt->postponed if a reply was sent*/
184static int
185auth2_challenge_start(Authctxt *authctxt)
186{
187 KbdintAuthctxt *kbdintctxt = authctxt->kbdintctxt;
188
189 debug2("auth2_challenge_start: devices %s",
190 kbdintctxt->devices ? kbdintctxt->devices : "<empty>");
191
192 if (kbdint_next_device(kbdintctxt) == 0) {
Damien Milleree116252001-12-21 12:42:34 +1100193 auth2_challenge_stop(authctxt);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000194 return 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000195 }
196 debug("auth2_challenge_start: trying authentication method '%s'",
197 kbdintctxt->device->name);
198
199 if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) {
Damien Milleree116252001-12-21 12:42:34 +1100200 auth2_challenge_stop(authctxt);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000201 return 0;
202 }
203 if (send_userauth_info_request(authctxt) == 0) {
Damien Milleree116252001-12-21 12:42:34 +1100204 auth2_challenge_stop(authctxt);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000205 return 0;
206 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000207 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE,
208 &input_userauth_info_response);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000209
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000210 authctxt->postponed = 1;
211 return 0;
212}
213
Ben Lindstrom551ea372001-06-05 18:56:16 +0000214static int
215send_userauth_info_request(Authctxt *authctxt)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000216{
Ben Lindstrom551ea372001-06-05 18:56:16 +0000217 KbdintAuthctxt *kbdintctxt;
218 char *name, *instr, **prompts;
219 int i;
Damien Millerfb7fd952002-06-26 23:58:39 +1000220 u_int *echo_on;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000221
222 kbdintctxt = authctxt->kbdintctxt;
223 if (kbdintctxt->device->query(kbdintctxt->ctxt,
Damien Millerfb7fd952002-06-26 23:58:39 +1000224 &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on))
Ben Lindstrom551ea372001-06-05 18:56:16 +0000225 return 0;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000226
227 packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000228 packet_put_cstring(name);
229 packet_put_cstring(instr);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000230 packet_put_cstring(""); /* language not used */
Damien Millerfb7fd952002-06-26 23:58:39 +1000231 packet_put_int(kbdintctxt->nreq);
232 for (i = 0; i < kbdintctxt->nreq; i++) {
Ben Lindstrom551ea372001-06-05 18:56:16 +0000233 packet_put_cstring(prompts[i]);
234 packet_put_char(echo_on[i]);
235 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000236 packet_send();
237 packet_write_wait();
Ben Lindstrom551ea372001-06-05 18:56:16 +0000238
Damien Millerfb7fd952002-06-26 23:58:39 +1000239 for (i = 0; i < kbdintctxt->nreq; i++)
Ben Lindstrom551ea372001-06-05 18:56:16 +0000240 xfree(prompts[i]);
241 xfree(prompts);
242 xfree(echo_on);
243 xfree(name);
244 xfree(instr);
245 return 1;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000246}
247
Ben Lindstrom551ea372001-06-05 18:56:16 +0000248static void
Damien Miller630d6f42002-01-22 23:17:30 +1100249input_userauth_info_response(int type, u_int32_t seq, void *ctxt)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000250{
251 Authctxt *authctxt = ctxt;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000252 KbdintAuthctxt *kbdintctxt;
253 int i, authenticated = 0, res, len;
254 u_int nresp;
255 char **response = NULL, *method;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000256
257 if (authctxt == NULL)
258 fatal("input_userauth_info_response: no authctxt");
Ben Lindstrom551ea372001-06-05 18:56:16 +0000259 kbdintctxt = authctxt->kbdintctxt;
260 if (kbdintctxt == NULL || kbdintctxt->ctxt == NULL)
261 fatal("input_userauth_info_response: no kbdintctxt");
262 if (kbdintctxt->device == NULL)
263 fatal("input_userauth_info_response: no device");
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000264
265 authctxt->postponed = 0; /* reset */
266 nresp = packet_get_int();
Damien Millerfb7fd952002-06-26 23:58:39 +1000267 if (nresp != kbdintctxt->nreq)
268 fatal("input_userauth_info_response: wrong number of replies");
269 if (nresp > 100)
270 fatal("input_userauth_info_response: too many replies");
Ben Lindstrom551ea372001-06-05 18:56:16 +0000271 if (nresp > 0) {
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000272 response = xmalloc(nresp * sizeof(char *));
Ben Lindstrom551ea372001-06-05 18:56:16 +0000273 for (i = 0; i < nresp; i++)
274 response[i] = packet_get_string(NULL);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000275 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100276 packet_check_eom();
Ben Lindstrom551ea372001-06-05 18:56:16 +0000277
278 if (authctxt->valid) {
279 res = kbdintctxt->device->respond(kbdintctxt->ctxt,
280 nresp, response);
281 } else {
282 res = -1;
283 }
284
285 for (i = 0; i < nresp; i++) {
286 memset(response[i], 'r', strlen(response[i]));
287 xfree(response[i]);
288 }
289 if (response)
290 xfree(response);
291
292 switch (res) {
293 case 0:
294 /* Success! */
295 authenticated = 1;
296 break;
297 case 1:
298 /* Authentication needs further interaction */
Damien Milleree116252001-12-21 12:42:34 +1100299 if (send_userauth_info_request(authctxt) == 1)
300 authctxt->postponed = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000301 break;
302 default:
303 /* Failure! */
304 break;
305 }
306
307 len = strlen("keyboard-interactive") + 2 +
308 strlen(kbdintctxt->device->name);
309 method = xmalloc(len);
Damien Miller209ee4e2002-01-22 23:25:08 +1100310 snprintf(method, len, "keyboard-interactive/%s",
311 kbdintctxt->device->name);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000312
313 if (!authctxt->postponed) {
Ben Lindstrom551ea372001-06-05 18:56:16 +0000314 if (authenticated) {
Damien Milleree116252001-12-21 12:42:34 +1100315 auth2_challenge_stop(authctxt);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000316 } else {
317 /* start next device */
318 /* may set authctxt->postponed */
319 auth2_challenge_start(authctxt);
320 }
321 }
Damien Miller5d57e502001-03-30 10:48:31 +1000322 userauth_finish(authctxt, authenticated, method);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000323 xfree(method);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000324}
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000325
326void
327privsep_challenge_enable(void)
328{
Damien Millera6a7c192003-05-26 21:36:13 +1000329#if defined(BSD_AUTH) || defined(USE_PAM) || defined(SKEY)
330 int n = 0;
331#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000332#ifdef BSD_AUTH
333 extern KbdintDevice mm_bsdauth_device;
334#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000335#ifdef USE_PAM
336 extern KbdintDevice mm_sshpam_device;
337#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000338#ifdef SKEY
339 extern KbdintDevice mm_skey_device;
340#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000341
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000342#ifdef BSD_AUTH
Damien Miller4f9f42a2003-05-10 19:28:02 +1000343 devices[n++] = &mm_bsdauth_device;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000344#else
Damien Miller4f9f42a2003-05-10 19:28:02 +1000345#ifdef USE_PAM
346 devices[n++] = &mm_sshpam_device;
347#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000348#ifdef SKEY
Damien Miller4f9f42a2003-05-10 19:28:02 +1000349 devices[n++] = &mm_skey_device;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000350#endif
351#endif
352}