auth-bsdauth.c revision 146998
1218792Snp/*
2218792Snp * Copyright (c) 2001 Markus Friedl.  All rights reserved.
3218792Snp *
4218792Snp * Redistribution and use in source and binary forms, with or without
5218792Snp * modification, are permitted provided that the following conditions
6218792Snp * are met:
7218792Snp * 1. Redistributions of source code must retain the above copyright
8218792Snp *    notice, this list of conditions and the following disclaimer.
9218792Snp * 2. Redistributions in binary form must reproduce the above copyright
10218792Snp *    notice, this list of conditions and the following disclaimer in the
11218792Snp *    documentation and/or other materials provided with the distribution.
12218792Snp *
13218792Snp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14218792Snp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15218792Snp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16218792Snp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17218792Snp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18218792Snp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19218792Snp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20218792Snp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21218792Snp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22218792Snp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23218792Snp */
24218792Snp#include "includes.h"
25218792SnpRCSID("$OpenBSD: auth-bsdauth.c,v 1.6 2005/01/19 13:11:47 dtucker Exp $");
26218792Snp
27218792Snp#ifdef BSD_AUTH
28218792Snp#include "xmalloc.h"
29218792Snp#include "auth.h"
30218792Snp#include "log.h"
31218792Snp#include "monitor_wrap.h"
32218792Snp
33218792Snpstatic void *
34218792Snpbsdauth_init_ctx(Authctxt *authctxt)
35218792Snp{
36218792Snp	return authctxt;
37218792Snp}
38218792Snp
39219286Snpint
40219286Snpbsdauth_query(void *ctx, char **name, char **infotxt,
41219286Snp   u_int *numprompts, char ***prompts, u_int **echo_on)
42218792Snp{
43218792Snp	Authctxt *authctxt = ctx;
44218792Snp	char *challenge = NULL;
45218792Snp
46218792Snp	if (authctxt->as != NULL) {
47219436Snp		debug2("bsdauth_query: try reuse session");
48218792Snp		challenge = auth_getitem(authctxt->as, AUTHV_CHALLENGE);
49218792Snp		if (challenge == NULL) {
50218792Snp			auth_close(authctxt->as);
51218792Snp			authctxt->as = NULL;
52218792Snp		}
53218792Snp	}
54218792Snp
55218792Snp	if (challenge == NULL) {
56222003Snp		debug2("bsdauth_query: new bsd auth session");
57218792Snp		debug3("bsdauth_query: style %s",
58218792Snp		    authctxt->style ? authctxt->style : "<default>");
59221474Snp		authctxt->as = auth_userchallenge(authctxt->user,
60218792Snp		    authctxt->style, "auth-ssh", &challenge);
61218792Snp		if (authctxt->as == NULL)
62218792Snp			challenge = NULL;
63222509Snp		debug2("bsdauth_query: <%s>", challenge ? challenge : "empty");
64218792Snp	}
65218792Snp
66218792Snp	if (challenge == NULL)
67218792Snp		return -1;
68218792Snp
69218792Snp	*name = xstrdup("");
70218792Snp	*infotxt = xstrdup("");
71218792Snp	*numprompts = 1;
72218792Snp	*prompts = xmalloc(*numprompts * sizeof(char *));
73218792Snp	*echo_on = xmalloc(*numprompts * sizeof(u_int));
74227843Smarius	(*echo_on)[0] = 0;
75218792Snp	(*prompts)[0] = xstrdup(challenge);
76218792Snp
77218792Snp	return 0;
78218792Snp}
79218792Snp
80218792Snpint
81218792Snpbsdauth_respond(void *ctx, u_int numresponses, char **responses)
82218792Snp{
83218792Snp	Authctxt *authctxt = ctx;
84218792Snp	int authok;
85218792Snp
86218792Snp	if (!authctxt->valid)
87218792Snp		return -1;
88218792Snp
89218792Snp	if (authctxt->as == 0)
90218792Snp		error("bsdauth_respond: no bsd auth session");
91218792Snp
92218792Snp	if (numresponses != 1)
93218792Snp		return -1;
94218792Snp
95218792Snp	authok = auth_userresponse(authctxt->as, responses[0], 0);
96218792Snp	authctxt->as = NULL;
97218792Snp	debug3("bsdauth_respond: <%s> = <%d>", responses[0], authok);
98218792Snp
99218792Snp	return (authok == 0) ? -1 : 0;
100218792Snp}
101218792Snp
102218792Snpstatic void
103218792Snpbsdauth_free_ctx(void *ctx)
104218792Snp{
105218792Snp	Authctxt *authctxt = ctx;
106218792Snp
107218792Snp	if (authctxt && authctxt->as) {
108218792Snp		auth_close(authctxt->as);
109218792Snp		authctxt->as = NULL;
110218792Snp	}
111218792Snp}
112218792Snp
113218792SnpKbdintDevice bsdauth_device = {
114218792Snp	"bsdauth",
115218792Snp	bsdauth_init_ctx,
116218792Snp	bsdauth_query,
117218792Snp	bsdauth_respond,
118218792Snp	bsdauth_free_ctx
119218792Snp};
120218792Snp
121218792SnpKbdintDevice mm_bsdauth_device = {
122237263Snp	"bsdauth",
123237263Snp	bsdauth_init_ctx,
124237263Snp	mm_bsdauth_query,
125237263Snp	mm_bsdauth_respond,
126228561Snp	bsdauth_free_ctx
127228561Snp};
128237263Snp#endif
129228561Snp