1/*-
2 * Copyright (c) 2005 Doug Rabson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 *	$FreeBSD$
27 */
28
29#include <gssapi/gssapi.h>
30#include <stdlib.h>
31#include <string.h>
32#include <errno.h>
33
34#include "mech_switch.h"
35#include "name.h"
36#include "utils.h"
37
38/*
39 * The implementation must reserve static storage for a
40 * gss_OID_desc object containing the value
41 * {10, (void *)"\x2a\x86\x48\x86\xf7\x12"
42 * "\x01\x02\x01\x01"},
43 * corresponding to an object-identifier value of
44 * {iso(1) member-body(2) United States(840) mit(113554)
45 * infosys(1) gssapi(2) generic(1) user_name(1)}.  The constant
46 * GSS_C_NT_USER_NAME should be initialized to point
47 * to that gss_OID_desc.
48 */
49static gss_OID_desc GSS_C_NT_USER_NAME_storage =
50	{10, (void *)(uintptr_t)"\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x01"};
51gss_OID GSS_C_NT_USER_NAME = &GSS_C_NT_USER_NAME_storage;
52
53/*
54 * The implementation must reserve static storage for a
55 * gss_OID_desc object containing the value
56 * {10, (void *)"\x2a\x86\x48\x86\xf7\x12"
57 *              "\x01\x02\x01\x02"},
58 * corresponding to an object-identifier value of
59 * {iso(1) member-body(2) United States(840) mit(113554)
60 * infosys(1) gssapi(2) generic(1) machine_uid_name(2)}.
61 * The constant GSS_C_NT_MACHINE_UID_NAME should be
62 * initialized to point to that gss_OID_desc.
63 */
64static gss_OID_desc GSS_C_NT_MACHINE_UID_NAME_storage =
65	{10, (void *)(uintptr_t)"\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x02"};
66gss_OID GSS_C_NT_MACHINE_UID_NAME = &GSS_C_NT_MACHINE_UID_NAME_storage;
67
68/*
69 * The implementation must reserve static storage for a
70 * gss_OID_desc object containing the value
71 * {10, (void *)"\x2a\x86\x48\x86\xf7\x12"
72 *              "\x01\x02\x01\x03"},
73 * corresponding to an object-identifier value of
74 * {iso(1) member-body(2) United States(840) mit(113554)
75 * infosys(1) gssapi(2) generic(1) string_uid_name(3)}.
76 * The constant GSS_C_NT_STRING_UID_NAME should be
77 * initialized to point to that gss_OID_desc.
78 */
79static gss_OID_desc GSS_C_NT_STRING_UID_NAME_storage =
80	{10, (void *)(uintptr_t)"\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x03"};
81gss_OID GSS_C_NT_STRING_UID_NAME = &GSS_C_NT_STRING_UID_NAME_storage;
82
83/*
84 * The implementation must reserve static storage for a
85 * gss_OID_desc object containing the value
86 * {6, (void *)"\x2b\x06\x01\x05\x06\x02"},
87 * corresponding to an object-identifier value of
88 * {iso(1) org(3) dod(6) internet(1) security(5)
89 * nametypes(6) gss-host-based-services(2)).  The constant
90 * GSS_C_NT_HOSTBASED_SERVICE_X should be initialized to point
91 * to that gss_OID_desc.  This is a deprecated OID value, and
92 * implementations wishing to support hostbased-service names
93 * should instead use the GSS_C_NT_HOSTBASED_SERVICE OID,
94 * defined below, to identify such names;
95 * GSS_C_NT_HOSTBASED_SERVICE_X should be accepted a synonym
96 * for GSS_C_NT_HOSTBASED_SERVICE when presented as an input
97 * parameter, but should not be emitted by GSS-API
98 * implementations
99 */
100static gss_OID_desc GSS_C_NT_HOSTBASED_SERVICE_X_storage =
101	{6, (void *)(uintptr_t)"\x2b\x06\x01\x05\x06\x02"};
102gss_OID GSS_C_NT_HOSTBASED_SERVICE_X = &GSS_C_NT_HOSTBASED_SERVICE_X_storage;
103
104/*
105 * The implementation must reserve static storage for a
106 * gss_OID_desc object containing the value
107 * {10, (void *)"\x2a\x86\x48\x86\xf7\x12"
108 *              "\x01\x02\x01\x04"}, corresponding to an
109 * object-identifier value of {iso(1) member-body(2)
110 * Unites States(840) mit(113554) infosys(1) gssapi(2)
111 * generic(1) service_name(4)}.  The constant
112 * GSS_C_NT_HOSTBASED_SERVICE should be initialized
113 * to point to that gss_OID_desc.
114 */
115static gss_OID_desc GSS_C_NT_HOSTBASED_SERVICE_storage =
116	{10, (void *)(uintptr_t)"\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04"};
117gss_OID GSS_C_NT_HOSTBASED_SERVICE = &GSS_C_NT_HOSTBASED_SERVICE_storage;
118
119/*
120 * The implementation must reserve static storage for a
121 * gss_OID_desc object containing the value
122 * {6, (void *)"\x2b\x06\01\x05\x06\x03"},
123 * corresponding to an object identifier value of
124 * {1(iso), 3(org), 6(dod), 1(internet), 5(security),
125 * 6(nametypes), 3(gss-anonymous-name)}.  The constant
126 * and GSS_C_NT_ANONYMOUS should be initialized to point
127 * to that gss_OID_desc.
128 */
129static gss_OID_desc GSS_C_NT_ANONYMOUS_storage =
130	{6, (void *)(uintptr_t)"\x2b\x06\01\x05\x06\x03"};
131gss_OID GSS_C_NT_ANONYMOUS = &GSS_C_NT_ANONYMOUS_storage;
132
133/*
134 * The implementation must reserve static storage for a
135 * gss_OID_desc object containing the value
136 * {6, (void *)"\x2b\x06\x01\x05\x06\x04"},
137 * corresponding to an object-identifier value of
138 * {1(iso), 3(org), 6(dod), 1(internet), 5(security),
139 * 6(nametypes), 4(gss-api-exported-name)}.  The constant
140 * GSS_C_NT_EXPORT_NAME should be initialized to point
141 * to that gss_OID_desc.
142 */
143static gss_OID_desc GSS_C_NT_EXPORT_NAME_storage =
144	{6, (void *)(uintptr_t)"\x2b\x06\x01\x05\x06\x04"};
145gss_OID GSS_C_NT_EXPORT_NAME = &GSS_C_NT_EXPORT_NAME_storage;
146
147/*
148 *   This name form shall be represented by the Object Identifier {iso(1)
149 *   member-body(2) United States(840) mit(113554) infosys(1) gssapi(2)
150 *   krb5(2) krb5_name(1)}.  The recommended symbolic name for this type
151 *   is "GSS_KRB5_NT_PRINCIPAL_NAME".
152 */
153static gss_OID_desc GSS_KRB5_NT_PRINCIPAL_NAME_storage =
154        {10, (void *)(uintptr_t)"\x2a\x86\x48\x86\xf7\x12\x01\x02\x02\x01"};
155gss_OID GSS_KRB5_NT_PRINCIPAL_NAME = &GSS_KRB5_NT_PRINCIPAL_NAME_storage;
156
157/*
158 * This name form shall be represented by the Object Identifier {iso(1)
159 * member-body(2) United States(840) mit(113554) infosys(1) gssapi(2)
160 * generic(1) user_name(1)}.  The recommended symbolic name for this
161 * type is "GSS_KRB5_NT_USER_NAME".
162 */
163gss_OID GSS_KRB5_NT_USER_NAME = &GSS_C_NT_USER_NAME_storage;
164
165/*
166 * This name form shall be represented by the Object Identifier {iso(1)
167 * member-body(2) United States(840) mit(113554) infosys(1) gssapi(2)
168 * generic(1) machine_uid_name(2)}.  The recommended symbolic name for
169 * this type is "GSS_KRB5_NT_MACHINE_UID_NAME".
170 */
171gss_OID GSS_KRB5_NT_MACHINE_UID_NAME = &GSS_C_NT_MACHINE_UID_NAME_storage;
172
173/*
174 * This name form shall be represented by the Object Identifier {iso(1)
175 * member-body(2) United States(840) mit(113554) infosys(1) gssapi(2)
176 * generic(1) string_uid_name(3)}.  The recommended symbolic name for
177 * this type is "GSS_KRB5_NT_STRING_UID_NAME".
178 */
179gss_OID GSS_KRB5_NT_STRING_UID_NAME = &GSS_C_NT_STRING_UID_NAME_storage;
180
181OM_uint32
182_gss_find_mn(OM_uint32 *minor_status, struct _gss_name *name, gss_OID mech,
183	     struct _gss_mechanism_name **output_mn)
184{
185	OM_uint32 major_status;
186	struct _gss_mech_switch *m;
187	struct _gss_mechanism_name *mn;
188
189	*output_mn = NULL;
190
191	SLIST_FOREACH(mn, &name->gn_mn, gmn_link) {
192		if (gss_oid_equal(mech, mn->gmn_mech_oid))
193			break;
194	}
195
196	if (!mn) {
197		/*
198		 * If this name is canonical (i.e. there is only an
199		 * MN but it is from a different mech), give up now.
200		 */
201		if (!name->gn_value.value)
202			return (GSS_S_BAD_NAME);
203
204		m = _gss_find_mech_switch(mech);
205		if (!m)
206			return (GSS_S_BAD_MECH);
207
208		mn = malloc(sizeof(struct _gss_mechanism_name));
209		if (!mn)
210			return (GSS_S_FAILURE);
211
212		major_status = m->gm_import_name(minor_status,
213		    &name->gn_value,
214		    (name->gn_type.elements
215			? &name->gn_type : GSS_C_NO_OID),
216		    &mn->gmn_name);
217		if (major_status != GSS_S_COMPLETE) {
218			_gss_mg_error(m, major_status, *minor_status);
219			free(mn);
220			return (major_status);
221		}
222
223		mn->gmn_mech = m;
224		mn->gmn_mech_oid = &m->gm_mech_oid;
225		SLIST_INSERT_HEAD(&name->gn_mn, mn, gmn_link);
226	}
227	*output_mn = mn;
228	return (GSS_S_COMPLETE);
229}
230
231
232/*
233 * Make a name from an MN.
234 */
235struct _gss_name *
236_gss_make_name(struct _gss_mech_switch *m, gss_name_t new_mn)
237{
238	struct _gss_name *name;
239	struct _gss_mechanism_name *mn;
240
241	name = malloc(sizeof(struct _gss_name));
242	if (!name)
243		return (0);
244	memset(name, 0, sizeof(struct _gss_name));
245
246	mn = malloc(sizeof(struct _gss_mechanism_name));
247	if (!mn) {
248		free(name);
249		return (0);
250	}
251
252	SLIST_INIT(&name->gn_mn);
253	mn->gmn_mech = m;
254	mn->gmn_mech_oid = &m->gm_mech_oid;
255	mn->gmn_name = new_mn;
256	SLIST_INSERT_HEAD(&name->gn_mn, mn, gmn_link);
257
258	return (name);
259}
260
261