xref: /titanic_41/usr/src/lib/libgss/g_canon_name.c (revision d5ace9454616652a717c9831d949dffa319381f9)
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_canonicalize_name
30  *
31  * This routine is used to produce a mechanism specific
32  * representation of name that has been previously
33  * imported with gss_import_name.  The routine uses the mechanism
34  * specific implementation of gss_import_name to implement this
35  * function.
36  *
37  * We allow a NULL output_name, in which case we modify the
38  * input_name to include the mechanism specific name.
39  */
40 
41 #include <mechglueP.h>
42 #ifdef HAVE_STDLIB_H
43 #include <stdlib.h>
44 #endif
45 #include <string.h>
46 #include <errno.h>
47 
48 OM_uint32
49 gss_canonicalize_name(minor_status,
50 				input_name,
51 				mech_type,
52 				output_name)
53 OM_uint32 *minor_status;
54 const gss_name_t input_name;
55 const gss_OID mech_type;
56 gss_name_t *output_name;
57 {
58 	gss_union_name_t in_union, out_union = NULL, dest_union = NULL;
59 	OM_uint32 major_status = GSS_S_FAILURE;
60 
61 	if (minor_status == NULL)
62 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
63 
64 	*minor_status = 0;
65 
66 	if (output_name)
67 		*output_name = 0;
68 
69 	/* check the input parameters */
70 	if (input_name == NULL || mech_type == GSS_C_NULL_OID)
71 		return (GSS_S_CALL_INACCESSIBLE_READ);
72 
73 	in_union = (gss_union_name_t)input_name;
74 	/*
75 	 * If the caller wants to reuse the name, and the name has already
76 	 * been converted, then there is nothing for us to do.
77 	 */
78 	if (!output_name && in_union->mech_type &&
79 		g_OID_equal(in_union->mech_type, mech_type))
80 		return (GSS_S_COMPLETE);
81 
82 	/* ok, then we need to do something - start by creating data struct */
83 	if (output_name) {
84 		out_union =
85 			(gss_union_name_t)malloc(sizeof (gss_union_name_desc));
86 		if (!out_union)
87 			goto allocation_failure;
88 
89 		out_union->mech_type = 0;
90 		out_union->mech_name = 0;
91 		out_union->name_type = 0;
92 		out_union->external_name = 0;
93 
94 		/* Allocate the buffer for the user specified representation */
95 		if (gssint_create_copy_buffer(in_union->external_name,
96 				&out_union->external_name, 1))
97 			goto allocation_failure;
98 
99 		if (in_union->name_type != GSS_C_NULL_OID) {
100 			if ((major_status = generic_gss_copy_oid(minor_status,
101 				in_union->name_type, &out_union->name_type)))
102 			goto allocation_failure;
103 		}
104 
105 	}
106 
107 	/*
108 	 * might need to delete any old mechanism names if we are
109 	 * reusing the buffer.
110 	 */
111 	if (!output_name) {
112 		if (in_union->mech_type) {
113 			(void) __gss_release_internal_name(minor_status,
114 							in_union->mech_type,
115 							&in_union->mech_name);
116 			(void) gss_release_oid(minor_status,
117 					    &in_union->mech_type);
118 			in_union->mech_type = 0;
119 		}
120 		dest_union = in_union;
121 	} else
122 		dest_union = out_union;
123 
124 	/* now let's create the new mech name */
125 	if (major_status = generic_gss_copy_oid(minor_status, mech_type,
126 						&dest_union->mech_type))
127 		goto allocation_failure;
128 
129 	if (major_status =
130 		__gss_import_internal_name(minor_status, mech_type,
131 						dest_union,
132 						&dest_union->mech_name))
133 		goto allocation_failure;
134 
135 	if (output_name)
136 		*output_name = (gss_name_t)dest_union;
137 
138 	return (GSS_S_COMPLETE);
139 
140 allocation_failure:
141 	/* do not delete the src name external name format */
142 	if (output_name) {
143 		if (out_union->external_name) {
144 			if (out_union->external_name->value)
145 				free(out_union->external_name->value);
146 			free(out_union->external_name);
147 		}
148 		if (out_union->name_type)
149 			(void) gss_release_oid(minor_status,
150 					    &out_union->name_type);
151 
152 		dest_union = out_union;
153 	} else
154 		dest_union = in_union;
155 
156 	/*
157 	 * delete the partially created mech specific name
158 	 * applies for both src and dest which ever is being used for output
159 	 */
160 
161 	if (dest_union->mech_name) {
162 		(void) __gss_release_internal_name(minor_status,
163 						dest_union->mech_type,
164 						&dest_union->mech_name);
165 	}
166 
167 	if (dest_union->mech_type)
168 		(void) gss_release_oid(minor_status, &dest_union->mech_type);
169 
170 
171 	if (output_name)
172 		free(out_union);
173 
174 	return (major_status);
175 } /**********  gss_canonicalize_name ********/
176