s4 dns: Revert erroneous push from wrong branch
[garming/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 #include "auth/auth.h"
37 #include "auth/credentials/credentials.h"
38 #include "auth/gensec/gensec.h"
39
40 static WERROR create_response_rr(const struct dns_name_question *question,
41                                  const struct dnsp_DnssrvRpcRecord *rec,
42                                  struct dns_res_rec **answers, uint16_t *ancount)
43 {
44         struct dns_res_rec *ans = *answers;
45         uint16_t ai = *ancount;
46         char *tmp;
47         uint32_t i;
48
49         ZERO_STRUCT(ans[ai]);
50
51         switch (rec->wType) {
52         case DNS_QTYPE_CNAME:
53                 ans[ai].rdata.cname_record = talloc_strdup(ans, rec->data.cname);
54                 W_ERROR_HAVE_NO_MEMORY(ans[ai].rdata.cname_record);
55                 break;
56         case DNS_QTYPE_A:
57                 ans[ai].rdata.ipv4_record = talloc_strdup(ans, rec->data.ipv4);
58                 W_ERROR_HAVE_NO_MEMORY(ans[ai].rdata.ipv4_record);
59                 break;
60         case DNS_QTYPE_AAAA:
61                 ans[ai].rdata.ipv6_record = talloc_strdup(ans, rec->data.ipv6);
62                 W_ERROR_HAVE_NO_MEMORY(ans[ai].rdata.ipv6_record);
63                 break;
64         case DNS_TYPE_NS:
65                 ans[ai].rdata.ns_record = talloc_strdup(ans, rec->data.ns);
66                 W_ERROR_HAVE_NO_MEMORY(ans[ai].rdata.ns_record);
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                 W_ERROR_HAVE_NO_MEMORY(ans[ai].rdata.srv_record.target);
75                 break;
76         case DNS_QTYPE_SOA:
77                 ans[ai].rdata.soa_record.mname   = talloc_strdup(
78                         ans, rec->data.soa.mname);
79                 W_ERROR_HAVE_NO_MEMORY(ans[ai].rdata.soa_record.mname);
80                 ans[ai].rdata.soa_record.rname   = talloc_strdup(
81                         ans, rec->data.soa.rname);
82                 W_ERROR_HAVE_NO_MEMORY(ans[ai].rdata.soa_record.rname);
83                 ans[ai].rdata.soa_record.serial  = rec->data.soa.serial;
84                 ans[ai].rdata.soa_record.refresh = rec->data.soa.refresh;
85                 ans[ai].rdata.soa_record.retry   = rec->data.soa.retry;
86                 ans[ai].rdata.soa_record.expire  = rec->data.soa.expire;
87                 ans[ai].rdata.soa_record.minimum = rec->data.soa.minimum;
88                 break;
89         case DNS_QTYPE_PTR:
90                 ans[ai].rdata.ptr_record = talloc_strdup(ans, rec->data.ptr);
91                 break;
92         case DNS_QTYPE_TXT:
93                 tmp = talloc_asprintf(ans, "\"%s\"", rec->data.txt.str[0]);
94                 W_ERROR_HAVE_NO_MEMORY(tmp);
95                 for (i=1; i<rec->data.txt.count; i++) {
96                         tmp = talloc_asprintf_append_buffer(
97                                 tmp, " \"%s\"", rec->data.txt.str[i]);
98                         W_ERROR_HAVE_NO_MEMORY(tmp);
99                 }
100                 ans[ai].rdata.txt_record.txt = tmp;
101                 break;
102         default:
103                 DEBUG(0, ("Got unhandled type %u query.\n", rec->wType));
104                 return DNS_ERR(NOT_IMPLEMENTED);
105         }
106
107         ans[ai].name = talloc_strdup(ans, question->name);
108         W_ERROR_HAVE_NO_MEMORY(ans[ai].name);
109         ans[ai].rr_type = rec->wType;
110         ans[ai].rr_class = DNS_QCLASS_IN;
111         ans[ai].ttl = rec->dwTtlSeconds;
112         ans[ai].length = UINT16_MAX;
113         ai++;
114
115         *answers = ans;
116         *ancount = ai;
117
118         return WERR_OK;
119 }
120
121 struct ask_forwarder_state {
122         struct tevent_context *ev;
123         uint16_t id;
124         struct dns_name_packet in_packet;
125 };
126
127 static void ask_forwarder_done(struct tevent_req *subreq);
128
129 static struct tevent_req *ask_forwarder_send(
130         TALLOC_CTX *mem_ctx, struct tevent_context *ev,
131         const char *forwarder, struct dns_name_question *question)
132 {
133         struct tevent_req *req, *subreq;
134         struct ask_forwarder_state *state;
135         struct dns_name_packet out_packet = { 0, };
136         DATA_BLOB out_blob;
137         enum ndr_err_code ndr_err;
138
139         req = tevent_req_create(mem_ctx, &state, struct ask_forwarder_state);
140         if (req == NULL) {
141                 return NULL;
142         }
143         state->ev = ev;
144         generate_random_buffer((uint8_t *)&state->id, sizeof(state->id));
145
146         if (!is_ipaddress(forwarder)) {
147                 DEBUG(0, ("Invalid 'dns forwarder' setting '%s', needs to be "
148                           "an IP address\n", forwarder));
149                 tevent_req_werror(req, DNS_ERR(NAME_ERROR));
150                 return tevent_req_post(req, ev);
151         }
152
153         out_packet.id = state->id;
154         out_packet.operation |= DNS_OPCODE_QUERY | DNS_FLAG_RECURSION_DESIRED;
155         out_packet.qdcount = 1;
156         out_packet.questions = question;
157
158         ndr_err = ndr_push_struct_blob(
159                 &out_blob, state, &out_packet,
160                 (ndr_push_flags_fn_t)ndr_push_dns_name_packet);
161         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
162                 tevent_req_werror(req, DNS_ERR(SERVER_FAILURE));
163                 return tevent_req_post(req, ev);
164         }
165         subreq = dns_udp_request_send(state, ev, forwarder, out_blob.data,
166                                       out_blob.length);
167         if (tevent_req_nomem(subreq, req)) {
168                 return tevent_req_post(req, ev);
169         }
170         tevent_req_set_callback(subreq, ask_forwarder_done, req);
171         return req;
172 }
173
174 static void ask_forwarder_done(struct tevent_req *subreq)
175 {
176         struct tevent_req *req = tevent_req_callback_data(
177                 subreq, struct tevent_req);
178         struct ask_forwarder_state *state = tevent_req_data(
179                 req, struct ask_forwarder_state);
180         DATA_BLOB in_blob;
181         enum ndr_err_code ndr_err;
182         WERROR ret;
183
184         ret = dns_udp_request_recv(subreq, state,
185                                    &in_blob.data, &in_blob.length);
186         TALLOC_FREE(subreq);
187         if (tevent_req_werror(req, ret)) {
188                 return;
189         }
190
191         ndr_err = ndr_pull_struct_blob(
192                 &in_blob, state, &state->in_packet,
193                 (ndr_pull_flags_fn_t)ndr_pull_dns_name_packet);
194         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
195                 tevent_req_werror(req, DNS_ERR(SERVER_FAILURE));
196                 return;
197         }
198         if (state->in_packet.id != state->id) {
199                 tevent_req_werror(req, DNS_ERR(NAME_ERROR));
200                 return;
201         }
202         tevent_req_done(req);
203 }
204
205 static WERROR ask_forwarder_recv(
206         struct tevent_req *req, TALLOC_CTX *mem_ctx,
207         struct dns_res_rec **answers, uint16_t *ancount,
208         struct dns_res_rec **nsrecs, uint16_t *nscount,
209         struct dns_res_rec **additional, uint16_t *arcount)
210 {
211         struct ask_forwarder_state *state = tevent_req_data(
212                 req, struct ask_forwarder_state);
213         struct dns_name_packet *in_packet = &state->in_packet;
214         WERROR err;
215
216         if (tevent_req_is_werror(req, &err)) {
217                 return err;
218         }
219
220         *ancount = in_packet->ancount;
221         *answers = talloc_move(mem_ctx, &in_packet->answers);
222
223         *nscount = in_packet->nscount;
224         *nsrecs = talloc_move(mem_ctx, &in_packet->nsrecs);
225
226         *arcount = in_packet->arcount;
227         *additional = talloc_move(mem_ctx, &in_packet->additional);
228
229         return WERR_OK;
230 }
231
232 static WERROR handle_question(struct dns_server *dns,
233                               TALLOC_CTX *mem_ctx,
234                               const struct dns_name_question *question,
235                               struct dns_res_rec **answers, uint16_t *ancount)
236 {
237         struct dns_res_rec *ans = *answers;
238         WERROR werror;
239         unsigned int ri;
240         struct dnsp_DnssrvRpcRecord *recs;
241         uint16_t rec_count, ai = *ancount;
242         struct ldb_dn *dn = NULL;
243
244         werror = dns_name2dn(dns, mem_ctx, question->name, &dn);
245         W_ERROR_NOT_OK_RETURN(werror);
246
247         werror = dns_lookup_records(dns, mem_ctx, dn, &recs, &rec_count);
248         W_ERROR_NOT_OK_RETURN(werror);
249
250         ans = talloc_realloc(mem_ctx, ans, struct dns_res_rec, rec_count + ai);
251         if (ans == NULL) {
252                 return WERR_NOMEM;
253         }
254
255         for (ri = 0; ri < rec_count; ri++) {
256                 if ((recs[ri].wType == DNS_TYPE_CNAME) &&
257                     ((question->question_type == DNS_QTYPE_A) ||
258                      (question->question_type == DNS_QTYPE_AAAA))) {
259                         struct dns_name_question *new_q =
260                                 talloc(mem_ctx, struct dns_name_question);
261
262                         if (new_q == NULL) {
263                                 return WERR_NOMEM;
264                         }
265
266                         /* We reply with one more record, so grow the array */
267                         ans = talloc_realloc(mem_ctx, ans, struct dns_res_rec,
268                                              rec_count + 1);
269                         if (ans == NULL) {
270                                 TALLOC_FREE(new_q);
271                                 return WERR_NOMEM;
272                         }
273
274                         /* First put in the CNAME record */
275                         werror = create_response_rr(question, &recs[ri], &ans, &ai);
276                         if (!W_ERROR_IS_OK(werror)) {
277                                 return werror;
278                         }
279
280                         /* And then look up the name it points at.. */
281
282                         /* First build up the new question */
283                         new_q->question_type = question->question_type;
284                         new_q->question_class = question->question_class;
285                         if (new_q->question_type == DNS_QTYPE_A) {
286                                 new_q->name = talloc_strdup(new_q, recs[ri].data.ipv4);
287                         } else if (new_q->question_type == DNS_QTYPE_AAAA) {
288                                 new_q->name = talloc_strdup(new_q, recs[ri].data.ipv6);
289                         }
290                         if (new_q->name == NULL) {
291                                 TALLOC_FREE(new_q);
292                                 return WERR_NOMEM;
293                         }
294                         /* and then call the lookup again */
295                         werror = handle_question(dns, mem_ctx, new_q, &ans, &ai);
296                         if (!W_ERROR_IS_OK(werror)) {
297                                 return werror;
298                         }
299
300
301                         continue;
302                 }
303                 if ((question->question_type != DNS_QTYPE_ALL) &&
304                     (recs[ri].wType != question->question_type)) {
305                         continue;
306                 }
307                 werror = create_response_rr(question, &recs[ri], &ans, &ai);
308                 if (!W_ERROR_IS_OK(werror)) {
309                         return werror;
310                 }
311         }
312
313         if (ai == 0) {
314                 return DNS_ERR(NAME_ERROR);
315         }
316
317         *ancount = ai;
318         *answers = ans;
319
320         return WERR_OK;
321 }
322
323 static NTSTATUS accept_gss_ticket(TALLOC_CTX *mem_ctx,
324                                   struct dns_server *dns,
325                                   struct dns_server_tkey *tkey,
326                                   const DATA_BLOB *key,
327                                   DATA_BLOB *reply,
328                                   uint16_t *dns_auth_error)
329 {
330         NTSTATUS status;
331
332         status = gensec_update(tkey->gensec, mem_ctx, dns->task->event_ctx,
333                                *key, reply);
334
335         if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
336                 *dns_auth_error = DNS_RCODE_OK;
337                 return status;
338         }
339
340         if (NT_STATUS_IS_OK(status)) {
341
342                 status = gensec_session_info(tkey->gensec, tkey, &tkey->session_info);
343                 if (!NT_STATUS_IS_OK(status)) {
344                         *dns_auth_error = DNS_RCODE_BADKEY;
345                         return status;
346                 }
347                 *dns_auth_error = DNS_RCODE_OK;
348         }
349
350         return status;
351 }
352
353 static struct dns_server_tkey *find_tkey(struct dns_server_tkey_store *store,
354                                          const char *name)
355 {
356         struct dns_server_tkey *tkey = NULL;
357         uint16_t i = 0;
358
359         do {
360                 struct dns_server_tkey *tmp_key = store->tkeys[i];
361
362                 i++;
363                 i %= TKEY_BUFFER_SIZE;
364
365                 if (tmp_key == NULL) {
366                         continue;
367                 }
368                 if (dns_name_equal(name, tmp_key->name)) {
369                         tkey = tmp_key;
370                         break;
371                 }
372         } while (i != 0);
373
374         return tkey;
375 }
376
377 static NTSTATUS create_tkey(struct dns_server *dns,
378                             const char* name,
379                             struct dns_server_tkey **tkey)
380 {
381         NTSTATUS status;
382         struct dns_server_tkey_store *store = dns->tkeys;
383         struct dns_server_tkey *k = talloc_zero(store, struct dns_server_tkey);
384
385         if (k == NULL) {
386                 return NT_STATUS_NO_MEMORY;
387         }
388
389         k->name = talloc_strdup(k, name);
390
391         if (k->name  == NULL) {
392                 return NT_STATUS_NO_MEMORY;
393         }
394
395         status = samba_server_gensec_start(k,
396                                            dns->task->event_ctx,
397                                            dns->task->msg_ctx,
398                                            dns->task->lp_ctx,
399                                            dns->server_credentials,
400                                            "dns",
401                                            &k->gensec);
402         if (!NT_STATUS_IS_OK(status)) {
403                 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
404                 *tkey = NULL;
405                 return status;
406         }
407
408         gensec_want_feature(k->gensec, GENSEC_FEATURE_SIGN);
409
410         status = gensec_start_mech_by_oid(k->gensec, GENSEC_OID_SPNEGO);
411
412         if (!NT_STATUS_IS_OK(status)) {
413                 DEBUG(1, ("Failed to start GENSEC server code: %s\n",
414                           nt_errstr(status)));
415                 *tkey = NULL;
416                 return status;
417         }
418
419         if (store->tkeys[store->next_idx] != NULL) {
420                 TALLOC_FREE(store->tkeys[store->next_idx]);
421         }
422
423         store->tkeys[store->next_idx] = k;
424         (store->next_idx)++;
425         store->next_idx %= store->size;
426
427         *tkey = k;
428         return NT_STATUS_OK;
429 }
430
431 static WERROR handle_tkey(struct dns_server *dns,
432                           TALLOC_CTX *mem_ctx,
433                           const struct dns_name_packet *in,
434                           struct dns_res_rec **answers,
435                           uint16_t *ancount)
436 {
437         struct dns_res_rec *in_tkey = NULL;
438         struct dns_res_rec *ret_tkey;
439         uint16_t i;
440
441         for (i = 0; i < in->arcount; i++) {
442                 if (in->additional[i].rr_type == DNS_QTYPE_TKEY) {
443                         in_tkey = &in->additional[i];
444                         break;
445                 }
446         }
447
448         /* If this is a TKEY query, it should have a TKEY RR.
449          * Behaviour is not really specified in RFC 2930 or RFC 3645, but
450          * FORMAT_ERROR seems to be what BIND uses .*/
451         if (in_tkey == NULL) {
452                 return DNS_ERR(FORMAT_ERROR);
453         }
454
455         ret_tkey = talloc_zero(mem_ctx, struct dns_res_rec);
456         if (ret_tkey == NULL) {
457                 return WERR_NOMEM;
458         }
459
460         ret_tkey->name = talloc_strdup(ret_tkey, in_tkey->name);
461         if (ret_tkey->name == NULL) {
462                 return WERR_NOMEM;
463         }
464
465         ret_tkey->rr_type = DNS_QTYPE_TKEY;
466         ret_tkey->rr_class = DNS_QCLASS_ANY;
467         ret_tkey->length = UINT16_MAX;
468
469         ret_tkey->rdata.tkey_record.algorithm = talloc_strdup(ret_tkey, ret_tkey->name);
470         if (ret_tkey->rdata.tkey_record.algorithm  == NULL) {
471                 return WERR_NOMEM;
472         }
473
474         ret_tkey->rdata.tkey_record.inception = in_tkey->rdata.tkey_record.inception;
475         ret_tkey->rdata.tkey_record.expiration = in_tkey->rdata.tkey_record.expiration;
476         ret_tkey->rdata.tkey_record.mode = in_tkey->rdata.tkey_record.mode;
477
478         switch (in_tkey->rdata.tkey_record.mode) {
479         case DNS_TKEY_MODE_DH:
480                 /* FIXME: According to RFC 2930, we MUST support this, but we don't.
481                  * Still, claim it's a bad key instead of a bad mode */
482                 ret_tkey->rdata.tkey_record.error = DNS_RCODE_BADKEY;
483                 break;
484         case DNS_TKEY_MODE_GSSAPI: {
485                 NTSTATUS status;
486                 struct dns_server_tkey *tkey;
487                 DATA_BLOB key;
488                 DATA_BLOB reply;
489
490                 tkey = find_tkey(dns->tkeys, in->questions[0].name);
491                 if (tkey != NULL && tkey->complete) {
492                         /* TODO: check if the key is still valid */
493                         DEBUG(1, ("Rejecting tkey negotiation for already established key\n"));
494                         ret_tkey->rdata.tkey_record.error = DNS_RCODE_BADNAME;
495                         break;
496                 }
497
498                 if (tkey == NULL) {
499                         status  = create_tkey(dns, in->questions[0].name,
500                                               &tkey);
501                         if (!NT_STATUS_IS_OK(status)) {
502                                 ret_tkey->rdata.tkey_record.error = DNS_RCODE_BADKEY;
503                                 return ntstatus_to_werror(status);
504                         }
505                 }
506
507                 key.data = in_tkey->rdata.tkey_record.key_data;
508                 key.length = in_tkey->rdata.tkey_record.key_size;
509
510                 status = accept_gss_ticket(ret_tkey, dns, tkey, &key, &reply,
511                                            &ret_tkey->rdata.tkey_record.error);
512                 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
513                         DEBUG(1, ("More processing required\n"));
514                         ret_tkey->rdata.tkey_record.error = DNS_RCODE_BADKEY;
515                 } else if (NT_STATUS_IS_OK(status)) {
516                         DEBUG(1, ("Tkey handshake completed\n"));
517                 } else {
518                         DEBUG(0, ("GSS key negotiation returned %s\n", nt_errstr(status)));
519                         ret_tkey->rdata.tkey_record.error = DNS_RCODE_BADKEY;
520                 }
521
522                 break;
523                 }
524         case DNS_TKEY_MODE_DELETE:
525                 /* TODO: implement me */
526                 DEBUG(1, ("Should delete tkey here\n"));
527                 ret_tkey->rdata.tkey_record.error = DNS_RCODE_OK;
528                 break;
529         case DNS_TKEY_MODE_NULL:
530         case DNS_TKEY_MODE_SERVER:
531         case DNS_TKEY_MODE_CLIENT:
532         case DNS_TKEY_MODE_LAST:
533                 /* We don't have to implement these, return a mode error */
534                 ret_tkey->rdata.tkey_record.error = DNS_RCODE_BADMODE;
535                 break;
536         default:
537                 DEBUG(1, ("Unsupported TKEY mode %d\n",
538                       in_tkey->rdata.tkey_record.mode));
539         }
540
541         *answers = ret_tkey;
542         *ancount = 1;
543
544         return WERR_OK;
545 }
546
547 struct dns_server_process_query_state {
548         struct dns_res_rec *answers;
549         uint16_t ancount;
550         struct dns_res_rec *nsrecs;
551         uint16_t nscount;
552         struct dns_res_rec *additional;
553         uint16_t arcount;
554 };
555
556 static void dns_server_process_query_got_response(struct tevent_req *subreq);
557
558 struct tevent_req *dns_server_process_query_send(
559         TALLOC_CTX *mem_ctx, struct tevent_context *ev,
560         struct dns_server *dns, struct dns_request_state *req_state,
561         const struct dns_name_packet *in)
562 {
563         struct tevent_req *req, *subreq;
564         struct dns_server_process_query_state *state;
565
566         req = tevent_req_create(mem_ctx, &state,
567                                 struct dns_server_process_query_state);
568         if (req == NULL) {
569                 return NULL;
570         }
571         if (in->qdcount != 1) {
572                 tevent_req_werror(req, DNS_ERR(FORMAT_ERROR));
573                 return tevent_req_post(req, ev);
574         }
575
576         /* Windows returns NOT_IMPLEMENTED on this as well */
577         if (in->questions[0].question_class == DNS_QCLASS_NONE) {
578                 tevent_req_werror(req, DNS_ERR(NOT_IMPLEMENTED));
579                 return tevent_req_post(req, ev);
580         }
581
582         if (in->questions[0].question_type == DNS_QTYPE_TKEY) {
583                 WERROR err;
584
585                 err = handle_tkey(dns, state, in, &state->answers,
586                                   &state->ancount);
587                 if (tevent_req_werror(req, err)) {
588                         return tevent_req_post(req, ev);
589                 }
590                 tevent_req_done(req);
591                 return tevent_req_post(req, ev);
592         }
593
594         if (dns_authorative_for_zone(dns, in->questions[0].name)) {
595                 WERROR err;
596
597                 req_state->flags |= DNS_FLAG_AUTHORITATIVE;
598                 err = handle_question(dns, state, &in->questions[0],
599                                       &state->answers, &state->ancount);
600                 if (tevent_req_werror(req, err)) {
601                         return tevent_req_post(req, ev);
602                 }
603                 tevent_req_done(req);
604                 return tevent_req_post(req, ev);
605         }
606
607         if ((req_state->flags & DNS_FLAG_RECURSION_DESIRED) &&
608             (req_state->flags & DNS_FLAG_RECURSION_AVAIL)) {
609                 DEBUG(2, ("Not authoritative for '%s', forwarding\n",
610                           in->questions[0].name));
611
612                 subreq = ask_forwarder_send(
613                         state, ev, lpcfg_dns_forwarder(dns->task->lp_ctx),
614                         &in->questions[0]);
615                 if (tevent_req_nomem(subreq, req)) {
616                         return tevent_req_post(req, ev);
617                 }
618                 tevent_req_set_callback(
619                         subreq, dns_server_process_query_got_response, req);
620                 return req;
621         }
622
623         tevent_req_werror(req, DNS_ERR(NAME_ERROR));
624         return tevent_req_post(req, ev);
625 }
626
627 static void dns_server_process_query_got_response(struct tevent_req *subreq)
628 {
629         struct tevent_req *req = tevent_req_callback_data(
630                 subreq, struct tevent_req);
631         struct dns_server_process_query_state *state = tevent_req_data(
632                 req, struct dns_server_process_query_state);
633         WERROR err;
634
635         err = ask_forwarder_recv(subreq, state,
636                                  &state->answers, &state->ancount,
637                                  &state->nsrecs, &state->nscount,
638                                  &state->additional, &state->arcount);
639         TALLOC_FREE(subreq);
640         if (tevent_req_werror(req, err)) {
641                 return;
642         }
643         tevent_req_done(req);
644 }
645
646 WERROR dns_server_process_query_recv(
647         struct tevent_req *req, TALLOC_CTX *mem_ctx,
648         struct dns_res_rec **answers,    uint16_t *ancount,
649         struct dns_res_rec **nsrecs,     uint16_t *nscount,
650         struct dns_res_rec **additional, uint16_t *arcount)
651 {
652         struct dns_server_process_query_state *state = tevent_req_data(
653                 req, struct dns_server_process_query_state);
654         WERROR err;
655
656         if (tevent_req_is_werror(req, &err)) {
657                 return err;
658         }
659         *answers = talloc_move(mem_ctx, &state->answers);
660         *ancount = state->ancount;
661         *nsrecs = talloc_move(mem_ctx, &state->nsrecs);
662         *nscount = state->nscount;
663         *additional = talloc_move(mem_ctx, &state->additional);
664         *arcount = state->arcount;
665         return WERR_OK;
666 }