2016-01-06 11:28:34 +01:00
|
|
|
/*
|
|
|
|
* dns.c: An interface to the resolver module in authd
|
|
|
|
* (based somewhat on ircd-ratbox dns.c)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Aaron Sethman <androsyn@ratbox.org>
|
|
|
|
* Copyright (C) 2005-2012 ircd-ratbox development team
|
|
|
|
* Copyright (C) 2016 William Pitcock <nenolod@dereferenced.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
|
|
|
|
* USA
|
|
|
|
*/
|
|
|
|
|
2016-03-08 09:53:25 +01:00
|
|
|
#include "stdinc.h"
|
|
|
|
#include "rb_lib.h"
|
|
|
|
#include "client.h"
|
|
|
|
#include "ircd_defs.h"
|
|
|
|
#include "parse.h"
|
|
|
|
#include "dns.h"
|
|
|
|
#include "match.h"
|
|
|
|
#include "logger.h"
|
|
|
|
#include "s_conf.h"
|
|
|
|
#include "client.h"
|
|
|
|
#include "send.h"
|
|
|
|
#include "numeric.h"
|
|
|
|
#include "msg.h"
|
|
|
|
#include "hash.h"
|
2016-01-06 11:28:34 +01:00
|
|
|
|
2016-01-08 12:44:05 +01:00
|
|
|
#define DNS_HOST_IPV4 ((char)'4')
|
|
|
|
#define DNS_HOST_IPV6 ((char)'6')
|
|
|
|
#define DNS_REVERSE_IPV4 ((char)'R')
|
|
|
|
#define DNS_REVERSE_IPV6 ((char)'S')
|
2016-01-06 11:28:34 +01:00
|
|
|
|
2016-03-13 09:23:41 +01:00
|
|
|
static void submit_dns(uint32_t uid, char type, const char *addr);
|
|
|
|
static void submit_dns_stat(uint32_t uid);
|
2016-01-06 11:28:34 +01:00
|
|
|
|
|
|
|
struct dnsreq
|
|
|
|
{
|
|
|
|
DNSCB callback;
|
|
|
|
void *data;
|
|
|
|
};
|
|
|
|
|
2016-03-08 09:53:25 +01:00
|
|
|
struct dnsstatreq
|
|
|
|
{
|
|
|
|
DNSLISTCB callback;
|
|
|
|
void *data;
|
|
|
|
};
|
|
|
|
|
2016-03-13 09:23:41 +01:00
|
|
|
/* These serve as a form of sparse array */
|
2016-03-23 14:09:58 +01:00
|
|
|
static rb_dictionary *query_dict;
|
|
|
|
static rb_dictionary *stat_dict;
|
2016-03-13 09:23:41 +01:00
|
|
|
|
2016-03-08 10:22:25 +01:00
|
|
|
rb_dlink_list nameservers;
|
|
|
|
|
2016-03-13 09:23:41 +01:00
|
|
|
static uint32_t query_id = 0;
|
|
|
|
static uint32_t stat_id = 0;
|
2016-01-06 11:28:34 +01:00
|
|
|
|
|
|
|
|
2016-04-03 03:19:37 +02:00
|
|
|
static inline uint32_t
|
|
|
|
assign_id(uint32_t *id)
|
|
|
|
{
|
|
|
|
if(++(*id) == 0)
|
|
|
|
*id = 1;
|
|
|
|
|
|
|
|
return *id;
|
|
|
|
}
|
2016-03-08 09:53:25 +01:00
|
|
|
|
2016-01-06 11:28:34 +01:00
|
|
|
static void
|
2016-03-13 09:23:41 +01:00
|
|
|
handle_dns_failure(uint32_t xid)
|
2016-01-06 11:28:34 +01:00
|
|
|
{
|
2016-03-13 09:23:41 +01:00
|
|
|
struct dnsreq *req = rb_dictionary_retrieve(query_dict, RB_UINT_TO_POINTER(xid));
|
|
|
|
s_assert(req);
|
2016-01-06 11:28:34 +01:00
|
|
|
|
|
|
|
if(req->callback == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
req->callback("FAILED", 0, 0, req->data);
|
|
|
|
req->callback = NULL;
|
|
|
|
req->data = NULL;
|
|
|
|
}
|
|
|
|
|
2016-03-08 09:53:25 +01:00
|
|
|
static void
|
2016-03-13 09:23:41 +01:00
|
|
|
handle_dns_stat_failure(uint32_t xid)
|
2016-03-08 09:53:25 +01:00
|
|
|
{
|
2016-03-13 09:23:41 +01:00
|
|
|
struct dnsstatreq *req = rb_dictionary_retrieve(stat_dict, RB_UINT_TO_POINTER(xid));
|
|
|
|
s_assert(req);
|
2016-03-08 09:53:25 +01:00
|
|
|
|
|
|
|
if(req->callback == NULL)
|
|
|
|
return;
|
|
|
|
|
2016-03-13 09:23:41 +01:00
|
|
|
req->callback(1, NULL, 2, req->data);
|
2016-03-08 09:53:25 +01:00
|
|
|
req->callback = NULL;
|
|
|
|
req->data = NULL;
|
|
|
|
}
|
|
|
|
|
2016-03-13 09:23:41 +01:00
|
|
|
|
2016-01-06 11:28:34 +01:00
|
|
|
void
|
2016-03-13 09:23:41 +01:00
|
|
|
cancel_lookup(uint32_t xid)
|
2016-01-06 11:28:34 +01:00
|
|
|
{
|
2016-03-13 09:23:41 +01:00
|
|
|
struct dnsreq *req = rb_dictionary_retrieve(query_dict, RB_UINT_TO_POINTER(xid));
|
|
|
|
s_assert(req);
|
|
|
|
req->callback = NULL;
|
|
|
|
req->data = NULL;
|
2016-01-06 11:28:34 +01:00
|
|
|
}
|
|
|
|
|
2016-03-08 09:53:25 +01:00
|
|
|
void
|
2016-03-13 09:23:41 +01:00
|
|
|
cancel_dns_stats(uint32_t xid)
|
2016-03-08 09:53:25 +01:00
|
|
|
{
|
2016-03-13 09:23:41 +01:00
|
|
|
struct dnsstatreq *req = rb_dictionary_retrieve(stat_dict, RB_UINT_TO_POINTER(xid));
|
|
|
|
s_assert(req);
|
|
|
|
req->callback = NULL;
|
|
|
|
req->data = NULL;
|
2016-03-08 09:53:25 +01:00
|
|
|
}
|
|
|
|
|
2016-03-13 09:23:41 +01:00
|
|
|
|
|
|
|
uint32_t
|
2016-01-06 11:28:34 +01:00
|
|
|
lookup_hostname(const char *hostname, int aftype, DNSCB callback, void *data)
|
|
|
|
{
|
2016-03-13 09:23:41 +01:00
|
|
|
struct dnsreq *req = rb_malloc(sizeof(struct dnsreq));
|
2016-01-06 11:28:34 +01:00
|
|
|
int aft;
|
2016-04-03 03:19:37 +02:00
|
|
|
uint32_t rid = assign_id(&query_id);
|
2016-03-13 09:23:41 +01:00
|
|
|
|
2016-01-06 11:28:34 +01:00
|
|
|
check_authd();
|
|
|
|
|
2016-03-13 09:23:41 +01:00
|
|
|
rb_dictionary_add(query_dict, RB_UINT_TO_POINTER(rid), req);
|
2016-01-06 11:28:34 +01:00
|
|
|
|
|
|
|
req->callback = callback;
|
|
|
|
req->data = data;
|
|
|
|
|
|
|
|
#ifdef RB_IPV6
|
|
|
|
if(aftype == AF_INET6)
|
|
|
|
aft = 6;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
aft = 4;
|
|
|
|
|
2016-03-13 09:23:41 +01:00
|
|
|
submit_dns(rid, aft == 4 ? DNS_HOST_IPV4 : DNS_HOST_IPV6, hostname);
|
|
|
|
return (rid);
|
2016-01-06 11:28:34 +01:00
|
|
|
}
|
|
|
|
|
2016-03-13 09:23:41 +01:00
|
|
|
uint32_t
|
2016-01-06 11:28:34 +01:00
|
|
|
lookup_ip(const char *addr, int aftype, DNSCB callback, void *data)
|
|
|
|
{
|
2016-03-13 09:23:41 +01:00
|
|
|
struct dnsreq *req = rb_malloc(sizeof(struct dnsreq));
|
2016-01-06 11:28:34 +01:00
|
|
|
int aft;
|
2016-04-03 03:19:37 +02:00
|
|
|
uint32_t rid = assign_id(&query_id);
|
2016-01-06 11:28:34 +01:00
|
|
|
|
2016-03-13 09:23:41 +01:00
|
|
|
check_authd();
|
2016-03-08 09:53:25 +01:00
|
|
|
|
2016-03-13 09:23:41 +01:00
|
|
|
rb_dictionary_add(query_dict, RB_UINT_TO_POINTER(rid), req);
|
2016-01-06 11:28:34 +01:00
|
|
|
|
|
|
|
req->callback = callback;
|
|
|
|
req->data = data;
|
|
|
|
|
|
|
|
#ifdef RB_IPV6
|
|
|
|
if(aftype == AF_INET6)
|
|
|
|
aft = 6;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
aft = 4;
|
|
|
|
|
2016-03-13 09:23:41 +01:00
|
|
|
submit_dns(rid, aft == 4 ? DNS_REVERSE_IPV4 : DNS_REVERSE_IPV6, addr);
|
|
|
|
return (rid);
|
2016-01-06 11:28:34 +01:00
|
|
|
}
|
|
|
|
|
2016-03-13 09:23:41 +01:00
|
|
|
uint32_t
|
2016-03-08 09:53:25 +01:00
|
|
|
get_nameservers(DNSLISTCB callback, void *data)
|
|
|
|
{
|
2016-03-13 09:23:41 +01:00
|
|
|
struct dnsstatreq *req = rb_malloc(sizeof(struct dnsstatreq));
|
2016-04-03 03:19:37 +02:00
|
|
|
uint32_t qid = assign_id(&stat_id);
|
2016-03-13 09:23:41 +01:00
|
|
|
|
2016-03-08 09:53:25 +01:00
|
|
|
check_authd();
|
|
|
|
|
2016-03-13 09:23:41 +01:00
|
|
|
rb_dictionary_add(stat_dict, RB_UINT_TO_POINTER(qid), req);
|
2016-03-08 09:53:25 +01:00
|
|
|
|
|
|
|
req->callback = callback;
|
|
|
|
req->data = data;
|
|
|
|
|
2016-03-13 09:23:41 +01:00
|
|
|
submit_dns_stat(qid);
|
|
|
|
return (qid);
|
2016-03-08 09:53:25 +01:00
|
|
|
}
|
|
|
|
|
2016-03-13 09:23:41 +01:00
|
|
|
|
2016-01-06 11:41:57 +01:00
|
|
|
void
|
2016-01-08 12:44:05 +01:00
|
|
|
dns_results_callback(const char *callid, const char *status, const char *type, const char *results)
|
2016-01-06 11:28:34 +01:00
|
|
|
{
|
|
|
|
struct dnsreq *req;
|
2016-03-13 09:23:41 +01:00
|
|
|
uint32_t rid;
|
2016-01-06 11:28:34 +01:00
|
|
|
int st;
|
|
|
|
int aft;
|
2016-03-13 09:23:41 +01:00
|
|
|
long lrid = strtol(callid, NULL, 16);
|
2016-01-06 11:28:34 +01:00
|
|
|
|
2016-03-13 09:23:41 +01:00
|
|
|
if(lrid > UINT32_MAX)
|
2016-01-06 11:28:34 +01:00
|
|
|
return;
|
2016-03-13 09:23:41 +01:00
|
|
|
|
|
|
|
rid = (uint32_t)lrid;
|
|
|
|
req = rb_dictionary_retrieve(query_dict, RB_UINT_TO_POINTER(rid));
|
|
|
|
if(req == NULL)
|
|
|
|
return;
|
|
|
|
|
2016-03-08 09:53:25 +01:00
|
|
|
st = (*status == 'O');
|
2016-01-08 12:44:05 +01:00
|
|
|
aft = *type == '6' || *type == 'S' ? 6 : 4;
|
2016-01-06 11:28:34 +01:00
|
|
|
if(req->callback == NULL)
|
|
|
|
{
|
|
|
|
/* got cancelled..oh well */
|
|
|
|
req->data = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#ifdef RB_IPV6
|
|
|
|
if(aft == 6)
|
|
|
|
aft = AF_INET6;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
aft = AF_INET;
|
|
|
|
|
|
|
|
req->callback(results, st, aft, req->data);
|
2016-03-13 09:23:41 +01:00
|
|
|
|
|
|
|
rb_free(req);
|
|
|
|
rb_dictionary_delete(query_dict, RB_UINT_TO_POINTER(rid));
|
2016-01-06 11:28:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-03-08 09:53:25 +01:00
|
|
|
dns_stats_results_callback(const char *callid, const char *status, int resc, const char *resv[])
|
2016-01-06 11:28:34 +01:00
|
|
|
{
|
2016-03-08 09:53:25 +01:00
|
|
|
struct dnsstatreq *req;
|
2016-03-13 09:23:41 +01:00
|
|
|
uint32_t qid;
|
2016-03-22 02:52:40 +01:00
|
|
|
int st;
|
2016-03-13 09:23:41 +01:00
|
|
|
long lqid = strtol(callid, NULL, 16);
|
2016-03-08 09:53:25 +01:00
|
|
|
|
2016-03-13 09:23:41 +01:00
|
|
|
if(lqid > UINT32_MAX)
|
2016-03-08 09:53:25 +01:00
|
|
|
return;
|
2016-03-13 09:23:41 +01:00
|
|
|
|
|
|
|
qid = (uint32_t)lqid;
|
|
|
|
req = rb_dictionary_retrieve(stat_dict, RB_UINT_TO_POINTER(qid));
|
|
|
|
|
|
|
|
s_assert(req);
|
2016-03-08 09:53:25 +01:00
|
|
|
|
|
|
|
if(req->callback == NULL)
|
2016-01-06 11:28:34 +01:00
|
|
|
{
|
2016-03-08 09:53:25 +01:00
|
|
|
req->data = NULL;
|
|
|
|
return;
|
2016-01-06 11:28:34 +01:00
|
|
|
}
|
2016-03-08 09:53:25 +01:00
|
|
|
|
|
|
|
switch(*status)
|
|
|
|
{
|
|
|
|
case 'Y':
|
|
|
|
st = 0;
|
|
|
|
break;
|
|
|
|
case 'X':
|
|
|
|
/* Error */
|
|
|
|
st = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Shouldn't happen... */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Query complete */
|
2016-03-13 09:23:41 +01:00
|
|
|
req->callback(resc, resv, st, req->data);
|
2016-03-08 09:53:25 +01:00
|
|
|
|
2016-03-13 09:23:41 +01:00
|
|
|
rb_free(req);
|
|
|
|
rb_dictionary_delete(stat_dict, RB_UINT_TO_POINTER(qid));
|
2016-03-08 09:53:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-03-13 09:23:41 +01:00
|
|
|
stats_results_callback(int resc, const char *resv[], int status, void *data)
|
2016-03-08 09:53:25 +01:00
|
|
|
{
|
|
|
|
if(status == 0)
|
|
|
|
{
|
2016-03-08 10:22:25 +01:00
|
|
|
rb_dlink_node *n, *tn;
|
|
|
|
|
|
|
|
RB_DLINK_FOREACH_SAFE(n, tn, nameservers.head)
|
|
|
|
{
|
|
|
|
/* Clean up old nameservers */
|
|
|
|
rb_free(n->data);
|
|
|
|
rb_dlinkDestroy(n, &nameservers);
|
|
|
|
}
|
|
|
|
|
2016-03-08 09:53:25 +01:00
|
|
|
for(int i = 0; i < resc; i++)
|
2016-03-08 10:22:25 +01:00
|
|
|
rb_dlinkAddAlloc(rb_strdup(resv[i]), &nameservers);
|
2016-03-08 09:53:25 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-08 10:22:25 +01:00
|
|
|
const char *error = resc ? resv[resc] : "Unknown error";
|
2016-03-08 20:47:41 +01:00
|
|
|
iwarn("Error getting DNS servers: %s", error);
|
2016-03-08 09:53:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-13 09:23:41 +01:00
|
|
|
|
2016-03-08 10:22:25 +01:00
|
|
|
void
|
2016-03-13 09:23:41 +01:00
|
|
|
init_dns(void)
|
2016-03-08 10:22:25 +01:00
|
|
|
{
|
2016-03-13 09:23:41 +01:00
|
|
|
query_dict = rb_dictionary_create("dns queries", rb_uint32cmp);
|
|
|
|
stat_dict = rb_dictionary_create("dns stat queries", rb_uint32cmp);
|
|
|
|
(void)get_nameservers(stats_results_callback, NULL);
|
2016-01-06 11:28:34 +01:00
|
|
|
}
|
|
|
|
|
2016-03-13 09:23:41 +01:00
|
|
|
void
|
2016-03-09 10:46:04 +01:00
|
|
|
reload_nameservers(void)
|
|
|
|
{
|
2016-03-13 09:23:41 +01:00
|
|
|
check_authd();
|
2016-03-26 03:00:13 +01:00
|
|
|
rb_helper_write(authd_helper, "R D");
|
2016-03-13 09:23:41 +01:00
|
|
|
(void)get_nameservers(stats_results_callback, NULL);
|
2016-03-09 10:46:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-06 11:28:34 +01:00
|
|
|
static void
|
2016-03-13 09:23:41 +01:00
|
|
|
submit_dns(uint32_t nid, char type, const char *addr)
|
2016-01-06 11:28:34 +01:00
|
|
|
{
|
|
|
|
if(authd_helper == NULL)
|
|
|
|
{
|
|
|
|
handle_dns_failure(nid);
|
|
|
|
return;
|
|
|
|
}
|
2016-01-08 12:44:05 +01:00
|
|
|
rb_helper_write(authd_helper, "D %x %c %s", nid, type, addr);
|
2016-01-06 11:28:34 +01:00
|
|
|
}
|
2016-03-08 09:53:25 +01:00
|
|
|
|
|
|
|
static void
|
2016-03-13 09:23:41 +01:00
|
|
|
submit_dns_stat(uint32_t nid)
|
2016-03-08 09:53:25 +01:00
|
|
|
{
|
|
|
|
if(authd_helper == NULL)
|
|
|
|
{
|
|
|
|
handle_dns_stat_failure(nid);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rb_helper_write(authd_helper, "S %x D", nid);
|
|
|
|
}
|