2 * Copyright (c) 1997 - 2010 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #undef KRB5_DEPRECATED_FUNCTION
37 #define KRB5_DEPRECATED_FUNCTION(x)
39 #include "krb5_locl.h"
43 static void _krb5_init_ets(krb5_context);
45 #define INIT_FIELD(C, T, E, D, F) \
46 (C)->E = krb5_config_get_ ## T ## _default ((C), NULL, (D), \
47 "libdefaults", F, NULL)
49 #define INIT_FLAG(C, O, V, D, F) \
51 if (krb5_config_get_bool_default((C), NULL, (D),"libdefaults", F, NULL)) { \
56 static krb5_error_code
57 copy_enctypes(krb5_context context,
58 const krb5_enctype *in,
62 * Set the list of etypes `ret_etypes' from the configuration variable
66 static krb5_error_code
67 set_etypes (krb5_context context,
69 krb5_enctype **ret_enctypes)
72 krb5_enctype *etypes = NULL;
74 etypes_str = krb5_config_get_strings(context, NULL, "libdefaults",
78 for(i = 0; etypes_str[i]; i++);
79 etypes = malloc((i+1) * sizeof(*etypes));
81 krb5_config_free_strings (etypes_str);
82 return krb5_enomem(context);
84 for(j = 0, k = 0; j < i; j++) {
86 if(krb5_string_to_enctype(context, etypes_str[j], &e) != 0)
88 if (krb5_enctype_valid(context, e) != 0)
92 etypes[k] = ETYPE_NULL;
93 krb5_config_free_strings(etypes_str);
95 *ret_enctypes = etypes;
100 * read variables from the configuration file and set in `context'
103 static krb5_error_code
104 init_context_from_config_file(krb5_context context)
109 krb5_enctype *tmptypes;
111 INIT_FIELD(context, time, max_skew, 5 * 60, "clockskew");
112 INIT_FIELD(context, time, kdc_timeout, 30, "kdc_timeout");
113 INIT_FIELD(context, time, host_timeout, 3, "host_timeout");
114 INIT_FIELD(context, int, max_retries, 3, "max_retries");
116 INIT_FIELD(context, string, http_proxy, NULL, "http_proxy");
118 ret = krb5_config_get_bool_default(context, NULL, FALSE,
120 "allow_weak_crypto", NULL);
122 krb5_enctype_enable(context, ETYPE_DES_CBC_CRC);
123 krb5_enctype_enable(context, ETYPE_DES_CBC_MD4);
124 krb5_enctype_enable(context, ETYPE_DES_CBC_MD5);
125 krb5_enctype_enable(context, ETYPE_DES_CBC_NONE);
126 krb5_enctype_enable(context, ETYPE_DES_CFB64_NONE);
127 krb5_enctype_enable(context, ETYPE_DES_PCBC_NONE);
130 ret = set_etypes (context, "default_etypes", &tmptypes);
133 free(context->etypes);
134 context->etypes = tmptypes;
136 /* The etypes member may change during the lifetime
137 * of the context. To be able to reset it to
138 * config value, we keep another copy.
140 free(context->cfg_etypes);
141 context->cfg_etypes = NULL;
143 ret = copy_enctypes(context, tmptypes, &context->cfg_etypes);
148 ret = set_etypes (context, "default_etypes_des", &tmptypes);
151 free(context->etypes_des);
152 context->etypes_des = tmptypes;
154 ret = set_etypes (context, "default_as_etypes", &tmptypes);
157 free(context->as_etypes);
158 context->as_etypes = tmptypes;
160 ret = set_etypes (context, "default_tgs_etypes", &tmptypes);
163 free(context->tgs_etypes);
164 context->tgs_etypes = tmptypes;
166 ret = set_etypes (context, "permitted_enctypes", &tmptypes);
169 free(context->permitted_enctypes);
170 context->permitted_enctypes = tmptypes;
172 INIT_FIELD(context, string, default_keytab,
173 KEYTAB_DEFAULT, "default_keytab_name");
175 INIT_FIELD(context, string, default_keytab_modify,
176 NULL, "default_keytab_modify_name");
178 INIT_FIELD(context, string, time_fmt,
179 "%Y-%m-%dT%H:%M:%S", "time_format");
181 INIT_FIELD(context, string, date_fmt,
182 "%Y-%m-%d", "date_format");
184 INIT_FIELD(context, bool, log_utc,
187 context->no_ticket_store =
188 getenv("KRB5_NO_TICKET_STORE") != NULL;
190 /* init dns-proxy slime */
191 tmp = krb5_config_get_string(context, NULL, "libdefaults",
194 roken_gethostby_setup(context->http_proxy, tmp);
195 krb5_free_host_realm (context, context->default_realms);
196 context->default_realms = NULL;
199 krb5_addresses addresses;
202 krb5_set_extra_addresses(context, NULL);
203 adr = krb5_config_get_strings(context, NULL,
207 memset(&addresses, 0, sizeof(addresses));
208 for(a = adr; a && *a; a++) {
209 ret = krb5_parse_address(context, *a, &addresses);
211 krb5_add_extra_addresses(context, &addresses);
212 krb5_free_addresses(context, &addresses);
215 krb5_config_free_strings(adr);
217 krb5_set_ignore_addresses(context, NULL);
218 adr = krb5_config_get_strings(context, NULL,
222 memset(&addresses, 0, sizeof(addresses));
223 for(a = adr; a && *a; a++) {
224 ret = krb5_parse_address(context, *a, &addresses);
226 krb5_add_ignore_addresses(context, &addresses);
227 krb5_free_addresses(context, &addresses);
230 krb5_config_free_strings(adr);
233 INIT_FIELD(context, bool, scan_interfaces, TRUE, "scan_interfaces");
234 INIT_FIELD(context, int, fcache_vno, 0, "fcache_version");
235 /* prefer dns_lookup_kdc over srv_lookup. */
236 INIT_FIELD(context, bool, srv_lookup, TRUE, "srv_lookup");
237 INIT_FIELD(context, bool, srv_lookup, context->srv_lookup, "dns_lookup_kdc");
238 INIT_FIELD(context, int, large_msg_size, 1400, "large_message_size");
239 INIT_FIELD(context, int, max_msg_size, 1000 * 1024, "maximum_message_size");
240 INIT_FLAG(context, flags, KRB5_CTX_F_DNS_CANONICALIZE_HOSTNAME, TRUE, "dns_canonicalize_hostname");
241 INIT_FLAG(context, flags, KRB5_CTX_F_CHECK_PAC, TRUE, "check_pac");
242 INIT_FLAG(context, flags, KRB5_CTX_F_ENFORCE_OK_AS_DELEGATE, FALSE, "enforce_ok_as_delegate");
243 INIT_FLAG(context, flags, KRB5_CTX_F_REPORT_CANONICAL_CLIENT_NAME, FALSE, "report_canonical_client_name");
245 /* report_canonical_client_name implies check_pac */
246 if (context->flags & KRB5_CTX_F_REPORT_CANONICAL_CLIENT_NAME)
247 context->flags |= KRB5_CTX_F_CHECK_PAC;
249 if (context->default_cc_name)
250 free(context->default_cc_name);
251 context->default_cc_name = NULL;
252 context->default_cc_name_set = 0;
253 context->configured_default_cc_name = NULL;
255 tmp = secure_getenv("KRB5_TRACE");
257 heim_add_debug_dest(context->hcontext, "libkrb5", tmp);
258 s = krb5_config_get_strings(context, NULL, "logging", "krb5", NULL);
263 heim_add_debug_dest(context->hcontext, "libkrb5", *p);
264 krb5_config_free_strings(s);
267 tmp = krb5_config_get_string(context, NULL, "libdefaults",
268 "check-rd-req-server", NULL);
270 tmp = secure_getenv("KRB5_CHECK_RD_REQ_SERVER");
272 if (strcasecmp(tmp, "ignore") == 0)
273 context->flags |= KRB5_CTX_F_RD_REQ_IGNORE;
275 ret = krb5_config_get_bool_default(context, NULL, TRUE,
277 "fcache_strict_checking", NULL);
279 context->flags |= KRB5_CTX_F_FCACHE_STRICT_CHECKING;
284 static krb5_error_code
285 cc_ops_register(krb5_context context)
287 context->cc_ops = NULL;
288 context->num_cc_ops = 0;
290 #ifndef KCM_IS_API_CACHE
291 krb5_cc_register(context, &krb5_acc_ops, TRUE);
293 krb5_cc_register(context, &krb5_fcc_ops, TRUE);
294 krb5_cc_register(context, &krb5_dcc_ops, TRUE);
295 krb5_cc_register(context, &krb5_mcc_ops, TRUE);
297 krb5_cc_register(context, &krb5_scc_ops, TRUE);
300 #ifdef KCM_IS_API_CACHE
301 krb5_cc_register(context, &krb5_akcm_ops, TRUE);
303 krb5_cc_register(context, &krb5_kcm_ops, TRUE);
305 #if defined(HAVE_KEYUTILS_H)
306 krb5_cc_register(context, &krb5_krcc_ops, TRUE);
308 _krb5_load_ccache_plugins(context);
312 static krb5_error_code
313 cc_ops_copy(krb5_context context, const krb5_context src_context)
315 const krb5_cc_ops **cc_ops;
317 context->cc_ops = NULL;
318 context->num_cc_ops = 0;
320 if (src_context->num_cc_ops == 0)
323 cc_ops = malloc(sizeof(cc_ops[0]) * src_context->num_cc_ops);
324 if (cc_ops == NULL) {
325 krb5_set_error_message(context, KRB5_CC_NOMEM,
326 N_("malloc: out of memory", ""));
327 return KRB5_CC_NOMEM;
330 memcpy(rk_UNCONST(cc_ops), src_context->cc_ops,
331 sizeof(cc_ops[0]) * src_context->num_cc_ops);
332 context->cc_ops = cc_ops;
333 context->num_cc_ops = src_context->num_cc_ops;
338 static krb5_error_code
339 kt_ops_register(krb5_context context)
341 context->num_kt_types = 0;
342 context->kt_types = NULL;
344 krb5_kt_register (context, &krb5_fkt_ops);
345 krb5_kt_register (context, &krb5_wrfkt_ops);
346 krb5_kt_register (context, &krb5_javakt_ops);
347 krb5_kt_register (context, &krb5_mkt_ops);
348 #ifndef HEIMDAL_SMALLER
349 krb5_kt_register (context, &krb5_akf_ops);
351 krb5_kt_register (context, &krb5_any_ops);
355 static krb5_error_code
356 kt_ops_copy(krb5_context context, const krb5_context src_context)
358 context->num_kt_types = 0;
359 context->kt_types = NULL;
361 if (src_context->num_kt_types == 0)
364 context->kt_types = malloc(sizeof(context->kt_types[0]) * src_context->num_kt_types);
365 if (context->kt_types == NULL)
366 return krb5_enomem(context);
368 context->num_kt_types = src_context->num_kt_types;
369 memcpy(context->kt_types, src_context->kt_types,
370 sizeof(context->kt_types[0]) * src_context->num_kt_types);
375 static const char *sysplugin_dirs[] = {
379 "$ORIGIN/../lib/plugin/krb5",
382 LIBDIR "/plugin/krb5",
383 #ifdef HEIM_PLUGINS_SEARCH_SYSTEM
384 "/Library/KerberosPlugins/KerberosFrameworkPlugins",
385 "/System/Library/KerberosPlugins/KerberosFrameworkPlugins",
392 init_context_once(void *ctx)
394 krb5_context context = ctx;
398 dirs = rk_UNCONST(sysplugin_dirs);
400 dirs = krb5_config_get_strings(context, NULL, "libdefaults",
403 dirs = rk_UNCONST(sysplugin_dirs);
406 _krb5_load_plugins(context, "krb5", (const char **)dirs);
408 if (dirs != rk_UNCONST(sysplugin_dirs))
409 krb5_config_free_strings(dirs);
411 bindtextdomain(HEIMDAL_TEXTDOMAIN, HEIMDAL_LOCALEDIR);
415 * Initializes the context structure and reads the configuration file
416 * /etc/krb5.conf. The structure should be freed by calling
417 * krb5_free_context() when it is no longer being used.
419 * @param context pointer to returned context
421 * @return Returns 0 to indicate success. Otherwise an errno code is
422 * returned. Failure means either that something bad happened during
423 * initialization (typically ENOMEM) or that Kerberos should not be
424 * used ENXIO. If the function returns HEIM_ERR_RANDOM_OFFLINE, the
425 * random source is not available and later Kerberos calls might fail.
430 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
431 krb5_init_context(krb5_context *context)
433 static heim_base_once_t init_context = HEIM_BASE_ONCE_INIT;
442 * krb5_init_context() will get one random byte to make sure our
443 * random is alive. Assumption is that once the non blocking
444 * source allows us to pull bytes, its all seeded and allows us to
447 * Most Kerberos users calls krb5_init_context(), so this is
448 * useful point where we can do the checking.
450 ret = krb5_generate_random(&rnd, sizeof(rnd));
454 p = calloc(1, sizeof(*p));
458 if ((p->hcontext = heim_context_init()) == NULL) {
464 p->flags |= KRB5_CTX_F_HOMEDIR_ACCESS;
466 ret = krb5_get_default_config_files(&files);
469 ret = krb5_set_config_files(p, files);
470 krb5_free_config_files(files);
474 /* done enough to load plugins */
475 heim_base_once_f(&init_context, p, init_context_once);
477 /* init error tables */
483 ret = hx509_context_init(&p->hx509ctx);
488 p->flags |= KRB5_CTX_F_SOCKETS_INITIALIZED;
492 krb5_free_context(p);
495 heim_context_set_log_utc(p->hcontext, p->log_utc);
501 #ifndef HEIMDAL_SMALLER
503 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
504 krb5_get_permitted_enctypes(krb5_context context,
505 krb5_enctype **etypes)
507 return krb5_get_default_in_tkt_etypes(context, KRB5_PDU_NONE, etypes);
514 static krb5_error_code
515 copy_etypes (krb5_context context,
516 krb5_enctype *enctypes,
517 krb5_enctype **ret_enctypes)
521 for (i = 0; enctypes[i]; i++)
525 *ret_enctypes = malloc(sizeof(enctypes[0]) * i);
526 if (*ret_enctypes == NULL)
527 return krb5_enomem(context);
528 memcpy(*ret_enctypes, enctypes, sizeof(enctypes[0]) * i);
533 * Make a copy for the Kerberos 5 context, the new krb5_context shoud
534 * be freed with krb5_free_context().
536 * @param context the Kerberos context to copy
537 * @param out the copy of the Kerberos, set to NULL error.
539 * @return Returns 0 to indicate success. Otherwise an kerberos et
540 * error code is returned, see krb5_get_error_message().
545 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
546 krb5_copy_context(krb5_context context, krb5_context *out)
553 p = calloc(1, sizeof(*p));
555 return krb5_enomem(context);
557 if ((p->hcontext = heim_context_init()) == NULL) {
562 heim_context_set_log_utc(p->hcontext, context->log_utc);
564 if (context->default_cc_name &&
565 (p->default_cc_name = strdup(context->default_cc_name)) == NULL) {
569 if (context->default_cc_name_env &&
570 (p->default_cc_name_env =
571 strdup(context->default_cc_name_env)) == NULL) {
575 if (context->configured_default_cc_name &&
576 (p->configured_default_cc_name =
577 strdup(context->configured_default_cc_name)) == NULL) {
582 if (context->etypes) {
583 ret = copy_etypes(context, context->etypes, &p->etypes);
587 if (context->cfg_etypes) {
588 ret = copy_etypes(context, context->cfg_etypes, &p->cfg_etypes);
592 if (context->etypes_des) {
593 ret = copy_etypes(context, context->etypes_des, &p->etypes_des);
598 if (context->default_realms) {
599 ret = krb5_copy_host_realm(context,
600 context->default_realms, &p->default_realms);
605 ret = _krb5_config_copy(context, context->cf, &p->cf);
609 /* XXX should copy */
612 cc_ops_copy(p, context);
613 kt_ops_copy(p, context);
615 ret = krb5_set_extra_addresses(p, context->extra_addresses);
618 ret = krb5_set_extra_addresses(p, context->ignore_addresses);
622 ret = _krb5_copy_send_to_kdc_func(p, context);
631 krb5_free_context(p);
638 * Frees the krb5_context allocated by krb5_init_context().
640 * @param context context to be freed.
645 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
646 krb5_free_context(krb5_context context)
648 _krb5_free_name_canon_rules(context, context->name_canon_rules);
649 if (context->default_cc_name)
650 free(context->default_cc_name);
651 if (context->default_cc_name_env)
652 free(context->default_cc_name_env);
653 if (context->configured_default_cc_name)
654 free(context->configured_default_cc_name);
655 free(context->etypes);
656 free(context->cfg_etypes);
657 free(context->etypes_des);
658 free(context->permitted_enctypes);
659 free(context->tgs_etypes);
660 free(context->as_etypes);
661 krb5_free_host_realm (context, context->default_realms);
662 krb5_config_file_free (context, context->cf);
663 free(rk_UNCONST(context->cc_ops));
664 free(context->kt_types);
665 krb5_clear_error_message(context);
666 krb5_set_extra_addresses(context, NULL);
667 krb5_set_ignore_addresses(context, NULL);
668 krb5_set_send_to_kdc_func(context, NULL, NULL);
671 hx509_context_free(&context->hx509ctx);
674 if (context->flags & KRB5_CTX_F_SOCKETS_INITIALIZED) {
678 heim_context_free(&context->hcontext);
679 memset(context, 0, sizeof(*context));
684 * Reinit the context from a new set of filenames.
686 * @param context context to add configuration too.
687 * @param filenames array of filenames, end of list is indicated with a NULL filename.
689 * @return Returns 0 to indicate success. Otherwise an kerberos et
690 * error code is returned, see krb5_get_error_message().
695 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
696 krb5_set_config_files(krb5_context context, char **filenames)
699 heim_config_binding *tmp = NULL;
701 if ((ret = heim_set_config_files(context->hcontext, filenames,
704 krb5_config_file_free(context, context->cf);
705 context->cf = (krb5_config_binding *)tmp;
706 return init_context_from_config_file(context);
709 #ifndef HEIMDAL_SMALLER
711 * Reinit the context from configuration file contents in a C string.
712 * This should only be used in tests.
714 * @param context context to add configuration too.
715 * @param config configuration.
717 * @return Returns 0 to indicate success. Otherwise an kerberos et
718 * error code is returned, see krb5_get_error_message().
723 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
724 krb5_set_config(krb5_context context, const char *config)
727 krb5_config_binding *tmp = NULL;
729 if ((ret = krb5_config_parse_string_multi(context, config, &tmp)))
732 /* with this enabled and if there are no config files, Kerberos is
733 considererd disabled */
738 krb5_config_file_free(context, context->cf);
740 ret = init_context_from_config_file(context);
746 * `pq' isn't free, it's up the the caller
749 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
750 krb5_prepend_config_files(const char *filelist, char **pq, char ***ret_pp)
752 return heim_prepend_config_files(filelist, pq, ret_pp);
756 * Prepend the filename to the global configuration list.
758 * @param filelist a filename to add to the default list of filename
759 * @param pfilenames return array of filenames, should be freed with krb5_free_config_files().
761 * @return Returns 0 to indicate success. Otherwise an kerberos et
762 * error code is returned, see krb5_get_error_message().
767 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
768 krb5_prepend_config_files_default(const char *filelist, char ***pfilenames)
770 return heim_prepend_config_files_default(filelist, krb5_config_file,
771 "KRB5_CONFIG", pfilenames);
775 * Get the global configuration list.
777 * @param pfilenames return array of filenames, should be freed with krb5_free_config_files().
779 * @return Returns 0 to indicate success. Otherwise an kerberos et
780 * error code is returned, see krb5_get_error_message().
785 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
786 krb5_get_default_config_files(char ***pfilenames)
788 if (pfilenames == NULL)
790 return heim_get_default_config_files(krb5_config_file, "KRB5_CONFIG",
795 * Free a list of configuration files.
797 * @param filenames list, terminated with a NULL pointer, to be
798 * freed. NULL is an valid argument.
800 * @return Returns 0 to indicate success. Otherwise an kerberos et
801 * error code is returned, see krb5_get_error_message().
806 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
807 krb5_free_config_files(char **filenames)
809 heim_free_config_files(filenames);
813 * Returns the list of Kerberos encryption types sorted in order of
814 * most preferred to least preferred encryption type. Note that some
815 * encryption types might be disabled, so you need to check with
816 * krb5_enctype_valid() before using the encryption type.
818 * @return list of enctypes, terminated with ETYPE_NULL. Its a static
819 * array completed into the Kerberos library so the content doesn't
825 KRB5_LIB_FUNCTION const krb5_enctype * KRB5_LIB_CALL
826 krb5_kerberos_enctypes(krb5_context context)
828 static const krb5_enctype p[] = {
829 ETYPE_AES256_CTS_HMAC_SHA1_96,
830 ETYPE_AES128_CTS_HMAC_SHA1_96,
831 ETYPE_AES256_CTS_HMAC_SHA384_192,
832 ETYPE_AES128_CTS_HMAC_SHA256_128,
834 ETYPE_ARCFOUR_HMAC_MD5,
838 static const krb5_enctype weak[] = {
839 ETYPE_AES256_CTS_HMAC_SHA1_96,
840 ETYPE_AES128_CTS_HMAC_SHA1_96,
841 ETYPE_AES256_CTS_HMAC_SHA384_192,
842 ETYPE_AES128_CTS_HMAC_SHA256_128,
845 ETYPE_ARCFOUR_HMAC_MD5,
853 * if the list of enctypes enabled by "allow_weak_crypto"
854 * are valid, then return the former default enctype list
855 * that contained the weak entries.
857 if (krb5_enctype_valid(context, ETYPE_DES_CBC_CRC) == 0 &&
858 krb5_enctype_valid(context, ETYPE_DES_CBC_MD4) == 0 &&
859 krb5_enctype_valid(context, ETYPE_DES_CBC_MD5) == 0 &&
860 krb5_enctype_valid(context, ETYPE_DES_CBC_NONE) == 0 &&
861 krb5_enctype_valid(context, ETYPE_DES_CFB64_NONE) == 0 &&
862 krb5_enctype_valid(context, ETYPE_DES_PCBC_NONE) == 0)
872 static krb5_error_code
873 copy_enctypes(krb5_context context,
874 const krb5_enctype *in,
877 krb5_enctype *p = NULL;
880 for (n = 0; in[n]; n++)
885 return krb5_enomem(context);
886 for (n = 0, m = 0; in[n]; n++) {
887 if (krb5_enctype_valid(context, in[n]) != 0)
891 p[m] = KRB5_ENCTYPE_NULL;
894 krb5_set_error_message (context, KRB5_PROG_ETYPE_NOSUPP,
895 N_("no valid enctype set", ""));
896 return KRB5_PROG_ETYPE_NOSUPP;
904 * set `etype' to a malloced list of the default enctypes
907 static krb5_error_code
908 default_etypes(krb5_context context, krb5_enctype **etype)
910 const krb5_enctype *p = krb5_kerberos_enctypes(context);
911 return copy_enctypes(context, p, etype);
915 * Set the default encryption types that will be use in communcation
916 * with the KDC, clients and servers.
918 * @param context Kerberos 5 context.
919 * @param etypes Encryption types, array terminated with ETYPE_NULL (0).
920 * A value of NULL resets the encryption types to the defaults set in the
921 * configuration file.
923 * @return Returns 0 to indicate success. Otherwise an kerberos et
924 * error code is returned, see krb5_get_error_message().
929 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
930 krb5_set_default_in_tkt_etypes(krb5_context context,
931 const krb5_enctype *etypes)
934 krb5_enctype *p = NULL;
937 etypes = context->cfg_etypes;
941 ret = copy_enctypes(context, etypes, &p);
946 free(context->etypes);
952 * Get the default encryption types that will be use in communcation
953 * with the KDC, clients and servers.
955 * @param context Kerberos 5 context.
956 * @param pdu_type request type (AS, TGS or none)
957 * @param etypes Encryption types, array terminated with
958 * ETYPE_NULL(0), caller should free array with krb5_xfree():
960 * @return Returns 0 to indicate success. Otherwise an kerberos et
961 * error code is returned, see krb5_get_error_message().
966 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
967 krb5_get_default_in_tkt_etypes(krb5_context context,
969 krb5_enctype **etypes)
971 krb5_enctype *enctypes = NULL;
975 heim_assert(pdu_type == KRB5_PDU_AS_REQUEST ||
976 pdu_type == KRB5_PDU_TGS_REQUEST ||
977 pdu_type == KRB5_PDU_NONE, "unexpected pdu type");
979 if (pdu_type == KRB5_PDU_AS_REQUEST && context->as_etypes != NULL)
980 enctypes = context->as_etypes;
981 else if (pdu_type == KRB5_PDU_TGS_REQUEST && context->tgs_etypes != NULL)
982 enctypes = context->tgs_etypes;
983 else if (context->etypes != NULL)
984 enctypes = context->etypes;
986 if (enctypes != NULL) {
987 ret = copy_enctypes(context, enctypes, &p);
991 ret = default_etypes(context, &p);
1000 * Init the built-in ets in the Kerberos library.
1002 * @param context kerberos context to add the ets too
1007 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
1008 krb5_init_ets(krb5_context context)
1013 _krb5_init_ets(krb5_context context)
1015 heim_add_et_list(context->hcontext, initialize_krb5_error_table_r);
1016 heim_add_et_list(context->hcontext, initialize_asn1_error_table_r);
1017 heim_add_et_list(context->hcontext, initialize_heim_error_table_r);
1019 heim_add_et_list(context->hcontext, initialize_k524_error_table_r);
1020 heim_add_et_list(context->hcontext, initialize_k5e1_error_table_r);
1022 #ifdef COM_ERR_BINDDOMAIN_krb5
1023 bindtextdomain(COM_ERR_BINDDOMAIN_krb5, HEIMDAL_LOCALEDIR);
1024 bindtextdomain(COM_ERR_BINDDOMAIN_asn1, HEIMDAL_LOCALEDIR);
1025 bindtextdomain(COM_ERR_BINDDOMAIN_heim, HEIMDAL_LOCALEDIR);
1026 bindtextdomain(COM_ERR_BINDDOMAIN_k524, HEIMDAL_LOCALEDIR);
1030 heim_add_et_list(context->hcontext, initialize_hx_error_table_r);
1031 #ifdef COM_ERR_BINDDOMAIN_hx
1032 bindtextdomain(COM_ERR_BINDDOMAIN_hx, HEIMDAL_LOCALEDIR);
1038 * Make the kerberos library default to the admin KDC.
1040 * @param context Kerberos 5 context.
1041 * @param flag boolean flag to select if the use the admin KDC or not.
1046 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
1047 krb5_set_use_admin_kdc (krb5_context context, krb5_boolean flag)
1049 context->use_admin_kdc = flag;
1053 * Make the kerberos library default to the admin KDC.
1055 * @param context Kerberos 5 context.
1057 * @return boolean flag to telling the context will use admin KDC as the default KDC.
1062 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1063 krb5_get_use_admin_kdc (krb5_context context)
1065 return context->use_admin_kdc;
1069 * Add extra address to the address list that the library will add to
1070 * the client's address list when communicating with the KDC.
1072 * @param context Kerberos 5 context.
1073 * @param addresses addreses to add
1075 * @return Returns 0 to indicate success. Otherwise an kerberos et
1076 * error code is returned, see krb5_get_error_message().
1081 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1082 krb5_add_extra_addresses(krb5_context context, krb5_addresses *addresses)
1085 if(context->extra_addresses)
1086 return krb5_append_addresses(context,
1087 context->extra_addresses, addresses);
1089 return krb5_set_extra_addresses(context, addresses);
1093 * Set extra address to the address list that the library will add to
1094 * the client's address list when communicating with the KDC.
1096 * @param context Kerberos 5 context.
1097 * @param addresses addreses to set
1099 * @return Returns 0 to indicate success. Otherwise an kerberos et
1100 * error code is returned, see krb5_get_error_message().
1105 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1106 krb5_set_extra_addresses(krb5_context context, const krb5_addresses *addresses)
1108 if(context->extra_addresses)
1109 krb5_free_addresses(context, context->extra_addresses);
1111 if(addresses == NULL) {
1112 if(context->extra_addresses != NULL) {
1113 free(context->extra_addresses);
1114 context->extra_addresses = NULL;
1118 if(context->extra_addresses == NULL) {
1119 context->extra_addresses = malloc(sizeof(*context->extra_addresses));
1120 if (context->extra_addresses == NULL)
1121 return krb5_enomem(context);
1123 return krb5_copy_addresses(context, addresses, context->extra_addresses);
1127 * Get extra address to the address list that the library will add to
1128 * the client's address list when communicating with the KDC.
1130 * @param context Kerberos 5 context.
1131 * @param addresses addreses to set
1133 * @return Returns 0 to indicate success. Otherwise an kerberos et
1134 * error code is returned, see krb5_get_error_message().
1139 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1140 krb5_get_extra_addresses(krb5_context context, krb5_addresses *addresses)
1142 if(context->extra_addresses == NULL) {
1143 memset(addresses, 0, sizeof(*addresses));
1146 return krb5_copy_addresses(context,context->extra_addresses, addresses);
1150 * Add extra addresses to ignore when fetching addresses from the
1151 * underlaying operating system.
1153 * @param context Kerberos 5 context.
1154 * @param addresses addreses to ignore
1156 * @return Returns 0 to indicate success. Otherwise an kerberos et
1157 * error code is returned, see krb5_get_error_message().
1162 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1163 krb5_add_ignore_addresses(krb5_context context, krb5_addresses *addresses)
1166 if(context->ignore_addresses)
1167 return krb5_append_addresses(context,
1168 context->ignore_addresses, addresses);
1170 return krb5_set_ignore_addresses(context, addresses);
1174 * Set extra addresses to ignore when fetching addresses from the
1175 * underlaying operating system.
1177 * @param context Kerberos 5 context.
1178 * @param addresses addreses to ignore
1180 * @return Returns 0 to indicate success. Otherwise an kerberos et
1181 * error code is returned, see krb5_get_error_message().
1186 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1187 krb5_set_ignore_addresses(krb5_context context, const krb5_addresses *addresses)
1189 if(context->ignore_addresses)
1190 krb5_free_addresses(context, context->ignore_addresses);
1191 if(addresses == NULL) {
1192 if(context->ignore_addresses != NULL) {
1193 free(context->ignore_addresses);
1194 context->ignore_addresses = NULL;
1198 if(context->ignore_addresses == NULL) {
1199 context->ignore_addresses = malloc(sizeof(*context->ignore_addresses));
1200 if (context->ignore_addresses == NULL)
1201 return krb5_enomem(context);
1203 return krb5_copy_addresses(context, addresses, context->ignore_addresses);
1207 * Get extra addresses to ignore when fetching addresses from the
1208 * underlaying operating system.
1210 * @param context Kerberos 5 context.
1211 * @param addresses list addreses ignored
1213 * @return Returns 0 to indicate success. Otherwise an kerberos et
1214 * error code is returned, see krb5_get_error_message().
1219 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1220 krb5_get_ignore_addresses(krb5_context context, krb5_addresses *addresses)
1222 if(context->ignore_addresses == NULL) {
1223 memset(addresses, 0, sizeof(*addresses));
1226 return krb5_copy_addresses(context, context->ignore_addresses, addresses);
1230 * Set version of fcache that the library should use.
1232 * @param context Kerberos 5 context.
1233 * @param version version number.
1235 * @return Returns 0 to indicate success. Otherwise an kerberos et
1236 * error code is returned, see krb5_get_error_message().
1241 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1242 krb5_set_fcache_version(krb5_context context, int version)
1244 context->fcache_vno = version;
1249 * Get version of fcache that the library should use.
1251 * @param context Kerberos 5 context.
1252 * @param version version number.
1254 * @return Returns 0 to indicate success. Otherwise an kerberos et
1255 * error code is returned, see krb5_get_error_message().
1260 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1261 krb5_get_fcache_version(krb5_context context, int *version)
1263 *version = context->fcache_vno;
1268 * Runtime check if the Kerberos library was complied with thread support.
1270 * @return TRUE if the library was compiled with thread support, FALSE if not.
1276 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1277 krb5_is_thread_safe(void)
1279 #ifdef ENABLE_PTHREAD_SUPPORT
1287 * Set if the library should use DNS to canonicalize hostnames.
1289 * @param context Kerberos 5 context.
1290 * @param flag if its dns canonicalizion is used or not.
1295 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
1296 krb5_set_dns_canonicalize_hostname (krb5_context context, krb5_boolean flag)
1299 context->flags |= KRB5_CTX_F_DNS_CANONICALIZE_HOSTNAME;
1301 context->flags &= ~KRB5_CTX_F_DNS_CANONICALIZE_HOSTNAME;
1305 * Get if the library uses DNS to canonicalize hostnames.
1307 * @param context Kerberos 5 context.
1309 * @return return non zero if the library uses DNS to canonicalize hostnames.
1314 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1315 krb5_get_dns_canonicalize_hostname (krb5_context context)
1317 return (context->flags & KRB5_CTX_F_DNS_CANONICALIZE_HOSTNAME) ? 1 : 0;
1321 * Get current offset in time to the KDC.
1323 * @param context Kerberos 5 context.
1324 * @param sec seconds part of offset.
1325 * @param usec micro seconds part of offset.
1327 * @return returns zero
1332 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1333 krb5_get_kdc_sec_offset (krb5_context context, int32_t *sec, int32_t *usec)
1336 *sec = context->kdc_sec_offset;
1338 *usec = context->kdc_usec_offset;
1343 * Set 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
1354 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1355 krb5_set_kdc_sec_offset (krb5_context context, int32_t sec, int32_t usec)
1357 context->kdc_sec_offset = sec;
1359 context->kdc_usec_offset = usec;
1364 * Get max time skew allowed.
1366 * @param context Kerberos 5 context.
1368 * @return timeskew in seconds.
1373 KRB5_LIB_FUNCTION time_t KRB5_LIB_CALL
1374 krb5_get_max_time_skew (krb5_context context)
1376 return context->max_skew;
1380 * Set max time skew allowed.
1382 * @param context Kerberos 5 context.
1383 * @param t timeskew in seconds.
1388 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
1389 krb5_set_max_time_skew (krb5_context context, time_t t)
1391 context->max_skew = t;
1395 * Init encryption types in len, val with etypes.
1397 * @param context Kerberos 5 context.
1398 * @param pdu_type type of pdu
1399 * @param len output length of val.
1400 * @param val output array of enctypes.
1401 * @param etypes etypes to set val and len to, if NULL, use default enctypes.
1403 * @return Returns 0 to indicate success. Otherwise an kerberos et
1404 * error code is returned, see krb5_get_error_message().
1409 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1410 _krb5_init_etype(krb5_context context,
1414 const krb5_enctype *etypes)
1416 krb5_error_code ret;
1419 ret = krb5_get_default_in_tkt_etypes(context, pdu_type, val);
1421 ret = copy_enctypes(context, etypes, val);
1427 while ((*val)[*len] != KRB5_ENCTYPE_NULL)
1434 * Allow homedir access
1437 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1438 _krb5_homedir_access(krb5_context context)
1441 return !!(context->flags & KRB5_CTX_F_HOMEDIR_ACCESS);
1446 * Enable and disable home directory access on either the global state
1447 * or the krb5_context state. By calling krb5_set_home_dir_access()
1448 * with context set to NULL, the global state is configured otherwise
1449 * the state for the krb5_context is modified.
1451 * For home directory access to be allowed, both the global state and
1452 * the krb5_context state have to be allowed.
1454 * @param context a Kerberos 5 context or NULL
1455 * @param allow allow if TRUE home directory
1456 * @return the old value
1461 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1462 krb5_set_home_dir_access(krb5_context context, krb5_boolean allow)
1464 krb5_boolean old = _krb5_homedir_access(context);
1468 context->flags |= KRB5_CTX_F_HOMEDIR_ACCESS;
1470 context->flags &= ~KRB5_CTX_F_HOMEDIR_ACCESS;
1471 heim_context_set_homedir_access(context->hcontext, allow ? 1 : 0);