1 /* $NetBSD: gss_mech_switch.c,v 1.1.1.2 2014/04/24 12:45:29 pettai Exp $ */
4 * Copyright (c) 2005 Doug Rabson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
28 * $FreeBSD: src/lib/libgssapi/gss_mech_switch.c,v 1.2 2006/02/04 09:40:21 dfr Exp $
31 #include "mech_locl.h"
32 #include <heim_threads.h>
34 #ifndef _PATH_GSS_MECH
35 #define _PATH_GSS_MECH "/etc/gss/mech"
38 struct _gss_mech_switch_list _gss_mechs
= { NULL
} ;
39 gss_OID_set _gss_mech_oids
;
40 static HEIMDAL_MUTEX _gss_mech_mutex
= HEIMDAL_MUTEX_INITIALIZER
;
43 * Convert a string containing an OID in 'dot' form
44 * (e.g. 1.2.840.113554.1.2.2) to a gss_OID.
47 _gss_string_to_oid(const char* s
, gss_OID oid
)
49 int number_count
, i
, j
;
58 * First figure out how many numbers in the oid, then
59 * calculate the compiled oid size.
62 for (p
= s
; p
; p
= q
) {
69 * The first two numbers are in the first byte and each
70 * subsequent number is encoded in a variable byte sequence.
76 * We do this in two passes. The first pass, we just figure
77 * out the size. Second time around, we actually encode the
81 for (i
= 0; i
< 2; i
++) {
83 for (p
= s
, j
= 0; p
; p
= q
, j
++) {
84 unsigned int number
= 0;
87 * Find the end of this number.
93 * Read the number of of the string. Don't
94 * bother with anything except base ten.
96 while (*p
&& *p
!= '.') {
97 number
= 10 * number
+ (*p
- '0');
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
119 * The number is encoded in seven bit chunks.
125 for (t
= number
; t
; t
>>= 7)
127 if (bytes
== 0) bytes
= 1;
130 int bit
= 7*(bytes
-1);
132 *res
= (number
>> bit
) & 0x7f;
143 res
= malloc(byte_count
);
146 oid
->length
= byte_count
;
156 m->gm_mech.gm_ ## name = dlsym(so, "gss_" #name); \
157 if (!m->gm_mech.gm_ ## name || \
158 m->gm_mech.gm_ ##name == gss_ ## name) { \
159 fprintf(stderr, "can't find symbol gss_" #name "\n"); \
164 #define OPTSYM(name) \
166 m->gm_mech.gm_ ## name = dlsym(so, "gss_" #name); \
167 if (m->gm_mech.gm_ ## name == gss_ ## name) \
168 m->gm_mech.gm_ ## name = NULL; \
171 #define OPTSPISYM(name) \
173 m->gm_mech.gm_ ## name = dlsym(so, "gssspi_" #name); \
176 #define COMPATSYM(name) \
178 m->gm_mech.gm_compat->gmc_ ## name = dlsym(so, "gss_" #name); \
179 if (m->gm_mech.gm_compat->gmc_ ## name == gss_ ## name) \
180 m->gm_mech.gm_compat->gmc_ ## name = NULL; \
183 #define COMPATSPISYM(name) \
185 m->gm_mech.gm_compat->gmc_ ## name = dlsym(so, "gssspi_" #name);\
186 if (m->gm_mech.gm_compat->gmc_ ## name == gss_ ## name) \
187 m->gm_mech.gm_compat->gmc_ ## name = NULL; \
194 add_builtin(gssapi_mech_interface mech
)
196 struct _gss_mech_switch
*m
;
197 OM_uint32 minor_status
;
199 /* not registering any mech is ok */
203 m
= calloc(1, sizeof(*m
));
208 m
->gm_mech_oid
= mech
->gm_mech_oid
; /* XXX */
209 gss_add_oid_set_member(&minor_status
,
210 &m
->gm_mech
.gm_mech_oid
, &_gss_mech_oids
);
212 /* pick up the oid sets of names */
214 if (m
->gm_mech
.gm_inquire_names_for_mech
)
215 (*m
->gm_mech
.gm_inquire_names_for_mech
)(&minor_status
,
216 &m
->gm_mech
.gm_mech_oid
, &m
->gm_name_types
);
218 if (m
->gm_name_types
== NULL
)
219 gss_create_empty_oid_set(&minor_status
, &m
->gm_name_types
);
221 HEIM_SLIST_INSERT_HEAD(&_gss_mechs
, m
, gm_link
);
226 * Load the mechanisms file (/etc/gss/mech).
231 OM_uint32 major_status
, minor_status
;
235 char *name
, *oid
, *lib
, *kobj
;
236 struct _gss_mech_switch
*m
;
238 gss_OID_desc mech_oid
;
242 HEIMDAL_MUTEX_lock(&_gss_mech_mutex
);
244 if (HEIM_SLIST_FIRST(&_gss_mechs
)) {
245 HEIMDAL_MUTEX_unlock(&_gss_mech_mutex
);
249 major_status
= gss_create_empty_oid_set(&minor_status
,
252 HEIMDAL_MUTEX_unlock(&_gss_mech_mutex
);
256 add_builtin(__gss_krb5_initialize());
257 add_builtin(__gss_spnego_initialize());
258 add_builtin(__gss_ntlm_initialize());
261 fp
= fopen(_PATH_GSS_MECH
, "r");
263 HEIMDAL_MUTEX_unlock(&_gss_mech_mutex
);
268 while (fgets(buf
, sizeof(buf
), fp
)) {
274 name
= strsep(&p
, "\t\n ");
275 if (p
) while (isspace((unsigned char)*p
)) p
++;
276 oid
= strsep(&p
, "\t\n ");
277 if (p
) while (isspace((unsigned char)*p
)) p
++;
278 lib
= strsep(&p
, "\t\n ");
279 if (p
) while (isspace((unsigned char)*p
)) p
++;
280 kobj
= strsep(&p
, "\t\n ");
281 if (!name
|| !oid
|| !lib
|| !kobj
)
284 if (_gss_string_to_oid(oid
, &mech_oid
))
288 * Check for duplicates, already loaded mechs.
291 HEIM_SLIST_FOREACH(m
, &_gss_mechs
, gm_link
) {
292 if (gss_oid_equal(&m
->gm_mech
.gm_mech_oid
, &mech_oid
)) {
294 free(mech_oid
.elements
);
309 so
= dlopen(lib
, RTLD_LAZY
| RTLD_LOCAL
| RTLD_GROUP
);
311 /* fprintf(stderr, "dlopen: %s\n", dlerror()); */
315 m
= calloc(1, sizeof(*m
));
320 m
->gm_mech
.gm_mech_oid
= mech_oid
;
321 m
->gm_mech
.gm_flags
= 0;
322 m
->gm_mech
.gm_compat
= calloc(1, sizeof(struct gss_mech_compat_desc_struct
));
323 if (m
->gm_mech
.gm_compat
== NULL
)
326 major_status
= gss_add_oid_set_member(&minor_status
,
327 &m
->gm_mech
.gm_mech_oid
, &_gss_mech_oids
);
328 if (GSS_ERROR(major_status
))
333 SYM(init_sec_context
);
334 SYM(accept_sec_context
);
335 SYM(process_context_token
);
336 SYM(delete_sec_context
);
350 SYM(inquire_context
);
351 SYM(wrap_size_limit
);
353 SYM(inquire_cred_by_mech
);
354 SYM(export_sec_context
);
355 SYM(import_sec_context
);
356 SYM(inquire_names_for_mech
);
357 SYM(inquire_mechs_for_name
);
358 SYM(canonicalize_name
);
360 OPTSYM(inquire_cred_by_oid
);
361 OPTSYM(inquire_sec_context_by_oid
);
362 OPTSYM(set_sec_context_option
);
363 OPTSPISYM(set_cred_option
);
364 OPTSYM(pseudo_random
);
367 OPTSYM(wrap_iov_length
);
372 OPTSYM(acquire_cred_ext
);
374 OPTSYM(destroy_cred
);
377 OPTSYM(cred_label_get
);
378 OPTSYM(cred_label_set
);
380 OPTSYM(display_name_ext
);
381 OPTSYM(inquire_name
);
382 OPTSYM(get_name_attribute
);
383 OPTSYM(set_name_attribute
);
384 OPTSYM(delete_name_attribute
);
385 OPTSYM(export_name_composite
);
386 OPTSYM(pname_to_uid
);
387 OPTSPISYM(authorize_localname
);
389 mi
= dlsym(so
, "gss_mo_init");
391 major_status
= mi(&minor_status
, &mech_oid
,
392 &m
->gm_mech
.gm_mo
, &m
->gm_mech
.gm_mo_num
);
393 if (GSS_ERROR(major_status
))
396 /* API-as-SPI compatibility */
397 COMPATSYM(inquire_saslname_for_mech
);
398 COMPATSYM(inquire_mech_for_saslname
);
399 COMPATSYM(inquire_attrs_for_mech
);
400 COMPATSPISYM(acquire_cred_with_password
);
403 /* pick up the oid sets of names */
405 if (m
->gm_mech
.gm_inquire_names_for_mech
)
406 (*m
->gm_mech
.gm_inquire_names_for_mech
)(&minor_status
,
407 &m
->gm_mech
.gm_mech_oid
, &m
->gm_name_types
);
409 if (m
->gm_name_types
== NULL
)
410 gss_create_empty_oid_set(&minor_status
, &m
->gm_name_types
);
412 HEIM_SLIST_INSERT_HEAD(&_gss_mechs
, m
, gm_link
);
417 free(m
->gm_mech
.gm_compat
);
418 free(m
->gm_mech
.gm_mech_oid
.elements
);
426 HEIMDAL_MUTEX_unlock(&_gss_mech_mutex
);
429 gssapi_mech_interface
430 __gss_get_mechanism(gss_const_OID mech
)
432 struct _gss_mech_switch
*m
;
435 HEIM_SLIST_FOREACH(m
, &_gss_mechs
, gm_link
) {
436 if (gss_oid_equal(&m
->gm_mech
.gm_mech_oid
, mech
))