xref: /freebsd/crypto/heimdal/kdc/kdc-replay.c (revision 0a36787e4c1fa0cf77dcf83be0867178476e372b)
1 /*
2  * Copyright (c) 2007 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include "kdc_locl.h"
35 
36 static int version_flag;
37 static int help_flag;
38 
39 struct getargs args[] = {
40     { "version",   0,	arg_flag, &version_flag },
41     { "help",     'h',	arg_flag, &help_flag }
42 };
43 
44 const static int num_args = sizeof(args) / sizeof(args[0]);
45 
46 static void
47 usage(int ret)
48 {
49     arg_printusage (args, num_args, NULL, "kdc-request-log-file");
50     exit (ret);
51 }
52 
53 int
54 main(int argc, char **argv)
55 {
56     krb5_error_code ret;
57     krb5_context context;
58     krb5_kdc_configuration *config;
59     krb5_storage *sp;
60     int fd, optidx = 0;
61 
62     setprogname(argv[0]);
63 
64     if(getarg(args, num_args, argc, argv, &optidx))
65 	usage(1);
66 
67     if(help_flag)
68 	usage(0);
69 
70     if(version_flag){
71 	print_version(NULL);
72 	exit(0);
73     }
74 
75     ret = krb5_init_context(&context);
76     if (ret)
77 	errx (1, "krb5_init_context failed to parse configuration file");
78 
79     ret = krb5_kdc_get_config(context, &config);
80     if (ret)
81 	krb5_err(context, 1, ret, "krb5_kdc_default_config");
82 
83     kdc_openlog(context, "kdc-replay", config);
84 
85     ret = krb5_kdc_set_dbinfo(context, config);
86     if (ret)
87 	krb5_err(context, 1, ret, "krb5_kdc_set_dbinfo");
88 
89 #ifdef PKINIT
90     if (config->enable_pkinit) {
91 	if (config->pkinit_kdc_identity == NULL)
92 	    krb5_errx(context, 1, "pkinit enabled but no identity");
93 
94 	if (config->pkinit_kdc_anchors == NULL)
95 	    krb5_errx(context, 1, "pkinit enabled but no X509 anchors");
96 
97 	krb5_kdc_pk_initialize(context, config,
98 			       config->pkinit_kdc_identity,
99 			       config->pkinit_kdc_anchors,
100 			       config->pkinit_kdc_cert_pool,
101 			       config->pkinit_kdc_revoke);
102 
103     }
104 #endif /* PKINIT */
105 
106     if (argc != 2)
107 	errx(1, "argc != 2");
108 
109     printf("kdc replay\n");
110 
111     fd = open(argv[1], O_RDONLY);
112     if (fd < 0)
113 	err(1, "open: %s", argv[1]);
114 
115     sp = krb5_storage_from_fd(fd);
116     if (sp == NULL)
117 	krb5_errx(context, 1, "krb5_storage_from_fd");
118 
119     while(1) {
120 	struct sockaddr_storage sa;
121 	krb5_socklen_t salen = sizeof(sa);
122 	struct timeval tv;
123 	krb5_address a;
124 	krb5_data d, r;
125 	uint32_t t, clty, tag;
126 	char astr[80];
127 
128 	ret = krb5_ret_uint32(sp, &t);
129 	if (ret == HEIM_ERR_EOF)
130 	    break;
131 	else if (ret)
132 	    krb5_errx(context, 1, "krb5_ret_uint32(version)");
133 	if (t != 1)
134 	    krb5_errx(context, 1, "version not 1");
135 	ret = krb5_ret_uint32(sp, &t);
136 	if (ret)
137 	    krb5_errx(context, 1, "krb5_ret_uint32(time)");
138 	ret = krb5_ret_address(sp, &a);
139 	if (ret)
140 	    krb5_errx(context, 1, "krb5_ret_address");
141 	ret = krb5_ret_data(sp, &d);
142 	if (ret)
143 	    krb5_errx(context, 1, "krb5_ret_data");
144 	ret = krb5_ret_uint32(sp, &clty);
145 	if (ret)
146 	    krb5_errx(context, 1, "krb5_ret_uint32(class|type)");
147 	ret = krb5_ret_uint32(sp, &tag);
148 	if (ret)
149 	    krb5_errx(context, 1, "krb5_ret_uint32(tag)");
150 
151 
152 	ret = krb5_addr2sockaddr (context, &a, (struct sockaddr *)&sa,
153 				  &salen, 88);
154 	if (ret == KRB5_PROG_ATYPE_NOSUPP)
155 	    goto out;
156 	else if (ret)
157 	    krb5_err(context, 1, ret, "krb5_addr2sockaddr");
158 
159 	ret = krb5_print_address(&a, astr, sizeof(astr), NULL);
160 	if (ret)
161 	    krb5_err(context, 1, ret, "krb5_print_address");
162 
163 	printf("processing request from %s, %lu bytes\n",
164 	       astr, (unsigned long)d.length);
165 
166 	r.length = 0;
167 	r.data = NULL;
168 
169 	tv.tv_sec = t;
170 	tv.tv_usec = 0;
171 
172 	krb5_kdc_update_time(&tv);
173 	krb5_set_real_time(context, tv.tv_sec, 0);
174 
175 	ret = krb5_kdc_process_request(context, config, d.data, d.length,
176 				       &r, NULL, astr,
177 				       (struct sockaddr *)&sa, 0);
178 	if (ret)
179 	    krb5_err(context, 1, ret, "krb5_kdc_process_request");
180 
181 	if (r.length) {
182 	    Der_class cl;
183 	    Der_type ty;
184 	    unsigned int tag2;
185 	    ret = der_get_tag (r.data, r.length,
186 			       &cl, &ty, &tag2, NULL);
187 	    if (MAKE_TAG(cl, ty, 0) != clty)
188 		krb5_errx(context, 1, "class|type mismatch: %d != %d",
189 			  (int)MAKE_TAG(cl, ty, 0), (int)clty);
190 	    if (tag != tag2)
191 		krb5_errx(context, 1, "tag mismatch");
192 
193 	    krb5_data_free(&r);
194 	} else {
195 	    if (clty != 0xffffffff)
196 		krb5_errx(context, 1, "clty not invalid");
197 	    if (tag != 0xffffffff)
198 		krb5_errx(context, 1, "tag not invalid");
199 	}
200 
201     out:
202 	krb5_data_free(&d);
203 	krb5_free_address(context, &a);
204     }
205 
206     krb5_storage_free(sp);
207     krb5_free_context(context);
208 
209     printf("done\n");
210 
211     return 0;
212 }
213