diff options
author | dfr <dfr@FreeBSD.org> | 2008-05-07 13:39:42 +0000 |
---|---|---|
committer | dfr <dfr@FreeBSD.org> | 2008-05-07 13:39:42 +0000 |
commit | 52bf09d8197dd1ec84e1ab72684f2058f0eae9e1 (patch) | |
tree | 07a0d6761d1b42410a27e4c7d583b766d6671f80 /crypto/heimdal/lib/krb5/config_file.c | |
parent | 6c68306921f6e85bce52c905cf2606c25acdb436 (diff) | |
parent | 51b6601db456e699ea5d4843cbc7239ee92d9c13 (diff) | |
download | FreeBSD-src-52bf09d8197dd1ec84e1ab72684f2058f0eae9e1.zip FreeBSD-src-52bf09d8197dd1ec84e1ab72684f2058f0eae9e1.tar.gz |
This commit was generated by cvs2svn to compensate for changes in r178825,
which included commits to RCS files with non-trunk default branches.
Diffstat (limited to 'crypto/heimdal/lib/krb5/config_file.c')
-rw-r--r-- | crypto/heimdal/lib/krb5/config_file.c | 183 |
1 files changed, 116 insertions, 67 deletions
diff --git a/crypto/heimdal/lib/krb5/config_file.c b/crypto/heimdal/lib/krb5/config_file.c index 47c1a94..ac5eba3 100644 --- a/crypto/heimdal/lib/krb5/config_file.c +++ b/crypto/heimdal/lib/krb5/config_file.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997 - 2003 Kungliga Tekniska Högskolan + * Copyright (c) 1997 - 2004 Kungliga Tekniska Högskolan * (Royal Institute of Technology, Stockholm, Sweden). * All rights reserved. * @@ -32,18 +32,50 @@ */ #include "krb5_locl.h" -RCSID("$Id: config_file.c,v 1.46.4.2 2003/10/13 13:46:10 lha Exp $"); +RCSID("$Id: config_file.c 19213 2006-12-04 23:36:36Z lha $"); #ifndef HAVE_NETINFO +/* Gaah! I want a portable funopen */ +struct fileptr { + const char *s; + FILE *f; +}; + +static char * +config_fgets(char *str, size_t len, struct fileptr *ptr) +{ + /* XXX this is not correct, in that they don't do the same if the + line is longer than len */ + if(ptr->f != NULL) + return fgets(str, len, ptr->f); + else { + /* this is almost strsep_copy */ + const char *p; + ssize_t l; + if(*ptr->s == '\0') + return NULL; + p = ptr->s + strcspn(ptr->s, "\n"); + if(*p == '\n') + p++; + l = min(len, p - ptr->s); + if(len > 0) { + memcpy(str, ptr->s, l); + str[l] = '\0'; + } + ptr->s = p; + return str; + } +} + static krb5_error_code parse_section(char *p, krb5_config_section **s, krb5_config_section **res, const char **error_message); -static krb5_error_code parse_binding(FILE *f, unsigned *lineno, char *p, +static krb5_error_code parse_binding(struct fileptr *f, unsigned *lineno, char *p, krb5_config_binding **b, krb5_config_binding **parent, const char **error_message); -static krb5_error_code parse_list(FILE *f, unsigned *lineno, +static krb5_error_code parse_list(struct fileptr *f, unsigned *lineno, krb5_config_binding **parent, const char **error_message); @@ -114,7 +146,7 @@ parse_section(char *p, krb5_config_section **s, krb5_config_section **parent, */ static krb5_error_code -parse_list(FILE *f, unsigned *lineno, krb5_config_binding **parent, +parse_list(struct fileptr *f, unsigned *lineno, krb5_config_binding **parent, const char **error_message) { char buf[BUFSIZ]; @@ -122,12 +154,11 @@ parse_list(FILE *f, unsigned *lineno, krb5_config_binding **parent, krb5_config_binding *b = NULL; unsigned beg_lineno = *lineno; - while(fgets(buf, sizeof(buf), f) != NULL) { + while(config_fgets(buf, sizeof(buf), f) != NULL) { char *p; ++*lineno; - if (buf[strlen(buf) - 1] == '\n') - buf[strlen(buf) - 1] = '\0'; + buf[strcspn(buf, "\r\n")] = '\0'; p = buf; while(isspace((unsigned char)*p)) ++p; @@ -153,7 +184,7 @@ parse_list(FILE *f, unsigned *lineno, krb5_config_binding **parent, */ static krb5_error_code -parse_binding(FILE *f, unsigned *lineno, char *p, +parse_binding(struct fileptr *f, unsigned *lineno, char *p, krb5_config_binding **b, krb5_config_binding **parent, const char **error_message) { @@ -209,31 +240,21 @@ parse_binding(FILE *f, unsigned *lineno, char *p, */ static krb5_error_code -krb5_config_parse_file_debug (const char *fname, - krb5_config_section **res, - unsigned *lineno, - const char **error_message) +krb5_config_parse_debug (struct fileptr *f, + krb5_config_section **res, + unsigned *lineno, + const char **error_message) { - FILE *f; - krb5_config_section *s; - krb5_config_binding *b; + krb5_config_section *s = NULL; + krb5_config_binding *b = NULL; char buf[BUFSIZ]; - krb5_error_code ret = 0; + krb5_error_code ret; - s = NULL; - b = NULL; - *lineno = 0; - f = fopen (fname, "r"); - if (f == NULL) { - *error_message = "cannot open file"; - return ENOENT; - } - while (fgets(buf, sizeof(buf), f) != NULL) { + while (config_fgets(buf, sizeof(buf), f) != NULL) { char *p; ++*lineno; - if(buf[strlen(buf) - 1] == '\n') - buf[strlen(buf) - 1] = '\0'; + buf[strcspn(buf, "\r\n")] = '\0'; p = buf; while(isspace((unsigned char)*p)) ++p; @@ -241,40 +262,64 @@ krb5_config_parse_file_debug (const char *fname, continue; if (*p == '[') { ret = parse_section(p, &s, res, error_message); - if (ret) { - goto out; - } + if (ret) + return ret; b = NULL; } else if (*p == '}') { *error_message = "unmatched }"; - ret = EINVAL; /* XXX */ - goto out; + return EINVAL; /* XXX */ } else if(*p != '\0') { if (s == NULL) { *error_message = "binding before section"; - ret = EINVAL; - goto out; + return EINVAL; } ret = parse_binding(f, lineno, p, &b, &s->u.list, error_message); if (ret) - goto out; + return ret; } } -out: - fclose (f); - return ret; + return 0; } -krb5_error_code +krb5_error_code KRB5_LIB_FUNCTION +krb5_config_parse_string_multi(krb5_context context, + const char *string, + krb5_config_section **res) +{ + const char *str; + unsigned lineno = 0; + krb5_error_code ret; + struct fileptr f; + f.f = NULL; + f.s = string; + + ret = krb5_config_parse_debug (&f, res, &lineno, &str); + if (ret) { + krb5_set_error_string (context, "%s:%u: %s", "<constant>", lineno, str); + return ret; + } + return 0; +} + +krb5_error_code KRB5_LIB_FUNCTION krb5_config_parse_file_multi (krb5_context context, const char *fname, krb5_config_section **res) { const char *str; - unsigned lineno; + unsigned lineno = 0; krb5_error_code ret; + struct fileptr f; + f.f = fopen(fname, "r"); + f.s = NULL; + if(f.f == NULL) { + ret = errno; + krb5_set_error_string (context, "open %s: %s", fname, strerror(ret)); + return ret; + } - ret = krb5_config_parse_file_debug (fname, res, &lineno, &str); + ret = krb5_config_parse_debug (&f, res, &lineno, &str); + fclose(f.f); if (ret) { krb5_set_error_string (context, "%s:%u: %s", fname, lineno, str); return ret; @@ -282,7 +327,7 @@ krb5_config_parse_file_multi (krb5_context context, return 0; } -krb5_error_code +krb5_error_code KRB5_LIB_FUNCTION krb5_config_parse_file (krb5_context context, const char *fname, krb5_config_section **res) @@ -313,7 +358,7 @@ free_binding (krb5_context context, krb5_config_binding *b) } } -krb5_error_code +krb5_error_code KRB5_LIB_FUNCTION krb5_config_file_free (krb5_context context, krb5_config_section *s) { free_binding (context, s); @@ -443,7 +488,7 @@ krb5_config_vget_list (krb5_context context, return krb5_config_vget (context, c, krb5_config_list, args); } -const char * +const char* KRB5_LIB_FUNCTION krb5_config_get_string (krb5_context context, const krb5_config_section *c, ...) @@ -457,7 +502,7 @@ krb5_config_get_string (krb5_context context, return ret; } -const char * +const char* KRB5_LIB_FUNCTION krb5_config_vget_string (krb5_context context, const krb5_config_section *c, va_list args) @@ -465,7 +510,7 @@ krb5_config_vget_string (krb5_context context, return krb5_config_vget (context, c, krb5_config_string, args); } -const char * +const char* KRB5_LIB_FUNCTION krb5_config_vget_string_default (krb5_context context, const krb5_config_section *c, const char *def_value, @@ -479,7 +524,7 @@ krb5_config_vget_string_default (krb5_context context, return ret; } -const char * +const char* KRB5_LIB_FUNCTION krb5_config_get_string_default (krb5_context context, const krb5_config_section *c, const char *def_value, @@ -494,7 +539,7 @@ krb5_config_get_string_default (krb5_context context, return ret; } -char ** +char ** KRB5_LIB_FUNCTION krb5_config_vget_strings(krb5_context context, const krb5_config_section *c, va_list args) @@ -513,10 +558,10 @@ krb5_config_vget_strings(krb5_context context, goto cleanup; s = strtok_r(tmp, " \t", &pos); while(s){ - char **tmp = realloc(strings, (nstr + 1) * sizeof(*strings)); - if(tmp == NULL) + char **tmp2 = realloc(strings, (nstr + 1) * sizeof(*strings)); + if(tmp2 == NULL) goto cleanup; - strings = tmp; + strings = tmp2; strings[nstr] = strdup(s); nstr++; if(strings[nstr-1] == NULL) @@ -527,7 +572,7 @@ krb5_config_vget_strings(krb5_context context, } if(nstr){ char **tmp = realloc(strings, (nstr + 1) * sizeof(*strings)); - if(strings == NULL) + if(tmp == NULL) goto cleanup; strings = tmp; strings[nstr] = NULL; @@ -554,7 +599,7 @@ krb5_config_get_strings(krb5_context context, return ret; } -void +void KRB5_LIB_FUNCTION krb5_config_free_strings(char **strings) { char **s = strings; @@ -565,7 +610,7 @@ krb5_config_free_strings(char **strings) free(strings); } -krb5_boolean +krb5_boolean KRB5_LIB_FUNCTION krb5_config_vget_bool_default (krb5_context context, const krb5_config_section *c, krb5_boolean def_value, @@ -581,7 +626,7 @@ krb5_config_vget_bool_default (krb5_context context, return FALSE; } -krb5_boolean +krb5_boolean KRB5_LIB_FUNCTION krb5_config_vget_bool (krb5_context context, const krb5_config_section *c, va_list args) @@ -589,7 +634,7 @@ krb5_config_vget_bool (krb5_context context, return krb5_config_vget_bool_default (context, c, FALSE, args); } -krb5_boolean +krb5_boolean KRB5_LIB_FUNCTION krb5_config_get_bool_default (krb5_context context, const krb5_config_section *c, krb5_boolean def_value, @@ -603,7 +648,7 @@ krb5_config_get_bool_default (krb5_context context, return ret; } -krb5_boolean +krb5_boolean KRB5_LIB_FUNCTION krb5_config_get_bool (krb5_context context, const krb5_config_section *c, ...) @@ -616,20 +661,24 @@ krb5_config_get_bool (krb5_context context, return ret; } -int +int KRB5_LIB_FUNCTION krb5_config_vget_time_default (krb5_context context, const krb5_config_section *c, int def_value, va_list args) { const char *str; + krb5_deltat t; + str = krb5_config_vget_string (context, c, args); if(str == NULL) return def_value; - return parse_time (str, NULL); + if (krb5_string_to_deltat(str, &t)) + return def_value; + return t; } -int +int KRB5_LIB_FUNCTION krb5_config_vget_time (krb5_context context, const krb5_config_section *c, va_list args) @@ -637,7 +686,7 @@ krb5_config_vget_time (krb5_context context, return krb5_config_vget_time_default (context, c, -1, args); } -int +int KRB5_LIB_FUNCTION krb5_config_get_time_default (krb5_context context, const krb5_config_section *c, int def_value, @@ -651,7 +700,7 @@ krb5_config_get_time_default (krb5_context context, return ret; } -int +int KRB5_LIB_FUNCTION krb5_config_get_time (krb5_context context, const krb5_config_section *c, ...) @@ -665,7 +714,7 @@ krb5_config_get_time (krb5_context context, } -int +int KRB5_LIB_FUNCTION krb5_config_vget_int_default (krb5_context context, const krb5_config_section *c, int def_value, @@ -686,7 +735,7 @@ krb5_config_vget_int_default (krb5_context context, } } -int +int KRB5_LIB_FUNCTION krb5_config_vget_int (krb5_context context, const krb5_config_section *c, va_list args) @@ -694,7 +743,7 @@ krb5_config_vget_int (krb5_context context, return krb5_config_vget_int_default (context, c, -1, args); } -int +int KRB5_LIB_FUNCTION krb5_config_get_int_default (krb5_context context, const krb5_config_section *c, int def_value, @@ -708,7 +757,7 @@ krb5_config_get_int_default (krb5_context context, return ret; } -int +int KRB5_LIB_FUNCTION krb5_config_get_int (krb5_context context, const krb5_config_section *c, ...) |