Debian Codegraph @

lib/krb5/config_file.c

raw ◀ ▶
Source reconstructed at patch state 1 of 3 — not the installed (fully patched) source.
3 Debian patches touch this file
1 /*
2 * Copyright (c) 1997 - 2004 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 #include "krb5_locl.h"
37
38 #ifdef __APPLE__
39 #include <CoreFoundation/CoreFoundation.h>
40 #endif
41
42 /* Gaah! I want a portable funopen */
43 struct fileptr {
44 krb5_context context;
45 const char *s;
46 FILE *f;
47 };
48
49 static char *
50 config_fgets(char *str, size_t len, struct fileptr *ptr)
51 {
52 /* XXX this is not correct, in that they don't do the same if the
53 line is longer than len */
54 if(ptr->f != NULL)
55 return fgets(str, len, ptr->f);
56 else {
57 /* this is almost strsep_copy */
58 const char *p;
59 ssize_t l;
60 if(*ptr->s == '\0')
61 return NULL;
62 p = ptr->s + strcspn(ptr->s, "\n");
63 if(*p == '\n')
64 p++;
65 l = min(len, (size_t)(p - ptr->s));
66 if(len > 0) {
67 memcpy(str, ptr->s, l);
68 str[l] = '\0';
69 }
70 ptr->s = p;
71 return str;
72 }
73 }
74
75 static krb5_error_code parse_section(char *p, krb5_config_section **s,
76 krb5_config_section **res,
77 const char **err_message);
78 static krb5_error_code parse_binding(struct fileptr *f, unsigned *lineno, char *p,
79 krb5_config_binding **b,
80 krb5_config_binding **parent,
81 const char **err_message);
82 static krb5_error_code parse_list(struct fileptr *f, unsigned *lineno,
83 krb5_config_binding **parent,
84 const char **err_message);
85
86 KRB5_LIB_FUNCTION krb5_config_section * KRB5_LIB_CALL
87 _krb5_config_get_entry(krb5_config_section **parent, const char *name, int type)
88 {
89 krb5_config_section **q;
90
91 for(q = parent; *q != NULL; q = &(*q)->next)
92 if(type == krb5_config_list &&
93 (unsigned)type == (*q)->type &&
94 strcmp(name, (*q)->name) == 0)
95 return *q;
96 *q = calloc(1, sizeof(**q));
97 if(*q == NULL)
98 return NULL;
99 (*q)->name = strdup(name);
100 (*q)->type = type;
101 if((*q)->name == NULL) {
102 free(*q);
103 *q = NULL;
104 return NULL;
105 }
106 return *q;
107 }
108
109 /*
110 * Parse a section:
111 *
112 * [section]
113 * foo = bar
114 * b = {
115 * a
116 * }
117 * ...
118 *
119 * starting at the line in `p', storing the resulting structure in
120 * `s' and hooking it into `parent'.
121 * Store the error message in `err_message'.
122 */
123
124 static krb5_error_code
125 parse_section(char *p, krb5_config_section **s, krb5_config_section **parent,
126 const char **err_message)
127 {
128 char *p1;
129 krb5_config_section *tmp;
130
131 p1 = strchr (p + 1, ']');
132 if (p1 == NULL) {
133 *err_message = "missing ]";
134 return KRB5_CONFIG_BADFORMAT;
135 }
136 *p1 = '\0';
137 tmp = _krb5_config_get_entry(parent, p + 1, krb5_config_list);
138 if(tmp == NULL) {
139 *err_message = "out of memory";
140 return KRB5_CONFIG_BADFORMAT;
141 }
142 *s = tmp;
143 return 0;
144 }
145
146 /*
147 * Parse a brace-enclosed list from `f', hooking in the structure at
148 * `parent'.
149 * Store the error message in `err_message'.
150 */
151
152 static krb5_error_code
153 parse_list(struct fileptr *f, unsigned *lineno, krb5_config_binding **parent,
154 const char **err_message)
155 {
156 char buf[KRB5_BUFSIZ];
157 krb5_error_code ret;
158 krb5_config_binding *b = NULL;
159 unsigned beg_lineno = *lineno;
160
161 while(config_fgets(buf, sizeof(buf), f) != NULL) {
162 char *p;
163
164 ++*lineno;
165 buf[strcspn(buf, "\r\n")] = '\0';
166 p = buf;
167 while(isspace((unsigned char)*p))
168 ++p;
169 if (*p == '#' || *p == ';' || *p == '\0')
170 continue;
171 while(isspace((unsigned char)*p))
172 ++p;
173 if (*p == '}')
174 return 0;
175 if (*p == '\0')
176 continue;
177 ret = parse_binding (f, lineno, p, &b, parent, err_message);
178 if (ret)
179 return ret;
180 }
181 *lineno = beg_lineno;
182 *err_message = "unclosed {";
183 return KRB5_CONFIG_BADFORMAT;
184 }
185
186 /*
187 *
188 */
189
190 static krb5_error_code
191 parse_binding(struct fileptr *f, unsigned *lineno, char *p,
192 krb5_config_binding **b, krb5_config_binding **parent,
193 const char **err_message)
194 {
195 krb5_config_binding *tmp;
196 char *p1, *p2;
197 krb5_error_code ret = 0;
198
199 p1 = p;
200 while (*p && *p != '=' && !isspace((unsigned char)*p))
201 ++p;
202 if (*p == '\0') {
203 *err_message = "missing =";
204 return KRB5_CONFIG_BADFORMAT;
205 }
206 p2 = p;
207 while (isspace((unsigned char)*p))
208 ++p;
209 if (*p != '=') {
210 *err_message = "missing =";
211 return KRB5_CONFIG_BADFORMAT;
212 }
213 ++p;
214 while(isspace((unsigned char)*p))
215 ++p;
216 *p2 = '\0';
217 if (*p == '{') {
218 tmp = _krb5_config_get_entry(parent, p1, krb5_config_list);
219 if (tmp == NULL) {
220 *err_message = "out of memory";
221 return KRB5_CONFIG_BADFORMAT;
222 }
223 ret = parse_list (f, lineno, &tmp->u.list, err_message);
224 } else {
225 tmp = _krb5_config_get_entry(parent, p1, krb5_config_string);
226 if (tmp == NULL) {
227 *err_message = "out of memory";
228 return KRB5_CONFIG_BADFORMAT;
229 }
230 p1 = p;
231 p = p1 + strlen(p1);
232 while(p > p1 && isspace((unsigned char)*(p-1)))
233 --p;
234 *p = '\0';
235 tmp->u.string = strdup(p1);
236 }
237 *b = tmp;
238 return ret;
239 }
240
241 #if defined(__APPLE__)
242
243 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
244 #define HAVE_CFPROPERTYLISTCREATEWITHSTREAM 1
245 #endif
246
247 static char *
248 cfstring2cstring(CFStringRef string)
249 {
250 CFIndex len;
251 char *str;
252
253 str = (char *) CFStringGetCStringPtr(string, kCFStringEncodingUTF8);
254 if (str)
255 return strdup(str);
256
257 len = CFStringGetLength(string);
258 len = 1 + CFStringGetMaximumSizeForEncoding(len, kCFStringEncodingUTF8);
259 str = malloc(len);
260 if (str == NULL)
261 return NULL;
262
263 if (!CFStringGetCString (string, str, len, kCFStringEncodingUTF8)) {
264 free (str);
265 return NULL;
266 }
267 return str;
268 }
269
270 static void
271 convert_content(const void *key, const void *value, void *context)
272 {
273 krb5_config_section *tmp, **parent = context;
274 char *k;
275
276 if (CFGetTypeID(key) != CFStringGetTypeID())
277 return;
278
279 k = cfstring2cstring(key);
280 if (k == NULL)
281 return;
282
283 if (CFGetTypeID(value) == CFStringGetTypeID()) {
284 tmp = _krb5_config_get_entry(parent, k, krb5_config_string);
285 tmp->u.string = cfstring2cstring(value);
286 } else if (CFGetTypeID(value) == CFDictionaryGetTypeID()) {
287 tmp = _krb5_config_get_entry(parent, k, krb5_config_list);
288 CFDictionaryApplyFunction(value, convert_content, &tmp->u.list);
289 } else {
290 /* log */
291 }
292 free(k);
293 }
294
295 static krb5_error_code
296 parse_plist_config(krb5_context context, const char *path, krb5_config_section **parent)
297 {
298 CFReadStreamRef s;
299 CFDictionaryRef d;
300 CFURLRef url;
301
302 url = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (UInt8 *)path, strlen(path), FALSE);
303 if (url == NULL) {
304 krb5_clear_error_message(context);
305 return ENOMEM;
306 }
307
308 s = CFReadStreamCreateWithFile(kCFAllocatorDefault, url);
309 CFRelease(url);
310 if (s == NULL) {
311 krb5_clear_error_message(context);
312 return ENOMEM;
313 }
314
315 if (!CFReadStreamOpen(s)) {
316 CFRelease(s);
317 krb5_clear_error_message(context);
318 return ENOENT;
319 }
320
321 #ifdef HAVE_CFPROPERTYLISTCREATEWITHSTREAM
322 d = (CFDictionaryRef)CFPropertyListCreateWithStream(NULL, s, 0, kCFPropertyListImmutable, NULL, NULL);
323 #else
324 d = (CFDictionaryRef)CFPropertyListCreateFromStream(NULL, s, 0, kCFPropertyListImmutable, NULL, NULL);
325 #endif
326 CFRelease(s);
327 if (d == NULL) {
328 krb5_clear_error_message(context);
329 return ENOENT;
330 }
331
332 CFDictionaryApplyFunction(d, convert_content, parent);
333 CFRelease(d);
334
335 return 0;
336 }
337
338 #endif
339
340
341 /*
342 * Parse the config file `fname', generating the structures into `res'
343 * returning error messages in `err_message'
344 */
345
346 static krb5_error_code
347 krb5_config_parse_debug (struct fileptr *f,
348 krb5_config_section **res,
349 unsigned *lineno,
350 const char **err_message)
351 {
352 krb5_config_section *s = NULL;
353 krb5_config_binding *b = NULL;
354 char buf[KRB5_BUFSIZ];
355 krb5_error_code ret;
356
357 *lineno = 0;
358 *err_message = "";
359
360 while (config_fgets(buf, sizeof(buf), f) != NULL) {
361 char *p;
362
363 ++*lineno;
364 buf[strcspn(buf, "\r\n")] = '\0';
365 p = buf;
366 while(isspace((unsigned char)*p))
367 ++p;
368 if (*p == '#' || *p == ';')
369 continue;
370 if (*p == '[') {
371 ret = parse_section(p, &s, res, err_message);
372 if (ret)
373 return ret;
374 b = NULL;
375 } else if (*p == '}') {
376 *err_message = "unmatched }";
377 return KRB5_CONFIG_BADFORMAT;
378 } else if (strncmp(p, "include", sizeof("include") - 1) == 0 &&
379 isspace(p[sizeof("include") - 1])) {
380 p += sizeof("include");
381 while (isspace(*p))
382 p++;
383 ret = krb5_config_parse_file_multi(f->context, p, res);
384 if (ret)
385 return ret;
386 } else if (strncmp(p, "includedir", sizeof("includedir") - 1) == 0 &&
387 isspace(p[sizeof("includedir") - 1])) {
388 p += sizeof("includedir");
389 while (isspace(*p))
390 p++;
391 ret = krb5_config_parse_dir_multi(f->context, p, res);
392 if (ret)
393 return ret;
394 } else if(*p != '\0') {
395 if (s == NULL) {
396 *err_message = "binding before section";
397 return KRB5_CONFIG_BADFORMAT;
398 }
399 ret = parse_binding(f, lineno, p, &b, &s->u.list, err_message);
400 if (ret)
401 return ret;
402 }
403 }
404 return 0;
405 }
406
407 static int
408 is_plist_file(const char *fname)
409 {
410 size_t len = strlen(fname);
411 char suffix[] = ".plist";
412 if (len < sizeof(suffix))
413 return 0;
414 if (strcasecmp(&fname[len - (sizeof(suffix) - 1)], suffix) != 0)
415 return 0;
416 return 1;
417 }
418
419 /**
420 * Parse configuration files in the given directory and add the result
421 * into res. Only files whose names consist only of alphanumeric
422 * characters, hyphen, and underscore, will be parsed, though files
423 * ending in ".conf" will also be parsed.
424 *
425 * This interface can be used to parse several configuration directories
426 * into one resulting krb5_config_section by calling it repeatably.
427 *
428 * @param context a Kerberos 5 context.
429 * @param dname a directory name to a Kerberos configuration file
430 * @param res the returned result, must be free with krb5_free_config_files().
431 * @return Return an error code or 0, see krb5_get_error_message().
432 *
433 * @ingroup krb5_support
434 */
435
436 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
437 krb5_config_parse_dir_multi(krb5_context context,
438 const char *dname,
439 krb5_config_section **res)
440 {
441 struct dirent *entry;
442 krb5_error_code ret;
443 DIR *d;
444
445 if ((d = opendir(dname)) == NULL)
446 return errno;
447
448 while ((entry = readdir(d)) != NULL) {
449 char *p = entry->d_name;
450 char *path;
451 int is_valid = 1;
452
453 while (*p) {
454 if (!isalpha(*p) && *p != '_' && *p != '-' &&
455 strcmp(p, ".conf") != 0) {
456 is_valid = 0;
457 break;
458 }
459 p++;
460 }
461 if (!is_valid)
462 continue;
463
464 if (asprintf(&path, "%s/%s", dname, entry->d_name) == -1 ||
465 path == NULL)
466 return krb5_enomem(context);
467 ret = krb5_config_parse_file_multi(context, path, res);
468 free(path);
469 if (ret == ENOMEM)
470 return krb5_enomem(context);;
471 /* Ignore malformed config files */
472 }
473 (void) closedir(d);
474 return 0;
475 }
476
477 /**
478 * Parse a configuration file and add the result into res. This
479 * interface can be used to parse several configuration files into one
480 * resulting krb5_config_section by calling it repeatably.
481 *
482 * @param context a Kerberos 5 context.
483 * @param fname a file name to a Kerberos configuration file
484 * @param res the returned result, must be free with krb5_free_config_files().
485 * @return Return an error code or 0, see krb5_get_error_message().
486 *
487 * @ingroup krb5_support
488 */
489
490 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
491 krb5_config_parse_file_multi (krb5_context context,
492 const char *fname,
493 krb5_config_section **res)
494 {
495 const char *str;
496 char *newfname = NULL;
497 unsigned lineno = 0;
498 krb5_error_code ret;
499 struct fileptr f;
500
501 if (context->config_include_depth > 5) {
502 krb5_warnx(context, "Maximum config file include depth reached; "
503 "not including %s", fname);
504 return 0;
505 }
506 context->config_include_depth++;
507
508 /**
509 * If the fname starts with "~/" parse configuration file in the
510 * current users home directory. The behavior can be disabled and
511 * enabled by calling krb5_set_home_dir_access().
512 */
513 if (ISTILDE(fname[0]) && ISPATHSEP(fname[1])) {
514 #ifndef KRB5_USE_PATH_TOKENS
515 const char *home = NULL;
516
517 if (!_krb5_homedir_access(context)) {
518 context->config_include_depth--;
519 krb5_set_error_message(context, EPERM,
520 "Access to home directory not allowed");
521 return EPERM;
522 }
523
524 if(!issuid())
525 home = getenv("HOME");
526
527 if (home == NULL) {
528 struct passwd *pw = getpwuid(getuid());
529 if(pw != NULL)
530 home = pw->pw_dir;
531 }
532 if (home) {
533 int aret;
534
535 aret = asprintf(&newfname, "%s%s", home, &fname[1]);
536 if (aret == -1 || newfname == NULL) {
537 context->config_include_depth--;
538 return krb5_enomem(context);
539 }
540 fname = newfname;
541 }
542 #else /* KRB5_USE_PATH_TOKENS */
543 if (asprintf(&newfname, "%%{USERCONFIG}%s", &fname[1]) < 0 ||
544 newfname == NULL) {
545 context->config_include_depth--;
546 return krb5_enomem(context);
547 }
548 fname = newfname;
549 #endif
550 }
551
552 if (is_plist_file(fname)) {
553 #ifdef __APPLE__
554 ret = parse_plist_config(context, fname, res);
555 context->config_include_depth--;
556 if (ret) {
557 krb5_set_error_message(context, ret,
558 "Failed to parse plist %s", fname);
559 if (newfname)
560 free(newfname);
561 return ret;
562 }
563 #else
564 krb5_set_error_message(context, ENOENT,
565 "no support for plist configuration files");
566 return ENOENT;
567 #endif
568 } else {
569 #ifdef KRB5_USE_PATH_TOKENS
570 char * exp_fname = NULL;
571
572 ret = _krb5_expand_path_tokens(context, fname, 1, &exp_fname);
573 if (ret) {
574 context->config_include_depth--;
575 if (newfname)
576 free(newfname);
577 return ret;
578 }
579
580 if (newfname)
581 free(newfname);
582 fname = newfname = exp_fname;
583 #endif
584
585 f.context = context;
586 f.f = fopen(fname, "r");
587 f.s = NULL;
588 if(f.f == NULL) {
589 context->config_include_depth--;
590 ret = errno;
591 krb5_set_error_message (context, ret, "open %s: %s",
592 fname, strerror(ret));
593 if (newfname)
594 free(newfname);
595 return ret;
596 }
597
598 ret = krb5_config_parse_debug (&f, res, &lineno, &str);
599 context->config_include_depth--;
600 fclose(f.f);
601 if (ret) {
602 krb5_set_error_message (context, ret, "%s:%u: %s",
603 fname, lineno, str);
604 if (newfname)
605 free(newfname);
606 return ret;
607 }
608 }
609 return 0;
610 }
611
612 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
613 krb5_config_parse_file (krb5_context context,
614 const char *fname,
615 krb5_config_section **res)
616 {
617 *res = NULL;
618 return krb5_config_parse_file_multi(context, fname, res);
619 }
620
621 static void
622 free_binding (krb5_context context, krb5_config_binding *b)
623 {
624 krb5_config_binding *next_b;
625
626 while (b) {
627 free (b->name);
628 if (b->type == krb5_config_string)
629 free (b->u.string);
630 else if (b->type == krb5_config_list)
631 free_binding (context, b->u.list);
632 else
633 krb5_abortx(context, "unknown binding type (%d) in free_binding",
634 b->type);
635 next_b = b->next;
636 free (b);
637 b = next_b;
638 }
639 }
640
641 /**
642 * Free configuration file section, the result of
643 * krb5_config_parse_file() and krb5_config_parse_file_multi().
644 *
645 * @param context A Kerberos 5 context
646 * @param s the configuration section to free
647 *
648 * @return returns 0 on successes, otherwise an error code, see
649 * krb5_get_error_message()
650 *
651 * @ingroup krb5_support
652 */
653
654 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
655 krb5_config_file_free (krb5_context context, krb5_config_section *s)
656 {
657 free_binding (context, s);
658 return 0;
659 }
660
661 #ifndef HEIMDAL_SMALLER
662
663 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
664 _krb5_config_copy(krb5_context context,
665 krb5_config_section *c,
666 krb5_config_section **head)
667 {
668 krb5_config_binding *d, *previous = NULL;
669
670 *head = NULL;
671
672 while (c) {
673 d = calloc(1, sizeof(*d));
674
675 if (*head == NULL)
676 *head = d;
677
678 d->name = strdup(c->name);
679 d->type = c->type;
680 if (d->type == krb5_config_string)
681 d->u.string = strdup(c->u.string);
682 else if (d->type == krb5_config_list)
683 _krb5_config_copy (context, c->u.list, &d->u.list);
684 else
685 krb5_abortx(context,
686 "unknown binding type (%d) in krb5_config_copy",
687 d->type);
688 if (previous)
689 previous->next = d;
690
691 previous = d;
692 c = c->next;
693 }
694 return 0;
695 }
696
697 #endif /* HEIMDAL_SMALLER */
698
699 KRB5_LIB_FUNCTION const void * KRB5_LIB_CALL
700 _krb5_config_get_next (krb5_context context,
701 const krb5_config_section *c,
702 const krb5_config_binding **pointer,
703 int type,
704 ...)
705 {
706 const char *ret;
707 va_list args;
708
709 va_start(args, type);
710 ret = _krb5_config_vget_next (context, c, pointer, type, args);
711 va_end(args);
712 return ret;
713 }
714
715 static const void *
716 vget_next(krb5_context context,
717 const krb5_config_binding *b,
718 const krb5_config_binding **pointer,
719 int type,
720 const char *name,
721 va_list args)
722 {
723 const char *p = va_arg(args, const char *);
724 while(b != NULL) {
725 if(strcmp(b->name, name) == 0) {
726 if(b->type == (unsigned)type && p == NULL) {
727 *pointer = b;
728 return b->u.generic;
729 } else if(b->type == krb5_config_list && p != NULL) {
730 return vget_next(context, b->u.list, pointer, type, p, args);
731 }
732 }
733 b = b->next;
734 }
735 return NULL;
736 }
737
738 KRB5_LIB_FUNCTION const void * KRB5_LIB_CALL
739 _krb5_config_vget_next (krb5_context context,
740 const krb5_config_section *c,
741 const krb5_config_binding **pointer,
742 int type,
743 va_list args)
744 {
745 const krb5_config_binding *b;
746 const char *p;
747
748 if(c == NULL)
749 c = context->cf;
750
751 if (c == NULL)
752 return NULL;
753
754 if (*pointer == NULL) {
755 /* first time here, walk down the tree looking for the right
756 section */
757 p = va_arg(args, const char *);
758 if (p == NULL)
759 return NULL;
760 return vget_next(context, c, pointer, type, p, args);
761 }
762
763 /* we were called again, so just look for more entries with the
764 same name and type */
765 for (b = (*pointer)->next; b != NULL; b = b->next) {
766 if(strcmp(b->name, (*pointer)->name) == 0 && b->type == (unsigned)type) {
767 *pointer = b;
768 return b->u.generic;
769 }
770 }
771 return NULL;
772 }
773
774 KRB5_LIB_FUNCTION const void * KRB5_LIB_CALL
775 _krb5_config_get (krb5_context context,
776 const krb5_config_section *c,
777 int type,
778 ...)
779 {
780 const void *ret;
781 va_list args;
782
783 va_start(args, type);
784 ret = _krb5_config_vget (context, c, type, args);
785 va_end(args);
786 return ret;
787 }
788
789
790 KRB5_LIB_FUNCTION const void * KRB5_LIB_CALL
791 _krb5_config_vget (krb5_context context,
792 const krb5_config_section *c,
793 int type,
794 va_list args)
795 {
796 const krb5_config_binding *foo = NULL;
797
798 return _krb5_config_vget_next (context, c, &foo, type, args);
799 }
800
801 /**
802 * Get a list of configuration binding list for more processing
803 *
804 * @param context A Kerberos 5 context.
805 * @param c a configuration section, or NULL to use the section from context
806 * @param ... a list of names, terminated with NULL.
807 *
808 * @return NULL if configuration list is not found, a list otherwise
809 *
810 * @ingroup krb5_support
811 */
812
813 KRB5_LIB_FUNCTION const krb5_config_binding * KRB5_LIB_CALL
814 krb5_config_get_list (krb5_context context,
815 const krb5_config_section *c,
816 ...)
817 {
818 const krb5_config_binding *ret;
819 va_list args;
820
821 va_start(args, c);
822 ret = krb5_config_vget_list (context, c, args);
823 va_end(args);
824 return ret;
825 }
826
827 /**
828 * Get a list of configuration binding list for more processing
829 *
830 * @param context A Kerberos 5 context.
831 * @param c a configuration section, or NULL to use the section from context
832 * @param args a va_list of arguments
833 *
834 * @return NULL if configuration list is not found, a list otherwise
835 *
836 * @ingroup krb5_support
837 */
838
839 KRB5_LIB_FUNCTION const krb5_config_binding * KRB5_LIB_CALL
840 krb5_config_vget_list (krb5_context context,
841 const krb5_config_section *c,
842 va_list args)
843 {
844 return _krb5_config_vget (context, c, krb5_config_list, args);
845 }
846
847 /**
848 * Returns a "const char *" to a string in the configuration database.
849 * The string may not be valid after a reload of the configuration
850 * database so a caller should make a local copy if it needs to keep
851 * the string.
852 *
853 * @param context A Kerberos 5 context.
854 * @param c a configuration section, or NULL to use the section from context
855 * @param ... a list of names, terminated with NULL.
856 *
857 * @return NULL if configuration string not found, a string otherwise
858 *
859 * @ingroup krb5_support
860 */
861
862 KRB5_LIB_FUNCTION const char* KRB5_LIB_CALL
863 krb5_config_get_string (krb5_context context,
864 const krb5_config_section *c,
865 ...)
866 {
867 const char *ret;
868 va_list args;
869
870 va_start(args, c);
871 ret = krb5_config_vget_string (context, c, args);
872 va_end(args);
873 return ret;
874 }
875
876 /**
877 * Like krb5_config_get_string(), but uses a va_list instead of ...
878 *
879 * @param context A Kerberos 5 context.
880 * @param c a configuration section, or NULL to use the section from context
881 * @param args a va_list of arguments
882 *
883 * @return NULL if configuration string not found, a string otherwise
884 *
885 * @ingroup krb5_support
886 */
887
888 KRB5_LIB_FUNCTION const char* KRB5_LIB_CALL
889 krb5_config_vget_string (krb5_context context,
890 const krb5_config_section *c,
891 va_list args)
892 {
893 return _krb5_config_vget (context, c, krb5_config_string, args);
894 }
895
896 /**
897 * Like krb5_config_vget_string(), but instead of returning NULL,
898 * instead return a default value.
899 *
900 * @param context A Kerberos 5 context.
901 * @param c a configuration section, or NULL to use the section from context
902 * @param def_value the default value to return if no configuration
903 * found in the database.
904 * @param args a va_list of arguments
905 *
906 * @return a configuration string
907 *
908 * @ingroup krb5_support
909 */
910
911 KRB5_LIB_FUNCTION const char* KRB5_LIB_CALL
912 krb5_config_vget_string_default (krb5_context context,
913 const krb5_config_section *c,
914 const char *def_value,
915 va_list args)
916 {
917 const char *ret;
918
919 ret = krb5_config_vget_string (context, c, args);
920 if (ret == NULL)
921 ret = def_value;
922 return ret;
923 }
924
925 /**
926 * Like krb5_config_get_string(), but instead of returning NULL,
927 * instead return a default value.
928 *
929 * @param context A Kerberos 5 context.
930 * @param c a configuration section, or NULL to use the section from context
931 * @param def_value the default value to return if no configuration
932 * found in the database.
933 * @param ... a list of names, terminated with NULL.
934 *
935 * @return a configuration string
936 *
937 * @ingroup krb5_support
938 */
939
940 KRB5_LIB_FUNCTION const char* KRB5_LIB_CALL
941 krb5_config_get_string_default (krb5_context context,
942 const krb5_config_section *c,
943 const char *def_value,
944 ...)
945 {
946 const char *ret;
947 va_list args;
948
949 va_start(args, def_value);
950 ret = krb5_config_vget_string_default (context, c, def_value, args);
951 va_end(args);
952 return ret;
953 }
954
955 static char *
956 next_component_string(char * begin, const char * delims, char **state)
957 {
958 char * end;
959
960 if (begin == NULL)
961 begin = *state;
962
963 if (*begin == '\0')
964 return NULL;
965
966 end = begin;
967 while (*end == '"') {
968 char * t = strchr(end + 1, '"');
969
970 if (t)
971 end = ++t;
972 else
973 end += strlen(end);
974 }
975
976 if (*end != '\0') {
977 size_t pos;
978
979 pos = strcspn(end, delims);
980 end = end + pos;
981 }
982
983 if (*end != '\0') {
984 *end = '\0';
985 *state = end + 1;
986 if (*begin == '"' && *(end - 1) == '"' && begin + 1 < end) {
987 begin++; *(end - 1) = '\0';
988 }
989 return begin;
990 }
991
992 *state = end;
993 if (*begin == '"' && *(end - 1) == '"' && begin + 1 < end) {
994 begin++; *(end - 1) = '\0';
995 }
996 return begin;
997 }
998
999 /**
1000 * Get a list of configuration strings, free the result with
1001 * krb5_config_free_strings().
1002 *
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
1006 *
1007 * @return TRUE or FALSE
1008 *
1009 * @ingroup krb5_support
1010 */
1011
1012 KRB5_LIB_FUNCTION char ** KRB5_LIB_CALL
1013 krb5_config_vget_strings(krb5_context context,
1014 const krb5_config_section *c,
1015 va_list args)
1016 {
1017 char **strings = NULL;
1018 int nstr = 0;
1019 const krb5_config_binding *b = NULL;
1020 const char *p;
1021
1022 while((p = _krb5_config_vget_next(context, c, &b,
1023 krb5_config_string, args))) {
1024 char *tmp = strdup(p);
1025 char *pos = NULL;
1026 char *s;
1027 if(tmp == NULL)
1028 goto cleanup;
1029 s = next_component_string(tmp, " \t", &pos);
1030 while(s){
1031 char **tmp2 = realloc(strings, (nstr + 1) * sizeof(*strings));
1032 if(tmp2 == NULL) {
1033 free(tmp);
1034 goto cleanup;
1035 }
1036 strings = tmp2;
1037 strings[nstr] = strdup(s);
1038 nstr++;
1039 if(strings[nstr-1] == NULL) {
1040 free(tmp);
1041 goto cleanup;
1042 }
1043 s = next_component_string(NULL, " \t", &pos);
1044 }
1045 free(tmp);
1046 }
1047 if(nstr){
1048 char **tmp = realloc(strings, (nstr + 1) * sizeof(*strings));
1049 if(tmp == NULL)
1050 goto cleanup;
1051 strings = tmp;
1052 strings[nstr] = NULL;
1053 }
1054 return strings;
1055 cleanup:
1056 while(nstr--)
1057 free(strings[nstr]);
1058 free(strings);
1059 return NULL;
1060
1061 }
1062
1063 /**
1064 * Get a list of configuration strings, free the result with
1065 * krb5_config_free_strings().
1066 *
1067 * @param context A Kerberos 5 context.
1068 * @param c a configuration section, or NULL to use the section from context
1069 * @param ... a list of names, terminated with NULL.
1070 *
1071 * @return TRUE or FALSE
1072 *
1073 * @ingroup krb5_support
1074 */
1075
1076 KRB5_LIB_FUNCTION char** KRB5_LIB_CALL
1077 krb5_config_get_strings(krb5_context context,
1078 const krb5_config_section *c,
1079 ...)
1080 {
1081 va_list ap;
1082 char **ret;
1083 va_start(ap, c);
1084 ret = krb5_config_vget_strings(context, c, ap);
1085 va_end(ap);
1086 return ret;
1087 }
1088
1089 /**
1090 * Free the resulting strings from krb5_config-get_strings() and
1091 * krb5_config_vget_strings().
1092 *
1093 * @param strings strings to free
1094 *
1095 * @ingroup krb5_support
1096 */
1097
1098 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
1099 krb5_config_free_strings(char **strings)
1100 {
1101 char **s = strings;
1102 while(s && *s){
1103 free(*s);
1104 s++;
1105 }
1106 free(strings);
1107 }
1108
1109 /**
1110 * Like krb5_config_get_bool_default() but with a va_list list of
1111 * configuration selection.
1112 *
1113 * Configuration value to a boolean value, where yes/true and any
1114 * non-zero number means TRUE and other value is FALSE.
1115 *
1116 * @param context A Kerberos 5 context.
1117 * @param c a configuration section, or NULL to use the section from context
1118 * @param def_value the default value to return if no configuration
1119 * found in the database.
1120 * @param args a va_list of arguments
1121 *
1122 * @return TRUE or FALSE
1123 *
1124 * @ingroup krb5_support
1125 */
1126
1127 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1128 krb5_config_vget_bool_default (krb5_context context,
1129 const krb5_config_section *c,
1130 krb5_boolean def_value,
1131 va_list args)
1132 {
1133 const char *str;
1134 str = krb5_config_vget_string (context, c, args);
1135 if(str == NULL)
1136 return def_value;
1137 if(strcasecmp(str, "yes") == 0 ||
1138 strcasecmp(str, "true") == 0 ||
1139 atoi(str)) return TRUE;
1140 return FALSE;
1141 }
1142
1143 /**
1144 * krb5_config_get_bool() will convert the configuration
1145 * option value to a boolean value, where yes/true and any non-zero
1146 * number means TRUE and other value is FALSE.
1147 *
1148 * @param context A Kerberos 5 context.
1149 * @param c a configuration section, or NULL to use the section from context
1150 * @param args a va_list of arguments
1151 *
1152 * @return TRUE or FALSE
1153 *
1154 * @ingroup krb5_support
1155 */
1156
1157 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1158 krb5_config_vget_bool (krb5_context context,
1159 const krb5_config_section *c,
1160 va_list args)
1161 {
1162 return krb5_config_vget_bool_default (context, c, FALSE, args);
1163 }
1164
1165 /**
1166 * krb5_config_get_bool_default() will convert the configuration
1167 * option value to a boolean value, where yes/true and any non-zero
1168 * number means TRUE and other value is FALSE.
1169 *
1170 * @param context A Kerberos 5 context.
1171 * @param c a configuration section, or NULL to use the section from context
1172 * @param def_value the default value to return if no configuration
1173 * found in the database.
1174 * @param ... a list of names, terminated with NULL.
1175 *
1176 * @return TRUE or FALSE
1177 *
1178 * @ingroup krb5_support
1179 */
1180
1181 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1182 krb5_config_get_bool_default (krb5_context context,
1183 const krb5_config_section *c,
1184 krb5_boolean def_value,
1185 ...)
1186 {
1187 va_list ap;
1188 krb5_boolean ret;
1189 va_start(ap, def_value);
1190 ret = krb5_config_vget_bool_default(context, c, def_value, ap);
1191 va_end(ap);
1192 return ret;
1193 }
1194
1195 /**
1196 * Like krb5_config_get_bool() but with a va_list list of
1197 * configuration selection.
1198 *
1199 * Configuration value to a boolean value, where yes/true and any
1200 * non-zero number means TRUE and other value is FALSE.
1201 *
1202 * @param context A Kerberos 5 context.
1203 * @param c a configuration section, or NULL to use the section from context
1204 * @param ... a list of names, terminated with NULL.
1205 *
1206 * @return TRUE or FALSE
1207 *
1208 * @ingroup krb5_support
1209 */
1210
1211 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1212 krb5_config_get_bool (krb5_context context,
1213 const krb5_config_section *c,
1214 ...)
1215 {
1216 va_list ap;
1217 krb5_boolean ret;
1218 va_start(ap, c);
1219 ret = krb5_config_vget_bool (context, c, ap);
1220 va_end(ap);
1221 return ret;
1222 }
1223
1224 /**
1225 * Get the time from the configuration file using a relative time.
1226 *
1227 * Like krb5_config_get_time_default() but with a va_list list of
1228 * configuration selection.
1229 *
1230 * @param context A Kerberos 5 context.
1231 * @param c a configuration section, or NULL to use the section from context
1232 * @param def_value the default value to return if no configuration
1233 * found in the database.
1234 * @param args a va_list of arguments
1235 *
1236 * @return parsed the time (or def_value on parse error)
1237 *
1238 * @ingroup krb5_support
1239 */
1240
1241 KRB5_LIB_FUNCTION int KRB5_LIB_CALL
1242 krb5_config_vget_time_default (krb5_context context,
1243 const krb5_config_section *c,
1244 int def_value,
1245 va_list args)
1246 {
1247 const char *str;
1248 krb5_deltat t;
1249
1250 str = krb5_config_vget_string (context, c, args);
1251 if(str == NULL)
1252 return def_value;
1253 if (krb5_string_to_deltat(str, &t))
1254 return def_value;
1255 return t;
1256 }
1257
1258 /**
1259 * Get the time from the configuration file using a relative time, for example: 1h30s
1260 *
1261 * @param context A Kerberos 5 context.
1262 * @param c a configuration section, or NULL to use the section from context
1263 * @param args a va_list of arguments
1264 *
1265 * @return parsed the time or -1 on error
1266 *
1267 * @ingroup krb5_support
1268 */
1269
1270 KRB5_LIB_FUNCTION int KRB5_LIB_CALL
1271 krb5_config_vget_time (krb5_context context,
1272 const krb5_config_section *c,
1273 va_list args)
1274 {
1275 return krb5_config_vget_time_default (context, c, -1, args);
1276 }
1277
1278 /**
1279 * Get the time from the configuration file using a relative time, for example: 1h30s
1280 *
1281 * @param context A Kerberos 5 context.
1282 * @param c a configuration section, or NULL to use the section from context
1283 * @param def_value the default value to return if no configuration
1284 * found in the database.
1285 * @param ... a list of names, terminated with NULL.
1286 *
1287 * @return parsed the time (or def_value on parse error)
1288 *
1289 * @ingroup krb5_support
1290 */
1291
1292 KRB5_LIB_FUNCTION int KRB5_LIB_CALL
1293 krb5_config_get_time_default (krb5_context context,
1294 const krb5_config_section *c,
1295 int def_value,
1296 ...)
1297 {
1298 va_list ap;
1299 int ret;
1300 va_start(ap, def_value);
1301 ret = krb5_config_vget_time_default(context, c, def_value, ap);
1302 va_end(ap);
1303 return ret;
1304 }
1305
1306 /**
1307 * Get the time from the configuration file using a relative time, for example: 1h30s
1308 *
1309 * @param context A Kerberos 5 context.
1310 * @param c a configuration section, or NULL to use the section from context
1311 * @param ... a list of names, terminated with NULL.
1312 *
1313 * @return parsed the time or -1 on error
1314 *
1315 * @ingroup krb5_support
1316 */
1317
1318 KRB5_LIB_FUNCTION int KRB5_LIB_CALL
1319 krb5_config_get_time (krb5_context context,
1320 const krb5_config_section *c,
1321 ...)
1322 {
1323 va_list ap;
1324 int ret;
1325 va_start(ap, c);
1326 ret = krb5_config_vget_time (context, c, ap);
1327 va_end(ap);
1328 return ret;
1329 }
1330
1331
1332 KRB5_LIB_FUNCTION int KRB5_LIB_CALL
1333 krb5_config_vget_int_default (krb5_context context,
1334 const krb5_config_section *c,
1335 int def_value,
1336 va_list args)
1337 {
1338 const char *str;
1339 str = krb5_config_vget_string (context, c, args);
1340 if(str == NULL)
1341 return def_value;
1342 else {
1343 char *endptr;
1344 long l;
1345 l = strtol(str, &endptr, 0);
1346 if (endptr == str)
1347 return def_value;
1348 else
1349 return l;
1350 }
1351 }
1352
1353 KRB5_LIB_FUNCTION int KRB5_LIB_CALL
1354 krb5_config_vget_int (krb5_context context,
1355 const krb5_config_section *c,
1356 va_list args)
1357 {
1358 return krb5_config_vget_int_default (context, c, -1, args);
1359 }
1360
1361 KRB5_LIB_FUNCTION int KRB5_LIB_CALL
1362 krb5_config_get_int_default (krb5_context context,
1363 const krb5_config_section *c,
1364 int def_value,
1365 ...)
1366 {
1367 va_list ap;
1368 int ret;
1369 va_start(ap, def_value);
1370 ret = krb5_config_vget_int_default(context, c, def_value, ap);
1371 va_end(ap);
1372 return ret;
1373 }
1374
1375 KRB5_LIB_FUNCTION int KRB5_LIB_CALL
1376 krb5_config_get_int (krb5_context context,
1377 const krb5_config_section *c,
1378 ...)
1379 {
1380 va_list ap;
1381 int ret;
1382 va_start(ap, c);
1383 ret = krb5_config_vget_int (context, c, ap);
1384 va_end(ap);
1385 return ret;
1386 }
1387
1388
1389 #ifndef HEIMDAL_SMALLER
1390
1391 /**
1392 * Deprecated: configuration files are not strings
1393 *
1394 * @ingroup krb5_deprecated
1395 */
1396
1397 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1398 krb5_config_parse_string_multi(krb5_context context,
1399 const char *string,
1400 krb5_config_section **res)
1401 KRB5_DEPRECATED_FUNCTION("Use X instead")
1402 {
1403 const char *str;
1404 unsigned lineno = 0;
1405 krb5_error_code ret;
1406 struct fileptr f;
1407
1408 f.context = context;
1409 f.f = NULL;
1410 f.s = string;
1411
1412 ret = krb5_config_parse_debug (&f, res, &lineno, &str);
1413 if (ret) {
1414 krb5_set_error_message (context, ret, "%s:%u: %s",
1415 "<constant>", lineno, str);
1416 return ret;
1417 }
1418 return 0;
1419 }
1420
1421 #endif