- (djm) [openbsd-compat/glob.c]
[openssh-git.git] / auth2-chall.c
blobad6b7233fe9309796427666b8a1293ee199c40bd
1 /* $OpenBSD: auth2-chall.c,v 1.28 2006/07/22 20:48:22 stevesk Exp $ */
2 /*
3 * Copyright (c) 2001 Markus Friedl. All rights reserved.
4 * Copyright (c) 2001 Per Allansson. All rights reserved.
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.
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.
26 #include "includes.h"
28 #include <string.h>
30 #include "ssh2.h"
31 #include "auth.h"
32 #include "buffer.h"
33 #include "packet.h"
34 #include "xmalloc.h"
35 #include "dispatch.h"
36 #include "log.h"
37 #include "servconf.h"
39 /* import */
40 extern ServerOptions options;
42 static int auth2_challenge_start(Authctxt *);
43 static int send_userauth_info_request(Authctxt *);
44 static void input_userauth_info_response(int, u_int32_t, void *);
46 #ifdef BSD_AUTH
47 extern KbdintDevice bsdauth_device;
48 #else
49 #ifdef USE_PAM
50 extern KbdintDevice sshpam_device;
51 #endif
52 #ifdef SKEY
53 extern KbdintDevice skey_device;
54 #endif
55 #endif
57 KbdintDevice *devices[] = {
58 #ifdef BSD_AUTH
59 &bsdauth_device,
60 #else
61 #ifdef USE_PAM
62 &sshpam_device,
63 #endif
64 #ifdef SKEY
65 &skey_device,
66 #endif
67 #endif
68 NULL
71 typedef struct KbdintAuthctxt KbdintAuthctxt;
72 struct KbdintAuthctxt
74 char *devices;
75 void *ctxt;
76 KbdintDevice *device;
77 u_int nreq;
80 #ifdef USE_PAM
81 void
82 remove_kbdint_device(const char *devname)
84 int i, j;
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--;
93 #endif
95 static KbdintAuthctxt *
96 kbdint_alloc(const char *devs)
98 KbdintAuthctxt *kbdintctxt;
99 Buffer b;
100 int i;
102 #ifdef USE_PAM
103 if (!options.use_pam)
104 remove_kbdint_device("pam");
105 #endif
107 kbdintctxt = xmalloc(sizeof(KbdintAuthctxt));
108 if (strcmp(devs, "") == 0) {
109 buffer_init(&b);
110 for (i = 0; devices[i]; i++) {
111 if (buffer_len(&b) > 0)
112 buffer_append(&b, ",", 1);
113 buffer_append(&b, devices[i]->name,
114 strlen(devices[i]->name));
116 buffer_append(&b, "\0", 1);
117 kbdintctxt->devices = xstrdup(buffer_ptr(&b));
118 buffer_free(&b);
119 } else {
120 kbdintctxt->devices = xstrdup(devs);
122 debug("kbdint_alloc: devices '%s'", kbdintctxt->devices);
123 kbdintctxt->ctxt = NULL;
124 kbdintctxt->device = NULL;
125 kbdintctxt->nreq = 0;
127 return kbdintctxt;
129 static void
130 kbdint_reset_device(KbdintAuthctxt *kbdintctxt)
132 if (kbdintctxt->ctxt) {
133 kbdintctxt->device->free_ctx(kbdintctxt->ctxt);
134 kbdintctxt->ctxt = NULL;
136 kbdintctxt->device = NULL;
138 static void
139 kbdint_free(KbdintAuthctxt *kbdintctxt)
141 if (kbdintctxt->device)
142 kbdint_reset_device(kbdintctxt);
143 if (kbdintctxt->devices) {
144 xfree(kbdintctxt->devices);
145 kbdintctxt->devices = NULL;
147 xfree(kbdintctxt);
149 /* get next device */
150 static int
151 kbdint_next_device(KbdintAuthctxt *kbdintctxt)
153 size_t len;
154 char *t;
155 int i;
157 if (kbdintctxt->device)
158 kbdint_reset_device(kbdintctxt);
159 do {
160 len = kbdintctxt->devices ?
161 strcspn(kbdintctxt->devices, ",") : 0;
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 ?
172 kbdintctxt->devices : "<empty>");
173 } while (kbdintctxt->devices && !kbdintctxt->device);
175 return kbdintctxt->device ? 1 : 0;
179 * try challenge-response, set authctxt->postponed if we have to
180 * wait for the response.
183 auth2_challenge(Authctxt *authctxt, char *devs)
185 debug("auth2_challenge: user=%s devs=%s",
186 authctxt->user ? authctxt->user : "<nouser>",
187 devs ? devs : "<no devs>");
189 if (authctxt->user == NULL || !devs)
190 return 0;
191 if (authctxt->kbdintctxt == NULL)
192 authctxt->kbdintctxt = kbdint_alloc(devs);
193 return auth2_challenge_start(authctxt);
196 /* unregister kbd-int callbacks and context */
197 void
198 auth2_challenge_stop(Authctxt *authctxt)
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;
208 /* side effect: sets authctxt->postponed if a reply was sent*/
209 static int
210 auth2_challenge_start(Authctxt *authctxt)
212 KbdintAuthctxt *kbdintctxt = authctxt->kbdintctxt;
214 debug2("auth2_challenge_start: devices %s",
215 kbdintctxt->devices ? kbdintctxt->devices : "<empty>");
217 if (kbdint_next_device(kbdintctxt) == 0) {
218 auth2_challenge_stop(authctxt);
219 return 0;
221 debug("auth2_challenge_start: trying authentication method '%s'",
222 kbdintctxt->device->name);
224 if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) {
225 auth2_challenge_stop(authctxt);
226 return 0;
228 if (send_userauth_info_request(authctxt) == 0) {
229 auth2_challenge_stop(authctxt);
230 return 0;
232 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE,
233 &input_userauth_info_response);
235 authctxt->postponed = 1;
236 return 0;
239 static int
240 send_userauth_info_request(Authctxt *authctxt)
242 KbdintAuthctxt *kbdintctxt;
243 char *name, *instr, **prompts;
244 u_int i, *echo_on;
246 kbdintctxt = authctxt->kbdintctxt;
247 if (kbdintctxt->device->query(kbdintctxt->ctxt,
248 &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on))
249 return 0;
251 packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST);
252 packet_put_cstring(name);
253 packet_put_cstring(instr);
254 packet_put_cstring(""); /* language not used */
255 packet_put_int(kbdintctxt->nreq);
256 for (i = 0; i < kbdintctxt->nreq; i++) {
257 packet_put_cstring(prompts[i]);
258 packet_put_char(echo_on[i]);
260 packet_send();
261 packet_write_wait();
263 for (i = 0; i < kbdintctxt->nreq; i++)
264 xfree(prompts[i]);
265 xfree(prompts);
266 xfree(echo_on);
267 xfree(name);
268 xfree(instr);
269 return 1;
272 static void
273 input_userauth_info_response(int type, u_int32_t seq, void *ctxt)
275 Authctxt *authctxt = ctxt;
276 KbdintAuthctxt *kbdintctxt;
277 int authenticated = 0, res, len;
278 u_int i, nresp;
279 char **response = NULL, *method;
281 if (authctxt == NULL)
282 fatal("input_userauth_info_response: no authctxt");
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");
289 authctxt->postponed = 0; /* reset */
290 nresp = packet_get_int();
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");
295 if (nresp > 0) {
296 response = xcalloc(nresp, sizeof(char *));
297 for (i = 0; i < nresp; i++)
298 response[i] = packet_get_string(NULL);
300 packet_check_eom();
302 res = kbdintctxt->device->respond(kbdintctxt->ctxt, nresp, response);
304 for (i = 0; i < nresp; i++) {
305 memset(response[i], 'r', strlen(response[i]));
306 xfree(response[i]);
308 if (response)
309 xfree(response);
311 switch (res) {
312 case 0:
313 /* Success! */
314 authenticated = authctxt->valid ? 1 : 0;
315 break;
316 case 1:
317 /* Authentication needs further interaction */
318 if (send_userauth_info_request(authctxt) == 1)
319 authctxt->postponed = 1;
320 break;
321 default:
322 /* Failure! */
323 break;
326 len = strlen("keyboard-interactive") + 2 +
327 strlen(kbdintctxt->device->name);
328 method = xmalloc(len);
329 snprintf(method, len, "keyboard-interactive/%s",
330 kbdintctxt->device->name);
332 if (!authctxt->postponed) {
333 if (authenticated) {
334 auth2_challenge_stop(authctxt);
335 } else {
336 /* start next device */
337 /* may set authctxt->postponed */
338 auth2_challenge_start(authctxt);
341 userauth_finish(authctxt, authenticated, method);
342 xfree(method);
345 void
346 privsep_challenge_enable(void)
348 #if defined(BSD_AUTH) || defined(USE_PAM) || defined(SKEY)
349 int n = 0;
350 #endif
351 #ifdef BSD_AUTH
352 extern KbdintDevice mm_bsdauth_device;
353 #endif
354 #ifdef USE_PAM
355 extern KbdintDevice mm_sshpam_device;
356 #endif
357 #ifdef SKEY
358 extern KbdintDevice mm_skey_device;
359 #endif
361 #ifdef BSD_AUTH
362 devices[n++] = &mm_bsdauth_device;
363 #else
364 #ifdef USE_PAM
365 devices[n++] = &mm_sshpam_device;
366 #endif
367 #ifdef SKEY
368 devices[n++] = &mm_skey_device;
369 #endif
370 #endif