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