2 Unix SMB/CIFS implementation.
3 test suite for session setup operations
4 Copyright (C) Andrew Tridgell 2003
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 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "libcli/raw/libcliraw.h"
23 #include "librpc/gen_ndr/ndr_security.h"
24 #include "libcli/composite/composite.h"
25 #include "lib/cmdline/popt_common.h"
26 #include "lib/events/events.h"
28 #define BASEDIR "\\rawcontext"
30 #define CHECK_STATUS(status, correct) do { \
31 if (!NT_STATUS_EQUAL(status, correct)) { \
32 printf("(%s) Incorrect status %s - should be %s\n", \
33 __location__, nt_errstr(status), nt_errstr(correct)); \
38 #define CHECK_VALUE(v, correct) do { \
39 if ((v) != (correct)) { \
40 printf("(%s) Incorrect value %s=%d - should be %d\n", \
41 __location__, #v, v, correct); \
46 #define CHECK_NOT_VALUE(v, correct) do { \
47 if ((v) == (correct)) { \
48 printf("(%s) Incorrect value %s=%d - should not be %d\n", \
49 __location__, #v, v, correct); \
58 static BOOL test_session(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
62 struct smbcli_session *session;
63 struct smbcli_session *session2;
64 struct smbcli_session *session3;
65 struct smbcli_session *session4;
66 struct cli_credentials *anon_creds;
67 struct smbcli_session *sessions[15];
68 struct composite_context *composite_contexts[15];
69 struct smbcli_tree *tree;
70 struct smb_composite_sesssetup setup;
71 struct smb_composite_sesssetup setups[15];
76 const char *fname = BASEDIR "\\test.txt";
80 printf("TESTING SESSION HANDLING\n");
82 if (!torture_setup_dir(cli, BASEDIR)) {
86 printf("create a second security context on the same transport\n");
87 session = smbcli_session_init(cli->transport, mem_ctx, False);
89 setup.in.sesskey = cli->transport->negotiate.sesskey;
90 setup.in.capabilities = cli->transport->negotiate.capabilities; /* ignored in secondary session setup, except by our libs, which care about the extended security bit */
91 setup.in.workgroup = lp_workgroup();
93 setup.in.credentials = cmdline_credentials;
95 status = smb_composite_sesssetup(session, &setup);
96 CHECK_STATUS(status, NT_STATUS_OK);
98 session->vuid = setup.out.vuid;
100 printf("create a third security context on the same transport, with vuid set\n");
101 session2 = smbcli_session_init(cli->transport, mem_ctx, False);
103 session2->vuid = session->vuid;
104 setup.in.sesskey = cli->transport->negotiate.sesskey;
105 setup.in.capabilities = cli->transport->negotiate.capabilities; /* ignored in secondary session setup, except by our libs, which care about the extended security bit */
106 setup.in.workgroup = lp_workgroup();
108 setup.in.credentials = cmdline_credentials;
110 status = smb_composite_sesssetup(session2, &setup);
111 CHECK_STATUS(status, NT_STATUS_OK);
113 session2->vuid = setup.out.vuid;
114 printf("vuid1=%d vuid2=%d vuid3=%d\n", cli->session->vuid, session->vuid, session2->vuid);
116 if (cli->transport->negotiate.capabilities & CAP_EXTENDED_SECURITY) {
117 /* Samba4 currently fails this - we need to determine if this insane behaviour is important */
118 if (session2->vuid == session->vuid) {
119 printf("server allows the user to re-use an existing vuid in session setup \n");
122 CHECK_NOT_VALUE(session2->vuid, session->vuid);
124 talloc_free(session2);
126 if (cli->transport->negotiate.capabilities & CAP_EXTENDED_SECURITY) {
127 printf("create a fourth security context on the same transport, without extended security\n");
128 session3 = smbcli_session_init(cli->transport, mem_ctx, False);
130 session3->vuid = session->vuid;
131 setup.in.sesskey = cli->transport->negotiate.sesskey;
132 setup.in.capabilities &= ~CAP_EXTENDED_SECURITY; /* force a non extended security login (should fail) */
133 setup.in.workgroup = lp_workgroup();
135 setup.in.credentials = cmdline_credentials;
138 status = smb_composite_sesssetup(session3, &setup);
139 CHECK_STATUS(status, NT_STATUS_LOGON_FAILURE);
141 printf("create a fouth anonymous security context on the same transport, without extended security\n");
142 session4 = smbcli_session_init(cli->transport, mem_ctx, False);
144 session4->vuid = session->vuid;
145 setup.in.sesskey = cli->transport->negotiate.sesskey;
146 setup.in.capabilities &= ~CAP_EXTENDED_SECURITY; /* force a non extended security login (should fail) */
147 setup.in.workgroup = lp_workgroup();
149 anon_creds = cli_credentials_init(mem_ctx);
150 cli_credentials_set_conf(anon_creds);
151 cli_credentials_set_anonymous(anon_creds);
153 setup.in.credentials = anon_creds;
155 status = smb_composite_sesssetup(session3, &setup);
156 CHECK_STATUS(status, NT_STATUS_OK);
158 talloc_free(session4);
161 printf("use the same tree as the existing connection\n");
162 tree = smbcli_tree_init(session, mem_ctx, False);
163 tree->tid = cli->tree->tid;
165 printf("create a file using the new vuid\n");
166 io.generic.level = RAW_OPEN_NTCREATEX;
167 io.ntcreatex.in.root_fid = 0;
168 io.ntcreatex.in.flags = 0;
169 io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
170 io.ntcreatex.in.create_options = 0;
171 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
172 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
173 io.ntcreatex.in.alloc_size = 0;
174 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
175 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
176 io.ntcreatex.in.security_flags = 0;
177 io.ntcreatex.in.fname = fname;
178 status = smb_raw_open(tree, mem_ctx, &io);
179 CHECK_STATUS(status, NT_STATUS_OK);
180 fnum = io.ntcreatex.out.fnum;
182 printf("write using the old vuid\n");
183 wr.generic.level = RAW_WRITE_WRITEX;
184 wr.writex.in.fnum = fnum;
185 wr.writex.in.offset = 0;
186 wr.writex.in.wmode = 0;
187 wr.writex.in.remaining = 0;
188 wr.writex.in.count = 1;
189 wr.writex.in.data = &c;
191 status = smb_raw_write(cli->tree, &wr);
192 CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
194 printf("write with the new vuid\n");
195 status = smb_raw_write(tree, &wr);
196 CHECK_STATUS(status, NT_STATUS_OK);
197 CHECK_VALUE(wr.writex.out.nwritten, 1);
199 printf("logoff the new vuid\n");
200 status = smb_raw_ulogoff(session);
201 CHECK_STATUS(status, NT_STATUS_OK);
203 printf("the new vuid should not now be accessible\n");
204 status = smb_raw_write(tree, &wr);
205 CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
207 printf("second logoff for the new vuid should fail\n");
208 status = smb_raw_ulogoff(session);
209 CHECK_STATUS(status, NT_STATUS_DOS(ERRSRV, ERRbaduid));
210 talloc_free(session);
212 printf("the fnum should have been auto-closed\n");
213 cl.close.level = RAW_CLOSE_CLOSE;
214 cl.close.in.fnum = fnum;
215 cl.close.in.write_time = 0;
216 status = smb_raw_close(cli->tree, &cl);
217 CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
219 printf("create %d secondary security contexts on the same transport\n",
220 ARRAY_SIZE(sessions));
221 for (i=0; i <ARRAY_SIZE(sessions); i++) {
222 setups[i].in.sesskey = cli->transport->negotiate.sesskey;
223 setups[i].in.capabilities = cli->transport->negotiate.capabilities; /* ignored in secondary session setup, except by our libs, which care about the extended security bit */
224 setups[i].in.workgroup = lp_workgroup();
226 setups[i].in.credentials = cmdline_credentials;
228 sessions[i] = smbcli_session_init(cli->transport, mem_ctx, False);
229 composite_contexts[i] = smb_composite_sesssetup_send(sessions[i], &setups[i]);
234 /* flush the queue */
235 for (i=0; i < ARRAY_SIZE(sessions); i++) {
236 event_loop_once(composite_contexts[0]->event_ctx);
239 printf("finishing %d secondary security contexts on the same transport\n",
240 ARRAY_SIZE(sessions));
241 for (i=0; i< ARRAY_SIZE(sessions); i++) {
242 status = smb_composite_sesssetup_recv(composite_contexts[i]);
243 CHECK_STATUS(status, NT_STATUS_OK);
244 sessions[i]->vuid = setups[i].out.vuid;
245 printf("VUID: %d\n", sessions[i]->vuid);
246 status = smb_raw_ulogoff(sessions[i]);
247 CHECK_STATUS(status, NT_STATUS_OK);
261 static BOOL test_tree(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
266 struct smbcli_tree *tree;
272 const char *fname = BASEDIR "\\test.txt";
275 printf("TESTING TREE HANDLING\n");
277 if (!torture_setup_dir(cli, BASEDIR)) {
281 share = lp_parm_string(-1, "torture", "share");
283 printf("create a second tree context on the same session\n");
284 tree = smbcli_tree_init(cli->session, mem_ctx, False);
286 tcon.generic.level = RAW_TCON_TCONX;
287 tcon.tconx.in.flags = 0;
288 tcon.tconx.in.password = data_blob(NULL, 0);
289 tcon.tconx.in.path = share;
290 tcon.tconx.in.device = "A:";
291 status = smb_tree_connect(tree, mem_ctx, &tcon);
292 CHECK_STATUS(status, NT_STATUS_OK);
294 tree->tid = tcon.tconx.out.tid;
295 printf("tid1=%d tid2=%d\n", cli->tree->tid, tree->tid);
297 printf("try a tconx with a bad device type\n");
298 tcon.tconx.in.device = "FOO";
299 status = smb_tree_connect(tree, mem_ctx, &tcon);
300 CHECK_STATUS(status, NT_STATUS_BAD_DEVICE_TYPE);
303 printf("create a file using the new tid\n");
304 io.generic.level = RAW_OPEN_NTCREATEX;
305 io.ntcreatex.in.root_fid = 0;
306 io.ntcreatex.in.flags = 0;
307 io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
308 io.ntcreatex.in.create_options = 0;
309 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
310 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
311 io.ntcreatex.in.alloc_size = 0;
312 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
313 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
314 io.ntcreatex.in.security_flags = 0;
315 io.ntcreatex.in.fname = fname;
316 status = smb_raw_open(tree, mem_ctx, &io);
317 CHECK_STATUS(status, NT_STATUS_OK);
318 fnum = io.ntcreatex.out.fnum;
320 printf("write using the old tid\n");
321 wr.generic.level = RAW_WRITE_WRITEX;
322 wr.writex.in.fnum = fnum;
323 wr.writex.in.offset = 0;
324 wr.writex.in.wmode = 0;
325 wr.writex.in.remaining = 0;
326 wr.writex.in.count = 1;
327 wr.writex.in.data = &c;
329 status = smb_raw_write(cli->tree, &wr);
330 CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
332 printf("write with the new tid\n");
333 status = smb_raw_write(tree, &wr);
334 CHECK_STATUS(status, NT_STATUS_OK);
335 CHECK_VALUE(wr.writex.out.nwritten, 1);
337 printf("disconnect the new tid\n");
338 status = smb_tree_disconnect(tree);
339 CHECK_STATUS(status, NT_STATUS_OK);
341 printf("the new tid should not now be accessible\n");
342 status = smb_raw_write(tree, &wr);
343 CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
345 printf("the fnum should have been auto-closed\n");
346 cl.close.level = RAW_CLOSE_CLOSE;
347 cl.close.in.fnum = fnum;
348 cl.close.in.write_time = 0;
349 status = smb_raw_close(cli->tree, &cl);
350 CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
352 /* close down the new tree */
363 static BOOL test_pid(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
371 const char *fname = BASEDIR "\\test.txt";
375 printf("TESTING PID HANDLING\n");
377 if (!torture_setup_dir(cli, BASEDIR)) {
381 printf("create a second pid\n");
382 pid1 = cli->session->pid;
385 printf("pid1=%d pid2=%d\n", pid1, pid2);
387 printf("create a file using the new pid\n");
388 cli->session->pid = pid2;
389 io.generic.level = RAW_OPEN_NTCREATEX;
390 io.ntcreatex.in.root_fid = 0;
391 io.ntcreatex.in.flags = 0;
392 io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
393 io.ntcreatex.in.create_options = 0;
394 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
395 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
396 io.ntcreatex.in.alloc_size = 0;
397 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
398 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
399 io.ntcreatex.in.security_flags = 0;
400 io.ntcreatex.in.fname = fname;
401 status = smb_raw_open(cli->tree, mem_ctx, &io);
402 CHECK_STATUS(status, NT_STATUS_OK);
403 fnum = io.ntcreatex.out.fnum;
405 printf("write using the old pid\n");
406 cli->session->pid = pid1;
407 wr.generic.level = RAW_WRITE_WRITEX;
408 wr.writex.in.fnum = fnum;
409 wr.writex.in.offset = 0;
410 wr.writex.in.wmode = 0;
411 wr.writex.in.remaining = 0;
412 wr.writex.in.count = 1;
413 wr.writex.in.data = &c;
415 status = smb_raw_write(cli->tree, &wr);
416 CHECK_STATUS(status, NT_STATUS_OK);
417 CHECK_VALUE(wr.writex.out.nwritten, 1);
419 printf("write with the new pid\n");
420 cli->session->pid = pid2;
421 status = smb_raw_write(cli->tree, &wr);
422 CHECK_STATUS(status, NT_STATUS_OK);
423 CHECK_VALUE(wr.writex.out.nwritten, 1);
425 printf("exit the old pid\n");
426 cli->session->pid = pid1;
427 status = smb_raw_exit(cli->session);
428 CHECK_STATUS(status, NT_STATUS_OK);
430 printf("the fnum should still be accessible\n");
431 cli->session->pid = pid1;
432 status = smb_raw_write(cli->tree, &wr);
433 CHECK_STATUS(status, NT_STATUS_OK);
434 CHECK_VALUE(wr.writex.out.nwritten, 1);
436 printf("exit the new pid\n");
437 cli->session->pid = pid2;
438 status = smb_raw_exit(cli->session);
439 CHECK_STATUS(status, NT_STATUS_OK);
441 printf("the fnum should not now be accessible\n");
442 cli->session->pid = pid1;
443 status = smb_raw_write(cli->tree, &wr);
444 CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
446 printf("the fnum should have been auto-closed\n");
447 cl.close.level = RAW_CLOSE_CLOSE;
448 cl.close.in.fnum = fnum;
449 cl.close.in.write_time = 0;
450 status = smb_raw_close(cli->tree, &cl);
451 CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
459 basic testing of session/tree context calls
461 static BOOL torture_raw_context_int(void)
463 struct smbcli_state *cli;
467 if (!torture_open_connection(&cli)) {
471 mem_ctx = talloc_init("torture_raw_context");
473 if (!test_session(cli, mem_ctx)) {
477 if (!test_tree(cli, mem_ctx)) {
481 if (!test_pid(cli, mem_ctx)) {
485 smb_raw_exit(cli->session);
486 smbcli_deltree(cli->tree, BASEDIR);
488 torture_close_connection(cli);
489 talloc_free(mem_ctx);
494 basic testing of session/tree context calls
496 BOOL torture_raw_context(void)
499 if (lp_use_spnego()) {
500 ret &= torture_raw_context_int();
501 lp_set_cmdline("use spnego", "False");
504 ret &= torture_raw_context_int();