From 3067ec21fb34f46fd1683aad6d455e7d6da8f52e Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Fri, 12 Apr 2002 03:26:19 +0000 Subject: - added a mangling test suite that measures the collision rate on randomised filenames - fixed several mangling bugs that the test suite pointed out (This used to be commit 858fa7efc34f6e7cdf8500900aed3f7943c91348) --- source3/Makefile.in | 2 +- source3/smbd/mangle_hash2.c | 14 ++++ source3/torture/mangle_test.c | 162 ++++++++++++++++++++++++++++++++++++++++++ source3/torture/torture.c | 28 +++++--- 4 files changed, 194 insertions(+), 12 deletions(-) create mode 100644 source3/torture/mangle_test.c diff --git a/source3/Makefile.in b/source3/Makefile.in index 54e3238fba..476997512f 100644 --- a/source3/Makefile.in +++ b/source3/Makefile.in @@ -364,7 +364,7 @@ NMBLOOKUP_OBJ = utils/nmblookup.o $(PARAM_OBJ) $(UBIQX_OBJ) \ $(LIBSMB_OBJ) $(LIB_OBJ) SMBTORTURE_OBJ = torture/torture.o torture/nbio.o torture/scanner.o torture/utable.o \ - torture/denytest.o \ + torture/denytest.o torture/mangle_test.o \ $(LIBSMB_OBJ) $(PARAM_OBJ) $(UBIQX_OBJ) $(LIB_OBJ) MASKTEST_OBJ = torture/masktest.o $(LIBSMB_OBJ) $(PARAM_OBJ) \ diff --git a/source3/smbd/mangle_hash2.c b/source3/smbd/mangle_hash2.c index 96ca7360b8..959a93e07b 100644 --- a/source3/smbd/mangle_hash2.c +++ b/source3/smbd/mangle_hash2.c @@ -344,6 +344,7 @@ static BOOL check_cache(char *name) /* we found it - construct the full name */ strncpy(extension, name+9, 3); + extension[3] = 0; if (extension[0]) { M_DEBUG(0,("check_cache: %s -> %s.%s\n", name, prefix, extension)); @@ -435,6 +436,19 @@ static BOOL name_map(char *name, BOOL need83, BOOL cache83) /* find the '.' if any */ dot_p = strrchr(name, '.'); + if (dot_p) { + /* if the extension contains any illegal characters or + is too long or zero length then we treat it as part + of the prefix */ + for (i=0; i<4 && dot_p[i+1]; i++) { + if (! FLAG_CHECK(dot_p[i+1], FLAG_ASCII)) { + dot_p = NULL; + break; + } + } + if (i == 0 || i == 4) dot_p = NULL; + } + /* the leading character in the mangled name is taken from the first character of the name, if it is ascii otherwise '_' is used diff --git a/source3/torture/mangle_test.c b/source3/torture/mangle_test.c new file mode 100644 index 0000000000..9024925beb --- /dev/null +++ b/source3/torture/mangle_test.c @@ -0,0 +1,162 @@ +/* + Unix SMB/CIFS implementation. + SMB torture tester - mangling test + Copyright (C) Andrew Tridgell 2002 + + 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 2 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, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include "includes.h" + +static TDB_CONTEXT *tdb; + +#define NAME_LENGTH 30 + +static unsigned total, collisions; + +static BOOL test_one(struct cli_state *cli, const char *name) +{ + int fnum; + fstring shortname; + fstring name2; + NTSTATUS status; + TDB_DATA data; + + total++; + + fnum = cli_open(cli, name, O_RDWR|O_CREAT|O_EXCL, DENY_NONE); + if (fnum == -1) { + printf("open of %s failed (%s)\n", name, cli_errstr(cli)); + return False; + } + + if (!cli_close(cli, fnum)) { + printf("close of %s failed (%s)\n", name, cli_errstr(cli)); + return False; + } + + /* get the short name */ + status = cli_qpathinfo_alt_name(cli, name, shortname); + if (!NT_STATUS_IS_OK(status)) { + printf("query altname of %s failed (%s)\n", name, cli_errstr(cli)); + return False; + } + + snprintf(name2, sizeof(name2), "\\mangle_test\\%s", shortname); + if (!cli_unlink(cli, name2)) { + printf("unlink of %s (%s) failed (%s)\n", + name2, name, cli_errstr(cli)); + return False; + } + + /* see if the short name is already in the tdb */ + data = tdb_fetch_by_string(tdb, shortname); + if (data.dptr) { + /* maybe its a duplicate long name? */ + if (strcasecmp(name, data.dptr) != 0) { + /* we have a collision */ + collisions++; + printf("Collision between %s and %s -> %s\n", + name, data.dptr, shortname); + } + free(data.dptr); + } else { + /* store it for later */ + tdb_store_by_string(tdb, shortname, name, strlen(name)+1); + } + + return True; +} + + +static void gen_name(char *name) +{ + const char *chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._-$~"; + unsigned max_idx = strlen(chars); + unsigned len; + int i; + char *p; + + fstrcpy(name, "\\mangle_test\\"); + p = name + strlen(name); + + len = 1 + random() % NAME_LENGTH; + + for (i=0;i