r12200: - move the the winsreplication client and server code to the packet_context
[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 "lib/stream/packet.h"
28 #include "smbd/service_task.h"
29 #include "smbd/service_stream.h"
30 #include "lib/messaging/irpc.h"
31 #include "librpc/gen_ndr/ndr_winsrepl.h"
32 #include "wrepl_server/wrepl_server.h"
33 #include "nbt_server/wins/winsdb.h"
34 #include "ldb/include/ldb.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(void *ptr)
42 {
43         struct wreplsrv_in_connection **tas = talloc_get_type(ptr, struct wreplsrv_in_connection *);
44         wreplsrv_terminate_in_connection(*tas, "wreplsrv_in_connection: terminate_after_send");
45         return 0;
46 }
47
48 /*
49   receive some data on a WREPL connection
50 */
51 static NTSTATUS wreplsrv_recv_request(void *private, DATA_BLOB blob)
52 {
53         struct wreplsrv_in_connection *wreplconn = talloc_get_type(private, struct wreplsrv_in_connection);
54         struct wreplsrv_in_call *call;
55         DATA_BLOB packet_in_blob;
56         DATA_BLOB packet_out_blob;
57         struct wrepl_wrap packet_out_wrap;
58         NTSTATUS status;
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         status = ndr_pull_struct_blob(&packet_in_blob, call, &call->req_packet,
69                                       (ndr_pull_flags_fn_t)ndr_pull_wrepl_packet);
70         NT_STATUS_NOT_OK_RETURN(status);
71
72         if (DEBUGLVL(10)) {
73                 DEBUG(10,("Received WINS-Replication packet of length %u\n", packet_in_blob.length + 4));
74                 NDR_PRINT_DEBUG(wrepl_packet, &call->req_packet);
75         }
76
77         status = wreplsrv_in_call(call);
78         NT_STATUS_IS_ERR_RETURN(status);
79         if (!NT_STATUS_IS_OK(status)) {
80                 /* w2k just ignores invalid packets, so we do */
81                 DEBUG(10,("Received WINS-Replication packet was invalid, we just ignore it\n"));
82                 talloc_free(call);
83                 return NT_STATUS_OK;
84         }
85
86         /* and now encode the reply */
87         packet_out_wrap.packet = call->rep_packet;
88         status = ndr_push_struct_blob(&packet_out_blob, call, &packet_out_wrap,
89                                       (ndr_push_flags_fn_t)ndr_push_wrepl_wrap);
90         NT_STATUS_NOT_OK_RETURN(status);
91
92         if (DEBUGLVL(10)) {
93                 DEBUG(10,("Sending WINS-Replication packet of length %d\n", (int)packet_out_blob.length));
94                 NDR_PRINT_DEBUG(wrepl_packet, &call->rep_packet);
95         }
96
97         if (call->terminate_after_send) {
98                 struct wreplsrv_in_connection **tas;
99                 tas = talloc(packet_out_blob.data, struct wreplsrv_in_connection *);
100                 NT_STATUS_HAVE_NO_MEMORY(tas);
101                 *tas = wreplconn;
102                 talloc_set_destructor(tas, terminate_after_send_destructor);
103         }
104
105         status = packet_send(wreplconn->packet, packet_out_blob);
106         NT_STATUS_NOT_OK_RETURN(status);
107
108         talloc_free(call);
109         return NT_STATUS_OK;
110 }
111
112 /*
113   called when the socket becomes readable
114 */
115 static void wreplsrv_recv(struct stream_connection *conn, uint16_t flags)
116 {
117         struct wreplsrv_in_connection *wreplconn = talloc_get_type(conn->private,
118                                                                    struct wreplsrv_in_connection);
119
120         packet_recv(wreplconn->packet);
121 }
122
123 /*
124   called when the socket becomes writable
125 */
126 static void wreplsrv_send(struct stream_connection *conn, uint16_t flags)
127 {
128         struct wreplsrv_in_connection *wreplconn = talloc_get_type(conn->private,
129                                                                    struct wreplsrv_in_connection);
130         packet_queue_run(wreplconn->packet);
131 }
132
133 /*
134   handle socket recv errors
135 */
136 static void wreplsrv_recv_error(void *private, NTSTATUS status)
137 {
138         struct wreplsrv_in_connection *wreplconn = talloc_get_type(private,
139                                                                    struct wreplsrv_in_connection);
140         wreplsrv_terminate_in_connection(wreplconn, nt_errstr(status));
141 }
142
143 /*
144   called when we get a new connection
145 */
146 static void wreplsrv_accept(struct stream_connection *conn)
147 {
148         struct wreplsrv_service *service = talloc_get_type(conn->private, struct wreplsrv_service);
149         struct wreplsrv_in_connection *wreplconn;
150         const char *peer_ip;
151
152         wreplconn = talloc_zero(conn, struct wreplsrv_in_connection);
153         if (!wreplconn) {
154                 stream_terminate_connection(conn, "wreplsrv_accept: out of memory");
155                 return;
156         }
157
158         wreplconn->packet = packet_init(wreplconn);
159         if (!wreplconn->packet) {
160                 wreplsrv_terminate_in_connection(wreplconn, "wreplsrv_accept: out of memory");
161                 return;
162         }
163         packet_set_private(wreplconn->packet, wreplconn);
164         packet_set_socket(wreplconn->packet, conn->socket);
165         packet_set_callback(wreplconn->packet, wreplsrv_recv_request);
166         packet_set_full_request(wreplconn->packet, packet_full_request_u32);
167         packet_set_error_handler(wreplconn->packet, wreplsrv_recv_error);
168         packet_set_event_context(wreplconn->packet, conn->event.ctx);
169         packet_set_fde(wreplconn->packet, conn->event.fde);
170         packet_set_serialise(wreplconn->packet);
171
172         wreplconn->conn         = conn;
173         wreplconn->service      = service;
174         wreplconn->our_ip       = socket_get_my_addr(conn->socket, wreplconn);
175         if (!wreplconn->our_ip) {
176                 wreplsrv_terminate_in_connection(wreplconn, "wreplsrv_accept: out of memory");
177                 return;
178         }
179
180         peer_ip = socket_get_peer_addr(conn->socket, wreplconn);
181         if (!peer_ip) {
182                 wreplsrv_terminate_in_connection(wreplconn, "wreplsrv_accept: out of memory");
183                 return;
184         }
185
186         wreplconn->partner      = wreplsrv_find_partner(service, peer_ip);
187
188         conn->private = wreplconn;
189
190         irpc_add_name(conn->msg_ctx, "wreplsrv_connection");
191 }
192
193 static const struct stream_server_ops wreplsrv_stream_ops = {
194         .name                   = "wreplsrv",
195         .accept_connection      = wreplsrv_accept,
196         .recv_handler           = wreplsrv_recv,
197         .send_handler           = wreplsrv_send,
198 };
199
200 /*
201   called when we get a new connection
202 */
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)
207 {
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;
212         NTSTATUS status;
213
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");
218         if (!model_ops) {
219                 DEBUG(0,("Can't find 'single' process model_ops"));
220                 return NT_STATUS_INTERNAL_ERROR;
221         }
222
223         wrepl_in = talloc_zero(partner, struct wreplsrv_in_connection);
224         NT_STATUS_HAVE_NO_MEMORY(wrepl_in);
225
226         wrepl_in->service       = service;
227         wrepl_in->partner       = partner;
228         wrepl_in->our_ip        = socket_get_my_addr(sock, wrepl_in);
229         NT_STATUS_HAVE_NO_MEMORY(wrepl_in->our_ip);
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)
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_bind_interfaces_only()) {
281                 int num_interfaces = iface_count();
282                 int i;
283
284                 /* We have been given an interfaces line, and been 
285                    told to only bind to those interfaces. Create a
286                    socket per interface and bind to only these.
287                 */
288                 for(i = 0; i < num_interfaces; i++) {
289                         address = iface_n_ip(i);
290                         status = stream_setup_socket(task->event_ctx, model_ops, &wreplsrv_stream_ops,
291                                                      "ipv4", address, &port, service);
292                         if (!NT_STATUS_IS_OK(status)) {
293                                 DEBUG(0,("stream_setup_socket(address=%s,port=%u) failed - %s\n",
294                                          address, port, nt_errstr(status)));
295                                 return status;
296                         }
297                 }
298         } else {
299                 address = lp_socket_address();
300                 status = stream_setup_socket(task->event_ctx, model_ops, &wreplsrv_stream_ops,
301                                              "ipv4", address, &port, service);
302                 if (!NT_STATUS_IS_OK(status)) {
303                         DEBUG(0,("stream_setup_socket(address=%s,port=%u) failed - %s\n",
304                                  address, port, nt_errstr(status)));
305                         return status;
306                 }
307         }
308
309         return NT_STATUS_OK;
310 }