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 "heimdal/lib/roken/resolve.h"
47 struct socket_address **addrs;
52 struct event_context *event_ctx;
56 kill off a wayward child if needed. This allows us to stop an async
57 name resolution without leaving a potentially blocking call running
60 static int dns_ex_destructor(struct dns_ex_state *state)
64 kill(state->child, SIGTERM);
65 close(state->child_fd);
66 if (waitpid(state->child, &status, WNOHANG) == 0) {
67 kill(state->child, SIGKILL);
68 waitpid(state->child, &status, 0);
77 static void run_child_dns_lookup(struct dns_ex_state *state, int fd)
79 struct dns_reply *reply;
80 struct resource_record *rr;
82 uint32_t srv_valid = 0;
83 struct resource_record **srv_rr;
84 uint32_t addrs_valid = 0;
85 struct resource_record **addrs_rr;
89 bool do_srv = (state->flags & RESOLVE_NAME_FLAG_DNS_SRV);
91 /* this is the blocking call we are going to lots of trouble
92 to avoid in the parent */
93 reply = dns_lookup(state->name.name, do_srv?"SRV":"A");
102 /* Loop over all returned records and pick the "srv" records */
103 for (rr=reply->head; rr; rr=rr->next) {
104 /* we are only interested in the IN class */
105 if (rr->class != C_IN) {
110 /* we are only interested in SRV records */
111 if (rr->type != T_SRV) {
115 /* verify we actually have a SRV record here */
120 /* Verify we got a port */
121 if (rr->u.srv->port == 0) {
125 /* we are only interested in A records */
126 /* TODO: add AAAA support */
127 if (rr->type != T_A) {
131 /* verify we actually have a A record here */
143 srv_rr = talloc_zero_array(state,
144 struct resource_record *,
150 addrs_rr = talloc_zero_array(state,
151 struct resource_record *,
157 /* Loop over all returned records and pick the records */
158 for (rr=reply->head;rr;rr=rr->next) {
159 /* we are only interested in the IN class */
160 if (rr->class != C_IN) {
165 /* we are only interested in SRV records */
166 if (rr->type != T_SRV) {
170 /* verify we actually have a srv record here */
175 /* Verify we got a port */
176 if (rr->u.srv->port == 0) {
180 srv_rr[srv_valid] = rr;
183 /* we are only interested in A records */
184 /* TODO: add AAAA support */
185 if (rr->type != T_A) {
189 /* verify we actually have a A record here */
194 addrs_rr[addrs_valid] = rr;
199 for (i=0; i < srv_valid; i++) {
200 for (rr=reply->head;rr;rr=rr->next) {
202 if (rr->class != C_IN) {
206 /* we are only interested in SRV records */
207 if (rr->type != T_A) {
211 /* verify we actually have a srv record here */
212 if (strcmp(&srv_rr[i]->u.srv->target[0], rr->domain) != 0) {
222 if (addrs_valid == 0) {
226 addrs = talloc_strdup(state, "");
231 for (i=0; i < count; i++) {
238 (state->flags & RESOLVE_NAME_FLAG_OVERWRITE_PORT)) {
239 port = srv_rr[i]->u.srv->port;
244 addrs = talloc_asprintf_append_buffer(addrs, "%s%s:%u/%s",
246 inet_ntoa(*addrs_rr[i]->u.a),
248 addrs_rr[i]->domain);
256 write(fd, addrs, talloc_get_size(addrs));
266 static void run_child_getaddrinfo(struct dns_ex_state *state, int fd)
269 struct addrinfo hints;
270 struct addrinfo *res;
271 struct addrinfo *res_list = NULL;
276 hints.ai_socktype = SOCK_STREAM;
277 hints.ai_family = AF_INET;/* TODO: add AF_INET6 support */
278 hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
280 ret = getaddrinfo(state->name.name, "0", &hints, &res_list);
281 #if defined(EAI_NODATA) && (EAI_NODATA != EAI_NONAME)
282 if (ret == EAI_NODATA && state->do_fallback) {
284 if (ret == EAI_NONAME && state->do_fallback) {
286 /* getaddrinfo() doesn't handle CNAME records */
287 run_child_dns_lookup(state, fd);
294 addrs = talloc_strdup(state, "");
299 for (res = res_list; res; res = res->ai_next) {
300 struct sockaddr_in *in;
302 if (res->ai_family != AF_INET) {
305 in = (struct sockaddr_in *)res->ai_addr;
307 addrs = talloc_asprintf_append_buffer(addrs, "%s%s:%u/%s",
309 inet_ntoa(in->sin_addr),
319 write(fd, addrs, talloc_get_size(addrs));
323 freeaddrinfo(res_list);
329 handle a read event on the pipe
331 static void pipe_handler(struct event_context *ev, struct fd_event *fde,
332 uint16_t flags, void *private_data)
334 struct composite_context *c = talloc_get_type(private_data, struct composite_context);
335 struct dns_ex_state *state = talloc_get_type(c->private_data,
336 struct dns_ex_state);
338 uint32_t num_addrs, i;
344 /* if we get any event from the child then we know that we
345 won't need to kill it off */
346 talloc_set_destructor(state, NULL);
348 if (ioctl(state->child_fd, FIONREAD, &value) != 0) {
352 address = talloc_array(state, char, value+1);
354 /* yes, we don't care about EAGAIN or other niceities
355 here. They just can't happen with this parent/child
356 relationship, and even if they did then giving an error is
357 the right thing to do */
358 ret = read(state->child_fd, address, value);
362 close(state->child_fd);
363 if (waitpid(state->child, &status, WNOHANG) == 0) {
364 kill(state->child, SIGKILL);
365 waitpid(state->child, &status, 0);
369 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
373 /* enusre the address looks good */
376 addrs = str_list_make(state, address, ",");
377 if (composite_nomem(addrs, c)) return;
379 num_addrs = str_list_length((const char * const *)addrs);
381 state->addrs = talloc_array(state, struct socket_address *,
383 if (composite_nomem(state->addrs, c)) return;
385 state->names = talloc_array(state, char *, num_addrs+1);
386 if (composite_nomem(state->names, c)) return;
388 for (i=0; i < num_addrs; i++) {
390 char *p = strrchr(addrs[i], ':');
394 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
403 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
410 if (strcmp(addrs[i], "0.0.0.0") == 0 ||
411 inet_addr(addrs[i]) == INADDR_NONE) {
412 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
415 port = strtoul(p, NULL, 10);
416 if (port > UINT16_MAX) {
417 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
420 state->addrs[i] = socket_address_from_strings(state->addrs,
424 if (composite_nomem(state->addrs[i], c)) return;
426 state->names[i] = talloc_strdup(state->names, n);
427 if (composite_nomem(state->names[i], c)) return;
429 state->addrs[i] = NULL;
430 state->names[i] = NULL;
436 getaddrinfo() or dns_lookup() name resolution method - async send
438 struct composite_context *resolve_name_dns_ex_send(TALLOC_CTX *mem_ctx,
439 struct event_context *event_ctx,
443 struct nbt_name *name,
446 struct composite_context *c;
447 struct dns_ex_state *state;
448 int fd[2] = { -1, -1 };
451 c = composite_create(mem_ctx, event_ctx);
452 if (c == NULL) return NULL;
454 if (flags & RESOLVE_NAME_FLAG_FORCE_NBT) {
455 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
459 state = talloc_zero(c, struct dns_ex_state);
460 if (composite_nomem(state, c)) return c;
461 c->private_data = state;
463 c->status = nbt_name_dup(state, name, &state->name);
464 if (!composite_is_ok(c)) return c;
466 /* setup a pipe to chat to our child */
469 composite_error(c, map_nt_error_from_unix(errno));
473 state->do_fallback = do_fallback;
474 state->flags = flags;
477 state->child_fd = fd[0];
478 state->event_ctx = c->event_ctx;
480 /* we need to put the child in our event context so
481 we know when the dns_lookup() has finished */
482 state->fde = event_add_fd(c->event_ctx, c, state->child_fd, EVENT_FD_READ,
484 if (composite_nomem(state->fde, c)) {
490 state->child = fork();
491 if (state->child == (pid_t)-1) {
492 composite_error(c, map_nt_error_from_unix(errno));
496 if (state->child == 0) {
498 if (state->flags & RESOLVE_NAME_FLAG_FORCE_DNS) {
499 run_child_dns_lookup(state, fd[1]);
501 run_child_getaddrinfo(state, fd[1]);
507 /* cleanup wayward children */
508 talloc_set_destructor(state, dns_ex_destructor);
514 getaddrinfo() or dns_lookup() name resolution method - recv side
516 NTSTATUS resolve_name_dns_ex_recv(struct composite_context *c,
518 struct socket_address ***addrs,
523 status = composite_wait(c);
525 if (NT_STATUS_IS_OK(status)) {
526 struct dns_ex_state *state = talloc_get_type(c->private_data,
527 struct dns_ex_state);
528 *addrs = talloc_steal(mem_ctx, state->addrs);
530 *names = talloc_steal(mem_ctx, state->names);