Revert "s3/service: convert lp_force_group() to const"
[Samba.git] / source3 / smbd / close.c
blob8581b04e8aaf43710e959e530dc41fcf9004d073
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"
23 #include "system/filesys.h"
24 #include "lib/util/server_id.h"
25 #include "printing.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"
31 #include "auth.h"
32 #include "messages.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)
41 int ret;
42 const char *magic_output = NULL;
43 SMB_STRUCT_STAT st;
44 int tmp_fd, outfd;
45 TALLOC_CTX *ctx = NULL;
46 const char *p;
47 struct connection_struct *conn = fsp->conn;
48 char *fname = NULL;
49 NTSTATUS status;
51 if (!*lp_magic_script(talloc_tos(), SNUM(conn))) {
52 return NT_STATUS_OK;
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,'/'))) {
62 p = fname;
63 } else {
64 p++;
67 if (!strequal(lp_magic_script(talloc_tos(), SNUM(conn)),p)) {
68 status = NT_STATUS_OK;
69 goto out;
72 if (*lp_magic_output(talloc_tos(), SNUM(conn))) {
73 magic_output = lp_magic_output(talloc_tos(), SNUM(conn));
74 } else {
75 magic_output = talloc_asprintf(ctx,
76 "%s.out",
77 fname);
79 if (!magic_output) {
80 status = NT_STATUS_NO_MEMORY;
81 goto out;
84 /* Ensure we don't depend on user's PATH. */
85 p = talloc_asprintf(ctx, "./%s", fname);
86 if (!p) {
87 status = NT_STATUS_NO_MEMORY;
88 goto out;
91 if (chmod(fname, 0755) == -1) {
92 status = map_nt_error_from_unix(errno);
93 goto out;
95 ret = smbrun(p, &tmp_fd, NULL);
96 DEBUG(3,("Invoking magic command %s gave %d\n",
97 p,ret));
99 unlink(fname);
100 if (ret != 0 || tmp_fd == -1) {
101 if (tmp_fd != -1) {
102 close(tmp_fd);
104 status = NT_STATUS_UNSUCCESSFUL;
105 goto out;
107 outfd = open(magic_output, O_CREAT|O_EXCL|O_RDWR, 0600);
108 if (outfd == -1) {
109 int err = errno;
110 close(tmp_fd);
111 status = map_nt_error_from_unix(err);
112 goto out;
115 if (sys_fstat(tmp_fd, &st, false) == -1) {
116 int err = errno;
117 close(tmp_fd);
118 close(outfd);
119 status = map_nt_error_from_unix(err);
120 goto out;
123 if (transfer_file(tmp_fd,outfd,(off_t)st.st_ex_size) == (off_t)-1) {
124 int err = errno;
125 close(tmp_fd);
126 close(outfd);
127 status = map_nt_error_from_unix(err);
128 goto out;
130 close(tmp_fd);
131 if (close(outfd) == -1) {
132 status = map_nt_error_from_unix(errno);
133 goto out;
136 status = NT_STATUS_OK;
138 out:
139 TALLOC_FREE(ctx);
140 return status;
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);
158 return status;
161 /****************************************************************************
162 Delete all streams
163 ****************************************************************************/
165 NTSTATUS delete_all_streams(connection_struct *conn,
166 const struct smb_filename *smb_fname)
168 struct stream_struct *stream_info = NULL;
169 int i;
170 unsigned int num_streams = 0;
171 TALLOC_CTX *frame = talloc_stackframe();
172 NTSTATUS status;
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"));
179 TALLOC_FREE(frame);
180 return NT_STATUS_OK;
183 if (!NT_STATUS_IS_OK(status)) {
184 DEBUG(10, ("vfs_streaminfo failed: %s\n",
185 nt_errstr(status)));
186 goto fail;
189 DEBUG(10, ("delete_all_streams found %d streams\n",
190 num_streams));
192 if (num_streams == 0) {
193 TALLOC_FREE(frame);
194 return NT_STATUS_OK;
197 for (i=0; i<num_streams; i++) {
198 int res;
199 struct smb_filename *smb_fname_stream;
201 if (strequal(stream_info[i].name, "::$DATA")) {
202 continue;
205 smb_fname_stream = synthetic_smb_fname(talloc_tos(),
206 smb_fname->base_name,
207 stream_info[i].name,
208 NULL,
209 (smb_fname->flags &
210 ~SMB_FILENAME_POSIX_PATH));
212 if (smb_fname_stream == NULL) {
213 DEBUG(0, ("talloc_aprintf failed\n"));
214 status = NT_STATUS_NO_MEMORY;
215 goto fail;
218 res = SMB_VFS_UNLINK(conn, smb_fname_stream);
220 if (res == -1) {
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),
224 strerror(errno)));
225 TALLOC_FREE(smb_fname_stream);
226 break;
228 TALLOC_FREE(smb_fname_stream);
231 fail:
232 TALLOC_FREE(frame);
233 return status;
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;
249 NTSTATUS tmp_status;
250 struct file_id id;
251 const struct security_unix_token *del_token = NULL;
252 const struct security_token *del_nt_token = NULL;
253 bool got_tokens = false;
254 bool normal_close;
256 /* Ensure any pending write time updates are done. */
257 if (fsp->update_write_time_event) {
258 fsp_flush_write_time_update(fsp);
262 * Lock the share entries, and determine if we should delete
263 * on close. If so delete whilst the lock is still in effect.
264 * This prevents race conditions with the file being created. JRA.
267 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
268 if (lck == NULL) {
269 DEBUG(0, ("close_remove_share_mode: Could not get share mode "
270 "lock for file %s\n", fsp_str_dbg(fsp)));
271 return NT_STATUS_INVALID_PARAMETER;
274 /* Remove the oplock before potentially deleting the file. */
275 if(fsp->oplock_type) {
276 remove_oplock_under_lock(fsp, lck);
279 if (fsp->write_time_forced) {
280 DEBUG(10,("close_remove_share_mode: write time forced "
281 "for file %s\n",
282 fsp_str_dbg(fsp)));
283 set_close_write_time(fsp, lck->data->changed_write_time);
284 } else if (fsp->update_write_time_on_close) {
285 /* Someone had a pending write. */
286 if (null_timespec(fsp->close_write_time)) {
287 DEBUG(10,("close_remove_share_mode: update to current time "
288 "for file %s\n",
289 fsp_str_dbg(fsp)));
290 /* Update to current time due to "normal" write. */
291 set_close_write_time(fsp, timespec_current());
292 } else {
293 DEBUG(10,("close_remove_share_mode: write time pending "
294 "for file %s\n",
295 fsp_str_dbg(fsp)));
296 /* Update to time set on close call. */
297 set_close_write_time(fsp, fsp->close_write_time);
301 if (fsp->initial_delete_on_close &&
302 !is_delete_on_close_set(lck, fsp->name_hash)) {
303 bool became_user = False;
305 /* Initial delete on close was set and no one else
306 * wrote a real delete on close. */
308 if (get_current_vuid(conn) != fsp->vuid) {
309 become_user(conn, fsp->vuid);
310 became_user = True;
312 fsp->delete_on_close = true;
313 set_delete_on_close_lck(fsp, lck,
314 get_current_nttok(conn),
315 get_current_utok(conn));
316 if (became_user) {
317 unbecome_user();
321 delete_file = is_delete_on_close_set(lck, fsp->name_hash);
323 if (delete_file) {
324 int i;
325 /* See if others still have the file open via this pathname.
326 If this is the case, then don't delete. If all opens are
327 POSIX delete now. */
328 for (i=0; i<lck->data->num_share_modes; i++) {
329 struct share_mode_entry *e = &lck->data->share_modes[i];
331 if (!is_valid_share_mode_entry(e)) {
332 continue;
334 if (e->name_hash != fsp->name_hash) {
335 continue;
337 if ((fsp->posix_flags & FSP_POSIX_FLAGS_OPEN)
338 && (e->flags & SHARE_MODE_FLAG_POSIX_OPEN)) {
339 continue;
341 if (serverid_equal(&self, &e->pid) &&
342 (e->share_file_id == fsp->fh->gen_id)) {
343 continue;
345 if (share_mode_stale_pid(lck->data, i)) {
346 continue;
348 delete_file = False;
349 break;
354 * NT can set delete_on_close of the last open
355 * reference to a file.
358 normal_close = (close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE);
360 if (!normal_close || !delete_file) {
361 status = NT_STATUS_OK;
362 goto done;
366 * Ok, we have to delete the file
369 DEBUG(5,("close_remove_share_mode: file %s. Delete on close was set "
370 "- deleting file.\n", fsp_str_dbg(fsp)));
373 * Don't try to update the write time when we delete the file
375 fsp->update_write_time_on_close = false;
377 got_tokens = get_delete_on_close_token(lck, fsp->name_hash,
378 &del_nt_token, &del_token);
379 SMB_ASSERT(got_tokens);
381 if (!unix_token_equal(del_token, get_current_utok(conn))) {
382 /* Become the user who requested the delete. */
384 DEBUG(5,("close_remove_share_mode: file %s. "
385 "Change user to uid %u\n",
386 fsp_str_dbg(fsp),
387 (unsigned int)del_token->uid));
389 if (!push_sec_ctx()) {
390 smb_panic("close_remove_share_mode: file %s. failed to push "
391 "sec_ctx.\n");
394 set_sec_ctx(del_token->uid,
395 del_token->gid,
396 del_token->ngroups,
397 del_token->groups,
398 del_nt_token);
400 changed_user = true;
403 /* We can only delete the file if the name we have is still valid and
404 hasn't been renamed. */
406 tmp_status = vfs_stat_fsp(fsp);
407 if (!NT_STATUS_IS_OK(tmp_status)) {
408 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
409 "was set and stat failed with error %s\n",
410 fsp_str_dbg(fsp), nt_errstr(tmp_status)));
412 * Don't save the errno here, we ignore this error
414 goto done;
417 id = vfs_file_id_from_sbuf(conn, &fsp->fsp_name->st);
419 if (!file_id_equal(&fsp->file_id, &id)) {
420 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
421 "was set and dev and/or inode does not match\n",
422 fsp_str_dbg(fsp)));
423 DEBUG(5,("close_remove_share_mode: file %s. stored file_id %s, "
424 "stat file_id %s\n",
425 fsp_str_dbg(fsp),
426 file_id_string_tos(&fsp->file_id),
427 file_id_string_tos(&id)));
429 * Don't save the errno here, we ignore this error
431 goto done;
434 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
435 && !is_ntfs_stream_smb_fname(fsp->fsp_name)) {
437 status = delete_all_streams(conn, fsp->fsp_name);
439 if (!NT_STATUS_IS_OK(status)) {
440 DEBUG(5, ("delete_all_streams failed: %s\n",
441 nt_errstr(status)));
442 goto done;
446 if (fsp->kernel_share_modes_taken) {
447 int ret_flock;
450 * A file system sharemode could block the unlink;
451 * remove filesystem sharemodes first.
453 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, 0, 0);
454 if (ret_flock == -1) {
455 DBG_INFO("removing kernel flock for %s failed: %s\n",
456 fsp_str_dbg(fsp), strerror(errno));
459 fsp->kernel_share_modes_taken = false;
463 if (SMB_VFS_UNLINK(conn, fsp->fsp_name) != 0) {
465 * This call can potentially fail as another smbd may
466 * have had the file open with delete on close set and
467 * deleted it when its last reference to this file
468 * went away. Hence we log this but not at debug level
469 * zero.
472 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
473 "was set and unlink failed with error %s\n",
474 fsp_str_dbg(fsp), strerror(errno)));
476 status = map_nt_error_from_unix(errno);
479 /* As we now have POSIX opens which can unlink
480 * with other open files we may have taken
481 * this code path with more than one share mode
482 * entry - ensure we only delete once by resetting
483 * the delete on close flag. JRA.
486 fsp->delete_on_close = false;
487 reset_delete_on_close_lck(fsp, lck);
489 done:
491 if (changed_user) {
492 /* unbecome user. */
493 pop_sec_ctx();
496 if (fsp->kernel_share_modes_taken) {
497 int ret_flock;
499 /* remove filesystem sharemodes */
500 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, 0, 0);
501 if (ret_flock == -1) {
502 DEBUG(2, ("close_remove_share_mode: removing kernel "
503 "flock for %s failed: %s\n",
504 fsp_str_dbg(fsp), strerror(errno)));
508 if (!del_share_mode(lck, fsp)) {
509 DEBUG(0, ("close_remove_share_mode: Could not delete share "
510 "entry for file %s\n", fsp_str_dbg(fsp)));
513 TALLOC_FREE(lck);
515 if (delete_file) {
517 * Do the notification after we released the share
518 * mode lock. Inside notify_fname we take out another
519 * tdb lock. With ctdb also accessing our databases,
520 * this can lead to deadlocks. Putting this notify
521 * after the TALLOC_FREE(lck) above we avoid locking
522 * two records simultaneously. Notifies are async and
523 * informational only, so calling the notify_fname
524 * without holding the share mode lock should not do
525 * any harm.
527 notify_fname(conn, NOTIFY_ACTION_REMOVED,
528 FILE_NOTIFY_CHANGE_FILE_NAME,
529 fsp->fsp_name->base_name);
532 return status;
535 void set_close_write_time(struct files_struct *fsp, struct timespec ts)
537 DEBUG(6,("close_write_time: %s" , time_to_asc(convert_timespec_to_time_t(ts))));
539 if (null_timespec(ts)) {
540 return;
542 fsp->write_time_forced = false;
543 fsp->update_write_time_on_close = true;
544 fsp->close_write_time = ts;
547 static NTSTATUS update_write_time_on_close(struct files_struct *fsp)
549 struct smb_file_time ft;
550 NTSTATUS status;
551 struct share_mode_lock *lck = NULL;
553 ZERO_STRUCT(ft);
555 if (!fsp->update_write_time_on_close) {
556 return NT_STATUS_OK;
559 if (null_timespec(fsp->close_write_time)) {
560 fsp->close_write_time = timespec_current();
563 /* Ensure we have a valid stat struct for the source. */
564 status = vfs_stat_fsp(fsp);
565 if (!NT_STATUS_IS_OK(status)) {
566 return status;
569 if (!VALID_STAT(fsp->fsp_name->st)) {
570 /* if it doesn't seem to be a real file */
571 return NT_STATUS_OK;
575 * get_existing_share_mode_lock() isn't really the right
576 * call here, as we're being called after
577 * close_remove_share_mode() inside close_normal_file()
578 * so it's quite normal to not have an existing share
579 * mode here. However, get_share_mode_lock() doesn't
580 * work because that will create a new share mode if
581 * one doesn't exist - so stick with this call (just
582 * ignore any error we get if the share mode doesn't
583 * exist.
586 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
587 if (lck) {
588 /* On close if we're changing the real file time we
589 * must update it in the open file db too. */
590 (void)set_write_time(fsp->file_id, fsp->close_write_time);
592 /* Close write times overwrite sticky write times
593 so we must replace any sticky write time here. */
594 if (!null_timespec(lck->data->changed_write_time)) {
595 (void)set_sticky_write_time(fsp->file_id, fsp->close_write_time);
597 TALLOC_FREE(lck);
600 ft.mtime = fsp->close_write_time;
601 /* As this is a close based update, we are not directly changing the
602 file attributes from a client call, but indirectly from a write. */
603 status = smb_set_file_time(fsp->conn, fsp, fsp->fsp_name, &ft, false);
604 if (!NT_STATUS_IS_OK(status)) {
605 DEBUG(10,("update_write_time_on_close: smb_set_file_time "
606 "on file %s returned %s\n",
607 fsp_str_dbg(fsp),
608 nt_errstr(status)));
609 return status;
612 return status;
615 static NTSTATUS ntstatus_keeperror(NTSTATUS s1, NTSTATUS s2)
617 if (!NT_STATUS_IS_OK(s1)) {
618 return s1;
620 return s2;
623 /****************************************************************************
624 Close a file.
626 close_type can be NORMAL_CLOSE=0,SHUTDOWN_CLOSE,ERROR_CLOSE.
627 printing and magic scripts are only run on normal close.
628 delete on close is done on normal and shutdown close.
629 ****************************************************************************/
631 static NTSTATUS close_normal_file(struct smb_request *req, files_struct *fsp,
632 enum file_close_type close_type)
634 NTSTATUS status = NT_STATUS_OK;
635 NTSTATUS tmp;
636 connection_struct *conn = fsp->conn;
637 bool is_durable = false;
639 if (fsp->num_aio_requests != 0) {
641 if (close_type != SHUTDOWN_CLOSE) {
643 * reply_close and the smb2 close must have
644 * taken care of this. No other callers of
645 * close_file should ever have created async
646 * I/O.
648 * We need to panic here because if we close()
649 * the fd while we have outstanding async I/O
650 * requests, in the worst case we could end up
651 * writing to the wrong file.
653 DEBUG(0, ("fsp->num_aio_requests=%u\n",
654 fsp->num_aio_requests));
655 smb_panic("can not close with outstanding aio "
656 "requests");
660 * For shutdown close, just drop the async requests
661 * including a potential close request pending for
662 * this fsp. Drop the close request first, the
663 * destructor for the aio_requests would execute it.
665 TALLOC_FREE(fsp->deferred_close);
667 while (fsp->num_aio_requests != 0) {
669 * The destructor of the req will remove
670 * itself from the fsp.
671 * Don't use TALLOC_FREE here, this will overwrite
672 * what the destructor just wrote into
673 * aio_requests[0].
675 talloc_free(fsp->aio_requests[0]);
680 * If we're flushing on a close we can get a write
681 * error here, we must remember this.
684 tmp = close_filestruct(fsp);
685 status = ntstatus_keeperror(status, tmp);
687 if (NT_STATUS_IS_OK(status) && fsp->op != NULL) {
688 is_durable = fsp->op->global->durable;
691 if (close_type != SHUTDOWN_CLOSE) {
692 is_durable = false;
695 if (is_durable) {
696 DATA_BLOB new_cookie = data_blob_null;
698 tmp = SMB_VFS_DURABLE_DISCONNECT(fsp,
699 fsp->op->global->backend_cookie,
700 fsp->op,
701 &new_cookie);
702 if (NT_STATUS_IS_OK(tmp)) {
703 struct timeval tv;
704 NTTIME now;
706 if (req != NULL) {
707 tv = req->request_time;
708 } else {
709 tv = timeval_current();
711 now = timeval_to_nttime(&tv);
713 data_blob_free(&fsp->op->global->backend_cookie);
714 fsp->op->global->backend_cookie = new_cookie;
716 fsp->op->compat = NULL;
717 tmp = smbXsrv_open_close(fsp->op, now);
718 if (!NT_STATUS_IS_OK(tmp)) {
719 DEBUG(1, ("Failed to update smbXsrv_open "
720 "record when disconnecting durable "
721 "handle for file %s: %s - "
722 "proceeding with normal close\n",
723 fsp_str_dbg(fsp), nt_errstr(tmp)));
725 scavenger_schedule_disconnected(fsp);
726 } else {
727 DEBUG(1, ("Failed to disconnect durable handle for "
728 "file %s: %s - proceeding with normal "
729 "close\n", fsp_str_dbg(fsp), nt_errstr(tmp)));
731 if (!NT_STATUS_IS_OK(tmp)) {
732 is_durable = false;
736 if (is_durable) {
738 * This is the case where we successfully disconnected
739 * a durable handle and closed the underlying file.
740 * In all other cases, we proceed with a genuine close.
742 DEBUG(10, ("%s disconnected durable handle for file %s\n",
743 conn->session_info->unix_info->unix_name,
744 fsp_str_dbg(fsp)));
745 file_free(req, fsp);
746 return NT_STATUS_OK;
749 if (fsp->op != NULL) {
751 * Make sure the handle is not marked as durable anymore
753 fsp->op->global->durable = false;
756 if (fsp->print_file) {
757 /* FIXME: return spool errors */
758 print_spool_end(fsp, close_type);
759 file_free(req, fsp);
760 return NT_STATUS_OK;
763 /* If this is an old DOS or FCB open and we have multiple opens on
764 the same handle we only have one share mode. Ensure we only remove
765 the share mode on the last close. */
767 if (fsp->fh->ref_count == 1) {
768 /* Should we return on error here... ? */
769 tmp = close_remove_share_mode(fsp, close_type);
770 status = ntstatus_keeperror(status, tmp);
773 locking_close_file(conn->sconn->msg_ctx, fsp, close_type);
775 tmp = fd_close(fsp);
776 status = ntstatus_keeperror(status, tmp);
778 /* check for magic scripts */
779 if (close_type == NORMAL_CLOSE) {
780 tmp = check_magic(fsp);
781 status = ntstatus_keeperror(status, tmp);
785 * Ensure pending modtime is set after close.
788 tmp = update_write_time_on_close(fsp);
789 if (NT_STATUS_EQUAL(tmp, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
790 /* Someone renamed the file or a parent directory containing
791 * this file. We can't do anything about this, we don't have
792 * an "update timestamp by fd" call in POSIX. Eat the error. */
794 tmp = NT_STATUS_OK;
797 status = ntstatus_keeperror(status, tmp);
799 DEBUG(2,("%s closed file %s (numopen=%d) %s\n",
800 conn->session_info->unix_info->unix_name, fsp_str_dbg(fsp),
801 conn->num_files_open - 1,
802 nt_errstr(status) ));
804 file_free(req, fsp);
805 return status;
807 /****************************************************************************
808 Function used by reply_rmdir to delete an entire directory
809 tree recursively. Return True on ok, False on fail.
810 ****************************************************************************/
812 bool recursive_rmdir(TALLOC_CTX *ctx,
813 connection_struct *conn,
814 struct smb_filename *smb_dname)
816 const char *dname = NULL;
817 char *talloced = NULL;
818 bool ret = True;
819 long offset = 0;
820 SMB_STRUCT_STAT st;
821 struct smb_Dir *dir_hnd;
823 SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname));
825 dir_hnd = OpenDir(talloc_tos(), conn, smb_dname, NULL, 0);
826 if(dir_hnd == NULL)
827 return False;
829 while((dname = ReadDirName(dir_hnd, &offset, &st, &talloced))) {
830 struct smb_filename *smb_dname_full = NULL;
831 char *fullname = NULL;
832 bool do_break = true;
834 if (ISDOT(dname) || ISDOTDOT(dname)) {
835 TALLOC_FREE(talloced);
836 continue;
839 if (!is_visible_file(conn, smb_dname->base_name, dname, &st,
840 false)) {
841 TALLOC_FREE(talloced);
842 continue;
845 /* Construct the full name. */
846 fullname = talloc_asprintf(ctx,
847 "%s/%s",
848 smb_dname->base_name,
849 dname);
850 if (!fullname) {
851 errno = ENOMEM;
852 goto err_break;
855 smb_dname_full = synthetic_smb_fname(talloc_tos(),
856 fullname,
857 NULL,
858 NULL,
859 smb_dname->flags);
860 if (smb_dname_full == NULL) {
861 errno = ENOMEM;
862 goto err_break;
865 if(SMB_VFS_LSTAT(conn, smb_dname_full) != 0) {
866 goto err_break;
869 if(smb_dname_full->st.st_ex_mode & S_IFDIR) {
870 if(!recursive_rmdir(ctx, conn, smb_dname_full)) {
871 goto err_break;
873 if(SMB_VFS_RMDIR(conn, smb_dname_full) != 0) {
874 goto err_break;
876 } else if(SMB_VFS_UNLINK(conn, smb_dname_full) != 0) {
877 goto err_break;
880 /* Successful iteration. */
881 do_break = false;
883 err_break:
884 TALLOC_FREE(smb_dname_full);
885 TALLOC_FREE(fullname);
886 TALLOC_FREE(talloced);
887 if (do_break) {
888 ret = false;
889 break;
892 TALLOC_FREE(dir_hnd);
893 return ret;
896 /****************************************************************************
897 The internals of the rmdir code - called elsewhere.
898 ****************************************************************************/
900 static NTSTATUS rmdir_internals(TALLOC_CTX *ctx, files_struct *fsp)
902 connection_struct *conn = fsp->conn;
903 struct smb_filename *smb_dname = fsp->fsp_name;
904 int ret;
906 SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname));
908 /* Might be a symlink. */
909 if(SMB_VFS_LSTAT(conn, smb_dname) != 0) {
910 return map_nt_error_from_unix(errno);
913 if (S_ISLNK(smb_dname->st.st_ex_mode)) {
914 /* Is what it points to a directory ? */
915 if(SMB_VFS_STAT(conn, smb_dname) != 0) {
916 return map_nt_error_from_unix(errno);
918 if (!(S_ISDIR(smb_dname->st.st_ex_mode))) {
919 return NT_STATUS_NOT_A_DIRECTORY;
921 ret = SMB_VFS_UNLINK(conn, smb_dname);
922 } else {
923 ret = SMB_VFS_RMDIR(conn, smb_dname);
925 if (ret == 0) {
926 notify_fname(conn, NOTIFY_ACTION_REMOVED,
927 FILE_NOTIFY_CHANGE_DIR_NAME,
928 smb_dname->base_name);
929 return NT_STATUS_OK;
932 if(((errno == ENOTEMPTY)||(errno == EEXIST)) && *lp_veto_files(talloc_tos(), SNUM(conn))) {
934 * Check to see if the only thing in this directory are
935 * vetoed files/directories. If so then delete them and
936 * retry. If we fail to delete any of them (and we *don't*
937 * do a recursive delete) then fail the rmdir.
939 SMB_STRUCT_STAT st;
940 const char *dname = NULL;
941 char *talloced = NULL;
942 long dirpos = 0;
943 struct smb_Dir *dir_hnd = OpenDir(talloc_tos(), conn,
944 smb_dname, NULL,
947 if(dir_hnd == NULL) {
948 errno = ENOTEMPTY;
949 goto err;
952 while ((dname = ReadDirName(dir_hnd, &dirpos, &st,
953 &talloced)) != NULL) {
954 if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0)) {
955 TALLOC_FREE(talloced);
956 continue;
958 if (!is_visible_file(conn, smb_dname->base_name, dname,
959 &st, false)) {
960 TALLOC_FREE(talloced);
961 continue;
963 if(!IS_VETO_PATH(conn, dname)) {
964 TALLOC_FREE(dir_hnd);
965 TALLOC_FREE(talloced);
966 errno = ENOTEMPTY;
967 goto err;
969 TALLOC_FREE(talloced);
972 /* We only have veto files/directories.
973 * Are we allowed to delete them ? */
975 if(!lp_delete_veto_files(SNUM(conn))) {
976 TALLOC_FREE(dir_hnd);
977 errno = ENOTEMPTY;
978 goto err;
981 /* Do a recursive delete. */
982 RewindDir(dir_hnd,&dirpos);
983 while ((dname = ReadDirName(dir_hnd, &dirpos, &st,
984 &talloced)) != NULL) {
985 struct smb_filename *smb_dname_full = NULL;
986 char *fullname = NULL;
987 bool do_break = true;
989 if (ISDOT(dname) || ISDOTDOT(dname)) {
990 TALLOC_FREE(talloced);
991 continue;
993 if (!is_visible_file(conn, smb_dname->base_name, dname,
994 &st, false)) {
995 TALLOC_FREE(talloced);
996 continue;
999 fullname = talloc_asprintf(ctx,
1000 "%s/%s",
1001 smb_dname->base_name,
1002 dname);
1004 if(!fullname) {
1005 errno = ENOMEM;
1006 goto err_break;
1009 smb_dname_full = synthetic_smb_fname(talloc_tos(),
1010 fullname,
1011 NULL,
1012 NULL,
1013 smb_dname->flags);
1014 if (smb_dname_full == NULL) {
1015 errno = ENOMEM;
1016 goto err_break;
1019 if(SMB_VFS_LSTAT(conn, smb_dname_full) != 0) {
1020 goto err_break;
1022 if(smb_dname_full->st.st_ex_mode & S_IFDIR) {
1023 if(!recursive_rmdir(ctx, conn,
1024 smb_dname_full)) {
1025 goto err_break;
1027 if(SMB_VFS_RMDIR(conn,
1028 smb_dname_full) != 0) {
1029 goto err_break;
1031 } else if(SMB_VFS_UNLINK(conn, smb_dname_full) != 0) {
1032 goto err_break;
1035 /* Successful iteration. */
1036 do_break = false;
1038 err_break:
1039 TALLOC_FREE(fullname);
1040 TALLOC_FREE(smb_dname_full);
1041 TALLOC_FREE(talloced);
1042 if (do_break)
1043 break;
1045 TALLOC_FREE(dir_hnd);
1046 /* Retry the rmdir */
1047 ret = SMB_VFS_RMDIR(conn, smb_dname);
1050 err:
1052 if (ret != 0) {
1053 DEBUG(3,("rmdir_internals: couldn't remove directory %s : "
1054 "%s\n", smb_fname_str_dbg(smb_dname),
1055 strerror(errno)));
1056 return map_nt_error_from_unix(errno);
1059 notify_fname(conn, NOTIFY_ACTION_REMOVED,
1060 FILE_NOTIFY_CHANGE_DIR_NAME,
1061 smb_dname->base_name);
1063 return NT_STATUS_OK;
1066 /****************************************************************************
1067 Close a directory opened by an NT SMB call.
1068 ****************************************************************************/
1070 static NTSTATUS close_directory(struct smb_request *req, files_struct *fsp,
1071 enum file_close_type close_type)
1073 struct server_id self = messaging_server_id(fsp->conn->sconn->msg_ctx);
1074 struct share_mode_lock *lck = NULL;
1075 bool delete_dir = False;
1076 NTSTATUS status = NT_STATUS_OK;
1077 NTSTATUS status1 = NT_STATUS_OK;
1078 const struct security_token *del_nt_token = NULL;
1079 const struct security_unix_token *del_token = NULL;
1080 NTSTATUS notify_status;
1082 if (fsp->conn->sconn->using_smb2) {
1083 notify_status = STATUS_NOTIFY_CLEANUP;
1084 } else {
1085 notify_status = NT_STATUS_OK;
1089 * NT can set delete_on_close of the last open
1090 * reference to a directory also.
1093 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
1094 if (lck == NULL) {
1095 DEBUG(0, ("close_directory: Could not get share mode lock for "
1096 "%s\n", fsp_str_dbg(fsp)));
1097 return NT_STATUS_INVALID_PARAMETER;
1100 if (fsp->initial_delete_on_close) {
1101 bool became_user = False;
1103 /* Initial delete on close was set - for
1104 * directories we don't care if anyone else
1105 * wrote a real delete on close. */
1107 if (get_current_vuid(fsp->conn) != fsp->vuid) {
1108 become_user(fsp->conn, fsp->vuid);
1109 became_user = True;
1111 send_stat_cache_delete_message(fsp->conn->sconn->msg_ctx,
1112 fsp->fsp_name->base_name);
1113 set_delete_on_close_lck(fsp, lck,
1114 get_current_nttok(fsp->conn),
1115 get_current_utok(fsp->conn));
1116 fsp->delete_on_close = true;
1117 if (became_user) {
1118 unbecome_user();
1122 delete_dir = get_delete_on_close_token(lck, fsp->name_hash,
1123 &del_nt_token, &del_token);
1125 if (delete_dir) {
1126 int i;
1127 /* See if others still have the dir open. If this is the
1128 * case, then don't delete. If all opens are POSIX delete now. */
1129 for (i=0; i<lck->data->num_share_modes; i++) {
1130 struct share_mode_entry *e = &lck->data->share_modes[i];
1131 if (is_valid_share_mode_entry(e) &&
1132 e->name_hash == fsp->name_hash) {
1133 if ((fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) &&
1134 (e->flags & SHARE_MODE_FLAG_POSIX_OPEN))
1136 continue;
1138 if (serverid_equal(&self, &e->pid) &&
1139 (e->share_file_id == fsp->fh->gen_id)) {
1140 continue;
1142 if (share_mode_stale_pid(lck->data, i)) {
1143 continue;
1145 delete_dir = False;
1146 break;
1151 if ((close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE) &&
1152 delete_dir) {
1154 /* Become the user who requested the delete. */
1156 if (!push_sec_ctx()) {
1157 smb_panic("close_directory: failed to push sec_ctx.\n");
1160 set_sec_ctx(del_token->uid,
1161 del_token->gid,
1162 del_token->ngroups,
1163 del_token->groups,
1164 del_nt_token);
1166 if (!del_share_mode(lck, fsp)) {
1167 DEBUG(0, ("close_directory: Could not delete share entry for "
1168 "%s\n", fsp_str_dbg(fsp)));
1171 TALLOC_FREE(lck);
1173 if ((fsp->conn->fs_capabilities & FILE_NAMED_STREAMS)
1174 && !is_ntfs_stream_smb_fname(fsp->fsp_name)) {
1176 status = delete_all_streams(fsp->conn, fsp->fsp_name);
1177 if (!NT_STATUS_IS_OK(status)) {
1178 DEBUG(5, ("delete_all_streams failed: %s\n",
1179 nt_errstr(status)));
1180 return status;
1184 status = rmdir_internals(talloc_tos(), fsp);
1186 DEBUG(5,("close_directory: %s. Delete on close was set - "
1187 "deleting directory returned %s.\n",
1188 fsp_str_dbg(fsp), nt_errstr(status)));
1190 /* unbecome user. */
1191 pop_sec_ctx();
1194 * Ensure we remove any change notify requests that would
1195 * now fail as the directory has been deleted.
1198 if (NT_STATUS_IS_OK(status)) {
1199 notify_status = NT_STATUS_DELETE_PENDING;
1201 } else {
1202 if (!del_share_mode(lck, fsp)) {
1203 DEBUG(0, ("close_directory: Could not delete share entry for "
1204 "%s\n", fsp_str_dbg(fsp)));
1207 TALLOC_FREE(lck);
1210 remove_pending_change_notify_requests_by_fid(fsp, notify_status);
1212 status1 = fd_close(fsp);
1214 if (!NT_STATUS_IS_OK(status1)) {
1215 DEBUG(0, ("Could not close dir! fname=%s, fd=%d, err=%d=%s\n",
1216 fsp_str_dbg(fsp), fsp->fh->fd, errno,
1217 strerror(errno)));
1221 * Do the code common to files and directories.
1223 close_filestruct(fsp);
1224 file_free(req, fsp);
1226 if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(status1)) {
1227 status = status1;
1229 return status;
1232 /****************************************************************************
1233 Close a files_struct.
1234 ****************************************************************************/
1236 NTSTATUS close_file(struct smb_request *req, files_struct *fsp,
1237 enum file_close_type close_type)
1239 NTSTATUS status;
1240 struct files_struct *base_fsp = fsp->base_fsp;
1242 if(fsp->is_directory) {
1243 status = close_directory(req, fsp, close_type);
1244 } else if (fsp->fake_file_handle != NULL) {
1245 status = close_fake_file(req, fsp);
1246 } else {
1247 status = close_normal_file(req, fsp, close_type);
1250 if ((base_fsp != NULL) && (close_type != SHUTDOWN_CLOSE)) {
1253 * fsp was a stream, the base fsp can't be a stream as well
1255 * For SHUTDOWN_CLOSE this is not possible here, because
1256 * SHUTDOWN_CLOSE only happens from files.c which walks the
1257 * complete list of files. If we mess with more than one fsp
1258 * those loops will become confused.
1261 SMB_ASSERT(base_fsp->base_fsp == NULL);
1262 close_file(req, base_fsp, close_type);
1265 return status;
1268 /****************************************************************************
1269 Deal with an (authorized) message to close a file given the share mode
1270 entry.
1271 ****************************************************************************/
1273 void msg_close_file(struct messaging_context *msg_ctx,
1274 void *private_data,
1275 uint32_t msg_type,
1276 struct server_id server_id,
1277 DATA_BLOB *data)
1279 files_struct *fsp = NULL;
1280 struct file_id id;
1281 struct share_mode_entry e;
1282 struct smbd_server_connection *sconn =
1283 talloc_get_type_abort(private_data,
1284 struct smbd_server_connection);
1286 message_to_share_mode_entry(&id, &e, (char *)data->data);
1288 if(DEBUGLVL(10)) {
1289 char *sm_str = share_mode_str(NULL, 0, &id, &e);
1290 if (!sm_str) {
1291 smb_panic("talloc failed");
1293 DEBUG(10,("msg_close_file: got request to close share mode "
1294 "entry %s\n", sm_str));
1295 TALLOC_FREE(sm_str);
1298 fsp = file_find_dif(sconn, id, e.share_file_id);
1299 if (!fsp) {
1300 DEBUG(10,("msg_close_file: failed to find file.\n"));
1301 return;
1303 close_file(NULL, fsp, NORMAL_CLOSE);