auth2-chall.c revision 162856
1162856Sdes/* $OpenBSD: auth2-chall.c,v 1.31 2006/08/05 08:28:24 dtucker 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"
28162856Sdes__RCSID("$FreeBSD: head/crypto/openssh/auth2-chall.c 162856 2006-09-30 13:38:06Z des $");
2976259Sgreen
30162856Sdes#include <sys/types.h>
31162856Sdes
32162856Sdes#include <stdarg.h>
33162856Sdes#include <stdio.h>
34162856Sdes#include <string.h>
35162856Sdes
36162856Sdes#include "xmalloc.h"
3776259Sgreen#include "ssh2.h"
38162856Sdes#include "key.h"
39162856Sdes#include "hostfile.h"
4076259Sgreen#include "auth.h"
4192555Sdes#include "buffer.h"
4276259Sgreen#include "packet.h"
4376259Sgreen#include "dispatch.h"
4476259Sgreen#include "log.h"
45147005Sdes#include "servconf.h"
4676259Sgreen
47147005Sdes/* import */
48147005Sdesextern ServerOptions options;
49147005Sdes
5092555Sdesstatic int auth2_challenge_start(Authctxt *);
5192555Sdesstatic int send_userauth_info_request(Authctxt *);
5292555Sdesstatic void input_userauth_info_response(int, u_int32_t, void *);
5376259Sgreen
5492555Sdes#ifdef BSD_AUTH
5592555Sdesextern KbdintDevice bsdauth_device;
5698941Sdes#else
5799052Sdes#ifdef USE_PAM
58124211Sdesextern KbdintDevice sshpam_device;
5999052Sdes#endif
6098941Sdes#ifdef SKEY
6192555Sdesextern KbdintDevice skey_device;
6292555Sdes#endif
6398941Sdes#endif
6492555Sdes
6592555SdesKbdintDevice *devices[] = {
6692555Sdes#ifdef BSD_AUTH
6792555Sdes	&bsdauth_device,
6898941Sdes#else
6999052Sdes#ifdef USE_PAM
70124211Sdes	&sshpam_device,
7199052Sdes#endif
7298941Sdes#ifdef SKEY
7392555Sdes	&skey_device,
7492555Sdes#endif
7598941Sdes#endif
7692555Sdes	NULL
7792555Sdes};
7892555Sdes
7992555Sdestypedef struct KbdintAuthctxt KbdintAuthctxt;
8092555Sdesstruct KbdintAuthctxt
8192555Sdes{
8292555Sdes	char *devices;
8392555Sdes	void *ctxt;
8492555Sdes	KbdintDevice *device;
8599063Sdes	u_int nreq;
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
11592555Sdes	kbdintctxt = xmalloc(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);
15192555Sdes	if (kbdintctxt->devices) {
15292555Sdes		xfree(kbdintctxt->devices);
15392555Sdes		kbdintctxt->devices = NULL;
15492555Sdes	}
15592555Sdes	xfree(kbdintctxt);
15692555Sdes}
15792555Sdes/* get next device */
15892555Sdesstatic int
15992555Sdeskbdint_next_device(KbdintAuthctxt *kbdintctxt)
16092555Sdes{
16192555Sdes	size_t len;
16292555Sdes	char *t;
16392555Sdes	int i;
16492555Sdes
16592555Sdes	if (kbdintctxt->device)
16692555Sdes		kbdint_reset_device(kbdintctxt);
16792555Sdes	do {
16892555Sdes		len = kbdintctxt->devices ?
16992555Sdes		    strcspn(kbdintctxt->devices, ",") : 0;
17092555Sdes
17192555Sdes		if (len == 0)
17292555Sdes			break;
17392555Sdes		for (i = 0; devices[i]; i++)
17492555Sdes			if (strncmp(kbdintctxt->devices, devices[i]->name, len) == 0)
17592555Sdes				kbdintctxt->device = devices[i];
17692555Sdes		t = kbdintctxt->devices;
17792555Sdes		kbdintctxt->devices = t[len] ? xstrdup(t+len+1) : NULL;
17892555Sdes		xfree(t);
17992555Sdes		debug2("kbdint_next_device: devices %s", kbdintctxt->devices ?
180149753Sdes		    kbdintctxt->devices : "<empty>");
18192555Sdes	} while (kbdintctxt->devices && !kbdintctxt->device);
18292555Sdes
18392555Sdes	return kbdintctxt->device ? 1 : 0;
18492555Sdes}
18592555Sdes
18676259Sgreen/*
18792555Sdes * try challenge-response, set authctxt->postponed if we have to
18876259Sgreen * wait for the response.
18976259Sgreen */
19076259Sgreenint
19176259Sgreenauth2_challenge(Authctxt *authctxt, char *devs)
19276259Sgreen{
19392555Sdes	debug("auth2_challenge: user=%s devs=%s",
19492555Sdes	    authctxt->user ? authctxt->user : "<nouser>",
19592555Sdes	    devs ? devs : "<no devs>");
19676259Sgreen
19792555Sdes	if (authctxt->user == NULL || !devs)
19876259Sgreen		return 0;
19992555Sdes	if (authctxt->kbdintctxt == NULL)
20092555Sdes		authctxt->kbdintctxt = kbdint_alloc(devs);
20192555Sdes	return auth2_challenge_start(authctxt);
20292555Sdes}
20392555Sdes
20492555Sdes/* unregister kbd-int callbacks and context */
20592555Sdesvoid
20692555Sdesauth2_challenge_stop(Authctxt *authctxt)
20792555Sdes{
20892555Sdes	/* unregister callback */
20992555Sdes	dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL);
21092555Sdes	if (authctxt->kbdintctxt != NULL)  {
21192555Sdes		kbdint_free(authctxt->kbdintctxt);
21292555Sdes		authctxt->kbdintctxt = NULL;
21392555Sdes	}
21492555Sdes}
21592555Sdes
21692555Sdes/* side effect: sets authctxt->postponed if a reply was sent*/
21792555Sdesstatic int
21892555Sdesauth2_challenge_start(Authctxt *authctxt)
21992555Sdes{
22092555Sdes	KbdintAuthctxt *kbdintctxt = authctxt->kbdintctxt;
22192555Sdes
22292555Sdes	debug2("auth2_challenge_start: devices %s",
22392555Sdes	    kbdintctxt->devices ?  kbdintctxt->devices : "<empty>");
22492555Sdes
22592555Sdes	if (kbdint_next_device(kbdintctxt) == 0) {
22692555Sdes		auth2_challenge_stop(authctxt);
22776259Sgreen		return 0;
22892555Sdes	}
22992555Sdes	debug("auth2_challenge_start: trying authentication method '%s'",
23092555Sdes	    kbdintctxt->device->name);
23192555Sdes
23292555Sdes	if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) {
23392555Sdes		auth2_challenge_stop(authctxt);
23492555Sdes		return 0;
23592555Sdes	}
23692555Sdes	if (send_userauth_info_request(authctxt) == 0) {
23792555Sdes		auth2_challenge_stop(authctxt);
23892555Sdes		return 0;
23992555Sdes	}
24076259Sgreen	dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE,
24176259Sgreen	    &input_userauth_info_response);
24292555Sdes
24376259Sgreen	authctxt->postponed = 1;
24476259Sgreen	return 0;
24576259Sgreen}
24676259Sgreen
24792555Sdesstatic int
24892555Sdessend_userauth_info_request(Authctxt *authctxt)
24976259Sgreen{
25092555Sdes	KbdintAuthctxt *kbdintctxt;
25192555Sdes	char *name, *instr, **prompts;
252149753Sdes	u_int i, *echo_on;
25376259Sgreen
25492555Sdes	kbdintctxt = authctxt->kbdintctxt;
25592555Sdes	if (kbdintctxt->device->query(kbdintctxt->ctxt,
25699063Sdes	    &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on))
25792555Sdes		return 0;
25892555Sdes
25976259Sgreen	packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST);
26092555Sdes	packet_put_cstring(name);
26192555Sdes	packet_put_cstring(instr);
26298684Sdes	packet_put_cstring("");		/* language not used */
26399063Sdes	packet_put_int(kbdintctxt->nreq);
26499063Sdes	for (i = 0; i < kbdintctxt->nreq; i++) {
26592555Sdes		packet_put_cstring(prompts[i]);
26692555Sdes		packet_put_char(echo_on[i]);
26792555Sdes	}
26876259Sgreen	packet_send();
26976259Sgreen	packet_write_wait();
27092555Sdes
27199063Sdes	for (i = 0; i < kbdintctxt->nreq; i++)
27292555Sdes		xfree(prompts[i]);
27392555Sdes	xfree(prompts);
27492555Sdes	xfree(echo_on);
27592555Sdes	xfree(name);
27692555Sdes	xfree(instr);
27792555Sdes	return 1;
27876259Sgreen}
27976259Sgreen
28092555Sdesstatic void
28192555Sdesinput_userauth_info_response(int type, u_int32_t seq, void *ctxt)
28276259Sgreen{
28376259Sgreen	Authctxt *authctxt = ctxt;
28492555Sdes	KbdintAuthctxt *kbdintctxt;
285149753Sdes	int authenticated = 0, res, len;
286149753Sdes	u_int i, nresp;
28792555Sdes	char **response = NULL, *method;
28876259Sgreen
28976259Sgreen	if (authctxt == NULL)
29076259Sgreen		fatal("input_userauth_info_response: no authctxt");
29192555Sdes	kbdintctxt = authctxt->kbdintctxt;
29292555Sdes	if (kbdintctxt == NULL || kbdintctxt->ctxt == NULL)
29392555Sdes		fatal("input_userauth_info_response: no kbdintctxt");
29492555Sdes	if (kbdintctxt->device == NULL)
29592555Sdes		fatal("input_userauth_info_response: no device");
29676259Sgreen
29776259Sgreen	authctxt->postponed = 0;	/* reset */
29876259Sgreen	nresp = packet_get_int();
29999063Sdes	if (nresp != kbdintctxt->nreq)
30099063Sdes		fatal("input_userauth_info_response: wrong number of replies");
30199063Sdes	if (nresp > 100)
30299063Sdes		fatal("input_userauth_info_response: too many replies");
30392555Sdes	if (nresp > 0) {
304162856Sdes		response = xcalloc(nresp, sizeof(char *));
30592555Sdes		for (i = 0; i < nresp; i++)
30692555Sdes			response[i] = packet_get_string(NULL);
30792555Sdes	}
30892555Sdes	packet_check_eom();
30992555Sdes
310147005Sdes	res = kbdintctxt->device->respond(kbdintctxt->ctxt, nresp, response);
31192555Sdes
31292555Sdes	for (i = 0; i < nresp; i++) {
31392555Sdes		memset(response[i], 'r', strlen(response[i]));
31492555Sdes		xfree(response[i]);
31592555Sdes	}
31692555Sdes	if (response)
31776259Sgreen		xfree(response);
31892555Sdes
31992555Sdes	switch (res) {
32092555Sdes	case 0:
32192555Sdes		/* Success! */
322147005Sdes		authenticated = authctxt->valid ? 1 : 0;
32392555Sdes		break;
32492555Sdes	case 1:
32592555Sdes		/* Authentication needs further interaction */
32692555Sdes		if (send_userauth_info_request(authctxt) == 1)
32792555Sdes			authctxt->postponed = 1;
32892555Sdes		break;
32992555Sdes	default:
33092555Sdes		/* Failure! */
33192555Sdes		break;
33276259Sgreen	}
33376259Sgreen
33492555Sdes	len = strlen("keyboard-interactive") + 2 +
33592555Sdes		strlen(kbdintctxt->device->name);
33692555Sdes	method = xmalloc(len);
33792555Sdes	snprintf(method, len, "keyboard-interactive/%s",
33892555Sdes	    kbdintctxt->device->name);
33992555Sdes
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	}
34976259Sgreen	userauth_finish(authctxt, authenticated, method);
35092555Sdes	xfree(method);
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