]> andersk Git - gssapi-openssh.git/blame - openssh/gss-serv.c
Initial revision
[gssapi-openssh.git] / openssh / gss-serv.c
CommitLineData
2ce0bfe4 1/* $OpenBSD: gss-serv.c,v 1.8 2005/08/30 22:08:05 djm Exp $ */
7cac2b65 2
5598e598 3/*
23987cb8 4 * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
5598e598 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
29#ifdef GSSAPI
30
5598e598 31#include "buffer.h"
32#include "bufaux.h"
5598e598 33#include "compat.h"
34#include <openssl/evp.h>
5598e598 35#include "kex.h"
36#include "auth.h"
37#include "log.h"
e5affddc 38#include "channels.h"
5598e598 39#include "session.h"
5598e598 40#include "servconf.h"
b59afbfe 41#include "monitor_wrap.h"
7cac2b65 42#include "xmalloc.h"
43#include "getput.h"
5598e598 44
45#include "ssh-gss.h"
46
47extern ServerOptions options;
5598e598 48
23987cb8 49static ssh_gssapi_client gssapi_client =
7cac2b65 50 { GSS_C_EMPTY_BUFFER, GSS_C_EMPTY_BUFFER,
51 GSS_C_NO_CREDENTIAL, NULL, {NULL, NULL, NULL}};
5598e598 52
7cac2b65 53ssh_gssapi_mech gssapi_null_mech =
54 { NULL, NULL, {0, NULL}, NULL, NULL, NULL, NULL};
44a053a3 55
5598e598 56#ifdef KRB5
23987cb8 57extern ssh_gssapi_mech gssapi_kerberos_mech;
54425c7a 58#endif
5598e598 59#ifdef GSI
23987cb8 60extern ssh_gssapi_mech gssapi_gsi_mech;
b311f64e 61#endif
62
23987cb8 63ssh_gssapi_mech* supported_mechs[]= {
5598e598 64#ifdef KRB5
7cac2b65 65 &gssapi_kerberos_mech,
5598e598 66#endif
67#ifdef GSI
7cac2b65 68 &gssapi_gsi_mech,
7b5a625b 69#endif
7cac2b65 70 &gssapi_null_mech,
23987cb8 71};
5598e598 72
b4cfa386 73#ifdef GSS_C_GLOBUS_LIMITED_PROXY_FLAG
74static int limited = 0;
75#endif
76
fe4ad273 77/* Unpriviledged */
78char *
79ssh_gssapi_server_mechanisms() {
80 gss_OID_set supported;
81
82 ssh_gssapi_supported_oids(&supported);
83 return (ssh_gssapi_kex_mechs(supported, &ssh_gssapi_server_check_mech,
84 NULL));
85}
86
87/* Unpriviledged */
88int
89ssh_gssapi_server_check_mech(gss_OID oid, void *data) {
90 Gssctxt * ctx = NULL;
91 int res;
92
93 res = !GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctx, oid)));
94 ssh_gssapi_delete_ctx(&ctx);
95
96 return (res);
97}
98
7cac2b65 99/* Unpriviledged */
100void
101ssh_gssapi_supported_oids(gss_OID_set *oidset)
102{
103 int i = 0;
104 OM_uint32 min_status;
105 int present;
106 gss_OID_set supported;
107
108 gss_create_empty_oid_set(&min_status, oidset);
186e1568 109 /* Ask priviledged process what mechanisms it supports. */
110 PRIVSEP(gss_indicate_mechs(&min_status, &supported));
7cac2b65 111
112 while (supported_mechs[i]->name != NULL) {
113 if (GSS_ERROR(gss_test_oid_set_member(&min_status,
114 &supported_mechs[i]->oid, supported, &present)))
115 present = 0;
116 if (present)
117 gss_add_oid_set_member(&min_status,
118 &supported_mechs[i]->oid, oidset);
119 i++;
120 }
121}
122
123
124/* Wrapper around accept_sec_context
125 * Requires that the context contains:
126 * oid
127 * credentials (from ssh_gssapi_acquire_cred)
128 */
129/* Priviledged */
130OM_uint32
131ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *recv_tok,
132 gss_buffer_desc *send_tok, OM_uint32 *flags)
133{
134 OM_uint32 status;
135 gss_OID mech;
136
137 ctx->major = gss_accept_sec_context(&ctx->minor,
138 &ctx->context, ctx->creds, recv_tok,
139 GSS_C_NO_CHANNEL_BINDINGS, &ctx->client, &mech,
140 send_tok, flags, NULL, &ctx->client_creds);
141
142 if (GSS_ERROR(ctx->major))
143 ssh_gssapi_error(ctx);
144
145 if (ctx->client_creds)
146 debug("Received some client credentials");
147 else
148 debug("Got no client credentials");
149
150 status = ctx->major;
151
152 /* Now, if we're complete and we have the right flags, then
153 * we flag the user as also having been authenticated
154 */
155
156 if (((flags == NULL) || ((*flags & GSS_C_MUTUAL_FLAG) &&
157 (*flags & GSS_C_INTEG_FLAG))) && (ctx->major == GSS_S_COMPLETE)) {
158 if (ssh_gssapi_getclient(ctx, &gssapi_client))
159 fatal("Couldn't convert client name");
b4cfa386 160#ifdef GSS_C_GLOBUS_LIMITED_PROXY_FLAG
161 if (flags && (*flags & GSS_C_GLOBUS_LIMITED_PROXY_FLAG))
162 limited=1;
163#endif
7cac2b65 164 }
165
166 return (status);
167}
168
169/*
170 * This parses an exported name, extracting the mechanism specific portion
171 * to use for ACL checking. It verifies that the name belongs the mechanism
172 * originally selected.
173 */
174static OM_uint32
175ssh_gssapi_parse_ename(Gssctxt *ctx, gss_buffer_t ename, gss_buffer_t name)
176{
2ce0bfe4 177 u_char *tok;
7cac2b65 178 OM_uint32 offset;
179 OM_uint32 oidl;
180
181 tok=ename->value;
182
183#ifdef GSI /* GSI gss_export_name() is broken. */
184 if ((ctx->oid->length == gssapi_gsi_mech.oid.length) &&
185 (memcmp(ctx->oid->elements, gssapi_gsi_mech.oid.elements,
186 gssapi_gsi_mech.oid.length) == 0)) {
187 name->length = ename->length;
188 name->value = xmalloc(ename->length+1);
189 memcpy(name->value, ename->value, ename->length);
190 return GSS_S_COMPLETE;
191 }
192#endif
193
194 /*
195 * Check that ename is long enough for all of the fixed length
196 * header, and that the initial ID bytes are correct
197 */
198
199 if (ename->length<6 || memcmp(tok,"\x04\x01", 2)!=0)
200 return GSS_S_FAILURE;
201
202 /*
203 * Extract the OID, and check it. Here GSSAPI breaks with tradition
204 * and does use the OID type and length bytes. To confuse things
205 * there are two lengths - the first including these, and the
206 * second without.
207 */
208
209 oidl = GET_16BIT(tok+2); /* length including next two bytes */
210 oidl = oidl-2; /* turn it into the _real_ length of the variable OID */
211
212 /*
213 * Check the BER encoding for correct type and length, that the
214 * string is long enough and that the OID matches that in our context
215 */
216 if (tok[4] != 0x06 || tok[5] != oidl ||
217 ename->length < oidl+6 ||
2ce0bfe4 218 !ssh_gssapi_check_oid(ctx,tok+6,oidl))
7cac2b65 219 return GSS_S_FAILURE;
220
221 offset = oidl+6;
222
223 if (ename->length < offset+4)
224 return GSS_S_FAILURE;
225
226 name->length = GET_32BIT(tok+offset);
227 offset += 4;
228
229 if (ename->length < offset+name->length)
230 return GSS_S_FAILURE;
231
232 name->value = xmalloc(name->length+1);
233 memcpy(name->value,tok+offset,name->length);
234 ((char *)name->value)[name->length] = 0;
235
236 return GSS_S_COMPLETE;
237}
238
239/* Extract the client details from a given context. This can only reliably
240 * be called once for a context */
241
242/* Priviledged (called from accept_secure_ctx) */
243OM_uint32
244ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
245{
246 int i = 0;
247
248 gss_buffer_desc ename;
249
250 client->mech = NULL;
251
252 while (supported_mechs[i]->name != NULL) {
253 if (supported_mechs[i]->oid.length == ctx->oid->length &&
254 (memcmp(supported_mechs[i]->oid.elements,
255 ctx->oid->elements, ctx->oid->length) == 0))
256 client->mech = supported_mechs[i];
257 i++;
258 }
259
260 if (client->mech == NULL)
261 return GSS_S_FAILURE;
262
263 if ((ctx->major = gss_display_name(&ctx->minor, ctx->client,
264 &client->displayname, NULL))) {
265 ssh_gssapi_error(ctx);
266 return (ctx->major);
267 }
268
269 if ((ctx->major = gss_export_name(&ctx->minor, ctx->client,
270 &ename))) {
271 ssh_gssapi_error(ctx);
272 return (ctx->major);
273 }
274
275 if ((ctx->major = ssh_gssapi_parse_ename(ctx,&ename,
276 &client->exportedname))) {
277 return (ctx->major);
278 }
279
280 /* We can't copy this structure, so we just move the pointer to it */
281 client->creds = ctx->client_creds;
282 ctx->client_creds = GSS_C_NO_CREDENTIAL;
283 return (ctx->major);
284}
285
540d72c3 286/* As user - called on fatal/exit */
7cac2b65 287void
540d72c3 288ssh_gssapi_cleanup_creds(void)
7cac2b65 289{
290 if (gssapi_client.store.filename != NULL) {
291 /* Unlink probably isn't sufficient */
292 debug("removing gssapi cred file\"%s\"", gssapi_client.store.filename);
293 unlink(gssapi_client.store.filename);
294 }
295}
296
297/* As user */
298void
299ssh_gssapi_storecreds(void)
300{
301 if (gssapi_client.mech && gssapi_client.mech->storecreds) {
302 (*gssapi_client.mech->storecreds)(&gssapi_client);
7cac2b65 303 } else
304 debug("ssh_gssapi_storecreds: Not a GSSAPI mechanism");
305}
306
307/* This allows GSSAPI methods to do things to the childs environment based
308 * on the passed authentication process and credentials.
309 */
310/* As user */
311void
312ssh_gssapi_do_child(char ***envp, u_int *envsizep)
313{
314
315 if (gssapi_client.store.envvar != NULL &&
316 gssapi_client.store.envval != NULL) {
317
318 debug("Setting %s to %s", gssapi_client.store.envvar,
319 gssapi_client.store.envval);
320 child_set_env(envp, envsizep, gssapi_client.store.envvar,
2ce0bfe4 321 gssapi_client.store.envval);
7cac2b65 322 }
323}
324
325/* Priviledged */
326int
327ssh_gssapi_userok(char *user)
328{
2ce0bfe4 329 OM_uint32 lmin;
330
7cac2b65 331 if (gssapi_client.exportedname.length == 0 ||
332 gssapi_client.exportedname.value == NULL) {
333 debug("No suitable client data");
334 return 0;
335 }
b4cfa386 336#ifdef GSS_C_GLOBUS_LIMITED_PROXY_FLAG
337 if (limited) {
338 debug("limited proxy not acceptable for remote login");
339 return 0;
340 }
341#endif
7cac2b65 342 if (gssapi_client.mech && gssapi_client.mech->userok)
2ce0bfe4 343 if ((*gssapi_client.mech->userok)(&gssapi_client, user))
344 return 1;
345 else {
346 /* Destroy delegated credentials if userok fails */
347 gss_release_buffer(&lmin, &gssapi_client.displayname);
348 gss_release_buffer(&lmin, &gssapi_client.exportedname);
349 gss_release_cred(&lmin, &gssapi_client.creds);
350 memset(&gssapi_client, 0, sizeof(ssh_gssapi_client));
351 return 0;
352 }
7cac2b65 353 else
354 debug("ssh_gssapi_userok: Unknown GSSAPI mechanism");
355 return (0);
356}
357
e23e524c 358/* Priviledged */
23987cb8 359int
360ssh_gssapi_localname(char **user)
361{
362 *user = NULL;
7cac2b65 363 if (gssapi_client.displayname.length==0 ||
364 gssapi_client.displayname.value==NULL) {
23987cb8 365 debug("No suitable client data");
366 return(0);;
367 }
368 if (gssapi_client.mech && gssapi_client.mech->localname) {
369 return((*gssapi_client.mech->localname)(&gssapi_client,user));
370 } else {
371 debug("Unknown client authentication type");
372 }
373 return(0);
374}
540d72c3 375
23987cb8 376#endif
This page took 0.210633 seconds and 5 git commands to generate.