2008-04-02 15:37:50 +00:00
|
|
|
/*
|
|
|
|
* ircd-ratbox: A slightly useful ircd.
|
|
|
|
* listener.c: Listens on a port.
|
|
|
|
*
|
|
|
|
* Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
|
|
|
|
* Copyright (C) 1996-2002 Hybrid Development Team
|
|
|
|
* Copyright (C) 2002-2005 ircd-ratbox development team
|
2021-06-19 10:51:50 +00:00
|
|
|
* Copyright (C) 2021 Ariadne Conill
|
2008-04-02 15:37:50 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stdinc.h"
|
|
|
|
#include "setup.h"
|
|
|
|
#include "listener.h"
|
|
|
|
#include "client.h"
|
2008-04-20 05:47:38 +00:00
|
|
|
#include "match.h"
|
2008-04-02 15:37:50 +00:00
|
|
|
#include "ircd.h"
|
|
|
|
#include "ircd_defs.h"
|
|
|
|
#include "numeric.h"
|
|
|
|
#include "s_conf.h"
|
|
|
|
#include "s_newconf.h"
|
|
|
|
#include "s_stats.h"
|
|
|
|
#include "send.h"
|
2016-04-02 21:44:04 +00:00
|
|
|
#include "authproc.h"
|
2008-04-02 15:37:50 +00:00
|
|
|
#include "reject.h"
|
|
|
|
#include "hostmask.h"
|
2008-04-06 14:52:42 +00:00
|
|
|
#include "sslproc.h"
|
2016-04-02 07:56:03 +00:00
|
|
|
#include "wsproc.h"
|
2008-04-06 14:52:42 +00:00
|
|
|
#include "hash.h"
|
2013-09-10 05:35:56 +00:00
|
|
|
#include "s_assert.h"
|
|
|
|
#include "logger.h"
|
2008-04-02 15:37:50 +00:00
|
|
|
|
2021-06-19 10:51:50 +00:00
|
|
|
static rb_dlink_list listener_list = {};
|
2008-04-02 18:50:20 +00:00
|
|
|
static int accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data);
|
2008-04-02 16:40:38 +00:00
|
|
|
static void accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data);
|
2016-04-25 20:35:58 +00:00
|
|
|
static SSL_OPEN_CB accept_sslcallback;
|
2008-04-02 15:37:50 +00:00
|
|
|
|
2010-01-30 14:57:10 +00:00
|
|
|
static struct Listener *
|
2008-04-02 15:57:37 +00:00
|
|
|
make_listener(struct rb_sockaddr_storage *addr)
|
2008-04-02 15:37:50 +00:00
|
|
|
{
|
2010-01-30 14:57:10 +00:00
|
|
|
struct Listener *listener = (struct Listener *) rb_malloc(sizeof(struct Listener));
|
2008-04-02 15:37:50 +00:00
|
|
|
s_assert(0 != listener);
|
|
|
|
listener->name = me.name;
|
2008-04-02 16:06:11 +00:00
|
|
|
listener->F = NULL;
|
|
|
|
|
2017-08-19 14:11:49 +00:00
|
|
|
memcpy(&listener->addr, addr, sizeof(listener->addr));
|
2008-04-02 15:37:50 +00:00
|
|
|
return listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-01-30 14:57:10 +00:00
|
|
|
free_listener(struct Listener *listener)
|
2008-04-02 15:37:50 +00:00
|
|
|
{
|
|
|
|
s_assert(NULL != listener);
|
|
|
|
if(listener == NULL)
|
|
|
|
return;
|
|
|
|
|
2021-06-19 10:51:50 +00:00
|
|
|
rb_dlinkDelete(&listener->lnode, &listener_list);
|
2008-04-02 15:37:50 +00:00
|
|
|
rb_free(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define PORTNAMELEN 6 /* ":31337" */
|
|
|
|
|
2014-03-01 18:05:55 +00:00
|
|
|
/*
|
|
|
|
* get_listener_port - return displayable listener port
|
|
|
|
*/
|
|
|
|
static uint16_t
|
|
|
|
get_listener_port(const struct Listener *listener)
|
|
|
|
{
|
2017-08-19 14:11:49 +00:00
|
|
|
return ntohs(GET_SS_PORT(&listener->addr[0]));
|
2014-03-01 18:05:55 +00:00
|
|
|
}
|
|
|
|
|
2008-04-02 15:37:50 +00:00
|
|
|
/*
|
|
|
|
* get_listener_name - return displayable listener name and port
|
|
|
|
*/
|
|
|
|
const char *
|
2010-01-30 14:57:10 +00:00
|
|
|
get_listener_name(const struct Listener *listener)
|
2008-04-02 15:37:50 +00:00
|
|
|
{
|
2017-08-19 14:11:49 +00:00
|
|
|
static char buf[BUFSIZE];
|
2008-04-02 15:37:50 +00:00
|
|
|
|
2016-02-10 01:25:32 +00:00
|
|
|
snprintf(buf, sizeof(buf), "%s[%s/%u]",
|
2014-03-01 18:05:55 +00:00
|
|
|
me.name, listener->name, get_listener_port(listener));
|
2008-04-02 15:37:50 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* show_ports - send port listing to a client
|
|
|
|
* inputs - pointer to client to show ports to
|
|
|
|
* output - none
|
|
|
|
* side effects - show ports
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
show_ports(struct Client *source_p)
|
|
|
|
{
|
2021-06-19 10:51:50 +00:00
|
|
|
rb_dlink_node *n;
|
2008-04-02 15:37:50 +00:00
|
|
|
|
2021-06-19 10:51:50 +00:00
|
|
|
RB_DLINK_FOREACH(n, listener_list.head)
|
2008-04-02 15:37:50 +00:00
|
|
|
{
|
2021-06-19 10:51:50 +00:00
|
|
|
struct Listener *listener = n->data;
|
|
|
|
|
2014-03-03 04:25:47 +00:00
|
|
|
sendto_one_numeric(source_p, RPL_STATSPLINE,
|
2014-03-01 18:05:55 +00:00
|
|
|
form_str(RPL_STATSPLINE), 'P',
|
|
|
|
get_listener_port(listener),
|
2008-04-02 15:37:50 +00:00
|
|
|
IsOperAdmin(source_p) ? listener->name : me.name,
|
2008-04-13 15:54:23 +00:00
|
|
|
listener->ref_count, (listener->active) ? "active" : "disabled",
|
2017-08-19 14:11:49 +00:00
|
|
|
listener->sctp ? " sctp" : " tcp",
|
2008-04-06 14:52:42 +00:00
|
|
|
listener->ssl ? " ssl" : "");
|
2008-04-02 15:37:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* inetport - create a listener socket in the AF_INET or AF_INET6 domain,
|
|
|
|
* bind it to the port given in 'port' and listen to it
|
|
|
|
* returns true (1) if successful false (0) on error.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
2010-01-30 14:57:10 +00:00
|
|
|
inetport(struct Listener *listener)
|
2008-04-02 15:37:50 +00:00
|
|
|
{
|
2008-04-02 16:06:11 +00:00
|
|
|
rb_fde_t *F;
|
2014-03-01 18:05:55 +00:00
|
|
|
const char *errstr;
|
2017-08-19 14:11:49 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (listener->sctp) {
|
|
|
|
#ifdef HAVE_LIBSCTP
|
|
|
|
/* only AF_INET6 sockets can have both AF_INET and AF_INET6 addresses */
|
|
|
|
F = rb_socket(AF_INET6, SOCK_STREAM, IPPROTO_SCTP, "Listener socket");
|
|
|
|
#else
|
|
|
|
F = NULL;
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
F = rb_socket(GET_SS_FAMILY(&listener->addr[0]), SOCK_STREAM, IPPROTO_TCP, "Listener socket");
|
|
|
|
}
|
2008-04-02 15:37:50 +00:00
|
|
|
|
2017-08-19 14:11:49 +00:00
|
|
|
memset(listener->vhost, 0, sizeof(listener->vhost));
|
2014-03-03 04:25:47 +00:00
|
|
|
|
2017-08-19 14:11:49 +00:00
|
|
|
if (GET_SS_FAMILY(&listener->addr[0]) == AF_INET6) {
|
|
|
|
struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&listener->addr[0];
|
|
|
|
rb_inet_ntop(AF_INET6, &in6->sin6_addr, listener->vhost, sizeof(listener->vhost));
|
|
|
|
} else if (GET_SS_FAMILY(&listener->addr[0]) == AF_INET) {
|
|
|
|
struct sockaddr_in *in = (struct sockaddr_in *)&listener->addr[0];
|
|
|
|
rb_inet_ntop(AF_INET, &in->sin_addr, listener->vhost, sizeof(listener->vhost));
|
|
|
|
}
|
2008-04-02 15:37:50 +00:00
|
|
|
|
2017-08-19 14:11:49 +00:00
|
|
|
if (GET_SS_FAMILY(&listener->addr[1]) == AF_INET6) {
|
|
|
|
struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&listener->addr[1];
|
|
|
|
strncat(listener->vhost, "&", sizeof(listener->vhost));
|
|
|
|
rb_inet_ntop(AF_INET6, &in6->sin6_addr, &listener->vhost[strlen(listener->vhost)], sizeof(listener->vhost) - strlen(listener->vhost));
|
|
|
|
} else if (GET_SS_FAMILY(&listener->addr[1]) == AF_INET) {
|
|
|
|
struct sockaddr_in *in = (struct sockaddr_in *)&listener->addr[1];
|
|
|
|
strncat(listener->vhost, "&", sizeof(listener->vhost));
|
|
|
|
rb_inet_ntop(AF_INET, &in->sin_addr, &listener->vhost[strlen(listener->vhost)], sizeof(listener->vhost) - strlen(listener->vhost));
|
2008-04-02 15:37:50 +00:00
|
|
|
}
|
|
|
|
|
2017-08-19 14:11:49 +00:00
|
|
|
if (listener->vhost[0] != '\0') {
|
|
|
|
listener->name = listener->vhost;
|
2008-04-02 18:50:20 +00:00
|
|
|
}
|
2017-08-19 14:11:49 +00:00
|
|
|
|
|
|
|
if (F == NULL) {
|
2020-11-08 19:30:41 +00:00
|
|
|
sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
|
2017-08-19 14:11:49 +00:00
|
|
|
"Cannot open socket for listener on %s port %d",
|
|
|
|
listener->sctp ? "SCTP" : "TCP",
|
2014-03-01 18:05:55 +00:00
|
|
|
get_listener_port(listener));
|
2017-08-19 14:11:49 +00:00
|
|
|
ilog(L_MAIN, "Cannot open socket for %s listener %s",
|
|
|
|
listener->sctp ? "SCTP" : "TCP",
|
2014-03-01 18:05:55 +00:00
|
|
|
get_listener_name(listener));
|
2008-04-02 18:50:20 +00:00
|
|
|
return 0;
|
2008-04-02 15:37:50 +00:00
|
|
|
}
|
|
|
|
|
2017-08-19 14:11:49 +00:00
|
|
|
if (listener->sctp) {
|
|
|
|
ret = rb_sctp_bindx(F, listener->addr, ARRAY_SIZE(listener->addr));
|
|
|
|
} else {
|
|
|
|
ret = rb_bind(F, (struct sockaddr *)&listener->addr[0]);
|
2008-04-02 15:37:50 +00:00
|
|
|
}
|
|
|
|
|
2017-08-19 14:11:49 +00:00
|
|
|
if (ret) {
|
2014-03-01 18:05:55 +00:00
|
|
|
errstr = strerror(rb_get_sockerr(F));
|
2020-11-08 19:30:41 +00:00
|
|
|
sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
|
2017-08-19 14:11:49 +00:00
|
|
|
"Cannot bind for listener on %s port %d: %s",
|
|
|
|
listener->sctp ? "SCTP" : "TCP",
|
2014-03-01 18:05:55 +00:00
|
|
|
get_listener_port(listener), errstr);
|
2017-08-19 14:11:49 +00:00
|
|
|
ilog(L_MAIN, "Cannot bind for %s listener %s: %s",
|
|
|
|
listener->sctp ? "SCTP" : "TCP",
|
2014-03-01 18:05:55 +00:00
|
|
|
get_listener_name(listener), errstr);
|
2008-04-02 18:50:20 +00:00
|
|
|
rb_close(F);
|
|
|
|
return 0;
|
2008-04-02 15:37:50 +00:00
|
|
|
}
|
|
|
|
|
2016-04-07 09:47:48 +00:00
|
|
|
if(rb_listen(F, SOMAXCONN, listener->defer_accept))
|
2008-04-02 18:50:20 +00:00
|
|
|
{
|
2014-03-01 18:05:55 +00:00
|
|
|
errstr = strerror(rb_get_sockerr(F));
|
2020-11-08 19:30:41 +00:00
|
|
|
sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
|
2017-08-19 14:11:49 +00:00
|
|
|
"Cannot listen() for listener on %s port %d: %s",
|
|
|
|
listener->sctp ? "SCTP" : "TCP",
|
2014-03-01 18:05:55 +00:00
|
|
|
get_listener_port(listener), errstr);
|
2017-08-19 14:11:49 +00:00
|
|
|
ilog(L_MAIN, "Cannot listen() for %s listener %s: %s",
|
|
|
|
listener->sctp ? "SCTP" : "TCP",
|
2014-03-01 18:05:55 +00:00
|
|
|
get_listener_name(listener), errstr);
|
2008-04-02 18:50:20 +00:00
|
|
|
rb_close(F);
|
|
|
|
return 0;
|
2008-04-02 15:37:50 +00:00
|
|
|
}
|
|
|
|
|
2008-04-02 18:50:20 +00:00
|
|
|
listener->F = F;
|
|
|
|
|
2008-04-02 16:06:11 +00:00
|
|
|
rb_accept_tcp(listener->F, accept_precallback, accept_callback, listener);
|
2008-04-02 15:37:50 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-01-30 14:57:10 +00:00
|
|
|
static struct Listener *
|
2017-08-19 14:11:49 +00:00
|
|
|
find_listener(struct rb_sockaddr_storage *addr, int sctp)
|
2008-04-02 15:37:50 +00:00
|
|
|
{
|
2021-06-19 10:51:50 +00:00
|
|
|
rb_dlink_node *n;
|
2010-01-30 14:57:10 +00:00
|
|
|
struct Listener *last_closed = NULL;
|
2008-04-02 15:37:50 +00:00
|
|
|
|
2021-06-19 10:51:50 +00:00
|
|
|
RB_DLINK_FOREACH(n, listener_list.head) {
|
|
|
|
struct Listener *listener = n->data;
|
|
|
|
|
2017-08-19 14:11:49 +00:00
|
|
|
if (listener->sctp != sctp)
|
2008-04-02 15:37:50 +00:00
|
|
|
continue;
|
2014-03-03 04:25:47 +00:00
|
|
|
|
2017-08-19 14:11:49 +00:00
|
|
|
for (int i = 0; i < ARRAY_SIZE(listener->addr); i++) {
|
|
|
|
if (GET_SS_FAMILY(&addr[i]) != GET_SS_FAMILY(&listener->addr[i]))
|
|
|
|
goto next;
|
|
|
|
|
|
|
|
switch (GET_SS_FAMILY(&addr[i])) {
|
|
|
|
case AF_INET:
|
2008-04-02 15:37:50 +00:00
|
|
|
{
|
2017-08-19 14:11:49 +00:00
|
|
|
struct sockaddr_in *in4 = (struct sockaddr_in *)&addr[i];
|
|
|
|
struct sockaddr_in *lin4 = (struct sockaddr_in *)&listener->addr[i];
|
|
|
|
if(in4->sin_addr.s_addr != lin4->sin_addr.s_addr ||
|
|
|
|
in4->sin_port != lin4->sin_port)
|
|
|
|
{
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
break;
|
2008-04-02 15:37:50 +00:00
|
|
|
}
|
2017-08-19 14:11:49 +00:00
|
|
|
|
|
|
|
case AF_INET6:
|
2008-04-02 15:37:50 +00:00
|
|
|
{
|
2017-08-19 14:11:49 +00:00
|
|
|
struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&addr[i];
|
|
|
|
struct sockaddr_in6 *lin6 =(struct sockaddr_in6 *)&listener->addr[i];
|
|
|
|
if (!IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &lin6->sin6_addr) ||
|
|
|
|
in6->sin6_port != lin6->sin6_port)
|
|
|
|
{
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
break;
|
2008-04-02 15:37:50 +00:00
|
|
|
}
|
2014-03-03 04:25:47 +00:00
|
|
|
|
2017-08-19 14:11:49 +00:00
|
|
|
default:
|
|
|
|
break;
|
2008-04-02 15:37:50 +00:00
|
|
|
}
|
2017-08-19 14:11:49 +00:00
|
|
|
}
|
2008-04-02 15:37:50 +00:00
|
|
|
|
2017-08-19 14:11:49 +00:00
|
|
|
if (listener->F == NULL) {
|
|
|
|
last_closed = listener;
|
|
|
|
} else {
|
|
|
|
return listener;
|
2008-04-02 15:37:50 +00:00
|
|
|
}
|
2017-08-19 14:11:49 +00:00
|
|
|
|
|
|
|
next:
|
|
|
|
continue;
|
2008-04-02 15:37:50 +00:00
|
|
|
}
|
|
|
|
return last_closed;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2017-08-19 14:11:49 +00:00
|
|
|
* add_tcp_listener- create a new listener
|
2008-04-02 15:37:50 +00:00
|
|
|
* port - the port number to listen on
|
|
|
|
* vhost_ip - if non-null must contain a valid IP address string in
|
|
|
|
* the format "255.255.255.255"
|
|
|
|
*/
|
|
|
|
void
|
2017-08-19 14:11:49 +00:00
|
|
|
add_tcp_listener(int port, const char *vhost_ip, int family, int ssl, int defer_accept, int wsock)
|
2008-04-02 15:37:50 +00:00
|
|
|
{
|
2010-01-30 14:57:10 +00:00
|
|
|
struct Listener *listener;
|
2017-08-19 14:11:49 +00:00
|
|
|
struct rb_sockaddr_storage vaddr[ARRAY_SIZE(listener->addr)];
|
2008-04-02 15:37:50 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* if no port in conf line, don't bother
|
|
|
|
*/
|
2017-08-19 14:11:49 +00:00
|
|
|
if (port == 0)
|
2008-04-02 15:37:50 +00:00
|
|
|
return;
|
|
|
|
memset(&vaddr, 0, sizeof(vaddr));
|
2017-08-19 14:11:49 +00:00
|
|
|
SET_SS_FAMILY(&vaddr[0], AF_UNSPEC);
|
|
|
|
SET_SS_LEN(&vaddr[0], sizeof(struct sockaddr_storage));
|
|
|
|
SET_SS_FAMILY(&vaddr[1], AF_UNSPEC);
|
|
|
|
SET_SS_LEN(&vaddr[1], sizeof(struct sockaddr_storage));
|
|
|
|
|
|
|
|
if (vhost_ip != NULL) {
|
|
|
|
if (family == AF_INET) {
|
|
|
|
if (rb_inet_pton(family, vhost_ip, &((struct sockaddr_in *)&vaddr[0])->sin_addr) <= 0)
|
2008-04-02 15:37:50 +00:00
|
|
|
return;
|
2017-08-19 14:11:49 +00:00
|
|
|
} else {
|
|
|
|
if (rb_inet_pton(family, vhost_ip, &((struct sockaddr_in6 *)&vaddr[0])->sin6_addr) <= 0)
|
2008-04-02 15:37:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-08-19 14:11:49 +00:00
|
|
|
} else {
|
|
|
|
switch(family) {
|
2008-04-02 15:37:50 +00:00
|
|
|
case AF_INET:
|
2017-08-19 14:11:49 +00:00
|
|
|
((struct sockaddr_in *)&vaddr[0])->sin_addr.s_addr = INADDR_ANY;
|
2008-04-02 15:37:50 +00:00
|
|
|
break;
|
|
|
|
case AF_INET6:
|
2017-08-19 14:11:49 +00:00
|
|
|
memcpy(&((struct sockaddr_in6 *)&vaddr[0])->sin6_addr, &in6addr_any, sizeof(struct in6_addr));
|
2008-04-02 15:37:50 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
2014-03-03 04:25:47 +00:00
|
|
|
}
|
2008-04-02 15:37:50 +00:00
|
|
|
}
|
2017-08-19 14:11:49 +00:00
|
|
|
switch(family) {
|
2008-04-02 15:37:50 +00:00
|
|
|
case AF_INET:
|
2017-08-19 14:11:49 +00:00
|
|
|
SET_SS_LEN(&vaddr[0], sizeof(struct sockaddr_in));
|
|
|
|
SET_SS_FAMILY(&vaddr[0], AF_INET);
|
|
|
|
SET_SS_PORT(&vaddr[0], htons(port));
|
2008-04-02 15:37:50 +00:00
|
|
|
break;
|
|
|
|
case AF_INET6:
|
2017-08-19 14:11:49 +00:00
|
|
|
SET_SS_LEN(&vaddr[0], sizeof(struct sockaddr_in6));
|
|
|
|
SET_SS_FAMILY(&vaddr[0], AF_INET6);
|
|
|
|
SET_SS_PORT(&vaddr[0], htons(port));
|
2008-04-02 15:37:50 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-08-19 14:11:49 +00:00
|
|
|
if ((listener = find_listener(vaddr, 0))) {
|
|
|
|
if (listener->F != NULL)
|
2008-04-02 15:37:50 +00:00
|
|
|
return;
|
2017-08-19 14:11:49 +00:00
|
|
|
} else {
|
|
|
|
listener = make_listener(vaddr);
|
2021-06-19 10:51:50 +00:00
|
|
|
rb_dlinkAdd(listener, &listener->lnode, &listener_list);
|
2008-04-02 15:37:50 +00:00
|
|
|
}
|
|
|
|
|
2008-04-02 16:06:11 +00:00
|
|
|
listener->F = NULL;
|
2008-04-06 14:52:42 +00:00
|
|
|
listener->ssl = ssl;
|
2012-03-17 15:00:39 +00:00
|
|
|
listener->defer_accept = defer_accept;
|
2017-08-19 14:11:49 +00:00
|
|
|
listener->sctp = 0;
|
2016-04-02 07:56:03 +00:00
|
|
|
listener->wsock = wsock;
|
2008-04-02 15:37:50 +00:00
|
|
|
|
2017-08-19 14:11:49 +00:00
|
|
|
if (inetport(listener)) {
|
2008-04-02 15:37:50 +00:00
|
|
|
listener->active = 1;
|
2017-08-19 14:11:49 +00:00
|
|
|
} else {
|
2008-04-02 15:37:50 +00:00
|
|
|
close_listener(listener);
|
2017-08-19 14:11:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* add_sctp_listener- create a new listener
|
|
|
|
* port - the port number to listen on
|
|
|
|
* vhost_ip1/2 - if non-null must contain a valid IP address string
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
add_sctp_listener(int port, const char *vhost_ip1, const char *vhost_ip2, int ssl, int wsock)
|
|
|
|
{
|
|
|
|
struct Listener *listener;
|
|
|
|
struct rb_sockaddr_storage vaddr[ARRAY_SIZE(listener->addr)];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if no port in conf line, don't bother
|
|
|
|
*/
|
|
|
|
if (port == 0)
|
|
|
|
return;
|
|
|
|
memset(&vaddr, 0, sizeof(vaddr));
|
|
|
|
|
|
|
|
if (vhost_ip1 != NULL) {
|
2019-08-31 15:10:50 +00:00
|
|
|
if (rb_inet_pton_sock(vhost_ip1, &vaddr[0]) <= 0)
|
2017-08-19 14:11:49 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (vhost_ip2 != NULL) {
|
2019-08-31 15:10:50 +00:00
|
|
|
if (rb_inet_pton_sock(vhost_ip2, &vaddr[1]) <= 0)
|
2017-08-19 14:11:49 +00:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
SET_SS_FAMILY(&vaddr[1], AF_UNSPEC);
|
|
|
|
SET_SS_LEN(&vaddr[1], sizeof(struct sockaddr_storage));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GET_SS_FAMILY(&vaddr[0]) == AF_INET && GET_SS_FAMILY(&vaddr[1]) == AF_INET6) {
|
|
|
|
/* always put INET6 first */
|
|
|
|
struct rb_sockaddr_storage tmp;
|
|
|
|
tmp = vaddr[0];
|
|
|
|
vaddr[0] = vaddr[1];
|
|
|
|
vaddr[1] = tmp;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
memcpy(&((struct sockaddr_in6 *)&vaddr[0])->sin6_addr, &in6addr_any, sizeof(struct in6_addr));
|
|
|
|
SET_SS_FAMILY(&vaddr[0], AF_INET6);
|
|
|
|
SET_SS_LEN(&vaddr[0], sizeof(struct sockaddr_in6));
|
|
|
|
|
|
|
|
SET_SS_FAMILY(&vaddr[1], AF_UNSPEC);
|
|
|
|
SET_SS_LEN(&vaddr[1], sizeof(struct sockaddr_storage));
|
|
|
|
}
|
|
|
|
|
|
|
|
SET_SS_PORT(&vaddr[0], htons(port));
|
|
|
|
SET_SS_PORT(&vaddr[1], htons(port));
|
|
|
|
|
|
|
|
if ((listener = find_listener(vaddr, 1))) {
|
|
|
|
if(listener->F != NULL)
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
listener = make_listener(vaddr);
|
2021-06-19 10:51:50 +00:00
|
|
|
rb_dlinkAdd(listener, &listener->lnode, &listener_list);
|
2017-08-19 14:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
listener->F = NULL;
|
|
|
|
listener->ssl = ssl;
|
|
|
|
listener->defer_accept = 0;
|
|
|
|
listener->sctp = 1;
|
|
|
|
listener->wsock = wsock;
|
|
|
|
|
|
|
|
if (inetport(listener)) {
|
|
|
|
listener->active = 1;
|
|
|
|
} else {
|
|
|
|
close_listener(listener);
|
|
|
|
}
|
2008-04-02 15:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* close_listener - close a single listener
|
|
|
|
*/
|
|
|
|
void
|
2010-01-30 14:57:10 +00:00
|
|
|
close_listener(struct Listener *listener)
|
2008-04-02 15:37:50 +00:00
|
|
|
{
|
|
|
|
s_assert(listener != NULL);
|
|
|
|
if(listener == NULL)
|
|
|
|
return;
|
2008-04-02 18:50:20 +00:00
|
|
|
if(listener->F != NULL)
|
|
|
|
{
|
|
|
|
rb_close(listener->F);
|
|
|
|
listener->F = NULL;
|
2008-04-02 15:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
listener->active = 0;
|
|
|
|
|
|
|
|
if(listener->ref_count)
|
|
|
|
return;
|
|
|
|
|
|
|
|
free_listener(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* close_listeners - close and free all listeners that are not being used
|
|
|
|
*/
|
|
|
|
void
|
2021-06-19 10:51:50 +00:00
|
|
|
close_listeners(void)
|
2008-04-02 15:37:50 +00:00
|
|
|
{
|
2021-06-19 10:51:50 +00:00
|
|
|
rb_dlink_node *n, *tn;
|
|
|
|
|
2008-04-02 15:37:50 +00:00
|
|
|
/*
|
|
|
|
* close all 'extra' listening ports we have
|
|
|
|
*/
|
2021-06-19 10:51:50 +00:00
|
|
|
RB_DLINK_FOREACH_SAFE(n, tn, listener_list.head)
|
2008-04-02 15:37:50 +00:00
|
|
|
{
|
2021-06-19 10:51:50 +00:00
|
|
|
struct Listener *listener = n->data;
|
|
|
|
|
2008-04-02 15:37:50 +00:00
|
|
|
close_listener(listener);
|
|
|
|
}
|
2021-06-19 10:51:50 +00:00
|
|
|
|
2021-06-19 10:41:37 +00:00
|
|
|
rb_close_pending_fds();
|
2008-04-02 15:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-03-03 04:25:47 +00:00
|
|
|
* add_connection - creates a client which has just connected to us on
|
2008-04-02 15:37:50 +00:00
|
|
|
* the given fd. The sockhost field is initialized with the ip# of the host.
|
|
|
|
* The client is sent to the auth module for verification, and not put in
|
|
|
|
* any client list yet.
|
|
|
|
*/
|
2008-04-02 18:50:20 +00:00
|
|
|
static void
|
2015-12-12 11:20:51 +00:00
|
|
|
add_connection(struct Listener *listener, rb_fde_t *F, struct sockaddr *sai, struct sockaddr *lai)
|
2008-04-02 15:37:50 +00:00
|
|
|
{
|
|
|
|
struct Client *new_client;
|
2016-04-25 20:35:58 +00:00
|
|
|
bool defer = false;
|
2008-04-02 15:37:50 +00:00
|
|
|
s_assert(NULL != listener);
|
|
|
|
|
2014-03-03 04:25:47 +00:00
|
|
|
/*
|
2008-04-02 15:37:50 +00:00
|
|
|
* get the client socket name from the socket
|
|
|
|
* the client has already been checked out in accept_connection
|
|
|
|
*/
|
|
|
|
new_client = make_client(NULL);
|
2016-11-20 20:43:53 +00:00
|
|
|
new_client->localClient->F = F;
|
|
|
|
|
|
|
|
memcpy(&new_client->localClient->ip, sai, sizeof(struct rb_sockaddr_storage));
|
|
|
|
memcpy(&new_client->preClient->lip, lai, sizeof(struct rb_sockaddr_storage));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* copy address to 'sockhost' as a string, copy it to host too
|
|
|
|
* so we have something valid to put into error messages...
|
|
|
|
*/
|
|
|
|
rb_inet_ntop_sock((struct sockaddr *)&new_client->localClient->ip, new_client->sockhost,
|
|
|
|
sizeof(new_client->sockhost));
|
|
|
|
|
|
|
|
rb_strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host));
|
2008-04-02 15:37:50 +00:00
|
|
|
|
2017-08-19 14:11:49 +00:00
|
|
|
if (listener->sctp) {
|
|
|
|
SetSCTP(new_client);
|
|
|
|
}
|
|
|
|
|
2015-12-12 11:20:51 +00:00
|
|
|
if (listener->ssl)
|
|
|
|
{
|
|
|
|
rb_fde_t *xF[2];
|
|
|
|
if(rb_socketpair(AF_UNIX, SOCK_STREAM, 0, &xF[0], &xF[1], "Incoming ssld Connection") == -1)
|
|
|
|
{
|
2016-11-20 20:43:53 +00:00
|
|
|
SetIOError(new_client);
|
|
|
|
exit_client(new_client, new_client, new_client, "Fatal Error");
|
2015-12-12 11:20:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-04-25 20:35:58 +00:00
|
|
|
new_client->localClient->ssl_callback = accept_sslcallback;
|
|
|
|
defer = true;
|
2016-03-26 00:49:01 +00:00
|
|
|
new_client->localClient->ssl_ctl = start_ssld_accept(F, xF[1], connid_get(new_client)); /* this will close F for us */
|
2015-12-12 11:20:51 +00:00
|
|
|
if(new_client->localClient->ssl_ctl == NULL)
|
|
|
|
{
|
2016-11-20 20:43:53 +00:00
|
|
|
SetIOError(new_client);
|
|
|
|
exit_client(new_client, new_client, new_client, "Service Unavailable");
|
2015-12-12 11:20:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
F = xF[0];
|
2016-11-20 20:43:53 +00:00
|
|
|
new_client->localClient->F = F;
|
2015-12-12 11:20:51 +00:00
|
|
|
SetSSL(new_client);
|
2020-10-28 15:28:51 +00:00
|
|
|
SetSecure(new_client);
|
2015-12-12 11:20:51 +00:00
|
|
|
}
|
2020-10-28 20:55:35 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
struct ConfItem *aconf;
|
|
|
|
aconf = find_conf_by_address(NULL, NULL, NULL, sai, CONF_SECURE | 1, sai->sa_family, NULL, NULL);
|
|
|
|
|
|
|
|
if (aconf != NULL)
|
|
|
|
SetSecure(new_client);
|
|
|
|
}
|
2015-12-12 11:20:51 +00:00
|
|
|
|
2016-04-02 07:56:03 +00:00
|
|
|
if (listener->wsock)
|
|
|
|
{
|
|
|
|
rb_fde_t *xF[2];
|
|
|
|
if(rb_socketpair(AF_UNIX, SOCK_STREAM, 0, &xF[0], &xF[1], "Incoming wsockd Connection") == -1)
|
|
|
|
{
|
2016-11-20 20:43:53 +00:00
|
|
|
SetIOError(new_client);
|
|
|
|
exit_client(new_client, new_client, new_client, "Fatal Error");
|
2016-04-02 07:56:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
new_client->localClient->ws_ctl = start_wsockd_accept(F, xF[1], connid_get(new_client)); /* this will close F for us */
|
|
|
|
if(new_client->localClient->ws_ctl == NULL)
|
|
|
|
{
|
2016-11-20 20:43:53 +00:00
|
|
|
SetIOError(new_client);
|
|
|
|
exit_client(new_client, new_client, new_client, "Service Unavailable");
|
2016-04-02 07:56:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
F = xF[0];
|
2016-11-20 20:43:53 +00:00
|
|
|
new_client->localClient->F = F;
|
2016-04-02 07:56:03 +00:00
|
|
|
}
|
|
|
|
|
2008-04-02 15:37:50 +00:00
|
|
|
new_client->localClient->listener = listener;
|
2008-04-06 14:52:42 +00:00
|
|
|
|
2008-04-02 15:37:50 +00:00
|
|
|
++listener->ref_count;
|
|
|
|
|
2016-04-25 20:35:58 +00:00
|
|
|
authd_initiate_client(new_client, defer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
accept_sslcallback(struct Client *client_p, int status)
|
|
|
|
{
|
|
|
|
authd_deferred_client(client_p);
|
|
|
|
return 0; /* use default handler if status != RB_OK */
|
2008-04-02 15:37:50 +00:00
|
|
|
}
|
2008-04-02 16:39:36 +00:00
|
|
|
|
2008-07-31 23:59:08 +00:00
|
|
|
static const char *toofast = "ERROR :Reconnecting too fast, throttled.\r\n";
|
|
|
|
|
2008-04-02 18:50:20 +00:00
|
|
|
static int
|
|
|
|
accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data)
|
|
|
|
{
|
|
|
|
struct Listener *listener = (struct Listener *)data;
|
|
|
|
char buf[BUFSIZE];
|
|
|
|
struct ConfItem *aconf;
|
|
|
|
static time_t last_oper_notice = 0;
|
2010-02-27 23:46:56 +00:00
|
|
|
int len;
|
2008-04-02 18:50:20 +00:00
|
|
|
|
2016-03-19 05:57:32 +00:00
|
|
|
if(listener->ssl && (!ircd_ssl_ok || !get_ssld_count()))
|
2008-04-13 15:54:23 +00:00
|
|
|
{
|
|
|
|
rb_close(F);
|
|
|
|
return 0;
|
2008-04-06 14:52:42 +00:00
|
|
|
}
|
|
|
|
|
2008-04-02 18:50:20 +00:00
|
|
|
if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus */
|
|
|
|
{
|
2008-04-04 15:54:37 +00:00
|
|
|
++ServerStats.is_ref;
|
2008-04-02 18:50:20 +00:00
|
|
|
/*
|
|
|
|
* slow down the whining to opers bit
|
|
|
|
*/
|
|
|
|
if((last_oper_notice + 20) <= rb_current_time())
|
|
|
|
{
|
2020-11-08 19:30:41 +00:00
|
|
|
sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
|
2008-04-02 18:50:20 +00:00
|
|
|
"All connections in use. (%s)",
|
|
|
|
get_listener_name(listener));
|
|
|
|
last_oper_notice = rb_current_time();
|
|
|
|
}
|
2014-03-03 04:25:47 +00:00
|
|
|
|
2014-11-04 20:57:37 +00:00
|
|
|
rb_write(F, "ERROR :All connections in use\r\n", 31);
|
2008-04-02 18:50:20 +00:00
|
|
|
rb_close(F);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-07-31 19:56:05 +00:00
|
|
|
aconf = find_dline(addr, addr->sa_family);
|
2008-04-02 18:50:20 +00:00
|
|
|
if(aconf != NULL && (aconf->status & CONF_EXEMPTDLINE))
|
|
|
|
return 1;
|
2014-03-03 04:25:47 +00:00
|
|
|
|
2008-04-02 18:50:20 +00:00
|
|
|
/* Do an initial check we aren't connecting too fast or with too many
|
|
|
|
* from this IP... */
|
|
|
|
if(aconf != NULL)
|
|
|
|
{
|
2008-04-04 15:54:37 +00:00
|
|
|
ServerStats.is_ref++;
|
2014-03-03 04:25:47 +00:00
|
|
|
|
2008-04-02 18:50:20 +00:00
|
|
|
if(ConfigFileEntry.dline_with_reason)
|
|
|
|
{
|
2016-02-10 01:25:32 +00:00
|
|
|
len = snprintf(buf, sizeof(buf), "ERROR :*** Banned: %s\r\n", get_user_ban_reason(aconf));
|
2010-02-27 23:46:56 +00:00
|
|
|
if (len >= (int)(sizeof(buf)-1))
|
2008-04-02 18:50:20 +00:00
|
|
|
{
|
|
|
|
buf[sizeof(buf) - 3] = '\r';
|
|
|
|
buf[sizeof(buf) - 2] = '\n';
|
|
|
|
buf[sizeof(buf) - 1] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
strcpy(buf, "ERROR :You have been D-lined.\r\n");
|
2014-03-03 04:25:47 +00:00
|
|
|
|
2008-04-02 18:50:20 +00:00
|
|
|
rb_write(F, buf, strlen(buf));
|
|
|
|
rb_close(F);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-29 20:31:17 +00:00
|
|
|
if(check_reject(F, addr)) {
|
|
|
|
/* Reject the connection without closing the socket
|
|
|
|
* because it is now on the delay_exit list. */
|
2008-07-31 23:59:08 +00:00
|
|
|
return 0;
|
2017-07-29 20:31:17 +00:00
|
|
|
}
|
2014-03-03 04:25:47 +00:00
|
|
|
|
2008-07-31 23:59:08 +00:00
|
|
|
if(throttle_add(addr))
|
|
|
|
{
|
|
|
|
rb_write(F, toofast, strlen(toofast));
|
|
|
|
rb_close(F);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-02 18:50:20 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data)
|
|
|
|
{
|
|
|
|
struct Listener *listener = data;
|
|
|
|
struct rb_sockaddr_storage lip;
|
|
|
|
unsigned int locallen = sizeof(struct rb_sockaddr_storage);
|
2014-03-03 04:25:47 +00:00
|
|
|
|
2008-04-04 15:54:37 +00:00
|
|
|
ServerStats.is_ac++;
|
2008-04-02 18:50:20 +00:00
|
|
|
|
|
|
|
if(getsockname(rb_get_fd(F), (struct sockaddr *) &lip, &locallen) < 0)
|
|
|
|
{
|
2008-08-27 23:06:19 +00:00
|
|
|
/* this can fail if the connection disappeared in the meantime */
|
2008-04-02 18:50:20 +00:00
|
|
|
rb_close(F);
|
2008-08-27 23:05:41 +00:00
|
|
|
return;
|
2008-04-02 18:50:20 +00:00
|
|
|
}
|
2014-03-03 04:25:47 +00:00
|
|
|
|
2015-12-12 11:20:51 +00:00
|
|
|
add_connection(listener, F, addr, (struct sockaddr *)&lip);
|
2008-04-02 16:39:36 +00:00
|
|
|
}
|