]> andersk Git - openssh.git/blame - auth2-chall.c
- djm@cvs.openbsd.org 2006/03/25 00:05:41
[openssh.git] / auth2-chall.c
CommitLineData
59c97189 1/*
f3c7c613 2 * Copyright (c) 2001 Markus Friedl. All rights reserved.
5ba55ada 3 * Copyright (c) 2001 Per Allansson. All rights reserved.
59c97189 4 *
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"
59c97189 26
59c97189 27#include "ssh2.h"
28#include "auth.h"
3469eac4 29#include "buffer.h"
59c97189 30#include "packet.h"
31#include "xmalloc.h"
32#include "dispatch.h"
42f11eb2 33#include "log.h"
c384a74c 34#include "servconf.h"
35
36/* import */
37extern ServerOptions options;
59c97189 38
396c147e 39static int auth2_challenge_start(Authctxt *);
40static int send_userauth_info_request(Authctxt *);
7819b5c3 41static void input_userauth_info_response(int, u_int32_t, void *);
5ba55ada 42
43#ifdef BSD_AUTH
44extern KbdintDevice bsdauth_device;
45#else
05114c74 46#ifdef USE_PAM
47extern KbdintDevice sshpam_device;
48#endif
5ba55ada 49#ifdef SKEY
50extern KbdintDevice skey_device;
51#endif
52#endif
53
54KbdintDevice *devices[] = {
55#ifdef BSD_AUTH
56 &bsdauth_device,
57#else
05114c74 58#ifdef USE_PAM
59 &sshpam_device,
60#endif
5ba55ada 61#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;
477edc5d 74 u_int nreq;
5ba55ada 75};
76
c384a74c 77#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
396c147e 92static KbdintAuthctxt *
5ba55ada 93kbdint_alloc(const char *devs)
94{
95 KbdintAuthctxt *kbdintctxt;
3469eac4 96 Buffer b;
5ba55ada 97 int i;
5ba55ada 98
c384a74c 99#ifdef USE_PAM
100 if (!options.use_pam)
101 remove_kbdint_device("pam");
102#endif
103
5ba55ada 104 kbdintctxt = xmalloc(sizeof(KbdintAuthctxt));
105 if (strcmp(devs, "") == 0) {
3469eac4 106 buffer_init(&b);
5ba55ada 107 for (i = 0; devices[i]; i++) {
3469eac4 108 if (buffer_len(&b) > 0)
109 buffer_append(&b, ",", 1);
110 buffer_append(&b, devices[i]->name,
111 strlen(devices[i]->name));
5ba55ada 112 }
3469eac4 113 buffer_append(&b, "\0", 1);
114 kbdintctxt->devices = xstrdup(buffer_ptr(&b));
115 buffer_free(&b);
5ba55ada 116 } else {
117 kbdintctxt->devices = xstrdup(devs);
118 }
3469eac4 119 debug("kbdint_alloc: devices '%s'", kbdintctxt->devices);
5ba55ada 120 kbdintctxt->ctxt = NULL;
121 kbdintctxt->device = NULL;
477edc5d 122 kbdintctxt->nreq = 0;
5ba55ada 123
124 return kbdintctxt;
125}
396c147e 126static void
5ba55ada 127kbdint_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}
396c147e 135static void
5ba55ada 136kbdint_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 */
396c147e 147static int
5ba55ada 148kbdint_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 ?
4e2e5cfd 169 kbdintctxt->devices : "<empty>");
5ba55ada 170 } while (kbdintctxt->devices && !kbdintctxt->device);
171
172 return kbdintctxt->device ? 1 : 0;
173}
59c97189 174
175/*
aa8003d6 176 * try challenge-response, set authctxt->postponed if we have to
59c97189 177 * wait for the response.
178 */
179int
180auth2_challenge(Authctxt *authctxt, char *devs)
181{
5ba55ada 182 debug("auth2_challenge: user=%s devs=%s",
183 authctxt->user ? authctxt->user : "<nouser>",
184 devs ? devs : "<no devs>");
185
f5e69c65 186 if (authctxt->user == NULL || !devs)
5ba55ada 187 return 0;
184eed6a 188 if (authctxt->kbdintctxt == NULL)
5ba55ada 189 authctxt->kbdintctxt = kbdint_alloc(devs);
190 return auth2_challenge_start(authctxt);
191}
192
2f293d43 193/* 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
5ba55ada 205/* 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) {
2f293d43 215 auth2_challenge_stop(authctxt);
5ba55ada 216 return 0;
217 }
218 debug("auth2_challenge_start: trying authentication method '%s'",
219 kbdintctxt->device->name);
59c97189 220
5ba55ada 221 if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) {
2f293d43 222 auth2_challenge_stop(authctxt);
59c97189 223 return 0;
5ba55ada 224 }
225 if (send_userauth_info_request(authctxt) == 0) {
2f293d43 226 auth2_challenge_stop(authctxt);
59c97189 227 return 0;
5ba55ada 228 }
59c97189 229 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE,
230 &input_userauth_info_response);
5ba55ada 231
59c97189 232 authctxt->postponed = 1;
233 return 0;
234}
235
5ba55ada 236static int
237send_userauth_info_request(Authctxt *authctxt)
59c97189 238{
5ba55ada 239 KbdintAuthctxt *kbdintctxt;
240 char *name, *instr, **prompts;
2ceb8101 241 u_int i, *echo_on;
5ba55ada 242
243 kbdintctxt = authctxt->kbdintctxt;
244 if (kbdintctxt->device->query(kbdintctxt->ctxt,
477edc5d 245 &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on))
5ba55ada 246 return 0;
59c97189 247
248 packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST);
5ba55ada 249 packet_put_cstring(name);
250 packet_put_cstring(instr);
7203d6bb 251 packet_put_cstring(""); /* language not used */
477edc5d 252 packet_put_int(kbdintctxt->nreq);
253 for (i = 0; i < kbdintctxt->nreq; i++) {
5ba55ada 254 packet_put_cstring(prompts[i]);
255 packet_put_char(echo_on[i]);
256 }
59c97189 257 packet_send();
258 packet_write_wait();
5ba55ada 259
477edc5d 260 for (i = 0; i < kbdintctxt->nreq; i++)
5ba55ada 261 xfree(prompts[i]);
262 xfree(prompts);
263 xfree(echo_on);
264 xfree(name);
265 xfree(instr);
266 return 1;
59c97189 267}
268
5ba55ada 269static void
7819b5c3 270input_userauth_info_response(int type, u_int32_t seq, void *ctxt)
59c97189 271{
272 Authctxt *authctxt = ctxt;
5ba55ada 273 KbdintAuthctxt *kbdintctxt;
2ceb8101 274 int authenticated = 0, res, len;
275 u_int i, nresp;
5ba55ada 276 char **response = NULL, *method;
59c97189 277
278 if (authctxt == NULL)
279 fatal("input_userauth_info_response: no authctxt");
5ba55ada 280 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");
59c97189 285
286 authctxt->postponed = 0; /* reset */
287 nresp = packet_get_int();
477edc5d 288 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");
5ba55ada 292 if (nresp > 0) {
52e3daed 293 response = xcalloc(nresp, sizeof(char *));
5ba55ada 294 for (i = 0; i < nresp; i++)
295 response[i] = packet_get_string(NULL);
296 }
95500969 297 packet_check_eom();
5ba55ada 298
37ea4f91 299 res = kbdintctxt->device->respond(kbdintctxt->ctxt, nresp, response);
5ba55ada 300
301 for (i = 0; i < nresp; i++) {
302 memset(response[i], 'r', strlen(response[i]));
303 xfree(response[i]);
304 }
305 if (response)
59c97189 306 xfree(response);
5ba55ada 307
308 switch (res) {
309 case 0:
310 /* Success! */
37ea4f91 311 authenticated = authctxt->valid ? 1 : 0;
5ba55ada 312 break;
313 case 1:
314 /* Authentication needs further interaction */
2f293d43 315 if (send_userauth_info_request(authctxt) == 1)
316 authctxt->postponed = 1;
5ba55ada 317 break;
318 default:
319 /* Failure! */
320 break;
59c97189 321 }
5ba55ada 322
323 len = strlen("keyboard-interactive") + 2 +
324 strlen(kbdintctxt->device->name);
325 method = xmalloc(len);
bd2d2ac4 326 snprintf(method, len, "keyboard-interactive/%s",
327 kbdintctxt->device->name);
5ba55ada 328
329 if (!authctxt->postponed) {
5ba55ada 330 if (authenticated) {
2f293d43 331 auth2_challenge_stop(authctxt);
5ba55ada 332 } else {
333 /* start next device */
334 /* may set authctxt->postponed */
335 auth2_challenge_start(authctxt);
336 }
337 }
d9cd3575 338 userauth_finish(authctxt, authenticated, method);
5ba55ada 339 xfree(method);
59c97189 340}
1853d1ef 341
342void
343privsep_challenge_enable(void)
344{
4881aebb 345#if defined(BSD_AUTH) || defined(USE_PAM) || defined(SKEY)
346 int n = 0;
347#endif
1853d1ef 348#ifdef BSD_AUTH
349 extern KbdintDevice mm_bsdauth_device;
350#endif
05114c74 351#ifdef USE_PAM
352 extern KbdintDevice mm_sshpam_device;
353#endif
1853d1ef 354#ifdef SKEY
355 extern KbdintDevice mm_skey_device;
356#endif
05114c74 357
1853d1ef 358#ifdef BSD_AUTH
05114c74 359 devices[n++] = &mm_bsdauth_device;
1853d1ef 360#else
05114c74 361#ifdef USE_PAM
362 devices[n++] = &mm_sshpam_device;
363#endif
1853d1ef 364#ifdef SKEY
05114c74 365 devices[n++] = &mm_skey_device;
1853d1ef 366#endif
367#endif
368}
This page took 0.209948 seconds and 5 git commands to generate.