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