[GLUE] Rsync SAMBA_3_0 SVN r25598 in order to create the v3-0-test branch.
[Samba.git] / source / smbd / filename.c
blob570297bf69fea1e1c0d077e0cd2f7962ec3b94ab
1 /*
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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * New hash table stat cache code added by Ying Chen.
27 #include "includes.h"
29 static BOOL scan_directory(connection_struct *conn, const char *path, char *name,size_t maxlength);
31 /****************************************************************************
32 Check if two filenames are equal.
33 This needs to be careful about whether we are case sensitive.
34 ****************************************************************************/
36 static BOOL fname_equal(const char *name1, const char *name2, BOOL case_sensitive)
38 /* Normal filename handling */
39 if (case_sensitive)
40 return(strcmp(name1,name2) == 0);
42 return(strequal(name1,name2));
45 /****************************************************************************
46 Mangle the 2nd name and check if it is then equal to the first name.
47 ****************************************************************************/
49 static BOOL mangled_equal(const char *name1, const char *name2,
50 const struct share_params *p)
52 pstring tmpname;
54 pstrcpy(tmpname, name2);
55 mangle_map(tmpname, True, False, p);
56 return strequal(name1, tmpname);
59 /****************************************************************************
60 Cope with the differing wildcard and non-wildcard error cases.
61 ****************************************************************************/
63 static NTSTATUS determine_path_error(const char *name, BOOL allow_wcard_last_component)
65 const char *p;
67 if (!allow_wcard_last_component) {
68 /* Error code within a pathname. */
69 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
72 /* We're terminating here so we
73 * can be a little slower and get
74 * the error code right. Windows
75 * treats the last part of the pathname
76 * separately I think, so if the last
77 * component is a wildcard then we treat
78 * this ./ as "end of component" */
80 p = strchr(name, '/');
82 if (!p && (ms_has_wild(name) || ISDOT(name))) {
83 /* Error code at the end of a pathname. */
84 return NT_STATUS_OBJECT_NAME_INVALID;
85 } else {
86 /* Error code within a pathname. */
87 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
91 /****************************************************************************
92 This routine is called to convert names from the dos namespace to unix
93 namespace. It needs to handle any case conversions, mangling, format
94 changes etc.
96 We assume that we have already done a chdir() to the right "root" directory
97 for this service.
99 The function will return an NTSTATUS error if some part of the name except for the last
100 part cannot be resolved, else NT_STATUS_OK.
102 Note NT_STATUS_OK doesn't mean the name exists or is valid, just that we didn't
103 get any fatal errors that should immediately terminate the calling
104 SMB processing whilst resolving.
106 If the saved_last_component != 0, then the unmodified last component
107 of the pathname is returned there. This is used in an exceptional
108 case in reply_mv (so far). If saved_last_component == 0 then nothing
109 is returned there.
111 If last_component_wcard is true then a MS wildcard was detected and
112 should be allowed in the last component of the path only.
114 On exit from unix_convert, if *pst was not null, then the file stat
115 struct will be returned if the file exists and was found, if not this
116 stat struct will be filled with zeros (and this can be detected by checking
117 for nlinks = 0, which can never be true for any file).
118 ****************************************************************************/
120 NTSTATUS unix_convert(connection_struct *conn,
121 pstring name,
122 BOOL allow_wcard_last_component,
123 char *saved_last_component,
124 SMB_STRUCT_STAT *pst)
126 SMB_STRUCT_STAT st;
127 char *start, *end;
128 pstring dirpath;
129 pstring orig_path;
130 BOOL component_was_mangled = False;
131 BOOL name_has_wildcard = False;
133 SET_STAT_INVALID(*pst);
135 *dirpath = 0;
137 if(saved_last_component) {
138 *saved_last_component = 0;
141 if (conn->printer) {
142 /* we don't ever use the filenames on a printer share as a
143 filename - so don't convert them */
144 return NT_STATUS_OK;
147 DEBUG(5, ("unix_convert called on file \"%s\"\n", name));
150 * Conversion to basic unix format is already done in check_path_syntax().
154 * Names must be relative to the root of the service - any leading /.
155 * and trailing /'s should have been trimmed by check_path_syntax().
158 #ifdef DEVELOPER
159 SMB_ASSERT(*name != '/');
160 #endif
163 * If we trimmed down to a single '\0' character
164 * then we should use the "." directory to avoid
165 * searching the cache, but not if we are in a
166 * printing share.
167 * As we know this is valid we can return true here.
170 if (!*name) {
171 name[0] = '.';
172 name[1] = '\0';
173 if (SMB_VFS_STAT(conn,name,&st) == 0) {
174 *pst = st;
176 DEBUG(5,("conversion finished \"\" -> %s\n",name));
177 return NT_STATUS_OK;
180 if (name[0] == '.' && (name[1] == '/' || name[1] == '\0')) {
181 /* Start of pathname can't be "." only. */
182 if (name[1] == '\0' || name[2] == '\0') {
183 return NT_STATUS_OBJECT_NAME_INVALID;
184 } else {
185 return determine_path_error(&name[2], allow_wcard_last_component);
190 * Ensure saved_last_component is valid even if file exists.
193 if(saved_last_component) {
194 end = strrchr_m(name, '/');
195 if (end) {
196 pstrcpy(saved_last_component, end + 1);
197 } else {
198 pstrcpy(saved_last_component, name);
203 * Large directory fix normalization. If we're case sensitive, and
204 * the case preserving parameters are set to "no", normalize the case of
205 * the incoming filename from the client WHETHER IT EXISTS OR NOT !
206 * This is in conflict with the current (3.0.20) man page, but is
207 * what people expect from the "large directory howto". I'll update
208 * the man page. Thanks to jht@samba.org for finding this. JRA.
211 if (conn->case_sensitive && !conn->case_preserve && !conn->short_case_preserve) {
212 strnorm(name, lp_defaultcase(SNUM(conn)));
215 start = name;
216 pstrcpy(orig_path, name);
218 if(!conn->case_sensitive && stat_cache_lookup(conn, name, dirpath, &start, &st)) {
219 *pst = st;
220 return NT_STATUS_OK;
224 * stat the name - if it exists then we are all done!
227 if (SMB_VFS_STAT(conn,name,&st) == 0) {
228 /* Ensure we catch all names with in "/."
229 this is disallowed under Windows. */
230 const char *p = strstr(name, "/."); /* mb safe. */
231 if (p) {
232 if (p[2] == '/') {
233 /* Error code within a pathname. */
234 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
235 } else if (p[2] == '\0') {
236 /* Error code at the end of a pathname. */
237 return NT_STATUS_OBJECT_NAME_INVALID;
240 stat_cache_add(orig_path, name, conn->case_sensitive);
241 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
242 *pst = st;
243 return NT_STATUS_OK;
246 DEBUG(5,("unix_convert begin: name = %s, dirpath = %s, start = %s\n", name, dirpath, start));
249 * A special case - if we don't have any mangling chars and are case
250 * sensitive then searching won't help.
253 if (conn->case_sensitive &&
254 !mangle_is_mangled(name, conn->params) &&
255 !*lp_mangled_map(conn->params)) {
256 return NT_STATUS_OK;
260 * is_mangled() was changed to look at an entire pathname, not
261 * just a component. JRA.
264 if (mangle_is_mangled(start, conn->params)) {
265 component_was_mangled = True;
269 * Now we need to recursively match the name against the real
270 * directory structure.
274 * Match each part of the path name separately, trying the names
275 * as is first, then trying to scan the directory for matching names.
278 for (; start ; start = (end?end+1:(char *)NULL)) {
280 * Pinpoint the end of this section of the filename.
282 end = strchr(start, '/'); /* mb safe. '/' can't be in any encoded char. */
285 * Chop the name at this point.
287 if (end) {
288 *end = 0;
291 if (saved_last_component != 0) {
292 pstrcpy(saved_last_component, end ? end + 1 : start);
295 /* The name cannot have a component of "." */
297 if (ISDOT(start)) {
298 if (!end) {
299 /* Error code at the end of a pathname. */
300 return NT_STATUS_OBJECT_NAME_INVALID;
302 return determine_path_error(end+1, allow_wcard_last_component);
305 /* The name cannot have a wildcard if it's not
306 the last component. */
308 name_has_wildcard = ms_has_wild(start);
310 /* Wildcard not valid anywhere. */
311 if (name_has_wildcard && !allow_wcard_last_component) {
312 return NT_STATUS_OBJECT_NAME_INVALID;
315 /* Wildcards never valid within a pathname. */
316 if (name_has_wildcard && end) {
317 return NT_STATUS_OBJECT_NAME_INVALID;
321 * Check if the name exists up to this point.
324 if (SMB_VFS_STAT(conn,name, &st) == 0) {
326 * It exists. it must either be a directory or this must be
327 * the last part of the path for it to be OK.
329 if (end && !(st.st_mode & S_IFDIR)) {
331 * An intermediate part of the name isn't a directory.
333 DEBUG(5,("Not a dir %s\n",start));
334 *end = '/';
336 * We need to return the fact that the intermediate
337 * name resolution failed. This is used to return an
338 * error of ERRbadpath rather than ERRbadfile. Some
339 * Windows applications depend on the difference between
340 * these two errors.
342 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
345 if (!end) {
347 * We just scanned for, and found the end of the path.
348 * We must return the valid stat struct.
349 * JRA.
352 *pst = st;
355 } else {
356 pstring rest;
358 /* Stat failed - ensure we don't use it. */
359 SET_STAT_INVALID(st);
360 *rest = 0;
363 * Remember the rest of the pathname so it can be restored
364 * later.
367 if (end) {
368 pstrcpy(rest,end+1);
371 /* Reset errno so we can detect directory open errors. */
372 errno = 0;
375 * Try to find this part of the path in the directory.
378 if (name_has_wildcard ||
379 !scan_directory(conn, dirpath, start, sizeof(pstring) - 1 - (start - name))) {
380 if (end) {
382 * An intermediate part of the name can't be found.
384 DEBUG(5,("Intermediate not found %s\n",start));
385 *end = '/';
388 * We need to return the fact that the intermediate
389 * name resolution failed. This is used to return an
390 * error of ERRbadpath rather than ERRbadfile. Some
391 * Windows applications depend on the difference between
392 * these two errors.
395 /* ENOENT, ENOTDIR and ELOOP all map to
396 * NT_STATUS_OBJECT_PATH_NOT_FOUND
397 * in the filename walk. */
399 if (errno == ENOENT ||
400 errno == ENOTDIR ||
401 errno == ELOOP) {
402 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
404 return map_nt_error_from_unix(errno);
407 /* ENOENT is the only valid error here. */
408 if (errno != ENOENT) {
409 /* ENOTDIR and ELOOP both map to
410 * NT_STATUS_OBJECT_PATH_NOT_FOUND
411 * in the filename walk. */
412 if (errno == ENOTDIR ||
413 errno == ELOOP) {
414 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
416 return map_nt_error_from_unix(errno);
420 * Just the last part of the name doesn't exist.
421 * We need to strupper() or strlower() it as
422 * this conversion may be used for file creation
423 * purposes. Fix inspired by Thomas Neumann <t.neumann@iku-ag.de>.
425 if (!conn->case_preserve ||
426 (mangle_is_8_3(start, False, conn->params) &&
427 !conn->short_case_preserve)) {
428 strnorm(start, lp_defaultcase(SNUM(conn)));
432 * check on the mangled stack to see if we can recover the
433 * base of the filename.
436 if (mangle_is_mangled(start, conn->params)) {
437 mangle_check_cache( start, sizeof(pstring) - 1 - (start - name), conn->params);
440 DEBUG(5,("New file %s\n",start));
441 return NT_STATUS_OK;
445 * Restore the rest of the string. If the string was mangled the size
446 * may have changed.
448 if (end) {
449 end = start + strlen(start);
450 if (!safe_strcat(start, "/", sizeof(pstring) - 1 - (start - name)) ||
451 !safe_strcat(start, rest, sizeof(pstring) - 1 - (start - name))) {
452 return map_nt_error_from_unix(ENAMETOOLONG);
454 *end = '\0';
455 } else {
457 * We just scanned for, and found the end of the path.
458 * We must return a valid stat struct if it exists.
459 * JRA.
462 if (SMB_VFS_STAT(conn,name, &st) == 0) {
463 *pst = st;
464 } else {
465 SET_STAT_INVALID(st);
468 } /* end else */
470 #ifdef DEVELOPER
471 if (VALID_STAT(st) && get_delete_on_close_flag(st.st_dev, st.st_ino)) {
472 return NT_STATUS_DELETE_PENDING;
474 #endif
477 * Add to the dirpath that we have resolved so far.
479 if (*dirpath) {
480 pstrcat(dirpath,"/");
483 pstrcat(dirpath,start);
486 * Don't cache a name with mangled or wildcard components
487 * as this can change the size.
490 if(!component_was_mangled && !name_has_wildcard) {
491 stat_cache_add(orig_path, dirpath, conn->case_sensitive);
495 * Restore the / that we wiped out earlier.
497 if (end) {
498 *end = '/';
503 * Don't cache a name with mangled or wildcard components
504 * as this can change the size.
507 if(!component_was_mangled && !name_has_wildcard) {
508 stat_cache_add(orig_path, name, conn->case_sensitive);
512 * The name has been resolved.
515 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
516 return NT_STATUS_OK;
519 /****************************************************************************
520 Check a filename - possibly caling reducename.
521 This is called by every routine before it allows an operation on a filename.
522 It does any final confirmation necessary to ensure that the filename is
523 a valid one for the user to access.
524 ****************************************************************************/
526 NTSTATUS check_name(connection_struct *conn, const pstring name)
528 if (IS_VETO_PATH(conn, name)) {
529 /* Is it not dot or dot dot. */
530 if (!((name[0] == '.') && (!name[1] || (name[1] == '.' && !name[2])))) {
531 DEBUG(5,("check_name: file path name %s vetoed\n",name));
532 return map_nt_error_from_unix(ENOENT);
536 if (!lp_widelinks(SNUM(conn)) || !lp_symlinks(SNUM(conn))) {
537 NTSTATUS status = reduce_name(conn,name);
538 if (!NT_STATUS_IS_OK(status)) {
539 DEBUG(5,("check_name: name %s failed with %s\n",name, nt_errstr(status)));
540 return status;
544 return NT_STATUS_OK;
547 /****************************************************************************
548 Scan a directory to find a filename, matching without case sensitivity.
549 If the name looks like a mangled name then try via the mangling functions
550 ****************************************************************************/
552 static BOOL scan_directory(connection_struct *conn, const char *path, char *name, size_t maxlength)
554 struct smb_Dir *cur_dir;
555 const char *dname;
556 BOOL mangled;
557 long curpos;
559 mangled = mangle_is_mangled(name, conn->params);
561 /* handle null paths */
562 if (*path == 0)
563 path = ".";
566 * The incoming name can be mangled, and if we de-mangle it
567 * here it will not compare correctly against the filename (name2)
568 * read from the directory and then mangled by the mangle_map()
569 * call. We need to mangle both names or neither.
570 * (JRA).
572 * Fix for bug found by Dina Fine. If in case sensitive mode then
573 * the mangle cache is no good (3 letter extension could be wrong
574 * case - so don't demangle in this case - leave as mangled and
575 * allow the mangling of the directory entry read (which is done
576 * case insensitively) to match instead. This will lead to more
577 * false positive matches but we fail completely without it. JRA.
580 if (mangled && !conn->case_sensitive) {
581 mangled = !mangle_check_cache( name, maxlength, conn->params);
584 /* open the directory */
585 if (!(cur_dir = OpenDir(conn, path, NULL, 0))) {
586 DEBUG(3,("scan dir didn't open dir [%s]\n",path));
587 return(False);
590 /* now scan for matching names */
591 curpos = 0;
592 while ((dname = ReadDirName(cur_dir, &curpos))) {
594 /* Is it dot or dot dot. */
595 if ((dname[0] == '.') && (!dname[1] || (dname[1] == '.' && !dname[2]))) {
596 continue;
600 * At this point dname is the unmangled name.
601 * name is either mangled or not, depending on the state of the "mangled"
602 * variable. JRA.
606 * Check mangled name against mangled name, or unmangled name
607 * against unmangled name.
610 if ((mangled && mangled_equal(name,dname,conn->params)) || fname_equal(name, dname, conn->case_sensitive)) {
611 /* we've found the file, change it's name and return */
612 safe_strcpy(name, dname, maxlength);
613 CloseDir(cur_dir);
614 return(True);
618 CloseDir(cur_dir);
619 errno = ENOENT;
620 return(False);