blob: 384a543ee4e4e8450e64645c5935b62e061d2cd5 [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"
Darren Tucker611649e2005-01-20 11:05:34 +110026RCSID("$OpenBSD: auth2-chall.c,v 1.22 2005/01/19 13:11:47 dtucker 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 Lindstrom226cfa02001-01-22 05:34:40 +000034#include "log.h"
Darren Tucker3c660802005-01-20 22:20:50 +110035#include "servconf.h"
36
37/* import */
38extern ServerOptions options;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000039
Ben Lindstrombba81212001-06-25 05:01:22 +000040static int auth2_challenge_start(Authctxt *);
41static int send_userauth_info_request(Authctxt *);
Damien Miller630d6f42002-01-22 23:17:30 +110042static void input_userauth_info_response(int, u_int32_t, void *);
Ben Lindstrom551ea372001-06-05 18:56:16 +000043
44#ifdef BSD_AUTH
45extern KbdintDevice bsdauth_device;
46#else
Damien Miller4f9f42a2003-05-10 19:28:02 +100047#ifdef USE_PAM
48extern KbdintDevice sshpam_device;
49#endif
Ben Lindstrom551ea372001-06-05 18:56:16 +000050#ifdef SKEY
51extern KbdintDevice skey_device;
52#endif
53#endif
54
55KbdintDevice *devices[] = {
56#ifdef BSD_AUTH
57 &bsdauth_device,
58#else
Damien Miller4f9f42a2003-05-10 19:28:02 +100059#ifdef USE_PAM
60 &sshpam_device,
61#endif
Ben Lindstrom551ea372001-06-05 18:56:16 +000062#ifdef SKEY
63 &skey_device,
64#endif
65#endif
66 NULL
67};
68
69typedef struct KbdintAuthctxt KbdintAuthctxt;
70struct KbdintAuthctxt
71{
72 char *devices;
73 void *ctxt;
74 KbdintDevice *device;
Damien Millerfb7fd952002-06-26 23:58:39 +100075 u_int nreq;
Ben Lindstrom551ea372001-06-05 18:56:16 +000076};
77
Darren Tucker3c660802005-01-20 22:20:50 +110078#ifdef USE_PAM
79void
80remove_kbdint_device(const char *devname)
81{
82 int i, j;
83
84 for (i = 0; devices[i] != NULL; i++)
85 if (strcmp(devices[i]->name, devname) == 0) {
86 for (j = i; devices[j] != NULL; j++)
87 devices[j] = devices[j+1];
88 i--;
89 }
90}
91#endif
92
Ben Lindstrombba81212001-06-25 05:01:22 +000093static KbdintAuthctxt *
Ben Lindstrom551ea372001-06-05 18:56:16 +000094kbdint_alloc(const char *devs)
95{
96 KbdintAuthctxt *kbdintctxt;
Damien Miller0e3b8722002-01-22 23:26:38 +110097 Buffer b;
Ben Lindstrom551ea372001-06-05 18:56:16 +000098 int i;
Ben Lindstrom551ea372001-06-05 18:56:16 +000099
Darren Tucker3c660802005-01-20 22:20:50 +1100100#ifdef USE_PAM
101 if (!options.use_pam)
102 remove_kbdint_device("pam");
103#endif
104
Ben Lindstrom551ea372001-06-05 18:56:16 +0000105 kbdintctxt = xmalloc(sizeof(KbdintAuthctxt));
106 if (strcmp(devs, "") == 0) {
Damien Miller0e3b8722002-01-22 23:26:38 +1100107 buffer_init(&b);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000108 for (i = 0; devices[i]; i++) {
Damien Miller0e3b8722002-01-22 23:26:38 +1100109 if (buffer_len(&b) > 0)
110 buffer_append(&b, ",", 1);
111 buffer_append(&b, devices[i]->name,
112 strlen(devices[i]->name));
Ben Lindstrom551ea372001-06-05 18:56:16 +0000113 }
Damien Miller0e3b8722002-01-22 23:26:38 +1100114 buffer_append(&b, "\0", 1);
115 kbdintctxt->devices = xstrdup(buffer_ptr(&b));
116 buffer_free(&b);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000117 } else {
118 kbdintctxt->devices = xstrdup(devs);
119 }
Damien Miller0e3b8722002-01-22 23:26:38 +1100120 debug("kbdint_alloc: devices '%s'", kbdintctxt->devices);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000121 kbdintctxt->ctxt = NULL;
122 kbdintctxt->device = NULL;
Damien Millerfb7fd952002-06-26 23:58:39 +1000123 kbdintctxt->nreq = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000124
125 return kbdintctxt;
126}
Ben Lindstrombba81212001-06-25 05:01:22 +0000127static void
Ben Lindstrom551ea372001-06-05 18:56:16 +0000128kbdint_reset_device(KbdintAuthctxt *kbdintctxt)
129{
130 if (kbdintctxt->ctxt) {
131 kbdintctxt->device->free_ctx(kbdintctxt->ctxt);
132 kbdintctxt->ctxt = NULL;
133 }
134 kbdintctxt->device = NULL;
135}
Ben Lindstrombba81212001-06-25 05:01:22 +0000136static void
Ben Lindstrom551ea372001-06-05 18:56:16 +0000137kbdint_free(KbdintAuthctxt *kbdintctxt)
138{
139 if (kbdintctxt->device)
140 kbdint_reset_device(kbdintctxt);
141 if (kbdintctxt->devices) {
142 xfree(kbdintctxt->devices);
143 kbdintctxt->devices = NULL;
144 }
145 xfree(kbdintctxt);
146}
147/* get next device */
Ben Lindstrombba81212001-06-25 05:01:22 +0000148static int
Ben Lindstrom551ea372001-06-05 18:56:16 +0000149kbdint_next_device(KbdintAuthctxt *kbdintctxt)
150{
151 size_t len;
152 char *t;
153 int i;
154
155 if (kbdintctxt->device)
156 kbdint_reset_device(kbdintctxt);
157 do {
158 len = kbdintctxt->devices ?
159 strcspn(kbdintctxt->devices, ",") : 0;
160
161 if (len == 0)
162 break;
163 for (i = 0; devices[i]; i++)
164 if (strncmp(kbdintctxt->devices, devices[i]->name, len) == 0)
165 kbdintctxt->device = devices[i];
166 t = kbdintctxt->devices;
167 kbdintctxt->devices = t[len] ? xstrdup(t+len+1) : NULL;
168 xfree(t);
169 debug2("kbdint_next_device: devices %s", kbdintctxt->devices ?
170 kbdintctxt->devices : "<empty>");
171 } while (kbdintctxt->devices && !kbdintctxt->device);
172
173 return kbdintctxt->device ? 1 : 0;
174}
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000175
176/*
Ben Lindstrombdfb4df2001-10-03 17:12:43 +0000177 * try challenge-response, set authctxt->postponed if we have to
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000178 * wait for the response.
179 */
180int
181auth2_challenge(Authctxt *authctxt, char *devs)
182{
Ben Lindstrom551ea372001-06-05 18:56:16 +0000183 debug("auth2_challenge: user=%s devs=%s",
184 authctxt->user ? authctxt->user : "<nouser>",
185 devs ? devs : "<no devs>");
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000186
Ben Lindstrom742e89e2001-06-09 01:17:23 +0000187 if (authctxt->user == NULL || !devs)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000188 return 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100189 if (authctxt->kbdintctxt == NULL)
Ben Lindstrom551ea372001-06-05 18:56:16 +0000190 authctxt->kbdintctxt = kbdint_alloc(devs);
191 return auth2_challenge_start(authctxt);
192}
193
Damien Milleree116252001-12-21 12:42:34 +1100194/* unregister kbd-int callbacks and context */
195void
196auth2_challenge_stop(Authctxt *authctxt)
197{
198 /* unregister callback */
199 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL);
200 if (authctxt->kbdintctxt != NULL) {
201 kbdint_free(authctxt->kbdintctxt);
202 authctxt->kbdintctxt = NULL;
203 }
204}
205
Ben Lindstrom551ea372001-06-05 18:56:16 +0000206/* side effect: sets authctxt->postponed if a reply was sent*/
207static int
208auth2_challenge_start(Authctxt *authctxt)
209{
210 KbdintAuthctxt *kbdintctxt = authctxt->kbdintctxt;
211
212 debug2("auth2_challenge_start: devices %s",
213 kbdintctxt->devices ? kbdintctxt->devices : "<empty>");
214
215 if (kbdint_next_device(kbdintctxt) == 0) {
Damien Milleree116252001-12-21 12:42:34 +1100216 auth2_challenge_stop(authctxt);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000217 return 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000218 }
219 debug("auth2_challenge_start: trying authentication method '%s'",
220 kbdintctxt->device->name);
221
222 if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) {
Damien Milleree116252001-12-21 12:42:34 +1100223 auth2_challenge_stop(authctxt);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000224 return 0;
225 }
226 if (send_userauth_info_request(authctxt) == 0) {
Damien Milleree116252001-12-21 12:42:34 +1100227 auth2_challenge_stop(authctxt);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000228 return 0;
229 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000230 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE,
231 &input_userauth_info_response);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000232
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000233 authctxt->postponed = 1;
234 return 0;
235}
236
Ben Lindstrom551ea372001-06-05 18:56:16 +0000237static int
238send_userauth_info_request(Authctxt *authctxt)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000239{
Ben Lindstrom551ea372001-06-05 18:56:16 +0000240 KbdintAuthctxt *kbdintctxt;
241 char *name, *instr, **prompts;
242 int i;
Damien Millerfb7fd952002-06-26 23:58:39 +1000243 u_int *echo_on;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000244
245 kbdintctxt = authctxt->kbdintctxt;
246 if (kbdintctxt->device->query(kbdintctxt->ctxt,
Damien Millerfb7fd952002-06-26 23:58:39 +1000247 &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on))
Ben Lindstrom551ea372001-06-05 18:56:16 +0000248 return 0;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000249
250 packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000251 packet_put_cstring(name);
252 packet_put_cstring(instr);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000253 packet_put_cstring(""); /* language not used */
Damien Millerfb7fd952002-06-26 23:58:39 +1000254 packet_put_int(kbdintctxt->nreq);
255 for (i = 0; i < kbdintctxt->nreq; i++) {
Ben Lindstrom551ea372001-06-05 18:56:16 +0000256 packet_put_cstring(prompts[i]);
257 packet_put_char(echo_on[i]);
258 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000259 packet_send();
260 packet_write_wait();
Ben Lindstrom551ea372001-06-05 18:56:16 +0000261
Damien Millerfb7fd952002-06-26 23:58:39 +1000262 for (i = 0; i < kbdintctxt->nreq; i++)
Ben Lindstrom551ea372001-06-05 18:56:16 +0000263 xfree(prompts[i]);
264 xfree(prompts);
265 xfree(echo_on);
266 xfree(name);
267 xfree(instr);
268 return 1;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000269}
270
Ben Lindstrom551ea372001-06-05 18:56:16 +0000271static void
Damien Miller630d6f42002-01-22 23:17:30 +1100272input_userauth_info_response(int type, u_int32_t seq, void *ctxt)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000273{
274 Authctxt *authctxt = ctxt;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000275 KbdintAuthctxt *kbdintctxt;
276 int i, authenticated = 0, res, len;
277 u_int nresp;
278 char **response = NULL, *method;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000279
280 if (authctxt == NULL)
281 fatal("input_userauth_info_response: no authctxt");
Ben Lindstrom551ea372001-06-05 18:56:16 +0000282 kbdintctxt = authctxt->kbdintctxt;
283 if (kbdintctxt == NULL || kbdintctxt->ctxt == NULL)
284 fatal("input_userauth_info_response: no kbdintctxt");
285 if (kbdintctxt->device == NULL)
286 fatal("input_userauth_info_response: no device");
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000287
288 authctxt->postponed = 0; /* reset */
289 nresp = packet_get_int();
Damien Millerfb7fd952002-06-26 23:58:39 +1000290 if (nresp != kbdintctxt->nreq)
291 fatal("input_userauth_info_response: wrong number of replies");
292 if (nresp > 100)
293 fatal("input_userauth_info_response: too many replies");
Ben Lindstrom551ea372001-06-05 18:56:16 +0000294 if (nresp > 0) {
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000295 response = xmalloc(nresp * sizeof(char *));
Ben Lindstrom551ea372001-06-05 18:56:16 +0000296 for (i = 0; i < nresp; i++)
297 response[i] = packet_get_string(NULL);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000298 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100299 packet_check_eom();
Ben Lindstrom551ea372001-06-05 18:56:16 +0000300
Darren Tucker611649e2005-01-20 11:05:34 +1100301 res = kbdintctxt->device->respond(kbdintctxt->ctxt, nresp, response);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000302
303 for (i = 0; i < nresp; i++) {
304 memset(response[i], 'r', strlen(response[i]));
305 xfree(response[i]);
306 }
307 if (response)
308 xfree(response);
309
310 switch (res) {
311 case 0:
312 /* Success! */
Darren Tucker611649e2005-01-20 11:05:34 +1100313 authenticated = authctxt->valid ? 1 : 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000314 break;
315 case 1:
316 /* Authentication needs further interaction */
Damien Milleree116252001-12-21 12:42:34 +1100317 if (send_userauth_info_request(authctxt) == 1)
318 authctxt->postponed = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000319 break;
320 default:
321 /* Failure! */
322 break;
323 }
324
325 len = strlen("keyboard-interactive") + 2 +
326 strlen(kbdintctxt->device->name);
327 method = xmalloc(len);
Damien Miller209ee4e2002-01-22 23:25:08 +1100328 snprintf(method, len, "keyboard-interactive/%s",
329 kbdintctxt->device->name);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000330
331 if (!authctxt->postponed) {
Ben Lindstrom551ea372001-06-05 18:56:16 +0000332 if (authenticated) {
Damien Milleree116252001-12-21 12:42:34 +1100333 auth2_challenge_stop(authctxt);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000334 } else {
335 /* start next device */
336 /* may set authctxt->postponed */
337 auth2_challenge_start(authctxt);
338 }
339 }
Damien Miller5d57e502001-03-30 10:48:31 +1000340 userauth_finish(authctxt, authenticated, method);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000341 xfree(method);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000342}
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000343
344void
345privsep_challenge_enable(void)
346{
Damien Millera6a7c192003-05-26 21:36:13 +1000347#if defined(BSD_AUTH) || defined(USE_PAM) || defined(SKEY)
348 int n = 0;
349#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000350#ifdef BSD_AUTH
351 extern KbdintDevice mm_bsdauth_device;
352#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000353#ifdef USE_PAM
354 extern KbdintDevice mm_sshpam_device;
355#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000356#ifdef SKEY
357 extern KbdintDevice mm_skey_device;
358#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000359
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000360#ifdef BSD_AUTH
Damien Miller4f9f42a2003-05-10 19:28:02 +1000361 devices[n++] = &mm_bsdauth_device;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000362#else
Damien Miller4f9f42a2003-05-10 19:28:02 +1000363#ifdef USE_PAM
364 devices[n++] = &mm_sshpam_device;
365#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000366#ifdef SKEY
Damien Miller4f9f42a2003-05-10 19:28:02 +1000367 devices[n++] = &mm_skey_device;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000368#endif
369#endif
370}