xref: /freebsd/crypto/heimdal/kdc/kdc-replay.c (revision 39beb93c3f8bdbf72a61fda42300b5ebed7390c8)
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 RCSID("$Id: kdc-replay.c 21945 2007-10-03 21:52:24Z lha $");
37 
38 static int version_flag;
39 static int help_flag;
40 
41 struct getargs args[] = {
42     { "version",   0,	arg_flag, &version_flag },
43     { "help",     'h',	arg_flag, &help_flag }
44 };
45 
46 const static int num_args = sizeof(args) / sizeof(args[0]);
47 
48 static void
49 usage(int ret)
50 {
51     arg_printusage (args, num_args, NULL, "kdc-request-log-file");
52     exit (ret);
53 }
54 
55 int
56 main(int argc, char **argv)
57 {
58     krb5_error_code ret;
59     krb5_context context;
60     krb5_kdc_configuration *config;
61     krb5_storage *sp;
62     int fd, optidx = 0;
63 
64     setprogname(argv[0]);
65 
66     if(getarg(args, num_args, argc, argv, &optidx))
67 	usage(1);
68 
69     if(help_flag)
70 	usage(0);
71 
72     if(version_flag){
73 	print_version(NULL);
74 	exit(0);
75     }
76 
77     ret = krb5_init_context(&context);
78     if (ret)
79 	errx (1, "krb5_init_context failed to parse configuration file");
80 
81     ret = krb5_kdc_get_config(context, &config);
82     if (ret)
83 	krb5_err(context, 1, ret, "krb5_kdc_default_config");
84 
85     kdc_openlog(context, config);
86 
87     ret = krb5_kdc_set_dbinfo(context, config);
88     if (ret)
89 	krb5_err(context, 1, ret, "krb5_kdc_set_dbinfo");
90 
91     if (argc != 2)
92 	errx(1, "argc != 2");
93 
94     printf("kdc replay\n");
95 
96     fd = open(argv[1], O_RDONLY);
97     if (fd < 0)
98 	err(1, "open: %s", argv[1]);
99 
100     sp = krb5_storage_from_fd(fd);
101     if (sp == NULL)
102 	krb5_errx(context, 1, "krb5_storage_from_fd");
103 
104     while(1) {
105 	struct sockaddr_storage sa;
106 	krb5_socklen_t salen = sizeof(sa);
107 	struct timeval tv;
108 	krb5_address a;
109 	krb5_data d, r;
110 	uint32_t t, clty, tag;
111 	char astr[80];
112 
113 	ret = krb5_ret_uint32(sp, &t);
114 	if (ret == HEIM_ERR_EOF)
115 	    break;
116 	else if (ret)
117 	    krb5_errx(context, 1, "krb5_ret_uint32(version)");
118 	if (t != 1)
119 	    krb5_errx(context, 1, "version not 1");
120 	ret = krb5_ret_uint32(sp, &t);
121 	if (ret)
122 	    krb5_errx(context, 1, "krb5_ret_uint32(time)");
123 	ret = krb5_ret_address(sp, &a);
124 	if (ret)
125 	    krb5_errx(context, 1, "krb5_ret_address");
126 	ret = krb5_ret_data(sp, &d);
127 	if (ret)
128 	    krb5_errx(context, 1, "krb5_ret_data");
129 	ret = krb5_ret_uint32(sp, &clty);
130 	if (ret)
131 	    krb5_errx(context, 1, "krb5_ret_uint32(class|type)");
132 	ret = krb5_ret_uint32(sp, &tag);
133 	if (ret)
134 	    krb5_errx(context, 1, "krb5_ret_uint32(tag)");
135 
136 
137 	ret = krb5_addr2sockaddr (context, &a, (struct sockaddr *)&sa,
138 				  &salen, 88);
139 	if (ret == KRB5_PROG_ATYPE_NOSUPP)
140 	    goto out;
141 	else if (ret)
142 	    krb5_err(context, 1, ret, "krb5_addr2sockaddr");
143 
144 	ret = krb5_print_address(&a, astr, sizeof(astr), NULL);
145 	if (ret)
146 	    krb5_err(context, 1, ret, "krb5_print_address");
147 
148 	printf("processing request from %s, %lu bytes\n",
149 	       astr, (unsigned long)d.length);
150 
151 	r.length = 0;
152 	r.data = NULL;
153 
154 	tv.tv_sec = t;
155 	tv.tv_usec = 0;
156 
157 	krb5_kdc_update_time(&tv);
158 	krb5_set_real_time(context, tv.tv_sec, 0);
159 
160 	ret = krb5_kdc_process_request(context, config, d.data, d.length,
161 				       &r, NULL, astr,
162 				       (struct sockaddr *)&sa, 0);
163 	if (ret)
164 	    krb5_err(context, 1, ret, "krb5_kdc_process_request");
165 
166 	if (r.length) {
167 	    Der_class cl;
168 	    Der_type ty;
169 	    unsigned int tag2;
170 	    ret = der_get_tag (r.data, r.length,
171 			       &cl, &ty, &tag2, NULL);
172 	    if (MAKE_TAG(cl, ty, 0) != clty)
173 		krb5_errx(context, 1, "class|type mismatch: %d != %d",
174 			  (int)MAKE_TAG(cl, ty, 0), (int)clty);
175 	    if (tag != tag2)
176 		krb5_errx(context, 1, "tag mismatch");
177 
178 	    krb5_data_free(&r);
179 	} else {
180 	    if (clty != 0xffffffff)
181 		krb5_errx(context, 1, "clty not invalid");
182 	    if (tag != 0xffffffff)
183 		krb5_errx(context, 1, "tag not invalid");
184 	}
185 
186     out:
187 	krb5_data_free(&d);
188 	krb5_free_address(context, &a);
189     }
190 
191     krb5_storage_free(sp);
192     krb5_free_context(context);
193 
194     printf("done\n");
195 
196     return 0;
197 }
198