2000-01-09 20:58:00 +00:00
|
|
|
|
/*
|
2008-05-07 13:39:42 +00:00
|
|
|
|
* Copyright (c) 1997-2005 Kungliga Tekniska H<EFBFBD>gskolan
|
2000-01-09 20:58:00 +00:00
|
|
|
|
* (Royal Institute of Technology, Stockholm, Sweden).
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
|
* are met:
|
|
|
|
|
*
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
|
*
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
|
* without specific prior written permission.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "kdc_locl.h"
|
|
|
|
|
|
2008-05-07 13:39:42 +00:00
|
|
|
|
RCSID("$Id: connect.c 22434 2008-01-14 09:21:37Z lha $");
|
|
|
|
|
|
|
|
|
|
/* Should we enable the HTTP hack? */
|
|
|
|
|
int enable_http = -1;
|
|
|
|
|
|
|
|
|
|
/* Log over requests to the KDC */
|
|
|
|
|
const char *request_log;
|
|
|
|
|
|
|
|
|
|
/* A string describing on what ports to listen */
|
|
|
|
|
const char *port_str;
|
|
|
|
|
|
|
|
|
|
krb5_addresses explicit_addresses;
|
|
|
|
|
|
|
|
|
|
size_t max_request; /* maximal size of a request */
|
2000-02-24 11:19:29 +00:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* a tuple describing on what to listen
|
|
|
|
|
*/
|
2000-01-09 20:58:00 +00:00
|
|
|
|
|
|
|
|
|
struct port_desc{
|
|
|
|
|
int family;
|
|
|
|
|
int type;
|
|
|
|
|
int port;
|
|
|
|
|
};
|
|
|
|
|
|
2000-02-24 11:19:29 +00:00
|
|
|
|
/* the current ones */
|
|
|
|
|
|
2000-01-09 20:58:00 +00:00
|
|
|
|
static struct port_desc *ports;
|
|
|
|
|
static int num_ports;
|
|
|
|
|
|
2000-02-24 11:19:29 +00:00
|
|
|
|
/*
|
|
|
|
|
* add `family, port, protocol' to the list with duplicate suppresion.
|
|
|
|
|
*/
|
|
|
|
|
|
2000-01-09 20:58:00 +00:00
|
|
|
|
static void
|
2008-05-07 13:39:42 +00:00
|
|
|
|
add_port(krb5_context context,
|
|
|
|
|
int family, int port, const char *protocol)
|
2000-01-09 20:58:00 +00:00
|
|
|
|
{
|
|
|
|
|
int type;
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
if(strcmp(protocol, "udp") == 0)
|
|
|
|
|
type = SOCK_DGRAM;
|
|
|
|
|
else if(strcmp(protocol, "tcp") == 0)
|
|
|
|
|
type = SOCK_STREAM;
|
|
|
|
|
else
|
|
|
|
|
return;
|
|
|
|
|
for(i = 0; i < num_ports; i++){
|
|
|
|
|
if(ports[i].type == type
|
|
|
|
|
&& ports[i].port == port
|
|
|
|
|
&& ports[i].family == family)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
ports = realloc(ports, (num_ports + 1) * sizeof(*ports));
|
2000-02-24 11:19:29 +00:00
|
|
|
|
if (ports == NULL)
|
|
|
|
|
krb5_err (context, 1, errno, "realloc");
|
2000-01-09 20:58:00 +00:00
|
|
|
|
ports[num_ports].family = family;
|
|
|
|
|
ports[num_ports].type = type;
|
|
|
|
|
ports[num_ports].port = port;
|
|
|
|
|
num_ports++;
|
|
|
|
|
}
|
|
|
|
|
|
2000-02-24 11:19:29 +00:00
|
|
|
|
/*
|
|
|
|
|
* add a triple but with service -> port lookup
|
|
|
|
|
* (this prints warnings for stuff that does not exist)
|
|
|
|
|
*/
|
|
|
|
|
|
2000-01-09 20:58:00 +00:00
|
|
|
|
static void
|
2008-05-07 13:39:42 +00:00
|
|
|
|
add_port_service(krb5_context context,
|
|
|
|
|
int family, const char *service, int port,
|
2000-01-09 20:58:00 +00:00
|
|
|
|
const char *protocol)
|
|
|
|
|
{
|
|
|
|
|
port = krb5_getportbyname (context, service, protocol, port);
|
2008-05-07 13:39:42 +00:00
|
|
|
|
add_port (context, family, port, protocol);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-02-24 11:19:29 +00:00
|
|
|
|
/*
|
|
|
|
|
* add the port with service -> port lookup or string -> number
|
|
|
|
|
* (no warning is printed)
|
|
|
|
|
*/
|
|
|
|
|
|
2000-01-09 20:58:00 +00:00
|
|
|
|
static void
|
2008-05-07 13:39:42 +00:00
|
|
|
|
add_port_string (krb5_context context,
|
|
|
|
|
int family, const char *str, const char *protocol)
|
2000-01-09 20:58:00 +00:00
|
|
|
|
{
|
|
|
|
|
struct servent *sp;
|
|
|
|
|
int port;
|
|
|
|
|
|
2008-05-07 13:39:42 +00:00
|
|
|
|
sp = roken_getservbyname (str, protocol);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
if (sp != NULL) {
|
|
|
|
|
port = sp->s_port;
|
|
|
|
|
} else {
|
|
|
|
|
char *end;
|
|
|
|
|
|
2008-05-07 13:39:42 +00:00
|
|
|
|
port = htons(strtol(str, &end, 0));
|
|
|
|
|
if (end == str)
|
2000-01-09 20:58:00 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2008-05-07 13:39:42 +00:00
|
|
|
|
add_port (context, family, port, protocol);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-02-24 11:19:29 +00:00
|
|
|
|
/*
|
|
|
|
|
* add the standard collection of ports for `family'
|
|
|
|
|
*/
|
|
|
|
|
|
2000-01-09 20:58:00 +00:00
|
|
|
|
static void
|
2008-05-07 13:39:42 +00:00
|
|
|
|
add_standard_ports (krb5_context context,
|
|
|
|
|
krb5_kdc_configuration *config,
|
|
|
|
|
int family)
|
2000-01-09 20:58:00 +00:00
|
|
|
|
{
|
2008-05-07 13:39:42 +00:00
|
|
|
|
add_port_service(context, family, "kerberos", 88, "udp");
|
|
|
|
|
add_port_service(context, family, "kerberos", 88, "tcp");
|
|
|
|
|
add_port_service(context, family, "kerberos-sec", 88, "udp");
|
|
|
|
|
add_port_service(context, family, "kerberos-sec", 88, "tcp");
|
2000-01-09 20:58:00 +00:00
|
|
|
|
if(enable_http)
|
2008-05-07 13:39:42 +00:00
|
|
|
|
add_port_service(context, family, "http", 80, "tcp");
|
|
|
|
|
if(config->enable_524) {
|
|
|
|
|
add_port_service(context, family, "krb524", 4444, "udp");
|
|
|
|
|
add_port_service(context, family, "krb524", 4444, "tcp");
|
|
|
|
|
}
|
|
|
|
|
if(config->enable_v4) {
|
|
|
|
|
add_port_service(context, family, "kerberos-iv", 750, "udp");
|
|
|
|
|
add_port_service(context, family, "kerberos-iv", 750, "tcp");
|
|
|
|
|
}
|
|
|
|
|
if (config->enable_kaserver)
|
|
|
|
|
add_port_service(context, family, "afs3-kaserver", 7004, "udp");
|
|
|
|
|
if(config->enable_kx509) {
|
|
|
|
|
add_port_service(context, family, "kca_service", 9878, "udp");
|
|
|
|
|
add_port_service(context, family, "kca_service", 9878, "tcp");
|
|
|
|
|
}
|
|
|
|
|
|
2000-01-09 20:58:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-02-24 11:19:29 +00:00
|
|
|
|
/*
|
|
|
|
|
* parse the set of space-delimited ports in `str' and add them.
|
|
|
|
|
* "+" => all the standard ones
|
|
|
|
|
* otherwise it's port|service[/protocol]
|
|
|
|
|
*/
|
|
|
|
|
|
2000-01-09 20:58:00 +00:00
|
|
|
|
static void
|
2008-05-07 13:39:42 +00:00
|
|
|
|
parse_ports(krb5_context context,
|
|
|
|
|
krb5_kdc_configuration *config,
|
|
|
|
|
const char *str)
|
2000-01-09 20:58:00 +00:00
|
|
|
|
{
|
|
|
|
|
char *pos = NULL;
|
|
|
|
|
char *p;
|
|
|
|
|
char *str_copy = strdup (str);
|
|
|
|
|
|
|
|
|
|
p = strtok_r(str_copy, " \t", &pos);
|
|
|
|
|
while(p != NULL) {
|
|
|
|
|
if(strcmp(p, "+") == 0) {
|
|
|
|
|
#ifdef HAVE_IPV6
|
2008-05-07 13:39:42 +00:00
|
|
|
|
add_standard_ports(context, config, AF_INET6);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
#endif
|
2008-05-07 13:39:42 +00:00
|
|
|
|
add_standard_ports(context, config, AF_INET);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
} else {
|
|
|
|
|
char *q = strchr(p, '/');
|
|
|
|
|
if(q){
|
|
|
|
|
*q++ = 0;
|
|
|
|
|
#ifdef HAVE_IPV6
|
2008-05-07 13:39:42 +00:00
|
|
|
|
add_port_string(context, AF_INET6, p, q);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
#endif
|
2008-05-07 13:39:42 +00:00
|
|
|
|
add_port_string(context, AF_INET, p, q);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
}else {
|
|
|
|
|
#ifdef HAVE_IPV6
|
2008-05-07 13:39:42 +00:00
|
|
|
|
add_port_string(context, AF_INET6, p, "udp");
|
|
|
|
|
add_port_string(context, AF_INET6, p, "tcp");
|
2000-01-09 20:58:00 +00:00
|
|
|
|
#endif
|
2008-05-07 13:39:42 +00:00
|
|
|
|
add_port_string(context, AF_INET, p, "udp");
|
|
|
|
|
add_port_string(context, AF_INET, p, "tcp");
|
2000-01-09 20:58:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p = strtok_r(NULL, " \t", &pos);
|
|
|
|
|
}
|
|
|
|
|
free (str_copy);
|
|
|
|
|
}
|
|
|
|
|
|
2000-02-24 11:19:29 +00:00
|
|
|
|
/*
|
|
|
|
|
* every socket we listen on
|
|
|
|
|
*/
|
|
|
|
|
|
2000-01-09 20:58:00 +00:00
|
|
|
|
struct descr {
|
|
|
|
|
int s;
|
|
|
|
|
int type;
|
2008-05-07 13:39:42 +00:00
|
|
|
|
int port;
|
2000-01-09 20:58:00 +00:00
|
|
|
|
unsigned char *buf;
|
|
|
|
|
size_t size;
|
|
|
|
|
size_t len;
|
|
|
|
|
time_t timeout;
|
2000-02-24 11:28:20 +00:00
|
|
|
|
struct sockaddr_storage __ss;
|
|
|
|
|
struct sockaddr *sa;
|
2001-02-13 16:46:19 +00:00
|
|
|
|
socklen_t sock_len;
|
2000-02-24 11:28:20 +00:00
|
|
|
|
char addr_string[128];
|
2000-01-09 20:58:00 +00:00
|
|
|
|
};
|
|
|
|
|
|
2001-02-13 16:46:19 +00:00
|
|
|
|
static void
|
|
|
|
|
init_descr(struct descr *d)
|
|
|
|
|
{
|
|
|
|
|
memset(d, 0, sizeof(*d));
|
|
|
|
|
d->sa = (struct sockaddr *)&d->__ss;
|
|
|
|
|
d->s = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2003-10-09 19:36:20 +00:00
|
|
|
|
* re-initialize all `n' ->sa in `d'.
|
2001-02-13 16:46:19 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
reinit_descrs (struct descr *d, int n)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < n; ++i)
|
|
|
|
|
d[i].sa = (struct sockaddr *)&d[i].__ss;
|
|
|
|
|
}
|
|
|
|
|
|
2000-01-09 20:58:00 +00:00
|
|
|
|
/*
|
|
|
|
|
* Create the socket (family, type, port) in `d'
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static void
|
2008-05-07 13:39:42 +00:00
|
|
|
|
init_socket(krb5_context context,
|
|
|
|
|
krb5_kdc_configuration *config,
|
|
|
|
|
struct descr *d, krb5_address *a, int family, int type, int port)
|
2000-01-09 20:58:00 +00:00
|
|
|
|
{
|
|
|
|
|
krb5_error_code ret;
|
|
|
|
|
struct sockaddr_storage __ss;
|
|
|
|
|
struct sockaddr *sa = (struct sockaddr *)&__ss;
|
2008-05-07 13:39:42 +00:00
|
|
|
|
krb5_socklen_t sa_size = sizeof(__ss);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
|
2001-02-13 16:46:19 +00:00
|
|
|
|
init_descr (d);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
|
2001-06-21 02:12:07 +00:00
|
|
|
|
ret = krb5_addr2sockaddr (context, a, sa, &sa_size, port);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
if (ret) {
|
2000-02-24 11:19:29 +00:00
|
|
|
|
krb5_warn(context, ret, "krb5_addr2sockaddr");
|
2000-01-09 20:58:00 +00:00
|
|
|
|
close(d->s);
|
|
|
|
|
d->s = -1;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sa->sa_family != family)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
d->s = socket(family, type, 0);
|
|
|
|
|
if(d->s < 0){
|
|
|
|
|
krb5_warn(context, errno, "socket(%d, %d, 0)", family, type);
|
|
|
|
|
d->s = -1;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
#if defined(HAVE_SETSOCKOPT) && defined(SOL_SOCKET) && defined(SO_REUSEADDR)
|
|
|
|
|
{
|
|
|
|
|
int one = 1;
|
|
|
|
|
setsockopt(d->s, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(one));
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
d->type = type;
|
2008-05-07 13:39:42 +00:00
|
|
|
|
d->port = port;
|
2000-01-09 20:58:00 +00:00
|
|
|
|
|
|
|
|
|
if(bind(d->s, sa, sa_size) < 0){
|
2000-02-24 11:19:29 +00:00
|
|
|
|
char a_str[256];
|
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
|
|
krb5_print_address (a, a_str, sizeof(a_str), &len);
|
|
|
|
|
krb5_warn(context, errno, "bind %s/%d", a_str, ntohs(port));
|
2000-01-09 20:58:00 +00:00
|
|
|
|
close(d->s);
|
|
|
|
|
d->s = -1;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if(type == SOCK_STREAM && listen(d->s, SOMAXCONN) < 0){
|
2000-02-24 11:19:29 +00:00
|
|
|
|
char a_str[256];
|
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
|
|
krb5_print_address (a, a_str, sizeof(a_str), &len);
|
|
|
|
|
krb5_warn(context, errno, "listen %s/%d", a_str, ntohs(port));
|
2000-01-09 20:58:00 +00:00
|
|
|
|
close(d->s);
|
2000-02-24 11:19:29 +00:00
|
|
|
|
d->s = -1;
|
2000-01-09 20:58:00 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Allocate descriptors for all the sockets that we should listen on
|
|
|
|
|
* and return the number of them.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static int
|
2008-05-07 13:39:42 +00:00
|
|
|
|
init_sockets(krb5_context context,
|
|
|
|
|
krb5_kdc_configuration *config,
|
|
|
|
|
struct descr **desc)
|
2000-01-09 20:58:00 +00:00
|
|
|
|
{
|
|
|
|
|
krb5_error_code ret;
|
|
|
|
|
int i, j;
|
|
|
|
|
struct descr *d;
|
|
|
|
|
int num = 0;
|
|
|
|
|
krb5_addresses addresses;
|
|
|
|
|
|
2000-02-24 11:19:29 +00:00
|
|
|
|
if (explicit_addresses.len) {
|
|
|
|
|
addresses = explicit_addresses;
|
|
|
|
|
} else {
|
|
|
|
|
ret = krb5_get_all_server_addrs (context, &addresses);
|
|
|
|
|
if (ret)
|
|
|
|
|
krb5_err (context, 1, ret, "krb5_get_all_server_addrs");
|
|
|
|
|
}
|
2008-05-07 13:39:42 +00:00
|
|
|
|
parse_ports(context, config, port_str);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
d = malloc(addresses.len * num_ports * sizeof(*d));
|
|
|
|
|
if (d == NULL)
|
2001-02-13 16:46:19 +00:00
|
|
|
|
krb5_errx(context, 1, "malloc(%lu) failed",
|
|
|
|
|
(unsigned long)num_ports * sizeof(*d));
|
2000-01-09 20:58:00 +00:00
|
|
|
|
|
|
|
|
|
for (i = 0; i < num_ports; i++){
|
|
|
|
|
for (j = 0; j < addresses.len; ++j) {
|
2008-05-07 13:39:42 +00:00
|
|
|
|
init_socket(context, config, &d[num], &addresses.val[j],
|
2000-01-09 20:58:00 +00:00
|
|
|
|
ports[i].family, ports[i].type, ports[i].port);
|
|
|
|
|
if(d[num].s != -1){
|
|
|
|
|
char a_str[80];
|
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
|
|
krb5_print_address (&addresses.val[j], a_str,
|
|
|
|
|
sizeof(a_str), &len);
|
|
|
|
|
|
2008-05-07 13:39:42 +00:00
|
|
|
|
kdc_log(context, config, 5, "listening on %s port %u/%s",
|
2000-01-09 20:58:00 +00:00
|
|
|
|
a_str,
|
|
|
|
|
ntohs(ports[i].port),
|
|
|
|
|
(ports[i].type == SOCK_STREAM) ? "tcp" : "udp");
|
|
|
|
|
/* XXX */
|
|
|
|
|
num++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
krb5_free_addresses (context, &addresses);
|
|
|
|
|
d = realloc(d, num * sizeof(*d));
|
|
|
|
|
if (d == NULL && num != 0)
|
2001-02-13 16:46:19 +00:00
|
|
|
|
krb5_errx(context, 1, "realloc(%lu) failed",
|
|
|
|
|
(unsigned long)num * sizeof(*d));
|
|
|
|
|
reinit_descrs (d, num);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
*desc = d;
|
|
|
|
|
return num;
|
|
|
|
|
}
|
|
|
|
|
|
2000-02-24 11:19:29 +00:00
|
|
|
|
/*
|
2008-05-07 13:39:42 +00:00
|
|
|
|
*
|
2000-02-24 11:19:29 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2008-05-07 13:39:42 +00:00
|
|
|
|
static const char *
|
|
|
|
|
descr_type(struct descr *d)
|
2000-01-09 20:58:00 +00:00
|
|
|
|
{
|
2008-05-07 13:39:42 +00:00
|
|
|
|
if (d->type == SOCK_DGRAM)
|
|
|
|
|
return "udp";
|
|
|
|
|
else if (d->type == SOCK_STREAM)
|
|
|
|
|
return "tcp";
|
|
|
|
|
return "unknown";
|
2000-01-09 20:58:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2008-05-07 13:39:42 +00:00
|
|
|
|
addr_to_string(krb5_context context,
|
|
|
|
|
struct sockaddr *addr, size_t addr_len, char *str, size_t len)
|
2000-01-09 20:58:00 +00:00
|
|
|
|
{
|
|
|
|
|
krb5_address a;
|
2002-08-30 21:23:27 +00:00
|
|
|
|
if(krb5_sockaddr2address(context, addr, &a) == 0) {
|
|
|
|
|
if(krb5_print_address(&a, str, len, &len) == 0) {
|
|
|
|
|
krb5_free_address(context, &a);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2000-01-09 20:58:00 +00:00
|
|
|
|
krb5_free_address(context, &a);
|
|
|
|
|
}
|
|
|
|
|
snprintf(str, len, "<family=%d>", addr->sa_family);
|
|
|
|
|
}
|
|
|
|
|
|
2008-05-07 13:39:42 +00:00
|
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
send_reply(krb5_context context,
|
|
|
|
|
krb5_kdc_configuration *config,
|
|
|
|
|
krb5_boolean prependlength,
|
|
|
|
|
struct descr *d,
|
|
|
|
|
krb5_data *reply)
|
|
|
|
|
{
|
|
|
|
|
kdc_log(context, config, 5,
|
|
|
|
|
"sending %lu bytes to %s", (unsigned long)reply->length,
|
|
|
|
|
d->addr_string);
|
|
|
|
|
if(prependlength){
|
|
|
|
|
unsigned char l[4];
|
|
|
|
|
l[0] = (reply->length >> 24) & 0xff;
|
|
|
|
|
l[1] = (reply->length >> 16) & 0xff;
|
|
|
|
|
l[2] = (reply->length >> 8) & 0xff;
|
|
|
|
|
l[3] = reply->length & 0xff;
|
|
|
|
|
if(sendto(d->s, l, sizeof(l), 0, d->sa, d->sock_len) < 0) {
|
|
|
|
|
kdc_log (context, config,
|
|
|
|
|
0, "sendto(%s): %s", d->addr_string, strerror(errno));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(sendto(d->s, reply->data, reply->length, 0, d->sa, d->sock_len) < 0) {
|
|
|
|
|
kdc_log (context, config,
|
|
|
|
|
0, "sendto(%s): %s", d->addr_string, strerror(errno));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2000-02-24 11:28:20 +00:00
|
|
|
|
/*
|
|
|
|
|
* Handle the request in `buf, len' to socket `d'
|
|
|
|
|
*/
|
|
|
|
|
|
2000-01-09 20:58:00 +00:00
|
|
|
|
static void
|
2008-05-07 13:39:42 +00:00
|
|
|
|
do_request(krb5_context context,
|
|
|
|
|
krb5_kdc_configuration *config,
|
|
|
|
|
void *buf, size_t len, krb5_boolean prependlength,
|
2000-02-24 11:28:20 +00:00
|
|
|
|
struct descr *d)
|
2000-01-09 20:58:00 +00:00
|
|
|
|
{
|
|
|
|
|
krb5_error_code ret;
|
|
|
|
|
krb5_data reply;
|
2008-05-07 13:39:42 +00:00
|
|
|
|
int datagram_reply = (d->type == SOCK_DGRAM);
|
|
|
|
|
|
|
|
|
|
krb5_kdc_update_time(NULL);
|
|
|
|
|
|
|
|
|
|
krb5_data_zero(&reply);
|
|
|
|
|
ret = krb5_kdc_process_request(context, config,
|
|
|
|
|
buf, len, &reply, &prependlength,
|
|
|
|
|
d->addr_string, d->sa,
|
|
|
|
|
datagram_reply);
|
|
|
|
|
if(request_log)
|
|
|
|
|
krb5_kdc_save_request(context, request_log, buf, len, &reply, d->sa);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
if(reply.length){
|
2008-05-07 13:39:42 +00:00
|
|
|
|
send_reply(context, config, prependlength, d, &reply);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
krb5_data_free(&reply);
|
|
|
|
|
}
|
|
|
|
|
if(ret)
|
2008-05-07 13:39:42 +00:00
|
|
|
|
kdc_log(context, config, 0,
|
|
|
|
|
"Failed processing %lu byte request from %s",
|
2000-02-24 11:28:20 +00:00
|
|
|
|
(unsigned long)len, d->addr_string);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-02-24 11:28:20 +00:00
|
|
|
|
/*
|
|
|
|
|
* Handle incoming data to the UDP socket in `d'
|
|
|
|
|
*/
|
|
|
|
|
|
2000-01-09 20:58:00 +00:00
|
|
|
|
static void
|
2008-05-07 13:39:42 +00:00
|
|
|
|
handle_udp(krb5_context context,
|
|
|
|
|
krb5_kdc_configuration *config,
|
|
|
|
|
struct descr *d)
|
2000-01-09 20:58:00 +00:00
|
|
|
|
{
|
|
|
|
|
unsigned char *buf;
|
|
|
|
|
int n;
|
|
|
|
|
|
|
|
|
|
buf = malloc(max_request);
|
|
|
|
|
if(buf == NULL){
|
2008-05-07 13:39:42 +00:00
|
|
|
|
kdc_log(context, config, 0, "Failed to allocate %lu bytes", (unsigned long)max_request);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2000-02-24 11:28:20 +00:00
|
|
|
|
d->sock_len = sizeof(d->__ss);
|
|
|
|
|
n = recvfrom(d->s, buf, max_request, 0, d->sa, &d->sock_len);
|
|
|
|
|
if(n < 0)
|
2000-01-09 20:58:00 +00:00
|
|
|
|
krb5_warn(context, errno, "recvfrom");
|
2000-02-24 11:28:20 +00:00
|
|
|
|
else {
|
2008-05-07 13:39:42 +00:00
|
|
|
|
addr_to_string (context, d->sa, d->sock_len,
|
2000-02-24 11:28:20 +00:00
|
|
|
|
d->addr_string, sizeof(d->addr_string));
|
2008-05-07 13:39:42 +00:00
|
|
|
|
do_request(context, config, buf, n, FALSE, d);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
}
|
|
|
|
|
free (buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
clear_descr(struct descr *d)
|
|
|
|
|
{
|
|
|
|
|
if(d->buf)
|
|
|
|
|
memset(d->buf, 0, d->size);
|
|
|
|
|
d->len = 0;
|
|
|
|
|
if(d->s != -1)
|
|
|
|
|
close(d->s);
|
|
|
|
|
d->s = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* remove HTTP %-quoting from buf */
|
|
|
|
|
static int
|
|
|
|
|
de_http(char *buf)
|
|
|
|
|
{
|
2008-05-07 13:39:42 +00:00
|
|
|
|
unsigned char *p, *q;
|
|
|
|
|
for(p = q = (unsigned char *)buf; *p; p++, q++) {
|
2002-11-24 20:59:25 +00:00
|
|
|
|
if(*p == '%' && isxdigit(p[1]) && isxdigit(p[2])) {
|
2000-01-09 20:58:00 +00:00
|
|
|
|
unsigned int x;
|
2008-05-07 13:39:42 +00:00
|
|
|
|
if(sscanf((char *)p + 1, "%2x", &x) != 1)
|
2000-01-09 20:58:00 +00:00
|
|
|
|
return -1;
|
|
|
|
|
*q = x;
|
|
|
|
|
p += 2;
|
|
|
|
|
} else
|
|
|
|
|
*q = *p;
|
|
|
|
|
}
|
|
|
|
|
*q = '\0';
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define TCP_TIMEOUT 4
|
|
|
|
|
|
|
|
|
|
/*
|
2001-02-13 16:46:19 +00:00
|
|
|
|
* accept a new TCP connection on `d[parent]' and store it in `d[child]'
|
2000-01-09 20:58:00 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static void
|
2008-05-07 13:39:42 +00:00
|
|
|
|
add_new_tcp (krb5_context context,
|
|
|
|
|
krb5_kdc_configuration *config,
|
|
|
|
|
struct descr *d, int parent, int child)
|
2000-01-09 20:58:00 +00:00
|
|
|
|
{
|
|
|
|
|
int s;
|
|
|
|
|
|
2001-02-13 16:46:19 +00:00
|
|
|
|
if (child == -1)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
d[child].sock_len = sizeof(d[child].__ss);
|
|
|
|
|
s = accept(d[parent].s, d[child].sa, &d[child].sock_len);
|
2000-02-24 11:28:20 +00:00
|
|
|
|
if(s < 0) {
|
2000-01-09 20:58:00 +00:00
|
|
|
|
krb5_warn(context, errno, "accept");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2001-02-13 16:46:19 +00:00
|
|
|
|
|
|
|
|
|
if (s >= FD_SETSIZE) {
|
|
|
|
|
krb5_warnx(context, "socket FD too large");
|
|
|
|
|
close (s);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2001-02-13 16:46:19 +00:00
|
|
|
|
|
|
|
|
|
d[child].s = s;
|
|
|
|
|
d[child].timeout = time(NULL) + TCP_TIMEOUT;
|
|
|
|
|
d[child].type = SOCK_STREAM;
|
2008-05-07 13:39:42 +00:00
|
|
|
|
addr_to_string (context,
|
|
|
|
|
d[child].sa, d[child].sock_len,
|
2001-02-13 16:46:19 +00:00
|
|
|
|
d[child].addr_string, sizeof(d[child].addr_string));
|
2000-01-09 20:58:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Grow `d' to handle at least `n'.
|
|
|
|
|
* Return != 0 if fails
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static int
|
2008-05-07 13:39:42 +00:00
|
|
|
|
grow_descr (krb5_context context,
|
|
|
|
|
krb5_kdc_configuration *config,
|
|
|
|
|
struct descr *d, size_t n)
|
2000-01-09 20:58:00 +00:00
|
|
|
|
{
|
|
|
|
|
if (d->size - d->len < n) {
|
|
|
|
|
unsigned char *tmp;
|
2004-04-03 21:22:55 +00:00
|
|
|
|
size_t grow;
|
2000-01-09 20:58:00 +00:00
|
|
|
|
|
2004-04-03 21:22:55 +00:00
|
|
|
|
grow = max(1024, d->len + n);
|
|
|
|
|
if (d->size + grow > max_request) {
|
2008-05-07 13:39:42 +00:00
|
|
|
|
kdc_log(context, config, 0, "Request exceeds max request size (%lu bytes).",
|
2004-04-03 21:22:55 +00:00
|
|
|
|
(unsigned long)d->size + grow);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
clear_descr(d);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2004-04-03 21:22:55 +00:00
|
|
|
|
tmp = realloc (d->buf, d->size + grow);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
if (tmp == NULL) {
|
2008-05-07 13:39:42 +00:00
|
|
|
|
kdc_log(context, config, 0, "Failed to re-allocate %lu bytes.",
|
2004-04-03 21:22:55 +00:00
|
|
|
|
(unsigned long)d->size + grow);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
clear_descr(d);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2004-04-03 21:22:55 +00:00
|
|
|
|
d->size += grow;
|
2000-01-09 20:58:00 +00:00
|
|
|
|
d->buf = tmp;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Try to handle the TCP data at `d->buf, d->len'.
|
|
|
|
|
* Return -1 if failed, 0 if succesful, and 1 if data is complete.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static int
|
2008-05-07 13:39:42 +00:00
|
|
|
|
handle_vanilla_tcp (krb5_context context,
|
|
|
|
|
krb5_kdc_configuration *config,
|
|
|
|
|
struct descr *d)
|
2000-01-09 20:58:00 +00:00
|
|
|
|
{
|
|
|
|
|
krb5_storage *sp;
|
2008-05-07 13:39:42 +00:00
|
|
|
|
uint32_t len;
|
2000-01-09 20:58:00 +00:00
|
|
|
|
|
|
|
|
|
sp = krb5_storage_from_mem(d->buf, d->len);
|
|
|
|
|
if (sp == NULL) {
|
2008-05-07 13:39:42 +00:00
|
|
|
|
kdc_log (context, config, 0, "krb5_storage_from_mem failed");
|
2000-01-09 20:58:00 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
2008-05-07 13:39:42 +00:00
|
|
|
|
krb5_ret_uint32(sp, &len);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
krb5_storage_free(sp);
|
|
|
|
|
if(d->len - 4 >= len) {
|
2001-02-13 16:46:19 +00:00
|
|
|
|
memmove(d->buf, d->buf + 4, d->len - 4);
|
2008-05-07 13:39:42 +00:00
|
|
|
|
d->len -= 4;
|
2000-01-09 20:58:00 +00:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Try to handle the TCP/HTTP data at `d->buf, d->len'.
|
|
|
|
|
* Return -1 if failed, 0 if succesful, and 1 if data is complete.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static int
|
2008-05-07 13:39:42 +00:00
|
|
|
|
handle_http_tcp (krb5_context context,
|
|
|
|
|
krb5_kdc_configuration *config,
|
|
|
|
|
struct descr *d)
|
2000-01-09 20:58:00 +00:00
|
|
|
|
{
|
|
|
|
|
char *s, *p, *t;
|
|
|
|
|
void *data;
|
|
|
|
|
char *proto;
|
|
|
|
|
int len;
|
|
|
|
|
|
|
|
|
|
s = (char *)d->buf;
|
|
|
|
|
|
|
|
|
|
p = strstr(s, "\r\n");
|
|
|
|
|
if (p == NULL) {
|
2008-05-07 13:39:42 +00:00
|
|
|
|
kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
*p = 0;
|
|
|
|
|
|
|
|
|
|
p = NULL;
|
|
|
|
|
t = strtok_r(s, " \t", &p);
|
|
|
|
|
if (t == NULL) {
|
2008-05-07 13:39:42 +00:00
|
|
|
|
kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
t = strtok_r(NULL, " \t", &p);
|
|
|
|
|
if(t == NULL) {
|
2008-05-07 13:39:42 +00:00
|
|
|
|
kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
data = malloc(strlen(t));
|
|
|
|
|
if (data == NULL) {
|
2008-05-07 13:39:42 +00:00
|
|
|
|
kdc_log(context, config, 0, "Failed to allocate %lu bytes",
|
2001-06-21 02:12:07 +00:00
|
|
|
|
(unsigned long)strlen(t));
|
2000-01-09 20:58:00 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if(*t == '/')
|
|
|
|
|
t++;
|
|
|
|
|
if(de_http(t) != 0) {
|
2008-05-07 13:39:42 +00:00
|
|
|
|
kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string);
|
|
|
|
|
kdc_log(context, config, 5, "HTTP request: %s", t);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
free(data);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
proto = strtok_r(NULL, " \t", &p);
|
|
|
|
|
if (proto == NULL) {
|
2008-05-07 13:39:42 +00:00
|
|
|
|
kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
free(data);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
len = base64_decode(t, data);
|
|
|
|
|
if(len <= 0){
|
|
|
|
|
const char *msg =
|
|
|
|
|
" 404 Not found\r\n"
|
|
|
|
|
"Server: Heimdal/" VERSION "\r\n"
|
2002-08-30 21:23:27 +00:00
|
|
|
|
"Cache-Control: no-cache\r\n"
|
|
|
|
|
"Pragma: no-cache\r\n"
|
2000-01-09 20:58:00 +00:00
|
|
|
|
"Content-type: text/html\r\n"
|
|
|
|
|
"Content-transfer-encoding: 8bit\r\n\r\n"
|
|
|
|
|
"<TITLE>404 Not found</TITLE>\r\n"
|
|
|
|
|
"<H1>404 Not found</H1>\r\n"
|
|
|
|
|
"That page doesn't exist, maybe you are looking for "
|
2008-05-07 13:39:42 +00:00
|
|
|
|
"<A HREF=\"http://www.h5l.org/\">Heimdal</A>?\r\n";
|
|
|
|
|
kdc_log(context, config, 0, "HTTP request from %s is non KDC request", d->addr_string);
|
|
|
|
|
kdc_log(context, config, 5, "HTTP request: %s", t);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
free(data);
|
2008-05-07 13:39:42 +00:00
|
|
|
|
if (write(d->s, proto, strlen(proto)) < 0) {
|
|
|
|
|
kdc_log(context, config, 0, "HTTP write failed: %s: %s",
|
|
|
|
|
d->addr_string, strerror(errno));
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (write(d->s, msg, strlen(msg)) < 0) {
|
|
|
|
|
kdc_log(context, config, 0, "HTTP write failed: %s: %s",
|
|
|
|
|
d->addr_string, strerror(errno));
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2000-01-09 20:58:00 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
const char *msg =
|
|
|
|
|
" 200 OK\r\n"
|
|
|
|
|
"Server: Heimdal/" VERSION "\r\n"
|
2002-08-30 21:23:27 +00:00
|
|
|
|
"Cache-Control: no-cache\r\n"
|
|
|
|
|
"Pragma: no-cache\r\n"
|
2000-01-09 20:58:00 +00:00
|
|
|
|
"Content-type: application/octet-stream\r\n"
|
|
|
|
|
"Content-transfer-encoding: binary\r\n\r\n";
|
2008-05-07 13:39:42 +00:00
|
|
|
|
if (write(d->s, proto, strlen(proto)) < 0) {
|
|
|
|
|
kdc_log(context, config, 0, "HTTP write failed: %s: %s",
|
|
|
|
|
d->addr_string, strerror(errno));
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (write(d->s, msg, strlen(msg)) < 0) {
|
|
|
|
|
kdc_log(context, config, 0, "HTTP write failed: %s: %s",
|
|
|
|
|
d->addr_string, strerror(errno));
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2000-01-09 20:58:00 +00:00
|
|
|
|
}
|
|
|
|
|
memcpy(d->buf, data, len);
|
|
|
|
|
d->len = len;
|
|
|
|
|
free(data);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Handle incoming data to the TCP socket in `d[index]'
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static void
|
2008-05-07 13:39:42 +00:00
|
|
|
|
handle_tcp(krb5_context context,
|
|
|
|
|
krb5_kdc_configuration *config,
|
|
|
|
|
struct descr *d, int idx, int min_free)
|
2000-01-09 20:58:00 +00:00
|
|
|
|
{
|
|
|
|
|
unsigned char buf[1024];
|
|
|
|
|
int n;
|
|
|
|
|
int ret = 0;
|
|
|
|
|
|
2008-05-07 13:39:42 +00:00
|
|
|
|
if (d[idx].timeout == 0) {
|
|
|
|
|
add_new_tcp (context, config, d, idx, min_free);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2008-05-07 13:39:42 +00:00
|
|
|
|
n = recvfrom(d[idx].s, buf, sizeof(buf), 0, NULL, NULL);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
if(n < 0){
|
2008-05-07 13:39:42 +00:00
|
|
|
|
krb5_warn(context, errno, "recvfrom failed from %s to %s/%d",
|
|
|
|
|
d[idx].addr_string, descr_type(d + idx),
|
|
|
|
|
ntohs(d[idx].port));
|
2000-01-09 20:58:00 +00:00
|
|
|
|
return;
|
2004-04-03 21:22:55 +00:00
|
|
|
|
} else if (n == 0) {
|
2005-02-24 22:22:53 +00:00
|
|
|
|
krb5_warnx(context, "connection closed before end of data after %lu "
|
2008-05-07 13:39:42 +00:00
|
|
|
|
"bytes from %s to %s/%d", (unsigned long)d[idx].len,
|
|
|
|
|
d[idx].addr_string, descr_type(d + idx),
|
|
|
|
|
ntohs(d[idx].port));
|
|
|
|
|
clear_descr (d + idx);
|
2004-04-03 21:22:55 +00:00
|
|
|
|
return;
|
2000-01-09 20:58:00 +00:00
|
|
|
|
}
|
2008-05-07 13:39:42 +00:00
|
|
|
|
if (grow_descr (context, config, &d[idx], n))
|
2000-01-09 20:58:00 +00:00
|
|
|
|
return;
|
2008-05-07 13:39:42 +00:00
|
|
|
|
memcpy(d[idx].buf + d[idx].len, buf, n);
|
|
|
|
|
d[idx].len += n;
|
|
|
|
|
if(d[idx].len > 4 && d[idx].buf[0] == 0) {
|
|
|
|
|
ret = handle_vanilla_tcp (context, config, &d[idx]);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
} else if(enable_http &&
|
2008-05-07 13:39:42 +00:00
|
|
|
|
d[idx].len >= 4 &&
|
|
|
|
|
strncmp((char *)d[idx].buf, "GET ", 4) == 0 &&
|
|
|
|
|
strncmp((char *)d[idx].buf + d[idx].len - 4,
|
2000-01-09 20:58:00 +00:00
|
|
|
|
"\r\n\r\n", 4) == 0) {
|
2008-05-07 13:39:42 +00:00
|
|
|
|
ret = handle_http_tcp (context, config, &d[idx]);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
if (ret < 0)
|
2008-05-07 13:39:42 +00:00
|
|
|
|
clear_descr (d + idx);
|
|
|
|
|
} else if (d[idx].len > 4) {
|
|
|
|
|
kdc_log (context, config,
|
|
|
|
|
0, "TCP data of strange type from %s to %s/%d",
|
|
|
|
|
d[idx].addr_string, descr_type(d + idx),
|
|
|
|
|
ntohs(d[idx].port));
|
|
|
|
|
if (d[idx].buf[0] & 0x80) {
|
|
|
|
|
krb5_data reply;
|
|
|
|
|
|
|
|
|
|
kdc_log (context, config, 0, "TCP extension not supported");
|
|
|
|
|
|
|
|
|
|
ret = krb5_mk_error(context,
|
|
|
|
|
KRB5KRB_ERR_FIELD_TOOLONG,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
&reply);
|
|
|
|
|
if (ret == 0) {
|
|
|
|
|
send_reply(context, config, TRUE, d + idx, &reply);
|
|
|
|
|
krb5_data_free(&reply);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
clear_descr(d + idx);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (ret < 0)
|
|
|
|
|
return;
|
|
|
|
|
else if (ret == 1) {
|
2008-05-07 13:39:42 +00:00
|
|
|
|
do_request(context, config,
|
|
|
|
|
d[idx].buf, d[idx].len, TRUE, &d[idx]);
|
|
|
|
|
clear_descr(d + idx);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2008-05-07 13:39:42 +00:00
|
|
|
|
loop(krb5_context context,
|
|
|
|
|
krb5_kdc_configuration *config)
|
2000-01-09 20:58:00 +00:00
|
|
|
|
{
|
|
|
|
|
struct descr *d;
|
|
|
|
|
int ndescr;
|
|
|
|
|
|
2008-05-07 13:39:42 +00:00
|
|
|
|
ndescr = init_sockets(context, config, &d);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
if(ndescr <= 0)
|
|
|
|
|
krb5_errx(context, 1, "No sockets!");
|
2008-05-07 13:39:42 +00:00
|
|
|
|
kdc_log(context, config, 0, "KDC started");
|
2000-01-09 20:58:00 +00:00
|
|
|
|
while(exit_flag == 0){
|
|
|
|
|
struct timeval tmout;
|
|
|
|
|
fd_set fds;
|
|
|
|
|
int min_free = -1;
|
|
|
|
|
int max_fd = 0;
|
|
|
|
|
int i;
|
2001-02-13 16:46:19 +00:00
|
|
|
|
|
2000-01-09 20:58:00 +00:00
|
|
|
|
FD_ZERO(&fds);
|
2001-02-13 16:46:19 +00:00
|
|
|
|
for(i = 0; i < ndescr; i++) {
|
2000-01-09 20:58:00 +00:00
|
|
|
|
if(d[i].s >= 0){
|
|
|
|
|
if(d[i].type == SOCK_STREAM &&
|
2000-02-24 11:28:20 +00:00
|
|
|
|
d[i].timeout && d[i].timeout < time(NULL)) {
|
2008-05-07 13:39:42 +00:00
|
|
|
|
kdc_log(context, config, 1,
|
|
|
|
|
"TCP-connection from %s expired after %lu bytes",
|
2001-06-21 02:12:07 +00:00
|
|
|
|
d[i].addr_string, (unsigned long)d[i].len);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
clear_descr(&d[i]);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if(max_fd < d[i].s)
|
|
|
|
|
max_fd = d[i].s;
|
2001-02-13 16:46:19 +00:00
|
|
|
|
if (max_fd >= FD_SETSIZE)
|
|
|
|
|
krb5_errx(context, 1, "fd too large");
|
2000-01-09 20:58:00 +00:00
|
|
|
|
FD_SET(d[i].s, &fds);
|
2001-02-13 16:46:19 +00:00
|
|
|
|
} else if(min_free < 0 || i < min_free)
|
2000-01-09 20:58:00 +00:00
|
|
|
|
min_free = i;
|
|
|
|
|
}
|
|
|
|
|
if(min_free == -1){
|
|
|
|
|
struct descr *tmp;
|
|
|
|
|
tmp = realloc(d, (ndescr + 4) * sizeof(*d));
|
|
|
|
|
if(tmp == NULL)
|
|
|
|
|
krb5_warnx(context, "No memory");
|
2001-02-13 16:46:19 +00:00
|
|
|
|
else {
|
2000-01-09 20:58:00 +00:00
|
|
|
|
d = tmp;
|
2001-02-13 16:46:19 +00:00
|
|
|
|
reinit_descrs (d, ndescr);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
memset(d + ndescr, 0, 4 * sizeof(*d));
|
|
|
|
|
for(i = ndescr; i < ndescr + 4; i++)
|
2001-02-13 16:46:19 +00:00
|
|
|
|
init_descr (&d[i]);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
min_free = ndescr;
|
|
|
|
|
ndescr += 4;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tmout.tv_sec = TCP_TIMEOUT;
|
|
|
|
|
tmout.tv_usec = 0;
|
|
|
|
|
switch(select(max_fd + 1, &fds, 0, 0, &tmout)){
|
|
|
|
|
case 0:
|
|
|
|
|
break;
|
|
|
|
|
case -1:
|
2000-02-24 11:19:29 +00:00
|
|
|
|
if (errno != EINTR)
|
|
|
|
|
krb5_warn(context, errno, "select");
|
2000-01-09 20:58:00 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
for(i = 0; i < ndescr; i++)
|
|
|
|
|
if(d[i].s >= 0 && FD_ISSET(d[i].s, &fds)) {
|
|
|
|
|
if(d[i].type == SOCK_DGRAM)
|
2008-05-07 13:39:42 +00:00
|
|
|
|
handle_udp(context, config, &d[i]);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
else if(d[i].type == SOCK_STREAM)
|
2008-05-07 13:39:42 +00:00
|
|
|
|
handle_tcp(context, config, d, i, min_free);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-05-07 13:39:42 +00:00
|
|
|
|
if(exit_flag == SIGXCPU)
|
|
|
|
|
kdc_log(context, config, 0, "CPU time limit exceeded");
|
|
|
|
|
else if(exit_flag == SIGINT || exit_flag == SIGTERM)
|
|
|
|
|
kdc_log(context, config, 0, "Terminated");
|
|
|
|
|
else
|
|
|
|
|
kdc_log(context, config, 0, "Unexpected exit reason: %d", exit_flag);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
free (d);
|
|
|
|
|
}
|