copy_creds.c revision 7934:6aeeafc994de
1169695Skan/*
2169695Skan * lib/krb5/krb/copy_creds.c
3169695Skan *
4169695Skan * Copyright 1990,1991 by the Massachusetts Institute of Technology.
5169695Skan * All Rights Reserved.
6169695Skan *
7169695Skan * Export of this software from the United States of America may
8169695Skan *   require a specific license from the United States Government.
9169695Skan *   It is the responsibility of any person or organization contemplating
10169695Skan *   export to obtain such a license before exporting.
11169695Skan *
12169695Skan * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13169695Skan * distribute this software and its documentation for any purpose and
14169695Skan * without fee is hereby granted, provided that the above copyright
15169695Skan * notice appear in all copies and that both that copyright notice and
16169695Skan * this permission notice appear in supporting documentation, and that
17169695Skan * the name of M.I.T. not be used in advertising or publicity pertaining
18169695Skan * to distribution of the software without specific, written prior
19169695Skan * permission.  Furthermore if you modify this software you must label
20169695Skan * your software as modified software and not distribute it in such a
21169695Skan * fashion that it might be confused with the original M.I.T. software.
22169695Skan * M.I.T. makes no representations about the suitability of
23169695Skan * this software for any purpose.  It is provided "as is" without express
24169695Skan * or implied warranty.
25169695Skan *
26169695Skan *
27169695Skan * krb5_copy_cred()
28169695Skan */
29169695Skan
30169695Skan#include "k5-int.h"
31169695Skan
32169695Skan/*
33169695Skan * Copy credentials, allocating fresh storage where needed.
34169695Skan */
35169695Skan
36169695Skankrb5_error_code KRB5_CALLCONV
37169695Skankrb5_copy_creds(krb5_context context, const krb5_creds *incred, krb5_creds **outcred)
38169695Skan{
39169695Skan    krb5_creds *tempcred;
40169695Skan    krb5_error_code retval;
41169695Skan    krb5_data *scratch;
42169695Skan
43169695Skan    if (!(tempcred = (krb5_creds *)malloc(sizeof(*tempcred))))
44169695Skan	return ENOMEM;
45169695Skan
46169695Skan    *tempcred = *incred;
47169695Skan    retval = krb5_copy_principal(context, incred->client, &tempcred->client);
48169695Skan    if (retval)
49169695Skan	goto cleanlast;
50169695Skan    retval = krb5_copy_principal(context, incred->server, &tempcred->server);
51169695Skan    if (retval)
52169695Skan	goto cleanclient;
53169695Skan    retval = krb5_copy_keyblock_contents(context, &incred->keyblock,
54169695Skan					 &tempcred->keyblock);
55169695Skan    if (retval)
56169695Skan	goto cleanserver;
57169695Skan    retval = krb5_copy_addresses(context, incred->addresses, &tempcred->addresses);
58169695Skan    if (retval)
59169695Skan	goto cleanblock;
60169695Skan    retval = krb5_copy_data(context, &incred->ticket, &scratch);
61169695Skan    if (retval)
62169695Skan	goto cleanaddrs;
63169695Skan    tempcred->ticket = *scratch;
64169695Skan    krb5_xfree(scratch);
65169695Skan    retval = krb5_copy_data(context, &incred->second_ticket, &scratch);
66169695Skan    if (retval)
67169695Skan	goto cleanticket;
68169695Skan
69169695Skan    tempcred->second_ticket = *scratch;
70169695Skan    krb5_xfree(scratch);
71169695Skan
72169695Skan    retval = krb5_copy_authdata(context, incred->authdata,&tempcred->authdata);
73169695Skan    if (retval)
74169695Skan        goto clearticket;
75169695Skan
76169695Skan    *outcred = tempcred;
77169695Skan    return 0;
78169695Skan
79169695Skan clearticket:
80169695Skan    memset(tempcred->ticket.data,0,tempcred->ticket.length);
81169695Skan cleanticket:
82169695Skan    free(tempcred->ticket.data);
83169695Skan cleanaddrs:
84169695Skan    krb5_free_addresses(context, tempcred->addresses);
85169695Skan cleanblock:
86169695Skan    krb5_xfree(tempcred->keyblock.contents);
87169695Skan cleanserver:
88169695Skan    krb5_free_principal(context, tempcred->server);
89169695Skan cleanclient:
90169695Skan    krb5_free_principal(context, tempcred->client);
91169695Skan cleanlast:
92169695Skan    krb5_xfree(tempcred);
93169695Skan    return retval;
94169695Skan}
95169695Skan