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;
90 struct rk_dns_reply **srv_replies = NULL;
94 bool do_srv = (state->flags & RESOLVE_NAME_FLAG_DNS_SRV);
96 if (strchr(state->name.name, '.') && state->name.name[strlen(state->name.name)-1] != '.') {
97 /* we are asking for a fully qualified name, but the
98 name doesn't end in a '.'. We need to prevent the
99 DNS library trying the search domains configured in
101 state->name.name = talloc_strdup_append(discard_const_p(char, state->name.name),
105 /* this is the blocking call we are going to lots of trouble
106 to avoid in the parent */
107 reply = rk_dns_lookup(state->name.name, do_srv?"SRV":"A");
113 rk_dns_srv_order(reply);
116 /* Loop over all returned records and pick the "srv" records */
117 for (rr=reply->head; rr; rr=rr->next) {
118 /* we are only interested in the IN class */
119 if (rr->class != rk_ns_c_in) {
124 /* we are only interested in SRV records */
125 if (rr->type != rk_ns_t_srv) {
129 /* verify we actually have a SRV record here */
134 /* Verify we got a port */
135 if (rr->u.srv->port == 0) {
139 /* we are only interested in A records */
140 /* TODO: add AAAA support */
141 if (rr->type != rk_ns_t_a) {
145 /* verify we actually have a 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 records */
205 /* TODO: add AAAA support */
206 if (rr->type != rk_ns_t_a) {
210 /* verify we actually have a A record here */
215 addrs_rr[addrs_valid] = rr;
220 for (i=0; i < srv_valid; i++) {
221 srv_replies[i] = rk_dns_lookup(srv_rr[i]->u.srv->target, "A");
222 if (srv_replies[i] == NULL)
225 /* Add first A record to addrs_rr */
226 for (rr=srv_replies[i]->head;rr;rr=rr->next) {
227 if (rr->class != rk_ns_c_in) {
231 /* we are only interested in A records */
232 if (rr->type != rk_ns_t_a) {
236 /* verify we actually have a A record here */
247 if (addrs_valid == 0) {
251 addrs = talloc_strdup(state, "");
256 for (i=0; i < addrs_valid; i++) {
263 (state->flags & RESOLVE_NAME_FLAG_OVERWRITE_PORT)) {
264 port = srv_rr[i]->u.srv->port;
269 addrs = talloc_asprintf_append_buffer(addrs, "%s%s:%u/%s",
271 inet_ntoa(*addrs_rr[i]->u.a),
273 addrs_rr[i]->domain);
281 write(fd, addrs, talloc_get_size(addrs));
286 rk_dns_free_data(reply);
287 for (i=0; i < srv_valid; i++) {
288 if (srv_replies[i] != NULL)
289 rk_dns_free_data(srv_replies[i]);
297 static void run_child_getaddrinfo(struct dns_ex_state *state, int fd)
300 struct addrinfo hints;
301 struct addrinfo *res;
302 struct addrinfo *res_list = NULL;
307 hints.ai_socktype = SOCK_STREAM;
308 hints.ai_family = AF_INET;/* TODO: add AF_INET6 support */
309 hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
311 ret = getaddrinfo(state->name.name, "0", &hints, &res_list);
312 /* try to fallback in case of error */
313 if (state->do_fallback) {
319 /* getaddrinfo() doesn't handle CNAME records */
320 run_child_dns_lookup(state, fd);
330 addrs = talloc_strdup(state, "");
335 for (res = res_list; res; res = res->ai_next) {
336 struct sockaddr_in *in;
338 if (res->ai_family != AF_INET) {
341 in = (struct sockaddr_in *)res->ai_addr;
343 addrs = talloc_asprintf_append_buffer(addrs, "%s%s:%u/%s",
345 inet_ntoa(in->sin_addr),
355 write(fd, addrs, talloc_get_size(addrs));
359 freeaddrinfo(res_list);
365 handle a read event on the pipe
367 static void pipe_handler(struct tevent_context *ev, struct tevent_fd *fde,
368 uint16_t flags, void *private_data)
370 struct composite_context *c = talloc_get_type(private_data, struct composite_context);
371 struct dns_ex_state *state = talloc_get_type(c->private_data,
372 struct dns_ex_state);
374 uint32_t num_addrs, i;
380 /* if we get any event from the child then we know that we
381 won't need to kill it off */
382 talloc_set_destructor(state, NULL);
384 if (ioctl(state->child_fd, FIONREAD, &value) != 0) {
388 address = talloc_array(state, char, value+1);
390 /* yes, we don't care about EAGAIN or other niceities
391 here. They just can't happen with this parent/child
392 relationship, and even if they did then giving an error is
393 the right thing to do */
394 ret = read(state->child_fd, address, value);
398 if (waitpid(state->child, &status, WNOHANG) == 0) {
399 kill(state->child, SIGKILL);
400 waitpid(state->child, &status, 0);
404 DEBUG(3,("dns child failed to find name '%s' of type %s\n",
405 state->name.name, (state->flags & RESOLVE_NAME_FLAG_DNS_SRV)?"SRV":"A"));
406 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
410 /* enusre the address looks good */
413 addrs = str_list_make(state, address, ",");
414 if (composite_nomem(addrs, c)) return;
416 num_addrs = str_list_length((const char * const *)addrs);
418 state->addrs = talloc_array(state, struct socket_address *,
420 if (composite_nomem(state->addrs, c)) return;
422 state->names = talloc_array(state, char *, num_addrs+1);
423 if (composite_nomem(state->names, c)) return;
425 for (i=0; i < num_addrs; i++) {
427 char *p = strrchr(addrs[i], ':');
431 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
440 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
447 if (strcmp(addrs[i], "0.0.0.0") == 0 ||
448 inet_addr(addrs[i]) == INADDR_NONE) {
449 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
452 port = strtoul(p, NULL, 10);
453 if (port > UINT16_MAX) {
454 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
457 state->addrs[i] = socket_address_from_strings(state->addrs,
461 if (composite_nomem(state->addrs[i], c)) return;
463 state->names[i] = talloc_strdup(state->names, n);
464 if (composite_nomem(state->names[i], c)) return;
466 state->addrs[i] = NULL;
467 state->names[i] = NULL;
473 getaddrinfo() or dns_lookup() name resolution method - async send
475 struct composite_context *resolve_name_dns_ex_send(TALLOC_CTX *mem_ctx,
476 struct tevent_context *event_ctx,
480 struct nbt_name *name,
483 struct composite_context *c;
484 struct dns_ex_state *state;
485 int fd[2] = { -1, -1 };
488 c = composite_create(mem_ctx, event_ctx);
489 if (c == NULL) return NULL;
491 if (flags & RESOLVE_NAME_FLAG_FORCE_NBT) {
492 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
496 state = talloc_zero(c, struct dns_ex_state);
497 if (composite_nomem(state, c)) return c;
498 c->private_data = state;
500 c->status = nbt_name_dup(state, name, &state->name);
501 if (!composite_is_ok(c)) return c;
503 /* setup a pipe to chat to our child */
506 composite_error(c, map_nt_error_from_unix(errno));
510 state->do_fallback = do_fallback;
511 state->flags = flags;
514 state->child_fd = fd[0];
515 state->event_ctx = c->event_ctx;
517 /* we need to put the child in our event context so
518 we know when the dns_lookup() has finished */
519 state->fde = event_add_fd(c->event_ctx, c, state->child_fd, EVENT_FD_READ,
521 if (composite_nomem(state->fde, c)) {
526 tevent_fd_set_auto_close(state->fde);
528 state->child = fork();
529 if (state->child == (pid_t)-1) {
530 composite_error(c, map_nt_error_from_unix(errno));
534 if (state->child == 0) {
536 if (state->flags & RESOLVE_NAME_FLAG_FORCE_DNS) {
537 run_child_dns_lookup(state, fd[1]);
539 run_child_getaddrinfo(state, fd[1]);
545 /* cleanup wayward children */
546 talloc_set_destructor(state, dns_ex_destructor);
552 getaddrinfo() or dns_lookup() name resolution method - recv side
554 NTSTATUS resolve_name_dns_ex_recv(struct composite_context *c,
556 struct socket_address ***addrs,
561 status = composite_wait(c);
563 if (NT_STATUS_IS_OK(status)) {
564 struct dns_ex_state *state = talloc_get_type(c->private_data,
565 struct dns_ex_state);
566 *addrs = talloc_steal(mem_ctx, state->addrs);
568 *names = talloc_steal(mem_ctx, state->names);