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