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/>.
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)
33 const char *magic_output = NULL;
36 TALLOC_CTX *ctx = NULL;
38 struct connection_struct *conn = fsp->conn;
42 if (!*lp_magicscript(SNUM(conn))) {
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,'/'))) {
58 if (!strequal(lp_magicscript(SNUM(conn)),p)) {
59 status = NT_STATUS_OK;
63 if (*lp_magicoutput(SNUM(conn))) {
64 magic_output = lp_magicoutput(SNUM(conn));
66 magic_output = talloc_asprintf(ctx,
71 status = NT_STATUS_NO_MEMORY;
75 /* Ensure we don't depend on user's PATH. */
76 p = talloc_asprintf(ctx, "./%s", fname);
78 status = NT_STATUS_NO_MEMORY;
82 if (chmod(fname, 0755) == -1) {
83 status = map_nt_error_from_unix(errno);
86 ret = smbrun(p,&tmp_fd);
87 DEBUG(3,("Invoking magic command %s gave %d\n",
91 if (ret != 0 || tmp_fd == -1) {
95 status = NT_STATUS_UNSUCCESSFUL;
98 outfd = open(magic_output, O_CREAT|O_EXCL|O_RDWR, 0600);
102 status = map_nt_error_from_unix(err);
106 if (sys_fstat(tmp_fd,&st) == -1) {
110 status = map_nt_error_from_unix(err);
114 if (transfer_file(tmp_fd,outfd,(SMB_OFF_T)st.st_ex_size) == (SMB_OFF_T)-1) {
118 status = map_nt_error_from_unix(err);
122 if (close(outfd) == -1) {
123 status = map_nt_error_from_unix(errno);
127 status = NT_STATUS_OK;
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);
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)
160 if (!should_notify_deferred_opens()) {
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)) {
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
178 schedule_deferred_open_smb_message(e->op_mid);
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,
187 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
192 /****************************************************************************
194 ****************************************************************************/
196 NTSTATUS delete_all_streams(connection_struct *conn, const char *fname)
198 struct stream_struct *stream_info;
200 unsigned int num_streams;
201 TALLOC_CTX *frame = talloc_stackframe();
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"));
213 if (!NT_STATUS_IS_OK(status)) {
214 DEBUG(10, ("SMB_VFS_STREAMINFO failed: %s\n",
219 DEBUG(10, ("delete_all_streams found %d streams\n",
222 if (num_streams == 0) {
227 for (i=0; i<num_streams; i++) {
229 struct smb_filename *smb_fname_stream = NULL;
231 if (strequal(stream_info[i].name, "::$DATA")) {
235 status = create_synthetic_smb_fname(talloc_tos(), fname,
236 stream_info[i].name, NULL,
239 if (!NT_STATUS_IS_OK(status)) {
240 DEBUG(0, ("talloc_aprintf failed\n"));
244 res = SMB_VFS_UNLINK(conn, smb_fname_stream);
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),
251 TALLOC_FREE(smb_fname_stream);
254 TALLOC_FREE(smb_fname_stream);
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;
278 * Lock the share entries, and determine if we should delete
279 * on close. If so delete whilst the lock is still in effect.
280 * This prevents race conditions with the file being created. JRA.
283 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
287 DEBUG(0, ("close_remove_share_mode: Could not get share mode "
288 "lock for file %s\n", fsp_str_dbg(fsp)));
289 status = NT_STATUS_INVALID_PARAMETER;
293 if (fsp->write_time_forced) {
294 DEBUG(10,("close_remove_share_mode: write time forced "
297 set_close_write_time(fsp, lck->changed_write_time);
300 if (!del_share_mode(lck, fsp)) {
301 DEBUG(0, ("close_remove_share_mode: Could not delete share "
302 "entry for file %s\n",
306 if (fsp->initial_delete_on_close && (lck->delete_token == NULL)) {
307 bool became_user = False;
309 /* Initial delete on close was set and no one else
310 * wrote a real delete on close. */
312 if (current_user.vuid != fsp->vuid) {
313 become_user(conn, fsp->vuid);
316 set_delete_on_close_lck(lck, True, ¤t_user.ut);
322 delete_file = lck->delete_on_close;
326 /* See if others still have the file open. If this is the
327 * case, then don't delete. If all opens are POSIX delete now. */
328 for (i=0; i<lck->num_share_modes; i++) {
329 struct share_mode_entry *e = &lck->share_modes[i];
330 if (is_valid_share_mode_entry(e)) {
331 if (fsp->posix_open && (e->flags & SHARE_MODE_FLAG_POSIX_OPEN)) {
340 /* Notify any deferred opens waiting on this close. */
341 notify_deferred_opens(lck);
342 reply_to_oplock_break_requests(fsp);
345 * NT can set delete_on_close of the last open
346 * reference to a file.
349 if (!(close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE)
351 || (lck->delete_token == NULL)) {
357 * Ok, we have to delete the file
360 DEBUG(5,("close_remove_share_mode: file %s. Delete on close was set "
361 "- deleting file.\n", fsp_str_dbg(fsp)));
364 * Don't try to update the write time when we delete the file
366 fsp->update_write_time_on_close = false;
368 if (!unix_token_equal(lck->delete_token, ¤t_user.ut)) {
369 /* Become the user who requested the delete. */
371 DEBUG(5,("close_remove_share_mode: file %s. "
372 "Change user to uid %u\n",
374 (unsigned int)lck->delete_token->uid));
376 if (!push_sec_ctx()) {
377 smb_panic("close_remove_share_mode: file %s. failed to push "
381 set_sec_ctx(lck->delete_token->uid,
382 lck->delete_token->gid,
383 lck->delete_token->ngroups,
384 lck->delete_token->groups,
390 /* We can only delete the file if the name we have is still valid and
391 hasn't been renamed. */
393 tmp_status = vfs_stat_fsp(fsp);
394 if (!NT_STATUS_IS_OK(tmp_status)) {
395 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
396 "was set and stat failed with error %s\n",
397 fsp_str_dbg(fsp), nt_errstr(tmp_status)));
399 * Don't save the errno here, we ignore this error
404 id = vfs_file_id_from_sbuf(conn, &fsp->fsp_name->st);
406 if (!file_id_equal(&fsp->file_id, &id)) {
407 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
408 "was set and dev and/or inode does not match\n",
410 DEBUG(5,("close_remove_share_mode: file %s. stored file_id %s, "
413 file_id_string_tos(&fsp->file_id),
414 file_id_string_tos(&id)));
416 * Don't save the errno here, we ignore this error
421 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
422 && !is_ntfs_stream_smb_fname(fsp->fsp_name)) {
424 status = delete_all_streams(conn, fsp->fsp_name->base_name);
426 if (!NT_STATUS_IS_OK(status)) {
427 DEBUG(5, ("delete_all_streams failed: %s\n",
434 if (SMB_VFS_UNLINK(conn, fsp->fsp_name) != 0) {
436 * This call can potentially fail as another smbd may
437 * have had the file open with delete on close set and
438 * deleted it when its last reference to this file
439 * went away. Hence we log this but not at debug level
443 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
444 "was set and unlink failed with error %s\n",
445 fsp_str_dbg(fsp), strerror(errno)));
447 status = map_nt_error_from_unix(errno);
450 notify_fname(conn, NOTIFY_ACTION_REMOVED,
451 FILE_NOTIFY_CHANGE_FILE_NAME,
452 fsp->fsp_name->base_name);
454 /* As we now have POSIX opens which can unlink
455 * with other open files we may have taken
456 * this code path with more than one share mode
457 * entry - ensure we only delete once by resetting
458 * the delete on close flag. JRA.
461 set_delete_on_close_lck(lck, False, NULL);
474 void set_close_write_time(struct files_struct *fsp, struct timespec ts)
476 DEBUG(6,("close_write_time: %s" , time_to_asc(convert_timespec_to_time_t(ts))));
478 if (null_timespec(ts)) {
481 fsp->update_write_time_on_close = true;
482 fsp->close_write_time = ts;
485 static NTSTATUS update_write_time_on_close(struct files_struct *fsp)
487 struct smb_file_time ft;
492 if (!fsp->update_write_time_on_close) {
496 if (null_timespec(fsp->close_write_time)) {
497 fsp->close_write_time = timespec_current();
500 /* Ensure we have a valid stat struct for the source. */
501 status = vfs_stat_fsp(fsp);
502 if (!NT_STATUS_IS_OK(status)) {
506 if (!VALID_STAT(fsp->fsp_name->st)) {
507 /* if it doesn't seem to be a real file */
511 /* On close if we're changing the real file time we
512 * must update it in the open file db too. */
513 (void)set_write_time(fsp->file_id, fsp->close_write_time);
515 ft.mtime = fsp->close_write_time;
516 status = smb_set_file_time(fsp->conn, fsp, fsp->fsp_name, &ft, false);
517 if (!NT_STATUS_IS_OK(status)) {
524 static NTSTATUS ntstatus_keeperror(NTSTATUS s1, NTSTATUS s2)
526 if (!NT_STATUS_IS_OK(s1)) {
532 /****************************************************************************
535 close_type can be NORMAL_CLOSE=0,SHUTDOWN_CLOSE,ERROR_CLOSE.
536 printing and magic scripts are only run on normal close.
537 delete on close is done on normal and shutdown close.
538 ****************************************************************************/
540 static NTSTATUS close_normal_file(struct smb_request *req, files_struct *fsp,
541 enum file_close_type close_type)
543 NTSTATUS status = NT_STATUS_OK;
545 connection_struct *conn = fsp->conn;
547 if (fsp->aio_write_behind) {
549 * If we're finishing write behind on a close we can get a write
550 * error here, we must remember this.
552 int ret = wait_for_aio_completion(fsp);
554 status = ntstatus_keeperror(
555 status, map_nt_error_from_unix(ret));
558 cancel_aio_by_fsp(fsp);
562 * If we're flushing on a close we can get a write
563 * error here, we must remember this.
566 tmp = close_filestruct(fsp);
567 status = ntstatus_keeperror(status, tmp);
569 if (fsp->print_file) {
570 print_fsp_end(fsp, close_type);
575 /* Remove the oplock before potentially deleting the file. */
576 if(fsp->oplock_type) {
577 release_file_oplock(fsp);
580 /* If this is an old DOS or FCB open and we have multiple opens on
581 the same handle we only have one share mode. Ensure we only remove
582 the share mode on the last close. */
584 if (fsp->fh->ref_count == 1) {
585 /* Should we return on error here... ? */
586 tmp = close_remove_share_mode(fsp, close_type);
587 status = ntstatus_keeperror(status, tmp);
590 locking_close_file(smbd_messaging_context(), fsp);
593 status = ntstatus_keeperror(status, tmp);
595 /* check for magic scripts */
596 if (close_type == NORMAL_CLOSE) {
597 tmp = check_magic(fsp);
598 status = ntstatus_keeperror(status, tmp);
602 * Ensure pending modtime is set after close.
605 tmp = update_write_time_on_close(fsp);
606 if (NT_STATUS_EQUAL(tmp, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
607 /* Someone renamed the file or a parent directory containing
608 * this file. We can't do anything about this, we don't have
609 * an "update timestamp by fd" call in POSIX. Eat the error. */
614 status = ntstatus_keeperror(status, tmp);
616 DEBUG(2,("%s closed file %s (numopen=%d) %s\n",
617 conn->server_info->unix_name, fsp_str_dbg(fsp),
618 conn->num_files_open - 1,
619 nt_errstr(status) ));
625 /****************************************************************************
626 Close a directory opened by an NT SMB call.
627 ****************************************************************************/
629 static NTSTATUS close_directory(struct smb_request *req, files_struct *fsp,
630 enum file_close_type close_type)
632 struct share_mode_lock *lck = NULL;
633 bool delete_dir = False;
634 NTSTATUS status = NT_STATUS_OK;
637 * NT can set delete_on_close of the last open
638 * reference to a directory also.
641 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
645 DEBUG(0, ("close_directory: Could not get share mode lock for "
646 "%s\n", fsp_str_dbg(fsp)));
647 status = NT_STATUS_INVALID_PARAMETER;
651 if (!del_share_mode(lck, fsp)) {
652 DEBUG(0, ("close_directory: Could not delete share entry for "
653 "%s\n", fsp_str_dbg(fsp)));
656 if (fsp->initial_delete_on_close) {
657 bool became_user = False;
659 /* Initial delete on close was set - for
660 * directories we don't care if anyone else
661 * wrote a real delete on close. */
663 if (current_user.vuid != fsp->vuid) {
664 become_user(fsp->conn, fsp->vuid);
667 send_stat_cache_delete_message(fsp->fsp_name->base_name);
668 set_delete_on_close_lck(lck, True, ¤t_user.ut);
674 delete_dir = lck->delete_on_close;
678 /* See if others still have the dir open. If this is the
679 * case, then don't delete. If all opens are POSIX delete now. */
680 for (i=0; i<lck->num_share_modes; i++) {
681 struct share_mode_entry *e = &lck->share_modes[i];
682 if (is_valid_share_mode_entry(e)) {
683 if (fsp->posix_open && (e->flags & SHARE_MODE_FLAG_POSIX_OPEN)) {
692 if ((close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE) &&
696 /* Become the user who requested the delete. */
698 if (!push_sec_ctx()) {
699 smb_panic("close_directory: failed to push sec_ctx.\n");
702 set_sec_ctx(lck->delete_token->uid,
703 lck->delete_token->gid,
704 lck->delete_token->ngroups,
705 lck->delete_token->groups,
710 status = rmdir_internals(talloc_tos(), fsp->conn,
713 DEBUG(5,("close_directory: %s. Delete on close was set - "
714 "deleting directory returned %s.\n",
715 fsp_str_dbg(fsp), nt_errstr(status)));
721 * Ensure we remove any change notify requests that would
722 * now fail as the directory has been deleted.
725 if(NT_STATUS_IS_OK(status)) {
726 remove_pending_change_notify_requests_by_fid(fsp, NT_STATUS_DELETE_PENDING);
730 remove_pending_change_notify_requests_by_fid(
734 status = fd_close(fsp);
736 if (!NT_STATUS_IS_OK(status)) {
737 DEBUG(0, ("Could not close dir! fname=%s, fd=%d, err=%d=%s\n",
738 fsp_str_dbg(fsp), fsp->fh->fd, errno,
743 dptr_CloseDir(fsp->dptr);
747 * Do the code common to files and directories.
749 close_filestruct(fsp);
757 /****************************************************************************
758 Close a files_struct.
759 ****************************************************************************/
761 NTSTATUS close_file(struct smb_request *req, files_struct *fsp,
762 enum file_close_type close_type)
765 struct files_struct *base_fsp = fsp->base_fsp;
767 if(fsp->is_directory) {
768 status = close_directory(req, fsp, close_type);
769 } else if (fsp->fake_file_handle != NULL) {
770 status = close_fake_file(req, fsp);
772 status = close_normal_file(req, fsp, close_type);
775 if ((base_fsp != NULL) && (close_type != SHUTDOWN_CLOSE)) {
778 * fsp was a stream, the base fsp can't be a stream as well
780 * For SHUTDOWN_CLOSE this is not possible here, because
781 * SHUTDOWN_CLOSE only happens from files.c which walks the
782 * complete list of files. If we mess with more than one fsp
783 * those loops will become confused.
786 SMB_ASSERT(base_fsp->base_fsp == NULL);
787 close_file(req, base_fsp, close_type);
793 /****************************************************************************
794 Deal with an (authorized) message to close a file given the share mode
796 ****************************************************************************/
798 void msg_close_file(struct messaging_context *msg_ctx,
801 struct server_id server_id,
804 files_struct *fsp = NULL;
805 struct share_mode_entry e;
807 message_to_share_mode_entry(&e, (char *)data->data);
810 char *sm_str = share_mode_str(NULL, 0, &e);
812 smb_panic("talloc failed");
814 DEBUG(10,("msg_close_file: got request to close share mode "
815 "entry %s\n", sm_str));
819 fsp = file_find_dif(e.id, e.share_file_id);
821 DEBUG(10,("msg_close_file: failed to find file.\n"));
824 close_file(NULL, fsp, NORMAL_CLOSE);