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