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 3 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, see <http://www.gnu.org/licenses/>.
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"
30 struct wins_challenge_state {
31 struct wins_challenge_io *io;
32 uint32_t current_address;
33 struct nbt_name_query query;
36 static void wins_challenge_handler(struct nbt_name_request *req)
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);
41 ctx->status = nbt_name_query_recv(req, state, &state->query);
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;
49 state->query.in.dest_addr = state->io->in.addresses[state->current_address];
51 iface = nbtd_find_request_iface(state->io->in.nbtd_server, state->query.in.dest_addr, True);
53 composite_error(ctx, NT_STATUS_INTERNAL_ERROR);
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);
67 NTSTATUS wins_challenge_recv(struct composite_context *ctx, TALLOC_CTX *mem_ctx, struct wins_challenge_io *io)
69 NTSTATUS status = ctx->status;
70 struct wins_challenge_state *state = talloc_get_type(ctx->private_data, struct wins_challenge_state);
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);
84 struct composite_context *wins_challenge_send(TALLOC_CTX *mem_ctx, struct wins_challenge_io *io)
86 struct composite_context *result;
87 struct wins_challenge_state *state;
88 struct nbt_name_request *req;
89 struct nbtd_interface *iface;
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);
96 state = talloc_zero(result, struct wins_challenge_state);
97 if (state == NULL) goto failed;
98 result->private_data = state;
100 /* package up the state variables for this wack request */
102 state->current_address = 0;
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);
113 iface = nbtd_find_request_iface(state->io->in.nbtd_server, state->query.in.dest_addr, True);
118 req = nbt_name_query_send(iface->nbtsock, &state->query);
119 if (req == NULL) goto failed;
121 req->async.fn = wins_challenge_handler;
122 req->async.private = result;
130 struct wins_release_demand_io {
132 struct nbtd_server *nbtd_server;
133 struct event_context *event_ctx;
134 struct nbt_name *name;
136 uint32_t num_addresses;
137 const char **addresses;
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;
148 static void wins_release_demand_handler(struct nbt_name_request *req)
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);
153 ctx->status = nbt_name_release_recv(req, state, &state->release);
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;
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);
167 iface = nbtd_find_request_iface(state->io->in.nbtd_server, state->release.in.dest_addr, True);
169 composite_error(ctx, NT_STATUS_INTERNAL_ERROR);
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);
183 static NTSTATUS wins_release_demand_recv(struct composite_context *ctx,
185 struct wins_release_demand_io *io)
187 NTSTATUS status = ctx->status;
192 static struct composite_context *wins_release_demand_send(TALLOC_CTX *mem_ctx, struct wins_release_demand_io *io)
194 struct composite_context *result;
195 struct wins_release_demand_state *state;
196 struct nbt_name_request *req;
197 struct nbtd_interface *iface;
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);
204 state = talloc_zero(result, struct wins_release_demand_state);
205 if (state == NULL) goto failed;
206 result->private_data = state;
208 /* package up the state variables for this wack request */
210 state->current_address = 0;
211 state->addresses_left = state->io->in.num_addresses;
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)
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);
228 iface = nbtd_find_request_iface(state->io->in.nbtd_server, state->release.in.dest_addr, True);
233 req = nbt_name_release_send(iface->nbtsock, &state->release);
234 if (req == NULL) goto failed;
236 req->async.fn = wins_release_demand_handler;
237 req->async.private = result;
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
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;
258 static void proxy_wins_challenge_handler(struct composite_context *c_req)
262 struct proxy_wins_challenge_state *s = talloc_get_type(c_req->async.private_data,
263 struct proxy_wins_challenge_state);
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);
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);
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]);
285 irpc_send_reply(s->msg, status);
288 NTSTATUS nbtd_proxy_wins_challenge(struct irpc_message *msg,
289 struct nbtd_proxy_wins_challenge *req)
291 struct nbtd_server *nbtd_server =
292 talloc_get_type(msg->private, struct nbtd_server);
293 struct proxy_wins_challenge_state *s;
296 s = talloc(msg, struct proxy_wins_challenge_state);
297 NT_STATUS_HAVE_NO_MEMORY(s);
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);
313 s->c_req = wins_challenge_send(s, &s->io);
314 NT_STATUS_HAVE_NO_MEMORY(s->c_req);
316 s->c_req->async.fn = proxy_wins_challenge_handler;
317 s->c_req->async.private_data = s;
319 msg->defer_reply = True;
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
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;
336 static void proxy_wins_release_demand_handler(struct composite_context *c_req)
339 struct proxy_wins_release_demand_state *s = talloc_get_type(c_req->async.private_data,
340 struct proxy_wins_release_demand_state);
342 status = wins_release_demand_recv(s->c_req, s, &s->io);
344 irpc_send_reply(s->msg, status);
347 NTSTATUS nbtd_proxy_wins_release_demand(struct irpc_message *msg,
348 struct nbtd_proxy_wins_release_demand *req)
350 struct nbtd_server *nbtd_server =
351 talloc_get_type(msg->private, struct nbtd_server);
352 struct proxy_wins_release_demand_state *s;
355 s = talloc(msg, struct proxy_wins_release_demand_state);
356 NT_STATUS_HAVE_NO_MEMORY(s);
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);
372 s->c_req = wins_release_demand_send(s, &s->io);
373 NT_STATUS_HAVE_NO_MEMORY(s->c_req);
375 s->c_req->async.fn = proxy_wins_release_demand_handler;
376 s->c_req->async.private_data = s;
378 msg->defer_reply = True;