1 /* 2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 3 * Use is subject to license terms. 4 */ 5 6 #pragma ident "%Z%%M% %I% %E% SMI" 7 8 /* 9 * lib/krb5/krb/copy_creds.c 10 * 11 * Copyright 1990,1991 by the Massachusetts Institute of Technology. 12 * All Rights Reserved. 13 * 14 * Export of this software from the United States of America may 15 * require a specific license from the United States Government. 16 * It is the responsibility of any person or organization contemplating 17 * export to obtain such a license before exporting. 18 * 19 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and 20 * distribute this software and its documentation for any purpose and 21 * without fee is hereby granted, provided that the above copyright 22 * notice appear in all copies and that both that copyright notice and 23 * this permission notice appear in supporting documentation, and that 24 * the name of M.I.T. not be used in advertising or publicity pertaining 25 * to distribution of the software without specific, written prior 26 * permission. Furthermore if you modify this software you must label 27 * your software as modified software and not distribute it in such a 28 * fashion that it might be confused with the original M.I.T. software. 29 * M.I.T. makes no representations about the suitability of 30 * this software for any purpose. It is provided "as is" without express 31 * or implied warranty. 32 * 33 * 34 * krb5_copy_cred() 35 */ 36 37 #include <k5-int.h> 38 39 /* 40 * Copy credentials, allocating fresh storage where needed. 41 */ 42 43 krb5_error_code KRB5_CALLCONV 44 krb5_copy_creds(krb5_context context, const krb5_creds *incred, krb5_creds **outcred) 45 { 46 krb5_creds *tempcred; 47 krb5_error_code retval; 48 krb5_data *scratch; 49 50 if (!(tempcred = (krb5_creds *)malloc(sizeof(*tempcred)))) 51 return ENOMEM; 52 53 #ifdef HAVE_C_STRUCTURE_ASSIGNMENT 54 *tempcred = *incred; /* copy everything quickly */ 55 #else 56 memcpy(tempcred, incred, sizeof(krb5_creds)); 57 #endif 58 retval = krb5_copy_principal(context, incred->client, &tempcred->client); 59 if (retval) 60 goto cleanlast; 61 retval = krb5_copy_principal(context, incred->server, &tempcred->server); 62 if (retval) 63 goto cleanclient; 64 retval = krb5_copy_keyblock_contents(context, &incred->keyblock, 65 &tempcred->keyblock); 66 if (retval) 67 goto cleanserver; 68 retval = krb5_copy_addresses(context, incred->addresses, &tempcred->addresses); 69 if (retval) 70 goto cleanblock; 71 retval = krb5_copy_data(context, &incred->ticket, &scratch); 72 if (retval) 73 goto cleanaddrs; 74 tempcred->ticket = *scratch; 75 krb5_xfree(scratch); 76 retval = krb5_copy_data(context, &incred->second_ticket, &scratch); 77 if (retval) 78 goto cleanticket; 79 80 tempcred->second_ticket = *scratch; 81 krb5_xfree(scratch); 82 83 retval = krb5_copy_authdata(context, incred->authdata,&tempcred->authdata); 84 if (retval) 85 goto clearticket; 86 87 *outcred = tempcred; 88 return 0; 89 90 clearticket: 91 memset(tempcred->ticket.data,0,tempcred->ticket.length); 92 cleanticket: 93 free(tempcred->ticket.data); 94 cleanaddrs: 95 krb5_free_addresses(context, tempcred->addresses); 96 cleanblock: 97 krb5_xfree(tempcred->keyblock.contents); 98 cleanserver: 99 krb5_free_principal(context, tempcred->server); 100 cleanclient: 101 krb5_free_principal(context, tempcred->client); 102 cleanlast: 103 krb5_xfree(tempcred); 104 return retval; 105 } 106