Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / crypto / dist / heimdal / kdc / kdc-replay.c
blobbded48a228e047f839462af33512fed38f2521a5
1 /*
2 * Copyright (c) 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 #include "kdc_locl.h"
36 __RCSID("$Heimdal: kdc-replay.c 21945 2007-10-03 21:52:24Z lha $"
37 "$NetBSD$");
39 static int version_flag;
40 static int help_flag;
42 struct getargs args[] = {
43 { "version", 0, arg_flag, &version_flag },
44 { "help", 'h', arg_flag, &help_flag }
47 const static int num_args = sizeof(args) / sizeof(args[0]);
49 static void
50 usage(int ret)
52 arg_printusage (args, num_args, NULL, "kdc-request-log-file");
53 exit (ret);
56 int
57 main(int argc, char **argv)
59 krb5_error_code ret;
60 krb5_context context;
61 krb5_kdc_configuration *config;
62 krb5_storage *sp;
63 int fd, optidx = 0;
65 setprogname(argv[0]);
67 if(getarg(args, num_args, argc, argv, &optidx))
68 usage(1);
70 if(help_flag)
71 usage(0);
73 if(version_flag){
74 print_version(NULL);
75 exit(0);
78 ret = krb5_init_context(&context);
79 if (ret)
80 errx (1, "krb5_init_context failed to parse configuration file");
82 ret = krb5_kdc_get_config(context, &config);
83 if (ret)
84 krb5_err(context, 1, ret, "krb5_kdc_default_config");
86 kdc_openlog(context, config);
88 ret = krb5_kdc_set_dbinfo(context, config);
89 if (ret)
90 krb5_err(context, 1, ret, "krb5_kdc_set_dbinfo");
92 if (argc != 2)
93 errx(1, "argc != 2");
95 printf("kdc replay\n");
97 fd = open(argv[1], O_RDONLY);
98 if (fd < 0)
99 err(1, "open: %s", argv[1]);
101 sp = krb5_storage_from_fd(fd);
102 if (sp == NULL)
103 krb5_errx(context, 1, "krb5_storage_from_fd");
105 while(1) {
106 struct sockaddr_storage sa;
107 krb5_socklen_t salen = sizeof(sa);
108 struct timeval tv;
109 krb5_address a;
110 krb5_data d, r;
111 uint32_t t, clty, tag;
112 char astr[80];
114 ret = krb5_ret_uint32(sp, &t);
115 if (ret == HEIM_ERR_EOF)
116 break;
117 else if (ret)
118 krb5_errx(context, 1, "krb5_ret_uint32(version)");
119 if (t != 1)
120 krb5_errx(context, 1, "version not 1");
121 ret = krb5_ret_uint32(sp, &t);
122 if (ret)
123 krb5_errx(context, 1, "krb5_ret_uint32(time)");
124 ret = krb5_ret_address(sp, &a);
125 if (ret)
126 krb5_errx(context, 1, "krb5_ret_address");
127 ret = krb5_ret_data(sp, &d);
128 if (ret)
129 krb5_errx(context, 1, "krb5_ret_data");
130 ret = krb5_ret_uint32(sp, &clty);
131 if (ret)
132 krb5_errx(context, 1, "krb5_ret_uint32(class|type)");
133 ret = krb5_ret_uint32(sp, &tag);
134 if (ret)
135 krb5_errx(context, 1, "krb5_ret_uint32(tag)");
138 ret = krb5_addr2sockaddr (context, &a, (struct sockaddr *)&sa,
139 &salen, 88);
140 if (ret == KRB5_PROG_ATYPE_NOSUPP)
141 goto out;
142 else if (ret)
143 krb5_err(context, 1, ret, "krb5_addr2sockaddr");
145 ret = krb5_print_address(&a, astr, sizeof(astr), NULL);
146 if (ret)
147 krb5_err(context, 1, ret, "krb5_print_address");
149 printf("processing request from %s, %lu bytes\n",
150 astr, (unsigned long)d.length);
152 r.length = 0;
153 r.data = NULL;
155 tv.tv_sec = t;
156 tv.tv_usec = 0;
158 krb5_kdc_update_time(&tv);
159 krb5_set_real_time(context, tv.tv_sec, 0);
161 ret = krb5_kdc_process_request(context, config, d.data, d.length,
162 &r, NULL, astr,
163 (struct sockaddr *)&sa, 0);
164 if (ret)
165 krb5_err(context, 1, ret, "krb5_kdc_process_request");
167 if (r.length) {
168 Der_class cl;
169 Der_type ty;
170 unsigned int tag2;
171 ret = der_get_tag (r.data, r.length,
172 &cl, &ty, &tag2, NULL);
173 if (MAKE_TAG(cl, ty, 0) != clty)
174 krb5_errx(context, 1, "class|type mismatch: %d != %d",
175 (int)MAKE_TAG(cl, ty, 0), (int)clty);
176 if (tag != tag2)
177 krb5_errx(context, 1, "tag mismatch");
179 krb5_data_free(&r);
180 } else {
181 if (clty != 0xffffffff)
182 krb5_errx(context, 1, "clty not invalid");
183 if (tag != 0xffffffff)
184 krb5_errx(context, 1, "tag not invalid");
187 out:
188 krb5_data_free(&d);
189 krb5_free_address(context, &a);
192 krb5_storage_free(sp);
193 krb5_free_context(context);
195 printf("done\n");
197 return 0;