gss_mech_switch.c revision 233294
1867Sache/*-
2867Sache * Copyright (c) 2005 Doug Rabson
3867Sache * All rights reserved.
4867Sache *
5867Sache * Redistribution and use in source and binary forms, with or without
6867Sache * modification, are permitted provided that the following conditions
7867Sache * are met:
8867Sache * 1. Redistributions of source code must retain the above copyright
9867Sache *    notice, this list of conditions and the following disclaimer.
10867Sache * 2. Redistributions in binary form must reproduce the above copyright
11867Sache *    notice, this list of conditions and the following disclaimer in the
12867Sache *    documentation and/or other materials provided with the distribution.
13867Sache *
14867Sache * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15867Sache * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16867Sache * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17867Sache * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18867Sache * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19867Sache * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20867Sache * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21867Sache * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22867Sache * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23867Sache * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24867Sache * SUCH DAMAGE.
25867Sache *
26867Sache *	$FreeBSD: src/lib/libgssapi/gss_mech_switch.c,v 1.2 2006/02/04 09:40:21 dfr Exp $
27867Sache */
28867Sache
29867Sache#include "mech_locl.h"
30867Sache#include <heim_threads.h>
31867Sache
32867Sache#ifndef _PATH_GSS_MECH
33867Sache#define _PATH_GSS_MECH	"/etc/gss/mech"
34867Sache#endif
35867Sache
36867Sachestruct _gss_mech_switch_list _gss_mechs = { NULL } ;
37867Sachegss_OID_set _gss_mech_oids;
38867Sachestatic HEIMDAL_MUTEX _gss_mech_mutex = HEIMDAL_MUTEX_INITIALIZER;
39867Sache
40867Sache/*
41867Sache * Convert a string containing an OID in 'dot' form
42867Sache * (e.g. 1.2.840.113554.1.2.2) to a gss_OID.
43867Sache */
44867Sachestatic int
45867Sache_gss_string_to_oid(const char* s, gss_OID oid)
46867Sache{
47867Sache	int			number_count, i, j;
48867Sache	size_t			byte_count;
49867Sache	const char		*p, *q;
50867Sache	char			*res;
51867Sache
52867Sache	oid->length = 0;
53867Sache	oid->elements = NULL;
54867Sache
55867Sache	/*
56867Sache	 * First figure out how many numbers in the oid, then
57867Sache	 * calculate the compiled oid size.
58867Sache	 */
59867Sache	number_count = 0;
60867Sache	for (p = s; p; p = q) {
61867Sache		q = strchr(p, '.');
62867Sache		if (q) q = q + 1;
63867Sache		number_count++;
64867Sache	}
65867Sache
66867Sache	/*
67867Sache	 * The first two numbers are in the first byte and each
68867Sache	 * subsequent number is encoded in a variable byte sequence.
69867Sache	 */
70867Sache	if (number_count < 2)
71867Sache		return (EINVAL);
72867Sache
73867Sache	/*
74867Sache	 * We do this in two passes. The first pass, we just figure
75867Sache	 * out the size. Second time around, we actually encode the
76867Sache	 * number.
77867Sache	 */
78867Sache	res = 0;
79867Sache	for (i = 0; i < 2; i++) {
80867Sache		byte_count = 0;
81867Sache		for (p = s, j = 0; p; p = q, j++) {
82867Sache			unsigned int number = 0;
83867Sache
84867Sache			/*
85867Sache			 * Find the end of this number.
86867Sache			 */
87867Sache			q = strchr(p, '.');
88867Sache			if (q) q = q + 1;
89867Sache
90867Sache			/*
91867Sache			 * Read the number of of the string. Don't
92867Sache			 * bother with anything except base ten.
93867Sache			 */
94867Sache			while (*p && *p != '.') {
95867Sache				number = 10 * number + (*p - '0');
96867Sache				p++;
97867Sache			}
98867Sache
99867Sache			/*
100867Sache			 * Encode the number. The first two numbers
101867Sache			 * are packed into the first byte. Subsequent
102867Sache			 * numbers are encoded in bytes seven bits at
103867Sache			 * a time with the last byte having the high
104867Sache			 * bit set.
105867Sache			 */
106867Sache			if (j == 0) {
107867Sache				if (res)
108867Sache					*res = number * 40;
109867Sache			} else if (j == 1) {
110867Sache				if (res) {
111867Sache					*res += number;
112867Sache					res++;
113867Sache				}
114867Sache				byte_count++;
115867Sache			} else if (j >= 2) {
116867Sache				/*
117867Sache				 * The number is encoded in seven bit chunks.
118867Sache				 */
119867Sache				unsigned int t;
120867Sache				unsigned int bytes;
121867Sache
122867Sache				bytes = 0;
123867Sache				for (t = number; t; t >>= 7)
124867Sache					bytes++;
125867Sache				if (bytes == 0) bytes = 1;
126867Sache				while (bytes) {
127867Sache					if (res) {
128867Sache						int bit = 7*(bytes-1);
129867Sache
130867Sache						*res = (number >> bit) & 0x7f;
131867Sache						if (bytes != 1)
132867Sache							*res |= 0x80;
133867Sache						res++;
134867Sache					}
135867Sache					byte_count++;
136867Sache					bytes--;
137867Sache				}
138867Sache			}
139867Sache		}
140867Sache		if (!res) {
141867Sache			res = malloc(byte_count);
142867Sache			if (!res)
143867Sache				return (ENOMEM);
144867Sache			oid->length = byte_count;
145867Sache			oid->elements = res;
146867Sache		}
147867Sache	}
148867Sache
149867Sache	return (0);
150867Sache}
151867Sache
152867Sache#define SYM(name)							\
153867Sachedo {									\
154867Sache	m->gm_mech.gm_ ## name = dlsym(so, "gss_" #name);		\
155867Sache	if (!m->gm_mech.gm_ ## name ||					\
156867Sache	    m->gm_mech.gm_ ##name == gss_ ## name) {			\
157867Sache		fprintf(stderr, "can't find symbol gss_" #name "\n");	\
158867Sache		goto bad;						\
159867Sache	}								\
160867Sache} while (0)
161867Sache
162867Sache#define OPTSYM(name)							\
163867Sachedo {									\
164867Sache	m->gm_mech.gm_ ## name = dlsym(so, "gss_" #name);		\
165867Sache	if (m->gm_mech.gm_ ## name == gss_ ## name)			\
166867Sache		m->gm_mech.gm_ ## name = NULL;				\
167867Sache} while (0)
168867Sache
169867Sache#define OPTSPISYM(name)							\
170867Sachedo {									\
171867Sache	m->gm_mech.gm_ ## name = dlsym(so, "gssspi_" #name);		\
172867Sache} while (0)
173867Sache
174867Sache#define COMPATSYM(name)							\
175867Sachedo {									\
176867Sache	m->gm_mech.gm_compat->gmc_ ## name = dlsym(so, "gss_" #name);	\
177867Sache	if (m->gm_mech.gm_compat->gmc_ ## name == gss_ ## name)		\
178867Sache		m->gm_mech.gm_compat->gmc_ ## name = NULL;		\
179867Sache} while (0)
180867Sache
181#define COMPATSPISYM(name)						\
182do {									\
183	m->gm_mech.gm_compat->gmc_ ## name = dlsym(so, "gssspi_" #name);\
184	if (m->gm_mech.gm_compat->gmc_ ## name == gss_ ## name)		\
185		m->gm_mech.gm_compat->gmc_ ## name = NULL;		\
186} while (0)
187
188/*
189 *
190 */
191static int
192add_builtin(gssapi_mech_interface mech)
193{
194    struct _gss_mech_switch *m;
195    OM_uint32 minor_status;
196
197    /* not registering any mech is ok */
198    if (mech == NULL)
199	return 0;
200
201    m = calloc(1, sizeof(*m));
202    if (m == NULL)
203	return ENOMEM;
204    m->gm_so = NULL;
205    m->gm_mech = *mech;
206    m->gm_mech_oid = mech->gm_mech_oid; /* XXX */
207    gss_add_oid_set_member(&minor_status,
208			   &m->gm_mech.gm_mech_oid, &_gss_mech_oids);
209
210    /* pick up the oid sets of names */
211
212    if (m->gm_mech.gm_inquire_names_for_mech)
213	(*m->gm_mech.gm_inquire_names_for_mech)(&minor_status,
214	    &m->gm_mech.gm_mech_oid, &m->gm_name_types);
215
216    if (m->gm_name_types == NULL)
217	gss_create_empty_oid_set(&minor_status, &m->gm_name_types);
218
219    HEIM_SLIST_INSERT_HEAD(&_gss_mechs, m, gm_link);
220    return 0;
221}
222
223/*
224 * Load the mechanisms file (/etc/gss/mech).
225 */
226void
227_gss_load_mech(void)
228{
229	OM_uint32	major_status, minor_status;
230	FILE		*fp;
231	char		buf[256];
232	char		*p;
233	char		*name, *oid, *lib, *kobj;
234	struct _gss_mech_switch *m;
235	void		*so;
236	gss_OID_desc	mech_oid;
237	int		found;
238
239
240	HEIMDAL_MUTEX_lock(&_gss_mech_mutex);
241
242	if (HEIM_SLIST_FIRST(&_gss_mechs)) {
243		HEIMDAL_MUTEX_unlock(&_gss_mech_mutex);
244		return;
245	}
246
247	major_status = gss_create_empty_oid_set(&minor_status,
248	    &_gss_mech_oids);
249	if (major_status) {
250		HEIMDAL_MUTEX_unlock(&_gss_mech_mutex);
251		return;
252	}
253
254	add_builtin(__gss_krb5_initialize());
255	add_builtin(__gss_spnego_initialize());
256	add_builtin(__gss_ntlm_initialize());
257
258#ifdef HAVE_DLOPEN
259	fp = fopen(_PATH_GSS_MECH, "r");
260	if (!fp) {
261		HEIMDAL_MUTEX_unlock(&_gss_mech_mutex);
262		return;
263	}
264	rk_cloexec_file(fp);
265
266	while (fgets(buf, sizeof(buf), fp)) {
267		_gss_mo_init *mi;
268
269		if (*buf == '#')
270			continue;
271		p = buf;
272		name = strsep(&p, "\t\n ");
273		if (p) while (isspace((unsigned char)*p)) p++;
274		oid = strsep(&p, "\t\n ");
275		if (p) while (isspace((unsigned char)*p)) p++;
276		lib = strsep(&p, "\t\n ");
277		if (p) while (isspace((unsigned char)*p)) p++;
278		kobj = strsep(&p, "\t\n ");
279		if (!name || !oid || !lib || !kobj)
280			continue;
281
282		if (_gss_string_to_oid(oid, &mech_oid))
283			continue;
284
285		/*
286		 * Check for duplicates, already loaded mechs.
287		 */
288		found = 0;
289		HEIM_SLIST_FOREACH(m, &_gss_mechs, gm_link) {
290			if (gss_oid_equal(&m->gm_mech.gm_mech_oid, &mech_oid)) {
291				found = 1;
292				free(mech_oid.elements);
293				break;
294			}
295		}
296		if (found)
297			continue;
298
299#ifndef RTLD_LOCAL
300#define RTLD_LOCAL 0
301#endif
302
303#ifndef RTLD_GROUP
304#define RTLD_GROUP 0
305#endif
306
307		so = dlopen(lib, RTLD_LAZY | RTLD_LOCAL | RTLD_GROUP);
308		if (so == NULL) {
309/*			fprintf(stderr, "dlopen: %s\n", dlerror()); */
310			goto bad;
311		}
312
313		m = calloc(1, sizeof(*m));
314		if (m == NULL)
315			goto bad;
316
317		m->gm_so = so;
318		m->gm_mech.gm_mech_oid = mech_oid;
319		m->gm_mech.gm_flags = 0;
320		m->gm_mech.gm_compat = calloc(1, sizeof(struct gss_mech_compat_desc_struct));
321		if (m->gm_mech.gm_compat == NULL)
322			goto bad;
323
324		major_status = gss_add_oid_set_member(&minor_status,
325		    &m->gm_mech.gm_mech_oid, &_gss_mech_oids);
326		if (GSS_ERROR(major_status))
327			goto bad;
328
329		SYM(acquire_cred);
330		SYM(release_cred);
331		SYM(init_sec_context);
332		SYM(accept_sec_context);
333		SYM(process_context_token);
334		SYM(delete_sec_context);
335		SYM(context_time);
336		SYM(get_mic);
337		SYM(verify_mic);
338		SYM(wrap);
339		SYM(unwrap);
340		SYM(display_status);
341		SYM(indicate_mechs);
342		SYM(compare_name);
343		SYM(display_name);
344		SYM(import_name);
345		SYM(export_name);
346		SYM(release_name);
347		SYM(inquire_cred);
348		SYM(inquire_context);
349		SYM(wrap_size_limit);
350		SYM(add_cred);
351		SYM(inquire_cred_by_mech);
352		SYM(export_sec_context);
353		SYM(import_sec_context);
354		SYM(inquire_names_for_mech);
355		SYM(inquire_mechs_for_name);
356		SYM(canonicalize_name);
357		SYM(duplicate_name);
358		OPTSYM(inquire_cred_by_oid);
359		OPTSYM(inquire_sec_context_by_oid);
360		OPTSYM(set_sec_context_option);
361		OPTSPISYM(set_cred_option);
362		OPTSYM(pseudo_random);
363		OPTSYM(wrap_iov);
364		OPTSYM(unwrap_iov);
365		OPTSYM(wrap_iov_length);
366		OPTSYM(store_cred);
367		OPTSYM(export_cred);
368		OPTSYM(import_cred);
369#if 0
370		OPTSYM(acquire_cred_ext);
371		OPTSYM(iter_creds);
372		OPTSYM(destroy_cred);
373		OPTSYM(cred_hold);
374		OPTSYM(cred_unhold);
375		OPTSYM(cred_label_get);
376		OPTSYM(cred_label_set);
377#endif
378		OPTSYM(display_name_ext);
379		OPTSYM(inquire_name);
380		OPTSYM(get_name_attribute);
381		OPTSYM(set_name_attribute);
382		OPTSYM(delete_name_attribute);
383		OPTSYM(export_name_composite);
384		OPTSYM(pname_to_uid);
385		OPTSPISYM(authorize_localname);
386
387		mi = dlsym(so, "gss_mo_init");
388		if (mi != NULL) {
389			major_status = mi(&minor_status, &mech_oid,
390					  &m->gm_mech.gm_mo, &m->gm_mech.gm_mo_num);
391			if (GSS_ERROR(major_status))
392				goto bad;
393		} else {
394			/* API-as-SPI compatibility */
395			COMPATSYM(inquire_saslname_for_mech);
396			COMPATSYM(inquire_mech_for_saslname);
397			COMPATSYM(inquire_attrs_for_mech);
398			COMPATSPISYM(acquire_cred_with_password);
399		}
400
401		/* pick up the oid sets of names */
402
403		if (m->gm_mech.gm_inquire_names_for_mech)
404			(*m->gm_mech.gm_inquire_names_for_mech)(&minor_status,
405			&m->gm_mech.gm_mech_oid, &m->gm_name_types);
406
407		if (m->gm_name_types == NULL)
408			gss_create_empty_oid_set(&minor_status, &m->gm_name_types);
409
410		HEIM_SLIST_INSERT_HEAD(&_gss_mechs, m, gm_link);
411		continue;
412
413	bad:
414		if (m != NULL) {
415			free(m->gm_mech.gm_compat);
416			free(m->gm_mech.gm_mech_oid.elements);
417			free(m);
418		}
419		dlclose(so);
420		continue;
421	}
422	fclose(fp);
423#endif
424	HEIMDAL_MUTEX_unlock(&_gss_mech_mutex);
425}
426
427gssapi_mech_interface
428__gss_get_mechanism(gss_const_OID mech)
429{
430        struct _gss_mech_switch	*m;
431
432	_gss_load_mech();
433	HEIM_SLIST_FOREACH(m, &_gss_mechs, gm_link) {
434		if (gss_oid_equal(&m->gm_mech.gm_mech_oid, mech))
435			return &m->gm_mech;
436	}
437	return NULL;
438}
439