gss-serv.c revision 126274
1126274Sdes/*	$OpenBSD: gss-serv.c,v 1.5 2003/11/17 11:06:07 markus Exp $	*/
2124208Sdes
3124208Sdes/*
4124208Sdes * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
5124208Sdes *
6124208Sdes * Redistribution and use in source and binary forms, with or without
7124208Sdes * modification, are permitted provided that the following conditions
8124208Sdes * are met:
9124208Sdes * 1. Redistributions of source code must retain the above copyright
10124208Sdes *    notice, this list of conditions and the following disclaimer.
11124208Sdes * 2. Redistributions in binary form must reproduce the above copyright
12124208Sdes *    notice, this list of conditions and the following disclaimer in the
13124208Sdes *    documentation and/or other materials provided with the distribution.
14124208Sdes *
15124208Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
16124208Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17124208Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18124208Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19124208Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20124208Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21124208Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22124208Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23124208Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24124208Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25124208Sdes */
26124208Sdes
27124208Sdes#include "includes.h"
28124208Sdes
29124208Sdes#ifdef GSSAPI
30124208Sdes
31124208Sdes#include "bufaux.h"
32124208Sdes#include "compat.h"
33124208Sdes#include "auth.h"
34124208Sdes#include "log.h"
35124208Sdes#include "channels.h"
36124208Sdes#include "session.h"
37124208Sdes#include "servconf.h"
38124208Sdes#include "monitor_wrap.h"
39124208Sdes#include "xmalloc.h"
40124208Sdes#include "getput.h"
41124208Sdes
42124208Sdes#include "ssh-gss.h"
43124208Sdes
44124208Sdesextern ServerOptions options;
45124208Sdes
46124208Sdesstatic ssh_gssapi_client gssapi_client =
47124208Sdes    { GSS_C_EMPTY_BUFFER, GSS_C_EMPTY_BUFFER,
48124208Sdes    GSS_C_NO_CREDENTIAL, NULL, {NULL, NULL, NULL}};
49124208Sdes
50124208Sdesssh_gssapi_mech gssapi_null_mech =
51124208Sdes    { NULL, NULL, {0, NULL}, NULL, NULL, NULL, NULL};
52124208Sdes
53124208Sdes#ifdef KRB5
54124208Sdesextern ssh_gssapi_mech gssapi_kerberos_mech;
55124208Sdes#endif
56124208Sdes
57124208Sdesssh_gssapi_mech* supported_mechs[]= {
58124208Sdes#ifdef KRB5
59124208Sdes	&gssapi_kerberos_mech,
60124208Sdes#endif
61124208Sdes	&gssapi_null_mech,
62124208Sdes};
63124208Sdes
64124208Sdes/* Unpriviledged */
65124208Sdesvoid
66124208Sdesssh_gssapi_supported_oids(gss_OID_set *oidset)
67124208Sdes{
68124208Sdes	int i = 0;
69124208Sdes	OM_uint32 min_status;
70124208Sdes	int present;
71124208Sdes	gss_OID_set supported;
72124208Sdes
73124208Sdes	gss_create_empty_oid_set(&min_status, oidset);
74124208Sdes	gss_indicate_mechs(&min_status, &supported);
75124208Sdes
76124208Sdes	while (supported_mechs[i]->name != NULL) {
77124208Sdes		if (GSS_ERROR(gss_test_oid_set_member(&min_status,
78124208Sdes		    &supported_mechs[i]->oid, supported, &present)))
79124208Sdes			present = 0;
80124208Sdes		if (present)
81124208Sdes			gss_add_oid_set_member(&min_status,
82124208Sdes			    &supported_mechs[i]->oid, oidset);
83124208Sdes		i++;
84124208Sdes	}
85124208Sdes}
86124208Sdes
87124208Sdes
88124208Sdes/* Wrapper around accept_sec_context
89124208Sdes * Requires that the context contains:
90124208Sdes *    oid
91124208Sdes *    credentials	(from ssh_gssapi_acquire_cred)
92124208Sdes */
93124208Sdes/* Priviledged */
94124208SdesOM_uint32
95124208Sdesssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *recv_tok,
96124208Sdes    gss_buffer_desc *send_tok, OM_uint32 *flags)
97124208Sdes{
98124208Sdes	OM_uint32 status;
99124208Sdes	gss_OID mech;
100124208Sdes
101124208Sdes	ctx->major = gss_accept_sec_context(&ctx->minor,
102124208Sdes	    &ctx->context, ctx->creds, recv_tok,
103124208Sdes	    GSS_C_NO_CHANNEL_BINDINGS, &ctx->client, &mech,
104124208Sdes	    send_tok, flags, NULL, &ctx->client_creds);
105124208Sdes
106124208Sdes	if (GSS_ERROR(ctx->major))
107124208Sdes		ssh_gssapi_error(ctx);
108124208Sdes
109124208Sdes	if (ctx->client_creds)
110124208Sdes		debug("Received some client credentials");
111124208Sdes	else
112124208Sdes		debug("Got no client credentials");
113124208Sdes
114124208Sdes	status = ctx->major;
115124208Sdes
116124208Sdes	/* Now, if we're complete and we have the right flags, then
117124208Sdes	 * we flag the user as also having been authenticated
118124208Sdes	 */
119124208Sdes
120124208Sdes	if (((flags == NULL) || ((*flags & GSS_C_MUTUAL_FLAG) &&
121124208Sdes	    (*flags & GSS_C_INTEG_FLAG))) && (ctx->major == GSS_S_COMPLETE)) {
122124208Sdes		if (ssh_gssapi_getclient(ctx, &gssapi_client))
123124208Sdes			fatal("Couldn't convert client name");
124124208Sdes	}
125124208Sdes
126124208Sdes	return (status);
127124208Sdes}
128124208Sdes
129124208Sdes/*
130124208Sdes * This parses an exported name, extracting the mechanism specific portion
131124208Sdes * to use for ACL checking. It verifies that the name belongs the mechanism
132124208Sdes * originally selected.
133124208Sdes */
134124208Sdesstatic OM_uint32
135124208Sdesssh_gssapi_parse_ename(Gssctxt *ctx, gss_buffer_t ename, gss_buffer_t name)
136124208Sdes{
137124208Sdes	char *tok;
138124208Sdes	OM_uint32 offset;
139124208Sdes	OM_uint32 oidl;
140124208Sdes
141124208Sdes	tok=ename->value;
142124208Sdes
143124208Sdes	/*
144124208Sdes	 * Check that ename is long enough for all of the fixed length
145124208Sdes	 * header, and that the initial ID bytes are correct
146124208Sdes	 */
147124208Sdes
148124208Sdes	if (ename->length<6 || memcmp(tok,"\x04\x01", 2)!=0)
149124208Sdes		return GSS_S_FAILURE;
150124208Sdes
151124208Sdes	/*
152124208Sdes	 * Extract the OID, and check it. Here GSSAPI breaks with tradition
153124208Sdes	 * and does use the OID type and length bytes. To confuse things
154124208Sdes	 * there are two lengths - the first including these, and the
155124208Sdes	 * second without.
156124208Sdes	 */
157124208Sdes
158124208Sdes	oidl = GET_16BIT(tok+2); /* length including next two bytes */
159124208Sdes	oidl = oidl-2; /* turn it into the _real_ length of the variable OID */
160124208Sdes
161124208Sdes	/*
162124208Sdes	 * Check the BER encoding for correct type and length, that the
163124208Sdes	 * string is long enough and that the OID matches that in our context
164124208Sdes	 */
165124208Sdes	if (tok[4] != 0x06 || tok[5] != oidl ||
166124208Sdes	    ename->length < oidl+6 ||
167124208Sdes	   !ssh_gssapi_check_oid(ctx,tok+6,oidl))
168124208Sdes		return GSS_S_FAILURE;
169124208Sdes
170124208Sdes	offset = oidl+6;
171124208Sdes
172124208Sdes	if (ename->length < offset+4)
173124208Sdes		return GSS_S_FAILURE;
174124208Sdes
175124208Sdes	name->length = GET_32BIT(tok+offset);
176124208Sdes	offset += 4;
177124208Sdes
178124208Sdes	if (ename->length < offset+name->length)
179124208Sdes		return GSS_S_FAILURE;
180124208Sdes
181124208Sdes	name->value = xmalloc(name->length+1);
182124208Sdes	memcpy(name->value,tok+offset,name->length);
183124208Sdes	((char *)name->value)[name->length] = 0;
184124208Sdes
185124208Sdes	return GSS_S_COMPLETE;
186124208Sdes}
187124208Sdes
188124208Sdes/* Extract the client details from a given context. This can only reliably
189124208Sdes * be called once for a context */
190124208Sdes
191124208Sdes/* Priviledged (called from accept_secure_ctx) */
192124208SdesOM_uint32
193124208Sdesssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
194124208Sdes{
195124208Sdes	int i = 0;
196124208Sdes
197124208Sdes	gss_buffer_desc ename;
198124208Sdes
199124208Sdes	client->mech = NULL;
200124208Sdes
201124208Sdes	while (supported_mechs[i]->name != NULL) {
202124208Sdes		if (supported_mechs[i]->oid.length == ctx->oid->length &&
203124208Sdes		    (memcmp(supported_mechs[i]->oid.elements,
204124208Sdes		    ctx->oid->elements, ctx->oid->length) == 0))
205124208Sdes			client->mech = supported_mechs[i];
206124208Sdes		i++;
207124208Sdes	}
208124208Sdes
209124208Sdes	if (client->mech == NULL)
210124208Sdes		return GSS_S_FAILURE;
211124208Sdes
212124208Sdes	if ((ctx->major = gss_display_name(&ctx->minor, ctx->client,
213124208Sdes	    &client->displayname, NULL))) {
214124208Sdes		ssh_gssapi_error(ctx);
215124208Sdes		return (ctx->major);
216124208Sdes	}
217124208Sdes
218124208Sdes	if ((ctx->major = gss_export_name(&ctx->minor, ctx->client,
219124208Sdes	    &ename))) {
220124208Sdes		ssh_gssapi_error(ctx);
221124208Sdes		return (ctx->major);
222124208Sdes	}
223124208Sdes
224124208Sdes	if ((ctx->major = ssh_gssapi_parse_ename(ctx,&ename,
225124208Sdes	    &client->exportedname))) {
226124208Sdes		return (ctx->major);
227124208Sdes	}
228124208Sdes
229124208Sdes	/* We can't copy this structure, so we just move the pointer to it */
230124208Sdes	client->creds = ctx->client_creds;
231124208Sdes	ctx->client_creds = GSS_C_NO_CREDENTIAL;
232124208Sdes	return (ctx->major);
233124208Sdes}
234124208Sdes
235126274Sdes/* As user - called on fatal/exit */
236124208Sdesvoid
237126274Sdesssh_gssapi_cleanup_creds(void)
238124208Sdes{
239124208Sdes	if (gssapi_client.store.filename != NULL) {
240124208Sdes		/* Unlink probably isn't sufficient */
241124208Sdes		debug("removing gssapi cred file\"%s\"", gssapi_client.store.filename);
242124208Sdes		unlink(gssapi_client.store.filename);
243124208Sdes	}
244124208Sdes}
245124208Sdes
246124208Sdes/* As user */
247124208Sdesvoid
248124208Sdesssh_gssapi_storecreds(void)
249124208Sdes{
250124208Sdes	if (gssapi_client.mech && gssapi_client.mech->storecreds) {
251124208Sdes		(*gssapi_client.mech->storecreds)(&gssapi_client);
252124208Sdes	} else
253124208Sdes		debug("ssh_gssapi_storecreds: Not a GSSAPI mechanism");
254124208Sdes}
255124208Sdes
256124208Sdes/* This allows GSSAPI methods to do things to the childs environment based
257124208Sdes * on the passed authentication process and credentials.
258124208Sdes */
259124208Sdes/* As user */
260124208Sdesvoid
261124208Sdesssh_gssapi_do_child(char ***envp, u_int *envsizep)
262124208Sdes{
263124208Sdes
264124208Sdes	if (gssapi_client.store.envvar != NULL &&
265124208Sdes	    gssapi_client.store.envval != NULL) {
266124208Sdes
267124208Sdes		debug("Setting %s to %s", gssapi_client.store.envvar,
268124208Sdes		gssapi_client.store.envval);
269124208Sdes		child_set_env(envp, envsizep, gssapi_client.store.envvar,
270124208Sdes		     gssapi_client.store.envval);
271124208Sdes	}
272124208Sdes}
273124208Sdes
274124208Sdes/* Priviledged */
275124208Sdesint
276124208Sdesssh_gssapi_userok(char *user)
277124208Sdes{
278124208Sdes	if (gssapi_client.exportedname.length == 0 ||
279124208Sdes	    gssapi_client.exportedname.value == NULL) {
280124208Sdes		debug("No suitable client data");
281124208Sdes		return 0;
282124208Sdes	}
283124208Sdes	if (gssapi_client.mech && gssapi_client.mech->userok)
284124208Sdes		return ((*gssapi_client.mech->userok)(&gssapi_client, user));
285124208Sdes	else
286124208Sdes		debug("ssh_gssapi_userok: Unknown GSSAPI mechanism");
287124208Sdes	return (0);
288124208Sdes}
289124208Sdes
290126274Sdes/* Priviledged */
291126274SdesOM_uint32
292126274Sdesssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
293126274Sdes{
294126274Sdes	ctx->major = gss_verify_mic(&ctx->minor, ctx->context,
295126274Sdes	    gssbuf, gssmic, NULL);
296126274Sdes
297126274Sdes	return (ctx->major);
298126274Sdes}
299126274Sdes
300124208Sdes#endif
301