2 Unix SMB/CIFS implementation.
3 Initial test for the smb2 client lib
4 Copyright (C) Volker Lendecke 2011
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.
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.
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/>.
21 #include "torture/proto.h"
23 #include "../libcli/smb/smbXcli_base.h"
24 #include "libsmb/smb2cli.h"
25 #include "libcli/security/security.h"
27 extern fstring host, workgroup, share, password, username, myname;
29 bool run_smb2_basic(int dummy)
31 struct cli_state *cli;
33 uint64_t fid_persistent, fid_volatile;
34 const char *hello = "Hello, world\n";
38 uint32_t dir_data_length;
40 printf("Starting SMB2-BASIC\n");
42 if (!torture_init_connection(&cli)) {
45 cli->smb2.pid = 0xFEFF;
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));
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));
60 status = smb2cli_tcon(cli, share);
61 if (!NT_STATUS_IS_OK(status)) {
62 printf("smb2cli_tcon returned %s\n", nt_errstr(status));
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 */
77 if (!NT_STATUS_IS_OK(status)) {
78 printf("smb2cli_create returned %s\n", nt_errstr(status));
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));
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));
95 status = smb2cli_read(cli, 0x10000, 0, fid_persistent,
97 talloc_tos(), &result, &nread);
98 if (!NT_STATUS_IS_OK(status)) {
99 printf("smb2cli_read returned %s\n", nt_errstr(status));
103 if (nread != strlen(hello)) {
104 printf("smb2cli_read returned %d bytes, expected %d\n",
105 (int)nread, (int)strlen(hello));
109 if (memcmp(hello, result, nread) != 0) {
110 printf("smb2cli_read returned '%s', expected '%s'\n",
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));
121 status = smb2cli_create(cli, "",
122 SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
123 SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
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 */
134 if (!NT_STATUS_IS_OK(status)) {
135 printf("smb2cli_create returned %s\n", nt_errstr(status));
139 status = smb2cli_query_directory(
140 cli, 1, 0, 0, fid_persistent, fid_volatile, "*", 0xffff,
141 talloc_tos(), &dir_data, &dir_data_length);
143 if (!NT_STATUS_IS_OK(status)) {
144 printf("smb2cli_query_directory returned %s\n", nt_errstr(status));
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));
157 bool run_smb2_negprot(int dummy)
159 struct cli_state *cli;
161 enum protocol_types protocol;
162 const char *name = NULL;
164 printf("Starting SMB2-NEGPROT\n");
166 if (!torture_init_connection(&cli)) {
169 cli->smb2.pid = 0xFEFF;
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));
178 protocol = smbXcli_conn_protocol(cli->conn);
181 case PROTOCOL_SMB2_02:
184 case PROTOCOL_SMB2_10:
187 case PROTOCOL_SMB2_22:
195 printf("Server supports %s\n", name);
197 printf("Server DOES NOT support SMB2\n");
201 status = smbXcli_negprot(cli->conn, cli->timeout,
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",
211 if (smbXcli_conn_is_connected(cli->conn)) {
212 printf("2nd smbXcli_negprot should disconnect "
213 "- still connected\n");