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