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