No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / krb5 / send_to_kdc.c
blob408c043d6ca9f16d83831f6baa6304203f1c8891
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"
36 __RCSID("$Heimdal: send_to_kdc.c 21934 2007-08-27 14:21:04Z lha $"
37 "$NetBSD$");
39 struct send_to_kdc {
40 krb5_send_to_kdc_func func;
41 void *data;
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
51 static int
52 recv_loop (int fd,
53 time_t tmout,
54 int udp,
55 size_t limit,
56 krb5_data *rep)
58 fd_set fdset;
59 struct timeval timeout;
60 int ret;
61 int nbytes;
63 if (fd >= FD_SETSIZE) {
64 return -1;
67 krb5_data_zero(rep);
68 do {
69 FD_ZERO(&fdset);
70 FD_SET(fd, &fdset);
71 timeout.tv_sec = tmout;
72 timeout.tv_usec = 0;
73 ret = select (fd + 1, &fdset, NULL, NULL, &timeout);
74 if (ret < 0) {
75 if (errno == EINTR)
76 continue;
77 return -1;
78 } else if (ret == 0) {
79 return 0;
80 } else {
81 void *tmp;
83 if (ioctl (fd, FIONREAD, &nbytes) < 0) {
84 krb5_data_free (rep);
85 return -1;
87 if(nbytes <= 0)
88 return 0;
90 if (limit)
91 nbytes = min(nbytes, limit - rep->length);
93 tmp = realloc (rep->data, rep->length + nbytes);
94 if (tmp == NULL) {
95 krb5_data_free (rep);
96 return -1;
98 rep->data = tmp;
99 ret = recv (fd, (char*)tmp + rep->length, nbytes, 0);
100 if (ret < 0) {
101 krb5_data_free (rep);
102 return -1;
104 rep->length += ret;
106 } while(!udp && (limit == 0 || rep->length < limit));
107 return 0;
111 * Send kerberos requests and receive a reply on a udp or any other kind
112 * of a datagram socket. See `recv_loop'.
115 static int
116 send_and_recv_udp(int fd,
117 time_t tmout,
118 const krb5_data *req,
119 krb5_data *rep)
121 if (send (fd, req->data, req->length, 0) < 0)
122 return -1;
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.
134 static int
135 send_and_recv_tcp(int fd,
136 time_t tmout,
137 const krb5_data *req,
138 krb5_data *rep)
140 unsigned char len[4];
141 unsigned long rep_len;
142 krb5_data len_data;
144 _krb5_put_int(len, req->length, 4);
145 if(net_write(fd, len, sizeof(len)) < 0)
146 return -1;
147 if(net_write(fd, req->data, req->length) < 0)
148 return -1;
149 if (recv_loop (fd, tmout, 0, 4, &len_data) < 0)
150 return -1;
151 if (len_data.length != 4) {
152 krb5_data_free (&len_data);
153 return -1;
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)
158 return -1;
159 if(rep->length != rep_len) {
160 krb5_data_free (rep);
161 return -1;
163 return 0;
167 _krb5_send_and_recv_tcp(int fd,
168 time_t tmout,
169 const krb5_data *req,
170 krb5_data *rep)
172 return send_and_recv_tcp(fd, tmout, req, rep);
176 * `send_and_recv' tailored for the HTTP protocol.
179 static int
180 send_and_recv_http(int fd,
181 time_t tmout,
182 const char *prefix,
183 const krb5_data *req,
184 krb5_data *rep)
186 char *request;
187 char *str;
188 int ret;
189 int len = base64_encode(req->data, req->length, &str);
191 if(len < 0)
192 return -1;
193 asprintf(&request, "GET %s%s HTTP/1.0\r\n\r\n", prefix, str);
194 free(str);
195 if (request == NULL)
196 return -1;
197 ret = net_write (fd, request, strlen(request));
198 free (request);
199 if (ret < 0)
200 return ret;
201 ret = recv_loop(fd, tmout, 0, 0, rep);
202 if(ret)
203 return ret;
205 unsigned long rep_len;
206 char *s, *p;
208 s = realloc(rep->data, rep->length + 1);
209 if (s == NULL) {
210 krb5_data_free (rep);
211 return -1;
213 s[rep->length] = 0;
214 p = strstr(s, "\r\n\r\n");
215 if(p == NULL) {
216 krb5_data_zero(rep);
217 free(s);
218 return -1;
220 p += 4;
221 rep->data = s;
222 rep->length -= p - s;
223 if(rep->length < 4) { /* remove length */
224 krb5_data_zero(rep);
225 free(s);
226 return -1;
228 rep->length -= 4;
229 _krb5_get_int(p, &rep_len, 4);
230 if (rep_len != rep->length) {
231 krb5_data_zero(rep);
232 free(s);
233 return -1;
235 memmove(rep->data, p + 4, rep->length);
237 return 0;
240 static int
241 init_port(const char *s, int fallback)
243 if (s) {
244 int tmp;
246 sscanf (s, "%d", &tmp);
247 return htons(tmp);
248 } else
249 return fallback;
253 * Return 0 if succesful, otherwise 1
256 static int
257 send_via_proxy (krb5_context context,
258 const krb5_krbhst_info *hi,
259 const krb5_data *send_data,
260 krb5_data *receive)
262 char *proxy2 = strdup(context->http_proxy);
263 char *proxy = proxy2;
264 char *prefix;
265 char *colon;
266 struct addrinfo hints;
267 struct addrinfo *ai, *a;
268 int ret;
269 int s = -1;
270 char portstr[NI_MAXSERV];
272 if (proxy == NULL)
273 return ENOMEM;
274 if (strncmp (proxy, "http://", 7) == 0)
275 proxy += 7;
277 colon = strchr(proxy, ':');
278 if(colon != NULL)
279 *colon++ = '\0';
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);
286 free (proxy2);
287 if (ret)
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);
292 if (s < 0)
293 continue;
294 if (connect (s, a->ai_addr, a->ai_addrlen) < 0) {
295 close (s);
296 continue;
298 break;
300 if (a == NULL) {
301 freeaddrinfo (ai);
302 return 1;
304 freeaddrinfo (ai);
306 asprintf(&prefix, "http://%s/", hi->hostname);
307 if(prefix == NULL) {
308 close(s);
309 return 1;
311 ret = send_and_recv_http(s, context->kdc_timeout,
312 prefix, send_data, receive);
313 close (s);
314 free(prefix);
315 if(ret == 0 && receive->length != 0)
316 return 0;
317 return 1;
321 * Send the data `send' to one host from `handle` and get back the reply
322 * in `receive'.
325 krb5_error_code KRB5_LIB_FUNCTION
326 krb5_sendto (krb5_context context,
327 const krb5_data *send_data,
328 krb5_krbhst_handle handle,
329 krb5_data *receive)
331 krb5_error_code ret;
332 int fd;
333 int i;
335 krb5_data_zero(receive);
337 for (i = 0; i < context->max_retries; ++i) {
338 krb5_krbhst_info *hi;
340 while (krb5_krbhst_next(context, handle, &hi) == 0) {
341 struct addrinfo *ai, *a;
343 if (context->send_to_kdc) {
344 struct send_to_kdc *s = context->send_to_kdc;
346 ret = (*s->func)(context, s->data,
347 hi, send_data, receive);
348 if (ret == 0 && receive->length != 0)
349 goto out;
350 continue;
353 if(hi->proto == KRB5_KRBHST_HTTP && context->http_proxy) {
354 if (send_via_proxy (context, hi, send_data, receive) == 0) {
355 ret = 0;
356 goto out;
358 continue;
361 ret = krb5_krbhst_get_addrinfo(context, hi, &ai);
362 if (ret)
363 continue;
365 for (a = ai; a != NULL; a = a->ai_next) {
366 fd = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
367 if (fd < 0)
368 continue;
369 if (connect (fd, a->ai_addr, a->ai_addrlen) < 0) {
370 close (fd);
371 continue;
373 switch (hi->proto) {
374 case KRB5_KRBHST_HTTP :
375 ret = send_and_recv_http(fd, context->kdc_timeout,
376 "", send_data, receive);
377 break;
378 case KRB5_KRBHST_TCP :
379 ret = send_and_recv_tcp (fd, context->kdc_timeout,
380 send_data, receive);
381 break;
382 case KRB5_KRBHST_UDP :
383 ret = send_and_recv_udp (fd, context->kdc_timeout,
384 send_data, receive);
385 break;
387 close (fd);
388 if(ret == 0 && receive->length != 0)
389 goto out;
392 krb5_krbhst_reset(context, handle);
394 krb5_clear_error_string (context);
395 ret = KRB5_KDC_UNREACH;
396 out:
397 return ret;
400 krb5_error_code KRB5_LIB_FUNCTION
401 krb5_sendto_kdc(krb5_context context,
402 const krb5_data *send_data,
403 const krb5_realm *realm,
404 krb5_data *receive)
406 return krb5_sendto_kdc_flags(context, send_data, realm, receive, 0);
409 krb5_error_code KRB5_LIB_FUNCTION
410 krb5_sendto_kdc_flags(krb5_context context,
411 const krb5_data *send_data,
412 const krb5_realm *realm,
413 krb5_data *receive,
414 int flags)
416 krb5_error_code ret;
417 krb5_sendto_ctx ctx;
419 ret = krb5_sendto_ctx_alloc(context, &ctx);
420 if (ret)
421 return ret;
422 krb5_sendto_ctx_add_flags(ctx, flags);
423 krb5_sendto_ctx_set_func(ctx, _krb5_kdc_retry, NULL);
425 ret = krb5_sendto_context(context, ctx, send_data, *realm, receive);
426 krb5_sendto_ctx_free(context, ctx);
427 return ret;
430 krb5_error_code KRB5_LIB_FUNCTION
431 krb5_set_send_to_kdc_func(krb5_context context,
432 krb5_send_to_kdc_func func,
433 void *data)
435 free(context->send_to_kdc);
436 if (func == NULL) {
437 context->send_to_kdc = NULL;
438 return 0;
441 context->send_to_kdc = malloc(sizeof(*context->send_to_kdc));
442 if (context->send_to_kdc == NULL) {
443 krb5_set_error_string(context, "Out of memory");
444 return ENOMEM;
447 context->send_to_kdc->func = func;
448 context->send_to_kdc->data = data;
449 return 0;
452 struct krb5_sendto_ctx_data {
453 int flags;
454 int type;
455 krb5_sendto_ctx_func func;
456 void *data;
459 krb5_error_code KRB5_LIB_FUNCTION
460 krb5_sendto_ctx_alloc(krb5_context context, krb5_sendto_ctx *ctx)
462 *ctx = calloc(1, sizeof(**ctx));
463 if (*ctx == NULL) {
464 krb5_set_error_string(context, "out of memory");
465 return ENOMEM;
467 return 0;
470 void KRB5_LIB_FUNCTION
471 krb5_sendto_ctx_add_flags(krb5_sendto_ctx ctx, int flags)
473 ctx->flags |= flags;
476 int KRB5_LIB_FUNCTION
477 krb5_sendto_ctx_get_flags(krb5_sendto_ctx ctx)
479 return ctx->flags;
482 void KRB5_LIB_FUNCTION
483 krb5_sendto_ctx_set_type(krb5_sendto_ctx ctx, int type)
485 ctx->type = type;
489 void KRB5_LIB_FUNCTION
490 krb5_sendto_ctx_set_func(krb5_sendto_ctx ctx,
491 krb5_sendto_ctx_func func,
492 void *data)
494 ctx->func = func;
495 ctx->data = data;
498 void KRB5_LIB_FUNCTION
499 krb5_sendto_ctx_free(krb5_context context, krb5_sendto_ctx ctx)
501 memset(ctx, 0, sizeof(*ctx));
502 free(ctx);
505 krb5_error_code KRB5_LIB_FUNCTION
506 krb5_sendto_context(krb5_context context,
507 krb5_sendto_ctx ctx,
508 const krb5_data *send_data,
509 const krb5_realm realm,
510 krb5_data *receive)
512 krb5_error_code ret;
513 krb5_krbhst_handle handle = NULL;
514 int type, freectx = 0;
515 int action;
517 krb5_data_zero(receive);
519 if (ctx == NULL) {
520 freectx = 1;
521 ret = krb5_sendto_ctx_alloc(context, &ctx);
522 if (ret)
523 return ret;
526 type = ctx->type;
527 if (type == 0) {
528 if ((ctx->flags & KRB5_KRBHST_FLAGS_MASTER) || context->use_admin_kdc)
529 type = KRB5_KRBHST_ADMIN;
530 else
531 type = KRB5_KRBHST_KDC;
534 if (send_data->length > context->large_msg_size)
535 ctx->flags |= KRB5_KRBHST_FLAGS_LARGE_MSG;
537 /* loop until we get back a appropriate response */
539 do {
540 action = KRB5_SENDTO_DONE;
542 krb5_data_free(receive);
544 if (handle == NULL) {
545 ret = krb5_krbhst_init_flags(context, realm, type,
546 ctx->flags, &handle);
547 if (ret) {
548 if (freectx)
549 krb5_sendto_ctx_free(context, ctx);
550 return ret;
554 ret = krb5_sendto(context, send_data, handle, receive);
555 if (ret)
556 break;
557 if (ctx->func) {
558 ret = (*ctx->func)(context, ctx, ctx->data, receive, &action);
559 if (ret)
560 break;
562 if (action != KRB5_SENDTO_CONTINUE) {
563 krb5_krbhst_free(context, handle);
564 handle = NULL;
566 } while (action != KRB5_SENDTO_DONE);
567 if (handle)
568 krb5_krbhst_free(context, handle);
569 if (ret == KRB5_KDC_UNREACH)
570 krb5_set_error_string(context,
571 "unable to reach any KDC in realm %s", realm);
572 if (ret)
573 krb5_data_free(receive);
574 if (freectx)
575 krb5_sendto_ctx_free(context, ctx);
576 return ret;
579 krb5_error_code
580 _krb5_kdc_retry(krb5_context context, krb5_sendto_ctx ctx, void *data,
581 const krb5_data *reply, int *action)
583 krb5_error_code ret;
584 KRB_ERROR error;
586 if(krb5_rd_error(context, reply, &error))
587 return 0;
589 ret = krb5_error_from_rd_error(context, &error, NULL);
590 krb5_free_error_contents(context, &error);
592 switch(ret) {
593 case KRB5KRB_ERR_RESPONSE_TOO_BIG: {
594 if (krb5_sendto_ctx_get_flags(ctx) & KRB5_KRBHST_FLAGS_LARGE_MSG)
595 break;
596 krb5_sendto_ctx_add_flags(ctx, KRB5_KRBHST_FLAGS_LARGE_MSG);
597 *action = KRB5_SENDTO_RESTART;
598 break;
600 case KRB5KDC_ERR_SVC_UNAVAILABLE:
601 *action = KRB5_SENDTO_CONTINUE;
602 break;
604 return 0;