r20722: RAW-CHKPATH should now pass, build farm should
[tprouty/samba.git] / source3 / smbd / filename.c
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
7    
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.
12    
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.
17    
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.
21 */
22
23 /*
24  * New hash table stat cache code added by Ying Chen.
25  */
26
27 #include "includes.h"
28
29 static BOOL scan_directory(connection_struct *conn, const char *path, char *name,size_t maxlength);
30
31 /****************************************************************************
32  Check if two filenames are equal.
33  This needs to be careful about whether we are case sensitive.
34 ****************************************************************************/
35
36 static BOOL fname_equal(const char *name1, const char *name2, BOOL case_sensitive)
37 {
38         /* Normal filename handling */
39         if (case_sensitive)
40                 return(strcmp(name1,name2) == 0);
41
42         return(strequal(name1,name2));
43 }
44
45 /****************************************************************************
46  Mangle the 2nd name and check if it is then equal to the first name.
47 ****************************************************************************/
48
49 static BOOL mangled_equal(const char *name1, const char *name2,
50                           const struct share_params *p)
51 {
52         pstring tmpname;
53         
54         pstrcpy(tmpname, name2);
55         mangle_map(tmpname, True, False, p);
56         return strequal(name1, tmpname);
57 }
58
59 /****************************************************************************
60  Cope with the differing wildcard and non-wildcard error cases.
61 ****************************************************************************/
62
63 static NTSTATUS determine_path_error(const char *name, BOOL allow_wcard_last_component)
64 {
65         const char *p;
66
67         if (!allow_wcard_last_component) {
68                 /* Error code within a pathname. */
69                 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
70         }
71
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" */
79
80         p = strchr(name, '/');
81
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;
88         }
89 }
90         
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.
95
96 We assume that we have already done a chdir() to the right "root" directory
97 for this service.
98
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.
101
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.
105
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.
110
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.
113
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 ****************************************************************************/
119
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)
125 {
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;
132
133         SET_STAT_INVALID(*pst);
134
135         *dirpath = 0;
136
137         if(saved_last_component) {
138                 *saved_last_component = 0;
139         }
140
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;
145         }
146
147         DEBUG(5, ("unix_convert called on file \"%s\"\n", name));
148
149         /* 
150          * Conversion to basic unix format is already done in check_path_syntax().
151          */
152
153         /* 
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().
156          */
157
158 #ifdef DEVELOPER
159         SMB_ASSERT(*name != '/');
160 #endif
161
162         /*
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.
168          */
169
170         if (!*name) {
171                 name[0] = '.';
172                 name[1] = '\0';
173                 if (SMB_VFS_STAT(conn,name,&st) == 0) {
174                         *pst = st;
175                 }
176                 DEBUG(5,("conversion finished \"\" -> %s\n",name));
177                 return NT_STATUS_OK;
178         }
179
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);
186                 }
187         }
188
189         /*
190          * Ensure saved_last_component is valid even if file exists.
191          */
192
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);
199                 }
200         }
201
202         /*
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.
209          */
210
211         if (conn->case_sensitive && !conn->case_preserve && !conn->short_case_preserve) {
212                 strnorm(name, lp_defaultcase(SNUM(conn)));
213         }
214         
215         start = name;
216         pstrcpy(orig_path, name);
217
218         if(!conn->case_sensitive && stat_cache_lookup(conn, name, dirpath, &start, &st)) {
219                 *pst = st;
220                 return NT_STATUS_OK;
221         }
222
223         /* 
224          * stat the name - if it exists then we are all done!
225          */
226
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;
238                         }
239                 }
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;
244         }
245
246         DEBUG(5,("unix_convert begin: name = %s, dirpath = %s, start = %s\n", name, dirpath, start));
247
248         /* 
249          * A special case - if we don't have any mangling chars and are case
250          * sensitive then searching won't help.
251          */
252
253         if (conn->case_sensitive && 
254                         !mangle_is_mangled(name, conn->params) &&
255                         !*lp_mangled_map(conn->params)) {
256                 return NT_STATUS_OK;
257         }
258
259         /* 
260          * is_mangled() was changed to look at an entire pathname, not 
261          * just a component. JRA.
262          */
263
264         if (mangle_is_mangled(start, conn->params)) {
265                 component_was_mangled = True;
266         }
267
268         /* 
269          * Now we need to recursively match the name against the real 
270          * directory structure.
271          */
272
273         /* 
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.
276          */
277
278         for (; start ; start = (end?end+1:(char *)NULL)) {
279                 /* 
280                  * Pinpoint the end of this section of the filename.
281                  */
282                 end = strchr(start, '/'); /* mb safe. '/' can't be in any encoded char. */
283
284                 /* 
285                  * Chop the name at this point.
286                  */
287                 if (end) {
288                         *end = 0;
289                 }
290
291                 if (saved_last_component != 0) {
292                         pstrcpy(saved_last_component, end ? end + 1 : start);
293                 }
294
295                 /* The name cannot have a component of "." */
296
297                 if (ISDOT(start)) {
298                         if (!end)  {
299                                 /* Error code at the end of a pathname. */
300                                 return NT_STATUS_OBJECT_NAME_INVALID;
301                         }
302                         return determine_path_error(end+1, allow_wcard_last_component);
303                 }
304
305                 /* The name cannot have a wildcard if it's not
306                    the last component. */
307
308                 name_has_wildcard = ms_has_wild(start);
309
310                 /* Wildcard not valid anywhere. */
311                 if (name_has_wildcard && !allow_wcard_last_component) {
312                         return NT_STATUS_OBJECT_NAME_INVALID;
313                 }
314
315                 /* Wildcards never valid within a pathname. */
316                 if (name_has_wildcard && end) {
317                         return NT_STATUS_OBJECT_NAME_INVALID;
318                 }
319
320                 /* 
321                  * Check if the name exists up to this point.
322                  */
323
324                 if (SMB_VFS_STAT(conn,name, &st) == 0) {
325                         /*
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.
328                          */
329                         if (end && !(st.st_mode & S_IFDIR)) {
330                                 /*
331                                  * An intermediate part of the name isn't a directory.
332                                  */
333                                 DEBUG(5,("Not a dir %s\n",start));
334                                 *end = '/';
335                                 /* 
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.
341                                  */
342                                 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
343                         }
344
345                         if (!end) {
346                                 /*
347                                  * We just scanned for, and found the end of the path.
348                                  * We must return the valid stat struct.
349                                  * JRA.
350                                  */
351
352                                 *pst = st;
353                         }
354
355                 } else {
356                         pstring rest;
357
358                         /* Stat failed - ensure we don't use it. */
359                         SET_STAT_INVALID(st);
360                         *rest = 0;
361
362                         /*
363                          * Remember the rest of the pathname so it can be restored
364                          * later.
365                          */
366
367                         if (end) {
368                                 pstrcpy(rest,end+1);
369                         }
370
371                         /* Reset errno so we can detect directory open errors. */
372                         errno = 0;
373
374                         /*
375                          * Try to find this part of the path in the directory.
376                          */
377
378                         if (name_has_wildcard || 
379                             !scan_directory(conn, dirpath, start, sizeof(pstring) - 1 - (start - name))) {
380                                 if (end) {
381                                         /*
382                                          * An intermediate part of the name can't be found.
383                                          */
384                                         DEBUG(5,("Intermediate not found %s\n",start));
385                                         *end = '/';
386
387                                         /* 
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.
393                                          */
394
395                                         /* ENOENT and ENOTDIR both map to NT_STATUS_OBJECT_PATH_NOT_FOUND
396                                            in the filename walk. */
397
398                                         if (errno == ENOENT || errno == ENOTDIR) {
399                                                 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
400                                         }
401                                         return map_nt_error_from_unix(errno);
402                                 }
403               
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;
410                                         }
411                                         return map_nt_error_from_unix(errno);
412                                 }
413
414                                 /*
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>.
419                                  */
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)));
424                                 }
425
426                                 /*
427                                  * check on the mangled stack to see if we can recover the 
428                                  * base of the filename.
429                                  */
430
431                                 if (mangle_is_mangled(start, conn->params)) {
432                                         mangle_check_cache( start, sizeof(pstring) - 1 - (start - name), conn->params);
433                                 }
434
435                                 DEBUG(5,("New file %s\n",start));
436                                 return NT_STATUS_OK;
437                         }
438
439                         /* 
440                          * Restore the rest of the string. If the string was mangled the size
441                          * may have changed.
442                          */
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);
448                                 }
449                                 *end = '\0';
450                         } else {
451                                 /*
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.
455                                  */
456
457                                 if (SMB_VFS_STAT(conn,name, &st) == 0) {
458                                         *pst = st;
459                                 } else {
460                                         SET_STAT_INVALID(st);
461                                 }
462                         }
463                 } /* end else */
464
465                 /* 
466                  * Add to the dirpath that we have resolved so far.
467                  */
468                 if (*dirpath) {
469                         pstrcat(dirpath,"/");
470                 }
471
472                 pstrcat(dirpath,start);
473
474                 /*
475                  * Don't cache a name with mangled or wildcard components
476                  * as this can change the size.
477                  */
478                 
479                 if(!component_was_mangled && !name_has_wildcard) {
480                         stat_cache_add(orig_path, dirpath, conn->case_sensitive);
481                 }
482         
483                 /* 
484                  * Restore the / that we wiped out earlier.
485                  */
486                 if (end) {
487                         *end = '/';
488                 }
489         }
490   
491         /*
492          * Don't cache a name with mangled or wildcard components
493          * as this can change the size.
494          */
495
496         if(!component_was_mangled && !name_has_wildcard) {
497                 stat_cache_add(orig_path, name, conn->case_sensitive);
498         }
499
500         /* 
501          * The name has been resolved.
502          */
503
504         DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
505         return NT_STATUS_OK;
506 }
507
508 /****************************************************************************
509  Check a filename - possibly caling reducename.
510  This is called by every routine before it allows an operation on a filename.
511  It does any final confirmation necessary to ensure that the filename is
512  a valid one for the user to access.
513 ****************************************************************************/
514
515 BOOL check_name(const pstring name,connection_struct *conn)
516 {
517         BOOL ret = True;
518
519         if (IS_VETO_PATH(conn, name))  {
520                 /* Is it not dot or dot dot. */
521                 if (!((name[0] == '.') && (!name[1] || (name[1] == '.' && !name[2])))) {
522                         DEBUG(5,("file path name %s vetoed\n",name));
523                         errno = ENOENT;
524                         return False;
525                 }
526         }
527
528         if (!lp_widelinks(SNUM(conn)) || !lp_symlinks(SNUM(conn))) {
529                 ret = reduce_name(conn,name);
530         }
531
532         if (!ret) {
533                 DEBUG(5,("check_name on %s failed\n",name));
534         }
535
536         return(ret);
537 }
538
539 /****************************************************************************
540  Scan a directory to find a filename, matching without case sensitivity.
541  If the name looks like a mangled name then try via the mangling functions
542 ****************************************************************************/
543
544 static BOOL scan_directory(connection_struct *conn, const char *path, char *name, size_t maxlength)
545 {
546         struct smb_Dir *cur_dir;
547         const char *dname;
548         BOOL mangled;
549         long curpos;
550
551         mangled = mangle_is_mangled(name, conn->params);
552
553         /* handle null paths */
554         if (*path == 0)
555                 path = ".";
556
557         /*
558          * The incoming name can be mangled, and if we de-mangle it
559          * here it will not compare correctly against the filename (name2)
560          * read from the directory and then mangled by the mangle_map()
561          * call. We need to mangle both names or neither.
562          * (JRA).
563          *
564          * Fix for bug found by Dina Fine. If in case sensitive mode then
565          * the mangle cache is no good (3 letter extension could be wrong
566          * case - so don't demangle in this case - leave as mangled and
567          * allow the mangling of the directory entry read (which is done
568          * case insensitively) to match instead. This will lead to more
569          * false positive matches but we fail completely without it. JRA.
570          */
571
572         if (mangled && !conn->case_sensitive) {
573                 mangled = !mangle_check_cache( name, maxlength, conn->params);
574         }
575
576         /* open the directory */
577         if (!(cur_dir = OpenDir(conn, path, NULL, 0))) {
578                 DEBUG(3,("scan dir didn't open dir [%s]\n",path));
579                 return(False);
580         }
581
582         /* now scan for matching names */
583         curpos = 0;
584         while ((dname = ReadDirName(cur_dir, &curpos))) {
585
586                 /* Is it dot or dot dot. */
587                 if ((dname[0] == '.') && (!dname[1] || (dname[1] == '.' && !dname[2]))) {
588                         continue;
589                 }
590
591                 /*
592                  * At this point dname is the unmangled name.
593                  * name is either mangled or not, depending on the state of the "mangled"
594                  * variable. JRA.
595                  */
596
597                 /*
598                  * Check mangled name against mangled name, or unmangled name
599                  * against unmangled name.
600                  */
601
602                 if ((mangled && mangled_equal(name,dname,conn->params)) || fname_equal(name, dname, conn->case_sensitive)) {
603                         /* we've found the file, change it's name and return */
604                         safe_strcpy(name, dname, maxlength);
605                         CloseDir(cur_dir);
606                         return(True);
607                 }
608         }
609
610         CloseDir(cur_dir);
611         errno = ENOENT;
612         return(False);
613 }