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