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