r5252: - fixed nmblookup for the nbt api changes
[samba.git] / source4 / torture / nbt / register.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    NBT name registration 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
27 #define CHECK_VALUE(v, correct) do { \
28         if ((v) != (correct)) { \
29                 printf("(%s) Incorrect value %s=%d - should be %d\n", \
30                        __location__, #v, v, correct); \
31                 ret = False; \
32         }} while (0)
33
34 #define CHECK_STRING(v, correct) do { \
35         if (StrCaseCmp(v, correct) != 0) { \
36                 printf("(%s) Incorrect value %s='%s' - should be '%s'\n", \
37                        __location__, #v, v, correct); \
38                 ret = False; \
39         }} while (0)
40
41 #define BOGUS_ADDRESS1 "255.255.255.254"
42 #define BOGUS_ADDRESS2 "255.255.255.253"
43
44 /*
45   test that a server responds correctly to attempted registrations of its name
46 */
47 static BOOL nbt_register_own(TALLOC_CTX *mem_ctx, struct nbt_name *name, 
48                              const char *address)
49 {
50         struct nbt_name_register io;
51         NTSTATUS status;
52         struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL);
53         BOOL ret = True;
54
55         printf("Testing name defense to name registration\n");
56
57         io.in.name = *name;
58         io.in.dest_addr = address;
59         io.in.address = BOGUS_ADDRESS1;
60         io.in.nb_flags = NBT_NODE_B | NBT_NM_ACTIVE;
61         io.in.register_demand = False;
62         io.in.broadcast = True;
63         io.in.ttl = 1234;
64         io.in.timeout = 3;
65         io.in.retries = 0;
66         
67         status = nbt_name_register(nbtsock, mem_ctx, &io);
68         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
69                 printf("No response from %s for name register\n", address);
70                 return False;
71         }
72         if (!NT_STATUS_IS_OK(status)) {
73                 printf("Bad response from %s for name register - %s\n",
74                        address, nt_errstr(status));
75                 return False;
76         }
77         
78         CHECK_STRING(io.out.name.name, name->name);
79         CHECK_VALUE(io.out.name.type, name->type);
80         CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
81
82         /* check a register demand */
83         io.in.address = BOGUS_ADDRESS2;
84         io.in.register_demand = True;
85
86         status = nbt_name_register(nbtsock, mem_ctx, &io);
87         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
88                 printf("No response from %s for name register\n", address);
89                 return False;
90         }
91         if (!NT_STATUS_IS_OK(status)) {
92                 printf("Bad response from %s for name register - %s\n",
93                        address, nt_errstr(status));
94                 return False;
95         }
96         
97         CHECK_STRING(io.out.name.name, name->name);
98         CHECK_VALUE(io.out.name.type, name->type);
99         CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
100
101         return ret;
102 }
103
104
105 /*
106   test that a server responds correctly to attempted name refresh requests
107 */
108 static BOOL nbt_refresh_own(TALLOC_CTX *mem_ctx, struct nbt_name *name, 
109                             const char *address)
110 {
111         struct nbt_name_refresh io;
112         NTSTATUS status;
113         struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL);
114         BOOL ret = True;
115
116         printf("Testing name defense to name refresh\n");
117
118         io.in.name = *name;
119         io.in.dest_addr = address;
120         io.in.address = BOGUS_ADDRESS1;
121         io.in.nb_flags = NBT_NODE_B | NBT_NM_ACTIVE;
122         io.in.broadcast = True;
123         io.in.ttl = 1234;
124         io.in.timeout = 3;
125         io.in.retries = 0;
126         
127         status = nbt_name_refresh(nbtsock, mem_ctx, &io);
128         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
129                 printf("No response from %s for name refresh\n", address);
130                 return False;
131         }
132         if (!NT_STATUS_IS_OK(status)) {
133                 printf("Bad response from %s for name refresh - %s\n",
134                        address, nt_errstr(status));
135                 return False;
136         }
137         
138         CHECK_STRING(io.out.name.name, name->name);
139         CHECK_VALUE(io.out.name.type, name->type);
140         CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
141
142         return ret;
143 }
144
145
146 /*
147   register names with a WINS server
148 */
149 static BOOL nbt_register_wins(TALLOC_CTX *mem_ctx, struct nbt_name *name, 
150                               const char *address)
151 {
152         struct nbt_name_refresh_wins io;
153         struct nbt_name_query q;
154         NTSTATUS status;
155         struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL);
156         BOOL ret = True;
157
158         printf("Testing name registration to WINS\n");
159
160         io.in.name.name = talloc_asprintf(mem_ctx, "_TORTURE-%5u", 
161                                           (unsigned)(random() % (100000)));
162         io.in.name.type = NBT_NAME_CLIENT;
163         io.in.name.scope = NULL;
164         io.in.wins_servers = str_list_make(mem_ctx, address, NULL);
165         io.in.addresses = str_list_make(mem_ctx, BOGUS_ADDRESS1, NULL);
166         io.in.nb_flags = NBT_NODE_M | NBT_NM_ACTIVE;
167         io.in.ttl = 12345;
168         
169         status = nbt_name_refresh_wins(nbtsock, mem_ctx, &io);
170         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
171                 printf("No response from %s for name register\n", address);
172                 return False;
173         }
174         if (!NT_STATUS_IS_OK(status)) {
175                 printf("Bad response from %s for name register - %s\n",
176                        address, nt_errstr(status));
177                 return False;
178         }
179         
180         CHECK_STRING(io.out.wins_server, address);
181         CHECK_VALUE(io.out.rcode, 0);
182
183         /* query the name to make sure its there */
184         q.in.name = io.in.name;
185         q.in.dest_addr = address;
186         q.in.broadcast = False;
187         q.in.wins_lookup = True;
188         q.in.timeout = 3;
189         q.in.retries = 0;
190
191         status = nbt_name_query(nbtsock, mem_ctx, &q);
192         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
193                 printf("No response from %s for name query\n", address);
194                 return False;
195         }
196         if (!NT_STATUS_IS_OK(status)) {
197                 printf("Bad response from %s for name query - %s\n",
198                        address, nt_errstr(status));
199                 return False;
200         }
201         
202         CHECK_STRING(q.out.name.name, q.in.name.name);
203         CHECK_VALUE(q.out.name.type, q.in.name.type);
204         CHECK_VALUE(q.out.num_addrs, 1);
205         CHECK_STRING(q.out.reply_addrs[0], BOGUS_ADDRESS1);
206
207         return ret;
208 }
209
210
211 /*
212   test name registration to a server
213 */
214 BOOL torture_nbt_register(void)
215 {
216         const char *address;
217         struct nbt_name name;
218         TALLOC_CTX *mem_ctx = talloc_new(NULL);
219         NTSTATUS status;
220         BOOL ret = True;
221         
222         name.name = lp_parm_string(-1, "torture", "host");
223         name.type = NBT_NAME_SERVER;
224         name.scope = NULL;
225
226         /* do an initial name resolution to find its IP */
227         status = resolve_name(&name, mem_ctx, &address);
228         if (!NT_STATUS_IS_OK(status)) {
229                 printf("Failed to resolve %s - %s\n",
230                        name.name, nt_errstr(status));
231                 talloc_free(mem_ctx);
232                 return False;
233         }
234
235         ret &= nbt_register_own(mem_ctx, &name, address);
236         ret &= nbt_refresh_own(mem_ctx, &name, address);
237         ret &= nbt_register_wins(mem_ctx, &name, address);
238
239         talloc_free(mem_ctx);
240
241         return ret;
242 }