py/security: Add test for dom_sid.split.
[Samba/fernandojvsilva.git] / source3 / smbd / close.c
blob05c3c709a1e7a31611d48ee9438dc843c08e1dad
1 /*
2 Unix SMB/CIFS implementation.
3 file closing
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/>.
22 #include "includes.h"
24 extern struct current_user current_user;
26 /****************************************************************************
27 Run a file if it is a magic script.
28 ****************************************************************************/
30 static NTSTATUS check_magic(struct files_struct *fsp)
32 int ret;
33 const char *magic_output = NULL;
34 SMB_STRUCT_STAT st;
35 int tmp_fd, outfd;
36 TALLOC_CTX *ctx = NULL;
37 const char *p;
38 struct connection_struct *conn = fsp->conn;
39 char *fname = NULL;
40 NTSTATUS status;
42 if (!*lp_magicscript(SNUM(conn))) {
43 return NT_STATUS_OK;
46 DEBUG(5,("checking magic for %s\n", fsp_str_dbg(fsp)));
48 ctx = talloc_stackframe();
50 fname = fsp->fsp_name->base_name;
52 if (!(p = strrchr_m(fname,'/'))) {
53 p = fname;
54 } else {
55 p++;
58 if (!strequal(lp_magicscript(SNUM(conn)),p)) {
59 status = NT_STATUS_OK;
60 goto out;
63 if (*lp_magicoutput(SNUM(conn))) {
64 magic_output = lp_magicoutput(SNUM(conn));
65 } else {
66 magic_output = talloc_asprintf(ctx,
67 "%s.out",
68 fname);
70 if (!magic_output) {
71 status = NT_STATUS_NO_MEMORY;
72 goto out;
75 /* Ensure we don't depend on user's PATH. */
76 p = talloc_asprintf(ctx, "./%s", fname);
77 if (!p) {
78 status = NT_STATUS_NO_MEMORY;
79 goto out;
82 if (chmod(fname, 0755) == -1) {
83 status = map_nt_error_from_unix(errno);
84 goto out;
86 ret = smbrun(p,&tmp_fd);
87 DEBUG(3,("Invoking magic command %s gave %d\n",
88 p,ret));
90 unlink(fname);
91 if (ret != 0 || tmp_fd == -1) {
92 if (tmp_fd != -1) {
93 close(tmp_fd);
95 status = NT_STATUS_UNSUCCESSFUL;
96 goto out;
98 outfd = open(magic_output, O_CREAT|O_EXCL|O_RDWR, 0600);
99 if (outfd == -1) {
100 int err = errno;
101 close(tmp_fd);
102 status = map_nt_error_from_unix(err);
103 goto out;
106 if (sys_fstat(tmp_fd, &st, false) == -1) {
107 int err = errno;
108 close(tmp_fd);
109 close(outfd);
110 status = map_nt_error_from_unix(err);
111 goto out;
114 if (transfer_file(tmp_fd,outfd,(SMB_OFF_T)st.st_ex_size) == (SMB_OFF_T)-1) {
115 int err = errno;
116 close(tmp_fd);
117 close(outfd);
118 status = map_nt_error_from_unix(err);
119 goto out;
121 close(tmp_fd);
122 if (close(outfd) == -1) {
123 status = map_nt_error_from_unix(errno);
124 goto out;
127 status = NT_STATUS_OK;
129 out:
130 TALLOC_FREE(ctx);
131 return status;
134 /****************************************************************************
135 Common code to close a file or a directory.
136 ****************************************************************************/
138 static NTSTATUS close_filestruct(files_struct *fsp)
140 NTSTATUS status = NT_STATUS_OK;
142 if (fsp->fh->fd != -1) {
143 if(flush_write_cache(fsp, CLOSE_FLUSH) == -1) {
144 status = map_nt_error_from_unix(errno);
146 delete_write_cache(fsp);
149 return status;
152 /****************************************************************************
153 If any deferred opens are waiting on this close, notify them.
154 ****************************************************************************/
156 static void notify_deferred_opens(struct share_mode_lock *lck)
158 int i;
160 if (!should_notify_deferred_opens()) {
161 return;
164 for (i=0; i<lck->num_share_modes; i++) {
165 struct share_mode_entry *e = &lck->share_modes[i];
167 if (!is_deferred_open_entry(e)) {
168 continue;
171 if (procid_is_me(&e->pid)) {
173 * We need to notify ourself to retry the open. Do
174 * this by finding the queued SMB record, moving it to
175 * the head of the queue and changing the wait time to
176 * zero.
178 schedule_deferred_open_smb_message(e->op_mid);
179 } else {
180 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
182 share_mode_entry_to_message(msg, e);
184 messaging_send_buf(smbd_messaging_context(),
185 e->pid, MSG_SMB_OPEN_RETRY,
186 (uint8 *)msg,
187 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
192 /****************************************************************************
193 Delete all streams
194 ****************************************************************************/
196 NTSTATUS delete_all_streams(connection_struct *conn, const char *fname)
198 struct stream_struct *stream_info;
199 int i;
200 unsigned int num_streams;
201 TALLOC_CTX *frame = talloc_stackframe();
202 NTSTATUS status;
204 status = SMB_VFS_STREAMINFO(conn, NULL, fname, talloc_tos(),
205 &num_streams, &stream_info);
207 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
208 DEBUG(10, ("no streams around\n"));
209 TALLOC_FREE(frame);
210 return NT_STATUS_OK;
213 if (!NT_STATUS_IS_OK(status)) {
214 DEBUG(10, ("SMB_VFS_STREAMINFO failed: %s\n",
215 nt_errstr(status)));
216 goto fail;
219 DEBUG(10, ("delete_all_streams found %d streams\n",
220 num_streams));
222 if (num_streams == 0) {
223 TALLOC_FREE(frame);
224 return NT_STATUS_OK;
227 for (i=0; i<num_streams; i++) {
228 int res;
229 struct smb_filename *smb_fname_stream = NULL;
231 if (strequal(stream_info[i].name, "::$DATA")) {
232 continue;
235 status = create_synthetic_smb_fname(talloc_tos(), fname,
236 stream_info[i].name, NULL,
237 &smb_fname_stream);
239 if (!NT_STATUS_IS_OK(status)) {
240 DEBUG(0, ("talloc_aprintf failed\n"));
241 goto fail;
244 res = SMB_VFS_UNLINK(conn, smb_fname_stream);
246 if (res == -1) {
247 status = map_nt_error_from_unix(errno);
248 DEBUG(10, ("Could not delete stream %s: %s\n",
249 smb_fname_str_dbg(smb_fname_stream),
250 strerror(errno)));
251 TALLOC_FREE(smb_fname_stream);
252 break;
254 TALLOC_FREE(smb_fname_stream);
257 fail:
258 TALLOC_FREE(frame);
259 return status;
262 /****************************************************************************
263 Deal with removing a share mode on last close.
264 ****************************************************************************/
266 static NTSTATUS close_remove_share_mode(files_struct *fsp,
267 enum file_close_type close_type)
269 connection_struct *conn = fsp->conn;
270 bool delete_file = false;
271 bool changed_user = false;
272 struct share_mode_lock *lck = NULL;
273 NTSTATUS status = NT_STATUS_OK;
274 NTSTATUS tmp_status;
275 struct file_id id;
277 /* Ensure any pending write time updates are done. */
278 if (fsp->update_write_time_event) {
279 update_write_time_handler(smbd_event_context(),
280 fsp->update_write_time_event,
281 timeval_current(),
282 (void *)fsp);
286 * Lock the share entries, and determine if we should delete
287 * on close. If so delete whilst the lock is still in effect.
288 * This prevents race conditions with the file being created. JRA.
291 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
292 NULL);
294 if (lck == NULL) {
295 DEBUG(0, ("close_remove_share_mode: Could not get share mode "
296 "lock for file %s\n", fsp_str_dbg(fsp)));
297 status = NT_STATUS_INVALID_PARAMETER;
298 goto done;
301 if (fsp->write_time_forced) {
302 DEBUG(10,("close_remove_share_mode: write time forced "
303 "for file %s\n",
304 fsp_str_dbg(fsp)));
305 set_close_write_time(fsp, lck->changed_write_time);
306 } else if (fsp->update_write_time_on_close) {
307 /* Someone had a pending write. */
308 if (null_timespec(fsp->close_write_time)) {
309 DEBUG(10,("close_remove_share_mode: update to current time "
310 "for file %s\n",
311 fsp_str_dbg(fsp)));
312 /* Update to current time due to "normal" write. */
313 set_close_write_time(fsp, timespec_current());
314 } else {
315 DEBUG(10,("close_remove_share_mode: write time pending "
316 "for file %s\n",
317 fsp_str_dbg(fsp)));
318 /* Update to time set on close call. */
319 set_close_write_time(fsp, fsp->close_write_time);
323 if (!del_share_mode(lck, fsp)) {
324 DEBUG(0, ("close_remove_share_mode: Could not delete share "
325 "entry for file %s\n",
326 fsp_str_dbg(fsp)));
329 if (fsp->initial_delete_on_close && (lck->delete_token == NULL)) {
330 bool became_user = False;
332 /* Initial delete on close was set and no one else
333 * wrote a real delete on close. */
335 if (current_user.vuid != fsp->vuid) {
336 become_user(conn, fsp->vuid);
337 became_user = True;
339 set_delete_on_close_lck(lck, True, &current_user.ut);
340 if (became_user) {
341 unbecome_user();
345 delete_file = lck->delete_on_close;
347 if (delete_file) {
348 int i;
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)) {
355 continue;
357 delete_file = False;
358 break;
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)
373 || !delete_file
374 || (lck->delete_token == NULL)) {
375 TALLOC_FREE(lck);
376 return NT_STATUS_OK;
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, &current_user.ut)) {
392 /* Become the user who requested the delete. */
394 DEBUG(5,("close_remove_share_mode: file %s. "
395 "Change user to uid %u\n",
396 fsp_str_dbg(fsp),
397 (unsigned int)lck->delete_token->uid));
399 if (!push_sec_ctx()) {
400 smb_panic("close_remove_share_mode: file %s. failed to push "
401 "sec_ctx.\n");
404 set_sec_ctx(lck->delete_token->uid,
405 lck->delete_token->gid,
406 lck->delete_token->ngroups,
407 lck->delete_token->groups,
408 NULL);
410 changed_user = true;
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
424 goto done;
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",
432 fsp_str_dbg(fsp)));
433 DEBUG(5,("close_remove_share_mode: file %s. stored file_id %s, "
434 "stat file_id %s\n",
435 fsp_str_dbg(fsp),
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
441 goto done;
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",
451 nt_errstr(status)));
452 goto done;
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
463 * zero.
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 set_delete_on_close_lck(lck, False, NULL);
486 done:
488 if (changed_user) {
489 /* unbecome user. */
490 pop_sec_ctx();
493 TALLOC_FREE(lck);
494 return status;
497 void set_close_write_time(struct files_struct *fsp, struct timespec ts)
499 DEBUG(6,("close_write_time: %s" , time_to_asc(convert_timespec_to_time_t(ts))));
501 if (null_timespec(ts)) {
502 return;
504 fsp->write_time_forced = false;
505 fsp->update_write_time_on_close = true;
506 fsp->close_write_time = ts;
509 static NTSTATUS update_write_time_on_close(struct files_struct *fsp)
511 struct smb_file_time ft;
512 NTSTATUS status;
513 struct share_mode_lock *lck = NULL;
515 ZERO_STRUCT(ft);
517 if (!fsp->update_write_time_on_close) {
518 return NT_STATUS_OK;
521 if (null_timespec(fsp->close_write_time)) {
522 fsp->close_write_time = timespec_current();
525 /* Ensure we have a valid stat struct for the source. */
526 status = vfs_stat_fsp(fsp);
527 if (!NT_STATUS_IS_OK(status)) {
528 return status;
531 if (!VALID_STAT(fsp->fsp_name->st)) {
532 /* if it doesn't seem to be a real file */
533 return NT_STATUS_OK;
536 /* On close if we're changing the real file time we
537 * must update it in the open file db too. */
538 (void)set_write_time(fsp->file_id, fsp->close_write_time);
540 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL, NULL);
541 if (lck) {
542 /* Close write times overwrite sticky write times
543 so we must replace any sticky write time here. */
544 if (!null_timespec(lck->changed_write_time)) {
545 (void)set_sticky_write_time(fsp->file_id, fsp->close_write_time);
547 TALLOC_FREE(lck);
550 ft.mtime = fsp->close_write_time;
551 status = smb_set_file_time(fsp->conn, fsp, fsp->fsp_name, &ft, false);
552 if (!NT_STATUS_IS_OK(status)) {
553 return status;
556 return status;
559 static NTSTATUS ntstatus_keeperror(NTSTATUS s1, NTSTATUS s2)
561 if (!NT_STATUS_IS_OK(s1)) {
562 return s1;
564 return s2;
567 /****************************************************************************
568 Close a file.
570 close_type can be NORMAL_CLOSE=0,SHUTDOWN_CLOSE,ERROR_CLOSE.
571 printing and magic scripts are only run on normal close.
572 delete on close is done on normal and shutdown close.
573 ****************************************************************************/
575 static NTSTATUS close_normal_file(struct smb_request *req, files_struct *fsp,
576 enum file_close_type close_type)
578 NTSTATUS status = NT_STATUS_OK;
579 NTSTATUS tmp;
580 connection_struct *conn = fsp->conn;
582 if (fsp->aio_write_behind) {
584 * If we're finishing write behind on a close we can get a write
585 * error here, we must remember this.
587 int ret = wait_for_aio_completion(fsp);
588 if (ret) {
589 status = ntstatus_keeperror(
590 status, map_nt_error_from_unix(ret));
592 } else {
593 cancel_aio_by_fsp(fsp);
597 * If we're flushing on a close we can get a write
598 * error here, we must remember this.
601 tmp = close_filestruct(fsp);
602 status = ntstatus_keeperror(status, tmp);
604 if (fsp->print_file) {
605 print_fsp_end(fsp, close_type);
606 file_free(req, fsp);
607 return NT_STATUS_OK;
610 /* Remove the oplock before potentially deleting the file. */
611 if(fsp->oplock_type) {
612 release_file_oplock(fsp);
615 /* If this is an old DOS or FCB open and we have multiple opens on
616 the same handle we only have one share mode. Ensure we only remove
617 the share mode on the last close. */
619 if (fsp->fh->ref_count == 1) {
620 /* Should we return on error here... ? */
621 tmp = close_remove_share_mode(fsp, close_type);
622 status = ntstatus_keeperror(status, tmp);
625 locking_close_file(smbd_messaging_context(), fsp);
627 tmp = fd_close(fsp);
628 status = ntstatus_keeperror(status, tmp);
630 /* check for magic scripts */
631 if (close_type == NORMAL_CLOSE) {
632 tmp = check_magic(fsp);
633 status = ntstatus_keeperror(status, tmp);
637 * Ensure pending modtime is set after close.
640 tmp = update_write_time_on_close(fsp);
641 if (NT_STATUS_EQUAL(tmp, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
642 /* Someone renamed the file or a parent directory containing
643 * this file. We can't do anything about this, we don't have
644 * an "update timestamp by fd" call in POSIX. Eat the error. */
646 tmp = NT_STATUS_OK;
649 status = ntstatus_keeperror(status, tmp);
651 DEBUG(2,("%s closed file %s (numopen=%d) %s\n",
652 conn->server_info->unix_name, fsp_str_dbg(fsp),
653 conn->num_files_open - 1,
654 nt_errstr(status) ));
656 file_free(req, fsp);
657 return status;
659 /****************************************************************************
660 Static function used by reply_rmdir to delete an entire directory
661 tree recursively. Return True on ok, False on fail.
662 ****************************************************************************/
664 static bool recursive_rmdir(TALLOC_CTX *ctx,
665 connection_struct *conn,
666 struct smb_filename *smb_dname)
668 const char *dname = NULL;
669 char *talloced = NULL;
670 bool ret = True;
671 long offset = 0;
672 SMB_STRUCT_STAT st;
673 struct smb_Dir *dir_hnd;
675 SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname));
677 dir_hnd = OpenDir(talloc_tos(), conn, smb_dname->base_name, NULL, 0);
678 if(dir_hnd == NULL)
679 return False;
681 while((dname = ReadDirName(dir_hnd, &offset, &st, &talloced))) {
682 struct smb_filename *smb_dname_full = NULL;
683 char *fullname = NULL;
684 bool do_break = true;
685 NTSTATUS status;
687 if (ISDOT(dname) || ISDOTDOT(dname)) {
688 TALLOC_FREE(talloced);
689 continue;
692 if (!is_visible_file(conn, smb_dname->base_name, dname, &st,
693 false)) {
694 TALLOC_FREE(talloced);
695 continue;
698 /* Construct the full name. */
699 fullname = talloc_asprintf(ctx,
700 "%s/%s",
701 smb_dname->base_name,
702 dname);
703 if (!fullname) {
704 errno = ENOMEM;
705 goto err_break;
708 status = create_synthetic_smb_fname(talloc_tos(), fullname,
709 NULL, NULL,
710 &smb_dname_full);
711 if (!NT_STATUS_IS_OK(status)) {
712 goto err_break;
715 if(SMB_VFS_LSTAT(conn, smb_dname_full) != 0) {
716 goto err_break;
719 if(smb_dname_full->st.st_ex_mode & S_IFDIR) {
720 if(!recursive_rmdir(ctx, conn, smb_dname_full)) {
721 goto err_break;
723 if(SMB_VFS_RMDIR(conn,
724 smb_dname_full->base_name) != 0) {
725 goto err_break;
727 } else if(SMB_VFS_UNLINK(conn, smb_dname_full) != 0) {
728 goto err_break;
731 /* Successful iteration. */
732 do_break = false;
734 err_break:
735 TALLOC_FREE(smb_dname_full);
736 TALLOC_FREE(fullname);
737 TALLOC_FREE(talloced);
738 if (do_break) {
739 ret = false;
740 break;
743 TALLOC_FREE(dir_hnd);
744 return ret;
747 /****************************************************************************
748 The internals of the rmdir code - called elsewhere.
749 ****************************************************************************/
751 static NTSTATUS rmdir_internals(TALLOC_CTX *ctx, files_struct *fsp)
753 connection_struct *conn = fsp->conn;
754 struct smb_filename *smb_dname = fsp->fsp_name;
755 int ret;
757 SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname));
759 /* Might be a symlink. */
760 if(SMB_VFS_LSTAT(conn, smb_dname) != 0) {
761 return map_nt_error_from_unix(errno);
764 if (S_ISLNK(smb_dname->st.st_ex_mode)) {
765 /* Is what it points to a directory ? */
766 if(SMB_VFS_STAT(conn, smb_dname) != 0) {
767 return map_nt_error_from_unix(errno);
769 if (!(S_ISDIR(smb_dname->st.st_ex_mode))) {
770 return NT_STATUS_NOT_A_DIRECTORY;
772 ret = SMB_VFS_UNLINK(conn, smb_dname);
773 } else {
774 ret = SMB_VFS_RMDIR(conn, smb_dname->base_name);
776 if (ret == 0) {
777 notify_fname(conn, NOTIFY_ACTION_REMOVED,
778 FILE_NOTIFY_CHANGE_DIR_NAME,
779 smb_dname->base_name);
780 return NT_STATUS_OK;
783 if(((errno == ENOTEMPTY)||(errno == EEXIST)) && lp_veto_files(SNUM(conn))) {
785 * Check to see if the only thing in this directory are
786 * vetoed files/directories. If so then delete them and
787 * retry. If we fail to delete any of them (and we *don't*
788 * do a recursive delete) then fail the rmdir.
790 SMB_STRUCT_STAT st;
791 const char *dname = NULL;
792 char *talloced = NULL;
793 long dirpos = 0;
794 struct smb_Dir *dir_hnd = OpenDir(talloc_tos(), conn,
795 smb_dname->base_name, NULL,
798 if(dir_hnd == NULL) {
799 errno = ENOTEMPTY;
800 goto err;
803 while ((dname = ReadDirName(dir_hnd, &dirpos, &st,
804 &talloced)) != NULL) {
805 if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0)) {
806 TALLOC_FREE(talloced);
807 continue;
809 if (!is_visible_file(conn, smb_dname->base_name, dname,
810 &st, false)) {
811 TALLOC_FREE(talloced);
812 continue;
814 if(!IS_VETO_PATH(conn, dname)) {
815 TALLOC_FREE(dir_hnd);
816 TALLOC_FREE(talloced);
817 errno = ENOTEMPTY;
818 goto err;
820 TALLOC_FREE(talloced);
823 /* We only have veto files/directories.
824 * Are we allowed to delete them ? */
826 if(!lp_recursive_veto_delete(SNUM(conn))) {
827 TALLOC_FREE(dir_hnd);
828 errno = ENOTEMPTY;
829 goto err;
832 /* Do a recursive delete. */
833 RewindDir(dir_hnd,&dirpos);
834 while ((dname = ReadDirName(dir_hnd, &dirpos, &st,
835 &talloced)) != NULL) {
836 struct smb_filename *smb_dname_full = NULL;
837 char *fullname = NULL;
838 bool do_break = true;
839 NTSTATUS status;
841 if (ISDOT(dname) || ISDOTDOT(dname)) {
842 TALLOC_FREE(talloced);
843 continue;
845 if (!is_visible_file(conn, smb_dname->base_name, dname,
846 &st, false)) {
847 TALLOC_FREE(talloced);
848 continue;
851 fullname = talloc_asprintf(ctx,
852 "%s/%s",
853 smb_dname->base_name,
854 dname);
856 if(!fullname) {
857 errno = ENOMEM;
858 goto err_break;
861 status = create_synthetic_smb_fname(talloc_tos(),
862 fullname, NULL,
863 NULL,
864 &smb_dname_full);
865 if (!NT_STATUS_IS_OK(status)) {
866 errno = map_errno_from_nt_status(status);
867 goto err_break;
870 if(SMB_VFS_LSTAT(conn, smb_dname_full) != 0) {
871 goto err_break;
873 if(smb_dname_full->st.st_ex_mode & S_IFDIR) {
874 if(!recursive_rmdir(ctx, conn,
875 smb_dname_full)) {
876 goto err_break;
878 if(SMB_VFS_RMDIR(conn,
879 smb_dname_full->base_name) != 0) {
880 goto err_break;
882 } else if(SMB_VFS_UNLINK(conn, smb_dname_full) != 0) {
883 goto err_break;
886 /* Successful iteration. */
887 do_break = false;
889 err_break:
890 TALLOC_FREE(fullname);
891 TALLOC_FREE(smb_dname_full);
892 TALLOC_FREE(talloced);
893 if (do_break)
894 break;
896 TALLOC_FREE(dir_hnd);
897 /* Retry the rmdir */
898 ret = SMB_VFS_RMDIR(conn, smb_dname->base_name);
901 err:
903 if (ret != 0) {
904 DEBUG(3,("rmdir_internals: couldn't remove directory %s : "
905 "%s\n", smb_fname_str_dbg(smb_dname),
906 strerror(errno)));
907 return map_nt_error_from_unix(errno);
910 notify_fname(conn, NOTIFY_ACTION_REMOVED,
911 FILE_NOTIFY_CHANGE_DIR_NAME,
912 smb_dname->base_name);
914 return NT_STATUS_OK;
917 /****************************************************************************
918 Close a directory opened by an NT SMB call.
919 ****************************************************************************/
921 static NTSTATUS close_directory(struct smb_request *req, files_struct *fsp,
922 enum file_close_type close_type)
924 struct share_mode_lock *lck = NULL;
925 bool delete_dir = False;
926 NTSTATUS status = NT_STATUS_OK;
929 * NT can set delete_on_close of the last open
930 * reference to a directory also.
933 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
934 NULL);
936 if (lck == NULL) {
937 DEBUG(0, ("close_directory: Could not get share mode lock for "
938 "%s\n", fsp_str_dbg(fsp)));
939 status = NT_STATUS_INVALID_PARAMETER;
940 goto out;
943 if (!del_share_mode(lck, fsp)) {
944 DEBUG(0, ("close_directory: Could not delete share entry for "
945 "%s\n", fsp_str_dbg(fsp)));
948 if (fsp->initial_delete_on_close) {
949 bool became_user = False;
951 /* Initial delete on close was set - for
952 * directories we don't care if anyone else
953 * wrote a real delete on close. */
955 if (current_user.vuid != fsp->vuid) {
956 become_user(fsp->conn, fsp->vuid);
957 became_user = True;
959 send_stat_cache_delete_message(fsp->fsp_name->base_name);
960 set_delete_on_close_lck(lck, True, &current_user.ut);
961 if (became_user) {
962 unbecome_user();
966 delete_dir = lck->delete_on_close;
968 if (delete_dir) {
969 int i;
970 /* See if others still have the dir open. If this is the
971 * case, then don't delete. If all opens are POSIX delete now. */
972 for (i=0; i<lck->num_share_modes; i++) {
973 struct share_mode_entry *e = &lck->share_modes[i];
974 if (is_valid_share_mode_entry(e)) {
975 if (fsp->posix_open && (e->flags & SHARE_MODE_FLAG_POSIX_OPEN)) {
976 continue;
978 delete_dir = False;
979 break;
984 if ((close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE) &&
985 delete_dir &&
986 lck->delete_token) {
988 /* Become the user who requested the delete. */
990 if (!push_sec_ctx()) {
991 smb_panic("close_directory: failed to push sec_ctx.\n");
994 set_sec_ctx(lck->delete_token->uid,
995 lck->delete_token->gid,
996 lck->delete_token->ngroups,
997 lck->delete_token->groups,
998 NULL);
1000 TALLOC_FREE(lck);
1002 status = rmdir_internals(talloc_tos(), fsp);
1004 DEBUG(5,("close_directory: %s. Delete on close was set - "
1005 "deleting directory returned %s.\n",
1006 fsp_str_dbg(fsp), nt_errstr(status)));
1008 /* unbecome user. */
1009 pop_sec_ctx();
1012 * Ensure we remove any change notify requests that would
1013 * now fail as the directory has been deleted.
1016 if(NT_STATUS_IS_OK(status)) {
1017 remove_pending_change_notify_requests_by_fid(fsp, NT_STATUS_DELETE_PENDING);
1019 } else {
1020 TALLOC_FREE(lck);
1021 remove_pending_change_notify_requests_by_fid(
1022 fsp, NT_STATUS_OK);
1025 status = fd_close(fsp);
1027 if (!NT_STATUS_IS_OK(status)) {
1028 DEBUG(0, ("Could not close dir! fname=%s, fd=%d, err=%d=%s\n",
1029 fsp_str_dbg(fsp), fsp->fh->fd, errno,
1030 strerror(errno)));
1033 if (fsp->dptr) {
1034 dptr_CloseDir(fsp->dptr);
1038 * Do the code common to files and directories.
1040 close_filestruct(fsp);
1041 file_free(req, fsp);
1043 out:
1044 TALLOC_FREE(lck);
1045 return status;
1048 /****************************************************************************
1049 Close a files_struct.
1050 ****************************************************************************/
1052 NTSTATUS close_file(struct smb_request *req, files_struct *fsp,
1053 enum file_close_type close_type)
1055 NTSTATUS status;
1056 struct files_struct *base_fsp = fsp->base_fsp;
1058 if(fsp->is_directory) {
1059 status = close_directory(req, fsp, close_type);
1060 } else if (fsp->fake_file_handle != NULL) {
1061 status = close_fake_file(req, fsp);
1062 } else {
1063 status = close_normal_file(req, fsp, close_type);
1066 if ((base_fsp != NULL) && (close_type != SHUTDOWN_CLOSE)) {
1069 * fsp was a stream, the base fsp can't be a stream as well
1071 * For SHUTDOWN_CLOSE this is not possible here, because
1072 * SHUTDOWN_CLOSE only happens from files.c which walks the
1073 * complete list of files. If we mess with more than one fsp
1074 * those loops will become confused.
1077 SMB_ASSERT(base_fsp->base_fsp == NULL);
1078 close_file(req, base_fsp, close_type);
1081 return status;
1084 /****************************************************************************
1085 Deal with an (authorized) message to close a file given the share mode
1086 entry.
1087 ****************************************************************************/
1089 void msg_close_file(struct messaging_context *msg_ctx,
1090 void *private_data,
1091 uint32_t msg_type,
1092 struct server_id server_id,
1093 DATA_BLOB *data)
1095 files_struct *fsp = NULL;
1096 struct share_mode_entry e;
1098 message_to_share_mode_entry(&e, (char *)data->data);
1100 if(DEBUGLVL(10)) {
1101 char *sm_str = share_mode_str(NULL, 0, &e);
1102 if (!sm_str) {
1103 smb_panic("talloc failed");
1105 DEBUG(10,("msg_close_file: got request to close share mode "
1106 "entry %s\n", sm_str));
1107 TALLOC_FREE(sm_str);
1110 fsp = file_find_dif(e.id, e.share_file_id);
1111 if (!fsp) {
1112 DEBUG(10,("msg_close_file: failed to find file.\n"));
1113 return;
1115 close_file(NULL, fsp, NORMAL_CLOSE);