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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "lib/socket/socket.h"
25 #include "libcli/resolve/resolve.h"
26 #include "system/network.h"
27 #include "lib/socket/netif.h"
28 #include "librpc/gen_ndr/ndr_nbt.h"
29 #include "torture/torture.h"
31 #define CHECK_VALUE(v, correct) do { \
32 if ((v) != (correct)) { \
33 printf("(%s) Incorrect value %s=%d (0x%X) - should be %d (0x%X)\n", \
34 __location__, #v, v, v, correct, correct); \
38 #define CHECK_STRING(v, correct) do { \
39 if ((v) != (correct) && \
40 ((v)==NULL || (correct)==NULL || strcasecmp_m(v, correct) != 0)) { \
41 printf("(%s) Incorrect value %s='%s' - should be '%s'\n", \
42 __location__, #v, v, correct); \
46 #define CHECK_NAME(_name, correct) do { \
47 CHECK_STRING((_name).name, (correct).name); \
48 CHECK_VALUE((uint8_t)(_name).type, (uint8_t)(correct).type); \
49 CHECK_STRING((_name).scope, (correct).scope); \
54 test operations against a WINS server
56 static BOOL nbt_test_wins_name(TALLOC_CTX *mem_ctx, const char *address,
57 struct nbt_name *name, uint16_t nb_flags)
59 struct nbt_name_register_wins io;
60 struct nbt_name_query query;
61 struct nbt_name_refresh_wins refresh;
62 struct nbt_name_release release;
64 struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL);
66 const char *myaddress = talloc_strdup(mem_ctx, iface_best_ip(address));
67 struct socket_address *socket_address;
69 socket_address = socket_address_from_strings(mem_ctx,
70 nbtsock->sock->backend_name,
72 if (!socket_address) {
76 /* we do the listen here to ensure the WINS server receives the packets from
78 status = socket_listen(nbtsock->sock, socket_address, 0, 0);
79 if (!NT_STATUS_IS_OK(status)) {
80 printf("socket_listen for WINS failed: %s\n", nt_errstr(status));
83 talloc_free(socket_address);
85 printf("Testing name registration to WINS with name %s at %s nb_flags=0x%x\n",
86 nbt_name_string(mem_ctx, name), myaddress, nb_flags);
88 printf("release the name\n");
89 release.in.name = *name;
90 release.in.dest_addr = address;
91 release.in.address = myaddress;
92 release.in.nb_flags = nb_flags;
93 release.in.broadcast = False;
94 release.in.timeout = 3;
95 release.in.retries = 0;
97 status = nbt_name_release(nbtsock, mem_ctx, &release);
98 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
99 printf("No response from %s for name release\n", address);
102 if (!NT_STATUS_IS_OK(status)) {
103 printf("Bad response from %s for name query - %s\n",
104 address, nt_errstr(status));
107 CHECK_VALUE(release.out.rcode, 0);
109 printf("register the name\n");
111 io.in.wins_servers = str_list_make(mem_ctx, address, NULL);
112 io.in.addresses = str_list_make(mem_ctx, myaddress, NULL);
113 io.in.nb_flags = nb_flags;
116 status = nbt_name_register_wins(nbtsock, mem_ctx, &io);
117 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
118 printf("No response from %s for name register\n", address);
121 if (!NT_STATUS_IS_OK(status)) {
122 printf("Bad response from %s for name register - %s\n",
123 address, nt_errstr(status));
127 CHECK_STRING(io.out.wins_server, address);
128 CHECK_VALUE(io.out.rcode, 0);
130 if (name->type != NBT_NAME_MASTER &&
131 name->type != NBT_NAME_LOGON &&
132 name->type != NBT_NAME_BROWSER &&
133 (nb_flags & NBT_NM_GROUP)) {
134 printf("Try to register as non-group\n");
135 io.in.nb_flags &= ~NBT_NM_GROUP;
136 status = nbt_name_register_wins(nbtsock, mem_ctx, &io);
137 if (!NT_STATUS_IS_OK(status)) {
138 printf("Bad response from %s for name register - %s\n",
139 address, nt_errstr(status));
142 CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
145 printf("query the name to make sure its there\n");
146 query.in.name = *name;
147 query.in.dest_addr = address;
148 query.in.broadcast = False;
149 query.in.wins_lookup = True;
150 query.in.timeout = 3;
151 query.in.retries = 0;
153 status = nbt_name_query(nbtsock, mem_ctx, &query);
154 if (name->type == NBT_NAME_MASTER) {
155 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
156 printf("Bad response from %s for name query - %s\n",
157 address, nt_errstr(status));
162 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
163 printf("No response from %s for name query\n", address);
166 if (!NT_STATUS_IS_OK(status)) {
167 printf("Bad response from %s for name query - %s\n",
168 address, nt_errstr(status));
172 CHECK_NAME(query.out.name, *name);
173 CHECK_VALUE(query.out.num_addrs, 1);
174 if (name->type != NBT_NAME_LOGON &&
175 (nb_flags & NBT_NM_GROUP)) {
176 CHECK_STRING(query.out.reply_addrs[0], "255.255.255.255");
178 CHECK_STRING(query.out.reply_addrs[0], myaddress);
182 query.in.name.name = strupper_talloc(mem_ctx, name->name);
183 if (query.in.name.name &&
184 strcmp(query.in.name.name, name->name) != 0) {
185 printf("check case sensitivity\n");
186 status = nbt_name_query(nbtsock, mem_ctx, &query);
187 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
188 printf("No response from %s for name query\n", address);
191 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
192 printf("Bad response from %s for name query - %s\n",
193 address, nt_errstr(status));
198 query.in.name = *name;
200 query.in.name.scope = strupper_talloc(mem_ctx, name->scope);
202 if (query.in.name.scope &&
203 strcmp(query.in.name.scope, name->scope) != 0) {
204 printf("check case sensitivity on scope\n");
205 status = nbt_name_query(nbtsock, mem_ctx, &query);
206 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
207 printf("No response from %s for name query\n", address);
210 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
211 printf("Bad response from %s for name query - %s\n",
212 address, nt_errstr(status));
217 printf("refresh the name\n");
218 refresh.in.name = *name;
219 refresh.in.wins_servers = str_list_make(mem_ctx, address, NULL);
220 refresh.in.addresses = str_list_make(mem_ctx, myaddress, NULL);
221 refresh.in.nb_flags = nb_flags;
222 refresh.in.ttl = 12345;
224 status = nbt_name_refresh_wins(nbtsock, mem_ctx, &refresh);
225 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
226 printf("No response from %s for name refresh\n", address);
229 if (!NT_STATUS_IS_OK(status)) {
230 printf("Bad response from %s for name refresh - %s\n",
231 address, nt_errstr(status));
235 CHECK_STRING(refresh.out.wins_server, address);
236 CHECK_VALUE(refresh.out.rcode, 0);
238 printf("release the name\n");
239 release.in.name = *name;
240 release.in.dest_addr = address;
241 release.in.address = myaddress;
242 release.in.nb_flags = nb_flags;
243 release.in.broadcast = False;
244 release.in.timeout = 3;
245 release.in.retries = 0;
247 status = nbt_name_release(nbtsock, mem_ctx, &release);
248 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
249 printf("No response from %s for name release\n", address);
252 if (!NT_STATUS_IS_OK(status)) {
253 printf("Bad response from %s for name query - %s\n",
254 address, nt_errstr(status));
258 CHECK_NAME(release.out.name, *name);
259 CHECK_VALUE(release.out.rcode, 0);
262 printf("release again\n");
263 status = nbt_name_release(nbtsock, mem_ctx, &release);
264 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
265 printf("No response from %s for name release\n", address);
268 if (!NT_STATUS_IS_OK(status)) {
269 printf("Bad response from %s for name query - %s\n",
270 address, nt_errstr(status));
274 CHECK_NAME(release.out.name, *name);
275 CHECK_VALUE(release.out.rcode, 0);
278 printf("query the name to make sure its gone\n");
279 query.in.name = *name;
280 status = nbt_name_query(nbtsock, mem_ctx, &query);
281 if (name->type != NBT_NAME_LOGON &&
282 (nb_flags & NBT_NM_GROUP)) {
283 if (!NT_STATUS_IS_OK(status)) {
284 printf("ERROR: Name query failed after group release - %s\n",
289 if (NT_STATUS_IS_OK(status)) {
290 printf("ERROR: Name query success after release\n");
293 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
294 printf("Incorrect response to name query - %s\n", nt_errstr(status));
305 test operations against a WINS server
307 static BOOL nbt_test_wins(TALLOC_CTX *mem_ctx, const char *address)
309 struct nbt_name name;
311 uint32_t r = (uint32_t)(random() % (100000));
313 name.name = talloc_asprintf(mem_ctx, "_TORTURE-%5u", r);
315 name.type = NBT_NAME_CLIENT;
317 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
319 name.type = NBT_NAME_MASTER;
320 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
322 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
324 name.type = NBT_NAME_SERVER;
325 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
327 name.type = NBT_NAME_LOGON;
328 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
330 name.type = NBT_NAME_BROWSER;
331 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
333 name.type = NBT_NAME_PDC;
334 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
337 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
340 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
342 name.scope = "example";
344 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
346 name.scope = "example";
348 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
350 name.scope = "foo.example.com";
352 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
354 name.name = talloc_asprintf(mem_ctx, "_T\01-%5u.foo", r);
355 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
358 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
360 name.name = talloc_asprintf(mem_ctx, ".");
361 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
363 name.name = talloc_asprintf(mem_ctx, "%5u-\377\200\300FOO", r);
364 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
372 BOOL torture_nbt_wins(struct torture_context *torture)
375 struct nbt_name name;
376 TALLOC_CTX *mem_ctx = talloc_new(NULL);
380 make_nbt_name_server(&name, lp_parm_string(-1, "torture", "host"));
382 /* do an initial name resolution to find its IP */
383 status = resolve_name(&name, mem_ctx, &address, NULL);
384 if (!NT_STATUS_IS_OK(status)) {
385 printf("Failed to resolve %s - %s\n",
386 name.name, nt_errstr(status));
387 talloc_free(mem_ctx);
391 ret &= nbt_test_wins(mem_ctx, address);
393 talloc_free(mem_ctx);