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