2 * Copyright (c) 1997-2004 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
36 RCSID("$Id: connect.c,v 1.90.2.2 2004/04/02 20:50:53 lha Exp $");
39 * a tuple describing on what to listen
48 /* the current ones */
50 static struct port_desc
*ports
;
54 * add `family, port, protocol' to the list with duplicate suppresion.
58 add_port(int family
, int port
, const char *protocol
)
63 if(strcmp(protocol
, "udp") == 0)
65 else if(strcmp(protocol
, "tcp") == 0)
69 for(i
= 0; i
< num_ports
; i
++){
70 if(ports
[i
].type
== type
71 && ports
[i
].port
== port
72 && ports
[i
].family
== family
)
75 ports
= realloc(ports
, (num_ports
+ 1) * sizeof(*ports
));
77 krb5_err (context
, 1, errno
, "realloc");
78 ports
[num_ports
].family
= family
;
79 ports
[num_ports
].type
= type
;
80 ports
[num_ports
].port
= port
;
85 * add a triple but with service -> port lookup
86 * (this prints warnings for stuff that does not exist)
90 add_port_service(int family
, const char *service
, int port
,
93 port
= krb5_getportbyname (context
, service
, protocol
, port
);
94 add_port (family
, port
, protocol
);
98 * add the port with service -> port lookup or string -> number
99 * (no warning is printed)
103 add_port_string (int family
, const char *port_str
, const char *protocol
)
108 sp
= roken_getservbyname (port_str
, protocol
);
114 port
= htons(strtol(port_str
, &end
, 0));
118 add_port (family
, port
, protocol
);
122 * add the standard collection of ports for `family'
126 add_standard_ports (int family
)
128 add_port_service(family
, "kerberos", 88, "udp");
129 add_port_service(family
, "kerberos", 88, "tcp");
130 add_port_service(family
, "kerberos-sec", 88, "udp");
131 add_port_service(family
, "kerberos-sec", 88, "tcp");
133 add_port_service(family
, "http", 80, "tcp");
135 add_port_service(family
, "krb524", 4444, "udp");
136 add_port_service(family
, "krb524", 4444, "tcp");
140 add_port_service(family
, "kerberos-iv", 750, "udp");
141 add_port_service(family
, "kerberos-iv", 750, "tcp");
144 add_port_service(family
, "afs3-kaserver", 7004, "udp");
149 * parse the set of space-delimited ports in `str' and add them.
150 * "+" => all the standard ones
151 * otherwise it's port|service[/protocol]
155 parse_ports(const char *str
)
159 char *str_copy
= strdup (str
);
161 p
= strtok_r(str_copy
, " \t", &pos
);
163 if(strcmp(p
, "+") == 0) {
165 add_standard_ports(AF_INET6
);
167 add_standard_ports(AF_INET
);
169 char *q
= strchr(p
, '/');
173 add_port_string(AF_INET6
, p
, q
);
175 add_port_string(AF_INET
, p
, q
);
178 add_port_string(AF_INET6
, p
, "udp");
179 add_port_string(AF_INET6
, p
, "tcp");
181 add_port_string(AF_INET
, p
, "udp");
182 add_port_string(AF_INET
, p
, "tcp");
186 p
= strtok_r(NULL
, " \t", &pos
);
192 * every socket we listen on
202 struct sockaddr_storage __ss
;
205 char addr_string
[128];
209 init_descr(struct descr
*d
)
211 memset(d
, 0, sizeof(*d
));
212 d
->sa
= (struct sockaddr
*)&d
->__ss
;
217 * re-initialize all `n' ->sa in `d'.
221 reinit_descrs (struct descr
*d
, int n
)
225 for (i
= 0; i
< n
; ++i
)
226 d
[i
].sa
= (struct sockaddr
*)&d
[i
].__ss
;
230 * Create the socket (family, type, port) in `d'
234 init_socket(struct descr
*d
, krb5_address
*a
, int family
, int type
, int port
)
237 struct sockaddr_storage __ss
;
238 struct sockaddr
*sa
= (struct sockaddr
*)&__ss
;
239 int sa_size
= sizeof(__ss
);
243 ret
= krb5_addr2sockaddr (context
, a
, sa
, &sa_size
, port
);
245 krb5_warn(context
, ret
, "krb5_addr2sockaddr");
251 if (sa
->sa_family
!= family
)
254 d
->s
= socket(family
, type
, 0);
256 krb5_warn(context
, errno
, "socket(%d, %d, 0)", family
, type
);
260 #if defined(HAVE_SETSOCKOPT) && defined(SOL_SOCKET) && defined(SO_REUSEADDR)
263 setsockopt(d
->s
, SOL_SOCKET
, SO_REUSEADDR
, (void *)&one
, sizeof(one
));
268 if(bind(d
->s
, sa
, sa_size
) < 0){
272 krb5_print_address (a
, a_str
, sizeof(a_str
), &len
);
273 krb5_warn(context
, errno
, "bind %s/%d", a_str
, ntohs(port
));
278 if(type
== SOCK_STREAM
&& listen(d
->s
, SOMAXCONN
) < 0){
282 krb5_print_address (a
, a_str
, sizeof(a_str
), &len
);
283 krb5_warn(context
, errno
, "listen %s/%d", a_str
, ntohs(port
));
291 * Allocate descriptors for all the sockets that we should listen on
292 * and return the number of them.
296 init_sockets(struct descr
**desc
)
302 krb5_addresses addresses
;
304 if (explicit_addresses
.len
) {
305 addresses
= explicit_addresses
;
307 ret
= krb5_get_all_server_addrs (context
, &addresses
);
309 krb5_err (context
, 1, ret
, "krb5_get_all_server_addrs");
311 parse_ports(port_str
);
312 d
= malloc(addresses
.len
* num_ports
* sizeof(*d
));
314 krb5_errx(context
, 1, "malloc(%lu) failed",
315 (unsigned long)num_ports
* sizeof(*d
));
317 for (i
= 0; i
< num_ports
; i
++){
318 for (j
= 0; j
< addresses
.len
; ++j
) {
319 init_socket(&d
[num
], &addresses
.val
[j
],
320 ports
[i
].family
, ports
[i
].type
, ports
[i
].port
);
325 krb5_print_address (&addresses
.val
[j
], a_str
,
326 sizeof(a_str
), &len
);
328 kdc_log(5, "listening on %s port %u/%s",
330 ntohs(ports
[i
].port
),
331 (ports
[i
].type
== SOCK_STREAM
) ? "tcp" : "udp");
337 krb5_free_addresses (context
, &addresses
);
338 d
= realloc(d
, num
* sizeof(*d
));
339 if (d
== NULL
&& num
!= 0)
340 krb5_errx(context
, 1, "realloc(%lu) failed",
341 (unsigned long)num
* sizeof(*d
));
342 reinit_descrs (d
, num
);
348 * handle the request in `buf, len', from `addr' (or `from' as a string),
349 * sending a reply in `reply'.
353 process_request(unsigned char *buf
,
358 struct sockaddr
*addr
)
365 gettimeofday(&now
, NULL
);
366 if(decode_AS_REQ(buf
, len
, &req
, &i
) == 0){
367 ret
= as_rep(&req
, reply
, from
, addr
);
370 }else if(decode_TGS_REQ(buf
, len
, &req
, &i
) == 0){
371 ret
= tgs_rep(&req
, reply
, from
, addr
);
374 }else if(decode_Ticket(buf
, len
, &ticket
, &i
) == 0){
375 ret
= do_524(&ticket
, reply
, from
, addr
);
376 free_Ticket(&ticket
);
379 } else if(maybe_version4(buf
, len
)){
380 *sendlength
= 0; /* elbitapmoc sdrawkcab XXX */
381 do_version4(buf
, len
, reply
, from
, (struct sockaddr_in
*)addr
);
383 } else if (enable_kaserver
) {
384 ret
= do_kaserver (buf
, len
, reply
, from
, (struct sockaddr_in
*)addr
);
393 addr_to_string(struct sockaddr
*addr
, size_t addr_len
, char *str
, size_t len
)
396 if(krb5_sockaddr2address(context
, addr
, &a
) == 0) {
397 if(krb5_print_address(&a
, str
, len
, &len
) == 0) {
398 krb5_free_address(context
, &a
);
401 krb5_free_address(context
, &a
);
403 snprintf(str
, len
, "<family=%d>", addr
->sa_family
);
407 * Handle the request in `buf, len' to socket `d'
411 do_request(void *buf
, size_t len
, int sendlength
,
418 ret
= process_request(buf
, len
, &reply
, &sendlength
,
419 d
->addr_string
, d
->sa
);
421 kdc_log(5, "sending %lu bytes to %s", (unsigned long)reply
.length
,
424 unsigned char len
[4];
425 len
[0] = (reply
.length
>> 24) & 0xff;
426 len
[1] = (reply
.length
>> 16) & 0xff;
427 len
[2] = (reply
.length
>> 8) & 0xff;
428 len
[3] = reply
.length
& 0xff;
429 if(sendto(d
->s
, len
, sizeof(len
), 0, d
->sa
, d
->sock_len
) < 0) {
430 kdc_log (0, "sendto(%s): %s", d
->addr_string
, strerror(errno
));
431 krb5_data_free(&reply
);
435 if(sendto(d
->s
, reply
.data
, reply
.length
, 0, d
->sa
, d
->sock_len
) < 0) {
436 kdc_log (0, "sendto(%s): %s", d
->addr_string
, strerror(errno
));
437 krb5_data_free(&reply
);
440 krb5_data_free(&reply
);
443 kdc_log(0, "Failed processing %lu byte request from %s",
444 (unsigned long)len
, d
->addr_string
);
448 * Handle incoming data to the UDP socket in `d'
452 handle_udp(struct descr
*d
)
457 buf
= malloc(max_request
);
459 kdc_log(0, "Failed to allocate %lu bytes", (unsigned long)max_request
);
463 d
->sock_len
= sizeof(d
->__ss
);
464 n
= recvfrom(d
->s
, buf
, max_request
, 0, d
->sa
, &d
->sock_len
);
466 krb5_warn(context
, errno
, "recvfrom");
468 addr_to_string (d
->sa
, d
->sock_len
,
469 d
->addr_string
, sizeof(d
->addr_string
));
470 do_request(buf
, n
, 0, d
);
476 clear_descr(struct descr
*d
)
479 memset(d
->buf
, 0, d
->size
);
487 /* remove HTTP %-quoting from buf */
492 for(p
= q
= buf
; *p
; p
++, q
++) {
493 if(*p
== '%' && isxdigit(p
[1]) && isxdigit(p
[2])) {
495 if(sscanf(p
+ 1, "%2x", &x
) != 1)
506 #define TCP_TIMEOUT 4
509 * accept a new TCP connection on `d[parent]' and store it in `d[child]'
513 add_new_tcp (struct descr
*d
, int parent
, int child
)
520 d
[child
].sock_len
= sizeof(d
[child
].__ss
);
521 s
= accept(d
[parent
].s
, d
[child
].sa
, &d
[child
].sock_len
);
523 krb5_warn(context
, errno
, "accept");
527 if (s
>= FD_SETSIZE
) {
528 krb5_warnx(context
, "socket FD too large");
534 d
[child
].timeout
= time(NULL
) + TCP_TIMEOUT
;
535 d
[child
].type
= SOCK_STREAM
;
536 addr_to_string (d
[child
].sa
, d
[child
].sock_len
,
537 d
[child
].addr_string
, sizeof(d
[child
].addr_string
));
541 * Grow `d' to handle at least `n'.
542 * Return != 0 if fails
546 grow_descr (struct descr
*d
, size_t n
)
548 if (d
->size
- d
->len
< n
) {
552 grow
= max(1024, d
->len
+ n
);
553 if (d
->size
+ grow
> max_request
) {
554 kdc_log(0, "Request exceeds max request size (%lu bytes).",
555 (unsigned long)d
->size
+ grow
);
559 tmp
= realloc (d
->buf
, d
->size
+ grow
);
561 kdc_log(0, "Failed to re-allocate %lu bytes.",
562 (unsigned long)d
->size
+ grow
);
573 * Try to handle the TCP data at `d->buf, d->len'.
574 * Return -1 if failed, 0 if succesful, and 1 if data is complete.
578 handle_vanilla_tcp (struct descr
*d
)
583 sp
= krb5_storage_from_mem(d
->buf
, d
->len
);
585 kdc_log (0, "krb5_storage_from_mem failed");
588 krb5_ret_int32(sp
, &len
);
589 krb5_storage_free(sp
);
590 if(d
->len
- 4 >= len
) {
591 memmove(d
->buf
, d
->buf
+ 4, d
->len
- 4);
598 * Try to handle the TCP/HTTP data at `d->buf, d->len'.
599 * Return -1 if failed, 0 if succesful, and 1 if data is complete.
603 handle_http_tcp (struct descr
*d
)
612 p
= strstr(s
, "\r\n");
614 kdc_log(0, "Malformed HTTP request from %s", d
->addr_string
);
620 t
= strtok_r(s
, " \t", &p
);
622 kdc_log(0, "Malformed HTTP request from %s", d
->addr_string
);
625 t
= strtok_r(NULL
, " \t", &p
);
627 kdc_log(0, "Malformed HTTP request from %s", d
->addr_string
);
630 data
= malloc(strlen(t
));
632 kdc_log(0, "Failed to allocate %lu bytes",
633 (unsigned long)strlen(t
));
638 if(de_http(t
) != 0) {
639 kdc_log(0, "Malformed HTTP request from %s", d
->addr_string
);
640 kdc_log(5, "Request: %s", t
);
644 proto
= strtok_r(NULL
, " \t", &p
);
646 kdc_log(0, "Malformed HTTP request from %s", d
->addr_string
);
650 len
= base64_decode(t
, data
);
654 "Server: Heimdal/" VERSION
"\r\n"
655 "Cache-Control: no-cache\r\n"
656 "Pragma: no-cache\r\n"
657 "Content-type: text/html\r\n"
658 "Content-transfer-encoding: 8bit\r\n\r\n"
659 "<TITLE>404 Not found</TITLE>\r\n"
660 "<H1>404 Not found</H1>\r\n"
661 "That page doesn't exist, maybe you are looking for "
662 "<A HREF=\"http://www.pdc.kth.se/heimdal/\">Heimdal</A>?\r\n";
663 write(d
->s
, proto
, strlen(proto
));
664 write(d
->s
, msg
, strlen(msg
));
665 kdc_log(0, "HTTP request from %s is non KDC request", d
->addr_string
);
666 kdc_log(5, "Request: %s", t
);
673 "Server: Heimdal/" VERSION
"\r\n"
674 "Cache-Control: no-cache\r\n"
675 "Pragma: no-cache\r\n"
676 "Content-type: application/octet-stream\r\n"
677 "Content-transfer-encoding: binary\r\n\r\n";
678 write(d
->s
, proto
, strlen(proto
));
679 write(d
->s
, msg
, strlen(msg
));
681 memcpy(d
->buf
, data
, len
);
688 * Handle incoming data to the TCP socket in `d[index]'
692 handle_tcp(struct descr
*d
, int index
, int min_free
)
694 unsigned char buf
[1024];
698 if (d
[index
].timeout
== 0) {
699 add_new_tcp (d
, index
, min_free
);
703 n
= recvfrom(d
[index
].s
, buf
, sizeof(buf
), 0, NULL
, NULL
);
705 krb5_warn(context
, errno
, "recvfrom");
708 krb5_warnx(context
, "connection closed before end of data after %lu "
710 (unsigned long)d
[index
].len
, d
[index
].addr_string
);
711 clear_descr (d
+ index
);
714 if (grow_descr (&d
[index
], n
))
716 memcpy(d
[index
].buf
+ d
[index
].len
, buf
, n
);
718 if(d
[index
].len
> 4 && d
[index
].buf
[0] == 0) {
719 ret
= handle_vanilla_tcp (&d
[index
]);
720 } else if(enable_http
&&
722 strncmp((char *)d
[index
].buf
, "GET ", 4) == 0 &&
723 strncmp((char *)d
[index
].buf
+ d
[index
].len
- 4,
724 "\r\n\r\n", 4) == 0) {
725 ret
= handle_http_tcp (&d
[index
]);
727 clear_descr (d
+ index
);
728 } else if (d
[index
].len
> 4) {
729 kdc_log (0, "TCP data of strange type from %s", d
[index
].addr_string
);
735 do_request(d
[index
].buf
, d
[index
].len
, 1, &d
[index
]);
736 clear_descr(d
+ index
);
746 ndescr
= init_sockets(&d
);
748 krb5_errx(context
, 1, "No sockets!");
749 while(exit_flag
== 0){
750 struct timeval tmout
;
757 for(i
= 0; i
< ndescr
; i
++) {
759 if(d
[i
].type
== SOCK_STREAM
&&
760 d
[i
].timeout
&& d
[i
].timeout
< time(NULL
)) {
761 kdc_log(1, "TCP-connection from %s expired after %lu bytes",
762 d
[i
].addr_string
, (unsigned long)d
[i
].len
);
768 if (max_fd
>= FD_SETSIZE
)
769 krb5_errx(context
, 1, "fd too large");
770 FD_SET(d
[i
].s
, &fds
);
771 } else if(min_free
< 0 || i
< min_free
)
776 tmp
= realloc(d
, (ndescr
+ 4) * sizeof(*d
));
778 krb5_warnx(context
, "No memory");
781 reinit_descrs (d
, ndescr
);
782 memset(d
+ ndescr
, 0, 4 * sizeof(*d
));
783 for(i
= ndescr
; i
< ndescr
+ 4; i
++)
790 tmout
.tv_sec
= TCP_TIMEOUT
;
792 switch(select(max_fd
+ 1, &fds
, 0, 0, &tmout
)){
797 krb5_warn(context
, errno
, "select");
800 for(i
= 0; i
< ndescr
; i
++)
801 if(d
[i
].s
>= 0 && FD_ISSET(d
[i
].s
, &fds
)) {
802 if(d
[i
].type
== SOCK_DGRAM
)
804 else if(d
[i
].type
== SOCK_STREAM
)
805 handle_tcp(d
, i
, min_free
);