Work in progress.
Jeremy Allison
jra at samba.org
Tue Sep 4 22:23:10 GMT 2007
Here's the current patch. Compiles smbd/filename.c statcache.c
but no changes to callers yet. Shows how I intend to change the
mangle interface and the unix_convert interface. The only remaining
pstring I need to think about is in check_reduced_name() in smbd/vfs.c,
where we need at least a PATH_MAX buf - currently on the stack....
Jeremy.
-------------- next part --------------
Index: Makefile.in
===================================================================
--- Makefile.in (revision 24957)
+++ Makefile.in (working copy)
@@ -511,7 +511,7 @@
auth/auth_compat.o auth/auth_ntlmssp.o \
$(PLAINTEXT_AUTH_OBJ) $(SLCACHE_OBJ) $(DCUTIL_OBJ)
-MANGLE_OBJ = smbd/mangle.o smbd/mangle_hash.o smbd/mangle_map.o smbd/mangle_hash2.o
+MANGLE_OBJ = smbd/mangle.o smbd/mangle_hash.o smbd/mangle_hash2.o
SMBD_OBJ_MAIN = smbd/server.o
Index: smbd/statcache.c
===================================================================
--- smbd/statcache.c (revision 24957)
+++ smbd/statcache.c (working copy)
@@ -2,7 +2,7 @@
Unix SMB/CIFS implementation.
stat cache code
Copyright (C) Andrew Tridgell 1992-2000
- Copyright (C) Jeremy Allison 1999-2004
+ Copyright (C) Jeremy Allison 1999-2007
Copyright (C) Andrew Bartlett <abartlet at samba.org> 2003
Copyright (C) Volker Lendecke 2007
@@ -48,9 +48,11 @@
char *original_path;
size_t original_path_length;
size_t sc_size = lp_max_stat_cache_size();
+ TALLOC_CTX *ctx = talloc_tos();
- if (!lp_stat_cache())
+ if (!lp_stat_cache()) {
return;
+ }
if (sc_size && (tdb_map_size(tdb_stat_cache) > sc_size*1024)) {
reset_stat_cache();
@@ -73,8 +75,9 @@
* would be a waste.
*/
- if (case_sensitive && (strcmp(full_orig_name, translated_path) == 0))
+ if (case_sensitive && (strcmp(full_orig_name, translated_path) == 0)) {
return;
+ }
/*
* Remove any trailing '/' characters from the
@@ -94,9 +97,9 @@
}
if(case_sensitive) {
- original_path = SMB_STRDUP(full_orig_name);
+ original_path = talloc_strdup(ctx,full_orig_name);
} else {
- original_path = strdup_upper(full_orig_name);
+ original_path = talloc_strdup_upper(ctx,full_orig_name);
}
if (!original_path) {
@@ -118,7 +121,7 @@
(unsigned long)original_path_length,
translated_path,
(unsigned long)translated_path_length));
- SAFE_FREE(original_path);
+ TALLOC_FREE(original_path);
return;
}
@@ -148,7 +151,7 @@
translated_path));
}
- SAFE_FREE(original_path);
+ TALLOC_FREE(original_path);
}
/**
@@ -179,9 +182,11 @@
size_t translated_path_length;
TDB_DATA data_val;
char *name;
+ TALLOC_CTX *ctx = talloc_tos();
- if (!lp_stat_cache())
+ if (!lp_stat_cache()) {
return False;
+ }
name = *pname;
namelen = strlen(name);
@@ -198,14 +203,14 @@
}
if (conn->case_sensitive) {
- chk_name = SMB_STRDUP(name);
+ chk_name = talloc_strdup(ctx,name);
if (!chk_name) {
DEBUG(0, ("stat_cache_lookup: strdup failed!\n"));
return False;
}
} else {
- chk_name = strdup_upper(name);
+ chk_name = talloc_strdup_upper(ctx,name);
if (!chk_name) {
DEBUG(0, ("stat_cache_lookup: strdup_upper failed!\n"));
return False;
@@ -239,7 +244,7 @@
* We reached the end of the name - no match.
*/
DO_PROFILE_INC(statcache_misses);
- SAFE_FREE(chk_name);
+ TALLOC_FREE(chk_name);
return False;
}
@@ -255,7 +260,7 @@
if ((*chk_name == '\0')
|| ISDOT(chk_name) || ISDOTDOT(chk_name)) {
DO_PROFILE_INC(statcache_misses);
- SAFE_FREE(chk_name);
+ TALLOC_FREE(chk_name);
return False;
}
}
@@ -276,7 +281,7 @@
if (SMB_VFS_STAT(conn, translated_path, pst) != 0) {
/* Discard this entry - it doesn't exist in the filesystem. */
tdb_delete_bystring(tdb_stat_cache, chk_name);
- SAFE_FREE(chk_name);
+ TALLOC_FREE(chk_name);
SAFE_FREE(data_val.dptr);
return False;
}
@@ -287,28 +292,29 @@
}
else {
if (num_components == 0) {
- name = SMB_STRNDUP(translated_path,
+ name = talloc_strndup(ctx, translated_path,
translated_path_length);
} else {
char *sp;
sp = strnrchr_m(name, '/', num_components);
if (sp) {
- asprintf(&name, "%.*s%s",
+ name = talloc_asprintf(ctx,"%.*s%s",
(int)translated_path_length,
translated_path, sp);
} else {
- name = SMB_STRNDUP(translated_path,
- translated_path_length);
+ name = talloc_strndup(ctx,
+ translated_path,
+ translated_path_length);
}
}
if (name == NULL) {
/*
* TODO: Get us out of here with a real error message
*/
- smb_panic("malloc failed");
+ smb_panic("talloc failed");
}
- SAFE_FREE(*pname);
+ TALLOC_FREE(*pname);
*pname = name;
}
@@ -319,7 +325,7 @@
++*start;
*dirpath = translated_path;
- SAFE_FREE(chk_name);
+ TALLOC_FREE(chk_name);
return (namelen == translated_path_length);
}
@@ -344,7 +350,7 @@
void stat_cache_delete(const char *name)
{
- char *lname = strdup_upper(name);
+ char *lname = talloc_strdup_upper(talloc_tos(), name);
if (!lname) {
return;
@@ -353,7 +359,7 @@
lname, name ));
tdb_delete_bystring(tdb_stat_cache, lname);
- SAFE_FREE(lname);
+ TALLOC_FREE(lname);
}
/***************************************************************
Index: smbd/mangle.c
===================================================================
--- smbd/mangle.c (revision 24957)
+++ smbd/mangle.c (working copy)
@@ -1,18 +1,18 @@
-/*
+/*
Unix SMB/CIFS implementation.
Name mangling interface
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 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 <http://www.gnu.org/licenses/>.
*/
@@ -101,49 +101,45 @@
}
/*
- try to reverse map a 8.3 name to the original filename. This doesn't have to
+ try to reverse map a 8.3 name to the original filename. This doesn't have to
always succeed, as the directory handling code in smbd will scan the directory
looking for a matching name if it doesn't. It should succeed most of the time
or there will be a huge performance penalty
*/
-BOOL mangle_check_cache(char *s, size_t maxlen,
+BOOL mangle_check_cache(TALLOC_CTX *ctx,
+ const char *in,
+ char **out, /* talloced on the given context. */
const struct share_params *p)
{
- return mangle_fns->check_cache(s, maxlen, p);
+ return mangle_fns->check_cache(ctx, in, out, p);
}
-BOOL mangle_check_cache_alloc(const char *name, char **presult,
- const struct share_params *p)
-{
- pstring tmp;
- char *result;
- pstrcpy(tmp, name);
-
- if (!mangle_check_cache(tmp, sizeof(pstring)-1, p)
- || !(result = SMB_STRDUP(tmp))) {
- return False;
- }
- *presult = result;
- return True;
-}
-
-/*
- map a long filename to a 8.3 name.
+/*
+ map a long filename to a 8.3 name.
*/
-void mangle_map(pstring OutName, BOOL need83, BOOL cache83,
+void mangle_map(const char *in,
+ char out[13],
+ BOOL need83,
+ BOOL cache83,
const struct share_params *p)
{
+ char *map = lp_mangled_map(p);
+
/* name mangling can be disabled for speed, in which case
we just truncate the string */
if (!lp_manglednames(p)) {
if (need83) {
- string_truncate(OutName, 12);
+ strncpy(out,in,12);
+ out[12] = '\0';
}
return;
}
- /* invoke the inane "mangled map" code */
- mangle_map_filename(OutName, p);
- mangle_fns->name_map(OutName, need83, cache83, lp_defaultcase(p->service), p);
+ mangle_fns->name_map(in,
+ out,
+ need83,
+ cache83,
+ lp_defaultcase(p->service),
+ p);
}
Index: smbd/filename.c
===================================================================
--- smbd/filename.c (revision 24957)
+++ smbd/filename.c (working copy)
@@ -2,7 +2,7 @@
Unix SMB/CIFS implementation.
filename handling routines
Copyright (C) Andrew Tridgell 1992-1998
- Copyright (C) Jeremy Allison 1999-2004
+ Copyright (C) Jeremy Allison 1999-2007
Copyright (C) Ying Chen 2000
Copyright (C) Volker Lendecke 2007
@@ -37,11 +37,10 @@
const char *name2,
const struct share_params *p)
{
- pstring tmpname;
+ char mname[13];
- pstrcpy(tmpname, name2);
- mangle_map(tmpname, True, False, p);
- return strequal(name1, tmpname);
+ mangle_map(name2, mname, True, False, p);
+ return strequal(name1, mname);
}
/****************************************************************************
@@ -107,9 +106,10 @@
****************************************************************************/
NTSTATUS unix_convert(connection_struct *conn,
- pstring orig_path,
+ const char *orig_path,
BOOL allow_wcard_last_component,
- char *saved_last_component,
+ char **pp_conv_path,
+ char **pp_saved_last_component,
SMB_STRUCT_STAT *pst)
{
SMB_STRUCT_STAT st;
@@ -119,16 +119,20 @@
BOOL component_was_mangled = False;
BOOL name_has_wildcard = False;
NTSTATUS result;
+ TALLOC_CTX *ctx = talloc_tos();
SET_STAT_INVALID(*pst);
-
- if(saved_last_component) {
- *saved_last_component = 0;
+ *pp_conv_path = NULL;
+ if(pp_saved_last_component) {
+ *pp_saved_last_component = NULL;
}
if (conn->printer) {
/* we don't ever use the filenames on a printer share as a
filename - so don't convert them */
+ if (!(*pp_conv_path = talloc_strdup(ctx,orig_path))) {
+ return NT_STATUS_NO_MEMORY;
+ }
return NT_STATUS_OK;
}
@@ -157,11 +161,13 @@
*/
if (!*orig_path) {
- if (!(name = SMB_STRDUP("."))) {
+ if (!(name = talloc_strdup(ctx,"."))) {
return NT_STATUS_NO_MEMORY;
}
if (SMB_VFS_STAT(conn,name,&st) == 0) {
*pst = st;
+ } else {
+ return map_nt_error_from_unix(errno);
}
DEBUG(5,("conversion finished \"\" -> %s\n",name));
goto done;
@@ -183,17 +189,18 @@
* Ensure saved_last_component is valid even if file exists.
*/
- if(saved_last_component) {
+ if(pp_saved_last_component) {
end = strrchr_m(orig_path, '/');
if (end) {
- pstrcpy(saved_last_component, end + 1);
+ *pp_saved_last_component = talloc_strdup(ctx, end + 1);
} else {
- pstrcpy(saved_last_component, orig_path);
+ *pp_saved_last_component = talloc_strdup(ctx,
+ orig_path);
}
}
- if (!(name = SMB_STRDUP(orig_path))) {
- DEBUG(0, ("strdup failed\n"));
+ if (!(name = talloc_strdup(ctx, orig_path))) {
+ DEBUG(0, ("talloc_strdup failed\n"));
return NT_STATUS_NO_MEMORY;
}
@@ -224,9 +231,9 @@
* building the directories with asprintf and free it.
*/
- if ((dirpath == NULL) && (!(dirpath = SMB_STRDUP("")))) {
- DEBUG(0, ("strdup failed\n"));
- SAFE_FREE(name);
+ if ((dirpath == NULL) && (!(dirpath = talloc_strdup(ctx,"")))) {
+ DEBUG(0, ("talloc_strdup failed\n"));
+ TALLOC_FREE(name);
return NT_STATUS_NO_MEMORY;
}
@@ -264,8 +271,7 @@
*/
if (conn->case_sensitive &&
- !mangle_is_mangled(name, conn->params) &&
- !*lp_mangled_map(conn->params)) {
+ !mangle_is_mangled(name, conn->params)) {
goto done;
}
@@ -302,8 +308,14 @@
*end = 0;
}
- if (saved_last_component != 0) {
- pstrcpy(saved_last_component, end ? end + 1 : start);
+ if (pp_saved_last_component) {
+ TALLOC_FREE(*pp_saved_last_component);
+ *pp_saved_last_component = talloc_strdup(ctx,
+ end ? end + 1 : start);
+ if (!*pp_saved_last_component) {
+ DEBUG(0, ("talloc failed\n"));
+ return NT_STATUS_NO_MEMORY;
+ }
}
/* The name cannot have a component of "." */
@@ -473,25 +485,27 @@
*/
if (mangle_is_mangled(start, conn->params)
- && mangle_check_cache_alloc(start,
- &unmangled,
- conn->params)) {
+ && mangle_check_cache(ctx,
+ start,
+ &unmangled,
+ conn->params)) {
char *tmp;
size_t start_ofs = start - name;
if (*dirpath != '\0') {
- asprintf(&tmp, "%s/%s", dirpath,
- unmangled);
- SAFE_FREE(unmangled);
+ tmp = talloc_asprintf(ctx,
+ "%s/%s", dirpath,
+ unmangled);
+ TALLOC_FREE(unmangled);
}
else {
tmp = unmangled;
}
if (tmp == NULL) {
- DEBUG(0, ("malloc failed\n"));
- result = NT_STATUS_NO_MEMORY;
+ DEBUG(0, ("talloc failed\n"));
+ return NT_STATUS_NO_MEMORY;
}
- SAFE_FREE(name);
+ TALLOC_FREE(name);
name = tmp;
start = name + start_ofs;
end = start + strlen(start);
@@ -511,18 +525,20 @@
size_t start_ofs = start - name;
if (*dirpath != '\0') {
- asprintf(&tmp, "%s/%s/%s", dirpath,
- found_name, end+1);
+ tmp = talloc_asprintf(ctx,
+ "%s/%s/%s", dirpath,
+ found_name, end+1);
}
else {
- asprintf(&tmp, "%s/%s", found_name,
- end+1);
+ tmp = talloc_asprintf(ctx,
+ "%s/%s", found_name,
+ end+1);
}
if (tmp == NULL) {
- DEBUG(0, ("asprintf failed\n"));
- result = NT_STATUS_NO_MEMORY;
+ DEBUG(0, ("talloc_asprintf failed\n"));
+ return NT_STATUS_NO_MEMORY;
}
- SAFE_FREE(name);
+ TALLOC_FREE(name);
name = tmp;
start = name + start_ofs;
end = start + strlen(found_name);
@@ -532,18 +548,19 @@
size_t start_ofs = start - name;
if (*dirpath != '\0') {
- asprintf(&tmp, "%s/%s", dirpath,
- found_name);
+ tmp = talloc_asprintf(ctx,
+ "%s/%s", dirpath,
+ found_name);
}
else {
- tmp = SMB_STRDUP(found_name);
+ tmp = talloc_strdup(ctx,
+ found_name);
}
if (tmp == NULL) {
- DEBUG(0, ("malloc failed\n"));
- result = NT_STATUS_NO_MEMORY;
- goto fail;
+ DEBUG(0, ("talloc failed\n"));
+ return NT_STATUS_NO_MEMORY;
}
- SAFE_FREE(name);
+ TALLOC_FREE(name);
name = tmp;
start = name + start_ofs;
@@ -560,7 +577,7 @@
}
}
- SAFE_FREE(found_name);
+ TALLOC_FREE(found_name);
} /* end else */
#ifdef DEVELOPER
@@ -577,19 +594,19 @@
*/
if (*dirpath != '\0') {
- char *tmp;
-
- if (asprintf(&tmp, "%s/%s", dirpath, start) == -1) {
- DEBUG(0, ("asprintf failed\n"));
+ char *tmp = talloc_asprintf(ctx,
+ "%s/%s", dirpath, start);
+ if (!tmp) {
+ DEBUG(0, ("talloc_asprintf failed\n"));
return NT_STATUS_NO_MEMORY;
}
- SAFE_FREE(dirpath);
+ TALLOC_FREE(dirpath);
dirpath = tmp;
}
else {
- SAFE_FREE(dirpath);
- if (!(dirpath = SMB_STRDUP(start))) {
- DEBUG(0, ("strdup failed\n"));
+ TALLOC_FREE(dirpath);
+ if (!(dirpath = talloc_strdup(ctx,start))) {
+ DEBUG(0, ("talloc_strdup failed\n"));
return NT_STATUS_NO_MEMORY;
}
}
@@ -628,17 +645,19 @@
DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
done:
- pstrcpy(orig_path, name);
- SAFE_FREE(name);
- SAFE_FREE(dirpath);
+ *pp_conv_path = name;
+ TALLOC_FREE(dirpath);
return NT_STATUS_OK;
fail:
DEBUG(10, ("dirpath = [%s] start = [%s]\n", dirpath, start));
- pstrcpy(orig_path, dirpath);
- pstrcat(orig_path, "/");
- pstrcat(orig_path, start);
- SAFE_FREE(name);
- SAFE_FREE(dirpath);
+ *pp_conv_path = talloc_asprintf(ctx,
+ "%s/%s", dirpath, start);
+ if (!*pp_conv_path) {
+ DEBUG(0, ("talloc_asprintf failed\n"));
+ return NT_STATUS_NO_MEMORY;
+ }
+ TALLOC_FREE(name);
+ TALLOC_FREE(dirpath);
return result;
}
@@ -649,7 +668,7 @@
a valid one for the user to access.
****************************************************************************/
-NTSTATUS check_name(connection_struct *conn, const pstring name)
+NTSTATUS check_name(connection_struct *conn, const char *name)
{
if (IS_VETO_PATH(conn, name)) {
/* Is it not dot or dot dot. */
@@ -682,8 +701,9 @@
BOOL case_sensitive)
{
/* Normal filename handling */
- if (case_sensitive)
+ if (case_sensitive) {
return(strcmp(name1,name2) == 0);
+ }
return(strequal(name1,name2));
}
@@ -701,12 +721,14 @@
BOOL mangled;
char *unmangled_name = NULL;
long curpos;
+ TALLOC_CTX *ctx = talloc_tos();
mangled = mangle_is_mangled(name, conn->params);
/* handle null paths */
- if ((path == NULL) || (*path == 0))
+ if ((path == NULL) || (*path == 0)) {
path = ".";
+ }
/*
* The incoming name can be mangled, and if we de-mangle it
@@ -724,15 +746,17 @@
*/
if (mangled && !conn->case_sensitive) {
- mangled = !mangle_check_cache_alloc(name, &unmangled_name,
- conn->params);
+ mangled = !mangle_check_cache(ctx,
+ name,
+ &unmangled_name,
+ conn->params);
name = unmangled_name;
}
/* open the directory */
if (!(cur_dir = OpenDir(conn, path, NULL, 0))) {
DEBUG(3,("scan dir didn't open dir [%s]\n",path));
- SAFE_FREE(unmangled_name);
+ TALLOC_FREE(unmangled_name);
return(False);
}
@@ -741,8 +765,7 @@
while ((dname = ReadDirName(cur_dir, &curpos))) {
/* Is it dot or dot dot. */
- if ((dname[0] == '.') && (!dname[1] ||
- (dname[1] == '.' && !dname[2]))) {
+ if (ISDOT(dname) || ISDOTDOT(dname)) {
continue;
}
@@ -760,15 +783,19 @@
if ((mangled && mangled_equal(name,dname,conn->params)) ||
fname_equal(name, dname, conn->case_sensitive)) {
/* we've found the file, change it's name and return */
- *found_name = SMB_STRDUP(dname);
- SAFE_FREE(unmangled_name);
+ *found_name = talloc_strdup(ctx,dname);
+ TALLOC_FREE(unmangled_name);
CloseDir(cur_dir);
+ if (!*found_name) {
+ errno = ENOMEM;
+ return False;
+ }
return(True);
}
}
- SAFE_FREE(unmangled_name);
+ TALLOC_FREE(unmangled_name);
CloseDir(cur_dir);
errno = ENOENT;
- return(False);
+ return False;
}
Index: lib/charcnv.c
===================================================================
--- lib/charcnv.c (revision 24957)
+++ lib/charcnv.c (working copy)
@@ -804,6 +804,71 @@
return SMB_STRDUP(out_buffer);
}
+/**
+ talloc_strdup() a unix string to upper case.
+**/
+
+char *talloc_strdup_upper(TALLOC_CTX *ctx, const char *s)
+{
+ char *out_buffer = talloc_strdup(ctx,s);
+ const unsigned char *p = (const unsigned char *)s;
+ unsigned char *q = (unsigned char *)out_buffer;
+
+ if (!q) {
+ return NULL;
+ }
+
+ /* this is quite a common operation, so we want it to be
+ fast. We optimise for the ascii case, knowing that all our
+ supported multi-byte character sets are ascii-compatible
+ (ie. they match for the first 128 chars) */
+
+ while (1) {
+ if (*p & 0x80)
+ break;
+ *q++ = toupper_ascii(*p);
+ if (!*p)
+ break;
+ p++;
+ }
+
+ if (*p) {
+ /* MB case. */
+ size_t size;
+ smb_ucs2_t *ubuf = NULL;
+
+ /* We're not using the ascii buffer above. */
+ TALLOC_FREE(out_buffer);
+
+ size = convert_string_talloc(ctx, CH_UNIX, CH_UTF16LE,
+ s, strlen(s),
+ (void *)&ubuf,
+ True);
+ if (size == (size_t)-1) {
+ return NULL;
+ }
+
+ strupper_w(ubuf);
+
+ size = convert_string_talloc(ctx, CH_UTF16LE, CH_UNIX,
+ ubuf, size,
+ (void *)&out_buffer,
+ True);
+
+ /* Don't need the intermediate buffer
+ * anymore.
+ */
+
+ TALLOC_FREE(ubuf);
+
+ if (size == (size_t)-1) {
+ return NULL;
+ }
+ }
+
+ return out_buffer;
+}
+
size_t unix_strlower(const char *src, size_t srclen, char *dest, size_t destlen)
{
size_t size;
Index: include/mangle.h
===================================================================
--- include/mangle.h (revision 24957)
+++ include/mangle.h (working copy)
@@ -9,10 +9,15 @@
BOOL (*is_mangled)(const char *s, const struct share_params *p);
BOOL (*is_8_3)(const char *fname, BOOL check_case, BOOL allow_wildcards,
const struct share_params *p);
- BOOL (*check_cache)(char *s, size_t maxlen,
- const struct share_params *p);
- void (*name_map)(char *OutName, BOOL need83, BOOL cache83,
- int default_case,
- const struct share_params *p);
+ BOOL (*check_cache)(TALLOC_CTX *ctx,
+ const char *in,
+ char **out, /* talloced on the given context. */
+ const struct share_params *p);
+ void (*name_map)(const char *in,
+ char out[13],
+ BOOL need83,
+ BOOL cache83,
+ int default_case,
+ const struct share_params *p);
};
#endif /* _MANGLE_H_ */
More information about the samba-technical
mailing list