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 void 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 |
|
---|
40 | if (!*lp_magicscript(SNUM(conn))) {
|
---|
41 | return;
|
---|
42 | }
|
---|
43 |
|
---|
44 | DEBUG(5,("checking magic for %s\n",fsp->fsp_name));
|
---|
45 |
|
---|
46 | if (!(p = strrchr_m(fsp->fsp_name,'/'))) {
|
---|
47 | p = fsp->fsp_name;
|
---|
48 | } else {
|
---|
49 | p++;
|
---|
50 | }
|
---|
51 |
|
---|
52 | if (!strequal(lp_magicscript(SNUM(conn)),p)) {
|
---|
53 | return;
|
---|
54 | }
|
---|
55 |
|
---|
56 | ctx = talloc_stackframe();
|
---|
57 |
|
---|
58 | if (*lp_magicoutput(SNUM(conn))) {
|
---|
59 | magic_output = lp_magicoutput(SNUM(conn));
|
---|
60 | } else {
|
---|
61 | magic_output = talloc_asprintf(ctx,
|
---|
62 | "%s.out",
|
---|
63 | fsp->fsp_name);
|
---|
64 | }
|
---|
65 | if (!magic_output) {
|
---|
66 | TALLOC_FREE(ctx);
|
---|
67 | return;
|
---|
68 | }
|
---|
69 |
|
---|
70 | chmod(fsp->fsp_name,0755);
|
---|
71 | ret = smbrun(fsp->fsp_name,&tmp_fd);
|
---|
72 | DEBUG(3,("Invoking magic command %s gave %d\n",
|
---|
73 | fsp->fsp_name,ret));
|
---|
74 |
|
---|
75 | unlink(fsp->fsp_name);
|
---|
76 | if (ret != 0 || tmp_fd == -1) {
|
---|
77 | if (tmp_fd != -1) {
|
---|
78 | close(tmp_fd);
|
---|
79 | }
|
---|
80 | TALLOC_FREE(ctx);
|
---|
81 | return;
|
---|
82 | }
|
---|
83 | outfd = open(magic_output, O_CREAT|O_EXCL|O_RDWR, 0600);
|
---|
84 | if (outfd == -1) {
|
---|
85 | close(tmp_fd);
|
---|
86 | TALLOC_FREE(ctx);
|
---|
87 | return;
|
---|
88 | }
|
---|
89 |
|
---|
90 | if (sys_fstat(tmp_fd,&st) == -1) {
|
---|
91 | close(tmp_fd);
|
---|
92 | close(outfd);
|
---|
93 | return;
|
---|
94 | }
|
---|
95 |
|
---|
96 | transfer_file(tmp_fd,outfd,(SMB_OFF_T)st.st_size);
|
---|
97 | close(tmp_fd);
|
---|
98 | close(outfd);
|
---|
99 | TALLOC_FREE(ctx);
|
---|
100 | }
|
---|
101 |
|
---|
102 | /****************************************************************************
|
---|
103 | Common code to close a file or a directory.
|
---|
104 | ****************************************************************************/
|
---|
105 |
|
---|
106 | static NTSTATUS close_filestruct(files_struct *fsp)
|
---|
107 | {
|
---|
108 | NTSTATUS status = NT_STATUS_OK;
|
---|
109 | connection_struct *conn = fsp->conn;
|
---|
110 |
|
---|
111 | if (fsp->fh->fd != -1) {
|
---|
112 | if(flush_write_cache(fsp, CLOSE_FLUSH) == -1) {
|
---|
113 | status = map_nt_error_from_unix(errno);
|
---|
114 | }
|
---|
115 | delete_write_cache(fsp);
|
---|
116 | }
|
---|
117 |
|
---|
118 | conn->num_files_open--;
|
---|
119 | return status;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /****************************************************************************
|
---|
123 | If any deferred opens are waiting on this close, notify them.
|
---|
124 | ****************************************************************************/
|
---|
125 |
|
---|
126 | static void notify_deferred_opens(struct share_mode_lock *lck)
|
---|
127 | {
|
---|
128 | int i;
|
---|
129 |
|
---|
130 | for (i=0; i<lck->num_share_modes; i++) {
|
---|
131 | struct share_mode_entry *e = &lck->share_modes[i];
|
---|
132 |
|
---|
133 | if (!is_deferred_open_entry(e)) {
|
---|
134 | continue;
|
---|
135 | }
|
---|
136 |
|
---|
137 | if (procid_is_me(&e->pid)) {
|
---|
138 | /*
|
---|
139 | * We need to notify ourself to retry the open. Do
|
---|
140 | * this by finding the queued SMB record, moving it to
|
---|
141 | * the head of the queue and changing the wait time to
|
---|
142 | * zero.
|
---|
143 | */
|
---|
144 | schedule_deferred_open_smb_message(e->op_mid);
|
---|
145 | } else {
|
---|
146 | char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
|
---|
147 |
|
---|
148 | share_mode_entry_to_message(msg, e);
|
---|
149 |
|
---|
150 | messaging_send_buf(smbd_messaging_context(),
|
---|
151 | e->pid, MSG_SMB_OPEN_RETRY,
|
---|
152 | (uint8 *)msg,
|
---|
153 | MSG_SMB_SHARE_MODE_ENTRY_SIZE);
|
---|
154 | }
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | /****************************************************************************
|
---|
159 | Delete all streams
|
---|
160 | ****************************************************************************/
|
---|
161 |
|
---|
162 | static NTSTATUS delete_all_streams(connection_struct *conn, const char *fname)
|
---|
163 | {
|
---|
164 | struct stream_struct *stream_info;
|
---|
165 | int i;
|
---|
166 | unsigned int num_streams;
|
---|
167 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
168 | NTSTATUS status;
|
---|
169 |
|
---|
170 | status = SMB_VFS_STREAMINFO(conn, NULL, fname, talloc_tos(),
|
---|
171 | &num_streams, &stream_info);
|
---|
172 |
|
---|
173 | if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
|
---|
174 | DEBUG(10, ("no streams around\n"));
|
---|
175 | TALLOC_FREE(frame);
|
---|
176 | return NT_STATUS_OK;
|
---|
177 | }
|
---|
178 |
|
---|
179 | if (!NT_STATUS_IS_OK(status)) {
|
---|
180 | DEBUG(10, ("SMB_VFS_STREAMINFO failed: %s\n",
|
---|
181 | nt_errstr(status)));
|
---|
182 | goto fail;
|
---|
183 | }
|
---|
184 |
|
---|
185 | DEBUG(10, ("delete_all_streams found %d streams\n",
|
---|
186 | num_streams));
|
---|
187 |
|
---|
188 | if (num_streams == 0) {
|
---|
189 | TALLOC_FREE(frame);
|
---|
190 | return NT_STATUS_OK;
|
---|
191 | }
|
---|
192 |
|
---|
193 | for (i=0; i<num_streams; i++) {
|
---|
194 | int res;
|
---|
195 | char *streamname;
|
---|
196 |
|
---|
197 | if (strequal(stream_info[i].name, "::$DATA")) {
|
---|
198 | continue;
|
---|
199 | }
|
---|
200 |
|
---|
201 | streamname = talloc_asprintf(talloc_tos(), "%s%s", fname,
|
---|
202 | stream_info[i].name);
|
---|
203 |
|
---|
204 | if (streamname == NULL) {
|
---|
205 | DEBUG(0, ("talloc_aprintf failed\n"));
|
---|
206 | status = NT_STATUS_NO_MEMORY;
|
---|
207 | goto fail;
|
---|
208 | }
|
---|
209 |
|
---|
210 | res = SMB_VFS_UNLINK(conn, streamname);
|
---|
211 |
|
---|
212 | TALLOC_FREE(streamname);
|
---|
213 |
|
---|
214 | if (res == -1) {
|
---|
215 | status = map_nt_error_from_unix(errno);
|
---|
216 | DEBUG(10, ("Could not delete stream %s: %s\n",
|
---|
217 | streamname, strerror(errno)));
|
---|
218 | break;
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 | fail:
|
---|
223 | TALLOC_FREE(frame);
|
---|
224 | return status;
|
---|
225 | }
|
---|
226 |
|
---|
227 | /****************************************************************************
|
---|
228 | Deal with removing a share mode on last close.
|
---|
229 | ****************************************************************************/
|
---|
230 |
|
---|
231 | static NTSTATUS close_remove_share_mode(files_struct *fsp,
|
---|
232 | enum file_close_type close_type)
|
---|
233 | {
|
---|
234 | connection_struct *conn = fsp->conn;
|
---|
235 | bool delete_file = false;
|
---|
236 | bool changed_user = false;
|
---|
237 | struct share_mode_lock *lck;
|
---|
238 | SMB_STRUCT_STAT sbuf;
|
---|
239 | NTSTATUS status = NT_STATUS_OK;
|
---|
240 | int ret;
|
---|
241 | struct file_id id;
|
---|
242 |
|
---|
243 | /*
|
---|
244 | * Lock the share entries, and determine if we should delete
|
---|
245 | * on close. If so delete whilst the lock is still in effect.
|
---|
246 | * This prevents race conditions with the file being created. JRA.
|
---|
247 | */
|
---|
248 |
|
---|
249 | lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
|
---|
250 | NULL);
|
---|
251 |
|
---|
252 | if (lck == NULL) {
|
---|
253 | DEBUG(0, ("close_remove_share_mode: Could not get share mode "
|
---|
254 | "lock for file %s\n", fsp->fsp_name));
|
---|
255 | return NT_STATUS_INVALID_PARAMETER;
|
---|
256 | }
|
---|
257 |
|
---|
258 | if (fsp->write_time_forced) {
|
---|
259 | set_close_write_time(fsp, lck->changed_write_time);
|
---|
260 | }
|
---|
261 |
|
---|
262 | if (!del_share_mode(lck, fsp)) {
|
---|
263 | DEBUG(0, ("close_remove_share_mode: Could not delete share "
|
---|
264 | "entry for file %s\n", fsp->fsp_name));
|
---|
265 | }
|
---|
266 |
|
---|
267 | if (fsp->initial_delete_on_close && (lck->delete_token == NULL)) {
|
---|
268 | bool became_user = False;
|
---|
269 |
|
---|
270 | /* Initial delete on close was set and no one else
|
---|
271 | * wrote a real delete on close. */
|
---|
272 |
|
---|
273 | if (current_user.vuid != fsp->vuid) {
|
---|
274 | become_user(conn, fsp->vuid);
|
---|
275 | became_user = True;
|
---|
276 | }
|
---|
277 | set_delete_on_close_lck(lck, True, ¤t_user.ut);
|
---|
278 | if (became_user) {
|
---|
279 | unbecome_user();
|
---|
280 | }
|
---|
281 | }
|
---|
282 |
|
---|
283 | delete_file = lck->delete_on_close;
|
---|
284 |
|
---|
285 | if (delete_file) {
|
---|
286 | int i;
|
---|
287 | /* See if others still have the file open. If this is the
|
---|
288 | * case, then don't delete. If all opens are POSIX delete now. */
|
---|
289 | for (i=0; i<lck->num_share_modes; i++) {
|
---|
290 | struct share_mode_entry *e = &lck->share_modes[i];
|
---|
291 | if (is_valid_share_mode_entry(e)) {
|
---|
292 | if (fsp->posix_open && (e->flags & SHARE_MODE_FLAG_POSIX_OPEN)) {
|
---|
293 | continue;
|
---|
294 | }
|
---|
295 | delete_file = False;
|
---|
296 | break;
|
---|
297 | }
|
---|
298 | }
|
---|
299 | }
|
---|
300 |
|
---|
301 | /* Notify any deferred opens waiting on this close. */
|
---|
302 | notify_deferred_opens(lck);
|
---|
303 | reply_to_oplock_break_requests(fsp);
|
---|
304 |
|
---|
305 | /*
|
---|
306 | * NT can set delete_on_close of the last open
|
---|
307 | * reference to a file.
|
---|
308 | */
|
---|
309 |
|
---|
310 | if (!(close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE)
|
---|
311 | || !delete_file
|
---|
312 | || (lck->delete_token == NULL)) {
|
---|
313 | TALLOC_FREE(lck);
|
---|
314 | return NT_STATUS_OK;
|
---|
315 | }
|
---|
316 |
|
---|
317 | /*
|
---|
318 | * Ok, we have to delete the file
|
---|
319 | */
|
---|
320 |
|
---|
321 | DEBUG(5,("close_remove_share_mode: file %s. Delete on close was set "
|
---|
322 | "- deleting file.\n", fsp->fsp_name));
|
---|
323 |
|
---|
324 | /*
|
---|
325 | * Don't try to update the write time when we delete the file
|
---|
326 | */
|
---|
327 | fsp->update_write_time_on_close = false;
|
---|
328 |
|
---|
329 | if (!unix_token_equal(lck->delete_token, ¤t_user.ut)) {
|
---|
330 | /* Become the user who requested the delete. */
|
---|
331 |
|
---|
332 | DEBUG(5,("close_remove_share_mode: file %s. "
|
---|
333 | "Change user to uid %u\n",
|
---|
334 | fsp->fsp_name,
|
---|
335 | (unsigned int)lck->delete_token->uid));
|
---|
336 |
|
---|
337 | if (!push_sec_ctx()) {
|
---|
338 | smb_panic("close_remove_share_mode: file %s. failed to push "
|
---|
339 | "sec_ctx.\n");
|
---|
340 | }
|
---|
341 |
|
---|
342 | set_sec_ctx(lck->delete_token->uid,
|
---|
343 | lck->delete_token->gid,
|
---|
344 | lck->delete_token->ngroups,
|
---|
345 | lck->delete_token->groups,
|
---|
346 | NULL);
|
---|
347 |
|
---|
348 | changed_user = true;
|
---|
349 | }
|
---|
350 |
|
---|
351 | /* We can only delete the file if the name we have is still valid and
|
---|
352 | hasn't been renamed. */
|
---|
353 |
|
---|
354 | if (fsp->posix_open) {
|
---|
355 | ret = SMB_VFS_LSTAT(conn,fsp->fsp_name,&sbuf);
|
---|
356 | } else {
|
---|
357 | ret = SMB_VFS_STAT(conn,fsp->fsp_name,&sbuf);
|
---|
358 | }
|
---|
359 |
|
---|
360 | if (ret != 0) {
|
---|
361 | DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
|
---|
362 | "was set and stat failed with error %s\n",
|
---|
363 | fsp->fsp_name, strerror(errno) ));
|
---|
364 | /*
|
---|
365 | * Don't save the errno here, we ignore this error
|
---|
366 | */
|
---|
367 | goto done;
|
---|
368 | }
|
---|
369 |
|
---|
370 | id = vfs_file_id_from_sbuf(conn, &sbuf);
|
---|
371 |
|
---|
372 | if (!file_id_equal(&fsp->file_id, &id)) {
|
---|
373 | DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
|
---|
374 | "was set and dev and/or inode does not match\n",
|
---|
375 | fsp->fsp_name ));
|
---|
376 | DEBUG(5,("close_remove_share_mode: file %s. stored file_id %s, "
|
---|
377 | "stat file_id %s\n",
|
---|
378 | fsp->fsp_name,
|
---|
379 | file_id_string_tos(&fsp->file_id),
|
---|
380 | file_id_string_tos(&id)));
|
---|
381 | /*
|
---|
382 | * Don't save the errno here, we ignore this error
|
---|
383 | */
|
---|
384 | goto done;
|
---|
385 | }
|
---|
386 |
|
---|
387 | if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
|
---|
388 | && !is_ntfs_stream_name(fsp->fsp_name)) {
|
---|
389 |
|
---|
390 | status = delete_all_streams(conn, fsp->fsp_name);
|
---|
391 |
|
---|
392 | if (!NT_STATUS_IS_OK(status)) {
|
---|
393 | DEBUG(5, ("delete_all_streams failed: %s\n",
|
---|
394 | nt_errstr(status)));
|
---|
395 | goto done;
|
---|
396 | }
|
---|
397 | }
|
---|
398 |
|
---|
399 |
|
---|
400 | if (SMB_VFS_UNLINK(conn,fsp->fsp_name) != 0) {
|
---|
401 | /*
|
---|
402 | * This call can potentially fail as another smbd may
|
---|
403 | * have had the file open with delete on close set and
|
---|
404 | * deleted it when its last reference to this file
|
---|
405 | * went away. Hence we log this but not at debug level
|
---|
406 | * zero.
|
---|
407 | */
|
---|
408 |
|
---|
409 | DEBUG(5,("close_remove_share_mode: file %s. Delete on close "
|
---|
410 | "was set and unlink failed with error %s\n",
|
---|
411 | fsp->fsp_name, strerror(errno) ));
|
---|
412 |
|
---|
413 | status = map_nt_error_from_unix(errno);
|
---|
414 | }
|
---|
415 |
|
---|
416 | notify_fname(conn, NOTIFY_ACTION_REMOVED,
|
---|
417 | FILE_NOTIFY_CHANGE_FILE_NAME,
|
---|
418 | fsp->fsp_name);
|
---|
419 |
|
---|
420 | /* As we now have POSIX opens which can unlink
|
---|
421 | * with other open files we may have taken
|
---|
422 | * this code path with more than one share mode
|
---|
423 | * entry - ensure we only delete once by resetting
|
---|
424 | * the delete on close flag. JRA.
|
---|
425 | */
|
---|
426 |
|
---|
427 | set_delete_on_close_lck(lck, False, NULL);
|
---|
428 |
|
---|
429 | done:
|
---|
430 |
|
---|
431 | if (changed_user) {
|
---|
432 | /* unbecome user. */
|
---|
433 | pop_sec_ctx();
|
---|
434 | }
|
---|
435 |
|
---|
436 | TALLOC_FREE(lck);
|
---|
437 | return status;
|
---|
438 | }
|
---|
439 |
|
---|
440 | void set_close_write_time(struct files_struct *fsp, struct timespec ts)
|
---|
441 | {
|
---|
442 | DEBUG(6,("close_write_time: %s" , time_to_asc(convert_timespec_to_time_t(ts))));
|
---|
443 |
|
---|
444 | if (null_timespec(ts)) {
|
---|
445 | return;
|
---|
446 | }
|
---|
447 | /*
|
---|
448 | * if the write time on close is explict set, then don't
|
---|
449 | * need to fix it up to the value in the locking db
|
---|
450 | */
|
---|
451 | fsp->write_time_forced = false;
|
---|
452 |
|
---|
453 | fsp->update_write_time_on_close = true;
|
---|
454 | fsp->close_write_time = ts;
|
---|
455 | }
|
---|
456 |
|
---|
457 | static NTSTATUS update_write_time_on_close(struct files_struct *fsp)
|
---|
458 | {
|
---|
459 | SMB_STRUCT_STAT sbuf;
|
---|
460 | struct timespec ts[2];
|
---|
461 | NTSTATUS status;
|
---|
462 |
|
---|
463 | ZERO_STRUCT(sbuf);
|
---|
464 | ZERO_STRUCT(ts);
|
---|
465 |
|
---|
466 | if (!fsp->update_write_time_on_close) {
|
---|
467 | return NT_STATUS_OK;
|
---|
468 | }
|
---|
469 |
|
---|
470 | if (null_timespec(fsp->close_write_time)) {
|
---|
471 | fsp->close_write_time = timespec_current();
|
---|
472 | }
|
---|
473 |
|
---|
474 | /* Ensure we have a valid stat struct for the source. */
|
---|
475 | if (fsp->fh->fd != -1) {
|
---|
476 | if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
|
---|
477 | return map_nt_error_from_unix(errno);
|
---|
478 | }
|
---|
479 | } else {
|
---|
480 | if (SMB_VFS_STAT(fsp->conn,fsp->fsp_name,&sbuf) == -1) {
|
---|
481 | return map_nt_error_from_unix(errno);
|
---|
482 | }
|
---|
483 | }
|
---|
484 |
|
---|
485 | if (!VALID_STAT(sbuf)) {
|
---|
486 | /* if it doesn't seem to be a real file */
|
---|
487 | return NT_STATUS_OK;
|
---|
488 | }
|
---|
489 |
|
---|
490 | ts[1] = fsp->close_write_time;
|
---|
491 | status = smb_set_file_time(fsp->conn, fsp, fsp->fsp_name,
|
---|
492 | &sbuf, ts, true);
|
---|
493 | if (!NT_STATUS_IS_OK(status)) {
|
---|
494 | return status;
|
---|
495 | }
|
---|
496 |
|
---|
497 | return NT_STATUS_OK;
|
---|
498 | }
|
---|
499 |
|
---|
500 | /****************************************************************************
|
---|
501 | Close a file.
|
---|
502 |
|
---|
503 | close_type can be NORMAL_CLOSE=0,SHUTDOWN_CLOSE,ERROR_CLOSE.
|
---|
504 | printing and magic scripts are only run on normal close.
|
---|
505 | delete on close is done on normal and shutdown close.
|
---|
506 | ****************************************************************************/
|
---|
507 |
|
---|
508 | static NTSTATUS close_normal_file(files_struct *fsp, enum file_close_type close_type)
|
---|
509 | {
|
---|
510 | NTSTATUS status = NT_STATUS_OK;
|
---|
511 | NTSTATUS saved_status1 = NT_STATUS_OK;
|
---|
512 | NTSTATUS saved_status2 = NT_STATUS_OK;
|
---|
513 | NTSTATUS saved_status3 = NT_STATUS_OK;
|
---|
514 | NTSTATUS saved_status4 = NT_STATUS_OK;
|
---|
515 | connection_struct *conn = fsp->conn;
|
---|
516 |
|
---|
517 | if (fsp->aio_write_behind) {
|
---|
518 | /*
|
---|
519 | * If we're finishing write behind on a close we can get a write
|
---|
520 | * error here, we must remember this.
|
---|
521 | */
|
---|
522 | int ret = wait_for_aio_completion(fsp);
|
---|
523 | if (ret) {
|
---|
524 | saved_status1 = map_nt_error_from_unix(ret);
|
---|
525 | }
|
---|
526 | } else {
|
---|
527 | cancel_aio_by_fsp(fsp);
|
---|
528 | }
|
---|
529 |
|
---|
530 | /*
|
---|
531 | * If we're flushing on a close we can get a write
|
---|
532 | * error here, we must remember this.
|
---|
533 | */
|
---|
534 |
|
---|
535 | saved_status2 = close_filestruct(fsp);
|
---|
536 |
|
---|
537 | if (fsp->print_file) {
|
---|
538 | print_fsp_end(fsp, close_type);
|
---|
539 | file_free(fsp);
|
---|
540 | return NT_STATUS_OK;
|
---|
541 | }
|
---|
542 |
|
---|
543 | /* If this is an old DOS or FCB open and we have multiple opens on
|
---|
544 | the same handle we only have one share mode. Ensure we only remove
|
---|
545 | the share mode on the last close. */
|
---|
546 |
|
---|
547 | if (fsp->fh->ref_count == 1) {
|
---|
548 | /* Should we return on error here... ? */
|
---|
549 | saved_status3 = close_remove_share_mode(fsp, close_type);
|
---|
550 | }
|
---|
551 |
|
---|
552 | if(fsp->oplock_type) {
|
---|
553 | release_file_oplock(fsp);
|
---|
554 | }
|
---|
555 |
|
---|
556 | locking_close_file(smbd_messaging_context(), fsp);
|
---|
557 |
|
---|
558 | status = fd_close(fsp);
|
---|
559 |
|
---|
560 | /* check for magic scripts */
|
---|
561 | if (close_type == NORMAL_CLOSE) {
|
---|
562 | check_magic(fsp);
|
---|
563 | }
|
---|
564 |
|
---|
565 | /*
|
---|
566 | * Ensure pending modtime is set after close.
|
---|
567 | */
|
---|
568 |
|
---|
569 | saved_status4 = update_write_time_on_close(fsp);
|
---|
570 |
|
---|
571 | if (NT_STATUS_IS_OK(status)) {
|
---|
572 | if (!NT_STATUS_IS_OK(saved_status1)) {
|
---|
573 | status = saved_status1;
|
---|
574 | } else if (!NT_STATUS_IS_OK(saved_status2)) {
|
---|
575 | status = saved_status2;
|
---|
576 | } else if (!NT_STATUS_IS_OK(saved_status3)) {
|
---|
577 | status = saved_status3;
|
---|
578 | } else if (!NT_STATUS_IS_OK(saved_status4)) {
|
---|
579 | status = saved_status4;
|
---|
580 | }
|
---|
581 | }
|
---|
582 |
|
---|
583 | DEBUG(2,("%s closed file %s (numopen=%d) %s\n",
|
---|
584 | conn->user,fsp->fsp_name,
|
---|
585 | conn->num_files_open,
|
---|
586 | nt_errstr(status) ));
|
---|
587 |
|
---|
588 | file_free(fsp);
|
---|
589 | return status;
|
---|
590 | }
|
---|
591 |
|
---|
592 | /****************************************************************************
|
---|
593 | Close a directory opened by an NT SMB call.
|
---|
594 | ****************************************************************************/
|
---|
595 |
|
---|
596 | static NTSTATUS close_directory(files_struct *fsp, enum file_close_type close_type)
|
---|
597 | {
|
---|
598 | struct share_mode_lock *lck = 0;
|
---|
599 | bool delete_dir = False;
|
---|
600 | NTSTATUS status = NT_STATUS_OK;
|
---|
601 |
|
---|
602 | /*
|
---|
603 | * NT can set delete_on_close of the last open
|
---|
604 | * reference to a directory also.
|
---|
605 | */
|
---|
606 |
|
---|
607 | lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
|
---|
608 | NULL);
|
---|
609 |
|
---|
610 | if (lck == NULL) {
|
---|
611 | DEBUG(0, ("close_directory: Could not get share mode lock for %s\n", fsp->fsp_name));
|
---|
612 | return NT_STATUS_INVALID_PARAMETER;
|
---|
613 | }
|
---|
614 |
|
---|
615 | if (!del_share_mode(lck, fsp)) {
|
---|
616 | DEBUG(0, ("close_directory: Could not delete share entry for %s\n", fsp->fsp_name));
|
---|
617 | }
|
---|
618 |
|
---|
619 | if (fsp->initial_delete_on_close) {
|
---|
620 | bool became_user = False;
|
---|
621 |
|
---|
622 | /* Initial delete on close was set - for
|
---|
623 | * directories we don't care if anyone else
|
---|
624 | * wrote a real delete on close. */
|
---|
625 |
|
---|
626 | if (current_user.vuid != fsp->vuid) {
|
---|
627 | become_user(fsp->conn, fsp->vuid);
|
---|
628 | became_user = True;
|
---|
629 | }
|
---|
630 | send_stat_cache_delete_message(fsp->fsp_name);
|
---|
631 | set_delete_on_close_lck(lck, True, ¤t_user.ut);
|
---|
632 | if (became_user) {
|
---|
633 | unbecome_user();
|
---|
634 | }
|
---|
635 | }
|
---|
636 |
|
---|
637 | delete_dir = lck->delete_on_close;
|
---|
638 |
|
---|
639 | if (delete_dir) {
|
---|
640 | int i;
|
---|
641 | /* See if others still have the dir open. If this is the
|
---|
642 | * case, then don't delete. If all opens are POSIX delete now. */
|
---|
643 | for (i=0; i<lck->num_share_modes; i++) {
|
---|
644 | struct share_mode_entry *e = &lck->share_modes[i];
|
---|
645 | if (is_valid_share_mode_entry(e)) {
|
---|
646 | if (fsp->posix_open && (e->flags & SHARE_MODE_FLAG_POSIX_OPEN)) {
|
---|
647 | continue;
|
---|
648 | }
|
---|
649 | delete_dir = False;
|
---|
650 | break;
|
---|
651 | }
|
---|
652 | }
|
---|
653 | }
|
---|
654 |
|
---|
655 | if ((close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE) &&
|
---|
656 | delete_dir &&
|
---|
657 | lck->delete_token) {
|
---|
658 |
|
---|
659 | /* Become the user who requested the delete. */
|
---|
660 |
|
---|
661 | if (!push_sec_ctx()) {
|
---|
662 | smb_panic("close_directory: failed to push sec_ctx.\n");
|
---|
663 | }
|
---|
664 |
|
---|
665 | set_sec_ctx(lck->delete_token->uid,
|
---|
666 | lck->delete_token->gid,
|
---|
667 | lck->delete_token->ngroups,
|
---|
668 | lck->delete_token->groups,
|
---|
669 | NULL);
|
---|
670 |
|
---|
671 | TALLOC_FREE(lck);
|
---|
672 |
|
---|
673 | status = rmdir_internals(talloc_tos(),
|
---|
674 | fsp->conn, fsp->fsp_name);
|
---|
675 |
|
---|
676 | DEBUG(5,("close_directory: %s. Delete on close was set - "
|
---|
677 | "deleting directory returned %s.\n",
|
---|
678 | fsp->fsp_name, nt_errstr(status)));
|
---|
679 |
|
---|
680 | /* unbecome user. */
|
---|
681 | pop_sec_ctx();
|
---|
682 |
|
---|
683 | /*
|
---|
684 | * Ensure we remove any change notify requests that would
|
---|
685 | * now fail as the directory has been deleted.
|
---|
686 | */
|
---|
687 |
|
---|
688 | if(NT_STATUS_IS_OK(status)) {
|
---|
689 | remove_pending_change_notify_requests_by_fid(fsp, NT_STATUS_DELETE_PENDING);
|
---|
690 | }
|
---|
691 | } else {
|
---|
692 | TALLOC_FREE(lck);
|
---|
693 | remove_pending_change_notify_requests_by_fid(
|
---|
694 | fsp, NT_STATUS_OK);
|
---|
695 | }
|
---|
696 |
|
---|
697 | /*
|
---|
698 | * Do the code common to files and directories.
|
---|
699 | */
|
---|
700 | close_filestruct(fsp);
|
---|
701 | file_free(fsp);
|
---|
702 | return status;
|
---|
703 | }
|
---|
704 |
|
---|
705 | /****************************************************************************
|
---|
706 | Close a 'stat file' opened internally.
|
---|
707 | ****************************************************************************/
|
---|
708 |
|
---|
709 | static NTSTATUS close_stat(files_struct *fsp)
|
---|
710 | {
|
---|
711 | /*
|
---|
712 | * Do the code common to files and directories.
|
---|
713 | */
|
---|
714 | close_filestruct(fsp);
|
---|
715 | file_free(fsp);
|
---|
716 | return NT_STATUS_OK;
|
---|
717 | }
|
---|
718 |
|
---|
719 | /****************************************************************************
|
---|
720 | Close a files_struct.
|
---|
721 | ****************************************************************************/
|
---|
722 |
|
---|
723 | NTSTATUS close_file(files_struct *fsp, enum file_close_type close_type)
|
---|
724 | {
|
---|
725 | NTSTATUS status;
|
---|
726 | struct files_struct *base_fsp = fsp->base_fsp;
|
---|
727 |
|
---|
728 | if(fsp->is_directory) {
|
---|
729 | status = close_directory(fsp, close_type);
|
---|
730 | } else if (fsp->is_stat) {
|
---|
731 | status = close_stat(fsp);
|
---|
732 | } else if (fsp->fake_file_handle != NULL) {
|
---|
733 | status = close_fake_file(fsp);
|
---|
734 | } else {
|
---|
735 | status = close_normal_file(fsp, close_type);
|
---|
736 | }
|
---|
737 |
|
---|
738 | if ((base_fsp != NULL) && (close_type != SHUTDOWN_CLOSE)) {
|
---|
739 |
|
---|
740 | /*
|
---|
741 | * fsp was a stream, the base fsp can't be a stream as well
|
---|
742 | *
|
---|
743 | * For SHUTDOWN_CLOSE this is not possible here, because
|
---|
744 | * SHUTDOWN_CLOSE only happens from files.c which walks the
|
---|
745 | * complete list of files. If we mess with more than one fsp
|
---|
746 | * those loops will become confused.
|
---|
747 | */
|
---|
748 |
|
---|
749 | SMB_ASSERT(base_fsp->base_fsp == NULL);
|
---|
750 | close_file(base_fsp, close_type);
|
---|
751 | }
|
---|
752 |
|
---|
753 | return status;
|
---|
754 | }
|
---|
755 |
|
---|
756 | /****************************************************************************
|
---|
757 | Deal with an (authorized) message to close a file given the share mode
|
---|
758 | entry.
|
---|
759 | ****************************************************************************/
|
---|
760 |
|
---|
761 | void msg_close_file(struct messaging_context *msg_ctx,
|
---|
762 | void *private_data,
|
---|
763 | uint32_t msg_type,
|
---|
764 | struct server_id server_id,
|
---|
765 | DATA_BLOB *data)
|
---|
766 | {
|
---|
767 | files_struct *fsp = NULL;
|
---|
768 | struct share_mode_entry e;
|
---|
769 |
|
---|
770 | message_to_share_mode_entry(&e, (char *)data->data);
|
---|
771 |
|
---|
772 | if(DEBUGLVL(10)) {
|
---|
773 | char *sm_str = share_mode_str(NULL, 0, &e);
|
---|
774 | if (!sm_str) {
|
---|
775 | smb_panic("talloc failed");
|
---|
776 | }
|
---|
777 | DEBUG(10,("msg_close_file: got request to close share mode "
|
---|
778 | "entry %s\n", sm_str));
|
---|
779 | TALLOC_FREE(sm_str);
|
---|
780 | }
|
---|
781 |
|
---|
782 | fsp = file_find_dif(e.id, e.share_file_id);
|
---|
783 | if (!fsp) {
|
---|
784 | DEBUG(10,("msg_close_file: failed to find file.\n"));
|
---|
785 | return;
|
---|
786 | }
|
---|
787 | close_file(fsp, NORMAL_CLOSE);
|
---|
788 | }
|
---|