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
;
68 load_interfaces(tctx
, lp_interfaces(tctx
->lp_ctx
), &ifaces
);
70 myaddress
= talloc_strdup(tctx
, iface_best_ip(ifaces
, address
));
72 socket_address
= socket_address_from_strings(tctx
,
73 nbtsock
->sock
->backend_name
,
74 myaddress
, lp_nbt_port(tctx
->lp_ctx
));
75 torture_assert(tctx
, socket_address
!= NULL
,
76 "Error getting address");
78 /* we do the listen here to ensure the WINS server receives the packets from
80 status
= socket_listen(nbtsock
->sock
, socket_address
, 0, 0);
81 talloc_free(socket_address
);
82 if (!NT_STATUS_IS_OK(status
)) {
84 socket_address
= socket_address_from_strings(tctx
,
85 nbtsock
->sock
->backend_name
,
87 torture_assert(tctx
, socket_address
!= NULL
,
88 "Error getting address");
90 status
= socket_listen(nbtsock
->sock
, socket_address
, 0, 0);
91 talloc_free(socket_address
);
92 torture_assert_ntstatus_ok(tctx
, status
,
93 "socket_listen for WINS failed");
96 torture_comment(tctx
, "Testing name registration to WINS with name %s at %s nb_flags=0x%x\n",
97 nbt_name_string(tctx
, name
), myaddress
, nb_flags
);
99 torture_comment(tctx
, "release the name\n");
100 release
.in
.name
= *name
;
101 release
.in
.dest_port
= lp_nbt_port(tctx
->lp_ctx
);
102 release
.in
.dest_addr
= address
;
103 release
.in
.address
= myaddress
;
104 release
.in
.nb_flags
= nb_flags
;
105 release
.in
.broadcast
= false;
106 release
.in
.timeout
= 3;
107 release
.in
.retries
= 0;
109 status
= nbt_name_release(nbtsock
, tctx
, &release
);
110 torture_assert_ntstatus_ok(tctx
, status
, talloc_asprintf(tctx
, "Bad response from %s for name query", address
));
111 CHECK_VALUE(tctx
, release
.out
.rcode
, 0);
113 if (nb_flags
& NBT_NM_GROUP
) {
114 /* ignore this for group names */
115 } else if (!low_port
) {
116 torture_comment(tctx
, "no low port - skip: register the name with a wrong address\n");
118 torture_comment(tctx
, "register the name with a wrong address (makes the next request slow!)\n");
120 io
.in
.wins_port
= lp_nbt_port(tctx
->lp_ctx
);
121 io
.in
.wins_servers
= const_str_list(
122 str_list_make_single(tctx
, address
));
123 io
.in
.addresses
= const_str_list(
124 str_list_make_single(tctx
, "127.64.64.1"));
125 io
.in
.nb_flags
= nb_flags
;
128 status
= nbt_name_register_wins(nbtsock
, tctx
, &io
);
129 if (NT_STATUS_EQUAL(status
, NT_STATUS_IO_TIMEOUT
)) {
130 torture_assert_ntstatus_ok(tctx
, status
,
131 talloc_asprintf(tctx
, "No response from %s for name register\n",
134 torture_assert_ntstatus_ok(tctx
, status
,
135 talloc_asprintf(tctx
, "Bad response from %s for name register\n",
138 CHECK_STRING(tctx
, io
.out
.wins_server
, address
);
139 CHECK_VALUE(tctx
, io
.out
.rcode
, 0);
141 torture_comment(tctx
, "register the name correct address\n");
142 name_register
.in
.name
= *name
;
143 name_register
.in
.dest_port
= lp_nbt_port(tctx
->lp_ctx
);
144 name_register
.in
.dest_addr
= address
;
145 name_register
.in
.address
= myaddress
;
146 name_register
.in
.nb_flags
= nb_flags
;
147 name_register
.in
.register_demand
= false;
148 name_register
.in
.broadcast
= false;
149 name_register
.in
.multi_homed
= true;
150 name_register
.in
.ttl
= 300000;
151 name_register
.in
.timeout
= 3;
152 name_register
.in
.retries
= 2;
155 * test if the server ignores resent requests
157 req
= nbt_name_register_send(nbtsock
, &name_register
);
159 event_loop_once(nbtsock
->event_ctx
);
160 if (req
->state
!= NBT_REQUEST_WAIT
) {
163 if (req
->received_wack
) {
165 * if we received the wack response
166 * we resend the request and the
167 * server should ignore that
168 * and not handle it as new request
170 req
->state
= NBT_REQUEST_SEND
;
171 DLIST_ADD_END(nbtsock
->send_queue
, req
,
172 struct nbt_name_request
*);
173 EVENT_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
= lp_nbt_port(tctx
->lp_ctx
);
195 io
.in
.wins_servers
= (const char **)str_list_make_single(tctx
, address
);
196 io
.in
.addresses
= (const char **)str_list_make_single(tctx
, myaddress
);
197 io
.in
.nb_flags
= nb_flags
;
200 status
= nbt_name_register_wins(nbtsock
, tctx
, &io
);
201 torture_assert_ntstatus_ok(tctx
, status
, talloc_asprintf(tctx
, "Bad response from %s for name register", address
));
203 CHECK_STRING(tctx
, io
.out
.wins_server
, address
);
204 CHECK_VALUE(tctx
, io
.out
.rcode
, register_rcode
);
206 if (register_rcode
!= NBT_RCODE_OK
) {
210 if (name
->type
!= NBT_NAME_MASTER
&&
211 name
->type
!= NBT_NAME_LOGON
&&
212 name
->type
!= NBT_NAME_BROWSER
&&
213 (nb_flags
& NBT_NM_GROUP
)) {
214 torture_comment(tctx
, "Try to register as non-group\n");
215 io
.in
.nb_flags
&= ~NBT_NM_GROUP
;
216 status
= nbt_name_register_wins(nbtsock
, tctx
, &io
);
217 torture_assert_ntstatus_ok(tctx
, status
, talloc_asprintf(tctx
, "Bad response from %s for name register\n",
219 CHECK_VALUE(tctx
, io
.out
.rcode
, NBT_RCODE_ACT
);
222 torture_comment(tctx
, "query the name to make sure its there\n");
223 query
.in
.name
= *name
;
224 query
.in
.dest_addr
= address
;
225 query
.in
.dest_port
= lp_nbt_port(tctx
->lp_ctx
);
226 query
.in
.broadcast
= false;
227 query
.in
.wins_lookup
= true;
228 query
.in
.timeout
= 3;
229 query
.in
.retries
= 0;
231 status
= nbt_name_query(nbtsock
, tctx
, &query
);
232 if (name
->type
== NBT_NAME_MASTER
) {
233 torture_assert_ntstatus_equal(
234 tctx
, status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
,
235 talloc_asprintf(tctx
, "Bad response from %s for name query", address
));
238 torture_assert_ntstatus_ok(tctx
, status
, talloc_asprintf(tctx
, "Bad response from %s for name query", address
));
240 CHECK_NAME(tctx
, query
.out
.name
, *name
);
241 CHECK_VALUE(tctx
, query
.out
.num_addrs
, 1);
242 if (name
->type
!= NBT_NAME_LOGON
&&
243 (nb_flags
& NBT_NM_GROUP
)) {
244 CHECK_STRING(tctx
, query
.out
.reply_addrs
[0], "255.255.255.255");
246 CHECK_STRING(tctx
, query
.out
.reply_addrs
[0], myaddress
);
250 query
.in
.name
.name
= strupper_talloc(tctx
, name
->name
);
251 if (query
.in
.name
.name
&&
252 strcmp(query
.in
.name
.name
, name
->name
) != 0) {
253 torture_comment(tctx
, "check case sensitivity\n");
254 status
= nbt_name_query(nbtsock
, tctx
, &query
);
255 torture_assert_ntstatus_equal(tctx
, status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
, talloc_asprintf(tctx
, "Bad response from %s for name query", address
));
258 query
.in
.name
= *name
;
260 query
.in
.name
.scope
= strupper_talloc(tctx
, name
->scope
);
262 if (query
.in
.name
.scope
&&
263 strcmp(query
.in
.name
.scope
, name
->scope
) != 0) {
264 torture_comment(tctx
, "check case sensitivity on scope\n");
265 status
= nbt_name_query(nbtsock
, tctx
, &query
);
266 torture_assert_ntstatus_equal(tctx
, status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
, talloc_asprintf(tctx
, "Bad response from %s for name query", address
));
269 torture_comment(tctx
, "refresh the name\n");
270 refresh
.in
.name
= *name
;
271 refresh
.in
.wins_port
= lp_nbt_port(tctx
->lp_ctx
);
272 refresh
.in
.wins_servers
= (const char **)str_list_make_single(tctx
, address
);
273 refresh
.in
.addresses
= (const char **)str_list_make_single(tctx
, myaddress
);
274 refresh
.in
.nb_flags
= nb_flags
;
275 refresh
.in
.ttl
= 12345;
277 status
= nbt_name_refresh_wins(nbtsock
, tctx
, &refresh
);
278 if (NT_STATUS_EQUAL(status
, NT_STATUS_IO_TIMEOUT
)) {
279 torture_assert_ntstatus_ok(tctx
, status
,
280 talloc_asprintf(tctx
, "No response from %s for name refresh",
283 torture_assert_ntstatus_ok(tctx
, status
,
284 talloc_asprintf(tctx
, "Bad response from %s for name refresh",
287 CHECK_STRING(tctx
, refresh
.out
.wins_server
, address
);
288 CHECK_VALUE(tctx
, refresh
.out
.rcode
, 0);
290 printf("release the name\n");
291 release
.in
.name
= *name
;
292 release
.in
.dest_port
= lp_nbt_port(tctx
->lp_ctx
);
293 release
.in
.dest_addr
= address
;
294 release
.in
.address
= myaddress
;
295 release
.in
.nb_flags
= nb_flags
;
296 release
.in
.broadcast
= false;
297 release
.in
.timeout
= 3;
298 release
.in
.retries
= 0;
300 status
= nbt_name_release(nbtsock
, tctx
, &release
);
301 if (NT_STATUS_EQUAL(status
, NT_STATUS_IO_TIMEOUT
)) {
302 torture_assert_ntstatus_ok(tctx
, status
,
303 talloc_asprintf(tctx
, "No response from %s for name release",
306 torture_assert_ntstatus_ok(tctx
, status
,
307 talloc_asprintf(tctx
, "Bad response from %s for name release",
310 CHECK_NAME(tctx
, release
.out
.name
, *name
);
311 CHECK_VALUE(tctx
, release
.out
.rcode
, 0);
313 if (nb_flags
& NBT_NM_GROUP
) {
314 /* ignore this for group names */
315 } else if (!low_port
) {
316 torture_comment(tctx
, "no low port - skip: register the name with a wrong address\n");
318 torture_comment(tctx
, "register the name with a wrong address (makes the next request slow!)\n");
320 io
.in
.wins_port
= lp_nbt_port(tctx
->lp_ctx
);
321 io
.in
.wins_servers
= const_str_list(
322 str_list_make_single(tctx
, address
));
323 io
.in
.addresses
= const_str_list(
324 str_list_make_single(tctx
, "127.64.64.1"));
325 io
.in
.nb_flags
= nb_flags
;
328 status
= nbt_name_register_wins(nbtsock
, tctx
, &io
);
329 if (NT_STATUS_EQUAL(status
, NT_STATUS_IO_TIMEOUT
)) {
330 torture_assert_ntstatus_ok(tctx
, status
,
331 talloc_asprintf(tctx
, "No response from %s for name register\n",
334 torture_assert_ntstatus_ok(tctx
, status
,
335 talloc_asprintf(tctx
, "Bad response from %s for name register\n",
338 CHECK_STRING(tctx
, io
.out
.wins_server
, address
);
339 CHECK_VALUE(tctx
, io
.out
.rcode
, 0);
342 torture_comment(tctx
, "refresh the name with the correct address\n");
343 refresh
.in
.name
= *name
;
344 refresh
.in
.wins_port
= lp_nbt_port(tctx
->lp_ctx
);
345 refresh
.in
.wins_servers
= const_str_list(
346 str_list_make_single(tctx
, address
));
347 refresh
.in
.addresses
= const_str_list(
348 str_list_make_single(tctx
, myaddress
));
349 refresh
.in
.nb_flags
= nb_flags
;
350 refresh
.in
.ttl
= 12345;
352 status
= nbt_name_refresh_wins(nbtsock
, tctx
, &refresh
);
353 if (NT_STATUS_EQUAL(status
, NT_STATUS_IO_TIMEOUT
)) {
354 torture_assert_ntstatus_ok(tctx
, status
,
355 talloc_asprintf(tctx
, "No response from %s for name refresh",
358 torture_assert_ntstatus_ok(tctx
, status
,
359 talloc_asprintf(tctx
, "Bad response from %s for name refresh",
362 CHECK_STRING(tctx
, refresh
.out
.wins_server
, address
);
363 CHECK_VALUE(tctx
, refresh
.out
.rcode
, 0);
365 torture_comment(tctx
, "release the name\n");
366 release
.in
.name
= *name
;
367 release
.in
.dest_port
= lp_nbt_port(tctx
->lp_ctx
);
368 release
.in
.dest_addr
= address
;
369 release
.in
.address
= myaddress
;
370 release
.in
.nb_flags
= nb_flags
;
371 release
.in
.broadcast
= false;
372 release
.in
.timeout
= 3;
373 release
.in
.retries
= 0;
375 status
= nbt_name_release(nbtsock
, tctx
, &release
);
376 torture_assert_ntstatus_ok(tctx
, status
, talloc_asprintf(tctx
, "Bad response from %s for name query", address
));
378 CHECK_NAME(tctx
, release
.out
.name
, *name
);
379 CHECK_VALUE(tctx
, release
.out
.rcode
, 0);
381 torture_comment(tctx
, "release again\n");
382 status
= nbt_name_release(nbtsock
, tctx
, &release
);
383 torture_assert_ntstatus_ok(tctx
, status
,
384 talloc_asprintf(tctx
, "Bad response from %s for name query",
387 CHECK_NAME(tctx
, release
.out
.name
, *name
);
388 CHECK_VALUE(tctx
, release
.out
.rcode
, 0);
391 torture_comment(tctx
, "query the name to make sure its gone\n");
392 query
.in
.name
= *name
;
393 status
= nbt_name_query(nbtsock
, tctx
, &query
);
394 if (name
->type
!= NBT_NAME_LOGON
&&
395 (nb_flags
& NBT_NM_GROUP
)) {
396 torture_assert_ntstatus_ok(tctx
, status
,
397 "ERROR: Name query failed after group release");
399 torture_assert_ntstatus_equal(tctx
, status
,
400 NT_STATUS_OBJECT_NAME_NOT_FOUND
,
401 "Incorrect response to name query");
408 static char *test_nbt_wins_scope_string(TALLOC_CTX
*mem_ctx
, uint8_t count
)
413 res
= talloc_array(mem_ctx
, char, count
+1);
418 for (i
=0; i
< count
; i
++) {
422 case 63 + 1 + 63 + 1 + 63:
426 res
[i
] = '0' + (i
%10);
433 talloc_set_name_const(res
, res
);
439 test operations against a WINS server
441 static bool nbt_test_wins(struct torture_context
*tctx
)
443 struct nbt_name name
;
444 uint32_t r
= (uint32_t)(random() % (100000));
448 if (!torture_nbt_get_name(tctx
, &name
, &address
))
451 name
.name
= talloc_asprintf(tctx
, "_TORTURE-%5u", r
);
453 name
.type
= NBT_NAME_CLIENT
;
455 ret
&= nbt_test_wins_name(tctx
, address
, &name
,
456 NBT_NODE_H
, true, NBT_RCODE_OK
);
458 name
.type
= NBT_NAME_MASTER
;
459 ret
&= nbt_test_wins_name(tctx
, address
, &name
,
460 NBT_NODE_H
, false, NBT_RCODE_OK
);
462 ret
&= nbt_test_wins_name(tctx
, address
, &name
,
463 NBT_NODE_H
| NBT_NM_GROUP
, false, NBT_RCODE_OK
);
465 name
.type
= NBT_NAME_SERVER
;
466 ret
&= nbt_test_wins_name(tctx
, address
, &name
,
467 NBT_NODE_H
, true, NBT_RCODE_OK
);
469 name
.type
= NBT_NAME_LOGON
;
470 ret
&= nbt_test_wins_name(tctx
, address
, &name
,
471 NBT_NODE_H
| NBT_NM_GROUP
, false, NBT_RCODE_OK
);
473 name
.type
= NBT_NAME_BROWSER
;
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_PDC
;
478 ret
&= nbt_test_wins_name(tctx
, address
, &name
,
479 NBT_NODE_H
, true, NBT_RCODE_OK
);
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
, false, NBT_RCODE_OK
);
489 name
.scope
= "example";
491 ret
&= nbt_test_wins_name(tctx
, address
, &name
,
492 NBT_NODE_H
, true, NBT_RCODE_OK
);
494 name
.scope
= "example";
496 ret
&= nbt_test_wins_name(tctx
, address
, &name
,
497 NBT_NODE_H
| NBT_NM_GROUP
, false, NBT_RCODE_OK
);
499 name
.scope
= "foo.example.com";
501 ret
&= nbt_test_wins_name(tctx
, address
, &name
,
502 NBT_NODE_H
, false, NBT_RCODE_OK
);
504 name
.name
= talloc_asprintf(tctx
, "_T\01-%5u.foo", r
);
505 ret
&= nbt_test_wins_name(tctx
, address
, &name
,
506 NBT_NODE_H
, false, NBT_RCODE_OK
);
509 ret
&= nbt_test_wins_name(tctx
, address
, &name
,
510 NBT_NODE_H
, false, NBT_RCODE_OK
);
512 name
.name
= talloc_asprintf(tctx
, ".");
513 ret
&= nbt_test_wins_name(tctx
, address
, &name
,
514 NBT_NODE_H
, false, NBT_RCODE_OK
);
516 name
.name
= talloc_asprintf(tctx
, "%5u-\377\200\300FOO", r
);
517 ret
&= nbt_test_wins_name(tctx
, address
, &name
,
518 NBT_NODE_H
, false, NBT_RCODE_OK
);
520 name
.scope
= test_nbt_wins_scope_string(tctx
, 237);
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
, 238);
525 ret
&= nbt_test_wins_name(tctx
, address
, &name
,
526 NBT_NODE_H
, false, NBT_RCODE_SVR
);
534 struct torture_suite
*torture_nbt_wins(TALLOC_CTX
*mem_ctx
)
536 struct torture_suite
*suite
= torture_suite_create(mem_ctx
, "WINS");
538 torture_suite_add_simple_test(suite
, "wins", nbt_test_wins
);