r5037: got rid of all of the TALLOC_DEPRECATED stuff. My apologies for the
[gd/samba-autobuild/.git] / source4 / torture / raw / context.c
1 /* 
2    Unix SMB/CIFS implementation.
3    test suite for session setup operations
4    Copyright (C) Andrew Tridgell 2003
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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "libcli/raw/libcliraw.h"
23 #include "librpc/gen_ndr/ndr_security.h"
24 #include "libcli/composite/composite.h"
25
26 #define BASEDIR "\\rawcontext"
27
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)); \
32                 ret = False; \
33                 goto done; \
34         }} while (0)
35
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); \
40                 ret = False; \
41                 goto done; \
42         }} while (0)
43
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); \
48                 ret = False; \
49                 goto done; \
50         }} while (0)
51
52
53 /*
54   test session ops
55 */
56 static BOOL test_session(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
57 {
58         NTSTATUS status;
59         BOOL ret = True;
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;
66         union smb_open io;
67         union smb_write wr;
68         union smb_close cl;
69         int fnum;
70         const char *fname = BASEDIR "\\test.txt";
71         uint8_t c = 1;
72
73         printf("TESTING SESSION HANDLING\n");
74
75         if (!torture_setup_dir(cli, BASEDIR)) {
76                 return False;
77         }
78
79         username = lp_parm_string(-1, "torture", "username");
80         password = lp_parm_string(-1, "torture", "password");
81         domain = lp_parm_string(-1, "torture", "userdomain");
82
83         printf("create a second security context on the same transport\n");
84         session = smbcli_session_init(cli->transport, mem_ctx, False);
85
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;
91
92         status = smb_composite_sesssetup(session, &setup);
93         CHECK_STATUS(status, NT_STATUS_OK);
94
95         session->vuid = setup.out.vuid;
96
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);
99
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;
106
107         status = smb_composite_sesssetup(session2, &setup);
108         CHECK_STATUS(status, NT_STATUS_OK);
109
110         session2->vuid = setup.out.vuid;
111         printf("vuid1=%d vuid2=%d vuid3=%d\n", cli->session->vuid, session->vuid, session2->vuid);
112         
113         CHECK_NOT_VALUE(session->vuid, session2->vuid);
114         talloc_free(session2);
115
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);
119
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;
126
127                 status = smb_composite_sesssetup(session3, &setup);
128                 CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
129
130                 talloc_free(session3);
131         }
132                 
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;
136
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;
153
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;
162
163         status = smb_raw_write(cli->tree, &wr);
164         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
165
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);
170
171         printf("logoff the new vuid\n");
172         status = smb_raw_ulogoff(session);
173         CHECK_STATUS(status, NT_STATUS_OK);
174         talloc_free(session);
175
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);
179
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);
186
187         talloc_free(tree);
188         
189 done:
190         return ret;
191 }
192
193
194 /*
195   test tree ops
196 */
197 static BOOL test_tree(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
198 {
199         NTSTATUS status;
200         BOOL ret = True;
201         const char *share;
202         struct smbcli_tree *tree;
203         union smb_tcon tcon;
204         union smb_open io;
205         union smb_write wr;
206         union smb_close cl;
207         int fnum;
208         const char *fname = BASEDIR "\\test.txt";
209         uint8_t c = 1;
210
211         printf("TESTING TREE HANDLING\n");
212
213         if (!torture_setup_dir(cli, BASEDIR)) {
214                 return False;
215         }
216
217         share = lp_parm_string(-1, "torture", "share");
218         
219         printf("create a second tree context on the same session\n");
220         tree = smbcli_tree_init(cli->session, mem_ctx, False);
221
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);
229
230         tree->tid = tcon.tconx.out.tid;
231         printf("tid1=%d tid2=%d\n", cli->tree->tid, tree->tid);
232
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);
237
238
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;
255
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;
264
265         status = smb_raw_write(cli->tree, &wr);
266         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
267
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);
272
273         printf("disconnect the new tid\n");
274         status = smb_tree_disconnect(tree);
275         CHECK_STATUS(status, NT_STATUS_OK);
276
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);
280
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);
287
288         /* close down the new tree */
289         talloc_free(tree);
290         
291 done:
292         return ret;
293 }
294
295
296 /*
297   test pid ops
298 */
299 static BOOL test_pid(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
300 {
301         NTSTATUS status;
302         BOOL ret = True;
303         union smb_open io;
304         union smb_write wr;
305         union smb_close cl;
306         int fnum;
307         const char *fname = BASEDIR "\\test.txt";
308         uint8_t c = 1;
309         uint16_t pid1, pid2;
310
311         printf("TESTING PID HANDLING\n");
312
313         if (!torture_setup_dir(cli, BASEDIR)) {
314                 return False;
315         }
316
317         printf("create a second pid\n");
318         pid1 = cli->session->pid;
319         pid2 = pid1+1;
320
321         printf("pid1=%d pid2=%d\n", pid1, pid2);
322
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;
340
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;
350
351         status = smb_raw_write(cli->tree, &wr);
352         CHECK_STATUS(status, NT_STATUS_OK);
353         CHECK_VALUE(wr.writex.out.nwritten, 1);
354
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);
360
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);
365
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);
371
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);
376
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);
381
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);
388
389 done:
390         return ret;
391 }
392
393
394 /* 
395    basic testing of session/tree context calls
396 */
397 BOOL torture_raw_context(void)
398 {
399         struct smbcli_state *cli;
400         BOOL ret = True;
401         TALLOC_CTX *mem_ctx;
402
403         lp_set_cmdline("use spnego", "False");
404
405         if (!torture_open_connection(&cli)) {
406                 return False;
407         }
408
409         mem_ctx = talloc_init("torture_raw_context");
410
411         if (!test_session(cli, mem_ctx)) {
412                 ret = False;
413         }
414
415         if (!test_tree(cli, mem_ctx)) {
416                 ret = False;
417         }
418
419         if (!test_pid(cli, mem_ctx)) {
420                 ret = False;
421         }
422
423         smb_raw_exit(cli->session);
424         smbcli_deltree(cli->tree, BASEDIR);
425
426         torture_close_connection(cli);
427         talloc_free(mem_ctx);
428
429         return ret;
430 }