1 /* $NetBSD: test_canon.c,v 1.2 2017/01/28 21:31:49 christos Exp $ */ 2 3 /* 4 * Copyright (c) 2011, Secure Endpoints Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * - Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * - Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 23 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 30 * OF THE POSSIBILITY OF SUCH DAMAGE. 31 * 32 */ 33 34 #include "krb5_locl.h" 35 #include <err.h> 36 #include <krb5/getarg.h> 37 38 #if 0 39 #include <stdio.h> 40 #include <string.h> 41 #include <strings.h> 42 #include <stdlib.h> 43 #include <unistd.h> 44 #include <krb5/krb5.h> 45 #endif 46 47 int 48 main(int argc, char **argv) 49 { 50 krb5_error_code retval; 51 krb5_context context; 52 krb5_principal princ = NULL; 53 krb5_principal me = NULL; 54 krb5_principal cmp_to_princ = NULL; 55 krb5_ccache cc = NULL; 56 krb5_creds *out_creds = NULL; 57 krb5_keytab kt = NULL; 58 krb5_keytab_entry ktent; 59 krb5_creds in_creds; 60 char *hostname = NULL; 61 char *unparsed = NULL; 62 char *unparsed_canon = NULL; 63 char *during; 64 char *cmp_to = NULL;; 65 int do_kt = 0; 66 int do_get_creds = 0; 67 int opt; 68 int ret = 1; 69 70 memset(&ktent, 0, sizeof(ktent)); 71 72 while ((opt = getopt(argc, argv, "hgkc:")) != -1) { 73 switch (opt) { 74 case 'g': 75 do_get_creds++; 76 break; 77 case 'k': 78 do_kt++; 79 break; 80 case 'c': 81 cmp_to = optarg; 82 break; 83 case 'h': 84 default: 85 fprintf(stderr, "Usage: %s [-g] [-k] [-c compare-to-principal] " 86 "[principal]\n", argv[0]); 87 return 1; 88 } 89 } 90 91 if (!do_get_creds && !do_kt && !cmp_to) 92 do_get_creds++; 93 94 if (optind < argc) 95 hostname = argv[optind]; 96 97 during = "init_context"; 98 retval = krb5_init_context(&context); 99 if (retval) goto err; 100 101 during = "sn2p"; 102 retval = krb5_sname_to_principal(context, hostname, "host", KRB5_NT_SRV_HST, &princ); 103 if (retval) goto err; 104 105 during = "unparse of sname2princ"; 106 retval = krb5_unparse_name(context, princ, &unparsed); 107 if (retval) goto err; 108 printf("krb5_sname_to_principal() output: %s\n", unparsed); 109 110 if (cmp_to) { 111 krb5_boolean eq; 112 113 during = "parsing principal name for comparison compare"; 114 retval = krb5_parse_name(context, cmp_to, &cmp_to_princ); 115 if (retval) goto err; 116 117 eq = krb5_principal_compare(context, princ, cmp_to_princ); 118 printf("%s %s %s\n", unparsed, eq ? "==" : "!=", cmp_to); 119 } 120 121 if (do_get_creds) { 122 during = "ccdefault"; 123 retval = krb5_cc_default(context, &cc); 124 if (retval) goto err; 125 126 during = "ccprinc"; 127 retval = krb5_cc_get_principal(context, cc, &me); 128 if (retval) goto err; 129 130 memset(&in_creds, 0, sizeof(in_creds)); 131 in_creds.client = me; 132 in_creds.server = princ; 133 134 during = "getcreds"; 135 retval = krb5_get_credentials(context, 0, cc, &in_creds, &out_creds); 136 if (retval) goto err; 137 138 during = "unparsing principal name canonicalized by krb5_get_credentials()"; 139 retval = krb5_unparse_name(context, in_creds.server, &unparsed_canon); 140 if (retval) goto err; 141 printf("Principal name as canonicalized by krb5_get_credentials() is %s\n", unparsed_canon); 142 } 143 144 if (do_kt) { 145 during = "getting keytab"; 146 retval = krb5_kt_default(context, &kt); 147 if (retval) goto err; 148 149 during = "getting keytab ktent"; 150 retval = krb5_kt_get_entry(context, kt, princ, 0, 0, &ktent); 151 if (retval) goto err; 152 153 during = "unparsing principal name canonicalized by krb5_kt_get_entry()"; 154 retval = krb5_unparse_name(context, ktent.principal, &unparsed_canon); 155 if (retval) goto err; 156 printf("Principal name as canonicalized by krb5_kt_get_entry() is %s\n", unparsed_canon); 157 } 158 159 ret = 0; 160 161 err: 162 krb5_free_principal(context, princ); 163 krb5_free_principal(context, me); 164 krb5_free_principal(context, cmp_to_princ); 165 krb5_xfree(unparsed); 166 krb5_xfree(unparsed_canon); 167 if (do_get_creds) { 168 krb5_free_creds(context, out_creds); 169 (void) krb5_cc_close(context, cc); 170 } 171 krb5_kt_free_entry(context, &ktent); 172 if (kt) 173 krb5_kt_close(context, kt); 174 krb5_free_context(context); 175 if (ret) 176 fprintf(stderr, "Failed while doing %s (%d)\n", during, retval); 177 return (ret); 178 } 179 180