1295367Sdes/* $OpenBSD: auth-chall.c,v 1.14 2014/06/24 01:13:21 djm Exp $ */
276259Sgreen/*
376259Sgreen * Copyright (c) 2001 Markus Friedl.  All rights reserved.
476259Sgreen *
576259Sgreen * Redistribution and use in source and binary forms, with or without
676259Sgreen * modification, are permitted provided that the following conditions
776259Sgreen * are met:
876259Sgreen * 1. Redistributions of source code must retain the above copyright
976259Sgreen *    notice, this list of conditions and the following disclaimer.
1076259Sgreen * 2. Redistributions in binary form must reproduce the above copyright
1176259Sgreen *    notice, this list of conditions and the following disclaimer in the
1276259Sgreen *    documentation and/or other materials provided with the distribution.
1376259Sgreen *
1476259Sgreen * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1576259Sgreen * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1676259Sgreen * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1776259Sgreen * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1876259Sgreen * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1976259Sgreen * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2076259Sgreen * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2176259Sgreen * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2276259Sgreen * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2376259Sgreen * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2476259Sgreen */
2576259Sgreen
2676259Sgreen#include "includes.h"
2776259Sgreen
28162856Sdes#include <sys/types.h>
29162856Sdes#include <stdarg.h>
30295367Sdes#include <stdlib.h>
31295367Sdes#include <stdio.h>
32162856Sdes
33162856Sdes#include "xmalloc.h"
34162856Sdes#include "key.h"
35162856Sdes#include "hostfile.h"
3676259Sgreen#include "auth.h"
3776259Sgreen#include "log.h"
38295367Sdes#include "misc.h"
39147005Sdes#include "servconf.h"
4076259Sgreen
4192559Sdes/* limited protocol v1 interface to kbd-interactive authentication */
4292559Sdes
4392559Sdesextern KbdintDevice *devices[];
4492559Sdesstatic KbdintDevice *device;
45147005Sdesextern ServerOptions options;
4692559Sdes
4776259Sgreenchar *
4892559Sdesget_challenge(Authctxt *authctxt)
4976259Sgreen{
5092559Sdes	char *challenge, *name, *info, **prompts;
5192559Sdes	u_int i, numprompts;
5292559Sdes	u_int *echo_on;
5376259Sgreen
54147005Sdes#ifdef USE_PAM
55147005Sdes	if (!options.use_pam)
56147005Sdes		remove_kbdint_device("pam");
57147005Sdes#endif
58147005Sdes
5992559Sdes	device = devices[0]; /* we always use the 1st device for protocol 1 */
6092559Sdes	if (device == NULL)
6192559Sdes		return NULL;
6292559Sdes	if ((authctxt->kbdintctxt = device->init_ctx(authctxt)) == NULL)
6392559Sdes		return NULL;
6492559Sdes	if (device->query(authctxt->kbdintctxt, &name, &info,
6592559Sdes	    &numprompts, &prompts, &echo_on)) {
6692559Sdes		device->free_ctx(authctxt->kbdintctxt);
6792559Sdes		authctxt->kbdintctxt = NULL;
6892559Sdes		return NULL;
6976259Sgreen	}
7092559Sdes	if (numprompts < 1)
7192559Sdes		fatal("get_challenge: numprompts < 1");
7292559Sdes	challenge = xstrdup(prompts[0]);
7392559Sdes	for (i = 0; i < numprompts; i++)
74255767Sdes		free(prompts[i]);
75255767Sdes	free(prompts);
76255767Sdes	free(name);
77255767Sdes	free(echo_on);
78255767Sdes	free(info);
7992559Sdes
8092559Sdes	return (challenge);
8176259Sgreen}
8276259Sgreenint
8392559Sdesverify_response(Authctxt *authctxt, const char *response)
8476259Sgreen{
85126277Sdes	char *resp[1], *name, *info, **prompts;
86126277Sdes	u_int i, numprompts, *echo_on;
87126277Sdes	int authenticated = 0;
8876259Sgreen
8992559Sdes	if (device == NULL)
9092559Sdes		return 0;
9192559Sdes	if (authctxt->kbdintctxt == NULL)
9292559Sdes		return 0;
9392559Sdes	resp[0] = (char *)response;
94126277Sdes	switch (device->respond(authctxt->kbdintctxt, 1, resp)) {
95126277Sdes	case 0: /* Success */
96126277Sdes		authenticated = 1;
97126277Sdes		break;
98126277Sdes	case 1: /* Postponed - retry with empty query for PAM */
99126277Sdes		if ((device->query(authctxt->kbdintctxt, &name, &info,
100126277Sdes		    &numprompts, &prompts, &echo_on)) != 0)
101126277Sdes			break;
102126277Sdes		if (numprompts == 0 &&
103126277Sdes		    device->respond(authctxt->kbdintctxt, 0, resp) == 0)
104126277Sdes			authenticated = 1;
105110138Sdes
106126277Sdes		for (i = 0; i < numprompts; i++)
107255767Sdes			free(prompts[i]);
108255767Sdes		free(prompts);
109255767Sdes		free(name);
110255767Sdes		free(echo_on);
111255767Sdes		free(info);
112126277Sdes		break;
113110138Sdes	}
11492559Sdes	device->free_ctx(authctxt->kbdintctxt);
11592559Sdes	authctxt->kbdintctxt = NULL;
116126277Sdes	return authenticated;
11776259Sgreen}
118112870Sdesvoid
119112870Sdesabandon_challenge_response(Authctxt *authctxt)
120112870Sdes{
121112870Sdes	if (authctxt->kbdintctxt != NULL) {
122112870Sdes		device->free_ctx(authctxt->kbdintctxt);
123112870Sdes		authctxt->kbdintctxt = NULL;
124112870Sdes	}
125112870Sdes}
126