s4-dns: Use proper talloc hierarchy for NS records in create_response_rr
[kai/samba.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 = talloc_strdup(ans, rec->data.ns);
67                 W_ERROR_HAVE_NO_MEMORY(ans[ai].rdata.ns_record);
68                 break;
69         case DNS_QTYPE_SRV:
70                 ans[ai].rdata.srv_record.priority = rec->data.srv.wPriority;
71                 ans[ai].rdata.srv_record.weight   = rec->data.srv.wWeight;
72                 ans[ai].rdata.srv_record.port     = rec->data.srv.wPort;
73                 ans[ai].rdata.srv_record.target   = talloc_strdup(
74                         ans, rec->data.srv.nameTarget);
75                 if (ans[ai].rdata.srv_record.target == NULL) {
76                         return WERR_NOMEM;
77                 }
78                 break;
79         case DNS_QTYPE_SOA:
80                 ans[ai].rdata.soa_record.mname   = talloc_strdup(
81                         ans, rec->data.soa.mname);
82                 if (ans[ai].rdata.soa_record.mname == NULL) {
83                         return WERR_NOMEM;
84                 }
85                 ans[ai].rdata.soa_record.rname   = talloc_strdup(
86                         ans, rec->data.soa.rname);
87                 if (ans[ai].rdata.soa_record.rname == NULL) {
88                         return WERR_NOMEM;
89                 }
90                 ans[ai].rdata.soa_record.serial  = rec->data.soa.serial;
91                 ans[ai].rdata.soa_record.refresh = rec->data.soa.refresh;
92                 ans[ai].rdata.soa_record.retry   = rec->data.soa.retry;
93                 ans[ai].rdata.soa_record.expire  = rec->data.soa.expire;
94                 ans[ai].rdata.soa_record.minimum = rec->data.soa.minimum;
95                 break;
96         case DNS_QTYPE_PTR:
97                 ans[ai].rdata.ptr_record = talloc_strdup(ans, rec->data.ptr);
98                 break;
99         case DNS_QTYPE_TXT:
100                 tmp = talloc_asprintf(ans, "\"%s\"", rec->data.txt.str[0]);
101                 if (tmp == NULL) {
102                         return WERR_NOMEM;
103                 }
104                 for (i=1; i<rec->data.txt.count; i++) {
105                         tmp = talloc_asprintf_append_buffer(
106                                 tmp, " \"%s\"", rec->data.txt.str[i]);
107                         if (tmp == NULL) {
108                                 return WERR_NOMEM;
109                         }
110                 }
111                 ans[ai].rdata.txt_record.txt = tmp;
112                 break;
113         default:
114                 DEBUG(0, ("Got unhandled type %u query.\n", rec->wType));
115                 return DNS_ERR(NOT_IMPLEMENTED);
116         }
117
118         ans[ai].name = talloc_strdup(ans, question->name);
119         if (ans[ai].name == NULL) {
120                 return WERR_NOMEM;
121         }
122         ans[ai].rr_type = rec->wType;
123         ans[ai].rr_class = DNS_QCLASS_IN;
124         ans[ai].ttl = rec->dwTtlSeconds;
125         ans[ai].length = UINT16_MAX;
126         ai++;
127
128         *answers = ans;
129         *ancount = ai;
130
131         return WERR_OK;
132 }
133
134 struct ask_forwarder_state {
135         struct tevent_context *ev;
136         uint16_t id;
137         struct dns_name_packet in_packet;
138 };
139
140 static void ask_forwarder_done(struct tevent_req *subreq);
141
142 static struct tevent_req *ask_forwarder_send(
143         TALLOC_CTX *mem_ctx, struct tevent_context *ev,
144         const char *forwarder, struct dns_name_question *question)
145 {
146         struct tevent_req *req, *subreq;
147         struct ask_forwarder_state *state;
148         struct dns_name_packet out_packet = { 0, };
149         DATA_BLOB out_blob;
150         enum ndr_err_code ndr_err;
151
152         req = tevent_req_create(mem_ctx, &state, struct ask_forwarder_state);
153         if (req == NULL) {
154                 return NULL;
155         }
156         state->ev = ev;
157         generate_random_buffer((uint8_t *)&state->id, sizeof(state->id));
158
159         if (!is_ipaddress(forwarder)) {
160                 DEBUG(0, ("Invalid 'dns forwarder' setting '%s', needs to be "
161                           "an IP address\n", forwarder));
162                 tevent_req_werror(req, DNS_ERR(NAME_ERROR));
163                 return tevent_req_post(req, ev);
164         }
165
166         out_packet.id = state->id;
167         out_packet.operation |= DNS_OPCODE_QUERY | DNS_FLAG_RECURSION_DESIRED;
168         out_packet.qdcount = 1;
169         out_packet.questions = question;
170
171         ndr_err = ndr_push_struct_blob(
172                 &out_blob, state, &out_packet,
173                 (ndr_push_flags_fn_t)ndr_push_dns_name_packet);
174         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
175                 tevent_req_werror(req, DNS_ERR(SERVER_FAILURE));
176                 return tevent_req_post(req, ev);
177         }
178         subreq = dns_udp_request_send(state, ev, forwarder, out_blob.data,
179                                       out_blob.length);
180         if (tevent_req_nomem(subreq, req)) {
181                 return tevent_req_post(req, ev);
182         }
183         tevent_req_set_callback(subreq, ask_forwarder_done, req);
184         return req;
185 }
186
187 static void ask_forwarder_done(struct tevent_req *subreq)
188 {
189         struct tevent_req *req = tevent_req_callback_data(
190                 subreq, struct tevent_req);
191         struct ask_forwarder_state *state = tevent_req_data(
192                 req, struct ask_forwarder_state);
193         DATA_BLOB in_blob;
194         enum ndr_err_code ndr_err;
195         WERROR ret;
196
197         ret = dns_udp_request_recv(subreq, state,
198                                    &in_blob.data, &in_blob.length);
199         TALLOC_FREE(subreq);
200         if (tevent_req_werror(req, ret)) {
201                 return;
202         }
203
204         ndr_err = ndr_pull_struct_blob(
205                 &in_blob, state, &state->in_packet,
206                 (ndr_pull_flags_fn_t)ndr_pull_dns_name_packet);
207         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
208                 tevent_req_werror(req, DNS_ERR(SERVER_FAILURE));
209                 return;
210         }
211         if (state->in_packet.id != state->id) {
212                 tevent_req_werror(req, DNS_ERR(NAME_ERROR));
213                 return;
214         }
215         tevent_req_done(req);
216 }
217
218 static WERROR ask_forwarder_recv(
219         struct tevent_req *req, TALLOC_CTX *mem_ctx,
220         struct dns_res_rec **answers, uint16_t *ancount,
221         struct dns_res_rec **nsrecs, uint16_t *nscount,
222         struct dns_res_rec **additional, uint16_t *arcount)
223 {
224         struct ask_forwarder_state *state = tevent_req_data(
225                 req, struct ask_forwarder_state);
226         struct dns_name_packet *in_packet = &state->in_packet;
227         WERROR err;
228
229         if (tevent_req_is_werror(req, &err)) {
230                 return err;
231         }
232
233         *ancount = in_packet->ancount;
234         *answers = talloc_move(mem_ctx, &in_packet->answers);
235
236         *nscount = in_packet->nscount;
237         *nsrecs = talloc_move(mem_ctx, &in_packet->nsrecs);
238
239         *arcount = in_packet->arcount;
240         *additional = talloc_move(mem_ctx, &in_packet->additional);
241
242         return WERR_OK;
243 }
244
245 static WERROR handle_question(struct dns_server *dns,
246                               TALLOC_CTX *mem_ctx,
247                               const struct dns_name_question *question,
248                               struct dns_res_rec **answers, uint16_t *ancount)
249 {
250         struct dns_res_rec *ans;
251         WERROR werror;
252         unsigned int ri;
253         struct dnsp_DnssrvRpcRecord *recs;
254         uint16_t rec_count, ai = 0;
255         struct ldb_dn *dn = NULL;
256
257         werror = dns_name2dn(dns, mem_ctx, question->name, &dn);
258         W_ERROR_NOT_OK_RETURN(werror);
259
260         werror = dns_lookup_records(dns, mem_ctx, dn, &recs, &rec_count);
261         W_ERROR_NOT_OK_RETURN(werror);
262
263         ans = talloc_zero_array(mem_ctx, struct dns_res_rec, rec_count);
264         W_ERROR_HAVE_NO_MEMORY(ans);
265
266         for (ri = 0; ri < rec_count; ri++) {
267                 if ((question->question_type != DNS_QTYPE_ALL) &&
268                     (recs[ri].wType != question->question_type)) {
269                         continue;
270                 }
271                 werror = create_response_rr(question, &recs[ri], &ans, &ai);
272                 W_ERROR_NOT_OK_RETURN(werror);
273         }
274
275         if (ai == 0) {
276                 return DNS_ERR(NAME_ERROR);
277         }
278
279         *ancount = ai;
280         *answers = ans;
281
282         return WERR_OK;
283
284 }
285
286 struct dns_server_process_query_state {
287         struct dns_res_rec *answers;
288         uint16_t ancount;
289         struct dns_res_rec *nsrecs;
290         uint16_t nscount;
291         struct dns_res_rec *additional;
292         uint16_t arcount;
293 };
294
295 static void dns_server_process_query_got_response(struct tevent_req *subreq);
296
297 struct tevent_req *dns_server_process_query_send(
298         TALLOC_CTX *mem_ctx, struct tevent_context *ev,
299         struct dns_server *dns, struct dns_request_state *req_state,
300         const struct dns_name_packet *in)
301 {
302         struct tevent_req *req, *subreq;
303         struct dns_server_process_query_state *state;
304
305         req = tevent_req_create(mem_ctx, &state,
306                                 struct dns_server_process_query_state);
307         if (req == NULL) {
308                 return NULL;
309         }
310         if (in->qdcount != 1) {
311                 tevent_req_werror(req, DNS_ERR(FORMAT_ERROR));
312                 return tevent_req_post(req, ev);
313         }
314
315         /* Windows returns NOT_IMPLEMENTED on this as well */
316         if (in->questions[0].question_class == DNS_QCLASS_NONE) {
317                 tevent_req_werror(req, DNS_ERR(NOT_IMPLEMENTED));
318                 return tevent_req_post(req, ev);
319         }
320
321         if (dns_authorative_for_zone(dns, in->questions[0].name)) {
322                 WERROR err;
323
324                 req_state->flags |= DNS_FLAG_AUTHORITATIVE;
325                 err = handle_question(dns, state, &in->questions[0],
326                                       &state->answers, &state->ancount);
327                 if (tevent_req_werror(req, err)) {
328                         return tevent_req_post(req, ev);
329                 }
330                 tevent_req_done(req);
331                 return tevent_req_post(req, ev);
332         }
333
334         if ((req_state->flags & DNS_FLAG_RECURSION_DESIRED) &&
335             (req_state->flags & DNS_FLAG_RECURSION_AVAIL)) {
336                 DEBUG(2, ("Not authoritative for '%s', forwarding\n",
337                           in->questions[0].name));
338
339                 subreq = ask_forwarder_send(
340                         state, ev, lpcfg_dns_forwarder(dns->task->lp_ctx),
341                         &in->questions[0]);
342                 if (tevent_req_nomem(subreq, req)) {
343                         return tevent_req_post(req, ev);
344                 }
345                 tevent_req_set_callback(
346                         subreq, dns_server_process_query_got_response, req);
347                 return req;
348         }
349
350         tevent_req_werror(req, DNS_ERR(NAME_ERROR));
351         return tevent_req_post(req, ev);
352 }
353
354 static void dns_server_process_query_got_response(struct tevent_req *subreq)
355 {
356         struct tevent_req *req = tevent_req_callback_data(
357                 subreq, struct tevent_req);
358         struct dns_server_process_query_state *state = tevent_req_data(
359                 req, struct dns_server_process_query_state);
360         WERROR err;
361
362         err = ask_forwarder_recv(subreq, state,
363                                  &state->answers, &state->ancount,
364                                  &state->nsrecs, &state->nscount,
365                                  &state->additional, &state->arcount);
366         TALLOC_FREE(subreq);
367         if (tevent_req_werror(req, err)) {
368                 return;
369         }
370         tevent_req_done(req);
371 }
372
373 WERROR dns_server_process_query_recv(
374         struct tevent_req *req, TALLOC_CTX *mem_ctx,
375         struct dns_res_rec **answers,    uint16_t *ancount,
376         struct dns_res_rec **nsrecs,     uint16_t *nscount,
377         struct dns_res_rec **additional, uint16_t *arcount)
378 {
379         struct dns_server_process_query_state *state = tevent_req_data(
380                 req, struct dns_server_process_query_state);
381         WERROR err;
382
383         if (tevent_req_is_werror(req, &err)) {
384                 return err;
385         }
386         *answers = talloc_move(mem_ctx, &state->answers);
387         *ancount = state->ancount;
388         *nsrecs = talloc_move(mem_ctx, &state->nsrecs);
389         *nscount = state->nscount;
390         *additional = talloc_move(mem_ctx, &state->additional);
391         *arcount = state->arcount;
392         return WERR_OK;
393 }