source: vendor/current/source3/smbd/close.c

Last change on this file was 989, checked in by Silvan Scherrer, 9 years ago

Samba Server: update vendor to version 4.4.7

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