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"
45 #include "heimdal/lib/roken/resolve.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 close(state
->child_fd
);
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
;
94 bool do_srv
= (state
->flags
& RESOLVE_NAME_FLAG_DNS_SRV
);
96 /* this is the blocking call we are going to lots of trouble
97 to avoid in the parent */
98 reply
= rk_dns_lookup(state
->name
.name
, do_srv
?"SRV":"A");
104 rk_dns_srv_order(reply
);
107 /* Loop over all returned records and pick the "srv" records */
108 for (rr
=reply
->head
; rr
; rr
=rr
->next
) {
109 /* we are only interested in the IN class */
110 if (rr
->class != rk_ns_c_in
) {
115 /* we are only interested in SRV records */
116 if (rr
->type
!= rk_ns_t_srv
) {
120 /* verify we actually have a SRV record here */
125 /* Verify we got a port */
126 if (rr
->u
.srv
->port
== 0) {
130 /* we are only interested in A records */
131 /* TODO: add AAAA support */
132 if (rr
->type
!= rk_ns_t_a
) {
136 /* verify we actually have a A record here */
148 srv_rr
= talloc_zero_array(state
,
149 struct rk_resource_record
*,
155 addrs_rr
= talloc_zero_array(state
,
156 struct rk_resource_record
*,
162 /* Loop over all returned records and pick the records */
163 for (rr
=reply
->head
;rr
;rr
=rr
->next
) {
164 /* we are only interested in the IN class */
165 if (rr
->class != rk_ns_c_in
) {
170 /* we are only interested in SRV records */
171 if (rr
->type
!= rk_ns_c_in
) {
175 /* verify we actually have a srv record here */
180 /* Verify we got a port */
181 if (rr
->u
.srv
->port
== 0) {
185 srv_rr
[srv_valid
] = rr
;
188 /* we are only interested in A records */
189 /* TODO: add AAAA support */
190 if (rr
->type
!= rk_ns_t_a
) {
194 /* verify we actually have a A record here */
199 addrs_rr
[addrs_valid
] = rr
;
204 for (i
=0; i
< srv_valid
; i
++) {
205 for (rr
=reply
->head
;rr
;rr
=rr
->next
) {
207 if (rr
->class != rk_ns_c_in
) {
211 /* we are only interested in SRV records */
212 if (rr
->type
!= rk_ns_t_a
) {
216 /* verify we actually have a srv record here */
217 if (strcmp(&srv_rr
[i
]->u
.srv
->target
[0], rr
->domain
) != 0) {
227 if (addrs_valid
== 0) {
231 addrs
= talloc_strdup(state
, "");
236 for (i
=0; i
< count
; i
++) {
243 (state
->flags
& RESOLVE_NAME_FLAG_OVERWRITE_PORT
)) {
244 port
= srv_rr
[i
]->u
.srv
->port
;
249 addrs
= talloc_asprintf_append_buffer(addrs
, "%s%s:%u/%s",
251 inet_ntoa(*addrs_rr
[i
]->u
.a
),
253 addrs_rr
[i
]->domain
);
261 write(fd
, addrs
, talloc_get_size(addrs
));
271 static void run_child_getaddrinfo(struct dns_ex_state
*state
, int fd
)
274 struct addrinfo hints
;
275 struct addrinfo
*res
;
276 struct addrinfo
*res_list
= NULL
;
281 hints
.ai_socktype
= SOCK_STREAM
;
282 hints
.ai_family
= AF_INET
;/* TODO: add AF_INET6 support */
283 hints
.ai_flags
= AI_ADDRCONFIG
| AI_NUMERICSERV
;
285 ret
= getaddrinfo(state
->name
.name
, "0", &hints
, &res_list
);
286 /* try to fallback in case of error */
287 if (state
->do_fallback
) {
293 /* getaddrinfo() doesn't handle CNAME records */
294 run_child_dns_lookup(state
, fd
);
304 addrs
= talloc_strdup(state
, "");
309 for (res
= res_list
; res
; res
= res
->ai_next
) {
310 struct sockaddr_in
*in
;
312 if (res
->ai_family
!= AF_INET
) {
315 in
= (struct sockaddr_in
*)res
->ai_addr
;
317 addrs
= talloc_asprintf_append_buffer(addrs
, "%s%s:%u/%s",
319 inet_ntoa(in
->sin_addr
),
329 write(fd
, addrs
, talloc_get_size(addrs
));
333 freeaddrinfo(res_list
);
339 handle a read event on the pipe
341 static void pipe_handler(struct tevent_context
*ev
, struct tevent_fd
*fde
,
342 uint16_t flags
, void *private_data
)
344 struct composite_context
*c
= talloc_get_type(private_data
, struct composite_context
);
345 struct dns_ex_state
*state
= talloc_get_type(c
->private_data
,
346 struct dns_ex_state
);
348 uint32_t num_addrs
, i
;
354 /* if we get any event from the child then we know that we
355 won't need to kill it off */
356 talloc_set_destructor(state
, NULL
);
358 if (ioctl(state
->child_fd
, FIONREAD
, &value
) != 0) {
362 address
= talloc_array(state
, char, value
+1);
364 /* yes, we don't care about EAGAIN or other niceities
365 here. They just can't happen with this parent/child
366 relationship, and even if they did then giving an error is
367 the right thing to do */
368 ret
= read(state
->child_fd
, address
, value
);
372 close(state
->child_fd
);
373 if (waitpid(state
->child
, &status
, WNOHANG
) == 0) {
374 kill(state
->child
, SIGKILL
);
375 waitpid(state
->child
, &status
, 0);
379 DEBUG(3,("dns child failed to find name '%s' of type %s\n",
380 state
->name
.name
, (state
->flags
& RESOLVE_NAME_FLAG_DNS_SRV
)?"SRV":"A"));
381 composite_error(c
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
385 /* enusre the address looks good */
388 addrs
= str_list_make(state
, address
, ",");
389 if (composite_nomem(addrs
, c
)) return;
391 num_addrs
= str_list_length((const char * const *)addrs
);
393 state
->addrs
= talloc_array(state
, struct socket_address
*,
395 if (composite_nomem(state
->addrs
, c
)) return;
397 state
->names
= talloc_array(state
, char *, num_addrs
+1);
398 if (composite_nomem(state
->names
, c
)) return;
400 for (i
=0; i
< num_addrs
; i
++) {
402 char *p
= strrchr(addrs
[i
], ':');
406 composite_error(c
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
415 composite_error(c
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
422 if (strcmp(addrs
[i
], "0.0.0.0") == 0 ||
423 inet_addr(addrs
[i
]) == INADDR_NONE
) {
424 composite_error(c
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
427 port
= strtoul(p
, NULL
, 10);
428 if (port
> UINT16_MAX
) {
429 composite_error(c
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
432 state
->addrs
[i
] = socket_address_from_strings(state
->addrs
,
436 if (composite_nomem(state
->addrs
[i
], c
)) return;
438 state
->names
[i
] = talloc_strdup(state
->names
, n
);
439 if (composite_nomem(state
->names
[i
], c
)) return;
441 state
->addrs
[i
] = NULL
;
442 state
->names
[i
] = NULL
;
448 getaddrinfo() or dns_lookup() name resolution method - async send
450 struct composite_context
*resolve_name_dns_ex_send(TALLOC_CTX
*mem_ctx
,
451 struct tevent_context
*event_ctx
,
455 struct nbt_name
*name
,
458 struct composite_context
*c
;
459 struct dns_ex_state
*state
;
460 int fd
[2] = { -1, -1 };
463 c
= composite_create(mem_ctx
, event_ctx
);
464 if (c
== NULL
) return NULL
;
466 if (flags
& RESOLVE_NAME_FLAG_FORCE_NBT
) {
467 composite_error(c
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
471 state
= talloc_zero(c
, struct dns_ex_state
);
472 if (composite_nomem(state
, c
)) return c
;
473 c
->private_data
= state
;
475 c
->status
= nbt_name_dup(state
, name
, &state
->name
);
476 if (!composite_is_ok(c
)) return c
;
478 /* setup a pipe to chat to our child */
481 composite_error(c
, map_nt_error_from_unix(errno
));
485 state
->do_fallback
= do_fallback
;
486 state
->flags
= flags
;
489 state
->child_fd
= fd
[0];
490 state
->event_ctx
= c
->event_ctx
;
492 /* we need to put the child in our event context so
493 we know when the dns_lookup() has finished */
494 state
->fde
= event_add_fd(c
->event_ctx
, c
, state
->child_fd
, EVENT_FD_READ
,
496 if (composite_nomem(state
->fde
, c
)) {
502 state
->child
= fork();
503 if (state
->child
== (pid_t
)-1) {
504 composite_error(c
, map_nt_error_from_unix(errno
));
508 if (state
->child
== 0) {
510 if (state
->flags
& RESOLVE_NAME_FLAG_FORCE_DNS
) {
511 run_child_dns_lookup(state
, fd
[1]);
513 run_child_getaddrinfo(state
, fd
[1]);
519 /* cleanup wayward children */
520 talloc_set_destructor(state
, dns_ex_destructor
);
526 getaddrinfo() or dns_lookup() name resolution method - recv side
528 NTSTATUS
resolve_name_dns_ex_recv(struct composite_context
*c
,
530 struct socket_address
***addrs
,
535 status
= composite_wait(c
);
537 if (NT_STATUS_IS_OK(status
)) {
538 struct dns_ex_state
*state
= talloc_get_type(c
->private_data
,
539 struct dns_ex_state
);
540 *addrs
= talloc_steal(mem_ctx
, state
->addrs
);
542 *names
= talloc_steal(mem_ctx
, state
->names
);