3f8b361a932686ba9e95480b6a772011a65d9a93
[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 (name->type != NBT_NAME_MASTER && 
115             name->type != NBT_NAME_LOGON && 
116             (nb_flags & NBT_NM_GROUP)) {
117                 printf("Try to register as non-group\n");
118                 io.in.nb_flags &= ~NBT_NM_GROUP;
119                 status = nbt_name_register_wins(nbtsock, mem_ctx, &io);
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                 CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
126         }
127
128         printf("query the name to make sure its there\n");
129         query.in.name = *name;
130         query.in.dest_addr = address;
131         query.in.broadcast = False;
132         query.in.wins_lookup = True;
133         query.in.timeout = 3;
134         query.in.retries = 0;
135
136         status = nbt_name_query(nbtsock, mem_ctx, &query);
137         if (name->type == NBT_NAME_MASTER) {
138                 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
139                         printf("Bad response from %s for name query - %s\n",
140                                address, nt_errstr(status));
141                         return False;
142                 }
143                 return ret;
144         }
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_IS_OK(status)) {
150                 printf("Bad response from %s for name query - %s\n",
151                        address, nt_errstr(status));
152                 return False;
153         }
154         
155         CHECK_NAME(query.out.name, *name);
156         CHECK_VALUE(query.out.num_addrs, 1);
157         if (name->type != NBT_NAME_LOGON &&
158             (nb_flags & NBT_NM_GROUP)) {
159                 CHECK_STRING(query.out.reply_addrs[0], "255.255.255.255");
160         } else {
161                 CHECK_STRING(query.out.reply_addrs[0], myaddress);
162         }
163
164
165         query.in.name.name = strupper_talloc(mem_ctx, name->name);
166         if (query.in.name.name &&
167             strcmp(query.in.name.name, name->name) != 0) {
168                 printf("check case sensitivity\n");
169                 status = nbt_name_query(nbtsock, mem_ctx, &query);
170                 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
171                         printf("No response from %s for name query\n", address);
172                         return False;
173                 }
174                 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
175                         printf("Bad response from %s for name query - %s\n",
176                                address, nt_errstr(status));
177                         return False;
178                 }
179         }
180
181         query.in.name = *name;
182         if (name->scope) {
183                 query.in.name.scope = strupper_talloc(mem_ctx, name->scope);
184         }
185         if (query.in.name.scope &&
186             strcmp(query.in.name.scope, name->scope) != 0) {
187                 printf("check case sensitivity on scope\n");
188                 status = nbt_name_query(nbtsock, mem_ctx, &query);
189                 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
190                         printf("No response from %s for name query\n", address);
191                         return False;
192                 }
193                 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
194                         printf("Bad response from %s for name query - %s\n",
195                                address, nt_errstr(status));
196                         return False;
197                 }
198         }
199
200         printf("refresh the name\n");
201         refresh.in.name = *name;
202         refresh.in.wins_servers = str_list_make(mem_ctx, address, NULL);
203         refresh.in.addresses = str_list_make(mem_ctx, myaddress, NULL);
204         refresh.in.nb_flags = nb_flags;
205         refresh.in.ttl = 12345;
206         
207         status = nbt_name_refresh_wins(nbtsock, mem_ctx, &refresh);
208         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
209                 printf("No response from %s for name refresh\n", address);
210                 return False;
211         }
212         if (!NT_STATUS_IS_OK(status)) {
213                 printf("Bad response from %s for name refresh - %s\n",
214                        address, nt_errstr(status));
215                 return False;
216         }
217         
218         CHECK_STRING(refresh.out.wins_server, address);
219         CHECK_VALUE(refresh.out.rcode, 0);
220
221         printf("release the name\n");
222         release.in.name = *name;
223         release.in.dest_addr = address;
224         release.in.address = myaddress;
225         release.in.nb_flags = nb_flags;
226         release.in.broadcast = False;
227         release.in.timeout = 3;
228         release.in.retries = 0;
229
230         status = nbt_name_release(nbtsock, mem_ctx, &release);
231         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
232                 printf("No response from %s for name release\n", address);
233                 return False;
234         }
235         if (!NT_STATUS_IS_OK(status)) {
236                 printf("Bad response from %s for name query - %s\n",
237                        address, nt_errstr(status));
238                 return False;
239         }
240         
241         CHECK_NAME(release.out.name, *name);
242         CHECK_VALUE(release.out.rcode, 0);
243
244
245         printf("release again\n");
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("query the name to make sure its gone\n");
262         query.in.name = *name;
263         status = nbt_name_query(nbtsock, mem_ctx, &query);
264         if (name->type != NBT_NAME_LOGON &&
265             (nb_flags & NBT_NM_GROUP)) {
266                 if (!NT_STATUS_IS_OK(status)) {
267                         printf("ERROR: Name query failed after group release - %s\n",
268                                nt_errstr(status));
269                         return False;
270                 }
271         } else {
272                 if (NT_STATUS_IS_OK(status)) {
273                         printf("ERROR: Name query success after release\n");
274                         return False;
275                 }
276                 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
277                         printf("Incorrect response to name query - %s\n", nt_errstr(status));
278                         return False;
279                 }
280         }
281         
282         return ret;
283 }
284
285
286
287 /*
288   test operations against a WINS server
289 */
290 static BOOL nbt_test_wins(TALLOC_CTX *mem_ctx, const char *address)
291 {
292         struct nbt_name name;
293         BOOL ret = True;
294         uint32_t r = (unsigned)(random() % (100000));
295
296         name.name = talloc_asprintf(mem_ctx, "_TORTURE-%5u", r);
297                                     
298         name.type = NBT_NAME_CLIENT;
299         name.scope = NULL;
300         ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
301
302         name.type = NBT_NAME_MASTER;
303         ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
304
305         ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
306
307         name.type = NBT_NAME_LOGON;
308         ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
309
310         name.scope = "example";
311         name.type = 0x72;
312         ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
313
314         name.scope = "example";
315         name.type = 0x71;
316         ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
317
318         name.scope = "foo.example.com";
319         name.type = 0x72;
320         ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
321
322         name.name = talloc_asprintf(mem_ctx, "_T\01-%5u.foo", r);
323         ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
324
325         name.name = "";
326         ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
327
328         name.name = talloc_asprintf(mem_ctx, ".");
329         ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
330
331         name.name = talloc_asprintf(mem_ctx, "%5u-\377\200\300FOO", r);
332         ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
333
334         return ret;
335 }
336
337 /*
338   test WINS operations
339 */
340 BOOL torture_nbt_wins(void)
341 {
342         const char *address;
343         struct nbt_name name;
344         TALLOC_CTX *mem_ctx = talloc_new(NULL);
345         NTSTATUS status;
346         BOOL ret = True;
347         
348         name.name = lp_parm_string(-1, "torture", "host");
349         name.type = NBT_NAME_SERVER;
350         name.scope = NULL;
351
352         /* do an initial name resolution to find its IP */
353         status = resolve_name(&name, mem_ctx, &address);
354         if (!NT_STATUS_IS_OK(status)) {
355                 printf("Failed to resolve %s - %s\n",
356                        name.name, nt_errstr(status));
357                 talloc_free(mem_ctx);
358                 return False;
359         }
360
361         ret &= nbt_test_wins(mem_ctx, address);
362
363         talloc_free(mem_ctx);
364
365         return ret;
366 }