r12804: This patch reworks the Samba4 sockets layer to use a socket_address
[jelmer/samba4-debian.git] / source / wrepl_server / wrepl_in_connection.c
1 /* 
2    Unix SMB/CIFS implementation.
3    
4    WINS Replication server
5    
6    Copyright (C) Stefan Metzmacher      2005
7    
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 2 of the License, or
11    (at your option) any later version.
12    
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.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "lib/socket/socket.h"
25 #include "lib/stream/packet.h"
26 #include "smbd/service_task.h"
27 #include "smbd/service_stream.h"
28 #include "lib/messaging/irpc.h"
29 #include "librpc/gen_ndr/ndr_winsrepl.h"
30 #include "wrepl_server/wrepl_server.h"
31
32 void wreplsrv_terminate_in_connection(struct wreplsrv_in_connection *wreplconn, const char *reason)
33 {
34         stream_terminate_connection(wreplconn->conn, reason);
35 }
36
37 static int terminate_after_send_destructor(void *ptr)
38 {
39         struct wreplsrv_in_connection **tas = talloc_get_type(ptr, struct wreplsrv_in_connection *);
40         wreplsrv_terminate_in_connection(*tas, "wreplsrv_in_connection: terminate_after_send");
41         return 0;
42 }
43
44 /*
45   receive some data on a WREPL connection
46 */
47 static NTSTATUS wreplsrv_recv_request(void *private, DATA_BLOB blob)
48 {
49         struct wreplsrv_in_connection *wreplconn = talloc_get_type(private, struct wreplsrv_in_connection);
50         struct wreplsrv_in_call *call;
51         DATA_BLOB packet_in_blob;
52         DATA_BLOB packet_out_blob;
53         struct wrepl_wrap packet_out_wrap;
54         NTSTATUS status;
55
56         call = talloc_zero(wreplconn, struct wreplsrv_in_call);
57         NT_STATUS_HAVE_NO_MEMORY(call);
58         call->wreplconn = wreplconn;
59         talloc_steal(call, blob.data);
60
61         packet_in_blob.data = blob.data + 4;
62         packet_in_blob.length = blob.length - 4;
63
64         status = ndr_pull_struct_blob(&packet_in_blob, call, &call->req_packet,
65                                       (ndr_pull_flags_fn_t)ndr_pull_wrepl_packet);
66         NT_STATUS_NOT_OK_RETURN(status);
67
68         if (DEBUGLVL(10)) {
69                 DEBUG(10,("Received WINS-Replication packet of length %u\n", packet_in_blob.length + 4));
70                 NDR_PRINT_DEBUG(wrepl_packet, &call->req_packet);
71         }
72
73         status = wreplsrv_in_call(call);
74         NT_STATUS_IS_ERR_RETURN(status);
75         if (!NT_STATUS_IS_OK(status)) {
76                 /* w2k just ignores invalid packets, so we do */
77                 DEBUG(10,("Received WINS-Replication packet was invalid, we just ignore it\n"));
78                 talloc_free(call);
79                 return NT_STATUS_OK;
80         }
81
82         /* and now encode the reply */
83         packet_out_wrap.packet = call->rep_packet;
84         status = ndr_push_struct_blob(&packet_out_blob, call, &packet_out_wrap,
85                                       (ndr_push_flags_fn_t)ndr_push_wrepl_wrap);
86         NT_STATUS_NOT_OK_RETURN(status);
87
88         if (DEBUGLVL(10)) {
89                 DEBUG(10,("Sending WINS-Replication packet of length %d\n", (int)packet_out_blob.length));
90                 NDR_PRINT_DEBUG(wrepl_packet, &call->rep_packet);
91         }
92
93         if (call->terminate_after_send) {
94                 struct wreplsrv_in_connection **tas;
95                 tas = talloc(packet_out_blob.data, struct wreplsrv_in_connection *);
96                 NT_STATUS_HAVE_NO_MEMORY(tas);
97                 *tas = wreplconn;
98                 talloc_set_destructor(tas, terminate_after_send_destructor);
99         }
100
101         status = packet_send(wreplconn->packet, packet_out_blob);
102         NT_STATUS_NOT_OK_RETURN(status);
103
104         talloc_free(call);
105         return NT_STATUS_OK;
106 }
107
108 /*
109   called when the socket becomes readable
110 */
111 static void wreplsrv_recv(struct stream_connection *conn, uint16_t flags)
112 {
113         struct wreplsrv_in_connection *wreplconn = talloc_get_type(conn->private,
114                                                                    struct wreplsrv_in_connection);
115
116         packet_recv(wreplconn->packet);
117 }
118
119 /*
120   called when the socket becomes writable
121 */
122 static void wreplsrv_send(struct stream_connection *conn, uint16_t flags)
123 {
124         struct wreplsrv_in_connection *wreplconn = talloc_get_type(conn->private,
125                                                                    struct wreplsrv_in_connection);
126         packet_queue_run(wreplconn->packet);
127 }
128
129 /*
130   handle socket recv errors
131 */
132 static void wreplsrv_recv_error(void *private, NTSTATUS status)
133 {
134         struct wreplsrv_in_connection *wreplconn = talloc_get_type(private,
135                                                                    struct wreplsrv_in_connection);
136         wreplsrv_terminate_in_connection(wreplconn, nt_errstr(status));
137 }
138
139 /*
140   called when we get a new connection
141 */
142 static void wreplsrv_accept(struct stream_connection *conn)
143 {
144         struct wreplsrv_service *service = talloc_get_type(conn->private, struct wreplsrv_service);
145         struct wreplsrv_in_connection *wreplconn;
146         struct socket_address *peer_ip;
147
148         wreplconn = talloc_zero(conn, struct wreplsrv_in_connection);
149         if (!wreplconn) {
150                 stream_terminate_connection(conn, "wreplsrv_accept: out of memory");
151                 return;
152         }
153
154         wreplconn->packet = packet_init(wreplconn);
155         if (!wreplconn->packet) {
156                 wreplsrv_terminate_in_connection(wreplconn, "wreplsrv_accept: out of memory");
157                 return;
158         }
159         packet_set_private(wreplconn->packet, wreplconn);
160         packet_set_socket(wreplconn->packet, conn->socket);
161         packet_set_callback(wreplconn->packet, wreplsrv_recv_request);
162         packet_set_full_request(wreplconn->packet, packet_full_request_u32);
163         packet_set_error_handler(wreplconn->packet, wreplsrv_recv_error);
164         packet_set_event_context(wreplconn->packet, conn->event.ctx);
165         packet_set_fde(wreplconn->packet, conn->event.fde);
166         packet_set_serialise(wreplconn->packet);
167
168         wreplconn->conn         = conn;
169         wreplconn->service      = service;
170
171         peer_ip = socket_get_peer_addr(conn->socket, wreplconn);
172         if (!peer_ip) {
173                 wreplsrv_terminate_in_connection(wreplconn, "wreplsrv_accept: could not obtain peer IP from kernel");
174                 return;
175         }
176
177         wreplconn->partner      = wreplsrv_find_partner(service, peer_ip->addr);
178
179         conn->private = wreplconn;
180
181         irpc_add_name(conn->msg_ctx, "wreplsrv_connection");
182 }
183
184 static const struct stream_server_ops wreplsrv_stream_ops = {
185         .name                   = "wreplsrv",
186         .accept_connection      = wreplsrv_accept,
187         .recv_handler           = wreplsrv_recv,
188         .send_handler           = wreplsrv_send,
189 };
190
191 /*
192   called when we get a new connection
193 */
194 NTSTATUS wreplsrv_in_connection_merge(struct wreplsrv_partner *partner,
195                                       struct socket_context *sock,
196                                       struct packet_context *packet,
197                                       struct wreplsrv_in_connection **_wrepl_in)
198 {
199         struct wreplsrv_service *service = partner->service;
200         struct wreplsrv_in_connection *wrepl_in;
201         const struct model_ops *model_ops;
202         struct stream_connection *conn;
203         NTSTATUS status;
204
205         /* within the wrepl task we want to be a single process, so
206            ask for the single process model ops and pass these to the
207            stream_setup_socket() call. */
208         model_ops = process_model_byname("single");
209         if (!model_ops) {
210                 DEBUG(0,("Can't find 'single' process model_ops"));
211                 return NT_STATUS_INTERNAL_ERROR;
212         }
213
214         wrepl_in = talloc_zero(partner, struct wreplsrv_in_connection);
215         NT_STATUS_HAVE_NO_MEMORY(wrepl_in);
216
217         wrepl_in->service       = service;
218         wrepl_in->partner       = partner;
219
220         status = stream_new_connection_merge(service->task->event_ctx, model_ops,
221                                              sock, &wreplsrv_stream_ops, service->task->msg_ctx,
222                                              wrepl_in, &conn);
223         NT_STATUS_NOT_OK_RETURN(status);
224
225         /*
226          * make the wreplsrv_in_connection structure a child of the 
227          * stream_connection, to match the hierachie of wreplsrv_accept
228          */
229         wrepl_in->conn          = conn;
230         talloc_steal(conn, wrepl_in);
231
232         /*
233          * now update the packet handling callback,...
234          */
235         wrepl_in->packet        = talloc_steal(wrepl_in, packet);
236         packet_set_private(wrepl_in->packet, wrepl_in);
237         packet_set_socket(wrepl_in->packet, conn->socket);
238         packet_set_callback(wrepl_in->packet, wreplsrv_recv_request);
239         packet_set_full_request(wrepl_in->packet, packet_full_request_u32);
240         packet_set_error_handler(wrepl_in->packet, wreplsrv_recv_error);
241         packet_set_event_context(wrepl_in->packet, conn->event.ctx);
242         packet_set_fde(wrepl_in->packet, conn->event.fde);
243         packet_set_serialise(wrepl_in->packet);
244
245         *_wrepl_in = wrepl_in;
246         return NT_STATUS_OK;
247 }
248
249 /*
250   startup the wrepl port 42 server sockets
251 */
252 NTSTATUS wreplsrv_setup_sockets(struct wreplsrv_service *service)
253 {
254         NTSTATUS status;
255         struct task_server *task = service->task;
256         const struct model_ops *model_ops;
257         const char *address;
258         uint16_t port = WINS_REPLICATION_PORT;
259
260         /* within the wrepl task we want to be a single process, so
261            ask for the single process model ops and pass these to the
262            stream_setup_socket() call. */
263         model_ops = process_model_byname("single");
264         if (!model_ops) {
265                 DEBUG(0,("Can't find 'single' process model_ops"));
266                 return NT_STATUS_INTERNAL_ERROR;
267         }
268
269         if (lp_interfaces() && lp_bind_interfaces_only()) {
270                 int num_interfaces = iface_count();
271                 int i;
272
273                 /* We have been given an interfaces line, and been 
274                    told to only bind to those interfaces. Create a
275                    socket per interface and bind to only these.
276                 */
277                 for(i = 0; i < num_interfaces; i++) {
278                         address = iface_n_ip(i);
279                         status = stream_setup_socket(task->event_ctx, model_ops, &wreplsrv_stream_ops,
280                                                      "ipv4", address, &port, service);
281                         if (!NT_STATUS_IS_OK(status)) {
282                                 DEBUG(0,("stream_setup_socket(address=%s,port=%u) failed - %s\n",
283                                          address, port, nt_errstr(status)));
284                                 return status;
285                         }
286                 }
287         } else {
288                 address = lp_socket_address();
289                 status = stream_setup_socket(task->event_ctx, model_ops, &wreplsrv_stream_ops,
290                                              "ipv4", address, &port, service);
291                 if (!NT_STATUS_IS_OK(status)) {
292                         DEBUG(0,("stream_setup_socket(address=%s,port=%u) failed - %s\n",
293                                  address, port, nt_errstr(status)));
294                         return status;
295                 }
296         }
297
298         return NT_STATUS_OK;
299 }