etc/services - sync with NetBSD-8
[minix.git] / crypto / external / bsd / heimdal / dist / lib / krb5 / context.c
blob71c4f390cca517c3fcc5c9e679cbaa1fbecfb487
1 /* $NetBSD: context.c,v 1.4 2014/04/24 13:45:34 pettai Exp $ */
3 /*
4 * Copyright (c) 1997 - 2010 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
8 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the Institute nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
38 #include "krb5_locl.h"
39 #include <assert.h>
40 #include <krb5/com_err.h>
42 #define INIT_FIELD(C, T, E, D, F) \
43 (C)->E = krb5_config_get_ ## T ## _default ((C), NULL, (D), \
44 "libdefaults", F, NULL)
46 #define INIT_FLAG(C, O, V, D, F) \
47 do { \
48 if (krb5_config_get_bool_default((C), NULL, (D),"libdefaults", F, NULL)) { \
49 (C)->O |= V; \
50 } \
51 } while(0)
54 * Set the list of etypes `ret_etypes' from the configuration variable
55 * `name'
58 static krb5_error_code
59 set_etypes (krb5_context context,
60 const char *name,
61 krb5_enctype **ret_enctypes)
63 char **etypes_str;
64 krb5_enctype *etypes = NULL;
66 etypes_str = krb5_config_get_strings(context, NULL, "libdefaults",
67 name, NULL);
68 if(etypes_str){
69 int i, j, k;
70 for(i = 0; etypes_str[i]; i++);
71 etypes = malloc((i+1) * sizeof(*etypes));
72 if (etypes == NULL) {
73 krb5_config_free_strings (etypes_str);
74 krb5_set_error_message (context, ENOMEM, N_("malloc: out of memory", ""));
75 return ENOMEM;
77 for(j = 0, k = 0; j < i; j++) {
78 krb5_enctype e;
79 if(krb5_string_to_enctype(context, etypes_str[j], &e) != 0)
80 continue;
81 if (krb5_enctype_valid(context, e) != 0)
82 continue;
83 etypes[k++] = e;
85 etypes[k] = ETYPE_NULL;
86 krb5_config_free_strings(etypes_str);
88 *ret_enctypes = etypes;
89 return 0;
93 * read variables from the configuration file and set in `context'
96 static krb5_error_code
97 init_context_from_config_file(krb5_context context)
99 krb5_error_code ret;
100 const char * tmp;
101 char **s;
102 krb5_enctype *tmptypes;
104 INIT_FIELD(context, time, max_skew, 5 * 60, "clockskew");
105 INIT_FIELD(context, time, kdc_timeout, 3, "kdc_timeout");
106 INIT_FIELD(context, int, max_retries, 3, "max_retries");
108 INIT_FIELD(context, string, http_proxy, NULL, "http_proxy");
110 ret = krb5_config_get_bool_default(context, NULL, FALSE,
111 "libdefaults",
112 "allow_weak_crypto", NULL);
113 if (ret) {
114 krb5_enctype_enable(context, ETYPE_DES_CBC_CRC);
115 krb5_enctype_enable(context, ETYPE_DES_CBC_MD4);
116 krb5_enctype_enable(context, ETYPE_DES_CBC_MD5);
117 krb5_enctype_enable(context, ETYPE_DES_CBC_NONE);
118 krb5_enctype_enable(context, ETYPE_DES_CFB64_NONE);
119 krb5_enctype_enable(context, ETYPE_DES_PCBC_NONE);
122 ret = set_etypes (context, "default_etypes", &tmptypes);
123 if(ret)
124 return ret;
125 free(context->etypes);
126 context->etypes = tmptypes;
128 ret = set_etypes (context, "default_etypes_des", &tmptypes);
129 if(ret)
130 return ret;
131 free(context->etypes_des);
132 context->etypes_des = tmptypes;
134 ret = set_etypes (context, "default_as_etypes", &tmptypes);
135 if(ret)
136 return ret;
137 free(context->as_etypes);
138 context->as_etypes = tmptypes;
140 ret = set_etypes (context, "default_tgs_etypes", &tmptypes);
141 if(ret)
142 return ret;
143 free(context->tgs_etypes);
144 context->tgs_etypes = tmptypes;
146 ret = set_etypes (context, "permitted_enctypes", &tmptypes);
147 if(ret)
148 return ret;
149 free(context->permitted_enctypes);
150 context->permitted_enctypes = tmptypes;
152 /* default keytab name */
153 tmp = NULL;
154 if(!issuid())
155 tmp = getenv("KRB5_KTNAME");
156 if(tmp != NULL)
157 context->default_keytab = tmp;
158 else
159 INIT_FIELD(context, string, default_keytab,
160 KEYTAB_DEFAULT, "default_keytab_name");
162 INIT_FIELD(context, string, default_keytab_modify,
163 NULL, "default_keytab_modify_name");
165 INIT_FIELD(context, string, time_fmt,
166 "%Y-%m-%dT%H:%M:%S", "time_format");
168 INIT_FIELD(context, string, date_fmt,
169 "%Y-%m-%d", "date_format");
171 INIT_FIELD(context, bool, log_utc,
172 FALSE, "log_utc");
176 /* init dns-proxy slime */
177 tmp = krb5_config_get_string(context, NULL, "libdefaults",
178 "dns_proxy", NULL);
179 if(tmp)
180 roken_gethostby_setup(context->http_proxy, tmp);
181 krb5_free_host_realm (context, context->default_realms);
182 context->default_realms = NULL;
185 krb5_addresses addresses;
186 char **adr, **a;
188 krb5_set_extra_addresses(context, NULL);
189 adr = krb5_config_get_strings(context, NULL,
190 "libdefaults",
191 "extra_addresses",
192 NULL);
193 memset(&addresses, 0, sizeof(addresses));
194 for(a = adr; a && *a; a++) {
195 ret = krb5_parse_address(context, *a, &addresses);
196 if (ret == 0) {
197 krb5_add_extra_addresses(context, &addresses);
198 krb5_free_addresses(context, &addresses);
201 krb5_config_free_strings(adr);
203 krb5_set_ignore_addresses(context, NULL);
204 adr = krb5_config_get_strings(context, NULL,
205 "libdefaults",
206 "ignore_addresses",
207 NULL);
208 memset(&addresses, 0, sizeof(addresses));
209 for(a = adr; a && *a; a++) {
210 ret = krb5_parse_address(context, *a, &addresses);
211 if (ret == 0) {
212 krb5_add_ignore_addresses(context, &addresses);
213 krb5_free_addresses(context, &addresses);
216 krb5_config_free_strings(adr);
219 INIT_FIELD(context, bool, scan_interfaces, TRUE, "scan_interfaces");
220 INIT_FIELD(context, int, fcache_vno, 0, "fcache_version");
221 /* prefer dns_lookup_kdc over srv_lookup. */
222 INIT_FIELD(context, bool, srv_lookup, TRUE, "srv_lookup");
223 INIT_FIELD(context, bool, srv_lookup, context->srv_lookup, "dns_lookup_kdc");
224 INIT_FIELD(context, int, large_msg_size, 1400, "large_message_size");
225 INIT_FLAG(context, flags, KRB5_CTX_F_DNS_CANONICALIZE_HOSTNAME, TRUE, "dns_canonicalize_hostname");
226 INIT_FLAG(context, flags, KRB5_CTX_F_CHECK_PAC, TRUE, "check_pac");
227 context->default_cc_name = NULL;
228 context->default_cc_name_set = 0;
230 s = krb5_config_get_strings(context, NULL, "logging", "krb5", NULL);
231 if(s) {
232 char **p;
233 krb5_initlog(context, "libkrb5", &context->debug_dest);
234 for(p = s; *p; p++)
235 krb5_addlog_dest(context, context->debug_dest, *p);
236 krb5_config_free_strings(s);
239 tmp = krb5_config_get_string(context, NULL, "libdefaults",
240 "check-rd-req-server", NULL);
241 if (tmp == NULL && !issuid())
242 tmp = getenv("KRB5_CHECK_RD_REQ_SERVER");
243 if(tmp) {
244 if (strcasecmp(tmp, "ignore") == 0)
245 context->flags |= KRB5_CTX_F_RD_REQ_IGNORE;
248 return 0;
251 static krb5_error_code
252 cc_ops_register(krb5_context context)
254 context->cc_ops = NULL;
255 context->num_cc_ops = 0;
257 #ifndef KCM_IS_API_CACHE
258 krb5_cc_register(context, &krb5_acc_ops, TRUE);
259 #endif
260 krb5_cc_register(context, &krb5_fcc_ops, TRUE);
261 krb5_cc_register(context, &krb5_mcc_ops, TRUE);
262 #ifdef HAVE_SCC
263 krb5_cc_register(context, &krb5_scc_ops, TRUE);
264 #endif
265 #ifdef HAVE_KCM
266 #ifdef KCM_IS_API_CACHE
267 krb5_cc_register(context, &krb5_akcm_ops, TRUE);
268 #endif
269 krb5_cc_register(context, &krb5_kcm_ops, TRUE);
270 #endif
271 _krb5_load_ccache_plugins(context);
272 return 0;
275 static krb5_error_code
276 cc_ops_copy(krb5_context context, const krb5_context src_context)
278 const krb5_cc_ops **cc_ops;
280 context->cc_ops = NULL;
281 context->num_cc_ops = 0;
283 if (src_context->num_cc_ops == 0)
284 return 0;
286 cc_ops = malloc(sizeof(cc_ops[0]) * src_context->num_cc_ops);
287 if (cc_ops == NULL) {
288 krb5_set_error_message(context, KRB5_CC_NOMEM,
289 N_("malloc: out of memory", ""));
290 return KRB5_CC_NOMEM;
293 memcpy(rk_UNCONST(cc_ops), src_context->cc_ops,
294 sizeof(cc_ops[0]) * src_context->num_cc_ops);
295 context->cc_ops = cc_ops;
296 context->num_cc_ops = src_context->num_cc_ops;
298 return 0;
301 static krb5_error_code
302 kt_ops_register(krb5_context context)
304 context->num_kt_types = 0;
305 context->kt_types = NULL;
307 krb5_kt_register (context, &krb5_fkt_ops);
308 krb5_kt_register (context, &krb5_wrfkt_ops);
309 krb5_kt_register (context, &krb5_javakt_ops);
310 krb5_kt_register (context, &krb5_mkt_ops);
311 #ifndef HEIMDAL_SMALLER
312 krb5_kt_register (context, &krb5_akf_ops);
313 #endif
314 krb5_kt_register (context, &krb5_any_ops);
315 return 0;
318 static krb5_error_code
319 kt_ops_copy(krb5_context context, const krb5_context src_context)
321 context->num_kt_types = 0;
322 context->kt_types = NULL;
324 if (src_context->num_kt_types == 0)
325 return 0;
327 context->kt_types = malloc(sizeof(context->kt_types[0]) * src_context->num_kt_types);
328 if (context->kt_types == NULL) {
329 krb5_set_error_message(context, ENOMEM,
330 N_("malloc: out of memory", ""));
331 return ENOMEM;
334 context->num_kt_types = src_context->num_kt_types;
335 memcpy(context->kt_types, src_context->kt_types,
336 sizeof(context->kt_types[0]) * src_context->num_kt_types);
338 return 0;
341 static const char *sysplugin_dirs[] = {
342 LIBDIR "/plugin/krb5",
343 #ifdef __APPLE__
344 "/Library/KerberosPlugins/KerberosFrameworkPlugins",
345 "/System/Library/KerberosPlugins/KerberosFrameworkPlugins",
346 #endif
347 NULL
350 static void
351 init_context_once(void *ctx)
353 krb5_context context = ctx;
355 _krb5_load_plugins(context, "krb5", sysplugin_dirs);
357 bindtextdomain(HEIMDAL_TEXTDOMAIN, HEIMDAL_LOCALEDIR);
362 * Initializes the context structure and reads the configuration file
363 * /etc/krb5.conf. The structure should be freed by calling
364 * krb5_free_context() when it is no longer being used.
366 * @param context pointer to returned context
368 * @return Returns 0 to indicate success. Otherwise an errno code is
369 * returned. Failure means either that something bad happened during
370 * initialization (typically ENOMEM) or that Kerberos should not be
371 * used ENXIO.
373 * @ingroup krb5
376 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
377 krb5_init_context(krb5_context *context)
379 static heim_base_once_t init_context = HEIM_BASE_ONCE_INIT;
380 krb5_context p;
381 krb5_error_code ret;
382 char **files;
384 *context = NULL;
386 p = calloc(1, sizeof(*p));
387 if(!p)
388 return ENOMEM;
390 p->mutex = malloc(sizeof(HEIMDAL_MUTEX));
391 if (p->mutex == NULL) {
392 free(p);
393 return ENOMEM;
395 HEIMDAL_MUTEX_init(p->mutex);
397 p->flags |= KRB5_CTX_F_HOMEDIR_ACCESS;
399 ret = krb5_get_default_config_files(&files);
400 if(ret)
401 goto out;
402 ret = krb5_set_config_files(p, files);
403 krb5_free_config_files(files);
404 if(ret)
405 goto out;
407 /* init error tables */
408 krb5_init_ets(p);
409 cc_ops_register(p);
410 kt_ops_register(p);
412 #ifdef PKINIT
413 ret = hx509_context_init(&p->hx509ctx);
414 if (ret)
415 goto out;
416 #endif
417 if (rk_SOCK_INIT())
418 p->flags |= KRB5_CTX_F_SOCKETS_INITIALIZED;
420 out:
421 if(ret) {
422 krb5_free_context(p);
423 p = NULL;
424 } else {
425 heim_base_once_f(&init_context, p, init_context_once);
427 *context = p;
428 return ret;
431 #ifndef HEIMDAL_SMALLER
433 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
434 krb5_get_permitted_enctypes(krb5_context context,
435 krb5_enctype **etypes)
437 return krb5_get_default_in_tkt_etypes(context, KRB5_PDU_NONE, etypes);
444 static krb5_error_code
445 copy_etypes (krb5_context context,
446 krb5_enctype *enctypes,
447 krb5_enctype **ret_enctypes)
449 unsigned int i;
451 for (i = 0; enctypes[i]; i++)
453 i++;
455 *ret_enctypes = malloc(sizeof(**ret_enctypes) * i);
456 if (*ret_enctypes == NULL) {
457 krb5_set_error_message(context, ENOMEM,
458 N_("malloc: out of memory", ""));
459 return ENOMEM;
461 memcpy(*ret_enctypes, enctypes, sizeof(**ret_enctypes) * i);
462 return 0;
466 * Make a copy for the Kerberos 5 context, the new krb5_context shoud
467 * be freed with krb5_free_context().
469 * @param context the Kerberos context to copy
470 * @param out the copy of the Kerberos, set to NULL error.
472 * @return Returns 0 to indicate success. Otherwise an kerberos et
473 * error code is returned, see krb5_get_error_message().
475 * @ingroup krb5
478 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
479 krb5_copy_context(krb5_context context, krb5_context *out)
481 krb5_error_code ret;
482 krb5_context p;
484 *out = NULL;
486 p = calloc(1, sizeof(*p));
487 if (p == NULL) {
488 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
489 return ENOMEM;
492 p->mutex = malloc(sizeof(HEIMDAL_MUTEX));
493 if (p->mutex == NULL) {
494 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
495 free(p);
496 return ENOMEM;
498 HEIMDAL_MUTEX_init(p->mutex);
501 if (context->default_cc_name)
502 p->default_cc_name = strdup(context->default_cc_name);
503 if (context->default_cc_name_env)
504 p->default_cc_name_env = strdup(context->default_cc_name_env);
506 if (context->etypes) {
507 ret = copy_etypes(context, context->etypes, &p->etypes);
508 if (ret)
509 goto out;
511 if (context->etypes_des) {
512 ret = copy_etypes(context, context->etypes_des, &p->etypes_des);
513 if (ret)
514 goto out;
517 if (context->default_realms) {
518 ret = krb5_copy_host_realm(context,
519 context->default_realms, &p->default_realms);
520 if (ret)
521 goto out;
524 ret = _krb5_config_copy(context, context->cf, &p->cf);
525 if (ret)
526 goto out;
528 /* XXX should copy */
529 krb5_init_ets(p);
531 cc_ops_copy(p, context);
532 kt_ops_copy(p, context);
534 #if 0 /* XXX */
535 if(context->warn_dest != NULL)
537 if(context->debug_dest != NULL)
539 #endif
541 ret = krb5_set_extra_addresses(p, context->extra_addresses);
542 if (ret)
543 goto out;
544 ret = krb5_set_extra_addresses(p, context->ignore_addresses);
545 if (ret)
546 goto out;
548 ret = _krb5_copy_send_to_kdc_func(p, context);
549 if (ret)
550 goto out;
552 *out = p;
554 return 0;
556 out:
557 krb5_free_context(p);
558 return ret;
561 #endif
564 * Frees the krb5_context allocated by krb5_init_context().
566 * @param context context to be freed.
568 * @ingroup krb5
571 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
572 krb5_free_context(krb5_context context)
574 if (context->default_cc_name)
575 free(context->default_cc_name);
576 if (context->default_cc_name_env)
577 free(context->default_cc_name_env);
578 free(context->etypes);
579 free(context->etypes_des);
580 krb5_free_host_realm (context, context->default_realms);
581 krb5_config_file_free (context, context->cf);
582 free_error_table (context->et_list);
583 free(rk_UNCONST(context->cc_ops));
584 free(context->kt_types);
585 krb5_clear_error_message(context);
586 if(context->warn_dest != NULL)
587 krb5_closelog(context, context->warn_dest);
588 if(context->debug_dest != NULL)
589 krb5_closelog(context, context->debug_dest);
590 krb5_set_extra_addresses(context, NULL);
591 krb5_set_ignore_addresses(context, NULL);
592 krb5_set_send_to_kdc_func(context, NULL, NULL);
594 #ifdef PKINIT
595 if (context->hx509ctx)
596 hx509_context_free(&context->hx509ctx);
597 #endif
599 HEIMDAL_MUTEX_destroy(context->mutex);
600 free(context->mutex);
601 if (context->flags & KRB5_CTX_F_SOCKETS_INITIALIZED) {
602 rk_SOCK_EXIT();
605 memset(context, 0, sizeof(*context));
606 free(context);
610 * Reinit the context from a new set of filenames.
612 * @param context context to add configuration too.
613 * @param filenames array of filenames, end of list is indicated with a NULL filename.
615 * @return Returns 0 to indicate success. Otherwise an kerberos et
616 * error code is returned, see krb5_get_error_message().
618 * @ingroup krb5
621 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
622 krb5_set_config_files(krb5_context context, char **filenames)
624 krb5_error_code ret;
625 krb5_config_binding *tmp = NULL;
626 while(filenames != NULL && *filenames != NULL && **filenames != '\0') {
627 ret = krb5_config_parse_file_multi(context, *filenames, &tmp);
628 if(ret != 0 && ret != ENOENT && ret != EACCES && ret != EPERM) {
629 krb5_config_file_free(context, tmp);
630 return ret;
632 filenames++;
634 #if 1
635 /* with this enabled and if there are no config files, Kerberos is
636 considererd disabled */
637 if(tmp == NULL)
638 return ENXIO;
639 #endif
641 #ifdef _WIN32
642 _krb5_load_config_from_registry(context, &tmp);
643 #endif
645 krb5_config_file_free(context, context->cf);
646 context->cf = tmp;
647 ret = init_context_from_config_file(context);
648 return ret;
651 static krb5_error_code
652 add_file(char ***pfilenames, int *len, char *file)
654 char **pp = *pfilenames;
655 int i;
657 for(i = 0; i < *len; i++) {
658 if(strcmp(pp[i], file) == 0) {
659 free(file);
660 return 0;
664 pp = realloc(*pfilenames, (*len + 2) * sizeof(*pp));
665 if (pp == NULL) {
666 free(file);
667 return ENOMEM;
670 pp[*len] = file;
671 pp[*len + 1] = NULL;
672 *pfilenames = pp;
673 *len += 1;
674 return 0;
678 * `pq' isn't free, it's up the the caller
681 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
682 krb5_prepend_config_files(const char *filelist, char **pq, char ***ret_pp)
684 krb5_error_code ret;
685 const char *p, *q;
686 char **pp;
687 int len;
688 char *fn;
690 pp = NULL;
692 len = 0;
693 p = filelist;
694 while(1) {
695 ssize_t l;
696 q = p;
697 l = strsep_copy(&q, PATH_SEP, NULL, 0);
698 if(l == -1)
699 break;
700 fn = malloc(l + 1);
701 if(fn == NULL) {
702 krb5_free_config_files(pp);
703 return ENOMEM;
705 (void)strsep_copy(&p, PATH_SEP, fn, l + 1);
706 ret = add_file(&pp, &len, fn);
707 if (ret) {
708 krb5_free_config_files(pp);
709 return ret;
713 if (pq != NULL) {
714 int i;
716 for (i = 0; pq[i] != NULL; i++) {
717 fn = strdup(pq[i]);
718 if (fn == NULL) {
719 krb5_free_config_files(pp);
720 return ENOMEM;
722 ret = add_file(&pp, &len, fn);
723 if (ret) {
724 krb5_free_config_files(pp);
725 return ret;
730 *ret_pp = pp;
731 return 0;
735 * Prepend the filename to the global configuration list.
737 * @param filelist a filename to add to the default list of filename
738 * @param pfilenames return array of filenames, should be freed with krb5_free_config_files().
740 * @return Returns 0 to indicate success. Otherwise an kerberos et
741 * error code is returned, see krb5_get_error_message().
743 * @ingroup krb5
746 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
747 krb5_prepend_config_files_default(const char *filelist, char ***pfilenames)
749 krb5_error_code ret;
750 char **defpp, **pp = NULL;
752 ret = krb5_get_default_config_files(&defpp);
753 if (ret)
754 return ret;
756 ret = krb5_prepend_config_files(filelist, defpp, &pp);
757 krb5_free_config_files(defpp);
758 if (ret) {
759 return ret;
761 *pfilenames = pp;
762 return 0;
765 #ifdef _WIN32
768 * Checks the registry for configuration file location
770 * Kerberos for Windows and other legacy Kerberos applications expect
771 * to find the configuration file location in the
772 * SOFTWARE\MIT\Kerberos registry key under the value "config".
774 char *
775 _krb5_get_default_config_config_files_from_registry()
777 static const char * KeyName = "Software\\MIT\\Kerberos";
778 char *config_file = NULL;
779 LONG rcode;
780 HKEY key;
782 rcode = RegOpenKeyEx(HKEY_CURRENT_USER, KeyName, 0, KEY_READ, &key);
783 if (rcode == ERROR_SUCCESS) {
784 config_file = _krb5_parse_reg_value_as_multi_string(NULL, key, "config",
785 REG_NONE, 0, PATH_SEP);
786 RegCloseKey(key);
789 if (config_file)
790 return config_file;
792 rcode = RegOpenKeyEx(HKEY_LOCAL_MACHINE, KeyName, 0, KEY_READ, &key);
793 if (rcode == ERROR_SUCCESS) {
794 config_file = _krb5_parse_reg_value_as_multi_string(NULL, key, "config",
795 REG_NONE, 0, PATH_SEP);
796 RegCloseKey(key);
799 return config_file;
802 #endif
805 * Get the global configuration list.
807 * @param pfilenames return array of filenames, should be freed with krb5_free_config_files().
809 * @return Returns 0 to indicate success. Otherwise an kerberos et
810 * error code is returned, see krb5_get_error_message().
812 * @ingroup krb5
815 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
816 krb5_get_default_config_files(char ***pfilenames)
818 const char *files = NULL;
820 if (pfilenames == NULL)
821 return EINVAL;
822 if(!issuid())
823 files = getenv("KRB5_CONFIG");
825 #ifdef _WIN32
826 if (files == NULL) {
827 char * reg_files;
828 reg_files = _krb5_get_default_config_config_files_from_registry();
829 if (reg_files != NULL) {
830 krb5_error_code code;
832 code = krb5_prepend_config_files(reg_files, NULL, pfilenames);
833 free(reg_files);
835 return code;
838 #endif
840 if (files == NULL)
841 files = krb5_config_file;
843 return krb5_prepend_config_files(files, NULL, pfilenames);
847 * Free a list of configuration files.
849 * @param filenames list, terminated with a NULL pointer, to be
850 * freed. NULL is an valid argument.
852 * @return Returns 0 to indicate success. Otherwise an kerberos et
853 * error code is returned, see krb5_get_error_message().
855 * @ingroup krb5
858 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
859 krb5_free_config_files(char **filenames)
861 char **p;
862 for(p = filenames; p && *p != NULL; p++)
863 free(*p);
864 free(filenames);
868 * Returns the list of Kerberos encryption types sorted in order of
869 * most preferred to least preferred encryption type. Note that some
870 * encryption types might be disabled, so you need to check with
871 * krb5_enctype_valid() before using the encryption type.
873 * @return list of enctypes, terminated with ETYPE_NULL. Its a static
874 * array completed into the Kerberos library so the content doesn't
875 * need to be freed.
877 * @ingroup krb5
880 KRB5_LIB_FUNCTION const krb5_enctype * KRB5_LIB_CALL
881 krb5_kerberos_enctypes(krb5_context context)
883 static const krb5_enctype p[] = {
884 ETYPE_AES256_CTS_HMAC_SHA1_96,
885 ETYPE_AES128_CTS_HMAC_SHA1_96,
886 ETYPE_DES3_CBC_SHA1,
887 ETYPE_DES3_CBC_MD5,
888 ETYPE_ARCFOUR_HMAC_MD5,
889 ETYPE_DES_CBC_MD5,
890 ETYPE_DES_CBC_MD4,
891 ETYPE_DES_CBC_CRC,
892 ETYPE_NULL
894 return p;
901 static krb5_error_code
902 copy_enctypes(krb5_context context,
903 const krb5_enctype *in,
904 krb5_enctype **out)
906 krb5_enctype *p = NULL;
907 size_t m, n;
909 for (n = 0; in[n]; n++)
911 n++;
912 ALLOC(p, n);
913 if(p == NULL)
914 return krb5_enomem(context);
915 for (n = 0, m = 0; in[n]; n++) {
916 if (krb5_enctype_valid(context, in[n]) != 0)
917 continue;
918 p[m++] = in[n];
920 p[m] = KRB5_ENCTYPE_NULL;
921 if (m == 0) {
922 free(p);
923 krb5_set_error_message (context, KRB5_PROG_ETYPE_NOSUPP,
924 N_("no valid enctype set", ""));
925 return KRB5_PROG_ETYPE_NOSUPP;
927 *out = p;
928 return 0;
933 * set `etype' to a malloced list of the default enctypes
936 static krb5_error_code
937 default_etypes(krb5_context context, krb5_enctype **etype)
939 const krb5_enctype *p = krb5_kerberos_enctypes(context);
940 return copy_enctypes(context, p, etype);
944 * Set the default encryption types that will be use in communcation
945 * with the KDC, clients and servers.
947 * @param context Kerberos 5 context.
948 * @param etypes Encryption types, array terminated with ETYPE_NULL (0).
950 * @return Returns 0 to indicate success. Otherwise an kerberos et
951 * error code is returned, see krb5_get_error_message().
953 * @ingroup krb5
956 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
957 krb5_set_default_in_tkt_etypes(krb5_context context,
958 const krb5_enctype *etypes)
960 krb5_error_code ret;
961 krb5_enctype *p = NULL;
963 if(etypes) {
964 ret = copy_enctypes(context, etypes, &p);
965 if (ret)
966 return ret;
968 if(context->etypes)
969 free(context->etypes);
970 context->etypes = p;
971 return 0;
975 * Get the default encryption types that will be use in communcation
976 * with the KDC, clients and servers.
978 * @param context Kerberos 5 context.
979 * @param etypes Encryption types, array terminated with
980 * ETYPE_NULL(0), caller should free array with krb5_xfree():
982 * @return Returns 0 to indicate success. Otherwise an kerberos et
983 * error code is returned, see krb5_get_error_message().
985 * @ingroup krb5
988 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
989 krb5_get_default_in_tkt_etypes(krb5_context context,
990 krb5_pdu pdu_type,
991 krb5_enctype **etypes)
993 krb5_enctype *enctypes = NULL;
994 krb5_error_code ret;
995 krb5_enctype *p;
997 heim_assert(pdu_type == KRB5_PDU_AS_REQUEST ||
998 pdu_type == KRB5_PDU_TGS_REQUEST ||
999 pdu_type == KRB5_PDU_NONE, "pdu contant not as expected");
1001 if (pdu_type == KRB5_PDU_AS_REQUEST && context->as_etypes != NULL)
1002 enctypes = context->as_etypes;
1003 else if (pdu_type == KRB5_PDU_TGS_REQUEST && context->tgs_etypes != NULL)
1004 enctypes = context->tgs_etypes;
1005 else if (context->etypes != NULL)
1006 enctypes = context->etypes;
1008 if (enctypes != NULL) {
1009 ret = copy_enctypes(context, enctypes, &p);
1010 if (ret)
1011 return ret;
1012 } else {
1013 ret = default_etypes(context, &p);
1014 if (ret)
1015 return ret;
1017 *etypes = p;
1018 return 0;
1022 * Init the built-in ets in the Kerberos library.
1024 * @param context kerberos context to add the ets too
1026 * @ingroup krb5
1029 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
1030 krb5_init_ets(krb5_context context)
1032 if(context->et_list == NULL){
1033 krb5_add_et_list(context, initialize_krb5_error_table_r);
1034 krb5_add_et_list(context, initialize_asn1_error_table_r);
1035 krb5_add_et_list(context, initialize_heim_error_table_r);
1037 krb5_add_et_list(context, initialize_k524_error_table_r);
1039 #ifdef COM_ERR_BINDDOMAIN_krb5
1040 bindtextdomain(COM_ERR_BINDDOMAIN_krb5, HEIMDAL_LOCALEDIR);
1041 bindtextdomain(COM_ERR_BINDDOMAIN_asn1, HEIMDAL_LOCALEDIR);
1042 bindtextdomain(COM_ERR_BINDDOMAIN_heim, HEIMDAL_LOCALEDIR);
1043 bindtextdomain(COM_ERR_BINDDOMAIN_k524, HEIMDAL_LOCALEDIR);
1044 #endif
1046 #ifdef PKINIT
1047 krb5_add_et_list(context, initialize_hx_error_table_r);
1048 #ifdef COM_ERR_BINDDOMAIN_hx
1049 bindtextdomain(COM_ERR_BINDDOMAIN_hx, HEIMDAL_LOCALEDIR);
1050 #endif
1051 #endif
1056 * Make the kerberos library default to the admin KDC.
1058 * @param context Kerberos 5 context.
1059 * @param flag boolean flag to select if the use the admin KDC or not.
1061 * @ingroup krb5
1064 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
1065 krb5_set_use_admin_kdc (krb5_context context, krb5_boolean flag)
1067 context->use_admin_kdc = flag;
1071 * Make the kerberos library default to the admin KDC.
1073 * @param context Kerberos 5 context.
1075 * @return boolean flag to telling the context will use admin KDC as the default KDC.
1077 * @ingroup krb5
1080 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1081 krb5_get_use_admin_kdc (krb5_context context)
1083 return context->use_admin_kdc;
1087 * Add extra address to the address list that the library will add to
1088 * the client's address list when communicating with the KDC.
1090 * @param context Kerberos 5 context.
1091 * @param addresses addreses to add
1093 * @return Returns 0 to indicate success. Otherwise an kerberos et
1094 * error code is returned, see krb5_get_error_message().
1096 * @ingroup krb5
1099 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1100 krb5_add_extra_addresses(krb5_context context, krb5_addresses *addresses)
1103 if(context->extra_addresses)
1104 return krb5_append_addresses(context,
1105 context->extra_addresses, addresses);
1106 else
1107 return krb5_set_extra_addresses(context, addresses);
1111 * Set extra address to the address list that the library will add to
1112 * the client's address list when communicating with the KDC.
1114 * @param context Kerberos 5 context.
1115 * @param addresses addreses to set
1117 * @return Returns 0 to indicate success. Otherwise an kerberos et
1118 * error code is returned, see krb5_get_error_message().
1120 * @ingroup krb5
1123 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1124 krb5_set_extra_addresses(krb5_context context, const krb5_addresses *addresses)
1126 if(context->extra_addresses)
1127 krb5_free_addresses(context, context->extra_addresses);
1129 if(addresses == NULL) {
1130 if(context->extra_addresses != NULL) {
1131 free(context->extra_addresses);
1132 context->extra_addresses = NULL;
1134 return 0;
1136 if(context->extra_addresses == NULL) {
1137 context->extra_addresses = malloc(sizeof(*context->extra_addresses));
1138 if(context->extra_addresses == NULL) {
1139 krb5_set_error_message (context, ENOMEM, N_("malloc: out of memory", ""));
1140 return ENOMEM;
1143 return krb5_copy_addresses(context, addresses, context->extra_addresses);
1147 * Get extra address to the address list that the library will add to
1148 * the client's address list when communicating with the KDC.
1150 * @param context Kerberos 5 context.
1151 * @param addresses addreses to set
1153 * @return Returns 0 to indicate success. Otherwise an kerberos et
1154 * error code is returned, see krb5_get_error_message().
1156 * @ingroup krb5
1159 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1160 krb5_get_extra_addresses(krb5_context context, krb5_addresses *addresses)
1162 if(context->extra_addresses == NULL) {
1163 memset(addresses, 0, sizeof(*addresses));
1164 return 0;
1166 return krb5_copy_addresses(context,context->extra_addresses, addresses);
1170 * Add extra addresses to ignore when fetching addresses from the
1171 * underlaying operating system.
1173 * @param context Kerberos 5 context.
1174 * @param addresses addreses to ignore
1176 * @return Returns 0 to indicate success. Otherwise an kerberos et
1177 * error code is returned, see krb5_get_error_message().
1179 * @ingroup krb5
1182 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1183 krb5_add_ignore_addresses(krb5_context context, krb5_addresses *addresses)
1186 if(context->ignore_addresses)
1187 return krb5_append_addresses(context,
1188 context->ignore_addresses, addresses);
1189 else
1190 return krb5_set_ignore_addresses(context, addresses);
1194 * Set extra addresses to ignore when fetching addresses from the
1195 * underlaying operating system.
1197 * @param context Kerberos 5 context.
1198 * @param addresses addreses to ignore
1200 * @return Returns 0 to indicate success. Otherwise an kerberos et
1201 * error code is returned, see krb5_get_error_message().
1203 * @ingroup krb5
1206 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1207 krb5_set_ignore_addresses(krb5_context context, const krb5_addresses *addresses)
1209 if(context->ignore_addresses)
1210 krb5_free_addresses(context, context->ignore_addresses);
1211 if(addresses == NULL) {
1212 if(context->ignore_addresses != NULL) {
1213 free(context->ignore_addresses);
1214 context->ignore_addresses = NULL;
1216 return 0;
1218 if(context->ignore_addresses == NULL) {
1219 context->ignore_addresses = malloc(sizeof(*context->ignore_addresses));
1220 if(context->ignore_addresses == NULL) {
1221 krb5_set_error_message (context, ENOMEM, N_("malloc: out of memory", ""));
1222 return ENOMEM;
1225 return krb5_copy_addresses(context, addresses, context->ignore_addresses);
1229 * Get extra addresses to ignore when fetching addresses from the
1230 * underlaying operating system.
1232 * @param context Kerberos 5 context.
1233 * @param addresses list addreses ignored
1235 * @return Returns 0 to indicate success. Otherwise an kerberos et
1236 * error code is returned, see krb5_get_error_message().
1238 * @ingroup krb5
1241 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1242 krb5_get_ignore_addresses(krb5_context context, krb5_addresses *addresses)
1244 if(context->ignore_addresses == NULL) {
1245 memset(addresses, 0, sizeof(*addresses));
1246 return 0;
1248 return krb5_copy_addresses(context, context->ignore_addresses, addresses);
1252 * Set version of fcache that the library should use.
1254 * @param context Kerberos 5 context.
1255 * @param version version number.
1257 * @return Returns 0 to indicate success. Otherwise an kerberos et
1258 * error code is returned, see krb5_get_error_message().
1260 * @ingroup krb5
1263 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1264 krb5_set_fcache_version(krb5_context context, int version)
1266 context->fcache_vno = version;
1267 return 0;
1271 * Get version of fcache that the library should use.
1273 * @param context Kerberos 5 context.
1274 * @param version version number.
1276 * @return Returns 0 to indicate success. Otherwise an kerberos et
1277 * error code is returned, see krb5_get_error_message().
1279 * @ingroup krb5
1282 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1283 krb5_get_fcache_version(krb5_context context, int *version)
1285 *version = context->fcache_vno;
1286 return 0;
1290 * Runtime check if the Kerberos library was complied with thread support.
1292 * @return TRUE if the library was compiled with thread support, FALSE if not.
1294 * @ingroup krb5
1298 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1299 krb5_is_thread_safe(void)
1301 #ifdef ENABLE_PTHREAD_SUPPORT
1302 return TRUE;
1303 #else
1304 return FALSE;
1305 #endif
1309 * Set if the library should use DNS to canonicalize hostnames.
1311 * @param context Kerberos 5 context.
1312 * @param flag if its dns canonicalizion is used or not.
1314 * @ingroup krb5
1317 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
1318 krb5_set_dns_canonicalize_hostname (krb5_context context, krb5_boolean flag)
1320 if (flag)
1321 context->flags |= KRB5_CTX_F_DNS_CANONICALIZE_HOSTNAME;
1322 else
1323 context->flags &= ~KRB5_CTX_F_DNS_CANONICALIZE_HOSTNAME;
1327 * Get if the library uses DNS to canonicalize hostnames.
1329 * @param context Kerberos 5 context.
1331 * @return return non zero if the library uses DNS to canonicalize hostnames.
1333 * @ingroup krb5
1336 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1337 krb5_get_dns_canonicalize_hostname (krb5_context context)
1339 return (context->flags & KRB5_CTX_F_DNS_CANONICALIZE_HOSTNAME) ? 1 : 0;
1343 * Get current offset in time to the KDC.
1345 * @param context Kerberos 5 context.
1346 * @param sec seconds part of offset.
1347 * @param usec micro seconds part of offset.
1349 * @return returns zero
1351 * @ingroup krb5
1354 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1355 krb5_get_kdc_sec_offset (krb5_context context, int32_t *sec, int32_t *usec)
1357 if (sec)
1358 *sec = context->kdc_sec_offset;
1359 if (usec)
1360 *usec = context->kdc_usec_offset;
1361 return 0;
1365 * Set current offset in time to the KDC.
1367 * @param context Kerberos 5 context.
1368 * @param sec seconds part of offset.
1369 * @param usec micro seconds part of offset.
1371 * @return returns zero
1373 * @ingroup krb5
1376 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1377 krb5_set_kdc_sec_offset (krb5_context context, int32_t sec, int32_t usec)
1379 context->kdc_sec_offset = sec;
1380 if (usec >= 0)
1381 context->kdc_usec_offset = usec;
1382 return 0;
1386 * Get max time skew allowed.
1388 * @param context Kerberos 5 context.
1390 * @return timeskew in seconds.
1392 * @ingroup krb5
1395 KRB5_LIB_FUNCTION time_t KRB5_LIB_CALL
1396 krb5_get_max_time_skew (krb5_context context)
1398 return context->max_skew;
1402 * Set max time skew allowed.
1404 * @param context Kerberos 5 context.
1405 * @param t timeskew in seconds.
1407 * @ingroup krb5
1410 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
1411 krb5_set_max_time_skew (krb5_context context, time_t t)
1413 context->max_skew = t;
1417 * Init encryption types in len, val with etypes.
1419 * @param context Kerberos 5 context.
1420 * @param pdu_type type of pdu
1421 * @param len output length of val.
1422 * @param val output array of enctypes.
1423 * @param etypes etypes to set val and len to, if NULL, use default enctypes.
1425 * @return Returns 0 to indicate success. Otherwise an kerberos et
1426 * error code is returned, see krb5_get_error_message().
1428 * @ingroup krb5
1431 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1432 _krb5_init_etype(krb5_context context,
1433 krb5_pdu pdu_type,
1434 unsigned *len,
1435 krb5_enctype **val,
1436 const krb5_enctype *etypes)
1438 krb5_error_code ret;
1440 if (etypes == NULL)
1441 ret = krb5_get_default_in_tkt_etypes(context, pdu_type, val);
1442 else
1443 ret = copy_enctypes(context, etypes, val);
1444 if (ret)
1445 return ret;
1447 if (len) {
1448 *len = 0;
1449 while ((*val)[*len] != KRB5_ENCTYPE_NULL)
1450 (*len)++;
1452 return 0;
1456 * Allow homedir accces
1459 static HEIMDAL_MUTEX homedir_mutex = HEIMDAL_MUTEX_INITIALIZER;
1460 static krb5_boolean allow_homedir = TRUE;
1462 krb5_boolean
1463 _krb5_homedir_access(krb5_context context)
1465 krb5_boolean allow;
1467 #ifdef HAVE_GETEUID
1468 /* is never allowed for root */
1469 if (geteuid() == 0)
1470 return FALSE;
1471 #endif
1473 if (context && (context->flags & KRB5_CTX_F_HOMEDIR_ACCESS) == 0)
1474 return FALSE;
1476 HEIMDAL_MUTEX_lock(&homedir_mutex);
1477 allow = allow_homedir;
1478 HEIMDAL_MUTEX_unlock(&homedir_mutex);
1479 return allow;
1483 * Enable and disable home directory access on either the global state
1484 * or the krb5_context state. By calling krb5_set_home_dir_access()
1485 * with context set to NULL, the global state is configured otherwise
1486 * the state for the krb5_context is modified.
1488 * For home directory access to be allowed, both the global state and
1489 * the krb5_context state have to be allowed.
1491 * Administrator (root user), never uses the home directory.
1493 * @param context a Kerberos 5 context or NULL
1494 * @param allow allow if TRUE home directory
1495 * @return the old value
1497 * @ingroup krb5
1500 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1501 krb5_set_home_dir_access(krb5_context context, krb5_boolean allow)
1503 krb5_boolean old;
1504 if (context) {
1505 old = (context->flags & KRB5_CTX_F_HOMEDIR_ACCESS) ? TRUE : FALSE;
1506 if (allow)
1507 context->flags |= KRB5_CTX_F_HOMEDIR_ACCESS;
1508 else
1509 context->flags &= ~KRB5_CTX_F_HOMEDIR_ACCESS;
1510 } else {
1511 HEIMDAL_MUTEX_lock(&homedir_mutex);
1512 old = allow_homedir;
1513 allow_homedir = allow;
1514 HEIMDAL_MUTEX_unlock(&homedir_mutex);
1517 return old;