1 /*
2    Unix SMB/CIFS implementation.
3 
4    NBT WINS server testing
5 
6    Copyright (C) Andrew Tridgell 2005
7 
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.
12 
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.
17 
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/>.
20 */
21 
22 #include "includes.h"
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"
33 
34 #define CHECK_VALUE(tctx, v, correct) \
35 	torture_assert_int_equal(tctx, v, correct, "Incorrect value")
36 
37 #define CHECK_STRING(tctx, v, correct) \
38 	torture_assert_casestr_equal(tctx, v, correct, "Incorrect value")
39 
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); \
44 } while (0)
45 
46 
47 /*
48   test operations against a WINS server
49 */
nbt_test_wins_name(struct torture_context * tctx,const char * address,struct nbt_name * name,uint16_t nb_flags,bool try_low_port,uint8_t register_rcode)50 static bool nbt_test_wins_name(struct torture_context *tctx, const char *address,
51 			       struct nbt_name *name, uint16_t nb_flags,
52 			       bool try_low_port,
53 			       uint8_t register_rcode)
54 {
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;
61 	NTSTATUS status;
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;
67 	char **l;
68 
69 	load_interface_list(tctx, tctx->lp_ctx, &ifaces);
70 
71 	myaddress = talloc_strdup(tctx, iface_list_best_ip(ifaces, address));
72 
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");
78 
79 	/* we do the listen here to ensure the WINS server receives the packets from
80 	   the right IP */
81 	status = socket_listen(nbtsock->sock, socket_address, 0, 0);
82 	talloc_free(socket_address);
83 	if (!NT_STATUS_IS_OK(status)) {
84 		low_port = false;
85 		socket_address = socket_address_from_strings(tctx,
86 							     nbtsock->sock->backend_name,
87 							     myaddress, 0);
88 		torture_assert(tctx, socket_address != NULL,
89 			       "Error getting address");
90 
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");
95 	}
96 
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);
99 
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;
109 
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);
113 
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");
118 	} else {
119 		torture_comment(tctx, "register the name with a wrong address (makes the next request slow!)\n");
120 		io.in.name = *name;
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;
127 		io.in.ttl = 300000;
128 
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",
133 						address));
134 		}
135 		torture_assert_ntstatus_ok(tctx, status,
136 			talloc_asprintf(tctx, "Bad response from %s for name register\n",
137 					address));
138 
139 		CHECK_STRING(tctx, io.out.wins_server, address);
140 		CHECK_VALUE(tctx, io.out.rcode, 0);
141 
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;
154 
155 		/*
156 		 * test if the server ignores resent requests
157 		 */
158 		req = nbt_name_register_send(nbtsock, &name_register);
159 		while (true) {
160 			tevent_loop_once(nbtsock->event_ctx);
161 			if (req->state != NBT_REQUEST_WAIT) {
162 				break;
163 			}
164 			if (req->received_wack) {
165 				/*
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
170 				 */
171 				req->state = NBT_REQUEST_SEND;
172 				DLIST_ADD_END(nbtsock->send_queue, req);
173 				TEVENT_FD_WRITEABLE(nbtsock->fde);
174 				break;
175 			}
176 		}
177 
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",
182 						address));
183 		}
184 		torture_assert_ntstatus_ok(tctx, status,
185 			talloc_asprintf(tctx, "Bad response from %s for name register\n",
186 					address));
187 
188 		CHECK_VALUE(tctx, name_register.out.rcode, 0);
189 		CHECK_STRING(tctx, name_register.out.reply_addr, myaddress);
190 	}
191 
192 	torture_comment(tctx, "register the name correct address\n");
193 	io.in.name = *name;
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;
200 	io.in.ttl = 300000;
201 
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));
204 
205 	CHECK_STRING(tctx, io.out.wins_server, address);
206 	CHECK_VALUE(tctx, io.out.rcode, register_rcode);
207 
208 	if (register_rcode != NBT_RCODE_OK) {
209 		return true;
210 	}
211 
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",
220 			address));
221 		CHECK_VALUE(tctx, io.out.rcode, NBT_RCODE_ACT);
222 	}
223 
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;
232 
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));
238 		return true;
239 	}
240 	torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address));
241 
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");
247 	} else {
248 		CHECK_STRING(tctx, query.out.reply_addrs[0], myaddress);
249 	}
250 
251 
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));
258 	}
259 
260 	query.in.name = *name;
261 	if (name->scope) {
262 		query.in.name.scope = strupper_talloc(tctx, name->scope);
263 	}
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));
269 	}
270 
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;
280 
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",
285 					address));
286 	}
287 	torture_assert_ntstatus_ok(tctx, status,
288 		talloc_asprintf(tctx, "Bad response from %s for name refresh",
289 				address));
290 
291 	CHECK_STRING(tctx, refresh.out.wins_server, address);
292 	CHECK_VALUE(tctx, refresh.out.rcode, 0);
293 
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;
303 
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",
308 					address));
309 	}
310 	torture_assert_ntstatus_ok(tctx, status,
311 		talloc_asprintf(tctx, "Bad response from %s for name release",
312 				address));
313 
314 	CHECK_NAME(tctx, release.out.name, *name);
315 	CHECK_VALUE(tctx, release.out.rcode, 0);
316 
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");
321 	} else {
322 		torture_comment(tctx, "register the name with a wrong address (makes the next request slow!)\n");
323 		io.in.name = *name;
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;
330 		io.in.ttl = 300000;
331 
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",
336 						address));
337 		}
338 		torture_assert_ntstatus_ok(tctx, status,
339 			talloc_asprintf(tctx, "Bad response from %s for name register\n",
340 					address));
341 
342 		CHECK_STRING(tctx, io.out.wins_server, address);
343 		CHECK_VALUE(tctx, io.out.rcode, 0);
344 	}
345 
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;
355 
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",
360 					address));
361 	}
362 	torture_assert_ntstatus_ok(tctx, status,
363 		talloc_asprintf(tctx, "Bad response from %s for name refresh",
364 				address));
365 
366 	CHECK_STRING(tctx, refresh.out.wins_server, address);
367 	CHECK_VALUE(tctx, refresh.out.rcode, 0);
368 
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;
378 
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));
381 
382 	CHECK_NAME(tctx, release.out.name, *name);
383 	CHECK_VALUE(tctx, release.out.rcode, 0);
384 
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",
389 		       address));
390 
391 	CHECK_NAME(tctx, release.out.name, *name);
392 	CHECK_VALUE(tctx, release.out.rcode, 0);
393 
394 
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");
402 	} else {
403 		torture_assert_ntstatus_equal(tctx, status,
404 									  NT_STATUS_OBJECT_NAME_NOT_FOUND,
405 				"Incorrect response to name query");
406 	}
407 
408 	return true;
409 }
410 
411 
test_nbt_wins_scope_string(TALLOC_CTX * mem_ctx,uint8_t count)412 static char *test_nbt_wins_scope_string(TALLOC_CTX *mem_ctx, uint8_t count)
413 {
414 	char *res;
415 	uint8_t i;
416 
417 	res = talloc_array(mem_ctx, char, count+1);
418 	if (res == NULL) {
419 		return NULL;
420 	}
421 
422 	for (i=0; i < count; i++) {
423 		switch (i) {
424 		case 63:
425 		case 63 + 1 + 63:
426 		case 63 + 1 + 63 + 1 + 63:
427 			res[i] = '.';
428 			break;
429 		default:
430 			res[i] = '0' + (i%10);
431 			break;
432 		}
433 	}
434 
435 	res[count] = '\0';
436 
437 	talloc_set_name_const(res, res);
438 
439 	return res;
440 }
441 
442 /*
443   test operations against a WINS server
444 */
nbt_test_wins(struct torture_context * tctx)445 static bool nbt_test_wins(struct torture_context *tctx)
446 {
447 	struct nbt_name name;
448 	uint32_t r = (uint32_t)(random() % (100000));
449 	const char *address;
450 	bool ret = true;
451 
452 	if (!torture_nbt_get_name(tctx, &name, &address))
453 		return false;
454 
455 	name.name = talloc_asprintf(tctx, "_TORTURE-%5u", r);
456 
457 	name.type = NBT_NAME_CLIENT;
458 	name.scope = NULL;
459 	ret &= nbt_test_wins_name(tctx, address, &name,
460 				  NBT_NODE_H, true, NBT_RCODE_OK);
461 
462 	name.type = NBT_NAME_MASTER;
463 	ret &= nbt_test_wins_name(tctx, address, &name,
464 				  NBT_NODE_H, false, NBT_RCODE_OK);
465 
466 	ret &= nbt_test_wins_name(tctx, address, &name,
467 				  NBT_NODE_H | NBT_NM_GROUP, false, NBT_RCODE_OK);
468 
469 	name.type = NBT_NAME_SERVER;
470 	ret &= nbt_test_wins_name(tctx, address, &name,
471 				  NBT_NODE_H, true, NBT_RCODE_OK);
472 
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);
476 
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);
480 
481 	name.type = NBT_NAME_PDC;
482 	ret &= nbt_test_wins_name(tctx, address, &name,
483 				  NBT_NODE_H, true, NBT_RCODE_OK);
484 
485 	name.type = 0xBF;
486 	ret &= nbt_test_wins_name(tctx, address, &name,
487 				  NBT_NODE_H, true, NBT_RCODE_OK);
488 
489 	name.type = 0xBE;
490 	ret &= nbt_test_wins_name(tctx, address, &name,
491 				  NBT_NODE_H, false, NBT_RCODE_OK);
492 
493 	name.scope = "example";
494 	name.type = 0x72;
495 	ret &= nbt_test_wins_name(tctx, address, &name,
496 				  NBT_NODE_H, true, NBT_RCODE_OK);
497 
498 	name.scope = "example";
499 	name.type = 0x71;
500 	ret &= nbt_test_wins_name(tctx, address, &name,
501 				  NBT_NODE_H | NBT_NM_GROUP, false, NBT_RCODE_OK);
502 
503 	name.scope = "foo.example.com";
504 	name.type = 0x72;
505 	ret &= nbt_test_wins_name(tctx, address, &name,
506 				  NBT_NODE_H, false, NBT_RCODE_OK);
507 
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);
511 
512 	name.name = "";
513 	ret &= nbt_test_wins_name(tctx, address, &name,
514 				  NBT_NODE_H, false, NBT_RCODE_OK);
515 
516 	name.name = talloc_asprintf(tctx, ".");
517 	ret &= nbt_test_wins_name(tctx, address, &name,
518 				  NBT_NODE_H, false, NBT_RCODE_OK);
519 
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);
523 
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);
527 
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);
531 
532 	return ret;
533 }
534 
535 /*
536   test WINS operations
537 */
torture_nbt_wins(TALLOC_CTX * mem_ctx)538 struct torture_suite *torture_nbt_wins(TALLOC_CTX *mem_ctx)
539 {
540 	struct torture_suite *suite = torture_suite_create(mem_ctx, "wins");
541 
542 	torture_suite_add_simple_test(suite, "wins", nbt_test_wins);
543 
544 	return suite;
545 }
546