xref: /titanic_41/usr/src/cmd/ssh/sshd/auth-bsdauth.c (revision 9a8058b57457911fab0e3b4b6f0a97740e7a816d)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
5*7c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
6*7c478bd9Sstevel@tonic-gate  * are met:
7*7c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
8*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
9*7c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
10*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
11*7c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
12*7c478bd9Sstevel@tonic-gate  *
13*7c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14*7c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15*7c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16*7c478bd9Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17*7c478bd9Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18*7c478bd9Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19*7c478bd9Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20*7c478bd9Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21*7c478bd9Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22*7c478bd9Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23*7c478bd9Sstevel@tonic-gate  */
24*7c478bd9Sstevel@tonic-gate #include "includes.h"
25*7c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: auth-bsdauth.c,v 1.5 2002/06/30 21:59:45 deraadt Exp $");
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #ifdef BSD_AUTH
30*7c478bd9Sstevel@tonic-gate #include "xmalloc.h"
31*7c478bd9Sstevel@tonic-gate #include "auth.h"
32*7c478bd9Sstevel@tonic-gate #include "log.h"
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate static void *
bsdauth_init_ctx(Authctxt * authctxt)35*7c478bd9Sstevel@tonic-gate bsdauth_init_ctx(Authctxt *authctxt)
36*7c478bd9Sstevel@tonic-gate {
37*7c478bd9Sstevel@tonic-gate 	return authctxt;
38*7c478bd9Sstevel@tonic-gate }
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate int
bsdauth_query(void * ctx,char ** name,char ** infotxt,u_int * numprompts,char *** prompts,u_int ** echo_on)41*7c478bd9Sstevel@tonic-gate bsdauth_query(void *ctx, char **name, char **infotxt,
42*7c478bd9Sstevel@tonic-gate    u_int *numprompts, char ***prompts, u_int **echo_on)
43*7c478bd9Sstevel@tonic-gate {
44*7c478bd9Sstevel@tonic-gate 	Authctxt *authctxt = ctx;
45*7c478bd9Sstevel@tonic-gate 	char *challenge = NULL;
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate 	if (authctxt->as != NULL) {
48*7c478bd9Sstevel@tonic-gate 		debug2("bsdauth_query: try reuse session");
49*7c478bd9Sstevel@tonic-gate 		challenge = auth_getitem(authctxt->as, AUTHV_CHALLENGE);
50*7c478bd9Sstevel@tonic-gate 		if (challenge == NULL) {
51*7c478bd9Sstevel@tonic-gate 			auth_close(authctxt->as);
52*7c478bd9Sstevel@tonic-gate 			authctxt->as = NULL;
53*7c478bd9Sstevel@tonic-gate 		}
54*7c478bd9Sstevel@tonic-gate 	}
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate 	if (challenge == NULL) {
57*7c478bd9Sstevel@tonic-gate 		debug2("bsdauth_query: new bsd auth session");
58*7c478bd9Sstevel@tonic-gate 		debug3("bsdauth_query: style %s",
59*7c478bd9Sstevel@tonic-gate 		    authctxt->style ? authctxt->style : "<default>");
60*7c478bd9Sstevel@tonic-gate 		authctxt->as = auth_userchallenge(authctxt->user,
61*7c478bd9Sstevel@tonic-gate 		    authctxt->style, "auth-ssh", &challenge);
62*7c478bd9Sstevel@tonic-gate 		if (authctxt->as == NULL)
63*7c478bd9Sstevel@tonic-gate 			challenge = NULL;
64*7c478bd9Sstevel@tonic-gate 		debug2("bsdauth_query: <%s>", challenge ? challenge : "empty");
65*7c478bd9Sstevel@tonic-gate 	}
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 	if (challenge == NULL)
68*7c478bd9Sstevel@tonic-gate 		return -1;
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate 	*name = xstrdup("");
71*7c478bd9Sstevel@tonic-gate 	*infotxt = xstrdup("");
72*7c478bd9Sstevel@tonic-gate 	*numprompts = 1;
73*7c478bd9Sstevel@tonic-gate 	*prompts = xmalloc(*numprompts * sizeof(char *));
74*7c478bd9Sstevel@tonic-gate 	*echo_on = xmalloc(*numprompts * sizeof(u_int));
75*7c478bd9Sstevel@tonic-gate 	(*echo_on)[0] = 0;
76*7c478bd9Sstevel@tonic-gate 	(*prompts)[0] = xstrdup(challenge);
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 	return 0;
79*7c478bd9Sstevel@tonic-gate }
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate int
bsdauth_respond(void * ctx,u_int numresponses,char ** responses)82*7c478bd9Sstevel@tonic-gate bsdauth_respond(void *ctx, u_int numresponses, char **responses)
83*7c478bd9Sstevel@tonic-gate {
84*7c478bd9Sstevel@tonic-gate 	Authctxt *authctxt = ctx;
85*7c478bd9Sstevel@tonic-gate 	int authok;
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 	if (authctxt->as == 0)
88*7c478bd9Sstevel@tonic-gate 		error("bsdauth_respond: no bsd auth session");
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 	if (numresponses != 1)
91*7c478bd9Sstevel@tonic-gate 		return -1;
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 	authok = auth_userresponse(authctxt->as, responses[0], 0);
94*7c478bd9Sstevel@tonic-gate 	authctxt->as = NULL;
95*7c478bd9Sstevel@tonic-gate 	debug3("bsdauth_respond: <%s> = <%d>", responses[0], authok);
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 	return (authok == 0) ? -1 : 0;
98*7c478bd9Sstevel@tonic-gate }
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate static void
bsdauth_free_ctx(void * ctx)101*7c478bd9Sstevel@tonic-gate bsdauth_free_ctx(void *ctx)
102*7c478bd9Sstevel@tonic-gate {
103*7c478bd9Sstevel@tonic-gate 	Authctxt *authctxt = ctx;
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	if (authctxt && authctxt->as) {
106*7c478bd9Sstevel@tonic-gate 		auth_close(authctxt->as);
107*7c478bd9Sstevel@tonic-gate 		authctxt->as = NULL;
108*7c478bd9Sstevel@tonic-gate 	}
109*7c478bd9Sstevel@tonic-gate }
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate KbdintDevice bsdauth_device = {
112*7c478bd9Sstevel@tonic-gate 	"bsdauth",
113*7c478bd9Sstevel@tonic-gate 	bsdauth_init_ctx,
114*7c478bd9Sstevel@tonic-gate 	bsdauth_query,
115*7c478bd9Sstevel@tonic-gate 	bsdauth_respond,
116*7c478bd9Sstevel@tonic-gate 	bsdauth_free_ctx
117*7c478bd9Sstevel@tonic-gate };
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate KbdintDevice mm_bsdauth_device = {
120*7c478bd9Sstevel@tonic-gate 	"bsdauth",
121*7c478bd9Sstevel@tonic-gate 	bsdauth_init_ctx,
122*7c478bd9Sstevel@tonic-gate 	mm_bsdauth_query,
123*7c478bd9Sstevel@tonic-gate 	mm_bsdauth_respond,
124*7c478bd9Sstevel@tonic-gate 	bsdauth_free_ctx
125*7c478bd9Sstevel@tonic-gate };
126*7c478bd9Sstevel@tonic-gate #endif
127