r11052: bring samba4 uptodate with the samba4-winsrepl branch,
[kai/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 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 "dlinklist.h"
25 #include "lib/events/events.h"
26 #include "lib/socket/socket.h"
27 #include "smbd/service_task.h"
28 #include "smbd/service_stream.h"
29 #include "lib/messaging/irpc.h"
30 #include "librpc/gen_ndr/ndr_winsrepl.h"
31 #include "wrepl_server/wrepl_server.h"
32 #include "nbt_server/wins/winsdb.h"
33 #include "ldb/include/ldb.h"
34
35 void wreplsrv_terminate_in_connection(struct wreplsrv_in_connection *wreplconn, const char *reason)
36 {
37         stream_terminate_connection(wreplconn->conn, reason);
38 }
39
40 /*
41   called when we get a new connection
42 */
43 static void wreplsrv_accept(struct stream_connection *conn)
44 {
45         struct wreplsrv_service *service = talloc_get_type(conn->private, struct wreplsrv_service);
46         struct wreplsrv_in_connection *wreplconn;
47         const char *peer_ip;
48
49         wreplconn = talloc_zero(conn, struct wreplsrv_in_connection);
50         if (!wreplconn) {
51                 stream_terminate_connection(conn, "wreplsrv_accept: out of memory");
52                 return;
53         }
54
55         wreplconn->conn         = conn;
56         wreplconn->service      = service;
57         wreplconn->our_ip       = socket_get_my_addr(conn->socket, wreplconn);
58         if (!wreplconn->our_ip) {
59                 wreplsrv_terminate_in_connection(wreplconn, "wreplsrv_accept: out of memory");
60                 return;
61         }
62
63         peer_ip = socket_get_peer_addr(conn->socket, wreplconn);
64         if (!peer_ip) {
65                 wreplsrv_terminate_in_connection(wreplconn, "wreplsrv_accept: out of memory");
66                 return;
67         }
68
69         wreplconn->partner      = wreplsrv_find_partner(service, peer_ip);
70
71         conn->private = wreplconn;
72
73         irpc_add_name(conn->msg_ctx, "wreplsrv_connection");
74 }
75
76 /*
77   receive some data on a WREPL connection
78 */
79 static void wreplsrv_recv(struct stream_connection *conn, uint16_t flags)
80 {
81         struct wreplsrv_in_connection *wreplconn = talloc_get_type(conn->private, struct wreplsrv_in_connection);
82         struct wreplsrv_in_call *call;
83         DATA_BLOB packet_in_blob;
84         DATA_BLOB packet_out_blob;
85         struct wrepl_wrap packet_out_wrap;
86         struct data_blob_list_item *rep;
87         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
88         size_t nread;
89
90         /* avoid recursion, because of half async code */
91         if (wreplconn->processing) {
92                 EVENT_FD_NOT_READABLE(conn->event.fde);
93                 return;
94         }
95
96         if (wreplconn->partial.length == 0) {
97                 wreplconn->partial = data_blob_talloc(wreplconn, NULL, 4);
98                 if (wreplconn->partial.data == NULL) {
99                         status = NT_STATUS_NO_MEMORY;
100                         goto failed;
101                 }
102                 wreplconn->partial_read = 0;
103         }
104
105         /* read in the packet length */
106         if (wreplconn->partial_read < 4) {
107                 uint32_t packet_length;
108
109                 status = socket_recv(conn->socket, 
110                                      wreplconn->partial.data + wreplconn->partial_read,
111                                      4 - wreplconn->partial_read,
112                                      &nread, 0);
113                 if (NT_STATUS_IS_ERR(status)) goto failed;
114                 if (!NT_STATUS_IS_OK(status)) return;
115
116                 wreplconn->partial_read += nread;
117                 if (wreplconn->partial_read != 4) return;
118
119                 packet_length = RIVAL(wreplconn->partial.data, 0) + 4;
120
121                 wreplconn->partial.data = talloc_realloc(wreplconn, wreplconn->partial.data, 
122                                                          uint8_t, packet_length);
123                 if (wreplconn->partial.data == NULL) {
124                         status = NT_STATUS_NO_MEMORY;
125                         goto failed;
126                 }
127                 wreplconn->partial.length = packet_length;
128         }
129
130         /* read in the body */
131         status = socket_recv(conn->socket, 
132                              wreplconn->partial.data + wreplconn->partial_read,
133                              wreplconn->partial.length - wreplconn->partial_read,
134                              &nread, 0);
135         if (NT_STATUS_IS_ERR(status)) goto failed;
136         if (!NT_STATUS_IS_OK(status)) return;
137
138         wreplconn->partial_read += nread;
139         if (wreplconn->partial_read != wreplconn->partial.length) return;
140
141         packet_in_blob.data = wreplconn->partial.data + 4;
142         packet_in_blob.length = wreplconn->partial.length - 4;
143
144         call = talloc_zero(wreplconn, struct wreplsrv_in_call);
145         if (!call) {
146                 status = NT_STATUS_NO_MEMORY;
147                 goto failed;
148         }
149         call->wreplconn = wreplconn;
150
151         /* we have a full request - parse it */
152         status = ndr_pull_struct_blob(&packet_in_blob,
153                                       call, &call->req_packet,
154                                       (ndr_pull_flags_fn_t)ndr_pull_wrepl_packet);
155         if (!NT_STATUS_IS_OK(status)) {
156                 DEBUG(2,("Failed to parse incoming WINS-Replication packet - %s\n",
157                          nt_errstr(status)));
158                 DEBUG(10,("packet length %u\n", wreplconn->partial.length));
159                 NDR_PRINT_DEBUG(wrepl_packet, &call->req_packet);
160                 goto failed;
161         }
162
163         /*
164          * we have parsed the request, so we can reset the wreplconn->partial_read,
165          * maybe we could also free wreplconn->partial, but for now we keep it,
166          * and overwrite it the next time
167          */
168         wreplconn->partial_read = 0;
169
170         if (DEBUGLVL(10)) {
171                 DEBUG(10,("Received WINS-Replication packet of length %u\n", wreplconn->partial.length));
172                 NDR_PRINT_DEBUG(wrepl_packet, &call->req_packet);
173         }
174
175         /* actually process the request */
176         wreplconn->processing = True;
177         status = wreplsrv_in_call(call);
178         wreplconn->processing = False;
179         if (NT_STATUS_IS_ERR(status)) goto failed;
180         if (!NT_STATUS_IS_OK(status)) {
181                 /* w2k just ignores invalid packets, so we do */
182                 DEBUG(10,("Received WINS-Replication packet was invalid, we just ignore it\n"));
183                 talloc_free(call);
184                 return;
185         }
186
187         /* and now encode the reply */
188         packet_out_wrap.packet = call->rep_packet;
189         status = ndr_push_struct_blob(&packet_out_blob, call, &packet_out_wrap,
190                                       (ndr_push_flags_fn_t)ndr_push_wrepl_wrap);
191         if (!NT_STATUS_IS_OK(status)) goto failed;
192
193         if (DEBUGLVL(10)) {
194                 DEBUG(10,("Sending WINS-Replication packet of length %d\n", (int)packet_out_blob.length));
195                 NDR_PRINT_DEBUG(wrepl_packet, &call->rep_packet);
196         }
197
198         rep = talloc(wreplconn, struct data_blob_list_item);
199         if (!rep) {
200                 status = NT_STATUS_NO_MEMORY;
201                 goto failed;
202         }
203
204         rep->blob = packet_out_blob;
205         talloc_steal(rep, packet_out_blob.data);
206         /* we don't need the call anymore */
207         talloc_free(call);
208
209         if (!wreplconn->send_queue) {
210                 EVENT_FD_WRITEABLE(conn->event.fde);
211         }
212         DLIST_ADD_END(wreplconn->send_queue, rep, struct data_blob_list_item *);
213
214         if (wreplconn->terminate) {
215                 EVENT_FD_NOT_READABLE(conn->event.fde);
216         } else {
217                 EVENT_FD_READABLE(conn->event.fde);
218         }
219         return;
220
221 failed:
222         wreplsrv_terminate_in_connection(wreplconn, nt_errstr(status));
223 }
224
225 /*
226   called when we can write to a connection
227 */
228 static void wreplsrv_send(struct stream_connection *conn, uint16_t flags)
229 {
230         struct wreplsrv_in_connection *wreplconn = talloc_get_type(conn->private, struct wreplsrv_in_connection);
231         NTSTATUS status;
232
233         while (wreplconn->send_queue) {
234                 struct data_blob_list_item *rep = wreplconn->send_queue;
235                 size_t sendlen;
236
237                 status = socket_send(conn->socket, &rep->blob, &sendlen, 0);
238                 if (NT_STATUS_IS_ERR(status)) goto failed;
239                 if (!NT_STATUS_IS_OK(status)) return;
240
241                 rep->blob.length -= sendlen;
242                 rep->blob.data   += sendlen;
243
244                 if (rep->blob.length == 0) {
245                         DLIST_REMOVE(wreplconn->send_queue, rep);
246                         talloc_free(rep);
247                 }
248         }
249
250         if (wreplconn->terminate) {
251                 wreplsrv_terminate_in_connection(wreplconn, "connection terminated after all pending packets are send");
252                 return;
253         }
254
255         EVENT_FD_NOT_WRITEABLE(conn->event.fde);
256         return;
257
258 failed:
259         wreplsrv_terminate_in_connection(wreplconn, nt_errstr(status));
260 }
261
262 static const struct stream_server_ops wreplsrv_stream_ops = {
263         .name                   = "wreplsrv",
264         .accept_connection      = wreplsrv_accept,
265         .recv_handler           = wreplsrv_recv,
266         .send_handler           = wreplsrv_send,
267 };
268
269 /*
270   called when we get a new connection
271 */
272 NTSTATUS wreplsrv_in_connection_merge(struct wreplsrv_partner *partner,
273                                       struct socket_context *sock,
274                                       struct wreplsrv_in_connection **_wrepl_in)
275 {
276         struct wreplsrv_service *service = partner->service;
277         struct wreplsrv_in_connection *wrepl_in;
278         const struct model_ops *model_ops;
279         struct stream_connection *conn;
280         NTSTATUS status;
281
282         /* within the wrepl task we want to be a single process, so
283            ask for the single process model ops and pass these to the
284            stream_setup_socket() call. */
285         model_ops = process_model_byname("single");
286         if (!model_ops) {
287                 DEBUG(0,("Can't find 'single' process model_ops"));
288                 return NT_STATUS_INTERNAL_ERROR;
289         }
290
291         wrepl_in = talloc_zero(partner, struct wreplsrv_in_connection);
292         NT_STATUS_HAVE_NO_MEMORY(wrepl_in);
293
294         wrepl_in->service       = service;
295         wrepl_in->partner       = partner;
296         wrepl_in->our_ip        = socket_get_my_addr(sock, wrepl_in);
297         NT_STATUS_HAVE_NO_MEMORY(wrepl_in->our_ip);
298
299         status = stream_new_connection_merge(service->task->event_ctx, model_ops,
300                                              sock, &wreplsrv_stream_ops, service->task->msg_ctx,
301                                              wrepl_in, &conn);
302         NT_STATUS_NOT_OK_RETURN(status);
303
304         wrepl_in->conn          = conn;
305         talloc_steal(conn, wrepl_in);
306
307         *_wrepl_in = wrepl_in;
308         return NT_STATUS_OK;
309 }
310
311 /*
312   startup the wrepl port 42 server sockets
313 */
314 NTSTATUS wreplsrv_setup_sockets(struct wreplsrv_service *service)
315 {
316         NTSTATUS status;
317         struct task_server *task = service->task;
318         const struct model_ops *model_ops;
319         const char *address;
320         uint16_t port = WINS_REPLICATION_PORT;
321
322         /* within the wrepl task we want to be a single process, so
323            ask for the single process model ops and pass these to the
324            stream_setup_socket() call. */
325         model_ops = process_model_byname("single");
326         if (!model_ops) {
327                 DEBUG(0,("Can't find 'single' process model_ops"));
328                 return NT_STATUS_INTERNAL_ERROR;
329         }
330
331         if (lp_interfaces() && lp_bind_interfaces_only()) {
332                 int num_interfaces = iface_count();
333                 int i;
334
335                 /* We have been given an interfaces line, and been 
336                    told to only bind to those interfaces. Create a
337                    socket per interface and bind to only these.
338                 */
339                 for(i = 0; i < num_interfaces; i++) {
340                         address = iface_n_ip(i);
341                         status = stream_setup_socket(task->event_ctx, model_ops, &wreplsrv_stream_ops,
342                                                      "ipv4", address, &port, service);
343                         if (!NT_STATUS_IS_OK(status)) {
344                                 DEBUG(0,("stream_setup_socket(address=%s,port=%u) failed - %s\n",
345                                          address, port, nt_errstr(status)));
346                                 return status;
347                         }
348                 }
349         } else {
350                 address = lp_socket_address();
351                 status = stream_setup_socket(task->event_ctx, model_ops, &wreplsrv_stream_ops,
352                                              "ipv4", address, &port, service);
353                 if (!NT_STATUS_IS_OK(status)) {
354                         DEBUG(0,("stream_setup_socket(address=%s,port=%u) failed - %s\n",
355                                  address, port, nt_errstr(status)));
356                         return status;
357                 }
358         }
359
360         return NT_STATUS_OK;
361 }