1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*
28*7c478bd9Sstevel@tonic-gate  *  glue routine gss_import_name
29*7c478bd9Sstevel@tonic-gate  *
30*7c478bd9Sstevel@tonic-gate  */
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #include "mechglueP.h"
33*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
34*7c478bd9Sstevel@tonic-gate OM_uint32
gss_import_name(OM_uint32 * minor_status,const gss_buffer_t input_name_buffer,const gss_OID input_name_type,gss_name_t * output_name)35*7c478bd9Sstevel@tonic-gate gss_import_name(
36*7c478bd9Sstevel@tonic-gate 	OM_uint32 *minor_status,
37*7c478bd9Sstevel@tonic-gate 	const gss_buffer_t input_name_buffer,
38*7c478bd9Sstevel@tonic-gate 	const gss_OID input_name_type,
39*7c478bd9Sstevel@tonic-gate 	gss_name_t *output_name)
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate {
42*7c478bd9Sstevel@tonic-gate 	gss_union_name_t	union_name;
43*7c478bd9Sstevel@tonic-gate 	OM_uint32		major_status = GSS_S_FAILURE;
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate 	if (minor_status)
46*7c478bd9Sstevel@tonic-gate 		*minor_status = 0;
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate 	/* if output_name is NULL, simply return */
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate 	if (output_name == NULL)
51*7c478bd9Sstevel@tonic-gate 		return (GSS_S_COMPLETE);
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate 	*output_name = 0;
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 	if (input_name_buffer == GSS_C_NO_BUFFER || input_name_type == NULL)
56*7c478bd9Sstevel@tonic-gate 		return (GSS_S_BAD_NAME);
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate 	/*
59*7c478bd9Sstevel@tonic-gate 	 * First create the union name struct that will hold the external
60*7c478bd9Sstevel@tonic-gate 	 * name and the name type.
61*7c478bd9Sstevel@tonic-gate 	 */
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 	union_name = (gss_union_name_t) MALLOC(sizeof (gss_union_name_desc));
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 	if (!union_name) {
66*7c478bd9Sstevel@tonic-gate 		*minor_status = ENOMEM;
67*7c478bd9Sstevel@tonic-gate 		goto allocation_failure;
68*7c478bd9Sstevel@tonic-gate 	}
69*7c478bd9Sstevel@tonic-gate 	union_name->mech_type = 0;
70*7c478bd9Sstevel@tonic-gate 	union_name->mech_name = 0;
71*7c478bd9Sstevel@tonic-gate 	union_name->name_type = 0;
72*7c478bd9Sstevel@tonic-gate 	union_name->external_name = 0;
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 	/*
75*7c478bd9Sstevel@tonic-gate 	 * All we do here is record the external name and name_type.
76*7c478bd9Sstevel@tonic-gate 	 * When the name is actually used, the underlying gss_import_name()
77*7c478bd9Sstevel@tonic-gate 	 * is called for the appropriate mechanism.
78*7c478bd9Sstevel@tonic-gate 	 * Since the name type may be a constant or comming from the
79*7c478bd9Sstevel@tonic-gate 	 * rpc resoults, we must make a copy.
80*7c478bd9Sstevel@tonic-gate 	 */
81*7c478bd9Sstevel@tonic-gate 	union_name->external_name =
82*7c478bd9Sstevel@tonic-gate 	(gss_buffer_t) MALLOC(sizeof (gss_buffer_desc));
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	if (!union_name->external_name) {
85*7c478bd9Sstevel@tonic-gate 		*minor_status = ENOMEM;
86*7c478bd9Sstevel@tonic-gate 		goto allocation_failure;
87*7c478bd9Sstevel@tonic-gate 	}
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	union_name->external_name->length = input_name_buffer->length;
90*7c478bd9Sstevel@tonic-gate 	union_name->external_name->value =
91*7c478bd9Sstevel@tonic-gate 	(void *) MALLOC(input_name_buffer->length);
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 	if (!union_name->external_name->value) {
94*7c478bd9Sstevel@tonic-gate 		*minor_status = ENOMEM;
95*7c478bd9Sstevel@tonic-gate 		goto allocation_failure;
96*7c478bd9Sstevel@tonic-gate 	}
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 	(void) memcpy(union_name->external_name->value,
99*7c478bd9Sstevel@tonic-gate 	    input_name_buffer->value, input_name_buffer->length);
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	/*
102*7c478bd9Sstevel@tonic-gate 	 * making a copy of the name_type structure and elements
103*7c478bd9Sstevel@tonic-gate 	 * we now delete it when calling gss_release_name
104*7c478bd9Sstevel@tonic-gate 	 */
105*7c478bd9Sstevel@tonic-gate 	union_name->name_type = (gss_OID) MALLOC(sizeof (gss_OID_desc));
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 	if (!union_name->name_type) {
108*7c478bd9Sstevel@tonic-gate 		*minor_status = ENOMEM;
109*7c478bd9Sstevel@tonic-gate 		goto allocation_failure;
110*7c478bd9Sstevel@tonic-gate 	}
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	union_name->name_type->elements = (void *)
113*7c478bd9Sstevel@tonic-gate 		MALLOC(input_name_type->length);
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 	if (!union_name->name_type->elements) {
116*7c478bd9Sstevel@tonic-gate 		*minor_status = ENOMEM;
117*7c478bd9Sstevel@tonic-gate 		goto allocation_failure;
118*7c478bd9Sstevel@tonic-gate 	}
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 	(void) memcpy(union_name->name_type->elements,
121*7c478bd9Sstevel@tonic-gate 		input_name_type->elements, input_name_type->length);
122*7c478bd9Sstevel@tonic-gate 	union_name->name_type->length = input_name_type->length;
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 	*output_name = (gss_name_t) union_name;
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	return (GSS_S_COMPLETE);
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate allocation_failure:
129*7c478bd9Sstevel@tonic-gate 	if (union_name) {
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 		if (union_name->external_name) {
132*7c478bd9Sstevel@tonic-gate 			if (union_name->external_name->value)
133*7c478bd9Sstevel@tonic-gate 				FREE(union_name->external_name->value,
134*7c478bd9Sstevel@tonic-gate 					union_name->external_name->length);
135*7c478bd9Sstevel@tonic-gate 			FREE(union_name->external_name,
136*7c478bd9Sstevel@tonic-gate 				sizeof (gss_buffer_desc));
137*7c478bd9Sstevel@tonic-gate 		}
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 		if (union_name->name_type) {
140*7c478bd9Sstevel@tonic-gate 			FREE(union_name->name_type, sizeof (gss_OID_desc));
141*7c478bd9Sstevel@tonic-gate 		}
142*7c478bd9Sstevel@tonic-gate 		FREE(union_name, sizeof (gss_union_name_desc));
143*7c478bd9Sstevel@tonic-gate 	}
144*7c478bd9Sstevel@tonic-gate 	return (major_status);
145*7c478bd9Sstevel@tonic-gate }
146