5e0f24cb51dfc05d912104280533b937462c2028
[ira/wip.git] / source3 / torture / test_smb2.c
1 /*
2    Unix SMB/CIFS implementation.
3    Initial test for the smb2 client lib
4    Copyright (C) Volker Lendecke 2011
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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "torture/proto.h"
22 #include "client.h"
23 #include "../libcli/smb/smbXcli_base.h"
24 #include "libsmb/smb2cli.h"
25 #include "libcli/security/security.h"
26 #include "libsmb/proto.h"
27
28 extern fstring host, workgroup, share, password, username, myname;
29
30 bool run_smb2_basic(int dummy)
31 {
32         struct cli_state *cli;
33         NTSTATUS status;
34         uint64_t fid_persistent, fid_volatile;
35         const char *hello = "Hello, world\n";
36         uint8_t *result;
37         uint32_t nread;
38         uint8_t *dir_data;
39         uint32_t dir_data_length;
40
41         printf("Starting SMB2-BASIC\n");
42
43         if (!torture_init_connection(&cli)) {
44                 return false;
45         }
46         cli->smb2.pid = 0xFEFF;
47
48         status = smbXcli_negprot(cli->conn, cli->timeout,
49                                  PROTOCOL_SMB2_02, PROTOCOL_SMB2_02);
50         if (!NT_STATUS_IS_OK(status)) {
51                 printf("smbXcli_negprot returned %s\n", nt_errstr(status));
52                 return false;
53         }
54
55         status = cli_session_setup(cli, username,
56                                    password, strlen(password),
57                                    password, strlen(password),
58                                    workgroup);
59         if (!NT_STATUS_IS_OK(status)) {
60                 printf("cli_session_setup returned %s\n", nt_errstr(status));
61                 return false;
62         }
63
64         status = cli_tree_connect(cli, share, "?????", "", 0);
65         if (!NT_STATUS_IS_OK(status)) {
66                 printf("cli_tree_connect returned %s\n", nt_errstr(status));
67                 return false;
68         }
69
70         status = smb2cli_create(cli, "smb2-basic.txt",
71                         SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
72                         SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
73                         SEC_STD_ALL | SEC_FILE_ALL, /* desired_access, */
74                         FILE_ATTRIBUTE_NORMAL, /* file_attributes, */
75                         FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
76                         FILE_CREATE, /* create_disposition, */
77                         FILE_DELETE_ON_CLOSE, /* create_options, */
78                         NULL, /* smb2_create_blobs *blobs */
79                         &fid_persistent,
80                         &fid_volatile);
81         if (!NT_STATUS_IS_OK(status)) {
82                 printf("smb2cli_create returned %s\n", nt_errstr(status));
83                 return false;
84         }
85
86         status = smb2cli_write(cli, strlen(hello), 0, fid_persistent,
87                                fid_volatile, 0, 0, (const uint8_t *)hello);
88         if (!NT_STATUS_IS_OK(status)) {
89                 printf("smb2cli_write returned %s\n", nt_errstr(status));
90                 return false;
91         }
92
93         status = smb2cli_flush(cli, fid_persistent, fid_volatile);
94         if (!NT_STATUS_IS_OK(status)) {
95                 printf("smb2cli_flush returned %s\n", nt_errstr(status));
96                 return false;
97         }
98
99         status = smb2cli_read(cli, 0x10000, 0, fid_persistent,
100                                fid_volatile, 2, 0,
101                                talloc_tos(), &result, &nread);
102         if (!NT_STATUS_IS_OK(status)) {
103                 printf("smb2cli_read returned %s\n", nt_errstr(status));
104                 return false;
105         }
106
107         if (nread != strlen(hello)) {
108                 printf("smb2cli_read returned %d bytes, expected %d\n",
109                        (int)nread, (int)strlen(hello));
110                 return false;
111         }
112
113         if (memcmp(hello, result, nread) != 0) {
114                 printf("smb2cli_read returned '%s', expected '%s'\n",
115                        result, hello);
116                 return false;
117         }
118
119         status = smb2cli_close(cli, 0, fid_persistent, fid_volatile);
120         if (!NT_STATUS_IS_OK(status)) {
121                 printf("smb2cli_close returned %s\n", nt_errstr(status));
122                 return false;
123         }
124
125         status = smb2cli_create(cli, "",
126                         SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
127                         SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
128                         SEC_STD_SYNCHRONIZE|
129                         SEC_DIR_LIST|
130                         SEC_DIR_READ_ATTRIBUTE, /* desired_access, */
131                         0, /* file_attributes, */
132                         FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
133                         FILE_OPEN, /* create_disposition, */
134                         FILE_SYNCHRONOUS_IO_NONALERT|FILE_DIRECTORY_FILE, /* create_options, */
135                         NULL, /* smb2_create_blobs *blobs */
136                         &fid_persistent,
137                         &fid_volatile);
138         if (!NT_STATUS_IS_OK(status)) {
139                 printf("smb2cli_create returned %s\n", nt_errstr(status));
140                 return false;
141         }
142
143         status = smb2cli_query_directory(
144                 cli, 1, 0, 0, fid_persistent, fid_volatile, "*", 0xffff,
145                 talloc_tos(), &dir_data, &dir_data_length);
146
147         if (!NT_STATUS_IS_OK(status)) {
148                 printf("smb2cli_query_directory returned %s\n", nt_errstr(status));
149                 return false;
150         }
151
152         status = smb2cli_close(cli, 0, fid_persistent, fid_volatile);
153         if (!NT_STATUS_IS_OK(status)) {
154                 printf("smb2cli_close returned %s\n", nt_errstr(status));
155                 return false;
156         }
157
158         return true;
159 }
160
161 bool run_smb2_negprot(int dummy)
162 {
163         struct cli_state *cli;
164         NTSTATUS status;
165         enum protocol_types protocol;
166         const char *name = NULL;
167
168         printf("Starting SMB2-NEGPROT\n");
169
170         if (!torture_init_connection(&cli)) {
171                 return false;
172         }
173         cli->smb2.pid = 0xFEFF;
174
175         status = smbXcli_negprot(cli->conn, cli->timeout,
176                                  PROTOCOL_CORE, PROTOCOL_SMB2_22);
177         if (!NT_STATUS_IS_OK(status)) {
178                 printf("smbXcli_negprot returned %s\n", nt_errstr(status));
179                 return false;
180         }
181
182         protocol = smbXcli_conn_protocol(cli->conn);
183
184         switch (protocol) {
185         case PROTOCOL_SMB2_02:
186                 name = "SMB2_02";
187                 break;
188         case PROTOCOL_SMB2_10:
189                 name = "SMB2_10";
190                 break;
191         case PROTOCOL_SMB2_22:
192                 name = "SMB2_22";
193                 break;
194         default:
195                 break;
196         }
197
198         if (name) {
199                 printf("Server supports %s\n", name);
200         } else {
201                 printf("Server DOES NOT support SMB2\n");
202                 return false;
203         }
204
205         status = smbXcli_negprot(cli->conn, cli->timeout,
206                                  protocol, protocol);
207         if (!NT_STATUS_EQUAL(status, NT_STATUS_CONNECTION_RESET) &&
208             !NT_STATUS_EQUAL(status, NT_STATUS_CONNECTION_DISCONNECTED) &&
209             !NT_STATUS_EQUAL(status, NT_STATUS_CONNECTION_ABORTED)) {
210                 printf("2nd smbXcli_negprot should disconnect - returned %s\n",
211                         nt_errstr(status));
212                 return false;
213         }
214
215         if (smbXcli_conn_is_connected(cli->conn)) {
216                 printf("2nd smbXcli_negprot should disconnect "
217                        "- still connected\n");
218                 return false;
219         }
220
221         return true;
222 }