1 /*
2  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*
7  * lib/krb5/krb/copy_athctr.c
8  *
9  * Copyright 1990,1991 by the Massachusetts Institute of Technology.
10  * All Rights Reserved.
11  *
12  * Export of this software from the United States of America may
13  *   require a specific license from the United States Government.
14  *   It is the responsibility of any person or organization contemplating
15  *   export to obtain such a license before exporting.
16  *
17  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
18  * distribute this software and its documentation for any purpose and
19  * without fee is hereby granted, provided that the above copyright
20  * notice appear in all copies and that both that copyright notice and
21  * this permission notice appear in supporting documentation, and that
22  * the name of M.I.T. not be used in advertising or publicity pertaining
23  * to distribution of the software without specific, written prior
24  * permission.  Furthermore if you modify this software you must label
25  * your software as modified software and not distribute it in such a
26  * fashion that it might be confused with the original M.I.T. software.
27  * M.I.T. makes no representations about the suitability of
28  * this software for any purpose.  It is provided "as is" without express
29  * or implied warranty.
30  *
31  *
32  * krb5_copy_authenticator()
33  */
34 
35 #include <k5-int.h>
36 
37 krb5_error_code KRB5_CALLCONV
krb5_copy_authenticator(krb5_context context,const krb5_authenticator * authfrom,krb5_authenticator ** authto)38 krb5_copy_authenticator(krb5_context context, const krb5_authenticator *authfrom, krb5_authenticator **authto)
39 {
40     krb5_error_code retval;
41     krb5_authenticator *tempto;
42 
43     if (!(tempto = (krb5_authenticator *)MALLOC(sizeof(*tempto))))
44 	return ENOMEM;
45 #ifdef HAVE_C_STRUCTURE_ASSIGNMENT
46     *tempto = *authfrom;
47 #else
48     (void) memcpy(tempto, authfrom, sizeof(krb5_authenticator));
49 #endif
50 
51     retval = krb5_copy_principal(context, authfrom->client, &tempto->client);
52     if (retval) {
53 	krb5_xfree_wrap(tempto, sizeof(*tempto));
54 	return retval;
55     }
56 
57     if (authfrom->checksum &&
58 	(retval = krb5_copy_checksum(context, authfrom->checksum, &tempto->checksum))) {
59 	    krb5_free_principal(context, tempto->client);
60 	    krb5_xfree_wrap(tempto, sizeof(*tempto));
61 	    return retval;
62     }
63 
64     if (authfrom->subkey) {
65 	    retval = krb5_copy_keyblock(context, authfrom->subkey, &tempto->subkey);
66 	    if (retval) {
67 		    krb5_xfree_wrap(tempto->subkey, sizeof(krb5_keyblock));
68 		    krb5_free_checksum(context, tempto->checksum);
69 		    krb5_free_principal(context, tempto->client);
70 		    krb5_xfree_wrap(tempto, sizeof(*tempto));
71 		    return retval;
72 	    }
73     }
74 
75     if (authfrom->authorization_data) {
76 		retval = krb5_copy_authdata(context, authfrom->authorization_data,
77 				    &tempto->authorization_data);
78 		if (retval) {
79 		    krb5_xfree_wrap(tempto->subkey, sizeof(krb5_keyblock));
80 		    krb5_free_checksum(context, tempto->checksum);
81 		    krb5_free_principal(context, tempto->client);
82 		    krb5_free_authdata(context, tempto->authorization_data);
83 		    krb5_xfree_wrap(tempto, sizeof(*tempto));
84 		    return retval;
85 		}
86     }
87 
88     *authto = tempto;
89     return 0;
90 }
91