2 Unix SMB/CIFS implementation.
3 filename handling routines
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 1999-2004
6 Copyright (C) Ying Chen 2000
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 * New hash table stat cache code added by Ying Chen.
28 static BOOL scan_directory(connection_struct *conn, const char *path, char *name,size_t maxlength);
30 /****************************************************************************
31 Check if two filenames are equal.
32 This needs to be careful about whether we are case sensitive.
33 ****************************************************************************/
35 static BOOL fname_equal(const char *name1, const char *name2, BOOL case_sensitive)
37 /* Normal filename handling */
39 return(strcmp(name1,name2) == 0);
41 return(strequal(name1,name2));
44 /****************************************************************************
45 Mangle the 2nd name and check if it is then equal to the first name.
46 ****************************************************************************/
48 static BOOL mangled_equal(const char *name1, const char *name2,
49 const struct share_params *p)
53 pstrcpy(tmpname, name2);
54 mangle_map(tmpname, True, False, p);
55 return strequal(name1, tmpname);
58 /****************************************************************************
59 Cope with the differing wildcard and non-wildcard error cases.
60 ****************************************************************************/
62 static NTSTATUS determine_path_error(const char *name, BOOL allow_wcard_last_component)
66 if (!allow_wcard_last_component) {
67 /* Error code within a pathname. */
68 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
71 /* We're terminating here so we
72 * can be a little slower and get
73 * the error code right. Windows
74 * treats the last part of the pathname
75 * separately I think, so if the last
76 * component is a wildcard then we treat
77 * this ./ as "end of component" */
79 p = strchr(name, '/');
81 if (!p && (ms_has_wild(name) || ISDOT(name))) {
82 /* Error code at the end of a pathname. */
83 return NT_STATUS_OBJECT_NAME_INVALID;
85 /* Error code within a pathname. */
86 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
90 /****************************************************************************
91 This routine is called to convert names from the dos namespace to unix
92 namespace. It needs to handle any case conversions, mangling, format
95 We assume that we have already done a chdir() to the right "root" directory
98 The function will return an NTSTATUS error if some part of the name except for the last
99 part cannot be resolved, else NT_STATUS_OK.
101 Note NT_STATUS_OK doesn't mean the name exists or is valid, just that we didn't
102 get any fatal errors that should immediately terminate the calling
103 SMB processing whilst resolving.
105 If the saved_last_component != 0, then the unmodified last component
106 of the pathname is returned there. This is used in an exceptional
107 case in reply_mv (so far). If saved_last_component == 0 then nothing
110 If last_component_wcard is true then a MS wildcard was detected and
111 should be allowed in the last component of the path only.
113 On exit from unix_convert, if *pst was not null, then the file stat
114 struct will be returned if the file exists and was found, if not this
115 stat struct will be filled with zeros (and this can be detected by checking
116 for nlinks = 0, which can never be true for any file).
117 ****************************************************************************/
119 NTSTATUS unix_convert(connection_struct *conn,
121 BOOL allow_wcard_last_component,
122 char *saved_last_component,
123 SMB_STRUCT_STAT *pst)
129 BOOL component_was_mangled = False;
130 BOOL name_has_wildcard = False;
132 SET_STAT_INVALID(*pst);
136 if(saved_last_component) {
137 *saved_last_component = 0;
141 /* we don't ever use the filenames on a printer share as a
142 filename - so don't convert them */
146 DEBUG(5, ("unix_convert called on file \"%s\"\n", name));
149 * Conversion to basic unix format is already done in check_path_syntax().
153 * Names must be relative to the root of the service - any leading /.
154 * and trailing /'s should have been trimmed by check_path_syntax().
158 SMB_ASSERT(*name != '/');
162 * If we trimmed down to a single '\0' character
163 * then we should use the "." directory to avoid
164 * searching the cache, but not if we are in a
166 * As we know this is valid we can return true here.
172 if (SMB_VFS_STAT(conn,name,&st) == 0) {
175 DEBUG(5,("conversion finished \"\" -> %s\n",name));
179 if (name[0] == '.' && (name[1] == '/' || name[1] == '\0')) {
180 /* Start of pathname can't be "." only. */
181 if (name[1] == '\0' || name[2] == '\0') {
182 return NT_STATUS_OBJECT_NAME_INVALID;
184 return determine_path_error(&name[2], allow_wcard_last_component);
189 * Ensure saved_last_component is valid even if file exists.
192 if(saved_last_component) {
193 end = strrchr_m(name, '/');
195 pstrcpy(saved_last_component, end + 1);
197 pstrcpy(saved_last_component, name);
202 * Large directory fix normalization. If we're case sensitive, and
203 * the case preserving parameters are set to "no", normalize the case of
204 * the incoming filename from the client WHETHER IT EXISTS OR NOT !
205 * This is in conflict with the current (3.0.20) man page, but is
206 * what people expect from the "large directory howto". I'll update
207 * the man page. Thanks to jht@samba.org for finding this. JRA.
210 if (conn->case_sensitive && !conn->case_preserve && !conn->short_case_preserve) {
211 strnorm(name, lp_defaultcase(SNUM(conn)));
215 pstrcpy(orig_path, name);
217 if(!conn->case_sensitive && stat_cache_lookup(conn, name, dirpath, &start, &st)) {
223 * stat the name - if it exists then we are all done!
226 if (SMB_VFS_STAT(conn,name,&st) == 0) {
227 /* Ensure we catch all names with in "/."
228 this is disallowed under Windows. */
229 const char *p = strstr(name, "/."); /* mb safe. */
232 /* Error code within a pathname. */
233 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
234 } else if (p[2] == '\0') {
235 /* Error code at the end of a pathname. */
236 return NT_STATUS_OBJECT_NAME_INVALID;
239 stat_cache_add(orig_path, name, conn->case_sensitive);
240 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
245 DEBUG(5,("unix_convert begin: name = %s, dirpath = %s, start = %s\n", name, dirpath, start));
248 * A special case - if we don't have any mangling chars and are case
249 * sensitive then searching won't help.
252 if (conn->case_sensitive &&
253 !mangle_is_mangled(name, conn->params) &&
254 !*lp_mangled_map(conn->params)) {
259 * is_mangled() was changed to look at an entire pathname, not
260 * just a component. JRA.
263 if (mangle_is_mangled(start, conn->params)) {
264 component_was_mangled = True;
268 * Now we need to recursively match the name against the real
269 * directory structure.
273 * Match each part of the path name separately, trying the names
274 * as is first, then trying to scan the directory for matching names.
277 for (; start ; start = (end?end+1:(char *)NULL)) {
279 * Pinpoint the end of this section of the filename.
281 end = strchr(start, '/'); /* mb safe. '/' can't be in any encoded char. */
284 * Chop the name at this point.
290 if (saved_last_component != 0) {
291 pstrcpy(saved_last_component, end ? end + 1 : start);
294 /* The name cannot have a component of "." */
298 /* Error code at the end of a pathname. */
299 return NT_STATUS_OBJECT_NAME_INVALID;
301 return determine_path_error(end+1, allow_wcard_last_component);
304 /* The name cannot have a wildcard if it's not
305 the last component. */
307 name_has_wildcard = ms_has_wild(start);
309 /* Wildcard not valid anywhere. */
310 if (name_has_wildcard && !allow_wcard_last_component) {
311 return NT_STATUS_OBJECT_NAME_INVALID;
314 /* Wildcards never valid within a pathname. */
315 if (name_has_wildcard && end) {
316 return NT_STATUS_OBJECT_NAME_INVALID;
320 * Check if the name exists up to this point.
323 if (SMB_VFS_STAT(conn,name, &st) == 0) {
325 * It exists. it must either be a directory or this must be
326 * the last part of the path for it to be OK.
328 if (end && !(st.st_mode & S_IFDIR)) {
330 * An intermediate part of the name isn't a directory.
332 DEBUG(5,("Not a dir %s\n",start));
335 * We need to return the fact that the intermediate
336 * name resolution failed. This is used to return an
337 * error of ERRbadpath rather than ERRbadfile. Some
338 * Windows applications depend on the difference between
341 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
346 * We just scanned for, and found the end of the path.
347 * We must return the valid stat struct.
357 /* Stat failed - ensure we don't use it. */
358 SET_STAT_INVALID(st);
362 * Remember the rest of the pathname so it can be restored
370 /* Reset errno so we can detect directory open errors. */
374 * Try to find this part of the path in the directory.
377 if (name_has_wildcard ||
378 !scan_directory(conn, dirpath, start, sizeof(pstring) - 1 - (start - name))) {
381 * An intermediate part of the name can't be found.
383 DEBUG(5,("Intermediate not found %s\n",start));
387 * We need to return the fact that the intermediate
388 * name resolution failed. This is used to return an
389 * error of ERRbadpath rather than ERRbadfile. Some
390 * Windows applications depend on the difference between
394 /* ENOENT and ENOTDIR both map to NT_STATUS_OBJECT_PATH_NOT_FOUND
395 in the filename walk. */
397 if (errno == ENOENT || errno == ENOTDIR) {
398 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
400 return map_nt_error_from_unix(errno);
403 /* ENOENT is the only valid error here. */
404 if (errno != ENOENT) {
405 /* ENOENT and ENOTDIR both map to NT_STATUS_OBJECT_PATH_NOT_FOUND
406 in the filename walk. */
407 if (errno == ENOTDIR) {
408 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
410 return map_nt_error_from_unix(errno);
414 * Just the last part of the name doesn't exist.
415 * We need to strupper() or strlower() it as
416 * this conversion may be used for file creation
417 * purposes. Fix inspired by Thomas Neumann <t.neumann@iku-ag.de>.
419 if (!conn->case_preserve ||
420 (mangle_is_8_3(start, False, conn->params) &&
421 !conn->short_case_preserve)) {
422 strnorm(start, lp_defaultcase(SNUM(conn)));
426 * check on the mangled stack to see if we can recover the
427 * base of the filename.
430 if (mangle_is_mangled(start, conn->params)) {
431 mangle_check_cache( start, sizeof(pstring) - 1 - (start - name), conn->params);
434 DEBUG(5,("New file %s\n",start));
439 * Restore the rest of the string. If the string was mangled the size
443 end = start + strlen(start);
444 if (!safe_strcat(start, "/", sizeof(pstring) - 1 - (start - name)) ||
445 !safe_strcat(start, rest, sizeof(pstring) - 1 - (start - name))) {
446 return map_nt_error_from_unix(ENAMETOOLONG);
451 * We just scanned for, and found the end of the path.
452 * We must return a valid stat struct if it exists.
456 if (SMB_VFS_STAT(conn,name, &st) == 0) {
459 SET_STAT_INVALID(st);
465 if (VALID_STAT(st) && get_delete_on_close_flag(file_id_sbuf(&st))) {
466 return NT_STATUS_DELETE_PENDING;
471 * Add to the dirpath that we have resolved so far.
474 pstrcat(dirpath,"/");
477 pstrcat(dirpath,start);
480 * Don't cache a name with mangled or wildcard components
481 * as this can change the size.
484 if(!component_was_mangled && !name_has_wildcard) {
485 stat_cache_add(orig_path, dirpath, conn->case_sensitive);
489 * Restore the / that we wiped out earlier.
497 * Don't cache a name with mangled or wildcard components
498 * as this can change the size.
501 if(!component_was_mangled && !name_has_wildcard) {
502 stat_cache_add(orig_path, name, conn->case_sensitive);
506 * The name has been resolved.
509 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
513 /****************************************************************************
514 Check a filename - possibly calling check_reduced_name.
515 This is called by every routine before it allows an operation on a filename.
516 It does any final confirmation necessary to ensure that the filename is
517 a valid one for the user to access.
518 ****************************************************************************/
520 NTSTATUS check_name(connection_struct *conn, const pstring name)
522 if (IS_VETO_PATH(conn, name)) {
523 /* Is it not dot or dot dot. */
524 if (!((name[0] == '.') && (!name[1] || (name[1] == '.' && !name[2])))) {
525 DEBUG(5,("check_name: file path name %s vetoed\n",name));
526 return map_nt_error_from_unix(ENOENT);
530 if (!lp_widelinks(SNUM(conn)) || !lp_symlinks(SNUM(conn))) {
531 NTSTATUS status = check_reduced_name(conn,name);
532 if (!NT_STATUS_IS_OK(status)) {
533 DEBUG(5,("check_name: name %s failed with %s\n",name, nt_errstr(status)));
541 /****************************************************************************
542 Scan a directory to find a filename, matching without case sensitivity.
543 If the name looks like a mangled name then try via the mangling functions
544 ****************************************************************************/
546 static BOOL scan_directory(connection_struct *conn, const char *path, char *name, size_t maxlength)
548 struct smb_Dir *cur_dir;
553 mangled = mangle_is_mangled(name, conn->params);
555 /* handle null paths */
560 * The incoming name can be mangled, and if we de-mangle it
561 * here it will not compare correctly against the filename (name2)
562 * read from the directory and then mangled by the mangle_map()
563 * call. We need to mangle both names or neither.
566 * Fix for bug found by Dina Fine. If in case sensitive mode then
567 * the mangle cache is no good (3 letter extension could be wrong
568 * case - so don't demangle in this case - leave as mangled and
569 * allow the mangling of the directory entry read (which is done
570 * case insensitively) to match instead. This will lead to more
571 * false positive matches but we fail completely without it. JRA.
574 if (mangled && !conn->case_sensitive) {
575 mangled = !mangle_check_cache( name, maxlength, conn->params);
578 /* open the directory */
579 if (!(cur_dir = OpenDir(conn, path, NULL, 0))) {
580 DEBUG(3,("scan dir didn't open dir [%s]\n",path));
584 /* now scan for matching names */
586 while ((dname = ReadDirName(cur_dir, &curpos))) {
588 /* Is it dot or dot dot. */
589 if ((dname[0] == '.') && (!dname[1] || (dname[1] == '.' && !dname[2]))) {
594 * At this point dname is the unmangled name.
595 * name is either mangled or not, depending on the state of the "mangled"
600 * Check mangled name against mangled name, or unmangled name
601 * against unmangled name.
604 if ((mangled && mangled_equal(name,dname,conn->params)) || fname_equal(name, dname, conn->case_sensitive)) {
605 /* we've found the file, change it's name and return */
606 safe_strcpy(name, dname, maxlength);