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