2 Unix SMB/CIFS implementation.
4 "secure" wins server WACK processing
6 Copyright (C) Andrew Tridgell 2005
7 Copyright (C) Stefan Metzmacher 2005
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.
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.
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.
25 #include "nbt_server/nbt_server.h"
26 #include "nbt_server/wins/winsdb.h"
27 #include "nbt_server/wins/winsserver.h"
28 #include "system/time.h"
29 #include "libcli/composite/composite.h"
31 struct wins_challenge_state {
32 struct wins_challenge_io *io;
33 uint32_t current_address;
34 struct nbt_name_query query;
37 static void wins_challenge_handler(struct nbt_name_request *req)
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);
42 ctx->status = nbt_name_query_recv(req, state, &state->query);
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;
50 state->query.in.dest_addr = state->io->in.addresses[state->current_address];
52 iface = nbtd_find_interface(state->io->in.nbtd_server, state->query.in.dest_addr);
54 composite_error(ctx, NT_STATUS_INTERNAL_ERROR);
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);
68 NTSTATUS wins_challenge_recv(struct composite_context *ctx, TALLOC_CTX *mem_ctx, struct wins_challenge_io *io)
70 NTSTATUS status = ctx->status;
71 struct wins_challenge_state *state = talloc_get_type(ctx->private_data, struct wins_challenge_state);
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);
85 struct composite_context *wins_challenge_send(TALLOC_CTX *mem_ctx, struct wins_challenge_io *io)
87 struct composite_context *result;
88 struct wins_challenge_state *state;
89 struct nbt_name_request *req;
90 struct nbtd_interface *iface;
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);
97 state = talloc_zero(result, struct wins_challenge_state);
98 if (state == NULL) goto failed;
99 result->private_data = state;
101 /* package up the state variables for this wack request */
103 state->current_address = 0;
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);
114 iface = nbtd_find_interface(state->io->in.nbtd_server, state->query.in.dest_addr);
119 req = nbt_name_query_send(iface->nbtsock, &state->query);
120 if (req == NULL) goto failed;
122 req->async.fn = wins_challenge_handler;
123 req->async.private = result;
131 struct wins_release_demand_io {
133 struct nbtd_server *nbtd_server;
134 struct event_context *event_ctx;
135 struct nbt_name *name;
137 uint32_t num_addresses;
138 const char **addresses;
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;
149 static void wins_release_demand_handler(struct nbt_name_request *req)
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);
154 ctx->status = nbt_name_release_recv(req, state, &state->release);
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;
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);
168 iface = nbtd_find_interface(state->io->in.nbtd_server, state->release.in.dest_addr);
170 composite_error(ctx, NT_STATUS_INTERNAL_ERROR);
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);
184 static NTSTATUS wins_release_demand_recv(struct composite_context *ctx,
186 struct wins_release_demand_io *io)
188 NTSTATUS status = ctx->status;
193 static struct composite_context *wins_release_demand_send(TALLOC_CTX *mem_ctx, struct wins_release_demand_io *io)
195 struct composite_context *result;
196 struct wins_release_demand_state *state;
197 struct nbt_name_request *req;
198 struct nbtd_interface *iface;
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);
205 state = talloc_zero(result, struct wins_release_demand_state);
206 if (state == NULL) goto failed;
207 result->private_data = state;
209 /* package up the state variables for this wack request */
211 state->current_address = 0;
212 state->addresses_left = state->io->in.num_addresses;
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)
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);
229 iface = nbtd_find_interface(state->io->in.nbtd_server, state->release.in.dest_addr);
234 req = nbt_name_release_send(iface->nbtsock, &state->release);
235 if (req == NULL) goto failed;
237 req->async.fn = wins_release_demand_handler;
238 req->async.private = result;
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
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;
259 static void proxy_wins_challenge_handler(struct composite_context *c_req)
263 struct proxy_wins_challenge_state *s = talloc_get_type(c_req->async.private_data,
264 struct proxy_wins_challenge_state);
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);
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);
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]);
286 irpc_send_reply(s->msg, status);
289 NTSTATUS nbtd_proxy_wins_challenge(struct irpc_message *msg,
290 struct nbtd_proxy_wins_challenge *req)
292 struct nbtd_server *nbtd_server =
293 talloc_get_type(msg->private, struct nbtd_server);
294 struct proxy_wins_challenge_state *s;
297 s = talloc(msg, struct proxy_wins_challenge_state);
298 NT_STATUS_HAVE_NO_MEMORY(s);
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);
314 s->c_req = wins_challenge_send(s, &s->io);
315 NT_STATUS_HAVE_NO_MEMORY(s->c_req);
317 s->c_req->async.fn = proxy_wins_challenge_handler;
318 s->c_req->async.private_data = s;
320 msg->defer_reply = True;
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
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;
337 static void proxy_wins_release_demand_handler(struct composite_context *c_req)
340 struct proxy_wins_release_demand_state *s = talloc_get_type(c_req->async.private_data,
341 struct proxy_wins_release_demand_state);
343 status = wins_release_demand_recv(s->c_req, s, &s->io);
345 irpc_send_reply(s->msg, status);
348 NTSTATUS nbtd_proxy_wins_release_demand(struct irpc_message *msg,
349 struct nbtd_proxy_wins_release_demand *req)
351 struct nbtd_server *nbtd_server =
352 talloc_get_type(msg->private, struct nbtd_server);
353 struct proxy_wins_release_demand_state *s;
356 s = talloc(msg, struct proxy_wins_release_demand_state);
357 NT_STATUS_HAVE_NO_MEMORY(s);
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);
373 s->c_req = wins_release_demand_send(s, &s->io);
374 NT_STATUS_HAVE_NO_MEMORY(s->c_req);
376 s->c_req->async.fn = proxy_wins_release_demand_handler;
377 s->c_req->async.private_data = s;
379 msg->defer_reply = True;