1/*- 2 * Copyright (c) 2005 Doug Rabson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: src/lib/libgssapi/gss_mech_switch.c,v 1.2 2006/02/04 09:40:21 dfr Exp $ 27 */ 28 29#include "mech_locl.h" 30#include <heim_threads.h> 31 32#ifndef _PATH_GSS_MECH 33#define _PATH_GSS_MECH "/etc/gss/mech" 34#endif 35 36struct _gss_mech_switch_list _gss_mechs = { NULL } ; 37gss_OID_set _gss_mech_oids; 38static HEIMDAL_MUTEX _gss_mech_mutex = HEIMDAL_MUTEX_INITIALIZER; 39 40/* 41 * Convert a string containing an OID in 'dot' form 42 * (e.g. 1.2.840.113554.1.2.2) to a gss_OID. 43 */ 44static int 45_gss_string_to_oid(const char* s, gss_OID oid) 46{ 47 int number_count, i, j; 48 size_t byte_count; 49 const char *p, *q; 50 char *res; 51 52 oid->length = 0; 53 oid->elements = NULL; 54 55 /* 56 * First figure out how many numbers in the oid, then 57 * calculate the compiled oid size. 58 */ 59 number_count = 0; 60 for (p = s; p; p = q) { 61 q = strchr(p, '.'); 62 if (q) q = q + 1; 63 number_count++; 64 } 65 66 /* 67 * The first two numbers are in the first byte and each 68 * subsequent number is encoded in a variable byte sequence. 69 */ 70 if (number_count < 2) 71 return (EINVAL); 72 73 /* 74 * We do this in two passes. The first pass, we just figure 75 * out the size. Second time around, we actually encode the 76 * number. 77 */ 78 res = 0; 79 for (i = 0; i < 2; i++) { 80 byte_count = 0; 81 for (p = s, j = 0; p; p = q, j++) { 82 unsigned int number = 0; 83 84 /* 85 * Find the end of this number. 86 */ 87 q = strchr(p, '.'); 88 if (q) q = q + 1; 89 90 /* 91 * Read the number of of the string. Don't 92 * bother with anything except base ten. 93 */ 94 while (*p && *p != '.') { 95 number = 10 * number + (*p - '0'); 96 p++; 97 } 98 99 /* 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 104 * bit set. 105 */ 106 if (j == 0) { 107 if (res) 108 *res = number * 40; 109 } else if (j == 1) { 110 if (res) { 111 *res += number; 112 res++; 113 } 114 byte_count++; 115 } else if (j >= 2) { 116 /* 117 * The number is encoded in seven bit chunks. 118 */ 119 unsigned int t; 120 unsigned int bytes; 121 122 bytes = 0; 123 for (t = number; t; t >>= 7) 124 bytes++; 125 if (bytes == 0) bytes = 1; 126 while (bytes) { 127 if (res) { 128 int bit = 7*(bytes-1); 129 130 *res = (number >> bit) & 0x7f; 131 if (bytes != 1) 132 *res |= 0x80; 133 res++; 134 } 135 byte_count++; 136 bytes--; 137 } 138 } 139 } 140 if (byte_count == 0) 141 return EINVAL; 142 if (!res) { 143 res = malloc(byte_count); 144 if (!res) 145 return (ENOMEM); 146 oid->length = (OM_uint32)byte_count; 147 oid->elements = res; 148 } 149 } 150 151 return (0); 152} 153 154#define SYM(name) \ 155do { \ 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"); \ 160 goto bad; \ 161 } \ 162} while (0) 163 164#define OPTSYM(name) \ 165do { \ 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; \ 169} while (0) 170 171#define OPTSPISYM(name) \ 172do { \ 173 m->gm_mech.gm_ ## name = dlsym(so, "gssspi_" #name); \ 174} while (0) 175 176#define COMPATSYM(name) \ 177do { \ 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; \ 181} while (0) 182 183#define COMPATSPISYM(name) \ 184do { \ 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; \ 188} while (0) 189 190/* 191 * 192 */ 193static int 194add_builtin(gssapi_mech_interface mech) 195{ 196 struct _gss_mech_switch *m; 197 OM_uint32 minor_status; 198 199 /* not registering any mech is ok */ 200 if (mech == NULL) 201 return 0; 202 203 m = calloc(1, sizeof(*m)); 204 if (m == NULL) 205 return ENOMEM; 206 m->gm_so = NULL; 207 m->gm_mech = *mech; 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); 211 212 /* pick up the oid sets of names */ 213 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); 217 218 /* give sane defaults */ 219 if (m->gm_name_types == NULL) { 220 gss_create_empty_oid_set(&minor_status, &m->gm_name_types); 221 gss_add_oid_set_member(&minor_status, GSS_C_NT_USER_NAME, &m->gm_name_types); 222 gss_add_oid_set_member(&minor_status, GSS_C_NT_HOSTBASED_SERVICE, &m->gm_name_types); 223 } 224 225 HEIM_SLIST_INSERT_HEAD(&_gss_mechs, m, gm_link); 226 return 0; 227} 228 229/* 230 * Load the mechanisms file (/etc/gss/mech). 231 */ 232void 233_gss_load_mech(void) 234{ 235 OM_uint32 major_status, minor_status; 236 FILE *fp; 237 char buf[256]; 238 char *p; 239 char *name, *oid, *lib, *kobj; 240 struct _gss_mech_switch *m; 241 void *so; 242 gss_OID_desc mech_oid; 243 int found; 244 245 246 HEIMDAL_MUTEX_lock(&_gss_mech_mutex); 247 248 if (HEIM_SLIST_FIRST(&_gss_mechs)) { 249 HEIMDAL_MUTEX_unlock(&_gss_mech_mutex); 250 return; 251 } 252 253 major_status = gss_create_empty_oid_set(&minor_status, 254 &_gss_mech_oids); 255 if (major_status) { 256 HEIMDAL_MUTEX_unlock(&_gss_mech_mutex); 257 return; 258 } 259 260 /* 261 * order is reverse order of where they will appear in 262 * gss_indicate_mechs(), kerberos is first in the list to 263 * please SAP w/o configuration on what mech to use. 264 */ 265 add_builtin(__gss_pku2u_initialize()); 266 add_builtin(__gss_iakerb_initialize()); 267 add_builtin(__gss_ntlm_initialize()); 268 add_builtin(__gss_scram_initialize()); 269 add_builtin(__gss_netlogon_initialize()); 270 add_builtin(__gss_spnego_initialize()); 271 add_builtin(__gss_krb5_initialize()); 272 273#ifdef HAVE_DLOPEN 274 fp = fopen(_PATH_GSS_MECH, "r"); 275 if (!fp) { 276 HEIMDAL_MUTEX_unlock(&_gss_mech_mutex); 277 return; 278 } 279 rk_cloexec_file(fp); 280 281 while (fgets(buf, sizeof(buf), fp)) { 282 _gss_mo_init *mi; 283 284 if (*buf == '#') 285 continue; 286 p = buf; 287 name = strsep(&p, "\t\n "); 288 if (p) while (isspace((unsigned char)*p)) p++; 289 oid = strsep(&p, "\t\n "); 290 if (p) while (isspace((unsigned char)*p)) p++; 291 lib = strsep(&p, "\t\n "); 292 if (p) while (isspace((unsigned char)*p)) p++; 293 kobj = strsep(&p, "\t\n "); 294 if (!name || !oid || !lib || !kobj) 295 continue; 296 297 if (_gss_string_to_oid(oid, &mech_oid)) 298 continue; 299 300 /* 301 * Check for duplicates, already loaded mechs. 302 */ 303 found = 0; 304 HEIM_SLIST_FOREACH(m, &_gss_mechs, gm_link) { 305 if (gss_oid_equal(&m->gm_mech.gm_mech_oid, &mech_oid)) { 306 found = 1; 307 free(mech_oid.elements); 308 break; 309 } 310 } 311 if (found) 312 continue; 313 314#ifndef RTLD_LOCAL 315#define RTLD_LOCAL 0 316#endif 317 318#ifndef RTLD_GROUP 319#define RTLD_GROUP 0 320#endif 321 322 so = dlopen(lib, RTLD_LAZY | RTLD_LOCAL | RTLD_GROUP); 323 if (so == NULL) { 324/* fprintf(stderr, "dlopen: %s\n", dlerror()); */ 325 goto bad; 326 } 327 328 m = calloc(1, sizeof(*m)); 329 if (m == NULL) 330 goto bad; 331 332 m->gm_so = so; 333 m->gm_mech_oid = mech_oid; 334 m->gm_mech.gm_name = strdup(name); 335 m->gm_mech.gm_mech_oid = mech_oid; 336 m->gm_mech.gm_flags = 0; 337 m->gm_mech.gm_compat = calloc(1, sizeof(struct gss_mech_compat_desc_struct)); 338 if (m->gm_mech.gm_compat == NULL) 339 goto bad; 340 341 major_status = gss_add_oid_set_member(&minor_status, 342 &m->gm_mech.gm_mech_oid, &_gss_mech_oids); 343 if (GSS_ERROR(major_status)) 344 goto bad; 345 346 SYM(acquire_cred); 347 SYM(release_cred); 348 SYM(init_sec_context); 349 SYM(accept_sec_context); 350 SYM(process_context_token); 351 SYM(delete_sec_context); 352 SYM(context_time); 353 SYM(get_mic); 354 SYM(verify_mic); 355 SYM(wrap); 356 SYM(unwrap); 357 SYM(display_status); 358 SYM(indicate_mechs); 359 SYM(compare_name); 360 SYM(display_name); 361 SYM(import_name); 362 SYM(export_name); 363 SYM(release_name); 364 SYM(inquire_cred); 365 SYM(inquire_context); 366 SYM(wrap_size_limit); 367 SYM(add_cred); 368 SYM(inquire_cred_by_mech); 369 SYM(export_sec_context); 370 SYM(import_sec_context); 371 SYM(inquire_names_for_mech); 372 SYM(inquire_mechs_for_name); 373 SYM(canonicalize_name); 374 SYM(duplicate_name); 375 OPTSYM(inquire_cred_by_oid); 376 OPTSYM(inquire_sec_context_by_oid); 377 OPTSYM(set_sec_context_option); 378 OPTSPISYM(set_cred_option); 379 OPTSYM(pseudo_random); 380 OPTSYM(wrap_iov); 381 OPTSYM(unwrap_iov); 382 OPTSYM(wrap_iov_length); 383 OPTSYM(store_cred); 384 OPTSYM(export_cred); 385 OPTSYM(import_cred); 386#if 0 387 OPTSYM(acquire_cred_ext); 388 OPTSYM(iter_creds); 389 OPTSYM(destroy_cred); 390 OPTSYM(cred_hold); 391 OPTSYM(cred_unhold); 392 OPTSYM(cred_label_get); 393 OPTSYM(cred_label_set); 394#endif 395 OPTSYM(display_name_ext); 396 OPTSYM(inquire_name); 397 OPTSYM(get_name_attribute); 398 OPTSYM(set_name_attribute); 399 OPTSYM(delete_name_attribute); 400 OPTSYM(export_name_composite); 401 OPTSYM(pname_to_uid); 402 OPTSPISYM(authorize_localname); 403 404 mi = dlsym(so, "gss_mo_init"); 405 if (mi != NULL) { 406 major_status = mi(&minor_status, &mech_oid, 407 &m->gm_mech.gm_mo, &m->gm_mech.gm_mo_num); 408 if (GSS_ERROR(major_status)) 409 goto bad; 410 } else { 411 /* API-as-SPI compatibility */ 412 COMPATSYM(inquire_saslname_for_mech); 413 COMPATSYM(inquire_mech_for_saslname); 414 COMPATSYM(inquire_attrs_for_mech); 415 COMPATSPISYM(acquire_cred_with_password); 416 } 417 418 /* pick up the oid sets of names */ 419 420 if (m->gm_mech.gm_inquire_names_for_mech) 421 (*m->gm_mech.gm_inquire_names_for_mech)(&minor_status, 422 &m->gm_mech.gm_mech_oid, &m->gm_name_types); 423 424 if (m->gm_name_types == NULL) 425 gss_create_empty_oid_set(&minor_status, &m->gm_name_types); 426 427 HEIM_SLIST_INSERT_HEAD(&_gss_mechs, m, gm_link); 428 continue; 429 430 bad: 431 if (m != NULL) { 432 free(m->gm_mech.gm_compat); 433 free(m->gm_mech.gm_mech_oid.elements); 434 free((char *)m->gm_mech.gm_name); 435 free(m); 436 } 437 dlclose(so); 438 continue; 439 } 440 fclose(fp); 441#endif 442 HEIMDAL_MUTEX_unlock(&_gss_mech_mutex); 443} 444 445struct gssapi_mech_interface_desc * 446__gss_get_mechanism(gss_const_OID mech) 447{ 448 struct _gss_mech_switch *m; 449 450 _gss_load_mech(); 451 HEIM_SLIST_FOREACH(m, &_gss_mechs, gm_link) { 452 if (gss_oid_equal(&m->gm_mech.gm_mech_oid, mech)) 453 return &m->gm_mech; 454 } 455 return NULL; 456} 457 458gss_OID 459_gss_mg_support_mechanism(gss_const_OID mech) 460{ 461 struct _gss_mech_switch *m; 462 463 _gss_load_mech(); 464 HEIM_SLIST_FOREACH(m, &_gss_mechs, gm_link) { 465 if (gss_oid_equal(&m->gm_mech.gm_mech_oid, mech)) 466 return &m->gm_mech_oid; 467 } 468 return NULL; 469} 470