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