samba-tool: Add a gpo command for removing VGP Startup Scripts Group Policy
[samba.git] / libcli / dns / dns_lookup.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  Internal DNS query structures
4  *  Copyright (C) Volker Lendecke 2018
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "replace.h"
21 #include "libcli/dns/dns_lookup.h"
22 #include "libcli/dns/resolvconf.h"
23 #include "libcli/dns/libdns.h"
24 #include "lib/util/tevent_unix.h"
25 #include "lib/util/samba_util.h"
26 #include "lib/util/debug.h"
27
28 struct dns_lookup_state {
29         struct tevent_context *ev;
30         const char *name;
31         enum dns_qclass qclass;
32         enum dns_qtype qtype;
33
34         char **nameservers;
35         size_t num_nameservers;
36         size_t num_sent;
37
38         struct tevent_req **dns_subreqs;
39         struct tevent_req *wait_subreq;
40
41         struct dns_name_packet *reply;
42 };
43
44 static int dns_lookup_send_next(struct tevent_req *req);
45
46 static void dns_lookup_done(struct tevent_req *subreq);
47 static void dns_lookup_waited(struct tevent_req *subreq);
48
49 struct tevent_req *dns_lookup_send(TALLOC_CTX *mem_ctx,
50                                    struct tevent_context *ev,
51                                    FILE *resolv_conf_fp,
52                                    const char *name,
53                                    enum dns_qclass qclass,
54                                    enum dns_qtype qtype)
55 {
56         struct tevent_req *req;
57         struct dns_lookup_state *state;
58         FILE *fp = resolv_conf_fp;
59         int ret;
60
61         req = tevent_req_create(mem_ctx, &state, struct dns_lookup_state);
62         if (req == NULL) {
63                 return NULL;
64         }
65         state->ev = ev;
66         state->name = name;
67         state->qclass = qclass;
68         state->qtype = qtype;
69
70         if (resolv_conf_fp == NULL) {
71                 const char *resolvconf = "/etc/resolv.conf";
72
73 #ifdef ENABLE_SELFTEST
74                 {
75                         const char *envvar = getenv("RESOLV_CONF");
76                         if (envvar != NULL) {
77                                 resolvconf = envvar;
78                         }
79                 }
80 #endif
81
82                 fp = fopen(resolvconf, "r");
83                 if (fp == NULL) {
84                         tevent_req_error(req, errno);
85                         return tevent_req_post(req, ev);
86                 }
87         }
88
89         ret = parse_resolvconf_fp(
90                 fp,
91                 state,
92                 &state->nameservers,
93                 &state->num_nameservers);
94
95         if (resolv_conf_fp == NULL) {
96                 fclose(fp);
97         }
98
99         if (ret != 0) {
100                 tevent_req_error(req, ret);
101                 return tevent_req_post(req, ev);
102         }
103
104         if (state->num_nameservers == 0) {
105                 /*
106                  * glibc's getaddrinfo returns EAI_AGAIN when no
107                  * nameservers are configured. EAGAIN seems closest.
108                  */
109                 tevent_req_error(req, EAGAIN);
110                 return tevent_req_post(req, ev);
111         }
112
113         state->dns_subreqs = talloc_zero_array(
114                 state,
115                 struct tevent_req *,
116                 state->num_nameservers);
117
118         if (tevent_req_nomem(state->dns_subreqs, req)) {
119                 return tevent_req_post(req, ev);
120         }
121
122         ret = dns_lookup_send_next(req);
123         if (tevent_req_error(req, ret)) {
124                 return tevent_req_post(req, ev);
125         }
126
127         return req;
128 }
129
130 static int dns_lookup_send_next(struct tevent_req *req)
131 {
132         struct dns_lookup_state *state = tevent_req_data(
133                 req, struct dns_lookup_state);
134
135         DBG_DEBUG("Sending DNS request #%zu to %s\n",
136                   state->num_sent,
137                   state->nameservers[state->num_sent]);
138
139         state->dns_subreqs[state->num_sent] = dns_cli_request_send(
140                 state->dns_subreqs,
141                 state->ev,
142                 state->nameservers[state->num_sent],
143                 state->name,
144                 state->qclass,
145                 state->qtype);
146
147         if (state->dns_subreqs[state->num_sent] == NULL) {
148                 return ENOMEM;
149         }
150         tevent_req_set_callback(state->dns_subreqs[state->num_sent],
151                                 dns_lookup_done,
152                                 req);
153         state->num_sent += 1;
154
155         if (state->num_sent == state->num_nameservers) {
156                 /*
157                  * No more nameservers left
158                  */
159                 DBG_DEBUG("cancelling wait_subreq\n");
160                 TALLOC_FREE(state->wait_subreq);
161                 return 0;
162         }
163
164         if (state->wait_subreq != NULL) {
165                 /*
166                  * This can happen if we fire the next request upon
167                  * dns_cli_request returning a network-level error
168                  */
169                 return 0;
170         }
171
172         state->wait_subreq = tevent_wakeup_send(
173                 state,
174                 state->ev,
175                 tevent_timeval_current_ofs(1, 0));
176         if (state->wait_subreq == NULL) {
177                 return ENOMEM;
178         }
179         tevent_req_set_callback(state->wait_subreq, dns_lookup_waited, req);
180
181         return 0;
182 }
183
184 static void dns_lookup_done(struct tevent_req *subreq)
185 {
186         struct tevent_req *req = tevent_req_callback_data(
187                 subreq, struct tevent_req);
188         struct dns_lookup_state *state = tevent_req_data(
189                 req, struct dns_lookup_state);
190         int dns_cli_request_ret;
191         size_t i;
192
193         dns_cli_request_ret = dns_cli_request_recv(
194                 subreq,
195                 state,
196                 &state->reply);
197
198         for (i = 0; i < state->num_nameservers; i++) {
199                 if (state->dns_subreqs[i] == subreq) {
200                         break;
201                 }
202         }
203
204         TALLOC_FREE(subreq);
205
206         if (i == state->num_nameservers) {
207                 /* should never happen */
208                 DBG_WARNING("Failed to find subreq");
209                 tevent_req_error(req, EINVAL);
210                 return;
211         }
212         state->dns_subreqs[i] = NULL;
213
214         if (dns_cli_request_ret == 0) {
215                 /*
216                  * Success, cancel everything else
217                  */
218                 TALLOC_FREE(state->dns_subreqs);
219                 TALLOC_FREE(state->wait_subreq);
220                 tevent_req_done(req);
221                 return;
222         }
223
224         DBG_DEBUG("dns_cli_request[%zu] returned %s\n", i,
225                   strerror(dns_cli_request_ret));
226
227         if (state->num_sent < state->num_nameservers) {
228                 /*
229                  * We have a nameserver left to try
230                  */
231                 int ret;
232
233                 ret = dns_lookup_send_next(req);
234                 if (tevent_req_error(req, ret)) {
235                         return;
236                 }
237         }
238
239         DBG_DEBUG("looking for outstanding requests\n");
240
241         for (i = 0; i<state->num_nameservers; i++) {
242                 if (state->dns_subreqs[i] != NULL) {
243                         break;
244                 }
245         }
246
247         DBG_DEBUG("i=%zu, num_nameservers=%zu\n",
248                   i, state->num_nameservers);
249
250         if (i == state->num_nameservers) {
251                 /*
252                  * Report the lower-level error if we have nothing
253                  * outstanding anymore
254                  */
255                 tevent_req_error(req, dns_cli_request_ret);
256                 return;
257         }
258
259         /*
260          * Do nothing: We have other nameservers that might come back
261          * with something good.
262          */
263 }
264
265 static void dns_lookup_waited(struct tevent_req *subreq)
266 {
267         struct tevent_req *req = tevent_req_callback_data(
268                 subreq, struct tevent_req);
269         struct dns_lookup_state *state = tevent_req_data(
270                 req, struct dns_lookup_state);
271         int ret;
272         bool ok;
273
274         DBG_DEBUG("waited\n");
275
276         ok = tevent_wakeup_recv(subreq);
277         TALLOC_FREE(subreq);
278         if (!ok) {
279                 tevent_req_oom(req);
280                 return;
281         }
282         state->wait_subreq = NULL;
283
284         ret = dns_lookup_send_next(req);
285         if (tevent_req_error(req, ret)) {
286                 return;
287         }
288
289         /*
290          * dns_lookup_send_next() has already triggered the next wakeup
291          */
292 }
293
294 int dns_lookup_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
295                     struct dns_name_packet **reply)
296 {
297         struct dns_lookup_state *state = tevent_req_data(
298                 req, struct dns_lookup_state);
299         int err;
300
301         if (tevent_req_is_unix_error(req, &err)) {
302                 return err;
303         }
304
305         *reply = talloc_move(mem_ctx, &state->reply);
306
307         tevent_req_received(req);
308         return 0;
309 }
310
311 int dns_lookup(FILE *resolv_conf_fp,
312                const char *name,
313                enum dns_qclass qclass,
314                enum dns_qtype qtype,
315                TALLOC_CTX *mem_ctx,
316                struct dns_name_packet **reply)
317 {
318         struct tevent_context *ev;
319         struct tevent_req *req;
320         int ret = ENOMEM;
321
322         ev = samba_tevent_context_init(mem_ctx);
323         if (ev == NULL) {
324                 goto fail;
325         }
326         req = dns_lookup_send(ev, ev, resolv_conf_fp, name, qclass, qtype);
327         if (req == NULL) {
328                 goto fail;
329         }
330         if (!tevent_req_poll_unix(req, ev, &ret)) {
331                 goto fail;
332         }
333         ret = dns_lookup_recv(req, mem_ctx, reply);
334 fail:
335         TALLOC_FREE(ev);
336         return ret;
337 }
338
339 bool dns_res_rec_get_sockaddr(const struct dns_res_rec *rec,
340                               struct sockaddr_storage *addr)
341 {
342         sa_family_t family;
343         const char *src;
344         void *dst;
345         int ret;
346
347         switch (rec->rr_type) {
348             case DNS_QTYPE_A:
349                     family = AF_INET;
350                     src = rec->rdata.ipv4_record;
351                     dst = &(((struct sockaddr_in *)addr)->sin_addr);
352                     break;
353 #ifdef HAVE_IPV6
354             case DNS_QTYPE_AAAA:
355                     family = AF_INET6;
356                     src = rec->rdata.ipv6_record;
357                     dst = &(((struct sockaddr_in6 *)addr)->sin6_addr);
358                     break;
359 #endif
360             default:
361                     /* We only care about IP addresses */
362                     return false;
363         }
364
365         *addr = (struct sockaddr_storage) { .ss_family = family };
366
367         ret = inet_pton(family, src, dst);
368         if (ret != 1) {
369                 DBG_DEBUG("inet_pton(%s) failed\n", src);
370                 return false;
371         }
372
373         return true;
374 }