Use SOCKET data type instead of ints for sockets in kadmin
Also use the new mini_inetd() API
This commit is contained in:

committed by
Love Hornquist Astrand

parent
14ae739820
commit
a1942c1bad
@@ -124,15 +124,15 @@ spawn_child(krb5_context context, int *socks,
|
||||
struct sockaddr_storage __ss;
|
||||
struct sockaddr *sa = (struct sockaddr *)&__ss;
|
||||
socklen_t sa_size = sizeof(__ss);
|
||||
int s;
|
||||
SOCKET s;
|
||||
pid_t pid;
|
||||
krb5_address addr;
|
||||
char buf[128];
|
||||
size_t buf_len;
|
||||
|
||||
s = accept(socks[this_sock], sa, &sa_size);
|
||||
if(s < 0) {
|
||||
krb5_warn(context, errno, "accept");
|
||||
if(IS_BAD_SOCKET(s)) {
|
||||
krb5_warn(context, SOCK_ERRNO, "accept");
|
||||
return 1;
|
||||
}
|
||||
e = krb5_sockaddr2address(context, sa, &addr);
|
||||
@@ -151,21 +151,21 @@ spawn_child(krb5_context context, int *socks,
|
||||
pid = fork();
|
||||
if(pid == 0) {
|
||||
for(i = 0; i < num_socks; i++)
|
||||
close(socks[i]);
|
||||
closesocket(socks[i]);
|
||||
dup2(s, STDIN_FILENO);
|
||||
dup2(s, STDOUT_FILENO);
|
||||
if(s != STDIN_FILENO && s != STDOUT_FILENO)
|
||||
close(s);
|
||||
closesocket(s);
|
||||
return 0;
|
||||
} else {
|
||||
close(s);
|
||||
closesocket(s);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
wait_for_connection(krb5_context context,
|
||||
int *socks, unsigned int num_socks)
|
||||
SOCKET *socks, unsigned int num_socks)
|
||||
{
|
||||
unsigned int i;
|
||||
int e;
|
||||
@@ -175,8 +175,10 @@ wait_for_connection(krb5_context context,
|
||||
FD_ZERO(&orig_read_set);
|
||||
|
||||
for(i = 0; i < num_socks; i++) {
|
||||
#ifndef NO_LIMIT_FD_SETSIZE
|
||||
if (socks[i] >= FD_SETSIZE)
|
||||
errx (1, "fd too large");
|
||||
#endif
|
||||
FD_SET(socks[i], &orig_read_set);
|
||||
max_fd = max(max_fd, socks[i]);
|
||||
}
|
||||
@@ -193,9 +195,9 @@ wait_for_connection(krb5_context context,
|
||||
while (term_flag == 0) {
|
||||
read_set = orig_read_set;
|
||||
e = select(max_fd + 1, &read_set, NULL, NULL, NULL);
|
||||
if(e < 0) {
|
||||
if(errno != EINTR)
|
||||
krb5_warn(context, errno, "select");
|
||||
if(IS_SOCKET_ERROR(e)) {
|
||||
if(SOCK_ERRNO != EINTR)
|
||||
krb5_warn(context, SOCK_ERRNO, "select");
|
||||
} else if(e == 0)
|
||||
krb5_warnx(context, "select returned 0");
|
||||
else {
|
||||
@@ -224,7 +226,7 @@ start_server(krb5_context context)
|
||||
int e;
|
||||
struct kadm_port *p;
|
||||
|
||||
int *socks = NULL, *tmp;
|
||||
SOCKET *socks = NULL, *tmp;
|
||||
unsigned int num_socks = 0;
|
||||
int i;
|
||||
|
||||
@@ -257,23 +259,23 @@ start_server(krb5_context context)
|
||||
}
|
||||
socks = tmp;
|
||||
for(ap = ai; ap; ap = ap->ai_next) {
|
||||
int s = socket(ap->ai_family, ap->ai_socktype, ap->ai_protocol);
|
||||
if(s < 0) {
|
||||
krb5_warn(context, errno, "socket");
|
||||
SOCKET s = socket(ap->ai_family, ap->ai_socktype, ap->ai_protocol);
|
||||
if(IS_BAD_SOCKET(s)) {
|
||||
krb5_warn(context, SOCK_ERRNO, "socket");
|
||||
continue;
|
||||
}
|
||||
|
||||
socket_set_reuseaddr(s, 1);
|
||||
socket_set_ipv6only(s, 1);
|
||||
|
||||
if (bind (s, ap->ai_addr, ap->ai_addrlen) < 0) {
|
||||
krb5_warn(context, errno, "bind");
|
||||
close(s);
|
||||
if (IS_SOCKET_ERROR(bind (s, ap->ai_addr, ap->ai_addrlen))) {
|
||||
krb5_warn(context, SOCK_ERRNO, "bind");
|
||||
closesocket(s);
|
||||
continue;
|
||||
}
|
||||
if (listen (s, SOMAXCONN) < 0) {
|
||||
krb5_warn(context, errno, "listen");
|
||||
close(s);
|
||||
if (IS_SOCKET_ERROR(listen (s, SOMAXCONN))) {
|
||||
krb5_warn(context, SOCK_ERRNO, "listen");
|
||||
closesocket(s);
|
||||
continue;
|
||||
}
|
||||
socks[num_socks++] = s;
|
||||
@@ -282,5 +284,6 @@ start_server(krb5_context context)
|
||||
}
|
||||
if(num_socks == 0)
|
||||
krb5_errx(context, 1, "no sockets to listen to - exiting");
|
||||
|
||||
return wait_for_connection(context, socks, num_socks);
|
||||
}
|
||||
|
@@ -95,6 +95,7 @@ main(int argc, char **argv)
|
||||
int i;
|
||||
krb5_log_facility *logfacility;
|
||||
krb5_keytab keytab;
|
||||
SOCKET sfd = INVALID_SOCKET;
|
||||
|
||||
setprogname(argv[0]);
|
||||
|
||||
@@ -168,8 +169,9 @@ main(int argc, char **argv)
|
||||
"tcp", 749);
|
||||
else
|
||||
debug_port = htons(atoi(port_str));
|
||||
mini_inetd(debug_port);
|
||||
mini_inetd(debug_port, &sfd);
|
||||
} else {
|
||||
#ifndef NO_INETD
|
||||
struct sockaddr_storage __ss;
|
||||
struct sockaddr *sa = (struct sockaddr *)&__ss;
|
||||
socklen_t sa_size = sizeof(__ss);
|
||||
@@ -178,19 +180,24 @@ main(int argc, char **argv)
|
||||
* Check if we are running inside inetd or not, if not, start
|
||||
* our own server.
|
||||
*/
|
||||
|
||||
|
||||
if(roken_getsockname(STDIN_FILENO, sa, &sa_size) < 0 &&
|
||||
errno == ENOTSOCK) {
|
||||
SOCK_ERRNO == ENOTSOCK) {
|
||||
#endif
|
||||
parse_ports(context, port_str ? port_str : "+");
|
||||
pidfile(NULL);
|
||||
start_server(context);
|
||||
start_server(context, &sfd);
|
||||
#ifndef NO_INETD
|
||||
} else {
|
||||
sfd = STDIN_FILENO;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
if(realm)
|
||||
krb5_set_default_realm(context, realm); /* XXX */
|
||||
|
||||
kadmind_loop(context, keytab, STDIN_FILENO);
|
||||
kadmind_loop(context, keytab, sfd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@@ -1091,16 +1091,16 @@ process_stream(krb5_context context,
|
||||
|
||||
|
||||
int
|
||||
handle_mit(krb5_context context, void *buf, size_t len, int fd)
|
||||
handle_mit(krb5_context context, void *buf, size_t len, SOCKET sock)
|
||||
{
|
||||
krb5_storage *sp;
|
||||
|
||||
dcontext = context;
|
||||
|
||||
sp = krb5_storage_from_fd(fd);
|
||||
sp = krb5_storage_from_fd(sock);
|
||||
INSIST(sp != NULL);
|
||||
|
||||
process_stream(context, buf, len, sp);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@@ -440,7 +440,7 @@ v5_loop (krb5_context context,
|
||||
krb5_auth_context ac,
|
||||
krb5_boolean initial,
|
||||
void *kadm_handle,
|
||||
int fd)
|
||||
SOCKET fd)
|
||||
{
|
||||
krb5_error_code ret;
|
||||
krb5_data in, out;
|
||||
@@ -476,7 +476,7 @@ match_appl_version(const void *data, const char *appl_version)
|
||||
static void
|
||||
handle_v5(krb5_context context,
|
||||
krb5_keytab keytab,
|
||||
int fd)
|
||||
SOCKET fd)
|
||||
{
|
||||
krb5_error_code ret;
|
||||
krb5_ticket *ticket;
|
||||
@@ -539,13 +539,13 @@ handle_v5(krb5_context context,
|
||||
krb5_error_code
|
||||
kadmind_loop(krb5_context context,
|
||||
krb5_keytab keytab,
|
||||
int fd)
|
||||
SOCKET sock)
|
||||
{
|
||||
u_char buf[sizeof(KRB5_SENDAUTH_VERSION) + 4];
|
||||
ssize_t n;
|
||||
unsigned long len;
|
||||
|
||||
n = krb5_net_read(context, &fd, buf, 4);
|
||||
n = krb5_net_read(context, &sock, buf, 4);
|
||||
if(n == 0)
|
||||
exit(0);
|
||||
if(n < 0)
|
||||
@@ -554,21 +554,21 @@ kadmind_loop(krb5_context context,
|
||||
|
||||
if (len == sizeof(KRB5_SENDAUTH_VERSION)) {
|
||||
|
||||
n = krb5_net_read(context, &fd, buf + 4, len);
|
||||
n = krb5_net_read(context, &sock, buf + 4, len);
|
||||
if (n < 0)
|
||||
krb5_err (context, 1, errno, "reading sendauth version");
|
||||
if (n == 0)
|
||||
krb5_errx (context, 1, "EOF reading sendauth version");
|
||||
|
||||
if(memcmp(buf + 4, KRB5_SENDAUTH_VERSION, len) == 0) {
|
||||
handle_v5(context, keytab, fd);
|
||||
handle_v5(context, keytab, sock);
|
||||
return 0;
|
||||
}
|
||||
len += 4;
|
||||
} else
|
||||
len = 4;
|
||||
|
||||
handle_mit(context, buf, len, fd);
|
||||
handle_mit(context, buf, len, sock);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user