xref: /titanic_41/usr/src/lib/libgss/g_dup_name.c (revision 4e9cfc9a015e8ca7d41f7d018c74dc8a692305b3)
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 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  *  routine gss_duplicate_name
30  *
31  * This routine does not rely on mechanism implementation of this
32  * name, but instead uses mechanism specific gss_import_name routine.
33  */
34 
35 #include <mechglueP.h>
36 #ifdef HAVE_STDLIB_H
37 #include <stdlib.h>
38 #endif
39 #include <string.h>
40 #include <errno.h>
41 
42 OM_uint32
43 gss_duplicate_name(minor_status,
44 		src_name,
45 		dest_name)
46 OM_uint32 *minor_status;
47 const gss_name_t src_name;
48 gss_name_t *dest_name;
49 {
50 		gss_union_name_t src_union, dest_union;
51 		OM_uint32 major_status = GSS_S_FAILURE;
52 
53 
54 	if (!minor_status)
55 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
56 
57 	*minor_status = 0;
58 
59 	/* if output_name is NULL, simply return */
60 	if (dest_name == NULL)
61 		return (GSS_S_CALL_INACCESSIBLE_WRITE | GSS_S_BAD_NAME);
62 
63 	*dest_name = 0;
64 
65 	if (src_name == NULL)
66 		return (GSS_S_CALL_INACCESSIBLE_READ);
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->mech_type = 0;
79 	dest_union->mech_name = 0;
80 	dest_union->name_type = 0;
81 	dest_union->external_name = 0;
82 
83 	/* Now copy the external representaion */
84 	if (gssint_create_copy_buffer(src_union->external_name,
85 				&dest_union->external_name, 0))
86 		goto allocation_failure;
87 
88 	if (src_union->name_type != GSS_C_NULL_OID) {
89 		major_status = generic_gss_copy_oid(minor_status,
90 						src_union->name_type,
91 						&dest_union->name_type);
92 		if (major_status != GSS_S_COMPLETE)
93 			goto allocation_failure;
94 	}
95 
96 	/*
97 	 * See if source name is mechanim specific, if so then need to import it
98 	 */
99 	if (src_union->mech_type) {
100 		major_status = generic_gss_copy_oid(minor_status,
101 							src_union->mech_type,
102 							&dest_union->mech_type);
103 		if (major_status != GSS_S_COMPLETE)
104 			goto allocation_failure;
105 
106 		major_status = __gss_import_internal_name(minor_status,
107 							dest_union->mech_type,
108 							dest_union,
109 							&dest_union->mech_name);
110 		if (major_status != GSS_S_COMPLETE)
111 			goto allocation_failure;
112 	}
113 
114 
115 	*dest_name = (gss_name_t)dest_union;
116 	return (GSS_S_COMPLETE);
117 
118 allocation_failure:
119 	if (dest_union) {
120 		if (dest_union->external_name) {
121 			if (dest_union->external_name->value)
122 				free(dest_union->external_name->value);
123 				free(dest_union->external_name);
124 		}
125 		if (dest_union->name_type)
126 			(void) generic_gss_release_oid(minor_status,
127 							&dest_union->name_type);
128 		if (dest_union->mech_name)
129 			(void) __gss_release_internal_name(minor_status,
130 						dest_union->mech_type,
131 						&dest_union->mech_name);
132 		if (dest_union->mech_type)
133 			(void) generic_gss_release_oid(minor_status,
134 							&dest_union->mech_type);
135 		free(dest_union);
136 	}
137 	return (major_status);
138 } /*	gss_duplicate_name	*/
139