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"
36 /* Gaah! I want a portable funopen */
43 config_fgets(char *str, size_t len, struct fileptr *ptr)
45 /* XXX this is not correct, in that they don't do the same if the
46 line is longer than len */
48 return fgets(str, len, ptr->f);
50 /* this is almost strsep_copy */
55 p = ptr->s + strcspn(ptr->s, "\n");
58 l = min(len, p - ptr->s);
60 memcpy(str, ptr->s, l);
68 static krb5_error_code parse_section(char *p, krb5_config_section **s,
69 krb5_config_section **res,
70 const char **error_message);
71 static krb5_error_code parse_binding(struct fileptr *f, unsigned *lineno, char *p,
72 krb5_config_binding **b,
73 krb5_config_binding **parent,
74 const char **error_message);
75 static krb5_error_code parse_list(struct fileptr *f, unsigned *lineno,
76 krb5_config_binding **parent,
77 const char **error_message);
79 static krb5_config_section *
80 get_entry(krb5_config_section **parent, const char *name, int type)
82 krb5_config_section **q;
84 for(q = parent; *q != NULL; q = &(*q)->next)
85 if(type == krb5_config_list &&
87 strcmp(name, (*q)->name) == 0)
89 *q = calloc(1, sizeof(**q));
92 (*q)->name = strdup(name);
94 if((*q)->name == NULL) {
112 * starting at the line in `p', storing the resulting structure in
113 * `s' and hooking it into `parent'.
114 * Store the error message in `error_message'.
117 static krb5_error_code
118 parse_section(char *p, krb5_config_section **s, krb5_config_section **parent,
119 const char **error_message)
122 krb5_config_section *tmp;
124 p1 = strchr (p + 1, ']');
126 *error_message = "missing ]";
127 return KRB5_CONFIG_BADFORMAT;
130 tmp = get_entry(parent, p + 1, krb5_config_list);
132 *error_message = "out of memory";
133 return KRB5_CONFIG_BADFORMAT;
140 * Parse a brace-enclosed list from `f', hooking in the structure at
142 * Store the error message in `error_message'.
145 static krb5_error_code
146 parse_list(struct fileptr *f, unsigned *lineno, krb5_config_binding **parent,
147 const char **error_message)
151 krb5_config_binding *b = NULL;
152 unsigned beg_lineno = *lineno;
154 while(config_fgets(buf, sizeof(buf), f) != NULL) {
158 buf[strcspn(buf, "\r\n")] = '\0';
160 while(isspace((unsigned char)*p))
162 if (*p == '#' || *p == ';' || *p == '\0')
164 while(isspace((unsigned char)*p))
170 ret = parse_binding (f, lineno, p, &b, parent, error_message);
174 *lineno = beg_lineno;
175 *error_message = "unclosed {";
176 return KRB5_CONFIG_BADFORMAT;
183 static krb5_error_code
184 parse_binding(struct fileptr *f, unsigned *lineno, char *p,
185 krb5_config_binding **b, krb5_config_binding **parent,
186 const char **error_message)
188 krb5_config_binding *tmp;
190 krb5_error_code ret = 0;
193 while (*p && *p != '=' && !isspace((unsigned char)*p))
196 *error_message = "missing =";
197 return KRB5_CONFIG_BADFORMAT;
200 while (isspace((unsigned char)*p))
203 *error_message = "missing =";
204 return KRB5_CONFIG_BADFORMAT;
207 while(isspace((unsigned char)*p))
211 tmp = get_entry(parent, p1, krb5_config_list);
213 *error_message = "out of memory";
214 return KRB5_CONFIG_BADFORMAT;
216 ret = parse_list (f, lineno, &tmp->u.list, error_message);
218 tmp = get_entry(parent, p1, krb5_config_string);
220 *error_message = "out of memory";
221 return KRB5_CONFIG_BADFORMAT;
225 while(p > p1 && isspace((unsigned char)*(p-1)))
228 tmp->u.string = strdup(p1);
235 * Parse the config file `fname', generating the structures into `res'
236 * returning error messages in `error_message'
239 static krb5_error_code
240 krb5_config_parse_debug (struct fileptr *f,
241 krb5_config_section **res,
243 const char **error_message)
245 krb5_config_section *s = NULL;
246 krb5_config_binding *b = NULL;
250 while (config_fgets(buf, sizeof(buf), f) != NULL) {
254 buf[strcspn(buf, "\r\n")] = '\0';
256 while(isspace((unsigned char)*p))
258 if (*p == '#' || *p == ';')
261 ret = parse_section(p, &s, res, error_message);
265 } else if (*p == '}') {
266 *error_message = "unmatched }";
267 return EINVAL; /* XXX */
268 } else if(*p != '\0') {
270 *error_message = "binding before section";
273 ret = parse_binding(f, lineno, p, &b, &s->u.list, error_message);
281 krb5_error_code KRB5_LIB_FUNCTION
282 krb5_config_parse_string_multi(krb5_context context,
284 krb5_config_section **res)
293 ret = krb5_config_parse_debug (&f, res, &lineno, &str);
295 krb5_set_error_message (context, ret, "%s:%u: %s",
296 "<constant>", lineno, str);
303 * Parse a configuration file and add the result into res. This
304 * interface can be used to parse several configuration files into one
305 * resulting krb5_config_section by calling it repeatably.
307 * @param context a Kerberos 5 context.
308 * @param fname a file name to a Kerberos configuration file
309 * @param res the returned result, must be free with krb5_free_config_files().
310 * @return Return an error code or 0, see krb5_get_error_message().
312 * @ingroup krb5_support
315 krb5_error_code KRB5_LIB_FUNCTION
316 krb5_config_parse_file_multi (krb5_context context,
318 krb5_config_section **res)
321 char *newfname = NULL;
327 * If the fname starts with "~/" parse configuration file in the
328 * current users home directory. The behavior can be disabled and
329 * enabled by calling krb5_set_home_dir_access().
331 if (_krb5_homedir_access(context) && fname[0] == '~' && fname[1] == '/') {
332 const char *home = NULL;
335 home = getenv("HOME");
338 struct passwd *pw = getpwuid(getuid());
343 asprintf(&newfname, "%s%s", home, &fname[1]);
344 if (newfname == NULL) {
345 krb5_set_error_message(context, ENOMEM,
346 N_("malloc: out of memory", ""));
353 f.f = fopen(fname, "r");
357 krb5_set_error_message (context, ret, "open %s: %s",
358 fname, strerror(ret));
364 ret = krb5_config_parse_debug (&f, res, &lineno, &str);
367 krb5_set_error_message (context, ret, "%s:%u: %s", fname, lineno, str);
377 krb5_error_code KRB5_LIB_FUNCTION
378 krb5_config_parse_file (krb5_context context,
380 krb5_config_section **res)
383 return krb5_config_parse_file_multi(context, fname, res);
387 free_binding (krb5_context context, krb5_config_binding *b)
389 krb5_config_binding *next_b;
393 if (b->type == krb5_config_string)
395 else if (b->type == krb5_config_list)
396 free_binding (context, b->u.list);
398 krb5_abortx(context, "unknown binding type (%d) in free_binding",
406 krb5_error_code KRB5_LIB_FUNCTION
407 krb5_config_file_free (krb5_context context, krb5_config_section *s)
409 free_binding (context, s);
414 _krb5_config_copy(krb5_context context,
415 krb5_config_section *c,
416 krb5_config_section **head)
418 krb5_config_binding *d, *previous = NULL;
423 d = calloc(1, sizeof(*d));
428 d->name = strdup(c->name);
430 if (d->type == krb5_config_string)
431 d->u.string = strdup(c->u.string);
432 else if (d->type == krb5_config_list)
433 _krb5_config_copy (context, c->u.list, &d->u.list);
436 "unknown binding type (%d) in krb5_config_copy",
450 krb5_config_get_next (krb5_context context,
451 const krb5_config_section *c,
452 const krb5_config_binding **pointer,
459 va_start(args, type);
460 ret = krb5_config_vget_next (context, c, pointer, type, args);
466 vget_next(krb5_context context,
467 const krb5_config_binding *b,
468 const krb5_config_binding **pointer,
473 const char *p = va_arg(args, const char *);
475 if(strcmp(b->name, name) == 0) {
476 if(b->type == type && p == NULL) {
479 } else if(b->type == krb5_config_list && p != NULL) {
480 return vget_next(context, b->u.list, pointer, type, p, args);
489 krb5_config_vget_next (krb5_context context,
490 const krb5_config_section *c,
491 const krb5_config_binding **pointer,
495 const krb5_config_binding *b;
504 if (*pointer == NULL) {
505 /* first time here, walk down the tree looking for the right
507 p = va_arg(args, const char *);
510 return vget_next(context, c, pointer, type, p, args);
513 /* we were called again, so just look for more entries with the
514 same name and type */
515 for (b = (*pointer)->next; b != NULL; b = b->next) {
516 if(strcmp(b->name, (*pointer)->name) == 0 && b->type == type) {
525 krb5_config_get (krb5_context context,
526 const krb5_config_section *c,
533 va_start(args, type);
534 ret = krb5_config_vget (context, c, type, args);
540 krb5_config_vget (krb5_context context,
541 const krb5_config_section *c,
545 const krb5_config_binding *foo = NULL;
547 return krb5_config_vget_next (context, c, &foo, type, args);
550 const krb5_config_binding *
551 krb5_config_get_list (krb5_context context,
552 const krb5_config_section *c,
555 const krb5_config_binding *ret;
559 ret = krb5_config_vget_list (context, c, args);
564 const krb5_config_binding *
565 krb5_config_vget_list (krb5_context context,
566 const krb5_config_section *c,
569 return krb5_config_vget (context, c, krb5_config_list, args);
572 const char* KRB5_LIB_FUNCTION
573 krb5_config_get_string (krb5_context context,
574 const krb5_config_section *c,
581 ret = krb5_config_vget_string (context, c, args);
586 const char* KRB5_LIB_FUNCTION
587 krb5_config_vget_string (krb5_context context,
588 const krb5_config_section *c,
591 return krb5_config_vget (context, c, krb5_config_string, args);
594 const char* KRB5_LIB_FUNCTION
595 krb5_config_vget_string_default (krb5_context context,
596 const krb5_config_section *c,
597 const char *def_value,
602 ret = krb5_config_vget_string (context, c, args);
608 const char* KRB5_LIB_FUNCTION
609 krb5_config_get_string_default (krb5_context context,
610 const krb5_config_section *c,
611 const char *def_value,
617 va_start(args, def_value);
618 ret = krb5_config_vget_string_default (context, c, def_value, args);
623 char ** KRB5_LIB_FUNCTION
624 krb5_config_vget_strings(krb5_context context,
625 const krb5_config_section *c,
628 char **strings = NULL;
630 const krb5_config_binding *b = NULL;
633 while((p = krb5_config_vget_next(context, c, &b,
634 krb5_config_string, args))) {
635 char *tmp = strdup(p);
640 s = strtok_r(tmp, " \t", &pos);
642 char **tmp2 = realloc(strings, (nstr + 1) * sizeof(*strings));
646 strings[nstr] = strdup(s);
648 if(strings[nstr-1] == NULL)
650 s = strtok_r(NULL, " \t", &pos);
655 char **tmp = realloc(strings, (nstr + 1) * sizeof(*strings));
659 strings[nstr] = NULL;
671 krb5_config_get_strings(krb5_context context,
672 const krb5_config_section *c,
678 ret = krb5_config_vget_strings(context, c, ap);
683 void KRB5_LIB_FUNCTION
684 krb5_config_free_strings(char **strings)
694 krb5_boolean KRB5_LIB_FUNCTION
695 krb5_config_vget_bool_default (krb5_context context,
696 const krb5_config_section *c,
697 krb5_boolean def_value,
701 str = krb5_config_vget_string (context, c, args);
704 if(strcasecmp(str, "yes") == 0 ||
705 strcasecmp(str, "true") == 0 ||
706 atoi(str)) return TRUE;
710 krb5_boolean KRB5_LIB_FUNCTION
711 krb5_config_vget_bool (krb5_context context,
712 const krb5_config_section *c,
715 return krb5_config_vget_bool_default (context, c, FALSE, args);
718 krb5_boolean KRB5_LIB_FUNCTION
719 krb5_config_get_bool_default (krb5_context context,
720 const krb5_config_section *c,
721 krb5_boolean def_value,
726 va_start(ap, def_value);
727 ret = krb5_config_vget_bool_default(context, c, def_value, ap);
732 krb5_boolean KRB5_LIB_FUNCTION
733 krb5_config_get_bool (krb5_context context,
734 const krb5_config_section *c,
740 ret = krb5_config_vget_bool (context, c, ap);
745 int KRB5_LIB_FUNCTION
746 krb5_config_vget_time_default (krb5_context context,
747 const krb5_config_section *c,
754 str = krb5_config_vget_string (context, c, args);
757 if (krb5_string_to_deltat(str, &t))
762 int KRB5_LIB_FUNCTION
763 krb5_config_vget_time (krb5_context context,
764 const krb5_config_section *c,
767 return krb5_config_vget_time_default (context, c, -1, args);
770 int KRB5_LIB_FUNCTION
771 krb5_config_get_time_default (krb5_context context,
772 const krb5_config_section *c,
778 va_start(ap, def_value);
779 ret = krb5_config_vget_time_default(context, c, def_value, ap);
784 int KRB5_LIB_FUNCTION
785 krb5_config_get_time (krb5_context context,
786 const krb5_config_section *c,
792 ret = krb5_config_vget_time (context, c, ap);
798 int KRB5_LIB_FUNCTION
799 krb5_config_vget_int_default (krb5_context context,
800 const krb5_config_section *c,
805 str = krb5_config_vget_string (context, c, args);
811 l = strtol(str, &endptr, 0);
819 int KRB5_LIB_FUNCTION
820 krb5_config_vget_int (krb5_context context,
821 const krb5_config_section *c,
824 return krb5_config_vget_int_default (context, c, -1, args);
827 int KRB5_LIB_FUNCTION
828 krb5_config_get_int_default (krb5_context context,
829 const krb5_config_section *c,
835 va_start(ap, def_value);
836 ret = krb5_config_vget_int_default(context, c, def_value, ap);
841 int KRB5_LIB_FUNCTION
842 krb5_config_get_int (krb5_context context,
843 const krb5_config_section *c,
849 ret = krb5_config_vget_int (context, c, ap);