Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / crypto / dist / heimdal / kdc / process.c
blobf87655e619b1421fc280b3ffd9d57e070aefaec3
1 /*
2 * Copyright (c) 1997-2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the Institute nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 #include "kdc_locl.h"
37 __RCSID("$Heimdal: process.c 20959 2007-06-07 04:46:06Z lha $"
38 "$NetBSD$");
44 void
45 krb5_kdc_update_time(struct timeval *tv)
47 if (tv == NULL)
48 gettimeofday(&_kdc_now, NULL);
49 else
50 _kdc_now = *tv;
54 * handle the request in `buf, len', from `addr' (or `from' as a string),
55 * sending a reply in `reply'.
58 int
59 krb5_kdc_process_request(krb5_context context,
60 krb5_kdc_configuration *config,
61 unsigned char *buf,
62 size_t len,
63 krb5_data *reply,
64 krb5_boolean *prependlength,
65 const char *from,
66 struct sockaddr *addr,
67 int datagram_reply)
69 KDC_REQ req;
70 Ticket ticket;
71 DigestREQ digestreq;
72 Kx509Request kx509req;
73 krb5_error_code ret;
74 size_t i;
76 if(decode_AS_REQ(buf, len, &req, &i) == 0){
77 krb5_data req_buffer;
79 req_buffer.data = buf;
80 req_buffer.length = len;
82 ret = _kdc_as_rep(context, config, &req, &req_buffer,
83 reply, from, addr, datagram_reply);
84 free_AS_REQ(&req);
85 return ret;
86 }else if(decode_TGS_REQ(buf, len, &req, &i) == 0){
87 ret = _kdc_tgs_rep(context, config, &req, reply, from, addr, datagram_reply);
88 free_TGS_REQ(&req);
89 return ret;
90 }else if(decode_Ticket(buf, len, &ticket, &i) == 0){
91 ret = _kdc_do_524(context, config, &ticket, reply, from, addr);
92 free_Ticket(&ticket);
93 return ret;
94 }else if(decode_DigestREQ(buf, len, &digestreq, &i) == 0){
95 ret = _kdc_do_digest(context, config, &digestreq, reply, from, addr);
96 free_DigestREQ(&digestreq);
97 return ret;
98 } else if (_kdc_try_kx509_request(buf, len, &kx509req, &i) == 0) {
99 ret = _kdc_do_kx509(context, config, &kx509req, reply, from, addr);
100 free_Kx509Request(&kx509req);
101 return ret;
102 } else if(_kdc_maybe_version4(buf, len)){
103 *prependlength = FALSE; /* elbitapmoc sdrawkcab XXX */
104 _kdc_do_version4(context, config, buf, len, reply, from,
105 (struct sockaddr_in*)addr);
106 return 0;
107 } else if (config->enable_kaserver) {
108 ret = _kdc_do_kaserver(context, config, buf, len, reply, from,
109 (struct sockaddr_in*)addr);
110 return ret;
113 return -1;
117 * handle the request in `buf, len', from `addr' (or `from' as a string),
118 * sending a reply in `reply'.
120 * This only processes krb5 requests
124 krb5_kdc_process_krb5_request(krb5_context context,
125 krb5_kdc_configuration *config,
126 unsigned char *buf,
127 size_t len,
128 krb5_data *reply,
129 const char *from,
130 struct sockaddr *addr,
131 int datagram_reply)
133 KDC_REQ req;
134 krb5_error_code ret;
135 size_t i;
137 if(decode_AS_REQ(buf, len, &req, &i) == 0){
138 krb5_data req_buffer;
140 req_buffer.data = buf;
141 req_buffer.length = len;
143 ret = _kdc_as_rep(context, config, &req, &req_buffer,
144 reply, from, addr, datagram_reply);
145 free_AS_REQ(&req);
146 return ret;
147 }else if(decode_TGS_REQ(buf, len, &req, &i) == 0){
148 ret = _kdc_tgs_rep(context, config, &req, reply, from, addr, datagram_reply);
149 free_TGS_REQ(&req);
150 return ret;
152 return -1;
160 krb5_kdc_save_request(krb5_context context,
161 const char *fn,
162 const unsigned char *buf,
163 size_t len,
164 const krb5_data *reply,
165 const struct sockaddr *sa)
167 krb5_storage *sp;
168 krb5_address a;
169 int fd, ret;
170 uint32_t t;
171 krb5_data d;
173 memset(&a, 0, sizeof(a));
175 d.data = rk_UNCONST(buf);
176 d.length = len;
177 t = _kdc_now.tv_sec;
179 fd = open(fn, O_WRONLY|O_CREAT|O_APPEND, 0600);
180 if (fd < 0) {
181 krb5_set_error_string(context, "Failed to open: %s", fn);
182 return errno;
185 sp = krb5_storage_from_fd(fd);
186 close(fd);
187 if (sp == NULL) {
188 krb5_set_error_string(context, "Storage failed to open fd");
189 return ENOMEM;
192 ret = krb5_sockaddr2address(context, sa, &a);
193 if (ret)
194 goto out;
196 krb5_store_uint32(sp, 1);
197 krb5_store_uint32(sp, t);
198 krb5_store_address(sp, a);
199 krb5_store_data(sp, d);
201 Der_class cl;
202 Der_type ty;
203 unsigned int tag;
204 ret = der_get_tag (reply->data, reply->length,
205 &cl, &ty, &tag, NULL);
206 if (ret) {
207 krb5_store_uint32(sp, 0xffffffff);
208 krb5_store_uint32(sp, 0xffffffff);
209 } else {
210 krb5_store_uint32(sp, MAKE_TAG(cl, ty, 0));
211 krb5_store_uint32(sp, tag);
215 krb5_free_address(context, &a);
216 out:
217 krb5_storage_free(sp);
219 return 0;