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"
27 #define BASEDIR "\\rawcontext"
29 #define CHECK_STATUS(status, correct) do { \
30 if (!NT_STATUS_EQUAL(status, correct)) { \
31 printf("(%s) Incorrect status %s - should be %s\n", \
32 __location__, nt_errstr(status), nt_errstr(correct)); \
37 #define CHECK_VALUE(v, correct) do { \
38 if ((v) != (correct)) { \
39 printf("(%s) Incorrect value %s=%d - should be %d\n", \
40 __location__, #v, v, correct); \
45 #define CHECK_NOT_VALUE(v, correct) do { \
46 if ((v) == (correct)) { \
47 printf("(%s) Incorrect value %s=%d - should not be %d\n", \
48 __location__, #v, v, correct); \
57 static BOOL test_session(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
61 struct smbcli_session *session;
62 struct smbcli_session *session2;
63 struct smbcli_session *session3;
64 struct smbcli_tree *tree;
65 struct smb_composite_sesssetup setup;
70 const char *fname = BASEDIR "\\test.txt";
73 printf("TESTING SESSION HANDLING\n");
75 if (!torture_setup_dir(cli, BASEDIR)) {
79 printf("create a second security context on the same transport\n");
80 session = smbcli_session_init(cli->transport, mem_ctx, False);
82 setup.in.sesskey = cli->transport->negotiate.sesskey;
83 setup.in.capabilities = cli->transport->negotiate.capabilities; /* ignored in secondary session setup, except by our libs, which care about the extended security bit */
84 setup.in.password = cli_credentials_get_password(cmdline_credentials);
85 setup.in.user = cli_credentials_get_username(cmdline_credentials);
86 setup.in.domain = cli_credentials_get_domain(cmdline_credentials);
88 status = smb_composite_sesssetup(session, &setup);
89 CHECK_STATUS(status, NT_STATUS_OK);
91 session->vuid = setup.out.vuid;
93 printf("create a third security context on the same transport, with vuid set\n");
94 session2 = smbcli_session_init(cli->transport, mem_ctx, False);
96 session2->vuid = session->vuid;
97 setup.in.sesskey = cli->transport->negotiate.sesskey;
98 setup.in.capabilities = cli->transport->negotiate.capabilities; /* ignored in secondary session setup, except by our libs, which care about the extended security bit */
100 setup.in.password = cli_credentials_get_password(cmdline_credentials);
101 setup.in.user = cli_credentials_get_username(cmdline_credentials);
102 setup.in.domain = cli_credentials_get_domain(cmdline_credentials);
104 status = smb_composite_sesssetup(session2, &setup);
105 CHECK_STATUS(status, NT_STATUS_OK);
107 session2->vuid = setup.out.vuid;
108 printf("vuid1=%d vuid2=%d vuid3=%d\n", cli->session->vuid, session->vuid, session2->vuid);
110 CHECK_NOT_VALUE(session->vuid, session2->vuid);
111 talloc_free(session2);
113 if (cli->transport->negotiate.capabilities & CAP_EXTENDED_SECURITY) {
114 printf("create a fourth security context on the same transport, without extended security\n");
115 session3 = smbcli_session_init(cli->transport, mem_ctx, False);
117 session3->vuid = session->vuid;
118 setup.in.sesskey = cli->transport->negotiate.sesskey;
119 setup.in.capabilities = 0; /* force a non extended security login (should fail) */
122 setup.in.password = cli_credentials_get_password(cmdline_credentials);
123 setup.in.user = cli_credentials_get_username(cmdline_credentials);
124 setup.in.domain = cli_credentials_get_domain(cmdline_credentials);
126 status = smb_composite_sesssetup(session3, &setup);
127 CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
129 talloc_free(session3);
132 printf("use the same tree as the existing connection\n");
133 tree = smbcli_tree_init(session, mem_ctx, False);
134 tree->tid = cli->tree->tid;
136 printf("create a file using the new vuid\n");
137 io.generic.level = RAW_OPEN_NTCREATEX;
138 io.ntcreatex.in.root_fid = 0;
139 io.ntcreatex.in.flags = 0;
140 io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
141 io.ntcreatex.in.create_options = 0;
142 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
143 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
144 io.ntcreatex.in.alloc_size = 0;
145 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
146 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
147 io.ntcreatex.in.security_flags = 0;
148 io.ntcreatex.in.fname = fname;
149 status = smb_raw_open(tree, mem_ctx, &io);
150 CHECK_STATUS(status, NT_STATUS_OK);
151 fnum = io.ntcreatex.out.fnum;
153 printf("write using the old vuid\n");
154 wr.generic.level = RAW_WRITE_WRITEX;
155 wr.writex.in.fnum = fnum;
156 wr.writex.in.offset = 0;
157 wr.writex.in.wmode = 0;
158 wr.writex.in.remaining = 0;
159 wr.writex.in.count = 1;
160 wr.writex.in.data = &c;
162 status = smb_raw_write(cli->tree, &wr);
163 CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
165 printf("write with the new vuid\n");
166 status = smb_raw_write(tree, &wr);
167 CHECK_STATUS(status, NT_STATUS_OK);
168 CHECK_VALUE(wr.writex.out.nwritten, 1);
170 printf("logoff the new vuid\n");
171 status = smb_raw_ulogoff(session);
172 CHECK_STATUS(status, NT_STATUS_OK);
173 talloc_free(session);
175 printf("the new vuid should not now be accessible\n");
176 status = smb_raw_write(tree, &wr);
177 CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
179 printf("the fnum should have been auto-closed\n");
180 cl.close.level = RAW_CLOSE_CLOSE;
181 cl.close.in.fnum = fnum;
182 cl.close.in.write_time = 0;
183 status = smb_raw_close(cli->tree, &cl);
184 CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
196 static BOOL test_tree(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
201 struct smbcli_tree *tree;
207 const char *fname = BASEDIR "\\test.txt";
210 printf("TESTING TREE HANDLING\n");
212 if (!torture_setup_dir(cli, BASEDIR)) {
216 share = lp_parm_string(-1, "torture", "share");
218 printf("create a second tree context on the same session\n");
219 tree = smbcli_tree_init(cli->session, mem_ctx, False);
221 tcon.generic.level = RAW_TCON_TCONX;
222 tcon.tconx.in.flags = 0;
223 tcon.tconx.in.password = data_blob(NULL, 0);
224 tcon.tconx.in.path = share;
225 tcon.tconx.in.device = "A:";
226 status = smb_tree_connect(tree, mem_ctx, &tcon);
227 CHECK_STATUS(status, NT_STATUS_OK);
229 tree->tid = tcon.tconx.out.tid;
230 printf("tid1=%d tid2=%d\n", cli->tree->tid, tree->tid);
232 printf("try a tconx with a bad device type\n");
233 tcon.tconx.in.device = "FOO";
234 status = smb_tree_connect(tree, mem_ctx, &tcon);
235 CHECK_STATUS(status, NT_STATUS_BAD_DEVICE_TYPE);
238 printf("create a file using the new tid\n");
239 io.generic.level = RAW_OPEN_NTCREATEX;
240 io.ntcreatex.in.root_fid = 0;
241 io.ntcreatex.in.flags = 0;
242 io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
243 io.ntcreatex.in.create_options = 0;
244 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
245 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
246 io.ntcreatex.in.alloc_size = 0;
247 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
248 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
249 io.ntcreatex.in.security_flags = 0;
250 io.ntcreatex.in.fname = fname;
251 status = smb_raw_open(tree, mem_ctx, &io);
252 CHECK_STATUS(status, NT_STATUS_OK);
253 fnum = io.ntcreatex.out.fnum;
255 printf("write using the old tid\n");
256 wr.generic.level = RAW_WRITE_WRITEX;
257 wr.writex.in.fnum = fnum;
258 wr.writex.in.offset = 0;
259 wr.writex.in.wmode = 0;
260 wr.writex.in.remaining = 0;
261 wr.writex.in.count = 1;
262 wr.writex.in.data = &c;
264 status = smb_raw_write(cli->tree, &wr);
265 CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
267 printf("write with the new tid\n");
268 status = smb_raw_write(tree, &wr);
269 CHECK_STATUS(status, NT_STATUS_OK);
270 CHECK_VALUE(wr.writex.out.nwritten, 1);
272 printf("disconnect the new tid\n");
273 status = smb_tree_disconnect(tree);
274 CHECK_STATUS(status, NT_STATUS_OK);
276 printf("the new tid should not now be accessible\n");
277 status = smb_raw_write(tree, &wr);
278 CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
280 printf("the fnum should have been auto-closed\n");
281 cl.close.level = RAW_CLOSE_CLOSE;
282 cl.close.in.fnum = fnum;
283 cl.close.in.write_time = 0;
284 status = smb_raw_close(cli->tree, &cl);
285 CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
287 /* close down the new tree */
298 static BOOL test_pid(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
306 const char *fname = BASEDIR "\\test.txt";
310 printf("TESTING PID HANDLING\n");
312 if (!torture_setup_dir(cli, BASEDIR)) {
316 printf("create a second pid\n");
317 pid1 = cli->session->pid;
320 printf("pid1=%d pid2=%d\n", pid1, pid2);
322 printf("create a file using the new pid\n");
323 cli->session->pid = pid2;
324 io.generic.level = RAW_OPEN_NTCREATEX;
325 io.ntcreatex.in.root_fid = 0;
326 io.ntcreatex.in.flags = 0;
327 io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
328 io.ntcreatex.in.create_options = 0;
329 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
330 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
331 io.ntcreatex.in.alloc_size = 0;
332 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
333 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
334 io.ntcreatex.in.security_flags = 0;
335 io.ntcreatex.in.fname = fname;
336 status = smb_raw_open(cli->tree, mem_ctx, &io);
337 CHECK_STATUS(status, NT_STATUS_OK);
338 fnum = io.ntcreatex.out.fnum;
340 printf("write using the old pid\n");
341 cli->session->pid = pid1;
342 wr.generic.level = RAW_WRITE_WRITEX;
343 wr.writex.in.fnum = fnum;
344 wr.writex.in.offset = 0;
345 wr.writex.in.wmode = 0;
346 wr.writex.in.remaining = 0;
347 wr.writex.in.count = 1;
348 wr.writex.in.data = &c;
350 status = smb_raw_write(cli->tree, &wr);
351 CHECK_STATUS(status, NT_STATUS_OK);
352 CHECK_VALUE(wr.writex.out.nwritten, 1);
354 printf("write with the new pid\n");
355 cli->session->pid = pid2;
356 status = smb_raw_write(cli->tree, &wr);
357 CHECK_STATUS(status, NT_STATUS_OK);
358 CHECK_VALUE(wr.writex.out.nwritten, 1);
360 printf("exit the old pid\n");
361 cli->session->pid = pid1;
362 status = smb_raw_exit(cli->session);
363 CHECK_STATUS(status, NT_STATUS_OK);
365 printf("the fnum should still be accessible\n");
366 cli->session->pid = pid1;
367 status = smb_raw_write(cli->tree, &wr);
368 CHECK_STATUS(status, NT_STATUS_OK);
369 CHECK_VALUE(wr.writex.out.nwritten, 1);
371 printf("exit the new pid\n");
372 cli->session->pid = pid2;
373 status = smb_raw_exit(cli->session);
374 CHECK_STATUS(status, NT_STATUS_OK);
376 printf("the fnum should not now be accessible\n");
377 cli->session->pid = pid1;
378 status = smb_raw_write(cli->tree, &wr);
379 CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
381 printf("the fnum should have been auto-closed\n");
382 cl.close.level = RAW_CLOSE_CLOSE;
383 cl.close.in.fnum = fnum;
384 cl.close.in.write_time = 0;
385 status = smb_raw_close(cli->tree, &cl);
386 CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
394 basic testing of session/tree context calls
396 BOOL torture_raw_context(void)
398 struct smbcli_state *cli;
402 lp_set_cmdline("use spnego", "False");
404 if (!torture_open_connection(&cli)) {
408 mem_ctx = talloc_init("torture_raw_context");
410 if (!test_session(cli, mem_ctx)) {
414 if (!test_tree(cli, mem_ctx)) {
418 if (!test_pid(cli, mem_ctx)) {
422 smb_raw_exit(cli->session);
423 smbcli_deltree(cli->tree, BASEDIR);
425 torture_close_connection(cli);
426 talloc_free(mem_ctx);