From 60141cfa8c26971ec2258118a661609c7a15d261 Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Sun, 2 Apr 2017 19:50:49 +0100 Subject: Add reverse_lookup() function which does a more proper (verified) DNS reverse lookup. (Code copied from OpenSSH instead of redoing it poorly.) --- irc.c | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) (limited to 'irc.c') diff --git a/irc.c b/irc.c index c1821eac..7f3f66a7 100644 --- a/irc.c +++ b/irc.c @@ -67,21 +67,11 @@ irc_t *irc_new(int fd) if (global.conf->hostname) { myhost = g_strdup(global.conf->hostname); } else if (getsockname(irc->fd, (struct sockaddr*) &sock, &socklen) == 0) { - char buf[NI_MAXHOST + 1]; - - if (getnameinfo((struct sockaddr *) &sock, socklen, buf, - NI_MAXHOST, NULL, 0, 0) == 0) { - myhost = g_strdup(ipv6_unwrap(buf)); - } + myhost = reverse_lookup((struct sockaddr*) &sock, socklen); } if (getpeername(irc->fd, (struct sockaddr*) &sock, &socklen) == 0) { - char buf[NI_MAXHOST + 1]; - - if (getnameinfo((struct sockaddr *) &sock, socklen, buf, - NI_MAXHOST, NULL, 0, 0) == 0) { - host = g_strdup(ipv6_unwrap(buf)); - } + host = reverse_lookup((struct sockaddr*) &sock, socklen); } if (host == NULL) { -- cgit v1.2.3 From d179fd900a89a41c8cf95a0f61caef4ec7c6c09e Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Mon, 3 Apr 2017 21:55:50 +0100 Subject: Add PROXY command. Not actually an IRC protocol command, it's a HAProxy trick supported by stunnel to indicate where the connection originally came from. Looks a little better on public servers. --- irc.c | 74 +++++++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 48 insertions(+), 26 deletions(-) (limited to 'irc.c') diff --git a/irc.c b/irc.c index 7f3f66a7..655aa967 100644 --- a/irc.c +++ b/irc.c @@ -40,9 +40,6 @@ static char *set_eval_utf8_nicks(set_t *set, char *value); irc_t *irc_new(int fd) { irc_t *irc; - struct sockaddr_storage sock; - socklen_t socklen = sizeof(sock); - char *host = NULL, *myhost = NULL; irc_user_t *iu; GSList *l; set_t *s; @@ -64,23 +61,6 @@ irc_t *irc_new(int fd) irc->iconv = (GIConv) - 1; irc->oconv = (GIConv) - 1; - if (global.conf->hostname) { - myhost = g_strdup(global.conf->hostname); - } else if (getsockname(irc->fd, (struct sockaddr*) &sock, &socklen) == 0) { - myhost = reverse_lookup((struct sockaddr*) &sock, socklen); - } - - if (getpeername(irc->fd, (struct sockaddr*) &sock, &socklen) == 0) { - host = reverse_lookup((struct sockaddr*) &sock, socklen); - } - - if (host == NULL) { - host = g_strdup("localhost.localdomain"); - } - if (myhost == NULL) { - myhost = g_strdup("localhost.localdomain"); - } - if (global.conf->ping_interval > 0 && global.conf->ping_timeout > 0) { irc->ping_source_id = b_timeout_add(global.conf->ping_interval * 1000, irc_userping, irc); } @@ -127,17 +107,16 @@ irc_t *irc_new(int fd) s = set_add(&b->set, "utf8_nicks", "false", set_eval_utf8_nicks, irc); irc->root = iu = irc_user_new(irc, ROOT_NICK); - iu->host = g_strdup(myhost); iu->fullname = g_strdup(ROOT_FN); iu->f = &irc_user_root_funcs; iu = irc_user_new(irc, NS_NICK); - iu->host = g_strdup(myhost); iu->fullname = g_strdup(ROOT_FN); iu->f = &irc_user_root_funcs; irc->user = g_new0(irc_user_t, 1); - irc->user->host = g_strdup(host); + + irc_set_hosts(irc, NULL, 0); conf_loaddefaults(irc); @@ -153,9 +132,6 @@ irc_t *irc_new(int fd) "See doc/README for more information."); } - g_free(myhost); - g_free(host); - /* libpurple doesn't like fork()s after initializing itself, so this is the right moment to initialize it. */ #ifdef WITH_PURPLE @@ -177,6 +153,52 @@ irc_t *irc_new(int fd) return irc; } +void irc_set_hosts(irc_t *irc, const struct sockaddr *remote_addr, const socklen_t remote_addrlen) +{ + struct sockaddr_storage sock; + socklen_t socklen = sizeof(sock); + char *host = NULL, *myhost = NULL; + struct irc_user *iu; + + if (global.conf->hostname) { + myhost = g_strdup(global.conf->hostname); + } else if (getsockname(irc->fd, (struct sockaddr*) &sock, &socklen) == 0) { + myhost = reverse_lookup((struct sockaddr*) &sock, socklen); + } + + if (remote_addrlen > 0) { + host = reverse_lookup(remote_addr, remote_addrlen); + } else if (getpeername(irc->fd, (struct sockaddr*) &sock, &socklen) == 0) { + host = reverse_lookup((struct sockaddr*) &sock, socklen); + } + + if (myhost == NULL) { + myhost = g_strdup("localhost.localdomain"); + } + if (host == NULL) { + host = g_strdup("localhost.localdomain"); + } + + if (irc->root->host != irc->root->nick) { + g_free(irc->root->host); + } + irc->root->host = g_strdup(myhost); + if ((iu = irc_user_by_name(irc, NS_NICK))) { + if (iu->host != iu->nick) { + g_free(iu->host); + } + iu->host = g_strdup(myhost); + } + + if (irc->user->host != irc->user->nick) { + g_free(irc->user->host); + } + irc->user->host = g_strdup(host); + + g_free(myhost); + g_free(host); +} + /* immed=1 makes this function pretty much equal to irc_free(), except that this one will "log". In case the connection is already broken and we shouldn't try to write to it. */ -- cgit v1.2.3 From b0979452e9d42896b8f6fdc0da76dc0c79651b62 Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Wed, 5 Apr 2017 23:38:08 +0100 Subject: Move canohost functions (diff licence) to separate file. --- irc.c | 1 + 1 file changed, 1 insertion(+) (limited to 'irc.c') diff --git a/irc.c b/irc.c index 655aa967..0010b3e2 100644 --- a/irc.c +++ b/irc.c @@ -24,6 +24,7 @@ */ #include "bitlbee.h" +#include "canohost.h" #include "ipc.h" #include "dcc.h" #include "lib/ssl_client.h" -- cgit v1.2.3