1 /* 2 * Copyright (c) 1997 - 2002 Kungliga Tekniska H�gskolan 3 * (Royal Institute of Technology, Stockholm, Sweden). 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * 3. Neither the name of the Institute nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include "krb5_locl.h" 35 36 RCSID("$Id: context.c,v 1.73 2002/02/12 08:24:08 joda Exp $"); 37 38 #define INIT_FIELD(C, T, E, D, F) \ 39 (C)->E = krb5_config_get_ ## T ## _default ((C), NULL, (D), \ 40 "libdefaults", F, NULL) 41 42 /* 43 * Set the list of etypes `ret_etypes' from the configuration variable 44 * `name' 45 */ 46 47 static krb5_error_code 48 set_etypes (krb5_context context, 49 const char *name, 50 krb5_enctype **ret_enctypes) 51 { 52 char **etypes_str; 53 krb5_enctype *etypes; 54 55 etypes_str = krb5_config_get_strings(context, NULL, "libdefaults", 56 name, NULL); 57 if(etypes_str){ 58 int i, j, k; 59 for(i = 0; etypes_str[i]; i++); 60 etypes = malloc((i+1) * sizeof(*etypes)); 61 if (etypes == NULL) { 62 krb5_config_free_strings (etypes_str); 63 krb5_set_error_string (context, "malloc: out of memory"); 64 return ENOMEM; 65 } 66 for(j = 0, k = 0; j < i; j++) { 67 if(krb5_string_to_enctype(context, etypes_str[j], &etypes[k]) == 0) 68 k++; 69 } 70 etypes[k] = ETYPE_NULL; 71 krb5_config_free_strings(etypes_str); 72 *ret_enctypes = etypes; 73 } 74 return 0; 75 } 76 77 /* 78 * read variables from the configuration file and set in `context' 79 */ 80 81 static krb5_error_code 82 init_context_from_config_file(krb5_context context) 83 { 84 krb5_error_code ret; 85 const char * tmp; 86 87 INIT_FIELD(context, time, max_skew, 5 * 60, "clockskew"); 88 INIT_FIELD(context, time, kdc_timeout, 3, "kdc_timeout"); 89 INIT_FIELD(context, int, max_retries, 3, "max_retries"); 90 91 INIT_FIELD(context, string, http_proxy, NULL, "http_proxy"); 92 93 set_etypes (context, "default_etypes", &context->etypes); 94 set_etypes (context, "default_etypes_des", &context->etypes_des); 95 96 /* default keytab name */ 97 INIT_FIELD(context, string, default_keytab, 98 KEYTAB_DEFAULT, "default_keytab_name"); 99 100 INIT_FIELD(context, string, default_keytab_modify, 101 NULL, "default_keytab_modify_name"); 102 103 INIT_FIELD(context, string, time_fmt, 104 "%Y-%m-%dT%H:%M:%S", "time_format"); 105 106 INIT_FIELD(context, string, date_fmt, 107 "%Y-%m-%d", "date_format"); 108 109 INIT_FIELD(context, bool, log_utc, 110 FALSE, "log_utc"); 111 112 113 114 /* init dns-proxy slime */ 115 tmp = krb5_config_get_string(context, NULL, "libdefaults", 116 "dns_proxy", NULL); 117 if(tmp) 118 roken_gethostby_setup(context->http_proxy, tmp); 119 context->default_realms = NULL; 120 121 { 122 krb5_addresses addresses; 123 char **adr, **a; 124 adr = krb5_config_get_strings(context, NULL, 125 "libdefaults", 126 "extra_addresses", 127 NULL); 128 memset(&addresses, 0, sizeof(addresses)); 129 for(a = adr; a && *a; a++) { 130 ret = krb5_parse_address(context, *a, &addresses); 131 if (ret == 0) { 132 krb5_add_extra_addresses(context, &addresses); 133 krb5_free_addresses(context, &addresses); 134 } 135 } 136 krb5_config_free_strings(adr); 137 138 adr = krb5_config_get_strings(context, NULL, 139 "libdefaults", 140 "ignore_addresses", 141 NULL); 142 memset(&addresses, 0, sizeof(addresses)); 143 for(a = adr; a && *a; a++) { 144 ret = krb5_parse_address(context, *a, &addresses); 145 if (ret == 0) { 146 krb5_add_ignore_addresses(context, &addresses); 147 krb5_free_addresses(context, &addresses); 148 } 149 } 150 krb5_config_free_strings(adr); 151 } 152 153 INIT_FIELD(context, bool, scan_interfaces, TRUE, "scan_interfaces"); 154 INIT_FIELD(context, bool, srv_lookup, TRUE, "srv_lookup"); 155 INIT_FIELD(context, int, fcache_vno, 0, "fcache_version"); 156 157 context->cc_ops = NULL; 158 context->num_cc_ops = 0; 159 krb5_cc_register(context, &krb5_fcc_ops, TRUE); 160 krb5_cc_register(context, &krb5_mcc_ops, TRUE); 161 162 context->num_kt_types = 0; 163 context->kt_types = NULL; 164 krb5_kt_register (context, &krb5_fkt_ops); 165 krb5_kt_register (context, &krb5_mkt_ops); 166 krb5_kt_register (context, &krb5_akf_ops); 167 krb5_kt_register (context, &krb4_fkt_ops); 168 krb5_kt_register (context, &krb5_srvtab_fkt_ops); 169 krb5_kt_register (context, &krb5_any_ops); 170 return 0; 171 } 172 173 krb5_error_code 174 krb5_init_context(krb5_context *context) 175 { 176 krb5_context p; 177 const char *config_file = NULL; 178 krb5_config_section *tmp_cf; 179 krb5_error_code ret; 180 181 ALLOC(p, 1); 182 if(!p) 183 return ENOMEM; 184 memset(p, 0, sizeof(krb5_context_data)); 185 186 /* init error tables */ 187 krb5_init_ets(p); 188 189 if(!issuid()) 190 config_file = getenv("KRB5_CONFIG"); 191 if (config_file == NULL) 192 config_file = krb5_config_file; 193 194 ret = krb5_config_parse_file (p, config_file, &tmp_cf); 195 196 if (ret == 0) 197 p->cf = tmp_cf; 198 #if 0 199 else 200 krb5_warnx (p, "Unable to parse config file %s. Ignoring.", 201 config_file); /* XXX */ 202 #endif 203 204 ret = init_context_from_config_file(p); 205 if(ret) { 206 krb5_free_context(p); 207 return ret; 208 } 209 210 *context = p; 211 return 0; 212 } 213 214 void 215 krb5_free_context(krb5_context context) 216 { 217 int i; 218 219 free(context->etypes); 220 free(context->etypes_des); 221 krb5_free_host_realm (context, context->default_realms); 222 krb5_config_file_free (context, context->cf); 223 free_error_table (context->et_list); 224 for(i = 0; i < context->num_cc_ops; ++i) 225 free(context->cc_ops[i].prefix); 226 free(context->cc_ops); 227 free(context->kt_types); 228 krb5_clear_error_string(context); 229 if(context->warn_dest != NULL) 230 krb5_closelog(context, context->warn_dest); 231 krb5_set_extra_addresses(context, NULL); 232 krb5_set_ignore_addresses(context, NULL); 233 free(context); 234 } 235 236 /* 237 * set `etype' to a malloced list of the default enctypes 238 */ 239 240 static krb5_error_code 241 default_etypes(krb5_context context, krb5_enctype **etype) 242 { 243 krb5_enctype p[] = { 244 ETYPE_DES3_CBC_SHA1, 245 ETYPE_DES3_CBC_MD5, 246 ETYPE_ARCFOUR_HMAC_MD5, 247 ETYPE_DES_CBC_MD5, 248 ETYPE_DES_CBC_MD4, 249 ETYPE_DES_CBC_CRC, 250 ETYPE_NULL 251 }; 252 253 *etype = malloc(sizeof(p)); 254 if(*etype == NULL) { 255 krb5_set_error_string (context, "malloc: out of memory"); 256 return ENOMEM; 257 } 258 memcpy(*etype, p, sizeof(p)); 259 return 0; 260 } 261 262 krb5_error_code 263 krb5_set_default_in_tkt_etypes(krb5_context context, 264 const krb5_enctype *etypes) 265 { 266 int i; 267 krb5_enctype *p = NULL; 268 269 if(etypes) { 270 for (i = 0; etypes[i]; ++i) 271 if(!krb5_enctype_valid(context, etypes[i])) { 272 krb5_set_error_string(context, "enctype %d not supported", 273 etypes[i]); 274 return KRB5_PROG_ETYPE_NOSUPP; 275 } 276 ++i; 277 ALLOC(p, i); 278 if(!p) { 279 krb5_set_error_string (context, "malloc: out of memory"); 280 return ENOMEM; 281 } 282 memmove(p, etypes, i * sizeof(krb5_enctype)); 283 } 284 if(context->etypes) 285 free(context->etypes); 286 context->etypes = p; 287 return 0; 288 } 289 290 291 krb5_error_code 292 krb5_get_default_in_tkt_etypes(krb5_context context, 293 krb5_enctype **etypes) 294 { 295 krb5_enctype *p; 296 int i; 297 krb5_error_code ret; 298 299 if(context->etypes) { 300 for(i = 0; context->etypes[i]; i++); 301 ++i; 302 ALLOC(p, i); 303 if(!p) { 304 krb5_set_error_string (context, "malloc: out of memory"); 305 return ENOMEM; 306 } 307 memmove(p, context->etypes, i * sizeof(krb5_enctype)); 308 } else { 309 ret = default_etypes(context, &p); 310 if (ret) 311 return ret; 312 } 313 *etypes = p; 314 return 0; 315 } 316 317 const char * 318 krb5_get_err_text(krb5_context context, krb5_error_code code) 319 { 320 const char *p = NULL; 321 if(context != NULL) 322 p = com_right(context->et_list, code); 323 if(p == NULL) 324 p = strerror(code); 325 return p; 326 } 327 328 void 329 krb5_init_ets(krb5_context context) 330 { 331 if(context->et_list == NULL){ 332 krb5_add_et_list(context, initialize_krb5_error_table_r); 333 krb5_add_et_list(context, initialize_asn1_error_table_r); 334 krb5_add_et_list(context, initialize_heim_error_table_r); 335 krb5_add_et_list(context, initialize_k524_error_table_r); 336 } 337 } 338 339 void 340 krb5_set_use_admin_kdc (krb5_context context, krb5_boolean flag) 341 { 342 context->use_admin_kdc = flag; 343 } 344 345 krb5_boolean 346 krb5_get_use_admin_kdc (krb5_context context) 347 { 348 return context->use_admin_kdc; 349 } 350 351 krb5_error_code 352 krb5_add_extra_addresses(krb5_context context, krb5_addresses *addresses) 353 { 354 355 if(context->extra_addresses) 356 return krb5_append_addresses(context, 357 context->extra_addresses, addresses); 358 else 359 return krb5_set_extra_addresses(context, addresses); 360 } 361 362 krb5_error_code 363 krb5_set_extra_addresses(krb5_context context, const krb5_addresses *addresses) 364 { 365 if(context->extra_addresses) 366 krb5_free_addresses(context, context->extra_addresses); 367 368 if(addresses == NULL) { 369 if(context->extra_addresses != NULL) { 370 free(context->extra_addresses); 371 context->extra_addresses = NULL; 372 } 373 return 0; 374 } 375 if(context->extra_addresses == NULL) { 376 context->extra_addresses = malloc(sizeof(*context->extra_addresses)); 377 if(context->extra_addresses == NULL) { 378 krb5_set_error_string (context, "malloc: out of memory"); 379 return ENOMEM; 380 } 381 } 382 return krb5_copy_addresses(context, addresses, context->extra_addresses); 383 } 384 385 krb5_error_code 386 krb5_get_extra_addresses(krb5_context context, krb5_addresses *addresses) 387 { 388 if(context->extra_addresses == NULL) { 389 memset(addresses, 0, sizeof(*addresses)); 390 return 0; 391 } 392 return krb5_copy_addresses(context,context->extra_addresses, addresses); 393 } 394 395 krb5_error_code 396 krb5_add_ignore_addresses(krb5_context context, krb5_addresses *addresses) 397 { 398 399 if(context->ignore_addresses) 400 return krb5_append_addresses(context, 401 context->ignore_addresses, addresses); 402 else 403 return krb5_set_ignore_addresses(context, addresses); 404 } 405 406 krb5_error_code 407 krb5_set_ignore_addresses(krb5_context context, const krb5_addresses *addresses) 408 { 409 if(context->ignore_addresses) 410 krb5_free_addresses(context, context->ignore_addresses); 411 if(addresses == NULL) { 412 if(context->ignore_addresses != NULL) { 413 free(context->ignore_addresses); 414 context->ignore_addresses = NULL; 415 } 416 return 0; 417 } 418 if(context->ignore_addresses == NULL) { 419 context->ignore_addresses = malloc(sizeof(*context->ignore_addresses)); 420 if(context->ignore_addresses == NULL) { 421 krb5_set_error_string (context, "malloc: out of memory"); 422 return ENOMEM; 423 } 424 } 425 return krb5_copy_addresses(context, addresses, context->ignore_addresses); 426 } 427 428 krb5_error_code 429 krb5_get_ignore_addresses(krb5_context context, krb5_addresses *addresses) 430 { 431 if(context->ignore_addresses == NULL) { 432 memset(addresses, 0, sizeof(*addresses)); 433 return 0; 434 } 435 return krb5_copy_addresses(context, context->ignore_addresses, addresses); 436 } 437 438 krb5_error_code 439 krb5_set_fcache_version(krb5_context context, int version) 440 { 441 context->fcache_vno = version; 442 return 0; 443 } 444 445 krb5_error_code 446 krb5_get_fcache_version(krb5_context context, int *version) 447 { 448 *version = context->fcache_vno; 449 return 0; 450 } 451