5d9a5086ba8c5921d61cf7a2a164a212c4e0fd25
[mat/samba.git] / source4 / dns_server / dns_server.c
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 "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"
36 #include "librpc/ndr/libndr.h"
37 #include "librpc/gen_ndr/ndr_dns.h"
38
39 /* hold information about one dns socket */
40 struct dns_socket {
41         struct dns_server *dns;
42         struct tsocket_address *local_address;
43 };
44
45 struct dns_udp_socket {
46         struct dns_socket *dns_socket;
47         struct tdgram_context *dgram;
48         struct tevent_queue *send_queue;
49 };
50
51 /*
52   state of an open tcp connection
53 */
54 struct dns_tcp_connection {
55         /* stream connection we belong to */
56         struct stream_connection *conn;
57
58         /* the dns_server the connection belongs to */
59         struct dns_socket *dns_socket;
60
61         struct tstream_context *tstream;
62
63         struct tevent_queue *send_queue;
64 };
65
66 static void dns_tcp_terminate_connection(struct dns_tcp_connection *dnsconn, const char *reason)
67 {
68         stream_terminate_connection(dnsconn->conn, reason);
69 }
70
71 static void dns_tcp_recv(struct stream_connection *conn, uint16_t flags)
72 {
73         struct dns_tcp_connection *dnsconn = talloc_get_type(conn->private_data,
74                                                              struct dns_tcp_connection);
75         /* this should never be triggered! */
76         dns_tcp_terminate_connection(dnsconn, "dns_tcp_recv: called");
77 }
78
79 static void dns_tcp_send(struct stream_connection *conn, uint16_t flags)
80 {
81         struct dns_tcp_connection *dnsconn = talloc_get_type(conn->private_data,
82                                                              struct dns_tcp_connection);
83         /* this should never be triggered! */
84         dns_tcp_terminate_connection(dnsconn, "dns_tcp_send: called");
85 }
86
87 bool dns_process(struct dns_server *dns,
88                  TALLOC_CTX *mem_ctx,
89                  DATA_BLOB *in,
90                  DATA_BLOB *out)
91 {
92         enum ndr_err_code ndr_err;
93         struct dns_name_packet *packet = talloc(mem_ctx, struct dns_name_packet);
94         if (packet == NULL) return false;
95
96         dump_data(0, in->data, in->length);
97
98         ndr_err = ndr_pull_struct_blob(in, packet, packet,
99                         (ndr_pull_flags_fn_t)ndr_pull_dns_name_packet);
100         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
101                 TALLOC_FREE(packet);
102                 DEBUG(0, ("Failed to parse packet %d!\n", ndr_err));
103                 return false;
104         }
105
106         NDR_PRINT_DEBUG(dns_name_packet, packet);
107         return true;
108 }
109
110 struct dns_tcp_call {
111         struct dns_tcp_connection *dns_conn;
112         DATA_BLOB in;
113         DATA_BLOB out;
114         uint8_t out_hdr[4];
115         struct iovec out_iov[2];
116 };
117
118 static void dns_tcp_call_writev_done(struct tevent_req *subreq);
119
120 static void dns_tcp_call_loop(struct tevent_req *subreq)
121 {
122         struct dns_tcp_connection *dns_conn = tevent_req_callback_data(subreq,
123                                       struct dns_tcp_connection);
124         struct dns_tcp_call *call;
125         NTSTATUS status;
126         bool ok;
127
128         call = talloc(dns_conn, struct dns_tcp_call);
129         if (call == NULL) {
130                 dns_tcp_terminate_connection(dns_conn, "dns_tcp_call_loop: "
131                                 "no memory for dns_tcp_call");
132                 return;
133         }
134         call->dns_conn = dns_conn;
135
136         status = tstream_read_pdu_blob_recv(subreq,
137                                             call,
138                                             &call->in);
139         TALLOC_FREE(subreq);
140         if (!NT_STATUS_IS_OK(status)) {
141                 const char *reason;
142
143                 reason = talloc_asprintf(call, "dns_tcp_call_loop: "
144                                          "tstream_read_pdu_blob_recv() - %s",
145                                          nt_errstr(status));
146                 if (!reason) {
147                         reason = nt_errstr(status);
148                 }
149
150                 dns_tcp_terminate_connection(dns_conn, reason);
151                 return;
152         }
153
154         DEBUG(10,("Received krb5 TCP packet of length %lu from %s\n",
155                  (long) call->in.length,
156                  tsocket_address_string(dns_conn->conn->remote_address, call)));
157
158         /* skip length header */
159         call->in.data +=4;
160         call->in.length -= 4;
161
162         /* Call dns */
163         ok = dns_process(dns_conn->dns_socket->dns, call, &call->in, &call->out);
164         if (!ok) {
165                 dns_tcp_terminate_connection(dns_conn,
166                                 "dns_tcp_call_loop: process function failed");
167                 return;
168         }
169
170         /* First add the length of the out buffer */
171         RSIVAL(call->out_hdr, 0, call->out.length);
172         call->out_iov[0].iov_base = (char *) call->out_hdr;
173         call->out_iov[0].iov_len = 4;
174
175         call->out_iov[1].iov_base = (char *) call->out.data;
176         call->out_iov[1].iov_len = call->out.length;
177
178         subreq = tstream_writev_queue_send(call,
179                                            dns_conn->conn->event.ctx,
180                                            dns_conn->tstream,
181                                            dns_conn->send_queue,
182                                            call->out_iov, 2);
183         if (subreq == NULL) {
184                 dns_tcp_terminate_connection(dns_conn, "dns_tcp_call_loop: "
185                                 "no memory for tstream_writev_queue_send");
186                 return;
187         }
188         tevent_req_set_callback(subreq, dns_tcp_call_writev_done, call);
189
190         /*
191          * The krb5 tcp pdu's has the length as 4 byte (initial_read_size),
192          * packet_full_request_u32 provides the pdu length then.
193          */
194         subreq = tstream_read_pdu_blob_send(dns_conn,
195                                             dns_conn->conn->event.ctx,
196                                             dns_conn->tstream,
197                                             4, /* initial_read_size */
198                                             packet_full_request_u32,
199                                             dns_conn);
200         if (subreq == NULL) {
201                 dns_tcp_terminate_connection(dns_conn, "dns_tcp_call_loop: "
202                                 "no memory for tstream_read_pdu_blob_send");
203                 return;
204         }
205         tevent_req_set_callback(subreq, dns_tcp_call_loop, dns_conn);
206 }
207
208 static void dns_tcp_call_writev_done(struct tevent_req *subreq)
209 {
210         struct dns_tcp_call *call = tevent_req_callback_data(subreq,
211                         struct dns_tcp_call);
212         int sys_errno;
213         int rc;
214
215         rc = tstream_writev_queue_recv(subreq, &sys_errno);
216         TALLOC_FREE(subreq);
217         if (rc == -1) {
218                 const char *reason;
219
220                 reason = talloc_asprintf(call, "dns_tcp_call_writev_done: "
221                                          "tstream_writev_queue_recv() - %d:%s",
222                                          sys_errno, strerror(sys_errno));
223                 if (!reason) {
224                         reason = "dns_tcp_call_writev_done: tstream_writev_queue_recv() failed";
225                 }
226
227                 dns_tcp_terminate_connection(call->dns_conn, reason);
228                 return;
229         }
230
231         /* We don't care about errors */
232
233         talloc_free(call);
234 }
235
236 /*
237   called when we get a new connection
238 */
239 static void dns_tcp_accept(struct stream_connection *conn)
240 {
241         struct dns_socket *dns_socket;
242         struct dns_tcp_connection *dns_conn;
243         struct tevent_req *subreq;
244         int rc;
245
246         dns_conn = talloc_zero(conn, struct dns_tcp_connection);
247         if (dns_conn == NULL) {
248                 stream_terminate_connection(conn,
249                                 "dns_tcp_accept: out of memory");
250                 return;
251         }
252
253         dns_conn->send_queue = tevent_queue_create(conn, "dns_tcp_accept");
254         if (dns_conn->send_queue == NULL) {
255                 stream_terminate_connection(conn,
256                                 "dns_tcp_accept: out of memory");
257                 return;
258         }
259
260         dns_socket = talloc_get_type(conn->private_data, struct dns_socket);
261
262         TALLOC_FREE(conn->event.fde);
263
264         rc = tstream_bsd_existing_socket(dns_conn,
265                         socket_get_fd(conn->socket),
266                         &dns_conn->tstream);
267         if (rc < 0) {
268                 stream_terminate_connection(conn,
269                                 "dns_tcp_accept: out of memory");
270                 return;
271         }
272
273         dns_conn->conn = conn;
274         dns_conn->dns_socket = dns_socket;
275         conn->private_data = dns_conn;
276
277         /*
278          * The krb5 tcp pdu's has the length as 4 byte (initial_read_size),
279          * packet_full_request_u32 provides the pdu length then.
280          */
281         subreq = tstream_read_pdu_blob_send(dns_conn,
282                                             dns_conn->conn->event.ctx,
283                                             dns_conn->tstream,
284                                             4, /* initial_read_size */
285                                             packet_full_request_u32,
286                                             dns_conn);
287         if (subreq == NULL) {
288                 dns_tcp_terminate_connection(dns_conn, "dns_tcp_accept: "
289                                 "no memory for tstream_read_pdu_blob_send");
290                 return;
291         }
292         tevent_req_set_callback(subreq, dns_tcp_call_loop, dns_conn);
293 }
294
295 static const struct stream_server_ops dns_tcp_stream_ops = {
296         .name                   = "dns_tcp",
297         .accept_connection      = dns_tcp_accept,
298         .recv_handler           = dns_tcp_recv,
299         .send_handler           = dns_tcp_send
300 };
301
302 struct dns_udp_call {
303         struct tsocket_address *src;
304         DATA_BLOB in;
305         DATA_BLOB out;
306 };
307
308 static void dns_udp_call_sendto_done(struct tevent_req *subreq);
309
310 static void dns_udp_call_loop(struct tevent_req *subreq)
311 {
312         struct dns_udp_socket *sock = tevent_req_callback_data(subreq,
313                                       struct dns_udp_socket);
314         struct dns_udp_call *call;
315         uint8_t *buf;
316         ssize_t len;
317         int sys_errno;
318         bool ok;
319
320         call = talloc(sock, struct dns_udp_call);
321         if (call == NULL) {
322                 talloc_free(call);
323                 goto done;
324         }
325
326         len = tdgram_recvfrom_recv(subreq, &sys_errno,
327                                    call, &buf, &call->src);
328         TALLOC_FREE(subreq);
329         if (len == -1) {
330                 talloc_free(call);
331                 goto done;
332         }
333
334         call->in.data = buf;
335         call->in.length = len;
336
337         DEBUG(10,("Received krb5 UDP packet of length %lu from %s\n",
338                  (long)call->in.length,
339                  tsocket_address_string(call->src, call)));
340
341         /* Call krb5 */
342         ok = dns_process(sock->dns_socket->dns, call, &call->in, &call->out);
343         if (!ok) {
344                 talloc_free(call);
345                 goto done;
346         }
347
348         subreq = tdgram_sendto_queue_send(call,
349                                           sock->dns_socket->dns->task->event_ctx,
350                                           sock->dgram,
351                                           sock->send_queue,
352                                           call->out.data,
353                                           call->out.length,
354                                           call->src);
355         if (subreq == NULL) {
356                 talloc_free(call);
357                 goto done;
358         }
359         tevent_req_set_callback(subreq, dns_udp_call_sendto_done, call);
360
361 done:
362         subreq = tdgram_recvfrom_send(sock,
363                                       sock->dns_socket->dns->task->event_ctx,
364                                       sock->dgram);
365         if (subreq == NULL) {
366                 task_server_terminate(sock->dns_socket->dns->task,
367                                       "no memory for tdgram_recvfrom_send",
368                                       true);
369                 return;
370         }
371         tevent_req_set_callback(subreq, dns_udp_call_loop, sock);
372 }
373
374 static void dns_udp_call_sendto_done(struct tevent_req *subreq)
375 {
376         struct dns_udp_call *call = tevent_req_callback_data(subreq,
377                                        struct dns_udp_call);
378         ssize_t ret;
379         int sys_errno;
380
381         ret = tdgram_sendto_queue_recv(subreq, &sys_errno);
382
383         /* We don't care about errors */
384
385         talloc_free(call);
386 }
387
388 /*
389   start listening on the given address
390 */
391 static NTSTATUS dns_add_socket(struct dns_server *dns,
392                                const struct model_ops *model_ops,
393                                const char *name,
394                                const char *address,
395                                uint16_t port)
396 {
397         struct dns_socket *dns_socket;
398         struct dns_udp_socket *dns_udp_socket;
399         struct tevent_req *udpsubreq;
400         NTSTATUS status;
401         int ret;
402
403         dns_socket = talloc(dns, struct dns_socket);
404         NT_STATUS_HAVE_NO_MEMORY(dns_socket);
405
406         dns_socket->dns = dns;
407
408         ret = tsocket_address_inet_from_strings(dns_socket, "ip",
409                                                 address, port,
410                                                 &dns_socket->local_address);
411         if (ret != 0) {
412                 status = map_nt_error_from_unix(errno);
413                 return status;
414         }
415
416         status = stream_setup_socket(dns->task->event_ctx,
417                                      dns->task->lp_ctx,
418                                      model_ops,
419                                      &dns_tcp_stream_ops,
420                                      "ip", address, &port,
421                                      lpcfg_socket_options(dns->task->lp_ctx),
422                                      dns_socket);
423         if (!NT_STATUS_IS_OK(status)) {
424                 DEBUG(0,("Failed to bind to %s:%u TCP - %s\n",
425                          address, port, nt_errstr(status)));
426                 talloc_free(dns_socket);
427                 return status;
428         }
429
430         dns_udp_socket = talloc(dns_socket, struct dns_udp_socket);
431         NT_STATUS_HAVE_NO_MEMORY(dns_udp_socket);
432
433         dns_udp_socket->dns_socket = dns_socket;
434
435         ret = tdgram_inet_udp_socket(dns_socket->local_address,
436                                      NULL,
437                                      dns_udp_socket,
438                                      &dns_udp_socket->dgram);
439         if (ret != 0) {
440                 status = map_nt_error_from_unix(errno);
441                 DEBUG(0,("Failed to bind to %s:%u UDP - %s\n",
442                          address, port, nt_errstr(status)));
443                 return status;
444         }
445
446         dns_udp_socket->send_queue = tevent_queue_create(dns_udp_socket,
447                                                          "dns_udp_send_queue");
448         NT_STATUS_HAVE_NO_MEMORY(dns_udp_socket->send_queue);
449
450         udpsubreq = tdgram_recvfrom_send(dns_udp_socket,
451                                          dns->task->event_ctx,
452                                          dns_udp_socket->dgram);
453         NT_STATUS_HAVE_NO_MEMORY(udpsubreq);
454         tevent_req_set_callback(udpsubreq, dns_udp_call_loop, dns_udp_socket);
455
456         return NT_STATUS_OK;
457 }
458
459 /*
460   setup our listening sockets on the configured network interfaces
461 */
462 static NTSTATUS dns_startup_interfaces(struct dns_server *dns, struct loadparm_context *lp_ctx,
463                                        struct interface *ifaces)
464 {
465         const struct model_ops *model_ops;
466         int num_interfaces;
467         TALLOC_CTX *tmp_ctx = talloc_new(dns);
468         NTSTATUS status;
469         int i;
470
471         /* within the dns task we want to be a single process, so
472            ask for the single process model ops and pass these to the
473            stream_setup_socket() call. */
474         model_ops = process_model_startup(dns->task->event_ctx, "single");
475         if (!model_ops) {
476                 DEBUG(0,("Can't find 'single' process model_ops\n"));
477                 return NT_STATUS_INTERNAL_ERROR;
478         }
479
480         num_interfaces = iface_count(ifaces);
481
482         for (i=0; i<num_interfaces; i++) {
483                 const char *address = talloc_strdup(tmp_ctx, iface_n_ip(ifaces, i));
484
485                 status = dns_add_socket(dns, model_ops, "dns", address, DNS_SERVICE_PORT);
486                 NT_STATUS_NOT_OK_RETURN(status);
487         }
488
489         talloc_free(tmp_ctx);
490
491         return NT_STATUS_OK;
492 }
493 static void dns_task_init(struct task_server *task)
494 {
495         struct dns_server *dns;
496         NTSTATUS status;
497         struct interface *ifaces;
498
499         switch (lpcfg_server_role(task->lp_ctx)) {
500         case ROLE_STANDALONE:
501                 task_server_terminate(task, "dns: no DNS required in standalone configuration", false);
502                 return;
503         case ROLE_DOMAIN_MEMBER:
504                 task_server_terminate(task, "dns: no DNS required in member server configuration", false);
505                 return;
506         case ROLE_DOMAIN_CONTROLLER:
507                 /* Yes, we want a DNS */
508                 break;
509         }
510
511         load_interfaces(task, lpcfg_interfaces(task->lp_ctx), &ifaces);
512
513         if (iface_count(ifaces) == 0) {
514                 task_server_terminate(task, "dns: no network interfaces configured", false);
515                 return;
516         }
517
518         task_server_set_title(task, "task[dns]");
519
520         dns = talloc(task, struct dns_server);
521         if (dns == NULL) {
522                 task_server_terminate(task, "dns: out of memory", true);
523                 return;
524         }
525
526         dns->task = task;
527
528         status = dns_startup_interfaces(dns, task->lp_ctx, ifaces);
529         if (!NT_STATUS_IS_OK(status)) {
530                 task_server_terminate(task, "dns failed to setup interfaces", true);
531                 return;
532         }
533 }
534
535 NTSTATUS server_service_dns_init(void)
536 {
537         return register_server_service("dns", dns_task_init);
538 }