auth2-chall.c revision 1.6 1 1.5 christos /* $NetBSD: auth2-chall.c,v 1.6 2014/10/19 16:30:58 christos Exp $ */
2 1.6 christos /* $OpenBSD: auth2-chall.c,v 1.41 2014/02/02 03:44:31 djm Exp $ */
3 1.1 christos /*
4 1.1 christos * Copyright (c) 2001 Markus Friedl. All rights reserved.
5 1.1 christos * Copyright (c) 2001 Per Allansson. All rights reserved.
6 1.1 christos *
7 1.1 christos * Redistribution and use in source and binary forms, with or without
8 1.1 christos * modification, are permitted provided that the following conditions
9 1.1 christos * are met:
10 1.1 christos * 1. Redistributions of source code must retain the above copyright
11 1.1 christos * notice, this list of conditions and the following disclaimer.
12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 christos * notice, this list of conditions and the following disclaimer in the
14 1.1 christos * documentation and/or other materials provided with the distribution.
15 1.1 christos *
16 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1 christos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1 christos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1 christos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1 christos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1 christos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1 christos */
27 1.1 christos
28 1.2 christos #include "includes.h"
29 1.5 christos __RCSID("$NetBSD: auth2-chall.c,v 1.6 2014/10/19 16:30:58 christos Exp $");
30 1.1 christos #include <sys/types.h>
31 1.1 christos
32 1.1 christos #include <stdio.h>
33 1.1 christos #include <string.h>
34 1.1 christos
35 1.1 christos #include "xmalloc.h"
36 1.1 christos #include "ssh2.h"
37 1.1 christos #include "key.h"
38 1.1 christos #include "hostfile.h"
39 1.1 christos #include "auth.h"
40 1.1 christos #include "buffer.h"
41 1.1 christos #include "packet.h"
42 1.1 christos #include "dispatch.h"
43 1.1 christos #include "log.h"
44 1.6 christos #include "misc.h"
45 1.2 christos #include "servconf.h"
46 1.2 christos
47 1.2 christos /* import */
48 1.2 christos extern ServerOptions options;
49 1.1 christos
50 1.1 christos static int auth2_challenge_start(Authctxt *);
51 1.1 christos static int send_userauth_info_request(Authctxt *);
52 1.1 christos static void input_userauth_info_response(int, u_int32_t, void *);
53 1.1 christos
54 1.2 christos #ifdef BSD_AUTH
55 1.1 christos extern KbdintDevice bsdauth_device;
56 1.2 christos #else
57 1.2 christos #ifdef USE_PAM
58 1.2 christos extern KbdintDevice sshpam_device;
59 1.2 christos #endif
60 1.2 christos #ifdef SKEY
61 1.2 christos extern KbdintDevice skey_device;
62 1.2 christos #endif
63 1.2 christos #endif
64 1.1 christos
65 1.1 christos KbdintDevice *devices[] = {
66 1.2 christos #ifdef BSD_AUTH
67 1.1 christos &bsdauth_device,
68 1.2 christos #else
69 1.2 christos #ifdef USE_PAM
70 1.2 christos &sshpam_device,
71 1.2 christos #endif
72 1.2 christos #ifdef SKEY
73 1.2 christos &skey_device,
74 1.2 christos #endif
75 1.2 christos #endif
76 1.1 christos NULL
77 1.1 christos };
78 1.1 christos
79 1.1 christos typedef struct KbdintAuthctxt KbdintAuthctxt;
80 1.1 christos struct KbdintAuthctxt
81 1.1 christos {
82 1.1 christos char *devices;
83 1.1 christos void *ctxt;
84 1.1 christos KbdintDevice *device;
85 1.1 christos u_int nreq;
86 1.1 christos };
87 1.1 christos
88 1.2 christos #ifdef USE_PAM
89 1.2 christos void remove_kbdint_device(const char *);
90 1.2 christos void
91 1.3 christos remove_kbdint_device(const char *xdevname)
92 1.2 christos {
93 1.2 christos int i, j;
94 1.2 christos
95 1.2 christos for (i = 0; devices[i] != NULL; i++)
96 1.3 christos if (strcmp(devices[i]->name, xdevname) == 0) {
97 1.2 christos for (j = i; devices[j] != NULL; j++)
98 1.2 christos devices[j] = devices[j+1];
99 1.2 christos i--;
100 1.2 christos }
101 1.2 christos }
102 1.2 christos #endif
103 1.2 christos
104 1.1 christos static KbdintAuthctxt *
105 1.1 christos kbdint_alloc(const char *devs)
106 1.1 christos {
107 1.1 christos KbdintAuthctxt *kbdintctxt;
108 1.1 christos Buffer b;
109 1.1 christos int i;
110 1.1 christos
111 1.2 christos #ifdef USE_PAM
112 1.2 christos if (!options.use_pam)
113 1.2 christos remove_kbdint_device("pam");
114 1.2 christos #endif
115 1.2 christos
116 1.5 christos kbdintctxt = xcalloc(1, sizeof(KbdintAuthctxt));
117 1.1 christos if (strcmp(devs, "") == 0) {
118 1.1 christos buffer_init(&b);
119 1.1 christos for (i = 0; devices[i]; i++) {
120 1.1 christos if (buffer_len(&b) > 0)
121 1.1 christos buffer_append(&b, ",", 1);
122 1.1 christos buffer_append(&b, devices[i]->name,
123 1.1 christos strlen(devices[i]->name));
124 1.1 christos }
125 1.1 christos buffer_append(&b, "\0", 1);
126 1.6 christos kbdintctxt->devices = xstrdup((const char *)buffer_ptr(&b));
127 1.1 christos buffer_free(&b);
128 1.1 christos } else {
129 1.1 christos kbdintctxt->devices = xstrdup(devs);
130 1.1 christos }
131 1.1 christos debug("kbdint_alloc: devices '%s'", kbdintctxt->devices);
132 1.1 christos kbdintctxt->ctxt = NULL;
133 1.1 christos kbdintctxt->device = NULL;
134 1.1 christos kbdintctxt->nreq = 0;
135 1.1 christos
136 1.1 christos return kbdintctxt;
137 1.1 christos }
138 1.1 christos static void
139 1.1 christos kbdint_reset_device(KbdintAuthctxt *kbdintctxt)
140 1.1 christos {
141 1.1 christos if (kbdintctxt->ctxt) {
142 1.1 christos kbdintctxt->device->free_ctx(kbdintctxt->ctxt);
143 1.1 christos kbdintctxt->ctxt = NULL;
144 1.1 christos }
145 1.1 christos kbdintctxt->device = NULL;
146 1.1 christos }
147 1.1 christos static void
148 1.1 christos kbdint_free(KbdintAuthctxt *kbdintctxt)
149 1.1 christos {
150 1.1 christos if (kbdintctxt->device)
151 1.1 christos kbdint_reset_device(kbdintctxt);
152 1.5 christos free(kbdintctxt->devices);
153 1.6 christos explicit_bzero(kbdintctxt, sizeof(*kbdintctxt));
154 1.5 christos free(kbdintctxt);
155 1.1 christos }
156 1.1 christos /* get next device */
157 1.1 christos static int
158 1.5 christos kbdint_next_device(Authctxt *authctxt, KbdintAuthctxt *kbdintctxt)
159 1.1 christos {
160 1.1 christos size_t len;
161 1.1 christos char *t;
162 1.1 christos int i;
163 1.1 christos
164 1.1 christos if (kbdintctxt->device)
165 1.1 christos kbdint_reset_device(kbdintctxt);
166 1.1 christos do {
167 1.1 christos len = kbdintctxt->devices ?
168 1.1 christos strcspn(kbdintctxt->devices, ",") : 0;
169 1.1 christos
170 1.1 christos if (len == 0)
171 1.1 christos break;
172 1.5 christos for (i = 0; devices[i]; i++) {
173 1.5 christos if (!auth2_method_allowed(authctxt,
174 1.5 christos "keyboard-interactive", devices[i]->name))
175 1.5 christos continue;
176 1.1 christos if (strncmp(kbdintctxt->devices, devices[i]->name, len) == 0)
177 1.1 christos kbdintctxt->device = devices[i];
178 1.5 christos }
179 1.1 christos t = kbdintctxt->devices;
180 1.1 christos kbdintctxt->devices = t[len] ? xstrdup(t+len+1) : NULL;
181 1.5 christos free(t);
182 1.1 christos debug2("kbdint_next_device: devices %s", kbdintctxt->devices ?
183 1.1 christos kbdintctxt->devices : "<empty>");
184 1.1 christos } while (kbdintctxt->devices && !kbdintctxt->device);
185 1.1 christos
186 1.1 christos return kbdintctxt->device ? 1 : 0;
187 1.1 christos }
188 1.1 christos
189 1.1 christos /*
190 1.1 christos * try challenge-response, set authctxt->postponed if we have to
191 1.1 christos * wait for the response.
192 1.1 christos */
193 1.1 christos int
194 1.1 christos auth2_challenge(Authctxt *authctxt, char *devs)
195 1.1 christos {
196 1.1 christos debug("auth2_challenge: user=%s devs=%s",
197 1.1 christos authctxt->user ? authctxt->user : "<nouser>",
198 1.1 christos devs ? devs : "<no devs>");
199 1.1 christos
200 1.1 christos if (authctxt->user == NULL || !devs)
201 1.1 christos return 0;
202 1.1 christos if (authctxt->kbdintctxt == NULL)
203 1.1 christos authctxt->kbdintctxt = kbdint_alloc(devs);
204 1.1 christos return auth2_challenge_start(authctxt);
205 1.1 christos }
206 1.1 christos
207 1.1 christos /* unregister kbd-int callbacks and context */
208 1.1 christos void
209 1.1 christos auth2_challenge_stop(Authctxt *authctxt)
210 1.1 christos {
211 1.1 christos /* unregister callback */
212 1.1 christos dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL);
213 1.1 christos if (authctxt->kbdintctxt != NULL) {
214 1.1 christos kbdint_free(authctxt->kbdintctxt);
215 1.1 christos authctxt->kbdintctxt = NULL;
216 1.1 christos }
217 1.1 christos }
218 1.1 christos
219 1.1 christos /* side effect: sets authctxt->postponed if a reply was sent*/
220 1.1 christos static int
221 1.1 christos auth2_challenge_start(Authctxt *authctxt)
222 1.1 christos {
223 1.1 christos KbdintAuthctxt *kbdintctxt = authctxt->kbdintctxt;
224 1.1 christos
225 1.1 christos debug2("auth2_challenge_start: devices %s",
226 1.1 christos kbdintctxt->devices ? kbdintctxt->devices : "<empty>");
227 1.1 christos
228 1.5 christos if (kbdint_next_device(authctxt, kbdintctxt) == 0) {
229 1.1 christos auth2_challenge_stop(authctxt);
230 1.1 christos return 0;
231 1.1 christos }
232 1.1 christos debug("auth2_challenge_start: trying authentication method '%s'",
233 1.1 christos kbdintctxt->device->name);
234 1.1 christos
235 1.1 christos if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) {
236 1.1 christos auth2_challenge_stop(authctxt);
237 1.1 christos return 0;
238 1.1 christos }
239 1.1 christos if (send_userauth_info_request(authctxt) == 0) {
240 1.1 christos auth2_challenge_stop(authctxt);
241 1.1 christos return 0;
242 1.1 christos }
243 1.1 christos dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE,
244 1.1 christos &input_userauth_info_response);
245 1.1 christos
246 1.1 christos authctxt->postponed = 1;
247 1.1 christos return 0;
248 1.1 christos }
249 1.1 christos
250 1.1 christos static int
251 1.1 christos send_userauth_info_request(Authctxt *authctxt)
252 1.1 christos {
253 1.1 christos KbdintAuthctxt *kbdintctxt;
254 1.1 christos char *name, *instr, **prompts;
255 1.1 christos u_int i, *echo_on;
256 1.1 christos
257 1.1 christos kbdintctxt = authctxt->kbdintctxt;
258 1.1 christos if (kbdintctxt->device->query(kbdintctxt->ctxt,
259 1.1 christos &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on))
260 1.1 christos return 0;
261 1.1 christos
262 1.1 christos packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST);
263 1.1 christos packet_put_cstring(name);
264 1.1 christos packet_put_cstring(instr);
265 1.1 christos packet_put_cstring(""); /* language not used */
266 1.1 christos packet_put_int(kbdintctxt->nreq);
267 1.1 christos for (i = 0; i < kbdintctxt->nreq; i++) {
268 1.1 christos packet_put_cstring(prompts[i]);
269 1.1 christos packet_put_char(echo_on[i]);
270 1.1 christos }
271 1.1 christos packet_send();
272 1.1 christos packet_write_wait();
273 1.1 christos
274 1.1 christos for (i = 0; i < kbdintctxt->nreq; i++)
275 1.5 christos free(prompts[i]);
276 1.5 christos free(prompts);
277 1.5 christos free(echo_on);
278 1.5 christos free(name);
279 1.5 christos free(instr);
280 1.1 christos return 1;
281 1.1 christos }
282 1.1 christos
283 1.1 christos static void
284 1.1 christos input_userauth_info_response(int type, u_int32_t seq, void *ctxt)
285 1.1 christos {
286 1.1 christos Authctxt *authctxt = ctxt;
287 1.1 christos KbdintAuthctxt *kbdintctxt;
288 1.1 christos int authenticated = 0, res;
289 1.1 christos u_int i, nresp;
290 1.4 christos const char *devicename = NULL;
291 1.4 christos char **response = NULL;
292 1.1 christos
293 1.1 christos if (authctxt == NULL)
294 1.1 christos fatal("input_userauth_info_response: no authctxt");
295 1.1 christos kbdintctxt = authctxt->kbdintctxt;
296 1.1 christos if (kbdintctxt == NULL || kbdintctxt->ctxt == NULL)
297 1.1 christos fatal("input_userauth_info_response: no kbdintctxt");
298 1.1 christos if (kbdintctxt->device == NULL)
299 1.1 christos fatal("input_userauth_info_response: no device");
300 1.1 christos
301 1.1 christos authctxt->postponed = 0; /* reset */
302 1.1 christos nresp = packet_get_int();
303 1.1 christos if (nresp != kbdintctxt->nreq)
304 1.1 christos fatal("input_userauth_info_response: wrong number of replies");
305 1.1 christos if (nresp > 100)
306 1.1 christos fatal("input_userauth_info_response: too many replies");
307 1.1 christos if (nresp > 0) {
308 1.1 christos response = xcalloc(nresp, sizeof(char *));
309 1.1 christos for (i = 0; i < nresp; i++)
310 1.1 christos response[i] = packet_get_string(NULL);
311 1.1 christos }
312 1.1 christos packet_check_eom();
313 1.1 christos
314 1.1 christos res = kbdintctxt->device->respond(kbdintctxt->ctxt, nresp, response);
315 1.1 christos
316 1.1 christos for (i = 0; i < nresp; i++) {
317 1.6 christos explicit_bzero(response[i], strlen(response[i]));
318 1.5 christos free(response[i]);
319 1.1 christos }
320 1.5 christos free(response);
321 1.1 christos
322 1.1 christos switch (res) {
323 1.1 christos case 0:
324 1.1 christos /* Success! */
325 1.1 christos authenticated = authctxt->valid ? 1 : 0;
326 1.1 christos break;
327 1.1 christos case 1:
328 1.1 christos /* Authentication needs further interaction */
329 1.1 christos if (send_userauth_info_request(authctxt) == 1)
330 1.1 christos authctxt->postponed = 1;
331 1.1 christos break;
332 1.1 christos default:
333 1.1 christos /* Failure! */
334 1.1 christos break;
335 1.1 christos }
336 1.4 christos devicename = kbdintctxt->device->name;
337 1.1 christos if (!authctxt->postponed) {
338 1.1 christos if (authenticated) {
339 1.1 christos auth2_challenge_stop(authctxt);
340 1.1 christos } else {
341 1.1 christos /* start next device */
342 1.1 christos /* may set authctxt->postponed */
343 1.1 christos auth2_challenge_start(authctxt);
344 1.1 christos }
345 1.1 christos }
346 1.4 christos userauth_finish(authctxt, authenticated, "keyboard-interactive",
347 1.4 christos devicename);
348 1.1 christos }
349 1.1 christos
350 1.1 christos void
351 1.1 christos privsep_challenge_enable(void)
352 1.1 christos {
353 1.2 christos #if defined(BSD_AUTH) || defined(USE_PAM) || defined(SKEY)
354 1.2 christos int n = 0;
355 1.2 christos #endif
356 1.2 christos #ifdef BSD_AUTH
357 1.1 christos extern KbdintDevice mm_bsdauth_device;
358 1.2 christos #endif
359 1.2 christos #ifdef USE_PAM
360 1.2 christos extern KbdintDevice mm_sshpam_device;
361 1.2 christos #endif
362 1.2 christos #ifdef SKEY
363 1.2 christos extern KbdintDevice mm_skey_device;
364 1.2 christos #endif
365 1.1 christos /* As long as SSHv1 has devices[0] hard coded this is fine */
366 1.2 christos #ifdef BSD_AUTH
367 1.2 christos devices[n++] = &mm_bsdauth_device;
368 1.2 christos #endif
369 1.2 christos #ifdef USE_PAM
370 1.2 christos devices[n++] = &mm_sshpam_device;
371 1.2 christos #endif
372 1.2 christos #ifdef SKEY
373 1.2 christos devices[n++] = &mm_skey_device;
374 1.2 christos #endif
375 1.1 christos }
376