Path: blob/main/crypto/heimdal/lib/gssapi/mech/gss_mech_switch.c
34907 views
/*-1* Copyright (c) 2005 Doug Rabson2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*25* $FreeBSD: src/lib/libgssapi/gss_mech_switch.c,v 1.2 2006/02/04 09:40:21 dfr Exp $26*/2728#include "mech_locl.h"29#include <heim_threads.h>3031#ifndef _PATH_GSS_MECH32#define _PATH_GSS_MECH "/etc/gss/mech"33#endif3435struct _gss_mech_switch_list _gss_mechs = { NULL } ;36gss_OID_set _gss_mech_oids;37static HEIMDAL_MUTEX _gss_mech_mutex = HEIMDAL_MUTEX_INITIALIZER;3839/*40* Convert a string containing an OID in 'dot' form41* (e.g. 1.2.840.113554.1.2.2) to a gss_OID.42*/43static int44_gss_string_to_oid(const char* s, gss_OID oid)45{46int number_count, i, j;47size_t byte_count;48const char *p, *q;49char *res;5051oid->length = 0;52oid->elements = NULL;5354/*55* First figure out how many numbers in the oid, then56* calculate the compiled oid size.57*/58number_count = 0;59for (p = s; p; p = q) {60q = strchr(p, '.');61if (q) q = q + 1;62number_count++;63}6465/*66* The first two numbers are in the first byte and each67* subsequent number is encoded in a variable byte sequence.68*/69if (number_count < 2)70return (EINVAL);7172/*73* We do this in two passes. The first pass, we just figure74* out the size. Second time around, we actually encode the75* number.76*/77res = 0;78for (i = 0; i < 2; i++) {79byte_count = 0;80for (p = s, j = 0; p; p = q, j++) {81unsigned int number = 0;8283/*84* Find the end of this number.85*/86q = strchr(p, '.');87if (q) q = q + 1;8889/*90* Read the number of of the string. Don't91* bother with anything except base ten.92*/93while (*p && *p != '.') {94number = 10 * number + (*p - '0');95p++;96}9798/*99* Encode the number. The first two numbers100* are packed into the first byte. Subsequent101* numbers are encoded in bytes seven bits at102* a time with the last byte having the high103* bit set.104*/105if (j == 0) {106if (res)107*res = number * 40;108} else if (j == 1) {109if (res) {110*res += number;111res++;112}113byte_count++;114} else if (j >= 2) {115/*116* The number is encoded in seven bit chunks.117*/118unsigned int t;119unsigned int bytes;120121bytes = 0;122for (t = number; t; t >>= 7)123bytes++;124if (bytes == 0) bytes = 1;125while (bytes) {126if (res) {127int bit = 7*(bytes-1);128129*res = (number >> bit) & 0x7f;130if (bytes != 1)131*res |= 0x80;132res++;133}134byte_count++;135bytes--;136}137}138}139if (byte_count == 0)140return EINVAL;141if (!res) {142res = malloc(byte_count);143if (!res)144return (ENOMEM);145oid->length = byte_count;146oid->elements = res;147}148}149150return (0);151}152153#define SYM(name) \154do { \155m->gm_mech.gm_ ## name = dlsym(so, "gss_" #name); \156if (!m->gm_mech.gm_ ## name || \157m->gm_mech.gm_ ##name == gss_ ## name) { \158fprintf(stderr, "can't find symbol gss_" #name "\n"); \159goto bad; \160} \161} while (0)162163#define OPTSYM(name) \164do { \165m->gm_mech.gm_ ## name = dlsym(so, "gss_" #name); \166if (m->gm_mech.gm_ ## name == gss_ ## name) \167m->gm_mech.gm_ ## name = NULL; \168} while (0)169170#define OPTSPISYM(name) \171do { \172m->gm_mech.gm_ ## name = dlsym(so, "gssspi_" #name); \173} while (0)174175#define COMPATSYM(name) \176do { \177m->gm_mech.gm_compat->gmc_ ## name = dlsym(so, "gss_" #name); \178if (m->gm_mech.gm_compat->gmc_ ## name == gss_ ## name) \179m->gm_mech.gm_compat->gmc_ ## name = NULL; \180} while (0)181182#define COMPATSPISYM(name) \183do { \184m->gm_mech.gm_compat->gmc_ ## name = dlsym(so, "gssspi_" #name);\185if (m->gm_mech.gm_compat->gmc_ ## name == gss_ ## name) \186m->gm_mech.gm_compat->gmc_ ## name = NULL; \187} while (0)188189/*190*191*/192static int193add_builtin(gssapi_mech_interface mech)194{195struct _gss_mech_switch *m;196OM_uint32 minor_status;197198/* not registering any mech is ok */199if (mech == NULL)200return 0;201202m = calloc(1, sizeof(*m));203if (m == NULL)204return ENOMEM;205m->gm_so = NULL;206m->gm_mech = *mech;207m->gm_mech_oid = mech->gm_mech_oid; /* XXX */208gss_add_oid_set_member(&minor_status,209&m->gm_mech.gm_mech_oid, &_gss_mech_oids);210211/* pick up the oid sets of names */212213if (m->gm_mech.gm_inquire_names_for_mech)214(*m->gm_mech.gm_inquire_names_for_mech)(&minor_status,215&m->gm_mech.gm_mech_oid, &m->gm_name_types);216217if (m->gm_name_types == NULL)218gss_create_empty_oid_set(&minor_status, &m->gm_name_types);219220HEIM_SLIST_INSERT_HEAD(&_gss_mechs, m, gm_link);221return 0;222}223224/*225* Load the mechanisms file (/etc/gss/mech).226*/227void228_gss_load_mech(void)229{230OM_uint32 major_status, minor_status;231FILE *fp;232char buf[256];233char *p;234char *name, *oid, *lib, *kobj;235struct _gss_mech_switch *m;236void *so;237gss_OID_desc mech_oid;238int found;239240241HEIMDAL_MUTEX_lock(&_gss_mech_mutex);242243if (HEIM_SLIST_FIRST(&_gss_mechs)) {244HEIMDAL_MUTEX_unlock(&_gss_mech_mutex);245return;246}247248major_status = gss_create_empty_oid_set(&minor_status,249&_gss_mech_oids);250if (major_status) {251HEIMDAL_MUTEX_unlock(&_gss_mech_mutex);252return;253}254255add_builtin(__gss_krb5_initialize());256add_builtin(__gss_spnego_initialize());257add_builtin(__gss_ntlm_initialize());258259#ifdef HAVE_DLOPEN260fp = fopen(_PATH_GSS_MECH, "r");261if (!fp) {262HEIMDAL_MUTEX_unlock(&_gss_mech_mutex);263return;264}265rk_cloexec_file(fp);266267while (fgets(buf, sizeof(buf), fp)) {268_gss_mo_init *mi;269270if (*buf == '#')271continue;272p = buf;273name = strsep(&p, "\t\n ");274if (p) while (isspace((unsigned char)*p)) p++;275oid = strsep(&p, "\t\n ");276if (p) while (isspace((unsigned char)*p)) p++;277lib = strsep(&p, "\t\n ");278if (p) while (isspace((unsigned char)*p)) p++;279kobj = strsep(&p, "\t\n ");280if (!name || !oid || !lib || !kobj)281continue;282283if (_gss_string_to_oid(oid, &mech_oid))284continue;285286/*287* Check for duplicates, already loaded mechs.288*/289found = 0;290HEIM_SLIST_FOREACH(m, &_gss_mechs, gm_link) {291if (gss_oid_equal(&m->gm_mech.gm_mech_oid, &mech_oid)) {292found = 1;293free(mech_oid.elements);294break;295}296}297if (found)298continue;299300#ifndef RTLD_LOCAL301#define RTLD_LOCAL 0302#endif303304#ifndef RTLD_GROUP305#define RTLD_GROUP 0306#endif307308so = dlopen(lib, RTLD_LAZY | RTLD_LOCAL | RTLD_GROUP);309if (so == NULL) {310/* fprintf(stderr, "dlopen: %s\n", dlerror()); */311goto bad;312}313314m = calloc(1, sizeof(*m));315if (m == NULL)316goto bad;317318m->gm_so = so;319m->gm_mech.gm_mech_oid = mech_oid;320m->gm_mech.gm_flags = 0;321m->gm_mech.gm_compat = calloc(1, sizeof(struct gss_mech_compat_desc_struct));322if (m->gm_mech.gm_compat == NULL)323goto bad;324325major_status = gss_add_oid_set_member(&minor_status,326&m->gm_mech.gm_mech_oid, &_gss_mech_oids);327if (GSS_ERROR(major_status))328goto bad;329330SYM(acquire_cred);331SYM(release_cred);332SYM(init_sec_context);333SYM(accept_sec_context);334SYM(process_context_token);335SYM(delete_sec_context);336SYM(context_time);337SYM(get_mic);338SYM(verify_mic);339SYM(wrap);340SYM(unwrap);341SYM(display_status);342SYM(indicate_mechs);343SYM(compare_name);344SYM(display_name);345SYM(import_name);346SYM(export_name);347SYM(release_name);348SYM(inquire_cred);349SYM(inquire_context);350SYM(wrap_size_limit);351SYM(add_cred);352SYM(inquire_cred_by_mech);353SYM(export_sec_context);354SYM(import_sec_context);355SYM(inquire_names_for_mech);356SYM(inquire_mechs_for_name);357SYM(canonicalize_name);358SYM(duplicate_name);359OPTSYM(inquire_cred_by_oid);360OPTSYM(inquire_sec_context_by_oid);361OPTSYM(set_sec_context_option);362OPTSPISYM(set_cred_option);363OPTSYM(pseudo_random);364OPTSYM(wrap_iov);365OPTSYM(unwrap_iov);366OPTSYM(wrap_iov_length);367OPTSYM(store_cred);368OPTSYM(export_cred);369OPTSYM(import_cred);370#if 0371OPTSYM(acquire_cred_ext);372OPTSYM(iter_creds);373OPTSYM(destroy_cred);374OPTSYM(cred_hold);375OPTSYM(cred_unhold);376OPTSYM(cred_label_get);377OPTSYM(cred_label_set);378#endif379OPTSYM(display_name_ext);380OPTSYM(inquire_name);381OPTSYM(get_name_attribute);382OPTSYM(set_name_attribute);383OPTSYM(delete_name_attribute);384OPTSYM(export_name_composite);385OPTSYM(pname_to_uid);386OPTSPISYM(authorize_localname);387388mi = dlsym(so, "gss_mo_init");389if (mi != NULL) {390major_status = mi(&minor_status, &mech_oid,391&m->gm_mech.gm_mo, &m->gm_mech.gm_mo_num);392if (GSS_ERROR(major_status))393goto bad;394} else {395/* API-as-SPI compatibility */396COMPATSYM(inquire_saslname_for_mech);397COMPATSYM(inquire_mech_for_saslname);398COMPATSYM(inquire_attrs_for_mech);399COMPATSPISYM(acquire_cred_with_password);400}401402/* pick up the oid sets of names */403404if (m->gm_mech.gm_inquire_names_for_mech)405(*m->gm_mech.gm_inquire_names_for_mech)(&minor_status,406&m->gm_mech.gm_mech_oid, &m->gm_name_types);407408if (m->gm_name_types == NULL)409gss_create_empty_oid_set(&minor_status, &m->gm_name_types);410411HEIM_SLIST_INSERT_HEAD(&_gss_mechs, m, gm_link);412continue;413414bad:415if (m != NULL) {416free(m->gm_mech.gm_compat);417free(m->gm_mech.gm_mech_oid.elements);418free(m);419}420dlclose(so);421continue;422}423fclose(fp);424#endif425HEIMDAL_MUTEX_unlock(&_gss_mech_mutex);426}427428gssapi_mech_interface429__gss_get_mechanism(gss_const_OID mech)430{431struct _gss_mech_switch *m;432433_gss_load_mech();434HEIM_SLIST_FOREACH(m, &_gss_mechs, gm_link) {435if (gss_oid_equal(&m->gm_mech.gm_mech_oid, mech))436return &m->gm_mech;437}438return NULL;439}440441442