Debian Codegraph @

lib/krb5/config_file.c

raw ◀ ▶
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 static int
341 is_absolute_path(const char *path)
342 {
343 /*
344 * An absolute path is one that refers to an explicit object
345 * without ambiguity.
346 */
347 #ifdef WIN32
348 size_t len = strlen(path);
349
350 /* UNC path is by definition absolute */
351 if (len > 2
352 && ISPATHSEP(path[0])
353 && ISPATHSEP(path[1]))
354 return 1;
355
356 /* A drive letter path might be absolute */
357 if (len > 3
358 && isalpha(path[0])
359 && path[1] == ':'
360 && ISPATHSEP(path[2]))
361 return 1;
362
363 /*
364 * if no drive letter but first char is a path
365 * separator then the drive letter must be obtained
366 * from the including file.
367 */
368 #else
369 /* UNIX is easy, first char '/' is absolute */
370 if (ISPATHSEP(path[0]))
371 return 1;
372 #endif
373 return 0;
374 }
375
376 /*
377 * Parse the config file `fname', generating the structures into `res'
378 * returning error messages in `err_message'
379 */
380
381 static krb5_error_code
382 krb5_config_parse_debug (struct fileptr *f,
383 krb5_config_section **res,
384 unsigned *lineno,
385 const char **err_message)
386 {
387 krb5_config_section *s = NULL;
388 krb5_config_binding *b = NULL;
389 char buf[KRB5_BUFSIZ];
390 krb5_error_code ret;
391
392 *lineno = 0;
393 *err_message = "";
394
395 while (config_fgets(buf, sizeof(buf), f) != NULL) {
396 char *p;
397
398 ++*lineno;
399 buf[strcspn(buf, "\r\n")] = '\0';
400 p = buf;
401 while(isspace((unsigned char)*p))
402 ++p;
403 if (*p == '#' || *p == ';')
404 continue;
•405 if (*p == '[') {
406 ret = parse_section(p, &s, res, err_message);
407 if (ret)
408 return ret;
409 b = NULL;
410 } else if (*p == '}') {
411 *err_message = "unmatched }";
412 return KRB5_CONFIG_BADFORMAT;
•413 } else if (strncmp(p, "include", sizeof("include") - 1) == 0 &&
414 isspace(p[sizeof("include") - 1])) {
415 p += sizeof("include");
416 while (isspace(*p))
417 p++;
•418 if (!is_absolute_path(p)) {
419 krb5_set_error_message(f->context, EINVAL,
420 "Configuration include path must be "
421 "absolute");
422 return EINVAL;
423 }
424 ret = krb5_config_parse_file_multi(f->context, p, res);
425 if (ret)
426 return ret;
427 } else if (strncmp(p, "includedir", sizeof("includedir") - 1) == 0 &&
428 isspace(p[sizeof("includedir") - 1])) {
429 p += sizeof("includedir");
430 while (isspace(*p))
431 p++;
•432 if (!is_absolute_path(p)) {
433 krb5_set_error_message(f->context, EINVAL,
434 "Configuration includedir path must be "
435 "absolute");
436 return EINVAL;
437 }
438 ret = krb5_config_parse_dir_multi(f->context, p, res);
439 if (ret)
440 return ret;
441 } else if(*p != '\0') {
442 if (s == NULL) {
443 *err_message = "binding before section";
444 return KRB5_CONFIG_BADFORMAT;
445 }
446 ret = parse_binding(f, lineno, p, &b, &s->u.list, err_message);
447 if (ret)
448 return ret;
449 }
450 }
451 return 0;
452 }
453
454 static int
455 is_plist_file(const char *fname)
456 {
457 size_t len = strlen(fname);
458 char suffix[] = ".plist";
459 if (len < sizeof(suffix))
460 return 0;
461 if (strcasecmp(&fname[len - (sizeof(suffix) - 1)], suffix) != 0)
462 return 0;
463 return 1;
464 }
465
•466 /**
467 * Parse configuration files in the given directory and add the result
468 * into res. Only files whose names consist only of alphanumeric
469 * characters, hyphen, and underscore, will be parsed, though files
470 * ending in ".conf" will also be parsed.
471 *
472 * This interface can be used to parse several configuration directories
473 * into one resulting krb5_config_section by calling it repeatably.
474 *
475 * @param context a Kerberos 5 context.
476 * @param dname a directory name to a Kerberos configuration file
477 * @param res the returned result, must be free with krb5_free_config_files().
478 * @return Return an error code or 0, see krb5_get_error_message().
479 *
480 * @ingroup krb5_support
481 */
482
483 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
484 krb5_config_parse_dir_multi(krb5_context context,
485 const char *dname,
486 krb5_config_section **res)
487 {
488 struct dirent *entry;
489 krb5_error_code ret;
490 DIR *d;
491
492 if ((d = opendir(dname)) == NULL)
493 return errno;
494
495 while ((entry = readdir(d)) != NULL) {
496 char *p = entry->d_name;
497 char *path;
498 int is_valid = 1;
499
500 while (*p) {
•501 /*
502 * Here be dragons. The call to krb5_config_parse_file_multi()
503 * below expands path tokens. Because of the limitations here
504 * on file naming, we can't have path tokens in the file name,
505 * so we're safe. Anyone changing this if condition here should
506 * be aware.
507 */
508 if (!isalnum(*p) && *p != '_' && *p != '-' &&
509 strcmp(p, ".conf") != 0) {
510 is_valid = 0;
511 break;
512 }
513 p++;
514 }
515 if (!is_valid)
516 continue;
517
518 if (asprintf(&path, "%s/%s", dname, entry->d_name) == -1 ||
•519 path == NULL) {
520 (void) closedir(d);
521 return krb5_enomem(context);
•522 }
523 ret = krb5_config_parse_file_multi(context, path, res);
524 free(path);
•525 if (ret == ENOMEM) {
526 (void) closedir(d);
527 return krb5_enomem(context);;
•528 }
529 /* Ignore malformed config files so we don't lock out admins, etc... */
530 }
531 (void) closedir(d);
532 return 0;
533 }
534
535 /**
536 * Parse a configuration file and add the result into res. This
537 * interface can be used to parse several configuration files into one
538 * resulting krb5_config_section by calling it repeatably.
539 *
540 * @param context a Kerberos 5 context.
541 * @param fname a file name to a Kerberos configuration file
542 * @param res the returned result, must be free with krb5_free_config_files().
543 * @return Return an error code or 0, see krb5_get_error_message().
544 *
545 * @ingroup krb5_support
546 */
547
548 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
549 krb5_config_parse_file_multi (krb5_context context,
550 const char *fname,
551 krb5_config_section **res)
552 {
553 const char *str;
554 char *newfname = NULL;
555 unsigned lineno = 0;
556 krb5_error_code ret;
557 struct fileptr f;
•558 struct stat st;
•559
560 if (context->config_include_depth > 5) {
561 krb5_warnx(context, "Maximum config file include depth reached; "
562 "not including %s", fname);
563 return 0;
564 }
565 context->config_include_depth++;
566
567 /**
568 * If the fname starts with "~/" parse configuration file in the
569 * current users home directory. The behavior can be disabled and
570 * enabled by calling krb5_set_home_dir_access().
571 */
572 if (ISTILDE(fname[0]) && ISPATHSEP(fname[1])) {
573 #ifndef KRB5_USE_PATH_TOKENS
574 const char *home = NULL;
575
576 if (!_krb5_homedir_access(context)) {
•577 context->config_include_depth--;
578 krb5_set_error_message(context, EPERM,
579 "Access to home directory not allowed");
580 return EPERM;
581 }
582
583 if(!issuid())
584 home = getenv("HOME");
585
586 if (home == NULL) {
587 struct passwd *pw = getpwuid(getuid());
588 if(pw != NULL)
589 home = pw->pw_dir;
590 }
591 if (home) {
592 int aret;
593
594 aret = asprintf(&newfname, "%s%s", home, &fname[1]);
•595 if (aret == -1 || newfname == NULL) {
596 context->config_include_depth--;
597 return krb5_enomem(context);
•598 }
599 fname = newfname;
600 }
601 #else /* KRB5_USE_PATH_TOKENS */
602 if (asprintf(&newfname, "%%{USERCONFIG}%s", &fname[1]) < 0 ||
•603 newfname == NULL) {
604 context->config_include_depth--;
605 return krb5_enomem(context);
•606 }
607 fname = newfname;
608 #endif
609 }
610
611 if (is_plist_file(fname)) {
•612 context->config_include_depth--;
613 #ifdef __APPLE__
•614 ret = parse_plist_config(context, fname, res);
615 if (ret) {
616 krb5_set_error_message(context, ret,
617 "Failed to parse plist %s", fname);
•618 free(newfname);
619 return ret;
620 }
621 #else
622 krb5_set_error_message(context, ENOENT,
623 "no support for plist configuration files");
624 return ENOENT;
625 #endif
626 } else {
627 #ifdef KRB5_USE_PATH_TOKENS
628 char * exp_fname = NULL;
629
•630 /*
631 * Note that krb5_config_parse_dir_multi() doesn't want tokens
•632 * expanded here, but it happens to limit the names of files to
633 * include such that there can be no tokens to expand. Don't
634 * add token expansion for tokens using _, say.
635 */
636 ret = _krb5_expand_path_tokens(context, fname, 1, &exp_fname);
637 if (ret) {
638 context->config_include_depth--;
•639 free(newfname);
640 return ret;
641 }
642
•643 free(newfname);
644 fname = newfname = exp_fname;
645 #endif
646
•647 f.context = context;
648 f.f = fopen(fname, "r");
649 f.s = NULL;
•650 if (f.f == NULL || fstat(fileno(f.f), &st) == -1) {
651 if (f.f != NULL)
652 (void) fclose(f.f);
653 context->config_include_depth--;
654 ret = errno;
•655 krb5_set_error_message(context, ret, "open or stat %s: %s",
656 fname, strerror(ret));
•657 free(newfname);
658 return ret;
659 }
660
•661 if (!S_ISREG(st.st_mode)) {
662 (void) fclose(f.f);
663 context->config_include_depth--;
664 krb5_set_error_message(context, EISDIR, "not a regular file %s: %s",
665 fname, strerror(EISDIR));
•666 free(newfname);
667 return EISDIR;
668 }
669
670 ret = krb5_config_parse_debug (&f, res, &lineno, &str);
671 context->config_include_depth--;
672 fclose(f.f);
673 if (ret) {
674 krb5_set_error_message (context, ret, "%s:%u: %s",
675 fname, lineno, str);
•676 free(newfname);
677 return ret;
678 }
679 }
680 return 0;
681 }
682
683 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
684 krb5_config_parse_file (krb5_context context,
685 const char *fname,
686 krb5_config_section **res)
687 {
688 *res = NULL;
689 return krb5_config_parse_file_multi(context, fname, res);
690 }
691
692 static void
693 free_binding (krb5_context context, krb5_config_binding *b)
694 {
695 krb5_config_binding *next_b;
696
697 while (b) {
698 free (b->name);
699 if (b->type == krb5_config_string)
700 free (b->u.string);
701 else if (b->type == krb5_config_list)
702 free_binding (context, b->u.list);
703 else
704 krb5_abortx(context, "unknown binding type (%d) in free_binding",
705 b->type);
706 next_b = b->next;
707 free (b);
708 b = next_b;
709 }
710 }
711
712 /**
713 * Free configuration file section, the result of
714 * krb5_config_parse_file() and krb5_config_parse_file_multi().
715 *
716 * @param context A Kerberos 5 context
717 * @param s the configuration section to free
718 *
719 * @return returns 0 on successes, otherwise an error code, see
720 * krb5_get_error_message()
721 *
722 * @ingroup krb5_support
723 */
724
725 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
726 krb5_config_file_free (krb5_context context, krb5_config_section *s)
727 {
728 free_binding (context, s);
729 return 0;
730 }
731
732 #ifndef HEIMDAL_SMALLER
733
734 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
735 _krb5_config_copy(krb5_context context,
736 krb5_config_section *c,
737 krb5_config_section **head)
738 {
739 krb5_config_binding *d, *previous = NULL;
740
741 *head = NULL;
742
743 while (c) {
744 d = calloc(1, sizeof(*d));
745
746 if (*head == NULL)
747 *head = d;
748
749 d->name = strdup(c->name);
750 d->type = c->type;
751 if (d->type == krb5_config_string)
752 d->u.string = strdup(c->u.string);
753 else if (d->type == krb5_config_list)
754 _krb5_config_copy (context, c->u.list, &d->u.list);
755 else
756 krb5_abortx(context,
757 "unknown binding type (%d) in krb5_config_copy",
758 d->type);
759 if (previous)
760 previous->next = d;
761
762 previous = d;
763 c = c->next;
764 }
765 return 0;
766 }
767
768 #endif /* HEIMDAL_SMALLER */
769
770 KRB5_LIB_FUNCTION const void * KRB5_LIB_CALL
771 _krb5_config_get_next (krb5_context context,
772 const krb5_config_section *c,
773 const krb5_config_binding **pointer,
774 int type,
775 ...)
776 {
777 const char *ret;
778 va_list args;
779
780 va_start(args, type);
781 ret = _krb5_config_vget_next (context, c, pointer, type, args);
782 va_end(args);
783 return ret;
784 }
785
786 static const void *
787 vget_next(krb5_context context,
788 const krb5_config_binding *b,
789 const krb5_config_binding **pointer,
790 int type,
791 const char *name,
792 va_list args)
793 {
794 const char *p = va_arg(args, const char *);
795 while(b != NULL) {
796 if(strcmp(b->name, name) == 0) {
797 if(b->type == (unsigned)type && p == NULL) {
798 *pointer = b;
799 return b->u.generic;
800 } else if(b->type == krb5_config_list && p != NULL) {
801 return vget_next(context, b->u.list, pointer, type, p, args);
802 }
803 }
804 b = b->next;
805 }
806 return NULL;
807 }
808
809 KRB5_LIB_FUNCTION const void * KRB5_LIB_CALL
810 _krb5_config_vget_next (krb5_context context,
811 const krb5_config_section *c,
812 const krb5_config_binding **pointer,
813 int type,
814 va_list args)
815 {
816 const krb5_config_binding *b;
817 const char *p;
818
819 if(c == NULL)
820 c = context->cf;
821
822 if (c == NULL)
823 return NULL;
824
825 if (*pointer == NULL) {
826 /* first time here, walk down the tree looking for the right
827 section */
828 p = va_arg(args, const char *);
829 if (p == NULL)
830 return NULL;
831 return vget_next(context, c, pointer, type, p, args);
832 }
833
834 /* we were called again, so just look for more entries with the
835 same name and type */
836 for (b = (*pointer)->next; b != NULL; b = b->next) {
837 if(strcmp(b->name, (*pointer)->name) == 0 && b->type == (unsigned)type) {
838 *pointer = b;
839 return b->u.generic;
840 }
841 }
842 return NULL;
843 }
844
845 KRB5_LIB_FUNCTION const void * KRB5_LIB_CALL
846 _krb5_config_get (krb5_context context,
847 const krb5_config_section *c,
848 int type,
849 ...)
850 {
851 const void *ret;
852 va_list args;
853
854 va_start(args, type);
855 ret = _krb5_config_vget (context, c, type, args);
856 va_end(args);
857 return ret;
858 }
859
860
861 KRB5_LIB_FUNCTION const void * KRB5_LIB_CALL
862 _krb5_config_vget (krb5_context context,
863 const krb5_config_section *c,
864 int type,
865 va_list args)
866 {
867 const krb5_config_binding *foo = NULL;
868
869 return _krb5_config_vget_next (context, c, &foo, type, args);
870 }
871
872 /**
873 * Get a list of configuration binding list for more processing
874 *
875 * @param context A Kerberos 5 context.
876 * @param c a configuration section, or NULL to use the section from context
877 * @param ... a list of names, terminated with NULL.
878 *
879 * @return NULL if configuration list is not found, a list otherwise
880 *
881 * @ingroup krb5_support
882 */
883
884 KRB5_LIB_FUNCTION const krb5_config_binding * KRB5_LIB_CALL
885 krb5_config_get_list (krb5_context context,
886 const krb5_config_section *c,
887 ...)
888 {
889 const krb5_config_binding *ret;
890 va_list args;
891
892 va_start(args, c);
893 ret = krb5_config_vget_list (context, c, args);
894 va_end(args);
895 return ret;
896 }
897
898 /**
899 * Get a list of configuration binding list for more processing
900 *
901 * @param context A Kerberos 5 context.
902 * @param c a configuration section, or NULL to use the section from context
903 * @param args a va_list of arguments
904 *
905 * @return NULL if configuration list is not found, a list otherwise
906 *
907 * @ingroup krb5_support
908 */
909
910 KRB5_LIB_FUNCTION const krb5_config_binding * KRB5_LIB_CALL
911 krb5_config_vget_list (krb5_context context,
912 const krb5_config_section *c,
913 va_list args)
914 {
915 return _krb5_config_vget (context, c, krb5_config_list, args);
916 }
917
918 /**
919 * Returns a "const char *" to a string in the configuration database.
920 * The string may not be valid after a reload of the configuration
921 * database so a caller should make a local copy if it needs to keep
922 * the string.
923 *
924 * @param context A Kerberos 5 context.
925 * @param c a configuration section, or NULL to use the section from context
926 * @param ... a list of names, terminated with NULL.
927 *
928 * @return NULL if configuration string not found, a string otherwise
929 *
930 * @ingroup krb5_support
931 */
932
933 KRB5_LIB_FUNCTION const char* KRB5_LIB_CALL
934 krb5_config_get_string (krb5_context context,
935 const krb5_config_section *c,
936 ...)
937 {
938 const char *ret;
939 va_list args;
940
941 va_start(args, c);
942 ret = krb5_config_vget_string (context, c, args);
943 va_end(args);
944 return ret;
945 }
946
947 /**
948 * Like krb5_config_get_string(), but uses a va_list instead of ...
949 *
950 * @param context A Kerberos 5 context.
951 * @param c a configuration section, or NULL to use the section from context
952 * @param args a va_list of arguments
953 *
954 * @return NULL if configuration string not found, a string otherwise
955 *
956 * @ingroup krb5_support
957 */
958
959 KRB5_LIB_FUNCTION const char* KRB5_LIB_CALL
960 krb5_config_vget_string (krb5_context context,
961 const krb5_config_section *c,
962 va_list args)
963 {
964 return _krb5_config_vget (context, c, krb5_config_string, args);
965 }
966
967 /**
968 * Like krb5_config_vget_string(), but instead of returning NULL,
969 * instead return a default value.
970 *
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
976 *
977 * @return a configuration string
978 *
979 * @ingroup krb5_support
980 */
981
982 KRB5_LIB_FUNCTION const char* KRB5_LIB_CALL
983 krb5_config_vget_string_default (krb5_context context,
984 const krb5_config_section *c,
985 const char *def_value,
986 va_list args)
987 {
988 const char *ret;
989
990 ret = krb5_config_vget_string (context, c, args);
991 if (ret == NULL)
992 ret = def_value;
993 return ret;
994 }
995
996 /**
997 * Like krb5_config_get_string(), but instead of returning NULL,
998 * instead return a default value.
999 *
1000 * @param context A Kerberos 5 context.
1001 * @param c a configuration section, or NULL to use the section from context
1002 * @param def_value the default value to return if no configuration
1003 * found in the database.
1004 * @param ... a list of names, terminated with NULL.
1005 *
1006 * @return a configuration string
1007 *
1008 * @ingroup krb5_support
1009 */
1010
1011 KRB5_LIB_FUNCTION const char* KRB5_LIB_CALL
1012 krb5_config_get_string_default (krb5_context context,
1013 const krb5_config_section *c,
1014 const char *def_value,
1015 ...)
1016 {
1017 const char *ret;
1018 va_list args;
1019
1020 va_start(args, def_value);
1021 ret = krb5_config_vget_string_default (context, c, def_value, args);
1022 va_end(args);
1023 return ret;
1024 }
1025
1026 static char *
1027 next_component_string(char * begin, const char * delims, char **state)
1028 {
1029 char * end;
1030
1031 if (begin == NULL)
1032 begin = *state;
1033
1034 if (*begin == '\0')
1035 return NULL;
1036
1037 end = begin;
1038 while (*end == '"') {
1039 char * t = strchr(end + 1, '"');
1040
1041 if (t)
1042 end = ++t;
1043 else
1044 end += strlen(end);
1045 }
1046
1047 if (*end != '\0') {
1048 size_t pos;
1049
1050 pos = strcspn(end, delims);
1051 end = end + pos;
1052 }
1053
1054 if (*end != '\0') {
1055 *end = '\0';
1056 *state = end + 1;
1057 if (*begin == '"' && *(end - 1) == '"' && begin + 1 < end) {
1058 begin++; *(end - 1) = '\0';
1059 }
1060 return begin;
1061 }
1062
1063 *state = end;
1064 if (*begin == '"' && *(end - 1) == '"' && begin + 1 < end) {
1065 begin++; *(end - 1) = '\0';
1066 }
1067 return begin;
1068 }
1069
1070 /**
1071 * Get a list of configuration strings, free the result with
1072 * krb5_config_free_strings().
1073 *
1074 * @param context A Kerberos 5 context.
1075 * @param c a configuration section, or NULL to use the section from context
1076 * @param args a va_list of arguments
1077 *
1078 * @return TRUE or FALSE
1079 *
1080 * @ingroup krb5_support
1081 */
1082
1083 KRB5_LIB_FUNCTION char ** KRB5_LIB_CALL
1084 krb5_config_vget_strings(krb5_context context,
1085 const krb5_config_section *c,
1086 va_list args)
1087 {
1088 char **strings = NULL;
1089 int nstr = 0;
1090 const krb5_config_binding *b = NULL;
1091 const char *p;
1092
1093 while((p = _krb5_config_vget_next(context, c, &b,
1094 krb5_config_string, args))) {
1095 char *tmp = strdup(p);
1096 char *pos = NULL;
1097 char *s;
1098 if(tmp == NULL)
1099 goto cleanup;
1100 s = next_component_string(tmp, " \t", &pos);
1101 while(s){
1102 char **tmp2 = realloc(strings, (nstr + 1) * sizeof(*strings));
1103 if(tmp2 == NULL) {
1104 free(tmp);
1105 goto cleanup;
1106 }
1107 strings = tmp2;
1108 strings[nstr] = strdup(s);
1109 nstr++;
1110 if(strings[nstr-1] == NULL) {
1111 free(tmp);
1112 goto cleanup;
1113 }
1114 s = next_component_string(NULL, " \t", &pos);
1115 }
1116 free(tmp);
1117 }
1118 if(nstr){
1119 char **tmp = realloc(strings, (nstr + 1) * sizeof(*strings));
1120 if(tmp == NULL)
1121 goto cleanup;
1122 strings = tmp;
1123 strings[nstr] = NULL;
1124 }
1125 return strings;
1126 cleanup:
1127 while(nstr--)
1128 free(strings[nstr]);
1129 free(strings);
1130 return NULL;
1131
1132 }
1133
1134 /**
1135 * Get a list of configuration strings, free the result with
1136 * krb5_config_free_strings().
1137 *
1138 * @param context A Kerberos 5 context.
1139 * @param c a configuration section, or NULL to use the section from context
1140 * @param ... a list of names, terminated with NULL.
1141 *
1142 * @return TRUE or FALSE
1143 *
1144 * @ingroup krb5_support
1145 */
1146
1147 KRB5_LIB_FUNCTION char** KRB5_LIB_CALL
1148 krb5_config_get_strings(krb5_context context,
1149 const krb5_config_section *c,
1150 ...)
1151 {
1152 va_list ap;
1153 char **ret;
1154 va_start(ap, c);
1155 ret = krb5_config_vget_strings(context, c, ap);
1156 va_end(ap);
1157 return ret;
1158 }
1159
1160 /**
1161 * Free the resulting strings from krb5_config-get_strings() and
1162 * krb5_config_vget_strings().
1163 *
1164 * @param strings strings to free
1165 *
1166 * @ingroup krb5_support
1167 */
1168
1169 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
1170 krb5_config_free_strings(char **strings)
1171 {
1172 char **s = strings;
1173 while(s && *s){
1174 free(*s);
1175 s++;
1176 }
1177 free(strings);
1178 }
1179
1180 /**
1181 * Like krb5_config_get_bool_default() but with a va_list list of
1182 * configuration selection.
1183 *
1184 * Configuration value to a boolean value, where yes/true and any
1185 * non-zero number means TRUE and other value is FALSE.
1186 *
1187 * @param context A Kerberos 5 context.
1188 * @param c a configuration section, or NULL to use the section from context
1189 * @param def_value the default value to return if no configuration
1190 * found in the database.
1191 * @param args a va_list of arguments
1192 *
1193 * @return TRUE or FALSE
1194 *
1195 * @ingroup krb5_support
1196 */
1197
1198 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1199 krb5_config_vget_bool_default (krb5_context context,
1200 const krb5_config_section *c,
1201 krb5_boolean def_value,
1202 va_list args)
1203 {
1204 const char *str;
1205 str = krb5_config_vget_string (context, c, args);
1206 if(str == NULL)
1207 return def_value;
1208 if(strcasecmp(str, "yes") == 0 ||
1209 strcasecmp(str, "true") == 0 ||
1210 atoi(str)) return TRUE;
1211 return FALSE;
1212 }
1213
1214 /**
1215 * krb5_config_get_bool() will convert the configuration
1216 * option value to a boolean value, where yes/true and any non-zero
1217 * number means TRUE and other value is FALSE.
1218 *
1219 * @param context A Kerberos 5 context.
1220 * @param c a configuration section, or NULL to use the section from context
1221 * @param args a va_list of arguments
1222 *
1223 * @return TRUE or FALSE
1224 *
1225 * @ingroup krb5_support
1226 */
1227
1228 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1229 krb5_config_vget_bool (krb5_context context,
1230 const krb5_config_section *c,
1231 va_list args)
1232 {
1233 return krb5_config_vget_bool_default (context, c, FALSE, args);
1234 }
1235
1236 /**
1237 * krb5_config_get_bool_default() will convert the configuration
1238 * option value to a boolean value, where yes/true and any non-zero
1239 * number means TRUE and other value is FALSE.
1240 *
1241 * @param context A Kerberos 5 context.
1242 * @param c a configuration section, or NULL to use the section from context
1243 * @param def_value the default value to return if no configuration
1244 * found in the database.
1245 * @param ... a list of names, terminated with NULL.
1246 *
1247 * @return TRUE or FALSE
1248 *
1249 * @ingroup krb5_support
1250 */
1251
1252 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1253 krb5_config_get_bool_default (krb5_context context,
1254 const krb5_config_section *c,
1255 krb5_boolean def_value,
1256 ...)
1257 {
1258 va_list ap;
1259 krb5_boolean ret;
1260 va_start(ap, def_value);
1261 ret = krb5_config_vget_bool_default(context, c, def_value, ap);
1262 va_end(ap);
1263 return ret;
1264 }
1265
1266 /**
1267 * Like krb5_config_get_bool() but with a va_list list of
1268 * configuration selection.
1269 *
1270 * Configuration value to a boolean value, where yes/true and any
1271 * non-zero number means TRUE and other value is FALSE.
1272 *
1273 * @param context A Kerberos 5 context.
1274 * @param c a configuration section, or NULL to use the section from context
1275 * @param ... a list of names, terminated with NULL.
1276 *
1277 * @return TRUE or FALSE
1278 *
1279 * @ingroup krb5_support
1280 */
1281
1282 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
1283 krb5_config_get_bool (krb5_context context,
1284 const krb5_config_section *c,
1285 ...)
1286 {
1287 va_list ap;
1288 krb5_boolean ret;
1289 va_start(ap, c);
1290 ret = krb5_config_vget_bool (context, c, ap);
1291 va_end(ap);
1292 return ret;
1293 }
1294
1295 /**
1296 * Get the time from the configuration file using a relative time.
1297 *
1298 * Like krb5_config_get_time_default() but with a va_list list of
1299 * configuration selection.
1300 *
1301 * @param context A Kerberos 5 context.
1302 * @param c a configuration section, or NULL to use the section from context
1303 * @param def_value the default value to return if no configuration
1304 * found in the database.
1305 * @param args a va_list of arguments
1306 *
1307 * @return parsed the time (or def_value on parse error)
1308 *
1309 * @ingroup krb5_support
1310 */
1311
1312 KRB5_LIB_FUNCTION int KRB5_LIB_CALL
1313 krb5_config_vget_time_default (krb5_context context,
1314 const krb5_config_section *c,
1315 int def_value,
1316 va_list args)
1317 {
1318 const char *str;
1319 krb5_deltat t;
1320
1321 str = krb5_config_vget_string (context, c, args);
1322 if(str == NULL)
1323 return def_value;
1324 if (krb5_string_to_deltat(str, &t))
1325 return def_value;
1326 return t;
1327 }
1328
1329 /**
1330 * Get the time from the configuration file using a relative time, for example: 1h30s
1331 *
1332 * @param context A Kerberos 5 context.
1333 * @param c a configuration section, or NULL to use the section from context
1334 * @param args a va_list of arguments
1335 *
1336 * @return parsed the time or -1 on error
1337 *
1338 * @ingroup krb5_support
1339 */
1340
1341 KRB5_LIB_FUNCTION int KRB5_LIB_CALL
1342 krb5_config_vget_time (krb5_context context,
1343 const krb5_config_section *c,
1344 va_list args)
1345 {
1346 return krb5_config_vget_time_default (context, c, -1, args);
1347 }
1348
1349 /**
1350 * Get the time from the configuration file using a relative time, for example: 1h30s
1351 *
1352 * @param context A Kerberos 5 context.
1353 * @param c a configuration section, or NULL to use the section from context
1354 * @param def_value the default value to return if no configuration
1355 * found in the database.
1356 * @param ... a list of names, terminated with NULL.
1357 *
1358 * @return parsed the time (or def_value on parse error)
1359 *
1360 * @ingroup krb5_support
1361 */
1362
1363 KRB5_LIB_FUNCTION int KRB5_LIB_CALL
1364 krb5_config_get_time_default (krb5_context context,
1365 const krb5_config_section *c,
1366 int def_value,
1367 ...)
1368 {
1369 va_list ap;
1370 int ret;
1371 va_start(ap, def_value);
1372 ret = krb5_config_vget_time_default(context, c, def_value, ap);
1373 va_end(ap);
1374 return ret;
1375 }
1376
1377 /**
1378 * Get the time from the configuration file using a relative time, for example: 1h30s
1379 *
1380 * @param context A Kerberos 5 context.
1381 * @param c a configuration section, or NULL to use the section from context
1382 * @param ... a list of names, terminated with NULL.
1383 *
1384 * @return parsed the time or -1 on error
1385 *
1386 * @ingroup krb5_support
1387 */
1388
1389 KRB5_LIB_FUNCTION int KRB5_LIB_CALL
1390 krb5_config_get_time (krb5_context context,
1391 const krb5_config_section *c,
1392 ...)
1393 {
1394 va_list ap;
1395 int ret;
1396 va_start(ap, c);
1397 ret = krb5_config_vget_time (context, c, ap);
1398 va_end(ap);
1399 return ret;
1400 }
1401
1402
1403 KRB5_LIB_FUNCTION int KRB5_LIB_CALL
1404 krb5_config_vget_int_default (krb5_context context,
1405 const krb5_config_section *c,
1406 int def_value,
1407 va_list args)
1408 {
1409 const char *str;
1410 str = krb5_config_vget_string (context, c, args);
1411 if(str == NULL)
1412 return def_value;
1413 else {
1414 char *endptr;
1415 long l;
1416 l = strtol(str, &endptr, 0);
1417 if (endptr == str)
1418 return def_value;
1419 else
1420 return l;
1421 }
1422 }
1423
1424 KRB5_LIB_FUNCTION int KRB5_LIB_CALL
1425 krb5_config_vget_int (krb5_context context,
1426 const krb5_config_section *c,
1427 va_list args)
1428 {
1429 return krb5_config_vget_int_default (context, c, -1, args);
1430 }
1431
1432 KRB5_LIB_FUNCTION int KRB5_LIB_CALL
1433 krb5_config_get_int_default (krb5_context context,
1434 const krb5_config_section *c,
1435 int def_value,
1436 ...)
1437 {
1438 va_list ap;
1439 int ret;
1440 va_start(ap, def_value);
1441 ret = krb5_config_vget_int_default(context, c, def_value, ap);
1442 va_end(ap);
1443 return ret;
1444 }
1445
1446 KRB5_LIB_FUNCTION int KRB5_LIB_CALL
1447 krb5_config_get_int (krb5_context context,
1448 const krb5_config_section *c,
1449 ...)
1450 {
1451 va_list ap;
1452 int ret;
1453 va_start(ap, c);
1454 ret = krb5_config_vget_int (context, c, ap);
1455 va_end(ap);
1456 return ret;
1457 }
1458
1459
1460 #ifndef HEIMDAL_SMALLER
1461
1462 /**
1463 * Deprecated: configuration files are not strings
1464 *
1465 * @ingroup krb5_deprecated
1466 */
1467
1468 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1469 krb5_config_parse_string_multi(krb5_context context,
1470 const char *string,
1471 krb5_config_section **res)
1472 KRB5_DEPRECATED_FUNCTION("Use X instead")
1473 {
1474 const char *str;
1475 unsigned lineno = 0;
1476 krb5_error_code ret;
1477 struct fileptr f;
•1478
1479 f.context = context;
1480 f.f = NULL;
1481 f.s = string;
1482
1483 ret = krb5_config_parse_debug (&f, res, &lineno, &str);
1484 if (ret) {
1485 krb5_set_error_message (context, ret, "%s:%u: %s",
1486 "<constant>", lineno, str);
1487 return ret;
1488 }
1489 return 0;
1490 }
1491
1492 #endif