Remove spurious slash when expanding path tokens
[heimdal.git] / lib / krb5 / config_file.c
blobf6f1570630b424dcfc17b5d544a5f918ec74c7fe
1 /*
2 * Copyright (c) 1997 - 2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
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
10 * are met:
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
33 * SUCH DAMAGE.
36 #define KRB5_DEPRECATED
38 #include "krb5_locl.h"
40 #ifdef __APPLE__
41 #include <CoreFoundation/CoreFoundation.h>
42 #endif
44 /* Gaah! I want a portable funopen */
45 struct fileptr {
46 const char *s;
47 FILE *f;
50 static char *
51 config_fgets(char *str, size_t len, struct fileptr *ptr)
53 /* XXX this is not correct, in that they don't do the same if the
54 line is longer than len */
55 if(ptr->f != NULL)
56 return fgets(str, len, ptr->f);
57 else {
58 /* this is almost strsep_copy */
59 const char *p;
60 ssize_t l;
61 if(*ptr->s == '\0')
62 return NULL;
63 p = ptr->s + strcspn(ptr->s, "\n");
64 if(*p == '\n')
65 p++;
66 l = min(len, p - ptr->s);
67 if(len > 0) {
68 memcpy(str, ptr->s, l);
69 str[l] = '\0';
71 ptr->s = p;
72 return str;
76 static krb5_error_code parse_section(char *p, krb5_config_section **s,
77 krb5_config_section **res,
78 const char **error_message);
79 static krb5_error_code parse_binding(struct fileptr *f, unsigned *lineno, char *p,
80 krb5_config_binding **b,
81 krb5_config_binding **parent,
82 const char **error_message);
83 static krb5_error_code parse_list(struct fileptr *f, unsigned *lineno,
84 krb5_config_binding **parent,
85 const char **error_message);
87 static krb5_config_section *
88 get_entry(krb5_config_section **parent, const char *name, int type)
90 krb5_config_section **q;
92 for(q = parent; *q != NULL; q = &(*q)->next)
93 if(type == krb5_config_list &&
94 type == (*q)->type &&
95 strcmp(name, (*q)->name) == 0)
96 return *q;
97 *q = calloc(1, sizeof(**q));
98 if(*q == NULL)
99 return NULL;
100 (*q)->name = strdup(name);
101 (*q)->type = type;
102 if((*q)->name == NULL) {
103 free(*q);
104 *q = NULL;
105 return NULL;
107 return *q;
111 * Parse a section:
113 * [section]
114 * foo = bar
115 * b = {
118 * ...
120 * starting at the line in `p', storing the resulting structure in
121 * `s' and hooking it into `parent'.
122 * Store the error message in `error_message'.
125 static krb5_error_code
126 parse_section(char *p, krb5_config_section **s, krb5_config_section **parent,
127 const char **error_message)
129 char *p1;
130 krb5_config_section *tmp;
132 p1 = strchr (p + 1, ']');
133 if (p1 == NULL) {
134 *error_message = "missing ]";
135 return KRB5_CONFIG_BADFORMAT;
137 *p1 = '\0';
138 tmp = get_entry(parent, p + 1, krb5_config_list);
139 if(tmp == NULL) {
140 *error_message = "out of memory";
141 return KRB5_CONFIG_BADFORMAT;
143 *s = tmp;
144 return 0;
148 * Parse a brace-enclosed list from `f', hooking in the structure at
149 * `parent'.
150 * Store the error message in `error_message'.
153 static krb5_error_code
154 parse_list(struct fileptr *f, unsigned *lineno, krb5_config_binding **parent,
155 const char **error_message)
157 char buf[BUFSIZ];
158 krb5_error_code ret;
159 krb5_config_binding *b = NULL;
160 unsigned beg_lineno = *lineno;
162 while(config_fgets(buf, sizeof(buf), f) != NULL) {
163 char *p;
165 ++*lineno;
166 buf[strcspn(buf, "\r\n")] = '\0';
167 p = buf;
168 while(isspace((unsigned char)*p))
169 ++p;
170 if (*p == '#' || *p == ';' || *p == '\0')
171 continue;
172 while(isspace((unsigned char)*p))
173 ++p;
174 if (*p == '}')
175 return 0;
176 if (*p == '\0')
177 continue;
178 ret = parse_binding (f, lineno, p, &b, parent, error_message);
179 if (ret)
180 return ret;
182 *lineno = beg_lineno;
183 *error_message = "unclosed {";
184 return KRB5_CONFIG_BADFORMAT;
191 static krb5_error_code
192 parse_binding(struct fileptr *f, unsigned *lineno, char *p,
193 krb5_config_binding **b, krb5_config_binding **parent,
194 const char **error_message)
196 krb5_config_binding *tmp;
197 char *p1, *p2;
198 krb5_error_code ret = 0;
200 p1 = p;
201 while (*p && *p != '=' && !isspace((unsigned char)*p))
202 ++p;
203 if (*p == '\0') {
204 *error_message = "missing =";
205 return KRB5_CONFIG_BADFORMAT;
207 p2 = p;
208 while (isspace((unsigned char)*p))
209 ++p;
210 if (*p != '=') {
211 *error_message = "missing =";
212 return KRB5_CONFIG_BADFORMAT;
214 ++p;
215 while(isspace((unsigned char)*p))
216 ++p;
217 *p2 = '\0';
218 if (*p == '{') {
219 tmp = get_entry(parent, p1, krb5_config_list);
220 if (tmp == NULL) {
221 *error_message = "out of memory";
222 return KRB5_CONFIG_BADFORMAT;
224 ret = parse_list (f, lineno, &tmp->u.list, error_message);
225 } else {
226 tmp = get_entry(parent, p1, krb5_config_string);
227 if (tmp == NULL) {
228 *error_message = "out of memory";
229 return KRB5_CONFIG_BADFORMAT;
231 p1 = p;
232 p = p1 + strlen(p1);
233 while(p > p1 && isspace((unsigned char)*(p-1)))
234 --p;
235 *p = '\0';
236 tmp->u.string = strdup(p1);
238 *b = tmp;
239 return ret;
242 #ifdef __APPLE__
243 static char *
244 cfstring2cstring(CFStringRef string)
246 CFIndex len;
247 char *str;
249 str = (char *) CFStringGetCStringPtr(string, kCFStringEncodingUTF8);
250 if (str)
251 return strdup(str);
253 len = CFStringGetLength(string);
254 len = 1 + CFStringGetMaximumSizeForEncoding(len, kCFStringEncodingUTF8);
255 str = malloc(len);
256 if (str == NULL)
257 return NULL;
259 if (!CFStringGetCString (string, str, len, kCFStringEncodingUTF8)) {
260 free (str);
261 return NULL;
263 return str;
266 static void
267 convert_content(const void *key, const void *value, void *context)
269 krb5_config_section *tmp, **parent = context;
270 char *k;
272 if (CFGetTypeID(key) != CFStringGetTypeID())
273 return;
275 k = cfstring2cstring(key);
276 if (k == NULL)
277 return;
279 if (CFGetTypeID(value) == CFStringGetTypeID()) {
280 tmp = get_entry(parent, k, krb5_config_string);
281 tmp->u.string = cfstring2cstring(value);
282 } else if (CFGetTypeID(value) == CFDictionaryGetTypeID()) {
283 tmp = get_entry(parent, k, krb5_config_list);
284 CFDictionaryApplyFunction(value, convert_content, &tmp->u.list);
285 } else {
286 /* log */
288 free(k);
291 static krb5_error_code
292 parse_plist_config(krb5_context context, const char *path, krb5_config_section **parent)
294 CFReadStreamRef s;
295 CFDictionaryRef d;
296 CFErrorRef e;
297 CFURLRef url;
299 url = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (UInt8 *)path, strlen(path), FALSE);
300 if (url == NULL) {
301 krb5_clear_error_message(context);
302 return ENOMEM;
305 s = CFReadStreamCreateWithFile(kCFAllocatorDefault, url);
306 CFRelease(url);
307 if (s == NULL) {
308 krb5_clear_error_message(context);
309 return ENOMEM;
312 if (!CFReadStreamOpen(s)) {
313 CFRelease(s);
314 krb5_clear_error_message(context);
315 return ENOENT;
318 d = (CFDictionaryRef)CFPropertyListCreateWithStream (kCFAllocatorDefault, s, 0, kCFPropertyListImmutable, NULL, &e);
319 CFRelease(s);
320 if (d == NULL) {
321 krb5_clear_error_message(context);
322 return ENOENT;
325 CFDictionaryApplyFunction(d, convert_content, parent);
326 CFRelease(d);
328 return 0;
331 #endif
335 * Parse the config file `fname', generating the structures into `res'
336 * returning error messages in `error_message'
339 static krb5_error_code
340 krb5_config_parse_debug (struct fileptr *f,
341 krb5_config_section **res,
342 unsigned *lineno,
343 const char **error_message)
345 krb5_config_section *s = NULL;
346 krb5_config_binding *b = NULL;
347 char buf[BUFSIZ];
348 krb5_error_code ret;
350 while (config_fgets(buf, sizeof(buf), f) != NULL) {
351 char *p;
353 ++*lineno;
354 buf[strcspn(buf, "\r\n")] = '\0';
355 p = buf;
356 while(isspace((unsigned char)*p))
357 ++p;
358 if (*p == '#' || *p == ';')
359 continue;
360 if (*p == '[') {
361 ret = parse_section(p, &s, res, error_message);
362 if (ret)
363 return ret;
364 b = NULL;
365 } else if (*p == '}') {
366 *error_message = "unmatched }";
367 return EINVAL; /* XXX */
368 } else if(*p != '\0') {
369 if (s == NULL) {
370 *error_message = "binding before section";
371 return EINVAL;
373 ret = parse_binding(f, lineno, p, &b, &s->u.list, error_message);
374 if (ret)
375 return ret;
378 return 0;
381 static int
382 is_plist_file(const char *fname)
384 size_t len = strlen(fname);
385 char suffix[] = ".plist";
386 if (len < sizeof(suffix))
387 return 0;
388 if (strcasecmp(&fname[len - (sizeof(suffix) - 1)], suffix) != 0)
389 return 0;
390 return 1;
394 * Parse a configuration file and add the result into res. This
395 * interface can be used to parse several configuration files into one
396 * resulting krb5_config_section by calling it repeatably.
398 * @param context a Kerberos 5 context.
399 * @param fname a file name to a Kerberos configuration file
400 * @param res the returned result, must be free with krb5_free_config_files().
401 * @return Return an error code or 0, see krb5_get_error_message().
403 * @ingroup krb5_support
406 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
407 krb5_config_parse_file_multi (krb5_context context,
408 const char *fname,
409 krb5_config_section **res)
411 const char *str;
412 char *newfname = NULL;
413 unsigned lineno = 0;
414 krb5_error_code ret;
415 struct fileptr f;
418 * If the fname starts with "~/" parse configuration file in the
419 * current users home directory. The behavior can be disabled and
420 * enabled by calling krb5_set_home_dir_access().
422 if (fname[0] == '~' && fname[1] == '/') {
423 #ifndef KRB5_USE_PATH_TOKENS
424 const char *home = NULL;
426 if (!_krb5_homedir_access(context)) {
427 krb5_set_error_message(context, EPERM,
428 "Access to home directory not allowed");
429 return EPERM;
432 if(!issuid())
433 home = getenv("HOME");
435 if (home == NULL) {
436 struct passwd *pw = getpwuid(getuid());
437 if(pw != NULL)
438 home = pw->pw_dir;
440 if (home) {
441 asprintf(&newfname, "%s%s", home, &fname[1]);
442 if (newfname == NULL) {
443 krb5_set_error_message(context, ENOMEM,
444 N_("malloc: out of memory", ""));
445 return ENOMEM;
447 fname = newfname;
449 #else /* KRB5_USE_PATH_TOKENS */
450 asprintf(&newfname, "%%{USERCONFIG}%s", &fname[1]);
451 if (newfname == NULL) {
452 krb5_set_error_message(context, ENOMEM,
453 N_("malloc: out of memory", ""));
454 return ENOMEM;
456 fname = newfname;
457 #endif
460 if (is_plist_file(fname)) {
461 #ifdef __APPLE__
462 ret = parse_plist_config(context, fname, res);
463 if (ret) {
464 krb5_set_error_message(context, ret,
465 "Failed to parse plist %s", fname);
466 if (newfname)
467 free(newfname);
468 return ret;
470 #else
471 krb5_set_error_message(context, ENOENT,
472 "no support for plist configuration files");
473 return ENOENT;
474 #endif
475 } else {
476 #ifdef KRB5_USE_PATH_TOKENS
477 char * exp_fname = NULL;
479 ret = _krb5_expand_path_tokens(context, fname, &exp_fname);
480 if (ret) {
481 if (newfname)
482 free(newfname);
483 return ret;
486 if (newfname)
487 free(newfname);
488 fname = newfname = exp_fname;
489 #endif
491 f.f = fopen(fname, "r");
492 f.s = NULL;
493 if(f.f == NULL) {
494 ret = errno;
495 krb5_set_error_message (context, ret, "open %s: %s",
496 fname, strerror(ret));
497 if (newfname)
498 free(newfname);
499 return ret;
502 ret = krb5_config_parse_debug (&f, res, &lineno, &str);
503 fclose(f.f);
504 if (ret) {
505 krb5_set_error_message (context, ret, "%s:%u: %s",
506 fname, lineno, str);
507 if (newfname)
508 free(newfname);
509 return ret;
512 return 0;
515 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
516 krb5_config_parse_file (krb5_context context,
517 const char *fname,
518 krb5_config_section **res)
520 *res = NULL;
521 return krb5_config_parse_file_multi(context, fname, res);
524 static void
525 free_binding (krb5_context context, krb5_config_binding *b)
527 krb5_config_binding *next_b;
529 while (b) {
530 free (b->name);
531 if (b->type == krb5_config_string)
532 free (b->u.string);
533 else if (b->type == krb5_config_list)
534 free_binding (context, b->u.list);
535 else
536 krb5_abortx(context, "unknown binding type (%d) in free_binding",
537 b->type);
538 next_b = b->next;
539 free (b);
540 b = next_b;
545 * Free configuration file section, the result of
546 * krb5_config_parse_file() and krb5_config_parse_file_multi().
548 * @param context A Kerberos 5 context
549 * @param s the configuration section to free
551 * @return returns 0 on successes, otherwise an error code, see
552 * krb5_get_error_message()
554 * @ingroup krb5_support
557 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
558 krb5_config_file_free (krb5_context context, krb5_config_section *s)
560 free_binding (context, s);
561 return 0;
564 #ifndef HEIMDAL_SMALLER
566 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
567 _krb5_config_copy(krb5_context context,
568 krb5_config_section *c,
569 krb5_config_section **head)
571 krb5_config_binding *d, *previous = NULL;
573 *head = NULL;
575 while (c) {
576 d = calloc(1, sizeof(*d));
578 if (*head == NULL)
579 *head = d;
581 d->name = strdup(c->name);
582 d->type = c->type;
583 if (d->type == krb5_config_string)
584 d->u.string = strdup(c->u.string);
585 else if (d->type == krb5_config_list)
586 _krb5_config_copy (context, c->u.list, &d->u.list);
587 else
588 krb5_abortx(context,
589 "unknown binding type (%d) in krb5_config_copy",
590 d->type);
591 if (previous)
592 previous->next = d;
594 previous = d;
595 c = c->next;
597 return 0;
600 #endif /* HEIMDAL_SMALLER */
602 KRB5_LIB_FUNCTION const void * KRB5_LIB_CALL
603 _krb5_config_get_next (krb5_context context,
604 const krb5_config_section *c,
605 const krb5_config_binding **pointer,
606 int type,
607 ...)
609 const char *ret;
610 va_list args;
612 va_start(args, type);
613 ret = _krb5_config_vget_next (context, c, pointer, type, args);
614 va_end(args);
615 return ret;
618 static const void *
619 vget_next(krb5_context context,
620 const krb5_config_binding *b,
621 const krb5_config_binding **pointer,
622 int type,
623 const char *name,
624 va_list args)
626 const char *p = va_arg(args, const char *);
627 while(b != NULL) {
628 if(strcmp(b->name, name) == 0) {
629 if(b->type == type && p == NULL) {
630 *pointer = b;
631 return b->u.generic;
632 } else if(b->type == krb5_config_list && p != NULL) {
633 return vget_next(context, b->u.list, pointer, type, p, args);
636 b = b->next;
638 return NULL;
641 KRB5_LIB_FUNCTION const void * KRB5_LIB_CALL
642 _krb5_config_vget_next (krb5_context context,
643 const krb5_config_section *c,
644 const krb5_config_binding **pointer,
645 int type,
646 va_list args)
648 const krb5_config_binding *b;
649 const char *p;
651 if(c == NULL)
652 c = context->cf;
654 if (c == NULL)
655 return NULL;
657 if (*pointer == NULL) {
658 /* first time here, walk down the tree looking for the right
659 section */
660 p = va_arg(args, const char *);
661 if (p == NULL)
662 return NULL;
663 return vget_next(context, c, pointer, type, p, args);
666 /* we were called again, so just look for more entries with the
667 same name and type */
668 for (b = (*pointer)->next; b != NULL; b = b->next) {
669 if(strcmp(b->name, (*pointer)->name) == 0 && b->type == type) {
670 *pointer = b;
671 return b->u.generic;
674 return NULL;
677 KRB5_LIB_FUNCTION const void * KRB5_LIB_CALL
678 _krb5_config_get (krb5_context context,
679 const krb5_config_section *c,
680 int type,
681 ...)
683 const void *ret;
684 va_list args;
686 va_start(args, type);
687 ret = _krb5_config_vget (context, c, type, args);
688 va_end(args);
689 return ret;
693 const void *
694 _krb5_config_vget (krb5_context context,
695 const krb5_config_section *c,
696 int type,
697 va_list args)
699 const krb5_config_binding *foo = NULL;
701 return _krb5_config_vget_next (context, c, &foo, type, args);
705 * Get a list of configuration binding list for more processing
707 * @param context A Kerberos 5 context.
708 * @param c a configuration section, or NULL to use the section from context
709 * @param ... a list of names, terminated with NULL.
711 * @return NULL if configuration list is not found, a list otherwise
713 * @ingroup krb5_support
716 KRB5_LIB_FUNCTION const krb5_config_binding * KRB5_LIB_CALL
717 krb5_config_get_list (krb5_context context,
718 const krb5_config_section *c,
719 ...)
721 const krb5_config_binding *ret;
722 va_list args;
724 va_start(args, c);
725 ret = krb5_config_vget_list (context, c, args);
726 va_end(args);
727 return ret;
731 * Get a list of configuration binding list for more processing
733 * @param context A Kerberos 5 context.
734 * @param c a configuration section, or NULL to use the section from context
735 * @param args a va_list of arguments
737 * @return NULL if configuration list is not found, a list otherwise
739 * @ingroup krb5_support
742 KRB5_LIB_FUNCTION const krb5_config_binding * KRB5_LIB_CALL
743 krb5_config_vget_list (krb5_context context,
744 const krb5_config_section *c,
745 va_list args)
747 return _krb5_config_vget (context, c, krb5_config_list, args);
751 * Returns a "const char *" to a string in the configuration database.
752 * The string may not be valid after a reload of the configuration
753 * database so a caller should make a local copy if it needs to keep
754 * the string.
756 * @param context A Kerberos 5 context.
757 * @param c a configuration section, or NULL to use the section from context
758 * @param ... a list of names, terminated with NULL.
760 * @return NULL if configuration string not found, a string otherwise
762 * @ingroup krb5_support
765 KRB5_LIB_FUNCTION const char* KRB5_LIB_CALL
766 krb5_config_get_string (krb5_context context,
767 const krb5_config_section *c,
768 ...)
770 const char *ret;
771 va_list args;
773 va_start(args, c);
774 ret = krb5_config_vget_string (context, c, args);
775 va_end(args);
776 return ret;
780 * Like krb5_config_get_string(), but uses a va_list instead of ...
782 * @param context A Kerberos 5 context.
783 * @param c a configuration section, or NULL to use the section from context
784 * @param args a va_list of arguments
786 * @return NULL if configuration string not found, a string otherwise
788 * @ingroup krb5_support
791 KRB5_LIB_FUNCTION const char* KRB5_LIB_CALL
792 krb5_config_vget_string (krb5_context context,
793 const krb5_config_section *c,
794 va_list args)
796 return _krb5_config_vget (context, c, krb5_config_string, args);
800 * Like krb5_config_vget_string(), but instead of returning NULL,
801 * instead return a default value.
803 * @param context A Kerberos 5 context.
804 * @param c a configuration section, or NULL to use the section from context
805 * @param def_value the default value to return if no configuration
806 * found in the database.
807 * @param args a va_list of arguments
809 * @return a configuration string
811 * @ingroup krb5_support
814 KRB5_LIB_FUNCTION const char* KRB5_LIB_CALL
815 krb5_config_vget_string_default (krb5_context context,
816 const krb5_config_section *c,
817 const char *def_value,
818 va_list args)
820 const char *ret;
822 ret = krb5_config_vget_string (context, c, args);
823 if (ret == NULL)
824 ret = def_value;
825 return ret;
829 * Like krb5_config_get_string(), but instead of returning NULL,
830 * instead return a default value.
832 * @param context A Kerberos 5 context.
833 * @param c a configuration section, or NULL to use the section from context
834 * @param def_value the default value to return if no configuration
835 * found in the database.
836 * @param ... a list of names, terminated with NULL.
838 * @return a configuration string
840 * @ingroup krb5_support
843 KRB5_LIB_FUNCTION const char* KRB5_LIB_CALL
844 krb5_config_get_string_default (krb5_context context,
845 const krb5_config_section *c,
846 const char *def_value,
847 ...)
849 const char *ret;
850 va_list args;
852 va_start(args, def_value);
853 ret = krb5_config_vget_string_default (context, c, def_value, args);
854 va_end(args);
855 return ret;
859 * Get a list of configuration strings, free the result with
860 * krb5_config_free_strings().
862 * @param context A Kerberos 5 context.
863 * @param c a configuration section, or NULL to use the section from context
864 * @param args a va_list of arguments
866 * @return TRUE or FALSE
868 * @ingroup krb5_support
871 KRB5_LIB_FUNCTION char ** KRB5_LIB_CALL
872 krb5_config_vget_strings(krb5_context context,
873 const krb5_config_section *c,
874 va_list args)
876 char **strings = NULL;
877 int nstr = 0;
878 const krb5_config_binding *b = NULL;
879 const char *p;
881 while((p = _krb5_config_vget_next(context, c, &b,
882 krb5_config_string, args))) {
883 char *tmp = strdup(p);
884 char *pos = NULL;
885 char *s;
886 if(tmp == NULL)
887 goto cleanup;
888 s = strtok_r(tmp, " \t", &pos);
889 while(s){
890 char **tmp2 = realloc(strings, (nstr + 1) * sizeof(*strings));
891 if(tmp2 == NULL)
892 goto cleanup;
893 strings = tmp2;
894 strings[nstr] = strdup(s);
895 nstr++;
896 if(strings[nstr-1] == NULL)
897 goto cleanup;
898 s = strtok_r(NULL, " \t", &pos);
900 free(tmp);
902 if(nstr){
903 char **tmp = realloc(strings, (nstr + 1) * sizeof(*strings));
904 if(tmp == NULL)
905 goto cleanup;
906 strings = tmp;
907 strings[nstr] = NULL;
909 return strings;
910 cleanup:
911 while(nstr--)
912 free(strings[nstr]);
913 free(strings);
914 return NULL;
919 * Get a list of configuration strings, free the result with
920 * krb5_config_free_strings().
922 * @param context A Kerberos 5 context.
923 * @param c a configuration section, or NULL to use the section from context
924 * @param ... a list of names, terminated with NULL.
926 * @return TRUE or FALSE
928 * @ingroup krb5_support
931 KRB5_LIB_FUNCTION char** KRB5_LIB_CALL
932 krb5_config_get_strings(krb5_context context,
933 const krb5_config_section *c,
934 ...)
936 va_list ap;
937 char **ret;
938 va_start(ap, c);
939 ret = krb5_config_vget_strings(context, c, ap);
940 va_end(ap);
941 return ret;
945 * Free the resulting strings from krb5_config-get_strings() and
946 * krb5_config_vget_strings().
948 * @param strings strings to free
950 * @ingroup krb5_support
953 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
954 krb5_config_free_strings(char **strings)
956 char **s = strings;
957 while(s && *s){
958 free(*s);
959 s++;
961 free(strings);
965 * Like krb5_config_get_bool_default() but with a va_list list of
966 * configuration selection.
968 * Configuration value to a boolean value, where yes/true and any
969 * non-zero number means TRUE and other value is FALSE.
971 * @param context A Kerberos 5 context.
972 * @param c a configuration section, or NULL to use the section from context
973 * @param def_value the default value to return if no configuration
974 * found in the database.
975 * @param args a va_list of arguments
977 * @return TRUE or FALSE
979 * @ingroup krb5_support
982 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
983 krb5_config_vget_bool_default (krb5_context context,
984 const krb5_config_section *c,
985 krb5_boolean def_value,
986 va_list args)
988 const char *str;
989 str = krb5_config_vget_string (context, c, args);
990 if(str == NULL)
991 return def_value;
992 if(strcasecmp(str, "yes") == 0 ||
993 strcasecmp(str, "true") == 0 ||
994 atoi(str)) return TRUE;
995 return FALSE;
999 * krb5_config_get_bool() will convert the configuration
1000 * option value to a boolean value, where yes/true and any non-zero
1001 * number means TRUE and other value is FALSE.
1003 * @param context A Kerberos 5 context.
1004 * @param c a configuration section, or NULL to use the section from context
1005 * @param args a va_list of arguments
1007 * @return TRUE or FALSE
1009 * @ingroup krb5_support
1012 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1013 krb5_config_vget_bool (krb5_context context,
1014 const krb5_config_section *c,
1015 va_list args)
1017 return krb5_config_vget_bool_default (context, c, FALSE, args);
1021 * krb5_config_get_bool_default() will convert the configuration
1022 * option value to a boolean value, where yes/true and any non-zero
1023 * number means TRUE and other value is FALSE.
1025 * @param context A Kerberos 5 context.
1026 * @param c a configuration section, or NULL to use the section from context
1027 * @param def_value the default value to return if no configuration
1028 * found in the database.
1029 * @param ... a list of names, terminated with NULL.
1031 * @return TRUE or FALSE
1033 * @ingroup krb5_support
1036 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1037 krb5_config_get_bool_default (krb5_context context,
1038 const krb5_config_section *c,
1039 krb5_boolean def_value,
1040 ...)
1042 va_list ap;
1043 krb5_boolean ret;
1044 va_start(ap, def_value);
1045 ret = krb5_config_vget_bool_default(context, c, def_value, ap);
1046 va_end(ap);
1047 return ret;
1051 * Like krb5_config_get_bool() but with a va_list list of
1052 * configuration selection.
1054 * Configuration value to a boolean value, where yes/true and any
1055 * non-zero number means TRUE and other value is FALSE.
1057 * @param context A Kerberos 5 context.
1058 * @param c a configuration section, or NULL to use the section from context
1059 * @param ... a list of names, terminated with NULL.
1061 * @return TRUE or FALSE
1063 * @ingroup krb5_support
1066 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1067 krb5_config_get_bool (krb5_context context,
1068 const krb5_config_section *c,
1069 ...)
1071 va_list ap;
1072 krb5_boolean ret;
1073 va_start(ap, c);
1074 ret = krb5_config_vget_bool (context, c, ap);
1075 va_end(ap);
1076 return ret;
1080 * Get the time from the configuration file using a relative time.
1082 * Like krb5_config_get_time_default() but with a va_list list of
1083 * configuration selection.
1085 * @param context A Kerberos 5 context.
1086 * @param c a configuration section, or NULL to use the section from context
1087 * @param def_value the default value to return if no configuration
1088 * found in the database.
1089 * @param args a va_list of arguments
1091 * @return parsed the time (or def_value on parse error)
1093 * @ingroup krb5_support
1096 KRB5_LIB_FUNCTION int KRB5_LIB_CALL
1097 krb5_config_vget_time_default (krb5_context context,
1098 const krb5_config_section *c,
1099 int def_value,
1100 va_list args)
1102 const char *str;
1103 krb5_deltat t;
1105 str = krb5_config_vget_string (context, c, args);
1106 if(str == NULL)
1107 return def_value;
1108 if (krb5_string_to_deltat(str, &t))
1109 return def_value;
1110 return t;
1114 * Get the time from the configuration file using a relative time, for example: 1h30s
1116 * @param context A Kerberos 5 context.
1117 * @param c a configuration section, or NULL to use the section from context
1118 * @param args a va_list of arguments
1120 * @return parsed the time or -1 on error
1122 * @ingroup krb5_support
1125 KRB5_LIB_FUNCTION int KRB5_LIB_CALL
1126 krb5_config_vget_time (krb5_context context,
1127 const krb5_config_section *c,
1128 va_list args)
1130 return krb5_config_vget_time_default (context, c, -1, args);
1134 * Get the time from the configuration file using a relative time, for example: 1h30s
1136 * @param context A Kerberos 5 context.
1137 * @param c a configuration section, or NULL to use the section from context
1138 * @param def_value the default value to return if no configuration
1139 * found in the database.
1140 * @param ... a list of names, terminated with NULL.
1142 * @return parsed the time (or def_value on parse error)
1144 * @ingroup krb5_support
1147 KRB5_LIB_FUNCTION int KRB5_LIB_CALL
1148 krb5_config_get_time_default (krb5_context context,
1149 const krb5_config_section *c,
1150 int def_value,
1151 ...)
1153 va_list ap;
1154 int ret;
1155 va_start(ap, def_value);
1156 ret = krb5_config_vget_time_default(context, c, def_value, ap);
1157 va_end(ap);
1158 return ret;
1162 * Get the time from the configuration file using a relative time, for example: 1h30s
1164 * @param context A Kerberos 5 context.
1165 * @param c a configuration section, or NULL to use the section from context
1166 * @param ... a list of names, terminated with NULL.
1168 * @return parsed the time or -1 on error
1170 * @ingroup krb5_support
1173 KRB5_LIB_FUNCTION int KRB5_LIB_CALL
1174 krb5_config_get_time (krb5_context context,
1175 const krb5_config_section *c,
1176 ...)
1178 va_list ap;
1179 int ret;
1180 va_start(ap, c);
1181 ret = krb5_config_vget_time (context, c, ap);
1182 va_end(ap);
1183 return ret;
1187 KRB5_LIB_FUNCTION int KRB5_LIB_CALL
1188 krb5_config_vget_int_default (krb5_context context,
1189 const krb5_config_section *c,
1190 int def_value,
1191 va_list args)
1193 const char *str;
1194 str = krb5_config_vget_string (context, c, args);
1195 if(str == NULL)
1196 return def_value;
1197 else {
1198 char *endptr;
1199 long l;
1200 l = strtol(str, &endptr, 0);
1201 if (endptr == str)
1202 return def_value;
1203 else
1204 return l;
1208 KRB5_LIB_FUNCTION int KRB5_LIB_CALL
1209 krb5_config_vget_int (krb5_context context,
1210 const krb5_config_section *c,
1211 va_list args)
1213 return krb5_config_vget_int_default (context, c, -1, args);
1216 KRB5_LIB_FUNCTION int KRB5_LIB_CALL
1217 krb5_config_get_int_default (krb5_context context,
1218 const krb5_config_section *c,
1219 int def_value,
1220 ...)
1222 va_list ap;
1223 int ret;
1224 va_start(ap, def_value);
1225 ret = krb5_config_vget_int_default(context, c, def_value, ap);
1226 va_end(ap);
1227 return ret;
1230 KRB5_LIB_FUNCTION int KRB5_LIB_CALL
1231 krb5_config_get_int (krb5_context context,
1232 const krb5_config_section *c,
1233 ...)
1235 va_list ap;
1236 int ret;
1237 va_start(ap, c);
1238 ret = krb5_config_vget_int (context, c, ap);
1239 va_end(ap);
1240 return ret;
1244 #ifndef HEIMDAL_SMALLER
1247 * Deprecated: configuration files are not strings
1249 * @ingroup krb5_deprecated
1252 KRB5_DEPRECATED
1253 krb5_error_code KRB5_LIB_FUNCTION
1254 krb5_config_parse_string_multi(krb5_context context,
1255 const char *string,
1256 krb5_config_section **res)
1258 const char *str;
1259 unsigned lineno = 0;
1260 krb5_error_code ret;
1261 struct fileptr f;
1262 f.f = NULL;
1263 f.s = string;
1265 ret = krb5_config_parse_debug (&f, res, &lineno, &str);
1266 if (ret) {
1267 krb5_set_error_message (context, ret, "%s:%u: %s",
1268 "<constant>", lineno, str);
1269 return ret;
1271 return 0;
1274 #endif