Add heimdal-0.6.3
[dragonfly.git] / crypto / heimdal-0.6.3 / 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
35 RCSID("$Id: name-45-test.c,v 1.3.2.1 2003/05/06 16:49:14 joda Exp $");
36
37 enum { MAX_COMPONENTS = 3 };
38
39 static struct testcase {
40     const char *v4_name;
41     const char *v4_inst;
42     const char *v4_realm;
43
44     krb5_realm v5_realm;
45     unsigned ncomponents;
46     char *comp_val[MAX_COMPONENTS];
47
48     const char *config_file;
49     krb5_error_code ret;        /* expected error code from 524 */
50
51     krb5_error_code ret2;       /* expected error code from 425 */
52 } tests[] = {
53     {"", "", "", "", 1, {""}, NULL, 0, 0},
54     {"a", "", "", "", 1, {"a"}, NULL, 0, 0},
55     {"a", "b", "", "", 2, {"a", "b"}, NULL, 0, 0},
56     {"a", "b", "c", "c", 2, {"a", "b"}, NULL, 0, 0},
57
58     {"krbtgt", "FOO.SE", "FOO.SE", "FOO.SE", 2,
59      {"krbtgt", "FOO.SE"}, NULL, 0, 0},
60
61     {"foo", "bar", "BAZ", "BAZ", 2,
62      {"foo", "bar"}, NULL, 0, 0},
63     {"foo", "bar", "BAZ", "BAZ", 2,
64      {"foo", "bar"},
65      "[libdefaults]\n"
66      "  v4_name_convert = {\n"
67      "          host = {\n"
68      "                  foo = foo5\n"
69      "          }\n"
70      "}\n",
71     HEIM_ERR_V4_PRINC_NO_CONV, 0},
72     {"foo", "bar", "BAZ", "BAZ", 2,
73      {"foo5", "bar.baz"},
74      "[realms]\n"
75      "  BAZ = {\n"
76      "          v4_name_convert = {\n"
77      "                  host = {\n"
78      "                          foo = foo5\n"
79      "                  }\n"
80      "          }\n"
81      "          v4_instance_convert = {\n"
82      "                  bar = bar.baz\n"
83      "          }\n"
84      "  }\n",
85      0, 0},
86
87     {"rcmd", "foo", "realm", "realm", 2, {"host", "foo"}, NULL,
88      HEIM_ERR_V4_PRINC_NO_CONV, 0},
89     {"rcmd", "foo", "realm", "realm", 2, {"host", "foo.realm"},
90      "[realms]\n"
91      "  realm = {\n"
92      "          v4_instance_convert = {\n"
93      "                  foo = foo.realm\n"
94      "          }\n"
95      "  }\n",
96      0, 0},
97
98     {"pop", "mail0", "NADA.KTH.SE", "NADA.KTH.SE", 2,
99      {"pop", "mail0.nada.kth.se"}, "", HEIM_ERR_V4_PRINC_NO_CONV, 0},
100     {"pop", "mail0", "NADA.KTH.SE", "NADA.KTH.SE", 2,
101      {"pop", "mail0.nada.kth.se"},
102      "[realms]\n"
103      "  NADA.KTH.SE = {\n"
104      "          default_domain = nada.kth.se\n"
105      "  }\n",
106      0, 0},
107     {"pop", "mail0", "NADA.KTH.SE", "NADA.KTH.SE", 2,
108      {"pop", "mail0.nada.kth.se"},
109      "[libdefaults]\n"
110      "  v4_instance_resolve = true\n",
111      HEIM_ERR_V4_PRINC_NO_CONV, 0},
112
113     {"rcmd", "hokkigai", "NADA.KTH.SE", "NADA.KTH.SE", 2,
114      {"host", "hokkigai.pdc.kth.se"}, "", HEIM_ERR_V4_PRINC_NO_CONV, 0},
115     {"rcmd", "hokkigai", "NADA.KTH.SE", "NADA.KTH.SE", 2,
116      {"host", "hokkigai.pdc.kth.se"},
117      "[libdefaults]\n"
118      "  v4_instance_resolve = true\n"
119      "[realms]\n"
120      "  NADA.KTH.SE = {\n"
121      "          v4_name_convert = {\n"
122      "                  host = {\n"
123      "                          rcmd = host\n"
124      "                  }\n"
125      "          }\n"
126      "          default_domain = pdc.kth.se\n"
127      "  }\n",
128      0, 0},
129
130     {"0123456789012345678901234567890123456789",
131      "0123456789012345678901234567890123456789",
132      "0123456789012345678901234567890123456789",
133      "0123456789012345678901234567890123456789",
134      2, {"0123456789012345678901234567890123456789",
135          "0123456789012345678901234567890123456789"}, NULL,
136      0, KRB5_PARSE_MALFORMED},
137
138     {"012345678901234567890123456789012345678",
139      "012345678901234567890123456789012345678",
140      "012345678901234567890123456789012345678",
141      "012345678901234567890123456789012345678",
142      2, {"012345678901234567890123456789012345678",
143          "012345678901234567890123456789012345678"}, NULL,
144      0, 0},
145
146     {NULL, NULL, NULL, NULL, 0, {NULL}, NULL, 0}
147 };
148
149 int
150 main(int argc, char **argv)
151 {
152     struct testcase *t;
153     krb5_context context;
154     krb5_error_code ret;
155     int val = 0;
156
157     for (t = tests; t->v4_name; ++t) {
158         krb5_principal princ;
159         int i;
160         char name[40], inst[40], realm[40];
161         char printable_princ[256];
162
163         ret = krb5_init_context (&context);
164         if (ret)
165             errx (1, "krb5_init_context failed: %d", ret);
166
167         if (t->config_file != NULL) {
168             char template[] = "/tmp/krb5-conf-XXXXXX";
169             int fd = mkstemp(template);
170             char *files[2];
171
172             if (fd < 0)
173                 krb5_err (context, 1, errno, "mkstemp %s", template);
174
175             if (write (fd, t->config_file, strlen(t->config_file))
176                 != strlen(t->config_file))
177                 krb5_err (context, 1, errno, "write %s", template);
178             close (fd);
179             files[0] = template;
180             files[1] = NULL;
181
182             ret = krb5_set_config_files (context, files);
183             unlink (template);
184             if (ret)
185                 krb5_err (context, 1, ret, "krb5_set_config_files");
186         }
187
188         ret = krb5_425_conv_principal (context,
189                                        t->v4_name,
190                                        t->v4_inst,
191                                        t->v4_realm,
192                                        &princ);
193         if (ret) {
194             if (ret != t->ret) {
195                 krb5_warn (context, ret,
196                            "krb5_425_conv_principal %s.%s@%s",
197                            t->v4_name, t->v4_inst, t->v4_realm);
198                 val = 1;
199             }
200         } else {
201             if (t->ret) {
202                 char *s;
203                 krb5_unparse_name(context, princ, &s);
204                 krb5_warnx (context,
205                             "krb5_425_conv_principal %s.%s@%s "
206                             "passed unexpected: %s",
207                             t->v4_name, t->v4_inst, t->v4_realm, s);
208                 free(s);
209                 val = 1;
210                 continue;
211             }
212         }
213
214         if (ret)
215             continue;
216
217         if (strcmp (t->v5_realm, princ->realm) != 0) {
218             printf ("wrong realm (\"%s\" should be \"%s\")"
219                     " for \"%s.%s@%s\"\n",
220                     princ->realm, t->v5_realm,
221                     t->v4_name,
222                     t->v4_inst,
223                     t->v4_realm);
224             val = 1;
225         }
226
227         if (t->ncomponents != princ->name.name_string.len) {
228             printf ("wrong number of components (%u should be %u)"
229                     " for \"%s.%s@%s\"\n",
230                     princ->name.name_string.len, t->ncomponents,
231                     t->v4_name,
232                     t->v4_inst,
233                     t->v4_realm);
234             val = 1;
235         } else {
236             for (i = 0; i < t->ncomponents; ++i) {
237                 if (strcmp(t->comp_val[i],
238                            princ->name.name_string.val[i]) != 0) {
239                     printf ("bad component %d (\"%s\" should be \"%s\")"
240                             " for \"%s.%s@%s\"\n",
241                             i,
242                             princ->name.name_string.val[i],
243                             t->comp_val[i],
244                             t->v4_name,
245                             t->v4_inst,
246                             t->v4_realm);
247                     val = 1;
248                 }
249             }
250         }
251         ret = krb5_524_conv_principal (context, princ,
252                                        name, inst, realm);
253         if (krb5_unparse_name_fixed(context, princ,
254                                     printable_princ, sizeof(printable_princ)))
255             strlcpy(printable_princ, "unknown principal",
256                     sizeof(printable_princ));
257         if (ret) {
258             if (ret != t->ret2) {
259                 krb5_warn (context, ret,
260                            "krb5_524_conv_principal %s", printable_princ);
261                 val = 1;
262             }
263         } else {
264             if (t->ret2) {
265                 krb5_warnx (context,
266                             "krb5_524_conv_principal %s "
267                             "passed unexpected", printable_princ);
268                 val = 1;
269                 continue;
270             }
271         }
272         if (ret) {
273             krb5_free_principal (context, princ);
274             continue;
275         }
276
277         krb5_free_principal (context, princ);
278     }
279     return val;
280 }