s4-dns: Use proper talloc hierarchy for AAAA records in create_response_rr
[bbaumbach/samba-autobuild/.git] / source4 / dns_server / dns_query.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    DNS server handler for queries
5
6    Copyright (C) 2010 Kai Blin  <kai@samba.org>
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "smbd/service_task.h"
24 #include "libcli/util/werror.h"
25 #include "librpc/ndr/libndr.h"
26 #include "librpc/gen_ndr/ndr_dns.h"
27 #include "librpc/gen_ndr/ndr_dnsp.h"
28 #include <ldb.h>
29 #include "param/param.h"
30 #include "dsdb/samdb/samdb.h"
31 #include "dsdb/common/util.h"
32 #include "dns_server/dns_server.h"
33 #include "libcli/dns/libdns.h"
34 #include "lib/util/util_net.h"
35 #include "lib/util/tevent_werror.h"
36
37 static WERROR create_response_rr(const struct dns_name_question *question,
38                                  const struct dnsp_DnssrvRpcRecord *rec,
39                                  struct dns_res_rec **answers, uint16_t *ancount)
40 {
41         struct dns_res_rec *ans = *answers;
42         uint16_t ai = *ancount;
43         char *tmp;
44         uint32_t i;
45
46         ZERO_STRUCT(ans[ai]);
47
48         switch (rec->wType) {
49         case DNS_QTYPE_CNAME:
50                 ans[ai].rdata.cname_record = talloc_strdup(ans, rec->data.cname);
51                 if (ans[ai].rdata.cname_record == NULL) {
52                         return WERR_NOMEM;
53                 }
54                 break;
55         case DNS_QTYPE_A:
56                 ans[ai].rdata.ipv4_record = talloc_strdup(ans, rec->data.ipv4);
57                 if (ans[ai].rdata.ipv4_record == NULL) {
58                         return WERR_NOMEM;
59                 }
60                 break;
61         case DNS_QTYPE_AAAA:
62                 ans[ai].rdata.ipv6_record = talloc_strdup(ans, rec->data.ipv6);
63                 W_ERROR_HAVE_NO_MEMORY(ans[ai].rdata.ipv6_record);
64                 break;
65         case DNS_TYPE_NS:
66                 ans[ai].rdata.ns_record = rec->data.ns;
67                 break;
68         case DNS_QTYPE_SRV:
69                 ans[ai].rdata.srv_record.priority = rec->data.srv.wPriority;
70                 ans[ai].rdata.srv_record.weight   = rec->data.srv.wWeight;
71                 ans[ai].rdata.srv_record.port     = rec->data.srv.wPort;
72                 ans[ai].rdata.srv_record.target   = talloc_strdup(
73                         ans, rec->data.srv.nameTarget);
74                 if (ans[ai].rdata.srv_record.target == NULL) {
75                         return WERR_NOMEM;
76                 }
77                 break;
78         case DNS_QTYPE_SOA:
79                 ans[ai].rdata.soa_record.mname   = talloc_strdup(
80                         ans, rec->data.soa.mname);
81                 if (ans[ai].rdata.soa_record.mname == NULL) {
82                         return WERR_NOMEM;
83                 }
84                 ans[ai].rdata.soa_record.rname   = talloc_strdup(
85                         ans, rec->data.soa.rname);
86                 if (ans[ai].rdata.soa_record.rname == NULL) {
87                         return WERR_NOMEM;
88                 }
89                 ans[ai].rdata.soa_record.serial  = rec->data.soa.serial;
90                 ans[ai].rdata.soa_record.refresh = rec->data.soa.refresh;
91                 ans[ai].rdata.soa_record.retry   = rec->data.soa.retry;
92                 ans[ai].rdata.soa_record.expire  = rec->data.soa.expire;
93                 ans[ai].rdata.soa_record.minimum = rec->data.soa.minimum;
94                 break;
95         case DNS_QTYPE_PTR:
96                 ans[ai].rdata.ptr_record = talloc_strdup(ans, rec->data.ptr);
97                 break;
98         case DNS_QTYPE_TXT:
99                 tmp = talloc_asprintf(ans, "\"%s\"", rec->data.txt.str[0]);
100                 if (tmp == NULL) {
101                         return WERR_NOMEM;
102                 }
103                 for (i=1; i<rec->data.txt.count; i++) {
104                         tmp = talloc_asprintf_append_buffer(
105                                 tmp, " \"%s\"", rec->data.txt.str[i]);
106                         if (tmp == NULL) {
107                                 return WERR_NOMEM;
108                         }
109                 }
110                 ans[ai].rdata.txt_record.txt = tmp;
111                 break;
112         default:
113                 DEBUG(0, ("Got unhandled type %u query.\n", rec->wType));
114                 return DNS_ERR(NOT_IMPLEMENTED);
115         }
116
117         ans[ai].name = talloc_strdup(ans, question->name);
118         if (ans[ai].name == NULL) {
119                 return WERR_NOMEM;
120         }
121         ans[ai].rr_type = rec->wType;
122         ans[ai].rr_class = DNS_QCLASS_IN;
123         ans[ai].ttl = rec->dwTtlSeconds;
124         ans[ai].length = UINT16_MAX;
125         ai++;
126
127         *answers = ans;
128         *ancount = ai;
129
130         return WERR_OK;
131 }
132
133 struct ask_forwarder_state {
134         struct tevent_context *ev;
135         uint16_t id;
136         struct dns_name_packet in_packet;
137 };
138
139 static void ask_forwarder_done(struct tevent_req *subreq);
140
141 static struct tevent_req *ask_forwarder_send(
142         TALLOC_CTX *mem_ctx, struct tevent_context *ev,
143         const char *forwarder, struct dns_name_question *question)
144 {
145         struct tevent_req *req, *subreq;
146         struct ask_forwarder_state *state;
147         struct dns_name_packet out_packet = { 0, };
148         DATA_BLOB out_blob;
149         enum ndr_err_code ndr_err;
150
151         req = tevent_req_create(mem_ctx, &state, struct ask_forwarder_state);
152         if (req == NULL) {
153                 return NULL;
154         }
155         state->ev = ev;
156         generate_random_buffer((uint8_t *)&state->id, sizeof(state->id));
157
158         if (!is_ipaddress(forwarder)) {
159                 DEBUG(0, ("Invalid 'dns forwarder' setting '%s', needs to be "
160                           "an IP address\n", forwarder));
161                 tevent_req_werror(req, DNS_ERR(NAME_ERROR));
162                 return tevent_req_post(req, ev);
163         }
164
165         out_packet.id = state->id;
166         out_packet.operation |= DNS_OPCODE_QUERY | DNS_FLAG_RECURSION_DESIRED;
167         out_packet.qdcount = 1;
168         out_packet.questions = question;
169
170         ndr_err = ndr_push_struct_blob(
171                 &out_blob, state, &out_packet,
172                 (ndr_push_flags_fn_t)ndr_push_dns_name_packet);
173         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
174                 tevent_req_werror(req, DNS_ERR(SERVER_FAILURE));
175                 return tevent_req_post(req, ev);
176         }
177         subreq = dns_udp_request_send(state, ev, forwarder, out_blob.data,
178                                       out_blob.length);
179         if (tevent_req_nomem(subreq, req)) {
180                 return tevent_req_post(req, ev);
181         }
182         tevent_req_set_callback(subreq, ask_forwarder_done, req);
183         return req;
184 }
185
186 static void ask_forwarder_done(struct tevent_req *subreq)
187 {
188         struct tevent_req *req = tevent_req_callback_data(
189                 subreq, struct tevent_req);
190         struct ask_forwarder_state *state = tevent_req_data(
191                 req, struct ask_forwarder_state);
192         DATA_BLOB in_blob;
193         enum ndr_err_code ndr_err;
194         WERROR ret;
195
196         ret = dns_udp_request_recv(subreq, state,
197                                    &in_blob.data, &in_blob.length);
198         TALLOC_FREE(subreq);
199         if (tevent_req_werror(req, ret)) {
200                 return;
201         }
202
203         ndr_err = ndr_pull_struct_blob(
204                 &in_blob, state, &state->in_packet,
205                 (ndr_pull_flags_fn_t)ndr_pull_dns_name_packet);
206         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
207                 tevent_req_werror(req, DNS_ERR(SERVER_FAILURE));
208                 return;
209         }
210         if (state->in_packet.id != state->id) {
211                 tevent_req_werror(req, DNS_ERR(NAME_ERROR));
212                 return;
213         }
214         tevent_req_done(req);
215 }
216
217 static WERROR ask_forwarder_recv(
218         struct tevent_req *req, TALLOC_CTX *mem_ctx,
219         struct dns_res_rec **answers, uint16_t *ancount,
220         struct dns_res_rec **nsrecs, uint16_t *nscount,
221         struct dns_res_rec **additional, uint16_t *arcount)
222 {
223         struct ask_forwarder_state *state = tevent_req_data(
224                 req, struct ask_forwarder_state);
225         struct dns_name_packet *in_packet = &state->in_packet;
226         WERROR err;
227
228         if (tevent_req_is_werror(req, &err)) {
229                 return err;
230         }
231
232         *ancount = in_packet->ancount;
233         *answers = talloc_move(mem_ctx, &in_packet->answers);
234
235         *nscount = in_packet->nscount;
236         *nsrecs = talloc_move(mem_ctx, &in_packet->nsrecs);
237
238         *arcount = in_packet->arcount;
239         *additional = talloc_move(mem_ctx, &in_packet->additional);
240
241         return WERR_OK;
242 }
243
244 static WERROR handle_question(struct dns_server *dns,
245                               TALLOC_CTX *mem_ctx,
246                               const struct dns_name_question *question,
247                               struct dns_res_rec **answers, uint16_t *ancount)
248 {
249         struct dns_res_rec *ans;
250         WERROR werror;
251         unsigned int ri;
252         struct dnsp_DnssrvRpcRecord *recs;
253         uint16_t rec_count, ai = 0;
254         struct ldb_dn *dn = NULL;
255
256         werror = dns_name2dn(dns, mem_ctx, question->name, &dn);
257         W_ERROR_NOT_OK_RETURN(werror);
258
259         werror = dns_lookup_records(dns, mem_ctx, dn, &recs, &rec_count);
260         W_ERROR_NOT_OK_RETURN(werror);
261
262         ans = talloc_zero_array(mem_ctx, struct dns_res_rec, rec_count);
263         W_ERROR_HAVE_NO_MEMORY(ans);
264
265         for (ri = 0; ri < rec_count; ri++) {
266                 if ((question->question_type != DNS_QTYPE_ALL) &&
267                     (recs[ri].wType != question->question_type)) {
268                         continue;
269                 }
270                 werror = create_response_rr(question, &recs[ri], &ans, &ai);
271                 W_ERROR_NOT_OK_RETURN(werror);
272         }
273
274         if (ai == 0) {
275                 return DNS_ERR(NAME_ERROR);
276         }
277
278         *ancount = ai;
279         *answers = ans;
280
281         return WERR_OK;
282
283 }
284
285 struct dns_server_process_query_state {
286         struct dns_res_rec *answers;
287         uint16_t ancount;
288         struct dns_res_rec *nsrecs;
289         uint16_t nscount;
290         struct dns_res_rec *additional;
291         uint16_t arcount;
292 };
293
294 static void dns_server_process_query_got_response(struct tevent_req *subreq);
295
296 struct tevent_req *dns_server_process_query_send(
297         TALLOC_CTX *mem_ctx, struct tevent_context *ev,
298         struct dns_server *dns, struct dns_request_state *req_state,
299         const struct dns_name_packet *in)
300 {
301         struct tevent_req *req, *subreq;
302         struct dns_server_process_query_state *state;
303
304         req = tevent_req_create(mem_ctx, &state,
305                                 struct dns_server_process_query_state);
306         if (req == NULL) {
307                 return NULL;
308         }
309         if (in->qdcount != 1) {
310                 tevent_req_werror(req, DNS_ERR(FORMAT_ERROR));
311                 return tevent_req_post(req, ev);
312         }
313
314         /* Windows returns NOT_IMPLEMENTED on this as well */
315         if (in->questions[0].question_class == DNS_QCLASS_NONE) {
316                 tevent_req_werror(req, DNS_ERR(NOT_IMPLEMENTED));
317                 return tevent_req_post(req, ev);
318         }
319
320         if (dns_authorative_for_zone(dns, in->questions[0].name)) {
321                 WERROR err;
322
323                 req_state->flags |= DNS_FLAG_AUTHORITATIVE;
324                 err = handle_question(dns, state, &in->questions[0],
325                                       &state->answers, &state->ancount);
326                 if (tevent_req_werror(req, err)) {
327                         return tevent_req_post(req, ev);
328                 }
329                 tevent_req_done(req);
330                 return tevent_req_post(req, ev);
331         }
332
333         if ((req_state->flags & DNS_FLAG_RECURSION_DESIRED) &&
334             (req_state->flags & DNS_FLAG_RECURSION_AVAIL)) {
335                 DEBUG(2, ("Not authoritative for '%s', forwarding\n",
336                           in->questions[0].name));
337
338                 subreq = ask_forwarder_send(
339                         state, ev, lpcfg_dns_forwarder(dns->task->lp_ctx),
340                         &in->questions[0]);
341                 if (tevent_req_nomem(subreq, req)) {
342                         return tevent_req_post(req, ev);
343                 }
344                 tevent_req_set_callback(
345                         subreq, dns_server_process_query_got_response, req);
346                 return req;
347         }
348
349         tevent_req_werror(req, DNS_ERR(NAME_ERROR));
350         return tevent_req_post(req, ev);
351 }
352
353 static void dns_server_process_query_got_response(struct tevent_req *subreq)
354 {
355         struct tevent_req *req = tevent_req_callback_data(
356                 subreq, struct tevent_req);
357         struct dns_server_process_query_state *state = tevent_req_data(
358                 req, struct dns_server_process_query_state);
359         WERROR err;
360
361         err = ask_forwarder_recv(subreq, state,
362                                  &state->answers, &state->ancount,
363                                  &state->nsrecs, &state->nscount,
364                                  &state->additional, &state->arcount);
365         TALLOC_FREE(subreq);
366         if (tevent_req_werror(req, err)) {
367                 return;
368         }
369         tevent_req_done(req);
370 }
371
372 WERROR dns_server_process_query_recv(
373         struct tevent_req *req, TALLOC_CTX *mem_ctx,
374         struct dns_res_rec **answers,    uint16_t *ancount,
375         struct dns_res_rec **nsrecs,     uint16_t *nscount,
376         struct dns_res_rec **additional, uint16_t *arcount)
377 {
378         struct dns_server_process_query_state *state = tevent_req_data(
379                 req, struct dns_server_process_query_state);
380         WERROR err;
381
382         if (tevent_req_is_werror(req, &err)) {
383                 return err;
384         }
385         *answers = talloc_move(mem_ctx, &state->answers);
386         *ancount = state->ancount;
387         *nsrecs = talloc_move(mem_ctx, &state->nsrecs);
388         *nscount = state->nscount;
389         *additional = talloc_move(mem_ctx, &state->additional);
390         *arcount = state->arcount;
391         return WERR_OK;
392 }