r11695: added SMB2-SCAN torture test for scanning for active SMB2 opcodes
[kai/samba-autobuild/.git] / source / torture / smb2 / connect.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    test suite for SMB2 connection operations
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/raw/libcliraw.h"
25 #include "libcli/smb2/smb2.h"
26 #include "libcli/smb2/smb2_calls.h"
27 #include "lib/cmdline/popt_common.h"
28 #include "lib/events/events.h"
29
30 /*
31   send a close
32 */
33 static NTSTATUS torture_smb2_close(struct smb2_tree *tree, struct smb2_handle handle)
34 {
35         struct smb2_close io;
36         NTSTATUS status;
37         TALLOC_CTX *tmp_ctx = talloc_new(tree);
38
39         ZERO_STRUCT(io);
40         io.in.buffer_code = 0x18;
41         io.in.flags       = SMB2_CLOSE_FLAGS_FULL_INFORMATION;
42         io.in.handle   = handle;
43         status = smb2_close(tree, &io);
44         if (!NT_STATUS_IS_OK(status)) {
45                 printf("close failed - %s\n", nt_errstr(status));
46                 return status;
47         }
48
49         printf("Close gave:\n");
50         printf("create_time     = %s\n", nt_time_string(tmp_ctx, io.out.create_time));
51         printf("access_time     = %s\n", nt_time_string(tmp_ctx, io.out.access_time));
52         printf("write_time      = %s\n", nt_time_string(tmp_ctx, io.out.write_time));
53         printf("change_time     = %s\n", nt_time_string(tmp_ctx, io.out.change_time));
54         printf("alloc_size      = %lld\n", io.out.alloc_size);
55         printf("size            = %lld\n", io.out.size);
56         printf("file_attr       = 0x%x\n", io.out.file_attr);
57
58         talloc_free(tmp_ctx);
59         
60         return status;
61 }
62
63
64 /*
65   send a create
66 */
67 static struct smb2_handle torture_smb2_create(struct smb2_tree *tree, 
68                                               const char *fname)
69 {
70         struct smb2_create io;
71         NTSTATUS status;
72         TALLOC_CTX *tmp_ctx = talloc_new(tree);
73
74         ZERO_STRUCT(io);
75         io.in.buffer_code = 0x39;
76         io.in.oplock_flags = 0;
77         io.in.access_mask = SEC_RIGHTS_FILE_ALL;
78         io.in.file_attr   = FILE_ATTRIBUTE_NORMAL;
79         io.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
80         io.in.share_access = 
81                 NTCREATEX_SHARE_ACCESS_DELETE|
82                 NTCREATEX_SHARE_ACCESS_READ|
83                 NTCREATEX_SHARE_ACCESS_WRITE;
84         io.in.create_options = NTCREATEX_OPTIONS_WRITE_THROUGH;
85         io.in.fname = fname;
86
87         status = smb2_create(tree, &io);
88         if (!NT_STATUS_IS_OK(status)) {
89                 printf("create1 failed - %s\n", nt_errstr(status));
90                 return io.out.handle;
91         }
92
93         printf("Open gave:\n");
94         printf("oplock_flags    = 0x%x\n", io.out.oplock_flags);
95         printf("create_action   = 0x%x\n", io.out.create_action);
96         printf("create_time     = %s\n", nt_time_string(tmp_ctx, io.out.create_time));
97         printf("access_time     = %s\n", nt_time_string(tmp_ctx, io.out.access_time));
98         printf("write_time      = %s\n", nt_time_string(tmp_ctx, io.out.write_time));
99         printf("change_time     = %s\n", nt_time_string(tmp_ctx, io.out.change_time));
100         printf("alloc_size      = %lld\n", io.out.alloc_size);
101         printf("size            = %lld\n", io.out.size);
102         printf("file_attr       = 0x%x\n", io.out.file_attr);
103         printf("handle          = %016llx%016llx\n", 
104                io.out.handle.data[0], 
105                io.out.handle.data[1]);
106
107         talloc_free(tmp_ctx);
108         
109         return io.out.handle;
110 }
111
112 /* 
113    basic testing of SMB2 connection calls
114 */
115 BOOL torture_smb2_connect(void)
116 {
117         TALLOC_CTX *mem_ctx = talloc_new(NULL);
118         struct smb2_tree *tree;
119         const char *host = lp_parm_string(-1, "torture", "host");
120         const char *share = lp_parm_string(-1, "torture", "share");
121         struct cli_credentials *credentials = cmdline_credentials;
122         struct smb2_handle h1, h2;
123         NTSTATUS status;
124
125         status = smb2_connect(mem_ctx, host, share, credentials, &tree, 
126                               event_context_find(mem_ctx));
127         if (!NT_STATUS_IS_OK(status)) {
128                 printf("Connection failed - %s\n", nt_errstr(status));
129                 return False;
130         }
131
132         h1 = torture_smb2_create(tree, "test9.dat");
133         h2 = torture_smb2_create(tree, "test9.dat");
134         torture_smb2_close(tree, h1);
135         torture_smb2_close(tree, h2);
136
137         talloc_free(mem_ctx);
138
139         return True;
140 }