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