r12606: - fix multihomed registrations
[samba.git] / source4 / nbt_server / wins / winswack.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    "secure" wins server WACK processing
5
6    Copyright (C) Andrew Tridgell        2005
7    Copyright (C) Stefan Metzmacher      2005
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "nbt_server/nbt_server.h"
26 #include "nbt_server/wins/winsdb.h"
27 #include "nbt_server/wins/winswack.h"
28 #include "system/time.h"
29 #include "libcli/composite/composite.h"
30
31 struct wins_challenge_state {
32         struct wins_challenge_io *io;
33         uint32_t current_address;
34         struct nbt_name_query query;
35 };
36
37 static void wins_challenge_handler(struct nbt_name_request *req)
38 {
39         struct composite_context *ctx = talloc_get_type(req->async.private, struct composite_context);
40         struct wins_challenge_state *state = talloc_get_type(ctx->private_data, struct wins_challenge_state);
41
42         ctx->status = nbt_name_query_recv(req, state, &state->query);
43
44         /* if we timed out then try the next owner address, if any */
45         if (NT_STATUS_EQUAL(ctx->status, NT_STATUS_IO_TIMEOUT)) {
46                 state->current_address++;
47                 if (state->current_address < state->io->in.num_addresses) {
48                         struct nbtd_interface *iface;
49
50                         state->query.in.dest_addr = state->io->in.addresses[state->current_address];
51                         
52                         iface = nbtd_find_interface(state->io->in.nbtd_server, state->query.in.dest_addr);
53                         if (!iface) {
54                                 composite_error(ctx, NT_STATUS_INTERNAL_ERROR);
55                                 return;
56                         }
57
58                         ZERO_STRUCT(state->query.out);
59                         req = nbt_name_query_send(iface->nbtsock, &state->query);
60                         composite_continue_nbt(ctx, req, wins_challenge_handler, ctx);
61                         return;
62                 }
63         }
64
65         composite_done(ctx);
66 }
67
68 NTSTATUS wins_challenge_recv(struct composite_context *ctx, TALLOC_CTX *mem_ctx, struct wins_challenge_io *io)
69 {
70         NTSTATUS status = ctx->status;
71         struct wins_challenge_state *state = talloc_get_type(ctx->private_data, struct wins_challenge_state);
72
73         if (NT_STATUS_IS_OK(status)) {
74                 io->out.num_addresses   = state->query.out.num_addrs;
75                 io->out.addresses       = state->query.out.reply_addrs;
76                 talloc_steal(mem_ctx, io->out.addresses);
77         } else {
78                 ZERO_STRUCT(io->out);
79         }
80
81         talloc_free(ctx);
82         return status;
83 }
84
85 struct composite_context *wins_challenge_send(TALLOC_CTX *mem_ctx, struct wins_challenge_io *io)
86 {
87         struct composite_context *result;
88         struct wins_challenge_state *state;
89         struct nbt_name_request *req;
90         struct nbtd_interface *iface;
91
92         result = talloc_zero(mem_ctx, struct composite_context);
93         if (result == NULL) return NULL;
94         result->state = COMPOSITE_STATE_IN_PROGRESS;
95         result->event_ctx = talloc_reference(result, io->in.event_ctx);
96
97         state = talloc_zero(result, struct wins_challenge_state);
98         if (state == NULL) goto failed;
99         result->private_data = state;
100
101         /* package up the state variables for this wack request */
102         state->io               = io;
103         state->current_address  = 0;
104
105         /* setup a name query to the first address */
106         state->query.in.name        = *state->io->in.name;
107         state->query.in.dest_addr   = state->io->in.addresses[state->current_address];
108         state->query.in.broadcast   = False;
109         state->query.in.wins_lookup = True;
110         state->query.in.timeout     = 1;
111         state->query.in.retries     = 2;
112         ZERO_STRUCT(state->query.out);
113
114         iface = nbtd_find_interface(state->io->in.nbtd_server, state->query.in.dest_addr);
115         if (!iface) {
116                 goto failed;
117         }
118
119         req = nbt_name_query_send(iface->nbtsock, &state->query);
120         if (req == NULL) goto failed;
121
122         req->async.fn = wins_challenge_handler;
123         req->async.private = result;
124
125         return result;
126 failed:
127         talloc_free(result);
128         return NULL;
129 }
130
131 struct wins_release_demand_io {
132         struct {
133                 struct nbtd_server *nbtd_server;
134                 struct event_context *event_ctx;
135                 struct nbt_name *name;
136                 uint16_t nb_flags;
137                 uint32_t num_addresses;
138                 const char **addresses;
139         } in;
140 };
141
142 struct wins_release_demand_state {
143         struct wins_release_demand_io *io;
144         uint32_t current_address;
145         uint32_t addresses_left;
146         struct nbt_name_release release;
147 };
148
149 static void wins_release_demand_handler(struct nbt_name_request *req)
150 {
151         struct composite_context *ctx = talloc_get_type(req->async.private, struct composite_context);
152         struct wins_release_demand_state *state = talloc_get_type(ctx->private_data, struct wins_release_demand_state);
153
154         ctx->status = nbt_name_release_recv(req, state, &state->release);
155
156         /* if we timed out then try the next owner address, if any */
157         if (NT_STATUS_EQUAL(ctx->status, NT_STATUS_IO_TIMEOUT)) {
158                 state->current_address++;
159                 state->addresses_left--;
160                 if (state->current_address < state->io->in.num_addresses) {
161                         struct nbtd_interface *iface;
162
163                         state->release.in.dest_addr = state->io->in.addresses[state->current_address];
164                         state->release.in.address   = state->release.in.dest_addr;
165                         state->release.in.timeout   = (state->addresses_left > 1 ? 2 : 1);
166                         state->release.in.retries   = (state->addresses_left > 1 ? 0 : 2);
167
168                         iface = nbtd_find_interface(state->io->in.nbtd_server, state->release.in.dest_addr);
169                         if (!iface) {
170                                 composite_error(ctx, NT_STATUS_INTERNAL_ERROR);
171                                 return;
172                         }
173
174                         ZERO_STRUCT(state->release.out);
175                         req = nbt_name_release_send(iface->nbtsock, &state->release);
176                         composite_continue_nbt(ctx, req, wins_release_demand_handler, ctx);
177                         return;
178                 }
179         }
180
181         composite_done(ctx);
182 }
183
184 static NTSTATUS wins_release_demand_recv(struct composite_context *ctx,
185                                          TALLOC_CTX *mem_ctx,
186                                          struct wins_release_demand_io *io)
187 {
188         NTSTATUS status = ctx->status;
189         talloc_free(ctx);
190         return status;
191 }
192
193 static struct composite_context *wins_release_demand_send(TALLOC_CTX *mem_ctx, struct wins_release_demand_io *io)
194 {
195         struct composite_context *result;
196         struct wins_release_demand_state *state;
197         struct nbt_name_request *req;
198         struct nbtd_interface *iface;
199
200         result = talloc_zero(mem_ctx, struct composite_context);
201         if (result == NULL) return NULL;
202         result->state = COMPOSITE_STATE_IN_PROGRESS;
203         result->event_ctx = talloc_reference(result, io->in.event_ctx);
204
205         state = talloc_zero(result, struct wins_release_demand_state);
206         if (state == NULL) goto failed;
207         result->private_data = state;
208
209         /* package up the state variables for this wack request */
210         state->io               = io;
211         state->current_address  = 0;
212         state->addresses_left   = state->io->in.num_addresses;
213
214         /* 
215          * setup a name query to the first address
216          * - if we have more than one address try the first
217          *   with 2 secs timeout and no retry
218          * - otherwise use 1 sec timeout (w2k3 uses 0.5 sec here)
219          *   with 2 retries
220          */
221         state->release.in.name        = *state->io->in.name;
222         state->release.in.dest_addr   = state->io->in.addresses[state->current_address];
223         state->release.in.address     = state->release.in.dest_addr;
224         state->release.in.broadcast   = False;
225         state->release.in.timeout     = (state->addresses_left > 1 ? 2 : 1);
226         state->release.in.retries     = (state->addresses_left > 1 ? 0 : 2);
227         ZERO_STRUCT(state->release.out);
228
229         iface = nbtd_find_interface(state->io->in.nbtd_server, state->release.in.dest_addr);
230         if (!iface) {
231                 goto failed;
232         }
233
234         req = nbt_name_release_send(iface->nbtsock, &state->release);
235         if (req == NULL) goto failed;
236
237         req->async.fn = wins_release_demand_handler;
238         req->async.private = result;
239
240         return result;
241 failed:
242         talloc_free(result);
243         return NULL;
244 }
245
246 /*
247   wrepl_server needs to be able to do a name query request, but some windows
248   servers always send the reply to port 137, regardless of the request
249   port. To cope with this we use a irpc request to the NBT server
250   which has port 137 open, and thus can receive the replies
251 */
252 struct proxy_wins_challenge_state {
253         struct irpc_message *msg;
254         struct nbtd_proxy_wins_challenge *req;
255         struct wins_challenge_io io;
256         struct composite_context *c_req;
257 };
258
259 static void proxy_wins_challenge_handler(struct composite_context *c_req)
260 {
261         NTSTATUS status;
262         uint32_t i;
263         struct proxy_wins_challenge_state *s = talloc_get_type(c_req->async.private_data,
264                                                                struct proxy_wins_challenge_state);
265
266         status = wins_challenge_recv(s->c_req, s, &s->io);
267         if (!NT_STATUS_IS_OK(status)) {
268                 ZERO_STRUCT(s->req->out);
269                 irpc_send_reply(s->msg, status);
270                 return;
271         }
272
273         s->req->out.num_addrs   = s->io.out.num_addresses;              
274         /* TODO: fix pidl to handle inline ipv4address arrays */
275         s->req->out.addrs       = talloc_array(s->msg, struct nbtd_proxy_wins_addr,
276                                                s->io.out.num_addresses);
277         if (!s->req->out.addrs) {
278                 ZERO_STRUCT(s->req->out);
279                 irpc_send_reply(s->msg, NT_STATUS_NO_MEMORY);
280                 return;
281         }
282         for (i=0; i < s->io.out.num_addresses; i++) {
283                 s->req->out.addrs[i].addr = talloc_steal(s->req->out.addrs, s->io.out.addresses[i]);
284         }
285
286         irpc_send_reply(s->msg, status);
287 }
288
289 NTSTATUS nbtd_proxy_wins_challenge(struct irpc_message *msg, 
290                                    struct nbtd_proxy_wins_challenge *req)
291 {
292         struct nbtd_server *nbtd_server =
293                 talloc_get_type(msg->private, struct nbtd_server);
294         struct proxy_wins_challenge_state *s;
295         uint32_t i;
296
297         s = talloc(msg, struct proxy_wins_challenge_state);
298         NT_STATUS_HAVE_NO_MEMORY(s);
299
300         s->msg = msg;
301         s->req = req;
302
303         s->io.in.nbtd_server    = nbtd_server;
304         s->io.in.event_ctx      = msg->ev;
305         s->io.in.name           = &req->in.name;
306         s->io.in.num_addresses  = req->in.num_addrs;
307         s->io.in.addresses      = talloc_array(s, const char *, req->in.num_addrs);
308         NT_STATUS_HAVE_NO_MEMORY(s->io.in.addresses);
309         /* TODO: fix pidl to handle inline ipv4address arrays */
310         for (i=0; i < req->in.num_addrs; i++) {
311                 s->io.in.addresses[i]   = talloc_steal(s->io.in.addresses, req->in.addrs[i].addr);
312         }
313
314         s->c_req = wins_challenge_send(s, &s->io);
315         NT_STATUS_HAVE_NO_MEMORY(s->c_req);
316
317         s->c_req->async.fn              = proxy_wins_challenge_handler;
318         s->c_req->async.private_data    = s;
319
320         msg->defer_reply = True;
321         return NT_STATUS_OK;
322 }
323
324 /*
325   wrepl_server needs to be able to do a name release demands, but some windows
326   servers always send the reply to port 137, regardless of the request
327   port. To cope with this we use a irpc request to the NBT server
328   which has port 137 open, and thus can receive the replies
329 */
330 struct proxy_wins_release_demand_state {
331         struct irpc_message *msg;
332         struct nbtd_proxy_wins_release_demand *req;
333         struct wins_release_demand_io io;
334         struct composite_context *c_req;
335 };
336
337 static void proxy_wins_release_demand_handler(struct composite_context *c_req)
338 {
339         NTSTATUS status;
340         struct proxy_wins_release_demand_state *s = talloc_get_type(c_req->async.private_data,
341                                                                struct proxy_wins_release_demand_state);
342
343         status = wins_release_demand_recv(s->c_req, s, &s->io);
344
345         irpc_send_reply(s->msg, status);
346 }
347
348 NTSTATUS nbtd_proxy_wins_release_demand(struct irpc_message *msg, 
349                                    struct nbtd_proxy_wins_release_demand *req)
350 {
351         struct nbtd_server *nbtd_server =
352                 talloc_get_type(msg->private, struct nbtd_server);
353         struct proxy_wins_release_demand_state *s;
354         uint32_t i;
355
356         s = talloc(msg, struct proxy_wins_release_demand_state);
357         NT_STATUS_HAVE_NO_MEMORY(s);
358
359         s->msg = msg;
360         s->req = req;
361
362         s->io.in.nbtd_server    = nbtd_server;
363         s->io.in.event_ctx      = msg->ev;
364         s->io.in.name           = &req->in.name;
365         s->io.in.num_addresses  = req->in.num_addrs;
366         s->io.in.addresses      = talloc_array(s, const char *, req->in.num_addrs);
367         NT_STATUS_HAVE_NO_MEMORY(s->io.in.addresses);
368         /* TODO: fix pidl to handle inline ipv4address arrays */
369         for (i=0; i < req->in.num_addrs; i++) {
370                 s->io.in.addresses[i]   = talloc_steal(s->io.in.addresses, req->in.addrs[i].addr);
371         }
372
373         s->c_req = wins_release_demand_send(s, &s->io);
374         NT_STATUS_HAVE_NO_MEMORY(s->c_req);
375
376         s->c_req->async.fn              = proxy_wins_release_demand_handler;
377         s->c_req->async.private_data    = s;
378
379         msg->defer_reply = True;
380         return NT_STATUS_OK;
381 }