]> andersk Git - gssapi-openssh.git/blame - openssh/gss-serv.c
merged OpenSSH 4.2p1 to trunk
[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;
48extern u_char *session_id2;
49extern int session_id2_len;
50
23987cb8 51static ssh_gssapi_client gssapi_client =
7cac2b65 52 { GSS_C_EMPTY_BUFFER, GSS_C_EMPTY_BUFFER,
53 GSS_C_NO_CREDENTIAL, NULL, {NULL, NULL, NULL}};
5598e598 54
7cac2b65 55ssh_gssapi_mech gssapi_null_mech =
56 { NULL, NULL, {0, NULL}, NULL, NULL, NULL, NULL};
44a053a3 57
5598e598 58#ifdef KRB5
23987cb8 59extern ssh_gssapi_mech gssapi_kerberos_mech;
54425c7a 60#endif
5598e598 61#ifdef GSI
23987cb8 62extern ssh_gssapi_mech gssapi_gsi_mech;
b311f64e 63#endif
64
23987cb8 65ssh_gssapi_mech* supported_mechs[]= {
5598e598 66#ifdef KRB5
7cac2b65 67 &gssapi_kerberos_mech,
5598e598 68#endif
69#ifdef GSI
7cac2b65 70 &gssapi_gsi_mech,
7b5a625b 71#endif
7cac2b65 72 &gssapi_null_mech,
23987cb8 73};
5598e598 74
b4cfa386 75#ifdef GSS_C_GLOBUS_LIMITED_PROXY_FLAG
76static int limited = 0;
77#endif
78
7cac2b65 79/* Unpriviledged */
80void
81ssh_gssapi_supported_oids(gss_OID_set *oidset)
82{
83 int i = 0;
84 OM_uint32 min_status;
85 int present;
86 gss_OID_set supported;
87
88 gss_create_empty_oid_set(&min_status, oidset);
186e1568 89 /* Ask priviledged process what mechanisms it supports. */
90 PRIVSEP(gss_indicate_mechs(&min_status, &supported));
7cac2b65 91
92 while (supported_mechs[i]->name != NULL) {
93 if (GSS_ERROR(gss_test_oid_set_member(&min_status,
94 &supported_mechs[i]->oid, supported, &present)))
95 present = 0;
96 if (present)
97 gss_add_oid_set_member(&min_status,
98 &supported_mechs[i]->oid, oidset);
99 i++;
100 }
101}
102
103
104/* Wrapper around accept_sec_context
105 * Requires that the context contains:
106 * oid
107 * credentials (from ssh_gssapi_acquire_cred)
108 */
109/* Priviledged */
110OM_uint32
111ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *recv_tok,
112 gss_buffer_desc *send_tok, OM_uint32 *flags)
113{
114 OM_uint32 status;
115 gss_OID mech;
116
117 ctx->major = gss_accept_sec_context(&ctx->minor,
118 &ctx->context, ctx->creds, recv_tok,
119 GSS_C_NO_CHANNEL_BINDINGS, &ctx->client, &mech,
120 send_tok, flags, NULL, &ctx->client_creds);
121
122 if (GSS_ERROR(ctx->major))
123 ssh_gssapi_error(ctx);
124
125 if (ctx->client_creds)
126 debug("Received some client credentials");
127 else
128 debug("Got no client credentials");
129
130 status = ctx->major;
131
132 /* Now, if we're complete and we have the right flags, then
133 * we flag the user as also having been authenticated
134 */
135
136 if (((flags == NULL) || ((*flags & GSS_C_MUTUAL_FLAG) &&
137 (*flags & GSS_C_INTEG_FLAG))) && (ctx->major == GSS_S_COMPLETE)) {
138 if (ssh_gssapi_getclient(ctx, &gssapi_client))
139 fatal("Couldn't convert client name");
b4cfa386 140#ifdef GSS_C_GLOBUS_LIMITED_PROXY_FLAG
141 if (flags && (*flags & GSS_C_GLOBUS_LIMITED_PROXY_FLAG))
142 limited=1;
143#endif
7cac2b65 144 }
145
146 return (status);
147}
148
149/*
150 * This parses an exported name, extracting the mechanism specific portion
151 * to use for ACL checking. It verifies that the name belongs the mechanism
152 * originally selected.
153 */
154static OM_uint32
155ssh_gssapi_parse_ename(Gssctxt *ctx, gss_buffer_t ename, gss_buffer_t name)
156{
2ce0bfe4 157 u_char *tok;
7cac2b65 158 OM_uint32 offset;
159 OM_uint32 oidl;
160
161 tok=ename->value;
162
163#ifdef GSI /* GSI gss_export_name() is broken. */
164 if ((ctx->oid->length == gssapi_gsi_mech.oid.length) &&
165 (memcmp(ctx->oid->elements, gssapi_gsi_mech.oid.elements,
166 gssapi_gsi_mech.oid.length) == 0)) {
167 name->length = ename->length;
168 name->value = xmalloc(ename->length+1);
169 memcpy(name->value, ename->value, ename->length);
170 return GSS_S_COMPLETE;
171 }
172#endif
173
174 /*
175 * Check that ename is long enough for all of the fixed length
176 * header, and that the initial ID bytes are correct
177 */
178
179 if (ename->length<6 || memcmp(tok,"\x04\x01", 2)!=0)
180 return GSS_S_FAILURE;
181
182 /*
183 * Extract the OID, and check it. Here GSSAPI breaks with tradition
184 * and does use the OID type and length bytes. To confuse things
185 * there are two lengths - the first including these, and the
186 * second without.
187 */
188
189 oidl = GET_16BIT(tok+2); /* length including next two bytes */
190 oidl = oidl-2; /* turn it into the _real_ length of the variable OID */
191
192 /*
193 * Check the BER encoding for correct type and length, that the
194 * string is long enough and that the OID matches that in our context
195 */
196 if (tok[4] != 0x06 || tok[5] != oidl ||
197 ename->length < oidl+6 ||
2ce0bfe4 198 !ssh_gssapi_check_oid(ctx,tok+6,oidl))
7cac2b65 199 return GSS_S_FAILURE;
200
201 offset = oidl+6;
202
203 if (ename->length < offset+4)
204 return GSS_S_FAILURE;
205
206 name->length = GET_32BIT(tok+offset);
207 offset += 4;
208
209 if (ename->length < offset+name->length)
210 return GSS_S_FAILURE;
211
212 name->value = xmalloc(name->length+1);
213 memcpy(name->value,tok+offset,name->length);
214 ((char *)name->value)[name->length] = 0;
215
216 return GSS_S_COMPLETE;
217}
218
219/* Extract the client details from a given context. This can only reliably
220 * be called once for a context */
221
222/* Priviledged (called from accept_secure_ctx) */
223OM_uint32
224ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
225{
226 int i = 0;
227
228 gss_buffer_desc ename;
229
230 client->mech = NULL;
231
232 while (supported_mechs[i]->name != NULL) {
233 if (supported_mechs[i]->oid.length == ctx->oid->length &&
234 (memcmp(supported_mechs[i]->oid.elements,
235 ctx->oid->elements, ctx->oid->length) == 0))
236 client->mech = supported_mechs[i];
237 i++;
238 }
239
240 if (client->mech == NULL)
241 return GSS_S_FAILURE;
242
243 if ((ctx->major = gss_display_name(&ctx->minor, ctx->client,
244 &client->displayname, NULL))) {
245 ssh_gssapi_error(ctx);
246 return (ctx->major);
247 }
248
249 if ((ctx->major = gss_export_name(&ctx->minor, ctx->client,
250 &ename))) {
251 ssh_gssapi_error(ctx);
252 return (ctx->major);
253 }
254
255 if ((ctx->major = ssh_gssapi_parse_ename(ctx,&ename,
256 &client->exportedname))) {
257 return (ctx->major);
258 }
259
260 /* We can't copy this structure, so we just move the pointer to it */
261 client->creds = ctx->client_creds;
262 ctx->client_creds = GSS_C_NO_CREDENTIAL;
263 return (ctx->major);
264}
265
540d72c3 266/* As user - called on fatal/exit */
7cac2b65 267void
540d72c3 268ssh_gssapi_cleanup_creds(void)
7cac2b65 269{
270 if (gssapi_client.store.filename != NULL) {
271 /* Unlink probably isn't sufficient */
272 debug("removing gssapi cred file\"%s\"", gssapi_client.store.filename);
273 unlink(gssapi_client.store.filename);
274 }
275}
276
277/* As user */
278void
279ssh_gssapi_storecreds(void)
280{
281 if (gssapi_client.mech && gssapi_client.mech->storecreds) {
282 (*gssapi_client.mech->storecreds)(&gssapi_client);
7cac2b65 283 } else
284 debug("ssh_gssapi_storecreds: Not a GSSAPI mechanism");
285}
286
287/* This allows GSSAPI methods to do things to the childs environment based
288 * on the passed authentication process and credentials.
289 */
290/* As user */
291void
292ssh_gssapi_do_child(char ***envp, u_int *envsizep)
293{
294
295 if (gssapi_client.store.envvar != NULL &&
296 gssapi_client.store.envval != NULL) {
297
298 debug("Setting %s to %s", gssapi_client.store.envvar,
299 gssapi_client.store.envval);
300 child_set_env(envp, envsizep, gssapi_client.store.envvar,
2ce0bfe4 301 gssapi_client.store.envval);
7cac2b65 302 }
303}
304
305/* Priviledged */
306int
307ssh_gssapi_userok(char *user)
308{
2ce0bfe4 309 OM_uint32 lmin;
310
7cac2b65 311 if (gssapi_client.exportedname.length == 0 ||
312 gssapi_client.exportedname.value == NULL) {
313 debug("No suitable client data");
314 return 0;
315 }
b4cfa386 316#ifdef GSS_C_GLOBUS_LIMITED_PROXY_FLAG
317 if (limited) {
318 debug("limited proxy not acceptable for remote login");
319 return 0;
320 }
321#endif
7cac2b65 322 if (gssapi_client.mech && gssapi_client.mech->userok)
2ce0bfe4 323 if ((*gssapi_client.mech->userok)(&gssapi_client, user))
324 return 1;
325 else {
326 /* Destroy delegated credentials if userok fails */
327 gss_release_buffer(&lmin, &gssapi_client.displayname);
328 gss_release_buffer(&lmin, &gssapi_client.exportedname);
329 gss_release_cred(&lmin, &gssapi_client.creds);
330 memset(&gssapi_client, 0, sizeof(ssh_gssapi_client));
331 return 0;
332 }
7cac2b65 333 else
334 debug("ssh_gssapi_userok: Unknown GSSAPI mechanism");
335 return (0);
336}
337
23987cb8 338/* Return a list of the gss-group1-sha1-x mechanisms supported by this
339 * program.
5598e598 340 *
23987cb8 341 * We only support the mechanisms that we've indicated in the list above,
342 * but we check that they're supported by the GSSAPI mechanism on the
343 * machine. We also check, before including them in the list, that
344 * we have the necesary information in order to carry out the key exchange
345 * (that is, that the user has credentials, the server's creds are accessible,
346 * etc)
347 *
348 * The way that this is done is fairly nasty, as we do a lot of work that
349 * is then thrown away. This should possibly be implemented with a cache
350 * that stores the results (in an expanded Gssctxt structure), which are
351 * then used by the first calls if that key exchange mechanism is chosen.
6a8bca29 352 */
e23e524c 353
354/* Unpriviledged */
f001f132 355char *
23987cb8 356ssh_gssapi_server_mechanisms() {
f001f132 357 gss_OID_set supported;
23987cb8 358 Gssctxt *ctx = NULL;
f001f132 359 OM_uint32 maj_status, min_status;
360 Buffer buf;
361 int i = 0;
23987cb8 362 int first = 0;
f001f132 363 int present;
f001f132 364 char * mechs;
f001f132 365
23987cb8 366 ssh_gssapi_supported_oids(&supported);
f001f132 367
23987cb8 368 buffer_init(&buf);
f001f132 369
23987cb8 370 while(supported_mechs[i]->name != NULL) {
f001f132 371 if ((maj_status=gss_test_oid_set_member(&min_status,
23987cb8 372 &supported_mechs[i]->oid,
f001f132 373 supported,
374 &present))) {
375 present=0;
376 }
23987cb8 377
f001f132 378 if (present) {
23987cb8 379 if (!GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctx,
380 &supported_mechs[i]->oid)))) {
381 /* Append gss_group1_sha1_x to our list */
382 if (first++!=0)
383 buffer_put_char(&buf,',');
384 buffer_append(&buf, KEX_GSS_SHA1,
385 sizeof(KEX_GSS_SHA1)-1);
386 buffer_append(&buf,
387 supported_mechs[i]->enc_name,
388 strlen(supported_mechs[i]->enc_name));
389 debug("GSSAPI mechanism %s (%s%s) supported",
390 supported_mechs[i]->name, KEX_GSS_SHA1,
391 supported_mechs[i]->enc_name);
392 } else {
393 debug("no credentials for GSSAPI mechanism %s",
394 supported_mechs[i]->name);
395 }
396 } else {
f001f132 397 debug("GSSAPI mechanism %s not supported",
23987cb8 398 supported_mechs[i]->name);
f001f132 399 }
23987cb8 400 ssh_gssapi_delete_ctx(&ctx);
401 i++;
402 }
f001f132 403
404 buffer_put_char(&buf,'\0');
405
406 mechs=xmalloc(buffer_len(&buf));
407 buffer_get(&buf,mechs,buffer_len(&buf));
408 buffer_free(&buf);
f6c8c2f9 409 if (strlen(mechs)==0) {
410 options.gss_authentication = 0; /* no mechs. skip gss auth. */
411 return(NULL);
412 } else {
413 return(mechs);
414 }
f001f132 415}
416
e23e524c 417/* Return the OID that corresponds to the given context name */
23987cb8 418
e23e524c 419/* Unpriviledged */
23987cb8 420gss_OID
e23e524c 421ssh_gssapi_server_id_kex(char *name) {
23987cb8 422 int i=0;
423
424 if (strncmp(name, KEX_GSS_SHA1, sizeof(KEX_GSS_SHA1)-1) !=0) {
425 return(NULL);
426 }
427
428 name+=sizeof(KEX_GSS_SHA1)-1; /* Move to the start of the MIME string */
429
430 while (supported_mechs[i]->name!=NULL &&
e23e524c 431 strcmp(name,supported_mechs[i]->enc_name)!=0) {
23987cb8 432 i++;
433 }
434
435 if (supported_mechs[i]->name==NULL)
436 return (NULL);
437
23987cb8 438 debug("using GSSAPI mechanism %s (%s%s)", supported_mechs[i]->name,
439 KEX_GSS_SHA1, supported_mechs[i]->enc_name);
440
441 return &supported_mechs[i]->oid;
442}
443
e23e524c 444/* Priviledged */
23987cb8 445int
446ssh_gssapi_localname(char **user)
447{
448 *user = NULL;
7cac2b65 449 if (gssapi_client.displayname.length==0 ||
450 gssapi_client.displayname.value==NULL) {
23987cb8 451 debug("No suitable client data");
452 return(0);;
453 }
454 if (gssapi_client.mech && gssapi_client.mech->localname) {
455 return((*gssapi_client.mech->localname)(&gssapi_client,user));
456 } else {
457 debug("Unknown client authentication type");
458 }
459 return(0);
460}
540d72c3 461
462/* Priviledged */
463OM_uint32
464ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
465{
466 ctx->major = gss_verify_mic(&ctx->minor, ctx->context,
467 gssbuf, gssmic, NULL);
468
469 return (ctx->major);
470}
471
23987cb8 472#endif
This page took 0.141357 seconds and 5 git commands to generate.