xref: /titanic_44/usr/src/lib/libgss/g_dup_name.c (revision 349b53dd4e695e3d833b5380540385145b2d3ae8)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  *  routine gss_duplicate_name
28  *
29  * This routine does not rely on mechanism implementation of this
30  * name, but instead uses mechanism specific gss_import_name routine.
31  */
32 
33 #include <mechglueP.h>
34 #ifdef HAVE_STDLIB_H
35 #include <stdlib.h>
36 #endif
37 #include <string.h>
38 #include <errno.h>
39 
40 static OM_uint32
41 val_dup_name_args(
42 	OM_uint32 *minor_status,
43 	const gss_name_t src_name,
44 	gss_name_t *dest_name)
45 {
46 
47 	/* Initialize outputs. */
48 
49 	if (minor_status != NULL)
50 		*minor_status = 0;
51 
52 	if (dest_name != NULL)
53 		*dest_name = GSS_C_NO_NAME;
54 
55 	/* Validate arguments. */
56 
57 	if (minor_status == NULL)
58 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
59 
60 	/* if output_name is NULL, simply return */
61 	if (dest_name == NULL)
62 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
63 
64 	if (src_name == GSS_C_NO_NAME)
65 		return (GSS_S_CALL_INACCESSIBLE_READ | GSS_S_BAD_NAME);
66 
67 	return (GSS_S_COMPLETE);
68 }
69 
70 OM_uint32
71 gss_duplicate_name(minor_status,
72 		src_name,
73 		dest_name)
74 OM_uint32 *minor_status;
75 const gss_name_t src_name;
76 gss_name_t *dest_name;
77 {
78 		gss_union_name_t src_union, dest_union;
79 		OM_uint32 major_status = GSS_S_FAILURE;
80 
81 	major_status = val_dup_name_args(minor_status, src_name, dest_name);
82 	if (major_status != GSS_S_COMPLETE)
83 		return (major_status);
84 
85 	major_status = GSS_S_FAILURE;
86 
87 	src_union = (gss_union_name_t)src_name;
88 
89 	/*
90 	 * First create the union name struct that will hold the external
91 	 * name and the name type.
92 	 */
93 	dest_union = (gss_union_name_t)malloc(sizeof (gss_union_name_desc));
94 	if (!dest_union)
95 		goto allocation_failure;
96 
97 	dest_union->mech_type = 0;
98 	dest_union->mech_name = 0;
99 	dest_union->name_type = 0;
100 	dest_union->external_name = 0;
101 
102 	/* Now copy the external representaion */
103 	if (gssint_create_copy_buffer(src_union->external_name,
104 				&dest_union->external_name, 0))
105 		goto allocation_failure;
106 
107 	if (src_union->name_type != GSS_C_NULL_OID) {
108 		major_status = generic_gss_copy_oid(minor_status,
109 						src_union->name_type,
110 						&dest_union->name_type);
111 		if (major_status != GSS_S_COMPLETE)
112 			goto allocation_failure;
113 	}
114 
115 	/*
116 	 * See if source name is mechanim specific, if so then need to import it
117 	 */
118 	if (src_union->mech_type) {
119 		major_status = generic_gss_copy_oid(minor_status,
120 							src_union->mech_type,
121 							&dest_union->mech_type);
122 		if (major_status != GSS_S_COMPLETE)
123 			goto allocation_failure;
124 
125 		major_status = __gss_import_internal_name(minor_status,
126 							dest_union->mech_type,
127 							dest_union,
128 							&dest_union->mech_name);
129 		if (major_status != GSS_S_COMPLETE)
130 			goto allocation_failure;
131 	}
132 
133 
134 	*dest_name = (gss_name_t)dest_union;
135 	return (GSS_S_COMPLETE);
136 
137 allocation_failure:
138 	if (dest_union) {
139 		if (dest_union->external_name) {
140 			if (dest_union->external_name->value)
141 				free(dest_union->external_name->value);
142 				free(dest_union->external_name);
143 		}
144 		if (dest_union->name_type)
145 			(void) generic_gss_release_oid(minor_status,
146 							&dest_union->name_type);
147 		if (dest_union->mech_name)
148 			(void) __gss_release_internal_name(minor_status,
149 						dest_union->mech_type,
150 						&dest_union->mech_name);
151 		if (dest_union->mech_type)
152 			(void) generic_gss_release_oid(minor_status,
153 							&dest_union->mech_type);
154 		free(dest_union);
155 	}
156 	return (major_status);
157 } /*	gss_duplicate_name	*/
158