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 if (strchr(state->name.name, '.') && state->name.name[strlen(state->name.name)-1] != '.') {
96 /* we are asking for a fully qualified name, but the
97 name doesn't end in a '.'. We need to prevent the
98 DNS library trying the search domains configured in
100 state->name.name = talloc_strdup_append(discard_const_p(char, state->name.name),
104 /* this is the blocking call we are going to lots of trouble
105 to avoid in the parent */
106 reply = rk_dns_lookup(state->name.name, do_srv?"SRV":"A");
112 rk_dns_srv_order(reply);
115 /* Loop over all returned records and pick the "srv" records */
116 for (rr=reply->head; rr; rr=rr->next) {
117 /* we are only interested in the IN class */
118 if (rr->class != rk_ns_c_in) {
123 /* we are only interested in SRV records */
124 if (rr->type != rk_ns_t_srv) {
128 /* verify we actually have a SRV record here */
133 /* Verify we got a port */
134 if (rr->u.srv->port == 0) {
138 /* we are only interested in A records */
139 /* TODO: add AAAA support */
140 if (rr->type != rk_ns_t_a) {
144 /* verify we actually have a A record here */
156 srv_rr = talloc_zero_array(state,
157 struct rk_resource_record *,
163 addrs_rr = talloc_zero_array(state,
164 struct rk_resource_record *,
170 /* Loop over all returned records and pick the records */
171 for (rr=reply->head;rr;rr=rr->next) {
172 /* we are only interested in the IN class */
173 if (rr->class != rk_ns_c_in) {
178 /* we are only interested in SRV records */
179 if (rr->type != rk_ns_t_srv) {
183 /* verify we actually have a srv record here */
188 /* Verify we got a port */
189 if (rr->u.srv->port == 0) {
193 srv_rr[srv_valid] = rr;
196 /* we are only interested in A records */
197 /* TODO: add AAAA support */
198 if (rr->type != rk_ns_t_a) {
202 /* verify we actually have a A record here */
207 addrs_rr[addrs_valid] = rr;
212 for (i=0; i < srv_valid; i++) {
213 for (rr=reply->head;rr;rr=rr->next) {
215 if (rr->class != rk_ns_c_in) {
219 /* we are only interested in A records */
220 if (rr->type != rk_ns_t_a) {
224 /* verify we actually have a srv record here */
225 if (strcmp(&srv_rr[i]->u.srv->target[0], rr->domain) != 0) {
235 if (addrs_valid == 0) {
239 addrs = talloc_strdup(state, "");
244 for (i=0; i < count; i++) {
251 (state->flags & RESOLVE_NAME_FLAG_OVERWRITE_PORT)) {
252 port = srv_rr[i]->u.srv->port;
257 addrs = talloc_asprintf_append_buffer(addrs, "%s%s:%u/%s",
259 inet_ntoa(*addrs_rr[i]->u.a),
261 addrs_rr[i]->domain);
269 write(fd, addrs, talloc_get_size(addrs));
279 static void run_child_getaddrinfo(struct dns_ex_state *state, int fd)
282 struct addrinfo hints;
283 struct addrinfo *res;
284 struct addrinfo *res_list = NULL;
289 hints.ai_socktype = SOCK_STREAM;
290 hints.ai_family = AF_INET;/* TODO: add AF_INET6 support */
291 hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
293 ret = getaddrinfo(state->name.name, "0", &hints, &res_list);
294 /* try to fallback in case of error */
295 if (state->do_fallback) {
301 /* getaddrinfo() doesn't handle CNAME records */
302 run_child_dns_lookup(state, fd);
312 addrs = talloc_strdup(state, "");
317 for (res = res_list; res; res = res->ai_next) {
318 struct sockaddr_in *in;
320 if (res->ai_family != AF_INET) {
323 in = (struct sockaddr_in *)res->ai_addr;
325 addrs = talloc_asprintf_append_buffer(addrs, "%s%s:%u/%s",
327 inet_ntoa(in->sin_addr),
337 write(fd, addrs, talloc_get_size(addrs));
341 freeaddrinfo(res_list);
347 handle a read event on the pipe
349 static void pipe_handler(struct tevent_context *ev, struct tevent_fd *fde,
350 uint16_t flags, void *private_data)
352 struct composite_context *c = talloc_get_type(private_data, struct composite_context);
353 struct dns_ex_state *state = talloc_get_type(c->private_data,
354 struct dns_ex_state);
356 uint32_t num_addrs, i;
362 /* if we get any event from the child then we know that we
363 won't need to kill it off */
364 talloc_set_destructor(state, NULL);
366 if (ioctl(state->child_fd, FIONREAD, &value) != 0) {
370 address = talloc_array(state, char, value+1);
372 /* yes, we don't care about EAGAIN or other niceities
373 here. They just can't happen with this parent/child
374 relationship, and even if they did then giving an error is
375 the right thing to do */
376 ret = read(state->child_fd, address, value);
380 if (waitpid(state->child, &status, WNOHANG) == 0) {
381 kill(state->child, SIGKILL);
382 waitpid(state->child, &status, 0);
386 DEBUG(3,("dns child failed to find name '%s' of type %s\n",
387 state->name.name, (state->flags & RESOLVE_NAME_FLAG_DNS_SRV)?"SRV":"A"));
388 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
392 /* enusre the address looks good */
395 addrs = str_list_make(state, address, ",");
396 if (composite_nomem(addrs, c)) return;
398 num_addrs = str_list_length((const char * const *)addrs);
400 state->addrs = talloc_array(state, struct socket_address *,
402 if (composite_nomem(state->addrs, c)) return;
404 state->names = talloc_array(state, char *, num_addrs+1);
405 if (composite_nomem(state->names, c)) return;
407 for (i=0; i < num_addrs; i++) {
409 char *p = strrchr(addrs[i], ':');
413 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
422 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
429 if (strcmp(addrs[i], "0.0.0.0") == 0 ||
430 inet_addr(addrs[i]) == INADDR_NONE) {
431 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
434 port = strtoul(p, NULL, 10);
435 if (port > UINT16_MAX) {
436 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
439 state->addrs[i] = socket_address_from_strings(state->addrs,
443 if (composite_nomem(state->addrs[i], c)) return;
445 state->names[i] = talloc_strdup(state->names, n);
446 if (composite_nomem(state->names[i], c)) return;
448 state->addrs[i] = NULL;
449 state->names[i] = NULL;
455 getaddrinfo() or dns_lookup() name resolution method - async send
457 struct composite_context *resolve_name_dns_ex_send(TALLOC_CTX *mem_ctx,
458 struct tevent_context *event_ctx,
462 struct nbt_name *name,
465 struct composite_context *c;
466 struct dns_ex_state *state;
467 int fd[2] = { -1, -1 };
470 c = composite_create(mem_ctx, event_ctx);
471 if (c == NULL) return NULL;
473 if (flags & RESOLVE_NAME_FLAG_FORCE_NBT) {
474 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
478 state = talloc_zero(c, struct dns_ex_state);
479 if (composite_nomem(state, c)) return c;
480 c->private_data = state;
482 c->status = nbt_name_dup(state, name, &state->name);
483 if (!composite_is_ok(c)) return c;
485 /* setup a pipe to chat to our child */
488 composite_error(c, map_nt_error_from_unix(errno));
492 state->do_fallback = do_fallback;
493 state->flags = flags;
496 state->child_fd = fd[0];
497 state->event_ctx = c->event_ctx;
499 /* we need to put the child in our event context so
500 we know when the dns_lookup() has finished */
501 state->fde = event_add_fd(c->event_ctx, c, state->child_fd, EVENT_FD_READ,
503 if (composite_nomem(state->fde, c)) {
508 tevent_fd_set_auto_close(state->fde);
510 state->child = fork();
511 if (state->child == (pid_t)-1) {
512 composite_error(c, map_nt_error_from_unix(errno));
516 if (state->child == 0) {
518 if (state->flags & RESOLVE_NAME_FLAG_FORCE_DNS) {
519 run_child_dns_lookup(state, fd[1]);
521 run_child_getaddrinfo(state, fd[1]);
527 /* cleanup wayward children */
528 talloc_set_destructor(state, dns_ex_destructor);
534 getaddrinfo() or dns_lookup() name resolution method - recv side
536 NTSTATUS resolve_name_dns_ex_recv(struct composite_context *c,
538 struct socket_address ***addrs,
543 status = composite_wait(c);
545 if (NT_STATUS_IS_OK(status)) {
546 struct dns_ex_state *state = talloc_get_type(c->private_data,
547 struct dns_ex_state);
548 *addrs = talloc_steal(mem_ctx, state->addrs);
550 *names = talloc_steal(mem_ctx, state->names);