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
|
/*
Unix SMB/CIFS implementation.
NBT WINS server testing
Copyright (C) Andrew Tridgell 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 "libcli/nbt/libnbt.h"
#include "librpc/gen_ndr/ndr_nbt.h"
#include "lib/socket/socket.h"
#define CHECK_VALUE(v, correct) do { \
if ((v) != (correct)) { \
printf("(%s) Incorrect value %s=%d - should be %d\n", \
__location__, #v, v, correct); \
ret = False; \
}} while (0)
#define CHECK_STRING(v, correct) do { \
if ((v) != (correct) && \
((v)==NULL || (correct)==NULL || StrCaseCmp(v, correct) != 0)) { \
printf("(%s) Incorrect value %s='%s' - should be '%s'\n", \
__location__, #v, v, correct); \
ret = False; \
}} while (0)
#define CHECK_NAME(_name, correct) do { \
CHECK_STRING((_name).name, (correct).name); \
CHECK_VALUE((_name).type, (correct).type); \
CHECK_STRING((_name).scope, (correct).scope); \
} while (0)
/*
test operations against a WINS server
*/
static BOOL nbt_test_wins_name(TALLOC_CTX *mem_ctx, const char *address,
struct nbt_name *name, uint16_t nb_flags)
{
struct nbt_name_register_wins io;
struct nbt_name_query query;
struct nbt_name_refresh_wins refresh;
struct nbt_name_release release;
NTSTATUS status;
struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL);
BOOL ret = True;
const char *myaddress = talloc_strdup(mem_ctx, iface_best_ip(address));
/* we do the listen here to ensure the WINS server receives the packets from
the right IP */
socket_listen(nbtsock->sock, myaddress, 0, 0, 0);
printf("Testing name registration to WINS with name %s at %s nb_flags=0x%x\n",
nbt_name_string(mem_ctx, name), myaddress, nb_flags);
printf("release the name\n");
release.in.name = *name;
release.in.dest_addr = address;
release.in.address = myaddress;
release.in.nb_flags = nb_flags;
release.in.broadcast = False;
release.in.timeout = 3;
release.in.retries = 0;
status = nbt_name_release(nbtsock, mem_ctx, &release);
if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
printf("No response from %s for name release\n", address);
return False;
}
if (!NT_STATUS_IS_OK(status)) {
printf("Bad response from %s for name query - %s\n",
address, nt_errstr(status));
return False;
}
CHECK_VALUE(release.out.rcode, 0);
printf("register the name\n");
io.in.name = *name;
io.in.wins_servers = str_list_make(mem_ctx, address, NULL);
io.in.addresses = str_list_make(mem_ctx, myaddress, NULL);
io.in.nb_flags = nb_flags;
io.in.ttl = 300000;
status = nbt_name_register_wins(nbtsock, mem_ctx, &io);
if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
printf("No response from %s for name register\n", address);
return False;
}
if (!NT_STATUS_IS_OK(status)) {
printf("Bad response from %s for name register - %s\n",
address, nt_errstr(status));
return False;
}
CHECK_STRING(io.out.wins_server, address);
CHECK_VALUE(io.out.rcode, 0);
if (name->type != NBT_NAME_MASTER &&
name->type != NBT_NAME_LOGON &&
(nb_flags & NBT_NM_GROUP)) {
printf("Try to register as non-group\n");
io.in.nb_flags &= ~NBT_NM_GROUP;
status = nbt_name_register_wins(nbtsock, mem_ctx, &io);
if (!NT_STATUS_IS_OK(status)) {
printf("Bad response from %s for name register - %s\n",
address, nt_errstr(status));
return False;
}
CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
}
printf("query the name to make sure its there\n");
query.in.name = *name;
query.in.dest_addr = address;
query.in.broadcast = False;
query.in.wins_lookup = True;
query.in.timeout = 3;
query.in.retries = 0;
status = nbt_name_query(nbtsock, mem_ctx, &query);
if (name->type == NBT_NAME_MASTER) {
if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
printf("Bad response from %s for name query - %s\n",
address, nt_errstr(status));
return False;
}
return ret;
}
if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
printf("No response from %s for name query\n", address);
return False;
}
if (!NT_STATUS_IS_OK(status)) {
printf("Bad response from %s for name query - %s\n",
address, nt_errstr(status));
return False;
}
CHECK_NAME(query.out.name, *name);
CHECK_VALUE(query.out.num_addrs, 1);
if (name->type != NBT_NAME_LOGON &&
(nb_flags & NBT_NM_GROUP)) {
CHECK_STRING(query.out.reply_addrs[0], "255.255.255.255");
} else {
CHECK_STRING(query.out.reply_addrs[0], myaddress);
}
query.in.name.name = strupper_talloc(mem_ctx, name->name);
if (query.in.name.name &&
strcmp(query.in.name.name, name->name) != 0) {
printf("check case sensitivity\n");
status = nbt_name_query(nbtsock, mem_ctx, &query);
if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
printf("No response from %s for name query\n", address);
return False;
}
if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
printf("Bad response from %s for name query - %s\n",
address, nt_errstr(status));
return False;
}
}
query.in.name = *name;
if (name->scope) {
query.in.name.scope = strupper_talloc(mem_ctx, name->scope);
}
if (query.in.name.scope &&
strcmp(query.in.name.scope, name->scope) != 0) {
printf("check case sensitivity on scope\n");
status = nbt_name_query(nbtsock, mem_ctx, &query);
if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
printf("No response from %s for name query\n", address);
return False;
}
if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
printf("Bad response from %s for name query - %s\n",
address, nt_errstr(status));
return False;
}
}
printf("refresh the name\n");
refresh.in.name = *name;
refresh.in.wins_servers = str_list_make(mem_ctx, address, NULL);
refresh.in.addresses = str_list_make(mem_ctx, myaddress, NULL);
refresh.in.nb_flags = nb_flags;
refresh.in.ttl = 12345;
status = nbt_name_refresh_wins(nbtsock, mem_ctx, &refresh);
if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
printf("No response from %s for name refresh\n", address);
return False;
}
if (!NT_STATUS_IS_OK(status)) {
printf("Bad response from %s for name refresh - %s\n",
address, nt_errstr(status));
return False;
}
CHECK_STRING(refresh.out.wins_server, address);
CHECK_VALUE(refresh.out.rcode, 0);
printf("release the name\n");
release.in.name = *name;
release.in.dest_addr = address;
release.in.address = myaddress;
release.in.nb_flags = nb_flags;
release.in.broadcast = False;
release.in.timeout = 3;
release.in.retries = 0;
status = nbt_name_release(nbtsock, mem_ctx, &release);
if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
printf("No response from %s for name release\n", address);
return False;
}
if (!NT_STATUS_IS_OK(status)) {
printf("Bad response from %s for name query - %s\n",
address, nt_errstr(status));
return False;
}
CHECK_NAME(release.out.name, *name);
CHECK_VALUE(release.out.rcode, 0);
printf("release again\n");
status = nbt_name_release(nbtsock, mem_ctx, &release);
if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
printf("No response from %s for name release\n", address);
return False;
}
if (!NT_STATUS_IS_OK(status)) {
printf("Bad response from %s for name query - %s\n",
address, nt_errstr(status));
return False;
}
CHECK_NAME(release.out.name, *name);
CHECK_VALUE(release.out.rcode, 0);
printf("query the name to make sure its gone\n");
query.in.name = *name;
status = nbt_name_query(nbtsock, mem_ctx, &query);
if (name->type != NBT_NAME_LOGON &&
(nb_flags & NBT_NM_GROUP)) {
if (!NT_STATUS_IS_OK(status)) {
printf("ERROR: Name query failed after group release - %s\n",
nt_errstr(status));
return False;
}
} else {
if (NT_STATUS_IS_OK(status)) {
printf("ERROR: Name query success after release\n");
return False;
}
if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
printf("Incorrect response to name query - %s\n", nt_errstr(status));
return False;
}
}
return ret;
}
/*
test operations against a WINS server
*/
static BOOL nbt_test_wins(TALLOC_CTX *mem_ctx, const char *address)
{
struct nbt_name name;
BOOL ret = True;
uint32_t r = (unsigned)(random() % (100000));
name.name = talloc_asprintf(mem_ctx, "_TORTURE-%5u", r);
name.type = NBT_NAME_CLIENT;
name.scope = NULL;
ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
name.type = NBT_NAME_MASTER;
ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
name.type = NBT_NAME_LOGON;
ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
name.scope = "example";
name.type = 0x72;
ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
name.scope = "example";
name.type = 0x71;
ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
name.scope = "foo.example.com";
name.type = 0x72;
ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
name.name = talloc_asprintf(mem_ctx, "_T\01-%5u.foo", r);
ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
name.name = "";
ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
name.name = talloc_asprintf(mem_ctx, ".");
ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
name.name = talloc_asprintf(mem_ctx, "%5u-\377\200\300FOO", r);
ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
return ret;
}
/*
test WINS operations
*/
BOOL torture_nbt_wins(void)
{
const char *address;
struct nbt_name name;
TALLOC_CTX *mem_ctx = talloc_new(NULL);
NTSTATUS status;
BOOL ret = True;
make_nbt_name_server(&name, lp_parm_string(-1, "torture", "host"));
/* do an initial name resolution to find its IP */
status = resolve_name(&name, mem_ctx, &address);
if (!NT_STATUS_IS_OK(status)) {
printf("Failed to resolve %s - %s\n",
name.name, nt_errstr(status));
talloc_free(mem_ctx);
return False;
}
ret &= nbt_test_wins(mem_ctx, address);
talloc_free(mem_ctx);
return ret;
}
|