s4:torture: Adapt KDC canon test to Heimdal upstream changes
[samba.git] / third_party / heimdal / lib / krb5 / context.c
1 /*
2  * Copyright (c) 1997 - 2010 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
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.
18  *
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.
22  *
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
33  * SUCH DAMAGE.
34  */
35
36 #undef KRB5_DEPRECATED_FUNCTION
37 #define KRB5_DEPRECATED_FUNCTION(x)
38
39 #include "krb5_locl.h"
40 #include <assert.h>
41 #include <com_err.h>
42
43 static void _krb5_init_ets(krb5_context);
44
45 #define INIT_FIELD(C, T, E, D, F)                                       \
46     (C)->E = krb5_config_get_ ## T ## _default ((C), NULL, (D),         \
47                                                 "libdefaults", F, NULL)
48
49 #define INIT_FLAG(C, O, V, D, F)                                        \
50     do {                                                                \
51         if (krb5_config_get_bool_default((C), NULL, (D),"libdefaults", F, NULL)) { \
52             (C)->O |= V;                                                \
53         }                                                               \
54     } while(0)
55
56 static krb5_error_code
57 copy_enctypes(krb5_context context,
58               const krb5_enctype *in,
59               krb5_enctype **out);
60
61 /*
62  * Set the list of etypes `ret_etypes' from the configuration variable
63  * `name'
64  */
65
66 static krb5_error_code
67 set_etypes (krb5_context context,
68             const char *name,
69             krb5_enctype **ret_enctypes)
70 {
71     char **etypes_str;
72     krb5_enctype *etypes = NULL;
73
74     etypes_str = krb5_config_get_strings(context, NULL, "libdefaults",
75                                          name, NULL);
76     if(etypes_str){
77         int i, j, k;
78         for(i = 0; etypes_str[i]; i++);
79         etypes = malloc((i+1) * sizeof(*etypes));
80         if (etypes == NULL) {
81             krb5_config_free_strings (etypes_str);
82             return krb5_enomem(context);
83         }
84         for(j = 0, k = 0; j < i; j++) {
85             krb5_enctype e;
86             if(krb5_string_to_enctype(context, etypes_str[j], &e) != 0)
87                 continue;
88             if (krb5_enctype_valid(context, e) != 0)
89                 continue;
90             etypes[k++] = e;
91         }
92         etypes[k] = ETYPE_NULL;
93         krb5_config_free_strings(etypes_str);
94     }
95     *ret_enctypes = etypes;
96     return 0;
97 }
98
99 /*
100  * read variables from the configuration file and set in `context'
101  */
102
103 static krb5_error_code
104 init_context_from_config_file(krb5_context context)
105 {
106     krb5_error_code ret;
107     const char * tmp;
108     char **s;
109     krb5_enctype *tmptypes;
110
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");
115
116     INIT_FIELD(context, string, http_proxy, NULL, "http_proxy");
117
118     ret = krb5_config_get_bool_default(context, NULL, FALSE,
119                                        "libdefaults",
120                                        "allow_weak_crypto", NULL);
121     if (ret) {
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);
128     }
129
130     ret = set_etypes (context, "default_etypes", &tmptypes);
131     if(ret)
132         return ret;
133     free(context->etypes);
134     context->etypes = tmptypes;
135
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.
139      */
140     free(context->cfg_etypes);
141     context->cfg_etypes = NULL;
142     if (tmptypes) {
143         ret = copy_enctypes(context, tmptypes, &context->cfg_etypes);
144         if (ret)
145             return ret;
146     }
147
148     ret = set_etypes (context, "default_etypes_des", &tmptypes);
149     if(ret)
150         return ret;
151     free(context->etypes_des);
152     context->etypes_des = tmptypes;
153
154     ret = set_etypes (context, "default_as_etypes", &tmptypes);
155     if(ret)
156         return ret;
157     free(context->as_etypes);
158     context->as_etypes = tmptypes;
159
160     ret = set_etypes (context, "default_tgs_etypes", &tmptypes);
161     if(ret)
162         return ret;
163     free(context->tgs_etypes);
164     context->tgs_etypes = tmptypes;
165
166     ret = set_etypes (context, "permitted_enctypes", &tmptypes);
167     if(ret)
168         return ret;
169     free(context->permitted_enctypes);
170     context->permitted_enctypes = tmptypes;
171
172     INIT_FIELD(context, string, default_keytab,
173                KEYTAB_DEFAULT, "default_keytab_name");
174
175     INIT_FIELD(context, string, default_keytab_modify,
176                NULL, "default_keytab_modify_name");
177
178     INIT_FIELD(context, string, time_fmt,
179                "%Y-%m-%dT%H:%M:%S", "time_format");
180
181     INIT_FIELD(context, string, date_fmt,
182                "%Y-%m-%d", "date_format");
183
184     INIT_FIELD(context, bool, log_utc,
185                FALSE, "log_utc");
186
187     context->no_ticket_store =
188         getenv("KRB5_NO_TICKET_STORE") != NULL;
189
190     /* init dns-proxy slime */
191     tmp = krb5_config_get_string(context, NULL, "libdefaults",
192                                  "dns_proxy", NULL);
193     if(tmp)
194         roken_gethostby_setup(context->http_proxy, tmp);
195     krb5_free_host_realm (context, context->default_realms);
196     context->default_realms = NULL;
197
198     {
199         krb5_addresses addresses;
200         char **adr, **a;
201
202         krb5_set_extra_addresses(context, NULL);
203         adr = krb5_config_get_strings(context, NULL,
204                                       "libdefaults",
205                                       "extra_addresses",
206                                       NULL);
207         memset(&addresses, 0, sizeof(addresses));
208         for(a = adr; a && *a; a++) {
209             ret = krb5_parse_address(context, *a, &addresses);
210             if (ret == 0) {
211                 krb5_add_extra_addresses(context, &addresses);
212                 krb5_free_addresses(context, &addresses);
213             }
214         }
215         krb5_config_free_strings(adr);
216
217         krb5_set_ignore_addresses(context, NULL);
218         adr = krb5_config_get_strings(context, NULL,
219                                       "libdefaults",
220                                       "ignore_addresses",
221                                       NULL);
222         memset(&addresses, 0, sizeof(addresses));
223         for(a = adr; a && *a; a++) {
224             ret = krb5_parse_address(context, *a, &addresses);
225             if (ret == 0) {
226                 krb5_add_ignore_addresses(context, &addresses);
227                 krb5_free_addresses(context, &addresses);
228             }
229         }
230         krb5_config_free_strings(adr);
231     }
232
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");
244
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;
248
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;
254
255     tmp = secure_getenv("KRB5_TRACE");
256     if (tmp)
257         heim_add_debug_dest(context->hcontext, "libkrb5", tmp);
258     s = krb5_config_get_strings(context, NULL, "logging", "krb5", NULL);
259     if (s) {
260         char **p;
261
262         for (p = s; *p; p++)
263             heim_add_debug_dest(context->hcontext, "libkrb5", *p);
264         krb5_config_free_strings(s);
265     }
266
267     tmp = krb5_config_get_string(context, NULL, "libdefaults",
268                                  "check-rd-req-server", NULL);
269     if (tmp == NULL)
270         tmp = secure_getenv("KRB5_CHECK_RD_REQ_SERVER");
271     if(tmp) {
272         if (strcasecmp(tmp, "ignore") == 0)
273             context->flags |= KRB5_CTX_F_RD_REQ_IGNORE;
274     }
275     ret = krb5_config_get_bool_default(context, NULL, TRUE,
276                                        "libdefaults",
277                                        "fcache_strict_checking", NULL);
278     if (ret)
279         context->flags |= KRB5_CTX_F_FCACHE_STRICT_CHECKING;
280
281     return 0;
282 }
283
284 static krb5_error_code
285 cc_ops_register(krb5_context context)
286 {
287     context->cc_ops = NULL;
288     context->num_cc_ops = 0;
289
290 #ifndef KCM_IS_API_CACHE
291     krb5_cc_register(context, &krb5_acc_ops, TRUE);
292 #endif
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);
296 #ifdef HAVE_SCC
297     krb5_cc_register(context, &krb5_scc_ops, TRUE);
298 #endif
299 #ifdef HAVE_KCM
300 #ifdef KCM_IS_API_CACHE
301     krb5_cc_register(context, &krb5_akcm_ops, TRUE);
302 #endif
303     krb5_cc_register(context, &krb5_kcm_ops, TRUE);
304 #endif
305 #if defined(HAVE_KEYUTILS_H)
306     krb5_cc_register(context, &krb5_krcc_ops, TRUE);
307 #endif
308     _krb5_load_ccache_plugins(context);
309     return 0;
310 }
311
312 static krb5_error_code
313 cc_ops_copy(krb5_context context, const krb5_context src_context)
314 {
315     const krb5_cc_ops **cc_ops;
316
317     context->cc_ops = NULL;
318     context->num_cc_ops = 0;
319
320     if (src_context->num_cc_ops == 0)
321         return 0;
322
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;
328     }
329
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;
334
335     return 0;
336 }
337
338 static krb5_error_code
339 kt_ops_register(krb5_context context)
340 {
341     context->num_kt_types = 0;
342     context->kt_types     = NULL;
343
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);
350 #endif
351     krb5_kt_register (context, &krb5_any_ops);
352     return 0;
353 }
354
355 static krb5_error_code
356 kt_ops_copy(krb5_context context, const krb5_context src_context)
357 {
358     context->num_kt_types = 0;
359     context->kt_types     = NULL;
360
361     if (src_context->num_kt_types == 0)
362         return 0;
363
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);
367
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);
371
372     return 0;
373 }
374
375 static const char *sysplugin_dirs[] =  {
376 #ifdef _WIN32
377     "$ORIGIN",
378 #else
379     "$ORIGIN/../lib/plugin/krb5",
380 #endif
381 #ifdef __APPLE__
382     LIBDIR "/plugin/krb5",
383 #ifdef HEIM_PLUGINS_SEARCH_SYSTEM
384     "/Library/KerberosPlugins/KerberosFrameworkPlugins",
385     "/System/Library/KerberosPlugins/KerberosFrameworkPlugins",
386 #endif
387 #endif
388     NULL
389 };
390
391 static void
392 init_context_once(void *ctx)
393 {
394     krb5_context context = ctx;
395     char **dirs;
396
397 #ifdef _WIN32
398     dirs = rk_UNCONST(sysplugin_dirs);
399 #else
400     dirs = krb5_config_get_strings(context, NULL, "libdefaults",
401                                    "plugin_dir", NULL);
402     if (dirs == NULL)
403         dirs = rk_UNCONST(sysplugin_dirs);
404 #endif
405
406     _krb5_load_plugins(context, "krb5", (const char **)dirs);
407
408     if (dirs != rk_UNCONST(sysplugin_dirs))
409         krb5_config_free_strings(dirs);
410
411     bindtextdomain(HEIMDAL_TEXTDOMAIN, HEIMDAL_LOCALEDIR);
412 }
413
414 /**
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.
418  *
419  * @param context pointer to returned context
420  *
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.
426  *
427  * @ingroup krb5
428  */
429
430 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
431 krb5_init_context(krb5_context *context)
432 {
433     static heim_base_once_t init_context = HEIM_BASE_ONCE_INIT;
434     krb5_context p;
435     krb5_error_code ret;
436     char **files;
437     uint8_t rnd;
438
439     *context = NULL;
440
441     /**
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
445      * pull more bytes.
446      *
447      * Most Kerberos users calls krb5_init_context(), so this is
448      * useful point where we can do the checking.
449      */
450     ret = krb5_generate_random(&rnd, sizeof(rnd));
451     if (ret)
452         return ret;
453
454     p = calloc(1, sizeof(*p));
455     if(!p)
456         return ENOMEM;
457
458     if ((p->hcontext = heim_context_init()) == NULL) {
459         ret = ENOMEM;
460         goto out;
461     }
462
463     if (!issuid())
464         p->flags |= KRB5_CTX_F_HOMEDIR_ACCESS;
465
466     ret = krb5_get_default_config_files(&files);
467     if(ret)
468         goto out;
469     ret = krb5_set_config_files(p, files);
470     krb5_free_config_files(files);
471     if(ret)
472         goto out;
473
474     /* done enough to load plugins */
475     heim_base_once_f(&init_context, p, init_context_once);
476
477     /* init error tables */
478     _krb5_init_ets(p);
479     cc_ops_register(p);
480     kt_ops_register(p);
481
482 #ifdef PKINIT
483     ret = hx509_context_init(&p->hx509ctx);
484     if (ret)
485         goto out;
486 #endif
487     if (rk_SOCK_INIT())
488         p->flags |= KRB5_CTX_F_SOCKETS_INITIALIZED;
489
490 out:
491     if (ret) {
492         krb5_free_context(p);
493         p = NULL;
494     } else {
495         heim_context_set_log_utc(p->hcontext, p->log_utc);
496     }
497     *context = p;
498     return ret;
499 }
500
501 #ifndef HEIMDAL_SMALLER
502
503 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
504 krb5_get_permitted_enctypes(krb5_context context,
505                             krb5_enctype **etypes)
506 {
507     return krb5_get_default_in_tkt_etypes(context, KRB5_PDU_NONE, etypes);
508 }
509
510 /*
511  *
512  */
513
514 static krb5_error_code
515 copy_etypes (krb5_context context,
516              krb5_enctype *enctypes,
517              krb5_enctype **ret_enctypes)
518 {
519     unsigned int i;
520
521     for (i = 0; enctypes[i]; i++)
522         ;
523     i++;
524
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);
529     return 0;
530 }
531
532 /**
533  * Make a copy for the Kerberos 5 context, the new krb5_context shoud
534  * be freed with krb5_free_context().
535  *
536  * @param context the Kerberos context to copy
537  * @param out the copy of the Kerberos, set to NULL error.
538  *
539  * @return Returns 0 to indicate success.  Otherwise an kerberos et
540  * error code is returned, see krb5_get_error_message().
541  *
542  * @ingroup krb5
543  */
544
545 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
546 krb5_copy_context(krb5_context context, krb5_context *out)
547 {
548     krb5_error_code ret;
549     krb5_context p;
550
551     *out = NULL;
552
553     p = calloc(1, sizeof(*p));
554     if (p == NULL)
555         return krb5_enomem(context);
556
557     if ((p->hcontext = heim_context_init()) == NULL) {
558         ret = ENOMEM;
559         goto out;
560     }
561
562     heim_context_set_log_utc(p->hcontext, context->log_utc);
563
564     if (context->default_cc_name &&
565         (p->default_cc_name = strdup(context->default_cc_name)) == NULL) {
566         ret = ENOMEM;
567         goto out;
568     }
569     if (context->default_cc_name_env &&
570         (p->default_cc_name_env =
571             strdup(context->default_cc_name_env)) == NULL) {
572         ret = ENOMEM;
573         goto out;
574     }
575     if (context->configured_default_cc_name &&
576         (p->configured_default_cc_name =
577             strdup(context->configured_default_cc_name)) == NULL) {
578         ret = ENOMEM;
579         goto out;
580     }
581
582     if (context->etypes) {
583         ret = copy_etypes(context, context->etypes, &p->etypes);
584         if (ret)
585             goto out;
586     }
587     if (context->cfg_etypes) {
588         ret = copy_etypes(context, context->cfg_etypes, &p->cfg_etypes);
589         if (ret)
590             goto out;
591     }
592     if (context->etypes_des) {
593         ret = copy_etypes(context, context->etypes_des, &p->etypes_des);
594         if (ret)
595             goto out;
596     }
597
598     if (context->default_realms) {
599         ret = krb5_copy_host_realm(context,
600                                    context->default_realms, &p->default_realms);
601         if (ret)
602             goto out;
603     }
604
605     ret = _krb5_config_copy(context, context->cf, &p->cf);
606     if (ret)
607         goto out;
608
609     /* XXX should copy */
610     _krb5_init_ets(p);
611
612     cc_ops_copy(p, context);
613     kt_ops_copy(p, context);
614
615     ret = krb5_set_extra_addresses(p, context->extra_addresses);
616     if (ret)
617         goto out;
618     ret = krb5_set_extra_addresses(p, context->ignore_addresses);
619     if (ret)
620         goto out;
621
622     ret = _krb5_copy_send_to_kdc_func(p, context);
623     if (ret)
624         goto out;
625
626     *out = p;
627
628     return 0;
629
630  out:
631     krb5_free_context(p);
632     return ret;
633 }
634
635 #endif
636
637 /**
638  * Frees the krb5_context allocated by krb5_init_context().
639  *
640  * @param context context to be freed.
641  *
642  * @ingroup krb5
643  */
644
645 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
646 krb5_free_context(krb5_context context)
647 {
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);
669
670 #ifdef PKINIT
671     hx509_context_free(&context->hx509ctx);
672 #endif
673
674     if (context->flags & KRB5_CTX_F_SOCKETS_INITIALIZED) {
675         rk_SOCK_EXIT();
676     }
677
678     heim_context_free(&context->hcontext);
679     memset(context, 0, sizeof(*context));
680     free(context);
681 }
682
683 /**
684  * Reinit the context from a new set of filenames.
685  *
686  * @param context context to add configuration too.
687  * @param filenames array of filenames, end of list is indicated with a NULL filename.
688  *
689  * @return Returns 0 to indicate success.  Otherwise an kerberos et
690  * error code is returned, see krb5_get_error_message().
691  *
692  * @ingroup krb5
693  */
694
695 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
696 krb5_set_config_files(krb5_context context, char **filenames)
697 {
698     krb5_error_code ret;
699     heim_config_binding *tmp = NULL;
700
701     if ((ret = heim_set_config_files(context->hcontext, filenames,
702                                      &tmp)))
703         return ret;
704     krb5_config_file_free(context, context->cf);
705     context->cf = (krb5_config_binding *)tmp;
706     return init_context_from_config_file(context);
707 }
708
709 #ifndef HEIMDAL_SMALLER
710 /**
711  * Reinit the context from configuration file contents in a C string.
712  * This should only be used in tests.
713  *
714  * @param context context to add configuration too.
715  * @param config configuration.
716  *
717  * @return Returns 0 to indicate success.  Otherwise an kerberos et
718  * error code is returned, see krb5_get_error_message().
719  *
720  * @ingroup krb5
721  */
722
723 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
724 krb5_set_config(krb5_context context, const char *config)
725 {
726     krb5_error_code ret;
727     krb5_config_binding *tmp = NULL;
728
729     if ((ret = krb5_config_parse_string_multi(context, config, &tmp)))
730         return ret;
731 #if 0
732     /* with this enabled and if there are no config files, Kerberos is
733        considererd disabled */
734     if (tmp == NULL)
735         return ENXIO;
736 #endif
737
738     krb5_config_file_free(context, context->cf);
739     context->cf = tmp;
740     ret = init_context_from_config_file(context);
741     return ret;
742 }
743 #endif
744
745 /*
746  *  `pq' isn't free, it's up the the caller
747  */
748
749 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
750 krb5_prepend_config_files(const char *filelist, char **pq, char ***ret_pp)
751 {
752     return heim_prepend_config_files(filelist, pq, ret_pp);
753 }
754
755 /**
756  * Prepend the filename to the global configuration list.
757  *
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().
760  *
761  * @return Returns 0 to indicate success.  Otherwise an kerberos et
762  * error code is returned, see krb5_get_error_message().
763  *
764  * @ingroup krb5
765  */
766
767 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
768 krb5_prepend_config_files_default(const char *filelist, char ***pfilenames)
769 {
770     return heim_prepend_config_files_default(filelist, krb5_config_file,
771                                              "KRB5_CONFIG", pfilenames);
772 }
773
774 /**
775  * Get the global configuration list.
776  *
777  * @param pfilenames return array of filenames, should be freed with krb5_free_config_files().
778  *
779  * @return Returns 0 to indicate success.  Otherwise an kerberos et
780  * error code is returned, see krb5_get_error_message().
781  *
782  * @ingroup krb5
783  */
784
785 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
786 krb5_get_default_config_files(char ***pfilenames)
787 {
788     if (pfilenames == NULL)
789         return EINVAL;
790     return heim_get_default_config_files(krb5_config_file, "KRB5_CONFIG",
791                                          pfilenames);
792 }
793
794 /**
795  * Free a list of configuration files.
796  *
797  * @param filenames list, terminated with a NULL pointer, to be
798  * freed. NULL is an valid argument.
799  *
800  * @return Returns 0 to indicate success. Otherwise an kerberos et
801  * error code is returned, see krb5_get_error_message().
802  *
803  * @ingroup krb5
804  */
805
806 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
807 krb5_free_config_files(char **filenames)
808 {
809     heim_free_config_files(filenames);
810 }
811
812 /**
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.
817  *
818  * @return list of enctypes, terminated with ETYPE_NULL. Its a static
819  * array completed into the Kerberos library so the content doesn't
820  * need to be freed.
821  *
822  * @ingroup krb5
823  */
824
825 KRB5_LIB_FUNCTION const krb5_enctype * KRB5_LIB_CALL
826 krb5_kerberos_enctypes(krb5_context context)
827 {
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,
833         ETYPE_DES3_CBC_SHA1,
834         ETYPE_ARCFOUR_HMAC_MD5,
835         ETYPE_NULL
836     };
837
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,
843         ETYPE_DES3_CBC_SHA1,
844         ETYPE_DES3_CBC_MD5,
845         ETYPE_ARCFOUR_HMAC_MD5,
846         ETYPE_DES_CBC_MD5,
847         ETYPE_DES_CBC_MD4,
848         ETYPE_DES_CBC_CRC,
849         ETYPE_NULL
850     };
851
852     /*
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.
856      */
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)
863         return weak;
864
865     return p;
866 }
867
868 /*
869  *
870  */
871
872 static krb5_error_code
873 copy_enctypes(krb5_context context,
874               const krb5_enctype *in,
875               krb5_enctype **out)
876 {
877     krb5_enctype *p = NULL;
878     size_t m, n;
879
880     for (n = 0; in[n]; n++)
881         ;
882     n++;
883     ALLOC(p, n);
884     if(p == NULL)
885         return krb5_enomem(context);
886     for (n = 0, m = 0; in[n]; n++) {
887         if (krb5_enctype_valid(context, in[n]) != 0)
888             continue;
889         p[m++] = in[n];
890     }
891     p[m] = KRB5_ENCTYPE_NULL;
892     if (m == 0) {
893         free(p);
894         krb5_set_error_message (context, KRB5_PROG_ETYPE_NOSUPP,
895                                 N_("no valid enctype set", ""));
896         return KRB5_PROG_ETYPE_NOSUPP;
897     }
898     *out = p;
899     return 0;
900 }
901
902
903 /*
904  * set `etype' to a malloced list of the default enctypes
905  */
906
907 static krb5_error_code
908 default_etypes(krb5_context context, krb5_enctype **etype)
909 {
910     const krb5_enctype *p = krb5_kerberos_enctypes(context);
911     return copy_enctypes(context, p, etype);
912 }
913
914 /**
915  * Set the default encryption types that will be use in communcation
916  * with the KDC, clients and servers.
917  *
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.
922  *
923  * @return Returns 0 to indicate success. Otherwise an kerberos et
924  * error code is returned, see krb5_get_error_message().
925  *
926  * @ingroup krb5
927  */
928
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)
932 {
933     krb5_error_code ret;
934     krb5_enctype *p = NULL;
935
936     if(!etypes) {
937         etypes = context->cfg_etypes;
938     }
939
940     if(etypes) {
941         ret = copy_enctypes(context, etypes, &p);
942         if (ret)
943             return ret;
944     }
945     if(context->etypes)
946         free(context->etypes);
947     context->etypes = p;
948     return 0;
949 }
950
951 /**
952  * Get the default encryption types that will be use in communcation
953  * with the KDC, clients and servers.
954  *
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():
959  *
960  * @return Returns 0 to indicate success. Otherwise an kerberos et
961  * error code is returned, see krb5_get_error_message().
962  *
963  * @ingroup krb5
964  */
965
966 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
967 krb5_get_default_in_tkt_etypes(krb5_context context,
968                                krb5_pdu pdu_type,
969                                krb5_enctype **etypes)
970 {
971     krb5_enctype *enctypes = NULL;
972     krb5_error_code ret;
973     krb5_enctype *p;
974
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");
978
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;
985
986     if (enctypes != NULL) {
987         ret = copy_enctypes(context, enctypes, &p);
988         if (ret)
989             return ret;
990     } else {
991         ret = default_etypes(context, &p);
992         if (ret)
993             return ret;
994     }
995     *etypes = p;
996     return 0;
997 }
998
999 /**
1000  * Init the built-in ets in the Kerberos library.
1001  *
1002  * @param context kerberos context to add the ets too
1003  *
1004  * @ingroup krb5
1005  */
1006
1007 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
1008 krb5_init_ets(krb5_context context)
1009 {
1010 }
1011
1012 static void
1013 _krb5_init_ets(krb5_context context)
1014 {
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);
1018
1019     heim_add_et_list(context->hcontext, initialize_k524_error_table_r);
1020     heim_add_et_list(context->hcontext, initialize_k5e1_error_table_r);
1021
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);
1027 #endif
1028
1029 #ifdef PKINIT
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);
1033 #endif
1034 #endif
1035 }
1036
1037 /**
1038  * Make the kerberos library default to the admin KDC.
1039  *
1040  * @param context Kerberos 5 context.
1041  * @param flag boolean flag to select if the use the admin KDC or not.
1042  *
1043  * @ingroup krb5
1044  */
1045
1046 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
1047 krb5_set_use_admin_kdc (krb5_context context, krb5_boolean flag)
1048 {
1049     context->use_admin_kdc = flag;
1050 }
1051
1052 /**
1053  * Make the kerberos library default to the admin KDC.
1054  *
1055  * @param context Kerberos 5 context.
1056  *
1057  * @return boolean flag to telling the context will use admin KDC as the default KDC.
1058  *
1059  * @ingroup krb5
1060  */
1061
1062 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1063 krb5_get_use_admin_kdc (krb5_context context)
1064 {
1065     return context->use_admin_kdc;
1066 }
1067
1068 /**
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.
1071  *
1072  * @param context Kerberos 5 context.
1073  * @param addresses addreses to add
1074  *
1075  * @return Returns 0 to indicate success. Otherwise an kerberos et
1076  * error code is returned, see krb5_get_error_message().
1077  *
1078  * @ingroup krb5
1079  */
1080
1081 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1082 krb5_add_extra_addresses(krb5_context context, krb5_addresses *addresses)
1083 {
1084
1085     if(context->extra_addresses)
1086         return krb5_append_addresses(context,
1087                                      context->extra_addresses, addresses);
1088     else
1089         return krb5_set_extra_addresses(context, addresses);
1090 }
1091
1092 /**
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.
1095  *
1096  * @param context Kerberos 5 context.
1097  * @param addresses addreses to set
1098  *
1099  * @return Returns 0 to indicate success. Otherwise an kerberos et
1100  * error code is returned, see krb5_get_error_message().
1101  *
1102  * @ingroup krb5
1103  */
1104
1105 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1106 krb5_set_extra_addresses(krb5_context context, const krb5_addresses *addresses)
1107 {
1108     if(context->extra_addresses)
1109         krb5_free_addresses(context, context->extra_addresses);
1110
1111     if(addresses == NULL) {
1112         if(context->extra_addresses != NULL) {
1113             free(context->extra_addresses);
1114             context->extra_addresses = NULL;
1115         }
1116         return 0;
1117     }
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);
1122     }
1123     return krb5_copy_addresses(context, addresses, context->extra_addresses);
1124 }
1125
1126 /**
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.
1129  *
1130  * @param context Kerberos 5 context.
1131  * @param addresses addreses to set
1132  *
1133  * @return Returns 0 to indicate success. Otherwise an kerberos et
1134  * error code is returned, see krb5_get_error_message().
1135  *
1136  * @ingroup krb5
1137  */
1138
1139 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1140 krb5_get_extra_addresses(krb5_context context, krb5_addresses *addresses)
1141 {
1142     if(context->extra_addresses == NULL) {
1143         memset(addresses, 0, sizeof(*addresses));
1144         return 0;
1145     }
1146     return krb5_copy_addresses(context,context->extra_addresses, addresses);
1147 }
1148
1149 /**
1150  * Add extra addresses to ignore when fetching addresses from the
1151  * underlaying operating system.
1152  *
1153  * @param context Kerberos 5 context.
1154  * @param addresses addreses to ignore
1155  *
1156  * @return Returns 0 to indicate success. Otherwise an kerberos et
1157  * error code is returned, see krb5_get_error_message().
1158  *
1159  * @ingroup krb5
1160  */
1161
1162 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1163 krb5_add_ignore_addresses(krb5_context context, krb5_addresses *addresses)
1164 {
1165
1166     if(context->ignore_addresses)
1167         return krb5_append_addresses(context,
1168                                      context->ignore_addresses, addresses);
1169     else
1170         return krb5_set_ignore_addresses(context, addresses);
1171 }
1172
1173 /**
1174  * Set extra addresses to ignore when fetching addresses from the
1175  * underlaying operating system.
1176  *
1177  * @param context Kerberos 5 context.
1178  * @param addresses addreses to ignore
1179  *
1180  * @return Returns 0 to indicate success. Otherwise an kerberos et
1181  * error code is returned, see krb5_get_error_message().
1182  *
1183  * @ingroup krb5
1184  */
1185
1186 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1187 krb5_set_ignore_addresses(krb5_context context, const krb5_addresses *addresses)
1188 {
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;
1195         }
1196         return 0;
1197     }
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);
1202     }
1203     return krb5_copy_addresses(context, addresses, context->ignore_addresses);
1204 }
1205
1206 /**
1207  * Get extra addresses to ignore when fetching addresses from the
1208  * underlaying operating system.
1209  *
1210  * @param context Kerberos 5 context.
1211  * @param addresses list addreses ignored
1212  *
1213  * @return Returns 0 to indicate success. Otherwise an kerberos et
1214  * error code is returned, see krb5_get_error_message().
1215  *
1216  * @ingroup krb5
1217  */
1218
1219 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1220 krb5_get_ignore_addresses(krb5_context context, krb5_addresses *addresses)
1221 {
1222     if(context->ignore_addresses == NULL) {
1223         memset(addresses, 0, sizeof(*addresses));
1224         return 0;
1225     }
1226     return krb5_copy_addresses(context, context->ignore_addresses, addresses);
1227 }
1228
1229 /**
1230  * Set version of fcache that the library should use.
1231  *
1232  * @param context Kerberos 5 context.
1233  * @param version version number.
1234  *
1235  * @return Returns 0 to indicate success. Otherwise an kerberos et
1236  * error code is returned, see krb5_get_error_message().
1237  *
1238  * @ingroup krb5
1239  */
1240
1241 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1242 krb5_set_fcache_version(krb5_context context, int version)
1243 {
1244     context->fcache_vno = version;
1245     return 0;
1246 }
1247
1248 /**
1249  * Get version of fcache that the library should use.
1250  *
1251  * @param context Kerberos 5 context.
1252  * @param version version number.
1253  *
1254  * @return Returns 0 to indicate success. Otherwise an kerberos et
1255  * error code is returned, see krb5_get_error_message().
1256  *
1257  * @ingroup krb5
1258  */
1259
1260 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1261 krb5_get_fcache_version(krb5_context context, int *version)
1262 {
1263     *version = context->fcache_vno;
1264     return 0;
1265 }
1266
1267 /**
1268  * Runtime check if the Kerberos library was complied with thread support.
1269  *
1270  * @return TRUE if the library was compiled with thread support, FALSE if not.
1271  *
1272  * @ingroup krb5
1273  */
1274
1275
1276 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1277 krb5_is_thread_safe(void)
1278 {
1279 #ifdef ENABLE_PTHREAD_SUPPORT
1280     return TRUE;
1281 #else
1282     return FALSE;
1283 #endif
1284 }
1285
1286 /**
1287  * Set if the library should use DNS to canonicalize hostnames.
1288  *
1289  * @param context Kerberos 5 context.
1290  * @param flag if its dns canonicalizion is used or not.
1291  *
1292  * @ingroup krb5
1293  */
1294
1295 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
1296 krb5_set_dns_canonicalize_hostname (krb5_context context, krb5_boolean flag)
1297 {
1298     if (flag)
1299         context->flags |= KRB5_CTX_F_DNS_CANONICALIZE_HOSTNAME;
1300     else
1301         context->flags &= ~KRB5_CTX_F_DNS_CANONICALIZE_HOSTNAME;
1302 }
1303
1304 /**
1305  * Get if the library uses DNS to canonicalize hostnames.
1306  *
1307  * @param context Kerberos 5 context.
1308  *
1309  * @return return non zero if the library uses DNS to canonicalize hostnames.
1310  *
1311  * @ingroup krb5
1312  */
1313
1314 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1315 krb5_get_dns_canonicalize_hostname (krb5_context context)
1316 {
1317     return (context->flags & KRB5_CTX_F_DNS_CANONICALIZE_HOSTNAME) ? 1 : 0;
1318 }
1319
1320 /**
1321  * Get current offset in time to the KDC.
1322  *
1323  * @param context Kerberos 5 context.
1324  * @param sec seconds part of offset.
1325  * @param usec micro seconds part of offset.
1326  *
1327  * @return returns zero
1328  *
1329  * @ingroup krb5
1330  */
1331
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)
1334 {
1335     if (sec)
1336         *sec = context->kdc_sec_offset;
1337     if (usec)
1338         *usec = context->kdc_usec_offset;
1339     return 0;
1340 }
1341
1342 /**
1343  * Set current offset in time to the KDC.
1344  *
1345  * @param context Kerberos 5 context.
1346  * @param sec seconds part of offset.
1347  * @param usec micro seconds part of offset.
1348  *
1349  * @return returns zero
1350  *
1351  * @ingroup krb5
1352  */
1353
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)
1356 {
1357     context->kdc_sec_offset = sec;
1358     if (usec >= 0)
1359         context->kdc_usec_offset = usec;
1360     return 0;
1361 }
1362
1363 /**
1364  * Get max time skew allowed.
1365  *
1366  * @param context Kerberos 5 context.
1367  *
1368  * @return timeskew in seconds.
1369  *
1370  * @ingroup krb5
1371  */
1372
1373 KRB5_LIB_FUNCTION time_t KRB5_LIB_CALL
1374 krb5_get_max_time_skew (krb5_context context)
1375 {
1376     return context->max_skew;
1377 }
1378
1379 /**
1380  * Set max time skew allowed.
1381  *
1382  * @param context Kerberos 5 context.
1383  * @param t timeskew in seconds.
1384  *
1385  * @ingroup krb5
1386  */
1387
1388 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
1389 krb5_set_max_time_skew (krb5_context context, time_t t)
1390 {
1391     context->max_skew = t;
1392 }
1393
1394 /*
1395  * Init encryption types in len, val with etypes.
1396  *
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.
1402
1403  * @return Returns 0 to indicate success. Otherwise an kerberos et
1404  * error code is returned, see krb5_get_error_message().
1405  *
1406  * @ingroup krb5
1407  */
1408
1409 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1410 _krb5_init_etype(krb5_context context,
1411                  krb5_pdu pdu_type,
1412                  unsigned *len,
1413                  krb5_enctype **val,
1414                  const krb5_enctype *etypes)
1415 {
1416     krb5_error_code ret;
1417
1418     if (etypes == NULL)
1419         ret = krb5_get_default_in_tkt_etypes(context, pdu_type, val);
1420     else
1421         ret = copy_enctypes(context, etypes, val);
1422     if (ret)
1423         return ret;
1424
1425     if (len) {
1426         *len = 0;
1427         while ((*val)[*len] != KRB5_ENCTYPE_NULL)
1428             (*len)++;
1429     }
1430     return 0;
1431 }
1432
1433 /*
1434  * Allow homedir access
1435  */
1436
1437 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1438 _krb5_homedir_access(krb5_context context)
1439 {
1440     if (context)
1441         return !!(context->flags & KRB5_CTX_F_HOMEDIR_ACCESS);
1442     return !issuid();
1443 }
1444
1445 /**
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.
1450  *
1451  * For home directory access to be allowed, both the global state and
1452  * the krb5_context state have to be allowed.
1453  *
1454  * @param context a Kerberos 5 context or NULL
1455  * @param allow allow if TRUE home directory
1456  * @return the old value
1457  *
1458  * @ingroup krb5
1459  */
1460
1461 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1462 krb5_set_home_dir_access(krb5_context context, krb5_boolean allow)
1463 {
1464     krb5_boolean old = _krb5_homedir_access(context);
1465
1466     if (context) {
1467         if (allow)
1468             context->flags |= KRB5_CTX_F_HOMEDIR_ACCESS;
1469         else
1470             context->flags &= ~KRB5_CTX_F_HOMEDIR_ACCESS;
1471         heim_context_set_homedir_access(context->hcontext, allow ? 1 : 0);
1472     }
1473
1474     return old;
1475 }
1476