From a66cb84e7061d37da983e480bcc1b55e7e7a61ca Mon Sep 17 00:00:00 2001 From: "Roland C. Dowdeswell" Date: Wed, 23 Oct 2019 22:40:57 +0100 Subject: [PATCH] In krb5.conf make underscores and dashes equivalent --- lib/base/config_file.c | 26 +++++++++++++++++++--- lib/krb5/test_config.c | 37 ++++++++++++++++++++++++++++++++ lib/krb5/test_config_strings.cfg | 5 +++++ 3 files changed, 65 insertions(+), 3 deletions(-) diff --git a/lib/base/config_file.c b/lib/base/config_file.c index f2734a83f..2bac712b4 100644 --- a/lib/base/config_file.c +++ b/lib/base/config_file.c @@ -86,6 +86,26 @@ static heim_error_code parse_list(struct fileptr *f, unsigned *lineno, heim_config_binding **parent, const char **err_message); +static int +todash(int c) +{ + if (c == '_') + return '-'; + return c; +} + +static int +strcmp_du(const char *s1, const char *s2) +{ + while (todash((unsigned char)*s1) == todash((unsigned char)*s2)) { + if (*s1 == '\0') + return 0; + s1++; + s2++; + } + return todash((unsigned char)*s1) - todash((unsigned char)*s2); +} + heim_config_section * heim_config_get_entry(heim_config_section **parent, const char *name, int type) { @@ -94,7 +114,7 @@ heim_config_get_entry(heim_config_section **parent, const char *name, int type) for (q = parent; *q != NULL; q = &(*q)->next) if (type == heim_config_list && (unsigned)type == (*q)->type && - strcmp(name, (*q)->name) == 0) + strcmp_du(name, (*q)->name) == 0) return *q; *q = calloc(1, sizeof(**q)); if (*q == NULL) @@ -788,7 +808,7 @@ vget_next(heim_context context, const char *p = va_arg(args, const char *); while (b != NULL) { - if (strcmp(b->name, name) == 0) { + if (strcmp_du(b->name, name) == 0) { if (b->type == (unsigned)type && p == NULL) { *pointer = b; return b->u.generic; @@ -826,7 +846,7 @@ heim_config_vget_next(heim_context context, /* we were called again, so just look for more entries with the same name and type */ for (b = (*pointer)->next; b != NULL; b = b->next) { - if(strcmp(b->name, (*pointer)->name) == 0 && b->type == (unsigned)type) { + if(strcmp_du(b->name, (*pointer)->name) == 0 && b->type == (unsigned)type) { *pointer = b; return b->u.generic; } diff --git a/lib/krb5/test_config.c b/lib/krb5/test_config.c index 2924a85bc..386e600b1 100644 --- a/lib/krb5/test_config.c +++ b/lib/krb5/test_config.c @@ -239,10 +239,47 @@ check_escaped_strings(void) krb5_free_context(context); } +static void +check_dash_underscore(void) +{ + krb5_context context; + krb5_config_section *c = NULL; + krb5_error_code ret; + const char *s; + char **ps; + + ret = krb5_init_context(&context); + if (ret) + errx(1, "krb5_init_context %d", ret); + + ret = krb5_config_parse_file(context, "test_config_strings.out", &c); + if (ret) + krb5_errx(context, 1, "krb5_config_parse_file()"); + + s = krb5_config_get_string(context, c, "dash-test", "final-value", NULL); + if (s == NULL || strcmp(s, "through-section") != 0) + errx(1, "dash/underscore lookup failed"); + + ps = krb5_config_get_strings(context, c, "dash-test", "same-name", NULL); + if (ps == NULL || + ps[0] == NULL || strcmp(ps[0], "first") != 0 || + ps[1] == NULL || strcmp(ps[1], "second") != 0 || + ps[2] != NULL) + errx(1, "dash/underscore iteration failed"); + krb5_config_free_strings(ps); + + ret = krb5_config_file_free(context, c); + if (ret) + krb5_errx(context, 1, "krb5_config_file_free()"); + + krb5_free_context(context); +} + int main(int argc, char **argv) { check_config_files(); check_escaped_strings(); + check_dash_underscore(); return 0; } diff --git a/lib/krb5/test_config_strings.cfg b/lib/krb5/test_config_strings.cfg index c62201b13..5a34f376d 100644 --- a/lib/krb5/test_config_strings.cfg +++ b/lib/krb5/test_config_strings.cfg @@ -10,3 +10,8 @@ internal2 = "TownOf Sandwich: Massachusetts"Oldest Town In "Cape Cod" internal3 = "Begins and"ends In One String longer_strings = "Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:" "1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer." "2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution." "3. Neither the name of the Institute nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission." "THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." "Why do we test with such long strings? Because some people have config files" That look "Like this." + +[dash_test] + final_value = through-section + same_name = first + same-name = second