r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[tprouty/samba.git] / source / 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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 /*
23  * New hash table stat cache code added by Ying Chen.
24  */
25
26 #include "includes.h"
27
28 static BOOL scan_directory(connection_struct *conn, const char *path, char *name,size_t maxlength);
29
30 /****************************************************************************
31  Check if two filenames are equal.
32  This needs to be careful about whether we are case sensitive.
33 ****************************************************************************/
34
35 static BOOL fname_equal(const char *name1, const char *name2, BOOL case_sensitive)
36 {
37         /* Normal filename handling */
38         if (case_sensitive)
39                 return(strcmp(name1,name2) == 0);
40
41         return(strequal(name1,name2));
42 }
43
44 /****************************************************************************
45  Mangle the 2nd name and check if it is then equal to the first name.
46 ****************************************************************************/
47
48 static BOOL mangled_equal(const char *name1, const char *name2,
49                           const struct share_params *p)
50 {
51         pstring tmpname;
52         
53         pstrcpy(tmpname, name2);
54         mangle_map(tmpname, True, False, p);
55         return strequal(name1, tmpname);
56 }
57
58 /****************************************************************************
59  Cope with the differing wildcard and non-wildcard error cases.
60 ****************************************************************************/
61
62 static NTSTATUS determine_path_error(const char *name, BOOL allow_wcard_last_component)
63 {
64         const char *p;
65
66         if (!allow_wcard_last_component) {
67                 /* Error code within a pathname. */
68                 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
69         }
70
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" */
78
79         p = strchr(name, '/');
80
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;
84         } else {
85                 /* Error code within a pathname. */
86                 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
87         }
88 }
89         
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
93 changes etc.
94
95 We assume that we have already done a chdir() to the right "root" directory
96 for this service.
97
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.
100
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.
104
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
108 is returned there.
109
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.
112
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 ****************************************************************************/
118
119 NTSTATUS unix_convert(connection_struct *conn,
120                         pstring name,
121                         BOOL allow_wcard_last_component,
122                         char *saved_last_component, 
123                         SMB_STRUCT_STAT *pst)
124 {
125         SMB_STRUCT_STAT st;
126         char *start, *end;
127         pstring dirpath;
128         pstring orig_path;
129         BOOL component_was_mangled = False;
130         BOOL name_has_wildcard = False;
131
132         SET_STAT_INVALID(*pst);
133
134         *dirpath = 0;
135
136         if(saved_last_component) {
137                 *saved_last_component = 0;
138         }
139
140         if (conn->printer) {
141                 /* we don't ever use the filenames on a printer share as a
142                         filename - so don't convert them */
143                 return NT_STATUS_OK;
144         }
145
146         DEBUG(5, ("unix_convert called on file \"%s\"\n", name));
147
148         /* 
149          * Conversion to basic unix format is already done in check_path_syntax().
150          */
151
152         /* 
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().
155          */
156
157 #ifdef DEVELOPER
158         SMB_ASSERT(*name != '/');
159 #endif
160
161         /*
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
165          * printing share.
166          * As we know this is valid we can return true here.
167          */
168
169         if (!*name) {
170                 name[0] = '.';
171                 name[1] = '\0';
172                 if (SMB_VFS_STAT(conn,name,&st) == 0) {
173                         *pst = st;
174                 }
175                 DEBUG(5,("conversion finished \"\" -> %s\n",name));
176                 return NT_STATUS_OK;
177         }
178
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;
183                 } else {
184                         return determine_path_error(&name[2], allow_wcard_last_component);
185                 }
186         }
187
188         /*
189          * Ensure saved_last_component is valid even if file exists.
190          */
191
192         if(saved_last_component) {
193                 end = strrchr_m(name, '/');
194                 if (end) {
195                         pstrcpy(saved_last_component, end + 1);
196                 } else {
197                         pstrcpy(saved_last_component, name);
198                 }
199         }
200
201         /*
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.
208          */
209
210         if (conn->case_sensitive && !conn->case_preserve && !conn->short_case_preserve) {
211                 strnorm(name, lp_defaultcase(SNUM(conn)));
212         }
213         
214         start = name;
215         pstrcpy(orig_path, name);
216
217         if(!conn->case_sensitive && stat_cache_lookup(conn, name, dirpath, &start, &st)) {
218                 *pst = st;
219                 return NT_STATUS_OK;
220         }
221
222         /* 
223          * stat the name - if it exists then we are all done!
224          */
225
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. */
230                 if (p) {
231                         if (p[2] == '/') {
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;
237                         }
238                 }
239                 stat_cache_add(orig_path, name, conn->case_sensitive);
240                 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
241                 *pst = st;
242                 return NT_STATUS_OK;
243         }
244
245         DEBUG(5,("unix_convert begin: name = %s, dirpath = %s, start = %s\n", name, dirpath, start));
246
247         /* 
248          * A special case - if we don't have any mangling chars and are case
249          * sensitive then searching won't help.
250          */
251
252         if (conn->case_sensitive && 
253                         !mangle_is_mangled(name, conn->params) &&
254                         !*lp_mangled_map(conn->params)) {
255                 return NT_STATUS_OK;
256         }
257
258         /* 
259          * is_mangled() was changed to look at an entire pathname, not 
260          * just a component. JRA.
261          */
262
263         if (mangle_is_mangled(start, conn->params)) {
264                 component_was_mangled = True;
265         }
266
267         /* 
268          * Now we need to recursively match the name against the real 
269          * directory structure.
270          */
271
272         /* 
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.
275          */
276
277         for (; start ; start = (end?end+1:(char *)NULL)) {
278                 /* 
279                  * Pinpoint the end of this section of the filename.
280                  */
281                 end = strchr(start, '/'); /* mb safe. '/' can't be in any encoded char. */
282
283                 /* 
284                  * Chop the name at this point.
285                  */
286                 if (end) {
287                         *end = 0;
288                 }
289
290                 if (saved_last_component != 0) {
291                         pstrcpy(saved_last_component, end ? end + 1 : start);
292                 }
293
294                 /* The name cannot have a component of "." */
295
296                 if (ISDOT(start)) {
297                         if (!end)  {
298                                 /* Error code at the end of a pathname. */
299                                 return NT_STATUS_OBJECT_NAME_INVALID;
300                         }
301                         return determine_path_error(end+1, allow_wcard_last_component);
302                 }
303
304                 /* The name cannot have a wildcard if it's not
305                    the last component. */
306
307                 name_has_wildcard = ms_has_wild(start);
308
309                 /* Wildcard not valid anywhere. */
310                 if (name_has_wildcard && !allow_wcard_last_component) {
311                         return NT_STATUS_OBJECT_NAME_INVALID;
312                 }
313
314                 /* Wildcards never valid within a pathname. */
315                 if (name_has_wildcard && end) {
316                         return NT_STATUS_OBJECT_NAME_INVALID;
317                 }
318
319                 /* 
320                  * Check if the name exists up to this point.
321                  */
322
323                 if (SMB_VFS_STAT(conn,name, &st) == 0) {
324                         /*
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.
327                          */
328                         if (end && !(st.st_mode & S_IFDIR)) {
329                                 /*
330                                  * An intermediate part of the name isn't a directory.
331                                  */
332                                 DEBUG(5,("Not a dir %s\n",start));
333                                 *end = '/';
334                                 /* 
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
339                                  * these two errors.
340                                  */
341                                 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
342                         }
343
344                         if (!end) {
345                                 /*
346                                  * We just scanned for, and found the end of the path.
347                                  * We must return the valid stat struct.
348                                  * JRA.
349                                  */
350
351                                 *pst = st;
352                         }
353
354                 } else {
355                         pstring rest;
356
357                         /* Stat failed - ensure we don't use it. */
358                         SET_STAT_INVALID(st);
359                         *rest = 0;
360
361                         /*
362                          * Remember the rest of the pathname so it can be restored
363                          * later.
364                          */
365
366                         if (end) {
367                                 pstrcpy(rest,end+1);
368                         }
369
370                         /* Reset errno so we can detect directory open errors. */
371                         errno = 0;
372
373                         /*
374                          * Try to find this part of the path in the directory.
375                          */
376
377                         if (name_has_wildcard || 
378                             !scan_directory(conn, dirpath, start, sizeof(pstring) - 1 - (start - name))) {
379                                 if (end) {
380                                         /*
381                                          * An intermediate part of the name can't be found.
382                                          */
383                                         DEBUG(5,("Intermediate not found %s\n",start));
384                                         *end = '/';
385
386                                         /* 
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
391                                          * these two errors.
392                                          */
393
394                                         /* ENOENT and ENOTDIR both map to NT_STATUS_OBJECT_PATH_NOT_FOUND
395                                            in the filename walk. */
396
397                                         if (errno == ENOENT || errno == ENOTDIR) {
398                                                 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
399                                         }
400                                         return map_nt_error_from_unix(errno);
401                                 }
402               
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;
409                                         }
410                                         return map_nt_error_from_unix(errno);
411                                 }
412
413                                 /*
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>.
418                                  */
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)));
423                                 }
424
425                                 /*
426                                  * check on the mangled stack to see if we can recover the 
427                                  * base of the filename.
428                                  */
429
430                                 if (mangle_is_mangled(start, conn->params)) {
431                                         mangle_check_cache( start, sizeof(pstring) - 1 - (start - name), conn->params);
432                                 }
433
434                                 DEBUG(5,("New file %s\n",start));
435                                 return NT_STATUS_OK;
436                         }
437
438                         /* 
439                          * Restore the rest of the string. If the string was mangled the size
440                          * may have changed.
441                          */
442                         if (end) {
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);
447                                 }
448                                 *end = '\0';
449                         } else {
450                                 /*
451                                  * We just scanned for, and found the end of the path.
452                                  * We must return a valid stat struct if it exists.
453                                  * JRA.
454                                  */
455
456                                 if (SMB_VFS_STAT(conn,name, &st) == 0) {
457                                         *pst = st;
458                                 } else {
459                                         SET_STAT_INVALID(st);
460                                 }
461                         }
462                 } /* end else */
463
464 #ifdef DEVELOPER
465                 if (VALID_STAT(st) && get_delete_on_close_flag(file_id_sbuf(&st))) {
466                         return NT_STATUS_DELETE_PENDING;
467                 }
468 #endif
469
470                 /* 
471                  * Add to the dirpath that we have resolved so far.
472                  */
473                 if (*dirpath) {
474                         pstrcat(dirpath,"/");
475                 }
476
477                 pstrcat(dirpath,start);
478
479                 /*
480                  * Don't cache a name with mangled or wildcard components
481                  * as this can change the size.
482                  */
483                 
484                 if(!component_was_mangled && !name_has_wildcard) {
485                         stat_cache_add(orig_path, dirpath, conn->case_sensitive);
486                 }
487         
488                 /* 
489                  * Restore the / that we wiped out earlier.
490                  */
491                 if (end) {
492                         *end = '/';
493                 }
494         }
495   
496         /*
497          * Don't cache a name with mangled or wildcard components
498          * as this can change the size.
499          */
500
501         if(!component_was_mangled && !name_has_wildcard) {
502                 stat_cache_add(orig_path, name, conn->case_sensitive);
503         }
504
505         /* 
506          * The name has been resolved.
507          */
508
509         DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
510         return NT_STATUS_OK;
511 }
512
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 ****************************************************************************/
519
520 NTSTATUS check_name(connection_struct *conn, const pstring name)
521 {
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);
527                 }
528         }
529
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)));
534                         return status;
535                 }
536         }
537
538         return NT_STATUS_OK;
539 }
540
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 ****************************************************************************/
545
546 static BOOL scan_directory(connection_struct *conn, const char *path, char *name, size_t maxlength)
547 {
548         struct smb_Dir *cur_dir;
549         const char *dname;
550         BOOL mangled;
551         long curpos;
552
553         mangled = mangle_is_mangled(name, conn->params);
554
555         /* handle null paths */
556         if (*path == 0)
557                 path = ".";
558
559         /*
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.
564          * (JRA).
565          *
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.
572          */
573
574         if (mangled && !conn->case_sensitive) {
575                 mangled = !mangle_check_cache( name, maxlength, conn->params);
576         }
577
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));
581                 return(False);
582         }
583
584         /* now scan for matching names */
585         curpos = 0;
586         while ((dname = ReadDirName(cur_dir, &curpos))) {
587
588                 /* Is it dot or dot dot. */
589                 if ((dname[0] == '.') && (!dname[1] || (dname[1] == '.' && !dname[2]))) {
590                         continue;
591                 }
592
593                 /*
594                  * At this point dname is the unmangled name.
595                  * name is either mangled or not, depending on the state of the "mangled"
596                  * variable. JRA.
597                  */
598
599                 /*
600                  * Check mangled name against mangled name, or unmangled name
601                  * against unmangled name.
602                  */
603
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);
607                         CloseDir(cur_dir);
608                         return(True);
609                 }
610         }
611
612         CloseDir(cur_dir);
613         errno = ENOENT;
614         return(False);
615 }