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"
26 #define BASEDIR "\\rawcontext"
28 #define CHECK_STATUS(status, correct) do { \
29 if (!NT_STATUS_EQUAL(status, correct)) { \
30 printf("(%s) Incorrect status %s - should be %s\n", \
31 __location__, nt_errstr(status), nt_errstr(correct)); \
36 #define CHECK_VALUE(v, correct) do { \
37 if ((v) != (correct)) { \
38 printf("(%s) Incorrect value %s=%d - should be %d\n", \
39 __location__, #v, v, correct); \
44 #define CHECK_NOT_VALUE(v, correct) do { \
45 if ((v) == (correct)) { \
46 printf("(%s) Incorrect value %s=%d - should not be %d\n", \
47 __location__, #v, v, correct); \
56 static BOOL test_session(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
60 const char *username, *domain, *password;
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 username = lp_parm_string(-1, "torture", "username");
80 password = lp_parm_string(-1, "torture", "password");
81 domain = lp_parm_string(-1, "torture", "userdomain");
83 printf("create a second security context on the same transport\n");
84 session = smbcli_session_init(cli->transport, mem_ctx, False);
86 setup.in.sesskey = cli->transport->negotiate.sesskey;
87 setup.in.capabilities = cli->transport->negotiate.capabilities; /* ignored in secondary session setup, except by our libs, which care about the extended security bit */
88 setup.in.password = password;
89 setup.in.user = username;
90 setup.in.domain = domain;
92 status = smb_composite_sesssetup(session, &setup);
93 CHECK_STATUS(status, NT_STATUS_OK);
95 session->vuid = setup.out.vuid;
97 printf("create a third security context on the same transport, with vuid set\n");
98 session2 = smbcli_session_init(cli->transport, mem_ctx, False);
100 session2->vuid = session->vuid;
101 setup.in.sesskey = cli->transport->negotiate.sesskey;
102 setup.in.capabilities = cli->transport->negotiate.capabilities; /* ignored in secondary session setup, except by our libs, which care about the extended security bit */
103 setup.in.password = password;
104 setup.in.user = username;
105 setup.in.domain = domain;
107 status = smb_composite_sesssetup(session2, &setup);
108 CHECK_STATUS(status, NT_STATUS_OK);
110 session2->vuid = setup.out.vuid;
111 printf("vuid1=%d vuid2=%d vuid3=%d\n", cli->session->vuid, session->vuid, session2->vuid);
113 CHECK_NOT_VALUE(session->vuid, session2->vuid);
114 talloc_free(session2);
116 if (cli->transport->negotiate.capabilities & CAP_EXTENDED_SECURITY) {
117 printf("create a fourth security context on the same transport, without extended security\n");
118 session3 = smbcli_session_init(cli->transport, mem_ctx, False);
120 session3->vuid = session->vuid;
121 setup.in.sesskey = cli->transport->negotiate.sesskey;
122 setup.in.capabilities = 0; /* force a non extended security login (should fail) */
123 setup.in.password = password;
124 setup.in.user = username;
125 setup.in.domain = domain;
127 status = smb_composite_sesssetup(session3, &setup);
128 CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
130 talloc_free(session3);
133 printf("use the same tree as the existing connection\n");
134 tree = smbcli_tree_init(session, mem_ctx, False);
135 tree->tid = cli->tree->tid;
137 printf("create a file using the new vuid\n");
138 io.generic.level = RAW_OPEN_NTCREATEX;
139 io.ntcreatex.in.root_fid = 0;
140 io.ntcreatex.in.flags = 0;
141 io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
142 io.ntcreatex.in.create_options = 0;
143 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
144 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
145 io.ntcreatex.in.alloc_size = 0;
146 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
147 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
148 io.ntcreatex.in.security_flags = 0;
149 io.ntcreatex.in.fname = fname;
150 status = smb_raw_open(tree, mem_ctx, &io);
151 CHECK_STATUS(status, NT_STATUS_OK);
152 fnum = io.ntcreatex.out.fnum;
154 printf("write using the old vuid\n");
155 wr.generic.level = RAW_WRITE_WRITEX;
156 wr.writex.in.fnum = fnum;
157 wr.writex.in.offset = 0;
158 wr.writex.in.wmode = 0;
159 wr.writex.in.remaining = 0;
160 wr.writex.in.count = 1;
161 wr.writex.in.data = &c;
163 status = smb_raw_write(cli->tree, &wr);
164 CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
166 printf("write with the new vuid\n");
167 status = smb_raw_write(tree, &wr);
168 CHECK_STATUS(status, NT_STATUS_OK);
169 CHECK_VALUE(wr.writex.out.nwritten, 1);
171 printf("logoff the new vuid\n");
172 status = smb_raw_ulogoff(session);
173 CHECK_STATUS(status, NT_STATUS_OK);
174 talloc_free(session);
176 printf("the new vuid should not now be accessible\n");
177 status = smb_raw_write(tree, &wr);
178 CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
180 printf("the fnum should have been auto-closed\n");
181 cl.close.level = RAW_CLOSE_CLOSE;
182 cl.close.in.fnum = fnum;
183 cl.close.in.write_time = 0;
184 status = smb_raw_close(cli->tree, &cl);
185 CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
197 static BOOL test_tree(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
202 struct smbcli_tree *tree;
208 const char *fname = BASEDIR "\\test.txt";
211 printf("TESTING TREE HANDLING\n");
213 if (!torture_setup_dir(cli, BASEDIR)) {
217 share = lp_parm_string(-1, "torture", "share");
219 printf("create a second tree context on the same session\n");
220 tree = smbcli_tree_init(cli->session, mem_ctx, False);
222 tcon.generic.level = RAW_TCON_TCONX;
223 tcon.tconx.in.flags = 0;
224 tcon.tconx.in.password = data_blob(NULL, 0);
225 tcon.tconx.in.path = share;
226 tcon.tconx.in.device = "A:";
227 status = smb_tree_connect(tree, mem_ctx, &tcon);
228 CHECK_STATUS(status, NT_STATUS_OK);
230 tree->tid = tcon.tconx.out.tid;
231 printf("tid1=%d tid2=%d\n", cli->tree->tid, tree->tid);
233 printf("try a tconx with a bad device type\n");
234 tcon.tconx.in.device = "FOO";
235 status = smb_tree_connect(tree, mem_ctx, &tcon);
236 CHECK_STATUS(status, NT_STATUS_BAD_DEVICE_TYPE);
239 printf("create a file using the new tid\n");
240 io.generic.level = RAW_OPEN_NTCREATEX;
241 io.ntcreatex.in.root_fid = 0;
242 io.ntcreatex.in.flags = 0;
243 io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
244 io.ntcreatex.in.create_options = 0;
245 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
246 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
247 io.ntcreatex.in.alloc_size = 0;
248 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
249 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
250 io.ntcreatex.in.security_flags = 0;
251 io.ntcreatex.in.fname = fname;
252 status = smb_raw_open(tree, mem_ctx, &io);
253 CHECK_STATUS(status, NT_STATUS_OK);
254 fnum = io.ntcreatex.out.fnum;
256 printf("write using the old tid\n");
257 wr.generic.level = RAW_WRITE_WRITEX;
258 wr.writex.in.fnum = fnum;
259 wr.writex.in.offset = 0;
260 wr.writex.in.wmode = 0;
261 wr.writex.in.remaining = 0;
262 wr.writex.in.count = 1;
263 wr.writex.in.data = &c;
265 status = smb_raw_write(cli->tree, &wr);
266 CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
268 printf("write with the new tid\n");
269 status = smb_raw_write(tree, &wr);
270 CHECK_STATUS(status, NT_STATUS_OK);
271 CHECK_VALUE(wr.writex.out.nwritten, 1);
273 printf("disconnect the new tid\n");
274 status = smb_tree_disconnect(tree);
275 CHECK_STATUS(status, NT_STATUS_OK);
277 printf("the new tid should not now be accessible\n");
278 status = smb_raw_write(tree, &wr);
279 CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
281 printf("the fnum should have been auto-closed\n");
282 cl.close.level = RAW_CLOSE_CLOSE;
283 cl.close.in.fnum = fnum;
284 cl.close.in.write_time = 0;
285 status = smb_raw_close(cli->tree, &cl);
286 CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
288 /* close down the new tree */
299 static BOOL test_pid(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
307 const char *fname = BASEDIR "\\test.txt";
311 printf("TESTING PID HANDLING\n");
313 if (!torture_setup_dir(cli, BASEDIR)) {
317 printf("create a second pid\n");
318 pid1 = cli->session->pid;
321 printf("pid1=%d pid2=%d\n", pid1, pid2);
323 printf("create a file using the new pid\n");
324 cli->session->pid = pid2;
325 io.generic.level = RAW_OPEN_NTCREATEX;
326 io.ntcreatex.in.root_fid = 0;
327 io.ntcreatex.in.flags = 0;
328 io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
329 io.ntcreatex.in.create_options = 0;
330 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
331 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
332 io.ntcreatex.in.alloc_size = 0;
333 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
334 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
335 io.ntcreatex.in.security_flags = 0;
336 io.ntcreatex.in.fname = fname;
337 status = smb_raw_open(cli->tree, mem_ctx, &io);
338 CHECK_STATUS(status, NT_STATUS_OK);
339 fnum = io.ntcreatex.out.fnum;
341 printf("write using the old pid\n");
342 cli->session->pid = pid1;
343 wr.generic.level = RAW_WRITE_WRITEX;
344 wr.writex.in.fnum = fnum;
345 wr.writex.in.offset = 0;
346 wr.writex.in.wmode = 0;
347 wr.writex.in.remaining = 0;
348 wr.writex.in.count = 1;
349 wr.writex.in.data = &c;
351 status = smb_raw_write(cli->tree, &wr);
352 CHECK_STATUS(status, NT_STATUS_OK);
353 CHECK_VALUE(wr.writex.out.nwritten, 1);
355 printf("write with the new pid\n");
356 cli->session->pid = pid2;
357 status = smb_raw_write(cli->tree, &wr);
358 CHECK_STATUS(status, NT_STATUS_OK);
359 CHECK_VALUE(wr.writex.out.nwritten, 1);
361 printf("exit the old pid\n");
362 cli->session->pid = pid1;
363 status = smb_raw_exit(cli->session);
364 CHECK_STATUS(status, NT_STATUS_OK);
366 printf("the fnum should still be accessible\n");
367 cli->session->pid = pid1;
368 status = smb_raw_write(cli->tree, &wr);
369 CHECK_STATUS(status, NT_STATUS_OK);
370 CHECK_VALUE(wr.writex.out.nwritten, 1);
372 printf("exit the new pid\n");
373 cli->session->pid = pid2;
374 status = smb_raw_exit(cli->session);
375 CHECK_STATUS(status, NT_STATUS_OK);
377 printf("the fnum should not now be accessible\n");
378 cli->session->pid = pid1;
379 status = smb_raw_write(cli->tree, &wr);
380 CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
382 printf("the fnum should have been auto-closed\n");
383 cl.close.level = RAW_CLOSE_CLOSE;
384 cl.close.in.fnum = fnum;
385 cl.close.in.write_time = 0;
386 status = smb_raw_close(cli->tree, &cl);
387 CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
395 basic testing of session/tree context calls
397 BOOL torture_raw_context(void)
399 struct smbcli_state *cli;
403 lp_set_cmdline("use spnego", "False");
405 if (!torture_open_connection(&cli)) {
409 mem_ctx = talloc_init("torture_raw_context");
411 if (!test_session(cli, mem_ctx)) {
415 if (!test_tree(cli, mem_ctx)) {
419 if (!test_pid(cli, mem_ctx)) {
423 smb_raw_exit(cli->session);
424 smbcli_deltree(cli->tree, BASEDIR);
426 torture_close_connection(cli);
427 talloc_free(mem_ctx);