r11039: r10352@SERNOX: metze | 2005-09-20 16:50:04 +0200
[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 "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 static 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   startup the wrepl port 42 server sockets
271 */
272 NTSTATUS wreplsrv_setup_sockets(struct wreplsrv_service *service)
273 {
274         NTSTATUS status;
275         struct task_server *task = service->task;
276         const struct model_ops *model_ops;
277         const char *address;
278         uint16_t port = WINS_REPLICATION_PORT;
279
280         /* within the wrepl task we want to be a single process, so
281            ask for the single process model ops and pass these to the
282            stream_setup_socket() call. */
283         model_ops = process_model_byname("single");
284         if (!model_ops) {
285                 DEBUG(0,("Can't find 'single' process model_ops"));
286                 return NT_STATUS_INTERNAL_ERROR;
287         }
288
289         if (lp_interfaces() && lp_bind_interfaces_only()) {
290                 int num_interfaces = iface_count();
291                 int i;
292
293                 /* We have been given an interfaces line, and been 
294                    told to only bind to those interfaces. Create a
295                    socket per interface and bind to only these.
296                 */
297                 for(i = 0; i < num_interfaces; i++) {
298                         address = iface_n_ip(i);
299                         status = stream_setup_socket(task->event_ctx, model_ops, &wreplsrv_stream_ops,
300                                                      "ipv4", address, &port, service);
301                         if (!NT_STATUS_IS_OK(status)) {
302                                 DEBUG(0,("stream_setup_socket(address=%s,port=%u) failed - %s\n",
303                                          address, port, nt_errstr(status)));
304                                 return status;
305                         }
306                 }
307         } else {
308                 address = lp_socket_address();
309                 status = stream_setup_socket(task->event_ctx, model_ops, &wreplsrv_stream_ops,
310                                              "ipv4", address, &port, service);
311                 if (!NT_STATUS_IS_OK(status)) {
312                         DEBUG(0,("stream_setup_socket(address=%s,port=%u) failed - %s\n",
313                                  address, port, nt_errstr(status)));
314                         return status;
315                 }
316         }
317
318         return NT_STATUS_OK;
319 }