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