2 Unix SMB/CIFS implementation.
4 async getaddrinfo()/dns_lookup() name resolution module
6 Copyright (C) Andrew Tridgell 2005
7 Copyright (C) Stefan Metzmacher 2008
8 Copyright (C) Matthieu Patou 2011
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 this module uses a fork() per getaddrinfo() or dns_looup() call.
26 At first that might seem crazy, but it is actually very fast,
27 and solves many of the tricky problems of keeping a child
28 hanging around in a librar (like what happens when the parent forks).
29 We use a talloc destructor to ensure that the child is cleaned up
30 when we have finished with this name resolution.
34 #include "lib/events/events.h"
35 #include "system/network.h"
36 #include "system/filesys.h"
37 #include "lib/socket/socket.h"
38 #include "libcli/composite/composite.h"
39 #include "librpc/gen_ndr/ndr_nbt.h"
40 #include "libcli/resolve/resolve.h"
41 #include "lib/util/util_net.h"
42 #include "lib/addns/dnsquery.h"
43 #include "lib/addns/dns.h"
44 #include <arpa/nameser.h>
52 struct socket_address
**addrs
;
56 struct tevent_fd
*fde
;
57 struct tevent_context
*event_ctx
;
61 kill off a wayward child if needed. This allows us to stop an async
62 name resolution without leaving a potentially blocking call running
65 static int dns_ex_destructor(struct dns_ex_state
*state
)
69 kill(state
->child
, SIGTERM
);
70 if (waitpid(state
->child
, &status
, WNOHANG
) == 0) {
71 kill(state
->child
, SIGKILL
);
72 waitpid(state
->child
, &status
, 0);
78 struct dns_records_container
{
83 static int reply_to_addrs(TALLOC_CTX
*mem_ctx
, uint32_t *a_num
,
84 char ***cur_addrs
, uint32_t total
,
85 struct dns_request
*reply
, int port
)
87 char addrstr
[INET6_ADDRSTRLEN
];
93 /* at most we over-allocate here, but not by much */
94 addrs
= talloc_realloc(mem_ctx
, *cur_addrs
, char *,
95 total
+ reply
->num_answers
);
101 for (i
= 0; i
< reply
->num_answers
; i
++) {
102 rr
= reply
->answers
[i
];
104 /* we are only interested in the IN class */
105 if (rr
->r_class
!= DNS_CLASS_IN
) {
109 if (rr
->type
== QTYPE_NS
) {
111 * After the record for NS will come the A or AAAA
117 /* verify we actually have a record here */
122 /* we are only interested in A and AAAA records */
125 addr
= inet_ntop(AF_INET
,
126 (struct in_addr
*)rr
->data
,
127 addrstr
, sizeof(addrstr
));
134 addr
= inet_ntop(AF_INET6
,
135 (struct in6_addr
*)rr
->data
,
136 addrstr
, sizeof(addrstr
));
148 addrs
[total
] = talloc_asprintf(addrs
, "%s@%u/%s",
150 rr
->name
->pLabelList
->label
);
153 if (rr
->type
== QTYPE_A
) {
162 static DNS_ERROR
dns_lookup(TALLOC_CTX
*mem_ctx
, const char* name
,
163 uint16_t q_type
, struct dns_request
**reply
)
168 struct dns_buffer buf
;
171 /* give space for a good sized answer by default */
175 answer
= talloc_realloc(mem_ctx
, answer
, uint8_t, len
);
177 return ERROR_DNS_NO_MEMORY
;
179 rlen
= res_search(name
, DNS_CLASS_IN
, q_type
, answer
, len
);
182 return ERROR_DNS_SOCKET_ERROR
;
184 /* retry once with max packet size */
187 } else if (rlen
> len
) {
198 buf
.error
= ERROR_DNS_SUCCESS
;
200 err
= dns_unmarshall_request(mem_ctx
, &buf
, reply
);
206 static struct dns_records_container
get_a_aaaa_records(TALLOC_CTX
*mem_ctx
,
210 struct dns_request
*reply
;
211 struct dns_records_container ret
;
213 uint32_t a_num
, total
;
218 memset(&ret
, 0, sizeof(struct dns_records_container
));
220 tmp_ctx
= talloc_new(mem_ctx
);
227 /* this is the blocking call we are going to lots of trouble
228 to avoid them in the parent */
229 err
= dns_lookup(tmp_ctx
, name
, qtype
, &reply
);
230 if (!ERR_DNS_IS_OK(err
)) {
232 err
= dns_lookup(tmp_ctx
, name
, qtype
, &reply
);
233 if (!ERR_DNS_IS_OK(err
)) {
239 total
= reply_to_addrs(tmp_ctx
, &a_num
, &addrs
, total
, reply
, port
);
241 if (qtype
== QTYPE_AAAA
&& a_num
== 0) {
243 * DNS server didn't returned A when asked for AAAA records.
244 * Most of the server do it, let's ask for A specificaly.
246 err
= dns_lookup(tmp_ctx
, name
, QTYPE_A
, &reply
);
247 if (!ERR_DNS_IS_OK(err
)) {
251 total
= reply_to_addrs(tmp_ctx
, &a_num
, &addrs
, total
,
257 talloc_steal(mem_ctx
, addrs
);
263 TALLOC_FREE(tmp_ctx
);
267 static struct dns_records_container
get_srv_records(TALLOC_CTX
*mem_ctx
,
270 struct dns_records_container ret
;
272 struct dns_rr_srv
*dclist
;
278 memset(&ret
, 0, sizeof(struct dns_records_container
));
279 /* this is the blocking call we are going to lots of trouble
280 to avoid them in the parent */
281 status
= ads_dns_lookup_srv(mem_ctx
, NULL
, name
, &dclist
, &count
);
282 if (!NT_STATUS_IS_OK(status
)) {
290 /* Loop over all returned records and pick the records */
291 for (i
= 0; i
< count
; i
++) {
292 struct dns_records_container c
;
295 tmp_str
= dclist
[i
].hostname
;
296 if (strchr(tmp_str
, '.') && tmp_str
[strlen(tmp_str
)-1] != '.') {
297 /* we are asking for a fully qualified name, but the
298 name doesn't end in a '.'. We need to prevent the
299 DNS library trying the search domains configured in
301 tmp_str
= talloc_asprintf(mem_ctx
, "%s.", tmp_str
);
304 c
= get_a_aaaa_records(mem_ctx
, tmp_str
, dclist
[i
].port
);
311 addrs
= talloc_realloc(mem_ctx
, addrs
, char*, total
);
312 for (j
=0; j
< c
.count
; j
++) {
313 addrs
[total
- j
- 1] = talloc_steal(addrs
, c
.list
[j
]);
328 static void run_child_dns_lookup(struct dns_ex_state
*state
, int fd
)
331 bool do_srv
= (state
->flags
& RESOLVE_NAME_FLAG_DNS_SRV
);
332 struct dns_records_container c
;
336 if (strchr(state
->name
.name
, '.') && state
->name
.name
[strlen(state
->name
.name
)-1] != '.') {
337 /* we are asking for a fully qualified name, but the
338 name doesn't end in a '.'. We need to prevent the
339 DNS library trying the search domains configured in
341 state
->name
.name
= talloc_strdup_append(discard_const_p(char, state
->name
.name
),
347 c
= get_srv_records(state
, state
->name
.name
);
349 c
= get_a_aaaa_records(state
, state
->name
.name
, state
->port
);
352 /* This line in critical - if we return without writing to the
353 * pipe, this is the signal that the name did not exist */
358 addrs
= talloc_strdup(state
, "");
364 for (i
=0; i
< c
.count
; i
++) {
365 addrs
= talloc_asprintf_append_buffer(addrs
, "%s%s",
372 DEBUG(11, ("Addrs = %s\n", addrs
));
373 write(fd
, addrs
, talloc_get_size(addrs
));
383 static void run_child_getaddrinfo(struct dns_ex_state
*state
, int fd
)
386 struct addrinfo hints
;
387 struct addrinfo
*res
;
388 struct addrinfo
*res_list
= NULL
;
393 hints
.ai_socktype
= SOCK_STREAM
;
394 hints
.ai_flags
= AI_ADDRCONFIG
| AI_NUMERICSERV
;
396 ret
= getaddrinfo(state
->name
.name
, "0", &hints
, &res_list
);
397 /* try to fallback in case of error */
398 if (state
->do_fallback
) {
404 /* Linux returns EAI_NODATA on non-RFC1034-compliant names. FreeBSD returns EAI_FAIL */
406 /* getaddrinfo() doesn't handle CNAME or non-RFC1034 compatible records */
407 run_child_dns_lookup(state
, fd
);
417 addrs
= talloc_strdup(state
, "");
422 for (res
= res_list
; res
; res
= res
->ai_next
) {
423 char addrstr
[INET6_ADDRSTRLEN
];
424 if (!print_sockaddr_len(addrstr
, sizeof(addrstr
), (struct sockaddr
*)res
->ai_addr
, res
->ai_addrlen
)) {
427 addrs
= talloc_asprintf_append_buffer(addrs
, "%s%s@%u/%s",
439 write(fd
, addrs
, talloc_get_size(addrs
));
443 freeaddrinfo(res_list
);
449 handle a read event on the pipe
451 static void pipe_handler(struct tevent_context
*ev
, struct tevent_fd
*fde
,
452 uint16_t flags
, void *private_data
)
454 struct composite_context
*c
= talloc_get_type(private_data
, struct composite_context
);
455 struct dns_ex_state
*state
= talloc_get_type(c
->private_data
,
456 struct dns_ex_state
);
458 uint32_t num_addrs
, i
;
464 /* if we get any event from the child then we know that we
465 won't need to kill it off */
466 talloc_set_destructor(state
, NULL
);
468 if (ioctl(state
->child_fd
, FIONREAD
, &value
) != 0) {
472 address
= talloc_array(state
, char, value
+1);
474 /* yes, we don't care about EAGAIN or other niceities
475 here. They just can't happen with this parent/child
476 relationship, and even if they did then giving an error is
477 the right thing to do */
478 ret
= read(state
->child_fd
, address
, value
);
482 if (waitpid(state
->child
, &status
, WNOHANG
) == 0) {
483 kill(state
->child
, SIGKILL
);
484 waitpid(state
->child
, &status
, 0);
488 /* The check for ret == 0 here is important, if the
489 * name does not exist, then no bytes are written to
491 DEBUG(3,("dns child failed to find name '%s' of type %s\n",
492 state
->name
.name
, (state
->flags
& RESOLVE_NAME_FLAG_DNS_SRV
)?"SRV":"A"));
493 composite_error(c
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
497 /* enusre the address looks good */
500 addrs
= str_list_make(state
, address
, ",");
501 if (composite_nomem(addrs
, c
)) return;
503 num_addrs
= str_list_length((const char * const *)addrs
);
505 state
->addrs
= talloc_array(state
, struct socket_address
*,
507 if (composite_nomem(state
->addrs
, c
)) return;
509 state
->names
= talloc_array(state
, char *, num_addrs
+1);
510 if (composite_nomem(state
->names
, c
)) return;
512 for (i
=0; i
< num_addrs
; i
++) {
514 char *p
= strrchr(addrs
[i
], '@');
518 composite_error(c
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
527 composite_error(c
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
534 if (strcmp(addrs
[i
], "0.0.0.0") == 0) {
535 composite_error(c
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
538 port
= strtoul(p
, NULL
, 10);
539 if (port
> UINT16_MAX
) {
540 composite_error(c
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
543 state
->addrs
[i
] = socket_address_from_strings(state
->addrs
,
547 if (composite_nomem(state
->addrs
[i
], c
)) return;
549 state
->names
[i
] = talloc_strdup(state
->names
, n
);
550 if (composite_nomem(state
->names
[i
], c
)) return;
552 state
->addrs
[i
] = NULL
;
553 state
->names
[i
] = NULL
;
559 getaddrinfo() or dns_lookup() name resolution method - async send
561 struct composite_context
*resolve_name_dns_ex_send(TALLOC_CTX
*mem_ctx
,
562 struct tevent_context
*event_ctx
,
566 struct nbt_name
*name
,
569 struct composite_context
*c
;
570 struct dns_ex_state
*state
;
571 int fd
[2] = { -1, -1 };
574 c
= composite_create(mem_ctx
, event_ctx
);
575 if (c
== NULL
) return NULL
;
577 if (flags
& RESOLVE_NAME_FLAG_FORCE_NBT
) {
578 composite_error(c
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
582 state
= talloc_zero(c
, struct dns_ex_state
);
583 if (composite_nomem(state
, c
)) return c
;
584 c
->private_data
= state
;
586 c
->status
= nbt_name_dup(state
, name
, &state
->name
);
587 if (!composite_is_ok(c
)) return c
;
589 /* setup a pipe to chat to our child */
592 composite_error(c
, map_nt_error_from_unix_common(errno
));
596 state
->do_fallback
= do_fallback
;
597 state
->flags
= flags
;
600 state
->child_fd
= fd
[0];
601 state
->event_ctx
= c
->event_ctx
;
603 /* we need to put the child in our event context so
604 we know when the dns_lookup() has finished */
605 state
->fde
= tevent_add_fd(c
->event_ctx
, c
, state
->child_fd
, TEVENT_FD_READ
,
607 if (composite_nomem(state
->fde
, c
)) {
612 tevent_fd_set_auto_close(state
->fde
);
614 state
->child
= fork();
615 if (state
->child
== (pid_t
)-1) {
616 composite_error(c
, map_nt_error_from_unix_common(errno
));
620 if (state
->child
== 0) {
622 if (state
->flags
& RESOLVE_NAME_FLAG_FORCE_DNS
) {
623 run_child_dns_lookup(state
, fd
[1]);
625 run_child_getaddrinfo(state
, fd
[1]);
631 /* cleanup wayward children */
632 talloc_set_destructor(state
, dns_ex_destructor
);
638 getaddrinfo() or dns_lookup() name resolution method - recv side
640 NTSTATUS
resolve_name_dns_ex_recv(struct composite_context
*c
,
642 struct socket_address
***addrs
,
647 status
= composite_wait(c
);
649 if (NT_STATUS_IS_OK(status
)) {
650 struct dns_ex_state
*state
= talloc_get_type(c
->private_data
,
651 struct dns_ex_state
);
652 *addrs
= talloc_steal(mem_ctx
, state
->addrs
);
654 *names
= talloc_steal(mem_ctx
, state
->names
);