vfs: Make function pointer names consistent. They all end in _fn
[Samba/gebeck_regimport.git] / source3 / torture / test_smb2.c
blob34fac4d958d1bba177b0ca7756c2a59028d37413
1 /*
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/>.
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 #include "../auth/ntlmssp/ntlmssp.h"
29 extern fstring host, workgroup, share, password, username, myname;
31 bool run_smb2_basic(int dummy)
33 struct cli_state *cli;
34 NTSTATUS status;
35 uint64_t fid_persistent, fid_volatile;
36 const char *hello = "Hello, world\n";
37 uint8_t *result;
38 uint32_t nread;
39 uint8_t *dir_data;
40 uint32_t dir_data_length;
41 uint32_t saved_tid = 0;
42 uint64_t saved_uid = 0;
44 printf("Starting SMB2-BASIC\n");
46 if (!torture_init_connection(&cli)) {
47 return false;
49 cli->smb2.pid = 0xFEFF;
51 status = smbXcli_negprot(cli->conn, cli->timeout,
52 PROTOCOL_SMB2_02, PROTOCOL_SMB2_02);
53 if (!NT_STATUS_IS_OK(status)) {
54 printf("smbXcli_negprot returned %s\n", nt_errstr(status));
55 return false;
58 status = cli_session_setup(cli, username,
59 password, strlen(password),
60 password, strlen(password),
61 workgroup);
62 if (!NT_STATUS_IS_OK(status)) {
63 printf("cli_session_setup returned %s\n", nt_errstr(status));
64 return false;
67 status = cli_tree_connect(cli, share, "?????", "", 0);
68 if (!NT_STATUS_IS_OK(status)) {
69 printf("cli_tree_connect returned %s\n", nt_errstr(status));
70 return false;
73 status = smb2cli_create(cli, "smb2-basic.txt",
74 SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
75 SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
76 SEC_STD_ALL | SEC_FILE_ALL, /* desired_access, */
77 FILE_ATTRIBUTE_NORMAL, /* file_attributes, */
78 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
79 FILE_CREATE, /* create_disposition, */
80 FILE_DELETE_ON_CLOSE, /* create_options, */
81 NULL, /* smb2_create_blobs *blobs */
82 &fid_persistent,
83 &fid_volatile);
84 if (!NT_STATUS_IS_OK(status)) {
85 printf("smb2cli_create returned %s\n", nt_errstr(status));
86 return false;
89 status = smb2cli_write(cli, strlen(hello), 0, fid_persistent,
90 fid_volatile, 0, 0, (const uint8_t *)hello);
91 if (!NT_STATUS_IS_OK(status)) {
92 printf("smb2cli_write returned %s\n", nt_errstr(status));
93 return false;
96 status = smb2cli_flush(cli, fid_persistent, fid_volatile);
97 if (!NT_STATUS_IS_OK(status)) {
98 printf("smb2cli_flush returned %s\n", nt_errstr(status));
99 return false;
102 status = smb2cli_read(cli, 0x10000, 0, fid_persistent,
103 fid_volatile, 2, 0,
104 talloc_tos(), &result, &nread);
105 if (!NT_STATUS_IS_OK(status)) {
106 printf("smb2cli_read returned %s\n", nt_errstr(status));
107 return false;
110 if (nread != strlen(hello)) {
111 printf("smb2cli_read returned %d bytes, expected %d\n",
112 (int)nread, (int)strlen(hello));
113 return false;
116 if (memcmp(hello, result, nread) != 0) {
117 printf("smb2cli_read returned '%s', expected '%s'\n",
118 result, hello);
119 return false;
122 status = smb2cli_close(cli, 0, fid_persistent, fid_volatile);
123 if (!NT_STATUS_IS_OK(status)) {
124 printf("smb2cli_close returned %s\n", nt_errstr(status));
125 return false;
128 status = smb2cli_create(cli, "",
129 SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
130 SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
131 SEC_STD_SYNCHRONIZE|
132 SEC_DIR_LIST|
133 SEC_DIR_READ_ATTRIBUTE, /* desired_access, */
134 0, /* file_attributes, */
135 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
136 FILE_OPEN, /* create_disposition, */
137 FILE_SYNCHRONOUS_IO_NONALERT|FILE_DIRECTORY_FILE, /* create_options, */
138 NULL, /* smb2_create_blobs *blobs */
139 &fid_persistent,
140 &fid_volatile);
141 if (!NT_STATUS_IS_OK(status)) {
142 printf("smb2cli_create returned %s\n", nt_errstr(status));
143 return false;
146 status = smb2cli_query_directory(
147 cli, 1, 0, 0, fid_persistent, fid_volatile, "*", 0xffff,
148 talloc_tos(), &dir_data, &dir_data_length);
150 if (!NT_STATUS_IS_OK(status)) {
151 printf("smb2cli_query_directory returned %s\n", nt_errstr(status));
152 return false;
155 status = smb2cli_close(cli, 0, fid_persistent, fid_volatile);
156 if (!NT_STATUS_IS_OK(status)) {
157 printf("smb2cli_close returned %s\n", nt_errstr(status));
158 return false;
161 saved_tid = cli->smb2.tid;
162 status = smb2cli_tdis(cli);
163 if (!NT_STATUS_IS_OK(status)) {
164 printf("smb2cli_tdis returned %s\n", nt_errstr(status));
165 return false;
167 cli->smb2.tid = saved_tid;
169 status = smb2cli_tdis(cli);
170 if (!NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED)) {
171 printf("2nd smb2cli_tdis returned %s\n", nt_errstr(status));
172 return false;
175 saved_uid = smb2cli_session_current_id(cli->smb2.session);
176 status = smb2cli_logoff(cli);
177 if (!NT_STATUS_IS_OK(status)) {
178 printf("smb2cli_logoff returned %s\n", nt_errstr(status));
179 return false;
182 cli->smb2.session = smbXcli_session_create(cli, cli->conn);
183 if (cli->smb2.session == NULL) {
184 printf("smbXcli_session_create() returned NULL\n");
185 return false;
188 smb2cli_session_set_id_and_flags(cli->smb2.session, saved_uid, 0);
190 status = smb2cli_logoff(cli);
191 if (!NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
192 printf("2nd smb2cli_logoff returned %s\n", nt_errstr(status));
193 return false;
196 return true;
199 bool run_smb2_negprot(int dummy)
201 struct cli_state *cli;
202 NTSTATUS status;
203 enum protocol_types protocol;
204 const char *name = NULL;
206 printf("Starting SMB2-NEGPROT\n");
208 if (!torture_init_connection(&cli)) {
209 return false;
211 cli->smb2.pid = 0xFEFF;
213 status = smbXcli_negprot(cli->conn, cli->timeout,
214 PROTOCOL_CORE, PROTOCOL_SMB2_22);
215 if (!NT_STATUS_IS_OK(status)) {
216 printf("smbXcli_negprot returned %s\n", nt_errstr(status));
217 return false;
220 protocol = smbXcli_conn_protocol(cli->conn);
222 switch (protocol) {
223 case PROTOCOL_SMB2_02:
224 name = "SMB2_02";
225 break;
226 case PROTOCOL_SMB2_10:
227 name = "SMB2_10";
228 break;
229 case PROTOCOL_SMB2_22:
230 name = "SMB2_22";
231 break;
232 default:
233 break;
236 if (name) {
237 printf("Server supports %s\n", name);
238 } else {
239 printf("Server DOES NOT support SMB2\n");
240 return false;
243 status = smbXcli_negprot(cli->conn, cli->timeout,
244 protocol, protocol);
245 if (!NT_STATUS_EQUAL(status, NT_STATUS_CONNECTION_RESET) &&
246 !NT_STATUS_EQUAL(status, NT_STATUS_CONNECTION_DISCONNECTED) &&
247 !NT_STATUS_EQUAL(status, NT_STATUS_CONNECTION_ABORTED)) {
248 printf("2nd smbXcli_negprot should disconnect - returned %s\n",
249 nt_errstr(status));
250 return false;
253 if (smbXcli_conn_is_connected(cli->conn)) {
254 printf("2nd smbXcli_negprot should disconnect "
255 "- still connected\n");
256 return false;
259 return true;
262 bool run_smb2_session_reconnect(int dummy)
264 struct cli_state *cli1;
265 struct cli_state *cli2;
266 NTSTATUS status;
267 bool ok;
268 uint64_t fid_persistent, fid_volatile;
269 struct tevent_context *ev;
270 struct tevent_req *subreq;
271 DATA_BLOB in_blob = data_blob_null;
272 DATA_BLOB out_blob;
273 struct ntlmssp_state *ntlmssp;
274 struct iovec *recv_iov;
275 const char *hello = "Hello, world\n";
276 uint8_t *result;
277 uint32_t nread;
279 printf("Starting SMB2-SESSION-RECONNECT\n");
281 if (!torture_init_connection(&cli1)) {
282 return false;
284 cli1->smb2.pid = 0xFEFF;
286 status = smbXcli_negprot(cli1->conn, cli1->timeout,
287 PROTOCOL_SMB2_02, PROTOCOL_SMB2_22);
288 if (!NT_STATUS_IS_OK(status)) {
289 printf("smbXcli_negprot returned %s\n", nt_errstr(status));
290 return false;
293 status = cli_session_setup(cli1, username,
294 password, strlen(password),
295 password, strlen(password),
296 workgroup);
297 if (!NT_STATUS_IS_OK(status)) {
298 printf("cli_session_setup returned %s\n", nt_errstr(status));
299 return false;
302 status = cli_tree_connect(cli1, share, "?????", "", 0);
303 if (!NT_STATUS_IS_OK(status)) {
304 printf("cli_tree_connect returned %s\n", nt_errstr(status));
305 return false;
308 status = smb2cli_create(cli1, "session-reconnect.txt",
309 SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
310 SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
311 SEC_STD_ALL | SEC_FILE_ALL, /* desired_access, */
312 FILE_ATTRIBUTE_NORMAL, /* file_attributes, */
313 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
314 FILE_CREATE, /* create_disposition, */
315 FILE_DELETE_ON_CLOSE, /* create_options, */
316 NULL, /* smb2_create_blobs *blobs */
317 &fid_persistent,
318 &fid_volatile);
319 if (!NT_STATUS_IS_OK(status)) {
320 printf("smb2cli_create on cli1 %s\n", nt_errstr(status));
321 return false;
324 status = smb2cli_write(cli1, strlen(hello), 0, fid_persistent,
325 fid_volatile, 0, 0, (const uint8_t *)hello);
326 if (!NT_STATUS_IS_OK(status)) {
327 printf("smb2cli_write returned %s\n", nt_errstr(status));
328 return false;
331 status = smb2cli_flush(cli1, fid_persistent, fid_volatile);
332 if (!NT_STATUS_IS_OK(status)) {
333 printf("smb2cli_flush returned %s\n", nt_errstr(status));
334 return false;
337 status = smb2cli_read(cli1, 0x10000, 0, fid_persistent,
338 fid_volatile, 2, 0,
339 talloc_tos(), &result, &nread);
340 if (!NT_STATUS_IS_OK(status)) {
341 printf("smb2cli_read returned %s\n", nt_errstr(status));
342 return false;
345 if (nread != strlen(hello)) {
346 printf("smb2cli_read returned %d bytes, expected %d\n",
347 (int)nread, (int)strlen(hello));
348 return false;
351 if (memcmp(hello, result, nread) != 0) {
352 printf("smb2cli_read returned '%s', expected '%s'\n",
353 result, hello);
354 return false;
357 /* prepare second session */
359 if (!torture_init_connection(&cli2)) {
360 return false;
362 cli2->smb2.pid = 0xFEFF;
364 status = smbXcli_negprot(cli2->conn, cli2->timeout,
365 PROTOCOL_SMB2_02, PROTOCOL_SMB2_22);
366 if (!NT_STATUS_IS_OK(status)) {
367 printf("smbXcli_negprot returned %s\n", nt_errstr(status));
368 return false;
371 status = ntlmssp_client_start(talloc_tos(),
372 lp_netbios_name(),
373 lp_workgroup(),
374 lp_client_ntlmv2_auth(),
375 &ntlmssp);
376 if (!NT_STATUS_IS_OK(status)) {
377 printf("ntlmssp_client_start returned %s\n", nt_errstr(status));
378 return false;
381 ntlmssp_want_feature(ntlmssp,
382 NTLMSSP_FEATURE_SESSION_KEY);
383 status = ntlmssp_set_username(ntlmssp, username);
384 if (!NT_STATUS_IS_OK(status)) {
385 printf("ntlmssp_set_username returned %s\n", nt_errstr(status));
386 return false;
389 status = ntlmssp_set_domain(ntlmssp, workgroup);
390 if (!NT_STATUS_IS_OK(status)) {
391 printf("ntlmssp_set_domain returned %s\n", nt_errstr(status));
392 return false;
395 status = ntlmssp_set_password(ntlmssp, password);
396 if (!NT_STATUS_IS_OK(status)) {
397 printf("ntlmssp_set_password returned %s\n", nt_errstr(status));
398 return false;
401 status = ntlmssp_update(ntlmssp, data_blob_null, &in_blob);
402 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
403 printf("ntlmssp_update returned %s\n", nt_errstr(status));
404 return false;
407 cli2->smb2.session = smbXcli_session_create(cli2, cli2->conn);
409 ev = event_context_init(talloc_tos());
410 if (ev == NULL) {
411 printf("event_context_init() returned NULL\n");
412 return false;
415 subreq = smb2cli_session_setup_send(talloc_tos(), ev,
416 cli2->conn,
417 cli2->timeout,
418 cli2->smb2.session,
419 0x0, /* in_flags */
420 SMB2_CAP_DFS, /* in_capabilities */
421 0, /* in_channel */
422 cli1->smb2.session, /* in_previous_session */
423 &in_blob); /* in_security_buffer */
424 if (subreq == NULL) {
425 printf("smb2cli_session_setup_send() returned NULL\n");
426 return false;
429 ok = tevent_req_poll(subreq, ev);
430 if (!ok) {
431 printf("tevent_req_poll() returned false\n");
432 return false;
435 status = smb2cli_session_setup_recv(subreq, talloc_tos(),
436 NULL, &out_blob);
437 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
438 printf("smb2cli_session_setup_recv returned %s\n",
439 nt_errstr(status));
440 return false;
443 status = ntlmssp_update(ntlmssp, out_blob, &in_blob);
444 if (!NT_STATUS_IS_OK(status)) {
445 printf("ntlmssp_update returned %s\n", nt_errstr(status));
446 return false;
449 subreq = smb2cli_session_setup_send(talloc_tos(), ev,
450 cli2->conn,
451 cli2->timeout,
452 cli2->smb2.session,
453 0x0, /* in_flags */
454 SMB2_CAP_DFS, /* in_capabilities */
455 0, /* in_channel */
456 cli1->smb2.session, /* in_previous_session */
457 &in_blob); /* in_security_buffer */
458 if (subreq == NULL) {
459 printf("smb2cli_session_setup_send() returned NULL\n");
460 return false;
463 ok = tevent_req_poll(subreq, ev);
464 if (!ok) {
465 printf("tevent_req_poll() returned false\n");
466 return false;
469 status = smb2cli_session_setup_recv(subreq, talloc_tos(),
470 &recv_iov, &out_blob);
471 if (!NT_STATUS_IS_OK(status)) {
472 printf("smb2cli_session_setup_recv returned %s\n",
473 nt_errstr(status));
474 return false;
477 status = smb2_signing_check_pdu(ntlmssp->session_key, recv_iov, 3);
478 if (!NT_STATUS_IS_OK(status)) {
479 printf("check pdu returned %s\n", nt_errstr(status));
480 return false;
483 /* check file operation on the old client */
485 status = smb2cli_flush(cli1, fid_persistent, fid_volatile);
486 if (!NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
487 printf("smb2cli_flush returned %s\n", nt_errstr(status));
488 return false;
491 status = cli_tree_connect(cli1, share, "?????", "", 0);
492 if (!NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
493 printf("cli_tree_connect returned %s\n", nt_errstr(status));
494 return false;
498 * checking file operations without signing.
499 * on w2k8r2 at least, flush, read and write also work the same way,
500 * while create gives ACCESS_DENIED without signing
502 status = smb2cli_flush(cli2, fid_persistent, fid_volatile);
503 if (!NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED)) {
504 printf("smb2cli_flush returned %s\n", nt_errstr(status));
505 return false;
508 status = smb2cli_write(cli2, strlen(hello), 0, fid_persistent,
509 fid_volatile, 0, 0, (const uint8_t *)hello);
510 if (!NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED)) {
511 printf("smb2cli_write returned %s\n", nt_errstr(status));
512 return false;
515 status = smb2cli_read(cli2, 0x10000, 0, fid_persistent,
516 fid_volatile, 2, 0,
517 talloc_tos(), &result, &nread);
518 if (!NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED)) {
519 printf("smb2cli_read returned %s\n", nt_errstr(status));
520 return false;
523 status = smb2cli_create(cli2, "session-reconnect.txt",
524 SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
525 SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
526 SEC_STD_ALL | SEC_FILE_ALL, /* desired_access, */
527 FILE_ATTRIBUTE_NORMAL, /* file_attributes, */
528 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
529 FILE_CREATE, /* create_disposition, */
530 FILE_DELETE_ON_CLOSE, /* create_options, */
531 NULL, /* smb2_create_blobs *blobs */
532 &fid_persistent,
533 &fid_volatile);
534 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
535 printf("smb2cli_create on cli2 %s\n", nt_errstr(status));
536 return false;
539 /* now grab the session key and try with signing */
541 status = smb2cli_session_update_session_key(cli2->smb2.session,
542 ntlmssp->session_key,
543 recv_iov);
544 if (!NT_STATUS_IS_OK(status)) {
545 printf("smb2cli_session_update_session_key %s\n", nt_errstr(status));
546 return false;
549 /* the tid seems to be irrelevant at this stage */
551 cli2->smb2.tid = cli1->smb2.tid;
553 status = smb2cli_flush(cli2, fid_persistent, fid_volatile);
554 if (!NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED)) {
555 printf("smb2cli_flush returned %s\n", nt_errstr(status));
556 return false;
559 status = smb2cli_write(cli2, strlen(hello), 0, fid_persistent,
560 fid_volatile, 0, 0, (const uint8_t *)hello);
561 if (!NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED)) {
562 printf("smb2cli_write returned %s\n", nt_errstr(status));
563 return false;
566 status = smb2cli_read(cli2, 0x10000, 0, fid_persistent,
567 fid_volatile, 2, 0,
568 talloc_tos(), &result, &nread);
569 if (!NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED)) {
570 printf("smb2cli_read returned %s\n", nt_errstr(status));
571 return false;
574 status = smb2cli_create(cli2, "session-reconnect.txt",
575 SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
576 SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
577 SEC_STD_ALL | SEC_FILE_ALL, /* desired_access, */
578 FILE_ATTRIBUTE_NORMAL, /* file_attributes, */
579 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
580 FILE_CREATE, /* create_disposition, */
581 FILE_DELETE_ON_CLOSE, /* create_options, */
582 NULL, /* smb2_create_blobs *blobs */
583 &fid_persistent,
584 &fid_volatile);
585 if (!NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED)) {
586 printf("smb2cli_create on cli2 %s\n", nt_errstr(status));
587 return false;
590 /* now do a new tcon and test file calls again */
592 status = cli_tree_connect(cli2, share, "?????", "", 0);
593 if (!NT_STATUS_IS_OK(status)) {
594 printf("cli_tree_connect returned %s\n", nt_errstr(status));
595 return false;
598 status = smb2cli_create(cli2, "session-reconnect.txt",
599 SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
600 SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
601 SEC_STD_ALL | SEC_FILE_ALL, /* desired_access, */
602 FILE_ATTRIBUTE_NORMAL, /* file_attributes, */
603 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
604 FILE_CREATE, /* create_disposition, */
605 FILE_DELETE_ON_CLOSE, /* create_options, */
606 NULL, /* smb2_create_blobs *blobs */
607 &fid_persistent,
608 &fid_volatile);
609 if (!NT_STATUS_IS_OK(status)) {
610 printf("smb2cli_create on cli2 %s\n", nt_errstr(status));
611 return false;
614 status = smb2cli_write(cli2, strlen(hello), 0, fid_persistent,
615 fid_volatile, 0, 0, (const uint8_t *)hello);
616 if (!NT_STATUS_IS_OK(status)) {
617 printf("smb2cli_write returned %s\n", nt_errstr(status));
618 return false;
621 status = smb2cli_flush(cli2, fid_persistent, fid_volatile);
622 if (!NT_STATUS_IS_OK(status)) {
623 printf("smb2cli_flush returned %s\n", nt_errstr(status));
624 return false;
627 status = smb2cli_read(cli2, 0x10000, 0, fid_persistent,
628 fid_volatile, 2, 0,
629 talloc_tos(), &result, &nread);
630 if (!NT_STATUS_IS_OK(status)) {
631 printf("smb2cli_read returned %s\n", nt_errstr(status));
632 return false;
635 if (nread != strlen(hello)) {
636 printf("smb2cli_read returned %d bytes, expected %d\n",
637 (int)nread, (int)strlen(hello));
638 return false;
641 if (memcmp(hello, result, nread) != 0) {
642 printf("smb2cli_read returned '%s', expected '%s'\n",
643 result, hello);
644 return false;
647 return true;
650 bool run_smb2_tcon_dependence(int dummy)
652 struct cli_state *cli;
653 NTSTATUS status;
654 uint64_t fid_persistent, fid_volatile;
655 const char *hello = "Hello, world\n";
656 uint8_t *result;
657 uint32_t nread;
659 printf("Starting SMB2-TCON-DEPENDENCE\n");
661 if (!torture_init_connection(&cli)) {
662 return false;
664 cli->smb2.pid = 0xFEFF;
666 status = smbXcli_negprot(cli->conn, cli->timeout,
667 PROTOCOL_SMB2_02, PROTOCOL_SMB2_22);
668 if (!NT_STATUS_IS_OK(status)) {
669 printf("smbXcli_negprot returned %s\n", nt_errstr(status));
670 return false;
673 status = cli_session_setup(cli, username,
674 password, strlen(password),
675 password, strlen(password),
676 workgroup);
677 if (!NT_STATUS_IS_OK(status)) {
678 printf("cli_session_setup returned %s\n", nt_errstr(status));
679 return false;
682 status = cli_tree_connect(cli, share, "?????", "", 0);
683 if (!NT_STATUS_IS_OK(status)) {
684 printf("cli_tree_connect returned %s\n", nt_errstr(status));
685 return false;
688 status = smb2cli_create(cli, "tcon_depedence.txt",
689 SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
690 SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
691 SEC_STD_ALL | SEC_FILE_ALL, /* desired_access, */
692 FILE_ATTRIBUTE_NORMAL, /* file_attributes, */
693 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
694 FILE_CREATE, /* create_disposition, */
695 FILE_DELETE_ON_CLOSE, /* create_options, */
696 NULL, /* smb2_create_blobs *blobs */
697 &fid_persistent,
698 &fid_volatile);
699 if (!NT_STATUS_IS_OK(status)) {
700 printf("smb2cli_create on cli %s\n", nt_errstr(status));
701 return false;
704 status = smb2cli_write(cli, strlen(hello), 0, fid_persistent,
705 fid_volatile, 0, 0, (const uint8_t *)hello);
706 if (!NT_STATUS_IS_OK(status)) {
707 printf("smb2cli_write returned %s\n", nt_errstr(status));
708 return false;
711 status = smb2cli_flush(cli, fid_persistent, fid_volatile);
712 if (!NT_STATUS_IS_OK(status)) {
713 printf("smb2cli_flush returned %s\n", nt_errstr(status));
714 return false;
717 status = smb2cli_read(cli, 0x10000, 0, fid_persistent,
718 fid_volatile, 2, 0,
719 talloc_tos(), &result, &nread);
720 if (!NT_STATUS_IS_OK(status)) {
721 printf("smb2cli_read returned %s\n", nt_errstr(status));
722 return false;
725 if (nread != strlen(hello)) {
726 printf("smb2cli_read returned %d bytes, expected %d\n",
727 (int)nread, (int)strlen(hello));
728 return false;
731 if (memcmp(hello, result, nread) != 0) {
732 printf("smb2cli_read returned '%s', expected '%s'\n",
733 result, hello);
734 return false;
737 /* check behaviour with wrong tid... */
739 cli->smb2.tid++;
741 status = smb2cli_read(cli, 0x10000, 0, fid_persistent,
742 fid_volatile, 2, 0,
743 talloc_tos(), &result, &nread);
744 if (!NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED)) {
745 printf("smb2cli_read returned %s\n", nt_errstr(status));
746 return false;
749 cli->smb2.tid--;
751 return true;
754 bool run_smb2_multi_channel(int dummy)
756 struct cli_state *cli1;
757 struct cli_state *cli2;
758 NTSTATUS status;
759 bool ok;
760 uint64_t fid_persistent, fid_volatile;
761 struct tevent_context *ev;
762 struct tevent_req *subreq;
763 DATA_BLOB in_blob = data_blob_null;
764 DATA_BLOB out_blob;
765 struct ntlmssp_state *ntlmssp;
766 struct iovec *recv_iov;
767 const char *hello = "Hello, world\n";
768 uint8_t *result;
769 uint32_t nread;
771 printf("Starting SMB2-MULTI-CHANNEL\n");
773 if (!torture_init_connection(&cli1)) {
774 return false;
776 cli1->smb2.pid = 0xFEFF;
778 if (!torture_init_connection(&cli2)) {
779 return false;
781 cli2->smb2.pid = 0xFEFF;
783 status = smbXcli_negprot(cli1->conn, cli1->timeout,
784 PROTOCOL_SMB2_22, PROTOCOL_SMB2_22);
785 if (!NT_STATUS_IS_OK(status)) {
786 printf("smbXcli_negprot returned %s\n", nt_errstr(status));
787 return false;
790 status = smbXcli_negprot(cli2->conn, cli2->timeout,
791 PROTOCOL_SMB2_22, PROTOCOL_SMB2_22);
792 if (!NT_STATUS_IS_OK(status)) {
793 printf("smbXcli_negprot returned %s\n", nt_errstr(status));
794 return false;
797 status = cli_session_setup(cli1, username,
798 password, strlen(password),
799 password, strlen(password),
800 workgroup);
801 if (!NT_STATUS_IS_OK(status)) {
802 printf("smb2cli_sesssetup returned %s\n", nt_errstr(status));
803 return false;
806 status = cli_tree_connect(cli1, share, "?????", "", 0);
807 if (!NT_STATUS_IS_OK(status)) {
808 printf("cli_tree_connect returned %s\n", nt_errstr(status));
809 return false;
812 status = smb2cli_session_create_channel(cli2,
813 cli1->smb2.session,
814 cli2->conn,
815 &cli2->smb2.session);
816 if (!NT_STATUS_IS_OK(status)) {
817 printf("smb2cli_session_create_channel returned %s\n",
818 nt_errstr(status));
819 return false;
822 status = ntlmssp_client_start(talloc_tos(),
823 lp_netbios_name(),
824 lp_workgroup(),
825 lp_client_ntlmv2_auth(),
826 &ntlmssp);
827 if (!NT_STATUS_IS_OK(status)) {
828 printf("ntlmssp_client_start returned %s\n", nt_errstr(status));
829 return false;
832 ntlmssp_want_feature(ntlmssp,
833 NTLMSSP_FEATURE_SESSION_KEY);
834 status = ntlmssp_set_username(ntlmssp, username);
835 if (!NT_STATUS_IS_OK(status)) {
836 printf("ntlmssp_set_username returned %s\n", nt_errstr(status));
837 return false;
840 status = ntlmssp_set_domain(ntlmssp, workgroup);
841 if (!NT_STATUS_IS_OK(status)) {
842 printf("ntlmssp_set_domain returned %s\n", nt_errstr(status));
843 return false;
846 status = ntlmssp_set_password(ntlmssp, password);
847 if (!NT_STATUS_IS_OK(status)) {
848 printf("ntlmssp_set_password returned %s\n", nt_errstr(status));
849 return false;
852 status = ntlmssp_update(ntlmssp, data_blob_null, &in_blob);
853 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
854 printf("ntlmssp_update returned %s\n", nt_errstr(status));
855 return false;
858 ev = event_context_init(talloc_tos());
859 if (ev == NULL) {
860 printf("event_context_init() returned NULL\n");
861 return false;
864 subreq = smb2cli_session_setup_send(talloc_tos(), ev,
865 cli2->conn,
866 cli2->timeout,
867 cli2->smb2.session,
868 0x01, /* in_flags */
869 SMB2_CAP_DFS, /* in_capabilities */
870 0, /* in_channel */
871 NULL, /* in_previous_session */
872 &in_blob); /* in_security_buffer */
873 if (subreq == NULL) {
874 printf("smb2cli_session_setup_send() returned NULL\n");
875 return false;
878 ok = tevent_req_poll(subreq, ev);
879 if (!ok) {
880 printf("tevent_req_poll() returned false\n");
881 return false;
884 status = smb2cli_session_setup_recv(subreq, talloc_tos(),
885 NULL, &out_blob);
886 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
887 printf("smb2cli_session_setup_recv returned %s\n",
888 nt_errstr(status));
889 return false;
892 status = ntlmssp_update(ntlmssp, out_blob, &in_blob);
893 if (!NT_STATUS_IS_OK(status)) {
894 printf("ntlmssp_update returned %s\n", nt_errstr(status));
895 return false;
898 subreq = smb2cli_session_setup_send(talloc_tos(), ev,
899 cli2->conn,
900 cli2->timeout,
901 cli2->smb2.session,
902 0x01, /* in_flags */
903 SMB2_CAP_DFS, /* in_capabilities */
904 0, /* in_channel */
905 NULL, /* in_previous_session */
906 &in_blob); /* in_security_buffer */
907 if (subreq == NULL) {
908 printf("smb2cli_session_setup_send() returned NULL\n");
909 return false;
912 ok = tevent_req_poll(subreq, ev);
913 if (!ok) {
914 printf("tevent_req_poll() returned false\n");
915 return false;
918 status = smb2cli_session_setup_recv(subreq, talloc_tos(),
919 &recv_iov, &out_blob);
920 if (!NT_STATUS_IS_OK(status)) {
921 printf("smb2cli_session_setup_recv returned %s\n",
922 nt_errstr(status));
923 return false;
926 status = smb2cli_session_update_session_key(cli2->smb2.session,
927 ntlmssp->session_key,
928 recv_iov);
929 if (!NT_STATUS_IS_OK(status)) {
930 printf("smb2cli_session_update_session_key %s\n", nt_errstr(status));
931 return false;
934 cli2->smb2.tid = cli1->smb2.tid;
936 status = smb2cli_create(cli2, "multi-channel.txt",
937 SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
938 SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
939 SEC_STD_ALL | SEC_FILE_ALL, /* desired_access, */
940 FILE_ATTRIBUTE_NORMAL, /* file_attributes, */
941 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
942 FILE_CREATE, /* create_disposition, */
943 FILE_DELETE_ON_CLOSE, /* create_options, */
944 NULL, /* smb2_create_blobs *blobs */
945 &fid_persistent,
946 &fid_volatile);
947 if (!NT_STATUS_IS_OK(status)) {
948 printf("smb2cli_create on cli2 %s\n", nt_errstr(status));
949 return false;
952 status = smb2cli_write(cli1, strlen(hello), 0, fid_persistent,
953 fid_volatile, 0, 0, (const uint8_t *)hello);
954 if (!NT_STATUS_IS_OK(status)) {
955 printf("smb2cli_write returned %s\n", nt_errstr(status));
956 return false;
959 status = smb2cli_flush(cli2, fid_persistent, fid_volatile);
960 if (!NT_STATUS_IS_OK(status)) {
961 printf("smb2cli_flush returned %s\n", nt_errstr(status));
962 return false;
965 status = smb2cli_flush(cli1, fid_persistent, fid_volatile);
966 if (!NT_STATUS_IS_OK(status)) {
967 printf("smb2cli_flush returned %s\n", nt_errstr(status));
968 return false;
971 status = smb2cli_read(cli2, 0x10000, 0, fid_persistent,
972 fid_volatile, 2, 0,
973 talloc_tos(), &result, &nread);
974 if (!NT_STATUS_IS_OK(status)) {
975 printf("smb2cli_read returned %s\n", nt_errstr(status));
976 return false;
979 if (nread != strlen(hello)) {
980 printf("smb2cli_read returned %d bytes, expected %d\n",
981 (int)nread, (int)strlen(hello));
982 return false;
985 if (memcmp(hello, result, nread) != 0) {
986 printf("smb2cli_read returned '%s', expected '%s'\n",
987 result, hello);
988 return false;
991 status = smb2cli_close(cli1, 0, fid_persistent, fid_volatile);
992 if (!NT_STATUS_IS_OK(status)) {
993 printf("smb2cli_close returned %s\n", nt_errstr(status));
994 return false;
997 status = smb2cli_flush(cli2, fid_persistent, fid_volatile);
998 if (!NT_STATUS_IS_OK(status)) {
999 printf("smb2cli_flush returned %s\n", nt_errstr(status));
1002 status = smb2cli_flush(cli1, fid_persistent, fid_volatile);
1003 if (!NT_STATUS_IS_OK(status)) {
1004 printf("smb2cli_flush returned %s\n", nt_errstr(status));
1007 return true;
1010 bool run_smb2_session_reauth(int dummy)
1012 struct cli_state *cli;
1013 NTSTATUS status;
1014 bool ok;
1015 uint64_t fid_persistent, fid_volatile;
1016 struct tevent_context *ev;
1017 struct tevent_req *subreq;
1018 DATA_BLOB in_blob = data_blob_null;
1019 DATA_BLOB out_blob;
1020 struct ntlmssp_state *ntlmssp;
1021 struct iovec *recv_iov;
1023 printf("Starting SMB2-SESSION_REAUTH\n");
1025 if (!torture_init_connection(&cli)) {
1026 return false;
1028 cli->smb2.pid = 0xFEFF;
1031 * PROTOCOL_SMB2_22 has a bug in win8pre0
1032 * it behaves like PROTOCOL_SMB2_02
1033 * and returns NT_STATUS_REQUEST_NOT_ACCEPTED,
1034 * while it allows it on PROTOCOL_SMB2_02.
1036 status = smbXcli_negprot(cli->conn, cli->timeout,
1037 PROTOCOL_SMB2_10, PROTOCOL_SMB2_10);
1038 if (!NT_STATUS_IS_OK(status)) {
1039 printf("smbXcli_negprot returned %s\n", nt_errstr(status));
1040 return false;
1043 status = cli_session_setup(cli, username,
1044 password, strlen(password),
1045 password, strlen(password),
1046 workgroup);
1047 if (!NT_STATUS_IS_OK(status)) {
1048 printf("smb2cli_sesssetup returned %s\n", nt_errstr(status));
1049 return false;
1052 status = cli_tree_connect(cli, share, "?????", "", 0);
1053 if (!NT_STATUS_IS_OK(status)) {
1054 printf("cli_tree_connect returned %s\n", nt_errstr(status));
1055 return false;
1058 status = smb2cli_create(cli, "session-reauth.txt",
1059 SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
1060 SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
1061 SEC_STD_ALL | SEC_FILE_ALL, /* desired_access, */
1062 FILE_ATTRIBUTE_NORMAL, /* file_attributes, */
1063 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
1064 FILE_CREATE, /* create_disposition, */
1065 FILE_DELETE_ON_CLOSE, /* create_options, */
1066 NULL, /* smb2_create_blobs *blobs */
1067 &fid_persistent,
1068 &fid_volatile);
1069 if (!NT_STATUS_IS_OK(status)) {
1070 printf("smb2cli_create %s\n", nt_errstr(status));
1071 return false;
1074 status = ntlmssp_client_start(talloc_tos(),
1075 lp_netbios_name(),
1076 lp_workgroup(),
1077 lp_client_ntlmv2_auth(),
1078 &ntlmssp);
1079 if (!NT_STATUS_IS_OK(status)) {
1080 printf("ntlmssp_client_start returned %s\n", nt_errstr(status));
1081 return false;
1084 ntlmssp_want_feature(ntlmssp,
1085 NTLMSSP_FEATURE_SESSION_KEY);
1086 status = ntlmssp_set_username(ntlmssp, username);
1087 if (!NT_STATUS_IS_OK(status)) {
1088 printf("ntlmssp_set_username returned %s\n", nt_errstr(status));
1089 return false;
1092 status = ntlmssp_set_domain(ntlmssp, workgroup);
1093 if (!NT_STATUS_IS_OK(status)) {
1094 printf("ntlmssp_set_domain returned %s\n", nt_errstr(status));
1095 return false;
1098 status = ntlmssp_set_password(ntlmssp, password);
1099 if (!NT_STATUS_IS_OK(status)) {
1100 printf("ntlmssp_set_password returned %s\n", nt_errstr(status));
1101 return false;
1104 status = ntlmssp_update(ntlmssp, data_blob_null, &in_blob);
1105 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1106 printf("ntlmssp_update returned %s\n", nt_errstr(status));
1107 return false;
1110 ev = event_context_init(talloc_tos());
1111 if (ev == NULL) {
1112 printf("event_context_init() returned NULL\n");
1113 return false;
1116 subreq = smb2cli_session_setup_send(talloc_tos(), ev,
1117 cli->conn,
1118 cli->timeout,
1119 cli->smb2.session,
1120 0x0, /* in_flags */
1121 SMB2_CAP_DFS, /* in_capabilities */
1122 0, /* in_channel */
1123 NULL, /* in_previous_session */
1124 &in_blob); /* in_security_buffer */
1125 if (subreq == NULL) {
1126 printf("smb2cli_session_setup_send() returned NULL\n");
1127 return false;
1130 ok = tevent_req_poll(subreq, ev);
1131 if (!ok) {
1132 printf("tevent_req_poll() returned false\n");
1133 return false;
1136 status = smb2cli_session_setup_recv(subreq, talloc_tos(),
1137 NULL, &out_blob);
1138 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1139 printf("smb2cli_session_setup_recv returned %s\n",
1140 nt_errstr(status));
1141 return false;
1144 status = ntlmssp_update(ntlmssp, out_blob, &in_blob);
1145 if (!NT_STATUS_IS_OK(status)) {
1146 printf("ntlmssp_update returned %s\n", nt_errstr(status));
1147 return false;
1150 subreq = smb2cli_session_setup_send(talloc_tos(), ev,
1151 cli->conn,
1152 cli->timeout,
1153 cli->smb2.session,
1154 0x0, /* in_flags */
1155 SMB2_CAP_DFS, /* in_capabilities */
1156 0, /* in_channel */
1157 NULL, /* in_previous_session */
1158 &in_blob); /* in_security_buffer */
1159 if (subreq == NULL) {
1160 printf("smb2cli_session_setup_send() returned NULL\n");
1161 return false;
1164 ok = tevent_req_poll(subreq, ev);
1165 if (!ok) {
1166 printf("tevent_req_poll() returned false\n");
1167 return false;
1170 status = smb2cli_session_setup_recv(subreq, talloc_tos(),
1171 &recv_iov, &out_blob);
1172 if (!NT_STATUS_IS_OK(status)) {
1173 printf("smb2cli_session_setup_recv returned %s\n",
1174 nt_errstr(status));
1175 return false;
1178 status = smb2cli_session_update_session_key(cli->smb2.session,
1179 ntlmssp->session_key,
1180 recv_iov);
1181 if (!NT_STATUS_IS_OK(status)) {
1182 printf("smb2cli_session_update_session_key %s\n", nt_errstr(status));
1183 return false;
1186 status = smb2cli_flush(cli, fid_persistent, fid_volatile);
1187 if (!NT_STATUS_IS_OK(status)) {
1188 printf("smb2cli_flush returned %s\n", nt_errstr(status));
1189 return false;
1192 status = smb2cli_close(cli, 0, fid_persistent, fid_volatile);
1193 if (!NT_STATUS_IS_OK(status)) {
1194 printf("smb2cli_close returned %s\n", nt_errstr(status));
1195 return false;
1198 status = smb2cli_create(cli, "multi-channel.txt",
1199 SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
1200 SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
1201 SEC_STD_ALL | SEC_FILE_ALL, /* desired_access, */
1202 FILE_ATTRIBUTE_NORMAL, /* file_attributes, */
1203 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
1204 FILE_CREATE, /* create_disposition, */
1205 FILE_DELETE_ON_CLOSE, /* create_options, */
1206 NULL, /* smb2_create_blobs *blobs */
1207 &fid_persistent,
1208 &fid_volatile);
1209 if (!NT_STATUS_IS_OK(status)) {
1210 printf("smb2cli_create %s\n", nt_errstr(status));
1211 return false;
1214 status = smb2cli_close(cli, 0, fid_persistent, fid_volatile);
1215 if (!NT_STATUS_IS_OK(status)) {
1216 printf("smb2cli_close returned %s\n", nt_errstr(status));
1217 return false;
1220 return true;