1226031Sstas/*-
2226031Sstas * Copyright (c) 2005 Doug Rabson
3226031Sstas * All rights reserved.
4226031Sstas *
5226031Sstas * Redistribution and use in source and binary forms, with or without
6226031Sstas * modification, are permitted provided that the following conditions
7226031Sstas * are met:
8226031Sstas * 1. Redistributions of source code must retain the above copyright
9226031Sstas *    notice, this list of conditions and the following disclaimer.
10226031Sstas * 2. Redistributions in binary form must reproduce the above copyright
11226031Sstas *    notice, this list of conditions and the following disclaimer in the
12226031Sstas *    documentation and/or other materials provided with the distribution.
13226031Sstas *
14226031Sstas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15226031Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16226031Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17226031Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18226031Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19226031Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20226031Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21226031Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22226031Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23226031Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24226031Sstas * SUCH DAMAGE.
25226031Sstas *
26226031Sstas *	$FreeBSD: src/lib/libgssapi/gss_names.c,v 1.1 2005/12/29 14:40:20 dfr Exp $
27226031Sstas */
28226031Sstas
29226031Sstas#include "mech_locl.h"
30226031Sstas
31226031SstasOM_uint32
32226031Sstas_gss_find_mn(OM_uint32 *minor_status, struct _gss_name *name, gss_OID mech,
33226031Sstas	     struct _gss_mechanism_name **output_mn)
34226031Sstas{
35226031Sstas	OM_uint32 major_status;
36226031Sstas	gssapi_mech_interface m;
37226031Sstas	struct _gss_mechanism_name *mn;
38226031Sstas
39226031Sstas	*output_mn = NULL;
40226031Sstas
41226031Sstas	HEIM_SLIST_FOREACH(mn, &name->gn_mn, gmn_link) {
42226031Sstas		if (gss_oid_equal(mech, mn->gmn_mech_oid))
43226031Sstas			break;
44226031Sstas	}
45226031Sstas
46226031Sstas	if (!mn) {
47226031Sstas		/*
48226031Sstas		 * If this name is canonical (i.e. there is only an
49226031Sstas		 * MN but it is from a different mech), give up now.
50226031Sstas		 */
51226031Sstas		if (!name->gn_value.value)
52226031Sstas			return GSS_S_BAD_NAME;
53226031Sstas
54226031Sstas		m = __gss_get_mechanism(mech);
55226031Sstas		if (!m)
56226031Sstas			return (GSS_S_BAD_MECH);
57226031Sstas
58226031Sstas		mn = malloc(sizeof(struct _gss_mechanism_name));
59226031Sstas		if (!mn)
60226031Sstas			return GSS_S_FAILURE;
61226031Sstas
62226031Sstas		major_status = m->gm_import_name(minor_status,
63226031Sstas		    &name->gn_value,
64226031Sstas		    (name->gn_type.elements
65226031Sstas			? &name->gn_type : GSS_C_NO_OID),
66226031Sstas		    &mn->gmn_name);
67226031Sstas		if (major_status != GSS_S_COMPLETE) {
68226031Sstas			_gss_mg_error(m, major_status, *minor_status);
69226031Sstas			free(mn);
70226031Sstas			return major_status;
71226031Sstas		}
72226031Sstas
73226031Sstas		mn->gmn_mech = m;
74226031Sstas		mn->gmn_mech_oid = &m->gm_mech_oid;
75226031Sstas		HEIM_SLIST_INSERT_HEAD(&name->gn_mn, mn, gmn_link);
76226031Sstas	}
77226031Sstas	*output_mn = mn;
78226031Sstas	return 0;
79226031Sstas}
80226031Sstas
81226031Sstas
82226031Sstas/*
83226031Sstas * Make a name from an MN.
84226031Sstas */
85226031Sstasstruct _gss_name *
86226031Sstas_gss_make_name(gssapi_mech_interface m, gss_name_t new_mn)
87226031Sstas{
88226031Sstas	struct _gss_name *name;
89226031Sstas	struct _gss_mechanism_name *mn;
90226031Sstas
91226031Sstas	name = malloc(sizeof(struct _gss_name));
92226031Sstas	if (!name)
93226031Sstas		return (0);
94226031Sstas	memset(name, 0, sizeof(struct _gss_name));
95226031Sstas
96226031Sstas	mn = malloc(sizeof(struct _gss_mechanism_name));
97226031Sstas	if (!mn) {
98226031Sstas		free(name);
99226031Sstas		return (0);
100226031Sstas	}
101226031Sstas
102226031Sstas	HEIM_SLIST_INIT(&name->gn_mn);
103226031Sstas	mn->gmn_mech = m;
104226031Sstas	mn->gmn_mech_oid = &m->gm_mech_oid;
105226031Sstas	mn->gmn_name = new_mn;
106226031Sstas	HEIM_SLIST_INSERT_HEAD(&name->gn_mn, mn, gmn_link);
107226031Sstas
108226031Sstas	return (name);
109226031Sstas}
110226031Sstas
111