| 1 |
/* |
| 2 |
Unix SMB/CIFS implementation. |
| 3 |
SMB torture tester - mangling test |
| 4 |
Copyright (C) Andrew Tridgell 2002 |
| 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 3 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, see <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "includes.h" |
| 21 |
#include "system/filesys.h" |
| 22 |
#include "system/dir.h" |
| 23 |
#include <tdb.h> |
| 24 |
#include "../lib/util/util_tdb.h" |
| 25 |
#include "libcli/libcli.h" |
| 26 |
#include "torture/util.h" |
| 27 |
#include "torture/basic/proto.h" |
| 28 |
|
| 29 |
#undef strcasecmp |
| 30 |
|
| 31 |
static TDB_CONTEXT *tdb; |
| 32 |
|
| 33 |
#define NAME_LENGTH 20 |
| 34 |
|
| 35 |
static unsigned int total, collisions, failures; |
| 36 |
|
| 37 |
static bool test_one(struct torture_context *tctx ,struct smbcli_state *cli, |
| 38 |
const char *name) |
| 39 |
{ |
| 40 |
int fnum; |
| 41 |
const char *shortname; |
| 42 |
const char *name2; |
| 43 |
NTSTATUS status; |
| 44 |
TDB_DATA data; |
| 45 |
|
| 46 |
total++; |
| 47 |
|
| 48 |
fnum = smbcli_open(cli->tree, name, O_RDWR|O_CREAT|O_EXCL, DENY_NONE); |
| 49 |
if (fnum == -1) { |
| 50 |
printf("open of %s failed (%s)\n", name, smbcli_errstr(cli->tree)); |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) { |
| 55 |
printf("close of %s failed (%s)\n", name, smbcli_errstr(cli->tree)); |
| 56 |
return false; |
| 57 |
} |
| 58 |
|
| 59 |
/* get the short name */ |
| 60 |
status = smbcli_qpathinfo_alt_name(cli->tree, name, &shortname); |
| 61 |
if (!NT_STATUS_IS_OK(status)) { |
| 62 |
printf("query altname of %s failed (%s)\n", name, smbcli_errstr(cli->tree)); |
| 63 |
return false; |
| 64 |
} |
| 65 |
|
| 66 |
name2 = talloc_asprintf(tctx, "\\mangle_test\\%s", shortname); |
| 67 |
if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, name2))) { |
| 68 |
printf("unlink of %s (%s) failed (%s)\n", |
| 69 |
name2, name, smbcli_errstr(cli->tree)); |
| 70 |
return false; |
| 71 |
} |
| 72 |
|
| 73 |
/* recreate by short name */ |
| 74 |
fnum = smbcli_open(cli->tree, name2, O_RDWR|O_CREAT|O_EXCL, DENY_NONE); |
| 75 |
if (fnum == -1) { |
| 76 |
printf("open2 of %s failed (%s)\n", name2, smbcli_errstr(cli->tree)); |
| 77 |
return false; |
| 78 |
} |
| 79 |
if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) { |
| 80 |
printf("close of %s failed (%s)\n", name, smbcli_errstr(cli->tree)); |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
/* and unlink by long name */ |
| 85 |
if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, name))) { |
| 86 |
printf("unlink2 of %s (%s) failed (%s)\n", |
| 87 |
name, name2, smbcli_errstr(cli->tree)); |
| 88 |
failures++; |
| 89 |
smbcli_unlink(cli->tree, name2); |
| 90 |
return true; |
| 91 |
} |
| 92 |
|
| 93 |
/* see if the short name is already in the tdb */ |
| 94 |
data = tdb_fetch_bystring(tdb, shortname); |
| 95 |
if (data.dptr) { |
| 96 |
/* maybe its a duplicate long name? */ |
| 97 |
if (strcasecmp(name, (const char *)data.dptr) != 0) { |
| 98 |
/* we have a collision */ |
| 99 |
collisions++; |
| 100 |
printf("Collision between %s and %s -> %s " |
| 101 |
" (coll/tot: %u/%u)\n", |
| 102 |
name, data.dptr, shortname, collisions, total); |
| 103 |
} |
| 104 |
free(data.dptr); |
| 105 |
} else { |
| 106 |
TDB_DATA namedata; |
| 107 |
/* store it for later */ |
| 108 |
namedata.dptr = discard_const_p(uint8_t, name); |
| 109 |
namedata.dsize = strlen(name)+1; |
| 110 |
tdb_store_bystring(tdb, shortname, namedata, TDB_REPLACE); |
| 111 |
} |
| 112 |
|
| 113 |
return true; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
static char *gen_name(TALLOC_CTX *mem_ctx) |
| 118 |
{ |
| 119 |
const char *chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._-$~..."; |
| 120 |
unsigned int max_idx = strlen(chars); |
| 121 |
unsigned int len; |
| 122 |
int i; |
| 123 |
char *p; |
| 124 |
char *name; |
| 125 |
|
| 126 |
name = talloc_strdup(mem_ctx, "\\mangle_test\\"); |
| 127 |
|
| 128 |
len = 1 + random() % NAME_LENGTH; |
| 129 |
|
| 130 |
name = talloc_realloc(mem_ctx, name, char, strlen(name) + len + 6); |
| 131 |
p = name + strlen(name); |
| 132 |
|
| 133 |
for (i=0;i<len;i++) { |
| 134 |
p[i] = chars[random() % max_idx]; |
| 135 |
} |
| 136 |
|
| 137 |
p[i] = 0; |
| 138 |
|
| 139 |
if (ISDOT(p) || ISDOTDOT(p)) { |
| 140 |
p[0] = '_'; |
| 141 |
} |
| 142 |
|
| 143 |
/* have a high probability of a common lead char */ |
| 144 |
if (random() % 2 == 0) { |
| 145 |
p[0] = 'A'; |
| 146 |
} |
| 147 |
|
| 148 |
/* and a medium probability of a common lead string */ |
| 149 |
if ((len > 5) && (random() % 10 == 0)) { |
| 150 |
strlcpy(p, "ABCDE", 6); |
| 151 |
} |
| 152 |
|
| 153 |
/* and a high probability of a good extension length */ |
| 154 |
if (random() % 2 == 0) { |
| 155 |
char *s = strrchr(p, '.'); |
| 156 |
if (s) { |
| 157 |
s[4] = 0; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
return name; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
bool torture_mangle(struct torture_context *torture, |
| 166 |
struct smbcli_state *cli) |
| 167 |
{ |
| 168 |
extern int torture_numops; |
| 169 |
int i; |
| 170 |
|
| 171 |
/* we will use an internal tdb to store the names we have used */ |
| 172 |
tdb = tdb_open(NULL, 100000, TDB_INTERNAL, 0, 0); |
| 173 |
if (!tdb) { |
| 174 |
printf("ERROR: Failed to open tdb\n"); |
| 175 |
return false; |
| 176 |
} |
| 177 |
|
| 178 |
if (!torture_setup_dir(cli, "\\mangle_test")) { |
| 179 |
return false; |
| 180 |
} |
| 181 |
|
| 182 |
for (i=0;i<torture_numops;i++) { |
| 183 |
char *name; |
| 184 |
|
| 185 |
name = gen_name(torture); |
| 186 |
|
| 187 |
if (!test_one(torture, cli, name)) { |
| 188 |
break; |
| 189 |
} |
| 190 |
if (total && total % 100 == 0) { |
| 191 |
if (torture_setting_bool(torture, "progress", true)) { |
| 192 |
printf("collisions %u/%u - %.2f%% (%u failures)\r", |
| 193 |
collisions, total, (100.0*collisions) / total, failures); |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
smbcli_unlink_wcard(cli->tree, "\\mangle_test\\*"); |
| 199 |
if (NT_STATUS_IS_ERR(smbcli_rmdir(cli->tree, "\\mangle_test"))) { |
| 200 |
printf("ERROR: Failed to remove directory\n"); |
| 201 |
return false; |
| 202 |
} |
| 203 |
|
| 204 |
printf("\nTotal collisions %u/%u - %.2f%% (%u failures)\n", |
| 205 |
collisions, total, (100.0*collisions) / total, failures); |
| 206 |
|
| 207 |
return (failures == 0); |
| 208 |
} |