s4:NBT-WINS: pass the expected rcode of the name registration to the test code
[metze/samba/wip.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/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 */
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
68         load_interfaces(tctx, lp_interfaces(tctx->lp_ctx), &ifaces);
69
70         myaddress = talloc_strdup(tctx, iface_best_ip(ifaces, address));
71
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");
77
78         /* we do the listen here to ensure the WINS server receives the packets from
79            the right IP */
80         status = socket_listen(nbtsock->sock, socket_address, 0, 0);
81         talloc_free(socket_address);
82         if (!NT_STATUS_IS_OK(status)) {
83                 low_port = false;
84                 socket_address = socket_address_from_strings(tctx,
85                                                              nbtsock->sock->backend_name,
86                                                              myaddress, 0);
87                 torture_assert(tctx, socket_address != NULL,
88                                "Error getting address");
89
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");
94         }
95
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);
98
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;
108
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);
112
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");
117         } else {
118                 torture_comment(tctx, "register the name with a wrong address (makes the next request slow!)\n");
119                 io.in.name = *name;
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;
126                 io.in.ttl = 300000;
127
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",
132                                                 address));
133                 }
134                 torture_assert_ntstatus_ok(tctx, status,
135                         talloc_asprintf(tctx, "Bad response from %s for name register\n",
136                                         address));
137
138                 CHECK_STRING(tctx, io.out.wins_server, address);
139                 CHECK_VALUE(tctx, io.out.rcode, 0);
140
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;
153
154                 /*
155                  * test if the server ignores resent requests
156                  */
157                 req = nbt_name_register_send(nbtsock, &name_register);
158                 while (true) {
159                         event_loop_once(nbtsock->event_ctx);
160                         if (req->state != NBT_REQUEST_WAIT) {
161                                 break;
162                         }
163                         if (req->received_wack) {
164                                 /*
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
169                                  */
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);
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 = 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;
198         io.in.ttl = 300000;
199         
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));
202         
203         CHECK_STRING(tctx, io.out.wins_server, address);
204         CHECK_VALUE(tctx, io.out.rcode, register_rcode);
205
206         if (register_rcode != NBT_RCODE_OK) {
207                 return true;
208         }
209
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",
218                         address));
219                 CHECK_VALUE(tctx, io.out.rcode, NBT_RCODE_ACT);
220         }
221
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;
230
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));
236                 return true;
237         }
238         torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address));
239         
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");
245         } else {
246                 CHECK_STRING(tctx, query.out.reply_addrs[0], myaddress);
247         }
248
249
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));
256         }
257
258         query.in.name = *name;
259         if (name->scope) {
260                 query.in.name.scope = strupper_talloc(tctx, name->scope);
261         }
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));
267         }
268
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;
276         
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",
281                                         address));
282         }
283         torture_assert_ntstatus_ok(tctx, status,
284                 talloc_asprintf(tctx, "Bad response from %s for name refresh",
285                                 address));
286
287         CHECK_STRING(tctx, refresh.out.wins_server, address);
288         CHECK_VALUE(tctx, refresh.out.rcode, 0);
289
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;
299
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",
304                                         address));
305         }
306         torture_assert_ntstatus_ok(tctx, status,
307                 talloc_asprintf(tctx, "Bad response from %s for name release",
308                                 address));
309
310         CHECK_NAME(tctx, release.out.name, *name);
311         CHECK_VALUE(tctx, release.out.rcode, 0);
312
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");
317         } else {
318                 torture_comment(tctx, "register the name with a wrong address (makes the next request slow!)\n");
319                 io.in.name = *name;
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;
326                 io.in.ttl = 300000;
327         
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",
332                                                 address));
333                 }
334                 torture_assert_ntstatus_ok(tctx, status,
335                         talloc_asprintf(tctx, "Bad response from %s for name register\n",
336                                         address));
337
338                 CHECK_STRING(tctx, io.out.wins_server, address);
339                 CHECK_VALUE(tctx, io.out.rcode, 0);
340         }
341
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;
351
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",
356                                         address));
357         }
358         torture_assert_ntstatus_ok(tctx, status,
359                 talloc_asprintf(tctx, "Bad response from %s for name refresh",
360                                 address));
361
362         CHECK_STRING(tctx, refresh.out.wins_server, address);
363         CHECK_VALUE(tctx, refresh.out.rcode, 0);
364
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;
374
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));
377         
378         CHECK_NAME(tctx, release.out.name, *name);
379         CHECK_VALUE(tctx, release.out.rcode, 0);
380
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",
385                        address));
386         
387         CHECK_NAME(tctx, release.out.name, *name);
388         CHECK_VALUE(tctx, release.out.rcode, 0);
389
390
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");
398         } else {
399                 torture_assert_ntstatus_equal(tctx, status, 
400                                                                           NT_STATUS_OBJECT_NAME_NOT_FOUND,
401                                 "Incorrect response to name query");
402         }
403         
404         return true;
405 }
406
407
408
409 /*
410   test operations against a WINS server
411 */
412 static bool nbt_test_wins(struct torture_context *tctx)
413 {
414         struct nbt_name name;
415         uint32_t r = (uint32_t)(random() % (100000));
416         const char *address;
417         bool ret = true;
418
419         if (!torture_nbt_get_name(tctx, &name, &address))
420                 return false;
421
422         name.name = talloc_asprintf(tctx, "_TORTURE-%5u", r);
423
424         name.type = NBT_NAME_CLIENT;
425         name.scope = NULL;
426         ret &= nbt_test_wins_name(tctx, address, &name,
427                                   NBT_NODE_H, true, NBT_RCODE_OK);
428
429         name.type = NBT_NAME_MASTER;
430         ret &= nbt_test_wins_name(tctx, address, &name,
431                                   NBT_NODE_H, false, NBT_RCODE_OK);
432
433         ret &= nbt_test_wins_name(tctx, address, &name,
434                                   NBT_NODE_H | NBT_NM_GROUP, false, NBT_RCODE_OK);
435
436         name.type = NBT_NAME_SERVER;
437         ret &= nbt_test_wins_name(tctx, address, &name,
438                                   NBT_NODE_H, true, NBT_RCODE_OK);
439
440         name.type = NBT_NAME_LOGON;
441         ret &= nbt_test_wins_name(tctx, address, &name,
442                                   NBT_NODE_H | NBT_NM_GROUP, false, NBT_RCODE_OK);
443
444         name.type = NBT_NAME_BROWSER;
445         ret &= nbt_test_wins_name(tctx, address, &name,
446                                   NBT_NODE_H | NBT_NM_GROUP, false, NBT_RCODE_OK);
447
448         name.type = NBT_NAME_PDC;
449         ret &= nbt_test_wins_name(tctx, address, &name,
450                                   NBT_NODE_H, true, NBT_RCODE_OK);
451
452         name.type = 0xBF;
453         ret &= nbt_test_wins_name(tctx, address, &name,
454                                   NBT_NODE_H, true, NBT_RCODE_OK);
455
456         name.type = 0xBE;
457         ret &= nbt_test_wins_name(tctx, address, &name,
458                                   NBT_NODE_H, false, NBT_RCODE_OK);
459
460         name.scope = "example";
461         name.type = 0x72;
462         ret &= nbt_test_wins_name(tctx, address, &name,
463                                   NBT_NODE_H, true, NBT_RCODE_OK);
464
465         name.scope = "example";
466         name.type = 0x71;
467         ret &= nbt_test_wins_name(tctx, address, &name,
468                                   NBT_NODE_H | NBT_NM_GROUP, false, NBT_RCODE_OK);
469
470         name.scope = "foo.example.com";
471         name.type = 0x72;
472         ret &= nbt_test_wins_name(tctx, address, &name,
473                                   NBT_NODE_H, false, NBT_RCODE_OK);
474
475         name.name = talloc_asprintf(tctx, "_T\01-%5u.foo", r);
476         ret &= nbt_test_wins_name(tctx, address, &name,
477                                   NBT_NODE_H, false, NBT_RCODE_OK);
478
479         name.name = "";
480         ret &= nbt_test_wins_name(tctx, address, &name,
481                                   NBT_NODE_H, false, NBT_RCODE_OK);
482
483         name.name = talloc_asprintf(tctx, ".");
484         ret &= nbt_test_wins_name(tctx, address, &name,
485                                   NBT_NODE_H, false, NBT_RCODE_OK);
486
487         name.name = talloc_asprintf(tctx, "%5u-\377\200\300FOO", r);
488         ret &= nbt_test_wins_name(tctx, address, &name,
489                                   NBT_NODE_H, false, NBT_RCODE_OK);
490
491         return ret;
492 }
493
494 /*
495   test WINS operations
496 */
497 struct torture_suite *torture_nbt_wins(TALLOC_CTX *mem_ctx)
498 {
499         struct torture_suite *suite = torture_suite_create(mem_ctx, "WINS");
500
501         torture_suite_add_simple_test(suite, "wins", nbt_test_wins);
502
503         return suite;
504 }