xref: /dflybsd-src/crypto/openssh/gss-serv.c (revision ba1276acd1c8c22d225b1bcf370a14c878644f44)
1*ba1276acSMatthew Dillon /* $OpenBSD: gss-serv.c,v 1.32 2020/03/13 03:17:07 djm Exp $ */
2*ba1276acSMatthew Dillon 
3*ba1276acSMatthew Dillon /*
4*ba1276acSMatthew Dillon  * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
5*ba1276acSMatthew Dillon  *
6*ba1276acSMatthew Dillon  * Redistribution and use in source and binary forms, with or without
7*ba1276acSMatthew Dillon  * modification, are permitted provided that the following conditions
8*ba1276acSMatthew Dillon  * are met:
9*ba1276acSMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
10*ba1276acSMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
11*ba1276acSMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
12*ba1276acSMatthew Dillon  *    notice, this list of conditions and the following disclaimer in the
13*ba1276acSMatthew Dillon  *    documentation and/or other materials provided with the distribution.
14*ba1276acSMatthew Dillon  *
15*ba1276acSMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
16*ba1276acSMatthew Dillon  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17*ba1276acSMatthew Dillon  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18*ba1276acSMatthew Dillon  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19*ba1276acSMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20*ba1276acSMatthew Dillon  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21*ba1276acSMatthew Dillon  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22*ba1276acSMatthew Dillon  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23*ba1276acSMatthew Dillon  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24*ba1276acSMatthew Dillon  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*ba1276acSMatthew Dillon  */
26*ba1276acSMatthew Dillon 
27*ba1276acSMatthew Dillon #include "includes.h"
28*ba1276acSMatthew Dillon 
29*ba1276acSMatthew Dillon #ifdef GSSAPI
30*ba1276acSMatthew Dillon 
31*ba1276acSMatthew Dillon #include <sys/types.h>
32*ba1276acSMatthew Dillon 
33*ba1276acSMatthew Dillon #include <stdarg.h>
34*ba1276acSMatthew Dillon #include <string.h>
35*ba1276acSMatthew Dillon #include <unistd.h>
36*ba1276acSMatthew Dillon 
37*ba1276acSMatthew Dillon #include "openbsd-compat/sys-queue.h"
38*ba1276acSMatthew Dillon #include "xmalloc.h"
39*ba1276acSMatthew Dillon #include "sshkey.h"
40*ba1276acSMatthew Dillon #include "hostfile.h"
41*ba1276acSMatthew Dillon #include "auth.h"
42*ba1276acSMatthew Dillon #include "log.h"
43*ba1276acSMatthew Dillon #include "channels.h"
44*ba1276acSMatthew Dillon #include "session.h"
45*ba1276acSMatthew Dillon #include "misc.h"
46*ba1276acSMatthew Dillon #include "servconf.h"
47*ba1276acSMatthew Dillon 
48*ba1276acSMatthew Dillon #include "ssh-gss.h"
49*ba1276acSMatthew Dillon 
50*ba1276acSMatthew Dillon extern ServerOptions options;
51*ba1276acSMatthew Dillon 
52*ba1276acSMatthew Dillon static ssh_gssapi_client gssapi_client =
53*ba1276acSMatthew Dillon     { GSS_C_EMPTY_BUFFER, GSS_C_EMPTY_BUFFER,
54*ba1276acSMatthew Dillon     GSS_C_NO_CREDENTIAL, NULL, {NULL, NULL, NULL, NULL}};
55*ba1276acSMatthew Dillon 
56*ba1276acSMatthew Dillon ssh_gssapi_mech gssapi_null_mech =
57*ba1276acSMatthew Dillon     { NULL, NULL, {0, NULL}, NULL, NULL, NULL, NULL};
58*ba1276acSMatthew Dillon 
59*ba1276acSMatthew Dillon #ifdef KRB5
60*ba1276acSMatthew Dillon extern ssh_gssapi_mech gssapi_kerberos_mech;
61*ba1276acSMatthew Dillon #endif
62*ba1276acSMatthew Dillon 
63*ba1276acSMatthew Dillon ssh_gssapi_mech* supported_mechs[]= {
64*ba1276acSMatthew Dillon #ifdef KRB5
65*ba1276acSMatthew Dillon 	&gssapi_kerberos_mech,
66*ba1276acSMatthew Dillon #endif
67*ba1276acSMatthew Dillon 	&gssapi_null_mech,
68*ba1276acSMatthew Dillon };
69*ba1276acSMatthew Dillon 
70*ba1276acSMatthew Dillon /*
71*ba1276acSMatthew Dillon  * ssh_gssapi_supported_oids() can cause sandbox violations, so prepare the
72*ba1276acSMatthew Dillon  * list of supported mechanisms before privsep is set up.
73*ba1276acSMatthew Dillon  */
74*ba1276acSMatthew Dillon static gss_OID_set supported_oids;
75*ba1276acSMatthew Dillon 
76*ba1276acSMatthew Dillon void
ssh_gssapi_prepare_supported_oids(void)77*ba1276acSMatthew Dillon ssh_gssapi_prepare_supported_oids(void)
78*ba1276acSMatthew Dillon {
79*ba1276acSMatthew Dillon 	ssh_gssapi_supported_oids(&supported_oids);
80*ba1276acSMatthew Dillon }
81*ba1276acSMatthew Dillon 
82*ba1276acSMatthew Dillon OM_uint32
ssh_gssapi_test_oid_supported(OM_uint32 * ms,gss_OID member,int * present)83*ba1276acSMatthew Dillon ssh_gssapi_test_oid_supported(OM_uint32 *ms, gss_OID member, int *present)
84*ba1276acSMatthew Dillon {
85*ba1276acSMatthew Dillon 	if (supported_oids == NULL)
86*ba1276acSMatthew Dillon 		ssh_gssapi_prepare_supported_oids();
87*ba1276acSMatthew Dillon 	return gss_test_oid_set_member(ms, member, supported_oids, present);
88*ba1276acSMatthew Dillon }
89*ba1276acSMatthew Dillon 
90*ba1276acSMatthew Dillon /*
91*ba1276acSMatthew Dillon  * Acquire credentials for a server running on the current host.
92*ba1276acSMatthew Dillon  * Requires that the context structure contains a valid OID
93*ba1276acSMatthew Dillon  */
94*ba1276acSMatthew Dillon 
95*ba1276acSMatthew Dillon /* Returns a GSSAPI error code */
96*ba1276acSMatthew Dillon /* Privileged (called from ssh_gssapi_server_ctx) */
97*ba1276acSMatthew Dillon static OM_uint32
ssh_gssapi_acquire_cred(Gssctxt * ctx)98*ba1276acSMatthew Dillon ssh_gssapi_acquire_cred(Gssctxt *ctx)
99*ba1276acSMatthew Dillon {
100*ba1276acSMatthew Dillon 	OM_uint32 status;
101*ba1276acSMatthew Dillon 	char lname[NI_MAXHOST];
102*ba1276acSMatthew Dillon 	gss_OID_set oidset;
103*ba1276acSMatthew Dillon 
104*ba1276acSMatthew Dillon 	if (options.gss_strict_acceptor) {
105*ba1276acSMatthew Dillon 		gss_create_empty_oid_set(&status, &oidset);
106*ba1276acSMatthew Dillon 		gss_add_oid_set_member(&status, ctx->oid, &oidset);
107*ba1276acSMatthew Dillon 
108*ba1276acSMatthew Dillon 		if (gethostname(lname, HOST_NAME_MAX)) {
109*ba1276acSMatthew Dillon 			gss_release_oid_set(&status, &oidset);
110*ba1276acSMatthew Dillon 			return (-1);
111*ba1276acSMatthew Dillon 		}
112*ba1276acSMatthew Dillon 
113*ba1276acSMatthew Dillon 		if (GSS_ERROR(ssh_gssapi_import_name(ctx, lname))) {
114*ba1276acSMatthew Dillon 			gss_release_oid_set(&status, &oidset);
115*ba1276acSMatthew Dillon 			return (ctx->major);
116*ba1276acSMatthew Dillon 		}
117*ba1276acSMatthew Dillon 
118*ba1276acSMatthew Dillon 		if ((ctx->major = gss_acquire_cred(&ctx->minor,
119*ba1276acSMatthew Dillon 		    ctx->name, 0, oidset, GSS_C_ACCEPT, &ctx->creds,
120*ba1276acSMatthew Dillon 		    NULL, NULL)))
121*ba1276acSMatthew Dillon 			ssh_gssapi_error(ctx);
122*ba1276acSMatthew Dillon 
123*ba1276acSMatthew Dillon 		gss_release_oid_set(&status, &oidset);
124*ba1276acSMatthew Dillon 		return (ctx->major);
125*ba1276acSMatthew Dillon 	} else {
126*ba1276acSMatthew Dillon 		ctx->name = GSS_C_NO_NAME;
127*ba1276acSMatthew Dillon 		ctx->creds = GSS_C_NO_CREDENTIAL;
128*ba1276acSMatthew Dillon 	}
129*ba1276acSMatthew Dillon 	return GSS_S_COMPLETE;
130*ba1276acSMatthew Dillon }
131*ba1276acSMatthew Dillon 
132*ba1276acSMatthew Dillon /* Privileged */
133*ba1276acSMatthew Dillon OM_uint32
ssh_gssapi_server_ctx(Gssctxt ** ctx,gss_OID oid)134*ba1276acSMatthew Dillon ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID oid)
135*ba1276acSMatthew Dillon {
136*ba1276acSMatthew Dillon 	if (*ctx)
137*ba1276acSMatthew Dillon 		ssh_gssapi_delete_ctx(ctx);
138*ba1276acSMatthew Dillon 	ssh_gssapi_build_ctx(ctx);
139*ba1276acSMatthew Dillon 	ssh_gssapi_set_oid(*ctx, oid);
140*ba1276acSMatthew Dillon 	return (ssh_gssapi_acquire_cred(*ctx));
141*ba1276acSMatthew Dillon }
142*ba1276acSMatthew Dillon 
143*ba1276acSMatthew Dillon /* Unprivileged */
144*ba1276acSMatthew Dillon void
ssh_gssapi_supported_oids(gss_OID_set * oidset)145*ba1276acSMatthew Dillon ssh_gssapi_supported_oids(gss_OID_set *oidset)
146*ba1276acSMatthew Dillon {
147*ba1276acSMatthew Dillon 	int i = 0;
148*ba1276acSMatthew Dillon 	OM_uint32 min_status;
149*ba1276acSMatthew Dillon 	int present;
150*ba1276acSMatthew Dillon 	gss_OID_set supported;
151*ba1276acSMatthew Dillon 
152*ba1276acSMatthew Dillon 	gss_create_empty_oid_set(&min_status, oidset);
153*ba1276acSMatthew Dillon 	gss_indicate_mechs(&min_status, &supported);
154*ba1276acSMatthew Dillon 
155*ba1276acSMatthew Dillon 	while (supported_mechs[i]->name != NULL) {
156*ba1276acSMatthew Dillon 		if (GSS_ERROR(gss_test_oid_set_member(&min_status,
157*ba1276acSMatthew Dillon 		    &supported_mechs[i]->oid, supported, &present)))
158*ba1276acSMatthew Dillon 			present = 0;
159*ba1276acSMatthew Dillon 		if (present)
160*ba1276acSMatthew Dillon 			gss_add_oid_set_member(&min_status,
161*ba1276acSMatthew Dillon 			    &supported_mechs[i]->oid, oidset);
162*ba1276acSMatthew Dillon 		i++;
163*ba1276acSMatthew Dillon 	}
164*ba1276acSMatthew Dillon 
165*ba1276acSMatthew Dillon 	gss_release_oid_set(&min_status, &supported);
166*ba1276acSMatthew Dillon }
167*ba1276acSMatthew Dillon 
168*ba1276acSMatthew Dillon 
169*ba1276acSMatthew Dillon /* Wrapper around accept_sec_context
170*ba1276acSMatthew Dillon  * Requires that the context contains:
171*ba1276acSMatthew Dillon  *    oid
172*ba1276acSMatthew Dillon  *    credentials	(from ssh_gssapi_acquire_cred)
173*ba1276acSMatthew Dillon  */
174*ba1276acSMatthew Dillon /* Privileged */
175*ba1276acSMatthew Dillon OM_uint32
ssh_gssapi_accept_ctx(Gssctxt * ctx,gss_buffer_desc * recv_tok,gss_buffer_desc * send_tok,OM_uint32 * flags)176*ba1276acSMatthew Dillon ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *recv_tok,
177*ba1276acSMatthew Dillon     gss_buffer_desc *send_tok, OM_uint32 *flags)
178*ba1276acSMatthew Dillon {
179*ba1276acSMatthew Dillon 	OM_uint32 status;
180*ba1276acSMatthew Dillon 	gss_OID mech;
181*ba1276acSMatthew Dillon 
182*ba1276acSMatthew Dillon 	ctx->major = gss_accept_sec_context(&ctx->minor,
183*ba1276acSMatthew Dillon 	    &ctx->context, ctx->creds, recv_tok,
184*ba1276acSMatthew Dillon 	    GSS_C_NO_CHANNEL_BINDINGS, &ctx->client, &mech,
185*ba1276acSMatthew Dillon 	    send_tok, flags, NULL, &ctx->client_creds);
186*ba1276acSMatthew Dillon 
187*ba1276acSMatthew Dillon 	if (GSS_ERROR(ctx->major))
188*ba1276acSMatthew Dillon 		ssh_gssapi_error(ctx);
189*ba1276acSMatthew Dillon 
190*ba1276acSMatthew Dillon 	if (ctx->client_creds)
191*ba1276acSMatthew Dillon 		debug("Received some client credentials");
192*ba1276acSMatthew Dillon 	else
193*ba1276acSMatthew Dillon 		debug("Got no client credentials");
194*ba1276acSMatthew Dillon 
195*ba1276acSMatthew Dillon 	status = ctx->major;
196*ba1276acSMatthew Dillon 
197*ba1276acSMatthew Dillon 	/* Now, if we're complete and we have the right flags, then
198*ba1276acSMatthew Dillon 	 * we flag the user as also having been authenticated
199*ba1276acSMatthew Dillon 	 */
200*ba1276acSMatthew Dillon 
201*ba1276acSMatthew Dillon 	if (((flags == NULL) || ((*flags & GSS_C_MUTUAL_FLAG) &&
202*ba1276acSMatthew Dillon 	    (*flags & GSS_C_INTEG_FLAG))) && (ctx->major == GSS_S_COMPLETE)) {
203*ba1276acSMatthew Dillon 		if (ssh_gssapi_getclient(ctx, &gssapi_client))
204*ba1276acSMatthew Dillon 			fatal("Couldn't convert client name");
205*ba1276acSMatthew Dillon 	}
206*ba1276acSMatthew Dillon 
207*ba1276acSMatthew Dillon 	return (status);
208*ba1276acSMatthew Dillon }
209*ba1276acSMatthew Dillon 
210*ba1276acSMatthew Dillon /*
211*ba1276acSMatthew Dillon  * This parses an exported name, extracting the mechanism specific portion
212*ba1276acSMatthew Dillon  * to use for ACL checking. It verifies that the name belongs the mechanism
213*ba1276acSMatthew Dillon  * originally selected.
214*ba1276acSMatthew Dillon  */
215*ba1276acSMatthew Dillon static OM_uint32
ssh_gssapi_parse_ename(Gssctxt * ctx,gss_buffer_t ename,gss_buffer_t name)216*ba1276acSMatthew Dillon ssh_gssapi_parse_ename(Gssctxt *ctx, gss_buffer_t ename, gss_buffer_t name)
217*ba1276acSMatthew Dillon {
218*ba1276acSMatthew Dillon 	u_char *tok;
219*ba1276acSMatthew Dillon 	OM_uint32 offset;
220*ba1276acSMatthew Dillon 	OM_uint32 oidl;
221*ba1276acSMatthew Dillon 
222*ba1276acSMatthew Dillon 	tok = ename->value;
223*ba1276acSMatthew Dillon 
224*ba1276acSMatthew Dillon 	/*
225*ba1276acSMatthew Dillon 	 * Check that ename is long enough for all of the fixed length
226*ba1276acSMatthew Dillon 	 * header, and that the initial ID bytes are correct
227*ba1276acSMatthew Dillon 	 */
228*ba1276acSMatthew Dillon 
229*ba1276acSMatthew Dillon 	if (ename->length < 6 || memcmp(tok, "\x04\x01", 2) != 0)
230*ba1276acSMatthew Dillon 		return GSS_S_FAILURE;
231*ba1276acSMatthew Dillon 
232*ba1276acSMatthew Dillon 	/*
233*ba1276acSMatthew Dillon 	 * Extract the OID, and check it. Here GSSAPI breaks with tradition
234*ba1276acSMatthew Dillon 	 * and does use the OID type and length bytes. To confuse things
235*ba1276acSMatthew Dillon 	 * there are two lengths - the first including these, and the
236*ba1276acSMatthew Dillon 	 * second without.
237*ba1276acSMatthew Dillon 	 */
238*ba1276acSMatthew Dillon 
239*ba1276acSMatthew Dillon 	oidl = get_u16(tok+2); /* length including next two bytes */
240*ba1276acSMatthew Dillon 	oidl = oidl-2; /* turn it into the _real_ length of the variable OID */
241*ba1276acSMatthew Dillon 
242*ba1276acSMatthew Dillon 	/*
243*ba1276acSMatthew Dillon 	 * Check the BER encoding for correct type and length, that the
244*ba1276acSMatthew Dillon 	 * string is long enough and that the OID matches that in our context
245*ba1276acSMatthew Dillon 	 */
246*ba1276acSMatthew Dillon 	if (tok[4] != 0x06 || tok[5] != oidl ||
247*ba1276acSMatthew Dillon 	    ename->length < oidl+6 ||
248*ba1276acSMatthew Dillon 	    !ssh_gssapi_check_oid(ctx, tok+6, oidl))
249*ba1276acSMatthew Dillon 		return GSS_S_FAILURE;
250*ba1276acSMatthew Dillon 
251*ba1276acSMatthew Dillon 	offset = oidl+6;
252*ba1276acSMatthew Dillon 
253*ba1276acSMatthew Dillon 	if (ename->length < offset+4)
254*ba1276acSMatthew Dillon 		return GSS_S_FAILURE;
255*ba1276acSMatthew Dillon 
256*ba1276acSMatthew Dillon 	name->length = get_u32(tok+offset);
257*ba1276acSMatthew Dillon 	offset += 4;
258*ba1276acSMatthew Dillon 
259*ba1276acSMatthew Dillon 	if (UINT_MAX - offset < name->length)
260*ba1276acSMatthew Dillon 		return GSS_S_FAILURE;
261*ba1276acSMatthew Dillon 	if (ename->length < offset+name->length)
262*ba1276acSMatthew Dillon 		return GSS_S_FAILURE;
263*ba1276acSMatthew Dillon 
264*ba1276acSMatthew Dillon 	name->value = xmalloc(name->length+1);
265*ba1276acSMatthew Dillon 	memcpy(name->value, tok+offset, name->length);
266*ba1276acSMatthew Dillon 	((char *)name->value)[name->length] = 0;
267*ba1276acSMatthew Dillon 
268*ba1276acSMatthew Dillon 	return GSS_S_COMPLETE;
269*ba1276acSMatthew Dillon }
270*ba1276acSMatthew Dillon 
271*ba1276acSMatthew Dillon /* Extract the client details from a given context. This can only reliably
272*ba1276acSMatthew Dillon  * be called once for a context */
273*ba1276acSMatthew Dillon 
274*ba1276acSMatthew Dillon /* Privileged (called from accept_secure_ctx) */
275*ba1276acSMatthew Dillon OM_uint32
ssh_gssapi_getclient(Gssctxt * ctx,ssh_gssapi_client * client)276*ba1276acSMatthew Dillon ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
277*ba1276acSMatthew Dillon {
278*ba1276acSMatthew Dillon 	int i = 0;
279*ba1276acSMatthew Dillon 
280*ba1276acSMatthew Dillon 	gss_buffer_desc ename;
281*ba1276acSMatthew Dillon 
282*ba1276acSMatthew Dillon 	client->mech = NULL;
283*ba1276acSMatthew Dillon 
284*ba1276acSMatthew Dillon 	while (supported_mechs[i]->name != NULL) {
285*ba1276acSMatthew Dillon 		if (supported_mechs[i]->oid.length == ctx->oid->length &&
286*ba1276acSMatthew Dillon 		    (memcmp(supported_mechs[i]->oid.elements,
287*ba1276acSMatthew Dillon 		    ctx->oid->elements, ctx->oid->length) == 0))
288*ba1276acSMatthew Dillon 			client->mech = supported_mechs[i];
289*ba1276acSMatthew Dillon 		i++;
290*ba1276acSMatthew Dillon 	}
291*ba1276acSMatthew Dillon 
292*ba1276acSMatthew Dillon 	if (client->mech == NULL)
293*ba1276acSMatthew Dillon 		return GSS_S_FAILURE;
294*ba1276acSMatthew Dillon 
295*ba1276acSMatthew Dillon 	if ((ctx->major = gss_display_name(&ctx->minor, ctx->client,
296*ba1276acSMatthew Dillon 	    &client->displayname, NULL))) {
297*ba1276acSMatthew Dillon 		ssh_gssapi_error(ctx);
298*ba1276acSMatthew Dillon 		return (ctx->major);
299*ba1276acSMatthew Dillon 	}
300*ba1276acSMatthew Dillon 
301*ba1276acSMatthew Dillon 	if ((ctx->major = gss_export_name(&ctx->minor, ctx->client,
302*ba1276acSMatthew Dillon 	    &ename))) {
303*ba1276acSMatthew Dillon 		ssh_gssapi_error(ctx);
304*ba1276acSMatthew Dillon 		return (ctx->major);
305*ba1276acSMatthew Dillon 	}
306*ba1276acSMatthew Dillon 
307*ba1276acSMatthew Dillon 	if ((ctx->major = ssh_gssapi_parse_ename(ctx,&ename,
308*ba1276acSMatthew Dillon 	    &client->exportedname))) {
309*ba1276acSMatthew Dillon 		return (ctx->major);
310*ba1276acSMatthew Dillon 	}
311*ba1276acSMatthew Dillon 
312*ba1276acSMatthew Dillon 	/* We can't copy this structure, so we just move the pointer to it */
313*ba1276acSMatthew Dillon 	client->creds = ctx->client_creds;
314*ba1276acSMatthew Dillon 	ctx->client_creds = GSS_C_NO_CREDENTIAL;
315*ba1276acSMatthew Dillon 	return (ctx->major);
316*ba1276acSMatthew Dillon }
317*ba1276acSMatthew Dillon 
318*ba1276acSMatthew Dillon /* As user - called on fatal/exit */
319*ba1276acSMatthew Dillon void
ssh_gssapi_cleanup_creds(void)320*ba1276acSMatthew Dillon ssh_gssapi_cleanup_creds(void)
321*ba1276acSMatthew Dillon {
322*ba1276acSMatthew Dillon 	if (gssapi_client.store.filename != NULL) {
323*ba1276acSMatthew Dillon 		/* Unlink probably isn't sufficient */
324*ba1276acSMatthew Dillon 		debug("removing gssapi cred file\"%s\"",
325*ba1276acSMatthew Dillon 		    gssapi_client.store.filename);
326*ba1276acSMatthew Dillon 		unlink(gssapi_client.store.filename);
327*ba1276acSMatthew Dillon 	}
328*ba1276acSMatthew Dillon }
329*ba1276acSMatthew Dillon 
330*ba1276acSMatthew Dillon /* As user */
331*ba1276acSMatthew Dillon void
ssh_gssapi_storecreds(void)332*ba1276acSMatthew Dillon ssh_gssapi_storecreds(void)
333*ba1276acSMatthew Dillon {
334*ba1276acSMatthew Dillon 	if (gssapi_client.mech && gssapi_client.mech->storecreds) {
335*ba1276acSMatthew Dillon 		(*gssapi_client.mech->storecreds)(&gssapi_client);
336*ba1276acSMatthew Dillon 	} else
337*ba1276acSMatthew Dillon 		debug("ssh_gssapi_storecreds: Not a GSSAPI mechanism");
338*ba1276acSMatthew Dillon }
339*ba1276acSMatthew Dillon 
340*ba1276acSMatthew Dillon /* This allows GSSAPI methods to do things to the child's environment based
341*ba1276acSMatthew Dillon  * on the passed authentication process and credentials.
342*ba1276acSMatthew Dillon  */
343*ba1276acSMatthew Dillon /* As user */
344*ba1276acSMatthew Dillon void
ssh_gssapi_do_child(char *** envp,u_int * envsizep)345*ba1276acSMatthew Dillon ssh_gssapi_do_child(char ***envp, u_int *envsizep)
346*ba1276acSMatthew Dillon {
347*ba1276acSMatthew Dillon 
348*ba1276acSMatthew Dillon 	if (gssapi_client.store.envvar != NULL &&
349*ba1276acSMatthew Dillon 	    gssapi_client.store.envval != NULL) {
350*ba1276acSMatthew Dillon 		debug("Setting %s to %s", gssapi_client.store.envvar,
351*ba1276acSMatthew Dillon 		    gssapi_client.store.envval);
352*ba1276acSMatthew Dillon 		child_set_env(envp, envsizep, gssapi_client.store.envvar,
353*ba1276acSMatthew Dillon 		    gssapi_client.store.envval);
354*ba1276acSMatthew Dillon 	}
355*ba1276acSMatthew Dillon }
356*ba1276acSMatthew Dillon 
357*ba1276acSMatthew Dillon /* Privileged */
358*ba1276acSMatthew Dillon int
ssh_gssapi_userok(char * user)359*ba1276acSMatthew Dillon ssh_gssapi_userok(char *user)
360*ba1276acSMatthew Dillon {
361*ba1276acSMatthew Dillon 	OM_uint32 lmin;
362*ba1276acSMatthew Dillon 
363*ba1276acSMatthew Dillon 	if (gssapi_client.exportedname.length == 0 ||
364*ba1276acSMatthew Dillon 	    gssapi_client.exportedname.value == NULL) {
365*ba1276acSMatthew Dillon 		debug("No suitable client data");
366*ba1276acSMatthew Dillon 		return 0;
367*ba1276acSMatthew Dillon 	}
368*ba1276acSMatthew Dillon 	if (gssapi_client.mech && gssapi_client.mech->userok)
369*ba1276acSMatthew Dillon 		if ((*gssapi_client.mech->userok)(&gssapi_client, user))
370*ba1276acSMatthew Dillon 			return 1;
371*ba1276acSMatthew Dillon 		else {
372*ba1276acSMatthew Dillon 			/* Destroy delegated credentials if userok fails */
373*ba1276acSMatthew Dillon 			gss_release_buffer(&lmin, &gssapi_client.displayname);
374*ba1276acSMatthew Dillon 			gss_release_buffer(&lmin, &gssapi_client.exportedname);
375*ba1276acSMatthew Dillon 			gss_release_cred(&lmin, &gssapi_client.creds);
376*ba1276acSMatthew Dillon 			explicit_bzero(&gssapi_client,
377*ba1276acSMatthew Dillon 			    sizeof(ssh_gssapi_client));
378*ba1276acSMatthew Dillon 			return 0;
379*ba1276acSMatthew Dillon 		}
380*ba1276acSMatthew Dillon 	else
381*ba1276acSMatthew Dillon 		debug("ssh_gssapi_userok: Unknown GSSAPI mechanism");
382*ba1276acSMatthew Dillon 	return (0);
383*ba1276acSMatthew Dillon }
384*ba1276acSMatthew Dillon 
385*ba1276acSMatthew Dillon /* Privileged */
386*ba1276acSMatthew Dillon OM_uint32
ssh_gssapi_checkmic(Gssctxt * ctx,gss_buffer_t gssbuf,gss_buffer_t gssmic)387*ba1276acSMatthew Dillon ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
388*ba1276acSMatthew Dillon {
389*ba1276acSMatthew Dillon 	ctx->major = gss_verify_mic(&ctx->minor, ctx->context,
390*ba1276acSMatthew Dillon 	    gssbuf, gssmic, NULL);
391*ba1276acSMatthew Dillon 
392*ba1276acSMatthew Dillon 	return (ctx->major);
393*ba1276acSMatthew Dillon }
394*ba1276acSMatthew Dillon 
395*ba1276acSMatthew Dillon /* Privileged */
ssh_gssapi_displayname(void)396*ba1276acSMatthew Dillon const char *ssh_gssapi_displayname(void)
397*ba1276acSMatthew Dillon {
398*ba1276acSMatthew Dillon 	if (gssapi_client.displayname.length == 0 ||
399*ba1276acSMatthew Dillon 	    gssapi_client.displayname.value == NULL)
400*ba1276acSMatthew Dillon 		return NULL;
401*ba1276acSMatthew Dillon 	return (char *)gssapi_client.displayname.value;
402*ba1276acSMatthew Dillon }
403*ba1276acSMatthew Dillon 
404*ba1276acSMatthew Dillon #endif
405