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