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