Set the "stable" vendor string in VERSION.
[bbaumbach/samba-autobuild/.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-2007.
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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23
24 extern struct current_user current_user;
25
26 /****************************************************************************
27  Run a file if it is a magic script.
28 ****************************************************************************/
29
30 static void check_magic(struct files_struct *fsp)
31 {
32         int ret;
33         const char *magic_output = NULL;
34         SMB_STRUCT_STAT st;
35         int tmp_fd, outfd;
36         TALLOC_CTX *ctx = NULL;
37         const char *p;
38         struct connection_struct *conn = fsp->conn;
39
40         if (!*lp_magicscript(SNUM(conn))) {
41                 return;
42         }
43
44         DEBUG(5,("checking magic for %s\n",fsp->fsp_name));
45
46         if (!(p = strrchr_m(fsp->fsp_name,'/'))) {
47                 p = fsp->fsp_name;
48         } else {
49                 p++;
50         }
51
52         if (!strequal(lp_magicscript(SNUM(conn)),p)) {
53                 return;
54         }
55
56         ctx = talloc_stackframe();
57
58         if (*lp_magicoutput(SNUM(conn))) {
59                 magic_output = lp_magicoutput(SNUM(conn));
60         } else {
61                 magic_output = talloc_asprintf(ctx,
62                                 "%s.out",
63                                 fsp->fsp_name);
64         }
65         if (!magic_output) {
66                 TALLOC_FREE(ctx);
67                 return;
68         }
69
70         chmod(fsp->fsp_name,0755);
71         ret = smbrun(fsp->fsp_name,&tmp_fd);
72         DEBUG(3,("Invoking magic command %s gave %d\n",
73                 fsp->fsp_name,ret));
74
75         unlink(fsp->fsp_name);
76         if (ret != 0 || tmp_fd == -1) {
77                 if (tmp_fd != -1) {
78                         close(tmp_fd);
79                 }
80                 TALLOC_FREE(ctx);
81                 return;
82         }
83         outfd = open(magic_output, O_CREAT|O_EXCL|O_RDWR, 0600);
84         if (outfd == -1) {
85                 close(tmp_fd);
86                 TALLOC_FREE(ctx);
87                 return;
88         }
89
90         if (sys_fstat(tmp_fd,&st) == -1) {
91                 close(tmp_fd);
92                 close(outfd);
93                 return;
94         }
95
96         transfer_file(tmp_fd,outfd,(SMB_OFF_T)st.st_size);
97         close(tmp_fd);
98         close(outfd);
99         TALLOC_FREE(ctx);
100 }
101
102 /****************************************************************************
103   Common code to close a file or a directory.
104 ****************************************************************************/
105
106 static NTSTATUS close_filestruct(files_struct *fsp)
107 {
108         NTSTATUS status = NT_STATUS_OK;
109         connection_struct *conn = fsp->conn;
110     
111         if (fsp->fh->fd != -1) {
112                 if(flush_write_cache(fsp, CLOSE_FLUSH) == -1) {
113                         status = map_nt_error_from_unix(errno);
114                 }
115                 delete_write_cache(fsp);
116         }
117
118         conn->num_files_open--;
119         return status;
120 }    
121
122 /****************************************************************************
123  If any deferred opens are waiting on this close, notify them.
124 ****************************************************************************/
125
126 static void notify_deferred_opens(struct share_mode_lock *lck)
127 {
128         int i;
129  
130         for (i=0; i<lck->num_share_modes; i++) {
131                 struct share_mode_entry *e = &lck->share_modes[i];
132  
133                 if (!is_deferred_open_entry(e)) {
134                         continue;
135                 }
136  
137                 if (procid_is_me(&e->pid)) {
138                         /*
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
142                          * zero.
143                          */
144                         schedule_deferred_open_smb_message(e->op_mid);
145                 } else {
146                         char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
147
148                         share_mode_entry_to_message(msg, e);
149
150                         messaging_send_buf(smbd_messaging_context(),
151                                            e->pid, MSG_SMB_OPEN_RETRY,
152                                            (uint8 *)msg,
153                                            MSG_SMB_SHARE_MODE_ENTRY_SIZE);
154                 }
155         }
156 }
157
158 /****************************************************************************
159  Deal with removing a share mode on last close.
160 ****************************************************************************/
161
162 static NTSTATUS close_remove_share_mode(files_struct *fsp,
163                                         enum file_close_type close_type)
164 {
165         connection_struct *conn = fsp->conn;
166         bool delete_file = False;
167         struct share_mode_lock *lck;
168         SMB_STRUCT_STAT sbuf;
169         NTSTATUS status = NT_STATUS_OK;
170         int ret;
171         struct file_id id;
172
173         /*
174          * Lock the share entries, and determine if we should delete
175          * on close. If so delete whilst the lock is still in effect.
176          * This prevents race conditions with the file being created. JRA.
177          */
178
179         lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL);
180
181         if (lck == NULL) {
182                 DEBUG(0, ("close_remove_share_mode: Could not get share mode "
183                           "lock for file %s\n", fsp->fsp_name));
184                 return NT_STATUS_INVALID_PARAMETER;
185         }
186
187         if (!del_share_mode(lck, fsp)) {
188                 DEBUG(0, ("close_remove_share_mode: Could not delete share "
189                           "entry for file %s\n", fsp->fsp_name));
190         }
191
192         if (fsp->initial_delete_on_close && (lck->delete_token == NULL)) {
193                 bool became_user = False;
194
195                 /* Initial delete on close was set and no one else
196                  * wrote a real delete on close. */
197
198                 if (current_user.vuid != fsp->vuid) {
199                         become_user(conn, fsp->vuid);
200                         became_user = True;
201                 }
202                 set_delete_on_close_lck(lck, True, &current_user.ut);
203                 if (became_user) {
204                         unbecome_user();
205                 }
206         }
207
208         delete_file = lck->delete_on_close;
209
210         if (delete_file) {
211                 int i;
212                 /* See if others still have the file open. If this is the
213                  * case, then don't delete. If all opens are POSIX delete now. */
214                 for (i=0; i<lck->num_share_modes; i++) {
215                         struct share_mode_entry *e = &lck->share_modes[i];
216                         if (is_valid_share_mode_entry(e)) {
217                                 if (fsp->posix_open && (e->flags & SHARE_MODE_FLAG_POSIX_OPEN)) {
218                                         continue;
219                                 }
220                                 delete_file = False;
221                                 break;
222                         }
223                 }
224         }
225
226         /* Notify any deferred opens waiting on this close. */
227         notify_deferred_opens(lck);
228         reply_to_oplock_break_requests(fsp);
229
230         /*
231          * NT can set delete_on_close of the last open
232          * reference to a file.
233          */
234
235         if (!(close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE)
236             || !delete_file
237             || (lck->delete_token == NULL)) {
238                 TALLOC_FREE(lck);
239                 return NT_STATUS_OK;
240         }
241
242         /*
243          * Ok, we have to delete the file
244          */
245
246         DEBUG(5,("close_remove_share_mode: file %s. Delete on close was set "
247                  "- deleting file.\n", fsp->fsp_name));
248
249         /* Become the user who requested the delete. */
250
251         if (!push_sec_ctx()) {
252                 smb_panic("close_remove_share_mode: file %s. failed to push "
253                           "sec_ctx.\n");
254         }
255
256         set_sec_ctx(lck->delete_token->uid,
257                     lck->delete_token->gid,
258                     lck->delete_token->ngroups,
259                     lck->delete_token->groups,
260                     NULL);
261
262         /* We can only delete the file if the name we have is still valid and
263            hasn't been renamed. */
264
265         if (fsp->posix_open) {
266                 ret = SMB_VFS_LSTAT(conn,fsp->fsp_name,&sbuf);
267         } else {
268                 ret = SMB_VFS_STAT(conn,fsp->fsp_name,&sbuf);
269         }
270
271         if (ret != 0) {
272                 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
273                          "was set and stat failed with error %s\n",
274                          fsp->fsp_name, strerror(errno) ));
275                 /*
276                  * Don't save the errno here, we ignore this error
277                  */
278                 goto done;
279         }
280
281         id = vfs_file_id_from_sbuf(conn, &sbuf);
282
283         if (!file_id_equal(&fsp->file_id, &id)) {
284                 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
285                          "was set and dev and/or inode does not match\n",
286                          fsp->fsp_name ));
287                 DEBUG(5,("close_remove_share_mode: file %s. stored file_id %s, "
288                          "stat file_id %s\n",
289                          fsp->fsp_name,
290                          file_id_string_tos(&fsp->file_id),
291                          file_id_string_tos(&id)));
292                 /*
293                  * Don't save the errno here, we ignore this error
294                  */
295                 goto done;
296         }
297
298         if (SMB_VFS_UNLINK(conn,fsp->fsp_name) != 0) {
299                 /*
300                  * This call can potentially fail as another smbd may
301                  * have had the file open with delete on close set and
302                  * deleted it when its last reference to this file
303                  * went away. Hence we log this but not at debug level
304                  * zero.
305                  */
306
307                 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
308                          "was set and unlink failed with error %s\n",
309                          fsp->fsp_name, strerror(errno) ));
310
311                 status = map_nt_error_from_unix(errno);
312         }
313
314         notify_fname(conn, NOTIFY_ACTION_REMOVED,
315                      FILE_NOTIFY_CHANGE_FILE_NAME,
316                      fsp->fsp_name);
317
318         /* As we now have POSIX opens which can unlink
319          * with other open files we may have taken
320          * this code path with more than one share mode
321          * entry - ensure we only delete once by resetting
322          * the delete on close flag. JRA.
323          */
324
325         set_delete_on_close_lck(lck, False, NULL);
326
327  done:
328
329         /* unbecome user. */
330         pop_sec_ctx();
331         
332         TALLOC_FREE(lck);
333         return status;
334 }
335
336 /****************************************************************************
337  Close a file.
338
339  close_type can be NORMAL_CLOSE=0,SHUTDOWN_CLOSE,ERROR_CLOSE.
340  printing and magic scripts are only run on normal close.
341  delete on close is done on normal and shutdown close.
342 ****************************************************************************/
343
344 static NTSTATUS close_normal_file(files_struct *fsp, enum file_close_type close_type)
345 {
346         NTSTATUS status = NT_STATUS_OK;
347         NTSTATUS saved_status1 = NT_STATUS_OK;
348         NTSTATUS saved_status2 = NT_STATUS_OK;
349         NTSTATUS saved_status3 = NT_STATUS_OK;
350         connection_struct *conn = fsp->conn;
351
352         if (fsp->aio_write_behind) {
353                 /*
354                  * If we're finishing write behind on a close we can get a write
355                  * error here, we must remember this.
356                  */
357                 int ret = wait_for_aio_completion(fsp);
358                 if (ret) {
359                         saved_status1 = map_nt_error_from_unix(ret);
360                 }
361         } else {
362                 cancel_aio_by_fsp(fsp);
363         }
364  
365         /*
366          * If we're flushing on a close we can get a write
367          * error here, we must remember this.
368          */
369
370         saved_status2 = close_filestruct(fsp);
371
372         if (fsp->print_file) {
373                 print_fsp_end(fsp, close_type);
374                 file_free(fsp);
375                 return NT_STATUS_OK;
376         }
377
378         /* If this is an old DOS or FCB open and we have multiple opens on
379            the same handle we only have one share mode. Ensure we only remove
380            the share mode on the last close. */
381
382         if (fsp->fh->ref_count == 1) {
383                 /* Should we return on error here... ? */
384                 saved_status3 = close_remove_share_mode(fsp, close_type);
385         }
386
387         if(fsp->oplock_type) {
388                 release_file_oplock(fsp);
389         }
390
391         locking_close_file(smbd_messaging_context(), fsp);
392
393         status = fd_close(fsp);
394
395         /* check for magic scripts */
396         if (close_type == NORMAL_CLOSE) {
397                 check_magic(fsp);
398         }
399
400         /*
401          * Ensure pending modtime is set after close.
402          */
403
404         if (fsp->pending_modtime_owner && !null_timespec(fsp->pending_modtime)) {
405                 set_filetime(conn, fsp->fsp_name, fsp->pending_modtime);
406         } else if (!null_timespec(fsp->last_write_time)) {
407                 set_filetime(conn, fsp->fsp_name, fsp->last_write_time);
408         }
409
410         if (NT_STATUS_IS_OK(status)) {
411                 if (!NT_STATUS_IS_OK(saved_status1)) {
412                         status = saved_status1;
413                 } else if (!NT_STATUS_IS_OK(saved_status2)) {
414                         status = saved_status2;
415                 } else if (!NT_STATUS_IS_OK(saved_status3)) {
416                         status = saved_status3;
417                 }
418         }
419
420         DEBUG(2,("%s closed file %s (numopen=%d) %s\n",
421                 conn->user,fsp->fsp_name,
422                 conn->num_files_open,
423                 nt_errstr(status) ));
424
425         file_free(fsp);
426         return status;
427 }
428
429 /****************************************************************************
430  Close a directory opened by an NT SMB call. 
431 ****************************************************************************/
432   
433 static NTSTATUS close_directory(files_struct *fsp, enum file_close_type close_type)
434 {
435         struct share_mode_lock *lck = 0;
436         bool delete_dir = False;
437         NTSTATUS status = NT_STATUS_OK;
438
439         /*
440          * NT can set delete_on_close of the last open
441          * reference to a directory also.
442          */
443
444         lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL);
445
446         if (lck == NULL) {
447                 DEBUG(0, ("close_directory: Could not get share mode lock for %s\n", fsp->fsp_name));
448                 return NT_STATUS_INVALID_PARAMETER;
449         }
450
451         if (!del_share_mode(lck, fsp)) {
452                 DEBUG(0, ("close_directory: Could not delete share entry for %s\n", fsp->fsp_name));
453         }
454
455         if (fsp->initial_delete_on_close) {
456                 bool became_user = False;
457
458                 /* Initial delete on close was set - for
459                  * directories we don't care if anyone else
460                  * wrote a real delete on close. */
461
462                 if (current_user.vuid != fsp->vuid) {
463                         become_user(fsp->conn, fsp->vuid);
464                         became_user = True;
465                 }
466                 send_stat_cache_delete_message(fsp->fsp_name);
467                 set_delete_on_close_lck(lck, True, &current_user.ut);
468                 if (became_user) {
469                         unbecome_user();
470                 }
471         }
472
473         delete_dir = lck->delete_on_close;
474
475         if (delete_dir) {
476                 int i;
477                 /* See if others still have the dir open. If this is the
478                  * case, then don't delete. If all opens are POSIX delete now. */
479                 for (i=0; i<lck->num_share_modes; i++) {
480                         struct share_mode_entry *e = &lck->share_modes[i];
481                         if (is_valid_share_mode_entry(e)) {
482                                 if (fsp->posix_open && (e->flags & SHARE_MODE_FLAG_POSIX_OPEN)) {
483                                         continue;
484                                 }
485                                 delete_dir = False;
486                                 break;
487                         }
488                 }
489         }
490
491         if ((close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE) &&
492                                 delete_dir &&
493                                 lck->delete_token) {
494         
495                 /* Become the user who requested the delete. */
496
497                 if (!push_sec_ctx()) {
498                         smb_panic("close_directory: failed to push sec_ctx.\n");
499                 }
500
501                 set_sec_ctx(lck->delete_token->uid,
502                                 lck->delete_token->gid,
503                                 lck->delete_token->ngroups,
504                                 lck->delete_token->groups,
505                                 NULL);
506
507                 TALLOC_FREE(lck);
508
509                 status = rmdir_internals(talloc_tos(),
510                                 fsp->conn, fsp->fsp_name);
511
512                 DEBUG(5,("close_directory: %s. Delete on close was set - "
513                          "deleting directory returned %s.\n",
514                          fsp->fsp_name, nt_errstr(status)));
515
516                 /* unbecome user. */
517                 pop_sec_ctx();
518
519                 /*
520                  * Ensure we remove any change notify requests that would
521                  * now fail as the directory has been deleted.
522                  */
523
524                 if(NT_STATUS_IS_OK(status)) {
525                         remove_pending_change_notify_requests_by_fid(fsp, NT_STATUS_DELETE_PENDING);
526                 }
527         } else {
528                 TALLOC_FREE(lck);
529                 remove_pending_change_notify_requests_by_fid(
530                         fsp, NT_STATUS_OK);
531         }
532
533         /*
534          * Do the code common to files and directories.
535          */
536         close_filestruct(fsp);
537         file_free(fsp);
538         return status;
539 }
540
541 /****************************************************************************
542  Close a 'stat file' opened internally.
543 ****************************************************************************/
544   
545 static NTSTATUS close_stat(files_struct *fsp)
546 {
547         /*
548          * Do the code common to files and directories.
549          */
550         close_filestruct(fsp);
551         file_free(fsp);
552         return NT_STATUS_OK;
553 }
554
555 /****************************************************************************
556  Close a files_struct.
557 ****************************************************************************/
558   
559 NTSTATUS close_file(files_struct *fsp, enum file_close_type close_type)
560 {
561         if(fsp->is_directory) {
562                 return close_directory(fsp, close_type);
563         } else if (fsp->is_stat) {
564                 return close_stat(fsp);
565         } else if (fsp->fake_file_handle != NULL) {
566                 return close_fake_file(fsp);
567         }
568         return close_normal_file(fsp, close_type);
569 }