2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 1992-2007.
6 Copyright (C) Volker Lendecke 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "system/filesys.h"
25 #include "smbd/smbd.h"
26 #include "smbd/globals.h"
27 #include "smbd/scavenger.h"
28 #include "fake_file.h"
29 #include "transfer_file.h"
32 #include "../librpc/gen_ndr/open_files.h"
34 /****************************************************************************
35 Run a file if it is a magic script.
36 ****************************************************************************/
38 static NTSTATUS check_magic(struct files_struct *fsp)
41 const char *magic_output = NULL;
44 TALLOC_CTX *ctx = NULL;
46 struct connection_struct *conn = fsp->conn;
50 if (!*lp_magic_script(talloc_tos(), SNUM(conn))) {
54 DEBUG(5,("checking magic for %s\n", fsp_str_dbg(fsp)));
56 ctx = talloc_stackframe();
58 fname = fsp->fsp_name->base_name;
60 if (!(p = strrchr_m(fname,'/'))) {
66 if (!strequal(lp_magic_script(talloc_tos(), SNUM(conn)),p)) {
67 status = NT_STATUS_OK;
71 if (*lp_magic_output(talloc_tos(), SNUM(conn))) {
72 magic_output = lp_magic_output(talloc_tos(), SNUM(conn));
74 magic_output = talloc_asprintf(ctx,
79 status = NT_STATUS_NO_MEMORY;
83 /* Ensure we don't depend on user's PATH. */
84 p = talloc_asprintf(ctx, "./%s", fname);
86 status = NT_STATUS_NO_MEMORY;
90 if (chmod(fname, 0755) == -1) {
91 status = map_nt_error_from_unix(errno);
94 ret = smbrun(p,&tmp_fd);
95 DEBUG(3,("Invoking magic command %s gave %d\n",
99 if (ret != 0 || tmp_fd == -1) {
103 status = NT_STATUS_UNSUCCESSFUL;
106 outfd = open(magic_output, O_CREAT|O_EXCL|O_RDWR, 0600);
110 status = map_nt_error_from_unix(err);
114 if (sys_fstat(tmp_fd, &st, false) == -1) {
118 status = map_nt_error_from_unix(err);
122 if (transfer_file(tmp_fd,outfd,(off_t)st.st_ex_size) == (off_t)-1) {
126 status = map_nt_error_from_unix(err);
130 if (close(outfd) == -1) {
131 status = map_nt_error_from_unix(errno);
135 status = NT_STATUS_OK;
142 /****************************************************************************
143 Common code to close a file or a directory.
144 ****************************************************************************/
146 static NTSTATUS close_filestruct(files_struct *fsp)
148 NTSTATUS status = NT_STATUS_OK;
150 if (fsp->fh->fd != -1) {
151 if(flush_write_cache(fsp, SAMBA_CLOSE_FLUSH) == -1) {
152 status = map_nt_error_from_unix(errno);
154 delete_write_cache(fsp);
160 /****************************************************************************
162 ****************************************************************************/
164 NTSTATUS delete_all_streams(connection_struct *conn, const char *fname)
166 struct stream_struct *stream_info = NULL;
168 unsigned int num_streams = 0;
169 TALLOC_CTX *frame = talloc_stackframe();
172 status = vfs_streaminfo(conn, NULL, fname, talloc_tos(),
173 &num_streams, &stream_info);
175 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
176 DEBUG(10, ("no streams around\n"));
181 if (!NT_STATUS_IS_OK(status)) {
182 DEBUG(10, ("vfs_streaminfo failed: %s\n",
187 DEBUG(10, ("delete_all_streams found %d streams\n",
190 if (num_streams == 0) {
195 for (i=0; i<num_streams; i++) {
197 struct smb_filename *smb_fname_stream;
199 if (strequal(stream_info[i].name, "::$DATA")) {
203 smb_fname_stream = synthetic_smb_fname(
204 talloc_tos(), fname, stream_info[i].name, NULL);
206 if (smb_fname_stream == NULL) {
207 DEBUG(0, ("talloc_aprintf failed\n"));
208 status = NT_STATUS_NO_MEMORY;
212 res = SMB_VFS_UNLINK(conn, smb_fname_stream);
215 status = map_nt_error_from_unix(errno);
216 DEBUG(10, ("Could not delete stream %s: %s\n",
217 smb_fname_str_dbg(smb_fname_stream),
219 TALLOC_FREE(smb_fname_stream);
222 TALLOC_FREE(smb_fname_stream);
230 /****************************************************************************
231 Deal with removing a share mode on last close.
232 ****************************************************************************/
234 static NTSTATUS close_remove_share_mode(files_struct *fsp,
235 enum file_close_type close_type)
237 connection_struct *conn = fsp->conn;
238 struct server_id self = messaging_server_id(conn->sconn->msg_ctx);
239 bool delete_file = false;
240 bool changed_user = false;
241 struct share_mode_lock *lck = NULL;
242 NTSTATUS status = NT_STATUS_OK;
245 const struct security_unix_token *del_token = NULL;
246 const struct security_token *del_nt_token = NULL;
247 bool got_tokens = false;
251 /* Ensure any pending write time updates are done. */
252 if (fsp->update_write_time_event) {
253 update_write_time_handler(fsp->conn->sconn->ev_ctx,
254 fsp->update_write_time_event,
260 * Lock the share entries, and determine if we should delete
261 * on close. If so delete whilst the lock is still in effect.
262 * This prevents race conditions with the file being created. JRA.
265 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
267 DEBUG(0, ("close_remove_share_mode: Could not get share mode "
268 "lock for file %s\n", fsp_str_dbg(fsp)));
269 return NT_STATUS_INVALID_PARAMETER;
272 if (fsp->write_time_forced) {
273 DEBUG(10,("close_remove_share_mode: write time forced "
276 set_close_write_time(fsp, lck->data->changed_write_time);
277 } else if (fsp->update_write_time_on_close) {
278 /* Someone had a pending write. */
279 if (null_timespec(fsp->close_write_time)) {
280 DEBUG(10,("close_remove_share_mode: update to current time "
283 /* Update to current time due to "normal" write. */
284 set_close_write_time(fsp, timespec_current());
286 DEBUG(10,("close_remove_share_mode: write time pending "
289 /* Update to time set on close call. */
290 set_close_write_time(fsp, fsp->close_write_time);
294 if (fsp->initial_delete_on_close &&
295 !is_delete_on_close_set(lck, fsp->name_hash)) {
296 bool became_user = False;
298 /* Initial delete on close was set and no one else
299 * wrote a real delete on close. */
301 if (get_current_vuid(conn) != fsp->vuid) {
302 become_user(conn, fsp->vuid);
305 fsp->delete_on_close = true;
306 set_delete_on_close_lck(fsp, lck,
307 get_current_nttok(conn),
308 get_current_utok(conn));
314 delete_file = is_delete_on_close_set(lck, fsp->name_hash);
318 /* See if others still have the file open via this pathname.
319 If this is the case, then don't delete. If all opens are
321 for (i=0; i<lck->data->num_share_modes; i++) {
322 struct share_mode_entry *e = &lck->data->share_modes[i];
324 if (!is_valid_share_mode_entry(e)) {
327 if (e->name_hash != fsp->name_hash) {
330 if ((fsp->posix_flags & FSP_POSIX_FLAGS_OPEN)
331 && (e->flags & SHARE_MODE_FLAG_POSIX_OPEN)) {
334 if (serverid_equal(&self, &e->pid) &&
335 (e->share_file_id == fsp->fh->gen_id)) {
338 if (share_mode_stale_pid(lck->data, i)) {
347 * NT can set delete_on_close of the last open
348 * reference to a file.
351 normal_close = (close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE);
353 if (!normal_close || !delete_file) {
354 status = NT_STATUS_OK;
359 * Ok, we have to delete the file
362 DEBUG(5,("close_remove_share_mode: file %s. Delete on close was set "
363 "- deleting file.\n", fsp_str_dbg(fsp)));
366 * Don't try to update the write time when we delete the file
368 fsp->update_write_time_on_close = false;
370 got_tokens = get_delete_on_close_token(lck, fsp->name_hash,
371 &del_nt_token, &del_token);
372 SMB_ASSERT(got_tokens);
374 if (!unix_token_equal(del_token, get_current_utok(conn))) {
375 /* Become the user who requested the delete. */
377 DEBUG(5,("close_remove_share_mode: file %s. "
378 "Change user to uid %u\n",
380 (unsigned int)del_token->uid));
382 if (!push_sec_ctx()) {
383 smb_panic("close_remove_share_mode: file %s. failed to push "
387 set_sec_ctx(del_token->uid,
396 /* We can only delete the file if the name we have is still valid and
397 hasn't been renamed. */
399 tmp_status = vfs_stat_fsp(fsp);
400 if (!NT_STATUS_IS_OK(tmp_status)) {
401 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
402 "was set and stat failed with error %s\n",
403 fsp_str_dbg(fsp), nt_errstr(tmp_status)));
405 * Don't save the errno here, we ignore this error
410 id = vfs_file_id_from_sbuf(conn, &fsp->fsp_name->st);
412 if (!file_id_equal(&fsp->file_id, &id)) {
413 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
414 "was set and dev and/or inode does not match\n",
416 DEBUG(5,("close_remove_share_mode: file %s. stored file_id %s, "
419 file_id_string_tos(&fsp->file_id),
420 file_id_string_tos(&id)));
422 * Don't save the errno here, we ignore this error
427 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
428 && !is_ntfs_stream_smb_fname(fsp->fsp_name)) {
430 status = delete_all_streams(conn, fsp->fsp_name->base_name);
432 if (!NT_STATUS_IS_OK(status)) {
433 DEBUG(5, ("delete_all_streams failed: %s\n",
440 if (SMB_VFS_UNLINK(conn, fsp->fsp_name) != 0) {
442 * This call can potentially fail as another smbd may
443 * have had the file open with delete on close set and
444 * deleted it when its last reference to this file
445 * went away. Hence we log this but not at debug level
449 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
450 "was set and unlink failed with error %s\n",
451 fsp_str_dbg(fsp), strerror(errno)));
453 status = map_nt_error_from_unix(errno);
456 /* As we now have POSIX opens which can unlink
457 * with other open files we may have taken
458 * this code path with more than one share mode
459 * entry - ensure we only delete once by resetting
460 * the delete on close flag. JRA.
463 fsp->delete_on_close = false;
464 reset_delete_on_close_lck(fsp, lck);
473 /* remove filesystem sharemodes */
474 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, 0, 0);
475 if (ret_flock == -1) {
476 DEBUG(2, ("close_remove_share_mode: removing kernel flock for "
477 "%s failed: %s\n", fsp_str_dbg(fsp),
481 if (!del_share_mode(lck, fsp)) {
482 DEBUG(0, ("close_remove_share_mode: Could not delete share "
483 "entry for file %s\n", fsp_str_dbg(fsp)));
490 * Do the notification after we released the share
491 * mode lock. Inside notify_fname we take out another
492 * tdb lock. With ctdb also accessing our databases,
493 * this can lead to deadlocks. Putting this notify
494 * after the TALLOC_FREE(lck) above we avoid locking
495 * two records simultaneously. Notifies are async and
496 * informational only, so calling the notify_fname
497 * without holding the share mode lock should not do
500 notify_fname(conn, NOTIFY_ACTION_REMOVED,
501 FILE_NOTIFY_CHANGE_FILE_NAME,
502 fsp->fsp_name->base_name);
508 void set_close_write_time(struct files_struct *fsp, struct timespec ts)
510 DEBUG(6,("close_write_time: %s" , time_to_asc(convert_timespec_to_time_t(ts))));
512 if (null_timespec(ts)) {
515 fsp->write_time_forced = false;
516 fsp->update_write_time_on_close = true;
517 fsp->close_write_time = ts;
520 static NTSTATUS update_write_time_on_close(struct files_struct *fsp)
522 struct smb_file_time ft;
524 struct share_mode_lock *lck = NULL;
528 if (!fsp->update_write_time_on_close) {
532 if (null_timespec(fsp->close_write_time)) {
533 fsp->close_write_time = timespec_current();
536 /* Ensure we have a valid stat struct for the source. */
537 status = vfs_stat_fsp(fsp);
538 if (!NT_STATUS_IS_OK(status)) {
542 if (!VALID_STAT(fsp->fsp_name->st)) {
543 /* if it doesn't seem to be a real file */
548 * get_existing_share_mode_lock() isn't really the right
549 * call here, as we're being called after
550 * close_remove_share_mode() inside close_normal_file()
551 * so it's quite normal to not have an existing share
552 * mode here. However, get_share_mode_lock() doesn't
553 * work because that will create a new share mode if
554 * one doesn't exist - so stick with this call (just
555 * ignore any error we get if the share mode doesn't
559 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
561 /* On close if we're changing the real file time we
562 * must update it in the open file db too. */
563 (void)set_write_time(fsp->file_id, fsp->close_write_time);
565 /* Close write times overwrite sticky write times
566 so we must replace any sticky write time here. */
567 if (!null_timespec(lck->data->changed_write_time)) {
568 (void)set_sticky_write_time(fsp->file_id, fsp->close_write_time);
573 ft.mtime = fsp->close_write_time;
574 /* As this is a close based update, we are not directly changing the
575 file attributes from a client call, but indirectly from a write. */
576 status = smb_set_file_time(fsp->conn, fsp, fsp->fsp_name, &ft, false);
577 if (!NT_STATUS_IS_OK(status)) {
578 DEBUG(10,("update_write_time_on_close: smb_set_file_time "
579 "on file %s returned %s\n",
588 static NTSTATUS ntstatus_keeperror(NTSTATUS s1, NTSTATUS s2)
590 if (!NT_STATUS_IS_OK(s1)) {
596 /****************************************************************************
599 close_type can be NORMAL_CLOSE=0,SHUTDOWN_CLOSE,ERROR_CLOSE.
600 printing and magic scripts are only run on normal close.
601 delete on close is done on normal and shutdown close.
602 ****************************************************************************/
604 static NTSTATUS close_normal_file(struct smb_request *req, files_struct *fsp,
605 enum file_close_type close_type)
607 NTSTATUS status = NT_STATUS_OK;
609 connection_struct *conn = fsp->conn;
610 bool is_durable = false;
612 if (fsp->num_aio_requests != 0) {
614 if (close_type != SHUTDOWN_CLOSE) {
616 * reply_close and the smb2 close must have
617 * taken care of this. No other callers of
618 * close_file should ever have created async
621 * We need to panic here because if we close()
622 * the fd while we have outstanding async I/O
623 * requests, in the worst case we could end up
624 * writing to the wrong file.
626 DEBUG(0, ("fsp->num_aio_requests=%u\n",
627 fsp->num_aio_requests));
628 smb_panic("can not close with outstanding aio "
633 * For shutdown close, just drop the async requests
634 * including a potential close request pending for
635 * this fsp. Drop the close request first, the
636 * destructor for the aio_requests would execute it.
638 TALLOC_FREE(fsp->deferred_close);
640 while (fsp->num_aio_requests != 0) {
642 * The destructor of the req will remove
643 * itself from the fsp.
644 * Don't use TALLOC_FREE here, this will overwrite
645 * what the destructor just wrote into
648 talloc_free(fsp->aio_requests[0]);
653 * If we're flushing on a close we can get a write
654 * error here, we must remember this.
657 tmp = close_filestruct(fsp);
658 status = ntstatus_keeperror(status, tmp);
660 if (NT_STATUS_IS_OK(status) && fsp->op != NULL) {
661 is_durable = fsp->op->global->durable;
664 if (close_type != SHUTDOWN_CLOSE) {
669 DATA_BLOB new_cookie = data_blob_null;
671 tmp = SMB_VFS_DURABLE_DISCONNECT(fsp,
672 fsp->op->global->backend_cookie,
675 if (NT_STATUS_IS_OK(tmp)) {
680 tv = req->request_time;
682 tv = timeval_current();
684 now = timeval_to_nttime(&tv);
686 data_blob_free(&fsp->op->global->backend_cookie);
687 fsp->op->global->backend_cookie = new_cookie;
689 fsp->op->compat = NULL;
690 tmp = smbXsrv_open_close(fsp->op, now);
691 if (!NT_STATUS_IS_OK(tmp)) {
692 DEBUG(1, ("Failed to update smbXsrv_open "
693 "record when disconnecting durable "
694 "handle for file %s: %s - "
695 "proceeding with normal close\n",
696 fsp_str_dbg(fsp), nt_errstr(tmp)));
698 scavenger_schedule_disconnected(fsp);
700 DEBUG(1, ("Failed to disconnect durable handle for "
701 "file %s: %s - proceeding with normal "
702 "close\n", fsp_str_dbg(fsp), nt_errstr(tmp)));
704 if (!NT_STATUS_IS_OK(tmp)) {
711 * This is the case where we successfully disconnected
712 * a durable handle and closed the underlying file.
713 * In all other cases, we proceed with a genuine close.
715 DEBUG(10, ("%s disconnected durable handle for file %s\n",
716 conn->session_info->unix_info->unix_name,
722 if (fsp->op != NULL) {
724 * Make sure the handle is not marked as durable anymore
726 fsp->op->global->durable = false;
729 if (fsp->print_file) {
730 /* FIXME: return spool errors */
731 print_spool_end(fsp, close_type);
736 /* Remove the oplock before potentially deleting the file. */
737 if(fsp->oplock_type) {
741 /* If this is an old DOS or FCB open and we have multiple opens on
742 the same handle we only have one share mode. Ensure we only remove
743 the share mode on the last close. */
745 if (fsp->fh->ref_count == 1) {
746 /* Should we return on error here... ? */
747 tmp = close_remove_share_mode(fsp, close_type);
748 status = ntstatus_keeperror(status, tmp);
751 locking_close_file(conn->sconn->msg_ctx, fsp, close_type);
754 status = ntstatus_keeperror(status, tmp);
756 /* check for magic scripts */
757 if (close_type == NORMAL_CLOSE) {
758 tmp = check_magic(fsp);
759 status = ntstatus_keeperror(status, tmp);
763 * Ensure pending modtime is set after close.
766 tmp = update_write_time_on_close(fsp);
767 if (NT_STATUS_EQUAL(tmp, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
768 /* Someone renamed the file or a parent directory containing
769 * this file. We can't do anything about this, we don't have
770 * an "update timestamp by fd" call in POSIX. Eat the error. */
775 status = ntstatus_keeperror(status, tmp);
777 DEBUG(2,("%s closed file %s (numopen=%d) %s\n",
778 conn->session_info->unix_info->unix_name, fsp_str_dbg(fsp),
779 conn->num_files_open - 1,
780 nt_errstr(status) ));
785 /****************************************************************************
786 Function used by reply_rmdir to delete an entire directory
787 tree recursively. Return True on ok, False on fail.
788 ****************************************************************************/
790 bool recursive_rmdir(TALLOC_CTX *ctx,
791 connection_struct *conn,
792 struct smb_filename *smb_dname)
794 const char *dname = NULL;
795 char *talloced = NULL;
799 struct smb_Dir *dir_hnd;
801 SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname));
803 dir_hnd = OpenDir(talloc_tos(), conn, smb_dname, NULL, 0);
807 while((dname = ReadDirName(dir_hnd, &offset, &st, &talloced))) {
808 struct smb_filename *smb_dname_full = NULL;
809 char *fullname = NULL;
810 bool do_break = true;
812 if (ISDOT(dname) || ISDOTDOT(dname)) {
813 TALLOC_FREE(talloced);
817 if (!is_visible_file(conn, smb_dname->base_name, dname, &st,
819 TALLOC_FREE(talloced);
823 /* Construct the full name. */
824 fullname = talloc_asprintf(ctx,
826 smb_dname->base_name,
833 smb_dname_full = synthetic_smb_fname(talloc_tos(), fullname,
835 if (smb_dname_full == NULL) {
840 if(SMB_VFS_LSTAT(conn, smb_dname_full) != 0) {
844 if(smb_dname_full->st.st_ex_mode & S_IFDIR) {
845 if(!recursive_rmdir(ctx, conn, smb_dname_full)) {
848 if(SMB_VFS_RMDIR(conn, smb_dname_full) != 0) {
851 } else if(SMB_VFS_UNLINK(conn, smb_dname_full) != 0) {
855 /* Successful iteration. */
859 TALLOC_FREE(smb_dname_full);
860 TALLOC_FREE(fullname);
861 TALLOC_FREE(talloced);
867 TALLOC_FREE(dir_hnd);
871 /****************************************************************************
872 The internals of the rmdir code - called elsewhere.
873 ****************************************************************************/
875 static NTSTATUS rmdir_internals(TALLOC_CTX *ctx, files_struct *fsp)
877 connection_struct *conn = fsp->conn;
878 struct smb_filename *smb_dname = fsp->fsp_name;
881 SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname));
883 /* Might be a symlink. */
884 if(SMB_VFS_LSTAT(conn, smb_dname) != 0) {
885 return map_nt_error_from_unix(errno);
888 if (S_ISLNK(smb_dname->st.st_ex_mode)) {
889 /* Is what it points to a directory ? */
890 if(SMB_VFS_STAT(conn, smb_dname) != 0) {
891 return map_nt_error_from_unix(errno);
893 if (!(S_ISDIR(smb_dname->st.st_ex_mode))) {
894 return NT_STATUS_NOT_A_DIRECTORY;
896 ret = SMB_VFS_UNLINK(conn, smb_dname);
898 ret = SMB_VFS_RMDIR(conn, smb_dname);
901 notify_fname(conn, NOTIFY_ACTION_REMOVED,
902 FILE_NOTIFY_CHANGE_DIR_NAME,
903 smb_dname->base_name);
907 if(((errno == ENOTEMPTY)||(errno == EEXIST)) && *lp_veto_files(talloc_tos(), SNUM(conn))) {
909 * Check to see if the only thing in this directory are
910 * vetoed files/directories. If so then delete them and
911 * retry. If we fail to delete any of them (and we *don't*
912 * do a recursive delete) then fail the rmdir.
915 const char *dname = NULL;
916 char *talloced = NULL;
918 struct smb_Dir *dir_hnd = OpenDir(talloc_tos(), conn,
922 if(dir_hnd == NULL) {
927 while ((dname = ReadDirName(dir_hnd, &dirpos, &st,
928 &talloced)) != NULL) {
929 if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0)) {
930 TALLOC_FREE(talloced);
933 if (!is_visible_file(conn, smb_dname->base_name, dname,
935 TALLOC_FREE(talloced);
938 if(!IS_VETO_PATH(conn, dname)) {
939 TALLOC_FREE(dir_hnd);
940 TALLOC_FREE(talloced);
944 TALLOC_FREE(talloced);
947 /* We only have veto files/directories.
948 * Are we allowed to delete them ? */
950 if(!lp_delete_veto_files(SNUM(conn))) {
951 TALLOC_FREE(dir_hnd);
956 /* Do a recursive delete. */
957 RewindDir(dir_hnd,&dirpos);
958 while ((dname = ReadDirName(dir_hnd, &dirpos, &st,
959 &talloced)) != NULL) {
960 struct smb_filename *smb_dname_full = NULL;
961 char *fullname = NULL;
962 bool do_break = true;
964 if (ISDOT(dname) || ISDOTDOT(dname)) {
965 TALLOC_FREE(talloced);
968 if (!is_visible_file(conn, smb_dname->base_name, dname,
970 TALLOC_FREE(talloced);
974 fullname = talloc_asprintf(ctx,
976 smb_dname->base_name,
984 smb_dname_full = synthetic_smb_fname(
985 talloc_tos(), fullname, NULL, NULL);
986 if (smb_dname_full == NULL) {
991 if(SMB_VFS_LSTAT(conn, smb_dname_full) != 0) {
994 if(smb_dname_full->st.st_ex_mode & S_IFDIR) {
995 if(!recursive_rmdir(ctx, conn,
999 if(SMB_VFS_RMDIR(conn,
1000 smb_dname_full) != 0) {
1003 } else if(SMB_VFS_UNLINK(conn, smb_dname_full) != 0) {
1007 /* Successful iteration. */
1011 TALLOC_FREE(fullname);
1012 TALLOC_FREE(smb_dname_full);
1013 TALLOC_FREE(talloced);
1017 TALLOC_FREE(dir_hnd);
1018 /* Retry the rmdir */
1019 ret = SMB_VFS_RMDIR(conn, smb_dname);
1025 DEBUG(3,("rmdir_internals: couldn't remove directory %s : "
1026 "%s\n", smb_fname_str_dbg(smb_dname),
1028 return map_nt_error_from_unix(errno);
1031 notify_fname(conn, NOTIFY_ACTION_REMOVED,
1032 FILE_NOTIFY_CHANGE_DIR_NAME,
1033 smb_dname->base_name);
1035 return NT_STATUS_OK;
1038 /****************************************************************************
1039 Close a directory opened by an NT SMB call.
1040 ****************************************************************************/
1042 static NTSTATUS close_directory(struct smb_request *req, files_struct *fsp,
1043 enum file_close_type close_type)
1045 struct server_id self = messaging_server_id(fsp->conn->sconn->msg_ctx);
1046 struct share_mode_lock *lck = NULL;
1047 bool delete_dir = False;
1048 NTSTATUS status = NT_STATUS_OK;
1049 NTSTATUS status1 = NT_STATUS_OK;
1050 const struct security_token *del_nt_token = NULL;
1051 const struct security_unix_token *del_token = NULL;
1052 NTSTATUS notify_status;
1054 if (fsp->conn->sconn->using_smb2) {
1055 notify_status = STATUS_NOTIFY_CLEANUP;
1057 notify_status = NT_STATUS_OK;
1061 * NT can set delete_on_close of the last open
1062 * reference to a directory also.
1065 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
1067 DEBUG(0, ("close_directory: Could not get share mode lock for "
1068 "%s\n", fsp_str_dbg(fsp)));
1069 return NT_STATUS_INVALID_PARAMETER;
1072 if (fsp->initial_delete_on_close) {
1073 bool became_user = False;
1075 /* Initial delete on close was set - for
1076 * directories we don't care if anyone else
1077 * wrote a real delete on close. */
1079 if (get_current_vuid(fsp->conn) != fsp->vuid) {
1080 become_user(fsp->conn, fsp->vuid);
1083 send_stat_cache_delete_message(fsp->conn->sconn->msg_ctx,
1084 fsp->fsp_name->base_name);
1085 set_delete_on_close_lck(fsp, lck,
1086 get_current_nttok(fsp->conn),
1087 get_current_utok(fsp->conn));
1088 fsp->delete_on_close = true;
1094 delete_dir = get_delete_on_close_token(lck, fsp->name_hash,
1095 &del_nt_token, &del_token);
1099 /* See if others still have the dir open. If this is the
1100 * case, then don't delete. If all opens are POSIX delete now. */
1101 for (i=0; i<lck->data->num_share_modes; i++) {
1102 struct share_mode_entry *e = &lck->data->share_modes[i];
1103 if (is_valid_share_mode_entry(e) &&
1104 e->name_hash == fsp->name_hash) {
1105 if ((fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) &&
1106 (e->flags & SHARE_MODE_FLAG_POSIX_OPEN))
1110 if (serverid_equal(&self, &e->pid) &&
1111 (e->share_file_id == fsp->fh->gen_id)) {
1114 if (share_mode_stale_pid(lck->data, i)) {
1123 if ((close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE) &&
1126 /* Become the user who requested the delete. */
1128 if (!push_sec_ctx()) {
1129 smb_panic("close_directory: failed to push sec_ctx.\n");
1132 set_sec_ctx(del_token->uid,
1138 if (!del_share_mode(lck, fsp)) {
1139 DEBUG(0, ("close_directory: Could not delete share entry for "
1140 "%s\n", fsp_str_dbg(fsp)));
1145 if ((fsp->conn->fs_capabilities & FILE_NAMED_STREAMS)
1146 && !is_ntfs_stream_smb_fname(fsp->fsp_name)) {
1148 status = delete_all_streams(fsp->conn, fsp->fsp_name->base_name);
1149 if (!NT_STATUS_IS_OK(status)) {
1150 DEBUG(5, ("delete_all_streams failed: %s\n",
1151 nt_errstr(status)));
1156 status = rmdir_internals(talloc_tos(), fsp);
1158 DEBUG(5,("close_directory: %s. Delete on close was set - "
1159 "deleting directory returned %s.\n",
1160 fsp_str_dbg(fsp), nt_errstr(status)));
1162 /* unbecome user. */
1166 * Ensure we remove any change notify requests that would
1167 * now fail as the directory has been deleted.
1170 if (NT_STATUS_IS_OK(status)) {
1171 notify_status = NT_STATUS_DELETE_PENDING;
1174 if (!del_share_mode(lck, fsp)) {
1175 DEBUG(0, ("close_directory: Could not delete share entry for "
1176 "%s\n", fsp_str_dbg(fsp)));
1182 remove_pending_change_notify_requests_by_fid(fsp, notify_status);
1184 status1 = fd_close(fsp);
1186 if (!NT_STATUS_IS_OK(status1)) {
1187 DEBUG(0, ("Could not close dir! fname=%s, fd=%d, err=%d=%s\n",
1188 fsp_str_dbg(fsp), fsp->fh->fd, errno,
1193 * Do the code common to files and directories.
1195 close_filestruct(fsp);
1196 file_free(req, fsp);
1198 if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(status1)) {
1204 /****************************************************************************
1205 Close a files_struct.
1206 ****************************************************************************/
1208 NTSTATUS close_file(struct smb_request *req, files_struct *fsp,
1209 enum file_close_type close_type)
1212 struct files_struct *base_fsp = fsp->base_fsp;
1214 if(fsp->is_directory) {
1215 status = close_directory(req, fsp, close_type);
1216 } else if (fsp->fake_file_handle != NULL) {
1217 status = close_fake_file(req, fsp);
1219 status = close_normal_file(req, fsp, close_type);
1222 if ((base_fsp != NULL) && (close_type != SHUTDOWN_CLOSE)) {
1225 * fsp was a stream, the base fsp can't be a stream as well
1227 * For SHUTDOWN_CLOSE this is not possible here, because
1228 * SHUTDOWN_CLOSE only happens from files.c which walks the
1229 * complete list of files. If we mess with more than one fsp
1230 * those loops will become confused.
1233 SMB_ASSERT(base_fsp->base_fsp == NULL);
1234 close_file(req, base_fsp, close_type);
1240 /****************************************************************************
1241 Deal with an (authorized) message to close a file given the share mode
1243 ****************************************************************************/
1245 void msg_close_file(struct messaging_context *msg_ctx,
1248 struct server_id server_id,
1251 files_struct *fsp = NULL;
1252 struct share_mode_entry e;
1253 struct smbd_server_connection *sconn =
1254 talloc_get_type_abort(private_data,
1255 struct smbd_server_connection);
1257 message_to_share_mode_entry(&e, (char *)data->data);
1260 char *sm_str = share_mode_str(NULL, 0, &e);
1262 smb_panic("talloc failed");
1264 DEBUG(10,("msg_close_file: got request to close share mode "
1265 "entry %s\n", sm_str));
1266 TALLOC_FREE(sm_str);
1269 fsp = file_find_dif(sconn, e.id, e.share_file_id);
1271 DEBUG(10,("msg_close_file: failed to find file.\n"));
1274 close_file(NULL, fsp, NORMAL_CLOSE);