2 * Copyright (c) 1997 - 2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
34 #include "krb5_locl.h"
35 RCSID("$Id: config_file.c 19213 2006-12-04 23:36:36Z lha $");
39 /* Gaah! I want a portable funopen */
46 config_fgets(char *str, size_t len, struct fileptr *ptr)
48 /* XXX this is not correct, in that they don't do the same if the
49 line is longer than len */
51 return fgets(str, len, ptr->f);
53 /* this is almost strsep_copy */
58 p = ptr->s + strcspn(ptr->s, "\n");
61 l = min(len, p - ptr->s);
63 memcpy(str, ptr->s, l);
71 static krb5_error_code parse_section(char *p, krb5_config_section **s,
72 krb5_config_section **res,
73 const char **error_message);
74 static krb5_error_code parse_binding(struct fileptr *f, unsigned *lineno, char *p,
75 krb5_config_binding **b,
76 krb5_config_binding **parent,
77 const char **error_message);
78 static krb5_error_code parse_list(struct fileptr *f, unsigned *lineno,
79 krb5_config_binding **parent,
80 const char **error_message);
82 static krb5_config_section *
83 get_entry(krb5_config_section **parent, const char *name, int type)
85 krb5_config_section **q;
87 for(q = parent; *q != NULL; q = &(*q)->next)
88 if(type == krb5_config_list &&
90 strcmp(name, (*q)->name) == 0)
92 *q = calloc(1, sizeof(**q));
95 (*q)->name = strdup(name);
97 if((*q)->name == NULL) {
115 * starting at the line in `p', storing the resulting structure in
116 * `s' and hooking it into `parent'.
117 * Store the error message in `error_message'.
120 static krb5_error_code
121 parse_section(char *p, krb5_config_section **s, krb5_config_section **parent,
122 const char **error_message)
125 krb5_config_section *tmp;
127 p1 = strchr (p + 1, ']');
129 *error_message = "missing ]";
130 return KRB5_CONFIG_BADFORMAT;
133 tmp = get_entry(parent, p + 1, krb5_config_list);
135 *error_message = "out of memory";
136 return KRB5_CONFIG_BADFORMAT;
143 * Parse a brace-enclosed list from `f', hooking in the structure at
145 * Store the error message in `error_message'.
148 static krb5_error_code
149 parse_list(struct fileptr *f, unsigned *lineno, krb5_config_binding **parent,
150 const char **error_message)
154 krb5_config_binding *b = NULL;
155 unsigned beg_lineno = *lineno;
157 while(config_fgets(buf, sizeof(buf), f) != NULL) {
161 buf[strcspn(buf, "\r\n")] = '\0';
163 while(isspace((unsigned char)*p))
165 if (*p == '#' || *p == ';' || *p == '\0')
167 while(isspace((unsigned char)*p))
173 ret = parse_binding (f, lineno, p, &b, parent, error_message);
177 *lineno = beg_lineno;
178 *error_message = "unclosed {";
179 return KRB5_CONFIG_BADFORMAT;
186 static krb5_error_code
187 parse_binding(struct fileptr *f, unsigned *lineno, char *p,
188 krb5_config_binding **b, krb5_config_binding **parent,
189 const char **error_message)
191 krb5_config_binding *tmp;
193 krb5_error_code ret = 0;
196 while (*p && *p != '=' && !isspace((unsigned char)*p))
199 *error_message = "missing =";
200 return KRB5_CONFIG_BADFORMAT;
203 while (isspace((unsigned char)*p))
206 *error_message = "missing =";
207 return KRB5_CONFIG_BADFORMAT;
210 while(isspace((unsigned char)*p))
214 tmp = get_entry(parent, p1, krb5_config_list);
216 *error_message = "out of memory";
217 return KRB5_CONFIG_BADFORMAT;
219 ret = parse_list (f, lineno, &tmp->u.list, error_message);
221 tmp = get_entry(parent, p1, krb5_config_string);
223 *error_message = "out of memory";
224 return KRB5_CONFIG_BADFORMAT;
228 while(p > p1 && isspace((unsigned char)*(p-1)))
231 tmp->u.string = strdup(p1);
238 * Parse the config file `fname', generating the structures into `res'
239 * returning error messages in `error_message'
242 static krb5_error_code
243 krb5_config_parse_debug (struct fileptr *f,
244 krb5_config_section **res,
246 const char **error_message)
248 krb5_config_section *s = NULL;
249 krb5_config_binding *b = NULL;
253 while (config_fgets(buf, sizeof(buf), f) != NULL) {
257 buf[strcspn(buf, "\r\n")] = '\0';
259 while(isspace((unsigned char)*p))
261 if (*p == '#' || *p == ';')
264 ret = parse_section(p, &s, res, error_message);
268 } else if (*p == '}') {
269 *error_message = "unmatched }";
270 return EINVAL; /* XXX */
271 } else if(*p != '\0') {
273 *error_message = "binding before section";
276 ret = parse_binding(f, lineno, p, &b, &s->u.list, error_message);
284 krb5_error_code KRB5_LIB_FUNCTION
285 krb5_config_parse_string_multi(krb5_context context,
287 krb5_config_section **res)
296 ret = krb5_config_parse_debug (&f, res, &lineno, &str);
298 krb5_set_error_string (context, "%s:%u: %s", "<constant>", lineno, str);
304 krb5_error_code KRB5_LIB_FUNCTION
305 krb5_config_parse_file_multi (krb5_context context,
307 krb5_config_section **res)
313 f.f = fopen(fname, "r");
317 krb5_set_error_string (context, "open %s: %s", fname, strerror(ret));
321 ret = krb5_config_parse_debug (&f, res, &lineno, &str);
324 krb5_set_error_string (context, "%s:%u: %s", fname, lineno, str);
330 krb5_error_code KRB5_LIB_FUNCTION
331 krb5_config_parse_file (krb5_context context,
333 krb5_config_section **res)
336 return krb5_config_parse_file_multi(context, fname, res);
339 #endif /* !HAVE_NETINFO */
342 free_binding (krb5_context context, krb5_config_binding *b)
344 krb5_config_binding *next_b;
348 if (b->type == krb5_config_string)
350 else if (b->type == krb5_config_list)
351 free_binding (context, b->u.list);
353 krb5_abortx(context, "unknown binding type (%d) in free_binding",
361 krb5_error_code KRB5_LIB_FUNCTION
362 krb5_config_file_free (krb5_context context, krb5_config_section *s)
364 free_binding (context, s);
369 krb5_config_get_next (krb5_context context,
370 const krb5_config_section *c,
371 const krb5_config_binding **pointer,
378 va_start(args, type);
379 ret = krb5_config_vget_next (context, c, pointer, type, args);
385 vget_next(krb5_context context,
386 const krb5_config_binding *b,
387 const krb5_config_binding **pointer,
392 const char *p = va_arg(args, const char *);
394 if(strcmp(b->name, name) == 0) {
395 if(b->type == type && p == NULL) {
398 } else if(b->type == krb5_config_list && p != NULL) {
399 return vget_next(context, b->u.list, pointer, type, p, args);
408 krb5_config_vget_next (krb5_context context,
409 const krb5_config_section *c,
410 const krb5_config_binding **pointer,
414 const krb5_config_binding *b;
423 if (*pointer == NULL) {
424 /* first time here, walk down the tree looking for the right
426 p = va_arg(args, const char *);
429 return vget_next(context, c, pointer, type, p, args);
432 /* we were called again, so just look for more entries with the
433 same name and type */
434 for (b = (*pointer)->next; b != NULL; b = b->next) {
435 if(strcmp(b->name, (*pointer)->name) == 0 && b->type == type) {
444 krb5_config_get (krb5_context context,
445 const krb5_config_section *c,
452 va_start(args, type);
453 ret = krb5_config_vget (context, c, type, args);
459 krb5_config_vget (krb5_context context,
460 const krb5_config_section *c,
464 const krb5_config_binding *foo = NULL;
466 return krb5_config_vget_next (context, c, &foo, type, args);
469 const krb5_config_binding *
470 krb5_config_get_list (krb5_context context,
471 const krb5_config_section *c,
474 const krb5_config_binding *ret;
478 ret = krb5_config_vget_list (context, c, args);
483 const krb5_config_binding *
484 krb5_config_vget_list (krb5_context context,
485 const krb5_config_section *c,
488 return krb5_config_vget (context, c, krb5_config_list, args);
491 const char* KRB5_LIB_FUNCTION
492 krb5_config_get_string (krb5_context context,
493 const krb5_config_section *c,
500 ret = krb5_config_vget_string (context, c, args);
505 const char* KRB5_LIB_FUNCTION
506 krb5_config_vget_string (krb5_context context,
507 const krb5_config_section *c,
510 return krb5_config_vget (context, c, krb5_config_string, args);
513 const char* KRB5_LIB_FUNCTION
514 krb5_config_vget_string_default (krb5_context context,
515 const krb5_config_section *c,
516 const char *def_value,
521 ret = krb5_config_vget_string (context, c, args);
527 const char* KRB5_LIB_FUNCTION
528 krb5_config_get_string_default (krb5_context context,
529 const krb5_config_section *c,
530 const char *def_value,
536 va_start(args, def_value);
537 ret = krb5_config_vget_string_default (context, c, def_value, args);
542 char ** KRB5_LIB_FUNCTION
543 krb5_config_vget_strings(krb5_context context,
544 const krb5_config_section *c,
547 char **strings = NULL;
549 const krb5_config_binding *b = NULL;
552 while((p = krb5_config_vget_next(context, c, &b,
553 krb5_config_string, args))) {
554 char *tmp = strdup(p);
559 s = strtok_r(tmp, " \t", &pos);
561 char **tmp2 = realloc(strings, (nstr + 1) * sizeof(*strings));
565 strings[nstr] = strdup(s);
567 if(strings[nstr-1] == NULL)
569 s = strtok_r(NULL, " \t", &pos);
574 char **tmp = realloc(strings, (nstr + 1) * sizeof(*strings));
578 strings[nstr] = NULL;
590 krb5_config_get_strings(krb5_context context,
591 const krb5_config_section *c,
597 ret = krb5_config_vget_strings(context, c, ap);
602 void KRB5_LIB_FUNCTION
603 krb5_config_free_strings(char **strings)
613 krb5_boolean KRB5_LIB_FUNCTION
614 krb5_config_vget_bool_default (krb5_context context,
615 const krb5_config_section *c,
616 krb5_boolean def_value,
620 str = krb5_config_vget_string (context, c, args);
623 if(strcasecmp(str, "yes") == 0 ||
624 strcasecmp(str, "true") == 0 ||
625 atoi(str)) return TRUE;
629 krb5_boolean KRB5_LIB_FUNCTION
630 krb5_config_vget_bool (krb5_context context,
631 const krb5_config_section *c,
634 return krb5_config_vget_bool_default (context, c, FALSE, args);
637 krb5_boolean KRB5_LIB_FUNCTION
638 krb5_config_get_bool_default (krb5_context context,
639 const krb5_config_section *c,
640 krb5_boolean def_value,
645 va_start(ap, def_value);
646 ret = krb5_config_vget_bool_default(context, c, def_value, ap);
651 krb5_boolean KRB5_LIB_FUNCTION
652 krb5_config_get_bool (krb5_context context,
653 const krb5_config_section *c,
659 ret = krb5_config_vget_bool (context, c, ap);
664 int KRB5_LIB_FUNCTION
665 krb5_config_vget_time_default (krb5_context context,
666 const krb5_config_section *c,
673 str = krb5_config_vget_string (context, c, args);
676 if (krb5_string_to_deltat(str, &t))
681 int KRB5_LIB_FUNCTION
682 krb5_config_vget_time (krb5_context context,
683 const krb5_config_section *c,
686 return krb5_config_vget_time_default (context, c, -1, args);
689 int KRB5_LIB_FUNCTION
690 krb5_config_get_time_default (krb5_context context,
691 const krb5_config_section *c,
697 va_start(ap, def_value);
698 ret = krb5_config_vget_time_default(context, c, def_value, ap);
703 int KRB5_LIB_FUNCTION
704 krb5_config_get_time (krb5_context context,
705 const krb5_config_section *c,
711 ret = krb5_config_vget_time (context, c, ap);
717 int KRB5_LIB_FUNCTION
718 krb5_config_vget_int_default (krb5_context context,
719 const krb5_config_section *c,
724 str = krb5_config_vget_string (context, c, args);
730 l = strtol(str, &endptr, 0);
738 int KRB5_LIB_FUNCTION
739 krb5_config_vget_int (krb5_context context,
740 const krb5_config_section *c,
743 return krb5_config_vget_int_default (context, c, -1, args);
746 int KRB5_LIB_FUNCTION
747 krb5_config_get_int_default (krb5_context context,
748 const krb5_config_section *c,
754 va_start(ap, def_value);
755 ret = krb5_config_vget_int_default(context, c, def_value, ap);
760 int KRB5_LIB_FUNCTION
761 krb5_config_get_int (krb5_context context,
762 const krb5_config_section *c,
768 ret = krb5_config_vget_int (context, c, ap);