loadparm: make the source3/ lp_ functions take an explicit TALLOC_CTX *.
[jlayton/samba.git] / source3 / smbd / close.c
index 8b91da81d543c2563098dac83e4377fb0053aece..8633f82c0a6cec6bb0321e04e78bf745d7d19349 100644 (file)
@@ -46,7 +46,7 @@ static NTSTATUS check_magic(struct files_struct *fsp)
        char *fname = NULL;
        NTSTATUS status;
 
-       if (!*lp_magicscript(SNUM(conn))) {
+       if (!*lp_magicscript(talloc_tos(), SNUM(conn))) {
                return NT_STATUS_OK;
        }
 
@@ -62,13 +62,13 @@ static NTSTATUS check_magic(struct files_struct *fsp)
                p++;
        }
 
-       if (!strequal(lp_magicscript(SNUM(conn)),p)) {
+       if (!strequal(lp_magicscript(talloc_tos(), SNUM(conn)),p)) {
                status = NT_STATUS_OK;
                goto out;
        }
 
-       if (*lp_magicoutput(SNUM(conn))) {
-               magic_output = lp_magicoutput(SNUM(conn));
+       if (*lp_magicoutput(talloc_tos(), SNUM(conn))) {
+               magic_output = lp_magicoutput(talloc_tos(), SNUM(conn));
        } else {
                magic_output = talloc_asprintf(ctx,
                                "%s.out",
@@ -118,7 +118,7 @@ static NTSTATUS check_magic(struct files_struct *fsp)
                goto out;
        }
 
-       if (transfer_file(tmp_fd,outfd,(SMB_OFF_T)st.st_ex_size) == (SMB_OFF_T)-1) {
+       if (transfer_file(tmp_fd,outfd,(off_t)st.st_ex_size) == (off_t)-1) {
                int err = errno;
                close(tmp_fd);
                close(outfd);
@@ -170,18 +170,25 @@ static int compare_share_mode_times(const void *p1, const void *p2)
 static void notify_deferred_opens(struct smbd_server_connection *sconn,
                                  struct share_mode_lock *lck)
 {
+       struct server_id self = messaging_server_id(sconn->msg_ctx);
        uint32_t i, num_deferred;
        struct share_mode_entry *deferred;
 
-       if (!should_notify_deferred_opens()) {
+       if (!should_notify_deferred_opens(sconn)) {
                return;
        }
 
        num_deferred = 0;
        for (i=0; i<lck->data->num_share_modes; i++) {
-               if (is_deferred_open_entry(&lck->data->share_modes[i])) {
-                       num_deferred += 1;
+               struct share_mode_entry *e = &lck->data->share_modes[i];
+
+               if (!is_deferred_open_entry(e)) {
+                       continue;
                }
+               if (share_mode_stale_pid(lck->data, i)) {
+                       continue;
+               }
+               num_deferred += 1;
        }
        if (num_deferred == 0) {
                return;
@@ -225,7 +232,7 @@ static void notify_deferred_opens(struct smbd_server_connection *sconn,
        for (i=0; i<num_deferred; i++) {
                struct share_mode_entry *e = &deferred[i];
 
-               if (procid_is_me(&e->pid)) {
+               if (serverid_equal(&self, &e->pid)) {
                        /*
                         * We need to notify ourself to retry the open.  Do
                         * this by finding the queued SMB record, moving it to
@@ -325,6 +332,7 @@ static NTSTATUS close_remove_share_mode(files_struct *fsp,
                                        enum file_close_type close_type)
 {
        connection_struct *conn = fsp->conn;
+       struct server_id self = messaging_server_id(conn->sconn->msg_ctx);
        bool delete_file = false;
        bool changed_user = false;
        struct share_mode_lock *lck = NULL;
@@ -334,6 +342,7 @@ static NTSTATUS close_remove_share_mode(files_struct *fsp,
        const struct security_unix_token *del_token = NULL;
        const struct security_token *del_nt_token = NULL;
        bool got_tokens = false;
+       bool normal_close;
 
        /* Ensure any pending write time updates are done. */
        if (fsp->update_write_time_event) {
@@ -353,8 +362,7 @@ static NTSTATUS close_remove_share_mode(files_struct *fsp,
        if (lck == NULL) {
                DEBUG(0, ("close_remove_share_mode: Could not get share mode "
                          "lock for file %s\n", fsp_str_dbg(fsp)));
-               status = NT_STATUS_INVALID_PARAMETER;
-               goto done;
+               return NT_STATUS_INVALID_PARAMETER;
        }
 
        if (fsp->write_time_forced) {
@@ -379,12 +387,6 @@ static NTSTATUS close_remove_share_mode(files_struct *fsp,
                }
        }
 
-       if (!del_share_mode(lck, fsp)) {
-               DEBUG(0, ("close_remove_share_mode: Could not delete share "
-                         "entry for file %s\n",
-                         fsp_str_dbg(fsp)));
-       }
-
        if (fsp->initial_delete_on_close &&
                        !is_delete_on_close_set(lck, fsp->name_hash)) {
                bool became_user = False;
@@ -414,14 +416,26 @@ static NTSTATUS close_remove_share_mode(files_struct *fsp,
                   POSIX delete now. */
                for (i=0; i<lck->data->num_share_modes; i++) {
                        struct share_mode_entry *e = &lck->data->share_modes[i];
-                       if (is_valid_share_mode_entry(e) &&
-                                       e->name_hash == fsp->name_hash) {
-                               if (fsp->posix_open && (e->flags & SHARE_MODE_FLAG_POSIX_OPEN)) {
-                                       continue;
-                               }
-                               delete_file = False;
-                               break;
+
+                       if (!is_valid_share_mode_entry(e)) {
+                               continue;
+                       }
+                       if (e->name_hash != fsp->name_hash) {
+                               continue;
+                       }
+                       if (fsp->posix_open
+                           && (e->flags & SHARE_MODE_FLAG_POSIX_OPEN)) {
+                               continue;
                        }
+                       if (serverid_equal(&self, &e->pid) &&
+                           (e->share_file_id == fsp->fh->gen_id)) {
+                               continue;
+                       }
+                       if (share_mode_stale_pid(lck->data, i)) {
+                               continue;
+                       }
+                       delete_file = False;
+                       break;
                }
        }
 
@@ -434,8 +448,16 @@ static NTSTATUS close_remove_share_mode(files_struct *fsp,
         * reference to a file.
         */
 
-       if (!(close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE) ||
-                       !delete_file) {
+       normal_close = (close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE);
+
+       if (!normal_close || !delete_file) {
+
+               if (!del_share_mode(lck, fsp)) {
+                       DEBUG(0, ("close_remove_share_mode: Could not delete "
+                                 "share entry for file %s\n",
+                                 fsp_str_dbg(fsp)));
+               }
+
                TALLOC_FREE(lck);
                return NT_STATUS_OK;
        }
@@ -555,6 +577,11 @@ static NTSTATUS close_remove_share_mode(files_struct *fsp,
                pop_sec_ctx();
        }
 
+       if (!del_share_mode(lck, fsp)) {
+               DEBUG(0, ("close_remove_share_mode: Could not delete share "
+                         "entry for file %s\n", fsp_str_dbg(fsp)));
+       }
+
        TALLOC_FREE(lck);
 
        if (delete_file) {
@@ -616,12 +643,24 @@ static NTSTATUS update_write_time_on_close(struct files_struct *fsp)
                return NT_STATUS_OK;
        }
 
-       /* On close if we're changing the real file time we
-        * must update it in the open file db too. */
-       (void)set_write_time(fsp->file_id, fsp->close_write_time);
+       /*
+        * get_existing_share_mode_lock() isn't really the right
+        * call here, as we're being called after
+        * close_remove_share_mode() inside close_normal_file()
+        * so it's quite normal to not have an existing share
+        * mode here. However, get_share_mode_lock() doesn't
+        * work because that will create a new share mode if
+        * one doesn't exist - so stick with this call (just
+        * ignore any error we get if the share mode doesn't
+        * exist.
+        */
 
        lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
        if (lck) {
+               /* On close if we're changing the real file time we
+                * must update it in the open file db too. */
+               (void)set_write_time(fsp->file_id, fsp->close_write_time);
+
                /* Close write times overwrite sticky write times
                   so we must replace any sticky write time here. */
                if (!null_timespec(lck->data->changed_write_time)) {
@@ -667,19 +706,16 @@ static NTSTATUS close_normal_file(struct smb_request *req, files_struct *fsp,
        NTSTATUS status = NT_STATUS_OK;
        NTSTATUS tmp;
        connection_struct *conn = fsp->conn;
+       int ret;
 
-       if (close_type == ERROR_CLOSE) {
-               cancel_aio_by_fsp(fsp);
-       } else {
-               /*
-                * If we're finishing async io on a close we can get a write
-                * error here, we must remember this.
-                */
-               int ret = wait_for_aio_completion(fsp);
-               if (ret) {
-                       status = ntstatus_keeperror(
-                               status, map_nt_error_from_unix(ret));
-               }
+       /*
+        * If we're finishing async io on a close we can get a write
+        * error here, we must remember this.
+        */
+       ret = wait_for_aio_completion(fsp);
+       if (ret) {
+               status = ntstatus_keeperror(
+                       status, map_nt_error_from_unix(ret));
        }
 
        /*
@@ -870,7 +906,7 @@ static NTSTATUS rmdir_internals(TALLOC_CTX *ctx, files_struct *fsp)
                return NT_STATUS_OK;
        }
 
-       if(((errno == ENOTEMPTY)||(errno == EEXIST)) && lp_veto_files(SNUM(conn))) {
+       if(((errno == ENOTEMPTY)||(errno == EEXIST)) && *lp_veto_files(talloc_tos(), SNUM(conn))) {
                /*
                 * Check to see if the only thing in this directory are
                 * vetoed files/directories. If so then delete them and
@@ -1011,6 +1047,7 @@ static NTSTATUS rmdir_internals(TALLOC_CTX *ctx, files_struct *fsp)
 static NTSTATUS close_directory(struct smb_request *req, files_struct *fsp,
                                enum file_close_type close_type)
 {
+       struct server_id self = messaging_server_id(fsp->conn->sconn->msg_ctx);
        struct share_mode_lock *lck = NULL;
        bool delete_dir = False;
        NTSTATUS status = NT_STATUS_OK;
@@ -1027,13 +1064,7 @@ static NTSTATUS close_directory(struct smb_request *req, files_struct *fsp,
        if (lck == NULL) {
                DEBUG(0, ("close_directory: Could not get share mode lock for "
                          "%s\n", fsp_str_dbg(fsp)));
-               status = NT_STATUS_INVALID_PARAMETER;
-               goto out;
-       }
-
-       if (!del_share_mode(lck, fsp)) {
-               DEBUG(0, ("close_directory: Could not delete share entry for "
-                         "%s\n", fsp_str_dbg(fsp)));
+               return NT_STATUS_INVALID_PARAMETER;
        }
 
        if (fsp->initial_delete_on_close) {
@@ -1072,6 +1103,13 @@ static NTSTATUS close_directory(struct smb_request *req, files_struct *fsp,
                                if (fsp->posix_open && (e->flags & SHARE_MODE_FLAG_POSIX_OPEN)) {
                                        continue;
                                }
+                               if (serverid_equal(&self, &e->pid) &&
+                                   (e->share_file_id == fsp->fh->gen_id)) {
+                                       continue;
+                               }
+                               if (share_mode_stale_pid(lck->data, i)) {
+                                       continue;
+                               }
                                delete_dir = False;
                                break;
                        }
@@ -1093,6 +1131,11 @@ static NTSTATUS close_directory(struct smb_request *req, files_struct *fsp,
                                del_token->groups,
                                del_nt_token);
 
+               if (!del_share_mode(lck, fsp)) {
+                       DEBUG(0, ("close_directory: Could not delete share entry for "
+                                 "%s\n", fsp_str_dbg(fsp)));
+               }
+
                TALLOC_FREE(lck);
 
                if ((fsp->conn->fs_capabilities & FILE_NAMED_STREAMS)
@@ -1102,7 +1145,7 @@ static NTSTATUS close_directory(struct smb_request *req, files_struct *fsp,
                        if (!NT_STATUS_IS_OK(status)) {
                                DEBUG(5, ("delete_all_streams failed: %s\n",
                                          nt_errstr(status)));
-                               goto out;
+                               return status;
                        }
                }
 
@@ -1124,6 +1167,11 @@ static NTSTATUS close_directory(struct smb_request *req, files_struct *fsp,
                        remove_pending_change_notify_requests_by_fid(fsp, NT_STATUS_DELETE_PENDING);
                }
        } else {
+               if (!del_share_mode(lck, fsp)) {
+                       DEBUG(0, ("close_directory: Could not delete share entry for "
+                                 "%s\n", fsp_str_dbg(fsp)));
+               }
+
                TALLOC_FREE(lck);
                remove_pending_change_notify_requests_by_fid(
                        fsp, NT_STATUS_OK);
@@ -1143,8 +1191,6 @@ static NTSTATUS close_directory(struct smb_request *req, files_struct *fsp,
        close_filestruct(fsp);
        file_free(req, fsp);
 
- out:
-       TALLOC_FREE(lck);
        if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(status1)) {
                status = status1;
        }