summaryrefslogtreecommitdiffstats
path: root/libcli/dns/dns_lookup.c
blob: cb0ede80f98b1d1e22c8acf6700b89b254d9a922 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
 *  Unix SMB/CIFS implementation.
 *  Internal DNS query structures
 *  Copyright (C) Volker Lendecke 2018
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include "replace.h"
#include "libcli/dns/dns_lookup.h"
#include "libcli/dns/resolvconf.h"
#include "libcli/dns/libdns.h"
#include "lib/util/tevent_unix.h"
#include "lib/util/samba_util.h"
#include "lib/util/debug.h"

struct dns_lookup_state {
	struct tevent_context *ev;
	const char *name;
	enum dns_qclass qclass;
	enum dns_qtype qtype;

	char **nameservers;
	size_t num_nameservers;
	size_t num_sent;

	struct tevent_req **dns_subreqs;
	struct tevent_req *wait_subreq;

	struct dns_name_packet *reply;
};

static int dns_lookup_send_next(struct tevent_req *req);

static void dns_lookup_done(struct tevent_req *subreq);
static void dns_lookup_waited(struct tevent_req *subreq);

struct tevent_req *dns_lookup_send(TALLOC_CTX *mem_ctx,
				   struct tevent_context *ev,
				   FILE *resolv_conf_fp,
				   const char *name,
				   enum dns_qclass qclass,
				   enum dns_qtype qtype)
{
	struct tevent_req *req;
	struct dns_lookup_state *state;
	FILE *fp = resolv_conf_fp;
	int ret;

	req = tevent_req_create(mem_ctx, &state, struct dns_lookup_state);
	if (req == NULL) {
		return NULL;
	}
	state->ev = ev;
	state->name = name;
	state->qclass = qclass;
	state->qtype = qtype;

	if (resolv_conf_fp == NULL) {
		const char *resolvconf = "/etc/resolv.conf";

#ifdef ENABLE_SELFTEST
		{
			const char *envvar = getenv("RESOLV_CONF");
			if (envvar != NULL) {
				resolvconf = envvar;
			}
		}
#endif

		fp = fopen(resolvconf, "r");
		if (fp == NULL) {
			tevent_req_error(req, errno);
			return tevent_req_post(req, ev);
		}
	}

	ret = parse_resolvconf_fp(
		fp,
		state,
		&state->nameservers,
		&state->num_nameservers);

	if (resolv_conf_fp == NULL) {
		fclose(fp);
	}

	if (ret != 0) {
		tevent_req_error(req, ret);
		return tevent_req_post(req, ev);
	}

	if (state->num_nameservers == 0) {
		/*
		 * glibc's getaddrinfo returns EAI_AGAIN when no
		 * nameservers are configured. EAGAIN seems closest.
		 */
		tevent_req_error(req, EAGAIN);
		return tevent_req_post(req, ev);
	}

	state->dns_subreqs = talloc_zero_array(
		state,
		struct tevent_req *,
		state->num_nameservers);

	if (tevent_req_nomem(state->dns_subreqs, req)) {
		return tevent_req_post(req, ev);
	}

	ret = dns_lookup_send_next(req);
	if (tevent_req_error(req, ret)) {
		return tevent_req_post(req, ev);
	}

	return req;
}

static int dns_lookup_send_next(struct tevent_req *req)
{
	struct dns_lookup_state *state = tevent_req_data(
		req, struct dns_lookup_state);

	DBG_DEBUG("Sending DNS request #%zu to %s\n",
		  state->num_sent,
		  state->nameservers[state->num_sent]);

	state->dns_subreqs[state->num_sent] = dns_cli_request_send(
		state->dns_subreqs,
		state->ev,
		state->nameservers[state->num_sent],
		state->name,
		state->qclass,
		state->qtype);

	if (state->dns_subreqs[state->num_sent] == NULL) {
		return ENOMEM;
	}
	tevent_req_set_callback(state->dns_subreqs[state->num_sent],
				dns_lookup_done,
				req);
	state->num_sent += 1;

	if (state->num_sent == state->num_nameservers) {
		/*
		 * No more nameservers left
		 */
		DBG_DEBUG("cancelling wait_subreq\n");
		TALLOC_FREE(state->wait_subreq);
		return 0;
	}

	if (state->wait_subreq != NULL) {
		/*
		 * This can happen if we fire the next request upon
		 * dns_cli_request returning a network-level error
		 */
		return 0;
	}

	state->wait_subreq = tevent_wakeup_send(
		state,
		state->ev,
		tevent_timeval_current_ofs(1, 0));
	if (state->wait_subreq == NULL) {
		return ENOMEM;
	}
	tevent_req_set_callback(state->wait_subreq, dns_lookup_waited, req);

	return 0;
}

static void dns_lookup_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct dns_lookup_state *state = tevent_req_data(
		req, struct dns_lookup_state);
	int dns_cli_request_ret;
	size_t i;

	dns_cli_request_ret = dns_cli_request_recv(
		subreq,
		state,
		&state->reply);

	for (i = 0; i < state->num_nameservers; i++) {
		if (state->dns_subreqs[i] == subreq) {
			break;
		}
	}

	TALLOC_FREE(subreq);

	if (i == state->num_nameservers) {
		/* should never happen */
		DBG_WARNING("Failed to find subreq");
		tevent_req_error(req, EINVAL);
		return;
	}
	state->dns_subreqs[i] = NULL;

	if (dns_cli_request_ret == 0) {
		/*
		 * Success, cancel everything else
		 */
		TALLOC_FREE(state->dns_subreqs);
		TALLOC_FREE(state->wait_subreq);
		tevent_req_done(req);
		return;
	}

	DBG_DEBUG("dns_cli_request[%zu] returned %s\n", i,
		  strerror(dns_cli_request_ret));

	if (state->num_sent < state->num_nameservers) {
		/*
		 * We have a nameserver left to try
		 */
		int ret;

		ret = dns_lookup_send_next(req);
		if (tevent_req_error(req, ret)) {
			return;
		}
	}

	DBG_DEBUG("looking for outstanding requests\n");

	for (i = 0; i<state->num_nameservers; i++) {
		if (state->dns_subreqs[i] != NULL) {
			break;
		}
	}

	DBG_DEBUG("i=%zu, num_nameservers=%zu\n",
		  i, state->num_nameservers);

	if (i == state->num_nameservers) {
		/*
		 * Report the lower-level error if we have nothing
		 * outstanding anymore
		 */
		tevent_req_error(req, dns_cli_request_ret);
		return;
	}

	/*
	 * Do nothing: We have other nameservers that might come back
	 * with something good.
	 */
}

static void dns_lookup_waited(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct dns_lookup_state *state = tevent_req_data(
		req, struct dns_lookup_state);
	int ret;
	bool ok;

	DBG_DEBUG("waited\n");

	ok = tevent_wakeup_recv(subreq);
	TALLOC_FREE(subreq);
	if (!ok) {
		tevent_req_oom(req);
		return;
	}
	state->wait_subreq = NULL;

	ret = dns_lookup_send_next(req);
	if (tevent_req_error(req, ret)) {
		return;
	}

	/*
	 * dns_lookup_send_next() has already triggered the next wakeup
	 */
}

int dns_lookup_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
		    struct dns_name_packet **reply)
{
	struct dns_lookup_state *state = tevent_req_data(
		req, struct dns_lookup_state);
	int err;

	if (tevent_req_is_unix_error(req, &err)) {
		return err;
	}

	*reply = talloc_move(mem_ctx, &state->reply);

	tevent_req_received(req);
	return 0;
}

int dns_lookup(FILE *resolv_conf_fp,
	       const char *name,
	       enum dns_qclass qclass,
	       enum dns_qtype qtype,
	       TALLOC_CTX *mem_ctx,
	       struct dns_name_packet **reply)
{
	struct tevent_context *ev;
	struct tevent_req *req;
	int ret = ENOMEM;

	ev = samba_tevent_context_init(mem_ctx);
	if (ev == NULL) {
		goto fail;
	}
	req = dns_lookup_send(ev, ev, resolv_conf_fp, name, qclass, qtype);
	if (req == NULL) {
		goto fail;
	}
	if (!tevent_req_poll_unix(req, ev, &ret)) {
		goto fail;
	}
	ret = dns_lookup_recv(req, mem_ctx, reply);
fail:
	TALLOC_FREE(ev);
	return ret;
}

bool dns_res_rec_get_sockaddr(const struct dns_res_rec *rec,
			      struct sockaddr_storage *addr)
{
	sa_family_t family;
	const char *src;
	void *dst;
	int ret;

	switch (rec->rr_type) {
	    case DNS_QTYPE_A:
		    family = AF_INET;
		    src = rec->rdata.ipv4_record;
		    dst = &(((struct sockaddr_in *)addr)->sin_addr);
		    break;
#ifdef HAVE_IPV6
	    case DNS_QTYPE_AAAA:
		    family = AF_INET6;
		    src = rec->rdata.ipv6_record;
		    dst = &(((struct sockaddr_in6 *)addr)->sin6_addr);
		    break;
#endif
	    default:
		    /* We only care about IP addresses */
		    return false;
	}

	*addr = (struct sockaddr_storage) { .ss_family = family };

	ret = inet_pton(family, src, dst);
	if (ret != 1) {
		DBG_DEBUG("inet_pton(%s) failed\n", src);
		return false;
	}

	return true;
}