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 void check_magic(struct files_struct *fsp)
33 const char *magic_output = NULL;
36 TALLOC_CTX *ctx = NULL;
38 struct connection_struct *conn = fsp->conn;
40 if (!*lp_magicscript(SNUM(conn))) {
44 DEBUG(5,("checking magic for %s\n",fsp->fsp_name));
46 if (!(p = strrchr_m(fsp->fsp_name,'/'))) {
52 if (!strequal(lp_magicscript(SNUM(conn)),p)) {
56 ctx = talloc_stackframe();
58 if (*lp_magicoutput(SNUM(conn))) {
59 magic_output = lp_magicoutput(SNUM(conn));
61 magic_output = talloc_asprintf(ctx,
70 /* Ensure we don't depend on user's PATH. */
71 p = talloc_asprintf(ctx, "./%s", fsp->fsp_name);
77 if (chmod(fsp->fsp_name,0755) == -1) {
81 ret = smbrun(p,&tmp_fd);
82 DEBUG(3,("Invoking magic command %s gave %d\n",
85 unlink(fsp->fsp_name);
86 if (ret != 0 || tmp_fd == -1) {
93 outfd = open(magic_output, O_CREAT|O_EXCL|O_RDWR, 0600);
100 if (sys_fstat(tmp_fd,&st) == -1) {
106 transfer_file(tmp_fd,outfd,(SMB_OFF_T)st.st_size);
112 /****************************************************************************
113 Common code to close a file or a directory.
114 ****************************************************************************/
116 static NTSTATUS close_filestruct(files_struct *fsp)
118 NTSTATUS status = NT_STATUS_OK;
120 if (fsp->fh->fd != -1) {
121 if(flush_write_cache(fsp, CLOSE_FLUSH) == -1) {
122 status = map_nt_error_from_unix(errno);
124 delete_write_cache(fsp);
130 /****************************************************************************
131 If any deferred opens are waiting on this close, notify them.
132 ****************************************************************************/
134 static void notify_deferred_opens(struct share_mode_lock *lck)
138 if (!should_notify_deferred_opens()) {
142 for (i=0; i<lck->num_share_modes; i++) {
143 struct share_mode_entry *e = &lck->share_modes[i];
145 if (!is_deferred_open_entry(e)) {
149 if (procid_is_me(&e->pid)) {
151 * We need to notify ourself to retry the open. Do
152 * this by finding the queued SMB record, moving it to
153 * the head of the queue and changing the wait time to
156 schedule_deferred_open_smb_message(e->op_mid);
158 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
160 share_mode_entry_to_message(msg, e);
162 messaging_send_buf(smbd_messaging_context(),
163 e->pid, MSG_SMB_OPEN_RETRY,
165 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
170 /****************************************************************************
172 ****************************************************************************/
174 NTSTATUS delete_all_streams(connection_struct *conn, const char *fname)
176 struct stream_struct *stream_info;
178 unsigned int num_streams;
179 TALLOC_CTX *frame = talloc_stackframe();
182 status = SMB_VFS_STREAMINFO(conn, NULL, fname, talloc_tos(),
183 &num_streams, &stream_info);
185 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
186 DEBUG(10, ("no streams around\n"));
191 if (!NT_STATUS_IS_OK(status)) {
192 DEBUG(10, ("SMB_VFS_STREAMINFO failed: %s\n",
197 DEBUG(10, ("delete_all_streams found %d streams\n",
200 if (num_streams == 0) {
205 for (i=0; i<num_streams; i++) {
209 if (strequal(stream_info[i].name, "::$DATA")) {
213 streamname = talloc_asprintf(talloc_tos(), "%s%s", fname,
214 stream_info[i].name);
216 if (streamname == NULL) {
217 DEBUG(0, ("talloc_aprintf failed\n"));
218 status = NT_STATUS_NO_MEMORY;
222 res = SMB_VFS_UNLINK(conn, streamname);
224 TALLOC_FREE(streamname);
227 status = map_nt_error_from_unix(errno);
228 DEBUG(10, ("Could not delete stream %s: %s\n",
229 streamname, strerror(errno)));
239 /****************************************************************************
240 Deal with removing a share mode on last close.
241 ****************************************************************************/
243 static NTSTATUS close_remove_share_mode(files_struct *fsp,
244 enum file_close_type close_type)
246 connection_struct *conn = fsp->conn;
247 bool delete_file = false;
248 bool changed_user = false;
249 struct share_mode_lock *lck;
250 SMB_STRUCT_STAT sbuf;
251 NTSTATUS status = NT_STATUS_OK;
256 * Lock the share entries, and determine if we should delete
257 * on close. If so delete whilst the lock is still in effect.
258 * This prevents race conditions with the file being created. JRA.
261 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
265 DEBUG(0, ("close_remove_share_mode: Could not get share mode "
266 "lock for file %s\n", fsp->fsp_name));
267 return NT_STATUS_INVALID_PARAMETER;
270 if (fsp->write_time_forced) {
271 set_close_write_time(fsp, lck->changed_write_time);
274 if (!del_share_mode(lck, fsp)) {
275 DEBUG(0, ("close_remove_share_mode: Could not delete share "
276 "entry for file %s\n", fsp->fsp_name));
279 if (fsp->initial_delete_on_close && (lck->delete_token == NULL)) {
280 bool became_user = False;
282 /* Initial delete on close was set and no one else
283 * wrote a real delete on close. */
285 if (current_user.vuid != fsp->vuid) {
286 become_user(conn, fsp->vuid);
289 set_delete_on_close_lck(lck, True, ¤t_user.ut);
295 delete_file = lck->delete_on_close;
299 /* See if others still have the file open. If this is the
300 * case, then don't delete. If all opens are POSIX delete now. */
301 for (i=0; i<lck->num_share_modes; i++) {
302 struct share_mode_entry *e = &lck->share_modes[i];
303 if (is_valid_share_mode_entry(e)) {
304 if (fsp->posix_open && (e->flags & SHARE_MODE_FLAG_POSIX_OPEN)) {
313 /* Notify any deferred opens waiting on this close. */
314 notify_deferred_opens(lck);
315 reply_to_oplock_break_requests(fsp);
318 * NT can set delete_on_close of the last open
319 * reference to a file.
322 if (!(close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE)
324 || (lck->delete_token == NULL)) {
330 * Ok, we have to delete the file
333 DEBUG(5,("close_remove_share_mode: file %s. Delete on close was set "
334 "- deleting file.\n", fsp->fsp_name));
337 * Don't try to update the write time when we delete the file
339 fsp->update_write_time_on_close = false;
341 if (!unix_token_equal(lck->delete_token, ¤t_user.ut)) {
342 /* Become the user who requested the delete. */
344 DEBUG(5,("close_remove_share_mode: file %s. "
345 "Change user to uid %u\n",
347 (unsigned int)lck->delete_token->uid));
349 if (!push_sec_ctx()) {
350 smb_panic("close_remove_share_mode: file %s. failed to push "
354 set_sec_ctx(lck->delete_token->uid,
355 lck->delete_token->gid,
356 lck->delete_token->ngroups,
357 lck->delete_token->groups,
363 /* We can only delete the file if the name we have is still valid and
364 hasn't been renamed. */
366 if (fsp->posix_open) {
367 ret = SMB_VFS_LSTAT(conn,fsp->fsp_name,&sbuf);
369 ret = SMB_VFS_STAT(conn,fsp->fsp_name,&sbuf);
373 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
374 "was set and stat failed with error %s\n",
375 fsp->fsp_name, strerror(errno) ));
377 * Don't save the errno here, we ignore this error
382 id = vfs_file_id_from_sbuf(conn, &sbuf);
384 if (!file_id_equal(&fsp->file_id, &id)) {
385 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
386 "was set and dev and/or inode does not match\n",
388 DEBUG(5,("close_remove_share_mode: file %s. stored file_id %s, "
391 file_id_string_tos(&fsp->file_id),
392 file_id_string_tos(&id)));
394 * Don't save the errno here, we ignore this error
399 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
400 && !is_ntfs_stream_name(fsp->fsp_name)) {
402 status = delete_all_streams(conn, fsp->fsp_name);
404 if (!NT_STATUS_IS_OK(status)) {
405 DEBUG(5, ("delete_all_streams failed: %s\n",
412 if (SMB_VFS_UNLINK(conn,fsp->fsp_name) != 0) {
414 * This call can potentially fail as another smbd may
415 * have had the file open with delete on close set and
416 * deleted it when its last reference to this file
417 * went away. Hence we log this but not at debug level
421 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
422 "was set and unlink failed with error %s\n",
423 fsp->fsp_name, strerror(errno) ));
425 status = map_nt_error_from_unix(errno);
428 notify_fname(conn, NOTIFY_ACTION_REMOVED,
429 FILE_NOTIFY_CHANGE_FILE_NAME,
432 /* As we now have POSIX opens which can unlink
433 * with other open files we may have taken
434 * this code path with more than one share mode
435 * entry - ensure we only delete once by resetting
436 * the delete on close flag. JRA.
439 set_delete_on_close_lck(lck, False, NULL);
452 void set_close_write_time(struct files_struct *fsp, struct timespec ts)
454 DEBUG(6,("close_write_time: %s" , time_to_asc(convert_timespec_to_time_t(ts))));
456 if (null_timespec(ts)) {
460 * if the write time on close is explict set, then don't
461 * need to fix it up to the value in the locking db
463 fsp->write_time_forced = false;
465 fsp->update_write_time_on_close = true;
466 fsp->close_write_time = ts;
469 static NTSTATUS update_write_time_on_close(struct files_struct *fsp)
471 SMB_STRUCT_STAT sbuf;
472 struct smb_file_time ft;
478 if (!fsp->update_write_time_on_close) {
482 if (null_timespec(fsp->close_write_time)) {
483 fsp->close_write_time = timespec_current();
486 /* Ensure we have a valid stat struct for the source. */
487 if (fsp->fh->fd != -1) {
488 if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
489 return map_nt_error_from_unix(errno);
492 if (SMB_VFS_STAT(fsp->conn,fsp->fsp_name,&sbuf) == -1) {
493 return map_nt_error_from_unix(errno);
497 if (!VALID_STAT(sbuf)) {
498 /* if it doesn't seem to be a real file */
502 ft.mtime = fsp->close_write_time;
503 status = smb_set_file_time(fsp->conn, fsp, fsp->fsp_name,
505 if (!NT_STATUS_IS_OK(status)) {
512 /****************************************************************************
515 close_type can be NORMAL_CLOSE=0,SHUTDOWN_CLOSE,ERROR_CLOSE.
516 printing and magic scripts are only run on normal close.
517 delete on close is done on normal and shutdown close.
518 ****************************************************************************/
520 static NTSTATUS close_normal_file(struct smb_request *req, files_struct *fsp,
521 enum file_close_type close_type)
523 NTSTATUS status = NT_STATUS_OK;
524 NTSTATUS saved_status1 = NT_STATUS_OK;
525 NTSTATUS saved_status2 = NT_STATUS_OK;
526 NTSTATUS saved_status3 = NT_STATUS_OK;
527 NTSTATUS saved_status4 = NT_STATUS_OK;
528 connection_struct *conn = fsp->conn;
530 if (fsp->aio_write_behind) {
532 * If we're finishing write behind on a close we can get a write
533 * error here, we must remember this.
535 int ret = wait_for_aio_completion(fsp);
537 saved_status1 = map_nt_error_from_unix(ret);
540 cancel_aio_by_fsp(fsp);
544 * If we're flushing on a close we can get a write
545 * error here, we must remember this.
548 saved_status2 = close_filestruct(fsp);
550 if (fsp->print_file) {
551 print_fsp_end(fsp, close_type);
556 /* If this is an old DOS or FCB open and we have multiple opens on
557 the same handle we only have one share mode. Ensure we only remove
558 the share mode on the last close. */
560 if (fsp->fh->ref_count == 1) {
561 /* Should we return on error here... ? */
562 saved_status3 = close_remove_share_mode(fsp, close_type);
565 if(fsp->oplock_type) {
566 release_file_oplock(fsp);
569 locking_close_file(smbd_messaging_context(), fsp);
571 status = fd_close(fsp);
573 /* check for magic scripts */
574 if (close_type == NORMAL_CLOSE) {
579 * Ensure pending modtime is set after close.
582 saved_status4 = update_write_time_on_close(fsp);
584 if (NT_STATUS_IS_OK(status)) {
585 if (!NT_STATUS_IS_OK(saved_status1)) {
586 status = saved_status1;
587 } else if (!NT_STATUS_IS_OK(saved_status2)) {
588 status = saved_status2;
589 } else if (!NT_STATUS_IS_OK(saved_status3)) {
590 status = saved_status3;
591 } else if (!NT_STATUS_IS_OK(saved_status4)) {
592 status = saved_status4;
596 DEBUG(2,("%s closed file %s (numopen=%d) %s\n",
597 conn->server_info->unix_name,fsp->fsp_name,
598 conn->num_files_open - 1,
599 nt_errstr(status) ));
605 /****************************************************************************
606 Close a directory opened by an NT SMB call.
607 ****************************************************************************/
609 static NTSTATUS close_directory(struct smb_request *req, files_struct *fsp,
610 enum file_close_type close_type)
612 struct share_mode_lock *lck = 0;
613 bool delete_dir = False;
614 NTSTATUS status = NT_STATUS_OK;
617 * NT can set delete_on_close of the last open
618 * reference to a directory also.
621 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
625 DEBUG(0, ("close_directory: Could not get share mode lock for %s\n", fsp->fsp_name));
626 return NT_STATUS_INVALID_PARAMETER;
629 if (!del_share_mode(lck, fsp)) {
630 DEBUG(0, ("close_directory: Could not delete share entry for %s\n", fsp->fsp_name));
633 if (fsp->initial_delete_on_close) {
634 bool became_user = False;
636 /* Initial delete on close was set - for
637 * directories we don't care if anyone else
638 * wrote a real delete on close. */
640 if (current_user.vuid != fsp->vuid) {
641 become_user(fsp->conn, fsp->vuid);
644 send_stat_cache_delete_message(fsp->fsp_name);
645 set_delete_on_close_lck(lck, True, ¤t_user.ut);
651 delete_dir = lck->delete_on_close;
655 /* See if others still have the dir open. If this is the
656 * case, then don't delete. If all opens are POSIX delete now. */
657 for (i=0; i<lck->num_share_modes; i++) {
658 struct share_mode_entry *e = &lck->share_modes[i];
659 if (is_valid_share_mode_entry(e)) {
660 if (fsp->posix_open && (e->flags & SHARE_MODE_FLAG_POSIX_OPEN)) {
669 if ((close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE) &&
673 /* Become the user who requested the delete. */
675 if (!push_sec_ctx()) {
676 smb_panic("close_directory: failed to push sec_ctx.\n");
679 set_sec_ctx(lck->delete_token->uid,
680 lck->delete_token->gid,
681 lck->delete_token->ngroups,
682 lck->delete_token->groups,
687 status = rmdir_internals(talloc_tos(),
688 fsp->conn, fsp->fsp_name);
690 DEBUG(5,("close_directory: %s. Delete on close was set - "
691 "deleting directory returned %s.\n",
692 fsp->fsp_name, nt_errstr(status)));
698 * Ensure we remove any change notify requests that would
699 * now fail as the directory has been deleted.
702 if(NT_STATUS_IS_OK(status)) {
703 remove_pending_change_notify_requests_by_fid(fsp, NT_STATUS_DELETE_PENDING);
707 remove_pending_change_notify_requests_by_fid(
711 status = fd_close(fsp);
713 if (!NT_STATUS_IS_OK(status)) {
714 DEBUG(0, ("Could not close dir! fname=%s, fd=%d, err=%d=%s\n",
715 fsp->fsp_name, fsp->fh->fd, errno, strerror(errno)));
719 * Do the code common to files and directories.
721 close_filestruct(fsp);
726 /****************************************************************************
727 Close a files_struct.
728 ****************************************************************************/
730 NTSTATUS close_file(struct smb_request *req, files_struct *fsp,
731 enum file_close_type close_type)
734 struct files_struct *base_fsp = fsp->base_fsp;
736 if(fsp->is_directory) {
737 status = close_directory(req, fsp, close_type);
738 } else if (fsp->fake_file_handle != NULL) {
739 status = close_fake_file(req, fsp);
741 status = close_normal_file(req, fsp, close_type);
744 if ((base_fsp != NULL) && (close_type != SHUTDOWN_CLOSE)) {
747 * fsp was a stream, the base fsp can't be a stream as well
749 * For SHUTDOWN_CLOSE this is not possible here, because
750 * SHUTDOWN_CLOSE only happens from files.c which walks the
751 * complete list of files. If we mess with more than one fsp
752 * those loops will become confused.
755 SMB_ASSERT(base_fsp->base_fsp == NULL);
756 close_file(req, base_fsp, close_type);
762 /****************************************************************************
763 Deal with an (authorized) message to close a file given the share mode
765 ****************************************************************************/
767 void msg_close_file(struct messaging_context *msg_ctx,
770 struct server_id server_id,
773 files_struct *fsp = NULL;
774 struct share_mode_entry e;
776 message_to_share_mode_entry(&e, (char *)data->data);
779 char *sm_str = share_mode_str(NULL, 0, &e);
781 smb_panic("talloc failed");
783 DEBUG(10,("msg_close_file: got request to close share mode "
784 "entry %s\n", sm_str));
788 fsp = file_find_dif(e.id, e.share_file_id);
790 DEBUG(10,("msg_close_file: failed to find file.\n"));
793 close_file(NULL, fsp, NORMAL_CLOSE);