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 "librpc/gen_ndr/messaging.h"
29 #include "../libcli/security/security.h"
30 #include "../librpc/gen_ndr/ndr_security.h"
32 extern const struct generic_mapping file_generic_mapping;
34 struct deferred_open_record {
35 bool delayed_for_oplocks;
39 /****************************************************************************
40 SMB1 file varient of se_access_check. Never test FILE_READ_ATTRIBUTES.
41 ****************************************************************************/
43 NTSTATUS smb1_file_se_access_check(struct connection_struct *conn,
44 const struct security_descriptor *sd,
45 const struct security_token *token,
46 uint32_t access_desired,
47 uint32_t *access_granted)
51 if (get_current_uid(conn) == (uid_t)0) {
52 /* I'm sorry sir, I didn't know you were root... */
53 *access_granted = access_desired;
54 if (access_desired & SEC_FLAG_MAXIMUM_ALLOWED) {
55 *access_granted |= FILE_GENERIC_ALL;
60 return se_access_check(sd,
62 (access_desired & ~FILE_READ_ATTRIBUTES),
66 /****************************************************************************
67 Check if we have open rights.
68 ****************************************************************************/
70 NTSTATUS smbd_check_open_rights(struct connection_struct *conn,
71 const struct smb_filename *smb_fname,
73 uint32_t *access_granted)
75 /* Check if we have rights to open. */
77 struct security_descriptor *sd = NULL;
79 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
84 if (!NT_STATUS_IS_OK(status)) {
85 DEBUG(10, ("smbd_check_open_rights: Could not get acl "
87 smb_fname_str_dbg(smb_fname),
92 status = smb1_file_se_access_check(conn,
94 get_current_nttok(conn),
98 DEBUG(10,("smbd_check_open_rights: file %s requesting "
99 "0x%x returning 0x%x (%s)\n",
100 smb_fname_str_dbg(smb_fname),
101 (unsigned int)access_mask,
102 (unsigned int)*access_granted,
103 nt_errstr(status) ));
105 if (!NT_STATUS_IS_OK(status)) {
106 if (DEBUGLEVEL >= 10) {
107 DEBUG(10,("smbd_check_open_rights: acl for %s is:\n",
108 smb_fname_str_dbg(smb_fname) ));
109 NDR_PRINT_DEBUG(security_descriptor, sd);
118 /****************************************************************************
119 fd support routines - attempt to do a dos_open.
120 ****************************************************************************/
122 static NTSTATUS fd_open(struct connection_struct *conn,
127 struct smb_filename *smb_fname = fsp->fsp_name;
128 NTSTATUS status = NT_STATUS_OK;
132 * Never follow symlinks on a POSIX client. The
133 * client should be doing this.
136 if (fsp->posix_open || !lp_symlinks(SNUM(conn))) {
141 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, mode);
142 if (fsp->fh->fd == -1) {
143 status = map_nt_error_from_unix(errno);
144 if (errno == EMFILE) {
145 static time_t last_warned = 0L;
147 if (time((time_t *) NULL) > last_warned) {
148 DEBUG(0,("Too many open files, unable "
149 "to open more! smbd's max "
151 lp_max_open_files()));
152 last_warned = time((time_t *) NULL);
158 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
159 smb_fname_str_dbg(smb_fname), flags, (int)mode, fsp->fh->fd,
160 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
165 /****************************************************************************
166 Close the file associated with a fsp.
167 ****************************************************************************/
169 NTSTATUS fd_close(files_struct *fsp)
176 if (fsp->fh->fd == -1) {
177 return NT_STATUS_OK; /* What we used to call a stat open. */
179 if (fsp->fh->ref_count > 1) {
180 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
183 ret = SMB_VFS_CLOSE(fsp);
186 return map_nt_error_from_unix(errno);
191 /****************************************************************************
192 Change the ownership of a file to that of the parent directory.
193 Do this by fd if possible.
194 ****************************************************************************/
196 void change_file_owner_to_parent(connection_struct *conn,
197 const char *inherit_from_dir,
200 struct smb_filename *smb_fname_parent = NULL;
204 status = create_synthetic_smb_fname(talloc_tos(), inherit_from_dir,
205 NULL, NULL, &smb_fname_parent);
206 if (!NT_STATUS_IS_OK(status)) {
210 ret = SMB_VFS_STAT(conn, smb_fname_parent);
212 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
213 "directory %s. Error was %s\n",
214 smb_fname_str_dbg(smb_fname_parent),
220 ret = SMB_VFS_FCHOWN(fsp, smb_fname_parent->st.st_ex_uid, (gid_t)-1);
223 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
224 "file %s to parent directory uid %u. Error "
225 "was %s\n", fsp_str_dbg(fsp),
226 (unsigned int)smb_fname_parent->st.st_ex_uid,
230 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
231 "parent directory uid %u.\n", fsp_str_dbg(fsp),
232 (unsigned int)smb_fname_parent->st.st_ex_uid));
234 TALLOC_FREE(smb_fname_parent);
237 NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
238 const char *inherit_from_dir,
240 SMB_STRUCT_STAT *psbuf)
242 struct smb_filename *smb_fname_parent = NULL;
243 struct smb_filename *smb_fname_cwd = NULL;
244 char *saved_dir = NULL;
245 TALLOC_CTX *ctx = talloc_tos();
246 NTSTATUS status = NT_STATUS_OK;
249 status = create_synthetic_smb_fname(ctx, inherit_from_dir, NULL, NULL,
251 if (!NT_STATUS_IS_OK(status)) {
255 ret = SMB_VFS_STAT(conn, smb_fname_parent);
257 status = map_nt_error_from_unix(errno);
258 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
259 "directory %s. Error was %s\n",
260 smb_fname_str_dbg(smb_fname_parent),
265 /* We've already done an lstat into psbuf, and we know it's a
266 directory. If we can cd into the directory and the dev/ino
267 are the same then we can safely chown without races as
268 we're locking the directory in place by being in it. This
269 should work on any UNIX (thanks tridge :-). JRA.
272 saved_dir = vfs_GetWd(ctx,conn);
274 status = map_nt_error_from_unix(errno);
275 DEBUG(0,("change_dir_owner_to_parent: failed to get "
276 "current working directory. Error was %s\n",
281 /* Chdir into the new path. */
282 if (vfs_ChDir(conn, fname) == -1) {
283 status = map_nt_error_from_unix(errno);
284 DEBUG(0,("change_dir_owner_to_parent: failed to change "
285 "current working directory to %s. Error "
286 "was %s\n", fname, strerror(errno) ));
290 status = create_synthetic_smb_fname(ctx, ".", NULL, NULL,
292 if (!NT_STATUS_IS_OK(status)) {
296 ret = SMB_VFS_STAT(conn, smb_fname_cwd);
298 status = map_nt_error_from_unix(errno);
299 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
300 "directory '.' (%s) Error was %s\n",
301 fname, strerror(errno)));
305 /* Ensure we're pointing at the same place. */
306 if (smb_fname_cwd->st.st_ex_dev != psbuf->st_ex_dev ||
307 smb_fname_cwd->st.st_ex_ino != psbuf->st_ex_ino ||
308 smb_fname_cwd->st.st_ex_mode != psbuf->st_ex_mode ) {
309 DEBUG(0,("change_dir_owner_to_parent: "
310 "device/inode/mode on directory %s changed. "
311 "Refusing to chown !\n", fname ));
312 status = NT_STATUS_ACCESS_DENIED;
317 ret = SMB_VFS_CHOWN(conn, ".", smb_fname_parent->st.st_ex_uid,
321 status = map_nt_error_from_unix(errno);
322 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
323 "directory %s to parent directory uid %u. "
324 "Error was %s\n", fname,
325 (unsigned int)smb_fname_parent->st.st_ex_uid,
330 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
331 "directory %s to parent directory uid %u.\n",
332 fname, (unsigned int)smb_fname_parent->st.st_ex_uid ));
335 vfs_ChDir(conn,saved_dir);
337 TALLOC_FREE(smb_fname_parent);
338 TALLOC_FREE(smb_fname_cwd);
342 /****************************************************************************
344 ****************************************************************************/
346 static NTSTATUS open_file(files_struct *fsp,
347 connection_struct *conn,
348 struct smb_request *req,
349 const char *parent_dir,
352 uint32 access_mask, /* client requested access mask. */
353 uint32 open_access_mask) /* what we're actually using in the open. */
355 struct smb_filename *smb_fname = fsp->fsp_name;
356 NTSTATUS status = NT_STATUS_OK;
357 int accmode = (flags & O_ACCMODE);
358 int local_flags = flags;
359 bool file_existed = VALID_STAT(fsp->fsp_name->st);
364 /* Check permissions */
367 * This code was changed after seeing a client open request
368 * containing the open mode of (DENY_WRITE/read-only) with
369 * the 'create if not exist' bit set. The previous code
370 * would fail to open the file read only on a read-only share
371 * as it was checking the flags parameter directly against O_RDONLY,
372 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
376 if (!CAN_WRITE(conn)) {
377 /* It's a read-only share - fail if we wanted to write. */
378 if(accmode != O_RDONLY || (flags & O_TRUNC) || (flags & O_APPEND)) {
379 DEBUG(3,("Permission denied opening %s\n",
380 smb_fname_str_dbg(smb_fname)));
381 return NT_STATUS_ACCESS_DENIED;
382 } else if(flags & O_CREAT) {
383 /* We don't want to write - but we must make sure that
384 O_CREAT doesn't create the file if we have write
385 access into the directory.
387 flags &= ~(O_CREAT|O_EXCL);
388 local_flags &= ~(O_CREAT|O_EXCL);
393 * This little piece of insanity is inspired by the
394 * fact that an NT client can open a file for O_RDONLY,
395 * but set the create disposition to FILE_EXISTS_TRUNCATE.
396 * If the client *can* write to the file, then it expects to
397 * truncate the file, even though it is opening for readonly.
398 * Quicken uses this stupid trick in backup file creation...
399 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
400 * for helping track this one down. It didn't bite us in 2.0.x
401 * as we always opened files read-write in that release. JRA.
404 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
405 DEBUG(10,("open_file: truncate requested on read-only open "
406 "for file %s\n", smb_fname_str_dbg(smb_fname)));
407 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
410 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
411 (!file_existed && (local_flags & O_CREAT)) ||
412 ((local_flags & O_TRUNC) == O_TRUNC) ) {
416 * We can't actually truncate here as the file may be locked.
417 * open_file_ntcreate will take care of the truncate later. JRA.
420 local_flags &= ~O_TRUNC;
422 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
424 * We would block on opening a FIFO with no one else on the
425 * other end. Do what we used to do and add O_NONBLOCK to the
429 if (file_existed && S_ISFIFO(smb_fname->st.st_ex_mode)) {
430 local_flags |= O_NONBLOCK;
434 /* Don't create files with Microsoft wildcard characters. */
437 * wildcard characters are allowed in stream names
438 * only test the basefilename
440 wild = fsp->base_fsp->fsp_name->base_name;
442 wild = smb_fname->base_name;
444 if ((local_flags & O_CREAT) && !file_existed &&
446 return NT_STATUS_OBJECT_NAME_INVALID;
449 /* Actually do the open */
450 status = fd_open(conn, fsp, local_flags, unx_mode);
451 if (!NT_STATUS_IS_OK(status)) {
452 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
453 "(flags=%d)\n", smb_fname_str_dbg(smb_fname),
454 nt_errstr(status),local_flags,flags));
458 if ((local_flags & O_CREAT) && !file_existed) {
460 /* Inherit the ACL if required */
461 if (lp_inherit_perms(SNUM(conn))) {
462 inherit_access_posix_acl(conn, parent_dir,
463 smb_fname->base_name,
467 /* Change the owner if required. */
468 if (lp_inherit_owner(SNUM(conn))) {
469 change_file_owner_to_parent(conn, parent_dir,
473 notify_fname(conn, NOTIFY_ACTION_ADDED,
474 FILE_NOTIFY_CHANGE_FILE_NAME,
475 smb_fname->base_name);
479 fsp->fh->fd = -1; /* What we used to call a stat open. */
481 uint32_t access_granted = 0;
483 status = smbd_check_open_rights(conn,
487 if (!NT_STATUS_IS_OK(status)) {
488 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
490 * On NT_STATUS_ACCESS_DENIED, access_granted
491 * contains the denied bits.
494 if ((access_mask & FILE_WRITE_ATTRIBUTES) &&
495 (access_granted & FILE_WRITE_ATTRIBUTES) &&
496 (lp_map_readonly(SNUM(conn)) ||
497 lp_map_archive(SNUM(conn)) ||
498 lp_map_hidden(SNUM(conn)) ||
499 lp_map_system(SNUM(conn)))) {
500 access_granted &= ~FILE_WRITE_ATTRIBUTES;
502 DEBUG(10,("open_file: "
511 if ((access_mask & DELETE_ACCESS) &&
512 (access_granted & DELETE_ACCESS) &&
513 can_delete_file_in_directory(conn,
515 /* Were we trying to do a stat open
516 * for delete and didn't get DELETE
517 * access (only) ? Check if the
518 * directory allows DELETE_CHILD.
520 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
523 access_granted &= ~DELETE_ACCESS;
525 DEBUG(10,("open_file: "
533 if (access_granted != 0) {
534 DEBUG(10,("open_file: Access "
541 } else if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
543 S_ISLNK(smb_fname->st.st_ex_mode)) {
544 /* This is a POSIX stat open for delete
545 * or rename on a symlink that points
547 DEBUG(10,("open_file: allowing POSIX "
548 "open on bad symlink %s\n",
552 DEBUG(10,("open_file: "
553 "smbd_check_open_rights on file "
555 smb_fname_str_dbg(smb_fname),
556 nt_errstr(status) ));
566 if (fsp->fh->fd == -1) {
567 ret = SMB_VFS_STAT(conn, smb_fname);
569 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
570 /* If we have an fd, this stat should succeed. */
572 DEBUG(0,("Error doing fstat on open file %s "
574 smb_fname_str_dbg(smb_fname),
579 /* For a non-io open, this stat failing means file not found. JRA */
581 status = map_nt_error_from_unix(errno);
588 * POSIX allows read-only opens of directories. We don't
589 * want to do this (we use a different code path for this)
590 * so catch a directory open and return an EISDIR. JRA.
593 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
596 return NT_STATUS_FILE_IS_A_DIRECTORY;
599 fsp->mode = smb_fname->st.st_ex_mode;
600 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
601 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
602 fsp->file_pid = req ? req->smbpid : 0;
603 fsp->can_lock = True;
604 fsp->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
605 if (!CAN_WRITE(conn)) {
606 fsp->can_write = False;
608 fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ?
611 fsp->print_file = NULL;
612 fsp->modified = False;
613 fsp->sent_oplock_break = NO_BREAK_SENT;
614 fsp->is_directory = False;
615 if (conn->aio_write_behind_list &&
616 is_in_path(smb_fname->base_name, conn->aio_write_behind_list,
617 conn->case_sensitive)) {
618 fsp->aio_write_behind = True;
621 fsp->wcp = NULL; /* Write cache pointer. */
623 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
624 conn->session_info->unix_name,
625 smb_fname_str_dbg(smb_fname),
626 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
627 conn->num_files_open));
633 /*******************************************************************
634 Return True if the filename is one of the special executable types.
635 ********************************************************************/
637 bool is_executable(const char *fname)
639 if ((fname = strrchr_m(fname,'.'))) {
640 if (strequal(fname,".com") ||
641 strequal(fname,".dll") ||
642 strequal(fname,".exe") ||
643 strequal(fname,".sym")) {
650 /****************************************************************************
651 Check if we can open a file with a share mode.
652 Returns True if conflict, False if not.
653 ****************************************************************************/
655 static bool share_conflict(struct share_mode_entry *entry,
659 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
660 "entry->share_access = 0x%x, "
661 "entry->private_options = 0x%x\n",
662 (unsigned int)entry->access_mask,
663 (unsigned int)entry->share_access,
664 (unsigned int)entry->private_options));
666 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
667 (unsigned int)access_mask, (unsigned int)share_access));
669 if ((entry->access_mask & (FILE_WRITE_DATA|
673 DELETE_ACCESS)) == 0) {
674 DEBUG(10,("share_conflict: No conflict due to "
675 "entry->access_mask = 0x%x\n",
676 (unsigned int)entry->access_mask ));
680 if ((access_mask & (FILE_WRITE_DATA|
684 DELETE_ACCESS)) == 0) {
685 DEBUG(10,("share_conflict: No conflict due to "
686 "access_mask = 0x%x\n",
687 (unsigned int)access_mask ));
691 #if 1 /* JRA TEST - Superdebug. */
692 #define CHECK_MASK(num, am, right, sa, share) \
693 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
694 (unsigned int)(num), (unsigned int)(am), \
695 (unsigned int)(right), (unsigned int)(am)&(right) )); \
696 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
697 (unsigned int)(num), (unsigned int)(sa), \
698 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
699 if (((am) & (right)) && !((sa) & (share))) { \
700 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
701 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
702 (unsigned int)(share) )); \
706 #define CHECK_MASK(num, am, right, sa, share) \
707 if (((am) & (right)) && !((sa) & (share))) { \
708 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
709 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
710 (unsigned int)(share) )); \
715 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
716 share_access, FILE_SHARE_WRITE);
717 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
718 entry->share_access, FILE_SHARE_WRITE);
720 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
721 share_access, FILE_SHARE_READ);
722 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
723 entry->share_access, FILE_SHARE_READ);
725 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
726 share_access, FILE_SHARE_DELETE);
727 CHECK_MASK(6, access_mask, DELETE_ACCESS,
728 entry->share_access, FILE_SHARE_DELETE);
730 DEBUG(10,("share_conflict: No conflict.\n"));
734 #if defined(DEVELOPER)
735 static void validate_my_share_entries(struct smbd_server_connection *sconn,
737 struct share_mode_entry *share_entry)
741 if (!procid_is_me(&share_entry->pid)) {
745 if (is_deferred_open_entry(share_entry) &&
746 !open_was_deferred(share_entry->op_mid)) {
747 char *str = talloc_asprintf(talloc_tos(),
748 "Got a deferred entry without a request: "
750 share_mode_str(talloc_tos(), num, share_entry));
754 if (!is_valid_share_mode_entry(share_entry)) {
758 fsp = file_find_dif(sconn, share_entry->id,
759 share_entry->share_file_id);
761 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
762 share_mode_str(talloc_tos(), num, share_entry) ));
763 smb_panic("validate_my_share_entries: Cannot match a "
764 "share entry with an open file\n");
767 if (is_deferred_open_entry(share_entry) ||
768 is_unused_share_mode_entry(share_entry)) {
772 if ((share_entry->op_type == NO_OPLOCK) &&
773 (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK)) {
774 /* Someone has already written to it, but I haven't yet
779 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
788 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
789 share_mode_str(talloc_tos(), num, share_entry) ));
790 str = talloc_asprintf(talloc_tos(),
791 "validate_my_share_entries: "
792 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
793 fsp->fsp_name->base_name,
794 (unsigned int)fsp->oplock_type,
795 (unsigned int)share_entry->op_type );
801 bool is_stat_open(uint32 access_mask)
803 return (access_mask &&
804 ((access_mask & ~(SYNCHRONIZE_ACCESS| FILE_READ_ATTRIBUTES|
805 FILE_WRITE_ATTRIBUTES))==0) &&
806 ((access_mask & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|
807 FILE_WRITE_ATTRIBUTES)) != 0));
810 /****************************************************************************
811 Deal with share modes
812 Invarient: Share mode must be locked on entry and exit.
813 Returns -1 on error, or number of share modes on success (may be zero).
814 ****************************************************************************/
816 static NTSTATUS open_mode_check(connection_struct *conn,
817 struct share_mode_lock *lck,
821 uint32 create_options,
826 if(lck->num_share_modes == 0) {
830 *file_existed = True;
832 /* A delete on close prohibits everything */
834 if (is_delete_on_close_set(lck, name_hash)) {
835 return NT_STATUS_DELETE_PENDING;
838 if (is_stat_open(access_mask)) {
839 /* Stat open that doesn't trigger oplock breaks or share mode
840 * checks... ! JRA. */
845 * Check if the share modes will give us access.
848 #if defined(DEVELOPER)
849 for(i = 0; i < lck->num_share_modes; i++) {
850 validate_my_share_entries(conn->sconn, i,
851 &lck->share_modes[i]);
855 if (!lp_share_modes(SNUM(conn))) {
859 /* Now we check the share modes, after any oplock breaks. */
860 for(i = 0; i < lck->num_share_modes; i++) {
862 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
866 /* someone else has a share lock on it, check to see if we can
868 if (share_conflict(&lck->share_modes[i],
869 access_mask, share_access)) {
870 return NT_STATUS_SHARING_VIOLATION;
877 static bool is_delete_request(files_struct *fsp) {
878 return ((fsp->access_mask == DELETE_ACCESS) &&
879 (fsp->oplock_type == NO_OPLOCK));
883 * Send a break message to the oplock holder and delay the open for
887 static NTSTATUS send_break_message(files_struct *fsp,
888 struct share_mode_entry *exclusive,
893 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
895 DEBUG(10, ("Sending break request to PID %s\n",
896 procid_str_static(&exclusive->pid)));
897 exclusive->op_mid = mid;
899 /* Create the message. */
900 share_mode_entry_to_message(msg, exclusive);
902 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
903 don't want this set in the share mode struct pointed to by lck. */
905 if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
906 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,
907 exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
910 status = messaging_send_buf(fsp->conn->sconn->msg_ctx, exclusive->pid,
911 MSG_SMB_BREAK_REQUEST,
913 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
914 if (!NT_STATUS_IS_OK(status)) {
915 DEBUG(3, ("Could not send oplock break message: %s\n",
923 * Return share_mode_entry pointers for :
924 * 1). Batch oplock entry.
925 * 2). Batch or exclusive oplock entry (may be identical to #1).
926 * bool have_level2_oplock
927 * bool have_no_oplock.
928 * Do internal consistency checks on the share mode for a file.
931 static void find_oplock_types(struct share_mode_lock *lck,
932 struct share_mode_entry **pp_batch,
933 struct share_mode_entry **pp_ex_or_batch,
940 *pp_ex_or_batch = NULL;
942 *got_no_oplock = false;
944 for (i=0; i<lck->num_share_modes; i++) {
945 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
949 if (BATCH_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
950 /* batch - can only be one. */
951 if (*pp_ex_or_batch || *pp_batch || *got_level2 || *got_no_oplock) {
952 smb_panic("Bad batch oplock entry.");
954 *pp_batch = &lck->share_modes[i];
957 if (EXCLUSIVE_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
958 /* Exclusive or batch - can only be one. */
959 if (*pp_ex_or_batch || *got_level2 || *got_no_oplock) {
960 smb_panic("Bad exclusive or batch oplock entry.");
962 *pp_ex_or_batch = &lck->share_modes[i];
965 if (LEVEL_II_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
966 if (*pp_batch || *pp_ex_or_batch) {
967 smb_panic("Bad levelII oplock entry.");
972 if (lck->share_modes[i].op_type == NO_OPLOCK) {
973 if (*pp_batch || *pp_ex_or_batch) {
974 smb_panic("Bad no oplock entry.");
976 *got_no_oplock = true;
981 static bool delay_for_batch_oplocks(files_struct *fsp,
984 struct share_mode_entry *batch_entry)
986 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
990 if (batch_entry != NULL) {
991 /* Found a batch oplock */
992 send_break_message(fsp, batch_entry, mid, oplock_request);
998 static bool delay_for_exclusive_oplocks(files_struct *fsp,
1001 struct share_mode_entry *ex_entry)
1003 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1007 if (ex_entry != NULL) {
1008 /* Found an exclusive or batch oplock */
1009 bool delay_it = is_delete_request(fsp) ?
1010 BATCH_OPLOCK_TYPE(ex_entry->op_type) : true;
1012 send_break_message(fsp, ex_entry, mid, oplock_request);
1019 static bool file_has_brlocks(files_struct *fsp)
1021 struct byte_range_lock *br_lck;
1023 br_lck = brl_get_locks_readonly(fsp);
1027 return br_lck->num_locks > 0 ? true : false;
1030 static void grant_fsp_oplock_type(files_struct *fsp,
1032 bool got_level2_oplock,
1033 bool got_a_none_oplock)
1035 bool allow_level2 = (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
1036 lp_level2_oplocks(SNUM(fsp->conn));
1038 /* Start by granting what the client asked for,
1039 but ensure no SAMBA_PRIVATE bits can be set. */
1040 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1042 if (oplock_request & INTERNAL_OPEN_ONLY) {
1043 /* No oplocks on internal open. */
1044 fsp->oplock_type = NO_OPLOCK;
1045 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1046 fsp->oplock_type, fsp_str_dbg(fsp)));
1048 } else if (lp_locking(fsp->conn->params) && file_has_brlocks(fsp)) {
1049 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1051 fsp->oplock_type = NO_OPLOCK;
1054 if (is_stat_open(fsp->access_mask)) {
1055 /* Leave the value already set. */
1056 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1057 fsp->oplock_type, fsp_str_dbg(fsp)));
1062 * Match what was requested (fsp->oplock_type) with
1063 * what was found in the existing share modes.
1066 if (got_a_none_oplock) {
1067 fsp->oplock_type = NO_OPLOCK;
1068 } else if (got_level2_oplock) {
1069 if (fsp->oplock_type == NO_OPLOCK ||
1070 fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
1071 /* Store a level2 oplock, but don't tell the client */
1072 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1074 fsp->oplock_type = LEVEL_II_OPLOCK;
1077 /* All share_mode_entries are placeholders or deferred.
1078 * Silently upgrade to fake levelII if the client didn't
1079 * ask for an oplock. */
1080 if (fsp->oplock_type == NO_OPLOCK) {
1081 /* Store a level2 oplock, but don't tell the client */
1082 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1087 * Don't grant level2 to clients that don't want them
1088 * or if we've turned them off.
1090 if (fsp->oplock_type == LEVEL_II_OPLOCK && !allow_level2) {
1091 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1094 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1095 fsp->oplock_type, fsp_str_dbg(fsp)));
1098 bool request_timed_out(struct timeval request_time,
1099 struct timeval timeout)
1101 struct timeval now, end_time;
1103 end_time = timeval_sum(&request_time, &timeout);
1104 return (timeval_compare(&end_time, &now) < 0);
1107 /****************************************************************************
1108 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1109 ****************************************************************************/
1111 static void defer_open(struct share_mode_lock *lck,
1112 struct timeval request_time,
1113 struct timeval timeout,
1114 struct smb_request *req,
1115 struct deferred_open_record *state)
1119 /* Paranoia check */
1121 for (i=0; i<lck->num_share_modes; i++) {
1122 struct share_mode_entry *e = &lck->share_modes[i];
1124 if (!is_deferred_open_entry(e)) {
1128 if (procid_is_me(&e->pid) && (e->op_mid == req->mid)) {
1129 DEBUG(0, ("Trying to defer an already deferred "
1130 "request: mid=%llu, exiting\n",
1131 (unsigned long long)req->mid));
1132 exit_server("attempt to defer a deferred request");
1136 /* End paranoia check */
1138 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1139 "open entry for mid %llu\n",
1140 (unsigned int)request_time.tv_sec,
1141 (unsigned int)request_time.tv_usec,
1142 (unsigned long long)req->mid));
1144 if (!push_deferred_open_message_smb(req, request_time, timeout,
1145 state->id, (char *)state, sizeof(*state))) {
1146 exit_server("push_deferred_open_message_smb failed");
1148 add_deferred_open(lck, req->mid, request_time,
1149 sconn_server_id(req->sconn), state->id);
1153 /****************************************************************************
1154 On overwrite open ensure that the attributes match.
1155 ****************************************************************************/
1157 bool open_match_attributes(connection_struct *conn,
1158 uint32 old_dos_attr,
1159 uint32 new_dos_attr,
1160 mode_t existing_unx_mode,
1161 mode_t new_unx_mode,
1162 mode_t *returned_unx_mode)
1164 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
1166 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1167 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1169 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
1170 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
1171 *returned_unx_mode = new_unx_mode;
1173 *returned_unx_mode = (mode_t)0;
1176 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1177 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1178 "returned_unx_mode = 0%o\n",
1179 (unsigned int)old_dos_attr,
1180 (unsigned int)existing_unx_mode,
1181 (unsigned int)new_dos_attr,
1182 (unsigned int)*returned_unx_mode ));
1184 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1185 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1186 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
1187 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
1191 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1192 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
1193 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
1200 /****************************************************************************
1201 Special FCB or DOS processing in the case of a sharing violation.
1202 Try and find a duplicated file handle.
1203 ****************************************************************************/
1205 NTSTATUS fcb_or_dos_open(struct smb_request *req,
1206 connection_struct *conn,
1207 files_struct *fsp_to_dup_into,
1208 const struct smb_filename *smb_fname,
1213 uint32 share_access,
1214 uint32 create_options)
1218 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1219 "file %s.\n", smb_fname_str_dbg(smb_fname)));
1221 for(fsp = file_find_di_first(conn->sconn, id); fsp;
1222 fsp = file_find_di_next(fsp)) {
1224 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1225 "vuid = %u, file_pid = %u, private_options = 0x%x "
1226 "access_mask = 0x%x\n", fsp_str_dbg(fsp),
1227 fsp->fh->fd, (unsigned int)fsp->vuid,
1228 (unsigned int)fsp->file_pid,
1229 (unsigned int)fsp->fh->private_options,
1230 (unsigned int)fsp->access_mask ));
1232 if (fsp->fh->fd != -1 &&
1233 fsp->vuid == vuid &&
1234 fsp->file_pid == file_pid &&
1235 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
1236 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
1237 (fsp->access_mask & FILE_WRITE_DATA) &&
1238 strequal(fsp->fsp_name->base_name, smb_fname->base_name) &&
1239 strequal(fsp->fsp_name->stream_name,
1240 smb_fname->stream_name)) {
1241 DEBUG(10,("fcb_or_dos_open: file match\n"));
1247 return NT_STATUS_NOT_FOUND;
1250 /* quite an insane set of semantics ... */
1251 if (is_executable(smb_fname->base_name) &&
1252 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1253 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1254 return NT_STATUS_INVALID_PARAMETER;
1257 /* We need to duplicate this fsp. */
1258 return dup_file_fsp(req, fsp, access_mask, share_access,
1259 create_options, fsp_to_dup_into);
1262 /****************************************************************************
1263 Open a file with a share mode - old openX method - map into NTCreate.
1264 ****************************************************************************/
1266 bool map_open_params_to_ntcreate(const struct smb_filename *smb_fname,
1267 int deny_mode, int open_func,
1268 uint32 *paccess_mask,
1269 uint32 *pshare_mode,
1270 uint32 *pcreate_disposition,
1271 uint32 *pcreate_options,
1272 uint32_t *pprivate_flags)
1276 uint32 create_disposition;
1277 uint32 create_options = FILE_NON_DIRECTORY_FILE;
1278 uint32_t private_flags = 0;
1280 DEBUG(10,("map_open_params_to_ntcreate: fname = %s, deny_mode = 0x%x, "
1281 "open_func = 0x%x\n",
1282 smb_fname_str_dbg(smb_fname), (unsigned int)deny_mode,
1283 (unsigned int)open_func ));
1285 /* Create the NT compatible access_mask. */
1286 switch (GET_OPENX_MODE(deny_mode)) {
1287 case DOS_OPEN_EXEC: /* Implies read-only - used to be FILE_READ_DATA */
1288 case DOS_OPEN_RDONLY:
1289 access_mask = FILE_GENERIC_READ;
1291 case DOS_OPEN_WRONLY:
1292 access_mask = FILE_GENERIC_WRITE;
1296 access_mask = FILE_GENERIC_READ|FILE_GENERIC_WRITE;
1299 DEBUG(10,("map_open_params_to_ntcreate: bad open mode = 0x%x\n",
1300 (unsigned int)GET_OPENX_MODE(deny_mode)));
1304 /* Create the NT compatible create_disposition. */
1305 switch (open_func) {
1306 case OPENX_FILE_EXISTS_FAIL|OPENX_FILE_CREATE_IF_NOT_EXIST:
1307 create_disposition = FILE_CREATE;
1310 case OPENX_FILE_EXISTS_OPEN:
1311 create_disposition = FILE_OPEN;
1314 case OPENX_FILE_EXISTS_OPEN|OPENX_FILE_CREATE_IF_NOT_EXIST:
1315 create_disposition = FILE_OPEN_IF;
1318 case OPENX_FILE_EXISTS_TRUNCATE:
1319 create_disposition = FILE_OVERWRITE;
1322 case OPENX_FILE_EXISTS_TRUNCATE|OPENX_FILE_CREATE_IF_NOT_EXIST:
1323 create_disposition = FILE_OVERWRITE_IF;
1327 /* From samba4 - to be confirmed. */
1328 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_EXEC) {
1329 create_disposition = FILE_CREATE;
1332 DEBUG(10,("map_open_params_to_ntcreate: bad "
1333 "open_func 0x%x\n", (unsigned int)open_func));
1337 /* Create the NT compatible share modes. */
1338 switch (GET_DENY_MODE(deny_mode)) {
1340 share_mode = FILE_SHARE_NONE;
1344 share_mode = FILE_SHARE_READ;
1348 share_mode = FILE_SHARE_WRITE;
1352 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1356 private_flags |= NTCREATEX_OPTIONS_PRIVATE_DENY_DOS;
1357 if (is_executable(smb_fname->base_name)) {
1358 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1360 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_RDONLY) {
1361 share_mode = FILE_SHARE_READ;
1363 share_mode = FILE_SHARE_NONE;
1369 private_flags |= NTCREATEX_OPTIONS_PRIVATE_DENY_FCB;
1370 share_mode = FILE_SHARE_NONE;
1374 DEBUG(10,("map_open_params_to_ntcreate: bad deny_mode 0x%x\n",
1375 (unsigned int)GET_DENY_MODE(deny_mode) ));
1379 DEBUG(10,("map_open_params_to_ntcreate: file %s, access_mask = 0x%x, "
1380 "share_mode = 0x%x, create_disposition = 0x%x, "
1381 "create_options = 0x%x private_flags = 0x%x\n",
1382 smb_fname_str_dbg(smb_fname),
1383 (unsigned int)access_mask,
1384 (unsigned int)share_mode,
1385 (unsigned int)create_disposition,
1386 (unsigned int)create_options,
1387 (unsigned int)private_flags));
1390 *paccess_mask = access_mask;
1393 *pshare_mode = share_mode;
1395 if (pcreate_disposition) {
1396 *pcreate_disposition = create_disposition;
1398 if (pcreate_options) {
1399 *pcreate_options = create_options;
1401 if (pprivate_flags) {
1402 *pprivate_flags = private_flags;
1409 static void schedule_defer_open(struct share_mode_lock *lck,
1410 struct timeval request_time,
1411 struct smb_request *req)
1413 struct deferred_open_record state;
1415 /* This is a relative time, added to the absolute
1416 request_time value to get the absolute timeout time.
1417 Note that if this is the second or greater time we enter
1418 this codepath for this particular request mid then
1419 request_time is left as the absolute time of the *first*
1420 time this request mid was processed. This is what allows
1421 the request to eventually time out. */
1423 struct timeval timeout;
1425 /* Normally the smbd we asked should respond within
1426 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1427 * the client did, give twice the timeout as a safety
1428 * measure here in case the other smbd is stuck
1429 * somewhere else. */
1431 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1433 /* Nothing actually uses state.delayed_for_oplocks
1434 but it's handy to differentiate in debug messages
1435 between a 30 second delay due to oplock break, and
1436 a 1 second delay for share mode conflicts. */
1438 state.delayed_for_oplocks = True;
1441 if (!request_timed_out(request_time, timeout)) {
1442 defer_open(lck, request_time, timeout, req, &state);
1446 /****************************************************************************
1447 Work out what access_mask to use from what the client sent us.
1448 ****************************************************************************/
1450 static NTSTATUS calculate_access_mask(connection_struct *conn,
1451 const struct smb_filename *smb_fname,
1453 uint32_t access_mask,
1454 uint32_t *access_mask_out)
1459 * Convert GENERIC bits to specific bits.
1462 se_map_generic(&access_mask, &file_generic_mapping);
1464 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1465 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1468 struct security_descriptor *sd;
1469 uint32_t access_granted = 0;
1471 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
1476 if (!NT_STATUS_IS_OK(status)) {
1477 DEBUG(10, ("calculate_access_mask: Could not get acl "
1479 smb_fname_str_dbg(smb_fname),
1480 nt_errstr(status)));
1481 return NT_STATUS_ACCESS_DENIED;
1484 status = smb1_file_se_access_check(conn,
1486 get_current_nttok(conn),
1492 if (!NT_STATUS_IS_OK(status)) {
1493 DEBUG(10, ("calculate_access_mask: Access denied on "
1494 "file %s: when calculating maximum access\n",
1495 smb_fname_str_dbg(smb_fname)));
1496 return NT_STATUS_ACCESS_DENIED;
1499 access_mask = access_granted;
1501 access_mask = FILE_GENERIC_ALL;
1505 *access_mask_out = access_mask;
1506 return NT_STATUS_OK;
1509 /****************************************************************************
1510 Remove the deferred open entry under lock.
1511 ****************************************************************************/
1513 void remove_deferred_open_entry(struct file_id id, uint64_t mid,
1514 struct server_id pid)
1516 struct share_mode_lock *lck = get_share_mode_lock(talloc_tos(), id,
1519 DEBUG(0, ("could not get share mode lock\n"));
1521 del_deferred_open_entry(lck, mid, pid);
1526 /****************************************************************************
1527 Open a file with a share mode. Passed in an already created files_struct *.
1528 ****************************************************************************/
1530 static NTSTATUS open_file_ntcreate(connection_struct *conn,
1531 struct smb_request *req,
1532 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1533 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1534 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1535 uint32 create_options, /* options such as delete on close. */
1536 uint32 new_dos_attributes, /* attributes used for new file. */
1537 int oplock_request, /* internal Samba oplock codes. */
1538 /* Information (FILE_EXISTS etc.) */
1539 uint32_t private_flags, /* Samba specific flags. */
1543 struct smb_filename *smb_fname = fsp->fsp_name;
1546 bool file_existed = VALID_STAT(smb_fname->st);
1547 bool def_acl = False;
1548 bool posix_open = False;
1549 bool new_file_created = False;
1550 bool clear_ads = false;
1552 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1553 mode_t new_unx_mode = (mode_t)0;
1554 mode_t unx_mode = (mode_t)0;
1556 uint32 existing_dos_attributes = 0;
1557 struct timeval request_time = timeval_zero();
1558 struct share_mode_lock *lck = NULL;
1559 uint32 open_access_mask = access_mask;
1565 /* Windows allows a new file to be created and
1566 silently removes a FILE_ATTRIBUTE_DIRECTORY
1567 sent by the client. Do the same. */
1569 new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
1571 if (conn->printer) {
1573 * Printers are handled completely differently.
1574 * Most of the passed parameters are ignored.
1578 *pinfo = FILE_WAS_CREATED;
1581 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1582 smb_fname_str_dbg(smb_fname)));
1585 DEBUG(0,("open_file_ntcreate: printer open without "
1586 "an SMB request!\n"));
1587 return NT_STATUS_INTERNAL_ERROR;
1590 return print_spool_open(fsp, smb_fname->base_name,
1594 if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
1596 return NT_STATUS_NO_MEMORY;
1599 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1601 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1602 new_dos_attributes = 0;
1604 /* We add aARCH to this as this mode is only used if the file is
1606 unx_mode = unix_mode(conn, new_dos_attributes | aARCH,
1607 smb_fname, parent_dir);
1610 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1611 "access_mask=0x%x share_access=0x%x "
1612 "create_disposition = 0x%x create_options=0x%x "
1613 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
1614 smb_fname_str_dbg(smb_fname), new_dos_attributes,
1615 access_mask, share_access, create_disposition,
1616 create_options, (unsigned int)unx_mode, oplock_request,
1617 (unsigned int)private_flags));
1619 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
1620 DEBUG(0, ("No smb request but not an internal only open!\n"));
1621 return NT_STATUS_INTERNAL_ERROR;
1625 * Only non-internal opens can be deferred at all
1630 if (get_deferred_open_message_state(req,
1634 struct deferred_open_record *state = (struct deferred_open_record *)ptr;
1635 /* Remember the absolute time of the original
1636 request with this mid. We'll use it later to
1637 see if this has timed out. */
1639 /* Remove the deferred open entry under lock. */
1640 remove_deferred_open_entry(
1641 state->id, req->mid,
1642 sconn_server_id(req->sconn));
1644 /* Ensure we don't reprocess this message. */
1645 remove_deferred_open_message_smb(req->mid);
1649 status = check_name(conn, smb_fname->base_name);
1650 if (!NT_STATUS_IS_OK(status)) {
1655 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
1657 existing_dos_attributes = dos_mode(conn, smb_fname);
1661 /* ignore any oplock requests if oplocks are disabled */
1662 if (!lp_oplocks(SNUM(conn)) ||
1663 IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
1664 /* Mask off everything except the private Samba bits. */
1665 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
1668 /* this is for OS/2 long file names - say we don't support them */
1669 if (!lp_posix_pathnames() && strstr(smb_fname->base_name,".+,;=[].")) {
1670 /* OS/2 Workplace shell fix may be main code stream in a later
1672 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1674 if (use_nt_status()) {
1675 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1677 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
1680 switch( create_disposition ) {
1682 * Currently we're using FILE_SUPERSEDE as the same as
1683 * FILE_OVERWRITE_IF but they really are
1684 * different. FILE_SUPERSEDE deletes an existing file
1685 * (requiring delete access) then recreates it.
1687 case FILE_SUPERSEDE:
1688 /* If file exists replace/overwrite. If file doesn't
1690 flags2 |= (O_CREAT | O_TRUNC);
1694 case FILE_OVERWRITE_IF:
1695 /* If file exists replace/overwrite. If file doesn't
1697 flags2 |= (O_CREAT | O_TRUNC);
1702 /* If file exists open. If file doesn't exist error. */
1703 if (!file_existed) {
1704 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1705 "requested for file %s and file "
1707 smb_fname_str_dbg(smb_fname)));
1709 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1713 case FILE_OVERWRITE:
1714 /* If file exists overwrite. If file doesn't exist
1716 if (!file_existed) {
1717 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1718 "requested for file %s and file "
1720 smb_fname_str_dbg(smb_fname) ));
1722 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1729 /* If file exists error. If file doesn't exist
1732 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1733 "requested for file %s and file "
1734 "already exists.\n",
1735 smb_fname_str_dbg(smb_fname)));
1736 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
1741 return map_nt_error_from_unix(errno);
1743 flags2 |= (O_CREAT|O_EXCL);
1747 /* If file exists open. If file doesn't exist
1753 return NT_STATUS_INVALID_PARAMETER;
1756 /* We only care about matching attributes on file exists and
1759 if (!posix_open && file_existed && ((create_disposition == FILE_OVERWRITE) ||
1760 (create_disposition == FILE_OVERWRITE_IF))) {
1761 if (!open_match_attributes(conn, existing_dos_attributes,
1763 smb_fname->st.st_ex_mode,
1764 unx_mode, &new_unx_mode)) {
1765 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1766 "for file %s (%x %x) (0%o, 0%o)\n",
1767 smb_fname_str_dbg(smb_fname),
1768 existing_dos_attributes,
1770 (unsigned int)smb_fname->st.st_ex_mode,
1771 (unsigned int)unx_mode ));
1773 return NT_STATUS_ACCESS_DENIED;
1777 status = calculate_access_mask(conn, smb_fname, file_existed,
1780 if (!NT_STATUS_IS_OK(status)) {
1781 DEBUG(10, ("open_file_ntcreate: calculate_access_mask "
1782 "on file %s returned %s\n",
1783 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
1787 open_access_mask = access_mask;
1789 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1790 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
1793 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1794 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
1798 * Note that we ignore the append flag as append does not
1799 * mean the same thing under DOS and Unix.
1802 if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
1803 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1804 /* DENY_DOS opens are always underlying read-write on the
1805 file handle, no matter what the requested access mask
1807 if ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
1808 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|FILE_READ_EA|FILE_EXECUTE)) {
1818 * Currently we only look at FILE_WRITE_THROUGH for create options.
1822 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
1827 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
1831 if (!posix_open && !CAN_WRITE(conn)) {
1833 * We should really return a permission denied error if either
1834 * O_CREAT or O_TRUNC are set, but for compatibility with
1835 * older versions of Samba we just AND them out.
1837 flags2 &= ~(O_CREAT|O_TRUNC);
1841 * Ensure we can't write on a read-only share or file.
1844 if (flags != O_RDONLY && file_existed &&
1845 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
1846 DEBUG(5,("open_file_ntcreate: write access requested for "
1847 "file %s on read only %s\n",
1848 smb_fname_str_dbg(smb_fname),
1849 !CAN_WRITE(conn) ? "share" : "file" ));
1851 return NT_STATUS_ACCESS_DENIED;
1854 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1855 fsp->share_access = share_access;
1856 fsp->fh->private_options = private_flags;
1857 fsp->access_mask = open_access_mask; /* We change this to the
1858 * requested access_mask after
1859 * the open is done. */
1860 fsp->posix_open = posix_open;
1862 /* Ensure no SAMBA_PRIVATE bits can be set. */
1863 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1865 if (timeval_is_zero(&request_time)) {
1866 request_time = fsp->open_time;
1870 struct share_mode_entry *batch_entry = NULL;
1871 struct share_mode_entry *exclusive_entry = NULL;
1872 bool got_level2_oplock = false;
1873 bool got_a_none_oplock = false;
1875 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
1876 id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1878 lck = get_share_mode_lock(talloc_tos(), id,
1880 smb_fname, &old_write_time);
1883 DEBUG(0, ("Could not get share mode lock\n"));
1884 return NT_STATUS_SHARING_VIOLATION;
1887 /* Get the types we need to examine. */
1888 find_oplock_types(lck,
1892 &got_a_none_oplock);
1894 /* First pass - send break only on batch oplocks. */
1895 if ((req != NULL) &&
1896 delay_for_batch_oplocks(fsp,
1900 schedule_defer_open(lck, request_time, req);
1902 return NT_STATUS_SHARING_VIOLATION;
1905 /* Use the client requested access mask here, not the one we
1907 status = open_mode_check(conn, lck, fsp->name_hash,
1908 access_mask, share_access,
1909 create_options, &file_existed);
1911 if (NT_STATUS_IS_OK(status)) {
1912 /* We might be going to allow this open. Check oplock
1914 /* Second pass - send break for both batch or
1915 * exclusive oplocks. */
1916 if ((req != NULL) &&
1917 delay_for_exclusive_oplocks(
1922 schedule_defer_open(lck, request_time, req);
1924 return NT_STATUS_SHARING_VIOLATION;
1928 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
1929 /* DELETE_PENDING is not deferred for a second */
1934 grant_fsp_oplock_type(fsp,
1939 if (!NT_STATUS_IS_OK(status)) {
1940 uint32 can_access_mask;
1941 bool can_access = True;
1943 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
1945 /* Check if this can be done with the deny_dos and fcb
1948 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
1949 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
1951 DEBUG(0, ("DOS open without an SMB "
1954 return NT_STATUS_INTERNAL_ERROR;
1957 /* Use the client requested access mask here,
1958 * not the one we open with. */
1959 status = fcb_or_dos_open(req,
1970 if (NT_STATUS_IS_OK(status)) {
1973 *pinfo = FILE_WAS_OPENED;
1975 return NT_STATUS_OK;
1980 * This next line is a subtlety we need for
1981 * MS-Access. If a file open will fail due to share
1982 * permissions and also for security (access) reasons,
1983 * we need to return the access failed error, not the
1984 * share error. We can't open the file due to kernel
1985 * oplock deadlock (it's possible we failed above on
1986 * the open_mode_check()) so use a userspace check.
1989 if (flags & O_RDWR) {
1990 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
1991 } else if (flags & O_WRONLY) {
1992 can_access_mask = FILE_WRITE_DATA;
1994 can_access_mask = FILE_READ_DATA;
1997 if (((can_access_mask & FILE_WRITE_DATA) &&
1998 !CAN_WRITE(conn)) ||
1999 !can_access_file_data(conn, smb_fname,
2005 * If we're returning a share violation, ensure we
2006 * cope with the braindead 1 second delay.
2009 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
2010 lp_defer_sharing_violations()) {
2011 struct timeval timeout;
2012 struct deferred_open_record state;
2015 /* this is a hack to speed up torture tests
2017 timeout_usecs = lp_parm_int(SNUM(conn),
2018 "smbd","sharedelay",
2019 SHARING_VIOLATION_USEC_WAIT);
2021 /* This is a relative time, added to the absolute
2022 request_time value to get the absolute timeout time.
2023 Note that if this is the second or greater time we enter
2024 this codepath for this particular request mid then
2025 request_time is left as the absolute time of the *first*
2026 time this request mid was processed. This is what allows
2027 the request to eventually time out. */
2029 timeout = timeval_set(0, timeout_usecs);
2031 /* Nothing actually uses state.delayed_for_oplocks
2032 but it's handy to differentiate in debug messages
2033 between a 30 second delay due to oplock break, and
2034 a 1 second delay for share mode conflicts. */
2036 state.delayed_for_oplocks = False;
2040 && !request_timed_out(request_time,
2042 defer_open(lck, request_time, timeout,
2050 * We have detected a sharing violation here
2051 * so return the correct error code
2053 status = NT_STATUS_SHARING_VIOLATION;
2055 status = NT_STATUS_ACCESS_DENIED;
2061 * We exit this block with the share entry *locked*.....
2065 SMB_ASSERT(!file_existed || (lck != NULL));
2068 * Ensure we pay attention to default ACLs on directories if required.
2071 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
2072 (def_acl = directory_has_default_acl(conn, parent_dir))) {
2073 unx_mode = (0777 & lp_create_mask(SNUM(conn)));
2076 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2077 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2078 (unsigned int)flags, (unsigned int)flags2,
2079 (unsigned int)unx_mode, (unsigned int)access_mask,
2080 (unsigned int)open_access_mask));
2083 * open_file strips any O_TRUNC flags itself.
2086 fsp_open = open_file(fsp, conn, req, parent_dir,
2087 flags|flags2, unx_mode, access_mask,
2090 if (!NT_STATUS_IS_OK(fsp_open)) {
2097 if (!file_existed) {
2098 struct share_mode_entry *batch_entry = NULL;
2099 struct share_mode_entry *exclusive_entry = NULL;
2100 bool got_level2_oplock = false;
2101 bool got_a_none_oplock = false;
2102 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
2104 * Deal with the race condition where two smbd's detect the
2105 * file doesn't exist and do the create at the same time. One
2106 * of them will win and set a share mode, the other (ie. this
2107 * one) should check if the requested share mode for this
2108 * create is allowed.
2112 * Now the file exists and fsp is successfully opened,
2113 * fsp->dev and fsp->inode are valid and should replace the
2114 * dev=0,inode=0 from a non existent file. Spotted by
2115 * Nadav Danieli <nadavd@exanet.com>. JRA.
2120 lck = get_share_mode_lock(talloc_tos(), id,
2122 smb_fname, &old_write_time);
2125 DEBUG(0, ("open_file_ntcreate: Could not get share "
2126 "mode lock for %s\n",
2127 smb_fname_str_dbg(smb_fname)));
2129 return NT_STATUS_SHARING_VIOLATION;
2132 /* Get the types we need to examine. */
2133 find_oplock_types(lck,
2137 &got_a_none_oplock);
2139 /* First pass - send break only on batch oplocks. */
2140 if ((req != NULL) &&
2141 delay_for_batch_oplocks(fsp,
2145 schedule_defer_open(lck, request_time, req);
2148 return NT_STATUS_SHARING_VIOLATION;
2151 status = open_mode_check(conn, lck, fsp->name_hash,
2152 access_mask, share_access,
2153 create_options, &file_existed);
2155 if (NT_STATUS_IS_OK(status)) {
2156 /* We might be going to allow this open. Check oplock
2158 /* Second pass - send break for both batch or
2159 * exclusive oplocks. */
2160 if ((req != NULL) &&
2161 delay_for_exclusive_oplocks(
2166 schedule_defer_open(lck, request_time, req);
2169 return NT_STATUS_SHARING_VIOLATION;
2173 if (!NT_STATUS_IS_OK(status)) {
2174 struct deferred_open_record state;
2178 state.delayed_for_oplocks = False;
2181 /* Do it all over again immediately. In the second
2182 * round we will find that the file existed and handle
2183 * the DELETE_PENDING and FCB cases correctly. No need
2184 * to duplicate the code here. Essentially this is a
2185 * "goto top of this function", but don't tell
2189 defer_open(lck, request_time, timeval_zero(),
2196 grant_fsp_oplock_type(fsp,
2202 * We exit this block with the share entry *locked*.....
2207 SMB_ASSERT(lck != NULL);
2209 /* Delete streams if create_disposition requires it */
2210 if (file_existed && clear_ads &&
2211 !is_ntfs_stream_smb_fname(smb_fname)) {
2212 status = delete_all_streams(conn, smb_fname->base_name);
2213 if (!NT_STATUS_IS_OK(status)) {
2220 /* note that we ignore failure for the following. It is
2221 basically a hack for NFS, and NFS will never set one of
2222 these only read them. Nobody but Samba can ever set a deny
2223 mode and we have already checked our more authoritative
2224 locking database for permission to set this deny mode. If
2225 the kernel refuses the operations then the kernel is wrong.
2226 note that GPFS supports it as well - jmcd */
2228 if (fsp->fh->fd != -1) {
2230 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
2231 if(ret_flock == -1 ){
2236 return NT_STATUS_SHARING_VIOLATION;
2241 * At this point onwards, we can guarentee that the share entry
2242 * is locked, whether we created the file or not, and that the
2243 * deny mode is compatible with all current opens.
2247 * If requested, truncate the file.
2250 if (file_existed && (flags2&O_TRUNC)) {
2252 * We are modifing the file after open - update the stat
2255 if ((SMB_VFS_FTRUNCATE(fsp, 0) == -1) ||
2256 (SMB_VFS_FSTAT(fsp, &smb_fname->st)==-1)) {
2257 status = map_nt_error_from_unix(errno);
2265 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2266 * but we don't have to store this - just ignore it on access check.
2268 fsp->access_mask = access_mask;
2271 /* stat opens on existing files don't get oplocks. */
2272 if (is_stat_open(open_access_mask)) {
2273 fsp->oplock_type = NO_OPLOCK;
2276 if (!(flags2 & O_TRUNC)) {
2277 info = FILE_WAS_OPENED;
2279 info = FILE_WAS_OVERWRITTEN;
2282 info = FILE_WAS_CREATED;
2290 * Setup the oplock info in both the shared memory and
2294 if (!set_file_oplock(fsp, fsp->oplock_type)) {
2296 * Could not get the kernel oplock or there are byte-range
2297 * locks on the file.
2299 fsp->oplock_type = NO_OPLOCK;
2302 if (info == FILE_WAS_OVERWRITTEN || info == FILE_WAS_CREATED || info == FILE_WAS_SUPERSEDED) {
2303 new_file_created = True;
2306 set_share_mode(lck, fsp, get_current_uid(conn), 0,
2309 /* Handle strange delete on close create semantics. */
2310 if (create_options & FILE_DELETE_ON_CLOSE) {
2312 status = can_set_delete_on_close(fsp, new_dos_attributes);
2314 if (!NT_STATUS_IS_OK(status)) {
2315 /* Remember to delete the mode we just added. */
2316 del_share_mode(lck, fsp);
2321 /* Note that here we set the *inital* delete on close flag,
2322 not the regular one. The magic gets handled in close. */
2323 fsp->initial_delete_on_close = True;
2326 if (new_file_created) {
2327 /* Files should be initially set as archive */
2328 if (lp_map_archive(SNUM(conn)) ||
2329 lp_store_dos_attributes(SNUM(conn))) {
2331 if (file_set_dosmode(conn, smb_fname,
2332 new_dos_attributes | aARCH,
2333 parent_dir, true) == 0) {
2334 unx_mode = smb_fname->st.st_ex_mode;
2340 /* Determine sparse flag. */
2342 /* POSIX opens are sparse by default. */
2343 fsp->is_sparse = true;
2345 fsp->is_sparse = (file_existed &&
2346 (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE));
2350 * Take care of inherited ACLs on created files - if default ACL not
2354 if (!posix_open && !file_existed && !def_acl) {
2356 int saved_errno = errno; /* We might get ENOSYS in the next
2359 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2361 errno = saved_errno; /* Ignore ENOSYS */
2364 } else if (new_unx_mode) {
2368 /* Attributes need changing. File already existed. */
2371 int saved_errno = errno; /* We might get ENOSYS in the
2373 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2375 if (ret == -1 && errno == ENOSYS) {
2376 errno = saved_errno; /* Ignore ENOSYS */
2378 DEBUG(5, ("open_file_ntcreate: reset "
2379 "attributes of file %s to 0%o\n",
2380 smb_fname_str_dbg(smb_fname),
2381 (unsigned int)new_unx_mode));
2382 ret = 0; /* Don't do the fchmod below. */
2387 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2388 DEBUG(5, ("open_file_ntcreate: failed to reset "
2389 "attributes of file %s to 0%o\n",
2390 smb_fname_str_dbg(smb_fname),
2391 (unsigned int)new_unx_mode));
2394 /* If this is a successful open, we must remove any deferred open
2397 del_deferred_open_entry(lck, req->mid,
2398 sconn_server_id(req->sconn));
2402 return NT_STATUS_OK;
2406 /****************************************************************************
2407 Open a file for for write to ensure that we can fchmod it.
2408 ****************************************************************************/
2410 NTSTATUS open_file_fchmod(connection_struct *conn,
2411 struct smb_filename *smb_fname,
2412 files_struct **result)
2414 if (!VALID_STAT(smb_fname->st)) {
2415 return NT_STATUS_INVALID_PARAMETER;
2418 return SMB_VFS_CREATE_FILE(
2421 0, /* root_dir_fid */
2422 smb_fname, /* fname */
2423 FILE_WRITE_DATA, /* access_mask */
2424 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2426 FILE_OPEN, /* create_disposition*/
2427 0, /* create_options */
2428 0, /* file_attributes */
2429 INTERNAL_OPEN_ONLY, /* oplock_request */
2430 0, /* allocation_size */
2431 0, /* private_flags */
2434 result, /* result */
2438 static NTSTATUS mkdir_internal(connection_struct *conn,
2439 struct smb_filename *smb_dname,
2440 uint32 file_attributes)
2445 bool posix_open = false;
2447 if(!CAN_WRITE(conn)) {
2448 DEBUG(5,("mkdir_internal: failing create on read-only share "
2449 "%s\n", lp_servicename(SNUM(conn))));
2450 return NT_STATUS_ACCESS_DENIED;
2453 status = check_name(conn, smb_dname->base_name);
2454 if (!NT_STATUS_IS_OK(status)) {
2458 if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
2460 return NT_STATUS_NO_MEMORY;
2463 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2465 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2467 mode = unix_mode(conn, aDIR, smb_dname, parent_dir);
2470 if (SMB_VFS_MKDIR(conn, smb_dname->base_name, mode) != 0) {
2471 return map_nt_error_from_unix(errno);
2474 /* Ensure we're checking for a symlink here.... */
2475 /* We don't want to get caught by a symlink racer. */
2477 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2478 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2479 smb_fname_str_dbg(smb_dname), strerror(errno)));
2480 return map_nt_error_from_unix(errno);
2483 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
2484 DEBUG(0, ("Directory just '%s' created is not a directory\n",
2485 smb_fname_str_dbg(smb_dname)));
2486 return NT_STATUS_ACCESS_DENIED;
2489 if (lp_store_dos_attributes(SNUM(conn))) {
2491 file_set_dosmode(conn, smb_dname,
2492 file_attributes | aDIR,
2497 if (lp_inherit_perms(SNUM(conn))) {
2498 inherit_access_posix_acl(conn, parent_dir,
2499 smb_dname->base_name, mode);
2504 * Check if high bits should have been set,
2505 * then (if bits are missing): add them.
2506 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2509 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
2510 (mode & ~smb_dname->st.st_ex_mode)) {
2511 SMB_VFS_CHMOD(conn, smb_dname->base_name,
2512 (smb_dname->st.st_ex_mode |
2513 (mode & ~smb_dname->st.st_ex_mode)));
2517 /* Change the owner if required. */
2518 if (lp_inherit_owner(SNUM(conn))) {
2519 change_dir_owner_to_parent(conn, parent_dir,
2520 smb_dname->base_name,
2524 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2525 smb_dname->base_name);
2527 return NT_STATUS_OK;
2530 /****************************************************************************
2531 Ensure we didn't get symlink raced on opening a directory.
2532 ****************************************************************************/
2534 static bool check_same_stat(const SMB_STRUCT_STAT *sbuf1,
2535 const SMB_STRUCT_STAT *sbuf2)
2537 if (sbuf1->st_ex_uid != sbuf2->st_ex_uid ||
2538 sbuf1->st_ex_gid != sbuf2->st_ex_gid ||
2539 sbuf1->st_ex_dev != sbuf2->st_ex_dev ||
2540 sbuf1->st_ex_ino != sbuf2->st_ex_ino) {
2546 /****************************************************************************
2547 Open a directory from an NT SMB call.
2548 ****************************************************************************/
2550 static NTSTATUS open_directory(connection_struct *conn,
2551 struct smb_request *req,
2552 struct smb_filename *smb_dname,
2554 uint32 share_access,
2555 uint32 create_disposition,
2556 uint32 create_options,
2557 uint32 file_attributes,
2559 files_struct **result)
2561 files_struct *fsp = NULL;
2562 bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
2563 struct share_mode_lock *lck = NULL;
2565 struct timespec mtimespec;
2568 SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname));
2570 /* Ensure we have a directory attribute. */
2571 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
2573 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2574 "share_access = 0x%x create_options = 0x%x, "
2575 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2576 smb_fname_str_dbg(smb_dname),
2577 (unsigned int)access_mask,
2578 (unsigned int)share_access,
2579 (unsigned int)create_options,
2580 (unsigned int)create_disposition,
2581 (unsigned int)file_attributes));
2583 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
2584 (conn->fs_capabilities & FILE_NAMED_STREAMS) &&
2585 is_ntfs_stream_smb_fname(smb_dname)) {
2586 DEBUG(2, ("open_directory: %s is a stream name!\n",
2587 smb_fname_str_dbg(smb_dname)));
2588 return NT_STATUS_NOT_A_DIRECTORY;
2591 status = calculate_access_mask(conn, smb_dname, dir_existed,
2592 access_mask, &access_mask);
2593 if (!NT_STATUS_IS_OK(status)) {
2594 DEBUG(10, ("open_directory: calculate_access_mask "
2595 "on file %s returned %s\n",
2596 smb_fname_str_dbg(smb_dname),
2597 nt_errstr(status)));
2601 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
2602 !security_token_has_privilege(get_current_nttok(conn),
2603 SEC_PRIV_SECURITY)) {
2604 DEBUG(10, ("open_directory: open on %s "
2605 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2606 smb_fname_str_dbg(smb_dname)));
2607 return NT_STATUS_PRIVILEGE_NOT_HELD;
2610 switch( create_disposition ) {
2614 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2617 info = FILE_WAS_OPENED;
2622 /* If directory exists error. If directory doesn't
2625 status = mkdir_internal(conn, smb_dname,
2628 if (!NT_STATUS_IS_OK(status)) {
2629 DEBUG(2, ("open_directory: unable to create "
2630 "%s. Error was %s\n",
2631 smb_fname_str_dbg(smb_dname),
2632 nt_errstr(status)));
2636 info = FILE_WAS_CREATED;
2641 * If directory exists open. If directory doesn't
2645 status = mkdir_internal(conn, smb_dname,
2648 if (NT_STATUS_IS_OK(status)) {
2649 info = FILE_WAS_CREATED;
2652 if (NT_STATUS_EQUAL(status,
2653 NT_STATUS_OBJECT_NAME_COLLISION)) {
2654 info = FILE_WAS_OPENED;
2655 status = NT_STATUS_OK;
2659 case FILE_SUPERSEDE:
2660 case FILE_OVERWRITE:
2661 case FILE_OVERWRITE_IF:
2663 DEBUG(5,("open_directory: invalid create_disposition "
2664 "0x%x for directory %s\n",
2665 (unsigned int)create_disposition,
2666 smb_fname_str_dbg(smb_dname)));
2667 return NT_STATUS_INVALID_PARAMETER;
2670 if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
2671 DEBUG(5,("open_directory: %s is not a directory !\n",
2672 smb_fname_str_dbg(smb_dname)));
2673 return NT_STATUS_NOT_A_DIRECTORY;
2676 if (info == FILE_WAS_OPENED) {
2677 uint32_t access_granted = 0;
2678 status = smbd_check_open_rights(conn, smb_dname, access_mask,
2681 /* Were we trying to do a directory open
2682 * for delete and didn't get DELETE
2683 * access (only) ? Check if the
2684 * directory allows DELETE_CHILD.
2686 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
2689 if ((NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) &&
2690 (access_mask & DELETE_ACCESS) &&
2691 (access_granted == DELETE_ACCESS) &&
2692 can_delete_file_in_directory(conn, smb_dname))) {
2693 DEBUG(10,("open_directory: overrode ACCESS_DENIED "
2694 "on directory %s\n",
2695 smb_fname_str_dbg(smb_dname)));
2696 status = NT_STATUS_OK;
2699 if (!NT_STATUS_IS_OK(status)) {
2700 DEBUG(10, ("open_directory: smbd_check_open_rights on "
2701 "file %s failed with %s\n",
2702 smb_fname_str_dbg(smb_dname),
2703 nt_errstr(status)));
2708 status = file_new(req, conn, &fsp);
2709 if(!NT_STATUS_IS_OK(status)) {
2714 * Setup the files_struct for it.
2717 fsp->mode = smb_dname->st.st_ex_mode;
2718 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
2719 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
2720 fsp->file_pid = req ? req->smbpid : 0;
2721 fsp->can_lock = False;
2722 fsp->can_read = False;
2723 fsp->can_write = False;
2725 fsp->share_access = share_access;
2726 fsp->fh->private_options = 0;
2728 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2730 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2731 fsp->print_file = NULL;
2732 fsp->modified = False;
2733 fsp->oplock_type = NO_OPLOCK;
2734 fsp->sent_oplock_break = NO_BREAK_SENT;
2735 fsp->is_directory = True;
2736 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
2737 status = fsp_set_smb_fname(fsp, smb_dname);
2738 if (!NT_STATUS_IS_OK(status)) {
2739 file_free(req, fsp);
2743 mtimespec = smb_dname->st.st_ex_mtime;
2746 status = fd_open(conn, fsp, O_RDONLY|O_DIRECTORY, 0);
2748 /* POSIX allows us to open a directory with O_RDONLY. */
2749 status = fd_open(conn, fsp, O_RDONLY, 0);
2751 if (!NT_STATUS_IS_OK(status)) {
2752 DEBUG(5, ("open_directory: Could not open fd for "
2754 smb_fname_str_dbg(smb_dname),
2755 nt_errstr(status)));
2756 file_free(req, fsp);
2760 status = vfs_stat_fsp(fsp);
2761 if (!NT_STATUS_IS_OK(status)) {
2763 file_free(req, fsp);
2767 /* Ensure there was no race condition. */
2768 if (!check_same_stat(&smb_dname->st, &fsp->fsp_name->st)) {
2769 DEBUG(5,("open_directory: stat struct differs for "
2771 smb_fname_str_dbg(smb_dname)));
2773 file_free(req, fsp);
2774 return NT_STATUS_ACCESS_DENIED;
2777 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
2778 conn->connectpath, smb_dname, &mtimespec);
2781 DEBUG(0, ("open_directory: Could not get share mode lock for "
2782 "%s\n", smb_fname_str_dbg(smb_dname)));
2784 file_free(req, fsp);
2785 return NT_STATUS_SHARING_VIOLATION;
2788 status = open_mode_check(conn, lck, fsp->name_hash,
2789 access_mask, share_access,
2790 create_options, &dir_existed);
2792 if (!NT_STATUS_IS_OK(status)) {
2795 file_free(req, fsp);
2799 set_share_mode(lck, fsp, get_current_uid(conn), 0, NO_OPLOCK);
2801 /* For directories the delete on close bit at open time seems
2802 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2803 if (create_options & FILE_DELETE_ON_CLOSE) {
2804 status = can_set_delete_on_close(fsp, 0);
2805 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
2808 file_free(req, fsp);
2812 if (NT_STATUS_IS_OK(status)) {
2813 /* Note that here we set the *inital* delete on close flag,
2814 not the regular one. The magic gets handled in close. */
2815 fsp->initial_delete_on_close = True;
2826 return NT_STATUS_OK;
2829 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
2830 struct smb_filename *smb_dname)
2835 status = SMB_VFS_CREATE_FILE(
2838 0, /* root_dir_fid */
2839 smb_dname, /* fname */
2840 FILE_READ_ATTRIBUTES, /* access_mask */
2841 FILE_SHARE_NONE, /* share_access */
2842 FILE_CREATE, /* create_disposition*/
2843 FILE_DIRECTORY_FILE, /* create_options */
2844 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
2845 0, /* oplock_request */
2846 0, /* allocation_size */
2847 0, /* private_flags */
2853 if (NT_STATUS_IS_OK(status)) {
2854 close_file(req, fsp, NORMAL_CLOSE);
2860 /****************************************************************************
2861 Receive notification that one of our open files has been renamed by another
2863 ****************************************************************************/
2865 void msg_file_was_renamed(struct messaging_context *msg,
2868 struct server_id server_id,
2871 struct smbd_server_connection *sconn;
2873 char *frm = (char *)data->data;
2875 const char *sharepath;
2876 const char *base_name;
2877 const char *stream_name;
2878 struct smb_filename *smb_fname = NULL;
2879 size_t sp_len, bn_len;
2882 sconn = msg_ctx_to_sconn(msg);
2883 if (sconn == NULL) {
2884 DEBUG(1, ("could not find sconn\n"));
2888 if (data->data == NULL
2889 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
2890 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
2891 (int)data->length));
2895 /* Unpack the message. */
2896 pull_file_id_24(frm, &id);
2897 sharepath = &frm[24];
2898 sp_len = strlen(sharepath);
2899 base_name = sharepath + sp_len + 1;
2900 bn_len = strlen(base_name);
2901 stream_name = sharepath + sp_len + 1 + bn_len + 1;
2903 /* stream_name must always be NULL if there is no stream. */
2904 if (stream_name[0] == '\0') {
2908 status = create_synthetic_smb_fname(talloc_tos(), base_name,
2909 stream_name, NULL, &smb_fname);
2910 if (!NT_STATUS_IS_OK(status)) {
2914 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
2916 sharepath, smb_fname_str_dbg(smb_fname),
2917 file_id_string_tos(&id)));
2919 for(fsp = file_find_di_first(sconn, id); fsp;
2920 fsp = file_find_di_next(fsp)) {
2921 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
2923 DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n",
2924 fsp->fnum, fsp_str_dbg(fsp),
2925 smb_fname_str_dbg(smb_fname)));
2926 status = fsp_set_smb_fname(fsp, smb_fname);
2927 if (!NT_STATUS_IS_OK(status)) {
2932 /* Now we have the complete path we can work out if this is
2933 actually within this share and adjust newname accordingly. */
2934 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
2935 "not sharepath %s) "
2936 "fnum %d from %s -> %s\n",
2937 fsp->conn->connectpath,
2941 smb_fname_str_dbg(smb_fname)));
2945 TALLOC_FREE(smb_fname);
2950 * If a main file is opened for delete, all streams need to be checked for
2951 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
2952 * If that works, delete them all by setting the delete on close and close.
2955 NTSTATUS open_streams_for_delete(connection_struct *conn,
2958 struct stream_struct *stream_info;
2959 files_struct **streams;
2961 unsigned int num_streams;
2962 TALLOC_CTX *frame = talloc_stackframe();
2965 status = SMB_VFS_STREAMINFO(conn, NULL, fname, talloc_tos(),
2966 &num_streams, &stream_info);
2968 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
2969 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
2970 DEBUG(10, ("no streams around\n"));
2972 return NT_STATUS_OK;
2975 if (!NT_STATUS_IS_OK(status)) {
2976 DEBUG(10, ("SMB_VFS_STREAMINFO failed: %s\n",
2977 nt_errstr(status)));
2981 DEBUG(10, ("open_streams_for_delete found %d streams\n",
2984 if (num_streams == 0) {
2986 return NT_STATUS_OK;
2989 streams = TALLOC_ARRAY(talloc_tos(), files_struct *, num_streams);
2990 if (streams == NULL) {
2991 DEBUG(0, ("talloc failed\n"));
2992 status = NT_STATUS_NO_MEMORY;
2996 for (i=0; i<num_streams; i++) {
2997 struct smb_filename *smb_fname = NULL;
2999 if (strequal(stream_info[i].name, "::$DATA")) {
3004 status = create_synthetic_smb_fname(talloc_tos(), fname,
3005 stream_info[i].name,
3007 if (!NT_STATUS_IS_OK(status)) {
3011 if (SMB_VFS_STAT(conn, smb_fname) == -1) {
3012 DEBUG(10, ("Unable to stat stream: %s\n",
3013 smb_fname_str_dbg(smb_fname)));
3016 status = SMB_VFS_CREATE_FILE(
3019 0, /* root_dir_fid */
3020 smb_fname, /* fname */
3021 DELETE_ACCESS, /* access_mask */
3022 (FILE_SHARE_READ | /* share_access */
3023 FILE_SHARE_WRITE | FILE_SHARE_DELETE),
3024 FILE_OPEN, /* create_disposition*/
3025 0, /* create_options */
3026 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
3027 0, /* oplock_request */
3028 0, /* allocation_size */
3029 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */
3032 &streams[i], /* result */
3035 if (!NT_STATUS_IS_OK(status)) {
3036 DEBUG(10, ("Could not open stream %s: %s\n",
3037 smb_fname_str_dbg(smb_fname),
3038 nt_errstr(status)));
3040 TALLOC_FREE(smb_fname);
3043 TALLOC_FREE(smb_fname);
3047 * don't touch the variable "status" beyond this point :-)
3050 for (i -= 1 ; i >= 0; i--) {
3051 if (streams[i] == NULL) {
3055 DEBUG(10, ("Closing stream # %d, %s\n", i,
3056 fsp_str_dbg(streams[i])));
3057 close_file(NULL, streams[i], NORMAL_CLOSE);
3066 * Wrapper around open_file_ntcreate and open_directory
3069 static NTSTATUS create_file_unixpath(connection_struct *conn,
3070 struct smb_request *req,
3071 struct smb_filename *smb_fname,
3072 uint32_t access_mask,
3073 uint32_t share_access,
3074 uint32_t create_disposition,
3075 uint32_t create_options,
3076 uint32_t file_attributes,
3077 uint32_t oplock_request,
3078 uint64_t allocation_size,
3079 uint32_t private_flags,
3080 struct security_descriptor *sd,
3081 struct ea_list *ea_list,
3083 files_struct **result,
3086 int info = FILE_WAS_OPENED;
3087 files_struct *base_fsp = NULL;
3088 files_struct *fsp = NULL;
3091 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3092 "file_attributes = 0x%x, share_access = 0x%x, "
3093 "create_disposition = 0x%x create_options = 0x%x "
3094 "oplock_request = 0x%x private_flags = 0x%x "
3095 "ea_list = 0x%p, sd = 0x%p, "
3097 (unsigned int)access_mask,
3098 (unsigned int)file_attributes,
3099 (unsigned int)share_access,
3100 (unsigned int)create_disposition,
3101 (unsigned int)create_options,
3102 (unsigned int)oplock_request,
3103 (unsigned int)private_flags,
3104 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3106 if (create_options & FILE_OPEN_BY_FILE_ID) {
3107 status = NT_STATUS_NOT_SUPPORTED;
3111 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
3112 status = NT_STATUS_INVALID_PARAMETER;
3117 oplock_request |= INTERNAL_OPEN_ONLY;
3120 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3121 && (access_mask & DELETE_ACCESS)
3122 && !is_ntfs_stream_smb_fname(smb_fname)) {
3124 * We can't open a file with DELETE access if any of the
3125 * streams is open without FILE_SHARE_DELETE
3127 status = open_streams_for_delete(conn, smb_fname->base_name);
3129 if (!NT_STATUS_IS_OK(status)) {
3134 /* This is the correct thing to do (check every time) but can_delete
3135 * is expensive (it may have to read the parent directory
3136 * permissions). So for now we're not doing it unless we have a strong
3137 * hint the client is really going to delete this file. If the client
3138 * is forcing FILE_CREATE let the filesystem take care of the
3141 /* Setting FILE_SHARE_DELETE is the hint. */
3143 if (lp_acl_check_permissions(SNUM(conn))
3144 && (create_disposition != FILE_CREATE)
3145 && (access_mask & DELETE_ACCESS)
3146 && (!(can_delete_file_in_directory(conn, smb_fname) ||
3147 can_access_file_acl(conn, smb_fname, DELETE_ACCESS)))) {
3148 status = NT_STATUS_ACCESS_DENIED;
3149 DEBUG(10,("create_file_unixpath: open file %s "
3150 "for delete ACCESS_DENIED\n",
3151 smb_fname_str_dbg(smb_fname)));
3155 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3156 !security_token_has_privilege(get_current_nttok(conn),
3157 SEC_PRIV_SECURITY)) {
3158 DEBUG(10, ("create_file_unixpath: open on %s "
3159 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3160 smb_fname_str_dbg(smb_fname)));
3161 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3165 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3166 && is_ntfs_stream_smb_fname(smb_fname)
3167 && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
3168 uint32 base_create_disposition;
3169 struct smb_filename *smb_fname_base = NULL;
3171 if (create_options & FILE_DIRECTORY_FILE) {
3172 status = NT_STATUS_NOT_A_DIRECTORY;
3176 switch (create_disposition) {
3178 base_create_disposition = FILE_OPEN;
3181 base_create_disposition = FILE_OPEN_IF;
3185 /* Create an smb_filename with stream_name == NULL. */
3186 status = create_synthetic_smb_fname(talloc_tos(),
3187 smb_fname->base_name,
3190 if (!NT_STATUS_IS_OK(status)) {
3194 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
3195 DEBUG(10, ("Unable to stat stream: %s\n",
3196 smb_fname_str_dbg(smb_fname_base)));
3199 /* Open the base file. */
3200 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
3203 | FILE_SHARE_DELETE,
3204 base_create_disposition,
3205 0, 0, 0, 0, 0, NULL, NULL,
3207 TALLOC_FREE(smb_fname_base);
3209 if (!NT_STATUS_IS_OK(status)) {
3210 DEBUG(10, ("create_file_unixpath for base %s failed: "
3211 "%s\n", smb_fname->base_name,
3212 nt_errstr(status)));
3215 /* we don't need to low level fd */
3220 * If it's a request for a directory open, deal with it separately.
3223 if (create_options & FILE_DIRECTORY_FILE) {
3225 if (create_options & FILE_NON_DIRECTORY_FILE) {
3226 status = NT_STATUS_INVALID_PARAMETER;
3230 /* Can't open a temp directory. IFS kit test. */
3231 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
3232 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
3233 status = NT_STATUS_INVALID_PARAMETER;
3238 * We will get a create directory here if the Win32
3239 * app specified a security descriptor in the
3240 * CreateDirectory() call.
3244 status = open_directory(
3245 conn, req, smb_fname, access_mask, share_access,
3246 create_disposition, create_options, file_attributes,
3251 * Ordinary file case.
3254 status = file_new(req, conn, &fsp);
3255 if(!NT_STATUS_IS_OK(status)) {
3259 status = fsp_set_smb_fname(fsp, smb_fname);
3260 if (!NT_STATUS_IS_OK(status)) {
3265 * We're opening the stream element of a base_fsp
3266 * we already opened. Set up the base_fsp pointer.
3269 fsp->base_fsp = base_fsp;
3272 status = open_file_ntcreate(conn,
3284 if(!NT_STATUS_IS_OK(status)) {
3285 file_free(req, fsp);
3289 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
3291 /* A stream open never opens a directory */
3294 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3299 * Fail the open if it was explicitly a non-directory
3303 if (create_options & FILE_NON_DIRECTORY_FILE) {
3304 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3309 status = open_directory(
3310 conn, req, smb_fname, access_mask,
3311 share_access, create_disposition,
3312 create_options, file_attributes,
3317 if (!NT_STATUS_IS_OK(status)) {
3321 fsp->base_fsp = base_fsp;
3324 * According to the MS documentation, the only time the security
3325 * descriptor is applied to the opened file is iff we *created* the
3326 * file; an existing file stays the same.
3328 * Also, it seems (from observation) that you can open the file with
3329 * any access mask but you can still write the sd. We need to override
3330 * the granted access before we call set_sd
3331 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3334 if ((sd != NULL) && (info == FILE_WAS_CREATED)
3335 && lp_nt_acl_support(SNUM(conn))) {
3337 uint32_t sec_info_sent;
3338 uint32_t saved_access_mask = fsp->access_mask;
3340 sec_info_sent = get_sec_info(sd);
3342 fsp->access_mask = FILE_GENERIC_ALL;
3344 /* Convert all the generic bits. */
3345 security_acl_map_generic(sd->dacl, &file_generic_mapping);
3346 security_acl_map_generic(sd->sacl, &file_generic_mapping);
3348 if (sec_info_sent & (SECINFO_OWNER|
3352 status = SMB_VFS_FSET_NT_ACL(fsp, sec_info_sent, sd);
3355 fsp->access_mask = saved_access_mask;
3357 if (!NT_STATUS_IS_OK(status)) {
3362 if ((ea_list != NULL) &&
3363 ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
3364 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
3365 if (!NT_STATUS_IS_OK(status)) {
3370 if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
3371 status = NT_STATUS_ACCESS_DENIED;
3375 /* Save the requested allocation size. */
3376 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
3378 && (allocation_size > fsp->fsp_name->st.st_ex_size)) {
3379 fsp->initial_allocation_size = smb_roundup(
3380 fsp->conn, allocation_size);
3381 if (fsp->is_directory) {
3382 /* Can't set allocation size on a directory. */
3383 status = NT_STATUS_ACCESS_DENIED;
3386 if (vfs_allocate_file_space(
3387 fsp, fsp->initial_allocation_size) == -1) {
3388 status = NT_STATUS_DISK_FULL;
3392 fsp->initial_allocation_size = smb_roundup(
3393 fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
3397 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
3400 if (pinfo != NULL) {
3404 smb_fname->st = fsp->fsp_name->st;
3406 return NT_STATUS_OK;
3409 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
3412 if (base_fsp && fsp->base_fsp == base_fsp) {
3414 * The close_file below will close
3419 close_file(req, fsp, ERROR_CLOSE);
3422 if (base_fsp != NULL) {
3423 close_file(req, base_fsp, ERROR_CLOSE);
3430 * Calculate the full path name given a relative fid.
3432 NTSTATUS get_relative_fid_filename(connection_struct *conn,
3433 struct smb_request *req,
3434 uint16_t root_dir_fid,
3435 const struct smb_filename *smb_fname,
3436 struct smb_filename **smb_fname_out)
3438 files_struct *dir_fsp;
3439 char *parent_fname = NULL;
3440 char *new_base_name = NULL;
3443 if (root_dir_fid == 0 || !smb_fname) {
3444 status = NT_STATUS_INTERNAL_ERROR;
3448 dir_fsp = file_fsp(req, root_dir_fid);
3450 if (dir_fsp == NULL) {
3451 status = NT_STATUS_INVALID_HANDLE;
3455 if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) {
3456 status = NT_STATUS_INVALID_HANDLE;
3460 if (!dir_fsp->is_directory) {
3463 * Check to see if this is a mac fork of some kind.
3466 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
3467 is_ntfs_stream_smb_fname(smb_fname)) {
3468 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3473 we need to handle the case when we get a
3474 relative open relative to a file and the
3475 pathname is blank - this is a reopen!
3476 (hint from demyn plantenberg)
3479 status = NT_STATUS_INVALID_HANDLE;
3483 if (ISDOT(dir_fsp->fsp_name->base_name)) {
3485 * We're at the toplevel dir, the final file name
3486 * must not contain ./, as this is filtered out
3487 * normally by srvstr_get_path and unix_convert
3488 * explicitly rejects paths containing ./.
3490 parent_fname = talloc_strdup(talloc_tos(), "");
3491 if (parent_fname == NULL) {
3492 status = NT_STATUS_NO_MEMORY;
3496 size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name);
3499 * Copy in the base directory name.
3502 parent_fname = TALLOC_ARRAY(talloc_tos(), char,
3504 if (parent_fname == NULL) {
3505 status = NT_STATUS_NO_MEMORY;
3508 memcpy(parent_fname, dir_fsp->fsp_name->base_name,
3512 * Ensure it ends in a '/'.
3513 * We used TALLOC_SIZE +2 to add space for the '/'.
3517 && (parent_fname[dir_name_len-1] != '\\')
3518 && (parent_fname[dir_name_len-1] != '/')) {
3519 parent_fname[dir_name_len] = '/';
3520 parent_fname[dir_name_len+1] = '\0';
3524 new_base_name = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
3525 smb_fname->base_name);
3526 if (new_base_name == NULL) {
3527 status = NT_STATUS_NO_MEMORY;
3531 status = filename_convert(req,
3533 req->flags2 & FLAGS2_DFS_PATHNAMES,
3538 if (!NT_STATUS_IS_OK(status)) {
3543 TALLOC_FREE(parent_fname);
3544 TALLOC_FREE(new_base_name);
3548 NTSTATUS create_file_default(connection_struct *conn,
3549 struct smb_request *req,
3550 uint16_t root_dir_fid,
3551 struct smb_filename *smb_fname,
3552 uint32_t access_mask,
3553 uint32_t share_access,
3554 uint32_t create_disposition,
3555 uint32_t create_options,
3556 uint32_t file_attributes,
3557 uint32_t oplock_request,
3558 uint64_t allocation_size,
3559 uint32_t private_flags,
3560 struct security_descriptor *sd,
3561 struct ea_list *ea_list,
3562 files_struct **result,
3565 int info = FILE_WAS_OPENED;
3566 files_struct *fsp = NULL;
3568 bool stream_name = false;
3570 DEBUG(10,("create_file: access_mask = 0x%x "
3571 "file_attributes = 0x%x, share_access = 0x%x, "
3572 "create_disposition = 0x%x create_options = 0x%x "
3573 "oplock_request = 0x%x "
3574 "private_flags = 0x%x "
3575 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
3577 (unsigned int)access_mask,
3578 (unsigned int)file_attributes,
3579 (unsigned int)share_access,
3580 (unsigned int)create_disposition,
3581 (unsigned int)create_options,
3582 (unsigned int)oplock_request,
3583 (unsigned int)private_flags,
3584 (unsigned int)root_dir_fid,
3585 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3588 * Calculate the filename from the root_dir_if if necessary.
3591 if (root_dir_fid != 0) {
3592 struct smb_filename *smb_fname_out = NULL;
3593 status = get_relative_fid_filename(conn, req, root_dir_fid,
3594 smb_fname, &smb_fname_out);
3595 if (!NT_STATUS_IS_OK(status)) {
3598 smb_fname = smb_fname_out;
3602 * Check to see if this is a mac fork of some kind.
3605 stream_name = is_ntfs_stream_smb_fname(smb_fname);
3607 enum FAKE_FILE_TYPE fake_file_type;
3609 fake_file_type = is_fake_file(smb_fname);
3611 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
3614 * Here we go! support for changing the disk quotas
3617 * We need to fake up to open this MAGIC QUOTA file
3618 * and return a valid FID.
3620 * w2k close this file directly after openening xp
3621 * also tries a QUERY_FILE_INFO on the file and then
3624 status = open_fake_file(req, conn, req->vuid,
3625 fake_file_type, smb_fname,
3627 if (!NT_STATUS_IS_OK(status)) {
3631 ZERO_STRUCT(smb_fname->st);
3635 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
3636 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
3641 /* All file access must go through check_name() */
3643 status = check_name(conn, smb_fname->base_name);
3644 if (!NT_STATUS_IS_OK(status)) {
3648 if (stream_name && is_ntfs_default_stream_smb_fname(smb_fname)) {
3650 smb_fname->stream_name = NULL;
3651 /* We have to handle this error here. */
3652 if (create_options & FILE_DIRECTORY_FILE) {
3653 status = NT_STATUS_NOT_A_DIRECTORY;
3656 if (lp_posix_pathnames()) {
3657 ret = SMB_VFS_LSTAT(conn, smb_fname);
3659 ret = SMB_VFS_STAT(conn, smb_fname);
3662 if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
3663 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3668 status = create_file_unixpath(
3669 conn, req, smb_fname, access_mask, share_access,
3670 create_disposition, create_options, file_attributes,
3671 oplock_request, allocation_size, private_flags,
3675 if (!NT_STATUS_IS_OK(status)) {
3680 DEBUG(10, ("create_file: info=%d\n", info));
3683 if (pinfo != NULL) {
3686 return NT_STATUS_OK;
3689 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
3692 close_file(req, fsp, ERROR_CLOSE);