1c19800e8SDoug Rabson /*
2*ae771770SStanislav Sedov * Copyright (c) 2003 Kungliga Tekniska Högskolan
3c19800e8SDoug Rabson * (Royal Institute of Technology, Stockholm, Sweden).
4c19800e8SDoug Rabson * All rights reserved.
5c19800e8SDoug Rabson *
6c19800e8SDoug Rabson * Redistribution and use in source and binary forms, with or without
7c19800e8SDoug Rabson * modification, are permitted provided that the following conditions
8c19800e8SDoug Rabson * are met:
9c19800e8SDoug Rabson *
10c19800e8SDoug Rabson * 1. Redistributions of source code must retain the above copyright
11c19800e8SDoug Rabson * notice, this list of conditions and the following disclaimer.
12c19800e8SDoug Rabson *
13c19800e8SDoug Rabson * 2. Redistributions in binary form must reproduce the above copyright
14c19800e8SDoug Rabson * notice, this list of conditions and the following disclaimer in the
15c19800e8SDoug Rabson * documentation and/or other materials provided with the distribution.
16c19800e8SDoug Rabson *
17c19800e8SDoug Rabson * 3. Neither the name of KTH nor the names of its contributors may be
18c19800e8SDoug Rabson * used to endorse or promote products derived from this software without
19c19800e8SDoug Rabson * specific prior written permission.
20c19800e8SDoug Rabson *
21c19800e8SDoug Rabson * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
22c19800e8SDoug Rabson * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23c19800e8SDoug Rabson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24c19800e8SDoug Rabson * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
25c19800e8SDoug Rabson * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26c19800e8SDoug Rabson * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27c19800e8SDoug Rabson * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28c19800e8SDoug Rabson * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29c19800e8SDoug Rabson * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30c19800e8SDoug Rabson * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31c19800e8SDoug Rabson * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32c19800e8SDoug Rabson */
33c19800e8SDoug Rabson
34c19800e8SDoug Rabson #include "krb5_locl.h"
35c19800e8SDoug Rabson #include <err.h>
36c19800e8SDoug Rabson
37c19800e8SDoug Rabson static int
check_config_file(krb5_context context,char * filelist,char ** res,int def)38c19800e8SDoug Rabson check_config_file(krb5_context context, char *filelist, char **res, int def)
39c19800e8SDoug Rabson {
40c19800e8SDoug Rabson krb5_error_code ret;
41c19800e8SDoug Rabson char **pp;
42c19800e8SDoug Rabson int i;
43c19800e8SDoug Rabson
44c19800e8SDoug Rabson pp = NULL;
45c19800e8SDoug Rabson
46c19800e8SDoug Rabson if (def)
47c19800e8SDoug Rabson ret = krb5_prepend_config_files_default(filelist, &pp);
48c19800e8SDoug Rabson else
49c19800e8SDoug Rabson ret = krb5_prepend_config_files(filelist, NULL, &pp);
50c19800e8SDoug Rabson
51c19800e8SDoug Rabson if (ret)
52c19800e8SDoug Rabson krb5_err(context, 1, ret, "prepend_config_files");
53c19800e8SDoug Rabson
54c19800e8SDoug Rabson for (i = 0; res[i] && pp[i]; i++)
55c19800e8SDoug Rabson if (strcmp(pp[i], res[i]) != 0)
56c19800e8SDoug Rabson krb5_errx(context, 1, "'%s' != '%s'", pp[i], res[i]);
57c19800e8SDoug Rabson
58c19800e8SDoug Rabson if (res[i] != NULL)
59c19800e8SDoug Rabson krb5_errx(context, 1, "pp ended before res list");
60c19800e8SDoug Rabson
61c19800e8SDoug Rabson if (def) {
62c19800e8SDoug Rabson char **deflist;
63c19800e8SDoug Rabson int j;
64c19800e8SDoug Rabson
65c19800e8SDoug Rabson ret = krb5_get_default_config_files(&deflist);
66c19800e8SDoug Rabson if (ret)
67c19800e8SDoug Rabson krb5_err(context, 1, ret, "get_default_config_files");
68c19800e8SDoug Rabson
69c19800e8SDoug Rabson for (j = 0 ; pp[i] && deflist[j]; i++, j++)
70c19800e8SDoug Rabson if (strcmp(pp[i], deflist[j]) != 0)
71c19800e8SDoug Rabson krb5_errx(context, 1, "'%s' != '%s'", pp[i], deflist[j]);
72c19800e8SDoug Rabson
73c19800e8SDoug Rabson if (deflist[j] != NULL)
74c19800e8SDoug Rabson krb5_errx(context, 1, "pp ended before def list");
75c19800e8SDoug Rabson krb5_free_config_files(deflist);
76c19800e8SDoug Rabson }
77c19800e8SDoug Rabson
78c19800e8SDoug Rabson if (pp[i] != NULL)
79c19800e8SDoug Rabson krb5_errx(context, 1, "pp ended after res (and def) list");
80c19800e8SDoug Rabson
81c19800e8SDoug Rabson krb5_free_config_files(pp);
82c19800e8SDoug Rabson
83c19800e8SDoug Rabson return 0;
84c19800e8SDoug Rabson }
85c19800e8SDoug Rabson
86c19800e8SDoug Rabson char *list0[] = { "/tmp/foo", NULL };
87c19800e8SDoug Rabson char *list1[] = { "/tmp/foo", "/tmp/foo/bar", NULL };
88c19800e8SDoug Rabson char *list2[] = { "", NULL };
89c19800e8SDoug Rabson
90c19800e8SDoug Rabson struct {
91c19800e8SDoug Rabson char *fl;
92c19800e8SDoug Rabson char **res;
93c19800e8SDoug Rabson } test[] = {
94c19800e8SDoug Rabson { "/tmp/foo", NULL },
95*ae771770SStanislav Sedov { "/tmp/foo" PATH_SEP "/tmp/foo/bar", NULL },
96c19800e8SDoug Rabson { "", NULL }
97c19800e8SDoug Rabson };
98c19800e8SDoug Rabson
99*ae771770SStanislav Sedov static void
check_config_files(void)100*ae771770SStanislav Sedov check_config_files(void)
101c19800e8SDoug Rabson {
102c19800e8SDoug Rabson krb5_context context;
103c19800e8SDoug Rabson krb5_error_code ret;
104c19800e8SDoug Rabson int i;
105c19800e8SDoug Rabson
106c19800e8SDoug Rabson ret = krb5_init_context(&context);
107c19800e8SDoug Rabson if (ret)
108c19800e8SDoug Rabson errx(1, "krb5_init_context %d", ret);
109c19800e8SDoug Rabson
110c19800e8SDoug Rabson test[0].res = list0;
111c19800e8SDoug Rabson test[1].res = list1;
112c19800e8SDoug Rabson test[2].res = list2;
113c19800e8SDoug Rabson
114c19800e8SDoug Rabson for (i = 0; i < sizeof(test)/sizeof(*test); i++) {
115c19800e8SDoug Rabson check_config_file(context, test[i].fl, test[i].res, 0);
116c19800e8SDoug Rabson check_config_file(context, test[i].fl, test[i].res, 1);
117c19800e8SDoug Rabson }
118c19800e8SDoug Rabson
119c19800e8SDoug Rabson krb5_free_context(context);
120*ae771770SStanislav Sedov }
121c19800e8SDoug Rabson
122*ae771770SStanislav Sedov const char *config_string_result0[] = {
123*ae771770SStanislav Sedov "A", "B", "C", "D", NULL
124*ae771770SStanislav Sedov };
125*ae771770SStanislav Sedov
126*ae771770SStanislav Sedov const char *config_string_result1[] = {
127*ae771770SStanislav Sedov "A", "B", "C D", NULL
128*ae771770SStanislav Sedov };
129*ae771770SStanislav Sedov
130*ae771770SStanislav Sedov const char *config_string_result2[] = {
131*ae771770SStanislav Sedov "A", "B", "", NULL
132*ae771770SStanislav Sedov };
133*ae771770SStanislav Sedov
134*ae771770SStanislav Sedov const char *config_string_result3[] = {
135*ae771770SStanislav Sedov "A B;C: D", NULL
136*ae771770SStanislav Sedov };
137*ae771770SStanislav Sedov
138*ae771770SStanislav Sedov const char *config_string_result4[] = {
139*ae771770SStanislav Sedov "\"\"", "", "\"\"", NULL
140*ae771770SStanislav Sedov };
141*ae771770SStanislav Sedov
142*ae771770SStanislav Sedov const char *config_string_result5[] = {
143*ae771770SStanislav Sedov "A\"BQd", NULL
144*ae771770SStanislav Sedov };
145*ae771770SStanislav Sedov
146*ae771770SStanislav Sedov const char *config_string_result6[] = {
147*ae771770SStanislav Sedov "efgh\"", "ABC", NULL
148*ae771770SStanislav Sedov };
149*ae771770SStanislav Sedov
150*ae771770SStanislav Sedov const char *config_string_result7[] = {
151*ae771770SStanislav Sedov "SnapeKills\\", "Dumbledore", NULL
152*ae771770SStanislav Sedov };
153*ae771770SStanislav Sedov
154*ae771770SStanislav Sedov const char *config_string_result8[] = {
155*ae771770SStanislav Sedov "\"TownOf Sandwich: Massachusetts\"Oldest", "Town", "In", "Cape Cod", NULL
156*ae771770SStanislav Sedov };
157*ae771770SStanislav Sedov
158*ae771770SStanislav Sedov const char *config_string_result9[] = {
159*ae771770SStanislav Sedov "\"Begins and\"ends", "In", "One", "String", NULL
160*ae771770SStanislav Sedov };
161*ae771770SStanislav Sedov
162*ae771770SStanislav Sedov const char *config_string_result10[] = {
163*ae771770SStanislav Sedov "Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:",
164*ae771770SStanislav Sedov "1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.",
165*ae771770SStanislav Sedov "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.",
166*ae771770SStanislav Sedov "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.",
167*ae771770SStanislav Sedov "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.",
168*ae771770SStanislav Sedov "Why do we test with such long strings? Because some people have config files",
169*ae771770SStanislav Sedov "That", "look", "Like this.", NULL
170*ae771770SStanislav Sedov };
171*ae771770SStanislav Sedov
172*ae771770SStanislav Sedov const struct {
173*ae771770SStanislav Sedov const char * name;
174*ae771770SStanislav Sedov const char ** expected;
175*ae771770SStanislav Sedov } config_strings_tests[] = {
176*ae771770SStanislav Sedov { "foo", config_string_result0 },
177*ae771770SStanislav Sedov { "bar", config_string_result1 },
178*ae771770SStanislav Sedov { "baz", config_string_result2 },
179*ae771770SStanislav Sedov { "quux", config_string_result3 },
180*ae771770SStanislav Sedov { "questionable", config_string_result4 },
181*ae771770SStanislav Sedov { "mismatch1", config_string_result5 },
182*ae771770SStanislav Sedov { "mismatch2", config_string_result6 },
183*ae771770SStanislav Sedov { "internal1", config_string_result7 },
184*ae771770SStanislav Sedov { "internal2", config_string_result8 },
185*ae771770SStanislav Sedov { "internal3", config_string_result9 },
186*ae771770SStanislav Sedov { "longer_strings", config_string_result10 }
187*ae771770SStanislav Sedov };
188*ae771770SStanislav Sedov
189*ae771770SStanislav Sedov static void
check_escaped_strings(void)190*ae771770SStanislav Sedov check_escaped_strings(void)
191*ae771770SStanislav Sedov {
192*ae771770SStanislav Sedov krb5_context context;
193*ae771770SStanislav Sedov krb5_config_section *c = NULL;
194*ae771770SStanislav Sedov krb5_error_code ret;
195*ae771770SStanislav Sedov int i;
196*ae771770SStanislav Sedov
197*ae771770SStanislav Sedov ret = krb5_init_context(&context);
198*ae771770SStanislav Sedov if (ret)
199*ae771770SStanislav Sedov errx(1, "krb5_init_context %d", ret);
200*ae771770SStanislav Sedov
201*ae771770SStanislav Sedov ret = krb5_config_parse_file(context, "test_config_strings.out", &c);
202*ae771770SStanislav Sedov if (ret)
203*ae771770SStanislav Sedov krb5_errx(context, 1, "krb5_config_parse_file()");
204*ae771770SStanislav Sedov
205*ae771770SStanislav Sedov for (i=0; i < sizeof(config_strings_tests)/sizeof(config_strings_tests[0]); i++) {
206*ae771770SStanislav Sedov char **ps;
207*ae771770SStanislav Sedov const char **s;
208*ae771770SStanislav Sedov const char **e;
209*ae771770SStanislav Sedov
210*ae771770SStanislav Sedov ps = krb5_config_get_strings(context, c, "escapes", config_strings_tests[i].name,
211*ae771770SStanislav Sedov NULL);
212*ae771770SStanislav Sedov if (ps == NULL)
213*ae771770SStanislav Sedov errx(1, "Failed to read string value %s", config_strings_tests[i].name);
214*ae771770SStanislav Sedov
215*ae771770SStanislav Sedov e = config_strings_tests[i].expected;
216*ae771770SStanislav Sedov
217*ae771770SStanislav Sedov for (s = (const char **)ps; *s && *e; s++, e++) {
218*ae771770SStanislav Sedov if (strcmp(*s, *e))
219*ae771770SStanislav Sedov errx(1,
220*ae771770SStanislav Sedov "Unexpected configuration string at value [%s].\n"
221*ae771770SStanislav Sedov "Actual=[%s]\n"
222*ae771770SStanislav Sedov "Expected=[%s]\n",
223*ae771770SStanislav Sedov config_strings_tests[i].name, *s, *e);
224*ae771770SStanislav Sedov }
225*ae771770SStanislav Sedov
226*ae771770SStanislav Sedov if (*s || *e)
227*ae771770SStanislav Sedov errx(1, "Configuation string list for value [%s] has incorrect length.",
228*ae771770SStanislav Sedov config_strings_tests[i].name);
229*ae771770SStanislav Sedov
230*ae771770SStanislav Sedov krb5_config_free_strings(ps);
231*ae771770SStanislav Sedov }
232*ae771770SStanislav Sedov
233*ae771770SStanislav Sedov ret = krb5_config_file_free(context, c);
234*ae771770SStanislav Sedov if (ret)
235*ae771770SStanislav Sedov krb5_errx(context, 1, "krb5_config_file_free()");
236*ae771770SStanislav Sedov
237*ae771770SStanislav Sedov krb5_free_context(context);
238*ae771770SStanislav Sedov }
239*ae771770SStanislav Sedov
240*ae771770SStanislav Sedov int
main(int argc,char ** argv)241*ae771770SStanislav Sedov main(int argc, char **argv)
242*ae771770SStanislav Sedov {
243*ae771770SStanislav Sedov check_config_files();
244*ae771770SStanislav Sedov check_escaped_strings();
245c19800e8SDoug Rabson return 0;
246c19800e8SDoug Rabson }
247