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