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"
24 #include "lib/util/server_id.h"
26 #include "smbd/smbd.h"
27 #include "smbd/globals.h"
28 #include "smbd/scavenger.h"
29 #include "fake_file.h"
30 #include "transfer_file.h"
33 #include "../librpc/gen_ndr/open_files.h"
35 /****************************************************************************
36 Run a file if it is a magic script.
37 ****************************************************************************/
39 static NTSTATUS check_magic(struct files_struct *fsp)
42 const char *magic_output = NULL;
45 TALLOC_CTX *ctx = NULL;
47 struct connection_struct *conn = fsp->conn;
51 if (!*lp_magic_script(talloc_tos(), SNUM(conn))) {
55 DEBUG(5,("checking magic for %s\n", fsp_str_dbg(fsp)));
57 ctx = talloc_stackframe();
59 fname = fsp->fsp_name->base_name;
61 if (!(p = strrchr_m(fname,'/'))) {
67 if (!strequal(lp_magic_script(talloc_tos(), SNUM(conn)),p)) {
68 status = NT_STATUS_OK;
72 if (*lp_magic_output(talloc_tos(), SNUM(conn))) {
73 magic_output = lp_magic_output(talloc_tos(), SNUM(conn));
75 magic_output = talloc_asprintf(ctx,
80 status = NT_STATUS_NO_MEMORY;
84 /* Ensure we don't depend on user's PATH. */
85 p = talloc_asprintf(ctx, "./%s", fname);
87 status = NT_STATUS_NO_MEMORY;
91 if (chmod(fname, 0755) == -1) {
92 status = map_nt_error_from_unix(errno);
95 ret = smbrun(p, &tmp_fd, NULL);
96 DEBUG(3,("Invoking magic command %s gave %d\n",
100 if (ret != 0 || tmp_fd == -1) {
104 status = NT_STATUS_UNSUCCESSFUL;
107 outfd = open(magic_output, O_CREAT|O_EXCL|O_RDWR, 0600);
111 status = map_nt_error_from_unix(err);
115 if (sys_fstat(tmp_fd, &st, false) == -1) {
119 status = map_nt_error_from_unix(err);
123 if (transfer_file(tmp_fd,outfd,(off_t)st.st_ex_size) == (off_t)-1) {
127 status = map_nt_error_from_unix(err);
131 if (close(outfd) == -1) {
132 status = map_nt_error_from_unix(errno);
136 status = NT_STATUS_OK;
143 /****************************************************************************
144 Common code to close a file or a directory.
145 ****************************************************************************/
147 static NTSTATUS close_filestruct(files_struct *fsp)
149 NTSTATUS status = NT_STATUS_OK;
151 if (fsp->fh->fd != -1) {
152 if(flush_write_cache(fsp, SAMBA_CLOSE_FLUSH) == -1) {
153 status = map_nt_error_from_unix(errno);
155 delete_write_cache(fsp);
161 /****************************************************************************
163 ****************************************************************************/
165 NTSTATUS delete_all_streams(connection_struct *conn,
166 const struct smb_filename *smb_fname)
168 struct stream_struct *stream_info = NULL;
170 unsigned int num_streams = 0;
171 TALLOC_CTX *frame = talloc_stackframe();
174 status = vfs_streaminfo(conn, NULL, smb_fname, talloc_tos(),
175 &num_streams, &stream_info);
177 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
178 DEBUG(10, ("no streams around\n"));
183 if (!NT_STATUS_IS_OK(status)) {
184 DEBUG(10, ("vfs_streaminfo failed: %s\n",
189 DEBUG(10, ("delete_all_streams found %d streams\n",
192 if (num_streams == 0) {
197 for (i=0; i<num_streams; i++) {
199 struct smb_filename *smb_fname_stream;
201 if (strequal(stream_info[i].name, "::$DATA")) {
205 smb_fname_stream = synthetic_smb_fname(talloc_tos(),
206 smb_fname->base_name,
210 ~SMB_FILENAME_POSIX_PATH));
212 if (smb_fname_stream == NULL) {
213 DEBUG(0, ("talloc_aprintf failed\n"));
214 status = NT_STATUS_NO_MEMORY;
218 res = SMB_VFS_UNLINK(conn, smb_fname_stream);
221 status = map_nt_error_from_unix(errno);
222 DEBUG(10, ("Could not delete stream %s: %s\n",
223 smb_fname_str_dbg(smb_fname_stream),
225 TALLOC_FREE(smb_fname_stream);
228 TALLOC_FREE(smb_fname_stream);
236 bool has_other_nonposix_opens(struct share_mode_lock *lck,
237 struct files_struct *fsp,
238 struct server_id self)
240 struct share_mode_data *data = lck->data;
243 for (i=0; i<data->num_share_modes; i++) {
244 struct share_mode_entry *e = &data->share_modes[i];
246 if (!is_valid_share_mode_entry(e)) {
249 if (e->name_hash != fsp->name_hash) {
252 if ((fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) &&
253 (e->flags & SHARE_MODE_FLAG_POSIX_OPEN)) {
256 if (serverid_equal(&self, &e->pid) &&
257 (e->share_file_id == fsp->fh->gen_id)) {
260 if (share_mode_stale_pid(data, i)) {
269 /****************************************************************************
270 Deal with removing a share mode on last close.
271 ****************************************************************************/
273 static NTSTATUS close_remove_share_mode(files_struct *fsp,
274 enum file_close_type close_type)
276 connection_struct *conn = fsp->conn;
277 struct server_id self = messaging_server_id(conn->sconn->msg_ctx);
278 bool delete_file = false;
279 bool changed_user = false;
280 struct share_mode_lock *lck = NULL;
281 NTSTATUS status = NT_STATUS_OK;
284 const struct security_unix_token *del_token = NULL;
285 const struct security_token *del_nt_token = NULL;
286 bool got_tokens = false;
289 /* Ensure any pending write time updates are done. */
290 if (fsp->update_write_time_event) {
291 fsp_flush_write_time_update(fsp);
295 * Lock the share entries, and determine if we should delete
296 * on close. If so delete whilst the lock is still in effect.
297 * This prevents race conditions with the file being created. JRA.
300 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
302 DEBUG(0, ("close_remove_share_mode: Could not get share mode "
303 "lock for file %s\n", fsp_str_dbg(fsp)));
304 return NT_STATUS_INVALID_PARAMETER;
307 /* Remove the oplock before potentially deleting the file. */
308 if(fsp->oplock_type) {
309 remove_oplock_under_lock(fsp, lck);
312 if (fsp->write_time_forced) {
313 DEBUG(10,("close_remove_share_mode: write time forced "
316 set_close_write_time(fsp, lck->data->changed_write_time);
317 } else if (fsp->update_write_time_on_close) {
318 /* Someone had a pending write. */
319 if (null_timespec(fsp->close_write_time)) {
320 DEBUG(10,("close_remove_share_mode: update to current time "
323 /* Update to current time due to "normal" write. */
324 set_close_write_time(fsp, timespec_current());
326 DEBUG(10,("close_remove_share_mode: write time pending "
329 /* Update to time set on close call. */
330 set_close_write_time(fsp, fsp->close_write_time);
334 if (fsp->initial_delete_on_close &&
335 !is_delete_on_close_set(lck, fsp->name_hash)) {
336 bool became_user = False;
338 /* Initial delete on close was set and no one else
339 * wrote a real delete on close. */
341 if (get_current_vuid(conn) != fsp->vuid) {
342 become_user(conn, fsp->vuid);
345 fsp->delete_on_close = true;
346 set_delete_on_close_lck(fsp, lck,
347 get_current_nttok(conn),
348 get_current_utok(conn));
354 delete_file = is_delete_on_close_set(lck, fsp->name_hash);
356 delete_file &= !has_other_nonposix_opens(lck, fsp, self);
359 * NT can set delete_on_close of the last open
360 * reference to a file.
363 normal_close = (close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE);
365 if (!normal_close || !delete_file) {
366 status = NT_STATUS_OK;
371 * Ok, we have to delete the file
374 DEBUG(5,("close_remove_share_mode: file %s. Delete on close was set "
375 "- deleting file.\n", fsp_str_dbg(fsp)));
378 * Don't try to update the write time when we delete the file
380 fsp->update_write_time_on_close = false;
382 got_tokens = get_delete_on_close_token(lck, fsp->name_hash,
383 &del_nt_token, &del_token);
384 SMB_ASSERT(got_tokens);
386 if (!unix_token_equal(del_token, get_current_utok(conn))) {
387 /* Become the user who requested the delete. */
389 DEBUG(5,("close_remove_share_mode: file %s. "
390 "Change user to uid %u\n",
392 (unsigned int)del_token->uid));
394 if (!push_sec_ctx()) {
395 smb_panic("close_remove_share_mode: file %s. failed to push "
399 set_sec_ctx(del_token->uid,
408 /* We can only delete the file if the name we have is still valid and
409 hasn't been renamed. */
411 tmp_status = vfs_stat_fsp(fsp);
412 if (!NT_STATUS_IS_OK(tmp_status)) {
413 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
414 "was set and stat failed with error %s\n",
415 fsp_str_dbg(fsp), nt_errstr(tmp_status)));
417 * Don't save the errno here, we ignore this error
422 id = vfs_file_id_from_sbuf(conn, &fsp->fsp_name->st);
424 if (!file_id_equal(&fsp->file_id, &id)) {
425 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
426 "was set and dev and/or inode does not match\n",
428 DEBUG(5,("close_remove_share_mode: file %s. stored file_id %s, "
431 file_id_string_tos(&fsp->file_id),
432 file_id_string_tos(&id)));
434 * Don't save the errno here, we ignore this error
439 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
440 && !is_ntfs_stream_smb_fname(fsp->fsp_name)) {
442 status = delete_all_streams(conn, fsp->fsp_name);
444 if (!NT_STATUS_IS_OK(status)) {
445 DEBUG(5, ("delete_all_streams failed: %s\n",
451 if (fsp->kernel_share_modes_taken) {
455 * A file system sharemode could block the unlink;
456 * remove filesystem sharemodes first.
458 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, 0, 0);
459 if (ret_flock == -1) {
460 DBG_INFO("removing kernel flock for %s failed: %s\n",
461 fsp_str_dbg(fsp), strerror(errno));
464 fsp->kernel_share_modes_taken = false;
468 if (SMB_VFS_UNLINK(conn, fsp->fsp_name) != 0) {
470 * This call can potentially fail as another smbd may
471 * have had the file open with delete on close set and
472 * deleted it when its last reference to this file
473 * went away. Hence we log this but not at debug level
477 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
478 "was set and unlink failed with error %s\n",
479 fsp_str_dbg(fsp), strerror(errno)));
481 status = map_nt_error_from_unix(errno);
484 /* As we now have POSIX opens which can unlink
485 * with other open files we may have taken
486 * this code path with more than one share mode
487 * entry - ensure we only delete once by resetting
488 * the delete on close flag. JRA.
491 fsp->delete_on_close = false;
492 reset_delete_on_close_lck(fsp, lck);
501 if (fsp->kernel_share_modes_taken) {
504 /* remove filesystem sharemodes */
505 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, 0, 0);
506 if (ret_flock == -1) {
507 DEBUG(2, ("close_remove_share_mode: removing kernel "
508 "flock for %s failed: %s\n",
509 fsp_str_dbg(fsp), strerror(errno)));
513 if (!del_share_mode(lck, fsp)) {
514 DEBUG(0, ("close_remove_share_mode: Could not delete share "
515 "entry for file %s\n", fsp_str_dbg(fsp)));
522 * Do the notification after we released the share
523 * mode lock. Inside notify_fname we take out another
524 * tdb lock. With ctdb also accessing our databases,
525 * this can lead to deadlocks. Putting this notify
526 * after the TALLOC_FREE(lck) above we avoid locking
527 * two records simultaneously. Notifies are async and
528 * informational only, so calling the notify_fname
529 * without holding the share mode lock should not do
532 notify_fname(conn, NOTIFY_ACTION_REMOVED,
533 FILE_NOTIFY_CHANGE_FILE_NAME,
534 fsp->fsp_name->base_name);
540 void set_close_write_time(struct files_struct *fsp, struct timespec ts)
542 DEBUG(6,("close_write_time: %s" , time_to_asc(convert_timespec_to_time_t(ts))));
544 if (null_timespec(ts)) {
547 fsp->write_time_forced = false;
548 fsp->update_write_time_on_close = true;
549 fsp->close_write_time = ts;
552 static NTSTATUS update_write_time_on_close(struct files_struct *fsp)
554 struct smb_file_time ft;
556 struct share_mode_lock *lck = NULL;
560 if (!fsp->update_write_time_on_close) {
564 if (null_timespec(fsp->close_write_time)) {
565 fsp->close_write_time = timespec_current();
568 /* Ensure we have a valid stat struct for the source. */
569 status = vfs_stat_fsp(fsp);
570 if (!NT_STATUS_IS_OK(status)) {
574 if (!VALID_STAT(fsp->fsp_name->st)) {
575 /* if it doesn't seem to be a real file */
580 * get_existing_share_mode_lock() isn't really the right
581 * call here, as we're being called after
582 * close_remove_share_mode() inside close_normal_file()
583 * so it's quite normal to not have an existing share
584 * mode here. However, get_share_mode_lock() doesn't
585 * work because that will create a new share mode if
586 * one doesn't exist - so stick with this call (just
587 * ignore any error we get if the share mode doesn't
591 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
593 /* On close if we're changing the real file time we
594 * must update it in the open file db too. */
595 (void)set_write_time(fsp->file_id, fsp->close_write_time);
597 /* Close write times overwrite sticky write times
598 so we must replace any sticky write time here. */
599 if (!null_timespec(lck->data->changed_write_time)) {
600 (void)set_sticky_write_time(fsp->file_id, fsp->close_write_time);
605 ft.mtime = fsp->close_write_time;
606 /* As this is a close based update, we are not directly changing the
607 file attributes from a client call, but indirectly from a write. */
608 status = smb_set_file_time(fsp->conn, fsp, fsp->fsp_name, &ft, false);
609 if (!NT_STATUS_IS_OK(status)) {
610 DEBUG(10,("update_write_time_on_close: smb_set_file_time "
611 "on file %s returned %s\n",
620 static NTSTATUS ntstatus_keeperror(NTSTATUS s1, NTSTATUS s2)
622 if (!NT_STATUS_IS_OK(s1)) {
628 /****************************************************************************
631 close_type can be NORMAL_CLOSE=0,SHUTDOWN_CLOSE,ERROR_CLOSE.
632 printing and magic scripts are only run on normal close.
633 delete on close is done on normal and shutdown close.
634 ****************************************************************************/
636 static NTSTATUS close_normal_file(struct smb_request *req, files_struct *fsp,
637 enum file_close_type close_type)
639 NTSTATUS status = NT_STATUS_OK;
641 connection_struct *conn = fsp->conn;
642 bool is_durable = false;
644 if (fsp->num_aio_requests != 0) {
646 if (close_type != SHUTDOWN_CLOSE) {
648 * reply_close and the smb2 close must have
649 * taken care of this. No other callers of
650 * close_file should ever have created async
653 * We need to panic here because if we close()
654 * the fd while we have outstanding async I/O
655 * requests, in the worst case we could end up
656 * writing to the wrong file.
658 DEBUG(0, ("fsp->num_aio_requests=%u\n",
659 fsp->num_aio_requests));
660 smb_panic("can not close with outstanding aio "
665 * For shutdown close, just drop the async requests
666 * including a potential close request pending for
667 * this fsp. Drop the close request first, the
668 * destructor for the aio_requests would execute it.
670 TALLOC_FREE(fsp->deferred_close);
672 while (fsp->num_aio_requests != 0) {
674 * The destructor of the req will remove
675 * itself from the fsp.
676 * Don't use TALLOC_FREE here, this will overwrite
677 * what the destructor just wrote into
680 talloc_free(fsp->aio_requests[0]);
685 * If we're flushing on a close we can get a write
686 * error here, we must remember this.
689 tmp = close_filestruct(fsp);
690 status = ntstatus_keeperror(status, tmp);
692 if (NT_STATUS_IS_OK(status) && fsp->op != NULL) {
693 is_durable = fsp->op->global->durable;
696 if (close_type != SHUTDOWN_CLOSE) {
701 DATA_BLOB new_cookie = data_blob_null;
703 tmp = SMB_VFS_DURABLE_DISCONNECT(fsp,
704 fsp->op->global->backend_cookie,
707 if (NT_STATUS_IS_OK(tmp)) {
712 tv = req->request_time;
714 tv = timeval_current();
716 now = timeval_to_nttime(&tv);
718 data_blob_free(&fsp->op->global->backend_cookie);
719 fsp->op->global->backend_cookie = new_cookie;
721 fsp->op->compat = NULL;
722 tmp = smbXsrv_open_close(fsp->op, now);
723 if (!NT_STATUS_IS_OK(tmp)) {
724 DEBUG(1, ("Failed to update smbXsrv_open "
725 "record when disconnecting durable "
726 "handle for file %s: %s - "
727 "proceeding with normal close\n",
728 fsp_str_dbg(fsp), nt_errstr(tmp)));
730 scavenger_schedule_disconnected(fsp);
732 DEBUG(1, ("Failed to disconnect durable handle for "
733 "file %s: %s - proceeding with normal "
734 "close\n", fsp_str_dbg(fsp), nt_errstr(tmp)));
736 if (!NT_STATUS_IS_OK(tmp)) {
743 * This is the case where we successfully disconnected
744 * a durable handle and closed the underlying file.
745 * In all other cases, we proceed with a genuine close.
747 DEBUG(10, ("%s disconnected durable handle for file %s\n",
748 conn->session_info->unix_info->unix_name,
754 if (fsp->op != NULL) {
756 * Make sure the handle is not marked as durable anymore
758 fsp->op->global->durable = false;
761 if (fsp->print_file) {
762 /* FIXME: return spool errors */
763 print_spool_end(fsp, close_type);
768 /* If this is an old DOS or FCB open and we have multiple opens on
769 the same handle we only have one share mode. Ensure we only remove
770 the share mode on the last close. */
772 if (fsp->fh->ref_count == 1) {
773 /* Should we return on error here... ? */
774 tmp = close_remove_share_mode(fsp, close_type);
775 status = ntstatus_keeperror(status, tmp);
778 locking_close_file(conn->sconn->msg_ctx, fsp, close_type);
781 status = ntstatus_keeperror(status, tmp);
783 /* check for magic scripts */
784 if (close_type == NORMAL_CLOSE) {
785 tmp = check_magic(fsp);
786 status = ntstatus_keeperror(status, tmp);
790 * Ensure pending modtime is set after close.
793 tmp = update_write_time_on_close(fsp);
794 if (NT_STATUS_EQUAL(tmp, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
795 /* Someone renamed the file or a parent directory containing
796 * this file. We can't do anything about this, we don't have
797 * an "update timestamp by fd" call in POSIX. Eat the error. */
802 status = ntstatus_keeperror(status, tmp);
804 DEBUG(2,("%s closed file %s (numopen=%d) %s\n",
805 conn->session_info->unix_info->unix_name, fsp_str_dbg(fsp),
806 conn->num_files_open - 1,
807 nt_errstr(status) ));
812 /****************************************************************************
813 Function used by reply_rmdir to delete an entire directory
814 tree recursively. Return True on ok, False on fail.
815 ****************************************************************************/
817 bool recursive_rmdir(TALLOC_CTX *ctx,
818 connection_struct *conn,
819 struct smb_filename *smb_dname)
821 const char *dname = NULL;
822 char *talloced = NULL;
826 struct smb_Dir *dir_hnd;
828 SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname));
830 dir_hnd = OpenDir(talloc_tos(), conn, smb_dname, NULL, 0);
834 while((dname = ReadDirName(dir_hnd, &offset, &st, &talloced))) {
835 struct smb_filename *smb_dname_full = NULL;
836 char *fullname = NULL;
837 bool do_break = true;
839 if (ISDOT(dname) || ISDOTDOT(dname)) {
840 TALLOC_FREE(talloced);
844 if (!is_visible_file(conn, smb_dname->base_name, dname, &st,
846 TALLOC_FREE(talloced);
850 /* Construct the full name. */
851 fullname = talloc_asprintf(ctx,
853 smb_dname->base_name,
860 smb_dname_full = synthetic_smb_fname(talloc_tos(),
865 if (smb_dname_full == NULL) {
870 if(SMB_VFS_LSTAT(conn, smb_dname_full) != 0) {
874 if(smb_dname_full->st.st_ex_mode & S_IFDIR) {
875 if(!recursive_rmdir(ctx, conn, smb_dname_full)) {
878 if(SMB_VFS_RMDIR(conn, smb_dname_full) != 0) {
881 } else if(SMB_VFS_UNLINK(conn, smb_dname_full) != 0) {
885 /* Successful iteration. */
889 TALLOC_FREE(smb_dname_full);
890 TALLOC_FREE(fullname);
891 TALLOC_FREE(talloced);
897 TALLOC_FREE(dir_hnd);
901 /****************************************************************************
902 The internals of the rmdir code - called elsewhere.
903 ****************************************************************************/
905 static NTSTATUS rmdir_internals(TALLOC_CTX *ctx, files_struct *fsp)
907 connection_struct *conn = fsp->conn;
908 struct smb_filename *smb_dname = fsp->fsp_name;
911 SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname));
913 /* Might be a symlink. */
914 if(SMB_VFS_LSTAT(conn, smb_dname) != 0) {
915 return map_nt_error_from_unix(errno);
918 if (S_ISLNK(smb_dname->st.st_ex_mode)) {
919 /* Is what it points to a directory ? */
920 if(SMB_VFS_STAT(conn, smb_dname) != 0) {
921 return map_nt_error_from_unix(errno);
923 if (!(S_ISDIR(smb_dname->st.st_ex_mode))) {
924 return NT_STATUS_NOT_A_DIRECTORY;
926 ret = SMB_VFS_UNLINK(conn, smb_dname);
928 ret = SMB_VFS_RMDIR(conn, smb_dname);
931 notify_fname(conn, NOTIFY_ACTION_REMOVED,
932 FILE_NOTIFY_CHANGE_DIR_NAME,
933 smb_dname->base_name);
937 if(((errno == ENOTEMPTY)||(errno == EEXIST)) && *lp_veto_files(talloc_tos(), SNUM(conn))) {
939 * Check to see if the only thing in this directory are
940 * vetoed files/directories. If so then delete them and
941 * retry. If we fail to delete any of them (and we *don't*
942 * do a recursive delete) then fail the rmdir.
945 const char *dname = NULL;
946 char *talloced = NULL;
948 struct smb_Dir *dir_hnd = OpenDir(talloc_tos(), conn,
952 if(dir_hnd == NULL) {
957 while ((dname = ReadDirName(dir_hnd, &dirpos, &st,
958 &talloced)) != NULL) {
959 if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0)) {
960 TALLOC_FREE(talloced);
963 if (!is_visible_file(conn, smb_dname->base_name, dname,
965 TALLOC_FREE(talloced);
968 if(!IS_VETO_PATH(conn, dname)) {
969 TALLOC_FREE(dir_hnd);
970 TALLOC_FREE(talloced);
974 TALLOC_FREE(talloced);
977 /* We only have veto files/directories.
978 * Are we allowed to delete them ? */
980 if(!lp_delete_veto_files(SNUM(conn))) {
981 TALLOC_FREE(dir_hnd);
986 /* Do a recursive delete. */
987 RewindDir(dir_hnd,&dirpos);
988 while ((dname = ReadDirName(dir_hnd, &dirpos, &st,
989 &talloced)) != NULL) {
990 struct smb_filename *smb_dname_full = NULL;
991 char *fullname = NULL;
992 bool do_break = true;
994 if (ISDOT(dname) || ISDOTDOT(dname)) {
995 TALLOC_FREE(talloced);
998 if (!is_visible_file(conn, smb_dname->base_name, dname,
1000 TALLOC_FREE(talloced);
1004 fullname = talloc_asprintf(ctx,
1006 smb_dname->base_name,
1014 smb_dname_full = synthetic_smb_fname(talloc_tos(),
1019 if (smb_dname_full == NULL) {
1024 if(SMB_VFS_LSTAT(conn, smb_dname_full) != 0) {
1027 if(smb_dname_full->st.st_ex_mode & S_IFDIR) {
1028 if(!recursive_rmdir(ctx, conn,
1032 if(SMB_VFS_RMDIR(conn,
1033 smb_dname_full) != 0) {
1036 } else if(SMB_VFS_UNLINK(conn, smb_dname_full) != 0) {
1040 /* Successful iteration. */
1044 TALLOC_FREE(fullname);
1045 TALLOC_FREE(smb_dname_full);
1046 TALLOC_FREE(talloced);
1050 TALLOC_FREE(dir_hnd);
1051 /* Retry the rmdir */
1052 ret = SMB_VFS_RMDIR(conn, smb_dname);
1058 DEBUG(3,("rmdir_internals: couldn't remove directory %s : "
1059 "%s\n", smb_fname_str_dbg(smb_dname),
1061 return map_nt_error_from_unix(errno);
1064 notify_fname(conn, NOTIFY_ACTION_REMOVED,
1065 FILE_NOTIFY_CHANGE_DIR_NAME,
1066 smb_dname->base_name);
1068 return NT_STATUS_OK;
1071 /****************************************************************************
1072 Close a directory opened by an NT SMB call.
1073 ****************************************************************************/
1075 static NTSTATUS close_directory(struct smb_request *req, files_struct *fsp,
1076 enum file_close_type close_type)
1078 struct server_id self = messaging_server_id(fsp->conn->sconn->msg_ctx);
1079 struct share_mode_lock *lck = NULL;
1080 bool delete_dir = False;
1081 NTSTATUS status = NT_STATUS_OK;
1082 NTSTATUS status1 = NT_STATUS_OK;
1083 const struct security_token *del_nt_token = NULL;
1084 const struct security_unix_token *del_token = NULL;
1085 NTSTATUS notify_status;
1087 if (fsp->conn->sconn->using_smb2) {
1088 notify_status = STATUS_NOTIFY_CLEANUP;
1090 notify_status = NT_STATUS_OK;
1093 if (fsp->num_aio_requests != 0) {
1094 if (close_type != SHUTDOWN_CLOSE) {
1096 * We panic here because if we close() the fd while we
1097 * have outstanding async I/O requests, an async IO
1098 * request might use the fd. For directories the fd is
1099 * read-only, so this is not as bad as with files, but
1100 * still, better safe then sorry.
1102 DBG_ERR("fsp->num_aio_requests=%u\n",
1103 fsp->num_aio_requests);
1104 smb_panic("close with outstanding aio requests");
1105 return NT_STATUS_INTERNAL_ERROR;
1108 while (fsp->num_aio_requests != 0) {
1110 * The destructor of the req will remove itself from the
1111 * fsp. Don't use TALLOC_FREE here, this will overwrite
1112 * what the destructor just wrote into aio_requests[0].
1114 talloc_free(fsp->aio_requests[0]);
1119 * NT can set delete_on_close of the last open
1120 * reference to a directory also.
1123 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
1125 DEBUG(0, ("close_directory: Could not get share mode lock for "
1126 "%s\n", fsp_str_dbg(fsp)));
1127 close_filestruct(fsp);
1128 file_free(req, fsp);
1129 return NT_STATUS_INVALID_PARAMETER;
1132 if (fsp->initial_delete_on_close) {
1133 bool became_user = False;
1135 /* Initial delete on close was set - for
1136 * directories we don't care if anyone else
1137 * wrote a real delete on close. */
1139 if (get_current_vuid(fsp->conn) != fsp->vuid) {
1140 become_user(fsp->conn, fsp->vuid);
1143 send_stat_cache_delete_message(fsp->conn->sconn->msg_ctx,
1144 fsp->fsp_name->base_name);
1145 set_delete_on_close_lck(fsp, lck,
1146 get_current_nttok(fsp->conn),
1147 get_current_utok(fsp->conn));
1148 fsp->delete_on_close = true;
1154 delete_dir = get_delete_on_close_token(lck, fsp->name_hash,
1155 &del_nt_token, &del_token);
1157 delete_dir &= !has_other_nonposix_opens(lck, fsp, self);
1159 if ((close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE) &&
1162 /* Become the user who requested the delete. */
1164 if (!push_sec_ctx()) {
1165 smb_panic("close_directory: failed to push sec_ctx.\n");
1168 set_sec_ctx(del_token->uid,
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)));
1181 if ((fsp->conn->fs_capabilities & FILE_NAMED_STREAMS)
1182 && !is_ntfs_stream_smb_fname(fsp->fsp_name)) {
1184 status = delete_all_streams(fsp->conn, fsp->fsp_name);
1185 if (!NT_STATUS_IS_OK(status)) {
1186 DEBUG(5, ("delete_all_streams failed: %s\n",
1187 nt_errstr(status)));
1188 close_filestruct(fsp);
1189 file_free(req, fsp);
1194 status = rmdir_internals(talloc_tos(), fsp);
1196 DEBUG(5,("close_directory: %s. Delete on close was set - "
1197 "deleting directory returned %s.\n",
1198 fsp_str_dbg(fsp), nt_errstr(status)));
1200 /* unbecome user. */
1204 * Ensure we remove any change notify requests that would
1205 * now fail as the directory has been deleted.
1208 if (NT_STATUS_IS_OK(status)) {
1209 notify_status = NT_STATUS_DELETE_PENDING;
1212 if (!del_share_mode(lck, fsp)) {
1213 DEBUG(0, ("close_directory: Could not delete share entry for "
1214 "%s\n", fsp_str_dbg(fsp)));
1220 remove_pending_change_notify_requests_by_fid(fsp, notify_status);
1222 status1 = fd_close(fsp);
1224 if (!NT_STATUS_IS_OK(status1)) {
1225 DEBUG(0, ("Could not close dir! fname=%s, fd=%d, err=%d=%s\n",
1226 fsp_str_dbg(fsp), fsp->fh->fd, errno,
1231 * Do the code common to files and directories.
1233 close_filestruct(fsp);
1234 file_free(req, fsp);
1236 if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(status1)) {
1242 /****************************************************************************
1243 Close a files_struct.
1244 ****************************************************************************/
1246 NTSTATUS close_file(struct smb_request *req, files_struct *fsp,
1247 enum file_close_type close_type)
1250 struct files_struct *base_fsp = fsp->base_fsp;
1252 if(fsp->is_directory) {
1253 status = close_directory(req, fsp, close_type);
1254 } else if (fsp->fake_file_handle != NULL) {
1255 status = close_fake_file(req, fsp);
1257 status = close_normal_file(req, fsp, close_type);
1260 if ((base_fsp != NULL) && (close_type != SHUTDOWN_CLOSE)) {
1263 * fsp was a stream, the base fsp can't be a stream as well
1265 * For SHUTDOWN_CLOSE this is not possible here, because
1266 * SHUTDOWN_CLOSE only happens from files.c which walks the
1267 * complete list of files. If we mess with more than one fsp
1268 * those loops will become confused.
1271 SMB_ASSERT(base_fsp->base_fsp == NULL);
1272 close_file(req, base_fsp, close_type);
1278 /****************************************************************************
1279 Deal with an (authorized) message to close a file given the share mode
1281 ****************************************************************************/
1283 void msg_close_file(struct messaging_context *msg_ctx,
1286 struct server_id server_id,
1289 files_struct *fsp = NULL;
1291 struct share_mode_entry e;
1292 struct smbd_server_connection *sconn =
1293 talloc_get_type_abort(private_data,
1294 struct smbd_server_connection);
1296 message_to_share_mode_entry(&id, &e, (char *)data->data);
1299 char *sm_str = share_mode_str(NULL, 0, &id, &e);
1301 smb_panic("talloc failed");
1303 DEBUG(10,("msg_close_file: got request to close share mode "
1304 "entry %s\n", sm_str));
1305 TALLOC_FREE(sm_str);
1308 fsp = file_find_dif(sconn, id, e.share_file_id);
1310 DEBUG(10,("msg_close_file: failed to find file.\n"));
1313 close_file(NULL, fsp, NORMAL_CLOSE);