Fix bug #6254 - PUT/GET produces an error in IPv6 to a smb-server(3.3)
[Samba.git] / source / smbd / open.c
blob535abcc26d61a112265f7b9dc63ad93e4dd832a8
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 bool clear_ads = false;
1197 struct file_id id;
1198 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1199 mode_t new_unx_mode = (mode_t)0;
1200 mode_t unx_mode = (mode_t)0;
1201 int info;
1202 uint32 existing_dos_attributes = 0;
1203 struct pending_message_list *pml = NULL;
1204 struct timeval request_time = timeval_zero();
1205 struct share_mode_lock *lck = NULL;
1206 uint32 open_access_mask = access_mask;
1207 NTSTATUS status;
1208 int ret_flock;
1209 char *parent_dir;
1210 const char *newname;
1212 ZERO_STRUCT(id);
1214 if (conn->printer) {
1216 * Printers are handled completely differently.
1217 * Most of the passed parameters are ignored.
1220 if (pinfo) {
1221 *pinfo = FILE_WAS_CREATED;
1224 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n", fname));
1226 return print_fsp_open(conn, fname, fsp, psbuf);
1229 if (!parent_dirname_talloc(talloc_tos(), fname, &parent_dir,
1230 &newname)) {
1231 return NT_STATUS_NO_MEMORY;
1234 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1235 posix_open = True;
1236 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1237 new_dos_attributes = 0;
1238 } else {
1239 /* We add aARCH to this as this mode is only used if the file is
1240 * created new. */
1241 unx_mode = unix_mode(conn, new_dos_attributes | aARCH, fname,
1242 parent_dir);
1245 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1246 "access_mask=0x%x share_access=0x%x "
1247 "create_disposition = 0x%x create_options=0x%x "
1248 "unix mode=0%o oplock_request=%d\n",
1249 fname, new_dos_attributes, access_mask, share_access,
1250 create_disposition, create_options, unx_mode,
1251 oplock_request));
1253 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
1254 DEBUG(0, ("No smb request but not an internal only open!\n"));
1255 return NT_STATUS_INTERNAL_ERROR;
1259 * Only non-internal opens can be deferred at all
1262 if ((req != NULL)
1263 && ((pml = get_open_deferred_message(req->mid)) != NULL)) {
1264 struct deferred_open_record *state =
1265 (struct deferred_open_record *)pml->private_data.data;
1267 /* Remember the absolute time of the original
1268 request with this mid. We'll use it later to
1269 see if this has timed out. */
1271 request_time = pml->request_time;
1273 /* Remove the deferred open entry under lock. */
1274 lck = get_share_mode_lock(talloc_tos(), state->id, NULL, NULL,
1275 NULL);
1276 if (lck == NULL) {
1277 DEBUG(0, ("could not get share mode lock\n"));
1278 } else {
1279 del_deferred_open_entry(lck, req->mid);
1280 TALLOC_FREE(lck);
1283 /* Ensure we don't reprocess this message. */
1284 remove_deferred_open_smb_message(req->mid);
1287 status = check_name(conn, fname);
1288 if (!NT_STATUS_IS_OK(status)) {
1289 return status;
1292 if (!posix_open) {
1293 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
1294 if (file_existed) {
1295 existing_dos_attributes = dos_mode(conn, fname, psbuf);
1299 /* ignore any oplock requests if oplocks are disabled */
1300 if (!lp_oplocks(SNUM(conn)) || global_client_failed_oplock_break ||
1301 IS_VETO_OPLOCK_PATH(conn, fname)) {
1302 /* Mask off everything except the private Samba bits. */
1303 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
1306 /* this is for OS/2 long file names - say we don't support them */
1307 if (!lp_posix_pathnames() && strstr(fname,".+,;=[].")) {
1308 /* OS/2 Workplace shell fix may be main code stream in a later
1309 * release. */
1310 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1311 "supported.\n"));
1312 if (use_nt_status()) {
1313 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1315 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
1318 switch( create_disposition ) {
1320 * Currently we're using FILE_SUPERSEDE as the same as
1321 * FILE_OVERWRITE_IF but they really are
1322 * different. FILE_SUPERSEDE deletes an existing file
1323 * (requiring delete access) then recreates it.
1325 case FILE_SUPERSEDE:
1326 /* If file exists replace/overwrite. If file doesn't
1327 * exist create. */
1328 flags2 |= (O_CREAT | O_TRUNC);
1329 clear_ads = true;
1330 break;
1332 case FILE_OVERWRITE_IF:
1333 /* If file exists replace/overwrite. If file doesn't
1334 * exist create. */
1335 flags2 |= (O_CREAT | O_TRUNC);
1336 clear_ads = true;
1337 break;
1339 case FILE_OPEN:
1340 /* If file exists open. If file doesn't exist error. */
1341 if (!file_existed) {
1342 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1343 "requested for file %s and file "
1344 "doesn't exist.\n", fname ));
1345 errno = ENOENT;
1346 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1348 break;
1350 case FILE_OVERWRITE:
1351 /* If file exists overwrite. If file doesn't exist
1352 * error. */
1353 if (!file_existed) {
1354 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1355 "requested for file %s and file "
1356 "doesn't exist.\n", fname ));
1357 errno = ENOENT;
1358 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1360 flags2 |= O_TRUNC;
1361 clear_ads = true;
1362 break;
1364 case FILE_CREATE:
1365 /* If file exists error. If file doesn't exist
1366 * create. */
1367 if (file_existed) {
1368 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1369 "requested for file %s and file "
1370 "already exists.\n", fname ));
1371 if (S_ISDIR(psbuf->st_mode)) {
1372 errno = EISDIR;
1373 } else {
1374 errno = EEXIST;
1376 return map_nt_error_from_unix(errno);
1378 flags2 |= (O_CREAT|O_EXCL);
1379 break;
1381 case FILE_OPEN_IF:
1382 /* If file exists open. If file doesn't exist
1383 * create. */
1384 flags2 |= O_CREAT;
1385 break;
1387 default:
1388 return NT_STATUS_INVALID_PARAMETER;
1391 /* We only care about matching attributes on file exists and
1392 * overwrite. */
1394 if (!posix_open && file_existed && ((create_disposition == FILE_OVERWRITE) ||
1395 (create_disposition == FILE_OVERWRITE_IF))) {
1396 if (!open_match_attributes(conn, fname,
1397 existing_dos_attributes,
1398 new_dos_attributes, psbuf->st_mode,
1399 unx_mode, &new_unx_mode)) {
1400 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1401 "for file %s (%x %x) (0%o, 0%o)\n",
1402 fname, existing_dos_attributes,
1403 new_dos_attributes,
1404 (unsigned int)psbuf->st_mode,
1405 (unsigned int)unx_mode ));
1406 errno = EACCES;
1407 return NT_STATUS_ACCESS_DENIED;
1411 /* This is a nasty hack - must fix... JRA. */
1412 if (access_mask == MAXIMUM_ALLOWED_ACCESS) {
1413 open_access_mask = access_mask = FILE_GENERIC_ALL;
1417 * Convert GENERIC bits to specific bits.
1420 se_map_generic(&access_mask, &file_generic_mapping);
1421 open_access_mask = access_mask;
1423 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1424 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
1427 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1428 "access_mask=0x%x\n", fname, access_mask ));
1431 * Note that we ignore the append flag as append does not
1432 * mean the same thing under DOS and Unix.
1435 if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
1436 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1437 /* DENY_DOS opens are always underlying read-write on the
1438 file handle, no matter what the requested access mask
1439 says. */
1440 if ((create_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
1441 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|FILE_READ_EA|FILE_EXECUTE)) {
1442 flags = O_RDWR;
1443 } else {
1444 flags = O_WRONLY;
1446 } else {
1447 flags = O_RDONLY;
1451 * Currently we only look at FILE_WRITE_THROUGH for create options.
1454 #if defined(O_SYNC)
1455 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
1456 flags2 |= O_SYNC;
1458 #endif /* O_SYNC */
1460 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
1461 flags2 |= O_APPEND;
1464 if (!posix_open && !CAN_WRITE(conn)) {
1466 * We should really return a permission denied error if either
1467 * O_CREAT or O_TRUNC are set, but for compatibility with
1468 * older versions of Samba we just AND them out.
1470 flags2 &= ~(O_CREAT|O_TRUNC);
1474 * Ensure we can't write on a read-only share or file.
1477 if (flags != O_RDONLY && file_existed &&
1478 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
1479 DEBUG(5,("open_file_ntcreate: write access requested for "
1480 "file %s on read only %s\n",
1481 fname, !CAN_WRITE(conn) ? "share" : "file" ));
1482 errno = EACCES;
1483 return NT_STATUS_ACCESS_DENIED;
1486 fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
1487 fsp->share_access = share_access;
1488 fsp->fh->private_options = create_options;
1489 fsp->access_mask = open_access_mask; /* We change this to the
1490 * requested access_mask after
1491 * the open is done. */
1492 fsp->posix_open = posix_open;
1494 /* Ensure no SAMBA_PRIVATE bits can be set. */
1495 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1497 if (timeval_is_zero(&request_time)) {
1498 request_time = fsp->open_time;
1501 if (file_existed) {
1502 struct timespec old_write_time = get_mtimespec(psbuf);
1503 id = vfs_file_id_from_sbuf(conn, psbuf);
1505 lck = get_share_mode_lock(talloc_tos(), id,
1506 conn->connectpath,
1507 fname, &old_write_time);
1509 if (lck == NULL) {
1510 DEBUG(0, ("Could not get share mode lock\n"));
1511 return NT_STATUS_SHARING_VIOLATION;
1514 /* First pass - send break only on batch oplocks. */
1515 if ((req != NULL)
1516 && delay_for_oplocks(lck, fsp, req->mid, 1,
1517 oplock_request)) {
1518 schedule_defer_open(lck, request_time, req);
1519 TALLOC_FREE(lck);
1520 return NT_STATUS_SHARING_VIOLATION;
1523 /* Use the client requested access mask here, not the one we
1524 * open with. */
1525 status = open_mode_check(conn, fname, lck,
1526 access_mask, share_access,
1527 create_options, &file_existed);
1529 if (NT_STATUS_IS_OK(status)) {
1530 /* We might be going to allow this open. Check oplock
1531 * status again. */
1532 /* Second pass - send break for both batch or
1533 * exclusive oplocks. */
1534 if ((req != NULL)
1535 && delay_for_oplocks(lck, fsp, req->mid, 2,
1536 oplock_request)) {
1537 schedule_defer_open(lck, request_time, req);
1538 TALLOC_FREE(lck);
1539 return NT_STATUS_SHARING_VIOLATION;
1543 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
1544 /* DELETE_PENDING is not deferred for a second */
1545 TALLOC_FREE(lck);
1546 return status;
1549 if (!NT_STATUS_IS_OK(status)) {
1550 uint32 can_access_mask;
1551 bool can_access = True;
1553 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
1555 /* Check if this can be done with the deny_dos and fcb
1556 * calls. */
1557 if (create_options &
1558 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
1559 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
1560 if (req == NULL) {
1561 DEBUG(0, ("DOS open without an SMB "
1562 "request!\n"));
1563 TALLOC_FREE(lck);
1564 return NT_STATUS_INTERNAL_ERROR;
1567 /* Use the client requested access mask here,
1568 * not the one we open with. */
1569 status = fcb_or_dos_open(conn,
1570 fsp,
1571 fname,
1573 req->smbpid,
1574 req->vuid,
1575 access_mask,
1576 share_access,
1577 create_options);
1579 if (NT_STATUS_IS_OK(status)) {
1580 TALLOC_FREE(lck);
1581 if (pinfo) {
1582 *pinfo = FILE_WAS_OPENED;
1584 return NT_STATUS_OK;
1589 * This next line is a subtlety we need for
1590 * MS-Access. If a file open will fail due to share
1591 * permissions and also for security (access) reasons,
1592 * we need to return the access failed error, not the
1593 * share error. We can't open the file due to kernel
1594 * oplock deadlock (it's possible we failed above on
1595 * the open_mode_check()) so use a userspace check.
1598 if (flags & O_RDWR) {
1599 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
1600 } else if (flags & O_WRONLY) {
1601 can_access_mask = FILE_WRITE_DATA;
1602 } else {
1603 can_access_mask = FILE_READ_DATA;
1606 if (((can_access_mask & FILE_WRITE_DATA) && !CAN_WRITE(conn)) ||
1607 !can_access_file(conn,fname,psbuf,can_access_mask)) {
1608 can_access = False;
1612 * If we're returning a share violation, ensure we
1613 * cope with the braindead 1 second delay.
1616 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
1617 lp_defer_sharing_violations()) {
1618 struct timeval timeout;
1619 struct deferred_open_record state;
1620 int timeout_usecs;
1622 /* this is a hack to speed up torture tests
1623 in 'make test' */
1624 timeout_usecs = lp_parm_int(SNUM(conn),
1625 "smbd","sharedelay",
1626 SHARING_VIOLATION_USEC_WAIT);
1628 /* This is a relative time, added to the absolute
1629 request_time value to get the absolute timeout time.
1630 Note that if this is the second or greater time we enter
1631 this codepath for this particular request mid then
1632 request_time is left as the absolute time of the *first*
1633 time this request mid was processed. This is what allows
1634 the request to eventually time out. */
1636 timeout = timeval_set(0, timeout_usecs);
1638 /* Nothing actually uses state.delayed_for_oplocks
1639 but it's handy to differentiate in debug messages
1640 between a 30 second delay due to oplock break, and
1641 a 1 second delay for share mode conflicts. */
1643 state.delayed_for_oplocks = False;
1644 state.id = id;
1646 if ((req != NULL)
1647 && !request_timed_out(request_time,
1648 timeout)) {
1649 defer_open(lck, request_time, timeout,
1650 req, &state);
1654 TALLOC_FREE(lck);
1655 if (can_access) {
1657 * We have detected a sharing violation here
1658 * so return the correct error code
1660 status = NT_STATUS_SHARING_VIOLATION;
1661 } else {
1662 status = NT_STATUS_ACCESS_DENIED;
1664 return status;
1668 * We exit this block with the share entry *locked*.....
1672 SMB_ASSERT(!file_existed || (lck != NULL));
1675 * Ensure we pay attention to default ACLs on directories if required.
1678 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
1679 (def_acl = directory_has_default_acl(conn, parent_dir))) {
1680 unx_mode = 0777;
1683 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
1684 "access_mask = 0x%x, open_access_mask = 0x%x\n",
1685 (unsigned int)flags, (unsigned int)flags2,
1686 (unsigned int)unx_mode, (unsigned int)access_mask,
1687 (unsigned int)open_access_mask));
1690 * open_file strips any O_TRUNC flags itself.
1693 fsp_open = open_file(fsp, conn, req, parent_dir, newname, fname, psbuf,
1694 flags|flags2, unx_mode, access_mask,
1695 open_access_mask);
1697 if (!NT_STATUS_IS_OK(fsp_open)) {
1698 if (lck != NULL) {
1699 TALLOC_FREE(lck);
1701 return fsp_open;
1704 if (!file_existed) {
1705 struct timespec old_write_time = get_mtimespec(psbuf);
1707 * Deal with the race condition where two smbd's detect the
1708 * file doesn't exist and do the create at the same time. One
1709 * of them will win and set a share mode, the other (ie. this
1710 * one) should check if the requested share mode for this
1711 * create is allowed.
1715 * Now the file exists and fsp is successfully opened,
1716 * fsp->dev and fsp->inode are valid and should replace the
1717 * dev=0,inode=0 from a non existent file. Spotted by
1718 * Nadav Danieli <nadavd@exanet.com>. JRA.
1721 id = fsp->file_id;
1723 lck = get_share_mode_lock(talloc_tos(), id,
1724 conn->connectpath,
1725 fname, &old_write_time);
1727 if (lck == NULL) {
1728 DEBUG(0, ("open_file_ntcreate: Could not get share "
1729 "mode lock for %s\n", fname));
1730 fd_close(fsp);
1731 return NT_STATUS_SHARING_VIOLATION;
1734 /* First pass - send break only on batch oplocks. */
1735 if ((req != NULL)
1736 && delay_for_oplocks(lck, fsp, req->mid, 1,
1737 oplock_request)) {
1738 schedule_defer_open(lck, request_time, req);
1739 TALLOC_FREE(lck);
1740 fd_close(fsp);
1741 return NT_STATUS_SHARING_VIOLATION;
1744 status = open_mode_check(conn, fname, lck,
1745 access_mask, share_access,
1746 create_options, &file_existed);
1748 if (NT_STATUS_IS_OK(status)) {
1749 /* We might be going to allow this open. Check oplock
1750 * status again. */
1751 /* Second pass - send break for both batch or
1752 * exclusive oplocks. */
1753 if ((req != NULL)
1754 && delay_for_oplocks(lck, fsp, req->mid, 2,
1755 oplock_request)) {
1756 schedule_defer_open(lck, request_time, req);
1757 TALLOC_FREE(lck);
1758 fd_close(fsp);
1759 return NT_STATUS_SHARING_VIOLATION;
1763 if (!NT_STATUS_IS_OK(status)) {
1764 struct deferred_open_record state;
1766 fd_close(fsp);
1768 state.delayed_for_oplocks = False;
1769 state.id = id;
1771 /* Do it all over again immediately. In the second
1772 * round we will find that the file existed and handle
1773 * the DELETE_PENDING and FCB cases correctly. No need
1774 * to duplicate the code here. Essentially this is a
1775 * "goto top of this function", but don't tell
1776 * anybody... */
1778 if (req != NULL) {
1779 defer_open(lck, request_time, timeval_zero(),
1780 req, &state);
1782 TALLOC_FREE(lck);
1783 return status;
1787 * We exit this block with the share entry *locked*.....
1792 SMB_ASSERT(lck != NULL);
1794 /* Delete streams if create_disposition requires it */
1795 if (file_existed && clear_ads && !is_ntfs_stream_name(fname)) {
1796 status = delete_all_streams(conn, fname);
1797 if (!NT_STATUS_IS_OK(status)) {
1798 TALLOC_FREE(lck);
1799 fd_close(fsp);
1800 return status;
1804 /* note that we ignore failure for the following. It is
1805 basically a hack for NFS, and NFS will never set one of
1806 these only read them. Nobody but Samba can ever set a deny
1807 mode and we have already checked our more authoritative
1808 locking database for permission to set this deny mode. If
1809 the kernel refuses the operations then the kernel is wrong.
1810 note that GPFS supports it as well - jmcd */
1812 if (fsp->fh->fd != -1) {
1813 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access);
1814 if(ret_flock == -1 ){
1816 TALLOC_FREE(lck);
1817 fd_close(fsp);
1819 return NT_STATUS_SHARING_VIOLATION;
1824 * At this point onwards, we can guarentee that the share entry
1825 * is locked, whether we created the file or not, and that the
1826 * deny mode is compatible with all current opens.
1830 * If requested, truncate the file.
1833 if (flags2&O_TRUNC) {
1835 * We are modifing the file after open - update the stat
1836 * struct..
1838 if ((SMB_VFS_FTRUNCATE(fsp, 0) == -1) ||
1839 (SMB_VFS_FSTAT(fsp, psbuf)==-1)) {
1840 status = map_nt_error_from_unix(errno);
1841 TALLOC_FREE(lck);
1842 fd_close(fsp);
1843 return status;
1847 /* Record the options we were opened with. */
1848 fsp->share_access = share_access;
1849 fsp->fh->private_options = create_options;
1850 fsp->access_mask = access_mask;
1852 if (file_existed) {
1853 /* stat opens on existing files don't get oplocks. */
1854 if (is_stat_open(open_access_mask)) {
1855 fsp->oplock_type = NO_OPLOCK;
1858 if (!(flags2 & O_TRUNC)) {
1859 info = FILE_WAS_OPENED;
1860 } else {
1861 info = FILE_WAS_OVERWRITTEN;
1863 } else {
1864 info = FILE_WAS_CREATED;
1867 if (pinfo) {
1868 *pinfo = info;
1872 * Setup the oplock info in both the shared memory and
1873 * file structs.
1876 if (!set_file_oplock(fsp, fsp->oplock_type)) {
1877 /* Could not get the kernel oplock */
1878 fsp->oplock_type = NO_OPLOCK;
1881 if (info == FILE_WAS_OVERWRITTEN || info == FILE_WAS_CREATED || info == FILE_WAS_SUPERSEDED) {
1882 new_file_created = True;
1885 set_share_mode(lck, fsp, current_user.ut.uid, 0,
1886 fsp->oplock_type);
1888 /* Handle strange delete on close create semantics. */
1889 if (create_options & FILE_DELETE_ON_CLOSE) {
1891 status = can_set_delete_on_close(fsp, True, new_dos_attributes);
1893 if (!NT_STATUS_IS_OK(status)) {
1894 /* Remember to delete the mode we just added. */
1895 del_share_mode(lck, fsp);
1896 TALLOC_FREE(lck);
1897 fd_close(fsp);
1898 return status;
1900 /* Note that here we set the *inital* delete on close flag,
1901 not the regular one. The magic gets handled in close. */
1902 fsp->initial_delete_on_close = True;
1905 if (new_file_created) {
1906 /* Files should be initially set as archive */
1907 if (lp_map_archive(SNUM(conn)) ||
1908 lp_store_dos_attributes(SNUM(conn))) {
1909 if (!posix_open) {
1910 SMB_STRUCT_STAT tmp_sbuf;
1911 SET_STAT_INVALID(tmp_sbuf);
1912 if (file_set_dosmode(
1913 conn, fname,
1914 new_dos_attributes | aARCH,
1915 &tmp_sbuf, parent_dir,
1916 true) == 0) {
1917 unx_mode = tmp_sbuf.st_mode;
1924 * Take care of inherited ACLs on created files - if default ACL not
1925 * selected.
1928 if (!posix_open && !file_existed && !def_acl) {
1930 int saved_errno = errno; /* We might get ENOSYS in the next
1931 * call.. */
1933 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
1934 errno == ENOSYS) {
1935 errno = saved_errno; /* Ignore ENOSYS */
1938 } else if (new_unx_mode) {
1940 int ret = -1;
1942 /* Attributes need changing. File already existed. */
1945 int saved_errno = errno; /* We might get ENOSYS in the
1946 * next call.. */
1947 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
1949 if (ret == -1 && errno == ENOSYS) {
1950 errno = saved_errno; /* Ignore ENOSYS */
1951 } else {
1952 DEBUG(5, ("open_file_ntcreate: reset "
1953 "attributes of file %s to 0%o\n",
1954 fname, (unsigned int)new_unx_mode));
1955 ret = 0; /* Don't do the fchmod below. */
1959 if ((ret == -1) &&
1960 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
1961 DEBUG(5, ("open_file_ntcreate: failed to reset "
1962 "attributes of file %s to 0%o\n",
1963 fname, (unsigned int)new_unx_mode));
1966 /* If this is a successful open, we must remove any deferred open
1967 * records. */
1968 if (req != NULL) {
1969 del_deferred_open_entry(lck, req->mid);
1971 TALLOC_FREE(lck);
1973 return NT_STATUS_OK;
1976 /****************************************************************************
1977 Open a file with a share mode.
1978 ****************************************************************************/
1980 NTSTATUS open_file_ntcreate(connection_struct *conn,
1981 struct smb_request *req,
1982 const char *fname,
1983 SMB_STRUCT_STAT *psbuf,
1984 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1985 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1986 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1987 uint32 create_options, /* options such as delete on close. */
1988 uint32 new_dos_attributes, /* attributes used for new file. */
1989 int oplock_request, /* internal Samba oplock codes. */
1990 /* Information (FILE_EXISTS etc.) */
1991 int *pinfo,
1992 files_struct **result)
1994 NTSTATUS status;
1995 files_struct *fsp = NULL;
1997 *result = NULL;
1999 status = file_new(conn, &fsp);
2000 if(!NT_STATUS_IS_OK(status)) {
2001 return status;
2004 status = open_file_ntcreate_internal(conn,
2005 req,
2006 fname,
2007 psbuf,
2008 access_mask,
2009 share_access,
2010 create_disposition,
2011 create_options,
2012 new_dos_attributes,
2013 oplock_request,
2014 pinfo,
2015 fsp);
2017 if(!NT_STATUS_IS_OK(status)) {
2018 file_free(fsp);
2019 return status;
2022 *result = fsp;
2023 return status;
2026 /****************************************************************************
2027 Open a file for for write to ensure that we can fchmod it.
2028 ****************************************************************************/
2030 NTSTATUS open_file_fchmod(connection_struct *conn, const char *fname,
2031 SMB_STRUCT_STAT *psbuf, files_struct **result)
2033 files_struct *fsp = NULL;
2034 NTSTATUS status;
2036 if (!VALID_STAT(*psbuf)) {
2037 return NT_STATUS_INVALID_PARAMETER;
2040 status = file_new(conn, &fsp);
2041 if(!NT_STATUS_IS_OK(status)) {
2042 return status;
2045 /* note! we must use a non-zero desired access or we don't get
2046 a real file descriptor. Oh what a twisted web we weave. */
2047 status = open_file(fsp, conn, NULL, NULL, NULL, fname, psbuf, O_WRONLY,
2048 0, FILE_WRITE_DATA, FILE_WRITE_DATA);
2051 * This is not a user visible file open.
2052 * Don't set a share mode.
2055 if (!NT_STATUS_IS_OK(status)) {
2056 file_free(fsp);
2057 return status;
2060 *result = fsp;
2061 return NT_STATUS_OK;
2064 /****************************************************************************
2065 Close the fchmod file fd - ensure no locks are lost.
2066 ****************************************************************************/
2068 NTSTATUS close_file_fchmod(files_struct *fsp)
2070 NTSTATUS status = fd_close(fsp);
2071 file_free(fsp);
2072 return status;
2075 static NTSTATUS mkdir_internal(connection_struct *conn,
2076 const char *name,
2077 uint32 file_attributes,
2078 SMB_STRUCT_STAT *psbuf)
2080 mode_t mode;
2081 char *parent_dir;
2082 const char *dirname;
2083 NTSTATUS status;
2084 bool posix_open = false;
2086 if(!CAN_WRITE(conn)) {
2087 DEBUG(5,("mkdir_internal: failing create on read-only share "
2088 "%s\n", lp_servicename(SNUM(conn))));
2089 return NT_STATUS_ACCESS_DENIED;
2092 status = check_name(conn, name);
2093 if (!NT_STATUS_IS_OK(status)) {
2094 return status;
2097 if (!parent_dirname_talloc(talloc_tos(), name, &parent_dir,
2098 &dirname)) {
2099 return NT_STATUS_NO_MEMORY;
2102 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2103 posix_open = true;
2104 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2105 } else {
2106 mode = unix_mode(conn, aDIR, name, parent_dir);
2109 if (SMB_VFS_MKDIR(conn, name, mode) != 0) {
2110 return map_nt_error_from_unix(errno);
2113 /* Ensure we're checking for a symlink here.... */
2114 /* We don't want to get caught by a symlink racer. */
2116 if (SMB_VFS_LSTAT(conn, name, psbuf) == -1) {
2117 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2118 name, strerror(errno)));
2119 return map_nt_error_from_unix(errno);
2122 if (!S_ISDIR(psbuf->st_mode)) {
2123 DEBUG(0, ("Directory just '%s' created is not a directory\n",
2124 name));
2125 return NT_STATUS_ACCESS_DENIED;
2128 if (lp_store_dos_attributes(SNUM(conn))) {
2129 if (!posix_open) {
2130 file_set_dosmode(conn, name,
2131 file_attributes | aDIR, NULL,
2132 parent_dir,
2133 true);
2137 if (lp_inherit_perms(SNUM(conn))) {
2138 inherit_access_acl(conn, parent_dir, name, mode);
2141 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
2143 * Check if high bits should have been set,
2144 * then (if bits are missing): add them.
2145 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2146 * dir.
2148 if (mode & ~(S_IRWXU|S_IRWXG|S_IRWXO) && (mode & ~psbuf->st_mode)) {
2149 SMB_VFS_CHMOD(conn, name,
2150 psbuf->st_mode | (mode & ~psbuf->st_mode));
2154 /* Change the owner if required. */
2155 if (lp_inherit_owner(SNUM(conn))) {
2156 change_dir_owner_to_parent(conn, parent_dir, name, psbuf);
2159 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2160 name);
2162 return NT_STATUS_OK;
2165 /****************************************************************************
2166 Open a directory from an NT SMB call.
2167 ****************************************************************************/
2169 NTSTATUS open_directory(connection_struct *conn,
2170 struct smb_request *req,
2171 const char *fname,
2172 SMB_STRUCT_STAT *psbuf,
2173 uint32 access_mask,
2174 uint32 share_access,
2175 uint32 create_disposition,
2176 uint32 create_options,
2177 uint32 file_attributes,
2178 int *pinfo,
2179 files_struct **result)
2181 files_struct *fsp = NULL;
2182 bool dir_existed = VALID_STAT(*psbuf) ? True : False;
2183 struct share_mode_lock *lck = NULL;
2184 NTSTATUS status;
2185 struct timespec mtimespec;
2186 int info = 0;
2188 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2189 "share_access = 0x%x create_options = 0x%x, "
2190 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2191 fname,
2192 (unsigned int)access_mask,
2193 (unsigned int)share_access,
2194 (unsigned int)create_options,
2195 (unsigned int)create_disposition,
2196 (unsigned int)file_attributes));
2198 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
2199 (conn->fs_capabilities & FILE_NAMED_STREAMS) &&
2200 is_ntfs_stream_name(fname)) {
2201 DEBUG(2, ("open_directory: %s is a stream name!\n", fname));
2202 return NT_STATUS_NOT_A_DIRECTORY;
2205 switch( create_disposition ) {
2206 case FILE_OPEN:
2208 info = FILE_WAS_OPENED;
2211 * We want to follow symlinks here.
2214 if (SMB_VFS_STAT(conn, fname, psbuf) != 0) {
2215 return map_nt_error_from_unix(errno);
2218 break;
2220 case FILE_CREATE:
2222 /* If directory exists error. If directory doesn't
2223 * exist create. */
2225 status = mkdir_internal(conn,
2226 fname,
2227 file_attributes,
2228 psbuf);
2230 if (!NT_STATUS_IS_OK(status)) {
2231 DEBUG(2, ("open_directory: unable to create "
2232 "%s. Error was %s\n", fname,
2233 nt_errstr(status)));
2234 return status;
2237 info = FILE_WAS_CREATED;
2238 break;
2240 case FILE_OPEN_IF:
2242 * If directory exists open. If directory doesn't
2243 * exist create.
2246 status = mkdir_internal(conn,
2247 fname,
2248 file_attributes,
2249 psbuf);
2251 if (NT_STATUS_IS_OK(status)) {
2252 info = FILE_WAS_CREATED;
2255 if (NT_STATUS_EQUAL(status,
2256 NT_STATUS_OBJECT_NAME_COLLISION)) {
2257 info = FILE_WAS_OPENED;
2258 status = NT_STATUS_OK;
2261 break;
2263 case FILE_SUPERSEDE:
2264 case FILE_OVERWRITE:
2265 case FILE_OVERWRITE_IF:
2266 default:
2267 DEBUG(5,("open_directory: invalid create_disposition "
2268 "0x%x for directory %s\n",
2269 (unsigned int)create_disposition, fname));
2270 return NT_STATUS_INVALID_PARAMETER;
2273 if(!S_ISDIR(psbuf->st_mode)) {
2274 DEBUG(5,("open_directory: %s is not a directory !\n",
2275 fname ));
2276 return NT_STATUS_NOT_A_DIRECTORY;
2279 status = file_new(conn, &fsp);
2280 if(!NT_STATUS_IS_OK(status)) {
2281 return status;
2285 * Setup the files_struct for it.
2288 fsp->mode = psbuf->st_mode;
2289 fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
2290 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
2291 fsp->file_pid = req ? req->smbpid : 0;
2292 fsp->can_lock = False;
2293 fsp->can_read = False;
2294 fsp->can_write = False;
2296 fsp->share_access = share_access;
2297 fsp->fh->private_options = create_options;
2298 fsp->access_mask = access_mask;
2300 fsp->print_file = False;
2301 fsp->modified = False;
2302 fsp->oplock_type = NO_OPLOCK;
2303 fsp->sent_oplock_break = NO_BREAK_SENT;
2304 fsp->is_directory = True;
2305 fsp->is_stat = False;
2306 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
2308 string_set(&fsp->fsp_name,fname);
2310 mtimespec = get_mtimespec(psbuf);
2312 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
2313 conn->connectpath,
2314 fname, &mtimespec);
2316 if (lck == NULL) {
2317 DEBUG(0, ("open_directory: Could not get share mode lock for %s\n", fname));
2318 file_free(fsp);
2319 return NT_STATUS_SHARING_VIOLATION;
2322 status = open_mode_check(conn, fname, lck,
2323 access_mask, share_access,
2324 create_options, &dir_existed);
2326 if (!NT_STATUS_IS_OK(status)) {
2327 TALLOC_FREE(lck);
2328 file_free(fsp);
2329 return status;
2332 set_share_mode(lck, fsp, current_user.ut.uid, 0, NO_OPLOCK);
2334 /* For directories the delete on close bit at open time seems
2335 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2336 if (create_options & FILE_DELETE_ON_CLOSE) {
2337 status = can_set_delete_on_close(fsp, True, 0);
2338 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
2339 TALLOC_FREE(lck);
2340 file_free(fsp);
2341 return status;
2344 if (NT_STATUS_IS_OK(status)) {
2345 /* Note that here we set the *inital* delete on close flag,
2346 not the regular one. The magic gets handled in close. */
2347 fsp->initial_delete_on_close = True;
2351 TALLOC_FREE(lck);
2353 if (pinfo) {
2354 *pinfo = info;
2357 *result = fsp;
2358 return NT_STATUS_OK;
2361 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req, const char *directory)
2363 NTSTATUS status;
2364 SMB_STRUCT_STAT sbuf;
2365 files_struct *fsp;
2367 SET_STAT_INVALID(sbuf);
2369 status = open_directory(conn, req, directory, &sbuf,
2370 FILE_READ_ATTRIBUTES, /* Just a stat open */
2371 FILE_SHARE_NONE, /* Ignored for stat opens */
2372 FILE_CREATE,
2374 FILE_ATTRIBUTE_DIRECTORY,
2375 NULL,
2376 &fsp);
2378 if (NT_STATUS_IS_OK(status)) {
2379 close_file(fsp, NORMAL_CLOSE);
2382 return status;
2385 /****************************************************************************
2386 Open a pseudo-file (no locking checks - a 'stat' open).
2387 ****************************************************************************/
2389 NTSTATUS open_file_stat(connection_struct *conn, struct smb_request *req,
2390 const char *fname, SMB_STRUCT_STAT *psbuf,
2391 files_struct **result)
2393 files_struct *fsp = NULL;
2394 NTSTATUS status;
2396 if (!VALID_STAT(*psbuf)) {
2397 return NT_STATUS_INVALID_PARAMETER;
2400 /* Can't 'stat' open directories. */
2401 if(S_ISDIR(psbuf->st_mode)) {
2402 return NT_STATUS_FILE_IS_A_DIRECTORY;
2405 status = file_new(conn, &fsp);
2406 if(!NT_STATUS_IS_OK(status)) {
2407 return status;
2410 DEBUG(5,("open_file_stat: 'opening' file %s\n", fname));
2413 * Setup the files_struct for it.
2416 fsp->mode = psbuf->st_mode;
2417 fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
2418 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
2419 fsp->file_pid = req ? req->smbpid : 0;
2420 fsp->can_lock = False;
2421 fsp->can_read = False;
2422 fsp->can_write = False;
2423 fsp->print_file = False;
2424 fsp->modified = False;
2425 fsp->oplock_type = NO_OPLOCK;
2426 fsp->sent_oplock_break = NO_BREAK_SENT;
2427 fsp->is_directory = False;
2428 fsp->is_stat = True;
2429 string_set(&fsp->fsp_name,fname);
2431 *result = fsp;
2432 return NT_STATUS_OK;
2435 /****************************************************************************
2436 Receive notification that one of our open files has been renamed by another
2437 smbd process.
2438 ****************************************************************************/
2440 void msg_file_was_renamed(struct messaging_context *msg,
2441 void *private_data,
2442 uint32_t msg_type,
2443 struct server_id server_id,
2444 DATA_BLOB *data)
2446 files_struct *fsp;
2447 char *frm = (char *)data->data;
2448 struct file_id id;
2449 const char *sharepath;
2450 const char *newname;
2451 size_t sp_len;
2453 if (data->data == NULL
2454 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
2455 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
2456 (int)data->length));
2457 return;
2460 /* Unpack the message. */
2461 pull_file_id_16(frm, &id);
2462 sharepath = &frm[16];
2463 newname = sharepath + strlen(sharepath) + 1;
2464 sp_len = strlen(sharepath);
2466 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
2467 "file_id %s\n",
2468 sharepath, newname, file_id_string_tos(&id)));
2470 for(fsp = file_find_di_first(id); fsp; fsp = file_find_di_next(fsp)) {
2471 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
2472 DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n",
2473 fsp->fnum, fsp->fsp_name, newname ));
2474 string_set(&fsp->fsp_name, newname);
2475 } else {
2476 /* TODO. JRA. */
2477 /* Now we have the complete path we can work out if this is
2478 actually within this share and adjust newname accordingly. */
2479 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
2480 "not sharepath %s) "
2481 "fnum %d from %s -> %s\n",
2482 fsp->conn->connectpath,
2483 sharepath,
2484 fsp->fnum,
2485 fsp->fsp_name,
2486 newname ));
2491 struct case_semantics_state {
2492 connection_struct *conn;
2493 bool case_sensitive;
2494 bool case_preserve;
2495 bool short_case_preserve;
2498 /****************************************************************************
2499 Restore case semantics.
2500 ****************************************************************************/
2501 static int restore_case_semantics(struct case_semantics_state *state)
2503 state->conn->case_sensitive = state->case_sensitive;
2504 state->conn->case_preserve = state->case_preserve;
2505 state->conn->short_case_preserve = state->short_case_preserve;
2506 return 0;
2509 /****************************************************************************
2510 Save case semantics.
2511 ****************************************************************************/
2512 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
2513 connection_struct *conn)
2515 struct case_semantics_state *result;
2517 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
2518 DEBUG(0, ("talloc failed\n"));
2519 return NULL;
2522 result->conn = conn;
2523 result->case_sensitive = conn->case_sensitive;
2524 result->case_preserve = conn->case_preserve;
2525 result->short_case_preserve = conn->short_case_preserve;
2527 /* Set to POSIX. */
2528 conn->case_sensitive = True;
2529 conn->case_preserve = True;
2530 conn->short_case_preserve = True;
2532 talloc_set_destructor(result, restore_case_semantics);
2534 return result;
2538 * If a main file is opened for delete, all streams need to be checked for
2539 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
2540 * If that works, delete them all by setting the delete on close and close.
2543 static NTSTATUS open_streams_for_delete(connection_struct *conn,
2544 const char *fname)
2546 struct stream_struct *stream_info;
2547 files_struct **streams;
2548 int i;
2549 unsigned int num_streams;
2550 TALLOC_CTX *frame = talloc_stackframe();
2551 NTSTATUS status;
2553 status = SMB_VFS_STREAMINFO(conn, NULL, fname, talloc_tos(),
2554 &num_streams, &stream_info);
2556 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
2557 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
2558 DEBUG(10, ("no streams around\n"));
2559 TALLOC_FREE(frame);
2560 return NT_STATUS_OK;
2563 if (!NT_STATUS_IS_OK(status)) {
2564 DEBUG(10, ("SMB_VFS_STREAMINFO failed: %s\n",
2565 nt_errstr(status)));
2566 goto fail;
2569 DEBUG(10, ("open_streams_for_delete found %d streams\n",
2570 num_streams));
2572 if (num_streams == 0) {
2573 TALLOC_FREE(frame);
2574 return NT_STATUS_OK;
2577 streams = TALLOC_ARRAY(talloc_tos(), files_struct *, num_streams);
2578 if (streams == NULL) {
2579 DEBUG(0, ("talloc failed\n"));
2580 status = NT_STATUS_NO_MEMORY;
2581 goto fail;
2584 for (i=0; i<num_streams; i++) {
2585 char *streamname;
2587 if (strequal(stream_info[i].name, "::$DATA")) {
2588 streams[i] = NULL;
2589 continue;
2592 streamname = talloc_asprintf(talloc_tos(), "%s%s", fname,
2593 stream_info[i].name);
2595 if (streamname == NULL) {
2596 DEBUG(0, ("talloc_aprintf failed\n"));
2597 status = NT_STATUS_NO_MEMORY;
2598 goto fail;
2601 status = create_file_unixpath
2602 (conn, /* conn */
2603 NULL, /* req */
2604 streamname, /* fname */
2605 DELETE_ACCESS, /* access_mask */
2606 FILE_SHARE_READ | FILE_SHARE_WRITE
2607 | FILE_SHARE_DELETE, /* share_access */
2608 FILE_OPEN, /* create_disposition*/
2609 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* create_options */
2610 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
2611 0, /* oplock_request */
2612 0, /* allocation_size */
2613 NULL, /* sd */
2614 NULL, /* ea_list */
2615 &streams[i], /* result */
2616 NULL, /* pinfo */
2617 NULL); /* psbuf */
2619 TALLOC_FREE(streamname);
2621 if (!NT_STATUS_IS_OK(status)) {
2622 DEBUG(10, ("Could not open stream %s: %s\n",
2623 streamname, nt_errstr(status)));
2624 break;
2629 * don't touch the variable "status" beyond this point :-)
2632 for (i -= 1 ; i >= 0; i--) {
2633 if (streams[i] == NULL) {
2634 continue;
2637 DEBUG(10, ("Closing stream # %d, %s\n", i,
2638 streams[i]->fsp_name));
2639 close_file(streams[i], NORMAL_CLOSE);
2642 fail:
2643 TALLOC_FREE(frame);
2644 return status;
2648 * Wrapper around open_file_ntcreate and open_directory
2651 NTSTATUS create_file_unixpath(connection_struct *conn,
2652 struct smb_request *req,
2653 const char *fname,
2654 uint32_t access_mask,
2655 uint32_t share_access,
2656 uint32_t create_disposition,
2657 uint32_t create_options,
2658 uint32_t file_attributes,
2659 uint32_t oplock_request,
2660 SMB_BIG_UINT allocation_size,
2661 struct security_descriptor *sd,
2662 struct ea_list *ea_list,
2664 files_struct **result,
2665 int *pinfo,
2666 SMB_STRUCT_STAT *psbuf)
2668 SMB_STRUCT_STAT sbuf;
2669 int info = FILE_WAS_OPENED;
2670 files_struct *base_fsp = NULL;
2671 files_struct *fsp = NULL;
2672 NTSTATUS status;
2674 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
2675 "file_attributes = 0x%x, share_access = 0x%x, "
2676 "create_disposition = 0x%x create_options = 0x%x "
2677 "oplock_request = 0x%x ea_list = 0x%p, sd = 0x%p, "
2678 "fname = %s\n",
2679 (unsigned int)access_mask,
2680 (unsigned int)file_attributes,
2681 (unsigned int)share_access,
2682 (unsigned int)create_disposition,
2683 (unsigned int)create_options,
2684 (unsigned int)oplock_request,
2685 ea_list, sd, fname));
2687 if (create_options & FILE_OPEN_BY_FILE_ID) {
2688 status = NT_STATUS_NOT_SUPPORTED;
2689 goto fail;
2692 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
2693 status = NT_STATUS_INVALID_PARAMETER;
2694 goto fail;
2697 if (req == NULL) {
2698 oplock_request |= INTERNAL_OPEN_ONLY;
2701 if (psbuf != NULL) {
2702 sbuf = *psbuf;
2704 else {
2705 if (SMB_VFS_STAT(conn, fname, &sbuf) == -1) {
2706 SET_STAT_INVALID(sbuf);
2710 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
2711 && (access_mask & DELETE_ACCESS)
2712 && !is_ntfs_stream_name(fname)) {
2714 * We can't open a file with DELETE access if any of the
2715 * streams is open without FILE_SHARE_DELETE
2717 status = open_streams_for_delete(conn, fname);
2719 if (!NT_STATUS_IS_OK(status)) {
2720 goto fail;
2724 /* This is the correct thing to do (check every time) but can_delete
2725 * is expensive (it may have to read the parent directory
2726 * permissions). So for now we're not doing it unless we have a strong
2727 * hint the client is really going to delete this file. If the client
2728 * is forcing FILE_CREATE let the filesystem take care of the
2729 * permissions. */
2731 /* Setting FILE_SHARE_DELETE is the hint. */
2733 if (lp_acl_check_permissions(SNUM(conn))
2734 && (create_disposition != FILE_CREATE)
2735 && (share_access & FILE_SHARE_DELETE)
2736 && (access_mask & DELETE_ACCESS)
2737 && (!can_delete_file_in_directory(conn, fname))) {
2738 status = NT_STATUS_ACCESS_DENIED;
2739 goto fail;
2742 #if 0
2743 /* We need to support SeSecurityPrivilege for this. */
2744 if ((access_mask & SEC_RIGHT_SYSTEM_SECURITY) &&
2745 !user_has_privileges(current_user.nt_user_token,
2746 &se_security)) {
2747 status = NT_STATUS_PRIVILEGE_NOT_HELD;
2748 goto fail;
2750 #endif
2752 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
2753 && is_ntfs_stream_name(fname)
2754 && (!(create_options & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
2755 char *base;
2756 uint32 base_create_disposition;
2758 if (create_options & FILE_DIRECTORY_FILE) {
2759 status = NT_STATUS_NOT_A_DIRECTORY;
2760 goto fail;
2763 status = split_ntfs_stream_name(talloc_tos(), fname,
2764 &base, NULL);
2765 if (!NT_STATUS_IS_OK(status)) {
2766 DEBUG(10, ("create_file_unixpath: "
2767 "split_ntfs_stream_name failed: %s\n",
2768 nt_errstr(status)));
2769 goto fail;
2772 SMB_ASSERT(!is_ntfs_stream_name(base)); /* paranoia.. */
2774 switch (create_disposition) {
2775 case FILE_OPEN:
2776 base_create_disposition = FILE_OPEN;
2777 break;
2778 default:
2779 base_create_disposition = FILE_OPEN_IF;
2780 break;
2783 DEBUG(10, ("Recursing into create_file_unixpath for "
2784 "base %s\n", base));
2786 status = create_file_unixpath(conn, NULL, base, 0,
2787 FILE_SHARE_READ
2788 | FILE_SHARE_WRITE
2789 | FILE_SHARE_DELETE,
2790 base_create_disposition,
2791 0, 0, 0, 0, NULL, NULL,
2792 &base_fsp, NULL, NULL);
2793 if (!NT_STATUS_IS_OK(status)) {
2794 DEBUG(10, ("create_file_unixpath for base %s failed: "
2795 "%s\n", base, nt_errstr(status)));
2796 goto fail;
2798 /* we don't need to low level fd */
2799 fd_close(base_fsp);
2803 * If it's a request for a directory open, deal with it separately.
2806 if (create_options & FILE_DIRECTORY_FILE) {
2808 if (create_options & FILE_NON_DIRECTORY_FILE) {
2809 status = NT_STATUS_INVALID_PARAMETER;
2810 goto fail;
2813 /* Can't open a temp directory. IFS kit test. */
2814 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
2815 status = NT_STATUS_INVALID_PARAMETER;
2816 goto fail;
2820 * We will get a create directory here if the Win32
2821 * app specified a security descriptor in the
2822 * CreateDirectory() call.
2825 oplock_request = 0;
2826 status = open_directory(
2827 conn, req, fname, &sbuf, access_mask, share_access,
2828 create_disposition, create_options, file_attributes,
2829 &info, &fsp);
2830 } else {
2833 * Ordinary file case.
2836 if (base_fsp) {
2838 * We're opening the stream element of a base_fsp
2839 * we already opened. We need to initialize
2840 * the fsp first, and set up the base_fsp pointer.
2842 status = file_new(conn, &fsp);
2843 if(!NT_STATUS_IS_OK(status)) {
2844 goto fail;
2847 fsp->base_fsp = base_fsp;
2849 status = open_file_ntcreate_internal(conn,
2850 req,
2851 fname,
2852 &sbuf,
2853 access_mask,
2854 share_access,
2855 create_disposition,
2856 create_options,
2857 file_attributes,
2858 oplock_request,
2859 &info,
2860 fsp);
2862 if(!NT_STATUS_IS_OK(status)) {
2863 file_free(fsp);
2864 fsp = NULL;
2866 } else {
2867 status = open_file_ntcreate(
2868 conn, req, fname, &sbuf, access_mask, share_access,
2869 create_disposition, create_options, file_attributes,
2870 oplock_request, &info, &fsp);
2873 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
2875 /* A stream open never opens a directory */
2877 if (base_fsp) {
2878 status = NT_STATUS_FILE_IS_A_DIRECTORY;
2879 goto fail;
2883 * Fail the open if it was explicitly a non-directory
2884 * file.
2887 if (create_options & FILE_NON_DIRECTORY_FILE) {
2888 status = NT_STATUS_FILE_IS_A_DIRECTORY;
2889 goto fail;
2892 oplock_request = 0;
2893 status = open_directory(
2894 conn, req, fname, &sbuf, access_mask,
2895 share_access, create_disposition,
2896 create_options, file_attributes,
2897 &info, &fsp);
2901 if (!NT_STATUS_IS_OK(status)) {
2902 goto fail;
2905 fsp->base_fsp = base_fsp;
2908 * According to the MS documentation, the only time the security
2909 * descriptor is applied to the opened file is iff we *created* the
2910 * file; an existing file stays the same.
2912 * Also, it seems (from observation) that you can open the file with
2913 * any access mask but you can still write the sd. We need to override
2914 * the granted access before we call set_sd
2915 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
2918 if ((sd != NULL) && (info == FILE_WAS_CREATED)
2919 && lp_nt_acl_support(SNUM(conn))) {
2921 uint32_t sec_info_sent = ALL_SECURITY_INFORMATION;
2922 uint32_t saved_access_mask = fsp->access_mask;
2924 if (sd->owner_sid == NULL) {
2925 sec_info_sent &= ~OWNER_SECURITY_INFORMATION;
2927 if (sd->group_sid == NULL) {
2928 sec_info_sent &= ~GROUP_SECURITY_INFORMATION;
2930 if (sd->sacl == NULL) {
2931 sec_info_sent &= ~SACL_SECURITY_INFORMATION;
2933 if (sd->dacl == NULL) {
2934 sec_info_sent &= ~DACL_SECURITY_INFORMATION;
2937 fsp->access_mask = FILE_GENERIC_ALL;
2939 status = SMB_VFS_FSET_NT_ACL(fsp, sec_info_sent, sd);
2941 fsp->access_mask = saved_access_mask;
2943 if (!NT_STATUS_IS_OK(status)) {
2944 goto fail;
2948 if ((ea_list != NULL) && (info == FILE_WAS_CREATED)) {
2949 status = set_ea(conn, fsp, fname, ea_list);
2950 if (!NT_STATUS_IS_OK(status)) {
2951 goto fail;
2955 if (!fsp->is_directory && S_ISDIR(sbuf.st_mode)) {
2956 status = NT_STATUS_ACCESS_DENIED;
2957 goto fail;
2960 /* Save the requested allocation size. */
2961 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
2962 if (allocation_size
2963 && (allocation_size > sbuf.st_size)) {
2964 fsp->initial_allocation_size = smb_roundup(
2965 fsp->conn, allocation_size);
2966 if (fsp->is_directory) {
2967 /* Can't set allocation size on a directory. */
2968 status = NT_STATUS_ACCESS_DENIED;
2969 goto fail;
2971 if (vfs_allocate_file_space(
2972 fsp, fsp->initial_allocation_size) == -1) {
2973 status = NT_STATUS_DISK_FULL;
2974 goto fail;
2976 } else {
2977 fsp->initial_allocation_size = smb_roundup(
2978 fsp->conn, (SMB_BIG_UINT)sbuf.st_size);
2982 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
2984 *result = fsp;
2985 if (pinfo != NULL) {
2986 *pinfo = info;
2988 if (psbuf != NULL) {
2989 if ((fsp->fh == NULL) || (fsp->fh->fd == -1)) {
2990 *psbuf = sbuf;
2992 else {
2993 SMB_VFS_FSTAT(fsp, psbuf);
2996 return NT_STATUS_OK;
2998 fail:
2999 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
3001 if (fsp != NULL) {
3002 if (base_fsp && fsp->base_fsp == base_fsp) {
3004 * The close_file below will close
3005 * fsp->base_fsp.
3007 base_fsp = NULL;
3009 close_file(fsp, ERROR_CLOSE);
3010 fsp = NULL;
3012 if (base_fsp != NULL) {
3013 close_file(base_fsp, ERROR_CLOSE);
3014 base_fsp = NULL;
3016 return status;
3019 NTSTATUS create_file(connection_struct *conn,
3020 struct smb_request *req,
3021 uint16_t root_dir_fid,
3022 const char *fname,
3023 uint32_t access_mask,
3024 uint32_t share_access,
3025 uint32_t create_disposition,
3026 uint32_t create_options,
3027 uint32_t file_attributes,
3028 uint32_t oplock_request,
3029 SMB_BIG_UINT allocation_size,
3030 struct security_descriptor *sd,
3031 struct ea_list *ea_list,
3033 files_struct **result,
3034 int *pinfo,
3035 SMB_STRUCT_STAT *psbuf)
3037 struct case_semantics_state *case_state = NULL;
3038 SMB_STRUCT_STAT sbuf;
3039 int info = FILE_WAS_OPENED;
3040 files_struct *fsp = NULL;
3041 NTSTATUS status;
3043 DEBUG(10,("create_file: access_mask = 0x%x "
3044 "file_attributes = 0x%x, share_access = 0x%x, "
3045 "create_disposition = 0x%x create_options = 0x%x "
3046 "oplock_request = 0x%x "
3047 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
3048 "fname = %s\n",
3049 (unsigned int)access_mask,
3050 (unsigned int)file_attributes,
3051 (unsigned int)share_access,
3052 (unsigned int)create_disposition,
3053 (unsigned int)create_options,
3054 (unsigned int)oplock_request,
3055 (unsigned int)root_dir_fid,
3056 ea_list, sd, fname));
3058 /* MSDFS pathname processing must be done FIRST.
3059 MSDFS pathnames containing IPv6 addresses can
3060 be confused with NTFS stream names (they contain
3061 ":" characters. JRA. */
3063 if ((req != NULL) && (req->flags2 & FLAGS2_DFS_PATHNAMES)) {
3064 char *resolved_fname;
3066 status = resolve_dfspath(talloc_tos(), conn, true, fname,
3067 &resolved_fname);
3069 if (!NT_STATUS_IS_OK(status)) {
3071 * For PATH_NOT_COVERED we had
3072 * reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
3073 * ERRSRV, ERRbadpath);
3074 * Need to fix in callers
3076 goto fail;
3078 fname = resolved_fname;
3082 * Get the file name.
3085 if (root_dir_fid != 0) {
3087 * This filename is relative to a directory fid.
3089 char *parent_fname = NULL;
3090 files_struct *dir_fsp = file_fsp(root_dir_fid);
3092 if (dir_fsp == NULL) {
3093 status = NT_STATUS_INVALID_HANDLE;
3094 goto fail;
3097 if (!dir_fsp->is_directory) {
3100 * Check to see if this is a mac fork of some kind.
3103 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
3104 is_ntfs_stream_name(fname)) {
3105 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3106 goto fail;
3110 we need to handle the case when we get a
3111 relative open relative to a file and the
3112 pathname is blank - this is a reopen!
3113 (hint from demyn plantenberg)
3116 status = NT_STATUS_INVALID_HANDLE;
3117 goto fail;
3120 if (ISDOT(dir_fsp->fsp_name)) {
3122 * We're at the toplevel dir, the final file name
3123 * must not contain ./, as this is filtered out
3124 * normally by srvstr_get_path and unix_convert
3125 * explicitly rejects paths containing ./.
3127 parent_fname = talloc_strdup(talloc_tos(), "");
3128 if (parent_fname == NULL) {
3129 status = NT_STATUS_NO_MEMORY;
3130 goto fail;
3132 } else {
3133 size_t dir_name_len = strlen(dir_fsp->fsp_name);
3136 * Copy in the base directory name.
3139 parent_fname = TALLOC_ARRAY(talloc_tos(), char,
3140 dir_name_len+2);
3141 if (parent_fname == NULL) {
3142 status = NT_STATUS_NO_MEMORY;
3143 goto fail;
3145 memcpy(parent_fname, dir_fsp->fsp_name,
3146 dir_name_len+1);
3149 * Ensure it ends in a '/'.
3150 * We used TALLOC_SIZE +2 to add space for the '/'.
3153 if(dir_name_len
3154 && (parent_fname[dir_name_len-1] != '\\')
3155 && (parent_fname[dir_name_len-1] != '/')) {
3156 parent_fname[dir_name_len] = '/';
3157 parent_fname[dir_name_len+1] = '\0';
3161 fname = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
3162 fname);
3163 if (fname == NULL) {
3164 status = NT_STATUS_NO_MEMORY;
3165 goto fail;
3170 * Check to see if this is a mac fork of some kind.
3173 if (is_ntfs_stream_name(fname)) {
3174 enum FAKE_FILE_TYPE fake_file_type;
3176 fake_file_type = is_fake_file(fname);
3178 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
3181 * Here we go! support for changing the disk quotas
3182 * --metze
3184 * We need to fake up to open this MAGIC QUOTA file
3185 * and return a valid FID.
3187 * w2k close this file directly after openening xp
3188 * also tries a QUERY_FILE_INFO on the file and then
3189 * close it
3192 status = open_fake_file(conn, fake_file_type, fname,
3193 access_mask, &fsp);
3194 if (!NT_STATUS_IS_OK(status)) {
3195 goto fail;
3198 ZERO_STRUCT(sbuf);
3199 goto done;
3202 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
3203 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3204 goto fail;
3209 * Check if POSIX semantics are wanted.
3212 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
3213 case_state = set_posix_case_semantics(talloc_tos(), conn);
3214 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
3218 char *converted_fname;
3220 SET_STAT_INVALID(sbuf);
3222 status = unix_convert(talloc_tos(), conn, fname, False,
3223 &converted_fname, NULL, &sbuf);
3224 if (!NT_STATUS_IS_OK(status)) {
3225 goto fail;
3227 fname = converted_fname;
3230 TALLOC_FREE(case_state);
3232 /* All file access must go through check_name() */
3234 status = check_name(conn, fname);
3235 if (!NT_STATUS_IS_OK(status)) {
3236 goto fail;
3239 status = create_file_unixpath(
3240 conn, req, fname, access_mask, share_access,
3241 create_disposition, create_options, file_attributes,
3242 oplock_request, allocation_size, sd, ea_list,
3243 &fsp, &info, &sbuf);
3245 if (!NT_STATUS_IS_OK(status)) {
3246 goto fail;
3249 done:
3250 DEBUG(10, ("create_file: info=%d\n", info));
3252 *result = fsp;
3253 if (pinfo != NULL) {
3254 *pinfo = info;
3256 if (psbuf != NULL) {
3257 *psbuf = sbuf;
3259 return NT_STATUS_OK;
3261 fail:
3262 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
3264 if (fsp != NULL) {
3265 close_file(fsp, ERROR_CLOSE);
3266 fsp = NULL;
3268 return status;