/* Unix SMB/CIFS implementation. generic testing tool - version with SMB2 support Copyright (C) Andrew Tridgell 2003-2008 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "includes.h" #include "lib/cmdline/popt_common.h" #include "lib/events/events.h" #include "system/time.h" #include "system/filesys.h" #include "libcli/raw/request.h" #include "libcli/libcli.h" #include "libcli/raw/libcliraw.h" #include "libcli/smb2/smb2.h" #include "libcli/smb2/smb2_calls.h" #include "librpc/gen_ndr/security.h" #include "auth/credentials/credentials.h" #include "libcli/resolve/resolve.h" #include "auth/gensec/gensec.h" #include "param/param.h" #include "dynconfig.h" #define NSERVERS 2 #define NINSTANCES 2 /* global options */ static struct gentest_options { int showall; int analyze; int analyze_always; int analyze_continuous; uint_t max_open_handles; uint_t seed; uint_t numops; int use_oplocks; char **ignore_patterns; const char *seeds_file; int use_preset_seeds; int fast_reconnect; } options; /* mapping between open handles on the server and local handles */ static struct { bool active; uint_t instance; struct smb2_handle server_handle[NSERVERS]; const char *name; } *open_handles; static uint_t num_open_handles; /* state information for the servers. We open NINSTANCES connections to each server */ static struct { struct smb2_tree *tree[NINSTANCES]; char *server_name; char *share_name; struct cli_credentials *credentials; } servers[NSERVERS]; /* the seeds and flags for each operation */ static struct { uint_t seed; bool disabled; } *op_parms; /* oplock break info */ static struct { bool got_break; struct smb2_handle server_handle; uint16_t handle; uint8_t level; bool do_close; } oplocks[NSERVERS][NINSTANCES]; /* change notify reply info */ static struct { int notify_count; NTSTATUS status; union smb_notify notify; } notifies[NSERVERS][NINSTANCES]; /* info relevant to the current operation */ static struct { const char *name; uint_t seed; NTSTATUS status; uint_t opnum; TALLOC_CTX *mem_ctx; } current_op; static struct smb2_handle bad_smb2_handle; #define BAD_HANDLE 0xFFFE static bool oplock_handler(struct smbcli_transport *transport, uint16_t tid, uint16_t fnum, uint8_t level, void *private); static void idle_func(struct smb2_transport *transport, void *private); /* check if a string should be ignored. This is used as the basis for all error ignore settings */ static bool ignore_pattern(const char *str) { int i; if (!options.ignore_patterns) return false; for (i=0;options.ignore_patterns[i];i++) { if (strcmp(options.ignore_patterns[i], str) == 0 || gen_fnmatch(options.ignore_patterns[i], str) == 0) { DEBUG(2,("Ignoring '%s'\n", str)); return true; } } return false; } /***************************************************** connect to the servers *******************************************************/ static bool connect_servers_fast(void) { int h, i; /* close all open files */ for (h=0;husername, j); cli_credentials_set_workstation(servers[i].credentials, "gentest", CRED_SPECIFIED); status = smb2_connect(NULL, servers[i].server_name, servers[i].share_name, lp_resolve_context(lp_ctx), servers[i].credentials, &servers[i].tree[j], ev); if (!NT_STATUS_IS_OK(status)) { printf("Failed to connect to \\\\%s\\%s - %s\n", servers[i].server_name, servers[i].share_name, nt_errstr(status)); return false; } // smb2_oplock_handler(servers[i].cli[j]->transport, oplock_handler, NULL); smb2_transport_idle_handler(servers[i].tree[j]->session->transport, idle_func, 50000, NULL); } } return true; } /* work out the time skew between the servers - be conservative */ static uint_t time_skew(void) { uint_t ret; ret = labs(servers[0].tree[0]->session->transport->negotiate.system_time - servers[1].tree[0]->session->transport->negotiate.system_time); return ret + 300; } static bool smb2_handle_equal(const struct smb2_handle *h1, const struct smb2_handle *h2) { return memcmp(h1, h2, sizeof(struct smb2_handle)) == 0; } /* turn a server handle into a local handle */ static uint_t fnum_to_handle(int server, int instance, struct smb2_handle server_handle) { uint_t i; for (i=0;i 0 && count++ < 10*options.max_open_handles) { h = random() % options.max_open_handles; if (open_handles[h].active && open_handles[h].instance == instance) { return h; } } return BAD_HANDLE; } /* return a file handle, but skewed so we don't close the last couple of handles too readily */ static uint16_t gen_fnum_close(int instance) { if (num_open_handles < 3) { if (gen_chance(80)) return BAD_HANDLE; } return gen_fnum(instance); } /* generate an integer in a specified range */ static int gen_int_range(uint64_t min, uint64_t max) { uint_t r = random(); return min + (r % (1+max-min)); } /* return a fnum for use as a root fid be careful to call GEN_SET_FNUM() when you use this! */ static uint16_t gen_root_fid(int instance) { if (gen_chance(5)) return gen_fnum(instance); return 0; } /* generate a file offset */ static int gen_offset(void) { if (gen_chance(20)) return 0; // if (gen_chance(5)) return gen_int_range(0, 0xFFFFFFFF); return gen_int_range(0, 1024*1024); } /* generate a io count */ static int gen_io_count(void) { if (gen_chance(20)) return 0; // if (gen_chance(5)) return gen_int_range(0, 0xFFFFFFFF); return gen_int_range(0, 4096); } /* generate a filename */ static const char *gen_fname(void) { const char *names[] = {"gentest\\gentest.dat", "gentest\\foo", "gentest\\foo2.sym", "gentest\\foo3.dll", "gentest\\foo4", "gentest\\foo4:teststream1", "gentest\\foo4:teststream2", "gentest\\foo5.exe", "gentest\\foo5.exe:teststream3", "gentest\\foo5.exe:teststream4", "gentest\\foo6.com", "gentest\\blah", "gentest\\blah\\blergh.txt", "gentest\\blah\\blergh2", "gentest\\blah\\blergh3.txt", "gentest\\blah\\blergh4", "gentest\\blah\\blergh5.txt", "gentest\\blah\\blergh5", "gentest\\blah\\.", #if 0 /* this causes problem with w2k3 */ "gentest\\blah\\..", #endif "gentest\\a_very_long_name.bin", "gentest\\x.y", "gentest\\blah"}; int i; do { i = gen_int_range(0, ARRAY_SIZE(names)-1); } while (ignore_pattern(names[i])); return names[i]; } /* generate a filename with a higher chance of choosing an already open file */ static const char *gen_fname_open(int instance) { uint16_t h; h = gen_fnum(instance); if (h == BAD_HANDLE) { return gen_fname(); } return open_handles[h].name; } /* generate a wildcard pattern */ static const char *gen_pattern(void) { int i; const char *names[] = {"gentest\\*.dat", "gentest\\*", "gentest\\*.*", "gentest\\blah\\*.*", "gentest\\blah\\*", "gentest\\?"}; if (gen_chance(50)) return gen_fname(); do { i = gen_int_range(0, ARRAY_SIZE(names)-1); } while (ignore_pattern(names[i])); return names[i]; } static uint32_t gen_bits_levels(int nlevels, ...) { va_list ap; uint32_t pct; uint32_t mask; int i; va_start(ap, nlevels); for (i=0;isession->transport) { // smb2_transport_process(servers[i].tree[j]->session->transport); } } } } /* compare NTSTATUS, using checking ignored patterns */ static bool compare_status(NTSTATUS status1, NTSTATUS status2) { if (NT_STATUS_EQUAL(status1, status2)) return true; /* one code being an error and the other OK is always an error */ if (NT_STATUS_IS_OK(status1) || NT_STATUS_IS_OK(status2)) return false; /* if we are ignoring one of the status codes then consider this a match */ if (ignore_pattern(nt_errstr(status1)) || ignore_pattern(nt_errstr(status2))) { return true; } return false; } /* check for pending packets on all connections */ static void check_pending(void) { int i, j; msleep(20); for (j=0;jsession->transport); } } } /* check that the same oplock breaks have been received by all instances */ static bool check_oplocks(const char *call) { #if 0 int i, j; int tries = 0; again: check_pending(); for (j=0;j time_skew() && \ !ignore_pattern(#field)) { \ printf("Mismatch in %s - 0x%x 0x%x\n", #field, \ (int)parm[0].field, (int)parm[1].field); \ return false; \ } \ } while(0) #define CHECK_NTTIMES_EQUAL(field) do { \ if (labs(nt_time_to_unix(parm[0].field) - \ nt_time_to_unix(parm[1].field)) > time_skew() && \ !ignore_pattern(#field)) { \ printf("Mismatch in %s - 0x%x 0x%x\n", #field, \ (int)nt_time_to_unix(parm[0].field), \ (int)nt_time_to_unix(parm[1].field)); \ return false; \ } \ } while(0) /* generate ntcreatex operations */ static bool handler_ntcreatex(int instance) { struct smb2_create parm[NSERVERS]; NTSTATUS status[NSERVERS]; ZERO_STRUCT(parm[0]); parm[0].in.security_flags = gen_bits_levels(3, 70, 0x0, 70, 0x3, 100, 0xFF); parm[0].in.oplock_level = gen_bits_levels(3, 70, 0x0, 70, 0x9, 100, 0xFF); parm[0].in.impersonation_level = gen_bits_levels(3, 70, 0x0, 70, 0x3, 100, 0xFFFFFFFF); parm[0].in.create_flags = gen_bits_levels(2, 80, 0x0, 100, 0xFFFFFFFF); if (gen_chance(2)) { parm[0].in.create_flags |= gen_bits_mask(0xFFFFFFFF); } parm[0].in.reserved = gen_bits_levels(2, 80, 0x0, 100, 0xFFFFFFFF); if (gen_chance(2)) { parm[0].in.reserved |= gen_bits_mask(0xFFFFFFFF); } parm[0].in.desired_access = gen_access_mask(); parm[0].in.file_attributes = gen_attrib(); parm[0].in.share_access = gen_bits_mask2(0x7, 0xFFFFFFFF); parm[0].in.create_disposition = gen_open_disp(); parm[0].in.create_options = gen_create_options(); parm[0].in.fname = gen_fname_open(instance); if (!options.use_oplocks) { /* mask out oplocks */ parm[0].in.oplock_level = 0; } GEN_COPY_PARM; GEN_CALL(smb2_create(tree, current_op.mem_ctx, &parm[i])); CHECK_EQUAL(out.oplock_level); CHECK_EQUAL(out.reserved); CHECK_EQUAL(out.create_action); CHECK_NTTIMES_EQUAL(out.create_time); CHECK_NTTIMES_EQUAL(out.access_time); CHECK_NTTIMES_EQUAL(out.write_time); CHECK_NTTIMES_EQUAL(out.change_time); CHECK_EQUAL(out.alloc_size); CHECK_EQUAL(out.size); CHECK_EQUAL(out.file_attr); CHECK_EQUAL(out.reserved2); /* ntcreatex creates a new file handle */ ADD_HANDLE(parm[0].in.fname, out.file.handle); return true; } /* generate close operations */ static bool handler_close(int instance) { struct smb2_close parm[NSERVERS]; NTSTATUS status[NSERVERS]; ZERO_STRUCT(parm[0]); parm[0].in.file.handle.data[0] = gen_fnum_close(instance); parm[0].in.flags = gen_bits_mask2(0x1, 0xFFFF); GEN_COPY_PARM; GEN_SET_FNUM(in.file.handle); GEN_CALL(smb2_close(tree, &parm[i])); CHECK_EQUAL(out.flags); CHECK_EQUAL(out._pad); CHECK_NTTIMES_EQUAL(out.create_time); CHECK_NTTIMES_EQUAL(out.access_time); CHECK_NTTIMES_EQUAL(out.write_time); CHECK_NTTIMES_EQUAL(out.change_time); CHECK_EQUAL(out.alloc_size); CHECK_EQUAL(out.size); CHECK_EQUAL(out.file_attr); REMOVE_HANDLE(in.file.handle); return true; } /* generate read operations */ static bool handler_read(int instance) { struct smb2_read parm[NSERVERS]; NTSTATUS status[NSERVERS]; parm[0].in.file.handle.data[0] = gen_fnum(instance); parm[0].in.reserved = gen_bits_mask2(0x0, 0xFF); parm[0].in.length = gen_io_count(); parm[0].in.offset = gen_offset(); parm[0].in.min_count = gen_io_count(); parm[0].in.channel = gen_bits_mask2(0x0, 0xFFFFFFFF); parm[0].in.remaining = gen_bits_mask2(0x0, 0xFFFFFFFF); parm[0].in.channel_offset = gen_bits_mask2(0x0, 0xFFFF); parm[0].in.channel_length = gen_bits_mask2(0x0, 0xFFFF); GEN_COPY_PARM; GEN_SET_FNUM(in.file.handle); GEN_CALL(smb2_read(tree, current_op.mem_ctx, &parm[i])); CHECK_EQUAL(out.remaining); CHECK_EQUAL(out.reserved); CHECK_EQUAL(out.data.length); return true; } /* generate write operations */ static bool handler_write(int instance) { struct smb2_write parm[NSERVERS]; NTSTATUS status[NSERVERS]; parm[0].in.file.handle.data[0] = gen_fnum(instance); parm[0].in.offset = gen_offset(); parm[0].in.unknown1 = gen_bits_mask2(0, 0xFFFFFFFF); parm[0].in.unknown2 = gen_bits_mask2(0, 0xFFFFFFFF); parm[0].in.data = data_blob_talloc(current_op.mem_ctx, NULL, gen_io_count()); GEN_COPY_PARM; GEN_SET_FNUM(in.file.handle); GEN_CALL(smb2_write(tree, &parm[i])); CHECK_EQUAL(out._pad); CHECK_EQUAL(out.nwritten); CHECK_EQUAL(out.unknown1); return true; } #if 0 /* generate lockingx operations */ static bool handler_lock(int instance) { union smb_lock parm[NSERVERS]; NTSTATUS status[NSERVERS]; int n, nlocks; parm[0].lockx.level = RAW_LOCK_LOCKX; parm[0].lockx.in.file.fnum = gen_fnum(instance); parm[0].lockx.in.mode = gen_lock_mode(); parm[0].lockx.in.timeout = gen_timeout(); do { /* make sure we don't accidentially generate an oplock break ack - otherwise the server can just block forever */ parm[0].lockx.in.ulock_cnt = gen_lock_count(); parm[0].lockx.in.lock_cnt = gen_lock_count(); nlocks = parm[0].lockx.in.ulock_cnt + parm[0].lockx.in.lock_cnt; } while (nlocks == 0); if (nlocks > 0) { parm[0].lockx.in.locks = talloc_array(current_op.mem_ctx, struct smb_lock_entry, nlocks); for (n=0;ngeneric.level = levels[i].level; } /* compare returned fileinfo structures */ static bool cmp_fileinfo(int instance, union smb_fileinfo parm[NSERVERS], NTSTATUS status[NSERVERS]) { int i; switch (parm[0].generic.level) { case RAW_FILEINFO_GENERIC: return false; case RAW_FILEINFO_GETATTR: CHECK_EQUAL(getattr.out.attrib); CHECK_EQUAL(getattr.out.size); CHECK_TIMES_EQUAL(getattr.out.write_time); break; case RAW_FILEINFO_GETATTRE: CHECK_TIMES_EQUAL(getattre.out.create_time); CHECK_TIMES_EQUAL(getattre.out.access_time); CHECK_TIMES_EQUAL(getattre.out.write_time); CHECK_EQUAL(getattre.out.size); CHECK_EQUAL(getattre.out.alloc_size); CHECK_EQUAL(getattre.out.attrib); break; case RAW_FILEINFO_STANDARD: CHECK_TIMES_EQUAL(standard.out.create_time); CHECK_TIMES_EQUAL(standard.out.access_time); CHECK_TIMES_EQUAL(standard.out.write_time); CHECK_EQUAL(standard.out.size); CHECK_EQUAL(standard.out.alloc_size); CHECK_EQUAL(standard.out.attrib); break; case RAW_FILEINFO_EA_SIZE: CHECK_TIMES_EQUAL(ea_size.out.create_time); CHECK_TIMES_EQUAL(ea_size.out.access_time); CHECK_TIMES_EQUAL(ea_size.out.write_time); CHECK_EQUAL(ea_size.out.size); CHECK_EQUAL(ea_size.out.alloc_size); CHECK_EQUAL(ea_size.out.attrib); CHECK_EQUAL(ea_size.out.ea_size); break; case RAW_FILEINFO_ALL_EAS: CHECK_EQUAL(all_eas.out.num_eas); for (i=0;igeneric.level = levels[i].level; switch (info->generic.level) { case RAW_SFILEINFO_SETATTR: info->setattr.in.attrib = gen_attrib(); info->setattr.in.write_time = gen_timet(); break; case RAW_SFILEINFO_SETATTRE: info->setattre.in.create_time = gen_timet(); info->setattre.in.access_time = gen_timet(); info->setattre.in.write_time = gen_timet(); break; case RAW_SFILEINFO_STANDARD: info->standard.in.create_time = gen_timet(); info->standard.in.access_time = gen_timet(); info->standard.in.write_time = gen_timet(); break; case RAW_SFILEINFO_EA_SET: { static struct ea_struct ea; info->ea_set.in.num_eas = 1; info->ea_set.in.eas = &ea; info->ea_set.in.eas[0] = gen_ea_struct(); } break; case RAW_SFILEINFO_BASIC_INFO: case RAW_SFILEINFO_BASIC_INFORMATION: info->basic_info.in.create_time = gen_nttime(); info->basic_info.in.access_time = gen_nttime(); info->basic_info.in.write_time = gen_nttime(); info->basic_info.in.change_time = gen_nttime(); info->basic_info.in.attrib = gen_attrib(); break; case RAW_SFILEINFO_DISPOSITION_INFO: case RAW_SFILEINFO_DISPOSITION_INFORMATION: info->disposition_info.in.delete_on_close = gen_bool(); break; case RAW_SFILEINFO_ALLOCATION_INFO: case RAW_SFILEINFO_ALLOCATION_INFORMATION: info->allocation_info.in.alloc_size = gen_alloc_size(); break; case RAW_SFILEINFO_END_OF_FILE_INFO: case RAW_SFILEINFO_END_OF_FILE_INFORMATION: info->end_of_file_info.in.size = gen_offset(); break; case RAW_SFILEINFO_RENAME_INFORMATION: case RAW_SFILEINFO_RENAME_INFORMATION_SMB2: info->rename_information.in.overwrite = gen_bool(); info->rename_information.in.root_fid = gen_root_fid(instance); info->rename_information.in.new_name = gen_fname_open(instance); break; case RAW_SFILEINFO_POSITION_INFORMATION: info->position_information.in.position = gen_offset(); break; case RAW_SFILEINFO_MODE_INFORMATION: info->mode_information.in.mode = gen_bits_mask(0xFFFFFFFF); break; case RAW_SFILEINFO_GENERIC: case RAW_SFILEINFO_SEC_DESC: case RAW_SFILEINFO_UNIX_BASIC: case RAW_SFILEINFO_UNIX_LINK: case RAW_SFILEINFO_UNIX_HLINK: case RAW_SFILEINFO_1023: case RAW_SFILEINFO_1025: case RAW_SFILEINFO_1029: case RAW_SFILEINFO_1032: case RAW_SFILEINFO_1039: case RAW_SFILEINFO_1040: case RAW_SFILEINFO_UNIX_INFO2: /* Untested */ break; } } /* generate setfileinfo operations */ static bool handler_sfileinfo(int instance) { union smb_setfileinfo parm[NSERVERS]; NTSTATUS status[NSERVERS]; parm[0].generic.in.file.fnum = gen_fnum(instance); gen_setfileinfo(instance, &parm[0]); GEN_COPY_PARM; GEN_SET_FNUM(generic.in.file.fnum); GEN_CALL(smb_raw_setfileinfo(tree, &parm[i])); return true; } #endif /* wipe any relevant files */ static void wipe_files(void) { int i; NTSTATUS status; for (i=0;i 0) { printf("Deleted %d files on server %d\n", n, i); } } } /* dump the current seeds - useful for continuing a backtrack */ static void dump_seeds(void) { int i; FILE *f; if (!options.seeds_file) { return; } f = fopen("seeds.tmp", "w"); if (!f) return; for (i=0;i 0 && base+chunk < options.numops && options.numops > 1; ) { int i, max; chunk = MIN(chunk, options.numops / 2); /* mark this range as disabled */ max = MIN(options.numops, base+chunk); for (i=base;i 0); printf("Reduced to %d ops\n", options.numops); ret = run_test(ev, lp_ctx); if (ret != options.numops - 1) { printf("Inconsistent result? ret=%d numops=%d\n", ret, options.numops); } } /* start the main gentest process */ static bool start_gentest(struct event_context *ev, struct loadparm_context *lp_ctx) { int op; int ret; /* allocate the open_handles array */ open_handles = calloc(options.max_open_handles, sizeof(open_handles[0])); srandom(options.seed); op_parms = calloc(options.numops, sizeof(op_parms[0])); /* generate the seeds - after this everything is deterministic */ if (options.use_preset_seeds) { int numops; char **preset = file_lines_load(options.seeds_file, &numops, NULL); if (!preset) { printf("Failed to load %s - %s\n", options.seeds_file, strerror(errno)); exit(1); } if (numops < options.numops) { options.numops = numops; } for (op=0;op "); lp_ctx = cmdline_lp_ctx; servers[0].credentials = cli_credentials_init(talloc_autofree_context()); servers[1].credentials = cli_credentials_init(talloc_autofree_context()); cli_credentials_guess(servers[0].credentials, lp_ctx); cli_credentials_guess(servers[1].credentials, lp_ctx); while((opt = poptGetNextOpt(pc)) != -1) { switch (opt) { case OPT_UNCLIST: lp_set_cmdline(cmdline_lp_ctx, "torture:unclist", poptGetOptArg(pc)); break; case 'U': if (username_count == 2) { usage(pc); exit(1); } cli_credentials_parse_string(servers[username_count].credentials, poptGetOptArg(pc), CRED_SPECIFIED); username_count++; break; } } if (ignore_file) { options.ignore_patterns = file_lines_load(ignore_file, NULL, NULL); } argv_new = discard_const_p(char *, poptGetArgs(pc)); argc_new = argc; for (i=0; i= 3)) { usage(pc); exit(1); } setlinebuf(stdout); setup_logging("gentest", DEBUG_STDOUT); if (argc < 3 || argv[1][0] == '-') { usage(pc); exit(1); } setup_logging(argv[0], DEBUG_STDOUT); for (i=0;i