2016-03-12 14:08:27 +01:00
|
|
|
/* authd/dns.c - authd DNS functions
|
2016-01-06 10:52:37 +01:00
|
|
|
* Copyright (c) 2016 William Pitcock <nenolod@dereferenced.org>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice is present in all copies.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "authd.h"
|
|
|
|
#include "dns.h"
|
2016-03-28 00:04:14 +02:00
|
|
|
#include "notice.h"
|
2016-03-08 09:53:25 +01:00
|
|
|
#include "res.h"
|
2016-01-06 10:52:37 +01:00
|
|
|
|
2016-03-12 14:08:27 +01:00
|
|
|
static void handle_lookup_ip_reply(void *data, struct DNSReply *reply);
|
|
|
|
static void handle_lookup_hostname_reply(void *data, struct DNSReply *reply);
|
|
|
|
|
|
|
|
uint64_t query_count = 0;
|
|
|
|
|
|
|
|
/* A bit different from ircd... you just get a dns_query object.
|
|
|
|
*
|
|
|
|
* It gets freed whenever the res code gets back to us.
|
|
|
|
*/
|
|
|
|
struct dns_query *
|
|
|
|
lookup_ip(const char *host, int aftype, DNSCB callback, void *data)
|
2016-01-06 10:52:37 +01:00
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
struct dns_query *query = (dns_query *)rb_malloc(sizeof(struct dns_query));
|
2016-03-12 14:08:27 +01:00
|
|
|
int g_type;
|
2016-01-06 10:52:37 +01:00
|
|
|
|
2016-03-12 14:08:27 +01:00
|
|
|
if(aftype == AF_INET)
|
2016-01-06 10:52:37 +01:00
|
|
|
{
|
2016-03-12 14:08:27 +01:00
|
|
|
query->type = QUERY_A;
|
|
|
|
g_type = T_A;
|
2016-01-06 10:52:37 +01:00
|
|
|
}
|
2016-03-12 14:08:27 +01:00
|
|
|
#ifdef RB_IPV6
|
|
|
|
else if(aftype == AF_INET6)
|
|
|
|
{
|
|
|
|
query->type = QUERY_AAAA;
|
|
|
|
g_type = T_AAAA;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rb_free(query);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
query->id = query_count++;
|
|
|
|
query->callback = callback;
|
|
|
|
query->data = data;
|
2016-01-06 10:52:37 +01:00
|
|
|
|
2016-03-12 14:08:27 +01:00
|
|
|
query->query.ptr = query;
|
|
|
|
query->query.callback = handle_lookup_ip_reply;
|
|
|
|
|
|
|
|
gethost_byname_type(host, &query->query, g_type);
|
|
|
|
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* See lookup_ip's comment */
|
|
|
|
struct dns_query *
|
2016-03-17 22:23:27 +01:00
|
|
|
lookup_hostname(const char *ip, DNSCB callback, void *data)
|
2016-03-12 14:08:27 +01:00
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
struct dns_query *query = (dns_query *)rb_malloc(sizeof(struct dns_query));
|
2016-03-17 22:23:27 +01:00
|
|
|
int aftype;
|
2016-03-12 14:08:27 +01:00
|
|
|
|
|
|
|
if(!rb_inet_pton_sock(ip, (struct sockaddr *)&query->addr))
|
2016-01-06 10:52:37 +01:00
|
|
|
{
|
2016-03-12 14:08:27 +01:00
|
|
|
rb_free(query);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-03-17 22:23:27 +01:00
|
|
|
aftype = GET_SS_FAMILY(&query->addr);
|
|
|
|
|
2016-03-12 14:08:27 +01:00
|
|
|
if(aftype == AF_INET)
|
|
|
|
query->type = QUERY_PTR_A;
|
2016-01-06 10:52:37 +01:00
|
|
|
#ifdef RB_IPV6
|
2016-03-12 14:08:27 +01:00
|
|
|
else if(aftype == AF_INET6)
|
|
|
|
query->type = QUERY_PTR_AAAA;
|
|
|
|
#endif
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rb_free(query);
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-01-06 10:52:37 +01:00
|
|
|
|
2016-03-12 14:08:27 +01:00
|
|
|
query->id = query_count++;
|
|
|
|
query->callback = callback;
|
|
|
|
query->data = data;
|
2016-01-06 10:52:37 +01:00
|
|
|
|
2016-03-12 14:08:27 +01:00
|
|
|
query->query.ptr = query;
|
|
|
|
query->query.callback = handle_lookup_hostname_reply;
|
2016-01-06 10:52:37 +01:00
|
|
|
|
2016-03-12 14:08:27 +01:00
|
|
|
gethost_byaddr(&query->addr, &query->query);
|
|
|
|
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
2016-03-12 14:10:27 +01:00
|
|
|
/* Cancel a pending query */
|
2016-03-12 14:36:57 +01:00
|
|
|
void
|
2016-03-12 14:10:27 +01:00
|
|
|
cancel_query(struct dns_query *query)
|
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
query->callback = NULL;
|
|
|
|
query->data = NULL;
|
2016-03-12 14:10:27 +01:00
|
|
|
}
|
|
|
|
|
2016-03-12 14:08:27 +01:00
|
|
|
/* Callback from gethost_byname_type */
|
|
|
|
static void
|
|
|
|
handle_lookup_ip_reply(void *data, struct DNSReply *reply)
|
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
struct dns_query *query = (dns_query *)data;
|
2016-03-29 00:50:45 +02:00
|
|
|
char ip[HOSTIPLEN] = "*";
|
2016-03-12 14:08:27 +01:00
|
|
|
|
2016-03-22 23:43:43 +01:00
|
|
|
if(query == NULL)
|
2016-03-29 02:22:02 +02:00
|
|
|
{
|
2016-03-12 14:08:27 +01:00
|
|
|
/* Shouldn't happen */
|
2016-03-29 02:22:02 +02:00
|
|
|
warn_opers(L_CRIT, "DNS: handle_lookup_ip_reply: query == NULL!");
|
|
|
|
exit(EX_DNS_ERROR);
|
|
|
|
}
|
2016-03-17 22:23:27 +01:00
|
|
|
|
|
|
|
if(reply == NULL)
|
2016-03-12 14:08:27 +01:00
|
|
|
goto end;
|
|
|
|
|
|
|
|
switch(query->type)
|
|
|
|
{
|
|
|
|
case QUERY_A:
|
|
|
|
if(GET_SS_FAMILY(&reply->addr) == AF_INET)
|
|
|
|
rb_inet_ntop_sock((struct sockaddr *)&reply->addr, ip, sizeof(ip));
|
2016-01-06 10:52:37 +01:00
|
|
|
break;
|
|
|
|
#ifdef RB_IPV6
|
2016-03-12 14:08:27 +01:00
|
|
|
case QUERY_AAAA:
|
|
|
|
if(GET_SS_FAMILY(&reply->addr) == AF_INET6)
|
2016-01-06 10:52:37 +01:00
|
|
|
{
|
2016-03-12 14:08:27 +01:00
|
|
|
rb_inet_ntop_sock((struct sockaddr *)&reply->addr, ip, sizeof(ip));
|
|
|
|
if(ip[0] == ':')
|
2016-01-06 10:52:37 +01:00
|
|
|
{
|
2016-03-12 14:08:27 +01:00
|
|
|
memmove(&ip[1], ip, strlen(ip));
|
|
|
|
ip[0] = '0';
|
2016-01-06 10:52:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2016-03-29 02:22:02 +02:00
|
|
|
warn_opers(L_CRIT, "DNS: handle_lookup_ip_reply: unknown query type %d",
|
|
|
|
query->type);
|
|
|
|
exit(EX_DNS_ERROR);
|
2016-01-06 10:52:37 +01:00
|
|
|
}
|
|
|
|
|
2016-03-12 14:08:27 +01:00
|
|
|
end:
|
|
|
|
if(query->callback)
|
2016-03-22 23:43:43 +01:00
|
|
|
query->callback(ip, ip[0] != '*', query->type, query->data);
|
2016-03-12 14:08:27 +01:00
|
|
|
|
|
|
|
rb_free(query);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback from gethost_byaddr */
|
|
|
|
static void
|
|
|
|
handle_lookup_hostname_reply(void *data, struct DNSReply *reply)
|
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
struct dns_query *query = (dns_query *)data;
|
2016-03-12 14:08:27 +01:00
|
|
|
char *hostname = NULL;
|
|
|
|
|
2016-03-17 22:23:27 +01:00
|
|
|
if(query == NULL)
|
2016-03-29 02:22:02 +02:00
|
|
|
{
|
2016-03-12 14:08:27 +01:00
|
|
|
/* Shouldn't happen */
|
2016-03-29 02:22:02 +02:00
|
|
|
warn_opers(L_CRIT, "DNS: handle_lookup_hostname_reply: query == NULL!");
|
|
|
|
exit(EX_DNS_ERROR);
|
|
|
|
}
|
2016-03-12 14:08:27 +01:00
|
|
|
|
2016-03-17 22:23:27 +01:00
|
|
|
if(reply == NULL)
|
2016-03-12 14:08:27 +01:00
|
|
|
goto end;
|
|
|
|
|
|
|
|
if(query->type == QUERY_PTR_A)
|
|
|
|
{
|
|
|
|
struct sockaddr_in *ip, *ip_fwd;
|
|
|
|
ip = (struct sockaddr_in *) &query->addr;
|
|
|
|
ip_fwd = (struct sockaddr_in *) &reply->addr;
|
|
|
|
|
2016-03-17 22:23:27 +01:00
|
|
|
if(ip->sin_addr.s_addr == ip_fwd->sin_addr.s_addr)
|
2016-03-12 14:08:27 +01:00
|
|
|
hostname = reply->h_name;
|
|
|
|
}
|
|
|
|
#ifdef RB_IPV6
|
|
|
|
else if(query->type == QUERY_PTR_AAAA)
|
|
|
|
{
|
|
|
|
struct sockaddr_in6 *ip, *ip_fwd;
|
|
|
|
ip = (struct sockaddr_in6 *) &query->addr;
|
|
|
|
ip_fwd = (struct sockaddr_in6 *) &reply->addr;
|
|
|
|
|
2016-03-17 22:23:27 +01:00
|
|
|
if(memcmp(&ip->sin6_addr, &ip_fwd->sin6_addr, sizeof(struct in6_addr)) == 0)
|
2016-03-12 14:08:27 +01:00
|
|
|
hostname = reply->h_name;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else
|
2016-03-29 02:22:02 +02:00
|
|
|
{
|
2016-03-12 14:08:27 +01:00
|
|
|
/* Shouldn't happen */
|
2016-03-29 02:22:02 +02:00
|
|
|
warn_opers(L_CRIT, "DNS: handle_lookup_hostname_reply: unknown query type %d",
|
|
|
|
query->type);
|
|
|
|
exit(EX_DNS_ERROR);
|
|
|
|
}
|
2016-03-12 14:08:27 +01:00
|
|
|
end:
|
|
|
|
if(query->callback)
|
|
|
|
query->callback(hostname, hostname != NULL, query->type, query->data);
|
|
|
|
|
|
|
|
rb_free(query);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
submit_dns_answer(const char *reply, bool status, query_type type, void *data)
|
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
char *id = (char *)data;
|
2016-03-12 14:08:27 +01:00
|
|
|
|
|
|
|
if(!id || type == QUERY_INVALID)
|
2016-03-29 02:22:02 +02:00
|
|
|
{
|
|
|
|
warn_opers(L_CRIT, "DNS: submit_dns_answer gave us a bad query");
|
|
|
|
exit(EX_DNS_ERROR);
|
|
|
|
}
|
2016-03-12 14:08:27 +01:00
|
|
|
|
2016-03-17 22:23:27 +01:00
|
|
|
if(reply == NULL || status == false)
|
2016-03-12 14:08:27 +01:00
|
|
|
{
|
|
|
|
rb_helper_write(authd_helper, "E %s E %c *", id, type);
|
|
|
|
rb_free(id);
|
2016-03-13 09:22:41 +01:00
|
|
|
return;
|
2016-03-12 14:08:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
rb_helper_write(authd_helper, "E %s O %c %s", id, type, reply);
|
|
|
|
rb_free(id);
|
2016-01-06 10:52:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-03-27 06:54:21 +02:00
|
|
|
handle_resolve_dns(int parc, char *parv[])
|
2016-01-06 10:52:37 +01:00
|
|
|
{
|
2016-03-12 14:08:27 +01:00
|
|
|
char *id = rb_strdup(parv[1]);
|
|
|
|
char qtype = *parv[2];
|
|
|
|
char *record = parv[3];
|
|
|
|
int aftype = AF_INET;
|
2016-01-06 10:52:37 +01:00
|
|
|
|
2016-03-12 14:08:27 +01:00
|
|
|
switch(qtype)
|
2016-01-06 10:52:37 +01:00
|
|
|
{
|
2016-03-12 14:08:27 +01:00
|
|
|
#ifdef RB_IPV6
|
2016-01-06 10:52:37 +01:00
|
|
|
case '6':
|
2016-03-12 14:08:27 +01:00
|
|
|
aftype = AF_INET6;
|
|
|
|
#endif
|
|
|
|
case '4':
|
|
|
|
if(!lookup_ip(record, aftype, submit_dns_answer, id))
|
2016-07-13 07:17:21 +02:00
|
|
|
submit_dns_answer(NULL, false, query_type(qtype), NULL);
|
2016-01-06 10:52:37 +01:00
|
|
|
break;
|
2016-03-12 14:08:27 +01:00
|
|
|
#ifdef RB_IPV6
|
2016-01-06 10:52:37 +01:00
|
|
|
case 'S':
|
2016-03-12 14:08:27 +01:00
|
|
|
#endif
|
|
|
|
case 'R':
|
2016-03-17 22:23:27 +01:00
|
|
|
if(!lookup_hostname(record, submit_dns_answer, id))
|
2016-07-13 07:17:21 +02:00
|
|
|
submit_dns_answer(NULL, false, query_type(qtype), NULL);
|
2016-01-06 10:52:37 +01:00
|
|
|
break;
|
2016-03-12 14:08:27 +01:00
|
|
|
default:
|
2016-03-29 02:22:02 +02:00
|
|
|
warn_opers(L_CRIT, "DNS: handle_resolve_dns got an unknown query: %c", qtype);
|
|
|
|
exit(EX_DNS_ERROR);
|
2016-01-06 10:52:37 +01:00
|
|
|
}
|
|
|
|
}
|
2016-03-08 09:53:25 +01:00
|
|
|
|
|
|
|
void
|
2016-03-28 00:28:26 +02:00
|
|
|
enumerate_nameservers(uint32_t rid, const char letter)
|
2016-03-08 09:53:25 +01:00
|
|
|
{
|
2016-03-17 22:23:27 +01:00
|
|
|
char buf[(HOSTIPLEN + 1) * IRCD_MAXNS];
|
2016-03-28 00:04:14 +02:00
|
|
|
size_t s = 0;
|
2016-03-08 09:53:25 +01:00
|
|
|
|
|
|
|
if (!irc_nscount)
|
|
|
|
{
|
|
|
|
/* Shouldn't happen */
|
2016-03-29 02:22:02 +02:00
|
|
|
warn_opers(L_CRIT, "DNS: no name servers!");
|
2016-03-28 00:28:26 +02:00
|
|
|
stats_error(rid, letter, "NONAMESERVERS");
|
2016-03-29 02:22:02 +02:00
|
|
|
exit(EX_DNS_ERROR);
|
2016-03-08 09:53:25 +01:00
|
|
|
}
|
|
|
|
|
2016-03-28 00:04:14 +02:00
|
|
|
for(int i = 0; i < irc_nscount; i++)
|
2016-03-08 09:53:25 +01:00
|
|
|
{
|
2016-03-17 22:23:27 +01:00
|
|
|
char addr[HOSTIPLEN];
|
2016-03-28 00:04:14 +02:00
|
|
|
size_t addrlen;
|
2016-03-08 09:53:25 +01:00
|
|
|
|
|
|
|
rb_inet_ntop_sock((struct sockaddr *)&irc_nsaddr_list[i], addr, sizeof(addr));
|
|
|
|
|
|
|
|
if (!addr[0])
|
|
|
|
{
|
|
|
|
/* Shouldn't happen */
|
2016-03-29 02:22:02 +02:00
|
|
|
warn_opers(L_CRIT, "DNS: bad nameserver!");
|
2016-03-28 00:28:26 +02:00
|
|
|
stats_error(rid, letter, "INVALIDNAMESERVER");
|
2016-03-29 02:22:02 +02:00
|
|
|
exit(EX_DNS_ERROR);
|
2016-03-08 09:53:25 +01:00
|
|
|
}
|
|
|
|
|
2016-03-28 00:04:14 +02:00
|
|
|
addrlen = strlen(addr) + 1;
|
2016-03-28 00:28:26 +02:00
|
|
|
(void)snprintf(&buf[s], sizeof(buf) - s, "%s ", addr);
|
2016-03-28 00:04:14 +02:00
|
|
|
s += addrlen;
|
2016-03-08 09:53:25 +01:00
|
|
|
}
|
|
|
|
|
2016-03-28 00:28:26 +02:00
|
|
|
if(s > 0)
|
|
|
|
buf[--s] = '\0';
|
2016-03-08 09:53:25 +01:00
|
|
|
|
2016-03-28 00:28:26 +02:00
|
|
|
stats_result(rid, letter, "%s", buf);
|
2016-03-08 09:53:25 +01:00
|
|
|
}
|
2016-03-09 10:46:04 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
reload_nameservers(const char letter)
|
|
|
|
{
|
|
|
|
/* Not a whole lot to it */
|
|
|
|
restart_resolver();
|
|
|
|
}
|