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 /****************************************************************************
145 ****************************************************************************/
147 NTSTATUS delete_all_streams(connection_struct *conn,
148 const struct smb_filename *smb_fname)
150 struct stream_struct *stream_info = NULL;
152 unsigned int num_streams = 0;
153 TALLOC_CTX *frame = talloc_stackframe();
156 status = vfs_streaminfo(conn, NULL, smb_fname, talloc_tos(),
157 &num_streams, &stream_info);
159 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
160 DEBUG(10, ("no streams around\n"));
165 if (!NT_STATUS_IS_OK(status)) {
166 DEBUG(10, ("vfs_streaminfo failed: %s\n",
171 DEBUG(10, ("delete_all_streams found %d streams\n",
174 if (num_streams == 0) {
179 for (i=0; i<num_streams; i++) {
181 struct smb_filename *smb_fname_stream;
183 if (strequal(stream_info[i].name, "::$DATA")) {
187 smb_fname_stream = synthetic_smb_fname(talloc_tos(),
188 smb_fname->base_name,
192 ~SMB_FILENAME_POSIX_PATH));
194 if (smb_fname_stream == NULL) {
195 DEBUG(0, ("talloc_aprintf failed\n"));
196 status = NT_STATUS_NO_MEMORY;
200 res = SMB_VFS_UNLINKAT(conn,
206 status = map_nt_error_from_unix(errno);
207 DEBUG(10, ("Could not delete stream %s: %s\n",
208 smb_fname_str_dbg(smb_fname_stream),
210 TALLOC_FREE(smb_fname_stream);
213 TALLOC_FREE(smb_fname_stream);
221 struct has_other_nonposix_opens_state {
226 static bool has_other_nonposix_opens_fn(
227 struct share_mode_entry *e,
231 struct has_other_nonposix_opens_state *state = private_data;
232 struct files_struct *fsp = state->fsp;
234 if (e->name_hash != fsp->name_hash) {
237 if ((fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) &&
238 (e->flags & SHARE_MODE_FLAG_POSIX_OPEN)) {
241 if (e->share_file_id == fsp->fh->gen_id) {
242 struct server_id self = messaging_server_id(
243 fsp->conn->sconn->msg_ctx);
244 if (server_id_equal(&self, &e->pid)) {
248 if (share_entry_stale_pid(e)) {
252 state->found_another = true;
256 bool has_other_nonposix_opens(struct share_mode_lock *lck,
257 struct files_struct *fsp)
259 struct has_other_nonposix_opens_state state = { .fsp = fsp };
262 ok = share_mode_forall_entries(
263 lck, has_other_nonposix_opens_fn, &state);
267 return state.found_another;
270 /****************************************************************************
271 Deal with removing a share mode on last close.
272 ****************************************************************************/
274 static NTSTATUS close_remove_share_mode(files_struct *fsp,
275 enum file_close_type close_type)
277 connection_struct *conn = fsp->conn;
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;
290 /* Ensure any pending write time updates are done. */
291 if (fsp->update_write_time_event) {
292 fsp_flush_write_time_update(fsp);
296 * Lock the share entries, and determine if we should delete
297 * on close. If so delete whilst the lock is still in effect.
298 * This prevents race conditions with the file being created. JRA.
301 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
303 DEBUG(0, ("close_remove_share_mode: Could not get share mode "
304 "lock for file %s\n", fsp_str_dbg(fsp)));
305 return NT_STATUS_INVALID_PARAMETER;
308 /* Remove the oplock before potentially deleting the file. */
309 if(fsp->oplock_type) {
313 if (fsp->write_time_forced) {
314 DEBUG(10,("close_remove_share_mode: write time forced "
317 set_close_write_time(fsp, lck->data->changed_write_time);
318 } else if (fsp->update_write_time_on_close) {
319 /* Someone had a pending write. */
320 if (null_timespec(fsp->close_write_time)) {
321 DEBUG(10,("close_remove_share_mode: update to current time "
324 /* Update to current time due to "normal" write. */
325 set_close_write_time(fsp, timespec_current());
327 DEBUG(10,("close_remove_share_mode: write time pending "
330 /* Update to time set on close call. */
331 set_close_write_time(fsp, fsp->close_write_time);
335 if (fsp->initial_delete_on_close &&
336 !is_delete_on_close_set(lck, fsp->name_hash)) {
337 bool became_user = False;
339 /* Initial delete on close was set and no one else
340 * wrote a real delete on close. */
342 if (get_current_vuid(conn) != fsp->vuid) {
343 become_user_without_service(conn, fsp->vuid);
346 fsp->delete_on_close = true;
347 set_delete_on_close_lck(fsp, lck,
348 get_current_nttok(conn),
349 get_current_utok(conn));
351 unbecome_user_without_service();
355 delete_file = is_delete_on_close_set(lck, fsp->name_hash) &&
356 !has_other_nonposix_opens(lck, fsp);
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 struct file_id_buf ftmp1, ftmp2;
426 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
427 "was set and dev and/or inode does not match\n",
429 DEBUG(5,("close_remove_share_mode: file %s. stored file_id %s, "
432 file_id_str_buf(fsp->file_id, &ftmp1),
433 file_id_str_buf(id, &ftmp2)));
435 * Don't save the errno here, we ignore this error
440 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
441 && !is_ntfs_stream_smb_fname(fsp->fsp_name)) {
443 status = delete_all_streams(conn, fsp->fsp_name);
445 if (!NT_STATUS_IS_OK(status)) {
446 DEBUG(5, ("delete_all_streams failed: %s\n",
452 if (fsp->kernel_share_modes_taken) {
456 * A file system sharemode could block the unlink;
457 * remove filesystem sharemodes first.
459 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, 0, 0);
460 if (ret_flock == -1) {
461 DBG_INFO("removing kernel flock for %s failed: %s\n",
462 fsp_str_dbg(fsp), strerror(errno));
465 fsp->kernel_share_modes_taken = false;
469 ret = SMB_VFS_UNLINKAT(conn,
475 * This call can potentially fail as another smbd may
476 * have had the file open with delete on close set and
477 * deleted it when its last reference to this file
478 * went away. Hence we log this but not at debug level
482 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
483 "was set and unlink failed with error %s\n",
484 fsp_str_dbg(fsp), strerror(errno)));
486 status = map_nt_error_from_unix(errno);
489 /* As we now have POSIX opens which can unlink
490 * with other open files we may have taken
491 * this code path with more than one share mode
492 * entry - ensure we only delete once by resetting
493 * the delete on close flag. JRA.
496 fsp->delete_on_close = false;
497 reset_delete_on_close_lck(fsp, lck);
506 if (fsp->kernel_share_modes_taken) {
509 /* remove filesystem sharemodes */
510 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, 0, 0);
511 if (ret_flock == -1) {
512 DEBUG(2, ("close_remove_share_mode: removing kernel "
513 "flock for %s failed: %s\n",
514 fsp_str_dbg(fsp), strerror(errno)));
518 if (!del_share_mode(lck, fsp)) {
519 DEBUG(0, ("close_remove_share_mode: Could not delete share "
520 "entry for file %s\n", fsp_str_dbg(fsp)));
527 * Do the notification after we released the share
528 * mode lock. Inside notify_fname we take out another
529 * tdb lock. With ctdb also accessing our databases,
530 * this can lead to deadlocks. Putting this notify
531 * after the TALLOC_FREE(lck) above we avoid locking
532 * two records simultaneously. Notifies are async and
533 * informational only, so calling the notify_fname
534 * without holding the share mode lock should not do
537 notify_fname(conn, NOTIFY_ACTION_REMOVED,
538 FILE_NOTIFY_CHANGE_FILE_NAME,
539 fsp->fsp_name->base_name);
545 void set_close_write_time(struct files_struct *fsp, struct timespec ts)
547 DEBUG(6,("close_write_time: %s" , time_to_asc(convert_timespec_to_time_t(ts))));
549 if (null_timespec(ts)) {
552 fsp->write_time_forced = false;
553 fsp->update_write_time_on_close = true;
554 fsp->close_write_time = ts;
557 static NTSTATUS update_write_time_on_close(struct files_struct *fsp)
559 struct smb_file_time ft;
561 struct share_mode_lock *lck = NULL;
565 if (!fsp->update_write_time_on_close) {
569 if (null_timespec(fsp->close_write_time)) {
570 fsp->close_write_time = timespec_current();
573 /* Ensure we have a valid stat struct for the source. */
574 status = vfs_stat_fsp(fsp);
575 if (!NT_STATUS_IS_OK(status)) {
579 if (!VALID_STAT(fsp->fsp_name->st)) {
580 /* if it doesn't seem to be a real file */
585 * get_existing_share_mode_lock() isn't really the right
586 * call here, as we're being called after
587 * close_remove_share_mode() inside close_normal_file()
588 * so it's quite normal to not have an existing share
589 * mode here. However, get_share_mode_lock() doesn't
590 * work because that will create a new share mode if
591 * one doesn't exist - so stick with this call (just
592 * ignore any error we get if the share mode doesn't
596 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
598 /* On close if we're changing the real file time we
599 * must update it in the open file db too. */
600 (void)set_write_time(fsp->file_id, fsp->close_write_time);
602 /* Close write times overwrite sticky write times
603 so we must replace any sticky write time here. */
604 if (!null_timespec(lck->data->changed_write_time)) {
605 (void)set_sticky_write_time(fsp->file_id, fsp->close_write_time);
610 ft.mtime = fsp->close_write_time;
611 /* As this is a close based update, we are not directly changing the
612 file attributes from a client call, but indirectly from a write. */
613 status = smb_set_file_time(fsp->conn, fsp, fsp->fsp_name, &ft, false);
614 if (!NT_STATUS_IS_OK(status)) {
615 DEBUG(10,("update_write_time_on_close: smb_set_file_time "
616 "on file %s returned %s\n",
625 static NTSTATUS ntstatus_keeperror(NTSTATUS s1, NTSTATUS s2)
627 if (!NT_STATUS_IS_OK(s1)) {
633 /****************************************************************************
636 close_type can be NORMAL_CLOSE=0,SHUTDOWN_CLOSE,ERROR_CLOSE.
637 printing and magic scripts are only run on normal close.
638 delete on close is done on normal and shutdown close.
639 ****************************************************************************/
641 static NTSTATUS close_normal_file(struct smb_request *req, files_struct *fsp,
642 enum file_close_type close_type)
644 NTSTATUS status = NT_STATUS_OK;
646 connection_struct *conn = fsp->conn;
647 bool is_durable = false;
649 if (fsp->num_aio_requests != 0) {
651 if (close_type != SHUTDOWN_CLOSE) {
653 * reply_close and the smb2 close must have
654 * taken care of this. No other callers of
655 * close_file should ever have created async
658 * We need to panic here because if we close()
659 * the fd while we have outstanding async I/O
660 * requests, in the worst case we could end up
661 * writing to the wrong file.
663 DEBUG(0, ("fsp->num_aio_requests=%u\n",
664 fsp->num_aio_requests));
665 smb_panic("can not close with outstanding aio "
670 * For shutdown close, just drop the async requests
671 * including a potential close request pending for
672 * this fsp. Drop the close request first, the
673 * destructor for the aio_requests would execute it.
675 TALLOC_FREE(fsp->deferred_close);
677 while (fsp->num_aio_requests != 0) {
679 * The destructor of the req will remove
680 * itself from the fsp.
681 * Don't use TALLOC_FREE here, this will overwrite
682 * what the destructor just wrote into
685 talloc_free(fsp->aio_requests[0]);
689 while (talloc_array_length(fsp->blocked_smb1_lock_reqs) != 0) {
690 smbd_smb1_brl_finish_by_req(
691 fsp->blocked_smb1_lock_reqs[0],
692 NT_STATUS_RANGE_NOT_LOCKED);
696 * If we're flushing on a close we can get a write
697 * error here, we must remember this.
700 if (NT_STATUS_IS_OK(status) && fsp->op != NULL) {
701 is_durable = fsp->op->global->durable;
704 if (close_type != SHUTDOWN_CLOSE) {
709 DATA_BLOB new_cookie = data_blob_null;
711 tmp = SMB_VFS_DURABLE_DISCONNECT(fsp,
712 fsp->op->global->backend_cookie,
715 if (NT_STATUS_IS_OK(tmp)) {
720 tv = req->request_time;
722 tv = timeval_current();
724 now = timeval_to_nttime(&tv);
726 data_blob_free(&fsp->op->global->backend_cookie);
727 fsp->op->global->backend_cookie = new_cookie;
729 fsp->op->compat = NULL;
730 tmp = smbXsrv_open_close(fsp->op, now);
731 if (!NT_STATUS_IS_OK(tmp)) {
732 DEBUG(1, ("Failed to update smbXsrv_open "
733 "record when disconnecting durable "
734 "handle for file %s: %s - "
735 "proceeding with normal close\n",
736 fsp_str_dbg(fsp), nt_errstr(tmp)));
738 scavenger_schedule_disconnected(fsp);
740 DEBUG(1, ("Failed to disconnect durable handle for "
741 "file %s: %s - proceeding with normal "
742 "close\n", fsp_str_dbg(fsp), nt_errstr(tmp)));
744 if (!NT_STATUS_IS_OK(tmp)) {
751 * This is the case where we successfully disconnected
752 * a durable handle and closed the underlying file.
753 * In all other cases, we proceed with a genuine close.
755 DEBUG(10, ("%s disconnected durable handle for file %s\n",
756 conn->session_info->unix_info->unix_name,
762 if (fsp->op != NULL) {
764 * Make sure the handle is not marked as durable anymore
766 fsp->op->global->durable = false;
769 if (fsp->print_file) {
770 /* FIXME: return spool errors */
771 print_spool_end(fsp, close_type);
776 /* If this is an old DOS or FCB open and we have multiple opens on
777 the same handle we only have one share mode. Ensure we only remove
778 the share mode on the last close. */
780 if (fsp->fh->ref_count == 1) {
781 /* Should we return on error here... ? */
782 tmp = close_remove_share_mode(fsp, close_type);
783 status = ntstatus_keeperror(status, tmp);
786 locking_close_file(fsp, close_type);
789 status = ntstatus_keeperror(status, tmp);
791 /* check for magic scripts */
792 if (close_type == NORMAL_CLOSE) {
793 tmp = check_magic(fsp);
794 status = ntstatus_keeperror(status, tmp);
798 * Ensure pending modtime is set after close.
801 tmp = update_write_time_on_close(fsp);
802 if (NT_STATUS_EQUAL(tmp, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
803 /* Someone renamed the file or a parent directory containing
804 * this file. We can't do anything about this, we don't have
805 * an "update timestamp by fd" call in POSIX. Eat the error. */
810 status = ntstatus_keeperror(status, tmp);
812 DEBUG(2,("%s closed file %s (numopen=%d) %s\n",
813 conn->session_info->unix_info->unix_name, fsp_str_dbg(fsp),
814 conn->num_files_open - 1,
815 nt_errstr(status) ));
820 /****************************************************************************
821 Function used by reply_rmdir to delete an entire directory
822 tree recursively. Return True on ok, False on fail.
823 ****************************************************************************/
825 bool recursive_rmdir(TALLOC_CTX *ctx,
826 connection_struct *conn,
827 struct smb_filename *smb_dname)
829 const char *dname = NULL;
830 char *talloced = NULL;
834 struct smb_Dir *dir_hnd;
837 SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname));
839 dir_hnd = OpenDir(talloc_tos(), conn, smb_dname, NULL, 0);
843 while((dname = ReadDirName(dir_hnd, &offset, &st, &talloced))) {
844 struct smb_filename *smb_dname_full = NULL;
845 char *fullname = NULL;
846 bool do_break = true;
848 if (ISDOT(dname) || ISDOTDOT(dname)) {
849 TALLOC_FREE(talloced);
853 if (!is_visible_file(conn, smb_dname->base_name, dname, &st,
855 TALLOC_FREE(talloced);
859 /* Construct the full name. */
860 fullname = talloc_asprintf(ctx,
862 smb_dname->base_name,
869 smb_dname_full = synthetic_smb_fname(talloc_tos(),
874 if (smb_dname_full == NULL) {
879 if(SMB_VFS_LSTAT(conn, smb_dname_full) != 0) {
883 if(smb_dname_full->st.st_ex_mode & S_IFDIR) {
884 if(!recursive_rmdir(ctx, conn, smb_dname_full)) {
887 retval = SMB_VFS_UNLINKAT(conn,
895 retval = SMB_VFS_UNLINKAT(conn,
904 /* Successful iteration. */
908 TALLOC_FREE(smb_dname_full);
909 TALLOC_FREE(fullname);
910 TALLOC_FREE(talloced);
916 TALLOC_FREE(dir_hnd);
920 /****************************************************************************
921 The internals of the rmdir code - called elsewhere.
922 ****************************************************************************/
924 static NTSTATUS rmdir_internals(TALLOC_CTX *ctx, files_struct *fsp)
926 connection_struct *conn = fsp->conn;
927 struct smb_filename *smb_dname = fsp->fsp_name;
930 SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname));
932 /* Might be a symlink. */
933 if(SMB_VFS_LSTAT(conn, smb_dname) != 0) {
934 return map_nt_error_from_unix(errno);
937 if (S_ISLNK(smb_dname->st.st_ex_mode)) {
938 /* Is what it points to a directory ? */
939 if(SMB_VFS_STAT(conn, smb_dname) != 0) {
940 return map_nt_error_from_unix(errno);
942 if (!(S_ISDIR(smb_dname->st.st_ex_mode))) {
943 return NT_STATUS_NOT_A_DIRECTORY;
945 ret = SMB_VFS_UNLINKAT(conn,
950 ret = SMB_VFS_UNLINKAT(conn,
956 notify_fname(conn, NOTIFY_ACTION_REMOVED,
957 FILE_NOTIFY_CHANGE_DIR_NAME,
958 smb_dname->base_name);
962 if(((errno == ENOTEMPTY)||(errno == EEXIST)) && *lp_veto_files(talloc_tos(), SNUM(conn))) {
964 * Check to see if the only thing in this directory are
965 * vetoed files/directories. If so then delete them and
966 * retry. If we fail to delete any of them (and we *don't*
967 * do a recursive delete) then fail the rmdir.
970 const char *dname = NULL;
971 char *talloced = NULL;
973 struct smb_Dir *dir_hnd = OpenDir(talloc_tos(), conn,
977 if(dir_hnd == NULL) {
982 while ((dname = ReadDirName(dir_hnd, &dirpos, &st,
983 &talloced)) != NULL) {
984 if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0)) {
985 TALLOC_FREE(talloced);
988 if (!is_visible_file(conn, smb_dname->base_name, dname,
990 TALLOC_FREE(talloced);
993 if(!IS_VETO_PATH(conn, dname)) {
994 TALLOC_FREE(dir_hnd);
995 TALLOC_FREE(talloced);
999 TALLOC_FREE(talloced);
1002 /* We only have veto files/directories.
1003 * Are we allowed to delete them ? */
1005 if(!lp_delete_veto_files(SNUM(conn))) {
1006 TALLOC_FREE(dir_hnd);
1011 /* Do a recursive delete. */
1012 RewindDir(dir_hnd,&dirpos);
1013 while ((dname = ReadDirName(dir_hnd, &dirpos, &st,
1014 &talloced)) != NULL) {
1015 struct smb_filename *smb_dname_full = NULL;
1016 char *fullname = NULL;
1017 bool do_break = true;
1019 if (ISDOT(dname) || ISDOTDOT(dname)) {
1020 TALLOC_FREE(talloced);
1023 if (!is_visible_file(conn, smb_dname->base_name, dname,
1025 TALLOC_FREE(talloced);
1029 fullname = talloc_asprintf(ctx,
1031 smb_dname->base_name,
1039 smb_dname_full = synthetic_smb_fname(talloc_tos(),
1044 if (smb_dname_full == NULL) {
1049 if(SMB_VFS_LSTAT(conn, smb_dname_full) != 0) {
1052 if(smb_dname_full->st.st_ex_mode & S_IFDIR) {
1054 if(!recursive_rmdir(ctx, conn,
1058 retval = SMB_VFS_UNLINKAT(conn,
1066 int retval = SMB_VFS_UNLINKAT(conn,
1075 /* Successful iteration. */
1079 TALLOC_FREE(fullname);
1080 TALLOC_FREE(smb_dname_full);
1081 TALLOC_FREE(talloced);
1085 TALLOC_FREE(dir_hnd);
1086 /* Retry the rmdir */
1087 ret = SMB_VFS_UNLINKAT(conn,
1096 DEBUG(3,("rmdir_internals: couldn't remove directory %s : "
1097 "%s\n", smb_fname_str_dbg(smb_dname),
1099 return map_nt_error_from_unix(errno);
1102 notify_fname(conn, NOTIFY_ACTION_REMOVED,
1103 FILE_NOTIFY_CHANGE_DIR_NAME,
1104 smb_dname->base_name);
1106 return NT_STATUS_OK;
1109 /****************************************************************************
1110 Close a directory opened by an NT SMB call.
1111 ****************************************************************************/
1113 static NTSTATUS close_directory(struct smb_request *req, files_struct *fsp,
1114 enum file_close_type close_type)
1116 struct share_mode_lock *lck = NULL;
1117 bool delete_dir = False;
1118 NTSTATUS status = NT_STATUS_OK;
1119 NTSTATUS status1 = NT_STATUS_OK;
1120 const struct security_token *del_nt_token = NULL;
1121 const struct security_unix_token *del_token = NULL;
1122 NTSTATUS notify_status;
1124 if (fsp->conn->sconn->using_smb2) {
1125 notify_status = STATUS_NOTIFY_CLEANUP;
1127 notify_status = NT_STATUS_OK;
1130 if (fsp->num_aio_requests != 0) {
1131 if (close_type != SHUTDOWN_CLOSE) {
1133 * We panic here because if we close() the fd while we
1134 * have outstanding async I/O requests, an async IO
1135 * request might use the fd. For directories the fd is
1136 * read-only, so this is not as bad as with files, but
1137 * still, better safe then sorry.
1139 DBG_ERR("fsp->num_aio_requests=%u\n",
1140 fsp->num_aio_requests);
1141 smb_panic("close with outstanding aio requests");
1142 return NT_STATUS_INTERNAL_ERROR;
1145 while (fsp->num_aio_requests != 0) {
1147 * The destructor of the req will remove itself from the
1148 * fsp. Don't use TALLOC_FREE here, this will overwrite
1149 * what the destructor just wrote into aio_requests[0].
1151 talloc_free(fsp->aio_requests[0]);
1156 * NT can set delete_on_close of the last open
1157 * reference to a directory also.
1160 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
1162 DEBUG(0, ("close_directory: Could not get share mode lock for "
1163 "%s\n", fsp_str_dbg(fsp)));
1164 file_free(req, fsp);
1165 return NT_STATUS_INVALID_PARAMETER;
1168 if (fsp->initial_delete_on_close) {
1169 bool became_user = False;
1171 /* Initial delete on close was set - for
1172 * directories we don't care if anyone else
1173 * wrote a real delete on close. */
1175 if (get_current_vuid(fsp->conn) != fsp->vuid) {
1176 become_user_without_service(fsp->conn, fsp->vuid);
1179 send_stat_cache_delete_message(fsp->conn->sconn->msg_ctx,
1180 fsp->fsp_name->base_name);
1181 set_delete_on_close_lck(fsp, lck,
1182 get_current_nttok(fsp->conn),
1183 get_current_utok(fsp->conn));
1184 fsp->delete_on_close = true;
1186 unbecome_user_without_service();
1190 delete_dir = get_delete_on_close_token(
1191 lck, fsp->name_hash, &del_nt_token, &del_token) &&
1192 !has_other_nonposix_opens(lck, fsp);
1194 if ((close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE) &&
1197 /* Become the user who requested the delete. */
1199 if (!push_sec_ctx()) {
1200 smb_panic("close_directory: failed to push sec_ctx.\n");
1203 set_sec_ctx(del_token->uid,
1209 if (!del_share_mode(lck, fsp)) {
1210 DEBUG(0, ("close_directory: Could not delete share entry for "
1211 "%s\n", fsp_str_dbg(fsp)));
1216 if ((fsp->conn->fs_capabilities & FILE_NAMED_STREAMS)
1217 && !is_ntfs_stream_smb_fname(fsp->fsp_name)) {
1219 status = delete_all_streams(fsp->conn, fsp->fsp_name);
1220 if (!NT_STATUS_IS_OK(status)) {
1221 DEBUG(5, ("delete_all_streams failed: %s\n",
1222 nt_errstr(status)));
1223 file_free(req, fsp);
1228 status = rmdir_internals(talloc_tos(), fsp);
1230 DEBUG(5,("close_directory: %s. Delete on close was set - "
1231 "deleting directory returned %s.\n",
1232 fsp_str_dbg(fsp), nt_errstr(status)));
1234 /* unbecome user. */
1238 * Ensure we remove any change notify requests that would
1239 * now fail as the directory has been deleted.
1242 if (NT_STATUS_IS_OK(status)) {
1243 notify_status = NT_STATUS_DELETE_PENDING;
1246 if (!del_share_mode(lck, fsp)) {
1247 DEBUG(0, ("close_directory: Could not delete share entry for "
1248 "%s\n", fsp_str_dbg(fsp)));
1254 remove_pending_change_notify_requests_by_fid(fsp, notify_status);
1256 status1 = fd_close(fsp);
1258 if (!NT_STATUS_IS_OK(status1)) {
1259 DEBUG(0, ("Could not close dir! fname=%s, fd=%d, err=%d=%s\n",
1260 fsp_str_dbg(fsp), fsp->fh->fd, errno,
1265 * Do the code common to files and directories.
1267 file_free(req, fsp);
1269 if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(status1)) {
1275 /****************************************************************************
1276 Close a files_struct.
1277 ****************************************************************************/
1279 NTSTATUS close_file(struct smb_request *req, files_struct *fsp,
1280 enum file_close_type close_type)
1283 struct files_struct *base_fsp = fsp->base_fsp;
1285 if(fsp->is_directory) {
1286 status = close_directory(req, fsp, close_type);
1287 } else if (fsp->fake_file_handle != NULL) {
1288 status = close_fake_file(req, fsp);
1290 status = close_normal_file(req, fsp, close_type);
1293 if ((base_fsp != NULL) && (close_type != SHUTDOWN_CLOSE)) {
1296 * fsp was a stream, the base fsp can't be a stream as well
1298 * For SHUTDOWN_CLOSE this is not possible here, because
1299 * SHUTDOWN_CLOSE only happens from files.c which walks the
1300 * complete list of files. If we mess with more than one fsp
1301 * those loops will become confused.
1304 SMB_ASSERT(base_fsp->base_fsp == NULL);
1305 close_file(req, base_fsp, close_type);
1311 /****************************************************************************
1312 Deal with an (authorized) message to close a file given the share mode
1314 ****************************************************************************/
1316 void msg_close_file(struct messaging_context *msg_ctx,
1319 struct server_id server_id,
1322 files_struct *fsp = NULL;
1324 struct share_mode_entry e;
1325 struct smbd_server_connection *sconn =
1326 talloc_get_type_abort(private_data,
1327 struct smbd_server_connection);
1329 message_to_share_mode_entry(&id, &e, (char *)data->data);
1332 char *sm_str = share_mode_str(NULL, 0, &id, &e);
1334 smb_panic("talloc failed");
1336 DEBUG(10,("msg_close_file: got request to close share mode "
1337 "entry %s\n", sm_str));
1338 TALLOC_FREE(sm_str);
1341 fsp = file_find_dif(sconn, id, e.share_file_id);
1343 DEBUG(10,("msg_close_file: failed to find file.\n"));
1346 close_file(NULL, fsp, NORMAL_CLOSE);