LCOV - code coverage report
Current view: top level - source3/smbd - files.c (source / functions) Hit Total Coverage
Test: coverage report for master 6248eab5 Lines: 540 619 87.2 %
Date: 2021-08-25 13:27:56 Functions: 40 40 100.0 %

          Line data    Source code
       1             : /* 
       2             :    Unix SMB/CIFS implementation.
       3             :    Files[] structure handling
       4             :    Copyright (C) Andrew Tridgell 1998
       5             : 
       6             :    This program is free software; you can redistribute it and/or modify
       7             :    it under the terms of the GNU General Public License as published by
       8             :    the Free Software Foundation; either version 3 of the License, or
       9             :    (at your option) any later version.
      10             : 
      11             :    This program is distributed in the hope that it will be useful,
      12             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :    GNU General Public License for more details.
      15             : 
      16             :    You should have received a copy of the GNU General Public License
      17             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      18             : */
      19             : 
      20             : #include "includes.h"
      21             : #include "smbd/smbd.h"
      22             : #include "smbd/globals.h"
      23             : #include "libcli/security/security.h"
      24             : #include "util_tdb.h"
      25             : #include "lib/util/bitmap.h"
      26             : 
      27             : #define FILE_HANDLE_OFFSET 0x1000
      28             : 
      29             : static NTSTATUS fsp_attach_smb_fname(struct files_struct *fsp,
      30             :                                      struct smb_filename **_smb_fname);
      31             : 
      32             : /**
      33             :  * create new fsp to be used for file_new or a durable handle reconnect
      34             :  */
      35    10580396 : NTSTATUS fsp_new(struct connection_struct *conn, TALLOC_CTX *mem_ctx,
      36             :                  files_struct **result)
      37             : {
      38    10580396 :         NTSTATUS status = NT_STATUS_NO_MEMORY;
      39    10580396 :         files_struct *fsp = NULL;
      40    10580396 :         struct smbd_server_connection *sconn = conn->sconn;
      41             : 
      42    10580396 :         fsp = talloc_zero(mem_ctx, struct files_struct);
      43    10580396 :         if (fsp == NULL) {
      44           0 :                 goto fail;
      45             :         }
      46             : 
      47             :         /*
      48             :          * This can't be a child of fsp because the file_handle can be ref'd
      49             :          * when doing a dos/fcb open, which will then share the file_handle
      50             :          * across multiple fsps.
      51             :          */
      52    10580396 :         fsp->fh = fd_handle_create(mem_ctx);
      53    10580396 :         if (fsp->fh == NULL) {
      54           0 :                 goto fail;
      55             :         }
      56             : 
      57    10580396 :         fsp->fsp_flags.use_ofd_locks = !lp_smbd_force_process_locks(SNUM(conn));
      58             : #ifndef HAVE_OFD_LOCKS
      59             :         fsp->fsp_flags.use_ofd_locks = false;
      60             : #endif
      61             : 
      62    10580396 :         fh_set_refcount(fsp->fh, 1);
      63    10580396 :         fsp_set_fd(fsp, -1);
      64             : 
      65    10580396 :         fsp->fnum = FNUM_FIELD_INVALID;
      66    10580396 :         fsp->conn = conn;
      67    10580396 :         fsp->close_write_time = make_omit_timespec();
      68             : 
      69    10580396 :         DLIST_ADD(sconn->files, fsp);
      70    10580396 :         sconn->num_files += 1;
      71             : 
      72    10580396 :         conn->num_files_open++;
      73             : 
      74    10580396 :         DBG_INFO("allocated files structure (%u used)\n",
      75             :                 (unsigned int)sconn->num_files);
      76             : 
      77    10580396 :         *result = fsp;
      78    10580396 :         return NT_STATUS_OK;
      79             : 
      80           0 : fail:
      81           0 :         if (fsp != NULL) {
      82           0 :                 TALLOC_FREE(fsp->fh);
      83             :         }
      84           0 :         TALLOC_FREE(fsp);
      85             : 
      86           0 :         return status;
      87             : }
      88             : 
      89    10580396 : void fsp_set_gen_id(files_struct *fsp)
      90             : {
      91             :         static uint64_t gen_id = 1;
      92             : 
      93             :         /*
      94             :          * A billion of 64-bit increments per second gives us
      95             :          * more than 500 years of runtime without wrap.
      96             :          */
      97    10580396 :         gen_id++;
      98    10580396 :         fh_set_gen_id(fsp->fh, gen_id);
      99    10580396 : }
     100             : 
     101             : /****************************************************************************
     102             :  Find first available file slot.
     103             : ****************************************************************************/
     104             : 
     105      875259 : NTSTATUS fsp_bind_smb(struct files_struct *fsp, struct smb_request *req)
     106             : {
     107      875259 :         struct smbXsrv_open *op = NULL;
     108             :         NTTIME now;
     109             :         NTSTATUS status;
     110             : 
     111      875259 :         if (req == NULL) {
     112      320120 :                 DBG_DEBUG("INTERNAL_OPEN_ONLY, skipping smbXsrv_open\n");
     113      320120 :                 return NT_STATUS_OK;
     114             :         }
     115             : 
     116      555139 :         now = timeval_to_nttime(&fsp->open_time);
     117             : 
     118      555139 :         status = smbXsrv_open_create(req->xconn,
     119      555139 :                                      fsp->conn->session_info,
     120             :                                      now,
     121             :                                      &op);
     122      555139 :         if (!NT_STATUS_IS_OK(status)) {
     123           0 :                 return status;
     124             :         }
     125      555139 :         fsp->op = op;
     126      555139 :         op->compat = fsp;
     127      555139 :         fsp->fnum = op->local_id;
     128             : 
     129      555139 :         fsp->mid = req->mid;
     130      555139 :         req->chain_fsp = fsp;
     131             : 
     132      555139 :         DBG_DEBUG("fsp [%s] mid [%" PRIu64"]\n",
     133             :                 fsp_str_dbg(fsp), fsp->mid);
     134             : 
     135      555139 :         return NT_STATUS_OK;
     136             : }
     137             : 
     138      608239 : NTSTATUS file_new(struct smb_request *req, connection_struct *conn,
     139             :                   files_struct **result)
     140             : {
     141      608239 :         struct smbd_server_connection *sconn = conn->sconn;
     142             :         files_struct *fsp;
     143             :         NTSTATUS status;
     144             : 
     145      608239 :         status = fsp_new(conn, conn, &fsp);
     146      608239 :         if (!NT_STATUS_IS_OK(status)) {
     147           0 :                 return status;
     148             :         }
     149             : 
     150      608239 :         GetTimeOfDay(&fsp->open_time);
     151             : 
     152      608239 :         status = fsp_bind_smb(fsp, req);
     153      608239 :         if (!NT_STATUS_IS_OK(status)) {
     154           0 :                 file_free(NULL, fsp);
     155           0 :                 return status;
     156             :         }
     157             : 
     158      608239 :         fsp_set_gen_id(fsp);
     159             : 
     160             :         /*
     161             :          * Create an smb_filename with "" for the base_name.  There are very
     162             :          * few NULL checks, so make sure it's initialized with something. to
     163             :          * be safe until an audit can be done.
     164             :          */
     165      608239 :         fsp->fsp_name = synthetic_smb_fname(fsp,
     166             :                                             "",
     167             :                                             NULL,
     168             :                                             NULL,
     169             :                                             0,
     170             :                                             0);
     171      608239 :         if (fsp->fsp_name == NULL) {
     172           0 :                 file_free(NULL, fsp);
     173           0 :                 return NT_STATUS_NO_MEMORY;
     174             :         }
     175             : 
     176      608239 :         DBG_INFO("new file %s\n", fsp_fnum_dbg(fsp));
     177             : 
     178             :         /* A new fsp invalidates the positive and
     179             :           negative fsp_fi_cache as the new fsp is pushed
     180             :           at the start of the list and we search from
     181             :           a cache hit to the *end* of the list. */
     182             : 
     183      608239 :         ZERO_STRUCT(sconn->fsp_fi_cache);
     184             : 
     185      608239 :         *result = fsp;
     186      608239 :         return NT_STATUS_OK;
     187             : }
     188             : 
     189      312143 : NTSTATUS create_internal_fsp(connection_struct *conn,
     190             :                              const struct smb_filename *smb_fname,
     191             :                              struct files_struct **_fsp)
     192             : {
     193      312143 :         struct files_struct *fsp = NULL;
     194             :         NTSTATUS status;
     195             : 
     196      312143 :         status = file_new(NULL, conn, &fsp);
     197      312143 :         if (!NT_STATUS_IS_OK(status)) {
     198           0 :                 return status;
     199             :         }
     200             : 
     201      312143 :         status = fsp_set_smb_fname(fsp, smb_fname);
     202      312143 :         if (!NT_STATUS_IS_OK(status)) {
     203           0 :                 file_free(NULL, fsp);
     204           0 :                 return status;
     205             :         }
     206             : 
     207      312143 :         *_fsp = fsp;
     208      312143 :         return NT_STATUS_OK;
     209             : }
     210             : 
     211             : /*
     212             :  * Create an internal fsp for an *existing* directory.
     213             :  *
     214             :  * This should only be used by callers in the VFS that need to control the
     215             :  * opening of the directory. Otherwise use open_internal_dirfsp_at().
     216             :  */
     217      302553 : NTSTATUS create_internal_dirfsp(connection_struct *conn,
     218             :                                 const struct smb_filename *smb_dname,
     219             :                                 struct files_struct **_fsp)
     220             : {
     221      302553 :         struct files_struct *fsp = NULL;
     222             :         NTSTATUS status;
     223             : 
     224      302553 :         status = create_internal_fsp(conn, smb_dname, &fsp);
     225      302553 :         if (!NT_STATUS_IS_OK(status)) {
     226           0 :                 return status;
     227             :         }
     228             : 
     229      302553 :         fsp->access_mask = FILE_LIST_DIRECTORY;
     230      302553 :         fsp->fsp_flags.is_directory = true;
     231      302553 :         fsp->fsp_flags.is_dirfsp = true;
     232             : 
     233      302553 :         *_fsp = fsp;
     234      302553 :         return NT_STATUS_OK;
     235             : }
     236             : 
     237             : /*
     238             :  * Open an internal fsp for an *existing* directory.
     239             :  */
     240      300047 : NTSTATUS open_internal_dirfsp(connection_struct *conn,
     241             :                               const struct smb_filename *smb_dname,
     242             :                               int open_flags,
     243             :                               struct files_struct **_fsp)
     244             : {
     245      300047 :         struct files_struct *fsp = NULL;
     246             :         NTSTATUS status;
     247             :         int ret;
     248             : 
     249      300047 :         status = create_internal_dirfsp(conn, smb_dname, &fsp);
     250      300047 :         if (!NT_STATUS_IS_OK(status)) {
     251           0 :                 return status;
     252             :         }
     253             : 
     254             : #ifdef O_DIRECTORY
     255      300047 :         open_flags |= O_DIRECTORY;
     256             : #endif
     257      300047 :         status = fd_openat(conn->cwd_fsp, fsp->fsp_name, fsp, open_flags, 0);
     258      300047 :         if (!NT_STATUS_IS_OK(status)) {
     259         411 :                 DBG_INFO("Could not open fd for %s (%s)\n",
     260             :                          smb_fname_str_dbg(smb_dname),
     261             :                          nt_errstr(status));
     262         411 :                 file_free(NULL, fsp);
     263         411 :                 return status;
     264             :         }
     265             : 
     266      299636 :         ret = SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st);
     267      299636 :         if (ret != 0) {
     268           0 :                 return map_nt_error_from_unix(errno);
     269             :         }
     270             : 
     271      299636 :         if (!S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
     272           0 :                 DBG_ERR("%s is not a directory!\n",
     273             :                         smb_fname_str_dbg(smb_dname));
     274           0 :                 file_free(NULL, fsp);
     275           0 :                 return NT_STATUS_NOT_A_DIRECTORY;
     276             :         }
     277             : 
     278      299636 :         fsp->file_id = vfs_file_id_from_sbuf(conn, &fsp->fsp_name->st);
     279             : 
     280      299636 :         *_fsp = fsp;
     281      299636 :         return NT_STATUS_OK;
     282             : }
     283             : 
     284             : /*
     285             :  * The "link" in the name doesn't imply link in the filesystem
     286             :  * sense. It's a object that "links" together an fsp and an smb_fname
     287             :  * and the link allocated as talloc child of an fsp.
     288             :  *
     289             :  * The link is created for fsps that open_smbfname_fsp() returns in
     290             :  * smb_fname->fsp. When this fsp is freed by fsp_free() by some caller
     291             :  * somewhere, the destructor fsp_smb_fname_link_destructor() on the link object
     292             :  * will use the link to reset the reference in smb_fname->fsp that is about to
     293             :  * go away.
     294             :  *
     295             :  * This prevents smb_fname_internal_fsp_destructor() from seeing dangling fsp
     296             :  * pointers.
     297             :  */
     298             : 
     299             : struct fsp_smb_fname_link {
     300             :         struct fsp_smb_fname_link **smb_fname_link;
     301             :         struct files_struct **smb_fname_fsp;
     302             : };
     303             : 
     304    10581175 : static int fsp_smb_fname_link_destructor(struct fsp_smb_fname_link *link)
     305             : {
     306    10581175 :         if (link->smb_fname_link == NULL) {
     307           0 :                 return 0;
     308             :         }
     309             : 
     310    10581175 :         *link->smb_fname_link = NULL;
     311    10581175 :         *link->smb_fname_fsp = NULL;
     312    10581175 :         return 0;
     313             : }
     314             : 
     315    21199448 : static NTSTATUS fsp_smb_fname_link(struct files_struct *fsp,
     316             :                                    struct fsp_smb_fname_link **smb_fname_link,
     317             :                                    struct files_struct **smb_fname_fsp)
     318             : {
     319    21199448 :         struct fsp_smb_fname_link *link = NULL;
     320             : 
     321    21199448 :         SMB_ASSERT(*smb_fname_link == NULL);
     322    21199448 :         SMB_ASSERT(*smb_fname_fsp == NULL);
     323             : 
     324    21199448 :         link = talloc_zero(fsp, struct fsp_smb_fname_link);
     325    21199448 :         if (link == NULL) {
     326           0 :                 return NT_STATUS_NO_MEMORY;
     327             :         }
     328             : 
     329    21199448 :         link->smb_fname_link = smb_fname_link;
     330    21199448 :         link->smb_fname_fsp = smb_fname_fsp;
     331    21199448 :         *smb_fname_link = link;
     332    21199448 :         *smb_fname_fsp = fsp;
     333             : 
     334    21199448 :         talloc_set_destructor(link, fsp_smb_fname_link_destructor);
     335    21199448 :         return NT_STATUS_OK;
     336             : }
     337             : 
     338             : /*
     339             :  * Free a link, carefully avoiding to trigger the link destructor
     340             :  */
     341    11227279 : static void destroy_fsp_smb_fname_link(struct fsp_smb_fname_link **_link)
     342             : {
     343    11227279 :         struct fsp_smb_fname_link *link = *_link;
     344             : 
     345    11227279 :         if (link == NULL) {
     346      607299 :                 return;
     347             :         }
     348    10618257 :         talloc_set_destructor(link, NULL);
     349    10618257 :         TALLOC_FREE(link);
     350    10618257 :         *_link = NULL;
     351             : }
     352             : 
     353             : /*
     354             :  * Talloc destructor set on an smb_fname set by openat_pathref_fsp() used to
     355             :  * close the embedded smb_fname->fsp.
     356             :  */
     357     9639243 : static int smb_fname_fsp_destructor(struct smb_filename *smb_fname)
     358             : {
     359     9639243 :         struct files_struct *fsp = smb_fname->fsp;
     360             :         NTSTATUS status;
     361     9639243 :         int saved_errno = errno;
     362             : 
     363     9639243 :         destroy_fsp_smb_fname_link(&smb_fname->fsp_link);
     364             : 
     365     9639243 :         if (fsp == NULL) {
     366         805 :                 errno = saved_errno;
     367         805 :                 return 0;
     368             :         }
     369             : 
     370     9638438 :         if (fsp->base_fsp != NULL) {
     371        1674 :                 struct files_struct *tmp_base_fsp = fsp->base_fsp;
     372             : 
     373        1674 :                 fsp_set_base_fsp(fsp, NULL);
     374             : 
     375        1674 :                 status = fd_close(tmp_base_fsp);
     376        1674 :                 SMB_ASSERT(NT_STATUS_IS_OK(status));
     377        1674 :                 file_free(NULL, tmp_base_fsp);
     378             :         }
     379             : 
     380     9638438 :         status = fd_close(fsp);
     381     9638438 :         SMB_ASSERT(NT_STATUS_IS_OK(status));
     382     9638438 :         file_free(NULL, fsp);
     383     9638438 :         smb_fname->fsp = NULL;
     384             : 
     385     9638438 :         errno = saved_errno;
     386     9638438 :         return 0;
     387             : }
     388             : 
     389             : /*
     390             :  * For proper streams support, we have to open the base_fsp for pathref
     391             :  * fsp's as well.
     392             :  */
     393        4724 : static NTSTATUS open_pathref_base_fsp(const struct files_struct *dirfsp,
     394             :                                       struct files_struct *fsp)
     395             : {
     396        4724 :         struct smb_filename *smb_fname_base = NULL;
     397             :         NTSTATUS status;
     398             :         int ret;
     399             : 
     400       14166 :         smb_fname_base = synthetic_smb_fname(talloc_tos(),
     401        4724 :                                              fsp->fsp_name->base_name,
     402             :                                              NULL,
     403             :                                              NULL,
     404        4718 :                                              fsp->fsp_name->twrp,
     405        4724 :                                              fsp->fsp_name->flags);
     406        4724 :         if (smb_fname_base == NULL) {
     407           0 :                 return NT_STATUS_NO_MEMORY;
     408             :         }
     409             : 
     410        4724 :         ret = vfs_stat(fsp->conn, smb_fname_base);
     411        4724 :         if (ret != 0) {
     412           0 :                 return map_nt_error_from_unix(errno);
     413             :         }
     414             : 
     415        4724 :         status = openat_pathref_fsp(dirfsp, smb_fname_base);
     416        4724 :         if (!NT_STATUS_IS_OK(status)) {
     417           0 :                 TALLOC_FREE(smb_fname_base);
     418           0 :                 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
     419           0 :                         DBG_DEBUG("Opening base file failed: %s\n",
     420             :                                   nt_errstr(status));
     421           0 :                         return status;
     422             :                 }
     423           0 :                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
     424             :         }
     425             : 
     426        4724 :         fsp_set_base_fsp(fsp, smb_fname_base->fsp);
     427        4724 :         smb_fname_fsp_unlink(smb_fname_base);
     428        4724 :         TALLOC_FREE(smb_fname_base);
     429             : 
     430        4724 :         return NT_STATUS_OK;
     431             : }
     432             : 
     433             : /*
     434             :  * Open an internal O_PATH based fsp for smb_fname. If O_PATH is not
     435             :  * available, open O_RDONLY as root. Both is done in fd_open() ->
     436             :  * non_widelink_open(), triggered by setting fsp->fsp_flags.is_pathref to
     437             :  * true.
     438             :  */
     439     9973527 : NTSTATUS openat_pathref_fsp(const struct files_struct *dirfsp,
     440             :                             struct smb_filename *smb_fname)
     441             : {
     442     9973527 :         connection_struct *conn = dirfsp->conn;
     443     9973527 :         struct smb_filename *full_fname = NULL;
     444     9973527 :         struct files_struct *fsp = NULL;
     445     9973527 :         int open_flags = O_RDONLY;
     446             :         NTSTATUS status;
     447             : 
     448     9973527 :         DBG_DEBUG("smb_fname [%s]\n", smb_fname_str_dbg(smb_fname));
     449             : 
     450     9973527 :         if (smb_fname->fsp != NULL) {
     451             :                 /* We already have one for this name. */
     452         418 :                 DBG_DEBUG("smb_fname [%s] already has a pathref fsp.\n",
     453             :                         smb_fname_str_dbg(smb_fname));
     454         418 :                 return NT_STATUS_OK;
     455             :         }
     456             : 
     457     9973109 :         if (!VALID_STAT(smb_fname->st)) {
     458         340 :                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
     459             :         }
     460             : 
     461     9972769 :         if (S_ISLNK(smb_fname->st.st_ex_mode)) {
     462         748 :                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
     463             :         }
     464             : 
     465     9972021 :         status = fsp_new(conn, conn, &fsp);
     466     9972021 :         if (!NT_STATUS_IS_OK(status)) {
     467           0 :                 return status;
     468             :         }
     469             : 
     470     9972021 :         GetTimeOfDay(&fsp->open_time);
     471     9972021 :         fsp_set_gen_id(fsp);
     472     9972021 :         ZERO_STRUCT(conn->sconn->fsp_fi_cache);
     473             : 
     474     9972021 :         fsp->fsp_flags.is_pathref = true;
     475     9972021 :         if (S_ISDIR(smb_fname->st.st_ex_mode)) {
     476     8919122 :                 fsp->fsp_flags.is_directory = true;
     477     8919122 :                 open_flags |= O_DIRECTORY;
     478             :         }
     479             : 
     480     9972021 :         full_fname = full_path_from_dirfsp_atname(fsp,
     481             :                                                   dirfsp,
     482             :                                                   smb_fname);
     483     9972021 :         if (full_fname == NULL) {
     484           0 :                 status = NT_STATUS_NO_MEMORY;
     485           0 :                 goto fail;
     486             :         }
     487             : 
     488     9972021 :         if (is_ntfs_default_stream_smb_fname(full_fname)) {
     489          92 :                 full_fname->stream_name = NULL;
     490             :         }
     491             : 
     492     9972021 :         status = fsp_attach_smb_fname(fsp, &full_fname);
     493     9972021 :         if (!NT_STATUS_IS_OK(status)) {
     494           0 :                 goto fail;
     495             :         }
     496             : 
     497     9972021 :         if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
     498     9611282 :             && is_ntfs_stream_smb_fname(fsp->fsp_name))
     499             :         {
     500        4724 :                 status = open_pathref_base_fsp(dirfsp, fsp);
     501        4724 :                 if (!NT_STATUS_IS_OK(status)) {
     502           0 :                         goto fail;
     503             :                 }
     504             :         }
     505             : 
     506     9972021 :         status = fd_openat(dirfsp, smb_fname, fsp, open_flags, 0);
     507     9972021 :         if (!NT_STATUS_IS_OK(status)) {
     508      120652 :                 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND) ||
     509      120652 :                     NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_PATH_NOT_FOUND) ||
     510       60054 :                     NT_STATUS_EQUAL(status, NT_STATUS_STOPPED_ON_SYMLINK))
     511             :                 {
     512             :                         /*
     513             :                          * streams_xattr return NT_STATUS_NOT_FOUND for
     514             :                          * opens of not yet exisiting streams.
     515             :                          *
     516             :                          * ELOOP maps to NT_STATUS_OBJECT_PATH_NOT_FOUND
     517             :                          * and this will result from a open request from
     518             :                          * a POSIX client on a symlink.
     519             :                          *
     520             :                          * NT_STATUS_OBJECT_NAME_NOT_FOUND is the simple
     521             :                          * ENOENT case.
     522             :                          *
     523             :                          * NT_STATUS_STOPPED_ON_SYMLINK is returned when trying
     524             :                          * to open a symlink, our callers are not interested in
     525             :                          * this.
     526             :                          */
     527         268 :                         status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
     528             :                 }
     529       60054 :                 goto fail;
     530             :         }
     531             : 
     532     9911423 :         if (!check_same_dev_ino(&smb_fname->st, &fsp->fsp_name->st)) {
     533         484 :                 DBG_DEBUG("file [%s] - dev/ino mismatch. "
     534             :                           "Old (dev=%ju, ino=%ju). "
     535             :                           "New (dev=%ju, ino=%ju).\n",
     536             :                           smb_fname_str_dbg(smb_fname),
     537             :                           (uintmax_t)smb_fname->st.st_ex_dev,
     538             :                           (uintmax_t)smb_fname->st.st_ex_ino,
     539             :                           (uintmax_t)fsp->fsp_name->st.st_ex_dev,
     540             :                           (uintmax_t)fsp->fsp_name->st.st_ex_ino);
     541         484 :                 status = NT_STATUS_ACCESS_DENIED;
     542         484 :                 goto fail;
     543             :         }
     544             : 
     545             :         /*
     546             :          * fd_openat() has done an FSTAT on the handle
     547             :          * so update the smb_fname stat info with "truth".
     548             :          * from the handle.
     549             :          */
     550     9910939 :         smb_fname->st = fsp->fsp_name->st;
     551             : 
     552     9910939 :         fsp->file_id = vfs_file_id_from_sbuf(conn, &fsp->fsp_name->st);
     553             : 
     554     9910939 :         status = fsp_smb_fname_link(fsp,
     555             :                                     &smb_fname->fsp_link,
     556             :                                     &smb_fname->fsp);
     557     9910939 :         if (!NT_STATUS_IS_OK(status)) {
     558           0 :                 goto fail;
     559             :         }
     560             : 
     561     9910939 :         DBG_DEBUG("fsp [%s]: OK\n", fsp_str_dbg(fsp));
     562             : 
     563     9910939 :         talloc_set_destructor(smb_fname, smb_fname_fsp_destructor);
     564     9910939 :         return NT_STATUS_OK;
     565             : 
     566       60538 : fail:
     567       61082 :         DBG_DEBUG("Opening pathref for [%s] failed: %s\n",
     568             :                   smb_fname_str_dbg(smb_fname),
     569             :                   nt_errstr(status));
     570             : 
     571       61082 :         if (fsp == NULL) {
     572           0 :                 return status;
     573             :         }
     574       61082 :         if (fsp->base_fsp != NULL) {
     575           6 :                 struct files_struct *tmp_base_fsp = fsp->base_fsp;
     576             : 
     577           6 :                 fsp_set_base_fsp(fsp, NULL);
     578             : 
     579           6 :                 fd_close(tmp_base_fsp);
     580           6 :                 file_free(NULL, tmp_base_fsp);
     581             :         }
     582       61082 :         fd_close(fsp);
     583       61082 :         file_free(NULL, fsp);
     584       61082 :         return status;
     585             : }
     586             : 
     587     1404031 : void smb_fname_fsp_unlink(struct smb_filename *smb_fname)
     588             : {
     589     1404031 :         talloc_set_destructor(smb_fname, NULL);
     590     1404031 :         smb_fname->fsp = NULL;
     591     1404031 :         destroy_fsp_smb_fname_link(&smb_fname->fsp_link);
     592     1404031 : }
     593             : 
     594             : /*
     595             :  * Move any existing embedded fsp refs from the src name to the
     596             :  * destination. It's safe to call this on src smb_fname's that have no embedded
     597             :  * pathref fsp.
     598             :  */
     599      524054 : NTSTATUS move_smb_fname_fsp_link(struct smb_filename *smb_fname_dst,
     600             :                                  struct smb_filename *smb_fname_src)
     601             : {
     602             :         NTSTATUS status;
     603             : 
     604             :         /*
     605             :          * The target should always not be linked yet!
     606             :          */
     607      524054 :         SMB_ASSERT(smb_fname_dst->fsp == NULL);
     608      524054 :         SMB_ASSERT(smb_fname_dst->fsp_link == NULL);
     609             : 
     610      524054 :         if (smb_fname_src->fsp == NULL) {
     611         568 :                 return NT_STATUS_OK;
     612             :         }
     613             : 
     614      523486 :         status = fsp_smb_fname_link(smb_fname_src->fsp,
     615             :                                     &smb_fname_dst->fsp_link,
     616             :                                     &smb_fname_dst->fsp);
     617      523486 :         if (!NT_STATUS_IS_OK(status)) {
     618           0 :                 return status;
     619             :         }
     620             : 
     621      523486 :         talloc_set_destructor(smb_fname_dst, smb_fname_fsp_destructor);
     622             : 
     623      523486 :         smb_fname_fsp_unlink(smb_fname_src);
     624             : 
     625      523486 :         return NT_STATUS_OK;
     626             : }
     627             : 
     628             : /**
     629             :  * Create an smb_fname and open smb_fname->fsp pathref
     630             :  **/
     631      239303 : NTSTATUS synthetic_pathref(TALLOC_CTX *mem_ctx,
     632             :                            struct files_struct *dirfsp,
     633             :                            const char *base_name,
     634             :                            const char *stream_name,
     635             :                            const SMB_STRUCT_STAT *psbuf,
     636             :                            NTTIME twrp,
     637             :                            uint32_t flags,
     638             :                            struct smb_filename **_smb_fname)
     639             : {
     640      239303 :         struct smb_filename *smb_fname = NULL;
     641             :         NTSTATUS status;
     642             :         int ret;
     643             : 
     644      239303 :         smb_fname = synthetic_smb_fname(mem_ctx,
     645             :                                         base_name,
     646             :                                         stream_name,
     647             :                                         psbuf,
     648             :                                         twrp,
     649             :                                         flags);
     650      239303 :         if (smb_fname == NULL) {
     651           0 :                 return NT_STATUS_NO_MEMORY;
     652             :         }
     653             : 
     654      239303 :         if (!VALID_STAT(smb_fname->st)) {
     655      237747 :                 ret = vfs_stat(dirfsp->conn, smb_fname);
     656      237747 :                 if (ret != 0) {
     657      197297 :                         int err = errno;
     658      197297 :                         int lvl = err == ENOENT ? DBGLVL_INFO : DBGLVL_ERR;
     659      197297 :                         DBG_PREFIX(lvl, ("stat [%s] failed: %s\n",
     660             :                                 smb_fname_str_dbg(smb_fname),
     661             :                                 strerror(err)));
     662      197297 :                         TALLOC_FREE(smb_fname);
     663      197297 :                         return map_nt_error_from_unix(err);
     664             :                 }
     665             :         }
     666             : 
     667       42006 :         status = openat_pathref_fsp(dirfsp, smb_fname);
     668       42006 :         if (!NT_STATUS_IS_OK(status)) {
     669         154 :                 DBG_ERR("opening [%s] failed\n",
     670             :                         smb_fname_str_dbg(smb_fname));
     671         154 :                 TALLOC_FREE(smb_fname);
     672         154 :                 return status;
     673             :         }
     674             : 
     675       41852 :         *_smb_fname = smb_fname;
     676       41852 :         return NT_STATUS_OK;
     677             : }
     678             : 
     679      184005 : static int atname_destructor(struct smb_filename *smb_fname)
     680             : {
     681      184005 :         destroy_fsp_smb_fname_link(&smb_fname->fsp_link);
     682      184005 :         return 0;
     683             : }
     684             : 
     685             : /**
     686             :  * Turn a path into a parent pathref and atname
     687             :  *
     688             :  * This returns the parent pathref in _parent and the name relative to it. If
     689             :  * smb_fname was a pathref (ie smb_fname->fsp != NULL), then _atname will be a
     690             :  * pathref as well, ie _atname->fsp will point at the same fsp as
     691             :  * smb_fname->fsp.
     692             :  **/
     693      748787 : NTSTATUS parent_pathref(TALLOC_CTX *mem_ctx,
     694             :                         struct files_struct *dirfsp,
     695             :                         const struct smb_filename *smb_fname,
     696             :                         struct smb_filename **_parent,
     697             :                         struct smb_filename **_atname)
     698             : {
     699      748787 :         struct smb_filename *parent = NULL;
     700      748787 :         struct smb_filename *atname = NULL;
     701             :         NTSTATUS status;
     702             :         int ret;
     703             : 
     704      748787 :         status = SMB_VFS_PARENT_PATHNAME(dirfsp->conn,
     705             :                                          mem_ctx,
     706             :                                          smb_fname,
     707             :                                          &parent,
     708             :                                          &atname);
     709      748787 :         if (!NT_STATUS_IS_OK(status)) {
     710           0 :                 return status;
     711             :         }
     712             : 
     713             :         /*
     714             :          * We know that the parent name must
     715             :          * exist, and the name has been canonicalized
     716             :          * even if this was a POSIX pathname.
     717             :          * Ensure that we follow symlinks for
     718             :          * the parent. See the torture test
     719             :          * POSIX-SYMLINK-PARENT for details.
     720             :          */
     721      748787 :         parent->flags &= ~SMB_FILENAME_POSIX_PATH;
     722             : 
     723      748787 :         ret = vfs_stat(dirfsp->conn, parent);
     724      748787 :         if (ret != 0) {
     725         364 :                 TALLOC_FREE(parent);
     726         364 :                 return map_nt_error_from_unix(errno);
     727             :         }
     728             : 
     729      748423 :         status = openat_pathref_fsp(dirfsp, parent);
     730      748423 :         if (!NT_STATUS_IS_OK(status)) {
     731           0 :                 TALLOC_FREE(parent);
     732           0 :                 return status;
     733             :         }
     734             : 
     735      748423 :         if (smb_fname->fsp != NULL) {
     736      367797 :                 status = fsp_smb_fname_link(smb_fname->fsp,
     737      183792 :                                             &atname->fsp_link,
     738      184005 :                                             &atname->fsp);
     739      184005 :                 if (!NT_STATUS_IS_OK(status)) {
     740           0 :                         TALLOC_FREE(parent);
     741           0 :                         return status;
     742             :                 }
     743      184005 :                 talloc_set_destructor(atname, atname_destructor);
     744             :         }
     745      748423 :         *_parent = parent;
     746      748423 :         *_atname = atname;
     747      748423 :         return NT_STATUS_OK;
     748             : }
     749             : 
     750             : /****************************************************************************
     751             :  Close all open files for a connection.
     752             : ****************************************************************************/
     753             : 
     754       42420 : void file_close_conn(connection_struct *conn)
     755             : {
     756             :         files_struct *fsp, *next;
     757             : 
     758       44378 :         for (fsp=conn->sconn->files; fsp; fsp=next) {
     759        1431 :                 next = fsp->next;
     760        1431 :                 if (fsp->conn != conn) {
     761        1102 :                         continue;
     762             :                 }
     763         329 :                 if (fsp->op != NULL && fsp->op->global->durable) {
     764             :                         /*
     765             :                          * A tree disconnect closes a durable handle
     766             :                          */
     767           4 :                         fsp->op->global->durable = false;
     768             :                 }
     769         329 :                 close_file(NULL, fsp, SHUTDOWN_CLOSE);
     770             :         }
     771       42420 : }
     772             : 
     773             : /****************************************************************************
     774             :  Initialise file structures.
     775             : ****************************************************************************/
     776             : 
     777             : static int files_max_open_fds;
     778             : 
     779       27595 : bool file_init_global(void)
     780             : {
     781       27595 :         int request_max = lp_max_open_files();
     782             :         int real_lim;
     783             :         int real_max;
     784             : 
     785       27595 :         if (files_max_open_fds != 0) {
     786       26974 :                 return true;
     787             :         }
     788             : 
     789             :         /*
     790             :          * Set the max_open files to be the requested
     791             :          * max plus a fudgefactor to allow for the extra
     792             :          * fd's we need such as log files etc...
     793             :          */
     794          91 :         real_lim = set_maxfiles(request_max + MAX_OPEN_FUDGEFACTOR);
     795             : 
     796          91 :         real_max = real_lim - MAX_OPEN_FUDGEFACTOR;
     797             : 
     798          91 :         if (real_max + FILE_HANDLE_OFFSET + MAX_OPEN_PIPES > 65536) {
     799           0 :                 real_max = 65536 - FILE_HANDLE_OFFSET - MAX_OPEN_PIPES;
     800             :         }
     801             : 
     802          91 :         if (real_max != request_max) {
     803           0 :                 DEBUG(1, ("file_init_global: Information only: requested %d "
     804             :                           "open files, %d are available.\n",
     805             :                           request_max, real_max));
     806             :         }
     807             : 
     808          91 :         SMB_ASSERT(real_max > 100);
     809             : 
     810          91 :         files_max_open_fds = real_max;
     811          91 :         return true;
     812             : }
     813             : 
     814       27520 : bool file_init(struct smbd_server_connection *sconn)
     815             : {
     816             :         bool ok;
     817             : 
     818       27520 :         ok = file_init_global();
     819       27520 :         if (!ok) {
     820           0 :                 return false;
     821             :         }
     822             : 
     823       27520 :         sconn->real_max_open_files = files_max_open_fds;
     824             : 
     825       27520 :         return true;
     826             : }
     827             : 
     828             : /****************************************************************************
     829             :  Close files open by a specified vuid.
     830             : ****************************************************************************/
     831             : 
     832       26915 : void file_close_user(struct smbd_server_connection *sconn, uint64_t vuid)
     833             : {
     834             :         files_struct *fsp, *next;
     835             : 
     836       29282 :         for (fsp=sconn->files; fsp; fsp=next) {
     837        1845 :                 next=fsp->next;
     838        1845 :                 if (fsp->vuid == vuid) {
     839        1751 :                         close_file(NULL, fsp, SHUTDOWN_CLOSE);
     840             :                 }
     841             :         }
     842       26915 : }
     843             : 
     844             : /*
     845             :  * Walk the files table until "fn" returns non-NULL
     846             :  */
     847             : 
     848      146170 : struct files_struct *files_forall(
     849             :         struct smbd_server_connection *sconn,
     850             :         struct files_struct *(*fn)(struct files_struct *fsp,
     851             :                                    void *private_data),
     852             :         void *private_data)
     853             : {
     854             :         struct files_struct *fsp, *next;
     855             : 
     856      337049 :         for (fsp = sconn->files; fsp; fsp = next) {
     857             :                 struct files_struct *ret;
     858      192607 :                 next = fsp->next;
     859      192607 :                 ret = fn(fsp, private_data);
     860      192607 :                 if (ret != NULL) {
     861        1961 :                         return ret;
     862             :                 }
     863             :         }
     864      143976 :         return NULL;
     865             : }
     866             : 
     867             : /****************************************************************************
     868             :  Find a fsp given a file descriptor.
     869             : ****************************************************************************/
     870             : 
     871           4 : files_struct *file_find_fd(struct smbd_server_connection *sconn, int fd)
     872             : {
     873           4 :         int count=0;
     874             :         files_struct *fsp;
     875             : 
     876           4 :         for (fsp=sconn->files; fsp; fsp=fsp->next,count++) {
     877           4 :                 if (fsp_get_pathref_fd(fsp) == fd) {
     878           4 :                         if (count > 10) {
     879           0 :                                 DLIST_PROMOTE(sconn->files, fsp);
     880             :                         }
     881           4 :                         return fsp;
     882             :                 }
     883             :         }
     884             : 
     885           0 :         return NULL;
     886             : }
     887             : 
     888             : /****************************************************************************
     889             :  Find a fsp given a device, inode and file_id.
     890             : ****************************************************************************/
     891             : 
     892       14739 : files_struct *file_find_dif(struct smbd_server_connection *sconn,
     893             :                             struct file_id id, unsigned long gen_id)
     894             : {
     895       14739 :         int count=0;
     896             :         files_struct *fsp;
     897             : 
     898       14739 :         if (gen_id == 0) {
     899           0 :                 return NULL;
     900             :         }
     901             : 
     902      202653 :         for (fsp = sconn->files; fsp; fsp = fsp->next,count++) {
     903             :                 /*
     904             :                  * We can have a fsp->fh->fd == -1 here as it could be a stat
     905             :                  * open.
     906             :                  */
     907      202653 :                 if (!file_id_equal(&fsp->file_id, &id)) {
     908       16656 :                         continue;
     909             :                 }
     910      185997 :                 if (!fsp->fsp_flags.is_fsa) {
     911       14696 :                         continue;
     912             :                 }
     913      171301 :                 if (fh_get_gen_id(fsp->fh) != gen_id) {
     914      156562 :                         continue;
     915             :                 }
     916       14739 :                 if (count > 10) {
     917        4646 :                         DLIST_PROMOTE(sconn->files, fsp);
     918             :                 }
     919             :                 /* Paranoia check. */
     920       14739 :                 if ((fsp_get_pathref_fd(fsp) == -1) &&
     921           0 :                     (fsp->oplock_type != NO_OPLOCK &&
     922           0 :                      fsp->oplock_type != LEASE_OPLOCK))
     923             :                 {
     924             :                         struct file_id_buf idbuf;
     925             : 
     926           0 :                         DBG_ERR("file %s file_id = "
     927             :                                 "%s, gen = %u oplock_type = %u is a "
     928             :                                 "stat open with oplock type !\n",
     929             :                                 fsp_str_dbg(fsp),
     930             :                                 file_id_str_buf(fsp->file_id, &idbuf),
     931             :                                 (unsigned int)fh_get_gen_id(fsp->fh),
     932             :                                 (unsigned int)fsp->oplock_type);
     933           0 :                         smb_panic("file_find_dif");
     934             :                 }
     935       14680 :                 return fsp;
     936             :         }
     937             : 
     938           0 :         return NULL;
     939             : }
     940             : 
     941             : /****************************************************************************
     942             :  Find the first fsp given a device and inode.
     943             :  We use a singleton cache here to speed up searching from getfilepathinfo
     944             :  calls.
     945             : ****************************************************************************/
     946             : 
     947       11564 : files_struct *file_find_di_first(struct smbd_server_connection *sconn,
     948             :                                  struct file_id id,
     949             :                                  bool need_fsa)
     950             : {
     951             :         files_struct *fsp;
     952             : 
     953       11564 :         if (file_id_equal(&sconn->fsp_fi_cache.id, &id)) {
     954             :                 /* Positive or negative cache hit. */
     955           0 :                 return sconn->fsp_fi_cache.fsp;
     956             :         }
     957             : 
     958       11564 :         sconn->fsp_fi_cache.id = id;
     959             : 
     960       25152 :         for (fsp=sconn->files;fsp;fsp=fsp->next) {
     961       16614 :                 if (need_fsa && !fsp->fsp_flags.is_fsa) {
     962        8885 :                         continue;
     963             :                 }
     964        7729 :                 if (file_id_equal(&fsp->file_id, &id)) {
     965             :                         /* Setup positive cache. */
     966        3026 :                         sconn->fsp_fi_cache.fsp = fsp;
     967        3026 :                         return fsp;
     968             :                 }
     969             :         }
     970             : 
     971             :         /* Setup negative cache. */
     972        8538 :         sconn->fsp_fi_cache.fsp = NULL;
     973        8538 :         return NULL;
     974             : }
     975             : 
     976             : /****************************************************************************
     977             :  Find the next fsp having the same device and inode.
     978             : ****************************************************************************/
     979             : 
     980        1290 : files_struct *file_find_di_next(files_struct *start_fsp,
     981             :                                 bool need_fsa)
     982             : {
     983             :         files_struct *fsp;
     984             : 
     985        1484 :         for (fsp = start_fsp->next;fsp;fsp=fsp->next) {
     986         347 :                 if (need_fsa && !fsp->fsp_flags.is_fsa) {
     987           0 :                         continue;
     988             :                 }
     989         347 :                 if (file_id_equal(&fsp->file_id, &start_fsp->file_id)) {
     990         148 :                         return fsp;
     991             :                 }
     992             :         }
     993             : 
     994        1118 :         return NULL;
     995             : }
     996             : 
     997           4 : struct files_struct *file_find_one_fsp_from_lease_key(
     998             :         struct smbd_server_connection *sconn,
     999             :         const struct smb2_lease_key *lease_key)
    1000             : {
    1001             :         struct files_struct *fsp;
    1002             : 
    1003           4 :         for (fsp = sconn->files; fsp; fsp=fsp->next) {
    1004           8 :                 if ((fsp->lease != NULL) &&
    1005           4 :                     (fsp->lease->lease.lease_key.data[0] ==
    1006           8 :                      lease_key->data[0]) &&
    1007           4 :                     (fsp->lease->lease.lease_key.data[1] ==
    1008           4 :                      lease_key->data[1])) {
    1009           4 :                         return fsp;
    1010             :                 }
    1011             :         }
    1012           0 :         return NULL;
    1013             : }
    1014             : 
    1015             : /****************************************************************************
    1016             :  Find any fsp open with a pathname below that of an already open path.
    1017             : ****************************************************************************/
    1018             : 
    1019          79 : bool file_find_subpath(files_struct *dir_fsp)
    1020             : {
    1021             :         files_struct *fsp;
    1022             :         size_t dlen;
    1023          79 :         char *d_fullname = NULL;
    1024             : 
    1025         158 :         d_fullname = talloc_asprintf(talloc_tos(), "%s/%s",
    1026          79 :                                      dir_fsp->conn->connectpath,
    1027          79 :                                      dir_fsp->fsp_name->base_name);
    1028             : 
    1029          79 :         if (!d_fullname) {
    1030           0 :                 return false;
    1031             :         }
    1032             : 
    1033          79 :         dlen = strlen(d_fullname);
    1034             : 
    1035         288 :         for (fsp=dir_fsp->conn->sconn->files; fsp; fsp=fsp->next) {
    1036             :                 char *d1_fullname;
    1037             : 
    1038         214 :                 if (fsp == dir_fsp) {
    1039          77 :                         continue;
    1040             :                 }
    1041             : 
    1042         274 :                 d1_fullname = talloc_asprintf(talloc_tos(),
    1043             :                                         "%s/%s",
    1044         137 :                                         fsp->conn->connectpath,
    1045         137 :                                         fsp->fsp_name->base_name);
    1046             : 
    1047             :                 /*
    1048             :                  * If the open file has a path that is a longer
    1049             :                  * component, then it's a subpath.
    1050             :                  */
    1051         154 :                 if (strnequal(d_fullname, d1_fullname, dlen) &&
    1052          17 :                                 (d1_fullname[dlen] == '/')) {
    1053           5 :                         TALLOC_FREE(d1_fullname);
    1054           5 :                         TALLOC_FREE(d_fullname);
    1055           5 :                         return true;
    1056             :                 }
    1057         132 :                 TALLOC_FREE(d1_fullname);
    1058             :         }
    1059             : 
    1060          74 :         TALLOC_FREE(d_fullname);
    1061          74 :         return false;
    1062             : }
    1063             : 
    1064             : /****************************************************************************
    1065             :  Free up a fsp.
    1066             : ****************************************************************************/
    1067             : 
    1068    10580380 : static void fsp_free(files_struct *fsp)
    1069             : {
    1070    10580380 :         struct smbd_server_connection *sconn = fsp->conn->sconn;
    1071             : 
    1072    10580380 :         if (fsp == sconn->fsp_fi_cache.fsp) {
    1073        1130 :                 ZERO_STRUCT(sconn->fsp_fi_cache);
    1074             :         }
    1075             : 
    1076    10580380 :         DLIST_REMOVE(sconn->files, fsp);
    1077    10580380 :         SMB_ASSERT(sconn->num_files > 0);
    1078    10580380 :         sconn->num_files--;
    1079             : 
    1080    10580380 :         TALLOC_FREE(fsp->fake_file_handle);
    1081             : 
    1082    10580380 :         if (fh_get_refcount(fsp->fh) == 1) {
    1083    10580267 :                 TALLOC_FREE(fsp->fh);
    1084             :         } else {
    1085         113 :                 size_t new_refcount = fh_get_refcount(fsp->fh) - 1;
    1086         113 :                 fh_set_refcount(fsp->fh, new_refcount);
    1087             :         }
    1088             : 
    1089    10580380 :         if (fsp->lease != NULL) {
    1090        1006 :                 if (fsp->lease->ref_count == 1) {
    1091         794 :                         TALLOC_FREE(fsp->lease);
    1092             :                 } else {
    1093         212 :                         fsp->lease->ref_count--;
    1094             :                 }
    1095             :         }
    1096             : 
    1097    10580380 :         fsp->conn->num_files_open--;
    1098             : 
    1099    21160752 :         if (fsp->fsp_name != NULL &&
    1100    10580372 :             fsp->fsp_name->fsp_link != NULL)
    1101             :         {
    1102             :                 /*
    1103             :                  * Free fsp_link of fsp->fsp_name. To do this in the correct
    1104             :                  * talloc destructor order we have to do it here. The
    1105             :                  * talloc_free() of the link should set the fsp pointer to NULL.
    1106             :                  */
    1107    10580344 :                 TALLOC_FREE(fsp->fsp_name->fsp_link);
    1108    10580344 :                 SMB_ASSERT(fsp->fsp_name->fsp == NULL);
    1109             :         }
    1110             : 
    1111             :         /* this is paranoia, just in case someone tries to reuse the
    1112             :            information */
    1113    10580380 :         ZERO_STRUCTP(fsp);
    1114             : 
    1115             :         /* fsp->fsp_name is a talloc child and is free'd automatically. */
    1116    10580380 :         TALLOC_FREE(fsp);
    1117    10580380 : }
    1118             : 
    1119    10580380 : void file_free(struct smb_request *req, files_struct *fsp)
    1120             : {
    1121    10580380 :         struct smbd_server_connection *sconn = fsp->conn->sconn;
    1122    10580380 :         uint64_t fnum = fsp->fnum;
    1123             : 
    1124    10580380 :         if (fsp == fsp->conn->cwd_fsp) {
    1125           0 :                 return;
    1126             :         }
    1127             : 
    1128    10580380 :         if (fsp->notify) {
    1129        1850 :                 size_t len = fsp_fullbasepath(fsp, NULL, 0);
    1130        1850 :                 char fullpath[len+1];
    1131             : 
    1132        1850 :                 fsp_fullbasepath(fsp, fullpath, sizeof(fullpath));
    1133             : 
    1134             :                 /*
    1135             :                  * Avoid /. at the end of the path name. notify can't
    1136             :                  * deal with it.
    1137             :                  */
    1138        1930 :                 if (len > 1 && fullpath[len-1] == '.' &&
    1139          80 :                     fullpath[len-2] == '/') {
    1140          80 :                         fullpath[len-2] = '\0';
    1141             :                 }
    1142             : 
    1143        1850 :                 notify_remove(fsp->conn->sconn->notify_ctx, fsp, fullpath);
    1144        1850 :                 TALLOC_FREE(fsp->notify);
    1145             :         }
    1146             : 
    1147             :         /* Ensure this event will never fire. */
    1148    10580380 :         TALLOC_FREE(fsp->update_write_time_event);
    1149             : 
    1150    10580380 :         if (fsp->op != NULL) {
    1151      555251 :                 fsp->op->compat = NULL;
    1152             :         }
    1153    10580380 :         TALLOC_FREE(fsp->op);
    1154             : 
    1155    10580380 :         if ((req != NULL) && (fsp == req->chain_fsp)) {
    1156      545815 :                 req->chain_fsp = NULL;
    1157             :         }
    1158             : 
    1159             :         /*
    1160             :          * Clear all possible chained fsp
    1161             :          * pointers in the SMB2 request queue.
    1162             :          */
    1163    10580380 :         remove_smb2_chained_fsp(fsp);
    1164             : 
    1165             :         /* Drop all remaining extensions. */
    1166    10580380 :         vfs_remove_all_fsp_extensions(fsp);
    1167             : 
    1168    10580380 :         fsp_free(fsp);
    1169             : 
    1170    10580380 :         DEBUG(5,("freed files structure %llu (%u used)\n",
    1171             :                  (unsigned long long)fnum, (unsigned int)sconn->num_files));
    1172             : }
    1173             : 
    1174             : /****************************************************************************
    1175             :  Get an fsp from a packet given a 16 bit fnum.
    1176             : ****************************************************************************/
    1177             : 
    1178      210623 : files_struct *file_fsp(struct smb_request *req, uint16_t fid)
    1179             : {
    1180             :         struct smbXsrv_open *op;
    1181             :         NTSTATUS status;
    1182      210623 :         NTTIME now = 0;
    1183             :         files_struct *fsp;
    1184             : 
    1185      210623 :         if (req == NULL) {
    1186             :                 /*
    1187             :                  * We should never get here. req==NULL could in theory
    1188             :                  * only happen from internal opens with a non-zero
    1189             :                  * root_dir_fid. Internal opens just don't do that, at
    1190             :                  * least they are not supposed to do so. And if they
    1191             :                  * start to do so, they better fake up a smb_request
    1192             :                  * from which we get the right smbd_server_conn. While
    1193             :                  * this should never happen, let's return NULL here.
    1194             :                  */
    1195           0 :                 return NULL;
    1196             :         }
    1197             : 
    1198      210623 :         if (req->chain_fsp != NULL) {
    1199          96 :                 if (req->chain_fsp->fsp_flags.closing) {
    1200           0 :                         return NULL;
    1201             :                 }
    1202          96 :                 return req->chain_fsp;
    1203             :         }
    1204             : 
    1205      210527 :         if (req->xconn == NULL) {
    1206           0 :                 return NULL;
    1207             :         }
    1208             : 
    1209      210527 :         now = timeval_to_nttime(&req->request_time);
    1210             : 
    1211      210527 :         status = smb1srv_open_lookup(req->xconn,
    1212             :                                      fid, now, &op);
    1213      210527 :         if (!NT_STATUS_IS_OK(status)) {
    1214        2686 :                 return NULL;
    1215             :         }
    1216             : 
    1217      207782 :         fsp = op->compat;
    1218      207782 :         if (fsp == NULL) {
    1219           0 :                 return NULL;
    1220             :         }
    1221             : 
    1222      207782 :         if (fsp->fsp_flags.closing) {
    1223           0 :                 return NULL;
    1224             :         }
    1225             : 
    1226      207782 :         req->chain_fsp = fsp;
    1227      207782 :         return fsp;
    1228             : }
    1229             : 
    1230      800388 : struct files_struct *file_fsp_get(struct smbd_smb2_request *smb2req,
    1231             :                                   uint64_t persistent_id,
    1232             :                                   uint64_t volatile_id)
    1233             : {
    1234             :         struct smbXsrv_open *op;
    1235             :         NTSTATUS status;
    1236      800388 :         NTTIME now = 0;
    1237             :         struct files_struct *fsp;
    1238             : 
    1239      800388 :         now = timeval_to_nttime(&smb2req->request_time);
    1240             : 
    1241      800388 :         status = smb2srv_open_lookup(smb2req->xconn,
    1242             :                                      persistent_id, volatile_id,
    1243             :                                      now, &op);
    1244      800388 :         if (!NT_STATUS_IS_OK(status)) {
    1245       12454 :                 return NULL;
    1246             :         }
    1247             : 
    1248      787934 :         fsp = op->compat;
    1249      787934 :         if (fsp == NULL) {
    1250           0 :                 return NULL;
    1251             :         }
    1252             : 
    1253      787934 :         if (smb2req->tcon == NULL) {
    1254           0 :                 return NULL;
    1255             :         }
    1256             : 
    1257      787934 :         if (smb2req->tcon->compat != fsp->conn) {
    1258           4 :                 return NULL;
    1259             :         }
    1260             : 
    1261      787930 :         if (smb2req->session == NULL) {
    1262           0 :                 return NULL;
    1263             :         }
    1264             : 
    1265      787930 :         if (smb2req->session->global->session_wire_id != fsp->vuid) {
    1266           0 :                 return NULL;
    1267             :         }
    1268             : 
    1269      787930 :         if (fsp->fsp_flags.closing) {
    1270           0 :                 return NULL;
    1271             :         }
    1272             : 
    1273      787930 :         return fsp;
    1274             : }
    1275             : 
    1276     1588184 : struct files_struct *file_fsp_smb2(struct smbd_smb2_request *smb2req,
    1277             :                                    uint64_t persistent_id,
    1278             :                                    uint64_t volatile_id)
    1279             : {
    1280             :         struct files_struct *fsp;
    1281             : 
    1282     1588184 :         if (smb2req->compat_chain_fsp != NULL) {
    1283      787796 :                 if (smb2req->compat_chain_fsp->fsp_flags.closing) {
    1284           0 :                         return NULL;
    1285             :                 }
    1286      787796 :                 return smb2req->compat_chain_fsp;
    1287             :         }
    1288             : 
    1289      800388 :         fsp = file_fsp_get(smb2req, persistent_id, volatile_id);
    1290      800388 :         if (fsp == NULL) {
    1291       12458 :                 return NULL;
    1292             :         }
    1293             : 
    1294      787930 :         smb2req->compat_chain_fsp = fsp;
    1295      787930 :         return fsp;
    1296             : }
    1297             : 
    1298             : /****************************************************************************
    1299             :  Duplicate the file handle part for a DOS or FCB open.
    1300             : ****************************************************************************/
    1301             : 
    1302         113 : NTSTATUS dup_file_fsp(
    1303             :         struct smb_request *req,
    1304             :         files_struct *from,
    1305             :         uint32_t access_mask,
    1306             :         uint32_t create_options,
    1307             :         files_struct *to)
    1308             : {
    1309             :         size_t new_refcount;
    1310             : 
    1311             :         /* this can never happen for print files */
    1312         113 :         SMB_ASSERT(from->print_file == NULL);
    1313             : 
    1314         113 :         TALLOC_FREE(to->fh);
    1315             : 
    1316         113 :         to->fh = from->fh;
    1317         113 :         new_refcount = fh_get_refcount(to->fh) + 1;
    1318         113 :         fh_set_refcount(to->fh, new_refcount);
    1319             : 
    1320         113 :         to->file_id = from->file_id;
    1321         113 :         to->initial_allocation_size = from->initial_allocation_size;
    1322         113 :         to->file_pid = from->file_pid;
    1323         113 :         to->vuid = from->vuid;
    1324         113 :         to->open_time = from->open_time;
    1325         113 :         to->access_mask = access_mask;
    1326         113 :         to->oplock_type = from->oplock_type;
    1327         113 :         to->fsp_flags.can_lock = from->fsp_flags.can_lock;
    1328         113 :         to->fsp_flags.can_read = ((access_mask & FILE_READ_DATA) != 0);
    1329         113 :         to->fsp_flags.can_write =
    1330         226 :                 CAN_WRITE(from->conn) &&
    1331         113 :                 ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) != 0);
    1332         113 :         to->fsp_flags.modified = from->fsp_flags.modified;
    1333         113 :         to->fsp_flags.is_directory = from->fsp_flags.is_directory;
    1334         113 :         to->fsp_flags.aio_write_behind = from->fsp_flags.aio_write_behind;
    1335         113 :         to->fsp_flags.is_fsa = from->fsp_flags.is_fsa;
    1336         113 :         to->fsp_flags.is_pathref = from->fsp_flags.is_pathref;
    1337         113 :         to->fsp_flags.have_proc_fds = from->fsp_flags.have_proc_fds;
    1338         113 :         to->fsp_flags.is_dirfsp = from->fsp_flags.is_dirfsp;
    1339             : 
    1340         113 :         return fsp_set_smb_fname(to, from->fsp_name);
    1341             : }
    1342             : 
    1343             : /**
    1344             :  * Return a jenkins hash of a pathname on a connection.
    1345             :  */
    1346             : 
    1347    10709708 : NTSTATUS file_name_hash(connection_struct *conn,
    1348             :                         const char *name, uint32_t *p_name_hash)
    1349             : {
    1350             :         char tmpbuf[PATH_MAX];
    1351             :         char *fullpath, *to_free;
    1352             :         ssize_t len;
    1353             :         TDB_DATA key;
    1354             : 
    1355             :         /* Set the hash of the full pathname. */
    1356             : 
    1357    10709708 :         if (name[0] == '/') {
    1358     3540648 :                 strlcpy(tmpbuf, name, sizeof(tmpbuf));
    1359     3540648 :                 fullpath = tmpbuf;
    1360     3540648 :                 len = strlen(fullpath);
    1361     3540648 :                 to_free = NULL;
    1362             :         } else {
    1363     7169060 :                 len = full_path_tos(conn->connectpath,
    1364             :                                     name,
    1365             :                                     tmpbuf,
    1366             :                                     sizeof(tmpbuf),
    1367             :                                     &fullpath,
    1368             :                                     &to_free);
    1369             :         }
    1370    10709708 :         if (len == -1) {
    1371           0 :                 return NT_STATUS_NO_MEMORY;
    1372             :         }
    1373    10709708 :         key = (TDB_DATA) { .dptr = (uint8_t *)fullpath, .dsize = len+1 };
    1374    10709708 :         *p_name_hash = tdb_jenkins_hash(&key);
    1375             : 
    1376    10709708 :         DEBUG(10,("file_name_hash: %s hash 0x%x\n",
    1377             :                   fullpath,
    1378             :                 (unsigned int)*p_name_hash ));
    1379             : 
    1380    10709708 :         TALLOC_FREE(to_free);
    1381    10709708 :         return NT_STATUS_OK;
    1382             : }
    1383             : 
    1384    10581018 : static NTSTATUS fsp_attach_smb_fname(struct files_struct *fsp,
    1385             :                                      struct smb_filename **_smb_fname)
    1386             : {
    1387    10581018 :         struct smb_filename *smb_fname_new = *_smb_fname;
    1388    10581018 :         const char *name_str = NULL;
    1389    10581018 :         uint32_t name_hash = 0;
    1390             :         NTSTATUS status;
    1391             : 
    1392    10581018 :         name_str = smb_fname_str_dbg(smb_fname_new);
    1393    10581018 :         if (name_str == NULL) {
    1394           0 :                 return NT_STATUS_NO_MEMORY;
    1395             :         }
    1396             : 
    1397    10581018 :         status = file_name_hash(fsp->conn,
    1398             :                                 name_str,
    1399             :                                 &name_hash);
    1400    10581018 :         if (!NT_STATUS_IS_OK(status)) {
    1401           0 :                 return status;
    1402             :         }
    1403             : 
    1404    10581018 :         status = fsp_smb_fname_link(fsp,
    1405             :                                     &smb_fname_new->fsp_link,
    1406             :                                     &smb_fname_new->fsp);
    1407    10581018 :         if (!NT_STATUS_IS_OK(status)) {
    1408           0 :                 return status;
    1409             :         }
    1410             : 
    1411    10581018 :         fsp->name_hash = name_hash;
    1412    10581018 :         fsp->fsp_name = smb_fname_new;
    1413    10581018 :         *_smb_fname = NULL;
    1414    10581018 :         return NT_STATUS_OK;
    1415             : }
    1416             : 
    1417             : /**
    1418             :  * The only way that the fsp->fsp_name field should ever be set.
    1419             :  */
    1420      608997 : NTSTATUS fsp_set_smb_fname(struct files_struct *fsp,
    1421             :                            const struct smb_filename *smb_fname_in)
    1422             : {
    1423      608997 :         struct smb_filename *smb_fname_old = fsp->fsp_name;
    1424      608997 :         struct smb_filename *smb_fname_new = NULL;
    1425             :         NTSTATUS status;
    1426             : 
    1427      608997 :         smb_fname_new = cp_smb_filename(fsp, smb_fname_in);
    1428      608997 :         if (smb_fname_new == NULL) {
    1429           0 :                 return NT_STATUS_NO_MEMORY;
    1430             :         }
    1431             : 
    1432      608997 :         status = fsp_attach_smb_fname(fsp, &smb_fname_new);
    1433      608997 :         if (!NT_STATUS_IS_OK(status)) {
    1434           0 :                 TALLOC_FREE(smb_fname_new);
    1435           0 :                 return status;
    1436             :         }
    1437             : 
    1438      608997 :         if (smb_fname_old != NULL) {
    1439      608869 :                 smb_fname_fsp_unlink(smb_fname_old);
    1440      608869 :                 TALLOC_FREE(smb_fname_old);
    1441             :         }
    1442             : 
    1443      608997 :         return NT_STATUS_OK;
    1444             : }
    1445             : 
    1446        7402 : size_t fsp_fullbasepath(struct files_struct *fsp, char *buf, size_t buflen)
    1447             : {
    1448        7402 :         int len = 0;
    1449        7402 :         char tmp_buf[1] = {'\0'};
    1450             : 
    1451             :         /*
    1452             :          * Don't pass NULL buffer to snprintf (to satisfy static checker)
    1453             :          * Some callers will call this function with NULL for buf and
    1454             :          * 0 for buflen in order to get length of fullbasepath (without
    1455             :          * needing to allocate or write to buf)
    1456             :          */
    1457        7402 :         if (buf == NULL) {
    1458        3702 :                 buf = tmp_buf;
    1459             :         }
    1460             : 
    1461        7402 :         len = snprintf(buf, buflen, "%s/%s", fsp->conn->connectpath,
    1462        7402 :                        fsp->fsp_name->base_name);
    1463        7402 :         SMB_ASSERT(len>0);
    1464             : 
    1465        7402 :         return len;
    1466             : }
    1467             : 
    1468       16449 : void fsp_set_base_fsp(struct files_struct *fsp, struct files_struct *base_fsp)
    1469             : {
    1470       16449 :         SMB_ASSERT(fsp->stream_fsp == NULL);
    1471       16449 :         if (base_fsp != NULL) {
    1472       11725 :                 SMB_ASSERT(base_fsp->base_fsp == NULL);
    1473       11725 :                 SMB_ASSERT(base_fsp->stream_fsp == NULL);
    1474             :         }
    1475             : 
    1476       16449 :         if (fsp->base_fsp != NULL) {
    1477        4724 :                 SMB_ASSERT(fsp->base_fsp->stream_fsp == fsp);
    1478        4724 :                 fsp->base_fsp->stream_fsp = NULL;
    1479             :         }
    1480             : 
    1481       16449 :         fsp->base_fsp = base_fsp;
    1482       16449 :         if (fsp->base_fsp != NULL) {
    1483       11725 :                 fsp->base_fsp->stream_fsp = fsp;
    1484             :         }
    1485       16449 : }

Generated by: LCOV version 1.13