gss_mech_switch.c revision 1.1.1.1
1/*	$NetBSD: gss_mech_switch.c,v 1.1.1.1 2011/04/13 18:14:46 elric Exp $	*/
2
3/*-
4 * Copyright (c) 2005 Doug Rabson
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 *	$FreeBSD: src/lib/libgssapi/gss_mech_switch.c,v 1.2 2006/02/04 09:40:21 dfr Exp $
29 */
30
31#include "mech_locl.h"
32#include <heim_threads.h>
33
34#ifndef _PATH_GSS_MECH
35#define _PATH_GSS_MECH	"/etc/gss/mech"
36#endif
37
38struct _gss_mech_switch_list _gss_mechs = { NULL } ;
39gss_OID_set _gss_mech_oids;
40static HEIMDAL_MUTEX _gss_mech_mutex = HEIMDAL_MUTEX_INITIALIZER;
41
42/*
43 * Convert a string containing an OID in 'dot' form
44 * (e.g. 1.2.840.113554.1.2.2) to a gss_OID.
45 */
46static int
47_gss_string_to_oid(const char* s, gss_OID oid)
48{
49	int			number_count, i, j;
50	size_t			byte_count;
51	const char		*p, *q;
52	char			*res;
53
54	oid->length = 0;
55	oid->elements = NULL;
56
57	/*
58	 * First figure out how many numbers in the oid, then
59	 * calculate the compiled oid size.
60	 */
61	number_count = 0;
62	for (p = s; p; p = q) {
63		q = strchr(p, '.');
64		if (q) q = q + 1;
65		number_count++;
66	}
67
68	/*
69	 * The first two numbers are in the first byte and each
70	 * subsequent number is encoded in a variable byte sequence.
71	 */
72	if (number_count < 2)
73		return (EINVAL);
74
75	/*
76	 * We do this in two passes. The first pass, we just figure
77	 * out the size. Second time around, we actually encode the
78	 * number.
79	 */
80	res = 0;
81	for (i = 0; i < 2; i++) {
82		byte_count = 0;
83		for (p = s, j = 0; p; p = q, j++) {
84			unsigned int number = 0;
85
86			/*
87			 * Find the end of this number.
88			 */
89			q = strchr(p, '.');
90			if (q) q = q + 1;
91
92			/*
93			 * Read the number of of the string. Don't
94			 * bother with anything except base ten.
95			 */
96			while (*p && *p != '.') {
97				number = 10 * number + (*p - '0');
98				p++;
99			}
100
101			/*
102			 * Encode the number. The first two numbers
103			 * are packed into the first byte. Subsequent
104			 * numbers are encoded in bytes seven bits at
105			 * a time with the last byte having the high
106			 * bit set.
107			 */
108			if (j == 0) {
109				if (res)
110					*res = number * 40;
111			} else if (j == 1) {
112				if (res) {
113					*res += number;
114					res++;
115				}
116				byte_count++;
117			} else if (j >= 2) {
118				/*
119				 * The number is encoded in seven bit chunks.
120				 */
121				unsigned int t;
122				unsigned int bytes;
123
124				bytes = 0;
125				for (t = number; t; t >>= 7)
126					bytes++;
127				if (bytes == 0) bytes = 1;
128				while (bytes) {
129					if (res) {
130						int bit = 7*(bytes-1);
131
132						*res = (number >> bit) & 0x7f;
133						if (bytes != 1)
134							*res |= 0x80;
135						res++;
136					}
137					byte_count++;
138					bytes--;
139				}
140			}
141		}
142		if (!res) {
143			res = malloc(byte_count);
144			if (!res)
145				return (ENOMEM);
146			oid->length = byte_count;
147			oid->elements = res;
148		}
149	}
150
151	return (0);
152}
153
154#define SYM(name)							\
155do {									\
156	m->gm_mech.gm_ ## name = dlsym(so, "gss_" #name);		\
157	if (!m->gm_mech.gm_ ## name) {					\
158		fprintf(stderr, "can't find symbol gss_" #name "\n");	\
159		goto bad;						\
160	}								\
161} while (0)
162
163#define OPTSYM(name)							\
164do {									\
165	m->gm_mech.gm_ ## name = dlsym(so, "gss_" #name);			\
166} while (0)
167
168/*
169 *
170 */
171static int
172add_builtin(gssapi_mech_interface mech)
173{
174    struct _gss_mech_switch *m;
175    OM_uint32 minor_status;
176
177    /* not registering any mech is ok */
178    if (mech == NULL)
179	return 0;
180
181    m = calloc(1, sizeof(*m));
182    if (m == NULL)
183	return ENOMEM;
184    m->gm_so = NULL;
185    m->gm_mech = *mech;
186    m->gm_mech_oid = mech->gm_mech_oid; /* XXX */
187    gss_add_oid_set_member(&minor_status,
188			   &m->gm_mech.gm_mech_oid, &_gss_mech_oids);
189
190    /* pick up the oid sets of names */
191
192    if (m->gm_mech.gm_inquire_names_for_mech)
193	(*m->gm_mech.gm_inquire_names_for_mech)(&minor_status,
194	    &m->gm_mech.gm_mech_oid, &m->gm_name_types);
195
196    if (m->gm_name_types == NULL)
197	gss_create_empty_oid_set(&minor_status, &m->gm_name_types);
198
199    HEIM_SLIST_INSERT_HEAD(&_gss_mechs, m, gm_link);
200    return 0;
201}
202
203/*
204 * Load the mechanisms file (/etc/gss/mech).
205 */
206void
207_gss_load_mech(void)
208{
209	OM_uint32	major_status, minor_status;
210	FILE		*fp;
211	char		buf[256];
212	char		*p;
213	char		*name, *oid, *lib, *kobj;
214	struct _gss_mech_switch *m;
215	void		*so;
216	gss_OID_desc	mech_oid;
217	int		found;
218
219
220	HEIMDAL_MUTEX_lock(&_gss_mech_mutex);
221
222	if (HEIM_SLIST_FIRST(&_gss_mechs)) {
223		HEIMDAL_MUTEX_unlock(&_gss_mech_mutex);
224		return;
225	}
226
227	major_status = gss_create_empty_oid_set(&minor_status,
228	    &_gss_mech_oids);
229	if (major_status) {
230		HEIMDAL_MUTEX_unlock(&_gss_mech_mutex);
231		return;
232	}
233
234	add_builtin(__gss_krb5_initialize());
235	add_builtin(__gss_spnego_initialize());
236	add_builtin(__gss_ntlm_initialize());
237
238#ifdef HAVE_DLOPEN
239	fp = fopen(_PATH_GSS_MECH, "r");
240	if (!fp) {
241		HEIMDAL_MUTEX_unlock(&_gss_mech_mutex);
242		return;
243	}
244	rk_cloexec_file(fp);
245
246	while (fgets(buf, sizeof(buf), fp)) {
247		_gss_mo_init *mi;
248
249		if (*buf == '#')
250			continue;
251		p = buf;
252		name = strsep(&p, "\t\n ");
253		if (p) while (isspace((unsigned char)*p)) p++;
254		oid = strsep(&p, "\t\n ");
255		if (p) while (isspace((unsigned char)*p)) p++;
256		lib = strsep(&p, "\t\n ");
257		if (p) while (isspace((unsigned char)*p)) p++;
258		kobj = strsep(&p, "\t\n ");
259		if (!name || !oid || !lib || !kobj)
260			continue;
261
262		if (_gss_string_to_oid(oid, &mech_oid))
263			continue;
264
265		/*
266		 * Check for duplicates, already loaded mechs.
267		 */
268		found = 0;
269		HEIM_SLIST_FOREACH(m, &_gss_mechs, gm_link) {
270			if (gss_oid_equal(&m->gm_mech.gm_mech_oid, &mech_oid)) {
271				found = 1;
272				free(mech_oid.elements);
273				break;
274			}
275		}
276		if (found)
277			continue;
278
279#ifndef RTLD_LOCAL
280#define RTLD_LOCAL 0
281#endif
282
283#ifndef RTLD_GROUP
284#define RTLD_GROUP 0
285#endif
286
287		so = dlopen(lib, RTLD_LAZY | RTLD_LOCAL | RTLD_GROUP);
288		if (!so) {
289/*			fprintf(stderr, "dlopen: %s\n", dlerror()); */
290			free(mech_oid.elements);
291			continue;
292		}
293
294		m = malloc(sizeof(*m));
295		if (!m) {
296			free(mech_oid.elements);
297			break;
298		}
299		m->gm_so = so;
300		m->gm_mech.gm_mech_oid = mech_oid;
301		m->gm_mech.gm_flags = 0;
302
303		major_status = gss_add_oid_set_member(&minor_status,
304		    &m->gm_mech.gm_mech_oid, &_gss_mech_oids);
305		if (major_status) {
306			free(m->gm_mech.gm_mech_oid.elements);
307			free(m);
308			continue;
309		}
310
311		SYM(acquire_cred);
312		SYM(release_cred);
313		SYM(init_sec_context);
314		SYM(accept_sec_context);
315		SYM(process_context_token);
316		SYM(delete_sec_context);
317		SYM(context_time);
318		SYM(get_mic);
319		SYM(verify_mic);
320		SYM(wrap);
321		SYM(unwrap);
322		SYM(display_status);
323		SYM(indicate_mechs);
324		SYM(compare_name);
325		SYM(display_name);
326		SYM(import_name);
327		SYM(export_name);
328		SYM(release_name);
329		SYM(inquire_cred);
330		SYM(inquire_context);
331		SYM(wrap_size_limit);
332		SYM(add_cred);
333		SYM(inquire_cred_by_mech);
334		SYM(export_sec_context);
335		SYM(import_sec_context);
336		SYM(inquire_names_for_mech);
337		SYM(inquire_mechs_for_name);
338		SYM(canonicalize_name);
339		SYM(duplicate_name);
340		OPTSYM(inquire_cred_by_oid);
341		OPTSYM(inquire_sec_context_by_oid);
342		OPTSYM(set_sec_context_option);
343		OPTSYM(set_cred_option);
344		OPTSYM(pseudo_random);
345		OPTSYM(wrap_iov);
346		OPTSYM(unwrap_iov);
347		OPTSYM(wrap_iov_length);
348		OPTSYM(display_name_ext);
349		OPTSYM(inquire_name);
350		OPTSYM(get_name_attribute);
351		OPTSYM(set_name_attribute);
352		OPTSYM(delete_name_attribute);
353		OPTSYM(export_name_composite);
354
355		mi = dlsym(so, "gss_mo_init");
356		if (mi != NULL) {
357			major_status = mi(&minor_status,
358					  &mech_oid,
359					  &m->gm_mech.gm_mo,
360					  &m->gm_mech.gm_mo_num);
361			if (GSS_ERROR(major_status))
362				goto bad;
363		}
364
365		HEIM_SLIST_INSERT_HEAD(&_gss_mechs, m, gm_link);
366		continue;
367
368	bad:
369		free(m->gm_mech.gm_mech_oid.elements);
370		free(m);
371		dlclose(so);
372		continue;
373	}
374	fclose(fp);
375#endif
376	HEIMDAL_MUTEX_unlock(&_gss_mech_mutex);
377}
378
379gssapi_mech_interface
380__gss_get_mechanism(gss_const_OID mech)
381{
382        struct _gss_mech_switch	*m;
383
384	_gss_load_mech();
385	HEIM_SLIST_FOREACH(m, &_gss_mechs, gm_link) {
386		if (gss_oid_equal(&m->gm_mech.gm_mech_oid, mech))
387			return &m->gm_mech;
388	}
389	return NULL;
390}
391