17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * lib/krb5/krb/copy_creds.c
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * Copyright 1990,1991 by the Massachusetts Institute of Technology.
57c478bd9Sstevel@tonic-gate  * All Rights Reserved.
67c478bd9Sstevel@tonic-gate  *
77c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may
87c478bd9Sstevel@tonic-gate  *   require a specific license from the United States Government.
97c478bd9Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
107c478bd9Sstevel@tonic-gate  *   export to obtain such a license before exporting.
11*55fea89dSDan Cross  *
127c478bd9Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
137c478bd9Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
147c478bd9Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
157c478bd9Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
167c478bd9Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
177c478bd9Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
187c478bd9Sstevel@tonic-gate  * to distribution of the software without specific, written prior
197c478bd9Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
207c478bd9Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
217c478bd9Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
227c478bd9Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
237c478bd9Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
247c478bd9Sstevel@tonic-gate  * or implied warranty.
25*55fea89dSDan Cross  *
267c478bd9Sstevel@tonic-gate  *
277c478bd9Sstevel@tonic-gate  * krb5_copy_cred()
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
30159d09a2SMark Phalan #include "k5-int.h"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * Copy credentials, allocating fresh storage where needed.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
36505d05c7Sgtb krb5_error_code KRB5_CALLCONV
krb5_copy_creds(krb5_context context,const krb5_creds * incred,krb5_creds ** outcred)37505d05c7Sgtb krb5_copy_creds(krb5_context context, const krb5_creds *incred, krb5_creds **outcred)
387c478bd9Sstevel@tonic-gate {
397c478bd9Sstevel@tonic-gate     krb5_creds *tempcred;
407c478bd9Sstevel@tonic-gate     krb5_error_code retval;
417c478bd9Sstevel@tonic-gate     krb5_data *scratch;
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate     if (!(tempcred = (krb5_creds *)malloc(sizeof(*tempcred))))
447c478bd9Sstevel@tonic-gate 	return ENOMEM;
457c478bd9Sstevel@tonic-gate 
46159d09a2SMark Phalan     *tempcred = *incred;
477c478bd9Sstevel@tonic-gate     retval = krb5_copy_principal(context, incred->client, &tempcred->client);
487c478bd9Sstevel@tonic-gate     if (retval)
497c478bd9Sstevel@tonic-gate 	goto cleanlast;
507c478bd9Sstevel@tonic-gate     retval = krb5_copy_principal(context, incred->server, &tempcred->server);
517c478bd9Sstevel@tonic-gate     if (retval)
527c478bd9Sstevel@tonic-gate 	goto cleanclient;
537c478bd9Sstevel@tonic-gate     retval = krb5_copy_keyblock_contents(context, &incred->keyblock,
547c478bd9Sstevel@tonic-gate 					 &tempcred->keyblock);
557c478bd9Sstevel@tonic-gate     if (retval)
567c478bd9Sstevel@tonic-gate 	goto cleanserver;
577c478bd9Sstevel@tonic-gate     retval = krb5_copy_addresses(context, incred->addresses, &tempcred->addresses);
587c478bd9Sstevel@tonic-gate     if (retval)
597c478bd9Sstevel@tonic-gate 	goto cleanblock;
607c478bd9Sstevel@tonic-gate     retval = krb5_copy_data(context, &incred->ticket, &scratch);
617c478bd9Sstevel@tonic-gate     if (retval)
627c478bd9Sstevel@tonic-gate 	goto cleanaddrs;
637c478bd9Sstevel@tonic-gate     tempcred->ticket = *scratch;
647c478bd9Sstevel@tonic-gate     krb5_xfree(scratch);
657c478bd9Sstevel@tonic-gate     retval = krb5_copy_data(context, &incred->second_ticket, &scratch);
667c478bd9Sstevel@tonic-gate     if (retval)
677c478bd9Sstevel@tonic-gate 	goto cleanticket;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate     tempcred->second_ticket = *scratch;
707c478bd9Sstevel@tonic-gate     krb5_xfree(scratch);
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate     retval = krb5_copy_authdata(context, incred->authdata,&tempcred->authdata);
737c478bd9Sstevel@tonic-gate     if (retval)
747c478bd9Sstevel@tonic-gate         goto clearticket;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate     *outcred = tempcred;
777c478bd9Sstevel@tonic-gate     return 0;
787c478bd9Sstevel@tonic-gate 
79*55fea89dSDan Cross  clearticket:
807c478bd9Sstevel@tonic-gate     memset(tempcred->ticket.data,0,tempcred->ticket.length);
817c478bd9Sstevel@tonic-gate  cleanticket:
827c478bd9Sstevel@tonic-gate     free(tempcred->ticket.data);
837c478bd9Sstevel@tonic-gate  cleanaddrs:
847c478bd9Sstevel@tonic-gate     krb5_free_addresses(context, tempcred->addresses);
857c478bd9Sstevel@tonic-gate  cleanblock:
867c478bd9Sstevel@tonic-gate     krb5_xfree(tempcred->keyblock.contents);
877c478bd9Sstevel@tonic-gate  cleanserver:
887c478bd9Sstevel@tonic-gate     krb5_free_principal(context, tempcred->server);
897c478bd9Sstevel@tonic-gate  cleanclient:
907c478bd9Sstevel@tonic-gate     krb5_free_principal(context, tempcred->client);
917c478bd9Sstevel@tonic-gate  cleanlast:
927c478bd9Sstevel@tonic-gate     krb5_xfree(tempcred);
937c478bd9Sstevel@tonic-gate     return retval;
947c478bd9Sstevel@tonic-gate }
95