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
,
55 lp_iconv_convenience(wrepl_conn
->service
->task
->lp_ctx
),
57 (ndr_pull_flags_fn_t
)ndr_pull_wrepl_packet
);
58 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
59 return ndr_map_error2ntstatus(ndr_err
);
63 DEBUG(10,("Received WINS-Replication packet of length %u\n",
64 (unsigned int) call
->in
.length
+ 4));
65 NDR_PRINT_DEBUG(wrepl_packet
, &call
->req_packet
);
68 status
= wreplsrv_in_call(call
);
69 NT_STATUS_IS_ERR_RETURN(status
);
70 if (!NT_STATUS_IS_OK(status
)) {
71 /* w2k just ignores invalid packets, so we do */
72 DEBUG(10,("Received WINS-Replication packet was invalid, we just ignore it\n"));
78 /* and now encode the reply */
79 packet_out_wrap
.packet
= call
->rep_packet
;
80 ndr_err
= ndr_push_struct_blob(&call
->out
, call
,
81 lp_iconv_convenience(wrepl_conn
->service
->task
->lp_ctx
),
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
);
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
);
97 static void wreplsrv_call_loop(struct tevent_req
*subreq
);
100 called when we get a new connection
102 static void wreplsrv_accept(struct stream_connection
*conn
)
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
;
108 struct tevent_req
*subreq
;
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");
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");
125 TALLOC_FREE(conn
->event
.fde
);
128 * Clone the fd that the connection isn't closed if we create a client
131 fd
= dup(socket_get_fd(conn
->socket
));
135 reason
= talloc_asprintf(conn
,
136 "wrepl_accept: failed to duplicate the file descriptor - %s",
138 if (reason
== NULL
) {
139 reason
= strerror(errno
);
141 stream_terminate_connection(conn
, reason
);
143 rc
= tstream_bsd_existing_socket(wrepl_conn
,
145 &wrepl_conn
->tstream
);
147 stream_terminate_connection(conn
,
148 "wrepl_accept: out of memory");
152 wrepl_conn
->conn
= conn
;
153 wrepl_conn
->service
= service
;
155 peer_addr
= conn
->remote_address
;
157 if (!tsocket_address_is_inet(peer_addr
, "ipv4")) {
158 DEBUG(0,("wreplsrv_accept: non ipv4 peer addr '%s'\n",
159 tsocket_address_string(peer_addr
, wrepl_conn
)));
160 wreplsrv_terminate_in_connection(wrepl_conn
, "wreplsrv_accept: "
165 peer_ip
= tsocket_address_inet_addr_string(peer_addr
, wrepl_conn
);
166 if (peer_ip
== NULL
) {
167 wreplsrv_terminate_in_connection(wrepl_conn
, "wreplsrv_accept: "
168 "could not convert peer IP into a string");
172 wrepl_conn
->partner
= wreplsrv_find_partner(service
, peer_ip
);
173 irpc_add_name(conn
->msg_ctx
, "wreplsrv_connection");
176 * The wrepl pdu's has the length as 4 byte (initial_read_size),
177 * packet_full_request_u32 provides the pdu length then.
179 subreq
= tstream_read_pdu_blob_send(wrepl_conn
,
180 wrepl_conn
->conn
->event
.ctx
,
182 4, /* initial_read_size */
183 packet_full_request_u32
,
185 if (subreq
== NULL
) {
186 wreplsrv_terminate_in_connection(wrepl_conn
, "wrepl_accept: "
187 "no memory for tstream_read_pdu_blob_send");
190 tevent_req_set_callback(subreq
, wreplsrv_call_loop
, wrepl_conn
);
193 static void wreplsrv_call_writev_done(struct tevent_req
*subreq
);
195 static void wreplsrv_call_loop(struct tevent_req
*subreq
)
197 struct wreplsrv_in_connection
*wrepl_conn
= tevent_req_callback_data(subreq
,
198 struct wreplsrv_in_connection
);
199 struct wreplsrv_in_call
*call
;
202 call
= talloc_zero(wrepl_conn
, struct wreplsrv_in_call
);
204 wreplsrv_terminate_in_connection(wrepl_conn
, "wreplsrv_call_loop: "
205 "no memory for wrepl_samba3_call");
208 call
->wreplconn
= wrepl_conn
;
210 status
= tstream_read_pdu_blob_recv(subreq
,
214 if (!NT_STATUS_IS_OK(status
)) {
217 reason
= talloc_asprintf(call
, "wreplsrv_call_loop: "
218 "tstream_read_pdu_blob_recv() - %s",
221 reason
= nt_errstr(status
);
224 wreplsrv_terminate_in_connection(wrepl_conn
, reason
);
228 DEBUG(10,("Received wrepl packet of length %lu from %s\n",
229 (long) call
->in
.length
,
230 tsocket_address_string(wrepl_conn
->conn
->remote_address
, call
)));
232 /* skip length header */
234 call
->in
.length
-= 4;
236 status
= wreplsrv_process(wrepl_conn
, &call
);
237 if (!NT_STATUS_IS_OK(status
)) {
240 reason
= talloc_asprintf(call
, "wreplsrv_call_loop: "
241 "tstream_read_pdu_blob_recv() - %s",
243 if (reason
== NULL
) {
244 reason
= nt_errstr(status
);
247 wreplsrv_terminate_in_connection(wrepl_conn
, reason
);
251 /* We handed over the connection so we're done here */
252 if (wrepl_conn
->tstream
== NULL
) {
256 /* Invalid WINS-Replication packet, we just ignore it */
261 call
->out_iov
[0].iov_base
= call
->out
.data
;
262 call
->out_iov
[0].iov_len
= call
->out
.length
;
264 subreq
= tstream_writev_queue_send(call
,
265 wrepl_conn
->conn
->event
.ctx
,
267 wrepl_conn
->send_queue
,
269 if (subreq
== NULL
) {
270 wreplsrv_terminate_in_connection(wrepl_conn
, "wreplsrv_call_loop: "
271 "no memory for tstream_writev_queue_send");
274 tevent_req_set_callback(subreq
, wreplsrv_call_writev_done
, call
);
278 * The wrepl pdu's has the length as 4 byte (initial_read_size),
279 * provides the pdu length then.
281 subreq
= tstream_read_pdu_blob_send(wrepl_conn
,
282 wrepl_conn
->conn
->event
.ctx
,
284 4, /* initial_read_size */
285 packet_full_request_u32
,
287 if (subreq
== NULL
) {
288 wreplsrv_terminate_in_connection(wrepl_conn
, "wreplsrv_call_loop: "
289 "no memory for tstream_read_pdu_blob_send");
292 tevent_req_set_callback(subreq
, wreplsrv_call_loop
, wrepl_conn
);
295 static void wreplsrv_call_writev_done(struct tevent_req
*subreq
)
297 struct wreplsrv_in_call
*call
= tevent_req_callback_data(subreq
,
298 struct wreplsrv_in_call
);
302 rc
= tstream_writev_queue_recv(subreq
, &sys_errno
);
307 reason
= talloc_asprintf(call
, "wreplsrv_call_writev_done: "
308 "tstream_writev_queue_recv() - %d:%s",
309 sys_errno
, strerror(sys_errno
));
310 if (reason
== NULL
) {
311 reason
= "wreplsrv_call_writev_done: "
312 "tstream_writev_queue_recv() failed";
315 wreplsrv_terminate_in_connection(call
->wreplconn
, reason
);
319 if (call
->terminate_after_send
) {
320 wreplsrv_terminate_in_connection(call
->wreplconn
,
321 "wreplsrv_in_connection: terminate_after_send");
331 static void wreplsrv_recv(struct stream_connection
*conn
, uint16_t flags
)
333 struct wreplsrv_in_connection
*wrepl_conn
= talloc_get_type(conn
->private_data
,
334 struct wreplsrv_in_connection
);
335 /* this should never be triggered! */
336 DEBUG(0,("Terminating connection - '%s'\n", "wrepl_recv: called"));
337 wreplsrv_terminate_in_connection(wrepl_conn
, "wrepl_recv: called");
341 called when we can write to a connection
343 static void wreplsrv_send(struct stream_connection
*conn
, uint16_t flags
)
345 struct wreplsrv_in_connection
*wrepl_conn
= talloc_get_type(conn
->private_data
,
346 struct wreplsrv_in_connection
);
347 /* this should never be triggered! */
348 DEBUG(0,("Terminating connection - '%s'\n", "wrepl_send: called"));
349 wreplsrv_terminate_in_connection(wrepl_conn
, "wrepl_send: called");
352 static const struct stream_server_ops wreplsrv_stream_ops
= {
354 .accept_connection
= wreplsrv_accept
,
355 .recv_handler
= wreplsrv_recv
,
356 .send_handler
= wreplsrv_send
,
360 called when we get a new connection
362 NTSTATUS
wreplsrv_in_connection_merge(struct wreplsrv_partner
*partner
,
363 uint32_t peer_assoc_ctx
,
364 struct tstream_context
**stream
,
365 struct wreplsrv_in_connection
**_wrepl_in
)
367 struct wreplsrv_service
*service
= partner
->service
;
368 struct wreplsrv_in_connection
*wrepl_in
;
369 const struct model_ops
*model_ops
;
370 struct stream_connection
*conn
;
371 struct tevent_req
*subreq
;
374 /* within the wrepl task we want to be a single process, so
375 ask for the single process model ops and pass these to the
376 stream_setup_socket() call. */
377 model_ops
= process_model_startup(service
->task
->event_ctx
, "single");
379 DEBUG(0,("Can't find 'single' process model_ops"));
380 return NT_STATUS_INTERNAL_ERROR
;
383 wrepl_in
= talloc_zero(partner
, struct wreplsrv_in_connection
);
384 NT_STATUS_HAVE_NO_MEMORY(wrepl_in
);
386 wrepl_in
->service
= service
;
387 wrepl_in
->partner
= partner
;
388 wrepl_in
->tstream
= talloc_move(wrepl_in
, stream
);
389 wrepl_in
->assoc_ctx
.peer_ctx
= peer_assoc_ctx
;
391 status
= stream_new_connection_merge(service
->task
->event_ctx
,
392 service
->task
->lp_ctx
,
394 &wreplsrv_stream_ops
,
395 service
->task
->msg_ctx
,
398 NT_STATUS_NOT_OK_RETURN(status
);
401 * make the wreplsrv_in_connection structure a child of the
402 * stream_connection, to match the hierarchy of wreplsrv_accept
404 wrepl_in
->conn
= conn
;
405 talloc_steal(conn
, wrepl_in
);
407 wrepl_in
->send_queue
= tevent_queue_create(wrepl_in
, "wreplsrv_in_connection_merge");
408 if (wrepl_in
->send_queue
== NULL
) {
409 stream_terminate_connection(conn
,
410 "wreplsrv_in_connection_merge: out of memory");
411 return NT_STATUS_NO_MEMORY
;
415 * The wrepl pdu's has the length as 4 byte (initial_read_size),
416 * packet_full_request_u32 provides the pdu length then.
418 subreq
= tstream_read_pdu_blob_send(wrepl_in
,
419 wrepl_in
->conn
->event
.ctx
,
421 4, /* initial_read_size */
422 packet_full_request_u32
,
424 if (subreq
== NULL
) {
425 wreplsrv_terminate_in_connection(wrepl_in
, "wreplsrv_in_connection_merge: "
426 "no memory for tstream_read_pdu_blob_send");
427 return NT_STATUS_NO_MEMORY
;
429 tevent_req_set_callback(subreq
, wreplsrv_call_loop
, wrepl_in
);
431 *_wrepl_in
= wrepl_in
;
437 startup the wrepl port 42 server sockets
439 NTSTATUS
wreplsrv_setup_sockets(struct wreplsrv_service
*service
, struct loadparm_context
*lp_ctx
)
442 struct task_server
*task
= service
->task
;
443 const struct model_ops
*model_ops
;
445 uint16_t port
= WINS_REPLICATION_PORT
;
447 /* within the wrepl task we want to be a single process, so
448 ask for the single process model ops and pass these to the
449 stream_setup_socket() call. */
450 model_ops
= process_model_startup(task
->event_ctx
, "single");
452 DEBUG(0,("Can't find 'single' process model_ops"));
453 return NT_STATUS_INTERNAL_ERROR
;
456 if (lp_interfaces(lp_ctx
) && lp_bind_interfaces_only(lp_ctx
)) {
459 struct interface
*ifaces
;
461 load_interfaces(task
, lp_interfaces(lp_ctx
), &ifaces
);
463 num_interfaces
= iface_count(ifaces
);
465 /* We have been given an interfaces line, and been
466 told to only bind to those interfaces. Create a
467 socket per interface and bind to only these.
469 for(i
= 0; i
< num_interfaces
; i
++) {
470 address
= iface_n_ip(ifaces
, i
);
471 status
= stream_setup_socket(task
->event_ctx
,
472 task
->lp_ctx
, model_ops
,
473 &wreplsrv_stream_ops
,
474 "ipv4", address
, &port
,
475 lp_socket_options(task
->lp_ctx
),
477 if (!NT_STATUS_IS_OK(status
)) {
478 DEBUG(0,("stream_setup_socket(address=%s,port=%u) failed - %s\n",
479 address
, port
, nt_errstr(status
)));
484 address
= lp_socket_address(lp_ctx
);
485 status
= stream_setup_socket(task
->event_ctx
, task
->lp_ctx
,
486 model_ops
, &wreplsrv_stream_ops
,
487 "ipv4", address
, &port
, lp_socket_options(task
->lp_ctx
),
489 if (!NT_STATUS_IS_OK(status
)) {
490 DEBUG(0,("stream_setup_socket(address=%s,port=%u) failed - %s\n",
491 address
, port
, nt_errstr(status
)));