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