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