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