2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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
31 #include <gssapi/gssapi.h>
39 #include "mech_switch.h"
42 #ifndef _PATH_GSS_MECH
43 #define _PATH_GSS_MECH "/etc/gss/mech"
46 struct _gss_mech_switch_list _gss_mechs =
47 SLIST_HEAD_INITIALIZER(_gss_mechs);
48 gss_OID_set _gss_mech_oids;
51 * Convert a string containing an OID in 'dot' form
52 * (e.g. 1.2.840.113554.1.2.2) to a gss_OID.
55 _gss_string_to_oid(const char* s, gss_OID oid)
57 int number_count, i, j;
66 * First figure out how many numbers in the oid, then
67 * calculate the compiled oid size.
70 for (p = s; p; p = q) {
77 * The first two numbers are in the first byte and each
78 * subsequent number is encoded in a variable byte sequence.
84 * We do this in two passes. The first pass, we just figure
85 * out the size. Second time around, we actually encode the
89 for (i = 0; i < 2; i++) {
91 for (p = s, j = 0; p; p = q, j++) {
92 unsigned int number = 0;
95 * Find the end of this number.
101 * Read the number of of the string. Don't
102 * bother with anything except base ten.
104 while (*p && *p != '.') {
105 number = 10 * number + (*p - '0');
110 * Encode the number. The first two numbers
111 * are packed into the first byte. Subsequent
112 * numbers are encoded in bytes seven bits at
113 * a time with the last byte having the high
127 * The number is encoded in seven bit chunks.
133 for (t = number; t; t >>= 7)
135 if (bytes == 0) bytes = 1;
138 int bit = 7*(bytes-1);
140 *res = (number >> bit) & 0x7f;
151 res = malloc(byte_count);
154 oid->length = byte_count;
165 snprintf(buf, sizeof(buf), "%s_%s", \
166 m->gm_name_prefix, #name); \
167 m->gm_ ## name = dlsym(so, buf); \
168 if (!m->gm_ ## name) { \
169 fprintf(stderr, "can't find symbol %s\n", buf); \
174 #define OPTSYM(name) \
176 snprintf(buf, sizeof(buf), "%s_%s", \
177 m->gm_name_prefix, #name); \
178 m->gm_ ## name = dlsym(so, buf); \
182 * Load the mechanisms file (/etc/gss/mech).
187 OM_uint32 major_status, minor_status;
191 char *name, *oid, *lib, *kobj;
192 struct _gss_mech_switch *m;
195 const char *(*prefix_fn)(void);
197 if (SLIST_FIRST(&_gss_mechs))
200 major_status = gss_create_empty_oid_set(&minor_status,
205 fp = fopen(_PATH_GSS_MECH, "r");
207 perror(_PATH_GSS_MECH);
212 while (fgets(buf, sizeof(buf), fp)) {
216 name = strsep(&p, "\t\n ");
217 if (p) while (isspace(*p)) p++;
218 oid = strsep(&p, "\t\n ");
219 if (p) while (isspace(*p)) p++;
220 lib = strsep(&p, "\t\n ");
221 if (p) while (isspace(*p)) p++;
222 kobj = strsep(&p, "\t\n ");
223 if (!name || !oid || !lib || !kobj)
226 so = dlopen(lib, RTLD_LOCAL);
228 fprintf(stderr, "dlopen: %s\n", dlerror());
232 m = malloc(sizeof(struct _gss_mech_switch));
236 if (_gss_string_to_oid(oid, &m->gm_mech_oid)) {
241 prefix_fn = (const char *(*)(void))
242 dlsym(so, "_gss_name_prefix");
244 m->gm_name_prefix = prefix_fn();
246 m->gm_name_prefix = "gss";
248 major_status = gss_add_oid_set_member(&minor_status,
249 &m->gm_mech_oid, &_gss_mech_oids);
251 free(m->gm_mech_oid.elements);
258 SYM(init_sec_context);
259 SYM(accept_sec_context);
260 SYM(process_context_token);
261 SYM(delete_sec_context);
268 OPTSYM(indicate_mechs);
275 SYM(inquire_context);
276 SYM(wrap_size_limit);
278 SYM(inquire_cred_by_mech);
279 SYM(export_sec_context);
280 SYM(import_sec_context);
281 SYM(inquire_names_for_mech);
282 SYM(inquire_mechs_for_name);
283 SYM(canonicalize_name);
285 OPTSYM(inquire_sec_context_by_oid);
286 OPTSYM(inquire_cred_by_oid);
287 OPTSYM(set_sec_context_option);
288 OPTSYM(set_cred_option);
289 OPTSYM(pseudo_random);
290 OPTSYM(pname_to_uid);
292 SLIST_INSERT_HEAD(&_gss_mechs, m, gm_link);
297 free(m->gm_mech_oid.elements);
305 struct _gss_mech_switch *
306 _gss_find_mech_switch(gss_OID mech)
308 struct _gss_mech_switch *m;
311 SLIST_FOREACH(m, &_gss_mechs, gm_link) {
312 if (gss_oid_equal(&m->gm_mech_oid, mech))