r23761: Rename reduce_name to check_reduced_name
[Samba.git] / source / smbd / filename.c
blobde15d923b8952db3ff6202f24ed8837164a10e25
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 and ENOTDIR both map to NT_STATUS_OBJECT_PATH_NOT_FOUND
396 in the filename walk. */
398 if (errno == ENOENT || errno == ENOTDIR) {
399 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
401 return map_nt_error_from_unix(errno);
404 /* ENOENT is the only valid error here. */
405 if (errno != ENOENT) {
406 /* ENOENT and ENOTDIR both map to NT_STATUS_OBJECT_PATH_NOT_FOUND
407 in the filename walk. */
408 if (errno == ENOTDIR) {
409 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
411 return map_nt_error_from_unix(errno);
415 * Just the last part of the name doesn't exist.
416 * We need to strupper() or strlower() it as
417 * this conversion may be used for file creation
418 * purposes. Fix inspired by Thomas Neumann <t.neumann@iku-ag.de>.
420 if (!conn->case_preserve ||
421 (mangle_is_8_3(start, False, conn->params) &&
422 !conn->short_case_preserve)) {
423 strnorm(start, lp_defaultcase(SNUM(conn)));
427 * check on the mangled stack to see if we can recover the
428 * base of the filename.
431 if (mangle_is_mangled(start, conn->params)) {
432 mangle_check_cache( start, sizeof(pstring) - 1 - (start - name), conn->params);
435 DEBUG(5,("New file %s\n",start));
436 return NT_STATUS_OK;
440 * Restore the rest of the string. If the string was mangled the size
441 * may have changed.
443 if (end) {
444 end = start + strlen(start);
445 if (!safe_strcat(start, "/", sizeof(pstring) - 1 - (start - name)) ||
446 !safe_strcat(start, rest, sizeof(pstring) - 1 - (start - name))) {
447 return map_nt_error_from_unix(ENAMETOOLONG);
449 *end = '\0';
450 } else {
452 * We just scanned for, and found the end of the path.
453 * We must return a valid stat struct if it exists.
454 * JRA.
457 if (SMB_VFS_STAT(conn,name, &st) == 0) {
458 *pst = st;
459 } else {
460 SET_STAT_INVALID(st);
463 } /* end else */
465 #ifdef DEVELOPER
466 if (VALID_STAT(st) && get_delete_on_close_flag(file_id_sbuf(&st))) {
467 return NT_STATUS_DELETE_PENDING;
469 #endif
472 * Add to the dirpath that we have resolved so far.
474 if (*dirpath) {
475 pstrcat(dirpath,"/");
478 pstrcat(dirpath,start);
481 * Don't cache a name with mangled or wildcard components
482 * as this can change the size.
485 if(!component_was_mangled && !name_has_wildcard) {
486 stat_cache_add(orig_path, dirpath, conn->case_sensitive);
490 * Restore the / that we wiped out earlier.
492 if (end) {
493 *end = '/';
498 * Don't cache a name with mangled or wildcard components
499 * as this can change the size.
502 if(!component_was_mangled && !name_has_wildcard) {
503 stat_cache_add(orig_path, name, conn->case_sensitive);
507 * The name has been resolved.
510 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
511 return NT_STATUS_OK;
514 /****************************************************************************
515 Check a filename - possibly calling check_reduced_name.
516 This is called by every routine before it allows an operation on a filename.
517 It does any final confirmation necessary to ensure that the filename is
518 a valid one for the user to access.
519 ****************************************************************************/
521 NTSTATUS check_name(connection_struct *conn, const pstring name)
523 if (IS_VETO_PATH(conn, name)) {
524 /* Is it not dot or dot dot. */
525 if (!((name[0] == '.') && (!name[1] || (name[1] == '.' && !name[2])))) {
526 DEBUG(5,("check_name: file path name %s vetoed\n",name));
527 return map_nt_error_from_unix(ENOENT);
531 if (!lp_widelinks(SNUM(conn)) || !lp_symlinks(SNUM(conn))) {
532 NTSTATUS status = check_reduced_name(conn,name);
533 if (!NT_STATUS_IS_OK(status)) {
534 DEBUG(5,("check_name: name %s failed with %s\n",name, nt_errstr(status)));
535 return status;
539 return NT_STATUS_OK;
542 /****************************************************************************
543 Scan a directory to find a filename, matching without case sensitivity.
544 If the name looks like a mangled name then try via the mangling functions
545 ****************************************************************************/
547 static BOOL scan_directory(connection_struct *conn, const char *path, char *name, size_t maxlength)
549 struct smb_Dir *cur_dir;
550 const char *dname;
551 BOOL mangled;
552 long curpos;
554 mangled = mangle_is_mangled(name, conn->params);
556 /* handle null paths */
557 if (*path == 0)
558 path = ".";
561 * The incoming name can be mangled, and if we de-mangle it
562 * here it will not compare correctly against the filename (name2)
563 * read from the directory and then mangled by the mangle_map()
564 * call. We need to mangle both names or neither.
565 * (JRA).
567 * Fix for bug found by Dina Fine. If in case sensitive mode then
568 * the mangle cache is no good (3 letter extension could be wrong
569 * case - so don't demangle in this case - leave as mangled and
570 * allow the mangling of the directory entry read (which is done
571 * case insensitively) to match instead. This will lead to more
572 * false positive matches but we fail completely without it. JRA.
575 if (mangled && !conn->case_sensitive) {
576 mangled = !mangle_check_cache( name, maxlength, conn->params);
579 /* open the directory */
580 if (!(cur_dir = OpenDir(conn, path, NULL, 0))) {
581 DEBUG(3,("scan dir didn't open dir [%s]\n",path));
582 return(False);
585 /* now scan for matching names */
586 curpos = 0;
587 while ((dname = ReadDirName(cur_dir, &curpos))) {
589 /* Is it dot or dot dot. */
590 if ((dname[0] == '.') && (!dname[1] || (dname[1] == '.' && !dname[2]))) {
591 continue;
595 * At this point dname is the unmangled name.
596 * name is either mangled or not, depending on the state of the "mangled"
597 * variable. JRA.
601 * Check mangled name against mangled name, or unmangled name
602 * against unmangled name.
605 if ((mangled && mangled_equal(name,dname,conn->params)) || fname_equal(name, dname, conn->case_sensitive)) {
606 /* we've found the file, change it's name and return */
607 safe_strcpy(name, dname, maxlength);
608 CloseDir(cur_dir);
609 return(True);
613 CloseDir(cur_dir);
614 errno = ENOENT;
615 return(False);