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 /****************************************************************************
237 Deal with removing a share mode on last close.
238 ****************************************************************************/
240 static NTSTATUS
close_remove_share_mode(files_struct
*fsp
,
241 enum file_close_type close_type
)
243 connection_struct
*conn
= fsp
->conn
;
244 struct server_id self
= messaging_server_id(conn
->sconn
->msg_ctx
);
245 bool delete_file
= false;
246 bool changed_user
= false;
247 struct share_mode_lock
*lck
= NULL
;
248 NTSTATUS status
= NT_STATUS_OK
;
251 const struct security_unix_token
*del_token
= NULL
;
252 const struct security_token
*del_nt_token
= NULL
;
253 bool got_tokens
= false;
256 /* Ensure any pending write time updates are done. */
257 if (fsp
->update_write_time_event
) {
258 update_write_time_handler(fsp
->conn
->sconn
->ev_ctx
,
259 fsp
->update_write_time_event
,
265 * Lock the share entries, and determine if we should delete
266 * on close. If so delete whilst the lock is still in effect.
267 * This prevents race conditions with the file being created. JRA.
270 lck
= get_existing_share_mode_lock(talloc_tos(), fsp
->file_id
);
272 DEBUG(0, ("close_remove_share_mode: Could not get share mode "
273 "lock for file %s\n", fsp_str_dbg(fsp
)));
274 return NT_STATUS_INVALID_PARAMETER
;
277 /* Remove the oplock before potentially deleting the file. */
278 if(fsp
->oplock_type
) {
279 remove_oplock_under_lock(fsp
, lck
);
282 if (fsp
->write_time_forced
) {
283 DEBUG(10,("close_remove_share_mode: write time forced "
286 set_close_write_time(fsp
, lck
->data
->changed_write_time
);
287 } else if (fsp
->update_write_time_on_close
) {
288 /* Someone had a pending write. */
289 if (null_timespec(fsp
->close_write_time
)) {
290 DEBUG(10,("close_remove_share_mode: update to current time "
293 /* Update to current time due to "normal" write. */
294 set_close_write_time(fsp
, timespec_current());
296 DEBUG(10,("close_remove_share_mode: write time pending "
299 /* Update to time set on close call. */
300 set_close_write_time(fsp
, fsp
->close_write_time
);
304 if (fsp
->initial_delete_on_close
&&
305 !is_delete_on_close_set(lck
, fsp
->name_hash
)) {
306 bool became_user
= False
;
308 /* Initial delete on close was set and no one else
309 * wrote a real delete on close. */
311 if (get_current_vuid(conn
) != fsp
->vuid
) {
312 become_user(conn
, fsp
->vuid
);
315 fsp
->delete_on_close
= true;
316 set_delete_on_close_lck(fsp
, lck
,
317 get_current_nttok(conn
),
318 get_current_utok(conn
));
324 delete_file
= is_delete_on_close_set(lck
, fsp
->name_hash
);
328 /* See if others still have the file open via this pathname.
329 If this is the case, then don't delete. If all opens are
331 for (i
=0; i
<lck
->data
->num_share_modes
; i
++) {
332 struct share_mode_entry
*e
= &lck
->data
->share_modes
[i
];
334 if (!is_valid_share_mode_entry(e
)) {
337 if (e
->name_hash
!= fsp
->name_hash
) {
340 if ((fsp
->posix_flags
& FSP_POSIX_FLAGS_OPEN
)
341 && (e
->flags
& SHARE_MODE_FLAG_POSIX_OPEN
)) {
344 if (serverid_equal(&self
, &e
->pid
) &&
345 (e
->share_file_id
== fsp
->fh
->gen_id
)) {
348 if (share_mode_stale_pid(lck
->data
, i
)) {
357 * NT can set delete_on_close of the last open
358 * reference to a file.
361 normal_close
= (close_type
== NORMAL_CLOSE
|| close_type
== SHUTDOWN_CLOSE
);
363 if (!normal_close
|| !delete_file
) {
364 status
= NT_STATUS_OK
;
369 * Ok, we have to delete the file
372 DEBUG(5,("close_remove_share_mode: file %s. Delete on close was set "
373 "- deleting file.\n", fsp_str_dbg(fsp
)));
376 * Don't try to update the write time when we delete the file
378 fsp
->update_write_time_on_close
= false;
380 got_tokens
= get_delete_on_close_token(lck
, fsp
->name_hash
,
381 &del_nt_token
, &del_token
);
382 SMB_ASSERT(got_tokens
);
384 if (!unix_token_equal(del_token
, get_current_utok(conn
))) {
385 /* Become the user who requested the delete. */
387 DEBUG(5,("close_remove_share_mode: file %s. "
388 "Change user to uid %u\n",
390 (unsigned int)del_token
->uid
));
392 if (!push_sec_ctx()) {
393 smb_panic("close_remove_share_mode: file %s. failed to push "
397 set_sec_ctx(del_token
->uid
,
406 /* We can only delete the file if the name we have is still valid and
407 hasn't been renamed. */
409 tmp_status
= vfs_stat_fsp(fsp
);
410 if (!NT_STATUS_IS_OK(tmp_status
)) {
411 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
412 "was set and stat failed with error %s\n",
413 fsp_str_dbg(fsp
), nt_errstr(tmp_status
)));
415 * Don't save the errno here, we ignore this error
420 id
= vfs_file_id_from_sbuf(conn
, &fsp
->fsp_name
->st
);
422 if (!file_id_equal(&fsp
->file_id
, &id
)) {
423 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
424 "was set and dev and/or inode does not match\n",
426 DEBUG(5,("close_remove_share_mode: file %s. stored file_id %s, "
429 file_id_string_tos(&fsp
->file_id
),
430 file_id_string_tos(&id
)));
432 * Don't save the errno here, we ignore this error
437 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
)
438 && !is_ntfs_stream_smb_fname(fsp
->fsp_name
)) {
440 status
= delete_all_streams(conn
, fsp
->fsp_name
);
442 if (!NT_STATUS_IS_OK(status
)) {
443 DEBUG(5, ("delete_all_streams failed: %s\n",
450 if (SMB_VFS_UNLINK(conn
, fsp
->fsp_name
) != 0) {
452 * This call can potentially fail as another smbd may
453 * have had the file open with delete on close set and
454 * deleted it when its last reference to this file
455 * went away. Hence we log this but not at debug level
459 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
460 "was set and unlink failed with error %s\n",
461 fsp_str_dbg(fsp
), strerror(errno
)));
463 status
= map_nt_error_from_unix(errno
);
466 /* As we now have POSIX opens which can unlink
467 * with other open files we may have taken
468 * this code path with more than one share mode
469 * entry - ensure we only delete once by resetting
470 * the delete on close flag. JRA.
473 fsp
->delete_on_close
= false;
474 reset_delete_on_close_lck(fsp
, lck
);
483 if (fsp
->kernel_share_modes_taken
) {
486 /* remove filesystem sharemodes */
487 ret_flock
= SMB_VFS_KERNEL_FLOCK(fsp
, 0, 0);
488 if (ret_flock
== -1) {
489 DEBUG(2, ("close_remove_share_mode: removing kernel "
490 "flock for %s failed: %s\n",
491 fsp_str_dbg(fsp
), strerror(errno
)));
495 if (!del_share_mode(lck
, fsp
)) {
496 DEBUG(0, ("close_remove_share_mode: Could not delete share "
497 "entry for file %s\n", fsp_str_dbg(fsp
)));
504 * Do the notification after we released the share
505 * mode lock. Inside notify_fname we take out another
506 * tdb lock. With ctdb also accessing our databases,
507 * this can lead to deadlocks. Putting this notify
508 * after the TALLOC_FREE(lck) above we avoid locking
509 * two records simultaneously. Notifies are async and
510 * informational only, so calling the notify_fname
511 * without holding the share mode lock should not do
514 notify_fname(conn
, NOTIFY_ACTION_REMOVED
,
515 FILE_NOTIFY_CHANGE_FILE_NAME
,
516 fsp
->fsp_name
->base_name
);
522 void set_close_write_time(struct files_struct
*fsp
, struct timespec ts
)
524 DEBUG(6,("close_write_time: %s" , time_to_asc(convert_timespec_to_time_t(ts
))));
526 if (null_timespec(ts
)) {
529 fsp
->write_time_forced
= false;
530 fsp
->update_write_time_on_close
= true;
531 fsp
->close_write_time
= ts
;
534 static NTSTATUS
update_write_time_on_close(struct files_struct
*fsp
)
536 struct smb_file_time ft
;
538 struct share_mode_lock
*lck
= NULL
;
542 if (!fsp
->update_write_time_on_close
) {
546 if (null_timespec(fsp
->close_write_time
)) {
547 fsp
->close_write_time
= timespec_current();
550 /* Ensure we have a valid stat struct for the source. */
551 status
= vfs_stat_fsp(fsp
);
552 if (!NT_STATUS_IS_OK(status
)) {
556 if (!VALID_STAT(fsp
->fsp_name
->st
)) {
557 /* if it doesn't seem to be a real file */
562 * get_existing_share_mode_lock() isn't really the right
563 * call here, as we're being called after
564 * close_remove_share_mode() inside close_normal_file()
565 * so it's quite normal to not have an existing share
566 * mode here. However, get_share_mode_lock() doesn't
567 * work because that will create a new share mode if
568 * one doesn't exist - so stick with this call (just
569 * ignore any error we get if the share mode doesn't
573 lck
= get_existing_share_mode_lock(talloc_tos(), fsp
->file_id
);
575 /* On close if we're changing the real file time we
576 * must update it in the open file db too. */
577 (void)set_write_time(fsp
->file_id
, fsp
->close_write_time
);
579 /* Close write times overwrite sticky write times
580 so we must replace any sticky write time here. */
581 if (!null_timespec(lck
->data
->changed_write_time
)) {
582 (void)set_sticky_write_time(fsp
->file_id
, fsp
->close_write_time
);
587 ft
.mtime
= fsp
->close_write_time
;
588 /* As this is a close based update, we are not directly changing the
589 file attributes from a client call, but indirectly from a write. */
590 status
= smb_set_file_time(fsp
->conn
, fsp
, fsp
->fsp_name
, &ft
, false);
591 if (!NT_STATUS_IS_OK(status
)) {
592 DEBUG(10,("update_write_time_on_close: smb_set_file_time "
593 "on file %s returned %s\n",
602 static NTSTATUS
ntstatus_keeperror(NTSTATUS s1
, NTSTATUS s2
)
604 if (!NT_STATUS_IS_OK(s1
)) {
610 /****************************************************************************
613 close_type can be NORMAL_CLOSE=0,SHUTDOWN_CLOSE,ERROR_CLOSE.
614 printing and magic scripts are only run on normal close.
615 delete on close is done on normal and shutdown close.
616 ****************************************************************************/
618 static NTSTATUS
close_normal_file(struct smb_request
*req
, files_struct
*fsp
,
619 enum file_close_type close_type
)
621 NTSTATUS status
= NT_STATUS_OK
;
623 connection_struct
*conn
= fsp
->conn
;
624 bool is_durable
= false;
626 if (fsp
->num_aio_requests
!= 0) {
628 if (close_type
!= SHUTDOWN_CLOSE
) {
630 * reply_close and the smb2 close must have
631 * taken care of this. No other callers of
632 * close_file should ever have created async
635 * We need to panic here because if we close()
636 * the fd while we have outstanding async I/O
637 * requests, in the worst case we could end up
638 * writing to the wrong file.
640 DEBUG(0, ("fsp->num_aio_requests=%u\n",
641 fsp
->num_aio_requests
));
642 smb_panic("can not close with outstanding aio "
647 * For shutdown close, just drop the async requests
648 * including a potential close request pending for
649 * this fsp. Drop the close request first, the
650 * destructor for the aio_requests would execute it.
652 TALLOC_FREE(fsp
->deferred_close
);
654 while (fsp
->num_aio_requests
!= 0) {
656 * The destructor of the req will remove
657 * itself from the fsp.
658 * Don't use TALLOC_FREE here, this will overwrite
659 * what the destructor just wrote into
662 talloc_free(fsp
->aio_requests
[0]);
667 * If we're flushing on a close we can get a write
668 * error here, we must remember this.
671 tmp
= close_filestruct(fsp
);
672 status
= ntstatus_keeperror(status
, tmp
);
674 if (NT_STATUS_IS_OK(status
) && fsp
->op
!= NULL
) {
675 is_durable
= fsp
->op
->global
->durable
;
678 if (close_type
!= SHUTDOWN_CLOSE
) {
683 DATA_BLOB new_cookie
= data_blob_null
;
685 tmp
= SMB_VFS_DURABLE_DISCONNECT(fsp
,
686 fsp
->op
->global
->backend_cookie
,
689 if (NT_STATUS_IS_OK(tmp
)) {
694 tv
= req
->request_time
;
696 tv
= timeval_current();
698 now
= timeval_to_nttime(&tv
);
700 data_blob_free(&fsp
->op
->global
->backend_cookie
);
701 fsp
->op
->global
->backend_cookie
= new_cookie
;
703 fsp
->op
->compat
= NULL
;
704 tmp
= smbXsrv_open_close(fsp
->op
, now
);
705 if (!NT_STATUS_IS_OK(tmp
)) {
706 DEBUG(1, ("Failed to update smbXsrv_open "
707 "record when disconnecting durable "
708 "handle for file %s: %s - "
709 "proceeding with normal close\n",
710 fsp_str_dbg(fsp
), nt_errstr(tmp
)));
712 scavenger_schedule_disconnected(fsp
);
714 DEBUG(1, ("Failed to disconnect durable handle for "
715 "file %s: %s - proceeding with normal "
716 "close\n", fsp_str_dbg(fsp
), nt_errstr(tmp
)));
718 if (!NT_STATUS_IS_OK(tmp
)) {
725 * This is the case where we successfully disconnected
726 * a durable handle and closed the underlying file.
727 * In all other cases, we proceed with a genuine close.
729 DEBUG(10, ("%s disconnected durable handle for file %s\n",
730 conn
->session_info
->unix_info
->unix_name
,
736 if (fsp
->op
!= NULL
) {
738 * Make sure the handle is not marked as durable anymore
740 fsp
->op
->global
->durable
= false;
743 if (fsp
->print_file
) {
744 /* FIXME: return spool errors */
745 print_spool_end(fsp
, close_type
);
750 /* If this is an old DOS or FCB open and we have multiple opens on
751 the same handle we only have one share mode. Ensure we only remove
752 the share mode on the last close. */
754 if (fsp
->fh
->ref_count
== 1) {
755 /* Should we return on error here... ? */
756 tmp
= close_remove_share_mode(fsp
, close_type
);
757 status
= ntstatus_keeperror(status
, tmp
);
760 locking_close_file(conn
->sconn
->msg_ctx
, fsp
, close_type
);
763 status
= ntstatus_keeperror(status
, tmp
);
765 /* check for magic scripts */
766 if (close_type
== NORMAL_CLOSE
) {
767 tmp
= check_magic(fsp
);
768 status
= ntstatus_keeperror(status
, tmp
);
772 * Ensure pending modtime is set after close.
775 tmp
= update_write_time_on_close(fsp
);
776 if (NT_STATUS_EQUAL(tmp
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
777 /* Someone renamed the file or a parent directory containing
778 * this file. We can't do anything about this, we don't have
779 * an "update timestamp by fd" call in POSIX. Eat the error. */
784 status
= ntstatus_keeperror(status
, tmp
);
786 DEBUG(2,("%s closed file %s (numopen=%d) %s\n",
787 conn
->session_info
->unix_info
->unix_name
, fsp_str_dbg(fsp
),
788 conn
->num_files_open
- 1,
789 nt_errstr(status
) ));
794 /****************************************************************************
795 Function used by reply_rmdir to delete an entire directory
796 tree recursively. Return True on ok, False on fail.
797 ****************************************************************************/
799 bool recursive_rmdir(TALLOC_CTX
*ctx
,
800 connection_struct
*conn
,
801 struct smb_filename
*smb_dname
)
803 const char *dname
= NULL
;
804 char *talloced
= NULL
;
808 struct smb_Dir
*dir_hnd
;
810 SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname
));
812 dir_hnd
= OpenDir(talloc_tos(), conn
, smb_dname
, NULL
, 0);
816 while((dname
= ReadDirName(dir_hnd
, &offset
, &st
, &talloced
))) {
817 struct smb_filename
*smb_dname_full
= NULL
;
818 char *fullname
= NULL
;
819 bool do_break
= true;
821 if (ISDOT(dname
) || ISDOTDOT(dname
)) {
822 TALLOC_FREE(talloced
);
826 if (!is_visible_file(conn
, smb_dname
->base_name
, dname
, &st
,
828 TALLOC_FREE(talloced
);
832 /* Construct the full name. */
833 fullname
= talloc_asprintf(ctx
,
835 smb_dname
->base_name
,
842 smb_dname_full
= synthetic_smb_fname(talloc_tos(),
847 if (smb_dname_full
== NULL
) {
852 if(SMB_VFS_LSTAT(conn
, smb_dname_full
) != 0) {
856 if(smb_dname_full
->st
.st_ex_mode
& S_IFDIR
) {
857 if(!recursive_rmdir(ctx
, conn
, smb_dname_full
)) {
860 if(SMB_VFS_RMDIR(conn
, smb_dname_full
) != 0) {
863 } else if(SMB_VFS_UNLINK(conn
, smb_dname_full
) != 0) {
867 /* Successful iteration. */
871 TALLOC_FREE(smb_dname_full
);
872 TALLOC_FREE(fullname
);
873 TALLOC_FREE(talloced
);
879 TALLOC_FREE(dir_hnd
);
883 /****************************************************************************
884 The internals of the rmdir code - called elsewhere.
885 ****************************************************************************/
887 static NTSTATUS
rmdir_internals(TALLOC_CTX
*ctx
, files_struct
*fsp
)
889 connection_struct
*conn
= fsp
->conn
;
890 struct smb_filename
*smb_dname
= fsp
->fsp_name
;
893 SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname
));
895 /* Might be a symlink. */
896 if(SMB_VFS_LSTAT(conn
, smb_dname
) != 0) {
897 return map_nt_error_from_unix(errno
);
900 if (S_ISLNK(smb_dname
->st
.st_ex_mode
)) {
901 /* Is what it points to a directory ? */
902 if(SMB_VFS_STAT(conn
, smb_dname
) != 0) {
903 return map_nt_error_from_unix(errno
);
905 if (!(S_ISDIR(smb_dname
->st
.st_ex_mode
))) {
906 return NT_STATUS_NOT_A_DIRECTORY
;
908 ret
= SMB_VFS_UNLINK(conn
, smb_dname
);
910 ret
= SMB_VFS_RMDIR(conn
, smb_dname
);
913 notify_fname(conn
, NOTIFY_ACTION_REMOVED
,
914 FILE_NOTIFY_CHANGE_DIR_NAME
,
915 smb_dname
->base_name
);
919 if(((errno
== ENOTEMPTY
)||(errno
== EEXIST
)) && *lp_veto_files(talloc_tos(), SNUM(conn
))) {
921 * Check to see if the only thing in this directory are
922 * vetoed files/directories. If so then delete them and
923 * retry. If we fail to delete any of them (and we *don't*
924 * do a recursive delete) then fail the rmdir.
927 const char *dname
= NULL
;
928 char *talloced
= NULL
;
930 struct smb_Dir
*dir_hnd
= OpenDir(talloc_tos(), conn
,
934 if(dir_hnd
== NULL
) {
939 while ((dname
= ReadDirName(dir_hnd
, &dirpos
, &st
,
940 &talloced
)) != NULL
) {
941 if((strcmp(dname
, ".") == 0) || (strcmp(dname
, "..")==0)) {
942 TALLOC_FREE(talloced
);
945 if (!is_visible_file(conn
, smb_dname
->base_name
, dname
,
947 TALLOC_FREE(talloced
);
950 if(!IS_VETO_PATH(conn
, dname
)) {
951 TALLOC_FREE(dir_hnd
);
952 TALLOC_FREE(talloced
);
956 TALLOC_FREE(talloced
);
959 /* We only have veto files/directories.
960 * Are we allowed to delete them ? */
962 if(!lp_delete_veto_files(SNUM(conn
))) {
963 TALLOC_FREE(dir_hnd
);
968 /* Do a recursive delete. */
969 RewindDir(dir_hnd
,&dirpos
);
970 while ((dname
= ReadDirName(dir_hnd
, &dirpos
, &st
,
971 &talloced
)) != NULL
) {
972 struct smb_filename
*smb_dname_full
= NULL
;
973 char *fullname
= NULL
;
974 bool do_break
= true;
976 if (ISDOT(dname
) || ISDOTDOT(dname
)) {
977 TALLOC_FREE(talloced
);
980 if (!is_visible_file(conn
, smb_dname
->base_name
, dname
,
982 TALLOC_FREE(talloced
);
986 fullname
= talloc_asprintf(ctx
,
988 smb_dname
->base_name
,
996 smb_dname_full
= synthetic_smb_fname(talloc_tos(),
1001 if (smb_dname_full
== NULL
) {
1006 if(SMB_VFS_LSTAT(conn
, smb_dname_full
) != 0) {
1009 if(smb_dname_full
->st
.st_ex_mode
& S_IFDIR
) {
1010 if(!recursive_rmdir(ctx
, conn
,
1014 if(SMB_VFS_RMDIR(conn
,
1015 smb_dname_full
) != 0) {
1018 } else if(SMB_VFS_UNLINK(conn
, smb_dname_full
) != 0) {
1022 /* Successful iteration. */
1026 TALLOC_FREE(fullname
);
1027 TALLOC_FREE(smb_dname_full
);
1028 TALLOC_FREE(talloced
);
1032 TALLOC_FREE(dir_hnd
);
1033 /* Retry the rmdir */
1034 ret
= SMB_VFS_RMDIR(conn
, smb_dname
);
1040 DEBUG(3,("rmdir_internals: couldn't remove directory %s : "
1041 "%s\n", smb_fname_str_dbg(smb_dname
),
1043 return map_nt_error_from_unix(errno
);
1046 notify_fname(conn
, NOTIFY_ACTION_REMOVED
,
1047 FILE_NOTIFY_CHANGE_DIR_NAME
,
1048 smb_dname
->base_name
);
1050 return NT_STATUS_OK
;
1053 /****************************************************************************
1054 Close a directory opened by an NT SMB call.
1055 ****************************************************************************/
1057 static NTSTATUS
close_directory(struct smb_request
*req
, files_struct
*fsp
,
1058 enum file_close_type close_type
)
1060 struct server_id self
= messaging_server_id(fsp
->conn
->sconn
->msg_ctx
);
1061 struct share_mode_lock
*lck
= NULL
;
1062 bool delete_dir
= False
;
1063 NTSTATUS status
= NT_STATUS_OK
;
1064 NTSTATUS status1
= NT_STATUS_OK
;
1065 const struct security_token
*del_nt_token
= NULL
;
1066 const struct security_unix_token
*del_token
= NULL
;
1067 NTSTATUS notify_status
;
1069 if (fsp
->conn
->sconn
->using_smb2
) {
1070 notify_status
= STATUS_NOTIFY_CLEANUP
;
1072 notify_status
= NT_STATUS_OK
;
1076 * NT can set delete_on_close of the last open
1077 * reference to a directory also.
1080 lck
= get_existing_share_mode_lock(talloc_tos(), fsp
->file_id
);
1082 DEBUG(0, ("close_directory: Could not get share mode lock for "
1083 "%s\n", fsp_str_dbg(fsp
)));
1084 return NT_STATUS_INVALID_PARAMETER
;
1087 if (fsp
->initial_delete_on_close
) {
1088 bool became_user
= False
;
1090 /* Initial delete on close was set - for
1091 * directories we don't care if anyone else
1092 * wrote a real delete on close. */
1094 if (get_current_vuid(fsp
->conn
) != fsp
->vuid
) {
1095 become_user(fsp
->conn
, fsp
->vuid
);
1098 send_stat_cache_delete_message(fsp
->conn
->sconn
->msg_ctx
,
1099 fsp
->fsp_name
->base_name
);
1100 set_delete_on_close_lck(fsp
, lck
,
1101 get_current_nttok(fsp
->conn
),
1102 get_current_utok(fsp
->conn
));
1103 fsp
->delete_on_close
= true;
1109 delete_dir
= get_delete_on_close_token(lck
, fsp
->name_hash
,
1110 &del_nt_token
, &del_token
);
1114 /* See if others still have the dir open. If this is the
1115 * case, then don't delete. If all opens are POSIX delete now. */
1116 for (i
=0; i
<lck
->data
->num_share_modes
; i
++) {
1117 struct share_mode_entry
*e
= &lck
->data
->share_modes
[i
];
1118 if (is_valid_share_mode_entry(e
) &&
1119 e
->name_hash
== fsp
->name_hash
) {
1120 if ((fsp
->posix_flags
& FSP_POSIX_FLAGS_OPEN
) &&
1121 (e
->flags
& SHARE_MODE_FLAG_POSIX_OPEN
))
1125 if (serverid_equal(&self
, &e
->pid
) &&
1126 (e
->share_file_id
== fsp
->fh
->gen_id
)) {
1129 if (share_mode_stale_pid(lck
->data
, i
)) {
1138 if ((close_type
== NORMAL_CLOSE
|| close_type
== SHUTDOWN_CLOSE
) &&
1141 /* Become the user who requested the delete. */
1143 if (!push_sec_ctx()) {
1144 smb_panic("close_directory: failed to push sec_ctx.\n");
1147 set_sec_ctx(del_token
->uid
,
1153 if (!del_share_mode(lck
, fsp
)) {
1154 DEBUG(0, ("close_directory: Could not delete share entry for "
1155 "%s\n", fsp_str_dbg(fsp
)));
1160 if ((fsp
->conn
->fs_capabilities
& FILE_NAMED_STREAMS
)
1161 && !is_ntfs_stream_smb_fname(fsp
->fsp_name
)) {
1163 status
= delete_all_streams(fsp
->conn
, fsp
->fsp_name
);
1164 if (!NT_STATUS_IS_OK(status
)) {
1165 DEBUG(5, ("delete_all_streams failed: %s\n",
1166 nt_errstr(status
)));
1171 status
= rmdir_internals(talloc_tos(), fsp
);
1173 DEBUG(5,("close_directory: %s. Delete on close was set - "
1174 "deleting directory returned %s.\n",
1175 fsp_str_dbg(fsp
), nt_errstr(status
)));
1177 /* unbecome user. */
1181 * Ensure we remove any change notify requests that would
1182 * now fail as the directory has been deleted.
1185 if (NT_STATUS_IS_OK(status
)) {
1186 notify_status
= NT_STATUS_DELETE_PENDING
;
1189 if (!del_share_mode(lck
, fsp
)) {
1190 DEBUG(0, ("close_directory: Could not delete share entry for "
1191 "%s\n", fsp_str_dbg(fsp
)));
1197 remove_pending_change_notify_requests_by_fid(fsp
, notify_status
);
1199 status1
= fd_close(fsp
);
1201 if (!NT_STATUS_IS_OK(status1
)) {
1202 DEBUG(0, ("Could not close dir! fname=%s, fd=%d, err=%d=%s\n",
1203 fsp_str_dbg(fsp
), fsp
->fh
->fd
, errno
,
1208 * Do the code common to files and directories.
1210 close_filestruct(fsp
);
1211 file_free(req
, fsp
);
1213 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(status1
)) {
1219 /****************************************************************************
1220 Close a files_struct.
1221 ****************************************************************************/
1223 NTSTATUS
close_file(struct smb_request
*req
, files_struct
*fsp
,
1224 enum file_close_type close_type
)
1227 struct files_struct
*base_fsp
= fsp
->base_fsp
;
1229 if(fsp
->is_directory
) {
1230 status
= close_directory(req
, fsp
, close_type
);
1231 } else if (fsp
->fake_file_handle
!= NULL
) {
1232 status
= close_fake_file(req
, fsp
);
1234 status
= close_normal_file(req
, fsp
, close_type
);
1237 if ((base_fsp
!= NULL
) && (close_type
!= SHUTDOWN_CLOSE
)) {
1240 * fsp was a stream, the base fsp can't be a stream as well
1242 * For SHUTDOWN_CLOSE this is not possible here, because
1243 * SHUTDOWN_CLOSE only happens from files.c which walks the
1244 * complete list of files. If we mess with more than one fsp
1245 * those loops will become confused.
1248 SMB_ASSERT(base_fsp
->base_fsp
== NULL
);
1249 close_file(req
, base_fsp
, close_type
);
1255 /****************************************************************************
1256 Deal with an (authorized) message to close a file given the share mode
1258 ****************************************************************************/
1260 void msg_close_file(struct messaging_context
*msg_ctx
,
1263 struct server_id server_id
,
1266 files_struct
*fsp
= NULL
;
1267 struct share_mode_entry e
;
1268 struct smbd_server_connection
*sconn
=
1269 talloc_get_type_abort(private_data
,
1270 struct smbd_server_connection
);
1272 message_to_share_mode_entry(&e
, (char *)data
->data
);
1275 char *sm_str
= share_mode_str(NULL
, 0, &e
);
1277 smb_panic("talloc failed");
1279 DEBUG(10,("msg_close_file: got request to close share mode "
1280 "entry %s\n", sm_str
));
1281 TALLOC_FREE(sm_str
);
1284 fsp
= file_find_dif(sconn
, e
.id
, e
.share_file_id
);
1286 DEBUG(10,("msg_close_file: failed to find file.\n"));
1289 close_file(NULL
, fsp
, NORMAL_CLOSE
);