]>
Commit | Line | Data |
---|---|---|
212380e3 AC |
1 | /* |
2 | * A rewrite of Darren Reeds original res.c As there is nothing | |
3 | * left of Darrens original code, this is now licensed by the hybrid group. | |
4 | * (Well, some of the function names are the same, and bits of the structs..) | |
5 | * You can use it where it is useful, free even. Buy us a beer and stuff. | |
6 | * | |
7 | * The authors takes no responsibility for any damage or loss | |
8 | * of property which results from the use of this software. | |
9 | * | |
42bda3f3 | 10 | * $Id: res.c 3301 2007-03-28 15:04:06Z jilles $ |
212380e3 AC |
11 | * from Hybrid Id: res.c 459 2006-02-12 22:21:37Z db $ |
12 | * | |
13 | * July 1999 - Rewrote a bunch of stuff here. Change hostent builder code, | |
14 | * added callbacks and reference counting of returned hostents. | |
15 | * --Bleep (Thomas Helvey <tomh@inxpress.net>) | |
16 | * | |
17 | * This was all needlessly complicated for irc. Simplified. No more hostent | |
55abcbb2 | 18 | * All we really care about is the IP -> hostname mappings. Thats all. |
212380e3 AC |
19 | * |
20 | * Apr 28, 2003 --cryogen and Dianora | |
21 | * | |
22 | * DNS server flooding lessened, AAAA-or-A lookup removed, ip6.int support | |
23 | * removed, various robustness fixes | |
24 | * | |
25 | * 2006 --jilles and nenolod | |
5bd79c2c KB |
26 | * |
27 | * Resend queries to other servers if the DNS server replies with an error or | |
28 | * an invalid response. Also, avoid servers that return errors or invalid | |
29 | * responses. | |
30 | * | |
31 | * October 2012 --mr_flea | |
212380e3 AC |
32 | */ |
33 | ||
34 | #include "stdinc.h" | |
35 | #include "ircd_defs.h" | |
36 | #include "common.h" | |
37 | #include "ircd.h" | |
212380e3 AC |
38 | #include "res.h" |
39 | #include "reslib.h" | |
4562c604 | 40 | #include "match.h" |
212380e3 AC |
41 | #include "numeric.h" |
42 | #include "client.h" /* SNO_* */ | |
77d3d2db KB |
43 | #include "s_assert.h" |
44 | #include "logger.h" | |
45 | #include "send.h" | |
212380e3 AC |
46 | |
47 | #if (CHAR_BIT != 8) | |
48 | #error this code needs to be able to address individual octets | |
49 | #endif | |
50 | ||
51 | static PF res_readreply; | |
52 | ||
53 | #define MAXPACKET 1024 /* rfc sez 512 but we expand names so ... */ | |
54 | #define RES_MAXALIASES 35 /* maximum aliases allowed */ | |
55 | #define RES_MAXADDRS 35 /* maximum addresses allowed */ | |
56 | #define AR_TTL 600 /* TTL in seconds for dns cache entries */ | |
57 | ||
58 | /* RFC 1104/1105 wasn't very helpful about what these fields | |
59 | * should be named, so for now, we'll just name them this way. | |
60 | * we probably should look at what named calls them or something. | |
61 | */ | |
62 | #define TYPE_SIZE (size_t)2 | |
63 | #define CLASS_SIZE (size_t)2 | |
64 | #define TTL_SIZE (size_t)4 | |
65 | #define RDLENGTH_SIZE (size_t)2 | |
66 | #define ANSWER_FIXED_SIZE (TYPE_SIZE + CLASS_SIZE + TTL_SIZE + RDLENGTH_SIZE) | |
67 | ||
212380e3 AC |
68 | struct reslist |
69 | { | |
330fc5c1 | 70 | rb_dlink_node node; |
212380e3 | 71 | int id; |
212380e3 AC |
72 | time_t ttl; |
73 | char type; | |
db3efb7a | 74 | char queryname[IRCD_RES_HOSTLEN + 1]; /* name currently being queried */ |
212380e3 AC |
75 | char retries; /* retry counter */ |
76 | char sends; /* number of sends (>1 means resent) */ | |
212380e3 AC |
77 | time_t sentat; |
78 | time_t timeout; | |
9279ad64 | 79 | int lastns; /* index of last server sent to */ |
e7046ee5 | 80 | struct rb_sockaddr_storage addr; |
212380e3 AC |
81 | char *name; |
82 | struct DNSQuery *query; /* query callback for this request */ | |
83 | }; | |
84 | ||
0f6d6f3d | 85 | static rb_fde_t *res_fd; |
330fc5c1 | 86 | static rb_dlink_list request_list = { NULL, NULL, 0 }; |
5bd79c2c | 87 | static int ns_failure_count[IRCD_MAXNS]; /* timeouts and invalid/failed replies */ |
212380e3 AC |
88 | |
89 | static void rem_request(struct reslist *request); | |
90 | static struct reslist *make_request(struct DNSQuery *query); | |
3881a3ce JT |
91 | static void gethost_byname_type_fqdn(const char *name, struct DNSQuery *query, |
92 | int type); | |
212380e3 | 93 | static void do_query_name(struct DNSQuery *query, const char *name, struct reslist *request, int); |
e7046ee5 | 94 | static void do_query_number(struct DNSQuery *query, const struct rb_sockaddr_storage *, |
212380e3 AC |
95 | struct reslist *request); |
96 | static void query_name(struct reslist *request); | |
97 | static int send_res_msg(const char *buf, int len, int count); | |
98 | static void resend_query(struct reslist *request); | |
99 | static int check_question(struct reslist *request, HEADER * header, char *buf, char *eob); | |
100 | static int proc_answer(struct reslist *request, HEADER * header, char *, char *); | |
101 | static struct reslist *find_id(int id); | |
102 | static struct DNSReply *make_dnsreply(struct reslist *request); | |
103 | ||
212380e3 AC |
104 | /* |
105 | * int | |
106 | * res_ourserver(inp) | |
107 | * looks up "inp" in irc_nsaddr_list[] | |
108 | * returns: | |
5bd79c2c | 109 | * server ID or -1 for not found |
212380e3 AC |
110 | * author: |
111 | * paul vixie, 29may94 | |
112 | * revised for ircd, cryogen(stu) may03 | |
5bd79c2c | 113 | * slightly modified for charybdis, mr_flea oct12 |
212380e3 | 114 | */ |
e7046ee5 | 115 | static int res_ourserver(const struct rb_sockaddr_storage *inp) |
212380e3 | 116 | { |
ccda6e3f | 117 | #ifdef RB_IPV6 |
01e9b1eb JT |
118 | const struct sockaddr_in6 *v6; |
119 | const struct sockaddr_in6 *v6in = (const struct sockaddr_in6 *)inp; | |
212380e3 | 120 | #endif |
01e9b1eb JT |
121 | const struct sockaddr_in *v4; |
122 | const struct sockaddr_in *v4in = (const struct sockaddr_in *)inp; | |
212380e3 AC |
123 | int ns; |
124 | ||
125 | for (ns = 0; ns < irc_nscount; ns++) | |
126 | { | |
e7046ee5 | 127 | const struct rb_sockaddr_storage *srv = &irc_nsaddr_list[ns]; |
5bd79c2c KB |
128 | |
129 | if (srv->ss_family != inp->ss_family) | |
130 | continue; | |
131 | ||
ccda6e3f | 132 | #ifdef RB_IPV6 |
01e9b1eb | 133 | v6 = (const struct sockaddr_in6 *)srv; |
212380e3 | 134 | #endif |
01e9b1eb | 135 | v4 = (const struct sockaddr_in *)srv; |
212380e3 AC |
136 | |
137 | /* could probably just memcmp(srv, inp, srv.ss_len) here | |
5bd79c2c | 138 | * but we'll err on the side of caution - stu |
212380e3 AC |
139 | */ |
140 | switch (srv->ss_family) | |
141 | { | |
ccda6e3f | 142 | #ifdef RB_IPV6 |
5bd79c2c KB |
143 | case AF_INET6: |
144 | if (v6->sin6_port == v6in->sin6_port) | |
145 | if ((memcmp(&v6->sin6_addr.s6_addr, &v6in->sin6_addr.s6_addr, | |
146 | sizeof(struct in6_addr)) == 0) || | |
147 | (memcmp(&v6->sin6_addr.s6_addr, &in6addr_any, | |
148 | sizeof(struct in6_addr)) == 0)) | |
149 | { | |
150 | return ns; | |
151 | } | |
152 | break; | |
212380e3 | 153 | #endif |
5bd79c2c KB |
154 | case AF_INET: |
155 | if (v4->sin_port == v4in->sin_port) | |
156 | if ((v4->sin_addr.s_addr == INADDR_ANY) | |
157 | || (v4->sin_addr.s_addr == v4in->sin_addr.s_addr)) | |
158 | { | |
159 | return ns; | |
160 | } | |
161 | break; | |
162 | default: | |
163 | break; | |
212380e3 AC |
164 | } |
165 | } | |
166 | ||
5bd79c2c | 167 | return -1; |
212380e3 AC |
168 | } |
169 | ||
170 | /* | |
55abcbb2 | 171 | * timeout_query_list - Remove queries from the list which have been |
212380e3 AC |
172 | * there too long without being resolved. |
173 | */ | |
174 | static time_t timeout_query_list(time_t now) | |
175 | { | |
330fc5c1 | 176 | rb_dlink_node *ptr; |
637c4932 | 177 | rb_dlink_node *next_ptr; |
212380e3 AC |
178 | struct reslist *request; |
179 | time_t next_time = 0; | |
180 | time_t timeout = 0; | |
181 | ||
637c4932 | 182 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, request_list.head) |
212380e3 AC |
183 | { |
184 | request = ptr->data; | |
185 | timeout = request->sentat + request->timeout; | |
186 | ||
187 | if (now >= timeout) | |
188 | { | |
5bd79c2c KB |
189 | ns_failure_count[request->lastns]++; |
190 | request->sentat = now; | |
191 | request->timeout += request->timeout; | |
192 | resend_query(request); | |
212380e3 AC |
193 | } |
194 | ||
195 | if ((next_time == 0) || timeout < next_time) | |
196 | { | |
197 | next_time = timeout; | |
198 | } | |
199 | } | |
200 | ||
201 | return (next_time > now) ? next_time : (now + AR_TTL); | |
202 | } | |
203 | ||
204 | /* | |
205 | * timeout_resolver - check request list | |
206 | */ | |
207 | static void timeout_resolver(void *notused) | |
208 | { | |
e3354945 | 209 | timeout_query_list(rb_current_time()); |
212380e3 AC |
210 | } |
211 | ||
0f6d6f3d AC |
212 | static struct ev_entry *timeout_resolver_ev = NULL; |
213 | ||
212380e3 AC |
214 | /* |
215 | * start_resolver - do everything we need to read the resolv.conf file | |
216 | * and initialize the resolver file descriptor if needed | |
217 | */ | |
218 | static void start_resolver(void) | |
219 | { | |
78dfd39d JT |
220 | int i; |
221 | ||
212380e3 | 222 | irc_res_init(); |
78dfd39d | 223 | for (i = 0; i < irc_nscount; i++) |
5bd79c2c | 224 | ns_failure_count[i] = 0; |
212380e3 | 225 | |
01e9b1eb | 226 | if (res_fd == NULL) |
212380e3 | 227 | { |
b2f0da88 | 228 | if ((res_fd = rb_socket(irc_nsaddr_list[0].ss_family, SOCK_DGRAM, 0, |
0f6d6f3d | 229 | "UDP resolver socket")) == NULL) |
212380e3 AC |
230 | return; |
231 | ||
232 | /* At the moment, the resolver FD data is global .. */ | |
0f6d6f3d AC |
233 | rb_setselect(res_fd, RB_SELECT_READ, res_readreply, NULL); |
234 | timeout_resolver_ev = rb_event_add("timeout_resolver", timeout_resolver, NULL, 1); | |
212380e3 AC |
235 | } |
236 | } | |
237 | ||
238 | /* | |
239 | * init_resolver - initialize resolver and resolver library | |
240 | */ | |
241 | void init_resolver(void) | |
242 | { | |
243 | #ifdef HAVE_SRAND48 | |
e3354945 | 244 | srand48(rb_current_time()); |
212380e3 AC |
245 | #endif |
246 | start_resolver(); | |
247 | } | |
248 | ||
249 | /* | |
250 | * restart_resolver - reread resolv.conf, reopen socket | |
251 | */ | |
252 | void restart_resolver(void) | |
253 | { | |
b2f0da88 | 254 | rb_close(res_fd); |
0f6d6f3d AC |
255 | res_fd = NULL; |
256 | rb_event_delete(timeout_resolver_ev); /* -ddosen */ | |
212380e3 AC |
257 | start_resolver(); |
258 | } | |
259 | ||
260 | /* | |
261 | * add_local_domain - Add the domain to hostname, if it is missing | |
262 | * (as suggested by eps@TOASTER.SFSU.EDU) | |
263 | */ | |
264 | void add_local_domain(char *hname, size_t size) | |
265 | { | |
266 | /* try to fix up unqualified names */ | |
267 | if (strchr(hname, '.') == NULL) | |
268 | { | |
269 | if (irc_domain[0]) | |
270 | { | |
271 | size_t len = strlen(hname); | |
272 | ||
273 | if ((strlen(irc_domain) + len + 2) < size) | |
274 | { | |
275 | hname[len++] = '.'; | |
276 | strcpy(hname + len, irc_domain); | |
277 | } | |
278 | } | |
279 | } | |
280 | } | |
281 | ||
282 | /* | |
55abcbb2 KB |
283 | * rem_request - remove a request from the list. |
284 | * This must also free any memory that has been allocated for | |
212380e3 AC |
285 | * temporary storage of DNS results. |
286 | */ | |
287 | static void rem_request(struct reslist *request) | |
288 | { | |
330fc5c1 | 289 | rb_dlinkDelete(&request->node, &request_list); |
637c4932 VY |
290 | rb_free(request->name); |
291 | rb_free(request); | |
212380e3 AC |
292 | } |
293 | ||
294 | /* | |
295 | * make_request - Create a DNS request record for the server. | |
296 | */ | |
297 | static struct reslist *make_request(struct DNSQuery *query) | |
298 | { | |
eddc2ab6 | 299 | struct reslist *request = rb_malloc(sizeof(struct reslist)); |
212380e3 | 300 | |
e3354945 | 301 | request->sentat = rb_current_time(); |
212380e3 | 302 | request->retries = 3; |
212380e3 AC |
303 | request->timeout = 4; /* start at 4 and exponential inc. */ |
304 | request->query = query; | |
212380e3 | 305 | |
5bd79c2c KB |
306 | /* |
307 | * generate a unique id | |
308 | * NOTE: we don't have to worry about converting this to and from | |
309 | * network byte order, the nameserver does not interpret this value | |
310 | * and returns it unchanged | |
311 | * | |
312 | * we generate an id per request now (instead of per send) to allow | |
313 | * late replies to be used. | |
314 | */ | |
315 | #ifdef HAVE_LRAND48 | |
316 | do | |
317 | { | |
318 | request->id = (request->id + lrand48()) & 0xffff; | |
319 | } while (find_id(request->id)); | |
320 | #else | |
321 | int k = 0; | |
322 | struct timeval tv; | |
323 | gettimeofday(&tv, NULL); | |
324 | do | |
325 | { | |
326 | request->id = (request->id + k + tv.tv_usec) & 0xffff; | |
327 | k++; | |
328 | } while (find_id(request->id)); | |
329 | #endif /* HAVE_LRAND48 */ | |
330 | ||
330fc5c1 | 331 | rb_dlinkAdd(request, &request->node, &request_list); |
212380e3 AC |
332 | |
333 | return request; | |
334 | } | |
335 | ||
336 | /* | |
55abcbb2 | 337 | * delete_resolver_queries - cleanup outstanding queries |
212380e3 AC |
338 | * for which there no longer exist clients or conf lines. |
339 | */ | |
340 | void delete_resolver_queries(const struct DNSQuery *query) | |
341 | { | |
330fc5c1 | 342 | rb_dlink_node *ptr; |
637c4932 | 343 | rb_dlink_node *next_ptr; |
212380e3 AC |
344 | struct reslist *request; |
345 | ||
637c4932 | 346 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, request_list.head) |
212380e3 AC |
347 | { |
348 | if ((request = ptr->data) != NULL) | |
349 | { | |
350 | if (query == request->query) | |
351 | rem_request(request); | |
352 | } | |
353 | } | |
354 | } | |
355 | ||
356 | /* | |
78dfd39d JT |
357 | * retryfreq - determine how many queries to wait before resending |
358 | * if there have been that many consecutive timeouts | |
359 | */ | |
360 | static int retryfreq(int timeouts) | |
361 | { | |
362 | switch (timeouts) | |
363 | { | |
364 | case 1: | |
365 | return 3; | |
366 | case 2: | |
367 | return 9; | |
368 | case 3: | |
369 | return 27; | |
370 | case 4: | |
371 | return 81; | |
372 | default: | |
373 | return 243; | |
374 | } | |
375 | } | |
376 | ||
377 | /* | |
378 | * send_res_msg - sends msg to a nameserver. | |
379 | * This should reflect /etc/resolv.conf. | |
55abcbb2 | 380 | * Returns number of nameserver successfully sent to |
78dfd39d | 381 | * or -1 if no successful sends. |
212380e3 AC |
382 | */ |
383 | static int send_res_msg(const char *msg, int len, int rcount) | |
384 | { | |
385 | int i; | |
78dfd39d JT |
386 | int ns; |
387 | static int retrycnt; | |
212380e3 | 388 | |
78dfd39d JT |
389 | retrycnt++; |
390 | /* First try a nameserver that seems to work. | |
391 | * Every once in a while, try a possibly broken one to check | |
392 | * if it is working again. | |
212380e3 | 393 | */ |
78dfd39d JT |
394 | for (i = 0; i < irc_nscount; i++) |
395 | { | |
396 | ns = (i + rcount - 1) % irc_nscount; | |
5bd79c2c | 397 | if (ns_failure_count[ns] && retrycnt % retryfreq(ns_failure_count[ns])) |
78dfd39d JT |
398 | continue; |
399 | if (sendto(rb_get_fd(res_fd), msg, len, 0, | |
55abcbb2 | 400 | (struct sockaddr *)&(irc_nsaddr_list[ns]), |
78dfd39d JT |
401 | GET_SS_LEN(&irc_nsaddr_list[ns])) == len) |
402 | return ns; | |
403 | } | |
212380e3 | 404 | |
78dfd39d JT |
405 | /* No known working nameservers, try some broken one. */ |
406 | for (i = 0; i < irc_nscount; i++) | |
212380e3 | 407 | { |
78dfd39d | 408 | ns = (i + rcount - 1) % irc_nscount; |
5bd79c2c | 409 | if (!ns_failure_count[ns]) |
78dfd39d | 410 | continue; |
0f6d6f3d | 411 | if (sendto(rb_get_fd(res_fd), msg, len, 0, |
55abcbb2 | 412 | (struct sockaddr *)&(irc_nsaddr_list[ns]), |
78dfd39d JT |
413 | GET_SS_LEN(&irc_nsaddr_list[ns])) == len) |
414 | return ns; | |
212380e3 AC |
415 | } |
416 | ||
78dfd39d | 417 | return -1; |
212380e3 AC |
418 | } |
419 | ||
420 | /* | |
421 | * find_id - find a dns request id (id is determined by dn_mkquery) | |
422 | */ | |
423 | static struct reslist *find_id(int id) | |
424 | { | |
330fc5c1 | 425 | rb_dlink_node *ptr; |
212380e3 AC |
426 | struct reslist *request; |
427 | ||
5cefa1d6 | 428 | RB_DLINK_FOREACH(ptr, request_list.head) |
212380e3 AC |
429 | { |
430 | request = ptr->data; | |
431 | ||
432 | if (request->id == id) | |
433 | return (request); | |
434 | } | |
435 | ||
436 | return (NULL); | |
437 | } | |
438 | ||
55abcbb2 | 439 | /* |
3881a3ce | 440 | * gethost_byname_type - get host address from name, adding domain if needed |
212380e3 AC |
441 | */ |
442 | void gethost_byname_type(const char *name, struct DNSQuery *query, int type) | |
3881a3ce JT |
443 | { |
444 | char fqdn[IRCD_RES_HOSTLEN + 1]; | |
445 | assert(name != 0); | |
446 | ||
447 | rb_strlcpy(fqdn, name, sizeof fqdn); | |
448 | add_local_domain(fqdn, IRCD_RES_HOSTLEN); | |
449 | gethost_byname_type_fqdn(fqdn, query, type); | |
450 | } | |
451 | ||
452 | /* | |
453 | * gethost_byname_type_fqdn - get host address from fqdn | |
454 | */ | |
455 | static void gethost_byname_type_fqdn(const char *name, struct DNSQuery *query, | |
456 | int type) | |
212380e3 AC |
457 | { |
458 | assert(name != 0); | |
459 | do_query_name(query, name, NULL, type); | |
460 | } | |
461 | ||
462 | /* | |
463 | * gethost_byaddr - get host name from address | |
464 | */ | |
e7046ee5 | 465 | void gethost_byaddr(const struct rb_sockaddr_storage *addr, struct DNSQuery *query) |
212380e3 AC |
466 | { |
467 | do_query_number(query, addr, NULL); | |
468 | } | |
469 | ||
470 | /* | |
471 | * do_query_name - nameserver lookup name | |
472 | */ | |
473 | static void do_query_name(struct DNSQuery *query, const char *name, struct reslist *request, | |
474 | int type) | |
475 | { | |
212380e3 AC |
476 | if (request == NULL) |
477 | { | |
478 | request = make_request(query); | |
3881a3ce | 479 | request->name = rb_strdup(name); |
212380e3 AC |
480 | } |
481 | ||
3881a3ce | 482 | rb_strlcpy(request->queryname, name, sizeof(request->queryname)); |
212380e3 AC |
483 | request->type = type; |
484 | query_name(request); | |
485 | } | |
486 | ||
487 | /* | |
488 | * do_query_number - Use this to do reverse IP# lookups. | |
489 | */ | |
e7046ee5 | 490 | static void do_query_number(struct DNSQuery *query, const struct rb_sockaddr_storage *addr, |
212380e3 AC |
491 | struct reslist *request) |
492 | { | |
493 | const unsigned char *cp; | |
494 | ||
495 | if (request == NULL) | |
496 | { | |
497 | request = make_request(query); | |
e7046ee5 | 498 | memcpy(&request->addr, addr, sizeof(struct rb_sockaddr_storage)); |
db3efb7a | 499 | request->name = (char *)rb_malloc(IRCD_RES_HOSTLEN + 1); |
212380e3 AC |
500 | } |
501 | ||
502 | if (addr->ss_family == AF_INET) | |
503 | { | |
01e9b1eb | 504 | const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr; |
212380e3 AC |
505 | cp = (const unsigned char *)&v4->sin_addr.s_addr; |
506 | ||
b2f0da88 | 507 | rb_sprintf(request->queryname, "%u.%u.%u.%u.in-addr.arpa", (unsigned int)(cp[3]), |
212380e3 AC |
508 | (unsigned int)(cp[2]), (unsigned int)(cp[1]), (unsigned int)(cp[0])); |
509 | } | |
ccda6e3f | 510 | #ifdef RB_IPV6 |
212380e3 AC |
511 | else if (addr->ss_family == AF_INET6) |
512 | { | |
01e9b1eb | 513 | const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr; |
212380e3 AC |
514 | cp = (const unsigned char *)&v6->sin6_addr.s6_addr; |
515 | ||
516 | (void)sprintf(request->queryname, "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x." | |
517 | "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa", | |
518 | (unsigned int)(cp[15] & 0xf), (unsigned int)(cp[15] >> 4), | |
519 | (unsigned int)(cp[14] & 0xf), (unsigned int)(cp[14] >> 4), | |
520 | (unsigned int)(cp[13] & 0xf), (unsigned int)(cp[13] >> 4), | |
521 | (unsigned int)(cp[12] & 0xf), (unsigned int)(cp[12] >> 4), | |
522 | (unsigned int)(cp[11] & 0xf), (unsigned int)(cp[11] >> 4), | |
523 | (unsigned int)(cp[10] & 0xf), (unsigned int)(cp[10] >> 4), | |
524 | (unsigned int)(cp[9] & 0xf), (unsigned int)(cp[9] >> 4), | |
525 | (unsigned int)(cp[8] & 0xf), (unsigned int)(cp[8] >> 4), | |
526 | (unsigned int)(cp[7] & 0xf), (unsigned int)(cp[7] >> 4), | |
527 | (unsigned int)(cp[6] & 0xf), (unsigned int)(cp[6] >> 4), | |
528 | (unsigned int)(cp[5] & 0xf), (unsigned int)(cp[5] >> 4), | |
529 | (unsigned int)(cp[4] & 0xf), (unsigned int)(cp[4] >> 4), | |
530 | (unsigned int)(cp[3] & 0xf), (unsigned int)(cp[3] >> 4), | |
531 | (unsigned int)(cp[2] & 0xf), (unsigned int)(cp[2] >> 4), | |
532 | (unsigned int)(cp[1] & 0xf), (unsigned int)(cp[1] >> 4), | |
533 | (unsigned int)(cp[0] & 0xf), (unsigned int)(cp[0] >> 4)); | |
534 | } | |
535 | #endif | |
536 | ||
537 | request->type = T_PTR; | |
538 | query_name(request); | |
539 | } | |
540 | ||
541 | /* | |
542 | * query_name - generate a query based on class, type and name. | |
543 | */ | |
544 | static void query_name(struct reslist *request) | |
545 | { | |
546 | char buf[MAXPACKET]; | |
547 | int request_len = 0; | |
78dfd39d | 548 | int ns; |
212380e3 AC |
549 | |
550 | memset(buf, 0, sizeof(buf)); | |
551 | ||
552 | if ((request_len = | |
553 | irc_res_mkquery(request->queryname, C_IN, request->type, (unsigned char *)buf, sizeof(buf))) > 0) | |
554 | { | |
29c92cf9 | 555 | HEADER *header = (HEADER *)(void *)buf; |
5bd79c2c | 556 | header->id = request->id; |
212380e3 AC |
557 | ++request->sends; |
558 | ||
78dfd39d JT |
559 | ns = send_res_msg(buf, request_len, request->sends); |
560 | if (ns != -1) | |
561 | request->lastns = ns; | |
212380e3 AC |
562 | } |
563 | } | |
564 | ||
565 | static void resend_query(struct reslist *request) | |
566 | { | |
5bd79c2c KB |
567 | if (--request->retries <= 0) |
568 | { | |
569 | (*request->query->callback) (request->query->ptr, NULL); | |
570 | rem_request(request); | |
571 | return; | |
572 | } | |
573 | ||
212380e3 AC |
574 | switch (request->type) |
575 | { | |
576 | case T_PTR: | |
577 | do_query_number(NULL, &request->addr, request); | |
578 | break; | |
579 | case T_A: | |
ccda6e3f | 580 | #ifdef RB_IPV6 |
212380e3 AC |
581 | case T_AAAA: |
582 | #endif | |
583 | do_query_name(NULL, request->name, request, request->type); | |
584 | break; | |
585 | default: | |
586 | break; | |
587 | } | |
588 | } | |
589 | ||
590 | /* | |
591 | * check_question - check if the reply really belongs to the | |
592 | * name we queried (to guard against late replies from previous | |
593 | * queries with the same id). | |
594 | */ | |
595 | static int check_question(struct reslist *request, HEADER * header, char *buf, char *eob) | |
596 | { | |
db3efb7a | 597 | char hostbuf[IRCD_RES_HOSTLEN + 1]; /* working buffer */ |
212380e3 AC |
598 | unsigned char *current; /* current position in buf */ |
599 | int n; /* temp count */ | |
600 | ||
601 | current = (unsigned char *)buf + sizeof(HEADER); | |
602 | if (header->qdcount != 1) | |
603 | return 0; | |
604 | n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current, hostbuf, | |
605 | sizeof(hostbuf)); | |
606 | if (n <= 0) | |
607 | return 0; | |
608 | if (strcasecmp(hostbuf, request->queryname)) | |
609 | return 0; | |
610 | return 1; | |
611 | } | |
612 | ||
613 | /* | |
614 | * proc_answer - process name server reply | |
615 | */ | |
616 | static int proc_answer(struct reslist *request, HEADER * header, char *buf, char *eob) | |
617 | { | |
db3efb7a | 618 | char hostbuf[IRCD_RES_HOSTLEN + 100]; /* working buffer */ |
212380e3 | 619 | unsigned char *current; /* current position in buf */ |
212380e3 AC |
620 | int type; /* answer type */ |
621 | int n; /* temp count */ | |
622 | int rd_length; | |
623 | struct sockaddr_in *v4; /* conversion */ | |
ccda6e3f | 624 | #ifdef RB_IPV6 |
212380e3 AC |
625 | struct sockaddr_in6 *v6; |
626 | #endif | |
627 | current = (unsigned char *)buf + sizeof(HEADER); | |
628 | ||
629 | for (; header->qdcount > 0; --header->qdcount) | |
630 | { | |
631 | if ((n = irc_dn_skipname(current, (unsigned char *)eob)) < 0) | |
632 | return 0; | |
633 | ||
634 | current += (size_t) n + QFIXEDSZ; | |
635 | } | |
636 | ||
637 | /* | |
638 | * process each answer sent to us blech. | |
639 | */ | |
640 | while (header->ancount > 0 && (char *)current < eob) | |
641 | { | |
642 | header->ancount--; | |
643 | ||
644 | n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current, hostbuf, | |
645 | sizeof(hostbuf)); | |
646 | ||
647 | if (n < 0) | |
648 | { | |
649 | /* | |
650 | * broken message | |
651 | */ | |
652 | return (0); | |
653 | } | |
654 | else if (n == 0) | |
655 | { | |
656 | /* | |
657 | * no more answers left | |
658 | */ | |
659 | return (0); | |
660 | } | |
661 | ||
db3efb7a | 662 | hostbuf[IRCD_RES_HOSTLEN] = '\0'; |
212380e3 AC |
663 | |
664 | /* With Address arithmetic you have to be very anal | |
665 | * this code was not working on alpha due to that | |
666 | * (spotted by rodder/jailbird/dianora) | |
667 | */ | |
668 | current += (size_t) n; | |
669 | ||
670 | if (!(((char *)current + ANSWER_FIXED_SIZE) < eob)) | |
671 | break; | |
672 | ||
673 | type = irc_ns_get16(current); | |
674 | current += TYPE_SIZE; | |
675 | ||
32fb5895 | 676 | (void) irc_ns_get16(current); |
212380e3 AC |
677 | current += CLASS_SIZE; |
678 | ||
679 | request->ttl = irc_ns_get32(current); | |
680 | current += TTL_SIZE; | |
681 | ||
682 | rd_length = irc_ns_get16(current); | |
683 | current += RDLENGTH_SIZE; | |
684 | ||
55abcbb2 KB |
685 | /* |
686 | * Wait to set request->type until we verify this structure | |
212380e3 AC |
687 | */ |
688 | switch (type) | |
689 | { | |
690 | case T_A: | |
691 | if (request->type != T_A) | |
692 | return (0); | |
693 | ||
694 | /* | |
695 | * check for invalid rd_length or too many addresses | |
696 | */ | |
697 | if (rd_length != sizeof(struct in_addr)) | |
698 | return (0); | |
699 | v4 = (struct sockaddr_in *)&request->addr; | |
0f6d6f3d | 700 | SET_SS_LEN(&request->addr, sizeof(struct sockaddr_in)); |
212380e3 AC |
701 | v4->sin_family = AF_INET; |
702 | memcpy(&v4->sin_addr, current, sizeof(struct in_addr)); | |
703 | return (1); | |
704 | break; | |
ccda6e3f | 705 | #ifdef RB_IPV6 |
212380e3 AC |
706 | case T_AAAA: |
707 | if (request->type != T_AAAA) | |
708 | return (0); | |
709 | if (rd_length != sizeof(struct in6_addr)) | |
710 | return (0); | |
0f6d6f3d | 711 | SET_SS_LEN(&request->addr, sizeof(struct sockaddr_in6)); |
212380e3 AC |
712 | v6 = (struct sockaddr_in6 *)&request->addr; |
713 | v6->sin6_family = AF_INET6; | |
714 | memcpy(&v6->sin6_addr, current, sizeof(struct in6_addr)); | |
715 | return (1); | |
716 | break; | |
717 | #endif | |
718 | case T_PTR: | |
719 | if (request->type != T_PTR) | |
720 | return (0); | |
721 | n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current, | |
722 | hostbuf, sizeof(hostbuf)); | |
723 | if (n < 0) | |
724 | return (0); /* broken message */ | |
725 | else if (n == 0) | |
726 | return (0); /* no more answers left */ | |
727 | ||
db3efb7a | 728 | rb_strlcpy(request->name, hostbuf, IRCD_RES_HOSTLEN + 1); |
212380e3 AC |
729 | |
730 | return (1); | |
731 | break; | |
c889c12f JT |
732 | case T_CNAME: |
733 | /* real answer will follow */ | |
212380e3 AC |
734 | current += rd_length; |
735 | break; | |
736 | ||
737 | default: | |
738 | /* XXX I'd rather just throw away the entire bogus thing | |
739 | * but its possible its just a broken nameserver with still | |
740 | * valid answers. But lets do some rudimentary logging for now... | |
741 | */ | |
742 | ilog(L_MAIN, "irc_res.c bogus type %d", type); | |
743 | break; | |
744 | } | |
745 | } | |
746 | ||
747 | return (1); | |
748 | } | |
749 | ||
750 | /* | |
ce1cab0f JT |
751 | * res_read_single_reply - read a dns reply from the nameserver and process it. |
752 | * Return value: 1 if a packet was read, 0 otherwise | |
212380e3 | 753 | */ |
ce1cab0f | 754 | static int res_read_single_reply(rb_fde_t *F, void *data) |
212380e3 AC |
755 | { |
756 | char buf[sizeof(HEADER) + MAXPACKET] | |
55abcbb2 KB |
757 | /* Sparc and alpha need 16bit-alignment for accessing header->id |
758 | * (which is uint16_t). Because of the header = (HEADER*) buf; | |
212380e3 AC |
759 | * lateron, this is neeeded. --FaUl |
760 | */ | |
761 | #if defined(__sparc__) || defined(__alpha__) | |
762 | __attribute__ ((aligned(16))) | |
763 | #endif | |
764 | ; | |
765 | HEADER *header; | |
766 | struct reslist *request = NULL; | |
767 | struct DNSReply *reply = NULL; | |
768 | int rc; | |
769 | int answer_count; | |
e7046ee5 VY |
770 | socklen_t len = sizeof(struct rb_sockaddr_storage); |
771 | struct rb_sockaddr_storage lsin; | |
5bd79c2c | 772 | int ns; |
212380e3 | 773 | |
0f6d6f3d | 774 | rc = recvfrom(rb_get_fd(F), buf, sizeof(buf), 0, (struct sockaddr *)&lsin, &len); |
212380e3 | 775 | |
ce1cab0f JT |
776 | /* No packet */ |
777 | if (rc == 0 || rc == -1) | |
778 | return 0; | |
779 | ||
780 | /* Too small */ | |
212380e3 | 781 | if (rc <= (int)(sizeof(HEADER))) |
ce1cab0f | 782 | return 1; |
212380e3 AC |
783 | |
784 | /* | |
785 | * convert DNS reply reader from Network byte order to CPU byte order. | |
786 | */ | |
29c92cf9 | 787 | header = (HEADER *)(void *)buf; |
212380e3 AC |
788 | header->ancount = ntohs(header->ancount); |
789 | header->qdcount = ntohs(header->qdcount); | |
790 | header->nscount = ntohs(header->nscount); | |
791 | header->arcount = ntohs(header->arcount); | |
792 | ||
793 | /* | |
794 | * response for an id which we have already received an answer for | |
795 | * just ignore this response. | |
796 | */ | |
797 | if (0 == (request = find_id(header->id))) | |
ce1cab0f | 798 | return 1; |
212380e3 AC |
799 | |
800 | /* | |
801 | * check against possibly fake replies | |
802 | */ | |
5bd79c2c KB |
803 | ns = res_ourserver(&lsin); |
804 | if (ns == -1) | |
ce1cab0f | 805 | return 1; |
212380e3 | 806 | |
5bd79c2c KB |
807 | if (ns != request->lastns) |
808 | { | |
809 | /* | |
810 | * We'll accept the late reply, but penalize it a little more to make | |
811 | * sure a laggy server doesn't end up favored. | |
812 | */ | |
813 | ns_failure_count[ns] += 3; | |
814 | } | |
815 | ||
816 | ||
212380e3 | 817 | if (!check_question(request, header, buf, buf + rc)) |
ce1cab0f | 818 | return 1; |
212380e3 AC |
819 | |
820 | if ((header->rcode != NO_ERRORS) || (header->ancount == 0)) | |
821 | { | |
5bd79c2c KB |
822 | /* |
823 | * RFC 2136 states that in the event of a server returning SERVFAIL | |
824 | * or NOTIMP, the request should be resent to the next server. | |
825 | * Additionally, if the server refuses our query, resend it as well. | |
826 | * -- mr_flea | |
827 | */ | |
828 | if (SERVFAIL == header->rcode || NOTIMP == header->rcode || | |
829 | REFUSED == header->rcode) | |
212380e3 | 830 | { |
5bd79c2c KB |
831 | ns_failure_count[ns]++; |
832 | resend_query(request); | |
212380e3 AC |
833 | } |
834 | else | |
835 | { | |
836 | /* | |
5bd79c2c KB |
837 | * Either a fatal error was returned or no answer. Cancel the |
838 | * request. | |
212380e3 | 839 | */ |
5bd79c2c KB |
840 | if (NXDOMAIN == header->rcode) |
841 | { | |
842 | /* If the rcode is NXDOMAIN, treat it as a good response. */ | |
843 | ns_failure_count[ns] /= 4; | |
844 | } | |
212380e3 AC |
845 | (*request->query->callback) (request->query->ptr, NULL); |
846 | rem_request(request); | |
847 | } | |
ce1cab0f | 848 | return 1; |
212380e3 AC |
849 | } |
850 | /* | |
5bd79c2c KB |
851 | * If this fails there was an error decoding the received packet. |
852 | * -- jilles | |
212380e3 AC |
853 | */ |
854 | answer_count = proc_answer(request, header, buf, buf + rc); | |
855 | ||
856 | if (answer_count) | |
857 | { | |
858 | if (request->type == T_PTR) | |
859 | { | |
860 | if (request->name == NULL) | |
861 | { | |
862 | /* | |
5bd79c2c KB |
863 | * Got a PTR response with no name, something strange is |
864 | * happening. Try another DNS server. | |
212380e3 | 865 | */ |
5bd79c2c KB |
866 | ns_failure_count[ns]++; |
867 | resend_query(request); | |
ce1cab0f | 868 | return 1; |
212380e3 AC |
869 | } |
870 | ||
871 | /* | |
872 | * Lookup the 'authoritative' name that we were given for the | |
55abcbb2 | 873 | * ip#. |
212380e3 | 874 | */ |
ccda6e3f | 875 | #ifdef RB_IPV6 |
212380e3 | 876 | if (request->addr.ss_family == AF_INET6) |
3881a3ce | 877 | gethost_byname_type_fqdn(request->name, request->query, T_AAAA); |
212380e3 AC |
878 | else |
879 | #endif | |
3881a3ce | 880 | gethost_byname_type_fqdn(request->name, request->query, T_A); |
212380e3 AC |
881 | rem_request(request); |
882 | } | |
883 | else | |
884 | { | |
885 | /* | |
886 | * got a name and address response, client resolved | |
887 | */ | |
888 | reply = make_dnsreply(request); | |
889 | (*request->query->callback) (request->query->ptr, reply); | |
637c4932 | 890 | rb_free(reply); |
212380e3 AC |
891 | rem_request(request); |
892 | } | |
5bd79c2c KB |
893 | |
894 | ns_failure_count[ns] /= 4; | |
212380e3 AC |
895 | } |
896 | else | |
897 | { | |
5bd79c2c KB |
898 | /* Invalid or corrupt reply - try another resolver. */ |
899 | ns_failure_count[ns]++; | |
900 | resend_query(request); | |
212380e3 | 901 | } |
ce1cab0f JT |
902 | return 1; |
903 | } | |
904 | ||
905 | static void res_readreply(rb_fde_t *F, void *data) | |
906 | { | |
907 | while (res_read_single_reply(F, data)) | |
908 | ; | |
909 | rb_setselect(F, RB_SELECT_READ, res_readreply, NULL); | |
212380e3 AC |
910 | } |
911 | ||
912 | static struct DNSReply *make_dnsreply(struct reslist *request) | |
913 | { | |
914 | struct DNSReply *cp; | |
915 | s_assert(request != 0); | |
916 | ||
eddc2ab6 | 917 | cp = (struct DNSReply *)rb_malloc(sizeof(struct DNSReply)); |
212380e3 AC |
918 | |
919 | cp->h_name = request->name; | |
920 | memcpy(&cp->addr, &request->addr, sizeof(cp->addr)); | |
921 | return (cp); | |
922 | } | |
923 | ||
924 | void report_dns_servers(struct Client *source_p) | |
925 | { | |
926 | int i; | |
927 | char ipaddr[128]; | |
928 | ||
929 | for (i = 0; i < irc_nscount; i++) | |
930 | { | |
caa4d9d2 | 931 | if (!rb_inet_ntop_sock((struct sockaddr *)&(irc_nsaddr_list[i]), |
212380e3 | 932 | ipaddr, sizeof ipaddr)) |
f427c8b0 | 933 | rb_strlcpy(ipaddr, "?", sizeof ipaddr); |
212380e3 | 934 | sendto_one_numeric(source_p, RPL_STATSDEBUG, |
5bd79c2c | 935 | "A %s %d", ipaddr, ns_failure_count[i]); |
212380e3 AC |
936 | } |
937 | } |