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