r2071: - change smbtorture to use the popt_common stuff
[abartlet/samba.git/.git] / source4 / torture / raw / context.c
1 /* 
2    Unix SMB/CIFS implementation.
3    test suite for session setup operations
4    Copyright (C) Andrew Tridgell 2003
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 #define BASEDIR "\\rawcontext"
24
25 #define CHECK_STATUS(status, correct) do { \
26         if (!NT_STATUS_EQUAL(status, correct)) { \
27                 printf("(%d) Incorrect status %s - should be %s\n", \
28                        __LINE__, nt_errstr(status), nt_errstr(correct)); \
29                 ret = False; \
30                 goto done; \
31         }} while (0)
32
33 #define CHECK_VALUE(v, correct) do { \
34         if ((v) != (correct)) { \
35                 printf("(%d) Incorrect value %s=%d - should be %d\n", \
36                        __LINE__, #v, v, correct); \
37                 ret = False; \
38                 goto done; \
39         }} while (0)
40
41 #define CHECK_NOT_VALUE(v, correct) do { \
42         if ((v) == (correct)) { \
43                 printf("(%d) Incorrect value %s=%d - should not be %d\n", \
44                        __LINE__, #v, v, correct); \
45                 ret = False; \
46                 goto done; \
47         }} while (0)
48
49
50 /*
51   test session ops
52 */
53 static BOOL test_session(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
54 {
55         NTSTATUS status;
56         BOOL ret = True;
57         const char *username, *domain, *password;
58         struct smbcli_session *session;
59         struct smbcli_session *session2;
60         struct smbcli_session *session3;
61         struct smbcli_tree *tree;
62         union smb_sesssetup setup;
63         union smb_open io;
64         union smb_write wr;
65         union smb_close cl;
66         int fnum;
67         const char *fname = BASEDIR "\\test.txt";
68         char c = 1;
69
70         printf("TESTING SESSION HANDLING\n");
71
72         if (smbcli_deltree(cli->tree, BASEDIR) == -1 ||
73             NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, BASEDIR))) {
74                 printf("Unable to setup %s - %s\n", BASEDIR, smbcli_errstr(cli->tree));
75                 return False;
76         }
77
78         username = lp_parm_string(-1, "torture", "username");
79         password = lp_parm_string(-1, "torture", "password");
80         domain = lp_parm_string(-1, "torture", "userdomain");
81
82         printf("create a second security context on the same transport\n");
83         session = smbcli_session_init(cli->transport);
84         setup.generic.level = RAW_SESSSETUP_GENERIC;
85         setup.generic.in.sesskey = cli->transport->negotiate.sesskey;
86         setup.generic.in.capabilities = cli->transport->negotiate.capabilities; /* ignored in secondary session setup, except by our libs, which care about the extended security bit */
87         setup.generic.in.password = password;
88         setup.generic.in.user = username;
89         setup.generic.in.domain = domain;
90
91         status = smb_raw_session_setup(session, mem_ctx, &setup);
92         CHECK_STATUS(status, NT_STATUS_OK);
93
94         session->vuid = setup.generic.out.vuid;
95
96         printf("create a third security context on the same transport, with vuid set\n");
97         session2 = smbcli_session_init(cli->transport);
98         session2->vuid = session->vuid;
99         setup.generic.level = RAW_SESSSETUP_GENERIC;
100         setup.generic.in.sesskey = cli->transport->negotiate.sesskey;
101         setup.generic.in.capabilities = cli->transport->negotiate.capabilities; /* ignored in secondary session setup, except by our libs, which care about the extended security bit */
102         setup.generic.in.password = password;
103         setup.generic.in.user = username;
104         setup.generic.in.domain = domain;
105
106         status = smb_raw_session_setup(session2, mem_ctx, &setup);
107         CHECK_STATUS(status, NT_STATUS_OK);
108
109         session2->vuid = setup.generic.out.vuid;
110         printf("vuid1=%d vuid2=%d vuid3=%d\n", cli->session->vuid, session->vuid, session2->vuid);
111         
112         CHECK_NOT_VALUE(session->vuid, session2->vuid);
113
114         if (cli->transport->negotiate.capabilities & CAP_EXTENDED_SECURITY) {
115                 printf("create a fourth security context on the same transport, without extended security\n");
116                 session3 = smbcli_session_init(cli->transport);
117                 session3->vuid = session->vuid;
118                 setup.generic.level = RAW_SESSSETUP_GENERIC;
119                 setup.generic.in.sesskey = cli->transport->negotiate.sesskey;
120                 setup.generic.in.capabilities = 0; /* force a non extended security login (should fail) */
121                 setup.generic.in.password = password;
122                 setup.generic.in.user = username;
123                 setup.generic.in.domain = domain;
124
125                 status = smb_raw_session_setup(session3, mem_ctx, &setup);
126                 CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
127         }
128                 
129         printf("use the same tree as the existing connection\n");
130         tree = smbcli_tree_init(session);
131         tree->tid = cli->tree->tid;
132         cli->tree->reference_count++;
133
134         printf("create a file using the new vuid\n");
135         io.generic.level = RAW_OPEN_NTCREATEX;
136         io.ntcreatex.in.root_fid = 0;
137         io.ntcreatex.in.flags = 0;
138         io.ntcreatex.in.access_mask = SEC_RIGHT_MAXIMUM_ALLOWED;
139         io.ntcreatex.in.create_options = 0;
140         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
141         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
142         io.ntcreatex.in.alloc_size = 0;
143         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
144         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
145         io.ntcreatex.in.security_flags = 0;
146         io.ntcreatex.in.fname = fname;
147         status = smb_raw_open(tree, mem_ctx, &io);
148         CHECK_STATUS(status, NT_STATUS_OK);
149         fnum = io.ntcreatex.out.fnum;
150
151         printf("write using the old vuid\n");
152         wr.generic.level = RAW_WRITE_WRITEX;
153         wr.writex.in.fnum = fnum;
154         wr.writex.in.offset = 0;
155         wr.writex.in.wmode = 0;
156         wr.writex.in.remaining = 0;
157         wr.writex.in.count = 1;
158         wr.writex.in.data = &c;
159
160         status = smb_raw_write(cli->tree, &wr);
161         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
162
163         printf("write with the new vuid\n");
164         status = smb_raw_write(tree, &wr);
165         CHECK_STATUS(status, NT_STATUS_OK);
166         CHECK_VALUE(wr.writex.out.nwritten, 1);
167
168         printf("logoff the new vuid\n");
169         status = smb_raw_ulogoff(session);
170         CHECK_STATUS(status, NT_STATUS_OK);
171
172         printf("the new vuid should not now be accessible\n");
173         status = smb_raw_write(tree, &wr);
174         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
175
176         printf("the fnum should have been auto-closed\n");
177         cl.close.level = RAW_CLOSE_CLOSE;
178         cl.close.in.fnum = fnum;
179         cl.close.in.write_time = 0;
180         status = smb_raw_close(cli->tree, &cl);
181         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
182
183         /* close down the new tree, which will also close the session
184            as the reference count will be 0 */
185         smbcli_tree_close(tree);
186         
187 done:
188         return ret;
189 }
190
191
192 /*
193   test tree ops
194 */
195 static BOOL test_tree(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
196 {
197         NTSTATUS status;
198         BOOL ret = True;
199         const char *share;
200         struct smbcli_tree *tree;
201         union smb_tcon tcon;
202         union smb_open io;
203         union smb_write wr;
204         union smb_close cl;
205         int fnum;
206         const char *fname = BASEDIR "\\test.txt";
207         char c = 1;
208
209         printf("TESTING TREE HANDLING\n");
210
211         if (smbcli_deltree(cli->tree, BASEDIR) == -1 ||
212             NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, BASEDIR))) {
213                 printf("Unable to setup %s - %s\n", BASEDIR, smbcli_errstr(cli->tree));
214                 return False;
215         }
216
217         share = lp_parm_string(-1, "torture", "share");
218
219         printf("create a second tree context on the same session\n");
220         tree = smbcli_tree_init(cli->session);
221
222         tcon.generic.level = RAW_TCON_TCONX;
223         tcon.tconx.in.flags = 0;
224         tcon.tconx.in.password = data_blob(NULL, 0);
225         tcon.tconx.in.path = share;
226         tcon.tconx.in.device = "A:";    
227         status = smb_tree_connect(tree, mem_ctx, &tcon);
228         CHECK_STATUS(status, NT_STATUS_OK);
229
230         tree->tid = tcon.tconx.out.cnum;
231         printf("tid1=%d tid2=%d\n", cli->tree->tid, tree->tid);
232
233         printf("try a tconx with a bad device type\n");
234         tcon.tconx.in.device = "FOO";   
235         status = smb_tree_connect(tree, mem_ctx, &tcon);
236         CHECK_STATUS(status, NT_STATUS_BAD_DEVICE_TYPE);
237
238
239         printf("create a file using the new tid\n");
240         io.generic.level = RAW_OPEN_NTCREATEX;
241         io.ntcreatex.in.root_fid = 0;
242         io.ntcreatex.in.flags = 0;
243         io.ntcreatex.in.access_mask = SEC_RIGHT_MAXIMUM_ALLOWED;
244         io.ntcreatex.in.create_options = 0;
245         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
246         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
247         io.ntcreatex.in.alloc_size = 0;
248         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
249         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
250         io.ntcreatex.in.security_flags = 0;
251         io.ntcreatex.in.fname = fname;
252         status = smb_raw_open(tree, mem_ctx, &io);
253         CHECK_STATUS(status, NT_STATUS_OK);
254         fnum = io.ntcreatex.out.fnum;
255
256         printf("write using the old tid\n");
257         wr.generic.level = RAW_WRITE_WRITEX;
258         wr.writex.in.fnum = fnum;
259         wr.writex.in.offset = 0;
260         wr.writex.in.wmode = 0;
261         wr.writex.in.remaining = 0;
262         wr.writex.in.count = 1;
263         wr.writex.in.data = &c;
264
265         status = smb_raw_write(cli->tree, &wr);
266         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
267
268         printf("write with the new tid\n");
269         status = smb_raw_write(tree, &wr);
270         CHECK_STATUS(status, NT_STATUS_OK);
271         CHECK_VALUE(wr.writex.out.nwritten, 1);
272
273         printf("disconnect the new tid\n");
274         status = smb_tree_disconnect(tree);
275         CHECK_STATUS(status, NT_STATUS_OK);
276
277         printf("the new tid should not now be accessible\n");
278         status = smb_raw_write(tree, &wr);
279         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
280
281         printf("the fnum should have been auto-closed\n");
282         cl.close.level = RAW_CLOSE_CLOSE;
283         cl.close.in.fnum = fnum;
284         cl.close.in.write_time = 0;
285         status = smb_raw_close(cli->tree, &cl);
286         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
287
288         /* close down the new tree */
289         smbcli_tree_close(tree);
290         
291 done:
292         return ret;
293 }
294
295
296 /*
297   test pid ops
298 */
299 static BOOL test_pid(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
300 {
301         NTSTATUS status;
302         BOOL ret = True;
303         union smb_open io;
304         union smb_write wr;
305         union smb_close cl;
306         int fnum;
307         const char *fname = BASEDIR "\\test.txt";
308         char c = 1;
309         uint16_t pid1, pid2;
310
311         printf("TESTING PID HANDLING\n");
312
313         if (smbcli_deltree(cli->tree, BASEDIR) == -1 ||
314             NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, BASEDIR))) {
315                 printf("Unable to setup %s - %s\n", BASEDIR, smbcli_errstr(cli->tree));
316                 return False;
317         }
318
319         printf("create a second pid\n");
320         pid1 = cli->session->pid;
321         pid2 = pid1+1;
322
323         printf("pid1=%d pid2=%d\n", pid1, pid2);
324
325         printf("create a file using the new pid\n");
326         cli->session->pid = pid2;
327         io.generic.level = RAW_OPEN_NTCREATEX;
328         io.ntcreatex.in.root_fid = 0;
329         io.ntcreatex.in.flags = 0;
330         io.ntcreatex.in.access_mask = SEC_RIGHT_MAXIMUM_ALLOWED;
331         io.ntcreatex.in.create_options = 0;
332         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
333         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
334         io.ntcreatex.in.alloc_size = 0;
335         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
336         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
337         io.ntcreatex.in.security_flags = 0;
338         io.ntcreatex.in.fname = fname;
339         status = smb_raw_open(cli->tree, mem_ctx, &io);
340         CHECK_STATUS(status, NT_STATUS_OK);
341         fnum = io.ntcreatex.out.fnum;
342
343         printf("write using the old pid\n");
344         cli->session->pid = pid1;
345         wr.generic.level = RAW_WRITE_WRITEX;
346         wr.writex.in.fnum = fnum;
347         wr.writex.in.offset = 0;
348         wr.writex.in.wmode = 0;
349         wr.writex.in.remaining = 0;
350         wr.writex.in.count = 1;
351         wr.writex.in.data = &c;
352
353         status = smb_raw_write(cli->tree, &wr);
354         CHECK_STATUS(status, NT_STATUS_OK);
355         CHECK_VALUE(wr.writex.out.nwritten, 1);
356
357         printf("write with the new pid\n");
358         cli->session->pid = pid2;
359         status = smb_raw_write(cli->tree, &wr);
360         CHECK_STATUS(status, NT_STATUS_OK);
361         CHECK_VALUE(wr.writex.out.nwritten, 1);
362
363         printf("exit the old pid\n");
364         cli->session->pid = pid1;
365         status = smb_raw_exit(cli->session);
366         CHECK_STATUS(status, NT_STATUS_OK);
367
368         printf("the fnum should still be accessible\n");
369         cli->session->pid = pid1;
370         status = smb_raw_write(cli->tree, &wr);
371         CHECK_STATUS(status, NT_STATUS_OK);
372         CHECK_VALUE(wr.writex.out.nwritten, 1);
373
374         printf("exit the new pid\n");
375         cli->session->pid = pid2;
376         status = smb_raw_exit(cli->session);
377         CHECK_STATUS(status, NT_STATUS_OK);
378
379         printf("the fnum should not now be accessible\n");
380         cli->session->pid = pid1;
381         status = smb_raw_write(cli->tree, &wr);
382         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
383
384         printf("the fnum should have been auto-closed\n");
385         cl.close.level = RAW_CLOSE_CLOSE;
386         cl.close.in.fnum = fnum;
387         cl.close.in.write_time = 0;
388         status = smb_raw_close(cli->tree, &cl);
389         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
390
391 done:
392         return ret;
393 }
394
395
396 /* 
397    basic testing of session/tree context calls
398 */
399 BOOL torture_raw_context(int dummy)
400 {
401         struct smbcli_state *cli;
402         BOOL ret = True;
403         TALLOC_CTX *mem_ctx;
404
405         if (!torture_open_connection(&cli)) {
406                 return False;
407         }
408
409         mem_ctx = talloc_init("torture_raw_context");
410
411         if (!test_session(cli, mem_ctx)) {
412                 ret = False;
413         }
414
415         if (!test_tree(cli, mem_ctx)) {
416                 ret = False;
417         }
418
419         if (!test_pid(cli, mem_ctx)) {
420                 ret = False;
421         }
422
423         smb_raw_exit(cli->session);
424         smbcli_deltree(cli->tree, BASEDIR);
425
426         torture_close_connection(cli);
427         talloc_destroy(mem_ctx);
428         return ret;
429 }