wafsamba: remove unused variable from copy_and_fix_python_path
[Samba.git] / source3 / smbd / open.c
blob4fcdff8c5b0e53f9fa07e86f8f1b8642c2fe1a28
1 /*
2 Unix SMB/CIFS implementation.
3 file opening and share modes
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 2001-2004
6 Copyright (C) Volker Lendecke 2005
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.
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.
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/>.
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 "fake_file.h"
28 #include "../libcli/security/security.h"
29 #include "../librpc/gen_ndr/ndr_security.h"
30 #include "../librpc/gen_ndr/open_files.h"
31 #include "../librpc/gen_ndr/idmap.h"
32 #include "passdb/lookup_sid.h"
33 #include "auth.h"
34 #include "serverid.h"
35 #include "messages.h"
36 #include "source3/lib/dbwrap/dbwrap_watch.h"
38 extern const struct generic_mapping file_generic_mapping;
40 struct deferred_open_record {
41 bool delayed_for_oplocks;
42 bool async_open;
43 struct file_id id;
46 /****************************************************************************
47 If the requester wanted DELETE_ACCESS and was rejected because
48 the file ACL didn't include DELETE_ACCESS, see if the parent ACL
49 overrides this.
50 ****************************************************************************/
52 static bool parent_override_delete(connection_struct *conn,
53 const struct smb_filename *smb_fname,
54 uint32_t access_mask,
55 uint32_t rejected_mask)
57 if ((access_mask & DELETE_ACCESS) &&
58 (rejected_mask & DELETE_ACCESS) &&
59 can_delete_file_in_directory(conn, smb_fname)) {
60 return true;
62 return false;
65 /****************************************************************************
66 Check if we have open rights.
67 ****************************************************************************/
69 NTSTATUS smbd_check_access_rights(struct connection_struct *conn,
70 const struct smb_filename *smb_fname,
71 bool use_privs,
72 uint32_t access_mask)
74 /* Check if we have rights to open. */
75 NTSTATUS status;
76 struct security_descriptor *sd = NULL;
77 uint32_t rejected_share_access;
78 uint32_t rejected_mask = access_mask;
79 uint32_t do_not_check_mask = 0;
81 rejected_share_access = access_mask & ~(conn->share_access);
83 if (rejected_share_access) {
84 DEBUG(10, ("smbd_check_access_rights: rejected share access 0x%x "
85 "on %s (0x%x)\n",
86 (unsigned int)access_mask,
87 smb_fname_str_dbg(smb_fname),
88 (unsigned int)rejected_share_access ));
89 return NT_STATUS_ACCESS_DENIED;
92 if (!use_privs && get_current_uid(conn) == (uid_t)0) {
93 /* I'm sorry sir, I didn't know you were root... */
94 DEBUG(10,("smbd_check_access_rights: root override "
95 "on %s. Granting 0x%x\n",
96 smb_fname_str_dbg(smb_fname),
97 (unsigned int)access_mask ));
98 return NT_STATUS_OK;
101 if ((access_mask & DELETE_ACCESS) && !lp_acl_check_permissions(SNUM(conn))) {
102 DEBUG(10,("smbd_check_access_rights: not checking ACL "
103 "on DELETE_ACCESS on file %s. Granting 0x%x\n",
104 smb_fname_str_dbg(smb_fname),
105 (unsigned int)access_mask ));
106 return NT_STATUS_OK;
109 if (access_mask == DELETE_ACCESS &&
110 VALID_STAT(smb_fname->st) &&
111 S_ISLNK(smb_fname->st.st_ex_mode)) {
112 /* We can always delete a symlink. */
113 DEBUG(10,("smbd_check_access_rights: not checking ACL "
114 "on DELETE_ACCESS on symlink %s.\n",
115 smb_fname_str_dbg(smb_fname) ));
116 return NT_STATUS_OK;
119 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
120 (SECINFO_OWNER |
121 SECINFO_GROUP |
122 SECINFO_DACL), talloc_tos(), &sd);
124 if (!NT_STATUS_IS_OK(status)) {
125 DEBUG(10, ("smbd_check_access_rights: Could not get acl "
126 "on %s: %s\n",
127 smb_fname_str_dbg(smb_fname),
128 nt_errstr(status)));
130 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
131 goto access_denied;
134 return status;
138 * If we can access the path to this file, by
139 * default we have FILE_READ_ATTRIBUTES from the
140 * containing directory. See the section:
141 * "Algorithm to Check Access to an Existing File"
142 * in MS-FSA.pdf.
144 * se_file_access_check() also takes care of
145 * owner WRITE_DAC and READ_CONTROL.
147 do_not_check_mask = FILE_READ_ATTRIBUTES;
150 * Samba 3.6 and earlier granted execute access even
151 * if the ACL did not contain execute rights.
152 * Samba 4.0 is more correct and checks it.
153 * The compatibilty mode allows to skip this check
154 * to smoothen upgrades.
156 if (lp_acl_allow_execute_always(SNUM(conn))) {
157 do_not_check_mask |= FILE_EXECUTE;
160 status = se_file_access_check(sd,
161 get_current_nttok(conn),
162 use_privs,
163 (access_mask & ~do_not_check_mask),
164 &rejected_mask);
166 DEBUG(10,("smbd_check_access_rights: file %s requesting "
167 "0x%x returning 0x%x (%s)\n",
168 smb_fname_str_dbg(smb_fname),
169 (unsigned int)access_mask,
170 (unsigned int)rejected_mask,
171 nt_errstr(status) ));
173 if (!NT_STATUS_IS_OK(status)) {
174 if (DEBUGLEVEL >= 10) {
175 DEBUG(10,("smbd_check_access_rights: acl for %s is:\n",
176 smb_fname_str_dbg(smb_fname) ));
177 NDR_PRINT_DEBUG(security_descriptor, sd);
181 TALLOC_FREE(sd);
183 if (NT_STATUS_IS_OK(status) ||
184 !NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
185 return status;
188 /* Here we know status == NT_STATUS_ACCESS_DENIED. */
190 access_denied:
192 if ((access_mask & FILE_WRITE_ATTRIBUTES) &&
193 (rejected_mask & FILE_WRITE_ATTRIBUTES) &&
194 !lp_store_dos_attributes(SNUM(conn)) &&
195 (lp_map_readonly(SNUM(conn)) ||
196 lp_map_archive(SNUM(conn)) ||
197 lp_map_hidden(SNUM(conn)) ||
198 lp_map_system(SNUM(conn)))) {
199 rejected_mask &= ~FILE_WRITE_ATTRIBUTES;
201 DEBUG(10,("smbd_check_access_rights: "
202 "overrode "
203 "FILE_WRITE_ATTRIBUTES "
204 "on file %s\n",
205 smb_fname_str_dbg(smb_fname)));
208 if (parent_override_delete(conn,
209 smb_fname,
210 access_mask,
211 rejected_mask)) {
212 /* Were we trying to do an open
213 * for delete and didn't get DELETE
214 * access (only) ? Check if the
215 * directory allows DELETE_CHILD.
216 * See here:
217 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
218 * for details. */
220 rejected_mask &= ~DELETE_ACCESS;
222 DEBUG(10,("smbd_check_access_rights: "
223 "overrode "
224 "DELETE_ACCESS on "
225 "file %s\n",
226 smb_fname_str_dbg(smb_fname)));
229 if (rejected_mask != 0) {
230 return NT_STATUS_ACCESS_DENIED;
232 return NT_STATUS_OK;
235 static NTSTATUS check_parent_access(struct connection_struct *conn,
236 struct smb_filename *smb_fname,
237 uint32_t access_mask)
239 NTSTATUS status;
240 char *parent_dir = NULL;
241 struct security_descriptor *parent_sd = NULL;
242 uint32_t access_granted = 0;
244 if (!parent_dirname(talloc_tos(),
245 smb_fname->base_name,
246 &parent_dir,
247 NULL)) {
248 return NT_STATUS_NO_MEMORY;
251 if (get_current_uid(conn) == (uid_t)0) {
252 /* I'm sorry sir, I didn't know you were root... */
253 DEBUG(10,("check_parent_access: root override "
254 "on %s. Granting 0x%x\n",
255 smb_fname_str_dbg(smb_fname),
256 (unsigned int)access_mask ));
257 return NT_STATUS_OK;
260 status = SMB_VFS_GET_NT_ACL(conn,
261 parent_dir,
262 SECINFO_DACL,
263 talloc_tos(),
264 &parent_sd);
266 if (!NT_STATUS_IS_OK(status)) {
267 DEBUG(5,("check_parent_access: SMB_VFS_GET_NT_ACL failed for "
268 "%s with error %s\n",
269 parent_dir,
270 nt_errstr(status)));
271 return status;
275 * If we can access the path to this file, by
276 * default we have FILE_READ_ATTRIBUTES from the
277 * containing directory. See the section:
278 * "Algorithm to Check Access to an Existing File"
279 * in MS-FSA.pdf.
281 * se_file_access_check() also takes care of
282 * owner WRITE_DAC and READ_CONTROL.
284 status = se_file_access_check(parent_sd,
285 get_current_nttok(conn),
286 false,
287 (access_mask & ~FILE_READ_ATTRIBUTES),
288 &access_granted);
289 if(!NT_STATUS_IS_OK(status)) {
290 DEBUG(5,("check_parent_access: access check "
291 "on directory %s for "
292 "path %s for mask 0x%x returned (0x%x) %s\n",
293 parent_dir,
294 smb_fname->base_name,
295 access_mask,
296 access_granted,
297 nt_errstr(status) ));
298 return status;
301 return NT_STATUS_OK;
304 /****************************************************************************
305 Ensure when opening a base file for a stream open that we have permissions
306 to do so given the access mask on the base file.
307 ****************************************************************************/
309 static NTSTATUS check_base_file_access(struct connection_struct *conn,
310 struct smb_filename *smb_fname,
311 uint32_t access_mask)
313 NTSTATUS status;
315 status = smbd_calculate_access_mask(conn, smb_fname,
316 false,
317 access_mask,
318 &access_mask);
319 if (!NT_STATUS_IS_OK(status)) {
320 DEBUG(10, ("smbd_calculate_access_mask "
321 "on file %s returned %s\n",
322 smb_fname_str_dbg(smb_fname),
323 nt_errstr(status)));
324 return status;
327 if (access_mask & (FILE_WRITE_DATA|FILE_APPEND_DATA)) {
328 uint32_t dosattrs;
329 if (!CAN_WRITE(conn)) {
330 return NT_STATUS_ACCESS_DENIED;
332 dosattrs = dos_mode(conn, smb_fname);
333 if (IS_DOS_READONLY(dosattrs)) {
334 return NT_STATUS_ACCESS_DENIED;
338 return smbd_check_access_rights(conn,
339 smb_fname,
340 false,
341 access_mask);
344 /****************************************************************************
345 fd support routines - attempt to do a dos_open.
346 ****************************************************************************/
348 NTSTATUS fd_open(struct connection_struct *conn,
349 files_struct *fsp,
350 int flags,
351 mode_t mode)
353 struct smb_filename *smb_fname = fsp->fsp_name;
354 NTSTATUS status = NT_STATUS_OK;
356 #ifdef O_NOFOLLOW
358 * Never follow symlinks on a POSIX client. The
359 * client should be doing this.
362 if (fsp->posix_open || !lp_symlinks(SNUM(conn))) {
363 flags |= O_NOFOLLOW;
365 #endif
367 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, mode);
368 if (fsp->fh->fd == -1) {
369 int posix_errno = errno;
370 #ifdef O_NOFOLLOW
371 #if defined(ENOTSUP) && defined(OSF1)
372 /* handle special Tru64 errno */
373 if (errno == ENOTSUP) {
374 posix_errno = ELOOP;
376 #endif /* ENOTSUP */
377 #ifdef EFTYPE
378 /* fix broken NetBSD errno */
379 if (errno == EFTYPE) {
380 posix_errno = ELOOP;
382 #endif /* EFTYPE */
383 /* fix broken FreeBSD errno */
384 if (errno == EMLINK) {
385 posix_errno = ELOOP;
387 #endif /* O_NOFOLLOW */
388 status = map_nt_error_from_unix(posix_errno);
389 if (errno == EMFILE) {
390 static time_t last_warned = 0L;
392 if (time((time_t *) NULL) > last_warned) {
393 DEBUG(0,("Too many open files, unable "
394 "to open more! smbd's max "
395 "open files = %d\n",
396 lp_max_open_files()));
397 last_warned = time((time_t *) NULL);
403 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
404 smb_fname_str_dbg(smb_fname), flags, (int)mode, fsp->fh->fd,
405 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
407 return status;
410 /****************************************************************************
411 Close the file associated with a fsp.
412 ****************************************************************************/
414 NTSTATUS fd_close(files_struct *fsp)
416 int ret;
418 if (fsp->dptr) {
419 dptr_CloseDir(fsp);
421 if (fsp->fh->fd == -1) {
422 return NT_STATUS_OK; /* What we used to call a stat open. */
424 if (fsp->fh->ref_count > 1) {
425 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
428 ret = SMB_VFS_CLOSE(fsp);
429 fsp->fh->fd = -1;
430 if (ret == -1) {
431 return map_nt_error_from_unix(errno);
433 return NT_STATUS_OK;
436 /****************************************************************************
437 Change the ownership of a file to that of the parent directory.
438 Do this by fd if possible.
439 ****************************************************************************/
441 void change_file_owner_to_parent(connection_struct *conn,
442 const char *inherit_from_dir,
443 files_struct *fsp)
445 struct smb_filename *smb_fname_parent;
446 int ret;
448 smb_fname_parent = synthetic_smb_fname(talloc_tos(), inherit_from_dir,
449 NULL, NULL);
450 if (smb_fname_parent == NULL) {
451 return;
454 ret = SMB_VFS_STAT(conn, smb_fname_parent);
455 if (ret == -1) {
456 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
457 "directory %s. Error was %s\n",
458 smb_fname_str_dbg(smb_fname_parent),
459 strerror(errno)));
460 TALLOC_FREE(smb_fname_parent);
461 return;
464 if (smb_fname_parent->st.st_ex_uid == fsp->fsp_name->st.st_ex_uid) {
465 /* Already this uid - no need to change. */
466 DEBUG(10,("change_file_owner_to_parent: file %s "
467 "is already owned by uid %d\n",
468 fsp_str_dbg(fsp),
469 (int)fsp->fsp_name->st.st_ex_uid ));
470 TALLOC_FREE(smb_fname_parent);
471 return;
474 become_root();
475 ret = SMB_VFS_FCHOWN(fsp, smb_fname_parent->st.st_ex_uid, (gid_t)-1);
476 unbecome_root();
477 if (ret == -1) {
478 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
479 "file %s to parent directory uid %u. Error "
480 "was %s\n", fsp_str_dbg(fsp),
481 (unsigned int)smb_fname_parent->st.st_ex_uid,
482 strerror(errno) ));
483 } else {
484 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
485 "parent directory uid %u.\n", fsp_str_dbg(fsp),
486 (unsigned int)smb_fname_parent->st.st_ex_uid));
487 /* Ensure the uid entry is updated. */
488 fsp->fsp_name->st.st_ex_uid = smb_fname_parent->st.st_ex_uid;
491 TALLOC_FREE(smb_fname_parent);
494 NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
495 const char *inherit_from_dir,
496 const char *fname,
497 SMB_STRUCT_STAT *psbuf)
499 struct smb_filename *smb_fname_parent;
500 struct smb_filename *smb_fname_cwd = NULL;
501 char *saved_dir = NULL;
502 TALLOC_CTX *ctx = talloc_tos();
503 NTSTATUS status = NT_STATUS_OK;
504 int ret;
506 smb_fname_parent = synthetic_smb_fname(ctx, inherit_from_dir,
507 NULL, NULL);
508 if (smb_fname_parent == NULL) {
509 return NT_STATUS_NO_MEMORY;
512 ret = SMB_VFS_STAT(conn, smb_fname_parent);
513 if (ret == -1) {
514 status = map_nt_error_from_unix(errno);
515 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
516 "directory %s. Error was %s\n",
517 smb_fname_str_dbg(smb_fname_parent),
518 strerror(errno)));
519 goto out;
522 /* We've already done an lstat into psbuf, and we know it's a
523 directory. If we can cd into the directory and the dev/ino
524 are the same then we can safely chown without races as
525 we're locking the directory in place by being in it. This
526 should work on any UNIX (thanks tridge :-). JRA.
529 saved_dir = vfs_GetWd(ctx,conn);
530 if (!saved_dir) {
531 status = map_nt_error_from_unix(errno);
532 DEBUG(0,("change_dir_owner_to_parent: failed to get "
533 "current working directory. Error was %s\n",
534 strerror(errno)));
535 goto out;
538 /* Chdir into the new path. */
539 if (vfs_ChDir(conn, fname) == -1) {
540 status = map_nt_error_from_unix(errno);
541 DEBUG(0,("change_dir_owner_to_parent: failed to change "
542 "current working directory to %s. Error "
543 "was %s\n", fname, strerror(errno) ));
544 goto chdir;
547 smb_fname_cwd = synthetic_smb_fname(ctx, ".", NULL, NULL);
548 if (smb_fname_cwd == NULL) {
549 status = NT_STATUS_NO_MEMORY;
550 goto chdir;
553 ret = SMB_VFS_STAT(conn, smb_fname_cwd);
554 if (ret == -1) {
555 status = map_nt_error_from_unix(errno);
556 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
557 "directory '.' (%s) Error was %s\n",
558 fname, strerror(errno)));
559 goto chdir;
562 /* Ensure we're pointing at the same place. */
563 if (smb_fname_cwd->st.st_ex_dev != psbuf->st_ex_dev ||
564 smb_fname_cwd->st.st_ex_ino != psbuf->st_ex_ino) {
565 DEBUG(0,("change_dir_owner_to_parent: "
566 "device/inode on directory %s changed. "
567 "Refusing to chown !\n", fname ));
568 status = NT_STATUS_ACCESS_DENIED;
569 goto chdir;
572 if (smb_fname_parent->st.st_ex_uid == smb_fname_cwd->st.st_ex_uid) {
573 /* Already this uid - no need to change. */
574 DEBUG(10,("change_dir_owner_to_parent: directory %s "
575 "is already owned by uid %d\n",
576 fname,
577 (int)smb_fname_cwd->st.st_ex_uid ));
578 status = NT_STATUS_OK;
579 goto chdir;
582 become_root();
583 ret = SMB_VFS_LCHOWN(conn, ".", smb_fname_parent->st.st_ex_uid,
584 (gid_t)-1);
585 unbecome_root();
586 if (ret == -1) {
587 status = map_nt_error_from_unix(errno);
588 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
589 "directory %s to parent directory uid %u. "
590 "Error was %s\n", fname,
591 (unsigned int)smb_fname_parent->st.st_ex_uid,
592 strerror(errno) ));
593 } else {
594 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
595 "directory %s to parent directory uid %u.\n",
596 fname, (unsigned int)smb_fname_parent->st.st_ex_uid ));
597 /* Ensure the uid entry is updated. */
598 psbuf->st_ex_uid = smb_fname_parent->st.st_ex_uid;
601 chdir:
602 vfs_ChDir(conn,saved_dir);
603 out:
604 TALLOC_FREE(smb_fname_parent);
605 TALLOC_FREE(smb_fname_cwd);
606 return status;
609 /****************************************************************************
610 Open a file - returning a guaranteed ATOMIC indication of if the
611 file was created or not.
612 ****************************************************************************/
614 static NTSTATUS fd_open_atomic(struct connection_struct *conn,
615 files_struct *fsp,
616 int flags,
617 mode_t mode,
618 bool *file_created)
620 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
621 bool file_existed = VALID_STAT(fsp->fsp_name->st);
623 *file_created = false;
625 if (!(flags & O_CREAT)) {
627 * We're not creating the file, just pass through.
629 return fd_open(conn, fsp, flags, mode);
632 if (flags & O_EXCL) {
634 * Fail if already exists, just pass through.
636 status = fd_open(conn, fsp, flags, mode);
639 * Here we've opened with O_CREAT|O_EXCL. If that went
640 * NT_STATUS_OK, we *know* we created this file.
642 *file_created = NT_STATUS_IS_OK(status);
644 return status;
648 * Now it gets tricky. We have O_CREAT, but not O_EXCL.
649 * To know absolutely if we created the file or not,
650 * we can never call O_CREAT without O_EXCL. So if
651 * we think the file existed, try without O_CREAT|O_EXCL.
652 * If we think the file didn't exist, try with
653 * O_CREAT|O_EXCL. Keep bouncing between these two
654 * requests until either the file is created, or
655 * opened. Either way, we keep going until we get
656 * a returnable result (error, or open/create).
659 while(1) {
660 int curr_flags = flags;
662 if (file_existed) {
663 /* Just try open, do not create. */
664 curr_flags &= ~(O_CREAT);
665 status = fd_open(conn, fsp, curr_flags, mode);
666 if (NT_STATUS_EQUAL(status,
667 NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
669 * Someone deleted it in the meantime.
670 * Retry with O_EXCL.
672 file_existed = false;
673 DEBUG(10,("fd_open_atomic: file %s existed. "
674 "Retry.\n",
675 smb_fname_str_dbg(fsp->fsp_name)));
676 continue;
678 } else {
679 /* Try create exclusively, fail if it exists. */
680 curr_flags |= O_EXCL;
681 status = fd_open(conn, fsp, curr_flags, mode);
682 if (NT_STATUS_EQUAL(status,
683 NT_STATUS_OBJECT_NAME_COLLISION)) {
685 * Someone created it in the meantime.
686 * Retry without O_CREAT.
688 file_existed = true;
689 DEBUG(10,("fd_open_atomic: file %s "
690 "did not exist. Retry.\n",
691 smb_fname_str_dbg(fsp->fsp_name)));
692 continue;
694 if (NT_STATUS_IS_OK(status)) {
696 * Here we've opened with O_CREAT|O_EXCL
697 * and got success. We *know* we created
698 * this file.
700 *file_created = true;
703 /* Create is done, or failed. */
704 break;
706 return status;
709 /****************************************************************************
710 Open a file.
711 ****************************************************************************/
713 static NTSTATUS open_file(files_struct *fsp,
714 connection_struct *conn,
715 struct smb_request *req,
716 const char *parent_dir,
717 int flags,
718 mode_t unx_mode,
719 uint32 access_mask, /* client requested access mask. */
720 uint32 open_access_mask, /* what we're actually using in the open. */
721 bool *p_file_created)
723 struct smb_filename *smb_fname = fsp->fsp_name;
724 NTSTATUS status = NT_STATUS_OK;
725 int accmode = (flags & O_ACCMODE);
726 int local_flags = flags;
727 bool file_existed = VALID_STAT(fsp->fsp_name->st);
729 fsp->fh->fd = -1;
730 errno = EPERM;
732 /* Check permissions */
735 * This code was changed after seeing a client open request
736 * containing the open mode of (DENY_WRITE/read-only) with
737 * the 'create if not exist' bit set. The previous code
738 * would fail to open the file read only on a read-only share
739 * as it was checking the flags parameter directly against O_RDONLY,
740 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
741 * JRA.
744 if (!CAN_WRITE(conn)) {
745 /* It's a read-only share - fail if we wanted to write. */
746 if(accmode != O_RDONLY || (flags & O_TRUNC) || (flags & O_APPEND)) {
747 DEBUG(3,("Permission denied opening %s\n",
748 smb_fname_str_dbg(smb_fname)));
749 return NT_STATUS_ACCESS_DENIED;
751 if (flags & O_CREAT) {
752 /* We don't want to write - but we must make sure that
753 O_CREAT doesn't create the file if we have write
754 access into the directory.
756 flags &= ~(O_CREAT|O_EXCL);
757 local_flags &= ~(O_CREAT|O_EXCL);
762 * This little piece of insanity is inspired by the
763 * fact that an NT client can open a file for O_RDONLY,
764 * but set the create disposition to FILE_EXISTS_TRUNCATE.
765 * If the client *can* write to the file, then it expects to
766 * truncate the file, even though it is opening for readonly.
767 * Quicken uses this stupid trick in backup file creation...
768 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
769 * for helping track this one down. It didn't bite us in 2.0.x
770 * as we always opened files read-write in that release. JRA.
773 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
774 DEBUG(10,("open_file: truncate requested on read-only open "
775 "for file %s\n", smb_fname_str_dbg(smb_fname)));
776 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
779 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
780 (!file_existed && (local_flags & O_CREAT)) ||
781 ((local_flags & O_TRUNC) == O_TRUNC) ) {
782 const char *wild;
783 int ret;
785 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
787 * We would block on opening a FIFO with no one else on the
788 * other end. Do what we used to do and add O_NONBLOCK to the
789 * open flags. JRA.
792 if (file_existed && S_ISFIFO(smb_fname->st.st_ex_mode)) {
793 local_flags &= ~O_TRUNC; /* Can't truncate a FIFO. */
794 local_flags |= O_NONBLOCK;
796 #endif
798 /* Don't create files with Microsoft wildcard characters. */
799 if (fsp->base_fsp) {
801 * wildcard characters are allowed in stream names
802 * only test the basefilename
804 wild = fsp->base_fsp->fsp_name->base_name;
805 } else {
806 wild = smb_fname->base_name;
808 if ((local_flags & O_CREAT) && !file_existed &&
809 ms_has_wild(wild)) {
810 return NT_STATUS_OBJECT_NAME_INVALID;
813 /* Can we access this file ? */
814 if (!fsp->base_fsp) {
815 /* Only do this check on non-stream open. */
816 if (file_existed) {
817 status = smbd_check_access_rights(conn,
818 smb_fname,
819 false,
820 access_mask);
822 if (!NT_STATUS_IS_OK(status)) {
823 DEBUG(10, ("open_file: "
824 "smbd_check_access_rights "
825 "on file %s returned %s\n",
826 smb_fname_str_dbg(smb_fname),
827 nt_errstr(status)));
830 if (!NT_STATUS_IS_OK(status) &&
831 !NT_STATUS_EQUAL(status,
832 NT_STATUS_OBJECT_NAME_NOT_FOUND))
834 return status;
837 if (NT_STATUS_EQUAL(status,
838 NT_STATUS_OBJECT_NAME_NOT_FOUND))
840 DEBUG(10, ("open_file: "
841 "file %s vanished since we "
842 "checked for existence.\n",
843 smb_fname_str_dbg(smb_fname)));
844 file_existed = false;
845 SET_STAT_INVALID(fsp->fsp_name->st);
849 if (!file_existed) {
850 if (!(local_flags & O_CREAT)) {
851 /* File didn't exist and no O_CREAT. */
852 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
855 status = check_parent_access(conn,
856 smb_fname,
857 SEC_DIR_ADD_FILE);
858 if (!NT_STATUS_IS_OK(status)) {
859 DEBUG(10, ("open_file: "
860 "check_parent_access on "
861 "file %s returned %s\n",
862 smb_fname_str_dbg(smb_fname),
863 nt_errstr(status) ));
864 return status;
870 * Actually do the open - if O_TRUNC is needed handle it
871 * below under the share mode lock.
873 status = fd_open_atomic(conn, fsp, local_flags & ~O_TRUNC,
874 unx_mode, p_file_created);
875 if (!NT_STATUS_IS_OK(status)) {
876 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
877 "(flags=%d)\n", smb_fname_str_dbg(smb_fname),
878 nt_errstr(status),local_flags,flags));
879 return status;
882 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
883 if (ret == -1) {
884 /* If we have an fd, this stat should succeed. */
885 DEBUG(0,("Error doing fstat on open file %s "
886 "(%s)\n",
887 smb_fname_str_dbg(smb_fname),
888 strerror(errno) ));
889 status = map_nt_error_from_unix(errno);
890 fd_close(fsp);
891 return status;
894 if (*p_file_created) {
895 /* We created this file. */
897 bool need_re_stat = false;
898 /* Do all inheritance work after we've
899 done a successful fstat call and filled
900 in the stat struct in fsp->fsp_name. */
902 /* Inherit the ACL if required */
903 if (lp_inherit_perms(SNUM(conn))) {
904 inherit_access_posix_acl(conn, parent_dir,
905 smb_fname->base_name,
906 unx_mode);
907 need_re_stat = true;
910 /* Change the owner if required. */
911 if (lp_inherit_owner(SNUM(conn))) {
912 change_file_owner_to_parent(conn, parent_dir,
913 fsp);
914 need_re_stat = true;
917 if (need_re_stat) {
918 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
919 /* If we have an fd, this stat should succeed. */
920 if (ret == -1) {
921 DEBUG(0,("Error doing fstat on open file %s "
922 "(%s)\n",
923 smb_fname_str_dbg(smb_fname),
924 strerror(errno) ));
928 notify_fname(conn, NOTIFY_ACTION_ADDED,
929 FILE_NOTIFY_CHANGE_FILE_NAME,
930 smb_fname->base_name);
932 } else {
933 fsp->fh->fd = -1; /* What we used to call a stat open. */
934 if (!file_existed) {
935 /* File must exist for a stat open. */
936 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
939 status = smbd_check_access_rights(conn,
940 smb_fname,
941 false,
942 access_mask);
944 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
945 fsp->posix_open &&
946 S_ISLNK(smb_fname->st.st_ex_mode)) {
947 /* This is a POSIX stat open for delete
948 * or rename on a symlink that points
949 * nowhere. Allow. */
950 DEBUG(10,("open_file: allowing POSIX "
951 "open on bad symlink %s\n",
952 smb_fname_str_dbg(smb_fname)));
953 status = NT_STATUS_OK;
956 if (!NT_STATUS_IS_OK(status)) {
957 DEBUG(10,("open_file: "
958 "smbd_check_access_rights on file "
959 "%s returned %s\n",
960 smb_fname_str_dbg(smb_fname),
961 nt_errstr(status) ));
962 return status;
967 * POSIX allows read-only opens of directories. We don't
968 * want to do this (we use a different code path for this)
969 * so catch a directory open and return an EISDIR. JRA.
972 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
973 fd_close(fsp);
974 errno = EISDIR;
975 return NT_STATUS_FILE_IS_A_DIRECTORY;
978 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
979 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
980 fsp->file_pid = req ? req->smbpid : 0;
981 fsp->can_lock = True;
982 fsp->can_read = ((access_mask & FILE_READ_DATA) != 0);
983 fsp->can_write =
984 CAN_WRITE(conn) &&
985 ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) != 0);
986 fsp->print_file = NULL;
987 fsp->modified = False;
988 fsp->sent_oplock_break = NO_BREAK_SENT;
989 fsp->is_directory = False;
990 if (conn->aio_write_behind_list &&
991 is_in_path(smb_fname->base_name, conn->aio_write_behind_list,
992 conn->case_sensitive)) {
993 fsp->aio_write_behind = True;
996 fsp->wcp = NULL; /* Write cache pointer. */
998 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
999 conn->session_info->unix_info->unix_name,
1000 smb_fname_str_dbg(smb_fname),
1001 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
1002 conn->num_files_open));
1004 errno = 0;
1005 return NT_STATUS_OK;
1008 /****************************************************************************
1009 Check if we can open a file with a share mode.
1010 Returns True if conflict, False if not.
1011 ****************************************************************************/
1013 static bool share_conflict(struct share_mode_entry *entry,
1014 uint32 access_mask,
1015 uint32 share_access)
1017 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
1018 "entry->share_access = 0x%x, "
1019 "entry->private_options = 0x%x\n",
1020 (unsigned int)entry->access_mask,
1021 (unsigned int)entry->share_access,
1022 (unsigned int)entry->private_options));
1024 if (server_id_is_disconnected(&entry->pid)) {
1026 * note: cleanup should have been done by
1027 * delay_for_batch_oplocks()
1029 return false;
1032 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
1033 (unsigned int)access_mask, (unsigned int)share_access));
1035 if ((entry->access_mask & (FILE_WRITE_DATA|
1036 FILE_APPEND_DATA|
1037 FILE_READ_DATA|
1038 FILE_EXECUTE|
1039 DELETE_ACCESS)) == 0) {
1040 DEBUG(10,("share_conflict: No conflict due to "
1041 "entry->access_mask = 0x%x\n",
1042 (unsigned int)entry->access_mask ));
1043 return False;
1046 if ((access_mask & (FILE_WRITE_DATA|
1047 FILE_APPEND_DATA|
1048 FILE_READ_DATA|
1049 FILE_EXECUTE|
1050 DELETE_ACCESS)) == 0) {
1051 DEBUG(10,("share_conflict: No conflict due to "
1052 "access_mask = 0x%x\n",
1053 (unsigned int)access_mask ));
1054 return False;
1057 #if 1 /* JRA TEST - Superdebug. */
1058 #define CHECK_MASK(num, am, right, sa, share) \
1059 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
1060 (unsigned int)(num), (unsigned int)(am), \
1061 (unsigned int)(right), (unsigned int)(am)&(right) )); \
1062 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
1063 (unsigned int)(num), (unsigned int)(sa), \
1064 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
1065 if (((am) & (right)) && !((sa) & (share))) { \
1066 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
1067 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
1068 (unsigned int)(share) )); \
1069 return True; \
1071 #else
1072 #define CHECK_MASK(num, am, right, sa, share) \
1073 if (((am) & (right)) && !((sa) & (share))) { \
1074 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
1075 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
1076 (unsigned int)(share) )); \
1077 return True; \
1079 #endif
1081 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
1082 share_access, FILE_SHARE_WRITE);
1083 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
1084 entry->share_access, FILE_SHARE_WRITE);
1086 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
1087 share_access, FILE_SHARE_READ);
1088 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
1089 entry->share_access, FILE_SHARE_READ);
1091 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
1092 share_access, FILE_SHARE_DELETE);
1093 CHECK_MASK(6, access_mask, DELETE_ACCESS,
1094 entry->share_access, FILE_SHARE_DELETE);
1096 DEBUG(10,("share_conflict: No conflict.\n"));
1097 return False;
1100 #if defined(DEVELOPER)
1101 static void validate_my_share_entries(struct smbd_server_connection *sconn,
1102 int num,
1103 struct share_mode_entry *share_entry)
1105 struct server_id self = messaging_server_id(sconn->msg_ctx);
1106 files_struct *fsp;
1108 if (!serverid_equal(&self, &share_entry->pid)) {
1109 return;
1112 if (share_entry->op_mid == 0) {
1113 /* INTERNAL_OPEN_ONLY */
1114 return;
1117 if (!is_valid_share_mode_entry(share_entry)) {
1118 return;
1121 fsp = file_find_dif(sconn, share_entry->id,
1122 share_entry->share_file_id);
1123 if (!fsp) {
1124 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1125 share_mode_str(talloc_tos(), num, share_entry) ));
1126 smb_panic("validate_my_share_entries: Cannot match a "
1127 "share entry with an open file\n");
1130 if ((share_entry->op_type == NO_OPLOCK) &&
1131 (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK))
1133 /* Someone has already written to it, but I haven't yet
1134 * noticed */
1135 return;
1138 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
1139 goto panic;
1142 return;
1144 panic:
1146 char *str;
1147 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1148 share_mode_str(talloc_tos(), num, share_entry) ));
1149 str = talloc_asprintf(talloc_tos(),
1150 "validate_my_share_entries: "
1151 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
1152 fsp->fsp_name->base_name,
1153 (unsigned int)fsp->oplock_type,
1154 (unsigned int)share_entry->op_type );
1155 smb_panic(str);
1158 #endif
1160 bool is_stat_open(uint32 access_mask)
1162 const uint32_t stat_open_bits =
1163 (SYNCHRONIZE_ACCESS|
1164 FILE_READ_ATTRIBUTES|
1165 FILE_WRITE_ATTRIBUTES);
1167 return (((access_mask & stat_open_bits) != 0) &&
1168 ((access_mask & ~stat_open_bits) == 0));
1171 /****************************************************************************
1172 Deal with share modes
1173 Invarient: Share mode must be locked on entry and exit.
1174 Returns -1 on error, or number of share modes on success (may be zero).
1175 ****************************************************************************/
1177 static NTSTATUS open_mode_check(connection_struct *conn,
1178 struct share_mode_lock *lck,
1179 uint32_t name_hash,
1180 uint32 access_mask,
1181 uint32 share_access,
1182 uint32 create_options,
1183 bool *file_existed)
1185 int i;
1187 if(lck->data->num_share_modes == 0) {
1188 return NT_STATUS_OK;
1191 /* A delete on close prohibits everything */
1193 if (is_delete_on_close_set(lck, name_hash)) {
1195 * Check the delete on close token
1196 * is valid. It could have been left
1197 * after a server crash.
1199 for(i = 0; i < lck->data->num_share_modes; i++) {
1200 if (!share_mode_stale_pid(lck->data, i)) {
1202 *file_existed = true;
1204 return NT_STATUS_DELETE_PENDING;
1207 return NT_STATUS_OK;
1210 if (is_stat_open(access_mask)) {
1211 /* Stat open that doesn't trigger oplock breaks or share mode
1212 * checks... ! JRA. */
1213 return NT_STATUS_OK;
1217 * Check if the share modes will give us access.
1220 #if defined(DEVELOPER)
1221 for(i = 0; i < lck->data->num_share_modes; i++) {
1222 validate_my_share_entries(conn->sconn, i,
1223 &lck->data->share_modes[i]);
1225 #endif
1227 /* Now we check the share modes, after any oplock breaks. */
1228 for(i = 0; i < lck->data->num_share_modes; i++) {
1230 if (!is_valid_share_mode_entry(&lck->data->share_modes[i])) {
1231 continue;
1234 /* someone else has a share lock on it, check to see if we can
1235 * too */
1236 if (share_conflict(&lck->data->share_modes[i],
1237 access_mask, share_access)) {
1239 if (share_mode_stale_pid(lck->data, i)) {
1240 continue;
1243 *file_existed = true;
1245 return NT_STATUS_SHARING_VIOLATION;
1249 if (lck->data->num_share_modes != 0) {
1250 *file_existed = true;
1253 return NT_STATUS_OK;
1257 * Send a break message to the oplock holder and delay the open for
1258 * our client.
1261 static NTSTATUS send_break_message(files_struct *fsp,
1262 struct share_mode_entry *exclusive,
1263 uint64_t mid,
1264 int oplock_request)
1266 NTSTATUS status;
1267 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
1269 DEBUG(10, ("Sending break request to PID %s\n",
1270 procid_str_static(&exclusive->pid)));
1271 exclusive->op_mid = mid;
1273 /* Create the message. */
1274 share_mode_entry_to_message(msg, exclusive);
1276 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
1277 don't want this set in the share mode struct pointed to by lck. */
1279 if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
1280 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,
1281 exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
1284 status = messaging_send_buf(fsp->conn->sconn->msg_ctx, exclusive->pid,
1285 MSG_SMB_BREAK_REQUEST,
1286 (uint8 *)msg,
1287 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
1288 if (!NT_STATUS_IS_OK(status)) {
1289 DEBUG(3, ("Could not send oplock break message: %s\n",
1290 nt_errstr(status)));
1293 return status;
1297 * Return share_mode_entry pointers for :
1298 * 1). Batch oplock entry.
1299 * 2). Batch or exclusive oplock entry (may be identical to #1).
1300 * bool have_level2_oplock
1301 * bool have_no_oplock.
1302 * Do internal consistency checks on the share mode for a file.
1305 static void find_oplock_types(files_struct *fsp,
1306 int oplock_request,
1307 const struct share_mode_lock *lck,
1308 struct share_mode_entry **pp_batch,
1309 struct share_mode_entry **pp_ex_or_batch,
1310 bool *got_level2,
1311 bool *got_no_oplock)
1313 int i;
1315 *pp_batch = NULL;
1316 *pp_ex_or_batch = NULL;
1317 *got_level2 = false;
1318 *got_no_oplock = false;
1320 /* Ignore stat or internal opens, as is done in
1321 delay_for_batch_oplocks() and
1322 delay_for_exclusive_oplocks().
1324 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1325 return;
1328 for (i=0; i<lck->data->num_share_modes; i++) {
1329 struct share_mode_entry *e = &lck->data->share_modes[i];
1331 if (!is_valid_share_mode_entry(e)) {
1332 continue;
1335 if (e->op_mid == 0) {
1336 /* INTERNAL_OPEN_ONLY */
1337 continue;
1340 if (e->op_type == NO_OPLOCK && is_stat_open(e->access_mask)) {
1341 /* We ignore stat opens in the table - they
1342 always have NO_OPLOCK and never get or
1343 cause breaks. JRA. */
1344 continue;
1347 if (BATCH_OPLOCK_TYPE(e->op_type)) {
1348 /* batch - can only be one. */
1349 if (share_mode_stale_pid(lck->data, i)) {
1350 DEBUG(10, ("Found stale batch oplock\n"));
1351 continue;
1353 if (*pp_ex_or_batch || *pp_batch || *got_level2 || *got_no_oplock) {
1354 smb_panic("Bad batch oplock entry.");
1356 *pp_batch = e;
1359 if (EXCLUSIVE_OPLOCK_TYPE(e->op_type)) {
1360 if (share_mode_stale_pid(lck->data, i)) {
1361 DEBUG(10, ("Found stale duplicate oplock\n"));
1362 continue;
1364 /* Exclusive or batch - can only be one. */
1365 if (*pp_ex_or_batch || *got_level2 || *got_no_oplock) {
1366 smb_panic("Bad exclusive or batch oplock entry.");
1368 *pp_ex_or_batch = e;
1371 if (LEVEL_II_OPLOCK_TYPE(e->op_type)) {
1372 if (*pp_batch || *pp_ex_or_batch) {
1373 if (share_mode_stale_pid(lck->data, i)) {
1374 DEBUG(10, ("Found stale LevelII "
1375 "oplock\n"));
1376 continue;
1378 smb_panic("Bad levelII oplock entry.");
1380 *got_level2 = true;
1383 if (e->op_type == NO_OPLOCK) {
1384 if (*pp_batch || *pp_ex_or_batch) {
1385 if (share_mode_stale_pid(lck->data, i)) {
1386 DEBUG(10, ("Found stale NO_OPLOCK "
1387 "entry\n"));
1388 continue;
1390 smb_panic("Bad no oplock entry.");
1392 *got_no_oplock = true;
1397 static bool delay_for_batch_oplocks(files_struct *fsp,
1398 uint64_t mid,
1399 int oplock_request,
1400 struct share_mode_entry *batch_entry)
1402 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1403 return false;
1405 if (batch_entry == NULL) {
1406 return false;
1409 if (server_id_is_disconnected(&batch_entry->pid)) {
1411 * TODO: clean up.
1412 * This could be achieved by sending a break message
1413 * to ourselves. Special considerations for files
1414 * with delete_on_close flag set!
1416 * For now we keep it simple and do not
1417 * allow delete on close for durable handles.
1419 return false;
1422 /* Found a batch oplock */
1423 send_break_message(fsp, batch_entry, mid, oplock_request);
1424 return true;
1427 static bool delay_for_exclusive_oplocks(files_struct *fsp,
1428 uint64_t mid,
1429 int oplock_request,
1430 struct share_mode_entry *ex_entry)
1432 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1433 return false;
1435 if (ex_entry == NULL) {
1436 return false;
1439 if (server_id_is_disconnected(&ex_entry->pid)) {
1441 * since only durable handles can get disconnected,
1442 * and we can only get durable handles with batch oplocks,
1443 * this should actually never be reached...
1445 return false;
1448 send_break_message(fsp, ex_entry, mid, oplock_request);
1449 return true;
1452 static bool file_has_brlocks(files_struct *fsp)
1454 struct byte_range_lock *br_lck;
1456 br_lck = brl_get_locks_readonly(fsp);
1457 if (!br_lck)
1458 return false;
1460 return br_lck->num_locks > 0 ? true : false;
1463 static void grant_fsp_oplock_type(files_struct *fsp,
1464 int oplock_request,
1465 bool got_level2_oplock,
1466 bool got_a_none_oplock)
1468 bool allow_level2 = (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
1469 lp_level2_oplocks(SNUM(fsp->conn));
1471 /* Start by granting what the client asked for,
1472 but ensure no SAMBA_PRIVATE bits can be set. */
1473 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1475 if (oplock_request & INTERNAL_OPEN_ONLY) {
1476 /* No oplocks on internal open. */
1477 fsp->oplock_type = NO_OPLOCK;
1478 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1479 fsp->oplock_type, fsp_str_dbg(fsp)));
1480 return;
1483 if (lp_locking(fsp->conn->params) && file_has_brlocks(fsp)) {
1484 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1485 fsp_str_dbg(fsp)));
1486 fsp->oplock_type = NO_OPLOCK;
1489 if (is_stat_open(fsp->access_mask)) {
1490 /* Leave the value already set. */
1491 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1492 fsp->oplock_type, fsp_str_dbg(fsp)));
1493 return;
1497 * Match what was requested (fsp->oplock_type) with
1498 * what was found in the existing share modes.
1501 if (got_a_none_oplock) {
1502 fsp->oplock_type = NO_OPLOCK;
1503 } else if (got_level2_oplock) {
1504 if (fsp->oplock_type == NO_OPLOCK ||
1505 fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
1506 /* Store a level2 oplock, but don't tell the client */
1507 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1508 } else {
1509 fsp->oplock_type = LEVEL_II_OPLOCK;
1511 } else {
1512 /* All share_mode_entries are placeholders or deferred.
1513 * Silently upgrade to fake levelII if the client didn't
1514 * ask for an oplock. */
1515 if (fsp->oplock_type == NO_OPLOCK) {
1516 /* Store a level2 oplock, but don't tell the client */
1517 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1522 * Don't grant level2 to clients that don't want them
1523 * or if we've turned them off.
1525 if (fsp->oplock_type == LEVEL_II_OPLOCK && !allow_level2) {
1526 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1529 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1530 fsp->oplock_type, fsp_str_dbg(fsp)));
1533 static bool request_timed_out(struct timeval request_time,
1534 struct timeval timeout)
1536 struct timeval now, end_time;
1537 GetTimeOfDay(&now);
1538 end_time = timeval_sum(&request_time, &timeout);
1539 return (timeval_compare(&end_time, &now) < 0);
1542 struct defer_open_state {
1543 struct smbd_server_connection *sconn;
1544 uint64_t mid;
1547 static void defer_open_done(struct tevent_req *req);
1549 /****************************************************************************
1550 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1551 ****************************************************************************/
1553 static void defer_open(struct share_mode_lock *lck,
1554 struct timeval request_time,
1555 struct timeval timeout,
1556 struct smb_request *req,
1557 struct deferred_open_record *state)
1559 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1560 "open entry for mid %llu\n",
1561 (unsigned int)request_time.tv_sec,
1562 (unsigned int)request_time.tv_usec,
1563 (unsigned long long)req->mid));
1565 if (!push_deferred_open_message_smb(req, request_time, timeout,
1566 state->id, (char *)state, sizeof(*state))) {
1567 TALLOC_FREE(lck);
1568 exit_server("push_deferred_open_message_smb failed");
1570 if (lck) {
1571 struct defer_open_state *watch_state;
1572 struct tevent_req *watch_req;
1573 bool ret;
1575 watch_state = talloc(req->sconn, struct defer_open_state);
1576 if (watch_state == NULL) {
1577 exit_server("talloc failed");
1579 watch_state->sconn = req->sconn;
1580 watch_state->mid = req->mid;
1582 DEBUG(10, ("defering mid %llu\n",
1583 (unsigned long long)req->mid));
1585 watch_req = dbwrap_record_watch_send(
1586 watch_state, req->sconn->ev_ctx, lck->data->record,
1587 req->sconn->msg_ctx);
1588 if (watch_req == NULL) {
1589 exit_server("Could not watch share mode record");
1591 tevent_req_set_callback(watch_req, defer_open_done,
1592 watch_state);
1594 ret = tevent_req_set_endtime(
1595 watch_req, req->sconn->ev_ctx,
1596 timeval_sum(&request_time, &timeout));
1597 SMB_ASSERT(ret);
1601 static void defer_open_done(struct tevent_req *req)
1603 struct defer_open_state *state = tevent_req_callback_data(
1604 req, struct defer_open_state);
1605 NTSTATUS status;
1606 bool ret;
1608 status = dbwrap_record_watch_recv(req, talloc_tos(), NULL);
1609 TALLOC_FREE(req);
1610 if (!NT_STATUS_IS_OK(status)) {
1611 DEBUG(5, ("dbwrap_record_watch_recv returned %s\n",
1612 nt_errstr(status)));
1614 * Even if it failed, retry anyway. TODO: We need a way to
1615 * tell a re-scheduled open about that error.
1619 DEBUG(10, ("scheduling mid %llu\n", (unsigned long long)state->mid));
1621 ret = schedule_deferred_open_message_smb(state->sconn, state->mid);
1622 SMB_ASSERT(ret);
1623 TALLOC_FREE(state);
1627 /****************************************************************************
1628 On overwrite open ensure that the attributes match.
1629 ****************************************************************************/
1631 static bool open_match_attributes(connection_struct *conn,
1632 uint32 old_dos_attr,
1633 uint32 new_dos_attr,
1634 mode_t existing_unx_mode,
1635 mode_t new_unx_mode,
1636 mode_t *returned_unx_mode)
1638 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
1640 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1641 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1643 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
1644 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
1645 *returned_unx_mode = new_unx_mode;
1646 } else {
1647 *returned_unx_mode = (mode_t)0;
1650 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1651 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1652 "returned_unx_mode = 0%o\n",
1653 (unsigned int)old_dos_attr,
1654 (unsigned int)existing_unx_mode,
1655 (unsigned int)new_dos_attr,
1656 (unsigned int)*returned_unx_mode ));
1658 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1659 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1660 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
1661 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
1662 return False;
1665 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1666 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
1667 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
1668 return False;
1671 return True;
1674 /****************************************************************************
1675 Special FCB or DOS processing in the case of a sharing violation.
1676 Try and find a duplicated file handle.
1677 ****************************************************************************/
1679 static NTSTATUS fcb_or_dos_open(struct smb_request *req,
1680 connection_struct *conn,
1681 files_struct *fsp_to_dup_into,
1682 const struct smb_filename *smb_fname,
1683 struct file_id id,
1684 uint16 file_pid,
1685 uint64_t vuid,
1686 uint32 access_mask,
1687 uint32 share_access,
1688 uint32 create_options)
1690 files_struct *fsp;
1692 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1693 "file %s.\n", smb_fname_str_dbg(smb_fname)));
1695 for(fsp = file_find_di_first(conn->sconn, id); fsp;
1696 fsp = file_find_di_next(fsp)) {
1698 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1699 "vuid = %llu, file_pid = %u, private_options = 0x%x "
1700 "access_mask = 0x%x\n", fsp_str_dbg(fsp),
1701 fsp->fh->fd, (unsigned long long)fsp->vuid,
1702 (unsigned int)fsp->file_pid,
1703 (unsigned int)fsp->fh->private_options,
1704 (unsigned int)fsp->access_mask ));
1706 if (fsp != fsp_to_dup_into &&
1707 fsp->fh->fd != -1 &&
1708 fsp->vuid == vuid &&
1709 fsp->file_pid == file_pid &&
1710 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
1711 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
1712 (fsp->access_mask & FILE_WRITE_DATA) &&
1713 strequal(fsp->fsp_name->base_name, smb_fname->base_name) &&
1714 strequal(fsp->fsp_name->stream_name,
1715 smb_fname->stream_name)) {
1716 DEBUG(10,("fcb_or_dos_open: file match\n"));
1717 break;
1721 if (!fsp) {
1722 return NT_STATUS_NOT_FOUND;
1725 /* quite an insane set of semantics ... */
1726 if (is_executable(smb_fname->base_name) &&
1727 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1728 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1729 return NT_STATUS_INVALID_PARAMETER;
1732 /* We need to duplicate this fsp. */
1733 return dup_file_fsp(req, fsp, access_mask, share_access,
1734 create_options, fsp_to_dup_into);
1737 static void schedule_defer_open(struct share_mode_lock *lck,
1738 struct timeval request_time,
1739 struct smb_request *req)
1741 struct deferred_open_record state;
1743 /* This is a relative time, added to the absolute
1744 request_time value to get the absolute timeout time.
1745 Note that if this is the second or greater time we enter
1746 this codepath for this particular request mid then
1747 request_time is left as the absolute time of the *first*
1748 time this request mid was processed. This is what allows
1749 the request to eventually time out. */
1751 struct timeval timeout;
1753 /* Normally the smbd we asked should respond within
1754 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1755 * the client did, give twice the timeout as a safety
1756 * measure here in case the other smbd is stuck
1757 * somewhere else. */
1759 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1761 /* Nothing actually uses state.delayed_for_oplocks
1762 but it's handy to differentiate in debug messages
1763 between a 30 second delay due to oplock break, and
1764 a 1 second delay for share mode conflicts. */
1766 state.delayed_for_oplocks = True;
1767 state.async_open = false;
1768 state.id = lck->data->id;
1770 if (!request_timed_out(request_time, timeout)) {
1771 defer_open(lck, request_time, timeout, req, &state);
1775 /****************************************************************************
1776 Reschedule an open call that went asynchronous.
1777 ****************************************************************************/
1779 static void schedule_async_open(struct timeval request_time,
1780 struct smb_request *req)
1782 struct deferred_open_record state;
1783 struct timeval timeout;
1785 timeout = timeval_set(20, 0);
1787 ZERO_STRUCT(state);
1788 state.delayed_for_oplocks = false;
1789 state.async_open = true;
1791 if (!request_timed_out(request_time, timeout)) {
1792 defer_open(NULL, request_time, timeout, req, &state);
1796 /****************************************************************************
1797 Work out what access_mask to use from what the client sent us.
1798 ****************************************************************************/
1800 static NTSTATUS smbd_calculate_maximum_allowed_access(
1801 connection_struct *conn,
1802 const struct smb_filename *smb_fname,
1803 bool use_privs,
1804 uint32_t *p_access_mask)
1806 struct security_descriptor *sd;
1807 uint32_t access_granted;
1808 NTSTATUS status;
1810 if (!use_privs && (get_current_uid(conn) == (uid_t)0)) {
1811 *p_access_mask |= FILE_GENERIC_ALL;
1812 return NT_STATUS_OK;
1815 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
1816 (SECINFO_OWNER |
1817 SECINFO_GROUP |
1818 SECINFO_DACL),
1819 talloc_tos(), &sd);
1821 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1823 * File did not exist
1825 *p_access_mask = FILE_GENERIC_ALL;
1826 return NT_STATUS_OK;
1828 if (!NT_STATUS_IS_OK(status)) {
1829 DEBUG(10,("Could not get acl on file %s: %s\n",
1830 smb_fname_str_dbg(smb_fname),
1831 nt_errstr(status)));
1832 return NT_STATUS_ACCESS_DENIED;
1836 * If we can access the path to this file, by
1837 * default we have FILE_READ_ATTRIBUTES from the
1838 * containing directory. See the section:
1839 * "Algorithm to Check Access to an Existing File"
1840 * in MS-FSA.pdf.
1842 * se_file_access_check()
1843 * also takes care of owner WRITE_DAC and READ_CONTROL.
1845 status = se_file_access_check(sd,
1846 get_current_nttok(conn),
1847 use_privs,
1848 (*p_access_mask & ~FILE_READ_ATTRIBUTES),
1849 &access_granted);
1851 TALLOC_FREE(sd);
1853 if (!NT_STATUS_IS_OK(status)) {
1854 DEBUG(10, ("Access denied on file %s: "
1855 "when calculating maximum access\n",
1856 smb_fname_str_dbg(smb_fname)));
1857 return NT_STATUS_ACCESS_DENIED;
1859 *p_access_mask = (access_granted | FILE_READ_ATTRIBUTES);
1861 if (!(access_granted & DELETE_ACCESS)) {
1862 if (can_delete_file_in_directory(conn, smb_fname)) {
1863 *p_access_mask |= DELETE_ACCESS;
1867 return NT_STATUS_OK;
1870 NTSTATUS smbd_calculate_access_mask(connection_struct *conn,
1871 const struct smb_filename *smb_fname,
1872 bool use_privs,
1873 uint32_t access_mask,
1874 uint32_t *access_mask_out)
1876 NTSTATUS status;
1877 uint32_t orig_access_mask = access_mask;
1878 uint32_t rejected_share_access;
1881 * Convert GENERIC bits to specific bits.
1884 se_map_generic(&access_mask, &file_generic_mapping);
1886 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1887 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1889 status = smbd_calculate_maximum_allowed_access(
1890 conn, smb_fname, use_privs, &access_mask);
1892 if (!NT_STATUS_IS_OK(status)) {
1893 return status;
1896 access_mask &= conn->share_access;
1899 rejected_share_access = access_mask & ~(conn->share_access);
1901 if (rejected_share_access) {
1902 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
1903 "file %s: rejected by share access mask[0x%08X] "
1904 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
1905 smb_fname_str_dbg(smb_fname),
1906 conn->share_access,
1907 orig_access_mask, access_mask,
1908 rejected_share_access));
1909 return NT_STATUS_ACCESS_DENIED;
1912 *access_mask_out = access_mask;
1913 return NT_STATUS_OK;
1916 /****************************************************************************
1917 Remove the deferred open entry under lock.
1918 ****************************************************************************/
1920 /****************************************************************************
1921 Return true if this is a state pointer to an asynchronous create.
1922 ****************************************************************************/
1924 bool is_deferred_open_async(const void *ptr)
1926 const struct deferred_open_record *state = (const struct deferred_open_record *)ptr;
1928 return state->async_open;
1931 static bool clear_ads(uint32_t create_disposition)
1933 bool ret = false;
1935 switch (create_disposition) {
1936 case FILE_SUPERSEDE:
1937 case FILE_OVERWRITE_IF:
1938 case FILE_OVERWRITE:
1939 ret = true;
1940 break;
1941 default:
1942 break;
1944 return ret;
1947 static int disposition_to_open_flags(uint32_t create_disposition)
1949 int ret = 0;
1952 * Currently we're using FILE_SUPERSEDE as the same as
1953 * FILE_OVERWRITE_IF but they really are
1954 * different. FILE_SUPERSEDE deletes an existing file
1955 * (requiring delete access) then recreates it.
1958 switch (create_disposition) {
1959 case FILE_SUPERSEDE:
1960 case FILE_OVERWRITE_IF:
1962 * If file exists replace/overwrite. If file doesn't
1963 * exist create.
1965 ret = O_CREAT|O_TRUNC;
1966 break;
1968 case FILE_OPEN:
1970 * If file exists open. If file doesn't exist error.
1972 ret = 0;
1973 break;
1975 case FILE_OVERWRITE:
1977 * If file exists overwrite. If file doesn't exist
1978 * error.
1980 ret = O_TRUNC;
1981 break;
1983 case FILE_CREATE:
1985 * If file exists error. If file doesn't exist create.
1987 ret = O_CREAT|O_EXCL;
1988 break;
1990 case FILE_OPEN_IF:
1992 * If file exists open. If file doesn't exist create.
1994 ret = O_CREAT;
1995 break;
1997 return ret;
2000 static int calculate_open_access_flags(uint32_t access_mask,
2001 int oplock_request,
2002 uint32_t private_flags)
2004 bool need_write, need_read;
2007 * Note that we ignore the append flag as append does not
2008 * mean the same thing under DOS and Unix.
2011 need_write =
2012 ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
2013 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE));
2015 if (!need_write) {
2016 return O_RDONLY;
2019 /* DENY_DOS opens are always underlying read-write on the
2020 file handle, no matter what the requested access mask
2021 says. */
2023 need_read =
2024 ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
2025 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|
2026 FILE_READ_EA|FILE_EXECUTE));
2028 if (!need_read) {
2029 return O_WRONLY;
2031 return O_RDWR;
2034 /****************************************************************************
2035 Open a file with a share mode. Passed in an already created files_struct *.
2036 ****************************************************************************/
2038 static NTSTATUS open_file_ntcreate(connection_struct *conn,
2039 struct smb_request *req,
2040 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
2041 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
2042 uint32 create_disposition, /* FILE_OPEN_IF etc. */
2043 uint32 create_options, /* options such as delete on close. */
2044 uint32 new_dos_attributes, /* attributes used for new file. */
2045 int oplock_request, /* internal Samba oplock codes. */
2046 /* Information (FILE_EXISTS etc.) */
2047 uint32_t private_flags, /* Samba specific flags. */
2048 int *pinfo,
2049 files_struct *fsp)
2051 struct smb_filename *smb_fname = fsp->fsp_name;
2052 int flags=0;
2053 int flags2=0;
2054 bool file_existed = VALID_STAT(smb_fname->st);
2055 bool def_acl = False;
2056 bool posix_open = False;
2057 bool new_file_created = False;
2058 bool first_open_attempt = true;
2059 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
2060 mode_t new_unx_mode = (mode_t)0;
2061 mode_t unx_mode = (mode_t)0;
2062 int info;
2063 uint32 existing_dos_attributes = 0;
2064 struct timeval request_time = timeval_zero();
2065 struct share_mode_lock *lck = NULL;
2066 uint32 open_access_mask = access_mask;
2067 NTSTATUS status;
2068 char *parent_dir;
2069 SMB_STRUCT_STAT saved_stat = smb_fname->st;
2070 struct share_mode_entry *batch_entry = NULL;
2071 struct share_mode_entry *exclusive_entry = NULL;
2072 bool got_level2_oplock = false;
2073 bool got_a_none_oplock = false;
2074 struct timespec old_write_time;
2075 struct file_id id;
2077 if (conn->printer) {
2079 * Printers are handled completely differently.
2080 * Most of the passed parameters are ignored.
2083 if (pinfo) {
2084 *pinfo = FILE_WAS_CREATED;
2087 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
2088 smb_fname_str_dbg(smb_fname)));
2090 if (!req) {
2091 DEBUG(0,("open_file_ntcreate: printer open without "
2092 "an SMB request!\n"));
2093 return NT_STATUS_INTERNAL_ERROR;
2096 return print_spool_open(fsp, smb_fname->base_name,
2097 req->vuid);
2100 if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
2101 NULL)) {
2102 return NT_STATUS_NO_MEMORY;
2105 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2106 posix_open = True;
2107 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2108 new_dos_attributes = 0;
2109 } else {
2110 /* Windows allows a new file to be created and
2111 silently removes a FILE_ATTRIBUTE_DIRECTORY
2112 sent by the client. Do the same. */
2114 new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
2116 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
2117 * created new. */
2118 unx_mode = unix_mode(conn, new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2119 smb_fname, parent_dir);
2122 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
2123 "access_mask=0x%x share_access=0x%x "
2124 "create_disposition = 0x%x create_options=0x%x "
2125 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
2126 smb_fname_str_dbg(smb_fname), new_dos_attributes,
2127 access_mask, share_access, create_disposition,
2128 create_options, (unsigned int)unx_mode, oplock_request,
2129 (unsigned int)private_flags));
2131 if (req == NULL) {
2132 /* Ensure req == NULL means INTERNAL_OPEN_ONLY */
2133 SMB_ASSERT(((oplock_request & INTERNAL_OPEN_ONLY) != 0));
2134 } else {
2135 /* And req != NULL means no INTERNAL_OPEN_ONLY */
2136 SMB_ASSERT(((oplock_request & INTERNAL_OPEN_ONLY) == 0));
2140 * Only non-internal opens can be deferred at all
2143 if (req) {
2144 void *ptr;
2145 if (get_deferred_open_message_state(req,
2146 &request_time,
2147 &ptr)) {
2148 /* Remember the absolute time of the original
2149 request with this mid. We'll use it later to
2150 see if this has timed out. */
2152 /* If it was an async create retry, the file
2153 didn't exist. */
2155 if (is_deferred_open_async(ptr)) {
2156 SET_STAT_INVALID(smb_fname->st);
2157 file_existed = false;
2160 /* Ensure we don't reprocess this message. */
2161 remove_deferred_open_message_smb(req->sconn, req->mid);
2163 first_open_attempt = false;
2167 if (!posix_open) {
2168 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
2169 if (file_existed) {
2170 existing_dos_attributes = dos_mode(conn, smb_fname);
2174 /* ignore any oplock requests if oplocks are disabled */
2175 if (!lp_oplocks(SNUM(conn)) ||
2176 IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
2177 /* Mask off everything except the private Samba bits. */
2178 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
2181 /* this is for OS/2 long file names - say we don't support them */
2182 if (!lp_posix_pathnames() && strstr(smb_fname->base_name,".+,;=[].")) {
2183 /* OS/2 Workplace shell fix may be main code stream in a later
2184 * release. */
2185 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
2186 "supported.\n"));
2187 if (use_nt_status()) {
2188 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2190 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
2193 switch( create_disposition ) {
2194 case FILE_OPEN:
2195 /* If file exists open. If file doesn't exist error. */
2196 if (!file_existed) {
2197 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
2198 "requested for file %s and file "
2199 "doesn't exist.\n",
2200 smb_fname_str_dbg(smb_fname)));
2201 errno = ENOENT;
2202 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2204 break;
2206 case FILE_OVERWRITE:
2207 /* If file exists overwrite. If file doesn't exist
2208 * error. */
2209 if (!file_existed) {
2210 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
2211 "requested for file %s and file "
2212 "doesn't exist.\n",
2213 smb_fname_str_dbg(smb_fname) ));
2214 errno = ENOENT;
2215 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2217 break;
2219 case FILE_CREATE:
2220 /* If file exists error. If file doesn't exist
2221 * create. */
2222 if (file_existed) {
2223 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
2224 "requested for file %s and file "
2225 "already exists.\n",
2226 smb_fname_str_dbg(smb_fname)));
2227 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
2228 errno = EISDIR;
2229 } else {
2230 errno = EEXIST;
2232 return map_nt_error_from_unix(errno);
2234 break;
2236 case FILE_SUPERSEDE:
2237 case FILE_OVERWRITE_IF:
2238 case FILE_OPEN_IF:
2239 break;
2240 default:
2241 return NT_STATUS_INVALID_PARAMETER;
2244 flags2 = disposition_to_open_flags(create_disposition);
2246 /* We only care about matching attributes on file exists and
2247 * overwrite. */
2249 if (!posix_open && file_existed &&
2250 ((create_disposition == FILE_OVERWRITE) ||
2251 (create_disposition == FILE_OVERWRITE_IF))) {
2252 if (!open_match_attributes(conn, existing_dos_attributes,
2253 new_dos_attributes,
2254 smb_fname->st.st_ex_mode,
2255 unx_mode, &new_unx_mode)) {
2256 DEBUG(5,("open_file_ntcreate: attributes missmatch "
2257 "for file %s (%x %x) (0%o, 0%o)\n",
2258 smb_fname_str_dbg(smb_fname),
2259 existing_dos_attributes,
2260 new_dos_attributes,
2261 (unsigned int)smb_fname->st.st_ex_mode,
2262 (unsigned int)unx_mode ));
2263 errno = EACCES;
2264 return NT_STATUS_ACCESS_DENIED;
2268 status = smbd_calculate_access_mask(conn, smb_fname,
2269 false,
2270 access_mask,
2271 &access_mask);
2272 if (!NT_STATUS_IS_OK(status)) {
2273 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
2274 "on file %s returned %s\n",
2275 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
2276 return status;
2279 open_access_mask = access_mask;
2281 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
2282 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
2285 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
2286 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
2287 access_mask));
2290 * Note that we ignore the append flag as append does not
2291 * mean the same thing under DOS and Unix.
2294 flags = calculate_open_access_flags(access_mask, oplock_request,
2295 private_flags);
2298 * Currently we only look at FILE_WRITE_THROUGH for create options.
2301 #if defined(O_SYNC)
2302 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
2303 flags2 |= O_SYNC;
2305 #endif /* O_SYNC */
2307 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
2308 flags2 |= O_APPEND;
2311 if (!posix_open && !CAN_WRITE(conn)) {
2313 * We should really return a permission denied error if either
2314 * O_CREAT or O_TRUNC are set, but for compatibility with
2315 * older versions of Samba we just AND them out.
2317 flags2 &= ~(O_CREAT|O_TRUNC);
2320 if (first_open_attempt && lp_kernel_oplocks(SNUM(conn))) {
2322 * With kernel oplocks the open breaking an oplock
2323 * blocks until the oplock holder has given up the
2324 * oplock or closed the file. We prevent this by first
2325 * trying to open the file with O_NONBLOCK (see "man
2326 * fcntl" on Linux). For the second try, triggered by
2327 * an oplock break response, we do not need this
2328 * anymore.
2330 * This is true under the assumption that only Samba
2331 * requests kernel oplocks. Once someone else like
2332 * NFSv4 starts to use that API, we will have to
2333 * modify this by communicating with the NFSv4 server.
2335 flags2 |= O_NONBLOCK;
2339 * Ensure we can't write on a read-only share or file.
2342 if (flags != O_RDONLY && file_existed &&
2343 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
2344 DEBUG(5,("open_file_ntcreate: write access requested for "
2345 "file %s on read only %s\n",
2346 smb_fname_str_dbg(smb_fname),
2347 !CAN_WRITE(conn) ? "share" : "file" ));
2348 errno = EACCES;
2349 return NT_STATUS_ACCESS_DENIED;
2352 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
2353 fsp->share_access = share_access;
2354 fsp->fh->private_options = private_flags;
2355 fsp->access_mask = open_access_mask; /* We change this to the
2356 * requested access_mask after
2357 * the open is done. */
2358 fsp->posix_open = posix_open;
2360 /* Ensure no SAMBA_PRIVATE bits can be set. */
2361 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
2363 if (timeval_is_zero(&request_time)) {
2364 request_time = fsp->open_time;
2368 * Ensure we pay attention to default ACLs on directories if required.
2371 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
2372 (def_acl = directory_has_default_acl(conn, parent_dir))) {
2373 unx_mode = (0777 & lp_create_mask(SNUM(conn)));
2376 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2377 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2378 (unsigned int)flags, (unsigned int)flags2,
2379 (unsigned int)unx_mode, (unsigned int)access_mask,
2380 (unsigned int)open_access_mask));
2382 fsp_open = open_file(fsp, conn, req, parent_dir,
2383 flags|flags2, unx_mode, access_mask,
2384 open_access_mask, &new_file_created);
2386 if (NT_STATUS_EQUAL(fsp_open, NT_STATUS_NETWORK_BUSY)) {
2387 struct deferred_open_record state;
2390 * EWOULDBLOCK/EAGAIN maps to NETWORK_BUSY.
2392 if (file_existed && S_ISFIFO(fsp->fsp_name->st.st_ex_mode)) {
2393 DEBUG(10, ("FIFO busy\n"));
2394 return NT_STATUS_NETWORK_BUSY;
2396 if (req == NULL) {
2397 DEBUG(10, ("Internal open busy\n"));
2398 return NT_STATUS_NETWORK_BUSY;
2402 * From here on we assume this is an oplock break triggered
2405 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
2406 if (lck == NULL) {
2407 state.delayed_for_oplocks = false;
2408 state.async_open = false;
2409 state.id = fsp->file_id;
2410 defer_open(NULL, request_time, timeval_set(0, 0),
2411 req, &state);
2412 DEBUG(10, ("No share mode lock found after "
2413 "EWOULDBLOCK, retrying sync\n"));
2414 return NT_STATUS_SHARING_VIOLATION;
2417 find_oplock_types(fsp, 0, lck, &batch_entry, &exclusive_entry,
2418 &got_level2_oplock, &got_a_none_oplock);
2420 if (delay_for_batch_oplocks(fsp, req->mid, 0, batch_entry) ||
2421 delay_for_exclusive_oplocks(fsp, req->mid, 0,
2422 exclusive_entry)) {
2423 schedule_defer_open(lck, request_time, req);
2424 TALLOC_FREE(lck);
2425 DEBUG(10, ("Sent oplock break request to kernel "
2426 "oplock holder\n"));
2427 return NT_STATUS_SHARING_VIOLATION;
2431 * No oplock from Samba around. Immediately retry with
2432 * a blocking open.
2434 state.delayed_for_oplocks = false;
2435 state.async_open = false;
2436 state.id = lck->data->id;
2437 defer_open(lck, request_time, timeval_set(0, 0), req, &state);
2438 TALLOC_FREE(lck);
2439 DEBUG(10, ("No Samba oplock around after EWOULDBLOCK. "
2440 "Retrying sync\n"));
2441 return NT_STATUS_SHARING_VIOLATION;
2444 if (!NT_STATUS_IS_OK(fsp_open)) {
2445 if (NT_STATUS_EQUAL(fsp_open, NT_STATUS_RETRY)) {
2446 schedule_async_open(request_time, req);
2448 TALLOC_FREE(lck);
2449 return fsp_open;
2452 if (new_file_created) {
2454 * As we atomically create using O_CREAT|O_EXCL,
2455 * then if new_file_created is true, then
2456 * file_existed *MUST* have been false (even
2457 * if the file was previously detected as being
2458 * there).
2460 file_existed = false;
2463 if (file_existed && !check_same_dev_ino(&saved_stat, &smb_fname->st)) {
2465 * The file did exist, but some other (local or NFS)
2466 * process either renamed/unlinked and re-created the
2467 * file with different dev/ino after we walked the path,
2468 * but before we did the open. We could retry the
2469 * open but it's a rare enough case it's easier to
2470 * just fail the open to prevent creating any problems
2471 * in the open file db having the wrong dev/ino key.
2473 TALLOC_FREE(lck);
2474 fd_close(fsp);
2475 DEBUG(1,("open_file_ntcreate: file %s - dev/ino mismatch. "
2476 "Old (dev=0x%llu, ino =0x%llu). "
2477 "New (dev=0x%llu, ino=0x%llu). Failing open "
2478 " with NT_STATUS_ACCESS_DENIED.\n",
2479 smb_fname_str_dbg(smb_fname),
2480 (unsigned long long)saved_stat.st_ex_dev,
2481 (unsigned long long)saved_stat.st_ex_ino,
2482 (unsigned long long)smb_fname->st.st_ex_dev,
2483 (unsigned long long)smb_fname->st.st_ex_ino));
2484 return NT_STATUS_ACCESS_DENIED;
2487 old_write_time = smb_fname->st.st_ex_mtime;
2490 * Deal with the race condition where two smbd's detect the
2491 * file doesn't exist and do the create at the same time. One
2492 * of them will win and set a share mode, the other (ie. this
2493 * one) should check if the requested share mode for this
2494 * create is allowed.
2498 * Now the file exists and fsp is successfully opened,
2499 * fsp->dev and fsp->inode are valid and should replace the
2500 * dev=0,inode=0 from a non existent file. Spotted by
2501 * Nadav Danieli <nadavd@exanet.com>. JRA.
2504 id = fsp->file_id;
2506 lck = get_share_mode_lock(talloc_tos(), id,
2507 conn->connectpath,
2508 smb_fname, &old_write_time);
2510 if (lck == NULL) {
2511 DEBUG(0, ("open_file_ntcreate: Could not get share "
2512 "mode lock for %s\n",
2513 smb_fname_str_dbg(smb_fname)));
2514 fd_close(fsp);
2515 return NT_STATUS_SHARING_VIOLATION;
2518 /* Get the types we need to examine. */
2519 find_oplock_types(fsp,
2520 oplock_request,
2521 lck,
2522 &batch_entry,
2523 &exclusive_entry,
2524 &got_level2_oplock,
2525 &got_a_none_oplock);
2527 /* First pass - send break only on batch oplocks. */
2528 if ((req != NULL) &&
2529 delay_for_batch_oplocks(fsp,
2530 req->mid,
2531 oplock_request,
2532 batch_entry)) {
2533 schedule_defer_open(lck, request_time, req);
2534 TALLOC_FREE(lck);
2535 fd_close(fsp);
2536 return NT_STATUS_SHARING_VIOLATION;
2539 status = open_mode_check(conn, lck, fsp->name_hash,
2540 access_mask, share_access,
2541 create_options, &file_existed);
2543 if (NT_STATUS_IS_OK(status)) {
2544 /* We might be going to allow this open. Check oplock
2545 * status again. */
2546 /* Second pass - send break for both batch or
2547 * exclusive oplocks. */
2548 if ((req != NULL) &&
2549 delay_for_exclusive_oplocks(
2550 fsp,
2551 req->mid,
2552 oplock_request,
2553 exclusive_entry)) {
2554 schedule_defer_open(lck, request_time, req);
2555 TALLOC_FREE(lck);
2556 fd_close(fsp);
2557 return NT_STATUS_SHARING_VIOLATION;
2561 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
2562 /* DELETE_PENDING is not deferred for a second */
2563 TALLOC_FREE(lck);
2564 fd_close(fsp);
2565 return status;
2568 if (!NT_STATUS_IS_OK(status)) {
2569 uint32 can_access_mask;
2570 bool can_access = True;
2572 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
2574 /* Check if this can be done with the deny_dos and fcb
2575 * calls. */
2576 if (private_flags &
2577 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
2578 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
2579 if (req == NULL) {
2580 DEBUG(0, ("DOS open without an SMB "
2581 "request!\n"));
2582 TALLOC_FREE(lck);
2583 fd_close(fsp);
2584 return NT_STATUS_INTERNAL_ERROR;
2587 /* Use the client requested access mask here,
2588 * not the one we open with. */
2589 status = fcb_or_dos_open(req,
2590 conn,
2591 fsp,
2592 smb_fname,
2594 req->smbpid,
2595 req->vuid,
2596 access_mask,
2597 share_access,
2598 create_options);
2600 if (NT_STATUS_IS_OK(status)) {
2601 TALLOC_FREE(lck);
2602 if (pinfo) {
2603 *pinfo = FILE_WAS_OPENED;
2605 return NT_STATUS_OK;
2610 * This next line is a subtlety we need for
2611 * MS-Access. If a file open will fail due to share
2612 * permissions and also for security (access) reasons,
2613 * we need to return the access failed error, not the
2614 * share error. We can't open the file due to kernel
2615 * oplock deadlock (it's possible we failed above on
2616 * the open_mode_check()) so use a userspace check.
2619 if (flags & O_RDWR) {
2620 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
2621 } else if (flags & O_WRONLY) {
2622 can_access_mask = FILE_WRITE_DATA;
2623 } else {
2624 can_access_mask = FILE_READ_DATA;
2627 if (((can_access_mask & FILE_WRITE_DATA) &&
2628 !CAN_WRITE(conn)) ||
2629 !NT_STATUS_IS_OK(smbd_check_access_rights(conn,
2630 smb_fname,
2631 false,
2632 can_access_mask))) {
2633 can_access = False;
2637 * If we're returning a share violation, ensure we
2638 * cope with the braindead 1 second delay (SMB1 only).
2641 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
2642 !conn->sconn->using_smb2 &&
2643 lp_defer_sharing_violations()) {
2644 struct timeval timeout;
2645 struct deferred_open_record state;
2646 int timeout_usecs;
2648 /* this is a hack to speed up torture tests
2649 in 'make test' */
2650 timeout_usecs = lp_parm_int(SNUM(conn),
2651 "smbd","sharedelay",
2652 SHARING_VIOLATION_USEC_WAIT);
2654 /* This is a relative time, added to the absolute
2655 request_time value to get the absolute timeout time.
2656 Note that if this is the second or greater time we enter
2657 this codepath for this particular request mid then
2658 request_time is left as the absolute time of the *first*
2659 time this request mid was processed. This is what allows
2660 the request to eventually time out. */
2662 timeout = timeval_set(0, timeout_usecs);
2664 /* Nothing actually uses state.delayed_for_oplocks
2665 but it's handy to differentiate in debug messages
2666 between a 30 second delay due to oplock break, and
2667 a 1 second delay for share mode conflicts. */
2669 state.delayed_for_oplocks = False;
2670 state.async_open = false;
2671 state.id = id;
2673 if ((req != NULL)
2674 && !request_timed_out(request_time,
2675 timeout)) {
2676 defer_open(lck, request_time, timeout,
2677 req, &state);
2681 TALLOC_FREE(lck);
2682 fd_close(fsp);
2683 if (can_access) {
2685 * We have detected a sharing violation here
2686 * so return the correct error code
2688 status = NT_STATUS_SHARING_VIOLATION;
2689 } else {
2690 status = NT_STATUS_ACCESS_DENIED;
2692 return status;
2695 /* Should we atomically (to the client at least) truncate ? */
2696 if ((!new_file_created) &&
2697 (flags2 & O_TRUNC) &&
2698 (!S_ISFIFO(fsp->fsp_name->st.st_ex_mode))) {
2699 int ret;
2701 ret = vfs_set_filelen(fsp, 0);
2702 if (ret != 0) {
2703 status = map_nt_error_from_unix(errno);
2704 TALLOC_FREE(lck);
2705 fd_close(fsp);
2706 return status;
2710 grant_fsp_oplock_type(fsp,
2711 oplock_request,
2712 got_level2_oplock,
2713 got_a_none_oplock);
2716 * We have the share entry *locked*.....
2719 /* Delete streams if create_disposition requires it */
2720 if (!new_file_created && clear_ads(create_disposition) &&
2721 !is_ntfs_stream_smb_fname(smb_fname)) {
2722 status = delete_all_streams(conn, smb_fname->base_name);
2723 if (!NT_STATUS_IS_OK(status)) {
2724 TALLOC_FREE(lck);
2725 fd_close(fsp);
2726 return status;
2730 /* note that we ignore failure for the following. It is
2731 basically a hack for NFS, and NFS will never set one of
2732 these only read them. Nobody but Samba can ever set a deny
2733 mode and we have already checked our more authoritative
2734 locking database for permission to set this deny mode. If
2735 the kernel refuses the operations then the kernel is wrong.
2736 note that GPFS supports it as well - jmcd */
2738 if (fsp->fh->fd != -1 && lp_kernel_share_modes(SNUM(conn))) {
2739 int ret_flock;
2740 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
2741 if(ret_flock == -1 ){
2743 TALLOC_FREE(lck);
2744 fd_close(fsp);
2746 return NT_STATUS_SHARING_VIOLATION;
2751 * At this point onwards, we can guarantee that the share entry
2752 * is locked, whether we created the file or not, and that the
2753 * deny mode is compatible with all current opens.
2757 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2758 * but we don't have to store this - just ignore it on access check.
2760 if (conn->sconn->using_smb2) {
2762 * SMB2 doesn't return it (according to Microsoft tests).
2763 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2764 * File created with access = 0x7 (Read, Write, Delete)
2765 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2767 fsp->access_mask = access_mask;
2768 } else {
2769 /* But SMB1 does. */
2770 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2773 if (file_existed) {
2774 /* stat opens on existing files don't get oplocks. */
2775 if (is_stat_open(open_access_mask)) {
2776 fsp->oplock_type = NO_OPLOCK;
2780 if (new_file_created) {
2781 info = FILE_WAS_CREATED;
2782 } else {
2783 if (flags2 & O_TRUNC) {
2784 info = FILE_WAS_OVERWRITTEN;
2785 } else {
2786 info = FILE_WAS_OPENED;
2790 if (pinfo) {
2791 *pinfo = info;
2795 * Setup the oplock info in both the shared memory and
2796 * file structs.
2799 status = set_file_oplock(fsp, fsp->oplock_type);
2800 if (!NT_STATUS_IS_OK(status)) {
2802 * Could not get the kernel oplock or there are byte-range
2803 * locks on the file.
2805 fsp->oplock_type = NO_OPLOCK;
2808 set_share_mode(lck, fsp, get_current_uid(conn),
2809 req ? req->mid : 0,
2810 fsp->oplock_type);
2812 /* Handle strange delete on close create semantics. */
2813 if (create_options & FILE_DELETE_ON_CLOSE) {
2815 status = can_set_delete_on_close(fsp, new_dos_attributes);
2817 if (!NT_STATUS_IS_OK(status)) {
2818 /* Remember to delete the mode we just added. */
2819 del_share_mode(lck, fsp);
2820 TALLOC_FREE(lck);
2821 fd_close(fsp);
2822 return status;
2824 /* Note that here we set the *inital* delete on close flag,
2825 not the regular one. The magic gets handled in close. */
2826 fsp->initial_delete_on_close = True;
2829 if (info != FILE_WAS_OPENED) {
2830 /* Files should be initially set as archive */
2831 if (lp_map_archive(SNUM(conn)) ||
2832 lp_store_dos_attributes(SNUM(conn))) {
2833 if (!posix_open) {
2834 if (file_set_dosmode(conn, smb_fname,
2835 new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2836 parent_dir, true) == 0) {
2837 unx_mode = smb_fname->st.st_ex_mode;
2843 /* Determine sparse flag. */
2844 if (posix_open) {
2845 /* POSIX opens are sparse by default. */
2846 fsp->is_sparse = true;
2847 } else {
2848 fsp->is_sparse = (file_existed &&
2849 (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE));
2853 * Take care of inherited ACLs on created files - if default ACL not
2854 * selected.
2857 if (!posix_open && new_file_created && !def_acl) {
2859 int saved_errno = errno; /* We might get ENOSYS in the next
2860 * call.. */
2862 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2863 errno == ENOSYS) {
2864 errno = saved_errno; /* Ignore ENOSYS */
2867 } else if (new_unx_mode) {
2869 int ret = -1;
2871 /* Attributes need changing. File already existed. */
2874 int saved_errno = errno; /* We might get ENOSYS in the
2875 * next call.. */
2876 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2878 if (ret == -1 && errno == ENOSYS) {
2879 errno = saved_errno; /* Ignore ENOSYS */
2880 } else {
2881 DEBUG(5, ("open_file_ntcreate: reset "
2882 "attributes of file %s to 0%o\n",
2883 smb_fname_str_dbg(smb_fname),
2884 (unsigned int)new_unx_mode));
2885 ret = 0; /* Don't do the fchmod below. */
2889 if ((ret == -1) &&
2890 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2891 DEBUG(5, ("open_file_ntcreate: failed to reset "
2892 "attributes of file %s to 0%o\n",
2893 smb_fname_str_dbg(smb_fname),
2894 (unsigned int)new_unx_mode));
2897 TALLOC_FREE(lck);
2899 return NT_STATUS_OK;
2902 static NTSTATUS mkdir_internal(connection_struct *conn,
2903 struct smb_filename *smb_dname,
2904 uint32 file_attributes)
2906 mode_t mode;
2907 char *parent_dir = NULL;
2908 NTSTATUS status;
2909 bool posix_open = false;
2910 bool need_re_stat = false;
2911 uint32_t access_mask = SEC_DIR_ADD_SUBDIR;
2913 if (!CAN_WRITE(conn) || (access_mask & ~(conn->share_access))) {
2914 DEBUG(5,("mkdir_internal: failing share access "
2915 "%s\n", lp_servicename(talloc_tos(), SNUM(conn))));
2916 return NT_STATUS_ACCESS_DENIED;
2919 if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
2920 NULL)) {
2921 return NT_STATUS_NO_MEMORY;
2924 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2925 posix_open = true;
2926 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2927 } else {
2928 mode = unix_mode(conn, FILE_ATTRIBUTE_DIRECTORY, smb_dname, parent_dir);
2931 status = check_parent_access(conn,
2932 smb_dname,
2933 access_mask);
2934 if(!NT_STATUS_IS_OK(status)) {
2935 DEBUG(5,("mkdir_internal: check_parent_access "
2936 "on directory %s for path %s returned %s\n",
2937 parent_dir,
2938 smb_dname->base_name,
2939 nt_errstr(status) ));
2940 return status;
2943 if (SMB_VFS_MKDIR(conn, smb_dname->base_name, mode) != 0) {
2944 return map_nt_error_from_unix(errno);
2947 /* Ensure we're checking for a symlink here.... */
2948 /* We don't want to get caught by a symlink racer. */
2950 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2951 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2952 smb_fname_str_dbg(smb_dname), strerror(errno)));
2953 return map_nt_error_from_unix(errno);
2956 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
2957 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
2958 smb_fname_str_dbg(smb_dname)));
2959 return NT_STATUS_NOT_A_DIRECTORY;
2962 if (lp_store_dos_attributes(SNUM(conn))) {
2963 if (!posix_open) {
2964 file_set_dosmode(conn, smb_dname,
2965 file_attributes | FILE_ATTRIBUTE_DIRECTORY,
2966 parent_dir, true);
2970 if (lp_inherit_perms(SNUM(conn))) {
2971 inherit_access_posix_acl(conn, parent_dir,
2972 smb_dname->base_name, mode);
2973 need_re_stat = true;
2976 if (!posix_open) {
2978 * Check if high bits should have been set,
2979 * then (if bits are missing): add them.
2980 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2981 * dir.
2983 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
2984 (mode & ~smb_dname->st.st_ex_mode)) {
2985 SMB_VFS_CHMOD(conn, smb_dname->base_name,
2986 (smb_dname->st.st_ex_mode |
2987 (mode & ~smb_dname->st.st_ex_mode)));
2988 need_re_stat = true;
2992 /* Change the owner if required. */
2993 if (lp_inherit_owner(SNUM(conn))) {
2994 change_dir_owner_to_parent(conn, parent_dir,
2995 smb_dname->base_name,
2996 &smb_dname->st);
2997 need_re_stat = true;
3000 if (need_re_stat) {
3001 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
3002 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
3003 smb_fname_str_dbg(smb_dname), strerror(errno)));
3004 return map_nt_error_from_unix(errno);
3008 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
3009 smb_dname->base_name);
3011 return NT_STATUS_OK;
3014 /****************************************************************************
3015 Open a directory from an NT SMB call.
3016 ****************************************************************************/
3018 static NTSTATUS open_directory(connection_struct *conn,
3019 struct smb_request *req,
3020 struct smb_filename *smb_dname,
3021 uint32 access_mask,
3022 uint32 share_access,
3023 uint32 create_disposition,
3024 uint32 create_options,
3025 uint32 file_attributes,
3026 int *pinfo,
3027 files_struct **result)
3029 files_struct *fsp = NULL;
3030 bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
3031 struct share_mode_lock *lck = NULL;
3032 NTSTATUS status;
3033 struct timespec mtimespec;
3034 int info = 0;
3036 if (is_ntfs_stream_smb_fname(smb_dname)) {
3037 DEBUG(2, ("open_directory: %s is a stream name!\n",
3038 smb_fname_str_dbg(smb_dname)));
3039 return NT_STATUS_NOT_A_DIRECTORY;
3042 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
3043 /* Ensure we have a directory attribute. */
3044 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
3047 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
3048 "share_access = 0x%x create_options = 0x%x, "
3049 "create_disposition = 0x%x, file_attributes = 0x%x\n",
3050 smb_fname_str_dbg(smb_dname),
3051 (unsigned int)access_mask,
3052 (unsigned int)share_access,
3053 (unsigned int)create_options,
3054 (unsigned int)create_disposition,
3055 (unsigned int)file_attributes));
3057 status = smbd_calculate_access_mask(conn, smb_dname, false,
3058 access_mask, &access_mask);
3059 if (!NT_STATUS_IS_OK(status)) {
3060 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
3061 "on file %s returned %s\n",
3062 smb_fname_str_dbg(smb_dname),
3063 nt_errstr(status)));
3064 return status;
3067 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3068 !security_token_has_privilege(get_current_nttok(conn),
3069 SEC_PRIV_SECURITY)) {
3070 DEBUG(10, ("open_directory: open on %s "
3071 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3072 smb_fname_str_dbg(smb_dname)));
3073 return NT_STATUS_PRIVILEGE_NOT_HELD;
3076 switch( create_disposition ) {
3077 case FILE_OPEN:
3079 if (!dir_existed) {
3080 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3083 info = FILE_WAS_OPENED;
3084 break;
3086 case FILE_CREATE:
3088 /* If directory exists error. If directory doesn't
3089 * exist create. */
3091 if (dir_existed) {
3092 status = NT_STATUS_OBJECT_NAME_COLLISION;
3093 DEBUG(2, ("open_directory: unable to create "
3094 "%s. Error was %s\n",
3095 smb_fname_str_dbg(smb_dname),
3096 nt_errstr(status)));
3097 return status;
3100 status = mkdir_internal(conn, smb_dname,
3101 file_attributes);
3103 if (!NT_STATUS_IS_OK(status)) {
3104 DEBUG(2, ("open_directory: unable to create "
3105 "%s. Error was %s\n",
3106 smb_fname_str_dbg(smb_dname),
3107 nt_errstr(status)));
3108 return status;
3111 info = FILE_WAS_CREATED;
3112 break;
3114 case FILE_OPEN_IF:
3116 * If directory exists open. If directory doesn't
3117 * exist create.
3120 if (dir_existed) {
3121 status = NT_STATUS_OK;
3122 info = FILE_WAS_OPENED;
3123 } else {
3124 status = mkdir_internal(conn, smb_dname,
3125 file_attributes);
3127 if (NT_STATUS_IS_OK(status)) {
3128 info = FILE_WAS_CREATED;
3129 } else {
3130 /* Cope with create race. */
3131 if (!NT_STATUS_EQUAL(status,
3132 NT_STATUS_OBJECT_NAME_COLLISION)) {
3133 DEBUG(2, ("open_directory: unable to create "
3134 "%s. Error was %s\n",
3135 smb_fname_str_dbg(smb_dname),
3136 nt_errstr(status)));
3137 return status;
3139 info = FILE_WAS_OPENED;
3143 break;
3145 case FILE_SUPERSEDE:
3146 case FILE_OVERWRITE:
3147 case FILE_OVERWRITE_IF:
3148 default:
3149 DEBUG(5,("open_directory: invalid create_disposition "
3150 "0x%x for directory %s\n",
3151 (unsigned int)create_disposition,
3152 smb_fname_str_dbg(smb_dname)));
3153 return NT_STATUS_INVALID_PARAMETER;
3156 if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
3157 DEBUG(5,("open_directory: %s is not a directory !\n",
3158 smb_fname_str_dbg(smb_dname)));
3159 return NT_STATUS_NOT_A_DIRECTORY;
3162 if (info == FILE_WAS_OPENED) {
3163 status = smbd_check_access_rights(conn,
3164 smb_dname,
3165 false,
3166 access_mask);
3167 if (!NT_STATUS_IS_OK(status)) {
3168 DEBUG(10, ("open_directory: smbd_check_access_rights on "
3169 "file %s failed with %s\n",
3170 smb_fname_str_dbg(smb_dname),
3171 nt_errstr(status)));
3172 return status;
3176 status = file_new(req, conn, &fsp);
3177 if(!NT_STATUS_IS_OK(status)) {
3178 return status;
3182 * Setup the files_struct for it.
3185 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
3186 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
3187 fsp->file_pid = req ? req->smbpid : 0;
3188 fsp->can_lock = False;
3189 fsp->can_read = False;
3190 fsp->can_write = False;
3192 fsp->share_access = share_access;
3193 fsp->fh->private_options = 0;
3195 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
3197 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
3198 fsp->print_file = NULL;
3199 fsp->modified = False;
3200 fsp->oplock_type = NO_OPLOCK;
3201 fsp->sent_oplock_break = NO_BREAK_SENT;
3202 fsp->is_directory = True;
3203 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
3204 status = fsp_set_smb_fname(fsp, smb_dname);
3205 if (!NT_STATUS_IS_OK(status)) {
3206 file_free(req, fsp);
3207 return status;
3210 /* Don't store old timestamps for directory
3211 handles in the internal database. We don't
3212 update them in there if new objects
3213 are creaded in the directory. Currently
3214 we only update timestamps on file writes.
3215 See bug #9870.
3217 ZERO_STRUCT(mtimespec);
3219 #ifdef O_DIRECTORY
3220 status = fd_open(conn, fsp, O_RDONLY|O_DIRECTORY, 0);
3221 #else
3222 /* POSIX allows us to open a directory with O_RDONLY. */
3223 status = fd_open(conn, fsp, O_RDONLY, 0);
3224 #endif
3225 if (!NT_STATUS_IS_OK(status)) {
3226 DEBUG(5, ("open_directory: Could not open fd for "
3227 "%s (%s)\n",
3228 smb_fname_str_dbg(smb_dname),
3229 nt_errstr(status)));
3230 file_free(req, fsp);
3231 return status;
3234 status = vfs_stat_fsp(fsp);
3235 if (!NT_STATUS_IS_OK(status)) {
3236 fd_close(fsp);
3237 file_free(req, fsp);
3238 return status;
3241 /* Ensure there was no race condition. */
3242 if (!check_same_stat(&smb_dname->st, &fsp->fsp_name->st)) {
3243 DEBUG(5,("open_directory: stat struct differs for "
3244 "directory %s.\n",
3245 smb_fname_str_dbg(smb_dname)));
3246 fd_close(fsp);
3247 file_free(req, fsp);
3248 return NT_STATUS_ACCESS_DENIED;
3251 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
3252 conn->connectpath, smb_dname,
3253 &mtimespec);
3255 if (lck == NULL) {
3256 DEBUG(0, ("open_directory: Could not get share mode lock for "
3257 "%s\n", smb_fname_str_dbg(smb_dname)));
3258 fd_close(fsp);
3259 file_free(req, fsp);
3260 return NT_STATUS_SHARING_VIOLATION;
3263 status = open_mode_check(conn, lck, fsp->name_hash,
3264 access_mask, share_access,
3265 create_options, &dir_existed);
3267 if (!NT_STATUS_IS_OK(status)) {
3268 TALLOC_FREE(lck);
3269 fd_close(fsp);
3270 file_free(req, fsp);
3271 return status;
3274 set_share_mode(lck, fsp, get_current_uid(conn),
3275 req ? req->mid : 0, NO_OPLOCK);
3277 /* For directories the delete on close bit at open time seems
3278 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
3279 if (create_options & FILE_DELETE_ON_CLOSE) {
3280 status = can_set_delete_on_close(fsp, 0);
3281 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
3282 TALLOC_FREE(lck);
3283 fd_close(fsp);
3284 file_free(req, fsp);
3285 return status;
3288 if (NT_STATUS_IS_OK(status)) {
3289 /* Note that here we set the *inital* delete on close flag,
3290 not the regular one. The magic gets handled in close. */
3291 fsp->initial_delete_on_close = True;
3295 TALLOC_FREE(lck);
3297 if (pinfo) {
3298 *pinfo = info;
3301 *result = fsp;
3302 return NT_STATUS_OK;
3305 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
3306 struct smb_filename *smb_dname)
3308 NTSTATUS status;
3309 files_struct *fsp;
3311 status = SMB_VFS_CREATE_FILE(
3312 conn, /* conn */
3313 req, /* req */
3314 0, /* root_dir_fid */
3315 smb_dname, /* fname */
3316 FILE_READ_ATTRIBUTES, /* access_mask */
3317 FILE_SHARE_NONE, /* share_access */
3318 FILE_CREATE, /* create_disposition*/
3319 FILE_DIRECTORY_FILE, /* create_options */
3320 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
3321 0, /* oplock_request */
3322 0, /* allocation_size */
3323 0, /* private_flags */
3324 NULL, /* sd */
3325 NULL, /* ea_list */
3326 &fsp, /* result */
3327 NULL); /* pinfo */
3329 if (NT_STATUS_IS_OK(status)) {
3330 close_file(req, fsp, NORMAL_CLOSE);
3333 return status;
3336 /****************************************************************************
3337 Receive notification that one of our open files has been renamed by another
3338 smbd process.
3339 ****************************************************************************/
3341 void msg_file_was_renamed(struct messaging_context *msg,
3342 void *private_data,
3343 uint32_t msg_type,
3344 struct server_id server_id,
3345 DATA_BLOB *data)
3347 files_struct *fsp;
3348 char *frm = (char *)data->data;
3349 struct file_id id;
3350 const char *sharepath;
3351 const char *base_name;
3352 const char *stream_name;
3353 struct smb_filename *smb_fname = NULL;
3354 size_t sp_len, bn_len;
3355 NTSTATUS status;
3356 struct smbd_server_connection *sconn =
3357 talloc_get_type_abort(private_data,
3358 struct smbd_server_connection);
3360 if (data->data == NULL
3361 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
3362 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
3363 (int)data->length));
3364 return;
3367 /* Unpack the message. */
3368 pull_file_id_24(frm, &id);
3369 sharepath = &frm[24];
3370 sp_len = strlen(sharepath);
3371 base_name = sharepath + sp_len + 1;
3372 bn_len = strlen(base_name);
3373 stream_name = sharepath + sp_len + 1 + bn_len + 1;
3375 /* stream_name must always be NULL if there is no stream. */
3376 if (stream_name[0] == '\0') {
3377 stream_name = NULL;
3380 smb_fname = synthetic_smb_fname(talloc_tos(), base_name,
3381 stream_name, NULL);
3382 if (smb_fname == NULL) {
3383 return;
3386 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3387 "file_id %s\n",
3388 sharepath, smb_fname_str_dbg(smb_fname),
3389 file_id_string_tos(&id)));
3391 for(fsp = file_find_di_first(sconn, id); fsp;
3392 fsp = file_find_di_next(fsp)) {
3393 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
3395 DEBUG(10,("msg_file_was_renamed: renaming file %s from %s -> %s\n",
3396 fsp_fnum_dbg(fsp), fsp_str_dbg(fsp),
3397 smb_fname_str_dbg(smb_fname)));
3398 status = fsp_set_smb_fname(fsp, smb_fname);
3399 if (!NT_STATUS_IS_OK(status)) {
3400 goto out;
3402 } else {
3403 /* TODO. JRA. */
3404 /* Now we have the complete path we can work out if this is
3405 actually within this share and adjust newname accordingly. */
3406 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3407 "not sharepath %s) "
3408 "%s from %s -> %s\n",
3409 fsp->conn->connectpath,
3410 sharepath,
3411 fsp_fnum_dbg(fsp),
3412 fsp_str_dbg(fsp),
3413 smb_fname_str_dbg(smb_fname)));
3416 out:
3417 TALLOC_FREE(smb_fname);
3418 return;
3422 * If a main file is opened for delete, all streams need to be checked for
3423 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3424 * If that works, delete them all by setting the delete on close and close.
3427 NTSTATUS open_streams_for_delete(connection_struct *conn,
3428 const char *fname)
3430 struct stream_struct *stream_info = NULL;
3431 files_struct **streams = NULL;
3432 int i;
3433 unsigned int num_streams = 0;
3434 TALLOC_CTX *frame = talloc_stackframe();
3435 NTSTATUS status;
3437 status = vfs_streaminfo(conn, NULL, fname, talloc_tos(),
3438 &num_streams, &stream_info);
3440 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
3441 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
3442 DEBUG(10, ("no streams around\n"));
3443 TALLOC_FREE(frame);
3444 return NT_STATUS_OK;
3447 if (!NT_STATUS_IS_OK(status)) {
3448 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3449 nt_errstr(status)));
3450 goto fail;
3453 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3454 num_streams));
3456 if (num_streams == 0) {
3457 TALLOC_FREE(frame);
3458 return NT_STATUS_OK;
3461 streams = talloc_array(talloc_tos(), files_struct *, num_streams);
3462 if (streams == NULL) {
3463 DEBUG(0, ("talloc failed\n"));
3464 status = NT_STATUS_NO_MEMORY;
3465 goto fail;
3468 for (i=0; i<num_streams; i++) {
3469 struct smb_filename *smb_fname;
3471 if (strequal(stream_info[i].name, "::$DATA")) {
3472 streams[i] = NULL;
3473 continue;
3476 smb_fname = synthetic_smb_fname(
3477 talloc_tos(), fname, stream_info[i].name, NULL);
3478 if (smb_fname == NULL) {
3479 status = NT_STATUS_NO_MEMORY;
3480 goto fail;
3483 if (SMB_VFS_STAT(conn, smb_fname) == -1) {
3484 DEBUG(10, ("Unable to stat stream: %s\n",
3485 smb_fname_str_dbg(smb_fname)));
3488 status = SMB_VFS_CREATE_FILE(
3489 conn, /* conn */
3490 NULL, /* req */
3491 0, /* root_dir_fid */
3492 smb_fname, /* fname */
3493 DELETE_ACCESS, /* access_mask */
3494 (FILE_SHARE_READ | /* share_access */
3495 FILE_SHARE_WRITE | FILE_SHARE_DELETE),
3496 FILE_OPEN, /* create_disposition*/
3497 0, /* create_options */
3498 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
3499 0, /* oplock_request */
3500 0, /* allocation_size */
3501 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */
3502 NULL, /* sd */
3503 NULL, /* ea_list */
3504 &streams[i], /* result */
3505 NULL); /* pinfo */
3507 if (!NT_STATUS_IS_OK(status)) {
3508 DEBUG(10, ("Could not open stream %s: %s\n",
3509 smb_fname_str_dbg(smb_fname),
3510 nt_errstr(status)));
3512 TALLOC_FREE(smb_fname);
3513 break;
3515 TALLOC_FREE(smb_fname);
3519 * don't touch the variable "status" beyond this point :-)
3522 for (i -= 1 ; i >= 0; i--) {
3523 if (streams[i] == NULL) {
3524 continue;
3527 DEBUG(10, ("Closing stream # %d, %s\n", i,
3528 fsp_str_dbg(streams[i])));
3529 close_file(NULL, streams[i], NORMAL_CLOSE);
3532 fail:
3533 TALLOC_FREE(frame);
3534 return status;
3537 /*********************************************************************
3538 Create a default ACL by inheriting from the parent. If no inheritance
3539 from the parent available, don't set anything. This will leave the actual
3540 permissions the new file or directory already got from the filesystem
3541 as the NT ACL when read.
3542 *********************************************************************/
3544 static NTSTATUS inherit_new_acl(files_struct *fsp)
3546 TALLOC_CTX *frame = talloc_stackframe();
3547 char *parent_name = NULL;
3548 struct security_descriptor *parent_desc = NULL;
3549 NTSTATUS status = NT_STATUS_OK;
3550 struct security_descriptor *psd = NULL;
3551 const struct dom_sid *owner_sid = NULL;
3552 const struct dom_sid *group_sid = NULL;
3553 uint32_t security_info_sent = (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL);
3554 struct security_token *token = fsp->conn->session_info->security_token;
3555 bool inherit_owner = lp_inherit_owner(SNUM(fsp->conn));
3556 bool inheritable_components = false;
3557 bool try_builtin_administrators = false;
3558 const struct dom_sid *BA_U_sid = NULL;
3559 const struct dom_sid *BA_G_sid = NULL;
3560 bool try_system = false;
3561 const struct dom_sid *SY_U_sid = NULL;
3562 const struct dom_sid *SY_G_sid = NULL;
3563 size_t size = 0;
3565 if (!parent_dirname(frame, fsp->fsp_name->base_name, &parent_name, NULL)) {
3566 TALLOC_FREE(frame);
3567 return NT_STATUS_NO_MEMORY;
3570 status = SMB_VFS_GET_NT_ACL(fsp->conn,
3571 parent_name,
3572 (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL),
3573 frame,
3574 &parent_desc);
3575 if (!NT_STATUS_IS_OK(status)) {
3576 TALLOC_FREE(frame);
3577 return status;
3580 inheritable_components = sd_has_inheritable_components(parent_desc,
3581 fsp->is_directory);
3583 if (!inheritable_components && !inherit_owner) {
3584 TALLOC_FREE(frame);
3585 /* Nothing to inherit and not setting owner. */
3586 return NT_STATUS_OK;
3589 /* Create an inherited descriptor from the parent. */
3591 if (DEBUGLEVEL >= 10) {
3592 DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
3593 fsp_str_dbg(fsp) ));
3594 NDR_PRINT_DEBUG(security_descriptor, parent_desc);
3597 /* Inherit from parent descriptor if "inherit owner" set. */
3598 if (inherit_owner) {
3599 owner_sid = parent_desc->owner_sid;
3600 group_sid = parent_desc->group_sid;
3603 if (owner_sid == NULL) {
3604 if (security_token_has_builtin_administrators(token)) {
3605 try_builtin_administrators = true;
3606 } else if (security_token_is_system(token)) {
3607 try_builtin_administrators = true;
3608 try_system = true;
3612 if (group_sid == NULL &&
3613 token->num_sids == PRIMARY_GROUP_SID_INDEX)
3615 if (security_token_is_system(token)) {
3616 try_builtin_administrators = true;
3617 try_system = true;
3621 if (try_builtin_administrators) {
3622 struct unixid ids;
3623 bool ok;
3625 ZERO_STRUCT(ids);
3626 ok = sids_to_unixids(&global_sid_Builtin_Administrators, 1, &ids);
3627 if (ok) {
3628 switch (ids.type) {
3629 case ID_TYPE_BOTH:
3630 BA_U_sid = &global_sid_Builtin_Administrators;
3631 BA_G_sid = &global_sid_Builtin_Administrators;
3632 break;
3633 case ID_TYPE_UID:
3634 BA_U_sid = &global_sid_Builtin_Administrators;
3635 break;
3636 case ID_TYPE_GID:
3637 BA_G_sid = &global_sid_Builtin_Administrators;
3638 break;
3639 default:
3640 break;
3645 if (try_system) {
3646 struct unixid ids;
3647 bool ok;
3649 ZERO_STRUCT(ids);
3650 ok = sids_to_unixids(&global_sid_System, 1, &ids);
3651 if (ok) {
3652 switch (ids.type) {
3653 case ID_TYPE_BOTH:
3654 SY_U_sid = &global_sid_System;
3655 SY_G_sid = &global_sid_System;
3656 break;
3657 case ID_TYPE_UID:
3658 SY_U_sid = &global_sid_System;
3659 break;
3660 case ID_TYPE_GID:
3661 SY_G_sid = &global_sid_System;
3662 break;
3663 default:
3664 break;
3669 if (owner_sid == NULL) {
3670 owner_sid = BA_U_sid;
3673 if (owner_sid == NULL) {
3674 owner_sid = SY_U_sid;
3677 if (group_sid == NULL) {
3678 group_sid = SY_G_sid;
3681 if (try_system && group_sid == NULL) {
3682 group_sid = BA_G_sid;
3685 if (owner_sid == NULL) {
3686 owner_sid = &token->sids[PRIMARY_USER_SID_INDEX];
3688 if (group_sid == NULL) {
3689 if (token->num_sids == PRIMARY_GROUP_SID_INDEX) {
3690 group_sid = &token->sids[PRIMARY_USER_SID_INDEX];
3691 } else {
3692 group_sid = &token->sids[PRIMARY_GROUP_SID_INDEX];
3696 status = se_create_child_secdesc(frame,
3697 &psd,
3698 &size,
3699 parent_desc,
3700 owner_sid,
3701 group_sid,
3702 fsp->is_directory);
3703 if (!NT_STATUS_IS_OK(status)) {
3704 TALLOC_FREE(frame);
3705 return status;
3708 /* If inheritable_components == false,
3709 se_create_child_secdesc()
3710 creates a security desriptor with a NULL dacl
3711 entry, but with SEC_DESC_DACL_PRESENT. We need
3712 to remove that flag. */
3714 if (!inheritable_components) {
3715 security_info_sent &= ~SECINFO_DACL;
3716 psd->type &= ~SEC_DESC_DACL_PRESENT;
3719 if (DEBUGLEVEL >= 10) {
3720 DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
3721 fsp_str_dbg(fsp) ));
3722 NDR_PRINT_DEBUG(security_descriptor, psd);
3725 if (inherit_owner) {
3726 /* We need to be root to force this. */
3727 become_root();
3729 status = SMB_VFS_FSET_NT_ACL(fsp,
3730 security_info_sent,
3731 psd);
3732 if (inherit_owner) {
3733 unbecome_root();
3735 TALLOC_FREE(frame);
3736 return status;
3740 * Wrapper around open_file_ntcreate and open_directory
3743 static NTSTATUS create_file_unixpath(connection_struct *conn,
3744 struct smb_request *req,
3745 struct smb_filename *smb_fname,
3746 uint32_t access_mask,
3747 uint32_t share_access,
3748 uint32_t create_disposition,
3749 uint32_t create_options,
3750 uint32_t file_attributes,
3751 uint32_t oplock_request,
3752 uint64_t allocation_size,
3753 uint32_t private_flags,
3754 struct security_descriptor *sd,
3755 struct ea_list *ea_list,
3757 files_struct **result,
3758 int *pinfo)
3760 int info = FILE_WAS_OPENED;
3761 files_struct *base_fsp = NULL;
3762 files_struct *fsp = NULL;
3763 NTSTATUS status;
3765 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3766 "file_attributes = 0x%x, share_access = 0x%x, "
3767 "create_disposition = 0x%x create_options = 0x%x "
3768 "oplock_request = 0x%x private_flags = 0x%x "
3769 "ea_list = 0x%p, sd = 0x%p, "
3770 "fname = %s\n",
3771 (unsigned int)access_mask,
3772 (unsigned int)file_attributes,
3773 (unsigned int)share_access,
3774 (unsigned int)create_disposition,
3775 (unsigned int)create_options,
3776 (unsigned int)oplock_request,
3777 (unsigned int)private_flags,
3778 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3780 if (create_options & FILE_OPEN_BY_FILE_ID) {
3781 status = NT_STATUS_NOT_SUPPORTED;
3782 goto fail;
3785 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
3786 status = NT_STATUS_INVALID_PARAMETER;
3787 goto fail;
3790 if (req == NULL) {
3791 oplock_request |= INTERNAL_OPEN_ONLY;
3794 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3795 && (access_mask & DELETE_ACCESS)
3796 && !is_ntfs_stream_smb_fname(smb_fname)) {
3798 * We can't open a file with DELETE access if any of the
3799 * streams is open without FILE_SHARE_DELETE
3801 status = open_streams_for_delete(conn, smb_fname->base_name);
3803 if (!NT_STATUS_IS_OK(status)) {
3804 goto fail;
3808 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3809 !security_token_has_privilege(get_current_nttok(conn),
3810 SEC_PRIV_SECURITY)) {
3811 DEBUG(10, ("create_file_unixpath: open on %s "
3812 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3813 smb_fname_str_dbg(smb_fname)));
3814 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3815 goto fail;
3818 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3819 && is_ntfs_stream_smb_fname(smb_fname)
3820 && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
3821 uint32 base_create_disposition;
3822 struct smb_filename *smb_fname_base = NULL;
3824 if (create_options & FILE_DIRECTORY_FILE) {
3825 status = NT_STATUS_NOT_A_DIRECTORY;
3826 goto fail;
3829 switch (create_disposition) {
3830 case FILE_OPEN:
3831 base_create_disposition = FILE_OPEN;
3832 break;
3833 default:
3834 base_create_disposition = FILE_OPEN_IF;
3835 break;
3838 /* Create an smb_filename with stream_name == NULL. */
3839 smb_fname_base = synthetic_smb_fname(talloc_tos(),
3840 smb_fname->base_name,
3841 NULL, NULL);
3842 if (smb_fname_base == NULL) {
3843 status = NT_STATUS_NO_MEMORY;
3844 goto fail;
3847 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
3848 DEBUG(10, ("Unable to stat stream: %s\n",
3849 smb_fname_str_dbg(smb_fname_base)));
3850 } else {
3852 * https://bugzilla.samba.org/show_bug.cgi?id=10229
3853 * We need to check if the requested access mask
3854 * could be used to open the underlying file (if
3855 * it existed), as we're passing in zero for the
3856 * access mask to the base filename.
3858 status = check_base_file_access(conn,
3859 smb_fname_base,
3860 access_mask);
3862 if (!NT_STATUS_IS_OK(status)) {
3863 DEBUG(10, ("Permission check "
3864 "for base %s failed: "
3865 "%s\n", smb_fname->base_name,
3866 nt_errstr(status)));
3867 goto fail;
3871 /* Open the base file. */
3872 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
3873 FILE_SHARE_READ
3874 | FILE_SHARE_WRITE
3875 | FILE_SHARE_DELETE,
3876 base_create_disposition,
3877 0, 0, 0, 0, 0, NULL, NULL,
3878 &base_fsp, NULL);
3879 TALLOC_FREE(smb_fname_base);
3881 if (!NT_STATUS_IS_OK(status)) {
3882 DEBUG(10, ("create_file_unixpath for base %s failed: "
3883 "%s\n", smb_fname->base_name,
3884 nt_errstr(status)));
3885 goto fail;
3887 /* we don't need to low level fd */
3888 fd_close(base_fsp);
3892 * If it's a request for a directory open, deal with it separately.
3895 if (create_options & FILE_DIRECTORY_FILE) {
3897 if (create_options & FILE_NON_DIRECTORY_FILE) {
3898 status = NT_STATUS_INVALID_PARAMETER;
3899 goto fail;
3902 /* Can't open a temp directory. IFS kit test. */
3903 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
3904 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
3905 status = NT_STATUS_INVALID_PARAMETER;
3906 goto fail;
3910 * We will get a create directory here if the Win32
3911 * app specified a security descriptor in the
3912 * CreateDirectory() call.
3915 oplock_request = 0;
3916 status = open_directory(
3917 conn, req, smb_fname, access_mask, share_access,
3918 create_disposition, create_options, file_attributes,
3919 &info, &fsp);
3920 } else {
3923 * Ordinary file case.
3926 status = file_new(req, conn, &fsp);
3927 if(!NT_STATUS_IS_OK(status)) {
3928 goto fail;
3931 status = fsp_set_smb_fname(fsp, smb_fname);
3932 if (!NT_STATUS_IS_OK(status)) {
3933 goto fail;
3936 if (base_fsp) {
3938 * We're opening the stream element of a
3939 * base_fsp we already opened. Set up the
3940 * base_fsp pointer.
3942 fsp->base_fsp = base_fsp;
3945 if (allocation_size) {
3946 fsp->initial_allocation_size = smb_roundup(fsp->conn,
3947 allocation_size);
3950 status = open_file_ntcreate(conn,
3951 req,
3952 access_mask,
3953 share_access,
3954 create_disposition,
3955 create_options,
3956 file_attributes,
3957 oplock_request,
3958 private_flags,
3959 &info,
3960 fsp);
3962 if(!NT_STATUS_IS_OK(status)) {
3963 file_free(req, fsp);
3964 fsp = NULL;
3967 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
3969 /* A stream open never opens a directory */
3971 if (base_fsp) {
3972 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3973 goto fail;
3977 * Fail the open if it was explicitly a non-directory
3978 * file.
3981 if (create_options & FILE_NON_DIRECTORY_FILE) {
3982 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3983 goto fail;
3986 oplock_request = 0;
3987 status = open_directory(
3988 conn, req, smb_fname, access_mask,
3989 share_access, create_disposition,
3990 create_options, file_attributes,
3991 &info, &fsp);
3995 if (!NT_STATUS_IS_OK(status)) {
3996 goto fail;
3999 fsp->base_fsp = base_fsp;
4001 if ((ea_list != NULL) &&
4002 ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
4003 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
4004 if (!NT_STATUS_IS_OK(status)) {
4005 goto fail;
4009 if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
4010 status = NT_STATUS_ACCESS_DENIED;
4011 goto fail;
4014 /* Save the requested allocation size. */
4015 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
4016 if (allocation_size
4017 && (allocation_size > fsp->fsp_name->st.st_ex_size)) {
4018 fsp->initial_allocation_size = smb_roundup(
4019 fsp->conn, allocation_size);
4020 if (fsp->is_directory) {
4021 /* Can't set allocation size on a directory. */
4022 status = NT_STATUS_ACCESS_DENIED;
4023 goto fail;
4025 if (vfs_allocate_file_space(
4026 fsp, fsp->initial_allocation_size) == -1) {
4027 status = NT_STATUS_DISK_FULL;
4028 goto fail;
4030 } else {
4031 fsp->initial_allocation_size = smb_roundup(
4032 fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
4034 } else {
4035 fsp->initial_allocation_size = 0;
4038 if ((info == FILE_WAS_CREATED) && lp_nt_acl_support(SNUM(conn)) &&
4039 fsp->base_fsp == NULL) {
4040 if (sd != NULL) {
4042 * According to the MS documentation, the only time the security
4043 * descriptor is applied to the opened file is iff we *created* the
4044 * file; an existing file stays the same.
4046 * Also, it seems (from observation) that you can open the file with
4047 * any access mask but you can still write the sd. We need to override
4048 * the granted access before we call set_sd
4049 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
4052 uint32_t sec_info_sent;
4053 uint32_t saved_access_mask = fsp->access_mask;
4055 sec_info_sent = get_sec_info(sd);
4057 fsp->access_mask = FILE_GENERIC_ALL;
4059 if (sec_info_sent & (SECINFO_OWNER|
4060 SECINFO_GROUP|
4061 SECINFO_DACL|
4062 SECINFO_SACL)) {
4063 status = set_sd(fsp, sd, sec_info_sent);
4066 fsp->access_mask = saved_access_mask;
4068 if (!NT_STATUS_IS_OK(status)) {
4069 goto fail;
4071 } else if (lp_inherit_acls(SNUM(conn))) {
4072 /* Inherit from parent. Errors here are not fatal. */
4073 status = inherit_new_acl(fsp);
4074 if (!NT_STATUS_IS_OK(status)) {
4075 DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
4076 fsp_str_dbg(fsp),
4077 nt_errstr(status) ));
4082 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
4084 *result = fsp;
4085 if (pinfo != NULL) {
4086 *pinfo = info;
4089 smb_fname->st = fsp->fsp_name->st;
4091 return NT_STATUS_OK;
4093 fail:
4094 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
4096 if (fsp != NULL) {
4097 if (base_fsp && fsp->base_fsp == base_fsp) {
4099 * The close_file below will close
4100 * fsp->base_fsp.
4102 base_fsp = NULL;
4104 close_file(req, fsp, ERROR_CLOSE);
4105 fsp = NULL;
4107 if (base_fsp != NULL) {
4108 close_file(req, base_fsp, ERROR_CLOSE);
4109 base_fsp = NULL;
4111 return status;
4115 * Calculate the full path name given a relative fid.
4117 NTSTATUS get_relative_fid_filename(connection_struct *conn,
4118 struct smb_request *req,
4119 uint16_t root_dir_fid,
4120 const struct smb_filename *smb_fname,
4121 struct smb_filename **smb_fname_out)
4123 files_struct *dir_fsp;
4124 char *parent_fname = NULL;
4125 char *new_base_name = NULL;
4126 NTSTATUS status;
4128 if (root_dir_fid == 0 || !smb_fname) {
4129 status = NT_STATUS_INTERNAL_ERROR;
4130 goto out;
4133 dir_fsp = file_fsp(req, root_dir_fid);
4135 if (dir_fsp == NULL) {
4136 status = NT_STATUS_INVALID_HANDLE;
4137 goto out;
4140 if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) {
4141 status = NT_STATUS_INVALID_HANDLE;
4142 goto out;
4145 if (!dir_fsp->is_directory) {
4148 * Check to see if this is a mac fork of some kind.
4151 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
4152 is_ntfs_stream_smb_fname(smb_fname)) {
4153 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
4154 goto out;
4158 we need to handle the case when we get a
4159 relative open relative to a file and the
4160 pathname is blank - this is a reopen!
4161 (hint from demyn plantenberg)
4164 status = NT_STATUS_INVALID_HANDLE;
4165 goto out;
4168 if (ISDOT(dir_fsp->fsp_name->base_name)) {
4170 * We're at the toplevel dir, the final file name
4171 * must not contain ./, as this is filtered out
4172 * normally by srvstr_get_path and unix_convert
4173 * explicitly rejects paths containing ./.
4175 parent_fname = talloc_strdup(talloc_tos(), "");
4176 if (parent_fname == NULL) {
4177 status = NT_STATUS_NO_MEMORY;
4178 goto out;
4180 } else {
4181 size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name);
4184 * Copy in the base directory name.
4187 parent_fname = talloc_array(talloc_tos(), char,
4188 dir_name_len+2);
4189 if (parent_fname == NULL) {
4190 status = NT_STATUS_NO_MEMORY;
4191 goto out;
4193 memcpy(parent_fname, dir_fsp->fsp_name->base_name,
4194 dir_name_len+1);
4197 * Ensure it ends in a '/'.
4198 * We used TALLOC_SIZE +2 to add space for the '/'.
4201 if(dir_name_len
4202 && (parent_fname[dir_name_len-1] != '\\')
4203 && (parent_fname[dir_name_len-1] != '/')) {
4204 parent_fname[dir_name_len] = '/';
4205 parent_fname[dir_name_len+1] = '\0';
4209 new_base_name = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
4210 smb_fname->base_name);
4211 if (new_base_name == NULL) {
4212 status = NT_STATUS_NO_MEMORY;
4213 goto out;
4216 status = filename_convert(req,
4217 conn,
4218 req->flags2 & FLAGS2_DFS_PATHNAMES,
4219 new_base_name,
4221 NULL,
4222 smb_fname_out);
4223 if (!NT_STATUS_IS_OK(status)) {
4224 goto out;
4227 out:
4228 TALLOC_FREE(parent_fname);
4229 TALLOC_FREE(new_base_name);
4230 return status;
4233 NTSTATUS create_file_default(connection_struct *conn,
4234 struct smb_request *req,
4235 uint16_t root_dir_fid,
4236 struct smb_filename *smb_fname,
4237 uint32_t access_mask,
4238 uint32_t share_access,
4239 uint32_t create_disposition,
4240 uint32_t create_options,
4241 uint32_t file_attributes,
4242 uint32_t oplock_request,
4243 uint64_t allocation_size,
4244 uint32_t private_flags,
4245 struct security_descriptor *sd,
4246 struct ea_list *ea_list,
4247 files_struct **result,
4248 int *pinfo)
4250 int info = FILE_WAS_OPENED;
4251 files_struct *fsp = NULL;
4252 NTSTATUS status;
4253 bool stream_name = false;
4255 DEBUG(10,("create_file: access_mask = 0x%x "
4256 "file_attributes = 0x%x, share_access = 0x%x, "
4257 "create_disposition = 0x%x create_options = 0x%x "
4258 "oplock_request = 0x%x "
4259 "private_flags = 0x%x "
4260 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
4261 "fname = %s\n",
4262 (unsigned int)access_mask,
4263 (unsigned int)file_attributes,
4264 (unsigned int)share_access,
4265 (unsigned int)create_disposition,
4266 (unsigned int)create_options,
4267 (unsigned int)oplock_request,
4268 (unsigned int)private_flags,
4269 (unsigned int)root_dir_fid,
4270 ea_list, sd, smb_fname_str_dbg(smb_fname)));
4273 * Calculate the filename from the root_dir_if if necessary.
4276 if (root_dir_fid != 0) {
4277 struct smb_filename *smb_fname_out = NULL;
4278 status = get_relative_fid_filename(conn, req, root_dir_fid,
4279 smb_fname, &smb_fname_out);
4280 if (!NT_STATUS_IS_OK(status)) {
4281 goto fail;
4283 smb_fname = smb_fname_out;
4287 * Check to see if this is a mac fork of some kind.
4290 stream_name = is_ntfs_stream_smb_fname(smb_fname);
4291 if (stream_name) {
4292 enum FAKE_FILE_TYPE fake_file_type;
4294 fake_file_type = is_fake_file(smb_fname);
4296 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
4299 * Here we go! support for changing the disk quotas
4300 * --metze
4302 * We need to fake up to open this MAGIC QUOTA file
4303 * and return a valid FID.
4305 * w2k close this file directly after openening xp
4306 * also tries a QUERY_FILE_INFO on the file and then
4307 * close it
4309 status = open_fake_file(req, conn, req->vuid,
4310 fake_file_type, smb_fname,
4311 access_mask, &fsp);
4312 if (!NT_STATUS_IS_OK(status)) {
4313 goto fail;
4316 ZERO_STRUCT(smb_fname->st);
4317 goto done;
4320 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
4321 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4322 goto fail;
4326 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
4327 int ret;
4328 smb_fname->stream_name = NULL;
4329 /* We have to handle this error here. */
4330 if (create_options & FILE_DIRECTORY_FILE) {
4331 status = NT_STATUS_NOT_A_DIRECTORY;
4332 goto fail;
4334 if (lp_posix_pathnames()) {
4335 ret = SMB_VFS_LSTAT(conn, smb_fname);
4336 } else {
4337 ret = SMB_VFS_STAT(conn, smb_fname);
4340 if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
4341 status = NT_STATUS_FILE_IS_A_DIRECTORY;
4342 goto fail;
4346 status = create_file_unixpath(
4347 conn, req, smb_fname, access_mask, share_access,
4348 create_disposition, create_options, file_attributes,
4349 oplock_request, allocation_size, private_flags,
4350 sd, ea_list,
4351 &fsp, &info);
4353 if (!NT_STATUS_IS_OK(status)) {
4354 goto fail;
4357 done:
4358 DEBUG(10, ("create_file: info=%d\n", info));
4360 *result = fsp;
4361 if (pinfo != NULL) {
4362 *pinfo = info;
4364 return NT_STATUS_OK;
4366 fail:
4367 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
4369 if (fsp != NULL) {
4370 close_file(req, fsp, ERROR_CLOSE);
4371 fsp = NULL;
4373 return status;