s4-dns: Make the TCP dns server async
[sfrench/samba-autobuild/.git] / source4 / dns_server / dns_server.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    DNS server startup
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 "smbd/service.h"
25 #include "smbd/service_stream.h"
26 #include "smbd/process_model.h"
27 #include "lib/events/events.h"
28 #include "lib/socket/socket.h"
29 #include "lib/tsocket/tsocket.h"
30 #include "libcli/util/tstream.h"
31 #include "libcli/util/ntstatus.h"
32 #include "system/network.h"
33 #include "lib/stream/packet.h"
34 #include "lib/socket/netif.h"
35 #include "dns_server/dns_server.h"
36 #include "param/param.h"
37 #include "librpc/ndr/libndr.h"
38 #include "librpc/gen_ndr/ndr_dns.h"
39 #include "librpc/gen_ndr/ndr_dnsp.h"
40 #include <ldb.h>
41 #include "dsdb/samdb/samdb.h"
42 #include "dsdb/common/util.h"
43 #include "auth/session.h"
44 #include "lib/util/dlinklist.h"
45 #include "lib/util/tevent_werror.h"
46
47 NTSTATUS server_service_dns_init(void);
48
49 /* hold information about one dns socket */
50 struct dns_socket {
51         struct dns_server *dns;
52         struct tsocket_address *local_address;
53 };
54
55 struct dns_udp_socket {
56         struct dns_socket *dns_socket;
57         struct tdgram_context *dgram;
58         struct tevent_queue *send_queue;
59 };
60
61 /*
62   state of an open tcp connection
63 */
64 struct dns_tcp_connection {
65         /* stream connection we belong to */
66         struct stream_connection *conn;
67
68         /* the dns_server the connection belongs to */
69         struct dns_socket *dns_socket;
70
71         struct tstream_context *tstream;
72
73         struct tevent_queue *send_queue;
74 };
75
76 static void dns_tcp_terminate_connection(struct dns_tcp_connection *dnsconn, const char *reason)
77 {
78         stream_terminate_connection(dnsconn->conn, reason);
79 }
80
81 static void dns_tcp_recv(struct stream_connection *conn, uint16_t flags)
82 {
83         struct dns_tcp_connection *dnsconn = talloc_get_type(conn->private_data,
84                                                              struct dns_tcp_connection);
85         /* this should never be triggered! */
86         dns_tcp_terminate_connection(dnsconn, "dns_tcp_recv: called");
87 }
88
89 static void dns_tcp_send(struct stream_connection *conn, uint16_t flags)
90 {
91         struct dns_tcp_connection *dnsconn = talloc_get_type(conn->private_data,
92                                                              struct dns_tcp_connection);
93         /* this should never be triggered! */
94         dns_tcp_terminate_connection(dnsconn, "dns_tcp_send: called");
95 }
96
97 struct dns_process_state {
98         DATA_BLOB *in;
99         struct dns_name_packet in_packet;
100         struct dns_request_state state;
101         uint16_t dns_err;
102         struct dns_name_packet out_packet;
103         DATA_BLOB out;
104 };
105
106 static void dns_process_done(struct tevent_req *subreq);
107
108 static struct tevent_req *dns_process_send(TALLOC_CTX *mem_ctx,
109                                            struct tevent_context *ev,
110                                            struct dns_server *dns,
111                                            DATA_BLOB *in)
112 {
113         struct tevent_req *req, *subreq;
114         struct dns_process_state *state;
115         enum ndr_err_code ndr_err;
116         WERROR ret;
117
118         req = tevent_req_create(mem_ctx, &state, struct dns_process_state);
119         if (req == NULL) {
120                 return NULL;
121         }
122         state->in = in;
123
124         if (in->length < 12) {
125                 tevent_req_werror(req, WERR_INVALID_PARAM);
126                 return tevent_req_post(req, ev);
127         }
128         dump_data(8, in->data, in->length);
129
130         ndr_err = ndr_pull_struct_blob(
131                 in, state, &state->in_packet,
132                 (ndr_pull_flags_fn_t)ndr_pull_dns_name_packet);
133
134         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
135                 state->dns_err = DNS_RCODE_FORMERR;
136                 tevent_req_done(req);
137                 return tevent_req_post(req, ev);
138         }
139         if (DEBUGLVL(8)) {
140                 NDR_PRINT_DEBUG(dns_name_packet, &state->in_packet);
141         }
142
143         state->state.flags = state->in_packet.operation;
144         state->state.flags |= DNS_FLAG_REPLY;
145
146         if (lpcfg_dns_recursive_queries(dns->task->lp_ctx)) {
147                 state->state.flags |= DNS_FLAG_RECURSION_AVAIL;
148         }
149
150         state->out_packet = state->in_packet;
151
152         switch (state->in_packet.operation & DNS_OPCODE) {
153         case DNS_OPCODE_QUERY:
154                 subreq = dns_server_process_query_send(
155                         state, ev, dns, &state->state, &state->in_packet);
156                 if (tevent_req_nomem(subreq, req)) {
157                         return tevent_req_post(req, ev);
158                 }
159                 tevent_req_set_callback(subreq, dns_process_done, req);
160                 return req;
161         case DNS_OPCODE_UPDATE:
162                 ret = dns_server_process_update(
163                         dns, &state->state, state, &state->in_packet,
164                         &state->out_packet.answers, &state->out_packet.ancount,
165                         &state->out_packet.nsrecs,  &state->out_packet.nscount,
166                         &state->out_packet.additional,
167                         &state->out_packet.arcount);
168                 break;
169         default:
170                 ret = WERR_DNS_ERROR_RCODE_NOT_IMPLEMENTED;
171         }
172         if (!W_ERROR_IS_OK(ret)) {
173                 state->dns_err = werr_to_dns_err(ret);
174         }
175         tevent_req_done(req);
176         return tevent_req_post(req, ev);
177 }
178
179 static void dns_process_done(struct tevent_req *subreq)
180 {
181         struct tevent_req *req = tevent_req_callback_data(
182                 subreq, struct tevent_req);
183         struct dns_process_state *state = tevent_req_data(
184                 req, struct dns_process_state);
185         WERROR ret;
186
187         ret = dns_server_process_query_recv(
188                 subreq, state,
189                 &state->out_packet.answers, &state->out_packet.ancount,
190                 &state->out_packet.nsrecs,  &state->out_packet.nscount,
191                 &state->out_packet.additional, &state->out_packet.arcount);
192         TALLOC_FREE(subreq);
193
194         if (!W_ERROR_IS_OK(ret)) {
195                 state->dns_err = werr_to_dns_err(ret);
196         }
197         tevent_req_done(req);
198 }
199
200 static WERROR dns_process_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
201                                DATA_BLOB *out)
202 {
203         struct dns_process_state *state = tevent_req_data(
204                 req, struct dns_process_state);
205         enum ndr_err_code ndr_err;
206         WERROR ret;
207
208         if (tevent_req_is_werror(req, &ret)) {
209                 return ret;
210         }
211         if (state->dns_err != DNS_RCODE_OK) {
212                 goto drop;
213         }
214         state->out_packet.operation |= state->state.flags;
215
216         ndr_err = ndr_push_struct_blob(
217                 out, mem_ctx, &state->out_packet,
218                 (ndr_push_flags_fn_t)ndr_push_dns_name_packet);
219         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
220                 DEBUG(1, ("Failed to push packet: %s!\n",
221                           ndr_errstr(ndr_err)));
222                 state->dns_err = DNS_RCODE_SERVFAIL;
223                 goto drop;
224         }
225         return WERR_OK;
226
227 drop:
228         *out = data_blob_talloc(mem_ctx, state->in->data, state->in->length);
229         if (out->data == NULL) {
230                 return WERR_NOMEM;
231         }
232         out->data[2] |= 0x80; /* Toggle DNS_FLAG_REPLY */
233         out->data[3] |= state->dns_err;
234         return WERR_OK;
235 }
236
237 static WERROR dns_process(struct dns_server *dns, TALLOC_CTX *mem_ctx,
238                           DATA_BLOB *in, DATA_BLOB *out)
239 {
240         struct tevent_context *ev;
241         struct tevent_req *req;
242         WERROR err = WERR_NOMEM;
243
244         ev = tevent_context_init(talloc_tos());
245         if (ev == NULL) {
246                 goto fail;
247         }
248         req = dns_process_send(ev, ev, dns, in);
249         if (req == NULL) {
250                 goto fail;
251         }
252         if (!tevent_req_poll_werror(req, ev, &err)) {
253                 goto fail;
254         }
255         err = dns_process_recv(req, mem_ctx, out);
256 fail:
257         TALLOC_FREE(ev);
258         return err;
259 }
260
261 struct dns_tcp_call {
262         struct dns_tcp_connection *dns_conn;
263         DATA_BLOB in;
264         DATA_BLOB out;
265         uint8_t out_hdr[4];
266         struct iovec out_iov[2];
267 };
268
269 static void dns_tcp_call_process_done(struct tevent_req *subreq);
270 static void dns_tcp_call_writev_done(struct tevent_req *subreq);
271
272 static void dns_tcp_call_loop(struct tevent_req *subreq)
273 {
274         struct dns_tcp_connection *dns_conn = tevent_req_callback_data(subreq,
275                                       struct dns_tcp_connection);
276         struct dns_server *dns = dns_conn->dns_socket->dns;
277         struct dns_tcp_call *call;
278         NTSTATUS status;
279
280         call = talloc(dns_conn, struct dns_tcp_call);
281         if (call == NULL) {
282                 dns_tcp_terminate_connection(dns_conn, "dns_tcp_call_loop: "
283                                 "no memory for dns_tcp_call");
284                 return;
285         }
286         call->dns_conn = dns_conn;
287
288         status = tstream_read_pdu_blob_recv(subreq,
289                                             call,
290                                             &call->in);
291         TALLOC_FREE(subreq);
292         if (!NT_STATUS_IS_OK(status)) {
293                 const char *reason;
294
295                 reason = talloc_asprintf(call, "dns_tcp_call_loop: "
296                                          "tstream_read_pdu_blob_recv() - %s",
297                                          nt_errstr(status));
298                 if (!reason) {
299                         reason = nt_errstr(status);
300                 }
301
302                 dns_tcp_terminate_connection(dns_conn, reason);
303                 return;
304         }
305
306         DEBUG(10,("Received DNS TCP packet of length %lu from %s\n",
307                  (long) call->in.length,
308                  tsocket_address_string(dns_conn->conn->remote_address, call)));
309
310         /* skip length header */
311         call->in.data += 2;
312         call->in.length -= 2;
313
314         subreq = dns_process_send(call, dns->task->event_ctx, dns,
315                                   &call->in);
316         if (subreq == NULL) {
317                 dns_tcp_terminate_connection(
318                         dns_conn, "dns_tcp_call_loop: dns_process_send "
319                         "failed\n");
320                 return;
321         }
322         tevent_req_set_callback(subreq, dns_tcp_call_process_done, call);
323
324         /*
325          * The dns tcp pdu's has the length as 2 byte (initial_read_size),
326          * packet_full_request_u16 provides the pdu length then.
327          */
328         subreq = tstream_read_pdu_blob_send(dns_conn,
329                                             dns_conn->conn->event.ctx,
330                                             dns_conn->tstream,
331                                             2, /* initial_read_size */
332                                             packet_full_request_u16,
333                                             dns_conn);
334         if (subreq == NULL) {
335                 dns_tcp_terminate_connection(dns_conn, "dns_tcp_call_loop: "
336                                 "no memory for tstream_read_pdu_blob_send");
337                 return;
338         }
339         tevent_req_set_callback(subreq, dns_tcp_call_loop, dns_conn);
340 }
341
342 static void dns_tcp_call_process_done(struct tevent_req *subreq)
343 {
344         struct dns_tcp_call *call = tevent_req_callback_data(subreq,
345                         struct dns_tcp_call);
346         struct dns_tcp_connection *dns_conn = call->dns_conn;
347         WERROR err;
348
349         err = dns_process_recv(subreq, call, &call->out);
350         TALLOC_FREE(subreq);
351         if (!W_ERROR_IS_OK(err)) {
352                 DEBUG(1, ("dns_process returned %s\n", win_errstr(err)));
353                 dns_tcp_terminate_connection(dns_conn,
354                                 "dns_tcp_call_loop: process function failed");
355                 return;
356         }
357
358         /* First add the length of the out buffer */
359         RSSVAL(call->out_hdr, 0, call->out.length);
360         call->out_iov[0].iov_base = (char *) call->out_hdr;
361         call->out_iov[0].iov_len = 2;
362
363         call->out_iov[1].iov_base = (char *) call->out.data;
364         call->out_iov[1].iov_len = call->out.length;
365
366         subreq = tstream_writev_queue_send(call,
367                                            dns_conn->conn->event.ctx,
368                                            dns_conn->tstream,
369                                            dns_conn->send_queue,
370                                            call->out_iov, 2);
371         if (subreq == NULL) {
372                 dns_tcp_terminate_connection(dns_conn, "dns_tcp_call_loop: "
373                                 "no memory for tstream_writev_queue_send");
374                 return;
375         }
376         tevent_req_set_callback(subreq, dns_tcp_call_writev_done, call);
377 }
378
379 static void dns_tcp_call_writev_done(struct tevent_req *subreq)
380 {
381         struct dns_tcp_call *call = tevent_req_callback_data(subreq,
382                         struct dns_tcp_call);
383         int sys_errno;
384         int rc;
385
386         rc = tstream_writev_queue_recv(subreq, &sys_errno);
387         TALLOC_FREE(subreq);
388         if (rc == -1) {
389                 const char *reason;
390
391                 reason = talloc_asprintf(call, "dns_tcp_call_writev_done: "
392                                          "tstream_writev_queue_recv() - %d:%s",
393                                          sys_errno, strerror(sys_errno));
394                 if (!reason) {
395                         reason = "dns_tcp_call_writev_done: tstream_writev_queue_recv() failed";
396                 }
397
398                 dns_tcp_terminate_connection(call->dns_conn, reason);
399                 return;
400         }
401
402         /* We don't care about errors */
403
404         talloc_free(call);
405 }
406
407 /*
408   called when we get a new connection
409 */
410 static void dns_tcp_accept(struct stream_connection *conn)
411 {
412         struct dns_socket *dns_socket;
413         struct dns_tcp_connection *dns_conn;
414         struct tevent_req *subreq;
415         int rc;
416
417         dns_conn = talloc_zero(conn, struct dns_tcp_connection);
418         if (dns_conn == NULL) {
419                 stream_terminate_connection(conn,
420                                 "dns_tcp_accept: out of memory");
421                 return;
422         }
423
424         dns_conn->send_queue = tevent_queue_create(conn, "dns_tcp_accept");
425         if (dns_conn->send_queue == NULL) {
426                 stream_terminate_connection(conn,
427                                 "dns_tcp_accept: out of memory");
428                 return;
429         }
430
431         dns_socket = talloc_get_type(conn->private_data, struct dns_socket);
432
433         TALLOC_FREE(conn->event.fde);
434
435         rc = tstream_bsd_existing_socket(dns_conn,
436                         socket_get_fd(conn->socket),
437                         &dns_conn->tstream);
438         if (rc < 0) {
439                 stream_terminate_connection(conn,
440                                 "dns_tcp_accept: out of memory");
441                 return;
442         }
443
444         dns_conn->conn = conn;
445         dns_conn->dns_socket = dns_socket;
446         conn->private_data = dns_conn;
447
448         /*
449          * The dns tcp pdu's has the length as 2 byte (initial_read_size),
450          * packet_full_request_u16 provides the pdu length then.
451          */
452         subreq = tstream_read_pdu_blob_send(dns_conn,
453                                             dns_conn->conn->event.ctx,
454                                             dns_conn->tstream,
455                                             2, /* initial_read_size */
456                                             packet_full_request_u16,
457                                             dns_conn);
458         if (subreq == NULL) {
459                 dns_tcp_terminate_connection(dns_conn, "dns_tcp_accept: "
460                                 "no memory for tstream_read_pdu_blob_send");
461                 return;
462         }
463         tevent_req_set_callback(subreq, dns_tcp_call_loop, dns_conn);
464 }
465
466 static const struct stream_server_ops dns_tcp_stream_ops = {
467         .name                   = "dns_tcp",
468         .accept_connection      = dns_tcp_accept,
469         .recv_handler           = dns_tcp_recv,
470         .send_handler           = dns_tcp_send
471 };
472
473 struct dns_udp_call {
474         struct dns_udp_socket *sock;
475         struct tsocket_address *src;
476         DATA_BLOB in;
477         DATA_BLOB out;
478 };
479
480 static void dns_udp_call_process_done(struct tevent_req *subreq);
481 static void dns_udp_call_sendto_done(struct tevent_req *subreq);
482
483 static void dns_udp_call_loop(struct tevent_req *subreq)
484 {
485         struct dns_udp_socket *sock = tevent_req_callback_data(subreq,
486                                       struct dns_udp_socket);
487         struct dns_server *dns = sock->dns_socket->dns;
488         struct dns_udp_call *call;
489         uint8_t *buf;
490         ssize_t len;
491         int sys_errno;
492
493         call = talloc(sock, struct dns_udp_call);
494         if (call == NULL) {
495                 talloc_free(call);
496                 goto done;
497         }
498         call->sock = sock;
499
500         len = tdgram_recvfrom_recv(subreq, &sys_errno,
501                                    call, &buf, &call->src);
502         TALLOC_FREE(subreq);
503         if (len == -1) {
504                 talloc_free(call);
505                 goto done;
506         }
507
508         call->in.data = buf;
509         call->in.length = len;
510
511         DEBUG(10,("Received DNS UDP packet of length %lu from %s\n",
512                  (long)call->in.length,
513                  tsocket_address_string(call->src, call)));
514
515         subreq = dns_process_send(call, dns->task->event_ctx, dns,
516                                   &call->in);
517         if (subreq == NULL) {
518                 TALLOC_FREE(call);
519                 goto done;
520         }
521         tevent_req_set_callback(subreq, dns_udp_call_process_done, call);
522
523 done:
524         subreq = tdgram_recvfrom_send(sock,
525                                       sock->dns_socket->dns->task->event_ctx,
526                                       sock->dgram);
527         if (subreq == NULL) {
528                 task_server_terminate(sock->dns_socket->dns->task,
529                                       "no memory for tdgram_recvfrom_send",
530                                       true);
531                 return;
532         }
533         tevent_req_set_callback(subreq, dns_udp_call_loop, sock);
534 }
535
536 static void dns_udp_call_process_done(struct tevent_req *subreq)
537 {
538         struct dns_udp_call *call = tevent_req_callback_data(
539                 subreq, struct dns_udp_call);
540         struct dns_udp_socket *sock = call->sock;
541         struct dns_server *dns = sock->dns_socket->dns;
542         WERROR err;
543
544         err = dns_process_recv(subreq, call, &call->out);
545         TALLOC_FREE(subreq);
546         if (!W_ERROR_IS_OK(err)) {
547                 DEBUG(1, ("dns_process returned %s\n", win_errstr(err)));
548                 TALLOC_FREE(call);
549                 return;
550         }
551
552         subreq = tdgram_sendto_queue_send(call,
553                                           dns->task->event_ctx,
554                                           sock->dgram,
555                                           sock->send_queue,
556                                           call->out.data,
557                                           call->out.length,
558                                           call->src);
559         if (subreq == NULL) {
560                 talloc_free(call);
561                 return;
562         }
563         tevent_req_set_callback(subreq, dns_udp_call_sendto_done, call);
564
565 }
566 static void dns_udp_call_sendto_done(struct tevent_req *subreq)
567 {
568         struct dns_udp_call *call = tevent_req_callback_data(subreq,
569                                        struct dns_udp_call);
570         ssize_t ret;
571         int sys_errno;
572
573         ret = tdgram_sendto_queue_recv(subreq, &sys_errno);
574
575         /* We don't care about errors */
576
577         talloc_free(call);
578 }
579
580 /*
581   start listening on the given address
582 */
583 static NTSTATUS dns_add_socket(struct dns_server *dns,
584                                const struct model_ops *model_ops,
585                                const char *name,
586                                const char *address,
587                                uint16_t port)
588 {
589         struct dns_socket *dns_socket;
590         struct dns_udp_socket *dns_udp_socket;
591         struct tevent_req *udpsubreq;
592         NTSTATUS status;
593         int ret;
594
595         dns_socket = talloc(dns, struct dns_socket);
596         NT_STATUS_HAVE_NO_MEMORY(dns_socket);
597
598         dns_socket->dns = dns;
599
600         ret = tsocket_address_inet_from_strings(dns_socket, "ip",
601                                                 address, port,
602                                                 &dns_socket->local_address);
603         if (ret != 0) {
604                 status = map_nt_error_from_unix_common(errno);
605                 return status;
606         }
607
608         status = stream_setup_socket(dns->task,
609                                      dns->task->event_ctx,
610                                      dns->task->lp_ctx,
611                                      model_ops,
612                                      &dns_tcp_stream_ops,
613                                      "ip", address, &port,
614                                      lpcfg_socket_options(dns->task->lp_ctx),
615                                      dns_socket);
616         if (!NT_STATUS_IS_OK(status)) {
617                 DEBUG(0,("Failed to bind to %s:%u TCP - %s\n",
618                          address, port, nt_errstr(status)));
619                 talloc_free(dns_socket);
620                 return status;
621         }
622
623         dns_udp_socket = talloc(dns_socket, struct dns_udp_socket);
624         NT_STATUS_HAVE_NO_MEMORY(dns_udp_socket);
625
626         dns_udp_socket->dns_socket = dns_socket;
627
628         ret = tdgram_inet_udp_socket(dns_socket->local_address,
629                                      NULL,
630                                      dns_udp_socket,
631                                      &dns_udp_socket->dgram);
632         if (ret != 0) {
633                 status = map_nt_error_from_unix_common(errno);
634                 DEBUG(0,("Failed to bind to %s:%u UDP - %s\n",
635                          address, port, nt_errstr(status)));
636                 return status;
637         }
638
639         dns_udp_socket->send_queue = tevent_queue_create(dns_udp_socket,
640                                                          "dns_udp_send_queue");
641         NT_STATUS_HAVE_NO_MEMORY(dns_udp_socket->send_queue);
642
643         udpsubreq = tdgram_recvfrom_send(dns_udp_socket,
644                                          dns->task->event_ctx,
645                                          dns_udp_socket->dgram);
646         NT_STATUS_HAVE_NO_MEMORY(udpsubreq);
647         tevent_req_set_callback(udpsubreq, dns_udp_call_loop, dns_udp_socket);
648
649         return NT_STATUS_OK;
650 }
651
652 /*
653   setup our listening sockets on the configured network interfaces
654 */
655 static NTSTATUS dns_startup_interfaces(struct dns_server *dns, struct loadparm_context *lp_ctx,
656                                        struct interface *ifaces)
657 {
658         const struct model_ops *model_ops;
659         int num_interfaces;
660         TALLOC_CTX *tmp_ctx = talloc_new(dns);
661         NTSTATUS status;
662         int i;
663
664         /* within the dns task we want to be a single process, so
665            ask for the single process model ops and pass these to the
666            stream_setup_socket() call. */
667         model_ops = process_model_startup("single");
668         if (!model_ops) {
669                 DEBUG(0,("Can't find 'single' process model_ops\n"));
670                 return NT_STATUS_INTERNAL_ERROR;
671         }
672
673         num_interfaces = iface_list_count(ifaces);
674
675         for (i=0; i<num_interfaces; i++) {
676                 const char *address = talloc_strdup(tmp_ctx, iface_list_n_ip(ifaces, i));
677
678                 status = dns_add_socket(dns, model_ops, "dns", address, DNS_SERVICE_PORT);
679                 NT_STATUS_NOT_OK_RETURN(status);
680         }
681
682         talloc_free(tmp_ctx);
683
684         return NT_STATUS_OK;
685 }
686
687 static int dns_server_sort_zones(struct ldb_message **m1, struct ldb_message **m2)
688 {
689         const char *n1, *n2;
690         size_t l1, l2;
691
692         n1 = ldb_msg_find_attr_as_string(*m1, "name", NULL);
693         n2 = ldb_msg_find_attr_as_string(*m2, "name", NULL);
694
695         l1 = strlen(n1);
696         l2 = strlen(n2);
697
698         /* If the string lengths are not equal just sort by length */
699         if (l1 != l2) {
700                 /* If m1 is the larger zone name, return it first */
701                 return l2 - l1;
702         }
703
704         /*TODO: We need to compare DNs here, we want the DomainDNSZones first */
705         return 0;
706 }
707
708 static void dns_task_init(struct task_server *task)
709 {
710         struct dns_server *dns;
711         NTSTATUS status;
712         struct interface *ifaces;
713         int ret;
714         struct ldb_result *res;
715         static const char * const attrs[] = { "name", NULL};
716         unsigned int i;
717
718         switch (lpcfg_server_role(task->lp_ctx)) {
719         case ROLE_STANDALONE:
720                 task_server_terminate(task, "dns: no DNS required in standalone configuration", false);
721                 return;
722         case ROLE_DOMAIN_MEMBER:
723                 task_server_terminate(task, "dns: no DNS required in member server configuration", false);
724                 return;
725         case ROLE_DOMAIN_CONTROLLER:
726                 /* Yes, we want a DNS */
727                 break;
728         }
729
730         load_interface_list(task, task->lp_ctx, &ifaces);
731
732         if (iface_list_count(ifaces) == 0) {
733                 task_server_terminate(task, "dns: no network interfaces configured", false);
734                 return;
735         }
736
737         task_server_set_title(task, "task[dns]");
738
739         dns = talloc_zero(task, struct dns_server);
740         if (dns == NULL) {
741                 task_server_terminate(task, "dns: out of memory", true);
742                 return;
743         }
744
745         dns->task = task;
746
747         dns->samdb = samdb_connect(dns, dns->task->event_ctx, dns->task->lp_ctx,
748                               system_session(dns->task->lp_ctx), 0);
749         if (!dns->samdb) {
750                 task_server_terminate(task, "dns: samdb_connect failed", true);
751                 return;
752         }
753
754         // TODO: this search does not work against windows
755         ret = dsdb_search(dns->samdb, dns, &res, NULL, LDB_SCOPE_SUBTREE,
756                           attrs, DSDB_SEARCH_SEARCH_ALL_PARTITIONS, "(objectClass=dnsZone)");
757         if (ret != LDB_SUCCESS) {
758                 task_server_terminate(task,
759                                       "dns: failed to look up root DNS zones",
760                                       true);
761                 return;
762         }
763
764         TYPESAFE_QSORT(res->msgs, res->count, dns_server_sort_zones);
765
766         for (i=0; i < res->count; i++) {
767                 struct dns_server_zone *z;
768
769                 z = talloc_zero(dns, struct dns_server_zone);
770                 if (z == NULL) {
771                 }
772
773                 z->name = ldb_msg_find_attr_as_string(res->msgs[i], "name", NULL);
774                 z->dn = talloc_move(z, &res->msgs[i]->dn);
775
776                 DLIST_ADD_END(dns->zones, z, NULL);
777         }
778
779         status = dns_startup_interfaces(dns, task->lp_ctx, ifaces);
780         if (!NT_STATUS_IS_OK(status)) {
781                 task_server_terminate(task, "dns failed to setup interfaces", true);
782                 return;
783         }
784 }
785
786 NTSTATUS server_service_dns_init(void)
787 {
788         return register_server_service("dns", dns_task_init);
789 }