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 (ret == EAI_NODATA && state->do_fallback) {
282 /* getaddrinfo() doesn't handle CNAME records */
283 run_child_dns_lookup(state, fd);
290 addrs = talloc_strdup(state, "");
295 for (res = res_list; res; res = res->ai_next) {
296 struct sockaddr_in *in;
298 if (res->ai_family != AF_INET) {
301 in = (struct sockaddr_in *)res->ai_addr;
303 addrs = talloc_asprintf_append_buffer(addrs, "%s%s:%u/%s",
305 inet_ntoa(in->sin_addr),
315 write(fd, addrs, talloc_get_size(addrs));
319 freeaddrinfo(res_list);
325 handle a read event on the pipe
327 static void pipe_handler(struct event_context *ev, struct fd_event *fde,
328 uint16_t flags, void *private_data)
330 struct composite_context *c = talloc_get_type(private_data, struct composite_context);
331 struct dns_ex_state *state = talloc_get_type(c->private_data,
332 struct dns_ex_state);
334 uint32_t num_addrs, i;
340 /* if we get any event from the child then we know that we
341 won't need to kill it off */
342 talloc_set_destructor(state, NULL);
344 if (ioctl(state->child_fd, FIONREAD, &value) != 0) {
348 address = talloc_array(state, char, value+1);
350 /* yes, we don't care about EAGAIN or other niceities
351 here. They just can't happen with this parent/child
352 relationship, and even if they did then giving an error is
353 the right thing to do */
354 ret = read(state->child_fd, address, value);
358 close(state->child_fd);
359 if (waitpid(state->child, &status, WNOHANG) == 0) {
360 kill(state->child, SIGKILL);
361 waitpid(state->child, &status, 0);
365 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
369 /* enusre the address looks good */
372 addrs = str_list_make(state, address, ",");
373 if (composite_nomem(addrs, c)) return;
375 num_addrs = str_list_length((const char * const *)addrs);
377 state->addrs = talloc_array(state, struct socket_address *,
379 if (composite_nomem(state->addrs, c)) return;
381 state->names = talloc_array(state, char *, num_addrs+1);
382 if (composite_nomem(state->names, c)) return;
384 for (i=0; i < num_addrs; i++) {
386 char *p = strrchr(addrs[i], ':');
390 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
399 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
406 if (strcmp(addrs[i], "0.0.0.0") == 0 ||
407 inet_addr(addrs[i]) == INADDR_NONE) {
408 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
411 port = strtoul(p, NULL, 10);
412 if (port > UINT16_MAX) {
413 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
416 state->addrs[i] = socket_address_from_strings(state->addrs,
420 if (composite_nomem(state->addrs[i], c)) return;
422 state->names[i] = talloc_strdup(state->names, n);
423 if (composite_nomem(state->names[i], c)) return;
425 state->addrs[i] = NULL;
426 state->names[i] = NULL;
432 getaddrinfo() or dns_lookup() name resolution method - async send
434 struct composite_context *resolve_name_dns_ex_send(TALLOC_CTX *mem_ctx,
435 struct event_context *event_ctx,
439 struct nbt_name *name,
442 struct composite_context *c;
443 struct dns_ex_state *state;
444 int fd[2] = { -1, -1 };
447 c = composite_create(mem_ctx, event_ctx);
448 if (c == NULL) return NULL;
450 if (flags & RESOLVE_NAME_FLAG_FORCE_NBT) {
451 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
455 state = talloc_zero(c, struct dns_ex_state);
456 if (composite_nomem(state, c)) return c;
457 c->private_data = state;
459 c->status = nbt_name_dup(state, name, &state->name);
460 if (!composite_is_ok(c)) return c;
462 /* setup a pipe to chat to our child */
465 composite_error(c, map_nt_error_from_unix(errno));
469 state->do_fallback = do_fallback;
470 state->flags = flags;
473 state->child_fd = fd[0];
474 state->event_ctx = c->event_ctx;
476 /* we need to put the child in our event context so
477 we know when the dns_lookup() has finished */
478 state->fde = event_add_fd(c->event_ctx, c, state->child_fd, EVENT_FD_READ,
480 if (composite_nomem(state->fde, c)) {
486 state->child = fork();
487 if (state->child == (pid_t)-1) {
488 composite_error(c, map_nt_error_from_unix(errno));
492 if (state->child == 0) {
494 if (state->flags & RESOLVE_NAME_FLAG_FORCE_DNS) {
495 run_child_dns_lookup(state, fd[1]);
497 run_child_getaddrinfo(state, fd[1]);
503 /* cleanup wayward children */
504 talloc_set_destructor(state, dns_ex_destructor);
510 getaddrinfo() or dns_lookup() name resolution method - recv side
512 NTSTATUS resolve_name_dns_ex_recv(struct composite_context *c,
514 struct socket_address ***addrs,
519 status = composite_wait(c);
521 if (NT_STATUS_IS_OK(status)) {
522 struct dns_ex_state *state = talloc_get_type(c->private_data,
523 struct dns_ex_state);
524 *addrs = talloc_steal(mem_ctx, state->addrs);
526 *names = talloc_steal(mem_ctx, state->names);