root/source3/smbd/filename.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. mangled_equal
  2. determine_path_error
  3. unix_convert
  4. check_name
  5. fname_equal
  6. get_real_filename_full_scan
  7. get_real_filename
  8. build_stream_path

   1 /*
   2    Unix SMB/CIFS implementation.
   3    filename handling routines
   4    Copyright (C) Andrew Tridgell 1992-1998
   5    Copyright (C) Jeremy Allison 1999-2007
   6    Copyright (C) Ying Chen 2000
   7    Copyright (C) Volker Lendecke 2007
   8 
   9    This program is free software; you can redistribute it and/or modify
  10    it under the terms of the GNU General Public License as published by
  11    the Free Software Foundation; either version 3 of the License, or
  12    (at your option) any later version.
  13 
  14    This program is distributed in the hope that it will be useful,
  15    but WITHOUT ANY WARRANTY; without even the implied warranty of
  16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17    GNU General Public License for more details.
  18 
  19    You should have received a copy of the GNU General Public License
  20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  21 */
  22 
  23 /*
  24  * New hash table stat cache code added by Ying Chen.
  25  */
  26 
  27 #include "includes.h"
  28 
  29 static NTSTATUS build_stream_path(TALLOC_CTX *mem_ctx,
  30                                   connection_struct *conn,
  31                                   const char *orig_path,
  32                                   const char *basepath,
  33                                   const char *streamname,
  34                                   SMB_STRUCT_STAT *pst,
  35                                   char **path);
  36 
  37 /****************************************************************************
  38  Mangle the 2nd name and check if it is then equal to the first name.
  39 ****************************************************************************/
  40 
  41 static bool mangled_equal(const char *name1,
     /* [<][>][^][v][top][bottom][index][help] */
  42                         const char *name2,
  43                         const struct share_params *p)
  44 {
  45         char mname[13];
  46 
  47         if (!name_to_8_3(name2, mname, False, p)) {
  48                 return False;
  49         }
  50         return strequal(name1, mname);
  51 }
  52 
  53 /****************************************************************************
  54  Cope with the differing wildcard and non-wildcard error cases.
  55 ****************************************************************************/
  56 
  57 static NTSTATUS determine_path_error(const char *name,
     /* [<][>][^][v][top][bottom][index][help] */
  58                         bool allow_wcard_last_component)
  59 {
  60         const char *p;
  61 
  62         if (!allow_wcard_last_component) {
  63                 /* Error code within a pathname. */
  64                 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
  65         }
  66 
  67         /* We're terminating here so we
  68          * can be a little slower and get
  69          * the error code right. Windows
  70          * treats the last part of the pathname
  71          * separately I think, so if the last
  72          * component is a wildcard then we treat
  73          * this ./ as "end of component" */
  74 
  75         p = strchr(name, '/');
  76 
  77         if (!p && (ms_has_wild(name) || ISDOT(name))) {
  78                 /* Error code at the end of a pathname. */
  79                 return NT_STATUS_OBJECT_NAME_INVALID;
  80         } else {
  81                 /* Error code within a pathname. */
  82                 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
  83         }
  84 }
  85 
  86 /****************************************************************************
  87 This routine is called to convert names from the dos namespace to unix
  88 namespace. It needs to handle any case conversions, mangling, format
  89 changes etc.
  90 
  91 We assume that we have already done a chdir() to the right "root" directory
  92 for this service.
  93 
  94 The function will return an NTSTATUS error if some part of the name except for
  95 the last part cannot be resolved, else NT_STATUS_OK.
  96 
  97 Note NT_STATUS_OK doesn't mean the name exists or is valid, just that we didn't
  98 get any fatal errors that should immediately terminate the calling
  99 SMB processing whilst resolving.
 100 
 101 If the saved_last_component != 0, then the unmodified last component
 102 of the pathname is returned there. If saved_last_component == 0 then nothing
 103 is returned there.
 104 
 105 If last_component_wcard is true then a MS wildcard was detected and
 106 should be allowed in the last component of the path only.
 107 
 108 On exit from unix_convert, if *pst was not null, then the file stat
 109 struct will be returned if the file exists and was found, if not this
 110 stat struct will be filled with zeros (and this can be detected by checking
 111 for nlinks = 0, which can never be true for any file).
 112 ****************************************************************************/
 113 
 114 NTSTATUS unix_convert(TALLOC_CTX *ctx,
     /* [<][>][^][v][top][bottom][index][help] */
 115                         connection_struct *conn,
 116                         const char *orig_path,
 117                         bool allow_wcard_last_component,
 118                         char **pp_conv_path,
 119                         char **pp_saved_last_component,
 120                         SMB_STRUCT_STAT *pst)
 121 {
 122         SMB_STRUCT_STAT st;
 123         char *start, *end;
 124         char *dirpath = NULL;
 125         char *name = NULL;
 126         char *stream = NULL;
 127         bool component_was_mangled = False;
 128         bool name_has_wildcard = False;
 129         bool posix_pathnames = false;
 130         NTSTATUS result;
 131         int ret = -1;
 132 
 133         SET_STAT_INVALID(*pst);
 134         *pp_conv_path = NULL;
 135         if(pp_saved_last_component) {
 136                 *pp_saved_last_component = NULL;
 137         }
 138 
 139         if (conn->printer) {
 140                 /* we don't ever use the filenames on a printer share as a
 141                         filename - so don't convert them */
 142                 if (!(*pp_conv_path = talloc_strdup(ctx,orig_path))) {
 143                         return NT_STATUS_NO_MEMORY;
 144                 }
 145                 return NT_STATUS_OK;
 146         }
 147 
 148         DEBUG(5, ("unix_convert called on file \"%s\"\n", orig_path));
 149 
 150         /*
 151          * Conversion to basic unix format is already done in
 152          * check_path_syntax().
 153          */
 154 
 155         /*
 156          * Names must be relative to the root of the service - any leading /.
 157          * and trailing /'s should have been trimmed by check_path_syntax().
 158          */
 159 
 160 #ifdef DEVELOPER
 161         SMB_ASSERT(*orig_path != '/');
 162 #endif
 163 
 164         /*
 165          * If we trimmed down to a single '\0' character
 166          * then we should use the "." directory to avoid
 167          * searching the cache, but not if we are in a
 168          * printing share.
 169          * As we know this is valid we can return true here.
 170          */
 171 
 172         if (!*orig_path) {
 173                 if (!(name = talloc_strdup(ctx,"."))) {
 174                         return NT_STATUS_NO_MEMORY;
 175                 }
 176                 if (SMB_VFS_STAT(conn,name,&st) == 0) {
 177                         *pst = st;
 178                 } else {
 179                         return map_nt_error_from_unix(errno);
 180                 }
 181                 DEBUG(5,("conversion finished \"\" -> %s\n",name));
 182                 goto done;
 183         }
 184 
 185         if (orig_path[0] == '.' && (orig_path[1] == '/' ||
 186                                 orig_path[1] == '\0')) {
 187                 /* Start of pathname can't be "." only. */
 188                 if (orig_path[1] == '\0' || orig_path[2] == '\0') {
 189                         result = NT_STATUS_OBJECT_NAME_INVALID;
 190                 } else {
 191                         result =determine_path_error(
 192                                 &orig_path[2], allow_wcard_last_component);
 193                 }
 194                 return result;
 195         }
 196 
 197         if (!(name = talloc_strdup(ctx, orig_path))) {
 198                 DEBUG(0, ("talloc_strdup failed\n"));
 199                 return NT_STATUS_NO_MEMORY;
 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 &&
 212                         !conn->short_case_preserve) {
 213                 strnorm(name, lp_defaultcase(SNUM(conn)));
 214         }
 215 
 216         /*
 217          * Ensure saved_last_component is valid even if file exists.
 218          */
 219 
 220         if(pp_saved_last_component) {
 221                 end = strrchr_m(name, '/');
 222                 if (end) {
 223                         *pp_saved_last_component = talloc_strdup(ctx, end + 1);
 224                 } else {
 225                         *pp_saved_last_component = talloc_strdup(ctx,
 226                                                         name);
 227                 }
 228         }
 229 
 230         posix_pathnames = lp_posix_pathnames();
 231 
 232         if (!posix_pathnames) {
 233                 stream = strchr_m(name, ':');
 234 
 235                 if (stream != NULL) {
 236                         char *tmp = talloc_strdup(ctx, stream);
 237                         if (tmp == NULL) {
 238                                 TALLOC_FREE(name);
 239                                 return NT_STATUS_NO_MEMORY;
 240                         }
 241                         *stream = '\0';
 242                         stream = tmp;
 243                 }
 244         }
 245 
 246         start = name;
 247 
 248         /* If we're providing case insentive semantics or
 249          * the underlying filesystem is case insensitive,
 250          * then a case-normalized hit in the stat-cache is
 251          * authoratitive. JRA.
 252          */
 253 
 254         if((!conn->case_sensitive || !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) &&
 255                         stat_cache_lookup(conn, &name, &dirpath, &start, &st)) {
 256                 *pst = st;
 257                 goto done;
 258         }
 259 
 260         /*
 261          * Make sure "dirpath" is an allocated string, we use this for
 262          * building the directories with asprintf and free it.
 263          */
 264 
 265         if ((dirpath == NULL) && (!(dirpath = talloc_strdup(ctx,"")))) {
 266                 DEBUG(0, ("talloc_strdup failed\n"));
 267                 TALLOC_FREE(name);
 268                 return NT_STATUS_NO_MEMORY;
 269         }
 270 
 271         /*
 272          * stat the name - if it exists then we are all done!
 273          */
 274 
 275         if (posix_pathnames) {
 276                 ret = SMB_VFS_LSTAT(conn,name,&st);
 277         } else {
 278                 ret = SMB_VFS_STAT(conn,name,&st);
 279         }
 280 
 281         if (ret == 0) {
 282                 /* Ensure we catch all names with in "/."
 283                    this is disallowed under Windows. */
 284                 const char *p = strstr(name, "/."); /* mb safe. */
 285                 if (p) {
 286                         if (p[2] == '/') {
 287                                 /* Error code within a pathname. */
 288                                 result = NT_STATUS_OBJECT_PATH_NOT_FOUND;
 289                                 goto fail;
 290                         } else if (p[2] == '\0') {
 291                                 /* Error code at the end of a pathname. */
 292                                 result = NT_STATUS_OBJECT_NAME_INVALID;
 293                                 goto fail;
 294                         }
 295                 }
 296                 stat_cache_add(orig_path, name, conn->case_sensitive);
 297                 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
 298                 *pst = st;
 299                 goto done;
 300         }
 301 
 302         DEBUG(5,("unix_convert begin: name = %s, dirpath = %s, start = %s\n",
 303                                 name, dirpath, start));
 304 
 305         /*
 306          * A special case - if we don't have any mangling chars and are case
 307          * sensitive or the underlying filesystem is case insentive then searching
 308          * won't help.
 309          */
 310 
 311         if ((conn->case_sensitive || !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) &&
 312                         !mangle_is_mangled(name, conn->params)) {
 313                 goto done;
 314         }
 315 
 316         /*
 317          * is_mangled() was changed to look at an entire pathname, not
 318          * just a component. JRA.
 319          */
 320 
 321         if (mangle_is_mangled(start, conn->params)) {
 322                 component_was_mangled = True;
 323         }
 324 
 325         /*
 326          * Now we need to recursively match the name against the real
 327          * directory structure.
 328          */
 329 
 330         /*
 331          * Match each part of the path name separately, trying the names
 332          * as is first, then trying to scan the directory for matching names.
 333          */
 334 
 335         for (; start ; start = (end?end+1:(char *)NULL)) {
 336                 /*
 337                  * Pinpoint the end of this section of the filename.
 338                  */
 339                 /* mb safe. '/' can't be in any encoded char. */
 340                 end = strchr(start, '/');
 341 
 342                 /*
 343                  * Chop the name at this point.
 344                  */
 345                 if (end) {
 346                         *end = 0;
 347                 }
 348 
 349                 if (pp_saved_last_component) {
 350                         TALLOC_FREE(*pp_saved_last_component);
 351                         *pp_saved_last_component = talloc_strdup(ctx,
 352                                                         end ? end + 1 : start);
 353                         if (!*pp_saved_last_component) {
 354                                 DEBUG(0, ("talloc failed\n"));
 355                                 return NT_STATUS_NO_MEMORY;
 356                         }
 357                 }
 358 
 359                 /* The name cannot have a component of "." */
 360 
 361                 if (ISDOT(start)) {
 362                         if (!end)  {
 363                                 /* Error code at the end of a pathname. */
 364                                 result = NT_STATUS_OBJECT_NAME_INVALID;
 365                         } else {
 366                                 result = determine_path_error(end+1,
 367                                                 allow_wcard_last_component);
 368                         }
 369                         goto fail;
 370                 }
 371 
 372                 /* The name cannot have a wildcard if it's not
 373                    the last component. */
 374 
 375                 name_has_wildcard = ms_has_wild(start);
 376 
 377                 /* Wildcard not valid anywhere. */
 378                 if (name_has_wildcard && !allow_wcard_last_component) {
 379                         result = NT_STATUS_OBJECT_NAME_INVALID;
 380                         goto fail;
 381                 }
 382 
 383                 /* Wildcards never valid within a pathname. */
 384                 if (name_has_wildcard && end) {
 385                         result = NT_STATUS_OBJECT_NAME_INVALID;
 386                         goto fail;
 387                 }
 388 
 389                 /*
 390                  * Check if the name exists up to this point.
 391                  */
 392 
 393                 if (posix_pathnames) {
 394                         ret = SMB_VFS_LSTAT(conn,name, &st);
 395                 } else {
 396                         ret = SMB_VFS_STAT(conn,name, &st);
 397                 }
 398 
 399                 if (ret == 0) {
 400                         /*
 401                          * It exists. it must either be a directory or this must
 402                          * be the last part of the path for it to be OK.
 403                          */
 404                         if (end && !(st.st_mode & S_IFDIR)) {
 405                                 /*
 406                                  * An intermediate part of the name isn't
 407                                  * a directory.
 408                                  */
 409                                 DEBUG(5,("Not a dir %s\n",start));
 410                                 *end = '/';
 411                                 /*
 412                                  * We need to return the fact that the
 413                                  * intermediate name resolution failed. This
 414                                  * is used to return an error of ERRbadpath
 415                                  * rather than ERRbadfile. Some Windows
 416                                  * applications depend on the difference between
 417                                  * these two errors.
 418                                  */
 419                                 result = NT_STATUS_OBJECT_PATH_NOT_FOUND;
 420                                 goto fail;
 421                         }
 422 
 423                         if (!end) {
 424                                 /*
 425                                  * We just scanned for, and found the end of
 426                                  * the path. We must return the valid stat
 427                                  * struct. JRA.
 428                                  */
 429 
 430                                 *pst = st;
 431                         }
 432 
 433                 } else {
 434                         char *found_name = NULL;
 435 
 436                         /* Stat failed - ensure we don't use it. */
 437                         SET_STAT_INVALID(st);
 438 
 439                         /*
 440                          * Reset errno so we can detect
 441                          * directory open errors.
 442                          */
 443                         errno = 0;
 444 
 445                         /*
 446                          * Try to find this part of the path in the directory.
 447                          */
 448 
 449                         if (name_has_wildcard ||
 450                             (get_real_filename(conn, dirpath, start,
 451                                                talloc_tos(),
 452                                                &found_name) == -1)) {
 453                                 char *unmangled;
 454 
 455                                 if (end) {
 456                                         /*
 457                                          * An intermediate part of the name
 458                                          * can't be found.
 459                                          */
 460                                         DEBUG(5,("Intermediate not found %s\n",
 461                                                         start));
 462                                         *end = '/';
 463 
 464                                         /*
 465                                          * We need to return the fact that the
 466                                          * intermediate name resolution failed.
 467                                          * This is used to return an error of
 468                                          * ERRbadpath rather than ERRbadfile.
 469                                          * Some Windows applications depend on
 470                                          * the difference between these two
 471                                          * errors.
 472                                          */
 473 
 474                                         /*
 475                                          * ENOENT, ENOTDIR and ELOOP all map
 476                                          * to NT_STATUS_OBJECT_PATH_NOT_FOUND
 477                                          * in the filename walk.
 478                                          */
 479 
 480                                         if (errno == ENOENT ||
 481                                                         errno == ENOTDIR ||
 482                                                         errno == ELOOP) {
 483                                                 result =
 484                                                 NT_STATUS_OBJECT_PATH_NOT_FOUND;
 485                                         }
 486                                         else {
 487                                                 result =
 488                                                 map_nt_error_from_unix(errno);
 489                                         }
 490                                         goto fail;
 491                                 }
 492 
 493                                 /*
 494                                  * ENOENT/EACCESS are the only valid errors
 495                                  * here. EACCESS needs handling here for
 496                                  * "dropboxes", i.e. directories where users
 497                                  * can only put stuff with permission -wx.
 498                                  */
 499                                 if ((errno != 0) && (errno != ENOENT)
 500                                     && (errno != EACCES)) {
 501                                         /*
 502                                          * ENOTDIR and ELOOP both map to
 503                                          * NT_STATUS_OBJECT_PATH_NOT_FOUND
 504                                          * in the filename walk.
 505                                          */
 506                                         if (errno == ENOTDIR ||
 507                                                         errno == ELOOP) {
 508                                                 result =
 509                                                 NT_STATUS_OBJECT_PATH_NOT_FOUND;
 510                                         } else {
 511                                                 result =
 512                                                 map_nt_error_from_unix(errno);
 513                                         }
 514                                         goto fail;
 515                                 }
 516 
 517                                 /*
 518                                  * Just the last part of the name doesn't exist.
 519                                  * We need to strupper() or strlower() it as
 520                                  * this conversion may be used for file creation
 521                                  * purposes. Fix inspired by
 522                                  * Thomas Neumann <t.neumann@iku-ag.de>.
 523                                  */
 524                                 if (!conn->case_preserve ||
 525                                     (mangle_is_8_3(start, False,
 526                                                    conn->params) &&
 527                                                  !conn->short_case_preserve)) {
 528                                         strnorm(start,
 529                                                 lp_defaultcase(SNUM(conn)));
 530                                 }
 531 
 532                                 /*
 533                                  * check on the mangled stack to see if we can
 534                                  * recover the base of the filename.
 535                                  */
 536 
 537                                 if (mangle_is_mangled(start, conn->params)
 538                                     && mangle_lookup_name_from_8_3(ctx,
 539                                                         start,
 540                                                         &unmangled,
 541                                                         conn->params)) {
 542                                         char *tmp;
 543                                         size_t start_ofs = start - name;
 544 
 545                                         if (*dirpath != '\0') {
 546                                                 tmp = talloc_asprintf(ctx,
 547                                                         "%s/%s", dirpath,
 548                                                         unmangled);
 549                                                 TALLOC_FREE(unmangled);
 550                                         }
 551                                         else {
 552                                                 tmp = unmangled;
 553                                         }
 554                                         if (tmp == NULL) {
 555                                                 DEBUG(0, ("talloc failed\n"));
 556                                                 return NT_STATUS_NO_MEMORY;
 557                                         }
 558                                         TALLOC_FREE(name);
 559                                         name = tmp;
 560                                         start = name + start_ofs;
 561                                         end = start + strlen(start);
 562                                 }
 563 
 564                                 DEBUG(5,("New file %s\n",start));
 565                                 goto done;
 566                         }
 567 
 568 
 569                         /*
 570                          * Restore the rest of the string. If the string was
 571                          * mangled the size may have changed.
 572                          */
 573                         if (end) {
 574                                 char *tmp;
 575                                 size_t start_ofs = start - name;
 576 
 577                                 if (*dirpath != '\0') {
 578                                         tmp = talloc_asprintf(ctx,
 579                                                 "%s/%s/%s", dirpath,
 580                                                 found_name, end+1);
 581                                 }
 582                                 else {
 583                                         tmp = talloc_asprintf(ctx,
 584                                                 "%s/%s", found_name,
 585                                                 end+1);
 586                                 }
 587                                 if (tmp == NULL) {
 588                                         DEBUG(0, ("talloc_asprintf failed\n"));
 589                                         return NT_STATUS_NO_MEMORY;
 590                                 }
 591                                 TALLOC_FREE(name);
 592                                 name = tmp;
 593                                 start = name + start_ofs;
 594                                 end = start + strlen(found_name);
 595                                 *end = '\0';
 596                         } else {
 597                                 char *tmp;
 598                                 size_t start_ofs = start - name;
 599 
 600                                 if (*dirpath != '\0') {
 601                                         tmp = talloc_asprintf(ctx,
 602                                                 "%s/%s", dirpath,
 603                                                 found_name);
 604                                 } else {
 605                                         tmp = talloc_strdup(ctx,
 606                                                 found_name);
 607                                 }
 608                                 if (tmp == NULL) {
 609                                         DEBUG(0, ("talloc failed\n"));
 610                                         return NT_STATUS_NO_MEMORY;
 611                                 }
 612                                 TALLOC_FREE(name);
 613                                 name = tmp;
 614                                 start = name + start_ofs;
 615 
 616                                 /*
 617                                  * We just scanned for, and found the end of
 618                                  * the path. We must return a valid stat struct
 619                                  * if it exists. JRA.
 620                                  */
 621 
 622                                 if (posix_pathnames) {
 623                                         ret = SMB_VFS_LSTAT(conn,name, &st);
 624                                 } else {
 625                                         ret = SMB_VFS_STAT(conn,name, &st);
 626                                 }
 627 
 628                                 if (ret == 0) {
 629                                         *pst = st;
 630                                 } else {
 631                                         SET_STAT_INVALID(st);
 632                                 }
 633                         }
 634 
 635                         TALLOC_FREE(found_name);
 636                 } /* end else */
 637 
 638 #ifdef DEVELOPER
 639                 /*
 640                  * This sucks!
 641                  * We should never provide different behaviors
 642                  * depending on DEVELOPER!!!
 643                  */
 644                 if (VALID_STAT(st)) {
 645                         bool delete_pending;
 646                         get_file_infos(vfs_file_id_from_sbuf(conn, &st),
 647                                        &delete_pending, NULL);
 648                         if (delete_pending) {
 649                                 result = NT_STATUS_DELETE_PENDING;
 650                                 goto fail;
 651                         }
 652                 }
 653 #endif
 654 
 655                 /*
 656                  * Add to the dirpath that we have resolved so far.
 657                  */
 658 
 659                 if (*dirpath != '\0') {
 660                         char *tmp = talloc_asprintf(ctx,
 661                                         "%s/%s", dirpath, start);
 662                         if (!tmp) {
 663                                 DEBUG(0, ("talloc_asprintf failed\n"));
 664                                 return NT_STATUS_NO_MEMORY;
 665                         }
 666                         TALLOC_FREE(dirpath);
 667                         dirpath = tmp;
 668                 }
 669                 else {
 670                         TALLOC_FREE(dirpath);
 671                         if (!(dirpath = talloc_strdup(ctx,start))) {
 672                                 DEBUG(0, ("talloc_strdup failed\n"));
 673                                 return NT_STATUS_NO_MEMORY;
 674                         }
 675                 }
 676 
 677                 /*
 678                  * Don't cache a name with mangled or wildcard components
 679                  * as this can change the size.
 680                  */
 681 
 682                 if(!component_was_mangled && !name_has_wildcard) {
 683                         stat_cache_add(orig_path, dirpath,
 684                                         conn->case_sensitive);
 685                 }
 686 
 687                 /*
 688                  * Restore the / that we wiped out earlier.
 689                  */
 690                 if (end) {
 691                         *end = '/';
 692                 }
 693         }
 694 
 695         /*
 696          * Don't cache a name with mangled or wildcard components
 697          * as this can change the size.
 698          */
 699 
 700         if(!component_was_mangled && !name_has_wildcard) {
 701                 stat_cache_add(orig_path, name, conn->case_sensitive);
 702         }
 703 
 704         /*
 705          * The name has been resolved.
 706          */
 707 
 708         DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
 709 
 710  done:
 711         if (stream != NULL) {
 712                 char *tmp = NULL;
 713 
 714                 result = build_stream_path(ctx, conn, orig_path, name, stream,
 715                                            pst, &tmp);
 716                 if (!NT_STATUS_IS_OK(result)) {
 717                         goto fail;
 718                 }
 719 
 720                 DEBUG(10, ("build_stream_path returned %s\n", tmp));
 721 
 722                 TALLOC_FREE(name);
 723                 name = tmp;
 724         }
 725         *pp_conv_path = name;
 726         TALLOC_FREE(dirpath);
 727         return NT_STATUS_OK;
 728  fail:
 729         DEBUG(10, ("dirpath = [%s] start = [%s]\n", dirpath, start));
 730         if (*dirpath != '\0') {
 731                 *pp_conv_path = talloc_asprintf(ctx,
 732                                 "%s/%s", dirpath, start);
 733         } else {
 734                 *pp_conv_path = talloc_strdup(ctx, start);
 735         }
 736         if (!*pp_conv_path) {
 737                 DEBUG(0, ("talloc_asprintf failed\n"));
 738                 return NT_STATUS_NO_MEMORY;
 739         }
 740         TALLOC_FREE(name);
 741         TALLOC_FREE(dirpath);
 742         return result;
 743 }
 744 
 745 /****************************************************************************
 746  Check a filename - possibly calling check_reduced_name.
 747  This is called by every routine before it allows an operation on a filename.
 748  It does any final confirmation necessary to ensure that the filename is
 749  a valid one for the user to access.
 750 ****************************************************************************/
 751 
 752 NTSTATUS check_name(connection_struct *conn, const char *name)
     /* [<][>][^][v][top][bottom][index][help] */
 753 {
 754         if (IS_VETO_PATH(conn, name))  {
 755                 /* Is it not dot or dot dot. */
 756                 if (!((name[0] == '.') && (!name[1] ||
 757                                         (name[1] == '.' && !name[2])))) {
 758                         DEBUG(5,("check_name: file path name %s vetoed\n",
 759                                                 name));
 760                         return map_nt_error_from_unix(ENOENT);
 761                 }
 762         }
 763 
 764         if (!lp_widelinks(SNUM(conn)) || !lp_symlinks(SNUM(conn))) {
 765                 NTSTATUS status = check_reduced_name(conn,name);
 766                 if (!NT_STATUS_IS_OK(status)) {
 767                         DEBUG(5,("check_name: name %s failed with %s\n",name,
 768                                                 nt_errstr(status)));
 769                         return status;
 770                 }
 771         }
 772 
 773         return NT_STATUS_OK;
 774 }
 775 
 776 /****************************************************************************
 777  Check if two filenames are equal.
 778  This needs to be careful about whether we are case sensitive.
 779 ****************************************************************************/
 780 
 781 static bool fname_equal(const char *name1, const char *name2,
     /* [<][>][^][v][top][bottom][index][help] */
 782                 bool case_sensitive)
 783 {
 784         /* Normal filename handling */
 785         if (case_sensitive) {
 786                 return(strcmp(name1,name2) == 0);
 787         }
 788 
 789         return(strequal(name1,name2));
 790 }
 791 
 792 /****************************************************************************
 793  Scan a directory to find a filename, matching without case sensitivity.
 794  If the name looks like a mangled name then try via the mangling functions
 795 ****************************************************************************/
 796 
 797 static int get_real_filename_full_scan(connection_struct *conn,
     /* [<][>][^][v][top][bottom][index][help] */
 798                                        const char *path, const char *name,
 799                                        bool mangled,
 800                                        TALLOC_CTX *mem_ctx, char **found_name)
 801 {
 802         struct smb_Dir *cur_dir;
 803         const char *dname;
 804         char *unmangled_name = NULL;
 805         long curpos;
 806 
 807         /* handle null paths */
 808         if ((path == NULL) || (*path == 0)) {
 809                 path = ".";
 810         }
 811 
 812         /* If we have a case-sensitive filesystem, it doesn't do us any
 813          * good to search for a name. If a case variation of the name was
 814          * there, then the original stat(2) would have found it.
 815          */
 816         if (!mangled && !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) {
 817                 errno = ENOENT;
 818                 return -1;
 819         }
 820 
 821         /*
 822          * The incoming name can be mangled, and if we de-mangle it
 823          * here it will not compare correctly against the filename (name2)
 824          * read from the directory and then mangled by the name_to_8_3()
 825          * call. We need to mangle both names or neither.
 826          * (JRA).
 827          *
 828          * Fix for bug found by Dina Fine. If in case sensitive mode then
 829          * the mangle cache is no good (3 letter extension could be wrong
 830          * case - so don't demangle in this case - leave as mangled and
 831          * allow the mangling of the directory entry read (which is done
 832          * case insensitively) to match instead. This will lead to more
 833          * false positive matches but we fail completely without it. JRA.
 834          */
 835 
 836         if (mangled && !conn->case_sensitive) {
 837                 mangled = !mangle_lookup_name_from_8_3(talloc_tos(), name,
 838                                                        &unmangled_name,
 839                                                        conn->params);
 840                 if (!mangled) {
 841                         /* Name is now unmangled. */
 842                         name = unmangled_name;
 843                 }
 844         }
 845 
 846         /* open the directory */
 847         if (!(cur_dir = OpenDir(talloc_tos(), conn, path, NULL, 0))) {
 848                 DEBUG(3,("scan dir didn't open dir [%s]\n",path));
 849                 TALLOC_FREE(unmangled_name);
 850                 return -1;
 851         }
 852 
 853         /* now scan for matching names */
 854         curpos = 0;
 855         while ((dname = ReadDirName(cur_dir, &curpos, NULL))) {
 856 
 857                 /* Is it dot or dot dot. */
 858                 if (ISDOT(dname) || ISDOTDOT(dname)) {
 859                         continue;
 860                 }
 861 
 862                 /*
 863                  * At this point dname is the unmangled name.
 864                  * name is either mangled or not, depending on the state
 865                  * of the "mangled" variable. JRA.
 866                  */
 867 
 868                 /*
 869                  * Check mangled name against mangled name, or unmangled name
 870                  * against unmangled name.
 871                  */
 872 
 873                 if ((mangled && mangled_equal(name,dname,conn->params)) ||
 874                         fname_equal(name, dname, conn->case_sensitive)) {
 875                         /* we've found the file, change it's name and return */
 876                         *found_name = talloc_strdup(mem_ctx, dname);
 877                         TALLOC_FREE(unmangled_name);
 878                         TALLOC_FREE(cur_dir);
 879                         if (!*found_name) {
 880                                 errno = ENOMEM;
 881                                 return -1;
 882                         }
 883                         return 0;
 884                 }
 885         }
 886 
 887         TALLOC_FREE(unmangled_name);
 888         TALLOC_FREE(cur_dir);
 889         errno = ENOENT;
 890         return -1;
 891 }
 892 
 893 /****************************************************************************
 894  Wrapper around the vfs get_real_filename and the full directory scan
 895  fallback.
 896 ****************************************************************************/
 897 
 898 int get_real_filename(connection_struct *conn, const char *path,
     /* [<][>][^][v][top][bottom][index][help] */
 899                       const char *name, TALLOC_CTX *mem_ctx,
 900                       char **found_name)
 901 {
 902         int ret;
 903         bool mangled;
 904 
 905         mangled = mangle_is_mangled(name, conn->params);
 906 
 907         if (mangled) {
 908                 return get_real_filename_full_scan(conn, path, name, mangled,
 909                                                    mem_ctx, found_name);
 910         }
 911 
 912         /* Try the vfs first to take advantage of case-insensitive stat. */
 913         ret = SMB_VFS_GET_REAL_FILENAME(conn, path, name, mem_ctx, found_name);
 914 
 915         /*
 916          * If the case-insensitive stat was successful, or returned an error
 917          * other than EOPNOTSUPP then there is no need to fall back on the
 918          * full directory scan.
 919          */
 920         if (ret == 0 || (ret == -1 && errno != EOPNOTSUPP)) {
 921                 return ret;
 922         }
 923 
 924         return get_real_filename_full_scan(conn, path, name, mangled, mem_ctx,
 925                                            found_name);
 926 }
 927 
 928 static NTSTATUS build_stream_path(TALLOC_CTX *mem_ctx,
     /* [<][>][^][v][top][bottom][index][help] */
 929                                   connection_struct *conn,
 930                                   const char *orig_path,
 931                                   const char *basepath,
 932                                   const char *streamname,
 933                                   SMB_STRUCT_STAT *pst,
 934                                   char **path)
 935 {
 936         SMB_STRUCT_STAT st;
 937         char *result = NULL;
 938         NTSTATUS status;
 939         unsigned int i, num_streams;
 940         struct stream_struct *streams = NULL;
 941 
 942         result = talloc_asprintf(mem_ctx, "%s%s", basepath, streamname);
 943         if (result == NULL) {
 944                 return NT_STATUS_NO_MEMORY;
 945         }
 946 
 947         if (SMB_VFS_STAT(conn, result, &st) == 0) {
 948                 *pst = st;
 949                 *path = result;
 950                 return NT_STATUS_OK;
 951         }
 952 
 953         if (errno != ENOENT) {
 954                 status = map_nt_error_from_unix(errno);
 955                 DEBUG(10, ("vfs_stat failed: %s\n", nt_errstr(status)));
 956                 goto fail;
 957         }
 958 
 959         status = SMB_VFS_STREAMINFO(conn, NULL, basepath, mem_ctx,
 960                                     &num_streams, &streams);
 961 
 962         if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
 963                 SET_STAT_INVALID(*pst);
 964                 *path = result;
 965                 return NT_STATUS_OK;
 966         }
 967 
 968         if (!NT_STATUS_IS_OK(status)) {
 969                 DEBUG(10, ("vfs_streaminfo failed: %s\n", nt_errstr(status)));
 970                 goto fail;
 971         }
 972 
 973         for (i=0; i<num_streams; i++) {
 974                 DEBUG(10, ("comparing [%s] and [%s]: ",
 975                            streamname, streams[i].name));
 976                 if (fname_equal(streamname, streams[i].name,
 977                                 conn->case_sensitive)) {
 978                         DEBUGADD(10, ("equal\n"));
 979                         break;
 980                 }
 981                 DEBUGADD(10, ("not equal\n"));
 982         }
 983 
 984         if (i == num_streams) {
 985                 SET_STAT_INVALID(*pst);
 986                 *path = result;
 987                 TALLOC_FREE(streams);
 988                 return NT_STATUS_OK;
 989         }
 990 
 991         TALLOC_FREE(result);
 992 
 993         result = talloc_asprintf(mem_ctx, "%s%s", basepath, streams[i].name);
 994         if (result == NULL) {
 995                 status = NT_STATUS_NO_MEMORY;
 996                 goto fail;
 997         }
 998 
 999         SET_STAT_INVALID(*pst);
1000 
1001         if (SMB_VFS_STAT(conn, result, pst) == 0) {
1002                 stat_cache_add(orig_path, result, conn->case_sensitive);
1003         }
1004 
1005         *path = result;
1006         TALLOC_FREE(streams);
1007         return NT_STATUS_OK;
1008 
1009  fail:
1010         TALLOC_FREE(result);
1011         TALLOC_FREE(streams);
1012         return status;
1013 }

/* [<][>][^][v][top][bottom][index][help] */