set id = NULL to avoid warning
[heimdal.git] / lib / krb5 / send_to_kdc.c
blob50b42f2f1095e0ed0c02db8d50d9480cf9f6c18a
1 /*
2 * Copyright (c) 1997 - 2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
34 #include "krb5_locl.h"
35 #include "send_to_kdc_plugin.h"
37 struct send_to_kdc {
38 krb5_send_to_kdc_func func;
39 void *data;
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
49 static int
50 recv_loop (int fd,
51 time_t tmout,
52 int udp,
53 size_t limit,
54 krb5_data *rep)
56 fd_set fdset;
57 struct timeval timeout;
58 int ret;
59 int nbytes;
61 if (fd >= FD_SETSIZE) {
62 return -1;
65 krb5_data_zero(rep);
66 do {
67 FD_ZERO(&fdset);
68 FD_SET(fd, &fdset);
69 timeout.tv_sec = tmout;
70 timeout.tv_usec = 0;
71 ret = select (fd + 1, &fdset, NULL, NULL, &timeout);
72 if (ret < 0) {
73 if (errno == EINTR)
74 continue;
75 return -1;
76 } else if (ret == 0) {
77 return 0;
78 } else {
79 void *tmp;
81 if (ioctl (fd, FIONREAD, &nbytes) < 0) {
82 krb5_data_free (rep);
83 return -1;
85 if(nbytes <= 0)
86 return 0;
88 if (limit)
89 nbytes = min(nbytes, limit - rep->length);
91 tmp = realloc (rep->data, rep->length + nbytes);
92 if (tmp == NULL) {
93 krb5_data_free (rep);
94 return -1;
96 rep->data = tmp;
97 ret = recv (fd, (char*)tmp + rep->length, nbytes, 0);
98 if (ret < 0) {
99 krb5_data_free (rep);
100 return -1;
102 rep->length += ret;
104 } while(!udp && (limit == 0 || rep->length < limit));
105 return 0;
109 * Send kerberos requests and receive a reply on a udp or any other kind
110 * of a datagram socket. See `recv_loop'.
113 static int
114 send_and_recv_udp(int fd,
115 time_t tmout,
116 const krb5_data *req,
117 krb5_data *rep)
119 if (send (fd, req->data, req->length, 0) < 0)
120 return -1;
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.
132 static int
133 send_and_recv_tcp(int fd,
134 time_t tmout,
135 const krb5_data *req,
136 krb5_data *rep)
138 unsigned char len[4];
139 unsigned long rep_len;
140 krb5_data len_data;
142 _krb5_put_int(len, req->length, 4);
143 if(net_write(fd, len, sizeof(len)) < 0)
144 return -1;
145 if(net_write(fd, req->data, req->length) < 0)
146 return -1;
147 if (recv_loop (fd, tmout, 0, 4, &len_data) < 0)
148 return -1;
149 if (len_data.length != 4) {
150 krb5_data_free (&len_data);
151 return -1;
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)
156 return -1;
157 if(rep->length != rep_len) {
158 krb5_data_free (rep);
159 return -1;
161 return 0;
165 _krb5_send_and_recv_tcp(int fd,
166 time_t tmout,
167 const krb5_data *req,
168 krb5_data *rep)
170 return send_and_recv_tcp(fd, tmout, req, rep);
174 * `send_and_recv' tailored for the HTTP protocol.
177 static int
178 send_and_recv_http(int fd,
179 time_t tmout,
180 const char *prefix,
181 const krb5_data *req,
182 krb5_data *rep)
184 char *request;
185 char *str;
186 int ret;
187 int len = base64_encode(req->data, req->length, &str);
189 if(len < 0)
190 return -1;
191 asprintf(&request, "GET %s%s HTTP/1.0\r\n\r\n", prefix, str);
192 free(str);
193 if (request == NULL)
194 return -1;
195 ret = net_write (fd, request, strlen(request));
196 free (request);
197 if (ret < 0)
198 return ret;
199 ret = recv_loop(fd, tmout, 0, 0, rep);
200 if(ret)
201 return ret;
203 unsigned long rep_len;
204 char *s, *p;
206 s = realloc(rep->data, rep->length + 1);
207 if (s == NULL) {
208 krb5_data_free (rep);
209 return -1;
211 s[rep->length] = 0;
212 p = strstr(s, "\r\n\r\n");
213 if(p == NULL) {
214 krb5_data_zero(rep);
215 free(s);
216 return -1;
218 p += 4;
219 rep->data = s;
220 rep->length -= p - s;
221 if(rep->length < 4) { /* remove length */
222 krb5_data_zero(rep);
223 free(s);
224 return -1;
226 rep->length -= 4;
227 _krb5_get_int(p, &rep_len, 4);
228 if (rep_len != rep->length) {
229 krb5_data_zero(rep);
230 free(s);
231 return -1;
233 memmove(rep->data, p + 4, rep->length);
235 return 0;
238 static int
239 init_port(const char *s, int fallback)
241 if (s) {
242 int tmp;
244 sscanf (s, "%d", &tmp);
245 return htons(tmp);
246 } else
247 return fallback;
251 * Return 0 if succesful, otherwise 1
254 static int
255 send_via_proxy (krb5_context context,
256 const krb5_krbhst_info *hi,
257 const krb5_data *send_data,
258 krb5_data *receive)
260 char *proxy2 = strdup(context->http_proxy);
261 char *proxy = proxy2;
262 char *prefix;
263 char *colon;
264 struct addrinfo hints;
265 struct addrinfo *ai, *a;
266 int ret;
267 int s = -1;
268 char portstr[NI_MAXSERV];
270 if (proxy == NULL)
271 return ENOMEM;
272 if (strncmp (proxy, "http://", 7) == 0)
273 proxy += 7;
275 colon = strchr(proxy, ':');
276 if(colon != NULL)
277 *colon++ = '\0';
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);
284 free (proxy2);
285 if (ret)
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);
290 if (s < 0)
291 continue;
292 rk_cloexec(s);
293 if (connect (s, a->ai_addr, a->ai_addrlen) < 0) {
294 close (s);
295 continue;
297 break;
299 if (a == NULL) {
300 freeaddrinfo (ai);
301 return 1;
303 freeaddrinfo (ai);
305 asprintf(&prefix, "http://%s/", hi->hostname);
306 if(prefix == NULL) {
307 close(s);
308 return 1;
310 ret = send_and_recv_http(s, context->kdc_timeout,
311 prefix, send_data, receive);
312 close (s);
313 free(prefix);
314 if(ret == 0 && receive->length != 0)
315 return 0;
316 return 1;
319 static krb5_error_code
320 send_via_plugin(krb5_context context,
321 krb5_krbhst_info *hi,
322 time_t timeout,
323 const krb5_data *send_data,
324 krb5_data *receive)
326 struct krb5_plugin *list = NULL, *e;
327 krb5_error_code ret;
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;
335 void *ctx;
337 service = _krb5_plugin_get_symbol(e);
338 if (service->minor_version != 0)
339 continue;
341 (*service->init)(context, &ctx);
342 ret = (*service->send_to_kdc)(context, ctx, hi,
343 timeout, send_data, receive);
344 (*service->fini)(ctx);
345 if (ret == 0)
346 break;
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);
351 break;
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
361 * in `receive'.
364 krb5_error_code KRB5_LIB_FUNCTION
365 krb5_sendto (krb5_context context,
366 const krb5_data *send_data,
367 krb5_krbhst_handle handle,
368 krb5_data *receive)
370 krb5_error_code ret;
371 int fd;
372 int i;
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)
388 goto out;
389 continue;
392 ret = send_via_plugin(context, hi, context->kdc_timeout,
393 send_data, receive);
394 if (ret == 0 && receive->length != 0)
395 goto out;
396 else if (ret != KRB5_PLUGIN_NO_HANDLE)
397 continue;
399 if(hi->proto == KRB5_KRBHST_HTTP && context->http_proxy) {
400 if (send_via_proxy (context, hi, send_data, receive) == 0) {
401 ret = 0;
402 goto out;
404 continue;
407 ret = krb5_krbhst_get_addrinfo(context, hi, &ai);
408 if (ret)
409 continue;
411 for (a = ai; a != NULL; a = a->ai_next) {
412 fd = socket (a->ai_family, a->ai_socktype | SOCK_CLOEXEC, a->ai_protocol);
413 if (fd < 0)
414 continue;
415 rk_cloexec(fd);
416 if (connect (fd, a->ai_addr, a->ai_addrlen) < 0) {
417 close (fd);
418 continue;
420 switch (hi->proto) {
421 case KRB5_KRBHST_HTTP :
422 ret = send_and_recv_http(fd, context->kdc_timeout,
423 "", send_data, receive);
424 break;
425 case KRB5_KRBHST_TCP :
426 ret = send_and_recv_tcp (fd, context->kdc_timeout,
427 send_data, receive);
428 break;
429 case KRB5_KRBHST_UDP :
430 ret = send_and_recv_udp (fd, context->kdc_timeout,
431 send_data, receive);
432 break;
434 close (fd);
435 if(ret == 0 && receive->length != 0)
436 goto out;
439 krb5_krbhst_reset(context, handle);
441 krb5_clear_error_message (context);
442 ret = KRB5_KDC_UNREACH;
443 out:
444 return ret;
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,
451 krb5_data *receive)
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,
460 krb5_data *receive,
461 int flags)
463 krb5_error_code ret;
464 krb5_sendto_ctx ctx;
466 ret = krb5_sendto_ctx_alloc(context, &ctx);
467 if (ret)
468 return ret;
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);
474 return ret;
477 krb5_error_code KRB5_LIB_FUNCTION
478 krb5_set_send_to_kdc_func(krb5_context context,
479 krb5_send_to_kdc_func func,
480 void *data)
482 free(context->send_to_kdc);
483 if (func == NULL) {
484 context->send_to_kdc = NULL;
485 return 0;
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", ""));
492 return ENOMEM;
495 context->send_to_kdc->func = func;
496 context->send_to_kdc->data = data;
497 return 0;
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);
507 else
508 return krb5_set_send_to_kdc_func(to, NULL, NULL);
513 struct krb5_sendto_ctx_data {
514 int flags;
515 int type;
516 krb5_sendto_ctx_func func;
517 void *data;
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));
524 if (*ctx == NULL) {
525 krb5_set_error_message(context, ENOMEM,
526 N_("malloc: out of memory", ""));
527 return ENOMEM;
529 return 0;
532 void KRB5_LIB_FUNCTION
533 krb5_sendto_ctx_add_flags(krb5_sendto_ctx ctx, int flags)
535 ctx->flags |= flags;
538 int KRB5_LIB_FUNCTION
539 krb5_sendto_ctx_get_flags(krb5_sendto_ctx ctx)
541 return ctx->flags;
544 void KRB5_LIB_FUNCTION
545 krb5_sendto_ctx_set_type(krb5_sendto_ctx ctx, int type)
547 ctx->type = type;
551 void KRB5_LIB_FUNCTION
552 krb5_sendto_ctx_set_func(krb5_sendto_ctx ctx,
553 krb5_sendto_ctx_func func,
554 void *data)
556 ctx->func = func;
557 ctx->data = data;
560 void KRB5_LIB_FUNCTION
561 krb5_sendto_ctx_free(krb5_context context, krb5_sendto_ctx ctx)
563 memset(ctx, 0, sizeof(*ctx));
564 free(ctx);
567 krb5_error_code KRB5_LIB_FUNCTION
568 krb5_sendto_context(krb5_context context,
569 krb5_sendto_ctx ctx,
570 const krb5_data *send_data,
571 const krb5_realm realm,
572 krb5_data *receive)
574 krb5_error_code ret;
575 krb5_krbhst_handle handle = NULL;
576 int type, freectx = 0;
577 int action;
579 krb5_data_zero(receive);
581 if (ctx == NULL) {
582 freectx = 1;
583 ret = krb5_sendto_ctx_alloc(context, &ctx);
584 if (ret)
585 return ret;
588 type = ctx->type;
589 if (type == 0) {
590 if ((ctx->flags & KRB5_KRBHST_FLAGS_MASTER) || context->use_admin_kdc)
591 type = KRB5_KRBHST_ADMIN;
592 else
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 */
601 do {
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);
609 if (ret) {
610 if (freectx)
611 krb5_sendto_ctx_free(context, ctx);
612 return ret;
616 ret = krb5_sendto(context, send_data, handle, receive);
617 if (ret)
618 break;
619 if (ctx->func) {
620 ret = (*ctx->func)(context, ctx, ctx->data, receive, &action);
621 if (ret)
622 break;
624 if (action != KRB5_SENDTO_CONTINUE) {
625 krb5_krbhst_free(context, handle);
626 handle = NULL;
628 } while (action != KRB5_SENDTO_DONE);
629 if (handle)
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", ""),
634 realm);
635 if (ret)
636 krb5_data_free(receive);
637 if (freectx)
638 krb5_sendto_ctx_free(context, ctx);
639 return ret;
642 krb5_error_code
643 _krb5_kdc_retry(krb5_context context, krb5_sendto_ctx ctx, void *data,
644 const krb5_data *reply, int *action)
646 krb5_error_code ret;
647 KRB_ERROR error;
649 if(krb5_rd_error(context, reply, &error))
650 return 0;
652 ret = krb5_error_from_rd_error(context, &error, NULL);
653 krb5_free_error_contents(context, &error);
655 switch(ret) {
656 case KRB5KRB_ERR_RESPONSE_TOO_BIG: {
657 if (krb5_sendto_ctx_get_flags(ctx) & KRB5_KRBHST_FLAGS_LARGE_MSG)
658 break;
659 krb5_sendto_ctx_add_flags(ctx, KRB5_KRBHST_FLAGS_LARGE_MSG);
660 *action = KRB5_SENDTO_RESTART;
661 break;
663 case KRB5KDC_ERR_SVC_UNAVAILABLE:
664 *action = KRB5_SENDTO_CONTINUE;
665 break;
667 return 0;