2 Unix SMB/CIFS implementation.
6 Copyright (C) 2010 Kai Blin <kai@samba.org>
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.
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.
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/>.
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"
41 #include "dsdb/samdb/samdb.h"
42 #include "dsdb/common/util.h"
43 #include "auth/session.h"
44 #include "lib/util/dlinklist.h"
46 /* hold information about one dns socket */
48 struct dns_server *dns;
49 struct tsocket_address *local_address;
52 struct dns_udp_socket {
53 struct dns_socket *dns_socket;
54 struct tdgram_context *dgram;
55 struct tevent_queue *send_queue;
59 state of an open tcp connection
61 struct dns_tcp_connection {
62 /* stream connection we belong to */
63 struct stream_connection *conn;
65 /* the dns_server the connection belongs to */
66 struct dns_socket *dns_socket;
68 struct tstream_context *tstream;
70 struct tevent_queue *send_queue;
73 static void dns_tcp_terminate_connection(struct dns_tcp_connection *dnsconn, const char *reason)
75 stream_terminate_connection(dnsconn->conn, reason);
78 static void dns_tcp_recv(struct stream_connection *conn, uint16_t flags)
80 struct dns_tcp_connection *dnsconn = talloc_get_type(conn->private_data,
81 struct dns_tcp_connection);
82 /* this should never be triggered! */
83 dns_tcp_terminate_connection(dnsconn, "dns_tcp_recv: called");
86 static void dns_tcp_send(struct stream_connection *conn, uint16_t flags)
88 struct dns_tcp_connection *dnsconn = talloc_get_type(conn->private_data,
89 struct dns_tcp_connection);
90 /* this should never be triggered! */
91 dns_tcp_terminate_connection(dnsconn, "dns_tcp_send: called");
94 static bool dns_name_match(const char *zone, const char *name, size_t *host_part_len)
96 size_t zl = strlen(zone);
97 size_t nl = strlen(name);
99 static const size_t fixup = 'a' - 'A';
105 for (zi = zl, ni = nl; zi >= 0; zi--, ni--) {
109 /* convert to lower case */
110 if (zc >= 'A' && zc <= 'Z') {
113 if (nc >= 'A' && nc <= 'Z') {
123 if (name[ni] != '.') {
130 *host_part_len = ni+1;
135 static NTSTATUS dns_name2dn(struct dns_server *dns,
142 const struct dns_server_zone *z;
143 size_t host_part_len = 0;
146 return NT_STATUS_INVALID_PARAMETER;
149 /*TODO: Check if 'name' is a valid DNS name */
151 if (strcmp(name, "") == 0) {
152 base = ldb_get_default_basedn(dns->samdb);
153 dn = ldb_dn_copy(mem_ctx, base);
154 ldb_dn_add_child_fmt(dn, "DC=@,DC=RootDNSServers,CN=MicrosoftDNS,CN=System");
159 for (z = dns->zones; z != NULL; z = z->next) {
162 match = dns_name_match(z->name, name, &host_part_len);
169 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
172 if (host_part_len == 0) {
173 dn = ldb_dn_copy(mem_ctx, z->dn);
174 ldb_dn_add_child_fmt(dn, "DC=@");
179 dn = ldb_dn_copy(mem_ctx, z->dn);
180 ldb_dn_add_child_fmt(dn, "DC=%*.*s", (int)host_part_len, (int)host_part_len, name);
185 static NTSTATUS handle_question(struct dns_server *dns,
187 const struct dns_name_question *question,
188 struct dns_res_rec **answers, uint16_t *ancount)
190 struct dns_res_rec *ans;
191 struct ldb_dn *dn = NULL;
193 static const char * const attrs[] = { "dnsRecord", NULL};
195 uint16_t ai = *ancount;
197 struct ldb_message *msg = NULL;
198 struct dnsp_DnssrvRpcRecord *recs;
199 struct ldb_message_element *el;
201 status = dns_name2dn(dns, mem_ctx, question->name, &dn);
202 NT_STATUS_NOT_OK_RETURN(status);
204 ret = dsdb_search_one(dns->samdb, mem_ctx, &msg, dn,
205 LDB_SCOPE_BASE, attrs, 0, "%s", "(objectClass=dnsNode)");
206 if (ret != LDB_SUCCESS) {
207 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
210 el = ldb_msg_find_element(msg, attrs[0]);
212 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
215 recs = talloc_array(mem_ctx, struct dnsp_DnssrvRpcRecord, el->num_values);
216 for (ri = 0; ri < el->num_values; ri++) {
217 struct ldb_val *v = &el->values[ri];
218 enum ndr_err_code ndr_err;
220 ndr_err = ndr_pull_struct_blob(v, recs, &recs[ri],
221 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
222 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
223 DEBUG(0, ("Failed to grab dnsp_DnssrvRpcRecord\n"));
224 return NT_STATUS_INTERNAL_DB_CORRUPTION;
228 ans = talloc_realloc(mem_ctx, *answers, struct dns_res_rec,
229 ai + el->num_values);
230 NT_STATUS_HAVE_NO_MEMORY(ans);
232 switch (question->question_type) {
233 case DNS_QTYPE_CNAME:
234 for (ri = 0; ri < el->num_values; ri++) {
235 if (recs[ri].wType != question->question_type) {
239 ZERO_STRUCT(ans[ai]);
240 ans[ai].name = talloc_strdup(ans, question->name);
241 ans[ai].rr_type = DNS_QTYPE_CNAME;
242 ans[ai].rr_class = DNS_QCLASS_IP;
243 ans[ai].ttl = recs[ri].dwTtlSeconds;
244 ans[ai].rdata.cname_record = talloc_strdup(ans, recs[ri].data.cname);
249 for (ri = 0; ri < el->num_values; ri++) {
250 if (recs[ri].wType != question->question_type) {
254 ZERO_STRUCT(ans[ai]);
255 ans[ai].name = talloc_strdup(ans, question->name);
256 ans[ai].rr_type = DNS_QTYPE_A;
257 ans[ai].rr_class = DNS_QCLASS_IP;
258 ans[ai].ttl = recs[ri].dwTtlSeconds;
259 ans[ai].rdata.ipv4_record = talloc_strdup(ans, recs[ri].data.ipv4);
264 for (ri = 0; ri < el->num_values; ri++) {
265 if (recs[ri].wType != question->question_type) {
269 ZERO_STRUCT(ans[ai]);
270 ans[ai].name = talloc_strdup(ans, question->name);
271 ans[ai].rr_type = DNS_QTYPE_AAAA;
272 ans[ai].rr_class = DNS_QCLASS_IP;
273 ans[ai].ttl = recs[ri].dwTtlSeconds;
274 ans[ai].rdata.ipv6_record = recs[ri].data.ipv6;
279 for (ri = 0; ri < el->num_values; ri++) {
280 if (recs[ri].wType != question->question_type) {
284 ZERO_STRUCT(ans[ai]);
285 ans[ai].name = question->name;
286 ans[ai].rr_type = DNS_QTYPE_NS;
287 ans[ai].rr_class = DNS_QCLASS_IP;
288 ans[ai].ttl = recs[ri].dwTtlSeconds;
289 ans[ai].rdata.ns_record = recs[ri].data.ns;
294 for (ri = 0; ri < el->num_values; ri++) {
295 if (recs[ri].wType != question->question_type) {
299 ZERO_STRUCT(ans[ai]);
300 ans[ai].name = question->name;
301 ans[ai].rr_type = DNS_QTYPE_SRV;
302 ans[ai].rr_class = DNS_QCLASS_IP;
303 ans[ai].ttl = recs[ri].dwTtlSeconds;
304 ans[ai].rdata.srv_record.priority = recs[ri].data.srv.wPriority;
305 ans[ai].rdata.srv_record.weight = recs[ri].data.srv.wWeight;
306 ans[ai].rdata.srv_record.port = recs[ri].data.srv.wPort;
307 ans[ai].rdata.srv_record.target = recs[ri].data.srv.nameTarget;
312 for (ri = 0; ri < el->num_values; ri++) {
313 if (recs[ri].wType != question->question_type) {
317 ZERO_STRUCT(ans[ai]);
318 ans[ai].name = question->name;
319 ans[ai].rr_type = DNS_QTYPE_SOA;
320 ans[ai].rr_class = DNS_QCLASS_IP;
321 ans[ai].ttl = recs[ri].dwTtlSeconds;
322 ans[ai].rdata.soa_record.mname = recs[ri].data.soa.mname;
323 ans[ai].rdata.soa_record.rname = recs[ri].data.soa.rname;
324 ans[ai].rdata.soa_record.serial = recs[ri].data.soa.serial;
325 ans[ai].rdata.soa_record.refresh= recs[ri].data.soa.refresh;
326 ans[ai].rdata.soa_record.retry = recs[ri].data.soa.retry;
327 ans[ai].rdata.soa_record.expire = recs[ri].data.soa.expire;
328 ans[ai].rdata.soa_record.minimum= recs[ri].data.soa.minimum;
333 return NT_STATUS_NOT_IMPLEMENTED;
343 static NTSTATUS compute_reply(struct dns_server *dns,
345 struct dns_name_packet *in,
346 struct dns_res_rec **answers, uint16_t *ancount,
347 struct dns_res_rec **nsrecs, uint16_t *nscount,
348 struct dns_res_rec **additional, uint16_t *arcount)
350 uint16_t num_answers=0;
351 struct dns_res_rec *ans=NULL;
355 ans = talloc_array(mem_ctx, struct dns_res_rec, 0);
356 if (answers == NULL) return NT_STATUS_NO_MEMORY;
358 for (i = 0; i < in->qdcount; ++i) {
359 status = handle_question(dns, mem_ctx, &in->questions[i], &ans, &num_answers);
360 NT_STATUS_NOT_OK_RETURN(status);
364 *ancount = num_answers;
366 /*FIXME: Do something for these */
376 static NTSTATUS dns_process(struct dns_server *dns,
381 enum ndr_err_code ndr_err;
383 struct dns_name_packet *in_packet = talloc_zero(mem_ctx, struct dns_name_packet);
384 struct dns_name_packet *out_packet = talloc_zero(mem_ctx, struct dns_name_packet);
385 struct dns_res_rec *answers, *nsrecs, *additional;
386 uint16_t num_answers, num_nsrecs, num_additional;
388 if (in_packet == NULL) return NT_STATUS_INVALID_PARAMETER;
390 dump_data(2, in->data, in->length);
392 ndr_err = ndr_pull_struct_blob(in, in_packet, in_packet,
393 (ndr_pull_flags_fn_t)ndr_pull_dns_name_packet);
394 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
395 TALLOC_FREE(in_packet);
396 DEBUG(0, ("Failed to parse packet %d!\n", ndr_err));
397 return NT_STATUS_COULD_NOT_INTERPRET;
400 NDR_PRINT_DEBUG(dns_name_packet, in_packet);
401 out_packet->id = in_packet->id;
402 out_packet->operation = DNS_FLAG_REPLY | DNS_FLAG_AUTHORITATIVE |
403 DNS_FLAG_RECURSION_DESIRED | DNS_FLAG_RECURSION_AVAIL;
405 out_packet->qdcount = in_packet->qdcount;
406 out_packet->questions = in_packet->questions;
408 out_packet->ancount = 0;
409 out_packet->answers = NULL;
411 out_packet->nscount = 0;
412 out_packet->nsrecs = NULL;
414 out_packet->arcount = 0;
415 out_packet->additional = NULL;
417 ret = compute_reply(dns, out_packet, in_packet, &answers, &num_answers,
418 &nsrecs, &num_nsrecs, &additional, &num_additional);
420 if (NT_STATUS_IS_OK(ret)) {
421 out_packet->ancount = num_answers;
422 out_packet->answers = answers;
424 out_packet->nscount = num_nsrecs;
425 out_packet->nsrecs = nsrecs;
427 out_packet->arcount = num_additional;
428 out_packet->additional = additional;
431 NDR_PRINT_DEBUG(dns_name_packet, out_packet);
432 ndr_err = ndr_push_struct_blob(out, out_packet, out_packet,
433 (ndr_push_flags_fn_t)ndr_push_dns_name_packet);
434 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
435 TALLOC_FREE(in_packet);
436 TALLOC_FREE(out_packet);
437 DEBUG(0, ("Failed to push packet %d!\n", ndr_err));
438 return NT_STATUS_INTERNAL_ERROR;
441 dump_data(2, out->data, out->length);
445 struct dns_tcp_call {
446 struct dns_tcp_connection *dns_conn;
450 struct iovec out_iov[2];
453 static void dns_tcp_call_writev_done(struct tevent_req *subreq);
455 static void dns_tcp_call_loop(struct tevent_req *subreq)
457 struct dns_tcp_connection *dns_conn = tevent_req_callback_data(subreq,
458 struct dns_tcp_connection);
459 struct dns_tcp_call *call;
462 call = talloc(dns_conn, struct dns_tcp_call);
464 dns_tcp_terminate_connection(dns_conn, "dns_tcp_call_loop: "
465 "no memory for dns_tcp_call");
468 call->dns_conn = dns_conn;
470 status = tstream_read_pdu_blob_recv(subreq,
474 if (!NT_STATUS_IS_OK(status)) {
477 reason = talloc_asprintf(call, "dns_tcp_call_loop: "
478 "tstream_read_pdu_blob_recv() - %s",
481 reason = nt_errstr(status);
484 dns_tcp_terminate_connection(dns_conn, reason);
488 DEBUG(10,("Received krb5 TCP packet of length %lu from %s\n",
489 (long) call->in.length,
490 tsocket_address_string(dns_conn->conn->remote_address, call)));
492 /* skip length header */
494 call->in.length -= 4;
497 status = dns_process(dns_conn->dns_socket->dns, call, &call->in, &call->out);
498 if (!NT_STATUS_IS_OK(status)) {
499 DEBUG(0, ("dns_process returned %s\n", nt_errstr(status)));
500 dns_tcp_terminate_connection(dns_conn,
501 "dns_tcp_call_loop: process function failed");
505 /* First add the length of the out buffer */
506 RSIVAL(call->out_hdr, 0, call->out.length);
507 call->out_iov[0].iov_base = (char *) call->out_hdr;
508 call->out_iov[0].iov_len = 4;
510 call->out_iov[1].iov_base = (char *) call->out.data;
511 call->out_iov[1].iov_len = call->out.length;
513 subreq = tstream_writev_queue_send(call,
514 dns_conn->conn->event.ctx,
516 dns_conn->send_queue,
518 if (subreq == NULL) {
519 dns_tcp_terminate_connection(dns_conn, "dns_tcp_call_loop: "
520 "no memory for tstream_writev_queue_send");
523 tevent_req_set_callback(subreq, dns_tcp_call_writev_done, call);
526 * The krb5 tcp pdu's has the length as 4 byte (initial_read_size),
527 * packet_full_request_u32 provides the pdu length then.
529 subreq = tstream_read_pdu_blob_send(dns_conn,
530 dns_conn->conn->event.ctx,
532 4, /* initial_read_size */
533 packet_full_request_u32,
535 if (subreq == NULL) {
536 dns_tcp_terminate_connection(dns_conn, "dns_tcp_call_loop: "
537 "no memory for tstream_read_pdu_blob_send");
540 tevent_req_set_callback(subreq, dns_tcp_call_loop, dns_conn);
543 static void dns_tcp_call_writev_done(struct tevent_req *subreq)
545 struct dns_tcp_call *call = tevent_req_callback_data(subreq,
546 struct dns_tcp_call);
550 rc = tstream_writev_queue_recv(subreq, &sys_errno);
555 reason = talloc_asprintf(call, "dns_tcp_call_writev_done: "
556 "tstream_writev_queue_recv() - %d:%s",
557 sys_errno, strerror(sys_errno));
559 reason = "dns_tcp_call_writev_done: tstream_writev_queue_recv() failed";
562 dns_tcp_terminate_connection(call->dns_conn, reason);
566 /* We don't care about errors */
572 called when we get a new connection
574 static void dns_tcp_accept(struct stream_connection *conn)
576 struct dns_socket *dns_socket;
577 struct dns_tcp_connection *dns_conn;
578 struct tevent_req *subreq;
581 dns_conn = talloc_zero(conn, struct dns_tcp_connection);
582 if (dns_conn == NULL) {
583 stream_terminate_connection(conn,
584 "dns_tcp_accept: out of memory");
588 dns_conn->send_queue = tevent_queue_create(conn, "dns_tcp_accept");
589 if (dns_conn->send_queue == NULL) {
590 stream_terminate_connection(conn,
591 "dns_tcp_accept: out of memory");
595 dns_socket = talloc_get_type(conn->private_data, struct dns_socket);
597 TALLOC_FREE(conn->event.fde);
599 rc = tstream_bsd_existing_socket(dns_conn,
600 socket_get_fd(conn->socket),
603 stream_terminate_connection(conn,
604 "dns_tcp_accept: out of memory");
608 dns_conn->conn = conn;
609 dns_conn->dns_socket = dns_socket;
610 conn->private_data = dns_conn;
613 * The krb5 tcp pdu's has the length as 4 byte (initial_read_size),
614 * packet_full_request_u32 provides the pdu length then.
616 subreq = tstream_read_pdu_blob_send(dns_conn,
617 dns_conn->conn->event.ctx,
619 4, /* initial_read_size */
620 packet_full_request_u32,
622 if (subreq == NULL) {
623 dns_tcp_terminate_connection(dns_conn, "dns_tcp_accept: "
624 "no memory for tstream_read_pdu_blob_send");
627 tevent_req_set_callback(subreq, dns_tcp_call_loop, dns_conn);
630 static const struct stream_server_ops dns_tcp_stream_ops = {
632 .accept_connection = dns_tcp_accept,
633 .recv_handler = dns_tcp_recv,
634 .send_handler = dns_tcp_send
637 struct dns_udp_call {
638 struct tsocket_address *src;
643 static void dns_udp_call_sendto_done(struct tevent_req *subreq);
645 static void dns_udp_call_loop(struct tevent_req *subreq)
647 struct dns_udp_socket *sock = tevent_req_callback_data(subreq,
648 struct dns_udp_socket);
649 struct dns_udp_call *call;
655 call = talloc(sock, struct dns_udp_call);
661 len = tdgram_recvfrom_recv(subreq, &sys_errno,
662 call, &buf, &call->src);
670 call->in.length = len;
672 DEBUG(10,("Received krb5 UDP packet of length %lu from %s\n",
673 (long)call->in.length,
674 tsocket_address_string(call->src, call)));
677 status = dns_process(sock->dns_socket->dns, call, &call->in, &call->out);
678 if (!NT_STATUS_IS_OK(status)) {
680 DEBUG(0, ("dns_process returned %s\n", nt_errstr(status)));
684 subreq = tdgram_sendto_queue_send(call,
685 sock->dns_socket->dns->task->event_ctx,
691 if (subreq == NULL) {
695 tevent_req_set_callback(subreq, dns_udp_call_sendto_done, call);
698 subreq = tdgram_recvfrom_send(sock,
699 sock->dns_socket->dns->task->event_ctx,
701 if (subreq == NULL) {
702 task_server_terminate(sock->dns_socket->dns->task,
703 "no memory for tdgram_recvfrom_send",
707 tevent_req_set_callback(subreq, dns_udp_call_loop, sock);
710 static void dns_udp_call_sendto_done(struct tevent_req *subreq)
712 struct dns_udp_call *call = tevent_req_callback_data(subreq,
713 struct dns_udp_call);
717 ret = tdgram_sendto_queue_recv(subreq, &sys_errno);
719 /* We don't care about errors */
725 start listening on the given address
727 static NTSTATUS dns_add_socket(struct dns_server *dns,
728 const struct model_ops *model_ops,
733 struct dns_socket *dns_socket;
734 struct dns_udp_socket *dns_udp_socket;
735 struct tevent_req *udpsubreq;
739 dns_socket = talloc(dns, struct dns_socket);
740 NT_STATUS_HAVE_NO_MEMORY(dns_socket);
742 dns_socket->dns = dns;
744 ret = tsocket_address_inet_from_strings(dns_socket, "ip",
746 &dns_socket->local_address);
748 status = map_nt_error_from_unix(errno);
752 status = stream_setup_socket(dns->task->event_ctx,
756 "ip", address, &port,
757 lpcfg_socket_options(dns->task->lp_ctx),
759 if (!NT_STATUS_IS_OK(status)) {
760 DEBUG(0,("Failed to bind to %s:%u TCP - %s\n",
761 address, port, nt_errstr(status)));
762 talloc_free(dns_socket);
766 dns_udp_socket = talloc(dns_socket, struct dns_udp_socket);
767 NT_STATUS_HAVE_NO_MEMORY(dns_udp_socket);
769 dns_udp_socket->dns_socket = dns_socket;
771 ret = tdgram_inet_udp_socket(dns_socket->local_address,
774 &dns_udp_socket->dgram);
776 status = map_nt_error_from_unix(errno);
777 DEBUG(0,("Failed to bind to %s:%u UDP - %s\n",
778 address, port, nt_errstr(status)));
782 dns_udp_socket->send_queue = tevent_queue_create(dns_udp_socket,
783 "dns_udp_send_queue");
784 NT_STATUS_HAVE_NO_MEMORY(dns_udp_socket->send_queue);
786 udpsubreq = tdgram_recvfrom_send(dns_udp_socket,
787 dns->task->event_ctx,
788 dns_udp_socket->dgram);
789 NT_STATUS_HAVE_NO_MEMORY(udpsubreq);
790 tevent_req_set_callback(udpsubreq, dns_udp_call_loop, dns_udp_socket);
796 setup our listening sockets on the configured network interfaces
798 static NTSTATUS dns_startup_interfaces(struct dns_server *dns, struct loadparm_context *lp_ctx,
799 struct interface *ifaces)
801 const struct model_ops *model_ops;
803 TALLOC_CTX *tmp_ctx = talloc_new(dns);
807 /* within the dns task we want to be a single process, so
808 ask for the single process model ops and pass these to the
809 stream_setup_socket() call. */
810 model_ops = process_model_startup(dns->task->event_ctx, "single");
812 DEBUG(0,("Can't find 'single' process model_ops\n"));
813 return NT_STATUS_INTERNAL_ERROR;
816 num_interfaces = iface_count(ifaces);
818 for (i=0; i<num_interfaces; i++) {
819 const char *address = talloc_strdup(tmp_ctx, iface_n_ip(ifaces, i));
821 status = dns_add_socket(dns, model_ops, "dns", address, DNS_SERVICE_PORT);
822 NT_STATUS_NOT_OK_RETURN(status);
825 talloc_free(tmp_ctx);
830 static int dns_server_sort_zones(struct ldb_message **m1, struct ldb_message **m2)
835 n1 = ldb_msg_find_attr_as_string(*m1, "name", NULL);
836 n2 = ldb_msg_find_attr_as_string(*m2, "name", NULL);
841 /* If the string lengths are not equal just sort by length */
843 /* If m1 is the larger zone name, return it first */
847 /*TODO: We need to compare DNs here, we want the DomainDNSZones first */
851 static void dns_task_init(struct task_server *task)
853 struct dns_server *dns;
855 struct interface *ifaces;
857 struct ldb_result *res;
858 struct ldb_dn *rootdn;
859 static const char * const attrs[] = { "name", NULL};
863 switch (lpcfg_server_role(task->lp_ctx)) {
864 case ROLE_STANDALONE:
865 task_server_terminate(task, "dns: no DNS required in standalone configuration", false);
867 case ROLE_DOMAIN_MEMBER:
868 task_server_terminate(task, "dns: no DNS required in member server configuration", false);
870 case ROLE_DOMAIN_CONTROLLER:
871 /* Yes, we want a DNS */
875 load_interfaces(task, lpcfg_interfaces(task->lp_ctx), &ifaces);
877 if (iface_count(ifaces) == 0) {
878 task_server_terminate(task, "dns: no network interfaces configured", false);
882 task_server_set_title(task, "task[dns]");
884 dns = talloc_zero(task, struct dns_server);
886 task_server_terminate(task, "dns: out of memory", true);
892 dns->samdb = samdb_connect(dns, dns->task->event_ctx, dns->task->lp_ctx,
893 system_session(dns->task->lp_ctx), 0);
895 task_server_terminate(task, "dns: samdb_connect failed", true);
899 rootdn = ldb_dn_new(dns, dns->samdb, "");
900 if (rootdn == NULL) {
901 task_server_terminate(task, "dns: out of memory", true);
905 // TODO: this search does not work against windows
906 ret = dsdb_search(dns->samdb, dns, &res, rootdn, LDB_SCOPE_SUBTREE,
907 attrs, DSDB_SEARCH_SEARCH_ALL_PARTITIONS, "(objectClass=dnsZone)");
908 if (ret != LDB_SUCCESS) {
909 task_server_terminate(task,
910 "dns: failed to look up root DNS zones",
915 TYPESAFE_QSORT(res->msgs, res->count, dns_server_sort_zones);
917 for (i=0; i < res->count; i++) {
918 struct dns_server_zone *z;
920 z = talloc_zero(dns, struct dns_server_zone);
924 z->name = ldb_msg_find_attr_as_string(res->msgs[i], "name", NULL);
925 z->dn = talloc_move(z, &res->msgs[i]->dn);
927 DLIST_ADD_END(dns->zones, z, NULL);
930 status = dns_startup_interfaces(dns, task->lp_ctx, ifaces);
931 if (!NT_STATUS_IS_OK(status)) {
932 task_server_terminate(task, "dns failed to setup interfaces", true);
937 NTSTATUS server_service_dns_init(void)
939 return register_server_service("dns", dns_task_init);