1 |
adx |
30 |
/* |
2 |
michael |
2916 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
|
|
* |
4 |
|
|
* Copyright (c) 1997-2014 ircd-hybrid development team |
5 |
|
|
* |
6 |
|
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU General Public License as published by |
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* This program is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
19 |
|
|
* USA |
20 |
|
|
*/ |
21 |
|
|
|
22 |
michael |
3322 |
/*! \file res.c |
23 |
michael |
2916 |
* \brief ircd resolver functions |
24 |
|
|
* \version $Id$ |
25 |
|
|
*/ |
26 |
|
|
|
27 |
|
|
/* |
28 |
michael |
4312 |
* A rewrite of Darren Reed's original res.c As there is nothing |
29 |
|
|
* left of Darren's original code, this is now licensed by the hybrid group. |
30 |
adx |
30 |
* (Well, some of the function names are the same, and bits of the structs..) |
31 |
|
|
* You can use it where it is useful, free even. Buy us a beer and stuff. |
32 |
|
|
* |
33 |
|
|
* The authors takes no responsibility for any damage or loss |
34 |
|
|
* of property which results from the use of this software. |
35 |
|
|
* |
36 |
|
|
* July 1999 - Rewrote a bunch of stuff here. Change hostent builder code, |
37 |
|
|
* added callbacks and reference counting of returned hostents. |
38 |
|
|
* --Bleep (Thomas Helvey <tomh@inxpress.net>) |
39 |
|
|
* |
40 |
|
|
* This was all needlessly complicated for irc. Simplified. No more hostent |
41 |
michael |
4312 |
* All we really care about is the IP -> hostname mappings. That's all. |
42 |
adx |
30 |
* |
43 |
|
|
* Apr 28, 2003 --cryogen and Dianora |
44 |
|
|
*/ |
45 |
|
|
|
46 |
|
|
#include "stdinc.h" |
47 |
michael |
1011 |
#include "list.h" |
48 |
adx |
30 |
#include "client.h" |
49 |
|
|
#include "event.h" |
50 |
|
|
#include "irc_string.h" |
51 |
|
|
#include "ircd.h" |
52 |
|
|
#include "numeric.h" |
53 |
michael |
982 |
#include "rng_mt.h" |
54 |
adx |
30 |
#include "fdlist.h" |
55 |
|
|
#include "s_bsd.h" |
56 |
michael |
1309 |
#include "log.h" |
57 |
michael |
3347 |
#include "misc.h" |
58 |
adx |
30 |
#include "send.h" |
59 |
|
|
#include "memory.h" |
60 |
michael |
1654 |
#include "mempool.h" |
61 |
michael |
3322 |
#include "res.h" |
62 |
|
|
#include "reslib.h" |
63 |
adx |
30 |
|
64 |
|
|
#if (CHAR_BIT != 8) |
65 |
michael |
2916 |
#error this code needs to be able to address individual octets |
66 |
adx |
30 |
#endif |
67 |
|
|
|
68 |
|
|
static PF res_readreply; |
69 |
|
|
|
70 |
michael |
4312 |
#define MAXPACKET 1024 /**< rfc says 512 but we expand names so ... */ |
71 |
|
|
#define AR_TTL 600 /**< TTL in seconds for dns cache entries */ |
72 |
adx |
30 |
|
73 |
michael |
4312 |
/* |
74 |
|
|
* RFC 1104/1105 wasn't very helpful about what these fields |
75 |
adx |
30 |
* should be named, so for now, we'll just name them this way. |
76 |
michael |
4312 |
* We probably should look at what named calls them or something. |
77 |
adx |
30 |
*/ |
78 |
|
|
#define TYPE_SIZE (size_t)2 |
79 |
|
|
#define CLASS_SIZE (size_t)2 |
80 |
|
|
#define TTL_SIZE (size_t)4 |
81 |
|
|
#define RDLENGTH_SIZE (size_t)2 |
82 |
|
|
#define ANSWER_FIXED_SIZE (TYPE_SIZE + CLASS_SIZE + TTL_SIZE + RDLENGTH_SIZE) |
83 |
|
|
|
84 |
michael |
2916 |
typedef enum |
85 |
adx |
30 |
{ |
86 |
michael |
4312 |
REQ_IDLE, /**< We're doing not much at all */ |
87 |
|
|
REQ_PTR, /**< Looking up a PTR */ |
88 |
|
|
REQ_A, /**< Looking up an A, possibly because AAAA failed */ |
89 |
adx |
30 |
#ifdef IPV6 |
90 |
michael |
4312 |
REQ_AAAA, /**< Looking up an AAAA */ |
91 |
adx |
30 |
#endif |
92 |
michael |
4312 |
REQ_CNAME /**< We got a CNAME in response, we better get a real answer next */ |
93 |
adx |
30 |
} request_state; |
94 |
|
|
|
95 |
michael |
2916 |
struct reslist |
96 |
adx |
30 |
{ |
97 |
michael |
4312 |
dlink_node node; /**< Doubly linked list node. */ |
98 |
|
|
int id; /**< Request ID (from request header). */ |
99 |
|
|
int sent; /**< Number of requests sent */ |
100 |
|
|
request_state state; /**< State the resolver machine is in */ |
101 |
|
|
char type; /**< Current request type. */ |
102 |
|
|
char retries; /**< Retry counter */ |
103 |
|
|
unsigned int sends; /**< Number of sends (>1 means resent). */ |
104 |
|
|
char resend; /**< Send flag; 0 == don't resend. */ |
105 |
|
|
time_t sentat; /**< Timestamp we last sent this request. */ |
106 |
|
|
time_t timeout; /**< When this request times out. */ |
107 |
|
|
struct irc_ssaddr addr; /**< Address for this request. */ |
108 |
|
|
char *name; /**< Hostname for this request. */ |
109 |
|
|
dns_callback_fnc callback; /**< Callback function on completion. */ |
110 |
|
|
void *callback_ctx; /**< Context pointer for callback. */ |
111 |
adx |
30 |
}; |
112 |
|
|
|
113 |
|
|
static fde_t ResolverFileDescriptor; |
114 |
michael |
3235 |
static dlink_list request_list; |
115 |
|
|
static mp_pool_t *dns_pool; |
116 |
adx |
30 |
|
117 |
|
|
|
118 |
michael |
4312 |
/* |
119 |
|
|
* rem_request - remove a request from the list. |
120 |
|
|
* This must also free any memory that has been allocated for |
121 |
|
|
* temporary storage of DNS results. |
122 |
|
|
*/ |
123 |
|
|
static void |
124 |
|
|
rem_request(struct reslist *request) |
125 |
|
|
{ |
126 |
|
|
dlinkDelete(&request->node, &request_list); |
127 |
adx |
30 |
|
128 |
michael |
4312 |
MyFree(request->name); |
129 |
|
|
mp_pool_release(request); |
130 |
|
|
} |
131 |
|
|
|
132 |
adx |
30 |
/* |
133 |
michael |
4312 |
* make_request - Create a DNS request record for the server. |
134 |
|
|
*/ |
135 |
|
|
static struct reslist * |
136 |
|
|
make_request(dns_callback_fnc callback, void *ctx) |
137 |
|
|
{ |
138 |
|
|
struct reslist *request = mp_pool_get(dns_pool); |
139 |
|
|
|
140 |
|
|
request->sentat = CurrentTime; |
141 |
|
|
request->retries = 2; |
142 |
|
|
request->resend = 1; |
143 |
|
|
request->timeout = 4; /* Start at 4 and exponential inc. */ |
144 |
|
|
request->state = REQ_IDLE; |
145 |
|
|
request->callback = callback; |
146 |
|
|
request->callback_ctx = ctx; |
147 |
|
|
|
148 |
|
|
dlinkAdd(request, &request->node, &request_list); |
149 |
|
|
return request; |
150 |
|
|
} |
151 |
|
|
|
152 |
|
|
/* |
153 |
adx |
30 |
* int |
154 |
|
|
* res_ourserver(inp) |
155 |
|
|
* looks up "inp" in irc_nsaddr_list[] |
156 |
|
|
* returns: |
157 |
|
|
* 0 : not found |
158 |
|
|
* >0 : found |
159 |
|
|
* author: |
160 |
|
|
* paul vixie, 29may94 |
161 |
|
|
* revised for ircd, cryogen(stu) may03 |
162 |
|
|
*/ |
163 |
|
|
static int |
164 |
michael |
2916 |
res_ourserver(const struct irc_ssaddr *inp) |
165 |
adx |
30 |
{ |
166 |
|
|
#ifdef IPV6 |
167 |
michael |
992 |
const struct sockaddr_in6 *v6; |
168 |
|
|
const struct sockaddr_in6 *v6in = (const struct sockaddr_in6 *)inp; |
169 |
adx |
30 |
#endif |
170 |
michael |
992 |
const struct sockaddr_in *v4; |
171 |
michael |
2916 |
const struct sockaddr_in *v4in = (const struct sockaddr_in *)inp; |
172 |
adx |
30 |
|
173 |
michael |
3297 |
for (unsigned int i = 0; i < irc_nscount; ++i) |
174 |
adx |
30 |
{ |
175 |
michael |
3235 |
const struct irc_ssaddr *srv = &irc_nsaddr_list[i]; |
176 |
adx |
30 |
#ifdef IPV6 |
177 |
michael |
992 |
v6 = (const struct sockaddr_in6 *)srv; |
178 |
adx |
30 |
#endif |
179 |
michael |
992 |
v4 = (const struct sockaddr_in *)srv; |
180 |
adx |
30 |
|
181 |
michael |
4312 |
/* |
182 |
|
|
* Could probably just memcmp(srv, inp, srv.ss_len) here |
183 |
adx |
30 |
* but we'll air on the side of caution - stu |
184 |
|
|
*/ |
185 |
|
|
switch (srv->ss.ss_family) |
186 |
|
|
{ |
187 |
|
|
#ifdef IPV6 |
188 |
|
|
case AF_INET6: |
189 |
|
|
if (srv->ss.ss_family == inp->ss.ss_family) |
190 |
|
|
if (v6->sin6_port == v6in->sin6_port) |
191 |
michael |
1346 |
if (!memcmp(&v6->sin6_addr.s6_addr, &v6in->sin6_addr.s6_addr, |
192 |
|
|
sizeof(struct in6_addr))) |
193 |
michael |
1124 |
return 1; |
194 |
adx |
30 |
break; |
195 |
|
|
#endif |
196 |
|
|
case AF_INET: |
197 |
|
|
if (srv->ss.ss_family == inp->ss.ss_family) |
198 |
|
|
if (v4->sin_port == v4in->sin_port) |
199 |
michael |
1346 |
if (v4->sin_addr.s_addr == v4in->sin_addr.s_addr) |
200 |
michael |
1124 |
return 1; |
201 |
adx |
30 |
break; |
202 |
|
|
default: |
203 |
|
|
break; |
204 |
|
|
} |
205 |
|
|
} |
206 |
|
|
|
207 |
michael |
1124 |
return 0; |
208 |
adx |
30 |
} |
209 |
|
|
|
210 |
|
|
/* |
211 |
|
|
* start_resolver - do everything we need to read the resolv.conf file |
212 |
|
|
* and initialize the resolver file descriptor if needed |
213 |
|
|
*/ |
214 |
|
|
static void |
215 |
|
|
start_resolver(void) |
216 |
|
|
{ |
217 |
|
|
irc_res_init(); |
218 |
|
|
|
219 |
|
|
if (!ResolverFileDescriptor.flags.open) |
220 |
|
|
{ |
221 |
|
|
if (comm_open(&ResolverFileDescriptor, irc_nsaddr_list[0].ss.ss_family, |
222 |
|
|
SOCK_DGRAM, 0, "Resolver socket") == -1) |
223 |
|
|
return; |
224 |
|
|
|
225 |
|
|
/* At the moment, the resolver FD data is global .. */ |
226 |
michael |
3298 |
comm_setselect(&ResolverFileDescriptor, COMM_SELECT_READ, res_readreply, NULL, 0); |
227 |
adx |
30 |
} |
228 |
|
|
} |
229 |
|
|
|
230 |
|
|
/* |
231 |
|
|
* restart_resolver - reread resolv.conf, reopen socket |
232 |
|
|
*/ |
233 |
|
|
void |
234 |
|
|
restart_resolver(void) |
235 |
|
|
{ |
236 |
|
|
fd_close(&ResolverFileDescriptor); |
237 |
|
|
start_resolver(); |
238 |
|
|
} |
239 |
|
|
|
240 |
|
|
/* |
241 |
michael |
2916 |
* delete_resolver_queries - cleanup outstanding queries |
242 |
adx |
30 |
* for which there no longer exist clients or conf lines. |
243 |
|
|
*/ |
244 |
|
|
void |
245 |
michael |
992 |
delete_resolver_queries(const void *vptr) |
246 |
adx |
30 |
{ |
247 |
michael |
3235 |
dlink_node *ptr = NULL, *ptr_next = NULL; |
248 |
adx |
30 |
|
249 |
michael |
3235 |
DLINK_FOREACH_SAFE(ptr, ptr_next, request_list.head) |
250 |
adx |
30 |
{ |
251 |
michael |
997 |
struct reslist *request = ptr->data; |
252 |
|
|
|
253 |
|
|
if (request->callback_ctx == vptr) |
254 |
|
|
rem_request(request); |
255 |
adx |
30 |
} |
256 |
|
|
} |
257 |
|
|
|
258 |
|
|
/* |
259 |
|
|
* send_res_msg - sends msg to all nameservers found in the "_res" structure. |
260 |
|
|
* This should reflect /etc/resolv.conf. We will get responses |
261 |
|
|
* which arent needed but is easier than checking to see if nameserver |
262 |
michael |
4299 |
* isn't present. Returns number of messages successfully sent to |
263 |
adx |
30 |
* nameservers or -1 if no successful sends. |
264 |
|
|
*/ |
265 |
|
|
static int |
266 |
michael |
3297 |
send_res_msg(const char *msg, int len, unsigned int rcount) |
267 |
adx |
30 |
{ |
268 |
|
|
int sent = 0; |
269 |
michael |
3297 |
unsigned int max_queries = IRCD_MIN(irc_nscount, rcount); |
270 |
adx |
30 |
|
271 |
|
|
/* RES_PRIMARY option is not implemented |
272 |
|
|
* if (res.options & RES_PRIMARY || 0 == max_queries) |
273 |
|
|
*/ |
274 |
|
|
if (max_queries == 0) |
275 |
|
|
max_queries = 1; |
276 |
|
|
|
277 |
michael |
3297 |
for (unsigned int i = 0; i < max_queries; ++i) |
278 |
adx |
30 |
{ |
279 |
michael |
2916 |
if (sendto(ResolverFileDescriptor.fd, msg, len, 0, |
280 |
|
|
(struct sockaddr*)&(irc_nsaddr_list[i]), |
281 |
|
|
irc_nsaddr_list[i].ss_len) == len) |
282 |
adx |
30 |
++sent; |
283 |
|
|
} |
284 |
|
|
|
285 |
michael |
1124 |
return sent; |
286 |
adx |
30 |
} |
287 |
|
|
|
288 |
|
|
/* |
289 |
|
|
* find_id - find a dns request id (id is determined by dn_mkquery) |
290 |
|
|
*/ |
291 |
|
|
static struct reslist * |
292 |
|
|
find_id(int id) |
293 |
|
|
{ |
294 |
michael |
1124 |
dlink_node *ptr = NULL; |
295 |
adx |
30 |
|
296 |
|
|
DLINK_FOREACH(ptr, request_list.head) |
297 |
|
|
{ |
298 |
michael |
1124 |
struct reslist *request = ptr->data; |
299 |
adx |
30 |
|
300 |
|
|
if (request->id == id) |
301 |
michael |
1124 |
return request; |
302 |
adx |
30 |
} |
303 |
|
|
|
304 |
michael |
1124 |
return NULL; |
305 |
adx |
30 |
} |
306 |
|
|
|
307 |
michael |
2916 |
/* |
308 |
michael |
4312 |
* query_name - generate a query based on class, type and name. |
309 |
adx |
30 |
*/ |
310 |
michael |
4312 |
static void |
311 |
|
|
query_name(const char *name, int query_class, int type, |
312 |
|
|
struct reslist *request) |
313 |
adx |
30 |
{ |
314 |
michael |
4312 |
char buf[MAXPACKET]; |
315 |
|
|
int request_len = 0; |
316 |
adx |
30 |
|
317 |
michael |
4312 |
memset(buf, 0, sizeof(buf)); |
318 |
adx |
30 |
|
319 |
michael |
4312 |
if ((request_len = irc_res_mkquery(name, query_class, type, |
320 |
|
|
(unsigned char *)buf, sizeof(buf))) > 0) |
321 |
|
|
{ |
322 |
|
|
HEADER *header = (HEADER *)buf; |
323 |
|
|
|
324 |
|
|
/* |
325 |
|
|
* Generate an unique id. |
326 |
|
|
* NOTE: we don't have to worry about converting this to and from |
327 |
|
|
* network byte order, the nameserver does not interpret this value |
328 |
|
|
* and returns it unchanged. |
329 |
|
|
*/ |
330 |
|
|
do |
331 |
|
|
header->id = (header->id + genrand_int32()) & 0xFFFF; |
332 |
|
|
while (find_id(header->id)); |
333 |
|
|
|
334 |
|
|
request->id = header->id; |
335 |
|
|
++request->sends; |
336 |
|
|
|
337 |
|
|
request->sent += send_res_msg(buf, request_len, request->sends); |
338 |
|
|
} |
339 |
adx |
30 |
} |
340 |
|
|
|
341 |
|
|
/* |
342 |
|
|
* do_query_name - nameserver lookup name |
343 |
|
|
*/ |
344 |
|
|
static void |
345 |
michael |
992 |
do_query_name(dns_callback_fnc callback, void *ctx, const char *name, |
346 |
adx |
30 |
struct reslist *request, int type) |
347 |
|
|
{ |
348 |
|
|
char host_name[HOSTLEN + 1]; |
349 |
|
|
|
350 |
michael |
998 |
strlcpy(host_name, name, sizeof(host_name)); |
351 |
adx |
30 |
|
352 |
|
|
if (request == NULL) |
353 |
|
|
{ |
354 |
michael |
992 |
request = make_request(callback, ctx); |
355 |
michael |
3505 |
request->name = MyCalloc(strlen(host_name) + 1); |
356 |
adx |
30 |
request->type = type; |
357 |
|
|
strcpy(request->name, host_name); |
358 |
|
|
#ifdef IPV6 |
359 |
michael |
984 |
if (type != T_A) |
360 |
|
|
request->state = REQ_AAAA; |
361 |
adx |
30 |
else |
362 |
michael |
984 |
#endif |
363 |
adx |
30 |
request->state = REQ_A; |
364 |
|
|
} |
365 |
|
|
|
366 |
|
|
request->type = type; |
367 |
|
|
query_name(host_name, C_IN, type, request); |
368 |
|
|
} |
369 |
|
|
|
370 |
|
|
/* |
371 |
|
|
* do_query_number - Use this to do reverse IP# lookups. |
372 |
|
|
*/ |
373 |
|
|
static void |
374 |
michael |
992 |
do_query_number(dns_callback_fnc callback, void *ctx, |
375 |
|
|
const struct irc_ssaddr *addr, |
376 |
adx |
30 |
struct reslist *request) |
377 |
|
|
{ |
378 |
michael |
3298 |
char ipbuf[128] = ""; |
379 |
michael |
985 |
|
380 |
adx |
30 |
if (addr->ss.ss_family == AF_INET) |
381 |
|
|
{ |
382 |
michael |
992 |
const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr; |
383 |
michael |
3298 |
const unsigned char *cp = (const unsigned char *)&v4->sin_addr.s_addr; |
384 |
adx |
30 |
|
385 |
michael |
1124 |
snprintf(ipbuf, sizeof(ipbuf), "%u.%u.%u.%u.in-addr.arpa.", |
386 |
|
|
(unsigned int)(cp[3]), (unsigned int)(cp[2]), |
387 |
|
|
(unsigned int)(cp[1]), (unsigned int)(cp[0])); |
388 |
adx |
30 |
} |
389 |
|
|
#ifdef IPV6 |
390 |
|
|
else if (addr->ss.ss_family == AF_INET6) |
391 |
|
|
{ |
392 |
michael |
992 |
const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr; |
393 |
michael |
3298 |
const unsigned char *cp = (const unsigned char *)&v6->sin6_addr.s6_addr; |
394 |
adx |
30 |
|
395 |
michael |
1124 |
snprintf(ipbuf, sizeof(ipbuf), |
396 |
|
|
"%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x." |
397 |
|
|
"%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa.", |
398 |
|
|
(unsigned int)(cp[15] & 0xf), (unsigned int)(cp[15] >> 4), |
399 |
|
|
(unsigned int)(cp[14] & 0xf), (unsigned int)(cp[14] >> 4), |
400 |
|
|
(unsigned int)(cp[13] & 0xf), (unsigned int)(cp[13] >> 4), |
401 |
|
|
(unsigned int)(cp[12] & 0xf), (unsigned int)(cp[12] >> 4), |
402 |
|
|
(unsigned int)(cp[11] & 0xf), (unsigned int)(cp[11] >> 4), |
403 |
|
|
(unsigned int)(cp[10] & 0xf), (unsigned int)(cp[10] >> 4), |
404 |
|
|
(unsigned int)(cp[9] & 0xf), (unsigned int)(cp[9] >> 4), |
405 |
|
|
(unsigned int)(cp[8] & 0xf), (unsigned int)(cp[8] >> 4), |
406 |
|
|
(unsigned int)(cp[7] & 0xf), (unsigned int)(cp[7] >> 4), |
407 |
|
|
(unsigned int)(cp[6] & 0xf), (unsigned int)(cp[6] >> 4), |
408 |
|
|
(unsigned int)(cp[5] & 0xf), (unsigned int)(cp[5] >> 4), |
409 |
|
|
(unsigned int)(cp[4] & 0xf), (unsigned int)(cp[4] >> 4), |
410 |
|
|
(unsigned int)(cp[3] & 0xf), (unsigned int)(cp[3] >> 4), |
411 |
|
|
(unsigned int)(cp[2] & 0xf), (unsigned int)(cp[2] >> 4), |
412 |
|
|
(unsigned int)(cp[1] & 0xf), (unsigned int)(cp[1] >> 4), |
413 |
|
|
(unsigned int)(cp[0] & 0xf), (unsigned int)(cp[0] >> 4)); |
414 |
adx |
30 |
} |
415 |
|
|
#endif |
416 |
|
|
if (request == NULL) |
417 |
|
|
{ |
418 |
michael |
992 |
request = make_request(callback, ctx); |
419 |
adx |
30 |
request->type = T_PTR; |
420 |
|
|
memcpy(&request->addr, addr, sizeof(struct irc_ssaddr)); |
421 |
michael |
3505 |
request->name = MyCalloc(HOSTLEN + 1); |
422 |
adx |
30 |
} |
423 |
|
|
|
424 |
|
|
query_name(ipbuf, C_IN, T_PTR, request); |
425 |
|
|
} |
426 |
|
|
|
427 |
|
|
/* |
428 |
michael |
4312 |
* gethost_byname_type - get host address from name |
429 |
|
|
* |
430 |
adx |
30 |
*/ |
431 |
michael |
4312 |
void |
432 |
|
|
gethost_byname_type(dns_callback_fnc callback, void *ctx, const char *name, int type) |
433 |
adx |
30 |
{ |
434 |
michael |
4312 |
assert(name); |
435 |
|
|
do_query_name(callback, ctx, name, NULL, type); |
436 |
|
|
} |
437 |
adx |
30 |
|
438 |
michael |
4312 |
/* |
439 |
|
|
* gethost_byname - wrapper for _type - send T_AAAA first if IPV6 supported |
440 |
|
|
*/ |
441 |
|
|
void |
442 |
|
|
gethost_byname(dns_callback_fnc callback, void *ctx, const char *name) |
443 |
|
|
{ |
444 |
|
|
#ifdef IPV6 |
445 |
|
|
gethost_byname_type(callback, ctx, name, T_AAAA); |
446 |
|
|
#else |
447 |
|
|
gethost_byname_type(callback, ctx, name, T_A); |
448 |
|
|
#endif |
449 |
|
|
} |
450 |
adx |
30 |
|
451 |
michael |
4312 |
/* |
452 |
|
|
* gethost_byaddr - get host name from address |
453 |
|
|
*/ |
454 |
|
|
void |
455 |
|
|
gethost_byaddr(dns_callback_fnc callback, void *ctx, const struct irc_ssaddr *addr) |
456 |
|
|
{ |
457 |
|
|
do_query_number(callback, ctx, addr, NULL); |
458 |
adx |
30 |
} |
459 |
|
|
|
460 |
|
|
static void |
461 |
|
|
resend_query(struct reslist *request) |
462 |
|
|
{ |
463 |
|
|
if (request->resend == 0) |
464 |
|
|
return; |
465 |
|
|
|
466 |
michael |
982 |
switch (request->type) |
467 |
adx |
30 |
{ |
468 |
|
|
case T_PTR: |
469 |
michael |
992 |
do_query_number(NULL, NULL, &request->addr, request); |
470 |
adx |
30 |
break; |
471 |
|
|
case T_A: |
472 |
michael |
992 |
do_query_name(NULL, NULL, request->name, request, request->type); |
473 |
adx |
30 |
break; |
474 |
|
|
#ifdef IPV6 |
475 |
michael |
4299 |
case T_AAAA: /* Didn't work, try A */ |
476 |
adx |
30 |
if (request->state == REQ_AAAA) |
477 |
michael |
992 |
do_query_name(NULL, NULL, request->name, request, T_A); |
478 |
adx |
30 |
#endif |
479 |
|
|
default: |
480 |
|
|
break; |
481 |
|
|
} |
482 |
|
|
} |
483 |
|
|
|
484 |
|
|
/* |
485 |
|
|
* proc_answer - process name server reply |
486 |
|
|
*/ |
487 |
|
|
static int |
488 |
michael |
1124 |
proc_answer(struct reslist *request, HEADER *header, char *buf, char *eob) |
489 |
adx |
30 |
{ |
490 |
|
|
char hostbuf[HOSTLEN + 100]; /* working buffer */ |
491 |
|
|
unsigned char *current; /* current position in buf */ |
492 |
|
|
int type; /* answer type */ |
493 |
|
|
int n; /* temp count */ |
494 |
|
|
int rd_length; |
495 |
|
|
struct sockaddr_in *v4; /* conversion */ |
496 |
|
|
#ifdef IPV6 |
497 |
|
|
struct sockaddr_in6 *v6; |
498 |
|
|
#endif |
499 |
|
|
current = (unsigned char *)buf + sizeof(HEADER); |
500 |
|
|
|
501 |
|
|
for (; header->qdcount > 0; --header->qdcount) |
502 |
|
|
{ |
503 |
|
|
if ((n = irc_dn_skipname(current, (unsigned char *)eob)) < 0) |
504 |
|
|
break; |
505 |
|
|
|
506 |
michael |
1124 |
current += (size_t)n + QFIXEDSZ; |
507 |
adx |
30 |
} |
508 |
|
|
|
509 |
|
|
/* |
510 |
michael |
4299 |
* Process each answer sent to us blech. |
511 |
adx |
30 |
*/ |
512 |
|
|
while (header->ancount > 0 && (char *)current < eob) |
513 |
|
|
{ |
514 |
|
|
header->ancount--; |
515 |
|
|
|
516 |
|
|
n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current, |
517 |
|
|
hostbuf, sizeof(hostbuf)); |
518 |
|
|
|
519 |
michael |
4299 |
if (n < 0 /* Broken message */ || n == 0 /* No more answers left */) |
520 |
michael |
1124 |
return 0; |
521 |
adx |
30 |
|
522 |
|
|
hostbuf[HOSTLEN] = '\0'; |
523 |
|
|
|
524 |
michael |
4299 |
/* |
525 |
|
|
* With Address arithmetic you have to be very anal |
526 |
adx |
30 |
* this code was not working on alpha due to that |
527 |
|
|
* (spotted by rodder/jailbird/dianora) |
528 |
|
|
*/ |
529 |
michael |
3298 |
current += (size_t)n; |
530 |
adx |
30 |
|
531 |
|
|
if (!(((char *)current + ANSWER_FIXED_SIZE) < eob)) |
532 |
|
|
break; |
533 |
|
|
|
534 |
|
|
type = irc_ns_get16(current); |
535 |
|
|
current += TYPE_SIZE; |
536 |
|
|
current += CLASS_SIZE; |
537 |
|
|
current += TTL_SIZE; |
538 |
|
|
rd_length = irc_ns_get16(current); |
539 |
|
|
current += RDLENGTH_SIZE; |
540 |
|
|
|
541 |
michael |
2916 |
/* |
542 |
|
|
* Wait to set request->type until we verify this structure |
543 |
adx |
30 |
*/ |
544 |
|
|
switch (type) |
545 |
|
|
{ |
546 |
|
|
case T_A: |
547 |
|
|
if (request->type != T_A) |
548 |
michael |
1124 |
return 0; |
549 |
adx |
30 |
|
550 |
|
|
/* |
551 |
michael |
3298 |
* Check for invalid rd_length or too many addresses |
552 |
adx |
30 |
*/ |
553 |
|
|
if (rd_length != sizeof(struct in_addr)) |
554 |
michael |
1124 |
return 0; |
555 |
|
|
|
556 |
adx |
30 |
v4 = (struct sockaddr_in *)&request->addr; |
557 |
|
|
request->addr.ss_len = sizeof(struct sockaddr_in); |
558 |
|
|
v4->sin_family = AF_INET; |
559 |
|
|
memcpy(&v4->sin_addr, current, sizeof(struct in_addr)); |
560 |
michael |
1124 |
return 1; |
561 |
adx |
30 |
break; |
562 |
|
|
#ifdef IPV6 |
563 |
|
|
case T_AAAA: |
564 |
|
|
if (request->type != T_AAAA) |
565 |
michael |
1124 |
return 0; |
566 |
|
|
|
567 |
adx |
30 |
if (rd_length != sizeof(struct in6_addr)) |
568 |
michael |
1124 |
return 0; |
569 |
|
|
|
570 |
adx |
30 |
request->addr.ss_len = sizeof(struct sockaddr_in6); |
571 |
|
|
v6 = (struct sockaddr_in6 *)&request->addr; |
572 |
|
|
v6->sin6_family = AF_INET6; |
573 |
|
|
memcpy(&v6->sin6_addr, current, sizeof(struct in6_addr)); |
574 |
michael |
1124 |
return 1; |
575 |
adx |
30 |
break; |
576 |
|
|
#endif |
577 |
|
|
case T_PTR: |
578 |
|
|
if (request->type != T_PTR) |
579 |
michael |
1124 |
return 0; |
580 |
|
|
|
581 |
adx |
30 |
n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, |
582 |
michael |
3298 |
current, hostbuf, sizeof(hostbuf)); |
583 |
michael |
4299 |
if (n < 0 /* Broken message */ || n == 0 /* No more answers left */) |
584 |
michael |
1124 |
return 0; |
585 |
adx |
30 |
|
586 |
michael |
986 |
strlcpy(request->name, hostbuf, HOSTLEN + 1); |
587 |
michael |
1124 |
return 1; |
588 |
adx |
30 |
break; |
589 |
michael |
4299 |
case T_CNAME: /* First check we already haven't started looking into a cname */ |
590 |
michael |
2916 |
if (request->type != T_PTR) |
591 |
michael |
1124 |
return 0; |
592 |
adx |
30 |
|
593 |
|
|
if (request->state == REQ_CNAME) |
594 |
|
|
{ |
595 |
|
|
n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, |
596 |
|
|
current, hostbuf, sizeof(hostbuf)); |
597 |
|
|
|
598 |
|
|
if (n < 0) |
599 |
michael |
1124 |
return 0; |
600 |
|
|
return 1; |
601 |
adx |
30 |
} |
602 |
|
|
|
603 |
|
|
request->state = REQ_CNAME; |
604 |
|
|
current += rd_length; |
605 |
|
|
break; |
606 |
michael |
2916 |
|
607 |
adx |
30 |
default: |
608 |
|
|
/* XXX I'd rather just throw away the entire bogus thing |
609 |
|
|
* but its possible its just a broken nameserver with still |
610 |
|
|
* valid answers. But lets do some rudimentary logging for now... |
611 |
|
|
*/ |
612 |
michael |
1247 |
ilog(LOG_TYPE_IRCD, "irc_res.c bogus type %d", type); |
613 |
adx |
30 |
break; |
614 |
|
|
} |
615 |
|
|
} |
616 |
|
|
|
617 |
michael |
1124 |
return 1; |
618 |
adx |
30 |
} |
619 |
|
|
|
620 |
|
|
/* |
621 |
|
|
* res_readreply - read a dns reply from the nameserver and process it. |
622 |
|
|
*/ |
623 |
|
|
static void |
624 |
|
|
res_readreply(fde_t *fd, void *data) |
625 |
|
|
{ |
626 |
michael |
4239 |
char buf[sizeof(HEADER) + MAXPACKET]; |
627 |
adx |
30 |
HEADER *header; |
628 |
|
|
struct reslist *request = NULL; |
629 |
michael |
4302 |
ssize_t rc = 0; |
630 |
adx |
30 |
socklen_t len = sizeof(struct irc_ssaddr); |
631 |
|
|
struct irc_ssaddr lsin; |
632 |
|
|
|
633 |
|
|
rc = recvfrom(fd->fd, buf, sizeof(buf), 0, (struct sockaddr *)&lsin, &len); |
634 |
|
|
|
635 |
michael |
4312 |
/* |
636 |
|
|
* Re-schedule a read *after* recvfrom, or we'll be registering |
637 |
adx |
30 |
* interest where it'll instantly be ready for read :-) -- adrian |
638 |
|
|
*/ |
639 |
|
|
comm_setselect(fd, COMM_SELECT_READ, res_readreply, NULL, 0); |
640 |
michael |
984 |
|
641 |
michael |
4302 |
if (rc <= (ssize_t)sizeof(HEADER)) |
642 |
adx |
30 |
return; |
643 |
|
|
|
644 |
|
|
/* |
645 |
michael |
4238 |
* Check against possibly fake replies |
646 |
|
|
*/ |
647 |
|
|
if (!res_ourserver(&lsin)) |
648 |
|
|
return; |
649 |
|
|
|
650 |
|
|
/* |
651 |
michael |
3298 |
* Convert DNS reply reader from Network byte order to CPU byte order. |
652 |
adx |
30 |
*/ |
653 |
|
|
header = (HEADER *)buf; |
654 |
|
|
header->ancount = ntohs(header->ancount); |
655 |
|
|
header->qdcount = ntohs(header->qdcount); |
656 |
|
|
header->nscount = ntohs(header->nscount); |
657 |
|
|
header->arcount = ntohs(header->arcount); |
658 |
|
|
|
659 |
|
|
/* |
660 |
michael |
3298 |
* Response for an id which we have already received an answer for |
661 |
michael |
994 |
* just ignore this response. |
662 |
adx |
30 |
*/ |
663 |
michael |
4312 |
if ((request = find_id(header->id)) == NULL) |
664 |
adx |
30 |
return; |
665 |
|
|
|
666 |
|
|
if ((header->rcode != NO_ERRORS) || (header->ancount == 0)) |
667 |
|
|
{ |
668 |
michael |
984 |
if (header->rcode == SERVFAIL || header->rcode == NXDOMAIN) |
669 |
db |
155 |
{ |
670 |
michael |
984 |
/* |
671 |
|
|
* If a bad error was returned, stop here and don't |
672 |
|
|
* send any more (no retries granted). |
673 |
|
|
*/ |
674 |
michael |
992 |
(*request->callback)(request->callback_ctx, NULL, NULL); |
675 |
michael |
984 |
rem_request(request); |
676 |
|
|
} |
677 |
|
|
#ifdef IPV6 |
678 |
|
|
else |
679 |
|
|
{ |
680 |
michael |
2916 |
/* |
681 |
michael |
4312 |
* If we havent already tried this, and we're looking up AAAA, try A now. |
682 |
adx |
30 |
*/ |
683 |
|
|
if (request->state == REQ_AAAA && request->type == T_AAAA) |
684 |
|
|
{ |
685 |
|
|
request->timeout += 4; |
686 |
|
|
resend_query(request); |
687 |
|
|
} |
688 |
michael |
984 |
} |
689 |
adx |
30 |
#endif |
690 |
michael |
984 |
|
691 |
db |
468 |
return; |
692 |
adx |
30 |
} |
693 |
michael |
984 |
|
694 |
adx |
30 |
/* |
695 |
michael |
2916 |
* If this fails there was an error decoding the received packet, |
696 |
adx |
30 |
* try it again and hope it works the next time. |
697 |
|
|
*/ |
698 |
michael |
994 |
if (proc_answer(request, header, buf, buf + rc)) |
699 |
adx |
30 |
{ |
700 |
|
|
if (request->type == T_PTR) |
701 |
|
|
{ |
702 |
|
|
if (request->name == NULL) |
703 |
|
|
{ |
704 |
|
|
/* |
705 |
michael |
3298 |
* Got a PTR response with no name, something bogus is happening |
706 |
adx |
30 |
* don't bother trying again, the client address doesn't resolve |
707 |
|
|
*/ |
708 |
michael |
992 |
(*request->callback)(request->callback_ctx, NULL, NULL); |
709 |
adx |
30 |
rem_request(request); |
710 |
|
|
return; |
711 |
|
|
} |
712 |
|
|
|
713 |
|
|
/* |
714 |
michael |
3298 |
* Lookup the 'authoritative' name that we were given for the ip#. |
715 |
adx |
30 |
*/ |
716 |
|
|
#ifdef IPV6 |
717 |
|
|
if (request->addr.ss.ss_family == AF_INET6) |
718 |
michael |
992 |
gethost_byname_type(request->callback, request->callback_ctx, request->name, T_AAAA); |
719 |
adx |
30 |
else |
720 |
|
|
#endif |
721 |
michael |
992 |
gethost_byname_type(request->callback, request->callback_ctx, request->name, T_A); |
722 |
adx |
30 |
rem_request(request); |
723 |
|
|
} |
724 |
|
|
else |
725 |
|
|
{ |
726 |
|
|
/* |
727 |
michael |
3298 |
* Got a name and address response, client resolved |
728 |
adx |
30 |
*/ |
729 |
michael |
992 |
(*request->callback)(request->callback_ctx, &request->addr, request->name); |
730 |
adx |
30 |
rem_request(request); |
731 |
|
|
} |
732 |
|
|
} |
733 |
|
|
else if (!request->sent) |
734 |
|
|
{ |
735 |
|
|
/* XXX - we got a response for a query we didn't send with a valid id? |
736 |
|
|
* this should never happen, bail here and leave the client unresolved |
737 |
|
|
*/ |
738 |
|
|
assert(0); |
739 |
|
|
|
740 |
michael |
4235 |
(*request->callback)(request->callback_ctx, NULL, NULL); |
741 |
adx |
30 |
/* XXX don't leak it */ |
742 |
|
|
rem_request(request); |
743 |
|
|
} |
744 |
|
|
} |
745 |
|
|
|
746 |
|
|
void |
747 |
|
|
report_dns_servers(struct Client *source_p) |
748 |
|
|
{ |
749 |
michael |
3235 |
char ipaddr[HOSTIPLEN + 1] = ""; |
750 |
adx |
30 |
|
751 |
michael |
3297 |
for (unsigned int i = 0; i < irc_nscount; ++i) |
752 |
adx |
30 |
{ |
753 |
michael |
1123 |
getnameinfo((struct sockaddr *)&(irc_nsaddr_list[i]), |
754 |
|
|
irc_nsaddr_list[i].ss_len, ipaddr, |
755 |
|
|
sizeof(ipaddr), NULL, 0, NI_NUMERICHOST); |
756 |
michael |
3109 |
sendto_one_numeric(source_p, &me, RPL_STATSALINE, ipaddr); |
757 |
adx |
30 |
} |
758 |
|
|
} |
759 |
michael |
4312 |
|
760 |
|
|
/* |
761 |
|
|
* timeout_query_list - Remove queries from the list which have been |
762 |
|
|
* there too long without being resolved. |
763 |
|
|
*/ |
764 |
|
|
static time_t |
765 |
|
|
timeout_query_list(void) |
766 |
|
|
{ |
767 |
|
|
dlink_node *ptr = NULL, *ptr_next = NULL; |
768 |
|
|
struct reslist *request = NULL; |
769 |
|
|
time_t next_time = 0; |
770 |
|
|
time_t timeout = 0; |
771 |
|
|
|
772 |
|
|
DLINK_FOREACH_SAFE(ptr, ptr_next, request_list.head) |
773 |
|
|
{ |
774 |
|
|
request = ptr->data; |
775 |
|
|
timeout = request->sentat + request->timeout; |
776 |
|
|
|
777 |
|
|
if (CurrentTime >= timeout) |
778 |
|
|
{ |
779 |
|
|
if (--request->retries <= 0) |
780 |
|
|
{ |
781 |
|
|
(*request->callback)(request->callback_ctx, NULL, NULL); |
782 |
|
|
rem_request(request); |
783 |
|
|
continue; |
784 |
|
|
} |
785 |
|
|
else |
786 |
|
|
{ |
787 |
|
|
request->sentat = CurrentTime; |
788 |
|
|
request->timeout += request->timeout; |
789 |
|
|
resend_query(request); |
790 |
|
|
} |
791 |
|
|
} |
792 |
|
|
|
793 |
|
|
if (next_time == 0 || timeout < next_time) |
794 |
|
|
next_time = timeout; |
795 |
|
|
} |
796 |
|
|
|
797 |
|
|
return (next_time > CurrentTime) ? next_time : (CurrentTime + AR_TTL); |
798 |
|
|
} |
799 |
|
|
|
800 |
|
|
/* |
801 |
|
|
* timeout_resolver - check request list |
802 |
|
|
*/ |
803 |
|
|
static void |
804 |
|
|
timeout_resolver(void *notused) |
805 |
|
|
{ |
806 |
|
|
timeout_query_list(); |
807 |
|
|
} |
808 |
|
|
|
809 |
|
|
/* |
810 |
|
|
* init_resolver - initialize resolver and resolver library |
811 |
|
|
*/ |
812 |
|
|
void |
813 |
|
|
init_resolver(void) |
814 |
|
|
{ |
815 |
|
|
static struct event event_timeout_resolver = |
816 |
|
|
{ |
817 |
|
|
.name = "timeout_resolver", |
818 |
|
|
.handler = timeout_resolver, |
819 |
|
|
.when = 1 |
820 |
|
|
}; |
821 |
|
|
|
822 |
|
|
dns_pool = mp_pool_new(sizeof(struct reslist), MP_CHUNK_SIZE_DNS); |
823 |
|
|
memset(&ResolverFileDescriptor, 0, sizeof(fde_t)); |
824 |
|
|
start_resolver(); |
825 |
|
|
|
826 |
|
|
event_add(&event_timeout_resolver, NULL); |
827 |
|
|
} |