s3:smbd: only try and fallback to open a directory if it's not a stream open
[Samba/bb.git] / source / smbd / open.c
blobdccfb842aa4fe9a2bd2acfa9dd73af47ff6a97b0
1 /*
2 Unix SMB/CIFS implementation.
3 file opening and share modes
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 2001-2004
6 Copyright (C) Volker Lendecke 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
24 extern const struct generic_mapping file_generic_mapping;
25 extern bool global_client_failed_oplock_break;
27 struct deferred_open_record {
28 bool delayed_for_oplocks;
29 struct file_id id;
32 /****************************************************************************
33 SMB1 file varient of se_access_check. Never test FILE_READ_ATTRIBUTES.
34 ****************************************************************************/
36 NTSTATUS smb1_file_se_access_check(const struct security_descriptor *sd,
37 const NT_USER_TOKEN *token,
38 uint32_t access_desired,
39 uint32_t *access_granted)
41 return se_access_check(sd,
42 token,
43 (access_desired & ~FILE_READ_ATTRIBUTES),
44 access_granted);
47 /****************************************************************************
48 Check if we have open rights.
49 ****************************************************************************/
51 static NTSTATUS check_open_rights(struct connection_struct *conn,
52 const char *fname,
53 uint32_t access_mask)
55 /* Check if we have rights to open. */
56 NTSTATUS status;
57 uint32_t access_granted = 0;
58 struct security_descriptor *sd;
60 status = SMB_VFS_GET_NT_ACL(conn, fname,
61 (OWNER_SECURITY_INFORMATION |
62 GROUP_SECURITY_INFORMATION |
63 DACL_SECURITY_INFORMATION),&sd);
65 if (!NT_STATUS_IS_OK(status)) {
66 DEBUG(10, ("check_open_rights: Could not get acl "
67 "on %s: %s\n",
68 fname,
69 nt_errstr(status)));
70 return status;
73 status = smb1_file_se_access_check(sd,
74 conn->server_info->ptok,
75 access_mask,
76 &access_granted);
78 TALLOC_FREE(sd);
79 return status;
82 /****************************************************************************
83 fd support routines - attempt to do a dos_open.
84 ****************************************************************************/
86 static NTSTATUS fd_open(struct connection_struct *conn,
87 const char *fname,
88 files_struct *fsp,
89 int flags,
90 mode_t mode)
92 NTSTATUS status = NT_STATUS_OK;
94 #ifdef O_NOFOLLOW
95 /*
96 * Never follow symlinks on a POSIX client. The
97 * client should be doing this.
100 if (fsp->posix_open || !lp_symlinks(SNUM(conn))) {
101 flags |= O_NOFOLLOW;
103 #endif
105 fsp->fh->fd = SMB_VFS_OPEN(conn,fname,fsp,flags,mode);
106 if (fsp->fh->fd == -1) {
107 status = map_nt_error_from_unix(errno);
110 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
111 fname, flags, (int)mode, fsp->fh->fd,
112 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
114 return status;
117 /****************************************************************************
118 Close the file associated with a fsp.
119 ****************************************************************************/
121 NTSTATUS fd_close(files_struct *fsp)
123 int ret;
125 if (fsp->fh->fd == -1) {
126 return NT_STATUS_OK; /* What we used to call a stat open. */
128 if (fsp->fh->ref_count > 1) {
129 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
132 ret = SMB_VFS_CLOSE(fsp);
133 fsp->fh->fd = -1;
134 if (ret == -1) {
135 return map_nt_error_from_unix(errno);
137 return NT_STATUS_OK;
140 /****************************************************************************
141 Change the ownership of a file to that of the parent directory.
142 Do this by fd if possible.
143 ****************************************************************************/
145 static void change_file_owner_to_parent(connection_struct *conn,
146 const char *inherit_from_dir,
147 files_struct *fsp)
149 SMB_STRUCT_STAT parent_st;
150 int ret;
152 ret = SMB_VFS_STAT(conn, inherit_from_dir, &parent_st);
153 if (ret == -1) {
154 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
155 "directory %s. Error was %s\n",
156 inherit_from_dir, strerror(errno) ));
157 return;
160 become_root();
161 ret = SMB_VFS_FCHOWN(fsp, parent_st.st_uid, (gid_t)-1);
162 unbecome_root();
163 if (ret == -1) {
164 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
165 "file %s to parent directory uid %u. Error "
166 "was %s\n", fsp->fsp_name,
167 (unsigned int)parent_st.st_uid,
168 strerror(errno) ));
171 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
172 "parent directory uid %u.\n", fsp->fsp_name,
173 (unsigned int)parent_st.st_uid ));
176 static NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
177 const char *inherit_from_dir,
178 const char *fname,
179 SMB_STRUCT_STAT *psbuf)
181 char *saved_dir = NULL;
182 SMB_STRUCT_STAT sbuf;
183 SMB_STRUCT_STAT parent_st;
184 TALLOC_CTX *ctx = talloc_tos();
185 NTSTATUS status = NT_STATUS_OK;
186 int ret;
188 ret = SMB_VFS_STAT(conn, inherit_from_dir, &parent_st);
189 if (ret == -1) {
190 status = map_nt_error_from_unix(errno);
191 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
192 "directory %s. Error was %s\n",
193 inherit_from_dir, strerror(errno) ));
194 return status;
197 /* We've already done an lstat into psbuf, and we know it's a
198 directory. If we can cd into the directory and the dev/ino
199 are the same then we can safely chown without races as
200 we're locking the directory in place by being in it. This
201 should work on any UNIX (thanks tridge :-). JRA.
204 saved_dir = vfs_GetWd(ctx,conn);
205 if (!saved_dir) {
206 status = map_nt_error_from_unix(errno);
207 DEBUG(0,("change_dir_owner_to_parent: failed to get "
208 "current working directory. Error was %s\n",
209 strerror(errno)));
210 return status;
213 /* Chdir into the new path. */
214 if (vfs_ChDir(conn, fname) == -1) {
215 status = map_nt_error_from_unix(errno);
216 DEBUG(0,("change_dir_owner_to_parent: failed to change "
217 "current working directory to %s. Error "
218 "was %s\n", fname, strerror(errno) ));
219 goto out;
222 if (SMB_VFS_STAT(conn,".",&sbuf) == -1) {
223 status = map_nt_error_from_unix(errno);
224 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
225 "directory '.' (%s) Error was %s\n",
226 fname, strerror(errno)));
227 goto out;
230 /* Ensure we're pointing at the same place. */
231 if (sbuf.st_dev != psbuf->st_dev ||
232 sbuf.st_ino != psbuf->st_ino ||
233 sbuf.st_mode != psbuf->st_mode ) {
234 DEBUG(0,("change_dir_owner_to_parent: "
235 "device/inode/mode on directory %s changed. "
236 "Refusing to chown !\n", fname ));
237 status = NT_STATUS_ACCESS_DENIED;
238 goto out;
241 become_root();
242 ret = SMB_VFS_CHOWN(conn, ".", parent_st.st_uid, (gid_t)-1);
243 unbecome_root();
244 if (ret == -1) {
245 status = map_nt_error_from_unix(errno);
246 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
247 "directory %s to parent directory uid %u. "
248 "Error was %s\n", fname,
249 (unsigned int)parent_st.st_uid, strerror(errno) ));
250 goto out;
253 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
254 "directory %s to parent directory uid %u.\n",
255 fname, (unsigned int)parent_st.st_uid ));
257 out:
259 vfs_ChDir(conn,saved_dir);
260 return status;
263 /****************************************************************************
264 Open a file.
265 ****************************************************************************/
267 static NTSTATUS open_file(files_struct *fsp,
268 connection_struct *conn,
269 struct smb_request *req,
270 const char *parent_dir,
271 const char *name,
272 const char *path,
273 SMB_STRUCT_STAT *psbuf,
274 int flags,
275 mode_t unx_mode,
276 uint32 access_mask, /* client requested access mask. */
277 uint32 open_access_mask) /* what we're actually using in the open. */
279 NTSTATUS status = NT_STATUS_OK;
280 int accmode = (flags & O_ACCMODE);
281 int local_flags = flags;
282 bool file_existed = VALID_STAT(*psbuf);
284 fsp->fh->fd = -1;
285 errno = EPERM;
287 /* Check permissions */
290 * This code was changed after seeing a client open request
291 * containing the open mode of (DENY_WRITE/read-only) with
292 * the 'create if not exist' bit set. The previous code
293 * would fail to open the file read only on a read-only share
294 * as it was checking the flags parameter directly against O_RDONLY,
295 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
296 * JRA.
299 if (!CAN_WRITE(conn)) {
300 /* It's a read-only share - fail if we wanted to write. */
301 if(accmode != O_RDONLY) {
302 DEBUG(3,("Permission denied opening %s\n", path));
303 return NT_STATUS_ACCESS_DENIED;
304 } else if(flags & O_CREAT) {
305 /* We don't want to write - but we must make sure that
306 O_CREAT doesn't create the file if we have write
307 access into the directory.
309 flags &= ~O_CREAT;
310 local_flags &= ~O_CREAT;
315 * This little piece of insanity is inspired by the
316 * fact that an NT client can open a file for O_RDONLY,
317 * but set the create disposition to FILE_EXISTS_TRUNCATE.
318 * If the client *can* write to the file, then it expects to
319 * truncate the file, even though it is opening for readonly.
320 * Quicken uses this stupid trick in backup file creation...
321 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
322 * for helping track this one down. It didn't bite us in 2.0.x
323 * as we always opened files read-write in that release. JRA.
326 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
327 DEBUG(10,("open_file: truncate requested on read-only open "
328 "for file %s\n", path));
329 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
332 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
333 (!file_existed && (local_flags & O_CREAT)) ||
334 ((local_flags & O_TRUNC) == O_TRUNC) ) {
335 const char *wild;
338 * We can't actually truncate here as the file may be locked.
339 * open_file_ntcreate will take care of the truncate later. JRA.
342 local_flags &= ~O_TRUNC;
344 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
346 * We would block on opening a FIFO with no one else on the
347 * other end. Do what we used to do and add O_NONBLOCK to the
348 * open flags. JRA.
351 if (file_existed && S_ISFIFO(psbuf->st_mode)) {
352 local_flags |= O_NONBLOCK;
354 #endif
356 /* Don't create files with Microsoft wildcard characters. */
357 if (fsp->base_fsp) {
359 * wildcard characters are allowed in stream names
360 * only test the basefilename
362 wild = fsp->base_fsp->fsp_name;
363 } else {
364 wild = path;
366 if ((local_flags & O_CREAT) && !file_existed &&
367 ms_has_wild(wild)) {
368 return NT_STATUS_OBJECT_NAME_INVALID;
371 /* Actually do the open */
372 status = fd_open(conn, path, fsp, local_flags, unx_mode);
373 if (!NT_STATUS_IS_OK(status)) {
374 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
375 "(flags=%d)\n",
376 path,nt_errstr(status),local_flags,flags));
377 return status;
380 if ((local_flags & O_CREAT) && !file_existed) {
382 /* Inherit the ACL if required */
383 if (lp_inherit_perms(SNUM(conn))) {
384 inherit_access_posix_acl(conn, parent_dir, path,
385 unx_mode);
388 /* Change the owner if required. */
389 if (lp_inherit_owner(SNUM(conn))) {
390 change_file_owner_to_parent(conn, parent_dir,
391 fsp);
394 notify_fname(conn, NOTIFY_ACTION_ADDED,
395 FILE_NOTIFY_CHANGE_FILE_NAME, path);
398 } else {
399 fsp->fh->fd = -1; /* What we used to call a stat open. */
400 if (file_existed) {
401 status = check_open_rights(conn,
402 path,
403 access_mask);
404 if (!NT_STATUS_IS_OK(status)) {
405 DEBUG(10, ("open_file: Access denied on "
406 "file %s\n",
407 path));
408 return status;
413 if (!file_existed) {
414 int ret;
416 if (fsp->fh->fd == -1) {
417 ret = SMB_VFS_STAT(conn, path, psbuf);
418 } else {
419 ret = SMB_VFS_FSTAT(fsp, psbuf);
420 /* If we have an fd, this stat should succeed. */
421 if (ret == -1) {
422 DEBUG(0,("Error doing fstat on open file %s "
423 "(%s)\n", path,strerror(errno) ));
427 /* For a non-io open, this stat failing means file not found. JRA */
428 if (ret == -1) {
429 status = map_nt_error_from_unix(errno);
430 fd_close(fsp);
431 return status;
436 * POSIX allows read-only opens of directories. We don't
437 * want to do this (we use a different code path for this)
438 * so catch a directory open and return an EISDIR. JRA.
441 if(S_ISDIR(psbuf->st_mode)) {
442 fd_close(fsp);
443 errno = EISDIR;
444 return NT_STATUS_FILE_IS_A_DIRECTORY;
447 fsp->mode = psbuf->st_mode;
448 fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
449 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
450 fsp->file_pid = req ? req->smbpid : 0;
451 fsp->can_lock = True;
452 fsp->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
453 if (!CAN_WRITE(conn)) {
454 fsp->can_write = False;
455 } else {
456 fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ?
457 True : False;
459 fsp->print_file = False;
460 fsp->modified = False;
461 fsp->sent_oplock_break = NO_BREAK_SENT;
462 fsp->is_directory = False;
463 if (conn->aio_write_behind_list &&
464 is_in_path(path, conn->aio_write_behind_list, conn->case_sensitive)) {
465 fsp->aio_write_behind = True;
468 string_set(&fsp->fsp_name, path);
469 fsp->wcp = NULL; /* Write cache pointer. */
471 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
472 conn->server_info->unix_name,
473 fsp->fsp_name,
474 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
475 conn->num_files_open));
477 errno = 0;
478 return NT_STATUS_OK;
481 /*******************************************************************
482 Return True if the filename is one of the special executable types.
483 ********************************************************************/
485 static bool is_executable(const char *fname)
487 if ((fname = strrchr_m(fname,'.'))) {
488 if (strequal(fname,".com") ||
489 strequal(fname,".dll") ||
490 strequal(fname,".exe") ||
491 strequal(fname,".sym")) {
492 return True;
495 return False;
498 /****************************************************************************
499 Check if we can open a file with a share mode.
500 Returns True if conflict, False if not.
501 ****************************************************************************/
503 static bool share_conflict(struct share_mode_entry *entry,
504 uint32 access_mask,
505 uint32 share_access)
507 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
508 "entry->share_access = 0x%x, "
509 "entry->private_options = 0x%x\n",
510 (unsigned int)entry->access_mask,
511 (unsigned int)entry->share_access,
512 (unsigned int)entry->private_options));
514 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
515 (unsigned int)access_mask, (unsigned int)share_access));
517 if ((entry->access_mask & (FILE_WRITE_DATA|
518 FILE_APPEND_DATA|
519 FILE_READ_DATA|
520 FILE_EXECUTE|
521 DELETE_ACCESS)) == 0) {
522 DEBUG(10,("share_conflict: No conflict due to "
523 "entry->access_mask = 0x%x\n",
524 (unsigned int)entry->access_mask ));
525 return False;
528 if ((access_mask & (FILE_WRITE_DATA|
529 FILE_APPEND_DATA|
530 FILE_READ_DATA|
531 FILE_EXECUTE|
532 DELETE_ACCESS)) == 0) {
533 DEBUG(10,("share_conflict: No conflict due to "
534 "access_mask = 0x%x\n",
535 (unsigned int)access_mask ));
536 return False;
539 #if 1 /* JRA TEST - Superdebug. */
540 #define CHECK_MASK(num, am, right, sa, share) \
541 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
542 (unsigned int)(num), (unsigned int)(am), \
543 (unsigned int)(right), (unsigned int)(am)&(right) )); \
544 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
545 (unsigned int)(num), (unsigned int)(sa), \
546 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
547 if (((am) & (right)) && !((sa) & (share))) { \
548 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
549 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
550 (unsigned int)(share) )); \
551 return True; \
553 #else
554 #define CHECK_MASK(num, am, right, sa, share) \
555 if (((am) & (right)) && !((sa) & (share))) { \
556 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
557 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
558 (unsigned int)(share) )); \
559 return True; \
561 #endif
563 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
564 share_access, FILE_SHARE_WRITE);
565 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
566 entry->share_access, FILE_SHARE_WRITE);
568 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
569 share_access, FILE_SHARE_READ);
570 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
571 entry->share_access, FILE_SHARE_READ);
573 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
574 share_access, FILE_SHARE_DELETE);
575 CHECK_MASK(6, access_mask, DELETE_ACCESS,
576 entry->share_access, FILE_SHARE_DELETE);
578 DEBUG(10,("share_conflict: No conflict.\n"));
579 return False;
582 #if defined(DEVELOPER)
583 static void validate_my_share_entries(int num,
584 struct share_mode_entry *share_entry)
586 files_struct *fsp;
588 if (!procid_is_me(&share_entry->pid)) {
589 return;
592 if (is_deferred_open_entry(share_entry) &&
593 !open_was_deferred(share_entry->op_mid)) {
594 char *str = talloc_asprintf(talloc_tos(),
595 "Got a deferred entry without a request: "
596 "PANIC: %s\n",
597 share_mode_str(talloc_tos(), num, share_entry));
598 smb_panic(str);
601 if (!is_valid_share_mode_entry(share_entry)) {
602 return;
605 fsp = file_find_dif(share_entry->id,
606 share_entry->share_file_id);
607 if (!fsp) {
608 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
609 share_mode_str(talloc_tos(), num, share_entry) ));
610 smb_panic("validate_my_share_entries: Cannot match a "
611 "share entry with an open file\n");
614 if (is_deferred_open_entry(share_entry) ||
615 is_unused_share_mode_entry(share_entry)) {
616 goto panic;
619 if ((share_entry->op_type == NO_OPLOCK) &&
620 (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK)) {
621 /* Someone has already written to it, but I haven't yet
622 * noticed */
623 return;
626 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
627 goto panic;
630 return;
632 panic:
634 char *str;
635 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
636 share_mode_str(talloc_tos(), num, share_entry) ));
637 str = talloc_asprintf(talloc_tos(),
638 "validate_my_share_entries: "
639 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
640 fsp->fsp_name, (unsigned int)fsp->oplock_type,
641 (unsigned int)share_entry->op_type );
642 smb_panic(str);
645 #endif
647 static bool is_stat_open(uint32 access_mask)
649 return (access_mask &&
650 ((access_mask & ~(SYNCHRONIZE_ACCESS| FILE_READ_ATTRIBUTES|
651 FILE_WRITE_ATTRIBUTES))==0) &&
652 ((access_mask & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|
653 FILE_WRITE_ATTRIBUTES)) != 0));
656 /****************************************************************************
657 Deal with share modes
658 Invarient: Share mode must be locked on entry and exit.
659 Returns -1 on error, or number of share modes on success (may be zero).
660 ****************************************************************************/
662 static NTSTATUS open_mode_check(connection_struct *conn,
663 const char *fname,
664 struct share_mode_lock *lck,
665 uint32 access_mask,
666 uint32 share_access,
667 uint32 create_options,
668 bool *file_existed)
670 int i;
672 if(lck->num_share_modes == 0) {
673 return NT_STATUS_OK;
676 *file_existed = True;
678 /* A delete on close prohibits everything */
680 if (lck->delete_on_close) {
681 return NT_STATUS_DELETE_PENDING;
684 if (is_stat_open(access_mask)) {
685 /* Stat open that doesn't trigger oplock breaks or share mode
686 * checks... ! JRA. */
687 return NT_STATUS_OK;
691 * Check if the share modes will give us access.
694 #if defined(DEVELOPER)
695 for(i = 0; i < lck->num_share_modes; i++) {
696 validate_my_share_entries(i, &lck->share_modes[i]);
698 #endif
700 if (!lp_share_modes(SNUM(conn))) {
701 return NT_STATUS_OK;
704 /* Now we check the share modes, after any oplock breaks. */
705 for(i = 0; i < lck->num_share_modes; i++) {
707 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
708 continue;
711 /* someone else has a share lock on it, check to see if we can
712 * too */
713 if (share_conflict(&lck->share_modes[i],
714 access_mask, share_access)) {
715 return NT_STATUS_SHARING_VIOLATION;
719 return NT_STATUS_OK;
722 static bool is_delete_request(files_struct *fsp) {
723 return ((fsp->access_mask == DELETE_ACCESS) &&
724 (fsp->oplock_type == NO_OPLOCK));
728 * 1) No files open at all or internal open: Grant whatever the client wants.
730 * 2) Exclusive (or batch) oplock around: If the requested access is a delete
731 * request, break if the oplock around is a batch oplock. If it's another
732 * requested access type, break.
734 * 3) Only level2 around: Grant level2 and do nothing else.
737 static bool delay_for_oplocks(struct share_mode_lock *lck,
738 files_struct *fsp,
739 uint16 mid,
740 int pass_number,
741 int oplock_request)
743 int i;
744 struct share_mode_entry *exclusive = NULL;
745 bool valid_entry = False;
746 bool delay_it = False;
747 bool have_level2 = False;
748 NTSTATUS status;
749 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
751 if (oplock_request & INTERNAL_OPEN_ONLY) {
752 fsp->oplock_type = NO_OPLOCK;
755 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
756 return False;
759 for (i=0; i<lck->num_share_modes; i++) {
761 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
762 continue;
765 /* At least one entry is not an invalid or deferred entry. */
766 valid_entry = True;
768 if (pass_number == 1) {
769 if (BATCH_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
770 SMB_ASSERT(exclusive == NULL);
771 exclusive = &lck->share_modes[i];
773 } else {
774 if (EXCLUSIVE_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
775 SMB_ASSERT(exclusive == NULL);
776 exclusive = &lck->share_modes[i];
780 if (lck->share_modes[i].op_type == LEVEL_II_OPLOCK) {
781 SMB_ASSERT(exclusive == NULL);
782 have_level2 = True;
786 if (!valid_entry) {
787 /* All entries are placeholders or deferred.
788 * Directly grant whatever the client wants. */
789 if (fsp->oplock_type == NO_OPLOCK) {
790 /* Store a level2 oplock, but don't tell the client */
791 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
793 return False;
796 if (exclusive != NULL) { /* Found an exclusive oplock */
797 SMB_ASSERT(!have_level2);
798 delay_it = is_delete_request(fsp) ?
799 BATCH_OPLOCK_TYPE(exclusive->op_type) : True;
802 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
803 /* We can at most grant level2 as there are other
804 * level2 or NO_OPLOCK entries. */
805 fsp->oplock_type = LEVEL_II_OPLOCK;
808 if ((fsp->oplock_type == NO_OPLOCK) && have_level2) {
809 /* Store a level2 oplock, but don't tell the client */
810 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
813 if (!delay_it) {
814 return False;
818 * Send a break message to the oplock holder and delay the open for
819 * our client.
822 DEBUG(10, ("Sending break request to PID %s\n",
823 procid_str_static(&exclusive->pid)));
824 exclusive->op_mid = mid;
826 /* Create the message. */
827 share_mode_entry_to_message(msg, exclusive);
829 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
830 don't want this set in the share mode struct pointed to by lck. */
832 if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
833 SSVAL(msg,6,exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
836 status = messaging_send_buf(smbd_messaging_context(), exclusive->pid,
837 MSG_SMB_BREAK_REQUEST,
838 (uint8 *)msg,
839 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
840 if (!NT_STATUS_IS_OK(status)) {
841 DEBUG(3, ("Could not send oplock break message: %s\n",
842 nt_errstr(status)));
845 return True;
848 static bool request_timed_out(struct timeval request_time,
849 struct timeval timeout)
851 struct timeval now, end_time;
852 GetTimeOfDay(&now);
853 end_time = timeval_sum(&request_time, &timeout);
854 return (timeval_compare(&end_time, &now) < 0);
857 /****************************************************************************
858 Handle the 1 second delay in returning a SHARING_VIOLATION error.
859 ****************************************************************************/
861 static void defer_open(struct share_mode_lock *lck,
862 struct timeval request_time,
863 struct timeval timeout,
864 struct smb_request *req,
865 struct deferred_open_record *state)
867 int i;
869 /* Paranoia check */
871 for (i=0; i<lck->num_share_modes; i++) {
872 struct share_mode_entry *e = &lck->share_modes[i];
874 if (!is_deferred_open_entry(e)) {
875 continue;
878 if (procid_is_me(&e->pid) && (e->op_mid == req->mid)) {
879 DEBUG(0, ("Trying to defer an already deferred "
880 "request: mid=%d, exiting\n", req->mid));
881 exit_server("attempt to defer a deferred request");
885 /* End paranoia check */
887 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
888 "open entry for mid %u\n",
889 (unsigned int)request_time.tv_sec,
890 (unsigned int)request_time.tv_usec,
891 (unsigned int)req->mid));
893 if (!push_deferred_smb_message(req, request_time, timeout,
894 (char *)state, sizeof(*state))) {
895 exit_server("push_deferred_smb_message failed");
897 add_deferred_open(lck, req->mid, request_time, state->id);
900 * Push the MID of this packet on the signing queue.
901 * We only do this once, the first time we push the packet
902 * onto the deferred open queue, as this has a side effect
903 * of incrementing the response sequence number.
906 srv_defer_sign_response(req->mid);
910 /****************************************************************************
911 On overwrite open ensure that the attributes match.
912 ****************************************************************************/
914 static bool open_match_attributes(connection_struct *conn,
915 const char *path,
916 uint32 old_dos_attr,
917 uint32 new_dos_attr,
918 mode_t existing_unx_mode,
919 mode_t new_unx_mode,
920 mode_t *returned_unx_mode)
922 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
924 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
925 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
927 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
928 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
929 *returned_unx_mode = new_unx_mode;
930 } else {
931 *returned_unx_mode = (mode_t)0;
934 DEBUG(10,("open_match_attributes: file %s old_dos_attr = 0x%x, "
935 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
936 "returned_unx_mode = 0%o\n",
937 path,
938 (unsigned int)old_dos_attr,
939 (unsigned int)existing_unx_mode,
940 (unsigned int)new_dos_attr,
941 (unsigned int)*returned_unx_mode ));
943 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
944 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
945 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
946 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
947 return False;
950 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
951 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
952 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
953 return False;
956 return True;
959 /****************************************************************************
960 Special FCB or DOS processing in the case of a sharing violation.
961 Try and find a duplicated file handle.
962 ****************************************************************************/
964 static NTSTATUS fcb_or_dos_open(connection_struct *conn,
965 files_struct *fsp_to_dup_into,
966 const char *fname,
967 struct file_id id,
968 uint16 file_pid,
969 uint16 vuid,
970 uint32 access_mask,
971 uint32 share_access,
972 uint32 create_options)
974 files_struct *fsp;
976 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
977 "file %s.\n", fname ));
979 for(fsp = file_find_di_first(id); fsp;
980 fsp = file_find_di_next(fsp)) {
982 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
983 "vuid = %u, file_pid = %u, private_options = 0x%x "
984 "access_mask = 0x%x\n", fsp->fsp_name,
985 fsp->fh->fd, (unsigned int)fsp->vuid,
986 (unsigned int)fsp->file_pid,
987 (unsigned int)fsp->fh->private_options,
988 (unsigned int)fsp->access_mask ));
990 if (fsp->fh->fd != -1 &&
991 fsp->vuid == vuid &&
992 fsp->file_pid == file_pid &&
993 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
994 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
995 (fsp->access_mask & FILE_WRITE_DATA) &&
996 strequal(fsp->fsp_name, fname)) {
997 DEBUG(10,("fcb_or_dos_open: file match\n"));
998 break;
1002 if (!fsp) {
1003 return NT_STATUS_NOT_FOUND;
1006 /* quite an insane set of semantics ... */
1007 if (is_executable(fname) &&
1008 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1009 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1010 return NT_STATUS_INVALID_PARAMETER;
1013 /* We need to duplicate this fsp. */
1014 dup_file_fsp(fsp, access_mask, share_access,
1015 create_options, fsp_to_dup_into);
1017 return NT_STATUS_OK;
1020 /****************************************************************************
1021 Open a file with a share mode - old openX method - map into NTCreate.
1022 ****************************************************************************/
1024 bool map_open_params_to_ntcreate(const char *fname, int deny_mode, int open_func,
1025 uint32 *paccess_mask,
1026 uint32 *pshare_mode,
1027 uint32 *pcreate_disposition,
1028 uint32 *pcreate_options)
1030 uint32 access_mask;
1031 uint32 share_mode;
1032 uint32 create_disposition;
1033 uint32 create_options = 0;
1035 DEBUG(10,("map_open_params_to_ntcreate: fname = %s, deny_mode = 0x%x, "
1036 "open_func = 0x%x\n",
1037 fname, (unsigned int)deny_mode, (unsigned int)open_func ));
1039 /* Create the NT compatible access_mask. */
1040 switch (GET_OPENX_MODE(deny_mode)) {
1041 case DOS_OPEN_EXEC: /* Implies read-only - used to be FILE_READ_DATA */
1042 case DOS_OPEN_RDONLY:
1043 access_mask = FILE_GENERIC_READ;
1044 break;
1045 case DOS_OPEN_WRONLY:
1046 access_mask = FILE_GENERIC_WRITE;
1047 break;
1048 case DOS_OPEN_RDWR:
1049 case DOS_OPEN_FCB:
1050 access_mask = FILE_GENERIC_READ|FILE_GENERIC_WRITE;
1051 break;
1052 default:
1053 DEBUG(10,("map_open_params_to_ntcreate: bad open mode = 0x%x\n",
1054 (unsigned int)GET_OPENX_MODE(deny_mode)));
1055 return False;
1058 /* Create the NT compatible create_disposition. */
1059 switch (open_func) {
1060 case OPENX_FILE_EXISTS_FAIL|OPENX_FILE_CREATE_IF_NOT_EXIST:
1061 create_disposition = FILE_CREATE;
1062 break;
1064 case OPENX_FILE_EXISTS_OPEN:
1065 create_disposition = FILE_OPEN;
1066 break;
1068 case OPENX_FILE_EXISTS_OPEN|OPENX_FILE_CREATE_IF_NOT_EXIST:
1069 create_disposition = FILE_OPEN_IF;
1070 break;
1072 case OPENX_FILE_EXISTS_TRUNCATE:
1073 create_disposition = FILE_OVERWRITE;
1074 break;
1076 case OPENX_FILE_EXISTS_TRUNCATE|OPENX_FILE_CREATE_IF_NOT_EXIST:
1077 create_disposition = FILE_OVERWRITE_IF;
1078 break;
1080 default:
1081 /* From samba4 - to be confirmed. */
1082 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_EXEC) {
1083 create_disposition = FILE_CREATE;
1084 break;
1086 DEBUG(10,("map_open_params_to_ntcreate: bad "
1087 "open_func 0x%x\n", (unsigned int)open_func));
1088 return False;
1091 /* Create the NT compatible share modes. */
1092 switch (GET_DENY_MODE(deny_mode)) {
1093 case DENY_ALL:
1094 share_mode = FILE_SHARE_NONE;
1095 break;
1097 case DENY_WRITE:
1098 share_mode = FILE_SHARE_READ;
1099 break;
1101 case DENY_READ:
1102 share_mode = FILE_SHARE_WRITE;
1103 break;
1105 case DENY_NONE:
1106 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1107 break;
1109 case DENY_DOS:
1110 create_options |= NTCREATEX_OPTIONS_PRIVATE_DENY_DOS;
1111 if (is_executable(fname)) {
1112 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1113 } else {
1114 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_RDONLY) {
1115 share_mode = FILE_SHARE_READ;
1116 } else {
1117 share_mode = FILE_SHARE_NONE;
1120 break;
1122 case DENY_FCB:
1123 create_options |= NTCREATEX_OPTIONS_PRIVATE_DENY_FCB;
1124 share_mode = FILE_SHARE_NONE;
1125 break;
1127 default:
1128 DEBUG(10,("map_open_params_to_ntcreate: bad deny_mode 0x%x\n",
1129 (unsigned int)GET_DENY_MODE(deny_mode) ));
1130 return False;
1133 DEBUG(10,("map_open_params_to_ntcreate: file %s, access_mask = 0x%x, "
1134 "share_mode = 0x%x, create_disposition = 0x%x, "
1135 "create_options = 0x%x\n",
1136 fname,
1137 (unsigned int)access_mask,
1138 (unsigned int)share_mode,
1139 (unsigned int)create_disposition,
1140 (unsigned int)create_options ));
1142 if (paccess_mask) {
1143 *paccess_mask = access_mask;
1145 if (pshare_mode) {
1146 *pshare_mode = share_mode;
1148 if (pcreate_disposition) {
1149 *pcreate_disposition = create_disposition;
1151 if (pcreate_options) {
1152 *pcreate_options = create_options;
1155 return True;
1159 static void schedule_defer_open(struct share_mode_lock *lck,
1160 struct timeval request_time,
1161 struct smb_request *req)
1163 struct deferred_open_record state;
1165 /* This is a relative time, added to the absolute
1166 request_time value to get the absolute timeout time.
1167 Note that if this is the second or greater time we enter
1168 this codepath for this particular request mid then
1169 request_time is left as the absolute time of the *first*
1170 time this request mid was processed. This is what allows
1171 the request to eventually time out. */
1173 struct timeval timeout;
1175 /* Normally the smbd we asked should respond within
1176 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1177 * the client did, give twice the timeout as a safety
1178 * measure here in case the other smbd is stuck
1179 * somewhere else. */
1181 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1183 /* Nothing actually uses state.delayed_for_oplocks
1184 but it's handy to differentiate in debug messages
1185 between a 30 second delay due to oplock break, and
1186 a 1 second delay for share mode conflicts. */
1188 state.delayed_for_oplocks = True;
1189 state.id = lck->id;
1191 if (!request_timed_out(request_time, timeout)) {
1192 defer_open(lck, request_time, timeout, req, &state);
1196 /****************************************************************************
1197 Work out what access_mask to use from what the client sent us.
1198 ****************************************************************************/
1200 static NTSTATUS calculate_access_mask(connection_struct *conn,
1201 const char *fname,
1202 bool file_existed,
1203 uint32_t access_mask,
1204 uint32_t *access_mask_out)
1206 NTSTATUS status;
1209 * Convert GENERIC bits to specific bits.
1212 se_map_generic(&access_mask, &file_generic_mapping);
1214 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1215 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1216 if (file_existed) {
1218 struct security_descriptor *sd;
1219 uint32_t access_granted = 0;
1221 status = SMB_VFS_GET_NT_ACL(conn, fname,
1222 (OWNER_SECURITY_INFORMATION |
1223 GROUP_SECURITY_INFORMATION |
1224 DACL_SECURITY_INFORMATION),&sd);
1226 if (!NT_STATUS_IS_OK(status)) {
1227 DEBUG(10, ("calculate_access_mask: Could not get acl "
1228 "on file %s: %s\n",
1229 fname,
1230 nt_errstr(status)));
1231 return NT_STATUS_ACCESS_DENIED;
1234 status = smb1_file_se_access_check(sd,
1235 conn->server_info->ptok,
1236 access_mask,
1237 &access_granted);
1239 TALLOC_FREE(sd);
1241 if (!NT_STATUS_IS_OK(status)) {
1242 DEBUG(10, ("calculate_access_mask: Access denied on "
1243 "file %s: when calculating maximum access\n",
1244 fname));
1245 return NT_STATUS_ACCESS_DENIED;
1248 access_mask = access_granted;
1249 } else {
1250 access_mask = FILE_GENERIC_ALL;
1254 *access_mask_out = access_mask;
1255 return NT_STATUS_OK;
1258 /****************************************************************************
1259 Open a file with a share mode. Passed in an already created files_struct *.
1260 ****************************************************************************/
1262 static NTSTATUS open_file_ntcreate_internal(connection_struct *conn,
1263 struct smb_request *req,
1264 const char *fname,
1265 SMB_STRUCT_STAT *psbuf,
1266 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1267 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1268 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1269 uint32 create_options, /* options such as delete on close. */
1270 uint32 new_dos_attributes, /* attributes used for new file. */
1271 int oplock_request, /* internal Samba oplock codes. */
1272 /* Information (FILE_EXISTS etc.) */
1273 int *pinfo,
1274 files_struct *fsp)
1276 int flags=0;
1277 int flags2=0;
1278 bool file_existed = VALID_STAT(*psbuf);
1279 bool def_acl = False;
1280 bool posix_open = False;
1281 bool new_file_created = False;
1282 struct file_id id;
1283 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1284 mode_t new_unx_mode = (mode_t)0;
1285 mode_t unx_mode = (mode_t)0;
1286 int info;
1287 uint32 existing_dos_attributes = 0;
1288 struct pending_message_list *pml = NULL;
1289 struct timeval request_time = timeval_zero();
1290 struct share_mode_lock *lck = NULL;
1291 uint32 open_access_mask = access_mask;
1292 NTSTATUS status;
1293 int ret_flock;
1294 char *parent_dir;
1295 const char *newname;
1297 ZERO_STRUCT(id);
1299 if (conn->printer) {
1301 * Printers are handled completely differently.
1302 * Most of the passed parameters are ignored.
1305 if (pinfo) {
1306 *pinfo = FILE_WAS_CREATED;
1309 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n", fname));
1311 return print_fsp_open(conn, fname, req->vuid, fsp);
1314 if (!parent_dirname_talloc(talloc_tos(), fname, &parent_dir,
1315 &newname)) {
1316 return NT_STATUS_NO_MEMORY;
1319 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1320 posix_open = True;
1321 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1322 new_dos_attributes = 0;
1323 } else {
1324 /* We add aARCH to this as this mode is only used if the file is
1325 * created new. */
1326 unx_mode = unix_mode(conn, new_dos_attributes | aARCH, fname,
1327 parent_dir);
1330 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1331 "access_mask=0x%x share_access=0x%x "
1332 "create_disposition = 0x%x create_options=0x%x "
1333 "unix mode=0%o oplock_request=%d\n",
1334 fname, new_dos_attributes, access_mask, share_access,
1335 create_disposition, create_options, unx_mode,
1336 oplock_request));
1338 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
1339 DEBUG(0, ("No smb request but not an internal only open!\n"));
1340 return NT_STATUS_INTERNAL_ERROR;
1344 * Only non-internal opens can be deferred at all
1347 if ((req != NULL)
1348 && ((pml = get_open_deferred_message(req->mid)) != NULL)) {
1349 struct deferred_open_record *state =
1350 (struct deferred_open_record *)pml->private_data.data;
1352 /* Remember the absolute time of the original
1353 request with this mid. We'll use it later to
1354 see if this has timed out. */
1356 request_time = pml->request_time;
1358 /* Remove the deferred open entry under lock. */
1359 lck = get_share_mode_lock(talloc_tos(), state->id, NULL, NULL,
1360 NULL);
1361 if (lck == NULL) {
1362 DEBUG(0, ("could not get share mode lock\n"));
1363 } else {
1364 del_deferred_open_entry(lck, req->mid);
1365 TALLOC_FREE(lck);
1368 /* Ensure we don't reprocess this message. */
1369 remove_deferred_open_smb_message(req->mid);
1372 status = check_name(conn, fname);
1373 if (!NT_STATUS_IS_OK(status)) {
1374 return status;
1377 if (!posix_open) {
1378 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
1379 if (file_existed) {
1380 existing_dos_attributes = dos_mode(conn, fname, psbuf);
1384 /* ignore any oplock requests if oplocks are disabled */
1385 if (!lp_oplocks(SNUM(conn)) || global_client_failed_oplock_break ||
1386 IS_VETO_OPLOCK_PATH(conn, fname)) {
1387 /* Mask off everything except the private Samba bits. */
1388 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
1391 /* this is for OS/2 long file names - say we don't support them */
1392 if (!lp_posix_pathnames() && strstr(fname,".+,;=[].")) {
1393 /* OS/2 Workplace shell fix may be main code stream in a later
1394 * release. */
1395 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1396 "supported.\n"));
1397 if (use_nt_status()) {
1398 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1400 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
1403 switch( create_disposition ) {
1405 * Currently we're using FILE_SUPERSEDE as the same as
1406 * FILE_OVERWRITE_IF but they really are
1407 * different. FILE_SUPERSEDE deletes an existing file
1408 * (requiring delete access) then recreates it.
1410 case FILE_SUPERSEDE:
1411 /* If file exists replace/overwrite. If file doesn't
1412 * exist create. */
1413 flags2 |= (O_CREAT | O_TRUNC);
1414 break;
1416 case FILE_OVERWRITE_IF:
1417 /* If file exists replace/overwrite. If file doesn't
1418 * exist create. */
1419 flags2 |= (O_CREAT | O_TRUNC);
1420 break;
1422 case FILE_OPEN:
1423 /* If file exists open. If file doesn't exist error. */
1424 if (!file_existed) {
1425 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1426 "requested for file %s and file "
1427 "doesn't exist.\n", fname ));
1428 errno = ENOENT;
1429 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1431 break;
1433 case FILE_OVERWRITE:
1434 /* If file exists overwrite. If file doesn't exist
1435 * error. */
1436 if (!file_existed) {
1437 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1438 "requested for file %s and file "
1439 "doesn't exist.\n", fname ));
1440 errno = ENOENT;
1441 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1443 flags2 |= O_TRUNC;
1444 break;
1446 case FILE_CREATE:
1447 /* If file exists error. If file doesn't exist
1448 * create. */
1449 if (file_existed) {
1450 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1451 "requested for file %s and file "
1452 "already exists.\n", fname ));
1453 if (S_ISDIR(psbuf->st_mode)) {
1454 errno = EISDIR;
1455 } else {
1456 errno = EEXIST;
1458 return map_nt_error_from_unix(errno);
1460 flags2 |= (O_CREAT|O_EXCL);
1461 break;
1463 case FILE_OPEN_IF:
1464 /* If file exists open. If file doesn't exist
1465 * create. */
1466 flags2 |= O_CREAT;
1467 break;
1469 default:
1470 return NT_STATUS_INVALID_PARAMETER;
1473 /* We only care about matching attributes on file exists and
1474 * overwrite. */
1476 if (!posix_open && file_existed && ((create_disposition == FILE_OVERWRITE) ||
1477 (create_disposition == FILE_OVERWRITE_IF))) {
1478 if (!open_match_attributes(conn, fname,
1479 existing_dos_attributes,
1480 new_dos_attributes, psbuf->st_mode,
1481 unx_mode, &new_unx_mode)) {
1482 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1483 "for file %s (%x %x) (0%o, 0%o)\n",
1484 fname, existing_dos_attributes,
1485 new_dos_attributes,
1486 (unsigned int)psbuf->st_mode,
1487 (unsigned int)unx_mode ));
1488 errno = EACCES;
1489 return NT_STATUS_ACCESS_DENIED;
1493 status = calculate_access_mask(conn, fname, file_existed,
1494 access_mask,
1495 &access_mask);
1496 if (!NT_STATUS_IS_OK(status)) {
1497 DEBUG(10, ("open_file_ntcreate: calculate_access_mask "
1498 "on file %s returned %s\n",
1499 fname,
1500 nt_errstr(status)));
1501 return status;
1504 open_access_mask = access_mask;
1506 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1507 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
1510 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1511 "access_mask=0x%x\n", fname, access_mask ));
1514 * Note that we ignore the append flag as append does not
1515 * mean the same thing under DOS and Unix.
1518 if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
1519 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1520 /* DENY_DOS opens are always underlying read-write on the
1521 file handle, no matter what the requested access mask
1522 says. */
1523 if ((create_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
1524 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|FILE_READ_EA|FILE_EXECUTE)) {
1525 flags = O_RDWR;
1526 } else {
1527 flags = O_WRONLY;
1529 } else {
1530 flags = O_RDONLY;
1534 * Currently we only look at FILE_WRITE_THROUGH for create options.
1537 #if defined(O_SYNC)
1538 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
1539 flags2 |= O_SYNC;
1541 #endif /* O_SYNC */
1543 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
1544 flags2 |= O_APPEND;
1547 if (!posix_open && !CAN_WRITE(conn)) {
1549 * We should really return a permission denied error if either
1550 * O_CREAT or O_TRUNC are set, but for compatibility with
1551 * older versions of Samba we just AND them out.
1553 flags2 &= ~(O_CREAT|O_TRUNC);
1557 * Ensure we can't write on a read-only share or file.
1560 if (flags != O_RDONLY && file_existed &&
1561 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
1562 DEBUG(5,("open_file_ntcreate: write access requested for "
1563 "file %s on read only %s\n",
1564 fname, !CAN_WRITE(conn) ? "share" : "file" ));
1565 errno = EACCES;
1566 return NT_STATUS_ACCESS_DENIED;
1569 fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
1570 fsp->share_access = share_access;
1571 fsp->fh->private_options = create_options;
1572 fsp->access_mask = open_access_mask; /* We change this to the
1573 * requested access_mask after
1574 * the open is done. */
1575 fsp->posix_open = posix_open;
1577 /* Ensure no SAMBA_PRIVATE bits can be set. */
1578 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1580 if (timeval_is_zero(&request_time)) {
1581 request_time = fsp->open_time;
1584 if (file_existed) {
1585 struct timespec old_write_time = get_mtimespec(psbuf);
1586 id = vfs_file_id_from_sbuf(conn, psbuf);
1588 lck = get_share_mode_lock(talloc_tos(), id,
1589 conn->connectpath,
1590 fname, &old_write_time);
1592 if (lck == NULL) {
1593 DEBUG(0, ("Could not get share mode lock\n"));
1594 return NT_STATUS_SHARING_VIOLATION;
1597 /* First pass - send break only on batch oplocks. */
1598 if ((req != NULL)
1599 && delay_for_oplocks(lck, fsp, req->mid, 1,
1600 oplock_request)) {
1601 schedule_defer_open(lck, request_time, req);
1602 TALLOC_FREE(lck);
1603 return NT_STATUS_SHARING_VIOLATION;
1606 /* Use the client requested access mask here, not the one we
1607 * open with. */
1608 status = open_mode_check(conn, fname, lck,
1609 access_mask, share_access,
1610 create_options, &file_existed);
1612 if (NT_STATUS_IS_OK(status)) {
1613 /* We might be going to allow this open. Check oplock
1614 * status again. */
1615 /* Second pass - send break for both batch or
1616 * exclusive oplocks. */
1617 if ((req != NULL)
1618 && delay_for_oplocks(lck, fsp, req->mid, 2,
1619 oplock_request)) {
1620 schedule_defer_open(lck, request_time, req);
1621 TALLOC_FREE(lck);
1622 return NT_STATUS_SHARING_VIOLATION;
1626 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
1627 /* DELETE_PENDING is not deferred for a second */
1628 TALLOC_FREE(lck);
1629 return status;
1632 if (!NT_STATUS_IS_OK(status)) {
1633 uint32 can_access_mask;
1634 bool can_access = True;
1636 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
1638 /* Check if this can be done with the deny_dos and fcb
1639 * calls. */
1640 if (create_options &
1641 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
1642 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
1643 if (req == NULL) {
1644 DEBUG(0, ("DOS open without an SMB "
1645 "request!\n"));
1646 TALLOC_FREE(lck);
1647 return NT_STATUS_INTERNAL_ERROR;
1650 /* Use the client requested access mask here,
1651 * not the one we open with. */
1652 status = fcb_or_dos_open(conn,
1653 fsp,
1654 fname,
1656 req->smbpid,
1657 req->vuid,
1658 access_mask,
1659 share_access,
1660 create_options);
1662 if (NT_STATUS_IS_OK(status)) {
1663 TALLOC_FREE(lck);
1664 if (pinfo) {
1665 *pinfo = FILE_WAS_OPENED;
1667 return NT_STATUS_OK;
1672 * This next line is a subtlety we need for
1673 * MS-Access. If a file open will fail due to share
1674 * permissions and also for security (access) reasons,
1675 * we need to return the access failed error, not the
1676 * share error. We can't open the file due to kernel
1677 * oplock deadlock (it's possible we failed above on
1678 * the open_mode_check()) so use a userspace check.
1681 if (flags & O_RDWR) {
1682 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
1683 } else if (flags & O_WRONLY) {
1684 can_access_mask = FILE_WRITE_DATA;
1685 } else {
1686 can_access_mask = FILE_READ_DATA;
1689 if (((can_access_mask & FILE_WRITE_DATA) && !CAN_WRITE(conn)) ||
1690 !can_access_file_data(conn,fname,psbuf,can_access_mask)) {
1691 can_access = False;
1695 * If we're returning a share violation, ensure we
1696 * cope with the braindead 1 second delay.
1699 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
1700 lp_defer_sharing_violations()) {
1701 struct timeval timeout;
1702 struct deferred_open_record state;
1703 int timeout_usecs;
1705 /* this is a hack to speed up torture tests
1706 in 'make test' */
1707 timeout_usecs = lp_parm_int(SNUM(conn),
1708 "smbd","sharedelay",
1709 SHARING_VIOLATION_USEC_WAIT);
1711 /* This is a relative time, added to the absolute
1712 request_time value to get the absolute timeout time.
1713 Note that if this is the second or greater time we enter
1714 this codepath for this particular request mid then
1715 request_time is left as the absolute time of the *first*
1716 time this request mid was processed. This is what allows
1717 the request to eventually time out. */
1719 timeout = timeval_set(0, timeout_usecs);
1721 /* Nothing actually uses state.delayed_for_oplocks
1722 but it's handy to differentiate in debug messages
1723 between a 30 second delay due to oplock break, and
1724 a 1 second delay for share mode conflicts. */
1726 state.delayed_for_oplocks = False;
1727 state.id = id;
1729 if ((req != NULL)
1730 && !request_timed_out(request_time,
1731 timeout)) {
1732 defer_open(lck, request_time, timeout,
1733 req, &state);
1737 TALLOC_FREE(lck);
1738 if (can_access) {
1740 * We have detected a sharing violation here
1741 * so return the correct error code
1743 status = NT_STATUS_SHARING_VIOLATION;
1744 } else {
1745 status = NT_STATUS_ACCESS_DENIED;
1747 return status;
1751 * We exit this block with the share entry *locked*.....
1755 SMB_ASSERT(!file_existed || (lck != NULL));
1758 * Ensure we pay attention to default ACLs on directories if required.
1761 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
1762 (def_acl = directory_has_default_acl(conn, parent_dir))) {
1763 unx_mode = 0777;
1766 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
1767 "access_mask = 0x%x, open_access_mask = 0x%x\n",
1768 (unsigned int)flags, (unsigned int)flags2,
1769 (unsigned int)unx_mode, (unsigned int)access_mask,
1770 (unsigned int)open_access_mask));
1773 * open_file strips any O_TRUNC flags itself.
1776 fsp_open = open_file(fsp, conn, req, parent_dir, newname, fname, psbuf,
1777 flags|flags2, unx_mode, access_mask,
1778 open_access_mask);
1780 if (!NT_STATUS_IS_OK(fsp_open)) {
1781 if (lck != NULL) {
1782 TALLOC_FREE(lck);
1784 return fsp_open;
1787 if (!file_existed) {
1788 struct timespec old_write_time = get_mtimespec(psbuf);
1790 * Deal with the race condition where two smbd's detect the
1791 * file doesn't exist and do the create at the same time. One
1792 * of them will win and set a share mode, the other (ie. this
1793 * one) should check if the requested share mode for this
1794 * create is allowed.
1798 * Now the file exists and fsp is successfully opened,
1799 * fsp->dev and fsp->inode are valid and should replace the
1800 * dev=0,inode=0 from a non existent file. Spotted by
1801 * Nadav Danieli <nadavd@exanet.com>. JRA.
1804 id = fsp->file_id;
1806 lck = get_share_mode_lock(talloc_tos(), id,
1807 conn->connectpath,
1808 fname, &old_write_time);
1810 if (lck == NULL) {
1811 DEBUG(0, ("open_file_ntcreate: Could not get share "
1812 "mode lock for %s\n", fname));
1813 fd_close(fsp);
1814 return NT_STATUS_SHARING_VIOLATION;
1817 /* First pass - send break only on batch oplocks. */
1818 if ((req != NULL)
1819 && delay_for_oplocks(lck, fsp, req->mid, 1,
1820 oplock_request)) {
1821 schedule_defer_open(lck, request_time, req);
1822 TALLOC_FREE(lck);
1823 fd_close(fsp);
1824 return NT_STATUS_SHARING_VIOLATION;
1827 status = open_mode_check(conn, fname, lck,
1828 access_mask, share_access,
1829 create_options, &file_existed);
1831 if (NT_STATUS_IS_OK(status)) {
1832 /* We might be going to allow this open. Check oplock
1833 * status again. */
1834 /* Second pass - send break for both batch or
1835 * exclusive oplocks. */
1836 if ((req != NULL)
1837 && delay_for_oplocks(lck, fsp, req->mid, 2,
1838 oplock_request)) {
1839 schedule_defer_open(lck, request_time, req);
1840 TALLOC_FREE(lck);
1841 fd_close(fsp);
1842 return NT_STATUS_SHARING_VIOLATION;
1846 if (!NT_STATUS_IS_OK(status)) {
1847 struct deferred_open_record state;
1849 fd_close(fsp);
1851 state.delayed_for_oplocks = False;
1852 state.id = id;
1854 /* Do it all over again immediately. In the second
1855 * round we will find that the file existed and handle
1856 * the DELETE_PENDING and FCB cases correctly. No need
1857 * to duplicate the code here. Essentially this is a
1858 * "goto top of this function", but don't tell
1859 * anybody... */
1861 if (req != NULL) {
1862 defer_open(lck, request_time, timeval_zero(),
1863 req, &state);
1865 TALLOC_FREE(lck);
1866 return status;
1870 * We exit this block with the share entry *locked*.....
1875 SMB_ASSERT(lck != NULL);
1877 /* note that we ignore failure for the following. It is
1878 basically a hack for NFS, and NFS will never set one of
1879 these only read them. Nobody but Samba can ever set a deny
1880 mode and we have already checked our more authoritative
1881 locking database for permission to set this deny mode. If
1882 the kernel refuses the operations then the kernel is wrong.
1883 note that GPFS supports it as well - jmcd */
1885 if (fsp->fh->fd != -1) {
1886 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access);
1887 if(ret_flock == -1 ){
1889 TALLOC_FREE(lck);
1890 fd_close(fsp);
1892 return NT_STATUS_SHARING_VIOLATION;
1897 * At this point onwards, we can guarentee that the share entry
1898 * is locked, whether we created the file or not, and that the
1899 * deny mode is compatible with all current opens.
1903 * If requested, truncate the file.
1906 if (flags2&O_TRUNC) {
1908 * We are modifing the file after open - update the stat
1909 * struct..
1911 if ((SMB_VFS_FTRUNCATE(fsp, 0) == -1) ||
1912 (SMB_VFS_FSTAT(fsp, psbuf)==-1)) {
1913 status = map_nt_error_from_unix(errno);
1914 TALLOC_FREE(lck);
1915 fd_close(fsp);
1916 return status;
1920 /* Record the options we were opened with. */
1921 fsp->share_access = share_access;
1922 fsp->fh->private_options = create_options;
1924 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
1926 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
1928 if (file_existed) {
1929 /* stat opens on existing files don't get oplocks. */
1930 if (is_stat_open(open_access_mask)) {
1931 fsp->oplock_type = NO_OPLOCK;
1934 if (!(flags2 & O_TRUNC)) {
1935 info = FILE_WAS_OPENED;
1936 } else {
1937 info = FILE_WAS_OVERWRITTEN;
1939 } else {
1940 info = FILE_WAS_CREATED;
1943 if (pinfo) {
1944 *pinfo = info;
1948 * Setup the oplock info in both the shared memory and
1949 * file structs.
1952 if ((fsp->oplock_type != NO_OPLOCK) &&
1953 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK)) {
1954 if (!set_file_oplock(fsp, fsp->oplock_type)) {
1955 /* Could not get the kernel oplock */
1956 fsp->oplock_type = NO_OPLOCK;
1960 if (info == FILE_WAS_OVERWRITTEN || info == FILE_WAS_CREATED || info == FILE_WAS_SUPERSEDED) {
1961 new_file_created = True;
1964 set_share_mode(lck, fsp, conn->server_info->utok.uid, 0,
1965 fsp->oplock_type, new_file_created);
1967 /* Handle strange delete on close create semantics. */
1968 if ((create_options & FILE_DELETE_ON_CLOSE)
1969 && (((conn->fs_capabilities & FILE_NAMED_STREAMS)
1970 && is_ntfs_stream_name(fname))
1971 || can_set_initial_delete_on_close(lck))) {
1972 status = can_set_delete_on_close(fsp, True, new_dos_attributes);
1974 if (!NT_STATUS_IS_OK(status)) {
1975 /* Remember to delete the mode we just added. */
1976 del_share_mode(lck, fsp);
1977 TALLOC_FREE(lck);
1978 fd_close(fsp);
1979 return status;
1981 /* Note that here we set the *inital* delete on close flag,
1982 not the regular one. The magic gets handled in close. */
1983 fsp->initial_delete_on_close = True;
1986 if (new_file_created) {
1987 /* Files should be initially set as archive */
1988 if (lp_map_archive(SNUM(conn)) ||
1989 lp_store_dos_attributes(SNUM(conn))) {
1990 if (!posix_open) {
1991 SMB_STRUCT_STAT tmp_sbuf;
1992 SET_STAT_INVALID(tmp_sbuf);
1993 if (file_set_dosmode(
1994 conn, fname,
1995 new_dos_attributes | aARCH,
1996 &tmp_sbuf, parent_dir,
1997 true) == 0) {
1998 unx_mode = tmp_sbuf.st_mode;
2005 * Take care of inherited ACLs on created files - if default ACL not
2006 * selected.
2009 if (!posix_open && !file_existed && !def_acl) {
2011 int saved_errno = errno; /* We might get ENOSYS in the next
2012 * call.. */
2014 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2015 errno == ENOSYS) {
2016 errno = saved_errno; /* Ignore ENOSYS */
2019 } else if (new_unx_mode) {
2021 int ret = -1;
2023 /* Attributes need changing. File already existed. */
2026 int saved_errno = errno; /* We might get ENOSYS in the
2027 * next call.. */
2028 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2030 if (ret == -1 && errno == ENOSYS) {
2031 errno = saved_errno; /* Ignore ENOSYS */
2032 } else {
2033 DEBUG(5, ("open_file_ntcreate: reset "
2034 "attributes of file %s to 0%o\n",
2035 fname, (unsigned int)new_unx_mode));
2036 ret = 0; /* Don't do the fchmod below. */
2040 if ((ret == -1) &&
2041 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2042 DEBUG(5, ("open_file_ntcreate: failed to reset "
2043 "attributes of file %s to 0%o\n",
2044 fname, (unsigned int)new_unx_mode));
2047 /* If this is a successful open, we must remove any deferred open
2048 * records. */
2049 if (req != NULL) {
2050 del_deferred_open_entry(lck, req->mid);
2052 TALLOC_FREE(lck);
2054 return NT_STATUS_OK;
2057 /****************************************************************************
2058 Open a file with a share mode.
2059 ****************************************************************************/
2061 NTSTATUS open_file_ntcreate(connection_struct *conn,
2062 struct smb_request *req,
2063 const char *fname,
2064 SMB_STRUCT_STAT *psbuf,
2065 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
2066 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
2067 uint32 create_disposition, /* FILE_OPEN_IF etc. */
2068 uint32 create_options, /* options such as delete on close. */
2069 uint32 new_dos_attributes, /* attributes used for new file. */
2070 int oplock_request, /* internal Samba oplock codes. */
2071 /* Information (FILE_EXISTS etc.) */
2072 int *pinfo,
2073 files_struct **result)
2075 NTSTATUS status;
2076 files_struct *fsp = NULL;
2078 *result = NULL;
2080 status = file_new(conn, &fsp);
2081 if(!NT_STATUS_IS_OK(status)) {
2082 return status;
2085 status = open_file_ntcreate_internal(conn,
2086 req,
2087 fname,
2088 psbuf,
2089 access_mask,
2090 share_access,
2091 create_disposition,
2092 create_options,
2093 new_dos_attributes,
2094 oplock_request,
2095 pinfo,
2096 fsp);
2098 if(!NT_STATUS_IS_OK(status)) {
2099 file_free(fsp);
2100 return status;
2103 *result = fsp;
2104 return status;
2107 /****************************************************************************
2108 Open a file for for write to ensure that we can fchmod it.
2109 ****************************************************************************/
2111 NTSTATUS open_file_fchmod(connection_struct *conn, const char *fname,
2112 SMB_STRUCT_STAT *psbuf, files_struct **result)
2114 files_struct *fsp = NULL;
2115 NTSTATUS status;
2117 if (!VALID_STAT(*psbuf)) {
2118 return NT_STATUS_INVALID_PARAMETER;
2121 status = file_new(conn, &fsp);
2122 if(!NT_STATUS_IS_OK(status)) {
2123 return status;
2126 /* note! we must use a non-zero desired access or we don't get
2127 a real file descriptor. Oh what a twisted web we weave. */
2128 status = open_file(fsp, conn, NULL, NULL, NULL, fname, psbuf, O_WRONLY,
2129 0, FILE_WRITE_DATA, FILE_WRITE_DATA);
2132 * This is not a user visible file open.
2133 * Don't set a share mode.
2136 if (!NT_STATUS_IS_OK(status)) {
2137 file_free(fsp);
2138 return status;
2141 *result = fsp;
2142 return NT_STATUS_OK;
2145 /****************************************************************************
2146 Close the fchmod file fd - ensure no locks are lost.
2147 ****************************************************************************/
2149 NTSTATUS close_file_fchmod(files_struct *fsp)
2151 NTSTATUS status = fd_close(fsp);
2152 file_free(fsp);
2153 return status;
2156 static NTSTATUS mkdir_internal(connection_struct *conn,
2157 const char *name,
2158 uint32 file_attributes,
2159 SMB_STRUCT_STAT *psbuf)
2161 mode_t mode;
2162 char *parent_dir;
2163 const char *dirname;
2164 NTSTATUS status;
2165 bool posix_open = false;
2167 if(!CAN_WRITE(conn)) {
2168 DEBUG(5,("mkdir_internal: failing create on read-only share "
2169 "%s\n", lp_servicename(SNUM(conn))));
2170 return NT_STATUS_ACCESS_DENIED;
2173 status = check_name(conn, name);
2174 if (!NT_STATUS_IS_OK(status)) {
2175 return status;
2178 if (!parent_dirname_talloc(talloc_tos(), name, &parent_dir,
2179 &dirname)) {
2180 return NT_STATUS_NO_MEMORY;
2183 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2184 posix_open = true;
2185 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2186 } else {
2187 mode = unix_mode(conn, aDIR, name, parent_dir);
2190 if (SMB_VFS_MKDIR(conn, name, mode) != 0) {
2191 return map_nt_error_from_unix(errno);
2194 /* Ensure we're checking for a symlink here.... */
2195 /* We don't want to get caught by a symlink racer. */
2197 if (SMB_VFS_LSTAT(conn, name, psbuf) == -1) {
2198 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2199 name, strerror(errno)));
2200 return map_nt_error_from_unix(errno);
2203 if (!S_ISDIR(psbuf->st_mode)) {
2204 DEBUG(0, ("Directory just '%s' created is not a directory\n",
2205 name));
2206 return NT_STATUS_ACCESS_DENIED;
2209 if (lp_store_dos_attributes(SNUM(conn))) {
2210 if (!posix_open) {
2211 file_set_dosmode(conn, name,
2212 file_attributes | aDIR, NULL,
2213 parent_dir,
2214 true);
2218 if (lp_inherit_perms(SNUM(conn))) {
2219 inherit_access_posix_acl(conn, parent_dir, name, mode);
2222 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
2224 * Check if high bits should have been set,
2225 * then (if bits are missing): add them.
2226 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2227 * dir.
2229 if (mode & ~(S_IRWXU|S_IRWXG|S_IRWXO) && (mode & ~psbuf->st_mode)) {
2230 SMB_VFS_CHMOD(conn, name,
2231 psbuf->st_mode | (mode & ~psbuf->st_mode));
2235 /* Change the owner if required. */
2236 if (lp_inherit_owner(SNUM(conn))) {
2237 change_dir_owner_to_parent(conn, parent_dir, name, psbuf);
2240 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2241 name);
2243 return NT_STATUS_OK;
2246 /****************************************************************************
2247 Open a directory from an NT SMB call.
2248 ****************************************************************************/
2250 NTSTATUS open_directory(connection_struct *conn,
2251 struct smb_request *req,
2252 const char *fname,
2253 SMB_STRUCT_STAT *psbuf,
2254 uint32 access_mask,
2255 uint32 share_access,
2256 uint32 create_disposition,
2257 uint32 create_options,
2258 uint32 file_attributes,
2259 int *pinfo,
2260 files_struct **result)
2262 files_struct *fsp = NULL;
2263 bool dir_existed = VALID_STAT(*psbuf) ? True : False;
2264 struct share_mode_lock *lck = NULL;
2265 NTSTATUS status;
2266 struct timespec mtimespec;
2267 int info = 0;
2269 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2270 "share_access = 0x%x create_options = 0x%x, "
2271 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2272 fname,
2273 (unsigned int)access_mask,
2274 (unsigned int)share_access,
2275 (unsigned int)create_options,
2276 (unsigned int)create_disposition,
2277 (unsigned int)file_attributes));
2279 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
2280 (conn->fs_capabilities & FILE_NAMED_STREAMS) &&
2281 is_ntfs_stream_name(fname)) {
2282 DEBUG(2, ("open_directory: %s is a stream name!\n", fname));
2283 return NT_STATUS_NOT_A_DIRECTORY;
2286 status = calculate_access_mask(conn, fname, dir_existed,
2287 access_mask,
2288 &access_mask);
2289 if (!NT_STATUS_IS_OK(status)) {
2290 DEBUG(10, ("open_directory: calculate_access_mask "
2291 "on file %s returned %s\n",
2292 fname,
2293 nt_errstr(status)));
2294 return status;
2297 switch( create_disposition ) {
2298 case FILE_OPEN:
2300 info = FILE_WAS_OPENED;
2303 * We want to follow symlinks here.
2306 if (SMB_VFS_STAT(conn, fname, psbuf) != 0) {
2307 return map_nt_error_from_unix(errno);
2310 break;
2312 case FILE_CREATE:
2314 /* If directory exists error. If directory doesn't
2315 * exist create. */
2317 status = mkdir_internal(conn,
2318 fname,
2319 file_attributes,
2320 psbuf);
2322 if (!NT_STATUS_IS_OK(status)) {
2323 DEBUG(2, ("open_directory: unable to create "
2324 "%s. Error was %s\n", fname,
2325 nt_errstr(status)));
2326 return status;
2329 info = FILE_WAS_CREATED;
2330 break;
2332 case FILE_OPEN_IF:
2334 * If directory exists open. If directory doesn't
2335 * exist create.
2338 status = mkdir_internal(conn,
2339 fname,
2340 file_attributes,
2341 psbuf);
2343 if (NT_STATUS_IS_OK(status)) {
2344 info = FILE_WAS_CREATED;
2347 if (NT_STATUS_EQUAL(status,
2348 NT_STATUS_OBJECT_NAME_COLLISION)) {
2349 info = FILE_WAS_OPENED;
2350 status = NT_STATUS_OK;
2353 break;
2355 case FILE_SUPERSEDE:
2356 case FILE_OVERWRITE:
2357 case FILE_OVERWRITE_IF:
2358 default:
2359 DEBUG(5,("open_directory: invalid create_disposition "
2360 "0x%x for directory %s\n",
2361 (unsigned int)create_disposition, fname));
2362 return NT_STATUS_INVALID_PARAMETER;
2365 if(!S_ISDIR(psbuf->st_mode)) {
2366 DEBUG(5,("open_directory: %s is not a directory !\n",
2367 fname ));
2368 return NT_STATUS_NOT_A_DIRECTORY;
2371 if (info == FILE_WAS_OPENED) {
2372 status = check_open_rights(conn,
2373 fname,
2374 access_mask);
2375 if (!NT_STATUS_IS_OK(status)) {
2376 DEBUG(10, ("open_directory: check_open_rights on "
2377 "file %s failed with %s\n",
2378 fname,
2379 nt_errstr(status)));
2380 return status;
2384 status = file_new(conn, &fsp);
2385 if(!NT_STATUS_IS_OK(status)) {
2386 return status;
2390 * Setup the files_struct for it.
2393 fsp->mode = psbuf->st_mode;
2394 fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
2395 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
2396 fsp->file_pid = req ? req->smbpid : 0;
2397 fsp->can_lock = False;
2398 fsp->can_read = False;
2399 fsp->can_write = False;
2401 fsp->share_access = share_access;
2402 fsp->fh->private_options = create_options;
2404 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2406 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2407 fsp->print_file = False;
2408 fsp->modified = False;
2409 fsp->oplock_type = NO_OPLOCK;
2410 fsp->sent_oplock_break = NO_BREAK_SENT;
2411 fsp->is_directory = True;
2412 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
2414 string_set(&fsp->fsp_name,fname);
2416 mtimespec = get_mtimespec(psbuf);
2418 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
2419 conn->connectpath,
2420 fname, &mtimespec);
2422 if (lck == NULL) {
2423 DEBUG(0, ("open_directory: Could not get share mode lock for %s\n", fname));
2424 file_free(fsp);
2425 return NT_STATUS_SHARING_VIOLATION;
2428 status = open_mode_check(conn, fname, lck,
2429 access_mask, share_access,
2430 create_options, &dir_existed);
2432 if (!NT_STATUS_IS_OK(status)) {
2433 TALLOC_FREE(lck);
2434 file_free(fsp);
2435 return status;
2438 set_share_mode(lck, fsp, conn->server_info->utok.uid, 0, NO_OPLOCK,
2439 True);
2441 /* For directories the delete on close bit at open time seems
2442 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2443 if (create_options & FILE_DELETE_ON_CLOSE) {
2444 status = can_set_delete_on_close(fsp, True, 0);
2445 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
2446 TALLOC_FREE(lck);
2447 file_free(fsp);
2448 return status;
2451 if (NT_STATUS_IS_OK(status)) {
2452 /* Note that here we set the *inital* delete on close flag,
2453 not the regular one. The magic gets handled in close. */
2454 fsp->initial_delete_on_close = True;
2458 TALLOC_FREE(lck);
2460 if (pinfo) {
2461 *pinfo = info;
2464 *result = fsp;
2465 return NT_STATUS_OK;
2468 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req, const char *directory)
2470 NTSTATUS status;
2471 SMB_STRUCT_STAT sbuf;
2472 files_struct *fsp;
2474 SET_STAT_INVALID(sbuf);
2476 status = open_directory(conn, req, directory, &sbuf,
2477 FILE_READ_ATTRIBUTES, /* Just a stat open */
2478 FILE_SHARE_NONE, /* Ignored for stat opens */
2479 FILE_CREATE,
2481 FILE_ATTRIBUTE_DIRECTORY,
2482 NULL,
2483 &fsp);
2485 if (NT_STATUS_IS_OK(status)) {
2486 close_file(fsp, NORMAL_CLOSE);
2489 return status;
2492 /****************************************************************************
2493 Receive notification that one of our open files has been renamed by another
2494 smbd process.
2495 ****************************************************************************/
2497 void msg_file_was_renamed(struct messaging_context *msg,
2498 void *private_data,
2499 uint32_t msg_type,
2500 struct server_id server_id,
2501 DATA_BLOB *data)
2503 files_struct *fsp;
2504 char *frm = (char *)data->data;
2505 struct file_id id;
2506 const char *sharepath;
2507 const char *newname;
2508 size_t sp_len;
2510 if (data->data == NULL
2511 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
2512 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
2513 (int)data->length));
2514 return;
2517 /* Unpack the message. */
2518 pull_file_id_16(frm, &id);
2519 sharepath = &frm[16];
2520 newname = sharepath + strlen(sharepath) + 1;
2521 sp_len = strlen(sharepath);
2523 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
2524 "file_id %s\n",
2525 sharepath, newname, file_id_string_tos(&id)));
2527 for(fsp = file_find_di_first(id); fsp; fsp = file_find_di_next(fsp)) {
2528 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
2529 DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n",
2530 fsp->fnum, fsp->fsp_name, newname ));
2531 string_set(&fsp->fsp_name, newname);
2532 } else {
2533 /* TODO. JRA. */
2534 /* Now we have the complete path we can work out if this is
2535 actually within this share and adjust newname accordingly. */
2536 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
2537 "not sharepath %s) "
2538 "fnum %d from %s -> %s\n",
2539 fsp->conn->connectpath,
2540 sharepath,
2541 fsp->fnum,
2542 fsp->fsp_name,
2543 newname ));
2548 struct case_semantics_state {
2549 connection_struct *conn;
2550 bool case_sensitive;
2551 bool case_preserve;
2552 bool short_case_preserve;
2555 /****************************************************************************
2556 Restore case semantics.
2557 ****************************************************************************/
2558 static int restore_case_semantics(struct case_semantics_state *state)
2560 state->conn->case_sensitive = state->case_sensitive;
2561 state->conn->case_preserve = state->case_preserve;
2562 state->conn->short_case_preserve = state->short_case_preserve;
2563 return 0;
2566 /****************************************************************************
2567 Save case semantics.
2568 ****************************************************************************/
2569 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
2570 connection_struct *conn)
2572 struct case_semantics_state *result;
2574 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
2575 DEBUG(0, ("talloc failed\n"));
2576 return NULL;
2579 result->conn = conn;
2580 result->case_sensitive = conn->case_sensitive;
2581 result->case_preserve = conn->case_preserve;
2582 result->short_case_preserve = conn->short_case_preserve;
2584 /* Set to POSIX. */
2585 conn->case_sensitive = True;
2586 conn->case_preserve = True;
2587 conn->short_case_preserve = True;
2589 talloc_set_destructor(result, restore_case_semantics);
2591 return result;
2595 * If a main file is opened for delete, all streams need to be checked for
2596 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
2597 * If that works, delete them all by setting the delete on close and close.
2600 static NTSTATUS open_streams_for_delete(connection_struct *conn,
2601 const char *fname)
2603 struct stream_struct *stream_info;
2604 files_struct **streams;
2605 int i;
2606 unsigned int num_streams;
2607 TALLOC_CTX *frame = talloc_stackframe();
2608 NTSTATUS status;
2610 status = SMB_VFS_STREAMINFO(conn, NULL, fname, talloc_tos(),
2611 &num_streams, &stream_info);
2613 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
2614 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
2615 DEBUG(10, ("no streams around\n"));
2616 TALLOC_FREE(frame);
2617 return NT_STATUS_OK;
2620 if (!NT_STATUS_IS_OK(status)) {
2621 DEBUG(10, ("SMB_VFS_STREAMINFO failed: %s\n",
2622 nt_errstr(status)));
2623 goto fail;
2626 DEBUG(10, ("open_streams_for_delete found %d streams\n",
2627 num_streams));
2629 if (num_streams == 0) {
2630 TALLOC_FREE(frame);
2631 return NT_STATUS_OK;
2634 streams = TALLOC_ARRAY(talloc_tos(), files_struct *, num_streams);
2635 if (streams == NULL) {
2636 DEBUG(0, ("talloc failed\n"));
2637 status = NT_STATUS_NO_MEMORY;
2638 goto fail;
2641 for (i=0; i<num_streams; i++) {
2642 char *streamname;
2644 if (strequal(stream_info[i].name, "::$DATA")) {
2645 streams[i] = NULL;
2646 continue;
2649 streamname = talloc_asprintf(talloc_tos(), "%s%s", fname,
2650 stream_info[i].name);
2652 if (streamname == NULL) {
2653 DEBUG(0, ("talloc_aprintf failed\n"));
2654 status = NT_STATUS_NO_MEMORY;
2655 goto fail;
2658 status = create_file_unixpath
2659 (conn, /* conn */
2660 NULL, /* req */
2661 streamname, /* fname */
2662 DELETE_ACCESS, /* access_mask */
2663 FILE_SHARE_READ | FILE_SHARE_WRITE
2664 | FILE_SHARE_DELETE, /* share_access */
2665 FILE_OPEN, /* create_disposition*/
2666 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* create_options */
2667 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
2668 0, /* oplock_request */
2669 0, /* allocation_size */
2670 NULL, /* sd */
2671 NULL, /* ea_list */
2672 &streams[i], /* result */
2673 NULL, /* pinfo */
2674 NULL); /* psbuf */
2676 TALLOC_FREE(streamname);
2678 if (!NT_STATUS_IS_OK(status)) {
2679 DEBUG(10, ("Could not open stream %s: %s\n",
2680 streamname, nt_errstr(status)));
2681 break;
2686 * don't touch the variable "status" beyond this point :-)
2689 for (i -= 1 ; i >= 0; i--) {
2690 if (streams[i] == NULL) {
2691 continue;
2694 DEBUG(10, ("Closing stream # %d, %s\n", i,
2695 streams[i]->fsp_name));
2696 close_file(streams[i], NORMAL_CLOSE);
2699 fail:
2700 TALLOC_FREE(frame);
2701 return status;
2705 * Wrapper around open_file_ntcreate and open_directory
2708 NTSTATUS create_file_unixpath(connection_struct *conn,
2709 struct smb_request *req,
2710 const char *fname,
2711 uint32_t access_mask,
2712 uint32_t share_access,
2713 uint32_t create_disposition,
2714 uint32_t create_options,
2715 uint32_t file_attributes,
2716 uint32_t oplock_request,
2717 SMB_BIG_UINT allocation_size,
2718 struct security_descriptor *sd,
2719 struct ea_list *ea_list,
2721 files_struct **result,
2722 int *pinfo,
2723 SMB_STRUCT_STAT *psbuf)
2725 SMB_STRUCT_STAT sbuf;
2726 int info = FILE_WAS_OPENED;
2727 files_struct *base_fsp = NULL;
2728 files_struct *fsp = NULL;
2729 NTSTATUS status;
2731 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
2732 "file_attributes = 0x%x, share_access = 0x%x, "
2733 "create_disposition = 0x%x create_options = 0x%x "
2734 "oplock_request = 0x%x ea_list = 0x%p, sd = 0x%p, "
2735 "fname = %s\n",
2736 (unsigned int)access_mask,
2737 (unsigned int)file_attributes,
2738 (unsigned int)share_access,
2739 (unsigned int)create_disposition,
2740 (unsigned int)create_options,
2741 (unsigned int)oplock_request,
2742 ea_list, sd, fname));
2744 if (create_options & FILE_OPEN_BY_FILE_ID) {
2745 status = NT_STATUS_NOT_SUPPORTED;
2746 goto fail;
2749 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
2750 status = NT_STATUS_INVALID_PARAMETER;
2751 goto fail;
2754 if (req == NULL) {
2755 oplock_request |= INTERNAL_OPEN_ONLY;
2758 if (psbuf != NULL) {
2759 sbuf = *psbuf;
2761 else {
2762 if (SMB_VFS_STAT(conn, fname, &sbuf) == -1) {
2763 SET_STAT_INVALID(sbuf);
2767 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
2768 && (access_mask & DELETE_ACCESS)
2769 && !is_ntfs_stream_name(fname)) {
2771 * We can't open a file with DELETE access if any of the
2772 * streams is open without FILE_SHARE_DELETE
2774 status = open_streams_for_delete(conn, fname);
2776 if (!NT_STATUS_IS_OK(status)) {
2777 goto fail;
2781 /* This is the correct thing to do (check every time) but can_delete
2782 * is expensive (it may have to read the parent directory
2783 * permissions). So for now we're not doing it unless we have a strong
2784 * hint the client is really going to delete this file. If the client
2785 * is forcing FILE_CREATE let the filesystem take care of the
2786 * permissions. */
2788 /* Setting FILE_SHARE_DELETE is the hint. */
2790 if (lp_acl_check_permissions(SNUM(conn))
2791 && (create_disposition != FILE_CREATE)
2792 && (share_access & FILE_SHARE_DELETE)
2793 && (access_mask & DELETE_ACCESS)
2794 && (!can_delete_file_in_directory(conn, fname))) {
2795 status = NT_STATUS_ACCESS_DENIED;
2796 goto fail;
2799 #if 0
2800 /* We need to support SeSecurityPrivilege for this. */
2801 if ((access_mask & SEC_RIGHT_SYSTEM_SECURITY) &&
2802 !user_has_privileges(current_user.nt_user_token,
2803 &se_security)) {
2804 status = NT_STATUS_PRIVILEGE_NOT_HELD;
2805 goto fail;
2807 #endif
2809 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
2810 && is_ntfs_stream_name(fname)
2811 && (!(create_options & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
2812 char *base;
2813 uint32 base_create_disposition;
2815 if (create_options & FILE_DIRECTORY_FILE) {
2816 status = NT_STATUS_NOT_A_DIRECTORY;
2817 goto fail;
2820 status = split_ntfs_stream_name(talloc_tos(), fname,
2821 &base, NULL);
2822 if (!NT_STATUS_IS_OK(status)) {
2823 DEBUG(10, ("create_file_unixpath: "
2824 "split_ntfs_stream_name failed: %s\n",
2825 nt_errstr(status)));
2826 goto fail;
2829 SMB_ASSERT(!is_ntfs_stream_name(base)); /* paranoia.. */
2831 switch (create_disposition) {
2832 case FILE_OPEN:
2833 base_create_disposition = FILE_OPEN;
2834 break;
2835 default:
2836 base_create_disposition = FILE_OPEN_IF;
2837 break;
2840 status = create_file_unixpath(conn, NULL, base, 0,
2841 FILE_SHARE_READ
2842 | FILE_SHARE_WRITE
2843 | FILE_SHARE_DELETE,
2844 base_create_disposition,
2845 0, 0, 0, 0, NULL, NULL,
2846 &base_fsp, NULL, NULL);
2847 if (!NT_STATUS_IS_OK(status)) {
2848 DEBUG(10, ("create_file_unixpath for base %s failed: "
2849 "%s\n", base, nt_errstr(status)));
2850 goto fail;
2855 * If it's a request for a directory open, deal with it separately.
2858 if (create_options & FILE_DIRECTORY_FILE) {
2860 if (create_options & FILE_NON_DIRECTORY_FILE) {
2861 status = NT_STATUS_INVALID_PARAMETER;
2862 goto fail;
2865 /* Can't open a temp directory. IFS kit test. */
2866 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
2867 status = NT_STATUS_INVALID_PARAMETER;
2868 goto fail;
2872 * We will get a create directory here if the Win32
2873 * app specified a security descriptor in the
2874 * CreateDirectory() call.
2877 oplock_request = 0;
2878 status = open_directory(
2879 conn, req, fname, &sbuf, access_mask, share_access,
2880 create_disposition, create_options, file_attributes,
2881 &info, &fsp);
2882 } else {
2885 * Ordinary file case.
2888 if (base_fsp) {
2890 * We're opening the stream element of a base_fsp
2891 * we already opened. We need to initialize
2892 * the fsp first, and set up the base_fsp pointer.
2894 status = file_new(conn, &fsp);
2895 if(!NT_STATUS_IS_OK(status)) {
2896 goto fail;
2899 fsp->base_fsp = base_fsp;
2901 status = open_file_ntcreate_internal(conn,
2902 req,
2903 fname,
2904 &sbuf,
2905 access_mask,
2906 share_access,
2907 create_disposition,
2908 create_options,
2909 file_attributes,
2910 oplock_request,
2911 &info,
2912 fsp);
2914 if(!NT_STATUS_IS_OK(status)) {
2915 file_free(fsp);
2916 fsp = NULL;
2918 } else {
2919 status = open_file_ntcreate(
2920 conn, req, fname, &sbuf, access_mask, share_access,
2921 create_disposition, create_options, file_attributes,
2922 oplock_request, &info, &fsp);
2925 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
2927 /* A stream open never opens a directory */
2929 if (base_fsp) {
2930 status = NT_STATUS_FILE_IS_A_DIRECTORY;
2931 goto fail;
2935 * Fail the open if it was explicitly a non-directory
2936 * file.
2939 if (create_options & FILE_NON_DIRECTORY_FILE) {
2940 status = NT_STATUS_FILE_IS_A_DIRECTORY;
2941 goto fail;
2944 oplock_request = 0;
2945 status = open_directory(
2946 conn, req, fname, &sbuf, access_mask,
2947 share_access, create_disposition,
2948 create_options, file_attributes,
2949 &info, &fsp);
2953 if (!NT_STATUS_IS_OK(status)) {
2954 goto fail;
2957 fsp->base_fsp = base_fsp;
2960 * According to the MS documentation, the only time the security
2961 * descriptor is applied to the opened file is iff we *created* the
2962 * file; an existing file stays the same.
2964 * Also, it seems (from observation) that you can open the file with
2965 * any access mask but you can still write the sd. We need to override
2966 * the granted access before we call set_sd
2967 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
2970 if ((sd != NULL) && (info == FILE_WAS_CREATED)
2971 && lp_nt_acl_support(SNUM(conn))) {
2973 uint32_t sec_info_sent = ALL_SECURITY_INFORMATION;
2974 uint32_t saved_access_mask = fsp->access_mask;
2976 if (sd->owner_sid == NULL) {
2977 sec_info_sent &= ~OWNER_SECURITY_INFORMATION;
2979 if (sd->group_sid == NULL) {
2980 sec_info_sent &= ~GROUP_SECURITY_INFORMATION;
2982 if (sd->sacl == NULL) {
2983 sec_info_sent &= ~SACL_SECURITY_INFORMATION;
2985 if (sd->dacl == NULL) {
2986 sec_info_sent &= ~DACL_SECURITY_INFORMATION;
2989 fsp->access_mask = FILE_GENERIC_ALL;
2991 /* Convert all the generic bits. */
2992 security_acl_map_generic(sd->dacl, &file_generic_mapping);
2993 security_acl_map_generic(sd->sacl, &file_generic_mapping);
2995 if (sec_info_sent & (OWNER_SECURITY_INFORMATION|
2996 GROUP_SECURITY_INFORMATION|
2997 DACL_SECURITY_INFORMATION|
2998 SACL_SECURITY_INFORMATION)) {
2999 status = SMB_VFS_FSET_NT_ACL(fsp, sec_info_sent, sd);
3002 fsp->access_mask = saved_access_mask;
3004 if (!NT_STATUS_IS_OK(status)) {
3005 goto fail;
3009 if ((ea_list != NULL) && (info == FILE_WAS_CREATED)) {
3010 status = set_ea(conn, fsp, fname, ea_list);
3011 if (!NT_STATUS_IS_OK(status)) {
3012 goto fail;
3016 if (!fsp->is_directory && S_ISDIR(sbuf.st_mode)) {
3017 status = NT_STATUS_ACCESS_DENIED;
3018 goto fail;
3021 /* Save the requested allocation size. */
3022 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
3023 if (allocation_size
3024 && (allocation_size > sbuf.st_size)) {
3025 fsp->initial_allocation_size = smb_roundup(
3026 fsp->conn, allocation_size);
3027 if (fsp->is_directory) {
3028 /* Can't set allocation size on a directory. */
3029 status = NT_STATUS_ACCESS_DENIED;
3030 goto fail;
3032 if (vfs_allocate_file_space(
3033 fsp, fsp->initial_allocation_size) == -1) {
3034 status = NT_STATUS_DISK_FULL;
3035 goto fail;
3037 } else {
3038 fsp->initial_allocation_size = smb_roundup(
3039 fsp->conn, (SMB_BIG_UINT)sbuf.st_size);
3043 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
3045 *result = fsp;
3046 if (pinfo != NULL) {
3047 *pinfo = info;
3049 if (psbuf != NULL) {
3050 if ((fsp->fh == NULL) || (fsp->fh->fd == -1)) {
3051 *psbuf = sbuf;
3053 else {
3054 SMB_VFS_FSTAT(fsp, psbuf);
3057 return NT_STATUS_OK;
3059 fail:
3060 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
3062 if (fsp != NULL) {
3063 if (base_fsp && fsp->base_fsp == base_fsp) {
3065 * The close_file below will close
3066 * fsp->base_fsp.
3068 base_fsp = NULL;
3070 close_file(fsp, ERROR_CLOSE);
3071 fsp = NULL;
3073 if (base_fsp != NULL) {
3074 close_file(base_fsp, ERROR_CLOSE);
3075 base_fsp = NULL;
3077 return status;
3080 NTSTATUS create_file(connection_struct *conn,
3081 struct smb_request *req,
3082 uint16_t root_dir_fid,
3083 const char *fname,
3084 uint32_t access_mask,
3085 uint32_t share_access,
3086 uint32_t create_disposition,
3087 uint32_t create_options,
3088 uint32_t file_attributes,
3089 uint32_t oplock_request,
3090 SMB_BIG_UINT allocation_size,
3091 struct security_descriptor *sd,
3092 struct ea_list *ea_list,
3094 files_struct **result,
3095 int *pinfo,
3096 SMB_STRUCT_STAT *psbuf)
3098 struct case_semantics_state *case_state = NULL;
3099 SMB_STRUCT_STAT sbuf;
3100 int info = FILE_WAS_OPENED;
3101 files_struct *fsp = NULL;
3102 NTSTATUS status;
3104 DEBUG(10,("create_file: access_mask = 0x%x "
3105 "file_attributes = 0x%x, share_access = 0x%x, "
3106 "create_disposition = 0x%x create_options = 0x%x "
3107 "oplock_request = 0x%x "
3108 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
3109 "fname = %s\n",
3110 (unsigned int)access_mask,
3111 (unsigned int)file_attributes,
3112 (unsigned int)share_access,
3113 (unsigned int)create_disposition,
3114 (unsigned int)create_options,
3115 (unsigned int)oplock_request,
3116 (unsigned int)root_dir_fid,
3117 ea_list, sd, fname));
3120 * Get the file name.
3123 if (root_dir_fid != 0) {
3125 * This filename is relative to a directory fid.
3127 char *parent_fname = NULL;
3128 files_struct *dir_fsp = file_fsp(root_dir_fid);
3130 if (dir_fsp == NULL) {
3131 status = NT_STATUS_INVALID_HANDLE;
3132 goto fail;
3135 if (!dir_fsp->is_directory) {
3138 * Check to see if this is a mac fork of some kind.
3141 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
3142 is_ntfs_stream_name(fname)) {
3143 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3144 goto fail;
3148 we need to handle the case when we get a
3149 relative open relative to a file and the
3150 pathname is blank - this is a reopen!
3151 (hint from demyn plantenberg)
3154 status = NT_STATUS_INVALID_HANDLE;
3155 goto fail;
3158 if (ISDOT(dir_fsp->fsp_name)) {
3160 * We're at the toplevel dir, the final file name
3161 * must not contain ./, as this is filtered out
3162 * normally by srvstr_get_path and unix_convert
3163 * explicitly rejects paths containing ./.
3165 parent_fname = talloc_strdup(talloc_tos(), "");
3166 if (parent_fname == NULL) {
3167 status = NT_STATUS_NO_MEMORY;
3168 goto fail;
3170 } else {
3171 size_t dir_name_len = strlen(dir_fsp->fsp_name);
3174 * Copy in the base directory name.
3177 parent_fname = TALLOC_ARRAY(talloc_tos(), char,
3178 dir_name_len+2);
3179 if (parent_fname == NULL) {
3180 status = NT_STATUS_NO_MEMORY;
3181 goto fail;
3183 memcpy(parent_fname, dir_fsp->fsp_name,
3184 dir_name_len+1);
3187 * Ensure it ends in a '/'.
3188 * We used TALLOC_SIZE +2 to add space for the '/'.
3191 if(dir_name_len
3192 && (parent_fname[dir_name_len-1] != '\\')
3193 && (parent_fname[dir_name_len-1] != '/')) {
3194 parent_fname[dir_name_len] = '/';
3195 parent_fname[dir_name_len+1] = '\0';
3199 fname = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
3200 fname);
3201 if (fname == NULL) {
3202 status = NT_STATUS_NO_MEMORY;
3203 goto fail;
3208 * Check to see if this is a mac fork of some kind.
3211 if (is_ntfs_stream_name(fname)) {
3212 enum FAKE_FILE_TYPE fake_file_type;
3214 fake_file_type = is_fake_file(fname);
3216 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
3219 * Here we go! support for changing the disk quotas
3220 * --metze
3222 * We need to fake up to open this MAGIC QUOTA file
3223 * and return a valid FID.
3225 * w2k close this file directly after openening xp
3226 * also tries a QUERY_FILE_INFO on the file and then
3227 * close it
3229 status = open_fake_file(conn, req->vuid,
3230 fake_file_type, fname,
3231 access_mask, &fsp);
3232 if (!NT_STATUS_IS_OK(status)) {
3233 goto fail;
3236 ZERO_STRUCT(sbuf);
3237 goto done;
3240 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
3241 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3242 goto fail;
3246 if ((req != NULL) && (req->flags2 & FLAGS2_DFS_PATHNAMES)) {
3247 char *resolved_fname;
3249 status = resolve_dfspath(talloc_tos(), conn, true, fname,
3250 &resolved_fname);
3252 if (!NT_STATUS_IS_OK(status)) {
3254 * For PATH_NOT_COVERED we had
3255 * reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
3256 * ERRSRV, ERRbadpath);
3257 * Need to fix in callers
3259 goto fail;
3261 fname = resolved_fname;
3265 * Check if POSIX semantics are wanted.
3268 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
3269 case_state = set_posix_case_semantics(talloc_tos(), conn);
3270 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
3274 char *converted_fname;
3276 SET_STAT_INVALID(sbuf);
3278 status = unix_convert(talloc_tos(), conn, fname, False,
3279 &converted_fname, NULL, &sbuf);
3280 if (!NT_STATUS_IS_OK(status)) {
3281 goto fail;
3283 fname = converted_fname;
3286 TALLOC_FREE(case_state);
3288 /* All file access must go through check_name() */
3290 status = check_name(conn, fname);
3291 if (!NT_STATUS_IS_OK(status)) {
3292 goto fail;
3295 status = create_file_unixpath(
3296 conn, req, fname, access_mask, share_access,
3297 create_disposition, create_options, file_attributes,
3298 oplock_request, allocation_size, sd, ea_list,
3299 &fsp, &info, &sbuf);
3301 if (!NT_STATUS_IS_OK(status)) {
3302 goto fail;
3305 done:
3306 DEBUG(10, ("create_file: info=%d\n", info));
3308 *result = fsp;
3309 if (pinfo != NULL) {
3310 *pinfo = info;
3312 if (psbuf != NULL) {
3313 *psbuf = sbuf;
3315 return NT_STATUS_OK;
3317 fail:
3318 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
3320 if (fsp != NULL) {
3321 close_file(fsp, ERROR_CLOSE);
3322 fsp = NULL;
3324 return status;