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
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 this module uses a fork() per getaddrinfo() or dns_looup() call.
25 At first that might seem crazy, but it is actually very fast,
26 and solves many of the tricky problems of keeping a child
27 hanging around in a librar (like what happens when the parent forks).
28 We use a talloc destructor to ensure that the child is cleaned up
29 when we have finished with this name resolution.
33 #include "lib/events/events.h"
34 #include "system/network.h"
35 #include "system/filesys.h"
36 #include "lib/socket/socket.h"
37 #include "libcli/composite/composite.h"
38 #include "librpc/gen_ndr/ndr_nbt.h"
39 #include "libcli/resolve/resolve.h"
40 #include "lib/util/util_net.h"
46 #include "heimdal/lib/roken/resolve.h"
53 struct socket_address
**addrs
;
57 struct tevent_fd
*fde
;
58 struct tevent_context
*event_ctx
;
62 kill off a wayward child if needed. This allows us to stop an async
63 name resolution without leaving a potentially blocking call running
66 static int dns_ex_destructor(struct dns_ex_state
*state
)
70 kill(state
->child
, SIGTERM
);
71 if (waitpid(state
->child
, &status
, WNOHANG
) == 0) {
72 kill(state
->child
, SIGKILL
);
73 waitpid(state
->child
, &status
, 0);
82 static void run_child_dns_lookup(struct dns_ex_state
*state
, int fd
)
84 struct rk_dns_reply
*reply
;
85 struct rk_resource_record
*rr
;
87 uint32_t srv_valid
= 0;
88 struct rk_resource_record
**srv_rr
;
89 uint32_t addrs_valid
= 0;
90 struct rk_resource_record
**addrs_rr
;
91 struct rk_dns_reply
**srv_replies
= NULL
;
95 bool do_srv
= (state
->flags
& RESOLVE_NAME_FLAG_DNS_SRV
);
97 if (strchr(state
->name
.name
, '.') && state
->name
.name
[strlen(state
->name
.name
)-1] != '.') {
98 /* we are asking for a fully qualified name, but the
99 name doesn't end in a '.'. We need to prevent the
100 DNS library trying the search domains configured in
102 state
->name
.name
= talloc_strdup_append(discard_const_p(char, state
->name
.name
),
106 /* this is the blocking call we are going to lots of trouble
107 to avoid in the parent */
108 reply
= rk_dns_lookup(state
->name
.name
, do_srv
?"SRV":"A");
114 rk_dns_srv_order(reply
);
117 /* Loop over all returned records and pick the "srv" records */
118 for (rr
=reply
->head
; rr
; rr
=rr
->next
) {
119 /* we are only interested in the IN class */
120 if (rr
->class != rk_ns_c_in
) {
125 /* we are only interested in SRV records */
126 if (rr
->type
!= rk_ns_t_srv
) {
130 /* verify we actually have a SRV record here */
135 /* Verify we got a port */
136 if (rr
->u
.srv
->port
== 0) {
140 /* we are only interested in A or AAAA records */
141 if (rr
->type
!= rk_ns_t_a
&& rr
->type
!= rk_ns_t_aaaa
) {
145 /* verify we actually have a record here */
157 srv_rr
= talloc_zero_array(state
,
158 struct rk_resource_record
*,
164 addrs_rr
= talloc_zero_array(state
,
165 struct rk_resource_record
*,
171 srv_replies
= talloc_zero_array(state
,
172 struct rk_dns_reply
*,
178 /* Loop over all returned records and pick the records */
179 for (rr
=reply
->head
;rr
;rr
=rr
->next
) {
180 /* we are only interested in the IN class */
181 if (rr
->class != rk_ns_c_in
) {
186 /* we are only interested in SRV records */
187 if (rr
->type
!= rk_ns_t_srv
) {
191 /* verify we actually have a srv record here */
196 /* Verify we got a port */
197 if (rr
->u
.srv
->port
== 0) {
201 srv_rr
[srv_valid
] = rr
;
204 /* we are only interested in A or AAAA records */
205 if (rr
->type
!= rk_ns_t_a
&& rr
->type
!= rk_ns_t_aaaa
) {
209 /* verify we actually have a record record here */
214 addrs_rr
[addrs_valid
] = rr
;
219 for (i
=0; i
< srv_valid
; i
++) {
220 srv_replies
[i
] = rk_dns_lookup(srv_rr
[i
]->u
.srv
->target
, "A");
221 if (srv_replies
[i
] == NULL
)
224 /* Add first A record to addrs_rr */
225 for (rr
=srv_replies
[i
]->head
;rr
;rr
=rr
->next
) {
226 if (rr
->class != rk_ns_c_in
) {
230 /* we are only interested in A or AAAA records */
231 if (rr
->type
!= rk_ns_t_a
&& rr
->type
!= rk_ns_t_aaaa
) {
235 /* verify we actually have a record here */
246 if (addrs_valid
== 0) {
250 addrs
= talloc_strdup(state
, "");
255 for (i
=0; i
< addrs_valid
; i
++) {
257 char addrstr
[INET6_ADDRSTRLEN
];
264 (state
->flags
& RESOLVE_NAME_FLAG_OVERWRITE_PORT
)) {
265 port
= srv_rr
[i
]->u
.srv
->port
;
270 switch (addrs_rr
[i
]->type
) {
272 if (inet_ntop(AF_INET
, addrs_rr
[i
]->u
.a
,
273 addrstr
, sizeof(addrstr
)) == NULL
) {
279 if (inet_ntop(AF_INET6
, (struct in6_addr
*)addrs_rr
[i
]->u
.data
,
280 addrstr
, sizeof(addrstr
)) == NULL
) {
289 addrs
= talloc_asprintf_append_buffer(addrs
, "%s%s@%u/%s",
293 addrs_rr
[i
]->domain
);
301 write(fd
, addrs
, talloc_get_size(addrs
));
306 rk_dns_free_data(reply
);
307 for (i
=0; i
< srv_valid
; i
++) {
308 if (srv_replies
[i
] != NULL
)
309 rk_dns_free_data(srv_replies
[i
]);
317 static void run_child_getaddrinfo(struct dns_ex_state
*state
, int fd
)
320 struct addrinfo hints
;
321 struct addrinfo
*res
;
322 struct addrinfo
*res_list
= NULL
;
327 hints
.ai_socktype
= SOCK_STREAM
;
328 hints
.ai_flags
= AI_ADDRCONFIG
| AI_NUMERICSERV
;
330 ret
= getaddrinfo(state
->name
.name
, "0", &hints
, &res_list
);
331 /* try to fallback in case of error */
332 if (state
->do_fallback
) {
338 /* getaddrinfo() doesn't handle CNAME records */
339 run_child_dns_lookup(state
, fd
);
349 addrs
= talloc_strdup(state
, "");
354 for (res
= res_list
; res
; res
= res
->ai_next
) {
355 char addrstr
[INET6_ADDRSTRLEN
];
356 if (!print_sockaddr_len(addrstr
, sizeof(addrstr
), (struct sockaddr
*)res
->ai_addr
, res
->ai_addrlen
)) {
359 addrs
= talloc_asprintf_append_buffer(addrs
, "%s%s@%u/%s",
371 write(fd
, addrs
, talloc_get_size(addrs
));
375 freeaddrinfo(res_list
);
381 handle a read event on the pipe
383 static void pipe_handler(struct tevent_context
*ev
, struct tevent_fd
*fde
,
384 uint16_t flags
, void *private_data
)
386 struct composite_context
*c
= talloc_get_type(private_data
, struct composite_context
);
387 struct dns_ex_state
*state
= talloc_get_type(c
->private_data
,
388 struct dns_ex_state
);
390 uint32_t num_addrs
, i
;
396 /* if we get any event from the child then we know that we
397 won't need to kill it off */
398 talloc_set_destructor(state
, NULL
);
400 if (ioctl(state
->child_fd
, FIONREAD
, &value
) != 0) {
404 address
= talloc_array(state
, char, value
+1);
406 /* yes, we don't care about EAGAIN or other niceities
407 here. They just can't happen with this parent/child
408 relationship, and even if they did then giving an error is
409 the right thing to do */
410 ret
= read(state
->child_fd
, address
, value
);
414 if (waitpid(state
->child
, &status
, WNOHANG
) == 0) {
415 kill(state
->child
, SIGKILL
);
416 waitpid(state
->child
, &status
, 0);
420 DEBUG(3,("dns child failed to find name '%s' of type %s\n",
421 state
->name
.name
, (state
->flags
& RESOLVE_NAME_FLAG_DNS_SRV
)?"SRV":"A"));
422 composite_error(c
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
426 /* enusre the address looks good */
429 addrs
= str_list_make(state
, address
, ",");
430 if (composite_nomem(addrs
, c
)) return;
432 num_addrs
= str_list_length((const char * const *)addrs
);
434 state
->addrs
= talloc_array(state
, struct socket_address
*,
436 if (composite_nomem(state
->addrs
, c
)) return;
438 state
->names
= talloc_array(state
, char *, num_addrs
+1);
439 if (composite_nomem(state
->names
, c
)) return;
441 for (i
=0; i
< num_addrs
; i
++) {
443 char *p
= strrchr(addrs
[i
], '@');
447 composite_error(c
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
456 composite_error(c
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
463 if (strcmp(addrs
[i
], "0.0.0.0") == 0) {
464 composite_error(c
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
467 port
= strtoul(p
, NULL
, 10);
468 if (port
> UINT16_MAX
) {
469 composite_error(c
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
472 state
->addrs
[i
] = socket_address_from_strings(state
->addrs
,
476 if (composite_nomem(state
->addrs
[i
], c
)) return;
478 state
->names
[i
] = talloc_strdup(state
->names
, n
);
479 if (composite_nomem(state
->names
[i
], c
)) return;
481 state
->addrs
[i
] = NULL
;
482 state
->names
[i
] = NULL
;
488 getaddrinfo() or dns_lookup() name resolution method - async send
490 struct composite_context
*resolve_name_dns_ex_send(TALLOC_CTX
*mem_ctx
,
491 struct tevent_context
*event_ctx
,
495 struct nbt_name
*name
,
498 struct composite_context
*c
;
499 struct dns_ex_state
*state
;
500 int fd
[2] = { -1, -1 };
503 c
= composite_create(mem_ctx
, event_ctx
);
504 if (c
== NULL
) return NULL
;
506 if (flags
& RESOLVE_NAME_FLAG_FORCE_NBT
) {
507 composite_error(c
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
511 state
= talloc_zero(c
, struct dns_ex_state
);
512 if (composite_nomem(state
, c
)) return c
;
513 c
->private_data
= state
;
515 c
->status
= nbt_name_dup(state
, name
, &state
->name
);
516 if (!composite_is_ok(c
)) return c
;
518 /* setup a pipe to chat to our child */
521 composite_error(c
, map_nt_error_from_unix_common(errno
));
525 state
->do_fallback
= do_fallback
;
526 state
->flags
= flags
;
529 state
->child_fd
= fd
[0];
530 state
->event_ctx
= c
->event_ctx
;
532 /* we need to put the child in our event context so
533 we know when the dns_lookup() has finished */
534 state
->fde
= event_add_fd(c
->event_ctx
, c
, state
->child_fd
, EVENT_FD_READ
,
536 if (composite_nomem(state
->fde
, c
)) {
541 tevent_fd_set_auto_close(state
->fde
);
543 state
->child
= fork();
544 if (state
->child
== (pid_t
)-1) {
545 composite_error(c
, map_nt_error_from_unix_common(errno
));
549 if (state
->child
== 0) {
551 if (state
->flags
& RESOLVE_NAME_FLAG_FORCE_DNS
) {
552 run_child_dns_lookup(state
, fd
[1]);
554 run_child_getaddrinfo(state
, fd
[1]);
560 /* cleanup wayward children */
561 talloc_set_destructor(state
, dns_ex_destructor
);
567 getaddrinfo() or dns_lookup() name resolution method - recv side
569 NTSTATUS
resolve_name_dns_ex_recv(struct composite_context
*c
,
571 struct socket_address
***addrs
,
576 status
= composite_wait(c
);
578 if (NT_STATUS_IS_OK(status
)) {
579 struct dns_ex_state
*state
= talloc_get_type(c
->private_data
,
580 struct dns_ex_state
);
581 *addrs
= talloc_steal(mem_ctx
, state
->addrs
);
583 *names
= talloc_steal(mem_ctx
, state
->names
);