2 Unix SMB/CIFS implementation.
6 Copyright (C) 2010 Kai Blin <kai@samba.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "smbd/service_task.h"
24 #include "smbd/service.h"
25 #include "smbd/service_stream.h"
26 #include "smbd/process_model.h"
27 #include "lib/events/events.h"
28 #include "lib/socket/socket.h"
29 #include "lib/tsocket/tsocket.h"
30 #include "libcli/util/tstream.h"
31 #include "system/network.h"
32 #include "lib/stream/packet.h"
33 #include "lib/socket/netif.h"
34 #include "dns_server/dns_server.h"
35 #include "param/param.h"
37 /* hold information about one dns socket */
39 struct dns_server
*dns
;
40 struct tsocket_address
*local_address
;
43 struct dns_udp_socket
{
44 struct dns_socket
*dns_socket
;
45 struct tdgram_context
*dgram
;
46 struct tevent_queue
*send_queue
;
50 state of an open tcp connection
52 struct dns_tcp_connection
{
53 /* stream connection we belong to */
54 struct stream_connection
*conn
;
56 /* the dns_server the connection belongs to */
57 struct dns_socket
*dns_socket
;
59 struct tstream_context
*tstream
;
61 struct tevent_queue
*send_queue
;
64 static void dns_tcp_terminate_connection(struct dns_tcp_connection
*dnsconn
, const char *reason
)
66 stream_terminate_connection(dnsconn
->conn
, reason
);
69 static void dns_tcp_recv(struct stream_connection
*conn
, uint16_t flags
)
71 struct dns_tcp_connection
*dnsconn
= talloc_get_type(conn
->private_data
,
72 struct dns_tcp_connection
);
73 /* this should never be triggered! */
74 dns_tcp_terminate_connection(dnsconn
, "dns_tcp_recv: called");
77 static void dns_tcp_send(struct stream_connection
*conn
, uint16_t flags
)
79 struct dns_tcp_connection
*dnsconn
= talloc_get_type(conn
->private_data
,
80 struct dns_tcp_connection
);
81 /* this should never be triggered! */
82 dns_tcp_terminate_connection(dnsconn
, "dns_tcp_send: called");
85 bool dns_process(struct dns_server
*dns
,
90 DEBUG(0, ("FIXME: actually process DNS packet here\n"));
95 struct dns_tcp_connection
*dns_conn
;
99 struct iovec out_iov
[2];
102 static void dns_tcp_call_writev_done(struct tevent_req
*subreq
);
104 static void dns_tcp_call_loop(struct tevent_req
*subreq
)
106 struct dns_tcp_connection
*dns_conn
= tevent_req_callback_data(subreq
,
107 struct dns_tcp_connection
);
108 struct dns_tcp_call
*call
;
112 call
= talloc(dns_conn
, struct dns_tcp_call
);
114 dns_tcp_terminate_connection(dns_conn
, "dns_tcp_call_loop: "
115 "no memory for dns_tcp_call");
118 call
->dns_conn
= dns_conn
;
120 status
= tstream_read_pdu_blob_recv(subreq
,
124 if (!NT_STATUS_IS_OK(status
)) {
127 reason
= talloc_asprintf(call
, "dns_tcp_call_loop: "
128 "tstream_read_pdu_blob_recv() - %s",
131 reason
= nt_errstr(status
);
134 dns_tcp_terminate_connection(dns_conn
, reason
);
138 DEBUG(10,("Received krb5 TCP packet of length %lu from %s\n",
139 (long) call
->in
.length
,
140 tsocket_address_string(dns_conn
->conn
->remote_address
, call
)));
142 /* skip length header */
144 call
->in
.length
-= 4;
147 ok
= dns_process(dns_conn
->dns_socket
->dns
, call
, &call
->in
, &call
->out
);
149 dns_tcp_terminate_connection(dns_conn
,
150 "dns_tcp_call_loop: process function failed");
154 /* First add the length of the out buffer */
155 RSIVAL(call
->out_hdr
, 0, call
->out
.length
);
156 call
->out_iov
[0].iov_base
= (char *) call
->out_hdr
;
157 call
->out_iov
[0].iov_len
= 4;
159 call
->out_iov
[1].iov_base
= (char *) call
->out
.data
;
160 call
->out_iov
[1].iov_len
= call
->out
.length
;
162 subreq
= tstream_writev_queue_send(call
,
163 dns_conn
->conn
->event
.ctx
,
165 dns_conn
->send_queue
,
167 if (subreq
== NULL
) {
168 dns_tcp_terminate_connection(dns_conn
, "dns_tcp_call_loop: "
169 "no memory for tstream_writev_queue_send");
172 tevent_req_set_callback(subreq
, dns_tcp_call_writev_done
, call
);
175 * The krb5 tcp pdu's has the length as 4 byte (initial_read_size),
176 * packet_full_request_u32 provides the pdu length then.
178 subreq
= tstream_read_pdu_blob_send(dns_conn
,
179 dns_conn
->conn
->event
.ctx
,
181 4, /* initial_read_size */
182 packet_full_request_u32
,
184 if (subreq
== NULL
) {
185 dns_tcp_terminate_connection(dns_conn
, "dns_tcp_call_loop: "
186 "no memory for tstream_read_pdu_blob_send");
189 tevent_req_set_callback(subreq
, dns_tcp_call_loop
, dns_conn
);
192 static void dns_tcp_call_writev_done(struct tevent_req
*subreq
)
194 struct dns_tcp_call
*call
= tevent_req_callback_data(subreq
,
195 struct dns_tcp_call
);
199 rc
= tstream_writev_queue_recv(subreq
, &sys_errno
);
204 reason
= talloc_asprintf(call
, "dns_tcp_call_writev_done: "
205 "tstream_writev_queue_recv() - %d:%s",
206 sys_errno
, strerror(sys_errno
));
208 reason
= "dns_tcp_call_writev_done: tstream_writev_queue_recv() failed";
211 dns_tcp_terminate_connection(call
->dns_conn
, reason
);
215 /* We don't care about errors */
221 called when we get a new connection
223 static void dns_tcp_accept(struct stream_connection
*conn
)
225 struct dns_socket
*dns_socket
;
226 struct dns_tcp_connection
*dns_conn
;
227 struct tevent_req
*subreq
;
230 dns_conn
= talloc_zero(conn
, struct dns_tcp_connection
);
231 if (dns_conn
== NULL
) {
232 stream_terminate_connection(conn
,
233 "dns_tcp_accept: out of memory");
237 dns_conn
->send_queue
= tevent_queue_create(conn
, "dns_tcp_accept");
238 if (dns_conn
->send_queue
== NULL
) {
239 stream_terminate_connection(conn
,
240 "dns_tcp_accept: out of memory");
244 dns_socket
= talloc_get_type(conn
->private_data
, struct dns_socket
);
246 TALLOC_FREE(conn
->event
.fde
);
248 rc
= tstream_bsd_existing_socket(dns_conn
,
249 socket_get_fd(conn
->socket
),
252 stream_terminate_connection(conn
,
253 "dns_tcp_accept: out of memory");
257 dns_conn
->conn
= conn
;
258 dns_conn
->dns_socket
= dns_socket
;
259 conn
->private_data
= dns_conn
;
262 * The krb5 tcp pdu's has the length as 4 byte (initial_read_size),
263 * packet_full_request_u32 provides the pdu length then.
265 subreq
= tstream_read_pdu_blob_send(dns_conn
,
266 dns_conn
->conn
->event
.ctx
,
268 4, /* initial_read_size */
269 packet_full_request_u32
,
271 if (subreq
== NULL
) {
272 dns_tcp_terminate_connection(dns_conn
, "dns_tcp_accept: "
273 "no memory for tstream_read_pdu_blob_send");
276 tevent_req_set_callback(subreq
, dns_tcp_call_loop
, dns_conn
);
279 static const struct stream_server_ops dns_tcp_stream_ops
= {
281 .accept_connection
= dns_tcp_accept
,
282 .recv_handler
= dns_tcp_recv
,
283 .send_handler
= dns_tcp_send
286 struct dns_udp_call
{
287 struct tsocket_address
*src
;
292 static void dns_udp_call_sendto_done(struct tevent_req
*subreq
);
294 static void dns_udp_call_loop(struct tevent_req
*subreq
)
296 struct dns_udp_socket
*sock
= tevent_req_callback_data(subreq
,
297 struct dns_udp_socket
);
298 struct dns_udp_call
*call
;
304 call
= talloc(sock
, struct dns_udp_call
);
310 len
= tdgram_recvfrom_recv(subreq
, &sys_errno
,
311 call
, &buf
, &call
->src
);
319 call
->in
.length
= len
;
321 DEBUG(10,("Received krb5 UDP packet of length %lu from %s\n",
322 (long)call
->in
.length
,
323 tsocket_address_string(call
->src
, call
)));
326 ok
= dns_process(sock
->dns_socket
->dns
, call
, &call
->in
, &call
->out
);
332 subreq
= tdgram_sendto_queue_send(call
,
333 sock
->dns_socket
->dns
->task
->event_ctx
,
339 if (subreq
== NULL
) {
343 tevent_req_set_callback(subreq
, dns_udp_call_sendto_done
, call
);
346 subreq
= tdgram_recvfrom_send(sock
,
347 sock
->dns_socket
->dns
->task
->event_ctx
,
349 if (subreq
== NULL
) {
350 task_server_terminate(sock
->dns_socket
->dns
->task
,
351 "no memory for tdgram_recvfrom_send",
355 tevent_req_set_callback(subreq
, dns_udp_call_loop
, sock
);
358 static void dns_udp_call_sendto_done(struct tevent_req
*subreq
)
360 struct dns_udp_call
*call
= tevent_req_callback_data(subreq
,
361 struct dns_udp_call
);
365 ret
= tdgram_sendto_queue_recv(subreq
, &sys_errno
);
367 /* We don't care about errors */
373 start listening on the given address
375 static NTSTATUS
dns_add_socket(struct dns_server
*dns
,
376 const struct model_ops
*model_ops
,
381 struct dns_socket
*dns_socket
;
382 struct dns_udp_socket
*dns_udp_socket
;
383 struct tevent_req
*udpsubreq
;
387 dns_socket
= talloc(dns
, struct dns_socket
);
388 NT_STATUS_HAVE_NO_MEMORY(dns_socket
);
390 dns_socket
->dns
= dns
;
392 ret
= tsocket_address_inet_from_strings(dns_socket
, "ip",
394 &dns_socket
->local_address
);
396 status
= map_nt_error_from_unix(errno
);
400 status
= stream_setup_socket(dns
->task
->event_ctx
,
404 "ip", address
, &port
,
405 lpcfg_socket_options(dns
->task
->lp_ctx
),
407 if (!NT_STATUS_IS_OK(status
)) {
408 DEBUG(0,("Failed to bind to %s:%u TCP - %s\n",
409 address
, port
, nt_errstr(status
)));
410 talloc_free(dns_socket
);
414 dns_udp_socket
= talloc(dns_socket
, struct dns_udp_socket
);
415 NT_STATUS_HAVE_NO_MEMORY(dns_udp_socket
);
417 dns_udp_socket
->dns_socket
= dns_socket
;
419 ret
= tdgram_inet_udp_socket(dns_socket
->local_address
,
422 &dns_udp_socket
->dgram
);
424 status
= map_nt_error_from_unix(errno
);
425 DEBUG(0,("Failed to bind to %s:%u UDP - %s\n",
426 address
, port
, nt_errstr(status
)));
430 dns_udp_socket
->send_queue
= tevent_queue_create(dns_udp_socket
,
431 "dns_udp_send_queue");
432 NT_STATUS_HAVE_NO_MEMORY(dns_udp_socket
->send_queue
);
434 udpsubreq
= tdgram_recvfrom_send(dns_udp_socket
,
435 dns
->task
->event_ctx
,
436 dns_udp_socket
->dgram
);
437 NT_STATUS_HAVE_NO_MEMORY(udpsubreq
);
438 tevent_req_set_callback(udpsubreq
, dns_udp_call_loop
, dns_udp_socket
);
444 setup our listening sockets on the configured network interfaces
446 static NTSTATUS
dns_startup_interfaces(struct dns_server
*dns
, struct loadparm_context
*lp_ctx
,
447 struct interface
*ifaces
)
449 const struct model_ops
*model_ops
;
451 TALLOC_CTX
*tmp_ctx
= talloc_new(dns
);
455 /* within the dns task we want to be a single process, so
456 ask for the single process model ops and pass these to the
457 stream_setup_socket() call. */
458 model_ops
= process_model_startup(dns
->task
->event_ctx
, "single");
460 DEBUG(0,("Can't find 'single' process model_ops\n"));
461 return NT_STATUS_INTERNAL_ERROR
;
464 num_interfaces
= iface_count(ifaces
);
466 for (i
=0; i
<num_interfaces
; i
++) {
467 const char *address
= talloc_strdup(tmp_ctx
, iface_n_ip(ifaces
, i
));
469 status
= dns_add_socket(dns
, model_ops
, "dns", address
, DNS_SERVICE_PORT
);
470 NT_STATUS_NOT_OK_RETURN(status
);
473 talloc_free(tmp_ctx
);
477 static void dns_task_init(struct task_server
*task
)
479 struct dns_server
*dns
;
481 struct interface
*ifaces
;
483 switch (lpcfg_server_role(task
->lp_ctx
)) {
484 case ROLE_STANDALONE
:
485 task_server_terminate(task
, "dns: no DNS required in standalone configuration", false);
487 case ROLE_DOMAIN_MEMBER
:
488 task_server_terminate(task
, "dns: no DNS required in member server configuration", false);
490 case ROLE_DOMAIN_CONTROLLER
:
491 /* Yes, we want a DNS */
495 load_interfaces(task
, lpcfg_interfaces(task
->lp_ctx
), &ifaces
);
497 if (iface_count(ifaces
) == 0) {
498 task_server_terminate(task
, "dns: no network interfaces configured", false);
502 task_server_set_title(task
, "task[dns]");
504 dns
= talloc(task
, struct dns_server
);
506 task_server_terminate(task
, "dns: out of memory", true);
512 status
= dns_startup_interfaces(dns
, task
->lp_ctx
, ifaces
);
513 if (!NT_STATUS_IS_OK(status
)) {
514 task_server_terminate(task
, "dns failed to setup interfaces", true);
519 NTSTATUS
server_service_dns_init(void)
521 return register_server_service("dns", dns_task_init
);