r26638: libndr: Require explicitly specifying iconv_convenience for ndr_struct_push_b...
[samba.git] / source4 / 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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
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"
35
36 void wreplsrv_terminate_in_connection(struct wreplsrv_in_connection *wreplconn, const char *reason)
37 {
38         stream_terminate_connection(wreplconn->conn, reason);
39 }
40
41 static int terminate_after_send_destructor(struct wreplsrv_in_connection **tas)
42 {
43         wreplsrv_terminate_in_connection(*tas, "wreplsrv_in_connection: terminate_after_send");
44         return 0;
45 }
46
47 /*
48   receive some data on a WREPL connection
49 */
50 static NTSTATUS wreplsrv_recv_request(void *private, DATA_BLOB blob)
51 {
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;
57         NTSTATUS status;
58         enum ndr_err_code ndr_err;
59
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);
64
65         packet_in_blob.data = blob.data + 4;
66         packet_in_blob.length = blob.length - 4;
67
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);
72         }
73
74         if (DEBUGLVL(10)) {
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);
78         }
79
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"));
85                 talloc_free(call);
86                 return NT_STATUS_OK;
87         }
88
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, 
92                                        lp_iconv_convenience(global_loadparm),
93                                        &packet_out_wrap,
94                                       (ndr_push_flags_fn_t)ndr_push_wrepl_wrap);
95         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
96                 return ndr_map_error2ntstatus(ndr_err);
97         }
98
99         if (DEBUGLVL(10)) {
100                 DEBUG(10,("Sending WINS-Replication packet of length %d\n", (int)packet_out_blob.length));
101                 NDR_PRINT_DEBUG(wrepl_packet, &call->rep_packet);
102         }
103
104         if (call->terminate_after_send) {
105                 struct wreplsrv_in_connection **tas;
106                 tas = talloc(packet_out_blob.data, struct wreplsrv_in_connection *);
107                 NT_STATUS_HAVE_NO_MEMORY(tas);
108                 *tas = wreplconn;
109                 talloc_set_destructor(tas, terminate_after_send_destructor);
110         }
111
112         status = packet_send(wreplconn->packet, packet_out_blob);
113         NT_STATUS_NOT_OK_RETURN(status);
114
115         talloc_free(call);
116         return NT_STATUS_OK;
117 }
118
119 /*
120   called when the socket becomes readable
121 */
122 static void wreplsrv_recv(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
127         packet_recv(wreplconn->packet);
128 }
129
130 /*
131   called when the socket becomes writable
132 */
133 static void wreplsrv_send(struct stream_connection *conn, uint16_t flags)
134 {
135         struct wreplsrv_in_connection *wreplconn = talloc_get_type(conn->private,
136                                                                    struct wreplsrv_in_connection);
137         packet_queue_run(wreplconn->packet);
138 }
139
140 /*
141   handle socket recv errors
142 */
143 static void wreplsrv_recv_error(void *private, NTSTATUS status)
144 {
145         struct wreplsrv_in_connection *wreplconn = talloc_get_type(private,
146                                                                    struct wreplsrv_in_connection);
147         wreplsrv_terminate_in_connection(wreplconn, nt_errstr(status));
148 }
149
150 /*
151   called when we get a new connection
152 */
153 static void wreplsrv_accept(struct stream_connection *conn)
154 {
155         struct wreplsrv_service *service = talloc_get_type(conn->private, struct wreplsrv_service);
156         struct wreplsrv_in_connection *wreplconn;
157         struct socket_address *peer_ip;
158
159         wreplconn = talloc_zero(conn, struct wreplsrv_in_connection);
160         if (!wreplconn) {
161                 stream_terminate_connection(conn, "wreplsrv_accept: out of memory");
162                 return;
163         }
164
165         wreplconn->packet = packet_init(wreplconn);
166         if (!wreplconn->packet) {
167                 wreplsrv_terminate_in_connection(wreplconn, "wreplsrv_accept: out of memory");
168                 return;
169         }
170         packet_set_private(wreplconn->packet, wreplconn);
171         packet_set_socket(wreplconn->packet, conn->socket);
172         packet_set_callback(wreplconn->packet, wreplsrv_recv_request);
173         packet_set_full_request(wreplconn->packet, packet_full_request_u32);
174         packet_set_error_handler(wreplconn->packet, wreplsrv_recv_error);
175         packet_set_event_context(wreplconn->packet, conn->event.ctx);
176         packet_set_fde(wreplconn->packet, conn->event.fde);
177         packet_set_serialise(wreplconn->packet);
178
179         wreplconn->conn         = conn;
180         wreplconn->service      = service;
181
182         peer_ip = socket_get_peer_addr(conn->socket, wreplconn);
183         if (!peer_ip) {
184                 wreplsrv_terminate_in_connection(wreplconn, "wreplsrv_accept: could not obtain peer IP from kernel");
185                 return;
186         }
187
188         wreplconn->partner      = wreplsrv_find_partner(service, peer_ip->addr);
189
190         conn->private = wreplconn;
191
192         irpc_add_name(conn->msg_ctx, "wreplsrv_connection");
193 }
194
195 static const struct stream_server_ops wreplsrv_stream_ops = {
196         .name                   = "wreplsrv",
197         .accept_connection      = wreplsrv_accept,
198         .recv_handler           = wreplsrv_recv,
199         .send_handler           = wreplsrv_send,
200 };
201
202 /*
203   called when we get a new connection
204 */
205 NTSTATUS wreplsrv_in_connection_merge(struct wreplsrv_partner *partner,
206                                       struct socket_context *sock,
207                                       struct packet_context *packet,
208                                       struct wreplsrv_in_connection **_wrepl_in)
209 {
210         struct wreplsrv_service *service = partner->service;
211         struct wreplsrv_in_connection *wrepl_in;
212         const struct model_ops *model_ops;
213         struct stream_connection *conn;
214         NTSTATUS status;
215
216         /* within the wrepl task we want to be a single process, so
217            ask for the single process model ops and pass these to the
218            stream_setup_socket() call. */
219         model_ops = process_model_byname("single");
220         if (!model_ops) {
221                 DEBUG(0,("Can't find 'single' process model_ops"));
222                 return NT_STATUS_INTERNAL_ERROR;
223         }
224
225         wrepl_in = talloc_zero(partner, struct wreplsrv_in_connection);
226         NT_STATUS_HAVE_NO_MEMORY(wrepl_in);
227
228         wrepl_in->service       = service;
229         wrepl_in->partner       = partner;
230
231         status = stream_new_connection_merge(service->task->event_ctx, model_ops,
232                                              sock, &wreplsrv_stream_ops, service->task->msg_ctx,
233                                              wrepl_in, &conn);
234         NT_STATUS_NOT_OK_RETURN(status);
235
236         /*
237          * make the wreplsrv_in_connection structure a child of the 
238          * stream_connection, to match the hierachie of wreplsrv_accept
239          */
240         wrepl_in->conn          = conn;
241         talloc_steal(conn, wrepl_in);
242
243         /*
244          * now update the packet handling callback,...
245          */
246         wrepl_in->packet        = talloc_steal(wrepl_in, packet);
247         packet_set_private(wrepl_in->packet, wrepl_in);
248         packet_set_socket(wrepl_in->packet, conn->socket);
249         packet_set_callback(wrepl_in->packet, wreplsrv_recv_request);
250         packet_set_full_request(wrepl_in->packet, packet_full_request_u32);
251         packet_set_error_handler(wrepl_in->packet, wreplsrv_recv_error);
252         packet_set_event_context(wrepl_in->packet, conn->event.ctx);
253         packet_set_fde(wrepl_in->packet, conn->event.fde);
254         packet_set_serialise(wrepl_in->packet);
255
256         *_wrepl_in = wrepl_in;
257         return NT_STATUS_OK;
258 }
259
260 /*
261   startup the wrepl port 42 server sockets
262 */
263 NTSTATUS wreplsrv_setup_sockets(struct wreplsrv_service *service, struct loadparm_context *lp_ctx)
264 {
265         NTSTATUS status;
266         struct task_server *task = service->task;
267         const struct model_ops *model_ops;
268         const char *address;
269         uint16_t port = WINS_REPLICATION_PORT;
270
271         /* within the wrepl task we want to be a single process, so
272            ask for the single process model ops and pass these to the
273            stream_setup_socket() call. */
274         model_ops = process_model_byname("single");
275         if (!model_ops) {
276                 DEBUG(0,("Can't find 'single' process model_ops"));
277                 return NT_STATUS_INTERNAL_ERROR;
278         }
279
280         if (lp_interfaces(lp_ctx) && lp_bind_interfaces_only(lp_ctx)) {
281                 int num_interfaces;
282                 int i;
283                 struct interface *ifaces;
284
285                 load_interfaces(task, lp_interfaces(lp_ctx), &ifaces);
286
287                 num_interfaces = iface_count(ifaces);
288
289                 /* We have been given an interfaces line, and been 
290                    told to only bind to those interfaces. Create a
291                    socket per interface and bind to only these.
292                 */
293                 for(i = 0; i < num_interfaces; i++) {
294                         address = iface_n_ip(ifaces, i);
295                         status = stream_setup_socket(task->event_ctx, model_ops, &wreplsrv_stream_ops,
296                                                      "ipv4", address, &port, 
297                                                       lp_socket_options(task->lp_ctx), 
298                                                      service);
299                         if (!NT_STATUS_IS_OK(status)) {
300                                 DEBUG(0,("stream_setup_socket(address=%s,port=%u) failed - %s\n",
301                                          address, port, nt_errstr(status)));
302                                 return status;
303                         }
304                 }
305         } else {
306                 address = lp_socket_address(lp_ctx);
307                 status = stream_setup_socket(task->event_ctx, model_ops, &wreplsrv_stream_ops,
308                                              "ipv4", address, &port, lp_socket_options(task->lp_ctx), 
309                                              service);
310                 if (!NT_STATUS_IS_OK(status)) {
311                         DEBUG(0,("stream_setup_socket(address=%s,port=%u) failed - %s\n",
312                                  address, port, nt_errstr(status)));
313                         return status;
314                 }
315         }
316
317         return NT_STATUS_OK;
318 }