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
);
125 rc
= tstream_bsd_existing_socket(wrepl_conn
,
126 socket_get_fd(conn
->socket
),
127 &wrepl_conn
->tstream
);
129 stream_terminate_connection(conn
,
130 "wrepl_accept: out of memory");
133 socket_set_flags(conn
->socket
, SOCKET_FLAG_NOCLOSE
);
135 wrepl_conn
->conn
= conn
;
136 wrepl_conn
->service
= service
;
138 peer_addr
= conn
->remote_address
;
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: "
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");
155 wrepl_conn
->partner
= wreplsrv_find_partner(service
, peer_ip
);
156 irpc_add_name(conn
->msg_ctx
, "wreplsrv_connection");
159 * The wrepl pdu's has the length as 4 byte (initial_read_size),
160 * packet_full_request_u32 provides the pdu length then.
162 subreq
= tstream_read_pdu_blob_send(wrepl_conn
,
163 wrepl_conn
->conn
->event
.ctx
,
165 4, /* initial_read_size */
166 packet_full_request_u32
,
168 if (subreq
== NULL
) {
169 wreplsrv_terminate_in_connection(wrepl_conn
, "wrepl_accept: "
170 "no memory for tstream_read_pdu_blob_send");
173 tevent_req_set_callback(subreq
, wreplsrv_call_loop
, wrepl_conn
);
176 static void wreplsrv_call_writev_done(struct tevent_req
*subreq
);
178 static void wreplsrv_call_loop(struct tevent_req
*subreq
)
180 struct wreplsrv_in_connection
*wrepl_conn
= tevent_req_callback_data(subreq
,
181 struct wreplsrv_in_connection
);
182 struct wreplsrv_in_call
*call
;
185 call
= talloc_zero(wrepl_conn
, struct wreplsrv_in_call
);
187 wreplsrv_terminate_in_connection(wrepl_conn
, "wreplsrv_call_loop: "
188 "no memory for wrepl_samba3_call");
191 call
->wreplconn
= wrepl_conn
;
193 status
= tstream_read_pdu_blob_recv(subreq
,
197 if (!NT_STATUS_IS_OK(status
)) {
200 reason
= talloc_asprintf(call
, "wreplsrv_call_loop: "
201 "tstream_read_pdu_blob_recv() - %s",
204 reason
= nt_errstr(status
);
207 wreplsrv_terminate_in_connection(wrepl_conn
, reason
);
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
)));
215 /* skip length header */
217 call
->in
.length
-= 4;
219 status
= wreplsrv_process(wrepl_conn
, &call
);
220 if (!NT_STATUS_IS_OK(status
)) {
223 reason
= talloc_asprintf(call
, "wreplsrv_call_loop: "
224 "tstream_read_pdu_blob_recv() - %s",
226 if (reason
== NULL
) {
227 reason
= nt_errstr(status
);
230 wreplsrv_terminate_in_connection(wrepl_conn
, reason
);
234 /* We handed over the connection so we're done here */
235 if (wrepl_conn
->tstream
== NULL
) {
239 /* Invalid WINS-Replication packet, we just ignore it */
244 call
->out_iov
[0].iov_base
= (char *) call
->out
.data
;
245 call
->out_iov
[0].iov_len
= call
->out
.length
;
247 subreq
= tstream_writev_queue_send(call
,
248 wrepl_conn
->conn
->event
.ctx
,
250 wrepl_conn
->send_queue
,
252 if (subreq
== NULL
) {
253 wreplsrv_terminate_in_connection(wrepl_conn
, "wreplsrv_call_loop: "
254 "no memory for tstream_writev_queue_send");
257 tevent_req_set_callback(subreq
, wreplsrv_call_writev_done
, call
);
261 * The wrepl pdu's has the length as 4 byte (initial_read_size),
262 * provides the pdu length then.
264 subreq
= tstream_read_pdu_blob_send(wrepl_conn
,
265 wrepl_conn
->conn
->event
.ctx
,
267 4, /* initial_read_size */
268 packet_full_request_u32
,
270 if (subreq
== NULL
) {
271 wreplsrv_terminate_in_connection(wrepl_conn
, "wreplsrv_call_loop: "
272 "no memory for tstream_read_pdu_blob_send");
275 tevent_req_set_callback(subreq
, wreplsrv_call_loop
, wrepl_conn
);
278 static void wreplsrv_call_writev_done(struct tevent_req
*subreq
)
280 struct wreplsrv_in_call
*call
= tevent_req_callback_data(subreq
,
281 struct wreplsrv_in_call
);
285 rc
= tstream_writev_queue_recv(subreq
, &sys_errno
);
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";
298 wreplsrv_terminate_in_connection(call
->wreplconn
, reason
);
302 if (call
->terminate_after_send
) {
303 wreplsrv_terminate_in_connection(call
->wreplconn
,
304 "wreplsrv_in_connection: terminate_after_send");
314 static void wreplsrv_recv(struct stream_connection
*conn
, uint16_t flags
)
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");
324 called when we can write to a connection
326 static void wreplsrv_send(struct stream_connection
*conn
, uint16_t flags
)
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");
335 static const struct stream_server_ops wreplsrv_stream_ops
= {
337 .accept_connection
= wreplsrv_accept
,
338 .recv_handler
= wreplsrv_recv
,
339 .send_handler
= wreplsrv_send
,
343 called when we get a new connection
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
)
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
;
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");
362 DEBUG(0,("Can't find 'single' process model_ops"));
363 return NT_STATUS_INTERNAL_ERROR
;
366 wrepl_in
= talloc_zero(partner
, struct wreplsrv_in_connection
);
367 NT_STATUS_HAVE_NO_MEMORY(wrepl_in
);
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
;
374 status
= stream_new_connection_merge(service
->task
->event_ctx
,
375 service
->task
->lp_ctx
,
377 &wreplsrv_stream_ops
,
378 service
->task
->msg_ctx
,
381 NT_STATUS_NOT_OK_RETURN(status
);
384 * make the wreplsrv_in_connection structure a child of the
385 * stream_connection, to match the hierarchy of wreplsrv_accept
387 wrepl_in
->conn
= conn
;
388 talloc_steal(conn
, wrepl_in
);
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
;
398 * The wrepl pdu's has the length as 4 byte (initial_read_size),
399 * packet_full_request_u32 provides the pdu length then.
401 subreq
= tstream_read_pdu_blob_send(wrepl_in
,
402 wrepl_in
->conn
->event
.ctx
,
404 4, /* initial_read_size */
405 packet_full_request_u32
,
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
;
412 tevent_req_set_callback(subreq
, wreplsrv_call_loop
, wrepl_in
);
414 *_wrepl_in
= wrepl_in
;
420 startup the wrepl port 42 server sockets
422 NTSTATUS
wreplsrv_setup_sockets(struct wreplsrv_service
*service
, struct loadparm_context
*lp_ctx
)
425 struct task_server
*task
= service
->task
;
426 const struct model_ops
*model_ops
;
428 uint16_t port
= WINS_REPLICATION_PORT
;
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");
435 DEBUG(0,("Can't find 'single' process model_ops"));
436 return NT_STATUS_INTERNAL_ERROR
;
439 if (lpcfg_interfaces(lp_ctx
) && lpcfg_bind_interfaces_only(lp_ctx
)) {
442 struct interface
*ifaces
;
444 load_interface_list(task
, lp_ctx
, &ifaces
);
446 num_interfaces
= iface_list_count(ifaces
);
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.
452 for(i
= 0; i
< num_interfaces
; i
++) {
453 if (!iface_list_n_is_v4(ifaces
, i
)) {
456 address
= iface_list_n_ip(ifaces
, i
);
457 status
= stream_setup_socket(task
, task
->event_ctx
,
458 task
->lp_ctx
, model_ops
,
459 &wreplsrv_stream_ops
,
460 "ipv4", address
, &port
,
461 lpcfg_socket_options(task
->lp_ctx
),
463 if (!NT_STATUS_IS_OK(status
)) {
464 DEBUG(0,("stream_setup_socket(address=%s,port=%u) failed - %s\n",
465 address
, port
, nt_errstr(status
)));
471 status
= stream_setup_socket(task
, task
->event_ctx
, task
->lp_ctx
,
472 model_ops
, &wreplsrv_stream_ops
,
473 "ipv4", address
, &port
, lpcfg_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
)));