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