(acquire_initiator_cred): handle the credential cache better, use
destroy/close when appriate and for all cases. Thanks to Michael Allen for point out the memory-leak that I also fixed. git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@22596 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
		| @@ -128,9 +128,12 @@ static OM_uint32 acquire_initiator_cred | |||||||
|     ret = GSS_S_FAILURE; |     ret = GSS_S_FAILURE; | ||||||
|     memset(&cred, 0, sizeof(cred)); |     memset(&cred, 0, sizeof(cred)); | ||||||
|  |  | ||||||
|     /* If we have a preferred principal, lets try to find it in all |     /*  | ||||||
|      * caches, otherwise, fall back to default cache.  Ignore |      * If we have a preferred principal, lets try to find it in all | ||||||
|      * errors. */ |      * caches, otherwise, fall back to default cache, ignore all | ||||||
|  |      * errors while searching. | ||||||
|  |      */ | ||||||
|  |  | ||||||
|     if (handle->principal) |     if (handle->principal) | ||||||
| 	kret = krb5_cc_cache_match (context, | 	kret = krb5_cc_cache_match (context, | ||||||
| 				    handle->principal, | 				    handle->principal, | ||||||
| @@ -142,32 +145,30 @@ static OM_uint32 acquire_initiator_cred | |||||||
| 	if (kret) | 	if (kret) | ||||||
| 	    goto end; | 	    goto end; | ||||||
|     } |     } | ||||||
|     kret = krb5_cc_get_principal(context, ccache, |     kret = krb5_cc_get_principal(context, ccache, &def_princ); | ||||||
| 	&def_princ); |  | ||||||
|     if (kret != 0) { |     if (kret != 0) { | ||||||
| 	/* we'll try to use a keytab below */ | 	/* we'll try to use a keytab below */ | ||||||
| 	krb5_cc_destroy(context, ccache); | 	krb5_cc_close(context, ccache); | ||||||
| 	ccache = NULL; | 	def_princ = NULL; | ||||||
| 	kret = 0; | 	kret = 0; | ||||||
|     } else if (handle->principal == NULL)  { |     } else if (handle->principal == NULL)  { | ||||||
| 	kret = krb5_copy_principal(context, def_princ, | 	kret = krb5_copy_principal(context, def_princ, &handle->principal); | ||||||
| 	    &handle->principal); |  | ||||||
| 	if (kret) | 	if (kret) | ||||||
| 	    goto end; | 	    goto end; | ||||||
|     } else if (handle->principal != NULL && |     } else if (handle->principal != NULL && | ||||||
| 	krb5_principal_compare(context, handle->principal, | 	       krb5_principal_compare(context, handle->principal, | ||||||
| 	def_princ) == FALSE) { | 				      def_princ) == FALSE) { | ||||||
| 	/* Before failing, lets check the keytab */ |  | ||||||
| 	krb5_free_principal(context, def_princ); | 	krb5_free_principal(context, def_princ); | ||||||
| 	def_princ = NULL; | 	def_princ = NULL; | ||||||
|  | 	krb5_cc_close(context, ccache); | ||||||
|  | 	ccache = NULL; | ||||||
|     } |     } | ||||||
|     if (def_princ == NULL) { |     if (def_princ == NULL) { | ||||||
| 	/* We have no existing credentials cache, | 	/* We have no existing credentials cache, | ||||||
| 	 * so attempt to get a TGT using a keytab. | 	 * so attempt to get a TGT using a keytab. | ||||||
| 	 */ | 	 */ | ||||||
| 	if (handle->principal == NULL) { | 	if (handle->principal == NULL) { | ||||||
| 	    kret = krb5_get_default_principal(context, | 	    kret = krb5_get_default_principal(context, &handle->principal); | ||||||
| 		&handle->principal); |  | ||||||
| 	    if (kret) | 	    if (kret) | ||||||
| 		goto end; | 		goto end; | ||||||
| 	} | 	} | ||||||
| @@ -182,16 +183,19 @@ static OM_uint32 acquire_initiator_cred | |||||||
| 	krb5_get_init_creds_opt_free(context, opt); | 	krb5_get_init_creds_opt_free(context, opt); | ||||||
| 	if (kret) | 	if (kret) | ||||||
| 	    goto end; | 	    goto end; | ||||||
| 	kret = krb5_cc_gen_new(context, &krb5_mcc_ops, | 	kret = krb5_cc_gen_new(context, &krb5_mcc_ops, &ccache); | ||||||
| 		&ccache); |  | ||||||
| 	if (kret) | 	if (kret) | ||||||
| 	    goto end; | 	    goto end; | ||||||
| 	kret = krb5_cc_initialize(context, ccache, cred.client); | 	kret = krb5_cc_initialize(context, ccache, cred.client); | ||||||
| 	if (kret) | 	if (kret) { | ||||||
|  | 	    krb5_cc_destroy(context, ccache); | ||||||
| 	    goto end; | 	    goto end; | ||||||
|  | 	} | ||||||
| 	kret = krb5_cc_store_cred(context, ccache, &cred); | 	kret = krb5_cc_store_cred(context, ccache, &cred); | ||||||
| 	if (kret) | 	if (kret) { | ||||||
|  | 	    krb5_cc_destroy(context, ccache); | ||||||
| 	    goto end; | 	    goto end; | ||||||
|  | 	} | ||||||
| 	handle->lifetime = cred.times.endtime; | 	handle->lifetime = cred.times.endtime; | ||||||
| 	handle->cred_flags |= GSS_CF_DESTROY_CRED_ON_RELEASE; | 	handle->cred_flags |= GSS_CF_DESTROY_CRED_ON_RELEASE; | ||||||
|     } else { |     } else { | ||||||
| @@ -201,8 +205,10 @@ static OM_uint32 acquire_initiator_cred | |||||||
| 					ccache, | 					ccache, | ||||||
| 					handle->principal, | 					handle->principal, | ||||||
| 					&handle->lifetime); | 					&handle->lifetime); | ||||||
| 	if (ret != GSS_S_COMPLETE) | 	if (ret != GSS_S_COMPLETE) { | ||||||
|  | 	    krb5_cc_close(context, ccache); | ||||||
| 	    goto end; | 	    goto end; | ||||||
|  | 	} | ||||||
| 	kret = 0; | 	kret = 0; | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -216,13 +222,8 @@ end: | |||||||
| 	krb5_free_principal(context, def_princ); | 	krb5_free_principal(context, def_princ); | ||||||
|     if (keytab != NULL) |     if (keytab != NULL) | ||||||
| 	krb5_kt_close(context, keytab); | 	krb5_kt_close(context, keytab); | ||||||
|     if (ret != GSS_S_COMPLETE) { |     if (ret != GSS_S_COMPLETE && kret != 0) | ||||||
| 	if (ccache != NULL) | 	*minor_status = kret; | ||||||
| 	    krb5_cc_close(context, ccache); |  | ||||||
| 	if (kret != 0) { |  | ||||||
| 	    *minor_status = kret; |  | ||||||
| 	} |  | ||||||
|     } |  | ||||||
|     return (ret); |     return (ret); | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Love Hörnquist Åstrand
					Love Hörnquist Åstrand