xref: /titanic_52/usr/src/lib/libgss/g_canon_name.c (revision 5e01956f3000408c2a2c5a08c8d0acf2c2a9d8ee)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ab9b2e15Sgtb  * Common Development and Distribution License (the "License").
6ab9b2e15Sgtb  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*5e01956fSGlenn Barry  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
267c478bd9Sstevel@tonic-gate  * routine gss_canonicalize_name
277c478bd9Sstevel@tonic-gate  *
287c478bd9Sstevel@tonic-gate  * This routine is used to produce a mechanism specific
297c478bd9Sstevel@tonic-gate  * representation of name that has been previously
307c478bd9Sstevel@tonic-gate  * imported with gss_import_name.  The routine uses the mechanism
317c478bd9Sstevel@tonic-gate  * specific implementation of gss_import_name to implement this
327c478bd9Sstevel@tonic-gate  * function.
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * We allow a NULL output_name, in which case we modify the
357c478bd9Sstevel@tonic-gate  * input_name to include the mechanism specific name.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <mechglueP.h>
39*5e01956fSGlenn Barry #include "gssapiP_generic.h"
407c478bd9Sstevel@tonic-gate #ifdef HAVE_STDLIB_H
417c478bd9Sstevel@tonic-gate #include <stdlib.h>
427c478bd9Sstevel@tonic-gate #endif
437c478bd9Sstevel@tonic-gate #include <string.h>
447c478bd9Sstevel@tonic-gate #include <errno.h>
45*5e01956fSGlenn Barry #include <syslog.h>
467c478bd9Sstevel@tonic-gate 
47503a2b89SPeter Shoults static OM_uint32 val_canon_name_args(
48503a2b89SPeter Shoults 	OM_uint32 *minor_status,
49503a2b89SPeter Shoults 	const gss_name_t input_name,
50503a2b89SPeter Shoults 	const gss_OID mech_type,
51503a2b89SPeter Shoults 	gss_name_t *output_name)
52503a2b89SPeter Shoults {
53503a2b89SPeter Shoults 
54503a2b89SPeter Shoults 	/* Initialize outputs. */
55503a2b89SPeter Shoults 
56503a2b89SPeter Shoults 	if (minor_status != NULL)
57503a2b89SPeter Shoults 		*minor_status = 0;
58503a2b89SPeter Shoults 
59503a2b89SPeter Shoults 	if (output_name != NULL)
60503a2b89SPeter Shoults 		*output_name = GSS_C_NO_NAME;
61503a2b89SPeter Shoults 
62503a2b89SPeter Shoults 	/* Validate arguments. */
63503a2b89SPeter Shoults 
64503a2b89SPeter Shoults 	if (minor_status == NULL)
65503a2b89SPeter Shoults 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
66503a2b89SPeter Shoults 
67503a2b89SPeter Shoults 	if (input_name == GSS_C_NO_NAME || mech_type == GSS_C_NULL_OID)
68503a2b89SPeter Shoults 		return (GSS_S_CALL_INACCESSIBLE_READ);
69503a2b89SPeter Shoults 
70503a2b89SPeter Shoults 	return (GSS_S_COMPLETE);
71503a2b89SPeter Shoults }
72503a2b89SPeter Shoults 
737c478bd9Sstevel@tonic-gate OM_uint32
747c478bd9Sstevel@tonic-gate gss_canonicalize_name(minor_status,
757c478bd9Sstevel@tonic-gate 				input_name,
767c478bd9Sstevel@tonic-gate 				mech_type,
777c478bd9Sstevel@tonic-gate 				output_name)
787c478bd9Sstevel@tonic-gate OM_uint32 *minor_status;
797c478bd9Sstevel@tonic-gate const gss_name_t input_name;
807c478bd9Sstevel@tonic-gate const gss_OID mech_type;
817c478bd9Sstevel@tonic-gate gss_name_t *output_name;
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate 	gss_union_name_t in_union, out_union = NULL, dest_union = NULL;
847c478bd9Sstevel@tonic-gate 	OM_uint32 major_status = GSS_S_FAILURE;
85*5e01956fSGlenn Barry 	/* Solaris Kerberos - need to preserve more important minor_status */
86*5e01956fSGlenn Barry 	OM_uint32 tmp_status = 0;
877c478bd9Sstevel@tonic-gate 
88503a2b89SPeter Shoults 	major_status = val_canon_name_args(minor_status,
89503a2b89SPeter Shoults 					input_name,
90503a2b89SPeter Shoults 					mech_type,
91503a2b89SPeter Shoults 					output_name);
92503a2b89SPeter Shoults 	if (major_status != GSS_S_COMPLETE)
93503a2b89SPeter Shoults 		return (major_status);
947c478bd9Sstevel@tonic-gate 
95503a2b89SPeter Shoults 	/* Initial value needed below. */
96503a2b89SPeter Shoults 	major_status = GSS_S_FAILURE;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	in_union = (gss_union_name_t)input_name;
997c478bd9Sstevel@tonic-gate 	/*
1007c478bd9Sstevel@tonic-gate 	 * If the caller wants to reuse the name, and the name has already
1017c478bd9Sstevel@tonic-gate 	 * been converted, then there is nothing for us to do.
1027c478bd9Sstevel@tonic-gate 	 */
1037c478bd9Sstevel@tonic-gate 	if (!output_name && in_union->mech_type &&
1047c478bd9Sstevel@tonic-gate 		g_OID_equal(in_union->mech_type, mech_type))
1057c478bd9Sstevel@tonic-gate 		return (GSS_S_COMPLETE);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	/* ok, then we need to do something - start by creating data struct */
1087c478bd9Sstevel@tonic-gate 	if (output_name) {
1097c478bd9Sstevel@tonic-gate 		out_union =
1107c478bd9Sstevel@tonic-gate 			(gss_union_name_t)malloc(sizeof (gss_union_name_desc));
1117c478bd9Sstevel@tonic-gate 		if (!out_union)
1127c478bd9Sstevel@tonic-gate 			goto allocation_failure;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 		out_union->mech_type = 0;
1157c478bd9Sstevel@tonic-gate 		out_union->mech_name = 0;
1167c478bd9Sstevel@tonic-gate 		out_union->name_type = 0;
1177c478bd9Sstevel@tonic-gate 		out_union->external_name = 0;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 		/* Allocate the buffer for the user specified representation */
120ab9b2e15Sgtb 		if (gssint_create_copy_buffer(in_union->external_name,
1217c478bd9Sstevel@tonic-gate 				&out_union->external_name, 1))
1227c478bd9Sstevel@tonic-gate 			goto allocation_failure;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 		if (in_union->name_type != GSS_C_NULL_OID) {
125*5e01956fSGlenn Barry 			major_status = generic_gss_copy_oid(minor_status,
126*5e01956fSGlenn Barry 							in_union->name_type,
127*5e01956fSGlenn Barry 							&out_union->name_type);
128*5e01956fSGlenn Barry 			if (major_status) {
129*5e01956fSGlenn Barry 				map_errcode(minor_status);
1307c478bd9Sstevel@tonic-gate 				goto allocation_failure;
1317c478bd9Sstevel@tonic-gate 			}
132*5e01956fSGlenn Barry 		}
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	/*
1367c478bd9Sstevel@tonic-gate 	 * might need to delete any old mechanism names if we are
1377c478bd9Sstevel@tonic-gate 	 * reusing the buffer.
1387c478bd9Sstevel@tonic-gate 	 */
1397c478bd9Sstevel@tonic-gate 	if (!output_name) {
1407c478bd9Sstevel@tonic-gate 		if (in_union->mech_type) {
1417c478bd9Sstevel@tonic-gate 			(void) __gss_release_internal_name(minor_status,
1427c478bd9Sstevel@tonic-gate 							in_union->mech_type,
1437c478bd9Sstevel@tonic-gate 							&in_union->mech_name);
1447c478bd9Sstevel@tonic-gate 			(void) gss_release_oid(minor_status,
1457c478bd9Sstevel@tonic-gate 					    &in_union->mech_type);
1467c478bd9Sstevel@tonic-gate 			in_union->mech_type = 0;
1477c478bd9Sstevel@tonic-gate 		}
1487c478bd9Sstevel@tonic-gate 		dest_union = in_union;
1497c478bd9Sstevel@tonic-gate 	} else
1507c478bd9Sstevel@tonic-gate 		dest_union = out_union;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	/* now let's create the new mech name */
1537c478bd9Sstevel@tonic-gate 	if (major_status = generic_gss_copy_oid(minor_status, mech_type,
154*5e01956fSGlenn Barry 						&dest_union->mech_type)) {
155*5e01956fSGlenn Barry 		map_errcode(minor_status);
1567c478bd9Sstevel@tonic-gate 		goto allocation_failure;
157*5e01956fSGlenn Barry 	}
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	if (major_status =
1607c478bd9Sstevel@tonic-gate 		__gss_import_internal_name(minor_status, mech_type,
1617c478bd9Sstevel@tonic-gate 						dest_union,
1627c478bd9Sstevel@tonic-gate 					&dest_union->mech_name))
1637c478bd9Sstevel@tonic-gate 		goto allocation_failure;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	if (output_name)
1667c478bd9Sstevel@tonic-gate 		*output_name = (gss_name_t)dest_union;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	return (GSS_S_COMPLETE);
1697c478bd9Sstevel@tonic-gate 
170*5e01956fSGlenn Barry /* Solaris Kerberos - note some fails are not "allocation fails".  Sigh. */
1717c478bd9Sstevel@tonic-gate allocation_failure:
1727c478bd9Sstevel@tonic-gate 	/* do not delete the src name external name format */
1737c478bd9Sstevel@tonic-gate 	if (output_name) {
1747c478bd9Sstevel@tonic-gate 		if (out_union->external_name) {
1757c478bd9Sstevel@tonic-gate 			if (out_union->external_name->value)
1767c478bd9Sstevel@tonic-gate 				free(out_union->external_name->value);
1777c478bd9Sstevel@tonic-gate 			free(out_union->external_name);
1787c478bd9Sstevel@tonic-gate 		}
1797c478bd9Sstevel@tonic-gate 		if (out_union->name_type)
180*5e01956fSGlenn Barry 			(void) gss_release_oid(&tmp_status,
1817c478bd9Sstevel@tonic-gate 					    &out_union->name_type);
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 		dest_union = out_union;
1847c478bd9Sstevel@tonic-gate 	} else
1857c478bd9Sstevel@tonic-gate 		dest_union = in_union;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	/*
1887c478bd9Sstevel@tonic-gate 	 * delete the partially created mech specific name
1897c478bd9Sstevel@tonic-gate 	 * applies for both src and dest which ever is being used for output
1907c478bd9Sstevel@tonic-gate 	 */
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	if (dest_union->mech_name) {
193*5e01956fSGlenn Barry 		(void) __gss_release_internal_name(&tmp_status,
1947c478bd9Sstevel@tonic-gate 						dest_union->mech_type,
1957c478bd9Sstevel@tonic-gate 						&dest_union->mech_name);
1967c478bd9Sstevel@tonic-gate 	}
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	if (dest_union->mech_type)
199*5e01956fSGlenn Barry 		(void) gss_release_oid(&tmp_status, &dest_union->mech_type);
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	if (output_name)
2037c478bd9Sstevel@tonic-gate 		free(out_union);
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	return (major_status);
2067c478bd9Sstevel@tonic-gate } /**********  gss_canonicalize_name ********/
207