Line data Source code
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 "samba/service_task.h"
24 : #include "samba/service.h"
25 : #include "samba/service_stream.h"
26 : #include "samba/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 : #include "auth/auth.h"
47 : #include "auth/credentials/credentials.h"
48 : #include "librpc/gen_ndr/ndr_irpc.h"
49 : #include "lib/messaging/irpc.h"
50 : #include "libds/common/roles.h"
51 :
52 : #undef DBGC_CLASS
53 : #define DBGC_CLASS DBGC_DNS
54 :
55 : NTSTATUS server_service_dns_init(TALLOC_CTX *);
56 :
57 : /* hold information about one dns socket */
58 : struct dns_socket {
59 : struct dns_server *dns;
60 : struct tsocket_address *local_address;
61 : };
62 :
63 : struct dns_udp_socket {
64 : struct dns_socket *dns_socket;
65 : struct tdgram_context *dgram;
66 : struct tevent_queue *send_queue;
67 : };
68 :
69 : /*
70 : state of an open tcp connection
71 : */
72 : struct dns_tcp_connection {
73 : /* stream connection we belong to */
74 : struct stream_connection *conn;
75 :
76 : /* the dns_server the connection belongs to */
77 : struct dns_socket *dns_socket;
78 :
79 : struct tstream_context *tstream;
80 :
81 : struct tevent_queue *send_queue;
82 : };
83 :
84 400 : static void dns_tcp_terminate_connection(struct dns_tcp_connection *dnsconn, const char *reason)
85 : {
86 400 : stream_terminate_connection(dnsconn->conn, reason);
87 400 : }
88 :
89 0 : static void dns_tcp_recv(struct stream_connection *conn, uint16_t flags)
90 : {
91 0 : struct dns_tcp_connection *dnsconn = talloc_get_type(conn->private_data,
92 : struct dns_tcp_connection);
93 : /* this should never be triggered! */
94 0 : dns_tcp_terminate_connection(dnsconn, "dns_tcp_recv: called");
95 0 : }
96 :
97 0 : static void dns_tcp_send(struct stream_connection *conn, uint16_t flags)
98 : {
99 0 : struct dns_tcp_connection *dnsconn = talloc_get_type(conn->private_data,
100 : struct dns_tcp_connection);
101 : /* this should never be triggered! */
102 0 : dns_tcp_terminate_connection(dnsconn, "dns_tcp_send: called");
103 0 : }
104 :
105 : struct dns_process_state {
106 : DATA_BLOB *in;
107 : struct dns_server *dns;
108 : struct dns_name_packet in_packet;
109 : struct dns_request_state state;
110 : uint16_t dns_err;
111 : struct dns_name_packet out_packet;
112 : DATA_BLOB out;
113 : };
114 :
115 : static void dns_process_done(struct tevent_req *subreq);
116 :
117 30900 : static struct tevent_req *dns_process_send(TALLOC_CTX *mem_ctx,
118 : struct tevent_context *ev,
119 : struct dns_server *dns,
120 : const struct tsocket_address *remote_address,
121 : const struct tsocket_address *local_address,
122 : DATA_BLOB *in)
123 : {
124 : struct tevent_req *req, *subreq;
125 : struct dns_process_state *state;
126 : enum ndr_err_code ndr_err;
127 : WERROR ret;
128 30900 : const char **forwarder = lpcfg_dns_forwarder(dns->task->lp_ctx);
129 30900 : req = tevent_req_create(mem_ctx, &state, struct dns_process_state);
130 30900 : if (req == NULL) {
131 0 : return NULL;
132 : }
133 30900 : state->state.mem_ctx = state;
134 30900 : state->in = in;
135 :
136 30900 : state->dns = dns;
137 :
138 30900 : if (in->length < 12) {
139 0 : tevent_req_werror(req, WERR_INVALID_PARAMETER);
140 0 : return tevent_req_post(req, ev);
141 : }
142 30900 : dump_data_dbgc(DBGC_DNS, 8, in->data, in->length);
143 :
144 30900 : ndr_err = ndr_pull_struct_blob(
145 30900 : in, state, &state->in_packet,
146 : (ndr_pull_flags_fn_t)ndr_pull_dns_name_packet);
147 :
148 30900 : if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
149 0 : state->dns_err = DNS_RCODE_FORMERR;
150 0 : tevent_req_done(req);
151 0 : return tevent_req_post(req, ev);
152 : }
153 30900 : if (DEBUGLVLC(DBGC_DNS, 8)) {
154 0 : NDR_PRINT_DEBUGC(DBGC_DNS, dns_name_packet, &state->in_packet);
155 : }
156 :
157 30900 : if (state->in_packet.operation & DNS_FLAG_REPLY) {
158 4 : DEBUG(1, ("Won't reply to replies.\n"));
159 4 : tevent_req_werror(req, WERR_INVALID_PARAMETER);
160 4 : return tevent_req_post(req, ev);
161 : }
162 :
163 30896 : state->state.flags = state->in_packet.operation;
164 30896 : state->state.flags |= DNS_FLAG_REPLY;
165 :
166 30896 : state->state.local_address = local_address;
167 30896 : state->state.remote_address = remote_address;
168 :
169 30896 : if (forwarder && *forwarder && **forwarder) {
170 1537 : state->state.flags |= DNS_FLAG_RECURSION_AVAIL;
171 : }
172 :
173 30896 : state->out_packet = state->in_packet;
174 :
175 30896 : ret = dns_verify_tsig(dns, state, &state->state,
176 30896 : &state->out_packet, in);
177 30896 : if (!W_ERROR_IS_OK(ret)) {
178 4 : state->dns_err = werr_to_dns_err(ret);
179 4 : tevent_req_done(req);
180 4 : return tevent_req_post(req, ev);
181 : }
182 :
183 30892 : switch (state->in_packet.operation & DNS_OPCODE) {
184 29450 : case DNS_OPCODE_QUERY:
185 29450 : subreq = dns_server_process_query_send(
186 : state, ev, dns,
187 29450 : &state->state, &state->in_packet);
188 29450 : if (tevent_req_nomem(subreq, req)) {
189 339 : return tevent_req_post(req, ev);
190 : }
191 29450 : tevent_req_set_callback(subreq, dns_process_done, req);
192 29450 : return req;
193 1442 : case DNS_OPCODE_UPDATE:
194 7252 : ret = dns_server_process_update(
195 2272 : dns, &state->state, state, &state->in_packet,
196 2272 : &state->out_packet.answers, &state->out_packet.ancount,
197 2272 : &state->out_packet.nsrecs, &state->out_packet.nscount,
198 1442 : &state->out_packet.additional,
199 1442 : &state->out_packet.arcount);
200 1442 : break;
201 0 : default:
202 0 : ret = WERR_DNS_ERROR_RCODE_NOT_IMPLEMENTED;
203 : }
204 1442 : if (!W_ERROR_IS_OK(ret)) {
205 151 : state->dns_err = werr_to_dns_err(ret);
206 : }
207 1442 : tevent_req_done(req);
208 1442 : return tevent_req_post(req, ev);
209 : }
210 :
211 29450 : static void dns_process_done(struct tevent_req *subreq)
212 : {
213 29450 : struct tevent_req *req = tevent_req_callback_data(
214 : subreq, struct tevent_req);
215 29450 : struct dns_process_state *state = tevent_req_data(
216 : req, struct dns_process_state);
217 : WERROR ret;
218 :
219 29450 : ret = dns_server_process_query_recv(
220 : subreq, state,
221 : &state->out_packet.answers, &state->out_packet.ancount,
222 : &state->out_packet.nsrecs, &state->out_packet.nscount,
223 : &state->out_packet.additional, &state->out_packet.arcount);
224 29450 : TALLOC_FREE(subreq);
225 :
226 29450 : if (!W_ERROR_IS_OK(ret)) {
227 18526 : state->dns_err = werr_to_dns_err(ret);
228 : }
229 29450 : tevent_req_done(req);
230 29450 : }
231 :
232 30900 : static WERROR dns_process_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
233 : DATA_BLOB *out)
234 : {
235 30900 : struct dns_process_state *state = tevent_req_data(
236 : req, struct dns_process_state);
237 : enum ndr_err_code ndr_err;
238 : WERROR ret;
239 :
240 30900 : if (tevent_req_is_werror(req, &ret)) {
241 4 : return ret;
242 : }
243 49496 : if ((state->dns_err != DNS_RCODE_OK) &&
244 18799 : (state->dns_err != DNS_RCODE_NXDOMAIN) &&
245 169 : (state->dns_err != DNS_RCODE_NOTAUTH))
246 : {
247 165 : goto drop;
248 : }
249 30731 : if (state->dns_err != DNS_RCODE_OK) {
250 18516 : state->out_packet.operation |= state->dns_err;
251 : }
252 30731 : state->out_packet.operation |= state->state.flags;
253 :
254 30731 : if (state->state.sign) {
255 396 : ret = dns_sign_tsig(state->dns, mem_ctx, &state->state,
256 : &state->out_packet, 0);
257 396 : if (!W_ERROR_IS_OK(ret)) {
258 0 : state->dns_err = DNS_RCODE_SERVFAIL;
259 0 : goto drop;
260 : }
261 : }
262 :
263 30731 : if (DEBUGLVLC(DBGC_DNS, 8)) {
264 0 : NDR_PRINT_DEBUGC(DBGC_DNS, dns_name_packet, &state->out_packet);
265 : }
266 :
267 30731 : ndr_err = ndr_push_struct_blob(
268 30731 : out, mem_ctx, &state->out_packet,
269 : (ndr_push_flags_fn_t)ndr_push_dns_name_packet);
270 30731 : if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
271 0 : DEBUG(1, ("Failed to push packet: %s!\n",
272 : ndr_errstr(ndr_err)));
273 0 : state->dns_err = DNS_RCODE_SERVFAIL;
274 0 : goto drop;
275 : }
276 30731 : return WERR_OK;
277 :
278 165 : drop:
279 165 : *out = data_blob_talloc(mem_ctx, state->in->data, state->in->length);
280 165 : if (out->data == NULL) {
281 0 : return WERR_NOT_ENOUGH_MEMORY;
282 : }
283 165 : out->data[2] |= 0x80; /* Toggle DNS_FLAG_REPLY */
284 165 : out->data[3] |= state->dns_err;
285 165 : return WERR_OK;
286 : }
287 :
288 : struct dns_tcp_call {
289 : struct dns_tcp_connection *dns_conn;
290 : DATA_BLOB in;
291 : DATA_BLOB out;
292 : uint8_t out_hdr[4];
293 : struct iovec out_iov[2];
294 : };
295 :
296 : static void dns_tcp_call_process_done(struct tevent_req *subreq);
297 : static void dns_tcp_call_writev_done(struct tevent_req *subreq);
298 :
299 864 : static void dns_tcp_call_loop(struct tevent_req *subreq)
300 : {
301 864 : struct dns_tcp_connection *dns_conn = tevent_req_callback_data(subreq,
302 : struct dns_tcp_connection);
303 864 : struct dns_server *dns = dns_conn->dns_socket->dns;
304 : struct dns_tcp_call *call;
305 : NTSTATUS status;
306 :
307 864 : call = talloc(dns_conn, struct dns_tcp_call);
308 864 : if (call == NULL) {
309 0 : dns_tcp_terminate_connection(dns_conn, "dns_tcp_call_loop: "
310 : "no memory for dns_tcp_call");
311 40 : return;
312 : }
313 864 : call->dns_conn = dns_conn;
314 :
315 864 : status = tstream_read_pdu_blob_recv(subreq,
316 : call,
317 : &call->in);
318 864 : TALLOC_FREE(subreq);
319 864 : if (!NT_STATUS_IS_OK(status)) {
320 : const char *reason;
321 :
322 396 : reason = talloc_asprintf(call, "dns_tcp_call_loop: "
323 : "tstream_read_pdu_blob_recv() - %s",
324 : nt_errstr(status));
325 396 : if (!reason) {
326 0 : reason = nt_errstr(status);
327 : }
328 :
329 396 : dns_tcp_terminate_connection(dns_conn, reason);
330 396 : return;
331 : }
332 :
333 468 : DEBUG(10,("Received DNS TCP packet of length %lu from %s\n",
334 : (long) call->in.length,
335 : tsocket_address_string(dns_conn->conn->remote_address, call)));
336 :
337 : /* skip length header */
338 468 : call->in.data += 2;
339 468 : call->in.length -= 2;
340 :
341 861 : subreq = dns_process_send(call, dns->task->event_ctx, dns,
342 468 : dns_conn->conn->remote_address,
343 468 : dns_conn->conn->local_address,
344 : &call->in);
345 468 : if (subreq == NULL) {
346 0 : dns_tcp_terminate_connection(
347 : dns_conn, "dns_tcp_call_loop: dns_process_send "
348 : "failed\n");
349 0 : return;
350 : }
351 468 : tevent_req_set_callback(subreq, dns_tcp_call_process_done, call);
352 :
353 : /*
354 : * The dns tcp pdu's has the length as 2 byte (initial_read_size),
355 : * packet_full_request_u16 provides the pdu length then.
356 : */
357 861 : subreq = tstream_read_pdu_blob_send(dns_conn,
358 468 : dns_conn->conn->event.ctx,
359 : dns_conn->tstream,
360 : 2, /* initial_read_size */
361 : packet_full_request_u16,
362 : dns_conn);
363 468 : if (subreq == NULL) {
364 0 : dns_tcp_terminate_connection(dns_conn, "dns_tcp_call_loop: "
365 : "no memory for tstream_read_pdu_blob_send");
366 0 : return;
367 : }
368 468 : tevent_req_set_callback(subreq, dns_tcp_call_loop, dns_conn);
369 : }
370 :
371 468 : static void dns_tcp_call_process_done(struct tevent_req *subreq)
372 : {
373 468 : struct dns_tcp_call *call = tevent_req_callback_data(subreq,
374 : struct dns_tcp_call);
375 468 : struct dns_tcp_connection *dns_conn = call->dns_conn;
376 : WERROR err;
377 :
378 468 : err = dns_process_recv(subreq, call, &call->out);
379 468 : TALLOC_FREE(subreq);
380 468 : if (!W_ERROR_IS_OK(err)) {
381 4 : DEBUG(1, ("dns_process returned %s\n", win_errstr(err)));
382 4 : dns_tcp_terminate_connection(dns_conn,
383 : "dns_tcp_call_loop: process function failed");
384 4 : return;
385 : }
386 :
387 : /* First add the length of the out buffer */
388 464 : RSSVAL(call->out_hdr, 0, call->out.length);
389 464 : call->out_iov[0].iov_base = (char *) call->out_hdr;
390 464 : call->out_iov[0].iov_len = 2;
391 :
392 464 : call->out_iov[1].iov_base = (char *) call->out.data;
393 464 : call->out_iov[1].iov_len = call->out.length;
394 :
395 854 : subreq = tstream_writev_queue_send(call,
396 464 : dns_conn->conn->event.ctx,
397 : dns_conn->tstream,
398 : dns_conn->send_queue,
399 464 : call->out_iov, 2);
400 464 : if (subreq == NULL) {
401 0 : dns_tcp_terminate_connection(dns_conn, "dns_tcp_call_loop: "
402 : "no memory for tstream_writev_queue_send");
403 0 : return;
404 : }
405 464 : tevent_req_set_callback(subreq, dns_tcp_call_writev_done, call);
406 : }
407 :
408 464 : static void dns_tcp_call_writev_done(struct tevent_req *subreq)
409 : {
410 464 : struct dns_tcp_call *call = tevent_req_callback_data(subreq,
411 : struct dns_tcp_call);
412 : int sys_errno;
413 : int rc;
414 :
415 464 : rc = tstream_writev_queue_recv(subreq, &sys_errno);
416 464 : TALLOC_FREE(subreq);
417 464 : if (rc == -1) {
418 : const char *reason;
419 :
420 0 : reason = talloc_asprintf(call, "dns_tcp_call_writev_done: "
421 : "tstream_writev_queue_recv() - %d:%s",
422 : sys_errno, strerror(sys_errno));
423 0 : if (!reason) {
424 0 : reason = "dns_tcp_call_writev_done: tstream_writev_queue_recv() failed";
425 : }
426 :
427 0 : dns_tcp_terminate_connection(call->dns_conn, reason);
428 0 : return;
429 : }
430 :
431 : /* We don't care about errors */
432 :
433 464 : talloc_free(call);
434 : }
435 :
436 : /*
437 : called when we get a new connection
438 : */
439 400 : static void dns_tcp_accept(struct stream_connection *conn)
440 : {
441 : struct dns_socket *dns_socket;
442 : struct dns_tcp_connection *dns_conn;
443 : struct tevent_req *subreq;
444 : int rc;
445 :
446 400 : dns_conn = talloc_zero(conn, struct dns_tcp_connection);
447 400 : if (dns_conn == NULL) {
448 0 : stream_terminate_connection(conn,
449 : "dns_tcp_accept: out of memory");
450 0 : return;
451 : }
452 :
453 400 : dns_conn->send_queue = tevent_queue_create(conn, "dns_tcp_accept");
454 400 : if (dns_conn->send_queue == NULL) {
455 0 : stream_terminate_connection(conn,
456 : "dns_tcp_accept: out of memory");
457 0 : return;
458 : }
459 :
460 400 : dns_socket = talloc_get_type(conn->private_data, struct dns_socket);
461 :
462 400 : TALLOC_FREE(conn->event.fde);
463 :
464 400 : rc = tstream_bsd_existing_socket(dns_conn,
465 : socket_get_fd(conn->socket),
466 : &dns_conn->tstream);
467 400 : if (rc < 0) {
468 0 : stream_terminate_connection(conn,
469 : "dns_tcp_accept: out of memory");
470 0 : return;
471 : }
472 :
473 400 : dns_conn->conn = conn;
474 400 : dns_conn->dns_socket = dns_socket;
475 400 : conn->private_data = dns_conn;
476 :
477 : /*
478 : * The dns tcp pdu's has the length as 2 byte (initial_read_size),
479 : * packet_full_request_u16 provides the pdu length then.
480 : */
481 759 : subreq = tstream_read_pdu_blob_send(dns_conn,
482 400 : dns_conn->conn->event.ctx,
483 : dns_conn->tstream,
484 : 2, /* initial_read_size */
485 : packet_full_request_u16,
486 : dns_conn);
487 400 : if (subreq == NULL) {
488 0 : dns_tcp_terminate_connection(dns_conn, "dns_tcp_accept: "
489 : "no memory for tstream_read_pdu_blob_send");
490 0 : return;
491 : }
492 400 : tevent_req_set_callback(subreq, dns_tcp_call_loop, dns_conn);
493 : }
494 :
495 : static const struct stream_server_ops dns_tcp_stream_ops = {
496 : .name = "dns_tcp",
497 : .accept_connection = dns_tcp_accept,
498 : .recv_handler = dns_tcp_recv,
499 : .send_handler = dns_tcp_send
500 : };
501 :
502 : struct dns_udp_call {
503 : struct dns_udp_socket *sock;
504 : struct tsocket_address *src;
505 : DATA_BLOB in;
506 : DATA_BLOB out;
507 : };
508 :
509 : static void dns_udp_call_process_done(struct tevent_req *subreq);
510 : static void dns_udp_call_sendto_done(struct tevent_req *subreq);
511 :
512 30436 : static void dns_udp_call_loop(struct tevent_req *subreq)
513 : {
514 30436 : struct dns_udp_socket *sock = tevent_req_callback_data(subreq,
515 : struct dns_udp_socket);
516 30436 : struct dns_server *dns = sock->dns_socket->dns;
517 : struct dns_udp_call *call;
518 : uint8_t *buf;
519 : ssize_t len;
520 : int sys_errno;
521 :
522 30436 : call = talloc(sock, struct dns_udp_call);
523 30436 : if (call == NULL) {
524 0 : talloc_free(call);
525 0 : goto done;
526 : }
527 30436 : call->sock = sock;
528 :
529 30436 : len = tdgram_recvfrom_recv(subreq, &sys_errno,
530 : call, &buf, &call->src);
531 30436 : TALLOC_FREE(subreq);
532 30436 : if (len == -1) {
533 4 : talloc_free(call);
534 4 : goto done;
535 : }
536 :
537 30432 : call->in.data = buf;
538 30432 : call->in.length = len;
539 :
540 30432 : DEBUG(10,("Received DNS UDP packet of length %lu from %s\n",
541 : (long)call->in.length,
542 : tsocket_address_string(call->src, call)));
543 :
544 59985 : subreq = dns_process_send(call, dns->task->event_ctx, dns,
545 30432 : call->src,
546 30432 : sock->dns_socket->local_address,
547 : &call->in);
548 30432 : if (subreq == NULL) {
549 0 : TALLOC_FREE(call);
550 0 : goto done;
551 : }
552 30432 : tevent_req_set_callback(subreq, dns_udp_call_process_done, call);
553 :
554 30436 : done:
555 59992 : subreq = tdgram_recvfrom_send(sock,
556 30436 : sock->dns_socket->dns->task->event_ctx,
557 : sock->dgram);
558 30436 : if (subreq == NULL) {
559 0 : task_server_terminate(sock->dns_socket->dns->task,
560 : "no memory for tdgram_recvfrom_send",
561 : true);
562 0 : return;
563 : }
564 30436 : tevent_req_set_callback(subreq, dns_udp_call_loop, sock);
565 : }
566 :
567 30432 : static void dns_udp_call_process_done(struct tevent_req *subreq)
568 : {
569 30432 : struct dns_udp_call *call = tevent_req_callback_data(
570 : subreq, struct dns_udp_call);
571 30432 : struct dns_udp_socket *sock = call->sock;
572 30432 : struct dns_server *dns = sock->dns_socket->dns;
573 : WERROR err;
574 :
575 30432 : err = dns_process_recv(subreq, call, &call->out);
576 30432 : TALLOC_FREE(subreq);
577 30432 : if (!W_ERROR_IS_OK(err)) {
578 0 : DEBUG(1, ("dns_process returned %s\n", win_errstr(err)));
579 0 : TALLOC_FREE(call);
580 0 : return;
581 : }
582 :
583 89538 : subreq = tdgram_sendto_queue_send(call,
584 30432 : dns->task->event_ctx,
585 : sock->dgram,
586 : sock->send_queue,
587 30432 : call->out.data,
588 : call->out.length,
589 : call->src);
590 30432 : if (subreq == NULL) {
591 0 : talloc_free(call);
592 0 : return;
593 : }
594 30432 : tevent_req_set_callback(subreq, dns_udp_call_sendto_done, call);
595 :
596 : }
597 30432 : static void dns_udp_call_sendto_done(struct tevent_req *subreq)
598 : {
599 30432 : struct dns_udp_call *call = tevent_req_callback_data(subreq,
600 : struct dns_udp_call);
601 : int sys_errno;
602 :
603 30432 : tdgram_sendto_queue_recv(subreq, &sys_errno);
604 :
605 : /* We don't care about errors */
606 :
607 30432 : talloc_free(call);
608 30432 : }
609 :
610 : /*
611 : start listening on the given address
612 : */
613 94 : static NTSTATUS dns_add_socket(struct dns_server *dns,
614 : const struct model_ops *model_ops,
615 : const char *name,
616 : const char *address,
617 : uint16_t port)
618 : {
619 : struct dns_socket *dns_socket;
620 : struct dns_udp_socket *dns_udp_socket;
621 : struct tevent_req *udpsubreq;
622 : NTSTATUS status;
623 : int ret;
624 :
625 94 : dns_socket = talloc(dns, struct dns_socket);
626 94 : NT_STATUS_HAVE_NO_MEMORY(dns_socket);
627 :
628 94 : dns_socket->dns = dns;
629 :
630 94 : ret = tsocket_address_inet_from_strings(dns_socket, "ip",
631 : address, port,
632 : &dns_socket->local_address);
633 94 : if (ret != 0) {
634 0 : status = map_nt_error_from_unix_common(errno);
635 0 : return status;
636 : }
637 :
638 328 : status = stream_setup_socket(dns->task,
639 94 : dns->task->event_ctx,
640 94 : dns->task->lp_ctx,
641 : model_ops,
642 : &dns_tcp_stream_ops,
643 : "ip", address, &port,
644 94 : lpcfg_socket_options(dns->task->lp_ctx),
645 : dns_socket,
646 94 : dns->task->process_context);
647 94 : if (!NT_STATUS_IS_OK(status)) {
648 0 : DEBUG(0,("Failed to bind to %s:%u TCP - %s\n",
649 : address, port, nt_errstr(status)));
650 0 : talloc_free(dns_socket);
651 0 : return status;
652 : }
653 :
654 94 : dns_udp_socket = talloc(dns_socket, struct dns_udp_socket);
655 94 : NT_STATUS_HAVE_NO_MEMORY(dns_udp_socket);
656 :
657 94 : dns_udp_socket->dns_socket = dns_socket;
658 :
659 94 : ret = tdgram_inet_udp_socket(dns_socket->local_address,
660 : NULL,
661 : dns_udp_socket,
662 : &dns_udp_socket->dgram);
663 94 : if (ret != 0) {
664 0 : status = map_nt_error_from_unix_common(errno);
665 0 : DEBUG(0,("Failed to bind to %s:%u UDP - %s\n",
666 : address, port, nt_errstr(status)));
667 0 : return status;
668 : }
669 :
670 94 : dns_udp_socket->send_queue = tevent_queue_create(dns_udp_socket,
671 : "dns_udp_send_queue");
672 94 : NT_STATUS_HAVE_NO_MEMORY(dns_udp_socket->send_queue);
673 :
674 164 : udpsubreq = tdgram_recvfrom_send(dns_udp_socket,
675 94 : dns->task->event_ctx,
676 : dns_udp_socket->dgram);
677 94 : NT_STATUS_HAVE_NO_MEMORY(udpsubreq);
678 94 : tevent_req_set_callback(udpsubreq, dns_udp_call_loop, dns_udp_socket);
679 :
680 94 : return NT_STATUS_OK;
681 : }
682 :
683 : /*
684 : setup our listening sockets on the configured network interfaces
685 : */
686 47 : static NTSTATUS dns_startup_interfaces(struct dns_server *dns,
687 : struct interface *ifaces,
688 : const struct model_ops *model_ops)
689 : {
690 : size_t num_interfaces;
691 47 : TALLOC_CTX *tmp_ctx = talloc_new(dns);
692 : NTSTATUS status;
693 : int i;
694 :
695 47 : if (ifaces != NULL) {
696 0 : num_interfaces = iface_list_count(ifaces);
697 :
698 0 : for (i=0; i<num_interfaces; i++) {
699 0 : const char *address = talloc_strdup(tmp_ctx,
700 : iface_list_n_ip(ifaces, i));
701 :
702 0 : status = dns_add_socket(dns, model_ops, "dns", address,
703 0 : lpcfg_dns_port(dns->task->lp_ctx));
704 0 : NT_STATUS_NOT_OK_RETURN(status);
705 : }
706 : } else {
707 47 : size_t num_binds = 0;
708 : char **wcard;
709 47 : wcard = iface_list_wildcard(tmp_ctx);
710 47 : if (wcard == NULL) {
711 0 : DEBUG(0, ("No wildcard address available\n"));
712 0 : return NT_STATUS_INTERNAL_ERROR;
713 : }
714 141 : for (i = 0; wcard[i] != NULL; i++) {
715 94 : status = dns_add_socket(dns, model_ops, "dns", wcard[i],
716 94 : lpcfg_dns_port(dns->task->lp_ctx));
717 94 : if (NT_STATUS_IS_OK(status)) {
718 94 : num_binds++;
719 : }
720 : }
721 47 : if (num_binds == 0) {
722 0 : talloc_free(tmp_ctx);
723 0 : return NT_STATUS_INVALID_PARAMETER_MIX;
724 : }
725 : }
726 :
727 47 : talloc_free(tmp_ctx);
728 :
729 47 : return NT_STATUS_OK;
730 : }
731 :
732 47 : static struct dns_server_tkey_store *tkey_store_init(TALLOC_CTX *mem_ctx,
733 : uint16_t size)
734 : {
735 47 : struct dns_server_tkey_store *buffer = talloc_zero(mem_ctx,
736 : struct dns_server_tkey_store);
737 :
738 47 : if (buffer == NULL) {
739 0 : return NULL;
740 : }
741 :
742 47 : buffer->size = size;
743 47 : buffer->next_idx = 0;
744 :
745 47 : buffer->tkeys = talloc_zero_array(buffer, struct dns_server_tkey *, size);
746 47 : if (buffer->tkeys == NULL) {
747 0 : TALLOC_FREE(buffer);
748 : }
749 :
750 47 : return buffer;
751 : }
752 :
753 1207 : static NTSTATUS dns_server_reload_zones(struct dns_server *dns)
754 : {
755 : NTSTATUS status;
756 1207 : struct dns_server_zone *new_list = NULL;
757 1207 : struct dns_server_zone *old_list = dns->zones;
758 : struct dns_server_zone *old_zone;
759 1207 : status = dns_common_zones(dns->samdb, dns, NULL, &new_list);
760 1207 : if (!NT_STATUS_IS_OK(status)) {
761 0 : return status;
762 : }
763 1207 : dns->zones = new_list;
764 5365 : while ((old_zone = DLIST_TAIL(old_list)) != NULL) {
765 3465 : DLIST_REMOVE(old_list, old_zone);
766 3465 : talloc_free(old_zone);
767 : }
768 :
769 1207 : return NT_STATUS_OK;
770 : }
771 :
772 : /**
773 : * Called when the internal DNS server should reload the zones from DB, for
774 : * example, when zones are added or deleted through RPC or replicated by
775 : * inbound DRS.
776 : */
777 1160 : static NTSTATUS dns_reload_zones(struct irpc_message *msg,
778 : struct dnssrv_reload_dns_zones *r)
779 : {
780 : struct dns_server *dns;
781 :
782 1160 : dns = talloc_get_type(msg->private_data, struct dns_server);
783 1160 : if (dns == NULL) {
784 0 : r->out.result = NT_STATUS_INTERNAL_ERROR;
785 0 : return NT_STATUS_INTERNAL_ERROR;
786 : }
787 :
788 1160 : r->out.result = dns_server_reload_zones(dns);
789 :
790 1160 : return NT_STATUS_OK;
791 : }
792 :
793 53 : static NTSTATUS dns_task_init(struct task_server *task)
794 : {
795 : struct dns_server *dns;
796 : NTSTATUS status;
797 53 : struct interface *ifaces = NULL;
798 : int ret;
799 : static const char * const attrs_none[] = { NULL};
800 : struct ldb_message *dns_acc;
801 : char *hostname_lower;
802 : char *dns_spn;
803 : bool ok;
804 :
805 53 : switch (lpcfg_server_role(task->lp_ctx)) {
806 0 : case ROLE_STANDALONE:
807 0 : task_server_terminate(task, "dns: no DNS required in standalone configuration", false);
808 0 : return NT_STATUS_INVALID_DOMAIN_ROLE;
809 6 : case ROLE_DOMAIN_MEMBER:
810 6 : task_server_terminate(task, "dns: no DNS required in member server configuration", false);
811 0 : return NT_STATUS_INVALID_DOMAIN_ROLE;
812 47 : case ROLE_ACTIVE_DIRECTORY_DC:
813 : /* Yes, we want a DNS */
814 47 : break;
815 : }
816 :
817 47 : if (lpcfg_interfaces(task->lp_ctx) && lpcfg_bind_interfaces_only(task->lp_ctx)) {
818 0 : load_interface_list(task, task->lp_ctx, &ifaces);
819 :
820 0 : if (iface_list_count(ifaces) == 0) {
821 0 : task_server_terminate(task, "dns: no network interfaces configured", false);
822 0 : return NT_STATUS_UNSUCCESSFUL;
823 : }
824 : }
825 :
826 47 : task_server_set_title(task, "task[dns]");
827 :
828 47 : dns = talloc_zero(task, struct dns_server);
829 47 : if (dns == NULL) {
830 0 : task_server_terminate(task, "dns: out of memory", true);
831 0 : return NT_STATUS_NO_MEMORY;
832 : }
833 :
834 47 : dns->task = task;
835 :
836 47 : dns->server_credentials = cli_credentials_init(dns);
837 47 : if (!dns->server_credentials) {
838 0 : task_server_terminate(task, "Failed to init server credentials\n", true);
839 0 : return NT_STATUS_UNSUCCESSFUL;
840 : }
841 :
842 141 : dns->samdb = samdb_connect(dns,
843 47 : dns->task->event_ctx,
844 47 : dns->task->lp_ctx,
845 47 : system_session(dns->task->lp_ctx),
846 : NULL,
847 : 0);
848 47 : if (!dns->samdb) {
849 0 : task_server_terminate(task, "dns: samdb_connect failed", true);
850 0 : return NT_STATUS_UNSUCCESSFUL;
851 : }
852 :
853 47 : ok = cli_credentials_set_conf(dns->server_credentials, task->lp_ctx);
854 47 : if (!ok) {
855 0 : task_server_terminate(task,
856 : "dns: failed to load smb.conf",
857 : true);
858 0 : return NT_STATUS_UNSUCCESSFUL;
859 : }
860 :
861 47 : hostname_lower = strlower_talloc(dns, lpcfg_netbios_name(task->lp_ctx));
862 47 : dns_spn = talloc_asprintf(dns, "DNS/%s.%s",
863 : hostname_lower,
864 : lpcfg_dnsdomain(task->lp_ctx));
865 47 : TALLOC_FREE(hostname_lower);
866 :
867 47 : ret = dsdb_search_one(dns->samdb, dns, &dns_acc,
868 : ldb_get_default_basedn(dns->samdb), LDB_SCOPE_SUBTREE,
869 : attrs_none, 0, "(servicePrincipalName=%s)",
870 : dns_spn);
871 47 : if (ret == LDB_SUCCESS) {
872 2 : TALLOC_FREE(dns_acc);
873 2 : if (!dns_spn) {
874 0 : task_server_terminate(task, "dns: talloc_asprintf failed", true);
875 0 : return NT_STATUS_UNSUCCESSFUL;
876 : }
877 2 : status = cli_credentials_set_stored_principal(dns->server_credentials, task->lp_ctx, dns_spn);
878 2 : if (!NT_STATUS_IS_OK(status)) {
879 0 : task_server_terminate(task,
880 0 : talloc_asprintf(task, "Failed to obtain server credentials for DNS, "
881 : "despite finding it in the samdb! %s\n",
882 : nt_errstr(status)),
883 : true);
884 0 : return status;
885 : }
886 : } else {
887 45 : TALLOC_FREE(dns_spn);
888 45 : status = cli_credentials_set_machine_account(dns->server_credentials, task->lp_ctx);
889 45 : if (!NT_STATUS_IS_OK(status)) {
890 0 : task_server_terminate(task,
891 0 : talloc_asprintf(task, "Failed to obtain server credentials, perhaps a standalone server?: %s\n",
892 : nt_errstr(status)),
893 : true);
894 0 : return status;
895 : }
896 : }
897 :
898 47 : dns->tkeys = tkey_store_init(dns, TKEY_BUFFER_SIZE);
899 47 : if (!dns->tkeys) {
900 0 : task_server_terminate(task, "Failed to allocate tkey storage\n", true);
901 0 : return NT_STATUS_NO_MEMORY;
902 : }
903 :
904 47 : status = dns_server_reload_zones(dns);
905 47 : if (!NT_STATUS_IS_OK(status)) {
906 0 : task_server_terminate(task, "dns: failed to load DNS zones", true);
907 0 : return status;
908 : }
909 :
910 47 : status = dns_startup_interfaces(dns, ifaces, task->model_ops);
911 47 : if (!NT_STATUS_IS_OK(status)) {
912 0 : task_server_terminate(task, "dns failed to setup interfaces", true);
913 0 : return status;
914 : }
915 :
916 : /* Setup the IRPC interface and register handlers */
917 47 : status = irpc_add_name(task->msg_ctx, "dnssrv");
918 47 : if (!NT_STATUS_IS_OK(status)) {
919 0 : task_server_terminate(task, "dns: failed to register IRPC name", true);
920 0 : return status;
921 : }
922 :
923 47 : status = IRPC_REGISTER(task->msg_ctx, irpc, DNSSRV_RELOAD_DNS_ZONES,
924 : dns_reload_zones, dns);
925 47 : if (!NT_STATUS_IS_OK(status)) {
926 0 : task_server_terminate(task, "dns: failed to setup reload handler", true);
927 0 : return status;
928 : }
929 47 : return NT_STATUS_OK;
930 : }
931 :
932 53 : NTSTATUS server_service_dns_init(TALLOC_CTX *ctx)
933 : {
934 : static const struct service_details details = {
935 : .inhibit_fork_on_accept = true,
936 : .inhibit_pre_fork = true,
937 : .task_init = dns_task_init,
938 : .post_fork = NULL
939 : };
940 53 : return register_server_service(ctx, "dns", &details);
941 : }
|