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 socket_address
*peer_ip
;
107 struct tevent_req
*subreq
;
110 wrepl_conn
= talloc_zero(conn
, struct wreplsrv_in_connection
);
111 if (wrepl_conn
== NULL
) {
112 stream_terminate_connection(conn
,
113 "wreplsrv_accept: out of memory");
117 wrepl_conn
->send_queue
= tevent_queue_create(conn
, "wrepl_accept");
118 if (wrepl_conn
->send_queue
== NULL
) {
119 stream_terminate_connection(conn
,
120 "wrepl_accept: out of memory");
124 TALLOC_FREE(conn
->event
.fde
);
127 * Clone the fd that the connection isn't closed if we create a client
130 fd
= dup(socket_get_fd(conn
->socket
));
134 reason
= talloc_asprintf(conn
,
135 "wrepl_accept: failed to duplicate the file descriptor - %s",
137 if (reason
== NULL
) {
138 reason
= strerror(errno
);
140 stream_terminate_connection(conn
, reason
);
142 rc
= tstream_bsd_existing_socket(wrepl_conn
,
144 &wrepl_conn
->tstream
);
146 stream_terminate_connection(conn
,
147 "wrepl_accept: out of memory");
151 wrepl_conn
->conn
= conn
;
152 wrepl_conn
->service
= service
;
154 peer_ip
= socket_get_peer_addr(conn
->socket
, wrepl_conn
);
155 if (peer_ip
== NULL
) {
156 wreplsrv_terminate_in_connection(wrepl_conn
, "wreplsrv_accept: "
157 "could not obtain peer IP from kernel");
161 wrepl_conn
->partner
= wreplsrv_find_partner(service
, peer_ip
->addr
);
162 irpc_add_name(conn
->msg_ctx
, "wreplsrv_connection");
165 * The wrepl pdu's has the length as 4 byte (initial_read_size),
166 * packet_full_request_u32 provides the pdu length then.
168 subreq
= tstream_read_pdu_blob_send(wrepl_conn
,
169 wrepl_conn
->conn
->event
.ctx
,
171 4, /* initial_read_size */
172 packet_full_request_u32
,
174 if (subreq
== NULL
) {
175 wreplsrv_terminate_in_connection(wrepl_conn
, "wrepl_accept: "
176 "no memory for tstream_read_pdu_blob_send");
179 tevent_req_set_callback(subreq
, wreplsrv_call_loop
, wrepl_conn
);
182 static void wreplsrv_call_writev_done(struct tevent_req
*subreq
);
184 static void wreplsrv_call_loop(struct tevent_req
*subreq
)
186 struct wreplsrv_in_connection
*wrepl_conn
= tevent_req_callback_data(subreq
,
187 struct wreplsrv_in_connection
);
188 struct wreplsrv_in_call
*call
;
191 call
= talloc_zero(wrepl_conn
, struct wreplsrv_in_call
);
193 wreplsrv_terminate_in_connection(wrepl_conn
, "wreplsrv_call_loop: "
194 "no memory for wrepl_samba3_call");
197 call
->wreplconn
= wrepl_conn
;
199 status
= tstream_read_pdu_blob_recv(subreq
,
203 if (!NT_STATUS_IS_OK(status
)) {
206 reason
= talloc_asprintf(call
, "wreplsrv_call_loop: "
207 "tstream_read_pdu_blob_recv() - %s",
210 reason
= nt_errstr(status
);
213 wreplsrv_terminate_in_connection(wrepl_conn
, reason
);
217 DEBUG(10,("Received wrepl packet of length %lu from %s\n",
218 (long) call
->in
.length
,
219 tsocket_address_string(wrepl_conn
->conn
->remote_address
, call
)));
221 /* skip length header */
223 call
->in
.length
-= 4;
225 status
= wreplsrv_process(wrepl_conn
, &call
);
226 if (!NT_STATUS_IS_OK(status
)) {
229 reason
= talloc_asprintf(call
, "wreplsrv_call_loop: "
230 "tstream_read_pdu_blob_recv() - %s",
232 if (reason
== NULL
) {
233 reason
= nt_errstr(status
);
236 wreplsrv_terminate_in_connection(wrepl_conn
, reason
);
240 /* We handed over the connection so we're done here */
241 if (wrepl_conn
->tstream
== NULL
) {
245 /* Invalid WINS-Replication packet, we just ignore it */
250 call
->out_iov
[0].iov_base
= call
->out
.data
;
251 call
->out_iov
[0].iov_len
= call
->out
.length
;
253 subreq
= tstream_writev_queue_send(call
,
254 wrepl_conn
->conn
->event
.ctx
,
256 wrepl_conn
->send_queue
,
258 if (subreq
== NULL
) {
259 wreplsrv_terminate_in_connection(wrepl_conn
, "wreplsrv_call_loop: "
260 "no memory for tstream_writev_queue_send");
263 tevent_req_set_callback(subreq
, wreplsrv_call_writev_done
, call
);
267 * The wrepl pdu's has the length as 4 byte (initial_read_size),
268 * provides the pdu length then.
270 subreq
= tstream_read_pdu_blob_send(wrepl_conn
,
271 wrepl_conn
->conn
->event
.ctx
,
273 4, /* initial_read_size */
274 packet_full_request_u32
,
276 if (subreq
== NULL
) {
277 wreplsrv_terminate_in_connection(wrepl_conn
, "wreplsrv_call_loop: "
278 "no memory for tstream_read_pdu_blob_send");
281 tevent_req_set_callback(subreq
, wreplsrv_call_loop
, wrepl_conn
);
284 static void wreplsrv_call_writev_done(struct tevent_req
*subreq
)
286 struct wreplsrv_in_call
*call
= tevent_req_callback_data(subreq
,
287 struct wreplsrv_in_call
);
291 rc
= tstream_writev_queue_recv(subreq
, &sys_errno
);
296 reason
= talloc_asprintf(call
, "wreplsrv_call_writev_done: "
297 "tstream_writev_queue_recv() - %d:%s",
298 sys_errno
, strerror(sys_errno
));
299 if (reason
== NULL
) {
300 reason
= "wreplsrv_call_writev_done: "
301 "tstream_writev_queue_recv() failed";
304 wreplsrv_terminate_in_connection(call
->wreplconn
, reason
);
308 if (call
->terminate_after_send
) {
309 wreplsrv_terminate_in_connection(call
->wreplconn
,
310 "wreplsrv_in_connection: terminate_after_send");
320 static void wreplsrv_recv(struct stream_connection
*conn
, uint16_t flags
)
322 struct wreplsrv_in_connection
*wrepl_conn
= talloc_get_type(conn
->private_data
,
323 struct wreplsrv_in_connection
);
324 /* this should never be triggered! */
325 DEBUG(0,("Terminating connection - '%s'\n", "wrepl_recv: called"));
326 wreplsrv_terminate_in_connection(wrepl_conn
, "wrepl_recv: called");
330 called when we can write to a connection
332 static void wreplsrv_send(struct stream_connection
*conn
, uint16_t flags
)
334 struct wreplsrv_in_connection
*wrepl_conn
= talloc_get_type(conn
->private_data
,
335 struct wreplsrv_in_connection
);
336 /* this should never be triggered! */
337 DEBUG(0,("Terminating connection - '%s'\n", "wrepl_send: called"));
338 wreplsrv_terminate_in_connection(wrepl_conn
, "wrepl_send: called");
341 static const struct stream_server_ops wreplsrv_stream_ops
= {
343 .accept_connection
= wreplsrv_accept
,
344 .recv_handler
= wreplsrv_recv
,
345 .send_handler
= wreplsrv_send
,
349 called when we get a new connection
351 NTSTATUS
wreplsrv_in_connection_merge(struct wreplsrv_partner
*partner
,
352 uint32_t peer_assoc_ctx
,
353 struct tstream_context
**stream
,
354 struct wreplsrv_in_connection
**_wrepl_in
)
356 struct wreplsrv_service
*service
= partner
->service
;
357 struct wreplsrv_in_connection
*wrepl_in
;
358 const struct model_ops
*model_ops
;
359 struct stream_connection
*conn
;
360 struct tevent_req
*subreq
;
363 /* within the wrepl task we want to be a single process, so
364 ask for the single process model ops and pass these to the
365 stream_setup_socket() call. */
366 model_ops
= process_model_startup(service
->task
->event_ctx
, "single");
368 DEBUG(0,("Can't find 'single' process model_ops"));
369 return NT_STATUS_INTERNAL_ERROR
;
372 wrepl_in
= talloc_zero(partner
, struct wreplsrv_in_connection
);
373 NT_STATUS_HAVE_NO_MEMORY(wrepl_in
);
375 wrepl_in
->service
= service
;
376 wrepl_in
->partner
= partner
;
377 wrepl_in
->tstream
= talloc_move(wrepl_in
, stream
);
378 wrepl_in
->assoc_ctx
.peer_ctx
= peer_assoc_ctx
;
380 status
= stream_new_connection_merge(service
->task
->event_ctx
,
381 service
->task
->lp_ctx
,
383 &wreplsrv_stream_ops
,
384 service
->task
->msg_ctx
,
387 NT_STATUS_NOT_OK_RETURN(status
);
390 * make the wreplsrv_in_connection structure a child of the
391 * stream_connection, to match the hierarchy of wreplsrv_accept
393 wrepl_in
->conn
= conn
;
394 talloc_steal(conn
, wrepl_in
);
396 wrepl_in
->send_queue
= tevent_queue_create(wrepl_in
, "wreplsrv_in_connection_merge");
397 if (wrepl_in
->send_queue
== NULL
) {
398 stream_terminate_connection(conn
,
399 "wreplsrv_in_connection_merge: out of memory");
400 return NT_STATUS_NO_MEMORY
;
404 * The wrepl pdu's has the length as 4 byte (initial_read_size),
405 * packet_full_request_u32 provides the pdu length then.
407 subreq
= tstream_read_pdu_blob_send(wrepl_in
,
408 wrepl_in
->conn
->event
.ctx
,
410 4, /* initial_read_size */
411 packet_full_request_u32
,
413 if (subreq
== NULL
) {
414 wreplsrv_terminate_in_connection(wrepl_in
, "wreplsrv_in_connection_merge: "
415 "no memory for tstream_read_pdu_blob_send");
416 return NT_STATUS_NO_MEMORY
;
418 tevent_req_set_callback(subreq
, wreplsrv_call_loop
, wrepl_in
);
420 *_wrepl_in
= wrepl_in
;
426 startup the wrepl port 42 server sockets
428 NTSTATUS
wreplsrv_setup_sockets(struct wreplsrv_service
*service
, struct loadparm_context
*lp_ctx
)
431 struct task_server
*task
= service
->task
;
432 const struct model_ops
*model_ops
;
434 uint16_t port
= WINS_REPLICATION_PORT
;
436 /* within the wrepl task we want to be a single process, so
437 ask for the single process model ops and pass these to the
438 stream_setup_socket() call. */
439 model_ops
= process_model_startup(task
->event_ctx
, "single");
441 DEBUG(0,("Can't find 'single' process model_ops"));
442 return NT_STATUS_INTERNAL_ERROR
;
445 if (lp_interfaces(lp_ctx
) && lp_bind_interfaces_only(lp_ctx
)) {
448 struct interface
*ifaces
;
450 load_interfaces(task
, lp_interfaces(lp_ctx
), &ifaces
);
452 num_interfaces
= iface_count(ifaces
);
454 /* We have been given an interfaces line, and been
455 told to only bind to those interfaces. Create a
456 socket per interface and bind to only these.
458 for(i
= 0; i
< num_interfaces
; i
++) {
459 address
= iface_n_ip(ifaces
, i
);
460 status
= stream_setup_socket(task
->event_ctx
,
461 task
->lp_ctx
, model_ops
,
462 &wreplsrv_stream_ops
,
463 "ipv4", address
, &port
,
464 lp_socket_options(task
->lp_ctx
),
466 if (!NT_STATUS_IS_OK(status
)) {
467 DEBUG(0,("stream_setup_socket(address=%s,port=%u) failed - %s\n",
468 address
, port
, nt_errstr(status
)));
473 address
= lp_socket_address(lp_ctx
);
474 status
= stream_setup_socket(task
->event_ctx
, task
->lp_ctx
,
475 model_ops
, &wreplsrv_stream_ops
,
476 "ipv4", address
, &port
, lp_socket_options(task
->lp_ctx
),
478 if (!NT_STATUS_IS_OK(status
)) {
479 DEBUG(0,("stream_setup_socket(address=%s,port=%u) failed - %s\n",
480 address
, port
, nt_errstr(status
)));