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
29 #include <gssapi/gssapi.h>
36 #include "mech_switch.h"
39 #ifndef _PATH_GSS_MECH
40 #define _PATH_GSS_MECH "/etc/gss/mech"
43 struct _gss_mech_switch_list _gss_mechs =
44 SLIST_HEAD_INITIALIZER(&_gss_mechs);
45 gss_OID_set _gss_mech_oids;
48 * Convert a string containing an OID in 'dot' form
49 * (e.g. 1.2.840.113554.1.2.2) to a gss_OID.
52 _gss_string_to_oid(const char* s, gss_OID oid)
54 int number_count, i, j;
60 * First figure out how many numbers in the oid, then
61 * calculate the compiled oid size.
64 for (p = s; p; p = q) {
71 * The first two numbers are in the first byte and each
72 * subsequent number is encoded in a variable byte sequence.
78 * We do this in two passes. The first pass, we just figure
79 * out the size. Second time around, we actually encode the
83 for (i = 0; i < 2; i++) {
85 for (p = s, j = 0; p; p = q, j++) {
86 unsigned int number = 0;
89 * Find the end of this number.
95 * Read the number of of the string. Don't
96 * bother with anything except base ten.
98 while (*p && *p != '.') {
99 number = 10 * number + (*p - '0');
104 * Encode the number. The first two numbers
105 * are packed into the first byte. Subsequent
106 * numbers are encoded in bytes seven bits at
107 * a time with the last byte having the high
121 * The number is encoded in seven bit chunks.
127 for (t = number; t; t >>= 7)
129 if (bytes == 0) bytes = 1;
132 int bit = 7*(bytes-1);
134 *res = (number >> bit) & 0x7f;
145 res = malloc(byte_count);
148 oid->length = byte_count;
158 m->gm_ ## name = dlsym(so, "gss_" #name); \
159 if (!m->gm_ ## name) { \
160 fprintf(stderr, "can't find symbol gss_" #name "\n"); \
165 #define OPTSYM(name) \
167 m->gm_ ## name = dlsym(so, "gss_" #name); \
170 #define OPTSYM2(symname, ourname) \
172 m->ourname = dlsym(so, #symname); \
176 * Load the mechanisms file (/etc/gss/mech).
181 OM_uint32 major_status, minor_status;
185 char *name, *oid, *lib, *kobj;
186 struct _gss_mech_switch *m;
191 if (SLIST_FIRST(&_gss_mechs))
194 major_status = gss_create_empty_oid_set(&minor_status,
199 fp = fopen(_PATH_GSS_MECH, "r");
201 perror(_PATH_GSS_MECH);
206 while (fgets(buf, sizeof(buf), fp)) {
210 name = strsep(&p, "\t\n ");
211 if (p) while (isspace(*p)) p++;
212 oid = strsep(&p, "\t\n ");
213 if (p) while (isspace(*p)) p++;
214 lib = strsep(&p, "\t\n ");
215 if (p) while (isspace(*p)) p++;
216 kobj = strsep(&p, "\t\n ");
217 if (!name || !oid || !lib || !kobj)
220 so = dlopen(lib, RTLD_LOCAL);
222 fprintf(stderr, "dlopen: %s\n", dlerror());
226 m = malloc(sizeof(struct _gss_mech_switch));
230 if (_gss_string_to_oid(oid, &m->gm_mech_oid)) {
235 major_status = gss_add_oid_set_member(&minor_status,
236 &m->gm_mech_oid, &_gss_mech_oids);
238 free(m->gm_mech_oid.elements);
245 SYM(init_sec_context);
246 SYM(accept_sec_context);
247 SYM(process_context_token);
248 SYM(delete_sec_context);
262 SYM(inquire_context);
263 SYM(wrap_size_limit);
265 SYM(inquire_cred_by_mech);
266 SYM(export_sec_context);
267 SYM(import_sec_context);
268 SYM(inquire_names_for_mech);
269 SYM(inquire_mechs_for_name);
270 SYM(canonicalize_name);
272 OPTSYM2(gsskrb5_register_acceptor_identity,
273 gm_krb5_register_acceptor_identity);
274 OPTSYM(krb5_copy_ccache);
275 OPTSYM(krb5_compat_des3_mic);
277 SLIST_INSERT_HEAD(&_gss_mechs, m, gm_link);
282 free(m->gm_mech_oid.elements);
290 struct _gss_mech_switch *
291 _gss_find_mech_switch(gss_OID mech)
293 struct _gss_mech_switch *m;
296 SLIST_FOREACH(m, &_gss_mechs, gm_link) {
297 if (_gss_oid_equal(&m->gm_mech_oid, mech))