Improve the Gemand pam_winbind translation.
[Samba/nascimento.git] / source3 / smbd / open.c
blob3fd0d1a03a49e4981373001f56de59b10501402f
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) ) {
337 * We can't actually truncate here as the file may be locked.
338 * open_file_ntcreate will take care of the truncate later. JRA.
341 local_flags &= ~O_TRUNC;
343 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
345 * We would block on opening a FIFO with no one else on the
346 * other end. Do what we used to do and add O_NONBLOCK to the
347 * open flags. JRA.
350 if (file_existed && S_ISFIFO(psbuf->st_mode)) {
351 local_flags |= O_NONBLOCK;
353 #endif
355 /* Don't create files with Microsoft wildcard characters. */
356 if ((local_flags & O_CREAT) && !file_existed &&
357 ms_has_wild(path)) {
358 return NT_STATUS_OBJECT_NAME_INVALID;
361 /* Actually do the open */
362 status = fd_open(conn, path, fsp, local_flags, unx_mode);
363 if (!NT_STATUS_IS_OK(status)) {
364 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
365 "(flags=%d)\n",
366 path,nt_errstr(status),local_flags,flags));
367 return status;
370 if ((local_flags & O_CREAT) && !file_existed) {
372 /* Inherit the ACL if required */
373 if (lp_inherit_perms(SNUM(conn))) {
374 inherit_access_posix_acl(conn, parent_dir, path,
375 unx_mode);
378 /* Change the owner if required. */
379 if (lp_inherit_owner(SNUM(conn))) {
380 change_file_owner_to_parent(conn, parent_dir,
381 fsp);
384 notify_fname(conn, NOTIFY_ACTION_ADDED,
385 FILE_NOTIFY_CHANGE_FILE_NAME, path);
388 } else {
389 fsp->fh->fd = -1; /* What we used to call a stat open. */
390 if (file_existed) {
391 status = check_open_rights(conn,
392 path,
393 access_mask);
394 if (!NT_STATUS_IS_OK(status)) {
395 DEBUG(10, ("open_file: Access denied on "
396 "file %s\n",
397 path));
398 return status;
403 if (!file_existed) {
404 int ret;
406 if (fsp->fh->fd == -1) {
407 ret = SMB_VFS_STAT(conn, path, psbuf);
408 } else {
409 ret = SMB_VFS_FSTAT(fsp, psbuf);
410 /* If we have an fd, this stat should succeed. */
411 if (ret == -1) {
412 DEBUG(0,("Error doing fstat on open file %s "
413 "(%s)\n", path,strerror(errno) ));
417 /* For a non-io open, this stat failing means file not found. JRA */
418 if (ret == -1) {
419 status = map_nt_error_from_unix(errno);
420 fd_close(fsp);
421 return status;
426 * POSIX allows read-only opens of directories. We don't
427 * want to do this (we use a different code path for this)
428 * so catch a directory open and return an EISDIR. JRA.
431 if(S_ISDIR(psbuf->st_mode)) {
432 fd_close(fsp);
433 errno = EISDIR;
434 return NT_STATUS_FILE_IS_A_DIRECTORY;
437 fsp->mode = psbuf->st_mode;
438 fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
439 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
440 fsp->file_pid = req ? req->smbpid : 0;
441 fsp->can_lock = True;
442 fsp->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
443 if (!CAN_WRITE(conn)) {
444 fsp->can_write = False;
445 } else {
446 fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ?
447 True : False;
449 fsp->print_file = False;
450 fsp->modified = False;
451 fsp->sent_oplock_break = NO_BREAK_SENT;
452 fsp->is_directory = False;
453 if (conn->aio_write_behind_list &&
454 is_in_path(path, conn->aio_write_behind_list, conn->case_sensitive)) {
455 fsp->aio_write_behind = True;
458 string_set(&fsp->fsp_name, path);
459 fsp->wcp = NULL; /* Write cache pointer. */
461 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
462 conn->server_info->unix_name,
463 fsp->fsp_name,
464 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
465 conn->num_files_open + 1));
467 errno = 0;
468 return NT_STATUS_OK;
471 /*******************************************************************
472 Return True if the filename is one of the special executable types.
473 ********************************************************************/
475 static bool is_executable(const char *fname)
477 if ((fname = strrchr_m(fname,'.'))) {
478 if (strequal(fname,".com") ||
479 strequal(fname,".dll") ||
480 strequal(fname,".exe") ||
481 strequal(fname,".sym")) {
482 return True;
485 return False;
488 /****************************************************************************
489 Check if we can open a file with a share mode.
490 Returns True if conflict, False if not.
491 ****************************************************************************/
493 static bool share_conflict(struct share_mode_entry *entry,
494 uint32 access_mask,
495 uint32 share_access)
497 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
498 "entry->share_access = 0x%x, "
499 "entry->private_options = 0x%x\n",
500 (unsigned int)entry->access_mask,
501 (unsigned int)entry->share_access,
502 (unsigned int)entry->private_options));
504 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
505 (unsigned int)access_mask, (unsigned int)share_access));
507 if ((entry->access_mask & (FILE_WRITE_DATA|
508 FILE_APPEND_DATA|
509 FILE_READ_DATA|
510 FILE_EXECUTE|
511 DELETE_ACCESS)) == 0) {
512 DEBUG(10,("share_conflict: No conflict due to "
513 "entry->access_mask = 0x%x\n",
514 (unsigned int)entry->access_mask ));
515 return False;
518 if ((access_mask & (FILE_WRITE_DATA|
519 FILE_APPEND_DATA|
520 FILE_READ_DATA|
521 FILE_EXECUTE|
522 DELETE_ACCESS)) == 0) {
523 DEBUG(10,("share_conflict: No conflict due to "
524 "access_mask = 0x%x\n",
525 (unsigned int)access_mask ));
526 return False;
529 #if 1 /* JRA TEST - Superdebug. */
530 #define CHECK_MASK(num, am, right, sa, share) \
531 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
532 (unsigned int)(num), (unsigned int)(am), \
533 (unsigned int)(right), (unsigned int)(am)&(right) )); \
534 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
535 (unsigned int)(num), (unsigned int)(sa), \
536 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
537 if (((am) & (right)) && !((sa) & (share))) { \
538 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
539 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
540 (unsigned int)(share) )); \
541 return True; \
543 #else
544 #define CHECK_MASK(num, am, right, sa, share) \
545 if (((am) & (right)) && !((sa) & (share))) { \
546 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
547 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
548 (unsigned int)(share) )); \
549 return True; \
551 #endif
553 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
554 share_access, FILE_SHARE_WRITE);
555 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
556 entry->share_access, FILE_SHARE_WRITE);
558 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
559 share_access, FILE_SHARE_READ);
560 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
561 entry->share_access, FILE_SHARE_READ);
563 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
564 share_access, FILE_SHARE_DELETE);
565 CHECK_MASK(6, access_mask, DELETE_ACCESS,
566 entry->share_access, FILE_SHARE_DELETE);
568 DEBUG(10,("share_conflict: No conflict.\n"));
569 return False;
572 #if defined(DEVELOPER)
573 static void validate_my_share_entries(int num,
574 struct share_mode_entry *share_entry)
576 files_struct *fsp;
578 if (!procid_is_me(&share_entry->pid)) {
579 return;
582 if (is_deferred_open_entry(share_entry) &&
583 !open_was_deferred(share_entry->op_mid)) {
584 char *str = talloc_asprintf(talloc_tos(),
585 "Got a deferred entry without a request: "
586 "PANIC: %s\n",
587 share_mode_str(talloc_tos(), num, share_entry));
588 smb_panic(str);
591 if (!is_valid_share_mode_entry(share_entry)) {
592 return;
595 fsp = file_find_dif(share_entry->id,
596 share_entry->share_file_id);
597 if (!fsp) {
598 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
599 share_mode_str(talloc_tos(), num, share_entry) ));
600 smb_panic("validate_my_share_entries: Cannot match a "
601 "share entry with an open file\n");
604 if (is_deferred_open_entry(share_entry) ||
605 is_unused_share_mode_entry(share_entry)) {
606 goto panic;
609 if ((share_entry->op_type == NO_OPLOCK) &&
610 (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK)) {
611 /* Someone has already written to it, but I haven't yet
612 * noticed */
613 return;
616 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
617 goto panic;
620 return;
622 panic:
624 char *str;
625 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
626 share_mode_str(talloc_tos(), num, share_entry) ));
627 str = talloc_asprintf(talloc_tos(),
628 "validate_my_share_entries: "
629 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
630 fsp->fsp_name, (unsigned int)fsp->oplock_type,
631 (unsigned int)share_entry->op_type );
632 smb_panic(str);
635 #endif
637 static bool is_stat_open(uint32 access_mask)
639 return (access_mask &&
640 ((access_mask & ~(SYNCHRONIZE_ACCESS| FILE_READ_ATTRIBUTES|
641 FILE_WRITE_ATTRIBUTES))==0) &&
642 ((access_mask & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|
643 FILE_WRITE_ATTRIBUTES)) != 0));
646 /****************************************************************************
647 Deal with share modes
648 Invarient: Share mode must be locked on entry and exit.
649 Returns -1 on error, or number of share modes on success (may be zero).
650 ****************************************************************************/
652 static NTSTATUS open_mode_check(connection_struct *conn,
653 const char *fname,
654 struct share_mode_lock *lck,
655 uint32 access_mask,
656 uint32 share_access,
657 uint32 create_options,
658 bool *file_existed)
660 int i;
662 if(lck->num_share_modes == 0) {
663 return NT_STATUS_OK;
666 *file_existed = True;
668 /* A delete on close prohibits everything */
670 if (lck->delete_on_close) {
671 return NT_STATUS_DELETE_PENDING;
674 if (is_stat_open(access_mask)) {
675 /* Stat open that doesn't trigger oplock breaks or share mode
676 * checks... ! JRA. */
677 return NT_STATUS_OK;
681 * Check if the share modes will give us access.
684 #if defined(DEVELOPER)
685 for(i = 0; i < lck->num_share_modes; i++) {
686 validate_my_share_entries(i, &lck->share_modes[i]);
688 #endif
690 if (!lp_share_modes(SNUM(conn))) {
691 return NT_STATUS_OK;
694 /* Now we check the share modes, after any oplock breaks. */
695 for(i = 0; i < lck->num_share_modes; i++) {
697 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
698 continue;
701 /* someone else has a share lock on it, check to see if we can
702 * too */
703 if (share_conflict(&lck->share_modes[i],
704 access_mask, share_access)) {
705 return NT_STATUS_SHARING_VIOLATION;
709 return NT_STATUS_OK;
712 static bool is_delete_request(files_struct *fsp) {
713 return ((fsp->access_mask == DELETE_ACCESS) &&
714 (fsp->oplock_type == NO_OPLOCK));
718 * 1) No files open at all or internal open: Grant whatever the client wants.
720 * 2) Exclusive (or batch) oplock around: If the requested access is a delete
721 * request, break if the oplock around is a batch oplock. If it's another
722 * requested access type, break.
724 * 3) Only level2 around: Grant level2 and do nothing else.
727 static bool delay_for_oplocks(struct share_mode_lock *lck,
728 files_struct *fsp,
729 uint16 mid,
730 int pass_number,
731 int oplock_request)
733 int i;
734 struct share_mode_entry *exclusive = NULL;
735 bool valid_entry = False;
736 bool delay_it = False;
737 bool have_level2 = False;
738 NTSTATUS status;
739 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
741 if (oplock_request & INTERNAL_OPEN_ONLY) {
742 fsp->oplock_type = NO_OPLOCK;
745 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
746 return False;
749 for (i=0; i<lck->num_share_modes; i++) {
751 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
752 continue;
755 /* At least one entry is not an invalid or deferred entry. */
756 valid_entry = True;
758 if (pass_number == 1) {
759 if (BATCH_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
760 SMB_ASSERT(exclusive == NULL);
761 exclusive = &lck->share_modes[i];
763 } else {
764 if (EXCLUSIVE_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
765 SMB_ASSERT(exclusive == NULL);
766 exclusive = &lck->share_modes[i];
770 if (lck->share_modes[i].op_type == LEVEL_II_OPLOCK) {
771 SMB_ASSERT(exclusive == NULL);
772 have_level2 = True;
776 if (!valid_entry) {
777 /* All entries are placeholders or deferred.
778 * Directly grant whatever the client wants. */
779 if (fsp->oplock_type == NO_OPLOCK) {
780 /* Store a level2 oplock, but don't tell the client */
781 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
783 return False;
786 if (exclusive != NULL) { /* Found an exclusive oplock */
787 SMB_ASSERT(!have_level2);
788 delay_it = is_delete_request(fsp) ?
789 BATCH_OPLOCK_TYPE(exclusive->op_type) : True;
792 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
793 /* We can at most grant level2 as there are other
794 * level2 or NO_OPLOCK entries. */
795 fsp->oplock_type = LEVEL_II_OPLOCK;
798 if ((fsp->oplock_type == NO_OPLOCK) && have_level2) {
799 /* Store a level2 oplock, but don't tell the client */
800 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
803 if (!delay_it) {
804 return False;
808 * Send a break message to the oplock holder and delay the open for
809 * our client.
812 DEBUG(10, ("Sending break request to PID %s\n",
813 procid_str_static(&exclusive->pid)));
814 exclusive->op_mid = mid;
816 /* Create the message. */
817 share_mode_entry_to_message(msg, exclusive);
819 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
820 don't want this set in the share mode struct pointed to by lck. */
822 if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
823 SSVAL(msg,6,exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
826 status = messaging_send_buf(smbd_messaging_context(), exclusive->pid,
827 MSG_SMB_BREAK_REQUEST,
828 (uint8 *)msg,
829 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
830 if (!NT_STATUS_IS_OK(status)) {
831 DEBUG(3, ("Could not send oplock break message: %s\n",
832 nt_errstr(status)));
835 return True;
838 static bool request_timed_out(struct timeval request_time,
839 struct timeval timeout)
841 struct timeval now, end_time;
842 GetTimeOfDay(&now);
843 end_time = timeval_sum(&request_time, &timeout);
844 return (timeval_compare(&end_time, &now) < 0);
847 /****************************************************************************
848 Handle the 1 second delay in returning a SHARING_VIOLATION error.
849 ****************************************************************************/
851 static void defer_open(struct share_mode_lock *lck,
852 struct timeval request_time,
853 struct timeval timeout,
854 struct smb_request *req,
855 struct deferred_open_record *state)
857 int i;
859 /* Paranoia check */
861 for (i=0; i<lck->num_share_modes; i++) {
862 struct share_mode_entry *e = &lck->share_modes[i];
864 if (!is_deferred_open_entry(e)) {
865 continue;
868 if (procid_is_me(&e->pid) && (e->op_mid == req->mid)) {
869 DEBUG(0, ("Trying to defer an already deferred "
870 "request: mid=%d, exiting\n", req->mid));
871 exit_server("attempt to defer a deferred request");
875 /* End paranoia check */
877 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
878 "open entry for mid %u\n",
879 (unsigned int)request_time.tv_sec,
880 (unsigned int)request_time.tv_usec,
881 (unsigned int)req->mid));
883 if (!push_deferred_smb_message(req, request_time, timeout,
884 (char *)state, sizeof(*state))) {
885 exit_server("push_deferred_smb_message failed");
887 add_deferred_open(lck, req->mid, request_time, state->id);
890 * Push the MID of this packet on the signing queue.
891 * We only do this once, the first time we push the packet
892 * onto the deferred open queue, as this has a side effect
893 * of incrementing the response sequence number.
896 srv_defer_sign_response(req->mid);
900 /****************************************************************************
901 On overwrite open ensure that the attributes match.
902 ****************************************************************************/
904 static bool open_match_attributes(connection_struct *conn,
905 const char *path,
906 uint32 old_dos_attr,
907 uint32 new_dos_attr,
908 mode_t existing_unx_mode,
909 mode_t new_unx_mode,
910 mode_t *returned_unx_mode)
912 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
914 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
915 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
917 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
918 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
919 *returned_unx_mode = new_unx_mode;
920 } else {
921 *returned_unx_mode = (mode_t)0;
924 DEBUG(10,("open_match_attributes: file %s old_dos_attr = 0x%x, "
925 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
926 "returned_unx_mode = 0%o\n",
927 path,
928 (unsigned int)old_dos_attr,
929 (unsigned int)existing_unx_mode,
930 (unsigned int)new_dos_attr,
931 (unsigned int)*returned_unx_mode ));
933 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
934 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
935 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
936 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
937 return False;
940 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
941 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
942 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
943 return False;
946 return True;
949 /****************************************************************************
950 Special FCB or DOS processing in the case of a sharing violation.
951 Try and find a duplicated file handle.
952 ****************************************************************************/
954 static files_struct *fcb_or_dos_open(struct smb_request *req,
955 connection_struct *conn,
956 const char *fname,
957 struct file_id id,
958 uint16 file_pid,
959 uint16 vuid,
960 uint32 access_mask,
961 uint32 share_access,
962 uint32 create_options)
964 files_struct *fsp;
965 files_struct *dup_fsp;
967 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
968 "file %s.\n", fname ));
970 for(fsp = file_find_di_first(id); fsp;
971 fsp = file_find_di_next(fsp)) {
973 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
974 "vuid = %u, file_pid = %u, private_options = 0x%x "
975 "access_mask = 0x%x\n", fsp->fsp_name,
976 fsp->fh->fd, (unsigned int)fsp->vuid,
977 (unsigned int)fsp->file_pid,
978 (unsigned int)fsp->fh->private_options,
979 (unsigned int)fsp->access_mask ));
981 if (fsp->fh->fd != -1 &&
982 fsp->vuid == vuid &&
983 fsp->file_pid == file_pid &&
984 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
985 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
986 (fsp->access_mask & FILE_WRITE_DATA) &&
987 strequal(fsp->fsp_name, fname)) {
988 DEBUG(10,("fcb_or_dos_open: file match\n"));
989 break;
993 if (!fsp) {
994 return NULL;
997 /* quite an insane set of semantics ... */
998 if (is_executable(fname) &&
999 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1000 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1001 return NULL;
1004 /* We need to duplicate this fsp. */
1005 if (!NT_STATUS_IS_OK(dup_file_fsp(req, fsp, access_mask, share_access,
1006 create_options, &dup_fsp))) {
1007 return NULL;
1010 return dup_fsp;
1013 /****************************************************************************
1014 Open a file with a share mode - old openX method - map into NTCreate.
1015 ****************************************************************************/
1017 bool map_open_params_to_ntcreate(const char *fname, int deny_mode, int open_func,
1018 uint32 *paccess_mask,
1019 uint32 *pshare_mode,
1020 uint32 *pcreate_disposition,
1021 uint32 *pcreate_options)
1023 uint32 access_mask;
1024 uint32 share_mode;
1025 uint32 create_disposition;
1026 uint32 create_options = 0;
1028 DEBUG(10,("map_open_params_to_ntcreate: fname = %s, deny_mode = 0x%x, "
1029 "open_func = 0x%x\n",
1030 fname, (unsigned int)deny_mode, (unsigned int)open_func ));
1032 /* Create the NT compatible access_mask. */
1033 switch (GET_OPENX_MODE(deny_mode)) {
1034 case DOS_OPEN_EXEC: /* Implies read-only - used to be FILE_READ_DATA */
1035 case DOS_OPEN_RDONLY:
1036 access_mask = FILE_GENERIC_READ;
1037 break;
1038 case DOS_OPEN_WRONLY:
1039 access_mask = FILE_GENERIC_WRITE;
1040 break;
1041 case DOS_OPEN_RDWR:
1042 case DOS_OPEN_FCB:
1043 access_mask = FILE_GENERIC_READ|FILE_GENERIC_WRITE;
1044 break;
1045 default:
1046 DEBUG(10,("map_open_params_to_ntcreate: bad open mode = 0x%x\n",
1047 (unsigned int)GET_OPENX_MODE(deny_mode)));
1048 return False;
1051 /* Create the NT compatible create_disposition. */
1052 switch (open_func) {
1053 case OPENX_FILE_EXISTS_FAIL|OPENX_FILE_CREATE_IF_NOT_EXIST:
1054 create_disposition = FILE_CREATE;
1055 break;
1057 case OPENX_FILE_EXISTS_OPEN:
1058 create_disposition = FILE_OPEN;
1059 break;
1061 case OPENX_FILE_EXISTS_OPEN|OPENX_FILE_CREATE_IF_NOT_EXIST:
1062 create_disposition = FILE_OPEN_IF;
1063 break;
1065 case OPENX_FILE_EXISTS_TRUNCATE:
1066 create_disposition = FILE_OVERWRITE;
1067 break;
1069 case OPENX_FILE_EXISTS_TRUNCATE|OPENX_FILE_CREATE_IF_NOT_EXIST:
1070 create_disposition = FILE_OVERWRITE_IF;
1071 break;
1073 default:
1074 /* From samba4 - to be confirmed. */
1075 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_EXEC) {
1076 create_disposition = FILE_CREATE;
1077 break;
1079 DEBUG(10,("map_open_params_to_ntcreate: bad "
1080 "open_func 0x%x\n", (unsigned int)open_func));
1081 return False;
1084 /* Create the NT compatible share modes. */
1085 switch (GET_DENY_MODE(deny_mode)) {
1086 case DENY_ALL:
1087 share_mode = FILE_SHARE_NONE;
1088 break;
1090 case DENY_WRITE:
1091 share_mode = FILE_SHARE_READ;
1092 break;
1094 case DENY_READ:
1095 share_mode = FILE_SHARE_WRITE;
1096 break;
1098 case DENY_NONE:
1099 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1100 break;
1102 case DENY_DOS:
1103 create_options |= NTCREATEX_OPTIONS_PRIVATE_DENY_DOS;
1104 if (is_executable(fname)) {
1105 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1106 } else {
1107 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_RDONLY) {
1108 share_mode = FILE_SHARE_READ;
1109 } else {
1110 share_mode = FILE_SHARE_NONE;
1113 break;
1115 case DENY_FCB:
1116 create_options |= NTCREATEX_OPTIONS_PRIVATE_DENY_FCB;
1117 share_mode = FILE_SHARE_NONE;
1118 break;
1120 default:
1121 DEBUG(10,("map_open_params_to_ntcreate: bad deny_mode 0x%x\n",
1122 (unsigned int)GET_DENY_MODE(deny_mode) ));
1123 return False;
1126 DEBUG(10,("map_open_params_to_ntcreate: file %s, access_mask = 0x%x, "
1127 "share_mode = 0x%x, create_disposition = 0x%x, "
1128 "create_options = 0x%x\n",
1129 fname,
1130 (unsigned int)access_mask,
1131 (unsigned int)share_mode,
1132 (unsigned int)create_disposition,
1133 (unsigned int)create_options ));
1135 if (paccess_mask) {
1136 *paccess_mask = access_mask;
1138 if (pshare_mode) {
1139 *pshare_mode = share_mode;
1141 if (pcreate_disposition) {
1142 *pcreate_disposition = create_disposition;
1144 if (pcreate_options) {
1145 *pcreate_options = create_options;
1148 return True;
1152 static void schedule_defer_open(struct share_mode_lock *lck,
1153 struct timeval request_time,
1154 struct smb_request *req)
1156 struct deferred_open_record state;
1158 /* This is a relative time, added to the absolute
1159 request_time value to get the absolute timeout time.
1160 Note that if this is the second or greater time we enter
1161 this codepath for this particular request mid then
1162 request_time is left as the absolute time of the *first*
1163 time this request mid was processed. This is what allows
1164 the request to eventually time out. */
1166 struct timeval timeout;
1168 /* Normally the smbd we asked should respond within
1169 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1170 * the client did, give twice the timeout as a safety
1171 * measure here in case the other smbd is stuck
1172 * somewhere else. */
1174 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1176 /* Nothing actually uses state.delayed_for_oplocks
1177 but it's handy to differentiate in debug messages
1178 between a 30 second delay due to oplock break, and
1179 a 1 second delay for share mode conflicts. */
1181 state.delayed_for_oplocks = True;
1182 state.id = lck->id;
1184 if (!request_timed_out(request_time, timeout)) {
1185 defer_open(lck, request_time, timeout, req, &state);
1189 /****************************************************************************
1190 Work out what access_mask to use from what the client sent us.
1191 ****************************************************************************/
1193 static NTSTATUS calculate_access_mask(connection_struct *conn,
1194 const char *fname,
1195 bool file_existed,
1196 uint32_t access_mask,
1197 uint32_t *access_mask_out)
1199 NTSTATUS status;
1202 * Convert GENERIC bits to specific bits.
1205 se_map_generic(&access_mask, &file_generic_mapping);
1207 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1208 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1209 if (file_existed) {
1211 struct security_descriptor *sd;
1212 uint32_t access_granted = 0;
1214 status = SMB_VFS_GET_NT_ACL(conn, fname,
1215 (OWNER_SECURITY_INFORMATION |
1216 GROUP_SECURITY_INFORMATION |
1217 DACL_SECURITY_INFORMATION),&sd);
1219 if (!NT_STATUS_IS_OK(status)) {
1220 DEBUG(10, ("calculate_access_mask: Could not get acl "
1221 "on file %s: %s\n",
1222 fname,
1223 nt_errstr(status)));
1224 return NT_STATUS_ACCESS_DENIED;
1227 status = smb1_file_se_access_check(sd,
1228 conn->server_info->ptok,
1229 access_mask,
1230 &access_granted);
1232 TALLOC_FREE(sd);
1234 if (!NT_STATUS_IS_OK(status)) {
1235 DEBUG(10, ("calculate_access_mask: Access denied on "
1236 "file %s: when calculating maximum access\n",
1237 fname));
1238 return NT_STATUS_ACCESS_DENIED;
1241 access_mask = access_granted;
1242 } else {
1243 access_mask = FILE_GENERIC_ALL;
1247 *access_mask_out = access_mask;
1248 return NT_STATUS_OK;
1251 /****************************************************************************
1252 Open a file with a share mode.
1253 ****************************************************************************/
1255 NTSTATUS open_file_ntcreate(connection_struct *conn,
1256 struct smb_request *req,
1257 const char *fname,
1258 SMB_STRUCT_STAT *psbuf,
1259 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1260 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1261 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1262 uint32 create_options, /* options such as delete on close. */
1263 uint32 new_dos_attributes, /* attributes used for new file. */
1264 int oplock_request, /* internal Samba oplock codes. */
1265 /* Information (FILE_EXISTS etc.) */
1266 int *pinfo,
1267 files_struct **result)
1269 int flags=0;
1270 int flags2=0;
1271 bool file_existed = VALID_STAT(*psbuf);
1272 bool def_acl = False;
1273 bool posix_open = False;
1274 bool new_file_created = False;
1275 struct file_id id;
1276 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1277 files_struct *fsp = NULL;
1278 mode_t new_unx_mode = (mode_t)0;
1279 mode_t unx_mode = (mode_t)0;
1280 int info;
1281 uint32 existing_dos_attributes = 0;
1282 struct pending_message_list *pml = NULL;
1283 struct timeval request_time = timeval_zero();
1284 struct share_mode_lock *lck = NULL;
1285 uint32 open_access_mask = access_mask;
1286 NTSTATUS status;
1287 int ret_flock;
1288 char *parent_dir;
1289 const char *newname;
1291 ZERO_STRUCT(id);
1293 if (conn->printer) {
1295 * Printers are handled completely differently.
1296 * Most of the passed parameters are ignored.
1299 if (pinfo) {
1300 *pinfo = FILE_WAS_CREATED;
1303 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n", fname));
1305 return print_fsp_open(req, conn, fname, req->vuid, result);
1308 if (!parent_dirname_talloc(talloc_tos(), fname, &parent_dir,
1309 &newname)) {
1310 return NT_STATUS_NO_MEMORY;
1313 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1314 posix_open = True;
1315 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1316 new_dos_attributes = 0;
1317 } else {
1318 /* We add aARCH to this as this mode is only used if the file is
1319 * created new. */
1320 unx_mode = unix_mode(conn, new_dos_attributes | aARCH, fname,
1321 parent_dir);
1324 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1325 "access_mask=0x%x share_access=0x%x "
1326 "create_disposition = 0x%x create_options=0x%x "
1327 "unix mode=0%o oplock_request=%d\n",
1328 fname, new_dos_attributes, access_mask, share_access,
1329 create_disposition, create_options, unx_mode,
1330 oplock_request));
1332 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
1333 DEBUG(0, ("No smb request but not an internal only open!\n"));
1334 return NT_STATUS_INTERNAL_ERROR;
1338 * Only non-internal opens can be deferred at all
1341 if ((req != NULL)
1342 && ((pml = get_open_deferred_message(req->mid)) != NULL)) {
1343 struct deferred_open_record *state =
1344 (struct deferred_open_record *)pml->private_data.data;
1346 /* Remember the absolute time of the original
1347 request with this mid. We'll use it later to
1348 see if this has timed out. */
1350 request_time = pml->request_time;
1352 /* Remove the deferred open entry under lock. */
1353 lck = get_share_mode_lock(talloc_tos(), state->id, NULL, NULL,
1354 NULL);
1355 if (lck == NULL) {
1356 DEBUG(0, ("could not get share mode lock\n"));
1357 } else {
1358 del_deferred_open_entry(lck, req->mid);
1359 TALLOC_FREE(lck);
1362 /* Ensure we don't reprocess this message. */
1363 remove_deferred_open_smb_message(req->mid);
1366 status = check_name(conn, fname);
1367 if (!NT_STATUS_IS_OK(status)) {
1368 return status;
1371 if (!posix_open) {
1372 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
1373 if (file_existed) {
1374 existing_dos_attributes = dos_mode(conn, fname, psbuf);
1378 /* ignore any oplock requests if oplocks are disabled */
1379 if (!lp_oplocks(SNUM(conn)) || global_client_failed_oplock_break ||
1380 IS_VETO_OPLOCK_PATH(conn, fname)) {
1381 /* Mask off everything except the private Samba bits. */
1382 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
1385 /* this is for OS/2 long file names - say we don't support them */
1386 if (!lp_posix_pathnames() && strstr(fname,".+,;=[].")) {
1387 /* OS/2 Workplace shell fix may be main code stream in a later
1388 * release. */
1389 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1390 "supported.\n"));
1391 if (use_nt_status()) {
1392 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1394 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
1397 switch( create_disposition ) {
1399 * Currently we're using FILE_SUPERSEDE as the same as
1400 * FILE_OVERWRITE_IF but they really are
1401 * different. FILE_SUPERSEDE deletes an existing file
1402 * (requiring delete access) then recreates it.
1404 case FILE_SUPERSEDE:
1405 /* If file exists replace/overwrite. If file doesn't
1406 * exist create. */
1407 flags2 |= (O_CREAT | O_TRUNC);
1408 break;
1410 case FILE_OVERWRITE_IF:
1411 /* If file exists replace/overwrite. If file doesn't
1412 * exist create. */
1413 flags2 |= (O_CREAT | O_TRUNC);
1414 break;
1416 case FILE_OPEN:
1417 /* If file exists open. If file doesn't exist error. */
1418 if (!file_existed) {
1419 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1420 "requested for file %s and file "
1421 "doesn't exist.\n", fname ));
1422 errno = ENOENT;
1423 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1425 break;
1427 case FILE_OVERWRITE:
1428 /* If file exists overwrite. If file doesn't exist
1429 * error. */
1430 if (!file_existed) {
1431 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1432 "requested for file %s and file "
1433 "doesn't exist.\n", fname ));
1434 errno = ENOENT;
1435 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1437 flags2 |= O_TRUNC;
1438 break;
1440 case FILE_CREATE:
1441 /* If file exists error. If file doesn't exist
1442 * create. */
1443 if (file_existed) {
1444 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1445 "requested for file %s and file "
1446 "already exists.\n", fname ));
1447 if (S_ISDIR(psbuf->st_mode)) {
1448 errno = EISDIR;
1449 } else {
1450 errno = EEXIST;
1452 return map_nt_error_from_unix(errno);
1454 flags2 |= (O_CREAT|O_EXCL);
1455 break;
1457 case FILE_OPEN_IF:
1458 /* If file exists open. If file doesn't exist
1459 * create. */
1460 flags2 |= O_CREAT;
1461 break;
1463 default:
1464 return NT_STATUS_INVALID_PARAMETER;
1467 /* We only care about matching attributes on file exists and
1468 * overwrite. */
1470 if (!posix_open && file_existed && ((create_disposition == FILE_OVERWRITE) ||
1471 (create_disposition == FILE_OVERWRITE_IF))) {
1472 if (!open_match_attributes(conn, fname,
1473 existing_dos_attributes,
1474 new_dos_attributes, psbuf->st_mode,
1475 unx_mode, &new_unx_mode)) {
1476 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1477 "for file %s (%x %x) (0%o, 0%o)\n",
1478 fname, existing_dos_attributes,
1479 new_dos_attributes,
1480 (unsigned int)psbuf->st_mode,
1481 (unsigned int)unx_mode ));
1482 errno = EACCES;
1483 return NT_STATUS_ACCESS_DENIED;
1487 status = calculate_access_mask(conn, fname, file_existed,
1488 access_mask,
1489 &access_mask);
1490 if (!NT_STATUS_IS_OK(status)) {
1491 DEBUG(10, ("open_file_ntcreate: calculate_access_mask "
1492 "on file %s returned %s\n",
1493 fname,
1494 nt_errstr(status)));
1495 return status;
1498 open_access_mask = access_mask;
1500 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1501 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
1504 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1505 "access_mask=0x%x\n", fname, access_mask ));
1508 * Note that we ignore the append flag as append does not
1509 * mean the same thing under DOS and Unix.
1512 if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
1513 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1514 /* DENY_DOS opens are always underlying read-write on the
1515 file handle, no matter what the requested access mask
1516 says. */
1517 if ((create_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
1518 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|FILE_READ_EA|FILE_EXECUTE)) {
1519 flags = O_RDWR;
1520 } else {
1521 flags = O_WRONLY;
1523 } else {
1524 flags = O_RDONLY;
1528 * Currently we only look at FILE_WRITE_THROUGH for create options.
1531 #if defined(O_SYNC)
1532 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
1533 flags2 |= O_SYNC;
1535 #endif /* O_SYNC */
1537 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
1538 flags2 |= O_APPEND;
1541 if (!posix_open && !CAN_WRITE(conn)) {
1543 * We should really return a permission denied error if either
1544 * O_CREAT or O_TRUNC are set, but for compatibility with
1545 * older versions of Samba we just AND them out.
1547 flags2 &= ~(O_CREAT|O_TRUNC);
1551 * Ensure we can't write on a read-only share or file.
1554 if (flags != O_RDONLY && file_existed &&
1555 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
1556 DEBUG(5,("open_file_ntcreate: write access requested for "
1557 "file %s on read only %s\n",
1558 fname, !CAN_WRITE(conn) ? "share" : "file" ));
1559 errno = EACCES;
1560 return NT_STATUS_ACCESS_DENIED;
1563 status = file_new(req, conn, &fsp);
1564 if(!NT_STATUS_IS_OK(status)) {
1565 return status;
1568 fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
1569 fsp->share_access = share_access;
1570 fsp->fh->private_options = create_options;
1571 fsp->access_mask = open_access_mask; /* We change this to the
1572 * requested access_mask after
1573 * the open is done. */
1574 fsp->posix_open = posix_open;
1576 /* Ensure no SAMBA_PRIVATE bits can be set. */
1577 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1579 if (timeval_is_zero(&request_time)) {
1580 request_time = fsp->open_time;
1583 if (file_existed) {
1584 struct timespec old_write_time = get_mtimespec(psbuf);
1585 id = vfs_file_id_from_sbuf(conn, psbuf);
1587 lck = get_share_mode_lock(talloc_tos(), id,
1588 conn->connectpath,
1589 fname, &old_write_time);
1591 if (lck == NULL) {
1592 file_free(req, fsp);
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 file_free(req, fsp);
1604 return NT_STATUS_SHARING_VIOLATION;
1607 /* Use the client requested access mask here, not the one we
1608 * open with. */
1609 status = open_mode_check(conn, fname, lck,
1610 access_mask, share_access,
1611 create_options, &file_existed);
1613 if (NT_STATUS_IS_OK(status)) {
1614 /* We might be going to allow this open. Check oplock
1615 * status again. */
1616 /* Second pass - send break for both batch or
1617 * exclusive oplocks. */
1618 if ((req != NULL)
1619 && delay_for_oplocks(lck, fsp, req->mid, 2,
1620 oplock_request)) {
1621 schedule_defer_open(lck, request_time, req);
1622 TALLOC_FREE(lck);
1623 file_free(req, fsp);
1624 return NT_STATUS_SHARING_VIOLATION;
1628 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
1629 /* DELETE_PENDING is not deferred for a second */
1630 TALLOC_FREE(lck);
1631 file_free(req, fsp);
1632 return status;
1635 if (!NT_STATUS_IS_OK(status)) {
1636 uint32 can_access_mask;
1637 bool can_access = True;
1639 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
1641 /* Check if this can be done with the deny_dos and fcb
1642 * calls. */
1643 if (create_options &
1644 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
1645 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
1646 files_struct *fsp_dup;
1648 if (req == NULL) {
1649 DEBUG(0, ("DOS open without an SMB "
1650 "request!\n"));
1651 TALLOC_FREE(lck);
1652 file_free(req, fsp);
1653 return NT_STATUS_INTERNAL_ERROR;
1656 /* Use the client requested access mask here,
1657 * not the one we open with. */
1658 fsp_dup = fcb_or_dos_open(req, conn, fname, id,
1659 req->smbpid,
1660 req->vuid,
1661 access_mask,
1662 share_access,
1663 create_options);
1665 if (fsp_dup) {
1666 TALLOC_FREE(lck);
1667 file_free(req, fsp);
1668 if (pinfo) {
1669 *pinfo = FILE_WAS_OPENED;
1671 conn->num_files_open++;
1672 *result = fsp_dup;
1673 return NT_STATUS_OK;
1678 * This next line is a subtlety we need for
1679 * MS-Access. If a file open will fail due to share
1680 * permissions and also for security (access) reasons,
1681 * we need to return the access failed error, not the
1682 * share error. We can't open the file due to kernel
1683 * oplock deadlock (it's possible we failed above on
1684 * the open_mode_check()) so use a userspace check.
1687 if (flags & O_RDWR) {
1688 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
1689 } else if (flags & O_WRONLY) {
1690 can_access_mask = FILE_WRITE_DATA;
1691 } else {
1692 can_access_mask = FILE_READ_DATA;
1695 if (((can_access_mask & FILE_WRITE_DATA) && !CAN_WRITE(conn)) ||
1696 !can_access_file_data(conn,fname,psbuf,can_access_mask)) {
1697 can_access = False;
1701 * If we're returning a share violation, ensure we
1702 * cope with the braindead 1 second delay.
1705 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
1706 lp_defer_sharing_violations()) {
1707 struct timeval timeout;
1708 struct deferred_open_record state;
1709 int timeout_usecs;
1711 /* this is a hack to speed up torture tests
1712 in 'make test' */
1713 timeout_usecs = lp_parm_int(SNUM(conn),
1714 "smbd","sharedelay",
1715 SHARING_VIOLATION_USEC_WAIT);
1717 /* This is a relative time, added to the absolute
1718 request_time value to get the absolute timeout time.
1719 Note that if this is the second or greater time we enter
1720 this codepath for this particular request mid then
1721 request_time is left as the absolute time of the *first*
1722 time this request mid was processed. This is what allows
1723 the request to eventually time out. */
1725 timeout = timeval_set(0, timeout_usecs);
1727 /* Nothing actually uses state.delayed_for_oplocks
1728 but it's handy to differentiate in debug messages
1729 between a 30 second delay due to oplock break, and
1730 a 1 second delay for share mode conflicts. */
1732 state.delayed_for_oplocks = False;
1733 state.id = id;
1735 if ((req != NULL)
1736 && !request_timed_out(request_time,
1737 timeout)) {
1738 defer_open(lck, request_time, timeout,
1739 req, &state);
1743 TALLOC_FREE(lck);
1744 if (can_access) {
1746 * We have detected a sharing violation here
1747 * so return the correct error code
1749 status = NT_STATUS_SHARING_VIOLATION;
1750 } else {
1751 status = NT_STATUS_ACCESS_DENIED;
1753 file_free(req, fsp);
1754 return status;
1758 * We exit this block with the share entry *locked*.....
1762 SMB_ASSERT(!file_existed || (lck != NULL));
1765 * Ensure we pay attention to default ACLs on directories if required.
1768 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
1769 (def_acl = directory_has_default_acl(conn, parent_dir))) {
1770 unx_mode = 0777;
1773 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
1774 "access_mask = 0x%x, open_access_mask = 0x%x\n",
1775 (unsigned int)flags, (unsigned int)flags2,
1776 (unsigned int)unx_mode, (unsigned int)access_mask,
1777 (unsigned int)open_access_mask));
1780 * open_file strips any O_TRUNC flags itself.
1783 fsp_open = open_file(fsp, conn, req, parent_dir, newname, fname, psbuf,
1784 flags|flags2, unx_mode, access_mask,
1785 open_access_mask);
1787 if (!NT_STATUS_IS_OK(fsp_open)) {
1788 if (lck != NULL) {
1789 TALLOC_FREE(lck);
1791 file_free(req, fsp);
1792 return fsp_open;
1795 if (!file_existed) {
1796 struct timespec old_write_time = get_mtimespec(psbuf);
1798 * Deal with the race condition where two smbd's detect the
1799 * file doesn't exist and do the create at the same time. One
1800 * of them will win and set a share mode, the other (ie. this
1801 * one) should check if the requested share mode for this
1802 * create is allowed.
1806 * Now the file exists and fsp is successfully opened,
1807 * fsp->dev and fsp->inode are valid and should replace the
1808 * dev=0,inode=0 from a non existent file. Spotted by
1809 * Nadav Danieli <nadavd@exanet.com>. JRA.
1812 id = fsp->file_id;
1814 lck = get_share_mode_lock(talloc_tos(), id,
1815 conn->connectpath,
1816 fname, &old_write_time);
1818 if (lck == NULL) {
1819 DEBUG(0, ("open_file_ntcreate: Could not get share "
1820 "mode lock for %s\n", fname));
1821 fd_close(fsp);
1822 file_free(req, fsp);
1823 return NT_STATUS_SHARING_VIOLATION;
1826 /* First pass - send break only on batch oplocks. */
1827 if ((req != NULL)
1828 && delay_for_oplocks(lck, fsp, req->mid, 1,
1829 oplock_request)) {
1830 schedule_defer_open(lck, request_time, req);
1831 TALLOC_FREE(lck);
1832 fd_close(fsp);
1833 file_free(req, fsp);
1834 return NT_STATUS_SHARING_VIOLATION;
1837 status = open_mode_check(conn, fname, lck,
1838 access_mask, share_access,
1839 create_options, &file_existed);
1841 if (NT_STATUS_IS_OK(status)) {
1842 /* We might be going to allow this open. Check oplock
1843 * status again. */
1844 /* Second pass - send break for both batch or
1845 * exclusive oplocks. */
1846 if ((req != NULL)
1847 && delay_for_oplocks(lck, fsp, req->mid, 2,
1848 oplock_request)) {
1849 schedule_defer_open(lck, request_time, req);
1850 TALLOC_FREE(lck);
1851 fd_close(fsp);
1852 file_free(req, fsp);
1853 return NT_STATUS_SHARING_VIOLATION;
1857 if (!NT_STATUS_IS_OK(status)) {
1858 struct deferred_open_record state;
1860 fd_close(fsp);
1861 file_free(req, fsp);
1863 state.delayed_for_oplocks = False;
1864 state.id = id;
1866 /* Do it all over again immediately. In the second
1867 * round we will find that the file existed and handle
1868 * the DELETE_PENDING and FCB cases correctly. No need
1869 * to duplicate the code here. Essentially this is a
1870 * "goto top of this function", but don't tell
1871 * anybody... */
1873 if (req != NULL) {
1874 defer_open(lck, request_time, timeval_zero(),
1875 req, &state);
1877 TALLOC_FREE(lck);
1878 return status;
1882 * We exit this block with the share entry *locked*.....
1887 SMB_ASSERT(lck != NULL);
1889 /* note that we ignore failure for the following. It is
1890 basically a hack for NFS, and NFS will never set one of
1891 these only read them. Nobody but Samba can ever set a deny
1892 mode and we have already checked our more authoritative
1893 locking database for permission to set this deny mode. If
1894 the kernel refuses the operations then the kernel is wrong.
1895 note that GPFS supports it as well - jmcd */
1897 if (fsp->fh->fd != -1) {
1898 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access);
1899 if(ret_flock == -1 ){
1901 TALLOC_FREE(lck);
1902 fd_close(fsp);
1903 file_free(req, fsp);
1905 return NT_STATUS_SHARING_VIOLATION;
1910 * At this point onwards, we can guarentee that the share entry
1911 * is locked, whether we created the file or not, and that the
1912 * deny mode is compatible with all current opens.
1916 * If requested, truncate the file.
1919 if (flags2&O_TRUNC) {
1921 * We are modifing the file after open - update the stat
1922 * struct..
1924 if ((SMB_VFS_FTRUNCATE(fsp, 0) == -1) ||
1925 (SMB_VFS_FSTAT(fsp, psbuf)==-1)) {
1926 status = map_nt_error_from_unix(errno);
1927 TALLOC_FREE(lck);
1928 fd_close(fsp);
1929 file_free(req, fsp);
1930 return status;
1934 /* Record the options we were opened with. */
1935 fsp->share_access = share_access;
1936 fsp->fh->private_options = create_options;
1938 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
1940 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
1942 if (file_existed) {
1943 /* stat opens on existing files don't get oplocks. */
1944 if (is_stat_open(open_access_mask)) {
1945 fsp->oplock_type = NO_OPLOCK;
1948 if (!(flags2 & O_TRUNC)) {
1949 info = FILE_WAS_OPENED;
1950 } else {
1951 info = FILE_WAS_OVERWRITTEN;
1953 } else {
1954 info = FILE_WAS_CREATED;
1957 if (pinfo) {
1958 *pinfo = info;
1962 * Setup the oplock info in both the shared memory and
1963 * file structs.
1966 if ((fsp->oplock_type != NO_OPLOCK) &&
1967 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK)) {
1968 if (!set_file_oplock(fsp, fsp->oplock_type)) {
1969 /* Could not get the kernel oplock */
1970 fsp->oplock_type = NO_OPLOCK;
1974 if (info == FILE_WAS_OVERWRITTEN || info == FILE_WAS_CREATED || info == FILE_WAS_SUPERSEDED) {
1975 new_file_created = True;
1978 set_share_mode(lck, fsp, conn->server_info->utok.uid, 0,
1979 fsp->oplock_type, new_file_created);
1981 /* Handle strange delete on close create semantics. */
1982 if ((create_options & FILE_DELETE_ON_CLOSE)
1983 && (((conn->fs_capabilities & FILE_NAMED_STREAMS)
1984 && is_ntfs_stream_name(fname))
1985 || can_set_initial_delete_on_close(lck))) {
1986 status = can_set_delete_on_close(fsp, True, new_dos_attributes);
1988 if (!NT_STATUS_IS_OK(status)) {
1989 /* Remember to delete the mode we just added. */
1990 del_share_mode(lck, fsp);
1991 TALLOC_FREE(lck);
1992 fd_close(fsp);
1993 file_free(req, fsp);
1994 return status;
1996 /* Note that here we set the *inital* delete on close flag,
1997 not the regular one. The magic gets handled in close. */
1998 fsp->initial_delete_on_close = True;
2001 if (new_file_created) {
2002 /* Files should be initially set as archive */
2003 if (lp_map_archive(SNUM(conn)) ||
2004 lp_store_dos_attributes(SNUM(conn))) {
2005 if (!posix_open) {
2006 SMB_STRUCT_STAT tmp_sbuf;
2007 SET_STAT_INVALID(tmp_sbuf);
2008 if (file_set_dosmode(
2009 conn, fname,
2010 new_dos_attributes | aARCH,
2011 &tmp_sbuf, parent_dir,
2012 true) == 0) {
2013 unx_mode = tmp_sbuf.st_mode;
2020 * Take care of inherited ACLs on created files - if default ACL not
2021 * selected.
2024 if (!posix_open && !file_existed && !def_acl) {
2026 int saved_errno = errno; /* We might get ENOSYS in the next
2027 * call.. */
2029 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2030 errno == ENOSYS) {
2031 errno = saved_errno; /* Ignore ENOSYS */
2034 } else if (new_unx_mode) {
2036 int ret = -1;
2038 /* Attributes need changing. File already existed. */
2041 int saved_errno = errno; /* We might get ENOSYS in the
2042 * next call.. */
2043 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2045 if (ret == -1 && errno == ENOSYS) {
2046 errno = saved_errno; /* Ignore ENOSYS */
2047 } else {
2048 DEBUG(5, ("open_file_ntcreate: reset "
2049 "attributes of file %s to 0%o\n",
2050 fname, (unsigned int)new_unx_mode));
2051 ret = 0; /* Don't do the fchmod below. */
2055 if ((ret == -1) &&
2056 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2057 DEBUG(5, ("open_file_ntcreate: failed to reset "
2058 "attributes of file %s to 0%o\n",
2059 fname, (unsigned int)new_unx_mode));
2062 /* If this is a successful open, we must remove any deferred open
2063 * records. */
2064 if (req != NULL) {
2065 del_deferred_open_entry(lck, req->mid);
2067 TALLOC_FREE(lck);
2069 conn->num_files_open++;
2071 *result = fsp;
2072 return NT_STATUS_OK;
2075 /****************************************************************************
2076 Open a file for for write to ensure that we can fchmod it.
2077 ****************************************************************************/
2079 NTSTATUS open_file_fchmod(struct smb_request *req, connection_struct *conn,
2080 const char *fname,
2081 SMB_STRUCT_STAT *psbuf, files_struct **result)
2083 files_struct *fsp = NULL;
2084 NTSTATUS status;
2086 if (!VALID_STAT(*psbuf)) {
2087 return NT_STATUS_INVALID_PARAMETER;
2090 status = file_new(req, conn, &fsp);
2091 if(!NT_STATUS_IS_OK(status)) {
2092 return status;
2095 /* note! we must use a non-zero desired access or we don't get
2096 a real file descriptor. Oh what a twisted web we weave. */
2097 status = open_file(fsp, conn, NULL, NULL, NULL, fname, psbuf, O_WRONLY,
2098 0, FILE_WRITE_DATA, FILE_WRITE_DATA);
2101 * This is not a user visible file open.
2102 * Don't set a share mode and don't increment
2103 * the conn->num_files_open.
2106 if (!NT_STATUS_IS_OK(status)) {
2107 file_free(req, fsp);
2108 return status;
2111 *result = fsp;
2112 return NT_STATUS_OK;
2115 /****************************************************************************
2116 Close the fchmod file fd - ensure no locks are lost.
2117 ****************************************************************************/
2119 NTSTATUS close_file_fchmod(struct smb_request *req, files_struct *fsp)
2121 NTSTATUS status = fd_close(fsp);
2122 file_free(req, fsp);
2123 return status;
2126 static NTSTATUS mkdir_internal(connection_struct *conn,
2127 const char *name,
2128 uint32 file_attributes,
2129 SMB_STRUCT_STAT *psbuf)
2131 mode_t mode;
2132 char *parent_dir;
2133 const char *dirname;
2134 NTSTATUS status;
2135 bool posix_open = false;
2137 if(!CAN_WRITE(conn)) {
2138 DEBUG(5,("mkdir_internal: failing create on read-only share "
2139 "%s\n", lp_servicename(SNUM(conn))));
2140 return NT_STATUS_ACCESS_DENIED;
2143 status = check_name(conn, name);
2144 if (!NT_STATUS_IS_OK(status)) {
2145 return status;
2148 if (!parent_dirname_talloc(talloc_tos(), name, &parent_dir,
2149 &dirname)) {
2150 return NT_STATUS_NO_MEMORY;
2153 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2154 posix_open = true;
2155 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2156 } else {
2157 mode = unix_mode(conn, aDIR, name, parent_dir);
2160 if (SMB_VFS_MKDIR(conn, name, mode) != 0) {
2161 return map_nt_error_from_unix(errno);
2164 /* Ensure we're checking for a symlink here.... */
2165 /* We don't want to get caught by a symlink racer. */
2167 if (SMB_VFS_LSTAT(conn, name, psbuf) == -1) {
2168 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2169 name, strerror(errno)));
2170 return map_nt_error_from_unix(errno);
2173 if (!S_ISDIR(psbuf->st_mode)) {
2174 DEBUG(0, ("Directory just '%s' created is not a directory\n",
2175 name));
2176 return NT_STATUS_ACCESS_DENIED;
2179 if (lp_store_dos_attributes(SNUM(conn))) {
2180 if (!posix_open) {
2181 file_set_dosmode(conn, name,
2182 file_attributes | aDIR, NULL,
2183 parent_dir,
2184 true);
2188 if (lp_inherit_perms(SNUM(conn))) {
2189 inherit_access_posix_acl(conn, parent_dir, name, mode);
2192 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
2194 * Check if high bits should have been set,
2195 * then (if bits are missing): add them.
2196 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2197 * dir.
2199 if (mode & ~(S_IRWXU|S_IRWXG|S_IRWXO) && (mode & ~psbuf->st_mode)) {
2200 SMB_VFS_CHMOD(conn, name,
2201 psbuf->st_mode | (mode & ~psbuf->st_mode));
2205 /* Change the owner if required. */
2206 if (lp_inherit_owner(SNUM(conn))) {
2207 change_dir_owner_to_parent(conn, parent_dir, name, psbuf);
2210 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2211 name);
2213 return NT_STATUS_OK;
2216 /****************************************************************************
2217 Open a directory from an NT SMB call.
2218 ****************************************************************************/
2220 NTSTATUS open_directory(connection_struct *conn,
2221 struct smb_request *req,
2222 const char *fname,
2223 SMB_STRUCT_STAT *psbuf,
2224 uint32 access_mask,
2225 uint32 share_access,
2226 uint32 create_disposition,
2227 uint32 create_options,
2228 uint32 file_attributes,
2229 int *pinfo,
2230 files_struct **result)
2232 files_struct *fsp = NULL;
2233 bool dir_existed = VALID_STAT(*psbuf) ? True : False;
2234 struct share_mode_lock *lck = NULL;
2235 NTSTATUS status;
2236 struct timespec mtimespec;
2237 int info = 0;
2239 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2240 "share_access = 0x%x create_options = 0x%x, "
2241 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2242 fname,
2243 (unsigned int)access_mask,
2244 (unsigned int)share_access,
2245 (unsigned int)create_options,
2246 (unsigned int)create_disposition,
2247 (unsigned int)file_attributes));
2249 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
2250 (conn->fs_capabilities & FILE_NAMED_STREAMS) &&
2251 is_ntfs_stream_name(fname)) {
2252 DEBUG(2, ("open_directory: %s is a stream name!\n", fname));
2253 return NT_STATUS_NOT_A_DIRECTORY;
2256 status = calculate_access_mask(conn, fname, dir_existed,
2257 access_mask,
2258 &access_mask);
2259 if (!NT_STATUS_IS_OK(status)) {
2260 DEBUG(10, ("open_directory: calculate_access_mask "
2261 "on file %s returned %s\n",
2262 fname,
2263 nt_errstr(status)));
2264 return status;
2267 switch( create_disposition ) {
2268 case FILE_OPEN:
2270 info = FILE_WAS_OPENED;
2273 * We want to follow symlinks here.
2276 if (SMB_VFS_STAT(conn, fname, psbuf) != 0) {
2277 return map_nt_error_from_unix(errno);
2280 break;
2282 case FILE_CREATE:
2284 /* If directory exists error. If directory doesn't
2285 * exist create. */
2287 status = mkdir_internal(conn,
2288 fname,
2289 file_attributes,
2290 psbuf);
2292 if (!NT_STATUS_IS_OK(status)) {
2293 DEBUG(2, ("open_directory: unable to create "
2294 "%s. Error was %s\n", fname,
2295 nt_errstr(status)));
2296 return status;
2299 info = FILE_WAS_CREATED;
2300 break;
2302 case FILE_OPEN_IF:
2304 * If directory exists open. If directory doesn't
2305 * exist create.
2308 status = mkdir_internal(conn,
2309 fname,
2310 file_attributes,
2311 psbuf);
2313 if (NT_STATUS_IS_OK(status)) {
2314 info = FILE_WAS_CREATED;
2317 if (NT_STATUS_EQUAL(status,
2318 NT_STATUS_OBJECT_NAME_COLLISION)) {
2319 info = FILE_WAS_OPENED;
2320 status = NT_STATUS_OK;
2323 break;
2325 case FILE_SUPERSEDE:
2326 case FILE_OVERWRITE:
2327 case FILE_OVERWRITE_IF:
2328 default:
2329 DEBUG(5,("open_directory: invalid create_disposition "
2330 "0x%x for directory %s\n",
2331 (unsigned int)create_disposition, fname));
2332 return NT_STATUS_INVALID_PARAMETER;
2335 if(!S_ISDIR(psbuf->st_mode)) {
2336 DEBUG(5,("open_directory: %s is not a directory !\n",
2337 fname ));
2338 return NT_STATUS_NOT_A_DIRECTORY;
2341 if (info == FILE_WAS_OPENED) {
2342 status = check_open_rights(conn,
2343 fname,
2344 access_mask);
2345 if (!NT_STATUS_IS_OK(status)) {
2346 DEBUG(10, ("open_directory: check_open_rights on "
2347 "file %s failed with %s\n",
2348 fname,
2349 nt_errstr(status)));
2350 return status;
2354 status = file_new(req, conn, &fsp);
2355 if(!NT_STATUS_IS_OK(status)) {
2356 return status;
2360 * Setup the files_struct for it.
2363 fsp->mode = psbuf->st_mode;
2364 fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
2365 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
2366 fsp->file_pid = req ? req->smbpid : 0;
2367 fsp->can_lock = False;
2368 fsp->can_read = False;
2369 fsp->can_write = False;
2371 fsp->share_access = share_access;
2372 fsp->fh->private_options = create_options;
2374 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2376 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2377 fsp->print_file = False;
2378 fsp->modified = False;
2379 fsp->oplock_type = NO_OPLOCK;
2380 fsp->sent_oplock_break = NO_BREAK_SENT;
2381 fsp->is_directory = True;
2382 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
2384 string_set(&fsp->fsp_name,fname);
2386 mtimespec = get_mtimespec(psbuf);
2388 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
2389 conn->connectpath,
2390 fname, &mtimespec);
2392 if (lck == NULL) {
2393 DEBUG(0, ("open_directory: Could not get share mode lock for %s\n", fname));
2394 file_free(req, fsp);
2395 return NT_STATUS_SHARING_VIOLATION;
2398 status = open_mode_check(conn, fname, lck,
2399 access_mask, share_access,
2400 create_options, &dir_existed);
2402 if (!NT_STATUS_IS_OK(status)) {
2403 TALLOC_FREE(lck);
2404 file_free(req, fsp);
2405 return status;
2408 set_share_mode(lck, fsp, conn->server_info->utok.uid, 0, NO_OPLOCK,
2409 True);
2411 /* For directories the delete on close bit at open time seems
2412 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2413 if (create_options & FILE_DELETE_ON_CLOSE) {
2414 status = can_set_delete_on_close(fsp, True, 0);
2415 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
2416 TALLOC_FREE(lck);
2417 file_free(req, fsp);
2418 return status;
2421 if (NT_STATUS_IS_OK(status)) {
2422 /* Note that here we set the *inital* delete on close flag,
2423 not the regular one. The magic gets handled in close. */
2424 fsp->initial_delete_on_close = True;
2428 TALLOC_FREE(lck);
2430 if (pinfo) {
2431 *pinfo = info;
2434 conn->num_files_open++;
2436 *result = fsp;
2437 return NT_STATUS_OK;
2440 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req, const char *directory)
2442 NTSTATUS status;
2443 SMB_STRUCT_STAT sbuf;
2444 files_struct *fsp;
2446 SET_STAT_INVALID(sbuf);
2448 status = open_directory(conn, req, directory, &sbuf,
2449 FILE_READ_ATTRIBUTES, /* Just a stat open */
2450 FILE_SHARE_NONE, /* Ignored for stat opens */
2451 FILE_CREATE,
2453 FILE_ATTRIBUTE_DIRECTORY,
2454 NULL,
2455 &fsp);
2457 if (NT_STATUS_IS_OK(status)) {
2458 close_file(req, fsp, NORMAL_CLOSE);
2461 return status;
2464 /****************************************************************************
2465 Receive notification that one of our open files has been renamed by another
2466 smbd process.
2467 ****************************************************************************/
2469 void msg_file_was_renamed(struct messaging_context *msg,
2470 void *private_data,
2471 uint32_t msg_type,
2472 struct server_id server_id,
2473 DATA_BLOB *data)
2475 files_struct *fsp;
2476 char *frm = (char *)data->data;
2477 struct file_id id;
2478 const char *sharepath;
2479 const char *newname;
2480 size_t sp_len;
2482 if (data->data == NULL
2483 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
2484 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
2485 (int)data->length));
2486 return;
2489 /* Unpack the message. */
2490 pull_file_id_16(frm, &id);
2491 sharepath = &frm[16];
2492 newname = sharepath + strlen(sharepath) + 1;
2493 sp_len = strlen(sharepath);
2495 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
2496 "file_id %s\n",
2497 sharepath, newname, file_id_string_tos(&id)));
2499 for(fsp = file_find_di_first(id); fsp; fsp = file_find_di_next(fsp)) {
2500 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
2501 DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n",
2502 fsp->fnum, fsp->fsp_name, newname ));
2503 string_set(&fsp->fsp_name, newname);
2504 } else {
2505 /* TODO. JRA. */
2506 /* Now we have the complete path we can work out if this is
2507 actually within this share and adjust newname accordingly. */
2508 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
2509 "not sharepath %s) "
2510 "fnum %d from %s -> %s\n",
2511 fsp->conn->connectpath,
2512 sharepath,
2513 fsp->fnum,
2514 fsp->fsp_name,
2515 newname ));
2520 struct case_semantics_state {
2521 connection_struct *conn;
2522 bool case_sensitive;
2523 bool case_preserve;
2524 bool short_case_preserve;
2527 /****************************************************************************
2528 Restore case semantics.
2529 ****************************************************************************/
2530 static int restore_case_semantics(struct case_semantics_state *state)
2532 state->conn->case_sensitive = state->case_sensitive;
2533 state->conn->case_preserve = state->case_preserve;
2534 state->conn->short_case_preserve = state->short_case_preserve;
2535 return 0;
2538 /****************************************************************************
2539 Save case semantics.
2540 ****************************************************************************/
2541 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
2542 connection_struct *conn)
2544 struct case_semantics_state *result;
2546 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
2547 DEBUG(0, ("talloc failed\n"));
2548 return NULL;
2551 result->conn = conn;
2552 result->case_sensitive = conn->case_sensitive;
2553 result->case_preserve = conn->case_preserve;
2554 result->short_case_preserve = conn->short_case_preserve;
2556 /* Set to POSIX. */
2557 conn->case_sensitive = True;
2558 conn->case_preserve = True;
2559 conn->short_case_preserve = True;
2561 talloc_set_destructor(result, restore_case_semantics);
2563 return result;
2567 * If a main file is opened for delete, all streams need to be checked for
2568 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
2569 * If that works, delete them all by setting the delete on close and close.
2572 static NTSTATUS open_streams_for_delete(connection_struct *conn,
2573 const char *fname)
2575 struct stream_struct *stream_info;
2576 files_struct **streams;
2577 int i;
2578 unsigned int num_streams;
2579 TALLOC_CTX *frame = talloc_stackframe();
2580 NTSTATUS status;
2582 status = SMB_VFS_STREAMINFO(conn, NULL, fname, talloc_tos(),
2583 &num_streams, &stream_info);
2585 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
2586 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
2587 DEBUG(10, ("no streams around\n"));
2588 TALLOC_FREE(frame);
2589 return NT_STATUS_OK;
2592 if (!NT_STATUS_IS_OK(status)) {
2593 DEBUG(10, ("SMB_VFS_STREAMINFO failed: %s\n",
2594 nt_errstr(status)));
2595 goto fail;
2598 DEBUG(10, ("open_streams_for_delete found %d streams\n",
2599 num_streams));
2601 if (num_streams == 0) {
2602 TALLOC_FREE(frame);
2603 return NT_STATUS_OK;
2606 streams = TALLOC_ARRAY(talloc_tos(), files_struct *, num_streams);
2607 if (streams == NULL) {
2608 DEBUG(0, ("talloc failed\n"));
2609 status = NT_STATUS_NO_MEMORY;
2610 goto fail;
2613 for (i=0; i<num_streams; i++) {
2614 char *streamname;
2616 if (strequal(stream_info[i].name, "::$DATA")) {
2617 streams[i] = NULL;
2618 continue;
2621 streamname = talloc_asprintf(talloc_tos(), "%s%s", fname,
2622 stream_info[i].name);
2624 if (streamname == NULL) {
2625 DEBUG(0, ("talloc_aprintf failed\n"));
2626 status = NT_STATUS_NO_MEMORY;
2627 goto fail;
2630 status = create_file_unixpath
2631 (conn, /* conn */
2632 NULL, /* req */
2633 streamname, /* fname */
2634 DELETE_ACCESS, /* access_mask */
2635 FILE_SHARE_READ | FILE_SHARE_WRITE
2636 | FILE_SHARE_DELETE, /* share_access */
2637 FILE_OPEN, /* create_disposition*/
2638 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* create_options */
2639 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
2640 0, /* oplock_request */
2641 0, /* allocation_size */
2642 NULL, /* sd */
2643 NULL, /* ea_list */
2644 &streams[i], /* result */
2645 NULL, /* pinfo */
2646 NULL); /* psbuf */
2648 TALLOC_FREE(streamname);
2650 if (!NT_STATUS_IS_OK(status)) {
2651 DEBUG(10, ("Could not open stream %s: %s\n",
2652 streamname, nt_errstr(status)));
2653 break;
2658 * don't touch the variable "status" beyond this point :-)
2661 for (i -= 1 ; i >= 0; i--) {
2662 if (streams[i] == NULL) {
2663 continue;
2666 DEBUG(10, ("Closing stream # %d, %s\n", i,
2667 streams[i]->fsp_name));
2668 close_file(NULL, streams[i], NORMAL_CLOSE);
2671 fail:
2672 TALLOC_FREE(frame);
2673 return status;
2677 * Wrapper around open_file_ntcreate and open_directory
2680 NTSTATUS create_file_unixpath(connection_struct *conn,
2681 struct smb_request *req,
2682 const char *fname,
2683 uint32_t access_mask,
2684 uint32_t share_access,
2685 uint32_t create_disposition,
2686 uint32_t create_options,
2687 uint32_t file_attributes,
2688 uint32_t oplock_request,
2689 uint64_t allocation_size,
2690 struct security_descriptor *sd,
2691 struct ea_list *ea_list,
2693 files_struct **result,
2694 int *pinfo,
2695 SMB_STRUCT_STAT *psbuf)
2697 SMB_STRUCT_STAT sbuf;
2698 int info = FILE_WAS_OPENED;
2699 files_struct *base_fsp = NULL;
2700 files_struct *fsp = NULL;
2701 NTSTATUS status;
2703 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
2704 "file_attributes = 0x%x, share_access = 0x%x, "
2705 "create_disposition = 0x%x create_options = 0x%x "
2706 "oplock_request = 0x%x ea_list = 0x%p, sd = 0x%p, "
2707 "fname = %s\n",
2708 (unsigned int)access_mask,
2709 (unsigned int)file_attributes,
2710 (unsigned int)share_access,
2711 (unsigned int)create_disposition,
2712 (unsigned int)create_options,
2713 (unsigned int)oplock_request,
2714 ea_list, sd, fname));
2716 if (create_options & FILE_OPEN_BY_FILE_ID) {
2717 status = NT_STATUS_NOT_SUPPORTED;
2718 goto fail;
2721 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
2722 status = NT_STATUS_INVALID_PARAMETER;
2723 goto fail;
2726 if (req == NULL) {
2727 oplock_request |= INTERNAL_OPEN_ONLY;
2730 if (psbuf != NULL) {
2731 sbuf = *psbuf;
2733 else {
2734 if (SMB_VFS_STAT(conn, fname, &sbuf) == -1) {
2735 SET_STAT_INVALID(sbuf);
2739 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
2740 && (access_mask & DELETE_ACCESS)
2741 && !is_ntfs_stream_name(fname)) {
2743 * We can't open a file with DELETE access if any of the
2744 * streams is open without FILE_SHARE_DELETE
2746 status = open_streams_for_delete(conn, fname);
2748 if (!NT_STATUS_IS_OK(status)) {
2749 goto fail;
2753 /* This is the correct thing to do (check every time) but can_delete
2754 * is expensive (it may have to read the parent directory
2755 * permissions). So for now we're not doing it unless we have a strong
2756 * hint the client is really going to delete this file. If the client
2757 * is forcing FILE_CREATE let the filesystem take care of the
2758 * permissions. */
2760 /* Setting FILE_SHARE_DELETE is the hint. */
2762 if (lp_acl_check_permissions(SNUM(conn))
2763 && (create_disposition != FILE_CREATE)
2764 && (share_access & FILE_SHARE_DELETE)
2765 && (access_mask & DELETE_ACCESS)
2766 && (!can_delete_file_in_directory(conn, fname))) {
2767 status = NT_STATUS_ACCESS_DENIED;
2768 goto fail;
2771 #if 0
2772 /* We need to support SeSecurityPrivilege for this. */
2773 if ((access_mask & SEC_RIGHT_SYSTEM_SECURITY) &&
2774 !user_has_privileges(current_user.nt_user_token,
2775 &se_security)) {
2776 status = NT_STATUS_PRIVILEGE_NOT_HELD;
2777 goto fail;
2779 #endif
2781 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
2782 && is_ntfs_stream_name(fname)
2783 && (!(create_options & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
2784 char *base;
2785 uint32 base_create_disposition;
2787 if (create_options & FILE_DIRECTORY_FILE) {
2788 status = NT_STATUS_NOT_A_DIRECTORY;
2789 goto fail;
2792 status = split_ntfs_stream_name(talloc_tos(), fname,
2793 &base, NULL);
2794 if (!NT_STATUS_IS_OK(status)) {
2795 DEBUG(10, ("create_file_unixpath: "
2796 "split_ntfs_stream_name failed: %s\n",
2797 nt_errstr(status)));
2798 goto fail;
2801 SMB_ASSERT(!is_ntfs_stream_name(base)); /* paranoia.. */
2803 switch (create_disposition) {
2804 case FILE_OPEN:
2805 base_create_disposition = FILE_OPEN;
2806 break;
2807 default:
2808 base_create_disposition = FILE_OPEN_IF;
2809 break;
2812 status = create_file_unixpath(conn, NULL, base, 0,
2813 FILE_SHARE_READ
2814 | FILE_SHARE_WRITE
2815 | FILE_SHARE_DELETE,
2816 base_create_disposition,
2817 0, 0, 0, 0, NULL, NULL,
2818 &base_fsp, NULL, NULL);
2819 if (!NT_STATUS_IS_OK(status)) {
2820 DEBUG(10, ("create_file_unixpath for base %s failed: "
2821 "%s\n", base, nt_errstr(status)));
2822 goto fail;
2827 * If it's a request for a directory open, deal with it separately.
2830 if (create_options & FILE_DIRECTORY_FILE) {
2832 if (create_options & FILE_NON_DIRECTORY_FILE) {
2833 status = NT_STATUS_INVALID_PARAMETER;
2834 goto fail;
2837 /* Can't open a temp directory. IFS kit test. */
2838 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
2839 status = NT_STATUS_INVALID_PARAMETER;
2840 goto fail;
2844 * We will get a create directory here if the Win32
2845 * app specified a security descriptor in the
2846 * CreateDirectory() call.
2849 oplock_request = 0;
2850 status = open_directory(
2851 conn, req, fname, &sbuf, access_mask, share_access,
2852 create_disposition, create_options, file_attributes,
2853 &info, &fsp);
2854 } else {
2857 * Ordinary file case.
2860 status = open_file_ntcreate(
2861 conn, req, fname, &sbuf, access_mask, share_access,
2862 create_disposition, create_options, file_attributes,
2863 oplock_request, &info, &fsp);
2865 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
2868 * Fail the open if it was explicitly a non-directory
2869 * file.
2872 if (create_options & FILE_NON_DIRECTORY_FILE) {
2873 status = NT_STATUS_FILE_IS_A_DIRECTORY;
2874 goto fail;
2877 oplock_request = 0;
2878 status = open_directory(
2879 conn, req, fname, &sbuf, access_mask,
2880 share_access, create_disposition,
2881 create_options, file_attributes,
2882 &info, &fsp);
2886 if (!NT_STATUS_IS_OK(status)) {
2887 goto fail;
2891 * According to the MS documentation, the only time the security
2892 * descriptor is applied to the opened file is iff we *created* the
2893 * file; an existing file stays the same.
2895 * Also, it seems (from observation) that you can open the file with
2896 * any access mask but you can still write the sd. We need to override
2897 * the granted access before we call set_sd
2898 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
2901 if ((sd != NULL) && (info == FILE_WAS_CREATED)
2902 && lp_nt_acl_support(SNUM(conn))) {
2904 uint32_t sec_info_sent = ALL_SECURITY_INFORMATION;
2905 uint32_t saved_access_mask = fsp->access_mask;
2907 if (sd->owner_sid == NULL) {
2908 sec_info_sent &= ~OWNER_SECURITY_INFORMATION;
2910 if (sd->group_sid == NULL) {
2911 sec_info_sent &= ~GROUP_SECURITY_INFORMATION;
2913 if (sd->sacl == NULL) {
2914 sec_info_sent &= ~SACL_SECURITY_INFORMATION;
2916 if (sd->dacl == NULL) {
2917 sec_info_sent &= ~DACL_SECURITY_INFORMATION;
2920 fsp->access_mask = FILE_GENERIC_ALL;
2922 /* Convert all the generic bits. */
2923 security_acl_map_generic(sd->dacl, &file_generic_mapping);
2924 security_acl_map_generic(sd->sacl, &file_generic_mapping);
2926 if (sec_info_sent & (OWNER_SECURITY_INFORMATION|
2927 GROUP_SECURITY_INFORMATION|
2928 DACL_SECURITY_INFORMATION|
2929 SACL_SECURITY_INFORMATION)) {
2930 status = SMB_VFS_FSET_NT_ACL(fsp, sec_info_sent, sd);
2933 fsp->access_mask = saved_access_mask;
2935 if (!NT_STATUS_IS_OK(status)) {
2936 goto fail;
2940 if ((ea_list != NULL) && (info == FILE_WAS_CREATED)) {
2941 status = set_ea(conn, fsp, fname, ea_list);
2942 if (!NT_STATUS_IS_OK(status)) {
2943 goto fail;
2947 if (!fsp->is_directory && S_ISDIR(sbuf.st_mode)) {
2948 status = NT_STATUS_ACCESS_DENIED;
2949 goto fail;
2952 /* Save the requested allocation size. */
2953 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
2954 if (allocation_size
2955 && (allocation_size > sbuf.st_size)) {
2956 fsp->initial_allocation_size = smb_roundup(
2957 fsp->conn, allocation_size);
2958 if (fsp->is_directory) {
2959 /* Can't set allocation size on a directory. */
2960 status = NT_STATUS_ACCESS_DENIED;
2961 goto fail;
2963 if (vfs_allocate_file_space(
2964 fsp, fsp->initial_allocation_size) == -1) {
2965 status = NT_STATUS_DISK_FULL;
2966 goto fail;
2968 } else {
2969 fsp->initial_allocation_size = smb_roundup(
2970 fsp->conn, (uint64_t)sbuf.st_size);
2974 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
2977 * Set fsp->base_fsp late enough that we can't "goto fail" anymore. In
2978 * the fail: branch we call close_file(fsp, ERROR_CLOSE) which would
2979 * also close fsp->base_fsp which we have to also do explicitly in
2980 * this routine here, as not in all "goto fail:" we have the fsp set
2981 * up already to be initialized with the base_fsp.
2984 fsp->base_fsp = base_fsp;
2986 *result = fsp;
2987 if (pinfo != NULL) {
2988 *pinfo = info;
2990 if (psbuf != NULL) {
2991 if ((fsp->fh == NULL) || (fsp->fh->fd == -1)) {
2992 *psbuf = sbuf;
2994 else {
2995 SMB_VFS_FSTAT(fsp, psbuf);
2998 return NT_STATUS_OK;
3000 fail:
3001 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
3003 if (fsp != NULL) {
3004 close_file(req, fsp, ERROR_CLOSE);
3005 fsp = NULL;
3007 if (base_fsp != NULL) {
3008 close_file(req, base_fsp, ERROR_CLOSE);
3009 base_fsp = NULL;
3011 return status;
3014 NTSTATUS create_file(connection_struct *conn,
3015 struct smb_request *req,
3016 uint16_t root_dir_fid,
3017 const char *fname,
3018 uint32_t access_mask,
3019 uint32_t share_access,
3020 uint32_t create_disposition,
3021 uint32_t create_options,
3022 uint32_t file_attributes,
3023 uint32_t oplock_request,
3024 uint64_t allocation_size,
3025 struct security_descriptor *sd,
3026 struct ea_list *ea_list,
3028 files_struct **result,
3029 int *pinfo,
3030 SMB_STRUCT_STAT *psbuf)
3032 struct case_semantics_state *case_state = NULL;
3033 SMB_STRUCT_STAT sbuf;
3034 int info = FILE_WAS_OPENED;
3035 files_struct *fsp = NULL;
3036 NTSTATUS status;
3038 DEBUG(10,("create_file: access_mask = 0x%x "
3039 "file_attributes = 0x%x, share_access = 0x%x, "
3040 "create_disposition = 0x%x create_options = 0x%x "
3041 "oplock_request = 0x%x "
3042 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
3043 "fname = %s\n",
3044 (unsigned int)access_mask,
3045 (unsigned int)file_attributes,
3046 (unsigned int)share_access,
3047 (unsigned int)create_disposition,
3048 (unsigned int)create_options,
3049 (unsigned int)oplock_request,
3050 (unsigned int)root_dir_fid,
3051 ea_list, sd, fname));
3054 * Get the file name.
3057 if (root_dir_fid != 0) {
3059 * This filename is relative to a directory fid.
3061 char *parent_fname = NULL;
3062 files_struct *dir_fsp = file_fsp(req, root_dir_fid);
3064 if (dir_fsp == NULL) {
3065 status = NT_STATUS_INVALID_HANDLE;
3066 goto fail;
3069 if (!dir_fsp->is_directory) {
3072 * Check to see if this is a mac fork of some kind.
3075 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
3076 is_ntfs_stream_name(fname)) {
3077 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3078 goto fail;
3082 we need to handle the case when we get a
3083 relative open relative to a file and the
3084 pathname is blank - this is a reopen!
3085 (hint from demyn plantenberg)
3088 status = NT_STATUS_INVALID_HANDLE;
3089 goto fail;
3092 if (ISDOT(dir_fsp->fsp_name)) {
3094 * We're at the toplevel dir, the final file name
3095 * must not contain ./, as this is filtered out
3096 * normally by srvstr_get_path and unix_convert
3097 * explicitly rejects paths containing ./.
3099 parent_fname = talloc_strdup(talloc_tos(), "");
3100 if (parent_fname == NULL) {
3101 status = NT_STATUS_NO_MEMORY;
3102 goto fail;
3104 } else {
3105 size_t dir_name_len = strlen(dir_fsp->fsp_name);
3108 * Copy in the base directory name.
3111 parent_fname = TALLOC_ARRAY(talloc_tos(), char,
3112 dir_name_len+2);
3113 if (parent_fname == NULL) {
3114 status = NT_STATUS_NO_MEMORY;
3115 goto fail;
3117 memcpy(parent_fname, dir_fsp->fsp_name,
3118 dir_name_len+1);
3121 * Ensure it ends in a '/'.
3122 * We used TALLOC_SIZE +2 to add space for the '/'.
3125 if(dir_name_len
3126 && (parent_fname[dir_name_len-1] != '\\')
3127 && (parent_fname[dir_name_len-1] != '/')) {
3128 parent_fname[dir_name_len] = '/';
3129 parent_fname[dir_name_len+1] = '\0';
3133 fname = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
3134 fname);
3135 if (fname == NULL) {
3136 status = NT_STATUS_NO_MEMORY;
3137 goto fail;
3142 * Check to see if this is a mac fork of some kind.
3145 if (is_ntfs_stream_name(fname)) {
3146 enum FAKE_FILE_TYPE fake_file_type;
3148 fake_file_type = is_fake_file(fname);
3150 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
3153 * Here we go! support for changing the disk quotas
3154 * --metze
3156 * We need to fake up to open this MAGIC QUOTA file
3157 * and return a valid FID.
3159 * w2k close this file directly after openening xp
3160 * also tries a QUERY_FILE_INFO on the file and then
3161 * close it
3163 status = open_fake_file(req, conn, req->vuid,
3164 fake_file_type, fname,
3165 access_mask, &fsp);
3166 if (!NT_STATUS_IS_OK(status)) {
3167 goto fail;
3170 ZERO_STRUCT(sbuf);
3171 goto done;
3174 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
3175 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3176 goto fail;
3180 if ((req != NULL) && (req->flags2 & FLAGS2_DFS_PATHNAMES)) {
3181 char *resolved_fname;
3183 status = resolve_dfspath(talloc_tos(), conn, true, fname,
3184 &resolved_fname);
3186 if (!NT_STATUS_IS_OK(status)) {
3188 * For PATH_NOT_COVERED we had
3189 * reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
3190 * ERRSRV, ERRbadpath);
3191 * Need to fix in callers
3193 goto fail;
3195 fname = resolved_fname;
3199 * Check if POSIX semantics are wanted.
3202 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
3203 case_state = set_posix_case_semantics(talloc_tos(), conn);
3204 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
3208 char *converted_fname;
3210 SET_STAT_INVALID(sbuf);
3212 status = unix_convert(talloc_tos(), conn, fname, False,
3213 &converted_fname, NULL, &sbuf);
3214 if (!NT_STATUS_IS_OK(status)) {
3215 goto fail;
3217 fname = converted_fname;
3220 TALLOC_FREE(case_state);
3222 /* All file access must go through check_name() */
3224 status = check_name(conn, fname);
3225 if (!NT_STATUS_IS_OK(status)) {
3226 goto fail;
3229 status = create_file_unixpath(
3230 conn, req, fname, access_mask, share_access,
3231 create_disposition, create_options, file_attributes,
3232 oplock_request, allocation_size, sd, ea_list,
3233 &fsp, &info, &sbuf);
3235 if (!NT_STATUS_IS_OK(status)) {
3236 goto fail;
3239 done:
3240 DEBUG(10, ("create_file: info=%d\n", info));
3242 *result = fsp;
3243 if (pinfo != NULL) {
3244 *pinfo = info;
3246 if (psbuf != NULL) {
3247 *psbuf = sbuf;
3249 return NT_STATUS_OK;
3251 fail:
3252 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
3254 if (fsp != NULL) {
3255 close_file(req, fsp, ERROR_CLOSE);
3256 fsp = NULL;
3258 return status;