r5333: weird, w2k3 always sends a positive name release response, even for names...
[samba.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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "libcli/nbt/libnbt.h"
25 #include "librpc/gen_ndr/ndr_nbt.h"
26 #include "lib/socket/socket.h"
27
28 #define CHECK_VALUE(v, correct) do { \
29         if ((v) != (correct)) { \
30                 printf("(%s) Incorrect value %s=%d - should be %d\n", \
31                        __location__, #v, v, correct); \
32                 ret = False; \
33         }} while (0)
34
35 #define CHECK_STRING(v, correct) do { \
36         if ((v) != (correct) && \
37             ((v)==NULL || (correct)==NULL || StrCaseCmp(v, correct) != 0)) { \
38                 printf("(%s) Incorrect value %s='%s' - should be '%s'\n", \
39                        __location__, #v, v, correct); \
40                 ret = False; \
41         }} while (0)
42
43 #define CHECK_NAME(_name, correct) do { \
44         CHECK_STRING((_name).name, (correct).name); \
45         CHECK_VALUE((_name).type, (correct).type); \
46         CHECK_STRING((_name).scope, (correct).scope); \
47 } while (0)
48
49 /*
50   test operations against a WINS server
51 */
52 static BOOL nbt_test_wins_name(TALLOC_CTX *mem_ctx, const char *address,
53                                struct nbt_name *name)
54 {
55         struct nbt_name_register_wins io;
56         struct nbt_name_query query;
57         struct nbt_name_refresh_wins refresh;
58         struct nbt_name_release release;
59         NTSTATUS status;
60         struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL);
61         BOOL ret = True;
62         const char *myaddress = talloc_strdup(mem_ctx, iface_n_ip(0));
63
64         /* we do the listen here to ensure the WINS server receives the packets from
65            the right IP */
66         socket_listen(nbtsock->sock, myaddress, 0, 0, 0);
67
68         printf("Testing name registration to WINS with name %s<%02x> at %s\n", 
69                name->name, name->type, myaddress);
70         if (name->scope) {
71                 printf("scope is %s\n", name->scope);
72         }
73
74         printf("release the name\n");
75         release.in.name = *name;
76         release.in.dest_addr = address;
77         release.in.address = myaddress;
78         release.in.nb_flags = NBT_NODE_H;
79         release.in.broadcast = False;
80         release.in.timeout = 3;
81         release.in.retries = 0;
82
83         status = nbt_name_release(nbtsock, mem_ctx, &release);
84         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
85                 printf("No response from %s for name release\n", address);
86                 return False;
87         }
88         if (!NT_STATUS_IS_OK(status)) {
89                 printf("Bad response from %s for name query - %s\n",
90                        address, nt_errstr(status));
91                 return False;
92         }
93         CHECK_VALUE(release.out.rcode, 0);
94
95         printf("register the name\n");
96         io.in.name = *name;
97         io.in.wins_servers = str_list_make(mem_ctx, address, NULL);
98         io.in.addresses = str_list_make(mem_ctx, myaddress, NULL);
99         io.in.nb_flags = NBT_NODE_H;
100         io.in.ttl = 300000;
101         
102         status = nbt_name_register_wins(nbtsock, mem_ctx, &io);
103         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
104                 printf("No response from %s for name register\n", address);
105                 return False;
106         }
107         if (!NT_STATUS_IS_OK(status)) {
108                 printf("Bad response from %s for name register - %s\n",
109                        address, nt_errstr(status));
110                 return False;
111         }
112         
113         CHECK_STRING(io.out.wins_server, address);
114         CHECK_VALUE(io.out.rcode, 0);
115
116         printf("query the name to make sure its there\n");
117         query.in.name = *name;
118         query.in.dest_addr = address;
119         query.in.broadcast = False;
120         query.in.wins_lookup = True;
121         query.in.timeout = 3;
122         query.in.retries = 0;
123
124         status = nbt_name_query(nbtsock, mem_ctx, &query);
125         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
126                 printf("No response from %s for name query\n", address);
127                 return False;
128         }
129         if (!NT_STATUS_IS_OK(status)) {
130                 printf("Bad response from %s for name query - %s\n",
131                        address, nt_errstr(status));
132                 return False;
133         }
134         
135         CHECK_NAME(query.out.name, *name);
136         CHECK_VALUE(query.out.num_addrs, 1);
137         CHECK_STRING(query.out.reply_addrs[0], myaddress);
138
139
140         query.in.name.name = strupper_talloc(mem_ctx, name->name);
141         if (query.in.name.name &&
142             strcmp(query.in.name.name, name->name) != 0) {
143                 printf("check case sensitivity\n");
144                 status = nbt_name_query(nbtsock, mem_ctx, &query);
145                 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
146                         printf("No response from %s for name query\n", address);
147                         return False;
148                 }
149                 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
150                         printf("Bad response from %s for name query - %s\n",
151                                address, nt_errstr(status));
152                         return False;
153                 }
154         }
155
156         query.in.name = *name;
157         if (name->scope) {
158                 query.in.name.scope = strupper_talloc(mem_ctx, name->scope);
159         }
160         if (query.in.name.scope &&
161             strcmp(query.in.name.scope, name->scope) != 0) {
162                 printf("check case sensitivity on scope\n");
163                 status = nbt_name_query(nbtsock, mem_ctx, &query);
164                 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
165                         printf("No response from %s for name query\n", address);
166                         return False;
167                 }
168                 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
169                         printf("Bad response from %s for name query - %s\n",
170                                address, nt_errstr(status));
171                         return False;
172                 }
173         }
174
175         printf("refresh the name\n");
176         refresh.in.name = *name;
177         refresh.in.wins_servers = str_list_make(mem_ctx, address, NULL);
178         refresh.in.addresses = str_list_make(mem_ctx, myaddress, NULL);
179         refresh.in.nb_flags = NBT_NODE_H;
180         refresh.in.ttl = 12345;
181         
182         status = nbt_name_refresh_wins(nbtsock, mem_ctx, &refresh);
183         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
184                 printf("No response from %s for name refresh\n", address);
185                 return False;
186         }
187         if (!NT_STATUS_IS_OK(status)) {
188                 printf("Bad response from %s for name refresh - %s\n",
189                        address, nt_errstr(status));
190                 return False;
191         }
192         
193         CHECK_STRING(io.out.wins_server, address);
194         CHECK_VALUE(io.out.rcode, 0);
195
196         printf("release the name\n");
197         release.in.name = *name;
198         release.in.dest_addr = address;
199         release.in.address = myaddress;
200         release.in.nb_flags = NBT_NODE_H;
201         release.in.broadcast = False;
202         release.in.timeout = 3;
203         release.in.retries = 0;
204
205         status = nbt_name_release(nbtsock, mem_ctx, &release);
206         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
207                 printf("No response from %s for name release\n", address);
208                 return False;
209         }
210         if (!NT_STATUS_IS_OK(status)) {
211                 printf("Bad response from %s for name query - %s\n",
212                        address, nt_errstr(status));
213                 return False;
214         }
215         
216         CHECK_NAME(release.out.name, *name);
217         CHECK_VALUE(release.out.rcode, 0);
218
219
220         printf("release again\n");
221         status = nbt_name_release(nbtsock, mem_ctx, &release);
222         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
223                 printf("No response from %s for name release\n", address);
224                 return False;
225         }
226         if (!NT_STATUS_IS_OK(status)) {
227                 printf("Bad response from %s for name query - %s\n",
228                        address, nt_errstr(status));
229                 return False;
230         }
231         
232         CHECK_NAME(release.out.name, *name);
233         CHECK_VALUE(release.out.rcode, 0);
234
235
236         printf("query the name to make sure its gone\n");
237         query.in.name = *name;
238         status = nbt_name_query(nbtsock, mem_ctx, &query);
239         if (NT_STATUS_IS_OK(status)) {
240                 printf("ERROR: Name query success after release\n");
241                 return False;
242         }
243         if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
244                 printf("Incorrect response to name query - %s\n", nt_errstr(status));
245                 return False;
246         }
247         
248         return ret;
249 }
250
251
252
253 /*
254   test operations against a WINS server
255 */
256 static BOOL nbt_test_wins(TALLOC_CTX *mem_ctx, const char *address)
257 {
258         struct nbt_name name;
259         BOOL ret = True;
260         uint32_t r = (unsigned)(random() % (100000));
261
262         name.name = talloc_asprintf(mem_ctx, "_TORTURE-%5u", r);
263                                     
264         name.type = NBT_NAME_CLIENT;
265         name.scope = NULL;
266         ret &= nbt_test_wins_name(mem_ctx, address, &name);
267
268         name.scope = "example";
269         name.type = 0x71;
270         ret &= nbt_test_wins_name(mem_ctx, address, &name);
271
272         name.scope = "foo.example.com";
273         ret &= nbt_test_wins_name(mem_ctx, address, &name);
274
275         name.name = talloc_asprintf(mem_ctx, "_T\01-%5u.foo", r);
276         ret &= nbt_test_wins_name(mem_ctx, address, &name);
277
278         name.name = "";
279         ret &= nbt_test_wins_name(mem_ctx, address, &name);
280
281         name.name = talloc_asprintf(mem_ctx, ".");
282         ret &= nbt_test_wins_name(mem_ctx, address, &name);
283
284         name.name = talloc_asprintf(mem_ctx, "%5u-\377\200\300FOO", r);
285         ret &= nbt_test_wins_name(mem_ctx, address, &name);
286
287         return ret;
288 }
289
290 /*
291   test WINS operations
292 */
293 BOOL torture_nbt_wins(void)
294 {
295         const char *address;
296         struct nbt_name name;
297         TALLOC_CTX *mem_ctx = talloc_new(NULL);
298         NTSTATUS status;
299         BOOL ret = True;
300         
301         name.name = lp_parm_string(-1, "torture", "host");
302         name.type = NBT_NAME_SERVER;
303         name.scope = NULL;
304
305         /* do an initial name resolution to find its IP */
306         status = resolve_name(&name, mem_ctx, &address);
307         if (!NT_STATUS_IS_OK(status)) {
308                 printf("Failed to resolve %s - %s\n",
309                        name.name, nt_errstr(status));
310                 talloc_free(mem_ctx);
311                 return False;
312         }
313
314         ret &= nbt_test_wins(mem_ctx, address);
315
316         talloc_free(mem_ctx);
317
318         return ret;
319 }