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