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 if (waitpid(state
->child
, &status
, WNOHANG
) == 0) {
71 kill(state
->child
, SIGKILL
);
72 waitpid(state
->child
, &status
, 0);
81 static void run_child_dns_lookup(struct dns_ex_state
*state
, int fd
)
83 struct rk_dns_reply
*reply
;
84 struct rk_resource_record
*rr
;
86 uint32_t srv_valid
= 0;
87 struct rk_resource_record
**srv_rr
;
88 uint32_t addrs_valid
= 0;
89 struct rk_resource_record
**addrs_rr
;
93 bool do_srv
= (state
->flags
& RESOLVE_NAME_FLAG_DNS_SRV
);
95 /* this is the blocking call we are going to lots of trouble
96 to avoid in the parent */
97 reply
= rk_dns_lookup(state
->name
.name
, do_srv
?"SRV":"A");
103 rk_dns_srv_order(reply
);
106 /* Loop over all returned records and pick the "srv" records */
107 for (rr
=reply
->head
; rr
; rr
=rr
->next
) {
108 /* we are only interested in the IN class */
109 if (rr
->class != rk_ns_c_in
) {
114 /* we are only interested in SRV records */
115 if (rr
->type
!= rk_ns_t_srv
) {
119 /* verify we actually have a SRV record here */
124 /* Verify we got a port */
125 if (rr
->u
.srv
->port
== 0) {
129 /* we are only interested in A records */
130 /* TODO: add AAAA support */
131 if (rr
->type
!= rk_ns_t_a
) {
135 /* verify we actually have a A record here */
147 srv_rr
= talloc_zero_array(state
,
148 struct rk_resource_record
*,
154 addrs_rr
= talloc_zero_array(state
,
155 struct rk_resource_record
*,
161 /* Loop over all returned records and pick the records */
162 for (rr
=reply
->head
;rr
;rr
=rr
->next
) {
163 /* we are only interested in the IN class */
164 if (rr
->class != rk_ns_c_in
) {
169 /* we are only interested in SRV records */
170 if (rr
->type
!= rk_ns_c_in
) {
174 /* verify we actually have a srv record here */
179 /* Verify we got a port */
180 if (rr
->u
.srv
->port
== 0) {
184 srv_rr
[srv_valid
] = rr
;
187 /* we are only interested in A records */
188 /* TODO: add AAAA support */
189 if (rr
->type
!= rk_ns_t_a
) {
193 /* verify we actually have a A record here */
198 addrs_rr
[addrs_valid
] = rr
;
203 for (i
=0; i
< srv_valid
; i
++) {
204 for (rr
=reply
->head
;rr
;rr
=rr
->next
) {
206 if (rr
->class != rk_ns_c_in
) {
210 /* we are only interested in SRV records */
211 if (rr
->type
!= rk_ns_t_a
) {
215 /* verify we actually have a srv record here */
216 if (strcmp(&srv_rr
[i
]->u
.srv
->target
[0], rr
->domain
) != 0) {
226 if (addrs_valid
== 0) {
230 addrs
= talloc_strdup(state
, "");
235 for (i
=0; i
< count
; i
++) {
242 (state
->flags
& RESOLVE_NAME_FLAG_OVERWRITE_PORT
)) {
243 port
= srv_rr
[i
]->u
.srv
->port
;
248 addrs
= talloc_asprintf_append_buffer(addrs
, "%s%s:%u/%s",
250 inet_ntoa(*addrs_rr
[i
]->u
.a
),
252 addrs_rr
[i
]->domain
);
260 write(fd
, addrs
, talloc_get_size(addrs
));
270 static void run_child_getaddrinfo(struct dns_ex_state
*state
, int fd
)
273 struct addrinfo hints
;
274 struct addrinfo
*res
;
275 struct addrinfo
*res_list
= NULL
;
280 hints
.ai_socktype
= SOCK_STREAM
;
281 hints
.ai_family
= AF_INET
;/* TODO: add AF_INET6 support */
282 hints
.ai_flags
= AI_ADDRCONFIG
| AI_NUMERICSERV
;
284 ret
= getaddrinfo(state
->name
.name
, "0", &hints
, &res_list
);
285 /* try to fallback in case of error */
286 if (state
->do_fallback
) {
292 /* getaddrinfo() doesn't handle CNAME records */
293 run_child_dns_lookup(state
, fd
);
303 addrs
= talloc_strdup(state
, "");
308 for (res
= res_list
; res
; res
= res
->ai_next
) {
309 struct sockaddr_in
*in
;
311 if (res
->ai_family
!= AF_INET
) {
314 in
= (struct sockaddr_in
*)res
->ai_addr
;
316 addrs
= talloc_asprintf_append_buffer(addrs
, "%s%s:%u/%s",
318 inet_ntoa(in
->sin_addr
),
328 write(fd
, addrs
, talloc_get_size(addrs
));
332 freeaddrinfo(res_list
);
338 handle a read event on the pipe
340 static void pipe_handler(struct tevent_context
*ev
, struct tevent_fd
*fde
,
341 uint16_t flags
, void *private_data
)
343 struct composite_context
*c
= talloc_get_type(private_data
, struct composite_context
);
344 struct dns_ex_state
*state
= talloc_get_type(c
->private_data
,
345 struct dns_ex_state
);
347 uint32_t num_addrs
, i
;
353 /* if we get any event from the child then we know that we
354 won't need to kill it off */
355 talloc_set_destructor(state
, NULL
);
357 if (ioctl(state
->child_fd
, FIONREAD
, &value
) != 0) {
361 address
= talloc_array(state
, char, value
+1);
363 /* yes, we don't care about EAGAIN or other niceities
364 here. They just can't happen with this parent/child
365 relationship, and even if they did then giving an error is
366 the right thing to do */
367 ret
= read(state
->child_fd
, address
, value
);
371 if (waitpid(state
->child
, &status
, WNOHANG
) == 0) {
372 kill(state
->child
, SIGKILL
);
373 waitpid(state
->child
, &status
, 0);
377 DEBUG(3,("dns child failed to find name '%s' of type %s\n",
378 state
->name
.name
, (state
->flags
& RESOLVE_NAME_FLAG_DNS_SRV
)?"SRV":"A"));
379 composite_error(c
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
383 /* enusre the address looks good */
386 addrs
= str_list_make(state
, address
, ",");
387 if (composite_nomem(addrs
, c
)) return;
389 num_addrs
= str_list_length((const char * const *)addrs
);
391 state
->addrs
= talloc_array(state
, struct socket_address
*,
393 if (composite_nomem(state
->addrs
, c
)) return;
395 state
->names
= talloc_array(state
, char *, num_addrs
+1);
396 if (composite_nomem(state
->names
, c
)) return;
398 for (i
=0; i
< num_addrs
; i
++) {
400 char *p
= strrchr(addrs
[i
], ':');
404 composite_error(c
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
413 composite_error(c
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
420 if (strcmp(addrs
[i
], "0.0.0.0") == 0 ||
421 inet_addr(addrs
[i
]) == INADDR_NONE
) {
422 composite_error(c
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
425 port
= strtoul(p
, NULL
, 10);
426 if (port
> UINT16_MAX
) {
427 composite_error(c
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
430 state
->addrs
[i
] = socket_address_from_strings(state
->addrs
,
434 if (composite_nomem(state
->addrs
[i
], c
)) return;
436 state
->names
[i
] = talloc_strdup(state
->names
, n
);
437 if (composite_nomem(state
->names
[i
], c
)) return;
439 state
->addrs
[i
] = NULL
;
440 state
->names
[i
] = NULL
;
446 getaddrinfo() or dns_lookup() name resolution method - async send
448 struct composite_context
*resolve_name_dns_ex_send(TALLOC_CTX
*mem_ctx
,
449 struct tevent_context
*event_ctx
,
453 struct nbt_name
*name
,
456 struct composite_context
*c
;
457 struct dns_ex_state
*state
;
458 int fd
[2] = { -1, -1 };
461 c
= composite_create(mem_ctx
, event_ctx
);
462 if (c
== NULL
) return NULL
;
464 if (flags
& RESOLVE_NAME_FLAG_FORCE_NBT
) {
465 composite_error(c
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
469 state
= talloc_zero(c
, struct dns_ex_state
);
470 if (composite_nomem(state
, c
)) return c
;
471 c
->private_data
= state
;
473 c
->status
= nbt_name_dup(state
, name
, &state
->name
);
474 if (!composite_is_ok(c
)) return c
;
476 /* setup a pipe to chat to our child */
479 composite_error(c
, map_nt_error_from_unix(errno
));
483 state
->do_fallback
= do_fallback
;
484 state
->flags
= flags
;
487 state
->child_fd
= fd
[0];
488 state
->event_ctx
= c
->event_ctx
;
490 /* we need to put the child in our event context so
491 we know when the dns_lookup() has finished */
492 state
->fde
= event_add_fd(c
->event_ctx
, c
, state
->child_fd
, EVENT_FD_READ
,
494 if (composite_nomem(state
->fde
, c
)) {
499 tevent_fd_set_auto_close(state
->fde
);
501 state
->child
= fork();
502 if (state
->child
== (pid_t
)-1) {
503 composite_error(c
, map_nt_error_from_unix(errno
));
507 if (state
->child
== 0) {
509 if (state
->flags
& RESOLVE_NAME_FLAG_FORCE_DNS
) {
510 run_child_dns_lookup(state
, fd
[1]);
512 run_child_getaddrinfo(state
, fd
[1]);
518 /* cleanup wayward children */
519 talloc_set_destructor(state
, dns_ex_destructor
);
525 getaddrinfo() or dns_lookup() name resolution method - recv side
527 NTSTATUS
resolve_name_dns_ex_recv(struct composite_context
*c
,
529 struct socket_address
***addrs
,
534 status
= composite_wait(c
);
536 if (NT_STATUS_IS_OK(status
)) {
537 struct dns_ex_state
*state
= talloc_get_type(c
->private_data
,
538 struct dns_ex_state
);
539 *addrs
= talloc_steal(mem_ctx
, state
->addrs
);
541 *names
= talloc_steal(mem_ctx
, state
->names
);