2 * Copyright (c) 1997-2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
42 krb5_kdc_update_time(struct timeval
*tv
)
45 gettimeofday(&_kdc_now
, NULL
);
50 static krb5_error_code
51 kdc_as_req(krb5_context context
,
52 krb5_kdc_configuration
*config
,
53 krb5_data
*req_buffer
,
56 struct sockaddr
*addr
,
64 ret
= decode_AS_REQ(req_buffer
->data
, req_buffer
->length
, &req
, &len
);
70 ret
= _kdc_as_rep(context
, config
, &req
, req_buffer
,
71 reply
, from
, addr
, datagram_reply
);
77 static krb5_error_code
78 kdc_tgs_req(krb5_context context
,
79 krb5_kdc_configuration
*config
,
80 krb5_data
*req_buffer
,
83 struct sockaddr
*addr
,
91 ret
= decode_TGS_REQ(req_buffer
->data
, req_buffer
->length
, &req
, &len
);
97 ret
= _kdc_tgs_rep(context
, config
, &req
, reply
,
98 from
, addr
, datagram_reply
);
105 static krb5_error_code
106 kdc_digest(krb5_context context
,
107 krb5_kdc_configuration
*config
,
108 krb5_data
*req_buffer
,
111 struct sockaddr
*addr
,
119 ret
= decode_DigestREQ(req_buffer
->data
, req_buffer
->length
,
126 ret
= _kdc_do_digest(context
, config
, &digestreq
, reply
, from
, addr
);
127 free_DigestREQ(&digestreq
);
135 static krb5_error_code
136 kdc_kx509(krb5_context context
,
137 krb5_kdc_configuration
*config
,
138 krb5_data
*req_buffer
,
141 struct sockaddr
*addr
,
145 Kx509Request kx509req
;
149 ret
= _kdc_try_kx509_request(req_buffer
->data
, req_buffer
->length
,
156 ret
= _kdc_do_kx509(context
, config
, &kx509req
, reply
, from
, addr
);
157 free_Kx509Request(&kx509req
);
166 static krb5_error_code
167 kdc_524(krb5_context context
,
168 krb5_kdc_configuration
*config
,
169 krb5_data
*req_buffer
,
172 struct sockaddr
*addr
,
180 ret
= decode_Ticket(req_buffer
->data
, req_buffer
->length
, &ticket
, &len
);
186 ret
= _kdc_do_524(context
, config
, &ticket
, reply
, from
, addr
);
187 free_Ticket(&ticket
);
191 static krb5_error_code
192 kdc_krb4(krb5_context context
,
193 krb5_kdc_configuration
*config
,
194 krb5_data
*req_buffer
,
197 struct sockaddr
*addr
,
201 if (_kdc_maybe_version4(req_buffer
->data
, req_buffer
->length
) == 0)
206 return _kdc_do_version4(context
, config
,
207 req_buffer
->data
, req_buffer
->length
,
209 (struct sockaddr_in
*)addr
);
212 static krb5_error_code
213 kdc_kaserver(krb5_context context
,
214 krb5_kdc_configuration
*config
,
215 krb5_data
*req_buffer
,
218 struct sockaddr
*addr
,
222 if (config
->enable_kaserver
== 0)
227 return _kdc_do_kaserver(context
, config
,
228 req_buffer
->data
, req_buffer
->length
,
230 (struct sockaddr_in
*)addr
);
236 static struct krb5_kdc_service services
[] = {
237 { KS_KRB5
, kdc_as_req
},
238 { KS_KRB5
, kdc_tgs_req
},
247 { KS_NO_LENGTH
, kdc_krb4
},
254 * handle the request in `buf, len', from `addr' (or `from' as a string),
255 * sending a reply in `reply'.
259 krb5_kdc_process_request(krb5_context context
,
260 krb5_kdc_configuration
*config
,
264 krb5_boolean
*prependlength
,
266 struct sockaddr
*addr
,
271 krb5_data req_buffer
;
274 req_buffer
.data
= buf
;
275 req_buffer
.length
= len
;
277 for (i
= 0; services
[i
].process
!= NULL
; i
++) {
278 ret
= (*services
[i
].process
)(context
, config
, &req_buffer
,
279 reply
, from
, addr
, datagram_reply
,
282 if (services
[i
].flags
& KS_NO_LENGTH
)
292 * handle the request in `buf, len', from `addr' (or `from' as a string),
293 * sending a reply in `reply'.
295 * This only processes krb5 requests
299 krb5_kdc_process_krb5_request(krb5_context context
,
300 krb5_kdc_configuration
*config
,
305 struct sockaddr
*addr
,
310 krb5_data req_buffer
;
313 req_buffer
.data
= buf
;
314 req_buffer
.length
= len
;
316 for (i
= 0; services
[i
].process
!= NULL
; i
++) {
317 if ((services
[i
].flags
& KS_KRB5
) == 0)
319 ret
= (*services
[i
].process
)(context
, config
, &req_buffer
,
320 reply
, from
, addr
, datagram_reply
,
334 krb5_kdc_save_request(krb5_context context
,
336 const unsigned char *buf
,
338 const krb5_data
*reply
,
339 const struct sockaddr
*sa
)
347 memset(&a
, 0, sizeof(a
));
349 d
.data
= rk_UNCONST(buf
);
353 fd
= open(fn
, O_WRONLY
|O_CREAT
|O_APPEND
, 0600);
355 int saved_errno
= errno
;
356 krb5_set_error_message(context
, saved_errno
, "Failed to open: %s", fn
);
360 sp
= krb5_storage_from_fd(fd
);
363 krb5_set_error_message(context
, ENOMEM
, "Storage failed to open fd");
367 ret
= krb5_sockaddr2address(context
, sa
, &a
);
371 krb5_store_uint32(sp
, 1);
372 krb5_store_uint32(sp
, t
);
373 krb5_store_address(sp
, a
);
374 krb5_store_data(sp
, d
);
379 ret
= der_get_tag (reply
->data
, reply
->length
,
380 &cl
, &ty
, &tag
, NULL
);
382 krb5_store_uint32(sp
, 0xffffffff);
383 krb5_store_uint32(sp
, 0xffffffff);
385 krb5_store_uint32(sp
, MAKE_TAG(cl
, ty
, 0));
386 krb5_store_uint32(sp
, tag
);
390 krb5_free_address(context
, &a
);
392 krb5_storage_free(sp
);