kadmin: refactor do_ext_keytab for common cleanup
Refactor do_ext_keytab() so that all cleanup is performed by jumping to the out label on error. Change-Id: Ic0c0f57e8ebabf30b49519f14743370d1c1672d2
This commit is contained in:
		
							
								
								
									
										23
									
								
								kadmin/ext.c
									
									
									
									
									
								
							
							
						
						
									
										23
									
								
								kadmin/ext.c
									
									
									
									
									
								
							| @@ -46,7 +46,7 @@ do_ext_keytab(krb5_principal principal, void *data) | |||||||
|     struct ext_keytab_data *e = data; |     struct ext_keytab_data *e = data; | ||||||
|     krb5_keytab_entry *keys = NULL; |     krb5_keytab_entry *keys = NULL; | ||||||
|     krb5_keyblock *k = NULL; |     krb5_keyblock *k = NULL; | ||||||
|     int i; |     size_t i; | ||||||
|     int n_k = 0; |     int n_k = 0; | ||||||
|  |  | ||||||
|     ret = kadm5_get_principal(kadm_handle, principal, &princ, |     ret = kadm5_get_principal(kadm_handle, principal, &princ, | ||||||
| @@ -57,9 +57,8 @@ do_ext_keytab(krb5_principal principal, void *data) | |||||||
|     if (princ.n_key_data) { |     if (princ.n_key_data) { | ||||||
| 	keys = calloc(sizeof(*keys), princ.n_key_data); | 	keys = calloc(sizeof(*keys), princ.n_key_data); | ||||||
| 	if (keys == NULL) { | 	if (keys == NULL) { | ||||||
| 	    kadm5_free_principal_ent(kadm_handle, &princ); | 	    ret = krb5_enomem(context); | ||||||
| 	    krb5_clear_error_message(context); | 	    goto out; | ||||||
| 	    return ENOMEM; |  | ||||||
| 	} | 	} | ||||||
| 	for (i = 0; i < princ.n_key_data; i++) { | 	for (i = 0; i < princ.n_key_data; i++) { | ||||||
| 	    krb5_key_data *kd = &princ.key_data[i]; | 	    krb5_key_data *kd = &princ.key_data[i]; | ||||||
| @@ -86,15 +85,13 @@ do_ext_keytab(krb5_principal principal, void *data) | |||||||
|     if (n_k == 0) { |     if (n_k == 0) { | ||||||
|         /* Probably lack get-keys privilege, but we may be able to set keys */ |         /* Probably lack get-keys privilege, but we may be able to set keys */ | ||||||
| 	ret = kadm5_randkey_principal(kadm_handle, principal, &k, &n_k); | 	ret = kadm5_randkey_principal(kadm_handle, principal, &k, &n_k); | ||||||
| 	if (ret) { | 	if (ret) | ||||||
| 	    kadm5_free_principal_ent(kadm_handle, &princ); | 	    goto out; | ||||||
| 	    return ret; |  | ||||||
| 	} |  | ||||||
| 	keys = calloc(sizeof(*keys), n_k); | 	keys = calloc(sizeof(*keys), n_k); | ||||||
| 	if (keys == NULL) { | 	if (keys == NULL) { | ||||||
| 	    kadm5_free_principal_ent(kadm_handle, &princ); | 	    ret = krb5_enomem(context); | ||||||
| 	    krb5_clear_error_message(context); | 	    goto out; | ||||||
| 	    return ENOMEM; |  | ||||||
| 	} | 	} | ||||||
| 	for (i = 0; i < n_k; i++) { | 	for (i = 0; i < n_k; i++) { | ||||||
| 	    keys[i].principal = principal; | 	    keys[i].principal = principal; | ||||||
| @@ -110,13 +107,13 @@ do_ext_keytab(krb5_principal principal, void *data) | |||||||
| 	    krb5_warn(context, ret, "krb5_kt_add_entry(%d)", i); | 	    krb5_warn(context, ret, "krb5_kt_add_entry(%d)", i); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |   out: | ||||||
|  |     kadm5_free_principal_ent(kadm_handle, &princ); | ||||||
|     if (k) { |     if (k) { | ||||||
| 	memset(k, 0, n_k * sizeof(*k)); | 	memset(k, 0, n_k * sizeof(*k)); | ||||||
| 	free(k); | 	free(k); | ||||||
|     } |     } | ||||||
|     if (keys) |  | ||||||
|     free(keys); |     free(keys); | ||||||
|     kadm5_free_principal_ent(kadm_handle, &princ); |  | ||||||
|     return 0; |     return 0; | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Jeffrey Altman
					Jeffrey Altman