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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 extern struct current_user current_user;
27 /****************************************************************************
28 Run a file if it is a magic script.
29 ****************************************************************************/
31 static void check_magic(files_struct *fsp,connection_struct *conn)
33 if (!*lp_magicscript(SNUM(conn)))
36 DEBUG(5,("checking magic for %s\n",fsp->fsp_name));
40 if (!(p = strrchr_m(fsp->fsp_name,'/')))
45 if (!strequal(lp_magicscript(SNUM(conn)),p))
56 pstrcpy(fname,fsp->fsp_name);
57 if (*lp_magicoutput(SNUM(conn)))
58 pstrcpy(magic_output,lp_magicoutput(SNUM(conn)));
60 slprintf(magic_output,sizeof(fname)-1, "%s.out",fname);
63 ret = smbrun(fname,&tmp_fd);
64 DEBUG(3,("Invoking magic command %s gave %d\n",fname,ret));
66 if (ret != 0 || tmp_fd == -1) {
71 outfd = open(magic_output, O_CREAT|O_EXCL|O_RDWR, 0600);
77 if (sys_fstat(tmp_fd,&st) == -1) {
83 transfer_file(tmp_fd,outfd,(SMB_OFF_T)st.st_size);
89 /****************************************************************************
90 Common code to close a file or a directory.
91 ****************************************************************************/
93 static NTSTATUS close_filestruct(files_struct *fsp)
95 NTSTATUS status = NT_STATUS_OK;
96 connection_struct *conn = fsp->conn;
98 if (fsp->fh->fd != -1) {
99 if(flush_write_cache(fsp, CLOSE_FLUSH) == -1) {
100 status = map_nt_error_from_unix(errno);
102 delete_write_cache(fsp);
105 conn->num_files_open--;
106 SAFE_FREE(fsp->wbmpx_ptr);
110 /****************************************************************************
111 If any deferred opens are waiting on this close, notify them.
112 ****************************************************************************/
114 static void notify_deferred_opens(struct share_mode_lock *lck)
118 for (i=0; i<lck->num_share_modes; i++) {
119 struct share_mode_entry *e = &lck->share_modes[i];
121 if (!is_deferred_open_entry(e)) {
125 if (procid_is_me(&e->pid)) {
127 * We need to notify ourself to retry the open. Do
128 * this by finding the queued SMB record, moving it to
129 * the head of the queue and changing the wait time to
132 schedule_deferred_open_smb_message(e->op_mid);
134 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
136 share_mode_entry_to_message(msg, e);
138 message_send_pid(e->pid, MSG_SMB_OPEN_RETRY,
139 msg, MSG_SMB_SHARE_MODE_ENTRY_SIZE, True);
144 /****************************************************************************
145 Deal with removing a share mode on last close.
146 ****************************************************************************/
148 static NTSTATUS close_remove_share_mode(files_struct *fsp,
149 enum file_close_type close_type)
151 connection_struct *conn = fsp->conn;
152 BOOL delete_file = False;
153 struct share_mode_lock *lck;
154 SMB_STRUCT_STAT sbuf;
155 NTSTATUS status = NT_STATUS_OK;
158 * Lock the share entries, and determine if we should delete
159 * on close. If so delete whilst the lock is still in effect.
160 * This prevents race conditions with the file being created. JRA.
163 lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL, NULL);
166 DEBUG(0, ("close_remove_share_mode: Could not get share mode "
167 "lock for file %s\n", fsp->fsp_name));
168 return NT_STATUS_INVALID_PARAMETER;
171 if (!del_share_mode(lck, fsp)) {
172 DEBUG(0, ("close_remove_share_mode: Could not delete share "
173 "entry for file %s\n", fsp->fsp_name));
176 if (fsp->initial_delete_on_close && (lck->delete_token == NULL)) {
177 BOOL became_user = False;
179 /* Initial delete on close was set and no one else
180 * wrote a real delete on close. */
182 if (current_user.vuid != fsp->vuid) {
183 become_user(conn, fsp->vuid);
186 set_delete_on_close_lck(lck, True, ¤t_user.ut);
192 delete_file = lck->delete_on_close;
196 /* See if others still have the file open. If this is the
197 * case, then don't delete. If all opens are POSIX delete now. */
198 for (i=0; i<lck->num_share_modes; i++) {
199 struct share_mode_entry *e = &lck->share_modes[i];
200 if (is_valid_share_mode_entry(e)) {
201 if (fsp->posix_open && (e->flags & SHARE_MODE_FLAG_POSIX_OPEN)) {
210 /* Notify any deferred opens waiting on this close. */
211 notify_deferred_opens(lck);
212 reply_to_oplock_break_requests(fsp);
215 * NT can set delete_on_close of the last open
216 * reference to a file.
219 if (!(close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE)
221 || (lck->delete_token == NULL)) {
227 * Ok, we have to delete the file
230 DEBUG(5,("close_remove_share_mode: file %s. Delete on close was set "
231 "- deleting file.\n", fsp->fsp_name));
233 /* Become the user who requested the delete. */
235 if (!push_sec_ctx()) {
236 smb_panic("close_remove_share_mode: file %s. failed to push "
240 set_sec_ctx(lck->delete_token->uid,
241 lck->delete_token->gid,
242 lck->delete_token->ngroups,
243 lck->delete_token->groups,
246 /* We can only delete the file if the name we have is still valid and
247 hasn't been renamed. */
249 if(SMB_VFS_STAT(conn,fsp->fsp_name,&sbuf) != 0) {
250 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
251 "was set and stat failed with error %s\n",
252 fsp->fsp_name, strerror(errno) ));
254 * Don't save the errno here, we ignore this error
259 if(sbuf.st_dev != fsp->dev || sbuf.st_ino != fsp->inode) {
260 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
261 "was set and dev and/or inode does not match\n",
263 DEBUG(5,("close_remove_share_mode: file %s. stored dev = %x, "
264 "inode = %.0f stat dev = %x, inode = %.0f\n",
266 (unsigned int)fsp->dev, (double)fsp->inode,
267 (unsigned int)sbuf.st_dev, (double)sbuf.st_ino ));
269 * Don't save the errno here, we ignore this error
274 if (SMB_VFS_UNLINK(conn,fsp->fsp_name) != 0) {
276 * This call can potentially fail as another smbd may
277 * have had the file open with delete on close set and
278 * deleted it when its last reference to this file
279 * went away. Hence we log this but not at debug level
283 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
284 "was set and unlink failed with error %s\n",
285 fsp->fsp_name, strerror(errno) ));
287 status = map_nt_error_from_unix(errno);
290 /* As we now have POSIX opens which can unlink
291 * with other open files we may have taken
292 * this code path with more than one share mode
293 * entry - ensure we only delete once by resetting
294 * the delete on close flag. JRA.
297 set_delete_on_close_lck(lck, False, NULL);
308 /****************************************************************************
311 close_type can be NORMAL_CLOSE=0,SHUTDOWN_CLOSE,ERROR_CLOSE.
312 printing and magic scripts are only run on normal close.
313 delete on close is done on normal and shutdown close.
314 ****************************************************************************/
316 static NTSTATUS close_normal_file(files_struct *fsp, enum file_close_type close_type)
318 NTSTATUS status = NT_STATUS_OK;
319 NTSTATUS saved_status1 = NT_STATUS_OK;
320 NTSTATUS saved_status2 = NT_STATUS_OK;
321 connection_struct *conn = fsp->conn;
323 cancel_aio_by_fsp(fsp);
326 * If we're flushing on a close we can get a write
327 * error here, we must remember this.
330 saved_status1 = close_filestruct(fsp);
332 if (fsp->print_file) {
333 print_fsp_end(fsp, close_type);
338 /* If this is an old DOS or FCB open and we have multiple opens on
339 the same handle we only have one share mode. Ensure we only remove
340 the share mode on the last close. */
342 if (fsp->fh->ref_count == 1) {
343 /* Should we return on error here... ? */
344 saved_status2 = close_remove_share_mode(fsp, close_type);
347 if(fsp->oplock_type) {
348 release_file_oplock(fsp);
351 locking_close_file(fsp);
353 status = fd_close(conn, fsp);
355 /* check for magic scripts */
356 if (close_type == NORMAL_CLOSE) {
357 check_magic(fsp,conn);
361 * Ensure pending modtime is set after close.
364 if (fsp->pending_modtime_owner && !null_timespec(fsp->pending_modtime)) {
365 set_filetime(conn, fsp->fsp_name, fsp->pending_modtime);
366 } else if (!null_timespec(fsp->last_write_time)) {
367 set_filetime(conn, fsp->fsp_name, fsp->last_write_time);
370 if (NT_STATUS_IS_OK(status)) {
371 if (!NT_STATUS_IS_OK(saved_status1)) {
372 status = saved_status1;
373 } else if (!NT_STATUS_IS_OK(saved_status2)) {
374 status = saved_status2;
378 DEBUG(2,("%s closed file %s (numopen=%d) %s\n",
379 conn->user,fsp->fsp_name,
380 conn->num_files_open,
381 nt_errstr(status) ));
387 /****************************************************************************
388 Close a directory opened by an NT SMB call.
389 ****************************************************************************/
391 static NTSTATUS close_directory(files_struct *fsp, enum file_close_type close_type)
393 struct share_mode_lock *lck = 0;
394 BOOL delete_dir = False;
395 NTSTATUS status = NT_STATUS_OK;
398 * NT can set delete_on_close of the last open
399 * reference to a directory also.
402 lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL, NULL);
405 DEBUG(0, ("close_directory: Could not get share mode lock for %s\n", fsp->fsp_name));
406 return NT_STATUS_INVALID_PARAMETER;
409 if (!del_share_mode(lck, fsp)) {
410 DEBUG(0, ("close_directory: Could not delete share entry for %s\n", fsp->fsp_name));
413 if (fsp->initial_delete_on_close) {
414 BOOL became_user = False;
416 /* Initial delete on close was set - for
417 * directories we don't care if anyone else
418 * wrote a real delete on close. */
420 if (current_user.vuid != fsp->vuid) {
421 become_user(fsp->conn, fsp->vuid);
424 send_stat_cache_delete_message(fsp->fsp_name);
425 set_delete_on_close_lck(lck, True, ¤t_user.ut);
431 delete_dir = lck->delete_on_close;
435 /* See if others still have the dir open. If this is the
436 * case, then don't delete. If all opens are POSIX delete now. */
437 for (i=0; i<lck->num_share_modes; i++) {
438 struct share_mode_entry *e = &lck->share_modes[i];
439 if (is_valid_share_mode_entry(e)) {
440 if (fsp->posix_open && (e->flags & SHARE_MODE_FLAG_POSIX_OPEN)) {
449 if ((close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE) &&
453 /* Become the user who requested the delete. */
455 if (!push_sec_ctx()) {
456 smb_panic("close_directory: failed to push sec_ctx.\n");
459 set_sec_ctx(lck->delete_token->uid,
460 lck->delete_token->gid,
461 lck->delete_token->ngroups,
462 lck->delete_token->groups,
467 status = rmdir_internals(fsp->conn, fsp->fsp_name);
469 DEBUG(5,("close_directory: %s. Delete on close was set - "
470 "deleting directory returned %s.\n",
471 fsp->fsp_name, nt_errstr(status)));
477 * Ensure we remove any change notify requests that would
478 * now fail as the directory has been deleted.
481 if(NT_STATUS_IS_OK(status)) {
482 remove_pending_change_notify_requests_by_fid(fsp, NT_STATUS_DELETE_PENDING);
486 remove_pending_change_notify_requests_by_fid(
491 * Do the code common to files and directories.
493 close_filestruct(fsp);
498 /****************************************************************************
499 Close a 'stat file' opened internally.
500 ****************************************************************************/
502 NTSTATUS close_stat(files_struct *fsp)
505 * Do the code common to files and directories.
507 close_filestruct(fsp);
512 /****************************************************************************
513 Close a files_struct.
514 ****************************************************************************/
516 NTSTATUS close_file(files_struct *fsp, enum file_close_type close_type)
518 if(fsp->is_directory) {
519 return close_directory(fsp, close_type);
520 } else if (fsp->is_stat) {
521 return close_stat(fsp);
522 } else if (fsp->fake_file_handle != NULL) {
523 return close_fake_file(fsp);
525 return close_normal_file(fsp, close_type);