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