2 * Copyright (c) 2005 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * $FreeBSD: src/lib/libgssapi/gss_mech_switch.c,v 1.2 2006/02/04 09:40:21 dfr Exp $
29 #include "mech_locl.h"
30 #include <heim_threads.h>
31 RCSID("$Id: gss_mech_switch.c 21698 2007-07-26 19:07:11Z lha $");
33 #ifndef _PATH_GSS_MECH
34 #define _PATH_GSS_MECH "/etc/gss/mech"
37 struct _gss_mech_switch_list _gss_mechs = { NULL } ;
38 gss_OID_set _gss_mech_oids;
39 static HEIMDAL_MUTEX _gss_mech_mutex = HEIMDAL_MUTEX_INITIALIZER;
42 * Convert a string containing an OID in 'dot' form
43 * (e.g. 1.2.840.113554.1.2.2) to a gss_OID.
46 _gss_string_to_oid(const char* s, gss_OID oid)
48 int number_count, i, j;
57 * First figure out how many numbers in the oid, then
58 * calculate the compiled oid size.
61 for (p = s; p; p = q) {
68 * The first two numbers are in the first byte and each
69 * subsequent number is encoded in a variable byte sequence.
75 * We do this in two passes. The first pass, we just figure
76 * out the size. Second time around, we actually encode the
80 for (i = 0; i < 2; i++) {
82 for (p = s, j = 0; p; p = q, j++) {
83 unsigned int number = 0;
86 * Find the end of this number.
92 * Read the number of of the string. Don't
93 * bother with anything except base ten.
95 while (*p && *p != '.') {
96 number = 10 * number + (*p - '0');
101 * Encode the number. The first two numbers
102 * are packed into the first byte. Subsequent
103 * numbers are encoded in bytes seven bits at
104 * a time with the last byte having the high
118 * The number is encoded in seven bit chunks.
124 for (t = number; t; t >>= 7)
126 if (bytes == 0) bytes = 1;
129 int bit = 7*(bytes-1);
131 *res = (number >> bit) & 0x7f;
142 res = malloc(byte_count);
145 oid->length = byte_count;
155 m->gm_mech.gm_ ## name = dlsym(so, "gss_" #name); \
156 if (!m->gm_mech.gm_ ## name) { \
157 fprintf(stderr, "can't find symbol gss_" #name "\n"); \
162 #define OPTSYM(name) \
164 m->gm_mech.gm_ ## name = dlsym(so, "gss_" #name); \
171 add_builtin(gssapi_mech_interface mech)
173 struct _gss_mech_switch *m;
174 OM_uint32 minor_status;
176 /* not registering any mech is ok */
180 m = malloc(sizeof(*m));
185 m->gm_mech_oid = mech->gm_mech_oid; /* XXX */
186 gss_add_oid_set_member(&minor_status,
187 &m->gm_mech.gm_mech_oid, &_gss_mech_oids);
189 SLIST_INSERT_HEAD(&_gss_mechs, m, gm_link);
194 * Load the mechanisms file (/etc/gss/mech).
199 OM_uint32 major_status, minor_status;
203 char *name, *oid, *lib, *kobj;
204 struct _gss_mech_switch *m;
208 HEIMDAL_MUTEX_lock(&_gss_mech_mutex);
210 if (SLIST_FIRST(&_gss_mechs)) {
211 HEIMDAL_MUTEX_unlock(&_gss_mech_mutex);
215 major_status = gss_create_empty_oid_set(&minor_status,
218 HEIMDAL_MUTEX_unlock(&_gss_mech_mutex);
222 add_builtin(__gss_krb5_initialize());
223 add_builtin(__gss_spnego_initialize());
224 add_builtin(__gss_ntlm_initialize());
227 fp = fopen(_PATH_GSS_MECH, "r");
229 HEIMDAL_MUTEX_unlock(&_gss_mech_mutex);
233 while (fgets(buf, sizeof(buf), fp)) {
237 name = strsep(&p, "\t\n ");
238 if (p) while (isspace((unsigned char)*p)) p++;
239 oid = strsep(&p, "\t\n ");
240 if (p) while (isspace((unsigned char)*p)) p++;
241 lib = strsep(&p, "\t\n ");
242 if (p) while (isspace((unsigned char)*p)) p++;
243 kobj = strsep(&p, "\t\n ");
244 if (!name || !oid || !lib || !kobj)
251 so = dlopen(lib, RTLD_LOCAL);
253 /* fprintf(stderr, "dlopen: %s\n", dlerror()); */
257 m = malloc(sizeof(*m));
261 if (_gss_string_to_oid(oid, &m->gm_mech.gm_mech_oid)) {
266 major_status = gss_add_oid_set_member(&minor_status,
267 &m->gm_mech.gm_mech_oid, &_gss_mech_oids);
269 free(m->gm_mech.gm_mech_oid.elements);
276 SYM(init_sec_context);
277 SYM(accept_sec_context);
278 SYM(process_context_token);
279 SYM(delete_sec_context);
293 SYM(inquire_context);
294 SYM(wrap_size_limit);
296 SYM(inquire_cred_by_mech);
297 SYM(export_sec_context);
298 SYM(import_sec_context);
299 SYM(inquire_names_for_mech);
300 SYM(inquire_mechs_for_name);
301 SYM(canonicalize_name);
303 OPTSYM(inquire_cred_by_oid);
304 OPTSYM(inquire_sec_context_by_oid);
305 OPTSYM(set_sec_context_option);
306 OPTSYM(set_cred_option);
307 OPTSYM(pseudo_random);
309 SLIST_INSERT_HEAD(&_gss_mechs, m, gm_link);
313 free(m->gm_mech.gm_mech_oid.elements);
320 HEIMDAL_MUTEX_unlock(&_gss_mech_mutex);
323 gssapi_mech_interface
324 __gss_get_mechanism(gss_OID mech)
326 struct _gss_mech_switch *m;
329 SLIST_FOREACH(m, &_gss_mechs, gm_link) {
330 if (gss_oid_equal(&m->gm_mech.gm_mech_oid, mech))