1 /* $OpenBSD: gss-serv.c,v 1.22 2008/05/08 12:02:23 djm Exp $ */
4 * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
31 #include <sys/types.h>
32 #include <sys/param.h>
38 #include "openbsd-compat/sys-queue.h"
53 #include "monitor_wrap.h"
55 extern ServerOptions options;
56 extern Authctxt *the_authctxt;
58 static ssh_gssapi_client gssapi_client =
59 { GSS_C_EMPTY_BUFFER, GSS_C_EMPTY_BUFFER,
60 GSS_C_NO_CREDENTIAL, GSS_C_NO_NAME, NULL, {NULL, NULL, NULL}, 0, 0};
62 ssh_gssapi_mech gssapi_null_mech =
63 { NULL, NULL, {0, NULL}, NULL, NULL, NULL, NULL, NULL};
66 extern ssh_gssapi_mech gssapi_kerberos_mech;
69 extern ssh_gssapi_mech gssapi_gsi_mech;
72 ssh_gssapi_mech* supported_mechs[]= {
74 &gssapi_kerberos_mech,
82 #ifdef GSS_C_GLOBUS_LIMITED_PROXY_FLAG
83 static int limited = 0;
87 * Acquire credentials for a server running on the current host.
88 * Requires that the context structure contains a valid OID
91 /* Returns a GSSAPI error code */
92 /* Privileged (called from ssh_gssapi_server_ctx) */
94 ssh_gssapi_acquire_cred(Gssctxt *ctx)
97 char lname[MAXHOSTNAMELEN];
100 if (options.gss_strict_acceptor) {
101 gss_create_empty_oid_set(&status, &oidset);
102 gss_add_oid_set_member(&status, ctx->oid, &oidset);
104 if (gethostname(lname, MAXHOSTNAMELEN)) {
105 gss_release_oid_set(&status, &oidset);
109 if (GSS_ERROR(ssh_gssapi_import_name(ctx, lname))) {
110 gss_release_oid_set(&status, &oidset);
114 if ((ctx->major = gss_acquire_cred(&ctx->minor,
115 ctx->name, 0, oidset, GSS_C_ACCEPT, &ctx->creds,
117 ssh_gssapi_error(ctx);
119 gss_release_oid_set(&status, &oidset);
122 ctx->name = GSS_C_NO_NAME;
123 ctx->creds = GSS_C_NO_CREDENTIAL;
125 return GSS_S_COMPLETE;
130 ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID oid)
133 ssh_gssapi_delete_ctx(ctx);
134 ssh_gssapi_build_ctx(ctx);
135 ssh_gssapi_set_oid(*ctx, oid);
136 return (ssh_gssapi_acquire_cred(*ctx));
141 ssh_gssapi_server_mechanisms() {
142 gss_OID_set supported;
144 ssh_gssapi_supported_oids(&supported);
145 return (ssh_gssapi_kex_mechs(supported, &ssh_gssapi_server_check_mech,
151 ssh_gssapi_server_check_mech(Gssctxt **dum, gss_OID oid, const char *data,
156 res = !GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctx, oid)));
157 ssh_gssapi_delete_ctx(&ctx);
164 ssh_gssapi_supported_oids(gss_OID_set *oidset)
167 OM_uint32 min_status;
169 gss_OID_set supported;
171 gss_create_empty_oid_set(&min_status, oidset);
173 /* Ask privileged process what mechanisms it supports. */
174 if (GSS_ERROR(PRIVSEP(gss_indicate_mechs(&min_status, &supported))))
177 while (supported_mechs[i]->name != NULL) {
178 if (GSS_ERROR(gss_test_oid_set_member(&min_status,
179 &supported_mechs[i]->oid, supported, &present)))
182 gss_add_oid_set_member(&min_status,
183 &supported_mechs[i]->oid, oidset);
187 gss_release_oid_set(&min_status, &supported);
191 /* Wrapper around accept_sec_context
192 * Requires that the context contains:
194 * credentials (from ssh_gssapi_acquire_cred)
198 ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *recv_tok,
199 gss_buffer_desc *send_tok, OM_uint32 *flags)
204 ctx->major = gss_accept_sec_context(&ctx->minor,
205 &ctx->context, ctx->creds, recv_tok,
206 GSS_C_NO_CHANNEL_BINDINGS, &ctx->client, &mech,
207 send_tok, flags, NULL, &ctx->client_creds);
209 if (GSS_ERROR(ctx->major))
210 ssh_gssapi_error(ctx);
212 if (ctx->client_creds)
213 debug("Received some client credentials");
215 debug("Got no client credentials");
219 /* Now, if we're complete and we have the right flags, then
220 * we flag the user as also having been authenticated
223 if (((flags == NULL) || ((*flags & GSS_C_MUTUAL_FLAG) &&
224 (*flags & GSS_C_INTEG_FLAG))) && (ctx->major == GSS_S_COMPLETE)) {
225 if (ssh_gssapi_getclient(ctx, &gssapi_client))
226 fatal("Couldn't convert client name");
227 #ifdef GSS_C_GLOBUS_LIMITED_PROXY_FLAG
228 if (flags && (*flags & GSS_C_GLOBUS_LIMITED_PROXY_FLAG))
237 * This parses an exported name, extracting the mechanism specific portion
238 * to use for ACL checking. It verifies that the name belongs the mechanism
239 * originally selected.
242 ssh_gssapi_parse_ename(Gssctxt *ctx, gss_buffer_t ename, gss_buffer_t name)
250 #ifdef GSI /* GSI gss_export_name() is broken. */
251 if ((ctx->oid->length == gssapi_gsi_mech.oid.length) &&
252 (memcmp(ctx->oid->elements, gssapi_gsi_mech.oid.elements,
253 gssapi_gsi_mech.oid.length) == 0)) {
254 name->length = ename->length;
255 name->value = xmalloc(ename->length+1);
256 memcpy(name->value, ename->value, ename->length);
257 return GSS_S_COMPLETE;
262 * Check that ename is long enough for all of the fixed length
263 * header, and that the initial ID bytes are correct
266 if (ename->length < 6 || memcmp(tok, "\x04\x01", 2) != 0)
267 return GSS_S_FAILURE;
270 * Extract the OID, and check it. Here GSSAPI breaks with tradition
271 * and does use the OID type and length bytes. To confuse things
272 * there are two lengths - the first including these, and the
276 oidl = get_u16(tok+2); /* length including next two bytes */
277 oidl = oidl-2; /* turn it into the _real_ length of the variable OID */
280 * Check the BER encoding for correct type and length, that the
281 * string is long enough and that the OID matches that in our context
283 if (tok[4] != 0x06 || tok[5] != oidl ||
284 ename->length < oidl+6 ||
285 !ssh_gssapi_check_oid(ctx, tok+6, oidl))
286 return GSS_S_FAILURE;
290 if (ename->length < offset+4)
291 return GSS_S_FAILURE;
293 name->length = get_u32(tok+offset);
296 if (ename->length < offset+name->length)
297 return GSS_S_FAILURE;
299 name->value = xmalloc(name->length+1);
300 memcpy(name->value, tok+offset, name->length);
301 ((char *)name->value)[name->length] = 0;
303 return GSS_S_COMPLETE;
306 /* Extract the client details from a given context. This can only reliably
307 * be called once for a context */
309 /* Privileged (called from accept_secure_ctx) */
311 ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
315 gss_name_t new_name = GSS_C_NO_NAME;
316 gss_buffer_desc ename = GSS_C_EMPTY_BUFFER;
318 if (options.gss_store_rekey && client->used && ctx->client_creds) {
319 if (client->mech->oid.length != ctx->oid->length ||
320 (memcmp(client->mech->oid.elements,
321 ctx->oid->elements, ctx->oid->length) !=0)) {
322 debug("Rekeyed credentials have different mechanism");
323 return GSS_S_COMPLETE;
326 /* Call gss_inquire_cred rather than gss_inquire_cred_by_mech
327 because GSI doesn't support the latter. -jbasney */
329 if ((ctx->major = gss_inquire_cred(&ctx->minor,
330 ctx->client_creds, &new_name,
331 NULL, NULL, NULL))) {
332 ssh_gssapi_error(ctx);
336 ctx->major = gss_compare_name(&ctx->minor, client->name,
339 if (GSS_ERROR(ctx->major)) {
340 ssh_gssapi_error(ctx);
345 debug("Rekeyed credentials have different name");
346 return GSS_S_COMPLETE;
349 debug("Marking rekeyed credentials for export");
351 gss_release_name(&ctx->minor, &client->name);
352 gss_release_cred(&ctx->minor, &client->creds);
353 client->name = new_name;
354 client->creds = ctx->client_creds;
355 ctx->client_creds = GSS_C_NO_CREDENTIAL;
357 return GSS_S_COMPLETE;
362 while (supported_mechs[i]->name != NULL) {
363 if (supported_mechs[i]->oid.length == ctx->oid->length &&
364 (memcmp(supported_mechs[i]->oid.elements,
365 ctx->oid->elements, ctx->oid->length) == 0))
366 client->mech = supported_mechs[i];
370 if (client->mech == NULL)
371 return GSS_S_FAILURE;
373 /* Call gss_inquire_cred rather than gss_inquire_cred_by_mech
374 because GSI doesn't support the latter. -jbasney */
376 if (ctx->client_creds &&
377 (ctx->major = gss_inquire_cred(&ctx->minor,
378 ctx->client_creds, &client->name, NULL, NULL, NULL))) {
379 ssh_gssapi_error(ctx);
383 if ((ctx->major = gss_display_name(&ctx->minor, ctx->client,
384 &client->displayname, NULL))) {
385 ssh_gssapi_error(ctx);
389 if ((ctx->major = gss_export_name(&ctx->minor, ctx->client,
391 ssh_gssapi_error(ctx);
395 if ((ctx->major = ssh_gssapi_parse_ename(ctx,&ename,
396 &client->exportedname))) {
400 gss_release_buffer(&ctx->minor, &ename);
402 /* We can't copy this structure, so we just move the pointer to it */
403 client->creds = ctx->client_creds;
404 ctx->client_creds = GSS_C_NO_CREDENTIAL;
406 /* needed for globus_gss_assist_map_and_authorize() */
407 client->context = ctx->context;
412 /* As user - called on fatal/exit */
414 ssh_gssapi_cleanup_creds(void)
416 if (gssapi_client.store.filename != NULL) {
417 /* Unlink probably isn't sufficient */
418 debug("removing gssapi cred file\"%s\"",
419 gssapi_client.store.filename);
420 unlink(gssapi_client.store.filename);
426 ssh_gssapi_storecreds(void)
428 if (gssapi_client.mech && gssapi_client.mech->storecreds) {
429 if (options.gss_creds_path) {
430 gssapi_client.store.filename =
431 expand_authorized_keys(options.gss_creds_path,
434 (*gssapi_client.mech->storecreds)(&gssapi_client);
436 debug("ssh_gssapi_storecreds: Not a GSSAPI mechanism");
439 /* This allows GSSAPI methods to do things to the childs environment based
440 * on the passed authentication process and credentials.
444 ssh_gssapi_do_child(char ***envp, u_int *envsizep)
447 if (gssapi_client.store.envvar != NULL &&
448 gssapi_client.store.envval != NULL) {
449 debug("Setting %s to %s", gssapi_client.store.envvar,
450 gssapi_client.store.envval);
451 child_set_env(envp, envsizep, gssapi_client.store.envvar,
452 gssapi_client.store.envval);
458 ssh_gssapi_userok(char *user, struct passwd *pw)
462 if (gssapi_client.exportedname.length == 0 ||
463 gssapi_client.exportedname.value == NULL) {
464 debug("No suitable client data");
467 #ifdef GSS_C_GLOBUS_LIMITED_PROXY_FLAG
468 if (limited && options.gsi_allow_limited_proxy != 1) {
469 debug("limited proxy not acceptable for remote login");
473 if (gssapi_client.mech && gssapi_client.mech->userok)
474 if ((*gssapi_client.mech->userok)(&gssapi_client, user)) {
475 gssapi_client.used = 1;
476 gssapi_client.store.owner = pw;
479 /* Destroy delegated credentials if userok fails */
480 gss_release_buffer(&lmin, &gssapi_client.displayname);
481 gss_release_buffer(&lmin, &gssapi_client.exportedname);
482 gss_release_cred(&lmin, &gssapi_client.creds);
483 memset(&gssapi_client, 0, sizeof(ssh_gssapi_client));
487 debug("ssh_gssapi_userok: Unknown GSSAPI mechanism");
491 /* ssh_gssapi_checkmic() moved to gss-genr.c so it can be called by
496 ssh_gssapi_localname(char **user)
499 if (gssapi_client.displayname.length==0 ||
500 gssapi_client.displayname.value==NULL) {
501 debug("No suitable client data");
504 if (gssapi_client.mech && gssapi_client.mech->localname) {
505 return((*gssapi_client.mech->localname)(&gssapi_client,user));
507 debug("Unknown client authentication type");
512 /* These bits are only used for rekeying. The unpriviledged child is running
513 * as the user, the monitor is root.
515 * In the child, we want to :
516 * *) Ask the monitor to store our credentials into the store we specify
517 * *) If it succeeds, maybe do a PAM update
523 static int ssh_gssapi_simple_conv(int n, const struct pam_message **msg,
524 struct pam_response **resp, void *data)
526 return (PAM_CONV_ERR);
531 ssh_gssapi_rekey_creds() {
534 pam_handle_t *pamh = NULL;
535 struct pam_conv pamconv = {ssh_gssapi_simple_conv, NULL};
539 if (gssapi_client.store.filename == NULL &&
540 gssapi_client.store.envval == NULL &&
541 gssapi_client.store.envvar == NULL)
544 ok = PRIVSEP(ssh_gssapi_update_creds(&gssapi_client.store));
549 debug("Rekeyed credentials stored successfully");
551 /* Actually managing to play with the ssh pam stack from here will
552 * be next to impossible. In any case, we may want different options
553 * for rekeying. So, use our own :)
557 debug("Not even going to try and do PAM with privsep disabled");
561 ret = pam_start("sshd-rekey", gssapi_client.store.owner->pw_name,
566 xasprintf(&envstr, "%s=%s", gssapi_client.store.envvar,
567 gssapi_client.store.envval);
569 ret = pam_putenv(pamh, envstr);
571 pam_setcred(pamh, PAM_REINITIALIZE_CRED);
572 pam_end(pamh, PAM_SUCCESS);
577 ssh_gssapi_update_creds(ssh_gssapi_ccache *store) {
580 /* Check we've got credentials to store */
581 if (!gssapi_client.updated)
584 gssapi_client.updated = 0;
586 temporarily_use_uid(gssapi_client.store.owner);
587 if (gssapi_client.mech && gssapi_client.mech->updatecreds)
588 ok = (*gssapi_client.mech->updatecreds)(store, &gssapi_client);
590 debug("No update function for this mechanism");