libcli/smb: Change smb2cli_create() and smb2cli_create_recv() to return a parameter...
[metze/samba/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 "trans2.h"
24 #include "../libcli/smb/smbXcli_base.h"
25 #include "libsmb/smb2cli.h"
26 #include "libcli/security/security.h"
27 #include "libsmb/proto.h"
28 #include "auth/gensec/gensec.h"
29 #include "auth_generic.h"
30
31 extern fstring host, workgroup, share, password, username, myname;
32
33 bool run_smb2_basic(int dummy)
34 {
35         struct cli_state *cli;
36         NTSTATUS status;
37         uint64_t fid_persistent, fid_volatile;
38         const char *hello = "Hello, world\n";
39         uint8_t *result;
40         uint32_t nread;
41         uint8_t *dir_data;
42         uint32_t dir_data_length;
43         uint32_t saved_tid = 0;
44         struct smbXcli_tcon *saved_tcon = NULL;
45         uint64_t saved_uid = 0;
46
47         printf("Starting SMB2-BASIC\n");
48
49         if (!torture_init_connection(&cli)) {
50                 return false;
51         }
52
53         status = smbXcli_negprot(cli->conn, cli->timeout,
54                                  PROTOCOL_SMB2_02, PROTOCOL_SMB2_02);
55         if (!NT_STATUS_IS_OK(status)) {
56                 printf("smbXcli_negprot returned %s\n", nt_errstr(status));
57                 return false;
58         }
59
60         status = cli_session_setup(cli, username,
61                                    password, strlen(password),
62                                    password, strlen(password),
63                                    workgroup);
64         if (!NT_STATUS_IS_OK(status)) {
65                 printf("cli_session_setup returned %s\n", nt_errstr(status));
66                 return false;
67         }
68
69         status = cli_tree_connect(cli, share, "?????", "", 0);
70         if (!NT_STATUS_IS_OK(status)) {
71                 printf("cli_tree_connect returned %s\n", nt_errstr(status));
72                 return false;
73         }
74
75         status = smb2cli_create(cli->conn, cli->timeout, cli->smb2.session,
76                         cli->smb2.tcon, "smb2-basic.txt",
77                         SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
78                         SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
79                         SEC_STD_ALL | SEC_FILE_ALL, /* desired_access, */
80                         FILE_ATTRIBUTE_NORMAL, /* file_attributes, */
81                         FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
82                         FILE_CREATE, /* create_disposition, */
83                         FILE_DELETE_ON_CLOSE, /* create_options, */
84                         NULL, /* smb2_create_blobs *blobs */
85                         &fid_persistent,
86                         &fid_volatile,
87                         NULL);
88         if (!NT_STATUS_IS_OK(status)) {
89                 printf("smb2cli_create returned %s\n", nt_errstr(status));
90                 return false;
91         }
92
93         status = smb2cli_write(cli->conn, cli->timeout, cli->smb2.session,
94                                cli->smb2.tcon, strlen(hello), 0, fid_persistent,
95                                fid_volatile, 0, 0, (const uint8_t *)hello, NULL);
96         if (!NT_STATUS_IS_OK(status)) {
97                 printf("smb2cli_write returned %s\n", nt_errstr(status));
98                 return false;
99         }
100
101         status = smb2cli_flush(cli->conn, cli->timeout, cli->smb2.session,
102                                cli->smb2.tcon, fid_persistent, fid_volatile);
103         if (!NT_STATUS_IS_OK(status)) {
104                 printf("smb2cli_flush returned %s\n", nt_errstr(status));
105                 return false;
106         }
107
108         status = smb2cli_read(cli->conn, cli->timeout, cli->smb2.session,
109                               cli->smb2.tcon, 0x10000, 0, fid_persistent,
110                               fid_volatile, 2, 0,
111                               talloc_tos(), &result, &nread);
112         if (!NT_STATUS_IS_OK(status)) {
113                 printf("smb2cli_read returned %s\n", nt_errstr(status));
114                 return false;
115         }
116
117         if (nread != strlen(hello)) {
118                 printf("smb2cli_read returned %d bytes, expected %d\n",
119                        (int)nread, (int)strlen(hello));
120                 return false;
121         }
122
123         if (memcmp(hello, result, nread) != 0) {
124                 printf("smb2cli_read returned '%s', expected '%s'\n",
125                        result, hello);
126                 return false;
127         }
128
129         status = smb2cli_close(cli->conn, cli->timeout, cli->smb2.session,
130                                cli->smb2.tcon, 0, fid_persistent, fid_volatile);
131         if (!NT_STATUS_IS_OK(status)) {
132                 printf("smb2cli_close returned %s\n", nt_errstr(status));
133                 return false;
134         }
135
136         status = smb2cli_create(cli->conn, cli->timeout, cli->smb2.session,
137                         cli->smb2.tcon, "",
138                         SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
139                         SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
140                         SEC_STD_SYNCHRONIZE|
141                         SEC_DIR_LIST|
142                         SEC_DIR_READ_ATTRIBUTE, /* desired_access, */
143                         0, /* file_attributes, */
144                         FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
145                         FILE_OPEN, /* create_disposition, */
146                         FILE_SYNCHRONOUS_IO_NONALERT|FILE_DIRECTORY_FILE, /* create_options, */
147                         NULL, /* smb2_create_blobs *blobs */
148                         &fid_persistent,
149                         &fid_volatile,
150                         NULL);
151         if (!NT_STATUS_IS_OK(status)) {
152                 printf("smb2cli_create returned %s\n", nt_errstr(status));
153                 return false;
154         }
155
156         status = smb2cli_query_directory(
157                 cli->conn, cli->timeout, cli->smb2.session, cli->smb2.tcon,
158                 1, 0, 0, fid_persistent, fid_volatile, "*", 0xffff,
159                 talloc_tos(), &dir_data, &dir_data_length);
160
161         if (!NT_STATUS_IS_OK(status)) {
162                 printf("smb2cli_query_directory returned %s\n", nt_errstr(status));
163                 return false;
164         }
165
166         status = smb2cli_close(cli->conn, cli->timeout, cli->smb2.session,
167                                cli->smb2.tcon, 0, fid_persistent, fid_volatile);
168         if (!NT_STATUS_IS_OK(status)) {
169                 printf("smb2cli_close returned %s\n", nt_errstr(status));
170                 return false;
171         }
172
173         saved_tid = smb2cli_tcon_current_id(cli->smb2.tcon);
174         saved_tcon = cli->smb2.tcon;
175         cli->smb2.tcon = smbXcli_tcon_create(cli);
176         smb2cli_tcon_set_values(cli->smb2.tcon,
177                                 NULL, /* session */
178                                 saved_tid,
179                                 0, /* type */
180                                 0, /* flags */
181                                 0, /* capabilities */
182                                 0  /* maximal_access */);
183         status = smb2cli_tdis(cli);
184         if (!NT_STATUS_IS_OK(status)) {
185                 printf("smb2cli_tdis returned %s\n", nt_errstr(status));
186                 return false;
187         }
188         talloc_free(cli->smb2.tcon);
189         cli->smb2.tcon = saved_tcon;
190
191         status = smb2cli_tdis(cli);
192         if (!NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED)) {
193                 printf("2nd smb2cli_tdis returned %s\n", nt_errstr(status));
194                 return false;
195         }
196
197         saved_uid = smb2cli_session_current_id(cli->smb2.session);
198         status = smb2cli_logoff(cli->conn, cli->timeout, cli->smb2.session);
199         if (!NT_STATUS_IS_OK(status)) {
200                 printf("smb2cli_logoff returned %s\n", nt_errstr(status));
201                 return false;
202         }
203
204         cli->smb2.session = smbXcli_session_create(cli, cli->conn);
205         if (cli->smb2.session == NULL) {
206                 printf("smbXcli_session_create() returned NULL\n");
207                 return false;
208         }
209
210         smb2cli_session_set_id_and_flags(cli->smb2.session, saved_uid, 0);
211
212         status = smb2cli_logoff(cli->conn, cli->timeout, cli->smb2.session);
213         if (!NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
214                 printf("2nd smb2cli_logoff returned %s\n", nt_errstr(status));
215                 return false;
216         }
217
218         return true;
219 }
220
221 bool run_smb2_negprot(int dummy)
222 {
223         struct cli_state *cli;
224         NTSTATUS status;
225         enum protocol_types protocol;
226         const char *name = NULL;
227
228         printf("Starting SMB2-NEGPROT\n");
229
230         if (!torture_init_connection(&cli)) {
231                 return false;
232         }
233
234         status = smbXcli_negprot(cli->conn, cli->timeout,
235                                  PROTOCOL_CORE, PROTOCOL_LATEST);
236         if (!NT_STATUS_IS_OK(status)) {
237                 printf("smbXcli_negprot returned %s\n", nt_errstr(status));
238                 return false;
239         }
240
241         protocol = smbXcli_conn_protocol(cli->conn);
242
243         switch (protocol) {
244         case PROTOCOL_SMB2_02:
245                 name = "SMB2_02";
246                 break;
247         case PROTOCOL_SMB2_10:
248                 name = "SMB2_10";
249                 break;
250         case PROTOCOL_SMB2_22:
251                 name = "SMB2_22";
252                 break;
253         case PROTOCOL_SMB2_24:
254                 name = "SMB2_24";
255                 break;
256         case PROTOCOL_SMB3_00:
257                 name = "SMB3_00";
258                 break;
259         default:
260                 break;
261         }
262
263         if (name) {
264                 printf("Server supports %s\n", name);
265         } else {
266                 printf("Server DOES NOT support SMB2\n");
267                 return false;
268         }
269
270         status = smbXcli_negprot(cli->conn, cli->timeout,
271                                  protocol, protocol);
272         if (!NT_STATUS_EQUAL(status, NT_STATUS_CONNECTION_RESET) &&
273             !NT_STATUS_EQUAL(status, NT_STATUS_CONNECTION_DISCONNECTED) &&
274             !NT_STATUS_EQUAL(status, NT_STATUS_CONNECTION_ABORTED)) {
275                 printf("2nd smbXcli_negprot should disconnect - returned %s\n",
276                         nt_errstr(status));
277                 return false;
278         }
279
280         if (smbXcli_conn_is_connected(cli->conn)) {
281                 printf("2nd smbXcli_negprot should disconnect "
282                        "- still connected\n");
283                 return false;
284         }
285
286         return true;
287 }
288
289 bool run_smb2_session_reconnect(int dummy)
290 {
291         struct cli_state *cli1;
292         struct cli_state *cli2;
293         NTSTATUS status;
294         bool ok;
295         uint64_t fid_persistent, fid_volatile;
296         struct tevent_context *ev;
297         struct tevent_req *subreq;
298         DATA_BLOB in_blob = data_blob_null;
299         DATA_BLOB out_blob;
300         DATA_BLOB session_key;
301         struct auth_generic_state *auth_generic_state;
302         struct iovec *recv_iov;
303         const char *hello = "Hello, world\n";
304         uint8_t *result;
305         uint32_t nread;
306
307         printf("Starting SMB2-SESSION-RECONNECT\n");
308
309         if (!torture_init_connection(&cli1)) {
310                 return false;
311         }
312
313         status = smbXcli_negprot(cli1->conn, cli1->timeout,
314                                  PROTOCOL_SMB2_02, PROTOCOL_LATEST);
315         if (!NT_STATUS_IS_OK(status)) {
316                 printf("smbXcli_negprot returned %s\n", nt_errstr(status));
317                 return false;
318         }
319
320         status = cli_session_setup(cli1, username,
321                                    password, strlen(password),
322                                    password, strlen(password),
323                                    workgroup);
324         if (!NT_STATUS_IS_OK(status)) {
325                 printf("cli_session_setup returned %s\n", nt_errstr(status));
326                 return false;
327         }
328
329         status = cli_tree_connect(cli1, share, "?????", "", 0);
330         if (!NT_STATUS_IS_OK(status)) {
331                 printf("cli_tree_connect returned %s\n", nt_errstr(status));
332                 return false;
333         }
334
335         status = smb2cli_create(cli1->conn, cli1->timeout, cli1->smb2.session,
336                         cli1->smb2.tcon, "session-reconnect.txt",
337                         SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
338                         SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
339                         SEC_STD_ALL | SEC_FILE_ALL, /* desired_access, */
340                         FILE_ATTRIBUTE_NORMAL, /* file_attributes, */
341                         FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
342                         FILE_CREATE, /* create_disposition, */
343                         FILE_DELETE_ON_CLOSE, /* create_options, */
344                         NULL, /* smb2_create_blobs *blobs */
345                         &fid_persistent,
346                         &fid_volatile,
347                         NULL);
348         if (!NT_STATUS_IS_OK(status)) {
349                 printf("smb2cli_create on cli1 %s\n", nt_errstr(status));
350                 return false;
351         }
352
353         status = smb2cli_write(cli1->conn, cli1->timeout, cli1->smb2.session,
354                                cli1->smb2.tcon, strlen(hello), 0, fid_persistent,
355                                fid_volatile, 0, 0, (const uint8_t *)hello, NULL);
356         if (!NT_STATUS_IS_OK(status)) {
357                 printf("smb2cli_write returned %s\n", nt_errstr(status));
358                 return false;
359         }
360
361         status = smb2cli_flush(cli1->conn, cli1->timeout, cli1->smb2.session,
362                                cli1->smb2.tcon, fid_persistent, fid_volatile);
363         if (!NT_STATUS_IS_OK(status)) {
364                 printf("smb2cli_flush returned %s\n", nt_errstr(status));
365                 return false;
366         }
367
368         status = smb2cli_read(cli1->conn, cli1->timeout, cli1->smb2.session,
369                               cli1->smb2.tcon, 0x10000, 0, fid_persistent,
370                               fid_volatile, 2, 0,
371                               talloc_tos(), &result, &nread);
372         if (!NT_STATUS_IS_OK(status)) {
373                 printf("smb2cli_read returned %s\n", nt_errstr(status));
374                 return false;
375         }
376
377         if (nread != strlen(hello)) {
378                 printf("smb2cli_read returned %d bytes, expected %d\n",
379                        (int)nread, (int)strlen(hello));
380                 return false;
381         }
382
383         if (memcmp(hello, result, nread) != 0) {
384                 printf("smb2cli_read returned '%s', expected '%s'\n",
385                        result, hello);
386                 return false;
387         }
388
389         /* prepare second session */
390
391         if (!torture_init_connection(&cli2)) {
392                 return false;
393         }
394
395         status = smbXcli_negprot(cli2->conn, cli2->timeout,
396                                  PROTOCOL_SMB2_02, PROTOCOL_LATEST);
397         if (!NT_STATUS_IS_OK(status)) {
398                 printf("smbXcli_negprot returned %s\n", nt_errstr(status));
399                 return false;
400         }
401
402         status = auth_generic_client_prepare(talloc_tos(), &auth_generic_state);
403         if (!NT_STATUS_IS_OK(status)) {
404                 printf("auth_generic_client_prepare returned %s\n", nt_errstr(status));
405                 return false;
406         }
407
408         gensec_want_feature(auth_generic_state->gensec_security,
409                             GENSEC_FEATURE_SESSION_KEY);
410         status = auth_generic_set_username(auth_generic_state, username);
411         if (!NT_STATUS_IS_OK(status)) {
412                 printf("auth_generic_set_username returned %s\n", nt_errstr(status));
413                 return false;
414         }
415
416         status = auth_generic_set_domain(auth_generic_state, workgroup);
417         if (!NT_STATUS_IS_OK(status)) {
418                 printf("auth_generic_set_domain returned %s\n", nt_errstr(status));
419                 return false;
420         }
421
422         status = auth_generic_set_password(auth_generic_state, password);
423         if (!NT_STATUS_IS_OK(status)) {
424                 printf("auth_generic_set_password returned %s\n", nt_errstr(status));
425                 return false;
426         }
427
428         status = auth_generic_client_start(auth_generic_state, GENSEC_OID_NTLMSSP);
429         if (!NT_STATUS_IS_OK(status)) {
430                 printf("auth_generic_client_start returned %s\n", nt_errstr(status));
431                 return false;
432         }
433
434         ev = samba_tevent_context_init(talloc_tos());
435         if (ev == NULL) {
436                 printf("samba_tevent_context_init() returned NULL\n");
437                 return false;
438         }
439
440         status = gensec_update(auth_generic_state->gensec_security, talloc_tos(), ev, data_blob_null, &in_blob);
441         if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
442                 printf("gensec_update returned %s\n", nt_errstr(status));
443                 return false;
444         }
445
446         cli2->smb2.session = smbXcli_session_create(cli2, cli2->conn);
447
448         subreq = smb2cli_session_setup_send(talloc_tos(), ev,
449                                             cli2->conn,
450                                             cli2->timeout,
451                                             cli2->smb2.session,
452                                             0x0, /* in_flags */
453                                             SMB2_CAP_DFS, /* in_capabilities */
454                                             0, /* in_channel */
455                                             /* in_previous_session_id: */
456                                             smb2cli_session_current_id(cli1->smb2.session),
457                                             &in_blob); /* in_security_buffer */
458         if (subreq == NULL) {
459                 printf("smb2cli_session_setup_send() returned NULL\n");
460                 return false;
461         }
462
463         ok = tevent_req_poll(subreq, ev);
464         if (!ok) {
465                 printf("tevent_req_poll() returned false\n");
466                 return false;
467         }
468
469         status = smb2cli_session_setup_recv(subreq, talloc_tos(),
470                                             NULL, &out_blob);
471         if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
472                 printf("smb2cli_session_setup_recv returned %s\n",
473                         nt_errstr(status));
474                 return false;
475         }
476
477         status = gensec_update(auth_generic_state->gensec_security, talloc_tos(), ev, out_blob, &in_blob);
478         if (!NT_STATUS_IS_OK(status)) {
479                 printf("auth_generic_update returned %s\n", nt_errstr(status));
480                 return false;
481         }
482
483         subreq = smb2cli_session_setup_send(talloc_tos(), ev,
484                                             cli2->conn,
485                                             cli2->timeout,
486                                             cli2->smb2.session,
487                                             0x0, /* in_flags */
488                                             SMB2_CAP_DFS, /* in_capabilities */
489                                             0, /* in_channel */
490                                             /* in_previous_session_id: */
491                                             smb2cli_session_current_id(cli1->smb2.session),
492                                             &in_blob); /* in_security_buffer */
493         if (subreq == NULL) {
494                 printf("smb2cli_session_setup_send() returned NULL\n");
495                 return false;
496         }
497
498         ok = tevent_req_poll(subreq, ev);
499         if (!ok) {
500                 printf("tevent_req_poll() returned false\n");
501                 return false;
502         }
503
504         status = smb2cli_session_setup_recv(subreq, talloc_tos(),
505                                             &recv_iov, &out_blob);
506         if (!NT_STATUS_IS_OK(status)) {
507                 printf("smb2cli_session_setup_recv returned %s\n",
508                         nt_errstr(status));
509                 return false;
510         }
511
512         status = gensec_session_key(auth_generic_state->gensec_security, talloc_tos(),
513                                     &session_key);
514         if (!NT_STATUS_IS_OK(status)) {
515                 printf("gensec_session_key returned %s\n",
516                         nt_errstr(status));
517                 return false;
518         }
519
520         /* check file operation on the old client */
521
522         status = smb2cli_flush(cli1->conn, cli1->timeout, cli1->smb2.session,
523                                cli1->smb2.tcon, fid_persistent, fid_volatile);
524         if (!NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
525                 printf("smb2cli_flush returned %s\n", nt_errstr(status));
526                 return false;
527         }
528
529         status = cli_tree_connect(cli1, share, "?????", "", 0);
530         if (!NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
531                 printf("cli_tree_connect returned %s\n", nt_errstr(status));
532                 return false;
533         }
534
535         /*
536          * checking file operations without signing.
537          * on w2k8r2 at least, flush, read and write also work the same way,
538          * while create gives ACCESS_DENIED without signing
539          */
540         status = smb2cli_flush(cli2->conn, cli2->timeout, cli2->smb2.session,
541                                cli2->smb2.tcon, fid_persistent, fid_volatile);
542         if (!NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) &&
543             !NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED))
544         {
545                 printf("smb2cli_flush returned %s\n", nt_errstr(status));
546                 return false;
547         }
548
549         status = smb2cli_write(cli2->conn, cli2->timeout, cli2->smb2.session,
550                                cli2->smb2.tcon, strlen(hello), 0, fid_persistent,
551                                fid_volatile, 0, 0, (const uint8_t *)hello, NULL);
552         if (!NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) &&
553             !NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED))
554         {
555                 printf("smb2cli_write returned %s\n", nt_errstr(status));
556                 return false;
557         }
558
559         status = smb2cli_read(cli2->conn, cli2->timeout, cli2->smb2.session,
560                               cli2->smb2.tcon, 0x10000, 0, fid_persistent,
561                               fid_volatile, 2, 0,
562                               talloc_tos(), &result, &nread);
563         if (!NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) &&
564             !NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED))
565         {
566                 printf("smb2cli_read returned %s\n", nt_errstr(status));
567                 return false;
568         }
569
570         status = smb2cli_create(cli2->conn, cli2->timeout, cli2->smb2.session,
571                         cli2->smb2.tcon, "session-reconnect.txt",
572                         SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
573                         SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
574                         SEC_STD_ALL | SEC_FILE_ALL, /* desired_access, */
575                         FILE_ATTRIBUTE_NORMAL, /* file_attributes, */
576                         FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
577                         FILE_CREATE, /* create_disposition, */
578                         FILE_DELETE_ON_CLOSE, /* create_options, */
579                         NULL, /* smb2_create_blobs *blobs */
580                         &fid_persistent,
581                         &fid_volatile,
582                         NULL);
583         if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) &&
584             !NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED)) {
585                 printf("smb2cli_create on cli2 %s\n", nt_errstr(status));
586                 return false;
587         }
588
589         /* now grab the session key and try with signing */
590
591         status = smb2cli_session_set_session_key(cli2->smb2.session,
592                                                  session_key,
593                                                  recv_iov);
594         if (!NT_STATUS_IS_OK(status)) {
595                 printf("smb2cli_session_set_session_key %s\n", nt_errstr(status));
596                 return false;
597         }
598
599         /* the tid seems to be irrelevant at this stage */
600
601         status = smb2cli_flush(cli2->conn, cli2->timeout, cli2->smb2.session,
602                                cli1->smb2.tcon, fid_persistent, fid_volatile);
603         if (!NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) &&
604             !NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED))
605         {
606                 printf("smb2cli_flush returned %s\n", nt_errstr(status));
607                 return false;
608         }
609
610         status = smb2cli_write(cli2->conn, cli2->timeout, cli2->smb2.session,
611                                cli1->smb2.tcon, strlen(hello), 0, fid_persistent,
612                                fid_volatile, 0, 0, (const uint8_t *)hello, NULL);
613         if (!NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) &&
614             !NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED))
615         {
616                 printf("smb2cli_write returned %s\n", nt_errstr(status));
617                 return false;
618         }
619
620         status = smb2cli_read(cli2->conn, cli2->timeout, cli2->smb2.session,
621                               cli1->smb2.tcon, 0x10000, 0, fid_persistent,
622                               fid_volatile, 2, 0,
623                               talloc_tos(), &result, &nread);
624         if (!NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) &&
625             !NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED))
626         {
627                 printf("smb2cli_read returned %s\n", nt_errstr(status));
628                 return false;
629         }
630
631         status = smb2cli_create(cli2->conn, cli2->timeout, cli2->smb2.session,
632                         cli1->smb2.tcon, "session-reconnect.txt",
633                         SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
634                         SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
635                         SEC_STD_ALL | SEC_FILE_ALL, /* desired_access, */
636                         FILE_ATTRIBUTE_NORMAL, /* file_attributes, */
637                         FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
638                         FILE_CREATE, /* create_disposition, */
639                         FILE_DELETE_ON_CLOSE, /* create_options, */
640                         NULL, /* smb2_create_blobs *blobs */
641                         &fid_persistent,
642                         &fid_volatile,
643                         NULL);
644         if (!NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) &&
645             !NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED))
646         {
647                 printf("smb2cli_create on cli2 %s\n", nt_errstr(status));
648                 return false;
649         }
650
651         /* now do a new tcon and test file calls again */
652
653         status = cli_tree_connect(cli2, share, "?????", "", 0);
654         if (!NT_STATUS_IS_OK(status)) {
655                 printf("cli_tree_connect returned %s\n", nt_errstr(status));
656                 return false;
657         }
658
659         status = smb2cli_create(cli2->conn, cli2->timeout, cli2->smb2.session,
660                         cli2->smb2.tcon, "session-reconnect.txt",
661                         SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
662                         SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
663                         SEC_STD_ALL | SEC_FILE_ALL, /* desired_access, */
664                         FILE_ATTRIBUTE_NORMAL, /* file_attributes, */
665                         FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
666                         FILE_CREATE, /* create_disposition, */
667                         FILE_DELETE_ON_CLOSE, /* create_options, */
668                         NULL, /* smb2_create_blobs *blobs */
669                         &fid_persistent,
670                         &fid_volatile,
671                         NULL);
672         if (!NT_STATUS_IS_OK(status)) {
673                 printf("smb2cli_create on cli2 %s\n", nt_errstr(status));
674                 return false;
675         }
676
677         status = smb2cli_write(cli2->conn, cli2->timeout, cli2->smb2.session,
678                                cli2->smb2.tcon, strlen(hello), 0, fid_persistent,
679                                fid_volatile, 0, 0, (const uint8_t *)hello, NULL);
680         if (!NT_STATUS_IS_OK(status)) {
681                 printf("smb2cli_write returned %s\n", nt_errstr(status));
682                 return false;
683         }
684
685         status = smb2cli_flush(cli2->conn, cli2->timeout, cli2->smb2.session,
686                                cli2->smb2.tcon, fid_persistent, fid_volatile);
687         if (!NT_STATUS_IS_OK(status)) {
688                 printf("smb2cli_flush returned %s\n", nt_errstr(status));
689                 return false;
690         }
691
692         status = smb2cli_read(cli2->conn, cli2->timeout, cli2->smb2.session,
693                               cli2->smb2.tcon, 0x10000, 0, fid_persistent,
694                               fid_volatile, 2, 0,
695                               talloc_tos(), &result, &nread);
696         if (!NT_STATUS_IS_OK(status)) {
697                 printf("smb2cli_read returned %s\n", nt_errstr(status));
698                 return false;
699         }
700
701         if (nread != strlen(hello)) {
702                 printf("smb2cli_read returned %d bytes, expected %d\n",
703                        (int)nread, (int)strlen(hello));
704                 return false;
705         }
706
707         if (memcmp(hello, result, nread) != 0) {
708                 printf("smb2cli_read returned '%s', expected '%s'\n",
709                        result, hello);
710                 return false;
711         }
712
713         return true;
714 }
715
716 bool run_smb2_tcon_dependence(int dummy)
717 {
718         struct cli_state *cli;
719         NTSTATUS status;
720         uint64_t fid_persistent, fid_volatile;
721         const char *hello = "Hello, world\n";
722         uint8_t *result;
723         uint32_t nread;
724         struct smbXcli_tcon *tcon2;
725         uint32_t tcon2_id;
726
727         printf("Starting SMB2-TCON-DEPENDENCE\n");
728
729         if (!torture_init_connection(&cli)) {
730                 return false;
731         }
732
733         status = smbXcli_negprot(cli->conn, cli->timeout,
734                                  PROTOCOL_SMB2_02, PROTOCOL_LATEST);
735         if (!NT_STATUS_IS_OK(status)) {
736                 printf("smbXcli_negprot returned %s\n", nt_errstr(status));
737                 return false;
738         }
739
740         status = cli_session_setup(cli, username,
741                                    password, strlen(password),
742                                    password, strlen(password),
743                                    workgroup);
744         if (!NT_STATUS_IS_OK(status)) {
745                 printf("cli_session_setup returned %s\n", nt_errstr(status));
746                 return false;
747         }
748
749         status = cli_tree_connect(cli, share, "?????", "", 0);
750         if (!NT_STATUS_IS_OK(status)) {
751                 printf("cli_tree_connect returned %s\n", nt_errstr(status));
752                 return false;
753         }
754
755         status = smb2cli_create(cli->conn, cli->timeout, cli->smb2.session,
756                         cli->smb2.tcon, "tcon_depedence.txt",
757                         SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
758                         SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
759                         SEC_STD_ALL | SEC_FILE_ALL, /* desired_access, */
760                         FILE_ATTRIBUTE_NORMAL, /* file_attributes, */
761                         FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
762                         FILE_CREATE, /* create_disposition, */
763                         FILE_DELETE_ON_CLOSE, /* create_options, */
764                         NULL, /* smb2_create_blobs *blobs */
765                         &fid_persistent,
766                         &fid_volatile,
767                         NULL);
768         if (!NT_STATUS_IS_OK(status)) {
769                 printf("smb2cli_create on cli %s\n", nt_errstr(status));
770                 return false;
771         }
772
773         status = smb2cli_write(cli->conn, cli->timeout, cli->smb2.session,
774                                cli->smb2.tcon, strlen(hello), 0, fid_persistent,
775                                fid_volatile, 0, 0, (const uint8_t *)hello, NULL);
776         if (!NT_STATUS_IS_OK(status)) {
777                 printf("smb2cli_write returned %s\n", nt_errstr(status));
778                 return false;
779         }
780
781         status = smb2cli_flush(cli->conn, cli->timeout, cli->smb2.session,
782                                cli->smb2.tcon, fid_persistent, fid_volatile);
783         if (!NT_STATUS_IS_OK(status)) {
784                 printf("smb2cli_flush returned %s\n", nt_errstr(status));
785                 return false;
786         }
787
788         status = smb2cli_read(cli->conn, cli->timeout, cli->smb2.session,
789                               cli->smb2.tcon, 0x10000, 0, fid_persistent,
790                               fid_volatile, 2, 0,
791                               talloc_tos(), &result, &nread);
792         if (!NT_STATUS_IS_OK(status)) {
793                 printf("smb2cli_read returned %s\n", nt_errstr(status));
794                 return false;
795         }
796
797         if (nread != strlen(hello)) {
798                 printf("smb2cli_read returned %d bytes, expected %d\n",
799                        (int)nread, (int)strlen(hello));
800                 return false;
801         }
802
803         if (memcmp(hello, result, nread) != 0) {
804                 printf("smb2cli_read returned '%s', expected '%s'\n",
805                        result, hello);
806                 return false;
807         }
808
809         /* check behaviour with wrong tid... */
810
811         tcon2 = smbXcli_tcon_create(cli);
812         tcon2_id = smb2cli_tcon_current_id(cli->smb2.tcon);
813         tcon2_id++;
814         smb2cli_tcon_set_values(tcon2,
815                                 NULL, /* session */
816                                 tcon2_id,
817                                 0, /* type */
818                                 0, /* flags */
819                                 0, /* capabilities */
820                                 0  /* maximal_access */);
821
822         status = smb2cli_read(cli->conn, cli->timeout, cli->smb2.session,
823                               tcon2, 0x10000, 0, fid_persistent,
824                               fid_volatile, 2, 0,
825                               talloc_tos(), &result, &nread);
826         if (!NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED)) {
827                 printf("smb2cli_read returned %s\n", nt_errstr(status));
828                 return false;
829         }
830
831         talloc_free(tcon2);
832
833         return true;
834 }
835
836 bool run_smb2_multi_channel(int dummy)
837 {
838         struct cli_state *cli1;
839         struct cli_state *cli2;
840         struct cli_state *cli3;
841         NTSTATUS status;
842         bool ok;
843         uint64_t fid_persistent, fid_volatile;
844         struct tevent_context *ev;
845         struct tevent_req *subreq;
846         DATA_BLOB in_blob = data_blob_null;
847         DATA_BLOB out_blob;
848         DATA_BLOB channel_session_key;
849         struct auth_generic_state *auth_generic_state;
850         struct iovec *recv_iov;
851         const char *hello = "Hello, world\n";
852         uint8_t *result;
853         uint32_t nread;
854
855         printf("Starting SMB2-MULTI-CHANNEL\n");
856
857         if (!torture_init_connection(&cli1)) {
858                 return false;
859         }
860
861         if (!torture_init_connection(&cli2)) {
862                 return false;
863         }
864
865         if (!torture_init_connection(&cli3)) {
866                 return false;
867         }
868
869         status = smbXcli_negprot(cli1->conn, cli1->timeout,
870                                  PROTOCOL_SMB2_22, PROTOCOL_LATEST);
871         if (!NT_STATUS_IS_OK(status)) {
872                 printf("smbXcli_negprot returned %s\n", nt_errstr(status));
873                 return false;
874         }
875
876         status = smbXcli_negprot(cli2->conn, cli2->timeout,
877                                  PROTOCOL_SMB2_22, PROTOCOL_LATEST);
878         if (!NT_STATUS_IS_OK(status)) {
879                 printf("smbXcli_negprot returned %s\n", nt_errstr(status));
880                 return false;
881         }
882
883         status = smbXcli_negprot(cli3->conn, cli3->timeout,
884                                  PROTOCOL_SMB2_22, PROTOCOL_LATEST);
885         if (!NT_STATUS_IS_OK(status)) {
886                 printf("smbXcli_negprot returned %s\n", nt_errstr(status));
887                 return false;
888         }
889
890         status = cli_session_setup(cli1, username,
891                                    password, strlen(password),
892                                    password, strlen(password),
893                                    workgroup);
894         if (!NT_STATUS_IS_OK(status)) {
895                 printf("smb2cli_sesssetup returned %s\n", nt_errstr(status));
896                 return false;
897         }
898
899         status = cli_tree_connect(cli1, share, "?????", "", 0);
900         if (!NT_STATUS_IS_OK(status)) {
901                 printf("cli_tree_connect returned %s\n", nt_errstr(status));
902                 return false;
903         }
904
905         status = smb2cli_session_create_channel(cli2,
906                                                 cli1->smb2.session,
907                                                 cli2->conn,
908                                                 &cli2->smb2.session);
909         if (!NT_STATUS_IS_OK(status)) {
910                 printf("smb2cli_session_create_channel returned %s\n",
911                         nt_errstr(status));
912                 return false;
913         }
914
915         status = auth_generic_client_prepare(talloc_tos(), &auth_generic_state);
916         if (!NT_STATUS_IS_OK(status)) {
917                 printf("auth_generic_client_prepare returned %s\n", nt_errstr(status));
918                 return false;
919         }
920
921         gensec_want_feature(auth_generic_state->gensec_security,
922                             GENSEC_FEATURE_SESSION_KEY);
923         status = auth_generic_set_username(auth_generic_state, username);
924         if (!NT_STATUS_IS_OK(status)) {
925                 printf("auth_generic_set_username returned %s\n", nt_errstr(status));
926                 return false;
927         }
928
929         status = auth_generic_set_domain(auth_generic_state, workgroup);
930         if (!NT_STATUS_IS_OK(status)) {
931                 printf("auth_generic_set_domain returned %s\n", nt_errstr(status));
932                 return false;
933         }
934
935         status = auth_generic_set_password(auth_generic_state, password);
936         if (!NT_STATUS_IS_OK(status)) {
937                 printf("auth_generic_set_password returned %s\n", nt_errstr(status));
938                 return false;
939         }
940
941         status = auth_generic_client_start(auth_generic_state, GENSEC_OID_NTLMSSP);
942         if (!NT_STATUS_IS_OK(status)) {
943                 printf("auth_generic_client_start returned %s\n", nt_errstr(status));
944                 return false;
945         }
946
947         ev = samba_tevent_context_init(talloc_tos());
948         if (ev == NULL) {
949                 printf("samba_tevent_context_init() returned NULL\n");
950                 return false;
951         }
952
953         status = gensec_update(auth_generic_state->gensec_security, talloc_tos(), ev, data_blob_null, &in_blob);
954         if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
955                 printf("gensec_update returned %s\n", nt_errstr(status));
956                 return false;
957         }
958
959         subreq = smb2cli_session_setup_send(talloc_tos(), ev,
960                                             cli2->conn,
961                                             cli2->timeout,
962                                             cli2->smb2.session,
963                                             0x01, /* in_flags */
964                                             SMB2_CAP_DFS, /* in_capabilities */
965                                             0, /* in_channel */
966                                             0, /* in_previous_session_id */
967                                             &in_blob); /* in_security_buffer */
968         if (subreq == NULL) {
969                 printf("smb2cli_session_setup_send() returned NULL\n");
970                 return false;
971         }
972
973         ok = tevent_req_poll(subreq, ev);
974         if (!ok) {
975                 printf("tevent_req_poll() returned false\n");
976                 return false;
977         }
978
979         status = smb2cli_session_setup_recv(subreq, talloc_tos(),
980                                             NULL, &out_blob);
981         if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
982                 printf("smb2cli_session_setup_recv returned %s\n",
983                         nt_errstr(status));
984                 return false;
985         }
986
987         status = gensec_update(auth_generic_state->gensec_security, talloc_tos(), ev, out_blob, &in_blob);
988         if (!NT_STATUS_IS_OK(status)) {
989                 printf("auth_generic_update returned %s\n", nt_errstr(status));
990                 return false;
991         }
992
993         subreq = smb2cli_session_setup_send(talloc_tos(), ev,
994                                             cli2->conn,
995                                             cli2->timeout,
996                                             cli2->smb2.session,
997                                             0x01, /* in_flags */
998                                             SMB2_CAP_DFS, /* in_capabilities */
999                                             0, /* in_channel */
1000                                             0, /* in_previous_session_id */
1001                                             &in_blob); /* in_security_buffer */
1002         if (subreq == NULL) {
1003                 printf("smb2cli_session_setup_send() returned NULL\n");
1004                 return false;
1005         }
1006
1007         ok = tevent_req_poll(subreq, ev);
1008         if (!ok) {
1009                 printf("tevent_req_poll() returned false\n");
1010                 return false;
1011         }
1012
1013         status = smb2cli_session_setup_recv(subreq, talloc_tos(),
1014                                             &recv_iov, &out_blob);
1015         if (!NT_STATUS_IS_OK(status)) {
1016                 printf("smb2cli_session_setup_recv returned %s\n",
1017                         nt_errstr(status));
1018                 return false;
1019         }
1020
1021         status = gensec_session_key(auth_generic_state->gensec_security, talloc_tos(),
1022                                     &channel_session_key);
1023         if (!NT_STATUS_IS_OK(status)) {
1024                 printf("gensec_session_key returned %s\n",
1025                         nt_errstr(status));
1026                 return false;
1027         }
1028
1029         status = smb2cli_session_set_channel_key(cli2->smb2.session,
1030                                                  channel_session_key,
1031                                                  recv_iov);
1032         if (!NT_STATUS_IS_OK(status)) {
1033                 printf("smb2cli_session_set_channel_key %s\n", nt_errstr(status));
1034                 return false;
1035         }
1036
1037         status = smb2cli_session_create_channel(cli3,
1038                                                 cli1->smb2.session,
1039                                                 cli3->conn,
1040                                                 &cli3->smb2.session);
1041         if (!NT_STATUS_IS_OK(status)) {
1042                 printf("smb2cli_session_create_channel returned %s\n",
1043                         nt_errstr(status));
1044                 return false;
1045         }
1046
1047         status = auth_generic_client_prepare(talloc_tos(), &auth_generic_state);
1048         if (!NT_STATUS_IS_OK(status)) {
1049                 printf("auth_generic_client_prepare returned %s\n", nt_errstr(status));
1050                 return false;
1051         }
1052
1053         gensec_want_feature(auth_generic_state->gensec_security,
1054                             GENSEC_FEATURE_SESSION_KEY);
1055         status = auth_generic_set_username(auth_generic_state, username);
1056         if (!NT_STATUS_IS_OK(status)) {
1057                 printf("auth_generic_set_username returned %s\n", nt_errstr(status));
1058                 return false;
1059         }
1060
1061         status = auth_generic_set_domain(auth_generic_state, workgroup);
1062         if (!NT_STATUS_IS_OK(status)) {
1063                 printf("auth_generic_set_domain returned %s\n", nt_errstr(status));
1064                 return false;
1065         }
1066
1067         status = auth_generic_set_password(auth_generic_state, password);
1068         if (!NT_STATUS_IS_OK(status)) {
1069                 printf("auth_generic_set_password returned %s\n", nt_errstr(status));
1070                 return false;
1071         }
1072
1073         status = auth_generic_client_start(auth_generic_state, GENSEC_OID_NTLMSSP);
1074         if (!NT_STATUS_IS_OK(status)) {
1075                 printf("auth_generic_client_start returned %s\n", nt_errstr(status));
1076                 return false;
1077         }
1078
1079         status = gensec_update(auth_generic_state->gensec_security, talloc_tos(), ev, data_blob_null, &in_blob);
1080         if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1081                 printf("gensec_update returned %s\n", nt_errstr(status));
1082                 return false;
1083         }
1084
1085         subreq = smb2cli_session_setup_send(talloc_tos(), ev,
1086                                             cli3->conn,
1087                                             cli3->timeout,
1088                                             cli3->smb2.session,
1089                                             0x01, /* in_flags */
1090                                             SMB2_CAP_DFS, /* in_capabilities */
1091                                             0, /* in_channel */
1092                                             0, /* in_previous_session_id */
1093                                             &in_blob); /* in_security_buffer */
1094         if (subreq == NULL) {
1095                 printf("smb2cli_session_setup_send() returned NULL\n");
1096                 return false;
1097         }
1098
1099         ok = tevent_req_poll(subreq, ev);
1100         if (!ok) {
1101                 printf("tevent_req_poll() returned false\n");
1102                 return false;
1103         }
1104
1105         status = smb2cli_session_setup_recv(subreq, talloc_tos(),
1106                                             NULL, &out_blob);
1107         if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1108                 printf("smb2cli_session_setup_recv returned %s\n",
1109                         nt_errstr(status));
1110                 return false;
1111         }
1112
1113         status = gensec_update(auth_generic_state->gensec_security, talloc_tos(), ev, out_blob, &in_blob);
1114         if (!NT_STATUS_IS_OK(status)) {
1115                 printf("auth_generic_update returned %s\n", nt_errstr(status));
1116                 return false;
1117         }
1118
1119         subreq = smb2cli_session_setup_send(talloc_tos(), ev,
1120                                             cli3->conn,
1121                                             cli3->timeout,
1122                                             cli3->smb2.session,
1123                                             0x01, /* in_flags */
1124                                             SMB2_CAP_DFS, /* in_capabilities */
1125                                             0, /* in_channel */
1126                                             0, /* in_previous_session_id */
1127                                             &in_blob); /* in_security_buffer */
1128         if (subreq == NULL) {
1129                 printf("smb2cli_session_setup_send() returned NULL\n");
1130                 return false;
1131         }
1132
1133         ok = tevent_req_poll(subreq, ev);
1134         if (!ok) {
1135                 printf("tevent_req_poll() returned false\n");
1136                 return false;
1137         }
1138
1139         status = smb2cli_session_setup_recv(subreq, talloc_tos(),
1140                                             &recv_iov, &out_blob);
1141         if (!NT_STATUS_IS_OK(status)) {
1142                 printf("smb2cli_session_setup_recv returned %s\n",
1143                         nt_errstr(status));
1144                 return false;
1145         }
1146
1147         status = gensec_session_key(auth_generic_state->gensec_security, talloc_tos(),
1148                                     &channel_session_key);
1149         if (!NT_STATUS_IS_OK(status)) {
1150                 printf("gensec_session_key returned %s\n",
1151                         nt_errstr(status));
1152                 return false;
1153         }
1154
1155         status = smb2cli_session_set_channel_key(cli3->smb2.session,
1156                                                  channel_session_key,
1157                                                  recv_iov);
1158         if (!NT_STATUS_IS_OK(status)) {
1159                 printf("smb2cli_session_set_channel_key %s\n", nt_errstr(status));
1160                 return false;
1161         }
1162
1163         status = smb2cli_create(cli2->conn, cli2->timeout, cli2->smb2.session,
1164                         cli1->smb2.tcon, "multi-channel.txt",
1165                         SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
1166                         SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
1167                         SEC_STD_ALL | SEC_FILE_ALL, /* desired_access, */
1168                         FILE_ATTRIBUTE_NORMAL, /* file_attributes, */
1169                         FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
1170                         FILE_CREATE, /* create_disposition, */
1171                         FILE_DELETE_ON_CLOSE, /* create_options, */
1172                         NULL, /* smb2_create_blobs *blobs */
1173                         &fid_persistent,
1174                         &fid_volatile,
1175                         NULL);
1176         if (!NT_STATUS_IS_OK(status)) {
1177                 printf("smb2cli_create on cli2 %s\n", nt_errstr(status));
1178                 return false;
1179         }
1180
1181         status = smb2cli_write(cli1->conn, cli1->timeout, cli1->smb2.session,
1182                                cli1->smb2.tcon, strlen(hello), 0, fid_persistent,
1183                                fid_volatile, 0, 0, (const uint8_t *)hello, NULL);
1184         if (!NT_STATUS_IS_OK(status)) {
1185                 printf("smb2cli_write returned %s\n", nt_errstr(status));
1186                 return false;
1187         }
1188
1189         status = smb2cli_flush(cli2->conn, cli2->timeout, cli2->smb2.session,
1190                                cli1->smb2.tcon, fid_persistent, fid_volatile);
1191         if (!NT_STATUS_IS_OK(status)) {
1192                 printf("smb2cli_flush returned %s\n", nt_errstr(status));
1193                 return false;
1194         }
1195
1196         status = smb2cli_flush(cli1->conn, cli1->timeout, cli1->smb2.session,
1197                                cli1->smb2.tcon, fid_persistent, fid_volatile);
1198         if (!NT_STATUS_IS_OK(status)) {
1199                 printf("smb2cli_flush returned %s\n", nt_errstr(status));
1200                 return false;
1201         }
1202
1203         status = smb2cli_flush(cli3->conn, cli3->timeout, cli3->smb2.session,
1204                                cli1->smb2.tcon, fid_persistent, fid_volatile);
1205         if (!NT_STATUS_IS_OK(status)) {
1206                 printf("smb2cli_flush returned %s\n", nt_errstr(status));
1207                 return false;
1208         }
1209
1210         status = smb2cli_read(cli2->conn, cli2->timeout, cli2->smb2.session,
1211                               cli1->smb2.tcon, 0x10000, 0, fid_persistent,
1212                               fid_volatile, 2, 0,
1213                               talloc_tos(), &result, &nread);
1214         if (!NT_STATUS_IS_OK(status)) {
1215                 printf("smb2cli_read returned %s\n", nt_errstr(status));
1216                 return false;
1217         }
1218
1219         if (nread != strlen(hello)) {
1220                 printf("smb2cli_read returned %d bytes, expected %d\n",
1221                        (int)nread, (int)strlen(hello));
1222                 return false;
1223         }
1224
1225         if (memcmp(hello, result, nread) != 0) {
1226                 printf("smb2cli_read returned '%s', expected '%s'\n",
1227                        result, hello);
1228                 return false;
1229         }
1230
1231         status = auth_generic_client_prepare(talloc_tos(), &auth_generic_state);
1232         if (!NT_STATUS_IS_OK(status)) {
1233                 printf("auth_generic_client_prepare returned %s\n", nt_errstr(status));
1234                 return false;
1235         }
1236
1237         gensec_want_feature(auth_generic_state->gensec_security,
1238                             GENSEC_FEATURE_SESSION_KEY);
1239         status = auth_generic_set_username(auth_generic_state, username);
1240         if (!NT_STATUS_IS_OK(status)) {
1241                 printf("auth_generic_set_username returned %s\n", nt_errstr(status));
1242                 return false;
1243         }
1244
1245         status = auth_generic_set_domain(auth_generic_state, workgroup);
1246         if (!NT_STATUS_IS_OK(status)) {
1247                 printf("auth_generic_set_domain returned %s\n", nt_errstr(status));
1248                 return false;
1249         }
1250
1251         status = auth_generic_set_password(auth_generic_state, password);
1252         if (!NT_STATUS_IS_OK(status)) {
1253                 printf("auth_generic_set_password returned %s\n", nt_errstr(status));
1254                 return false;
1255         }
1256
1257         status = auth_generic_client_start(auth_generic_state, GENSEC_OID_NTLMSSP);
1258         if (!NT_STATUS_IS_OK(status)) {
1259                 printf("auth_generic_client_start returned %s\n", nt_errstr(status));
1260                 return false;
1261         }
1262
1263         status = gensec_update(auth_generic_state->gensec_security, talloc_tos(), ev, data_blob_null, &in_blob);
1264         if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1265                 printf("gensec_update returned %s\n", nt_errstr(status));
1266                 return false;
1267         }
1268
1269         subreq = smb2cli_session_setup_send(talloc_tos(), ev,
1270                                             cli3->conn,
1271                                             cli3->timeout,
1272                                             cli3->smb2.session,
1273                                             0x0, /* in_flags */
1274                                             SMB2_CAP_DFS, /* in_capabilities */
1275                                             0, /* in_channel */
1276                                             0, /* in_previous_session_id */
1277                                             &in_blob); /* in_security_buffer */
1278         if (subreq == NULL) {
1279                 printf("smb2cli_session_setup_send() returned NULL\n");
1280                 return false;
1281         }
1282
1283         ok = tevent_req_poll(subreq, ev);
1284         if (!ok) {
1285                 printf("tevent_req_poll() returned false\n");
1286                 return false;
1287         }
1288
1289         status = smb2cli_session_setup_recv(subreq, talloc_tos(),
1290                                             NULL, &out_blob);
1291         if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1292                 printf("smb2cli_session_setup_recv returned %s\n",
1293                         nt_errstr(status));
1294                 return false;
1295         }
1296
1297         status = gensec_update(auth_generic_state->gensec_security, talloc_tos(), ev, out_blob, &in_blob);
1298         if (!NT_STATUS_IS_OK(status)) {
1299                 printf("auth_generic_update returned %s\n", nt_errstr(status));
1300                 return false;
1301         }
1302
1303         status = smb2cli_flush(cli1->conn, cli1->timeout, cli1->smb2.session,
1304                                cli1->smb2.tcon, fid_persistent, fid_volatile);
1305         if (!NT_STATUS_IS_OK(status)) {
1306                 printf("smb2cli_flush returned %s\n", nt_errstr(status));
1307                 return false;
1308         }
1309
1310         status = smb2cli_flush(cli2->conn, cli2->timeout, cli2->smb2.session,
1311                                cli1->smb2.tcon, fid_persistent, fid_volatile);
1312         if (!NT_STATUS_IS_OK(status)) {
1313                 printf("smb2cli_flush returned %s\n", nt_errstr(status));
1314                 return false;
1315         }
1316
1317         status = smb2cli_flush(cli3->conn, cli3->timeout, cli3->smb2.session,
1318                                cli1->smb2.tcon, fid_persistent, fid_volatile);
1319         if (!NT_STATUS_IS_OK(status)) {
1320                 printf("smb2cli_flush returned %s\n", nt_errstr(status));
1321                 return false;
1322         }
1323
1324         status = smb2cli_create(cli1->conn, cli1->timeout, cli1->smb2.session,
1325                         cli1->smb2.tcon, "multi-channel-invalid.txt",
1326                         SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
1327                         SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
1328                         SEC_STD_ALL | SEC_FILE_ALL, /* desired_access, */
1329                         FILE_ATTRIBUTE_NORMAL, /* file_attributes, */
1330                         FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
1331                         FILE_CREATE, /* create_disposition, */
1332                         FILE_DELETE_ON_CLOSE, /* create_options, */
1333                         NULL, /* smb2_create_blobs *blobs */
1334                         &fid_persistent,
1335                         &fid_volatile,
1336                         NULL);
1337         if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
1338                 printf("smb2cli_create %s\n", nt_errstr(status));
1339                 return false;
1340         }
1341
1342         status = smb2cli_create(cli2->conn, cli2->timeout, cli2->smb2.session,
1343                         cli1->smb2.tcon, "multi-channel-invalid.txt",
1344                         SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
1345                         SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
1346                         SEC_STD_ALL | SEC_FILE_ALL, /* desired_access, */
1347                         FILE_ATTRIBUTE_NORMAL, /* file_attributes, */
1348                         FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
1349                         FILE_CREATE, /* create_disposition, */
1350                         FILE_DELETE_ON_CLOSE, /* create_options, */
1351                         NULL, /* smb2_create_blobs *blobs */
1352                         &fid_persistent,
1353                         &fid_volatile,
1354                         NULL);
1355         if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
1356                 printf("smb2cli_create %s\n", nt_errstr(status));
1357                 return false;
1358         }
1359
1360         status = smb2cli_create(cli3->conn, cli3->timeout, cli3->smb2.session,
1361                         cli1->smb2.tcon, "multi-channel-invalid.txt",
1362                         SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
1363                         SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
1364                         SEC_STD_ALL | SEC_FILE_ALL, /* desired_access, */
1365                         FILE_ATTRIBUTE_NORMAL, /* file_attributes, */
1366                         FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
1367                         FILE_CREATE, /* create_disposition, */
1368                         FILE_DELETE_ON_CLOSE, /* create_options, */
1369                         NULL, /* smb2_create_blobs *blobs */
1370                         &fid_persistent,
1371                         &fid_volatile,
1372                         NULL);
1373         if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
1374                 printf("smb2cli_create %s\n", nt_errstr(status));
1375                 return false;
1376         }
1377
1378         subreq = smb2cli_session_setup_send(talloc_tos(), ev,
1379                                             cli2->conn,
1380                                             cli2->timeout,
1381                                             cli2->smb2.session,
1382                                             0x0, /* in_flags */
1383                                             SMB2_CAP_DFS, /* in_capabilities */
1384                                             0, /* in_channel */
1385                                             0, /* in_previous_session_id */
1386                                             &in_blob); /* in_security_buffer */
1387         if (subreq == NULL) {
1388                 printf("smb2cli_session_setup_send() returned NULL\n");
1389                 return false;
1390         }
1391
1392         ok = tevent_req_poll(subreq, ev);
1393         if (!ok) {
1394                 printf("tevent_req_poll() returned false\n");
1395                 return false;
1396         }
1397
1398         status = smb2cli_session_setup_recv(subreq, talloc_tos(),
1399                                             &recv_iov, &out_blob);
1400         if (!NT_STATUS_IS_OK(status)) {
1401                 printf("smb2cli_session_setup_recv returned %s\n",
1402                         nt_errstr(status));
1403                 return false;
1404         }
1405
1406         status = smb2cli_close(cli3->conn, cli3->timeout, cli3->smb2.session,
1407                                cli1->smb2.tcon, 0, fid_persistent, fid_volatile);
1408         if (!NT_STATUS_IS_OK(status)) {
1409                 printf("smb2cli_close returned %s\n", nt_errstr(status));
1410                 return false;
1411         }
1412
1413         status = smb2cli_flush(cli3->conn, cli3->timeout, cli3->smb2.session,
1414                                cli1->smb2.tcon, fid_persistent, fid_volatile);
1415         if (!NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED)) {
1416                 printf("smb2cli_flush returned %s\n", nt_errstr(status));
1417                 return false;
1418         }
1419
1420         status = smb2cli_flush(cli2->conn, cli2->timeout, cli2->smb2.session,
1421                                cli1->smb2.tcon, fid_persistent, fid_volatile);
1422         if (!NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED)) {
1423                 printf("smb2cli_flush returned %s\n", nt_errstr(status));
1424                 return false;
1425         }
1426
1427         status = smb2cli_flush(cli1->conn, cli1->timeout, cli1->smb2.session,
1428                                cli1->smb2.tcon, fid_persistent, fid_volatile);
1429         if (!NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED)) {
1430                 printf("smb2cli_flush returned %s\n", nt_errstr(status));
1431                 return false;
1432         }
1433
1434         return true;
1435 }
1436
1437 bool run_smb2_session_reauth(int dummy)
1438 {
1439         struct cli_state *cli;
1440         NTSTATUS status;
1441         bool ok;
1442         uint64_t fid_persistent, fid_volatile;
1443         uint64_t dir_persistent, dir_volatile;
1444         uint8_t *dir_data;
1445         uint32_t dir_data_length;
1446         struct tevent_context *ev;
1447         struct tevent_req *subreq;
1448         DATA_BLOB in_blob = data_blob_null;
1449         DATA_BLOB out_blob;
1450         DATA_BLOB in_input_buffer;
1451         DATA_BLOB out_output_buffer;
1452         uint8_t in_file_info_class;
1453         struct auth_generic_state *auth_generic_state;
1454         struct iovec *recv_iov;
1455         uint32_t saved_tid;
1456         struct smbXcli_tcon *saved_tcon;
1457
1458         printf("Starting SMB2-SESSION_REAUTH\n");
1459
1460         if (!torture_init_connection(&cli)) {
1461                 return false;
1462         }
1463
1464         /*
1465          * PROTOCOL_SMB2_22 has a bug in win8pre0
1466          * it behaves like PROTOCOL_SMB2_02
1467          * and returns NT_STATUS_REQUEST_NOT_ACCEPTED,
1468          * while it allows it on PROTOCOL_SMB2_02.
1469          */
1470         status = smbXcli_negprot(cli->conn, cli->timeout,
1471                                  PROTOCOL_SMB2_10, PROTOCOL_SMB2_10);
1472         if (!NT_STATUS_IS_OK(status)) {
1473                 printf("smbXcli_negprot returned %s\n", nt_errstr(status));
1474                 return false;
1475         }
1476
1477         status = cli_session_setup(cli, username,
1478                                    password, strlen(password),
1479                                    password, strlen(password),
1480                                    workgroup);
1481         if (!NT_STATUS_IS_OK(status)) {
1482                 printf("smb2cli_sesssetup returned %s\n", nt_errstr(status));
1483                 return false;
1484         }
1485
1486         status = cli_tree_connect(cli, share, "?????", "", 0);
1487         if (!NT_STATUS_IS_OK(status)) {
1488                 printf("cli_tree_connect returned %s\n", nt_errstr(status));
1489                 return false;
1490         }
1491
1492         status = smb2cli_create(cli->conn, cli->timeout, cli->smb2.session,
1493                         cli->smb2.tcon, "session-reauth.txt",
1494                         SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
1495                         SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
1496                         SEC_STD_ALL | SEC_FILE_ALL, /* desired_access, */
1497                         FILE_ATTRIBUTE_NORMAL, /* file_attributes, */
1498                         FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
1499                         FILE_CREATE, /* create_disposition, */
1500                         FILE_DELETE_ON_CLOSE, /* create_options, */
1501                         NULL, /* smb2_create_blobs *blobs */
1502                         &fid_persistent,
1503                         &fid_volatile,
1504                         NULL);
1505         if (!NT_STATUS_IS_OK(status)) {
1506                 printf("smb2cli_create %s\n", nt_errstr(status));
1507                 return false;
1508         }
1509
1510         status = smb2cli_create(cli->conn, cli->timeout, cli->smb2.session,
1511                         cli->smb2.tcon, "",
1512                         SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
1513                         SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
1514                         SEC_STD_SYNCHRONIZE|
1515                         SEC_DIR_LIST|
1516                         SEC_DIR_READ_ATTRIBUTE, /* desired_access, */
1517                         0, /* file_attributes, */
1518                         FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
1519                         FILE_OPEN, /* create_disposition, */
1520                         FILE_SYNCHRONOUS_IO_NONALERT|FILE_DIRECTORY_FILE, /* create_options, */
1521                         NULL, /* smb2_create_blobs *blobs */
1522                         &dir_persistent,
1523                         &dir_volatile,
1524                         NULL);
1525         if (!NT_STATUS_IS_OK(status)) {
1526                 printf("smb2cli_create returned %s\n", nt_errstr(status));
1527                 return false;
1528         }
1529
1530         status = smb2cli_query_directory(
1531                 cli->conn, cli->timeout, cli->smb2.session, cli->smb2.tcon,
1532                 1, 0x3, 0, dir_persistent, dir_volatile,
1533                 "session-reauth.txt", 0xffff,
1534                 talloc_tos(), &dir_data, &dir_data_length);
1535         if (!NT_STATUS_IS_OK(status)) {
1536                 printf("smb2cli_query_directory returned %s\n", nt_errstr(status));
1537                 return false;
1538         }
1539
1540         status = auth_generic_client_prepare(talloc_tos(), &auth_generic_state);
1541         if (!NT_STATUS_IS_OK(status)) {
1542                 printf("auth_generic_client_prepare returned %s\n", nt_errstr(status));
1543                 return false;
1544         }
1545
1546         gensec_want_feature(auth_generic_state->gensec_security,
1547                             GENSEC_FEATURE_SESSION_KEY);
1548         status = auth_generic_set_username(auth_generic_state, username);
1549         if (!NT_STATUS_IS_OK(status)) {
1550                 printf("auth_generic_set_username returned %s\n", nt_errstr(status));
1551                 return false;
1552         }
1553
1554         status = auth_generic_set_domain(auth_generic_state, workgroup);
1555         if (!NT_STATUS_IS_OK(status)) {
1556                 printf("auth_generic_set_domain returned %s\n", nt_errstr(status));
1557                 return false;
1558         }
1559
1560         status = auth_generic_set_password(auth_generic_state, password);
1561         if (!NT_STATUS_IS_OK(status)) {
1562                 printf("auth_generic_set_password returned %s\n", nt_errstr(status));
1563                 return false;
1564         }
1565
1566         status = auth_generic_client_start(auth_generic_state, GENSEC_OID_NTLMSSP);
1567         if (!NT_STATUS_IS_OK(status)) {
1568                 printf("auth_generic_client_start returned %s\n", nt_errstr(status));
1569                 return false;
1570         }
1571
1572         ev = samba_tevent_context_init(talloc_tos());
1573         if (ev == NULL) {
1574                 printf("samba_tevent_context_init() returned NULL\n");
1575                 return false;
1576         }
1577
1578         status = gensec_update(auth_generic_state->gensec_security, talloc_tos(), ev, data_blob_null, &in_blob);
1579         if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1580                 printf("gensec_update returned %s\n", nt_errstr(status));
1581                 return false;
1582         }
1583
1584         subreq = smb2cli_session_setup_send(talloc_tos(), ev,
1585                                             cli->conn,
1586                                             cli->timeout,
1587                                             cli->smb2.session,
1588                                             0x0, /* in_flags */
1589                                             SMB2_CAP_DFS, /* in_capabilities */
1590                                             0, /* in_channel */
1591                                             0, /* in_previous_session_id */
1592                                             &in_blob); /* in_security_buffer */
1593         if (subreq == NULL) {
1594                 printf("smb2cli_session_setup_send() returned NULL\n");
1595                 return false;
1596         }
1597
1598         ok = tevent_req_poll(subreq, ev);
1599         if (!ok) {
1600                 printf("tevent_req_poll() returned false\n");
1601                 return false;
1602         }
1603
1604         status = smb2cli_session_setup_recv(subreq, talloc_tos(),
1605                                             NULL, &out_blob);
1606         if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1607                 printf("smb2cli_session_setup_recv returned %s\n",
1608                         nt_errstr(status));
1609                 return false;
1610         }
1611
1612         status = gensec_update(auth_generic_state->gensec_security, talloc_tos(), ev, out_blob, &in_blob);
1613         if (!NT_STATUS_IS_OK(status)) {
1614                 printf("auth_generic_update returned %s\n", nt_errstr(status));
1615                 return false;
1616         }
1617
1618         status = smb2cli_flush(cli->conn, cli->timeout, cli->smb2.session,
1619                                cli->smb2.tcon, fid_persistent, fid_volatile);
1620         if (!NT_STATUS_IS_OK(status)) {
1621                 printf("smb2cli_flush returned %s\n", nt_errstr(status));
1622                 return false;
1623         }
1624
1625         status = smb2cli_query_directory(
1626                 cli->conn, cli->timeout, cli->smb2.session, cli->smb2.tcon,
1627                 1, 0x3, 0, dir_persistent, dir_volatile,
1628                 "session-reauth.txt", 0xffff,
1629                 talloc_tos(), &dir_data, &dir_data_length);
1630         if (!NT_STATUS_IS_OK(status)) {
1631                 printf("smb2cli_query_directory returned %s\n", nt_errstr(status));
1632                 return false;
1633         }
1634
1635         /*
1636          * query_info seems to be a path based operation on Windows...
1637          */
1638         status = smb2cli_query_info(cli->conn,
1639                                     cli->timeout,
1640                                     cli->smb2.session,
1641                                     cli->smb2.tcon,
1642                                     SMB2_GETINFO_SECURITY,
1643                                     0, /* in_file_info_class */
1644                                     1024, /* in_max_output_length */
1645                                     NULL, /* in_input_buffer */
1646                                     SECINFO_OWNER, /* in_additional_info */
1647                                     0, /* in_flags */
1648                                     fid_persistent,
1649                                     fid_volatile,
1650                                     talloc_tos(),
1651                                     &out_output_buffer);
1652         if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
1653                 printf("smb2cli_query_info (security) returned %s\n", nt_errstr(status));
1654                 return false;
1655         }
1656
1657         in_file_info_class = SMB_FILE_POSITION_INFORMATION - 1000;
1658         status = smb2cli_query_info(cli->conn,
1659                                     cli->timeout,
1660                                     cli->smb2.session,
1661                                     cli->smb2.tcon,
1662                                     SMB2_GETINFO_FILE,
1663                                     in_file_info_class,
1664                                     1024, /* in_max_output_length */
1665                                     NULL, /* in_input_buffer */
1666                                     0, /* in_additional_info */
1667                                     0, /* in_flags */
1668                                     fid_persistent,
1669                                     fid_volatile,
1670                                     talloc_tos(),
1671                                     &out_output_buffer);
1672         if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
1673                 printf("smb2cli_query_info (position) returned %s\n", nt_errstr(status));
1674                 return false;
1675         }
1676
1677         in_input_buffer = data_blob_talloc(talloc_tos(), NULL, 8);
1678         SBVAL(in_input_buffer.data, 0, 512);
1679
1680         in_file_info_class = SMB_FILE_POSITION_INFORMATION - 1000;
1681         status = smb2cli_set_info(cli->conn,
1682                                   cli->timeout,
1683                                   cli->smb2.session,
1684                                   cli->smb2.tcon,
1685                                   SMB2_GETINFO_FILE,
1686                                   in_file_info_class,
1687                                   &in_input_buffer,
1688                                   0, /* in_additional_info */
1689                                   fid_persistent,
1690                                   fid_volatile);
1691         if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
1692                 printf("smb2cli_set_info (position) returned %s\n", nt_errstr(status));
1693                 return false;
1694         }
1695
1696         status = smb2cli_create(cli->conn, cli->timeout, cli->smb2.session,
1697                         cli->smb2.tcon, "session-reauth-invalid.txt",
1698                         SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
1699                         SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
1700                         SEC_STD_ALL | SEC_FILE_ALL, /* desired_access, */
1701                         FILE_ATTRIBUTE_NORMAL, /* file_attributes, */
1702                         FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
1703                         FILE_CREATE, /* create_disposition, */
1704                         FILE_DELETE_ON_CLOSE, /* create_options, */
1705                         NULL, /* smb2_create_blobs *blobs */
1706                         &fid_persistent,
1707                         &fid_volatile,
1708                         NULL);
1709         if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
1710                 printf("smb2cli_create %s\n", nt_errstr(status));
1711                 return false;
1712         }
1713
1714         status = smb2cli_create(cli->conn, cli->timeout, cli->smb2.session,
1715                         cli->smb2.tcon, "",
1716                         SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
1717                         SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
1718                         SEC_STD_SYNCHRONIZE|
1719                         SEC_DIR_LIST|
1720                         SEC_DIR_READ_ATTRIBUTE, /* desired_access, */
1721                         0, /* file_attributes, */
1722                         FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
1723                         FILE_OPEN, /* create_disposition, */
1724                         FILE_SYNCHRONOUS_IO_NONALERT|FILE_DIRECTORY_FILE, /* create_options, */
1725                         NULL, /* smb2_create_blobs *blobs */
1726                         &dir_persistent,
1727                         &dir_volatile,
1728                         NULL);
1729         if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
1730                 printf("smb2cli_create returned %s\n", nt_errstr(status));
1731                 return false;
1732         }
1733
1734         saved_tid = smb2cli_tcon_current_id(cli->smb2.tcon);
1735         saved_tcon = cli->smb2.tcon;
1736         cli->smb2.tcon = smbXcli_tcon_create(cli);
1737         smb2cli_tcon_set_values(cli->smb2.tcon,
1738                                 NULL, /* session */
1739                                 saved_tid,
1740                                 0, /* type */
1741                                 0, /* flags */
1742                                 0, /* capabilities */
1743                                 0  /* maximal_access */);
1744         status = cli_tree_connect(cli, share, "?????", "", 0);
1745         if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
1746                 printf("cli_tree_connect returned %s\n", nt_errstr(status));
1747                 return false;
1748         }
1749         talloc_free(cli->smb2.tcon);
1750         cli->smb2.tcon = saved_tcon;
1751
1752         subreq = smb2cli_session_setup_send(talloc_tos(), ev,
1753                                             cli->conn,
1754                                             cli->timeout,
1755                                             cli->smb2.session,
1756                                             0x0, /* in_flags */
1757                                             SMB2_CAP_DFS, /* in_capabilities */
1758                                             0, /* in_channel */
1759                                             0, /* in_previous_session_id */
1760                                             &in_blob); /* in_security_buffer */
1761         if (subreq == NULL) {
1762                 printf("smb2cli_session_setup_send() returned NULL\n");
1763                 return false;
1764         }
1765
1766         ok = tevent_req_poll(subreq, ev);
1767         if (!ok) {
1768                 printf("tevent_req_poll() returned false\n");
1769                 return false;
1770         }
1771
1772         status = smb2cli_session_setup_recv(subreq, talloc_tos(),
1773                                             &recv_iov, &out_blob);
1774         if (!NT_STATUS_IS_OK(status)) {
1775                 printf("smb2cli_session_setup_recv returned %s\n",
1776                         nt_errstr(status));
1777                 return false;
1778         }
1779
1780         status = smb2cli_flush(cli->conn, cli->timeout, cli->smb2.session,
1781                                cli->smb2.tcon, fid_persistent, fid_volatile);
1782         if (!NT_STATUS_IS_OK(status)) {
1783                 printf("smb2cli_flush returned %s\n", nt_errstr(status));
1784                 return false;
1785         }
1786
1787         status = smb2cli_query_info(cli->conn,
1788                                     cli->timeout,
1789                                     cli->smb2.session,
1790                                     cli->smb2.tcon,
1791                                     SMB2_GETINFO_SECURITY,
1792                                     0, /* in_file_info_class */
1793                                     1024, /* in_max_output_length */
1794                                     NULL, /* in_input_buffer */
1795                                     SECINFO_OWNER, /* in_additional_info */
1796                                     0, /* in_flags */
1797                                     fid_persistent,
1798                                     fid_volatile,
1799                                     talloc_tos(),
1800                                     &out_output_buffer);
1801         if (!NT_STATUS_IS_OK(status)) {
1802                 printf("smb2cli_query_info (security) returned %s\n", nt_errstr(status));
1803                 return false;
1804         }
1805
1806         in_file_info_class = SMB_FILE_POSITION_INFORMATION - 1000;
1807         status = smb2cli_query_info(cli->conn,
1808                                     cli->timeout,
1809                                     cli->smb2.session,
1810                                     cli->smb2.tcon,
1811                                     SMB2_GETINFO_FILE,
1812                                     in_file_info_class,
1813                                     1024, /* in_max_output_length */
1814                                     NULL, /* in_input_buffer */
1815                                     0, /* in_additional_info */
1816                                     0, /* in_flags */
1817                                     fid_persistent,
1818                                     fid_volatile,
1819                                     talloc_tos(),
1820                                     &out_output_buffer);
1821         if (!NT_STATUS_IS_OK(status)) {
1822                 printf("smb2cli_query_info (position) returned %s\n", nt_errstr(status));
1823                 return false;
1824         }
1825
1826         in_input_buffer = data_blob_talloc(talloc_tos(), NULL, 8);
1827         SBVAL(in_input_buffer.data, 0, 512);
1828
1829         in_file_info_class = SMB_FILE_POSITION_INFORMATION - 1000;
1830         status = smb2cli_set_info(cli->conn,
1831                                   cli->timeout,
1832                                   cli->smb2.session,
1833                                   cli->smb2.tcon,
1834                                   SMB2_GETINFO_FILE,
1835                                   in_file_info_class,
1836                                   &in_input_buffer,
1837                                   0, /* in_additional_info */
1838                                   fid_persistent,
1839                                   fid_volatile);
1840         if (!NT_STATUS_IS_OK(status)) {
1841                 printf("smb2cli_set_info (position) returned %s\n", nt_errstr(status));
1842                 return false;
1843         }
1844
1845         in_file_info_class = SMB_FILE_POSITION_INFORMATION - 1000;
1846         status = smb2cli_query_info(cli->conn,
1847                                     cli->timeout,
1848                                     cli->smb2.session,
1849                                     cli->smb2.tcon,
1850                                     SMB2_GETINFO_FILE,
1851                                     in_file_info_class,
1852                                     1024, /* in_max_output_length */
1853                                     NULL, /* in_input_buffer */
1854                                     0, /* in_additional_info */
1855                                     0, /* in_flags */
1856                                     fid_persistent,
1857                                     fid_volatile,
1858                                     talloc_tos(),
1859                                     &out_output_buffer);
1860         if (!NT_STATUS_IS_OK(status)) {
1861                 printf("smb2cli_query_info (position) returned %s\n", nt_errstr(status));
1862                 return false;
1863         }
1864
1865         status = smb2cli_close(cli->conn, cli->timeout, cli->smb2.session,
1866                                cli->smb2.tcon, 0, fid_persistent, fid_volatile);
1867         if (!NT_STATUS_IS_OK(status)) {
1868                 printf("smb2cli_close returned %s\n", nt_errstr(status));
1869                 return false;
1870         }
1871
1872         status = smb2cli_create(cli->conn, cli->timeout, cli->smb2.session,
1873                         cli->smb2.tcon, "session-reauth.txt",
1874                         SMB2_OPLOCK_LEVEL_NONE, /* oplock_level, */
1875                         SMB2_IMPERSONATION_IMPERSONATION, /* impersonation_level, */
1876                         SEC_STD_ALL | SEC_FILE_ALL, /* desired_access, */
1877                         FILE_ATTRIBUTE_NORMAL, /* file_attributes, */
1878                         FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access, */
1879                         FILE_CREATE, /* create_disposition, */
1880                         FILE_DELETE_ON_CLOSE, /* create_options, */
1881                         NULL, /* smb2_create_blobs *blobs */
1882                         &fid_persistent,
1883                         &fid_volatile,
1884                         NULL);
1885         if (!NT_STATUS_IS_OK(status)) {
1886                 printf("smb2cli_create %s\n", nt_errstr(status));
1887                 return false;
1888         }
1889
1890         status = smb2cli_query_directory(
1891                 cli->conn, cli->timeout, cli->smb2.session, cli->smb2.tcon,
1892                 1, 0x3, 0, dir_persistent, dir_volatile,
1893                 "session-reauth.txt", 0xffff,
1894                 talloc_tos(), &dir_data, &dir_data_length);
1895         if (!NT_STATUS_IS_OK(status)) {
1896                 printf("smb2cli_query_directory returned %s\n", nt_errstr(status));
1897                 return false;
1898         }
1899
1900         status = smb2cli_close(cli->conn, cli->timeout, cli->smb2.session,
1901                                cli->smb2.tcon, 0, dir_persistent, dir_volatile);
1902         if (!NT_STATUS_IS_OK(status)) {
1903                 printf("smb2cli_close returned %s\n", nt_errstr(status));
1904                 return false;
1905         }
1906
1907         status = smb2cli_close(cli->conn, cli->timeout, cli->smb2.session,
1908                                cli->smb2.tcon, 0, fid_persistent, fid_volatile);
1909         if (!NT_STATUS_IS_OK(status)) {
1910                 printf("smb2cli_close returned %s\n", nt_errstr(status));
1911                 return false;
1912         }
1913
1914         saved_tid = smb2cli_tcon_current_id(cli->smb2.tcon);
1915         saved_tcon = cli->smb2.tcon;
1916         cli->smb2.tcon = smbXcli_tcon_create(cli);
1917         smb2cli_tcon_set_values(cli->smb2.tcon,
1918                                 NULL, /* session */
1919                                 saved_tid,
1920                                 0, /* type */
1921                                 0, /* flags */
1922                                 0, /* capabilities */
1923                                 0  /* maximal_access */);
1924         status = cli_tree_connect(cli, share, "?????", "", 0);
1925         if (!NT_STATUS_IS_OK(status)) {
1926                 printf("cli_tree_connect returned %s\n", nt_errstr(status));
1927                 return false;
1928         }
1929         talloc_free(cli->smb2.tcon);
1930         cli->smb2.tcon = saved_tcon;
1931
1932         return true;
1933 }