s3:torture: add SMB2-SESSION-REAUTH
[Samba/gebeck_regimport.git] / source3 / torture / test_smb2.c
blobd33b7ab3cd33c71ee1d9b9da35844c6731db897e
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;
42 printf("Starting SMB2-BASIC\n");
44 if (!torture_init_connection(&cli)) {
45 return false;
47 cli->smb2.pid = 0xFEFF;
49 status = smbXcli_negprot(cli->conn, cli->timeout,
50 PROTOCOL_SMB2_02, PROTOCOL_SMB2_02);
51 if (!NT_STATUS_IS_OK(status)) {
52 printf("smbXcli_negprot returned %s\n", nt_errstr(status));
53 return false;
56 status = cli_session_setup(cli, username,
57 password, strlen(password),
58 password, strlen(password),
59 workgroup);
60 if (!NT_STATUS_IS_OK(status)) {
61 printf("cli_session_setup returned %s\n", nt_errstr(status));
62 return false;
65 status = cli_tree_connect(cli, share, "?????", "", 0);
66 if (!NT_STATUS_IS_OK(status)) {
67 printf("cli_tree_connect returned %s\n", nt_errstr(status));
68 return false;
71 status = smb2cli_create(cli, "smb2-basic.txt",
72 SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
73 SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
74 SEC_STD_ALL | SEC_FILE_ALL, /* desired_access, */
75 FILE_ATTRIBUTE_NORMAL, /* file_attributes, */
76 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
77 FILE_CREATE, /* create_disposition, */
78 FILE_DELETE_ON_CLOSE, /* create_options, */
79 NULL, /* smb2_create_blobs *blobs */
80 &fid_persistent,
81 &fid_volatile);
82 if (!NT_STATUS_IS_OK(status)) {
83 printf("smb2cli_create returned %s\n", nt_errstr(status));
84 return false;
87 status = smb2cli_write(cli, strlen(hello), 0, fid_persistent,
88 fid_volatile, 0, 0, (const uint8_t *)hello);
89 if (!NT_STATUS_IS_OK(status)) {
90 printf("smb2cli_write returned %s\n", nt_errstr(status));
91 return false;
94 status = smb2cli_flush(cli, fid_persistent, fid_volatile);
95 if (!NT_STATUS_IS_OK(status)) {
96 printf("smb2cli_flush returned %s\n", nt_errstr(status));
97 return false;
100 status = smb2cli_read(cli, 0x10000, 0, fid_persistent,
101 fid_volatile, 2, 0,
102 talloc_tos(), &result, &nread);
103 if (!NT_STATUS_IS_OK(status)) {
104 printf("smb2cli_read returned %s\n", nt_errstr(status));
105 return false;
108 if (nread != strlen(hello)) {
109 printf("smb2cli_read returned %d bytes, expected %d\n",
110 (int)nread, (int)strlen(hello));
111 return false;
114 if (memcmp(hello, result, nread) != 0) {
115 printf("smb2cli_read returned '%s', expected '%s'\n",
116 result, hello);
117 return false;
120 status = smb2cli_close(cli, 0, fid_persistent, fid_volatile);
121 if (!NT_STATUS_IS_OK(status)) {
122 printf("smb2cli_close returned %s\n", nt_errstr(status));
123 return false;
126 status = smb2cli_create(cli, "",
127 SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
128 SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
129 SEC_STD_SYNCHRONIZE|
130 SEC_DIR_LIST|
131 SEC_DIR_READ_ATTRIBUTE, /* desired_access, */
132 0, /* file_attributes, */
133 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
134 FILE_OPEN, /* create_disposition, */
135 FILE_SYNCHRONOUS_IO_NONALERT|FILE_DIRECTORY_FILE, /* create_options, */
136 NULL, /* smb2_create_blobs *blobs */
137 &fid_persistent,
138 &fid_volatile);
139 if (!NT_STATUS_IS_OK(status)) {
140 printf("smb2cli_create returned %s\n", nt_errstr(status));
141 return false;
144 status = smb2cli_query_directory(
145 cli, 1, 0, 0, fid_persistent, fid_volatile, "*", 0xffff,
146 talloc_tos(), &dir_data, &dir_data_length);
148 if (!NT_STATUS_IS_OK(status)) {
149 printf("smb2cli_query_directory returned %s\n", nt_errstr(status));
150 return false;
153 status = smb2cli_close(cli, 0, fid_persistent, fid_volatile);
154 if (!NT_STATUS_IS_OK(status)) {
155 printf("smb2cli_close returned %s\n", nt_errstr(status));
156 return false;
159 return true;
162 bool run_smb2_negprot(int dummy)
164 struct cli_state *cli;
165 NTSTATUS status;
166 enum protocol_types protocol;
167 const char *name = NULL;
169 printf("Starting SMB2-NEGPROT\n");
171 if (!torture_init_connection(&cli)) {
172 return false;
174 cli->smb2.pid = 0xFEFF;
176 status = smbXcli_negprot(cli->conn, cli->timeout,
177 PROTOCOL_CORE, PROTOCOL_SMB2_22);
178 if (!NT_STATUS_IS_OK(status)) {
179 printf("smbXcli_negprot returned %s\n", nt_errstr(status));
180 return false;
183 protocol = smbXcli_conn_protocol(cli->conn);
185 switch (protocol) {
186 case PROTOCOL_SMB2_02:
187 name = "SMB2_02";
188 break;
189 case PROTOCOL_SMB2_10:
190 name = "SMB2_10";
191 break;
192 case PROTOCOL_SMB2_22:
193 name = "SMB2_22";
194 break;
195 default:
196 break;
199 if (name) {
200 printf("Server supports %s\n", name);
201 } else {
202 printf("Server DOES NOT support SMB2\n");
203 return false;
206 status = smbXcli_negprot(cli->conn, cli->timeout,
207 protocol, protocol);
208 if (!NT_STATUS_EQUAL(status, NT_STATUS_CONNECTION_RESET) &&
209 !NT_STATUS_EQUAL(status, NT_STATUS_CONNECTION_DISCONNECTED) &&
210 !NT_STATUS_EQUAL(status, NT_STATUS_CONNECTION_ABORTED)) {
211 printf("2nd smbXcli_negprot should disconnect - returned %s\n",
212 nt_errstr(status));
213 return false;
216 if (smbXcli_conn_is_connected(cli->conn)) {
217 printf("2nd smbXcli_negprot should disconnect "
218 "- still connected\n");
219 return false;
222 return true;
225 bool run_smb2_session_reconnect(int dummy)
227 struct cli_state *cli1;
228 struct cli_state *cli2;
229 NTSTATUS status;
230 bool ok;
231 uint64_t fid_persistent, fid_volatile;
232 struct tevent_context *ev;
233 struct tevent_req *subreq;
234 DATA_BLOB in_blob = data_blob_null;
235 DATA_BLOB out_blob;
236 struct ntlmssp_state *ntlmssp;
237 struct iovec *recv_iov;
238 const char *hello = "Hello, world\n";
239 uint8_t *result;
240 uint32_t nread;
242 printf("Starting SMB2-SESSION-RECONNECT\n");
244 if (!torture_init_connection(&cli1)) {
245 return false;
247 cli1->smb2.pid = 0xFEFF;
249 status = smbXcli_negprot(cli1->conn, cli1->timeout,
250 PROTOCOL_SMB2_02, PROTOCOL_SMB2_22);
251 if (!NT_STATUS_IS_OK(status)) {
252 printf("smbXcli_negprot returned %s\n", nt_errstr(status));
253 return false;
256 status = cli_session_setup(cli1, username,
257 password, strlen(password),
258 password, strlen(password),
259 workgroup);
260 if (!NT_STATUS_IS_OK(status)) {
261 printf("cli_session_setup returned %s\n", nt_errstr(status));
262 return false;
265 status = cli_tree_connect(cli1, share, "?????", "", 0);
266 if (!NT_STATUS_IS_OK(status)) {
267 printf("cli_tree_connect returned %s\n", nt_errstr(status));
268 return false;
271 status = smb2cli_create(cli1, "session-reconnect.txt",
272 SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
273 SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
274 SEC_STD_ALL | SEC_FILE_ALL, /* desired_access, */
275 FILE_ATTRIBUTE_NORMAL, /* file_attributes, */
276 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
277 FILE_CREATE, /* create_disposition, */
278 FILE_DELETE_ON_CLOSE, /* create_options, */
279 NULL, /* smb2_create_blobs *blobs */
280 &fid_persistent,
281 &fid_volatile);
282 if (!NT_STATUS_IS_OK(status)) {
283 printf("smb2cli_create on cli1 %s\n", nt_errstr(status));
284 return false;
287 status = smb2cli_write(cli1, strlen(hello), 0, fid_persistent,
288 fid_volatile, 0, 0, (const uint8_t *)hello);
289 if (!NT_STATUS_IS_OK(status)) {
290 printf("smb2cli_write returned %s\n", nt_errstr(status));
291 return false;
294 status = smb2cli_flush(cli1, fid_persistent, fid_volatile);
295 if (!NT_STATUS_IS_OK(status)) {
296 printf("smb2cli_flush returned %s\n", nt_errstr(status));
297 return false;
300 status = smb2cli_read(cli1, 0x10000, 0, fid_persistent,
301 fid_volatile, 2, 0,
302 talloc_tos(), &result, &nread);
303 if (!NT_STATUS_IS_OK(status)) {
304 printf("smb2cli_read returned %s\n", nt_errstr(status));
305 return false;
308 if (nread != strlen(hello)) {
309 printf("smb2cli_read returned %d bytes, expected %d\n",
310 (int)nread, (int)strlen(hello));
311 return false;
314 if (memcmp(hello, result, nread) != 0) {
315 printf("smb2cli_read returned '%s', expected '%s'\n",
316 result, hello);
317 return false;
320 /* prepare second session */
322 if (!torture_init_connection(&cli2)) {
323 return false;
325 cli2->smb2.pid = 0xFEFF;
327 status = smbXcli_negprot(cli2->conn, cli2->timeout,
328 PROTOCOL_SMB2_02, PROTOCOL_SMB2_22);
329 if (!NT_STATUS_IS_OK(status)) {
330 printf("smbXcli_negprot returned %s\n", nt_errstr(status));
331 return false;
334 status = ntlmssp_client_start(talloc_tos(),
335 lp_netbios_name(),
336 lp_workgroup(),
337 lp_client_ntlmv2_auth(),
338 &ntlmssp);
339 if (!NT_STATUS_IS_OK(status)) {
340 printf("ntlmssp_client_start returned %s\n", nt_errstr(status));
341 return false;
344 ntlmssp_want_feature(ntlmssp,
345 NTLMSSP_FEATURE_SESSION_KEY);
346 status = ntlmssp_set_username(ntlmssp, username);
347 if (!NT_STATUS_IS_OK(status)) {
348 printf("ntlmssp_set_username returned %s\n", nt_errstr(status));
349 return false;
352 status = ntlmssp_set_domain(ntlmssp, workgroup);
353 if (!NT_STATUS_IS_OK(status)) {
354 printf("ntlmssp_set_domain returned %s\n", nt_errstr(status));
355 return false;
358 status = ntlmssp_set_password(ntlmssp, password);
359 if (!NT_STATUS_IS_OK(status)) {
360 printf("ntlmssp_set_password returned %s\n", nt_errstr(status));
361 return false;
364 status = ntlmssp_update(ntlmssp, data_blob_null, &in_blob);
365 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
366 printf("ntlmssp_update returned %s\n", nt_errstr(status));
367 return false;
370 cli2->smb2.session = smbXcli_session_create(cli2, cli2->conn);
372 ev = event_context_init(talloc_tos());
373 if (ev == NULL) {
374 printf("event_context_init() returned NULL\n");
375 return false;
378 subreq = smb2cli_session_setup_send(talloc_tos(), ev,
379 cli2->conn,
380 cli2->timeout,
381 cli2->smb2.session,
382 0x0, /* in_flags */
383 SMB2_CAP_DFS, /* in_capabilities */
384 0, /* in_channel */
385 cli1->smb2.session, /* in_previous_session */
386 &in_blob); /* in_security_buffer */
387 if (subreq == NULL) {
388 printf("smb2cli_session_setup_send() returned NULL\n");
389 return false;
392 ok = tevent_req_poll(subreq, ev);
393 if (!ok) {
394 printf("tevent_req_poll() returned false\n");
395 return false;
398 status = smb2cli_session_setup_recv(subreq, talloc_tos(),
399 NULL, &out_blob);
400 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
401 printf("smb2cli_session_setup_recv returned %s\n",
402 nt_errstr(status));
403 return false;
406 status = ntlmssp_update(ntlmssp, out_blob, &in_blob);
407 if (!NT_STATUS_IS_OK(status)) {
408 printf("ntlmssp_update returned %s\n", nt_errstr(status));
409 return false;
412 subreq = smb2cli_session_setup_send(talloc_tos(), ev,
413 cli2->conn,
414 cli2->timeout,
415 cli2->smb2.session,
416 0x0, /* in_flags */
417 SMB2_CAP_DFS, /* in_capabilities */
418 0, /* in_channel */
419 cli1->smb2.session, /* in_previous_session */
420 &in_blob); /* in_security_buffer */
421 if (subreq == NULL) {
422 printf("smb2cli_session_setup_send() returned NULL\n");
423 return false;
426 ok = tevent_req_poll(subreq, ev);
427 if (!ok) {
428 printf("tevent_req_poll() returned false\n");
429 return false;
432 status = smb2cli_session_setup_recv(subreq, talloc_tos(),
433 &recv_iov, &out_blob);
434 if (!NT_STATUS_IS_OK(status)) {
435 printf("smb2cli_session_setup_recv returned %s\n",
436 nt_errstr(status));
437 return false;
440 status = smb2_signing_check_pdu(ntlmssp->session_key, recv_iov, 3);
441 if (!NT_STATUS_IS_OK(status)) {
442 printf("check pdu returned %s\n", nt_errstr(status));
443 return false;
446 /* check file operation on the old client */
448 status = smb2cli_flush(cli1, fid_persistent, fid_volatile);
449 if (!NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
450 printf("smb2cli_flush returned %s\n", nt_errstr(status));
451 return false;
454 status = cli_tree_connect(cli1, share, "?????", "", 0);
455 if (!NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
456 printf("cli_tree_connect returned %s\n", nt_errstr(status));
457 return false;
461 * checking file operations without signing.
462 * on w2k8r2 at least, flush, read and write also work the same way,
463 * while create gives ACCESS_DENIED without signing
465 status = smb2cli_flush(cli2, fid_persistent, fid_volatile);
466 if (!NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED)) {
467 printf("smb2cli_flush returned %s\n", nt_errstr(status));
468 return false;
471 status = smb2cli_write(cli2, strlen(hello), 0, fid_persistent,
472 fid_volatile, 0, 0, (const uint8_t *)hello);
473 if (!NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED)) {
474 printf("smb2cli_write returned %s\n", nt_errstr(status));
475 return false;
478 status = smb2cli_read(cli2, 0x10000, 0, fid_persistent,
479 fid_volatile, 2, 0,
480 talloc_tos(), &result, &nread);
481 if (!NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED)) {
482 printf("smb2cli_read returned %s\n", nt_errstr(status));
483 return false;
486 status = smb2cli_create(cli2, "session-reconnect.txt",
487 SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
488 SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
489 SEC_STD_ALL | SEC_FILE_ALL, /* desired_access, */
490 FILE_ATTRIBUTE_NORMAL, /* file_attributes, */
491 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
492 FILE_CREATE, /* create_disposition, */
493 FILE_DELETE_ON_CLOSE, /* create_options, */
494 NULL, /* smb2_create_blobs *blobs */
495 &fid_persistent,
496 &fid_volatile);
497 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
498 printf("smb2cli_create on cli2 %s\n", nt_errstr(status));
499 return false;
502 /* now grab the session key and try with signing */
504 status = smb2cli_session_update_session_key(cli2->smb2.session,
505 ntlmssp->session_key,
506 recv_iov);
507 if (!NT_STATUS_IS_OK(status)) {
508 printf("smb2cli_session_update_session_key %s\n", nt_errstr(status));
509 return false;
512 /* the tid seems to be irrelevant at this stage */
514 cli2->smb2.tid = cli1->smb2.tid;
516 status = smb2cli_flush(cli2, fid_persistent, fid_volatile);
517 if (!NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED)) {
518 printf("smb2cli_flush returned %s\n", nt_errstr(status));
519 return false;
522 status = smb2cli_write(cli2, strlen(hello), 0, fid_persistent,
523 fid_volatile, 0, 0, (const uint8_t *)hello);
524 if (!NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED)) {
525 printf("smb2cli_write returned %s\n", nt_errstr(status));
526 return false;
529 status = smb2cli_read(cli2, 0x10000, 0, fid_persistent,
530 fid_volatile, 2, 0,
531 talloc_tos(), &result, &nread);
532 if (!NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED)) {
533 printf("smb2cli_read returned %s\n", nt_errstr(status));
534 return false;
537 status = smb2cli_create(cli2, "session-reconnect.txt",
538 SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
539 SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
540 SEC_STD_ALL | SEC_FILE_ALL, /* desired_access, */
541 FILE_ATTRIBUTE_NORMAL, /* file_attributes, */
542 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
543 FILE_CREATE, /* create_disposition, */
544 FILE_DELETE_ON_CLOSE, /* create_options, */
545 NULL, /* smb2_create_blobs *blobs */
546 &fid_persistent,
547 &fid_volatile);
548 if (!NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED)) {
549 printf("smb2cli_create on cli2 %s\n", nt_errstr(status));
550 return false;
553 /* now do a new tcon and test file calls again */
555 status = cli_tree_connect(cli2, share, "?????", "", 0);
556 if (!NT_STATUS_IS_OK(status)) {
557 printf("cli_tree_connect returned %s\n", nt_errstr(status));
558 return false;
561 status = smb2cli_create(cli2, "session-reconnect.txt",
562 SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
563 SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
564 SEC_STD_ALL | SEC_FILE_ALL, /* desired_access, */
565 FILE_ATTRIBUTE_NORMAL, /* file_attributes, */
566 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
567 FILE_CREATE, /* create_disposition, */
568 FILE_DELETE_ON_CLOSE, /* create_options, */
569 NULL, /* smb2_create_blobs *blobs */
570 &fid_persistent,
571 &fid_volatile);
572 if (!NT_STATUS_IS_OK(status)) {
573 printf("smb2cli_create on cli2 %s\n", nt_errstr(status));
574 return false;
577 status = smb2cli_write(cli2, strlen(hello), 0, fid_persistent,
578 fid_volatile, 0, 0, (const uint8_t *)hello);
579 if (!NT_STATUS_IS_OK(status)) {
580 printf("smb2cli_write returned %s\n", nt_errstr(status));
581 return false;
584 status = smb2cli_flush(cli2, fid_persistent, fid_volatile);
585 if (!NT_STATUS_IS_OK(status)) {
586 printf("smb2cli_flush returned %s\n", nt_errstr(status));
587 return false;
590 status = smb2cli_read(cli2, 0x10000, 0, fid_persistent,
591 fid_volatile, 2, 0,
592 talloc_tos(), &result, &nread);
593 if (!NT_STATUS_IS_OK(status)) {
594 printf("smb2cli_read returned %s\n", nt_errstr(status));
595 return false;
598 if (nread != strlen(hello)) {
599 printf("smb2cli_read returned %d bytes, expected %d\n",
600 (int)nread, (int)strlen(hello));
601 return false;
604 if (memcmp(hello, result, nread) != 0) {
605 printf("smb2cli_read returned '%s', expected '%s'\n",
606 result, hello);
607 return false;
610 return true;
613 bool run_smb2_tcon_dependence(int dummy)
615 struct cli_state *cli;
616 NTSTATUS status;
617 uint64_t fid_persistent, fid_volatile;
618 const char *hello = "Hello, world\n";
619 uint8_t *result;
620 uint32_t nread;
622 printf("Starting SMB2-TCON-DEPENDENCE\n");
624 if (!torture_init_connection(&cli)) {
625 return false;
627 cli->smb2.pid = 0xFEFF;
629 status = smbXcli_negprot(cli->conn, cli->timeout,
630 PROTOCOL_SMB2_02, PROTOCOL_SMB2_22);
631 if (!NT_STATUS_IS_OK(status)) {
632 printf("smbXcli_negprot returned %s\n", nt_errstr(status));
633 return false;
636 status = cli_session_setup(cli, username,
637 password, strlen(password),
638 password, strlen(password),
639 workgroup);
640 if (!NT_STATUS_IS_OK(status)) {
641 printf("cli_session_setup returned %s\n", nt_errstr(status));
642 return false;
645 status = cli_tree_connect(cli, share, "?????", "", 0);
646 if (!NT_STATUS_IS_OK(status)) {
647 printf("cli_tree_connect returned %s\n", nt_errstr(status));
648 return false;
651 status = smb2cli_create(cli, "tcon_depedence.txt",
652 SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
653 SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
654 SEC_STD_ALL | SEC_FILE_ALL, /* desired_access, */
655 FILE_ATTRIBUTE_NORMAL, /* file_attributes, */
656 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
657 FILE_CREATE, /* create_disposition, */
658 FILE_DELETE_ON_CLOSE, /* create_options, */
659 NULL, /* smb2_create_blobs *blobs */
660 &fid_persistent,
661 &fid_volatile);
662 if (!NT_STATUS_IS_OK(status)) {
663 printf("smb2cli_create on cli %s\n", nt_errstr(status));
664 return false;
667 status = smb2cli_write(cli, strlen(hello), 0, fid_persistent,
668 fid_volatile, 0, 0, (const uint8_t *)hello);
669 if (!NT_STATUS_IS_OK(status)) {
670 printf("smb2cli_write returned %s\n", nt_errstr(status));
671 return false;
674 status = smb2cli_flush(cli, fid_persistent, fid_volatile);
675 if (!NT_STATUS_IS_OK(status)) {
676 printf("smb2cli_flush returned %s\n", nt_errstr(status));
677 return false;
680 status = smb2cli_read(cli, 0x10000, 0, fid_persistent,
681 fid_volatile, 2, 0,
682 talloc_tos(), &result, &nread);
683 if (!NT_STATUS_IS_OK(status)) {
684 printf("smb2cli_read returned %s\n", nt_errstr(status));
685 return false;
688 if (nread != strlen(hello)) {
689 printf("smb2cli_read returned %d bytes, expected %d\n",
690 (int)nread, (int)strlen(hello));
691 return false;
694 if (memcmp(hello, result, nread) != 0) {
695 printf("smb2cli_read returned '%s', expected '%s'\n",
696 result, hello);
697 return false;
700 /* check behaviour with wrong tid... */
702 cli->smb2.tid++;
704 status = smb2cli_read(cli, 0x10000, 0, fid_persistent,
705 fid_volatile, 2, 0,
706 talloc_tos(), &result, &nread);
707 if (!NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED)) {
708 printf("smb2cli_read returned %s\n", nt_errstr(status));
709 return false;
712 cli->smb2.tid--;
714 return true;
717 bool run_smb2_multi_channel(int dummy)
719 struct cli_state *cli1;
720 struct cli_state *cli2;
721 NTSTATUS status;
722 bool ok;
723 uint64_t fid_persistent, fid_volatile;
724 struct tevent_context *ev;
725 struct tevent_req *subreq;
726 DATA_BLOB in_blob = data_blob_null;
727 DATA_BLOB out_blob;
728 struct ntlmssp_state *ntlmssp;
729 struct iovec *recv_iov;
730 const char *hello = "Hello, world\n";
731 uint8_t *result;
732 uint32_t nread;
734 printf("Starting SMB2-MULTI-CHANNEL\n");
736 if (!torture_init_connection(&cli1)) {
737 return false;
739 cli1->smb2.pid = 0xFEFF;
741 if (!torture_init_connection(&cli2)) {
742 return false;
744 cli2->smb2.pid = 0xFEFF;
746 status = smbXcli_negprot(cli1->conn, cli1->timeout,
747 PROTOCOL_SMB2_22, PROTOCOL_SMB2_22);
748 if (!NT_STATUS_IS_OK(status)) {
749 printf("smbXcli_negprot returned %s\n", nt_errstr(status));
750 return false;
753 status = smbXcli_negprot(cli2->conn, cli2->timeout,
754 PROTOCOL_SMB2_22, PROTOCOL_SMB2_22);
755 if (!NT_STATUS_IS_OK(status)) {
756 printf("smbXcli_negprot returned %s\n", nt_errstr(status));
757 return false;
760 status = cli_session_setup(cli1, username,
761 password, strlen(password),
762 password, strlen(password),
763 workgroup);
764 if (!NT_STATUS_IS_OK(status)) {
765 printf("smb2cli_sesssetup returned %s\n", nt_errstr(status));
766 return false;
769 status = cli_tree_connect(cli1, share, "?????", "", 0);
770 if (!NT_STATUS_IS_OK(status)) {
771 printf("cli_tree_connect returned %s\n", nt_errstr(status));
772 return false;
775 status = smb2cli_session_create_channel(cli2,
776 cli1->smb2.session,
777 cli2->conn,
778 &cli2->smb2.session);
779 if (!NT_STATUS_IS_OK(status)) {
780 printf("smb2cli_session_create_channel returned %s\n",
781 nt_errstr(status));
782 return false;
785 status = ntlmssp_client_start(talloc_tos(),
786 lp_netbios_name(),
787 lp_workgroup(),
788 lp_client_ntlmv2_auth(),
789 &ntlmssp);
790 if (!NT_STATUS_IS_OK(status)) {
791 printf("ntlmssp_client_start returned %s\n", nt_errstr(status));
792 return false;
795 ntlmssp_want_feature(ntlmssp,
796 NTLMSSP_FEATURE_SESSION_KEY);
797 status = ntlmssp_set_username(ntlmssp, username);
798 if (!NT_STATUS_IS_OK(status)) {
799 printf("ntlmssp_set_username returned %s\n", nt_errstr(status));
800 return false;
803 status = ntlmssp_set_domain(ntlmssp, workgroup);
804 if (!NT_STATUS_IS_OK(status)) {
805 printf("ntlmssp_set_domain returned %s\n", nt_errstr(status));
806 return false;
809 status = ntlmssp_set_password(ntlmssp, password);
810 if (!NT_STATUS_IS_OK(status)) {
811 printf("ntlmssp_set_password returned %s\n", nt_errstr(status));
812 return false;
815 status = ntlmssp_update(ntlmssp, data_blob_null, &in_blob);
816 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
817 printf("ntlmssp_update returned %s\n", nt_errstr(status));
818 return false;
821 ev = event_context_init(talloc_tos());
822 if (ev == NULL) {
823 printf("event_context_init() returned NULL\n");
824 return false;
827 subreq = smb2cli_session_setup_send(talloc_tos(), ev,
828 cli2->conn,
829 cli2->timeout,
830 cli2->smb2.session,
831 0x01, /* in_flags */
832 SMB2_CAP_DFS, /* in_capabilities */
833 0, /* in_channel */
834 NULL, /* in_previous_session */
835 &in_blob); /* in_security_buffer */
836 if (subreq == NULL) {
837 printf("smb2cli_session_setup_send() returned NULL\n");
838 return false;
841 ok = tevent_req_poll(subreq, ev);
842 if (!ok) {
843 printf("tevent_req_poll() returned false\n");
844 return false;
847 status = smb2cli_session_setup_recv(subreq, talloc_tos(),
848 NULL, &out_blob);
849 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
850 printf("smb2cli_session_setup_recv returned %s\n",
851 nt_errstr(status));
852 return false;
855 status = ntlmssp_update(ntlmssp, out_blob, &in_blob);
856 if (!NT_STATUS_IS_OK(status)) {
857 printf("ntlmssp_update returned %s\n", nt_errstr(status));
858 return false;
861 subreq = smb2cli_session_setup_send(talloc_tos(), ev,
862 cli2->conn,
863 cli2->timeout,
864 cli2->smb2.session,
865 0x01, /* in_flags */
866 SMB2_CAP_DFS, /* in_capabilities */
867 0, /* in_channel */
868 NULL, /* in_previous_session */
869 &in_blob); /* in_security_buffer */
870 if (subreq == NULL) {
871 printf("smb2cli_session_setup_send() returned NULL\n");
872 return false;
875 ok = tevent_req_poll(subreq, ev);
876 if (!ok) {
877 printf("tevent_req_poll() returned false\n");
878 return false;
881 status = smb2cli_session_setup_recv(subreq, talloc_tos(),
882 &recv_iov, &out_blob);
883 if (!NT_STATUS_IS_OK(status)) {
884 printf("smb2cli_session_setup_recv returned %s\n",
885 nt_errstr(status));
886 return false;
889 status = smb2cli_session_update_session_key(cli2->smb2.session,
890 ntlmssp->session_key,
891 recv_iov);
892 if (!NT_STATUS_IS_OK(status)) {
893 printf("smb2cli_session_update_session_key %s\n", nt_errstr(status));
894 return false;
897 cli2->smb2.tid = cli1->smb2.tid;
899 status = smb2cli_create(cli2, "multi-channel.txt",
900 SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
901 SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
902 SEC_STD_ALL | SEC_FILE_ALL, /* desired_access, */
903 FILE_ATTRIBUTE_NORMAL, /* file_attributes, */
904 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
905 FILE_CREATE, /* create_disposition, */
906 FILE_DELETE_ON_CLOSE, /* create_options, */
907 NULL, /* smb2_create_blobs *blobs */
908 &fid_persistent,
909 &fid_volatile);
910 if (!NT_STATUS_IS_OK(status)) {
911 printf("smb2cli_create on cli2 %s\n", nt_errstr(status));
912 return false;
915 status = smb2cli_write(cli1, strlen(hello), 0, fid_persistent,
916 fid_volatile, 0, 0, (const uint8_t *)hello);
917 if (!NT_STATUS_IS_OK(status)) {
918 printf("smb2cli_write returned %s\n", nt_errstr(status));
919 return false;
922 status = smb2cli_flush(cli2, fid_persistent, fid_volatile);
923 if (!NT_STATUS_IS_OK(status)) {
924 printf("smb2cli_flush returned %s\n", nt_errstr(status));
925 return false;
928 status = smb2cli_flush(cli1, fid_persistent, fid_volatile);
929 if (!NT_STATUS_IS_OK(status)) {
930 printf("smb2cli_flush returned %s\n", nt_errstr(status));
931 return false;
934 status = smb2cli_read(cli2, 0x10000, 0, fid_persistent,
935 fid_volatile, 2, 0,
936 talloc_tos(), &result, &nread);
937 if (!NT_STATUS_IS_OK(status)) {
938 printf("smb2cli_read returned %s\n", nt_errstr(status));
939 return false;
942 if (nread != strlen(hello)) {
943 printf("smb2cli_read returned %d bytes, expected %d\n",
944 (int)nread, (int)strlen(hello));
945 return false;
948 if (memcmp(hello, result, nread) != 0) {
949 printf("smb2cli_read returned '%s', expected '%s'\n",
950 result, hello);
951 return false;
954 status = smb2cli_close(cli1, 0, fid_persistent, fid_volatile);
955 if (!NT_STATUS_IS_OK(status)) {
956 printf("smb2cli_close returned %s\n", nt_errstr(status));
957 return false;
960 status = smb2cli_flush(cli2, fid_persistent, fid_volatile);
961 if (!NT_STATUS_IS_OK(status)) {
962 printf("smb2cli_flush returned %s\n", nt_errstr(status));
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));
970 return true;
973 bool run_smb2_session_reauth(int dummy)
975 struct cli_state *cli;
976 NTSTATUS status;
977 bool ok;
978 uint64_t fid_persistent, fid_volatile;
979 struct tevent_context *ev;
980 struct tevent_req *subreq;
981 DATA_BLOB in_blob = data_blob_null;
982 DATA_BLOB out_blob;
983 struct ntlmssp_state *ntlmssp;
984 struct iovec *recv_iov;
986 printf("Starting SMB2-SESSION_REAUTH\n");
988 if (!torture_init_connection(&cli)) {
989 return false;
991 cli->smb2.pid = 0xFEFF;
994 * PROTOCOL_SMB2_22 has a bug in win8pre0
995 * it behaves like PROTOCOL_SMB2_02
996 * and returns NT_STATUS_REQUEST_NOT_ACCEPTED,
997 * while it allows it on PROTOCOL_SMB2_02.
999 status = smbXcli_negprot(cli->conn, cli->timeout,
1000 PROTOCOL_SMB2_10, PROTOCOL_SMB2_10);
1001 if (!NT_STATUS_IS_OK(status)) {
1002 printf("smbXcli_negprot returned %s\n", nt_errstr(status));
1003 return false;
1006 status = cli_session_setup(cli, username,
1007 password, strlen(password),
1008 password, strlen(password),
1009 workgroup);
1010 if (!NT_STATUS_IS_OK(status)) {
1011 printf("smb2cli_sesssetup returned %s\n", nt_errstr(status));
1012 return false;
1015 status = cli_tree_connect(cli, share, "?????", "", 0);
1016 if (!NT_STATUS_IS_OK(status)) {
1017 printf("cli_tree_connect returned %s\n", nt_errstr(status));
1018 return false;
1021 status = smb2cli_create(cli, "session-reauth.txt",
1022 SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
1023 SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
1024 SEC_STD_ALL | SEC_FILE_ALL, /* desired_access, */
1025 FILE_ATTRIBUTE_NORMAL, /* file_attributes, */
1026 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
1027 FILE_CREATE, /* create_disposition, */
1028 FILE_DELETE_ON_CLOSE, /* create_options, */
1029 NULL, /* smb2_create_blobs *blobs */
1030 &fid_persistent,
1031 &fid_volatile);
1032 if (!NT_STATUS_IS_OK(status)) {
1033 printf("smb2cli_create %s\n", nt_errstr(status));
1034 return false;
1037 status = ntlmssp_client_start(talloc_tos(),
1038 lp_netbios_name(),
1039 lp_workgroup(),
1040 lp_client_ntlmv2_auth(),
1041 &ntlmssp);
1042 if (!NT_STATUS_IS_OK(status)) {
1043 printf("ntlmssp_client_start returned %s\n", nt_errstr(status));
1044 return false;
1047 ntlmssp_want_feature(ntlmssp,
1048 NTLMSSP_FEATURE_SESSION_KEY);
1049 status = ntlmssp_set_username(ntlmssp, username);
1050 if (!NT_STATUS_IS_OK(status)) {
1051 printf("ntlmssp_set_username returned %s\n", nt_errstr(status));
1052 return false;
1055 status = ntlmssp_set_domain(ntlmssp, workgroup);
1056 if (!NT_STATUS_IS_OK(status)) {
1057 printf("ntlmssp_set_domain returned %s\n", nt_errstr(status));
1058 return false;
1061 status = ntlmssp_set_password(ntlmssp, password);
1062 if (!NT_STATUS_IS_OK(status)) {
1063 printf("ntlmssp_set_password returned %s\n", nt_errstr(status));
1064 return false;
1067 status = ntlmssp_update(ntlmssp, data_blob_null, &in_blob);
1068 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1069 printf("ntlmssp_update returned %s\n", nt_errstr(status));
1070 return false;
1073 ev = event_context_init(talloc_tos());
1074 if (ev == NULL) {
1075 printf("event_context_init() returned NULL\n");
1076 return false;
1079 subreq = smb2cli_session_setup_send(talloc_tos(), ev,
1080 cli->conn,
1081 cli->timeout,
1082 cli->smb2.session,
1083 0x0, /* in_flags */
1084 SMB2_CAP_DFS, /* in_capabilities */
1085 0, /* in_channel */
1086 NULL, /* in_previous_session */
1087 &in_blob); /* in_security_buffer */
1088 if (subreq == NULL) {
1089 printf("smb2cli_session_setup_send() returned NULL\n");
1090 return false;
1093 ok = tevent_req_poll(subreq, ev);
1094 if (!ok) {
1095 printf("tevent_req_poll() returned false\n");
1096 return false;
1099 status = smb2cli_session_setup_recv(subreq, talloc_tos(),
1100 NULL, &out_blob);
1101 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1102 printf("smb2cli_session_setup_recv returned %s\n",
1103 nt_errstr(status));
1104 return false;
1107 status = ntlmssp_update(ntlmssp, out_blob, &in_blob);
1108 if (!NT_STATUS_IS_OK(status)) {
1109 printf("ntlmssp_update returned %s\n", nt_errstr(status));
1110 return false;
1113 subreq = smb2cli_session_setup_send(talloc_tos(), ev,
1114 cli->conn,
1115 cli->timeout,
1116 cli->smb2.session,
1117 0x0, /* in_flags */
1118 SMB2_CAP_DFS, /* in_capabilities */
1119 0, /* in_channel */
1120 NULL, /* in_previous_session */
1121 &in_blob); /* in_security_buffer */
1122 if (subreq == NULL) {
1123 printf("smb2cli_session_setup_send() returned NULL\n");
1124 return false;
1127 ok = tevent_req_poll(subreq, ev);
1128 if (!ok) {
1129 printf("tevent_req_poll() returned false\n");
1130 return false;
1133 status = smb2cli_session_setup_recv(subreq, talloc_tos(),
1134 &recv_iov, &out_blob);
1135 if (!NT_STATUS_IS_OK(status)) {
1136 printf("smb2cli_session_setup_recv returned %s\n",
1137 nt_errstr(status));
1138 return false;
1141 status = smb2cli_session_update_session_key(cli->smb2.session,
1142 ntlmssp->session_key,
1143 recv_iov);
1144 if (!NT_STATUS_IS_OK(status)) {
1145 printf("smb2cli_session_update_session_key %s\n", nt_errstr(status));
1146 return false;
1149 status = smb2cli_flush(cli, fid_persistent, fid_volatile);
1150 if (!NT_STATUS_IS_OK(status)) {
1151 printf("smb2cli_flush returned %s\n", nt_errstr(status));
1152 return false;
1155 status = smb2cli_close(cli, 0, fid_persistent, fid_volatile);
1156 if (!NT_STATUS_IS_OK(status)) {
1157 printf("smb2cli_close returned %s\n", nt_errstr(status));
1158 return false;
1161 status = smb2cli_create(cli, "multi-channel.txt",
1162 SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
1163 SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
1164 SEC_STD_ALL | SEC_FILE_ALL, /* desired_access, */
1165 FILE_ATTRIBUTE_NORMAL, /* file_attributes, */
1166 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
1167 FILE_CREATE, /* create_disposition, */
1168 FILE_DELETE_ON_CLOSE, /* create_options, */
1169 NULL, /* smb2_create_blobs *blobs */
1170 &fid_persistent,
1171 &fid_volatile);
1172 if (!NT_STATUS_IS_OK(status)) {
1173 printf("smb2cli_create %s\n", nt_errstr(status));
1174 return false;
1177 status = smb2cli_close(cli, 0, fid_persistent, fid_volatile);
1178 if (!NT_STATUS_IS_OK(status)) {
1179 printf("smb2cli_close returned %s\n", nt_errstr(status));
1180 return false;
1183 return true;