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 "librpc/gen_ndr/messaging.h"
25 /****************************************************************************
26 Run a file if it is a magic script.
27 ****************************************************************************/
29 static NTSTATUS
check_magic(struct files_struct
*fsp
)
32 const char *magic_output
= NULL
;
35 TALLOC_CTX
*ctx
= NULL
;
37 struct connection_struct
*conn
= fsp
->conn
;
41 if (!*lp_magicscript(SNUM(conn
))) {
45 DEBUG(5,("checking magic for %s\n", fsp_str_dbg(fsp
)));
47 ctx
= talloc_stackframe();
49 fname
= fsp
->fsp_name
->base_name
;
51 if (!(p
= strrchr_m(fname
,'/'))) {
57 if (!strequal(lp_magicscript(SNUM(conn
)),p
)) {
58 status
= NT_STATUS_OK
;
62 if (*lp_magicoutput(SNUM(conn
))) {
63 magic_output
= lp_magicoutput(SNUM(conn
));
65 magic_output
= talloc_asprintf(ctx
,
70 status
= NT_STATUS_NO_MEMORY
;
74 /* Ensure we don't depend on user's PATH. */
75 p
= talloc_asprintf(ctx
, "./%s", fname
);
77 status
= NT_STATUS_NO_MEMORY
;
81 if (chmod(fname
, 0755) == -1) {
82 status
= map_nt_error_from_unix(errno
);
85 ret
= smbrun(p
,&tmp_fd
);
86 DEBUG(3,("Invoking magic command %s gave %d\n",
90 if (ret
!= 0 || tmp_fd
== -1) {
94 status
= NT_STATUS_UNSUCCESSFUL
;
97 outfd
= open(magic_output
, O_CREAT
|O_EXCL
|O_RDWR
, 0600);
101 status
= map_nt_error_from_unix(err
);
105 if (sys_fstat(tmp_fd
, &st
, false) == -1) {
109 status
= map_nt_error_from_unix(err
);
113 if (transfer_file(tmp_fd
,outfd
,(SMB_OFF_T
)st
.st_ex_size
) == (SMB_OFF_T
)-1) {
117 status
= map_nt_error_from_unix(err
);
121 if (close(outfd
) == -1) {
122 status
= map_nt_error_from_unix(errno
);
126 status
= NT_STATUS_OK
;
133 /****************************************************************************
134 Common code to close a file or a directory.
135 ****************************************************************************/
137 static NTSTATUS
close_filestruct(files_struct
*fsp
)
139 NTSTATUS status
= NT_STATUS_OK
;
141 if (fsp
->fh
->fd
!= -1) {
142 if(flush_write_cache(fsp
, CLOSE_FLUSH
) == -1) {
143 status
= map_nt_error_from_unix(errno
);
145 delete_write_cache(fsp
);
151 /****************************************************************************
152 If any deferred opens are waiting on this close, notify them.
153 ****************************************************************************/
155 static void notify_deferred_opens(struct share_mode_lock
*lck
)
159 if (!should_notify_deferred_opens()) {
163 for (i
=0; i
<lck
->num_share_modes
; i
++) {
164 struct share_mode_entry
*e
= &lck
->share_modes
[i
];
166 if (!is_deferred_open_entry(e
)) {
170 if (procid_is_me(&e
->pid
)) {
172 * We need to notify ourself to retry the open. Do
173 * this by finding the queued SMB record, moving it to
174 * the head of the queue and changing the wait time to
177 schedule_deferred_open_message_smb(e
->op_mid
);
179 char msg
[MSG_SMB_SHARE_MODE_ENTRY_SIZE
];
181 share_mode_entry_to_message(msg
, e
);
183 messaging_send_buf(smbd_messaging_context(),
184 e
->pid
, MSG_SMB_OPEN_RETRY
,
186 MSG_SMB_SHARE_MODE_ENTRY_SIZE
);
191 /****************************************************************************
193 ****************************************************************************/
195 NTSTATUS
delete_all_streams(connection_struct
*conn
, const char *fname
)
197 struct stream_struct
*stream_info
;
199 unsigned int num_streams
;
200 TALLOC_CTX
*frame
= talloc_stackframe();
203 status
= SMB_VFS_STREAMINFO(conn
, NULL
, fname
, talloc_tos(),
204 &num_streams
, &stream_info
);
206 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_IMPLEMENTED
)) {
207 DEBUG(10, ("no streams around\n"));
212 if (!NT_STATUS_IS_OK(status
)) {
213 DEBUG(10, ("SMB_VFS_STREAMINFO failed: %s\n",
218 DEBUG(10, ("delete_all_streams found %d streams\n",
221 if (num_streams
== 0) {
226 for (i
=0; i
<num_streams
; i
++) {
228 struct smb_filename
*smb_fname_stream
= NULL
;
230 if (strequal(stream_info
[i
].name
, "::$DATA")) {
234 status
= create_synthetic_smb_fname(talloc_tos(), fname
,
235 stream_info
[i
].name
, NULL
,
238 if (!NT_STATUS_IS_OK(status
)) {
239 DEBUG(0, ("talloc_aprintf failed\n"));
243 res
= SMB_VFS_UNLINK(conn
, smb_fname_stream
);
246 status
= map_nt_error_from_unix(errno
);
247 DEBUG(10, ("Could not delete stream %s: %s\n",
248 smb_fname_str_dbg(smb_fname_stream
),
250 TALLOC_FREE(smb_fname_stream
);
253 TALLOC_FREE(smb_fname_stream
);
261 /****************************************************************************
262 Deal with removing a share mode on last close.
263 ****************************************************************************/
265 static NTSTATUS
close_remove_share_mode(files_struct
*fsp
,
266 enum file_close_type close_type
)
268 connection_struct
*conn
= fsp
->conn
;
269 bool delete_file
= false;
270 bool changed_user
= false;
271 struct share_mode_lock
*lck
= NULL
;
272 NTSTATUS status
= NT_STATUS_OK
;
276 /* Ensure any pending write time updates are done. */
277 if (fsp
->update_write_time_event
) {
278 update_write_time_handler(smbd_event_context(),
279 fsp
->update_write_time_event
,
285 * Lock the share entries, and determine if we should delete
286 * on close. If so delete whilst the lock is still in effect.
287 * This prevents race conditions with the file being created. JRA.
290 lck
= get_share_mode_lock(talloc_tos(), fsp
->file_id
, NULL
, NULL
,
294 DEBUG(0, ("close_remove_share_mode: Could not get share mode "
295 "lock for file %s\n", fsp_str_dbg(fsp
)));
296 status
= NT_STATUS_INVALID_PARAMETER
;
300 if (fsp
->write_time_forced
) {
301 DEBUG(10,("close_remove_share_mode: write time forced "
304 set_close_write_time(fsp
, lck
->changed_write_time
);
305 } else if (fsp
->update_write_time_on_close
) {
306 /* Someone had a pending write. */
307 if (null_timespec(fsp
->close_write_time
)) {
308 DEBUG(10,("close_remove_share_mode: update to current time "
311 /* Update to current time due to "normal" write. */
312 set_close_write_time(fsp
, timespec_current());
314 DEBUG(10,("close_remove_share_mode: write time pending "
317 /* Update to time set on close call. */
318 set_close_write_time(fsp
, fsp
->close_write_time
);
322 if (!del_share_mode(lck
, fsp
)) {
323 DEBUG(0, ("close_remove_share_mode: Could not delete share "
324 "entry for file %s\n",
328 if (fsp
->initial_delete_on_close
&& (lck
->delete_token
== NULL
)) {
329 bool became_user
= False
;
331 /* Initial delete on close was set and no one else
332 * wrote a real delete on close. */
334 if (get_current_vuid(conn
) != fsp
->vuid
) {
335 become_user(conn
, fsp
->vuid
);
338 fsp
->delete_on_close
= true;
339 set_delete_on_close_lck(lck
, True
, get_current_utok(conn
));
345 delete_file
= lck
->delete_on_close
;
349 /* See if others still have the file open. If this is the
350 * case, then don't delete. If all opens are POSIX delete now. */
351 for (i
=0; i
<lck
->num_share_modes
; i
++) {
352 struct share_mode_entry
*e
= &lck
->share_modes
[i
];
353 if (is_valid_share_mode_entry(e
)) {
354 if (fsp
->posix_open
&& (e
->flags
& SHARE_MODE_FLAG_POSIX_OPEN
)) {
363 /* Notify any deferred opens waiting on this close. */
364 notify_deferred_opens(lck
);
365 reply_to_oplock_break_requests(fsp
);
368 * NT can set delete_on_close of the last open
369 * reference to a file.
372 if (!(close_type
== NORMAL_CLOSE
|| close_type
== SHUTDOWN_CLOSE
)
374 || (lck
->delete_token
== NULL
)) {
380 * Ok, we have to delete the file
383 DEBUG(5,("close_remove_share_mode: file %s. Delete on close was set "
384 "- deleting file.\n", fsp_str_dbg(fsp
)));
387 * Don't try to update the write time when we delete the file
389 fsp
->update_write_time_on_close
= false;
391 if (!unix_token_equal(lck
->delete_token
, get_current_utok(conn
))) {
392 /* Become the user who requested the delete. */
394 DEBUG(5,("close_remove_share_mode: file %s. "
395 "Change user to uid %u\n",
397 (unsigned int)lck
->delete_token
->uid
));
399 if (!push_sec_ctx()) {
400 smb_panic("close_remove_share_mode: file %s. failed to push "
404 set_sec_ctx(lck
->delete_token
->uid
,
405 lck
->delete_token
->gid
,
406 lck
->delete_token
->ngroups
,
407 lck
->delete_token
->groups
,
413 /* We can only delete the file if the name we have is still valid and
414 hasn't been renamed. */
416 tmp_status
= vfs_stat_fsp(fsp
);
417 if (!NT_STATUS_IS_OK(tmp_status
)) {
418 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
419 "was set and stat failed with error %s\n",
420 fsp_str_dbg(fsp
), nt_errstr(tmp_status
)));
422 * Don't save the errno here, we ignore this error
427 id
= vfs_file_id_from_sbuf(conn
, &fsp
->fsp_name
->st
);
429 if (!file_id_equal(&fsp
->file_id
, &id
)) {
430 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
431 "was set and dev and/or inode does not match\n",
433 DEBUG(5,("close_remove_share_mode: file %s. stored file_id %s, "
436 file_id_string_tos(&fsp
->file_id
),
437 file_id_string_tos(&id
)));
439 * Don't save the errno here, we ignore this error
444 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
)
445 && !is_ntfs_stream_smb_fname(fsp
->fsp_name
)) {
447 status
= delete_all_streams(conn
, fsp
->fsp_name
->base_name
);
449 if (!NT_STATUS_IS_OK(status
)) {
450 DEBUG(5, ("delete_all_streams failed: %s\n",
457 if (SMB_VFS_UNLINK(conn
, fsp
->fsp_name
) != 0) {
459 * This call can potentially fail as another smbd may
460 * have had the file open with delete on close set and
461 * deleted it when its last reference to this file
462 * went away. Hence we log this but not at debug level
466 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
467 "was set and unlink failed with error %s\n",
468 fsp_str_dbg(fsp
), strerror(errno
)));
470 status
= map_nt_error_from_unix(errno
);
473 notify_fname(conn
, NOTIFY_ACTION_REMOVED
,
474 FILE_NOTIFY_CHANGE_FILE_NAME
,
475 fsp
->fsp_name
->base_name
);
477 /* As we now have POSIX opens which can unlink
478 * with other open files we may have taken
479 * this code path with more than one share mode
480 * entry - ensure we only delete once by resetting
481 * the delete on close flag. JRA.
484 fsp
->delete_on_close
= false;
485 set_delete_on_close_lck(lck
, False
, NULL
);
498 void set_close_write_time(struct files_struct
*fsp
, struct timespec ts
)
500 DEBUG(6,("close_write_time: %s" , time_to_asc(convert_timespec_to_time_t(ts
))));
502 if (null_timespec(ts
)) {
505 fsp
->write_time_forced
= false;
506 fsp
->update_write_time_on_close
= true;
507 fsp
->close_write_time
= ts
;
510 static NTSTATUS
update_write_time_on_close(struct files_struct
*fsp
)
512 struct smb_file_time ft
;
514 struct share_mode_lock
*lck
= NULL
;
518 if (!fsp
->update_write_time_on_close
) {
522 if (null_timespec(fsp
->close_write_time
)) {
523 fsp
->close_write_time
= timespec_current();
526 /* Ensure we have a valid stat struct for the source. */
527 status
= vfs_stat_fsp(fsp
);
528 if (!NT_STATUS_IS_OK(status
)) {
532 if (!VALID_STAT(fsp
->fsp_name
->st
)) {
533 /* if it doesn't seem to be a real file */
537 /* On close if we're changing the real file time we
538 * must update it in the open file db too. */
539 (void)set_write_time(fsp
->file_id
, fsp
->close_write_time
);
541 lck
= get_share_mode_lock(talloc_tos(), fsp
->file_id
, NULL
, NULL
, NULL
);
543 /* Close write times overwrite sticky write times
544 so we must replace any sticky write time here. */
545 if (!null_timespec(lck
->changed_write_time
)) {
546 (void)set_sticky_write_time(fsp
->file_id
, fsp
->close_write_time
);
551 ft
.mtime
= fsp
->close_write_time
;
552 status
= smb_set_file_time(fsp
->conn
, fsp
, fsp
->fsp_name
, &ft
, false);
553 if (!NT_STATUS_IS_OK(status
)) {
560 static NTSTATUS
ntstatus_keeperror(NTSTATUS s1
, NTSTATUS s2
)
562 if (!NT_STATUS_IS_OK(s1
)) {
568 /****************************************************************************
571 close_type can be NORMAL_CLOSE=0,SHUTDOWN_CLOSE,ERROR_CLOSE.
572 printing and magic scripts are only run on normal close.
573 delete on close is done on normal and shutdown close.
574 ****************************************************************************/
576 static NTSTATUS
close_normal_file(struct smb_request
*req
, files_struct
*fsp
,
577 enum file_close_type close_type
)
579 NTSTATUS status
= NT_STATUS_OK
;
581 connection_struct
*conn
= fsp
->conn
;
583 if (close_type
== ERROR_CLOSE
) {
584 cancel_aio_by_fsp(fsp
);
587 * If we're finishing async io on a close we can get a write
588 * error here, we must remember this.
590 int ret
= wait_for_aio_completion(fsp
);
592 status
= ntstatus_keeperror(
593 status
, map_nt_error_from_unix(ret
));
598 * If we're flushing on a close we can get a write
599 * error here, we must remember this.
602 tmp
= close_filestruct(fsp
);
603 status
= ntstatus_keeperror(status
, tmp
);
605 if (fsp
->print_file
) {
606 print_fsp_end(fsp
, close_type
);
611 /* Remove the oplock before potentially deleting the file. */
612 if(fsp
->oplock_type
) {
613 release_file_oplock(fsp
);
616 /* If this is an old DOS or FCB open and we have multiple opens on
617 the same handle we only have one share mode. Ensure we only remove
618 the share mode on the last close. */
620 if (fsp
->fh
->ref_count
== 1) {
621 /* Should we return on error here... ? */
622 tmp
= close_remove_share_mode(fsp
, close_type
);
623 status
= ntstatus_keeperror(status
, tmp
);
626 locking_close_file(smbd_messaging_context(), fsp
, close_type
);
629 status
= ntstatus_keeperror(status
, tmp
);
631 /* check for magic scripts */
632 if (close_type
== NORMAL_CLOSE
) {
633 tmp
= check_magic(fsp
);
634 status
= ntstatus_keeperror(status
, tmp
);
638 * Ensure pending modtime is set after close.
641 tmp
= update_write_time_on_close(fsp
);
642 if (NT_STATUS_EQUAL(tmp
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
643 /* Someone renamed the file or a parent directory containing
644 * this file. We can't do anything about this, we don't have
645 * an "update timestamp by fd" call in POSIX. Eat the error. */
650 status
= ntstatus_keeperror(status
, tmp
);
652 DEBUG(2,("%s closed file %s (numopen=%d) %s\n",
653 conn
->server_info
->unix_name
, fsp_str_dbg(fsp
),
654 conn
->num_files_open
- 1,
655 nt_errstr(status
) ));
660 /****************************************************************************
661 Static function used by reply_rmdir to delete an entire directory
662 tree recursively. Return True on ok, False on fail.
663 ****************************************************************************/
665 static bool recursive_rmdir(TALLOC_CTX
*ctx
,
666 connection_struct
*conn
,
667 struct smb_filename
*smb_dname
)
669 const char *dname
= NULL
;
670 char *talloced
= NULL
;
674 struct smb_Dir
*dir_hnd
;
676 SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname
));
678 dir_hnd
= OpenDir(talloc_tos(), conn
, smb_dname
->base_name
, NULL
, 0);
682 while((dname
= ReadDirName(dir_hnd
, &offset
, &st
, &talloced
))) {
683 struct smb_filename
*smb_dname_full
= NULL
;
684 char *fullname
= NULL
;
685 bool do_break
= true;
688 if (ISDOT(dname
) || ISDOTDOT(dname
)) {
689 TALLOC_FREE(talloced
);
693 if (!is_visible_file(conn
, smb_dname
->base_name
, dname
, &st
,
695 TALLOC_FREE(talloced
);
699 /* Construct the full name. */
700 fullname
= talloc_asprintf(ctx
,
702 smb_dname
->base_name
,
709 status
= create_synthetic_smb_fname(talloc_tos(), fullname
,
712 if (!NT_STATUS_IS_OK(status
)) {
716 if(SMB_VFS_LSTAT(conn
, smb_dname_full
) != 0) {
720 if(smb_dname_full
->st
.st_ex_mode
& S_IFDIR
) {
721 if(!recursive_rmdir(ctx
, conn
, smb_dname_full
)) {
724 if(SMB_VFS_RMDIR(conn
,
725 smb_dname_full
->base_name
) != 0) {
728 } else if(SMB_VFS_UNLINK(conn
, smb_dname_full
) != 0) {
732 /* Successful iteration. */
736 TALLOC_FREE(smb_dname_full
);
737 TALLOC_FREE(fullname
);
738 TALLOC_FREE(talloced
);
744 TALLOC_FREE(dir_hnd
);
748 /****************************************************************************
749 The internals of the rmdir code - called elsewhere.
750 ****************************************************************************/
752 static NTSTATUS
rmdir_internals(TALLOC_CTX
*ctx
, files_struct
*fsp
)
754 connection_struct
*conn
= fsp
->conn
;
755 struct smb_filename
*smb_dname
= fsp
->fsp_name
;
758 SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname
));
760 /* Might be a symlink. */
761 if(SMB_VFS_LSTAT(conn
, smb_dname
) != 0) {
762 return map_nt_error_from_unix(errno
);
765 if (S_ISLNK(smb_dname
->st
.st_ex_mode
)) {
766 /* Is what it points to a directory ? */
767 if(SMB_VFS_STAT(conn
, smb_dname
) != 0) {
768 return map_nt_error_from_unix(errno
);
770 if (!(S_ISDIR(smb_dname
->st
.st_ex_mode
))) {
771 return NT_STATUS_NOT_A_DIRECTORY
;
773 ret
= SMB_VFS_UNLINK(conn
, smb_dname
);
775 ret
= SMB_VFS_RMDIR(conn
, smb_dname
->base_name
);
778 notify_fname(conn
, NOTIFY_ACTION_REMOVED
,
779 FILE_NOTIFY_CHANGE_DIR_NAME
,
780 smb_dname
->base_name
);
784 if(((errno
== ENOTEMPTY
)||(errno
== EEXIST
)) && lp_veto_files(SNUM(conn
))) {
786 * Check to see if the only thing in this directory are
787 * vetoed files/directories. If so then delete them and
788 * retry. If we fail to delete any of them (and we *don't*
789 * do a recursive delete) then fail the rmdir.
792 const char *dname
= NULL
;
793 char *talloced
= NULL
;
795 struct smb_Dir
*dir_hnd
= OpenDir(talloc_tos(), conn
,
796 smb_dname
->base_name
, NULL
,
799 if(dir_hnd
== NULL
) {
804 while ((dname
= ReadDirName(dir_hnd
, &dirpos
, &st
,
805 &talloced
)) != NULL
) {
806 if((strcmp(dname
, ".") == 0) || (strcmp(dname
, "..")==0)) {
807 TALLOC_FREE(talloced
);
810 if (!is_visible_file(conn
, smb_dname
->base_name
, dname
,
812 TALLOC_FREE(talloced
);
815 if(!IS_VETO_PATH(conn
, dname
)) {
816 TALLOC_FREE(dir_hnd
);
817 TALLOC_FREE(talloced
);
821 TALLOC_FREE(talloced
);
824 /* We only have veto files/directories.
825 * Are we allowed to delete them ? */
827 if(!lp_recursive_veto_delete(SNUM(conn
))) {
828 TALLOC_FREE(dir_hnd
);
833 /* Do a recursive delete. */
834 RewindDir(dir_hnd
,&dirpos
);
835 while ((dname
= ReadDirName(dir_hnd
, &dirpos
, &st
,
836 &talloced
)) != NULL
) {
837 struct smb_filename
*smb_dname_full
= NULL
;
838 char *fullname
= NULL
;
839 bool do_break
= true;
842 if (ISDOT(dname
) || ISDOTDOT(dname
)) {
843 TALLOC_FREE(talloced
);
846 if (!is_visible_file(conn
, smb_dname
->base_name
, dname
,
848 TALLOC_FREE(talloced
);
852 fullname
= talloc_asprintf(ctx
,
854 smb_dname
->base_name
,
862 status
= create_synthetic_smb_fname(talloc_tos(),
866 if (!NT_STATUS_IS_OK(status
)) {
867 errno
= map_errno_from_nt_status(status
);
871 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
,
879 if(SMB_VFS_RMDIR(conn
,
880 smb_dname_full
->base_name
) != 0) {
883 } else if(SMB_VFS_UNLINK(conn
, smb_dname_full
) != 0) {
887 /* Successful iteration. */
891 TALLOC_FREE(fullname
);
892 TALLOC_FREE(smb_dname_full
);
893 TALLOC_FREE(talloced
);
897 TALLOC_FREE(dir_hnd
);
898 /* Retry the rmdir */
899 ret
= SMB_VFS_RMDIR(conn
, smb_dname
->base_name
);
905 DEBUG(3,("rmdir_internals: couldn't remove directory %s : "
906 "%s\n", smb_fname_str_dbg(smb_dname
),
908 return map_nt_error_from_unix(errno
);
911 notify_fname(conn
, NOTIFY_ACTION_REMOVED
,
912 FILE_NOTIFY_CHANGE_DIR_NAME
,
913 smb_dname
->base_name
);
918 /****************************************************************************
919 Close a directory opened by an NT SMB call.
920 ****************************************************************************/
922 static NTSTATUS
close_directory(struct smb_request
*req
, files_struct
*fsp
,
923 enum file_close_type close_type
)
925 struct share_mode_lock
*lck
= NULL
;
926 bool delete_dir
= False
;
927 NTSTATUS status
= NT_STATUS_OK
;
928 NTSTATUS status1
= NT_STATUS_OK
;
931 * NT can set delete_on_close of the last open
932 * reference to a directory also.
935 lck
= get_share_mode_lock(talloc_tos(), fsp
->file_id
, NULL
, NULL
,
939 DEBUG(0, ("close_directory: Could not get share mode lock for "
940 "%s\n", fsp_str_dbg(fsp
)));
941 status
= NT_STATUS_INVALID_PARAMETER
;
945 if (!del_share_mode(lck
, fsp
)) {
946 DEBUG(0, ("close_directory: Could not delete share entry for "
947 "%s\n", fsp_str_dbg(fsp
)));
950 if (fsp
->initial_delete_on_close
) {
951 bool became_user
= False
;
953 /* Initial delete on close was set - for
954 * directories we don't care if anyone else
955 * wrote a real delete on close. */
957 if (get_current_vuid(fsp
->conn
) != fsp
->vuid
) {
958 become_user(fsp
->conn
, fsp
->vuid
);
961 send_stat_cache_delete_message(fsp
->fsp_name
->base_name
);
962 set_delete_on_close_lck(lck
, True
, get_current_utok(fsp
->conn
));
963 fsp
->delete_on_close
= true;
969 delete_dir
= lck
->delete_on_close
;
973 /* See if others still have the dir open. If this is the
974 * case, then don't delete. If all opens are POSIX delete now. */
975 for (i
=0; i
<lck
->num_share_modes
; i
++) {
976 struct share_mode_entry
*e
= &lck
->share_modes
[i
];
977 if (is_valid_share_mode_entry(e
)) {
978 if (fsp
->posix_open
&& (e
->flags
& SHARE_MODE_FLAG_POSIX_OPEN
)) {
987 if ((close_type
== NORMAL_CLOSE
|| close_type
== SHUTDOWN_CLOSE
) &&
991 /* Become the user who requested the delete. */
993 if (!push_sec_ctx()) {
994 smb_panic("close_directory: failed to push sec_ctx.\n");
997 set_sec_ctx(lck
->delete_token
->uid
,
998 lck
->delete_token
->gid
,
999 lck
->delete_token
->ngroups
,
1000 lck
->delete_token
->groups
,
1005 status
= rmdir_internals(talloc_tos(), fsp
);
1007 DEBUG(5,("close_directory: %s. Delete on close was set - "
1008 "deleting directory returned %s.\n",
1009 fsp_str_dbg(fsp
), nt_errstr(status
)));
1011 /* unbecome user. */
1015 * Ensure we remove any change notify requests that would
1016 * now fail as the directory has been deleted.
1019 if(NT_STATUS_IS_OK(status
)) {
1020 remove_pending_change_notify_requests_by_fid(fsp
, NT_STATUS_DELETE_PENDING
);
1024 remove_pending_change_notify_requests_by_fid(
1028 status1
= fd_close(fsp
);
1030 if (!NT_STATUS_IS_OK(status1
)) {
1031 DEBUG(0, ("Could not close dir! fname=%s, fd=%d, err=%d=%s\n",
1032 fsp_str_dbg(fsp
), fsp
->fh
->fd
, errno
,
1037 dptr_CloseDir(fsp
->dptr
);
1041 * Do the code common to files and directories.
1043 close_filestruct(fsp
);
1044 file_free(req
, fsp
);
1048 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(status1
)) {
1054 /****************************************************************************
1055 Close a files_struct.
1056 ****************************************************************************/
1058 NTSTATUS
close_file(struct smb_request
*req
, files_struct
*fsp
,
1059 enum file_close_type close_type
)
1062 struct files_struct
*base_fsp
= fsp
->base_fsp
;
1064 if(fsp
->is_directory
) {
1065 status
= close_directory(req
, fsp
, close_type
);
1066 } else if (fsp
->fake_file_handle
!= NULL
) {
1067 status
= close_fake_file(req
, fsp
);
1069 status
= close_normal_file(req
, fsp
, close_type
);
1072 if ((base_fsp
!= NULL
) && (close_type
!= SHUTDOWN_CLOSE
)) {
1075 * fsp was a stream, the base fsp can't be a stream as well
1077 * For SHUTDOWN_CLOSE this is not possible here, because
1078 * SHUTDOWN_CLOSE only happens from files.c which walks the
1079 * complete list of files. If we mess with more than one fsp
1080 * those loops will become confused.
1083 SMB_ASSERT(base_fsp
->base_fsp
== NULL
);
1084 close_file(req
, base_fsp
, close_type
);
1090 /****************************************************************************
1091 Deal with an (authorized) message to close a file given the share mode
1093 ****************************************************************************/
1095 void msg_close_file(struct messaging_context
*msg_ctx
,
1098 struct server_id server_id
,
1101 files_struct
*fsp
= NULL
;
1102 struct share_mode_entry e
;
1104 message_to_share_mode_entry(&e
, (char *)data
->data
);
1107 char *sm_str
= share_mode_str(NULL
, 0, &e
);
1109 smb_panic("talloc failed");
1111 DEBUG(10,("msg_close_file: got request to close share mode "
1112 "entry %s\n", sm_str
));
1113 TALLOC_FREE(sm_str
);
1116 fsp
= file_find_dif(e
.id
, e
.share_file_id
);
1118 DEBUG(10,("msg_close_file: failed to find file.\n"));
1121 close_file(NULL
, fsp
, NORMAL_CLOSE
);