xref: /titanic_41/usr/src/lib/libgss/g_dup_name.c (revision 49d3bc91e27cd871b950d56c01398fa2f2e12ab4)
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 (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 /*
26  *  routine gss_duplicate_name
27  *
28  * This routine does not rely on mechanism implementation of this
29  * name, but instead uses mechanism specific gss_import_name routine.
30  */
31 
32 #include <mechglueP.h>
33 #include "gssapiP_generic.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 			map_errcode(minor_status);
113 			goto allocation_failure;
114 		}
115 	}
116 
117 	/*
118 	 * See if source name is mechanim specific, if so then need to import it
119 	 */
120 	if (src_union->mech_type) {
121 		major_status = generic_gss_copy_oid(minor_status,
122 							src_union->mech_type,
123 							&dest_union->mech_type);
124 		if (major_status != GSS_S_COMPLETE) {
125 			map_errcode(minor_status);
126 			goto allocation_failure;
127 		}
128 
129 		major_status = __gss_import_internal_name(minor_status,
130 							dest_union->mech_type,
131 							dest_union,
132 							&dest_union->mech_name);
133 		if (major_status != GSS_S_COMPLETE)
134 			goto allocation_failure;
135 	}
136 
137 
138 	*dest_name = (gss_name_t)dest_union;
139 	return (GSS_S_COMPLETE);
140 
141 allocation_failure:
142 	if (dest_union) {
143 		if (dest_union->external_name) {
144 			if (dest_union->external_name->value)
145 				free(dest_union->external_name->value);
146 				free(dest_union->external_name);
147 		}
148 		if (dest_union->name_type)
149 			(void) generic_gss_release_oid(minor_status,
150 							&dest_union->name_type);
151 		if (dest_union->mech_name)
152 			(void) __gss_release_internal_name(minor_status,
153 						dest_union->mech_type,
154 						&dest_union->mech_name);
155 		if (dest_union->mech_type)
156 			(void) generic_gss_release_oid(minor_status,
157 							&dest_union->mech_type);
158 		free(dest_union);
159 	}
160 	return (major_status);
161 } /*	gss_duplicate_name	*/
162