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