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