1264377Sdes/* $OpenBSD: auth2-chall.c,v 1.41 2014/02/02 03:44:31 djm Exp $ */
276259Sgreen/*
376259Sgreen * Copyright (c) 2001 Markus Friedl.  All rights reserved.
492555Sdes * Copyright (c) 2001 Per Allansson.  All rights reserved.
576259Sgreen *
676259Sgreen * Redistribution and use in source and binary forms, with or without
776259Sgreen * modification, are permitted provided that the following conditions
876259Sgreen * are met:
976259Sgreen * 1. Redistributions of source code must retain the above copyright
1076259Sgreen *    notice, this list of conditions and the following disclaimer.
1176259Sgreen * 2. Redistributions in binary form must reproduce the above copyright
1276259Sgreen *    notice, this list of conditions and the following disclaimer in the
1376259Sgreen *    documentation and/or other materials provided with the distribution.
1476259Sgreen *
1576259Sgreen * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1676259Sgreen * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1776259Sgreen * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1876259Sgreen * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1976259Sgreen * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2076259Sgreen * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2176259Sgreen * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2276259Sgreen * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2376259Sgreen * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2476259Sgreen * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2576259Sgreen */
26162856Sdes
2776259Sgreen#include "includes.h"
2876259Sgreen
29162856Sdes#include <sys/types.h>
30162856Sdes
31162856Sdes#include <stdarg.h>
32162856Sdes#include <stdio.h>
33162856Sdes#include <string.h>
34162856Sdes
35162856Sdes#include "xmalloc.h"
3676259Sgreen#include "ssh2.h"
37162856Sdes#include "key.h"
38162856Sdes#include "hostfile.h"
3976259Sgreen#include "auth.h"
4092555Sdes#include "buffer.h"
4176259Sgreen#include "packet.h"
4276259Sgreen#include "dispatch.h"
4376259Sgreen#include "log.h"
44147005Sdes#include "servconf.h"
4576259Sgreen
46147005Sdes/* import */
47147005Sdesextern ServerOptions options;
48147005Sdes
4992555Sdesstatic int auth2_challenge_start(Authctxt *);
5092555Sdesstatic int send_userauth_info_request(Authctxt *);
5192555Sdesstatic void input_userauth_info_response(int, u_int32_t, void *);
5276259Sgreen
5392555Sdes#ifdef BSD_AUTH
5492555Sdesextern KbdintDevice bsdauth_device;
5598941Sdes#else
5699052Sdes#ifdef USE_PAM
57124211Sdesextern KbdintDevice sshpam_device;
5899052Sdes#endif
5998941Sdes#ifdef SKEY
6092555Sdesextern KbdintDevice skey_device;
6192555Sdes#endif
6298941Sdes#endif
6392555Sdes
6492555SdesKbdintDevice *devices[] = {
6592555Sdes#ifdef BSD_AUTH
6692555Sdes	&bsdauth_device,
6798941Sdes#else
6899052Sdes#ifdef USE_PAM
69124211Sdes	&sshpam_device,
7099052Sdes#endif
7198941Sdes#ifdef SKEY
7292555Sdes	&skey_device,
7392555Sdes#endif
7498941Sdes#endif
7592555Sdes	NULL
7692555Sdes};
7792555Sdes
7892555Sdestypedef struct KbdintAuthctxt KbdintAuthctxt;
7992555Sdesstruct KbdintAuthctxt
8092555Sdes{
8192555Sdes	char *devices;
8292555Sdes	void *ctxt;
8392555Sdes	KbdintDevice *device;
8499063Sdes	u_int nreq;
85285979Sdelphij	u_int devices_done;
8692555Sdes};
8792555Sdes
88147005Sdes#ifdef USE_PAM
89147005Sdesvoid
90147005Sdesremove_kbdint_device(const char *devname)
91147005Sdes{
92147005Sdes	int i, j;
93147005Sdes
94147005Sdes	for (i = 0; devices[i] != NULL; i++)
95147005Sdes		if (strcmp(devices[i]->name, devname) == 0) {
96147005Sdes			for (j = i; devices[j] != NULL; j++)
97147005Sdes				devices[j] = devices[j+1];
98147005Sdes			i--;
99147005Sdes		}
100147005Sdes}
101147005Sdes#endif
102147005Sdes
10392555Sdesstatic KbdintAuthctxt *
10492555Sdeskbdint_alloc(const char *devs)
10592555Sdes{
10692555Sdes	KbdintAuthctxt *kbdintctxt;
10792555Sdes	Buffer b;
10892555Sdes	int i;
10992555Sdes
110147005Sdes#ifdef USE_PAM
111147005Sdes	if (!options.use_pam)
112147005Sdes		remove_kbdint_device("pam");
113147005Sdes#endif
114147005Sdes
115258343Sdes	kbdintctxt = xcalloc(1, sizeof(KbdintAuthctxt));
11692555Sdes	if (strcmp(devs, "") == 0) {
11792555Sdes		buffer_init(&b);
11892555Sdes		for (i = 0; devices[i]; i++) {
11992555Sdes			if (buffer_len(&b) > 0)
12092555Sdes				buffer_append(&b, ",", 1);
12192555Sdes			buffer_append(&b, devices[i]->name,
12292555Sdes			    strlen(devices[i]->name));
12392555Sdes		}
12492555Sdes		buffer_append(&b, "\0", 1);
12592555Sdes		kbdintctxt->devices = xstrdup(buffer_ptr(&b));
12692555Sdes		buffer_free(&b);
12792555Sdes	} else {
12892555Sdes		kbdintctxt->devices = xstrdup(devs);
12992555Sdes	}
13092555Sdes	debug("kbdint_alloc: devices '%s'", kbdintctxt->devices);
13192555Sdes	kbdintctxt->ctxt = NULL;
13292555Sdes	kbdintctxt->device = NULL;
13399063Sdes	kbdintctxt->nreq = 0;
13492555Sdes
13592555Sdes	return kbdintctxt;
13692555Sdes}
13792555Sdesstatic void
13892555Sdeskbdint_reset_device(KbdintAuthctxt *kbdintctxt)
13992555Sdes{
14092555Sdes	if (kbdintctxt->ctxt) {
14192555Sdes		kbdintctxt->device->free_ctx(kbdintctxt->ctxt);
14292555Sdes		kbdintctxt->ctxt = NULL;
14392555Sdes	}
14492555Sdes	kbdintctxt->device = NULL;
14592555Sdes}
14692555Sdesstatic void
14792555Sdeskbdint_free(KbdintAuthctxt *kbdintctxt)
14892555Sdes{
14992555Sdes	if (kbdintctxt->device)
15092555Sdes		kbdint_reset_device(kbdintctxt);
151255767Sdes	free(kbdintctxt->devices);
152264377Sdes	explicit_bzero(kbdintctxt, sizeof(*kbdintctxt));
153255767Sdes	free(kbdintctxt);
15492555Sdes}
15592555Sdes/* get next device */
15692555Sdesstatic int
157255767Sdeskbdint_next_device(Authctxt *authctxt, KbdintAuthctxt *kbdintctxt)
15892555Sdes{
15992555Sdes	size_t len;
16092555Sdes	char *t;
16192555Sdes	int i;
16292555Sdes
16392555Sdes	if (kbdintctxt->device)
16492555Sdes		kbdint_reset_device(kbdintctxt);
16592555Sdes	do {
16692555Sdes		len = kbdintctxt->devices ?
16792555Sdes		    strcspn(kbdintctxt->devices, ",") : 0;
16892555Sdes
16992555Sdes		if (len == 0)
17092555Sdes			break;
171255767Sdes		for (i = 0; devices[i]; i++) {
172285979Sdelphij			if ((kbdintctxt->devices_done & (1 << i)) != 0 ||
173285979Sdelphij			    !auth2_method_allowed(authctxt,
174255767Sdes			    "keyboard-interactive", devices[i]->name))
175255767Sdes				continue;
176285979Sdelphij			if (strncmp(kbdintctxt->devices, devices[i]->name,
177285979Sdelphij			    len) == 0) {
17892555Sdes				kbdintctxt->device = devices[i];
179285979Sdelphij				kbdintctxt->devices_done |= 1 << i;
180285979Sdelphij			}
181255767Sdes		}
18292555Sdes		t = kbdintctxt->devices;
18392555Sdes		kbdintctxt->devices = t[len] ? xstrdup(t+len+1) : NULL;
184255767Sdes		free(t);
18592555Sdes		debug2("kbdint_next_device: devices %s", kbdintctxt->devices ?
186149753Sdes		    kbdintctxt->devices : "<empty>");
18792555Sdes	} while (kbdintctxt->devices && !kbdintctxt->device);
18892555Sdes
18992555Sdes	return kbdintctxt->device ? 1 : 0;
19092555Sdes}
19192555Sdes
19276259Sgreen/*
19392555Sdes * try challenge-response, set authctxt->postponed if we have to
19476259Sgreen * wait for the response.
19576259Sgreen */
19676259Sgreenint
19776259Sgreenauth2_challenge(Authctxt *authctxt, char *devs)
19876259Sgreen{
19992555Sdes	debug("auth2_challenge: user=%s devs=%s",
20092555Sdes	    authctxt->user ? authctxt->user : "<nouser>",
20192555Sdes	    devs ? devs : "<no devs>");
20276259Sgreen
20392555Sdes	if (authctxt->user == NULL || !devs)
20476259Sgreen		return 0;
20592555Sdes	if (authctxt->kbdintctxt == NULL)
20692555Sdes		authctxt->kbdintctxt = kbdint_alloc(devs);
20792555Sdes	return auth2_challenge_start(authctxt);
20892555Sdes}
20992555Sdes
21092555Sdes/* unregister kbd-int callbacks and context */
21192555Sdesvoid
21292555Sdesauth2_challenge_stop(Authctxt *authctxt)
21392555Sdes{
21492555Sdes	/* unregister callback */
21592555Sdes	dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL);
216181111Sdes	if (authctxt->kbdintctxt != NULL) {
21792555Sdes		kbdint_free(authctxt->kbdintctxt);
21892555Sdes		authctxt->kbdintctxt = NULL;
21992555Sdes	}
22092555Sdes}
22192555Sdes
22292555Sdes/* side effect: sets authctxt->postponed if a reply was sent*/
22392555Sdesstatic int
22492555Sdesauth2_challenge_start(Authctxt *authctxt)
22592555Sdes{
22692555Sdes	KbdintAuthctxt *kbdintctxt = authctxt->kbdintctxt;
22792555Sdes
22892555Sdes	debug2("auth2_challenge_start: devices %s",
22992555Sdes	    kbdintctxt->devices ?  kbdintctxt->devices : "<empty>");
23092555Sdes
231255767Sdes	if (kbdint_next_device(authctxt, kbdintctxt) == 0) {
23292555Sdes		auth2_challenge_stop(authctxt);
23376259Sgreen		return 0;
23492555Sdes	}
23592555Sdes	debug("auth2_challenge_start: trying authentication method '%s'",
23692555Sdes	    kbdintctxt->device->name);
23792555Sdes
23892555Sdes	if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) {
23992555Sdes		auth2_challenge_stop(authctxt);
24092555Sdes		return 0;
24192555Sdes	}
24292555Sdes	if (send_userauth_info_request(authctxt) == 0) {
24392555Sdes		auth2_challenge_stop(authctxt);
24492555Sdes		return 0;
24592555Sdes	}
24676259Sgreen	dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE,
24776259Sgreen	    &input_userauth_info_response);
24892555Sdes
24976259Sgreen	authctxt->postponed = 1;
25076259Sgreen	return 0;
25176259Sgreen}
25276259Sgreen
25392555Sdesstatic int
25492555Sdessend_userauth_info_request(Authctxt *authctxt)
25576259Sgreen{
25692555Sdes	KbdintAuthctxt *kbdintctxt;
25792555Sdes	char *name, *instr, **prompts;
258149753Sdes	u_int i, *echo_on;
25976259Sgreen
26092555Sdes	kbdintctxt = authctxt->kbdintctxt;
26192555Sdes	if (kbdintctxt->device->query(kbdintctxt->ctxt,
26299063Sdes	    &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on))
26392555Sdes		return 0;
26492555Sdes
26576259Sgreen	packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST);
26692555Sdes	packet_put_cstring(name);
26792555Sdes	packet_put_cstring(instr);
26898684Sdes	packet_put_cstring("");		/* language not used */
26999063Sdes	packet_put_int(kbdintctxt->nreq);
27099063Sdes	for (i = 0; i < kbdintctxt->nreq; i++) {
27192555Sdes		packet_put_cstring(prompts[i]);
27292555Sdes		packet_put_char(echo_on[i]);
27392555Sdes	}
27476259Sgreen	packet_send();
27576259Sgreen	packet_write_wait();
27692555Sdes
27799063Sdes	for (i = 0; i < kbdintctxt->nreq; i++)
278255767Sdes		free(prompts[i]);
279255767Sdes	free(prompts);
280255767Sdes	free(echo_on);
281255767Sdes	free(name);
282255767Sdes	free(instr);
28392555Sdes	return 1;
28476259Sgreen}
28576259Sgreen
28692555Sdesstatic void
28792555Sdesinput_userauth_info_response(int type, u_int32_t seq, void *ctxt)
28876259Sgreen{
28976259Sgreen	Authctxt *authctxt = ctxt;
29092555Sdes	KbdintAuthctxt *kbdintctxt;
291192595Sdes	int authenticated = 0, res;
292149753Sdes	u_int i, nresp;
293248619Sdes	const char *devicename = NULL;
294248619Sdes	char **response = NULL;
29576259Sgreen
29676259Sgreen	if (authctxt == NULL)
29776259Sgreen		fatal("input_userauth_info_response: no authctxt");
29892555Sdes	kbdintctxt = authctxt->kbdintctxt;
29992555Sdes	if (kbdintctxt == NULL || kbdintctxt->ctxt == NULL)
30092555Sdes		fatal("input_userauth_info_response: no kbdintctxt");
30192555Sdes	if (kbdintctxt->device == NULL)
30292555Sdes		fatal("input_userauth_info_response: no device");
30376259Sgreen
30476259Sgreen	authctxt->postponed = 0;	/* reset */
30576259Sgreen	nresp = packet_get_int();
30699063Sdes	if (nresp != kbdintctxt->nreq)
30799063Sdes		fatal("input_userauth_info_response: wrong number of replies");
30899063Sdes	if (nresp > 100)
30999063Sdes		fatal("input_userauth_info_response: too many replies");
31092555Sdes	if (nresp > 0) {
311162856Sdes		response = xcalloc(nresp, sizeof(char *));
31292555Sdes		for (i = 0; i < nresp; i++)
31392555Sdes			response[i] = packet_get_string(NULL);
31492555Sdes	}
31592555Sdes	packet_check_eom();
31692555Sdes
317147005Sdes	res = kbdintctxt->device->respond(kbdintctxt->ctxt, nresp, response);
31892555Sdes
31992555Sdes	for (i = 0; i < nresp; i++) {
320264377Sdes		explicit_bzero(response[i], strlen(response[i]));
321255767Sdes		free(response[i]);
32292555Sdes	}
323255767Sdes	free(response);
32492555Sdes
32592555Sdes	switch (res) {
32692555Sdes	case 0:
32792555Sdes		/* Success! */
328147005Sdes		authenticated = authctxt->valid ? 1 : 0;
32992555Sdes		break;
33092555Sdes	case 1:
33192555Sdes		/* Authentication needs further interaction */
33292555Sdes		if (send_userauth_info_request(authctxt) == 1)
33392555Sdes			authctxt->postponed = 1;
33492555Sdes		break;
33592555Sdes	default:
33692555Sdes		/* Failure! */
33792555Sdes		break;
33876259Sgreen	}
339248619Sdes	devicename = kbdintctxt->device->name;
34092555Sdes	if (!authctxt->postponed) {
34192555Sdes		if (authenticated) {
34292555Sdes			auth2_challenge_stop(authctxt);
34392555Sdes		} else {
34492555Sdes			/* start next device */
34592555Sdes			/* may set authctxt->postponed */
34692555Sdes			auth2_challenge_start(authctxt);
34792555Sdes		}
34892555Sdes	}
349248619Sdes	userauth_finish(authctxt, authenticated, "keyboard-interactive",
350248619Sdes	    devicename);
35176259Sgreen}
35298684Sdes
35398684Sdesvoid
35498684Sdesprivsep_challenge_enable(void)
35598684Sdes{
356124211Sdes#if defined(BSD_AUTH) || defined(USE_PAM) || defined(SKEY)
357124211Sdes	int n = 0;
358124211Sdes#endif
35998684Sdes#ifdef BSD_AUTH
36098684Sdes	extern KbdintDevice mm_bsdauth_device;
36198684Sdes#endif
36299052Sdes#ifdef USE_PAM
363124211Sdes	extern KbdintDevice mm_sshpam_device;
36499052Sdes#endif
36598684Sdes#ifdef SKEY
36698684Sdes	extern KbdintDevice mm_skey_device;
36798684Sdes#endif
36899052Sdes
36998684Sdes#ifdef BSD_AUTH
37099052Sdes	devices[n++] = &mm_bsdauth_device;
37198684Sdes#else
37299052Sdes#ifdef USE_PAM
373124211Sdes	devices[n++] = &mm_sshpam_device;
37499052Sdes#endif
37598684Sdes#ifdef SKEY
37699052Sdes	devices[n++] = &mm_skey_device;
37798684Sdes#endif
37898684Sdes#endif
37998684Sdes}
380