2 Unix SMB/CIFS implementation.
4 NBT WINS server testing
6 Copyright (C) Andrew Tridgell 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "lib/util/dlinklist.h"
24 #include "lib/events/events.h"
25 #include "lib/socket/socket.h"
26 #include "libcli/resolve/resolve.h"
27 #include "system/network.h"
28 #include "lib/socket/netif.h"
29 #include "librpc/gen_ndr/ndr_nbt.h"
30 #include "torture/torture.h"
31 #include "torture/nbt/proto.h"
32 #include "param/param.h"
34 #define CHECK_VALUE(tctx, v, correct) \
35 torture_assert_int_equal(tctx, v, correct, "Incorrect value")
37 #define CHECK_STRING(tctx, v, correct) \
38 torture_assert_casestr_equal(tctx, v, correct, "Incorrect value")
40 #define CHECK_NAME(tctx, _name, correct) do { \
41 CHECK_STRING(tctx, (_name).name, (correct).name); \
42 CHECK_VALUE(tctx, (uint8_t)(_name).type, (uint8_t)(correct).type); \
43 CHECK_STRING(tctx, (_name).scope, (correct).scope); \
48 test operations against a WINS server
50 static bool nbt_test_wins_name(struct torture_context
*tctx
, const char *address
,
51 struct nbt_name
*name
, uint16_t nb_flags
,
53 uint8_t register_rcode
)
55 struct nbt_name_register_wins io
;
56 struct nbt_name_register name_register
;
57 struct nbt_name_query query
;
58 struct nbt_name_refresh_wins refresh
;
59 struct nbt_name_release release
;
60 struct nbt_name_request
*req
;
62 struct nbt_name_socket
*nbtsock
= torture_init_nbt_socket(tctx
);
63 const char *myaddress
;
64 struct socket_address
*socket_address
;
65 struct interface
*ifaces
;
66 bool low_port
= try_low_port
;
69 load_interface_list(tctx
, tctx
->lp_ctx
, &ifaces
);
71 myaddress
= talloc_strdup(tctx
, iface_list_best_ip(ifaces
, address
));
73 socket_address
= socket_address_from_strings(tctx
,
74 nbtsock
->sock
->backend_name
,
75 myaddress
, lpcfg_nbt_port(tctx
->lp_ctx
));
76 torture_assert(tctx
, socket_address
!= NULL
,
77 "Error getting address");
79 /* we do the listen here to ensure the WINS server receives the packets from
81 status
= socket_listen(nbtsock
->sock
, socket_address
, 0, 0);
82 talloc_free(socket_address
);
83 if (!NT_STATUS_IS_OK(status
)) {
85 socket_address
= socket_address_from_strings(tctx
,
86 nbtsock
->sock
->backend_name
,
88 torture_assert(tctx
, socket_address
!= NULL
,
89 "Error getting address");
91 status
= socket_listen(nbtsock
->sock
, socket_address
, 0, 0);
92 talloc_free(socket_address
);
93 torture_assert_ntstatus_ok(tctx
, status
,
94 "socket_listen for WINS failed");
97 torture_comment(tctx
, "Testing name registration to WINS with name %s at %s nb_flags=0x%x\n",
98 nbt_name_string(tctx
, name
), myaddress
, nb_flags
);
100 torture_comment(tctx
, "release the name\n");
101 release
.in
.name
= *name
;
102 release
.in
.dest_port
= lpcfg_nbt_port(tctx
->lp_ctx
);
103 release
.in
.dest_addr
= address
;
104 release
.in
.address
= myaddress
;
105 release
.in
.nb_flags
= nb_flags
;
106 release
.in
.broadcast
= false;
107 release
.in
.timeout
= 3;
108 release
.in
.retries
= 0;
110 status
= nbt_name_release(nbtsock
, tctx
, &release
);
111 torture_assert_ntstatus_ok(tctx
, status
, talloc_asprintf(tctx
, "Bad response from %s for name query", address
));
112 CHECK_VALUE(tctx
, release
.out
.rcode
, 0);
114 if (nb_flags
& NBT_NM_GROUP
) {
115 /* ignore this for group names */
116 } else if (!low_port
) {
117 torture_comment(tctx
, "no low port - skip: register the name with a wrong address\n");
119 torture_comment(tctx
, "register the name with a wrong address (makes the next request slow!)\n");
121 io
.in
.wins_port
= lpcfg_nbt_port(tctx
->lp_ctx
);
122 io
.in
.wins_servers
= const_str_list(
123 str_list_make_single(tctx
, address
));
124 io
.in
.addresses
= const_str_list(
125 str_list_make_single(tctx
, "127.64.64.1"));
126 io
.in
.nb_flags
= nb_flags
;
129 status
= nbt_name_register_wins(nbtsock
, tctx
, &io
);
130 if (NT_STATUS_EQUAL(status
, NT_STATUS_IO_TIMEOUT
)) {
131 torture_assert_ntstatus_ok(tctx
, status
,
132 talloc_asprintf(tctx
, "No response from %s for name register\n",
135 torture_assert_ntstatus_ok(tctx
, status
,
136 talloc_asprintf(tctx
, "Bad response from %s for name register\n",
139 CHECK_STRING(tctx
, io
.out
.wins_server
, address
);
140 CHECK_VALUE(tctx
, io
.out
.rcode
, 0);
142 torture_comment(tctx
, "register the name correct address\n");
143 name_register
.in
.name
= *name
;
144 name_register
.in
.dest_port
= lpcfg_nbt_port(tctx
->lp_ctx
);
145 name_register
.in
.dest_addr
= address
;
146 name_register
.in
.address
= myaddress
;
147 name_register
.in
.nb_flags
= nb_flags
;
148 name_register
.in
.register_demand
= false;
149 name_register
.in
.broadcast
= false;
150 name_register
.in
.multi_homed
= true;
151 name_register
.in
.ttl
= 300000;
152 name_register
.in
.timeout
= 3;
153 name_register
.in
.retries
= 2;
156 * test if the server ignores resent requests
158 req
= nbt_name_register_send(nbtsock
, &name_register
);
160 tevent_loop_once(nbtsock
->event_ctx
);
161 if (req
->state
!= NBT_REQUEST_WAIT
) {
164 if (req
->received_wack
) {
166 * if we received the wack response
167 * we resend the request and the
168 * server should ignore that
169 * and not handle it as new request
171 req
->state
= NBT_REQUEST_SEND
;
172 DLIST_ADD_END(nbtsock
->send_queue
, req
);
173 TEVENT_FD_WRITEABLE(nbtsock
->fde
);
178 status
= nbt_name_register_recv(req
, tctx
, &name_register
);
179 if (NT_STATUS_EQUAL(status
, NT_STATUS_IO_TIMEOUT
)) {
180 torture_assert_ntstatus_ok(tctx
, status
,
181 talloc_asprintf(tctx
, "No response from %s for name register\n",
184 torture_assert_ntstatus_ok(tctx
, status
,
185 talloc_asprintf(tctx
, "Bad response from %s for name register\n",
188 CHECK_VALUE(tctx
, name_register
.out
.rcode
, 0);
189 CHECK_STRING(tctx
, name_register
.out
.reply_addr
, myaddress
);
192 torture_comment(tctx
, "register the name correct address\n");
194 io
.in
.wins_port
= lpcfg_nbt_port(tctx
->lp_ctx
);
195 l
= str_list_make_single(tctx
, address
);
196 io
.in
.wins_servers
= discard_const_p(const char *, l
);
197 l
= str_list_make_single(tctx
, myaddress
);
198 io
.in
.addresses
= discard_const_p(const char *, l
);
199 io
.in
.nb_flags
= nb_flags
;
202 status
= nbt_name_register_wins(nbtsock
, tctx
, &io
);
203 torture_assert_ntstatus_ok(tctx
, status
, talloc_asprintf(tctx
, "Bad response from %s for name register", address
));
205 CHECK_STRING(tctx
, io
.out
.wins_server
, address
);
206 CHECK_VALUE(tctx
, io
.out
.rcode
, register_rcode
);
208 if (register_rcode
!= NBT_RCODE_OK
) {
212 if (name
->type
!= NBT_NAME_MASTER
&&
213 name
->type
!= NBT_NAME_LOGON
&&
214 name
->type
!= NBT_NAME_BROWSER
&&
215 (nb_flags
& NBT_NM_GROUP
)) {
216 torture_comment(tctx
, "Try to register as non-group\n");
217 io
.in
.nb_flags
&= ~NBT_NM_GROUP
;
218 status
= nbt_name_register_wins(nbtsock
, tctx
, &io
);
219 torture_assert_ntstatus_ok(tctx
, status
, talloc_asprintf(tctx
, "Bad response from %s for name register\n",
221 CHECK_VALUE(tctx
, io
.out
.rcode
, NBT_RCODE_ACT
);
224 torture_comment(tctx
, "query the name to make sure its there\n");
225 query
.in
.name
= *name
;
226 query
.in
.dest_addr
= address
;
227 query
.in
.dest_port
= lpcfg_nbt_port(tctx
->lp_ctx
);
228 query
.in
.broadcast
= false;
229 query
.in
.wins_lookup
= true;
230 query
.in
.timeout
= 3;
231 query
.in
.retries
= 0;
233 status
= nbt_name_query(nbtsock
, tctx
, &query
);
234 if (name
->type
== NBT_NAME_MASTER
) {
235 torture_assert_ntstatus_equal(
236 tctx
, status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
,
237 talloc_asprintf(tctx
, "Bad response from %s for name query", address
));
240 torture_assert_ntstatus_ok(tctx
, status
, talloc_asprintf(tctx
, "Bad response from %s for name query", address
));
242 CHECK_NAME(tctx
, query
.out
.name
, *name
);
243 CHECK_VALUE(tctx
, query
.out
.num_addrs
, 1);
244 if (name
->type
!= NBT_NAME_LOGON
&&
245 (nb_flags
& NBT_NM_GROUP
)) {
246 CHECK_STRING(tctx
, query
.out
.reply_addrs
[0], "255.255.255.255");
248 CHECK_STRING(tctx
, query
.out
.reply_addrs
[0], myaddress
);
252 query
.in
.name
.name
= strupper_talloc(tctx
, name
->name
);
253 if (query
.in
.name
.name
&&
254 strcmp(query
.in
.name
.name
, name
->name
) != 0) {
255 torture_comment(tctx
, "check case sensitivity\n");
256 status
= nbt_name_query(nbtsock
, tctx
, &query
);
257 torture_assert_ntstatus_equal(tctx
, status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
, talloc_asprintf(tctx
, "Bad response from %s for name query", address
));
260 query
.in
.name
= *name
;
262 query
.in
.name
.scope
= strupper_talloc(tctx
, name
->scope
);
264 if (query
.in
.name
.scope
&&
265 strcmp(query
.in
.name
.scope
, name
->scope
) != 0) {
266 torture_comment(tctx
, "check case sensitivity on scope\n");
267 status
= nbt_name_query(nbtsock
, tctx
, &query
);
268 torture_assert_ntstatus_equal(tctx
, status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
, talloc_asprintf(tctx
, "Bad response from %s for name query", address
));
271 torture_comment(tctx
, "refresh the name\n");
272 refresh
.in
.name
= *name
;
273 refresh
.in
.wins_port
= lpcfg_nbt_port(tctx
->lp_ctx
);
274 l
= str_list_make_single(tctx
, address
);
275 refresh
.in
.wins_servers
= discard_const_p(const char *, l
);
276 l
= str_list_make_single(tctx
, myaddress
);
277 refresh
.in
.addresses
= discard_const_p(const char *, l
);
278 refresh
.in
.nb_flags
= nb_flags
;
279 refresh
.in
.ttl
= 12345;
281 status
= nbt_name_refresh_wins(nbtsock
, tctx
, &refresh
);
282 if (NT_STATUS_EQUAL(status
, NT_STATUS_IO_TIMEOUT
)) {
283 torture_assert_ntstatus_ok(tctx
, status
,
284 talloc_asprintf(tctx
, "No response from %s for name refresh",
287 torture_assert_ntstatus_ok(tctx
, status
,
288 talloc_asprintf(tctx
, "Bad response from %s for name refresh",
291 CHECK_STRING(tctx
, refresh
.out
.wins_server
, address
);
292 CHECK_VALUE(tctx
, refresh
.out
.rcode
, 0);
294 printf("release the name\n");
295 release
.in
.name
= *name
;
296 release
.in
.dest_port
= lpcfg_nbt_port(tctx
->lp_ctx
);
297 release
.in
.dest_addr
= address
;
298 release
.in
.address
= myaddress
;
299 release
.in
.nb_flags
= nb_flags
;
300 release
.in
.broadcast
= false;
301 release
.in
.timeout
= 3;
302 release
.in
.retries
= 0;
304 status
= nbt_name_release(nbtsock
, tctx
, &release
);
305 if (NT_STATUS_EQUAL(status
, NT_STATUS_IO_TIMEOUT
)) {
306 torture_assert_ntstatus_ok(tctx
, status
,
307 talloc_asprintf(tctx
, "No response from %s for name release",
310 torture_assert_ntstatus_ok(tctx
, status
,
311 talloc_asprintf(tctx
, "Bad response from %s for name release",
314 CHECK_NAME(tctx
, release
.out
.name
, *name
);
315 CHECK_VALUE(tctx
, release
.out
.rcode
, 0);
317 if (nb_flags
& NBT_NM_GROUP
) {
318 /* ignore this for group names */
319 } else if (!low_port
) {
320 torture_comment(tctx
, "no low port - skip: register the name with a wrong address\n");
322 torture_comment(tctx
, "register the name with a wrong address (makes the next request slow!)\n");
324 io
.in
.wins_port
= lpcfg_nbt_port(tctx
->lp_ctx
);
325 io
.in
.wins_servers
= const_str_list(
326 str_list_make_single(tctx
, address
));
327 io
.in
.addresses
= const_str_list(
328 str_list_make_single(tctx
, "127.64.64.1"));
329 io
.in
.nb_flags
= nb_flags
;
332 status
= nbt_name_register_wins(nbtsock
, tctx
, &io
);
333 if (NT_STATUS_EQUAL(status
, NT_STATUS_IO_TIMEOUT
)) {
334 torture_assert_ntstatus_ok(tctx
, status
,
335 talloc_asprintf(tctx
, "No response from %s for name register\n",
338 torture_assert_ntstatus_ok(tctx
, status
,
339 talloc_asprintf(tctx
, "Bad response from %s for name register\n",
342 CHECK_STRING(tctx
, io
.out
.wins_server
, address
);
343 CHECK_VALUE(tctx
, io
.out
.rcode
, 0);
346 torture_comment(tctx
, "refresh the name with the correct address\n");
347 refresh
.in
.name
= *name
;
348 refresh
.in
.wins_port
= lpcfg_nbt_port(tctx
->lp_ctx
);
349 refresh
.in
.wins_servers
= const_str_list(
350 str_list_make_single(tctx
, address
));
351 refresh
.in
.addresses
= const_str_list(
352 str_list_make_single(tctx
, myaddress
));
353 refresh
.in
.nb_flags
= nb_flags
;
354 refresh
.in
.ttl
= 12345;
356 status
= nbt_name_refresh_wins(nbtsock
, tctx
, &refresh
);
357 if (NT_STATUS_EQUAL(status
, NT_STATUS_IO_TIMEOUT
)) {
358 torture_assert_ntstatus_ok(tctx
, status
,
359 talloc_asprintf(tctx
, "No response from %s for name refresh",
362 torture_assert_ntstatus_ok(tctx
, status
,
363 talloc_asprintf(tctx
, "Bad response from %s for name refresh",
366 CHECK_STRING(tctx
, refresh
.out
.wins_server
, address
);
367 CHECK_VALUE(tctx
, refresh
.out
.rcode
, 0);
369 torture_comment(tctx
, "release the name\n");
370 release
.in
.name
= *name
;
371 release
.in
.dest_port
= lpcfg_nbt_port(tctx
->lp_ctx
);
372 release
.in
.dest_addr
= address
;
373 release
.in
.address
= myaddress
;
374 release
.in
.nb_flags
= nb_flags
;
375 release
.in
.broadcast
= false;
376 release
.in
.timeout
= 3;
377 release
.in
.retries
= 0;
379 status
= nbt_name_release(nbtsock
, tctx
, &release
);
380 torture_assert_ntstatus_ok(tctx
, status
, talloc_asprintf(tctx
, "Bad response from %s for name query", address
));
382 CHECK_NAME(tctx
, release
.out
.name
, *name
);
383 CHECK_VALUE(tctx
, release
.out
.rcode
, 0);
385 torture_comment(tctx
, "release again\n");
386 status
= nbt_name_release(nbtsock
, tctx
, &release
);
387 torture_assert_ntstatus_ok(tctx
, status
,
388 talloc_asprintf(tctx
, "Bad response from %s for name query",
391 CHECK_NAME(tctx
, release
.out
.name
, *name
);
392 CHECK_VALUE(tctx
, release
.out
.rcode
, 0);
395 torture_comment(tctx
, "query the name to make sure its gone\n");
396 query
.in
.name
= *name
;
397 status
= nbt_name_query(nbtsock
, tctx
, &query
);
398 if (name
->type
!= NBT_NAME_LOGON
&&
399 (nb_flags
& NBT_NM_GROUP
)) {
400 torture_assert_ntstatus_ok(tctx
, status
,
401 "ERROR: Name query failed after group release");
403 torture_assert_ntstatus_equal(tctx
, status
,
404 NT_STATUS_OBJECT_NAME_NOT_FOUND
,
405 "Incorrect response to name query");
412 static char *test_nbt_wins_scope_string(TALLOC_CTX
*mem_ctx
, uint8_t count
)
417 res
= talloc_array(mem_ctx
, char, count
+1);
422 for (i
=0; i
< count
; i
++) {
426 case 63 + 1 + 63 + 1 + 63:
430 res
[i
] = '0' + (i
%10);
437 talloc_set_name_const(res
, res
);
443 test operations against a WINS server
445 static bool nbt_test_wins(struct torture_context
*tctx
)
447 struct nbt_name name
;
448 uint32_t r
= (uint32_t)(random() % (100000));
452 if (!torture_nbt_get_name(tctx
, &name
, &address
))
455 name
.name
= talloc_asprintf(tctx
, "_TORTURE-%5u", r
);
457 name
.type
= NBT_NAME_CLIENT
;
459 ret
&= nbt_test_wins_name(tctx
, address
, &name
,
460 NBT_NODE_H
, true, NBT_RCODE_OK
);
462 name
.type
= NBT_NAME_MASTER
;
463 ret
&= nbt_test_wins_name(tctx
, address
, &name
,
464 NBT_NODE_H
, false, NBT_RCODE_OK
);
466 ret
&= nbt_test_wins_name(tctx
, address
, &name
,
467 NBT_NODE_H
| NBT_NM_GROUP
, false, NBT_RCODE_OK
);
469 name
.type
= NBT_NAME_SERVER
;
470 ret
&= nbt_test_wins_name(tctx
, address
, &name
,
471 NBT_NODE_H
, true, NBT_RCODE_OK
);
473 name
.type
= NBT_NAME_LOGON
;
474 ret
&= nbt_test_wins_name(tctx
, address
, &name
,
475 NBT_NODE_H
| NBT_NM_GROUP
, false, NBT_RCODE_OK
);
477 name
.type
= NBT_NAME_BROWSER
;
478 ret
&= nbt_test_wins_name(tctx
, address
, &name
,
479 NBT_NODE_H
| NBT_NM_GROUP
, false, NBT_RCODE_OK
);
481 name
.type
= NBT_NAME_PDC
;
482 ret
&= nbt_test_wins_name(tctx
, address
, &name
,
483 NBT_NODE_H
, true, NBT_RCODE_OK
);
486 ret
&= nbt_test_wins_name(tctx
, address
, &name
,
487 NBT_NODE_H
, true, NBT_RCODE_OK
);
490 ret
&= nbt_test_wins_name(tctx
, address
, &name
,
491 NBT_NODE_H
, false, NBT_RCODE_OK
);
493 name
.scope
= "example";
495 ret
&= nbt_test_wins_name(tctx
, address
, &name
,
496 NBT_NODE_H
, true, NBT_RCODE_OK
);
498 name
.scope
= "example";
500 ret
&= nbt_test_wins_name(tctx
, address
, &name
,
501 NBT_NODE_H
| NBT_NM_GROUP
, false, NBT_RCODE_OK
);
503 name
.scope
= "foo.example.com";
505 ret
&= nbt_test_wins_name(tctx
, address
, &name
,
506 NBT_NODE_H
, false, NBT_RCODE_OK
);
508 name
.name
= talloc_asprintf(tctx
, "_T\01-%5u.foo", r
);
509 ret
&= nbt_test_wins_name(tctx
, address
, &name
,
510 NBT_NODE_H
, false, NBT_RCODE_OK
);
513 ret
&= nbt_test_wins_name(tctx
, address
, &name
,
514 NBT_NODE_H
, false, NBT_RCODE_OK
);
516 name
.name
= talloc_asprintf(tctx
, ".");
517 ret
&= nbt_test_wins_name(tctx
, address
, &name
,
518 NBT_NODE_H
, false, NBT_RCODE_OK
);
520 name
.name
= talloc_asprintf(tctx
, "%5u-\377\200\300FOO", r
);
521 ret
&= nbt_test_wins_name(tctx
, address
, &name
,
522 NBT_NODE_H
, false, NBT_RCODE_OK
);
524 name
.scope
= test_nbt_wins_scope_string(tctx
, 237);
525 ret
&= nbt_test_wins_name(tctx
, address
, &name
,
526 NBT_NODE_H
, false, NBT_RCODE_OK
);
528 name
.scope
= test_nbt_wins_scope_string(tctx
, 238);
529 ret
&= nbt_test_wins_name(tctx
, address
, &name
,
530 NBT_NODE_H
, false, NBT_RCODE_SVR
);
538 struct torture_suite
*torture_nbt_wins(TALLOC_CTX
*mem_ctx
)
540 struct torture_suite
*suite
= torture_suite_create(mem_ctx
, "wins");
542 torture_suite_add_simple_test(suite
, "wins", nbt_test_wins
);