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