4920aa0328fe2990e7001a205140474ac996334d
[kai/samba-autobuild/.git] / source4 / wrepl_server / wrepl_in_connection.c
1 /* 
2    Unix SMB/CIFS implementation.
3    
4    WINS Replication server
5    
6    Copyright (C) Stefan Metzmacher      2005
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 "lib/socket/socket.h"
24 #include "lib/stream/packet.h"
25 #include "smbd/service_task.h"
26 #include "smbd/service_stream.h"
27 #include "smbd/service.h"
28 #include "lib/messaging/irpc.h"
29 #include "librpc/gen_ndr/ndr_winsrepl.h"
30 #include "wrepl_server/wrepl_server.h"
31 #include "smbd/process_model.h"
32 #include "system/network.h"
33 #include "lib/socket/netif.h"
34 #include "lib/tsocket/tsocket.h"
35 #include "libcli/util/tstream.h"
36 #include "param/param.h"
37
38 void wreplsrv_terminate_in_connection(struct wreplsrv_in_connection *wreplconn, const char *reason)
39 {
40         stream_terminate_connection(wreplconn->conn, reason);
41 }
42
43 /*
44   receive some data on a WREPL connection
45 */
46 static NTSTATUS wreplsrv_process(struct wreplsrv_in_connection *wrepl_conn,
47                                  struct wreplsrv_in_call **_call)
48 {
49         struct wrepl_wrap packet_out_wrap;
50         NTSTATUS status;
51         enum ndr_err_code ndr_err;
52         struct wreplsrv_in_call *call = *_call;
53
54         ndr_err = ndr_pull_struct_blob(&call->in, call,
55                                        &call->req_packet,
56                                        (ndr_pull_flags_fn_t)ndr_pull_wrepl_packet);
57         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
58                 return ndr_map_error2ntstatus(ndr_err);
59         }
60
61         if (DEBUGLVL(10)) {
62                 DEBUG(10,("Received WINS-Replication packet of length %u\n",
63                           (unsigned int) call->in.length + 4));
64                 NDR_PRINT_DEBUG(wrepl_packet, &call->req_packet);
65         }
66
67         status = wreplsrv_in_call(call);
68         if (NT_STATUS_IS_ERR(status)) {
69                 return status;
70         }
71         if (!NT_STATUS_IS_OK(status)) {
72                 /* w2k just ignores invalid packets, so we do */
73                 DEBUG(10,("Received WINS-Replication packet was invalid, we just ignore it\n"));
74                 TALLOC_FREE(call);
75                 *_call = NULL;
76                 return NT_STATUS_OK;
77         }
78
79         /* and now encode the reply */
80         packet_out_wrap.packet = call->rep_packet;
81         ndr_err = ndr_push_struct_blob(&call->out, call,
82                                        &packet_out_wrap,
83                                        (ndr_push_flags_fn_t) ndr_push_wrepl_wrap);
84         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
85                 return ndr_map_error2ntstatus(ndr_err);
86         }
87
88         if (DEBUGLVL(10)) {
89                 DEBUG(10,("Sending WINS-Replication packet of length %u\n",
90                          (unsigned int) call->out.length));
91                 NDR_PRINT_DEBUG(wrepl_packet, &call->rep_packet);
92         }
93
94         return NT_STATUS_OK;
95 }
96
97 static void wreplsrv_call_loop(struct tevent_req *subreq);
98
99 /*
100   called when we get a new connection
101 */
102 static void wreplsrv_accept(struct stream_connection *conn)
103 {
104         struct wreplsrv_service *service = talloc_get_type(conn->private_data, struct wreplsrv_service);
105         struct wreplsrv_in_connection *wrepl_conn;
106         struct tsocket_address *peer_addr;
107         char *peer_ip;
108         struct tevent_req *subreq;
109         int rc;
110
111         wrepl_conn = talloc_zero(conn, struct wreplsrv_in_connection);
112         if (wrepl_conn == NULL) {
113                 stream_terminate_connection(conn,
114                                             "wreplsrv_accept: out of memory");
115                 return;
116         }
117
118         wrepl_conn->send_queue = tevent_queue_create(conn, "wrepl_accept");
119         if (wrepl_conn->send_queue == NULL) {
120                 stream_terminate_connection(conn,
121                                             "wrepl_accept: out of memory");
122                 return;
123         }
124
125         TALLOC_FREE(conn->event.fde);
126
127         rc = tstream_bsd_existing_socket(wrepl_conn,
128                                          socket_get_fd(conn->socket),
129                                          &wrepl_conn->tstream);
130         if (rc < 0) {
131                 stream_terminate_connection(conn,
132                                             "wrepl_accept: out of memory");
133                 return;
134         }
135         socket_set_flags(conn->socket, SOCKET_FLAG_NOCLOSE);
136
137         wrepl_conn->conn = conn;
138         wrepl_conn->service = service;
139
140         peer_addr = conn->remote_address;
141
142         if (!tsocket_address_is_inet(peer_addr, "ipv4")) {
143                 DEBUG(0,("wreplsrv_accept: non ipv4 peer addr '%s'\n",
144                         tsocket_address_string(peer_addr, wrepl_conn)));
145                 wreplsrv_terminate_in_connection(wrepl_conn, "wreplsrv_accept: "
146                                 "invalid peer IP");
147                 return;
148         }
149
150         peer_ip = tsocket_address_inet_addr_string(peer_addr, wrepl_conn);
151         if (peer_ip == NULL) {
152                 wreplsrv_terminate_in_connection(wrepl_conn, "wreplsrv_accept: "
153                                 "could not convert peer IP into a string");
154                 return;
155         }
156
157         wrepl_conn->partner = wreplsrv_find_partner(service, peer_ip);
158         irpc_add_name(conn->msg_ctx, "wreplsrv_connection");
159
160         /*
161          * The wrepl pdu's has the length as 4 byte (initial_read_size),
162          * packet_full_request_u32 provides the pdu length then.
163          */
164         subreq = tstream_read_pdu_blob_send(wrepl_conn,
165                                             wrepl_conn->conn->event.ctx,
166                                             wrepl_conn->tstream,
167                                             4, /* initial_read_size */
168                                             packet_full_request_u32,
169                                             wrepl_conn);
170         if (subreq == NULL) {
171                 wreplsrv_terminate_in_connection(wrepl_conn, "wrepl_accept: "
172                                 "no memory for tstream_read_pdu_blob_send");
173                 return;
174         }
175         tevent_req_set_callback(subreq, wreplsrv_call_loop, wrepl_conn);
176 }
177
178 static void wreplsrv_call_writev_done(struct tevent_req *subreq);
179
180 static void wreplsrv_call_loop(struct tevent_req *subreq)
181 {
182         struct wreplsrv_in_connection *wrepl_conn = tevent_req_callback_data(subreq,
183                                       struct wreplsrv_in_connection);
184         struct wreplsrv_in_call *call;
185         NTSTATUS status;
186
187         call = talloc_zero(wrepl_conn, struct wreplsrv_in_call);
188         if (call == NULL) {
189                 wreplsrv_terminate_in_connection(wrepl_conn, "wreplsrv_call_loop: "
190                                 "no memory for wrepl_samba3_call");
191                 return;
192         }
193         call->wreplconn = wrepl_conn;
194
195         status = tstream_read_pdu_blob_recv(subreq,
196                                             call,
197                                             &call->in);
198         TALLOC_FREE(subreq);
199         if (!NT_STATUS_IS_OK(status)) {
200                 const char *reason;
201
202                 reason = talloc_asprintf(call, "wreplsrv_call_loop: "
203                                          "tstream_read_pdu_blob_recv() - %s",
204                                          nt_errstr(status));
205                 if (!reason) {
206                         reason = nt_errstr(status);
207                 }
208
209                 wreplsrv_terminate_in_connection(wrepl_conn, reason);
210                 return;
211         }
212
213         DEBUG(10,("Received wrepl packet of length %lu from %s\n",
214                  (long) call->in.length,
215                  tsocket_address_string(wrepl_conn->conn->remote_address, call)));
216
217         /* skip length header */
218         call->in.data += 4;
219         call->in.length -= 4;
220
221         status = wreplsrv_process(wrepl_conn, &call);
222         if (!NT_STATUS_IS_OK(status)) {
223                 const char *reason;
224
225                 reason = talloc_asprintf(call, "wreplsrv_call_loop: "
226                                          "tstream_read_pdu_blob_recv() - %s",
227                                          nt_errstr(status));
228                 if (reason == NULL) {
229                         reason = nt_errstr(status);
230                 }
231
232                 wreplsrv_terminate_in_connection(wrepl_conn, reason);
233                 return;
234         }
235
236         /* We handed over the connection so we're done here */
237         if (wrepl_conn->tstream == NULL) {
238             return;
239         }
240
241         /* Invalid WINS-Replication packet, we just ignore it */
242         if (call == NULL) {
243                 goto noreply;
244         }
245
246         call->out_iov[0].iov_base = (char *) call->out.data;
247         call->out_iov[0].iov_len = call->out.length;
248
249         subreq = tstream_writev_queue_send(call,
250                                            wrepl_conn->conn->event.ctx,
251                                            wrepl_conn->tstream,
252                                            wrepl_conn->send_queue,
253                                            call->out_iov, 1);
254         if (subreq == NULL) {
255                 wreplsrv_terminate_in_connection(wrepl_conn, "wreplsrv_call_loop: "
256                                 "no memory for tstream_writev_queue_send");
257                 return;
258         }
259         tevent_req_set_callback(subreq, wreplsrv_call_writev_done, call);
260
261 noreply:
262         /*
263          * The wrepl pdu's has the length as 4 byte (initial_read_size),
264          *  provides the pdu length then.
265          */
266         subreq = tstream_read_pdu_blob_send(wrepl_conn,
267                                             wrepl_conn->conn->event.ctx,
268                                             wrepl_conn->tstream,
269                                             4, /* initial_read_size */
270                                             packet_full_request_u32,
271                                             wrepl_conn);
272         if (subreq == NULL) {
273                 wreplsrv_terminate_in_connection(wrepl_conn, "wreplsrv_call_loop: "
274                                 "no memory for tstream_read_pdu_blob_send");
275                 return;
276         }
277         tevent_req_set_callback(subreq, wreplsrv_call_loop, wrepl_conn);
278 }
279
280 static void wreplsrv_call_writev_done(struct tevent_req *subreq)
281 {
282         struct wreplsrv_in_call *call = tevent_req_callback_data(subreq,
283                         struct wreplsrv_in_call);
284         int sys_errno;
285         int rc;
286
287         rc = tstream_writev_queue_recv(subreq, &sys_errno);
288         TALLOC_FREE(subreq);
289         if (rc == -1) {
290                 const char *reason;
291
292                 reason = talloc_asprintf(call, "wreplsrv_call_writev_done: "
293                                          "tstream_writev_queue_recv() - %d:%s",
294                                          sys_errno, strerror(sys_errno));
295                 if (reason == NULL) {
296                         reason = "wreplsrv_call_writev_done: "
297                                  "tstream_writev_queue_recv() failed";
298                 }
299
300                 wreplsrv_terminate_in_connection(call->wreplconn, reason);
301                 return;
302         }
303
304         if (call->terminate_after_send) {
305                 wreplsrv_terminate_in_connection(call->wreplconn,
306                                 "wreplsrv_in_connection: terminate_after_send");
307                 return;
308         }
309
310         talloc_free(call);
311 }
312
313 /*
314   called on a tcp recv
315 */
316 static void wreplsrv_recv(struct stream_connection *conn, uint16_t flags)
317 {
318         struct wreplsrv_in_connection *wrepl_conn = talloc_get_type(conn->private_data,
319                                                         struct wreplsrv_in_connection);
320         /* this should never be triggered! */
321         DEBUG(0,("Terminating connection - '%s'\n", "wrepl_recv: called"));
322         wreplsrv_terminate_in_connection(wrepl_conn, "wrepl_recv: called");
323 }
324
325 /*
326   called when we can write to a connection
327 */
328 static void wreplsrv_send(struct stream_connection *conn, uint16_t flags)
329 {
330         struct wreplsrv_in_connection *wrepl_conn = talloc_get_type(conn->private_data,
331                                                         struct wreplsrv_in_connection);
332         /* this should never be triggered! */
333         DEBUG(0,("Terminating connection - '%s'\n", "wrepl_send: called"));
334         wreplsrv_terminate_in_connection(wrepl_conn, "wrepl_send: called");
335 }
336
337 static const struct stream_server_ops wreplsrv_stream_ops = {
338         .name                   = "wreplsrv",
339         .accept_connection      = wreplsrv_accept,
340         .recv_handler           = wreplsrv_recv,
341         .send_handler           = wreplsrv_send,
342 };
343
344 /*
345   called when we get a new connection
346 */
347 NTSTATUS wreplsrv_in_connection_merge(struct wreplsrv_partner *partner,
348                                       uint32_t peer_assoc_ctx,
349                                       struct tstream_context **stream,
350                                       struct wreplsrv_in_connection **_wrepl_in,
351                                       void* process_context)
352 {
353         struct wreplsrv_service *service = partner->service;
354         struct wreplsrv_in_connection *wrepl_in;
355         const struct model_ops *model_ops;
356         struct stream_connection *conn;
357         struct tevent_req *subreq;
358         NTSTATUS status;
359
360         /* within the wrepl task we want to be a single process, so
361            ask for the single process model ops and pass these to the
362            stream_setup_socket() call. */
363         model_ops = process_model_startup("single");
364         if (!model_ops) {
365                 DEBUG(0,("Can't find 'single' process model_ops"));
366                 return NT_STATUS_INTERNAL_ERROR;
367         }
368
369         wrepl_in = talloc_zero(partner, struct wreplsrv_in_connection);
370         NT_STATUS_HAVE_NO_MEMORY(wrepl_in);
371
372         wrepl_in->service       = service;
373         wrepl_in->partner       = partner;
374         wrepl_in->tstream       = talloc_move(wrepl_in, stream);
375         wrepl_in->assoc_ctx.peer_ctx = peer_assoc_ctx;
376
377         status = stream_new_connection_merge(service->task->event_ctx,
378                                              service->task->lp_ctx,
379                                              model_ops,
380                                              &wreplsrv_stream_ops,
381                                              service->task->msg_ctx,
382                                              wrepl_in,
383                                              &conn,
384                                              process_context);
385         NT_STATUS_NOT_OK_RETURN(status);
386
387         /*
388          * make the wreplsrv_in_connection structure a child of the
389          * stream_connection, to match the hierarchy of wreplsrv_accept
390          */
391         wrepl_in->conn          = conn;
392         talloc_steal(conn, wrepl_in);
393
394         wrepl_in->send_queue = tevent_queue_create(wrepl_in, "wreplsrv_in_connection_merge");
395         if (wrepl_in->send_queue == NULL) {
396                 stream_terminate_connection(conn,
397                                             "wreplsrv_in_connection_merge: out of memory");
398                 return NT_STATUS_NO_MEMORY;
399         }
400
401         /*
402          * The wrepl pdu's has the length as 4 byte (initial_read_size),
403          * packet_full_request_u32 provides the pdu length then.
404          */
405         subreq = tstream_read_pdu_blob_send(wrepl_in,
406                                             wrepl_in->conn->event.ctx,
407                                             wrepl_in->tstream,
408                                             4, /* initial_read_size */
409                                             packet_full_request_u32,
410                                             wrepl_in);
411         if (subreq == NULL) {
412                 wreplsrv_terminate_in_connection(wrepl_in, "wreplsrv_in_connection_merge: "
413                                 "no memory for tstream_read_pdu_blob_send");
414                 return NT_STATUS_NO_MEMORY;
415         }
416         tevent_req_set_callback(subreq, wreplsrv_call_loop, wrepl_in);
417
418         *_wrepl_in = wrepl_in;
419
420         return NT_STATUS_OK;
421 }
422
423 /*
424   startup the wrepl port 42 server sockets
425 */
426 NTSTATUS wreplsrv_setup_sockets(struct wreplsrv_service *service, struct loadparm_context *lp_ctx)
427 {
428         NTSTATUS status;
429         struct task_server *task = service->task;
430         const struct model_ops *model_ops;
431         const char *address;
432         uint16_t port = WINS_REPLICATION_PORT;
433
434         /* within the wrepl task we want to be a single process, so
435            ask for the single process model ops and pass these to the
436            stream_setup_socket() call. */
437         model_ops = process_model_startup("single");
438         if (!model_ops) {
439                 DEBUG(0,("Can't find 'single' process model_ops"));
440                 return NT_STATUS_INTERNAL_ERROR;
441         }
442
443         if (lpcfg_interfaces(lp_ctx) && lpcfg_bind_interfaces_only(lp_ctx)) {
444                 int num_interfaces;
445                 int i;
446                 struct interface *ifaces;
447
448                 load_interface_list(task, lp_ctx, &ifaces);
449
450                 num_interfaces = iface_list_count(ifaces);
451
452                 /* We have been given an interfaces line, and been 
453                    told to only bind to those interfaces. Create a
454                    socket per interface and bind to only these.
455                 */
456                 for(i = 0; i < num_interfaces; i++) {
457                         if (!iface_list_n_is_v4(ifaces, i)) {
458                                 continue;
459                         }
460                         address = iface_list_n_ip(ifaces, i);
461                         status = stream_setup_socket(task, task->event_ctx,
462                                                      task->lp_ctx, model_ops,
463                                                      &wreplsrv_stream_ops,
464                                                      "ipv4", address, &port, 
465                                                       lpcfg_socket_options(task->lp_ctx),
466                                                      service, task->process_context);
467                         if (!NT_STATUS_IS_OK(status)) {
468                                 DEBUG(0,("stream_setup_socket(address=%s,port=%u) failed - %s\n",
469                                          address, port, nt_errstr(status)));
470                                 return status;
471                         }
472                 }
473         } else {
474                 address = "0.0.0.0";
475                 status = stream_setup_socket(task, task->event_ctx, task->lp_ctx,
476                                              model_ops, &wreplsrv_stream_ops,
477                                              "ipv4", address, &port, lpcfg_socket_options(task->lp_ctx),
478                                              service, task->process_context);
479                 if (!NT_STATUS_IS_OK(status)) {
480                         DEBUG(0,("stream_setup_socket(address=%s,port=%u) failed - %s\n",
481                                  address, port, nt_errstr(status)));
482                         return status;
483                 }
484         }
485
486         return NT_STATUS_OK;
487 }