blob: 8860a94c54dd62c13c4986d6c04f6bcacb70c7e0 [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 Lindstromdb65e8f2001-01-19 04:26:52 +000026
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000027#include "ssh2.h"
28#include "auth.h"
Damien Miller0e3b8722002-01-22 23:26:38 +110029#include "buffer.h"
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000030#include "packet.h"
31#include "xmalloc.h"
32#include "dispatch.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000033#include "log.h"
Darren Tucker3c660802005-01-20 22:20:50 +110034#include "servconf.h"
35
36/* import */
37extern ServerOptions options;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000038
Ben Lindstrombba81212001-06-25 05:01:22 +000039static int auth2_challenge_start(Authctxt *);
40static int send_userauth_info_request(Authctxt *);
Damien Miller630d6f42002-01-22 23:17:30 +110041static void input_userauth_info_response(int, u_int32_t, void *);
Ben Lindstrom551ea372001-06-05 18:56:16 +000042
43#ifdef BSD_AUTH
44extern KbdintDevice bsdauth_device;
45#else
Damien Miller4f9f42a2003-05-10 19:28:02 +100046#ifdef USE_PAM
47extern KbdintDevice sshpam_device;
48#endif
Ben Lindstrom551ea372001-06-05 18:56:16 +000049#ifdef SKEY
50extern KbdintDevice skey_device;
51#endif
52#endif
53
54KbdintDevice *devices[] = {
55#ifdef BSD_AUTH
56 &bsdauth_device,
57#else
Damien Miller4f9f42a2003-05-10 19:28:02 +100058#ifdef USE_PAM
59 &sshpam_device,
60#endif
Ben Lindstrom551ea372001-06-05 18:56:16 +000061#ifdef SKEY
62 &skey_device,
63#endif
64#endif
65 NULL
66};
67
68typedef struct KbdintAuthctxt KbdintAuthctxt;
69struct KbdintAuthctxt
70{
71 char *devices;
72 void *ctxt;
73 KbdintDevice *device;
Damien Millerfb7fd952002-06-26 23:58:39 +100074 u_int nreq;
Ben Lindstrom551ea372001-06-05 18:56:16 +000075};
76
Darren Tucker3c660802005-01-20 22:20:50 +110077#ifdef USE_PAM
78void
79remove_kbdint_device(const char *devname)
80{
81 int i, j;
82
83 for (i = 0; devices[i] != NULL; i++)
84 if (strcmp(devices[i]->name, devname) == 0) {
85 for (j = i; devices[j] != NULL; j++)
86 devices[j] = devices[j+1];
87 i--;
88 }
89}
90#endif
91
Ben Lindstrombba81212001-06-25 05:01:22 +000092static KbdintAuthctxt *
Ben Lindstrom551ea372001-06-05 18:56:16 +000093kbdint_alloc(const char *devs)
94{
95 KbdintAuthctxt *kbdintctxt;
Damien Miller0e3b8722002-01-22 23:26:38 +110096 Buffer b;
Ben Lindstrom551ea372001-06-05 18:56:16 +000097 int i;
Ben Lindstrom551ea372001-06-05 18:56:16 +000098
Darren Tucker3c660802005-01-20 22:20:50 +110099#ifdef USE_PAM
100 if (!options.use_pam)
101 remove_kbdint_device("pam");
102#endif
103
Ben Lindstrom551ea372001-06-05 18:56:16 +0000104 kbdintctxt = xmalloc(sizeof(KbdintAuthctxt));
105 if (strcmp(devs, "") == 0) {
Damien Miller0e3b8722002-01-22 23:26:38 +1100106 buffer_init(&b);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000107 for (i = 0; devices[i]; i++) {
Damien Miller0e3b8722002-01-22 23:26:38 +1100108 if (buffer_len(&b) > 0)
109 buffer_append(&b, ",", 1);
110 buffer_append(&b, devices[i]->name,
111 strlen(devices[i]->name));
Ben Lindstrom551ea372001-06-05 18:56:16 +0000112 }
Damien Miller0e3b8722002-01-22 23:26:38 +1100113 buffer_append(&b, "\0", 1);
114 kbdintctxt->devices = xstrdup(buffer_ptr(&b));
115 buffer_free(&b);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000116 } else {
117 kbdintctxt->devices = xstrdup(devs);
118 }
Damien Miller0e3b8722002-01-22 23:26:38 +1100119 debug("kbdint_alloc: devices '%s'", kbdintctxt->devices);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000120 kbdintctxt->ctxt = NULL;
121 kbdintctxt->device = NULL;
Damien Millerfb7fd952002-06-26 23:58:39 +1000122 kbdintctxt->nreq = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000123
124 return kbdintctxt;
125}
Ben Lindstrombba81212001-06-25 05:01:22 +0000126static void
Ben Lindstrom551ea372001-06-05 18:56:16 +0000127kbdint_reset_device(KbdintAuthctxt *kbdintctxt)
128{
129 if (kbdintctxt->ctxt) {
130 kbdintctxt->device->free_ctx(kbdintctxt->ctxt);
131 kbdintctxt->ctxt = NULL;
132 }
133 kbdintctxt->device = NULL;
134}
Ben Lindstrombba81212001-06-25 05:01:22 +0000135static void
Ben Lindstrom551ea372001-06-05 18:56:16 +0000136kbdint_free(KbdintAuthctxt *kbdintctxt)
137{
138 if (kbdintctxt->device)
139 kbdint_reset_device(kbdintctxt);
140 if (kbdintctxt->devices) {
141 xfree(kbdintctxt->devices);
142 kbdintctxt->devices = NULL;
143 }
144 xfree(kbdintctxt);
145}
146/* get next device */
Ben Lindstrombba81212001-06-25 05:01:22 +0000147static int
Ben Lindstrom551ea372001-06-05 18:56:16 +0000148kbdint_next_device(KbdintAuthctxt *kbdintctxt)
149{
150 size_t len;
151 char *t;
152 int i;
153
154 if (kbdintctxt->device)
155 kbdint_reset_device(kbdintctxt);
156 do {
157 len = kbdintctxt->devices ?
158 strcspn(kbdintctxt->devices, ",") : 0;
159
160 if (len == 0)
161 break;
162 for (i = 0; devices[i]; i++)
163 if (strncmp(kbdintctxt->devices, devices[i]->name, len) == 0)
164 kbdintctxt->device = devices[i];
165 t = kbdintctxt->devices;
166 kbdintctxt->devices = t[len] ? xstrdup(t+len+1) : NULL;
167 xfree(t);
168 debug2("kbdint_next_device: devices %s", kbdintctxt->devices ?
Damien Miller0dc1bef2005-07-17 17:22:45 +1000169 kbdintctxt->devices : "<empty>");
Ben Lindstrom551ea372001-06-05 18:56:16 +0000170 } while (kbdintctxt->devices && !kbdintctxt->device);
171
172 return kbdintctxt->device ? 1 : 0;
173}
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000174
175/*
Ben Lindstrombdfb4df2001-10-03 17:12:43 +0000176 * try challenge-response, set authctxt->postponed if we have to
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000177 * wait for the response.
178 */
179int
180auth2_challenge(Authctxt *authctxt, char *devs)
181{
Ben Lindstrom551ea372001-06-05 18:56:16 +0000182 debug("auth2_challenge: user=%s devs=%s",
183 authctxt->user ? authctxt->user : "<nouser>",
184 devs ? devs : "<no devs>");
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000185
Ben Lindstrom742e89e2001-06-09 01:17:23 +0000186 if (authctxt->user == NULL || !devs)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000187 return 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100188 if (authctxt->kbdintctxt == NULL)
Ben Lindstrom551ea372001-06-05 18:56:16 +0000189 authctxt->kbdintctxt = kbdint_alloc(devs);
190 return auth2_challenge_start(authctxt);
191}
192
Damien Milleree116252001-12-21 12:42:34 +1100193/* unregister kbd-int callbacks and context */
194void
195auth2_challenge_stop(Authctxt *authctxt)
196{
197 /* unregister callback */
198 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL);
199 if (authctxt->kbdintctxt != NULL) {
200 kbdint_free(authctxt->kbdintctxt);
201 authctxt->kbdintctxt = NULL;
202 }
203}
204
Ben Lindstrom551ea372001-06-05 18:56:16 +0000205/* side effect: sets authctxt->postponed if a reply was sent*/
206static int
207auth2_challenge_start(Authctxt *authctxt)
208{
209 KbdintAuthctxt *kbdintctxt = authctxt->kbdintctxt;
210
211 debug2("auth2_challenge_start: devices %s",
212 kbdintctxt->devices ? kbdintctxt->devices : "<empty>");
213
214 if (kbdint_next_device(kbdintctxt) == 0) {
Damien Milleree116252001-12-21 12:42:34 +1100215 auth2_challenge_stop(authctxt);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000216 return 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000217 }
218 debug("auth2_challenge_start: trying authentication method '%s'",
219 kbdintctxt->device->name);
220
221 if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) {
Damien Milleree116252001-12-21 12:42:34 +1100222 auth2_challenge_stop(authctxt);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000223 return 0;
224 }
225 if (send_userauth_info_request(authctxt) == 0) {
Damien Milleree116252001-12-21 12:42:34 +1100226 auth2_challenge_stop(authctxt);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000227 return 0;
228 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000229 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE,
230 &input_userauth_info_response);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000231
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000232 authctxt->postponed = 1;
233 return 0;
234}
235
Ben Lindstrom551ea372001-06-05 18:56:16 +0000236static int
237send_userauth_info_request(Authctxt *authctxt)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000238{
Ben Lindstrom551ea372001-06-05 18:56:16 +0000239 KbdintAuthctxt *kbdintctxt;
240 char *name, *instr, **prompts;
Damien Millereccb9de2005-06-17 12:59:34 +1000241 u_int i, *echo_on;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000242
243 kbdintctxt = authctxt->kbdintctxt;
244 if (kbdintctxt->device->query(kbdintctxt->ctxt,
Damien Millerfb7fd952002-06-26 23:58:39 +1000245 &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on))
Ben Lindstrom551ea372001-06-05 18:56:16 +0000246 return 0;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000247
248 packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000249 packet_put_cstring(name);
250 packet_put_cstring(instr);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000251 packet_put_cstring(""); /* language not used */
Damien Millerfb7fd952002-06-26 23:58:39 +1000252 packet_put_int(kbdintctxt->nreq);
253 for (i = 0; i < kbdintctxt->nreq; i++) {
Ben Lindstrom551ea372001-06-05 18:56:16 +0000254 packet_put_cstring(prompts[i]);
255 packet_put_char(echo_on[i]);
256 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000257 packet_send();
258 packet_write_wait();
Ben Lindstrom551ea372001-06-05 18:56:16 +0000259
Damien Millerfb7fd952002-06-26 23:58:39 +1000260 for (i = 0; i < kbdintctxt->nreq; i++)
Ben Lindstrom551ea372001-06-05 18:56:16 +0000261 xfree(prompts[i]);
262 xfree(prompts);
263 xfree(echo_on);
264 xfree(name);
265 xfree(instr);
266 return 1;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000267}
268
Ben Lindstrom551ea372001-06-05 18:56:16 +0000269static void
Damien Miller630d6f42002-01-22 23:17:30 +1100270input_userauth_info_response(int type, u_int32_t seq, void *ctxt)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000271{
272 Authctxt *authctxt = ctxt;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000273 KbdintAuthctxt *kbdintctxt;
Damien Millereccb9de2005-06-17 12:59:34 +1000274 int authenticated = 0, res, len;
275 u_int i, nresp;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000276 char **response = NULL, *method;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000277
278 if (authctxt == NULL)
279 fatal("input_userauth_info_response: no authctxt");
Ben Lindstrom551ea372001-06-05 18:56:16 +0000280 kbdintctxt = authctxt->kbdintctxt;
281 if (kbdintctxt == NULL || kbdintctxt->ctxt == NULL)
282 fatal("input_userauth_info_response: no kbdintctxt");
283 if (kbdintctxt->device == NULL)
284 fatal("input_userauth_info_response: no device");
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000285
286 authctxt->postponed = 0; /* reset */
287 nresp = packet_get_int();
Damien Millerfb7fd952002-06-26 23:58:39 +1000288 if (nresp != kbdintctxt->nreq)
289 fatal("input_userauth_info_response: wrong number of replies");
290 if (nresp > 100)
291 fatal("input_userauth_info_response: too many replies");
Ben Lindstrom551ea372001-06-05 18:56:16 +0000292 if (nresp > 0) {
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000293 response = xmalloc(nresp * sizeof(char *));
Ben Lindstrom551ea372001-06-05 18:56:16 +0000294 for (i = 0; i < nresp; i++)
295 response[i] = packet_get_string(NULL);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000296 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100297 packet_check_eom();
Ben Lindstrom551ea372001-06-05 18:56:16 +0000298
Darren Tucker611649e2005-01-20 11:05:34 +1100299 res = kbdintctxt->device->respond(kbdintctxt->ctxt, nresp, response);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000300
301 for (i = 0; i < nresp; i++) {
302 memset(response[i], 'r', strlen(response[i]));
303 xfree(response[i]);
304 }
305 if (response)
306 xfree(response);
307
308 switch (res) {
309 case 0:
310 /* Success! */
Darren Tucker611649e2005-01-20 11:05:34 +1100311 authenticated = authctxt->valid ? 1 : 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000312 break;
313 case 1:
314 /* Authentication needs further interaction */
Damien Milleree116252001-12-21 12:42:34 +1100315 if (send_userauth_info_request(authctxt) == 1)
316 authctxt->postponed = 1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000317 break;
318 default:
319 /* Failure! */
320 break;
321 }
322
323 len = strlen("keyboard-interactive") + 2 +
324 strlen(kbdintctxt->device->name);
325 method = xmalloc(len);
Damien Miller209ee4e2002-01-22 23:25:08 +1100326 snprintf(method, len, "keyboard-interactive/%s",
327 kbdintctxt->device->name);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000328
329 if (!authctxt->postponed) {
Ben Lindstrom551ea372001-06-05 18:56:16 +0000330 if (authenticated) {
Damien Milleree116252001-12-21 12:42:34 +1100331 auth2_challenge_stop(authctxt);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000332 } else {
333 /* start next device */
334 /* may set authctxt->postponed */
335 auth2_challenge_start(authctxt);
336 }
337 }
Damien Miller5d57e502001-03-30 10:48:31 +1000338 userauth_finish(authctxt, authenticated, method);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000339 xfree(method);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000340}
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000341
342void
343privsep_challenge_enable(void)
344{
Damien Millera6a7c192003-05-26 21:36:13 +1000345#if defined(BSD_AUTH) || defined(USE_PAM) || defined(SKEY)
346 int n = 0;
347#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000348#ifdef BSD_AUTH
349 extern KbdintDevice mm_bsdauth_device;
350#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000351#ifdef USE_PAM
352 extern KbdintDevice mm_sshpam_device;
353#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000354#ifdef SKEY
355 extern KbdintDevice mm_skey_device;
356#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000357
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000358#ifdef BSD_AUTH
Damien Miller4f9f42a2003-05-10 19:28:02 +1000359 devices[n++] = &mm_bsdauth_device;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000360#else
Damien Miller4f9f42a2003-05-10 19:28:02 +1000361#ifdef USE_PAM
362 devices[n++] = &mm_sshpam_device;
363#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000364#ifdef SKEY
Damien Miller4f9f42a2003-05-10 19:28:02 +1000365 devices[n++] = &mm_skey_device;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000366#endif
367#endif
368}