xref: /freebsd/crypto/heimdal/lib/krb5/test_princ.c (revision 8fc257994d0ce2396196d7a06d50d20c8015f4b7)
1 /*
2  * Copyright (c) 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: test_princ.c 22071 2007-11-14 20:04:50Z lha $");
37 
38 /*
39  * Check that a closed cc still keeps it data and that it's no longer
40  * there when it's destroyed.
41  */
42 
43 static void
44 test_princ(krb5_context context)
45 {
46     const char *princ = "lha@SU.SE";
47     const char *princ_short = "lha";
48     const char *noquote;
49     krb5_error_code ret;
50     char *princ_unparsed;
51     char *princ_reformed = NULL;
52     const char *realm;
53 
54     krb5_principal p, p2;
55 
56     ret = krb5_parse_name(context, princ, &p);
57     if (ret)
58 	krb5_err(context, 1, ret, "krb5_parse_name");
59 
60     ret = krb5_unparse_name(context, p, &princ_unparsed);
61     if (ret)
62 	krb5_err(context, 1, ret, "krb5_parse_name");
63 
64     if (strcmp(princ, princ_unparsed)) {
65 	krb5_errx(context, 1, "%s != %s", princ, princ_unparsed);
66     }
67 
68     free(princ_unparsed);
69 
70     ret = krb5_unparse_name_flags(context, p,
71 				  KRB5_PRINCIPAL_UNPARSE_NO_REALM,
72 				  &princ_unparsed);
73     if (ret)
74 	krb5_err(context, 1, ret, "krb5_parse_name");
75 
76     if (strcmp(princ_short, princ_unparsed))
77 	krb5_errx(context, 1, "%s != %s", princ_short, princ_unparsed);
78     free(princ_unparsed);
79 
80     realm = krb5_principal_get_realm(context, p);
81 
82     asprintf(&princ_reformed, "%s@%s", princ_short, realm);
83 
84     ret = krb5_parse_name(context, princ_reformed, &p2);
85     free(princ_reformed);
86     if (ret)
87 	krb5_err(context, 1, ret, "krb5_parse_name");
88 
89     if (!krb5_principal_compare(context, p, p2)) {
90 	krb5_errx(context, 1, "p != p2");
91     }
92 
93     krb5_free_principal(context, p2);
94 
95     ret = krb5_set_default_realm(context, "SU.SE");
96     if (ret)
97 	krb5_err(context, 1, ret, "krb5_parse_name");
98 
99     ret = krb5_unparse_name_flags(context, p,
100 				  KRB5_PRINCIPAL_UNPARSE_SHORT,
101 				  &princ_unparsed);
102     if (ret)
103 	krb5_err(context, 1, ret, "krb5_parse_name");
104 
105     if (strcmp(princ_short, princ_unparsed))
106 	krb5_errx(context, 1, "'%s' != '%s'", princ_short, princ_unparsed);
107     free(princ_unparsed);
108 
109     ret = krb5_parse_name(context, princ_short, &p2);
110     if (ret)
111 	krb5_err(context, 1, ret, "krb5_parse_name");
112 
113     if (!krb5_principal_compare(context, p, p2))
114 	krb5_errx(context, 1, "p != p2");
115     krb5_free_principal(context, p2);
116 
117     ret = krb5_unparse_name(context, p, &princ_unparsed);
118     if (ret)
119 	krb5_err(context, 1, ret, "krb5_parse_name");
120 
121     if (strcmp(princ, princ_unparsed))
122 	krb5_errx(context, 1, "'%s' != '%s'", princ, princ_unparsed);
123     free(princ_unparsed);
124 
125     ret = krb5_set_default_realm(context, "SAMBA.ORG");
126     if (ret)
127 	krb5_err(context, 1, ret, "krb5_parse_name");
128 
129     ret = krb5_parse_name(context, princ_short, &p2);
130     if (ret)
131 	krb5_err(context, 1, ret, "krb5_parse_name");
132 
133     if (krb5_principal_compare(context, p, p2))
134 	krb5_errx(context, 1, "p == p2");
135 
136     if (!krb5_principal_compare_any_realm(context, p, p2))
137 	krb5_errx(context, 1, "(ignoring realms) p != p2");
138 
139     ret = krb5_unparse_name(context, p2, &princ_unparsed);
140     if (ret)
141 	krb5_err(context, 1, ret, "krb5_parse_name");
142 
143     if (strcmp(princ, princ_unparsed) == 0)
144 	krb5_errx(context, 1, "%s == %s", princ, princ_unparsed);
145     free(princ_unparsed);
146 
147     krb5_free_principal(context, p2);
148 
149     ret = krb5_parse_name(context, princ, &p2);
150     if (ret)
151 	krb5_err(context, 1, ret, "krb5_parse_name");
152 
153     if (!krb5_principal_compare(context, p, p2))
154 	krb5_errx(context, 1, "p != p2");
155 
156     ret = krb5_unparse_name(context, p2, &princ_unparsed);
157     if (ret)
158 	krb5_err(context, 1, ret, "krb5_parse_name");
159 
160     if (strcmp(princ, princ_unparsed))
161 	krb5_errx(context, 1, "'%s' != '%s'", princ, princ_unparsed);
162     free(princ_unparsed);
163 
164     krb5_free_principal(context, p2);
165 
166     ret = krb5_unparse_name_flags(context, p,
167 				  KRB5_PRINCIPAL_UNPARSE_SHORT,
168 				  &princ_unparsed);
169     if (ret)
170 	krb5_err(context, 1, ret, "krb5_unparse_name_short");
171 
172     if (strcmp(princ, princ_unparsed) != 0)
173 	krb5_errx(context, 1, "'%s' != '%s'", princ, princ_unparsed);
174     free(princ_unparsed);
175 
176     ret = krb5_unparse_name(context, p, &princ_unparsed);
177     if (ret)
178 	krb5_err(context, 1, ret, "krb5_unparse_name_short");
179 
180     if (strcmp(princ, princ_unparsed))
181 	krb5_errx(context, 1, "'%s' != '%s'", princ, princ_unparsed);
182     free(princ_unparsed);
183 
184     ret = krb5_parse_name_flags(context, princ,
185 				KRB5_PRINCIPAL_PARSE_NO_REALM,
186 				&p2);
187     if (!ret)
188 	krb5_err(context, 1, ret, "Should have failed to parse %s a "
189 		 "short name", princ);
190 
191     ret = krb5_parse_name_flags(context, princ_short,
192 				KRB5_PRINCIPAL_PARSE_NO_REALM,
193 				&p2);
194     if (ret)
195 	krb5_err(context, 1, ret, "krb5_parse_name");
196 
197     ret = krb5_unparse_name_flags(context, p2,
198 				  KRB5_PRINCIPAL_UNPARSE_NO_REALM,
199 				  &princ_unparsed);
200     krb5_free_principal(context, p2);
201     if (ret)
202 	krb5_err(context, 1, ret, "krb5_unparse_name_norealm");
203 
204     if (strcmp(princ_short, princ_unparsed))
205 	krb5_errx(context, 1, "'%s' != '%s'", princ_short, princ_unparsed);
206     free(princ_unparsed);
207 
208     ret = krb5_parse_name_flags(context, princ_short,
209 				KRB5_PRINCIPAL_PARSE_MUST_REALM,
210 				&p2);
211     if (!ret)
212 	krb5_err(context, 1, ret, "Should have failed to parse %s "
213 		 "because it lacked a realm", princ_short);
214 
215     ret = krb5_parse_name_flags(context, princ,
216 				KRB5_PRINCIPAL_PARSE_MUST_REALM,
217 				&p2);
218     if (ret)
219 	krb5_err(context, 1, ret, "krb5_parse_name");
220 
221     if (!krb5_principal_compare(context, p, p2))
222 	krb5_errx(context, 1, "p != p2");
223 
224     ret = krb5_unparse_name_flags(context, p2,
225 				  KRB5_PRINCIPAL_UNPARSE_NO_REALM,
226 				  &princ_unparsed);
227     krb5_free_principal(context, p2);
228     if (ret)
229 	krb5_err(context, 1, ret, "krb5_unparse_name_norealm");
230 
231     if (strcmp(princ_short, princ_unparsed))
232 	krb5_errx(context, 1, "'%s' != '%s'", princ_short, princ_unparsed);
233     free(princ_unparsed);
234 
235     krb5_free_principal(context, p);
236 
237     /* test quoting */
238 
239     princ = "test\\ principal@SU.SE";
240     noquote = "test principal@SU.SE";
241 
242     ret = krb5_parse_name_flags(context, princ, 0, &p);
243     if (ret)
244 	krb5_err(context, 1, ret, "krb5_parse_name");
245 
246     ret = krb5_unparse_name_flags(context, p, 0, &princ_unparsed);
247     if (ret)
248 	krb5_err(context, 1, ret, "krb5_unparse_name_flags");
249 
250     if (strcmp(princ, princ_unparsed))
251 	krb5_errx(context, 1, "q '%s' != '%s'", princ, princ_unparsed);
252     free(princ_unparsed);
253 
254     ret = krb5_unparse_name_flags(context, p, KRB5_PRINCIPAL_UNPARSE_DISPLAY,
255 				  &princ_unparsed);
256     if (ret)
257 	krb5_err(context, 1, ret, "krb5_unparse_name_flags");
258 
259     if (strcmp(noquote, princ_unparsed))
260 	krb5_errx(context, 1, "nq '%s' != '%s'", noquote, princ_unparsed);
261     free(princ_unparsed);
262 
263     krb5_free_principal(context, p);
264 }
265 
266 static void
267 test_enterprise(krb5_context context)
268 {
269     krb5_error_code ret;
270     char *unparsed;
271     krb5_principal p;
272 
273     ret = krb5_set_default_realm(context, "SAMBA.ORG");
274     if (ret)
275 	krb5_err(context, 1, ret, "krb5_parse_name");
276 
277     ret = krb5_parse_name_flags(context, "lha@su.se@WIN.SU.SE",
278 				KRB5_PRINCIPAL_PARSE_ENTERPRISE, &p);
279     if (ret)
280 	krb5_err(context, 1, ret, "krb5_parse_name_flags");
281 
282     ret = krb5_unparse_name(context, p, &unparsed);
283     if (ret)
284 	krb5_err(context, 1, ret, "krb5_unparse_name");
285 
286     krb5_free_principal(context, p);
287 
288     if (strcmp(unparsed, "lha\\@su.se@WIN.SU.SE") != 0)
289 	krb5_errx(context, 1, "enterprise name failed 1");
290     free(unparsed);
291 
292     /*
293      *
294      */
295 
296     ret = krb5_parse_name_flags(context, "lha\\@su.se@WIN.SU.SE",
297 				KRB5_PRINCIPAL_PARSE_ENTERPRISE, &p);
298     if (ret)
299 	krb5_err(context, 1, ret, "krb5_parse_name_flags");
300 
301     ret = krb5_unparse_name(context, p, &unparsed);
302     if (ret)
303 	krb5_err(context, 1, ret, "krb5_unparse_name");
304 
305     krb5_free_principal(context, p);
306     if (strcmp(unparsed, "lha\\@su.se\\@WIN.SU.SE@SAMBA.ORG") != 0)
307 	krb5_errx(context, 1, "enterprise name failed 2: %s", unparsed);
308     free(unparsed);
309 
310     /*
311      *
312      */
313 
314     ret = krb5_parse_name_flags(context, "lha\\@su.se@WIN.SU.SE", 0, &p);
315     if (ret)
316 	krb5_err(context, 1, ret, "krb5_parse_name_flags");
317 
318     ret = krb5_unparse_name(context, p, &unparsed);
319     if (ret)
320 	krb5_err(context, 1, ret, "krb5_unparse_name");
321 
322     krb5_free_principal(context, p);
323     if (strcmp(unparsed, "lha\\@su.se@WIN.SU.SE") != 0)
324 	krb5_errx(context, 1, "enterprise name failed 3");
325     free(unparsed);
326 
327     /*
328      *
329      */
330 
331     ret = krb5_parse_name_flags(context, "lha@su.se",
332 				KRB5_PRINCIPAL_PARSE_ENTERPRISE, &p);
333     if (ret)
334 	krb5_err(context, 1, ret, "krb5_parse_name_flags");
335 
336     ret = krb5_unparse_name(context, p, &unparsed);
337     if (ret)
338 	krb5_err(context, 1, ret, "krb5_unparse_name");
339 
340     krb5_free_principal(context, p);
341     if (strcmp(unparsed, "lha\\@su.se@SAMBA.ORG") != 0)
342 	krb5_errx(context, 1, "enterprise name failed 2: %s", unparsed);
343     free(unparsed);
344 }
345 
346 
347 int
348 main(int argc, char **argv)
349 {
350     krb5_context context;
351     krb5_error_code ret;
352 
353     setprogname(argv[0]);
354 
355     ret = krb5_init_context(&context);
356     if (ret)
357 	errx (1, "krb5_init_context failed: %d", ret);
358 
359     test_princ(context);
360 
361     test_enterprise(context);
362 
363     krb5_free_context(context);
364 
365     return 0;
366 }
367