xref: /freebsd/crypto/heimdal/lib/krb5/context.c (revision 39beb93c)
1 /*
2  * Copyright (c) 1997 - 2005 Kungliga Tekniska H�gskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include "krb5_locl.h"
35 #include <com_err.h>
36 
37 RCSID("$Id: context.c 22293 2007-12-14 05:25:59Z lha $");
38 
39 #define INIT_FIELD(C, T, E, D, F)					\
40     (C)->E = krb5_config_get_ ## T ## _default ((C), NULL, (D), 	\
41 						"libdefaults", F, NULL)
42 
43 #define INIT_FLAG(C, O, V, D, F)					\
44     do {								\
45 	if (krb5_config_get_bool_default((C), NULL, (D),"libdefaults", F, NULL)) { \
46 	    (C)->O |= V;						\
47         }								\
48     } while(0)
49 
50 /*
51  * Set the list of etypes `ret_etypes' from the configuration variable
52  * `name'
53  */
54 
55 static krb5_error_code
56 set_etypes (krb5_context context,
57 	    const char *name,
58 	    krb5_enctype **ret_enctypes)
59 {
60     char **etypes_str;
61     krb5_enctype *etypes = NULL;
62 
63     etypes_str = krb5_config_get_strings(context, NULL, "libdefaults",
64 					 name, NULL);
65     if(etypes_str){
66 	int i, j, k;
67 	for(i = 0; etypes_str[i]; i++);
68 	etypes = malloc((i+1) * sizeof(*etypes));
69 	if (etypes == NULL) {
70 	    krb5_config_free_strings (etypes_str);
71 	    krb5_set_error_string (context, "malloc: out of memory");
72 	    return ENOMEM;
73 	}
74 	for(j = 0, k = 0; j < i; j++) {
75 	    krb5_enctype e;
76 	    if(krb5_string_to_enctype(context, etypes_str[j], &e) != 0)
77 		continue;
78 	    if (krb5_enctype_valid(context, e) != 0)
79 		continue;
80 	    etypes[k++] = e;
81 	}
82 	etypes[k] = ETYPE_NULL;
83 	krb5_config_free_strings(etypes_str);
84     }
85     *ret_enctypes = etypes;
86     return 0;
87 }
88 
89 /*
90  * read variables from the configuration file and set in `context'
91  */
92 
93 static krb5_error_code
94 init_context_from_config_file(krb5_context context)
95 {
96     krb5_error_code ret;
97     const char * tmp;
98     krb5_enctype *tmptypes;
99 
100     INIT_FIELD(context, time, max_skew, 5 * 60, "clockskew");
101     INIT_FIELD(context, time, kdc_timeout, 3, "kdc_timeout");
102     INIT_FIELD(context, int, max_retries, 3, "max_retries");
103 
104     INIT_FIELD(context, string, http_proxy, NULL, "http_proxy");
105 
106     ret = set_etypes (context, "default_etypes", &tmptypes);
107     if(ret)
108 	return ret;
109     free(context->etypes);
110     context->etypes = tmptypes;
111 
112     ret = set_etypes (context, "default_etypes_des", &tmptypes);
113     if(ret)
114 	return ret;
115     free(context->etypes_des);
116     context->etypes_des = tmptypes;
117 
118     /* default keytab name */
119     tmp = NULL;
120     if(!issuid())
121 	tmp = getenv("KRB5_KTNAME");
122     if(tmp != NULL)
123 	context->default_keytab = tmp;
124     else
125 	INIT_FIELD(context, string, default_keytab,
126 		   KEYTAB_DEFAULT, "default_keytab_name");
127 
128     INIT_FIELD(context, string, default_keytab_modify,
129 	       NULL, "default_keytab_modify_name");
130 
131     INIT_FIELD(context, string, time_fmt,
132 	       "%Y-%m-%dT%H:%M:%S", "time_format");
133 
134     INIT_FIELD(context, string, date_fmt,
135 	       "%Y-%m-%d", "date_format");
136 
137     INIT_FIELD(context, bool, log_utc,
138 	       FALSE, "log_utc");
139 
140 
141 
142     /* init dns-proxy slime */
143     tmp = krb5_config_get_string(context, NULL, "libdefaults",
144 				 "dns_proxy", NULL);
145     if(tmp)
146 	roken_gethostby_setup(context->http_proxy, tmp);
147     krb5_free_host_realm (context, context->default_realms);
148     context->default_realms = NULL;
149 
150     {
151 	krb5_addresses addresses;
152 	char **adr, **a;
153 
154 	krb5_set_extra_addresses(context, NULL);
155 	adr = krb5_config_get_strings(context, NULL,
156 				      "libdefaults",
157 				      "extra_addresses",
158 				      NULL);
159 	memset(&addresses, 0, sizeof(addresses));
160 	for(a = adr; a && *a; a++) {
161 	    ret = krb5_parse_address(context, *a, &addresses);
162 	    if (ret == 0) {
163 		krb5_add_extra_addresses(context, &addresses);
164 		krb5_free_addresses(context, &addresses);
165 	    }
166 	}
167 	krb5_config_free_strings(adr);
168 
169 	krb5_set_ignore_addresses(context, NULL);
170 	adr = krb5_config_get_strings(context, NULL,
171 				      "libdefaults",
172 				      "ignore_addresses",
173 				      NULL);
174 	memset(&addresses, 0, sizeof(addresses));
175 	for(a = adr; a && *a; a++) {
176 	    ret = krb5_parse_address(context, *a, &addresses);
177 	    if (ret == 0) {
178 		krb5_add_ignore_addresses(context, &addresses);
179 		krb5_free_addresses(context, &addresses);
180 	    }
181 	}
182 	krb5_config_free_strings(adr);
183     }
184 
185     INIT_FIELD(context, bool, scan_interfaces, TRUE, "scan_interfaces");
186     INIT_FIELD(context, int, fcache_vno, 0, "fcache_version");
187     /* prefer dns_lookup_kdc over srv_lookup. */
188     INIT_FIELD(context, bool, srv_lookup, TRUE, "srv_lookup");
189     INIT_FIELD(context, bool, srv_lookup, context->srv_lookup, "dns_lookup_kdc");
190     INIT_FIELD(context, int, large_msg_size, 1400, "large_message_size");
191     INIT_FLAG(context, flags, KRB5_CTX_F_DNS_CANONICALIZE_HOSTNAME, TRUE, "dns_canonicalize_hostname");
192     INIT_FLAG(context, flags, KRB5_CTX_F_CHECK_PAC, TRUE, "check_pac");
193     context->default_cc_name = NULL;
194     context->default_cc_name_set = 0;
195     return 0;
196 }
197 
198 /**
199  * Initializes the context structure and reads the configuration file
200  * /etc/krb5.conf. The structure should be freed by calling
201  * krb5_free_context() when it is no longer being used.
202  *
203  * @param context pointer to returned context
204  *
205  * @return Returns 0 to indicate success.  Otherwise an errno code is
206  * returned.  Failure means either that something bad happened during
207  * initialization (typically ENOMEM) or that Kerberos should not be
208  * used ENXIO.
209  *
210  * @ingroup krb5
211  */
212 
213 krb5_error_code KRB5_LIB_FUNCTION
214 krb5_init_context(krb5_context *context)
215 {
216     krb5_context p;
217     krb5_error_code ret;
218     char **files;
219 
220     *context = NULL;
221 
222     p = calloc(1, sizeof(*p));
223     if(!p)
224 	return ENOMEM;
225 
226     p->mutex = malloc(sizeof(HEIMDAL_MUTEX));
227     if (p->mutex == NULL) {
228 	free(p);
229 	return ENOMEM;
230     }
231     HEIMDAL_MUTEX_init(p->mutex);
232 
233     ret = krb5_get_default_config_files(&files);
234     if(ret)
235 	goto out;
236     ret = krb5_set_config_files(p, files);
237     krb5_free_config_files(files);
238     if(ret)
239 	goto out;
240 
241     /* init error tables */
242     krb5_init_ets(p);
243 
244     p->cc_ops = NULL;
245     p->num_cc_ops = 0;
246     krb5_cc_register(p, &krb5_acc_ops, TRUE);
247     krb5_cc_register(p, &krb5_fcc_ops, TRUE);
248     krb5_cc_register(p, &krb5_mcc_ops, TRUE);
249 #ifdef HAVE_KCM
250     krb5_cc_register(p, &krb5_kcm_ops, TRUE);
251 #endif
252 
253     p->num_kt_types = 0;
254     p->kt_types     = NULL;
255     krb5_kt_register (p, &krb5_fkt_ops);
256     krb5_kt_register (p, &krb5_wrfkt_ops);
257     krb5_kt_register (p, &krb5_javakt_ops);
258     krb5_kt_register (p, &krb5_mkt_ops);
259     krb5_kt_register (p, &krb5_akf_ops);
260     krb5_kt_register (p, &krb4_fkt_ops);
261     krb5_kt_register (p, &krb5_srvtab_fkt_ops);
262     krb5_kt_register (p, &krb5_any_ops);
263 
264 out:
265     if(ret) {
266 	krb5_free_context(p);
267 	p = NULL;
268     }
269     *context = p;
270     return ret;
271 }
272 
273 /**
274  * Frees the krb5_context allocated by krb5_init_context().
275  *
276  * @param context context to be freed.
277  *
278  *  @ingroup krb5
279 */
280 
281 void KRB5_LIB_FUNCTION
282 krb5_free_context(krb5_context context)
283 {
284     if (context->default_cc_name)
285 	free(context->default_cc_name);
286     if (context->default_cc_name_env)
287 	free(context->default_cc_name_env);
288     free(context->etypes);
289     free(context->etypes_des);
290     krb5_free_host_realm (context, context->default_realms);
291     krb5_config_file_free (context, context->cf);
292     free_error_table (context->et_list);
293     free(context->cc_ops);
294     free(context->kt_types);
295     krb5_clear_error_string(context);
296     if(context->warn_dest != NULL)
297 	krb5_closelog(context, context->warn_dest);
298     krb5_set_extra_addresses(context, NULL);
299     krb5_set_ignore_addresses(context, NULL);
300     krb5_set_send_to_kdc_func(context, NULL, NULL);
301     if (context->mutex != NULL) {
302 	HEIMDAL_MUTEX_destroy(context->mutex);
303 	free(context->mutex);
304     }
305     memset(context, 0, sizeof(*context));
306     free(context);
307 }
308 
309 /**
310  * Reinit the context from a new set of filenames.
311  *
312  * @param context context to add configuration too.
313  * @param filenames array of filenames, end of list is indicated with a NULL filename.
314  *
315  * @return Returns 0 to indicate success.  Otherwise an kerberos et
316  * error code is returned, see krb5_get_error_message().
317  *
318  * @ingroup krb5
319  */
320 
321 krb5_error_code KRB5_LIB_FUNCTION
322 krb5_set_config_files(krb5_context context, char **filenames)
323 {
324     krb5_error_code ret;
325     krb5_config_binding *tmp = NULL;
326     while(filenames != NULL && *filenames != NULL && **filenames != '\0') {
327 	ret = krb5_config_parse_file_multi(context, *filenames, &tmp);
328 	if(ret != 0 && ret != ENOENT && ret != EACCES) {
329 	    krb5_config_file_free(context, tmp);
330 	    return ret;
331 	}
332 	filenames++;
333     }
334 #if 0
335     /* with this enabled and if there are no config files, Kerberos is
336        considererd disabled */
337     if(tmp == NULL)
338 	return ENXIO;
339 #endif
340     krb5_config_file_free(context, context->cf);
341     context->cf = tmp;
342     ret = init_context_from_config_file(context);
343     return ret;
344 }
345 
346 static krb5_error_code
347 add_file(char ***pfilenames, int *len, char *file)
348 {
349     char **pp = *pfilenames;
350     int i;
351 
352     for(i = 0; i < *len; i++) {
353 	if(strcmp(pp[i], file) == 0) {
354 	    free(file);
355 	    return 0;
356 	}
357     }
358 
359     pp = realloc(*pfilenames, (*len + 2) * sizeof(*pp));
360     if (pp == NULL) {
361 	free(file);
362 	return ENOMEM;
363     }
364 
365     pp[*len] = file;
366     pp[*len + 1] = NULL;
367     *pfilenames = pp;
368     *len += 1;
369     return 0;
370 }
371 
372 /*
373  *  `pq' isn't free, it's up the the caller
374  */
375 
376 krb5_error_code KRB5_LIB_FUNCTION
377 krb5_prepend_config_files(const char *filelist, char **pq, char ***ret_pp)
378 {
379     krb5_error_code ret;
380     const char *p, *q;
381     char **pp;
382     int len;
383     char *fn;
384 
385     pp = NULL;
386 
387     len = 0;
388     p = filelist;
389     while(1) {
390 	ssize_t l;
391 	q = p;
392 	l = strsep_copy(&q, ":", NULL, 0);
393 	if(l == -1)
394 	    break;
395 	fn = malloc(l + 1);
396 	if(fn == NULL) {
397 	    krb5_free_config_files(pp);
398 	    return ENOMEM;
399 	}
400 	l = strsep_copy(&p, ":", fn, l + 1);
401 	ret = add_file(&pp, &len, fn);
402 	if (ret) {
403 	    krb5_free_config_files(pp);
404 	    return ret;
405 	}
406     }
407 
408     if (pq != NULL) {
409 	int i;
410 
411 	for (i = 0; pq[i] != NULL; i++) {
412 	    fn = strdup(pq[i]);
413 	    if (fn == NULL) {
414 		krb5_free_config_files(pp);
415 		return ENOMEM;
416 	    }
417 	    ret = add_file(&pp, &len, fn);
418 	    if (ret) {
419 		krb5_free_config_files(pp);
420 		return ret;
421 	    }
422 	}
423     }
424 
425     *ret_pp = pp;
426     return 0;
427 }
428 
429 /**
430  * Prepend the filename to the global configuration list.
431  *
432  * @param filelist a filename to add to the default list of filename
433  * @param pfilenames return array of filenames, should be freed with krb5_free_config_files().
434  *
435  * @return Returns 0 to indicate success.  Otherwise an kerberos et
436  * error code is returned, see krb5_get_error_message().
437  *
438  * @ingroup krb5
439  */
440 
441 krb5_error_code KRB5_LIB_FUNCTION
442 krb5_prepend_config_files_default(const char *filelist, char ***pfilenames)
443 {
444     krb5_error_code ret;
445     char **defpp, **pp = NULL;
446 
447     ret = krb5_get_default_config_files(&defpp);
448     if (ret)
449 	return ret;
450 
451     ret = krb5_prepend_config_files(filelist, defpp, &pp);
452     krb5_free_config_files(defpp);
453     if (ret) {
454 	return ret;
455     }
456     *pfilenames = pp;
457     return 0;
458 }
459 
460 /**
461  * Get the global configuration list.
462  *
463  * @param pfilenames return array of filenames, should be freed with krb5_free_config_files().
464  *
465  * @return Returns 0 to indicate success.  Otherwise an kerberos et
466  * error code is returned, see krb5_get_error_message().
467  *
468  * @ingroup krb5
469  */
470 
471 krb5_error_code KRB5_LIB_FUNCTION
472 krb5_get_default_config_files(char ***pfilenames)
473 {
474     const char *files = NULL;
475 
476     if (pfilenames == NULL)
477         return EINVAL;
478     if(!issuid())
479 	files = getenv("KRB5_CONFIG");
480     if (files == NULL)
481 	files = krb5_config_file;
482 
483     return krb5_prepend_config_files(files, NULL, pfilenames);
484 }
485 
486 /**
487  * Free a list of configuration files.
488  *
489  * @param filenames list to be freed.
490  *
491  * @return Returns 0 to indicate success. Otherwise an kerberos et
492  * error code is returned, see krb5_get_error_message().
493  *
494  * @ingroup krb5
495  */
496 
497 void KRB5_LIB_FUNCTION
498 krb5_free_config_files(char **filenames)
499 {
500     char **p;
501     for(p = filenames; *p != NULL; p++)
502 	free(*p);
503     free(filenames);
504 }
505 
506 /**
507  * Returns the list of Kerberos encryption types sorted in order of
508  * most preferred to least preferred encryption type.  Note that some
509  * encryption types might be disabled, so you need to check with
510  * krb5_enctype_valid() before using the encryption type.
511  *
512  * @return list of enctypes, terminated with ETYPE_NULL. Its a static
513  * array completed into the Kerberos library so the content doesn't
514  * need to be freed.
515  *
516  * @ingroup krb5
517  */
518 
519 const krb5_enctype * KRB5_LIB_FUNCTION
520 krb5_kerberos_enctypes(krb5_context context)
521 {
522     static const krb5_enctype p[] = {
523 	ETYPE_AES256_CTS_HMAC_SHA1_96,
524 	ETYPE_AES128_CTS_HMAC_SHA1_96,
525 	ETYPE_DES3_CBC_SHA1,
526 	ETYPE_DES3_CBC_MD5,
527 	ETYPE_ARCFOUR_HMAC_MD5,
528 	ETYPE_DES_CBC_MD5,
529 	ETYPE_DES_CBC_MD4,
530 	ETYPE_DES_CBC_CRC,
531 	ETYPE_NULL
532     };
533     return p;
534 }
535 
536 /*
537  * set `etype' to a malloced list of the default enctypes
538  */
539 
540 static krb5_error_code
541 default_etypes(krb5_context context, krb5_enctype **etype)
542 {
543     const krb5_enctype *p;
544     krb5_enctype *e = NULL, *ep;
545     int i, n = 0;
546 
547     p = krb5_kerberos_enctypes(context);
548 
549     for (i = 0; p[i] != ETYPE_NULL; i++) {
550 	if (krb5_enctype_valid(context, p[i]) != 0)
551 	    continue;
552 	ep = realloc(e, (n + 2) * sizeof(*e));
553 	if (ep == NULL) {
554 	    free(e);
555 	    krb5_set_error_string (context, "malloc: out of memory");
556 	    return ENOMEM;
557 	}
558 	e = ep;
559 	e[n] = p[i];
560 	e[n + 1] = ETYPE_NULL;
561 	n++;
562     }
563     *etype = e;
564     return 0;
565 }
566 
567 /**
568  * Set the default encryption types that will be use in communcation
569  * with the KDC, clients and servers.
570  *
571  * @param context Kerberos 5 context.
572  * @param etypes Encryption types, array terminated with ETYPE_NULL (0).
573  *
574  * @return Returns 0 to indicate success. Otherwise an kerberos et
575  * error code is returned, see krb5_get_error_message().
576  *
577  * @ingroup krb5
578  */
579 
580 krb5_error_code KRB5_LIB_FUNCTION
581 krb5_set_default_in_tkt_etypes(krb5_context context,
582 			       const krb5_enctype *etypes)
583 {
584     krb5_enctype *p = NULL;
585     int i;
586 
587     if(etypes) {
588 	for (i = 0; etypes[i]; ++i) {
589 	    krb5_error_code ret;
590 	    ret = krb5_enctype_valid(context, etypes[i]);
591 	    if (ret)
592 		return ret;
593 	}
594 	++i;
595 	ALLOC(p, i);
596 	if(!p) {
597 	    krb5_set_error_string (context, "malloc: out of memory");
598 	    return ENOMEM;
599 	}
600 	memmove(p, etypes, i * sizeof(krb5_enctype));
601     }
602     if(context->etypes)
603 	free(context->etypes);
604     context->etypes = p;
605     return 0;
606 }
607 
608 /**
609  * Get the default encryption types that will be use in communcation
610  * with the KDC, clients and servers.
611  *
612  * @param context Kerberos 5 context.
613  * @param etypes Encryption types, array terminated with
614  * ETYPE_NULL(0), caller should free array with krb5_xfree():
615  *
616  * @return Returns 0 to indicate success. Otherwise an kerberos et
617  * error code is returned, see krb5_get_error_message().
618  *
619  * @ingroup krb5
620  */
621 
622 krb5_error_code KRB5_LIB_FUNCTION
623 krb5_get_default_in_tkt_etypes(krb5_context context,
624 			       krb5_enctype **etypes)
625 {
626   krb5_enctype *p;
627   int i;
628   krb5_error_code ret;
629 
630   if(context->etypes) {
631     for(i = 0; context->etypes[i]; i++);
632     ++i;
633     ALLOC(p, i);
634     if(!p) {
635       krb5_set_error_string (context, "malloc: out of memory");
636       return ENOMEM;
637     }
638     memmove(p, context->etypes, i * sizeof(krb5_enctype));
639   } else {
640     ret = default_etypes(context, &p);
641     if (ret)
642       return ret;
643   }
644   *etypes = p;
645   return 0;
646 }
647 
648 /**
649  * Return the error string for the error code. The caller must not
650  * free the string.
651  *
652  * @param context Kerberos 5 context.
653  * @param code Kerberos error code.
654  *
655  * @return the error message matching code
656  *
657  * @ingroup krb5
658  */
659 
660 const char* KRB5_LIB_FUNCTION
661 krb5_get_err_text(krb5_context context, krb5_error_code code)
662 {
663     const char *p = NULL;
664     if(context != NULL)
665 	p = com_right(context->et_list, code);
666     if(p == NULL)
667 	p = strerror(code);
668     if (p == NULL)
669 	p = "Unknown error";
670     return p;
671 }
672 
673 /**
674  * Init the built-in ets in the Kerberos library.
675  *
676  * @param context kerberos context to add the ets too
677  *
678  * @ingroup krb5
679  */
680 
681 void KRB5_LIB_FUNCTION
682 krb5_init_ets(krb5_context context)
683 {
684     if(context->et_list == NULL){
685 	krb5_add_et_list(context, initialize_krb5_error_table_r);
686 	krb5_add_et_list(context, initialize_asn1_error_table_r);
687 	krb5_add_et_list(context, initialize_heim_error_table_r);
688 	krb5_add_et_list(context, initialize_k524_error_table_r);
689 #ifdef PKINIT
690 	krb5_add_et_list(context, initialize_hx_error_table_r);
691 #endif
692     }
693 }
694 
695 /**
696  * Make the kerberos library default to the admin KDC.
697  *
698  * @param context Kerberos 5 context.
699  * @param flag boolean flag to select if the use the admin KDC or not.
700  *
701  * @ingroup krb5
702  */
703 
704 void KRB5_LIB_FUNCTION
705 krb5_set_use_admin_kdc (krb5_context context, krb5_boolean flag)
706 {
707     context->use_admin_kdc = flag;
708 }
709 
710 /**
711  * Make the kerberos library default to the admin KDC.
712  *
713  * @param context Kerberos 5 context.
714  *
715  * @return boolean flag to telling the context will use admin KDC as the default KDC.
716  *
717  * @ingroup krb5
718  */
719 
720 krb5_boolean KRB5_LIB_FUNCTION
721 krb5_get_use_admin_kdc (krb5_context context)
722 {
723     return context->use_admin_kdc;
724 }
725 
726 /**
727  * Add extra address to the address list that the library will add to
728  * the client's address list when communicating with the KDC.
729  *
730  * @param context Kerberos 5 context.
731  * @param addresses addreses to add
732  *
733  * @return Returns 0 to indicate success. Otherwise an kerberos et
734  * error code is returned, see krb5_get_error_message().
735  *
736  * @ingroup krb5
737  */
738 
739 krb5_error_code KRB5_LIB_FUNCTION
740 krb5_add_extra_addresses(krb5_context context, krb5_addresses *addresses)
741 {
742 
743     if(context->extra_addresses)
744 	return krb5_append_addresses(context,
745 				     context->extra_addresses, addresses);
746     else
747 	return krb5_set_extra_addresses(context, addresses);
748 }
749 
750 /**
751  * Set extra address to the address list that the library will add to
752  * the client's address list when communicating with the KDC.
753  *
754  * @param context Kerberos 5 context.
755  * @param addresses addreses to set
756  *
757  * @return Returns 0 to indicate success. Otherwise an kerberos et
758  * error code is returned, see krb5_get_error_message().
759  *
760  * @ingroup krb5
761  */
762 
763 krb5_error_code KRB5_LIB_FUNCTION
764 krb5_set_extra_addresses(krb5_context context, const krb5_addresses *addresses)
765 {
766     if(context->extra_addresses)
767 	krb5_free_addresses(context, context->extra_addresses);
768 
769     if(addresses == NULL) {
770 	if(context->extra_addresses != NULL) {
771 	    free(context->extra_addresses);
772 	    context->extra_addresses = NULL;
773 	}
774 	return 0;
775     }
776     if(context->extra_addresses == NULL) {
777 	context->extra_addresses = malloc(sizeof(*context->extra_addresses));
778 	if(context->extra_addresses == NULL) {
779 	    krb5_set_error_string (context, "malloc: out of memory");
780 	    return ENOMEM;
781 	}
782     }
783     return krb5_copy_addresses(context, addresses, context->extra_addresses);
784 }
785 
786 /**
787  * Get extra address to the address list that the library will add to
788  * the client's address list when communicating with the KDC.
789  *
790  * @param context Kerberos 5 context.
791  * @param addresses addreses to set
792  *
793  * @return Returns 0 to indicate success. Otherwise an kerberos et
794  * error code is returned, see krb5_get_error_message().
795  *
796  * @ingroup krb5
797  */
798 
799 krb5_error_code KRB5_LIB_FUNCTION
800 krb5_get_extra_addresses(krb5_context context, krb5_addresses *addresses)
801 {
802     if(context->extra_addresses == NULL) {
803 	memset(addresses, 0, sizeof(*addresses));
804 	return 0;
805     }
806     return krb5_copy_addresses(context,context->extra_addresses, addresses);
807 }
808 
809 /**
810  * Add extra addresses to ignore when fetching addresses from the
811  * underlaying operating system.
812  *
813  * @param context Kerberos 5 context.
814  * @param addresses addreses to ignore
815  *
816  * @return Returns 0 to indicate success. Otherwise an kerberos et
817  * error code is returned, see krb5_get_error_message().
818  *
819  * @ingroup krb5
820  */
821 
822 krb5_error_code KRB5_LIB_FUNCTION
823 krb5_add_ignore_addresses(krb5_context context, krb5_addresses *addresses)
824 {
825 
826     if(context->ignore_addresses)
827 	return krb5_append_addresses(context,
828 				     context->ignore_addresses, addresses);
829     else
830 	return krb5_set_ignore_addresses(context, addresses);
831 }
832 
833 /**
834  * Set extra addresses to ignore when fetching addresses from the
835  * underlaying operating system.
836  *
837  * @param context Kerberos 5 context.
838  * @param addresses addreses to ignore
839  *
840  * @return Returns 0 to indicate success. Otherwise an kerberos et
841  * error code is returned, see krb5_get_error_message().
842  *
843  * @ingroup krb5
844  */
845 
846 krb5_error_code KRB5_LIB_FUNCTION
847 krb5_set_ignore_addresses(krb5_context context, const krb5_addresses *addresses)
848 {
849     if(context->ignore_addresses)
850 	krb5_free_addresses(context, context->ignore_addresses);
851     if(addresses == NULL) {
852 	if(context->ignore_addresses != NULL) {
853 	    free(context->ignore_addresses);
854 	    context->ignore_addresses = NULL;
855 	}
856 	return 0;
857     }
858     if(context->ignore_addresses == NULL) {
859 	context->ignore_addresses = malloc(sizeof(*context->ignore_addresses));
860 	if(context->ignore_addresses == NULL) {
861 	    krb5_set_error_string (context, "malloc: out of memory");
862 	    return ENOMEM;
863 	}
864     }
865     return krb5_copy_addresses(context, addresses, context->ignore_addresses);
866 }
867 
868 /**
869  * Get extra addresses to ignore when fetching addresses from the
870  * underlaying operating system.
871  *
872  * @param context Kerberos 5 context.
873  * @param addresses list addreses ignored
874  *
875  * @return Returns 0 to indicate success. Otherwise an kerberos et
876  * error code is returned, see krb5_get_error_message().
877  *
878  * @ingroup krb5
879  */
880 
881 krb5_error_code KRB5_LIB_FUNCTION
882 krb5_get_ignore_addresses(krb5_context context, krb5_addresses *addresses)
883 {
884     if(context->ignore_addresses == NULL) {
885 	memset(addresses, 0, sizeof(*addresses));
886 	return 0;
887     }
888     return krb5_copy_addresses(context, context->ignore_addresses, addresses);
889 }
890 
891 /**
892  * Set version of fcache that the library should use.
893  *
894  * @param context Kerberos 5 context.
895  * @param version version number.
896  *
897  * @return Returns 0 to indicate success. Otherwise an kerberos et
898  * error code is returned, see krb5_get_error_message().
899  *
900  * @ingroup krb5
901  */
902 
903 krb5_error_code KRB5_LIB_FUNCTION
904 krb5_set_fcache_version(krb5_context context, int version)
905 {
906     context->fcache_vno = version;
907     return 0;
908 }
909 
910 /**
911  * Get version of fcache that the library should use.
912  *
913  * @param context Kerberos 5 context.
914  * @param version version number.
915  *
916  * @return Returns 0 to indicate success. Otherwise an kerberos et
917  * error code is returned, see krb5_get_error_message().
918  *
919  * @ingroup krb5
920  */
921 
922 krb5_error_code KRB5_LIB_FUNCTION
923 krb5_get_fcache_version(krb5_context context, int *version)
924 {
925     *version = context->fcache_vno;
926     return 0;
927 }
928 
929 /**
930  * Runtime check if the Kerberos library was complied with thread support.
931  *
932  * @return TRUE if the library was compiled with thread support, FALSE if not.
933  *
934  * @ingroup krb5
935  */
936 
937 
938 krb5_boolean KRB5_LIB_FUNCTION
939 krb5_is_thread_safe(void)
940 {
941 #ifdef ENABLE_PTHREAD_SUPPORT
942     return TRUE;
943 #else
944     return FALSE;
945 #endif
946 }
947 
948 /**
949  * Set if the library should use DNS to canonicalize hostnames.
950  *
951  * @param context Kerberos 5 context.
952  * @param flag if its dns canonicalizion is used or not.
953  *
954  * @ingroup krb5
955  */
956 
957 void KRB5_LIB_FUNCTION
958 krb5_set_dns_canonicalize_hostname (krb5_context context, krb5_boolean flag)
959 {
960     if (flag)
961 	context->flags |= KRB5_CTX_F_DNS_CANONICALIZE_HOSTNAME;
962     else
963 	context->flags &= ~KRB5_CTX_F_DNS_CANONICALIZE_HOSTNAME;
964 }
965 
966 /**
967  * Get if the library uses DNS to canonicalize hostnames.
968  *
969  * @param context Kerberos 5 context.
970  *
971  * @return return non zero if the library uses DNS to canonicalize hostnames.
972  *
973  * @ingroup krb5
974  */
975 
976 krb5_boolean KRB5_LIB_FUNCTION
977 krb5_get_dns_canonicalize_hostname (krb5_context context)
978 {
979     return (context->flags & KRB5_CTX_F_DNS_CANONICALIZE_HOSTNAME) ? 1 : 0;
980 }
981 
982 /**
983  * Get current offset in time to the KDC.
984  *
985  * @param context Kerberos 5 context.
986  * @param sec seconds part of offset.
987  * @param usec micro seconds part of offset.
988  *
989  * @return return non zero if the library uses DNS to canonicalize hostnames.
990  *
991  * @ingroup krb5
992  */
993 
994 krb5_error_code KRB5_LIB_FUNCTION
995 krb5_get_kdc_sec_offset (krb5_context context, int32_t *sec, int32_t *usec)
996 {
997     if (sec)
998 	*sec = context->kdc_sec_offset;
999     if (usec)
1000 	*usec = context->kdc_usec_offset;
1001     return 0;
1002 }
1003 
1004 /**
1005  * Get max time skew allowed.
1006  *
1007  * @param context Kerberos 5 context.
1008  *
1009  * @return timeskew in seconds.
1010  *
1011  * @ingroup krb5
1012  */
1013 
1014 time_t KRB5_LIB_FUNCTION
1015 krb5_get_max_time_skew (krb5_context context)
1016 {
1017     return context->max_skew;
1018 }
1019 
1020 /**
1021  * Set max time skew allowed.
1022  *
1023  * @param context Kerberos 5 context.
1024  * @param t timeskew in seconds.
1025  *
1026  * @ingroup krb5
1027  */
1028 
1029 void KRB5_LIB_FUNCTION
1030 krb5_set_max_time_skew (krb5_context context, time_t t)
1031 {
1032     context->max_skew = t;
1033 }
1034