Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into v4-0-wsgi
[samba.git] / source4 / torture / util_smb.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB torture tester utility functions
4    Copyright (C) Andrew Tridgell 2003
5    Copyright (C) Jelmer Vernooij 2006
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "lib/cmdline/popt_common.h"
23 #include "libcli/raw/libcliraw.h"
24 #include "libcli/raw/raw_proto.h"
25 #include "libcli/raw/ioctl.h"
26 #include "libcli/libcli.h"
27 #include "system/filesys.h"
28 #include "system/shmem.h"
29 #include "system/wait.h"
30 #include "system/time.h"
31 #include "torture/torture.h"
32 #include "util/dlinklist.h"
33 #include "auth/credentials/credentials.h"
34 #include "libcli/resolve/resolve.h"
35 #include "param/param.h"
36
37
38 /**
39   setup a directory ready for a test
40 */
41 _PUBLIC_ bool torture_setup_dir(struct smbcli_state *cli, const char *dname)
42 {
43         smb_raw_exit(cli->session);
44         if (smbcli_deltree(cli->tree, dname) == -1 ||
45             NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, dname))) {
46                 printf("Unable to setup %s - %s\n", dname, smbcli_errstr(cli->tree));
47                 return false;
48         }
49         return true;
50 }
51
52 /*
53   create a directory, returning a handle to it
54 */
55 NTSTATUS create_directory_handle(struct smbcli_tree *tree, const char *dname, int *fnum)
56 {
57         NTSTATUS status;
58         union smb_open io;
59         TALLOC_CTX *mem_ctx;
60
61         mem_ctx = talloc_named_const(tree, 0, "create_directory_handle");
62
63         io.generic.level = RAW_OPEN_NTCREATEX;
64         io.ntcreatex.in.root_fid = 0;
65         io.ntcreatex.in.flags = 0;
66         io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
67         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
68         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
69         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
70         io.ntcreatex.in.alloc_size = 0;
71         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
72         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
73         io.ntcreatex.in.security_flags = 0;
74         io.ntcreatex.in.fname = dname;
75
76         status = smb_raw_open(tree, mem_ctx, &io);
77         talloc_free(mem_ctx);
78
79         if (NT_STATUS_IS_OK(status)) {
80                 *fnum = io.ntcreatex.out.file.fnum;
81         }
82
83         return status;
84 }
85
86
87 /**
88   sometimes we need a fairly complex file to work with, so we can test
89   all possible attributes. 
90 */
91 _PUBLIC_ int create_complex_file(struct smbcli_state *cli, TALLOC_CTX *mem_ctx, const char *fname)
92 {
93         int fnum;
94         char buf[7] = "abc";
95         union smb_setfileinfo setfile;
96         union smb_fileinfo fileinfo;
97         time_t t = (time(NULL) & ~1);
98         NTSTATUS status;
99
100         smbcli_unlink(cli->tree, fname);
101         fnum = smbcli_nt_create_full(cli->tree, fname, 0, 
102                                      SEC_RIGHTS_FILE_ALL,
103                                      FILE_ATTRIBUTE_NORMAL,
104                                      NTCREATEX_SHARE_ACCESS_DELETE|
105                                      NTCREATEX_SHARE_ACCESS_READ|
106                                      NTCREATEX_SHARE_ACCESS_WRITE, 
107                                      NTCREATEX_DISP_OVERWRITE_IF,
108                                      0, 0);
109         if (fnum == -1) return -1;
110
111         smbcli_write(cli->tree, fnum, 0, buf, 0, sizeof(buf));
112
113         if (strchr(fname, ':') == NULL) {
114                 /* setup some EAs */
115                 setfile.generic.level = RAW_SFILEINFO_EA_SET;
116                 setfile.generic.in.file.fnum = fnum;
117                 setfile.ea_set.in.num_eas = 2;  
118                 setfile.ea_set.in.eas = talloc_array(mem_ctx, struct ea_struct, 2);
119                 setfile.ea_set.in.eas[0].flags = 0;
120                 setfile.ea_set.in.eas[0].name.s = "EAONE";
121                 setfile.ea_set.in.eas[0].value = data_blob_talloc(mem_ctx, "VALUE1", 6);
122                 setfile.ea_set.in.eas[1].flags = 0;
123                 setfile.ea_set.in.eas[1].name.s = "SECONDEA";
124                 setfile.ea_set.in.eas[1].value = data_blob_talloc(mem_ctx, "ValueTwo", 8);
125                 status = smb_raw_setfileinfo(cli->tree, &setfile);
126                 if (!NT_STATUS_IS_OK(status)) {
127                         printf("Failed to setup EAs\n");
128                 }
129         }
130
131         /* make sure all the timestamps aren't the same, and are also 
132            in different DST zones*/
133         setfile.generic.level = RAW_SFILEINFO_SETATTRE;
134         setfile.generic.in.file.fnum = fnum;
135
136         setfile.setattre.in.create_time = t + 9*30*24*60*60;
137         setfile.setattre.in.access_time = t + 6*30*24*60*60;
138         setfile.setattre.in.write_time  = t + 3*30*24*60*60;
139
140         status = smb_raw_setfileinfo(cli->tree, &setfile);
141         if (!NT_STATUS_IS_OK(status)) {
142                 printf("Failed to setup file times - %s\n", nt_errstr(status));
143         }
144
145         /* make sure all the timestamps aren't the same */
146         fileinfo.generic.level = RAW_FILEINFO_GETATTRE;
147         fileinfo.generic.in.file.fnum = fnum;
148
149         status = smb_raw_fileinfo(cli->tree, mem_ctx, &fileinfo);
150         if (!NT_STATUS_IS_OK(status)) {
151                 printf("Failed to query file times - %s\n", nt_errstr(status));
152         }
153
154         if (setfile.setattre.in.create_time != fileinfo.getattre.out.create_time) {
155                 printf("create_time not setup correctly\n");
156         }
157         if (setfile.setattre.in.access_time != fileinfo.getattre.out.access_time) {
158                 printf("access_time not setup correctly\n");
159         }
160         if (setfile.setattre.in.write_time != fileinfo.getattre.out.write_time) {
161                 printf("write_time not setup correctly\n");
162         }
163
164         return fnum;
165 }
166
167
168 /*
169   sometimes we need a fairly complex directory to work with, so we can test
170   all possible attributes. 
171 */
172 int create_complex_dir(struct smbcli_state *cli, TALLOC_CTX *mem_ctx, const char *dname)
173 {
174         int fnum;
175         union smb_setfileinfo setfile;
176         union smb_fileinfo fileinfo;
177         time_t t = (time(NULL) & ~1);
178         NTSTATUS status;
179
180         smbcli_deltree(cli->tree, dname);
181         fnum = smbcli_nt_create_full(cli->tree, dname, 0, 
182                                      SEC_RIGHTS_DIR_ALL,
183                                      FILE_ATTRIBUTE_DIRECTORY,
184                                      NTCREATEX_SHARE_ACCESS_READ|
185                                      NTCREATEX_SHARE_ACCESS_WRITE, 
186                                      NTCREATEX_DISP_OPEN_IF,
187                                      NTCREATEX_OPTIONS_DIRECTORY, 0);
188         if (fnum == -1) return -1;
189
190         if (strchr(dname, ':') == NULL) {
191                 /* setup some EAs */
192                 setfile.generic.level = RAW_SFILEINFO_EA_SET;
193                 setfile.generic.in.file.fnum = fnum;
194                 setfile.ea_set.in.num_eas = 2;  
195                 setfile.ea_set.in.eas = talloc_array(mem_ctx, struct ea_struct, 2);
196                 setfile.ea_set.in.eas[0].flags = 0;
197                 setfile.ea_set.in.eas[0].name.s = "EAONE";
198                 setfile.ea_set.in.eas[0].value = data_blob_talloc(mem_ctx, "VALUE1", 6);
199                 setfile.ea_set.in.eas[1].flags = 0;
200                 setfile.ea_set.in.eas[1].name.s = "SECONDEA";
201                 setfile.ea_set.in.eas[1].value = data_blob_talloc(mem_ctx, "ValueTwo", 8);
202                 status = smb_raw_setfileinfo(cli->tree, &setfile);
203                 if (!NT_STATUS_IS_OK(status)) {
204                         printf("Failed to setup EAs\n");
205                 }
206         }
207
208         /* make sure all the timestamps aren't the same, and are also 
209            in different DST zones*/
210         setfile.generic.level = RAW_SFILEINFO_SETATTRE;
211         setfile.generic.in.file.fnum = fnum;
212
213         setfile.setattre.in.create_time = t + 9*30*24*60*60;
214         setfile.setattre.in.access_time = t + 6*30*24*60*60;
215         setfile.setattre.in.write_time  = t + 3*30*24*60*60;
216
217         status = smb_raw_setfileinfo(cli->tree, &setfile);
218         if (!NT_STATUS_IS_OK(status)) {
219                 printf("Failed to setup file times - %s\n", nt_errstr(status));
220         }
221
222         /* make sure all the timestamps aren't the same */
223         fileinfo.generic.level = RAW_FILEINFO_GETATTRE;
224         fileinfo.generic.in.file.fnum = fnum;
225
226         status = smb_raw_fileinfo(cli->tree, mem_ctx, &fileinfo);
227         if (!NT_STATUS_IS_OK(status)) {
228                 printf("Failed to query file times - %s\n", nt_errstr(status));
229         }
230
231         if (setfile.setattre.in.create_time != fileinfo.getattre.out.create_time) {
232                 printf("create_time not setup correctly\n");
233         }
234         if (setfile.setattre.in.access_time != fileinfo.getattre.out.access_time) {
235                 printf("access_time not setup correctly\n");
236         }
237         if (setfile.setattre.in.write_time != fileinfo.getattre.out.write_time) {
238                 printf("write_time not setup correctly\n");
239         }
240
241         return fnum;
242 }
243
244
245
246 /* return a pointer to a anonymous shared memory segment of size "size"
247    which will persist across fork() but will disappear when all processes
248    exit 
249
250    The memory is not zeroed 
251
252    This function uses system5 shared memory. It takes advantage of a property
253    that the memory is not destroyed if it is attached when the id is removed
254    */
255 void *shm_setup(int size)
256 {
257         int shmid;
258         void *ret;
259
260         shmid = shmget(IPC_PRIVATE, size, SHM_R | SHM_W);
261         if (shmid == -1) {
262                 printf("can't get shared memory\n");
263                 exit(1);
264         }
265         ret = (void *)shmat(shmid, 0, 0);
266         if (!ret || ret == (void *)-1) {
267                 printf("can't attach to shared memory\n");
268                 return NULL;
269         }
270         /* the following releases the ipc, but note that this process
271            and all its children will still have access to the memory, its
272            just that the shmid is no longer valid for other shm calls. This
273            means we don't leave behind lots of shm segments after we exit 
274
275            See Stevens "advanced programming in unix env" for details
276            */
277         shmctl(shmid, IPC_RMID, 0);
278         
279         return ret;
280 }
281
282
283 /**
284   check that a wire string matches the flags specified 
285   not 100% accurate, but close enough for testing
286 */
287 bool wire_bad_flags(struct smb_wire_string *str, int flags, 
288                     struct smbcli_transport *transport)
289 {
290         bool server_unicode;
291         int len;
292         if (!str || !str->s) return true;
293         len = strlen(str->s);
294         if (flags & STR_TERMINATE) len++;
295
296         server_unicode = (transport->negotiate.capabilities&CAP_UNICODE)?true:false;
297         if (getenv("CLI_FORCE_ASCII") || !transport->options.unicode) {
298                 server_unicode = false;
299         }
300
301         if ((flags & STR_UNICODE) || server_unicode) {
302                 len *= 2;
303         } else if (flags & STR_TERMINATE_ASCII) {
304                 len++;
305         }
306         if (str->private_length != len) {
307                 printf("Expected wire_length %d but got %d for '%s'\n", 
308                        len, str->private_length, str->s);
309                 return true;
310         }
311         return false;
312 }
313
314 /*
315   dump a all_info QFILEINFO structure
316 */
317 void dump_all_info(TALLOC_CTX *mem_ctx, union smb_fileinfo *finfo)
318 {
319         d_printf("\tcreate_time:    %s\n", nt_time_string(mem_ctx, finfo->all_info.out.create_time));
320         d_printf("\taccess_time:    %s\n", nt_time_string(mem_ctx, finfo->all_info.out.access_time));
321         d_printf("\twrite_time:     %s\n", nt_time_string(mem_ctx, finfo->all_info.out.write_time));
322         d_printf("\tchange_time:    %s\n", nt_time_string(mem_ctx, finfo->all_info.out.change_time));
323         d_printf("\tattrib:         0x%x\n", finfo->all_info.out.attrib);
324         d_printf("\talloc_size:     %llu\n", (long long)finfo->all_info.out.alloc_size);
325         d_printf("\tsize:           %llu\n", (long long)finfo->all_info.out.size);
326         d_printf("\tnlink:          %u\n", finfo->all_info.out.nlink);
327         d_printf("\tdelete_pending: %u\n", finfo->all_info.out.delete_pending);
328         d_printf("\tdirectory:      %u\n", finfo->all_info.out.directory);
329         d_printf("\tea_size:        %u\n", finfo->all_info.out.ea_size);
330         d_printf("\tfname:          '%s'\n", finfo->all_info.out.fname.s);
331 }
332
333 /*
334   dump file infor by name
335 */
336 void torture_all_info(struct smbcli_tree *tree, const char *fname)
337 {
338         TALLOC_CTX *mem_ctx = talloc_named(tree, 0, "%s", fname);
339         union smb_fileinfo finfo;
340         NTSTATUS status;
341
342         finfo.generic.level = RAW_FILEINFO_ALL_INFO;
343         finfo.generic.in.file.path = fname;
344         status = smb_raw_pathinfo(tree, mem_ctx, &finfo);
345         if (!NT_STATUS_IS_OK(status)) {
346                 d_printf("%s - %s\n", fname, nt_errstr(status));
347                 return;
348         }
349
350         d_printf("%s:\n", fname);
351         dump_all_info(mem_ctx, &finfo);
352         talloc_free(mem_ctx);
353 }
354
355
356 /*
357   set a attribute on a file
358 */
359 bool torture_set_file_attribute(struct smbcli_tree *tree, const char *fname, uint16_t attrib)
360 {
361         union smb_setfileinfo sfinfo;
362         NTSTATUS status;
363
364         ZERO_STRUCT(sfinfo.basic_info.in);
365         sfinfo.basic_info.level = RAW_SFILEINFO_BASIC_INFORMATION;
366         sfinfo.basic_info.in.file.path = fname;
367         sfinfo.basic_info.in.attrib = attrib;
368         status = smb_raw_setpathinfo(tree, &sfinfo);
369         return NT_STATUS_IS_OK(status);
370 }
371
372
373 /*
374   set a file descriptor as sparse
375 */
376 NTSTATUS torture_set_sparse(struct smbcli_tree *tree, int fnum)
377 {
378         union smb_ioctl nt;
379         NTSTATUS status;
380         TALLOC_CTX *mem_ctx;
381
382         mem_ctx = talloc_named_const(tree, 0, "torture_set_sparse");
383         if (!mem_ctx) {
384                 return NT_STATUS_NO_MEMORY;
385         }
386
387         nt.ntioctl.level = RAW_IOCTL_NTIOCTL;
388         nt.ntioctl.in.function = FSCTL_SET_SPARSE;
389         nt.ntioctl.in.file.fnum = fnum;
390         nt.ntioctl.in.fsctl = true;
391         nt.ntioctl.in.filter = 0;
392         nt.ntioctl.in.max_data = 0;
393         nt.ntioctl.in.blob = data_blob(NULL, 0);
394
395         status = smb_raw_ioctl(tree, mem_ctx, &nt);
396
397         talloc_free(mem_ctx);
398
399         return status;
400 }
401
402 /*
403   check that an EA has the right value 
404 */
405 NTSTATUS torture_check_ea(struct smbcli_state *cli, 
406                           const char *fname, const char *eaname, const char *value)
407 {
408         union smb_fileinfo info;
409         NTSTATUS status;
410         struct ea_name ea;
411         TALLOC_CTX *mem_ctx = talloc_new(cli);
412
413         info.ea_list.level = RAW_FILEINFO_EA_LIST;
414         info.ea_list.in.file.path = fname;
415         info.ea_list.in.num_names = 1;
416         info.ea_list.in.ea_names = &ea;
417
418         ea.name.s = eaname;
419
420         status = smb_raw_pathinfo(cli->tree, mem_ctx, &info);
421         if (!NT_STATUS_IS_OK(status)) {
422                 talloc_free(mem_ctx);
423                 return status;
424         }
425
426         if (info.ea_list.out.num_eas != 1) {
427                 printf("Expected 1 ea in ea_list\n");
428                 talloc_free(mem_ctx);
429                 return NT_STATUS_EA_CORRUPT_ERROR;
430         }
431
432         if (strcasecmp_m(eaname, info.ea_list.out.eas[0].name.s) != 0) {
433                 printf("Expected ea '%s' not '%s' in ea_list\n",
434                        eaname, info.ea_list.out.eas[0].name.s);
435                 talloc_free(mem_ctx);
436                 return NT_STATUS_EA_CORRUPT_ERROR;
437         }
438
439         if (value == NULL) {
440                 if (info.ea_list.out.eas[0].value.length != 0) {
441                         printf("Expected zero length ea for %s\n", eaname);
442                         talloc_free(mem_ctx);
443                         return NT_STATUS_EA_CORRUPT_ERROR;
444                 }
445                 talloc_free(mem_ctx);
446                 return NT_STATUS_OK;
447         }
448
449         if (strlen(value) == info.ea_list.out.eas[0].value.length &&
450             memcmp(value, info.ea_list.out.eas[0].value.data,
451                    info.ea_list.out.eas[0].value.length) == 0) {
452                 talloc_free(mem_ctx);
453                 return NT_STATUS_OK;
454         }
455
456         printf("Expected value '%s' not '%*.*s' for ea %s\n",
457                value, 
458                (int)info.ea_list.out.eas[0].value.length,
459                (int)info.ea_list.out.eas[0].value.length,
460                info.ea_list.out.eas[0].value.data,
461                eaname);
462
463         talloc_free(mem_ctx);
464
465         return NT_STATUS_EA_CORRUPT_ERROR;
466 }
467
468 _PUBLIC_ bool torture_open_connection_share(TALLOC_CTX *mem_ctx,
469                                    struct smbcli_state **c, 
470                                    struct torture_context *tctx,
471                                    const char *hostname, 
472                                    const char *sharename,
473                                    struct event_context *ev)
474 {
475         NTSTATUS status;
476
477         struct smbcli_options options;
478
479         lp_smbcli_options(tctx->lp_ctx, &options);
480
481         options.use_oplocks = torture_setting_bool(tctx, "use_oplocks", true);
482         options.use_level2_oplocks = torture_setting_bool(tctx, "use_level2_oplocks", true);
483
484         status = smbcli_full_connection(mem_ctx, c, hostname, 
485                                         lp_smb_ports(tctx->lp_ctx),
486                                         sharename, NULL,
487                                         cmdline_credentials, 
488                                         lp_resolve_context(tctx->lp_ctx),
489                                         ev, &options);
490         if (!NT_STATUS_IS_OK(status)) {
491                 printf("Failed to open connection - %s\n", nt_errstr(status));
492                 return false;
493         }
494
495         return true;
496 }
497
498 _PUBLIC_ bool torture_get_conn_index(int conn_index,
499                                      TALLOC_CTX *mem_ctx,
500                                      struct torture_context *tctx,
501                                      char **host, char **share)
502 {
503         char **unc_list = NULL;
504         int num_unc_names = 0;
505         const char *p;
506
507         (*host) = talloc_strdup(mem_ctx, torture_setting_string(tctx, "host", NULL));
508         (*share) = talloc_strdup(mem_ctx, torture_setting_string(tctx, "share", NULL));
509         
510         p = torture_setting_string(tctx, "unclist", NULL);
511         if (!p) {
512                 return true;
513         }
514
515         unc_list = file_lines_load(p, &num_unc_names, NULL);
516         if (!unc_list || num_unc_names <= 0) {
517                 DEBUG(0,("Failed to load unc names list from '%s'\n", p));
518                 return false;
519         }
520
521         if (!smbcli_parse_unc(unc_list[conn_index % num_unc_names],
522                               mem_ctx, host, share)) {
523                 DEBUG(0, ("Failed to parse UNC name %s\n",
524                           unc_list[conn_index % num_unc_names]));
525                 return false;
526         }
527
528         talloc_free(unc_list);
529         return true;
530 }
531
532
533
534 _PUBLIC_ bool torture_open_connection_ev(struct smbcli_state **c,
535                                          int conn_index,
536                                          struct torture_context *tctx,
537                                          struct event_context *ev)
538 {
539         char *host, *share;
540         bool ret;
541
542         if (!torture_get_conn_index(conn_index, ev, tctx, &host, &share)) {
543                 return false;
544         }
545
546         ret = torture_open_connection_share(NULL, c, tctx, host, share, ev);
547         talloc_free(host);
548         talloc_free(share);
549
550         return ret;
551 }
552
553 _PUBLIC_ bool torture_open_connection(struct smbcli_state **c, struct torture_context *tctx, int conn_index)
554 {
555         return torture_open_connection_ev(c, conn_index, tctx, tctx->ev);
556 }
557
558
559
560 _PUBLIC_ bool torture_close_connection(struct smbcli_state *c)
561 {
562         bool ret = true;
563         if (!c) return true;
564         if (NT_STATUS_IS_ERR(smbcli_tdis(c))) {
565                 printf("tdis failed (%s)\n", smbcli_errstr(c->tree));
566                 ret = false;
567         }
568         talloc_free(c);
569         return ret;
570 }
571
572
573 /* check if the server produced the expected error code */
574 _PUBLIC_ bool check_error(const char *location, struct smbcli_state *c, 
575                  uint8_t eclass, uint32_t ecode, NTSTATUS nterr)
576 {
577         NTSTATUS status;
578         
579         status = smbcli_nt_error(c->tree);
580         if (NT_STATUS_IS_DOS(status)) {
581                 int class, num;
582                 class = NT_STATUS_DOS_CLASS(status);
583                 num = NT_STATUS_DOS_CODE(status);
584                 if (eclass != class || ecode != num) {
585                         printf("unexpected error code %s\n", nt_errstr(status));
586                         printf(" expected %s or %s (at %s)\n", 
587                                nt_errstr(NT_STATUS_DOS(eclass, ecode)), 
588                                nt_errstr(nterr), location);
589                         return false;
590                 }
591         } else {
592                 if (!NT_STATUS_EQUAL(nterr, status)) {
593                         printf("unexpected error code %s\n", nt_errstr(status));
594                         printf(" expected %s (at %s)\n", nt_errstr(nterr), location);
595                         return false;
596                 }
597         }
598
599         return true;
600 }
601
602 static struct smbcli_state *current_cli;
603 static int procnum; /* records process count number when forking */
604
605 static void sigcont(int sig)
606 {
607 }
608
609 double torture_create_procs(struct torture_context *tctx, 
610                                                         bool (*fn)(struct torture_context *, struct smbcli_state *, int), bool *result)
611 {
612         int i, status;
613         volatile pid_t *child_status;
614         volatile bool *child_status_out;
615         int synccount;
616         int tries = 8;
617         int torture_nprocs = torture_setting_int(tctx, "nprocs", 4);
618         double start_time_limit = 10 + (torture_nprocs * 1.5);
619         struct timeval tv;
620
621         *result = true;
622
623         synccount = 0;
624
625         signal(SIGCONT, sigcont);
626
627         child_status = (volatile pid_t *)shm_setup(sizeof(pid_t)*torture_nprocs);
628         if (!child_status) {
629                 printf("Failed to setup shared memory\n");
630                 return -1;
631         }
632
633         child_status_out = (volatile bool *)shm_setup(sizeof(bool)*torture_nprocs);
634         if (!child_status_out) {
635                 printf("Failed to setup result status shared memory\n");
636                 return -1;
637         }
638
639         for (i = 0; i < torture_nprocs; i++) {
640                 child_status[i] = 0;
641                 child_status_out[i] = true;
642         }
643
644         tv = timeval_current();
645
646         for (i=0;i<torture_nprocs;i++) {
647                 procnum = i;
648                 if (fork() == 0) {
649                         char *myname;
650
651                         pid_t mypid = getpid();
652                         srandom(((int)mypid) ^ ((int)time(NULL)));
653
654                         asprintf(&myname, "CLIENT%d", i);
655                         lp_set_cmdline(tctx->lp_ctx, "netbios name", myname);
656                         free(myname);
657
658
659                         while (1) {
660                                 if (torture_open_connection(&current_cli, tctx, i)) {
661                                         break;
662                                 }
663                                 if (tries-- == 0) {
664                                         printf("pid %d failed to start\n", (int)getpid());
665                                         _exit(1);
666                                 }
667                                 msleep(100);    
668                         }
669
670                         child_status[i] = getpid();
671
672                         pause();
673
674                         if (child_status[i]) {
675                                 printf("Child %d failed to start!\n", i);
676                                 child_status_out[i] = 1;
677                                 _exit(1);
678                         }
679
680                         child_status_out[i] = fn(tctx, current_cli, i);
681                         _exit(0);
682                 }
683         }
684
685         do {
686                 synccount = 0;
687                 for (i=0;i<torture_nprocs;i++) {
688                         if (child_status[i]) synccount++;
689                 }
690                 if (synccount == torture_nprocs) break;
691                 msleep(100);
692         } while (timeval_elapsed(&tv) < start_time_limit);
693
694         if (synccount != torture_nprocs) {
695                 printf("FAILED TO START %d CLIENTS (started %d)\n", torture_nprocs, synccount);
696                 *result = false;
697                 return timeval_elapsed(&tv);
698         }
699
700         printf("Starting %d clients\n", torture_nprocs);
701
702         /* start the client load */
703         tv = timeval_current();
704         for (i=0;i<torture_nprocs;i++) {
705                 child_status[i] = 0;
706         }
707
708         printf("%d clients started\n", torture_nprocs);
709
710         kill(0, SIGCONT);
711
712         for (i=0;i<torture_nprocs;i++) {
713                 int ret;
714                 while ((ret=waitpid(0, &status, 0)) == -1 && errno == EINTR) /* noop */ ;
715                 if (ret == -1 || WEXITSTATUS(status) != 0) {
716                         *result = false;
717                 }
718         }
719
720         printf("\n");
721         
722         for (i=0;i<torture_nprocs;i++) {
723                 if (!child_status_out[i]) {
724                         *result = false;
725                 }
726         }
727         return timeval_elapsed(&tv);
728 }
729
730 static bool wrap_smb_multi_test(struct torture_context *torture,
731                                                                 struct torture_tcase *tcase,
732                                                                 struct torture_test *test)
733 {
734         bool (*fn)(struct torture_context *, struct smbcli_state *, int ) = test->fn;
735         bool result;
736
737         torture_create_procs(torture, fn, &result);
738
739         return result;
740 }
741
742 _PUBLIC_ struct torture_test *torture_suite_add_smb_multi_test(
743                                                                         struct torture_suite *suite,
744                                                                         const char *name,
745                                                                         bool (*run) (struct torture_context *,
746                                                                                                  struct smbcli_state *,
747                                                                                                 int i))
748 {
749         struct torture_test *test; 
750         struct torture_tcase *tcase;
751         
752         tcase = torture_suite_add_tcase(suite, name);
753
754         test = talloc(tcase, struct torture_test);
755
756         test->name = talloc_strdup(test, name);
757         test->description = NULL;
758         test->run = wrap_smb_multi_test;
759         test->fn = run;
760         test->dangerous = false;
761
762         DLIST_ADD_END(tcase->tests, test, struct torture_test *);
763
764         return test;
765
766 }
767
768 static bool wrap_simple_2smb_test(struct torture_context *torture_ctx,
769                                                                         struct torture_tcase *tcase,
770                                                                         struct torture_test *test)
771 {
772         bool (*fn) (struct torture_context *, struct smbcli_state *,
773                                 struct smbcli_state *);
774         bool ret;
775
776         struct smbcli_state *cli1, *cli2;
777
778         if (!torture_open_connection(&cli1, torture_ctx, 0) || 
779                 !torture_open_connection(&cli2, torture_ctx, 1))
780                 return false;
781
782         fn = test->fn;
783
784         ret = fn(torture_ctx, cli1, cli2);
785
786         talloc_free(cli1);
787         talloc_free(cli2);
788
789         return ret;
790 }
791
792
793
794 _PUBLIC_ struct torture_test *torture_suite_add_2smb_test(
795                                                                         struct torture_suite *suite,
796                                                                         const char *name,
797                                                                         bool (*run) (struct torture_context *,
798                                                                                                 struct smbcli_state *,
799                                                                                                 struct smbcli_state *))
800 {
801         struct torture_test *test; 
802         struct torture_tcase *tcase;
803         
804         tcase = torture_suite_add_tcase(suite, name);
805
806         test = talloc(tcase, struct torture_test);
807
808         test->name = talloc_strdup(test, name);
809         test->description = NULL;
810         test->run = wrap_simple_2smb_test;
811         test->fn = run;
812         test->dangerous = false;
813
814         DLIST_ADD_END(tcase->tests, test, struct torture_test *);
815
816         return test;
817
818 }
819
820 static bool wrap_simple_1smb_test(struct torture_context *torture_ctx,
821                                                                         struct torture_tcase *tcase,
822                                                                         struct torture_test *test)
823 {
824         bool (*fn) (struct torture_context *, struct smbcli_state *);
825         bool ret;
826
827         struct smbcli_state *cli1;
828
829         if (!torture_open_connection(&cli1, torture_ctx, 0))
830                 return false;
831
832         fn = test->fn;
833
834         ret = fn(torture_ctx, cli1);
835
836         talloc_free(cli1);
837
838         return ret;
839 }
840
841 _PUBLIC_ struct torture_test *torture_suite_add_1smb_test(
842                                 struct torture_suite *suite,
843                                 const char *name,
844                                 bool (*run) (struct torture_context *, struct smbcli_state *))
845 {
846         struct torture_test *test; 
847         struct torture_tcase *tcase;
848         
849         tcase = torture_suite_add_tcase(suite, name);
850
851         test = talloc(tcase, struct torture_test);
852
853         test->name = talloc_strdup(test, name);
854         test->description = NULL;
855         test->run = wrap_simple_1smb_test;
856         test->fn = run;
857         test->dangerous = false;
858
859         DLIST_ADD_END(tcase->tests, test, struct torture_test *);
860
861         return test;
862 }
863
864
865 NTSTATUS torture_second_tcon(TALLOC_CTX *mem_ctx,
866                              struct smbcli_session *session,
867                              const char *sharename,
868                              struct smbcli_tree **res)
869 {
870         union smb_tcon tcon;
871         struct smbcli_tree *result;
872         TALLOC_CTX *tmp_ctx;
873         NTSTATUS status;
874
875         if ((tmp_ctx = talloc_new(mem_ctx)) == NULL) {
876                 return NT_STATUS_NO_MEMORY;
877         }
878
879         result = smbcli_tree_init(session, tmp_ctx, false);
880         if (result == NULL) {
881                 talloc_free(tmp_ctx);
882                 return NT_STATUS_NO_MEMORY;
883         }
884
885         tcon.generic.level = RAW_TCON_TCONX;
886         tcon.tconx.in.flags = 0;
887
888         /* Ignore share mode security here */
889         tcon.tconx.in.password = data_blob(NULL, 0);
890         tcon.tconx.in.path = sharename;
891         tcon.tconx.in.device = "?????";
892
893         status = smb_raw_tcon(result, tmp_ctx, &tcon);
894         if (!NT_STATUS_IS_OK(status)) {
895                 talloc_free(tmp_ctx);
896                 return status;
897         }
898
899         result->tid = tcon.tconx.out.tid;
900         *res = talloc_steal(mem_ctx, result);
901         talloc_free(tmp_ctx);
902         return NT_STATUS_OK;
903 }