Import OpenSSH-5.1p1.
[dragonfly.git] / crypto / openssh-3.9p1 / auth2-chall.c
blob486baaaa332e0a7c9b35dd7f261a53d452614ba5
1 /*
2 * Copyright (c) 2001 Markus Friedl. All rights reserved.
3 * Copyright (c) 2001 Per Allansson. All rights reserved.
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.
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.
25 #include "includes.h"
26 RCSID("$OpenBSD: auth2-chall.c,v 1.21 2004/06/01 14:20:45 dtucker Exp $");
28 #include "ssh2.h"
29 #include "auth.h"
30 #include "buffer.h"
31 #include "packet.h"
32 #include "xmalloc.h"
33 #include "dispatch.h"
34 #include "log.h"
36 static int auth2_challenge_start(Authctxt *);
37 static int send_userauth_info_request(Authctxt *);
38 static void input_userauth_info_response(int, u_int32_t, void *);
40 #ifdef BSD_AUTH
41 extern KbdintDevice bsdauth_device;
42 #else
43 #ifdef USE_PAM
44 extern KbdintDevice sshpam_device;
45 #endif
46 #ifdef SKEY
47 extern KbdintDevice skey_device;
48 #endif
49 #endif
51 KbdintDevice *devices[] = {
52 #ifdef BSD_AUTH
53 &bsdauth_device,
54 #else
55 #ifdef USE_PAM
56 &sshpam_device,
57 #endif
58 #ifdef SKEY
59 &skey_device,
60 #endif
61 #endif
62 NULL
65 typedef struct KbdintAuthctxt KbdintAuthctxt;
66 struct KbdintAuthctxt
68 char *devices;
69 void *ctxt;
70 KbdintDevice *device;
71 u_int nreq;
74 static KbdintAuthctxt *
75 kbdint_alloc(const char *devs)
77 KbdintAuthctxt *kbdintctxt;
78 Buffer b;
79 int i;
81 kbdintctxt = xmalloc(sizeof(KbdintAuthctxt));
82 if (strcmp(devs, "") == 0) {
83 buffer_init(&b);
84 for (i = 0; devices[i]; i++) {
85 if (buffer_len(&b) > 0)
86 buffer_append(&b, ",", 1);
87 buffer_append(&b, devices[i]->name,
88 strlen(devices[i]->name));
90 buffer_append(&b, "\0", 1);
91 kbdintctxt->devices = xstrdup(buffer_ptr(&b));
92 buffer_free(&b);
93 } else {
94 kbdintctxt->devices = xstrdup(devs);
96 debug("kbdint_alloc: devices '%s'", kbdintctxt->devices);
97 kbdintctxt->ctxt = NULL;
98 kbdintctxt->device = NULL;
99 kbdintctxt->nreq = 0;
101 return kbdintctxt;
103 static void
104 kbdint_reset_device(KbdintAuthctxt *kbdintctxt)
106 if (kbdintctxt->ctxt) {
107 kbdintctxt->device->free_ctx(kbdintctxt->ctxt);
108 kbdintctxt->ctxt = NULL;
110 kbdintctxt->device = NULL;
112 static void
113 kbdint_free(KbdintAuthctxt *kbdintctxt)
115 if (kbdintctxt->device)
116 kbdint_reset_device(kbdintctxt);
117 if (kbdintctxt->devices) {
118 xfree(kbdintctxt->devices);
119 kbdintctxt->devices = NULL;
121 xfree(kbdintctxt);
123 /* get next device */
124 static int
125 kbdint_next_device(KbdintAuthctxt *kbdintctxt)
127 size_t len;
128 char *t;
129 int i;
131 if (kbdintctxt->device)
132 kbdint_reset_device(kbdintctxt);
133 do {
134 len = kbdintctxt->devices ?
135 strcspn(kbdintctxt->devices, ",") : 0;
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);
149 return kbdintctxt->device ? 1 : 0;
153 * try challenge-response, set authctxt->postponed if we have to
154 * wait for the response.
157 auth2_challenge(Authctxt *authctxt, char *devs)
159 debug("auth2_challenge: user=%s devs=%s",
160 authctxt->user ? authctxt->user : "<nouser>",
161 devs ? devs : "<no devs>");
163 if (authctxt->user == NULL || !devs)
164 return 0;
165 if (authctxt->kbdintctxt == NULL)
166 authctxt->kbdintctxt = kbdint_alloc(devs);
167 return auth2_challenge_start(authctxt);
170 /* unregister kbd-int callbacks and context */
171 void
172 auth2_challenge_stop(Authctxt *authctxt)
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;
182 /* side effect: sets authctxt->postponed if a reply was sent*/
183 static int
184 auth2_challenge_start(Authctxt *authctxt)
186 KbdintAuthctxt *kbdintctxt = authctxt->kbdintctxt;
188 debug2("auth2_challenge_start: devices %s",
189 kbdintctxt->devices ? kbdintctxt->devices : "<empty>");
191 if (kbdint_next_device(kbdintctxt) == 0) {
192 auth2_challenge_stop(authctxt);
193 return 0;
195 debug("auth2_challenge_start: trying authentication method '%s'",
196 kbdintctxt->device->name);
198 if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) {
199 auth2_challenge_stop(authctxt);
200 return 0;
202 if (send_userauth_info_request(authctxt) == 0) {
203 auth2_challenge_stop(authctxt);
204 return 0;
206 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE,
207 &input_userauth_info_response);
209 authctxt->postponed = 1;
210 return 0;
213 static int
214 send_userauth_info_request(Authctxt *authctxt)
216 KbdintAuthctxt *kbdintctxt;
217 char *name, *instr, **prompts;
218 int i;
219 u_int *echo_on;
221 kbdintctxt = authctxt->kbdintctxt;
222 if (kbdintctxt->device->query(kbdintctxt->ctxt,
223 &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on))
224 return 0;
226 packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST);
227 packet_put_cstring(name);
228 packet_put_cstring(instr);
229 packet_put_cstring(""); /* language not used */
230 packet_put_int(kbdintctxt->nreq);
231 for (i = 0; i < kbdintctxt->nreq; i++) {
232 packet_put_cstring(prompts[i]);
233 packet_put_char(echo_on[i]);
235 packet_send();
236 packet_write_wait();
238 for (i = 0; i < kbdintctxt->nreq; i++)
239 xfree(prompts[i]);
240 xfree(prompts);
241 xfree(echo_on);
242 xfree(name);
243 xfree(instr);
244 return 1;
247 static void
248 input_userauth_info_response(int type, u_int32_t seq, void *ctxt)
250 Authctxt *authctxt = ctxt;
251 KbdintAuthctxt *kbdintctxt;
252 int i, authenticated = 0, res, len;
253 u_int nresp;
254 char **response = NULL, *method;
256 if (authctxt == NULL)
257 fatal("input_userauth_info_response: no authctxt");
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");
264 authctxt->postponed = 0; /* reset */
265 nresp = packet_get_int();
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");
270 if (nresp > 0) {
271 response = xmalloc(nresp * sizeof(char *));
272 for (i = 0; i < nresp; i++)
273 response[i] = packet_get_string(NULL);
275 packet_check_eom();
277 if (authctxt->valid) {
278 res = kbdintctxt->device->respond(kbdintctxt->ctxt,
279 nresp, response);
280 } else {
281 res = -1;
284 for (i = 0; i < nresp; i++) {
285 memset(response[i], 'r', strlen(response[i]));
286 xfree(response[i]);
288 if (response)
289 xfree(response);
291 switch (res) {
292 case 0:
293 /* Success! */
294 authenticated = 1;
295 break;
296 case 1:
297 /* Authentication needs further interaction */
298 if (send_userauth_info_request(authctxt) == 1)
299 authctxt->postponed = 1;
300 break;
301 default:
302 /* Failure! */
303 break;
306 len = strlen("keyboard-interactive") + 2 +
307 strlen(kbdintctxt->device->name);
308 method = xmalloc(len);
309 snprintf(method, len, "keyboard-interactive/%s",
310 kbdintctxt->device->name);
312 if (!authctxt->postponed) {
313 if (authenticated) {
314 auth2_challenge_stop(authctxt);
315 } else {
316 /* start next device */
317 /* may set authctxt->postponed */
318 auth2_challenge_start(authctxt);
321 userauth_finish(authctxt, authenticated, method);
322 xfree(method);
325 void
326 privsep_challenge_enable(void)
328 #if defined(BSD_AUTH) || defined(USE_PAM) || defined(SKEY)
329 int n = 0;
330 #endif
331 #ifdef BSD_AUTH
332 extern KbdintDevice mm_bsdauth_device;
333 #endif
334 #ifdef USE_PAM
335 extern KbdintDevice mm_sshpam_device;
336 #endif
337 #ifdef SKEY
338 extern KbdintDevice mm_skey_device;
339 #endif
341 #ifdef BSD_AUTH
342 devices[n++] = &mm_bsdauth_device;
343 #else
344 #ifdef USE_PAM
345 devices[n++] = &mm_sshpam_device;
346 #endif
347 #ifdef SKEY
348 devices[n++] = &mm_skey_device;
349 #endif
350 #endif