X-Git-Url: https://jfr.im/git/solanum.git/blobdiff_plain/9b24cbdecc72337e825764f39cdf59ca23f41788..51fa2ab8a39eb4529a9544fcea76f2f09dac4f85:/ircd/newconf.c diff --git a/ircd/newconf.c b/ircd/newconf.c index 7a726db3..b9109570 100644 --- a/ircd/newconf.c +++ b/ircd/newconf.c @@ -24,7 +24,6 @@ #include "cache.h" #include "ircd.h" #include "snomask.h" -#include "blacklist.h" #include "sslproc.h" #include "privilege.h" #include "chmode.h" @@ -55,9 +54,14 @@ static struct alias_entry *yy_alias = NULL; static char *yy_blacklist_host = NULL; static char *yy_blacklist_reason = NULL; -static int yy_blacklist_ipv4 = 1; -static int yy_blacklist_ipv6 = 0; -static rb_dlink_list yy_blacklist_filters; +static uint8_t yy_blacklist_iptype = 0; +static rb_dlink_list yy_blacklist_filters = { NULL, NULL, 0 }; + +static char *yy_opm_address_ipv4 = NULL; +static char *yy_opm_address_ipv6 = NULL; +static uint16_t yy_opm_port_ipv4 = 0; +static uint16_t yy_opm_port_ipv6 = 0; +static rb_dlink_list yy_opm_scanner_list; static char *yy_privset_extends = NULL; @@ -888,7 +892,6 @@ conf_set_listen_port_both(void *data, int ssl) add_listener(args->v.number, listener_address, family, ssl, ssl || yy_defer_accept); } - } } @@ -1841,6 +1844,9 @@ conf_set_channel_autochanmodes(void *data) /* XXX for below */ static void conf_set_blacklist_reason(void *data); +#define IPTYPE_IPV4 1 +#define IPTYPE_IPV6 2 + static void conf_set_blacklist_host(void *data) { @@ -1854,8 +1860,7 @@ conf_set_blacklist_host(void *data) return; } - yy_blacklist_ipv4 = 1; - yy_blacklist_ipv6 = 0; + yy_blacklist_iptype |= IPTYPE_IPV4; yy_blacklist_host = rb_strdup(data); } @@ -1865,25 +1870,24 @@ conf_set_blacklist_type(void *data) conf_parm_t *args = data; /* Don't assume we have either if we got here */ - yy_blacklist_ipv4 = 0; - yy_blacklist_ipv6 = 0; + yy_blacklist_iptype = 0; for (; args; args = args->next) { if (!strcasecmp(args->v.string, "ipv4")) - yy_blacklist_ipv4 = 1; + yy_blacklist_iptype |= IPTYPE_IPV4; else if (!strcasecmp(args->v.string, "ipv6")) - yy_blacklist_ipv6 = 1; + yy_blacklist_iptype |= IPTYPE_IPV6; else conf_report_error("blacklist::type has unknown address family %s", args->v.string); } /* If we have neither, just default to IPv4 */ - if (!yy_blacklist_ipv4 && !yy_blacklist_ipv6) + if (!yy_blacklist_iptype) { - conf_report_error("blacklist::type has neither IPv4 nor IPv6 (defaulting to IPv4)"); - yy_blacklist_ipv4 = 1; + conf_report_warning("blacklist::type has neither IPv4 nor IPv6 (defaulting to IPv4)"); + yy_blacklist_iptype = IPTYPE_IPV4; } } @@ -1891,13 +1895,13 @@ static void conf_set_blacklist_matches(void *data) { conf_parm_t *args = data; + enum filter_t { FILTER_NONE, FILTER_ALL, FILTER_LAST }; for (; args; args = args->next) { - struct BlacklistFilter *filter; char *str = args->v.string; char *p; - int type = BLACKLIST_FILTER_LAST; + enum filter_t type = FILTER_LAST; if (CF_TYPE(args->type) != CF_QSTRING) { @@ -1922,17 +1926,17 @@ conf_set_blacklist_matches(void *data) { /* Check for validity */ if (*p == '.') - type = BLACKLIST_FILTER_ALL; - else if (!isalnum((unsigned char)*p)) + type = FILTER_ALL; + else if (!isdigit((unsigned char)*p)) { conf_report_error("blacklist::matches has invalid IP match entry %s", str); - type = 0; + type = FILTER_NONE; break; } } - if (type == BLACKLIST_FILTER_ALL) + if (type == FILTER_ALL) { /* Basic IP sanity check */ struct rb_sockaddr_storage tmp; @@ -1943,7 +1947,7 @@ conf_set_blacklist_matches(void *data) continue; } } - else if (type == BLACKLIST_FILTER_LAST) + else if (type == FILTER_LAST) { /* Verify it's the correct length */ if (strlen(str) > 3) @@ -1958,11 +1962,7 @@ conf_set_blacklist_matches(void *data) continue; /* Invalid entry */ } - filter = rb_malloc(sizeof(struct BlacklistFilter)); - filter->type = type; - rb_strlcpy(filter->filterstr, str, sizeof(filter->filterstr)); - - rb_dlinkAdd(filter, &filter->node, &yy_blacklist_filters); + rb_dlinkAddAlloc(rb_strdup(str), &yy_blacklist_filters); } } @@ -1974,9 +1974,11 @@ conf_set_blacklist_reason(void *data) if (yy_blacklist_host && data) { yy_blacklist_reason = rb_strdup(data); - if (yy_blacklist_ipv6) + if (yy_blacklist_iptype & IPTYPE_IPV6) { - /* Make sure things fit (64 = alnum count + dots) */ + /* Make sure things fit (magic number 64 = alnum count + dots) + * Example: 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa + */ if ((64 + strlen(yy_blacklist_host)) > IRCD_RES_HOSTLEN) { conf_report_error("blacklist::host %s results in IPv6 queries that are too long", @@ -1985,9 +1987,11 @@ conf_set_blacklist_reason(void *data) } } /* Avoid doing redundant check, IPv6 is bigger than IPv4 --Elizabeth */ - if (yy_blacklist_ipv4 && !yy_blacklist_ipv6) + if ((yy_blacklist_iptype & IPTYPE_IPV4) && !(yy_blacklist_iptype & IPTYPE_IPV6)) { - /* Make sure things fit (16 = number of nums + dots) */ + /* Make sure things fit for worst case (magic number 16 = number of nums + dots) + * Example: 127.127.127.127.in-addr.arpa + */ if ((16 + strlen(yy_blacklist_host)) > IRCD_RES_HOSTLEN) { conf_report_error("blacklist::host %s results in IPv4 queries that are too long", @@ -1996,30 +2000,285 @@ conf_set_blacklist_reason(void *data) } } - new_blacklist(yy_blacklist_host, yy_blacklist_reason, yy_blacklist_ipv4, yy_blacklist_ipv6, - &yy_blacklist_filters); + add_blacklist(yy_blacklist_host, yy_blacklist_reason, yy_blacklist_iptype, &yy_blacklist_filters); } cleanup_bl: - if (data == NULL) + RB_DLINK_FOREACH_SAFE(ptr, nptr, yy_blacklist_filters.head) + { + rb_free(ptr->data); + rb_dlinkDestroy(ptr, &yy_blacklist_filters); + } + + yy_blacklist_filters = (rb_dlink_list){ NULL, NULL, 0 }; + + rb_free(yy_blacklist_host); + rb_free(yy_blacklist_reason); + yy_blacklist_host = NULL; + yy_blacklist_reason = NULL; + yy_blacklist_iptype = 0; +} + + +struct opm_scanner +{ + const char *type; + uint16_t port; + + rb_dlink_node node; +}; + +static int +conf_begin_opm(struct TopConf *tc) +{ + yy_opm_address_ipv4 = yy_opm_address_ipv6 = NULL; + yy_opm_port_ipv4 = yy_opm_port_ipv6 = 0; + return 0; +} + +static int +conf_end_opm(struct TopConf *tc) +{ + rb_dlink_node *ptr, *nptr; + bool fail = false; + + if(!rb_dlink_list_length(&yy_opm_scanner_list)) { - RB_DLINK_FOREACH_SAFE(ptr, nptr, yy_blacklist_filters.head) + conf_report_error("No opm scanners configured, disabling opm."); + fail = true; + goto end; + } + + if(yy_opm_port_ipv4 > 0) + { + if(yy_opm_address_ipv4 != NULL) + create_opm_listener(yy_opm_address_ipv4, yy_opm_port_ipv4); + else { - rb_dlinkDelete(ptr, &yy_blacklist_filters); - rb_free(ptr); + char ip[HOSTIPLEN]; + if(!rb_inet_ntop_sock((struct sockaddr *)&ServerInfo.ip, ip, sizeof(ip))) + conf_report_error("No opm::listen_ipv4 nor serverinfo::vhost directive; cannot listen on IPv4"); + else + create_opm_listener(ip, yy_opm_port_ipv4); + } + } + + if(yy_opm_port_ipv6 > 0) + { + if(yy_opm_address_ipv6 != NULL) + create_opm_listener(yy_opm_address_ipv6, yy_opm_port_ipv6); + else + { + char ip[HOSTIPLEN]; + if(!rb_inet_ntop_sock((struct sockaddr *)&ServerInfo.ip6, ip, sizeof(ip))) + conf_report_error("No opm::listen_ipv6 nor serverinfo::vhost directive; cannot listen on IPv6"); + else + create_opm_listener(ip, yy_opm_port_ipv6); } } + + /* If there's no listeners... */ + fail = (yy_opm_port_ipv4 == 0 || yy_opm_port_ipv6 == 0); + +end: + RB_DLINK_FOREACH_SAFE(ptr, nptr, yy_opm_scanner_list.head) + { + struct opm_scanner *scanner = ptr->data; + + if(!fail) + create_opm_proxy_scanner(scanner->type, scanner->port); + + rb_dlinkDelete(&scanner->node, &yy_opm_scanner_list); + rb_free(scanner); + } + + rb_free(yy_opm_address_ipv4); + rb_free(yy_opm_address_ipv6); + return 0; +} + + +static void +conf_set_opm_listen_address_both(void *data, bool ipv6) +{ + struct rb_sockaddr_storage addr; + const char *confstr = (ipv6 ? "opm::listen_ipv6" : "opm::listen_ipv4"); + char *ip = data; + + if(!rb_inet_pton_sock(ip, (struct sockaddr *)&addr)) + { + conf_report_error("%s is an invalid address: %s", confstr, ip); + return; + } + + if(ipv6) + { +#ifdef RB_IPV6 + if(GET_SS_FAMILY(&addr) != AF_INET6) + { + conf_report_error("%s is of the wrong address type: %s", confstr, ip); + return; + } + + if(yy_opm_address_ipv6 != NULL) + { + conf_report_error("%s overwrites previous address %s", confstr, ip); + return; + } + + yy_opm_address_ipv6 = rb_strdup(ip); +#else + conf_report_error("%s requires IPv6 support in your ircd", confstr, ip); + return; +#endif + } else { - yy_blacklist_filters = (rb_dlink_list){ NULL, NULL, 0 }; + if(GET_SS_FAMILY(&addr) != AF_INET) + { + conf_report_error("%s is of the wrong address type: %s", confstr, ip); + return; + } + + if(yy_opm_address_ipv4 != NULL) + { + conf_report_error("%s overwrites previous address %s", confstr, ip); + return; + } + + yy_opm_address_ipv4 = rb_strdup(ip); } +} - rb_free(yy_blacklist_host); - rb_free(yy_blacklist_reason); - yy_blacklist_host = NULL; - yy_blacklist_reason = NULL; - yy_blacklist_ipv4 = 1; - yy_blacklist_ipv6 = 0; +static void +conf_set_opm_listen_address_ipv4(void *data) +{ + conf_set_opm_listen_address_both(data, false); +} + +static void +conf_set_opm_listen_address_ipv6(void *data) +{ + conf_set_opm_listen_address_both(data, true); +} + +static void +conf_set_opm_listen_port_both(void *data, bool ipv6) +{ + int port = *((int *)data); + const char *confstr = (ipv6 ? "opm::port_ipv6" : "opm::port_ipv4"); + +#ifndef RB_IPV6 + if(ipv6) + { + conf_report_error("%s requires IPv6 support in your ircd", confstr); + return; + } +#endif + + if(port > 65535 || port <= 0) + { + conf_report_error("%s is out of range: %d", confstr, port); + return; + } + + if(ipv6) + { + if(yy_opm_port_ipv4) + { + conf_report_error("%s overwrites existing port %hu", + confstr, yy_opm_port_ipv4); + return; + } + + yy_opm_port_ipv4 = port; + } + else + { + if(yy_opm_port_ipv6) + { + conf_report_error("%s overwrites existing port %hu", + confstr, yy_opm_port_ipv6); + return; + } + + yy_opm_port_ipv6 = port; + } +} + +static void +conf_set_opm_listen_port_ipv4(void *data) +{ + conf_set_opm_listen_port_both(data, false); +} + +static void +conf_set_opm_listen_port_ipv6(void *data) +{ + conf_set_opm_listen_port_both(data, true); +} + +static void +conf_set_opm_listen_port(void *data) +{ + conf_set_opm_listen_port_both(data, true); + conf_set_opm_listen_port_both(data, false); +} + +static void +conf_set_opm_scan_ports_all(void *data, const char *node, const char *type) +{ + conf_parm_t *args = data; + for (; args; args = args->next) + { + rb_dlink_node *ptr; + bool dup = false; + + if(CF_TYPE(args->type) != CF_INT) + { + conf_report_error("%s argument is not an integer -- ignoring.", node); + continue; + } + + if(args->v.number > 65535 || args->v.number <= 0) + { + conf_report_error("%s argument is not an integer between 1 and 65535 -- ignoring.", node); + continue; + } + + /* Check for duplicates */ + RB_DLINK_FOREACH(ptr, yy_opm_scanner_list.head) + { + struct opm_scanner *scanner = ptr->data; + + if(scanner->port == args->v.number && strcmp(type, scanner->type) == 0) + { + conf_report_error("%s argument is duplicate", node); + dup = true; + break; + } + } + + if(!dup) + { + struct opm_scanner *scanner = rb_malloc(sizeof(struct opm_scanner)); + scanner->port = args->v.number; + scanner->type = type; + rb_dlinkAdd(scanner, &scanner->node, &yy_opm_scanner_list); + } + } +} + +static void +conf_set_opm_scan_ports_socks4(void *data) +{ + conf_set_opm_scan_ports_all(data, "opm::socks4_ports", "socks4"); +} + +static void +conf_set_opm_scan_ports_socks5(void *data) +{ + conf_set_opm_scan_ports_all(data, "opm::socks5_ports", "socks5"); } /* public functions */ @@ -2550,4 +2809,13 @@ newconf_init() add_conf_item("blacklist", "type", CF_STRING | CF_FLIST, conf_set_blacklist_type); add_conf_item("blacklist", "matches", CF_QSTRING | CF_FLIST, conf_set_blacklist_matches); add_conf_item("blacklist", "reject_reason", CF_QSTRING, conf_set_blacklist_reason); + + add_top_conf("opm", conf_begin_opm, conf_end_opm, NULL); + add_conf_item("opm", "listen_ipv4", CF_QSTRING, conf_set_opm_listen_address_ipv4); + add_conf_item("opm", "listen_ipv6", CF_QSTRING, conf_set_opm_listen_address_ipv6); + add_conf_item("opm", "port_v4", CF_INT, conf_set_opm_listen_port_ipv4); + add_conf_item("opm", "port_v6", CF_INT, conf_set_opm_listen_port_ipv6); + add_conf_item("opm", "listen_port", CF_INT, conf_set_opm_listen_port); + add_conf_item("opm", "socks4_ports", CF_INT | CF_FLIST, conf_set_opm_scan_ports_socks4); + add_conf_item("opm", "socks5_ports", CF_INT | CF_FLIST, conf_set_opm_scan_ports_socks5); }