Path: blob/main/crypto/heimdal/lib/gssapi/krb5/copy_ccache.c
34923 views
/*1* Copyright (c) 2000 - 2001, 2003 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include "gsskrb5_locl.h"3435#if 036OM_uint3237gss_krb5_copy_ccache(OM_uint32 *minor_status,38krb5_context context,39gss_cred_id_t cred,40krb5_ccache out)41{42krb5_error_code kret;4344HEIMDAL_MUTEX_lock(&cred->cred_id_mutex);4546if (cred->ccache == NULL) {47HEIMDAL_MUTEX_unlock(&cred->cred_id_mutex);48*minor_status = EINVAL;49return GSS_S_FAILURE;50}5152kret = krb5_cc_copy_cache(context, cred->ccache, out);53HEIMDAL_MUTEX_unlock(&cred->cred_id_mutex);54if (kret) {55*minor_status = kret;56return GSS_S_FAILURE;57}58*minor_status = 0;59return GSS_S_COMPLETE;60}61#endif626364OM_uint3265_gsskrb5_krb5_import_cred(OM_uint32 *minor_status,66krb5_ccache id,67krb5_principal keytab_principal,68krb5_keytab keytab,69gss_cred_id_t *cred)70{71krb5_context context;72krb5_error_code kret;73gsskrb5_cred handle;74OM_uint32 ret;7576*cred = NULL;7778GSSAPI_KRB5_INIT (&context);7980handle = calloc(1, sizeof(*handle));81if (handle == NULL) {82_gsskrb5_clear_status ();83*minor_status = ENOMEM;84return (GSS_S_FAILURE);85}86HEIMDAL_MUTEX_init(&handle->cred_id_mutex);8788handle->usage = 0;8990if (id) {91char *str;9293handle->usage |= GSS_C_INITIATE;9495kret = krb5_cc_get_principal(context, id,96&handle->principal);97if (kret) {98free(handle);99*minor_status = kret;100return GSS_S_FAILURE;101}102103if (keytab_principal) {104krb5_boolean match;105106match = krb5_principal_compare(context,107handle->principal,108keytab_principal);109if (match == FALSE) {110krb5_free_principal(context, handle->principal);111free(handle);112_gsskrb5_clear_status ();113*minor_status = EINVAL;114return GSS_S_FAILURE;115}116}117118ret = __gsskrb5_ccache_lifetime(minor_status,119context,120id,121handle->principal,122&handle->lifetime);123if (ret != GSS_S_COMPLETE) {124krb5_free_principal(context, handle->principal);125free(handle);126return ret;127}128129130kret = krb5_cc_get_full_name(context, id, &str);131if (kret)132goto out;133134kret = krb5_cc_resolve(context, str, &handle->ccache);135free(str);136if (kret)137goto out;138}139140141if (keytab) {142char *str;143144handle->usage |= GSS_C_ACCEPT;145146if (keytab_principal && handle->principal == NULL) {147kret = krb5_copy_principal(context,148keytab_principal,149&handle->principal);150if (kret)151goto out;152}153154kret = krb5_kt_get_full_name(context, keytab, &str);155if (kret)156goto out;157158kret = krb5_kt_resolve(context, str, &handle->keytab);159free(str);160if (kret)161goto out;162}163164165if (id || keytab) {166ret = gss_create_empty_oid_set(minor_status, &handle->mechanisms);167if (ret == GSS_S_COMPLETE)168ret = gss_add_oid_set_member(minor_status, GSS_KRB5_MECHANISM,169&handle->mechanisms);170if (ret != GSS_S_COMPLETE) {171kret = *minor_status;172goto out;173}174}175176*minor_status = 0;177*cred = (gss_cred_id_t)handle;178return GSS_S_COMPLETE;179180out:181gss_release_oid_set(minor_status, &handle->mechanisms);182if (handle->ccache)183krb5_cc_close(context, handle->ccache);184if (handle->keytab)185krb5_kt_close(context, handle->keytab);186if (handle->principal)187krb5_free_principal(context, handle->principal);188HEIMDAL_MUTEX_destroy(&handle->cred_id_mutex);189free(handle);190*minor_status = kret;191return GSS_S_FAILURE;192}193194195