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,v 1.7 2006/10/09 11:13:30 lha Exp $");
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;
54 * First figure out how many numbers in the oid, then
55 * calculate the compiled oid size.
58 for (p = s; p; p = q) {
65 * The first two numbers are in the first byte and each
66 * subsequent number is encoded in a variable byte sequence.
72 * We do this in two passes. The first pass, we just figure
73 * out the size. Second time around, we actually encode the
77 for (i = 0; i < 2; i++) {
79 for (p = s, j = 0; p; p = q, j++) {
80 unsigned int number = 0;
83 * Find the end of this number.
89 * Read the number of of the string. Don't
90 * bother with anything except base ten.
92 while (*p && *p != '.') {
93 number = 10 * number + (*p - '0');
98 * Encode the number. The first two numbers
99 * are packed into the first byte. Subsequent
100 * numbers are encoded in bytes seven bits at
101 * a time with the last byte having the high
115 * The number is encoded in seven bit chunks.
121 for (t = number; t; t >>= 7)
123 if (bytes == 0) bytes = 1;
126 int bit = 7*(bytes-1);
128 *res = (number >> bit) & 0x7f;
139 res = malloc(byte_count);
142 oid->length = byte_count;
152 m->gm_mech.gm_ ## name = dlsym(so, "gss_" #name); \
153 if (!m->gm_mech.gm_ ## name) { \
154 fprintf(stderr, "can't find symbol gss_" #name "\n"); \
159 #define OPTSYM(name) \
161 m->gm_mech.gm_ ## name = dlsym(so, "gss_" #name); \
168 add_builtin(gssapi_mech_interface mech)
170 struct _gss_mech_switch *m;
171 OM_uint32 minor_status;
173 m = malloc(sizeof(*m));
178 m->gm_mech_oid = mech->gm_mech_oid; /* XXX */
179 gss_add_oid_set_member(&minor_status,
180 &m->gm_mech.gm_mech_oid, &_gss_mech_oids);
182 SLIST_INSERT_HEAD(&_gss_mechs, m, gm_link);
187 * Load the mechanisms file (/etc/gss/mech).
192 OM_uint32 major_status, minor_status;
196 char *name, *oid, *lib, *kobj;
197 struct _gss_mech_switch *m;
201 HEIMDAL_MUTEX_lock(&_gss_mech_mutex);
203 if (SLIST_FIRST(&_gss_mechs)) {
204 HEIMDAL_MUTEX_unlock(&_gss_mech_mutex);
208 major_status = gss_create_empty_oid_set(&minor_status,
211 HEIMDAL_MUTEX_unlock(&_gss_mech_mutex);
215 add_builtin(__gss_krb5_initialize());
216 add_builtin(__gss_spnego_initialize());
218 fp = fopen(_PATH_GSS_MECH, "r");
220 /* perror(_PATH_GSS_MECH); */
221 HEIMDAL_MUTEX_unlock(&_gss_mech_mutex);
225 while (fgets(buf, sizeof(buf), fp)) {
229 name = strsep(&p, "\t\n ");
230 if (p) while (isspace((unsigned char)*p)) p++;
231 oid = strsep(&p, "\t\n ");
232 if (p) while (isspace((unsigned char)*p)) p++;
233 lib = strsep(&p, "\t\n ");
234 if (p) while (isspace((unsigned char)*p)) p++;
235 kobj = strsep(&p, "\t\n ");
236 if (!name || !oid || !lib || !kobj)
243 so = dlopen(lib, RTLD_LOCAL);
245 /* fprintf(stderr, "dlopen: %s\n", dlerror()); */
249 m = malloc(sizeof(*m));
253 if (_gss_string_to_oid(oid, &m->gm_mech.gm_mech_oid)) {
258 major_status = gss_add_oid_set_member(&minor_status,
259 &m->gm_mech.gm_mech_oid, &_gss_mech_oids);
261 free(m->gm_mech.gm_mech_oid.elements);
268 SYM(init_sec_context);
269 SYM(accept_sec_context);
270 SYM(process_context_token);
271 SYM(delete_sec_context);
285 SYM(inquire_context);
286 SYM(wrap_size_limit);
288 SYM(inquire_cred_by_mech);
289 SYM(export_sec_context);
290 SYM(import_sec_context);
291 SYM(inquire_names_for_mech);
292 SYM(inquire_mechs_for_name);
293 SYM(canonicalize_name);
295 OPTSYM(inquire_cred_by_oid);
296 OPTSYM(inquire_sec_context_by_oid);
297 OPTSYM(set_sec_context_option);
298 OPTSYM(set_cred_option);
300 SLIST_INSERT_HEAD(&_gss_mechs, m, gm_link);
304 free(m->gm_mech.gm_mech_oid.elements);
310 HEIMDAL_MUTEX_unlock(&_gss_mech_mutex);
313 gssapi_mech_interface
314 __gss_get_mechanism(gss_OID mech)
316 struct _gss_mech_switch *m;
319 SLIST_FOREACH(m, &_gss_mechs, gm_link) {
320 if (gss_oid_equal(&m->gm_mech.gm_mech_oid, mech))