summaryrefslogtreecommitdiff
path: root/source3/smbd/filename.c
blob: 0910e03ee3409c7c6cc071bb7273cd4727212983 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
/* 
   Unix SMB/Netbios implementation.
   Version 1.9.
   filename handling routines
   Copyright (C) Andrew Tridgell 1992-1998
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"

extern int DEBUGLEVEL;
extern BOOL case_sensitive;
extern BOOL case_preserve;
extern BOOL short_case_preserve;
extern fstring remote_machine;
extern BOOL use_mangled_map;

static BOOL scan_directory(char *path, char *name,connection_struct *conn,BOOL docache);

/****************************************************************************
 Check if two filenames are equal.
 This needs to be careful about whether we are case sensitive.
****************************************************************************/
static BOOL fname_equal(char *name1, char *name2)
{
  int l1 = strlen(name1);
  int l2 = strlen(name2);

  /* handle filenames ending in a single dot */
  if (l1-l2 == 1 && name1[l1-1] == '.' && lp_strip_dot())
    {
      BOOL ret;
      name1[l1-1] = 0;
      ret = fname_equal(name1,name2);
      name1[l1-1] = '.';
      return(ret);
    }

  if (l2-l1 == 1 && name2[l2-1] == '.' && lp_strip_dot())
    {
      BOOL ret;
      name2[l2-1] = 0;
      ret = fname_equal(name1,name2);
      name2[l2-1] = '.';
      return(ret);
    }

  /* now normal filename handling */
  if (case_sensitive)
    return(strcmp(name1,name2) == 0);

  return(strequal(name1,name2));
}


/****************************************************************************
 Mangle the 2nd name and check if it is then equal to the first name.
****************************************************************************/
static BOOL mangled_equal(char *name1, char *name2)
{
  pstring tmpname;

  if (is_8_3(name2, True))
    return(False);

  pstrcpy(tmpname,name2);
  mangle_name_83(tmpname);

  return(strequal(name1,tmpname));
}

/****************************************************************************
 Stat cache code used in unix_convert.
*****************************************************************************/

static int global_stat_cache_lookups;
static int global_stat_cache_misses;
static int global_stat_cache_hits;

/****************************************************************************
 Stat cache statistics code.
*****************************************************************************/

void print_stat_cache_statistics(void)
{
  double eff = (100.0* (double)global_stat_cache_hits)/(double)global_stat_cache_lookups;

  DEBUG(0,("stat cache stats: lookups = %d, hits = %d, misses = %d, \
stat cache was %f%% effective.\n", global_stat_cache_lookups,
       global_stat_cache_hits, global_stat_cache_misses, eff ));
}

typedef struct {
  ubi_dlNode link;
  int name_len;
  pstring orig_name;
  pstring translated_name;
} stat_cache_entry;

#define MAX_STAT_CACHE_SIZE 50

static ubi_dlList stat_cache = { NULL, (ubi_dlNodePtr)&stat_cache, 0};

/****************************************************************************
 Compare two names in the stat cache - to check if we already have such an
 entry.
*****************************************************************************/

static BOOL stat_name_equal( char *s1, char *s2)
{
  return (case_sensitive ? (strcmp( s1, s2) == 0) : (StrCaseCmp(s1, s2) == 0));
}

/****************************************************************************
 Compare a pathname to a name in the stat cache - of a given length.
 Note - this code always checks that the next character in the pathname
 is either a '/' character, or a '\0' character - to ensure we only
 match *full* pathname components.
*****************************************************************************/

static BOOL stat_name_equal_len( char *stat_name, char *orig_name, int len)
{
  BOOL matched = (case_sensitive ? (strncmp( stat_name, orig_name, len) == 0) :
                           (StrnCaseCmp(stat_name, orig_name, len) == 0));
  if(orig_name[len] != '/' && orig_name[len] != '\0')
    return False;

  return matched;
}

/****************************************************************************
 Add an entry into the stat cache.
*****************************************************************************/

static void stat_cache_add( char *full_orig_name, char *orig_translated_path)
{
  stat_cache_entry *scp;
  pstring orig_name;
  pstring translated_path;
  int namelen;

  if (!lp_stat_cache()) return;

  namelen = strlen(orig_translated_path);

  /*
   * Don't cache trivial valid directory entries.
   */
  if(strequal(full_orig_name, ".") || strequal(full_orig_name, ".."))
    return;

  /*
   * If we are in case insentive mode, we need to
   * store names that need no translation - else, it
   * would be a waste.
   */

  if(case_sensitive && (strcmp(full_orig_name, orig_translated_path) == 0))
    return;

  /*
   * Remove any trailing '/' characters from the
   * translated path.
   */

  pstrcpy(translated_path, orig_translated_path);
  if(translated_path[namelen-1] == '/') {
    translated_path[namelen-1] = '\0';
    namelen--;
  }

  /*
   * We will only replace namelen characters 
   * of full_orig_name.
   * StrnCpy always null terminates.
   */

  StrnCpy(orig_name, full_orig_name, namelen);

  /*
   * Check this name doesn't exist in the cache before we 
   * add it.
   */

  for( scp = (stat_cache_entry *)ubi_dlFirst( &stat_cache); scp; 
                        scp = (stat_cache_entry *)ubi_dlNext( scp )) {
    if(stat_name_equal( scp->orig_name, orig_name) &&
       (strcmp( scp->translated_name, translated_path) == 0)) {
      /*
       * Name does exist - promote it.
       */
      if( (stat_cache_entry *)ubi_dlFirst( &stat_cache) != scp ) {
        ubi_dlRemThis( &stat_cache, scp);
        ubi_dlAddHead( &stat_cache, scp);
      }
      return;
    }
  }

  if((scp = (stat_cache_entry *)malloc(sizeof(stat_cache_entry))) == NULL) {
    DEBUG(0,("stat_cache_add: Out of memory !\n"));
    return;
  }

  pstrcpy(scp->orig_name, orig_name);
  pstrcpy(scp->translated_name, translated_path);
  scp->name_len = namelen;

  ubi_dlAddHead( &stat_cache, scp);

  DEBUG(10,("stat_cache_add: Added entry %s -> %s\n", scp->orig_name, scp->translated_name ));

  if(ubi_dlCount(&stat_cache) > lp_stat_cache_size()) {
    scp = (stat_cache_entry *)ubi_dlRemTail( &stat_cache );
    free((char *)scp);
    return;
  }
}

/****************************************************************************
 Look through the stat cache for an entry - promote it to the top if found.
 Return True if we translated (and did a scuccessful stat on) the entire name.
*****************************************************************************/

static BOOL stat_cache_lookup( char *name, char *dirpath, char **start, SMB_STRUCT_STAT *pst)
{
  stat_cache_entry *scp;
  stat_cache_entry *longest_hit = NULL;
  int namelen;

  if (!lp_stat_cache()) return False;
 
  namelen = strlen(name);

  *start = name;
  global_stat_cache_lookups++;

  /*
   * Don't lookup trivial valid directory entries.
   */
  if(strequal(name, ".") || strequal(name, "..")) {
    global_stat_cache_misses++;
    return False;
  }

  for( scp = (stat_cache_entry *)ubi_dlFirst( &stat_cache); scp; 
                        scp = (stat_cache_entry *)ubi_dlNext( scp )) {
    if(scp->name_len <= namelen) {
      if(stat_name_equal_len(scp->orig_name, name, scp->name_len)) {
        if((longest_hit == NULL) || (longest_hit->name_len <= scp->name_len))
          longest_hit = scp;
      }
    }
  }

  if(longest_hit == NULL) {
    DEBUG(10,("stat_cache_lookup: cache miss on %s\n", name));
    global_stat_cache_misses++;
    return False;
  }

  global_stat_cache_hits++;

  DEBUG(10,("stat_cache_lookup: cache hit for name %s. %s -> %s\n",
        name, longest_hit->orig_name, longest_hit->translated_name ));

  /*
   * longest_hit is the longest match we got in the list.
   * Check it exists - if so, overwrite the original name
   * and then promote it to the top.
   */

  if(dos_stat( longest_hit->translated_name, pst) != 0) {
    /*
     * Discard this entry.
     */
    ubi_dlRemThis( &stat_cache, longest_hit);
    free((char *)longest_hit);
    return False;
  }

  memcpy(name, longest_hit->translated_name, longest_hit->name_len);
  if( (stat_cache_entry *)ubi_dlFirst( &stat_cache) != longest_hit ) {
    ubi_dlRemThis( &stat_cache, longest_hit);
    ubi_dlAddHead( &stat_cache, longest_hit);
  }

  *start = &name[longest_hit->name_len];
  if(**start == '/')
    ++*start;

  StrnCpy( dirpath, longest_hit->translated_name, name - (*start));

  return (namelen == longest_hit->name_len);
}

/****************************************************************************
This routine is called to convert names from the dos namespace to unix
namespace. It needs to handle any case conversions, mangling, format
changes etc.

We assume that we have already done a chdir() to the right "root" directory
for this service.

The function will return False if some part of the name except for the last
part cannot be resolved

If the saved_last_component != 0, then the unmodified last component
of the pathname is returned there. This is used in an exceptional
case in reply_mv (so far). If saved_last_component == 0 then nothing
is returned there.

The bad_path arg is set to True if the filename walk failed. This is
used to pick the correct error code to return between ENOENT and ENOTDIR
as Windows applications depend on ERRbadpath being returned if a component
of a pathname does not exist.
****************************************************************************/

BOOL unix_convert(char *name,connection_struct *conn,char *saved_last_component, 
                  BOOL *bad_path, SMB_STRUCT_STAT *pst)
{
  SMB_STRUCT_STAT st;
  char *start, *end, *orig_start;
  pstring dirpath;
  pstring orig_path;
  int saved_errno;
  BOOL component_was_mangled = False;
  BOOL name_has_wildcard = False;
#if 0
  /* Andrew's conservative code... JRA. */
  extern char magic_char;
#endif

  *dirpath = 0;
  *bad_path = False;
  if(pst) {
	  ZERO_STRUCTP(pst);
  }

  if(saved_last_component)
    *saved_last_component = 0;

  /* 
   * Convert to basic unix format - removing \ chars and cleaning it up.
   */

  unix_format(name);
  unix_clean_name(name);

  /* 
   * Names must be relative to the root of the service - trim any leading /.
   * also trim trailing /'s.
   */

  trim_string(name,"/","/");

  /*
   * Ensure saved_last_component is valid even if file exists.
   */

  if(saved_last_component) {
    end = strrchr(name, '/');
    if(end)
      pstrcpy(saved_last_component, end + 1);
    else
      pstrcpy(saved_last_component, name);
  }

  if (!case_sensitive && 
      (!case_preserve || (is_8_3(name, False) && !short_case_preserve)))
    strnorm(name);

  /* 
   * Check if it's a printer file.
   */
  if (conn->printer) {
    if ((! *name) || strchr(name,'/') || !is_8_3(name, True)) {
      char *s;
      fstring name2;
      slprintf(name2,sizeof(name2)-1,"%.6s.XXXXXX",remote_machine);

      /* 
       * Sanitise the name.
       */

      for (s=name2 ; *s ; s++)
        if (!issafe(*s)) *s = '_';
          pstrcpy(name,(char *)mktemp(name2));	  
    }      
    return(True);
  }

  start = name;
  while (strncmp(start,"./",2) == 0)
    start += 2;

  pstrcpy(orig_path, name);

  if(stat_cache_lookup( name, dirpath, &start, &st)) {
    if(pst)
      *pst = st;
    return True;
  }

  /* 
   * stat the name - if it exists then we are all done!
   */

  if (dos_stat(name,&st) == 0) {
    stat_cache_add(orig_path, name);
    DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
    if(pst)
      *pst = st;
    return(True);
  }

  saved_errno = errno;

  DEBUG(5,("unix_convert begin: name = %s, dirpath = %s, start = %s\n",
        name, dirpath, start));

  /* 
   * A special case - if we don't have any mangling chars and are case
   * sensitive then searching won't help.
   */

  if (case_sensitive && !is_mangled(name) && 
      !lp_strip_dot() && !use_mangled_map && (saved_errno != ENOENT))
    return(False);

  if(strchr(start,'?') || strchr(start,'*'))
    name_has_wildcard = True;

  /* 
   * is_mangled() was changed to look at an entire pathname, not 
   * just a component. JRA.
   */

  if(is_mangled(start))
    component_was_mangled = True;

#if 0
  /* Keep Andrew's conservative code around, just in case. JRA. */
  /* this is an extremely conservative test for mangled names. */
  if (strchr(start,magic_char))
    component_was_mangled = True;
#endif

  /* 
   * Now we need to recursively match the name against the real 
   * directory structure.
   */

  /* 
   * Match each part of the path name separately, trying the names
   * as is first, then trying to scan the directory for matching names.
   */

  for (orig_start = start; start ; start = (end?end+1:(char *)NULL)) {
      /* 
       * Pinpoint the end of this section of the filename.
       */
      end = strchr(start, '/');

      /* 
       * Chop the name at this point.
       */
      if (end) 
        *end = 0;

      if(saved_last_component != 0)
        pstrcpy(saved_last_component, end ? end + 1 : start);

      /* 
       * Check if the name exists up to this point.
       */
      if (dos_stat(name, &st) == 0) {
        /*
         * It exists. it must either be a directory or this must be
         * the last part of the path for it to be OK.
         */
        if (end && !(st.st_mode & S_IFDIR)) {
          /*
           * An intermediate part of the name isn't a directory.
            */
          DEBUG(5,("Not a dir %s\n",start));
          *end = '/';
          return(False);
        }

      } else {
        pstring rest;

        *rest = 0;

        /*
         * Remember the rest of the pathname so it can be restored
         * later.
         */

        if (end)
          pstrcpy(rest,end+1);

        /*
         * Try to find this part of the path in the directory.
         */

        if (strchr(start,'?') || strchr(start,'*') ||
            !scan_directory(dirpath, start, conn, end?True:False)) {
          if (end) {
            /*
             * An intermediate part of the name can't be found.
             */
            DEBUG(5,("Intermediate not found %s\n",start));
            *end = '/';

            /* 
             * We need to return the fact that the intermediate
             * name resolution failed. This is used to return an
             * error of ERRbadpath rather than ERRbadfile. Some
             * Windows applications depend on the difference between
             * these two errors.
             */
            *bad_path = True;
            return(False);
          }
	      
          /* 
           * Just the last part of the name doesn't exist.
	       * We may need to strupper() or strlower() it in case
           * this conversion is being used for file creation 
           * purposes. If the filename is of mixed case then 
           * don't normalise it.
           */

          if (!case_preserve && (!strhasupper(start) || !strhaslower(start)))		
            strnorm(start);

          /*
           * check on the mangled stack to see if we can recover the 
           * base of the filename.
           */

          if (is_mangled(start)) {
            check_mangled_cache( start );
          }

          DEBUG(5,("New file %s\n",start));
          return(True); 
        }

      /* 
       * Restore the rest of the string.
       */
      if (end) {
        pstrcpy(start+strlen(start)+1,rest);
        end = start + strlen(start);
      }
    } /* end else */

    /* 
     * Add to the dirpath that we have resolved so far.
     */
    if (*dirpath)
      pstrcat(dirpath,"/");

    pstrcat(dirpath,start);

    /*
     * Don't cache a name with mangled or wildcard components
     * as this can change the size.
     */

    if(!component_was_mangled && !name_has_wildcard)
      stat_cache_add(orig_path, dirpath);

    /* 
     * Restore the / that we wiped out earlier.
     */
    if (end)
      *end = '/';
  }
  
  /*
   * Don't cache a name with mangled or wildcard components
   * as this can change the size.
   */

  if(!component_was_mangled && !name_has_wildcard)
    stat_cache_add(orig_path, name);

  /* 
   * The name has been resolved.
   */

  DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
  return(True);
}


/****************************************************************************
check a filename - possibly caling reducename

This is called by every routine before it allows an operation on a filename.
It does any final confirmation necessary to ensure that the filename is
a valid one for the user to access.
****************************************************************************/
BOOL check_name(char *name,connection_struct *conn)
{
  BOOL ret;

  errno = 0;

  if (IS_VETO_PATH(conn, name))  {
	  DEBUG(5,("file path name %s vetoed\n",name));
	  return(0);
  }

  ret = reduce_name(name,conn->connectpath,lp_widelinks(SNUM(conn)));

  /* Check if we are allowing users to follow symlinks */
  /* Patch from David Clerc <David.Clerc@cui.unige.ch>
     University of Geneva */

#ifdef S_ISLNK
  if (!lp_symlinks(SNUM(conn)))
    {
      SMB_STRUCT_STAT statbuf;
      if ( (dos_lstat(name,&statbuf) != -1) &&
          (S_ISLNK(statbuf.st_mode)) )
        {
          DEBUG(3,("check_name: denied: file path name %s is a symlink\n",name));
          ret=0; 
        }
    }
#endif

  if (!ret)
    DEBUG(5,("check_name on %s failed\n",name));

  return(ret);
}


/****************************************************************************
scan a directory to find a filename, matching without case sensitivity

If the name looks like a mangled name then try via the mangling functions
****************************************************************************/
static BOOL scan_directory(char *path, char *name,connection_struct *conn,BOOL docache)
{
  void *cur_dir;
  char *dname;
  BOOL mangled;
  pstring name2;

  mangled = is_mangled(name);

  /* handle null paths */
  if (*path == 0)
    path = ".";

  if (docache && (dname = DirCacheCheck(path,name,SNUM(conn)))) {
    pstrcpy(name, dname);	
    return(True);
  }      

  /*
   * The incoming name can be mangled, and if we de-mangle it
   * here it will not compare correctly against the filename (name2)
   * read from the directory and then mangled by the name_map_mangle()
   * call. We need to mangle both names or neither.
   * (JRA).
   */
  if (mangled)
    mangled = !check_mangled_cache( name );

  /* open the directory */
  if (!(cur_dir = OpenDir(conn, path, True))) 
    {
      DEBUG(3,("scan dir didn't open dir [%s]\n",path));
      return(False);
    }

  /* now scan for matching names */
  while ((dname = ReadDirName(cur_dir))) 
    {
      if (*dname == '.' &&
	  (strequal(dname,".") || strequal(dname,"..")))
	continue;

      pstrcpy(name2,dname);
      if (!name_map_mangle(name2,False,SNUM(conn))) continue;

      if ((mangled && mangled_equal(name,name2))
	  || fname_equal(name, name2))
	{
	  /* we've found the file, change it's name and return */
	  if (docache) DirCacheAdd(path,name,dname,SNUM(conn));
	  pstrcpy(name, dname);
	  CloseDir(cur_dir);
	  return(True);
	}
    }

  CloseDir(cur_dir);
  return(False);
}