]> andersk Git - gssapi-openssh.git/blame - openssh/gss-serv.c
merged OpenSSH 5.1p1 to trunk
[gssapi-openssh.git] / openssh / gss-serv.c
CommitLineData
5156b1a1 1/* $OpenBSD: gss-serv.c,v 1.22 2008/05/08 12:02:23 djm Exp $ */
7cac2b65 2
5598e598 3/*
f713db99 4 * Copyright (c) 2001-2006 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
30460aeb 31#include <sys/types.h>
fa0f0f45 32#include <sys/param.h>
30460aeb 33
34#include <stdarg.h>
35#include <string.h>
36#include <unistd.h>
37
5156b1a1 38#include "openbsd-compat/sys-queue.h"
30460aeb 39#include "xmalloc.h"
40#include "buffer.h"
41#include "key.h"
42#include "hostfile.h"
5598e598 43#include "auth.h"
44#include "log.h"
e5affddc 45#include "channels.h"
5598e598 46#include "session.h"
f713db99 47#include "misc.h"
5598e598 48#include "servconf.h"
f713db99 49
7cac2b65 50#include "xmalloc.h"
5598e598 51#include "ssh-gss.h"
30460aeb 52#include "monitor_wrap.h"
5598e598 53
bd3336ab 54extern ServerOptions options;
ae82558b 55extern Authctxt *the_authctxt;
bd3336ab 56
23987cb8 57static ssh_gssapi_client gssapi_client =
7cac2b65 58 { GSS_C_EMPTY_BUFFER, GSS_C_EMPTY_BUFFER,
59 GSS_C_NO_CREDENTIAL, NULL, {NULL, NULL, NULL}};
5598e598 60
7cac2b65 61ssh_gssapi_mech gssapi_null_mech =
62 { NULL, NULL, {0, NULL}, NULL, NULL, NULL, NULL};
44a053a3 63
5598e598 64#ifdef KRB5
23987cb8 65extern ssh_gssapi_mech gssapi_kerberos_mech;
54425c7a 66#endif
5598e598 67#ifdef GSI
23987cb8 68extern ssh_gssapi_mech gssapi_gsi_mech;
b311f64e 69#endif
70
23987cb8 71ssh_gssapi_mech* supported_mechs[]= {
5598e598 72#ifdef KRB5
7cac2b65 73 &gssapi_kerberos_mech,
5598e598 74#endif
75#ifdef GSI
7cac2b65 76 &gssapi_gsi_mech,
7b5a625b 77#endif
7cac2b65 78 &gssapi_null_mech,
23987cb8 79};
5598e598 80
b4cfa386 81#ifdef GSS_C_GLOBUS_LIMITED_PROXY_FLAG
82static int limited = 0;
83#endif
84
c06c9ae8 85/* Unprivileged */
fe4ad273 86char *
87ssh_gssapi_server_mechanisms() {
88 gss_OID_set supported;
89
90 ssh_gssapi_supported_oids(&supported);
91 return (ssh_gssapi_kex_mechs(supported, &ssh_gssapi_server_check_mech,
92 NULL));
93}
94
c06c9ae8 95/* Unprivileged */
fe4ad273 96int
f713db99 97ssh_gssapi_server_check_mech(Gssctxt **dum, gss_OID oid, const char *data) {
98 Gssctxt *ctx = NULL;
fe4ad273 99 int res;
c06c9ae8 100
f713db99 101 res = !GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctx, oid)));
102 ssh_gssapi_delete_ctx(&ctx);
fe4ad273 103
104 return (res);
105}
106
fa0f0f45 107/*
108 * Acquire credentials for a server running on the current host.
f713db99 109 * Requires that the context structure contains a valid OID
110 */
111
112/* Returns a GSSAPI error code */
fa0f0f45 113/* Privileged (called from ssh_gssapi_server_ctx) */
114static OM_uint32
f713db99 115ssh_gssapi_acquire_cred(Gssctxt *ctx)
116{
117 OM_uint32 status;
118 char lname[MAXHOSTNAMELEN];
119 gss_OID_set oidset;
120
121 if (options.gss_strict_acceptor) {
122 gss_create_empty_oid_set(&status, &oidset);
123 gss_add_oid_set_member(&status, ctx->oid, &oidset);
124
125 if (gethostname(lname, MAXHOSTNAMELEN)) {
126 gss_release_oid_set(&status, &oidset);
127 return (-1);
128 }
129
130 if (GSS_ERROR(ssh_gssapi_import_name(ctx, lname))) {
131 gss_release_oid_set(&status, &oidset);
132 return (ctx->major);
133 }
134
135 if ((ctx->major = gss_acquire_cred(&ctx->minor,
136 ctx->name, 0, oidset, GSS_C_ACCEPT, &ctx->creds,
137 NULL, NULL)))
138 ssh_gssapi_error(ctx);
139
140 gss_release_oid_set(&status, &oidset);
141 return (ctx->major);
142 } else {
143 ctx->name = GSS_C_NO_NAME;
144 ctx->creds = GSS_C_NO_CREDENTIAL;
145 }
146 return GSS_S_COMPLETE;
147}
148
7cac2b65 149
fa0f0f45 150/* Privileged */
151OM_uint32
152ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID oid)
153{
154 if (*ctx)
155 ssh_gssapi_delete_ctx(ctx);
156 ssh_gssapi_build_ctx(ctx);
157 ssh_gssapi_set_oid(*ctx, oid);
158 return (ssh_gssapi_acquire_cred(*ctx));
159}
160
c5b7701b 161/* Unprivileged */
162void
163ssh_gssapi_supported_oids(gss_OID_set *oidset)
164{
165 int i = 0;
166 OM_uint32 min_status;
167 int present;
168 gss_OID_set supported;
169
170 gss_create_empty_oid_set(&min_status, oidset);
171 /* Ask priviledged process what mechanisms it supports. */
172 PRIVSEP(gss_indicate_mechs(&min_status, &supported));
173
174 while (supported_mechs[i]->name != NULL) {
175 if (GSS_ERROR(gss_test_oid_set_member(&min_status,
176 &supported_mechs[i]->oid, supported, &present)))
177 present = 0;
178 if (present)
179 gss_add_oid_set_member(&min_status,
180 &supported_mechs[i]->oid, oidset);
181 i++;
182 }
183
184 gss_release_oid_set(&min_status, &supported);
185}
186
187
7cac2b65 188/* Wrapper around accept_sec_context
189 * Requires that the context contains:
190 * oid
191 * credentials (from ssh_gssapi_acquire_cred)
192 */
08822d99 193/* Privileged */
7cac2b65 194OM_uint32
195ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *recv_tok,
196 gss_buffer_desc *send_tok, OM_uint32 *flags)
197{
198 OM_uint32 status;
199 gss_OID mech;
200
201 ctx->major = gss_accept_sec_context(&ctx->minor,
202 &ctx->context, ctx->creds, recv_tok,
203 GSS_C_NO_CHANNEL_BINDINGS, &ctx->client, &mech,
204 send_tok, flags, NULL, &ctx->client_creds);
205
206 if (GSS_ERROR(ctx->major))
207 ssh_gssapi_error(ctx);
208
209 if (ctx->client_creds)
210 debug("Received some client credentials");
211 else
212 debug("Got no client credentials");
213
214 status = ctx->major;
215
216 /* Now, if we're complete and we have the right flags, then
217 * we flag the user as also having been authenticated
218 */
219
220 if (((flags == NULL) || ((*flags & GSS_C_MUTUAL_FLAG) &&
221 (*flags & GSS_C_INTEG_FLAG))) && (ctx->major == GSS_S_COMPLETE)) {
222 if (ssh_gssapi_getclient(ctx, &gssapi_client))
223 fatal("Couldn't convert client name");
b4cfa386 224#ifdef GSS_C_GLOBUS_LIMITED_PROXY_FLAG
225 if (flags && (*flags & GSS_C_GLOBUS_LIMITED_PROXY_FLAG))
226 limited=1;
227#endif
7cac2b65 228 }
229
230 return (status);
231}
232
233/*
234 * This parses an exported name, extracting the mechanism specific portion
235 * to use for ACL checking. It verifies that the name belongs the mechanism
236 * originally selected.
237 */
238static OM_uint32
239ssh_gssapi_parse_ename(Gssctxt *ctx, gss_buffer_t ename, gss_buffer_t name)
240{
2ce0bfe4 241 u_char *tok;
7cac2b65 242 OM_uint32 offset;
243 OM_uint32 oidl;
244
08822d99 245 tok = ename->value;
7cac2b65 246
247#ifdef GSI /* GSI gss_export_name() is broken. */
248 if ((ctx->oid->length == gssapi_gsi_mech.oid.length) &&
249 (memcmp(ctx->oid->elements, gssapi_gsi_mech.oid.elements,
250 gssapi_gsi_mech.oid.length) == 0)) {
251 name->length = ename->length;
252 name->value = xmalloc(ename->length+1);
253 memcpy(name->value, ename->value, ename->length);
254 return GSS_S_COMPLETE;
255 }
256#endif
257
258 /*
259 * Check that ename is long enough for all of the fixed length
260 * header, and that the initial ID bytes are correct
261 */
262
08822d99 263 if (ename->length < 6 || memcmp(tok, "\x04\x01", 2) != 0)
7cac2b65 264 return GSS_S_FAILURE;
265
266 /*
267 * Extract the OID, and check it. Here GSSAPI breaks with tradition
268 * and does use the OID type and length bytes. To confuse things
269 * there are two lengths - the first including these, and the
270 * second without.
271 */
272
30460aeb 273 oidl = get_u16(tok+2); /* length including next two bytes */
7cac2b65 274 oidl = oidl-2; /* turn it into the _real_ length of the variable OID */
275
276 /*
277 * Check the BER encoding for correct type and length, that the
278 * string is long enough and that the OID matches that in our context
279 */
280 if (tok[4] != 0x06 || tok[5] != oidl ||
281 ename->length < oidl+6 ||
08822d99 282 !ssh_gssapi_check_oid(ctx, tok+6, oidl))
7cac2b65 283 return GSS_S_FAILURE;
284
285 offset = oidl+6;
286
287 if (ename->length < offset+4)
288 return GSS_S_FAILURE;
289
30460aeb 290 name->length = get_u32(tok+offset);
7cac2b65 291 offset += 4;
292
293 if (ename->length < offset+name->length)
294 return GSS_S_FAILURE;
295
296 name->value = xmalloc(name->length+1);
30460aeb 297 memcpy(name->value, tok+offset, name->length);
7cac2b65 298 ((char *)name->value)[name->length] = 0;
299
300 return GSS_S_COMPLETE;
301}
302
303/* Extract the client details from a given context. This can only reliably
304 * be called once for a context */
305
08822d99 306/* Privileged (called from accept_secure_ctx) */
7cac2b65 307OM_uint32
308ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
309{
310 int i = 0;
311
312 gss_buffer_desc ename;
313
314 client->mech = NULL;
315
316 while (supported_mechs[i]->name != NULL) {
317 if (supported_mechs[i]->oid.length == ctx->oid->length &&
318 (memcmp(supported_mechs[i]->oid.elements,
319 ctx->oid->elements, ctx->oid->length) == 0))
320 client->mech = supported_mechs[i];
321 i++;
322 }
323
324 if (client->mech == NULL)
325 return GSS_S_FAILURE;
326
327 if ((ctx->major = gss_display_name(&ctx->minor, ctx->client,
328 &client->displayname, NULL))) {
329 ssh_gssapi_error(ctx);
330 return (ctx->major);
331 }
332
333 if ((ctx->major = gss_export_name(&ctx->minor, ctx->client,
334 &ename))) {
335 ssh_gssapi_error(ctx);
336 return (ctx->major);
337 }
338
339 if ((ctx->major = ssh_gssapi_parse_ename(ctx,&ename,
340 &client->exportedname))) {
341 return (ctx->major);
342 }
343
344 /* We can't copy this structure, so we just move the pointer to it */
345 client->creds = ctx->client_creds;
346 ctx->client_creds = GSS_C_NO_CREDENTIAL;
682b256d 347
348 /* needed for globus_gss_assist_map_and_authorize() */
349 client->context = ctx->context;
350
7cac2b65 351 return (ctx->major);
352}
353
540d72c3 354/* As user - called on fatal/exit */
7cac2b65 355void
540d72c3 356ssh_gssapi_cleanup_creds(void)
7cac2b65 357{
358 if (gssapi_client.store.filename != NULL) {
359 /* Unlink probably isn't sufficient */
30460aeb 360 debug("removing gssapi cred file\"%s\"",
361 gssapi_client.store.filename);
7cac2b65 362 unlink(gssapi_client.store.filename);
363 }
364}
365
366/* As user */
367void
368ssh_gssapi_storecreds(void)
369{
370 if (gssapi_client.mech && gssapi_client.mech->storecreds) {
ae82558b 371 if (options.gss_creds_path) {
372 gssapi_client.store.filename =
373 expand_authorized_keys(options.gss_creds_path,
374 the_authctxt->pw);
375 }
7cac2b65 376 (*gssapi_client.mech->storecreds)(&gssapi_client);
7cac2b65 377 } else
378 debug("ssh_gssapi_storecreds: Not a GSSAPI mechanism");
379}
380
381/* This allows GSSAPI methods to do things to the childs environment based
382 * on the passed authentication process and credentials.
383 */
384/* As user */
385void
386ssh_gssapi_do_child(char ***envp, u_int *envsizep)
387{
388
389 if (gssapi_client.store.envvar != NULL &&
390 gssapi_client.store.envval != NULL) {
7cac2b65 391 debug("Setting %s to %s", gssapi_client.store.envvar,
08822d99 392 gssapi_client.store.envval);
7cac2b65 393 child_set_env(envp, envsizep, gssapi_client.store.envvar,
2ce0bfe4 394 gssapi_client.store.envval);
7cac2b65 395 }
396}
397
08822d99 398/* Privileged */
7cac2b65 399int
400ssh_gssapi_userok(char *user)
401{
2ce0bfe4 402 OM_uint32 lmin;
403
7cac2b65 404 if (gssapi_client.exportedname.length == 0 ||
405 gssapi_client.exportedname.value == NULL) {
406 debug("No suitable client data");
407 return 0;
408 }
b4cfa386 409#ifdef GSS_C_GLOBUS_LIMITED_PROXY_FLAG
826a9049 410 if (limited && options.gsi_allow_limited_proxy != 1) {
b4cfa386 411 debug("limited proxy not acceptable for remote login");
412 return 0;
413 }
414#endif
7cac2b65 415 if (gssapi_client.mech && gssapi_client.mech->userok)
2ce0bfe4 416 if ((*gssapi_client.mech->userok)(&gssapi_client, user))
417 return 1;
418 else {
419 /* Destroy delegated credentials if userok fails */
420 gss_release_buffer(&lmin, &gssapi_client.displayname);
421 gss_release_buffer(&lmin, &gssapi_client.exportedname);
422 gss_release_cred(&lmin, &gssapi_client.creds);
423 memset(&gssapi_client, 0, sizeof(ssh_gssapi_client));
424 return 0;
425 }
7cac2b65 426 else
427 debug("ssh_gssapi_userok: Unknown GSSAPI mechanism");
428 return (0);
429}
430
c5b7701b 431/* ssh_gssapi_checkmic() moved to gss-genr.c so it can be called by
432 kexgss_client(). */
433
e23e524c 434/* Priviledged */
23987cb8 435int
436ssh_gssapi_localname(char **user)
437{
438 *user = NULL;
7cac2b65 439 if (gssapi_client.displayname.length==0 ||
440 gssapi_client.displayname.value==NULL) {
23987cb8 441 debug("No suitable client data");
442 return(0);;
443 }
444 if (gssapi_client.mech && gssapi_client.mech->localname) {
445 return((*gssapi_client.mech->localname)(&gssapi_client,user));
446 } else {
447 debug("Unknown client authentication type");
448 }
449 return(0);
450}
540d72c3 451
23987cb8 452#endif
This page took 0.559861 seconds and 5 git commands to generate.