smbd: Remove write cache
[samba.git] / source3 / 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 #include "system/filesys.h"
24 #include "lib/util/server_id.h"
25 #include "printing.h"
26 #include "smbd/smbd.h"
27 #include "smbd/globals.h"
28 #include "smbd/scavenger.h"
29 #include "fake_file.h"
30 #include "transfer_file.h"
31 #include "auth.h"
32 #include "messages.h"
33 #include "../librpc/gen_ndr/open_files.h"
34
35 /****************************************************************************
36  Run a file if it is a magic script.
37 ****************************************************************************/
38
39 static NTSTATUS check_magic(struct files_struct *fsp)
40 {
41         int ret;
42         const char *magic_output = NULL;
43         SMB_STRUCT_STAT st;
44         int tmp_fd, outfd;
45         TALLOC_CTX *ctx = NULL;
46         const char *p;
47         struct connection_struct *conn = fsp->conn;
48         char *fname = NULL;
49         NTSTATUS status;
50
51         if (!*lp_magic_script(talloc_tos(), SNUM(conn))) {
52                 return NT_STATUS_OK;
53         }
54
55         DEBUG(5,("checking magic for %s\n", fsp_str_dbg(fsp)));
56
57         ctx = talloc_stackframe();
58
59         fname = fsp->fsp_name->base_name;
60
61         if (!(p = strrchr_m(fname,'/'))) {
62                 p = fname;
63         } else {
64                 p++;
65         }
66
67         if (!strequal(lp_magic_script(talloc_tos(), SNUM(conn)),p)) {
68                 status = NT_STATUS_OK;
69                 goto out;
70         }
71
72         if (*lp_magic_output(talloc_tos(), SNUM(conn))) {
73                 magic_output = lp_magic_output(talloc_tos(), SNUM(conn));
74         } else {
75                 magic_output = talloc_asprintf(ctx,
76                                 "%s.out",
77                                 fname);
78         }
79         if (!magic_output) {
80                 status = NT_STATUS_NO_MEMORY;
81                 goto out;
82         }
83
84         /* Ensure we don't depend on user's PATH. */
85         p = talloc_asprintf(ctx, "./%s", fname);
86         if (!p) {
87                 status = NT_STATUS_NO_MEMORY;
88                 goto out;
89         }
90
91         if (chmod(fname, 0755) == -1) {
92                 status = map_nt_error_from_unix(errno);
93                 goto out;
94         }
95         ret = smbrun(p, &tmp_fd, NULL);
96         DEBUG(3,("Invoking magic command %s gave %d\n",
97                 p,ret));
98
99         unlink(fname);
100         if (ret != 0 || tmp_fd == -1) {
101                 if (tmp_fd != -1) {
102                         close(tmp_fd);
103                 }
104                 status = NT_STATUS_UNSUCCESSFUL;
105                 goto out;
106         }
107         outfd = open(magic_output, O_CREAT|O_EXCL|O_RDWR, 0600);
108         if (outfd == -1) {
109                 int err = errno;
110                 close(tmp_fd);
111                 status = map_nt_error_from_unix(err);
112                 goto out;
113         }
114
115         if (sys_fstat(tmp_fd, &st, false) == -1) {
116                 int err = errno;
117                 close(tmp_fd);
118                 close(outfd);
119                 status = map_nt_error_from_unix(err);
120                 goto out;
121         }
122
123         if (transfer_file(tmp_fd,outfd,(off_t)st.st_ex_size) == (off_t)-1) {
124                 int err = errno;
125                 close(tmp_fd);
126                 close(outfd);
127                 status = map_nt_error_from_unix(err);
128                 goto out;
129         }
130         close(tmp_fd);
131         if (close(outfd) == -1) {
132                 status = map_nt_error_from_unix(errno);
133                 goto out;
134         }
135
136         status = NT_STATUS_OK;
137
138  out:
139         TALLOC_FREE(ctx);
140         return status;
141 }
142
143 /****************************************************************************
144  Delete all streams
145 ****************************************************************************/
146
147 NTSTATUS delete_all_streams(connection_struct *conn,
148                         const struct smb_filename *smb_fname)
149 {
150         struct stream_struct *stream_info = NULL;
151         unsigned int i;
152         unsigned int num_streams = 0;
153         TALLOC_CTX *frame = talloc_stackframe();
154         NTSTATUS status;
155
156         status = vfs_streaminfo(conn, NULL, smb_fname, talloc_tos(),
157                                 &num_streams, &stream_info);
158
159         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
160                 DEBUG(10, ("no streams around\n"));
161                 TALLOC_FREE(frame);
162                 return NT_STATUS_OK;
163         }
164
165         if (!NT_STATUS_IS_OK(status)) {
166                 DEBUG(10, ("vfs_streaminfo failed: %s\n",
167                            nt_errstr(status)));
168                 goto fail;
169         }
170
171         DEBUG(10, ("delete_all_streams found %d streams\n",
172                    num_streams));
173
174         if (num_streams == 0) {
175                 TALLOC_FREE(frame);
176                 return NT_STATUS_OK;
177         }
178
179         for (i=0; i<num_streams; i++) {
180                 int res;
181                 struct smb_filename *smb_fname_stream;
182
183                 if (strequal(stream_info[i].name, "::$DATA")) {
184                         continue;
185                 }
186
187                 smb_fname_stream = synthetic_smb_fname(talloc_tos(),
188                                         smb_fname->base_name,
189                                         stream_info[i].name,
190                                         NULL,
191                                         (smb_fname->flags &
192                                                 ~SMB_FILENAME_POSIX_PATH));
193
194                 if (smb_fname_stream == NULL) {
195                         DEBUG(0, ("talloc_aprintf failed\n"));
196                         status = NT_STATUS_NO_MEMORY;
197                         goto fail;
198                 }
199
200                 res = SMB_VFS_UNLINKAT(conn,
201                                 conn->cwd_fsp,
202                                 smb_fname_stream,
203                                 0);
204
205                 if (res == -1) {
206                         status = map_nt_error_from_unix(errno);
207                         DEBUG(10, ("Could not delete stream %s: %s\n",
208                                    smb_fname_str_dbg(smb_fname_stream),
209                                    strerror(errno)));
210                         TALLOC_FREE(smb_fname_stream);
211                         break;
212                 }
213                 TALLOC_FREE(smb_fname_stream);
214         }
215
216  fail:
217         TALLOC_FREE(frame);
218         return status;
219 }
220
221 struct has_other_nonposix_opens_state {
222         files_struct *fsp;
223         bool found_another;
224 };
225
226 static bool has_other_nonposix_opens_fn(
227         struct share_mode_entry *e,
228         bool *modified,
229         void *private_data)
230 {
231         struct has_other_nonposix_opens_state *state = private_data;
232         struct files_struct *fsp = state->fsp;
233
234         if (e->name_hash != fsp->name_hash) {
235                 return false;
236         }
237         if ((fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) &&
238             (e->flags & SHARE_MODE_FLAG_POSIX_OPEN)) {
239                 return false;
240         }
241         if (e->share_file_id == fsp->fh->gen_id) {
242                 struct server_id self = messaging_server_id(
243                         fsp->conn->sconn->msg_ctx);
244                 if (server_id_equal(&self, &e->pid)) {
245                         return false;
246                 }
247         }
248         if (share_entry_stale_pid(e)) {
249                 return false;
250         }
251
252         state->found_another = true;
253         return true;
254 }
255
256 bool has_other_nonposix_opens(struct share_mode_lock *lck,
257                               struct files_struct *fsp)
258 {
259         struct has_other_nonposix_opens_state state = { .fsp = fsp };
260         bool ok;
261
262         ok = share_mode_forall_entries(
263                 lck, has_other_nonposix_opens_fn, &state);
264         if (!ok) {
265                 return false;
266         }
267         return state.found_another;
268 }
269
270 /****************************************************************************
271  Deal with removing a share mode on last close.
272 ****************************************************************************/
273
274 static NTSTATUS close_remove_share_mode(files_struct *fsp,
275                                         enum file_close_type close_type)
276 {
277         connection_struct *conn = fsp->conn;
278         bool delete_file = false;
279         bool changed_user = false;
280         struct share_mode_lock *lck = NULL;
281         NTSTATUS status = NT_STATUS_OK;
282         NTSTATUS tmp_status;
283         struct file_id id;
284         const struct security_unix_token *del_token = NULL;
285         const struct security_token *del_nt_token = NULL;
286         bool got_tokens = false;
287         bool normal_close;
288         int ret;
289
290         /* Ensure any pending write time updates are done. */
291         if (fsp->update_write_time_event) {
292                 fsp_flush_write_time_update(fsp);
293         }
294
295         /*
296          * Lock the share entries, and determine if we should delete
297          * on close. If so delete whilst the lock is still in effect.
298          * This prevents race conditions with the file being created. JRA.
299          */
300
301         lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
302         if (lck == NULL) {
303                 DEBUG(0, ("close_remove_share_mode: Could not get share mode "
304                           "lock for file %s\n", fsp_str_dbg(fsp)));
305                 return NT_STATUS_INVALID_PARAMETER;
306         }
307
308         /* Remove the oplock before potentially deleting the file. */
309         if(fsp->oplock_type) {
310                 remove_oplock(fsp);
311         }
312
313         if (fsp->write_time_forced) {
314                 DEBUG(10,("close_remove_share_mode: write time forced "
315                         "for file %s\n",
316                         fsp_str_dbg(fsp)));
317                 set_close_write_time(fsp, lck->data->changed_write_time);
318         } else if (fsp->update_write_time_on_close) {
319                 /* Someone had a pending write. */
320                 if (null_timespec(fsp->close_write_time)) {
321                         DEBUG(10,("close_remove_share_mode: update to current time "
322                                 "for file %s\n",
323                                 fsp_str_dbg(fsp)));
324                         /* Update to current time due to "normal" write. */
325                         set_close_write_time(fsp, timespec_current());
326                 } else {
327                         DEBUG(10,("close_remove_share_mode: write time pending "
328                                 "for file %s\n",
329                                 fsp_str_dbg(fsp)));
330                         /* Update to time set on close call. */
331                         set_close_write_time(fsp, fsp->close_write_time);
332                 }
333         }
334
335         if (fsp->initial_delete_on_close &&
336                         !is_delete_on_close_set(lck, fsp->name_hash)) {
337                 bool became_user = False;
338
339                 /* Initial delete on close was set and no one else
340                  * wrote a real delete on close. */
341
342                 if (get_current_vuid(conn) != fsp->vuid) {
343                         become_user_without_service(conn, fsp->vuid);
344                         became_user = True;
345                 }
346                 fsp->delete_on_close = true;
347                 set_delete_on_close_lck(fsp, lck,
348                                 get_current_nttok(conn),
349                                 get_current_utok(conn));
350                 if (became_user) {
351                         unbecome_user_without_service();
352                 }
353         }
354
355         delete_file = is_delete_on_close_set(lck, fsp->name_hash) &&
356                 !has_other_nonposix_opens(lck, fsp);
357
358         /*
359          * NT can set delete_on_close of the last open
360          * reference to a file.
361          */
362
363         normal_close = (close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE);
364
365         if (!normal_close || !delete_file) {
366                 status = NT_STATUS_OK;
367                 goto done;
368         }
369
370         /*
371          * Ok, we have to delete the file
372          */
373
374         DEBUG(5,("close_remove_share_mode: file %s. Delete on close was set "
375                  "- deleting file.\n", fsp_str_dbg(fsp)));
376
377         /*
378          * Don't try to update the write time when we delete the file
379          */
380         fsp->update_write_time_on_close = false;
381
382         got_tokens = get_delete_on_close_token(lck, fsp->name_hash,
383                                         &del_nt_token, &del_token);
384         SMB_ASSERT(got_tokens);
385
386         if (!unix_token_equal(del_token, get_current_utok(conn))) {
387                 /* Become the user who requested the delete. */
388
389                 DEBUG(5,("close_remove_share_mode: file %s. "
390                         "Change user to uid %u\n",
391                         fsp_str_dbg(fsp),
392                         (unsigned int)del_token->uid));
393
394                 if (!push_sec_ctx()) {
395                         smb_panic("close_remove_share_mode: file %s. failed to push "
396                                   "sec_ctx.\n");
397                 }
398
399                 set_sec_ctx(del_token->uid,
400                             del_token->gid,
401                             del_token->ngroups,
402                             del_token->groups,
403                             del_nt_token);
404
405                 changed_user = true;
406         }
407
408         /* We can only delete the file if the name we have is still valid and
409            hasn't been renamed. */
410
411         tmp_status = vfs_stat_fsp(fsp);
412         if (!NT_STATUS_IS_OK(tmp_status)) {
413                 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
414                          "was set and stat failed with error %s\n",
415                          fsp_str_dbg(fsp), nt_errstr(tmp_status)));
416                 /*
417                  * Don't save the errno here, we ignore this error
418                  */
419                 goto done;
420         }
421
422         id = vfs_file_id_from_sbuf(conn, &fsp->fsp_name->st);
423
424         if (!file_id_equal(&fsp->file_id, &id)) {
425                 struct file_id_buf ftmp1, ftmp2;
426                 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
427                          "was set and dev and/or inode does not match\n",
428                          fsp_str_dbg(fsp)));
429                 DEBUG(5,("close_remove_share_mode: file %s. stored file_id %s, "
430                          "stat file_id %s\n",
431                          fsp_str_dbg(fsp),
432                          file_id_str_buf(fsp->file_id, &ftmp1),
433                          file_id_str_buf(id, &ftmp2)));
434                 /*
435                  * Don't save the errno here, we ignore this error
436                  */
437                 goto done;
438         }
439
440         if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
441             && !is_ntfs_stream_smb_fname(fsp->fsp_name)) {
442
443                 status = delete_all_streams(conn, fsp->fsp_name);
444
445                 if (!NT_STATUS_IS_OK(status)) {
446                         DEBUG(5, ("delete_all_streams failed: %s\n",
447                                   nt_errstr(status)));
448                         goto done;
449                 }
450         }
451
452         if (fsp->kernel_share_modes_taken) {
453                 int ret_flock;
454
455                 /*
456                  * A file system sharemode could block the unlink;
457                  * remove filesystem sharemodes first.
458                  */
459                 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, 0, 0);
460                 if (ret_flock == -1) {
461                         DBG_INFO("removing kernel flock for %s failed: %s\n",
462                                   fsp_str_dbg(fsp), strerror(errno));
463                 }
464
465                 fsp->kernel_share_modes_taken = false;
466         }
467
468
469         ret = SMB_VFS_UNLINKAT(conn,
470                         conn->cwd_fsp,
471                         fsp->fsp_name,
472                         0);
473         if (ret != 0) {
474                 /*
475                  * This call can potentially fail as another smbd may
476                  * have had the file open with delete on close set and
477                  * deleted it when its last reference to this file
478                  * went away. Hence we log this but not at debug level
479                  * zero.
480                  */
481
482                 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
483                          "was set and unlink failed with error %s\n",
484                          fsp_str_dbg(fsp), strerror(errno)));
485
486                 status = map_nt_error_from_unix(errno);
487         }
488
489         /* As we now have POSIX opens which can unlink
490          * with other open files we may have taken
491          * this code path with more than one share mode
492          * entry - ensure we only delete once by resetting
493          * the delete on close flag. JRA.
494          */
495
496         fsp->delete_on_close = false;
497         reset_delete_on_close_lck(fsp, lck);
498
499  done:
500
501         if (changed_user) {
502                 /* unbecome user. */
503                 pop_sec_ctx();
504         }
505
506         if (fsp->kernel_share_modes_taken) {
507                 int ret_flock;
508
509                 /* remove filesystem sharemodes */
510                 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, 0, 0);
511                 if (ret_flock == -1) {
512                         DEBUG(2, ("close_remove_share_mode: removing kernel "
513                                   "flock for %s failed: %s\n",
514                                   fsp_str_dbg(fsp), strerror(errno)));
515                 }
516         }
517
518         if (!del_share_mode(lck, fsp)) {
519                 DEBUG(0, ("close_remove_share_mode: Could not delete share "
520                           "entry for file %s\n", fsp_str_dbg(fsp)));
521         }
522
523         TALLOC_FREE(lck);
524
525         if (delete_file) {
526                 /*
527                  * Do the notification after we released the share
528                  * mode lock. Inside notify_fname we take out another
529                  * tdb lock. With ctdb also accessing our databases,
530                  * this can lead to deadlocks. Putting this notify
531                  * after the TALLOC_FREE(lck) above we avoid locking
532                  * two records simultaneously. Notifies are async and
533                  * informational only, so calling the notify_fname
534                  * without holding the share mode lock should not do
535                  * any harm.
536                  */
537                 notify_fname(conn, NOTIFY_ACTION_REMOVED,
538                              FILE_NOTIFY_CHANGE_FILE_NAME,
539                              fsp->fsp_name->base_name);
540         }
541
542         return status;
543 }
544
545 void set_close_write_time(struct files_struct *fsp, struct timespec ts)
546 {
547         DEBUG(6,("close_write_time: %s" , time_to_asc(convert_timespec_to_time_t(ts))));
548
549         if (null_timespec(ts)) {
550                 return;
551         }
552         fsp->write_time_forced = false;
553         fsp->update_write_time_on_close = true;
554         fsp->close_write_time = ts;
555 }
556
557 static NTSTATUS update_write_time_on_close(struct files_struct *fsp)
558 {
559         struct smb_file_time ft;
560         NTSTATUS status;
561         struct share_mode_lock *lck = NULL;
562
563         ZERO_STRUCT(ft);
564
565         if (!fsp->update_write_time_on_close) {
566                 return NT_STATUS_OK;
567         }
568
569         if (null_timespec(fsp->close_write_time)) {
570                 fsp->close_write_time = timespec_current();
571         }
572
573         /* Ensure we have a valid stat struct for the source. */
574         status = vfs_stat_fsp(fsp);
575         if (!NT_STATUS_IS_OK(status)) {
576                 return status;
577         }
578
579         if (!VALID_STAT(fsp->fsp_name->st)) {
580                 /* if it doesn't seem to be a real file */
581                 return NT_STATUS_OK;
582         }
583
584         /*
585          * get_existing_share_mode_lock() isn't really the right
586          * call here, as we're being called after
587          * close_remove_share_mode() inside close_normal_file()
588          * so it's quite normal to not have an existing share
589          * mode here. However, get_share_mode_lock() doesn't
590          * work because that will create a new share mode if
591          * one doesn't exist - so stick with this call (just
592          * ignore any error we get if the share mode doesn't
593          * exist.
594          */
595
596         lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
597         if (lck) {
598                 /* On close if we're changing the real file time we
599                  * must update it in the open file db too. */
600                 (void)set_write_time(fsp->file_id, fsp->close_write_time);
601
602                 /* Close write times overwrite sticky write times
603                    so we must replace any sticky write time here. */
604                 if (!null_timespec(lck->data->changed_write_time)) {
605                         (void)set_sticky_write_time(fsp->file_id, fsp->close_write_time);
606                 }
607                 TALLOC_FREE(lck);
608         }
609
610         ft.mtime = fsp->close_write_time;
611         /* As this is a close based update, we are not directly changing the
612            file attributes from a client call, but indirectly from a write. */
613         status = smb_set_file_time(fsp->conn, fsp, fsp->fsp_name, &ft, false);
614         if (!NT_STATUS_IS_OK(status)) {
615                 DEBUG(10,("update_write_time_on_close: smb_set_file_time "
616                         "on file %s returned %s\n",
617                         fsp_str_dbg(fsp),
618                         nt_errstr(status)));
619                 return status;
620         }
621
622         return status;
623 }
624
625 static NTSTATUS ntstatus_keeperror(NTSTATUS s1, NTSTATUS s2)
626 {
627         if (!NT_STATUS_IS_OK(s1)) {
628                 return s1;
629         }
630         return s2;
631 }
632
633 /****************************************************************************
634  Close a file.
635
636  close_type can be NORMAL_CLOSE=0,SHUTDOWN_CLOSE,ERROR_CLOSE.
637  printing and magic scripts are only run on normal close.
638  delete on close is done on normal and shutdown close.
639 ****************************************************************************/
640
641 static NTSTATUS close_normal_file(struct smb_request *req, files_struct *fsp,
642                                   enum file_close_type close_type)
643 {
644         NTSTATUS status = NT_STATUS_OK;
645         NTSTATUS tmp;
646         connection_struct *conn = fsp->conn;
647         bool is_durable = false;
648
649         if (fsp->num_aio_requests != 0) {
650
651                 if (close_type != SHUTDOWN_CLOSE) {
652                         /*
653                          * reply_close and the smb2 close must have
654                          * taken care of this. No other callers of
655                          * close_file should ever have created async
656                          * I/O.
657                          *
658                          * We need to panic here because if we close()
659                          * the fd while we have outstanding async I/O
660                          * requests, in the worst case we could end up
661                          * writing to the wrong file.
662                          */
663                         DEBUG(0, ("fsp->num_aio_requests=%u\n",
664                                   fsp->num_aio_requests));
665                         smb_panic("can not close with outstanding aio "
666                                   "requests");
667                 }
668
669                 /*
670                  * For shutdown close, just drop the async requests
671                  * including a potential close request pending for
672                  * this fsp. Drop the close request first, the
673                  * destructor for the aio_requests would execute it.
674                  */
675                 TALLOC_FREE(fsp->deferred_close);
676
677                 while (fsp->num_aio_requests != 0) {
678                         /*
679                          * The destructor of the req will remove
680                          * itself from the fsp.
681                          * Don't use TALLOC_FREE here, this will overwrite
682                          * what the destructor just wrote into
683                          * aio_requests[0].
684                          */
685                         talloc_free(fsp->aio_requests[0]);
686                 }
687         }
688
689         while (talloc_array_length(fsp->blocked_smb1_lock_reqs) != 0) {
690                 smbd_smb1_brl_finish_by_req(
691                         fsp->blocked_smb1_lock_reqs[0],
692                         NT_STATUS_RANGE_NOT_LOCKED);
693         }
694
695         /*
696          * If we're flushing on a close we can get a write
697          * error here, we must remember this.
698          */
699
700         if (NT_STATUS_IS_OK(status) && fsp->op != NULL) {
701                 is_durable = fsp->op->global->durable;
702         }
703
704         if (close_type != SHUTDOWN_CLOSE) {
705                 is_durable = false;
706         }
707
708         if (is_durable) {
709                 DATA_BLOB new_cookie = data_blob_null;
710
711                 tmp = SMB_VFS_DURABLE_DISCONNECT(fsp,
712                                         fsp->op->global->backend_cookie,
713                                         fsp->op,
714                                         &new_cookie);
715                 if (NT_STATUS_IS_OK(tmp)) {
716                         struct timeval tv;
717                         NTTIME now;
718
719                         if (req != NULL) {
720                                 tv = req->request_time;
721                         } else {
722                                 tv = timeval_current();
723                         }
724                         now = timeval_to_nttime(&tv);
725
726                         data_blob_free(&fsp->op->global->backend_cookie);
727                         fsp->op->global->backend_cookie = new_cookie;
728
729                         fsp->op->compat = NULL;
730                         tmp = smbXsrv_open_close(fsp->op, now);
731                         if (!NT_STATUS_IS_OK(tmp)) {
732                                 DEBUG(1, ("Failed to update smbXsrv_open "
733                                           "record when disconnecting durable "
734                                           "handle for file %s: %s - "
735                                           "proceeding with normal close\n",
736                                           fsp_str_dbg(fsp), nt_errstr(tmp)));
737                         }
738                         scavenger_schedule_disconnected(fsp);
739                 } else {
740                         DEBUG(1, ("Failed to disconnect durable handle for "
741                                   "file %s: %s - proceeding with normal "
742                                   "close\n", fsp_str_dbg(fsp), nt_errstr(tmp)));
743                 }
744                 if (!NT_STATUS_IS_OK(tmp)) {
745                         is_durable = false;
746                 }
747         }
748
749         if (is_durable) {
750                 /*
751                  * This is the case where we successfully disconnected
752                  * a durable handle and closed the underlying file.
753                  * In all other cases, we proceed with a genuine close.
754                  */
755                 DEBUG(10, ("%s disconnected durable handle for file %s\n",
756                            conn->session_info->unix_info->unix_name,
757                            fsp_str_dbg(fsp)));
758                 file_free(req, fsp);
759                 return NT_STATUS_OK;
760         }
761
762         if (fsp->op != NULL) {
763                 /*
764                  * Make sure the handle is not marked as durable anymore
765                  */
766                 fsp->op->global->durable = false;
767         }
768
769         if (fsp->print_file) {
770                 /* FIXME: return spool errors */
771                 print_spool_end(fsp, close_type);
772                 file_free(req, fsp);
773                 return NT_STATUS_OK;
774         }
775
776         /* If this is an old DOS or FCB open and we have multiple opens on
777            the same handle we only have one share mode. Ensure we only remove
778            the share mode on the last close. */
779
780         if (fsp->fh->ref_count == 1) {
781                 /* Should we return on error here... ? */
782                 tmp = close_remove_share_mode(fsp, close_type);
783                 status = ntstatus_keeperror(status, tmp);
784         }
785
786         locking_close_file(fsp, close_type);
787
788         tmp = fd_close(fsp);
789         status = ntstatus_keeperror(status, tmp);
790
791         /* check for magic scripts */
792         if (close_type == NORMAL_CLOSE) {
793                 tmp = check_magic(fsp);
794                 status = ntstatus_keeperror(status, tmp);
795         }
796
797         /*
798          * Ensure pending modtime is set after close.
799          */
800
801         tmp = update_write_time_on_close(fsp);
802         if (NT_STATUS_EQUAL(tmp, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
803                 /* Someone renamed the file or a parent directory containing
804                  * this file. We can't do anything about this, we don't have
805                  * an "update timestamp by fd" call in POSIX. Eat the error. */
806
807                 tmp = NT_STATUS_OK;
808         }
809
810         status = ntstatus_keeperror(status, tmp);
811
812         DEBUG(2,("%s closed file %s (numopen=%d) %s\n",
813                 conn->session_info->unix_info->unix_name, fsp_str_dbg(fsp),
814                 conn->num_files_open - 1,
815                 nt_errstr(status) ));
816
817         file_free(req, fsp);
818         return status;
819 }
820 /****************************************************************************
821  Function used by reply_rmdir to delete an entire directory
822  tree recursively. Return True on ok, False on fail.
823 ****************************************************************************/
824
825 bool recursive_rmdir(TALLOC_CTX *ctx,
826                      connection_struct *conn,
827                      struct smb_filename *smb_dname)
828 {
829         const char *dname = NULL;
830         char *talloced = NULL;
831         bool ret = True;
832         long offset = 0;
833         SMB_STRUCT_STAT st;
834         struct smb_Dir *dir_hnd;
835         int retval;
836
837         SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname));
838
839         dir_hnd = OpenDir(talloc_tos(), conn, smb_dname, NULL, 0);
840         if(dir_hnd == NULL)
841                 return False;
842
843         while((dname = ReadDirName(dir_hnd, &offset, &st, &talloced))) {
844                 struct smb_filename *smb_dname_full = NULL;
845                 char *fullname = NULL;
846                 bool do_break = true;
847
848                 if (ISDOT(dname) || ISDOTDOT(dname)) {
849                         TALLOC_FREE(talloced);
850                         continue;
851                 }
852
853                 if (!is_visible_file(conn, smb_dname->base_name, dname, &st,
854                                      false)) {
855                         TALLOC_FREE(talloced);
856                         continue;
857                 }
858
859                 /* Construct the full name. */
860                 fullname = talloc_asprintf(ctx,
861                                 "%s/%s",
862                                 smb_dname->base_name,
863                                 dname);
864                 if (!fullname) {
865                         errno = ENOMEM;
866                         goto err_break;
867                 }
868
869                 smb_dname_full = synthetic_smb_fname(talloc_tos(),
870                                                 fullname,
871                                                 NULL,
872                                                 NULL,
873                                                 smb_dname->flags);
874                 if (smb_dname_full == NULL) {
875                         errno = ENOMEM;
876                         goto err_break;
877                 }
878
879                 if(SMB_VFS_LSTAT(conn, smb_dname_full) != 0) {
880                         goto err_break;
881                 }
882
883                 if(smb_dname_full->st.st_ex_mode & S_IFDIR) {
884                         if(!recursive_rmdir(ctx, conn, smb_dname_full)) {
885                                 goto err_break;
886                         }
887                         retval = SMB_VFS_UNLINKAT(conn,
888                                         conn->cwd_fsp,
889                                         smb_dname_full,
890                                         AT_REMOVEDIR);
891                         if (retval != 0) {
892                                 goto err_break;
893                         }
894                 } else {
895                         retval = SMB_VFS_UNLINKAT(conn,
896                                         conn->cwd_fsp,
897                                         smb_dname_full,
898                                         0);
899                         if (retval != 0) {
900                                 goto err_break;
901                         }
902                 }
903
904                 /* Successful iteration. */
905                 do_break = false;
906
907          err_break:
908                 TALLOC_FREE(smb_dname_full);
909                 TALLOC_FREE(fullname);
910                 TALLOC_FREE(talloced);
911                 if (do_break) {
912                         ret = false;
913                         break;
914                 }
915         }
916         TALLOC_FREE(dir_hnd);
917         return ret;
918 }
919
920 /****************************************************************************
921  The internals of the rmdir code - called elsewhere.
922 ****************************************************************************/
923
924 static NTSTATUS rmdir_internals(TALLOC_CTX *ctx, files_struct *fsp)
925 {
926         connection_struct *conn = fsp->conn;
927         struct smb_filename *smb_dname = fsp->fsp_name;
928         int ret;
929
930         SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname));
931
932         /* Might be a symlink. */
933         if(SMB_VFS_LSTAT(conn, smb_dname) != 0) {
934                 return map_nt_error_from_unix(errno);
935         }
936
937         if (S_ISLNK(smb_dname->st.st_ex_mode)) {
938                 /* Is what it points to a directory ? */
939                 if(SMB_VFS_STAT(conn, smb_dname) != 0) {
940                         return map_nt_error_from_unix(errno);
941                 }
942                 if (!(S_ISDIR(smb_dname->st.st_ex_mode))) {
943                         return NT_STATUS_NOT_A_DIRECTORY;
944                 }
945                 ret = SMB_VFS_UNLINKAT(conn,
946                                 conn->cwd_fsp,
947                                 smb_dname,
948                                 0);
949         } else {
950                 ret = SMB_VFS_UNLINKAT(conn,
951                                 conn->cwd_fsp,
952                                 smb_dname,
953                                 AT_REMOVEDIR);
954         }
955         if (ret == 0) {
956                 notify_fname(conn, NOTIFY_ACTION_REMOVED,
957                              FILE_NOTIFY_CHANGE_DIR_NAME,
958                              smb_dname->base_name);
959                 return NT_STATUS_OK;
960         }
961
962         if(((errno == ENOTEMPTY)||(errno == EEXIST)) && *lp_veto_files(talloc_tos(), SNUM(conn))) {
963                 /*
964                  * Check to see if the only thing in this directory are
965                  * vetoed files/directories. If so then delete them and
966                  * retry. If we fail to delete any of them (and we *don't*
967                  * do a recursive delete) then fail the rmdir.
968                  */
969                 SMB_STRUCT_STAT st;
970                 const char *dname = NULL;
971                 char *talloced = NULL;
972                 long dirpos = 0;
973                 struct smb_Dir *dir_hnd = OpenDir(talloc_tos(), conn,
974                                                   smb_dname, NULL,
975                                                   0);
976
977                 if(dir_hnd == NULL) {
978                         errno = ENOTEMPTY;
979                         goto err;
980                 }
981
982                 while ((dname = ReadDirName(dir_hnd, &dirpos, &st,
983                                             &talloced)) != NULL) {
984                         if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0)) {
985                                 TALLOC_FREE(talloced);
986                                 continue;
987                         }
988                         if (!is_visible_file(conn, smb_dname->base_name, dname,
989                                              &st, false)) {
990                                 TALLOC_FREE(talloced);
991                                 continue;
992                         }
993                         if(!IS_VETO_PATH(conn, dname)) {
994                                 TALLOC_FREE(dir_hnd);
995                                 TALLOC_FREE(talloced);
996                                 errno = ENOTEMPTY;
997                                 goto err;
998                         }
999                         TALLOC_FREE(talloced);
1000                 }
1001
1002                 /* We only have veto files/directories.
1003                  * Are we allowed to delete them ? */
1004
1005                 if(!lp_delete_veto_files(SNUM(conn))) {
1006                         TALLOC_FREE(dir_hnd);
1007                         errno = ENOTEMPTY;
1008                         goto err;
1009                 }
1010
1011                 /* Do a recursive delete. */
1012                 RewindDir(dir_hnd,&dirpos);
1013                 while ((dname = ReadDirName(dir_hnd, &dirpos, &st,
1014                                             &talloced)) != NULL) {
1015                         struct smb_filename *smb_dname_full = NULL;
1016                         char *fullname = NULL;
1017                         bool do_break = true;
1018
1019                         if (ISDOT(dname) || ISDOTDOT(dname)) {
1020                                 TALLOC_FREE(talloced);
1021                                 continue;
1022                         }
1023                         if (!is_visible_file(conn, smb_dname->base_name, dname,
1024                                              &st, false)) {
1025                                 TALLOC_FREE(talloced);
1026                                 continue;
1027                         }
1028
1029                         fullname = talloc_asprintf(ctx,
1030                                         "%s/%s",
1031                                         smb_dname->base_name,
1032                                         dname);
1033
1034                         if(!fullname) {
1035                                 errno = ENOMEM;
1036                                 goto err_break;
1037                         }
1038
1039                         smb_dname_full = synthetic_smb_fname(talloc_tos(),
1040                                                         fullname,
1041                                                         NULL,
1042                                                         NULL,
1043                                                         smb_dname->flags);
1044                         if (smb_dname_full == NULL) {
1045                                 errno = ENOMEM;
1046                                 goto err_break;
1047                         }
1048
1049                         if(SMB_VFS_LSTAT(conn, smb_dname_full) != 0) {
1050                                 goto err_break;
1051                         }
1052                         if(smb_dname_full->st.st_ex_mode & S_IFDIR) {
1053                                 int retval;
1054                                 if(!recursive_rmdir(ctx, conn,
1055                                                     smb_dname_full)) {
1056                                         goto err_break;
1057                                 }
1058                                 retval = SMB_VFS_UNLINKAT(conn,
1059                                                 conn->cwd_fsp,
1060                                                 smb_dname_full,
1061                                                 AT_REMOVEDIR);
1062                                 if(retval != 0) {
1063                                         goto err_break;
1064                                 }
1065                         } else {
1066                                 int retval = SMB_VFS_UNLINKAT(conn,
1067                                                 conn->cwd_fsp,
1068                                                 smb_dname_full,
1069                                                 0);
1070                                 if(retval != 0) {
1071                                         goto err_break;
1072                                 }
1073                         }
1074
1075                         /* Successful iteration. */
1076                         do_break = false;
1077
1078                  err_break:
1079                         TALLOC_FREE(fullname);
1080                         TALLOC_FREE(smb_dname_full);
1081                         TALLOC_FREE(talloced);
1082                         if (do_break)
1083                                 break;
1084                 }
1085                 TALLOC_FREE(dir_hnd);
1086                 /* Retry the rmdir */
1087                 ret = SMB_VFS_UNLINKAT(conn,
1088                                 conn->cwd_fsp,
1089                                 smb_dname,
1090                                 AT_REMOVEDIR);
1091         }
1092
1093   err:
1094
1095         if (ret != 0) {
1096                 DEBUG(3,("rmdir_internals: couldn't remove directory %s : "
1097                          "%s\n", smb_fname_str_dbg(smb_dname),
1098                          strerror(errno)));
1099                 return map_nt_error_from_unix(errno);
1100         }
1101
1102         notify_fname(conn, NOTIFY_ACTION_REMOVED,
1103                      FILE_NOTIFY_CHANGE_DIR_NAME,
1104                      smb_dname->base_name);
1105
1106         return NT_STATUS_OK;
1107 }
1108
1109 /****************************************************************************
1110  Close a directory opened by an NT SMB call. 
1111 ****************************************************************************/
1112   
1113 static NTSTATUS close_directory(struct smb_request *req, files_struct *fsp,
1114                                 enum file_close_type close_type)
1115 {
1116         struct share_mode_lock *lck = NULL;
1117         bool delete_dir = False;
1118         NTSTATUS status = NT_STATUS_OK;
1119         NTSTATUS status1 = NT_STATUS_OK;
1120         const struct security_token *del_nt_token = NULL;
1121         const struct security_unix_token *del_token = NULL;
1122         NTSTATUS notify_status;
1123
1124         if (fsp->conn->sconn->using_smb2) {
1125                 notify_status = STATUS_NOTIFY_CLEANUP;
1126         } else {
1127                 notify_status = NT_STATUS_OK;
1128         }
1129
1130         if (fsp->num_aio_requests != 0) {
1131                 if (close_type != SHUTDOWN_CLOSE) {
1132                         /*
1133                          * We panic here because if we close() the fd while we
1134                          * have outstanding async I/O requests, an async IO
1135                          * request might use the fd. For directories the fd is
1136                          * read-only, so this is not as bad as with files, but
1137                          * still, better safe then sorry.
1138                          */
1139                         DBG_ERR("fsp->num_aio_requests=%u\n",
1140                                 fsp->num_aio_requests);
1141                         smb_panic("close with outstanding aio requests");
1142                         return NT_STATUS_INTERNAL_ERROR;
1143                 }
1144
1145                 while (fsp->num_aio_requests != 0) {
1146                         /*
1147                          * The destructor of the req will remove itself from the
1148                          * fsp.  Don't use TALLOC_FREE here, this will overwrite
1149                          * what the destructor just wrote into aio_requests[0].
1150                          */
1151                         talloc_free(fsp->aio_requests[0]);
1152                 }
1153         }
1154
1155         /*
1156          * NT can set delete_on_close of the last open
1157          * reference to a directory also.
1158          */
1159
1160         lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
1161         if (lck == NULL) {
1162                 DEBUG(0, ("close_directory: Could not get share mode lock for "
1163                           "%s\n", fsp_str_dbg(fsp)));
1164                 file_free(req, fsp);
1165                 return NT_STATUS_INVALID_PARAMETER;
1166         }
1167
1168         if (fsp->initial_delete_on_close) {
1169                 bool became_user = False;
1170
1171                 /* Initial delete on close was set - for
1172                  * directories we don't care if anyone else
1173                  * wrote a real delete on close. */
1174
1175                 if (get_current_vuid(fsp->conn) != fsp->vuid) {
1176                         become_user_without_service(fsp->conn, fsp->vuid);
1177                         became_user = True;
1178                 }
1179                 send_stat_cache_delete_message(fsp->conn->sconn->msg_ctx,
1180                                                fsp->fsp_name->base_name);
1181                 set_delete_on_close_lck(fsp, lck,
1182                                 get_current_nttok(fsp->conn),
1183                                 get_current_utok(fsp->conn));
1184                 fsp->delete_on_close = true;
1185                 if (became_user) {
1186                         unbecome_user_without_service();
1187                 }
1188         }
1189
1190         delete_dir = get_delete_on_close_token(
1191                 lck, fsp->name_hash, &del_nt_token, &del_token) &&
1192                 !has_other_nonposix_opens(lck, fsp);
1193
1194         if ((close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE) &&
1195                                 delete_dir) {
1196         
1197                 /* Become the user who requested the delete. */
1198
1199                 if (!push_sec_ctx()) {
1200                         smb_panic("close_directory: failed to push sec_ctx.\n");
1201                 }
1202
1203                 set_sec_ctx(del_token->uid,
1204                                 del_token->gid,
1205                                 del_token->ngroups,
1206                                 del_token->groups,
1207                                 del_nt_token);
1208
1209                 if (!del_share_mode(lck, fsp)) {
1210                         DEBUG(0, ("close_directory: Could not delete share entry for "
1211                                   "%s\n", fsp_str_dbg(fsp)));
1212                 }
1213
1214                 TALLOC_FREE(lck);
1215
1216                 if ((fsp->conn->fs_capabilities & FILE_NAMED_STREAMS)
1217                     && !is_ntfs_stream_smb_fname(fsp->fsp_name)) {
1218
1219                         status = delete_all_streams(fsp->conn, fsp->fsp_name);
1220                         if (!NT_STATUS_IS_OK(status)) {
1221                                 DEBUG(5, ("delete_all_streams failed: %s\n",
1222                                           nt_errstr(status)));
1223                                 file_free(req, fsp);
1224                                 return status;
1225                         }
1226                 }
1227
1228                 status = rmdir_internals(talloc_tos(), fsp);
1229
1230                 DEBUG(5,("close_directory: %s. Delete on close was set - "
1231                          "deleting directory returned %s.\n",
1232                          fsp_str_dbg(fsp), nt_errstr(status)));
1233
1234                 /* unbecome user. */
1235                 pop_sec_ctx();
1236
1237                 /*
1238                  * Ensure we remove any change notify requests that would
1239                  * now fail as the directory has been deleted.
1240                  */
1241
1242                 if (NT_STATUS_IS_OK(status)) {
1243                         notify_status = NT_STATUS_DELETE_PENDING;
1244                 }
1245         } else {
1246                 if (!del_share_mode(lck, fsp)) {
1247                         DEBUG(0, ("close_directory: Could not delete share entry for "
1248                                   "%s\n", fsp_str_dbg(fsp)));
1249                 }
1250
1251                 TALLOC_FREE(lck);
1252         }
1253
1254         remove_pending_change_notify_requests_by_fid(fsp, notify_status);
1255
1256         status1 = fd_close(fsp);
1257
1258         if (!NT_STATUS_IS_OK(status1)) {
1259                 DEBUG(0, ("Could not close dir! fname=%s, fd=%d, err=%d=%s\n",
1260                           fsp_str_dbg(fsp), fsp->fh->fd, errno,
1261                           strerror(errno)));
1262         }
1263
1264         /*
1265          * Do the code common to files and directories.
1266          */
1267         file_free(req, fsp);
1268
1269         if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(status1)) {
1270                 status = status1;
1271         }
1272         return status;
1273 }
1274
1275 /****************************************************************************
1276  Close a files_struct.
1277 ****************************************************************************/
1278   
1279 NTSTATUS close_file(struct smb_request *req, files_struct *fsp,
1280                     enum file_close_type close_type)
1281 {
1282         NTSTATUS status;
1283         struct files_struct *base_fsp = fsp->base_fsp;
1284
1285         if(fsp->is_directory) {
1286                 status = close_directory(req, fsp, close_type);
1287         } else if (fsp->fake_file_handle != NULL) {
1288                 status = close_fake_file(req, fsp);
1289         } else {
1290                 status = close_normal_file(req, fsp, close_type);
1291         }
1292
1293         if ((base_fsp != NULL) && (close_type != SHUTDOWN_CLOSE)) {
1294
1295                 /*
1296                  * fsp was a stream, the base fsp can't be a stream as well
1297                  *
1298                  * For SHUTDOWN_CLOSE this is not possible here, because
1299                  * SHUTDOWN_CLOSE only happens from files.c which walks the
1300                  * complete list of files. If we mess with more than one fsp
1301                  * those loops will become confused.
1302                  */
1303
1304                 SMB_ASSERT(base_fsp->base_fsp == NULL);
1305                 close_file(req, base_fsp, close_type);
1306         }
1307
1308         return status;
1309 }
1310
1311 /****************************************************************************
1312  Deal with an (authorized) message to close a file given the share mode
1313  entry.
1314 ****************************************************************************/
1315
1316 void msg_close_file(struct messaging_context *msg_ctx,
1317                         void *private_data,
1318                         uint32_t msg_type,
1319                         struct server_id server_id,
1320                         DATA_BLOB *data)
1321 {
1322         files_struct *fsp = NULL;
1323         struct file_id id;
1324         struct share_mode_entry e;
1325         struct smbd_server_connection *sconn =
1326                 talloc_get_type_abort(private_data,
1327                 struct smbd_server_connection);
1328
1329         message_to_share_mode_entry(&id, &e, (char *)data->data);
1330
1331         if(DEBUGLVL(10)) {
1332                 char *sm_str = share_mode_str(NULL, 0, &id, &e);
1333                 if (!sm_str) {
1334                         smb_panic("talloc failed");
1335                 }
1336                 DEBUG(10,("msg_close_file: got request to close share mode "
1337                         "entry %s\n", sm_str));
1338                 TALLOC_FREE(sm_str);
1339         }
1340
1341         fsp = file_find_dif(sconn, id, e.share_file_id);
1342         if (!fsp) {
1343                 DEBUG(10,("msg_close_file: failed to find file.\n"));
1344                 return;
1345         }
1346         close_file(NULL, fsp, NORMAL_CLOSE);
1347 }