CVE-2016-2115: s3:winbindd: use lp_client_ipc_{min,max}_protocol()
[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 #include "auth/auth.h"
37 #include "auth/credentials/credentials.h"
38 #include "auth/gensec/gensec.h"
39
40 #undef DBGC_CLASS
41 #define DBGC_CLASS DBGC_DNS
42
43 static WERROR add_response_rr(const char *name,
44                               const struct dnsp_DnssrvRpcRecord *rec,
45                               struct dns_res_rec **answers)
46 {
47         struct dns_res_rec *ans = *answers;
48         uint16_t ai = talloc_array_length(ans);
49         enum ndr_err_code ndr_err;
50
51         if (ai == UINT16_MAX) {
52                 return WERR_BUFFER_OVERFLOW;
53         }
54
55         /*
56          * "ans" is always non-NULL and thus its own talloc context
57          */
58         ans = talloc_realloc(ans, ans, struct dns_res_rec, ai+1);
59         if (ans == NULL) {
60                 return WERR_NOMEM;
61         }
62
63         ZERO_STRUCT(ans[ai]);
64
65         switch (rec->wType) {
66         case DNS_QTYPE_CNAME:
67                 ans[ai].rdata.cname_record = talloc_strdup(ans, rec->data.cname);
68                 W_ERROR_HAVE_NO_MEMORY(ans[ai].rdata.cname_record);
69                 break;
70         case DNS_QTYPE_A:
71                 ans[ai].rdata.ipv4_record = talloc_strdup(ans, rec->data.ipv4);
72                 W_ERROR_HAVE_NO_MEMORY(ans[ai].rdata.ipv4_record);
73                 break;
74         case DNS_QTYPE_AAAA:
75                 ans[ai].rdata.ipv6_record = talloc_strdup(ans, rec->data.ipv6);
76                 W_ERROR_HAVE_NO_MEMORY(ans[ai].rdata.ipv6_record);
77                 break;
78         case DNS_TYPE_NS:
79                 ans[ai].rdata.ns_record = talloc_strdup(ans, rec->data.ns);
80                 W_ERROR_HAVE_NO_MEMORY(ans[ai].rdata.ns_record);
81                 break;
82         case DNS_QTYPE_SRV:
83                 ans[ai].rdata.srv_record.priority = rec->data.srv.wPriority;
84                 ans[ai].rdata.srv_record.weight   = rec->data.srv.wWeight;
85                 ans[ai].rdata.srv_record.port     = rec->data.srv.wPort;
86                 ans[ai].rdata.srv_record.target   = talloc_strdup(
87                         ans, rec->data.srv.nameTarget);
88                 W_ERROR_HAVE_NO_MEMORY(ans[ai].rdata.srv_record.target);
89                 break;
90         case DNS_QTYPE_SOA:
91                 ans[ai].rdata.soa_record.mname   = talloc_strdup(
92                         ans, rec->data.soa.mname);
93                 W_ERROR_HAVE_NO_MEMORY(ans[ai].rdata.soa_record.mname);
94                 ans[ai].rdata.soa_record.rname   = talloc_strdup(
95                         ans, rec->data.soa.rname);
96                 W_ERROR_HAVE_NO_MEMORY(ans[ai].rdata.soa_record.rname);
97                 ans[ai].rdata.soa_record.serial  = rec->data.soa.serial;
98                 ans[ai].rdata.soa_record.refresh = rec->data.soa.refresh;
99                 ans[ai].rdata.soa_record.retry   = rec->data.soa.retry;
100                 ans[ai].rdata.soa_record.expire  = rec->data.soa.expire;
101                 ans[ai].rdata.soa_record.minimum = rec->data.soa.minimum;
102                 break;
103         case DNS_QTYPE_PTR:
104                 ans[ai].rdata.ptr_record = talloc_strdup(ans, rec->data.ptr);
105                 W_ERROR_HAVE_NO_MEMORY(ans[ai].rdata.ptr_record);
106                 break;
107         case DNS_QTYPE_MX:
108                 ans[ai].rdata.mx_record.preference = rec->data.mx.wPriority;
109                 ans[ai].rdata.mx_record.exchange = talloc_strdup(
110                         ans, rec->data.mx.nameTarget);
111                 if (ans[ai].rdata.mx_record.exchange == NULL) {
112                         return WERR_NOMEM;
113                 }
114                 break;
115         case DNS_QTYPE_TXT:
116                 ndr_err = ndr_dnsp_string_list_copy(ans,
117                                                     &rec->data.txt,
118                                                     &ans[ai].rdata.txt_record.txt);
119                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
120                         return WERR_NOMEM;
121                 }
122                 break;
123         default:
124                 DEBUG(0, ("Got unhandled type %u query.\n", rec->wType));
125                 return DNS_ERR(NOT_IMPLEMENTED);
126         }
127
128         ans[ai].name = talloc_strdup(ans, name);
129         W_ERROR_HAVE_NO_MEMORY(ans[ai].name);
130         ans[ai].rr_type = rec->wType;
131         ans[ai].rr_class = DNS_QCLASS_IN;
132         ans[ai].ttl = rec->dwTtlSeconds;
133         ans[ai].length = UINT16_MAX;
134
135         *answers = ans;
136
137         return WERR_OK;
138 }
139
140 static WERROR add_dns_res_rec(struct dns_res_rec **pdst,
141                               const struct dns_res_rec *src)
142 {
143         struct dns_res_rec *dst = *pdst;
144         uint16_t di = talloc_array_length(dst);
145         enum ndr_err_code ndr_err;
146
147         if (di == UINT16_MAX) {
148                 return WERR_BUFFER_OVERFLOW;
149         }
150
151         dst = talloc_realloc(dst, dst, struct dns_res_rec, di+1);
152         if (dst == NULL) {
153                 return WERR_NOMEM;
154         }
155
156         ZERO_STRUCT(dst[di]);
157
158         dst[di] = (struct dns_res_rec) {
159                 .name = talloc_strdup(dst, src->name),
160                 .rr_type = src->rr_type,
161                 .rr_class = src->rr_class,
162                 .ttl = src->ttl,
163                 .length = src->length
164         };
165
166         if (dst[di].name == NULL) {
167                 return WERR_NOMEM;
168         }
169
170         switch (src->rr_type) {
171         case DNS_QTYPE_CNAME:
172                 dst[di].rdata.cname_record = talloc_strdup(
173                         dst, src->rdata.cname_record);
174                 if (dst[di].rdata.cname_record == NULL) {
175                         return WERR_NOMEM;
176                 }
177                 break;
178         case DNS_QTYPE_A:
179                 dst[di].rdata.ipv4_record = talloc_strdup(
180                         dst, src->rdata.ipv4_record);
181                 if (dst[di].rdata.ipv4_record == NULL) {
182                         return WERR_NOMEM;
183                 }
184                 break;
185         case DNS_QTYPE_AAAA:
186                 dst[di].rdata.ipv6_record = talloc_strdup(
187                         dst, src->rdata.ipv6_record);
188                 if (dst[di].rdata.ipv6_record == NULL) {
189                         return WERR_NOMEM;
190                 }
191                 break;
192         case DNS_TYPE_NS:
193                 dst[di].rdata.ns_record = talloc_strdup(
194                         dst, src->rdata.ns_record);
195                 if (dst[di].rdata.ns_record == NULL) {
196                         return WERR_NOMEM;
197                 }
198                 break;
199         case DNS_QTYPE_SRV:
200                 dst[di].rdata.srv_record = (struct dns_srv_record) {
201                         .priority = src->rdata.srv_record.priority,
202                         .weight   = src->rdata.srv_record.weight,
203                         .port     = src->rdata.srv_record.port,
204                         .target   = talloc_strdup(
205                                 dst, src->rdata.srv_record.target)
206                 };
207                 if (dst[di].rdata.srv_record.target == NULL) {
208                         return WERR_NOMEM;
209                 }
210                 break;
211         case DNS_QTYPE_SOA:
212                 dst[di].rdata.soa_record = (struct dns_soa_record) {
213                         .mname   = talloc_strdup(
214                                 dst, src->rdata.soa_record.mname),
215                         .rname   = talloc_strdup(
216                                 dst, src->rdata.soa_record.rname),
217                         .serial  = src->rdata.soa_record.serial,
218                         .refresh = src->rdata.soa_record.refresh,
219                         .retry   = src->rdata.soa_record.retry,
220                         .expire  = src->rdata.soa_record.expire,
221                         .minimum = src->rdata.soa_record.minimum
222                 };
223
224                 if ((dst[di].rdata.soa_record.mname == NULL) ||
225                     (dst[di].rdata.soa_record.rname == NULL)) {
226                         return WERR_NOMEM;
227                 }
228
229                 break;
230         case DNS_QTYPE_PTR:
231                 dst[di].rdata.ptr_record = talloc_strdup(
232                         dst, src->rdata.ptr_record);
233                 if (dst[di].rdata.ptr_record == NULL) {
234                         return WERR_NOMEM;
235                 }
236                 break;
237         case DNS_QTYPE_MX:
238                 dst[di].rdata.mx_record = (struct dns_mx_record) {
239                         .preference = src->rdata.mx_record.preference,
240                         .exchange   = talloc_strdup(
241                                 src, src->rdata.mx_record.exchange)
242                 };
243
244                 if (dst[di].rdata.mx_record.exchange == NULL) {
245                         return WERR_NOMEM;
246                 }
247                 break;
248         case DNS_QTYPE_TXT:
249                 ndr_err = ndr_dnsp_string_list_copy(dst,
250                                                     &src->rdata.txt_record.txt,
251                                                     &dst[di].rdata.txt_record.txt);
252                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
253                         return WERR_NOMEM;
254                 }
255                 break;
256         default:
257                 DBG_WARNING("Got unhandled type %u query.\n", src->rr_type);
258                 return DNS_ERR(NOT_IMPLEMENTED);
259         }
260
261         *pdst = dst;
262
263         return WERR_OK;
264 }
265
266 struct ask_forwarder_state {
267         struct tevent_context *ev;
268         uint16_t id;
269         struct dns_name_packet in_packet;
270 };
271
272 static void ask_forwarder_done(struct tevent_req *subreq);
273
274 static struct tevent_req *ask_forwarder_send(
275         TALLOC_CTX *mem_ctx, struct tevent_context *ev,
276         struct dns_server *dns,
277         const char *forwarder, struct dns_name_question *question)
278 {
279         struct tevent_req *req, *subreq;
280         struct ask_forwarder_state *state;
281         struct dns_res_rec *options;
282         struct dns_name_packet out_packet = { 0, };
283         DATA_BLOB out_blob;
284         enum ndr_err_code ndr_err;
285         WERROR werr;
286
287         req = tevent_req_create(mem_ctx, &state, struct ask_forwarder_state);
288         if (req == NULL) {
289                 return NULL;
290         }
291         state->ev = ev;
292         generate_random_buffer((uint8_t *)&state->id, sizeof(state->id));
293
294         if (!is_ipaddress(forwarder)) {
295                 DEBUG(0, ("Invalid 'dns forwarder' setting '%s', needs to be "
296                           "an IP address\n", forwarder));
297                 tevent_req_werror(req, DNS_ERR(NAME_ERROR));
298                 return tevent_req_post(req, ev);
299         }
300
301         out_packet.id = state->id;
302         out_packet.operation |= DNS_OPCODE_QUERY | DNS_FLAG_RECURSION_DESIRED;
303         out_packet.qdcount = 1;
304         out_packet.questions = question;
305
306         werr = dns_generate_options(dns, state, &options);
307         if (!W_ERROR_IS_OK(werr)) {
308                 tevent_req_werror(req, werr);
309                 return tevent_req_post(req, ev);
310         }
311
312         out_packet.arcount = 1;
313         out_packet.additional = options;
314
315         ndr_err = ndr_push_struct_blob(
316                 &out_blob, state, &out_packet,
317                 (ndr_push_flags_fn_t)ndr_push_dns_name_packet);
318         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
319                 tevent_req_werror(req, DNS_ERR(SERVER_FAILURE));
320                 return tevent_req_post(req, ev);
321         }
322         subreq = dns_udp_request_send(state, ev, forwarder, out_blob.data,
323                                       out_blob.length);
324         if (tevent_req_nomem(subreq, req)) {
325                 return tevent_req_post(req, ev);
326         }
327         tevent_req_set_callback(subreq, ask_forwarder_done, req);
328         return req;
329 }
330
331 static void ask_forwarder_done(struct tevent_req *subreq)
332 {
333         struct tevent_req *req = tevent_req_callback_data(
334                 subreq, struct tevent_req);
335         struct ask_forwarder_state *state = tevent_req_data(
336                 req, struct ask_forwarder_state);
337         DATA_BLOB in_blob;
338         enum ndr_err_code ndr_err;
339         int ret;
340
341         ret = dns_udp_request_recv(subreq, state,
342                                    &in_blob.data, &in_blob.length);
343         TALLOC_FREE(subreq);
344
345         if (ret != 0) {
346                 tevent_req_werror(req, unix_to_werror(ret));
347                 return;
348         }
349
350         ndr_err = ndr_pull_struct_blob(
351                 &in_blob, state, &state->in_packet,
352                 (ndr_pull_flags_fn_t)ndr_pull_dns_name_packet);
353         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
354                 tevent_req_werror(req, DNS_ERR(SERVER_FAILURE));
355                 return;
356         }
357         if (state->in_packet.id != state->id) {
358                 tevent_req_werror(req, DNS_ERR(NAME_ERROR));
359                 return;
360         }
361         tevent_req_done(req);
362 }
363
364 static WERROR ask_forwarder_recv(
365         struct tevent_req *req, TALLOC_CTX *mem_ctx,
366         struct dns_res_rec **answers, uint16_t *ancount,
367         struct dns_res_rec **nsrecs, uint16_t *nscount,
368         struct dns_res_rec **additional, uint16_t *arcount)
369 {
370         struct ask_forwarder_state *state = tevent_req_data(
371                 req, struct ask_forwarder_state);
372         struct dns_name_packet *in_packet = &state->in_packet;
373         WERROR err;
374
375         if (tevent_req_is_werror(req, &err)) {
376                 return err;
377         }
378
379         *ancount = in_packet->ancount;
380         *answers = talloc_move(mem_ctx, &in_packet->answers);
381
382         *nscount = in_packet->nscount;
383         *nsrecs = talloc_move(mem_ctx, &in_packet->nsrecs);
384
385         *arcount = in_packet->arcount;
386         *additional = talloc_move(mem_ctx, &in_packet->additional);
387
388         return WERR_OK;
389 }
390
391 static WERROR add_zone_authority_record(struct dns_server *dns,
392                                         TALLOC_CTX *mem_ctx,
393                                         const struct dns_name_question *question,
394                                         struct dns_res_rec **nsrecs)
395 {
396         const char *zone = NULL;
397         struct dnsp_DnssrvRpcRecord *recs;
398         struct dns_res_rec *ns = *nsrecs;
399         uint16_t rec_count;
400         struct ldb_dn *dn = NULL;
401         unsigned int ri;
402         WERROR werror;
403
404         zone = dns_get_authoritative_zone(dns, question->name);
405         DEBUG(10, ("Creating zone authority record for '%s'\n", zone));
406
407         werror = dns_name2dn(dns, mem_ctx, zone, &dn);
408         if (!W_ERROR_IS_OK(werror)) {
409                 return werror;
410         }
411
412         werror = dns_lookup_records(dns, mem_ctx, dn, &recs, &rec_count);
413         if (!W_ERROR_IS_OK(werror)) {
414                 return werror;
415         }
416
417         for (ri = 0; ri < rec_count; ri++) {
418                 if (recs[ri].wType == DNS_TYPE_SOA) {
419                         werror = add_response_rr(zone, &recs[ri], &ns);
420                         if (!W_ERROR_IS_OK(werror)) {
421                                 return werror;
422                         }
423                 }
424         }
425
426         *nsrecs = ns;
427
428         return WERR_OK;
429 }
430
431 static struct tevent_req *handle_authoritative_send(
432         TALLOC_CTX *mem_ctx, struct tevent_context *ev,
433         struct dns_server *dns, const char *forwarder,
434         struct dns_name_question *question,
435         struct dns_res_rec **answers, struct dns_res_rec **nsrecs);
436 static WERROR handle_authoritative_recv(struct tevent_req *req);
437
438 struct handle_dnsrpcrec_state {
439         struct dns_res_rec **answers;
440         struct dns_res_rec **nsrecs;
441 };
442
443 static void handle_dnsrpcrec_gotauth(struct tevent_req *subreq);
444 static void handle_dnsrpcrec_gotforwarded(struct tevent_req *subreq);
445
446 static struct tevent_req *handle_dnsrpcrec_send(
447         TALLOC_CTX *mem_ctx, struct tevent_context *ev,
448         struct dns_server *dns, const char *forwarder,
449         const struct dns_name_question *question,
450         struct dnsp_DnssrvRpcRecord *rec,
451         struct dns_res_rec **answers, struct dns_res_rec **nsrecs)
452 {
453         struct tevent_req *req, *subreq;
454         struct handle_dnsrpcrec_state *state;
455         struct dns_name_question *new_q;
456         bool resolve_cname;
457         WERROR werr;
458
459         req = tevent_req_create(mem_ctx, &state,
460                                 struct handle_dnsrpcrec_state);
461         if (req == NULL) {
462                 return NULL;
463         }
464         state->answers = answers;
465         state->nsrecs = nsrecs;
466
467         resolve_cname = ((rec->wType == DNS_TYPE_CNAME) &&
468                          ((question->question_type == DNS_QTYPE_A) ||
469                           (question->question_type == DNS_QTYPE_AAAA)));
470
471         if (!resolve_cname) {
472                 if ((question->question_type != DNS_QTYPE_ALL) &&
473                     (rec->wType !=
474                      (enum dns_record_type) question->question_type)) {
475                         tevent_req_done(req);
476                         return tevent_req_post(req, ev);
477                 }
478
479                 werr = add_response_rr(question->name, rec, state->answers);
480                 if (tevent_req_werror(req, werr)) {
481                         return tevent_req_post(req, ev);
482                 }
483
484                 tevent_req_done(req);
485                 return tevent_req_post(req, ev);
486         }
487
488         werr = add_response_rr(question->name, rec, state->answers);
489         if (tevent_req_werror(req, werr)) {
490                 return tevent_req_post(req, ev);
491         }
492
493         new_q = talloc(state, struct dns_name_question);
494         if (tevent_req_nomem(new_q, req)) {
495                 return tevent_req_post(req, ev);
496         }
497
498         *new_q = (struct dns_name_question) {
499                 .question_type = question->question_type,
500                 .question_class = question->question_class,
501                 .name = rec->data.cname
502         };
503
504         if (dns_authorative_for_zone(dns, new_q->name)) {
505                 subreq = handle_authoritative_send(
506                         state, ev, dns, forwarder, new_q,
507                         state->answers, state->nsrecs);
508                 if (tevent_req_nomem(subreq, req)) {
509                         return tevent_req_post(req, ev);
510                 }
511                 tevent_req_set_callback(subreq, handle_dnsrpcrec_gotauth, req);
512                 return req;
513         }
514
515         subreq = ask_forwarder_send(state, ev, dns, forwarder, new_q);
516         if (tevent_req_nomem(subreq, req)) {
517                 return tevent_req_post(req, ev);
518         }
519         tevent_req_set_callback(subreq, handle_dnsrpcrec_gotforwarded, req);
520
521         return req;
522 }
523
524 static void handle_dnsrpcrec_gotauth(struct tevent_req *subreq)
525 {
526         struct tevent_req *req = tevent_req_callback_data(
527                 subreq, struct tevent_req);
528         WERROR werr;
529
530         werr = handle_authoritative_recv(subreq);
531         TALLOC_FREE(subreq);
532         if (tevent_req_werror(req, werr)) {
533                 return;
534         }
535         tevent_req_done(req);
536 }
537
538 static void handle_dnsrpcrec_gotforwarded(struct tevent_req *subreq)
539 {
540         struct tevent_req *req = tevent_req_callback_data(
541                 subreq, struct tevent_req);
542         struct handle_dnsrpcrec_state *state = tevent_req_data(
543                 req, struct handle_dnsrpcrec_state);
544         struct dns_res_rec *answers, *nsrecs, *additional;
545         uint16_t ancount = 0;
546         uint16_t nscount = 0;
547         uint16_t arcount = 0;
548         uint16_t i;
549         WERROR werr;
550
551         werr = ask_forwarder_recv(subreq, state, &answers, &ancount,
552                                   &nsrecs, &nscount, &additional, &arcount);
553         if (tevent_req_werror(req, werr)) {
554                 return;
555         }
556
557         for (i=0; i<ancount; i++) {
558                 werr = add_dns_res_rec(state->answers, &answers[i]);
559                 if (tevent_req_werror(req, werr)) {
560                         return;
561                 }
562         }
563
564         for (i=0; i<nscount; i++) {
565                 werr = add_dns_res_rec(state->nsrecs, &nsrecs[i]);
566                 if (tevent_req_werror(req, werr)) {
567                         return;
568                 }
569         }
570
571         tevent_req_done(req);
572 }
573
574 static WERROR handle_dnsrpcrec_recv(struct tevent_req *req)
575 {
576         return tevent_req_simple_recv_werror(req);
577 }
578
579 struct handle_authoritative_state {
580         struct tevent_context *ev;
581         struct dns_server *dns;
582         struct dns_name_question *question;
583         const char *forwarder;
584
585         struct dnsp_DnssrvRpcRecord *recs;
586         uint16_t rec_count;
587         uint16_t recs_done;
588
589         struct dns_res_rec **answers;
590         struct dns_res_rec **nsrecs;
591 };
592
593 static void handle_authoritative_done(struct tevent_req *subreq);
594
595 static struct tevent_req *handle_authoritative_send(
596         TALLOC_CTX *mem_ctx, struct tevent_context *ev,
597         struct dns_server *dns, const char *forwarder,
598         struct dns_name_question *question,
599         struct dns_res_rec **answers, struct dns_res_rec **nsrecs)
600 {
601         struct tevent_req *req, *subreq;
602         struct handle_authoritative_state *state;
603         struct ldb_dn *dn = NULL;
604         WERROR werr;
605
606         req = tevent_req_create(mem_ctx, &state,
607                                 struct handle_authoritative_state);
608         if (req == NULL) {
609                 return NULL;
610         }
611         state->ev = ev;
612         state->dns = dns;
613         state->question = question;
614         state->forwarder = forwarder;
615         state->answers = answers;
616         state->nsrecs = nsrecs;
617
618         werr = dns_name2dn(dns, state, question->name, &dn);
619         if (tevent_req_werror(req, werr)) {
620                 return tevent_req_post(req, ev);
621         }
622
623         werr = dns_lookup_records(dns, state, dn, &state->recs,
624                                   &state->rec_count);
625         TALLOC_FREE(dn);
626         if (tevent_req_werror(req, werr)) {
627                 return tevent_req_post(req, ev);
628         }
629
630         if (state->rec_count == 0) {
631                 tevent_req_werror(req, DNS_ERR(NAME_ERROR));
632                 return tevent_req_post(req, ev);
633         }
634
635         subreq = handle_dnsrpcrec_send(
636                 state, state->ev, state->dns, state->forwarder,
637                 state->question, &state->recs[state->recs_done],
638                 state->answers, state->nsrecs);
639         if (tevent_req_nomem(subreq, req)) {
640                 return tevent_req_post(req, ev);
641         }
642         tevent_req_set_callback(subreq, handle_authoritative_done, req);
643         return req;
644 }
645
646 static void handle_authoritative_done(struct tevent_req *subreq)
647 {
648         struct tevent_req *req = tevent_req_callback_data(
649                 subreq, struct tevent_req);
650         struct handle_authoritative_state *state = tevent_req_data(
651                 req, struct handle_authoritative_state);
652         WERROR werr;
653
654         werr = handle_dnsrpcrec_recv(subreq);
655         TALLOC_FREE(subreq);
656         if (tevent_req_werror(req, werr)) {
657                 return;
658         }
659
660         state->recs_done += 1;
661
662         if (state->recs_done == state->rec_count) {
663                 tevent_req_done(req);
664                 return;
665         }
666
667         subreq = handle_dnsrpcrec_send(
668                 state, state->ev, state->dns, state->forwarder,
669                 state->question, &state->recs[state->recs_done],
670                 state->answers, state->nsrecs);
671         if (tevent_req_nomem(subreq, req)) {
672                 return;
673         }
674         tevent_req_set_callback(subreq, handle_authoritative_done, req);
675 }
676
677 static WERROR handle_authoritative_recv(struct tevent_req *req)
678 {
679         struct handle_authoritative_state *state = tevent_req_data(
680                 req, struct handle_authoritative_state);
681         WERROR werr;
682
683         if (tevent_req_is_werror(req, &werr)) {
684                 return werr;
685         }
686
687         werr = add_zone_authority_record(state->dns, state, state->question,
688                                          state->nsrecs);
689         if (!W_ERROR_IS_OK(werr)) {
690                 return werr;
691         }
692
693         return WERR_OK;
694 }
695
696 static NTSTATUS create_tkey(struct dns_server *dns,
697                             const char* name,
698                             const char* algorithm,
699                             struct dns_server_tkey **tkey)
700 {
701         NTSTATUS status;
702         struct dns_server_tkey_store *store = dns->tkeys;
703         struct dns_server_tkey *k = talloc_zero(store, struct dns_server_tkey);
704
705         if (k == NULL) {
706                 return NT_STATUS_NO_MEMORY;
707         }
708
709         k->name = talloc_strdup(k, name);
710
711         if (k->name  == NULL) {
712                 return NT_STATUS_NO_MEMORY;
713         }
714
715         k->algorithm = talloc_strdup(k, algorithm);
716         if (k->algorithm == NULL) {
717                 return NT_STATUS_NO_MEMORY;
718         }
719
720         status = samba_server_gensec_start(k,
721                                            dns->task->event_ctx,
722                                            dns->task->msg_ctx,
723                                            dns->task->lp_ctx,
724                                            dns->server_credentials,
725                                            "dns",
726                                            &k->gensec);
727         if (!NT_STATUS_IS_OK(status)) {
728                 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
729                 *tkey = NULL;
730                 return status;
731         }
732
733         gensec_want_feature(k->gensec, GENSEC_FEATURE_SIGN);
734
735         status = gensec_start_mech_by_oid(k->gensec, GENSEC_OID_SPNEGO);
736
737         if (!NT_STATUS_IS_OK(status)) {
738                 DEBUG(1, ("Failed to start GENSEC server code: %s\n",
739                           nt_errstr(status)));
740                 *tkey = NULL;
741                 return status;
742         }
743
744         if (store->tkeys[store->next_idx] != NULL) {
745                 TALLOC_FREE(store->tkeys[store->next_idx]);
746         }
747
748         store->tkeys[store->next_idx] = k;
749         (store->next_idx)++;
750         store->next_idx %= store->size;
751
752         *tkey = k;
753         return NT_STATUS_OK;
754 }
755
756 static NTSTATUS accept_gss_ticket(TALLOC_CTX *mem_ctx,
757                                   struct dns_server *dns,
758                                   struct dns_server_tkey *tkey,
759                                   const DATA_BLOB *key,
760                                   DATA_BLOB *reply,
761                                   uint16_t *dns_auth_error)
762 {
763         NTSTATUS status;
764
765         status = gensec_update_ev(tkey->gensec, mem_ctx, dns->task->event_ctx,
766                                   *key, reply);
767
768         if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
769                 *dns_auth_error = DNS_RCODE_OK;
770                 return status;
771         }
772
773         if (NT_STATUS_IS_OK(status)) {
774
775                 status = gensec_session_info(tkey->gensec, tkey, &tkey->session_info);
776                 if (!NT_STATUS_IS_OK(status)) {
777                         *dns_auth_error = DNS_RCODE_BADKEY;
778                         return status;
779                 }
780                 *dns_auth_error = DNS_RCODE_OK;
781         }
782
783         return status;
784 }
785
786 static WERROR handle_tkey(struct dns_server *dns,
787                           TALLOC_CTX *mem_ctx,
788                           const struct dns_name_packet *in,
789                           struct dns_request_state *state,
790                           struct dns_res_rec **answers,
791                           uint16_t *ancount)
792 {
793         struct dns_res_rec *in_tkey = NULL;
794         struct dns_res_rec *ret_tkey;
795         uint16_t i;
796
797         for (i = 0; i < in->arcount; i++) {
798                 if (in->additional[i].rr_type == DNS_QTYPE_TKEY) {
799                         in_tkey = &in->additional[i];
800                         break;
801                 }
802         }
803
804         /* If this is a TKEY query, it should have a TKEY RR.
805          * Behaviour is not really specified in RFC 2930 or RFC 3645, but
806          * FORMAT_ERROR seems to be what BIND uses .*/
807         if (in_tkey == NULL) {
808                 return DNS_ERR(FORMAT_ERROR);
809         }
810
811         ret_tkey = talloc_zero(mem_ctx, struct dns_res_rec);
812         if (ret_tkey == NULL) {
813                 return WERR_NOMEM;
814         }
815
816         ret_tkey->name = talloc_strdup(ret_tkey, in_tkey->name);
817         if (ret_tkey->name == NULL) {
818                 return WERR_NOMEM;
819         }
820
821         ret_tkey->rr_type = DNS_QTYPE_TKEY;
822         ret_tkey->rr_class = DNS_QCLASS_ANY;
823         ret_tkey->length = UINT16_MAX;
824
825         ret_tkey->rdata.tkey_record.algorithm = talloc_strdup(ret_tkey,
826                         in_tkey->rdata.tkey_record.algorithm);
827         if (ret_tkey->rdata.tkey_record.algorithm  == NULL) {
828                 return WERR_NOMEM;
829         }
830
831         ret_tkey->rdata.tkey_record.inception = in_tkey->rdata.tkey_record.inception;
832         ret_tkey->rdata.tkey_record.expiration = in_tkey->rdata.tkey_record.expiration;
833         ret_tkey->rdata.tkey_record.mode = in_tkey->rdata.tkey_record.mode;
834
835         switch (in_tkey->rdata.tkey_record.mode) {
836         case DNS_TKEY_MODE_DH:
837                 /* FIXME: According to RFC 2930, we MUST support this, but we don't.
838                  * Still, claim it's a bad key instead of a bad mode */
839                 ret_tkey->rdata.tkey_record.error = DNS_RCODE_BADKEY;
840                 break;
841         case DNS_TKEY_MODE_GSSAPI: {
842                 NTSTATUS status;
843                 struct dns_server_tkey *tkey;
844                 DATA_BLOB key;
845                 DATA_BLOB reply;
846
847                 tkey = dns_find_tkey(dns->tkeys, in->questions[0].name);
848                 if (tkey != NULL && tkey->complete) {
849                         /* TODO: check if the key is still valid */
850                         DEBUG(1, ("Rejecting tkey negotiation for already established key\n"));
851                         ret_tkey->rdata.tkey_record.error = DNS_RCODE_BADNAME;
852                         break;
853                 }
854
855                 if (tkey == NULL) {
856                         status  = create_tkey(dns, in->questions[0].name,
857                                               in_tkey->rdata.tkey_record.algorithm,
858                                               &tkey);
859                         if (!NT_STATUS_IS_OK(status)) {
860                                 ret_tkey->rdata.tkey_record.error = DNS_RCODE_BADKEY;
861                                 return ntstatus_to_werror(status);
862                         }
863                 }
864
865                 key.data = in_tkey->rdata.tkey_record.key_data;
866                 key.length = in_tkey->rdata.tkey_record.key_size;
867
868                 status = accept_gss_ticket(ret_tkey, dns, tkey, &key, &reply,
869                                            &ret_tkey->rdata.tkey_record.error);
870                 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
871                         DEBUG(1, ("More processing required\n"));
872                         ret_tkey->rdata.tkey_record.error = DNS_RCODE_BADKEY;
873                 } else if (NT_STATUS_IS_OK(status)) {
874                         DEBUG(1, ("Tkey handshake completed\n"));
875                         ret_tkey->rdata.tkey_record.key_size = reply.length;
876                         ret_tkey->rdata.tkey_record.key_data = talloc_memdup(ret_tkey,
877                                                                 reply.data,
878                                                                 reply.length);
879                         state->sign = true;
880                         state->key_name = talloc_strdup(state->mem_ctx, tkey->name);
881                         if (state->key_name == NULL) {
882                                 return WERR_NOMEM;
883                         }
884                 } else {
885                         DEBUG(1, ("GSS key negotiation returned %s\n", nt_errstr(status)));
886                         ret_tkey->rdata.tkey_record.error = DNS_RCODE_BADKEY;
887                 }
888
889                 break;
890                 }
891         case DNS_TKEY_MODE_DELETE:
892                 /* TODO: implement me */
893                 DEBUG(1, ("Should delete tkey here\n"));
894                 ret_tkey->rdata.tkey_record.error = DNS_RCODE_OK;
895                 break;
896         case DNS_TKEY_MODE_NULL:
897         case DNS_TKEY_MODE_SERVER:
898         case DNS_TKEY_MODE_CLIENT:
899         case DNS_TKEY_MODE_LAST:
900                 /* We don't have to implement these, return a mode error */
901                 ret_tkey->rdata.tkey_record.error = DNS_RCODE_BADMODE;
902                 break;
903         default:
904                 DEBUG(1, ("Unsupported TKEY mode %d\n",
905                       in_tkey->rdata.tkey_record.mode));
906         }
907
908         *answers = ret_tkey;
909         *ancount = 1;
910
911         return WERR_OK;
912 }
913
914 struct dns_server_process_query_state {
915         struct dns_res_rec *answers;
916         uint16_t ancount;
917         struct dns_res_rec *nsrecs;
918         uint16_t nscount;
919         struct dns_res_rec *additional;
920         uint16_t arcount;
921 };
922
923 static void dns_server_process_query_got_auth(struct tevent_req *subreq);
924 static void dns_server_process_query_got_response(struct tevent_req *subreq);
925
926 struct tevent_req *dns_server_process_query_send(
927         TALLOC_CTX *mem_ctx, struct tevent_context *ev,
928         struct dns_server *dns, struct dns_request_state *req_state,
929         const struct dns_name_packet *in)
930 {
931         struct tevent_req *req, *subreq;
932         struct dns_server_process_query_state *state;
933
934         req = tevent_req_create(mem_ctx, &state,
935                                 struct dns_server_process_query_state);
936         if (req == NULL) {
937                 return NULL;
938         }
939         if (in->qdcount != 1) {
940                 tevent_req_werror(req, DNS_ERR(FORMAT_ERROR));
941                 return tevent_req_post(req, ev);
942         }
943
944         /* Windows returns NOT_IMPLEMENTED on this as well */
945         if (in->questions[0].question_class == DNS_QCLASS_NONE) {
946                 tevent_req_werror(req, DNS_ERR(NOT_IMPLEMENTED));
947                 return tevent_req_post(req, ev);
948         }
949
950         if (in->questions[0].question_type == DNS_QTYPE_TKEY) {
951                 WERROR err;
952
953                 err = handle_tkey(dns, state, in, req_state,
954                                   &state->answers, &state->ancount);
955                 if (tevent_req_werror(req, err)) {
956                         return tevent_req_post(req, ev);
957                 }
958                 tevent_req_done(req);
959                 return tevent_req_post(req, ev);
960         }
961
962         if (dns_authorative_for_zone(dns, in->questions[0].name)) {
963
964                 req_state->flags |= DNS_FLAG_AUTHORITATIVE;
965
966                 /*
967                  * Initialize the response arrays, so that we can use
968                  * them as their own talloc contexts when doing the
969                  * realloc
970                  */
971                 state->answers = talloc_array(state, struct dns_res_rec, 0);
972                 if (tevent_req_nomem(state->answers, req)) {
973                         return tevent_req_post(req, ev);
974                 }
975                 state->nsrecs = talloc_array(state, struct dns_res_rec, 0);
976                 if (tevent_req_nomem(state->nsrecs, req)) {
977                         return tevent_req_post(req, ev);
978                 }
979
980                 subreq = handle_authoritative_send(
981                         state, ev, dns, lpcfg_dns_forwarder(dns->task->lp_ctx),
982                         &in->questions[0], &state->answers, &state->nsrecs);
983                 if (tevent_req_nomem(subreq, req)) {
984                         return tevent_req_post(req, ev);
985                 }
986                 tevent_req_set_callback(
987                         subreq, dns_server_process_query_got_auth, req);
988                 return req;
989         }
990
991         if ((req_state->flags & DNS_FLAG_RECURSION_DESIRED) &&
992             (req_state->flags & DNS_FLAG_RECURSION_AVAIL)) {
993                 DEBUG(2, ("Not authoritative for '%s', forwarding\n",
994                           in->questions[0].name));
995
996                 subreq = ask_forwarder_send(
997                         state, ev, dns, lpcfg_dns_forwarder(dns->task->lp_ctx),
998                         &in->questions[0]);
999                 if (tevent_req_nomem(subreq, req)) {
1000                         return tevent_req_post(req, ev);
1001                 }
1002                 tevent_req_set_callback(
1003                         subreq, dns_server_process_query_got_response, req);
1004                 return req;
1005         }
1006
1007         tevent_req_werror(req, DNS_ERR(NAME_ERROR));
1008         return tevent_req_post(req, ev);
1009 }
1010
1011 static void dns_server_process_query_got_response(struct tevent_req *subreq)
1012 {
1013         struct tevent_req *req = tevent_req_callback_data(
1014                 subreq, struct tevent_req);
1015         struct dns_server_process_query_state *state = tevent_req_data(
1016                 req, struct dns_server_process_query_state);
1017         WERROR err;
1018
1019         err = ask_forwarder_recv(subreq, state,
1020                                  &state->answers, &state->ancount,
1021                                  &state->nsrecs, &state->nscount,
1022                                  &state->additional, &state->arcount);
1023         TALLOC_FREE(subreq);
1024         if (tevent_req_werror(req, err)) {
1025                 return;
1026         }
1027         tevent_req_done(req);
1028 }
1029
1030 static void dns_server_process_query_got_auth(struct tevent_req *subreq)
1031 {
1032         struct tevent_req *req = tevent_req_callback_data(
1033                 subreq, struct tevent_req);
1034         struct dns_server_process_query_state *state = tevent_req_data(
1035                 req, struct dns_server_process_query_state);
1036         WERROR werr;
1037
1038         werr = handle_authoritative_recv(subreq);
1039         TALLOC_FREE(subreq);
1040         if (tevent_req_werror(req, werr)) {
1041                 return;
1042         }
1043         state->ancount = talloc_array_length(state->answers);
1044         state->nscount = talloc_array_length(state->nsrecs);
1045         state->arcount = talloc_array_length(state->additional);
1046
1047         tevent_req_done(req);
1048 }
1049
1050 WERROR dns_server_process_query_recv(
1051         struct tevent_req *req, TALLOC_CTX *mem_ctx,
1052         struct dns_res_rec **answers,    uint16_t *ancount,
1053         struct dns_res_rec **nsrecs,     uint16_t *nscount,
1054         struct dns_res_rec **additional, uint16_t *arcount)
1055 {
1056         struct dns_server_process_query_state *state = tevent_req_data(
1057                 req, struct dns_server_process_query_state);
1058         WERROR err = WERR_OK;
1059
1060         if (tevent_req_is_werror(req, &err)) {
1061
1062                 if ((!W_ERROR_EQUAL(err, DNS_ERR(NAME_ERROR))) &&
1063                     (!W_ERROR_EQUAL(err, WERR_DNS_ERROR_NAME_DOES_NOT_EXIST))) {
1064                         return err;
1065                 }
1066         }
1067         *answers = talloc_move(mem_ctx, &state->answers);
1068         *ancount = state->ancount;
1069         *nsrecs = talloc_move(mem_ctx, &state->nsrecs);
1070         *nscount = state->nscount;
1071         *additional = talloc_move(mem_ctx, &state->additional);
1072         *arcount = state->arcount;
1073         return err;
1074 }