xref: /freebsd/crypto/krb5/src/lib/gssapi/mechglue/g_dup_name.c (revision b670c9bafc0e31c7609969bf374b2e80bdc00211)
1 /*
2  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /* #pragma ident	"@(#)g_dup_name.c	1.14	04/02/23 SMI" */
7 
8 /*
9  *  routine gss_duplicate_name
10  *
11  * This routine does not rely on mechanism implementation of this
12  * name, but instead uses mechanism specific gss_import_name routine.
13  */
14 
15 #include <mglueP.h>
16 #ifdef HAVE_STDLIB_H
17 #include <stdlib.h>
18 #endif
19 #include <string.h>
20 #include <errno.h>
21 
22 static OM_uint32
23 val_dup_name_args(
24 	OM_uint32 *minor_status,
25 	const gss_name_t src_name,
26 	gss_name_t *dest_name)
27 {
28 
29 	/* Initialize outputs. */
30 
31 	if (minor_status != NULL)
32 		*minor_status = 0;
33 
34 	if (dest_name != NULL)
35 		*dest_name = GSS_C_NO_NAME;
36 
37 	/* Validate arguments. */
38 
39 	if (minor_status == NULL)
40 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
41 
42 	/* if output_name is NULL, simply return */
43 	if (dest_name == NULL)
44 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
45 
46 	if (src_name == GSS_C_NO_NAME)
47 		return (GSS_S_CALL_INACCESSIBLE_READ | GSS_S_BAD_NAME);
48 
49 	return (GSS_S_COMPLETE);
50 }
51 
52 
53 OM_uint32 KRB5_CALLCONV
54 gss_duplicate_name(minor_status,
55 		src_name,
56 		dest_name)
57 OM_uint32 *minor_status;
58 const gss_name_t src_name;
59 gss_name_t *dest_name;
60 {
61 		gss_union_name_t src_union, dest_union;
62 		OM_uint32 major_status = GSS_S_FAILURE;
63 
64 	major_status = val_dup_name_args(minor_status, src_name, dest_name);
65 	if (major_status != GSS_S_COMPLETE)
66 		return (major_status);
67 
68 	src_union = (gss_union_name_t)src_name;
69 
70 	/*
71 	 * First create the union name struct that will hold the external
72 	 * name and the name type.
73 	 */
74 	dest_union = (gss_union_name_t)malloc(sizeof (gss_union_name_desc));
75 	if (!dest_union)
76 		goto allocation_failure;
77 
78 	dest_union->loopback = 0;
79 	dest_union->mech_type = 0;
80 	dest_union->mech_name = 0;
81 	dest_union->name_type = 0;
82 	dest_union->external_name = 0;
83 
84 	/* Now copy the external representation. */
85 	if (gssint_create_copy_buffer(src_union->external_name,
86 				&dest_union->external_name, 0))
87 		goto allocation_failure;
88 
89 	if (src_union->name_type != GSS_C_NULL_OID) {
90 		major_status = generic_gss_copy_oid(minor_status,
91 						src_union->name_type,
92 						&dest_union->name_type);
93 		if (major_status != GSS_S_COMPLETE) {
94 		    map_errcode(minor_status);
95 		    goto allocation_failure;
96 		}
97 	}
98 
99 	/*
100 	 * See if source name is mechanim specific, if so then need to import it
101 	 */
102 	if (src_union->mech_type) {
103 		major_status = generic_gss_copy_oid(minor_status,
104 							src_union->mech_type,
105 							&dest_union->mech_type);
106 		if (major_status != GSS_S_COMPLETE) {
107 		    map_errcode(minor_status);
108 		    goto allocation_failure;
109 		}
110 
111 		major_status = gssint_import_internal_name(minor_status,
112 							src_union->mech_type,
113 							src_union,
114 							&dest_union->mech_name);
115 		if (major_status != GSS_S_COMPLETE)
116 			goto allocation_failure;
117 	}
118 
119 
120 	dest_union->loopback = dest_union;
121 	*dest_name = (gss_name_t)dest_union;
122 	return (GSS_S_COMPLETE);
123 
124 allocation_failure:
125 	if (dest_union) {
126 		if (dest_union->external_name) {
127 			if (dest_union->external_name->value)
128 				free(dest_union->external_name->value);
129 			free(dest_union->external_name);
130 		}
131 		if (dest_union->name_type)
132 			(void) generic_gss_release_oid(minor_status,
133 							&dest_union->name_type);
134 		if (dest_union->mech_name)
135 			(void) gssint_release_internal_name(minor_status,
136 						dest_union->mech_type,
137 						&dest_union->mech_name);
138 		if (dest_union->mech_type)
139 			(void) generic_gss_release_oid(minor_status,
140 							&dest_union->mech_type);
141 		free(dest_union);
142 	}
143 	return (major_status);
144 } /*	gss_duplicate_name	*/
145