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