Merge pull request #203 from sdigit/patch-1
[heimdal.git] / lib / krb5 / test_canon.c
blob8389c02c685e7a9edc91ff3c2f859edb742a31ba
1 /*
2 * Copyright (c) 2011, Secure Endpoints Inc.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
32 #include "krb5_locl.h"
33 #include <err.h>
34 #include <getarg.h>
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
45 int
46 main(int argc, char **argv)
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;
68 memset(&ktent, 0, sizeof(ktent));
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;
89 if (!do_get_creds && !do_kt && !cmp_to)
90 do_get_creds++;
92 if (optind < argc)
93 hostname = argv[optind];
95 during = "init_context";
96 retval = krb5_init_context(&context);
97 if (retval) goto err;
99 during = "sn2p";
100 retval = krb5_sname_to_principal(context, hostname, "host", KRB5_NT_SRV_HST, &princ);
101 if (retval) goto err;
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);
108 if (cmp_to) {
109 krb5_boolean eq;
111 during = "parsing principal name for comparison compare";
112 retval = krb5_parse_name(context, cmp_to, &cmp_to_princ);
113 if (retval) goto err;
115 eq = krb5_principal_compare(context, princ, cmp_to_princ);
116 printf("%s %s %s\n", unparsed, eq ? "==" : "!=", cmp_to);
119 if (do_get_creds) {
120 during = "ccdefault";
121 retval = krb5_cc_default(context, &cc);
122 if (retval) goto err;
124 during = "ccprinc";
125 retval = krb5_cc_get_principal(context, cc, &me);
126 if (retval) goto err;
128 memset(&in_creds, 0, sizeof(in_creds));
129 in_creds.client = me;
130 in_creds.server = princ;
132 during = "getcreds";
133 retval = krb5_get_credentials(context, 0, cc, &in_creds, &out_creds);
134 if (retval) goto err;
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);
142 if (do_kt) {
143 during = "getting keytab";
144 retval = krb5_kt_default(context, &kt);
145 if (retval) goto err;
147 during = "getting keytab ktent";
148 retval = krb5_kt_get_entry(context, kt, princ, 0, 0, &ktent);
149 if (retval) goto err;
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);
157 ret = 0;
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);
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);