#include "setup.h"
#include "listener.h"
#include "client.h"
-#include "irc_string.h"
-#include "sprintf_irc.h"
+#include "match.h"
#include "ircd.h"
#include "ircd_defs.h"
#include "numeric.h"
#include "reject.h"
#include "s_conf.h"
#include "hostmask.h"
+#include "sslproc.h"
+#include "hash.h"
#ifndef INADDR_NONE
#define INADDR_NONE ((unsigned int) 0xffffffff)
#endif
-#if defined(NO_IN6ADDR_ANY) && defined(IPV6)
+#if defined(NO_IN6ADDR_ANY) && defined(RB_IPV6)
static const struct in6_addr in6addr_any =
{ { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } };
#endif
-static listener_t *ListenerPollList = NULL;
-static int accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data);\r
+static struct Listener *ListenerPollList = NULL;
+static int accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data);
static void accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data);
-static listener_t *
+static struct Listener *
make_listener(struct rb_sockaddr_storage *addr)
{
- listener_t *listener = (listener_t *) rb_malloc(sizeof(listener_t));
+ struct Listener *listener = (struct Listener *) rb_malloc(sizeof(struct Listener));
s_assert(0 != listener);
listener->name = me.name;
listener->F = NULL;
}
void
-free_listener(listener_t *listener)
+free_listener(struct Listener *listener)
{
s_assert(NULL != listener);
if(listener == NULL)
ListenerPollList = listener->next;
else
{
- listener_t *prev = ListenerPollList;
+ struct Listener *prev = ListenerPollList;
for (; prev; prev = prev->next)
{
if(listener == prev->next)
* returns "host.foo.org:6667" for a given listener
*/
const char *
-get_listener_name(const listener_t *listener)
+get_listener_name(const struct Listener *listener)
{
static char buf[HOSTLEN + HOSTLEN + PORTNAMELEN + 4];
int port = 0;
if(listener == NULL)
return NULL;
-#ifdef IPV6
+#ifdef RB_IPV6
if(listener->addr.ss_family == AF_INET6)
port = ntohs(((const struct sockaddr_in6 *)&listener->addr)->sin6_port);
else
void
show_ports(struct Client *source_p)
{
- listener_t *listener = 0;
+ struct Listener *listener = 0;
for (listener = ListenerPollList; listener; listener = listener->next)
{
sendto_one_numeric(source_p, RPL_STATSPLINE,
form_str(RPL_STATSPLINE), 'P',
-#ifdef IPV6
+#ifdef RB_IPV6
ntohs(listener->addr.ss_family == AF_INET ? ((struct sockaddr_in *)&listener->addr)->sin_port :
((struct sockaddr_in6 *)&listener->addr)->sin6_port),
#else
ntohs(((struct sockaddr_in *)&listener->addr)->sin_port),
#endif
IsOperAdmin(source_p) ? listener->name : me.name,
- listener->ref_count, (listener->active) ? "active" : "disabled");
+ listener->ref_count, (listener->active) ? "active" : "disabled",
+ listener->ssl ? " ssl" : "");
}
}
#endif
static int
-inetport(listener_t *listener)
+inetport(struct Listener *listener)
{
rb_fde_t *F;
int ret;
F = rb_socket(GET_SS_FAMILY(&listener->addr), SOCK_STREAM, 0, "Listener socket");
-#ifdef IPV6
+#ifdef RB_IPV6
if(listener->addr.ss_family == AF_INET6)
{
struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&listener->addr;
if(!IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &in6addr_any))
{
- inetntop(AF_INET6, &in6->sin6_addr, listener->vhost, sizeof(listener->vhost));
+ rb_inet_ntop(AF_INET6, &in6->sin6_addr, listener->vhost, sizeof(listener->vhost));
listener->name = listener->vhost;
}
} else
struct sockaddr_in *in = (struct sockaddr_in *)&listener->addr;
if(in->sin_addr.s_addr != INADDR_ANY)
{
- inetntop(AF_INET, &in->sin_addr, listener->vhost, sizeof(listener->vhost));
+ rb_inet_ntop(AF_INET, &in->sin_addr, listener->vhost, sizeof(listener->vhost));
listener->name = listener->vhost;
}
}
- if(F == NULL)\r
- {\r
- report_error("opening listener socket %s:%s",\r
- get_listener_name(listener), \r
- get_listener_name(listener), errno);\r
- return 0;\r
- }\r
- else if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus*/\r
- {\r
- report_error("no more connections left for listener %s:%s",\r
- get_listener_name(listener), \r
- get_listener_name(listener), errno);\r
- rb_close(F);\r
- return 0;\r
+ if(F == NULL)
+ {
+ ilog_error("opening listener socket");
+ return 0;
+ }
+ else if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus*/
+ {
+ ilog_error("no more connections left for listener");
+ rb_close(F);
+ return 0;
}
- /*\r
- * XXX - we don't want to do all this crap for a listener\r
- * set_sock_opts(listener);\r
- */\r
- if(setsockopt(rb_get_fd(F), SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt)))\r
- {\r
- report_error("setting SO_REUSEADDR for listener %s:%s",\r
- get_listener_name(listener), \r
- get_listener_name(listener), errno);\r
- rb_close(F);\r
- return 0;\r
+ /*
+ * XXX - we don't want to do all this crap for a listener
+ * set_sock_opts(listener);
+ */
+ if(setsockopt(rb_get_fd(F), SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt)))
+ {
+ ilog_error("setting SO_REUSEADDR for listener");
+ rb_close(F);
+ return 0;
}
- /*\r
- * Bind a port to listen for new connections if port is non-null,\r
- * else assume it is already open and try get something from it.\r
- */\r
-\r
- if(bind(rb_get_fd(F), (struct sockaddr *) &listener->addr, GET_SS_LEN(&listener->addr)))\r
- {\r
- report_error("binding listener socket %s:%s",\r
- get_listener_name(listener), \r
- get_listener_name(listener), errno);\r
- rb_close(F);\r
- return 0;\r
+ /*
+ * Bind a port to listen for new connections if port is non-null,
+ * else assume it is already open and try get something from it.
+ */
+
+ if(bind(rb_get_fd(F), (struct sockaddr *) &listener->addr, GET_SS_LEN(&listener->addr)))
+ {
+ ilog_error("binding listener socket");
+ rb_close(F);
+ return 0;
}
- if((ret = rb_listen(F, RATBOX_SOMAXCONN)))\r
- {\r
- report_error("listen failed for %s:%s", \r
- get_listener_name(listener), \r
- get_listener_name(listener), errno);\r
- rb_close(F);\r
- return 0;\r
+ if((ret = rb_listen(F, RATBOX_SOMAXCONN)))
+ {
+ ilog_error("listen()");
+ rb_close(F);
+ return 0;
}
- listener->F = F;\r
-\r
+ listener->F = F;
+
rb_accept_tcp(listener->F, accept_precallback, accept_callback, listener);
return 1;
}
-static listener_t *
+static struct Listener *
find_listener(struct rb_sockaddr_storage *addr)
{
- listener_t *listener = NULL;
- listener_t *last_closed = NULL;
+ struct Listener *listener = NULL;
+ struct Listener *last_closed = NULL;
for (listener = ListenerPollList; listener; listener = listener->next)
{
if(in4->sin_addr.s_addr == lin4->sin_addr.s_addr &&
in4->sin_port == lin4->sin_port )
{
- if(listener->F == NULL)\r
- last_closed = listener;\r
- else\r
+ if(listener->F == NULL)
+ last_closed = listener;
+ else
return(listener);
}
break;
}
-#ifdef IPV6
+#ifdef RB_IPV6
case AF_INET6:
{
struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)addr;
if(IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &lin6->sin6_addr) &&
in6->sin6_port == lin6->sin6_port)
{
- if(listener->F == NULL)\r
- last_closed = listener;\r
- else\r
+ if(listener->F == NULL)
+ last_closed = listener;
+ else
return(listener);
}
break;
* the format "255.255.255.255"
*/
void
-add_listener(int port, const char *vhost_ip, int family)
+add_listener(int port, const char *vhost_ip, int family, int ssl)
{
- listener_t *listener;
+ struct Listener *listener;
struct rb_sockaddr_storage vaddr;
/*
{
if(family == AF_INET)
{
- if(inetpton(family, vhost_ip, &((struct sockaddr_in *)&vaddr)->sin_addr) <= 0)
+ if(rb_inet_pton(family, vhost_ip, &((struct sockaddr_in *)&vaddr)->sin_addr) <= 0)
return;
}
-#ifdef IPV6
+#ifdef RB_IPV6
else
{
- if(inetpton(family, vhost_ip, &((struct sockaddr_in6 *)&vaddr)->sin6_addr) <= 0)
+ if(rb_inet_pton(family, vhost_ip, &((struct sockaddr_in6 *)&vaddr)->sin6_addr) <= 0)
return;
}
case AF_INET:
((struct sockaddr_in *)&vaddr)->sin_addr.s_addr = INADDR_ANY;
break;
-#ifdef IPV6
+#ifdef RB_IPV6
case AF_INET6:
memcpy(&((struct sockaddr_in6 *)&vaddr)->sin6_addr, &in6addr_any, sizeof(struct in6_addr));
break;
SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in));
((struct sockaddr_in *)&vaddr)->sin_port = htons(port);
break;
-#ifdef IPV6
+#ifdef RB_IPV6
case AF_INET6:
SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in6));
((struct sockaddr_in6 *)&vaddr)->sin6_port = htons(port);
}
if((listener = find_listener(&vaddr)))
{
- if(listener->F != NULL)\r
+ if(listener->F != NULL)
return;
}
else
}
listener->F = NULL;
+ listener->ssl = ssl;
if(inetport(listener))
listener->active = 1;
* close_listener - close a single listener
*/
void
-close_listener(listener_t *listener)
+close_listener(struct Listener *listener)
{
s_assert(listener != NULL);
if(listener == NULL)
return;
- if(listener->F != NULL)\r
- {\r
- rb_close(listener->F);\r
- listener->F = NULL;\r
+ if(listener->F != NULL)
+ {
+ rb_close(listener->F);
+ listener->F = NULL;
}
listener->active = 0;
void
close_listeners()
{
- listener_t *listener;
- listener_t *listener_next = 0;
+ struct Listener *listener;
+ struct Listener *listener_next = 0;
/*
* close all 'extra' listening ports we have
*/
* The client is sent to the auth module for verification, and not put in
* any client list yet.
*/
-static void\r
-add_connection(struct Listener *listener, rb_fde_t *F, struct sockaddr *sai, int exempt)
+static void
+add_connection(struct Listener *listener, rb_fde_t *F, struct sockaddr *sai, struct sockaddr *lai, void *ssl_ctl)
{
struct Client *new_client;
s_assert(NULL != listener);
new_client = make_client(NULL);
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...
*/
- inetntop_sock((struct sockaddr *)&new_client->localClient->ip, new_client->sockhost,
+ rb_inet_ntop_sock((struct sockaddr *)&new_client->localClient->ip, new_client->sockhost,
sizeof(new_client->sockhost));
- strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host));
+ rb_strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host));
new_client->localClient->F = F;
-
+ add_to_cli_fd_hash(new_client);
new_client->localClient->listener = listener;
+ new_client->localClient->ssl_ctl = ssl_ctl;
+ if(ssl_ctl != NULL || rb_fd_ssl(F))
+ SetSSL(new_client);
+
++listener->ref_count;
- if(!exempt)
+ start_auth(new_client);
+}
+
+static const char *toofast = "ERROR :Reconnecting too fast, throttled.\r\n";
+
+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;
+ int len;
+
+ if(listener->ssl && (!ssl_ok || !get_ssld_count()))
{
- if(check_reject(new_client))
- return;
- if(add_unknown_ip(new_client))
- return;
+ rb_close(F);
+ return 0;
}
- start_auth(new_client);
+ if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus */
+ {
+ ++ServerStats.is_ref;
+ /*
+ * slow down the whining to opers bit
+ */
+ if((last_oper_notice + 20) <= rb_current_time())
+ {
+ sendto_realops_snomask(SNO_GENERAL, L_ALL,
+ "All connections in use. (%s)",
+ get_listener_name(listener));
+ last_oper_notice = rb_current_time();
+ }
+
+ rb_write(F, "ERROR :All connections in use\r\n", 32);
+ rb_close(F);
+ /* Re-register a new IO request for the next accept .. */
+ return 0;
+ }
+
+ aconf = find_dline(addr, addr->sa_family);
+ if(aconf != NULL && (aconf->status & CONF_EXEMPTDLINE))
+ return 1;
+
+ /* Do an initial check we aren't connecting too fast or with too many
+ * from this IP... */
+ if(aconf != NULL)
+ {
+ ServerStats.is_ref++;
+
+ if(ConfigFileEntry.dline_with_reason)
+ {
+ len = aconf->created ?
+ rb_snprintf(buf, sizeof(buf), "ERROR :*** Banned: %s (%s)\r\n", aconf->passwd, smalldate(aconf->created)) :
+ rb_snprintf(buf, sizeof(buf), "ERROR :*** Banned: %s\r\n", aconf->passwd);
+ if (len >= (int)(sizeof(buf)-1))
+ {
+ 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");
+
+ rb_write(F, buf, strlen(buf));
+ rb_close(F);
+ return 0;
+ }
+
+ if(check_reject(F, addr))
+ return 0;
+
+ if(throttle_add(addr))
+ {
+ rb_write(F, toofast, strlen(toofast));
+ rb_close(F);
+ return 0;
+ }
+
+ return 1;
}
-static int\r
-accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data)\r
-{\r
- struct Listener *listener = (struct Listener *)data;\r
- char buf[BUFSIZE];\r
- struct ConfItem *aconf;\r
- static time_t last_oper_notice = 0;\r
-\r
- if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus */\r
- {\r
- ++ServerStats->is_ref;\r
- /*\r
- * slow down the whining to opers bit\r
- */\r
- if((last_oper_notice + 20) <= rb_current_time())\r
- {\r
- sendto_realops_flags(SNO_GENERAL, L_ALL,\r
- "All connections in use. (%s)",\r
- get_listener_name(listener));\r
- last_oper_notice = rb_current_time();\r
- }\r
- \r
- rb_write(F, "ERROR :All connections in use\r\n", 32);\r
- rb_close(F);\r
- /* Re-register a new IO request for the next accept .. */\r
- return 0;\r
- }\r
-\r
- aconf = find_dline(addr, addr.ss_family);\r
- if(aconf != NULL && (aconf->status & CONF_EXEMPTDLINE))\r
- return 1;\r
- \r
- /* Do an initial check we aren't connecting too fast or with too many\r
- * from this IP... */\r
- if(aconf != NULL)\r
- {\r
- ServerStats->is_ref++;\r
- \r
- if(ConfigFileEntry.dline_with_reason)\r
- {\r
- if (rb_snprintf(buf, sizeof(buf), "ERROR :*** Banned: %s\r\n", aconf->passwd) >= (int)(sizeof(buf)-1))\r
- {\r
- buf[sizeof(buf) - 3] = '\r';\r
- buf[sizeof(buf) - 2] = '\n';\r
- buf[sizeof(buf) - 1] = '\0';\r
- }\r
- }\r
- else\r
- strcpy(buf, "ERROR :You have been D-lined.\r\n");\r
- \r
- rb_write(F, buf, strlen(buf));\r
- rb_close(F);\r
- return 0;\r
- }\r
-\r
- return 1;\r
-}\r
-\r
-static void\r
-accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data)\r
-{\r
- struct Listener *listener = data;\r
- struct rb_sockaddr_storage lip;\r
- unsigned int locallen = sizeof(struct rb_sockaddr_storage);\r
- \r
- ServerStats->is_ac++;\r
-\r
- if(getsockname(rb_get_fd(F), (struct sockaddr *) &lip, &locallen) < 0)\r
- {\r
- /* this shouldn't fail so... */\r
- /* XXX add logging of this */\r
- rb_close(F);\r
- }\r
- \r
- add_connection(listener, F, addr, 1);\r
+static void
+accept_ssld(rb_fde_t *F, struct sockaddr *addr, struct sockaddr *laddr, struct Listener *listener)
+{
+ ssl_ctl_t *ctl;
+ rb_fde_t *xF[2];
+ if(rb_socketpair(AF_UNIX, SOCK_STREAM, 0, &xF[0], &xF[1], "Incoming ssld Connection") == -1)
+ {
+ ilog_error("creating SSL/TLS socket pairs");
+ rb_close(F);
+ return;
+ }
+ ctl = start_ssld_accept(F, xF[1], rb_get_fd(xF[0])); /* this will close F for us */
+ add_connection(listener, xF[0], addr, laddr, ctl);
+}
+
+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);
+
+ ServerStats.is_ac++;
+
+ if(getsockname(rb_get_fd(F), (struct sockaddr *) &lip, &locallen) < 0)
+ {
+ /* this can fail if the connection disappeared in the meantime */
+ rb_close(F);
+ return;
+ }
+
+ if(listener->ssl)
+ accept_ssld(F, addr, (struct sockaddr *)&lip, listener);
+ else
+ add_connection(listener, F, addr, (struct sockaddr *)&lip, NULL);
}