NBT-WINS: if we're bound to port 137 trigger WACK reponses with timeouts
[kai/samba-autobuild/.git] / source4 / torture / nbt / wins.c
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/socket/socket.h"
24 #include "libcli/resolve/resolve.h"
25 #include "system/network.h"
26 #include "lib/socket/netif.h"
27 #include "librpc/gen_ndr/ndr_nbt.h"
28 #include "torture/torture.h"
29 #include "torture/nbt/proto.h"
30 #include "param/param.h"
31
32 #define CHECK_VALUE(tctx, v, correct) \
33         torture_assert_int_equal(tctx, v, correct, "Incorrect value")
34
35 #define CHECK_STRING(tctx, v, correct) \
36         torture_assert_casestr_equal(tctx, v, correct, "Incorrect value")
37
38 #define CHECK_NAME(tctx, _name, correct) do { \
39         CHECK_STRING(tctx, (_name).name, (correct).name); \
40         CHECK_VALUE(tctx, (uint8_t)(_name).type, (uint8_t)(correct).type); \
41         CHECK_STRING(tctx, (_name).scope, (correct).scope); \
42 } while (0)
43
44
45 /*
46   test operations against a WINS server
47 */
48 static bool nbt_test_wins_name(struct torture_context *tctx, const char *address,
49                                struct nbt_name *name, uint16_t nb_flags)
50 {
51         struct nbt_name_register_wins io;
52         struct nbt_name_query query;
53         struct nbt_name_refresh_wins refresh;
54         struct nbt_name_release release;
55         NTSTATUS status;
56         struct nbt_name_socket *nbtsock = torture_init_nbt_socket(tctx);
57         const char *myaddress;
58         struct socket_address *socket_address;
59         struct interface *ifaces;
60         bool low_port = true;
61
62         load_interfaces(tctx, lp_interfaces(tctx->lp_ctx), &ifaces);
63
64         myaddress = talloc_strdup(tctx, iface_best_ip(ifaces, address));
65
66         socket_address = socket_address_from_strings(tctx, 
67                                                      nbtsock->sock->backend_name,
68                                                      myaddress, lp_nbt_port(tctx->lp_ctx));
69         torture_assert(tctx, socket_address != NULL, 
70                                    "Error getting address");
71
72         /* we do the listen here to ensure the WINS server receives the packets from
73            the right IP */
74         status = socket_listen(nbtsock->sock, socket_address, 0, 0);
75         talloc_free(socket_address);
76         if (!NT_STATUS_IS_OK(status)) {
77                 low_port = false;
78                 socket_address = socket_address_from_strings(tctx,
79                                                              nbtsock->sock->backend_name,
80                                                              myaddress, 0);
81                 torture_assert(tctx, socket_address != NULL,
82                                "Error getting address");
83
84                 status = socket_listen(nbtsock->sock, socket_address, 0, 0);
85                 talloc_free(socket_address);
86                 torture_assert_ntstatus_ok(tctx, status,
87                                            "socket_listen for WINS failed");
88         }
89
90         torture_comment(tctx, "Testing name registration to WINS with name %s at %s nb_flags=0x%x\n", 
91                nbt_name_string(tctx, name), myaddress, nb_flags);
92
93         torture_comment(tctx, "release the name\n");
94         release.in.name = *name;
95         release.in.dest_port = lp_nbt_port(tctx->lp_ctx);
96         release.in.dest_addr = address;
97         release.in.address = myaddress;
98         release.in.nb_flags = nb_flags;
99         release.in.broadcast = false;
100         release.in.timeout = 3;
101         release.in.retries = 0;
102
103         status = nbt_name_release(nbtsock, tctx, &release);
104         torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address));
105         CHECK_VALUE(tctx, release.out.rcode, 0);
106
107         if (nb_flags & NBT_NM_GROUP) {
108                 /* ignore this for group names */
109         } else if (!low_port) {
110                 torture_comment(tctx, "no low port - skip: register the name with a wrong address\n");
111         } else {
112                 torture_comment(tctx, "register the name with a wrong address (makes the next request slow!)\n");
113                 io.in.name = *name;
114                 io.in.wins_port = lp_nbt_port(tctx->lp_ctx);
115                 io.in.wins_servers = str_list_make(tctx, address, NULL);
116                 io.in.addresses = str_list_make(tctx, "127.64.64.1", NULL);
117                 io.in.nb_flags = nb_flags;
118                 io.in.ttl = 300000;
119
120                 status = nbt_name_register_wins(nbtsock, tctx, &io);
121                 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
122                         torture_assert_ntstatus_ok(tctx, status,
123                                 talloc_asprintf(tctx, "No response from %s for name register\n",
124                                                 address));
125                 }
126                 torture_assert_ntstatus_ok(tctx, status,
127                         talloc_asprintf(tctx, "Bad response from %s for name register\n",
128                                         address));
129
130                 CHECK_STRING(tctx, io.out.wins_server, address);
131                 CHECK_VALUE(tctx, io.out.rcode, 0);
132         }
133
134         torture_comment(tctx, "register the name correct address\n");
135         io.in.name = *name;
136         io.in.wins_port = lp_nbt_port(tctx->lp_ctx);
137         io.in.wins_servers = (const char **)str_list_make(tctx, address, NULL);
138         io.in.addresses = (const char **)str_list_make(tctx, myaddress, NULL);
139         io.in.nb_flags = nb_flags;
140         io.in.ttl = 300000;
141         
142         status = nbt_name_register_wins(nbtsock, tctx, &io);
143         torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name register", address));
144         
145         CHECK_STRING(tctx, io.out.wins_server, address);
146         CHECK_VALUE(tctx, io.out.rcode, 0);
147
148         if (name->type != NBT_NAME_MASTER &&
149             name->type != NBT_NAME_LOGON && 
150             name->type != NBT_NAME_BROWSER && 
151             (nb_flags & NBT_NM_GROUP)) {
152                 torture_comment(tctx, "Try to register as non-group\n");
153                 io.in.nb_flags &= ~NBT_NM_GROUP;
154                 status = nbt_name_register_wins(nbtsock, tctx, &io);
155                 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name register\n",
156                         address));
157                 CHECK_VALUE(tctx, io.out.rcode, NBT_RCODE_ACT);
158         }
159
160         torture_comment(tctx, "query the name to make sure its there\n");
161         query.in.name = *name;
162         query.in.dest_addr = address;
163         query.in.dest_port = lp_nbt_port(tctx->lp_ctx);
164         query.in.broadcast = false;
165         query.in.wins_lookup = true;
166         query.in.timeout = 3;
167         query.in.retries = 0;
168
169         status = nbt_name_query(nbtsock, tctx, &query);
170         if (name->type == NBT_NAME_MASTER) {
171                 torture_assert_ntstatus_equal(
172                           tctx, status, NT_STATUS_OBJECT_NAME_NOT_FOUND, 
173                           talloc_asprintf(tctx, "Bad response from %s for name query", address));
174                 return true;
175         }
176         torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address));
177         
178         CHECK_NAME(tctx, query.out.name, *name);
179         CHECK_VALUE(tctx, query.out.num_addrs, 1);
180         if (name->type != NBT_NAME_LOGON &&
181             (nb_flags & NBT_NM_GROUP)) {
182                 CHECK_STRING(tctx, query.out.reply_addrs[0], "255.255.255.255");
183         } else {
184                 CHECK_STRING(tctx, query.out.reply_addrs[0], myaddress);
185         }
186
187
188         query.in.name.name = strupper_talloc(tctx, name->name);
189         if (query.in.name.name &&
190             strcmp(query.in.name.name, name->name) != 0) {
191                 torture_comment(tctx, "check case sensitivity\n");
192                 status = nbt_name_query(nbtsock, tctx, &query);
193                 torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OBJECT_NAME_NOT_FOUND, talloc_asprintf(tctx, "Bad response from %s for name query", address));
194         }
195
196         query.in.name = *name;
197         if (name->scope) {
198                 query.in.name.scope = strupper_talloc(tctx, name->scope);
199         }
200         if (query.in.name.scope &&
201             strcmp(query.in.name.scope, name->scope) != 0) {
202                 torture_comment(tctx, "check case sensitivity on scope\n");
203                 status = nbt_name_query(nbtsock, tctx, &query);
204                 torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OBJECT_NAME_NOT_FOUND, talloc_asprintf(tctx, "Bad response from %s for name query", address));
205         }
206
207         torture_comment(tctx, "refresh the name\n");
208         refresh.in.name = *name;
209         refresh.in.wins_port = lp_nbt_port(tctx->lp_ctx);
210         refresh.in.wins_servers = (const char **)str_list_make(tctx, address, NULL);
211         refresh.in.addresses = (const char **)str_list_make(tctx, myaddress, NULL);
212         refresh.in.nb_flags = nb_flags;
213         refresh.in.ttl = 12345;
214         
215         status = nbt_name_refresh_wins(nbtsock, tctx, &refresh);
216         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
217                 torture_assert_ntstatus_ok(tctx, status,
218                         talloc_asprintf(tctx, "No response from %s for name refresh",
219                                         address));
220         }
221         torture_assert_ntstatus_ok(tctx, status,
222                 talloc_asprintf(tctx, "Bad response from %s for name refresh",
223                                 address));
224
225         CHECK_STRING(tctx, refresh.out.wins_server, address);
226         CHECK_VALUE(tctx, refresh.out.rcode, 0);
227
228         printf("release the name\n");
229         release.in.name = *name;
230         release.in.dest_port = lp_nbt_port(tctx->lp_ctx);
231         release.in.dest_addr = address;
232         release.in.address = myaddress;
233         release.in.nb_flags = nb_flags;
234         release.in.broadcast = false;
235         release.in.timeout = 3;
236         release.in.retries = 0;
237
238         status = nbt_name_release(nbtsock, tctx, &release);
239         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
240                 torture_assert_ntstatus_ok(tctx, status,
241                         talloc_asprintf(tctx, "No response from %s for name release",
242                                         address));
243         }
244         torture_assert_ntstatus_ok(tctx, status,
245                 talloc_asprintf(tctx, "Bad response from %s for name release",
246                                 address));
247
248         CHECK_NAME(tctx, release.out.name, *name);
249         CHECK_VALUE(tctx, release.out.rcode, 0);
250
251         if (nb_flags & NBT_NM_GROUP) {
252                 /* ignore this for group names */
253         } else if (!low_port) {
254                 torture_comment(tctx, "no low port - skip: register the name with a wrong address\n");
255         } else {
256                 torture_comment(tctx, "register the name with a wrong address (makes the next request slow!)\n");
257                 io.in.name = *name;
258                 io.in.wins_port = lp_nbt_port(tctx->lp_ctx);
259                 io.in.wins_servers = str_list_make(tctx, address, NULL);
260                 io.in.addresses = str_list_make(tctx, "127.64.64.1", NULL);
261                 io.in.nb_flags = nb_flags;
262                 io.in.ttl = 300000;
263         
264                 status = nbt_name_register_wins(nbtsock, tctx, &io);
265                 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
266                         torture_assert_ntstatus_ok(tctx, status,
267                                 talloc_asprintf(tctx, "No response from %s for name register\n",
268                                                 address));
269                 }
270                 torture_assert_ntstatus_ok(tctx, status,
271                         talloc_asprintf(tctx, "Bad response from %s for name register\n",
272                                         address));
273
274                 CHECK_STRING(tctx, io.out.wins_server, address);
275                 CHECK_VALUE(tctx, io.out.rcode, 0);
276         }
277
278         torture_comment(tctx, "refresh the name with the correct address\n");
279         refresh.in.name = *name;
280         refresh.in.wins_port = lp_nbt_port(tctx->lp_ctx);
281         refresh.in.wins_servers = str_list_make(tctx, address, NULL);
282         refresh.in.addresses = str_list_make(tctx, myaddress, NULL);
283         refresh.in.nb_flags = nb_flags;
284         refresh.in.ttl = 12345;
285
286         status = nbt_name_refresh_wins(nbtsock, tctx, &refresh);
287         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
288                 torture_assert_ntstatus_ok(tctx, status,
289                         talloc_asprintf(tctx, "No response from %s for name refresh",
290                                         address));
291         }
292         torture_assert_ntstatus_ok(tctx, status,
293                 talloc_asprintf(tctx, "Bad response from %s for name refresh",
294                                 address));
295
296         CHECK_STRING(tctx, refresh.out.wins_server, address);
297         CHECK_VALUE(tctx, refresh.out.rcode, 0);
298
299         torture_comment(tctx, "release the name\n");
300         release.in.name = *name;
301         release.in.dest_port = lp_nbt_port(tctx->lp_ctx);
302         release.in.dest_addr = address;
303         release.in.address = myaddress;
304         release.in.nb_flags = nb_flags;
305         release.in.broadcast = false;
306         release.in.timeout = 3;
307         release.in.retries = 0;
308
309         status = nbt_name_release(nbtsock, tctx, &release);
310         torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address));
311         
312         CHECK_NAME(tctx, release.out.name, *name);
313         CHECK_VALUE(tctx, release.out.rcode, 0);
314
315         torture_comment(tctx, "release again\n");
316         status = nbt_name_release(nbtsock, tctx, &release);
317         torture_assert_ntstatus_ok(tctx, status, 
318                                 talloc_asprintf(tctx, "Bad response from %s for name query",
319                        address));
320         
321         CHECK_NAME(tctx, release.out.name, *name);
322         CHECK_VALUE(tctx, release.out.rcode, 0);
323
324
325         torture_comment(tctx, "query the name to make sure its gone\n");
326         query.in.name = *name;
327         status = nbt_name_query(nbtsock, tctx, &query);
328         if (name->type != NBT_NAME_LOGON &&
329             (nb_flags & NBT_NM_GROUP)) {
330                 torture_assert_ntstatus_ok(tctx, status, 
331                                 "ERROR: Name query failed after group release");
332         } else {
333                 torture_assert_ntstatus_equal(tctx, status, 
334                                                                           NT_STATUS_OBJECT_NAME_NOT_FOUND,
335                                 "Incorrect response to name query");
336         }
337         
338         return true;
339 }
340
341
342
343 /*
344   test operations against a WINS server
345 */
346 static bool nbt_test_wins(struct torture_context *tctx)
347 {
348         struct nbt_name name;
349         uint32_t r = (uint32_t)(random() % (100000));
350         const char *address;
351         bool ret = true;
352
353         if (!torture_nbt_get_name(tctx, &name, &address))
354                 return false;
355
356         name.name = talloc_asprintf(tctx, "_TORTURE-%5u", r);
357
358         name.type = NBT_NAME_CLIENT;
359         name.scope = NULL;
360         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
361
362         name.type = NBT_NAME_MASTER;
363         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
364
365         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
366
367         name.type = NBT_NAME_SERVER;
368         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
369
370         name.type = NBT_NAME_LOGON;
371         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
372
373         name.type = NBT_NAME_BROWSER;
374         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
375
376         name.type = NBT_NAME_PDC;
377         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
378
379         name.type = 0xBF;
380         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
381
382         name.type = 0xBE;
383         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
384
385         name.scope = "example";
386         name.type = 0x72;
387         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
388
389         name.scope = "example";
390         name.type = 0x71;
391         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
392
393         name.scope = "foo.example.com";
394         name.type = 0x72;
395         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
396
397         name.name = talloc_asprintf(tctx, "_T\01-%5u.foo", r);
398         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
399
400         name.name = "";
401         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
402
403         name.name = talloc_asprintf(tctx, ".");
404         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
405
406         name.name = talloc_asprintf(tctx, "%5u-\377\200\300FOO", r);
407         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
408
409         return ret;
410 }
411
412 /*
413   test WINS operations
414 */
415 struct torture_suite *torture_nbt_wins(TALLOC_CTX *mem_ctx)
416 {
417         struct torture_suite *suite = torture_suite_create(mem_ctx, "WINS");
418
419         torture_suite_add_simple_test(suite, "wins", nbt_test_wins);
420
421         return suite;
422 }