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