]>
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 | |
18 | * All we really care about is the IP -> hostname mappings. Thats all. | |
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 | |
26 | */ | |
27 | ||
28 | #include "stdinc.h" | |
29 | #include "ircd_defs.h" | |
30 | #include "common.h" | |
31 | #include "ircd.h" | |
32 | #include "commio.h" | |
33 | #include "res.h" | |
34 | #include "reslib.h" | |
35 | #include "tools.h" | |
36 | #include "event.h" | |
37 | #include "irc_string.h" | |
38 | #include "sprintf_irc.h" | |
39 | #include "numeric.h" | |
40 | #include "client.h" /* SNO_* */ | |
41 | ||
42 | #if (CHAR_BIT != 8) | |
43 | #error this code needs to be able to address individual octets | |
44 | #endif | |
45 | ||
46 | static PF res_readreply; | |
47 | ||
48 | #define MAXPACKET 1024 /* rfc sez 512 but we expand names so ... */ | |
49 | #define RES_MAXALIASES 35 /* maximum aliases allowed */ | |
50 | #define RES_MAXADDRS 35 /* maximum addresses allowed */ | |
51 | #define AR_TTL 600 /* TTL in seconds for dns cache entries */ | |
52 | ||
53 | /* RFC 1104/1105 wasn't very helpful about what these fields | |
54 | * should be named, so for now, we'll just name them this way. | |
55 | * we probably should look at what named calls them or something. | |
56 | */ | |
57 | #define TYPE_SIZE (size_t)2 | |
58 | #define CLASS_SIZE (size_t)2 | |
59 | #define TTL_SIZE (size_t)4 | |
60 | #define RDLENGTH_SIZE (size_t)2 | |
61 | #define ANSWER_FIXED_SIZE (TYPE_SIZE + CLASS_SIZE + TTL_SIZE + RDLENGTH_SIZE) | |
62 | ||
63 | typedef enum | |
64 | { | |
65 | REQ_IDLE, /* We're doing not much at all */ | |
66 | REQ_PTR, /* Looking up a PTR */ | |
67 | REQ_A, /* Looking up an A or AAAA */ | |
68 | REQ_CNAME /* We got a CNAME in response, we better get a real answer next */ | |
69 | } request_state; | |
70 | ||
71 | struct reslist | |
72 | { | |
73 | dlink_node node; | |
74 | int id; | |
75 | int sent; /* number of requests sent */ | |
76 | request_state state; /* State the resolver machine is in */ | |
77 | time_t ttl; | |
78 | char type; | |
79 | char queryname[128]; /* name currently being queried */ | |
80 | char retries; /* retry counter */ | |
81 | char sends; /* number of sends (>1 means resent) */ | |
82 | char resend; /* send flag. 0 == dont resend */ | |
83 | time_t sentat; | |
84 | time_t timeout; | |
85 | struct irc_sockaddr_storage addr; | |
86 | char *name; | |
87 | struct DNSQuery *query; /* query callback for this request */ | |
88 | }; | |
89 | ||
90 | static int res_fd; | |
91 | static dlink_list request_list = { NULL, NULL, 0 }; | |
92 | ||
93 | static void rem_request(struct reslist *request); | |
94 | static struct reslist *make_request(struct DNSQuery *query); | |
95 | static void do_query_name(struct DNSQuery *query, const char *name, struct reslist *request, int); | |
96 | static void do_query_number(struct DNSQuery *query, const struct irc_sockaddr_storage *, | |
97 | struct reslist *request); | |
98 | static void query_name(struct reslist *request); | |
99 | static int send_res_msg(const char *buf, int len, int count); | |
100 | static void resend_query(struct reslist *request); | |
101 | static int check_question(struct reslist *request, HEADER * header, char *buf, char *eob); | |
102 | static int proc_answer(struct reslist *request, HEADER * header, char *, char *); | |
103 | static struct reslist *find_id(int id); | |
104 | static struct DNSReply *make_dnsreply(struct reslist *request); | |
105 | ||
106 | extern struct irc_sockaddr_storage irc_nsaddr_list[IRCD_MAXNS]; | |
107 | extern int irc_nscount; | |
108 | extern char irc_domain[HOSTLEN + 1]; | |
109 | ||
110 | ||
111 | /* | |
112 | * int | |
113 | * res_ourserver(inp) | |
114 | * looks up "inp" in irc_nsaddr_list[] | |
115 | * returns: | |
116 | * 0 : not found | |
117 | * >0 : found | |
118 | * author: | |
119 | * paul vixie, 29may94 | |
120 | * revised for ircd, cryogen(stu) may03 | |
121 | */ | |
122 | static int res_ourserver(const struct irc_sockaddr_storage *inp) | |
123 | { | |
124 | #ifdef IPV6 | |
125 | struct sockaddr_in6 *v6; | |
126 | struct sockaddr_in6 *v6in = (struct sockaddr_in6 *)inp; | |
127 | #endif | |
128 | struct sockaddr_in *v4; | |
129 | struct sockaddr_in *v4in = (struct sockaddr_in *)inp; | |
130 | int ns; | |
131 | ||
132 | for (ns = 0; ns < irc_nscount; ns++) | |
133 | { | |
134 | const struct irc_sockaddr_storage *srv = &irc_nsaddr_list[ns]; | |
135 | #ifdef IPV6 | |
136 | v6 = (struct sockaddr_in6 *)srv; | |
137 | #endif | |
138 | v4 = (struct sockaddr_in *)srv; | |
139 | ||
140 | /* could probably just memcmp(srv, inp, srv.ss_len) here | |
141 | * but we'll air on the side of caution - stu | |
142 | */ | |
143 | switch (srv->ss_family) | |
144 | { | |
145 | #ifdef IPV6 | |
146 | case AF_INET6: | |
147 | if (srv->ss_family == inp->ss_family) | |
148 | if (v6->sin6_port == v6in->sin6_port) | |
149 | if ((memcmp(&v6->sin6_addr.s6_addr, &v6in->sin6_addr.s6_addr, | |
150 | sizeof(struct in6_addr)) == 0) || | |
151 | (memcmp(&v6->sin6_addr.s6_addr, &in6addr_any, | |
152 | sizeof(struct in6_addr)) == 0)) | |
153 | return 1; | |
154 | break; | |
155 | #endif | |
156 | case AF_INET: | |
157 | if (srv->ss_family == inp->ss_family) | |
158 | if (v4->sin_port == v4in->sin_port) | |
159 | if ((v4->sin_addr.s_addr == INADDR_ANY) | |
160 | || (v4->sin_addr.s_addr == v4in->sin_addr.s_addr)) | |
161 | return 1; | |
162 | break; | |
163 | default: | |
164 | break; | |
165 | } | |
166 | } | |
167 | ||
168 | return 0; | |
169 | } | |
170 | ||
171 | /* | |
172 | * timeout_query_list - Remove queries from the list which have been | |
173 | * there too long without being resolved. | |
174 | */ | |
175 | static time_t timeout_query_list(time_t now) | |
176 | { | |
177 | dlink_node *ptr; | |
178 | dlink_node *next_ptr; | |
179 | struct reslist *request; | |
180 | time_t next_time = 0; | |
181 | time_t timeout = 0; | |
182 | ||
183 | DLINK_FOREACH_SAFE(ptr, next_ptr, request_list.head) | |
184 | { | |
185 | request = ptr->data; | |
186 | timeout = request->sentat + request->timeout; | |
187 | ||
188 | if (now >= timeout) | |
189 | { | |
190 | if (--request->retries <= 0) | |
191 | { | |
192 | (*request->query->callback) (request->query->ptr, NULL); | |
193 | rem_request(request); | |
194 | continue; | |
195 | } | |
196 | else | |
197 | { | |
198 | request->sentat = now; | |
199 | request->timeout += request->timeout; | |
200 | resend_query(request); | |
201 | } | |
202 | } | |
203 | ||
204 | if ((next_time == 0) || timeout < next_time) | |
205 | { | |
206 | next_time = timeout; | |
207 | } | |
208 | } | |
209 | ||
210 | return (next_time > now) ? next_time : (now + AR_TTL); | |
211 | } | |
212 | ||
213 | /* | |
214 | * timeout_resolver - check request list | |
215 | */ | |
216 | static void timeout_resolver(void *notused) | |
217 | { | |
218 | timeout_query_list(CurrentTime); | |
219 | } | |
220 | ||
221 | /* | |
222 | * start_resolver - do everything we need to read the resolv.conf file | |
223 | * and initialize the resolver file descriptor if needed | |
224 | */ | |
225 | static void start_resolver(void) | |
226 | { | |
227 | irc_res_init(); | |
228 | ||
229 | if (res_fd <= 0) /* there isn't any such thing as fd 0, that's just a myth. */ | |
230 | { | |
231 | if ((res_fd = comm_socket(irc_nsaddr_list[0].ss_family, SOCK_DGRAM, 0, | |
232 | "UDP resolver socket")) == -1) | |
233 | return; | |
234 | ||
235 | /* At the moment, the resolver FD data is global .. */ | |
236 | comm_setselect(res_fd, FDLIST_NONE, COMM_SELECT_READ, res_readreply, NULL, 0); | |
237 | eventAdd("timeout_resolver", timeout_resolver, NULL, 1); | |
238 | } | |
239 | } | |
240 | ||
241 | /* | |
242 | * init_resolver - initialize resolver and resolver library | |
243 | */ | |
244 | void init_resolver(void) | |
245 | { | |
246 | #ifdef HAVE_SRAND48 | |
247 | srand48(CurrentTime); | |
248 | #endif | |
249 | start_resolver(); | |
250 | } | |
251 | ||
252 | /* | |
253 | * restart_resolver - reread resolv.conf, reopen socket | |
254 | */ | |
255 | void restart_resolver(void) | |
256 | { | |
257 | comm_close(res_fd); | |
258 | res_fd = -1; | |
259 | eventDelete(timeout_resolver, NULL); /* -ddosen */ | |
260 | start_resolver(); | |
261 | } | |
262 | ||
263 | /* | |
264 | * add_local_domain - Add the domain to hostname, if it is missing | |
265 | * (as suggested by eps@TOASTER.SFSU.EDU) | |
266 | */ | |
267 | void add_local_domain(char *hname, size_t size) | |
268 | { | |
269 | /* try to fix up unqualified names */ | |
270 | if (strchr(hname, '.') == NULL) | |
271 | { | |
272 | if (irc_domain[0]) | |
273 | { | |
274 | size_t len = strlen(hname); | |
275 | ||
276 | if ((strlen(irc_domain) + len + 2) < size) | |
277 | { | |
278 | hname[len++] = '.'; | |
279 | strcpy(hname + len, irc_domain); | |
280 | } | |
281 | } | |
282 | } | |
283 | } | |
284 | ||
285 | /* | |
286 | * rem_request - remove a request from the list. | |
287 | * This must also free any memory that has been allocated for | |
288 | * temporary storage of DNS results. | |
289 | */ | |
290 | static void rem_request(struct reslist *request) | |
291 | { | |
292 | dlinkDelete(&request->node, &request_list); | |
293 | MyFree(request->name); | |
294 | MyFree(request); | |
295 | } | |
296 | ||
297 | /* | |
298 | * make_request - Create a DNS request record for the server. | |
299 | */ | |
300 | static struct reslist *make_request(struct DNSQuery *query) | |
301 | { | |
302 | struct reslist *request = MyMalloc(sizeof(struct reslist)); | |
303 | ||
304 | request->sentat = CurrentTime; | |
305 | request->retries = 3; | |
306 | request->resend = 1; | |
307 | request->timeout = 4; /* start at 4 and exponential inc. */ | |
308 | request->query = query; | |
309 | request->state = REQ_IDLE; | |
310 | ||
311 | dlinkAdd(request, &request->node, &request_list); | |
312 | ||
313 | return request; | |
314 | } | |
315 | ||
316 | /* | |
317 | * delete_resolver_queries - cleanup outstanding queries | |
318 | * for which there no longer exist clients or conf lines. | |
319 | */ | |
320 | void delete_resolver_queries(const struct DNSQuery *query) | |
321 | { | |
322 | dlink_node *ptr; | |
323 | dlink_node *next_ptr; | |
324 | struct reslist *request; | |
325 | ||
326 | DLINK_FOREACH_SAFE(ptr, next_ptr, request_list.head) | |
327 | { | |
328 | if ((request = ptr->data) != NULL) | |
329 | { | |
330 | if (query == request->query) | |
331 | rem_request(request); | |
332 | } | |
333 | } | |
334 | } | |
335 | ||
336 | /* | |
337 | * send_res_msg - sends msg to all nameservers found in the "_res" structure. | |
338 | * This should reflect /etc/resolv.conf. We will get responses | |
339 | * which arent needed but is easier than checking to see if nameserver | |
340 | * isnt present. Returns number of messages successfully sent to | |
341 | * nameservers or -1 if no successful sends. | |
342 | */ | |
343 | static int send_res_msg(const char *msg, int len, int rcount) | |
344 | { | |
345 | int i; | |
346 | int sent = 0; | |
347 | int max_queries = IRCD_MIN(irc_nscount, rcount); | |
348 | ||
349 | /* RES_PRIMARY option is not implemented | |
350 | * if (res.options & RES_PRIMARY || 0 == max_queries) | |
351 | */ | |
352 | if (max_queries == 0) | |
353 | max_queries = 1; | |
354 | ||
42bda3f3 | 355 | for (i = 0; sent < max_queries && i < irc_nscount; i++) |
212380e3 AC |
356 | { |
357 | if (sendto(res_fd, msg, len, 0, | |
358 | (struct sockaddr *)&(irc_nsaddr_list[i]), | |
359 | GET_SS_LEN(irc_nsaddr_list[i])) == len) | |
360 | ++sent; | |
361 | } | |
362 | ||
363 | return (sent); | |
364 | } | |
365 | ||
366 | /* | |
367 | * find_id - find a dns request id (id is determined by dn_mkquery) | |
368 | */ | |
369 | static struct reslist *find_id(int id) | |
370 | { | |
371 | dlink_node *ptr; | |
372 | struct reslist *request; | |
373 | ||
374 | DLINK_FOREACH(ptr, request_list.head) | |
375 | { | |
376 | request = ptr->data; | |
377 | ||
378 | if (request->id == id) | |
379 | return (request); | |
380 | } | |
381 | ||
382 | return (NULL); | |
383 | } | |
384 | ||
385 | /* | |
386 | * gethost_byname_type - get host address from name | |
387 | * | |
388 | */ | |
389 | void gethost_byname_type(const char *name, struct DNSQuery *query, int type) | |
390 | { | |
391 | assert(name != 0); | |
392 | do_query_name(query, name, NULL, type); | |
393 | } | |
394 | ||
395 | /* | |
396 | * gethost_byaddr - get host name from address | |
397 | */ | |
398 | void gethost_byaddr(const struct irc_sockaddr_storage *addr, struct DNSQuery *query) | |
399 | { | |
400 | do_query_number(query, addr, NULL); | |
401 | } | |
402 | ||
403 | /* | |
404 | * do_query_name - nameserver lookup name | |
405 | */ | |
406 | static void do_query_name(struct DNSQuery *query, const char *name, struct reslist *request, | |
407 | int type) | |
408 | { | |
409 | char host_name[HOSTLEN + 1]; | |
410 | ||
14e23b0e | 411 | strlcpy(host_name, name, HOSTLEN + 1); |
212380e3 AC |
412 | add_local_domain(host_name, HOSTLEN); |
413 | ||
414 | if (request == NULL) | |
415 | { | |
416 | request = make_request(query); | |
417 | request->name = (char *)MyMalloc(strlen(host_name) + 1); | |
418 | strcpy(request->name, host_name); | |
419 | request->state = REQ_A; | |
420 | } | |
421 | ||
422 | strlcpy(request->queryname, host_name, sizeof(request->queryname)); | |
423 | request->type = type; | |
424 | query_name(request); | |
425 | } | |
426 | ||
427 | /* | |
428 | * do_query_number - Use this to do reverse IP# lookups. | |
429 | */ | |
430 | static void do_query_number(struct DNSQuery *query, const struct irc_sockaddr_storage *addr, | |
431 | struct reslist *request) | |
432 | { | |
433 | const unsigned char *cp; | |
434 | ||
435 | if (request == NULL) | |
436 | { | |
437 | request = make_request(query); | |
438 | memcpy(&request->addr, addr, sizeof(struct irc_sockaddr_storage)); | |
439 | request->name = (char *)MyMalloc(HOSTLEN + 1); | |
440 | } | |
441 | ||
442 | if (addr->ss_family == AF_INET) | |
443 | { | |
444 | struct sockaddr_in *v4 = (struct sockaddr_in *)addr; | |
445 | cp = (const unsigned char *)&v4->sin_addr.s_addr; | |
446 | ||
447 | ircsprintf(request->queryname, "%u.%u.%u.%u.in-addr.arpa", (unsigned int)(cp[3]), | |
448 | (unsigned int)(cp[2]), (unsigned int)(cp[1]), (unsigned int)(cp[0])); | |
449 | } | |
450 | #ifdef IPV6 | |
451 | else if (addr->ss_family == AF_INET6) | |
452 | { | |
453 | struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)addr; | |
454 | cp = (const unsigned char *)&v6->sin6_addr.s6_addr; | |
455 | ||
456 | (void)sprintf(request->queryname, "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x." | |
457 | "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa", | |
458 | (unsigned int)(cp[15] & 0xf), (unsigned int)(cp[15] >> 4), | |
459 | (unsigned int)(cp[14] & 0xf), (unsigned int)(cp[14] >> 4), | |
460 | (unsigned int)(cp[13] & 0xf), (unsigned int)(cp[13] >> 4), | |
461 | (unsigned int)(cp[12] & 0xf), (unsigned int)(cp[12] >> 4), | |
462 | (unsigned int)(cp[11] & 0xf), (unsigned int)(cp[11] >> 4), | |
463 | (unsigned int)(cp[10] & 0xf), (unsigned int)(cp[10] >> 4), | |
464 | (unsigned int)(cp[9] & 0xf), (unsigned int)(cp[9] >> 4), | |
465 | (unsigned int)(cp[8] & 0xf), (unsigned int)(cp[8] >> 4), | |
466 | (unsigned int)(cp[7] & 0xf), (unsigned int)(cp[7] >> 4), | |
467 | (unsigned int)(cp[6] & 0xf), (unsigned int)(cp[6] >> 4), | |
468 | (unsigned int)(cp[5] & 0xf), (unsigned int)(cp[5] >> 4), | |
469 | (unsigned int)(cp[4] & 0xf), (unsigned int)(cp[4] >> 4), | |
470 | (unsigned int)(cp[3] & 0xf), (unsigned int)(cp[3] >> 4), | |
471 | (unsigned int)(cp[2] & 0xf), (unsigned int)(cp[2] >> 4), | |
472 | (unsigned int)(cp[1] & 0xf), (unsigned int)(cp[1] >> 4), | |
473 | (unsigned int)(cp[0] & 0xf), (unsigned int)(cp[0] >> 4)); | |
474 | } | |
475 | #endif | |
476 | ||
477 | request->type = T_PTR; | |
478 | query_name(request); | |
479 | } | |
480 | ||
481 | /* | |
482 | * query_name - generate a query based on class, type and name. | |
483 | */ | |
484 | static void query_name(struct reslist *request) | |
485 | { | |
486 | char buf[MAXPACKET]; | |
487 | int request_len = 0; | |
488 | ||
489 | memset(buf, 0, sizeof(buf)); | |
490 | ||
491 | if ((request_len = | |
492 | irc_res_mkquery(request->queryname, C_IN, request->type, (unsigned char *)buf, sizeof(buf))) > 0) | |
493 | { | |
494 | HEADER *header = (HEADER *) buf; | |
495 | #ifndef HAVE_LRAND48 | |
496 | int k = 0; | |
497 | struct timeval tv; | |
498 | #endif | |
499 | /* | |
500 | * generate an unique id | |
501 | * NOTE: we don't have to worry about converting this to and from | |
502 | * network byte order, the nameserver does not interpret this value | |
503 | * and returns it unchanged | |
504 | */ | |
505 | #ifdef HAVE_LRAND48 | |
506 | do | |
507 | { | |
508 | header->id = (header->id + lrand48()) & 0xffff; | |
509 | } while (find_id(header->id)); | |
510 | #else | |
511 | gettimeofday(&tv, NULL); | |
512 | do | |
513 | { | |
514 | header->id = (header->id + k + tv.tv_usec) & 0xffff; | |
515 | k++; | |
516 | } while (find_id(header->id)); | |
517 | #endif /* HAVE_LRAND48 */ | |
518 | request->id = header->id; | |
519 | ++request->sends; | |
520 | ||
521 | request->sent += send_res_msg(buf, request_len, request->sends); | |
522 | } | |
523 | } | |
524 | ||
525 | static void resend_query(struct reslist *request) | |
526 | { | |
527 | if (request->resend == 0) | |
528 | return; | |
529 | ||
530 | switch (request->type) | |
531 | { | |
532 | case T_PTR: | |
533 | do_query_number(NULL, &request->addr, request); | |
534 | break; | |
535 | case T_A: | |
536 | #ifdef IPV6 | |
537 | case T_AAAA: | |
538 | #endif | |
539 | do_query_name(NULL, request->name, request, request->type); | |
540 | break; | |
541 | default: | |
542 | break; | |
543 | } | |
544 | } | |
545 | ||
546 | /* | |
547 | * check_question - check if the reply really belongs to the | |
548 | * name we queried (to guard against late replies from previous | |
549 | * queries with the same id). | |
550 | */ | |
551 | static int check_question(struct reslist *request, HEADER * header, char *buf, char *eob) | |
552 | { | |
553 | char hostbuf[128]; /* working buffer */ | |
554 | unsigned char *current; /* current position in buf */ | |
555 | int n; /* temp count */ | |
556 | ||
557 | current = (unsigned char *)buf + sizeof(HEADER); | |
558 | if (header->qdcount != 1) | |
559 | return 0; | |
560 | n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current, hostbuf, | |
561 | sizeof(hostbuf)); | |
562 | if (n <= 0) | |
563 | return 0; | |
564 | if (strcasecmp(hostbuf, request->queryname)) | |
565 | return 0; | |
566 | return 1; | |
567 | } | |
568 | ||
569 | /* | |
570 | * proc_answer - process name server reply | |
571 | */ | |
572 | static int proc_answer(struct reslist *request, HEADER * header, char *buf, char *eob) | |
573 | { | |
574 | char hostbuf[HOSTLEN + 100]; /* working buffer */ | |
575 | unsigned char *current; /* current position in buf */ | |
576 | int query_class; /* answer class */ | |
577 | int type; /* answer type */ | |
578 | int n; /* temp count */ | |
579 | int rd_length; | |
580 | struct sockaddr_in *v4; /* conversion */ | |
581 | #ifdef IPV6 | |
582 | struct sockaddr_in6 *v6; | |
583 | #endif | |
584 | current = (unsigned char *)buf + sizeof(HEADER); | |
585 | ||
586 | for (; header->qdcount > 0; --header->qdcount) | |
587 | { | |
588 | if ((n = irc_dn_skipname(current, (unsigned char *)eob)) < 0) | |
589 | return 0; | |
590 | ||
591 | current += (size_t) n + QFIXEDSZ; | |
592 | } | |
593 | ||
594 | /* | |
595 | * process each answer sent to us blech. | |
596 | */ | |
597 | while (header->ancount > 0 && (char *)current < eob) | |
598 | { | |
599 | header->ancount--; | |
600 | ||
601 | n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current, hostbuf, | |
602 | sizeof(hostbuf)); | |
603 | ||
604 | if (n < 0) | |
605 | { | |
606 | /* | |
607 | * broken message | |
608 | */ | |
609 | return (0); | |
610 | } | |
611 | else if (n == 0) | |
612 | { | |
613 | /* | |
614 | * no more answers left | |
615 | */ | |
616 | return (0); | |
617 | } | |
618 | ||
619 | hostbuf[HOSTLEN] = '\0'; | |
620 | ||
621 | /* With Address arithmetic you have to be very anal | |
622 | * this code was not working on alpha due to that | |
623 | * (spotted by rodder/jailbird/dianora) | |
624 | */ | |
625 | current += (size_t) n; | |
626 | ||
627 | if (!(((char *)current + ANSWER_FIXED_SIZE) < eob)) | |
628 | break; | |
629 | ||
630 | type = irc_ns_get16(current); | |
631 | current += TYPE_SIZE; | |
632 | ||
633 | query_class = irc_ns_get16(current); | |
634 | current += CLASS_SIZE; | |
635 | ||
636 | request->ttl = irc_ns_get32(current); | |
637 | current += TTL_SIZE; | |
638 | ||
639 | rd_length = irc_ns_get16(current); | |
640 | current += RDLENGTH_SIZE; | |
641 | ||
642 | /* | |
643 | * Wait to set request->type until we verify this structure | |
644 | */ | |
645 | switch (type) | |
646 | { | |
647 | case T_A: | |
648 | if (request->type != T_A) | |
649 | return (0); | |
650 | ||
651 | /* | |
652 | * check for invalid rd_length or too many addresses | |
653 | */ | |
654 | if (rd_length != sizeof(struct in_addr)) | |
655 | return (0); | |
656 | v4 = (struct sockaddr_in *)&request->addr; | |
657 | SET_SS_LEN(request->addr, sizeof(struct sockaddr_in)); | |
658 | v4->sin_family = AF_INET; | |
659 | memcpy(&v4->sin_addr, current, sizeof(struct in_addr)); | |
660 | return (1); | |
661 | break; | |
662 | #ifdef IPV6 | |
663 | case T_AAAA: | |
664 | if (request->type != T_AAAA) | |
665 | return (0); | |
666 | if (rd_length != sizeof(struct in6_addr)) | |
667 | return (0); | |
668 | SET_SS_LEN(request->addr, sizeof(struct sockaddr_in6)); | |
669 | v6 = (struct sockaddr_in6 *)&request->addr; | |
670 | v6->sin6_family = AF_INET6; | |
671 | memcpy(&v6->sin6_addr, current, sizeof(struct in6_addr)); | |
672 | return (1); | |
673 | break; | |
674 | #endif | |
675 | case T_PTR: | |
676 | if (request->type != T_PTR) | |
677 | return (0); | |
678 | n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current, | |
679 | hostbuf, sizeof(hostbuf)); | |
680 | if (n < 0) | |
681 | return (0); /* broken message */ | |
682 | else if (n == 0) | |
683 | return (0); /* no more answers left */ | |
684 | ||
14e23b0e | 685 | strlcpy(request->name, hostbuf, HOSTLEN + 1); |
212380e3 AC |
686 | |
687 | return (1); | |
688 | break; | |
689 | case T_CNAME: /* first check we already havent started looking | |
690 | into a cname */ | |
691 | if (request->type != T_PTR) | |
692 | return (0); | |
693 | ||
694 | if (request->state == REQ_CNAME) | |
695 | { | |
696 | n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, | |
697 | current, hostbuf, sizeof(hostbuf)); | |
698 | ||
699 | if (n < 0) | |
700 | return (0); | |
701 | return (1); | |
702 | } | |
703 | ||
704 | request->state = REQ_CNAME; | |
705 | current += rd_length; | |
706 | break; | |
707 | ||
708 | default: | |
709 | /* XXX I'd rather just throw away the entire bogus thing | |
710 | * but its possible its just a broken nameserver with still | |
711 | * valid answers. But lets do some rudimentary logging for now... | |
712 | */ | |
713 | ilog(L_MAIN, "irc_res.c bogus type %d", type); | |
714 | break; | |
715 | } | |
716 | } | |
717 | ||
718 | return (1); | |
719 | } | |
720 | ||
721 | /* | |
722 | * res_readreply - read a dns reply from the nameserver and process it. | |
723 | */ | |
724 | static void res_readreply(int fd, void *data) | |
725 | { | |
726 | char buf[sizeof(HEADER) + MAXPACKET] | |
727 | /* Sparc and alpha need 16bit-alignment for accessing header->id | |
728 | * (which is uint16_t). Because of the header = (HEADER*) buf; | |
729 | * lateron, this is neeeded. --FaUl | |
730 | */ | |
731 | #if defined(__sparc__) || defined(__alpha__) | |
732 | __attribute__ ((aligned(16))) | |
733 | #endif | |
734 | ; | |
735 | HEADER *header; | |
736 | struct reslist *request = NULL; | |
737 | struct DNSReply *reply = NULL; | |
738 | int rc; | |
739 | int answer_count; | |
740 | socklen_t len = sizeof(struct irc_sockaddr_storage); | |
741 | struct irc_sockaddr_storage lsin; | |
742 | ||
743 | rc = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *)&lsin, &len); | |
744 | ||
745 | /* Re-schedule a read *after* recvfrom, or we'll be registering | |
746 | * interest where it'll instantly be ready for read :-) -- adrian | |
747 | */ | |
748 | comm_setselect(fd, FDLIST_NONE, COMM_SELECT_READ, res_readreply, NULL, 0); | |
749 | /* Better to cast the sizeof instead of rc */ | |
750 | if (rc <= (int)(sizeof(HEADER))) | |
751 | return; | |
752 | ||
753 | /* | |
754 | * convert DNS reply reader from Network byte order to CPU byte order. | |
755 | */ | |
756 | header = (HEADER *) buf; | |
757 | header->ancount = ntohs(header->ancount); | |
758 | header->qdcount = ntohs(header->qdcount); | |
759 | header->nscount = ntohs(header->nscount); | |
760 | header->arcount = ntohs(header->arcount); | |
761 | ||
762 | /* | |
763 | * response for an id which we have already received an answer for | |
764 | * just ignore this response. | |
765 | */ | |
766 | if (0 == (request = find_id(header->id))) | |
767 | return; | |
768 | ||
769 | /* | |
770 | * check against possibly fake replies | |
771 | */ | |
772 | if (!res_ourserver(&lsin)) | |
773 | return; | |
774 | ||
775 | if (!check_question(request, header, buf, buf + rc)) | |
776 | return; | |
777 | ||
778 | if ((header->rcode != NO_ERRORS) || (header->ancount == 0)) | |
779 | { | |
780 | if (NXDOMAIN == header->rcode) | |
781 | { | |
782 | (*request->query->callback) (request->query->ptr, NULL); | |
783 | rem_request(request); | |
784 | } | |
785 | else | |
786 | { | |
787 | /* | |
788 | * If a bad error was returned, we stop here and dont send | |
789 | * send any more (no retries granted). | |
790 | */ | |
791 | (*request->query->callback) (request->query->ptr, NULL); | |
792 | rem_request(request); | |
793 | } | |
794 | return; | |
795 | } | |
796 | /* | |
797 | * If this fails there was an error decoding the received packet, | |
798 | * give up. -- jilles | |
799 | */ | |
800 | answer_count = proc_answer(request, header, buf, buf + rc); | |
801 | ||
802 | if (answer_count) | |
803 | { | |
804 | if (request->type == T_PTR) | |
805 | { | |
806 | if (request->name == NULL) | |
807 | { | |
808 | /* | |
809 | * got a PTR response with no name, something bogus is happening | |
810 | * don't bother trying again, the client address doesn't resolve | |
811 | */ | |
812 | (*request->query->callback) (request->query->ptr, reply); | |
813 | rem_request(request); | |
814 | return; | |
815 | } | |
816 | ||
817 | /* | |
818 | * Lookup the 'authoritative' name that we were given for the | |
819 | * ip#. | |
820 | * | |
821 | */ | |
822 | #ifdef IPV6 | |
823 | if (request->addr.ss_family == AF_INET6) | |
824 | gethost_byname_type(request->name, request->query, T_AAAA); | |
825 | else | |
826 | #endif | |
827 | gethost_byname_type(request->name, request->query, T_A); | |
828 | rem_request(request); | |
829 | } | |
830 | else | |
831 | { | |
832 | /* | |
833 | * got a name and address response, client resolved | |
834 | */ | |
835 | reply = make_dnsreply(request); | |
836 | (*request->query->callback) (request->query->ptr, reply); | |
837 | MyFree(reply); | |
838 | rem_request(request); | |
839 | } | |
840 | } | |
841 | else | |
842 | { | |
843 | /* couldn't decode, give up -- jilles */ | |
844 | (*request->query->callback) (request->query->ptr, NULL); | |
845 | rem_request(request); | |
846 | } | |
847 | } | |
848 | ||
849 | static struct DNSReply *make_dnsreply(struct reslist *request) | |
850 | { | |
851 | struct DNSReply *cp; | |
852 | s_assert(request != 0); | |
853 | ||
854 | cp = (struct DNSReply *)MyMalloc(sizeof(struct DNSReply)); | |
855 | ||
856 | cp->h_name = request->name; | |
857 | memcpy(&cp->addr, &request->addr, sizeof(cp->addr)); | |
858 | return (cp); | |
859 | } | |
860 | ||
861 | void report_dns_servers(struct Client *source_p) | |
862 | { | |
863 | int i; | |
864 | char ipaddr[128]; | |
865 | ||
866 | for (i = 0; i < irc_nscount; i++) | |
867 | { | |
868 | if (!inetntop_sock((struct sockaddr *)&(irc_nsaddr_list[i]), | |
869 | ipaddr, sizeof ipaddr)) | |
870 | strlcpy(ipaddr, "?", sizeof ipaddr); | |
871 | sendto_one_numeric(source_p, RPL_STATSDEBUG, | |
872 | "A %s", ipaddr); | |
873 | } | |
874 | } |