2 * Copyright (c) 2011, Secure Endpoints Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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"
46 main(int argc
, char **argv
)
48 krb5_error_code retval
;
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
;
58 char *hostname
= NULL
;
59 char *unparsed
= NULL
;
60 char *unparsed_canon
= NULL
;
68 memset(&ktent
, 0, sizeof(ktent
));
70 while ((opt
= getopt(argc
, argv
, "hgkc:")) != -1) {
83 fprintf(stderr
, "Usage: %s [-g] [-k] [-c compare-to-principal] "
84 "[principal]\n", argv
[0]);
89 if (!do_get_creds
&& !do_kt
&& !cmp_to
)
93 hostname
= argv
[optind
];
95 during
= "init_context";
96 retval
= krb5_init_context(&context
);
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
);
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
);
120 during
= "ccdefault";
121 retval
= krb5_cc_default(context
, &cc
);
122 if (retval
) goto err
;
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
;
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
);
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
);
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
);
166 krb5_free_creds(context
, out_creds
);
167 (void) krb5_cc_close(context
, cc
);
169 krb5_kt_free_entry(context
, &ktent
);
171 krb5_kt_close(context
, kt
);
172 krb5_free_context(context
);
174 fprintf(stderr
, "Failed while doing %s (%d)\n", during
, retval
);