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