2 * Copyright (c) 1997 - 2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
34 #include "krb5_locl.h"
35 #include "send_to_kdc_plugin.h"
38 krb5_send_to_kdc_func func
;
43 * send the data in `req' on the socket `fd' (which is datagram iff udp)
44 * waiting `tmout' for a reply and returning the reply in `rep'.
45 * iff limit read up to this many bytes
46 * returns 0 and data in `rep' if succesful, otherwise -1
57 struct timeval timeout
;
61 if (fd
>= FD_SETSIZE
) {
69 timeout
.tv_sec
= tmout
;
71 ret
= select (fd
+ 1, &fdset
, NULL
, NULL
, &timeout
);
76 } else if (ret
== 0) {
81 if (ioctl (fd
, FIONREAD
, &nbytes
) < 0) {
89 nbytes
= min(nbytes
, limit
- rep
->length
);
91 tmp
= realloc (rep
->data
, rep
->length
+ nbytes
);
97 ret
= recv (fd
, (char*)tmp
+ rep
->length
, nbytes
, 0);
104 } while(!udp
&& (limit
== 0 || rep
->length
< limit
));
109 * Send kerberos requests and receive a reply on a udp or any other kind
110 * of a datagram socket. See `recv_loop'.
114 send_and_recv_udp(int fd
,
116 const krb5_data
*req
,
119 if (send (fd
, req
->data
, req
->length
, 0) < 0)
122 return recv_loop(fd
, tmout
, 1, 0, rep
);
126 * `send_and_recv' for a TCP (or any other stream) socket.
127 * Since there are no record limits on a stream socket the protocol here
128 * is to prepend the request with 4 bytes of its length and the reply
129 * is similarly encoded.
133 send_and_recv_tcp(int fd
,
135 const krb5_data
*req
,
138 unsigned char len
[4];
139 unsigned long rep_len
;
142 _krb5_put_int(len
, req
->length
, 4);
143 if(net_write(fd
, len
, sizeof(len
)) < 0)
145 if(net_write(fd
, req
->data
, req
->length
) < 0)
147 if (recv_loop (fd
, tmout
, 0, 4, &len_data
) < 0)
149 if (len_data
.length
!= 4) {
150 krb5_data_free (&len_data
);
153 _krb5_get_int(len_data
.data
, &rep_len
, 4);
154 krb5_data_free (&len_data
);
155 if (recv_loop (fd
, tmout
, 0, rep_len
, rep
) < 0)
157 if(rep
->length
!= rep_len
) {
158 krb5_data_free (rep
);
165 _krb5_send_and_recv_tcp(int fd
,
167 const krb5_data
*req
,
170 return send_and_recv_tcp(fd
, tmout
, req
, rep
);
174 * `send_and_recv' tailored for the HTTP protocol.
178 send_and_recv_http(int fd
,
181 const krb5_data
*req
,
187 int len
= base64_encode(req
->data
, req
->length
, &str
);
191 asprintf(&request
, "GET %s%s HTTP/1.0\r\n\r\n", prefix
, str
);
195 ret
= net_write (fd
, request
, strlen(request
));
199 ret
= recv_loop(fd
, tmout
, 0, 0, rep
);
203 unsigned long rep_len
;
206 s
= realloc(rep
->data
, rep
->length
+ 1);
208 krb5_data_free (rep
);
212 p
= strstr(s
, "\r\n\r\n");
220 rep
->length
-= p
- s
;
221 if(rep
->length
< 4) { /* remove length */
227 _krb5_get_int(p
, &rep_len
, 4);
228 if (rep_len
!= rep
->length
) {
233 memmove(rep
->data
, p
+ 4, rep
->length
);
239 init_port(const char *s
, int fallback
)
244 sscanf (s
, "%d", &tmp
);
251 * Return 0 if succesful, otherwise 1
255 send_via_proxy (krb5_context context
,
256 const krb5_krbhst_info
*hi
,
257 const krb5_data
*send_data
,
260 char *proxy2
= strdup(context
->http_proxy
);
261 char *proxy
= proxy2
;
264 struct addrinfo hints
;
265 struct addrinfo
*ai
, *a
;
268 char portstr
[NI_MAXSERV
];
272 if (strncmp (proxy
, "http://", 7) == 0)
275 colon
= strchr(proxy
, ':');
278 memset (&hints
, 0, sizeof(hints
));
279 hints
.ai_family
= PF_UNSPEC
;
280 hints
.ai_socktype
= SOCK_STREAM
;
281 snprintf (portstr
, sizeof(portstr
), "%d",
282 ntohs(init_port (colon
, htons(80))));
283 ret
= getaddrinfo (proxy
, portstr
, &hints
, &ai
);
286 return krb5_eai_to_heim_errno(ret
, errno
);
288 for (a
= ai
; a
!= NULL
; a
= a
->ai_next
) {
289 s
= socket (a
->ai_family
, a
->ai_socktype
, a
->ai_protocol
| SOCK_CLOEXEC
);
293 if (connect (s
, a
->ai_addr
, a
->ai_addrlen
) < 0) {
305 asprintf(&prefix
, "http://%s/", hi
->hostname
);
310 ret
= send_and_recv_http(s
, context
->kdc_timeout
,
311 prefix
, send_data
, receive
);
314 if(ret
== 0 && receive
->length
!= 0)
319 static krb5_error_code
320 send_via_plugin(krb5_context context
,
321 krb5_krbhst_info
*hi
,
323 const krb5_data
*send_data
,
326 struct krb5_plugin
*list
= NULL
, *e
;
329 ret
= _krb5_plugin_find(context
, PLUGIN_TYPE_DATA
, KRB5_PLUGIN_SEND_TO_KDC
, &list
);
330 if(ret
!= 0 || list
== NULL
)
331 return KRB5_PLUGIN_NO_HANDLE
;
333 for (e
= list
; e
!= NULL
; e
= _krb5_plugin_get_next(e
)) {
334 krb5plugin_send_to_kdc_ftable
*service
;
337 service
= _krb5_plugin_get_symbol(e
);
338 if (service
->minor_version
!= 0)
341 (*service
->init
)(context
, &ctx
);
342 ret
= (*service
->send_to_kdc
)(context
, ctx
, hi
,
343 timeout
, send_data
, receive
);
344 (*service
->fini
)(ctx
);
347 if (ret
!= KRB5_PLUGIN_NO_HANDLE
) {
348 krb5_set_error_message(context
, ret
,
349 N_("Plugin send_to_kdc failed to "
350 "lookup with error: %d", ""), ret
);
354 _krb5_plugin_free(list
);
355 return KRB5_PLUGIN_NO_HANDLE
;
360 * Send the data `send' to one host from `handle` and get back the reply
364 krb5_error_code KRB5_LIB_FUNCTION
365 krb5_sendto (krb5_context context
,
366 const krb5_data
*send_data
,
367 krb5_krbhst_handle handle
,
374 krb5_data_zero(receive
);
376 for (i
= 0; i
< context
->max_retries
; ++i
) {
377 krb5_krbhst_info
*hi
;
379 while (krb5_krbhst_next(context
, handle
, &hi
) == 0) {
380 struct addrinfo
*ai
, *a
;
382 if (context
->send_to_kdc
) {
383 struct send_to_kdc
*s
= context
->send_to_kdc
;
385 ret
= (*s
->func
)(context
, s
->data
, hi
,
386 context
->kdc_timeout
, send_data
, receive
);
387 if (ret
== 0 && receive
->length
!= 0)
392 ret
= send_via_plugin(context
, hi
, context
->kdc_timeout
,
394 if (ret
== 0 && receive
->length
!= 0)
396 else if (ret
!= KRB5_PLUGIN_NO_HANDLE
)
399 if(hi
->proto
== KRB5_KRBHST_HTTP
&& context
->http_proxy
) {
400 if (send_via_proxy (context
, hi
, send_data
, receive
) == 0) {
407 ret
= krb5_krbhst_get_addrinfo(context
, hi
, &ai
);
411 for (a
= ai
; a
!= NULL
; a
= a
->ai_next
) {
412 fd
= socket (a
->ai_family
, a
->ai_socktype
| SOCK_CLOEXEC
, a
->ai_protocol
);
416 if (connect (fd
, a
->ai_addr
, a
->ai_addrlen
) < 0) {
421 case KRB5_KRBHST_HTTP
:
422 ret
= send_and_recv_http(fd
, context
->kdc_timeout
,
423 "", send_data
, receive
);
425 case KRB5_KRBHST_TCP
:
426 ret
= send_and_recv_tcp (fd
, context
->kdc_timeout
,
429 case KRB5_KRBHST_UDP
:
430 ret
= send_and_recv_udp (fd
, context
->kdc_timeout
,
435 if(ret
== 0 && receive
->length
!= 0)
439 krb5_krbhst_reset(context
, handle
);
441 krb5_clear_error_message (context
);
442 ret
= KRB5_KDC_UNREACH
;
447 krb5_error_code KRB5_LIB_FUNCTION
448 krb5_sendto_kdc(krb5_context context
,
449 const krb5_data
*send_data
,
450 const krb5_realm
*realm
,
453 return krb5_sendto_kdc_flags(context
, send_data
, realm
, receive
, 0);
456 krb5_error_code KRB5_LIB_FUNCTION
457 krb5_sendto_kdc_flags(krb5_context context
,
458 const krb5_data
*send_data
,
459 const krb5_realm
*realm
,
466 ret
= krb5_sendto_ctx_alloc(context
, &ctx
);
469 krb5_sendto_ctx_add_flags(ctx
, flags
);
470 krb5_sendto_ctx_set_func(ctx
, _krb5_kdc_retry
, NULL
);
472 ret
= krb5_sendto_context(context
, ctx
, send_data
, *realm
, receive
);
473 krb5_sendto_ctx_free(context
, ctx
);
477 krb5_error_code KRB5_LIB_FUNCTION
478 krb5_set_send_to_kdc_func(krb5_context context
,
479 krb5_send_to_kdc_func func
,
482 free(context
->send_to_kdc
);
484 context
->send_to_kdc
= NULL
;
488 context
->send_to_kdc
= malloc(sizeof(*context
->send_to_kdc
));
489 if (context
->send_to_kdc
== NULL
) {
490 krb5_set_error_message(context
, ENOMEM
,
491 N_("malloc: out of memory", ""));
495 context
->send_to_kdc
->func
= func
;
496 context
->send_to_kdc
->data
= data
;
500 krb5_error_code KRB5_LIB_FUNCTION
501 _krb5_copy_send_to_kdc_func(krb5_context context
, krb5_context to
)
503 if (context
->send_to_kdc
)
504 return krb5_set_send_to_kdc_func(to
,
505 context
->send_to_kdc
->func
,
506 context
->send_to_kdc
->data
);
508 return krb5_set_send_to_kdc_func(to
, NULL
, NULL
);
513 struct krb5_sendto_ctx_data
{
516 krb5_sendto_ctx_func func
;
520 krb5_error_code KRB5_LIB_FUNCTION
521 krb5_sendto_ctx_alloc(krb5_context context
, krb5_sendto_ctx
*ctx
)
523 *ctx
= calloc(1, sizeof(**ctx
));
525 krb5_set_error_message(context
, ENOMEM
,
526 N_("malloc: out of memory", ""));
532 void KRB5_LIB_FUNCTION
533 krb5_sendto_ctx_add_flags(krb5_sendto_ctx ctx
, int flags
)
538 int KRB5_LIB_FUNCTION
539 krb5_sendto_ctx_get_flags(krb5_sendto_ctx ctx
)
544 void KRB5_LIB_FUNCTION
545 krb5_sendto_ctx_set_type(krb5_sendto_ctx ctx
, int type
)
551 void KRB5_LIB_FUNCTION
552 krb5_sendto_ctx_set_func(krb5_sendto_ctx ctx
,
553 krb5_sendto_ctx_func func
,
560 void KRB5_LIB_FUNCTION
561 krb5_sendto_ctx_free(krb5_context context
, krb5_sendto_ctx ctx
)
563 memset(ctx
, 0, sizeof(*ctx
));
567 krb5_error_code KRB5_LIB_FUNCTION
568 krb5_sendto_context(krb5_context context
,
570 const krb5_data
*send_data
,
571 const krb5_realm realm
,
575 krb5_krbhst_handle handle
= NULL
;
576 int type
, freectx
= 0;
579 krb5_data_zero(receive
);
583 ret
= krb5_sendto_ctx_alloc(context
, &ctx
);
590 if ((ctx
->flags
& KRB5_KRBHST_FLAGS_MASTER
) || context
->use_admin_kdc
)
591 type
= KRB5_KRBHST_ADMIN
;
593 type
= KRB5_KRBHST_KDC
;
596 if (send_data
->length
> context
->large_msg_size
)
597 ctx
->flags
|= KRB5_KRBHST_FLAGS_LARGE_MSG
;
599 /* loop until we get back a appropriate response */
602 action
= KRB5_SENDTO_DONE
;
604 krb5_data_free(receive
);
606 if (handle
== NULL
) {
607 ret
= krb5_krbhst_init_flags(context
, realm
, type
,
608 ctx
->flags
, &handle
);
611 krb5_sendto_ctx_free(context
, ctx
);
616 ret
= krb5_sendto(context
, send_data
, handle
, receive
);
620 ret
= (*ctx
->func
)(context
, ctx
, ctx
->data
, receive
, &action
);
624 if (action
!= KRB5_SENDTO_CONTINUE
) {
625 krb5_krbhst_free(context
, handle
);
628 } while (action
!= KRB5_SENDTO_DONE
);
630 krb5_krbhst_free(context
, handle
);
631 if (ret
== KRB5_KDC_UNREACH
)
632 krb5_set_error_message(context
, ret
,
633 N_("unable to reach any KDC in realm %s", ""),
636 krb5_data_free(receive
);
638 krb5_sendto_ctx_free(context
, ctx
);
643 _krb5_kdc_retry(krb5_context context
, krb5_sendto_ctx ctx
, void *data
,
644 const krb5_data
*reply
, int *action
)
649 if(krb5_rd_error(context
, reply
, &error
))
652 ret
= krb5_error_from_rd_error(context
, &error
, NULL
);
653 krb5_free_error_contents(context
, &error
);
656 case KRB5KRB_ERR_RESPONSE_TOO_BIG
: {
657 if (krb5_sendto_ctx_get_flags(ctx
) & KRB5_KRBHST_FLAGS_LARGE_MSG
)
659 krb5_sendto_ctx_add_flags(ctx
, KRB5_KRBHST_FLAGS_LARGE_MSG
);
660 *action
= KRB5_SENDTO_RESTART
;
663 case KRB5KDC_ERR_SVC_UNAVAILABLE
:
664 *action
= KRB5_SENDTO_CONTINUE
;