r17098: Samba3 now cleanly passes Samba4 RAW-LOCK torture
[samba.git] / source / smbd / close.c
1 /* 
2    Unix SMB/CIFS implementation.
3    file closing
4    Copyright (C) Andrew Tridgell 1992-1998
5    Copyright (C) Jeremy Allison 1992-2004.
6    Copyright (C) Volker Lendecke 2005
7    
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.
12    
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.
17    
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.
21 */
22
23 #include "includes.h"
24
25 /****************************************************************************
26  Run a file if it is a magic script.
27 ****************************************************************************/
28
29 static void check_magic(files_struct *fsp,connection_struct *conn)
30 {
31         if (!*lp_magicscript(SNUM(conn)))
32                 return;
33
34         DEBUG(5,("checking magic for %s\n",fsp->fsp_name));
35
36         {
37                 char *p;
38                 if (!(p = strrchr_m(fsp->fsp_name,'/')))
39                         p = fsp->fsp_name;
40                 else
41                         p++;
42
43                 if (!strequal(lp_magicscript(SNUM(conn)),p))
44                         return;
45         }
46
47         {
48                 int ret;
49                 pstring magic_output;
50                 pstring fname;
51                 SMB_STRUCT_STAT st;
52                 int tmp_fd, outfd;
53
54                 pstrcpy(fname,fsp->fsp_name);
55                 if (*lp_magicoutput(SNUM(conn)))
56                         pstrcpy(magic_output,lp_magicoutput(SNUM(conn)));
57                 else
58                         slprintf(magic_output,sizeof(fname)-1, "%s.out",fname);
59
60                 chmod(fname,0755);
61                 ret = smbrun(fname,&tmp_fd);
62                 DEBUG(3,("Invoking magic command %s gave %d\n",fname,ret));
63                 unlink(fname);
64                 if (ret != 0 || tmp_fd == -1) {
65                         if (tmp_fd != -1)
66                                 close(tmp_fd);
67                         return;
68                 }
69                 outfd = open(magic_output, O_CREAT|O_EXCL|O_RDWR, 0600);
70                 if (outfd == -1) {
71                         close(tmp_fd);
72                         return;
73                 }
74
75                 if (sys_fstat(tmp_fd,&st) == -1) {
76                         close(tmp_fd);
77                         close(outfd);
78                         return;
79                 }
80
81                 transfer_file(tmp_fd,outfd,(SMB_OFF_T)st.st_size);
82                 close(tmp_fd);
83                 close(outfd);
84         }
85 }
86
87 /****************************************************************************
88   Common code to close a file or a directory.
89 ****************************************************************************/
90
91 static int close_filestruct(files_struct *fsp)
92 {   
93         connection_struct *conn = fsp->conn;
94         int ret = 0;
95     
96         if (fsp->fh->fd != -1) {
97                 if(flush_write_cache(fsp, CLOSE_FLUSH) == -1)
98                         ret = -1;
99
100                 delete_write_cache(fsp);
101         }
102
103         conn->num_files_open--;
104         SAFE_FREE(fsp->wbmpx_ptr);
105
106         return ret;
107 }    
108
109 /****************************************************************************
110  If any deferred opens are waiting on this close, notify them.
111 ****************************************************************************/
112
113 static void notify_deferred_opens(struct share_mode_lock *lck)
114 {
115         int i;
116  
117         for (i=0; i<lck->num_share_modes; i++) {
118                 struct share_mode_entry *e = &lck->share_modes[i];
119  
120                 if (!is_deferred_open_entry(e)) {
121                         continue;
122                 }
123  
124                 if (procid_is_me(&e->pid)) {
125                         /*
126                          * We need to notify ourself to retry the open.  Do
127                          * this by finding the queued SMB record, moving it to
128                          * the head of the queue and changing the wait time to
129                          * zero.
130                          */
131                         schedule_deferred_open_smb_message(e->op_mid);
132                 } else {
133                         char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
134
135                         share_mode_entry_to_message(msg, e);
136
137                         become_root();
138                         message_send_pid(e->pid, MSG_SMB_OPEN_RETRY,
139                                          msg, MSG_SMB_SHARE_MODE_ENTRY_SIZE, True);
140                         unbecome_root();
141                 }
142         }
143 }
144
145 /****************************************************************************
146  Deal with removing a share mode on last close.
147 ****************************************************************************/
148
149 static int close_remove_share_mode(files_struct *fsp, enum file_close_type close_type)
150 {
151         connection_struct *conn = fsp->conn;
152         BOOL delete_file = False;
153         struct share_mode_lock *lck;
154
155         /*
156          * Lock the share entries, and determine if we should delete
157          * on close. If so delete whilst the lock is still in effect.
158          * This prevents race conditions with the file being created. JRA.
159          */
160
161         lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL, NULL);
162
163         if (lck == NULL) {
164                 DEBUG(0, ("close_remove_share_mode: Could not get share mode lock for file %s\n", fsp->fsp_name));
165                 return EINVAL;
166         }
167
168         if (!del_share_mode(lck, fsp)) {
169                 DEBUG(0, ("close_remove_share_mode: Could not delete share entry for file %s\n", fsp->fsp_name));
170         }
171
172         delete_file = (lck->delete_on_close | lck->initial_delete_on_close);
173
174         if (delete_file) {
175                 int i;
176                 /* See if others still have the file open. If this is the
177                  * case, then don't delete */
178                 for (i=0; i<lck->num_share_modes; i++) {
179                         if (is_valid_share_mode_entry(&lck->share_modes[i])) {
180                                 delete_file = False;
181                                 break;
182                         }
183                 }
184         }
185
186         /* Notify any deferred opens waiting on this close. */
187         notify_deferred_opens(lck);
188         reply_to_oplock_break_requests(fsp);
189
190         /*
191          * NT can set delete_on_close of the last open
192          * reference to a file.
193          */
194
195         if ((close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE) &&
196                                 delete_file &&
197                                 lck->delete_token) {
198                 SMB_STRUCT_STAT sbuf;
199
200                 DEBUG(5,("close_remove_share_mode: file %s. Delete on close was set - deleting file.\n",
201                         fsp->fsp_name));
202
203                 /* Become the user who requested the delete. */
204
205                 if (!push_sec_ctx()) {
206                         smb_panic("close_remove_share_mode: file %s. failed to push sec_ctx.\n");
207                 }
208
209                 set_sec_ctx(lck->delete_token->uid,
210                                 lck->delete_token->gid,
211                                 lck->delete_token->ngroups,
212                                 lck->delete_token->groups,
213                                 NULL);
214
215                 /* We can only delete the file if the name we have
216                    is still valid and hasn't been renamed. */
217         
218                 if(SMB_VFS_STAT(conn,fsp->fsp_name,&sbuf) != 0) {
219                         DEBUG(5,("close_remove_share_mode: file %s. Delete on close was set "
220                                 "and stat failed with error %s\n",
221                                 fsp->fsp_name, strerror(errno) ));
222                 } else {
223                         if(sbuf.st_dev != fsp->dev || sbuf.st_ino != fsp->inode) {
224                                 DEBUG(5,("close_remove_share_mode: file %s. Delete on close was set and "
225                                         "dev and/or inode does not match\n",
226                                         fsp->fsp_name ));
227                                 DEBUG(5,("close_remove_share_mode: file %s. stored dev = %x, inode = %.0f "
228                                         "stat dev = %x, inode = %.0f\n",
229                                         fsp->fsp_name,
230                                         (unsigned int)fsp->dev, (double)fsp->inode,
231                                         (unsigned int)sbuf.st_dev, (double)sbuf.st_ino ));
232
233                         } else if(SMB_VFS_UNLINK(conn,fsp->fsp_name) != 0) {
234                                 /*
235                                  * This call can potentially fail as another smbd may have
236                                  * had the file open with delete on close set and deleted
237                                  * it when its last reference to this file went away. Hence
238                                  * we log this but not at debug level zero.
239                                  */
240
241                                 DEBUG(5,("close_remove_share_mode: file %s. Delete on close was set "
242                                         "and unlink failed with error %s\n",
243                                         fsp->fsp_name, strerror(errno) ));
244                         }
245                 }
246                 /* unbecome user. */
247                 pop_sec_ctx();
248         
249                 process_pending_change_notify_queue((time_t)0);
250         }
251
252         TALLOC_FREE(lck);
253         return 0;
254 }
255
256 /****************************************************************************
257  Close a file.
258
259  close_type can be NORMAL_CLOSE=0,SHUTDOWN_CLOSE,ERROR_CLOSE.
260  printing and magic scripts are only run on normal close.
261  delete on close is done on normal and shutdown close.
262 ****************************************************************************/
263
264 static int close_normal_file(files_struct *fsp, enum file_close_type close_type)
265 {
266         connection_struct *conn = fsp->conn;
267         int saved_errno = 0;
268         int err = 0;
269         int err1 = 0;
270
271         if (fsp->aio_write_behind) {
272                 /*
273                  * If we're finishing write behind on a close we can get a write
274                  * error here, we must remember this.
275                  */
276                 int ret = wait_for_aio_completion(fsp);
277                 if (ret) {
278                         saved_errno = ret;
279                         err1 = -1;
280                 }
281         } else {
282                 cancel_aio_by_fsp(fsp);
283         }
284  
285         /*
286          * If we're flushing on a close we can get a write
287          * error here, we must remember this.
288          */
289
290         if (close_filestruct(fsp) == -1) {
291                 saved_errno = errno;
292                 err1 = -1;
293         }
294
295         if (fsp->print_file) {
296                 print_fsp_end(fsp, close_type);
297                 file_free(fsp);
298                 return 0;
299         }
300
301         /* If this is an old DOS or FCB open and we have multiple opens on
302            the same handle we only have one share mode. Ensure we only remove
303            the share mode on the last close. */
304
305         if (fsp->fh->ref_count == 1) {
306                 /* Should we return on error here... ? */
307                 close_remove_share_mode(fsp, close_type);
308         }
309
310         if(fsp->oplock_type) {
311                 release_file_oplock(fsp);
312         }
313
314         locking_close_file(fsp);
315
316         err = fd_close(conn, fsp);
317
318         /* Only save errno if fd_close failed and we don't already
319            have an errno saved from a flush call. */
320         if ((err1 != -1) && (err == -1)) {
321                 saved_errno = errno;
322         }
323
324         /* check for magic scripts */
325         if (close_type == NORMAL_CLOSE) {
326                 check_magic(fsp,conn);
327         }
328
329         /*
330          * Ensure pending modtime is set after close.
331          */
332
333         if(fsp->pending_modtime && fsp->pending_modtime_owner) {
334                 set_filetime(conn, fsp->fsp_name, fsp->pending_modtime);
335         } else if (fsp->last_write_time) {
336                 set_filetime(conn, fsp->fsp_name, fsp->last_write_time);
337         }
338
339         DEBUG(2,("%s closed file %s (numopen=%d) %s\n",
340                 conn->user,fsp->fsp_name,
341                 conn->num_files_open,
342                 (err == -1 || err1 == -1) ? strerror(saved_errno) : ""));
343
344         file_free(fsp);
345
346         if (err == -1 || err1 == -1) {
347                 errno = saved_errno;
348                 return saved_errno;
349         } else {
350                 return 0;
351         }
352 }
353
354 /****************************************************************************
355  Close a directory opened by an NT SMB call. 
356 ****************************************************************************/
357   
358 static int close_directory(files_struct *fsp, enum file_close_type close_type)
359 {
360         struct share_mode_lock *lck = 0;
361         BOOL delete_dir = False;
362
363         /*
364          * NT can set delete_on_close of the last open
365          * reference to a directory also.
366          */
367
368         lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL, NULL);
369
370         if (lck == NULL) {
371                 DEBUG(0, ("close_directory: Could not get share mode lock for %s\n", fsp->fsp_name));
372                 return EINVAL;
373         }
374
375         if (!del_share_mode(lck, fsp)) {
376                 DEBUG(0, ("close_directory: Could not delete share entry for %s\n", fsp->fsp_name));
377         }
378
379         delete_dir = (lck->delete_on_close | lck->initial_delete_on_close);
380
381         if ((close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE) &&
382                                 delete_dir &&
383                                 lck->delete_token) {
384                 BOOL ok;
385         
386                 /* Become the user who requested the delete. */
387
388                 if (!push_sec_ctx()) {
389                         smb_panic("close_directory: failed to push sec_ctx.\n");
390                 }
391
392                 set_sec_ctx(lck->delete_token->uid,
393                                 lck->delete_token->gid,
394                                 lck->delete_token->ngroups,
395                                 lck->delete_token->groups,
396                                 NULL);
397
398                 TALLOC_FREE(lck);
399
400                 ok = rmdir_internals(fsp->conn, fsp->fsp_name);
401
402                 DEBUG(5,("close_directory: %s. Delete on close was set - deleting directory %s.\n",
403                         fsp->fsp_name, ok ? "succeeded" : "failed" ));
404
405                 /* unbecome user. */
406                 pop_sec_ctx();
407
408                 /*
409                  * Ensure we remove any change notify requests that would
410                  * now fail as the directory has been deleted.
411                  */
412
413                 if(ok) {
414                         remove_pending_change_notify_requests_by_fid(fsp, NT_STATUS_DELETE_PENDING);
415                         remove_pending_change_notify_requests_by_filename(fsp, NT_STATUS_DELETE_PENDING);
416
417                 }
418                 process_pending_change_notify_queue((time_t)0);
419         } else {
420                 TALLOC_FREE(lck);
421                 remove_pending_change_notify_requests_by_fid(fsp, NT_STATUS_CANCELLED);
422         }
423
424         /*
425          * Do the code common to files and directories.
426          */
427         close_filestruct(fsp);
428         file_free(fsp);
429         return 0;
430 }
431
432 /****************************************************************************
433  Close a 'stat file' opened internally.
434 ****************************************************************************/
435   
436 static int close_stat(files_struct *fsp)
437 {
438         /*
439          * Do the code common to files and directories.
440          */
441         close_filestruct(fsp);
442         file_free(fsp);
443         return 0;
444 }
445
446 /****************************************************************************
447  Close a files_struct.
448 ****************************************************************************/
449   
450 int close_file(files_struct *fsp, enum file_close_type close_type)
451 {
452         if(fsp->is_directory)
453                 return close_directory(fsp, close_type);
454         else if (fsp->is_stat)
455                 return close_stat(fsp);
456         else if (fsp->fake_file_handle != NULL)
457                 return close_fake_file(fsp);
458         else
459                 return close_normal_file(fsp, close_type);
460 }