Fix coverity CID-602. Possible use of uninitialized var.
[Samba.git] / source / smbd / open.c
blob82098647990749dac19ac7ae37b9a05814fc67b3
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 struct current_user current_user;
26 extern userdom_struct current_user_info;
27 extern bool global_client_failed_oplock_break;
29 struct deferred_open_record {
30 bool delayed_for_oplocks;
31 struct file_id id;
34 /****************************************************************************
35 fd support routines - attempt to do a dos_open.
36 ****************************************************************************/
38 static NTSTATUS fd_open(struct connection_struct *conn,
39 const char *fname,
40 files_struct *fsp,
41 int flags,
42 mode_t mode)
44 NTSTATUS status = NT_STATUS_OK;
46 #ifdef O_NOFOLLOW
47 /*
48 * Never follow symlinks on a POSIX client. The
49 * client should be doing this.
52 if (fsp->posix_open || !lp_symlinks(SNUM(conn))) {
53 flags |= O_NOFOLLOW;
55 #endif
57 fsp->fh->fd = SMB_VFS_OPEN(conn,fname,fsp,flags,mode);
58 if (fsp->fh->fd == -1) {
59 status = map_nt_error_from_unix(errno);
62 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
63 fname, flags, (int)mode, fsp->fh->fd,
64 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
66 return status;
69 /****************************************************************************
70 Close the file associated with a fsp.
71 ****************************************************************************/
73 NTSTATUS fd_close(files_struct *fsp)
75 int ret;
77 if (fsp->fh->fd == -1) {
78 return NT_STATUS_OK; /* What we used to call a stat open. */
80 if (fsp->fh->ref_count > 1) {
81 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
84 ret = SMB_VFS_CLOSE(fsp);
85 fsp->fh->fd = -1;
86 if (ret == -1) {
87 return map_nt_error_from_unix(errno);
89 return NT_STATUS_OK;
92 /****************************************************************************
93 Change the ownership of a file to that of the parent directory.
94 Do this by fd if possible.
95 ****************************************************************************/
97 static void change_file_owner_to_parent(connection_struct *conn,
98 const char *inherit_from_dir,
99 files_struct *fsp)
101 SMB_STRUCT_STAT parent_st;
102 int ret;
104 ret = SMB_VFS_STAT(conn, inherit_from_dir, &parent_st);
105 if (ret == -1) {
106 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
107 "directory %s. Error was %s\n",
108 inherit_from_dir, strerror(errno) ));
109 return;
112 become_root();
113 ret = SMB_VFS_FCHOWN(fsp, parent_st.st_uid, (gid_t)-1);
114 unbecome_root();
115 if (ret == -1) {
116 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
117 "file %s to parent directory uid %u. Error "
118 "was %s\n", fsp->fsp_name,
119 (unsigned int)parent_st.st_uid,
120 strerror(errno) ));
123 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
124 "parent directory uid %u.\n", fsp->fsp_name,
125 (unsigned int)parent_st.st_uid ));
128 static NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
129 const char *inherit_from_dir,
130 const char *fname,
131 SMB_STRUCT_STAT *psbuf)
133 char *saved_dir = NULL;
134 SMB_STRUCT_STAT sbuf;
135 SMB_STRUCT_STAT parent_st;
136 TALLOC_CTX *ctx = talloc_tos();
137 NTSTATUS status = NT_STATUS_OK;
138 int ret;
140 ret = SMB_VFS_STAT(conn, inherit_from_dir, &parent_st);
141 if (ret == -1) {
142 status = map_nt_error_from_unix(errno);
143 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
144 "directory %s. Error was %s\n",
145 inherit_from_dir, strerror(errno) ));
146 return status;
149 /* We've already done an lstat into psbuf, and we know it's a
150 directory. If we can cd into the directory and the dev/ino
151 are the same then we can safely chown without races as
152 we're locking the directory in place by being in it. This
153 should work on any UNIX (thanks tridge :-). JRA.
156 saved_dir = vfs_GetWd(ctx,conn);
157 if (!saved_dir) {
158 status = map_nt_error_from_unix(errno);
159 DEBUG(0,("change_dir_owner_to_parent: failed to get "
160 "current working directory. Error was %s\n",
161 strerror(errno)));
162 return status;
165 /* Chdir into the new path. */
166 if (vfs_ChDir(conn, fname) == -1) {
167 status = map_nt_error_from_unix(errno);
168 DEBUG(0,("change_dir_owner_to_parent: failed to change "
169 "current working directory to %s. Error "
170 "was %s\n", fname, strerror(errno) ));
171 goto out;
174 if (SMB_VFS_STAT(conn,".",&sbuf) == -1) {
175 status = map_nt_error_from_unix(errno);
176 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
177 "directory '.' (%s) Error was %s\n",
178 fname, strerror(errno)));
179 goto out;
182 /* Ensure we're pointing at the same place. */
183 if (sbuf.st_dev != psbuf->st_dev ||
184 sbuf.st_ino != psbuf->st_ino ||
185 sbuf.st_mode != psbuf->st_mode ) {
186 DEBUG(0,("change_dir_owner_to_parent: "
187 "device/inode/mode on directory %s changed. "
188 "Refusing to chown !\n", fname ));
189 status = NT_STATUS_ACCESS_DENIED;
190 goto out;
193 become_root();
194 ret = SMB_VFS_CHOWN(conn, ".", parent_st.st_uid, (gid_t)-1);
195 unbecome_root();
196 if (ret == -1) {
197 status = map_nt_error_from_unix(errno);
198 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
199 "directory %s to parent directory uid %u. "
200 "Error was %s\n", fname,
201 (unsigned int)parent_st.st_uid, strerror(errno) ));
202 goto out;
205 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
206 "directory %s to parent directory uid %u.\n",
207 fname, (unsigned int)parent_st.st_uid ));
209 out:
211 vfs_ChDir(conn,saved_dir);
212 return status;
215 /****************************************************************************
216 Open a file.
217 ****************************************************************************/
219 static NTSTATUS open_file(files_struct *fsp,
220 connection_struct *conn,
221 struct smb_request *req,
222 const char *parent_dir,
223 const char *name,
224 const char *path,
225 SMB_STRUCT_STAT *psbuf,
226 int flags,
227 mode_t unx_mode,
228 uint32 access_mask, /* client requested access mask. */
229 uint32 open_access_mask) /* what we're actually using in the open. */
231 NTSTATUS status = NT_STATUS_OK;
232 int accmode = (flags & O_ACCMODE);
233 int local_flags = flags;
234 bool file_existed = VALID_STAT(*psbuf);
236 fsp->fh->fd = -1;
237 errno = EPERM;
239 /* Check permissions */
242 * This code was changed after seeing a client open request
243 * containing the open mode of (DENY_WRITE/read-only) with
244 * the 'create if not exist' bit set. The previous code
245 * would fail to open the file read only on a read-only share
246 * as it was checking the flags parameter directly against O_RDONLY,
247 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
248 * JRA.
251 if (!CAN_WRITE(conn)) {
252 /* It's a read-only share - fail if we wanted to write. */
253 if(accmode != O_RDONLY) {
254 DEBUG(3,("Permission denied opening %s\n", path));
255 return NT_STATUS_ACCESS_DENIED;
256 } else if(flags & O_CREAT) {
257 /* We don't want to write - but we must make sure that
258 O_CREAT doesn't create the file if we have write
259 access into the directory.
261 flags &= ~O_CREAT;
262 local_flags &= ~O_CREAT;
267 * This little piece of insanity is inspired by the
268 * fact that an NT client can open a file for O_RDONLY,
269 * but set the create disposition to FILE_EXISTS_TRUNCATE.
270 * If the client *can* write to the file, then it expects to
271 * truncate the file, even though it is opening for readonly.
272 * Quicken uses this stupid trick in backup file creation...
273 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
274 * for helping track this one down. It didn't bite us in 2.0.x
275 * as we always opened files read-write in that release. JRA.
278 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
279 DEBUG(10,("open_file: truncate requested on read-only open "
280 "for file %s\n", path));
281 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
284 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
285 (!file_existed && (local_flags & O_CREAT)) ||
286 ((local_flags & O_TRUNC) == O_TRUNC) ) {
287 const char *wild;
290 * We can't actually truncate here as the file may be locked.
291 * open_file_ntcreate will take care of the truncate later. JRA.
294 local_flags &= ~O_TRUNC;
296 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
298 * We would block on opening a FIFO with no one else on the
299 * other end. Do what we used to do and add O_NONBLOCK to the
300 * open flags. JRA.
303 if (file_existed && S_ISFIFO(psbuf->st_mode)) {
304 local_flags |= O_NONBLOCK;
306 #endif
308 /* Don't create files with Microsoft wildcard characters. */
309 if (fsp->base_fsp) {
311 * wildcard characters are allowed in stream names
312 * only test the basefilename
314 wild = fsp->base_fsp->fsp_name;
315 } else {
316 wild = path;
318 if ((local_flags & O_CREAT) && !file_existed &&
319 ms_has_wild(wild)) {
320 return NT_STATUS_OBJECT_NAME_INVALID;
323 /* Actually do the open */
324 status = fd_open(conn, path, fsp, local_flags, unx_mode);
325 if (!NT_STATUS_IS_OK(status)) {
326 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
327 "(flags=%d)\n",
328 path,nt_errstr(status),local_flags,flags));
329 return status;
332 if ((local_flags & O_CREAT) && !file_existed) {
334 /* Inherit the ACL if required */
335 if (lp_inherit_perms(SNUM(conn))) {
336 inherit_access_acl(conn, parent_dir, path,
337 unx_mode);
340 /* Change the owner if required. */
341 if (lp_inherit_owner(SNUM(conn))) {
342 change_file_owner_to_parent(conn, parent_dir,
343 fsp);
346 notify_fname(conn, NOTIFY_ACTION_ADDED,
347 FILE_NOTIFY_CHANGE_FILE_NAME, path);
350 } else {
351 fsp->fh->fd = -1; /* What we used to call a stat open. */
354 if (!file_existed) {
355 int ret;
357 if (fsp->fh->fd == -1) {
358 ret = SMB_VFS_STAT(conn, path, psbuf);
359 } else {
360 ret = SMB_VFS_FSTAT(fsp, psbuf);
361 /* If we have an fd, this stat should succeed. */
362 if (ret == -1) {
363 DEBUG(0,("Error doing fstat on open file %s "
364 "(%s)\n", path,strerror(errno) ));
368 /* For a non-io open, this stat failing means file not found. JRA */
369 if (ret == -1) {
370 status = map_nt_error_from_unix(errno);
371 fd_close(fsp);
372 return status;
377 * POSIX allows read-only opens of directories. We don't
378 * want to do this (we use a different code path for this)
379 * so catch a directory open and return an EISDIR. JRA.
382 if(S_ISDIR(psbuf->st_mode)) {
383 fd_close(fsp);
384 errno = EISDIR;
385 return NT_STATUS_FILE_IS_A_DIRECTORY;
388 fsp->mode = psbuf->st_mode;
389 fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
390 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
391 fsp->file_pid = req ? req->smbpid : 0;
392 fsp->can_lock = True;
393 fsp->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
394 if (!CAN_WRITE(conn)) {
395 fsp->can_write = False;
396 } else {
397 fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ?
398 True : False;
400 fsp->print_file = False;
401 fsp->modified = False;
402 fsp->sent_oplock_break = NO_BREAK_SENT;
403 fsp->is_directory = False;
404 fsp->is_stat = False;
405 if (conn->aio_write_behind_list &&
406 is_in_path(path, conn->aio_write_behind_list, conn->case_sensitive)) {
407 fsp->aio_write_behind = True;
410 string_set(&fsp->fsp_name, path);
411 fsp->wcp = NULL; /* Write cache pointer. */
413 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
414 *current_user_info.smb_name ?
415 current_user_info.smb_name : conn->user,fsp->fsp_name,
416 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
417 conn->num_files_open));
419 errno = 0;
420 return NT_STATUS_OK;
423 /*******************************************************************
424 Return True if the filename is one of the special executable types.
425 ********************************************************************/
427 static bool is_executable(const char *fname)
429 if ((fname = strrchr_m(fname,'.'))) {
430 if (strequal(fname,".com") ||
431 strequal(fname,".dll") ||
432 strequal(fname,".exe") ||
433 strequal(fname,".sym")) {
434 return True;
437 return False;
440 /****************************************************************************
441 Check if we can open a file with a share mode.
442 Returns True if conflict, False if not.
443 ****************************************************************************/
445 static bool share_conflict(struct share_mode_entry *entry,
446 uint32 access_mask,
447 uint32 share_access)
449 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
450 "entry->share_access = 0x%x, "
451 "entry->private_options = 0x%x\n",
452 (unsigned int)entry->access_mask,
453 (unsigned int)entry->share_access,
454 (unsigned int)entry->private_options));
456 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
457 (unsigned int)access_mask, (unsigned int)share_access));
459 if ((entry->access_mask & (FILE_WRITE_DATA|
460 FILE_APPEND_DATA|
461 FILE_READ_DATA|
462 FILE_EXECUTE|
463 DELETE_ACCESS)) == 0) {
464 DEBUG(10,("share_conflict: No conflict due to "
465 "entry->access_mask = 0x%x\n",
466 (unsigned int)entry->access_mask ));
467 return False;
470 if ((access_mask & (FILE_WRITE_DATA|
471 FILE_APPEND_DATA|
472 FILE_READ_DATA|
473 FILE_EXECUTE|
474 DELETE_ACCESS)) == 0) {
475 DEBUG(10,("share_conflict: No conflict due to "
476 "access_mask = 0x%x\n",
477 (unsigned int)access_mask ));
478 return False;
481 #if 1 /* JRA TEST - Superdebug. */
482 #define CHECK_MASK(num, am, right, sa, share) \
483 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
484 (unsigned int)(num), (unsigned int)(am), \
485 (unsigned int)(right), (unsigned int)(am)&(right) )); \
486 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
487 (unsigned int)(num), (unsigned int)(sa), \
488 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
489 if (((am) & (right)) && !((sa) & (share))) { \
490 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
491 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
492 (unsigned int)(share) )); \
493 return True; \
495 #else
496 #define CHECK_MASK(num, am, right, sa, share) \
497 if (((am) & (right)) && !((sa) & (share))) { \
498 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
499 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
500 (unsigned int)(share) )); \
501 return True; \
503 #endif
505 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
506 share_access, FILE_SHARE_WRITE);
507 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
508 entry->share_access, FILE_SHARE_WRITE);
510 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
511 share_access, FILE_SHARE_READ);
512 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
513 entry->share_access, FILE_SHARE_READ);
515 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
516 share_access, FILE_SHARE_DELETE);
517 CHECK_MASK(6, access_mask, DELETE_ACCESS,
518 entry->share_access, FILE_SHARE_DELETE);
520 DEBUG(10,("share_conflict: No conflict.\n"));
521 return False;
524 #if defined(DEVELOPER)
525 static void validate_my_share_entries(int num,
526 struct share_mode_entry *share_entry)
528 files_struct *fsp;
530 if (!procid_is_me(&share_entry->pid)) {
531 return;
534 if (is_deferred_open_entry(share_entry) &&
535 !open_was_deferred(share_entry->op_mid)) {
536 char *str = talloc_asprintf(talloc_tos(),
537 "Got a deferred entry without a request: "
538 "PANIC: %s\n",
539 share_mode_str(talloc_tos(), num, share_entry));
540 smb_panic(str);
543 if (!is_valid_share_mode_entry(share_entry)) {
544 return;
547 fsp = file_find_dif(share_entry->id,
548 share_entry->share_file_id);
549 if (!fsp) {
550 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
551 share_mode_str(talloc_tos(), num, share_entry) ));
552 smb_panic("validate_my_share_entries: Cannot match a "
553 "share entry with an open file\n");
556 if (is_deferred_open_entry(share_entry) ||
557 is_unused_share_mode_entry(share_entry)) {
558 goto panic;
561 if ((share_entry->op_type == NO_OPLOCK) &&
562 (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK)) {
563 /* Someone has already written to it, but I haven't yet
564 * noticed */
565 return;
568 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
569 goto panic;
572 return;
574 panic:
576 char *str;
577 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
578 share_mode_str(talloc_tos(), num, share_entry) ));
579 str = talloc_asprintf(talloc_tos(),
580 "validate_my_share_entries: "
581 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
582 fsp->fsp_name, (unsigned int)fsp->oplock_type,
583 (unsigned int)share_entry->op_type );
584 smb_panic(str);
587 #endif
589 static bool is_stat_open(uint32 access_mask)
591 return (access_mask &&
592 ((access_mask & ~(SYNCHRONIZE_ACCESS| FILE_READ_ATTRIBUTES|
593 FILE_WRITE_ATTRIBUTES))==0) &&
594 ((access_mask & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|
595 FILE_WRITE_ATTRIBUTES)) != 0));
598 /****************************************************************************
599 Deal with share modes
600 Invarient: Share mode must be locked on entry and exit.
601 Returns -1 on error, or number of share modes on success (may be zero).
602 ****************************************************************************/
604 static NTSTATUS open_mode_check(connection_struct *conn,
605 const char *fname,
606 struct share_mode_lock *lck,
607 uint32 access_mask,
608 uint32 share_access,
609 uint32 create_options,
610 bool *file_existed)
612 int i;
614 if(lck->num_share_modes == 0) {
615 return NT_STATUS_OK;
618 *file_existed = True;
620 /* A delete on close prohibits everything */
622 if (lck->delete_on_close) {
623 return NT_STATUS_DELETE_PENDING;
626 if (is_stat_open(access_mask)) {
627 /* Stat open that doesn't trigger oplock breaks or share mode
628 * checks... ! JRA. */
629 return NT_STATUS_OK;
633 * Check if the share modes will give us access.
636 #if defined(DEVELOPER)
637 for(i = 0; i < lck->num_share_modes; i++) {
638 validate_my_share_entries(i, &lck->share_modes[i]);
640 #endif
642 if (!lp_share_modes(SNUM(conn))) {
643 return NT_STATUS_OK;
646 /* Now we check the share modes, after any oplock breaks. */
647 for(i = 0; i < lck->num_share_modes; i++) {
649 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
650 continue;
653 /* someone else has a share lock on it, check to see if we can
654 * too */
655 if (share_conflict(&lck->share_modes[i],
656 access_mask, share_access)) {
657 return NT_STATUS_SHARING_VIOLATION;
661 return NT_STATUS_OK;
664 static bool is_delete_request(files_struct *fsp) {
665 return ((fsp->access_mask == DELETE_ACCESS) &&
666 (fsp->oplock_type == NO_OPLOCK));
670 * Send a break message to the oplock holder and delay the open for
671 * our client.
674 static NTSTATUS send_break_message(files_struct *fsp,
675 struct share_mode_entry *exclusive,
676 uint16 mid,
677 int oplock_request)
679 NTSTATUS status;
680 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
682 DEBUG(10, ("Sending break request to PID %s\n",
683 procid_str_static(&exclusive->pid)));
684 exclusive->op_mid = mid;
686 /* Create the message. */
687 share_mode_entry_to_message(msg, exclusive);
689 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
690 don't want this set in the share mode struct pointed to by lck. */
692 if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
693 SSVAL(msg,6,exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
696 status = messaging_send_buf(smbd_messaging_context(), exclusive->pid,
697 MSG_SMB_BREAK_REQUEST,
698 (uint8 *)msg,
699 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
700 if (!NT_STATUS_IS_OK(status)) {
701 DEBUG(3, ("Could not send oplock break message: %s\n",
702 nt_errstr(status)));
705 return status;
709 * 1) No files open at all or internal open: Grant whatever the client wants.
711 * 2) Exclusive (or batch) oplock around: If the requested access is a delete
712 * request, break if the oplock around is a batch oplock. If it's another
713 * requested access type, break.
715 * 3) Only level2 around: Grant level2 and do nothing else.
718 static bool delay_for_oplocks(struct share_mode_lock *lck,
719 files_struct *fsp,
720 uint16 mid,
721 int pass_number,
722 int oplock_request)
724 extern uint32 global_client_caps;
725 int i;
726 struct share_mode_entry *exclusive = NULL;
727 bool valid_entry = false;
728 bool have_level2 = false;
729 bool have_a_none_oplock = false;
730 bool allow_level2 = (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
731 lp_level2_oplocks(SNUM(fsp->conn));
733 if (oplock_request & INTERNAL_OPEN_ONLY) {
734 fsp->oplock_type = NO_OPLOCK;
737 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
738 return false;
741 for (i=0; i<lck->num_share_modes; i++) {
743 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
744 continue;
747 /* At least one entry is not an invalid or deferred entry. */
748 valid_entry = true;
750 if (pass_number == 1) {
751 if (BATCH_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
752 SMB_ASSERT(exclusive == NULL);
753 exclusive = &lck->share_modes[i];
755 } else {
756 if (EXCLUSIVE_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
757 SMB_ASSERT(exclusive == NULL);
758 exclusive = &lck->share_modes[i];
762 if (LEVEL_II_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
763 SMB_ASSERT(exclusive == NULL);
764 have_level2 = true;
767 if (lck->share_modes[i].op_type == NO_OPLOCK) {
768 have_a_none_oplock = true;
772 if (exclusive != NULL) { /* Found an exclusive oplock */
773 bool delay_it = is_delete_request(fsp) ?
774 BATCH_OPLOCK_TYPE(exclusive->op_type) : true;
775 SMB_ASSERT(!have_level2);
776 if (delay_it) {
777 send_break_message(fsp, exclusive, mid, oplock_request);
778 return true;
783 * Match what was requested (fsp->oplock_type) with
784 * what was found in the existing share modes.
787 if (!valid_entry) {
788 /* All entries are placeholders or deferred.
789 * Directly grant whatever the client wants. */
790 if (fsp->oplock_type == NO_OPLOCK) {
791 /* Store a level2 oplock, but don't tell the client */
792 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
794 } else if (have_a_none_oplock) {
795 fsp->oplock_type = NO_OPLOCK;
796 } else if (have_level2) {
797 if (fsp->oplock_type == NO_OPLOCK ||
798 fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
799 /* Store a level2 oplock, but don't tell the client */
800 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
801 } else {
802 fsp->oplock_type = LEVEL_II_OPLOCK;
804 } else {
805 /* This case can never happen. */
806 SMB_ASSERT(1);
810 * Don't grant level2 to clients that don't want them
811 * or if we've turned them off.
813 if (fsp->oplock_type == LEVEL_II_OPLOCK && !allow_level2) {
814 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
817 DEBUG(10,("delay_for_oplocks: oplock type 0x%x on file %s\n",
818 fsp->oplock_type, fsp->fsp_name));
820 /* No delay. */
821 return false;
824 static bool request_timed_out(struct timeval request_time,
825 struct timeval timeout)
827 struct timeval now, end_time;
828 GetTimeOfDay(&now);
829 end_time = timeval_sum(&request_time, &timeout);
830 return (timeval_compare(&end_time, &now) < 0);
833 /****************************************************************************
834 Handle the 1 second delay in returning a SHARING_VIOLATION error.
835 ****************************************************************************/
837 static void defer_open(struct share_mode_lock *lck,
838 struct timeval request_time,
839 struct timeval timeout,
840 struct smb_request *req,
841 struct deferred_open_record *state)
843 int i;
845 /* Paranoia check */
847 for (i=0; i<lck->num_share_modes; i++) {
848 struct share_mode_entry *e = &lck->share_modes[i];
850 if (!is_deferred_open_entry(e)) {
851 continue;
854 if (procid_is_me(&e->pid) && (e->op_mid == req->mid)) {
855 DEBUG(0, ("Trying to defer an already deferred "
856 "request: mid=%d, exiting\n", req->mid));
857 exit_server("attempt to defer a deferred request");
861 /* End paranoia check */
863 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
864 "open entry for mid %u\n",
865 (unsigned int)request_time.tv_sec,
866 (unsigned int)request_time.tv_usec,
867 (unsigned int)req->mid));
869 if (!push_deferred_smb_message(req, request_time, timeout,
870 (char *)state, sizeof(*state))) {
871 exit_server("push_deferred_smb_message failed");
873 add_deferred_open(lck, req->mid, request_time, state->id);
876 * Push the MID of this packet on the signing queue.
877 * We only do this once, the first time we push the packet
878 * onto the deferred open queue, as this has a side effect
879 * of incrementing the response sequence number.
882 srv_defer_sign_response(req->mid);
886 /****************************************************************************
887 On overwrite open ensure that the attributes match.
888 ****************************************************************************/
890 static bool open_match_attributes(connection_struct *conn,
891 const char *path,
892 uint32 old_dos_attr,
893 uint32 new_dos_attr,
894 mode_t existing_unx_mode,
895 mode_t new_unx_mode,
896 mode_t *returned_unx_mode)
898 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
900 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
901 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
903 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
904 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
905 *returned_unx_mode = new_unx_mode;
906 } else {
907 *returned_unx_mode = (mode_t)0;
910 DEBUG(10,("open_match_attributes: file %s old_dos_attr = 0x%x, "
911 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
912 "returned_unx_mode = 0%o\n",
913 path,
914 (unsigned int)old_dos_attr,
915 (unsigned int)existing_unx_mode,
916 (unsigned int)new_dos_attr,
917 (unsigned int)*returned_unx_mode ));
919 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
920 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
921 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
922 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
923 return False;
926 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
927 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
928 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
929 return False;
932 return True;
935 /****************************************************************************
936 Special FCB or DOS processing in the case of a sharing violation.
937 Try and find a duplicated file handle.
938 ****************************************************************************/
940 static NTSTATUS fcb_or_dos_open(connection_struct *conn,
941 files_struct *fsp_to_dup_into,
942 const char *fname,
943 struct file_id id,
944 uint16 file_pid,
945 uint16 vuid,
946 uint32 access_mask,
947 uint32 share_access,
948 uint32 create_options)
950 files_struct *fsp;
952 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
953 "file %s.\n", fname ));
955 for(fsp = file_find_di_first(id); fsp;
956 fsp = file_find_di_next(fsp)) {
958 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
959 "vuid = %u, file_pid = %u, private_options = 0x%x "
960 "access_mask = 0x%x\n", fsp->fsp_name,
961 fsp->fh->fd, (unsigned int)fsp->vuid,
962 (unsigned int)fsp->file_pid,
963 (unsigned int)fsp->fh->private_options,
964 (unsigned int)fsp->access_mask ));
966 if (fsp->fh->fd != -1 &&
967 fsp->vuid == vuid &&
968 fsp->file_pid == file_pid &&
969 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
970 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
971 (fsp->access_mask & FILE_WRITE_DATA) &&
972 strequal(fsp->fsp_name, fname)) {
973 DEBUG(10,("fcb_or_dos_open: file match\n"));
974 break;
978 if (!fsp) {
979 return NT_STATUS_NOT_FOUND;
982 /* quite an insane set of semantics ... */
983 if (is_executable(fname) &&
984 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
985 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
986 return NT_STATUS_INVALID_PARAMETER;
989 /* We need to duplicate this fsp. */
990 dup_file_fsp(fsp, access_mask, share_access,
991 create_options, fsp_to_dup_into);
993 return NT_STATUS_OK;
996 /****************************************************************************
997 Open a file with a share mode - old openX method - map into NTCreate.
998 ****************************************************************************/
1000 bool map_open_params_to_ntcreate(const char *fname, int deny_mode, int open_func,
1001 uint32 *paccess_mask,
1002 uint32 *pshare_mode,
1003 uint32 *pcreate_disposition,
1004 uint32 *pcreate_options)
1006 uint32 access_mask;
1007 uint32 share_mode;
1008 uint32 create_disposition;
1009 uint32 create_options = FILE_NON_DIRECTORY_FILE;
1011 DEBUG(10,("map_open_params_to_ntcreate: fname = %s, deny_mode = 0x%x, "
1012 "open_func = 0x%x\n",
1013 fname, (unsigned int)deny_mode, (unsigned int)open_func ));
1015 /* Create the NT compatible access_mask. */
1016 switch (GET_OPENX_MODE(deny_mode)) {
1017 case DOS_OPEN_EXEC: /* Implies read-only - used to be FILE_READ_DATA */
1018 case DOS_OPEN_RDONLY:
1019 access_mask = FILE_GENERIC_READ;
1020 break;
1021 case DOS_OPEN_WRONLY:
1022 access_mask = FILE_GENERIC_WRITE;
1023 break;
1024 case DOS_OPEN_RDWR:
1025 case DOS_OPEN_FCB:
1026 access_mask = FILE_GENERIC_READ|FILE_GENERIC_WRITE;
1027 break;
1028 default:
1029 DEBUG(10,("map_open_params_to_ntcreate: bad open mode = 0x%x\n",
1030 (unsigned int)GET_OPENX_MODE(deny_mode)));
1031 return False;
1034 /* Create the NT compatible create_disposition. */
1035 switch (open_func) {
1036 case OPENX_FILE_EXISTS_FAIL|OPENX_FILE_CREATE_IF_NOT_EXIST:
1037 create_disposition = FILE_CREATE;
1038 break;
1040 case OPENX_FILE_EXISTS_OPEN:
1041 create_disposition = FILE_OPEN;
1042 break;
1044 case OPENX_FILE_EXISTS_OPEN|OPENX_FILE_CREATE_IF_NOT_EXIST:
1045 create_disposition = FILE_OPEN_IF;
1046 break;
1048 case OPENX_FILE_EXISTS_TRUNCATE:
1049 create_disposition = FILE_OVERWRITE;
1050 break;
1052 case OPENX_FILE_EXISTS_TRUNCATE|OPENX_FILE_CREATE_IF_NOT_EXIST:
1053 create_disposition = FILE_OVERWRITE_IF;
1054 break;
1056 default:
1057 /* From samba4 - to be confirmed. */
1058 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_EXEC) {
1059 create_disposition = FILE_CREATE;
1060 break;
1062 DEBUG(10,("map_open_params_to_ntcreate: bad "
1063 "open_func 0x%x\n", (unsigned int)open_func));
1064 return False;
1067 /* Create the NT compatible share modes. */
1068 switch (GET_DENY_MODE(deny_mode)) {
1069 case DENY_ALL:
1070 share_mode = FILE_SHARE_NONE;
1071 break;
1073 case DENY_WRITE:
1074 share_mode = FILE_SHARE_READ;
1075 break;
1077 case DENY_READ:
1078 share_mode = FILE_SHARE_WRITE;
1079 break;
1081 case DENY_NONE:
1082 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1083 break;
1085 case DENY_DOS:
1086 create_options |= NTCREATEX_OPTIONS_PRIVATE_DENY_DOS;
1087 if (is_executable(fname)) {
1088 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1089 } else {
1090 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_RDONLY) {
1091 share_mode = FILE_SHARE_READ;
1092 } else {
1093 share_mode = FILE_SHARE_NONE;
1096 break;
1098 case DENY_FCB:
1099 create_options |= NTCREATEX_OPTIONS_PRIVATE_DENY_FCB;
1100 share_mode = FILE_SHARE_NONE;
1101 break;
1103 default:
1104 DEBUG(10,("map_open_params_to_ntcreate: bad deny_mode 0x%x\n",
1105 (unsigned int)GET_DENY_MODE(deny_mode) ));
1106 return False;
1109 DEBUG(10,("map_open_params_to_ntcreate: file %s, access_mask = 0x%x, "
1110 "share_mode = 0x%x, create_disposition = 0x%x, "
1111 "create_options = 0x%x\n",
1112 fname,
1113 (unsigned int)access_mask,
1114 (unsigned int)share_mode,
1115 (unsigned int)create_disposition,
1116 (unsigned int)create_options ));
1118 if (paccess_mask) {
1119 *paccess_mask = access_mask;
1121 if (pshare_mode) {
1122 *pshare_mode = share_mode;
1124 if (pcreate_disposition) {
1125 *pcreate_disposition = create_disposition;
1127 if (pcreate_options) {
1128 *pcreate_options = create_options;
1131 return True;
1135 static void schedule_defer_open(struct share_mode_lock *lck,
1136 struct timeval request_time,
1137 struct smb_request *req)
1139 struct deferred_open_record state;
1141 /* This is a relative time, added to the absolute
1142 request_time value to get the absolute timeout time.
1143 Note that if this is the second or greater time we enter
1144 this codepath for this particular request mid then
1145 request_time is left as the absolute time of the *first*
1146 time this request mid was processed. This is what allows
1147 the request to eventually time out. */
1149 struct timeval timeout;
1151 /* Normally the smbd we asked should respond within
1152 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1153 * the client did, give twice the timeout as a safety
1154 * measure here in case the other smbd is stuck
1155 * somewhere else. */
1157 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1159 /* Nothing actually uses state.delayed_for_oplocks
1160 but it's handy to differentiate in debug messages
1161 between a 30 second delay due to oplock break, and
1162 a 1 second delay for share mode conflicts. */
1164 state.delayed_for_oplocks = True;
1165 state.id = lck->id;
1167 if (!request_timed_out(request_time, timeout)) {
1168 defer_open(lck, request_time, timeout, req, &state);
1172 /****************************************************************************
1173 Open a file with a share mode. Passed in an already created files_struct *.
1174 ****************************************************************************/
1176 static NTSTATUS open_file_ntcreate_internal(connection_struct *conn,
1177 struct smb_request *req,
1178 const char *fname,
1179 SMB_STRUCT_STAT *psbuf,
1180 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1181 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1182 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1183 uint32 create_options, /* options such as delete on close. */
1184 uint32 new_dos_attributes, /* attributes used for new file. */
1185 int oplock_request, /* internal Samba oplock codes. */
1186 /* Information (FILE_EXISTS etc.) */
1187 int *pinfo,
1188 files_struct *fsp)
1190 int flags=0;
1191 int flags2=0;
1192 bool file_existed = VALID_STAT(*psbuf);
1193 bool def_acl = False;
1194 bool posix_open = False;
1195 bool new_file_created = False;
1196 struct file_id id;
1197 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1198 mode_t new_unx_mode = (mode_t)0;
1199 mode_t unx_mode = (mode_t)0;
1200 int info;
1201 uint32 existing_dos_attributes = 0;
1202 struct pending_message_list *pml = NULL;
1203 struct timeval request_time = timeval_zero();
1204 struct share_mode_lock *lck = NULL;
1205 uint32 open_access_mask = access_mask;
1206 NTSTATUS status;
1207 int ret_flock;
1208 char *parent_dir;
1209 const char *newname;
1211 ZERO_STRUCT(id);
1213 if (conn->printer) {
1215 * Printers are handled completely differently.
1216 * Most of the passed parameters are ignored.
1219 if (pinfo) {
1220 *pinfo = FILE_WAS_CREATED;
1223 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n", fname));
1225 return print_fsp_open(conn, fname, fsp, psbuf);
1228 if (!parent_dirname_talloc(talloc_tos(), fname, &parent_dir,
1229 &newname)) {
1230 return NT_STATUS_NO_MEMORY;
1233 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1234 posix_open = True;
1235 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1236 new_dos_attributes = 0;
1237 } else {
1238 /* We add aARCH to this as this mode is only used if the file is
1239 * created new. */
1240 unx_mode = unix_mode(conn, new_dos_attributes | aARCH, fname,
1241 parent_dir);
1244 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1245 "access_mask=0x%x share_access=0x%x "
1246 "create_disposition = 0x%x create_options=0x%x "
1247 "unix mode=0%o oplock_request=%d\n",
1248 fname, new_dos_attributes, access_mask, share_access,
1249 create_disposition, create_options, unx_mode,
1250 oplock_request));
1252 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
1253 DEBUG(0, ("No smb request but not an internal only open!\n"));
1254 return NT_STATUS_INTERNAL_ERROR;
1258 * Only non-internal opens can be deferred at all
1261 if ((req != NULL)
1262 && ((pml = get_open_deferred_message(req->mid)) != NULL)) {
1263 struct deferred_open_record *state =
1264 (struct deferred_open_record *)pml->private_data.data;
1266 /* Remember the absolute time of the original
1267 request with this mid. We'll use it later to
1268 see if this has timed out. */
1270 request_time = pml->request_time;
1272 /* Remove the deferred open entry under lock. */
1273 lck = get_share_mode_lock(talloc_tos(), state->id, NULL, NULL,
1274 NULL);
1275 if (lck == NULL) {
1276 DEBUG(0, ("could not get share mode lock\n"));
1277 } else {
1278 del_deferred_open_entry(lck, req->mid);
1279 TALLOC_FREE(lck);
1282 /* Ensure we don't reprocess this message. */
1283 remove_deferred_open_smb_message(req->mid);
1286 status = check_name(conn, fname);
1287 if (!NT_STATUS_IS_OK(status)) {
1288 return status;
1291 if (!posix_open) {
1292 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
1293 if (file_existed) {
1294 existing_dos_attributes = dos_mode(conn, fname, psbuf);
1298 /* ignore any oplock requests if oplocks are disabled */
1299 if (!lp_oplocks(SNUM(conn)) || global_client_failed_oplock_break ||
1300 IS_VETO_OPLOCK_PATH(conn, fname)) {
1301 /* Mask off everything except the private Samba bits. */
1302 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
1305 /* this is for OS/2 long file names - say we don't support them */
1306 if (!lp_posix_pathnames() && strstr(fname,".+,;=[].")) {
1307 /* OS/2 Workplace shell fix may be main code stream in a later
1308 * release. */
1309 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1310 "supported.\n"));
1311 if (use_nt_status()) {
1312 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1314 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
1317 switch( create_disposition ) {
1319 * Currently we're using FILE_SUPERSEDE as the same as
1320 * FILE_OVERWRITE_IF but they really are
1321 * different. FILE_SUPERSEDE deletes an existing file
1322 * (requiring delete access) then recreates it.
1324 case FILE_SUPERSEDE:
1325 /* If file exists replace/overwrite. If file doesn't
1326 * exist create. */
1327 flags2 |= (O_CREAT | O_TRUNC);
1328 break;
1330 case FILE_OVERWRITE_IF:
1331 /* If file exists replace/overwrite. If file doesn't
1332 * exist create. */
1333 flags2 |= (O_CREAT | O_TRUNC);
1334 break;
1336 case FILE_OPEN:
1337 /* If file exists open. If file doesn't exist error. */
1338 if (!file_existed) {
1339 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1340 "requested for file %s and file "
1341 "doesn't exist.\n", fname ));
1342 errno = ENOENT;
1343 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1345 break;
1347 case FILE_OVERWRITE:
1348 /* If file exists overwrite. If file doesn't exist
1349 * error. */
1350 if (!file_existed) {
1351 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1352 "requested for file %s and file "
1353 "doesn't exist.\n", fname ));
1354 errno = ENOENT;
1355 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1357 flags2 |= O_TRUNC;
1358 break;
1360 case FILE_CREATE:
1361 /* If file exists error. If file doesn't exist
1362 * create. */
1363 if (file_existed) {
1364 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1365 "requested for file %s and file "
1366 "already exists.\n", fname ));
1367 if (S_ISDIR(psbuf->st_mode)) {
1368 errno = EISDIR;
1369 } else {
1370 errno = EEXIST;
1372 return map_nt_error_from_unix(errno);
1374 flags2 |= (O_CREAT|O_EXCL);
1375 break;
1377 case FILE_OPEN_IF:
1378 /* If file exists open. If file doesn't exist
1379 * create. */
1380 flags2 |= O_CREAT;
1381 break;
1383 default:
1384 return NT_STATUS_INVALID_PARAMETER;
1387 /* We only care about matching attributes on file exists and
1388 * overwrite. */
1390 if (!posix_open && file_existed && ((create_disposition == FILE_OVERWRITE) ||
1391 (create_disposition == FILE_OVERWRITE_IF))) {
1392 if (!open_match_attributes(conn, fname,
1393 existing_dos_attributes,
1394 new_dos_attributes, psbuf->st_mode,
1395 unx_mode, &new_unx_mode)) {
1396 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1397 "for file %s (%x %x) (0%o, 0%o)\n",
1398 fname, existing_dos_attributes,
1399 new_dos_attributes,
1400 (unsigned int)psbuf->st_mode,
1401 (unsigned int)unx_mode ));
1402 errno = EACCES;
1403 return NT_STATUS_ACCESS_DENIED;
1407 /* This is a nasty hack - must fix... JRA. */
1408 if (access_mask == MAXIMUM_ALLOWED_ACCESS) {
1409 open_access_mask = access_mask = FILE_GENERIC_ALL;
1413 * Convert GENERIC bits to specific bits.
1416 se_map_generic(&access_mask, &file_generic_mapping);
1417 open_access_mask = access_mask;
1419 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1420 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
1423 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1424 "access_mask=0x%x\n", fname, access_mask ));
1427 * Note that we ignore the append flag as append does not
1428 * mean the same thing under DOS and Unix.
1431 if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
1432 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1433 /* DENY_DOS opens are always underlying read-write on the
1434 file handle, no matter what the requested access mask
1435 says. */
1436 if ((create_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
1437 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|FILE_READ_EA|FILE_EXECUTE)) {
1438 flags = O_RDWR;
1439 } else {
1440 flags = O_WRONLY;
1442 } else {
1443 flags = O_RDONLY;
1447 * Currently we only look at FILE_WRITE_THROUGH for create options.
1450 #if defined(O_SYNC)
1451 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
1452 flags2 |= O_SYNC;
1454 #endif /* O_SYNC */
1456 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
1457 flags2 |= O_APPEND;
1460 if (!posix_open && !CAN_WRITE(conn)) {
1462 * We should really return a permission denied error if either
1463 * O_CREAT or O_TRUNC are set, but for compatibility with
1464 * older versions of Samba we just AND them out.
1466 flags2 &= ~(O_CREAT|O_TRUNC);
1470 * Ensure we can't write on a read-only share or file.
1473 if (flags != O_RDONLY && file_existed &&
1474 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
1475 DEBUG(5,("open_file_ntcreate: write access requested for "
1476 "file %s on read only %s\n",
1477 fname, !CAN_WRITE(conn) ? "share" : "file" ));
1478 errno = EACCES;
1479 return NT_STATUS_ACCESS_DENIED;
1482 fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
1483 fsp->share_access = share_access;
1484 fsp->fh->private_options = create_options;
1485 fsp->access_mask = open_access_mask; /* We change this to the
1486 * requested access_mask after
1487 * the open is done. */
1488 fsp->posix_open = posix_open;
1490 /* Ensure no SAMBA_PRIVATE bits can be set. */
1491 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1493 if (timeval_is_zero(&request_time)) {
1494 request_time = fsp->open_time;
1497 if (file_existed) {
1498 struct timespec old_write_time = get_mtimespec(psbuf);
1499 id = vfs_file_id_from_sbuf(conn, psbuf);
1501 lck = get_share_mode_lock(talloc_tos(), id,
1502 conn->connectpath,
1503 fname, &old_write_time);
1505 if (lck == NULL) {
1506 DEBUG(0, ("Could not get share mode lock\n"));
1507 return NT_STATUS_SHARING_VIOLATION;
1510 /* First pass - send break only on batch oplocks. */
1511 if ((req != NULL)
1512 && delay_for_oplocks(lck, fsp, req->mid, 1,
1513 oplock_request)) {
1514 schedule_defer_open(lck, request_time, req);
1515 TALLOC_FREE(lck);
1516 return NT_STATUS_SHARING_VIOLATION;
1519 /* Use the client requested access mask here, not the one we
1520 * open with. */
1521 status = open_mode_check(conn, fname, lck,
1522 access_mask, share_access,
1523 create_options, &file_existed);
1525 if (NT_STATUS_IS_OK(status)) {
1526 /* We might be going to allow this open. Check oplock
1527 * status again. */
1528 /* Second pass - send break for both batch or
1529 * exclusive oplocks. */
1530 if ((req != NULL)
1531 && delay_for_oplocks(lck, fsp, req->mid, 2,
1532 oplock_request)) {
1533 schedule_defer_open(lck, request_time, req);
1534 TALLOC_FREE(lck);
1535 return NT_STATUS_SHARING_VIOLATION;
1539 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
1540 /* DELETE_PENDING is not deferred for a second */
1541 TALLOC_FREE(lck);
1542 return status;
1545 if (!NT_STATUS_IS_OK(status)) {
1546 uint32 can_access_mask;
1547 bool can_access = True;
1549 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
1551 /* Check if this can be done with the deny_dos and fcb
1552 * calls. */
1553 if (create_options &
1554 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
1555 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
1556 if (req == NULL) {
1557 DEBUG(0, ("DOS open without an SMB "
1558 "request!\n"));
1559 TALLOC_FREE(lck);
1560 return NT_STATUS_INTERNAL_ERROR;
1563 /* Use the client requested access mask here,
1564 * not the one we open with. */
1565 status = fcb_or_dos_open(conn,
1566 fsp,
1567 fname,
1569 req->smbpid,
1570 req->vuid,
1571 access_mask,
1572 share_access,
1573 create_options);
1575 if (NT_STATUS_IS_OK(status)) {
1576 TALLOC_FREE(lck);
1577 if (pinfo) {
1578 *pinfo = FILE_WAS_OPENED;
1580 return NT_STATUS_OK;
1585 * This next line is a subtlety we need for
1586 * MS-Access. If a file open will fail due to share
1587 * permissions and also for security (access) reasons,
1588 * we need to return the access failed error, not the
1589 * share error. We can't open the file due to kernel
1590 * oplock deadlock (it's possible we failed above on
1591 * the open_mode_check()) so use a userspace check.
1594 if (flags & O_RDWR) {
1595 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
1596 } else if (flags & O_WRONLY) {
1597 can_access_mask = FILE_WRITE_DATA;
1598 } else {
1599 can_access_mask = FILE_READ_DATA;
1602 if (((can_access_mask & FILE_WRITE_DATA) && !CAN_WRITE(conn)) ||
1603 !can_access_file(conn,fname,psbuf,can_access_mask)) {
1604 can_access = False;
1608 * If we're returning a share violation, ensure we
1609 * cope with the braindead 1 second delay.
1612 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
1613 lp_defer_sharing_violations()) {
1614 struct timeval timeout;
1615 struct deferred_open_record state;
1616 int timeout_usecs;
1618 /* this is a hack to speed up torture tests
1619 in 'make test' */
1620 timeout_usecs = lp_parm_int(SNUM(conn),
1621 "smbd","sharedelay",
1622 SHARING_VIOLATION_USEC_WAIT);
1624 /* This is a relative time, added to the absolute
1625 request_time value to get the absolute timeout time.
1626 Note that if this is the second or greater time we enter
1627 this codepath for this particular request mid then
1628 request_time is left as the absolute time of the *first*
1629 time this request mid was processed. This is what allows
1630 the request to eventually time out. */
1632 timeout = timeval_set(0, timeout_usecs);
1634 /* Nothing actually uses state.delayed_for_oplocks
1635 but it's handy to differentiate in debug messages
1636 between a 30 second delay due to oplock break, and
1637 a 1 second delay for share mode conflicts. */
1639 state.delayed_for_oplocks = False;
1640 state.id = id;
1642 if ((req != NULL)
1643 && !request_timed_out(request_time,
1644 timeout)) {
1645 defer_open(lck, request_time, timeout,
1646 req, &state);
1650 TALLOC_FREE(lck);
1651 if (can_access) {
1653 * We have detected a sharing violation here
1654 * so return the correct error code
1656 status = NT_STATUS_SHARING_VIOLATION;
1657 } else {
1658 status = NT_STATUS_ACCESS_DENIED;
1660 return status;
1664 * We exit this block with the share entry *locked*.....
1668 SMB_ASSERT(!file_existed || (lck != NULL));
1671 * Ensure we pay attention to default ACLs on directories if required.
1674 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
1675 (def_acl = directory_has_default_acl(conn, parent_dir))) {
1676 unx_mode = 0777;
1679 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
1680 "access_mask = 0x%x, open_access_mask = 0x%x\n",
1681 (unsigned int)flags, (unsigned int)flags2,
1682 (unsigned int)unx_mode, (unsigned int)access_mask,
1683 (unsigned int)open_access_mask));
1686 * open_file strips any O_TRUNC flags itself.
1689 fsp_open = open_file(fsp, conn, req, parent_dir, newname, fname, psbuf,
1690 flags|flags2, unx_mode, access_mask,
1691 open_access_mask);
1693 if (!NT_STATUS_IS_OK(fsp_open)) {
1694 if (lck != NULL) {
1695 TALLOC_FREE(lck);
1697 return fsp_open;
1700 if (!file_existed) {
1701 struct timespec old_write_time = get_mtimespec(psbuf);
1703 * Deal with the race condition where two smbd's detect the
1704 * file doesn't exist and do the create at the same time. One
1705 * of them will win and set a share mode, the other (ie. this
1706 * one) should check if the requested share mode for this
1707 * create is allowed.
1711 * Now the file exists and fsp is successfully opened,
1712 * fsp->dev and fsp->inode are valid and should replace the
1713 * dev=0,inode=0 from a non existent file. Spotted by
1714 * Nadav Danieli <nadavd@exanet.com>. JRA.
1717 id = fsp->file_id;
1719 lck = get_share_mode_lock(talloc_tos(), id,
1720 conn->connectpath,
1721 fname, &old_write_time);
1723 if (lck == NULL) {
1724 DEBUG(0, ("open_file_ntcreate: Could not get share "
1725 "mode lock for %s\n", fname));
1726 fd_close(fsp);
1727 return NT_STATUS_SHARING_VIOLATION;
1730 /* First pass - send break only on batch oplocks. */
1731 if ((req != NULL)
1732 && delay_for_oplocks(lck, fsp, req->mid, 1,
1733 oplock_request)) {
1734 schedule_defer_open(lck, request_time, req);
1735 TALLOC_FREE(lck);
1736 fd_close(fsp);
1737 return NT_STATUS_SHARING_VIOLATION;
1740 status = open_mode_check(conn, fname, lck,
1741 access_mask, share_access,
1742 create_options, &file_existed);
1744 if (NT_STATUS_IS_OK(status)) {
1745 /* We might be going to allow this open. Check oplock
1746 * status again. */
1747 /* Second pass - send break for both batch or
1748 * exclusive oplocks. */
1749 if ((req != NULL)
1750 && delay_for_oplocks(lck, fsp, req->mid, 2,
1751 oplock_request)) {
1752 schedule_defer_open(lck, request_time, req);
1753 TALLOC_FREE(lck);
1754 fd_close(fsp);
1755 return NT_STATUS_SHARING_VIOLATION;
1759 if (!NT_STATUS_IS_OK(status)) {
1760 struct deferred_open_record state;
1762 fd_close(fsp);
1764 state.delayed_for_oplocks = False;
1765 state.id = id;
1767 /* Do it all over again immediately. In the second
1768 * round we will find that the file existed and handle
1769 * the DELETE_PENDING and FCB cases correctly. No need
1770 * to duplicate the code here. Essentially this is a
1771 * "goto top of this function", but don't tell
1772 * anybody... */
1774 if (req != NULL) {
1775 defer_open(lck, request_time, timeval_zero(),
1776 req, &state);
1778 TALLOC_FREE(lck);
1779 return status;
1783 * We exit this block with the share entry *locked*.....
1788 SMB_ASSERT(lck != NULL);
1790 /* note that we ignore failure for the following. It is
1791 basically a hack for NFS, and NFS will never set one of
1792 these only read them. Nobody but Samba can ever set a deny
1793 mode and we have already checked our more authoritative
1794 locking database for permission to set this deny mode. If
1795 the kernel refuses the operations then the kernel is wrong.
1796 note that GPFS supports it as well - jmcd */
1798 if (fsp->fh->fd != -1) {
1799 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access);
1800 if(ret_flock == -1 ){
1802 TALLOC_FREE(lck);
1803 fd_close(fsp);
1805 return NT_STATUS_SHARING_VIOLATION;
1810 * At this point onwards, we can guarentee that the share entry
1811 * is locked, whether we created the file or not, and that the
1812 * deny mode is compatible with all current opens.
1816 * If requested, truncate the file.
1819 if (flags2&O_TRUNC) {
1821 * We are modifing the file after open - update the stat
1822 * struct..
1824 if ((SMB_VFS_FTRUNCATE(fsp, 0) == -1) ||
1825 (SMB_VFS_FSTAT(fsp, psbuf)==-1)) {
1826 status = map_nt_error_from_unix(errno);
1827 TALLOC_FREE(lck);
1828 fd_close(fsp);
1829 return status;
1833 /* Record the options we were opened with. */
1834 fsp->share_access = share_access;
1835 fsp->fh->private_options = create_options;
1836 fsp->access_mask = access_mask;
1838 if (file_existed) {
1839 /* stat opens on existing files don't get oplocks. */
1840 if (is_stat_open(open_access_mask)) {
1841 fsp->oplock_type = NO_OPLOCK;
1844 if (!(flags2 & O_TRUNC)) {
1845 info = FILE_WAS_OPENED;
1846 } else {
1847 info = FILE_WAS_OVERWRITTEN;
1849 } else {
1850 info = FILE_WAS_CREATED;
1853 if (pinfo) {
1854 *pinfo = info;
1858 * Setup the oplock info in both the shared memory and
1859 * file structs.
1862 if (!set_file_oplock(fsp, fsp->oplock_type)) {
1863 /* Could not get the kernel oplock */
1864 fsp->oplock_type = NO_OPLOCK;
1867 if (info == FILE_WAS_OVERWRITTEN || info == FILE_WAS_CREATED || info == FILE_WAS_SUPERSEDED) {
1868 new_file_created = True;
1871 set_share_mode(lck, fsp, current_user.ut.uid, 0,
1872 fsp->oplock_type);
1874 /* Handle strange delete on close create semantics. */
1875 if (create_options & FILE_DELETE_ON_CLOSE) {
1877 status = can_set_delete_on_close(fsp, True, new_dos_attributes);
1879 if (!NT_STATUS_IS_OK(status)) {
1880 /* Remember to delete the mode we just added. */
1881 del_share_mode(lck, fsp);
1882 TALLOC_FREE(lck);
1883 fd_close(fsp);
1884 return status;
1886 /* Note that here we set the *inital* delete on close flag,
1887 not the regular one. The magic gets handled in close. */
1888 fsp->initial_delete_on_close = True;
1891 if (new_file_created) {
1892 /* Files should be initially set as archive */
1893 if (lp_map_archive(SNUM(conn)) ||
1894 lp_store_dos_attributes(SNUM(conn))) {
1895 if (!posix_open) {
1896 SMB_STRUCT_STAT tmp_sbuf;
1897 SET_STAT_INVALID(tmp_sbuf);
1898 if (file_set_dosmode(
1899 conn, fname,
1900 new_dos_attributes | aARCH,
1901 &tmp_sbuf, parent_dir,
1902 true) == 0) {
1903 unx_mode = tmp_sbuf.st_mode;
1910 * Take care of inherited ACLs on created files - if default ACL not
1911 * selected.
1914 if (!posix_open && !file_existed && !def_acl) {
1916 int saved_errno = errno; /* We might get ENOSYS in the next
1917 * call.. */
1919 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
1920 errno == ENOSYS) {
1921 errno = saved_errno; /* Ignore ENOSYS */
1924 } else if (new_unx_mode) {
1926 int ret = -1;
1928 /* Attributes need changing. File already existed. */
1931 int saved_errno = errno; /* We might get ENOSYS in the
1932 * next call.. */
1933 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
1935 if (ret == -1 && errno == ENOSYS) {
1936 errno = saved_errno; /* Ignore ENOSYS */
1937 } else {
1938 DEBUG(5, ("open_file_ntcreate: reset "
1939 "attributes of file %s to 0%o\n",
1940 fname, (unsigned int)new_unx_mode));
1941 ret = 0; /* Don't do the fchmod below. */
1945 if ((ret == -1) &&
1946 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
1947 DEBUG(5, ("open_file_ntcreate: failed to reset "
1948 "attributes of file %s to 0%o\n",
1949 fname, (unsigned int)new_unx_mode));
1952 /* If this is a successful open, we must remove any deferred open
1953 * records. */
1954 if (req != NULL) {
1955 del_deferred_open_entry(lck, req->mid);
1957 TALLOC_FREE(lck);
1959 return NT_STATUS_OK;
1962 /****************************************************************************
1963 Open a file with a share mode.
1964 ****************************************************************************/
1966 NTSTATUS open_file_ntcreate(connection_struct *conn,
1967 struct smb_request *req,
1968 const char *fname,
1969 SMB_STRUCT_STAT *psbuf,
1970 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1971 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1972 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1973 uint32 create_options, /* options such as delete on close. */
1974 uint32 new_dos_attributes, /* attributes used for new file. */
1975 int oplock_request, /* internal Samba oplock codes. */
1976 /* Information (FILE_EXISTS etc.) */
1977 int *pinfo,
1978 files_struct **result)
1980 NTSTATUS status;
1981 files_struct *fsp = NULL;
1983 *result = NULL;
1985 status = file_new(conn, &fsp);
1986 if(!NT_STATUS_IS_OK(status)) {
1987 return status;
1990 status = open_file_ntcreate_internal(conn,
1991 req,
1992 fname,
1993 psbuf,
1994 access_mask,
1995 share_access,
1996 create_disposition,
1997 create_options,
1998 new_dos_attributes,
1999 oplock_request,
2000 pinfo,
2001 fsp);
2003 if(!NT_STATUS_IS_OK(status)) {
2004 file_free(fsp);
2005 return status;
2008 *result = fsp;
2009 return status;
2012 /****************************************************************************
2013 Open a file for for write to ensure that we can fchmod it.
2014 ****************************************************************************/
2016 NTSTATUS open_file_fchmod(connection_struct *conn, const char *fname,
2017 SMB_STRUCT_STAT *psbuf, files_struct **result)
2019 files_struct *fsp = NULL;
2020 NTSTATUS status;
2022 if (!VALID_STAT(*psbuf)) {
2023 return NT_STATUS_INVALID_PARAMETER;
2026 status = file_new(conn, &fsp);
2027 if(!NT_STATUS_IS_OK(status)) {
2028 return status;
2031 /* note! we must use a non-zero desired access or we don't get
2032 a real file descriptor. Oh what a twisted web we weave. */
2033 status = open_file(fsp, conn, NULL, NULL, NULL, fname, psbuf, O_WRONLY,
2034 0, FILE_WRITE_DATA, FILE_WRITE_DATA);
2037 * This is not a user visible file open.
2038 * Don't set a share mode.
2041 if (!NT_STATUS_IS_OK(status)) {
2042 file_free(fsp);
2043 return status;
2046 *result = fsp;
2047 return NT_STATUS_OK;
2050 /****************************************************************************
2051 Close the fchmod file fd - ensure no locks are lost.
2052 ****************************************************************************/
2054 NTSTATUS close_file_fchmod(files_struct *fsp)
2056 NTSTATUS status = fd_close(fsp);
2057 file_free(fsp);
2058 return status;
2061 static NTSTATUS mkdir_internal(connection_struct *conn,
2062 const char *name,
2063 uint32 file_attributes,
2064 SMB_STRUCT_STAT *psbuf)
2066 mode_t mode;
2067 char *parent_dir;
2068 const char *dirname;
2069 NTSTATUS status;
2070 bool posix_open = false;
2072 if(!CAN_WRITE(conn)) {
2073 DEBUG(5,("mkdir_internal: failing create on read-only share "
2074 "%s\n", lp_servicename(SNUM(conn))));
2075 return NT_STATUS_ACCESS_DENIED;
2078 status = check_name(conn, name);
2079 if (!NT_STATUS_IS_OK(status)) {
2080 return status;
2083 if (!parent_dirname_talloc(talloc_tos(), name, &parent_dir,
2084 &dirname)) {
2085 return NT_STATUS_NO_MEMORY;
2088 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2089 posix_open = true;
2090 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2091 } else {
2092 mode = unix_mode(conn, aDIR, name, parent_dir);
2095 if (SMB_VFS_MKDIR(conn, name, mode) != 0) {
2096 return map_nt_error_from_unix(errno);
2099 /* Ensure we're checking for a symlink here.... */
2100 /* We don't want to get caught by a symlink racer. */
2102 if (SMB_VFS_LSTAT(conn, name, psbuf) == -1) {
2103 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2104 name, strerror(errno)));
2105 return map_nt_error_from_unix(errno);
2108 if (!S_ISDIR(psbuf->st_mode)) {
2109 DEBUG(0, ("Directory just '%s' created is not a directory\n",
2110 name));
2111 return NT_STATUS_ACCESS_DENIED;
2114 if (lp_store_dos_attributes(SNUM(conn))) {
2115 if (!posix_open) {
2116 file_set_dosmode(conn, name,
2117 file_attributes | aDIR, NULL,
2118 parent_dir,
2119 true);
2123 if (lp_inherit_perms(SNUM(conn))) {
2124 inherit_access_acl(conn, parent_dir, name, mode);
2127 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
2129 * Check if high bits should have been set,
2130 * then (if bits are missing): add them.
2131 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2132 * dir.
2134 if (mode & ~(S_IRWXU|S_IRWXG|S_IRWXO) && (mode & ~psbuf->st_mode)) {
2135 SMB_VFS_CHMOD(conn, name,
2136 psbuf->st_mode | (mode & ~psbuf->st_mode));
2140 /* Change the owner if required. */
2141 if (lp_inherit_owner(SNUM(conn))) {
2142 change_dir_owner_to_parent(conn, parent_dir, name, psbuf);
2145 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2146 name);
2148 return NT_STATUS_OK;
2151 /****************************************************************************
2152 Open a directory from an NT SMB call.
2153 ****************************************************************************/
2155 NTSTATUS open_directory(connection_struct *conn,
2156 struct smb_request *req,
2157 const char *fname,
2158 SMB_STRUCT_STAT *psbuf,
2159 uint32 access_mask,
2160 uint32 share_access,
2161 uint32 create_disposition,
2162 uint32 create_options,
2163 uint32 file_attributes,
2164 int *pinfo,
2165 files_struct **result)
2167 files_struct *fsp = NULL;
2168 bool dir_existed = VALID_STAT(*psbuf) ? True : False;
2169 struct share_mode_lock *lck = NULL;
2170 NTSTATUS status;
2171 struct timespec mtimespec;
2172 int info = 0;
2174 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2175 "share_access = 0x%x create_options = 0x%x, "
2176 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2177 fname,
2178 (unsigned int)access_mask,
2179 (unsigned int)share_access,
2180 (unsigned int)create_options,
2181 (unsigned int)create_disposition,
2182 (unsigned int)file_attributes));
2184 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
2185 (conn->fs_capabilities & FILE_NAMED_STREAMS) &&
2186 is_ntfs_stream_name(fname)) {
2187 DEBUG(2, ("open_directory: %s is a stream name!\n", fname));
2188 return NT_STATUS_NOT_A_DIRECTORY;
2191 switch( create_disposition ) {
2192 case FILE_OPEN:
2194 info = FILE_WAS_OPENED;
2197 * We want to follow symlinks here.
2200 if (SMB_VFS_STAT(conn, fname, psbuf) != 0) {
2201 return map_nt_error_from_unix(errno);
2204 break;
2206 case FILE_CREATE:
2208 /* If directory exists error. If directory doesn't
2209 * exist create. */
2211 status = mkdir_internal(conn,
2212 fname,
2213 file_attributes,
2214 psbuf);
2216 if (!NT_STATUS_IS_OK(status)) {
2217 DEBUG(2, ("open_directory: unable to create "
2218 "%s. Error was %s\n", fname,
2219 nt_errstr(status)));
2220 return status;
2223 info = FILE_WAS_CREATED;
2224 break;
2226 case FILE_OPEN_IF:
2228 * If directory exists open. If directory doesn't
2229 * exist create.
2232 status = mkdir_internal(conn,
2233 fname,
2234 file_attributes,
2235 psbuf);
2237 if (NT_STATUS_IS_OK(status)) {
2238 info = FILE_WAS_CREATED;
2241 if (NT_STATUS_EQUAL(status,
2242 NT_STATUS_OBJECT_NAME_COLLISION)) {
2243 info = FILE_WAS_OPENED;
2244 status = NT_STATUS_OK;
2247 break;
2249 case FILE_SUPERSEDE:
2250 case FILE_OVERWRITE:
2251 case FILE_OVERWRITE_IF:
2252 default:
2253 DEBUG(5,("open_directory: invalid create_disposition "
2254 "0x%x for directory %s\n",
2255 (unsigned int)create_disposition, fname));
2256 return NT_STATUS_INVALID_PARAMETER;
2259 if(!S_ISDIR(psbuf->st_mode)) {
2260 DEBUG(5,("open_directory: %s is not a directory !\n",
2261 fname ));
2262 return NT_STATUS_NOT_A_DIRECTORY;
2265 status = file_new(conn, &fsp);
2266 if(!NT_STATUS_IS_OK(status)) {
2267 return status;
2271 * Setup the files_struct for it.
2274 fsp->mode = psbuf->st_mode;
2275 fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
2276 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
2277 fsp->file_pid = req ? req->smbpid : 0;
2278 fsp->can_lock = False;
2279 fsp->can_read = False;
2280 fsp->can_write = False;
2282 fsp->share_access = share_access;
2283 fsp->fh->private_options = create_options;
2284 fsp->access_mask = access_mask;
2286 fsp->print_file = False;
2287 fsp->modified = False;
2288 fsp->oplock_type = NO_OPLOCK;
2289 fsp->sent_oplock_break = NO_BREAK_SENT;
2290 fsp->is_directory = True;
2291 fsp->is_stat = False;
2292 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
2294 string_set(&fsp->fsp_name,fname);
2296 mtimespec = get_mtimespec(psbuf);
2298 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
2299 conn->connectpath,
2300 fname, &mtimespec);
2302 if (lck == NULL) {
2303 DEBUG(0, ("open_directory: Could not get share mode lock for %s\n", fname));
2304 file_free(fsp);
2305 return NT_STATUS_SHARING_VIOLATION;
2308 status = open_mode_check(conn, fname, lck,
2309 access_mask, share_access,
2310 create_options, &dir_existed);
2312 if (!NT_STATUS_IS_OK(status)) {
2313 TALLOC_FREE(lck);
2314 file_free(fsp);
2315 return status;
2318 set_share_mode(lck, fsp, current_user.ut.uid, 0, NO_OPLOCK);
2320 /* For directories the delete on close bit at open time seems
2321 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2322 if (create_options & FILE_DELETE_ON_CLOSE) {
2323 status = can_set_delete_on_close(fsp, True, 0);
2324 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
2325 TALLOC_FREE(lck);
2326 file_free(fsp);
2327 return status;
2330 if (NT_STATUS_IS_OK(status)) {
2331 /* Note that here we set the *inital* delete on close flag,
2332 not the regular one. The magic gets handled in close. */
2333 fsp->initial_delete_on_close = True;
2337 TALLOC_FREE(lck);
2339 if (pinfo) {
2340 *pinfo = info;
2343 *result = fsp;
2344 return NT_STATUS_OK;
2347 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req, const char *directory)
2349 NTSTATUS status;
2350 SMB_STRUCT_STAT sbuf;
2351 files_struct *fsp;
2353 SET_STAT_INVALID(sbuf);
2355 status = open_directory(conn, req, directory, &sbuf,
2356 FILE_READ_ATTRIBUTES, /* Just a stat open */
2357 FILE_SHARE_NONE, /* Ignored for stat opens */
2358 FILE_CREATE,
2360 FILE_ATTRIBUTE_DIRECTORY,
2361 NULL,
2362 &fsp);
2364 if (NT_STATUS_IS_OK(status)) {
2365 close_file(fsp, NORMAL_CLOSE);
2368 return status;
2371 /****************************************************************************
2372 Open a pseudo-file (no locking checks - a 'stat' open).
2373 ****************************************************************************/
2375 NTSTATUS open_file_stat(connection_struct *conn, struct smb_request *req,
2376 const char *fname, SMB_STRUCT_STAT *psbuf,
2377 files_struct **result)
2379 files_struct *fsp = NULL;
2380 NTSTATUS status;
2382 if (!VALID_STAT(*psbuf)) {
2383 return NT_STATUS_INVALID_PARAMETER;
2386 /* Can't 'stat' open directories. */
2387 if(S_ISDIR(psbuf->st_mode)) {
2388 return NT_STATUS_FILE_IS_A_DIRECTORY;
2391 status = file_new(conn, &fsp);
2392 if(!NT_STATUS_IS_OK(status)) {
2393 return status;
2396 DEBUG(5,("open_file_stat: 'opening' file %s\n", fname));
2399 * Setup the files_struct for it.
2402 fsp->mode = psbuf->st_mode;
2403 fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
2404 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
2405 fsp->file_pid = req ? req->smbpid : 0;
2406 fsp->can_lock = False;
2407 fsp->can_read = False;
2408 fsp->can_write = False;
2409 fsp->print_file = False;
2410 fsp->modified = False;
2411 fsp->oplock_type = NO_OPLOCK;
2412 fsp->sent_oplock_break = NO_BREAK_SENT;
2413 fsp->is_directory = False;
2414 fsp->is_stat = True;
2415 string_set(&fsp->fsp_name,fname);
2417 *result = fsp;
2418 return NT_STATUS_OK;
2421 /****************************************************************************
2422 Receive notification that one of our open files has been renamed by another
2423 smbd process.
2424 ****************************************************************************/
2426 void msg_file_was_renamed(struct messaging_context *msg,
2427 void *private_data,
2428 uint32_t msg_type,
2429 struct server_id server_id,
2430 DATA_BLOB *data)
2432 files_struct *fsp;
2433 char *frm = (char *)data->data;
2434 struct file_id id;
2435 const char *sharepath;
2436 const char *newname;
2437 size_t sp_len;
2439 if (data->data == NULL
2440 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
2441 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
2442 (int)data->length));
2443 return;
2446 /* Unpack the message. */
2447 pull_file_id_16(frm, &id);
2448 sharepath = &frm[16];
2449 newname = sharepath + strlen(sharepath) + 1;
2450 sp_len = strlen(sharepath);
2452 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
2453 "file_id %s\n",
2454 sharepath, newname, file_id_string_tos(&id)));
2456 for(fsp = file_find_di_first(id); fsp; fsp = file_find_di_next(fsp)) {
2457 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
2458 DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n",
2459 fsp->fnum, fsp->fsp_name, newname ));
2460 string_set(&fsp->fsp_name, newname);
2461 } else {
2462 /* TODO. JRA. */
2463 /* Now we have the complete path we can work out if this is
2464 actually within this share and adjust newname accordingly. */
2465 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
2466 "not sharepath %s) "
2467 "fnum %d from %s -> %s\n",
2468 fsp->conn->connectpath,
2469 sharepath,
2470 fsp->fnum,
2471 fsp->fsp_name,
2472 newname ));
2477 struct case_semantics_state {
2478 connection_struct *conn;
2479 bool case_sensitive;
2480 bool case_preserve;
2481 bool short_case_preserve;
2484 /****************************************************************************
2485 Restore case semantics.
2486 ****************************************************************************/
2487 static int restore_case_semantics(struct case_semantics_state *state)
2489 state->conn->case_sensitive = state->case_sensitive;
2490 state->conn->case_preserve = state->case_preserve;
2491 state->conn->short_case_preserve = state->short_case_preserve;
2492 return 0;
2495 /****************************************************************************
2496 Save case semantics.
2497 ****************************************************************************/
2498 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
2499 connection_struct *conn)
2501 struct case_semantics_state *result;
2503 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
2504 DEBUG(0, ("talloc failed\n"));
2505 return NULL;
2508 result->conn = conn;
2509 result->case_sensitive = conn->case_sensitive;
2510 result->case_preserve = conn->case_preserve;
2511 result->short_case_preserve = conn->short_case_preserve;
2513 /* Set to POSIX. */
2514 conn->case_sensitive = True;
2515 conn->case_preserve = True;
2516 conn->short_case_preserve = True;
2518 talloc_set_destructor(result, restore_case_semantics);
2520 return result;
2524 * If a main file is opened for delete, all streams need to be checked for
2525 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
2526 * If that works, delete them all by setting the delete on close and close.
2529 static NTSTATUS open_streams_for_delete(connection_struct *conn,
2530 const char *fname)
2532 struct stream_struct *stream_info;
2533 files_struct **streams;
2534 int i;
2535 unsigned int num_streams;
2536 TALLOC_CTX *frame = talloc_stackframe();
2537 NTSTATUS status;
2539 status = SMB_VFS_STREAMINFO(conn, NULL, fname, talloc_tos(),
2540 &num_streams, &stream_info);
2542 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
2543 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
2544 DEBUG(10, ("no streams around\n"));
2545 TALLOC_FREE(frame);
2546 return NT_STATUS_OK;
2549 if (!NT_STATUS_IS_OK(status)) {
2550 DEBUG(10, ("SMB_VFS_STREAMINFO failed: %s\n",
2551 nt_errstr(status)));
2552 goto fail;
2555 DEBUG(10, ("open_streams_for_delete found %d streams\n",
2556 num_streams));
2558 if (num_streams == 0) {
2559 TALLOC_FREE(frame);
2560 return NT_STATUS_OK;
2563 streams = TALLOC_ARRAY(talloc_tos(), files_struct *, num_streams);
2564 if (streams == NULL) {
2565 DEBUG(0, ("talloc failed\n"));
2566 status = NT_STATUS_NO_MEMORY;
2567 goto fail;
2570 for (i=0; i<num_streams; i++) {
2571 char *streamname;
2573 if (strequal(stream_info[i].name, "::$DATA")) {
2574 streams[i] = NULL;
2575 continue;
2578 streamname = talloc_asprintf(talloc_tos(), "%s%s", fname,
2579 stream_info[i].name);
2581 if (streamname == NULL) {
2582 DEBUG(0, ("talloc_aprintf failed\n"));
2583 status = NT_STATUS_NO_MEMORY;
2584 goto fail;
2587 status = create_file_unixpath
2588 (conn, /* conn */
2589 NULL, /* req */
2590 streamname, /* fname */
2591 DELETE_ACCESS, /* access_mask */
2592 FILE_SHARE_READ | FILE_SHARE_WRITE
2593 | FILE_SHARE_DELETE, /* share_access */
2594 FILE_OPEN, /* create_disposition*/
2595 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* create_options */
2596 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
2597 0, /* oplock_request */
2598 0, /* allocation_size */
2599 NULL, /* sd */
2600 NULL, /* ea_list */
2601 &streams[i], /* result */
2602 NULL, /* pinfo */
2603 NULL); /* psbuf */
2605 TALLOC_FREE(streamname);
2607 if (!NT_STATUS_IS_OK(status)) {
2608 DEBUG(10, ("Could not open stream %s: %s\n",
2609 streamname, nt_errstr(status)));
2610 break;
2615 * don't touch the variable "status" beyond this point :-)
2618 for (i -= 1 ; i >= 0; i--) {
2619 if (streams[i] == NULL) {
2620 continue;
2623 DEBUG(10, ("Closing stream # %d, %s\n", i,
2624 streams[i]->fsp_name));
2625 close_file(streams[i], NORMAL_CLOSE);
2628 fail:
2629 TALLOC_FREE(frame);
2630 return status;
2634 * Wrapper around open_file_ntcreate and open_directory
2637 NTSTATUS create_file_unixpath(connection_struct *conn,
2638 struct smb_request *req,
2639 const char *fname,
2640 uint32_t access_mask,
2641 uint32_t share_access,
2642 uint32_t create_disposition,
2643 uint32_t create_options,
2644 uint32_t file_attributes,
2645 uint32_t oplock_request,
2646 SMB_BIG_UINT allocation_size,
2647 struct security_descriptor *sd,
2648 struct ea_list *ea_list,
2650 files_struct **result,
2651 int *pinfo,
2652 SMB_STRUCT_STAT *psbuf)
2654 SMB_STRUCT_STAT sbuf;
2655 int info = FILE_WAS_OPENED;
2656 files_struct *base_fsp = NULL;
2657 files_struct *fsp = NULL;
2658 NTSTATUS status;
2660 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
2661 "file_attributes = 0x%x, share_access = 0x%x, "
2662 "create_disposition = 0x%x create_options = 0x%x "
2663 "oplock_request = 0x%x ea_list = 0x%p, sd = 0x%p, "
2664 "fname = %s\n",
2665 (unsigned int)access_mask,
2666 (unsigned int)file_attributes,
2667 (unsigned int)share_access,
2668 (unsigned int)create_disposition,
2669 (unsigned int)create_options,
2670 (unsigned int)oplock_request,
2671 ea_list, sd, fname));
2673 if (create_options & FILE_OPEN_BY_FILE_ID) {
2674 status = NT_STATUS_NOT_SUPPORTED;
2675 goto fail;
2678 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
2679 status = NT_STATUS_INVALID_PARAMETER;
2680 goto fail;
2683 if (req == NULL) {
2684 oplock_request |= INTERNAL_OPEN_ONLY;
2687 if (psbuf != NULL) {
2688 sbuf = *psbuf;
2690 else {
2691 if (SMB_VFS_STAT(conn, fname, &sbuf) == -1) {
2692 SET_STAT_INVALID(sbuf);
2696 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
2697 && (access_mask & DELETE_ACCESS)
2698 && !is_ntfs_stream_name(fname)) {
2700 * We can't open a file with DELETE access if any of the
2701 * streams is open without FILE_SHARE_DELETE
2703 status = open_streams_for_delete(conn, fname);
2705 if (!NT_STATUS_IS_OK(status)) {
2706 goto fail;
2710 /* This is the correct thing to do (check every time) but can_delete
2711 * is expensive (it may have to read the parent directory
2712 * permissions). So for now we're not doing it unless we have a strong
2713 * hint the client is really going to delete this file. If the client
2714 * is forcing FILE_CREATE let the filesystem take care of the
2715 * permissions. */
2717 /* Setting FILE_SHARE_DELETE is the hint. */
2719 if (lp_acl_check_permissions(SNUM(conn))
2720 && (create_disposition != FILE_CREATE)
2721 && (share_access & FILE_SHARE_DELETE)
2722 && (access_mask & DELETE_ACCESS)
2723 && (!can_delete_file_in_directory(conn, fname))) {
2724 status = NT_STATUS_ACCESS_DENIED;
2725 goto fail;
2728 #if 0
2729 /* We need to support SeSecurityPrivilege for this. */
2730 if ((access_mask & SEC_RIGHT_SYSTEM_SECURITY) &&
2731 !user_has_privileges(current_user.nt_user_token,
2732 &se_security)) {
2733 status = NT_STATUS_PRIVILEGE_NOT_HELD;
2734 goto fail;
2736 #endif
2738 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
2739 && is_ntfs_stream_name(fname)
2740 && (!(create_options & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
2741 char *base;
2742 uint32 base_create_disposition;
2744 if (create_options & FILE_DIRECTORY_FILE) {
2745 status = NT_STATUS_NOT_A_DIRECTORY;
2746 goto fail;
2749 status = split_ntfs_stream_name(talloc_tos(), fname,
2750 &base, NULL);
2751 if (!NT_STATUS_IS_OK(status)) {
2752 DEBUG(10, ("create_file_unixpath: "
2753 "split_ntfs_stream_name failed: %s\n",
2754 nt_errstr(status)));
2755 goto fail;
2758 SMB_ASSERT(!is_ntfs_stream_name(base)); /* paranoia.. */
2760 switch (create_disposition) {
2761 case FILE_OPEN:
2762 base_create_disposition = FILE_OPEN;
2763 break;
2764 default:
2765 base_create_disposition = FILE_OPEN_IF;
2766 break;
2769 DEBUG(10, ("Recursing into create_file_unixpath for "
2770 "base %s\n", base));
2772 status = create_file_unixpath(conn, NULL, base, 0,
2773 FILE_SHARE_READ
2774 | FILE_SHARE_WRITE
2775 | FILE_SHARE_DELETE,
2776 base_create_disposition,
2777 0, 0, 0, 0, NULL, NULL,
2778 &base_fsp, NULL, NULL);
2779 if (!NT_STATUS_IS_OK(status)) {
2780 DEBUG(10, ("create_file_unixpath for base %s failed: "
2781 "%s\n", base, nt_errstr(status)));
2782 goto fail;
2784 /* we don't need to low level fd */
2785 fd_close(base_fsp);
2789 * If it's a request for a directory open, deal with it separately.
2792 if (create_options & FILE_DIRECTORY_FILE) {
2794 if (create_options & FILE_NON_DIRECTORY_FILE) {
2795 status = NT_STATUS_INVALID_PARAMETER;
2796 goto fail;
2799 /* Can't open a temp directory. IFS kit test. */
2800 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
2801 status = NT_STATUS_INVALID_PARAMETER;
2802 goto fail;
2806 * We will get a create directory here if the Win32
2807 * app specified a security descriptor in the
2808 * CreateDirectory() call.
2811 oplock_request = 0;
2812 status = open_directory(
2813 conn, req, fname, &sbuf, access_mask, share_access,
2814 create_disposition, create_options, file_attributes,
2815 &info, &fsp);
2816 } else {
2819 * Ordinary file case.
2822 if (base_fsp) {
2824 * We're opening the stream element of a base_fsp
2825 * we already opened. We need to initialize
2826 * the fsp first, and set up the base_fsp pointer.
2828 status = file_new(conn, &fsp);
2829 if(!NT_STATUS_IS_OK(status)) {
2830 goto fail;
2833 fsp->base_fsp = base_fsp;
2835 status = open_file_ntcreate_internal(conn,
2836 req,
2837 fname,
2838 &sbuf,
2839 access_mask,
2840 share_access,
2841 create_disposition,
2842 create_options,
2843 file_attributes,
2844 oplock_request,
2845 &info,
2846 fsp);
2848 if(!NT_STATUS_IS_OK(status)) {
2849 file_free(fsp);
2850 fsp = NULL;
2852 } else {
2853 status = open_file_ntcreate(
2854 conn, req, fname, &sbuf, access_mask, share_access,
2855 create_disposition, create_options, file_attributes,
2856 oplock_request, &info, &fsp);
2859 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
2861 /* A stream open never opens a directory */
2863 if (base_fsp) {
2864 status = NT_STATUS_FILE_IS_A_DIRECTORY;
2865 goto fail;
2869 * Fail the open if it was explicitly a non-directory
2870 * file.
2873 if (create_options & FILE_NON_DIRECTORY_FILE) {
2874 status = NT_STATUS_FILE_IS_A_DIRECTORY;
2875 goto fail;
2878 oplock_request = 0;
2879 status = open_directory(
2880 conn, req, fname, &sbuf, access_mask,
2881 share_access, create_disposition,
2882 create_options, file_attributes,
2883 &info, &fsp);
2887 if (!NT_STATUS_IS_OK(status)) {
2888 goto fail;
2891 fsp->base_fsp = base_fsp;
2894 * According to the MS documentation, the only time the security
2895 * descriptor is applied to the opened file is iff we *created* the
2896 * file; an existing file stays the same.
2898 * Also, it seems (from observation) that you can open the file with
2899 * any access mask but you can still write the sd. We need to override
2900 * the granted access before we call set_sd
2901 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
2904 if ((sd != NULL) && (info == FILE_WAS_CREATED)
2905 && lp_nt_acl_support(SNUM(conn))) {
2907 uint32_t sec_info_sent = ALL_SECURITY_INFORMATION;
2908 uint32_t saved_access_mask = fsp->access_mask;
2910 if (sd->owner_sid == NULL) {
2911 sec_info_sent &= ~OWNER_SECURITY_INFORMATION;
2913 if (sd->group_sid == NULL) {
2914 sec_info_sent &= ~GROUP_SECURITY_INFORMATION;
2916 if (sd->sacl == NULL) {
2917 sec_info_sent &= ~SACL_SECURITY_INFORMATION;
2919 if (sd->dacl == NULL) {
2920 sec_info_sent &= ~DACL_SECURITY_INFORMATION;
2923 fsp->access_mask = FILE_GENERIC_ALL;
2925 status = SMB_VFS_FSET_NT_ACL(fsp, sec_info_sent, sd);
2927 fsp->access_mask = saved_access_mask;
2929 if (!NT_STATUS_IS_OK(status)) {
2930 goto fail;
2934 if ((ea_list != NULL) && (info == FILE_WAS_CREATED)) {
2935 status = set_ea(conn, fsp, fname, ea_list);
2936 if (!NT_STATUS_IS_OK(status)) {
2937 goto fail;
2941 if (!fsp->is_directory && S_ISDIR(sbuf.st_mode)) {
2942 status = NT_STATUS_ACCESS_DENIED;
2943 goto fail;
2946 /* Save the requested allocation size. */
2947 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
2948 if (allocation_size
2949 && (allocation_size > sbuf.st_size)) {
2950 fsp->initial_allocation_size = smb_roundup(
2951 fsp->conn, allocation_size);
2952 if (fsp->is_directory) {
2953 /* Can't set allocation size on a directory. */
2954 status = NT_STATUS_ACCESS_DENIED;
2955 goto fail;
2957 if (vfs_allocate_file_space(
2958 fsp, fsp->initial_allocation_size) == -1) {
2959 status = NT_STATUS_DISK_FULL;
2960 goto fail;
2962 } else {
2963 fsp->initial_allocation_size = smb_roundup(
2964 fsp->conn, (SMB_BIG_UINT)sbuf.st_size);
2968 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
2970 *result = fsp;
2971 if (pinfo != NULL) {
2972 *pinfo = info;
2974 if (psbuf != NULL) {
2975 if ((fsp->fh == NULL) || (fsp->fh->fd == -1)) {
2976 *psbuf = sbuf;
2978 else {
2979 SMB_VFS_FSTAT(fsp, psbuf);
2982 return NT_STATUS_OK;
2984 fail:
2985 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
2987 if (fsp != NULL) {
2988 if (base_fsp && fsp->base_fsp == base_fsp) {
2990 * The close_file below will close
2991 * fsp->base_fsp.
2993 base_fsp = NULL;
2995 close_file(fsp, ERROR_CLOSE);
2996 fsp = NULL;
2998 if (base_fsp != NULL) {
2999 close_file(base_fsp, ERROR_CLOSE);
3000 base_fsp = NULL;
3002 return status;
3005 NTSTATUS create_file(connection_struct *conn,
3006 struct smb_request *req,
3007 uint16_t root_dir_fid,
3008 const char *fname,
3009 uint32_t access_mask,
3010 uint32_t share_access,
3011 uint32_t create_disposition,
3012 uint32_t create_options,
3013 uint32_t file_attributes,
3014 uint32_t oplock_request,
3015 SMB_BIG_UINT allocation_size,
3016 struct security_descriptor *sd,
3017 struct ea_list *ea_list,
3019 files_struct **result,
3020 int *pinfo,
3021 SMB_STRUCT_STAT *psbuf)
3023 struct case_semantics_state *case_state = NULL;
3024 SMB_STRUCT_STAT sbuf;
3025 int info = FILE_WAS_OPENED;
3026 files_struct *fsp = NULL;
3027 NTSTATUS status;
3029 DEBUG(10,("create_file: access_mask = 0x%x "
3030 "file_attributes = 0x%x, share_access = 0x%x, "
3031 "create_disposition = 0x%x create_options = 0x%x "
3032 "oplock_request = 0x%x "
3033 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
3034 "fname = %s\n",
3035 (unsigned int)access_mask,
3036 (unsigned int)file_attributes,
3037 (unsigned int)share_access,
3038 (unsigned int)create_disposition,
3039 (unsigned int)create_options,
3040 (unsigned int)oplock_request,
3041 (unsigned int)root_dir_fid,
3042 ea_list, sd, fname));
3045 * Get the file name.
3048 if (root_dir_fid != 0) {
3050 * This filename is relative to a directory fid.
3052 char *parent_fname = NULL;
3053 files_struct *dir_fsp = file_fsp(root_dir_fid);
3055 if (dir_fsp == NULL) {
3056 status = NT_STATUS_INVALID_HANDLE;
3057 goto fail;
3060 if (!dir_fsp->is_directory) {
3063 * Check to see if this is a mac fork of some kind.
3066 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
3067 is_ntfs_stream_name(fname)) {
3068 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3069 goto fail;
3073 we need to handle the case when we get a
3074 relative open relative to a file and the
3075 pathname is blank - this is a reopen!
3076 (hint from demyn plantenberg)
3079 status = NT_STATUS_INVALID_HANDLE;
3080 goto fail;
3083 if (ISDOT(dir_fsp->fsp_name)) {
3085 * We're at the toplevel dir, the final file name
3086 * must not contain ./, as this is filtered out
3087 * normally by srvstr_get_path and unix_convert
3088 * explicitly rejects paths containing ./.
3090 parent_fname = talloc_strdup(talloc_tos(), "");
3091 if (parent_fname == NULL) {
3092 status = NT_STATUS_NO_MEMORY;
3093 goto fail;
3095 } else {
3096 size_t dir_name_len = strlen(dir_fsp->fsp_name);
3099 * Copy in the base directory name.
3102 parent_fname = TALLOC_ARRAY(talloc_tos(), char,
3103 dir_name_len+2);
3104 if (parent_fname == NULL) {
3105 status = NT_STATUS_NO_MEMORY;
3106 goto fail;
3108 memcpy(parent_fname, dir_fsp->fsp_name,
3109 dir_name_len+1);
3112 * Ensure it ends in a '/'.
3113 * We used TALLOC_SIZE +2 to add space for the '/'.
3116 if(dir_name_len
3117 && (parent_fname[dir_name_len-1] != '\\')
3118 && (parent_fname[dir_name_len-1] != '/')) {
3119 parent_fname[dir_name_len] = '/';
3120 parent_fname[dir_name_len+1] = '\0';
3124 fname = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
3125 fname);
3126 if (fname == NULL) {
3127 status = NT_STATUS_NO_MEMORY;
3128 goto fail;
3133 * Check to see if this is a mac fork of some kind.
3136 if (is_ntfs_stream_name(fname)) {
3137 enum FAKE_FILE_TYPE fake_file_type;
3139 fake_file_type = is_fake_file(fname);
3141 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
3144 * Here we go! support for changing the disk quotas
3145 * --metze
3147 * We need to fake up to open this MAGIC QUOTA file
3148 * and return a valid FID.
3150 * w2k close this file directly after openening xp
3151 * also tries a QUERY_FILE_INFO on the file and then
3152 * close it
3155 status = open_fake_file(conn, fake_file_type, fname,
3156 access_mask, &fsp);
3157 if (!NT_STATUS_IS_OK(status)) {
3158 goto fail;
3161 ZERO_STRUCT(sbuf);
3162 goto done;
3165 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
3166 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3167 goto fail;
3171 if ((req != NULL) && (req->flags2 & FLAGS2_DFS_PATHNAMES)) {
3172 char *resolved_fname;
3174 status = resolve_dfspath(talloc_tos(), conn, true, fname,
3175 &resolved_fname);
3177 if (!NT_STATUS_IS_OK(status)) {
3179 * For PATH_NOT_COVERED we had
3180 * reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
3181 * ERRSRV, ERRbadpath);
3182 * Need to fix in callers
3184 goto fail;
3186 fname = resolved_fname;
3190 * Check if POSIX semantics are wanted.
3193 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
3194 case_state = set_posix_case_semantics(talloc_tos(), conn);
3195 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
3199 char *converted_fname;
3201 SET_STAT_INVALID(sbuf);
3203 status = unix_convert(talloc_tos(), conn, fname, False,
3204 &converted_fname, NULL, &sbuf);
3205 if (!NT_STATUS_IS_OK(status)) {
3206 goto fail;
3208 fname = converted_fname;
3211 TALLOC_FREE(case_state);
3213 /* All file access must go through check_name() */
3215 status = check_name(conn, fname);
3216 if (!NT_STATUS_IS_OK(status)) {
3217 goto fail;
3220 status = create_file_unixpath(
3221 conn, req, fname, access_mask, share_access,
3222 create_disposition, create_options, file_attributes,
3223 oplock_request, allocation_size, sd, ea_list,
3224 &fsp, &info, &sbuf);
3226 if (!NT_STATUS_IS_OK(status)) {
3227 goto fail;
3230 done:
3231 DEBUG(10, ("create_file: info=%d\n", info));
3233 *result = fsp;
3234 if (pinfo != NULL) {
3235 *pinfo = info;
3237 if (psbuf != NULL) {
3238 *psbuf = sbuf;
3240 return NT_STATUS_OK;
3242 fail:
3243 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
3245 if (fsp != NULL) {
3246 close_file(fsp, ERROR_CLOSE);
3247 fsp = NULL;
3249 return status;