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
));
147 addrs
[total
] = talloc_asprintf(addrs
, "%s@%u/%s",
149 rr
->name
->pLabelList
->label
);
152 if (rr
->type
== QTYPE_A
) {
161 static DNS_ERROR
dns_lookup(TALLOC_CTX
*mem_ctx
, const char* name
,
162 uint16_t q_type
, struct dns_request
**reply
)
167 struct dns_buffer buf
;
170 /* give space for a good sized answer by default */
174 answer
= talloc_realloc(mem_ctx
, answer
, uint8_t, len
);
176 return ERROR_DNS_NO_MEMORY
;
178 rlen
= res_search(name
, DNS_CLASS_IN
, q_type
, answer
, len
);
181 return ERROR_DNS_SOCKET_ERROR
;
183 /* retry once with max packet size */
186 } else if (rlen
> len
) {
197 buf
.error
= ERROR_DNS_SUCCESS
;
199 err
= dns_unmarshall_request(mem_ctx
, &buf
, reply
);
205 static struct dns_records_container
get_a_aaaa_records(TALLOC_CTX
*mem_ctx
,
209 struct dns_request
*reply
;
210 struct dns_records_container ret
;
212 uint32_t a_num
, total
;
217 memset(&ret
, 0, sizeof(struct dns_records_container
));
219 tmp_ctx
= talloc_new(mem_ctx
);
226 /* this is the blocking call we are going to lots of trouble
227 to avoid them in the parent */
228 err
= dns_lookup(tmp_ctx
, name
, qtype
, &reply
);
229 if (!ERR_DNS_IS_OK(err
)) {
231 err
= dns_lookup(tmp_ctx
, name
, qtype
, &reply
);
232 if (!ERR_DNS_IS_OK(err
)) {
238 total
= reply_to_addrs(tmp_ctx
, &a_num
, &addrs
, total
, reply
, port
);
240 if (qtype
== QTYPE_AAAA
&& a_num
== 0) {
242 * DNS server didn't returned A when asked for AAAA records.
243 * Most of the server do it, let's ask for A specificaly.
245 err
= dns_lookup(tmp_ctx
, name
, QTYPE_A
, &reply
);
246 if (!ERR_DNS_IS_OK(err
)) {
250 total
= reply_to_addrs(tmp_ctx
, &a_num
, &addrs
, total
,
256 talloc_steal(mem_ctx
, addrs
);
262 TALLOC_FREE(tmp_ctx
);
266 static struct dns_records_container
get_srv_records(TALLOC_CTX
*mem_ctx
,
269 struct dns_records_container ret
;
271 struct dns_rr_srv
*dclist
;
277 memset(&ret
, 0, sizeof(struct dns_records_container
));
278 /* this is the blocking call we are going to lots of trouble
279 to avoid them in the parent */
280 status
= ads_dns_lookup_srv(mem_ctx
, NULL
, name
, &dclist
, &count
);
281 if (!NT_STATUS_IS_OK(status
)) {
289 /* Loop over all returned records and pick the records */
290 for (i
= 0; i
< count
; i
++) {
291 struct dns_records_container c
;
294 tmp_str
= dclist
[i
].hostname
;
295 if (strchr(tmp_str
, '.') && tmp_str
[strlen(tmp_str
)-1] != '.') {
296 /* we are asking for a fully qualified name, but the
297 name doesn't end in a '.'. We need to prevent the
298 DNS library trying the search domains configured in
300 tmp_str
= talloc_asprintf(mem_ctx
, "%s.", tmp_str
);
303 c
= get_a_aaaa_records(mem_ctx
, tmp_str
, dclist
[i
].port
);
310 addrs
= talloc_realloc(mem_ctx
, addrs
, char*, total
);
311 for (j
=0; j
< c
.count
; j
++) {
312 addrs
[total
- j
- 1] = talloc_steal(addrs
, c
.list
[j
]);
327 static void run_child_dns_lookup(struct dns_ex_state
*state
, int fd
)
330 bool do_srv
= (state
->flags
& RESOLVE_NAME_FLAG_DNS_SRV
);
331 struct dns_records_container c
;
335 if (strchr(state
->name
.name
, '.') && state
->name
.name
[strlen(state
->name
.name
)-1] != '.') {
336 /* we are asking for a fully qualified name, but the
337 name doesn't end in a '.'. We need to prevent the
338 DNS library trying the search domains configured in
340 state
->name
.name
= talloc_strdup_append(discard_const_p(char, state
->name
.name
),
346 c
= get_srv_records(state
, state
->name
.name
);
348 c
= get_a_aaaa_records(state
, state
->name
.name
, state
->port
);
351 /* This line in critical - if we return without writing to the
352 * pipe, this is the signal that the name did not exist */
357 addrs
= talloc_strdup(state
, "");
363 for (i
=0; i
< c
.count
; i
++) {
364 addrs
= talloc_asprintf_append_buffer(addrs
, "%s%s",
371 DEBUG(11, ("Addrs = %s\n", addrs
));
372 write(fd
, addrs
, talloc_get_size(addrs
));
382 static void run_child_getaddrinfo(struct dns_ex_state
*state
, int fd
)
385 struct addrinfo hints
;
386 struct addrinfo
*res
;
387 struct addrinfo
*res_list
= NULL
;
392 hints
.ai_socktype
= SOCK_STREAM
;
393 hints
.ai_flags
= AI_ADDRCONFIG
| AI_NUMERICSERV
;
395 ret
= getaddrinfo(state
->name
.name
, "0", &hints
, &res_list
);
396 /* try to fallback in case of error */
397 if (state
->do_fallback
) {
403 /* getaddrinfo() doesn't handle CNAME records */
404 run_child_dns_lookup(state
, fd
);
414 addrs
= talloc_strdup(state
, "");
419 for (res
= res_list
; res
; res
= res
->ai_next
) {
420 char addrstr
[INET6_ADDRSTRLEN
];
421 if (!print_sockaddr_len(addrstr
, sizeof(addrstr
), (struct sockaddr
*)res
->ai_addr
, res
->ai_addrlen
)) {
424 addrs
= talloc_asprintf_append_buffer(addrs
, "%s%s@%u/%s",
436 write(fd
, addrs
, talloc_get_size(addrs
));
440 freeaddrinfo(res_list
);
446 handle a read event on the pipe
448 static void pipe_handler(struct tevent_context
*ev
, struct tevent_fd
*fde
,
449 uint16_t flags
, void *private_data
)
451 struct composite_context
*c
= talloc_get_type(private_data
, struct composite_context
);
452 struct dns_ex_state
*state
= talloc_get_type(c
->private_data
,
453 struct dns_ex_state
);
455 uint32_t num_addrs
, i
;
461 /* if we get any event from the child then we know that we
462 won't need to kill it off */
463 talloc_set_destructor(state
, NULL
);
465 if (ioctl(state
->child_fd
, FIONREAD
, &value
) != 0) {
469 address
= talloc_array(state
, char, value
+1);
471 /* yes, we don't care about EAGAIN or other niceities
472 here. They just can't happen with this parent/child
473 relationship, and even if they did then giving an error is
474 the right thing to do */
475 ret
= read(state
->child_fd
, address
, value
);
479 if (waitpid(state
->child
, &status
, WNOHANG
) == 0) {
480 kill(state
->child
, SIGKILL
);
481 waitpid(state
->child
, &status
, 0);
485 /* The check for ret == 0 here is important, if the
486 * name does not exist, then no bytes are written to
488 DEBUG(3,("dns child failed to find name '%s' of type %s\n",
489 state
->name
.name
, (state
->flags
& RESOLVE_NAME_FLAG_DNS_SRV
)?"SRV":"A"));
490 composite_error(c
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
494 /* enusre the address looks good */
497 addrs
= str_list_make(state
, address
, ",");
498 if (composite_nomem(addrs
, c
)) return;
500 num_addrs
= str_list_length((const char * const *)addrs
);
502 state
->addrs
= talloc_array(state
, struct socket_address
*,
504 if (composite_nomem(state
->addrs
, c
)) return;
506 state
->names
= talloc_array(state
, char *, num_addrs
+1);
507 if (composite_nomem(state
->names
, c
)) return;
509 for (i
=0; i
< num_addrs
; i
++) {
511 char *p
= strrchr(addrs
[i
], '@');
515 composite_error(c
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
524 composite_error(c
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
531 if (strcmp(addrs
[i
], "0.0.0.0") == 0) {
532 composite_error(c
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
535 port
= strtoul(p
, NULL
, 10);
536 if (port
> UINT16_MAX
) {
537 composite_error(c
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
540 state
->addrs
[i
] = socket_address_from_strings(state
->addrs
,
544 if (composite_nomem(state
->addrs
[i
], c
)) return;
546 state
->names
[i
] = talloc_strdup(state
->names
, n
);
547 if (composite_nomem(state
->names
[i
], c
)) return;
549 state
->addrs
[i
] = NULL
;
550 state
->names
[i
] = NULL
;
556 getaddrinfo() or dns_lookup() name resolution method - async send
558 struct composite_context
*resolve_name_dns_ex_send(TALLOC_CTX
*mem_ctx
,
559 struct tevent_context
*event_ctx
,
563 struct nbt_name
*name
,
566 struct composite_context
*c
;
567 struct dns_ex_state
*state
;
568 int fd
[2] = { -1, -1 };
571 c
= composite_create(mem_ctx
, event_ctx
);
572 if (c
== NULL
) return NULL
;
574 if (flags
& RESOLVE_NAME_FLAG_FORCE_NBT
) {
575 composite_error(c
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
579 state
= talloc_zero(c
, struct dns_ex_state
);
580 if (composite_nomem(state
, c
)) return c
;
581 c
->private_data
= state
;
583 c
->status
= nbt_name_dup(state
, name
, &state
->name
);
584 if (!composite_is_ok(c
)) return c
;
586 /* setup a pipe to chat to our child */
589 composite_error(c
, map_nt_error_from_unix_common(errno
));
593 state
->do_fallback
= do_fallback
;
594 state
->flags
= flags
;
597 state
->child_fd
= fd
[0];
598 state
->event_ctx
= c
->event_ctx
;
600 /* we need to put the child in our event context so
601 we know when the dns_lookup() has finished */
602 state
->fde
= tevent_add_fd(c
->event_ctx
, c
, state
->child_fd
, TEVENT_FD_READ
,
604 if (composite_nomem(state
->fde
, c
)) {
609 tevent_fd_set_auto_close(state
->fde
);
611 state
->child
= fork();
612 if (state
->child
== (pid_t
)-1) {
613 composite_error(c
, map_nt_error_from_unix_common(errno
));
617 if (state
->child
== 0) {
619 if (state
->flags
& RESOLVE_NAME_FLAG_FORCE_DNS
) {
620 run_child_dns_lookup(state
, fd
[1]);
622 run_child_getaddrinfo(state
, fd
[1]);
628 /* cleanup wayward children */
629 talloc_set_destructor(state
, dns_ex_destructor
);
635 getaddrinfo() or dns_lookup() name resolution method - recv side
637 NTSTATUS
resolve_name_dns_ex_recv(struct composite_context
*c
,
639 struct socket_address
***addrs
,
644 status
= composite_wait(c
);
646 if (NT_STATUS_IS_OK(status
)) {
647 struct dns_ex_state
*state
= talloc_get_type(c
->private_data
,
648 struct dns_ex_state
);
649 *addrs
= talloc_steal(mem_ctx
, state
->addrs
);
651 *names
= talloc_steal(mem_ctx
, state
->names
);