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 #include "krb5_locl.h"
40 #define INIT_FIELD(C, T, E, D, F) \
41 (C)->E = krb5_config_get_ ## T ## _default ((C), NULL, (D), \
42 "libdefaults", F, NULL)
44 #define INIT_FLAG(C, O, V, D, F) \
46 if (krb5_config_get_bool_default((C), NULL, (D),"libdefaults", F, NULL)) { \
52 * Set the list of etypes `ret_etypes' from the configuration variable
56 static krb5_error_code
57 set_etypes (krb5_context context
,
59 krb5_enctype
**ret_enctypes
)
62 krb5_enctype
*etypes
= NULL
;
64 etypes_str
= krb5_config_get_strings(context
, NULL
, "libdefaults",
68 for(i
= 0; etypes_str
[i
]; i
++);
69 etypes
= malloc((i
+1) * sizeof(*etypes
));
71 krb5_config_free_strings (etypes_str
);
72 return krb5_enomem(context
);
74 for(j
= 0, k
= 0; j
< i
; j
++) {
76 if(krb5_string_to_enctype(context
, etypes_str
[j
], &e
) != 0)
78 if (krb5_enctype_valid(context
, e
) != 0)
82 etypes
[k
] = ETYPE_NULL
;
83 krb5_config_free_strings(etypes_str
);
85 *ret_enctypes
= etypes
;
90 * read variables from the configuration file and set in `context'
93 static krb5_error_code
94 init_context_from_config_file(krb5_context context
)
99 krb5_enctype
*tmptypes
;
101 INIT_FIELD(context
, time
, max_skew
, 5 * 60, "clockskew");
102 INIT_FIELD(context
, time
, kdc_timeout
, 30, "kdc_timeout");
103 INIT_FIELD(context
, time
, host_timeout
, 3, "host_timeout");
104 INIT_FIELD(context
, int, max_retries
, 3, "max_retries");
106 INIT_FIELD(context
, string
, http_proxy
, NULL
, "http_proxy");
108 ret
= krb5_config_get_bool_default(context
, NULL
, FALSE
,
110 "allow_weak_crypto", NULL
);
112 krb5_enctype_enable(context
, ETYPE_DES_CBC_CRC
);
113 krb5_enctype_enable(context
, ETYPE_DES_CBC_MD4
);
114 krb5_enctype_enable(context
, ETYPE_DES_CBC_MD5
);
115 krb5_enctype_enable(context
, ETYPE_DES_CBC_NONE
);
116 krb5_enctype_enable(context
, ETYPE_DES_CFB64_NONE
);
117 krb5_enctype_enable(context
, ETYPE_DES_PCBC_NONE
);
120 ret
= set_etypes (context
, "default_etypes", &tmptypes
);
123 free(context
->etypes
);
124 context
->etypes
= tmptypes
;
126 ret
= set_etypes (context
, "default_etypes_des", &tmptypes
);
129 free(context
->etypes_des
);
130 context
->etypes_des
= tmptypes
;
132 ret
= set_etypes (context
, "default_as_etypes", &tmptypes
);
135 free(context
->as_etypes
);
136 context
->as_etypes
= tmptypes
;
138 ret
= set_etypes (context
, "default_tgs_etypes", &tmptypes
);
141 free(context
->tgs_etypes
);
142 context
->tgs_etypes
= tmptypes
;
144 ret
= set_etypes (context
, "permitted_enctypes", &tmptypes
);
147 free(context
->permitted_enctypes
);
148 context
->permitted_enctypes
= tmptypes
;
150 /* default keytab name */
153 tmp
= getenv("KRB5_KTNAME");
155 context
->default_keytab
= tmp
;
157 INIT_FIELD(context
, string
, default_keytab
,
158 KEYTAB_DEFAULT
, "default_keytab_name");
160 INIT_FIELD(context
, string
, default_keytab_modify
,
161 NULL
, "default_keytab_modify_name");
163 INIT_FIELD(context
, string
, time_fmt
,
164 "%Y-%m-%dT%H:%M:%S", "time_format");
166 INIT_FIELD(context
, string
, date_fmt
,
167 "%Y-%m-%d", "date_format");
169 INIT_FIELD(context
, bool, log_utc
,
174 /* init dns-proxy slime */
175 tmp
= krb5_config_get_string(context
, NULL
, "libdefaults",
178 roken_gethostby_setup(context
->http_proxy
, tmp
);
179 krb5_free_host_realm (context
, context
->default_realms
);
180 context
->default_realms
= NULL
;
183 krb5_addresses addresses
;
186 krb5_set_extra_addresses(context
, NULL
);
187 adr
= krb5_config_get_strings(context
, NULL
,
191 memset(&addresses
, 0, sizeof(addresses
));
192 for(a
= adr
; a
&& *a
; a
++) {
193 ret
= krb5_parse_address(context
, *a
, &addresses
);
195 krb5_add_extra_addresses(context
, &addresses
);
196 krb5_free_addresses(context
, &addresses
);
199 krb5_config_free_strings(adr
);
201 krb5_set_ignore_addresses(context
, NULL
);
202 adr
= krb5_config_get_strings(context
, NULL
,
206 memset(&addresses
, 0, sizeof(addresses
));
207 for(a
= adr
; a
&& *a
; a
++) {
208 ret
= krb5_parse_address(context
, *a
, &addresses
);
210 krb5_add_ignore_addresses(context
, &addresses
);
211 krb5_free_addresses(context
, &addresses
);
214 krb5_config_free_strings(adr
);
217 INIT_FIELD(context
, bool, scan_interfaces
, TRUE
, "scan_interfaces");
218 INIT_FIELD(context
, int, fcache_vno
, 0, "fcache_version");
219 /* prefer dns_lookup_kdc over srv_lookup. */
220 INIT_FIELD(context
, bool, srv_lookup
, TRUE
, "srv_lookup");
221 INIT_FIELD(context
, bool, srv_lookup
, context
->srv_lookup
, "dns_lookup_kdc");
222 INIT_FIELD(context
, int, large_msg_size
, 1400, "large_message_size");
223 INIT_FIELD(context
, int, max_msg_size
, 1000 * 1024, "maximum_message_size");
224 INIT_FLAG(context
, flags
, KRB5_CTX_F_DNS_CANONICALIZE_HOSTNAME
, TRUE
, "dns_canonicalize_hostname");
225 INIT_FLAG(context
, flags
, KRB5_CTX_F_CHECK_PAC
, TRUE
, "check_pac");
227 if (context
->default_cc_name
)
228 free(context
->default_cc_name
);
229 context
->default_cc_name
= NULL
;
230 context
->default_cc_name_set
= 0;
232 s
= krb5_config_get_strings(context
, NULL
, "logging", "krb5", NULL
);
236 if (context
->debug_dest
)
237 krb5_closelog(context
, context
->debug_dest
);
239 krb5_initlog(context
, "libkrb5", &context
->debug_dest
);
241 krb5_addlog_dest(context
, context
->debug_dest
, *p
);
242 krb5_config_free_strings(s
);
245 tmp
= krb5_config_get_string(context
, NULL
, "libdefaults",
246 "check-rd-req-server", NULL
);
247 if (tmp
== NULL
&& !issuid())
248 tmp
= getenv("KRB5_CHECK_RD_REQ_SERVER");
250 if (strcasecmp(tmp
, "ignore") == 0)
251 context
->flags
|= KRB5_CTX_F_RD_REQ_IGNORE
;
253 ret
= krb5_config_get_bool_default(context
, NULL
, TRUE
,
255 "fcache_strict_checking", NULL
);
257 context
->flags
|= KRB5_CTX_F_FCACHE_STRICT_CHECKING
;
262 static krb5_error_code
263 cc_ops_register(krb5_context context
)
265 context
->cc_ops
= NULL
;
266 context
->num_cc_ops
= 0;
268 #ifndef KCM_IS_API_CACHE
269 krb5_cc_register(context
, &krb5_acc_ops
, TRUE
);
271 krb5_cc_register(context
, &krb5_fcc_ops
, TRUE
);
272 krb5_cc_register(context
, &krb5_dcc_ops
, TRUE
);
273 krb5_cc_register(context
, &krb5_mcc_ops
, TRUE
);
275 krb5_cc_register(context
, &krb5_scc_ops
, TRUE
);
278 #ifdef KCM_IS_API_CACHE
279 krb5_cc_register(context
, &krb5_akcm_ops
, TRUE
);
281 krb5_cc_register(context
, &krb5_kcm_ops
, TRUE
);
283 _krb5_load_ccache_plugins(context
);
287 static krb5_error_code
288 cc_ops_copy(krb5_context context
, const krb5_context src_context
)
290 const krb5_cc_ops
**cc_ops
;
292 context
->cc_ops
= NULL
;
293 context
->num_cc_ops
= 0;
295 if (src_context
->num_cc_ops
== 0)
298 cc_ops
= malloc(sizeof(cc_ops
[0]) * src_context
->num_cc_ops
);
299 if (cc_ops
== NULL
) {
300 krb5_set_error_message(context
, KRB5_CC_NOMEM
,
301 N_("malloc: out of memory", ""));
302 return KRB5_CC_NOMEM
;
305 memcpy(rk_UNCONST(cc_ops
), src_context
->cc_ops
,
306 sizeof(cc_ops
[0]) * src_context
->num_cc_ops
);
307 context
->cc_ops
= cc_ops
;
308 context
->num_cc_ops
= src_context
->num_cc_ops
;
313 static krb5_error_code
314 kt_ops_register(krb5_context context
)
316 context
->num_kt_types
= 0;
317 context
->kt_types
= NULL
;
319 krb5_kt_register (context
, &krb5_fkt_ops
);
320 krb5_kt_register (context
, &krb5_wrfkt_ops
);
321 krb5_kt_register (context
, &krb5_javakt_ops
);
322 krb5_kt_register (context
, &krb5_mkt_ops
);
323 #ifndef HEIMDAL_SMALLER
324 krb5_kt_register (context
, &krb5_akf_ops
);
326 krb5_kt_register (context
, &krb5_any_ops
);
330 static krb5_error_code
331 kt_ops_copy(krb5_context context
, const krb5_context src_context
)
333 context
->num_kt_types
= 0;
334 context
->kt_types
= NULL
;
336 if (src_context
->num_kt_types
== 0)
339 context
->kt_types
= malloc(sizeof(context
->kt_types
[0]) * src_context
->num_kt_types
);
340 if (context
->kt_types
== NULL
)
341 return krb5_enomem(context
);
343 context
->num_kt_types
= src_context
->num_kt_types
;
344 memcpy(context
->kt_types
, src_context
->kt_types
,
345 sizeof(context
->kt_types
[0]) * src_context
->num_kt_types
);
350 static const char *sysplugin_dirs
[] = {
354 "$ORIGIN/../lib/plugin/krb5",
357 LIBDIR
"/plugin/krb5",
358 "/Library/KerberosPlugins/KerberosFrameworkPlugins",
359 "/System/Library/KerberosPlugins/KerberosFrameworkPlugins",
365 init_context_once(void *ctx
)
367 krb5_context context
= ctx
;
371 dirs
= rk_UNCONST(sysplugin_dirs
);
373 dirs
= krb5_config_get_strings(context
, NULL
, "libdefaults",
376 dirs
= rk_UNCONST(sysplugin_dirs
);
379 _krb5_load_plugins(context
, "krb5", (const char **)dirs
);
381 if (dirs
!= rk_UNCONST(sysplugin_dirs
))
382 krb5_config_free_strings(dirs
);
384 bindtextdomain(HEIMDAL_TEXTDOMAIN
, HEIMDAL_LOCALEDIR
);
389 * Initializes the context structure and reads the configuration file
390 * /etc/krb5.conf. The structure should be freed by calling
391 * krb5_free_context() when it is no longer being used.
393 * @param context pointer to returned context
395 * @return Returns 0 to indicate success. Otherwise an errno code is
396 * returned. Failure means either that something bad happened during
397 * initialization (typically ENOMEM) or that Kerberos should not be
403 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
404 krb5_init_context(krb5_context
*context
)
406 static heim_base_once_t init_context
= HEIM_BASE_ONCE_INIT
;
413 p
= calloc(1, sizeof(*p
));
417 p
->mutex
= malloc(sizeof(HEIMDAL_MUTEX
));
418 if (p
->mutex
== NULL
) {
422 HEIMDAL_MUTEX_init(p
->mutex
);
424 p
->flags
|= KRB5_CTX_F_HOMEDIR_ACCESS
;
426 ret
= krb5_get_default_config_files(&files
);
429 ret
= krb5_set_config_files(p
, files
);
430 krb5_free_config_files(files
);
434 /* done enough to load plugins */
435 heim_base_once_f(&init_context
, p
, init_context_once
);
437 /* init error tables */
443 ret
= hx509_context_init(&p
->hx509ctx
);
448 p
->flags
|= KRB5_CTX_F_SOCKETS_INITIALIZED
;
452 krb5_free_context(p
);
459 #ifndef HEIMDAL_SMALLER
461 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
462 krb5_get_permitted_enctypes(krb5_context context
,
463 krb5_enctype
**etypes
)
465 return krb5_get_default_in_tkt_etypes(context
, KRB5_PDU_NONE
, etypes
);
472 static krb5_error_code
473 copy_etypes (krb5_context context
,
474 krb5_enctype
*enctypes
,
475 krb5_enctype
**ret_enctypes
)
479 for (i
= 0; enctypes
[i
]; i
++)
483 *ret_enctypes
= malloc(sizeof(ret_enctypes
[0]) * i
);
484 if (*ret_enctypes
== NULL
)
485 return krb5_enomem(context
);
486 memcpy(*ret_enctypes
, enctypes
, sizeof(ret_enctypes
[0]) * i
);
491 * Make a copy for the Kerberos 5 context, the new krb5_context shoud
492 * be freed with krb5_free_context().
494 * @param context the Kerberos context to copy
495 * @param out the copy of the Kerberos, set to NULL error.
497 * @return Returns 0 to indicate success. Otherwise an kerberos et
498 * error code is returned, see krb5_get_error_message().
503 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
504 krb5_copy_context(krb5_context context
, krb5_context
*out
)
511 p
= calloc(1, sizeof(*p
));
513 return krb5_enomem(context
);
515 p
->mutex
= malloc(sizeof(HEIMDAL_MUTEX
));
516 if (p
->mutex
== NULL
) {
518 return krb5_enomem(context
);
520 HEIMDAL_MUTEX_init(p
->mutex
);
523 if (context
->default_cc_name
)
524 p
->default_cc_name
= strdup(context
->default_cc_name
);
525 if (context
->default_cc_name_env
)
526 p
->default_cc_name_env
= strdup(context
->default_cc_name_env
);
528 if (context
->etypes
) {
529 ret
= copy_etypes(context
, context
->etypes
, &p
->etypes
);
533 if (context
->etypes_des
) {
534 ret
= copy_etypes(context
, context
->etypes_des
, &p
->etypes_des
);
539 if (context
->default_realms
) {
540 ret
= krb5_copy_host_realm(context
,
541 context
->default_realms
, &p
->default_realms
);
546 ret
= _krb5_config_copy(context
, context
->cf
, &p
->cf
);
550 /* XXX should copy */
553 cc_ops_copy(p
, context
);
554 kt_ops_copy(p
, context
);
557 if(context
->warn_dest
!= NULL
)
559 if(context
->debug_dest
!= NULL
)
563 ret
= krb5_set_extra_addresses(p
, context
->extra_addresses
);
566 ret
= krb5_set_extra_addresses(p
, context
->ignore_addresses
);
570 ret
= _krb5_copy_send_to_kdc_func(p
, context
);
579 krb5_free_context(p
);
586 * Frees the krb5_context allocated by krb5_init_context().
588 * @param context context to be freed.
593 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
594 krb5_free_context(krb5_context context
)
596 if (context
->default_cc_name
)
597 free(context
->default_cc_name
);
598 if (context
->default_cc_name_env
)
599 free(context
->default_cc_name_env
);
600 free(context
->etypes
);
601 free(context
->etypes_des
);
602 krb5_free_host_realm (context
, context
->default_realms
);
603 krb5_config_file_free (context
, context
->cf
);
604 free_error_table (context
->et_list
);
605 free(rk_UNCONST(context
->cc_ops
));
606 free(context
->kt_types
);
607 krb5_clear_error_message(context
);
608 if(context
->warn_dest
!= NULL
)
609 krb5_closelog(context
, context
->warn_dest
);
610 if(context
->debug_dest
!= NULL
)
611 krb5_closelog(context
, context
->debug_dest
);
612 krb5_set_extra_addresses(context
, NULL
);
613 krb5_set_ignore_addresses(context
, NULL
);
614 krb5_set_send_to_kdc_func(context
, NULL
, NULL
);
617 if (context
->hx509ctx
)
618 hx509_context_free(&context
->hx509ctx
);
621 HEIMDAL_MUTEX_destroy(context
->mutex
);
622 free(context
->mutex
);
623 if (context
->flags
& KRB5_CTX_F_SOCKETS_INITIALIZED
) {
627 memset(context
, 0, sizeof(*context
));
632 * Reinit the context from a new set of filenames.
634 * @param context context to add configuration too.
635 * @param filenames array of filenames, end of list is indicated with a NULL filename.
637 * @return Returns 0 to indicate success. Otherwise an kerberos et
638 * error code is returned, see krb5_get_error_message().
643 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
644 krb5_set_config_files(krb5_context context
, char **filenames
)
647 krb5_config_binding
*tmp
= NULL
;
648 while(filenames
!= NULL
&& *filenames
!= NULL
&& **filenames
!= '\0') {
649 ret
= krb5_config_parse_file_multi(context
, *filenames
, &tmp
);
650 if(ret
!= 0 && ret
!= ENOENT
&& ret
!= EACCES
&& ret
!= EPERM
) {
651 krb5_config_file_free(context
, tmp
);
657 /* with this enabled and if there are no config files, Kerberos is
658 considererd disabled */
664 _krb5_load_config_from_registry(context
, &tmp
);
667 krb5_config_file_free(context
, context
->cf
);
669 ret
= init_context_from_config_file(context
);
673 static krb5_error_code
674 add_file(char ***pfilenames
, int *len
, char *file
)
676 char **pp
= *pfilenames
;
679 for(i
= 0; i
< *len
; i
++) {
680 if(strcmp(pp
[i
], file
) == 0) {
686 pp
= realloc(*pfilenames
, (*len
+ 2) * sizeof(*pp
));
700 * `pq' isn't free, it's up the the caller
703 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
704 krb5_prepend_config_files(const char *filelist
, char **pq
, char ***ret_pp
)
719 l
= strsep_copy(&q
, PATH_SEP
, NULL
, 0);
724 krb5_free_config_files(pp
);
727 (void)strsep_copy(&p
, PATH_SEP
, fn
, l
+ 1);
728 ret
= add_file(&pp
, &len
, fn
);
730 krb5_free_config_files(pp
);
738 for (i
= 0; pq
[i
] != NULL
; i
++) {
741 krb5_free_config_files(pp
);
744 ret
= add_file(&pp
, &len
, fn
);
746 krb5_free_config_files(pp
);
757 * Prepend the filename to the global configuration list.
759 * @param filelist a filename to add to the default list of filename
760 * @param pfilenames return array of filenames, should be freed with krb5_free_config_files().
762 * @return Returns 0 to indicate success. Otherwise an kerberos et
763 * error code is returned, see krb5_get_error_message().
768 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
769 krb5_prepend_config_files_default(const char *filelist
, char ***pfilenames
)
772 char **defpp
, **pp
= NULL
;
774 ret
= krb5_get_default_config_files(&defpp
);
778 ret
= krb5_prepend_config_files(filelist
, defpp
, &pp
);
779 krb5_free_config_files(defpp
);
790 * Checks the registry for configuration file location
792 * Kerberos for Windows and other legacy Kerberos applications expect
793 * to find the configuration file location in the
794 * SOFTWARE\MIT\Kerberos registry key under the value "config".
796 KRB5_LIB_FUNCTION
char * KRB5_LIB_CALL
797 _krb5_get_default_config_config_files_from_registry()
799 static const char * KeyName
= "Software\\MIT\\Kerberos";
800 char *config_file
= NULL
;
804 rcode
= RegOpenKeyEx(HKEY_CURRENT_USER
, KeyName
, 0, KEY_READ
, &key
);
805 if (rcode
== ERROR_SUCCESS
) {
806 config_file
= _krb5_parse_reg_value_as_multi_string(NULL
, key
, "config",
807 REG_NONE
, 0, PATH_SEP
);
814 rcode
= RegOpenKeyEx(HKEY_LOCAL_MACHINE
, KeyName
, 0, KEY_READ
, &key
);
815 if (rcode
== ERROR_SUCCESS
) {
816 config_file
= _krb5_parse_reg_value_as_multi_string(NULL
, key
, "config",
817 REG_NONE
, 0, PATH_SEP
);
827 * Get the global configuration list.
829 * @param pfilenames return array of filenames, should be freed with krb5_free_config_files().
831 * @return Returns 0 to indicate success. Otherwise an kerberos et
832 * error code is returned, see krb5_get_error_message().
837 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
838 krb5_get_default_config_files(char ***pfilenames
)
840 const char *files
= NULL
;
842 if (pfilenames
== NULL
)
845 files
= getenv("KRB5_CONFIG");
850 reg_files
= _krb5_get_default_config_config_files_from_registry();
851 if (reg_files
!= NULL
) {
852 krb5_error_code code
;
854 code
= krb5_prepend_config_files(reg_files
, NULL
, pfilenames
);
863 files
= krb5_config_file
;
865 return krb5_prepend_config_files(files
, NULL
, pfilenames
);
869 * Free a list of configuration files.
871 * @param filenames list, terminated with a NULL pointer, to be
872 * freed. NULL is an valid argument.
874 * @return Returns 0 to indicate success. Otherwise an kerberos et
875 * error code is returned, see krb5_get_error_message().
880 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
881 krb5_free_config_files(char **filenames
)
884 for(p
= filenames
; p
&& *p
!= NULL
; p
++)
890 * Returns the list of Kerberos encryption types sorted in order of
891 * most preferred to least preferred encryption type. Note that some
892 * encryption types might be disabled, so you need to check with
893 * krb5_enctype_valid() before using the encryption type.
895 * @return list of enctypes, terminated with ETYPE_NULL. Its a static
896 * array completed into the Kerberos library so the content doesn't
902 KRB5_LIB_FUNCTION
const krb5_enctype
* KRB5_LIB_CALL
903 krb5_kerberos_enctypes(krb5_context context
)
905 static const krb5_enctype p
[] = {
906 ETYPE_AES256_CTS_HMAC_SHA1_96
,
907 ETYPE_AES128_CTS_HMAC_SHA1_96
,
909 ETYPE_ARCFOUR_HMAC_MD5
,
913 static const krb5_enctype weak
[] = {
914 ETYPE_AES256_CTS_HMAC_SHA1_96
,
915 ETYPE_AES128_CTS_HMAC_SHA1_96
,
918 ETYPE_ARCFOUR_HMAC_MD5
,
926 * if the list of enctypes enabled by "allow_weak_crypto"
927 * are valid, then return the former default enctype list
928 * that contained the weak entries.
930 if (krb5_enctype_valid(context
, ETYPE_DES_CBC_CRC
) == 0 &&
931 krb5_enctype_valid(context
, ETYPE_DES_CBC_MD4
) == 0 &&
932 krb5_enctype_valid(context
, ETYPE_DES_CBC_MD5
) == 0 &&
933 krb5_enctype_valid(context
, ETYPE_DES_CBC_NONE
) == 0 &&
934 krb5_enctype_valid(context
, ETYPE_DES_CFB64_NONE
) == 0 &&
935 krb5_enctype_valid(context
, ETYPE_DES_PCBC_NONE
) == 0)
945 static krb5_error_code
946 copy_enctypes(krb5_context context
,
947 const krb5_enctype
*in
,
950 krb5_enctype
*p
= NULL
;
953 for (n
= 0; in
[n
]; n
++)
958 return krb5_enomem(context
);
959 for (n
= 0, m
= 0; in
[n
]; n
++) {
960 if (krb5_enctype_valid(context
, in
[n
]) != 0)
964 p
[m
] = KRB5_ENCTYPE_NULL
;
967 krb5_set_error_message (context
, KRB5_PROG_ETYPE_NOSUPP
,
968 N_("no valid enctype set", ""));
969 return KRB5_PROG_ETYPE_NOSUPP
;
977 * set `etype' to a malloced list of the default enctypes
980 static krb5_error_code
981 default_etypes(krb5_context context
, krb5_enctype
**etype
)
983 const krb5_enctype
*p
= krb5_kerberos_enctypes(context
);
984 return copy_enctypes(context
, p
, etype
);
988 * Set the default encryption types that will be use in communcation
989 * with the KDC, clients and servers.
991 * @param context Kerberos 5 context.
992 * @param etypes Encryption types, array terminated with ETYPE_NULL (0).
994 * @return Returns 0 to indicate success. Otherwise an kerberos et
995 * error code is returned, see krb5_get_error_message().
1000 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1001 krb5_set_default_in_tkt_etypes(krb5_context context
,
1002 const krb5_enctype
*etypes
)
1004 krb5_error_code ret
;
1005 krb5_enctype
*p
= NULL
;
1008 ret
= copy_enctypes(context
, etypes
, &p
);
1013 free(context
->etypes
);
1014 context
->etypes
= p
;
1019 * Get the default encryption types that will be use in communcation
1020 * with the KDC, clients and servers.
1022 * @param context Kerberos 5 context.
1023 * @param etypes Encryption types, array terminated with
1024 * ETYPE_NULL(0), caller should free array with krb5_xfree():
1026 * @return Returns 0 to indicate success. Otherwise an kerberos et
1027 * error code is returned, see krb5_get_error_message().
1032 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1033 krb5_get_default_in_tkt_etypes(krb5_context context
,
1035 krb5_enctype
**etypes
)
1037 krb5_enctype
*enctypes
= NULL
;
1038 krb5_error_code ret
;
1041 heim_assert(pdu_type
== KRB5_PDU_AS_REQUEST
||
1042 pdu_type
== KRB5_PDU_TGS_REQUEST
||
1043 pdu_type
== KRB5_PDU_NONE
, "pdu contant not as expected");
1045 if (pdu_type
== KRB5_PDU_AS_REQUEST
&& context
->as_etypes
!= NULL
)
1046 enctypes
= context
->as_etypes
;
1047 else if (pdu_type
== KRB5_PDU_TGS_REQUEST
&& context
->tgs_etypes
!= NULL
)
1048 enctypes
= context
->tgs_etypes
;
1049 else if (context
->etypes
!= NULL
)
1050 enctypes
= context
->etypes
;
1052 if (enctypes
!= NULL
) {
1053 ret
= copy_enctypes(context
, enctypes
, &p
);
1057 ret
= default_etypes(context
, &p
);
1066 * Init the built-in ets in the Kerberos library.
1068 * @param context kerberos context to add the ets too
1073 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
1074 krb5_init_ets(krb5_context context
)
1076 if(context
->et_list
== NULL
){
1077 krb5_add_et_list(context
, initialize_krb5_error_table_r
);
1078 krb5_add_et_list(context
, initialize_asn1_error_table_r
);
1079 krb5_add_et_list(context
, initialize_heim_error_table_r
);
1081 krb5_add_et_list(context
, initialize_k524_error_table_r
);
1083 #ifdef COM_ERR_BINDDOMAIN_krb5
1084 bindtextdomain(COM_ERR_BINDDOMAIN_krb5
, HEIMDAL_LOCALEDIR
);
1085 bindtextdomain(COM_ERR_BINDDOMAIN_asn1
, HEIMDAL_LOCALEDIR
);
1086 bindtextdomain(COM_ERR_BINDDOMAIN_heim
, HEIMDAL_LOCALEDIR
);
1087 bindtextdomain(COM_ERR_BINDDOMAIN_k524
, HEIMDAL_LOCALEDIR
);
1091 krb5_add_et_list(context
, initialize_hx_error_table_r
);
1092 #ifdef COM_ERR_BINDDOMAIN_hx
1093 bindtextdomain(COM_ERR_BINDDOMAIN_hx
, HEIMDAL_LOCALEDIR
);
1100 * Make the kerberos library default to the admin KDC.
1102 * @param context Kerberos 5 context.
1103 * @param flag boolean flag to select if the use the admin KDC or not.
1108 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
1109 krb5_set_use_admin_kdc (krb5_context context
, krb5_boolean flag
)
1111 context
->use_admin_kdc
= flag
;
1115 * Make the kerberos library default to the admin KDC.
1117 * @param context Kerberos 5 context.
1119 * @return boolean flag to telling the context will use admin KDC as the default KDC.
1124 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1125 krb5_get_use_admin_kdc (krb5_context context
)
1127 return context
->use_admin_kdc
;
1131 * Add extra address to the address list that the library will add to
1132 * the client's address list when communicating with the KDC.
1134 * @param context Kerberos 5 context.
1135 * @param addresses addreses to add
1137 * @return Returns 0 to indicate success. Otherwise an kerberos et
1138 * error code is returned, see krb5_get_error_message().
1143 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1144 krb5_add_extra_addresses(krb5_context context
, krb5_addresses
*addresses
)
1147 if(context
->extra_addresses
)
1148 return krb5_append_addresses(context
,
1149 context
->extra_addresses
, addresses
);
1151 return krb5_set_extra_addresses(context
, addresses
);
1155 * Set extra address to the address list that the library will add to
1156 * the client's address list when communicating with the KDC.
1158 * @param context Kerberos 5 context.
1159 * @param addresses addreses to set
1161 * @return Returns 0 to indicate success. Otherwise an kerberos et
1162 * error code is returned, see krb5_get_error_message().
1167 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1168 krb5_set_extra_addresses(krb5_context context
, const krb5_addresses
*addresses
)
1170 if(context
->extra_addresses
)
1171 krb5_free_addresses(context
, context
->extra_addresses
);
1173 if(addresses
== NULL
) {
1174 if(context
->extra_addresses
!= NULL
) {
1175 free(context
->extra_addresses
);
1176 context
->extra_addresses
= NULL
;
1180 if(context
->extra_addresses
== NULL
) {
1181 context
->extra_addresses
= malloc(sizeof(*context
->extra_addresses
));
1182 if (context
->extra_addresses
== NULL
)
1183 return krb5_enomem(context
);
1185 return krb5_copy_addresses(context
, addresses
, context
->extra_addresses
);
1189 * Get extra address to the address list that the library will add to
1190 * the client's address list when communicating with the KDC.
1192 * @param context Kerberos 5 context.
1193 * @param addresses addreses to set
1195 * @return Returns 0 to indicate success. Otherwise an kerberos et
1196 * error code is returned, see krb5_get_error_message().
1201 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1202 krb5_get_extra_addresses(krb5_context context
, krb5_addresses
*addresses
)
1204 if(context
->extra_addresses
== NULL
) {
1205 memset(addresses
, 0, sizeof(*addresses
));
1208 return krb5_copy_addresses(context
,context
->extra_addresses
, addresses
);
1212 * Add extra addresses to ignore when fetching addresses from the
1213 * underlaying operating system.
1215 * @param context Kerberos 5 context.
1216 * @param addresses addreses to ignore
1218 * @return Returns 0 to indicate success. Otherwise an kerberos et
1219 * error code is returned, see krb5_get_error_message().
1224 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1225 krb5_add_ignore_addresses(krb5_context context
, krb5_addresses
*addresses
)
1228 if(context
->ignore_addresses
)
1229 return krb5_append_addresses(context
,
1230 context
->ignore_addresses
, addresses
);
1232 return krb5_set_ignore_addresses(context
, addresses
);
1236 * Set extra addresses to ignore when fetching addresses from the
1237 * underlaying operating system.
1239 * @param context Kerberos 5 context.
1240 * @param addresses addreses to ignore
1242 * @return Returns 0 to indicate success. Otherwise an kerberos et
1243 * error code is returned, see krb5_get_error_message().
1248 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1249 krb5_set_ignore_addresses(krb5_context context
, const krb5_addresses
*addresses
)
1251 if(context
->ignore_addresses
)
1252 krb5_free_addresses(context
, context
->ignore_addresses
);
1253 if(addresses
== NULL
) {
1254 if(context
->ignore_addresses
!= NULL
) {
1255 free(context
->ignore_addresses
);
1256 context
->ignore_addresses
= NULL
;
1260 if(context
->ignore_addresses
== NULL
) {
1261 context
->ignore_addresses
= malloc(sizeof(*context
->ignore_addresses
));
1262 if (context
->ignore_addresses
== NULL
)
1263 return krb5_enomem(context
);
1265 return krb5_copy_addresses(context
, addresses
, context
->ignore_addresses
);
1269 * Get extra addresses to ignore when fetching addresses from the
1270 * underlaying operating system.
1272 * @param context Kerberos 5 context.
1273 * @param addresses list addreses ignored
1275 * @return Returns 0 to indicate success. Otherwise an kerberos et
1276 * error code is returned, see krb5_get_error_message().
1281 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1282 krb5_get_ignore_addresses(krb5_context context
, krb5_addresses
*addresses
)
1284 if(context
->ignore_addresses
== NULL
) {
1285 memset(addresses
, 0, sizeof(*addresses
));
1288 return krb5_copy_addresses(context
, context
->ignore_addresses
, addresses
);
1292 * Set version of fcache that the library should use.
1294 * @param context Kerberos 5 context.
1295 * @param version version number.
1297 * @return Returns 0 to indicate success. Otherwise an kerberos et
1298 * error code is returned, see krb5_get_error_message().
1303 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1304 krb5_set_fcache_version(krb5_context context
, int version
)
1306 context
->fcache_vno
= version
;
1311 * Get version of fcache that the library should use.
1313 * @param context Kerberos 5 context.
1314 * @param version version number.
1316 * @return Returns 0 to indicate success. Otherwise an kerberos et
1317 * error code is returned, see krb5_get_error_message().
1322 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1323 krb5_get_fcache_version(krb5_context context
, int *version
)
1325 *version
= context
->fcache_vno
;
1330 * Runtime check if the Kerberos library was complied with thread support.
1332 * @return TRUE if the library was compiled with thread support, FALSE if not.
1338 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1339 krb5_is_thread_safe(void)
1341 #ifdef ENABLE_PTHREAD_SUPPORT
1349 * Set if the library should use DNS to canonicalize hostnames.
1351 * @param context Kerberos 5 context.
1352 * @param flag if its dns canonicalizion is used or not.
1357 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
1358 krb5_set_dns_canonicalize_hostname (krb5_context context
, krb5_boolean flag
)
1361 context
->flags
|= KRB5_CTX_F_DNS_CANONICALIZE_HOSTNAME
;
1363 context
->flags
&= ~KRB5_CTX_F_DNS_CANONICALIZE_HOSTNAME
;
1367 * Get if the library uses DNS to canonicalize hostnames.
1369 * @param context Kerberos 5 context.
1371 * @return return non zero if the library uses DNS to canonicalize hostnames.
1376 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1377 krb5_get_dns_canonicalize_hostname (krb5_context context
)
1379 return (context
->flags
& KRB5_CTX_F_DNS_CANONICALIZE_HOSTNAME
) ? 1 : 0;
1383 * Get current offset in time to the KDC.
1385 * @param context Kerberos 5 context.
1386 * @param sec seconds part of offset.
1387 * @param usec micro seconds part of offset.
1389 * @return returns zero
1394 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1395 krb5_get_kdc_sec_offset (krb5_context context
, int32_t *sec
, int32_t *usec
)
1398 *sec
= context
->kdc_sec_offset
;
1400 *usec
= context
->kdc_usec_offset
;
1405 * Set current offset in time to the KDC.
1407 * @param context Kerberos 5 context.
1408 * @param sec seconds part of offset.
1409 * @param usec micro seconds part of offset.
1411 * @return returns zero
1416 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1417 krb5_set_kdc_sec_offset (krb5_context context
, int32_t sec
, int32_t usec
)
1419 context
->kdc_sec_offset
= sec
;
1421 context
->kdc_usec_offset
= usec
;
1426 * Get max time skew allowed.
1428 * @param context Kerberos 5 context.
1430 * @return timeskew in seconds.
1435 KRB5_LIB_FUNCTION
time_t KRB5_LIB_CALL
1436 krb5_get_max_time_skew (krb5_context context
)
1438 return context
->max_skew
;
1442 * Set max time skew allowed.
1444 * @param context Kerberos 5 context.
1445 * @param t timeskew in seconds.
1450 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
1451 krb5_set_max_time_skew (krb5_context context
, time_t t
)
1453 context
->max_skew
= t
;
1457 * Init encryption types in len, val with etypes.
1459 * @param context Kerberos 5 context.
1460 * @param pdu_type type of pdu
1461 * @param len output length of val.
1462 * @param val output array of enctypes.
1463 * @param etypes etypes to set val and len to, if NULL, use default enctypes.
1465 * @return Returns 0 to indicate success. Otherwise an kerberos et
1466 * error code is returned, see krb5_get_error_message().
1471 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1472 _krb5_init_etype(krb5_context context
,
1476 const krb5_enctype
*etypes
)
1478 krb5_error_code ret
;
1481 ret
= krb5_get_default_in_tkt_etypes(context
, pdu_type
, val
);
1483 ret
= copy_enctypes(context
, etypes
, val
);
1489 while ((*val
)[*len
] != KRB5_ENCTYPE_NULL
)
1496 * Allow homedir accces
1499 static HEIMDAL_MUTEX homedir_mutex
= HEIMDAL_MUTEX_INITIALIZER
;
1500 static krb5_boolean allow_homedir
= TRUE
;
1502 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1503 _krb5_homedir_access(krb5_context context
)
1507 if (context
&& (context
->flags
& KRB5_CTX_F_HOMEDIR_ACCESS
) == 0)
1510 HEIMDAL_MUTEX_lock(&homedir_mutex
);
1511 allow
= allow_homedir
;
1512 HEIMDAL_MUTEX_unlock(&homedir_mutex
);
1517 * Enable and disable home directory access on either the global state
1518 * or the krb5_context state. By calling krb5_set_home_dir_access()
1519 * with context set to NULL, the global state is configured otherwise
1520 * the state for the krb5_context is modified.
1522 * For home directory access to be allowed, both the global state and
1523 * the krb5_context state have to be allowed.
1525 * Administrator (root user), never uses the home directory.
1527 * @param context a Kerberos 5 context or NULL
1528 * @param allow allow if TRUE home directory
1529 * @return the old value
1534 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1535 krb5_set_home_dir_access(krb5_context context
, krb5_boolean allow
)
1539 old
= (context
->flags
& KRB5_CTX_F_HOMEDIR_ACCESS
) ? TRUE
: FALSE
;
1541 context
->flags
|= KRB5_CTX_F_HOMEDIR_ACCESS
;
1543 context
->flags
&= ~KRB5_CTX_F_HOMEDIR_ACCESS
;
1545 HEIMDAL_MUTEX_lock(&homedir_mutex
);
1546 old
= allow_homedir
;
1547 allow_homedir
= allow
;
1548 HEIMDAL_MUTEX_unlock(&homedir_mutex
);