- stevesk@cvs.openbsd.org 2006/08/01 23:36:12
[openssh-git.git] / auth2-chall.c
blob89b261e61f7a1ce10da777998b9e7095cd3298df
1 /* $OpenBSD: auth2-chall.c,v 1.29 2006/08/01 23:22:47 stevesk Exp $ */
2 /*
3 * Copyright (c) 2001 Markus Friedl. All rights reserved.
4 * Copyright (c) 2001 Per Allansson. All rights reserved.
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.
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.
26 #include "includes.h"
28 #include <stdio.h>
29 #include <string.h>
31 #include "ssh2.h"
32 #include "auth.h"
33 #include "buffer.h"
34 #include "packet.h"
35 #include "xmalloc.h"
36 #include "dispatch.h"
37 #include "log.h"
38 #include "servconf.h"
40 /* import */
41 extern ServerOptions options;
43 static int auth2_challenge_start(Authctxt *);
44 static int send_userauth_info_request(Authctxt *);
45 static void input_userauth_info_response(int, u_int32_t, void *);
47 #ifdef BSD_AUTH
48 extern KbdintDevice bsdauth_device;
49 #else
50 #ifdef USE_PAM
51 extern KbdintDevice sshpam_device;
52 #endif
53 #ifdef SKEY
54 extern KbdintDevice skey_device;
55 #endif
56 #endif
58 KbdintDevice *devices[] = {
59 #ifdef BSD_AUTH
60 &bsdauth_device,
61 #else
62 #ifdef USE_PAM
63 &sshpam_device,
64 #endif
65 #ifdef SKEY
66 &skey_device,
67 #endif
68 #endif
69 NULL
72 typedef struct KbdintAuthctxt KbdintAuthctxt;
73 struct KbdintAuthctxt
75 char *devices;
76 void *ctxt;
77 KbdintDevice *device;
78 u_int nreq;
81 #ifdef USE_PAM
82 void
83 remove_kbdint_device(const char *devname)
85 int i, j;
87 for (i = 0; devices[i] != NULL; i++)
88 if (strcmp(devices[i]->name, devname) == 0) {
89 for (j = i; devices[j] != NULL; j++)
90 devices[j] = devices[j+1];
91 i--;
94 #endif
96 static KbdintAuthctxt *
97 kbdint_alloc(const char *devs)
99 KbdintAuthctxt *kbdintctxt;
100 Buffer b;
101 int i;
103 #ifdef USE_PAM
104 if (!options.use_pam)
105 remove_kbdint_device("pam");
106 #endif
108 kbdintctxt = xmalloc(sizeof(KbdintAuthctxt));
109 if (strcmp(devs, "") == 0) {
110 buffer_init(&b);
111 for (i = 0; devices[i]; i++) {
112 if (buffer_len(&b) > 0)
113 buffer_append(&b, ",", 1);
114 buffer_append(&b, devices[i]->name,
115 strlen(devices[i]->name));
117 buffer_append(&b, "\0", 1);
118 kbdintctxt->devices = xstrdup(buffer_ptr(&b));
119 buffer_free(&b);
120 } else {
121 kbdintctxt->devices = xstrdup(devs);
123 debug("kbdint_alloc: devices '%s'", kbdintctxt->devices);
124 kbdintctxt->ctxt = NULL;
125 kbdintctxt->device = NULL;
126 kbdintctxt->nreq = 0;
128 return kbdintctxt;
130 static void
131 kbdint_reset_device(KbdintAuthctxt *kbdintctxt)
133 if (kbdintctxt->ctxt) {
134 kbdintctxt->device->free_ctx(kbdintctxt->ctxt);
135 kbdintctxt->ctxt = NULL;
137 kbdintctxt->device = NULL;
139 static void
140 kbdint_free(KbdintAuthctxt *kbdintctxt)
142 if (kbdintctxt->device)
143 kbdint_reset_device(kbdintctxt);
144 if (kbdintctxt->devices) {
145 xfree(kbdintctxt->devices);
146 kbdintctxt->devices = NULL;
148 xfree(kbdintctxt);
150 /* get next device */
151 static int
152 kbdint_next_device(KbdintAuthctxt *kbdintctxt)
154 size_t len;
155 char *t;
156 int i;
158 if (kbdintctxt->device)
159 kbdint_reset_device(kbdintctxt);
160 do {
161 len = kbdintctxt->devices ?
162 strcspn(kbdintctxt->devices, ",") : 0;
164 if (len == 0)
165 break;
166 for (i = 0; devices[i]; i++)
167 if (strncmp(kbdintctxt->devices, devices[i]->name, len) == 0)
168 kbdintctxt->device = devices[i];
169 t = kbdintctxt->devices;
170 kbdintctxt->devices = t[len] ? xstrdup(t+len+1) : NULL;
171 xfree(t);
172 debug2("kbdint_next_device: devices %s", kbdintctxt->devices ?
173 kbdintctxt->devices : "<empty>");
174 } while (kbdintctxt->devices && !kbdintctxt->device);
176 return kbdintctxt->device ? 1 : 0;
180 * try challenge-response, set authctxt->postponed if we have to
181 * wait for the response.
184 auth2_challenge(Authctxt *authctxt, char *devs)
186 debug("auth2_challenge: user=%s devs=%s",
187 authctxt->user ? authctxt->user : "<nouser>",
188 devs ? devs : "<no devs>");
190 if (authctxt->user == NULL || !devs)
191 return 0;
192 if (authctxt->kbdintctxt == NULL)
193 authctxt->kbdintctxt = kbdint_alloc(devs);
194 return auth2_challenge_start(authctxt);
197 /* unregister kbd-int callbacks and context */
198 void
199 auth2_challenge_stop(Authctxt *authctxt)
201 /* unregister callback */
202 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL);
203 if (authctxt->kbdintctxt != NULL) {
204 kbdint_free(authctxt->kbdintctxt);
205 authctxt->kbdintctxt = NULL;
209 /* side effect: sets authctxt->postponed if a reply was sent*/
210 static int
211 auth2_challenge_start(Authctxt *authctxt)
213 KbdintAuthctxt *kbdintctxt = authctxt->kbdintctxt;
215 debug2("auth2_challenge_start: devices %s",
216 kbdintctxt->devices ? kbdintctxt->devices : "<empty>");
218 if (kbdint_next_device(kbdintctxt) == 0) {
219 auth2_challenge_stop(authctxt);
220 return 0;
222 debug("auth2_challenge_start: trying authentication method '%s'",
223 kbdintctxt->device->name);
225 if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) {
226 auth2_challenge_stop(authctxt);
227 return 0;
229 if (send_userauth_info_request(authctxt) == 0) {
230 auth2_challenge_stop(authctxt);
231 return 0;
233 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE,
234 &input_userauth_info_response);
236 authctxt->postponed = 1;
237 return 0;
240 static int
241 send_userauth_info_request(Authctxt *authctxt)
243 KbdintAuthctxt *kbdintctxt;
244 char *name, *instr, **prompts;
245 u_int i, *echo_on;
247 kbdintctxt = authctxt->kbdintctxt;
248 if (kbdintctxt->device->query(kbdintctxt->ctxt,
249 &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on))
250 return 0;
252 packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST);
253 packet_put_cstring(name);
254 packet_put_cstring(instr);
255 packet_put_cstring(""); /* language not used */
256 packet_put_int(kbdintctxt->nreq);
257 for (i = 0; i < kbdintctxt->nreq; i++) {
258 packet_put_cstring(prompts[i]);
259 packet_put_char(echo_on[i]);
261 packet_send();
262 packet_write_wait();
264 for (i = 0; i < kbdintctxt->nreq; i++)
265 xfree(prompts[i]);
266 xfree(prompts);
267 xfree(echo_on);
268 xfree(name);
269 xfree(instr);
270 return 1;
273 static void
274 input_userauth_info_response(int type, u_int32_t seq, void *ctxt)
276 Authctxt *authctxt = ctxt;
277 KbdintAuthctxt *kbdintctxt;
278 int authenticated = 0, res, len;
279 u_int i, nresp;
280 char **response = NULL, *method;
282 if (authctxt == NULL)
283 fatal("input_userauth_info_response: no authctxt");
284 kbdintctxt = authctxt->kbdintctxt;
285 if (kbdintctxt == NULL || kbdintctxt->ctxt == NULL)
286 fatal("input_userauth_info_response: no kbdintctxt");
287 if (kbdintctxt->device == NULL)
288 fatal("input_userauth_info_response: no device");
290 authctxt->postponed = 0; /* reset */
291 nresp = packet_get_int();
292 if (nresp != kbdintctxt->nreq)
293 fatal("input_userauth_info_response: wrong number of replies");
294 if (nresp > 100)
295 fatal("input_userauth_info_response: too many replies");
296 if (nresp > 0) {
297 response = xcalloc(nresp, sizeof(char *));
298 for (i = 0; i < nresp; i++)
299 response[i] = packet_get_string(NULL);
301 packet_check_eom();
303 res = kbdintctxt->device->respond(kbdintctxt->ctxt, nresp, response);
305 for (i = 0; i < nresp; i++) {
306 memset(response[i], 'r', strlen(response[i]));
307 xfree(response[i]);
309 if (response)
310 xfree(response);
312 switch (res) {
313 case 0:
314 /* Success! */
315 authenticated = authctxt->valid ? 1 : 0;
316 break;
317 case 1:
318 /* Authentication needs further interaction */
319 if (send_userauth_info_request(authctxt) == 1)
320 authctxt->postponed = 1;
321 break;
322 default:
323 /* Failure! */
324 break;
327 len = strlen("keyboard-interactive") + 2 +
328 strlen(kbdintctxt->device->name);
329 method = xmalloc(len);
330 snprintf(method, len, "keyboard-interactive/%s",
331 kbdintctxt->device->name);
333 if (!authctxt->postponed) {
334 if (authenticated) {
335 auth2_challenge_stop(authctxt);
336 } else {
337 /* start next device */
338 /* may set authctxt->postponed */
339 auth2_challenge_start(authctxt);
342 userauth_finish(authctxt, authenticated, method);
343 xfree(method);
346 void
347 privsep_challenge_enable(void)
349 #if defined(BSD_AUTH) || defined(USE_PAM) || defined(SKEY)
350 int n = 0;
351 #endif
352 #ifdef BSD_AUTH
353 extern KbdintDevice mm_bsdauth_device;
354 #endif
355 #ifdef USE_PAM
356 extern KbdintDevice mm_sshpam_device;
357 #endif
358 #ifdef SKEY
359 extern KbdintDevice mm_skey_device;
360 #endif
362 #ifdef BSD_AUTH
363 devices[n++] = &mm_bsdauth_device;
364 #else
365 #ifdef USE_PAM
366 devices[n++] = &mm_sshpam_device;
367 #endif
368 #ifdef SKEY
369 devices[n++] = &mm_skey_device;
370 #endif
371 #endif