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 "smbd/globals.h"
25 extern const struct generic_mapping file_generic_mapping;
27 struct deferred_open_record {
28 bool delayed_for_oplocks;
32 static NTSTATUS create_file_unixpath(connection_struct *conn,
33 struct smb_request *req,
34 struct smb_filename *smb_fname,
36 uint32_t share_access,
37 uint32_t create_disposition,
38 uint32_t create_options,
39 uint32_t file_attributes,
40 uint32_t oplock_request,
41 uint64_t allocation_size,
42 struct security_descriptor *sd,
43 struct ea_list *ea_list,
45 files_struct **result,
48 /****************************************************************************
49 SMB1 file varient of se_access_check. Never test FILE_READ_ATTRIBUTES.
50 ****************************************************************************/
52 NTSTATUS smb1_file_se_access_check(const struct security_descriptor *sd,
53 const NT_USER_TOKEN *token,
54 uint32_t access_desired,
55 uint32_t *access_granted)
57 return se_access_check(sd,
59 (access_desired & ~FILE_READ_ATTRIBUTES),
63 /****************************************************************************
64 Check if we have open rights.
65 ****************************************************************************/
67 static NTSTATUS check_open_rights(struct connection_struct *conn,
68 const struct smb_filename *smb_fname,
70 uint32_t *access_granted)
72 /* Check if we have rights to open. */
74 struct security_descriptor *sd;
78 if (conn->server_info->utok.uid == 0 || conn->admin_user) {
79 /* I'm sorry sir, I didn't know you were root... */
80 *access_granted = access_mask;
81 if (access_mask & SEC_FLAG_MAXIMUM_ALLOWED) {
82 *access_granted |= FILE_GENERIC_ALL;
87 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
88 (OWNER_SECURITY_INFORMATION |
89 GROUP_SECURITY_INFORMATION |
90 DACL_SECURITY_INFORMATION),&sd);
92 if (!NT_STATUS_IS_OK(status)) {
93 DEBUG(10, ("check_open_rights: Could not get acl "
95 smb_fname_str_dbg(smb_fname),
100 status = smb1_file_se_access_check(sd,
101 conn->server_info->ptok,
107 DEBUG(10,("check_open_rights: file %s requesting "
108 "0x%x returning 0x%x (%s)\n",
109 smb_fname_str_dbg(smb_fname),
110 (unsigned int)access_mask,
111 (unsigned int)*access_granted,
112 nt_errstr(status) ));
117 /****************************************************************************
118 fd support routines - attempt to do a dos_open.
119 ****************************************************************************/
121 static NTSTATUS fd_open(struct connection_struct *conn,
122 struct smb_filename *smb_fname,
127 NTSTATUS status = NT_STATUS_OK;
131 * Never follow symlinks on a POSIX client. The
132 * client should be doing this.
135 if (fsp->posix_open || !lp_symlinks(SNUM(conn))) {
140 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, mode);
141 if (fsp->fh->fd == -1) {
142 status = map_nt_error_from_unix(errno);
143 if (errno == EMFILE) {
144 static time_t last_warned = 0L;
146 if (time((time_t *) NULL) > last_warned) {
147 DEBUG(0,("Too many open files, unable "
148 "to open more! smbd's max "
150 lp_max_open_files()));
151 last_warned = time((time_t *) NULL);
157 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
158 smb_fname_str_dbg(smb_fname), flags, (int)mode, fsp->fh->fd,
159 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
164 /****************************************************************************
165 Close the file associated with a fsp.
166 ****************************************************************************/
168 NTSTATUS fd_close(files_struct *fsp)
172 if (fsp->fh->fd == -1) {
173 return NT_STATUS_OK; /* What we used to call a stat open. */
175 if (fsp->fh->ref_count > 1) {
176 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
179 ret = SMB_VFS_CLOSE(fsp);
182 return map_nt_error_from_unix(errno);
187 /****************************************************************************
188 Change the ownership of a file to that of the parent directory.
189 Do this by fd if possible.
190 ****************************************************************************/
192 void change_file_owner_to_parent(connection_struct *conn,
193 const char *inherit_from_dir,
196 struct smb_filename *smb_fname_parent = NULL;
200 status = create_synthetic_smb_fname(talloc_tos(), inherit_from_dir,
201 NULL, NULL, &smb_fname_parent);
202 if (!NT_STATUS_IS_OK(status)) {
206 ret = SMB_VFS_STAT(conn, smb_fname_parent);
208 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
209 "directory %s. Error was %s\n",
210 smb_fname_str_dbg(smb_fname_parent),
216 ret = SMB_VFS_FCHOWN(fsp, smb_fname_parent->st.st_ex_uid, (gid_t)-1);
219 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
220 "file %s to parent directory uid %u. Error "
221 "was %s\n", fsp->fsp_name,
222 (unsigned int)smb_fname_parent->st.st_ex_uid,
226 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
227 "parent directory uid %u.\n", fsp->fsp_name,
228 (unsigned int)smb_fname_parent->st.st_ex_uid));
230 TALLOC_FREE(smb_fname_parent);
233 NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
234 const char *inherit_from_dir,
236 SMB_STRUCT_STAT *psbuf)
238 struct smb_filename *smb_fname_parent = NULL;
239 struct smb_filename *smb_fname_cwd = NULL;
240 char *saved_dir = NULL;
241 TALLOC_CTX *ctx = talloc_tos();
242 NTSTATUS status = NT_STATUS_OK;
245 status = create_synthetic_smb_fname(ctx, inherit_from_dir, NULL, NULL,
247 if (!NT_STATUS_IS_OK(status)) {
251 ret = SMB_VFS_STAT(conn, smb_fname_parent);
253 status = map_nt_error_from_unix(errno);
254 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
255 "directory %s. Error was %s\n",
256 smb_fname_str_dbg(smb_fname_parent),
261 /* We've already done an lstat into psbuf, and we know it's a
262 directory. If we can cd into the directory and the dev/ino
263 are the same then we can safely chown without races as
264 we're locking the directory in place by being in it. This
265 should work on any UNIX (thanks tridge :-). JRA.
268 saved_dir = vfs_GetWd(ctx,conn);
270 status = map_nt_error_from_unix(errno);
271 DEBUG(0,("change_dir_owner_to_parent: failed to get "
272 "current working directory. Error was %s\n",
277 /* Chdir into the new path. */
278 if (vfs_ChDir(conn, fname) == -1) {
279 status = map_nt_error_from_unix(errno);
280 DEBUG(0,("change_dir_owner_to_parent: failed to change "
281 "current working directory to %s. Error "
282 "was %s\n", fname, strerror(errno) ));
286 status = create_synthetic_smb_fname(ctx, ".", NULL, NULL,
288 if (!NT_STATUS_IS_OK(status)) {
292 ret = SMB_VFS_STAT(conn, smb_fname_cwd);
294 status = map_nt_error_from_unix(errno);
295 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
296 "directory '.' (%s) Error was %s\n",
297 fname, strerror(errno)));
301 /* Ensure we're pointing at the same place. */
302 if (smb_fname_cwd->st.st_ex_dev != psbuf->st_ex_dev ||
303 smb_fname_cwd->st.st_ex_ino != psbuf->st_ex_ino ||
304 smb_fname_cwd->st.st_ex_mode != psbuf->st_ex_mode ) {
305 DEBUG(0,("change_dir_owner_to_parent: "
306 "device/inode/mode on directory %s changed. "
307 "Refusing to chown !\n", fname ));
308 status = NT_STATUS_ACCESS_DENIED;
313 ret = SMB_VFS_CHOWN(conn, ".", smb_fname_parent->st.st_ex_uid,
317 status = map_nt_error_from_unix(errno);
318 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
319 "directory %s to parent directory uid %u. "
320 "Error was %s\n", fname,
321 (unsigned int)smb_fname_parent->st.st_ex_uid,
326 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
327 "directory %s to parent directory uid %u.\n",
328 fname, (unsigned int)smb_fname_parent->st.st_ex_uid ));
331 vfs_ChDir(conn,saved_dir);
333 TALLOC_FREE(smb_fname_parent);
334 TALLOC_FREE(smb_fname_cwd);
338 /****************************************************************************
340 ****************************************************************************/
342 static NTSTATUS open_file(files_struct *fsp,
343 connection_struct *conn,
344 struct smb_request *req,
345 const char *parent_dir,
346 struct smb_filename *smb_fname,
349 uint32 access_mask, /* client requested access mask. */
350 uint32 open_access_mask) /* what we're actually using in the open. */
353 NTSTATUS status = NT_STATUS_OK;
354 int accmode = (flags & O_ACCMODE);
355 int local_flags = flags;
356 bool file_existed = VALID_STAT(smb_fname->st);
361 status = get_full_smb_filename(talloc_tos(), smb_fname,
363 if (!NT_STATUS_IS_OK(status)) {
367 /* Check permissions */
370 * This code was changed after seeing a client open request
371 * containing the open mode of (DENY_WRITE/read-only) with
372 * the 'create if not exist' bit set. The previous code
373 * would fail to open the file read only on a read-only share
374 * as it was checking the flags parameter directly against O_RDONLY,
375 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
379 if (!CAN_WRITE(conn)) {
380 /* It's a read-only share - fail if we wanted to write. */
381 if(accmode != O_RDONLY || (flags & O_TRUNC) || (flags & O_APPEND)) {
382 DEBUG(3,("Permission denied opening %s\n",
383 smb_fname_str_dbg(smb_fname)));
384 return NT_STATUS_ACCESS_DENIED;
385 } else if(flags & O_CREAT) {
386 /* We don't want to write - but we must make sure that
387 O_CREAT doesn't create the file if we have write
388 access into the directory.
390 flags &= ~(O_CREAT|O_EXCL);
391 local_flags &= ~(O_CREAT|O_EXCL);
396 * This little piece of insanity is inspired by the
397 * fact that an NT client can open a file for O_RDONLY,
398 * but set the create disposition to FILE_EXISTS_TRUNCATE.
399 * If the client *can* write to the file, then it expects to
400 * truncate the file, even though it is opening for readonly.
401 * Quicken uses this stupid trick in backup file creation...
402 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
403 * for helping track this one down. It didn't bite us in 2.0.x
404 * as we always opened files read-write in that release. JRA.
407 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
408 DEBUG(10,("open_file: truncate requested on read-only open "
409 "for file %s\n", smb_fname_str_dbg(smb_fname)));
410 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
413 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
414 (!file_existed && (local_flags & O_CREAT)) ||
415 ((local_flags & O_TRUNC) == O_TRUNC) ) {
419 * We can't actually truncate here as the file may be locked.
420 * open_file_ntcreate will take care of the truncate later. JRA.
423 local_flags &= ~O_TRUNC;
425 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
427 * We would block on opening a FIFO with no one else on the
428 * other end. Do what we used to do and add O_NONBLOCK to the
432 if (file_existed && S_ISFIFO(smb_fname->st.st_ex_mode)) {
433 local_flags |= O_NONBLOCK;
437 /* Don't create files with Microsoft wildcard characters. */
440 * wildcard characters are allowed in stream names
441 * only test the basefilename
443 wild = fsp->base_fsp->fsp_name;
447 if ((local_flags & O_CREAT) && !file_existed &&
449 return NT_STATUS_OBJECT_NAME_INVALID;
452 /* Actually do the open */
453 status = fd_open(conn, smb_fname, fsp, local_flags, unx_mode);
454 if (!NT_STATUS_IS_OK(status)) {
455 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
456 "(flags=%d)\n", smb_fname_str_dbg(smb_fname),
457 nt_errstr(status),local_flags,flags));
461 if ((local_flags & O_CREAT) && !file_existed) {
463 /* Inherit the ACL if required */
464 if (lp_inherit_perms(SNUM(conn))) {
465 inherit_access_posix_acl(conn, parent_dir, path,
469 /* Change the owner if required. */
470 if (lp_inherit_owner(SNUM(conn))) {
471 change_file_owner_to_parent(conn, parent_dir,
475 notify_fname(conn, NOTIFY_ACTION_ADDED,
476 FILE_NOTIFY_CHANGE_FILE_NAME, path);
480 fsp->fh->fd = -1; /* What we used to call a stat open. */
482 uint32_t access_granted = 0;
484 status = check_open_rights(conn,
488 if (!NT_STATUS_IS_OK(status)) {
489 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
491 * On NT_STATUS_ACCESS_DENIED, access_granted
492 * contains the denied bits.
495 if ((access_mask & FILE_WRITE_ATTRIBUTES) &&
496 (access_granted & FILE_WRITE_ATTRIBUTES) &&
497 (lp_map_readonly(SNUM(conn)) ||
498 lp_map_archive(SNUM(conn)) ||
499 lp_map_hidden(SNUM(conn)) ||
500 lp_map_system(SNUM(conn)))) {
501 access_granted &= ~FILE_WRITE_ATTRIBUTES;
503 DEBUG(10,("open_file: "
512 if ((access_mask & DELETE_ACCESS) &&
513 (access_granted & DELETE_ACCESS) &&
514 can_delete_file_in_directory(conn,
516 /* Were we trying to do a stat open
517 * for delete and didn't get DELETE
518 * access (only) ? Check if the
519 * directory allows DELETE_CHILD.
521 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
524 access_granted &= ~DELETE_ACCESS;
526 DEBUG(10,("open_file: "
534 if (access_granted != 0) {
535 DEBUG(10,("open_file: Access "
542 } else if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
544 S_ISLNK(smb_fname->st.st_ex_mode)) {
545 /* This is a POSIX stat open for delete
546 * or rename on a symlink that points
548 DEBUG(10,("open_file: allowing POSIX "
549 "open on bad symlink %s\n",
553 DEBUG(10,("open_file: "
554 "check_open_rights on file "
556 smb_fname_str_dbg(smb_fname),
557 nt_errstr(status) ));
567 if (fsp->fh->fd == -1) {
568 ret = SMB_VFS_STAT(conn, smb_fname);
570 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
571 /* If we have an fd, this stat should succeed. */
573 DEBUG(0,("Error doing fstat on open file %s "
575 smb_fname_str_dbg(smb_fname),
580 /* For a non-io open, this stat failing means file not found. JRA */
582 status = map_nt_error_from_unix(errno);
589 * POSIX allows read-only opens of directories. We don't
590 * want to do this (we use a different code path for this)
591 * so catch a directory open and return an EISDIR. JRA.
594 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
597 return NT_STATUS_FILE_IS_A_DIRECTORY;
600 fsp->mode = smb_fname->st.st_ex_mode;
601 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
602 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
603 fsp->file_pid = req ? req->smbpid : 0;
604 fsp->can_lock = True;
605 fsp->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
606 if (!CAN_WRITE(conn)) {
607 fsp->can_write = False;
609 fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ?
612 fsp->print_file = False;
613 fsp->modified = False;
614 fsp->sent_oplock_break = NO_BREAK_SENT;
615 fsp->is_directory = False;
616 if (conn->aio_write_behind_list &&
617 is_in_path(smb_fname->base_name, conn->aio_write_behind_list,
618 conn->case_sensitive)) {
619 fsp->aio_write_behind = True;
622 string_set(&fsp->fsp_name, path);
623 fsp->wcp = NULL; /* Write cache pointer. */
625 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
626 conn->server_info->unix_name,
628 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
629 conn->num_files_open));
635 /*******************************************************************
636 Return True if the filename is one of the special executable types.
637 ********************************************************************/
639 bool is_executable(const char *fname)
641 if ((fname = strrchr_m(fname,'.'))) {
642 if (strequal(fname,".com") ||
643 strequal(fname,".dll") ||
644 strequal(fname,".exe") ||
645 strequal(fname,".sym")) {
652 /****************************************************************************
653 Check if we can open a file with a share mode.
654 Returns True if conflict, False if not.
655 ****************************************************************************/
657 static bool share_conflict(struct share_mode_entry *entry,
661 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
662 "entry->share_access = 0x%x, "
663 "entry->private_options = 0x%x\n",
664 (unsigned int)entry->access_mask,
665 (unsigned int)entry->share_access,
666 (unsigned int)entry->private_options));
668 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
669 (unsigned int)access_mask, (unsigned int)share_access));
671 if ((entry->access_mask & (FILE_WRITE_DATA|
675 DELETE_ACCESS)) == 0) {
676 DEBUG(10,("share_conflict: No conflict due to "
677 "entry->access_mask = 0x%x\n",
678 (unsigned int)entry->access_mask ));
682 if ((access_mask & (FILE_WRITE_DATA|
686 DELETE_ACCESS)) == 0) {
687 DEBUG(10,("share_conflict: No conflict due to "
688 "access_mask = 0x%x\n",
689 (unsigned int)access_mask ));
693 #if 1 /* JRA TEST - Superdebug. */
694 #define CHECK_MASK(num, am, right, sa, share) \
695 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
696 (unsigned int)(num), (unsigned int)(am), \
697 (unsigned int)(right), (unsigned int)(am)&(right) )); \
698 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
699 (unsigned int)(num), (unsigned int)(sa), \
700 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
701 if (((am) & (right)) && !((sa) & (share))) { \
702 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
703 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
704 (unsigned int)(share) )); \
708 #define CHECK_MASK(num, am, right, sa, share) \
709 if (((am) & (right)) && !((sa) & (share))) { \
710 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
711 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
712 (unsigned int)(share) )); \
717 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
718 share_access, FILE_SHARE_WRITE);
719 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
720 entry->share_access, FILE_SHARE_WRITE);
722 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
723 share_access, FILE_SHARE_READ);
724 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
725 entry->share_access, FILE_SHARE_READ);
727 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
728 share_access, FILE_SHARE_DELETE);
729 CHECK_MASK(6, access_mask, DELETE_ACCESS,
730 entry->share_access, FILE_SHARE_DELETE);
732 DEBUG(10,("share_conflict: No conflict.\n"));
736 #if defined(DEVELOPER)
737 static void validate_my_share_entries(int num,
738 struct share_mode_entry *share_entry)
742 if (!procid_is_me(&share_entry->pid)) {
746 if (is_deferred_open_entry(share_entry) &&
747 !open_was_deferred(share_entry->op_mid)) {
748 char *str = talloc_asprintf(talloc_tos(),
749 "Got a deferred entry without a request: "
751 share_mode_str(talloc_tos(), num, share_entry));
755 if (!is_valid_share_mode_entry(share_entry)) {
759 fsp = file_find_dif(share_entry->id,
760 share_entry->share_file_id);
762 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
763 share_mode_str(talloc_tos(), num, share_entry) ));
764 smb_panic("validate_my_share_entries: Cannot match a "
765 "share entry with an open file\n");
768 if (is_deferred_open_entry(share_entry) ||
769 is_unused_share_mode_entry(share_entry)) {
773 if ((share_entry->op_type == NO_OPLOCK) &&
774 (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK)) {
775 /* Someone has already written to it, but I haven't yet
780 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
789 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
790 share_mode_str(talloc_tos(), num, share_entry) ));
791 str = talloc_asprintf(talloc_tos(),
792 "validate_my_share_entries: "
793 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
794 fsp->fsp_name, (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,
820 uint32 create_options,
825 if(lck->num_share_modes == 0) {
829 *file_existed = True;
831 /* A delete on close prohibits everything */
833 if (lck->delete_on_close) {
834 return NT_STATUS_DELETE_PENDING;
837 if (is_stat_open(access_mask)) {
838 /* Stat open that doesn't trigger oplock breaks or share mode
839 * checks... ! JRA. */
844 * Check if the share modes will give us access.
847 #if defined(DEVELOPER)
848 for(i = 0; i < lck->num_share_modes; i++) {
849 validate_my_share_entries(i, &lck->share_modes[i]);
853 if (!lp_share_modes(SNUM(conn))) {
857 /* Now we check the share modes, after any oplock breaks. */
858 for(i = 0; i < lck->num_share_modes; i++) {
860 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
864 /* someone else has a share lock on it, check to see if we can
866 if (share_conflict(&lck->share_modes[i],
867 access_mask, share_access)) {
868 return NT_STATUS_SHARING_VIOLATION;
875 static bool is_delete_request(files_struct *fsp) {
876 return ((fsp->access_mask == DELETE_ACCESS) &&
877 (fsp->oplock_type == NO_OPLOCK));
881 * Send a break message to the oplock holder and delay the open for
885 static NTSTATUS send_break_message(files_struct *fsp,
886 struct share_mode_entry *exclusive,
891 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
893 DEBUG(10, ("Sending break request to PID %s\n",
894 procid_str_static(&exclusive->pid)));
895 exclusive->op_mid = mid;
897 /* Create the message. */
898 share_mode_entry_to_message(msg, exclusive);
900 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
901 don't want this set in the share mode struct pointed to by lck. */
903 if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
904 SSVAL(msg,6,exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
907 status = messaging_send_buf(smbd_messaging_context(), exclusive->pid,
908 MSG_SMB_BREAK_REQUEST,
910 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
911 if (!NT_STATUS_IS_OK(status)) {
912 DEBUG(3, ("Could not send oplock break message: %s\n",
920 * 1) No files open at all or internal open: Grant whatever the client wants.
922 * 2) Exclusive (or batch) oplock around: If the requested access is a delete
923 * request, break if the oplock around is a batch oplock. If it's another
924 * requested access type, break.
926 * 3) Only level2 around: Grant level2 and do nothing else.
929 static bool delay_for_oplocks(struct share_mode_lock *lck,
936 struct share_mode_entry *exclusive = NULL;
937 bool valid_entry = false;
938 bool have_level2 = false;
939 bool have_a_none_oplock = false;
940 bool allow_level2 = (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
941 lp_level2_oplocks(SNUM(fsp->conn));
943 if (oplock_request & INTERNAL_OPEN_ONLY) {
944 fsp->oplock_type = NO_OPLOCK;
947 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
951 for (i=0; i<lck->num_share_modes; i++) {
953 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
957 /* At least one entry is not an invalid or deferred entry. */
960 if (pass_number == 1) {
961 if (BATCH_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
962 SMB_ASSERT(exclusive == NULL);
963 exclusive = &lck->share_modes[i];
966 if (EXCLUSIVE_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
967 SMB_ASSERT(exclusive == NULL);
968 exclusive = &lck->share_modes[i];
972 if (LEVEL_II_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
973 SMB_ASSERT(exclusive == NULL);
977 if (lck->share_modes[i].op_type == NO_OPLOCK) {
978 have_a_none_oplock = true;
982 if (exclusive != NULL) { /* Found an exclusive oplock */
983 bool delay_it = is_delete_request(fsp) ?
984 BATCH_OPLOCK_TYPE(exclusive->op_type) : true;
985 SMB_ASSERT(!have_level2);
987 send_break_message(fsp, exclusive, mid, oplock_request);
993 * Match what was requested (fsp->oplock_type) with
994 * what was found in the existing share modes.
998 /* All entries are placeholders or deferred.
999 * Directly grant whatever the client wants. */
1000 if (fsp->oplock_type == NO_OPLOCK) {
1001 /* Store a level2 oplock, but don't tell the client */
1002 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1004 } else if (have_a_none_oplock) {
1005 fsp->oplock_type = NO_OPLOCK;
1006 } else if (have_level2) {
1007 if (fsp->oplock_type == NO_OPLOCK ||
1008 fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
1009 /* Store a level2 oplock, but don't tell the client */
1010 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1012 fsp->oplock_type = LEVEL_II_OPLOCK;
1015 /* This case can never happen. */
1020 * Don't grant level2 to clients that don't want them
1021 * or if we've turned them off.
1023 if (fsp->oplock_type == LEVEL_II_OPLOCK && !allow_level2) {
1024 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1027 DEBUG(10,("delay_for_oplocks: oplock type 0x%x on file %s\n",
1028 fsp->oplock_type, fsp->fsp_name));
1034 bool request_timed_out(struct timeval request_time,
1035 struct timeval timeout)
1037 struct timeval now, end_time;
1039 end_time = timeval_sum(&request_time, &timeout);
1040 return (timeval_compare(&end_time, &now) < 0);
1043 /****************************************************************************
1044 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1045 ****************************************************************************/
1047 static void defer_open(struct share_mode_lock *lck,
1048 struct timeval request_time,
1049 struct timeval timeout,
1050 struct smb_request *req,
1051 struct deferred_open_record *state)
1055 /* Paranoia check */
1057 for (i=0; i<lck->num_share_modes; i++) {
1058 struct share_mode_entry *e = &lck->share_modes[i];
1060 if (!is_deferred_open_entry(e)) {
1064 if (procid_is_me(&e->pid) && (e->op_mid == req->mid)) {
1065 DEBUG(0, ("Trying to defer an already deferred "
1066 "request: mid=%d, exiting\n", req->mid));
1067 exit_server("attempt to defer a deferred request");
1071 /* End paranoia check */
1073 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1074 "open entry for mid %u\n",
1075 (unsigned int)request_time.tv_sec,
1076 (unsigned int)request_time.tv_usec,
1077 (unsigned int)req->mid));
1079 if (!push_deferred_smb_message(req, request_time, timeout,
1080 (char *)state, sizeof(*state))) {
1081 exit_server("push_deferred_smb_message failed");
1083 add_deferred_open(lck, req->mid, request_time, state->id);
1087 /****************************************************************************
1088 On overwrite open ensure that the attributes match.
1089 ****************************************************************************/
1091 bool open_match_attributes(connection_struct *conn,
1092 uint32 old_dos_attr,
1093 uint32 new_dos_attr,
1094 mode_t existing_unx_mode,
1095 mode_t new_unx_mode,
1096 mode_t *returned_unx_mode)
1098 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
1100 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1101 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1103 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
1104 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
1105 *returned_unx_mode = new_unx_mode;
1107 *returned_unx_mode = (mode_t)0;
1110 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1111 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1112 "returned_unx_mode = 0%o\n",
1113 (unsigned int)old_dos_attr,
1114 (unsigned int)existing_unx_mode,
1115 (unsigned int)new_dos_attr,
1116 (unsigned int)*returned_unx_mode ));
1118 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1119 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1120 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
1121 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
1125 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1126 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
1127 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
1134 /****************************************************************************
1135 Special FCB or DOS processing in the case of a sharing violation.
1136 Try and find a duplicated file handle.
1137 ****************************************************************************/
1139 NTSTATUS fcb_or_dos_open(struct smb_request *req,
1140 connection_struct *conn,
1141 files_struct *fsp_to_dup_into,
1147 uint32 share_access,
1148 uint32 create_options)
1152 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1153 "file %s.\n", fname ));
1155 for(fsp = file_find_di_first(id); fsp;
1156 fsp = file_find_di_next(fsp)) {
1158 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1159 "vuid = %u, file_pid = %u, private_options = 0x%x "
1160 "access_mask = 0x%x\n", fsp->fsp_name,
1161 fsp->fh->fd, (unsigned int)fsp->vuid,
1162 (unsigned int)fsp->file_pid,
1163 (unsigned int)fsp->fh->private_options,
1164 (unsigned int)fsp->access_mask ));
1166 if (fsp->fh->fd != -1 &&
1167 fsp->vuid == vuid &&
1168 fsp->file_pid == file_pid &&
1169 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
1170 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
1171 (fsp->access_mask & FILE_WRITE_DATA) &&
1172 strequal(fsp->fsp_name, fname)) {
1173 DEBUG(10,("fcb_or_dos_open: file match\n"));
1179 return NT_STATUS_NOT_FOUND;
1182 /* quite an insane set of semantics ... */
1183 if (is_executable(fname) &&
1184 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1185 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1186 return NT_STATUS_INVALID_PARAMETER;
1189 /* We need to duplicate this fsp. */
1190 dup_file_fsp(req, fsp, access_mask, share_access,
1191 create_options, fsp_to_dup_into);
1193 return NT_STATUS_OK;
1196 /****************************************************************************
1197 Open a file with a share mode - old openX method - map into NTCreate.
1198 ****************************************************************************/
1200 bool map_open_params_to_ntcreate(const char *fname, int deny_mode, int open_func,
1201 uint32 *paccess_mask,
1202 uint32 *pshare_mode,
1203 uint32 *pcreate_disposition,
1204 uint32 *pcreate_options)
1208 uint32 create_disposition;
1209 uint32 create_options = FILE_NON_DIRECTORY_FILE;
1211 DEBUG(10,("map_open_params_to_ntcreate: fname = %s, deny_mode = 0x%x, "
1212 "open_func = 0x%x\n",
1213 fname, (unsigned int)deny_mode, (unsigned int)open_func ));
1215 /* Create the NT compatible access_mask. */
1216 switch (GET_OPENX_MODE(deny_mode)) {
1217 case DOS_OPEN_EXEC: /* Implies read-only - used to be FILE_READ_DATA */
1218 case DOS_OPEN_RDONLY:
1219 access_mask = FILE_GENERIC_READ;
1221 case DOS_OPEN_WRONLY:
1222 access_mask = FILE_GENERIC_WRITE;
1226 access_mask = FILE_GENERIC_READ|FILE_GENERIC_WRITE;
1229 DEBUG(10,("map_open_params_to_ntcreate: bad open mode = 0x%x\n",
1230 (unsigned int)GET_OPENX_MODE(deny_mode)));
1234 /* Create the NT compatible create_disposition. */
1235 switch (open_func) {
1236 case OPENX_FILE_EXISTS_FAIL|OPENX_FILE_CREATE_IF_NOT_EXIST:
1237 create_disposition = FILE_CREATE;
1240 case OPENX_FILE_EXISTS_OPEN:
1241 create_disposition = FILE_OPEN;
1244 case OPENX_FILE_EXISTS_OPEN|OPENX_FILE_CREATE_IF_NOT_EXIST:
1245 create_disposition = FILE_OPEN_IF;
1248 case OPENX_FILE_EXISTS_TRUNCATE:
1249 create_disposition = FILE_OVERWRITE;
1252 case OPENX_FILE_EXISTS_TRUNCATE|OPENX_FILE_CREATE_IF_NOT_EXIST:
1253 create_disposition = FILE_OVERWRITE_IF;
1257 /* From samba4 - to be confirmed. */
1258 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_EXEC) {
1259 create_disposition = FILE_CREATE;
1262 DEBUG(10,("map_open_params_to_ntcreate: bad "
1263 "open_func 0x%x\n", (unsigned int)open_func));
1267 /* Create the NT compatible share modes. */
1268 switch (GET_DENY_MODE(deny_mode)) {
1270 share_mode = FILE_SHARE_NONE;
1274 share_mode = FILE_SHARE_READ;
1278 share_mode = FILE_SHARE_WRITE;
1282 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1286 create_options |= NTCREATEX_OPTIONS_PRIVATE_DENY_DOS;
1287 if (is_executable(fname)) {
1288 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1290 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_RDONLY) {
1291 share_mode = FILE_SHARE_READ;
1293 share_mode = FILE_SHARE_NONE;
1299 create_options |= NTCREATEX_OPTIONS_PRIVATE_DENY_FCB;
1300 share_mode = FILE_SHARE_NONE;
1304 DEBUG(10,("map_open_params_to_ntcreate: bad deny_mode 0x%x\n",
1305 (unsigned int)GET_DENY_MODE(deny_mode) ));
1309 DEBUG(10,("map_open_params_to_ntcreate: file %s, access_mask = 0x%x, "
1310 "share_mode = 0x%x, create_disposition = 0x%x, "
1311 "create_options = 0x%x\n",
1313 (unsigned int)access_mask,
1314 (unsigned int)share_mode,
1315 (unsigned int)create_disposition,
1316 (unsigned int)create_options ));
1319 *paccess_mask = access_mask;
1322 *pshare_mode = share_mode;
1324 if (pcreate_disposition) {
1325 *pcreate_disposition = create_disposition;
1327 if (pcreate_options) {
1328 *pcreate_options = create_options;
1335 static void schedule_defer_open(struct share_mode_lock *lck,
1336 struct timeval request_time,
1337 struct smb_request *req)
1339 struct deferred_open_record state;
1341 /* This is a relative time, added to the absolute
1342 request_time value to get the absolute timeout time.
1343 Note that if this is the second or greater time we enter
1344 this codepath for this particular request mid then
1345 request_time is left as the absolute time of the *first*
1346 time this request mid was processed. This is what allows
1347 the request to eventually time out. */
1349 struct timeval timeout;
1351 /* Normally the smbd we asked should respond within
1352 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1353 * the client did, give twice the timeout as a safety
1354 * measure here in case the other smbd is stuck
1355 * somewhere else. */
1357 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1359 /* Nothing actually uses state.delayed_for_oplocks
1360 but it's handy to differentiate in debug messages
1361 between a 30 second delay due to oplock break, and
1362 a 1 second delay for share mode conflicts. */
1364 state.delayed_for_oplocks = True;
1367 if (!request_timed_out(request_time, timeout)) {
1368 defer_open(lck, request_time, timeout, req, &state);
1372 /****************************************************************************
1373 Work out what access_mask to use from what the client sent us.
1374 ****************************************************************************/
1376 static NTSTATUS calculate_access_mask(connection_struct *conn,
1377 const struct smb_filename *smb_fname,
1379 uint32_t access_mask,
1380 uint32_t *access_mask_out)
1385 * Convert GENERIC bits to specific bits.
1388 se_map_generic(&access_mask, &file_generic_mapping);
1390 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1391 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1394 struct security_descriptor *sd;
1395 uint32_t access_granted = 0;
1397 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
1398 (OWNER_SECURITY_INFORMATION |
1399 GROUP_SECURITY_INFORMATION |
1400 DACL_SECURITY_INFORMATION),&sd);
1402 if (!NT_STATUS_IS_OK(status)) {
1403 DEBUG(10, ("calculate_access_mask: Could not get acl "
1405 smb_fname_str_dbg(smb_fname),
1406 nt_errstr(status)));
1407 return NT_STATUS_ACCESS_DENIED;
1410 status = smb1_file_se_access_check(sd,
1411 conn->server_info->ptok,
1417 if (!NT_STATUS_IS_OK(status)) {
1418 DEBUG(10, ("calculate_access_mask: Access denied on "
1419 "file %s: when calculating maximum access\n",
1420 smb_fname_str_dbg(smb_fname)));
1421 return NT_STATUS_ACCESS_DENIED;
1424 access_mask = access_granted;
1426 access_mask = FILE_GENERIC_ALL;
1430 *access_mask_out = access_mask;
1431 return NT_STATUS_OK;
1434 /****************************************************************************
1435 Open a file with a share mode. Passed in an already created files_struct *.
1436 ****************************************************************************/
1438 static NTSTATUS open_file_ntcreate(connection_struct *conn,
1439 struct smb_request *req,
1440 struct smb_filename *smb_fname,
1441 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1442 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1443 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1444 uint32 create_options, /* options such as delete on close. */
1445 uint32 new_dos_attributes, /* attributes used for new file. */
1446 int oplock_request, /* internal Samba oplock codes. */
1447 /* Information (FILE_EXISTS etc.) */
1453 bool file_existed = VALID_STAT(smb_fname->st);
1454 bool def_acl = False;
1455 bool posix_open = False;
1456 bool new_file_created = False;
1457 bool clear_ads = false;
1459 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1460 mode_t new_unx_mode = (mode_t)0;
1461 mode_t unx_mode = (mode_t)0;
1463 uint32 existing_dos_attributes = 0;
1464 struct pending_message_list *pml = NULL;
1465 struct timeval request_time = timeval_zero();
1466 struct share_mode_lock *lck = NULL;
1467 uint32 open_access_mask = access_mask;
1475 status = get_full_smb_filename(talloc_tos(), smb_fname,
1477 if (!NT_STATUS_IS_OK(status)) {
1481 if (conn->printer) {
1483 * Printers are handled completely differently.
1484 * Most of the passed parameters are ignored.
1488 *pinfo = FILE_WAS_CREATED;
1491 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1492 smb_fname_str_dbg(smb_fname)));
1495 DEBUG(0,("open_file_ntcreate: printer open without "
1496 "an SMB request!\n"));
1497 return NT_STATUS_INTERNAL_ERROR;
1500 return print_fsp_open(req, conn, fname, req->vuid, fsp,
1504 if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
1506 return NT_STATUS_NO_MEMORY;
1509 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1511 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1512 new_dos_attributes = 0;
1514 /* We add aARCH to this as this mode is only used if the file is
1516 unx_mode = unix_mode(conn, new_dos_attributes | aARCH,
1517 smb_fname, parent_dir);
1520 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1521 "access_mask=0x%x share_access=0x%x "
1522 "create_disposition = 0x%x create_options=0x%x "
1523 "unix mode=0%o oplock_request=%d\n",
1524 smb_fname_str_dbg(smb_fname), new_dos_attributes,
1525 access_mask, share_access, create_disposition,
1526 create_options, (unsigned int)unx_mode, oplock_request));
1528 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
1529 DEBUG(0, ("No smb request but not an internal only open!\n"));
1530 return NT_STATUS_INTERNAL_ERROR;
1534 * Only non-internal opens can be deferred at all
1538 && ((pml = get_open_deferred_message(req->mid)) != NULL)) {
1539 struct deferred_open_record *state =
1540 (struct deferred_open_record *)pml->private_data.data;
1542 /* Remember the absolute time of the original
1543 request with this mid. We'll use it later to
1544 see if this has timed out. */
1546 request_time = pml->request_time;
1548 /* Remove the deferred open entry under lock. */
1549 lck = get_share_mode_lock(talloc_tos(), state->id, NULL, NULL,
1552 DEBUG(0, ("could not get share mode lock\n"));
1554 del_deferred_open_entry(lck, req->mid);
1558 /* Ensure we don't reprocess this message. */
1559 remove_deferred_open_smb_message(req->mid);
1562 status = check_name(conn, smb_fname->base_name);
1563 if (!NT_STATUS_IS_OK(status)) {
1568 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
1570 existing_dos_attributes = dos_mode(conn, smb_fname);
1574 /* ignore any oplock requests if oplocks are disabled */
1575 if (!lp_oplocks(SNUM(conn)) || global_client_failed_oplock_break ||
1576 IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
1577 /* Mask off everything except the private Samba bits. */
1578 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
1581 /* this is for OS/2 long file names - say we don't support them */
1582 if (!lp_posix_pathnames() && strstr(smb_fname->base_name,".+,;=[].")) {
1583 /* OS/2 Workplace shell fix may be main code stream in a later
1585 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1587 if (use_nt_status()) {
1588 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1590 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
1593 switch( create_disposition ) {
1595 * Currently we're using FILE_SUPERSEDE as the same as
1596 * FILE_OVERWRITE_IF but they really are
1597 * different. FILE_SUPERSEDE deletes an existing file
1598 * (requiring delete access) then recreates it.
1600 case FILE_SUPERSEDE:
1601 /* If file exists replace/overwrite. If file doesn't
1603 flags2 |= (O_CREAT | O_TRUNC);
1607 case FILE_OVERWRITE_IF:
1608 /* If file exists replace/overwrite. If file doesn't
1610 flags2 |= (O_CREAT | O_TRUNC);
1615 /* If file exists open. If file doesn't exist error. */
1616 if (!file_existed) {
1617 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1618 "requested for file %s and file "
1620 smb_fname_str_dbg(smb_fname)));
1622 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1626 case FILE_OVERWRITE:
1627 /* If file exists overwrite. If file doesn't exist
1629 if (!file_existed) {
1630 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1631 "requested for file %s and file "
1633 smb_fname_str_dbg(smb_fname) ));
1635 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1642 /* If file exists error. If file doesn't exist
1645 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1646 "requested for file %s and file "
1647 "already exists.\n",
1648 smb_fname_str_dbg(smb_fname)));
1649 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
1654 return map_nt_error_from_unix(errno);
1656 flags2 |= (O_CREAT|O_EXCL);
1660 /* If file exists open. If file doesn't exist
1666 return NT_STATUS_INVALID_PARAMETER;
1669 /* We only care about matching attributes on file exists and
1672 if (!posix_open && file_existed && ((create_disposition == FILE_OVERWRITE) ||
1673 (create_disposition == FILE_OVERWRITE_IF))) {
1674 if (!open_match_attributes(conn, existing_dos_attributes,
1676 smb_fname->st.st_ex_mode,
1677 unx_mode, &new_unx_mode)) {
1678 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1679 "for file %s (%x %x) (0%o, 0%o)\n",
1680 smb_fname_str_dbg(smb_fname),
1681 existing_dos_attributes,
1683 (unsigned int)smb_fname->st.st_ex_mode,
1684 (unsigned int)unx_mode ));
1686 return NT_STATUS_ACCESS_DENIED;
1690 status = calculate_access_mask(conn, smb_fname, file_existed,
1693 if (!NT_STATUS_IS_OK(status)) {
1694 DEBUG(10, ("open_file_ntcreate: calculate_access_mask "
1695 "on file %s returned %s\n",
1696 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
1700 open_access_mask = access_mask;
1702 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1703 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
1706 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1707 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
1711 * Note that we ignore the append flag as append does not
1712 * mean the same thing under DOS and Unix.
1715 if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
1716 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1717 /* DENY_DOS opens are always underlying read-write on the
1718 file handle, no matter what the requested access mask
1720 if ((create_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
1721 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|FILE_READ_EA|FILE_EXECUTE)) {
1731 * Currently we only look at FILE_WRITE_THROUGH for create options.
1735 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
1740 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
1744 if (!posix_open && !CAN_WRITE(conn)) {
1746 * We should really return a permission denied error if either
1747 * O_CREAT or O_TRUNC are set, but for compatibility with
1748 * older versions of Samba we just AND them out.
1750 flags2 &= ~(O_CREAT|O_TRUNC);
1754 * Ensure we can't write on a read-only share or file.
1757 if (flags != O_RDONLY && file_existed &&
1758 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
1759 DEBUG(5,("open_file_ntcreate: write access requested for "
1760 "file %s on read only %s\n",
1761 smb_fname_str_dbg(smb_fname),
1762 !CAN_WRITE(conn) ? "share" : "file" ));
1764 return NT_STATUS_ACCESS_DENIED;
1767 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1768 fsp->share_access = share_access;
1769 fsp->fh->private_options = create_options;
1770 fsp->access_mask = open_access_mask; /* We change this to the
1771 * requested access_mask after
1772 * the open is done. */
1773 fsp->posix_open = posix_open;
1775 /* Ensure no SAMBA_PRIVATE bits can be set. */
1776 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1778 if (timeval_is_zero(&request_time)) {
1779 request_time = fsp->open_time;
1783 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
1784 id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1786 lck = get_share_mode_lock(talloc_tos(), id,
1788 fname, &old_write_time);
1791 DEBUG(0, ("Could not get share mode lock\n"));
1792 return NT_STATUS_SHARING_VIOLATION;
1795 /* First pass - send break only on batch oplocks. */
1797 && delay_for_oplocks(lck, fsp, req->mid, 1,
1799 schedule_defer_open(lck, request_time, req);
1801 return NT_STATUS_SHARING_VIOLATION;
1804 /* Use the client requested access mask here, not the one we
1806 status = open_mode_check(conn, lck, access_mask, share_access,
1807 create_options, &file_existed);
1809 if (NT_STATUS_IS_OK(status)) {
1810 /* We might be going to allow this open. Check oplock
1812 /* Second pass - send break for both batch or
1813 * exclusive oplocks. */
1815 && delay_for_oplocks(lck, fsp, req->mid, 2,
1817 schedule_defer_open(lck, request_time, req);
1819 return NT_STATUS_SHARING_VIOLATION;
1823 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
1824 /* DELETE_PENDING is not deferred for a second */
1829 if (!NT_STATUS_IS_OK(status)) {
1830 uint32 can_access_mask;
1831 bool can_access = True;
1833 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
1835 /* Check if this can be done with the deny_dos and fcb
1837 if (create_options &
1838 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
1839 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
1841 DEBUG(0, ("DOS open without an SMB "
1844 return NT_STATUS_INTERNAL_ERROR;
1847 /* Use the client requested access mask here,
1848 * not the one we open with. */
1849 status = fcb_or_dos_open(req,
1860 if (NT_STATUS_IS_OK(status)) {
1863 *pinfo = FILE_WAS_OPENED;
1865 return NT_STATUS_OK;
1870 * This next line is a subtlety we need for
1871 * MS-Access. If a file open will fail due to share
1872 * permissions and also for security (access) reasons,
1873 * we need to return the access failed error, not the
1874 * share error. We can't open the file due to kernel
1875 * oplock deadlock (it's possible we failed above on
1876 * the open_mode_check()) so use a userspace check.
1879 if (flags & O_RDWR) {
1880 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
1881 } else if (flags & O_WRONLY) {
1882 can_access_mask = FILE_WRITE_DATA;
1884 can_access_mask = FILE_READ_DATA;
1887 if (((can_access_mask & FILE_WRITE_DATA) &&
1888 !CAN_WRITE(conn)) ||
1889 !can_access_file_data(conn, smb_fname,
1895 * If we're returning a share violation, ensure we
1896 * cope with the braindead 1 second delay.
1899 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
1900 lp_defer_sharing_violations()) {
1901 struct timeval timeout;
1902 struct deferred_open_record state;
1905 /* this is a hack to speed up torture tests
1907 timeout_usecs = lp_parm_int(SNUM(conn),
1908 "smbd","sharedelay",
1909 SHARING_VIOLATION_USEC_WAIT);
1911 /* This is a relative time, added to the absolute
1912 request_time value to get the absolute timeout time.
1913 Note that if this is the second or greater time we enter
1914 this codepath for this particular request mid then
1915 request_time is left as the absolute time of the *first*
1916 time this request mid was processed. This is what allows
1917 the request to eventually time out. */
1919 timeout = timeval_set(0, timeout_usecs);
1921 /* Nothing actually uses state.delayed_for_oplocks
1922 but it's handy to differentiate in debug messages
1923 between a 30 second delay due to oplock break, and
1924 a 1 second delay for share mode conflicts. */
1926 state.delayed_for_oplocks = False;
1930 && !request_timed_out(request_time,
1932 defer_open(lck, request_time, timeout,
1940 * We have detected a sharing violation here
1941 * so return the correct error code
1943 status = NT_STATUS_SHARING_VIOLATION;
1945 status = NT_STATUS_ACCESS_DENIED;
1951 * We exit this block with the share entry *locked*.....
1955 SMB_ASSERT(!file_existed || (lck != NULL));
1958 * Ensure we pay attention to default ACLs on directories if required.
1961 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
1962 (def_acl = directory_has_default_acl(conn, parent_dir))) {
1966 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
1967 "access_mask = 0x%x, open_access_mask = 0x%x\n",
1968 (unsigned int)flags, (unsigned int)flags2,
1969 (unsigned int)unx_mode, (unsigned int)access_mask,
1970 (unsigned int)open_access_mask));
1973 * open_file strips any O_TRUNC flags itself.
1976 fsp_open = open_file(fsp, conn, req, parent_dir, smb_fname,
1977 flags|flags2, unx_mode, access_mask,
1980 if (!NT_STATUS_IS_OK(fsp_open)) {
1987 if (!file_existed) {
1988 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
1990 * Deal with the race condition where two smbd's detect the
1991 * file doesn't exist and do the create at the same time. One
1992 * of them will win and set a share mode, the other (ie. this
1993 * one) should check if the requested share mode for this
1994 * create is allowed.
1998 * Now the file exists and fsp is successfully opened,
1999 * fsp->dev and fsp->inode are valid and should replace the
2000 * dev=0,inode=0 from a non existent file. Spotted by
2001 * Nadav Danieli <nadavd@exanet.com>. JRA.
2006 lck = get_share_mode_lock(talloc_tos(), id,
2008 fname, &old_write_time);
2011 DEBUG(0, ("open_file_ntcreate: Could not get share "
2012 "mode lock for %s\n",
2013 smb_fname_str_dbg(smb_fname)));
2015 return NT_STATUS_SHARING_VIOLATION;
2018 /* First pass - send break only on batch oplocks. */
2020 && delay_for_oplocks(lck, fsp, req->mid, 1,
2022 schedule_defer_open(lck, request_time, req);
2025 return NT_STATUS_SHARING_VIOLATION;
2028 status = open_mode_check(conn, lck, access_mask, share_access,
2029 create_options, &file_existed);
2031 if (NT_STATUS_IS_OK(status)) {
2032 /* We might be going to allow this open. Check oplock
2034 /* Second pass - send break for both batch or
2035 * exclusive oplocks. */
2037 && delay_for_oplocks(lck, fsp, req->mid, 2,
2039 schedule_defer_open(lck, request_time, req);
2042 return NT_STATUS_SHARING_VIOLATION;
2046 if (!NT_STATUS_IS_OK(status)) {
2047 struct deferred_open_record state;
2051 state.delayed_for_oplocks = False;
2054 /* Do it all over again immediately. In the second
2055 * round we will find that the file existed and handle
2056 * the DELETE_PENDING and FCB cases correctly. No need
2057 * to duplicate the code here. Essentially this is a
2058 * "goto top of this function", but don't tell
2062 defer_open(lck, request_time, timeval_zero(),
2070 * We exit this block with the share entry *locked*.....
2075 SMB_ASSERT(lck != NULL);
2077 /* Delete streams if create_disposition requires it */
2078 if (file_existed && clear_ads &&
2079 !is_ntfs_stream_smb_fname(smb_fname)) {
2080 status = delete_all_streams(conn, smb_fname->base_name);
2081 if (!NT_STATUS_IS_OK(status)) {
2088 /* note that we ignore failure for the following. It is
2089 basically a hack for NFS, and NFS will never set one of
2090 these only read them. Nobody but Samba can ever set a deny
2091 mode and we have already checked our more authoritative
2092 locking database for permission to set this deny mode. If
2093 the kernel refuses the operations then the kernel is wrong.
2094 note that GPFS supports it as well - jmcd */
2096 if (fsp->fh->fd != -1) {
2097 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access);
2098 if(ret_flock == -1 ){
2103 return NT_STATUS_SHARING_VIOLATION;
2108 * At this point onwards, we can guarentee that the share entry
2109 * is locked, whether we created the file or not, and that the
2110 * deny mode is compatible with all current opens.
2114 * If requested, truncate the file.
2117 if (flags2&O_TRUNC) {
2119 * We are modifing the file after open - update the stat
2122 if ((SMB_VFS_FTRUNCATE(fsp, 0) == -1) ||
2123 (SMB_VFS_FSTAT(fsp, &smb_fname->st)==-1)) {
2124 status = map_nt_error_from_unix(errno);
2131 /* Record the options we were opened with. */
2132 fsp->share_access = share_access;
2133 fsp->fh->private_options = create_options;
2135 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2137 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2140 /* stat opens on existing files don't get oplocks. */
2141 if (is_stat_open(open_access_mask)) {
2142 fsp->oplock_type = NO_OPLOCK;
2145 if (!(flags2 & O_TRUNC)) {
2146 info = FILE_WAS_OPENED;
2148 info = FILE_WAS_OVERWRITTEN;
2151 info = FILE_WAS_CREATED;
2159 * Setup the oplock info in both the shared memory and
2163 if (!set_file_oplock(fsp, fsp->oplock_type)) {
2164 /* Could not get the kernel oplock */
2165 fsp->oplock_type = NO_OPLOCK;
2168 if (info == FILE_WAS_OVERWRITTEN || info == FILE_WAS_CREATED || info == FILE_WAS_SUPERSEDED) {
2169 new_file_created = True;
2172 set_share_mode(lck, fsp, conn->server_info->utok.uid, 0,
2175 /* Handle strange delete on close create semantics. */
2176 if (create_options & FILE_DELETE_ON_CLOSE) {
2178 status = can_set_delete_on_close(fsp, True, new_dos_attributes);
2180 if (!NT_STATUS_IS_OK(status)) {
2181 /* Remember to delete the mode we just added. */
2182 del_share_mode(lck, fsp);
2187 /* Note that here we set the *inital* delete on close flag,
2188 not the regular one. The magic gets handled in close. */
2189 fsp->initial_delete_on_close = True;
2192 if (new_file_created) {
2193 /* Files should be initially set as archive */
2194 if (lp_map_archive(SNUM(conn)) ||
2195 lp_store_dos_attributes(SNUM(conn))) {
2197 if (file_set_dosmode(conn, smb_fname,
2198 new_dos_attributes | aARCH,
2199 parent_dir, true) == 0) {
2200 unx_mode = smb_fname->st.st_ex_mode;
2207 * Take care of inherited ACLs on created files - if default ACL not
2211 if (!posix_open && !file_existed && !def_acl) {
2213 int saved_errno = errno; /* We might get ENOSYS in the next
2216 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2218 errno = saved_errno; /* Ignore ENOSYS */
2221 } else if (new_unx_mode) {
2225 /* Attributes need changing. File already existed. */
2228 int saved_errno = errno; /* We might get ENOSYS in the
2230 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2232 if (ret == -1 && errno == ENOSYS) {
2233 errno = saved_errno; /* Ignore ENOSYS */
2235 DEBUG(5, ("open_file_ntcreate: reset "
2236 "attributes of file %s to 0%o\n",
2237 smb_fname_str_dbg(smb_fname),
2238 (unsigned int)new_unx_mode));
2239 ret = 0; /* Don't do the fchmod below. */
2244 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2245 DEBUG(5, ("open_file_ntcreate: failed to reset "
2246 "attributes of file %s to 0%o\n",
2247 smb_fname_str_dbg(smb_fname),
2248 (unsigned int)new_unx_mode));
2251 /* If this is a successful open, we must remove any deferred open
2254 del_deferred_open_entry(lck, req->mid);
2258 return NT_STATUS_OK;
2262 /****************************************************************************
2263 Open a file for for write to ensure that we can fchmod it.
2264 ****************************************************************************/
2266 NTSTATUS open_file_fchmod(struct smb_request *req, connection_struct *conn,
2267 struct smb_filename *smb_fname,
2268 files_struct **result)
2270 files_struct *fsp = NULL;
2273 if (!VALID_STAT(smb_fname->st)) {
2274 return NT_STATUS_INVALID_PARAMETER;
2277 status = file_new(req, conn, &fsp);
2278 if(!NT_STATUS_IS_OK(status)) {
2282 status = SMB_VFS_CREATE_FILE(
2285 0, /* root_dir_fid */
2286 smb_fname, /* fname */
2287 FILE_WRITE_DATA, /* access_mask */
2288 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2290 FILE_OPEN, /* create_disposition*/
2291 0, /* create_options */
2292 0, /* file_attributes */
2293 0, /* oplock_request */
2294 0, /* allocation_size */
2301 * This is not a user visible file open.
2302 * Don't set a share mode.
2305 if (!NT_STATUS_IS_OK(status)) {
2306 file_free(req, fsp);
2311 return NT_STATUS_OK;
2314 /****************************************************************************
2315 Close the fchmod file fd - ensure no locks are lost.
2316 ****************************************************************************/
2318 NTSTATUS close_file_fchmod(struct smb_request *req, files_struct *fsp)
2320 NTSTATUS status = fd_close(fsp);
2321 file_free(req, fsp);
2325 static NTSTATUS mkdir_internal(connection_struct *conn,
2326 struct smb_filename *smb_dname,
2327 uint32 file_attributes)
2332 bool posix_open = false;
2334 if(!CAN_WRITE(conn)) {
2335 DEBUG(5,("mkdir_internal: failing create on read-only share "
2336 "%s\n", lp_servicename(SNUM(conn))));
2337 return NT_STATUS_ACCESS_DENIED;
2340 status = check_name(conn, smb_dname->base_name);
2341 if (!NT_STATUS_IS_OK(status)) {
2345 if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
2347 return NT_STATUS_NO_MEMORY;
2350 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2352 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2354 mode = unix_mode(conn, aDIR, smb_dname, parent_dir);
2357 if (SMB_VFS_MKDIR(conn, smb_dname->base_name, mode) != 0) {
2358 return map_nt_error_from_unix(errno);
2361 /* Ensure we're checking for a symlink here.... */
2362 /* We don't want to get caught by a symlink racer. */
2364 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2365 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2366 smb_fname_str_dbg(smb_dname), strerror(errno)));
2367 return map_nt_error_from_unix(errno);
2370 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
2371 DEBUG(0, ("Directory just '%s' created is not a directory\n",
2372 smb_fname_str_dbg(smb_dname)));
2373 return NT_STATUS_ACCESS_DENIED;
2376 if (lp_store_dos_attributes(SNUM(conn))) {
2378 file_set_dosmode(conn, smb_dname,
2379 file_attributes | aDIR,
2384 if (lp_inherit_perms(SNUM(conn))) {
2385 inherit_access_posix_acl(conn, parent_dir,
2386 smb_dname->base_name, mode);
2389 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
2391 * Check if high bits should have been set,
2392 * then (if bits are missing): add them.
2393 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2396 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
2397 (mode & ~smb_dname->st.st_ex_mode)) {
2398 SMB_VFS_CHMOD(conn, smb_dname->base_name,
2399 (smb_dname->st.st_ex_mode |
2400 (mode & ~smb_dname->st.st_ex_mode)));
2404 /* Change the owner if required. */
2405 if (lp_inherit_owner(SNUM(conn))) {
2406 change_dir_owner_to_parent(conn, parent_dir,
2407 smb_dname->base_name,
2411 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2412 smb_dname->base_name);
2414 return NT_STATUS_OK;
2417 /****************************************************************************
2418 Open a directory from an NT SMB call.
2419 ****************************************************************************/
2421 static NTSTATUS open_directory(connection_struct *conn,
2422 struct smb_request *req,
2423 struct smb_filename *smb_dname,
2425 uint32 share_access,
2426 uint32 create_disposition,
2427 uint32 create_options,
2428 uint32 file_attributes,
2430 files_struct **result)
2432 files_struct *fsp = NULL;
2433 bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
2434 struct share_mode_lock *lck = NULL;
2436 struct timespec mtimespec;
2439 SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname));
2441 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2442 "share_access = 0x%x create_options = 0x%x, "
2443 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2444 smb_fname_str_dbg(smb_dname),
2445 (unsigned int)access_mask,
2446 (unsigned int)share_access,
2447 (unsigned int)create_options,
2448 (unsigned int)create_disposition,
2449 (unsigned int)file_attributes));
2451 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
2452 (conn->fs_capabilities & FILE_NAMED_STREAMS) &&
2453 is_ntfs_stream_smb_fname(smb_dname)) {
2454 DEBUG(2, ("open_directory: %s is a stream name!\n",
2455 smb_fname_str_dbg(smb_dname)));
2456 return NT_STATUS_NOT_A_DIRECTORY;
2459 status = calculate_access_mask(conn, smb_dname, dir_existed,
2460 access_mask, &access_mask);
2461 if (!NT_STATUS_IS_OK(status)) {
2462 DEBUG(10, ("open_directory: calculate_access_mask "
2463 "on file %s returned %s\n",
2464 smb_fname_str_dbg(smb_dname),
2465 nt_errstr(status)));
2469 /* We need to support SeSecurityPrivilege for this. */
2470 if (access_mask & SEC_FLAG_SYSTEM_SECURITY) {
2471 DEBUG(10, ("open_directory: open on %s "
2472 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2473 smb_fname_str_dbg(smb_dname)));
2474 return NT_STATUS_PRIVILEGE_NOT_HELD;
2477 switch( create_disposition ) {
2480 info = FILE_WAS_OPENED;
2483 * We want to follow symlinks here.
2486 if (SMB_VFS_STAT(conn, smb_dname) != 0) {
2487 return map_nt_error_from_unix(errno);
2494 /* If directory exists error. If directory doesn't
2497 status = mkdir_internal(conn, smb_dname,
2500 if (!NT_STATUS_IS_OK(status)) {
2501 DEBUG(2, ("open_directory: unable to create "
2502 "%s. Error was %s\n",
2503 smb_fname_str_dbg(smb_dname),
2504 nt_errstr(status)));
2508 info = FILE_WAS_CREATED;
2513 * If directory exists open. If directory doesn't
2517 status = mkdir_internal(conn, smb_dname,
2520 if (NT_STATUS_IS_OK(status)) {
2521 info = FILE_WAS_CREATED;
2524 if (NT_STATUS_EQUAL(status,
2525 NT_STATUS_OBJECT_NAME_COLLISION)) {
2526 info = FILE_WAS_OPENED;
2527 status = NT_STATUS_OK;
2532 case FILE_SUPERSEDE:
2533 case FILE_OVERWRITE:
2534 case FILE_OVERWRITE_IF:
2536 DEBUG(5,("open_directory: invalid create_disposition "
2537 "0x%x for directory %s\n",
2538 (unsigned int)create_disposition,
2539 smb_fname_str_dbg(smb_dname)));
2540 return NT_STATUS_INVALID_PARAMETER;
2543 if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
2544 DEBUG(5,("open_directory: %s is not a directory !\n",
2545 smb_fname_str_dbg(smb_dname)));
2546 return NT_STATUS_NOT_A_DIRECTORY;
2549 if (info == FILE_WAS_OPENED) {
2550 uint32_t access_granted = 0;
2551 status = check_open_rights(conn, smb_dname, access_mask,
2554 /* Were we trying to do a directory open
2555 * for delete and didn't get DELETE
2556 * access (only) ? Check if the
2557 * directory allows DELETE_CHILD.
2559 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
2562 if ((NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) &&
2563 (access_mask & DELETE_ACCESS) &&
2564 (access_granted == DELETE_ACCESS) &&
2565 can_delete_file_in_directory(conn, smb_dname))) {
2566 DEBUG(10,("open_directory: overrode ACCESS_DENIED "
2567 "on directory %s\n",
2568 smb_fname_str_dbg(smb_dname)));
2569 status = NT_STATUS_OK;
2572 if (!NT_STATUS_IS_OK(status)) {
2573 DEBUG(10, ("open_directory: check_open_rights on "
2574 "file %s failed with %s\n",
2575 smb_fname_str_dbg(smb_dname),
2576 nt_errstr(status)));
2581 status = file_new(req, conn, &fsp);
2582 if(!NT_STATUS_IS_OK(status)) {
2587 * Setup the files_struct for it.
2590 fsp->mode = smb_dname->st.st_ex_mode;
2591 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
2592 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
2593 fsp->file_pid = req ? req->smbpid : 0;
2594 fsp->can_lock = False;
2595 fsp->can_read = False;
2596 fsp->can_write = False;
2598 fsp->share_access = share_access;
2599 fsp->fh->private_options = create_options;
2601 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2603 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2604 fsp->print_file = False;
2605 fsp->modified = False;
2606 fsp->oplock_type = NO_OPLOCK;
2607 fsp->sent_oplock_break = NO_BREAK_SENT;
2608 fsp->is_directory = True;
2609 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
2611 string_set(&fsp->fsp_name, smb_dname->base_name);
2613 mtimespec = smb_dname->st.st_ex_mtime;
2615 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
2616 conn->connectpath, smb_dname->base_name,
2620 DEBUG(0, ("open_directory: Could not get share mode lock for "
2621 "%s\n", smb_fname_str_dbg(smb_dname)));
2622 file_free(req, fsp);
2623 return NT_STATUS_SHARING_VIOLATION;
2626 status = open_mode_check(conn, lck, access_mask, share_access,
2627 create_options, &dir_existed);
2629 if (!NT_STATUS_IS_OK(status)) {
2631 file_free(req, fsp);
2635 set_share_mode(lck, fsp, conn->server_info->utok.uid, 0, NO_OPLOCK);
2637 /* For directories the delete on close bit at open time seems
2638 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2639 if (create_options & FILE_DELETE_ON_CLOSE) {
2640 status = can_set_delete_on_close(fsp, True, 0);
2641 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
2643 file_free(req, fsp);
2647 if (NT_STATUS_IS_OK(status)) {
2648 /* Note that here we set the *inital* delete on close flag,
2649 not the regular one. The magic gets handled in close. */
2650 fsp->initial_delete_on_close = True;
2661 return NT_STATUS_OK;
2664 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
2665 struct smb_filename *smb_dname)
2670 status = SMB_VFS_CREATE_FILE(
2673 0, /* root_dir_fid */
2674 smb_dname, /* fname */
2675 FILE_READ_ATTRIBUTES, /* access_mask */
2676 FILE_SHARE_NONE, /* share_access */
2677 FILE_CREATE, /* create_disposition*/
2678 FILE_DIRECTORY_FILE, /* create_options */
2679 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
2680 0, /* oplock_request */
2681 0, /* allocation_size */
2687 if (NT_STATUS_IS_OK(status)) {
2688 close_file(req, fsp, NORMAL_CLOSE);
2694 /****************************************************************************
2695 Receive notification that one of our open files has been renamed by another
2697 ****************************************************************************/
2699 void msg_file_was_renamed(struct messaging_context *msg,
2702 struct server_id server_id,
2706 char *frm = (char *)data->data;
2708 const char *sharepath;
2709 const char *newname;
2712 if (data->data == NULL
2713 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
2714 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
2715 (int)data->length));
2719 /* Unpack the message. */
2720 pull_file_id_24(frm, &id);
2721 sharepath = &frm[24];
2722 newname = sharepath + strlen(sharepath) + 1;
2723 sp_len = strlen(sharepath);
2725 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
2727 sharepath, newname, file_id_string_tos(&id)));
2729 for(fsp = file_find_di_first(id); fsp; fsp = file_find_di_next(fsp)) {
2730 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
2731 DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n",
2732 fsp->fnum, fsp->fsp_name, newname ));
2733 string_set(&fsp->fsp_name, newname);
2736 /* Now we have the complete path we can work out if this is
2737 actually within this share and adjust newname accordingly. */
2738 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
2739 "not sharepath %s) "
2740 "fnum %d from %s -> %s\n",
2741 fsp->conn->connectpath,
2750 struct case_semantics_state {
2751 connection_struct *conn;
2752 bool case_sensitive;
2754 bool short_case_preserve;
2757 /****************************************************************************
2758 Restore case semantics.
2759 ****************************************************************************/
2760 static int restore_case_semantics(struct case_semantics_state *state)
2762 state->conn->case_sensitive = state->case_sensitive;
2763 state->conn->case_preserve = state->case_preserve;
2764 state->conn->short_case_preserve = state->short_case_preserve;
2768 /****************************************************************************
2769 Save case semantics.
2770 ****************************************************************************/
2771 struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
2772 connection_struct *conn)
2774 struct case_semantics_state *result;
2776 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
2777 DEBUG(0, ("talloc failed\n"));
2781 result->conn = conn;
2782 result->case_sensitive = conn->case_sensitive;
2783 result->case_preserve = conn->case_preserve;
2784 result->short_case_preserve = conn->short_case_preserve;
2787 conn->case_sensitive = True;
2788 conn->case_preserve = True;
2789 conn->short_case_preserve = True;
2791 talloc_set_destructor(result, restore_case_semantics);
2797 * If a main file is opened for delete, all streams need to be checked for
2798 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
2799 * If that works, delete them all by setting the delete on close and close.
2802 NTSTATUS open_streams_for_delete(connection_struct *conn,
2805 struct stream_struct *stream_info;
2806 files_struct **streams;
2808 unsigned int num_streams;
2809 TALLOC_CTX *frame = talloc_stackframe();
2812 status = SMB_VFS_STREAMINFO(conn, NULL, fname, talloc_tos(),
2813 &num_streams, &stream_info);
2815 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
2816 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
2817 DEBUG(10, ("no streams around\n"));
2819 return NT_STATUS_OK;
2822 if (!NT_STATUS_IS_OK(status)) {
2823 DEBUG(10, ("SMB_VFS_STREAMINFO failed: %s\n",
2824 nt_errstr(status)));
2828 DEBUG(10, ("open_streams_for_delete found %d streams\n",
2831 if (num_streams == 0) {
2833 return NT_STATUS_OK;
2836 streams = TALLOC_ARRAY(talloc_tos(), files_struct *, num_streams);
2837 if (streams == NULL) {
2838 DEBUG(0, ("talloc failed\n"));
2839 status = NT_STATUS_NO_MEMORY;
2843 for (i=0; i<num_streams; i++) {
2844 struct smb_filename *smb_fname = NULL;
2846 if (strequal(stream_info[i].name, "::$DATA")) {
2851 status = create_synthetic_smb_fname(talloc_tos(), fname,
2852 stream_info[i].name,
2854 if (!NT_STATUS_IS_OK(status)) {
2858 if (SMB_VFS_STAT(conn, smb_fname) == -1) {
2859 DEBUG(10, ("Unable to stat stream: %s\n",
2860 smb_fname_str_dbg(smb_fname)));
2863 status = SMB_VFS_CREATE_FILE(
2866 0, /* root_dir_fid */
2867 smb_fname, /* fname */
2868 DELETE_ACCESS, /* access_mask */
2869 (FILE_SHARE_READ | /* share_access */
2870 FILE_SHARE_WRITE | FILE_SHARE_DELETE),
2871 FILE_OPEN, /* create_disposition*/
2872 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* create_options */
2873 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
2874 0, /* oplock_request */
2875 0, /* allocation_size */
2878 &streams[i], /* result */
2881 if (!NT_STATUS_IS_OK(status)) {
2882 DEBUG(10, ("Could not open stream %s: %s\n",
2883 smb_fname_str_dbg(smb_fname),
2884 nt_errstr(status)));
2886 TALLOC_FREE(smb_fname);
2889 TALLOC_FREE(smb_fname);
2893 * don't touch the variable "status" beyond this point :-)
2896 for (i -= 1 ; i >= 0; i--) {
2897 if (streams[i] == NULL) {
2901 DEBUG(10, ("Closing stream # %d, %s\n", i,
2902 streams[i]->fsp_name));
2903 close_file(NULL, streams[i], NORMAL_CLOSE);
2912 * Wrapper around open_file_ntcreate and open_directory
2915 static NTSTATUS create_file_unixpath(connection_struct *conn,
2916 struct smb_request *req,
2917 struct smb_filename *smb_fname,
2918 uint32_t access_mask,
2919 uint32_t share_access,
2920 uint32_t create_disposition,
2921 uint32_t create_options,
2922 uint32_t file_attributes,
2923 uint32_t oplock_request,
2924 uint64_t allocation_size,
2925 struct security_descriptor *sd,
2926 struct ea_list *ea_list,
2928 files_struct **result,
2931 int info = FILE_WAS_OPENED;
2932 files_struct *base_fsp = NULL;
2933 files_struct *fsp = NULL;
2936 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
2937 "file_attributes = 0x%x, share_access = 0x%x, "
2938 "create_disposition = 0x%x create_options = 0x%x "
2939 "oplock_request = 0x%x ea_list = 0x%p, sd = 0x%p, "
2941 (unsigned int)access_mask,
2942 (unsigned int)file_attributes,
2943 (unsigned int)share_access,
2944 (unsigned int)create_disposition,
2945 (unsigned int)create_options,
2946 (unsigned int)oplock_request,
2947 ea_list, sd, smb_fname_str_dbg(smb_fname)));
2949 if (create_options & FILE_OPEN_BY_FILE_ID) {
2950 status = NT_STATUS_NOT_SUPPORTED;
2954 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
2955 status = NT_STATUS_INVALID_PARAMETER;
2960 oplock_request |= INTERNAL_OPEN_ONLY;
2963 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
2964 && (access_mask & DELETE_ACCESS)
2965 && !is_ntfs_stream_smb_fname(smb_fname)) {
2967 * We can't open a file with DELETE access if any of the
2968 * streams is open without FILE_SHARE_DELETE
2970 status = open_streams_for_delete(conn, smb_fname->base_name);
2972 if (!NT_STATUS_IS_OK(status)) {
2977 /* This is the correct thing to do (check every time) but can_delete
2978 * is expensive (it may have to read the parent directory
2979 * permissions). So for now we're not doing it unless we have a strong
2980 * hint the client is really going to delete this file. If the client
2981 * is forcing FILE_CREATE let the filesystem take care of the
2984 /* Setting FILE_SHARE_DELETE is the hint. */
2986 if (lp_acl_check_permissions(SNUM(conn))
2987 && (create_disposition != FILE_CREATE)
2988 && (share_access & FILE_SHARE_DELETE)
2989 && (access_mask & DELETE_ACCESS)
2990 && (!(can_delete_file_in_directory(conn, smb_fname) ||
2991 can_access_file_acl(conn, smb_fname, DELETE_ACCESS)))) {
2992 status = NT_STATUS_ACCESS_DENIED;
2993 DEBUG(10,("create_file_unixpath: open file %s "
2994 "for delete ACCESS_DENIED\n",
2995 smb_fname_str_dbg(smb_fname)));
3000 /* We need to support SeSecurityPrivilege for this. */
3001 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3002 !user_has_privileges(current_user.nt_user_token,
3004 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3008 /* We need to support SeSecurityPrivilege for this. */
3009 if (access_mask & SEC_FLAG_SYSTEM_SECURITY) {
3010 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3013 /* Don't allow a SACL set from an NTtrans create until we
3014 * support SeSecurityPrivilege. */
3015 if (!VALID_STAT(smb_fname->st) &&
3016 lp_nt_acl_support(SNUM(conn)) &&
3017 sd && (sd->sacl != NULL)) {
3018 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3023 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3024 && is_ntfs_stream_smb_fname(smb_fname)
3025 && (!(create_options & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
3026 uint32 base_create_disposition;
3027 struct smb_filename *smb_fname_base = NULL;
3029 if (create_options & FILE_DIRECTORY_FILE) {
3030 status = NT_STATUS_NOT_A_DIRECTORY;
3034 switch (create_disposition) {
3036 base_create_disposition = FILE_OPEN;
3039 base_create_disposition = FILE_OPEN_IF;
3043 /* Create an smb_filename with stream_name == NULL. */
3044 status = create_synthetic_smb_fname(talloc_tos(),
3045 smb_fname->base_name,
3048 if (!NT_STATUS_IS_OK(status)) {
3052 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
3053 DEBUG(10, ("Unable to stat stream: %s\n",
3054 smb_fname_str_dbg(smb_fname_base)));
3057 /* Open the base file. */
3058 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
3061 | FILE_SHARE_DELETE,
3062 base_create_disposition,
3063 0, 0, 0, 0, NULL, NULL,
3065 TALLOC_FREE(smb_fname_base);
3067 if (!NT_STATUS_IS_OK(status)) {
3068 DEBUG(10, ("create_file_unixpath for base %s failed: "
3069 "%s\n", smb_fname->base_name,
3070 nt_errstr(status)));
3073 /* we don't need to low level fd */
3078 * If it's a request for a directory open, deal with it separately.
3081 if (create_options & FILE_DIRECTORY_FILE) {
3083 if (create_options & FILE_NON_DIRECTORY_FILE) {
3084 status = NT_STATUS_INVALID_PARAMETER;
3088 /* Can't open a temp directory. IFS kit test. */
3089 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
3090 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
3091 status = NT_STATUS_INVALID_PARAMETER;
3096 * We will get a create directory here if the Win32
3097 * app specified a security descriptor in the
3098 * CreateDirectory() call.
3102 status = open_directory(
3103 conn, req, smb_fname, access_mask, share_access,
3104 create_disposition, create_options, file_attributes,
3109 * Ordinary file case.
3112 status = file_new(req, conn, &fsp);
3113 if(!NT_STATUS_IS_OK(status)) {
3118 * We're opening the stream element of a base_fsp
3119 * we already opened. Set up the base_fsp pointer.
3122 fsp->base_fsp = base_fsp;
3125 status = open_file_ntcreate(conn,
3137 if(!NT_STATUS_IS_OK(status)) {
3138 file_free(req, fsp);
3142 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
3144 /* A stream open never opens a directory */
3147 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3152 * Fail the open if it was explicitly a non-directory
3156 if (create_options & FILE_NON_DIRECTORY_FILE) {
3157 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3162 status = open_directory(
3163 conn, req, smb_fname, access_mask,
3164 share_access, create_disposition,
3165 create_options, file_attributes,
3170 if (!NT_STATUS_IS_OK(status)) {
3174 fsp->base_fsp = base_fsp;
3177 * According to the MS documentation, the only time the security
3178 * descriptor is applied to the opened file is iff we *created* the
3179 * file; an existing file stays the same.
3181 * Also, it seems (from observation) that you can open the file with
3182 * any access mask but you can still write the sd. We need to override
3183 * the granted access before we call set_sd
3184 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3187 if ((sd != NULL) && (info == FILE_WAS_CREATED)
3188 && lp_nt_acl_support(SNUM(conn))) {
3190 uint32_t sec_info_sent;
3191 uint32_t saved_access_mask = fsp->access_mask;
3193 sec_info_sent = get_sec_info(sd);
3195 fsp->access_mask = FILE_GENERIC_ALL;
3197 /* Convert all the generic bits. */
3198 security_acl_map_generic(sd->dacl, &file_generic_mapping);
3199 security_acl_map_generic(sd->sacl, &file_generic_mapping);
3201 if (sec_info_sent & (OWNER_SECURITY_INFORMATION|
3202 GROUP_SECURITY_INFORMATION|
3203 DACL_SECURITY_INFORMATION|
3204 SACL_SECURITY_INFORMATION)) {
3205 status = SMB_VFS_FSET_NT_ACL(fsp, sec_info_sent, sd);
3208 fsp->access_mask = saved_access_mask;
3210 if (!NT_STATUS_IS_OK(status)) {
3215 if ((ea_list != NULL) &&
3216 ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
3217 status = set_ea(conn, fsp, smb_fname, ea_list);
3218 if (!NT_STATUS_IS_OK(status)) {
3223 if (!fsp->is_directory && S_ISDIR(smb_fname->st.st_ex_mode)) {
3224 status = NT_STATUS_ACCESS_DENIED;
3228 /* Save the requested allocation size. */
3229 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
3231 && (allocation_size > smb_fname->st.st_ex_size)) {
3232 fsp->initial_allocation_size = smb_roundup(
3233 fsp->conn, allocation_size);
3234 if (fsp->is_directory) {
3235 /* Can't set allocation size on a directory. */
3236 status = NT_STATUS_ACCESS_DENIED;
3239 if (vfs_allocate_file_space(
3240 fsp, fsp->initial_allocation_size) == -1) {
3241 status = NT_STATUS_DISK_FULL;
3245 fsp->initial_allocation_size = smb_roundup(
3246 fsp->conn, (uint64_t)smb_fname->st.st_ex_size);
3250 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
3253 if (pinfo != NULL) {
3256 if ((fsp->fh != NULL) && (fsp->fh->fd != -1)) {
3257 SMB_VFS_FSTAT(fsp, &smb_fname->st);
3259 return NT_STATUS_OK;
3262 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
3265 if (base_fsp && fsp->base_fsp == base_fsp) {
3267 * The close_file below will close
3272 close_file(req, fsp, ERROR_CLOSE);
3275 if (base_fsp != NULL) {
3276 close_file(req, base_fsp, ERROR_CLOSE);
3283 * Calculate the full path name given a relative fid.
3285 NTSTATUS get_relative_fid_filename(connection_struct *conn,
3286 struct smb_request *req,
3287 uint16_t root_dir_fid,
3288 struct smb_filename *smb_fname)
3290 files_struct *dir_fsp;
3291 char *parent_fname = NULL;
3292 char *new_base_name = NULL;
3295 if (root_dir_fid == 0 || !smb_fname) {
3296 status = NT_STATUS_INTERNAL_ERROR;
3300 dir_fsp = file_fsp(req, root_dir_fid);
3302 if (dir_fsp == NULL) {
3303 status = NT_STATUS_INVALID_HANDLE;
3307 if (!dir_fsp->is_directory) {
3310 * Check to see if this is a mac fork of some kind.
3313 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
3314 is_ntfs_stream_smb_fname(smb_fname)) {
3315 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3320 we need to handle the case when we get a
3321 relative open relative to a file and the
3322 pathname is blank - this is a reopen!
3323 (hint from demyn plantenberg)
3326 status = NT_STATUS_INVALID_HANDLE;
3330 if (ISDOT(dir_fsp->fsp_name)) {
3332 * We're at the toplevel dir, the final file name
3333 * must not contain ./, as this is filtered out
3334 * normally by srvstr_get_path and unix_convert
3335 * explicitly rejects paths containing ./.
3337 parent_fname = talloc_strdup(talloc_tos(), "");
3338 if (parent_fname == NULL) {
3339 status = NT_STATUS_NO_MEMORY;
3343 size_t dir_name_len = strlen(dir_fsp->fsp_name);
3346 * Copy in the base directory name.
3349 parent_fname = TALLOC_ARRAY(talloc_tos(), char,
3351 if (parent_fname == NULL) {
3352 status = NT_STATUS_NO_MEMORY;
3355 memcpy(parent_fname, dir_fsp->fsp_name,
3359 * Ensure it ends in a '/'.
3360 * We used TALLOC_SIZE +2 to add space for the '/'.
3364 && (parent_fname[dir_name_len-1] != '\\')
3365 && (parent_fname[dir_name_len-1] != '/')) {
3366 parent_fname[dir_name_len] = '/';
3367 parent_fname[dir_name_len+1] = '\0';
3371 new_base_name = talloc_asprintf(smb_fname, "%s%s", parent_fname,
3372 smb_fname->base_name);
3373 if (new_base_name == NULL) {
3374 status = NT_STATUS_NO_MEMORY;
3378 TALLOC_FREE(smb_fname->base_name);
3379 smb_fname->base_name = new_base_name;
3380 status = NT_STATUS_OK;
3383 TALLOC_FREE(parent_fname);
3387 NTSTATUS create_file_default(connection_struct *conn,
3388 struct smb_request *req,
3389 uint16_t root_dir_fid,
3390 struct smb_filename *smb_fname,
3391 uint32_t access_mask,
3392 uint32_t share_access,
3393 uint32_t create_disposition,
3394 uint32_t create_options,
3395 uint32_t file_attributes,
3396 uint32_t oplock_request,
3397 uint64_t allocation_size,
3398 struct security_descriptor *sd,
3399 struct ea_list *ea_list,
3400 files_struct **result,
3403 int info = FILE_WAS_OPENED;
3404 files_struct *fsp = NULL;
3407 DEBUG(10,("create_file: access_mask = 0x%x "
3408 "file_attributes = 0x%x, share_access = 0x%x, "
3409 "create_disposition = 0x%x create_options = 0x%x "
3410 "oplock_request = 0x%x "
3411 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
3413 (unsigned int)access_mask,
3414 (unsigned int)file_attributes,
3415 (unsigned int)share_access,
3416 (unsigned int)create_disposition,
3417 (unsigned int)create_options,
3418 (unsigned int)oplock_request,
3419 (unsigned int)root_dir_fid,
3420 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3423 * Calculate the filename from the root_dir_if if necessary.
3426 if (root_dir_fid != 0) {
3427 status = get_relative_fid_filename(conn, req, root_dir_fid,
3429 if (!NT_STATUS_IS_OK(status)) {
3435 * Check to see if this is a mac fork of some kind.
3438 if (is_ntfs_stream_smb_fname(smb_fname)) {
3440 enum FAKE_FILE_TYPE fake_file_type;
3442 status = get_full_smb_filename(talloc_tos(), smb_fname,
3444 if (!NT_STATUS_IS_OK(status)) {
3448 fake_file_type = is_fake_file(fname);
3450 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
3453 * Here we go! support for changing the disk quotas
3456 * We need to fake up to open this MAGIC QUOTA file
3457 * and return a valid FID.
3459 * w2k close this file directly after openening xp
3460 * also tries a QUERY_FILE_INFO on the file and then
3463 status = open_fake_file(req, conn, req->vuid,
3464 fake_file_type, fname,
3467 if (!NT_STATUS_IS_OK(status)) {
3471 ZERO_STRUCT(smb_fname->st);
3476 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
3477 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
3482 /* All file access must go through check_name() */
3484 status = check_name(conn, smb_fname->base_name);
3485 if (!NT_STATUS_IS_OK(status)) {
3489 status = create_file_unixpath(
3490 conn, req, smb_fname, access_mask, share_access,
3491 create_disposition, create_options, file_attributes,
3492 oplock_request, allocation_size, sd, ea_list,
3495 if (!NT_STATUS_IS_OK(status)) {
3500 DEBUG(10, ("create_file: info=%d\n", info));
3503 if (pinfo != NULL) {
3506 return NT_STATUS_OK;
3509 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
3512 close_file(req, fsp, ERROR_CLOSE);