r8520: fixed a pile of warnings from the build farm gcc -Wall output on
[jelmer/samba4-debian.git] / source / 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 #include "lib/cmdline/popt_common.h"
26 #include "lib/events/events.h"
27
28 #define BASEDIR "\\rawcontext"
29
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)); \
34                 ret = False; \
35                 goto done; \
36         }} while (0)
37
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); \
42                 ret = False; \
43                 goto done; \
44         }} while (0)
45
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); \
50                 ret = False; \
51                 goto done; \
52         }} while (0)
53
54
55 /*
56   test session ops
57 */
58 static BOOL test_session(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
59 {
60         NTSTATUS status;
61         BOOL ret = True;
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];
72         union smb_open io;
73         union smb_write wr;
74         union smb_close cl;
75         int fnum;
76         const char *fname = BASEDIR "\\test.txt";
77         uint8_t c = 1;
78         int i;
79
80         printf("TESTING SESSION HANDLING\n");
81
82         if (!torture_setup_dir(cli, BASEDIR)) {
83                 return False;
84         }
85
86         printf("create a second security context on the same transport\n");
87         session = smbcli_session_init(cli->transport, mem_ctx, False);
88
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();
92
93         setup.in.credentials = cmdline_credentials;
94
95         status = smb_composite_sesssetup(session, &setup);
96         CHECK_STATUS(status, NT_STATUS_OK);
97         
98         session->vuid = setup.out.vuid;
99
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);
102
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();
107
108         setup.in.credentials = cmdline_credentials;
109
110         status = smb_composite_sesssetup(session2, &setup);
111         CHECK_STATUS(status, NT_STATUS_OK);
112
113         session2->vuid = setup.out.vuid;
114         printf("vuid1=%d vuid2=%d vuid3=%d\n", cli->session->vuid, session->vuid, session2->vuid);
115         
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");
120                 }
121         } else {
122                 CHECK_NOT_VALUE(session2->vuid, session->vuid);
123         }
124         talloc_free(session2);
125
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);
129
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();
134         
135                 setup.in.credentials = cmdline_credentials;
136         
137
138                 status = smb_composite_sesssetup(session3, &setup);
139                 CHECK_STATUS(status, NT_STATUS_LOGON_FAILURE);
140
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);
143
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();
148                 
149                 anon_creds = cli_credentials_init(mem_ctx);
150                 cli_credentials_set_conf(anon_creds);
151                 cli_credentials_set_anonymous(anon_creds);
152
153                 setup.in.credentials = anon_creds;
154         
155                 status = smb_composite_sesssetup(session3, &setup);
156                 CHECK_STATUS(status, NT_STATUS_OK);
157
158                 talloc_free(session4);
159         }
160                 
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;
164
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;
181
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;
190
191         status = smb_raw_write(cli->tree, &wr);
192         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
193
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);
198
199         printf("logoff the new vuid\n");
200         status = smb_raw_ulogoff(session);
201         CHECK_STATUS(status, NT_STATUS_OK);
202
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);
206
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);
211
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);
218
219         printf("create %d secondary security contexts on the same transport\n", 
220                (int)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();
225                 
226                 setups[i].in.credentials = cmdline_credentials;
227
228                 sessions[i] = smbcli_session_init(cli->transport, mem_ctx, False);
229                 composite_contexts[i] = smb_composite_sesssetup_send(sessions[i], &setups[i]);
230
231         }
232
233
234         /* flush the queue */
235         for (i=0; i < ARRAY_SIZE(sessions); i++) {
236                 event_loop_once(composite_contexts[0]->event_ctx);
237         }
238
239         printf("finishing %d secondary security contexts on the same transport\n", 
240                (int)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);
248         }
249
250
251         talloc_free(tree);
252         
253 done:
254         return ret;
255 }
256
257
258 /*
259   test tree ops
260 */
261 static BOOL test_tree(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
262 {
263         NTSTATUS status;
264         BOOL ret = True;
265         const char *share;
266         struct smbcli_tree *tree;
267         union smb_tcon tcon;
268         union smb_open io;
269         union smb_write wr;
270         union smb_close cl;
271         int fnum;
272         const char *fname = BASEDIR "\\test.txt";
273         uint8_t c = 1;
274
275         printf("TESTING TREE HANDLING\n");
276
277         if (!torture_setup_dir(cli, BASEDIR)) {
278                 return False;
279         }
280
281         share = lp_parm_string(-1, "torture", "share");
282         
283         printf("create a second tree context on the same session\n");
284         tree = smbcli_tree_init(cli->session, mem_ctx, False);
285
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);
293
294         tree->tid = tcon.tconx.out.tid;
295         printf("tid1=%d tid2=%d\n", cli->tree->tid, tree->tid);
296
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);
301
302
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;
319
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;
328
329         status = smb_raw_write(cli->tree, &wr);
330         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
331
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);
336
337         printf("disconnect the new tid\n");
338         status = smb_tree_disconnect(tree);
339         CHECK_STATUS(status, NT_STATUS_OK);
340
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);
344
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);
351
352         /* close down the new tree */
353         talloc_free(tree);
354         
355 done:
356         return ret;
357 }
358
359
360 /*
361   test pid ops
362 */
363 static BOOL test_pid(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
364 {
365         NTSTATUS status;
366         BOOL ret = True;
367         union smb_open io;
368         union smb_write wr;
369         union smb_close cl;
370         int fnum;
371         const char *fname = BASEDIR "\\test.txt";
372         uint8_t c = 1;
373         uint16_t pid1, pid2;
374
375         printf("TESTING PID HANDLING\n");
376
377         if (!torture_setup_dir(cli, BASEDIR)) {
378                 return False;
379         }
380
381         printf("create a second pid\n");
382         pid1 = cli->session->pid;
383         pid2 = pid1+1;
384
385         printf("pid1=%d pid2=%d\n", pid1, pid2);
386
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;
404
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;
414
415         status = smb_raw_write(cli->tree, &wr);
416         CHECK_STATUS(status, NT_STATUS_OK);
417         CHECK_VALUE(wr.writex.out.nwritten, 1);
418
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);
424
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);
429
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);
435
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);
440
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);
445
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);
452
453 done:
454         return ret;
455 }
456
457
458 /* 
459    basic testing of session/tree context calls
460 */
461 static BOOL torture_raw_context_int(void)
462 {
463         struct smbcli_state *cli;
464         BOOL ret = True;
465         TALLOC_CTX *mem_ctx;
466
467         if (!torture_open_connection(&cli)) {
468                 return False;
469         }
470
471         mem_ctx = talloc_init("torture_raw_context");
472
473         if (!test_session(cli, mem_ctx)) {
474                 ret = False;
475         }
476
477         if (!test_tree(cli, mem_ctx)) {
478                 ret = False;
479         }
480
481         if (!test_pid(cli, mem_ctx)) {
482                 ret = False;
483         }
484
485         smb_raw_exit(cli->session);
486         smbcli_deltree(cli->tree, BASEDIR);
487
488         torture_close_connection(cli);
489         talloc_free(mem_ctx);
490
491         return ret;
492 }
493 /* 
494    basic testing of session/tree context calls
495 */
496 BOOL torture_raw_context(void)
497 {
498         BOOL ret = True;
499         if (lp_use_spnego()) {
500                 ret &= torture_raw_context_int();
501                 lp_set_cmdline("use spnego", "False");
502         }
503
504         ret &= torture_raw_context_int();
505
506         return ret;
507 }