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