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 chmod(fsp
->fsp_name
,0755);
71 ret
= smbrun(fsp
->fsp_name
,&tmp_fd
);
72 DEBUG(3,("Invoking magic command %s gave %d\n",
75 unlink(fsp
->fsp_name
);
76 if (ret
!= 0 || tmp_fd
== -1) {
83 outfd
= open(magic_output
, O_CREAT
|O_EXCL
|O_RDWR
, 0600);
90 if (sys_fstat(tmp_fd
,&st
) == -1) {
96 transfer_file(tmp_fd
,outfd
,(SMB_OFF_T
)st
.st_size
);
102 /****************************************************************************
103 Common code to close a file or a directory.
104 ****************************************************************************/
106 static NTSTATUS
close_filestruct(files_struct
*fsp
)
108 NTSTATUS status
= NT_STATUS_OK
;
109 connection_struct
*conn
= fsp
->conn
;
111 if (fsp
->fh
->fd
!= -1) {
112 if(flush_write_cache(fsp
, CLOSE_FLUSH
) == -1) {
113 status
= map_nt_error_from_unix(errno
);
115 delete_write_cache(fsp
);
118 conn
->num_files_open
--;
122 /****************************************************************************
123 If any deferred opens are waiting on this close, notify them.
124 ****************************************************************************/
126 static void notify_deferred_opens(struct share_mode_lock
*lck
)
130 for (i
=0; i
<lck
->num_share_modes
; i
++) {
131 struct share_mode_entry
*e
= &lck
->share_modes
[i
];
133 if (!is_deferred_open_entry(e
)) {
137 if (procid_is_me(&e
->pid
)) {
139 * We need to notify ourself to retry the open. Do
140 * this by finding the queued SMB record, moving it to
141 * the head of the queue and changing the wait time to
144 schedule_deferred_open_smb_message(e
->op_mid
);
146 char msg
[MSG_SMB_SHARE_MODE_ENTRY_SIZE
];
148 share_mode_entry_to_message(msg
, e
);
150 messaging_send_buf(smbd_messaging_context(),
151 e
->pid
, MSG_SMB_OPEN_RETRY
,
153 MSG_SMB_SHARE_MODE_ENTRY_SIZE
);
158 /****************************************************************************
160 ****************************************************************************/
162 static NTSTATUS
delete_all_streams(connection_struct
*conn
, const char *fname
)
164 struct stream_struct
*stream_info
;
166 unsigned int num_streams
;
167 TALLOC_CTX
*frame
= talloc_stackframe();
170 status
= SMB_VFS_STREAMINFO(conn
, NULL
, fname
, talloc_tos(),
171 &num_streams
, &stream_info
);
173 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_IMPLEMENTED
)) {
174 DEBUG(10, ("no streams around\n"));
179 if (!NT_STATUS_IS_OK(status
)) {
180 DEBUG(10, ("SMB_VFS_STREAMINFO failed: %s\n",
185 DEBUG(10, ("delete_all_streams found %d streams\n",
188 if (num_streams
== 0) {
193 for (i
=0; i
<num_streams
; i
++) {
197 if (strequal(stream_info
[i
].name
, "::$DATA")) {
201 streamname
= talloc_asprintf(talloc_tos(), "%s%s", fname
,
202 stream_info
[i
].name
);
204 if (streamname
== NULL
) {
205 DEBUG(0, ("talloc_aprintf failed\n"));
206 status
= NT_STATUS_NO_MEMORY
;
210 res
= SMB_VFS_UNLINK(conn
, streamname
);
212 TALLOC_FREE(streamname
);
215 status
= map_nt_error_from_unix(errno
);
216 DEBUG(10, ("Could not delete stream %s: %s\n",
217 streamname
, strerror(errno
)));
227 /****************************************************************************
228 Deal with removing a share mode on last close.
229 ****************************************************************************/
231 static NTSTATUS
close_remove_share_mode(files_struct
*fsp
,
232 enum file_close_type close_type
)
234 connection_struct
*conn
= fsp
->conn
;
235 bool delete_file
= false;
236 bool changed_user
= false;
237 struct share_mode_lock
*lck
;
238 SMB_STRUCT_STAT sbuf
;
239 NTSTATUS status
= NT_STATUS_OK
;
244 * Lock the share entries, and determine if we should delete
245 * on close. If so delete whilst the lock is still in effect.
246 * This prevents race conditions with the file being created. JRA.
249 lck
= get_share_mode_lock(talloc_tos(), fsp
->file_id
, NULL
, NULL
,
253 DEBUG(0, ("close_remove_share_mode: Could not get share mode "
254 "lock for file %s\n", fsp
->fsp_name
));
255 return NT_STATUS_INVALID_PARAMETER
;
258 if (!del_share_mode(lck
, fsp
)) {
259 DEBUG(0, ("close_remove_share_mode: Could not delete share "
260 "entry for file %s\n", fsp
->fsp_name
));
263 if (fsp
->initial_delete_on_close
&& (lck
->delete_token
== NULL
)) {
264 bool became_user
= False
;
266 /* Initial delete on close was set and no one else
267 * wrote a real delete on close. */
269 if (current_user
.vuid
!= fsp
->vuid
) {
270 become_user(conn
, fsp
->vuid
);
273 set_delete_on_close_lck(lck
, True
, ¤t_user
.ut
);
279 delete_file
= lck
->delete_on_close
;
283 /* See if others still have the file open. If this is the
284 * case, then don't delete. If all opens are POSIX delete now. */
285 for (i
=0; i
<lck
->num_share_modes
; i
++) {
286 struct share_mode_entry
*e
= &lck
->share_modes
[i
];
287 if (is_valid_share_mode_entry(e
)) {
288 if (fsp
->posix_open
&& (e
->flags
& SHARE_MODE_FLAG_POSIX_OPEN
)) {
297 /* Notify any deferred opens waiting on this close. */
298 notify_deferred_opens(lck
);
299 reply_to_oplock_break_requests(fsp
);
302 * NT can set delete_on_close of the last open
303 * reference to a file.
306 if (!(close_type
== NORMAL_CLOSE
|| close_type
== SHUTDOWN_CLOSE
)
308 || (lck
->delete_token
== NULL
)) {
314 * Ok, we have to delete the file
317 DEBUG(5,("close_remove_share_mode: file %s. Delete on close was set "
318 "- deleting file.\n", fsp
->fsp_name
));
320 if (!unix_token_equal(lck
->delete_token
, ¤t_user
.ut
)) {
321 /* Become the user who requested the delete. */
323 DEBUG(5,("close_remove_share_mode: file %s. "
324 "Change user to uid %u\n",
326 (unsigned int)lck
->delete_token
->uid
));
328 if (!push_sec_ctx()) {
329 smb_panic("close_remove_share_mode: file %s. failed to push "
333 set_sec_ctx(lck
->delete_token
->uid
,
334 lck
->delete_token
->gid
,
335 lck
->delete_token
->ngroups
,
336 lck
->delete_token
->groups
,
342 /* We can only delete the file if the name we have is still valid and
343 hasn't been renamed. */
345 if (fsp
->posix_open
) {
346 ret
= SMB_VFS_LSTAT(conn
,fsp
->fsp_name
,&sbuf
);
348 ret
= SMB_VFS_STAT(conn
,fsp
->fsp_name
,&sbuf
);
352 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
353 "was set and stat failed with error %s\n",
354 fsp
->fsp_name
, strerror(errno
) ));
356 * Don't save the errno here, we ignore this error
361 id
= vfs_file_id_from_sbuf(conn
, &sbuf
);
363 if (!file_id_equal(&fsp
->file_id
, &id
)) {
364 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
365 "was set and dev and/or inode does not match\n",
367 DEBUG(5,("close_remove_share_mode: file %s. stored file_id %s, "
370 file_id_string_tos(&fsp
->file_id
),
371 file_id_string_tos(&id
)));
373 * Don't save the errno here, we ignore this error
378 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
)
379 && !is_ntfs_stream_name(fsp
->fsp_name
)) {
381 status
= delete_all_streams(conn
, fsp
->fsp_name
);
383 if (!NT_STATUS_IS_OK(status
)) {
384 DEBUG(5, ("delete_all_streams failed: %s\n",
391 if (SMB_VFS_UNLINK(conn
,fsp
->fsp_name
) != 0) {
393 * This call can potentially fail as another smbd may
394 * have had the file open with delete on close set and
395 * deleted it when its last reference to this file
396 * went away. Hence we log this but not at debug level
400 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
401 "was set and unlink failed with error %s\n",
402 fsp
->fsp_name
, strerror(errno
) ));
404 status
= map_nt_error_from_unix(errno
);
407 notify_fname(conn
, NOTIFY_ACTION_REMOVED
,
408 FILE_NOTIFY_CHANGE_FILE_NAME
,
411 /* As we now have POSIX opens which can unlink
412 * with other open files we may have taken
413 * this code path with more than one share mode
414 * entry - ensure we only delete once by resetting
415 * the delete on close flag. JRA.
418 set_delete_on_close_lck(lck
, False
, NULL
);
431 /****************************************************************************
434 close_type can be NORMAL_CLOSE=0,SHUTDOWN_CLOSE,ERROR_CLOSE.
435 printing and magic scripts are only run on normal close.
436 delete on close is done on normal and shutdown close.
437 ****************************************************************************/
439 static NTSTATUS
close_normal_file(files_struct
*fsp
, enum file_close_type close_type
)
441 NTSTATUS status
= NT_STATUS_OK
;
442 NTSTATUS saved_status1
= NT_STATUS_OK
;
443 NTSTATUS saved_status2
= NT_STATUS_OK
;
444 NTSTATUS saved_status3
= NT_STATUS_OK
;
445 connection_struct
*conn
= fsp
->conn
;
447 if (fsp
->aio_write_behind
) {
449 * If we're finishing write behind on a close we can get a write
450 * error here, we must remember this.
452 int ret
= wait_for_aio_completion(fsp
);
454 saved_status1
= map_nt_error_from_unix(ret
);
457 cancel_aio_by_fsp(fsp
);
461 * If we're flushing on a close we can get a write
462 * error here, we must remember this.
465 saved_status2
= close_filestruct(fsp
);
467 if (fsp
->print_file
) {
468 print_fsp_end(fsp
, close_type
);
473 /* If this is an old DOS or FCB open and we have multiple opens on
474 the same handle we only have one share mode. Ensure we only remove
475 the share mode on the last close. */
477 if (fsp
->fh
->ref_count
== 1) {
478 /* Should we return on error here... ? */
479 saved_status3
= close_remove_share_mode(fsp
, close_type
);
482 if(fsp
->oplock_type
) {
483 release_file_oplock(fsp
);
486 locking_close_file(smbd_messaging_context(), fsp
);
488 status
= fd_close(fsp
);
490 /* check for magic scripts */
491 if (close_type
== NORMAL_CLOSE
) {
496 * Ensure pending modtime is set after close.
499 if (fsp
->pending_modtime_owner
&& !null_timespec(fsp
->pending_modtime
)) {
500 set_filetime(conn
, fsp
->fsp_name
, fsp
->pending_modtime
);
501 } else if (!null_timespec(fsp
->last_write_time
)) {
502 set_filetime(conn
, fsp
->fsp_name
, fsp
->last_write_time
);
505 if (NT_STATUS_IS_OK(status
)) {
506 if (!NT_STATUS_IS_OK(saved_status1
)) {
507 status
= saved_status1
;
508 } else if (!NT_STATUS_IS_OK(saved_status2
)) {
509 status
= saved_status2
;
510 } else if (!NT_STATUS_IS_OK(saved_status3
)) {
511 status
= saved_status3
;
515 DEBUG(2,("%s closed file %s (numopen=%d) %s\n",
516 conn
->user
,fsp
->fsp_name
,
517 conn
->num_files_open
,
518 nt_errstr(status
) ));
524 /****************************************************************************
525 Close a directory opened by an NT SMB call.
526 ****************************************************************************/
528 static NTSTATUS
close_directory(files_struct
*fsp
, enum file_close_type close_type
)
530 struct share_mode_lock
*lck
= 0;
531 bool delete_dir
= False
;
532 NTSTATUS status
= NT_STATUS_OK
;
535 * NT can set delete_on_close of the last open
536 * reference to a directory also.
539 lck
= get_share_mode_lock(talloc_tos(), fsp
->file_id
, NULL
, NULL
,
543 DEBUG(0, ("close_directory: Could not get share mode lock for %s\n", fsp
->fsp_name
));
544 return NT_STATUS_INVALID_PARAMETER
;
547 if (!del_share_mode(lck
, fsp
)) {
548 DEBUG(0, ("close_directory: Could not delete share entry for %s\n", fsp
->fsp_name
));
551 if (fsp
->initial_delete_on_close
) {
552 bool became_user
= False
;
554 /* Initial delete on close was set - for
555 * directories we don't care if anyone else
556 * wrote a real delete on close. */
558 if (current_user
.vuid
!= fsp
->vuid
) {
559 become_user(fsp
->conn
, fsp
->vuid
);
562 send_stat_cache_delete_message(fsp
->fsp_name
);
563 set_delete_on_close_lck(lck
, True
, ¤t_user
.ut
);
569 delete_dir
= lck
->delete_on_close
;
573 /* See if others still have the dir open. If this is the
574 * case, then don't delete. If all opens are POSIX delete now. */
575 for (i
=0; i
<lck
->num_share_modes
; i
++) {
576 struct share_mode_entry
*e
= &lck
->share_modes
[i
];
577 if (is_valid_share_mode_entry(e
)) {
578 if (fsp
->posix_open
&& (e
->flags
& SHARE_MODE_FLAG_POSIX_OPEN
)) {
587 if ((close_type
== NORMAL_CLOSE
|| close_type
== SHUTDOWN_CLOSE
) &&
591 /* Become the user who requested the delete. */
593 if (!push_sec_ctx()) {
594 smb_panic("close_directory: failed to push sec_ctx.\n");
597 set_sec_ctx(lck
->delete_token
->uid
,
598 lck
->delete_token
->gid
,
599 lck
->delete_token
->ngroups
,
600 lck
->delete_token
->groups
,
605 status
= rmdir_internals(talloc_tos(),
606 fsp
->conn
, fsp
->fsp_name
);
608 DEBUG(5,("close_directory: %s. Delete on close was set - "
609 "deleting directory returned %s.\n",
610 fsp
->fsp_name
, nt_errstr(status
)));
616 * Ensure we remove any change notify requests that would
617 * now fail as the directory has been deleted.
620 if(NT_STATUS_IS_OK(status
)) {
621 remove_pending_change_notify_requests_by_fid(fsp
, NT_STATUS_DELETE_PENDING
);
625 remove_pending_change_notify_requests_by_fid(
630 * Do the code common to files and directories.
632 close_filestruct(fsp
);
637 /****************************************************************************
638 Close a 'stat file' opened internally.
639 ****************************************************************************/
641 static NTSTATUS
close_stat(files_struct
*fsp
)
644 * Do the code common to files and directories.
646 close_filestruct(fsp
);
651 /****************************************************************************
652 Close a files_struct.
653 ****************************************************************************/
655 NTSTATUS
close_file(files_struct
*fsp
, enum file_close_type close_type
)
658 struct files_struct
*base_fsp
= fsp
->base_fsp
;
660 if(fsp
->is_directory
) {
661 status
= close_directory(fsp
, close_type
);
662 } else if (fsp
->is_stat
) {
663 status
= close_stat(fsp
);
664 } else if (fsp
->fake_file_handle
!= NULL
) {
665 status
= close_fake_file(fsp
);
667 status
= close_normal_file(fsp
, close_type
);
670 if ((base_fsp
!= NULL
) && (close_type
!= SHUTDOWN_CLOSE
)) {
673 * fsp was a stream, the base fsp can't be a stream as well
675 * For SHUTDOWN_CLOSE this is not possible here, because
676 * SHUTDOWN_CLOSE only happens from files.c which walks the
677 * complete list of files. If we mess with more than one fsp
678 * those loops will become confused.
681 SMB_ASSERT(base_fsp
->base_fsp
== NULL
);
682 close_file(base_fsp
, close_type
);