]> andersk Git - gssapi-openssh.git/blame - openssh/gss-serv.c
don't abort gsskex if client gives a limited proxy.
[gssapi-openssh.git] / openssh / gss-serv.c
CommitLineData
540d72c3 1/* $OpenBSD: gss-serv.c,v 1.5 2003/11/17 11:06:07 markus 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;
60extern ssh_gssapi_mech gssapi_kerberos_mech_old;
54425c7a 61#endif
5598e598 62#ifdef GSI
23987cb8 63extern ssh_gssapi_mech gssapi_gsi_mech;
64extern ssh_gssapi_mech gssapi_gsi_mech_old;
b311f64e 65#endif
66
23987cb8 67ssh_gssapi_mech* supported_mechs[]= {
5598e598 68#ifdef KRB5
7cac2b65 69 &gssapi_kerberos_mech,
70 &gssapi_kerberos_mech_old, /* Support for legacy clients */
5598e598 71#endif
72#ifdef GSI
7cac2b65 73 &gssapi_gsi_mech,
74 &gssapi_gsi_mech_old, /* Support for legacy clients */
7b5a625b 75#endif
7cac2b65 76 &gssapi_null_mech,
23987cb8 77};
5598e598 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");
140 }
141
142 return (status);
143}
144
145/*
146 * This parses an exported name, extracting the mechanism specific portion
147 * to use for ACL checking. It verifies that the name belongs the mechanism
148 * originally selected.
149 */
150static OM_uint32
151ssh_gssapi_parse_ename(Gssctxt *ctx, gss_buffer_t ename, gss_buffer_t name)
152{
153 char *tok;
154 OM_uint32 offset;
155 OM_uint32 oidl;
156
157 tok=ename->value;
158
159#ifdef GSI /* GSI gss_export_name() is broken. */
160 if ((ctx->oid->length == gssapi_gsi_mech.oid.length) &&
161 (memcmp(ctx->oid->elements, gssapi_gsi_mech.oid.elements,
162 gssapi_gsi_mech.oid.length) == 0)) {
163 name->length = ename->length;
164 name->value = xmalloc(ename->length+1);
165 memcpy(name->value, ename->value, ename->length);
166 return GSS_S_COMPLETE;
167 }
168#endif
169
170 /*
171 * Check that ename is long enough for all of the fixed length
172 * header, and that the initial ID bytes are correct
173 */
174
175 if (ename->length<6 || memcmp(tok,"\x04\x01", 2)!=0)
176 return GSS_S_FAILURE;
177
178 /*
179 * Extract the OID, and check it. Here GSSAPI breaks with tradition
180 * and does use the OID type and length bytes. To confuse things
181 * there are two lengths - the first including these, and the
182 * second without.
183 */
184
185 oidl = GET_16BIT(tok+2); /* length including next two bytes */
186 oidl = oidl-2; /* turn it into the _real_ length of the variable OID */
187
188 /*
189 * Check the BER encoding for correct type and length, that the
190 * string is long enough and that the OID matches that in our context
191 */
192 if (tok[4] != 0x06 || tok[5] != oidl ||
193 ename->length < oidl+6 ||
194 !ssh_gssapi_check_oid(ctx,tok+6,oidl))
195 return GSS_S_FAILURE;
196
197 offset = oidl+6;
198
199 if (ename->length < offset+4)
200 return GSS_S_FAILURE;
201
202 name->length = GET_32BIT(tok+offset);
203 offset += 4;
204
205 if (ename->length < offset+name->length)
206 return GSS_S_FAILURE;
207
208 name->value = xmalloc(name->length+1);
209 memcpy(name->value,tok+offset,name->length);
210 ((char *)name->value)[name->length] = 0;
211
212 return GSS_S_COMPLETE;
213}
214
215/* Extract the client details from a given context. This can only reliably
216 * be called once for a context */
217
218/* Priviledged (called from accept_secure_ctx) */
219OM_uint32
220ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
221{
222 int i = 0;
223
224 gss_buffer_desc ename;
225
226 client->mech = NULL;
227
228 while (supported_mechs[i]->name != NULL) {
229 if (supported_mechs[i]->oid.length == ctx->oid->length &&
230 (memcmp(supported_mechs[i]->oid.elements,
231 ctx->oid->elements, ctx->oid->length) == 0))
232 client->mech = supported_mechs[i];
233 i++;
234 }
235
236 if (client->mech == NULL)
237 return GSS_S_FAILURE;
238
239 if ((ctx->major = gss_display_name(&ctx->minor, ctx->client,
240 &client->displayname, NULL))) {
241 ssh_gssapi_error(ctx);
242 return (ctx->major);
243 }
244
245 if ((ctx->major = gss_export_name(&ctx->minor, ctx->client,
246 &ename))) {
247 ssh_gssapi_error(ctx);
248 return (ctx->major);
249 }
250
251 if ((ctx->major = ssh_gssapi_parse_ename(ctx,&ename,
252 &client->exportedname))) {
253 return (ctx->major);
254 }
255
256 /* We can't copy this structure, so we just move the pointer to it */
257 client->creds = ctx->client_creds;
258 ctx->client_creds = GSS_C_NO_CREDENTIAL;
259 return (ctx->major);
260}
261
540d72c3 262/* As user - called on fatal/exit */
7cac2b65 263void
540d72c3 264ssh_gssapi_cleanup_creds(void)
7cac2b65 265{
266 if (gssapi_client.store.filename != NULL) {
267 /* Unlink probably isn't sufficient */
268 debug("removing gssapi cred file\"%s\"", gssapi_client.store.filename);
269 unlink(gssapi_client.store.filename);
270 }
271}
272
273/* As user */
274void
275ssh_gssapi_storecreds(void)
276{
277 if (gssapi_client.mech && gssapi_client.mech->storecreds) {
278 (*gssapi_client.mech->storecreds)(&gssapi_client);
7cac2b65 279 } else
280 debug("ssh_gssapi_storecreds: Not a GSSAPI mechanism");
281}
282
283/* This allows GSSAPI methods to do things to the childs environment based
284 * on the passed authentication process and credentials.
285 */
286/* As user */
287void
288ssh_gssapi_do_child(char ***envp, u_int *envsizep)
289{
290
291 if (gssapi_client.store.envvar != NULL &&
292 gssapi_client.store.envval != NULL) {
293
294 debug("Setting %s to %s", gssapi_client.store.envvar,
295 gssapi_client.store.envval);
296 child_set_env(envp, envsizep, gssapi_client.store.envvar,
297 gssapi_client.store.envval);
298 }
299}
300
301/* Priviledged */
302int
303ssh_gssapi_userok(char *user)
304{
305 if (gssapi_client.exportedname.length == 0 ||
306 gssapi_client.exportedname.value == NULL) {
307 debug("No suitable client data");
308 return 0;
309 }
310 if (gssapi_client.mech && gssapi_client.mech->userok)
311 return ((*gssapi_client.mech->userok)(&gssapi_client, user));
312 else
313 debug("ssh_gssapi_userok: Unknown GSSAPI mechanism");
314 return (0);
315}
316
23987cb8 317/* Return a list of the gss-group1-sha1-x mechanisms supported by this
318 * program.
5598e598 319 *
23987cb8 320 * We only support the mechanisms that we've indicated in the list above,
321 * but we check that they're supported by the GSSAPI mechanism on the
322 * machine. We also check, before including them in the list, that
323 * we have the necesary information in order to carry out the key exchange
324 * (that is, that the user has credentials, the server's creds are accessible,
325 * etc)
326 *
327 * The way that this is done is fairly nasty, as we do a lot of work that
328 * is then thrown away. This should possibly be implemented with a cache
329 * that stores the results (in an expanded Gssctxt structure), which are
330 * then used by the first calls if that key exchange mechanism is chosen.
6a8bca29 331 */
e23e524c 332
333/* Unpriviledged */
f001f132 334char *
23987cb8 335ssh_gssapi_server_mechanisms() {
f001f132 336 gss_OID_set supported;
23987cb8 337 Gssctxt *ctx = NULL;
f001f132 338 OM_uint32 maj_status, min_status;
339 Buffer buf;
340 int i = 0;
23987cb8 341 int first = 0;
f001f132 342 int present;
f001f132 343 char * mechs;
f001f132 344
345 if (datafellows & SSH_OLD_GSSAPI) return NULL;
346
23987cb8 347 ssh_gssapi_supported_oids(&supported);
f001f132 348
23987cb8 349 buffer_init(&buf);
f001f132 350
23987cb8 351 while(supported_mechs[i]->name != NULL) {
f001f132 352 if ((maj_status=gss_test_oid_set_member(&min_status,
23987cb8 353 &supported_mechs[i]->oid,
f001f132 354 supported,
355 &present))) {
356 present=0;
357 }
23987cb8 358
f001f132 359 if (present) {
23987cb8 360 if (!GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctx,
361 &supported_mechs[i]->oid)))) {
362 /* Append gss_group1_sha1_x to our list */
363 if (first++!=0)
364 buffer_put_char(&buf,',');
365 buffer_append(&buf, KEX_GSS_SHA1,
366 sizeof(KEX_GSS_SHA1)-1);
367 buffer_append(&buf,
368 supported_mechs[i]->enc_name,
369 strlen(supported_mechs[i]->enc_name));
370 debug("GSSAPI mechanism %s (%s%s) supported",
371 supported_mechs[i]->name, KEX_GSS_SHA1,
372 supported_mechs[i]->enc_name);
373 } else {
374 debug("no credentials for GSSAPI mechanism %s",
375 supported_mechs[i]->name);
376 }
377 } else {
f001f132 378 debug("GSSAPI mechanism %s not supported",
23987cb8 379 supported_mechs[i]->name);
f001f132 380 }
23987cb8 381 ssh_gssapi_delete_ctx(&ctx);
382 i++;
383 }
f001f132 384
385 buffer_put_char(&buf,'\0');
386
387 mechs=xmalloc(buffer_len(&buf));
388 buffer_get(&buf,mechs,buffer_len(&buf));
389 buffer_free(&buf);
390 if (strlen(mechs)==0)
391 return(NULL);
392 else
393 return(mechs);
394}
395
e23e524c 396/* Return the OID that corresponds to the given context name */
23987cb8 397
e23e524c 398/* Unpriviledged */
23987cb8 399gss_OID
e23e524c 400ssh_gssapi_server_id_kex(char *name) {
23987cb8 401 int i=0;
402
403 if (strncmp(name, KEX_GSS_SHA1, sizeof(KEX_GSS_SHA1)-1) !=0) {
404 return(NULL);
405 }
406
407 name+=sizeof(KEX_GSS_SHA1)-1; /* Move to the start of the MIME string */
408
409 while (supported_mechs[i]->name!=NULL &&
e23e524c 410 strcmp(name,supported_mechs[i]->enc_name)!=0) {
23987cb8 411 i++;
412 }
413
414 if (supported_mechs[i]->name==NULL)
415 return (NULL);
416
23987cb8 417 debug("using GSSAPI mechanism %s (%s%s)", supported_mechs[i]->name,
418 KEX_GSS_SHA1, supported_mechs[i]->enc_name);
419
420 return &supported_mechs[i]->oid;
421}
422
e23e524c 423/* Priviledged */
23987cb8 424int
425ssh_gssapi_localname(char **user)
426{
427 *user = NULL;
7cac2b65 428 if (gssapi_client.displayname.length==0 ||
429 gssapi_client.displayname.value==NULL) {
23987cb8 430 debug("No suitable client data");
431 return(0);;
432 }
433 if (gssapi_client.mech && gssapi_client.mech->localname) {
434 return((*gssapi_client.mech->localname)(&gssapi_client,user));
435 } else {
436 debug("Unknown client authentication type");
437 }
438 return(0);
439}
540d72c3 440
441/* Priviledged */
442OM_uint32
443ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
444{
445 ctx->major = gss_verify_mic(&ctx->minor, ctx->context,
446 gssbuf, gssmic, NULL);
447
448 return (ctx->major);
449}
450
23987cb8 451#endif
This page took 0.140119 seconds and 5 git commands to generate.