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/>.
23 #include "system/filesys.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"
33 extern const struct generic_mapping file_generic_mapping;
35 struct deferred_open_record {
36 bool delayed_for_oplocks;
40 /****************************************************************************
41 SMB1 file varient of se_access_check. Never test FILE_READ_ATTRIBUTES.
42 ****************************************************************************/
44 NTSTATUS smb1_file_se_access_check(struct connection_struct *conn,
45 const struct security_descriptor *sd,
46 const struct security_token *token,
47 uint32_t access_desired,
48 uint32_t *access_granted)
52 if (get_current_uid(conn) == (uid_t)0) {
53 /* I'm sorry sir, I didn't know you were root... */
54 *access_granted = access_desired;
55 if (access_desired & SEC_FLAG_MAXIMUM_ALLOWED) {
56 *access_granted |= FILE_GENERIC_ALL;
61 return se_access_check(sd,
63 (access_desired & ~FILE_READ_ATTRIBUTES),
67 /****************************************************************************
68 Check if we have open rights.
69 ****************************************************************************/
71 NTSTATUS smbd_check_open_rights(struct connection_struct *conn,
72 const struct smb_filename *smb_fname,
74 uint32_t *access_granted)
76 /* Check if we have rights to open. */
78 struct security_descriptor *sd = NULL;
80 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
85 if (!NT_STATUS_IS_OK(status)) {
86 DEBUG(10, ("smbd_check_open_rights: Could not get acl "
88 smb_fname_str_dbg(smb_fname),
93 status = smb1_file_se_access_check(conn,
95 get_current_nttok(conn),
99 DEBUG(10,("smbd_check_open_rights: file %s requesting "
100 "0x%x returning 0x%x (%s)\n",
101 smb_fname_str_dbg(smb_fname),
102 (unsigned int)access_mask,
103 (unsigned int)*access_granted,
104 nt_errstr(status) ));
106 if (!NT_STATUS_IS_OK(status)) {
107 if (DEBUGLEVEL >= 10) {
108 DEBUG(10,("smbd_check_open_rights: acl for %s is:\n",
109 smb_fname_str_dbg(smb_fname) ));
110 NDR_PRINT_DEBUG(security_descriptor, sd);
119 /****************************************************************************
120 fd support routines - attempt to do a dos_open.
121 ****************************************************************************/
123 static NTSTATUS fd_open(struct connection_struct *conn,
128 struct smb_filename *smb_fname = fsp->fsp_name;
129 NTSTATUS status = NT_STATUS_OK;
133 * Never follow symlinks on a POSIX client. The
134 * client should be doing this.
137 if (fsp->posix_open || !lp_symlinks(SNUM(conn))) {
142 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, mode);
143 if (fsp->fh->fd == -1) {
144 status = map_nt_error_from_unix(errno);
145 if (errno == EMFILE) {
146 static time_t last_warned = 0L;
148 if (time((time_t *) NULL) > last_warned) {
149 DEBUG(0,("Too many open files, unable "
150 "to open more! smbd's max "
152 lp_max_open_files()));
153 last_warned = time((time_t *) NULL);
159 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
160 smb_fname_str_dbg(smb_fname), flags, (int)mode, fsp->fh->fd,
161 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
166 /****************************************************************************
167 Close the file associated with a fsp.
168 ****************************************************************************/
170 NTSTATUS fd_close(files_struct *fsp)
177 if (fsp->fh->fd == -1) {
178 return NT_STATUS_OK; /* What we used to call a stat open. */
180 if (fsp->fh->ref_count > 1) {
181 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
184 ret = SMB_VFS_CLOSE(fsp);
187 return map_nt_error_from_unix(errno);
192 /****************************************************************************
193 Change the ownership of a file to that of the parent directory.
194 Do this by fd if possible.
195 ****************************************************************************/
197 void change_file_owner_to_parent(connection_struct *conn,
198 const char *inherit_from_dir,
201 struct smb_filename *smb_fname_parent = NULL;
205 status = create_synthetic_smb_fname(talloc_tos(), inherit_from_dir,
206 NULL, NULL, &smb_fname_parent);
207 if (!NT_STATUS_IS_OK(status)) {
211 ret = SMB_VFS_STAT(conn, smb_fname_parent);
213 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
214 "directory %s. Error was %s\n",
215 smb_fname_str_dbg(smb_fname_parent),
217 TALLOC_FREE(smb_fname_parent);
221 if (smb_fname_parent->st.st_ex_uid == fsp->fsp_name->st.st_ex_uid) {
222 /* Already this uid - no need to change. */
223 DEBUG(10,("change_file_owner_to_parent: file %s "
224 "is already owned by uid %d\n",
226 (int)fsp->fsp_name->st.st_ex_uid ));
227 TALLOC_FREE(smb_fname_parent);
232 ret = SMB_VFS_FCHOWN(fsp, smb_fname_parent->st.st_ex_uid, (gid_t)-1);
235 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
236 "file %s to parent directory uid %u. Error "
237 "was %s\n", fsp_str_dbg(fsp),
238 (unsigned int)smb_fname_parent->st.st_ex_uid,
241 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
242 "parent directory uid %u.\n", fsp_str_dbg(fsp),
243 (unsigned int)smb_fname_parent->st.st_ex_uid));
246 TALLOC_FREE(smb_fname_parent);
249 NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
250 const char *inherit_from_dir,
252 SMB_STRUCT_STAT *psbuf)
254 struct smb_filename *smb_fname_parent = NULL;
255 struct smb_filename *smb_fname_cwd = NULL;
256 char *saved_dir = NULL;
257 TALLOC_CTX *ctx = talloc_tos();
258 NTSTATUS status = NT_STATUS_OK;
261 status = create_synthetic_smb_fname(ctx, inherit_from_dir, NULL, NULL,
263 if (!NT_STATUS_IS_OK(status)) {
267 ret = SMB_VFS_STAT(conn, smb_fname_parent);
269 status = map_nt_error_from_unix(errno);
270 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
271 "directory %s. Error was %s\n",
272 smb_fname_str_dbg(smb_fname_parent),
277 /* We've already done an lstat into psbuf, and we know it's a
278 directory. If we can cd into the directory and the dev/ino
279 are the same then we can safely chown without races as
280 we're locking the directory in place by being in it. This
281 should work on any UNIX (thanks tridge :-). JRA.
284 saved_dir = vfs_GetWd(ctx,conn);
286 status = map_nt_error_from_unix(errno);
287 DEBUG(0,("change_dir_owner_to_parent: failed to get "
288 "current working directory. Error was %s\n",
293 /* Chdir into the new path. */
294 if (vfs_ChDir(conn, fname) == -1) {
295 status = map_nt_error_from_unix(errno);
296 DEBUG(0,("change_dir_owner_to_parent: failed to change "
297 "current working directory to %s. Error "
298 "was %s\n", fname, strerror(errno) ));
302 status = create_synthetic_smb_fname(ctx, ".", NULL, NULL,
304 if (!NT_STATUS_IS_OK(status)) {
308 ret = SMB_VFS_STAT(conn, smb_fname_cwd);
310 status = map_nt_error_from_unix(errno);
311 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
312 "directory '.' (%s) Error was %s\n",
313 fname, strerror(errno)));
317 /* Ensure we're pointing at the same place. */
318 if (smb_fname_cwd->st.st_ex_dev != psbuf->st_ex_dev ||
319 smb_fname_cwd->st.st_ex_ino != psbuf->st_ex_ino ||
320 smb_fname_cwd->st.st_ex_mode != psbuf->st_ex_mode ) {
321 DEBUG(0,("change_dir_owner_to_parent: "
322 "device/inode/mode on directory %s changed. "
323 "Refusing to chown !\n", fname ));
324 status = NT_STATUS_ACCESS_DENIED;
328 if (smb_fname_parent->st.st_ex_uid == smb_fname_cwd->st.st_ex_uid) {
329 /* Already this uid - no need to change. */
330 DEBUG(10,("change_dir_owner_to_parent: directory %s "
331 "is already owned by uid %d\n",
333 (int)smb_fname_cwd->st.st_ex_uid ));
334 status = NT_STATUS_OK;
339 ret = SMB_VFS_LCHOWN(conn, ".", smb_fname_parent->st.st_ex_uid,
343 status = map_nt_error_from_unix(errno);
344 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
345 "directory %s to parent directory uid %u. "
346 "Error was %s\n", fname,
347 (unsigned int)smb_fname_parent->st.st_ex_uid,
350 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
351 "directory %s to parent directory uid %u.\n",
352 fname, (unsigned int)smb_fname_parent->st.st_ex_uid ));
356 vfs_ChDir(conn,saved_dir);
358 TALLOC_FREE(smb_fname_parent);
359 TALLOC_FREE(smb_fname_cwd);
363 /****************************************************************************
365 ****************************************************************************/
367 static NTSTATUS open_file(files_struct *fsp,
368 connection_struct *conn,
369 struct smb_request *req,
370 const char *parent_dir,
373 uint32 access_mask, /* client requested access mask. */
374 uint32 open_access_mask) /* what we're actually using in the open. */
376 struct smb_filename *smb_fname = fsp->fsp_name;
377 NTSTATUS status = NT_STATUS_OK;
378 int accmode = (flags & O_ACCMODE);
379 int local_flags = flags;
380 bool file_existed = VALID_STAT(fsp->fsp_name->st);
385 /* Check permissions */
388 * This code was changed after seeing a client open request
389 * containing the open mode of (DENY_WRITE/read-only) with
390 * the 'create if not exist' bit set. The previous code
391 * would fail to open the file read only on a read-only share
392 * as it was checking the flags parameter directly against O_RDONLY,
393 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
397 if (!CAN_WRITE(conn)) {
398 /* It's a read-only share - fail if we wanted to write. */
399 if(accmode != O_RDONLY || (flags & O_TRUNC) || (flags & O_APPEND)) {
400 DEBUG(3,("Permission denied opening %s\n",
401 smb_fname_str_dbg(smb_fname)));
402 return NT_STATUS_ACCESS_DENIED;
403 } else if(flags & O_CREAT) {
404 /* We don't want to write - but we must make sure that
405 O_CREAT doesn't create the file if we have write
406 access into the directory.
408 flags &= ~(O_CREAT|O_EXCL);
409 local_flags &= ~(O_CREAT|O_EXCL);
414 * This little piece of insanity is inspired by the
415 * fact that an NT client can open a file for O_RDONLY,
416 * but set the create disposition to FILE_EXISTS_TRUNCATE.
417 * If the client *can* write to the file, then it expects to
418 * truncate the file, even though it is opening for readonly.
419 * Quicken uses this stupid trick in backup file creation...
420 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
421 * for helping track this one down. It didn't bite us in 2.0.x
422 * as we always opened files read-write in that release. JRA.
425 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
426 DEBUG(10,("open_file: truncate requested on read-only open "
427 "for file %s\n", smb_fname_str_dbg(smb_fname)));
428 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
431 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
432 (!file_existed && (local_flags & O_CREAT)) ||
433 ((local_flags & O_TRUNC) == O_TRUNC) ) {
437 * We can't actually truncate here as the file may be locked.
438 * open_file_ntcreate will take care of the truncate later. JRA.
441 local_flags &= ~O_TRUNC;
443 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
445 * We would block on opening a FIFO with no one else on the
446 * other end. Do what we used to do and add O_NONBLOCK to the
450 if (file_existed && S_ISFIFO(smb_fname->st.st_ex_mode)) {
451 local_flags |= O_NONBLOCK;
455 /* Don't create files with Microsoft wildcard characters. */
458 * wildcard characters are allowed in stream names
459 * only test the basefilename
461 wild = fsp->base_fsp->fsp_name->base_name;
463 wild = smb_fname->base_name;
465 if ((local_flags & O_CREAT) && !file_existed &&
467 return NT_STATUS_OBJECT_NAME_INVALID;
470 /* Actually do the open */
471 status = fd_open(conn, fsp, local_flags, unx_mode);
472 if (!NT_STATUS_IS_OK(status)) {
473 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
474 "(flags=%d)\n", smb_fname_str_dbg(smb_fname),
475 nt_errstr(status),local_flags,flags));
479 if ((local_flags & O_CREAT) && !file_existed) {
481 /* Inherit the ACL if required */
482 if (lp_inherit_perms(SNUM(conn))) {
483 inherit_access_posix_acl(conn, parent_dir,
484 smb_fname->base_name,
488 /* Change the owner if required. */
489 if (lp_inherit_owner(SNUM(conn))) {
490 change_file_owner_to_parent(conn, parent_dir,
494 notify_fname(conn, NOTIFY_ACTION_ADDED,
495 FILE_NOTIFY_CHANGE_FILE_NAME,
496 smb_fname->base_name);
500 fsp->fh->fd = -1; /* What we used to call a stat open. */
502 uint32_t access_granted = 0;
504 status = smbd_check_open_rights(conn,
508 if (!NT_STATUS_IS_OK(status)) {
509 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
511 * On NT_STATUS_ACCESS_DENIED, access_granted
512 * contains the denied bits.
515 if ((access_mask & FILE_WRITE_ATTRIBUTES) &&
516 (access_granted & FILE_WRITE_ATTRIBUTES) &&
517 (lp_map_readonly(SNUM(conn)) ||
518 lp_map_archive(SNUM(conn)) ||
519 lp_map_hidden(SNUM(conn)) ||
520 lp_map_system(SNUM(conn)))) {
521 access_granted &= ~FILE_WRITE_ATTRIBUTES;
523 DEBUG(10,("open_file: "
532 if ((access_mask & DELETE_ACCESS) &&
533 (access_granted & DELETE_ACCESS) &&
534 can_delete_file_in_directory(conn,
536 /* Were we trying to do a stat open
537 * for delete and didn't get DELETE
538 * access (only) ? Check if the
539 * directory allows DELETE_CHILD.
541 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
544 access_granted &= ~DELETE_ACCESS;
546 DEBUG(10,("open_file: "
554 if (access_granted != 0) {
555 DEBUG(10,("open_file: Access "
562 } else if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
564 S_ISLNK(smb_fname->st.st_ex_mode)) {
565 /* This is a POSIX stat open for delete
566 * or rename on a symlink that points
568 DEBUG(10,("open_file: allowing POSIX "
569 "open on bad symlink %s\n",
573 DEBUG(10,("open_file: "
574 "smbd_check_open_rights on file "
576 smb_fname_str_dbg(smb_fname),
577 nt_errstr(status) ));
587 if (fsp->fh->fd == -1) {
588 ret = SMB_VFS_STAT(conn, smb_fname);
590 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
591 /* If we have an fd, this stat should succeed. */
593 DEBUG(0,("Error doing fstat on open file %s "
595 smb_fname_str_dbg(smb_fname),
600 /* For a non-io open, this stat failing means file not found. JRA */
602 status = map_nt_error_from_unix(errno);
609 * POSIX allows read-only opens of directories. We don't
610 * want to do this (we use a different code path for this)
611 * so catch a directory open and return an EISDIR. JRA.
614 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
617 return NT_STATUS_FILE_IS_A_DIRECTORY;
620 fsp->mode = smb_fname->st.st_ex_mode;
621 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
622 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
623 fsp->file_pid = req ? req->smbpid : 0;
624 fsp->can_lock = True;
625 fsp->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
626 if (!CAN_WRITE(conn)) {
627 fsp->can_write = False;
629 fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ?
632 fsp->print_file = NULL;
633 fsp->modified = False;
634 fsp->sent_oplock_break = NO_BREAK_SENT;
635 fsp->is_directory = False;
636 if (conn->aio_write_behind_list &&
637 is_in_path(smb_fname->base_name, conn->aio_write_behind_list,
638 conn->case_sensitive)) {
639 fsp->aio_write_behind = True;
642 fsp->wcp = NULL; /* Write cache pointer. */
644 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
645 conn->session_info->unix_name,
646 smb_fname_str_dbg(smb_fname),
647 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
648 conn->num_files_open));
654 /*******************************************************************
655 Return True if the filename is one of the special executable types.
656 ********************************************************************/
658 bool is_executable(const char *fname)
660 if ((fname = strrchr_m(fname,'.'))) {
661 if (strequal(fname,".com") ||
662 strequal(fname,".dll") ||
663 strequal(fname,".exe") ||
664 strequal(fname,".sym")) {
671 /****************************************************************************
672 Check if we can open a file with a share mode.
673 Returns True if conflict, False if not.
674 ****************************************************************************/
676 static bool share_conflict(struct share_mode_entry *entry,
680 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
681 "entry->share_access = 0x%x, "
682 "entry->private_options = 0x%x\n",
683 (unsigned int)entry->access_mask,
684 (unsigned int)entry->share_access,
685 (unsigned int)entry->private_options));
687 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
688 (unsigned int)access_mask, (unsigned int)share_access));
690 if ((entry->access_mask & (FILE_WRITE_DATA|
694 DELETE_ACCESS)) == 0) {
695 DEBUG(10,("share_conflict: No conflict due to "
696 "entry->access_mask = 0x%x\n",
697 (unsigned int)entry->access_mask ));
701 if ((access_mask & (FILE_WRITE_DATA|
705 DELETE_ACCESS)) == 0) {
706 DEBUG(10,("share_conflict: No conflict due to "
707 "access_mask = 0x%x\n",
708 (unsigned int)access_mask ));
712 #if 1 /* JRA TEST - Superdebug. */
713 #define CHECK_MASK(num, am, right, sa, share) \
714 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
715 (unsigned int)(num), (unsigned int)(am), \
716 (unsigned int)(right), (unsigned int)(am)&(right) )); \
717 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
718 (unsigned int)(num), (unsigned int)(sa), \
719 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
720 if (((am) & (right)) && !((sa) & (share))) { \
721 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
722 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
723 (unsigned int)(share) )); \
727 #define CHECK_MASK(num, am, right, sa, share) \
728 if (((am) & (right)) && !((sa) & (share))) { \
729 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
730 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
731 (unsigned int)(share) )); \
736 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
737 share_access, FILE_SHARE_WRITE);
738 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
739 entry->share_access, FILE_SHARE_WRITE);
741 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
742 share_access, FILE_SHARE_READ);
743 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
744 entry->share_access, FILE_SHARE_READ);
746 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
747 share_access, FILE_SHARE_DELETE);
748 CHECK_MASK(6, access_mask, DELETE_ACCESS,
749 entry->share_access, FILE_SHARE_DELETE);
751 DEBUG(10,("share_conflict: No conflict.\n"));
755 #if defined(DEVELOPER)
756 static void validate_my_share_entries(struct smbd_server_connection *sconn,
758 struct share_mode_entry *share_entry)
762 if (!procid_is_me(&share_entry->pid)) {
766 if (is_deferred_open_entry(share_entry) &&
767 !open_was_deferred(share_entry->op_mid)) {
768 char *str = talloc_asprintf(talloc_tos(),
769 "Got a deferred entry without a request: "
771 share_mode_str(talloc_tos(), num, share_entry));
775 if (!is_valid_share_mode_entry(share_entry)) {
779 fsp = file_find_dif(sconn, share_entry->id,
780 share_entry->share_file_id);
782 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
783 share_mode_str(talloc_tos(), num, share_entry) ));
784 smb_panic("validate_my_share_entries: Cannot match a "
785 "share entry with an open file\n");
788 if (is_deferred_open_entry(share_entry) ||
789 is_unused_share_mode_entry(share_entry)) {
793 if ((share_entry->op_type == NO_OPLOCK) &&
794 (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK)) {
795 /* Someone has already written to it, but I haven't yet
800 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
809 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
810 share_mode_str(talloc_tos(), num, share_entry) ));
811 str = talloc_asprintf(talloc_tos(),
812 "validate_my_share_entries: "
813 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
814 fsp->fsp_name->base_name,
815 (unsigned int)fsp->oplock_type,
816 (unsigned int)share_entry->op_type );
822 bool is_stat_open(uint32 access_mask)
824 return (access_mask &&
825 ((access_mask & ~(SYNCHRONIZE_ACCESS| FILE_READ_ATTRIBUTES|
826 FILE_WRITE_ATTRIBUTES))==0) &&
827 ((access_mask & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|
828 FILE_WRITE_ATTRIBUTES)) != 0));
831 /****************************************************************************
832 Deal with share modes
833 Invarient: Share mode must be locked on entry and exit.
834 Returns -1 on error, or number of share modes on success (may be zero).
835 ****************************************************************************/
837 static NTSTATUS open_mode_check(connection_struct *conn,
838 struct share_mode_lock *lck,
842 uint32 create_options,
847 if(lck->num_share_modes == 0) {
851 *file_existed = True;
853 /* A delete on close prohibits everything */
855 if (is_delete_on_close_set(lck, name_hash)) {
856 return NT_STATUS_DELETE_PENDING;
859 if (is_stat_open(access_mask)) {
860 /* Stat open that doesn't trigger oplock breaks or share mode
861 * checks... ! JRA. */
866 * Check if the share modes will give us access.
869 #if defined(DEVELOPER)
870 for(i = 0; i < lck->num_share_modes; i++) {
871 validate_my_share_entries(conn->sconn, i,
872 &lck->share_modes[i]);
876 if (!lp_share_modes(SNUM(conn))) {
880 /* Now we check the share modes, after any oplock breaks. */
881 for(i = 0; i < lck->num_share_modes; i++) {
883 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
887 /* someone else has a share lock on it, check to see if we can
889 if (share_conflict(&lck->share_modes[i],
890 access_mask, share_access)) {
891 return NT_STATUS_SHARING_VIOLATION;
898 static bool is_delete_request(files_struct *fsp) {
899 return ((fsp->access_mask == DELETE_ACCESS) &&
900 (fsp->oplock_type == NO_OPLOCK));
904 * Send a break message to the oplock holder and delay the open for
908 static NTSTATUS send_break_message(files_struct *fsp,
909 struct share_mode_entry *exclusive,
914 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
916 DEBUG(10, ("Sending break request to PID %s\n",
917 procid_str_static(&exclusive->pid)));
918 exclusive->op_mid = mid;
920 /* Create the message. */
921 share_mode_entry_to_message(msg, exclusive);
923 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
924 don't want this set in the share mode struct pointed to by lck. */
926 if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
927 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,
928 exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
931 status = messaging_send_buf(fsp->conn->sconn->msg_ctx, exclusive->pid,
932 MSG_SMB_BREAK_REQUEST,
934 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
935 if (!NT_STATUS_IS_OK(status)) {
936 DEBUG(3, ("Could not send oplock break message: %s\n",
944 * Return share_mode_entry pointers for :
945 * 1). Batch oplock entry.
946 * 2). Batch or exclusive oplock entry (may be identical to #1).
947 * bool have_level2_oplock
948 * bool have_no_oplock.
949 * Do internal consistency checks on the share mode for a file.
952 static void find_oplock_types(files_struct *fsp,
954 struct share_mode_lock *lck,
955 struct share_mode_entry **pp_batch,
956 struct share_mode_entry **pp_ex_or_batch,
963 *pp_ex_or_batch = NULL;
965 *got_no_oplock = false;
967 /* Ignore stat or internal opens, as is done in
968 delay_for_batch_oplocks() and
969 delay_for_exclusive_oplocks().
971 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
975 for (i=0; i<lck->num_share_modes; i++) {
976 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
980 if (lck->share_modes[i].op_type == NO_OPLOCK &&
981 is_stat_open(lck->share_modes[i].access_mask)) {
982 /* We ignore stat opens in the table - they
983 always have NO_OPLOCK and never get or
984 cause breaks. JRA. */
988 if (BATCH_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
989 /* batch - can only be one. */
990 if (*pp_ex_or_batch || *pp_batch || *got_level2 || *got_no_oplock) {
991 smb_panic("Bad batch oplock entry.");
993 *pp_batch = &lck->share_modes[i];
996 if (EXCLUSIVE_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
997 /* Exclusive or batch - can only be one. */
998 if (*pp_ex_or_batch || *got_level2 || *got_no_oplock) {
999 smb_panic("Bad exclusive or batch oplock entry.");
1001 *pp_ex_or_batch = &lck->share_modes[i];
1004 if (LEVEL_II_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
1005 if (*pp_batch || *pp_ex_or_batch) {
1006 smb_panic("Bad levelII oplock entry.");
1011 if (lck->share_modes[i].op_type == NO_OPLOCK) {
1012 if (*pp_batch || *pp_ex_or_batch) {
1013 smb_panic("Bad no oplock entry.");
1015 *got_no_oplock = true;
1020 static bool delay_for_batch_oplocks(files_struct *fsp,
1023 struct share_mode_entry *batch_entry)
1025 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1029 if (batch_entry != NULL) {
1030 /* Found a batch oplock */
1031 send_break_message(fsp, batch_entry, mid, oplock_request);
1037 static bool delay_for_exclusive_oplocks(files_struct *fsp,
1040 struct share_mode_entry *ex_entry)
1042 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1046 if (ex_entry != NULL) {
1047 /* Found an exclusive or batch oplock */
1048 bool delay_it = is_delete_request(fsp) ?
1049 BATCH_OPLOCK_TYPE(ex_entry->op_type) : true;
1051 send_break_message(fsp, ex_entry, mid, oplock_request);
1058 static void grant_fsp_oplock_type(files_struct *fsp,
1059 const struct byte_range_lock *br_lck,
1061 bool got_level2_oplock,
1062 bool got_a_none_oplock)
1064 bool allow_level2 = (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
1065 lp_level2_oplocks(SNUM(fsp->conn));
1067 /* Start by granting what the client asked for,
1068 but ensure no SAMBA_PRIVATE bits can be set. */
1069 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1071 if (oplock_request & INTERNAL_OPEN_ONLY) {
1072 /* No oplocks on internal open. */
1073 fsp->oplock_type = NO_OPLOCK;
1074 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1075 fsp->oplock_type, fsp_str_dbg(fsp)));
1077 } else if (br_lck && br_lck->num_locks > 0) {
1078 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1080 fsp->oplock_type = NO_OPLOCK;
1083 if (is_stat_open(fsp->access_mask)) {
1084 /* Leave the value already set. */
1085 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1086 fsp->oplock_type, fsp_str_dbg(fsp)));
1091 * Match what was requested (fsp->oplock_type) with
1092 * what was found in the existing share modes.
1095 if (got_a_none_oplock) {
1096 fsp->oplock_type = NO_OPLOCK;
1097 } else if (got_level2_oplock) {
1098 if (fsp->oplock_type == NO_OPLOCK ||
1099 fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
1100 /* Store a level2 oplock, but don't tell the client */
1101 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1103 fsp->oplock_type = LEVEL_II_OPLOCK;
1106 /* All share_mode_entries are placeholders or deferred.
1107 * Silently upgrade to fake levelII if the client didn't
1108 * ask for an oplock. */
1109 if (fsp->oplock_type == NO_OPLOCK) {
1110 /* Store a level2 oplock, but don't tell the client */
1111 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1116 * Don't grant level2 to clients that don't want them
1117 * or if we've turned them off.
1119 if (fsp->oplock_type == LEVEL_II_OPLOCK && !allow_level2) {
1120 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1123 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1124 fsp->oplock_type, fsp_str_dbg(fsp)));
1127 bool request_timed_out(struct timeval request_time,
1128 struct timeval timeout)
1130 struct timeval now, end_time;
1132 end_time = timeval_sum(&request_time, &timeout);
1133 return (timeval_compare(&end_time, &now) < 0);
1136 /****************************************************************************
1137 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1138 ****************************************************************************/
1140 static void defer_open(struct share_mode_lock *lck,
1141 struct timeval request_time,
1142 struct timeval timeout,
1143 struct smb_request *req,
1144 struct deferred_open_record *state)
1148 /* Paranoia check */
1150 for (i=0; i<lck->num_share_modes; i++) {
1151 struct share_mode_entry *e = &lck->share_modes[i];
1153 if (!is_deferred_open_entry(e)) {
1157 if (procid_is_me(&e->pid) && (e->op_mid == req->mid)) {
1158 DEBUG(0, ("Trying to defer an already deferred "
1159 "request: mid=%llu, exiting\n",
1160 (unsigned long long)req->mid));
1161 exit_server("attempt to defer a deferred request");
1165 /* End paranoia check */
1167 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1168 "open entry for mid %llu\n",
1169 (unsigned int)request_time.tv_sec,
1170 (unsigned int)request_time.tv_usec,
1171 (unsigned long long)req->mid));
1173 if (!push_deferred_open_message_smb(req, request_time, timeout,
1174 state->id, (char *)state, sizeof(*state))) {
1175 exit_server("push_deferred_open_message_smb failed");
1177 add_deferred_open(lck, req->mid, request_time,
1178 sconn_server_id(req->sconn), state->id);
1182 /****************************************************************************
1183 On overwrite open ensure that the attributes match.
1184 ****************************************************************************/
1186 bool open_match_attributes(connection_struct *conn,
1187 uint32 old_dos_attr,
1188 uint32 new_dos_attr,
1189 mode_t existing_unx_mode,
1190 mode_t new_unx_mode,
1191 mode_t *returned_unx_mode)
1193 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
1195 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1196 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1198 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
1199 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
1200 *returned_unx_mode = new_unx_mode;
1202 *returned_unx_mode = (mode_t)0;
1205 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1206 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1207 "returned_unx_mode = 0%o\n",
1208 (unsigned int)old_dos_attr,
1209 (unsigned int)existing_unx_mode,
1210 (unsigned int)new_dos_attr,
1211 (unsigned int)*returned_unx_mode ));
1213 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1214 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1215 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
1216 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
1220 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1221 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
1222 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
1229 /****************************************************************************
1230 Special FCB or DOS processing in the case of a sharing violation.
1231 Try and find a duplicated file handle.
1232 ****************************************************************************/
1234 NTSTATUS fcb_or_dos_open(struct smb_request *req,
1235 connection_struct *conn,
1236 files_struct *fsp_to_dup_into,
1237 const struct smb_filename *smb_fname,
1242 uint32 share_access,
1243 uint32 create_options)
1247 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1248 "file %s.\n", smb_fname_str_dbg(smb_fname)));
1250 for(fsp = file_find_di_first(conn->sconn, id); fsp;
1251 fsp = file_find_di_next(fsp)) {
1253 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1254 "vuid = %u, file_pid = %u, private_options = 0x%x "
1255 "access_mask = 0x%x\n", fsp_str_dbg(fsp),
1256 fsp->fh->fd, (unsigned int)fsp->vuid,
1257 (unsigned int)fsp->file_pid,
1258 (unsigned int)fsp->fh->private_options,
1259 (unsigned int)fsp->access_mask ));
1261 if (fsp->fh->fd != -1 &&
1262 fsp->vuid == vuid &&
1263 fsp->file_pid == file_pid &&
1264 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
1265 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
1266 (fsp->access_mask & FILE_WRITE_DATA) &&
1267 strequal(fsp->fsp_name->base_name, smb_fname->base_name) &&
1268 strequal(fsp->fsp_name->stream_name,
1269 smb_fname->stream_name)) {
1270 DEBUG(10,("fcb_or_dos_open: file match\n"));
1276 return NT_STATUS_NOT_FOUND;
1279 /* quite an insane set of semantics ... */
1280 if (is_executable(smb_fname->base_name) &&
1281 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1282 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1283 return NT_STATUS_INVALID_PARAMETER;
1286 /* We need to duplicate this fsp. */
1287 return dup_file_fsp(req, fsp, access_mask, share_access,
1288 create_options, fsp_to_dup_into);
1291 /****************************************************************************
1292 Open a file with a share mode - old openX method - map into NTCreate.
1293 ****************************************************************************/
1295 bool map_open_params_to_ntcreate(const struct smb_filename *smb_fname,
1296 int deny_mode, int open_func,
1297 uint32 *paccess_mask,
1298 uint32 *pshare_mode,
1299 uint32 *pcreate_disposition,
1300 uint32 *pcreate_options,
1301 uint32_t *pprivate_flags)
1305 uint32 create_disposition;
1306 uint32 create_options = FILE_NON_DIRECTORY_FILE;
1307 uint32_t private_flags = 0;
1309 DEBUG(10,("map_open_params_to_ntcreate: fname = %s, deny_mode = 0x%x, "
1310 "open_func = 0x%x\n",
1311 smb_fname_str_dbg(smb_fname), (unsigned int)deny_mode,
1312 (unsigned int)open_func ));
1314 /* Create the NT compatible access_mask. */
1315 switch (GET_OPENX_MODE(deny_mode)) {
1316 case DOS_OPEN_EXEC: /* Implies read-only - used to be FILE_READ_DATA */
1317 case DOS_OPEN_RDONLY:
1318 access_mask = FILE_GENERIC_READ;
1320 case DOS_OPEN_WRONLY:
1321 access_mask = FILE_GENERIC_WRITE;
1325 access_mask = FILE_GENERIC_READ|FILE_GENERIC_WRITE;
1328 DEBUG(10,("map_open_params_to_ntcreate: bad open mode = 0x%x\n",
1329 (unsigned int)GET_OPENX_MODE(deny_mode)));
1333 /* Create the NT compatible create_disposition. */
1334 switch (open_func) {
1335 case OPENX_FILE_EXISTS_FAIL|OPENX_FILE_CREATE_IF_NOT_EXIST:
1336 create_disposition = FILE_CREATE;
1339 case OPENX_FILE_EXISTS_OPEN:
1340 create_disposition = FILE_OPEN;
1343 case OPENX_FILE_EXISTS_OPEN|OPENX_FILE_CREATE_IF_NOT_EXIST:
1344 create_disposition = FILE_OPEN_IF;
1347 case OPENX_FILE_EXISTS_TRUNCATE:
1348 create_disposition = FILE_OVERWRITE;
1351 case OPENX_FILE_EXISTS_TRUNCATE|OPENX_FILE_CREATE_IF_NOT_EXIST:
1352 create_disposition = FILE_OVERWRITE_IF;
1356 /* From samba4 - to be confirmed. */
1357 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_EXEC) {
1358 create_disposition = FILE_CREATE;
1361 DEBUG(10,("map_open_params_to_ntcreate: bad "
1362 "open_func 0x%x\n", (unsigned int)open_func));
1366 /* Create the NT compatible share modes. */
1367 switch (GET_DENY_MODE(deny_mode)) {
1369 share_mode = FILE_SHARE_NONE;
1373 share_mode = FILE_SHARE_READ;
1377 share_mode = FILE_SHARE_WRITE;
1381 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1385 private_flags |= NTCREATEX_OPTIONS_PRIVATE_DENY_DOS;
1386 if (is_executable(smb_fname->base_name)) {
1387 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1389 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_RDONLY) {
1390 share_mode = FILE_SHARE_READ;
1392 share_mode = FILE_SHARE_NONE;
1398 private_flags |= NTCREATEX_OPTIONS_PRIVATE_DENY_FCB;
1399 share_mode = FILE_SHARE_NONE;
1403 DEBUG(10,("map_open_params_to_ntcreate: bad deny_mode 0x%x\n",
1404 (unsigned int)GET_DENY_MODE(deny_mode) ));
1408 DEBUG(10,("map_open_params_to_ntcreate: file %s, access_mask = 0x%x, "
1409 "share_mode = 0x%x, create_disposition = 0x%x, "
1410 "create_options = 0x%x private_flags = 0x%x\n",
1411 smb_fname_str_dbg(smb_fname),
1412 (unsigned int)access_mask,
1413 (unsigned int)share_mode,
1414 (unsigned int)create_disposition,
1415 (unsigned int)create_options,
1416 (unsigned int)private_flags));
1419 *paccess_mask = access_mask;
1422 *pshare_mode = share_mode;
1424 if (pcreate_disposition) {
1425 *pcreate_disposition = create_disposition;
1427 if (pcreate_options) {
1428 *pcreate_options = create_options;
1430 if (pprivate_flags) {
1431 *pprivate_flags = private_flags;
1438 static void schedule_defer_open(struct share_mode_lock *lck,
1439 struct timeval request_time,
1440 struct smb_request *req)
1442 struct deferred_open_record state;
1444 /* This is a relative time, added to the absolute
1445 request_time value to get the absolute timeout time.
1446 Note that if this is the second or greater time we enter
1447 this codepath for this particular request mid then
1448 request_time is left as the absolute time of the *first*
1449 time this request mid was processed. This is what allows
1450 the request to eventually time out. */
1452 struct timeval timeout;
1454 /* Normally the smbd we asked should respond within
1455 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1456 * the client did, give twice the timeout as a safety
1457 * measure here in case the other smbd is stuck
1458 * somewhere else. */
1460 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1462 /* Nothing actually uses state.delayed_for_oplocks
1463 but it's handy to differentiate in debug messages
1464 between a 30 second delay due to oplock break, and
1465 a 1 second delay for share mode conflicts. */
1467 state.delayed_for_oplocks = True;
1470 if (!request_timed_out(request_time, timeout)) {
1471 defer_open(lck, request_time, timeout, req, &state);
1475 /****************************************************************************
1476 Work out what access_mask to use from what the client sent us.
1477 ****************************************************************************/
1479 static NTSTATUS calculate_access_mask(connection_struct *conn,
1480 const struct smb_filename *smb_fname,
1482 uint32_t access_mask,
1483 uint32_t *access_mask_out)
1488 * Convert GENERIC bits to specific bits.
1491 se_map_generic(&access_mask, &file_generic_mapping);
1493 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1494 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1497 struct security_descriptor *sd;
1498 uint32_t access_granted = 0;
1500 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
1505 if (!NT_STATUS_IS_OK(status)) {
1506 DEBUG(10, ("calculate_access_mask: Could not get acl "
1508 smb_fname_str_dbg(smb_fname),
1509 nt_errstr(status)));
1510 return NT_STATUS_ACCESS_DENIED;
1513 status = smb1_file_se_access_check(conn,
1515 get_current_nttok(conn),
1521 if (!NT_STATUS_IS_OK(status)) {
1522 DEBUG(10, ("calculate_access_mask: Access denied on "
1523 "file %s: when calculating maximum access\n",
1524 smb_fname_str_dbg(smb_fname)));
1525 return NT_STATUS_ACCESS_DENIED;
1528 access_mask = access_granted;
1530 access_mask = FILE_GENERIC_ALL;
1534 *access_mask_out = access_mask;
1535 return NT_STATUS_OK;
1538 /****************************************************************************
1539 Remove the deferred open entry under lock.
1540 ****************************************************************************/
1542 void remove_deferred_open_entry(struct file_id id, uint64_t mid,
1543 struct server_id pid)
1545 struct share_mode_lock *lck = get_share_mode_lock(talloc_tos(), id,
1548 DEBUG(0, ("could not get share mode lock\n"));
1550 del_deferred_open_entry(lck, mid, pid);
1555 /****************************************************************
1556 Ensure we get the brlock lock followed by the share mode lock
1557 in the correct order to prevent deadlocks if other smbd's are
1558 using the brlock database on this file simultaneously with this open
1559 (that code also gets the locks in brlock -> share mode lock order).
1560 ****************************************************************/
1562 static bool acquire_ordered_locks(TALLOC_CTX *mem_ctx,
1564 const struct file_id id,
1565 const char *connectpath,
1566 const struct smb_filename *smb_fname,
1567 const struct timespec *p_old_write_time,
1568 struct share_mode_lock **p_lck,
1569 struct byte_range_lock **p_br_lck)
1571 /* Ordering - we must get the br_lck for this
1572 file before the share mode. */
1573 if (lp_locking(fsp->conn->params)) {
1574 *p_br_lck = brl_get_locks_readonly(fsp);
1575 if (*p_br_lck == NULL) {
1576 DEBUG(0, ("Could not get br_lock\n"));
1579 /* Note - we don't need to free the returned
1580 br_lck explicitly as it was allocated on talloc_tos()
1581 and so will be autofreed (and release the lock)
1582 once the frame context disappears.
1584 If it was set to fsp->brlock_rec then it was
1585 talloc_move'd to hang off the fsp pointer and
1586 in this case is guarenteed to not be holding the
1587 lock on the brlock database. */
1590 *p_lck = get_share_mode_lock(mem_ctx,
1596 if (*p_lck == NULL) {
1597 DEBUG(0, ("Could not get share mode lock\n"));
1598 TALLOC_FREE(*p_br_lck);
1604 /****************************************************************************
1605 Open a file with a share mode. Passed in an already created files_struct *.
1606 ****************************************************************************/
1608 static NTSTATUS open_file_ntcreate(connection_struct *conn,
1609 struct smb_request *req,
1610 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1611 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1612 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1613 uint32 create_options, /* options such as delete on close. */
1614 uint32 new_dos_attributes, /* attributes used for new file. */
1615 int oplock_request, /* internal Samba oplock codes. */
1616 /* Information (FILE_EXISTS etc.) */
1617 uint32_t private_flags, /* Samba specific flags. */
1621 struct smb_filename *smb_fname = fsp->fsp_name;
1624 bool file_existed = VALID_STAT(smb_fname->st);
1625 bool def_acl = False;
1626 bool posix_open = False;
1627 bool new_file_created = False;
1628 bool clear_ads = false;
1630 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1631 mode_t new_unx_mode = (mode_t)0;
1632 mode_t unx_mode = (mode_t)0;
1634 uint32 existing_dos_attributes = 0;
1635 struct timeval request_time = timeval_zero();
1636 struct share_mode_lock *lck = NULL;
1637 uint32 open_access_mask = access_mask;
1643 /* Windows allows a new file to be created and
1644 silently removes a FILE_ATTRIBUTE_DIRECTORY
1645 sent by the client. Do the same. */
1647 new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
1649 if (conn->printer) {
1651 * Printers are handled completely differently.
1652 * Most of the passed parameters are ignored.
1656 *pinfo = FILE_WAS_CREATED;
1659 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1660 smb_fname_str_dbg(smb_fname)));
1663 DEBUG(0,("open_file_ntcreate: printer open without "
1664 "an SMB request!\n"));
1665 return NT_STATUS_INTERNAL_ERROR;
1668 return print_spool_open(fsp, smb_fname->base_name,
1672 if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
1674 return NT_STATUS_NO_MEMORY;
1677 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1679 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1680 new_dos_attributes = 0;
1682 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
1684 unx_mode = unix_mode(conn, new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
1685 smb_fname, parent_dir);
1688 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1689 "access_mask=0x%x share_access=0x%x "
1690 "create_disposition = 0x%x create_options=0x%x "
1691 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
1692 smb_fname_str_dbg(smb_fname), new_dos_attributes,
1693 access_mask, share_access, create_disposition,
1694 create_options, (unsigned int)unx_mode, oplock_request,
1695 (unsigned int)private_flags));
1697 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
1698 DEBUG(0, ("No smb request but not an internal only open!\n"));
1699 return NT_STATUS_INTERNAL_ERROR;
1703 * Only non-internal opens can be deferred at all
1708 if (get_deferred_open_message_state(req,
1712 struct deferred_open_record *state = (struct deferred_open_record *)ptr;
1713 /* Remember the absolute time of the original
1714 request with this mid. We'll use it later to
1715 see if this has timed out. */
1717 /* Remove the deferred open entry under lock. */
1718 remove_deferred_open_entry(
1719 state->id, req->mid,
1720 sconn_server_id(req->sconn));
1722 /* Ensure we don't reprocess this message. */
1723 remove_deferred_open_message_smb(req->mid);
1727 status = check_name(conn, smb_fname->base_name);
1728 if (!NT_STATUS_IS_OK(status)) {
1733 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
1735 existing_dos_attributes = dos_mode(conn, smb_fname);
1739 /* ignore any oplock requests if oplocks are disabled */
1740 if (!lp_oplocks(SNUM(conn)) ||
1741 IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
1742 /* Mask off everything except the private Samba bits. */
1743 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
1746 /* this is for OS/2 long file names - say we don't support them */
1747 if (!lp_posix_pathnames() && strstr(smb_fname->base_name,".+,;=[].")) {
1748 /* OS/2 Workplace shell fix may be main code stream in a later
1750 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1752 if (use_nt_status()) {
1753 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1755 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
1758 switch( create_disposition ) {
1760 * Currently we're using FILE_SUPERSEDE as the same as
1761 * FILE_OVERWRITE_IF but they really are
1762 * different. FILE_SUPERSEDE deletes an existing file
1763 * (requiring delete access) then recreates it.
1765 case FILE_SUPERSEDE:
1766 /* If file exists replace/overwrite. If file doesn't
1768 flags2 |= (O_CREAT | O_TRUNC);
1772 case FILE_OVERWRITE_IF:
1773 /* If file exists replace/overwrite. If file doesn't
1775 flags2 |= (O_CREAT | O_TRUNC);
1780 /* If file exists open. If file doesn't exist error. */
1781 if (!file_existed) {
1782 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1783 "requested for file %s and file "
1785 smb_fname_str_dbg(smb_fname)));
1787 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1791 case FILE_OVERWRITE:
1792 /* If file exists overwrite. If file doesn't exist
1794 if (!file_existed) {
1795 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1796 "requested for file %s and file "
1798 smb_fname_str_dbg(smb_fname) ));
1800 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1807 /* If file exists error. If file doesn't exist
1810 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1811 "requested for file %s and file "
1812 "already exists.\n",
1813 smb_fname_str_dbg(smb_fname)));
1814 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
1819 return map_nt_error_from_unix(errno);
1821 flags2 |= (O_CREAT|O_EXCL);
1825 /* If file exists open. If file doesn't exist
1831 return NT_STATUS_INVALID_PARAMETER;
1834 /* We only care about matching attributes on file exists and
1837 if (!posix_open && file_existed && ((create_disposition == FILE_OVERWRITE) ||
1838 (create_disposition == FILE_OVERWRITE_IF))) {
1839 if (!open_match_attributes(conn, existing_dos_attributes,
1841 smb_fname->st.st_ex_mode,
1842 unx_mode, &new_unx_mode)) {
1843 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1844 "for file %s (%x %x) (0%o, 0%o)\n",
1845 smb_fname_str_dbg(smb_fname),
1846 existing_dos_attributes,
1848 (unsigned int)smb_fname->st.st_ex_mode,
1849 (unsigned int)unx_mode ));
1851 return NT_STATUS_ACCESS_DENIED;
1855 status = calculate_access_mask(conn, smb_fname, file_existed,
1858 if (!NT_STATUS_IS_OK(status)) {
1859 DEBUG(10, ("open_file_ntcreate: calculate_access_mask "
1860 "on file %s returned %s\n",
1861 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
1865 open_access_mask = access_mask;
1867 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1868 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
1871 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1872 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
1876 * Note that we ignore the append flag as append does not
1877 * mean the same thing under DOS and Unix.
1880 if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
1881 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1882 /* DENY_DOS opens are always underlying read-write on the
1883 file handle, no matter what the requested access mask
1885 if ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
1886 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|FILE_READ_EA|FILE_EXECUTE)) {
1896 * Currently we only look at FILE_WRITE_THROUGH for create options.
1900 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
1905 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
1909 if (!posix_open && !CAN_WRITE(conn)) {
1911 * We should really return a permission denied error if either
1912 * O_CREAT or O_TRUNC are set, but for compatibility with
1913 * older versions of Samba we just AND them out.
1915 flags2 &= ~(O_CREAT|O_TRUNC);
1919 * Ensure we can't write on a read-only share or file.
1922 if (flags != O_RDONLY && file_existed &&
1923 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
1924 DEBUG(5,("open_file_ntcreate: write access requested for "
1925 "file %s on read only %s\n",
1926 smb_fname_str_dbg(smb_fname),
1927 !CAN_WRITE(conn) ? "share" : "file" ));
1929 return NT_STATUS_ACCESS_DENIED;
1932 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1933 fsp->share_access = share_access;
1934 fsp->fh->private_options = private_flags;
1935 fsp->access_mask = open_access_mask; /* We change this to the
1936 * requested access_mask after
1937 * the open is done. */
1938 fsp->posix_open = posix_open;
1940 /* Ensure no SAMBA_PRIVATE bits can be set. */
1941 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1943 if (timeval_is_zero(&request_time)) {
1944 request_time = fsp->open_time;
1948 struct byte_range_lock *br_lck = NULL;
1949 struct share_mode_entry *batch_entry = NULL;
1950 struct share_mode_entry *exclusive_entry = NULL;
1951 bool got_level2_oplock = false;
1952 bool got_a_none_oplock = false;
1954 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
1955 id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1957 if (!acquire_ordered_locks(talloc_tos(),
1965 return NT_STATUS_SHARING_VIOLATION;
1968 /* Get the types we need to examine. */
1969 find_oplock_types(fsp,
1975 &got_a_none_oplock);
1977 /* First pass - send break only on batch oplocks. */
1978 if ((req != NULL) &&
1979 delay_for_batch_oplocks(fsp,
1983 schedule_defer_open(lck, request_time, req);
1985 return NT_STATUS_SHARING_VIOLATION;
1988 /* Use the client requested access mask here, not the one we
1990 status = open_mode_check(conn, lck, fsp->name_hash,
1991 access_mask, share_access,
1992 create_options, &file_existed);
1994 if (NT_STATUS_IS_OK(status)) {
1995 /* We might be going to allow this open. Check oplock
1997 /* Second pass - send break for both batch or
1998 * exclusive oplocks. */
1999 if ((req != NULL) &&
2000 delay_for_exclusive_oplocks(
2005 schedule_defer_open(lck, request_time, req);
2007 return NT_STATUS_SHARING_VIOLATION;
2011 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
2012 /* DELETE_PENDING is not deferred for a second */
2017 grant_fsp_oplock_type(fsp,
2023 if (!NT_STATUS_IS_OK(status)) {
2024 uint32 can_access_mask;
2025 bool can_access = True;
2027 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
2029 /* Check if this can be done with the deny_dos and fcb
2032 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
2033 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
2035 DEBUG(0, ("DOS open without an SMB "
2038 return NT_STATUS_INTERNAL_ERROR;
2041 /* Use the client requested access mask here,
2042 * not the one we open with. */
2043 status = fcb_or_dos_open(req,
2054 if (NT_STATUS_IS_OK(status)) {
2057 *pinfo = FILE_WAS_OPENED;
2059 return NT_STATUS_OK;
2064 * This next line is a subtlety we need for
2065 * MS-Access. If a file open will fail due to share
2066 * permissions and also for security (access) reasons,
2067 * we need to return the access failed error, not the
2068 * share error. We can't open the file due to kernel
2069 * oplock deadlock (it's possible we failed above on
2070 * the open_mode_check()) so use a userspace check.
2073 if (flags & O_RDWR) {
2074 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
2075 } else if (flags & O_WRONLY) {
2076 can_access_mask = FILE_WRITE_DATA;
2078 can_access_mask = FILE_READ_DATA;
2081 if (((can_access_mask & FILE_WRITE_DATA) &&
2082 !CAN_WRITE(conn)) ||
2083 !can_access_file_data(conn, smb_fname,
2089 * If we're returning a share violation, ensure we
2090 * cope with the braindead 1 second delay.
2093 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
2094 lp_defer_sharing_violations()) {
2095 struct timeval timeout;
2096 struct deferred_open_record state;
2099 /* this is a hack to speed up torture tests
2101 timeout_usecs = lp_parm_int(SNUM(conn),
2102 "smbd","sharedelay",
2103 SHARING_VIOLATION_USEC_WAIT);
2105 /* This is a relative time, added to the absolute
2106 request_time value to get the absolute timeout time.
2107 Note that if this is the second or greater time we enter
2108 this codepath for this particular request mid then
2109 request_time is left as the absolute time of the *first*
2110 time this request mid was processed. This is what allows
2111 the request to eventually time out. */
2113 timeout = timeval_set(0, timeout_usecs);
2115 /* Nothing actually uses state.delayed_for_oplocks
2116 but it's handy to differentiate in debug messages
2117 between a 30 second delay due to oplock break, and
2118 a 1 second delay for share mode conflicts. */
2120 state.delayed_for_oplocks = False;
2124 && !request_timed_out(request_time,
2126 defer_open(lck, request_time, timeout,
2134 * We have detected a sharing violation here
2135 * so return the correct error code
2137 status = NT_STATUS_SHARING_VIOLATION;
2139 status = NT_STATUS_ACCESS_DENIED;
2145 * We exit this block with the share entry *locked*.....
2149 SMB_ASSERT(!file_existed || (lck != NULL));
2152 * Ensure we pay attention to default ACLs on directories if required.
2155 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
2156 (def_acl = directory_has_default_acl(conn, parent_dir))) {
2157 unx_mode = (0777 & lp_create_mask(SNUM(conn)));
2160 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2161 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2162 (unsigned int)flags, (unsigned int)flags2,
2163 (unsigned int)unx_mode, (unsigned int)access_mask,
2164 (unsigned int)open_access_mask));
2167 * open_file strips any O_TRUNC flags itself.
2170 fsp_open = open_file(fsp, conn, req, parent_dir,
2171 flags|flags2, unx_mode, access_mask,
2174 if (!NT_STATUS_IS_OK(fsp_open)) {
2181 if (!file_existed) {
2182 struct byte_range_lock *br_lck = NULL;
2183 struct share_mode_entry *batch_entry = NULL;
2184 struct share_mode_entry *exclusive_entry = NULL;
2185 bool got_level2_oplock = false;
2186 bool got_a_none_oplock = false;
2187 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
2189 * Deal with the race condition where two smbd's detect the
2190 * file doesn't exist and do the create at the same time. One
2191 * of them will win and set a share mode, the other (ie. this
2192 * one) should check if the requested share mode for this
2193 * create is allowed.
2197 * Now the file exists and fsp is successfully opened,
2198 * fsp->dev and fsp->inode are valid and should replace the
2199 * dev=0,inode=0 from a non existent file. Spotted by
2200 * Nadav Danieli <nadavd@exanet.com>. JRA.
2205 if (!acquire_ordered_locks(talloc_tos(),
2213 return NT_STATUS_SHARING_VIOLATION;
2216 /* Get the types we need to examine. */
2217 find_oplock_types(fsp,
2223 &got_a_none_oplock);
2225 /* First pass - send break only on batch oplocks. */
2226 if ((req != NULL) &&
2227 delay_for_batch_oplocks(fsp,
2231 schedule_defer_open(lck, request_time, req);
2234 return NT_STATUS_SHARING_VIOLATION;
2237 status = open_mode_check(conn, lck, fsp->name_hash,
2238 access_mask, share_access,
2239 create_options, &file_existed);
2241 if (NT_STATUS_IS_OK(status)) {
2242 /* We might be going to allow this open. Check oplock
2244 /* Second pass - send break for both batch or
2245 * exclusive oplocks. */
2246 if ((req != NULL) &&
2247 delay_for_exclusive_oplocks(
2252 schedule_defer_open(lck, request_time, req);
2255 return NT_STATUS_SHARING_VIOLATION;
2259 if (!NT_STATUS_IS_OK(status)) {
2260 struct deferred_open_record state;
2264 state.delayed_for_oplocks = False;
2267 /* Do it all over again immediately. In the second
2268 * round we will find that the file existed and handle
2269 * the DELETE_PENDING and FCB cases correctly. No need
2270 * to duplicate the code here. Essentially this is a
2271 * "goto top of this function", but don't tell
2275 defer_open(lck, request_time, timeval_zero(),
2282 grant_fsp_oplock_type(fsp,
2289 * We exit this block with the share entry *locked*.....
2294 SMB_ASSERT(lck != NULL);
2296 /* Delete streams if create_disposition requires it */
2297 if (file_existed && clear_ads &&
2298 !is_ntfs_stream_smb_fname(smb_fname)) {
2299 status = delete_all_streams(conn, smb_fname->base_name);
2300 if (!NT_STATUS_IS_OK(status)) {
2307 /* note that we ignore failure for the following. It is
2308 basically a hack for NFS, and NFS will never set one of
2309 these only read them. Nobody but Samba can ever set a deny
2310 mode and we have already checked our more authoritative
2311 locking database for permission to set this deny mode. If
2312 the kernel refuses the operations then the kernel is wrong.
2313 note that GPFS supports it as well - jmcd */
2315 if (fsp->fh->fd != -1) {
2317 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
2318 if(ret_flock == -1 ){
2323 return NT_STATUS_SHARING_VIOLATION;
2328 * At this point onwards, we can guarentee that the share entry
2329 * is locked, whether we created the file or not, and that the
2330 * deny mode is compatible with all current opens.
2334 * If requested, truncate the file.
2337 if (file_existed && (flags2&O_TRUNC)) {
2339 * We are modifing the file after open - update the stat
2342 if ((SMB_VFS_FTRUNCATE(fsp, 0) == -1) ||
2343 (SMB_VFS_FSTAT(fsp, &smb_fname->st)==-1)) {
2344 status = map_nt_error_from_unix(errno);
2352 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2353 * but we don't have to store this - just ignore it on access check.
2355 if (conn->sconn->using_smb2) {
2357 * SMB2 doesn't return it (according to Microsoft tests).
2358 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2359 * File created with access = 0x7 (Read, Write, Delete)
2360 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2362 fsp->access_mask = access_mask;
2364 /* But SMB1 does. */
2365 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2369 /* stat opens on existing files don't get oplocks. */
2370 if (is_stat_open(open_access_mask)) {
2371 fsp->oplock_type = NO_OPLOCK;
2374 if (!(flags2 & O_TRUNC)) {
2375 info = FILE_WAS_OPENED;
2377 info = FILE_WAS_OVERWRITTEN;
2380 info = FILE_WAS_CREATED;
2388 * Setup the oplock info in both the shared memory and
2392 if (!set_file_oplock(fsp, fsp->oplock_type)) {
2394 * Could not get the kernel oplock or there are byte-range
2395 * locks on the file.
2397 fsp->oplock_type = NO_OPLOCK;
2400 if (info == FILE_WAS_OVERWRITTEN || info == FILE_WAS_CREATED || info == FILE_WAS_SUPERSEDED) {
2401 new_file_created = True;
2404 set_share_mode(lck, fsp, get_current_uid(conn),
2408 /* Handle strange delete on close create semantics. */
2409 if (create_options & FILE_DELETE_ON_CLOSE) {
2411 status = can_set_delete_on_close(fsp, new_dos_attributes);
2413 if (!NT_STATUS_IS_OK(status)) {
2414 /* Remember to delete the mode we just added. */
2415 del_share_mode(lck, fsp);
2420 /* Note that here we set the *inital* delete on close flag,
2421 not the regular one. The magic gets handled in close. */
2422 fsp->initial_delete_on_close = True;
2425 if (new_file_created) {
2426 /* Files should be initially set as archive */
2427 if (lp_map_archive(SNUM(conn)) ||
2428 lp_store_dos_attributes(SNUM(conn))) {
2430 if (file_set_dosmode(conn, smb_fname,
2431 new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2432 parent_dir, true) == 0) {
2433 unx_mode = smb_fname->st.st_ex_mode;
2439 /* Determine sparse flag. */
2441 /* POSIX opens are sparse by default. */
2442 fsp->is_sparse = true;
2444 fsp->is_sparse = (file_existed &&
2445 (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE));
2449 * Take care of inherited ACLs on created files - if default ACL not
2453 if (!posix_open && !file_existed && !def_acl) {
2455 int saved_errno = errno; /* We might get ENOSYS in the next
2458 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2460 errno = saved_errno; /* Ignore ENOSYS */
2463 } else if (new_unx_mode) {
2467 /* Attributes need changing. File already existed. */
2470 int saved_errno = errno; /* We might get ENOSYS in the
2472 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2474 if (ret == -1 && errno == ENOSYS) {
2475 errno = saved_errno; /* Ignore ENOSYS */
2477 DEBUG(5, ("open_file_ntcreate: reset "
2478 "attributes of file %s to 0%o\n",
2479 smb_fname_str_dbg(smb_fname),
2480 (unsigned int)new_unx_mode));
2481 ret = 0; /* Don't do the fchmod below. */
2486 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2487 DEBUG(5, ("open_file_ntcreate: failed to reset "
2488 "attributes of file %s to 0%o\n",
2489 smb_fname_str_dbg(smb_fname),
2490 (unsigned int)new_unx_mode));
2493 /* If this is a successful open, we must remove any deferred open
2496 del_deferred_open_entry(lck, req->mid,
2497 sconn_server_id(req->sconn));
2501 return NT_STATUS_OK;
2505 /****************************************************************************
2506 Open a file for for write to ensure that we can fchmod it.
2507 ****************************************************************************/
2509 NTSTATUS open_file_fchmod(connection_struct *conn,
2510 struct smb_filename *smb_fname,
2511 files_struct **result)
2513 if (!VALID_STAT(smb_fname->st)) {
2514 return NT_STATUS_INVALID_PARAMETER;
2517 return SMB_VFS_CREATE_FILE(
2520 0, /* root_dir_fid */
2521 smb_fname, /* fname */
2522 FILE_WRITE_DATA, /* access_mask */
2523 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2525 FILE_OPEN, /* create_disposition*/
2526 0, /* create_options */
2527 0, /* file_attributes */
2528 INTERNAL_OPEN_ONLY, /* oplock_request */
2529 0, /* allocation_size */
2530 0, /* private_flags */
2533 result, /* result */
2537 static NTSTATUS mkdir_internal(connection_struct *conn,
2538 struct smb_filename *smb_dname,
2539 uint32 file_attributes)
2544 bool posix_open = false;
2546 if(!CAN_WRITE(conn)) {
2547 DEBUG(5,("mkdir_internal: failing create on read-only share "
2548 "%s\n", lp_servicename(SNUM(conn))));
2549 return NT_STATUS_ACCESS_DENIED;
2552 status = check_name(conn, smb_dname->base_name);
2553 if (!NT_STATUS_IS_OK(status)) {
2557 if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
2559 return NT_STATUS_NO_MEMORY;
2562 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2564 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2566 mode = unix_mode(conn, FILE_ATTRIBUTE_DIRECTORY, smb_dname, parent_dir);
2569 if (SMB_VFS_MKDIR(conn, smb_dname->base_name, mode) != 0) {
2570 return map_nt_error_from_unix(errno);
2573 /* Ensure we're checking for a symlink here.... */
2574 /* We don't want to get caught by a symlink racer. */
2576 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2577 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2578 smb_fname_str_dbg(smb_dname), strerror(errno)));
2579 return map_nt_error_from_unix(errno);
2582 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
2583 DEBUG(0, ("Directory just '%s' created is not a directory\n",
2584 smb_fname_str_dbg(smb_dname)));
2585 return NT_STATUS_ACCESS_DENIED;
2588 if (lp_store_dos_attributes(SNUM(conn))) {
2590 file_set_dosmode(conn, smb_dname,
2591 file_attributes | FILE_ATTRIBUTE_DIRECTORY,
2596 if (lp_inherit_perms(SNUM(conn))) {
2597 inherit_access_posix_acl(conn, parent_dir,
2598 smb_dname->base_name, mode);
2603 * Check if high bits should have been set,
2604 * then (if bits are missing): add them.
2605 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2608 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
2609 (mode & ~smb_dname->st.st_ex_mode)) {
2610 SMB_VFS_CHMOD(conn, smb_dname->base_name,
2611 (smb_dname->st.st_ex_mode |
2612 (mode & ~smb_dname->st.st_ex_mode)));
2616 /* Change the owner if required. */
2617 if (lp_inherit_owner(SNUM(conn))) {
2618 change_dir_owner_to_parent(conn, parent_dir,
2619 smb_dname->base_name,
2623 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2624 smb_dname->base_name);
2626 return NT_STATUS_OK;
2629 /****************************************************************************
2630 Ensure we didn't get symlink raced on opening a directory.
2631 ****************************************************************************/
2633 bool check_same_stat(const SMB_STRUCT_STAT *sbuf1,
2634 const SMB_STRUCT_STAT *sbuf2)
2636 if (sbuf1->st_ex_uid != sbuf2->st_ex_uid ||
2637 sbuf1->st_ex_gid != sbuf2->st_ex_gid ||
2638 sbuf1->st_ex_dev != sbuf2->st_ex_dev ||
2639 sbuf1->st_ex_ino != sbuf2->st_ex_ino) {
2645 /****************************************************************************
2646 Open a directory from an NT SMB call.
2647 ****************************************************************************/
2649 static NTSTATUS open_directory(connection_struct *conn,
2650 struct smb_request *req,
2651 struct smb_filename *smb_dname,
2653 uint32 share_access,
2654 uint32 create_disposition,
2655 uint32 create_options,
2656 uint32 file_attributes,
2658 files_struct **result)
2660 files_struct *fsp = NULL;
2661 bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
2662 struct share_mode_lock *lck = NULL;
2664 struct timespec mtimespec;
2667 SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname));
2669 /* Ensure we have a directory attribute. */
2670 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
2672 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2673 "share_access = 0x%x create_options = 0x%x, "
2674 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2675 smb_fname_str_dbg(smb_dname),
2676 (unsigned int)access_mask,
2677 (unsigned int)share_access,
2678 (unsigned int)create_options,
2679 (unsigned int)create_disposition,
2680 (unsigned int)file_attributes));
2682 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
2683 (conn->fs_capabilities & FILE_NAMED_STREAMS) &&
2684 is_ntfs_stream_smb_fname(smb_dname)) {
2685 DEBUG(2, ("open_directory: %s is a stream name!\n",
2686 smb_fname_str_dbg(smb_dname)));
2687 return NT_STATUS_NOT_A_DIRECTORY;
2690 status = calculate_access_mask(conn, smb_dname, dir_existed,
2691 access_mask, &access_mask);
2692 if (!NT_STATUS_IS_OK(status)) {
2693 DEBUG(10, ("open_directory: calculate_access_mask "
2694 "on file %s returned %s\n",
2695 smb_fname_str_dbg(smb_dname),
2696 nt_errstr(status)));
2700 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
2701 !security_token_has_privilege(get_current_nttok(conn),
2702 SEC_PRIV_SECURITY)) {
2703 DEBUG(10, ("open_directory: open on %s "
2704 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2705 smb_fname_str_dbg(smb_dname)));
2706 return NT_STATUS_PRIVILEGE_NOT_HELD;
2709 switch( create_disposition ) {
2713 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2716 info = FILE_WAS_OPENED;
2721 /* If directory exists error. If directory doesn't
2724 status = mkdir_internal(conn, smb_dname,
2727 if (!NT_STATUS_IS_OK(status)) {
2728 DEBUG(2, ("open_directory: unable to create "
2729 "%s. Error was %s\n",
2730 smb_fname_str_dbg(smb_dname),
2731 nt_errstr(status)));
2735 info = FILE_WAS_CREATED;
2740 * If directory exists open. If directory doesn't
2744 status = mkdir_internal(conn, smb_dname,
2747 if (NT_STATUS_IS_OK(status)) {
2748 info = FILE_WAS_CREATED;
2751 if (NT_STATUS_EQUAL(status,
2752 NT_STATUS_OBJECT_NAME_COLLISION)) {
2753 info = FILE_WAS_OPENED;
2754 status = NT_STATUS_OK;
2758 case FILE_SUPERSEDE:
2759 case FILE_OVERWRITE:
2760 case FILE_OVERWRITE_IF:
2762 DEBUG(5,("open_directory: invalid create_disposition "
2763 "0x%x for directory %s\n",
2764 (unsigned int)create_disposition,
2765 smb_fname_str_dbg(smb_dname)));
2766 return NT_STATUS_INVALID_PARAMETER;
2769 if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
2770 DEBUG(5,("open_directory: %s is not a directory !\n",
2771 smb_fname_str_dbg(smb_dname)));
2772 return NT_STATUS_NOT_A_DIRECTORY;
2775 if (info == FILE_WAS_OPENED) {
2776 uint32_t access_granted = 0;
2777 status = smbd_check_open_rights(conn, smb_dname, access_mask,
2780 /* Were we trying to do a directory open
2781 * for delete and didn't get DELETE
2782 * access (only) ? Check if the
2783 * directory allows DELETE_CHILD.
2785 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
2788 if ((NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) &&
2789 (access_mask & DELETE_ACCESS) &&
2790 (access_granted == DELETE_ACCESS) &&
2791 can_delete_file_in_directory(conn, smb_dname))) {
2792 DEBUG(10,("open_directory: overrode ACCESS_DENIED "
2793 "on directory %s\n",
2794 smb_fname_str_dbg(smb_dname)));
2795 status = NT_STATUS_OK;
2798 if (!NT_STATUS_IS_OK(status)) {
2799 DEBUG(10, ("open_directory: smbd_check_open_rights on "
2800 "file %s failed with %s\n",
2801 smb_fname_str_dbg(smb_dname),
2802 nt_errstr(status)));
2807 status = file_new(req, conn, &fsp);
2808 if(!NT_STATUS_IS_OK(status)) {
2813 * Setup the files_struct for it.
2816 fsp->mode = smb_dname->st.st_ex_mode;
2817 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
2818 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
2819 fsp->file_pid = req ? req->smbpid : 0;
2820 fsp->can_lock = False;
2821 fsp->can_read = False;
2822 fsp->can_write = False;
2824 fsp->share_access = share_access;
2825 fsp->fh->private_options = 0;
2827 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2829 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2830 fsp->print_file = NULL;
2831 fsp->modified = False;
2832 fsp->oplock_type = NO_OPLOCK;
2833 fsp->sent_oplock_break = NO_BREAK_SENT;
2834 fsp->is_directory = True;
2835 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
2836 status = fsp_set_smb_fname(fsp, smb_dname);
2837 if (!NT_STATUS_IS_OK(status)) {
2838 file_free(req, fsp);
2842 mtimespec = smb_dname->st.st_ex_mtime;
2845 status = fd_open(conn, fsp, O_RDONLY|O_DIRECTORY, 0);
2847 /* POSIX allows us to open a directory with O_RDONLY. */
2848 status = fd_open(conn, fsp, O_RDONLY, 0);
2850 if (!NT_STATUS_IS_OK(status)) {
2851 DEBUG(5, ("open_directory: Could not open fd for "
2853 smb_fname_str_dbg(smb_dname),
2854 nt_errstr(status)));
2855 file_free(req, fsp);
2859 status = vfs_stat_fsp(fsp);
2860 if (!NT_STATUS_IS_OK(status)) {
2862 file_free(req, fsp);
2866 /* Ensure there was no race condition. */
2867 if (!check_same_stat(&smb_dname->st, &fsp->fsp_name->st)) {
2868 DEBUG(5,("open_directory: stat struct differs for "
2870 smb_fname_str_dbg(smb_dname)));
2872 file_free(req, fsp);
2873 return NT_STATUS_ACCESS_DENIED;
2876 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
2877 conn->connectpath, smb_dname, &mtimespec);
2880 DEBUG(0, ("open_directory: Could not get share mode lock for "
2881 "%s\n", smb_fname_str_dbg(smb_dname)));
2883 file_free(req, fsp);
2884 return NT_STATUS_SHARING_VIOLATION;
2887 status = open_mode_check(conn, lck, fsp->name_hash,
2888 access_mask, share_access,
2889 create_options, &dir_existed);
2891 if (!NT_STATUS_IS_OK(status)) {
2894 file_free(req, fsp);
2898 set_share_mode(lck, fsp, get_current_uid(conn),
2899 req ? req->mid : 0, NO_OPLOCK);
2901 /* For directories the delete on close bit at open time seems
2902 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2903 if (create_options & FILE_DELETE_ON_CLOSE) {
2904 status = can_set_delete_on_close(fsp, 0);
2905 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
2908 file_free(req, fsp);
2912 if (NT_STATUS_IS_OK(status)) {
2913 /* Note that here we set the *inital* delete on close flag,
2914 not the regular one. The magic gets handled in close. */
2915 fsp->initial_delete_on_close = True;
2926 return NT_STATUS_OK;
2929 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
2930 struct smb_filename *smb_dname)
2935 status = SMB_VFS_CREATE_FILE(
2938 0, /* root_dir_fid */
2939 smb_dname, /* fname */
2940 FILE_READ_ATTRIBUTES, /* access_mask */
2941 FILE_SHARE_NONE, /* share_access */
2942 FILE_CREATE, /* create_disposition*/
2943 FILE_DIRECTORY_FILE, /* create_options */
2944 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
2945 0, /* oplock_request */
2946 0, /* allocation_size */
2947 0, /* private_flags */
2953 if (NT_STATUS_IS_OK(status)) {
2954 close_file(req, fsp, NORMAL_CLOSE);
2960 /****************************************************************************
2961 Receive notification that one of our open files has been renamed by another
2963 ****************************************************************************/
2965 void msg_file_was_renamed(struct messaging_context *msg,
2968 struct server_id server_id,
2971 struct smbd_server_connection *sconn;
2973 char *frm = (char *)data->data;
2975 const char *sharepath;
2976 const char *base_name;
2977 const char *stream_name;
2978 struct smb_filename *smb_fname = NULL;
2979 size_t sp_len, bn_len;
2982 sconn = msg_ctx_to_sconn(msg);
2983 if (sconn == NULL) {
2984 DEBUG(1, ("could not find sconn\n"));
2988 if (data->data == NULL
2989 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
2990 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
2991 (int)data->length));
2995 /* Unpack the message. */
2996 pull_file_id_24(frm, &id);
2997 sharepath = &frm[24];
2998 sp_len = strlen(sharepath);
2999 base_name = sharepath + sp_len + 1;
3000 bn_len = strlen(base_name);
3001 stream_name = sharepath + sp_len + 1 + bn_len + 1;
3003 /* stream_name must always be NULL if there is no stream. */
3004 if (stream_name[0] == '\0') {
3008 status = create_synthetic_smb_fname(talloc_tos(), base_name,
3009 stream_name, NULL, &smb_fname);
3010 if (!NT_STATUS_IS_OK(status)) {
3014 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3016 sharepath, smb_fname_str_dbg(smb_fname),
3017 file_id_string_tos(&id)));
3019 for(fsp = file_find_di_first(sconn, id); fsp;
3020 fsp = file_find_di_next(fsp)) {
3021 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
3023 DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n",
3024 fsp->fnum, fsp_str_dbg(fsp),
3025 smb_fname_str_dbg(smb_fname)));
3026 status = fsp_set_smb_fname(fsp, smb_fname);
3027 if (!NT_STATUS_IS_OK(status)) {
3032 /* Now we have the complete path we can work out if this is
3033 actually within this share and adjust newname accordingly. */
3034 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3035 "not sharepath %s) "
3036 "fnum %d from %s -> %s\n",
3037 fsp->conn->connectpath,
3041 smb_fname_str_dbg(smb_fname)));
3045 TALLOC_FREE(smb_fname);
3050 * If a main file is opened for delete, all streams need to be checked for
3051 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3052 * If that works, delete them all by setting the delete on close and close.
3055 NTSTATUS open_streams_for_delete(connection_struct *conn,
3058 struct stream_struct *stream_info;
3059 files_struct **streams;
3061 unsigned int num_streams;
3062 TALLOC_CTX *frame = talloc_stackframe();
3065 status = SMB_VFS_STREAMINFO(conn, NULL, fname, talloc_tos(),
3066 &num_streams, &stream_info);
3068 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
3069 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
3070 DEBUG(10, ("no streams around\n"));
3072 return NT_STATUS_OK;
3075 if (!NT_STATUS_IS_OK(status)) {
3076 DEBUG(10, ("SMB_VFS_STREAMINFO failed: %s\n",
3077 nt_errstr(status)));
3081 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3084 if (num_streams == 0) {
3086 return NT_STATUS_OK;
3089 streams = TALLOC_ARRAY(talloc_tos(), files_struct *, num_streams);
3090 if (streams == NULL) {
3091 DEBUG(0, ("talloc failed\n"));
3092 status = NT_STATUS_NO_MEMORY;
3096 for (i=0; i<num_streams; i++) {
3097 struct smb_filename *smb_fname = NULL;
3099 if (strequal(stream_info[i].name, "::$DATA")) {
3104 status = create_synthetic_smb_fname(talloc_tos(), fname,
3105 stream_info[i].name,
3107 if (!NT_STATUS_IS_OK(status)) {
3111 if (SMB_VFS_STAT(conn, smb_fname) == -1) {
3112 DEBUG(10, ("Unable to stat stream: %s\n",
3113 smb_fname_str_dbg(smb_fname)));
3116 status = SMB_VFS_CREATE_FILE(
3119 0, /* root_dir_fid */
3120 smb_fname, /* fname */
3121 DELETE_ACCESS, /* access_mask */
3122 (FILE_SHARE_READ | /* share_access */
3123 FILE_SHARE_WRITE | FILE_SHARE_DELETE),
3124 FILE_OPEN, /* create_disposition*/
3125 0, /* create_options */
3126 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
3127 0, /* oplock_request */
3128 0, /* allocation_size */
3129 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */
3132 &streams[i], /* result */
3135 if (!NT_STATUS_IS_OK(status)) {
3136 DEBUG(10, ("Could not open stream %s: %s\n",
3137 smb_fname_str_dbg(smb_fname),
3138 nt_errstr(status)));
3140 TALLOC_FREE(smb_fname);
3143 TALLOC_FREE(smb_fname);
3147 * don't touch the variable "status" beyond this point :-)
3150 for (i -= 1 ; i >= 0; i--) {
3151 if (streams[i] == NULL) {
3155 DEBUG(10, ("Closing stream # %d, %s\n", i,
3156 fsp_str_dbg(streams[i])));
3157 close_file(NULL, streams[i], NORMAL_CLOSE);
3166 * Wrapper around open_file_ntcreate and open_directory
3169 static NTSTATUS create_file_unixpath(connection_struct *conn,
3170 struct smb_request *req,
3171 struct smb_filename *smb_fname,
3172 uint32_t access_mask,
3173 uint32_t share_access,
3174 uint32_t create_disposition,
3175 uint32_t create_options,
3176 uint32_t file_attributes,
3177 uint32_t oplock_request,
3178 uint64_t allocation_size,
3179 uint32_t private_flags,
3180 struct security_descriptor *sd,
3181 struct ea_list *ea_list,
3183 files_struct **result,
3186 int info = FILE_WAS_OPENED;
3187 files_struct *base_fsp = NULL;
3188 files_struct *fsp = NULL;
3191 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3192 "file_attributes = 0x%x, share_access = 0x%x, "
3193 "create_disposition = 0x%x create_options = 0x%x "
3194 "oplock_request = 0x%x private_flags = 0x%x "
3195 "ea_list = 0x%p, sd = 0x%p, "
3197 (unsigned int)access_mask,
3198 (unsigned int)file_attributes,
3199 (unsigned int)share_access,
3200 (unsigned int)create_disposition,
3201 (unsigned int)create_options,
3202 (unsigned int)oplock_request,
3203 (unsigned int)private_flags,
3204 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3206 if (create_options & FILE_OPEN_BY_FILE_ID) {
3207 status = NT_STATUS_NOT_SUPPORTED;
3211 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
3212 status = NT_STATUS_INVALID_PARAMETER;
3217 oplock_request |= INTERNAL_OPEN_ONLY;
3220 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3221 && (access_mask & DELETE_ACCESS)
3222 && !is_ntfs_stream_smb_fname(smb_fname)) {
3224 * We can't open a file with DELETE access if any of the
3225 * streams is open without FILE_SHARE_DELETE
3227 status = open_streams_for_delete(conn, smb_fname->base_name);
3229 if (!NT_STATUS_IS_OK(status)) {
3234 /* This is the correct thing to do (check every time) but can_delete
3235 * is expensive (it may have to read the parent directory
3236 * permissions). So for now we're not doing it unless we have a strong
3237 * hint the client is really going to delete this file. If the client
3238 * is forcing FILE_CREATE let the filesystem take care of the
3241 /* Setting FILE_SHARE_DELETE is the hint. */
3243 if (lp_acl_check_permissions(SNUM(conn))
3244 && (create_disposition != FILE_CREATE)
3245 && (access_mask & DELETE_ACCESS)
3246 && (!(can_delete_file_in_directory(conn, smb_fname) ||
3247 can_access_file_acl(conn, smb_fname, DELETE_ACCESS)))) {
3248 status = NT_STATUS_ACCESS_DENIED;
3249 DEBUG(10,("create_file_unixpath: open file %s "
3250 "for delete ACCESS_DENIED\n",
3251 smb_fname_str_dbg(smb_fname)));
3255 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3256 !security_token_has_privilege(get_current_nttok(conn),
3257 SEC_PRIV_SECURITY)) {
3258 DEBUG(10, ("create_file_unixpath: open on %s "
3259 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3260 smb_fname_str_dbg(smb_fname)));
3261 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3265 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3266 && is_ntfs_stream_smb_fname(smb_fname)
3267 && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
3268 uint32 base_create_disposition;
3269 struct smb_filename *smb_fname_base = NULL;
3271 if (create_options & FILE_DIRECTORY_FILE) {
3272 status = NT_STATUS_NOT_A_DIRECTORY;
3276 switch (create_disposition) {
3278 base_create_disposition = FILE_OPEN;
3281 base_create_disposition = FILE_OPEN_IF;
3285 /* Create an smb_filename with stream_name == NULL. */
3286 status = create_synthetic_smb_fname(talloc_tos(),
3287 smb_fname->base_name,
3290 if (!NT_STATUS_IS_OK(status)) {
3294 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
3295 DEBUG(10, ("Unable to stat stream: %s\n",
3296 smb_fname_str_dbg(smb_fname_base)));
3299 /* Open the base file. */
3300 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
3303 | FILE_SHARE_DELETE,
3304 base_create_disposition,
3305 0, 0, 0, 0, 0, NULL, NULL,
3307 TALLOC_FREE(smb_fname_base);
3309 if (!NT_STATUS_IS_OK(status)) {
3310 DEBUG(10, ("create_file_unixpath for base %s failed: "
3311 "%s\n", smb_fname->base_name,
3312 nt_errstr(status)));
3315 /* we don't need to low level fd */
3320 * If it's a request for a directory open, deal with it separately.
3323 if (create_options & FILE_DIRECTORY_FILE) {
3325 if (create_options & FILE_NON_DIRECTORY_FILE) {
3326 status = NT_STATUS_INVALID_PARAMETER;
3330 /* Can't open a temp directory. IFS kit test. */
3331 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
3332 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
3333 status = NT_STATUS_INVALID_PARAMETER;
3338 * We will get a create directory here if the Win32
3339 * app specified a security descriptor in the
3340 * CreateDirectory() call.
3344 status = open_directory(
3345 conn, req, smb_fname, access_mask, share_access,
3346 create_disposition, create_options, file_attributes,
3351 * Ordinary file case.
3354 status = file_new(req, conn, &fsp);
3355 if(!NT_STATUS_IS_OK(status)) {
3359 status = fsp_set_smb_fname(fsp, smb_fname);
3360 if (!NT_STATUS_IS_OK(status)) {
3365 * We're opening the stream element of a base_fsp
3366 * we already opened. Set up the base_fsp pointer.
3369 fsp->base_fsp = base_fsp;
3372 status = open_file_ntcreate(conn,
3384 if(!NT_STATUS_IS_OK(status)) {
3385 file_free(req, fsp);
3389 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
3391 /* A stream open never opens a directory */
3394 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3399 * Fail the open if it was explicitly a non-directory
3403 if (create_options & FILE_NON_DIRECTORY_FILE) {
3404 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3409 status = open_directory(
3410 conn, req, smb_fname, access_mask,
3411 share_access, create_disposition,
3412 create_options, file_attributes,
3417 if (!NT_STATUS_IS_OK(status)) {
3421 fsp->base_fsp = base_fsp;
3424 * According to the MS documentation, the only time the security
3425 * descriptor is applied to the opened file is iff we *created* the
3426 * file; an existing file stays the same.
3428 * Also, it seems (from observation) that you can open the file with
3429 * any access mask but you can still write the sd. We need to override
3430 * the granted access before we call set_sd
3431 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3434 if ((sd != NULL) && (info == FILE_WAS_CREATED)
3435 && lp_nt_acl_support(SNUM(conn))) {
3437 uint32_t sec_info_sent;
3438 uint32_t saved_access_mask = fsp->access_mask;
3440 sec_info_sent = get_sec_info(sd);
3442 fsp->access_mask = FILE_GENERIC_ALL;
3444 /* Convert all the generic bits. */
3445 security_acl_map_generic(sd->dacl, &file_generic_mapping);
3446 security_acl_map_generic(sd->sacl, &file_generic_mapping);
3448 if (sec_info_sent & (SECINFO_OWNER|
3452 status = SMB_VFS_FSET_NT_ACL(fsp, sec_info_sent, sd);
3455 fsp->access_mask = saved_access_mask;
3457 if (!NT_STATUS_IS_OK(status)) {
3462 if ((ea_list != NULL) &&
3463 ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
3464 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
3465 if (!NT_STATUS_IS_OK(status)) {
3470 if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
3471 status = NT_STATUS_ACCESS_DENIED;
3475 /* Save the requested allocation size. */
3476 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
3478 && (allocation_size > fsp->fsp_name->st.st_ex_size)) {
3479 fsp->initial_allocation_size = smb_roundup(
3480 fsp->conn, allocation_size);
3481 if (fsp->is_directory) {
3482 /* Can't set allocation size on a directory. */
3483 status = NT_STATUS_ACCESS_DENIED;
3486 if (vfs_allocate_file_space(
3487 fsp, fsp->initial_allocation_size) == -1) {
3488 status = NT_STATUS_DISK_FULL;
3492 fsp->initial_allocation_size = smb_roundup(
3493 fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
3497 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
3500 if (pinfo != NULL) {
3504 smb_fname->st = fsp->fsp_name->st;
3506 return NT_STATUS_OK;
3509 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
3512 if (base_fsp && fsp->base_fsp == base_fsp) {
3514 * The close_file below will close
3519 close_file(req, fsp, ERROR_CLOSE);
3522 if (base_fsp != NULL) {
3523 close_file(req, base_fsp, ERROR_CLOSE);
3530 * Calculate the full path name given a relative fid.
3532 NTSTATUS get_relative_fid_filename(connection_struct *conn,
3533 struct smb_request *req,
3534 uint16_t root_dir_fid,
3535 const struct smb_filename *smb_fname,
3536 struct smb_filename **smb_fname_out)
3538 files_struct *dir_fsp;
3539 char *parent_fname = NULL;
3540 char *new_base_name = NULL;
3543 if (root_dir_fid == 0 || !smb_fname) {
3544 status = NT_STATUS_INTERNAL_ERROR;
3548 dir_fsp = file_fsp(req, root_dir_fid);
3550 if (dir_fsp == NULL) {
3551 status = NT_STATUS_INVALID_HANDLE;
3555 if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) {
3556 status = NT_STATUS_INVALID_HANDLE;
3560 if (!dir_fsp->is_directory) {
3563 * Check to see if this is a mac fork of some kind.
3566 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
3567 is_ntfs_stream_smb_fname(smb_fname)) {
3568 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3573 we need to handle the case when we get a
3574 relative open relative to a file and the
3575 pathname is blank - this is a reopen!
3576 (hint from demyn plantenberg)
3579 status = NT_STATUS_INVALID_HANDLE;
3583 if (ISDOT(dir_fsp->fsp_name->base_name)) {
3585 * We're at the toplevel dir, the final file name
3586 * must not contain ./, as this is filtered out
3587 * normally by srvstr_get_path and unix_convert
3588 * explicitly rejects paths containing ./.
3590 parent_fname = talloc_strdup(talloc_tos(), "");
3591 if (parent_fname == NULL) {
3592 status = NT_STATUS_NO_MEMORY;
3596 size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name);
3599 * Copy in the base directory name.
3602 parent_fname = TALLOC_ARRAY(talloc_tos(), char,
3604 if (parent_fname == NULL) {
3605 status = NT_STATUS_NO_MEMORY;
3608 memcpy(parent_fname, dir_fsp->fsp_name->base_name,
3612 * Ensure it ends in a '/'.
3613 * We used TALLOC_SIZE +2 to add space for the '/'.
3617 && (parent_fname[dir_name_len-1] != '\\')
3618 && (parent_fname[dir_name_len-1] != '/')) {
3619 parent_fname[dir_name_len] = '/';
3620 parent_fname[dir_name_len+1] = '\0';
3624 new_base_name = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
3625 smb_fname->base_name);
3626 if (new_base_name == NULL) {
3627 status = NT_STATUS_NO_MEMORY;
3631 status = filename_convert(req,
3633 req->flags2 & FLAGS2_DFS_PATHNAMES,
3638 if (!NT_STATUS_IS_OK(status)) {
3643 TALLOC_FREE(parent_fname);
3644 TALLOC_FREE(new_base_name);
3648 NTSTATUS create_file_default(connection_struct *conn,
3649 struct smb_request *req,
3650 uint16_t root_dir_fid,
3651 struct smb_filename *smb_fname,
3652 uint32_t access_mask,
3653 uint32_t share_access,
3654 uint32_t create_disposition,
3655 uint32_t create_options,
3656 uint32_t file_attributes,
3657 uint32_t oplock_request,
3658 uint64_t allocation_size,
3659 uint32_t private_flags,
3660 struct security_descriptor *sd,
3661 struct ea_list *ea_list,
3662 files_struct **result,
3665 int info = FILE_WAS_OPENED;
3666 files_struct *fsp = NULL;
3668 bool stream_name = false;
3670 DEBUG(10,("create_file: access_mask = 0x%x "
3671 "file_attributes = 0x%x, share_access = 0x%x, "
3672 "create_disposition = 0x%x create_options = 0x%x "
3673 "oplock_request = 0x%x "
3674 "private_flags = 0x%x "
3675 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
3677 (unsigned int)access_mask,
3678 (unsigned int)file_attributes,
3679 (unsigned int)share_access,
3680 (unsigned int)create_disposition,
3681 (unsigned int)create_options,
3682 (unsigned int)oplock_request,
3683 (unsigned int)private_flags,
3684 (unsigned int)root_dir_fid,
3685 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3688 * Calculate the filename from the root_dir_if if necessary.
3691 if (root_dir_fid != 0) {
3692 struct smb_filename *smb_fname_out = NULL;
3693 status = get_relative_fid_filename(conn, req, root_dir_fid,
3694 smb_fname, &smb_fname_out);
3695 if (!NT_STATUS_IS_OK(status)) {
3698 smb_fname = smb_fname_out;
3702 * Check to see if this is a mac fork of some kind.
3705 stream_name = is_ntfs_stream_smb_fname(smb_fname);
3707 enum FAKE_FILE_TYPE fake_file_type;
3709 fake_file_type = is_fake_file(smb_fname);
3711 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
3714 * Here we go! support for changing the disk quotas
3717 * We need to fake up to open this MAGIC QUOTA file
3718 * and return a valid FID.
3720 * w2k close this file directly after openening xp
3721 * also tries a QUERY_FILE_INFO on the file and then
3724 status = open_fake_file(req, conn, req->vuid,
3725 fake_file_type, smb_fname,
3727 if (!NT_STATUS_IS_OK(status)) {
3731 ZERO_STRUCT(smb_fname->st);
3735 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
3736 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
3741 /* All file access must go through check_name() */
3743 status = check_name(conn, smb_fname->base_name);
3744 if (!NT_STATUS_IS_OK(status)) {
3748 if (stream_name && is_ntfs_default_stream_smb_fname(smb_fname)) {
3750 smb_fname->stream_name = NULL;
3751 /* We have to handle this error here. */
3752 if (create_options & FILE_DIRECTORY_FILE) {
3753 status = NT_STATUS_NOT_A_DIRECTORY;
3756 if (lp_posix_pathnames()) {
3757 ret = SMB_VFS_LSTAT(conn, smb_fname);
3759 ret = SMB_VFS_STAT(conn, smb_fname);
3762 if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
3763 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3768 status = create_file_unixpath(
3769 conn, req, smb_fname, access_mask, share_access,
3770 create_disposition, create_options, file_attributes,
3771 oplock_request, allocation_size, private_flags,
3775 if (!NT_STATUS_IS_OK(status)) {
3780 DEBUG(10, ("create_file: info=%d\n", info));
3783 if (pinfo != NULL) {
3786 return NT_STATUS_OK;
3789 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
3792 close_file(req, fsp, ERROR_CLOSE);