2013-07-10 17:12:42 -04:00
|
|
|
/* Code and variables that is common to both fork and select-based
|
|
|
|
* servers.
|
|
|
|
*
|
|
|
|
* No code here should assume whether sockets are blocking or not.
|
|
|
|
**/
|
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <stdarg.h>
|
2014-03-30 11:28:00 -04:00
|
|
|
#include <grp.h>
|
2013-07-10 17:12:42 -04:00
|
|
|
|
|
|
|
#include "common.h"
|
2013-09-23 18:30:34 -04:00
|
|
|
#include "probe.h"
|
2013-07-10 17:12:42 -04:00
|
|
|
|
|
|
|
/* Added to make the code compilable under CYGWIN
|
|
|
|
* */
|
|
|
|
#ifndef SA_NOCLDWAIT
|
|
|
|
#define SA_NOCLDWAIT 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
2013-07-10 17:14:15 -04:00
|
|
|
* Settings that depend on the command line. They're set in main(), but also
|
|
|
|
* used in other places in common.c, and it'd be heavy-handed to pass it all as
|
|
|
|
* parameters
|
2013-07-10 17:12:42 -04:00
|
|
|
*/
|
|
|
|
int verbose = 0;
|
|
|
|
int probing_timeout = 2;
|
|
|
|
int inetd = 0;
|
|
|
|
int foreground = 0;
|
2013-07-10 17:16:50 -04:00
|
|
|
int background = 0;
|
2013-07-21 07:46:45 -04:00
|
|
|
int transparent = 0;
|
2013-07-10 17:13:32 -04:00
|
|
|
int numeric = 0;
|
2013-07-10 17:19:33 -04:00
|
|
|
const char *user_name, *pid_file;
|
2013-07-10 17:12:42 -04:00
|
|
|
|
2013-07-10 17:14:15 -04:00
|
|
|
struct addrinfo *addr_listen = NULL; /* what addresses do we listen to? */
|
2013-07-10 17:13:32 -04:00
|
|
|
|
2013-07-10 17:12:42 -04:00
|
|
|
#ifdef LIBWRAP
|
|
|
|
#include <tcpd.h>
|
|
|
|
int allow_severity =0, deny_severity = 0;
|
|
|
|
#endif
|
|
|
|
|
2013-07-10 17:13:32 -04:00
|
|
|
/* check result and die, printing the offending address and error */
|
2013-07-10 17:14:15 -04:00
|
|
|
void check_res_dumpdie(int res, struct addrinfo *addr, char* syscall)
|
2013-07-10 17:13:32 -04:00
|
|
|
{
|
2013-07-10 17:14:15 -04:00
|
|
|
char buf[NI_MAXHOST];
|
2013-07-10 17:13:32 -04:00
|
|
|
|
|
|
|
if (res == -1) {
|
|
|
|
fprintf(stderr, "%s:%s: %s\n",
|
2013-07-10 17:14:15 -04:00
|
|
|
sprintaddr(buf, sizeof(buf), addr),
|
2013-07-10 17:13:32 -04:00
|
|
|
syscall,
|
|
|
|
strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2013-07-10 17:12:42 -04:00
|
|
|
|
2013-07-10 17:13:32 -04:00
|
|
|
/* Starts listening sockets on specified addresses.
|
|
|
|
* IN: addr[], num_addr
|
2013-07-10 17:14:15 -04:00
|
|
|
* OUT: *sockfd[] pointer to newly-allocated array of file descriptors
|
|
|
|
* Returns number of addresses bound
|
|
|
|
* Bound file descriptors are returned in newly-allocated *sockfd pointer
|
2013-07-10 17:12:42 -04:00
|
|
|
*/
|
2013-07-10 17:14:15 -04:00
|
|
|
int start_listen_sockets(int *sockfd[], struct addrinfo *addr_list)
|
2013-07-10 17:12:42 -04:00
|
|
|
{
|
2013-07-10 17:13:32 -04:00
|
|
|
struct sockaddr_storage *saddr;
|
2013-07-10 17:14:15 -04:00
|
|
|
struct addrinfo *addr;
|
2014-02-09 07:29:49 -05:00
|
|
|
int i, res, one;
|
2013-07-10 17:14:15 -04:00
|
|
|
int num_addr = 0;
|
2013-07-10 17:12:42 -04:00
|
|
|
|
2013-07-10 17:14:15 -04:00
|
|
|
for (addr = addr_list; addr; addr = addr->ai_next)
|
|
|
|
num_addr++;
|
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "listening to %d addresses\n", num_addr);
|
|
|
|
|
|
|
|
*sockfd = malloc(num_addr * sizeof(*sockfd[0]));
|
|
|
|
|
|
|
|
for (i = 0, addr = addr_list; i < num_addr && addr; i++, addr = addr->ai_next) {
|
|
|
|
if (!addr) {
|
|
|
|
fprintf(stderr, "FATAL: Inconsistent listen number. This should not happen.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
saddr = (struct sockaddr_storage*)addr->ai_addr;
|
2013-07-10 17:12:42 -04:00
|
|
|
|
2013-07-10 17:14:15 -04:00
|
|
|
(*sockfd)[i] = socket(saddr->ss_family, SOCK_STREAM, 0);
|
|
|
|
check_res_dumpdie((*sockfd)[i], addr, "socket");
|
2013-07-10 17:12:42 -04:00
|
|
|
|
2014-02-09 07:29:49 -05:00
|
|
|
one = 1;
|
|
|
|
res = setsockopt((*sockfd)[i], SOL_SOCKET, SO_REUSEADDR, (char*)&one, sizeof(one));
|
|
|
|
check_res_dumpdie(res, addr, "setsockopt(SO_REUSEADDR)");
|
|
|
|
|
|
|
|
if (IP_FREEBIND) {
|
|
|
|
res = setsockopt((*sockfd)[i], IPPROTO_IP, IP_FREEBIND, (char*)&one, sizeof(one));
|
|
|
|
check_res_dumpdie(res, addr, "setsockopt(IP_FREEBIND)");
|
|
|
|
}
|
2013-07-10 17:12:42 -04:00
|
|
|
|
2013-07-10 17:14:15 -04:00
|
|
|
res = bind((*sockfd)[i], addr->ai_addr, addr->ai_addrlen);
|
|
|
|
check_res_dumpdie(res, addr, "bind");
|
|
|
|
|
|
|
|
res = listen ((*sockfd)[i], 50);
|
|
|
|
check_res_dumpdie(res, addr, "listen");
|
2013-07-10 17:12:42 -04:00
|
|
|
|
2013-07-10 17:13:32 -04:00
|
|
|
}
|
2013-07-10 17:14:15 -04:00
|
|
|
|
|
|
|
return num_addr;
|
|
|
|
}
|
|
|
|
|
2013-07-21 07:46:45 -04:00
|
|
|
/* Transparent proxying: bind the peer address of fd to the peer address of
|
|
|
|
* fd_from */
|
|
|
|
#define IP_TRANSPARENT 19
|
|
|
|
int bind_peer(int fd, int fd_from)
|
|
|
|
{
|
|
|
|
struct addrinfo from;
|
|
|
|
struct sockaddr_storage ss;
|
|
|
|
int res, trans = 1;
|
|
|
|
|
|
|
|
memset(&from, 0, sizeof(from));
|
|
|
|
from.ai_addr = (struct sockaddr*)&ss;
|
|
|
|
from.ai_addrlen = sizeof(ss);
|
|
|
|
|
2013-08-08 05:27:21 -04:00
|
|
|
/* getpeername can fail with ENOTCONN if connection was dropped before we
|
|
|
|
* got here */
|
2013-07-21 07:46:45 -04:00
|
|
|
res = getpeername(fd_from, from.ai_addr, &from.ai_addrlen);
|
2013-08-08 05:27:21 -04:00
|
|
|
CHECK_RES_RETURN(res, "getpeername");
|
2014-07-24 11:29:53 -04:00
|
|
|
#ifndef IP_BINDANY /* use IP_TRANSPARENT */
|
2013-07-25 15:33:07 -04:00
|
|
|
res = setsockopt(fd, IPPROTO_IP, IP_TRANSPARENT, &trans, sizeof(trans));
|
2013-07-21 07:46:45 -04:00
|
|
|
CHECK_RES_DIE(res, "setsockopt");
|
2014-07-24 11:29:53 -04:00
|
|
|
#else
|
2014-06-20 08:11:25 -04:00
|
|
|
if (from.ai_addr->sa_family==AF_INET) { /* IPv4 */
|
|
|
|
res = setsockopt(fd, IPPROTO_IP, IP_BINDANY, &trans, sizeof(trans));
|
2014-07-24 11:29:53 -04:00
|
|
|
CHECK_RES_RETURN(res, "setsockopt IP_BINDANY");
|
|
|
|
#ifdef IPV6_BINDANY
|
2014-06-20 08:11:25 -04:00
|
|
|
} else { /* IPv6 */
|
|
|
|
res = setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &trans, sizeof(trans));
|
2014-07-24 11:29:53 -04:00
|
|
|
CHECK_RES_RETURN(res, "setsockopt IPV6_BINDANY");
|
|
|
|
#endif /* IPV6_BINDANY */
|
2014-06-20 08:11:25 -04:00
|
|
|
}
|
2014-07-24 11:29:53 -04:00
|
|
|
#endif /* IP_TRANSPARENT / IP_BINDANY */
|
2013-07-21 07:46:45 -04:00
|
|
|
res = bind(fd, from.ai_addr, from.ai_addrlen);
|
|
|
|
CHECK_RES_RETURN(res, "bind");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-10 17:14:15 -04:00
|
|
|
/* Connect to first address that works and returns a file descriptor, or -1 if
|
2013-07-21 07:46:45 -04:00
|
|
|
* none work.
|
|
|
|
* If transparent proxying is on, use fd_from peer address on external address
|
2013-09-23 18:30:36 -04:00
|
|
|
* of new file descriptor. */
|
|
|
|
int connect_addr(struct connection *cnx, int fd_from)
|
2013-07-10 17:14:15 -04:00
|
|
|
{
|
2014-07-22 13:36:40 -04:00
|
|
|
struct addrinfo *a, from;
|
|
|
|
struct sockaddr_storage ss;
|
2013-07-10 17:14:15 -04:00
|
|
|
char buf[NI_MAXHOST];
|
|
|
|
int fd, res;
|
|
|
|
|
2014-07-22 13:36:40 -04:00
|
|
|
memset(&from, 0, sizeof(from));
|
|
|
|
from.ai_addr = (struct sockaddr*)&ss;
|
|
|
|
from.ai_addrlen = sizeof(ss);
|
|
|
|
|
|
|
|
res = getpeername(fd_from, from.ai_addr, &from.ai_addrlen);
|
|
|
|
CHECK_RES_RETURN(res, "getpeername");
|
|
|
|
|
2013-09-23 18:30:36 -04:00
|
|
|
for (a = cnx->proto->saddr; a; a = a->ai_next) {
|
2014-07-22 13:36:40 -04:00
|
|
|
/* When transparent, make sure both connections use the same address family */
|
|
|
|
if (transparent && a->ai_family != from.ai_addr->sa_family)
|
|
|
|
continue;
|
2013-07-10 17:14:15 -04:00
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "connecting to %s family %d len %d\n",
|
|
|
|
sprintaddr(buf, sizeof(buf), a),
|
|
|
|
a->ai_addr->sa_family, a->ai_addrlen);
|
2014-06-20 08:11:25 -04:00
|
|
|
|
2014-07-24 11:29:53 -04:00
|
|
|
/* XXX Needs to match ai_family from fd_from when being transparent! */
|
2013-07-10 17:14:15 -04:00
|
|
|
fd = socket(a->ai_family, SOCK_STREAM, 0);
|
|
|
|
if (fd == -1) {
|
2013-09-23 18:30:36 -04:00
|
|
|
log_message(LOG_ERR, "forward to %s failed:socket: %s\n",
|
|
|
|
cnx->proto->description, strerror(errno));
|
2013-07-10 17:14:15 -04:00
|
|
|
} else {
|
2013-08-08 05:27:21 -04:00
|
|
|
if (transparent) {
|
|
|
|
res = bind_peer(fd, fd_from);
|
|
|
|
CHECK_RES_RETURN(res, "bind_peer");
|
|
|
|
}
|
2013-07-10 17:14:15 -04:00
|
|
|
res = connect(fd, a->ai_addr, a->ai_addrlen);
|
|
|
|
if (res == -1) {
|
|
|
|
log_message(LOG_ERR, "forward to %s failed:connect: %s\n",
|
2013-09-23 18:30:36 -04:00
|
|
|
cnx->proto->description, strerror(errno));
|
2014-02-13 23:32:38 -05:00
|
|
|
close(fd);
|
2013-07-10 17:14:15 -04:00
|
|
|
} else {
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
2013-07-10 17:12:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Store some data to write to the queue later */
|
|
|
|
int defer_write(struct queue *q, void* data, int data_size)
|
|
|
|
{
|
2013-09-23 18:30:32 -04:00
|
|
|
char *p;
|
2013-07-10 17:12:42 -04:00
|
|
|
if (verbose)
|
2013-09-23 18:30:38 -04:00
|
|
|
fprintf(stderr, "**** writing deferred on fd %d\n", q->fd);
|
2013-09-23 18:30:32 -04:00
|
|
|
|
2013-09-28 15:33:25 -04:00
|
|
|
p = realloc(q->begin_deferred_data, q->deferred_data_size + data_size);
|
2013-09-23 18:30:32 -04:00
|
|
|
if (!p) {
|
|
|
|
perror("realloc");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2013-09-23 18:30:38 -04:00
|
|
|
q->deferred_data = q->begin_deferred_data = p;
|
|
|
|
p += q->deferred_data_size;
|
|
|
|
q->deferred_data_size += data_size;
|
2013-09-23 18:30:32 -04:00
|
|
|
memcpy(p, data, data_size);
|
2013-07-10 17:12:42 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* tries to flush some of the data for specified queue
|
|
|
|
* Upon success, the number of bytes written is returned.
|
|
|
|
* Upon failure, -1 returned (e.g. connexion closed)
|
|
|
|
* */
|
2013-09-23 18:30:38 -04:00
|
|
|
int flush_deferred(struct queue *q)
|
2013-07-10 17:12:42 -04:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (verbose)
|
2013-09-23 18:30:38 -04:00
|
|
|
fprintf(stderr, "flushing deferred data to fd %d\n", q->fd);
|
2013-07-10 17:12:42 -04:00
|
|
|
|
2013-09-23 18:30:38 -04:00
|
|
|
n = write(q->fd, q->deferred_data, q->deferred_data_size);
|
2013-07-10 17:12:42 -04:00
|
|
|
if (n == -1)
|
|
|
|
return n;
|
|
|
|
|
2013-09-23 18:30:38 -04:00
|
|
|
if (n == q->deferred_data_size) {
|
2013-07-10 17:12:42 -04:00
|
|
|
/* All has been written -- release the memory */
|
2013-09-23 18:30:38 -04:00
|
|
|
free(q->begin_deferred_data);
|
|
|
|
q->begin_deferred_data = NULL;
|
|
|
|
q->deferred_data = NULL;
|
|
|
|
q->deferred_data_size = 0;
|
2013-07-10 17:12:42 -04:00
|
|
|
} else {
|
|
|
|
/* There is data left */
|
2013-09-23 18:30:38 -04:00
|
|
|
q->deferred_data += n;
|
|
|
|
q->deferred_data_size -= n;
|
2013-07-10 17:12:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void init_cnx(struct connection *cnx)
|
|
|
|
{
|
|
|
|
memset(cnx, 0, sizeof(*cnx));
|
|
|
|
cnx->q[0].fd = -1;
|
|
|
|
cnx->q[1].fd = -1;
|
2013-09-23 18:30:34 -04:00
|
|
|
cnx->proto = get_first_protocol();
|
2013-07-10 17:12:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void dump_connection(struct connection *cnx)
|
|
|
|
{
|
|
|
|
printf("state: %d\n", cnx->state);
|
2013-09-23 18:30:38 -04:00
|
|
|
printf("fd %d, %d deferred\n", cnx->q[0].fd, cnx->q[0].deferred_data_size);
|
|
|
|
printf("fd %d, %d deferred\n", cnx->q[1].fd, cnx->q[1].deferred_data_size);
|
2013-07-10 17:12:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* moves data from one fd to other
|
|
|
|
*
|
2013-09-28 14:49:46 -04:00
|
|
|
* returns number of bytes copied if success
|
2013-07-10 17:12:42 -04:00
|
|
|
* returns 0 (FD_CNXCLOSED) if incoming socket closed
|
|
|
|
* returns FD_NODATA if no data was available
|
|
|
|
* returns FD_STALLED if data was read, could not be written, and has been
|
|
|
|
* stored in temporary buffer.
|
|
|
|
*/
|
|
|
|
int fd2fd(struct queue *target_q, struct queue *from_q)
|
|
|
|
{
|
|
|
|
char buffer[BUFSIZ];
|
|
|
|
int target, from, size_r, size_w;
|
|
|
|
|
|
|
|
target = target_q->fd;
|
|
|
|
from = from_q->fd;
|
|
|
|
|
|
|
|
size_r = read(from, buffer, sizeof(buffer));
|
|
|
|
if (size_r == -1) {
|
|
|
|
switch (errno) {
|
|
|
|
case EAGAIN:
|
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "reading 0 from %d\n", from);
|
|
|
|
return FD_NODATA;
|
|
|
|
|
|
|
|
case ECONNRESET:
|
|
|
|
case EPIPE:
|
|
|
|
return FD_CNXCLOSED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK_RES_RETURN(size_r, "read");
|
|
|
|
|
|
|
|
if (size_r == 0)
|
|
|
|
return FD_CNXCLOSED;
|
|
|
|
|
|
|
|
size_w = write(target, buffer, size_r);
|
|
|
|
/* process -1 when we know how to deal with it */
|
2013-09-28 14:49:46 -04:00
|
|
|
if (size_w == -1) {
|
2013-07-10 17:12:42 -04:00
|
|
|
switch (errno) {
|
|
|
|
case EAGAIN:
|
|
|
|
/* write blocked: Defer data */
|
|
|
|
defer_write(target_q, buffer, size_r);
|
|
|
|
return FD_STALLED;
|
|
|
|
|
|
|
|
case ECONNRESET:
|
|
|
|
case EPIPE:
|
|
|
|
/* remove end closed -- drop the connection */
|
|
|
|
return FD_CNXCLOSED;
|
|
|
|
}
|
|
|
|
} else if (size_w < size_r) {
|
|
|
|
/* incomplete write -- defer the rest of the data */
|
|
|
|
defer_write(target_q, buffer + size_w, size_r - size_w);
|
|
|
|
return FD_STALLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK_RES_RETURN(size_w, "write");
|
|
|
|
|
|
|
|
return size_w;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns a string that prints the IP and port of the sockaddr */
|
2013-07-10 17:14:15 -04:00
|
|
|
char* sprintaddr(char* buf, size_t size, struct addrinfo *a)
|
2013-07-10 17:12:42 -04:00
|
|
|
{
|
2013-07-10 17:13:32 -04:00
|
|
|
char host[NI_MAXHOST], serv[NI_MAXSERV];
|
2013-07-10 17:14:15 -04:00
|
|
|
int res;
|
|
|
|
|
|
|
|
res = getnameinfo(a->ai_addr, a->ai_addrlen,
|
|
|
|
host, sizeof(host),
|
|
|
|
serv, sizeof(serv),
|
|
|
|
numeric ? NI_NUMERICHOST | NI_NUMERICSERV : 0 );
|
|
|
|
|
|
|
|
if (res) {
|
2013-07-10 17:14:48 -04:00
|
|
|
log_message(LOG_ERR, "sprintaddr:getnameinfo: %s\n", gai_strerror(res));
|
|
|
|
/* Name resolution failed: do it numerically instead */
|
|
|
|
res = getnameinfo(a->ai_addr, a->ai_addrlen,
|
|
|
|
host, sizeof(host),
|
|
|
|
serv, sizeof(serv),
|
|
|
|
NI_NUMERICHOST | NI_NUMERICSERV);
|
|
|
|
/* should not fail but... */
|
|
|
|
if (res) {
|
|
|
|
log_message(LOG_ERR, "sprintaddr:getnameinfo(NUM): %s\n", gai_strerror(res));
|
|
|
|
strcpy(host, "?");
|
|
|
|
strcpy(serv, "?");
|
|
|
|
}
|
2013-07-10 17:14:15 -04:00
|
|
|
}
|
2013-07-10 17:13:32 -04:00
|
|
|
|
|
|
|
snprintf(buf, size, "%s:%s", host, serv);
|
2013-07-10 17:12:42 -04:00
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2013-07-10 17:15:38 -04:00
|
|
|
/* Turns a hostname and port (or service) into a list of struct addrinfo
|
|
|
|
* returns 0 on success, -1 otherwise and logs error
|
|
|
|
**/
|
|
|
|
int resolve_split_name(struct addrinfo **out, const char* host, const char* serv)
|
|
|
|
{
|
|
|
|
struct addrinfo hint;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
memset(&hint, 0, sizeof(hint));
|
|
|
|
hint.ai_family = PF_UNSPEC;
|
|
|
|
hint.ai_socktype = SOCK_STREAM;
|
|
|
|
|
|
|
|
res = getaddrinfo(host, serv, &hint, out);
|
|
|
|
if (res)
|
|
|
|
log_message(LOG_ERR, "%s `%s:%s'\n", gai_strerror(res), host, serv);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-07-10 17:14:15 -04:00
|
|
|
/* turns a "hostname:port" string into a list of struct addrinfo;
|
|
|
|
out: list of newly allocated addrinfo (see getaddrinfo(3)); freeaddrinfo(3) when done
|
2013-07-10 17:12:42 -04:00
|
|
|
fullname: input string -- it gets clobbered
|
|
|
|
*/
|
2013-07-10 17:14:15 -04:00
|
|
|
void resolve_name(struct addrinfo **out, char* fullname)
|
2013-07-10 17:12:42 -04:00
|
|
|
{
|
2014-12-22 12:19:02 -05:00
|
|
|
char *serv, *host, *end;
|
2013-07-10 17:12:42 -04:00
|
|
|
int res;
|
|
|
|
|
2014-12-22 12:19:02 -05:00
|
|
|
/* Find port */
|
2013-07-10 17:13:32 -04:00
|
|
|
char *sep = strrchr(fullname, ':');
|
2014-12-22 12:19:02 -05:00
|
|
|
if (!sep) { /* No separator: parameter is just a port */
|
2013-07-10 17:15:38 -04:00
|
|
|
fprintf(stderr, "%s: names must be fully specified as hostname:port\n", fullname);
|
2013-07-10 17:12:42 -04:00
|
|
|
exit(1);
|
|
|
|
}
|
2013-07-10 17:13:32 -04:00
|
|
|
serv = sep+1;
|
|
|
|
*sep = 0;
|
2013-07-10 17:12:42 -04:00
|
|
|
|
2014-12-22 12:19:02 -05:00
|
|
|
host = fullname;
|
|
|
|
|
|
|
|
/* If it is a RFC-Compliant IPv6 address ("[1234::12]:443"), remove brackets
|
|
|
|
* around IP address */
|
|
|
|
if (host[0] == '[') {
|
|
|
|
end = strrchr(host, ']');
|
|
|
|
if (!end) {
|
|
|
|
fprintf(stderr, "%s: no closing bracket in IPv6 address?\n", host);
|
|
|
|
}
|
|
|
|
host++; /* skip first bracket */
|
|
|
|
*end = 0; /* remove last bracket */
|
|
|
|
}
|
|
|
|
|
2013-07-10 17:15:38 -04:00
|
|
|
res = resolve_split_name(out, host, serv);
|
2013-07-10 17:12:42 -04:00
|
|
|
if (res) {
|
|
|
|
fprintf(stderr, "%s `%s'\n", gai_strerror(res), fullname);
|
|
|
|
if (res == EAI_SERVICE)
|
|
|
|
fprintf(stderr, "(Check you have specified all ports)\n");
|
2013-07-10 17:14:15 -04:00
|
|
|
exit(4);
|
2013-07-10 17:12:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-10 17:14:48 -04:00
|
|
|
/* Log to syslog or stderr if foreground */
|
2013-07-10 17:12:42 -04:00
|
|
|
void log_message(int type, char* msg, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, msg);
|
|
|
|
if (foreground)
|
|
|
|
vfprintf(stderr, msg, ap);
|
2013-07-10 17:14:48 -04:00
|
|
|
else
|
|
|
|
vsyslog(type, msg, ap);
|
2013-07-10 17:12:42 -04:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* syslogs who connected to where */
|
|
|
|
void log_connection(struct connection *cnx)
|
|
|
|
{
|
2013-07-10 17:14:15 -04:00
|
|
|
struct addrinfo addr;
|
|
|
|
struct sockaddr_storage ss;
|
2013-07-10 17:13:32 -04:00
|
|
|
#define MAX_NAMELENGTH (NI_MAXHOST + NI_MAXSERV + 1)
|
2013-07-10 17:14:15 -04:00
|
|
|
char peer[MAX_NAMELENGTH], service[MAX_NAMELENGTH],
|
|
|
|
local[MAX_NAMELENGTH], target[MAX_NAMELENGTH];
|
2013-07-10 17:12:42 -04:00
|
|
|
int res;
|
|
|
|
|
2013-07-10 17:14:15 -04:00
|
|
|
addr.ai_addr = (struct sockaddr*)&ss;
|
|
|
|
addr.ai_addrlen = sizeof(ss);
|
2013-07-10 17:12:42 -04:00
|
|
|
|
2013-07-10 17:14:15 -04:00
|
|
|
res = getpeername(cnx->q[0].fd, addr.ai_addr, &addr.ai_addrlen);
|
2013-08-08 05:27:21 -04:00
|
|
|
if (res == -1) return; /* Can happen if connection drops before we get here.
|
|
|
|
In that case, don't log anything (there is no connection) */
|
2013-07-10 17:14:15 -04:00
|
|
|
sprintaddr(peer, sizeof(peer), &addr);
|
2013-07-10 17:12:42 -04:00
|
|
|
|
2013-07-10 17:14:15 -04:00
|
|
|
addr.ai_addrlen = sizeof(ss);
|
|
|
|
res = getsockname(cnx->q[0].fd, addr.ai_addr, &addr.ai_addrlen);
|
2013-07-10 17:13:32 -04:00
|
|
|
if (res == -1) return;
|
2013-07-10 17:14:15 -04:00
|
|
|
sprintaddr(service, sizeof(service), &addr);
|
2013-07-10 17:13:32 -04:00
|
|
|
|
2013-07-10 17:14:15 -04:00
|
|
|
addr.ai_addrlen = sizeof(ss);
|
|
|
|
res = getpeername(cnx->q[1].fd, addr.ai_addr, &addr.ai_addrlen);
|
2013-07-10 17:13:32 -04:00
|
|
|
if (res == -1) return;
|
2013-07-10 17:14:15 -04:00
|
|
|
sprintaddr(target, sizeof(target), &addr);
|
2013-07-10 17:13:32 -04:00
|
|
|
|
2013-07-10 17:14:15 -04:00
|
|
|
addr.ai_addrlen = sizeof(ss);
|
|
|
|
res = getsockname(cnx->q[1].fd, addr.ai_addr, &addr.ai_addrlen);
|
2013-07-10 17:13:32 -04:00
|
|
|
if (res == -1) return;
|
2013-07-10 17:14:15 -04:00
|
|
|
sprintaddr(local, sizeof(local), &addr);
|
2013-07-10 17:13:32 -04:00
|
|
|
|
2013-07-10 17:14:15 -04:00
|
|
|
log_message(LOG_INFO, "connection from %s to %s forwarded from %s to %s\n",
|
|
|
|
peer,
|
|
|
|
service,
|
|
|
|
local,
|
|
|
|
target);
|
2013-07-10 17:12:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* libwrap (tcpd): check the connection is legal. This is necessary because
|
|
|
|
* the actual server will only see a connection coming from localhost and can't
|
|
|
|
* apply the rules itself.
|
|
|
|
*
|
|
|
|
* Returns -1 if access is denied, 0 otherwise
|
|
|
|
*/
|
2013-07-10 17:15:38 -04:00
|
|
|
int check_access_rights(int in_socket, const char* service)
|
2013-07-10 17:12:42 -04:00
|
|
|
{
|
|
|
|
#ifdef LIBWRAP
|
2015-09-27 15:14:00 -04:00
|
|
|
union {
|
|
|
|
struct sockaddr saddr;
|
|
|
|
struct sockaddr_storage ss;
|
|
|
|
} peer;
|
|
|
|
socklen_t size = sizeof(peer);
|
2013-07-10 17:13:32 -04:00
|
|
|
char addr_str[NI_MAXHOST], host[NI_MAXHOST];
|
2013-07-10 17:12:42 -04:00
|
|
|
int res;
|
|
|
|
|
2015-09-27 15:14:00 -04:00
|
|
|
res = getpeername(in_socket, &peer.saddr, &size);
|
2013-08-08 05:27:21 -04:00
|
|
|
CHECK_RES_RETURN(res, "getpeername");
|
2013-07-10 17:12:42 -04:00
|
|
|
|
2013-07-10 17:13:32 -04:00
|
|
|
/* extract peer address */
|
2015-09-27 15:14:00 -04:00
|
|
|
res = getnameinfo(&peer.saddr, size, addr_str, sizeof(addr_str), NULL, 0, NI_NUMERICHOST);
|
2013-07-10 17:13:32 -04:00
|
|
|
if (res) {
|
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "getnameinfo(NI_NUMERICHOST):%s\n", gai_strerror(res));
|
|
|
|
strcpy(addr_str, STRING_UNKNOWN);
|
|
|
|
}
|
|
|
|
/* extract peer name */
|
|
|
|
strcpy(host, STRING_UNKNOWN);
|
|
|
|
if (!numeric) {
|
2015-09-27 15:14:00 -04:00
|
|
|
res = getnameinfo(&peer.saddr, size, host, sizeof(host), NULL, 0, NI_NAMEREQD);
|
2013-07-10 17:13:32 -04:00
|
|
|
if (res) {
|
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "getnameinfo(NI_NAMEREQD):%s\n", gai_strerror(res));
|
|
|
|
}
|
|
|
|
}
|
2013-07-10 17:12:42 -04:00
|
|
|
|
2013-07-10 17:13:32 -04:00
|
|
|
if (!hosts_ctl(service, host, addr_str, STRING_UNKNOWN)) {
|
2013-07-10 17:12:42 -04:00
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "access denied\n");
|
2013-07-10 17:13:32 -04:00
|
|
|
log_message(LOG_INFO, "connection from %s(%s): access denied", host, addr_str);
|
2013-07-10 17:12:42 -04:00
|
|
|
close(in_socket);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setup_signals(void)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
struct sigaction action;
|
|
|
|
|
|
|
|
/* Request no SIGCHLD is sent upon termination of
|
|
|
|
* the children */
|
|
|
|
memset(&action, 0, sizeof(action));
|
|
|
|
action.sa_handler = NULL;
|
|
|
|
action.sa_flags = SA_NOCLDWAIT;
|
|
|
|
res = sigaction(SIGCHLD, &action, NULL);
|
|
|
|
CHECK_RES_DIE(res, "sigaction");
|
2013-07-10 17:14:15 -04:00
|
|
|
|
2013-09-28 14:49:46 -04:00
|
|
|
/* Set SIGTERM to exit. For some reason if it's not set explicitly,
|
2013-07-10 17:14:15 -04:00
|
|
|
* coverage information is lost when killing the process */
|
|
|
|
memset(&action, 0, sizeof(action));
|
|
|
|
action.sa_handler = exit;
|
|
|
|
res = sigaction(SIGTERM, &action, NULL);
|
|
|
|
CHECK_RES_DIE(res, "sigaction");
|
2013-07-10 17:15:38 -04:00
|
|
|
|
|
|
|
/* Ignore SIGPIPE . */
|
|
|
|
action.sa_handler = SIG_IGN;
|
|
|
|
res = sigaction(SIGPIPE, &action, NULL);
|
|
|
|
CHECK_RES_DIE(res, "sigaction");
|
|
|
|
|
2013-07-10 17:12:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Open syslog connection with appropriate banner;
|
|
|
|
* banner is made up of basename(bin_name)+"[pid]" */
|
2013-07-10 17:15:38 -04:00
|
|
|
void setup_syslog(const char* bin_name) {
|
2013-07-10 17:12:42 -04:00
|
|
|
char *name1, *name2;
|
2013-09-17 00:26:44 -04:00
|
|
|
int res;
|
2013-07-10 17:12:42 -04:00
|
|
|
|
|
|
|
name1 = strdup(bin_name);
|
2013-09-17 00:26:44 -04:00
|
|
|
res = asprintf(&name2, "%s[%d]", basename(name1), getpid());
|
|
|
|
CHECK_RES_DIE(res, "asprintf");
|
2013-07-10 17:12:42 -04:00
|
|
|
openlog(name2, LOG_CONS, LOG_AUTH);
|
|
|
|
free(name1);
|
|
|
|
/* Don't free name2, as openlog(3) uses it (at least in glibc) */
|
|
|
|
|
|
|
|
log_message(LOG_INFO, "%s %s started\n", server_type, VERSION);
|
|
|
|
}
|
|
|
|
|
2013-10-20 14:59:00 -04:00
|
|
|
/* Ask OS to keep capabilities over a setuid(nonzero) */
|
|
|
|
void set_keepcaps(int val) {
|
|
|
|
#ifdef LIBCAP
|
|
|
|
int res;
|
|
|
|
res = prctl(PR_SET_KEEPCAPS, val, 0, 0, 0);
|
|
|
|
if (res) {
|
|
|
|
perror("prctl");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set needed capabilities for effective and permitted, clear rest */
|
|
|
|
void set_capabilities(void) {
|
|
|
|
#ifdef LIBCAP
|
|
|
|
int res;
|
|
|
|
cap_t caps;
|
|
|
|
cap_value_t cap_list[10];
|
|
|
|
int ncap = 0;
|
|
|
|
|
|
|
|
if (transparent)
|
|
|
|
cap_list[ncap++] = CAP_NET_ADMIN;
|
|
|
|
|
|
|
|
caps = cap_init();
|
|
|
|
|
|
|
|
#define _cap_set_flag(flag) do { \
|
|
|
|
res = cap_clear_flag(caps, flag); \
|
|
|
|
CHECK_RES_DIE(res, "cap_clear_flag(" #flag ")"); \
|
|
|
|
if (ncap > 0) { \
|
|
|
|
res = cap_set_flag(caps, flag, ncap, cap_list, CAP_SET); \
|
|
|
|
CHECK_RES_DIE(res, "cap_set_flag(" #flag ")"); \
|
|
|
|
} \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
_cap_set_flag(CAP_EFFECTIVE);
|
|
|
|
_cap_set_flag(CAP_PERMITTED);
|
|
|
|
|
|
|
|
#undef _cap_set_flag
|
|
|
|
|
|
|
|
res = cap_set_proc(caps);
|
|
|
|
CHECK_RES_DIE(res, "cap_set_proc");
|
|
|
|
|
|
|
|
res = cap_free(caps);
|
|
|
|
if (res) {
|
|
|
|
perror("cap_free");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-09-28 14:49:46 -04:00
|
|
|
/* We don't want to run as root -- drop privileges if required */
|
2013-07-10 17:15:38 -04:00
|
|
|
void drop_privileges(const char* user_name)
|
2013-07-10 17:12:42 -04:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
struct passwd *pw = getpwnam(user_name);
|
|
|
|
if (!pw) {
|
|
|
|
fprintf(stderr, "%s: not found\n", user_name);
|
2013-07-10 17:14:15 -04:00
|
|
|
exit(2);
|
2013-07-10 17:12:42 -04:00
|
|
|
}
|
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "turning into %s\n", user_name);
|
|
|
|
|
2013-10-20 14:59:00 -04:00
|
|
|
set_keepcaps(1);
|
|
|
|
|
2014-03-30 11:28:00 -04:00
|
|
|
/* remove extraneous groups in case we belong to several extra groups that
|
|
|
|
* may have unwanted rights. If non-root when calling setgroups(), it
|
|
|
|
* fails, which is fine because... we have no unwanted rights
|
|
|
|
* (see POS36-C for security context)
|
|
|
|
* */
|
|
|
|
setgroups(0, NULL);
|
|
|
|
|
2013-07-10 17:12:42 -04:00
|
|
|
res = setgid(pw->pw_gid);
|
|
|
|
CHECK_RES_DIE(res, "setgid");
|
2013-09-17 00:23:26 -04:00
|
|
|
res = setuid(pw->pw_uid);
|
2013-07-10 17:12:42 -04:00
|
|
|
CHECK_RES_DIE(res, "setuid");
|
2013-10-20 14:59:00 -04:00
|
|
|
|
|
|
|
set_capabilities();
|
|
|
|
set_keepcaps(0);
|
2013-07-10 17:12:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Writes my PID */
|
2013-07-10 17:15:38 -04:00
|
|
|
void write_pid_file(const char* pidfile)
|
2013-07-10 17:12:42 -04:00
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
f = fopen(pidfile, "w");
|
|
|
|
if (!f) {
|
|
|
|
perror(pidfile);
|
2013-07-10 17:14:15 -04:00
|
|
|
exit(3);
|
2013-07-10 17:12:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(f, "%d\n", getpid());
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|