X-Git-Url: https://jfr.im/git/irc/rqf/shadowircd.git/blobdiff_plain/0632be9b472ad7322576a7fce686d14aefbbffb2..5d2fe5036edadb7476799923fbaa59d99335d98f:/src/listener.c diff --git a/src/listener.c b/src/listener.c index d88c672..0849ab6 100644 --- a/src/listener.c +++ b/src/listener.c @@ -28,8 +28,7 @@ #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" @@ -41,18 +40,20 @@ #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); +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 * @@ -112,7 +113,7 @@ get_listener_name(const listener_t *listener) 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 @@ -138,14 +139,15 @@ show_ports(struct Client *source_p) { 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" : ""); } } @@ -175,13 +177,13 @@ inetport(listener_t *listener) 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 @@ -190,65 +192,55 @@ inetport(listener_t *listener) 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) - { - report_error("opening listener socket %s:%s", - get_listener_name(listener), - get_listener_name(listener), errno); - return 0; - } - else if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus*/ - { - report_error("no more connections left for listener %s:%s", - get_listener_name(listener), - get_listener_name(listener), errno); - rb_close(F); - return 0; + 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; } - /* - * 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))) - { - report_error("setting SO_REUSEADDR for listener %s:%s", - get_listener_name(listener), - get_listener_name(listener), errno); - rb_close(F); - return 0; + /* + * 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; } - /* - * 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))) - { - report_error("binding listener socket %s:%s", - get_listener_name(listener), - get_listener_name(listener), errno); - rb_close(F); - return 0; + /* + * 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))) - { - report_error("listen failed for %s:%s", - get_listener_name(listener), - get_listener_name(listener), errno); - rb_close(F); - return 0; + if((ret = rb_listen(F, RATBOX_SOMAXCONN))) + { + ilog_error("listen()"); + rb_close(F); + return 0; } - listener->F = F; - + listener->F = F; + rb_accept_tcp(listener->F, accept_precallback, accept_callback, listener); return 1; } @@ -273,14 +265,14 @@ find_listener(struct rb_sockaddr_storage *addr) if(in4->sin_addr.s_addr == lin4->sin_addr.s_addr && in4->sin_port == lin4->sin_port ) { - if(listener->F == NULL) - last_closed = listener; - else + 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; @@ -288,9 +280,9 @@ find_listener(struct rb_sockaddr_storage *addr) if(IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &lin6->sin6_addr) && in6->sin6_port == lin6->sin6_port) { - if(listener->F == NULL) - last_closed = listener; - else + if(listener->F == NULL) + last_closed = listener; + else return(listener); } break; @@ -313,7 +305,7 @@ find_listener(struct rb_sockaddr_storage *addr) * 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 rb_sockaddr_storage vaddr; @@ -330,13 +322,13 @@ add_listener(int port, const char *vhost_ip, int family) { 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; } @@ -348,7 +340,7 @@ add_listener(int port, const char *vhost_ip, int family) 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; @@ -363,7 +355,7 @@ add_listener(int port, const char *vhost_ip, int family) 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); @@ -374,7 +366,7 @@ add_listener(int port, const char *vhost_ip, int family) } if((listener = find_listener(&vaddr))) { - if(listener->F != NULL) + if(listener->F != NULL) return; } else @@ -385,6 +377,7 @@ add_listener(int port, const char *vhost_ip, int family) } listener->F = NULL; + listener->ssl = ssl; if(inetport(listener)) listener->active = 1; @@ -401,10 +394,10 @@ close_listener(listener_t *listener) s_assert(listener != NULL); if(listener == NULL) return; - if(listener->F != NULL) - { - rb_close(listener->F); - listener->F = NULL; + if(listener->F != NULL) + { + rb_close(listener->F); + listener->F = NULL; } listener->active = 0; @@ -441,8 +434,8 @@ close_listeners() * The client is sent to the auth module for verification, and not put in * any client list yet. */ -static void -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, void *ssl_ctl) { struct Client *new_client; s_assert(NULL != listener); @@ -459,101 +452,128 @@ add_connection(struct Listener *listener, rb_fde_t *F, struct sockaddr *sai, int * 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; + + 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) + { + if (rb_snprintf(buf, sizeof(buf), "ERROR :*** Banned: %s\r\n", aconf->passwd) >= (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 -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; - - 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_flags(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->ss_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) - { - if (rb_snprintf(buf, sizeof(buf), "ERROR :*** Banned: %s\r\n", aconf->passwd) >= (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; - } - - 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); - - ServerStats->is_ac++; - - if(getsockname(rb_get_fd(F), (struct sockaddr *) &lip, &locallen) < 0) - { - /* this shouldn't fail so... */ - /* XXX add logging of this */ - rb_close(F); - } - - add_connection(listener, F, addr, 1); +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]; + rb_socketpair(AF_UNIX, SOCK_STREAM, 0, &xF[0], &xF[1], "Incoming ssld Connection"); + ctl = start_ssld_accept(F, xF[1], rb_get_fd(xF[0])); /* this will close F for us */ + add_connection(listener, xF[0], addr, 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 shouldn't fail so... */ + /* XXX add logging of this */ + rb_close(F); + } + + if(listener->ssl) + accept_ssld(F, addr, (struct sockaddr *)&lip, listener); + else + add_connection(listener, F, addr, NULL); }