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