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