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 "param/param.h"
36 void wreplsrv_terminate_in_connection(struct wreplsrv_in_connection *wreplconn, const char *reason)
38 stream_terminate_connection(wreplconn->conn, reason);
41 static int terminate_after_send_destructor(struct wreplsrv_in_connection **tas)
43 wreplsrv_terminate_in_connection(*tas, "wreplsrv_in_connection: terminate_after_send");
48 receive some data on a WREPL connection
50 static NTSTATUS wreplsrv_recv_request(void *private, DATA_BLOB blob)
52 struct wreplsrv_in_connection *wreplconn = talloc_get_type(private, struct wreplsrv_in_connection);
53 struct wreplsrv_in_call *call;
54 DATA_BLOB packet_in_blob;
55 DATA_BLOB packet_out_blob;
56 struct wrepl_wrap packet_out_wrap;
58 enum ndr_err_code ndr_err;
60 call = talloc_zero(wreplconn, struct wreplsrv_in_call);
61 NT_STATUS_HAVE_NO_MEMORY(call);
62 call->wreplconn = wreplconn;
63 talloc_steal(call, blob.data);
65 packet_in_blob.data = blob.data + 4;
66 packet_in_blob.length = blob.length - 4;
68 ndr_err = ndr_pull_struct_blob(&packet_in_blob, call, &call->req_packet,
69 (ndr_pull_flags_fn_t)ndr_pull_wrepl_packet);
70 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
71 return ndr_map_error2ntstatus(ndr_err);
75 DEBUG(10,("Received WINS-Replication packet of length %u\n",
76 (unsigned)packet_in_blob.length + 4));
77 NDR_PRINT_DEBUG(wrepl_packet, &call->req_packet);
80 status = wreplsrv_in_call(call);
81 NT_STATUS_IS_ERR_RETURN(status);
82 if (!NT_STATUS_IS_OK(status)) {
83 /* w2k just ignores invalid packets, so we do */
84 DEBUG(10,("Received WINS-Replication packet was invalid, we just ignore it\n"));
89 /* and now encode the reply */
90 packet_out_wrap.packet = call->rep_packet;
91 ndr_err = ndr_push_struct_blob(&packet_out_blob, call, &packet_out_wrap,
92 (ndr_push_flags_fn_t)ndr_push_wrepl_wrap);
93 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
94 return ndr_map_error2ntstatus(ndr_err);
98 DEBUG(10,("Sending WINS-Replication packet of length %d\n", (int)packet_out_blob.length));
99 NDR_PRINT_DEBUG(wrepl_packet, &call->rep_packet);
102 if (call->terminate_after_send) {
103 struct wreplsrv_in_connection **tas;
104 tas = talloc(packet_out_blob.data, struct wreplsrv_in_connection *);
105 NT_STATUS_HAVE_NO_MEMORY(tas);
107 talloc_set_destructor(tas, terminate_after_send_destructor);
110 status = packet_send(wreplconn->packet, packet_out_blob);
111 NT_STATUS_NOT_OK_RETURN(status);
118 called when the socket becomes readable
120 static void wreplsrv_recv(struct stream_connection *conn, uint16_t flags)
122 struct wreplsrv_in_connection *wreplconn = talloc_get_type(conn->private,
123 struct wreplsrv_in_connection);
125 packet_recv(wreplconn->packet);
129 called when the socket becomes writable
131 static void wreplsrv_send(struct stream_connection *conn, uint16_t flags)
133 struct wreplsrv_in_connection *wreplconn = talloc_get_type(conn->private,
134 struct wreplsrv_in_connection);
135 packet_queue_run(wreplconn->packet);
139 handle socket recv errors
141 static void wreplsrv_recv_error(void *private, NTSTATUS status)
143 struct wreplsrv_in_connection *wreplconn = talloc_get_type(private,
144 struct wreplsrv_in_connection);
145 wreplsrv_terminate_in_connection(wreplconn, nt_errstr(status));
149 called when we get a new connection
151 static void wreplsrv_accept(struct stream_connection *conn)
153 struct wreplsrv_service *service = talloc_get_type(conn->private, struct wreplsrv_service);
154 struct wreplsrv_in_connection *wreplconn;
155 struct socket_address *peer_ip;
157 wreplconn = talloc_zero(conn, struct wreplsrv_in_connection);
159 stream_terminate_connection(conn, "wreplsrv_accept: out of memory");
163 wreplconn->packet = packet_init(wreplconn);
164 if (!wreplconn->packet) {
165 wreplsrv_terminate_in_connection(wreplconn, "wreplsrv_accept: out of memory");
168 packet_set_private(wreplconn->packet, wreplconn);
169 packet_set_socket(wreplconn->packet, conn->socket);
170 packet_set_callback(wreplconn->packet, wreplsrv_recv_request);
171 packet_set_full_request(wreplconn->packet, packet_full_request_u32);
172 packet_set_error_handler(wreplconn->packet, wreplsrv_recv_error);
173 packet_set_event_context(wreplconn->packet, conn->event.ctx);
174 packet_set_fde(wreplconn->packet, conn->event.fde);
175 packet_set_serialise(wreplconn->packet);
177 wreplconn->conn = conn;
178 wreplconn->service = service;
180 peer_ip = socket_get_peer_addr(conn->socket, wreplconn);
182 wreplsrv_terminate_in_connection(wreplconn, "wreplsrv_accept: could not obtain peer IP from kernel");
186 wreplconn->partner = wreplsrv_find_partner(service, peer_ip->addr);
188 conn->private = wreplconn;
190 irpc_add_name(conn->msg_ctx, "wreplsrv_connection");
193 static const struct stream_server_ops wreplsrv_stream_ops = {
195 .accept_connection = wreplsrv_accept,
196 .recv_handler = wreplsrv_recv,
197 .send_handler = wreplsrv_send,
201 called when we get a new connection
203 NTSTATUS wreplsrv_in_connection_merge(struct wreplsrv_partner *partner,
204 struct socket_context *sock,
205 struct packet_context *packet,
206 struct wreplsrv_in_connection **_wrepl_in)
208 struct wreplsrv_service *service = partner->service;
209 struct wreplsrv_in_connection *wrepl_in;
210 const struct model_ops *model_ops;
211 struct stream_connection *conn;
214 /* within the wrepl task we want to be a single process, so
215 ask for the single process model ops and pass these to the
216 stream_setup_socket() call. */
217 model_ops = process_model_byname("single");
219 DEBUG(0,("Can't find 'single' process model_ops"));
220 return NT_STATUS_INTERNAL_ERROR;
223 wrepl_in = talloc_zero(partner, struct wreplsrv_in_connection);
224 NT_STATUS_HAVE_NO_MEMORY(wrepl_in);
226 wrepl_in->service = service;
227 wrepl_in->partner = partner;
229 status = stream_new_connection_merge(service->task->event_ctx, model_ops,
230 sock, &wreplsrv_stream_ops, service->task->msg_ctx,
232 NT_STATUS_NOT_OK_RETURN(status);
235 * make the wreplsrv_in_connection structure a child of the
236 * stream_connection, to match the hierachie of wreplsrv_accept
238 wrepl_in->conn = conn;
239 talloc_steal(conn, wrepl_in);
242 * now update the packet handling callback,...
244 wrepl_in->packet = talloc_steal(wrepl_in, packet);
245 packet_set_private(wrepl_in->packet, wrepl_in);
246 packet_set_socket(wrepl_in->packet, conn->socket);
247 packet_set_callback(wrepl_in->packet, wreplsrv_recv_request);
248 packet_set_full_request(wrepl_in->packet, packet_full_request_u32);
249 packet_set_error_handler(wrepl_in->packet, wreplsrv_recv_error);
250 packet_set_event_context(wrepl_in->packet, conn->event.ctx);
251 packet_set_fde(wrepl_in->packet, conn->event.fde);
252 packet_set_serialise(wrepl_in->packet);
254 *_wrepl_in = wrepl_in;
259 startup the wrepl port 42 server sockets
261 NTSTATUS wreplsrv_setup_sockets(struct wreplsrv_service *service, struct loadparm_context *lp_ctx)
264 struct task_server *task = service->task;
265 const struct model_ops *model_ops;
267 uint16_t port = WINS_REPLICATION_PORT;
269 /* within the wrepl task we want to be a single process, so
270 ask for the single process model ops and pass these to the
271 stream_setup_socket() call. */
272 model_ops = process_model_byname("single");
274 DEBUG(0,("Can't find 'single' process model_ops"));
275 return NT_STATUS_INTERNAL_ERROR;
278 if (lp_interfaces(lp_ctx) && lp_bind_interfaces_only(lp_ctx)) {
281 struct interface *ifaces;
283 load_interfaces(lp_interfaces(lp_ctx), &ifaces);
285 num_interfaces = iface_count(ifaces);
287 /* We have been given an interfaces line, and been
288 told to only bind to those interfaces. Create a
289 socket per interface and bind to only these.
291 for(i = 0; i < num_interfaces; i++) {
292 address = iface_n_ip(ifaces, i);
293 status = stream_setup_socket(task->event_ctx, model_ops, &wreplsrv_stream_ops,
294 "ipv4", address, &port,
295 lp_socket_options(task->lp_ctx),
297 if (!NT_STATUS_IS_OK(status)) {
298 DEBUG(0,("stream_setup_socket(address=%s,port=%u) failed - %s\n",
299 address, port, nt_errstr(status)));
304 address = lp_socket_address(lp_ctx);
305 status = stream_setup_socket(task->event_ctx, model_ops, &wreplsrv_stream_ops,
306 "ipv4", address, &port, lp_socket_options(task->lp_ctx),
308 if (!NT_STATUS_IS_OK(status)) {
309 DEBUG(0,("stream_setup_socket(address=%s,port=%u) failed - %s\n",
310 address, port, nt_errstr(status)));