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>
32 #ifndef _PATH_GSS_MECH
33 #define _PATH_GSS_MECH "/etc/gss/mech"
36 struct _gss_mech_switch_list _gss_mechs
= { NULL
} ;
37 gss_OID_set _gss_mech_oids
;
38 static HEIMDAL_MUTEX _gss_mech_mutex
= HEIMDAL_MUTEX_INITIALIZER
;
41 * Convert a string containing an OID in 'dot' form
42 * (e.g. 1.2.840.113554.1.2.2) to a gss_OID.
45 _gss_string_to_oid(const char* s
, gss_OID oid
)
47 int number_count
, i
, j
;
56 * First figure out how many numbers in the oid, then
57 * calculate the compiled oid size.
60 for (p
= s
; p
; p
= q
) {
67 * The first two numbers are in the first byte and each
68 * subsequent number is encoded in a variable byte sequence.
74 * We do this in two passes. The first pass, we just figure
75 * out the size. Second time around, we actually encode the
79 for (i
= 0; i
< 2; i
++) {
81 for (p
= s
, j
= 0; p
; p
= q
, j
++) {
82 unsigned int number
= 0;
85 * Find the end of this number.
91 * Read the number of of the string. Don't
92 * bother with anything except base ten.
94 while (*p
&& *p
!= '.') {
95 number
= 10 * number
+ (*p
- '0');
100 * Encode the number. The first two numbers
101 * are packed into the first byte. Subsequent
102 * numbers are encoded in bytes seven bits at
103 * a time with the last byte having the high
117 * The number is encoded in seven bit chunks.
123 for (t
= number
; t
; t
>>= 7)
125 if (bytes
== 0) bytes
= 1;
128 int bit
= 7*(bytes
-1);
130 *res
= (number
>> bit
) & 0x7f;
141 res
= malloc(byte_count
);
144 oid
->length
= byte_count
;
154 m->gm_mech.gm_ ## name = dlsym(so, "gss_" #name); \
155 if (!m->gm_mech.gm_ ## name) { \
156 fprintf(stderr, "can't find symbol gss_" #name "\n"); \
161 #define OPTSYM(name) \
163 m->gm_mech.gm_ ## name = dlsym(so, "gss_" #name); \
166 #define OPTSPISYM(name) \
168 m->gm_mech.gm_ ## name = dlsym(so, "gssspi_" #name); \
171 #define COMPATSYM(name) \
173 m->gm_mech.gm_compat->gmc_ ## name = dlsym(so, "gss_" #name); \
180 add_builtin(gssapi_mech_interface mech
)
182 struct _gss_mech_switch
*m
;
183 OM_uint32 minor_status
;
185 /* not registering any mech is ok */
189 m
= calloc(1, sizeof(*m
));
194 m
->gm_mech_oid
= mech
->gm_mech_oid
; /* XXX */
195 gss_add_oid_set_member(&minor_status
,
196 &m
->gm_mech
.gm_mech_oid
, &_gss_mech_oids
);
198 /* pick up the oid sets of names */
200 if (m
->gm_mech
.gm_inquire_names_for_mech
)
201 (*m
->gm_mech
.gm_inquire_names_for_mech
)(&minor_status
,
202 &m
->gm_mech
.gm_mech_oid
, &m
->gm_name_types
);
204 if (m
->gm_name_types
== NULL
)
205 gss_create_empty_oid_set(&minor_status
, &m
->gm_name_types
);
207 HEIM_SLIST_INSERT_HEAD(&_gss_mechs
, m
, gm_link
);
212 * Load the mechanisms file (/etc/gss/mech).
217 OM_uint32 major_status
, minor_status
;
221 char *name
, *oid
, *lib
, *kobj
;
222 struct _gss_mech_switch
*m
;
224 gss_OID_desc mech_oid
;
228 HEIMDAL_MUTEX_lock(&_gss_mech_mutex
);
230 if (HEIM_SLIST_FIRST(&_gss_mechs
)) {
231 HEIMDAL_MUTEX_unlock(&_gss_mech_mutex
);
235 major_status
= gss_create_empty_oid_set(&minor_status
,
238 HEIMDAL_MUTEX_unlock(&_gss_mech_mutex
);
242 add_builtin(__gss_krb5_initialize());
243 add_builtin(__gss_spnego_initialize());
244 add_builtin(__gss_ntlm_initialize());
247 fp
= fopen(_PATH_GSS_MECH
, "r");
249 HEIMDAL_MUTEX_unlock(&_gss_mech_mutex
);
254 while (fgets(buf
, sizeof(buf
), fp
)) {
260 name
= strsep(&p
, "\t\n ");
261 if (p
) while (isspace((unsigned char)*p
)) p
++;
262 oid
= strsep(&p
, "\t\n ");
263 if (p
) while (isspace((unsigned char)*p
)) p
++;
264 lib
= strsep(&p
, "\t\n ");
265 if (p
) while (isspace((unsigned char)*p
)) p
++;
266 kobj
= strsep(&p
, "\t\n ");
267 if (!name
|| !oid
|| !lib
|| !kobj
)
270 if (_gss_string_to_oid(oid
, &mech_oid
))
274 * Check for duplicates, already loaded mechs.
277 HEIM_SLIST_FOREACH(m
, &_gss_mechs
, gm_link
) {
278 if (gss_oid_equal(&m
->gm_mech
.gm_mech_oid
, &mech_oid
)) {
280 free(mech_oid
.elements
);
295 so
= dlopen(lib
, RTLD_LAZY
| RTLD_LOCAL
| RTLD_GROUP
);
297 /* fprintf(stderr, "dlopen: %s\n", dlerror()); */
301 m
= calloc(1, sizeof(*m
));
306 m
->gm_mech
.gm_mech_oid
= mech_oid
;
307 m
->gm_mech
.gm_flags
= 0;
308 m
->gm_mech
.gm_compat
= calloc(1, sizeof(struct gss_mech_compat_desc_struct
));
309 if (m
->gm_mech
.gm_compat
== NULL
)
312 major_status
= gss_add_oid_set_member(&minor_status
,
313 &m
->gm_mech
.gm_mech_oid
, &_gss_mech_oids
);
314 if (GSS_ERROR(major_status
))
319 SYM(init_sec_context
);
320 SYM(accept_sec_context
);
321 SYM(process_context_token
);
322 SYM(delete_sec_context
);
336 SYM(inquire_context
);
337 SYM(wrap_size_limit
);
339 SYM(inquire_cred_by_mech
);
340 SYM(export_sec_context
);
341 SYM(import_sec_context
);
342 SYM(inquire_names_for_mech
);
343 SYM(inquire_mechs_for_name
);
344 SYM(canonicalize_name
);
346 OPTSYM(inquire_cred_by_oid
);
347 OPTSYM(inquire_sec_context_by_oid
);
348 OPTSYM(set_sec_context_option
);
349 OPTSPISYM(set_cred_option
);
350 OPTSYM(pseudo_random
);
353 OPTSYM(wrap_iov_length
);
354 OPTSYM(display_name_ext
);
355 OPTSYM(inquire_name
);
356 OPTSYM(get_name_attribute
);
357 OPTSYM(set_name_attribute
);
358 OPTSYM(delete_name_attribute
);
359 OPTSYM(export_name_composite
);
360 OPTSPISYM(acquire_cred_with_password
);
361 OPTSYM(add_cred_with_password
);
362 OPTSYM(pname_to_uid
);
365 mi
= dlsym(so
, "gss_mo_init");
367 major_status
= mi(&minor_status
, &mech_oid
,
368 &m
->gm_mech
.gm_mo
, &m
->gm_mech
.gm_mo_num
);
369 if (GSS_ERROR(major_status
))
372 /* API-as-SPI compatibility */
373 COMPATSYM(inquire_saslname_for_mech
);
374 COMPATSYM(inquire_mech_for_saslname
);
375 COMPATSYM(inquire_attrs_for_mech
);
378 /* pick up the oid sets of names */
380 if (m
->gm_mech
.gm_inquire_names_for_mech
)
381 (*m
->gm_mech
.gm_inquire_names_for_mech
)(&minor_status
,
382 &m
->gm_mech
.gm_mech_oid
, &m
->gm_name_types
);
384 if (m
->gm_name_types
== NULL
)
385 gss_create_empty_oid_set(&minor_status
, &m
->gm_name_types
);
387 HEIM_SLIST_INSERT_HEAD(&_gss_mechs
, m
, gm_link
);
392 free(m
->gm_mech
.gm_compat
);
393 free(m
->gm_mech
.gm_mech_oid
.elements
);
401 HEIMDAL_MUTEX_unlock(&_gss_mech_mutex
);
404 gssapi_mech_interface
405 __gss_get_mechanism(gss_const_OID mech
)
407 struct _gss_mech_switch
*m
;
410 HEIM_SLIST_FOREACH(m
, &_gss_mechs
, gm_link
) {
411 if (gss_oid_equal(&m
->gm_mech
.gm_mech_oid
, mech
))