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"
36 RCSID("$Id: send_to_kdc.c,v 1.48 2002/03/27 09:32:50 joda Exp $");
39 * send the data in `req' on the socket `fd' (which is datagram iff udp)
40 * waiting `tmout' for a reply and returning the reply in `rep'.
41 * iff limit read up to this many bytes
42 * returns 0 and data in `rep' if succesful, otherwise -1
53 struct timeval timeout;
57 if (fd >= FD_SETSIZE) {
65 timeout.tv_sec = tmout;
67 ret = select (fd + 1, &fdset, NULL, NULL, &timeout);
72 } else if (ret == 0) {
77 if (ioctl (fd, FIONREAD, &nbytes) < 0) {
85 nbytes = min(nbytes, limit - rep->length);
87 tmp = realloc (rep->data, rep->length + nbytes);
93 ret = recv (fd, (char*)tmp + rep->length, nbytes, 0);
100 } while(!udp && (limit == 0 || rep->length < limit));
105 * Send kerberos requests and receive a reply on a udp or any other kind
106 * of a datagram socket. See `recv_loop'.
110 send_and_recv_udp(int fd,
112 const krb5_data *req,
115 if (send (fd, req->data, req->length, 0) < 0)
118 return recv_loop(fd, tmout, 1, 0, rep);
122 * `send_and_recv' for a TCP (or any other stream) socket.
123 * Since there are no record limits on a stream socket the protocol here
124 * is to prepend the request with 4 bytes of its length and the reply
125 * is similarly encoded.
129 send_and_recv_tcp(int fd,
131 const krb5_data *req,
134 unsigned char len[4];
135 unsigned long rep_len;
138 _krb5_put_int(len, req->length, 4);
139 if(net_write(fd, len, sizeof(len)) < 0)
141 if(net_write(fd, req->data, req->length) < 0)
143 if (recv_loop (fd, tmout, 0, 4, &len_data) < 0)
145 if (len_data.length != 4) {
146 krb5_data_free (&len_data);
149 _krb5_get_int(len_data.data, &rep_len, 4);
150 krb5_data_free (&len_data);
151 if (recv_loop (fd, tmout, 0, rep_len, rep) < 0)
153 if(rep->length != rep_len) {
154 krb5_data_free (rep);
161 * `send_and_recv' tailored for the HTTP protocol.
165 send_and_recv_http(int fd,
168 const krb5_data *req,
174 int len = base64_encode(req->data, req->length, &str);
178 asprintf(&request, "GET %s%s HTTP/1.0\r\n\r\n", prefix, str);
182 ret = net_write (fd, request, strlen(request));
186 ret = recv_loop(fd, tmout, 0, 0, rep);
190 unsigned long rep_len;
193 s = realloc(rep->data, rep->length + 1);
195 krb5_data_free (rep);
199 p = strstr(s, "\r\n\r\n");
206 rep->length -= p - s;
207 if(rep->length < 4) { /* remove length */
212 _krb5_get_int(p, &rep_len, 4);
213 if (rep_len != rep->length) {
217 memmove(rep->data, p + 4, rep->length);
223 init_port(const char *s, int fallback)
228 sscanf (s, "%d", &tmp);
235 * Return 0 if succesful, otherwise 1
239 send_via_proxy (krb5_context context,
240 const krb5_krbhst_info *hi,
241 const krb5_data *send_data,
244 char *proxy2 = strdup(context->http_proxy);
245 char *proxy = proxy2;
248 struct addrinfo hints;
249 struct addrinfo *ai, *a;
252 char portstr[NI_MAXSERV];
256 if (strncmp (proxy, "http://", 7) == 0)
259 colon = strchr(proxy, ':');
262 memset (&hints, 0, sizeof(hints));
263 hints.ai_family = PF_UNSPEC;
264 hints.ai_socktype = SOCK_STREAM;
265 snprintf (portstr, sizeof(portstr), "%d",
266 ntohs(init_port (colon, htons(80))));
267 ret = getaddrinfo (proxy, portstr, &hints, &ai);
270 return krb5_eai_to_heim_errno(ret, errno);
272 for (a = ai; a != NULL; a = a->ai_next) {
273 s = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
276 if (connect (s, a->ai_addr, a->ai_addrlen) < 0) {
288 asprintf(&prefix, "http://%s/", hi->hostname);
293 ret = send_and_recv_http(s, context->kdc_timeout,
294 prefix, send_data, receive);
297 if(ret == 0 && receive->length != 0)
303 * Send the data `send' to one host from `handle` and get back the reply
308 krb5_sendto (krb5_context context,
309 const krb5_data *send_data,
310 krb5_krbhst_handle handle,
313 krb5_error_code ret = 0;
317 for (i = 0; i < context->max_retries; ++i) {
318 krb5_krbhst_info *hi;
320 while (krb5_krbhst_next(context, handle, &hi) == 0) {
322 struct addrinfo *ai, *a;
324 if(hi->proto == KRB5_KRBHST_HTTP && context->http_proxy) {
325 if (send_via_proxy (context, hi, send_data, receive))
331 ret = krb5_krbhst_get_addrinfo(context, hi, &ai);
335 for (a = ai; a != NULL; a = a->ai_next) {
336 fd = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
339 if (connect (fd, a->ai_addr, a->ai_addrlen) < 0) {
344 case KRB5_KRBHST_HTTP :
345 ret = send_and_recv_http(fd, context->kdc_timeout,
346 "", send_data, receive);
348 case KRB5_KRBHST_TCP :
349 ret = send_and_recv_tcp (fd, context->kdc_timeout,
352 case KRB5_KRBHST_UDP :
353 ret = send_and_recv_udp (fd, context->kdc_timeout,
358 if(ret == 0 && receive->length != 0)
362 krb5_krbhst_reset(context, handle);
364 krb5_clear_error_string (context);
365 ret = KRB5_KDC_UNREACH;
371 krb5_sendto_kdc2(krb5_context context,
372 const krb5_data *send_data,
373 const krb5_realm *realm,
378 krb5_krbhst_handle handle;
381 if (master || context->use_admin_kdc)
382 type = KRB5_KRBHST_ADMIN;
384 type = KRB5_KRBHST_KDC;
386 ret = krb5_krbhst_init(context, *realm, type, &handle);
390 ret = krb5_sendto(context, send_data, handle, receive);
391 krb5_krbhst_free(context, handle);
392 if (ret == KRB5_KDC_UNREACH)
393 krb5_set_error_string(context,
394 "unable to reach any KDC in realm %s", *realm);
399 krb5_sendto_kdc(krb5_context context,
400 const krb5_data *send_data,
401 const krb5_realm *realm,
404 return krb5_sendto_kdc2(context, send_data, realm, receive, FALSE);