1264377Sdes/* $OpenBSD: gss-serv.c,v 1.26 2014/02/26 20:28:44 djm Exp $ */ 2124208Sdes 3124208Sdes/* 4124208Sdes * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved. 5124208Sdes * 6124208Sdes * Redistribution and use in source and binary forms, with or without 7124208Sdes * modification, are permitted provided that the following conditions 8124208Sdes * are met: 9124208Sdes * 1. Redistributions of source code must retain the above copyright 10124208Sdes * notice, this list of conditions and the following disclaimer. 11124208Sdes * 2. Redistributions in binary form must reproduce the above copyright 12124208Sdes * notice, this list of conditions and the following disclaimer in the 13124208Sdes * documentation and/or other materials provided with the distribution. 14124208Sdes * 15124208Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR 16124208Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17124208Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18124208Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19124208Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20124208Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21124208Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22124208Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23124208Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24124208Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25124208Sdes */ 26124208Sdes 27124208Sdes#include "includes.h" 28124208Sdes 29124208Sdes#ifdef GSSAPI 30124208Sdes 31162852Sdes#include <sys/types.h> 32181111Sdes#include <sys/param.h> 33162852Sdes 34162852Sdes#include <stdarg.h> 35162852Sdes#include <string.h> 36162852Sdes#include <unistd.h> 37162852Sdes 38181111Sdes#include "openbsd-compat/sys-queue.h" 39162852Sdes#include "xmalloc.h" 40162852Sdes#include "buffer.h" 41162852Sdes#include "key.h" 42162852Sdes#include "hostfile.h" 43124208Sdes#include "auth.h" 44124208Sdes#include "log.h" 45124208Sdes#include "channels.h" 46124208Sdes#include "session.h" 47162852Sdes#include "misc.h" 48124208Sdes 49124208Sdes#include "ssh-gss.h" 50124208Sdes 51124208Sdesstatic ssh_gssapi_client gssapi_client = 52124208Sdes { GSS_C_EMPTY_BUFFER, GSS_C_EMPTY_BUFFER, 53255767Sdes GSS_C_NO_CREDENTIAL, NULL, {NULL, NULL, NULL, NULL}}; 54124208Sdes 55124208Sdesssh_gssapi_mech gssapi_null_mech = 56124208Sdes { NULL, NULL, {0, NULL}, NULL, NULL, NULL, NULL}; 57124208Sdes 58124208Sdes#ifdef KRB5 59124208Sdesextern ssh_gssapi_mech gssapi_kerberos_mech; 60124208Sdes#endif 61124208Sdes 62124208Sdesssh_gssapi_mech* supported_mechs[]= { 63124208Sdes#ifdef KRB5 64124208Sdes &gssapi_kerberos_mech, 65124208Sdes#endif 66124208Sdes &gssapi_null_mech, 67124208Sdes}; 68124208Sdes 69264377Sdes/* 70264377Sdes * ssh_gssapi_supported_oids() can cause sandbox violations, so prepare the 71264377Sdes * list of supported mechanisms before privsep is set up. 72264377Sdes */ 73264377Sdesstatic gss_OID_set supported_oids; 74181111Sdes 75264377Sdesvoid 76264377Sdesssh_gssapi_prepare_supported_oids(void) 77264377Sdes{ 78264377Sdes ssh_gssapi_supported_oids(&supported_oids); 79264377Sdes} 80264377Sdes 81264377SdesOM_uint32 82264377Sdesssh_gssapi_test_oid_supported(OM_uint32 *ms, gss_OID member, int *present) 83264377Sdes{ 84264377Sdes if (supported_oids == NULL) 85264377Sdes ssh_gssapi_prepare_supported_oids(); 86264377Sdes return gss_test_oid_set_member(ms, member, supported_oids, present); 87264377Sdes} 88264377Sdes 89181111Sdes/* 90181111Sdes * Acquire credentials for a server running on the current host. 91181111Sdes * Requires that the context structure contains a valid OID 92181111Sdes */ 93181111Sdes 94181111Sdes/* Returns a GSSAPI error code */ 95181111Sdes/* Privileged (called from ssh_gssapi_server_ctx) */ 96181111Sdesstatic OM_uint32 97181111Sdesssh_gssapi_acquire_cred(Gssctxt *ctx) 98181111Sdes{ 99181111Sdes OM_uint32 status; 100181111Sdes char lname[MAXHOSTNAMELEN]; 101181111Sdes gss_OID_set oidset; 102181111Sdes 103181111Sdes gss_create_empty_oid_set(&status, &oidset); 104181111Sdes gss_add_oid_set_member(&status, ctx->oid, &oidset); 105181111Sdes 106181111Sdes if (gethostname(lname, MAXHOSTNAMELEN)) { 107181111Sdes gss_release_oid_set(&status, &oidset); 108181111Sdes return (-1); 109181111Sdes } 110181111Sdes 111181111Sdes if (GSS_ERROR(ssh_gssapi_import_name(ctx, lname))) { 112181111Sdes gss_release_oid_set(&status, &oidset); 113181111Sdes return (ctx->major); 114181111Sdes } 115181111Sdes 116181111Sdes if ((ctx->major = gss_acquire_cred(&ctx->minor, 117181111Sdes ctx->name, 0, oidset, GSS_C_ACCEPT, &ctx->creds, NULL, NULL))) 118181111Sdes ssh_gssapi_error(ctx); 119181111Sdes 120181111Sdes gss_release_oid_set(&status, &oidset); 121181111Sdes return (ctx->major); 122181111Sdes} 123181111Sdes 124181111Sdes/* Privileged */ 125181111SdesOM_uint32 126181111Sdesssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID oid) 127181111Sdes{ 128181111Sdes if (*ctx) 129181111Sdes ssh_gssapi_delete_ctx(ctx); 130181111Sdes ssh_gssapi_build_ctx(ctx); 131181111Sdes ssh_gssapi_set_oid(*ctx, oid); 132181111Sdes return (ssh_gssapi_acquire_cred(*ctx)); 133181111Sdes} 134181111Sdes 135157016Sdes/* Unprivileged */ 136124208Sdesvoid 137124208Sdesssh_gssapi_supported_oids(gss_OID_set *oidset) 138124208Sdes{ 139124208Sdes int i = 0; 140124208Sdes OM_uint32 min_status; 141124208Sdes int present; 142124208Sdes gss_OID_set supported; 143124208Sdes 144124208Sdes gss_create_empty_oid_set(&min_status, oidset); 145124208Sdes gss_indicate_mechs(&min_status, &supported); 146124208Sdes 147124208Sdes while (supported_mechs[i]->name != NULL) { 148124208Sdes if (GSS_ERROR(gss_test_oid_set_member(&min_status, 149124208Sdes &supported_mechs[i]->oid, supported, &present))) 150124208Sdes present = 0; 151124208Sdes if (present) 152124208Sdes gss_add_oid_set_member(&min_status, 153124208Sdes &supported_mechs[i]->oid, oidset); 154124208Sdes i++; 155124208Sdes } 156162852Sdes 157162852Sdes gss_release_oid_set(&min_status, &supported); 158124208Sdes} 159124208Sdes 160124208Sdes 161124208Sdes/* Wrapper around accept_sec_context 162124208Sdes * Requires that the context contains: 163124208Sdes * oid 164124208Sdes * credentials (from ssh_gssapi_acquire_cred) 165124208Sdes */ 166157016Sdes/* Privileged */ 167124208SdesOM_uint32 168124208Sdesssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *recv_tok, 169124208Sdes gss_buffer_desc *send_tok, OM_uint32 *flags) 170124208Sdes{ 171124208Sdes OM_uint32 status; 172124208Sdes gss_OID mech; 173124208Sdes 174124208Sdes ctx->major = gss_accept_sec_context(&ctx->minor, 175124208Sdes &ctx->context, ctx->creds, recv_tok, 176124208Sdes GSS_C_NO_CHANNEL_BINDINGS, &ctx->client, &mech, 177124208Sdes send_tok, flags, NULL, &ctx->client_creds); 178124208Sdes 179124208Sdes if (GSS_ERROR(ctx->major)) 180124208Sdes ssh_gssapi_error(ctx); 181124208Sdes 182124208Sdes if (ctx->client_creds) 183124208Sdes debug("Received some client credentials"); 184124208Sdes else 185124208Sdes debug("Got no client credentials"); 186124208Sdes 187124208Sdes status = ctx->major; 188124208Sdes 189124208Sdes /* Now, if we're complete and we have the right flags, then 190124208Sdes * we flag the user as also having been authenticated 191124208Sdes */ 192124208Sdes 193124208Sdes if (((flags == NULL) || ((*flags & GSS_C_MUTUAL_FLAG) && 194124208Sdes (*flags & GSS_C_INTEG_FLAG))) && (ctx->major == GSS_S_COMPLETE)) { 195124208Sdes if (ssh_gssapi_getclient(ctx, &gssapi_client)) 196124208Sdes fatal("Couldn't convert client name"); 197124208Sdes } 198124208Sdes 199124208Sdes return (status); 200124208Sdes} 201124208Sdes 202124208Sdes/* 203124208Sdes * This parses an exported name, extracting the mechanism specific portion 204124208Sdes * to use for ACL checking. It verifies that the name belongs the mechanism 205124208Sdes * originally selected. 206124208Sdes */ 207124208Sdesstatic OM_uint32 208124208Sdesssh_gssapi_parse_ename(Gssctxt *ctx, gss_buffer_t ename, gss_buffer_t name) 209124208Sdes{ 210149749Sdes u_char *tok; 211124208Sdes OM_uint32 offset; 212124208Sdes OM_uint32 oidl; 213124208Sdes 214157016Sdes tok = ename->value; 215124208Sdes 216124208Sdes /* 217124208Sdes * Check that ename is long enough for all of the fixed length 218124208Sdes * header, and that the initial ID bytes are correct 219124208Sdes */ 220124208Sdes 221157016Sdes if (ename->length < 6 || memcmp(tok, "\x04\x01", 2) != 0) 222124208Sdes return GSS_S_FAILURE; 223124208Sdes 224124208Sdes /* 225124208Sdes * Extract the OID, and check it. Here GSSAPI breaks with tradition 226124208Sdes * and does use the OID type and length bytes. To confuse things 227124208Sdes * there are two lengths - the first including these, and the 228124208Sdes * second without. 229124208Sdes */ 230124208Sdes 231162852Sdes oidl = get_u16(tok+2); /* length including next two bytes */ 232124208Sdes oidl = oidl-2; /* turn it into the _real_ length of the variable OID */ 233124208Sdes 234124208Sdes /* 235124208Sdes * Check the BER encoding for correct type and length, that the 236124208Sdes * string is long enough and that the OID matches that in our context 237124208Sdes */ 238124208Sdes if (tok[4] != 0x06 || tok[5] != oidl || 239124208Sdes ename->length < oidl+6 || 240157016Sdes !ssh_gssapi_check_oid(ctx, tok+6, oidl)) 241124208Sdes return GSS_S_FAILURE; 242124208Sdes 243124208Sdes offset = oidl+6; 244124208Sdes 245124208Sdes if (ename->length < offset+4) 246124208Sdes return GSS_S_FAILURE; 247124208Sdes 248162852Sdes name->length = get_u32(tok+offset); 249124208Sdes offset += 4; 250124208Sdes 251226046Sdes if (UINT_MAX - offset < name->length) 252226046Sdes return GSS_S_FAILURE; 253124208Sdes if (ename->length < offset+name->length) 254124208Sdes return GSS_S_FAILURE; 255124208Sdes 256124208Sdes name->value = xmalloc(name->length+1); 257162852Sdes memcpy(name->value, tok+offset, name->length); 258124208Sdes ((char *)name->value)[name->length] = 0; 259124208Sdes 260124208Sdes return GSS_S_COMPLETE; 261124208Sdes} 262124208Sdes 263124208Sdes/* Extract the client details from a given context. This can only reliably 264124208Sdes * be called once for a context */ 265124208Sdes 266157016Sdes/* Privileged (called from accept_secure_ctx) */ 267124208SdesOM_uint32 268124208Sdesssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client) 269124208Sdes{ 270124208Sdes int i = 0; 271124208Sdes 272124208Sdes gss_buffer_desc ename; 273124208Sdes 274124208Sdes client->mech = NULL; 275124208Sdes 276124208Sdes while (supported_mechs[i]->name != NULL) { 277124208Sdes if (supported_mechs[i]->oid.length == ctx->oid->length && 278124208Sdes (memcmp(supported_mechs[i]->oid.elements, 279124208Sdes ctx->oid->elements, ctx->oid->length) == 0)) 280124208Sdes client->mech = supported_mechs[i]; 281124208Sdes i++; 282124208Sdes } 283124208Sdes 284124208Sdes if (client->mech == NULL) 285124208Sdes return GSS_S_FAILURE; 286124208Sdes 287124208Sdes if ((ctx->major = gss_display_name(&ctx->minor, ctx->client, 288124208Sdes &client->displayname, NULL))) { 289124208Sdes ssh_gssapi_error(ctx); 290124208Sdes return (ctx->major); 291124208Sdes } 292124208Sdes 293124208Sdes if ((ctx->major = gss_export_name(&ctx->minor, ctx->client, 294124208Sdes &ename))) { 295124208Sdes ssh_gssapi_error(ctx); 296124208Sdes return (ctx->major); 297124208Sdes } 298124208Sdes 299124208Sdes if ((ctx->major = ssh_gssapi_parse_ename(ctx,&ename, 300124208Sdes &client->exportedname))) { 301124208Sdes return (ctx->major); 302124208Sdes } 303124208Sdes 304124208Sdes /* We can't copy this structure, so we just move the pointer to it */ 305124208Sdes client->creds = ctx->client_creds; 306124208Sdes ctx->client_creds = GSS_C_NO_CREDENTIAL; 307124208Sdes return (ctx->major); 308124208Sdes} 309124208Sdes 310126274Sdes/* As user - called on fatal/exit */ 311124208Sdesvoid 312126274Sdesssh_gssapi_cleanup_creds(void) 313124208Sdes{ 314124208Sdes if (gssapi_client.store.filename != NULL) { 315124208Sdes /* Unlink probably isn't sufficient */ 316162852Sdes debug("removing gssapi cred file\"%s\"", 317162852Sdes gssapi_client.store.filename); 318124208Sdes unlink(gssapi_client.store.filename); 319124208Sdes } 320124208Sdes} 321124208Sdes 322124208Sdes/* As user */ 323124208Sdesvoid 324124208Sdesssh_gssapi_storecreds(void) 325124208Sdes{ 326124208Sdes if (gssapi_client.mech && gssapi_client.mech->storecreds) { 327124208Sdes (*gssapi_client.mech->storecreds)(&gssapi_client); 328124208Sdes } else 329124208Sdes debug("ssh_gssapi_storecreds: Not a GSSAPI mechanism"); 330124208Sdes} 331124208Sdes 332124208Sdes/* This allows GSSAPI methods to do things to the childs environment based 333124208Sdes * on the passed authentication process and credentials. 334124208Sdes */ 335124208Sdes/* As user */ 336124208Sdesvoid 337124208Sdesssh_gssapi_do_child(char ***envp, u_int *envsizep) 338124208Sdes{ 339124208Sdes 340124208Sdes if (gssapi_client.store.envvar != NULL && 341124208Sdes gssapi_client.store.envval != NULL) { 342124208Sdes debug("Setting %s to %s", gssapi_client.store.envvar, 343157016Sdes gssapi_client.store.envval); 344124208Sdes child_set_env(envp, envsizep, gssapi_client.store.envvar, 345149749Sdes gssapi_client.store.envval); 346124208Sdes } 347124208Sdes} 348124208Sdes 349157016Sdes/* Privileged */ 350124208Sdesint 351124208Sdesssh_gssapi_userok(char *user) 352124208Sdes{ 353149749Sdes OM_uint32 lmin; 354149749Sdes 355124208Sdes if (gssapi_client.exportedname.length == 0 || 356124208Sdes gssapi_client.exportedname.value == NULL) { 357124208Sdes debug("No suitable client data"); 358124208Sdes return 0; 359124208Sdes } 360124208Sdes if (gssapi_client.mech && gssapi_client.mech->userok) 361149749Sdes if ((*gssapi_client.mech->userok)(&gssapi_client, user)) 362149749Sdes return 1; 363149749Sdes else { 364149749Sdes /* Destroy delegated credentials if userok fails */ 365149749Sdes gss_release_buffer(&lmin, &gssapi_client.displayname); 366149749Sdes gss_release_buffer(&lmin, &gssapi_client.exportedname); 367149749Sdes gss_release_cred(&lmin, &gssapi_client.creds); 368264377Sdes explicit_bzero(&gssapi_client, 369264377Sdes sizeof(ssh_gssapi_client)); 370149749Sdes return 0; 371149749Sdes } 372124208Sdes else 373124208Sdes debug("ssh_gssapi_userok: Unknown GSSAPI mechanism"); 374124208Sdes return (0); 375124208Sdes} 376124208Sdes 377157016Sdes/* Privileged */ 378126274SdesOM_uint32 379126274Sdesssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic) 380126274Sdes{ 381126274Sdes ctx->major = gss_verify_mic(&ctx->minor, ctx->context, 382126274Sdes gssbuf, gssmic, NULL); 383126274Sdes 384126274Sdes return (ctx->major); 385126274Sdes} 386126274Sdes 387124208Sdes#endif 388