No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / krb5 / config_file.c
blob9c5b9d595416ef54444b261206dfaaa95bae9067
1 /*
2 * Copyright (c) 1997 - 2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "krb5_locl.h"
35 __RCSID("$Heimdal: config_file.c 19213 2006-12-04 23:36:36Z lha $"
36 "$NetBSD$");
38 #ifndef HAVE_NETINFO
40 /* Gaah! I want a portable funopen */
41 struct fileptr {
42 const char *s;
43 FILE *f;
46 static char *
47 config_fgets(char *str, size_t len, struct fileptr *ptr)
49 /* XXX this is not correct, in that they don't do the same if the
50 line is longer than len */
51 if(ptr->f != NULL)
52 return fgets(str, len, ptr->f);
53 else {
54 /* this is almost strsep_copy */
55 const char *p;
56 ssize_t l;
57 if(*ptr->s == '\0')
58 return NULL;
59 p = ptr->s + strcspn(ptr->s, "\n");
60 if(*p == '\n')
61 p++;
62 l = min(len, p - ptr->s);
63 if(len > 0) {
64 memcpy(str, ptr->s, l);
65 str[l] = '\0';
67 ptr->s = p;
68 return str;
72 static krb5_error_code parse_section(char *p, krb5_config_section **s,
73 krb5_config_section **res,
74 const char **error_message);
75 static krb5_error_code parse_binding(struct fileptr *f, unsigned *lineno, char *p,
76 krb5_config_binding **b,
77 krb5_config_binding **parent,
78 const char **error_message);
79 static krb5_error_code parse_list(struct fileptr *f, unsigned *lineno,
80 krb5_config_binding **parent,
81 const char **error_message);
83 static krb5_config_section *
84 get_entry(krb5_config_section **parent, const char *name, int type)
86 krb5_config_section **q;
88 for(q = parent; *q != NULL; q = &(*q)->next)
89 if(type == krb5_config_list &&
90 type == (*q)->type &&
91 strcmp(name, (*q)->name) == 0)
92 return *q;
93 *q = calloc(1, sizeof(**q));
94 if(*q == NULL)
95 return NULL;
96 (*q)->name = strdup(name);
97 (*q)->type = type;
98 if((*q)->name == NULL) {
99 free(*q);
100 *q = NULL;
101 return NULL;
103 return *q;
107 * Parse a section:
109 * [section]
110 * foo = bar
111 * b = {
114 * ...
116 * starting at the line in `p', storing the resulting structure in
117 * `s' and hooking it into `parent'.
118 * Store the error message in `error_message'.
121 static krb5_error_code
122 parse_section(char *p, krb5_config_section **s, krb5_config_section **parent,
123 const char **error_message)
125 char *p1;
126 krb5_config_section *tmp;
128 p1 = strchr (p + 1, ']');
129 if (p1 == NULL) {
130 *error_message = "missing ]";
131 return KRB5_CONFIG_BADFORMAT;
133 *p1 = '\0';
134 tmp = get_entry(parent, p + 1, krb5_config_list);
135 if(tmp == NULL) {
136 *error_message = "out of memory";
137 return KRB5_CONFIG_BADFORMAT;
139 *s = tmp;
140 return 0;
144 * Parse a brace-enclosed list from `f', hooking in the structure at
145 * `parent'.
146 * Store the error message in `error_message'.
149 static krb5_error_code
150 parse_list(struct fileptr *f, unsigned *lineno, krb5_config_binding **parent,
151 const char **error_message)
153 char buf[BUFSIZ];
154 krb5_error_code ret;
155 krb5_config_binding *b = NULL;
156 unsigned beg_lineno = *lineno;
158 while(config_fgets(buf, sizeof(buf), f) != NULL) {
159 char *p;
161 ++*lineno;
162 buf[strcspn(buf, "\r\n")] = '\0';
163 p = buf;
164 while(isspace((unsigned char)*p))
165 ++p;
166 if (*p == '#' || *p == ';' || *p == '\0')
167 continue;
168 while(isspace((unsigned char)*p))
169 ++p;
170 if (*p == '}')
171 return 0;
172 if (*p == '\0')
173 continue;
174 ret = parse_binding (f, lineno, p, &b, parent, error_message);
175 if (ret)
176 return ret;
178 *lineno = beg_lineno;
179 *error_message = "unclosed {";
180 return KRB5_CONFIG_BADFORMAT;
187 static krb5_error_code
188 parse_binding(struct fileptr *f, unsigned *lineno, char *p,
189 krb5_config_binding **b, krb5_config_binding **parent,
190 const char **error_message)
192 krb5_config_binding *tmp;
193 char *p1, *p2;
194 krb5_error_code ret = 0;
196 p1 = p;
197 while (*p && *p != '=' && !isspace((unsigned char)*p))
198 ++p;
199 if (*p == '\0') {
200 *error_message = "missing =";
201 return KRB5_CONFIG_BADFORMAT;
203 p2 = p;
204 while (isspace((unsigned char)*p))
205 ++p;
206 if (*p != '=') {
207 *error_message = "missing =";
208 return KRB5_CONFIG_BADFORMAT;
210 ++p;
211 while(isspace((unsigned char)*p))
212 ++p;
213 *p2 = '\0';
214 if (*p == '{') {
215 tmp = get_entry(parent, p1, krb5_config_list);
216 if (tmp == NULL) {
217 *error_message = "out of memory";
218 return KRB5_CONFIG_BADFORMAT;
220 ret = parse_list (f, lineno, &tmp->u.list, error_message);
221 } else {
222 tmp = get_entry(parent, p1, krb5_config_string);
223 if (tmp == NULL) {
224 *error_message = "out of memory";
225 return KRB5_CONFIG_BADFORMAT;
227 p1 = p;
228 p = p1 + strlen(p1);
229 while(p > p1 && isspace((unsigned char)*(p-1)))
230 --p;
231 *p = '\0';
232 tmp->u.string = strdup(p1);
234 *b = tmp;
235 return ret;
239 * Parse the config file `fname', generating the structures into `res'
240 * returning error messages in `error_message'
243 static krb5_error_code
244 krb5_config_parse_debug (struct fileptr *f,
245 krb5_config_section **res,
246 unsigned *lineno,
247 const char **error_message)
249 krb5_config_section *s = NULL;
250 krb5_config_binding *b = NULL;
251 char buf[BUFSIZ];
252 krb5_error_code ret;
254 while (config_fgets(buf, sizeof(buf), f) != NULL) {
255 char *p;
257 ++*lineno;
258 buf[strcspn(buf, "\r\n")] = '\0';
259 p = buf;
260 while(isspace((unsigned char)*p))
261 ++p;
262 if (*p == '#' || *p == ';')
263 continue;
264 if (*p == '[') {
265 ret = parse_section(p, &s, res, error_message);
266 if (ret)
267 return ret;
268 b = NULL;
269 } else if (*p == '}') {
270 *error_message = "unmatched }";
271 return EINVAL; /* XXX */
272 } else if(*p != '\0') {
273 if (s == NULL) {
274 *error_message = "binding before section";
275 return EINVAL;
277 ret = parse_binding(f, lineno, p, &b, &s->u.list, error_message);
278 if (ret)
279 return ret;
282 return 0;
285 krb5_error_code KRB5_LIB_FUNCTION
286 krb5_config_parse_string_multi(krb5_context context,
287 const char *string,
288 krb5_config_section **res)
290 const char *str;
291 unsigned lineno = 0;
292 krb5_error_code ret;
293 struct fileptr f;
294 f.f = NULL;
295 f.s = string;
297 ret = krb5_config_parse_debug (&f, res, &lineno, &str);
298 if (ret) {
299 krb5_set_error_string (context, "%s:%u: %s", "<constant>", lineno, str);
300 return ret;
302 return 0;
305 krb5_error_code KRB5_LIB_FUNCTION
306 krb5_config_parse_file_multi (krb5_context context,
307 const char *fname,
308 krb5_config_section **res)
310 const char *str;
311 unsigned lineno = 0;
312 krb5_error_code ret;
313 struct fileptr f;
314 f.f = fopen(fname, "r");
315 f.s = NULL;
316 if(f.f == NULL) {
317 ret = errno;
318 krb5_set_error_string (context, "open %s: %s", fname, strerror(ret));
319 return ret;
322 ret = krb5_config_parse_debug (&f, res, &lineno, &str);
323 fclose(f.f);
324 if (ret) {
325 krb5_set_error_string (context, "%s:%u: %s", fname, lineno, str);
326 return ret;
328 return 0;
331 krb5_error_code KRB5_LIB_FUNCTION
332 krb5_config_parse_file (krb5_context context,
333 const char *fname,
334 krb5_config_section **res)
336 *res = NULL;
337 return krb5_config_parse_file_multi(context, fname, res);
340 #endif /* !HAVE_NETINFO */
342 static void
343 free_binding (krb5_context context, krb5_config_binding *b)
345 krb5_config_binding *next_b;
347 while (b) {
348 free (b->name);
349 if (b->type == krb5_config_string)
350 free (b->u.string);
351 else if (b->type == krb5_config_list)
352 free_binding (context, b->u.list);
353 else
354 krb5_abortx(context, "unknown binding type (%d) in free_binding",
355 b->type);
356 next_b = b->next;
357 free (b);
358 b = next_b;
362 krb5_error_code KRB5_LIB_FUNCTION
363 krb5_config_file_free (krb5_context context, krb5_config_section *s)
365 free_binding (context, s);
366 return 0;
369 const void *
370 krb5_config_get_next (krb5_context context,
371 const krb5_config_section *c,
372 const krb5_config_binding **pointer,
373 int type,
374 ...)
376 const char *ret;
377 va_list args;
379 va_start(args, type);
380 ret = krb5_config_vget_next (context, c, pointer, type, args);
381 va_end(args);
382 return ret;
385 static const void *
386 vget_next(krb5_context context,
387 const krb5_config_binding *b,
388 const krb5_config_binding **pointer,
389 int type,
390 const char *name,
391 va_list args)
393 const char *p = va_arg(args, const char *);
394 while(b != NULL) {
395 if(strcmp(b->name, name) == 0) {
396 if(b->type == type && p == NULL) {
397 *pointer = b;
398 return b->u.generic;
399 } else if(b->type == krb5_config_list && p != NULL) {
400 return vget_next(context, b->u.list, pointer, type, p, args);
403 b = b->next;
405 return NULL;
408 const void *
409 krb5_config_vget_next (krb5_context context,
410 const krb5_config_section *c,
411 const krb5_config_binding **pointer,
412 int type,
413 va_list args)
415 const krb5_config_binding *b;
416 const char *p;
418 if(c == NULL)
419 c = context->cf;
421 if (c == NULL)
422 return NULL;
424 if (*pointer == NULL) {
425 /* first time here, walk down the tree looking for the right
426 section */
427 p = va_arg(args, const char *);
428 if (p == NULL)
429 return NULL;
430 return vget_next(context, c, pointer, type, p, args);
433 /* we were called again, so just look for more entries with the
434 same name and type */
435 for (b = (*pointer)->next; b != NULL; b = b->next) {
436 if(strcmp(b->name, (*pointer)->name) == 0 && b->type == type) {
437 *pointer = b;
438 return b->u.generic;
441 return NULL;
444 const void *
445 krb5_config_get (krb5_context context,
446 const krb5_config_section *c,
447 int type,
448 ...)
450 const void *ret;
451 va_list args;
453 va_start(args, type);
454 ret = krb5_config_vget (context, c, type, args);
455 va_end(args);
456 return ret;
459 const void *
460 krb5_config_vget (krb5_context context,
461 const krb5_config_section *c,
462 int type,
463 va_list args)
465 const krb5_config_binding *foo = NULL;
467 return krb5_config_vget_next (context, c, &foo, type, args);
470 const krb5_config_binding *
471 krb5_config_get_list (krb5_context context,
472 const krb5_config_section *c,
473 ...)
475 const krb5_config_binding *ret;
476 va_list args;
478 va_start(args, c);
479 ret = krb5_config_vget_list (context, c, args);
480 va_end(args);
481 return ret;
484 const krb5_config_binding *
485 krb5_config_vget_list (krb5_context context,
486 const krb5_config_section *c,
487 va_list args)
489 return krb5_config_vget (context, c, krb5_config_list, args);
492 const char* KRB5_LIB_FUNCTION
493 krb5_config_get_string (krb5_context context,
494 const krb5_config_section *c,
495 ...)
497 const char *ret;
498 va_list args;
500 va_start(args, c);
501 ret = krb5_config_vget_string (context, c, args);
502 va_end(args);
503 return ret;
506 const char* KRB5_LIB_FUNCTION
507 krb5_config_vget_string (krb5_context context,
508 const krb5_config_section *c,
509 va_list args)
511 return krb5_config_vget (context, c, krb5_config_string, args);
514 const char* KRB5_LIB_FUNCTION
515 krb5_config_vget_string_default (krb5_context context,
516 const krb5_config_section *c,
517 const char *def_value,
518 va_list args)
520 const char *ret;
522 ret = krb5_config_vget_string (context, c, args);
523 if (ret == NULL)
524 ret = def_value;
525 return ret;
528 const char* KRB5_LIB_FUNCTION
529 krb5_config_get_string_default (krb5_context context,
530 const krb5_config_section *c,
531 const char *def_value,
532 ...)
534 const char *ret;
535 va_list args;
537 va_start(args, def_value);
538 ret = krb5_config_vget_string_default (context, c, def_value, args);
539 va_end(args);
540 return ret;
543 char ** KRB5_LIB_FUNCTION
544 krb5_config_vget_strings(krb5_context context,
545 const krb5_config_section *c,
546 va_list args)
548 char **strings = NULL;
549 int nstr = 0;
550 const krb5_config_binding *b = NULL;
551 const char *p;
553 while((p = krb5_config_vget_next(context, c, &b,
554 krb5_config_string, args))) {
555 char *tmp = strdup(p);
556 char *pos = NULL;
557 char *s;
558 if(tmp == NULL)
559 goto cleanup;
560 s = strtok_r(tmp, " \t", &pos);
561 while(s){
562 char **tmp2 = realloc(strings, (nstr + 1) * sizeof(*strings));
563 if(tmp2 == NULL)
564 goto cleanup;
565 strings = tmp2;
566 strings[nstr] = strdup(s);
567 nstr++;
568 if(strings[nstr-1] == NULL)
569 goto cleanup;
570 s = strtok_r(NULL, " \t", &pos);
572 free(tmp);
574 if(nstr){
575 char **tmp = realloc(strings, (nstr + 1) * sizeof(*strings));
576 if(tmp == NULL)
577 goto cleanup;
578 strings = tmp;
579 strings[nstr] = NULL;
581 return strings;
582 cleanup:
583 while(nstr--)
584 free(strings[nstr]);
585 free(strings);
586 return NULL;
590 char**
591 krb5_config_get_strings(krb5_context context,
592 const krb5_config_section *c,
593 ...)
595 va_list ap;
596 char **ret;
597 va_start(ap, c);
598 ret = krb5_config_vget_strings(context, c, ap);
599 va_end(ap);
600 return ret;
603 void KRB5_LIB_FUNCTION
604 krb5_config_free_strings(char **strings)
606 char **s = strings;
607 while(s && *s){
608 free(*s);
609 s++;
611 free(strings);
614 krb5_boolean KRB5_LIB_FUNCTION
615 krb5_config_vget_bool_default (krb5_context context,
616 const krb5_config_section *c,
617 krb5_boolean def_value,
618 va_list args)
620 const char *str;
621 str = krb5_config_vget_string (context, c, args);
622 if(str == NULL)
623 return def_value;
624 if(strcasecmp(str, "yes") == 0 ||
625 strcasecmp(str, "true") == 0 ||
626 atoi(str)) return TRUE;
627 return FALSE;
630 krb5_boolean KRB5_LIB_FUNCTION
631 krb5_config_vget_bool (krb5_context context,
632 const krb5_config_section *c,
633 va_list args)
635 return krb5_config_vget_bool_default (context, c, FALSE, args);
638 krb5_boolean KRB5_LIB_FUNCTION
639 krb5_config_get_bool_default (krb5_context context,
640 const krb5_config_section *c,
641 krb5_boolean def_value,
642 ...)
644 va_list ap;
645 krb5_boolean ret;
646 va_start(ap, def_value);
647 ret = krb5_config_vget_bool_default(context, c, def_value, ap);
648 va_end(ap);
649 return ret;
652 krb5_boolean KRB5_LIB_FUNCTION
653 krb5_config_get_bool (krb5_context context,
654 const krb5_config_section *c,
655 ...)
657 va_list ap;
658 krb5_boolean ret;
659 va_start(ap, c);
660 ret = krb5_config_vget_bool (context, c, ap);
661 va_end(ap);
662 return ret;
665 int KRB5_LIB_FUNCTION
666 krb5_config_vget_time_default (krb5_context context,
667 const krb5_config_section *c,
668 int def_value,
669 va_list args)
671 const char *str;
672 krb5_deltat t;
674 str = krb5_config_vget_string (context, c, args);
675 if(str == NULL)
676 return def_value;
677 if (krb5_string_to_deltat(str, &t))
678 return def_value;
679 return t;
682 int KRB5_LIB_FUNCTION
683 krb5_config_vget_time (krb5_context context,
684 const krb5_config_section *c,
685 va_list args)
687 return krb5_config_vget_time_default (context, c, -1, args);
690 int KRB5_LIB_FUNCTION
691 krb5_config_get_time_default (krb5_context context,
692 const krb5_config_section *c,
693 int def_value,
694 ...)
696 va_list ap;
697 int ret;
698 va_start(ap, def_value);
699 ret = krb5_config_vget_time_default(context, c, def_value, ap);
700 va_end(ap);
701 return ret;
704 int KRB5_LIB_FUNCTION
705 krb5_config_get_time (krb5_context context,
706 const krb5_config_section *c,
707 ...)
709 va_list ap;
710 int ret;
711 va_start(ap, c);
712 ret = krb5_config_vget_time (context, c, ap);
713 va_end(ap);
714 return ret;
718 int KRB5_LIB_FUNCTION
719 krb5_config_vget_int_default (krb5_context context,
720 const krb5_config_section *c,
721 int def_value,
722 va_list args)
724 const char *str;
725 str = krb5_config_vget_string (context, c, args);
726 if(str == NULL)
727 return def_value;
728 else {
729 char *endptr;
730 long l;
731 l = strtol(str, &endptr, 0);
732 if (endptr == str)
733 return def_value;
734 else
735 return l;
739 int KRB5_LIB_FUNCTION
740 krb5_config_vget_int (krb5_context context,
741 const krb5_config_section *c,
742 va_list args)
744 return krb5_config_vget_int_default (context, c, -1, args);
747 int KRB5_LIB_FUNCTION
748 krb5_config_get_int_default (krb5_context context,
749 const krb5_config_section *c,
750 int def_value,
751 ...)
753 va_list ap;
754 int ret;
755 va_start(ap, def_value);
756 ret = krb5_config_vget_int_default(context, c, def_value, ap);
757 va_end(ap);
758 return ret;
761 int KRB5_LIB_FUNCTION
762 krb5_config_get_int (krb5_context context,
763 const krb5_config_section *c,
764 ...)
766 va_list ap;
767 int ret;
768 va_start(ap, c);
769 ret = krb5_config_vget_int (context, c, ap);
770 va_end(ap);
771 return ret;