]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - crypto/heimdal/lib/krb5/name-45-test.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / crypto / heimdal / lib / krb5 / name-45-test.c
1 /*
2  * Copyright (c) 2002 - 2003 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden). 
4  * All rights reserved. 
5  *
6  * Redistribution and use in source and binary forms, with or without 
7  * modification, are permitted provided that the following conditions 
8  * are met: 
9  *
10  * 1. Redistributions of source code must retain the above copyright 
11  *    notice, this list of conditions and the following disclaimer. 
12  *
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. 
16  *
17  * 3. Neither the name of KTH nor the names of its contributors may be
18  *    used to endorse or promote products derived from this software without
19  *    specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
22  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
32
33 #include "krb5_locl.h"
34 #include <err.h>
35
36 RCSID("$Id: name-45-test.c 19763 2007-01-08 13:35:49Z lha $");
37
38 enum { MAX_COMPONENTS = 3 };
39
40 static struct testcase {
41     const char *v4_name;
42     const char *v4_inst;
43     const char *v4_realm;
44
45     krb5_realm v5_realm;
46     unsigned ncomponents;
47     char *comp_val[MAX_COMPONENTS];
48
49     const char *config_file;
50     krb5_error_code ret;        /* expected error code from 524 */
51
52     krb5_error_code ret2;       /* expected error code from 425 */
53 } tests[] = {
54     {"", "", "", "", 1, {""}, NULL, 0, 0},
55     {"a", "", "", "", 1, {"a"}, NULL, 0, 0},
56     {"a", "b", "", "", 2, {"a", "b"}, NULL, 0, 0},
57     {"a", "b", "c", "c", 2, {"a", "b"}, NULL, 0, 0},
58
59     {"krbtgt", "FOO.SE", "FOO.SE", "FOO.SE", 2,
60      {"krbtgt", "FOO.SE"}, NULL, 0, 0},
61
62     {"foo", "bar2", "BAZ", "BAZ", 2,
63      {"foo", "bar2"}, NULL, 0, 0},
64     {"foo", "bar2", "BAZ", "BAZ", 2,
65      {"foo", "bar2"},
66      "[libdefaults]\n"
67      "  v4_name_convert = {\n"
68      "          host = {\n"
69      "                  foo = foo5\n"
70      "          }\n"
71      "}\n",
72     HEIM_ERR_V4_PRINC_NO_CONV, 0},
73     {"foo", "bar2", "BAZ", "BAZ", 2,
74      {"foo5", "bar2.baz"},
75      "[realms]\n"
76      "  BAZ = {\n"
77      "          v4_name_convert = {\n"
78      "                  host = {\n"
79      "                          foo = foo5\n"
80      "                  }\n"
81      "          }\n"
82      "          v4_instance_convert = {\n"
83      "                  bar2 = bar2.baz\n"
84      "          }\n"
85      "  }\n",
86      0, 0},
87
88     {"rcmd", "foo", "realm", "realm", 2, {"host", "foo"}, NULL,
89      HEIM_ERR_V4_PRINC_NO_CONV, 0},
90     {"rcmd", "foo", "realm", "realm", 2, {"host", "foo.realm"},
91      "[realms]\n"
92      "  realm = {\n"
93      "          v4_instance_convert = {\n"
94      "                  foo = foo.realm\n"
95      "          }\n"
96      "  }\n",
97      0, 0},
98
99     {"pop", "mail0", "NADA.KTH.SE", "NADA.KTH.SE", 2,
100      {"pop", "mail0.nada.kth.se"}, "", HEIM_ERR_V4_PRINC_NO_CONV, 0},
101     {"pop", "mail0", "NADA.KTH.SE", "NADA.KTH.SE", 2,
102      {"pop", "mail0.nada.kth.se"},
103      "[realms]\n"
104      "  NADA.KTH.SE = {\n"
105      "          default_domain = nada.kth.se\n"
106      "  }\n",
107      0, 0},
108     {"pop", "mail0", "NADA.KTH.SE", "NADA.KTH.SE", 2,
109      {"pop", "mail0.nada.kth.se"},
110      "[libdefaults]\n"
111      "  v4_instance_resolve = true\n",
112      HEIM_ERR_V4_PRINC_NO_CONV, 0},
113
114     {"rcmd", "hokkigai", "NADA.KTH.SE", "NADA.KTH.SE", 2,
115      {"host", "hokkigai.pdc.kth.se"}, "", HEIM_ERR_V4_PRINC_NO_CONV, 0},
116     {"rcmd", "hokkigai", "NADA.KTH.SE", "NADA.KTH.SE", 2,
117      {"host", "hokkigai.pdc.kth.se"},
118      "[libdefaults]\n"
119      "  v4_instance_resolve = true\n"
120      "[realms]\n"
121      "  NADA.KTH.SE = {\n"
122      "          v4_name_convert = {\n"
123      "                  host = {\n"
124      "                          rcmd = host\n"
125      "                  }\n"
126      "          }\n"
127      "          default_domain = pdc.kth.se\n"
128      "  }\n",
129      0, 0},
130
131     {"0123456789012345678901234567890123456789",
132      "0123456789012345678901234567890123456789",
133      "0123456789012345678901234567890123456789",
134      "0123456789012345678901234567890123456789",
135      2, {"0123456789012345678901234567890123456789",
136          "0123456789012345678901234567890123456789"}, NULL,
137      0, KRB5_PARSE_MALFORMED},
138
139     {"012345678901234567890123456789012345678",
140      "012345678901234567890123456789012345678",
141      "012345678901234567890123456789012345678",
142      "012345678901234567890123456789012345678",
143      2, {"012345678901234567890123456789012345678",
144          "012345678901234567890123456789012345678"}, NULL,
145      0, 0},
146
147     {NULL, NULL, NULL, NULL, 0, {NULL}, NULL, 0}
148 };
149
150 int
151 main(int argc, char **argv)
152 {
153     struct testcase *t;
154     krb5_context context;
155     krb5_error_code ret;
156     char hostname[1024];
157     int val = 0;
158
159     setprogname(argv[0]);
160
161     gethostname(hostname, sizeof(hostname));
162     if (!(strstr(hostname, "kth.se") != NULL || strstr(hostname, "su.se") != NULL))
163         return 0;
164
165     for (t = tests; t->v4_name; ++t) {
166         krb5_principal princ;
167         int i;
168         char name[40], inst[40], realm[40];
169         char printable_princ[256];
170
171         ret = krb5_init_context (&context);
172         if (ret)
173             errx (1, "krb5_init_context failed: %d", ret);
174
175         if (t->config_file != NULL) {
176             char template[] = "/tmp/krb5-conf-XXXXXX";
177             int fd = mkstemp(template);
178             char *files[2];
179
180             if (fd < 0)
181                 krb5_err (context, 1, errno, "mkstemp %s", template);
182
183             if (write (fd, t->config_file, strlen(t->config_file))
184                 != strlen(t->config_file))
185                 krb5_err (context, 1, errno, "write %s", template);
186             close (fd);
187             files[0] = template;
188             files[1] = NULL;
189
190             ret = krb5_set_config_files (context, files);
191             unlink (template);
192             if (ret)
193                 krb5_err (context, 1, ret, "krb5_set_config_files");
194         }
195
196         ret = krb5_425_conv_principal (context,
197                                        t->v4_name,
198                                        t->v4_inst,
199                                        t->v4_realm,
200                                        &princ);
201         if (ret) {
202             if (ret != t->ret) {
203                 krb5_warn (context, ret,
204                            "krb5_425_conv_principal %s.%s@%s",
205                            t->v4_name, t->v4_inst, t->v4_realm);
206                 val = 1;
207             }
208         } else {
209             if (t->ret) {
210                 char *s;
211                 krb5_unparse_name(context, princ, &s);
212                 krb5_warnx (context,
213                             "krb5_425_conv_principal %s.%s@%s "
214                             "passed unexpected: %s",
215                             t->v4_name, t->v4_inst, t->v4_realm, s);
216                 free(s);
217                 val = 1;
218                 krb5_free_context(context);
219                 continue;
220             }
221         }
222
223         if (ret) {
224             krb5_free_context(context);
225             continue;
226         }
227
228         if (strcmp (t->v5_realm, princ->realm) != 0) {
229             printf ("wrong realm (\"%s\" should be \"%s\")"
230                     " for \"%s.%s@%s\"\n",
231                     princ->realm, t->v5_realm,
232                     t->v4_name,
233                     t->v4_inst,
234                     t->v4_realm);
235             val = 1;
236         }
237
238         if (t->ncomponents != princ->name.name_string.len) {
239             printf ("wrong number of components (%u should be %u)"
240                     " for \"%s.%s@%s\"\n",
241                     princ->name.name_string.len, t->ncomponents,
242                     t->v4_name,
243                     t->v4_inst,
244                     t->v4_realm);
245             val = 1;
246         } else {
247             for (i = 0; i < t->ncomponents; ++i) {
248                 if (strcmp(t->comp_val[i],
249                            princ->name.name_string.val[i]) != 0) {
250                     printf ("bad component %d (\"%s\" should be \"%s\")"
251                             " for \"%s.%s@%s\"\n",
252                             i,
253                             princ->name.name_string.val[i],
254                             t->comp_val[i],
255                             t->v4_name,
256                             t->v4_inst,
257                             t->v4_realm);
258                     val = 1;
259                 }
260             }
261         }
262         ret = krb5_524_conv_principal (context, princ,
263                                        name, inst, realm);
264         if (krb5_unparse_name_fixed(context, princ,
265                                     printable_princ, sizeof(printable_princ)))
266             strlcpy(printable_princ, "unknown principal",
267                     sizeof(printable_princ));
268         if (ret) {
269             if (ret != t->ret2) {
270                 krb5_warn (context, ret,
271                            "krb5_524_conv_principal %s", printable_princ);
272                 val = 1;
273             }
274         } else {
275             if (t->ret2) {
276                 krb5_warnx (context,
277                             "krb5_524_conv_principal %s "
278                             "passed unexpected", printable_princ);
279                 val = 1;
280                 krb5_free_context(context);
281                 continue;
282             }
283         }
284         if (ret) {
285             krb5_free_principal (context, princ);
286             krb5_free_context(context);
287             continue;
288         }
289
290         krb5_free_principal (context, princ);
291         krb5_free_context(context);
292     }
293     return val;
294 }