Get closer to passing S4 RAW-ACLs.
[Samba.git] / source / smbd / open.c
blob19b6b2736aff18dc87077d5aac280f6655b71be4
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 fd support routines - attempt to do a dos_open.
34 ****************************************************************************/
36 static NTSTATUS fd_open(struct connection_struct *conn,
37 const char *fname,
38 files_struct *fsp,
39 int flags,
40 mode_t mode)
42 NTSTATUS status = NT_STATUS_OK;
44 #ifdef O_NOFOLLOW
45 /*
46 * Never follow symlinks on a POSIX client. The
47 * client should be doing this.
50 if (fsp->posix_open || !lp_symlinks(SNUM(conn))) {
51 flags |= O_NOFOLLOW;
53 #endif
55 fsp->fh->fd = SMB_VFS_OPEN(conn,fname,fsp,flags,mode);
56 if (fsp->fh->fd == -1) {
57 status = map_nt_error_from_unix(errno);
60 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
61 fname, flags, (int)mode, fsp->fh->fd,
62 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
64 return status;
67 /****************************************************************************
68 Close the file associated with a fsp.
69 ****************************************************************************/
71 NTSTATUS fd_close(files_struct *fsp)
73 int ret;
75 if (fsp->fh->fd == -1) {
76 return NT_STATUS_OK; /* What we used to call a stat open. */
78 if (fsp->fh->ref_count > 1) {
79 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
82 ret = SMB_VFS_CLOSE(fsp);
83 fsp->fh->fd = -1;
84 if (ret == -1) {
85 return map_nt_error_from_unix(errno);
87 return NT_STATUS_OK;
90 /****************************************************************************
91 Change the ownership of a file to that of the parent directory.
92 Do this by fd if possible.
93 ****************************************************************************/
95 static void change_file_owner_to_parent(connection_struct *conn,
96 const char *inherit_from_dir,
97 files_struct *fsp)
99 SMB_STRUCT_STAT parent_st;
100 int ret;
102 ret = SMB_VFS_STAT(conn, inherit_from_dir, &parent_st);
103 if (ret == -1) {
104 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
105 "directory %s. Error was %s\n",
106 inherit_from_dir, strerror(errno) ));
107 return;
110 become_root();
111 ret = SMB_VFS_FCHOWN(fsp, parent_st.st_uid, (gid_t)-1);
112 unbecome_root();
113 if (ret == -1) {
114 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
115 "file %s to parent directory uid %u. Error "
116 "was %s\n", fsp->fsp_name,
117 (unsigned int)parent_st.st_uid,
118 strerror(errno) ));
121 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
122 "parent directory uid %u.\n", fsp->fsp_name,
123 (unsigned int)parent_st.st_uid ));
126 static NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
127 const char *inherit_from_dir,
128 const char *fname,
129 SMB_STRUCT_STAT *psbuf)
131 char *saved_dir = NULL;
132 SMB_STRUCT_STAT sbuf;
133 SMB_STRUCT_STAT parent_st;
134 TALLOC_CTX *ctx = talloc_tos();
135 NTSTATUS status = NT_STATUS_OK;
136 int ret;
138 ret = SMB_VFS_STAT(conn, inherit_from_dir, &parent_st);
139 if (ret == -1) {
140 status = map_nt_error_from_unix(errno);
141 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
142 "directory %s. Error was %s\n",
143 inherit_from_dir, strerror(errno) ));
144 return status;
147 /* We've already done an lstat into psbuf, and we know it's a
148 directory. If we can cd into the directory and the dev/ino
149 are the same then we can safely chown without races as
150 we're locking the directory in place by being in it. This
151 should work on any UNIX (thanks tridge :-). JRA.
154 saved_dir = vfs_GetWd(ctx,conn);
155 if (!saved_dir) {
156 status = map_nt_error_from_unix(errno);
157 DEBUG(0,("change_dir_owner_to_parent: failed to get "
158 "current working directory. Error was %s\n",
159 strerror(errno)));
160 return status;
163 /* Chdir into the new path. */
164 if (vfs_ChDir(conn, fname) == -1) {
165 status = map_nt_error_from_unix(errno);
166 DEBUG(0,("change_dir_owner_to_parent: failed to change "
167 "current working directory to %s. Error "
168 "was %s\n", fname, strerror(errno) ));
169 goto out;
172 if (SMB_VFS_STAT(conn,".",&sbuf) == -1) {
173 status = map_nt_error_from_unix(errno);
174 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
175 "directory '.' (%s) Error was %s\n",
176 fname, strerror(errno)));
177 goto out;
180 /* Ensure we're pointing at the same place. */
181 if (sbuf.st_dev != psbuf->st_dev ||
182 sbuf.st_ino != psbuf->st_ino ||
183 sbuf.st_mode != psbuf->st_mode ) {
184 DEBUG(0,("change_dir_owner_to_parent: "
185 "device/inode/mode on directory %s changed. "
186 "Refusing to chown !\n", fname ));
187 status = NT_STATUS_ACCESS_DENIED;
188 goto out;
191 become_root();
192 ret = SMB_VFS_CHOWN(conn, ".", parent_st.st_uid, (gid_t)-1);
193 unbecome_root();
194 if (ret == -1) {
195 status = map_nt_error_from_unix(errno);
196 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
197 "directory %s to parent directory uid %u. "
198 "Error was %s\n", fname,
199 (unsigned int)parent_st.st_uid, strerror(errno) ));
200 goto out;
203 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
204 "directory %s to parent directory uid %u.\n",
205 fname, (unsigned int)parent_st.st_uid ));
207 out:
209 vfs_ChDir(conn,saved_dir);
210 return status;
213 /****************************************************************************
214 Open a file.
215 ****************************************************************************/
217 static NTSTATUS open_file(files_struct *fsp,
218 connection_struct *conn,
219 struct smb_request *req,
220 const char *parent_dir,
221 const char *name,
222 const char *path,
223 SMB_STRUCT_STAT *psbuf,
224 int flags,
225 mode_t unx_mode,
226 uint32 access_mask, /* client requested access mask. */
227 uint32 open_access_mask) /* what we're actually using in the open. */
229 NTSTATUS status = NT_STATUS_OK;
230 int accmode = (flags & O_ACCMODE);
231 int local_flags = flags;
232 bool file_existed = VALID_STAT(*psbuf);
234 fsp->fh->fd = -1;
235 errno = EPERM;
237 /* Check permissions */
240 * This code was changed after seeing a client open request
241 * containing the open mode of (DENY_WRITE/read-only) with
242 * the 'create if not exist' bit set. The previous code
243 * would fail to open the file read only on a read-only share
244 * as it was checking the flags parameter directly against O_RDONLY,
245 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
246 * JRA.
249 if (!CAN_WRITE(conn)) {
250 /* It's a read-only share - fail if we wanted to write. */
251 if(accmode != O_RDONLY) {
252 DEBUG(3,("Permission denied opening %s\n", path));
253 return NT_STATUS_ACCESS_DENIED;
254 } else if(flags & O_CREAT) {
255 /* We don't want to write - but we must make sure that
256 O_CREAT doesn't create the file if we have write
257 access into the directory.
259 flags &= ~O_CREAT;
260 local_flags &= ~O_CREAT;
265 * This little piece of insanity is inspired by the
266 * fact that an NT client can open a file for O_RDONLY,
267 * but set the create disposition to FILE_EXISTS_TRUNCATE.
268 * If the client *can* write to the file, then it expects to
269 * truncate the file, even though it is opening for readonly.
270 * Quicken uses this stupid trick in backup file creation...
271 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
272 * for helping track this one down. It didn't bite us in 2.0.x
273 * as we always opened files read-write in that release. JRA.
276 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
277 DEBUG(10,("open_file: truncate requested on read-only open "
278 "for file %s\n", path));
279 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
282 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
283 (!file_existed && (local_flags & O_CREAT)) ||
284 ((local_flags & O_TRUNC) == O_TRUNC) ) {
287 * We can't actually truncate here as the file may be locked.
288 * open_file_ntcreate will take care of the truncate later. JRA.
291 local_flags &= ~O_TRUNC;
293 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
295 * We would block on opening a FIFO with no one else on the
296 * other end. Do what we used to do and add O_NONBLOCK to the
297 * open flags. JRA.
300 if (file_existed && S_ISFIFO(psbuf->st_mode)) {
301 local_flags |= O_NONBLOCK;
303 #endif
305 /* Don't create files with Microsoft wildcard characters. */
306 if ((local_flags & O_CREAT) && !file_existed &&
307 ms_has_wild(path)) {
308 return NT_STATUS_OBJECT_NAME_INVALID;
311 /* Actually do the open */
312 status = fd_open(conn, path, fsp, local_flags, unx_mode);
313 if (!NT_STATUS_IS_OK(status)) {
314 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
315 "(flags=%d)\n",
316 path,nt_errstr(status),local_flags,flags));
317 return status;
320 if ((local_flags & O_CREAT) && !file_existed) {
322 /* Inherit the ACL if required */
323 if (lp_inherit_perms(SNUM(conn))) {
324 inherit_access_posix_acl(conn, parent_dir, path,
325 unx_mode);
328 /* Change the owner if required. */
329 if (lp_inherit_owner(SNUM(conn))) {
330 change_file_owner_to_parent(conn, parent_dir,
331 fsp);
334 notify_fname(conn, NOTIFY_ACTION_ADDED,
335 FILE_NOTIFY_CHANGE_FILE_NAME, path);
338 } else {
339 fsp->fh->fd = -1; /* What we used to call a stat open. */
342 if (!file_existed) {
343 int ret;
345 if (fsp->fh->fd == -1) {
346 ret = SMB_VFS_STAT(conn, path, psbuf);
347 } else {
348 ret = SMB_VFS_FSTAT(fsp, psbuf);
349 /* If we have an fd, this stat should succeed. */
350 if (ret == -1) {
351 DEBUG(0,("Error doing fstat on open file %s "
352 "(%s)\n", path,strerror(errno) ));
356 /* For a non-io open, this stat failing means file not found. JRA */
357 if (ret == -1) {
358 status = map_nt_error_from_unix(errno);
359 fd_close(fsp);
360 return status;
365 * POSIX allows read-only opens of directories. We don't
366 * want to do this (we use a different code path for this)
367 * so catch a directory open and return an EISDIR. JRA.
370 if(S_ISDIR(psbuf->st_mode)) {
371 fd_close(fsp);
372 errno = EISDIR;
373 return NT_STATUS_FILE_IS_A_DIRECTORY;
376 fsp->mode = psbuf->st_mode;
377 fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
378 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
379 fsp->file_pid = req ? req->smbpid : 0;
380 fsp->can_lock = True;
381 fsp->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
382 if (!CAN_WRITE(conn)) {
383 fsp->can_write = False;
384 } else {
385 fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ?
386 True : False;
388 fsp->print_file = False;
389 fsp->modified = False;
390 fsp->sent_oplock_break = NO_BREAK_SENT;
391 fsp->is_directory = False;
392 if (conn->aio_write_behind_list &&
393 is_in_path(path, conn->aio_write_behind_list, conn->case_sensitive)) {
394 fsp->aio_write_behind = True;
397 string_set(&fsp->fsp_name, path);
398 fsp->wcp = NULL; /* Write cache pointer. */
400 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
401 conn->server_info->unix_name,
402 fsp->fsp_name,
403 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
404 conn->num_files_open + 1));
406 errno = 0;
407 return NT_STATUS_OK;
410 /*******************************************************************
411 Return True if the filename is one of the special executable types.
412 ********************************************************************/
414 static bool is_executable(const char *fname)
416 if ((fname = strrchr_m(fname,'.'))) {
417 if (strequal(fname,".com") ||
418 strequal(fname,".dll") ||
419 strequal(fname,".exe") ||
420 strequal(fname,".sym")) {
421 return True;
424 return False;
427 /****************************************************************************
428 Check if we can open a file with a share mode.
429 Returns True if conflict, False if not.
430 ****************************************************************************/
432 static bool share_conflict(struct share_mode_entry *entry,
433 uint32 access_mask,
434 uint32 share_access)
436 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
437 "entry->share_access = 0x%x, "
438 "entry->private_options = 0x%x\n",
439 (unsigned int)entry->access_mask,
440 (unsigned int)entry->share_access,
441 (unsigned int)entry->private_options));
443 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
444 (unsigned int)access_mask, (unsigned int)share_access));
446 if ((entry->access_mask & (FILE_WRITE_DATA|
447 FILE_APPEND_DATA|
448 FILE_READ_DATA|
449 FILE_EXECUTE|
450 DELETE_ACCESS)) == 0) {
451 DEBUG(10,("share_conflict: No conflict due to "
452 "entry->access_mask = 0x%x\n",
453 (unsigned int)entry->access_mask ));
454 return False;
457 if ((access_mask & (FILE_WRITE_DATA|
458 FILE_APPEND_DATA|
459 FILE_READ_DATA|
460 FILE_EXECUTE|
461 DELETE_ACCESS)) == 0) {
462 DEBUG(10,("share_conflict: No conflict due to "
463 "access_mask = 0x%x\n",
464 (unsigned int)access_mask ));
465 return False;
468 #if 1 /* JRA TEST - Superdebug. */
469 #define CHECK_MASK(num, am, right, sa, share) \
470 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
471 (unsigned int)(num), (unsigned int)(am), \
472 (unsigned int)(right), (unsigned int)(am)&(right) )); \
473 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
474 (unsigned int)(num), (unsigned int)(sa), \
475 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
476 if (((am) & (right)) && !((sa) & (share))) { \
477 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
478 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
479 (unsigned int)(share) )); \
480 return True; \
482 #else
483 #define CHECK_MASK(num, am, right, sa, share) \
484 if (((am) & (right)) && !((sa) & (share))) { \
485 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
486 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
487 (unsigned int)(share) )); \
488 return True; \
490 #endif
492 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
493 share_access, FILE_SHARE_WRITE);
494 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
495 entry->share_access, FILE_SHARE_WRITE);
497 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
498 share_access, FILE_SHARE_READ);
499 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
500 entry->share_access, FILE_SHARE_READ);
502 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
503 share_access, FILE_SHARE_DELETE);
504 CHECK_MASK(6, access_mask, DELETE_ACCESS,
505 entry->share_access, FILE_SHARE_DELETE);
507 DEBUG(10,("share_conflict: No conflict.\n"));
508 return False;
511 #if defined(DEVELOPER)
512 static void validate_my_share_entries(int num,
513 struct share_mode_entry *share_entry)
515 files_struct *fsp;
517 if (!procid_is_me(&share_entry->pid)) {
518 return;
521 if (is_deferred_open_entry(share_entry) &&
522 !open_was_deferred(share_entry->op_mid)) {
523 char *str = talloc_asprintf(talloc_tos(),
524 "Got a deferred entry without a request: "
525 "PANIC: %s\n",
526 share_mode_str(talloc_tos(), num, share_entry));
527 smb_panic(str);
530 if (!is_valid_share_mode_entry(share_entry)) {
531 return;
534 fsp = file_find_dif(share_entry->id,
535 share_entry->share_file_id);
536 if (!fsp) {
537 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
538 share_mode_str(talloc_tos(), num, share_entry) ));
539 smb_panic("validate_my_share_entries: Cannot match a "
540 "share entry with an open file\n");
543 if (is_deferred_open_entry(share_entry) ||
544 is_unused_share_mode_entry(share_entry)) {
545 goto panic;
548 if ((share_entry->op_type == NO_OPLOCK) &&
549 (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK)) {
550 /* Someone has already written to it, but I haven't yet
551 * noticed */
552 return;
555 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
556 goto panic;
559 return;
561 panic:
563 char *str;
564 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
565 share_mode_str(talloc_tos(), num, share_entry) ));
566 str = talloc_asprintf(talloc_tos(),
567 "validate_my_share_entries: "
568 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
569 fsp->fsp_name, (unsigned int)fsp->oplock_type,
570 (unsigned int)share_entry->op_type );
571 smb_panic(str);
574 #endif
576 static bool is_stat_open(uint32 access_mask)
578 return (access_mask &&
579 ((access_mask & ~(SYNCHRONIZE_ACCESS| FILE_READ_ATTRIBUTES|
580 FILE_WRITE_ATTRIBUTES))==0) &&
581 ((access_mask & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|
582 FILE_WRITE_ATTRIBUTES)) != 0));
585 /****************************************************************************
586 Deal with share modes
587 Invarient: Share mode must be locked on entry and exit.
588 Returns -1 on error, or number of share modes on success (may be zero).
589 ****************************************************************************/
591 static NTSTATUS open_mode_check(connection_struct *conn,
592 const char *fname,
593 struct share_mode_lock *lck,
594 uint32 access_mask,
595 uint32 share_access,
596 uint32 create_options,
597 bool *file_existed)
599 int i;
601 if(lck->num_share_modes == 0) {
602 return NT_STATUS_OK;
605 *file_existed = True;
607 /* A delete on close prohibits everything */
609 if (lck->delete_on_close) {
610 return NT_STATUS_DELETE_PENDING;
613 if (is_stat_open(access_mask)) {
614 /* Stat open that doesn't trigger oplock breaks or share mode
615 * checks... ! JRA. */
616 return NT_STATUS_OK;
620 * Check if the share modes will give us access.
623 #if defined(DEVELOPER)
624 for(i = 0; i < lck->num_share_modes; i++) {
625 validate_my_share_entries(i, &lck->share_modes[i]);
627 #endif
629 if (!lp_share_modes(SNUM(conn))) {
630 return NT_STATUS_OK;
633 /* Now we check the share modes, after any oplock breaks. */
634 for(i = 0; i < lck->num_share_modes; i++) {
636 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
637 continue;
640 /* someone else has a share lock on it, check to see if we can
641 * too */
642 if (share_conflict(&lck->share_modes[i],
643 access_mask, share_access)) {
644 return NT_STATUS_SHARING_VIOLATION;
648 return NT_STATUS_OK;
651 static bool is_delete_request(files_struct *fsp) {
652 return ((fsp->access_mask == DELETE_ACCESS) &&
653 (fsp->oplock_type == NO_OPLOCK));
657 * 1) No files open at all or internal open: Grant whatever the client wants.
659 * 2) Exclusive (or batch) oplock around: If the requested access is a delete
660 * request, break if the oplock around is a batch oplock. If it's another
661 * requested access type, break.
663 * 3) Only level2 around: Grant level2 and do nothing else.
666 static bool delay_for_oplocks(struct share_mode_lock *lck,
667 files_struct *fsp,
668 uint16 mid,
669 int pass_number,
670 int oplock_request)
672 int i;
673 struct share_mode_entry *exclusive = NULL;
674 bool valid_entry = False;
675 bool delay_it = False;
676 bool have_level2 = False;
677 NTSTATUS status;
678 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
680 if (oplock_request & INTERNAL_OPEN_ONLY) {
681 fsp->oplock_type = NO_OPLOCK;
684 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
685 return False;
688 for (i=0; i<lck->num_share_modes; i++) {
690 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
691 continue;
694 /* At least one entry is not an invalid or deferred entry. */
695 valid_entry = True;
697 if (pass_number == 1) {
698 if (BATCH_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
699 SMB_ASSERT(exclusive == NULL);
700 exclusive = &lck->share_modes[i];
702 } else {
703 if (EXCLUSIVE_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
704 SMB_ASSERT(exclusive == NULL);
705 exclusive = &lck->share_modes[i];
709 if (lck->share_modes[i].op_type == LEVEL_II_OPLOCK) {
710 SMB_ASSERT(exclusive == NULL);
711 have_level2 = True;
715 if (!valid_entry) {
716 /* All entries are placeholders or deferred.
717 * Directly grant whatever the client wants. */
718 if (fsp->oplock_type == NO_OPLOCK) {
719 /* Store a level2 oplock, but don't tell the client */
720 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
722 return False;
725 if (exclusive != NULL) { /* Found an exclusive oplock */
726 SMB_ASSERT(!have_level2);
727 delay_it = is_delete_request(fsp) ?
728 BATCH_OPLOCK_TYPE(exclusive->op_type) : True;
731 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
732 /* We can at most grant level2 as there are other
733 * level2 or NO_OPLOCK entries. */
734 fsp->oplock_type = LEVEL_II_OPLOCK;
737 if ((fsp->oplock_type == NO_OPLOCK) && have_level2) {
738 /* Store a level2 oplock, but don't tell the client */
739 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
742 if (!delay_it) {
743 return False;
747 * Send a break message to the oplock holder and delay the open for
748 * our client.
751 DEBUG(10, ("Sending break request to PID %s\n",
752 procid_str_static(&exclusive->pid)));
753 exclusive->op_mid = mid;
755 /* Create the message. */
756 share_mode_entry_to_message(msg, exclusive);
758 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
759 don't want this set in the share mode struct pointed to by lck. */
761 if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
762 SSVAL(msg,6,exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
765 status = messaging_send_buf(smbd_messaging_context(), exclusive->pid,
766 MSG_SMB_BREAK_REQUEST,
767 (uint8 *)msg,
768 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
769 if (!NT_STATUS_IS_OK(status)) {
770 DEBUG(3, ("Could not send oplock break message: %s\n",
771 nt_errstr(status)));
774 return True;
777 static bool request_timed_out(struct timeval request_time,
778 struct timeval timeout)
780 struct timeval now, end_time;
781 GetTimeOfDay(&now);
782 end_time = timeval_sum(&request_time, &timeout);
783 return (timeval_compare(&end_time, &now) < 0);
786 /****************************************************************************
787 Handle the 1 second delay in returning a SHARING_VIOLATION error.
788 ****************************************************************************/
790 static void defer_open(struct share_mode_lock *lck,
791 struct timeval request_time,
792 struct timeval timeout,
793 struct smb_request *req,
794 struct deferred_open_record *state)
796 int i;
798 /* Paranoia check */
800 for (i=0; i<lck->num_share_modes; i++) {
801 struct share_mode_entry *e = &lck->share_modes[i];
803 if (!is_deferred_open_entry(e)) {
804 continue;
807 if (procid_is_me(&e->pid) && (e->op_mid == req->mid)) {
808 DEBUG(0, ("Trying to defer an already deferred "
809 "request: mid=%d, exiting\n", req->mid));
810 exit_server("attempt to defer a deferred request");
814 /* End paranoia check */
816 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
817 "open entry for mid %u\n",
818 (unsigned int)request_time.tv_sec,
819 (unsigned int)request_time.tv_usec,
820 (unsigned int)req->mid));
822 if (!push_deferred_smb_message(req, request_time, timeout,
823 (char *)state, sizeof(*state))) {
824 exit_server("push_deferred_smb_message failed");
826 add_deferred_open(lck, req->mid, request_time, state->id);
829 * Push the MID of this packet on the signing queue.
830 * We only do this once, the first time we push the packet
831 * onto the deferred open queue, as this has a side effect
832 * of incrementing the response sequence number.
835 srv_defer_sign_response(req->mid);
839 /****************************************************************************
840 On overwrite open ensure that the attributes match.
841 ****************************************************************************/
843 static bool open_match_attributes(connection_struct *conn,
844 const char *path,
845 uint32 old_dos_attr,
846 uint32 new_dos_attr,
847 mode_t existing_unx_mode,
848 mode_t new_unx_mode,
849 mode_t *returned_unx_mode)
851 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
853 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
854 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
856 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
857 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
858 *returned_unx_mode = new_unx_mode;
859 } else {
860 *returned_unx_mode = (mode_t)0;
863 DEBUG(10,("open_match_attributes: file %s old_dos_attr = 0x%x, "
864 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
865 "returned_unx_mode = 0%o\n",
866 path,
867 (unsigned int)old_dos_attr,
868 (unsigned int)existing_unx_mode,
869 (unsigned int)new_dos_attr,
870 (unsigned int)*returned_unx_mode ));
872 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
873 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
874 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
875 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
876 return False;
879 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
880 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
881 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
882 return False;
885 return True;
888 /****************************************************************************
889 Special FCB or DOS processing in the case of a sharing violation.
890 Try and find a duplicated file handle.
891 ****************************************************************************/
893 static files_struct *fcb_or_dos_open(connection_struct *conn,
894 const char *fname,
895 struct file_id id,
896 uint16 file_pid,
897 uint16 vuid,
898 uint32 access_mask,
899 uint32 share_access,
900 uint32 create_options)
902 files_struct *fsp;
903 files_struct *dup_fsp;
905 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
906 "file %s.\n", fname ));
908 for(fsp = file_find_di_first(id); fsp;
909 fsp = file_find_di_next(fsp)) {
911 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
912 "vuid = %u, file_pid = %u, private_options = 0x%x "
913 "access_mask = 0x%x\n", fsp->fsp_name,
914 fsp->fh->fd, (unsigned int)fsp->vuid,
915 (unsigned int)fsp->file_pid,
916 (unsigned int)fsp->fh->private_options,
917 (unsigned int)fsp->access_mask ));
919 if (fsp->fh->fd != -1 &&
920 fsp->vuid == vuid &&
921 fsp->file_pid == file_pid &&
922 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
923 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
924 (fsp->access_mask & FILE_WRITE_DATA) &&
925 strequal(fsp->fsp_name, fname)) {
926 DEBUG(10,("fcb_or_dos_open: file match\n"));
927 break;
931 if (!fsp) {
932 return NULL;
935 /* quite an insane set of semantics ... */
936 if (is_executable(fname) &&
937 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
938 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
939 return NULL;
942 /* We need to duplicate this fsp. */
943 if (!NT_STATUS_IS_OK(dup_file_fsp(fsp, access_mask, share_access,
944 create_options, &dup_fsp))) {
945 return NULL;
948 return dup_fsp;
951 /****************************************************************************
952 Open a file with a share mode - old openX method - map into NTCreate.
953 ****************************************************************************/
955 bool map_open_params_to_ntcreate(const char *fname, int deny_mode, int open_func,
956 uint32 *paccess_mask,
957 uint32 *pshare_mode,
958 uint32 *pcreate_disposition,
959 uint32 *pcreate_options)
961 uint32 access_mask;
962 uint32 share_mode;
963 uint32 create_disposition;
964 uint32 create_options = 0;
966 DEBUG(10,("map_open_params_to_ntcreate: fname = %s, deny_mode = 0x%x, "
967 "open_func = 0x%x\n",
968 fname, (unsigned int)deny_mode, (unsigned int)open_func ));
970 /* Create the NT compatible access_mask. */
971 switch (GET_OPENX_MODE(deny_mode)) {
972 case DOS_OPEN_EXEC: /* Implies read-only - used to be FILE_READ_DATA */
973 case DOS_OPEN_RDONLY:
974 access_mask = FILE_GENERIC_READ;
975 break;
976 case DOS_OPEN_WRONLY:
977 access_mask = FILE_GENERIC_WRITE;
978 break;
979 case DOS_OPEN_RDWR:
980 case DOS_OPEN_FCB:
981 access_mask = FILE_GENERIC_READ|FILE_GENERIC_WRITE;
982 break;
983 default:
984 DEBUG(10,("map_open_params_to_ntcreate: bad open mode = 0x%x\n",
985 (unsigned int)GET_OPENX_MODE(deny_mode)));
986 return False;
989 /* Create the NT compatible create_disposition. */
990 switch (open_func) {
991 case OPENX_FILE_EXISTS_FAIL|OPENX_FILE_CREATE_IF_NOT_EXIST:
992 create_disposition = FILE_CREATE;
993 break;
995 case OPENX_FILE_EXISTS_OPEN:
996 create_disposition = FILE_OPEN;
997 break;
999 case OPENX_FILE_EXISTS_OPEN|OPENX_FILE_CREATE_IF_NOT_EXIST:
1000 create_disposition = FILE_OPEN_IF;
1001 break;
1003 case OPENX_FILE_EXISTS_TRUNCATE:
1004 create_disposition = FILE_OVERWRITE;
1005 break;
1007 case OPENX_FILE_EXISTS_TRUNCATE|OPENX_FILE_CREATE_IF_NOT_EXIST:
1008 create_disposition = FILE_OVERWRITE_IF;
1009 break;
1011 default:
1012 /* From samba4 - to be confirmed. */
1013 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_EXEC) {
1014 create_disposition = FILE_CREATE;
1015 break;
1017 DEBUG(10,("map_open_params_to_ntcreate: bad "
1018 "open_func 0x%x\n", (unsigned int)open_func));
1019 return False;
1022 /* Create the NT compatible share modes. */
1023 switch (GET_DENY_MODE(deny_mode)) {
1024 case DENY_ALL:
1025 share_mode = FILE_SHARE_NONE;
1026 break;
1028 case DENY_WRITE:
1029 share_mode = FILE_SHARE_READ;
1030 break;
1032 case DENY_READ:
1033 share_mode = FILE_SHARE_WRITE;
1034 break;
1036 case DENY_NONE:
1037 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1038 break;
1040 case DENY_DOS:
1041 create_options |= NTCREATEX_OPTIONS_PRIVATE_DENY_DOS;
1042 if (is_executable(fname)) {
1043 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1044 } else {
1045 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_RDONLY) {
1046 share_mode = FILE_SHARE_READ;
1047 } else {
1048 share_mode = FILE_SHARE_NONE;
1051 break;
1053 case DENY_FCB:
1054 create_options |= NTCREATEX_OPTIONS_PRIVATE_DENY_FCB;
1055 share_mode = FILE_SHARE_NONE;
1056 break;
1058 default:
1059 DEBUG(10,("map_open_params_to_ntcreate: bad deny_mode 0x%x\n",
1060 (unsigned int)GET_DENY_MODE(deny_mode) ));
1061 return False;
1064 DEBUG(10,("map_open_params_to_ntcreate: file %s, access_mask = 0x%x, "
1065 "share_mode = 0x%x, create_disposition = 0x%x, "
1066 "create_options = 0x%x\n",
1067 fname,
1068 (unsigned int)access_mask,
1069 (unsigned int)share_mode,
1070 (unsigned int)create_disposition,
1071 (unsigned int)create_options ));
1073 if (paccess_mask) {
1074 *paccess_mask = access_mask;
1076 if (pshare_mode) {
1077 *pshare_mode = share_mode;
1079 if (pcreate_disposition) {
1080 *pcreate_disposition = create_disposition;
1082 if (pcreate_options) {
1083 *pcreate_options = create_options;
1086 return True;
1090 static void schedule_defer_open(struct share_mode_lock *lck,
1091 struct timeval request_time,
1092 struct smb_request *req)
1094 struct deferred_open_record state;
1096 /* This is a relative time, added to the absolute
1097 request_time value to get the absolute timeout time.
1098 Note that if this is the second or greater time we enter
1099 this codepath for this particular request mid then
1100 request_time is left as the absolute time of the *first*
1101 time this request mid was processed. This is what allows
1102 the request to eventually time out. */
1104 struct timeval timeout;
1106 /* Normally the smbd we asked should respond within
1107 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1108 * the client did, give twice the timeout as a safety
1109 * measure here in case the other smbd is stuck
1110 * somewhere else. */
1112 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1114 /* Nothing actually uses state.delayed_for_oplocks
1115 but it's handy to differentiate in debug messages
1116 between a 30 second delay due to oplock break, and
1117 a 1 second delay for share mode conflicts. */
1119 state.delayed_for_oplocks = True;
1120 state.id = lck->id;
1122 if (!request_timed_out(request_time, timeout)) {
1123 defer_open(lck, request_time, timeout, req, &state);
1127 /****************************************************************************
1128 Open a file with a share mode.
1129 ****************************************************************************/
1131 NTSTATUS open_file_ntcreate(connection_struct *conn,
1132 struct smb_request *req,
1133 const char *fname,
1134 SMB_STRUCT_STAT *psbuf,
1135 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1136 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1137 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1138 uint32 create_options, /* options such as delete on close. */
1139 uint32 new_dos_attributes, /* attributes used for new file. */
1140 int oplock_request, /* internal Samba oplock codes. */
1141 /* Information (FILE_EXISTS etc.) */
1142 int *pinfo,
1143 files_struct **result)
1145 int flags=0;
1146 int flags2=0;
1147 bool file_existed = VALID_STAT(*psbuf);
1148 bool def_acl = False;
1149 bool posix_open = False;
1150 bool new_file_created = False;
1151 struct file_id id;
1152 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1153 files_struct *fsp = NULL;
1154 mode_t new_unx_mode = (mode_t)0;
1155 mode_t unx_mode = (mode_t)0;
1156 int info;
1157 uint32 existing_dos_attributes = 0;
1158 struct pending_message_list *pml = NULL;
1159 struct timeval request_time = timeval_zero();
1160 struct share_mode_lock *lck = NULL;
1161 uint32 open_access_mask = access_mask;
1162 NTSTATUS status;
1163 int ret_flock;
1164 char *parent_dir;
1165 const char *newname;
1167 ZERO_STRUCT(id);
1169 if (conn->printer) {
1171 * Printers are handled completely differently.
1172 * Most of the passed parameters are ignored.
1175 if (pinfo) {
1176 *pinfo = FILE_WAS_CREATED;
1179 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n", fname));
1181 return print_fsp_open(conn, fname, req->vuid, result);
1184 if (!parent_dirname_talloc(talloc_tos(), fname, &parent_dir,
1185 &newname)) {
1186 return NT_STATUS_NO_MEMORY;
1189 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1190 posix_open = True;
1191 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1192 new_dos_attributes = 0;
1193 } else {
1194 /* We add aARCH to this as this mode is only used if the file is
1195 * created new. */
1196 unx_mode = unix_mode(conn, new_dos_attributes | aARCH, fname,
1197 parent_dir);
1200 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1201 "access_mask=0x%x share_access=0x%x "
1202 "create_disposition = 0x%x create_options=0x%x "
1203 "unix mode=0%o oplock_request=%d\n",
1204 fname, new_dos_attributes, access_mask, share_access,
1205 create_disposition, create_options, unx_mode,
1206 oplock_request));
1208 if ((access_mask & FILE_READ_DATA)||(access_mask & FILE_WRITE_DATA)) {
1209 DEBUG(10, ("open_file_ntcreate: adding FILE_READ_ATTRIBUTES "
1210 "to requested access_mask 0x%x, new mask 0x%x",
1211 access_mask,
1212 access_mask | FILE_READ_ATTRIBUTES ));
1214 access_mask |= FILE_READ_ATTRIBUTES;
1217 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
1218 DEBUG(0, ("No smb request but not an internal only open!\n"));
1219 return NT_STATUS_INTERNAL_ERROR;
1223 * Only non-internal opens can be deferred at all
1226 if ((req != NULL)
1227 && ((pml = get_open_deferred_message(req->mid)) != NULL)) {
1228 struct deferred_open_record *state =
1229 (struct deferred_open_record *)pml->private_data.data;
1231 /* Remember the absolute time of the original
1232 request with this mid. We'll use it later to
1233 see if this has timed out. */
1235 request_time = pml->request_time;
1237 /* Remove the deferred open entry under lock. */
1238 lck = get_share_mode_lock(talloc_tos(), state->id, NULL, NULL,
1239 NULL);
1240 if (lck == NULL) {
1241 DEBUG(0, ("could not get share mode lock\n"));
1242 } else {
1243 del_deferred_open_entry(lck, req->mid);
1244 TALLOC_FREE(lck);
1247 /* Ensure we don't reprocess this message. */
1248 remove_deferred_open_smb_message(req->mid);
1251 status = check_name(conn, fname);
1252 if (!NT_STATUS_IS_OK(status)) {
1253 return status;
1256 if (!posix_open) {
1257 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
1258 if (file_existed) {
1259 existing_dos_attributes = dos_mode(conn, fname, psbuf);
1263 /* ignore any oplock requests if oplocks are disabled */
1264 if (!lp_oplocks(SNUM(conn)) || global_client_failed_oplock_break ||
1265 IS_VETO_OPLOCK_PATH(conn, fname)) {
1266 /* Mask off everything except the private Samba bits. */
1267 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
1270 /* this is for OS/2 long file names - say we don't support them */
1271 if (!lp_posix_pathnames() && strstr(fname,".+,;=[].")) {
1272 /* OS/2 Workplace shell fix may be main code stream in a later
1273 * release. */
1274 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1275 "supported.\n"));
1276 if (use_nt_status()) {
1277 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1279 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
1282 switch( create_disposition ) {
1284 * Currently we're using FILE_SUPERSEDE as the same as
1285 * FILE_OVERWRITE_IF but they really are
1286 * different. FILE_SUPERSEDE deletes an existing file
1287 * (requiring delete access) then recreates it.
1289 case FILE_SUPERSEDE:
1290 /* If file exists replace/overwrite. If file doesn't
1291 * exist create. */
1292 flags2 |= (O_CREAT | O_TRUNC);
1293 break;
1295 case FILE_OVERWRITE_IF:
1296 /* If file exists replace/overwrite. If file doesn't
1297 * exist create. */
1298 flags2 |= (O_CREAT | O_TRUNC);
1299 break;
1301 case FILE_OPEN:
1302 /* If file exists open. If file doesn't exist error. */
1303 if (!file_existed) {
1304 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1305 "requested for file %s and file "
1306 "doesn't exist.\n", fname ));
1307 errno = ENOENT;
1308 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1310 break;
1312 case FILE_OVERWRITE:
1313 /* If file exists overwrite. If file doesn't exist
1314 * error. */
1315 if (!file_existed) {
1316 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1317 "requested for file %s and file "
1318 "doesn't exist.\n", fname ));
1319 errno = ENOENT;
1320 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1322 flags2 |= O_TRUNC;
1323 break;
1325 case FILE_CREATE:
1326 /* If file exists error. If file doesn't exist
1327 * create. */
1328 if (file_existed) {
1329 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1330 "requested for file %s and file "
1331 "already exists.\n", fname ));
1332 if (S_ISDIR(psbuf->st_mode)) {
1333 errno = EISDIR;
1334 } else {
1335 errno = EEXIST;
1337 return map_nt_error_from_unix(errno);
1339 flags2 |= (O_CREAT|O_EXCL);
1340 break;
1342 case FILE_OPEN_IF:
1343 /* If file exists open. If file doesn't exist
1344 * create. */
1345 flags2 |= O_CREAT;
1346 break;
1348 default:
1349 return NT_STATUS_INVALID_PARAMETER;
1352 /* We only care about matching attributes on file exists and
1353 * overwrite. */
1355 if (!posix_open && file_existed && ((create_disposition == FILE_OVERWRITE) ||
1356 (create_disposition == FILE_OVERWRITE_IF))) {
1357 if (!open_match_attributes(conn, fname,
1358 existing_dos_attributes,
1359 new_dos_attributes, psbuf->st_mode,
1360 unx_mode, &new_unx_mode)) {
1361 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1362 "for file %s (%x %x) (0%o, 0%o)\n",
1363 fname, existing_dos_attributes,
1364 new_dos_attributes,
1365 (unsigned int)psbuf->st_mode,
1366 (unsigned int)unx_mode ));
1367 errno = EACCES;
1368 return NT_STATUS_ACCESS_DENIED;
1373 * Convert GENERIC bits to specific bits.
1376 se_map_generic(&access_mask, &file_generic_mapping);
1378 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1379 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1380 if (file_existed) {
1381 struct security_descriptor *sd;
1382 uint32_t access_granted = 0;
1384 status = SMB_VFS_GET_NT_ACL(conn, fname,
1385 (OWNER_SECURITY_INFORMATION |
1386 GROUP_SECURITY_INFORMATION |
1387 DACL_SECURITY_INFORMATION),&sd);
1389 if (!NT_STATUS_IS_OK(status)) {
1390 DEBUG(10, ("open_file_ntcreate: Could not get acl "
1391 "on file %s: %s\n",
1392 fname,
1393 nt_errstr(status)));
1394 return NT_STATUS_ACCESS_DENIED;
1397 status = se_access_check(sd, conn->server_info->ptok,
1398 access_mask, &access_granted);
1400 TALLOC_FREE(sd);
1402 if (!NT_STATUS_IS_OK(status)) {
1403 DEBUG(10, ("open_file_ntcreate: Access denied on "
1404 "file %s: when calculating maximum access\n",
1405 fname));
1406 return NT_STATUS_ACCESS_DENIED;
1409 access_mask = access_granted;
1411 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
1413 access_mask |= FILE_READ_ATTRIBUTES;
1414 } else {
1415 access_mask = FILE_GENERIC_ALL;
1419 open_access_mask = access_mask;
1421 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1422 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
1425 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1426 "access_mask=0x%x\n", fname, access_mask ));
1429 * Note that we ignore the append flag as append does not
1430 * mean the same thing under DOS and Unix.
1433 if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
1434 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1435 /* DENY_DOS opens are always underlying read-write on the
1436 file handle, no matter what the requested access mask
1437 says. */
1438 if ((create_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
1439 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|FILE_READ_EA|FILE_EXECUTE)) {
1440 flags = O_RDWR;
1441 } else {
1442 flags = O_WRONLY;
1444 } else {
1445 flags = O_RDONLY;
1449 * Currently we only look at FILE_WRITE_THROUGH for create options.
1452 #if defined(O_SYNC)
1453 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
1454 flags2 |= O_SYNC;
1456 #endif /* O_SYNC */
1458 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
1459 flags2 |= O_APPEND;
1462 if (!posix_open && !CAN_WRITE(conn)) {
1464 * We should really return a permission denied error if either
1465 * O_CREAT or O_TRUNC are set, but for compatibility with
1466 * older versions of Samba we just AND them out.
1468 flags2 &= ~(O_CREAT|O_TRUNC);
1472 * Ensure we can't write on a read-only share or file.
1475 if (flags != O_RDONLY && file_existed &&
1476 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
1477 DEBUG(5,("open_file_ntcreate: write access requested for "
1478 "file %s on read only %s\n",
1479 fname, !CAN_WRITE(conn) ? "share" : "file" ));
1480 errno = EACCES;
1481 return NT_STATUS_ACCESS_DENIED;
1484 status = file_new(conn, &fsp);
1485 if(!NT_STATUS_IS_OK(status)) {
1486 return status;
1489 fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
1490 fsp->share_access = share_access;
1491 fsp->fh->private_options = create_options;
1492 fsp->access_mask = open_access_mask; /* We change this to the
1493 * requested access_mask after
1494 * the open is done. */
1495 fsp->posix_open = posix_open;
1497 /* Ensure no SAMBA_PRIVATE bits can be set. */
1498 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1500 if (timeval_is_zero(&request_time)) {
1501 request_time = fsp->open_time;
1504 if (file_existed) {
1505 struct timespec old_write_time = get_mtimespec(psbuf);
1506 id = vfs_file_id_from_sbuf(conn, psbuf);
1508 lck = get_share_mode_lock(talloc_tos(), id,
1509 conn->connectpath,
1510 fname, &old_write_time);
1512 if (lck == NULL) {
1513 file_free(fsp);
1514 DEBUG(0, ("Could not get share mode lock\n"));
1515 return NT_STATUS_SHARING_VIOLATION;
1518 /* First pass - send break only on batch oplocks. */
1519 if ((req != NULL)
1520 && delay_for_oplocks(lck, fsp, req->mid, 1,
1521 oplock_request)) {
1522 schedule_defer_open(lck, request_time, req);
1523 TALLOC_FREE(lck);
1524 file_free(fsp);
1525 return NT_STATUS_SHARING_VIOLATION;
1528 /* Use the client requested access mask here, not the one we
1529 * open with. */
1530 status = open_mode_check(conn, fname, lck,
1531 access_mask, share_access,
1532 create_options, &file_existed);
1534 if (NT_STATUS_IS_OK(status)) {
1535 /* We might be going to allow this open. Check oplock
1536 * status again. */
1537 /* Second pass - send break for both batch or
1538 * exclusive oplocks. */
1539 if ((req != NULL)
1540 && delay_for_oplocks(lck, fsp, req->mid, 2,
1541 oplock_request)) {
1542 schedule_defer_open(lck, request_time, req);
1543 TALLOC_FREE(lck);
1544 file_free(fsp);
1545 return NT_STATUS_SHARING_VIOLATION;
1549 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
1550 /* DELETE_PENDING is not deferred for a second */
1551 TALLOC_FREE(lck);
1552 file_free(fsp);
1553 return status;
1556 if (!NT_STATUS_IS_OK(status)) {
1557 uint32 can_access_mask;
1558 bool can_access = True;
1560 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
1562 /* Check if this can be done with the deny_dos and fcb
1563 * calls. */
1564 if (create_options &
1565 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
1566 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
1567 files_struct *fsp_dup;
1569 if (req == NULL) {
1570 DEBUG(0, ("DOS open without an SMB "
1571 "request!\n"));
1572 TALLOC_FREE(lck);
1573 file_free(fsp);
1574 return NT_STATUS_INTERNAL_ERROR;
1577 /* Use the client requested access mask here,
1578 * not the one we open with. */
1579 fsp_dup = fcb_or_dos_open(conn, fname, id,
1580 req->smbpid,
1581 req->vuid,
1582 access_mask,
1583 share_access,
1584 create_options);
1586 if (fsp_dup) {
1587 TALLOC_FREE(lck);
1588 file_free(fsp);
1589 if (pinfo) {
1590 *pinfo = FILE_WAS_OPENED;
1592 conn->num_files_open++;
1593 *result = fsp_dup;
1594 return NT_STATUS_OK;
1599 * This next line is a subtlety we need for
1600 * MS-Access. If a file open will fail due to share
1601 * permissions and also for security (access) reasons,
1602 * we need to return the access failed error, not the
1603 * share error. We can't open the file due to kernel
1604 * oplock deadlock (it's possible we failed above on
1605 * the open_mode_check()) so use a userspace check.
1608 if (flags & O_RDWR) {
1609 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
1610 } else if (flags & O_WRONLY) {
1611 can_access_mask = FILE_WRITE_DATA;
1612 } else {
1613 can_access_mask = FILE_READ_DATA;
1616 if (((can_access_mask & FILE_WRITE_DATA) && !CAN_WRITE(conn)) ||
1617 !can_access_file_data(conn,fname,psbuf,can_access_mask)) {
1618 can_access = False;
1622 * If we're returning a share violation, ensure we
1623 * cope with the braindead 1 second delay.
1626 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
1627 lp_defer_sharing_violations()) {
1628 struct timeval timeout;
1629 struct deferred_open_record state;
1630 int timeout_usecs;
1632 /* this is a hack to speed up torture tests
1633 in 'make test' */
1634 timeout_usecs = lp_parm_int(SNUM(conn),
1635 "smbd","sharedelay",
1636 SHARING_VIOLATION_USEC_WAIT);
1638 /* This is a relative time, added to the absolute
1639 request_time value to get the absolute timeout time.
1640 Note that if this is the second or greater time we enter
1641 this codepath for this particular request mid then
1642 request_time is left as the absolute time of the *first*
1643 time this request mid was processed. This is what allows
1644 the request to eventually time out. */
1646 timeout = timeval_set(0, timeout_usecs);
1648 /* Nothing actually uses state.delayed_for_oplocks
1649 but it's handy to differentiate in debug messages
1650 between a 30 second delay due to oplock break, and
1651 a 1 second delay for share mode conflicts. */
1653 state.delayed_for_oplocks = False;
1654 state.id = id;
1656 if ((req != NULL)
1657 && !request_timed_out(request_time,
1658 timeout)) {
1659 defer_open(lck, request_time, timeout,
1660 req, &state);
1664 TALLOC_FREE(lck);
1665 if (can_access) {
1667 * We have detected a sharing violation here
1668 * so return the correct error code
1670 status = NT_STATUS_SHARING_VIOLATION;
1671 } else {
1672 status = NT_STATUS_ACCESS_DENIED;
1674 file_free(fsp);
1675 return status;
1679 * We exit this block with the share entry *locked*.....
1683 SMB_ASSERT(!file_existed || (lck != NULL));
1686 * Ensure we pay attention to default ACLs on directories if required.
1689 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
1690 (def_acl = directory_has_default_acl(conn, parent_dir))) {
1691 unx_mode = 0777;
1694 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
1695 "access_mask = 0x%x, open_access_mask = 0x%x\n",
1696 (unsigned int)flags, (unsigned int)flags2,
1697 (unsigned int)unx_mode, (unsigned int)access_mask,
1698 (unsigned int)open_access_mask));
1701 * open_file strips any O_TRUNC flags itself.
1704 fsp_open = open_file(fsp, conn, req, parent_dir, newname, fname, psbuf,
1705 flags|flags2, unx_mode, access_mask,
1706 open_access_mask);
1708 if (!NT_STATUS_IS_OK(fsp_open)) {
1709 if (lck != NULL) {
1710 TALLOC_FREE(lck);
1712 file_free(fsp);
1713 return fsp_open;
1716 if (!file_existed) {
1717 struct timespec old_write_time = get_mtimespec(psbuf);
1719 * Deal with the race condition where two smbd's detect the
1720 * file doesn't exist and do the create at the same time. One
1721 * of them will win and set a share mode, the other (ie. this
1722 * one) should check if the requested share mode for this
1723 * create is allowed.
1727 * Now the file exists and fsp is successfully opened,
1728 * fsp->dev and fsp->inode are valid and should replace the
1729 * dev=0,inode=0 from a non existent file. Spotted by
1730 * Nadav Danieli <nadavd@exanet.com>. JRA.
1733 id = fsp->file_id;
1735 lck = get_share_mode_lock(talloc_tos(), id,
1736 conn->connectpath,
1737 fname, &old_write_time);
1739 if (lck == NULL) {
1740 DEBUG(0, ("open_file_ntcreate: Could not get share "
1741 "mode lock for %s\n", fname));
1742 fd_close(fsp);
1743 file_free(fsp);
1744 return NT_STATUS_SHARING_VIOLATION;
1747 /* First pass - send break only on batch oplocks. */
1748 if ((req != NULL)
1749 && delay_for_oplocks(lck, fsp, req->mid, 1,
1750 oplock_request)) {
1751 schedule_defer_open(lck, request_time, req);
1752 TALLOC_FREE(lck);
1753 fd_close(fsp);
1754 file_free(fsp);
1755 return NT_STATUS_SHARING_VIOLATION;
1758 status = open_mode_check(conn, fname, lck,
1759 access_mask, share_access,
1760 create_options, &file_existed);
1762 if (NT_STATUS_IS_OK(status)) {
1763 /* We might be going to allow this open. Check oplock
1764 * status again. */
1765 /* Second pass - send break for both batch or
1766 * exclusive oplocks. */
1767 if ((req != NULL)
1768 && delay_for_oplocks(lck, fsp, req->mid, 2,
1769 oplock_request)) {
1770 schedule_defer_open(lck, request_time, req);
1771 TALLOC_FREE(lck);
1772 fd_close(fsp);
1773 file_free(fsp);
1774 return NT_STATUS_SHARING_VIOLATION;
1778 if (!NT_STATUS_IS_OK(status)) {
1779 struct deferred_open_record state;
1781 fd_close(fsp);
1782 file_free(fsp);
1784 state.delayed_for_oplocks = False;
1785 state.id = id;
1787 /* Do it all over again immediately. In the second
1788 * round we will find that the file existed and handle
1789 * the DELETE_PENDING and FCB cases correctly. No need
1790 * to duplicate the code here. Essentially this is a
1791 * "goto top of this function", but don't tell
1792 * anybody... */
1794 if (req != NULL) {
1795 defer_open(lck, request_time, timeval_zero(),
1796 req, &state);
1798 TALLOC_FREE(lck);
1799 return status;
1803 * We exit this block with the share entry *locked*.....
1808 SMB_ASSERT(lck != NULL);
1810 /* note that we ignore failure for the following. It is
1811 basically a hack for NFS, and NFS will never set one of
1812 these only read them. Nobody but Samba can ever set a deny
1813 mode and we have already checked our more authoritative
1814 locking database for permission to set this deny mode. If
1815 the kernel refuses the operations then the kernel is wrong.
1816 note that GPFS supports it as well - jmcd */
1818 if (fsp->fh->fd != -1) {
1819 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access);
1820 if(ret_flock == -1 ){
1822 TALLOC_FREE(lck);
1823 fd_close(fsp);
1824 file_free(fsp);
1826 return NT_STATUS_SHARING_VIOLATION;
1831 * At this point onwards, we can guarentee that the share entry
1832 * is locked, whether we created the file or not, and that the
1833 * deny mode is compatible with all current opens.
1837 * If requested, truncate the file.
1840 if (flags2&O_TRUNC) {
1842 * We are modifing the file after open - update the stat
1843 * struct..
1845 if ((SMB_VFS_FTRUNCATE(fsp, 0) == -1) ||
1846 (SMB_VFS_FSTAT(fsp, psbuf)==-1)) {
1847 status = map_nt_error_from_unix(errno);
1848 TALLOC_FREE(lck);
1849 fd_close(fsp);
1850 file_free(fsp);
1851 return status;
1855 /* Record the options we were opened with. */
1856 fsp->share_access = share_access;
1857 fsp->fh->private_options = create_options;
1858 fsp->access_mask = access_mask;
1860 if (file_existed) {
1861 /* stat opens on existing files don't get oplocks. */
1862 if (is_stat_open(open_access_mask)) {
1863 fsp->oplock_type = NO_OPLOCK;
1866 if (!(flags2 & O_TRUNC)) {
1867 info = FILE_WAS_OPENED;
1868 } else {
1869 info = FILE_WAS_OVERWRITTEN;
1871 } else {
1872 info = FILE_WAS_CREATED;
1875 if (pinfo) {
1876 *pinfo = info;
1880 * Setup the oplock info in both the shared memory and
1881 * file structs.
1884 if ((fsp->oplock_type != NO_OPLOCK) &&
1885 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK)) {
1886 if (!set_file_oplock(fsp, fsp->oplock_type)) {
1887 /* Could not get the kernel oplock */
1888 fsp->oplock_type = NO_OPLOCK;
1892 if (info == FILE_WAS_OVERWRITTEN || info == FILE_WAS_CREATED || info == FILE_WAS_SUPERSEDED) {
1893 new_file_created = True;
1896 set_share_mode(lck, fsp, conn->server_info->utok.uid, 0,
1897 fsp->oplock_type, new_file_created);
1899 /* Handle strange delete on close create semantics. */
1900 if ((create_options & FILE_DELETE_ON_CLOSE)
1901 && (((conn->fs_capabilities & FILE_NAMED_STREAMS)
1902 && is_ntfs_stream_name(fname))
1903 || can_set_initial_delete_on_close(lck))) {
1904 status = can_set_delete_on_close(fsp, True, new_dos_attributes);
1906 if (!NT_STATUS_IS_OK(status)) {
1907 /* Remember to delete the mode we just added. */
1908 del_share_mode(lck, fsp);
1909 TALLOC_FREE(lck);
1910 fd_close(fsp);
1911 file_free(fsp);
1912 return status;
1914 /* Note that here we set the *inital* delete on close flag,
1915 not the regular one. The magic gets handled in close. */
1916 fsp->initial_delete_on_close = True;
1919 if (new_file_created) {
1920 /* Files should be initially set as archive */
1921 if (lp_map_archive(SNUM(conn)) ||
1922 lp_store_dos_attributes(SNUM(conn))) {
1923 if (!posix_open) {
1924 SMB_STRUCT_STAT tmp_sbuf;
1925 SET_STAT_INVALID(tmp_sbuf);
1926 if (file_set_dosmode(
1927 conn, fname,
1928 new_dos_attributes | aARCH,
1929 &tmp_sbuf, parent_dir,
1930 true) == 0) {
1931 unx_mode = tmp_sbuf.st_mode;
1938 * Take care of inherited ACLs on created files - if default ACL not
1939 * selected.
1942 if (!posix_open && !file_existed && !def_acl) {
1944 int saved_errno = errno; /* We might get ENOSYS in the next
1945 * call.. */
1947 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
1948 errno == ENOSYS) {
1949 errno = saved_errno; /* Ignore ENOSYS */
1952 } else if (new_unx_mode) {
1954 int ret = -1;
1956 /* Attributes need changing. File already existed. */
1959 int saved_errno = errno; /* We might get ENOSYS in the
1960 * next call.. */
1961 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
1963 if (ret == -1 && errno == ENOSYS) {
1964 errno = saved_errno; /* Ignore ENOSYS */
1965 } else {
1966 DEBUG(5, ("open_file_ntcreate: reset "
1967 "attributes of file %s to 0%o\n",
1968 fname, (unsigned int)new_unx_mode));
1969 ret = 0; /* Don't do the fchmod below. */
1973 if ((ret == -1) &&
1974 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
1975 DEBUG(5, ("open_file_ntcreate: failed to reset "
1976 "attributes of file %s to 0%o\n",
1977 fname, (unsigned int)new_unx_mode));
1980 /* If this is a successful open, we must remove any deferred open
1981 * records. */
1982 if (req != NULL) {
1983 del_deferred_open_entry(lck, req->mid);
1985 TALLOC_FREE(lck);
1987 conn->num_files_open++;
1989 *result = fsp;
1990 return NT_STATUS_OK;
1993 /****************************************************************************
1994 Open a file for for write to ensure that we can fchmod it.
1995 ****************************************************************************/
1997 NTSTATUS open_file_fchmod(connection_struct *conn, const char *fname,
1998 SMB_STRUCT_STAT *psbuf, files_struct **result)
2000 files_struct *fsp = NULL;
2001 NTSTATUS status;
2003 if (!VALID_STAT(*psbuf)) {
2004 return NT_STATUS_INVALID_PARAMETER;
2007 status = file_new(conn, &fsp);
2008 if(!NT_STATUS_IS_OK(status)) {
2009 return status;
2012 /* note! we must use a non-zero desired access or we don't get
2013 a real file descriptor. Oh what a twisted web we weave. */
2014 status = open_file(fsp, conn, NULL, NULL, NULL, fname, psbuf, O_WRONLY,
2015 0, FILE_WRITE_DATA, FILE_WRITE_DATA);
2018 * This is not a user visible file open.
2019 * Don't set a share mode and don't increment
2020 * the conn->num_files_open.
2023 if (!NT_STATUS_IS_OK(status)) {
2024 file_free(fsp);
2025 return status;
2028 *result = fsp;
2029 return NT_STATUS_OK;
2032 /****************************************************************************
2033 Close the fchmod file fd - ensure no locks are lost.
2034 ****************************************************************************/
2036 NTSTATUS close_file_fchmod(files_struct *fsp)
2038 NTSTATUS status = fd_close(fsp);
2039 file_free(fsp);
2040 return status;
2043 static NTSTATUS mkdir_internal(connection_struct *conn,
2044 const char *name,
2045 uint32 file_attributes,
2046 SMB_STRUCT_STAT *psbuf)
2048 mode_t mode;
2049 char *parent_dir;
2050 const char *dirname;
2051 NTSTATUS status;
2052 bool posix_open = false;
2054 if(!CAN_WRITE(conn)) {
2055 DEBUG(5,("mkdir_internal: failing create on read-only share "
2056 "%s\n", lp_servicename(SNUM(conn))));
2057 return NT_STATUS_ACCESS_DENIED;
2060 status = check_name(conn, name);
2061 if (!NT_STATUS_IS_OK(status)) {
2062 return status;
2065 if (!parent_dirname_talloc(talloc_tos(), name, &parent_dir,
2066 &dirname)) {
2067 return NT_STATUS_NO_MEMORY;
2070 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2071 posix_open = true;
2072 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2073 } else {
2074 mode = unix_mode(conn, aDIR, name, parent_dir);
2077 if (SMB_VFS_MKDIR(conn, name, mode) != 0) {
2078 return map_nt_error_from_unix(errno);
2081 /* Ensure we're checking for a symlink here.... */
2082 /* We don't want to get caught by a symlink racer. */
2084 if (SMB_VFS_LSTAT(conn, name, psbuf) == -1) {
2085 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2086 name, strerror(errno)));
2087 return map_nt_error_from_unix(errno);
2090 if (!S_ISDIR(psbuf->st_mode)) {
2091 DEBUG(0, ("Directory just '%s' created is not a directory\n",
2092 name));
2093 return NT_STATUS_ACCESS_DENIED;
2096 if (lp_store_dos_attributes(SNUM(conn))) {
2097 if (!posix_open) {
2098 file_set_dosmode(conn, name,
2099 file_attributes | aDIR, NULL,
2100 parent_dir,
2101 true);
2105 if (lp_inherit_perms(SNUM(conn))) {
2106 inherit_access_posix_acl(conn, parent_dir, name, mode);
2109 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
2111 * Check if high bits should have been set,
2112 * then (if bits are missing): add them.
2113 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2114 * dir.
2116 if (mode & ~(S_IRWXU|S_IRWXG|S_IRWXO) && (mode & ~psbuf->st_mode)) {
2117 SMB_VFS_CHMOD(conn, name,
2118 psbuf->st_mode | (mode & ~psbuf->st_mode));
2122 /* Change the owner if required. */
2123 if (lp_inherit_owner(SNUM(conn))) {
2124 change_dir_owner_to_parent(conn, parent_dir, name, psbuf);
2127 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2128 name);
2130 return NT_STATUS_OK;
2133 /****************************************************************************
2134 Open a directory from an NT SMB call.
2135 ****************************************************************************/
2137 NTSTATUS open_directory(connection_struct *conn,
2138 struct smb_request *req,
2139 const char *fname,
2140 SMB_STRUCT_STAT *psbuf,
2141 uint32 access_mask,
2142 uint32 share_access,
2143 uint32 create_disposition,
2144 uint32 create_options,
2145 uint32 file_attributes,
2146 int *pinfo,
2147 files_struct **result)
2149 files_struct *fsp = NULL;
2150 bool dir_existed = VALID_STAT(*psbuf) ? True : False;
2151 struct share_mode_lock *lck = NULL;
2152 NTSTATUS status;
2153 struct timespec mtimespec;
2154 int info = 0;
2156 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2157 "share_access = 0x%x create_options = 0x%x, "
2158 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2159 fname,
2160 (unsigned int)access_mask,
2161 (unsigned int)share_access,
2162 (unsigned int)create_options,
2163 (unsigned int)create_disposition,
2164 (unsigned int)file_attributes));
2166 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
2167 (conn->fs_capabilities & FILE_NAMED_STREAMS) &&
2168 is_ntfs_stream_name(fname)) {
2169 DEBUG(2, ("open_directory: %s is a stream name!\n", fname));
2170 return NT_STATUS_NOT_A_DIRECTORY;
2173 switch( create_disposition ) {
2174 case FILE_OPEN:
2176 info = FILE_WAS_OPENED;
2179 * We want to follow symlinks here.
2182 if (SMB_VFS_STAT(conn, fname, psbuf) != 0) {
2183 return map_nt_error_from_unix(errno);
2186 break;
2188 case FILE_CREATE:
2190 /* If directory exists error. If directory doesn't
2191 * exist create. */
2193 status = mkdir_internal(conn,
2194 fname,
2195 file_attributes,
2196 psbuf);
2198 if (!NT_STATUS_IS_OK(status)) {
2199 DEBUG(2, ("open_directory: unable to create "
2200 "%s. Error was %s\n", fname,
2201 nt_errstr(status)));
2202 return status;
2205 info = FILE_WAS_CREATED;
2206 break;
2208 case FILE_OPEN_IF:
2210 * If directory exists open. If directory doesn't
2211 * exist create.
2214 status = mkdir_internal(conn,
2215 fname,
2216 file_attributes,
2217 psbuf);
2219 if (NT_STATUS_IS_OK(status)) {
2220 info = FILE_WAS_CREATED;
2223 if (NT_STATUS_EQUAL(status,
2224 NT_STATUS_OBJECT_NAME_COLLISION)) {
2225 info = FILE_WAS_OPENED;
2226 status = NT_STATUS_OK;
2229 break;
2231 case FILE_SUPERSEDE:
2232 case FILE_OVERWRITE:
2233 case FILE_OVERWRITE_IF:
2234 default:
2235 DEBUG(5,("open_directory: invalid create_disposition "
2236 "0x%x for directory %s\n",
2237 (unsigned int)create_disposition, fname));
2238 return NT_STATUS_INVALID_PARAMETER;
2241 if(!S_ISDIR(psbuf->st_mode)) {
2242 DEBUG(5,("open_directory: %s is not a directory !\n",
2243 fname ));
2244 return NT_STATUS_NOT_A_DIRECTORY;
2247 status = file_new(conn, &fsp);
2248 if(!NT_STATUS_IS_OK(status)) {
2249 return status;
2253 * Setup the files_struct for it.
2256 fsp->mode = psbuf->st_mode;
2257 fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
2258 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
2259 fsp->file_pid = req ? req->smbpid : 0;
2260 fsp->can_lock = False;
2261 fsp->can_read = False;
2262 fsp->can_write = False;
2264 fsp->share_access = share_access;
2265 fsp->fh->private_options = create_options;
2266 fsp->access_mask = access_mask;
2268 fsp->print_file = False;
2269 fsp->modified = False;
2270 fsp->oplock_type = NO_OPLOCK;
2271 fsp->sent_oplock_break = NO_BREAK_SENT;
2272 fsp->is_directory = True;
2273 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
2275 string_set(&fsp->fsp_name,fname);
2277 mtimespec = get_mtimespec(psbuf);
2279 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
2280 conn->connectpath,
2281 fname, &mtimespec);
2283 if (lck == NULL) {
2284 DEBUG(0, ("open_directory: Could not get share mode lock for %s\n", fname));
2285 file_free(fsp);
2286 return NT_STATUS_SHARING_VIOLATION;
2289 status = open_mode_check(conn, fname, lck,
2290 access_mask, share_access,
2291 create_options, &dir_existed);
2293 if (!NT_STATUS_IS_OK(status)) {
2294 TALLOC_FREE(lck);
2295 file_free(fsp);
2296 return status;
2299 set_share_mode(lck, fsp, conn->server_info->utok.uid, 0, NO_OPLOCK,
2300 True);
2302 /* For directories the delete on close bit at open time seems
2303 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2304 if (create_options & FILE_DELETE_ON_CLOSE) {
2305 status = can_set_delete_on_close(fsp, True, 0);
2306 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
2307 TALLOC_FREE(lck);
2308 file_free(fsp);
2309 return status;
2312 if (NT_STATUS_IS_OK(status)) {
2313 /* Note that here we set the *inital* delete on close flag,
2314 not the regular one. The magic gets handled in close. */
2315 fsp->initial_delete_on_close = True;
2319 TALLOC_FREE(lck);
2321 if (pinfo) {
2322 *pinfo = info;
2325 conn->num_files_open++;
2327 *result = fsp;
2328 return NT_STATUS_OK;
2331 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req, const char *directory)
2333 NTSTATUS status;
2334 SMB_STRUCT_STAT sbuf;
2335 files_struct *fsp;
2337 SET_STAT_INVALID(sbuf);
2339 status = open_directory(conn, req, directory, &sbuf,
2340 FILE_READ_ATTRIBUTES, /* Just a stat open */
2341 FILE_SHARE_NONE, /* Ignored for stat opens */
2342 FILE_CREATE,
2344 FILE_ATTRIBUTE_DIRECTORY,
2345 NULL,
2346 &fsp);
2348 if (NT_STATUS_IS_OK(status)) {
2349 close_file(fsp, NORMAL_CLOSE);
2352 return status;
2355 /****************************************************************************
2356 Receive notification that one of our open files has been renamed by another
2357 smbd process.
2358 ****************************************************************************/
2360 void msg_file_was_renamed(struct messaging_context *msg,
2361 void *private_data,
2362 uint32_t msg_type,
2363 struct server_id server_id,
2364 DATA_BLOB *data)
2366 files_struct *fsp;
2367 char *frm = (char *)data->data;
2368 struct file_id id;
2369 const char *sharepath;
2370 const char *newname;
2371 size_t sp_len;
2373 if (data->data == NULL
2374 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
2375 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
2376 (int)data->length));
2377 return;
2380 /* Unpack the message. */
2381 pull_file_id_16(frm, &id);
2382 sharepath = &frm[16];
2383 newname = sharepath + strlen(sharepath) + 1;
2384 sp_len = strlen(sharepath);
2386 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
2387 "file_id %s\n",
2388 sharepath, newname, file_id_string_tos(&id)));
2390 for(fsp = file_find_di_first(id); fsp; fsp = file_find_di_next(fsp)) {
2391 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
2392 DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n",
2393 fsp->fnum, fsp->fsp_name, newname ));
2394 string_set(&fsp->fsp_name, newname);
2395 } else {
2396 /* TODO. JRA. */
2397 /* Now we have the complete path we can work out if this is
2398 actually within this share and adjust newname accordingly. */
2399 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
2400 "not sharepath %s) "
2401 "fnum %d from %s -> %s\n",
2402 fsp->conn->connectpath,
2403 sharepath,
2404 fsp->fnum,
2405 fsp->fsp_name,
2406 newname ));
2411 struct case_semantics_state {
2412 connection_struct *conn;
2413 bool case_sensitive;
2414 bool case_preserve;
2415 bool short_case_preserve;
2418 /****************************************************************************
2419 Restore case semantics.
2420 ****************************************************************************/
2421 static int restore_case_semantics(struct case_semantics_state *state)
2423 state->conn->case_sensitive = state->case_sensitive;
2424 state->conn->case_preserve = state->case_preserve;
2425 state->conn->short_case_preserve = state->short_case_preserve;
2426 return 0;
2429 /****************************************************************************
2430 Save case semantics.
2431 ****************************************************************************/
2432 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
2433 connection_struct *conn)
2435 struct case_semantics_state *result;
2437 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
2438 DEBUG(0, ("talloc failed\n"));
2439 return NULL;
2442 result->conn = conn;
2443 result->case_sensitive = conn->case_sensitive;
2444 result->case_preserve = conn->case_preserve;
2445 result->short_case_preserve = conn->short_case_preserve;
2447 /* Set to POSIX. */
2448 conn->case_sensitive = True;
2449 conn->case_preserve = True;
2450 conn->short_case_preserve = True;
2452 talloc_set_destructor(result, restore_case_semantics);
2454 return result;
2458 * If a main file is opened for delete, all streams need to be checked for
2459 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
2460 * If that works, delete them all by setting the delete on close and close.
2463 static NTSTATUS open_streams_for_delete(connection_struct *conn,
2464 const char *fname)
2466 struct stream_struct *stream_info;
2467 files_struct **streams;
2468 int i;
2469 unsigned int num_streams;
2470 TALLOC_CTX *frame = talloc_stackframe();
2471 NTSTATUS status;
2473 status = SMB_VFS_STREAMINFO(conn, NULL, fname, talloc_tos(),
2474 &num_streams, &stream_info);
2476 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
2477 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
2478 DEBUG(10, ("no streams around\n"));
2479 TALLOC_FREE(frame);
2480 return NT_STATUS_OK;
2483 if (!NT_STATUS_IS_OK(status)) {
2484 DEBUG(10, ("SMB_VFS_STREAMINFO failed: %s\n",
2485 nt_errstr(status)));
2486 goto fail;
2489 DEBUG(10, ("open_streams_for_delete found %d streams\n",
2490 num_streams));
2492 if (num_streams == 0) {
2493 TALLOC_FREE(frame);
2494 return NT_STATUS_OK;
2497 streams = TALLOC_ARRAY(talloc_tos(), files_struct *, num_streams);
2498 if (streams == NULL) {
2499 DEBUG(0, ("talloc failed\n"));
2500 status = NT_STATUS_NO_MEMORY;
2501 goto fail;
2504 for (i=0; i<num_streams; i++) {
2505 char *streamname;
2507 if (strequal(stream_info[i].name, "::$DATA")) {
2508 streams[i] = NULL;
2509 continue;
2512 streamname = talloc_asprintf(talloc_tos(), "%s%s", fname,
2513 stream_info[i].name);
2515 if (streamname == NULL) {
2516 DEBUG(0, ("talloc_aprintf failed\n"));
2517 status = NT_STATUS_NO_MEMORY;
2518 goto fail;
2521 status = create_file_unixpath
2522 (conn, /* conn */
2523 NULL, /* req */
2524 streamname, /* fname */
2525 DELETE_ACCESS, /* access_mask */
2526 FILE_SHARE_READ | FILE_SHARE_WRITE
2527 | FILE_SHARE_DELETE, /* share_access */
2528 FILE_OPEN, /* create_disposition*/
2529 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* create_options */
2530 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
2531 0, /* oplock_request */
2532 0, /* allocation_size */
2533 NULL, /* sd */
2534 NULL, /* ea_list */
2535 &streams[i], /* result */
2536 NULL, /* pinfo */
2537 NULL); /* psbuf */
2539 TALLOC_FREE(streamname);
2541 if (!NT_STATUS_IS_OK(status)) {
2542 DEBUG(10, ("Could not open stream %s: %s\n",
2543 streamname, nt_errstr(status)));
2544 break;
2549 * don't touch the variable "status" beyond this point :-)
2552 for (i -= 1 ; i >= 0; i--) {
2553 if (streams[i] == NULL) {
2554 continue;
2557 DEBUG(10, ("Closing stream # %d, %s\n", i,
2558 streams[i]->fsp_name));
2559 close_file(streams[i], NORMAL_CLOSE);
2562 fail:
2563 TALLOC_FREE(frame);
2564 return status;
2568 * Wrapper around open_file_ntcreate and open_directory
2571 NTSTATUS create_file_unixpath(connection_struct *conn,
2572 struct smb_request *req,
2573 const char *fname,
2574 uint32_t access_mask,
2575 uint32_t share_access,
2576 uint32_t create_disposition,
2577 uint32_t create_options,
2578 uint32_t file_attributes,
2579 uint32_t oplock_request,
2580 SMB_BIG_UINT allocation_size,
2581 struct security_descriptor *sd,
2582 struct ea_list *ea_list,
2584 files_struct **result,
2585 int *pinfo,
2586 SMB_STRUCT_STAT *psbuf)
2588 SMB_STRUCT_STAT sbuf;
2589 int info = FILE_WAS_OPENED;
2590 files_struct *base_fsp = NULL;
2591 files_struct *fsp = NULL;
2592 NTSTATUS status;
2594 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
2595 "file_attributes = 0x%x, share_access = 0x%x, "
2596 "create_disposition = 0x%x create_options = 0x%x "
2597 "oplock_request = 0x%x ea_list = 0x%p, sd = 0x%p, "
2598 "fname = %s\n",
2599 (unsigned int)access_mask,
2600 (unsigned int)file_attributes,
2601 (unsigned int)share_access,
2602 (unsigned int)create_disposition,
2603 (unsigned int)create_options,
2604 (unsigned int)oplock_request,
2605 ea_list, sd, fname));
2607 if (create_options & FILE_OPEN_BY_FILE_ID) {
2608 status = NT_STATUS_NOT_SUPPORTED;
2609 goto fail;
2612 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
2613 status = NT_STATUS_INVALID_PARAMETER;
2614 goto fail;
2617 if (req == NULL) {
2618 oplock_request |= INTERNAL_OPEN_ONLY;
2621 if (psbuf != NULL) {
2622 sbuf = *psbuf;
2624 else {
2625 if (SMB_VFS_STAT(conn, fname, &sbuf) == -1) {
2626 SET_STAT_INVALID(sbuf);
2630 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
2631 && (access_mask & DELETE_ACCESS)
2632 && !is_ntfs_stream_name(fname)) {
2634 * We can't open a file with DELETE access if any of the
2635 * streams is open without FILE_SHARE_DELETE
2637 status = open_streams_for_delete(conn, fname);
2639 if (!NT_STATUS_IS_OK(status)) {
2640 goto fail;
2644 /* This is the correct thing to do (check every time) but can_delete
2645 * is expensive (it may have to read the parent directory
2646 * permissions). So for now we're not doing it unless we have a strong
2647 * hint the client is really going to delete this file. If the client
2648 * is forcing FILE_CREATE let the filesystem take care of the
2649 * permissions. */
2651 /* Setting FILE_SHARE_DELETE is the hint. */
2653 if (lp_acl_check_permissions(SNUM(conn))
2654 && (create_disposition != FILE_CREATE)
2655 && (share_access & FILE_SHARE_DELETE)
2656 && (access_mask & DELETE_ACCESS)
2657 && (!can_delete_file_in_directory(conn, fname))) {
2658 status = NT_STATUS_ACCESS_DENIED;
2659 goto fail;
2662 #if 0
2663 /* We need to support SeSecurityPrivilege for this. */
2664 if ((access_mask & SEC_RIGHT_SYSTEM_SECURITY) &&
2665 !user_has_privileges(current_user.nt_user_token,
2666 &se_security)) {
2667 status = NT_STATUS_PRIVILEGE_NOT_HELD;
2668 goto fail;
2670 #endif
2672 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
2673 && is_ntfs_stream_name(fname)
2674 && (!(create_options & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
2675 char *base;
2676 uint32 base_create_disposition;
2678 if (create_options & FILE_DIRECTORY_FILE) {
2679 status = NT_STATUS_NOT_A_DIRECTORY;
2680 goto fail;
2683 status = split_ntfs_stream_name(talloc_tos(), fname,
2684 &base, NULL);
2685 if (!NT_STATUS_IS_OK(status)) {
2686 DEBUG(10, ("create_file_unixpath: "
2687 "split_ntfs_stream_name failed: %s\n",
2688 nt_errstr(status)));
2689 goto fail;
2692 SMB_ASSERT(!is_ntfs_stream_name(base)); /* paranoia.. */
2694 switch (create_disposition) {
2695 case FILE_OPEN:
2696 base_create_disposition = FILE_OPEN;
2697 break;
2698 default:
2699 base_create_disposition = FILE_OPEN_IF;
2700 break;
2703 status = create_file_unixpath(conn, NULL, base, 0,
2704 FILE_SHARE_READ
2705 | FILE_SHARE_WRITE
2706 | FILE_SHARE_DELETE,
2707 base_create_disposition,
2708 0, 0, 0, 0, NULL, NULL,
2709 &base_fsp, NULL, NULL);
2710 if (!NT_STATUS_IS_OK(status)) {
2711 DEBUG(10, ("create_file_unixpath for base %s failed: "
2712 "%s\n", base, nt_errstr(status)));
2713 goto fail;
2718 * If it's a request for a directory open, deal with it separately.
2721 if (create_options & FILE_DIRECTORY_FILE) {
2723 if (create_options & FILE_NON_DIRECTORY_FILE) {
2724 status = NT_STATUS_INVALID_PARAMETER;
2725 goto fail;
2728 /* Can't open a temp directory. IFS kit test. */
2729 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
2730 status = NT_STATUS_INVALID_PARAMETER;
2731 goto fail;
2735 * We will get a create directory here if the Win32
2736 * app specified a security descriptor in the
2737 * CreateDirectory() call.
2740 oplock_request = 0;
2741 status = open_directory(
2742 conn, req, fname, &sbuf, access_mask, share_access,
2743 create_disposition, create_options, file_attributes,
2744 &info, &fsp);
2745 } else {
2748 * Ordinary file case.
2751 status = open_file_ntcreate(
2752 conn, req, fname, &sbuf, access_mask, share_access,
2753 create_disposition, create_options, file_attributes,
2754 oplock_request, &info, &fsp);
2756 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
2759 * Fail the open if it was explicitly a non-directory
2760 * file.
2763 if (create_options & FILE_NON_DIRECTORY_FILE) {
2764 status = NT_STATUS_FILE_IS_A_DIRECTORY;
2765 goto fail;
2768 oplock_request = 0;
2769 status = open_directory(
2770 conn, req, fname, &sbuf, access_mask,
2771 share_access, create_disposition,
2772 create_options, file_attributes,
2773 &info, &fsp);
2777 if (!NT_STATUS_IS_OK(status)) {
2778 goto fail;
2782 * According to the MS documentation, the only time the security
2783 * descriptor is applied to the opened file is iff we *created* the
2784 * file; an existing file stays the same.
2786 * Also, it seems (from observation) that you can open the file with
2787 * any access mask but you can still write the sd. We need to override
2788 * the granted access before we call set_sd
2789 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
2792 if ((sd != NULL) && (info == FILE_WAS_CREATED)
2793 && lp_nt_acl_support(SNUM(conn))) {
2795 uint32_t sec_info_sent = ALL_SECURITY_INFORMATION;
2796 uint32_t saved_access_mask = fsp->access_mask;
2798 if (sd->owner_sid == NULL) {
2799 sec_info_sent &= ~OWNER_SECURITY_INFORMATION;
2801 if (sd->group_sid == NULL) {
2802 sec_info_sent &= ~GROUP_SECURITY_INFORMATION;
2804 if (sd->sacl == NULL) {
2805 sec_info_sent &= ~SACL_SECURITY_INFORMATION;
2807 if (sd->dacl == NULL) {
2808 sec_info_sent &= ~DACL_SECURITY_INFORMATION;
2811 fsp->access_mask = FILE_GENERIC_ALL;
2813 /* Convert all the generic bits. */
2814 security_acl_map_generic(sd->dacl, &file_generic_mapping);
2815 security_acl_map_generic(sd->sacl, &file_generic_mapping);
2817 status = SMB_VFS_FSET_NT_ACL(fsp, sec_info_sent, sd);
2819 fsp->access_mask = saved_access_mask;
2821 if (!NT_STATUS_IS_OK(status)) {
2822 goto fail;
2826 if ((ea_list != NULL) && (info == FILE_WAS_CREATED)) {
2827 status = set_ea(conn, fsp, fname, ea_list);
2828 if (!NT_STATUS_IS_OK(status)) {
2829 goto fail;
2833 if (!fsp->is_directory && S_ISDIR(sbuf.st_mode)) {
2834 status = NT_STATUS_ACCESS_DENIED;
2835 goto fail;
2838 /* Save the requested allocation size. */
2839 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
2840 if (allocation_size
2841 && (allocation_size > sbuf.st_size)) {
2842 fsp->initial_allocation_size = smb_roundup(
2843 fsp->conn, allocation_size);
2844 if (fsp->is_directory) {
2845 /* Can't set allocation size on a directory. */
2846 status = NT_STATUS_ACCESS_DENIED;
2847 goto fail;
2849 if (vfs_allocate_file_space(
2850 fsp, fsp->initial_allocation_size) == -1) {
2851 status = NT_STATUS_DISK_FULL;
2852 goto fail;
2854 } else {
2855 fsp->initial_allocation_size = smb_roundup(
2856 fsp->conn, (SMB_BIG_UINT)sbuf.st_size);
2860 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
2863 * Set fsp->base_fsp late enough that we can't "goto fail" anymore. In
2864 * the fail: branch we call close_file(fsp, ERROR_CLOSE) which would
2865 * also close fsp->base_fsp which we have to also do explicitly in
2866 * this routine here, as not in all "goto fail:" we have the fsp set
2867 * up already to be initialized with the base_fsp.
2870 fsp->base_fsp = base_fsp;
2872 *result = fsp;
2873 if (pinfo != NULL) {
2874 *pinfo = info;
2876 if (psbuf != NULL) {
2877 if ((fsp->fh == NULL) || (fsp->fh->fd == -1)) {
2878 *psbuf = sbuf;
2880 else {
2881 SMB_VFS_FSTAT(fsp, psbuf);
2884 return NT_STATUS_OK;
2886 fail:
2887 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
2889 if (fsp != NULL) {
2890 close_file(fsp, ERROR_CLOSE);
2891 fsp = NULL;
2893 if (base_fsp != NULL) {
2894 close_file(base_fsp, ERROR_CLOSE);
2895 base_fsp = NULL;
2897 return status;
2900 NTSTATUS create_file(connection_struct *conn,
2901 struct smb_request *req,
2902 uint16_t root_dir_fid,
2903 const char *fname,
2904 uint32_t access_mask,
2905 uint32_t share_access,
2906 uint32_t create_disposition,
2907 uint32_t create_options,
2908 uint32_t file_attributes,
2909 uint32_t oplock_request,
2910 SMB_BIG_UINT allocation_size,
2911 struct security_descriptor *sd,
2912 struct ea_list *ea_list,
2914 files_struct **result,
2915 int *pinfo,
2916 SMB_STRUCT_STAT *psbuf)
2918 struct case_semantics_state *case_state = NULL;
2919 SMB_STRUCT_STAT sbuf;
2920 int info = FILE_WAS_OPENED;
2921 files_struct *fsp = NULL;
2922 NTSTATUS status;
2924 DEBUG(10,("create_file: access_mask = 0x%x "
2925 "file_attributes = 0x%x, share_access = 0x%x, "
2926 "create_disposition = 0x%x create_options = 0x%x "
2927 "oplock_request = 0x%x "
2928 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
2929 "fname = %s\n",
2930 (unsigned int)access_mask,
2931 (unsigned int)file_attributes,
2932 (unsigned int)share_access,
2933 (unsigned int)create_disposition,
2934 (unsigned int)create_options,
2935 (unsigned int)oplock_request,
2936 (unsigned int)root_dir_fid,
2937 ea_list, sd, fname));
2940 * Get the file name.
2943 if (root_dir_fid != 0) {
2945 * This filename is relative to a directory fid.
2947 char *parent_fname = NULL;
2948 files_struct *dir_fsp = file_fsp(root_dir_fid);
2950 if (dir_fsp == NULL) {
2951 status = NT_STATUS_INVALID_HANDLE;
2952 goto fail;
2955 if (!dir_fsp->is_directory) {
2958 * Check to see if this is a mac fork of some kind.
2961 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
2962 is_ntfs_stream_name(fname)) {
2963 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
2964 goto fail;
2968 we need to handle the case when we get a
2969 relative open relative to a file and the
2970 pathname is blank - this is a reopen!
2971 (hint from demyn plantenberg)
2974 status = NT_STATUS_INVALID_HANDLE;
2975 goto fail;
2978 if (ISDOT(dir_fsp->fsp_name)) {
2980 * We're at the toplevel dir, the final file name
2981 * must not contain ./, as this is filtered out
2982 * normally by srvstr_get_path and unix_convert
2983 * explicitly rejects paths containing ./.
2985 parent_fname = talloc_strdup(talloc_tos(), "");
2986 if (parent_fname == NULL) {
2987 status = NT_STATUS_NO_MEMORY;
2988 goto fail;
2990 } else {
2991 size_t dir_name_len = strlen(dir_fsp->fsp_name);
2994 * Copy in the base directory name.
2997 parent_fname = TALLOC_ARRAY(talloc_tos(), char,
2998 dir_name_len+2);
2999 if (parent_fname == NULL) {
3000 status = NT_STATUS_NO_MEMORY;
3001 goto fail;
3003 memcpy(parent_fname, dir_fsp->fsp_name,
3004 dir_name_len+1);
3007 * Ensure it ends in a '/'.
3008 * We used TALLOC_SIZE +2 to add space for the '/'.
3011 if(dir_name_len
3012 && (parent_fname[dir_name_len-1] != '\\')
3013 && (parent_fname[dir_name_len-1] != '/')) {
3014 parent_fname[dir_name_len] = '/';
3015 parent_fname[dir_name_len+1] = '\0';
3019 fname = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
3020 fname);
3021 if (fname == NULL) {
3022 status = NT_STATUS_NO_MEMORY;
3023 goto fail;
3028 * Check to see if this is a mac fork of some kind.
3031 if (is_ntfs_stream_name(fname)) {
3032 enum FAKE_FILE_TYPE fake_file_type;
3034 fake_file_type = is_fake_file(fname);
3036 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
3039 * Here we go! support for changing the disk quotas
3040 * --metze
3042 * We need to fake up to open this MAGIC QUOTA file
3043 * and return a valid FID.
3045 * w2k close this file directly after openening xp
3046 * also tries a QUERY_FILE_INFO on the file and then
3047 * close it
3049 status = open_fake_file(conn, req->vuid,
3050 fake_file_type, fname,
3051 access_mask, &fsp);
3052 if (!NT_STATUS_IS_OK(status)) {
3053 goto fail;
3056 ZERO_STRUCT(sbuf);
3057 goto done;
3060 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
3061 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3062 goto fail;
3066 if ((req != NULL) && (req->flags2 & FLAGS2_DFS_PATHNAMES)) {
3067 char *resolved_fname;
3069 status = resolve_dfspath(talloc_tos(), conn, true, fname,
3070 &resolved_fname);
3072 if (!NT_STATUS_IS_OK(status)) {
3074 * For PATH_NOT_COVERED we had
3075 * reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
3076 * ERRSRV, ERRbadpath);
3077 * Need to fix in callers
3079 goto fail;
3081 fname = resolved_fname;
3085 * Check if POSIX semantics are wanted.
3088 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
3089 case_state = set_posix_case_semantics(talloc_tos(), conn);
3090 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
3094 char *converted_fname;
3096 SET_STAT_INVALID(sbuf);
3098 status = unix_convert(talloc_tos(), conn, fname, False,
3099 &converted_fname, NULL, &sbuf);
3100 if (!NT_STATUS_IS_OK(status)) {
3101 goto fail;
3103 fname = converted_fname;
3106 TALLOC_FREE(case_state);
3108 /* All file access must go through check_name() */
3110 status = check_name(conn, fname);
3111 if (!NT_STATUS_IS_OK(status)) {
3112 goto fail;
3115 status = create_file_unixpath(
3116 conn, req, fname, access_mask, share_access,
3117 create_disposition, create_options, file_attributes,
3118 oplock_request, allocation_size, sd, ea_list,
3119 &fsp, &info, &sbuf);
3121 if (!NT_STATUS_IS_OK(status)) {
3122 goto fail;
3125 done:
3126 DEBUG(10, ("create_file: info=%d\n", info));
3128 *result = fsp;
3129 if (pinfo != NULL) {
3130 *pinfo = info;
3132 if (psbuf != NULL) {
3133 *psbuf = sbuf;
3135 return NT_STATUS_OK;
3137 fail:
3138 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
3140 if (fsp != NULL) {
3141 close_file(fsp, ERROR_CLOSE);
3142 fsp = NULL;
3144 return status;