1 /* $NetBSD: context.c,v 1.4 2014/04/24 13:45:34 pettai Exp $ */
4 * Copyright (c) 1997 - 2010 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
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
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
38 #include "krb5_locl.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) \
48 if (krb5_config_get_bool_default((C), NULL, (D),"libdefaults", F, NULL)) { \
54 * Set the list of etypes `ret_etypes' from the configuration variable
58 static krb5_error_code
59 set_etypes (krb5_context context
,
61 krb5_enctype
**ret_enctypes
)
64 krb5_enctype
*etypes
= NULL
;
66 etypes_str
= krb5_config_get_strings(context
, NULL
, "libdefaults",
70 for(i
= 0; etypes_str
[i
]; i
++);
71 etypes
= malloc((i
+1) * sizeof(*etypes
));
73 krb5_config_free_strings (etypes_str
);
74 krb5_set_error_message (context
, ENOMEM
, N_("malloc: out of memory", ""));
77 for(j
= 0, k
= 0; j
< i
; j
++) {
79 if(krb5_string_to_enctype(context
, etypes_str
[j
], &e
) != 0)
81 if (krb5_enctype_valid(context
, e
) != 0)
85 etypes
[k
] = ETYPE_NULL
;
86 krb5_config_free_strings(etypes_str
);
88 *ret_enctypes
= etypes
;
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
)
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
,
112 "allow_weak_crypto", NULL
);
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
);
125 free(context
->etypes
);
126 context
->etypes
= tmptypes
;
128 ret
= set_etypes (context
, "default_etypes_des", &tmptypes
);
131 free(context
->etypes_des
);
132 context
->etypes_des
= tmptypes
;
134 ret
= set_etypes (context
, "default_as_etypes", &tmptypes
);
137 free(context
->as_etypes
);
138 context
->as_etypes
= tmptypes
;
140 ret
= set_etypes (context
, "default_tgs_etypes", &tmptypes
);
143 free(context
->tgs_etypes
);
144 context
->tgs_etypes
= tmptypes
;
146 ret
= set_etypes (context
, "permitted_enctypes", &tmptypes
);
149 free(context
->permitted_enctypes
);
150 context
->permitted_enctypes
= tmptypes
;
152 /* default keytab name */
155 tmp
= getenv("KRB5_KTNAME");
157 context
->default_keytab
= tmp
;
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
,
176 /* init dns-proxy slime */
177 tmp
= krb5_config_get_string(context
, NULL
, "libdefaults",
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
;
188 krb5_set_extra_addresses(context
, NULL
);
189 adr
= krb5_config_get_strings(context
, NULL
,
193 memset(&addresses
, 0, sizeof(addresses
));
194 for(a
= adr
; a
&& *a
; a
++) {
195 ret
= krb5_parse_address(context
, *a
, &addresses
);
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
,
208 memset(&addresses
, 0, sizeof(addresses
));
209 for(a
= adr
; a
&& *a
; a
++) {
210 ret
= krb5_parse_address(context
, *a
, &addresses
);
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
);
233 krb5_initlog(context
, "libkrb5", &context
->debug_dest
);
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");
244 if (strcasecmp(tmp
, "ignore") == 0)
245 context
->flags
|= KRB5_CTX_F_RD_REQ_IGNORE
;
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
);
260 krb5_cc_register(context
, &krb5_fcc_ops
, TRUE
);
261 krb5_cc_register(context
, &krb5_mcc_ops
, TRUE
);
263 krb5_cc_register(context
, &krb5_scc_ops
, TRUE
);
266 #ifdef KCM_IS_API_CACHE
267 krb5_cc_register(context
, &krb5_akcm_ops
, TRUE
);
269 krb5_cc_register(context
, &krb5_kcm_ops
, TRUE
);
271 _krb5_load_ccache_plugins(context
);
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)
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
;
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
);
314 krb5_kt_register (context
, &krb5_any_ops
);
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)
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", ""));
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
);
341 static const char *sysplugin_dirs
[] = {
342 LIBDIR
"/plugin/krb5",
344 "/Library/KerberosPlugins/KerberosFrameworkPlugins",
345 "/System/Library/KerberosPlugins/KerberosFrameworkPlugins",
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
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
;
386 p
= calloc(1, sizeof(*p
));
390 p
->mutex
= malloc(sizeof(HEIMDAL_MUTEX
));
391 if (p
->mutex
== NULL
) {
395 HEIMDAL_MUTEX_init(p
->mutex
);
397 p
->flags
|= KRB5_CTX_F_HOMEDIR_ACCESS
;
399 ret
= krb5_get_default_config_files(&files
);
402 ret
= krb5_set_config_files(p
, files
);
403 krb5_free_config_files(files
);
407 /* init error tables */
413 ret
= hx509_context_init(&p
->hx509ctx
);
418 p
->flags
|= KRB5_CTX_F_SOCKETS_INITIALIZED
;
422 krb5_free_context(p
);
425 heim_base_once_f(&init_context
, p
, init_context_once
);
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
)
451 for (i
= 0; enctypes
[i
]; 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", ""));
461 memcpy(*ret_enctypes
, enctypes
, sizeof(**ret_enctypes
) * i
);
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().
478 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
479 krb5_copy_context(krb5_context context
, krb5_context
*out
)
486 p
= calloc(1, sizeof(*p
));
488 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
492 p
->mutex
= malloc(sizeof(HEIMDAL_MUTEX
));
493 if (p
->mutex
== NULL
) {
494 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
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
);
511 if (context
->etypes_des
) {
512 ret
= copy_etypes(context
, context
->etypes_des
, &p
->etypes_des
);
517 if (context
->default_realms
) {
518 ret
= krb5_copy_host_realm(context
,
519 context
->default_realms
, &p
->default_realms
);
524 ret
= _krb5_config_copy(context
, context
->cf
, &p
->cf
);
528 /* XXX should copy */
531 cc_ops_copy(p
, context
);
532 kt_ops_copy(p
, context
);
535 if(context
->warn_dest
!= NULL
)
537 if(context
->debug_dest
!= NULL
)
541 ret
= krb5_set_extra_addresses(p
, context
->extra_addresses
);
544 ret
= krb5_set_extra_addresses(p
, context
->ignore_addresses
);
548 ret
= _krb5_copy_send_to_kdc_func(p
, context
);
557 krb5_free_context(p
);
564 * Frees the krb5_context allocated by krb5_init_context().
566 * @param context context to be freed.
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
);
595 if (context
->hx509ctx
)
596 hx509_context_free(&context
->hx509ctx
);
599 HEIMDAL_MUTEX_destroy(context
->mutex
);
600 free(context
->mutex
);
601 if (context
->flags
& KRB5_CTX_F_SOCKETS_INITIALIZED
) {
605 memset(context
, 0, sizeof(*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().
621 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
622 krb5_set_config_files(krb5_context context
, char **filenames
)
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
);
635 /* with this enabled and if there are no config files, Kerberos is
636 considererd disabled */
642 _krb5_load_config_from_registry(context
, &tmp
);
645 krb5_config_file_free(context
, context
->cf
);
647 ret
= init_context_from_config_file(context
);
651 static krb5_error_code
652 add_file(char ***pfilenames
, int *len
, char *file
)
654 char **pp
= *pfilenames
;
657 for(i
= 0; i
< *len
; i
++) {
658 if(strcmp(pp
[i
], file
) == 0) {
664 pp
= realloc(*pfilenames
, (*len
+ 2) * sizeof(*pp
));
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
)
697 l
= strsep_copy(&q
, PATH_SEP
, NULL
, 0);
702 krb5_free_config_files(pp
);
705 (void)strsep_copy(&p
, PATH_SEP
, fn
, l
+ 1);
706 ret
= add_file(&pp
, &len
, fn
);
708 krb5_free_config_files(pp
);
716 for (i
= 0; pq
[i
] != NULL
; i
++) {
719 krb5_free_config_files(pp
);
722 ret
= add_file(&pp
, &len
, fn
);
724 krb5_free_config_files(pp
);
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().
746 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
747 krb5_prepend_config_files_default(const char *filelist
, char ***pfilenames
)
750 char **defpp
, **pp
= NULL
;
752 ret
= krb5_get_default_config_files(&defpp
);
756 ret
= krb5_prepend_config_files(filelist
, defpp
, &pp
);
757 krb5_free_config_files(defpp
);
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".
775 _krb5_get_default_config_config_files_from_registry()
777 static const char * KeyName
= "Software\\MIT\\Kerberos";
778 char *config_file
= NULL
;
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
);
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
);
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().
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
)
823 files
= getenv("KRB5_CONFIG");
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
);
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().
858 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
859 krb5_free_config_files(char **filenames
)
862 for(p
= filenames
; p
&& *p
!= NULL
; p
++)
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
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
,
888 ETYPE_ARCFOUR_HMAC_MD5
,
901 static krb5_error_code
902 copy_enctypes(krb5_context context
,
903 const krb5_enctype
*in
,
906 krb5_enctype
*p
= NULL
;
909 for (n
= 0; in
[n
]; n
++)
914 return krb5_enomem(context
);
915 for (n
= 0, m
= 0; in
[n
]; n
++) {
916 if (krb5_enctype_valid(context
, in
[n
]) != 0)
920 p
[m
] = KRB5_ENCTYPE_NULL
;
923 krb5_set_error_message (context
, KRB5_PROG_ETYPE_NOSUPP
,
924 N_("no valid enctype set", ""));
925 return KRB5_PROG_ETYPE_NOSUPP
;
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().
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
)
961 krb5_enctype
*p
= NULL
;
964 ret
= copy_enctypes(context
, etypes
, &p
);
969 free(context
->etypes
);
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().
988 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
989 krb5_get_default_in_tkt_etypes(krb5_context context
,
991 krb5_enctype
**etypes
)
993 krb5_enctype
*enctypes
= NULL
;
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
);
1013 ret
= default_etypes(context
, &p
);
1022 * Init the built-in ets in the Kerberos library.
1024 * @param context kerberos context to add the ets too
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
);
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
);
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.
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.
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().
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
);
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().
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
;
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", ""));
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().
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
));
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().
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
);
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().
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
;
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", ""));
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().
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
));
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().
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
;
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().
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
;
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.
1298 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1299 krb5_is_thread_safe(void)
1301 #ifdef ENABLE_PTHREAD_SUPPORT
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.
1317 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
1318 krb5_set_dns_canonicalize_hostname (krb5_context context
, krb5_boolean flag
)
1321 context
->flags
|= KRB5_CTX_F_DNS_CANONICALIZE_HOSTNAME
;
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.
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
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
)
1358 *sec
= context
->kdc_sec_offset
;
1360 *usec
= context
->kdc_usec_offset
;
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
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
;
1381 context
->kdc_usec_offset
= usec
;
1386 * Get max time skew allowed.
1388 * @param context Kerberos 5 context.
1390 * @return timeskew in seconds.
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.
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().
1431 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1432 _krb5_init_etype(krb5_context context
,
1436 const krb5_enctype
*etypes
)
1438 krb5_error_code ret
;
1441 ret
= krb5_get_default_in_tkt_etypes(context
, pdu_type
, val
);
1443 ret
= copy_enctypes(context
, etypes
, val
);
1449 while ((*val
)[*len
] != KRB5_ENCTYPE_NULL
)
1456 * Allow homedir accces
1459 static HEIMDAL_MUTEX homedir_mutex
= HEIMDAL_MUTEX_INITIALIZER
;
1460 static krb5_boolean allow_homedir
= TRUE
;
1463 _krb5_homedir_access(krb5_context context
)
1468 /* is never allowed for root */
1473 if (context
&& (context
->flags
& KRB5_CTX_F_HOMEDIR_ACCESS
) == 0)
1476 HEIMDAL_MUTEX_lock(&homedir_mutex
);
1477 allow
= allow_homedir
;
1478 HEIMDAL_MUTEX_unlock(&homedir_mutex
);
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
1500 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1501 krb5_set_home_dir_access(krb5_context context
, krb5_boolean allow
)
1505 old
= (context
->flags
& KRB5_CTX_F_HOMEDIR_ACCESS
) ? TRUE
: FALSE
;
1507 context
->flags
|= KRB5_CTX_F_HOMEDIR_ACCESS
;
1509 context
->flags
&= ~KRB5_CTX_F_HOMEDIR_ACCESS
;
1511 HEIMDAL_MUTEX_lock(&homedir_mutex
);
1512 old
= allow_homedir
;
1513 allow_homedir
= allow
;
1514 HEIMDAL_MUTEX_unlock(&homedir_mutex
);