fb0e20647e2676cc168d37950b52fab75576a6ff
[nivanova/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 "printing.h"
24 #include "librpc/gen_ndr/messaging.h"
25 #include "smbd/globals.h"
26
27 /****************************************************************************
28  Run a file if it is a magic script.
29 ****************************************************************************/
30
31 static NTSTATUS check_magic(struct files_struct *fsp)
32 {
33         int ret;
34         const char *magic_output = NULL;
35         SMB_STRUCT_STAT st;
36         int tmp_fd, outfd;
37         TALLOC_CTX *ctx = NULL;
38         const char *p;
39         struct connection_struct *conn = fsp->conn;
40         char *fname = NULL;
41         NTSTATUS status;
42
43         if (!*lp_magicscript(SNUM(conn))) {
44                 return NT_STATUS_OK;
45         }
46
47         DEBUG(5,("checking magic for %s\n", fsp_str_dbg(fsp)));
48
49         ctx = talloc_stackframe();
50
51         fname = fsp->fsp_name->base_name;
52
53         if (!(p = strrchr_m(fname,'/'))) {
54                 p = fname;
55         } else {
56                 p++;
57         }
58
59         if (!strequal(lp_magicscript(SNUM(conn)),p)) {
60                 status = NT_STATUS_OK;
61                 goto out;
62         }
63
64         if (*lp_magicoutput(SNUM(conn))) {
65                 magic_output = lp_magicoutput(SNUM(conn));
66         } else {
67                 magic_output = talloc_asprintf(ctx,
68                                 "%s.out",
69                                 fname);
70         }
71         if (!magic_output) {
72                 status = NT_STATUS_NO_MEMORY;
73                 goto out;
74         }
75
76         /* Ensure we don't depend on user's PATH. */
77         p = talloc_asprintf(ctx, "./%s", fname);
78         if (!p) {
79                 status = NT_STATUS_NO_MEMORY;
80                 goto out;
81         }
82
83         if (chmod(fname, 0755) == -1) {
84                 status = map_nt_error_from_unix(errno);
85                 goto out;
86         }
87         ret = smbrun(p,&tmp_fd);
88         DEBUG(3,("Invoking magic command %s gave %d\n",
89                 p,ret));
90
91         unlink(fname);
92         if (ret != 0 || tmp_fd == -1) {
93                 if (tmp_fd != -1) {
94                         close(tmp_fd);
95                 }
96                 status = NT_STATUS_UNSUCCESSFUL;
97                 goto out;
98         }
99         outfd = open(magic_output, O_CREAT|O_EXCL|O_RDWR, 0600);
100         if (outfd == -1) {
101                 int err = errno;
102                 close(tmp_fd);
103                 status = map_nt_error_from_unix(err);
104                 goto out;
105         }
106
107         if (sys_fstat(tmp_fd, &st, false) == -1) {
108                 int err = errno;
109                 close(tmp_fd);
110                 close(outfd);
111                 status = map_nt_error_from_unix(err);
112                 goto out;
113         }
114
115         if (transfer_file(tmp_fd,outfd,(SMB_OFF_T)st.st_ex_size) == (SMB_OFF_T)-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         close(tmp_fd);
123         if (close(outfd) == -1) {
124                 status = map_nt_error_from_unix(errno);
125                 goto out;
126         }
127
128         status = NT_STATUS_OK;
129
130  out:
131         TALLOC_FREE(ctx);
132         return status;
133 }
134
135 /****************************************************************************
136   Common code to close a file or a directory.
137 ****************************************************************************/
138
139 static NTSTATUS close_filestruct(files_struct *fsp)
140 {
141         NTSTATUS status = NT_STATUS_OK;
142
143         if (fsp->fh->fd != -1) {
144                 if(flush_write_cache(fsp, CLOSE_FLUSH) == -1) {
145                         status = map_nt_error_from_unix(errno);
146                 }
147                 delete_write_cache(fsp);
148         }
149
150         return status;
151 }
152
153 /****************************************************************************
154  If any deferred opens are waiting on this close, notify them.
155 ****************************************************************************/
156
157 static void notify_deferred_opens(struct messaging_context *msg_ctx,
158                                   struct share_mode_lock *lck)
159 {
160         int i;
161
162         if (!should_notify_deferred_opens()) {
163                 return;
164         }
165  
166         for (i=0; i<lck->num_share_modes; i++) {
167                 struct share_mode_entry *e = &lck->share_modes[i];
168  
169                 if (!is_deferred_open_entry(e)) {
170                         continue;
171                 }
172  
173                 if (procid_is_me(&e->pid)) {
174                         /*
175                          * We need to notify ourself to retry the open.  Do
176                          * this by finding the queued SMB record, moving it to
177                          * the head of the queue and changing the wait time to
178                          * zero.
179                          */
180                         schedule_deferred_open_message_smb(e->op_mid);
181                 } else {
182                         char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
183
184                         share_mode_entry_to_message(msg, e);
185
186                         messaging_send_buf(msg_ctx, e->pid, MSG_SMB_OPEN_RETRY,
187                                            (uint8 *)msg,
188                                            MSG_SMB_SHARE_MODE_ENTRY_SIZE);
189                 }
190         }
191 }
192
193 /****************************************************************************
194  Delete all streams
195 ****************************************************************************/
196
197 NTSTATUS delete_all_streams(connection_struct *conn, const char *fname)
198 {
199         struct stream_struct *stream_info;
200         int i;
201         unsigned int num_streams;
202         TALLOC_CTX *frame = talloc_stackframe();
203         NTSTATUS status;
204
205         status = SMB_VFS_STREAMINFO(conn, NULL, fname, talloc_tos(),
206                                     &num_streams, &stream_info);
207
208         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
209                 DEBUG(10, ("no streams around\n"));
210                 TALLOC_FREE(frame);
211                 return NT_STATUS_OK;
212         }
213
214         if (!NT_STATUS_IS_OK(status)) {
215                 DEBUG(10, ("SMB_VFS_STREAMINFO failed: %s\n",
216                            nt_errstr(status)));
217                 goto fail;
218         }
219
220         DEBUG(10, ("delete_all_streams found %d streams\n",
221                    num_streams));
222
223         if (num_streams == 0) {
224                 TALLOC_FREE(frame);
225                 return NT_STATUS_OK;
226         }
227
228         for (i=0; i<num_streams; i++) {
229                 int res;
230                 struct smb_filename *smb_fname_stream = NULL;
231
232                 if (strequal(stream_info[i].name, "::$DATA")) {
233                         continue;
234                 }
235
236                 status = create_synthetic_smb_fname(talloc_tos(), fname,
237                                                     stream_info[i].name, NULL,
238                                                     &smb_fname_stream);
239
240                 if (!NT_STATUS_IS_OK(status)) {
241                         DEBUG(0, ("talloc_aprintf failed\n"));
242                         goto fail;
243                 }
244
245                 res = SMB_VFS_UNLINK(conn, smb_fname_stream);
246
247                 if (res == -1) {
248                         status = map_nt_error_from_unix(errno);
249                         DEBUG(10, ("Could not delete stream %s: %s\n",
250                                    smb_fname_str_dbg(smb_fname_stream),
251                                    strerror(errno)));
252                         TALLOC_FREE(smb_fname_stream);
253                         break;
254                 }
255                 TALLOC_FREE(smb_fname_stream);
256         }
257
258  fail:
259         TALLOC_FREE(frame);
260         return status;
261 }
262
263 /****************************************************************************
264  Deal with removing a share mode on last close.
265 ****************************************************************************/
266
267 static NTSTATUS close_remove_share_mode(files_struct *fsp,
268                                         enum file_close_type close_type)
269 {
270         connection_struct *conn = fsp->conn;
271         bool delete_file = false;
272         bool changed_user = false;
273         struct share_mode_lock *lck = NULL;
274         NTSTATUS status = NT_STATUS_OK;
275         NTSTATUS tmp_status;
276         struct file_id id;
277
278         /* Ensure any pending write time updates are done. */
279         if (fsp->update_write_time_event) {
280                 update_write_time_handler(smbd_event_context(),
281                                         fsp->update_write_time_event,
282                                         timeval_current(),
283                                         (void *)fsp);
284         }
285
286         /*
287          * Lock the share entries, and determine if we should delete
288          * on close. If so delete whilst the lock is still in effect.
289          * This prevents race conditions with the file being created. JRA.
290          */
291
292         lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
293                                   NULL);
294
295         if (lck == NULL) {
296                 DEBUG(0, ("close_remove_share_mode: Could not get share mode "
297                           "lock for file %s\n", fsp_str_dbg(fsp)));
298                 status = NT_STATUS_INVALID_PARAMETER;
299                 goto done;
300         }
301
302         if (fsp->write_time_forced) {
303                 DEBUG(10,("close_remove_share_mode: write time forced "
304                         "for file %s\n",
305                         fsp_str_dbg(fsp)));
306                 set_close_write_time(fsp, lck->changed_write_time);
307         } else if (fsp->update_write_time_on_close) {
308                 /* Someone had a pending write. */
309                 if (null_timespec(fsp->close_write_time)) {
310                         DEBUG(10,("close_remove_share_mode: update to current time "
311                                 "for file %s\n",
312                                 fsp_str_dbg(fsp)));
313                         /* Update to current time due to "normal" write. */
314                         set_close_write_time(fsp, timespec_current());
315                 } else {
316                         DEBUG(10,("close_remove_share_mode: write time pending "
317                                 "for file %s\n",
318                                 fsp_str_dbg(fsp)));
319                         /* Update to time set on close call. */
320                         set_close_write_time(fsp, fsp->close_write_time);
321                 }
322         }
323
324         if (!del_share_mode(lck, fsp)) {
325                 DEBUG(0, ("close_remove_share_mode: Could not delete share "
326                           "entry for file %s\n",
327                           fsp_str_dbg(fsp)));
328         }
329
330         if (fsp->initial_delete_on_close && (lck->delete_token == NULL)) {
331                 bool became_user = False;
332
333                 /* Initial delete on close was set and no one else
334                  * wrote a real delete on close. */
335
336                 if (get_current_vuid(conn) != fsp->vuid) {
337                         become_user(conn, fsp->vuid);
338                         became_user = True;
339                 }
340                 fsp->delete_on_close = true;
341                 set_delete_on_close_lck(lck, True, get_current_utok(conn));
342                 if (became_user) {
343                         unbecome_user();
344                 }
345         }
346
347         delete_file = lck->delete_on_close;
348
349         if (delete_file) {
350                 int i;
351                 /* See if others still have the file open. If this is the
352                  * case, then don't delete. If all opens are POSIX delete now. */
353                 for (i=0; i<lck->num_share_modes; i++) {
354                         struct share_mode_entry *e = &lck->share_modes[i];
355                         if (is_valid_share_mode_entry(e)) {
356                                 if (fsp->posix_open && (e->flags & SHARE_MODE_FLAG_POSIX_OPEN)) {
357                                         continue;
358                                 }
359                                 delete_file = False;
360                                 break;
361                         }
362                 }
363         }
364
365         /* Notify any deferred opens waiting on this close. */
366         notify_deferred_opens(conn->sconn->msg_ctx, lck);
367         reply_to_oplock_break_requests(fsp);
368
369         /*
370          * NT can set delete_on_close of the last open
371          * reference to a file.
372          */
373
374         if (!(close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE)
375             || !delete_file
376             || (lck->delete_token == NULL)) {
377                 TALLOC_FREE(lck);
378                 return NT_STATUS_OK;
379         }
380
381         /*
382          * Ok, we have to delete the file
383          */
384
385         DEBUG(5,("close_remove_share_mode: file %s. Delete on close was set "
386                  "- deleting file.\n", fsp_str_dbg(fsp)));
387
388         /*
389          * Don't try to update the write time when we delete the file
390          */
391         fsp->update_write_time_on_close = false;
392
393         if (!unix_token_equal(lck->delete_token, get_current_utok(conn))) {
394                 /* Become the user who requested the delete. */
395
396                 DEBUG(5,("close_remove_share_mode: file %s. "
397                         "Change user to uid %u\n",
398                         fsp_str_dbg(fsp),
399                         (unsigned int)lck->delete_token->uid));
400
401                 if (!push_sec_ctx()) {
402                         smb_panic("close_remove_share_mode: file %s. failed to push "
403                                   "sec_ctx.\n");
404                 }
405
406                 set_sec_ctx(lck->delete_token->uid,
407                             lck->delete_token->gid,
408                             lck->delete_token->ngroups,
409                             lck->delete_token->groups,
410                             NULL);
411
412                 changed_user = true;
413         }
414
415         /* We can only delete the file if the name we have is still valid and
416            hasn't been renamed. */
417
418         tmp_status = vfs_stat_fsp(fsp);
419         if (!NT_STATUS_IS_OK(tmp_status)) {
420                 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
421                          "was set and stat failed with error %s\n",
422                          fsp_str_dbg(fsp), nt_errstr(tmp_status)));
423                 /*
424                  * Don't save the errno here, we ignore this error
425                  */
426                 goto done;
427         }
428
429         id = vfs_file_id_from_sbuf(conn, &fsp->fsp_name->st);
430
431         if (!file_id_equal(&fsp->file_id, &id)) {
432                 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
433                          "was set and dev and/or inode does not match\n",
434                          fsp_str_dbg(fsp)));
435                 DEBUG(5,("close_remove_share_mode: file %s. stored file_id %s, "
436                          "stat file_id %s\n",
437                          fsp_str_dbg(fsp),
438                          file_id_string_tos(&fsp->file_id),
439                          file_id_string_tos(&id)));
440                 /*
441                  * Don't save the errno here, we ignore this error
442                  */
443                 goto done;
444         }
445
446         if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
447             && !is_ntfs_stream_smb_fname(fsp->fsp_name)) {
448
449                 status = delete_all_streams(conn, fsp->fsp_name->base_name);
450
451                 if (!NT_STATUS_IS_OK(status)) {
452                         DEBUG(5, ("delete_all_streams failed: %s\n",
453                                   nt_errstr(status)));
454                         goto done;
455                 }
456         }
457
458
459         if (SMB_VFS_UNLINK(conn, fsp->fsp_name) != 0) {
460                 /*
461                  * This call can potentially fail as another smbd may
462                  * have had the file open with delete on close set and
463                  * deleted it when its last reference to this file
464                  * went away. Hence we log this but not at debug level
465                  * zero.
466                  */
467
468                 DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
469                          "was set and unlink failed with error %s\n",
470                          fsp_str_dbg(fsp), strerror(errno)));
471
472                 status = map_nt_error_from_unix(errno);
473         }
474
475         notify_fname(conn, NOTIFY_ACTION_REMOVED,
476                      FILE_NOTIFY_CHANGE_FILE_NAME,
477                      fsp->fsp_name->base_name);
478
479         /* As we now have POSIX opens which can unlink
480          * with other open files we may have taken
481          * this code path with more than one share mode
482          * entry - ensure we only delete once by resetting
483          * the delete on close flag. JRA.
484          */
485
486         fsp->delete_on_close = false;
487         set_delete_on_close_lck(lck, False, NULL);
488
489  done:
490
491         if (changed_user) {
492                 /* unbecome user. */
493                 pop_sec_ctx();
494         }
495
496         TALLOC_FREE(lck);
497         return status;
498 }
499
500 void set_close_write_time(struct files_struct *fsp, struct timespec ts)
501 {
502         DEBUG(6,("close_write_time: %s" , time_to_asc(convert_timespec_to_time_t(ts))));
503
504         if (null_timespec(ts)) {
505                 return;
506         }
507         fsp->write_time_forced = false;
508         fsp->update_write_time_on_close = true;
509         fsp->close_write_time = ts;
510 }
511
512 static NTSTATUS update_write_time_on_close(struct files_struct *fsp)
513 {
514         struct smb_file_time ft;
515         NTSTATUS status;
516         struct share_mode_lock *lck = NULL;
517
518         ZERO_STRUCT(ft);
519
520         if (!fsp->update_write_time_on_close) {
521                 return NT_STATUS_OK;
522         }
523
524         if (null_timespec(fsp->close_write_time)) {
525                 fsp->close_write_time = timespec_current();
526         }
527
528         /* Ensure we have a valid stat struct for the source. */
529         status = vfs_stat_fsp(fsp);
530         if (!NT_STATUS_IS_OK(status)) {
531                 return status;
532         }
533
534         if (!VALID_STAT(fsp->fsp_name->st)) {
535                 /* if it doesn't seem to be a real file */
536                 return NT_STATUS_OK;
537         }
538
539         /* On close if we're changing the real file time we
540          * must update it in the open file db too. */
541         (void)set_write_time(fsp->file_id, fsp->close_write_time);
542
543         lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL, NULL);
544         if (lck) {
545                 /* Close write times overwrite sticky write times
546                    so we must replace any sticky write time here. */
547                 if (!null_timespec(lck->changed_write_time)) {
548                         (void)set_sticky_write_time(fsp->file_id, fsp->close_write_time);
549                 }
550                 TALLOC_FREE(lck);
551         }
552
553         ft.mtime = fsp->close_write_time;
554         status = smb_set_file_time(fsp->conn, fsp, fsp->fsp_name, &ft, false);
555         if (!NT_STATUS_IS_OK(status)) {
556                 return status;
557         }
558
559         return status;
560 }
561
562 static NTSTATUS ntstatus_keeperror(NTSTATUS s1, NTSTATUS s2)
563 {
564         if (!NT_STATUS_IS_OK(s1)) {
565                 return s1;
566         }
567         return s2;
568 }
569
570 /****************************************************************************
571  Close a file.
572
573  close_type can be NORMAL_CLOSE=0,SHUTDOWN_CLOSE,ERROR_CLOSE.
574  printing and magic scripts are only run on normal close.
575  delete on close is done on normal and shutdown close.
576 ****************************************************************************/
577
578 static NTSTATUS close_normal_file(struct smb_request *req, files_struct *fsp,
579                                   enum file_close_type close_type)
580 {
581         NTSTATUS status = NT_STATUS_OK;
582         NTSTATUS tmp;
583         connection_struct *conn = fsp->conn;
584
585         if (close_type == ERROR_CLOSE) {
586                 cancel_aio_by_fsp(fsp);
587         } else {
588                 /*
589                  * If we're finishing async io on a close we can get a write
590                  * error here, we must remember this.
591                  */
592                 int ret = wait_for_aio_completion(fsp);
593                 if (ret) {
594                         status = ntstatus_keeperror(
595                                 status, map_nt_error_from_unix(ret));
596                 }
597         }
598
599         /*
600          * If we're flushing on a close we can get a write
601          * error here, we must remember this.
602          */
603
604         tmp = close_filestruct(fsp);
605         status = ntstatus_keeperror(status, tmp);
606
607         if (fsp->print_file) {
608                 /* FIXME: return spool errors */
609                 print_spool_end(fsp, close_type);
610                 file_free(req, fsp);
611                 return NT_STATUS_OK;
612         }
613
614         /* Remove the oplock before potentially deleting the file. */
615         if(fsp->oplock_type) {
616                 release_file_oplock(fsp);
617         }
618
619         /* If this is an old DOS or FCB open and we have multiple opens on
620            the same handle we only have one share mode. Ensure we only remove
621            the share mode on the last close. */
622
623         if (fsp->fh->ref_count == 1) {
624                 /* Should we return on error here... ? */
625                 tmp = close_remove_share_mode(fsp, close_type);
626                 status = ntstatus_keeperror(status, tmp);
627         }
628
629         locking_close_file(conn->sconn->msg_ctx, fsp, close_type);
630
631         tmp = fd_close(fsp);
632         status = ntstatus_keeperror(status, tmp);
633
634         /* check for magic scripts */
635         if (close_type == NORMAL_CLOSE) {
636                 tmp = check_magic(fsp);
637                 status = ntstatus_keeperror(status, tmp);
638         }
639
640         /*
641          * Ensure pending modtime is set after close.
642          */
643
644         tmp = update_write_time_on_close(fsp);
645         if (NT_STATUS_EQUAL(tmp, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
646                 /* Someone renamed the file or a parent directory containing
647                  * this file. We can't do anything about this, we don't have
648                  * an "update timestamp by fd" call in POSIX. Eat the error. */
649
650                 tmp = NT_STATUS_OK;
651         }
652
653         status = ntstatus_keeperror(status, tmp);
654
655         DEBUG(2,("%s closed file %s (numopen=%d) %s\n",
656                 conn->server_info->unix_name, fsp_str_dbg(fsp),
657                 conn->num_files_open - 1,
658                 nt_errstr(status) ));
659
660         file_free(req, fsp);
661         return status;
662 }
663 /****************************************************************************
664  Static function used by reply_rmdir to delete an entire directory
665  tree recursively. Return True on ok, False on fail.
666 ****************************************************************************/
667
668 static bool recursive_rmdir(TALLOC_CTX *ctx,
669                         connection_struct *conn,
670                         struct smb_filename *smb_dname)
671 {
672         const char *dname = NULL;
673         char *talloced = NULL;
674         bool ret = True;
675         long offset = 0;
676         SMB_STRUCT_STAT st;
677         struct smb_Dir *dir_hnd;
678
679         SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname));
680
681         dir_hnd = OpenDir(talloc_tos(), conn, smb_dname->base_name, NULL, 0);
682         if(dir_hnd == NULL)
683                 return False;
684
685         while((dname = ReadDirName(dir_hnd, &offset, &st, &talloced))) {
686                 struct smb_filename *smb_dname_full = NULL;
687                 char *fullname = NULL;
688                 bool do_break = true;
689                 NTSTATUS status;
690
691                 if (ISDOT(dname) || ISDOTDOT(dname)) {
692                         TALLOC_FREE(talloced);
693                         continue;
694                 }
695
696                 if (!is_visible_file(conn, smb_dname->base_name, dname, &st,
697                                      false)) {
698                         TALLOC_FREE(talloced);
699                         continue;
700                 }
701
702                 /* Construct the full name. */
703                 fullname = talloc_asprintf(ctx,
704                                 "%s/%s",
705                                 smb_dname->base_name,
706                                 dname);
707                 if (!fullname) {
708                         errno = ENOMEM;
709                         goto err_break;
710                 }
711
712                 status = create_synthetic_smb_fname(talloc_tos(), fullname,
713                                                     NULL, NULL,
714                                                     &smb_dname_full);
715                 if (!NT_STATUS_IS_OK(status)) {
716                         goto err_break;
717                 }
718
719                 if(SMB_VFS_LSTAT(conn, smb_dname_full) != 0) {
720                         goto err_break;
721                 }
722
723                 if(smb_dname_full->st.st_ex_mode & S_IFDIR) {
724                         if(!recursive_rmdir(ctx, conn, smb_dname_full)) {
725                                 goto err_break;
726                         }
727                         if(SMB_VFS_RMDIR(conn,
728                                          smb_dname_full->base_name) != 0) {
729                                 goto err_break;
730                         }
731                 } else if(SMB_VFS_UNLINK(conn, smb_dname_full) != 0) {
732                         goto err_break;
733                 }
734
735                 /* Successful iteration. */
736                 do_break = false;
737
738          err_break:
739                 TALLOC_FREE(smb_dname_full);
740                 TALLOC_FREE(fullname);
741                 TALLOC_FREE(talloced);
742                 if (do_break) {
743                         ret = false;
744                         break;
745                 }
746         }
747         TALLOC_FREE(dir_hnd);
748         return ret;
749 }
750
751 /****************************************************************************
752  The internals of the rmdir code - called elsewhere.
753 ****************************************************************************/
754
755 static NTSTATUS rmdir_internals(TALLOC_CTX *ctx, files_struct *fsp)
756 {
757         connection_struct *conn = fsp->conn;
758         struct smb_filename *smb_dname = fsp->fsp_name;
759         int ret;
760
761         SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname));
762
763         /* Might be a symlink. */
764         if(SMB_VFS_LSTAT(conn, smb_dname) != 0) {
765                 return map_nt_error_from_unix(errno);
766         }
767
768         if (S_ISLNK(smb_dname->st.st_ex_mode)) {
769                 /* Is what it points to a directory ? */
770                 if(SMB_VFS_STAT(conn, smb_dname) != 0) {
771                         return map_nt_error_from_unix(errno);
772                 }
773                 if (!(S_ISDIR(smb_dname->st.st_ex_mode))) {
774                         return NT_STATUS_NOT_A_DIRECTORY;
775                 }
776                 ret = SMB_VFS_UNLINK(conn, smb_dname);
777         } else {
778                 ret = SMB_VFS_RMDIR(conn, smb_dname->base_name);
779         }
780         if (ret == 0) {
781                 notify_fname(conn, NOTIFY_ACTION_REMOVED,
782                              FILE_NOTIFY_CHANGE_DIR_NAME,
783                              smb_dname->base_name);
784                 return NT_STATUS_OK;
785         }
786
787         if(((errno == ENOTEMPTY)||(errno == EEXIST)) && lp_veto_files(SNUM(conn))) {
788                 /*
789                  * Check to see if the only thing in this directory are
790                  * vetoed files/directories. If so then delete them and
791                  * retry. If we fail to delete any of them (and we *don't*
792                  * do a recursive delete) then fail the rmdir.
793                  */
794                 SMB_STRUCT_STAT st;
795                 const char *dname = NULL;
796                 char *talloced = NULL;
797                 long dirpos = 0;
798                 struct smb_Dir *dir_hnd = OpenDir(talloc_tos(), conn,
799                                                   smb_dname->base_name, NULL,
800                                                   0);
801
802                 if(dir_hnd == NULL) {
803                         errno = ENOTEMPTY;
804                         goto err;
805                 }
806
807                 while ((dname = ReadDirName(dir_hnd, &dirpos, &st,
808                                             &talloced)) != NULL) {
809                         if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0)) {
810                                 TALLOC_FREE(talloced);
811                                 continue;
812                         }
813                         if (!is_visible_file(conn, smb_dname->base_name, dname,
814                                              &st, false)) {
815                                 TALLOC_FREE(talloced);
816                                 continue;
817                         }
818                         if(!IS_VETO_PATH(conn, dname)) {
819                                 TALLOC_FREE(dir_hnd);
820                                 TALLOC_FREE(talloced);
821                                 errno = ENOTEMPTY;
822                                 goto err;
823                         }
824                         TALLOC_FREE(talloced);
825                 }
826
827                 /* We only have veto files/directories.
828                  * Are we allowed to delete them ? */
829
830                 if(!lp_recursive_veto_delete(SNUM(conn))) {
831                         TALLOC_FREE(dir_hnd);
832                         errno = ENOTEMPTY;
833                         goto err;
834                 }
835
836                 /* Do a recursive delete. */
837                 RewindDir(dir_hnd,&dirpos);
838                 while ((dname = ReadDirName(dir_hnd, &dirpos, &st,
839                                             &talloced)) != NULL) {
840                         struct smb_filename *smb_dname_full = NULL;
841                         char *fullname = NULL;
842                         bool do_break = true;
843                         NTSTATUS status;
844
845                         if (ISDOT(dname) || ISDOTDOT(dname)) {
846                                 TALLOC_FREE(talloced);
847                                 continue;
848                         }
849                         if (!is_visible_file(conn, smb_dname->base_name, dname,
850                                              &st, false)) {
851                                 TALLOC_FREE(talloced);
852                                 continue;
853                         }
854
855                         fullname = talloc_asprintf(ctx,
856                                         "%s/%s",
857                                         smb_dname->base_name,
858                                         dname);
859
860                         if(!fullname) {
861                                 errno = ENOMEM;
862                                 goto err_break;
863                         }
864
865                         status = create_synthetic_smb_fname(talloc_tos(),
866                                                             fullname, NULL,
867                                                             NULL,
868                                                             &smb_dname_full);
869                         if (!NT_STATUS_IS_OK(status)) {
870                                 errno = map_errno_from_nt_status(status);
871                                 goto err_break;
872                         }
873
874                         if(SMB_VFS_LSTAT(conn, smb_dname_full) != 0) {
875                                 goto err_break;
876                         }
877                         if(smb_dname_full->st.st_ex_mode & S_IFDIR) {
878                                 if(!recursive_rmdir(ctx, conn,
879                                                     smb_dname_full)) {
880                                         goto err_break;
881                                 }
882                                 if(SMB_VFS_RMDIR(conn,
883                                         smb_dname_full->base_name) != 0) {
884                                         goto err_break;
885                                 }
886                         } else if(SMB_VFS_UNLINK(conn, smb_dname_full) != 0) {
887                                 goto err_break;
888                         }
889
890                         /* Successful iteration. */
891                         do_break = false;
892
893                  err_break:
894                         TALLOC_FREE(fullname);
895                         TALLOC_FREE(smb_dname_full);
896                         TALLOC_FREE(talloced);
897                         if (do_break)
898                                 break;
899                 }
900                 TALLOC_FREE(dir_hnd);
901                 /* Retry the rmdir */
902                 ret = SMB_VFS_RMDIR(conn, smb_dname->base_name);
903         }
904
905   err:
906
907         if (ret != 0) {
908                 DEBUG(3,("rmdir_internals: couldn't remove directory %s : "
909                          "%s\n", smb_fname_str_dbg(smb_dname),
910                          strerror(errno)));
911                 return map_nt_error_from_unix(errno);
912         }
913
914         notify_fname(conn, NOTIFY_ACTION_REMOVED,
915                      FILE_NOTIFY_CHANGE_DIR_NAME,
916                      smb_dname->base_name);
917
918         return NT_STATUS_OK;
919 }
920
921 /****************************************************************************
922  Close a directory opened by an NT SMB call. 
923 ****************************************************************************/
924   
925 static NTSTATUS close_directory(struct smb_request *req, files_struct *fsp,
926                                 enum file_close_type close_type)
927 {
928         struct share_mode_lock *lck = NULL;
929         bool delete_dir = False;
930         NTSTATUS status = NT_STATUS_OK;
931         NTSTATUS status1 = NT_STATUS_OK;
932
933         /*
934          * NT can set delete_on_close of the last open
935          * reference to a directory also.
936          */
937
938         lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
939                                   NULL);
940
941         if (lck == NULL) {
942                 DEBUG(0, ("close_directory: Could not get share mode lock for "
943                           "%s\n", fsp_str_dbg(fsp)));
944                 status = NT_STATUS_INVALID_PARAMETER;
945                 goto out;
946         }
947
948         if (!del_share_mode(lck, fsp)) {
949                 DEBUG(0, ("close_directory: Could not delete share entry for "
950                           "%s\n", fsp_str_dbg(fsp)));
951         }
952
953         if (fsp->initial_delete_on_close) {
954                 bool became_user = False;
955
956                 /* Initial delete on close was set - for
957                  * directories we don't care if anyone else
958                  * wrote a real delete on close. */
959
960                 if (get_current_vuid(fsp->conn) != fsp->vuid) {
961                         become_user(fsp->conn, fsp->vuid);
962                         became_user = True;
963                 }
964                 send_stat_cache_delete_message(fsp->fsp_name->base_name);
965                 set_delete_on_close_lck(lck, True, get_current_utok(fsp->conn));
966                 fsp->delete_on_close = true;
967                 if (became_user) {
968                         unbecome_user();
969                 }
970         }
971
972         delete_dir = lck->delete_on_close;
973
974         if (delete_dir) {
975                 int i;
976                 /* See if others still have the dir open. If this is the
977                  * case, then don't delete. If all opens are POSIX delete now. */
978                 for (i=0; i<lck->num_share_modes; i++) {
979                         struct share_mode_entry *e = &lck->share_modes[i];
980                         if (is_valid_share_mode_entry(e)) {
981                                 if (fsp->posix_open && (e->flags & SHARE_MODE_FLAG_POSIX_OPEN)) {
982                                         continue;
983                                 }
984                                 delete_dir = False;
985                                 break;
986                         }
987                 }
988         }
989
990         if ((close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE) &&
991                                 delete_dir &&
992                                 lck->delete_token) {
993         
994                 /* Become the user who requested the delete. */
995
996                 if (!push_sec_ctx()) {
997                         smb_panic("close_directory: failed to push sec_ctx.\n");
998                 }
999
1000                 set_sec_ctx(lck->delete_token->uid,
1001                                 lck->delete_token->gid,
1002                                 lck->delete_token->ngroups,
1003                                 lck->delete_token->groups,
1004                                 NULL);
1005
1006                 TALLOC_FREE(lck);
1007
1008                 status = rmdir_internals(talloc_tos(), fsp);
1009
1010                 DEBUG(5,("close_directory: %s. Delete on close was set - "
1011                          "deleting directory returned %s.\n",
1012                          fsp_str_dbg(fsp), nt_errstr(status)));
1013
1014                 /* unbecome user. */
1015                 pop_sec_ctx();
1016
1017                 /*
1018                  * Ensure we remove any change notify requests that would
1019                  * now fail as the directory has been deleted.
1020                  */
1021
1022                 if(NT_STATUS_IS_OK(status)) {
1023                         remove_pending_change_notify_requests_by_fid(fsp, NT_STATUS_DELETE_PENDING);
1024                 }
1025         } else {
1026                 TALLOC_FREE(lck);
1027                 remove_pending_change_notify_requests_by_fid(
1028                         fsp, NT_STATUS_OK);
1029         }
1030
1031         status1 = fd_close(fsp);
1032
1033         if (!NT_STATUS_IS_OK(status1)) {
1034                 DEBUG(0, ("Could not close dir! fname=%s, fd=%d, err=%d=%s\n",
1035                           fsp_str_dbg(fsp), fsp->fh->fd, errno,
1036                           strerror(errno)));
1037         }
1038
1039         if (fsp->dptr) {
1040                 dptr_CloseDir(fsp->dptr);
1041         }
1042
1043         /*
1044          * Do the code common to files and directories.
1045          */
1046         close_filestruct(fsp);
1047         file_free(req, fsp);
1048
1049  out:
1050         TALLOC_FREE(lck);
1051         if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(status1)) {
1052                 status = status1;
1053         }
1054         return status;
1055 }
1056
1057 /****************************************************************************
1058  Close a files_struct.
1059 ****************************************************************************/
1060   
1061 NTSTATUS close_file(struct smb_request *req, files_struct *fsp,
1062                     enum file_close_type close_type)
1063 {
1064         NTSTATUS status;
1065         struct files_struct *base_fsp = fsp->base_fsp;
1066
1067         if(fsp->is_directory) {
1068                 status = close_directory(req, fsp, close_type);
1069         } else if (fsp->fake_file_handle != NULL) {
1070                 status = close_fake_file(req, fsp);
1071         } else {
1072                 status = close_normal_file(req, fsp, close_type);
1073         }
1074
1075         if ((base_fsp != NULL) && (close_type != SHUTDOWN_CLOSE)) {
1076
1077                 /*
1078                  * fsp was a stream, the base fsp can't be a stream as well
1079                  *
1080                  * For SHUTDOWN_CLOSE this is not possible here, because
1081                  * SHUTDOWN_CLOSE only happens from files.c which walks the
1082                  * complete list of files. If we mess with more than one fsp
1083                  * those loops will become confused.
1084                  */
1085
1086                 SMB_ASSERT(base_fsp->base_fsp == NULL);
1087                 close_file(req, base_fsp, close_type);
1088         }
1089
1090         return status;
1091 }
1092
1093 /****************************************************************************
1094  Deal with an (authorized) message to close a file given the share mode
1095  entry.
1096 ****************************************************************************/
1097
1098 void msg_close_file(struct messaging_context *msg_ctx,
1099                         void *private_data,
1100                         uint32_t msg_type,
1101                         struct server_id server_id,
1102                         DATA_BLOB *data)
1103 {
1104         files_struct *fsp = NULL;
1105         struct share_mode_entry e;
1106
1107         message_to_share_mode_entry(&e, (char *)data->data);
1108
1109         if(DEBUGLVL(10)) {
1110                 char *sm_str = share_mode_str(NULL, 0, &e);
1111                 if (!sm_str) {
1112                         smb_panic("talloc failed");
1113                 }
1114                 DEBUG(10,("msg_close_file: got request to close share mode "
1115                         "entry %s\n", sm_str));
1116                 TALLOC_FREE(sm_str);
1117         }
1118
1119         fsp = file_find_dif(e.id, e.share_file_id);
1120         if (!fsp) {
1121                 DEBUG(10,("msg_close_file: failed to find file.\n"));
1122                 return;
1123         }
1124         close_file(NULL, fsp, NORMAL_CLOSE);
1125 }