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