]> andersk Git - openssh.git/blame - auth2-chall.c
- stevesk@cvs.openbsd.org 2006/08/01 23:22:48
[openssh.git] / auth2-chall.c
CommitLineData
cf851879 1/* $OpenBSD: auth2-chall.c,v 1.29 2006/08/01 23:22:47 stevesk Exp $ */
59c97189 2/*
f3c7c613 3 * Copyright (c) 2001 Markus Friedl. All rights reserved.
5ba55ada 4 * Copyright (c) 2001 Per Allansson. All rights reserved.
59c97189 5 *
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.
14 *
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.
25 */
26#include "includes.h"
59c97189 27
cf851879 28#include <stdio.h>
00146caa 29#include <string.h>
30
59c97189 31#include "ssh2.h"
32#include "auth.h"
3469eac4 33#include "buffer.h"
59c97189 34#include "packet.h"
35#include "xmalloc.h"
36#include "dispatch.h"
42f11eb2 37#include "log.h"
c384a74c 38#include "servconf.h"
39
40/* import */
41extern ServerOptions options;
59c97189 42
396c147e 43static int auth2_challenge_start(Authctxt *);
44static int send_userauth_info_request(Authctxt *);
7819b5c3 45static void input_userauth_info_response(int, u_int32_t, void *);
5ba55ada 46
47#ifdef BSD_AUTH
48extern KbdintDevice bsdauth_device;
49#else
05114c74 50#ifdef USE_PAM
51extern KbdintDevice sshpam_device;
52#endif
5ba55ada 53#ifdef SKEY
54extern KbdintDevice skey_device;
55#endif
56#endif
57
58KbdintDevice *devices[] = {
59#ifdef BSD_AUTH
60 &bsdauth_device,
61#else
05114c74 62#ifdef USE_PAM
63 &sshpam_device,
64#endif
5ba55ada 65#ifdef SKEY
66 &skey_device,
67#endif
68#endif
69 NULL
70};
71
72typedef struct KbdintAuthctxt KbdintAuthctxt;
73struct KbdintAuthctxt
74{
75 char *devices;
76 void *ctxt;
77 KbdintDevice *device;
477edc5d 78 u_int nreq;
5ba55ada 79};
80
c384a74c 81#ifdef USE_PAM
82void
83remove_kbdint_device(const char *devname)
84{
85 int i, j;
86
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--;
92 }
93}
94#endif
95
396c147e 96static KbdintAuthctxt *
5ba55ada 97kbdint_alloc(const char *devs)
98{
99 KbdintAuthctxt *kbdintctxt;
3469eac4 100 Buffer b;
5ba55ada 101 int i;
5ba55ada 102
c384a74c 103#ifdef USE_PAM
104 if (!options.use_pam)
105 remove_kbdint_device("pam");
106#endif
107
5ba55ada 108 kbdintctxt = xmalloc(sizeof(KbdintAuthctxt));
109 if (strcmp(devs, "") == 0) {
3469eac4 110 buffer_init(&b);
5ba55ada 111 for (i = 0; devices[i]; i++) {
3469eac4 112 if (buffer_len(&b) > 0)
113 buffer_append(&b, ",", 1);
114 buffer_append(&b, devices[i]->name,
115 strlen(devices[i]->name));
5ba55ada 116 }
3469eac4 117 buffer_append(&b, "\0", 1);
118 kbdintctxt->devices = xstrdup(buffer_ptr(&b));
119 buffer_free(&b);
5ba55ada 120 } else {
121 kbdintctxt->devices = xstrdup(devs);
122 }
3469eac4 123 debug("kbdint_alloc: devices '%s'", kbdintctxt->devices);
5ba55ada 124 kbdintctxt->ctxt = NULL;
125 kbdintctxt->device = NULL;
477edc5d 126 kbdintctxt->nreq = 0;
5ba55ada 127
128 return kbdintctxt;
129}
396c147e 130static void
5ba55ada 131kbdint_reset_device(KbdintAuthctxt *kbdintctxt)
132{
133 if (kbdintctxt->ctxt) {
134 kbdintctxt->device->free_ctx(kbdintctxt->ctxt);
135 kbdintctxt->ctxt = NULL;
136 }
137 kbdintctxt->device = NULL;
138}
396c147e 139static void
5ba55ada 140kbdint_free(KbdintAuthctxt *kbdintctxt)
141{
142 if (kbdintctxt->device)
143 kbdint_reset_device(kbdintctxt);
144 if (kbdintctxt->devices) {
145 xfree(kbdintctxt->devices);
146 kbdintctxt->devices = NULL;
147 }
148 xfree(kbdintctxt);
149}
150/* get next device */
396c147e 151static int
5ba55ada 152kbdint_next_device(KbdintAuthctxt *kbdintctxt)
153{
154 size_t len;
155 char *t;
156 int i;
157
158 if (kbdintctxt->device)
159 kbdint_reset_device(kbdintctxt);
160 do {
161 len = kbdintctxt->devices ?
162 strcspn(kbdintctxt->devices, ",") : 0;
163
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 ?
4e2e5cfd 173 kbdintctxt->devices : "<empty>");
5ba55ada 174 } while (kbdintctxt->devices && !kbdintctxt->device);
175
176 return kbdintctxt->device ? 1 : 0;
177}
59c97189 178
179/*
aa8003d6 180 * try challenge-response, set authctxt->postponed if we have to
59c97189 181 * wait for the response.
182 */
183int
184auth2_challenge(Authctxt *authctxt, char *devs)
185{
5ba55ada 186 debug("auth2_challenge: user=%s devs=%s",
187 authctxt->user ? authctxt->user : "<nouser>",
188 devs ? devs : "<no devs>");
189
f5e69c65 190 if (authctxt->user == NULL || !devs)
5ba55ada 191 return 0;
184eed6a 192 if (authctxt->kbdintctxt == NULL)
5ba55ada 193 authctxt->kbdintctxt = kbdint_alloc(devs);
194 return auth2_challenge_start(authctxt);
195}
196
2f293d43 197/* unregister kbd-int callbacks and context */
198void
199auth2_challenge_stop(Authctxt *authctxt)
200{
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;
206 }
207}
208
5ba55ada 209/* side effect: sets authctxt->postponed if a reply was sent*/
210static int
211auth2_challenge_start(Authctxt *authctxt)
212{
213 KbdintAuthctxt *kbdintctxt = authctxt->kbdintctxt;
214
215 debug2("auth2_challenge_start: devices %s",
216 kbdintctxt->devices ? kbdintctxt->devices : "<empty>");
217
218 if (kbdint_next_device(kbdintctxt) == 0) {
2f293d43 219 auth2_challenge_stop(authctxt);
5ba55ada 220 return 0;
221 }
222 debug("auth2_challenge_start: trying authentication method '%s'",
223 kbdintctxt->device->name);
59c97189 224
5ba55ada 225 if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) {
2f293d43 226 auth2_challenge_stop(authctxt);
59c97189 227 return 0;
5ba55ada 228 }
229 if (send_userauth_info_request(authctxt) == 0) {
2f293d43 230 auth2_challenge_stop(authctxt);
59c97189 231 return 0;
5ba55ada 232 }
59c97189 233 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE,
234 &input_userauth_info_response);
5ba55ada 235
59c97189 236 authctxt->postponed = 1;
237 return 0;
238}
239
5ba55ada 240static int
241send_userauth_info_request(Authctxt *authctxt)
59c97189 242{
5ba55ada 243 KbdintAuthctxt *kbdintctxt;
244 char *name, *instr, **prompts;
2ceb8101 245 u_int i, *echo_on;
5ba55ada 246
247 kbdintctxt = authctxt->kbdintctxt;
248 if (kbdintctxt->device->query(kbdintctxt->ctxt,
477edc5d 249 &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on))
5ba55ada 250 return 0;
59c97189 251
252 packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST);
5ba55ada 253 packet_put_cstring(name);
254 packet_put_cstring(instr);
7203d6bb 255 packet_put_cstring(""); /* language not used */
477edc5d 256 packet_put_int(kbdintctxt->nreq);
257 for (i = 0; i < kbdintctxt->nreq; i++) {
5ba55ada 258 packet_put_cstring(prompts[i]);
259 packet_put_char(echo_on[i]);
260 }
59c97189 261 packet_send();
262 packet_write_wait();
5ba55ada 263
477edc5d 264 for (i = 0; i < kbdintctxt->nreq; i++)
5ba55ada 265 xfree(prompts[i]);
266 xfree(prompts);
267 xfree(echo_on);
268 xfree(name);
269 xfree(instr);
270 return 1;
59c97189 271}
272
5ba55ada 273static void
7819b5c3 274input_userauth_info_response(int type, u_int32_t seq, void *ctxt)
59c97189 275{
276 Authctxt *authctxt = ctxt;
5ba55ada 277 KbdintAuthctxt *kbdintctxt;
2ceb8101 278 int authenticated = 0, res, len;
279 u_int i, nresp;
5ba55ada 280 char **response = NULL, *method;
59c97189 281
282 if (authctxt == NULL)
283 fatal("input_userauth_info_response: no authctxt");
5ba55ada 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");
59c97189 289
290 authctxt->postponed = 0; /* reset */
291 nresp = packet_get_int();
477edc5d 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");
5ba55ada 296 if (nresp > 0) {
52e3daed 297 response = xcalloc(nresp, sizeof(char *));
5ba55ada 298 for (i = 0; i < nresp; i++)
299 response[i] = packet_get_string(NULL);
300 }
95500969 301 packet_check_eom();
5ba55ada 302
37ea4f91 303 res = kbdintctxt->device->respond(kbdintctxt->ctxt, nresp, response);
5ba55ada 304
305 for (i = 0; i < nresp; i++) {
306 memset(response[i], 'r', strlen(response[i]));
307 xfree(response[i]);
308 }
309 if (response)
59c97189 310 xfree(response);
5ba55ada 311
312 switch (res) {
313 case 0:
314 /* Success! */
37ea4f91 315 authenticated = authctxt->valid ? 1 : 0;
5ba55ada 316 break;
317 case 1:
318 /* Authentication needs further interaction */
2f293d43 319 if (send_userauth_info_request(authctxt) == 1)
320 authctxt->postponed = 1;
5ba55ada 321 break;
322 default:
323 /* Failure! */
324 break;
59c97189 325 }
5ba55ada 326
327 len = strlen("keyboard-interactive") + 2 +
328 strlen(kbdintctxt->device->name);
329 method = xmalloc(len);
bd2d2ac4 330 snprintf(method, len, "keyboard-interactive/%s",
331 kbdintctxt->device->name);
5ba55ada 332
333 if (!authctxt->postponed) {
5ba55ada 334 if (authenticated) {
2f293d43 335 auth2_challenge_stop(authctxt);
5ba55ada 336 } else {
337 /* start next device */
338 /* may set authctxt->postponed */
339 auth2_challenge_start(authctxt);
340 }
341 }
d9cd3575 342 userauth_finish(authctxt, authenticated, method);
5ba55ada 343 xfree(method);
59c97189 344}
1853d1ef 345
346void
347privsep_challenge_enable(void)
348{
4881aebb 349#if defined(BSD_AUTH) || defined(USE_PAM) || defined(SKEY)
350 int n = 0;
351#endif
1853d1ef 352#ifdef BSD_AUTH
353 extern KbdintDevice mm_bsdauth_device;
354#endif
05114c74 355#ifdef USE_PAM
356 extern KbdintDevice mm_sshpam_device;
357#endif
1853d1ef 358#ifdef SKEY
359 extern KbdintDevice mm_skey_device;
360#endif
05114c74 361
1853d1ef 362#ifdef BSD_AUTH
05114c74 363 devices[n++] = &mm_bsdauth_device;
1853d1ef 364#else
05114c74 365#ifdef USE_PAM
366 devices[n++] = &mm_sshpam_device;
367#endif
1853d1ef 368#ifdef SKEY
05114c74 369 devices[n++] = &mm_skey_device;
1853d1ef 370#endif
371#endif
372}
This page took 0.208384 seconds and 5 git commands to generate.