2 * Copyright (c) 2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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. */
33 #include "krb5_locl.h"
37 * Check that a closed cc still keeps it data and that it's no longer
38 * there when it's destroyed.
42 test_princ(krb5_context context
)
44 const char *princ
= "lha@SU.SE";
45 const char *princ_short
= "lha";
49 char *princ_reformed
= NULL
;
54 ret
= krb5_parse_name(context
, princ
, &p
);
56 krb5_err(context
, 1, ret
, "krb5_parse_name");
58 ret
= krb5_unparse_name(context
, p
, &princ_unparsed
);
60 krb5_err(context
, 1, ret
, "krb5_parse_name");
62 if (strcmp(princ
, princ_unparsed
)) {
63 krb5_errx(context
, 1, "%s != %s", princ
, princ_unparsed
);
68 ret
= krb5_unparse_name_flags(context
, p
,
69 KRB5_PRINCIPAL_UNPARSE_NO_REALM
,
72 krb5_err(context
, 1, ret
, "krb5_parse_name");
74 if (strcmp(princ_short
, princ_unparsed
))
75 krb5_errx(context
, 1, "%s != %s", princ_short
, princ_unparsed
);
78 realm
= krb5_principal_get_realm(context
, p
);
80 if (asprintf(&princ_reformed
, "%s@%s", princ_short
, realm
) < 0 || princ_reformed
== NULL
)
83 ret
= krb5_parse_name(context
, princ_reformed
, &p2
);
86 krb5_err(context
, 1, ret
, "krb5_parse_name");
88 if (!krb5_principal_compare(context
, p
, p2
)) {
89 krb5_errx(context
, 1, "p != p2");
92 krb5_free_principal(context
, p2
);
94 ret
= krb5_set_default_realm(context
, "SU.SE");
96 krb5_err(context
, 1, ret
, "krb5_parse_name");
98 ret
= krb5_unparse_name_flags(context
, p
,
99 KRB5_PRINCIPAL_UNPARSE_SHORT
,
102 krb5_err(context
, 1, ret
, "krb5_parse_name");
104 if (strcmp(princ_short
, princ_unparsed
))
105 krb5_errx(context
, 1, "'%s' != '%s'", princ_short
, princ_unparsed
);
106 free(princ_unparsed
);
108 ret
= krb5_parse_name(context
, princ_short
, &p2
);
110 krb5_err(context
, 1, ret
, "krb5_parse_name");
112 if (!krb5_principal_compare(context
, p
, p2
))
113 krb5_errx(context
, 1, "p != p2");
114 krb5_free_principal(context
, p2
);
116 ret
= krb5_unparse_name(context
, p
, &princ_unparsed
);
118 krb5_err(context
, 1, ret
, "krb5_parse_name");
120 if (strcmp(princ
, princ_unparsed
))
121 krb5_errx(context
, 1, "'%s' != '%s'", princ
, princ_unparsed
);
122 free(princ_unparsed
);
124 ret
= krb5_set_default_realm(context
, "SAMBA.ORG");
126 krb5_err(context
, 1, ret
, "krb5_parse_name");
128 ret
= krb5_parse_name(context
, princ_short
, &p2
);
130 krb5_err(context
, 1, ret
, "krb5_parse_name");
132 if (krb5_principal_compare(context
, p
, p2
))
133 krb5_errx(context
, 1, "p == p2");
135 if (!krb5_principal_compare_any_realm(context
, p
, p2
))
136 krb5_errx(context
, 1, "(ignoring realms) p != p2");
138 ret
= krb5_unparse_name(context
, p2
, &princ_unparsed
);
140 krb5_err(context
, 1, ret
, "krb5_parse_name");
142 if (strcmp(princ
, princ_unparsed
) == 0)
143 krb5_errx(context
, 1, "%s == %s", princ
, princ_unparsed
);
144 free(princ_unparsed
);
146 krb5_free_principal(context
, p2
);
148 ret
= krb5_parse_name(context
, princ
, &p2
);
150 krb5_err(context
, 1, ret
, "krb5_parse_name");
152 if (!krb5_principal_compare(context
, p
, p2
))
153 krb5_errx(context
, 1, "p != p2");
155 ret
= krb5_unparse_name(context
, p2
, &princ_unparsed
);
157 krb5_err(context
, 1, ret
, "krb5_parse_name");
159 if (strcmp(princ
, princ_unparsed
))
160 krb5_errx(context
, 1, "'%s' != '%s'", princ
, princ_unparsed
);
161 free(princ_unparsed
);
163 krb5_free_principal(context
, p2
);
165 ret
= krb5_unparse_name_flags(context
, p
,
166 KRB5_PRINCIPAL_UNPARSE_SHORT
,
169 krb5_err(context
, 1, ret
, "krb5_unparse_name_short");
171 if (strcmp(princ
, princ_unparsed
) != 0)
172 krb5_errx(context
, 1, "'%s' != '%s'", princ
, princ_unparsed
);
173 free(princ_unparsed
);
175 ret
= krb5_unparse_name(context
, p
, &princ_unparsed
);
177 krb5_err(context
, 1, ret
, "krb5_unparse_name_short");
179 if (strcmp(princ
, princ_unparsed
))
180 krb5_errx(context
, 1, "'%s' != '%s'", princ
, princ_unparsed
);
181 free(princ_unparsed
);
183 ret
= krb5_parse_name_flags(context
, princ
,
184 KRB5_PRINCIPAL_PARSE_NO_REALM
,
187 krb5_err(context
, 1, ret
, "Should have failed to parse %s a "
188 "short name", princ
);
190 ret
= krb5_parse_name_flags(context
, princ_short
,
191 KRB5_PRINCIPAL_PARSE_NO_REALM
,
194 krb5_err(context
, 1, ret
, "krb5_parse_name");
196 ret
= krb5_unparse_name_flags(context
, p2
,
197 KRB5_PRINCIPAL_UNPARSE_NO_REALM
,
199 krb5_free_principal(context
, p2
);
201 krb5_err(context
, 1, ret
, "krb5_unparse_name_norealm");
203 if (strcmp(princ_short
, princ_unparsed
))
204 krb5_errx(context
, 1, "'%s' != '%s'", princ_short
, princ_unparsed
);
205 free(princ_unparsed
);
207 ret
= krb5_parse_name_flags(context
, princ_short
,
208 KRB5_PRINCIPAL_PARSE_REQUIRE_REALM
,
211 krb5_err(context
, 1, ret
, "Should have failed to parse %s "
212 "because it lacked a realm", princ_short
);
214 ret
= krb5_parse_name_flags(context
, princ
,
215 KRB5_PRINCIPAL_PARSE_REQUIRE_REALM
,
218 krb5_err(context
, 1, ret
, "krb5_parse_name");
220 if (!krb5_principal_compare(context
, p
, p2
))
221 krb5_errx(context
, 1, "p != p2");
223 ret
= krb5_unparse_name_flags(context
, p2
,
224 KRB5_PRINCIPAL_UNPARSE_NO_REALM
,
226 krb5_free_principal(context
, p2
);
228 krb5_err(context
, 1, ret
, "krb5_unparse_name_norealm");
230 if (strcmp(princ_short
, princ_unparsed
))
231 krb5_errx(context
, 1, "'%s' != '%s'", princ_short
, princ_unparsed
);
232 free(princ_unparsed
);
234 krb5_free_principal(context
, p
);
238 princ
= "test\\ principal@SU.SE";
239 noquote
= "test principal@SU.SE";
241 ret
= krb5_parse_name_flags(context
, princ
, 0, &p
);
243 krb5_err(context
, 1, ret
, "krb5_parse_name");
245 ret
= krb5_unparse_name_flags(context
, p
, 0, &princ_unparsed
);
247 krb5_err(context
, 1, ret
, "krb5_unparse_name_flags");
249 if (strcmp(princ
, princ_unparsed
))
250 krb5_errx(context
, 1, "q '%s' != '%s'", princ
, princ_unparsed
);
251 free(princ_unparsed
);
253 ret
= krb5_unparse_name_flags(context
, p
, KRB5_PRINCIPAL_UNPARSE_DISPLAY
,
256 krb5_err(context
, 1, ret
, "krb5_unparse_name_flags");
258 if (strcmp(noquote
, princ_unparsed
))
259 krb5_errx(context
, 1, "nq '%s' != '%s'", noquote
, princ_unparsed
);
260 free(princ_unparsed
);
262 krb5_free_principal(context
, p
);
266 test_enterprise(krb5_context context
)
272 ret
= krb5_set_default_realm(context
, "SAMBA.ORG");
274 krb5_err(context
, 1, ret
, "krb5_parse_name");
276 ret
= krb5_parse_name_flags(context
, "lha@su.se@WIN.SU.SE",
277 KRB5_PRINCIPAL_PARSE_ENTERPRISE
, &p
);
279 krb5_err(context
, 1, ret
, "krb5_parse_name_flags");
281 ret
= krb5_unparse_name(context
, p
, &unparsed
);
283 krb5_err(context
, 1, ret
, "krb5_unparse_name");
285 krb5_free_principal(context
, p
);
287 if (strcmp(unparsed
, "lha\\@su.se@WIN.SU.SE") != 0)
288 krb5_errx(context
, 1, "enterprise name failed 1");
295 ret
= krb5_parse_name_flags(context
, "lha\\@su.se@WIN.SU.SE",
296 KRB5_PRINCIPAL_PARSE_ENTERPRISE
, &p
);
298 krb5_err(context
, 1, ret
, "krb5_parse_name_flags");
300 ret
= krb5_unparse_name(context
, p
, &unparsed
);
302 krb5_err(context
, 1, ret
, "krb5_unparse_name");
304 krb5_free_principal(context
, p
);
305 if (strcmp(unparsed
, "lha\\@su.se\\@WIN.SU.SE@SAMBA.ORG") != 0)
306 krb5_errx(context
, 1, "enterprise name failed 2: %s", unparsed
);
313 ret
= krb5_parse_name_flags(context
, "lha\\@su.se@WIN.SU.SE", 0, &p
);
315 krb5_err(context
, 1, ret
, "krb5_parse_name_flags");
317 ret
= krb5_unparse_name(context
, p
, &unparsed
);
319 krb5_err(context
, 1, ret
, "krb5_unparse_name");
321 krb5_free_principal(context
, p
);
322 if (strcmp(unparsed
, "lha\\@su.se@WIN.SU.SE") != 0)
323 krb5_errx(context
, 1, "enterprise name failed 3");
330 ret
= krb5_parse_name_flags(context
, "lha@su.se",
331 KRB5_PRINCIPAL_PARSE_ENTERPRISE
, &p
);
333 krb5_err(context
, 1, ret
, "krb5_parse_name_flags");
335 ret
= krb5_unparse_name(context
, p
, &unparsed
);
337 krb5_err(context
, 1, ret
, "krb5_unparse_name");
339 krb5_free_principal(context
, p
);
340 if (strcmp(unparsed
, "lha\\@su.se@SAMBA.ORG") != 0)
341 krb5_errx(context
, 1, "enterprise name failed 2: %s", unparsed
);
347 main(int argc
, char **argv
)
349 krb5_context context
;
352 setprogname(argv
[0]);
354 ret
= krb5_init_context(&context
);
356 errx (1, "krb5_init_context failed: %d", ret
);
360 test_enterprise(context
);
362 krb5_free_context(context
);