2 Unix SMB/CIFS implementation.
4 WINS Replication server
6 Copyright (C) Stefan Metzmacher 2005
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 "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"
38 void wreplsrv_terminate_in_connection(struct wreplsrv_in_connection
*wreplconn
, const char *reason
)
40 stream_terminate_connection(wreplconn
->conn
, reason
);
44 receive some data on a WREPL connection
46 static NTSTATUS
wreplsrv_process(struct wreplsrv_in_connection
*wrepl_conn
,
47 struct wreplsrv_in_call
**_call
)
49 struct wrepl_wrap packet_out_wrap
;
51 enum ndr_err_code ndr_err
;
52 struct wreplsrv_in_call
*call
= *_call
;
54 ndr_err
= ndr_pull_struct_blob(&call
->in
, call
,
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
);
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
);
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"));
77 /* and now encode the reply */
78 packet_out_wrap
.packet
= call
->rep_packet
;
79 ndr_err
= ndr_push_struct_blob(&call
->out
, call
,
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
);
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
);
95 static void wreplsrv_call_loop(struct tevent_req
*subreq
);
98 called when we get a new connection
100 static void wreplsrv_accept(struct stream_connection
*conn
)
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
;
106 struct tevent_req
*subreq
;
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");
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");
123 TALLOC_FREE(conn
->event
.fde
);
126 * Clone the fd that the connection isn't closed if we create a client
129 fd
= dup(socket_get_fd(conn
->socket
));
133 reason
= talloc_asprintf(conn
,
134 "wrepl_accept: failed to duplicate the file descriptor - %s",
136 if (reason
== NULL
) {
137 reason
= strerror(errno
);
139 stream_terminate_connection(conn
, reason
);
141 rc
= tstream_bsd_existing_socket(wrepl_conn
,
143 &wrepl_conn
->tstream
);
145 stream_terminate_connection(conn
,
146 "wrepl_accept: out of memory");
150 wrepl_conn
->conn
= conn
;
151 wrepl_conn
->service
= service
;
153 peer_addr
= conn
->remote_address
;
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: "
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");
170 wrepl_conn
->partner
= wreplsrv_find_partner(service
, peer_ip
);
171 irpc_add_name(conn
->msg_ctx
, "wreplsrv_connection");
174 * The wrepl pdu's has the length as 4 byte (initial_read_size),
175 * packet_full_request_u32 provides the pdu length then.
177 subreq
= tstream_read_pdu_blob_send(wrepl_conn
,
178 wrepl_conn
->conn
->event
.ctx
,
180 4, /* initial_read_size */
181 packet_full_request_u32
,
183 if (subreq
== NULL
) {
184 wreplsrv_terminate_in_connection(wrepl_conn
, "wrepl_accept: "
185 "no memory for tstream_read_pdu_blob_send");
188 tevent_req_set_callback(subreq
, wreplsrv_call_loop
, wrepl_conn
);
191 static void wreplsrv_call_writev_done(struct tevent_req
*subreq
);
193 static void wreplsrv_call_loop(struct tevent_req
*subreq
)
195 struct wreplsrv_in_connection
*wrepl_conn
= tevent_req_callback_data(subreq
,
196 struct wreplsrv_in_connection
);
197 struct wreplsrv_in_call
*call
;
200 call
= talloc_zero(wrepl_conn
, struct wreplsrv_in_call
);
202 wreplsrv_terminate_in_connection(wrepl_conn
, "wreplsrv_call_loop: "
203 "no memory for wrepl_samba3_call");
206 call
->wreplconn
= wrepl_conn
;
208 status
= tstream_read_pdu_blob_recv(subreq
,
212 if (!NT_STATUS_IS_OK(status
)) {
215 reason
= talloc_asprintf(call
, "wreplsrv_call_loop: "
216 "tstream_read_pdu_blob_recv() - %s",
219 reason
= nt_errstr(status
);
222 wreplsrv_terminate_in_connection(wrepl_conn
, reason
);
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
)));
230 /* skip length header */
232 call
->in
.length
-= 4;
234 status
= wreplsrv_process(wrepl_conn
, &call
);
235 if (!NT_STATUS_IS_OK(status
)) {
238 reason
= talloc_asprintf(call
, "wreplsrv_call_loop: "
239 "tstream_read_pdu_blob_recv() - %s",
241 if (reason
== NULL
) {
242 reason
= nt_errstr(status
);
245 wreplsrv_terminate_in_connection(wrepl_conn
, reason
);
249 /* We handed over the connection so we're done here */
250 if (wrepl_conn
->tstream
== NULL
) {
254 /* Invalid WINS-Replication packet, we just ignore it */
259 call
->out_iov
[0].iov_base
= call
->out
.data
;
260 call
->out_iov
[0].iov_len
= call
->out
.length
;
262 subreq
= tstream_writev_queue_send(call
,
263 wrepl_conn
->conn
->event
.ctx
,
265 wrepl_conn
->send_queue
,
267 if (subreq
== NULL
) {
268 wreplsrv_terminate_in_connection(wrepl_conn
, "wreplsrv_call_loop: "
269 "no memory for tstream_writev_queue_send");
272 tevent_req_set_callback(subreq
, wreplsrv_call_writev_done
, call
);
276 * The wrepl pdu's has the length as 4 byte (initial_read_size),
277 * provides the pdu length then.
279 subreq
= tstream_read_pdu_blob_send(wrepl_conn
,
280 wrepl_conn
->conn
->event
.ctx
,
282 4, /* initial_read_size */
283 packet_full_request_u32
,
285 if (subreq
== NULL
) {
286 wreplsrv_terminate_in_connection(wrepl_conn
, "wreplsrv_call_loop: "
287 "no memory for tstream_read_pdu_blob_send");
290 tevent_req_set_callback(subreq
, wreplsrv_call_loop
, wrepl_conn
);
293 static void wreplsrv_call_writev_done(struct tevent_req
*subreq
)
295 struct wreplsrv_in_call
*call
= tevent_req_callback_data(subreq
,
296 struct wreplsrv_in_call
);
300 rc
= tstream_writev_queue_recv(subreq
, &sys_errno
);
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";
313 wreplsrv_terminate_in_connection(call
->wreplconn
, reason
);
317 if (call
->terminate_after_send
) {
318 wreplsrv_terminate_in_connection(call
->wreplconn
,
319 "wreplsrv_in_connection: terminate_after_send");
329 static void wreplsrv_recv(struct stream_connection
*conn
, uint16_t flags
)
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");
339 called when we can write to a connection
341 static void wreplsrv_send(struct stream_connection
*conn
, uint16_t flags
)
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");
350 static const struct stream_server_ops wreplsrv_stream_ops
= {
352 .accept_connection
= wreplsrv_accept
,
353 .recv_handler
= wreplsrv_recv
,
354 .send_handler
= wreplsrv_send
,
358 called when we get a new connection
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
)
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
;
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");
377 DEBUG(0,("Can't find 'single' process model_ops"));
378 return NT_STATUS_INTERNAL_ERROR
;
381 wrepl_in
= talloc_zero(partner
, struct wreplsrv_in_connection
);
382 NT_STATUS_HAVE_NO_MEMORY(wrepl_in
);
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
;
389 status
= stream_new_connection_merge(service
->task
->event_ctx
,
390 service
->task
->lp_ctx
,
392 &wreplsrv_stream_ops
,
393 service
->task
->msg_ctx
,
396 NT_STATUS_NOT_OK_RETURN(status
);
399 * make the wreplsrv_in_connection structure a child of the
400 * stream_connection, to match the hierarchy of wreplsrv_accept
402 wrepl_in
->conn
= conn
;
403 talloc_steal(conn
, wrepl_in
);
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
;
413 * The wrepl pdu's has the length as 4 byte (initial_read_size),
414 * packet_full_request_u32 provides the pdu length then.
416 subreq
= tstream_read_pdu_blob_send(wrepl_in
,
417 wrepl_in
->conn
->event
.ctx
,
419 4, /* initial_read_size */
420 packet_full_request_u32
,
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
;
427 tevent_req_set_callback(subreq
, wreplsrv_call_loop
, wrepl_in
);
429 *_wrepl_in
= wrepl_in
;
435 startup the wrepl port 42 server sockets
437 NTSTATUS
wreplsrv_setup_sockets(struct wreplsrv_service
*service
, struct loadparm_context
*lp_ctx
)
440 struct task_server
*task
= service
->task
;
441 const struct model_ops
*model_ops
;
443 uint16_t port
= WINS_REPLICATION_PORT
;
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");
450 DEBUG(0,("Can't find 'single' process model_ops"));
451 return NT_STATUS_INTERNAL_ERROR
;
454 if (lp_interfaces(lp_ctx
) && lp_bind_interfaces_only(lp_ctx
)) {
457 struct interface
*ifaces
;
459 load_interfaces(task
, lp_interfaces(lp_ctx
), &ifaces
);
461 num_interfaces
= iface_count(ifaces
);
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.
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 lp_socket_options(task
->lp_ctx
),
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
)));
482 address
= lp_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
, lp_socket_options(task
->lp_ctx
),
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
)));