s4 dns: Better error handling when parsing invalid or unknown records
[Samba.git] / source4 / dns_server / dns_server.c
blob08d6c5dfc203777ec39a81071fb5cb51157be914
1 /*
2 Unix SMB/CIFS implementation.
4 DNS server startup
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/>.
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"
46 /* hold information about one dns socket */
47 struct 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);
98 ssize_t zi, ni;
99 static const size_t fixup = 'a' - 'A';
101 if (zl > nl) {
102 return false;
105 for (zi = zl, ni = nl; zi >= 0; zi--, ni--) {
106 char zc = zone[zi];
107 char nc = name[ni];
109 /* convert to lower case */
110 if (zc >= 'A' && zc <= 'Z') {
111 zc += fixup;
113 if (nc >= 'A' && nc <= 'Z') {
114 nc += fixup;
117 if (zc != nc) {
118 return false;
122 if (ni >= 0) {
123 if (name[ni] != '.') {
124 return false;
127 ni--;
130 *host_part_len = ni+1;
132 return true;
135 static NTSTATUS dns_name2dn(struct dns_server *dns,
136 TALLOC_CTX *mem_ctx,
137 const char *name,
138 struct ldb_dn **_dn)
140 struct ldb_dn *base;
141 struct ldb_dn *dn;
142 const struct dns_server_zone *z;
143 size_t host_part_len = 0;
145 if (name == NULL) {
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");
155 *_dn = dn;
156 return NT_STATUS_OK;
159 for (z = dns->zones; z != NULL; z = z->next) {
160 bool match;
162 match = dns_name_match(z->name, name, &host_part_len);
163 if (match) {
164 break;
168 if (z == NULL) {
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=@");
175 *_dn = dn;
176 return NT_STATUS_OK;
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);
181 *_dn = dn;
182 return NT_STATUS_OK;
185 static NTSTATUS handle_question(struct dns_server *dns,
186 TALLOC_CTX *mem_ctx,
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;
192 NTSTATUS status;
193 static const char * const attrs[] = { "dnsRecord", NULL};
194 int ret;
195 uint16_t ai = *ancount;
196 uint16_t ri;
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]);
211 if (el == NULL) {
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) {
236 continue;
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].length = UINT16_MAX;
245 ans[ai].rdata.cname_record = talloc_strdup(ans, recs[ri].data.cname);
246 ai++;
248 break;
249 case DNS_QTYPE_A:
250 for (ri = 0; ri < el->num_values; ri++) {
251 if (recs[ri].wType != question->question_type) {
252 continue;
255 ZERO_STRUCT(ans[ai]);
256 ans[ai].name = talloc_strdup(ans, question->name);
257 ans[ai].rr_type = DNS_QTYPE_A;
258 ans[ai].rr_class = DNS_QCLASS_IP;
259 ans[ai].ttl = recs[ri].dwTtlSeconds;
260 ans[ai].length = UINT16_MAX;
261 ans[ai].rdata.ipv4_record = talloc_strdup(ans, recs[ri].data.ipv4);
262 ai++;
264 break;
265 case DNS_QTYPE_AAAA:
266 for (ri = 0; ri < el->num_values; ri++) {
267 if (recs[ri].wType != question->question_type) {
268 continue;
271 ZERO_STRUCT(ans[ai]);
272 ans[ai].name = talloc_strdup(ans, question->name);
273 ans[ai].rr_type = DNS_QTYPE_AAAA;
274 ans[ai].rr_class = DNS_QCLASS_IP;
275 ans[ai].ttl = recs[ri].dwTtlSeconds;
276 ans[ai].length = UINT16_MAX;
277 ans[ai].rdata.ipv6_record = recs[ri].data.ipv6;
278 ai++;
280 break;
281 case DNS_QTYPE_NS:
282 for (ri = 0; ri < el->num_values; ri++) {
283 if (recs[ri].wType != question->question_type) {
284 continue;
287 ZERO_STRUCT(ans[ai]);
288 ans[ai].name = question->name;
289 ans[ai].rr_type = DNS_QTYPE_NS;
290 ans[ai].rr_class = DNS_QCLASS_IP;
291 ans[ai].ttl = recs[ri].dwTtlSeconds;
292 ans[ai].length = UINT16_MAX;
293 ans[ai].rdata.ns_record = recs[ri].data.ns;
294 ai++;
296 break;
297 case DNS_QTYPE_SRV:
298 for (ri = 0; ri < el->num_values; ri++) {
299 if (recs[ri].wType != question->question_type) {
300 continue;
303 ZERO_STRUCT(ans[ai]);
304 ans[ai].name = question->name;
305 ans[ai].rr_type = DNS_QTYPE_SRV;
306 ans[ai].rr_class = DNS_QCLASS_IP;
307 ans[ai].ttl = recs[ri].dwTtlSeconds;
308 ans[ai].length = UINT16_MAX;
309 ans[ai].rdata.srv_record.priority = recs[ri].data.srv.wPriority;
310 ans[ai].rdata.srv_record.weight = recs[ri].data.srv.wWeight;
311 ans[ai].rdata.srv_record.port = recs[ri].data.srv.wPort;
312 ans[ai].rdata.srv_record.target = recs[ri].data.srv.nameTarget;
313 ai++;
315 break;
316 case DNS_QTYPE_SOA:
317 for (ri = 0; ri < el->num_values; ri++) {
318 if (recs[ri].wType != question->question_type) {
319 continue;
322 ZERO_STRUCT(ans[ai]);
323 ans[ai].name = question->name;
324 ans[ai].rr_type = DNS_QTYPE_SOA;
325 ans[ai].rr_class = DNS_QCLASS_IP;
326 ans[ai].ttl = recs[ri].dwTtlSeconds;
327 ans[ai].length = UINT16_MAX;
328 ans[ai].rdata.soa_record.mname = recs[ri].data.soa.mname;
329 ans[ai].rdata.soa_record.rname = recs[ri].data.soa.rname;
330 ans[ai].rdata.soa_record.serial = recs[ri].data.soa.serial;
331 ans[ai].rdata.soa_record.refresh= recs[ri].data.soa.refresh;
332 ans[ai].rdata.soa_record.retry = recs[ri].data.soa.retry;
333 ans[ai].rdata.soa_record.expire = recs[ri].data.soa.expire;
334 ans[ai].rdata.soa_record.minimum= recs[ri].data.soa.minimum;
335 ai++;
337 break;
338 default:
339 return NT_STATUS_NOT_IMPLEMENTED;
342 if (*ancount == ai) {
343 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
346 *ancount = ai;
347 *answers = ans;
349 return NT_STATUS_OK;
353 static NTSTATUS dns_server_process_query(struct dns_server *dns,
354 TALLOC_CTX *mem_ctx,
355 struct dns_name_packet *in,
356 struct dns_res_rec **answers, uint16_t *ancount,
357 struct dns_res_rec **nsrecs, uint16_t *nscount,
358 struct dns_res_rec **additional, uint16_t *arcount)
360 uint16_t num_answers=0;
361 struct dns_res_rec *ans=NULL;
362 int i;
363 NTSTATUS status;
365 ans = talloc_array(mem_ctx, struct dns_res_rec, 0);
366 if (answers == NULL) return NT_STATUS_NO_MEMORY;
368 for (i = 0; i < in->qdcount; ++i) {
369 status = handle_question(dns, mem_ctx, &in->questions[i], &ans, &num_answers);
370 NT_STATUS_NOT_OK_RETURN(status);
373 *answers = ans;
374 *ancount = num_answers;
376 /*FIXME: Do something for these */
377 *nsrecs = NULL;
378 *nscount = 0;
380 *additional = NULL;
381 *arcount = 0;
383 return NT_STATUS_OK;
386 static NTSTATUS dns_server_process_update(struct dns_server *dns,
387 TALLOC_CTX *mem_ctx,
388 struct dns_name_packet *in,
389 struct dns_res_rec **prereqs, uint16_t *prereq_count,
390 struct dns_res_rec **updates, uint16_t *update_count,
391 struct dns_res_rec **additional, uint16_t *arcount)
393 struct dns_name_question *zone;
395 if (in->qdcount != 1) {
396 return NT_STATUS_NOT_IMPLEMENTED;
399 zone = in->questions;
401 DEBUG(0, ("Got a dns update request.\n"));
404 return NT_STATUS_NOT_IMPLEMENTED;
407 static NTSTATUS dns_process(struct dns_server *dns,
408 TALLOC_CTX *mem_ctx,
409 DATA_BLOB *in,
410 DATA_BLOB *out)
412 enum ndr_err_code ndr_err;
413 NTSTATUS ret;
414 struct dns_name_packet *in_packet;
415 struct dns_name_packet *out_packet;
416 struct dns_res_rec *answers = NULL, *nsrecs = NULL, *additional = NULL;
417 uint16_t num_answers = 0 , num_nsrecs = 0, num_additional = 0;
418 uint16_t reply_code;
420 if (in->length < 12) {
421 return NT_STATUS_INVALID_PARAMETER;
424 in_packet = talloc_zero(mem_ctx, struct dns_name_packet);
425 /* TODO: We don't really need an out_packet. */
426 out_packet = talloc_zero(mem_ctx, struct dns_name_packet);
428 if (in_packet == NULL) return NT_STATUS_NO_MEMORY;
429 if (out_packet == NULL) return NT_STATUS_NO_MEMORY;
431 dump_data(2, in->data, in->length);
433 ndr_err = ndr_pull_struct_blob(in, in_packet, in_packet,
434 (ndr_pull_flags_fn_t)ndr_pull_dns_name_packet);
435 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
436 TALLOC_FREE(in_packet);
437 DEBUG(0, ("Failed to parse packet %d!\n", ndr_err));
438 *out = *in;
440 out->data[2] |= 0x80; /* Toggle DNS_FLAG_REPLY */
441 out->data[3] |= DNS_RCODE_FORMERR;
443 return NT_STATUS_OK;
446 NDR_PRINT_DEBUG(dns_name_packet, in_packet);
447 *out_packet = *in_packet;
448 out_packet->operation |= DNS_FLAG_REPLY;
450 switch (in_packet->operation & DNS_OPCODE) {
451 case DNS_OPCODE_QUERY:
453 ret = dns_server_process_query(dns, out_packet, in_packet,
454 &answers, &num_answers,
455 &nsrecs, &num_nsrecs,
456 &additional, &num_additional);
457 reply_code = DNS_RCODE_NXDOMAIN;
459 break;
460 case DNS_OPCODE_REGISTER:
461 ret = dns_server_process_update(dns, out_packet, in_packet,
462 &answers, &num_answers,
463 &nsrecs, &num_nsrecs,
464 &additional, &num_additional);
465 reply_code = DNS_RCODE_NOTIMP;
466 break;
467 default:
468 ret = NT_STATUS_NOT_IMPLEMENTED;
469 reply_code = DNS_RCODE_NOTIMP;
470 break;
473 if (NT_STATUS_IS_OK(ret)) {
474 out_packet->ancount = num_answers;
475 out_packet->answers = answers;
477 out_packet->nscount = num_nsrecs;
478 out_packet->nsrecs = nsrecs;
480 out_packet->arcount = num_additional;
481 out_packet->additional = additional;
482 } else {
483 out_packet->operation |= reply_code;
486 NDR_PRINT_DEBUG(dns_name_packet, out_packet);
487 ndr_err = ndr_push_struct_blob(out, out_packet, out_packet,
488 (ndr_push_flags_fn_t)ndr_push_dns_name_packet);
489 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
490 TALLOC_FREE(in_packet);
491 TALLOC_FREE(out_packet);
492 DEBUG(0, ("Failed to push packet %d!\n", ndr_err));
493 *out = *in;
495 out->data[2] |= 0x80; /* Toggle DNS_FLAG_REPLY */
496 out->data[3] |= DNS_RCODE_SERVFAIL;
498 return NT_STATUS_OK;
501 dump_data(2, out->data, out->length);
502 return NT_STATUS_OK;
505 struct dns_tcp_call {
506 struct dns_tcp_connection *dns_conn;
507 DATA_BLOB in;
508 DATA_BLOB out;
509 uint8_t out_hdr[4];
510 struct iovec out_iov[2];
513 static void dns_tcp_call_writev_done(struct tevent_req *subreq);
515 static void dns_tcp_call_loop(struct tevent_req *subreq)
517 struct dns_tcp_connection *dns_conn = tevent_req_callback_data(subreq,
518 struct dns_tcp_connection);
519 struct dns_tcp_call *call;
520 NTSTATUS status;
522 call = talloc(dns_conn, struct dns_tcp_call);
523 if (call == NULL) {
524 dns_tcp_terminate_connection(dns_conn, "dns_tcp_call_loop: "
525 "no memory for dns_tcp_call");
526 return;
528 call->dns_conn = dns_conn;
530 status = tstream_read_pdu_blob_recv(subreq,
531 call,
532 &call->in);
533 TALLOC_FREE(subreq);
534 if (!NT_STATUS_IS_OK(status)) {
535 const char *reason;
537 reason = talloc_asprintf(call, "dns_tcp_call_loop: "
538 "tstream_read_pdu_blob_recv() - %s",
539 nt_errstr(status));
540 if (!reason) {
541 reason = nt_errstr(status);
544 dns_tcp_terminate_connection(dns_conn, reason);
545 return;
548 DEBUG(10,("Received krb5 TCP packet of length %lu from %s\n",
549 (long) call->in.length,
550 tsocket_address_string(dns_conn->conn->remote_address, call)));
552 /* skip length header */
553 call->in.data +=4;
554 call->in.length -= 4;
556 /* Call dns */
557 status = dns_process(dns_conn->dns_socket->dns, call, &call->in, &call->out);
558 if (!NT_STATUS_IS_OK(status)) {
559 DEBUG(0, ("dns_process returned %s\n", nt_errstr(status)));
560 dns_tcp_terminate_connection(dns_conn,
561 "dns_tcp_call_loop: process function failed");
562 return;
565 /* First add the length of the out buffer */
566 RSIVAL(call->out_hdr, 0, call->out.length);
567 call->out_iov[0].iov_base = (char *) call->out_hdr;
568 call->out_iov[0].iov_len = 4;
570 call->out_iov[1].iov_base = (char *) call->out.data;
571 call->out_iov[1].iov_len = call->out.length;
573 subreq = tstream_writev_queue_send(call,
574 dns_conn->conn->event.ctx,
575 dns_conn->tstream,
576 dns_conn->send_queue,
577 call->out_iov, 2);
578 if (subreq == NULL) {
579 dns_tcp_terminate_connection(dns_conn, "dns_tcp_call_loop: "
580 "no memory for tstream_writev_queue_send");
581 return;
583 tevent_req_set_callback(subreq, dns_tcp_call_writev_done, call);
586 * The krb5 tcp pdu's has the length as 4 byte (initial_read_size),
587 * packet_full_request_u32 provides the pdu length then.
589 subreq = tstream_read_pdu_blob_send(dns_conn,
590 dns_conn->conn->event.ctx,
591 dns_conn->tstream,
592 4, /* initial_read_size */
593 packet_full_request_u32,
594 dns_conn);
595 if (subreq == NULL) {
596 dns_tcp_terminate_connection(dns_conn, "dns_tcp_call_loop: "
597 "no memory for tstream_read_pdu_blob_send");
598 return;
600 tevent_req_set_callback(subreq, dns_tcp_call_loop, dns_conn);
603 static void dns_tcp_call_writev_done(struct tevent_req *subreq)
605 struct dns_tcp_call *call = tevent_req_callback_data(subreq,
606 struct dns_tcp_call);
607 int sys_errno;
608 int rc;
610 rc = tstream_writev_queue_recv(subreq, &sys_errno);
611 TALLOC_FREE(subreq);
612 if (rc == -1) {
613 const char *reason;
615 reason = talloc_asprintf(call, "dns_tcp_call_writev_done: "
616 "tstream_writev_queue_recv() - %d:%s",
617 sys_errno, strerror(sys_errno));
618 if (!reason) {
619 reason = "dns_tcp_call_writev_done: tstream_writev_queue_recv() failed";
622 dns_tcp_terminate_connection(call->dns_conn, reason);
623 return;
626 /* We don't care about errors */
628 talloc_free(call);
632 called when we get a new connection
634 static void dns_tcp_accept(struct stream_connection *conn)
636 struct dns_socket *dns_socket;
637 struct dns_tcp_connection *dns_conn;
638 struct tevent_req *subreq;
639 int rc;
641 dns_conn = talloc_zero(conn, struct dns_tcp_connection);
642 if (dns_conn == NULL) {
643 stream_terminate_connection(conn,
644 "dns_tcp_accept: out of memory");
645 return;
648 dns_conn->send_queue = tevent_queue_create(conn, "dns_tcp_accept");
649 if (dns_conn->send_queue == NULL) {
650 stream_terminate_connection(conn,
651 "dns_tcp_accept: out of memory");
652 return;
655 dns_socket = talloc_get_type(conn->private_data, struct dns_socket);
657 TALLOC_FREE(conn->event.fde);
659 rc = tstream_bsd_existing_socket(dns_conn,
660 socket_get_fd(conn->socket),
661 &dns_conn->tstream);
662 if (rc < 0) {
663 stream_terminate_connection(conn,
664 "dns_tcp_accept: out of memory");
665 return;
668 dns_conn->conn = conn;
669 dns_conn->dns_socket = dns_socket;
670 conn->private_data = dns_conn;
673 * The krb5 tcp pdu's has the length as 4 byte (initial_read_size),
674 * packet_full_request_u32 provides the pdu length then.
676 subreq = tstream_read_pdu_blob_send(dns_conn,
677 dns_conn->conn->event.ctx,
678 dns_conn->tstream,
679 4, /* initial_read_size */
680 packet_full_request_u32,
681 dns_conn);
682 if (subreq == NULL) {
683 dns_tcp_terminate_connection(dns_conn, "dns_tcp_accept: "
684 "no memory for tstream_read_pdu_blob_send");
685 return;
687 tevent_req_set_callback(subreq, dns_tcp_call_loop, dns_conn);
690 static const struct stream_server_ops dns_tcp_stream_ops = {
691 .name = "dns_tcp",
692 .accept_connection = dns_tcp_accept,
693 .recv_handler = dns_tcp_recv,
694 .send_handler = dns_tcp_send
697 struct dns_udp_call {
698 struct tsocket_address *src;
699 DATA_BLOB in;
700 DATA_BLOB out;
703 static void dns_udp_call_sendto_done(struct tevent_req *subreq);
705 static void dns_udp_call_loop(struct tevent_req *subreq)
707 struct dns_udp_socket *sock = tevent_req_callback_data(subreq,
708 struct dns_udp_socket);
709 struct dns_udp_call *call;
710 uint8_t *buf;
711 ssize_t len;
712 int sys_errno;
713 NTSTATUS status;
715 call = talloc(sock, struct dns_udp_call);
716 if (call == NULL) {
717 talloc_free(call);
718 goto done;
721 len = tdgram_recvfrom_recv(subreq, &sys_errno,
722 call, &buf, &call->src);
723 TALLOC_FREE(subreq);
724 if (len == -1) {
725 talloc_free(call);
726 goto done;
729 call->in.data = buf;
730 call->in.length = len;
732 DEBUG(10,("Received krb5 UDP packet of length %lu from %s\n",
733 (long)call->in.length,
734 tsocket_address_string(call->src, call)));
736 /* Call krb5 */
737 status = dns_process(sock->dns_socket->dns, call, &call->in, &call->out);
738 if (!NT_STATUS_IS_OK(status)) {
739 talloc_free(call);
740 DEBUG(0, ("dns_process returned %s\n", nt_errstr(status)));
741 goto done;
744 subreq = tdgram_sendto_queue_send(call,
745 sock->dns_socket->dns->task->event_ctx,
746 sock->dgram,
747 sock->send_queue,
748 call->out.data,
749 call->out.length,
750 call->src);
751 if (subreq == NULL) {
752 talloc_free(call);
753 goto done;
755 tevent_req_set_callback(subreq, dns_udp_call_sendto_done, call);
757 done:
758 subreq = tdgram_recvfrom_send(sock,
759 sock->dns_socket->dns->task->event_ctx,
760 sock->dgram);
761 if (subreq == NULL) {
762 task_server_terminate(sock->dns_socket->dns->task,
763 "no memory for tdgram_recvfrom_send",
764 true);
765 return;
767 tevent_req_set_callback(subreq, dns_udp_call_loop, sock);
770 static void dns_udp_call_sendto_done(struct tevent_req *subreq)
772 struct dns_udp_call *call = tevent_req_callback_data(subreq,
773 struct dns_udp_call);
774 ssize_t ret;
775 int sys_errno;
777 ret = tdgram_sendto_queue_recv(subreq, &sys_errno);
779 /* We don't care about errors */
781 talloc_free(call);
785 start listening on the given address
787 static NTSTATUS dns_add_socket(struct dns_server *dns,
788 const struct model_ops *model_ops,
789 const char *name,
790 const char *address,
791 uint16_t port)
793 struct dns_socket *dns_socket;
794 struct dns_udp_socket *dns_udp_socket;
795 struct tevent_req *udpsubreq;
796 NTSTATUS status;
797 int ret;
799 dns_socket = talloc(dns, struct dns_socket);
800 NT_STATUS_HAVE_NO_MEMORY(dns_socket);
802 dns_socket->dns = dns;
804 ret = tsocket_address_inet_from_strings(dns_socket, "ip",
805 address, port,
806 &dns_socket->local_address);
807 if (ret != 0) {
808 status = map_nt_error_from_unix(errno);
809 return status;
812 status = stream_setup_socket(dns->task->event_ctx,
813 dns->task->lp_ctx,
814 model_ops,
815 &dns_tcp_stream_ops,
816 "ip", address, &port,
817 lpcfg_socket_options(dns->task->lp_ctx),
818 dns_socket);
819 if (!NT_STATUS_IS_OK(status)) {
820 DEBUG(0,("Failed to bind to %s:%u TCP - %s\n",
821 address, port, nt_errstr(status)));
822 talloc_free(dns_socket);
823 return status;
826 dns_udp_socket = talloc(dns_socket, struct dns_udp_socket);
827 NT_STATUS_HAVE_NO_MEMORY(dns_udp_socket);
829 dns_udp_socket->dns_socket = dns_socket;
831 ret = tdgram_inet_udp_socket(dns_socket->local_address,
832 NULL,
833 dns_udp_socket,
834 &dns_udp_socket->dgram);
835 if (ret != 0) {
836 status = map_nt_error_from_unix(errno);
837 DEBUG(0,("Failed to bind to %s:%u UDP - %s\n",
838 address, port, nt_errstr(status)));
839 return status;
842 dns_udp_socket->send_queue = tevent_queue_create(dns_udp_socket,
843 "dns_udp_send_queue");
844 NT_STATUS_HAVE_NO_MEMORY(dns_udp_socket->send_queue);
846 udpsubreq = tdgram_recvfrom_send(dns_udp_socket,
847 dns->task->event_ctx,
848 dns_udp_socket->dgram);
849 NT_STATUS_HAVE_NO_MEMORY(udpsubreq);
850 tevent_req_set_callback(udpsubreq, dns_udp_call_loop, dns_udp_socket);
852 return NT_STATUS_OK;
856 setup our listening sockets on the configured network interfaces
858 static NTSTATUS dns_startup_interfaces(struct dns_server *dns, struct loadparm_context *lp_ctx,
859 struct interface *ifaces)
861 const struct model_ops *model_ops;
862 int num_interfaces;
863 TALLOC_CTX *tmp_ctx = talloc_new(dns);
864 NTSTATUS status;
865 int i;
867 /* within the dns task we want to be a single process, so
868 ask for the single process model ops and pass these to the
869 stream_setup_socket() call. */
870 model_ops = process_model_startup(dns->task->event_ctx, "single");
871 if (!model_ops) {
872 DEBUG(0,("Can't find 'single' process model_ops\n"));
873 return NT_STATUS_INTERNAL_ERROR;
876 num_interfaces = iface_count(ifaces);
878 for (i=0; i<num_interfaces; i++) {
879 const char *address = talloc_strdup(tmp_ctx, iface_n_ip(ifaces, i));
881 status = dns_add_socket(dns, model_ops, "dns", address, DNS_SERVICE_PORT);
882 NT_STATUS_NOT_OK_RETURN(status);
885 talloc_free(tmp_ctx);
887 return NT_STATUS_OK;
890 static int dns_server_sort_zones(struct ldb_message **m1, struct ldb_message **m2)
892 const char *n1, *n2;
893 size_t l1, l2;
895 n1 = ldb_msg_find_attr_as_string(*m1, "name", NULL);
896 n2 = ldb_msg_find_attr_as_string(*m2, "name", NULL);
898 l1 = strlen(n1);
899 l2 = strlen(n2);
901 /* If the string lengths are not equal just sort by length */
902 if (l1 != l2) {
903 /* If m1 is the larger zone name, return it first */
904 return l2 - l1;
907 /*TODO: We need to compare DNs here, we want the DomainDNSZones first */
908 return 0;
911 static void dns_task_init(struct task_server *task)
913 struct dns_server *dns;
914 NTSTATUS status;
915 struct interface *ifaces;
916 int ret;
917 struct ldb_result *res;
918 struct ldb_dn *rootdn;
919 static const char * const attrs[] = { "name", NULL};
920 int i;
923 switch (lpcfg_server_role(task->lp_ctx)) {
924 case ROLE_STANDALONE:
925 task_server_terminate(task, "dns: no DNS required in standalone configuration", false);
926 return;
927 case ROLE_DOMAIN_MEMBER:
928 task_server_terminate(task, "dns: no DNS required in member server configuration", false);
929 return;
930 case ROLE_DOMAIN_CONTROLLER:
931 /* Yes, we want a DNS */
932 break;
935 load_interfaces(task, lpcfg_interfaces(task->lp_ctx), &ifaces);
937 if (iface_count(ifaces) == 0) {
938 task_server_terminate(task, "dns: no network interfaces configured", false);
939 return;
942 task_server_set_title(task, "task[dns]");
944 dns = talloc_zero(task, struct dns_server);
945 if (dns == NULL) {
946 task_server_terminate(task, "dns: out of memory", true);
947 return;
950 dns->task = task;
952 dns->samdb = samdb_connect(dns, dns->task->event_ctx, dns->task->lp_ctx,
953 system_session(dns->task->lp_ctx), 0);
954 if (!dns->samdb) {
955 task_server_terminate(task, "dns: samdb_connect failed", true);
956 return;
959 rootdn = ldb_dn_new(dns, dns->samdb, "");
960 if (rootdn == NULL) {
961 task_server_terminate(task, "dns: out of memory", true);
962 return;
965 // TODO: this search does not work against windows
966 ret = dsdb_search(dns->samdb, dns, &res, rootdn, LDB_SCOPE_SUBTREE,
967 attrs, DSDB_SEARCH_SEARCH_ALL_PARTITIONS, "(objectClass=dnsZone)");
968 if (ret != LDB_SUCCESS) {
969 task_server_terminate(task,
970 "dns: failed to look up root DNS zones",
971 true);
972 return;
975 TYPESAFE_QSORT(res->msgs, res->count, dns_server_sort_zones);
977 for (i=0; i < res->count; i++) {
978 struct dns_server_zone *z;
980 z = talloc_zero(dns, struct dns_server_zone);
981 if (z == NULL) {
984 z->name = ldb_msg_find_attr_as_string(res->msgs[i], "name", NULL);
985 z->dn = talloc_move(z, &res->msgs[i]->dn);
987 DLIST_ADD_END(dns->zones, z, NULL);
990 status = dns_startup_interfaces(dns, task->lp_ctx, ifaces);
991 if (!NT_STATUS_IS_OK(status)) {
992 task_server_terminate(task, "dns failed to setup interfaces", true);
993 return;
997 NTSTATUS server_service_dns_init(void)
999 return register_server_service("dns", dns_task_init);