2 * Copyright (c) 1997 - 2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Portions Copyright (c) 2010 - 2013 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include "krb5_locl.h"
37 #include "send_to_kdc_plugin.h"
40 * @section send_to_kdc Locating and sending packets to the KDC
42 * The send to kdc code is responsible to request the list of KDC from
43 * the locate-kdc subsystem and then send requests to each of them.
45 * - Each second a new hostname is tried.
46 * - If the hostname have several addresses, the first will be tried
47 * directly then in turn the other will be tried every 3 seconds
49 * - UDP requests are tried 3 times, and it tried with a individual timeout of kdc_timeout / 3.
50 * - TCP and HTTP requests are tried 1 time.
52 * Total wait time shorter then (number of addresses * 3) + kdc_timeout seconds.
57 init_port(const char *s
, int fallback
)
62 sscanf (s
, "%d", &tmp
);
68 struct send_via_plugin_s
{
69 krb5_const_realm realm
;
72 const krb5_data
*send_data
;
77 static krb5_error_code KRB5_LIB_CALL
78 kdccallback(krb5_context context
, const void *plug
, void *plugctx
, void *userctx
)
80 const krb5plugin_send_to_kdc_ftable
*service
= (const krb5plugin_send_to_kdc_ftable
*)plug
;
81 struct send_via_plugin_s
*ctx
= userctx
;
83 if (service
->send_to_kdc
== NULL
)
84 return KRB5_PLUGIN_NO_HANDLE
;
85 return service
->send_to_kdc(context
, plugctx
, ctx
->hi
, ctx
->timeout
,
86 ctx
->send_data
, ctx
->receive
);
89 static krb5_error_code KRB5_LIB_CALL
90 realmcallback(krb5_context context
, const void *plug
, void *plugctx
, void *userctx
)
92 const krb5plugin_send_to_kdc_ftable
*service
= (const krb5plugin_send_to_kdc_ftable
*)plug
;
93 struct send_via_plugin_s
*ctx
= userctx
;
95 if (service
->send_to_realm
== NULL
)
96 return KRB5_PLUGIN_NO_HANDLE
;
97 return service
->send_to_realm(context
, plugctx
, ctx
->realm
, ctx
->timeout
,
98 ctx
->send_data
, ctx
->receive
);
101 static krb5_error_code
102 kdc_via_plugin(krb5_context context
,
103 krb5_krbhst_info
*hi
,
105 const krb5_data
*send_data
,
108 struct send_via_plugin_s userctx
;
110 userctx
.realm
= NULL
;
112 userctx
.timeout
= timeout
;
113 userctx
.send_data
= send_data
;
114 userctx
.receive
= receive
;
116 return _krb5_plugin_run_f(context
, "krb5", KRB5_PLUGIN_SEND_TO_KDC
,
117 KRB5_PLUGIN_SEND_TO_KDC_VERSION_0
, 0,
118 &userctx
, kdccallback
);
121 static krb5_error_code
122 realm_via_plugin(krb5_context context
,
123 krb5_const_realm realm
,
125 const krb5_data
*send_data
,
128 struct send_via_plugin_s userctx
;
130 userctx
.realm
= realm
;
132 userctx
.timeout
= timeout
;
133 userctx
.send_data
= send_data
;
134 userctx
.receive
= receive
;
136 return _krb5_plugin_run_f(context
, "krb5", KRB5_PLUGIN_SEND_TO_KDC
,
137 KRB5_PLUGIN_SEND_TO_KDC_VERSION_2
, 0,
138 &userctx
, realmcallback
);
141 struct krb5_sendto_ctx_data
{
144 krb5_sendto_ctx_func func
;
147 krb5_krbhst_handle krbhst
;
150 const krb5_data
*send_data
;
154 #define KRBHST_COMPLETED 1
157 krb5_sendto_prexmit prexmit_func
;
162 struct timeval start_time
;
163 struct timeval name_resolution
;
164 struct timeval krbhst
;
165 unsigned long sent_packets
;
166 unsigned long num_hosts
;
172 dealloc_sendto_ctx(void *ptr
)
174 krb5_sendto_ctx ctx
= (krb5_sendto_ctx
)ptr
;
177 heim_release(ctx
->hosts
);
178 heim_release(ctx
->krbhst
);
181 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
182 krb5_sendto_ctx_alloc(krb5_context context
, krb5_sendto_ctx
*ctx
)
184 *ctx
= heim_alloc(sizeof(**ctx
), "sendto-context", dealloc_sendto_ctx
);
186 return krb5_enomem(context
);
187 (*ctx
)->hosts
= heim_array_create();
192 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
193 krb5_sendto_ctx_add_flags(krb5_sendto_ctx ctx
, int flags
)
198 KRB5_LIB_FUNCTION
int KRB5_LIB_CALL
199 krb5_sendto_ctx_get_flags(krb5_sendto_ctx ctx
)
204 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
205 krb5_sendto_ctx_set_type(krb5_sendto_ctx ctx
, int type
)
210 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
211 krb5_sendto_ctx_set_func(krb5_sendto_ctx ctx
,
212 krb5_sendto_ctx_func func
,
219 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
220 _krb5_sendto_ctx_set_prexmit(krb5_sendto_ctx ctx
,
221 krb5_sendto_prexmit prexmit
,
224 ctx
->prexmit_func
= prexmit
;
225 ctx
->prexmit_ctx
= data
;
228 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
229 krb5_sendto_set_hostname(krb5_context context
,
231 const char *hostname
)
233 if (ctx
->hostname
== NULL
)
235 ctx
->hostname
= strdup(hostname
);
236 if (ctx
->hostname
== NULL
) {
237 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
243 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
244 _krb5_sendto_ctx_set_krb5hst(krb5_context context
,
246 krb5_krbhst_handle handle
)
248 heim_release(ctx
->krbhst
);
249 ctx
->krbhst
= heim_retain(handle
);
252 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
253 krb5_sendto_ctx_free(krb5_context context
, krb5_sendto_ctx ctx
)
258 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
259 _krb5_kdc_retry(krb5_context context
, krb5_sendto_ctx ctx
, void *data
,
260 const krb5_data
*reply
, int *action
)
265 if(krb5_rd_error(context
, reply
, &error
))
268 ret
= krb5_error_from_rd_error(context
, &error
, NULL
);
269 krb5_free_error_contents(context
, &error
);
272 case KRB5KRB_ERR_RESPONSE_TOO_BIG
: {
273 if (krb5_sendto_ctx_get_flags(ctx
) & KRB5_KRBHST_FLAGS_LARGE_MSG
)
275 krb5_sendto_ctx_add_flags(ctx
, KRB5_KRBHST_FLAGS_LARGE_MSG
);
276 *action
= KRB5_SENDTO_RESET
;
279 case KRB5KDC_ERR_SVC_UNAVAILABLE
:
280 *action
= KRB5_SENDTO_CONTINUE
;
293 krb5_error_code (*prepare
)(krb5_context
, struct host
*, const krb5_data
*);
294 krb5_error_code (*send_fn
)(krb5_context
, struct host
*);
295 krb5_error_code (*recv_fn
)(krb5_context
, struct host
*, krb5_data
*);
300 enum host_state
{ CONNECT
, CONNECTING
, CONNECTED
, WAITING_REPLY
, DEAD
} state
;
301 krb5_krbhst_info
*hi
;
304 struct host_fun
*fun
;
312 debug_host(krb5_context context
, int level
, struct host
*host
, const char *fmt
, ...)
313 __attribute__((__format__(__printf__
, 4, 5)));
316 debug_host(krb5_context context
, int level
, struct host
*host
, const char *fmt
, ...)
318 const char *proto
= "unknown";
319 char name
[NI_MAXHOST
], port
[NI_MAXSERV
];
324 if (!_krb5_have_debug(context
, 5))
328 ret
= vasprintf(&text
, fmt
, ap
);
330 if (ret
== -1 || text
== NULL
)
333 if (host
->hi
->proto
== KRB5_KRBHST_HTTP
)
335 else if (host
->hi
->proto
== KRB5_KRBHST_TCP
)
337 else if (host
->hi
->proto
== KRB5_KRBHST_UDP
)
340 if (getnameinfo(host
->ai
->ai_addr
, host
->ai
->ai_addrlen
,
341 name
, sizeof(name
), port
, sizeof(port
), NI_NUMERICHOST
) != 0)
344 _krb5_debug(context
, level
, "%s: %s %s:%s (%s) tid: %08x", text
,
345 proto
, name
, port
, host
->hi
->hostname
, host
->tid
);
351 deallocate_host(void *ptr
)
353 struct host
*host
= ptr
;
354 if (!rk_IS_BAD_SOCKET(host
->fd
))
355 rk_closesocket(host
->fd
);
356 krb5_data_free(&host
->data
);
361 host_dead(krb5_context context
, struct host
*host
, const char *msg
)
363 debug_host(context
, 5, host
, "%s", msg
);
364 rk_closesocket(host
->fd
);
365 host
->fd
= rk_INVALID_SOCKET
;
369 static krb5_error_code
370 send_stream(krb5_context context
, struct host
*host
)
374 len
= krb5_net_write(context
, &host
->fd
, host
->data
.data
, host
->data
.length
);
378 else if (len
< host
->data
.length
) {
379 host
->data
.length
-= len
;
380 memmove(host
->data
.data
, ((uint8_t *)host
->data
.data
) + len
, host
->data
.length
- len
);
383 krb5_data_free(&host
->data
);
388 static krb5_error_code
389 recv_stream(krb5_context context
, struct host
*host
)
396 if (rk_SOCK_IOCTL(host
->fd
, FIONREAD
, &nbytes
) != 0 || nbytes
<= 0)
397 return HEIM_NET_CONN_REFUSED
;
399 if (context
->max_msg_size
- host
->data
.length
< nbytes
) {
400 krb5_set_error_message(context
, KRB5KRB_ERR_FIELD_TOOLONG
,
401 N_("TCP message from KDC too large %d", ""),
402 (int)(host
->data
.length
+ nbytes
));
403 return KRB5KRB_ERR_FIELD_TOOLONG
;
406 oldlen
= host
->data
.length
;
408 ret
= krb5_data_realloc(&host
->data
, oldlen
+ nbytes
+ 1 /* NUL */);
412 sret
= krb5_net_read(context
, &host
->fd
, ((uint8_t *)host
->data
.data
) + oldlen
, nbytes
);
417 host
->data
.length
= oldlen
+ sret
;
418 /* zero terminate for http transport */
419 ((uint8_t *)host
->data
.data
)[host
->data
.length
] = '\0';
429 host_next_timeout(krb5_context context
, struct host
*host
)
431 host
->timeout
= context
->kdc_timeout
/ host
->fun
->ntries
;
432 if (host
->timeout
== 0)
435 host
->timeout
+= time(NULL
);
443 host_connected(krb5_context context
, krb5_sendto_ctx ctx
, struct host
*host
)
447 host
->state
= CONNECTED
;
449 * Now prepare data to send to host
451 if (ctx
->prexmit_func
) {
454 krb5_data_zero(&data
);
456 ret
= ctx
->prexmit_func(context
, host
->hi
->proto
,
457 ctx
->prexmit_ctx
, host
->fd
, &data
);
459 if (data
.length
== 0) {
460 host_dead(context
, host
, "prexmit function didn't send data");
463 ret
= host
->fun
->prepare(context
, host
, &data
);
464 krb5_data_free(&data
);
468 ret
= host
->fun
->prepare(context
, host
, ctx
->send_data
);
471 debug_host(context
, 5, host
, "failed to prexmit/prepare");
479 host_connect(krb5_context context
, krb5_sendto_ctx ctx
, struct host
*host
)
481 krb5_krbhst_info
*hi
= host
->hi
;
482 struct addrinfo
*ai
= host
->ai
;
484 debug_host(context
, 5, host
, "connecting to host");
486 if (connect(host
->fd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0) {
488 if (WSAGetLastError() == WSAEWOULDBLOCK
)
490 #endif /* HAVE_WINSOCK */
491 if (errno
== EINPROGRESS
&& (hi
->proto
== KRB5_KRBHST_HTTP
|| hi
->proto
== KRB5_KRBHST_TCP
)) {
492 debug_host(context
, 5, host
, "connecting to %d", host
->fd
);
493 host
->state
= CONNECTING
;
495 host_dead(context
, host
, "failed to connect");
498 host_connected(context
, ctx
, host
);
501 host_next_timeout(context
, host
);
508 static krb5_error_code
509 prepare_http(krb5_context context
, struct host
*host
, const krb5_data
*data
)
511 char *str
= NULL
, *request
= NULL
;
515 heim_assert(host
->data
.length
== 0, "prepare_http called twice");
517 len
= base64_encode(data
->data
, data
->length
, &str
);
521 if (context
->http_proxy
)
522 ret
= asprintf(&request
, "GET http://%s/%s HTTP/1.0\r\n\r\n", host
->hi
->hostname
, str
);
524 ret
= asprintf(&request
, "GET /%s HTTP/1.0\r\n\r\n", str
);
526 if(ret
< 0 || request
== NULL
)
529 host
->data
.data
= request
;
530 host
->data
.length
= strlen(request
);
535 static krb5_error_code
536 recv_http(krb5_context context
, struct host
*host
, krb5_data
*data
)
539 unsigned long rep_len
;
544 * recv_stream returns a NUL terminated stream
547 ret
= recv_stream(context
, host
);
551 p
= strstr(host
->data
.data
, "\r\n\r\n");
556 len
= host
->data
.length
- (p
- (char *)host
->data
.data
);
560 _krb5_get_int(p
, &rep_len
, 4);
566 memmove(host
->data
.data
, p
, rep_len
);
567 host
->data
.length
= rep_len
;
570 krb5_data_zero(&host
->data
);
579 static krb5_error_code
580 prepare_tcp(krb5_context context
, struct host
*host
, const krb5_data
*data
)
585 heim_assert(host
->data
.length
== 0, "prepare_tcp called twice");
587 sp
= krb5_storage_emem();
591 ret
= krb5_store_data(sp
, *data
);
593 krb5_storage_free(sp
);
596 ret
= krb5_storage_to_data(sp
, &host
->data
);
597 krb5_storage_free(sp
);
602 static krb5_error_code
603 recv_tcp(krb5_context context
, struct host
*host
, krb5_data
*data
)
606 unsigned long pktlen
;
608 ret
= recv_stream(context
, host
);
612 if (host
->data
.length
< 4)
615 _krb5_get_int(host
->data
.data
, &pktlen
, 4);
617 if (pktlen
> host
->data
.length
- 4)
620 memmove(host
->data
.data
, ((uint8_t *)host
->data
.data
) + 4, host
->data
.length
- 4);
621 host
->data
.length
-= 4;
624 krb5_data_zero(&host
->data
);
633 static krb5_error_code
634 prepare_udp(krb5_context context
, struct host
*host
, const krb5_data
*data
)
636 return krb5_data_copy(&host
->data
, data
->data
, data
->length
);
639 static krb5_error_code
640 send_udp(krb5_context context
, struct host
*host
)
642 if (send(host
->fd
, host
->data
.data
, host
->data
.length
, 0) < 0)
647 static krb5_error_code
648 recv_udp(krb5_context context
, struct host
*host
, krb5_data
*data
)
654 if (rk_SOCK_IOCTL(host
->fd
, FIONREAD
, &nbytes
) != 0 || nbytes
<= 0)
655 return HEIM_NET_CONN_REFUSED
;
657 if (context
->max_msg_size
< nbytes
) {
658 krb5_set_error_message(context
, KRB5KRB_ERR_FIELD_TOOLONG
,
659 N_("UDP message from KDC too large %d", ""),
661 return KRB5KRB_ERR_FIELD_TOOLONG
;
664 ret
= krb5_data_alloc(data
, nbytes
);
668 ret
= recv(host
->fd
, data
->data
, data
->length
, 0);
671 krb5_data_free(data
);
679 static struct host_fun http_fun
= {
685 static struct host_fun tcp_fun
= {
691 static struct host_fun udp_fun
= {
704 eval_host_state(krb5_context context
,
707 int readable
, int writeable
)
711 if (host
->state
== CONNECT
) {
712 /* check if its this host time to connect */
713 if (host
->timeout
< time(NULL
))
714 host_connect(context
, ctx
, host
);
718 if (host
->state
== CONNECTING
&& writeable
)
719 host_connected(context
, ctx
, host
);
723 debug_host(context
, 5, host
, "reading packet");
725 ret
= host
->fun
->recv_fn(context
, host
, &ctx
->response
);
728 } else if (ret
== 0) {
729 /* if recv_foo function returns 0, we have a complete reply */
730 debug_host(context
, 5, host
, "host completed");
733 host_dead(context
, host
, "host disconnected");
737 /* check if there is anything to send, state might DEAD after read */
738 if (writeable
&& host
->state
== CONNECTED
) {
740 ctx
->stats
.sent_packets
++;
742 debug_host(context
, 5, host
, "writing packet");
744 ret
= host
->fun
->send_fn(context
, host
);
748 host_dead(context
, host
, "host dead, write failed");
750 host
->state
= WAITING_REPLY
;
760 static krb5_error_code
761 submit_request(krb5_context context
, krb5_sendto_ctx ctx
, krb5_krbhst_info
*hi
)
763 unsigned long submitted_host
= 0;
764 krb5_boolean freeai
= FALSE
;
765 struct timeval nrstart
, nrstop
;
767 struct addrinfo
*ai
= NULL
, *a
;
770 ret
= kdc_via_plugin(context
, hi
, context
->kdc_timeout
,
771 ctx
->send_data
, &ctx
->response
);
774 } else if (ret
!= KRB5_PLUGIN_NO_HANDLE
) {
775 _krb5_debug(context
, 5, "send via plugin failed %s: %d",
781 * If we have a proxy, let use the address of the proxy instead of
782 * the KDC and let the proxy deal with the resolving of the KDC.
785 gettimeofday(&nrstart
, NULL
);
787 if (hi
->proto
== KRB5_KRBHST_HTTP
&& context
->http_proxy
) {
788 char *proxy2
= strdup(context
->http_proxy
);
789 char *el
, *proxy
= proxy2
;
790 struct addrinfo hints
;
791 char portstr
[NI_MAXSERV
];
795 if (strncmp(proxy
, "http://", 7) == 0)
798 /* check for url terminating slash */
799 el
= strchr(proxy
, '/');
803 /* check for port in hostname, used below as port */
804 el
= strchr(proxy
, ':');
808 memset(&hints
, 0, sizeof(hints
));
809 hints
.ai_family
= PF_UNSPEC
;
810 hints
.ai_socktype
= SOCK_STREAM
;
812 snprintf(portstr
, sizeof(portstr
), "%d",
813 ntohs(init_port(el
, htons(80))));
815 ret
= getaddrinfo(proxy
, portstr
, &hints
, &ai
);
818 return krb5_eai_to_heim_errno(ret
, errno
);
823 ret
= krb5_krbhst_get_addrinfo(context
, hi
, &ai
);
829 gettimeofday(&nrstop
, NULL
);
830 timevalsub(&nrstop
, &nrstart
);
831 timevaladd(&ctx
->stats
.name_resolution
, &nrstop
);
833 ctx
->stats
.num_hosts
++;
835 for (a
= ai
; a
!= NULL
; a
= a
->ai_next
) {
838 fd
= socket(a
->ai_family
, a
->ai_socktype
| SOCK_CLOEXEC
, a
->ai_protocol
);
839 if (rk_IS_BAD_SOCKET(fd
))
843 #ifndef NO_LIMIT_FD_SETSIZE
844 if (fd
>= FD_SETSIZE
) {
845 _krb5_debug(context
, 0, "fd too large for select");
850 socket_set_nonblocking(fd
, 1);
852 host
= heim_alloc(sizeof(*host
), "sendto-host", deallocate_host
);
860 /* next version of stid */
861 host
->tid
= ctx
->stid
= (ctx
->stid
& 0xffff0000) | ((ctx
->stid
& 0xffff) + 1);
863 host
->state
= CONNECT
;
865 switch (host
->hi
->proto
) {
866 case KRB5_KRBHST_HTTP
:
867 host
->fun
= &http_fun
;
869 case KRB5_KRBHST_TCP
:
870 host
->fun
= &tcp_fun
;
872 case KRB5_KRBHST_UDP
:
873 host
->fun
= &udp_fun
;
876 heim_abort("undefined http transport protocol: %d", (int)host
->hi
->proto
);
879 host
->tries
= host
->fun
->ntries
;
882 * Connect directly next host, wait a host_timeout for each next address
884 if (submitted_host
== 0)
885 host_connect(context
, ctx
, host
);
887 debug_host(context
, 5, host
,
888 "Queuing host in future (in %ds), its the %lu address on the same name",
889 (int)(context
->host_timeout
* submitted_host
), submitted_host
+ 1);
890 host
->timeout
= time(NULL
) + (submitted_host
* context
->host_timeout
);
893 heim_array_append_value(ctx
->hosts
, host
);
904 return KRB5_KDC_UNREACH
;
910 krb5_context context
;
920 wait_setup(heim_object_t obj
, void *iter_ctx
, int *stop
)
922 struct wait_ctx
*wait_ctx
= iter_ctx
;
923 struct host
*h
= (struct host
*)obj
;
925 /* skip dead hosts */
926 if (h
->state
== DEAD
)
929 if (h
->state
== CONNECT
) {
930 if (h
->timeout
< wait_ctx
->timenow
)
931 host_connect(wait_ctx
->context
, wait_ctx
->ctx
, h
);
935 /* if host timed out, dec tries and (retry or kill host) */
936 if (h
->timeout
< wait_ctx
->timenow
) {
937 heim_assert(h
->tries
!= 0, "tries should not reach 0");
940 host_dead(wait_ctx
->context
, h
, "host timed out");
943 debug_host(wait_ctx
->context
, 5, h
, "retrying sending to");
944 host_next_timeout(wait_ctx
->context
, h
);
945 host_connected(wait_ctx
->context
, wait_ctx
->ctx
, h
);
949 #ifndef NO_LIMIT_FD_SETSIZE
950 heim_assert(h
->fd
< FD_SETSIZE
, "fd too large");
954 FD_SET(h
->fd
, &wait_ctx
->rfds
);
958 FD_SET(h
->fd
, &wait_ctx
->rfds
);
959 FD_SET(h
->fd
, &wait_ctx
->wfds
);
962 heim_abort("invalid sendto host state");
964 if (h
->fd
> wait_ctx
->max_fd
)
965 wait_ctx
->max_fd
= h
->fd
;
969 wait_filter_dead(heim_object_t obj
, void *ctx
)
971 struct host
*h
= (struct host
*)obj
;
972 return (int)((h
->state
== DEAD
) ? true : false);
976 wait_process(heim_object_t obj
, void *ctx
, int *stop
)
978 struct wait_ctx
*wait_ctx
= ctx
;
979 struct host
*h
= (struct host
*)obj
;
980 int readable
, writeable
;
981 heim_assert(h
->state
!= DEAD
, "dead host resurected");
983 #ifndef NO_LIMIT_FD_SETSIZE
984 heim_assert(h
->fd
< FD_SETSIZE
, "fd too large");
986 readable
= FD_ISSET(h
->fd
, &wait_ctx
->rfds
);
987 writeable
= FD_ISSET(h
->fd
, &wait_ctx
->wfds
);
989 if (readable
|| writeable
|| h
->state
== CONNECT
)
990 wait_ctx
->got_reply
|= eval_host_state(wait_ctx
->context
, wait_ctx
->ctx
, h
, readable
, writeable
);
992 /* if there is already a reply, just fall though the array */
993 if (wait_ctx
->got_reply
)
997 static krb5_error_code
998 wait_response(krb5_context context
, int *action
, krb5_sendto_ctx ctx
)
1000 struct wait_ctx wait_ctx
;
1004 wait_ctx
.context
= context
;
1006 FD_ZERO(&wait_ctx
.rfds
);
1007 FD_ZERO(&wait_ctx
.wfds
);
1008 wait_ctx
.max_fd
= 0;
1010 /* oh, we have a reply, it must be a plugin that got it for us */
1011 if (ctx
->response
.length
) {
1012 *action
= KRB5_SENDTO_FILTER
;
1016 wait_ctx
.timenow
= time(NULL
);
1018 heim_array_iterate_f(ctx
->hosts
, &wait_ctx
, wait_setup
);
1019 heim_array_filter_f(ctx
->hosts
, &wait_ctx
, wait_filter_dead
);
1021 if (heim_array_get_length(ctx
->hosts
) == 0) {
1022 if (ctx
->stateflags
& KRBHST_COMPLETED
) {
1023 _krb5_debug(context
, 5, "no more hosts to send/recv packets to/from "
1024 "trying to pulling more hosts");
1025 *action
= KRB5_SENDTO_FAILED
;
1027 _krb5_debug(context
, 5, "no more hosts to send/recv packets to/from "
1028 "and no more hosts -> failure");
1029 *action
= KRB5_SENDTO_TIMEOUT
;
1037 ret
= select(wait_ctx
.max_fd
+ 1, &wait_ctx
.rfds
, &wait_ctx
.wfds
, NULL
, &tv
);
1041 *action
= KRB5_SENDTO_TIMEOUT
;
1045 wait_ctx
.got_reply
= 0;
1046 heim_array_iterate_f(ctx
->hosts
, &wait_ctx
, wait_process
);
1047 if (wait_ctx
.got_reply
)
1048 *action
= KRB5_SENDTO_FILTER
;
1050 *action
= KRB5_SENDTO_CONTINUE
;
1056 reset_context(krb5_context context
, krb5_sendto_ctx ctx
)
1058 krb5_data_free(&ctx
->response
);
1059 heim_release(ctx
->hosts
);
1060 ctx
->hosts
= heim_array_create();
1061 ctx
->stateflags
= 0;
1069 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1070 krb5_sendto_context(krb5_context context
,
1071 krb5_sendto_ctx ctx
,
1072 const krb5_data
*send_data
,
1073 krb5_const_realm realm
,
1076 krb5_error_code ret
= 0;
1077 krb5_krbhst_handle handle
= NULL
;
1078 struct timeval nrstart
, nrstop
, stop_time
;
1079 int type
, freectx
= 0;
1083 krb5_data_zero(receive
);
1086 ret
= krb5_sendto_ctx_alloc(context
, &ctx
);
1092 ctx
->stid
= (context
->num_kdc_requests
++) << 16;
1094 memset(&ctx
->stats
, 0, sizeof(ctx
->stats
));
1095 gettimeofday(&ctx
->stats
.start_time
, NULL
);
1099 if ((ctx
->flags
& KRB5_KRBHST_FLAGS_MASTER
) || context
->use_admin_kdc
)
1100 type
= KRB5_KRBHST_ADMIN
;
1102 type
= KRB5_KRBHST_KDC
;
1105 ctx
->send_data
= send_data
;
1107 if ((int)send_data
->length
> context
->large_msg_size
)
1108 ctx
->flags
|= KRB5_KRBHST_FLAGS_LARGE_MSG
;
1110 /* loop until we get back a appropriate response */
1112 action
= KRB5_SENDTO_INITIAL
;
1114 while (action
!= KRB5_SENDTO_DONE
&& action
!= KRB5_SENDTO_FAILED
) {
1115 krb5_krbhst_info
*hi
;
1118 case KRB5_SENDTO_INITIAL
:
1119 ret
= realm_via_plugin(context
, realm
, context
->kdc_timeout
,
1120 send_data
, &ctx
->response
);
1121 if (ret
== 0 || ret
!= KRB5_PLUGIN_NO_HANDLE
) {
1122 action
= KRB5_SENDTO_DONE
;
1125 action
= KRB5_SENDTO_KRBHST
;
1127 case KRB5_SENDTO_KRBHST
:
1128 if (ctx
->krbhst
== NULL
) {
1129 ret
= krb5_krbhst_init_flags(context
, realm
, type
,
1130 ctx
->flags
, &handle
);
1134 if (ctx
->hostname
) {
1135 ret
= krb5_krbhst_set_hostname(context
, handle
, ctx
->hostname
);
1141 handle
= heim_retain(ctx
->krbhst
);
1143 action
= KRB5_SENDTO_TIMEOUT
;
1145 case KRB5_SENDTO_TIMEOUT
:
1148 * If we completed, just got to next step
1151 if (ctx
->stateflags
& KRBHST_COMPLETED
) {
1152 action
= KRB5_SENDTO_CONTINUE
;
1157 * Pull out next host, if there is no more, close the
1158 * handle and mark as completed.
1160 * Collect time spent in krbhst (dns, plugin, etc)
1164 gettimeofday(&nrstart
, NULL
);
1166 ret
= krb5_krbhst_next(context
, handle
, &hi
);
1168 gettimeofday(&nrstop
, NULL
);
1169 timevalsub(&nrstop
, &nrstart
);
1170 timevaladd(&ctx
->stats
.krbhst
, &nrstop
);
1172 action
= KRB5_SENDTO_CONTINUE
;
1174 _krb5_debug(context
, 5, "submissing new requests to new host");
1175 if (submit_request(context
, ctx
, hi
) != 0)
1176 action
= KRB5_SENDTO_TIMEOUT
;
1178 _krb5_debug(context
, 5, "out of hosts, waiting for replies");
1179 ctx
->stateflags
|= KRBHST_COMPLETED
;
1183 case KRB5_SENDTO_CONTINUE
:
1185 ret
= wait_response(context
, &action
, ctx
);
1190 case KRB5_SENDTO_RESET
:
1192 _krb5_debug(context
, 5,
1193 "krb5_sendto trying over again (reset): %d",
1195 reset_context(context
, ctx
);
1197 krb5_krbhst_free(context
, handle
);
1202 action
= KRB5_SENDTO_FAILED
;
1204 action
= KRB5_SENDTO_KRBHST
;
1207 case KRB5_SENDTO_FILTER
:
1208 /* default to next state, the filter function might modify this */
1209 action
= KRB5_SENDTO_DONE
;
1212 ret
= (*ctx
->func
)(context
, ctx
, ctx
->data
,
1213 &ctx
->response
, &action
);
1218 case KRB5_SENDTO_FAILED
:
1219 ret
= KRB5_KDC_UNREACH
;
1221 case KRB5_SENDTO_DONE
:
1225 heim_abort("invalid krb5_sendto_context state");
1229 gettimeofday(&stop_time
, NULL
);
1230 timevalsub(&stop_time
, &ctx
->stats
.start_time
);
1233 if (ret
== 0 && ctx
->response
.length
) {
1234 *receive
= ctx
->response
;
1235 krb5_data_zero(&ctx
->response
);
1237 krb5_data_free(&ctx
->response
);
1238 krb5_clear_error_message (context
);
1239 ret
= KRB5_KDC_UNREACH
;
1240 krb5_set_error_message(context
, ret
,
1241 N_("unable to reach any KDC in realm %s", ""),
1245 _krb5_debug(context
, 1,
1246 "krb5_sendto_context %s done: %d hosts %lu packets %lu wc: %ld.%06ld nr: %ld.%06ld kh: %ld.%06ld tid: %08x",
1248 ctx
->stats
.num_hosts
, ctx
->stats
.sent_packets
,
1249 stop_time
.tv_sec
, (long)stop_time
.tv_usec
,
1250 ctx
->stats
.name_resolution
.tv_sec
, (long)ctx
->stats
.name_resolution
.tv_usec
,
1251 ctx
->stats
.krbhst
.tv_sec
, (long)ctx
->stats
.krbhst
.tv_usec
, ctx
->stid
);
1255 krb5_sendto_ctx_free(context
, ctx
);
1257 reset_context(context
, ctx
);
1260 krb5_krbhst_free(context
, handle
);