s4-dns: Fix an unlikely potential memleak
[Samba/gebeck_regimport.git] / source4 / dns_server / dns_server.c
blob28c57c0f6ebad9560c663d6e52043d047cdfd732
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 NTSTATUS server_service_dns_init(void);
48 /* hold information about one dns socket */
49 struct dns_socket {
50 struct dns_server *dns;
51 struct tsocket_address *local_address;
54 struct dns_udp_socket {
55 struct dns_socket *dns_socket;
56 struct tdgram_context *dgram;
57 struct tevent_queue *send_queue;
61 state of an open tcp connection
63 struct dns_tcp_connection {
64 /* stream connection we belong to */
65 struct stream_connection *conn;
67 /* the dns_server the connection belongs to */
68 struct dns_socket *dns_socket;
70 struct tstream_context *tstream;
72 struct tevent_queue *send_queue;
75 static void dns_tcp_terminate_connection(struct dns_tcp_connection *dnsconn, const char *reason)
77 stream_terminate_connection(dnsconn->conn, reason);
80 static void dns_tcp_recv(struct stream_connection *conn, uint16_t flags)
82 struct dns_tcp_connection *dnsconn = talloc_get_type(conn->private_data,
83 struct dns_tcp_connection);
84 /* this should never be triggered! */
85 dns_tcp_terminate_connection(dnsconn, "dns_tcp_recv: called");
88 static void dns_tcp_send(struct stream_connection *conn, uint16_t flags)
90 struct dns_tcp_connection *dnsconn = talloc_get_type(conn->private_data,
91 struct dns_tcp_connection);
92 /* this should never be triggered! */
93 dns_tcp_terminate_connection(dnsconn, "dns_tcp_send: called");
96 static NTSTATUS dns_process(struct dns_server *dns,
97 TALLOC_CTX *mem_ctx,
98 DATA_BLOB *in,
99 DATA_BLOB *out)
101 enum ndr_err_code ndr_err;
102 WERROR ret;
103 uint16_t dns_err = DNS_RCODE_OK;
104 struct dns_request_state *state;
105 struct dns_name_packet *in_packet;
106 struct dns_name_packet *out_packet;
107 struct dns_res_rec *answers = NULL, *nsrecs = NULL, *additional = NULL;
108 uint16_t num_answers = 0 , num_nsrecs = 0, num_additional = 0;
110 if (in->length < 12) {
111 return NT_STATUS_INVALID_PARAMETER;
114 state = talloc_zero(mem_ctx, struct dns_request_state);
116 in_packet = talloc_zero(state, struct dns_name_packet);
117 /* TODO: We don't really need an out_packet. */
118 out_packet = talloc_zero(state, struct dns_name_packet);
120 if ((state == NULL) || (in_packet == NULL) || (out_packet == NULL)) {
121 TALLOC_FREE(state);
122 return NT_STATUS_NO_MEMORY;
125 dump_data(8, in->data, in->length);
127 ndr_err = ndr_pull_struct_blob(in, in_packet, in_packet,
128 (ndr_pull_flags_fn_t)ndr_pull_dns_name_packet);
129 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
130 TALLOC_FREE(in_packet);
131 DEBUG(0, ("Failed to parse packet %d!\n", ndr_err));
132 dns_err = DNS_RCODE_FORMERR;
133 goto drop;
135 if (DEBUGLVL(8)) {
136 NDR_PRINT_DEBUG(dns_name_packet, in_packet);
138 *out_packet = *in_packet;
139 state->flags |= in_packet->operation | DNS_FLAG_REPLY;
141 if (lpcfg_dns_recursive_queries(dns->task->lp_ctx)) {
142 state->flags |= DNS_FLAG_RECURSION_AVAIL;
145 switch (in_packet->operation & DNS_OPCODE) {
146 case DNS_OPCODE_QUERY:
148 ret = dns_server_process_query(dns, state,
149 out_packet, in_packet,
150 &answers, &num_answers,
151 &nsrecs, &num_nsrecs,
152 &additional, &num_additional);
154 break;
155 case DNS_OPCODE_UPDATE:
156 ret = dns_server_process_update(dns, state,
157 out_packet, in_packet,
158 &answers, &num_answers,
159 &nsrecs, &num_nsrecs,
160 &additional, &num_additional);
161 break;
162 default:
163 ret = WERR_DNS_ERROR_RCODE_NOT_IMPLEMENTED;
164 break;
167 if (W_ERROR_IS_OK(ret)) {
168 out_packet->ancount = num_answers;
169 out_packet->answers = answers;
171 out_packet->nscount = num_nsrecs;
172 out_packet->nsrecs = nsrecs;
174 out_packet->arcount = num_additional;
175 out_packet->additional = additional;
176 } else {
177 out_packet->operation |= werr_to_dns_err(ret);
180 out_packet->operation |= state->flags;
182 if (DEBUGLVL(8)) {
183 NDR_PRINT_DEBUG(dns_name_packet, out_packet);
185 ndr_err = ndr_push_struct_blob(out, out_packet, out_packet,
186 (ndr_push_flags_fn_t)ndr_push_dns_name_packet);
187 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
188 TALLOC_FREE(in_packet);
189 TALLOC_FREE(out_packet);
190 DEBUG(0, ("Failed to push packet %d!\n", ndr_err));
191 dns_err = DNS_RCODE_SERVFAIL;
192 goto drop;
195 dump_data(8, out->data, out->length);
196 return NT_STATUS_OK;
198 drop:
199 *out = *in;
200 out->data[2] |= 0x80; /* Toggle DNS_FLAG_REPLY */
201 out->data[3] |= dns_err;
202 return NT_STATUS_OK;
205 struct dns_tcp_call {
206 struct dns_tcp_connection *dns_conn;
207 DATA_BLOB in;
208 DATA_BLOB out;
209 uint8_t out_hdr[4];
210 struct iovec out_iov[2];
213 static void dns_tcp_call_writev_done(struct tevent_req *subreq);
215 static void dns_tcp_call_loop(struct tevent_req *subreq)
217 struct dns_tcp_connection *dns_conn = tevent_req_callback_data(subreq,
218 struct dns_tcp_connection);
219 struct dns_tcp_call *call;
220 NTSTATUS status;
222 call = talloc(dns_conn, struct dns_tcp_call);
223 if (call == NULL) {
224 dns_tcp_terminate_connection(dns_conn, "dns_tcp_call_loop: "
225 "no memory for dns_tcp_call");
226 return;
228 call->dns_conn = dns_conn;
230 status = tstream_read_pdu_blob_recv(subreq,
231 call,
232 &call->in);
233 TALLOC_FREE(subreq);
234 if (!NT_STATUS_IS_OK(status)) {
235 const char *reason;
237 reason = talloc_asprintf(call, "dns_tcp_call_loop: "
238 "tstream_read_pdu_blob_recv() - %s",
239 nt_errstr(status));
240 if (!reason) {
241 reason = nt_errstr(status);
244 dns_tcp_terminate_connection(dns_conn, reason);
245 return;
248 DEBUG(10,("Received DNS TCP packet of length %lu from %s\n",
249 (long) call->in.length,
250 tsocket_address_string(dns_conn->conn->remote_address, call)));
252 /* skip length header */
253 call->in.data += 2;
254 call->in.length -= 2;
256 /* Call dns */
257 status = dns_process(dns_conn->dns_socket->dns, call, &call->in, &call->out);
258 if (!NT_STATUS_IS_OK(status)) {
259 DEBUG(0, ("dns_process returned %s\n", nt_errstr(status)));
260 dns_tcp_terminate_connection(dns_conn,
261 "dns_tcp_call_loop: process function failed");
262 return;
265 /* First add the length of the out buffer */
266 RSSVAL(call->out_hdr, 0, call->out.length);
267 call->out_iov[0].iov_base = (char *) call->out_hdr;
268 call->out_iov[0].iov_len = 2;
270 call->out_iov[1].iov_base = (char *) call->out.data;
271 call->out_iov[1].iov_len = call->out.length;
273 subreq = tstream_writev_queue_send(call,
274 dns_conn->conn->event.ctx,
275 dns_conn->tstream,
276 dns_conn->send_queue,
277 call->out_iov, 2);
278 if (subreq == NULL) {
279 dns_tcp_terminate_connection(dns_conn, "dns_tcp_call_loop: "
280 "no memory for tstream_writev_queue_send");
281 return;
283 tevent_req_set_callback(subreq, dns_tcp_call_writev_done, call);
286 * The dns tcp pdu's has the length as 2 byte (initial_read_size),
287 * packet_full_request_u16 provides the pdu length then.
289 subreq = tstream_read_pdu_blob_send(dns_conn,
290 dns_conn->conn->event.ctx,
291 dns_conn->tstream,
292 2, /* initial_read_size */
293 packet_full_request_u16,
294 dns_conn);
295 if (subreq == NULL) {
296 dns_tcp_terminate_connection(dns_conn, "dns_tcp_call_loop: "
297 "no memory for tstream_read_pdu_blob_send");
298 return;
300 tevent_req_set_callback(subreq, dns_tcp_call_loop, dns_conn);
303 static void dns_tcp_call_writev_done(struct tevent_req *subreq)
305 struct dns_tcp_call *call = tevent_req_callback_data(subreq,
306 struct dns_tcp_call);
307 int sys_errno;
308 int rc;
310 rc = tstream_writev_queue_recv(subreq, &sys_errno);
311 TALLOC_FREE(subreq);
312 if (rc == -1) {
313 const char *reason;
315 reason = talloc_asprintf(call, "dns_tcp_call_writev_done: "
316 "tstream_writev_queue_recv() - %d:%s",
317 sys_errno, strerror(sys_errno));
318 if (!reason) {
319 reason = "dns_tcp_call_writev_done: tstream_writev_queue_recv() failed";
322 dns_tcp_terminate_connection(call->dns_conn, reason);
323 return;
326 /* We don't care about errors */
328 talloc_free(call);
332 called when we get a new connection
334 static void dns_tcp_accept(struct stream_connection *conn)
336 struct dns_socket *dns_socket;
337 struct dns_tcp_connection *dns_conn;
338 struct tevent_req *subreq;
339 int rc;
341 dns_conn = talloc_zero(conn, struct dns_tcp_connection);
342 if (dns_conn == NULL) {
343 stream_terminate_connection(conn,
344 "dns_tcp_accept: out of memory");
345 return;
348 dns_conn->send_queue = tevent_queue_create(conn, "dns_tcp_accept");
349 if (dns_conn->send_queue == NULL) {
350 stream_terminate_connection(conn,
351 "dns_tcp_accept: out of memory");
352 return;
355 dns_socket = talloc_get_type(conn->private_data, struct dns_socket);
357 TALLOC_FREE(conn->event.fde);
359 rc = tstream_bsd_existing_socket(dns_conn,
360 socket_get_fd(conn->socket),
361 &dns_conn->tstream);
362 if (rc < 0) {
363 stream_terminate_connection(conn,
364 "dns_tcp_accept: out of memory");
365 return;
368 dns_conn->conn = conn;
369 dns_conn->dns_socket = dns_socket;
370 conn->private_data = dns_conn;
373 * The dns tcp pdu's has the length as 2 byte (initial_read_size),
374 * packet_full_request_u16 provides the pdu length then.
376 subreq = tstream_read_pdu_blob_send(dns_conn,
377 dns_conn->conn->event.ctx,
378 dns_conn->tstream,
379 2, /* initial_read_size */
380 packet_full_request_u16,
381 dns_conn);
382 if (subreq == NULL) {
383 dns_tcp_terminate_connection(dns_conn, "dns_tcp_accept: "
384 "no memory for tstream_read_pdu_blob_send");
385 return;
387 tevent_req_set_callback(subreq, dns_tcp_call_loop, dns_conn);
390 static const struct stream_server_ops dns_tcp_stream_ops = {
391 .name = "dns_tcp",
392 .accept_connection = dns_tcp_accept,
393 .recv_handler = dns_tcp_recv,
394 .send_handler = dns_tcp_send
397 struct dns_udp_call {
398 struct tsocket_address *src;
399 DATA_BLOB in;
400 DATA_BLOB out;
403 static void dns_udp_call_sendto_done(struct tevent_req *subreq);
405 static void dns_udp_call_loop(struct tevent_req *subreq)
407 struct dns_udp_socket *sock = tevent_req_callback_data(subreq,
408 struct dns_udp_socket);
409 struct dns_udp_call *call;
410 uint8_t *buf;
411 ssize_t len;
412 int sys_errno;
413 NTSTATUS status;
415 call = talloc(sock, struct dns_udp_call);
416 if (call == NULL) {
417 talloc_free(call);
418 goto done;
421 len = tdgram_recvfrom_recv(subreq, &sys_errno,
422 call, &buf, &call->src);
423 TALLOC_FREE(subreq);
424 if (len == -1) {
425 talloc_free(call);
426 goto done;
429 call->in.data = buf;
430 call->in.length = len;
432 DEBUG(10,("Received DNS UDP packet of length %lu from %s\n",
433 (long)call->in.length,
434 tsocket_address_string(call->src, call)));
436 /* Call dns_process */
437 status = dns_process(sock->dns_socket->dns, call, &call->in, &call->out);
438 if (!NT_STATUS_IS_OK(status)) {
439 talloc_free(call);
440 DEBUG(0, ("dns_process returned %s\n", nt_errstr(status)));
441 goto done;
444 subreq = tdgram_sendto_queue_send(call,
445 sock->dns_socket->dns->task->event_ctx,
446 sock->dgram,
447 sock->send_queue,
448 call->out.data,
449 call->out.length,
450 call->src);
451 if (subreq == NULL) {
452 talloc_free(call);
453 goto done;
455 tevent_req_set_callback(subreq, dns_udp_call_sendto_done, call);
457 done:
458 subreq = tdgram_recvfrom_send(sock,
459 sock->dns_socket->dns->task->event_ctx,
460 sock->dgram);
461 if (subreq == NULL) {
462 task_server_terminate(sock->dns_socket->dns->task,
463 "no memory for tdgram_recvfrom_send",
464 true);
465 return;
467 tevent_req_set_callback(subreq, dns_udp_call_loop, sock);
470 static void dns_udp_call_sendto_done(struct tevent_req *subreq)
472 struct dns_udp_call *call = tevent_req_callback_data(subreq,
473 struct dns_udp_call);
474 ssize_t ret;
475 int sys_errno;
477 ret = tdgram_sendto_queue_recv(subreq, &sys_errno);
479 /* We don't care about errors */
481 talloc_free(call);
485 start listening on the given address
487 static NTSTATUS dns_add_socket(struct dns_server *dns,
488 const struct model_ops *model_ops,
489 const char *name,
490 const char *address,
491 uint16_t port)
493 struct dns_socket *dns_socket;
494 struct dns_udp_socket *dns_udp_socket;
495 struct tevent_req *udpsubreq;
496 NTSTATUS status;
497 int ret;
499 dns_socket = talloc(dns, struct dns_socket);
500 NT_STATUS_HAVE_NO_MEMORY(dns_socket);
502 dns_socket->dns = dns;
504 ret = tsocket_address_inet_from_strings(dns_socket, "ip",
505 address, port,
506 &dns_socket->local_address);
507 if (ret != 0) {
508 status = map_nt_error_from_unix_common(errno);
509 return status;
512 status = stream_setup_socket(dns->task,
513 dns->task->event_ctx,
514 dns->task->lp_ctx,
515 model_ops,
516 &dns_tcp_stream_ops,
517 "ip", address, &port,
518 lpcfg_socket_options(dns->task->lp_ctx),
519 dns_socket);
520 if (!NT_STATUS_IS_OK(status)) {
521 DEBUG(0,("Failed to bind to %s:%u TCP - %s\n",
522 address, port, nt_errstr(status)));
523 talloc_free(dns_socket);
524 return status;
527 dns_udp_socket = talloc(dns_socket, struct dns_udp_socket);
528 NT_STATUS_HAVE_NO_MEMORY(dns_udp_socket);
530 dns_udp_socket->dns_socket = dns_socket;
532 ret = tdgram_inet_udp_socket(dns_socket->local_address,
533 NULL,
534 dns_udp_socket,
535 &dns_udp_socket->dgram);
536 if (ret != 0) {
537 status = map_nt_error_from_unix_common(errno);
538 DEBUG(0,("Failed to bind to %s:%u UDP - %s\n",
539 address, port, nt_errstr(status)));
540 return status;
543 dns_udp_socket->send_queue = tevent_queue_create(dns_udp_socket,
544 "dns_udp_send_queue");
545 NT_STATUS_HAVE_NO_MEMORY(dns_udp_socket->send_queue);
547 udpsubreq = tdgram_recvfrom_send(dns_udp_socket,
548 dns->task->event_ctx,
549 dns_udp_socket->dgram);
550 NT_STATUS_HAVE_NO_MEMORY(udpsubreq);
551 tevent_req_set_callback(udpsubreq, dns_udp_call_loop, dns_udp_socket);
553 return NT_STATUS_OK;
557 setup our listening sockets on the configured network interfaces
559 static NTSTATUS dns_startup_interfaces(struct dns_server *dns, struct loadparm_context *lp_ctx,
560 struct interface *ifaces)
562 const struct model_ops *model_ops;
563 int num_interfaces;
564 TALLOC_CTX *tmp_ctx = talloc_new(dns);
565 NTSTATUS status;
566 int i;
568 /* within the dns task we want to be a single process, so
569 ask for the single process model ops and pass these to the
570 stream_setup_socket() call. */
571 model_ops = process_model_startup("single");
572 if (!model_ops) {
573 DEBUG(0,("Can't find 'single' process model_ops\n"));
574 return NT_STATUS_INTERNAL_ERROR;
577 num_interfaces = iface_list_count(ifaces);
579 for (i=0; i<num_interfaces; i++) {
580 const char *address = talloc_strdup(tmp_ctx, iface_list_n_ip(ifaces, i));
582 status = dns_add_socket(dns, model_ops, "dns", address, DNS_SERVICE_PORT);
583 NT_STATUS_NOT_OK_RETURN(status);
586 talloc_free(tmp_ctx);
588 return NT_STATUS_OK;
591 static int dns_server_sort_zones(struct ldb_message **m1, struct ldb_message **m2)
593 const char *n1, *n2;
594 size_t l1, l2;
596 n1 = ldb_msg_find_attr_as_string(*m1, "name", NULL);
597 n2 = ldb_msg_find_attr_as_string(*m2, "name", NULL);
599 l1 = strlen(n1);
600 l2 = strlen(n2);
602 /* If the string lengths are not equal just sort by length */
603 if (l1 != l2) {
604 /* If m1 is the larger zone name, return it first */
605 return l2 - l1;
608 /*TODO: We need to compare DNs here, we want the DomainDNSZones first */
609 return 0;
612 static void dns_task_init(struct task_server *task)
614 struct dns_server *dns;
615 NTSTATUS status;
616 struct interface *ifaces;
617 int ret;
618 struct ldb_result *res;
619 static const char * const attrs[] = { "name", NULL};
620 unsigned int i;
622 switch (lpcfg_server_role(task->lp_ctx)) {
623 case ROLE_STANDALONE:
624 task_server_terminate(task, "dns: no DNS required in standalone configuration", false);
625 return;
626 case ROLE_DOMAIN_MEMBER:
627 task_server_terminate(task, "dns: no DNS required in member server configuration", false);
628 return;
629 case ROLE_DOMAIN_CONTROLLER:
630 /* Yes, we want a DNS */
631 break;
634 load_interface_list(task, task->lp_ctx, &ifaces);
636 if (iface_list_count(ifaces) == 0) {
637 task_server_terminate(task, "dns: no network interfaces configured", false);
638 return;
641 task_server_set_title(task, "task[dns]");
643 dns = talloc_zero(task, struct dns_server);
644 if (dns == NULL) {
645 task_server_terminate(task, "dns: out of memory", true);
646 return;
649 dns->task = task;
651 dns->samdb = samdb_connect(dns, dns->task->event_ctx, dns->task->lp_ctx,
652 system_session(dns->task->lp_ctx), 0);
653 if (!dns->samdb) {
654 task_server_terminate(task, "dns: samdb_connect failed", true);
655 return;
658 // TODO: this search does not work against windows
659 ret = dsdb_search(dns->samdb, dns, &res, NULL, LDB_SCOPE_SUBTREE,
660 attrs, DSDB_SEARCH_SEARCH_ALL_PARTITIONS, "(objectClass=dnsZone)");
661 if (ret != LDB_SUCCESS) {
662 task_server_terminate(task,
663 "dns: failed to look up root DNS zones",
664 true);
665 return;
668 TYPESAFE_QSORT(res->msgs, res->count, dns_server_sort_zones);
670 for (i=0; i < res->count; i++) {
671 struct dns_server_zone *z;
673 z = talloc_zero(dns, struct dns_server_zone);
674 if (z == NULL) {
677 z->name = ldb_msg_find_attr_as_string(res->msgs[i], "name", NULL);
678 z->dn = talloc_move(z, &res->msgs[i]->dn);
680 DLIST_ADD_END(dns->zones, z, NULL);
683 status = dns_startup_interfaces(dns, task->lp_ctx, ifaces);
684 if (!NT_STATUS_IS_OK(status)) {
685 task_server_terminate(task, "dns failed to setup interfaces", true);
686 return;
690 NTSTATUS server_service_dns_init(void)
692 return register_server_service("dns", dns_task_init);