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"
37 RCSID("$Id: send_to_kdc.c 23448 2008-07-27 12:09:22Z lha $");
40 krb5_send_to_kdc_func func;
45 * send the data in `req' on the socket `fd' (which is datagram iff udp)
46 * waiting `tmout' for a reply and returning the reply in `rep'.
47 * iff limit read up to this many bytes
48 * returns 0 and data in `rep' if succesful, otherwise -1
59 struct timeval timeout;
63 if (fd >= FD_SETSIZE) {
71 timeout.tv_sec = tmout;
73 ret = select (fd + 1, &fdset, NULL, NULL, &timeout);
78 } else if (ret == 0) {
83 if (ioctl (fd, FIONREAD, &nbytes) < 0) {
91 nbytes = min(nbytes, limit - rep->length);
93 tmp = realloc (rep->data, rep->length + nbytes);
99 ret = recv (fd, (char*)tmp + rep->length, nbytes, 0);
101 krb5_data_free (rep);
106 } while(!udp && (limit == 0 || rep->length < limit));
111 * Send kerberos requests and receive a reply on a udp or any other kind
112 * of a datagram socket. See `recv_loop'.
116 send_and_recv_udp(int fd,
118 const krb5_data *req,
121 if (send (fd, req->data, req->length, 0) < 0)
124 return recv_loop(fd, tmout, 1, 0, rep);
128 * `send_and_recv' for a TCP (or any other stream) socket.
129 * Since there are no record limits on a stream socket the protocol here
130 * is to prepend the request with 4 bytes of its length and the reply
131 * is similarly encoded.
135 send_and_recv_tcp(int fd,
137 const krb5_data *req,
140 unsigned char len[4];
141 unsigned long rep_len;
144 _krb5_put_int(len, req->length, 4);
145 if(net_write(fd, len, sizeof(len)) < 0)
147 if(net_write(fd, req->data, req->length) < 0)
149 if (recv_loop (fd, tmout, 0, 4, &len_data) < 0)
151 if (len_data.length != 4) {
152 krb5_data_free (&len_data);
155 _krb5_get_int(len_data.data, &rep_len, 4);
156 krb5_data_free (&len_data);
157 if (recv_loop (fd, tmout, 0, rep_len, rep) < 0)
159 if(rep->length != rep_len) {
160 krb5_data_free (rep);
167 _krb5_send_and_recv_tcp(int fd,
169 const krb5_data *req,
172 return send_and_recv_tcp(fd, tmout, req, rep);
176 * `send_and_recv' tailored for the HTTP protocol.
180 send_and_recv_http(int fd,
183 const krb5_data *req,
189 int len = base64_encode(req->data, req->length, &str);
193 asprintf(&request, "GET %s%s HTTP/1.0\r\n\r\n", prefix, str);
197 ret = net_write (fd, request, strlen(request));
201 ret = recv_loop(fd, tmout, 0, 0, rep);
205 unsigned long rep_len;
208 s = realloc(rep->data, rep->length + 1);
210 krb5_data_free (rep);
214 p = strstr(s, "\r\n\r\n");
222 rep->length -= p - s;
223 if(rep->length < 4) { /* remove length */
229 _krb5_get_int(p, &rep_len, 4);
230 if (rep_len != rep->length) {
235 memmove(rep->data, p + 4, rep->length);
241 init_port(const char *s, int fallback)
246 sscanf (s, "%d", &tmp);
253 * Return 0 if succesful, otherwise 1
257 send_via_proxy (krb5_context context,
258 const krb5_krbhst_info *hi,
259 const krb5_data *send_data,
262 char *proxy2 = strdup(context->http_proxy);
263 char *proxy = proxy2;
266 struct addrinfo hints;
267 struct addrinfo *ai, *a;
270 char portstr[NI_MAXSERV];
274 if (strncmp (proxy, "http://", 7) == 0)
277 colon = strchr(proxy, ':');
280 memset (&hints, 0, sizeof(hints));
281 hints.ai_family = PF_UNSPEC;
282 hints.ai_socktype = SOCK_STREAM;
283 snprintf (portstr, sizeof(portstr), "%d",
284 ntohs(init_port (colon, htons(80))));
285 ret = getaddrinfo (proxy, portstr, &hints, &ai);
288 return krb5_eai_to_heim_errno(ret, errno);
290 for (a = ai; a != NULL; a = a->ai_next) {
291 s = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
295 if (connect (s, a->ai_addr, a->ai_addrlen) < 0) {
307 asprintf(&prefix, "http://%s/", hi->hostname);
312 ret = send_and_recv_http(s, context->kdc_timeout,
313 prefix, send_data, receive);
316 if(ret == 0 && receive->length != 0)
321 static krb5_error_code
322 send_via_plugin(krb5_context context,
323 krb5_krbhst_info *hi,
325 const krb5_data *send_data,
328 struct krb5_plugin *list = NULL, *e;
331 ret = _krb5_plugin_find(context, PLUGIN_TYPE_DATA, KRB5_PLUGIN_SEND_TO_KDC, &list);
332 if(ret != 0 || list == NULL)
333 return KRB5_PLUGIN_NO_HANDLE;
335 for (e = list; e != NULL; e = _krb5_plugin_get_next(e)) {
336 krb5plugin_send_to_kdc_ftable *service;
339 service = _krb5_plugin_get_symbol(e);
340 if (service->minor_version != 0)
343 (*service->init)(context, &ctx);
344 ret = (*service->send_to_kdc)(context, ctx, hi,
345 timeout, send_data, receive);
346 (*service->fini)(ctx);
349 if (ret != KRB5_PLUGIN_NO_HANDLE) {
350 krb5_set_error_message(context, ret,
351 "Plugin %s failed to lookup with error: %d",
352 KRB5_PLUGIN_SEND_TO_KDC, ret);
356 _krb5_plugin_free(list);
357 return KRB5_PLUGIN_NO_HANDLE;
362 * Send the data `send' to one host from `handle` and get back the reply
366 krb5_error_code KRB5_LIB_FUNCTION
367 krb5_sendto (krb5_context context,
368 const krb5_data *send_data,
369 krb5_krbhst_handle handle,
376 krb5_data_zero(receive);
378 for (i = 0; i < context->max_retries; ++i) {
379 krb5_krbhst_info *hi;
381 while (krb5_krbhst_next(context, handle, &hi) == 0) {
382 struct addrinfo *ai, *a;
384 if (context->send_to_kdc) {
385 struct send_to_kdc *s = context->send_to_kdc;
387 ret = (*s->func)(context, s->data,
388 hi, context->kdc_timeout, send_data, receive);
389 if (ret == 0 && receive->length != 0)
394 ret = send_via_plugin(context, hi, context->kdc_timeout,
396 if (ret == 0 && receive->length != 0)
398 else if (ret != KRB5_PLUGIN_NO_HANDLE)
401 if(hi->proto == KRB5_KRBHST_HTTP && context->http_proxy) {
402 if (send_via_proxy (context, hi, send_data, receive) == 0) {
409 ret = krb5_krbhst_get_addrinfo(context, hi, &ai);
413 for (a = ai; a != NULL; a = a->ai_next) {
414 fd = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
418 if (connect (fd, a->ai_addr, a->ai_addrlen) < 0) {
423 case KRB5_KRBHST_HTTP :
424 ret = send_and_recv_http(fd, context->kdc_timeout,
425 "", send_data, receive);
427 case KRB5_KRBHST_TCP :
428 ret = send_and_recv_tcp (fd, context->kdc_timeout,
431 case KRB5_KRBHST_UDP :
432 ret = send_and_recv_udp (fd, context->kdc_timeout,
437 if(ret == 0 && receive->length != 0)
441 krb5_krbhst_reset(context, handle);
443 krb5_clear_error_string (context);
444 ret = KRB5_KDC_UNREACH;
449 krb5_error_code KRB5_LIB_FUNCTION
450 krb5_sendto_kdc(krb5_context context,
451 const krb5_data *send_data,
452 const krb5_realm *realm,
455 return krb5_sendto_kdc_flags(context, send_data, realm, receive, 0);
458 krb5_error_code KRB5_LIB_FUNCTION
459 krb5_sendto_kdc_flags(krb5_context context,
460 const krb5_data *send_data,
461 const krb5_realm *realm,
468 ret = krb5_sendto_ctx_alloc(context, &ctx);
471 krb5_sendto_ctx_add_flags(ctx, flags);
472 krb5_sendto_ctx_set_func(ctx, _krb5_kdc_retry, NULL);
474 ret = krb5_sendto_context(context, ctx, send_data, *realm, receive);
475 krb5_sendto_ctx_free(context, ctx);
479 krb5_error_code KRB5_LIB_FUNCTION
480 krb5_set_send_to_kdc_func(krb5_context context,
481 krb5_send_to_kdc_func func,
484 free(context->send_to_kdc);
486 context->send_to_kdc = NULL;
490 context->send_to_kdc = malloc(sizeof(*context->send_to_kdc));
491 if (context->send_to_kdc == NULL) {
492 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
496 context->send_to_kdc->func = func;
497 context->send_to_kdc->data = data;
501 struct krb5_sendto_ctx_data {
504 krb5_sendto_ctx_func func;
508 krb5_error_code KRB5_LIB_FUNCTION
509 krb5_sendto_ctx_alloc(krb5_context context, krb5_sendto_ctx *ctx)
511 *ctx = calloc(1, sizeof(**ctx));
513 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
519 void KRB5_LIB_FUNCTION
520 krb5_sendto_ctx_add_flags(krb5_sendto_ctx ctx, int flags)
525 int KRB5_LIB_FUNCTION
526 krb5_sendto_ctx_get_flags(krb5_sendto_ctx ctx)
531 void KRB5_LIB_FUNCTION
532 krb5_sendto_ctx_set_type(krb5_sendto_ctx ctx, int type)
538 void KRB5_LIB_FUNCTION
539 krb5_sendto_ctx_set_func(krb5_sendto_ctx ctx,
540 krb5_sendto_ctx_func func,
547 void KRB5_LIB_FUNCTION
548 krb5_sendto_ctx_free(krb5_context context, krb5_sendto_ctx ctx)
550 memset(ctx, 0, sizeof(*ctx));
554 krb5_error_code KRB5_LIB_FUNCTION
555 krb5_sendto_context(krb5_context context,
557 const krb5_data *send_data,
558 const krb5_realm realm,
562 krb5_krbhst_handle handle = NULL;
563 int type, freectx = 0;
566 krb5_data_zero(receive);
570 ret = krb5_sendto_ctx_alloc(context, &ctx);
577 if ((ctx->flags & KRB5_KRBHST_FLAGS_MASTER) || context->use_admin_kdc)
578 type = KRB5_KRBHST_ADMIN;
580 type = KRB5_KRBHST_KDC;
583 if (send_data->length > context->large_msg_size)
584 ctx->flags |= KRB5_KRBHST_FLAGS_LARGE_MSG;
586 /* loop until we get back a appropriate response */
589 action = KRB5_SENDTO_DONE;
591 krb5_data_free(receive);
593 if (handle == NULL) {
594 ret = krb5_krbhst_init_flags(context, realm, type,
595 ctx->flags, &handle);
598 krb5_sendto_ctx_free(context, ctx);
603 ret = krb5_sendto(context, send_data, handle, receive);
607 ret = (*ctx->func)(context, ctx, ctx->data, receive, &action);
611 if (action != KRB5_SENDTO_CONTINUE) {
612 krb5_krbhst_free(context, handle);
615 } while (action != KRB5_SENDTO_DONE);
617 krb5_krbhst_free(context, handle);
618 if (ret == KRB5_KDC_UNREACH)
619 krb5_set_error_message(context, ret,
620 "unable to reach any KDC in realm %s", realm);
622 krb5_data_free(receive);
624 krb5_sendto_ctx_free(context, ctx);
629 _krb5_kdc_retry(krb5_context context, krb5_sendto_ctx ctx, void *data,
630 const krb5_data *reply, int *action)
635 if(krb5_rd_error(context, reply, &error))
638 ret = krb5_error_from_rd_error(context, &error, NULL);
639 krb5_free_error_contents(context, &error);
642 case KRB5KRB_ERR_RESPONSE_TOO_BIG: {
643 if (krb5_sendto_ctx_get_flags(ctx) & KRB5_KRBHST_FLAGS_LARGE_MSG)
645 krb5_sendto_ctx_add_flags(ctx, KRB5_KRBHST_FLAGS_LARGE_MSG);
646 *action = KRB5_SENDTO_RESTART;
649 case KRB5KDC_ERR_SVC_UNAVAILABLE:
650 *action = KRB5_SENDTO_CONTINUE;