winbindd: create the messaging conntext earlier
[Samba.git] / source / smbd / open.c
blobf3ed234c87eb4c923d6c3c7b52f383ea01fc38f7
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 if (fsp->fh->fd == -1) {
76 return NT_STATUS_OK; /* What we used to call a stat open. */
78 if (fsp->fh->ref_count > 1) {
79 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
81 return fd_close_posix(fsp);
84 /****************************************************************************
85 Change the ownership of a file to that of the parent directory.
86 Do this by fd if possible.
87 ****************************************************************************/
89 static void change_file_owner_to_parent(connection_struct *conn,
90 const char *inherit_from_dir,
91 files_struct *fsp)
93 SMB_STRUCT_STAT parent_st;
94 int ret;
96 ret = SMB_VFS_STAT(conn, inherit_from_dir, &parent_st);
97 if (ret == -1) {
98 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
99 "directory %s. Error was %s\n",
100 inherit_from_dir, strerror(errno) ));
101 return;
104 become_root();
105 ret = SMB_VFS_FCHOWN(fsp, parent_st.st_uid, (gid_t)-1);
106 unbecome_root();
107 if (ret == -1) {
108 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
109 "file %s to parent directory uid %u. Error "
110 "was %s\n", fsp->fsp_name,
111 (unsigned int)parent_st.st_uid,
112 strerror(errno) ));
115 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
116 "parent directory uid %u.\n", fsp->fsp_name,
117 (unsigned int)parent_st.st_uid ));
120 static NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
121 const char *inherit_from_dir,
122 const char *fname,
123 SMB_STRUCT_STAT *psbuf)
125 char *saved_dir = NULL;
126 SMB_STRUCT_STAT sbuf;
127 SMB_STRUCT_STAT parent_st;
128 TALLOC_CTX *ctx = talloc_tos();
129 NTSTATUS status = NT_STATUS_OK;
130 int ret;
132 ret = SMB_VFS_STAT(conn, inherit_from_dir, &parent_st);
133 if (ret == -1) {
134 status = map_nt_error_from_unix(errno);
135 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
136 "directory %s. Error was %s\n",
137 inherit_from_dir, strerror(errno) ));
138 return status;
141 /* We've already done an lstat into psbuf, and we know it's a
142 directory. If we can cd into the directory and the dev/ino
143 are the same then we can safely chown without races as
144 we're locking the directory in place by being in it. This
145 should work on any UNIX (thanks tridge :-). JRA.
148 saved_dir = vfs_GetWd(ctx,conn);
149 if (!saved_dir) {
150 status = map_nt_error_from_unix(errno);
151 DEBUG(0,("change_dir_owner_to_parent: failed to get "
152 "current working directory. Error was %s\n",
153 strerror(errno)));
154 return status;
157 /* Chdir into the new path. */
158 if (vfs_ChDir(conn, fname) == -1) {
159 status = map_nt_error_from_unix(errno);
160 DEBUG(0,("change_dir_owner_to_parent: failed to change "
161 "current working directory to %s. Error "
162 "was %s\n", fname, strerror(errno) ));
163 goto out;
166 if (SMB_VFS_STAT(conn,".",&sbuf) == -1) {
167 status = map_nt_error_from_unix(errno);
168 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
169 "directory '.' (%s) Error was %s\n",
170 fname, strerror(errno)));
171 goto out;
174 /* Ensure we're pointing at the same place. */
175 if (sbuf.st_dev != psbuf->st_dev ||
176 sbuf.st_ino != psbuf->st_ino ||
177 sbuf.st_mode != psbuf->st_mode ) {
178 DEBUG(0,("change_dir_owner_to_parent: "
179 "device/inode/mode on directory %s changed. "
180 "Refusing to chown !\n", fname ));
181 status = NT_STATUS_ACCESS_DENIED;
182 goto out;
185 become_root();
186 ret = SMB_VFS_CHOWN(conn, ".", parent_st.st_uid, (gid_t)-1);
187 unbecome_root();
188 if (ret == -1) {
189 status = map_nt_error_from_unix(errno);
190 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
191 "directory %s to parent directory uid %u. "
192 "Error was %s\n", fname,
193 (unsigned int)parent_st.st_uid, strerror(errno) ));
194 goto out;
197 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
198 "directory %s to parent directory uid %u.\n",
199 fname, (unsigned int)parent_st.st_uid ));
201 out:
203 vfs_ChDir(conn,saved_dir);
204 return status;
207 /****************************************************************************
208 Open a file.
209 ****************************************************************************/
211 static NTSTATUS open_file(files_struct *fsp,
212 connection_struct *conn,
213 struct smb_request *req,
214 const char *parent_dir,
215 const char *name,
216 const char *path,
217 SMB_STRUCT_STAT *psbuf,
218 int flags,
219 mode_t unx_mode,
220 uint32 access_mask, /* client requested access mask. */
221 uint32 open_access_mask) /* what we're actually using in the open. */
223 NTSTATUS status = NT_STATUS_OK;
224 int accmode = (flags & O_ACCMODE);
225 int local_flags = flags;
226 bool file_existed = VALID_STAT(*psbuf);
228 fsp->fh->fd = -1;
229 errno = EPERM;
231 /* Check permissions */
234 * This code was changed after seeing a client open request
235 * containing the open mode of (DENY_WRITE/read-only) with
236 * the 'create if not exist' bit set. The previous code
237 * would fail to open the file read only on a read-only share
238 * as it was checking the flags parameter directly against O_RDONLY,
239 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
240 * JRA.
243 if (!CAN_WRITE(conn)) {
244 /* It's a read-only share - fail if we wanted to write. */
245 if(accmode != O_RDONLY) {
246 DEBUG(3,("Permission denied opening %s\n", path));
247 return NT_STATUS_ACCESS_DENIED;
248 } else if(flags & O_CREAT) {
249 /* We don't want to write - but we must make sure that
250 O_CREAT doesn't create the file if we have write
251 access into the directory.
253 flags &= ~O_CREAT;
254 local_flags &= ~O_CREAT;
259 * This little piece of insanity is inspired by the
260 * fact that an NT client can open a file for O_RDONLY,
261 * but set the create disposition to FILE_EXISTS_TRUNCATE.
262 * If the client *can* write to the file, then it expects to
263 * truncate the file, even though it is opening for readonly.
264 * Quicken uses this stupid trick in backup file creation...
265 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
266 * for helping track this one down. It didn't bite us in 2.0.x
267 * as we always opened files read-write in that release. JRA.
270 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
271 DEBUG(10,("open_file: truncate requested on read-only open "
272 "for file %s\n", path));
273 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
276 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
277 (!file_existed && (local_flags & O_CREAT)) ||
278 ((local_flags & O_TRUNC) == O_TRUNC) ) {
281 * We can't actually truncate here as the file may be locked.
282 * open_file_ntcreate will take care of the truncate later. JRA.
285 local_flags &= ~O_TRUNC;
287 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
289 * We would block on opening a FIFO with no one else on the
290 * other end. Do what we used to do and add O_NONBLOCK to the
291 * open flags. JRA.
294 if (file_existed && S_ISFIFO(psbuf->st_mode)) {
295 local_flags |= O_NONBLOCK;
297 #endif
299 /* Don't create files with Microsoft wildcard characters. */
300 if ((local_flags & O_CREAT) && !file_existed &&
301 ms_has_wild(path)) {
302 return NT_STATUS_OBJECT_NAME_INVALID;
305 /* Actually do the open */
306 status = fd_open(conn, path, fsp, local_flags, unx_mode);
307 if (!NT_STATUS_IS_OK(status)) {
308 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
309 "(flags=%d)\n",
310 path,nt_errstr(status),local_flags,flags));
311 return status;
314 if ((local_flags & O_CREAT) && !file_existed) {
316 /* Inherit the ACL if required */
317 if (lp_inherit_perms(SNUM(conn))) {
318 inherit_access_acl(conn, parent_dir, path,
319 unx_mode);
322 /* Change the owner if required. */
323 if (lp_inherit_owner(SNUM(conn))) {
324 change_file_owner_to_parent(conn, parent_dir,
325 fsp);
328 notify_fname(conn, NOTIFY_ACTION_ADDED,
329 FILE_NOTIFY_CHANGE_FILE_NAME, path);
332 } else {
333 fsp->fh->fd = -1; /* What we used to call a stat open. */
336 if (!file_existed) {
337 int ret;
339 if (fsp->fh->fd == -1) {
340 ret = SMB_VFS_STAT(conn, path, psbuf);
341 } else {
342 ret = SMB_VFS_FSTAT(fsp, psbuf);
343 /* If we have an fd, this stat should succeed. */
344 if (ret == -1) {
345 DEBUG(0,("Error doing fstat on open file %s "
346 "(%s)\n", path,strerror(errno) ));
350 /* For a non-io open, this stat failing means file not found. JRA */
351 if (ret == -1) {
352 status = map_nt_error_from_unix(errno);
353 fd_close(fsp);
354 return status;
359 * POSIX allows read-only opens of directories. We don't
360 * want to do this (we use a different code path for this)
361 * so catch a directory open and return an EISDIR. JRA.
364 if(S_ISDIR(psbuf->st_mode)) {
365 fd_close(fsp);
366 errno = EISDIR;
367 return NT_STATUS_FILE_IS_A_DIRECTORY;
370 fsp->mode = psbuf->st_mode;
371 fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
372 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
373 fsp->file_pid = req ? req->smbpid : 0;
374 fsp->can_lock = True;
375 fsp->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
376 if (!CAN_WRITE(conn)) {
377 fsp->can_write = False;
378 } else {
379 fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ?
380 True : False;
382 fsp->print_file = False;
383 fsp->modified = False;
384 fsp->sent_oplock_break = NO_BREAK_SENT;
385 fsp->is_directory = False;
386 fsp->is_stat = False;
387 if (conn->aio_write_behind_list &&
388 is_in_path(path, conn->aio_write_behind_list, conn->case_sensitive)) {
389 fsp->aio_write_behind = True;
392 string_set(&fsp->fsp_name, path);
393 fsp->wcp = NULL; /* Write cache pointer. */
395 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
396 *current_user_info.smb_name ?
397 current_user_info.smb_name : conn->user,fsp->fsp_name,
398 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
399 conn->num_files_open + 1));
401 errno = 0;
402 return NT_STATUS_OK;
405 /*******************************************************************
406 Return True if the filename is one of the special executable types.
407 ********************************************************************/
409 static bool is_executable(const char *fname)
411 if ((fname = strrchr_m(fname,'.'))) {
412 if (strequal(fname,".com") ||
413 strequal(fname,".dll") ||
414 strequal(fname,".exe") ||
415 strequal(fname,".sym")) {
416 return True;
419 return False;
422 /****************************************************************************
423 Check if we can open a file with a share mode.
424 Returns True if conflict, False if not.
425 ****************************************************************************/
427 static bool share_conflict(struct share_mode_entry *entry,
428 uint32 access_mask,
429 uint32 share_access)
431 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
432 "entry->share_access = 0x%x, "
433 "entry->private_options = 0x%x\n",
434 (unsigned int)entry->access_mask,
435 (unsigned int)entry->share_access,
436 (unsigned int)entry->private_options));
438 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
439 (unsigned int)access_mask, (unsigned int)share_access));
441 if ((entry->access_mask & (FILE_WRITE_DATA|
442 FILE_APPEND_DATA|
443 FILE_READ_DATA|
444 FILE_EXECUTE|
445 DELETE_ACCESS)) == 0) {
446 DEBUG(10,("share_conflict: No conflict due to "
447 "entry->access_mask = 0x%x\n",
448 (unsigned int)entry->access_mask ));
449 return False;
452 if ((access_mask & (FILE_WRITE_DATA|
453 FILE_APPEND_DATA|
454 FILE_READ_DATA|
455 FILE_EXECUTE|
456 DELETE_ACCESS)) == 0) {
457 DEBUG(10,("share_conflict: No conflict due to "
458 "access_mask = 0x%x\n",
459 (unsigned int)access_mask ));
460 return False;
463 #if 1 /* JRA TEST - Superdebug. */
464 #define CHECK_MASK(num, am, right, sa, share) \
465 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
466 (unsigned int)(num), (unsigned int)(am), \
467 (unsigned int)(right), (unsigned int)(am)&(right) )); \
468 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
469 (unsigned int)(num), (unsigned int)(sa), \
470 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
471 if (((am) & (right)) && !((sa) & (share))) { \
472 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
473 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
474 (unsigned int)(share) )); \
475 return True; \
477 #else
478 #define CHECK_MASK(num, am, right, sa, share) \
479 if (((am) & (right)) && !((sa) & (share))) { \
480 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
481 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
482 (unsigned int)(share) )); \
483 return True; \
485 #endif
487 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
488 share_access, FILE_SHARE_WRITE);
489 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
490 entry->share_access, FILE_SHARE_WRITE);
492 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
493 share_access, FILE_SHARE_READ);
494 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
495 entry->share_access, FILE_SHARE_READ);
497 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
498 share_access, FILE_SHARE_DELETE);
499 CHECK_MASK(6, access_mask, DELETE_ACCESS,
500 entry->share_access, FILE_SHARE_DELETE);
502 DEBUG(10,("share_conflict: No conflict.\n"));
503 return False;
506 #if defined(DEVELOPER)
507 static void validate_my_share_entries(int num,
508 struct share_mode_entry *share_entry)
510 files_struct *fsp;
512 if (!procid_is_me(&share_entry->pid)) {
513 return;
516 if (is_deferred_open_entry(share_entry) &&
517 !open_was_deferred(share_entry->op_mid)) {
518 char *str = talloc_asprintf(talloc_tos(),
519 "Got a deferred entry without a request: "
520 "PANIC: %s\n",
521 share_mode_str(talloc_tos(), num, share_entry));
522 smb_panic(str);
525 if (!is_valid_share_mode_entry(share_entry)) {
526 return;
529 fsp = file_find_dif(share_entry->id,
530 share_entry->share_file_id);
531 if (!fsp) {
532 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
533 share_mode_str(talloc_tos(), num, share_entry) ));
534 smb_panic("validate_my_share_entries: Cannot match a "
535 "share entry with an open file\n");
538 if (is_deferred_open_entry(share_entry) ||
539 is_unused_share_mode_entry(share_entry)) {
540 goto panic;
543 if ((share_entry->op_type == NO_OPLOCK) &&
544 (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK)) {
545 /* Someone has already written to it, but I haven't yet
546 * noticed */
547 return;
550 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
551 goto panic;
554 return;
556 panic:
558 char *str;
559 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
560 share_mode_str(talloc_tos(), num, share_entry) ));
561 str = talloc_asprintf(talloc_tos(),
562 "validate_my_share_entries: "
563 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
564 fsp->fsp_name, (unsigned int)fsp->oplock_type,
565 (unsigned int)share_entry->op_type );
566 smb_panic(str);
569 #endif
571 static bool is_stat_open(uint32 access_mask)
573 return (access_mask &&
574 ((access_mask & ~(SYNCHRONIZE_ACCESS| FILE_READ_ATTRIBUTES|
575 FILE_WRITE_ATTRIBUTES))==0) &&
576 ((access_mask & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|
577 FILE_WRITE_ATTRIBUTES)) != 0));
580 /****************************************************************************
581 Deal with share modes
582 Invarient: Share mode must be locked on entry and exit.
583 Returns -1 on error, or number of share modes on success (may be zero).
584 ****************************************************************************/
586 static NTSTATUS open_mode_check(connection_struct *conn,
587 const char *fname,
588 struct share_mode_lock *lck,
589 uint32 access_mask,
590 uint32 share_access,
591 uint32 create_options,
592 bool *file_existed)
594 int i;
596 if(lck->num_share_modes == 0) {
597 return NT_STATUS_OK;
600 *file_existed = True;
602 /* A delete on close prohibits everything */
604 if (lck->delete_on_close) {
605 return NT_STATUS_DELETE_PENDING;
608 if (is_stat_open(access_mask)) {
609 /* Stat open that doesn't trigger oplock breaks or share mode
610 * checks... ! JRA. */
611 return NT_STATUS_OK;
615 * Check if the share modes will give us access.
618 #if defined(DEVELOPER)
619 for(i = 0; i < lck->num_share_modes; i++) {
620 validate_my_share_entries(i, &lck->share_modes[i]);
622 #endif
624 if (!lp_share_modes(SNUM(conn))) {
625 return NT_STATUS_OK;
628 /* Now we check the share modes, after any oplock breaks. */
629 for(i = 0; i < lck->num_share_modes; i++) {
631 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
632 continue;
635 /* someone else has a share lock on it, check to see if we can
636 * too */
637 if (share_conflict(&lck->share_modes[i],
638 access_mask, share_access)) {
639 return NT_STATUS_SHARING_VIOLATION;
643 return NT_STATUS_OK;
646 static bool is_delete_request(files_struct *fsp) {
647 return ((fsp->access_mask == DELETE_ACCESS) &&
648 (fsp->oplock_type == NO_OPLOCK));
652 * 1) No files open at all or internal open: Grant whatever the client wants.
654 * 2) Exclusive (or batch) oplock around: If the requested access is a delete
655 * request, break if the oplock around is a batch oplock. If it's another
656 * requested access type, break.
658 * 3) Only level2 around: Grant level2 and do nothing else.
661 static bool delay_for_oplocks(struct share_mode_lock *lck,
662 files_struct *fsp,
663 uint16 mid,
664 int pass_number,
665 int oplock_request)
667 int i;
668 struct share_mode_entry *exclusive = NULL;
669 bool valid_entry = False;
670 bool delay_it = False;
671 bool have_level2 = False;
672 NTSTATUS status;
673 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
675 if (oplock_request & INTERNAL_OPEN_ONLY) {
676 fsp->oplock_type = NO_OPLOCK;
679 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
680 return False;
683 for (i=0; i<lck->num_share_modes; i++) {
685 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
686 continue;
689 /* At least one entry is not an invalid or deferred entry. */
690 valid_entry = True;
692 if (pass_number == 1) {
693 if (BATCH_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
694 SMB_ASSERT(exclusive == NULL);
695 exclusive = &lck->share_modes[i];
697 } else {
698 if (EXCLUSIVE_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
699 SMB_ASSERT(exclusive == NULL);
700 exclusive = &lck->share_modes[i];
704 if (lck->share_modes[i].op_type == LEVEL_II_OPLOCK) {
705 SMB_ASSERT(exclusive == NULL);
706 have_level2 = True;
710 if (!valid_entry) {
711 /* All entries are placeholders or deferred.
712 * Directly grant whatever the client wants. */
713 if (fsp->oplock_type == NO_OPLOCK) {
714 /* Store a level2 oplock, but don't tell the client */
715 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
717 return False;
720 if (exclusive != NULL) { /* Found an exclusive oplock */
721 SMB_ASSERT(!have_level2);
722 delay_it = is_delete_request(fsp) ?
723 BATCH_OPLOCK_TYPE(exclusive->op_type) : True;
726 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
727 /* We can at most grant level2 as there are other
728 * level2 or NO_OPLOCK entries. */
729 fsp->oplock_type = LEVEL_II_OPLOCK;
732 if ((fsp->oplock_type == NO_OPLOCK) && have_level2) {
733 /* Store a level2 oplock, but don't tell the client */
734 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
737 if (!delay_it) {
738 return False;
742 * Send a break message to the oplock holder and delay the open for
743 * our client.
746 DEBUG(10, ("Sending break request to PID %s\n",
747 procid_str_static(&exclusive->pid)));
748 exclusive->op_mid = mid;
750 /* Create the message. */
751 share_mode_entry_to_message(msg, exclusive);
753 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
754 don't want this set in the share mode struct pointed to by lck. */
756 if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
757 SSVAL(msg,6,exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
760 status = messaging_send_buf(smbd_messaging_context(), exclusive->pid,
761 MSG_SMB_BREAK_REQUEST,
762 (uint8 *)msg,
763 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
764 if (!NT_STATUS_IS_OK(status)) {
765 DEBUG(3, ("Could not send oplock break message: %s\n",
766 nt_errstr(status)));
769 return True;
772 static bool request_timed_out(struct timeval request_time,
773 struct timeval timeout)
775 struct timeval now, end_time;
776 GetTimeOfDay(&now);
777 end_time = timeval_sum(&request_time, &timeout);
778 return (timeval_compare(&end_time, &now) < 0);
781 /****************************************************************************
782 Handle the 1 second delay in returning a SHARING_VIOLATION error.
783 ****************************************************************************/
785 static void defer_open(struct share_mode_lock *lck,
786 struct timeval request_time,
787 struct timeval timeout,
788 struct smb_request *req,
789 struct deferred_open_record *state)
791 int i;
793 /* Paranoia check */
795 for (i=0; i<lck->num_share_modes; i++) {
796 struct share_mode_entry *e = &lck->share_modes[i];
798 if (!is_deferred_open_entry(e)) {
799 continue;
802 if (procid_is_me(&e->pid) && (e->op_mid == req->mid)) {
803 DEBUG(0, ("Trying to defer an already deferred "
804 "request: mid=%d, exiting\n", req->mid));
805 exit_server("attempt to defer a deferred request");
809 /* End paranoia check */
811 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
812 "open entry for mid %u\n",
813 (unsigned int)request_time.tv_sec,
814 (unsigned int)request_time.tv_usec,
815 (unsigned int)req->mid));
817 if (!push_deferred_smb_message(req, request_time, timeout,
818 (char *)state, sizeof(*state))) {
819 exit_server("push_deferred_smb_message failed");
821 add_deferred_open(lck, req->mid, request_time, state->id);
824 * Push the MID of this packet on the signing queue.
825 * We only do this once, the first time we push the packet
826 * onto the deferred open queue, as this has a side effect
827 * of incrementing the response sequence number.
830 srv_defer_sign_response(req->mid);
834 /****************************************************************************
835 On overwrite open ensure that the attributes match.
836 ****************************************************************************/
838 static bool open_match_attributes(connection_struct *conn,
839 const char *path,
840 uint32 old_dos_attr,
841 uint32 new_dos_attr,
842 mode_t existing_unx_mode,
843 mode_t new_unx_mode,
844 mode_t *returned_unx_mode)
846 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
848 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
849 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
851 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
852 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
853 *returned_unx_mode = new_unx_mode;
854 } else {
855 *returned_unx_mode = (mode_t)0;
858 DEBUG(10,("open_match_attributes: file %s old_dos_attr = 0x%x, "
859 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
860 "returned_unx_mode = 0%o\n",
861 path,
862 (unsigned int)old_dos_attr,
863 (unsigned int)existing_unx_mode,
864 (unsigned int)new_dos_attr,
865 (unsigned int)*returned_unx_mode ));
867 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
868 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
869 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
870 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
871 return False;
874 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
875 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
876 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
877 return False;
880 return True;
883 /****************************************************************************
884 Special FCB or DOS processing in the case of a sharing violation.
885 Try and find a duplicated file handle.
886 ****************************************************************************/
888 static files_struct *fcb_or_dos_open(connection_struct *conn,
889 const char *fname,
890 struct file_id id,
891 uint16 file_pid,
892 uint16 vuid,
893 uint32 access_mask,
894 uint32 share_access,
895 uint32 create_options)
897 files_struct *fsp;
898 files_struct *dup_fsp;
900 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
901 "file %s.\n", fname ));
903 for(fsp = file_find_di_first(id); fsp;
904 fsp = file_find_di_next(fsp)) {
906 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
907 "vuid = %u, file_pid = %u, private_options = 0x%x "
908 "access_mask = 0x%x\n", fsp->fsp_name,
909 fsp->fh->fd, (unsigned int)fsp->vuid,
910 (unsigned int)fsp->file_pid,
911 (unsigned int)fsp->fh->private_options,
912 (unsigned int)fsp->access_mask ));
914 if (fsp->fh->fd != -1 &&
915 fsp->vuid == vuid &&
916 fsp->file_pid == file_pid &&
917 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
918 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
919 (fsp->access_mask & FILE_WRITE_DATA) &&
920 strequal(fsp->fsp_name, fname)) {
921 DEBUG(10,("fcb_or_dos_open: file match\n"));
922 break;
926 if (!fsp) {
927 return NULL;
930 /* quite an insane set of semantics ... */
931 if (is_executable(fname) &&
932 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
933 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
934 return NULL;
937 /* We need to duplicate this fsp. */
938 if (!NT_STATUS_IS_OK(dup_file_fsp(fsp, access_mask, share_access,
939 create_options, &dup_fsp))) {
940 return NULL;
943 return dup_fsp;
946 /****************************************************************************
947 Open a file with a share mode - old openX method - map into NTCreate.
948 ****************************************************************************/
950 bool map_open_params_to_ntcreate(const char *fname, int deny_mode, int open_func,
951 uint32 *paccess_mask,
952 uint32 *pshare_mode,
953 uint32 *pcreate_disposition,
954 uint32 *pcreate_options)
956 uint32 access_mask;
957 uint32 share_mode;
958 uint32 create_disposition;
959 uint32 create_options = 0;
961 DEBUG(10,("map_open_params_to_ntcreate: fname = %s, deny_mode = 0x%x, "
962 "open_func = 0x%x\n",
963 fname, (unsigned int)deny_mode, (unsigned int)open_func ));
965 /* Create the NT compatible access_mask. */
966 switch (GET_OPENX_MODE(deny_mode)) {
967 case DOS_OPEN_EXEC: /* Implies read-only - used to be FILE_READ_DATA */
968 case DOS_OPEN_RDONLY:
969 access_mask = FILE_GENERIC_READ;
970 break;
971 case DOS_OPEN_WRONLY:
972 access_mask = FILE_GENERIC_WRITE;
973 break;
974 case DOS_OPEN_RDWR:
975 case DOS_OPEN_FCB:
976 access_mask = FILE_GENERIC_READ|FILE_GENERIC_WRITE;
977 break;
978 default:
979 DEBUG(10,("map_open_params_to_ntcreate: bad open mode = 0x%x\n",
980 (unsigned int)GET_OPENX_MODE(deny_mode)));
981 return False;
984 /* Create the NT compatible create_disposition. */
985 switch (open_func) {
986 case OPENX_FILE_EXISTS_FAIL|OPENX_FILE_CREATE_IF_NOT_EXIST:
987 create_disposition = FILE_CREATE;
988 break;
990 case OPENX_FILE_EXISTS_OPEN:
991 create_disposition = FILE_OPEN;
992 break;
994 case OPENX_FILE_EXISTS_OPEN|OPENX_FILE_CREATE_IF_NOT_EXIST:
995 create_disposition = FILE_OPEN_IF;
996 break;
998 case OPENX_FILE_EXISTS_TRUNCATE:
999 create_disposition = FILE_OVERWRITE;
1000 break;
1002 case OPENX_FILE_EXISTS_TRUNCATE|OPENX_FILE_CREATE_IF_NOT_EXIST:
1003 create_disposition = FILE_OVERWRITE_IF;
1004 break;
1006 default:
1007 /* From samba4 - to be confirmed. */
1008 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_EXEC) {
1009 create_disposition = FILE_CREATE;
1010 break;
1012 DEBUG(10,("map_open_params_to_ntcreate: bad "
1013 "open_func 0x%x\n", (unsigned int)open_func));
1014 return False;
1017 /* Create the NT compatible share modes. */
1018 switch (GET_DENY_MODE(deny_mode)) {
1019 case DENY_ALL:
1020 share_mode = FILE_SHARE_NONE;
1021 break;
1023 case DENY_WRITE:
1024 share_mode = FILE_SHARE_READ;
1025 break;
1027 case DENY_READ:
1028 share_mode = FILE_SHARE_WRITE;
1029 break;
1031 case DENY_NONE:
1032 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1033 break;
1035 case DENY_DOS:
1036 create_options |= NTCREATEX_OPTIONS_PRIVATE_DENY_DOS;
1037 if (is_executable(fname)) {
1038 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1039 } else {
1040 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_RDONLY) {
1041 share_mode = FILE_SHARE_READ;
1042 } else {
1043 share_mode = FILE_SHARE_NONE;
1046 break;
1048 case DENY_FCB:
1049 create_options |= NTCREATEX_OPTIONS_PRIVATE_DENY_FCB;
1050 share_mode = FILE_SHARE_NONE;
1051 break;
1053 default:
1054 DEBUG(10,("map_open_params_to_ntcreate: bad deny_mode 0x%x\n",
1055 (unsigned int)GET_DENY_MODE(deny_mode) ));
1056 return False;
1059 DEBUG(10,("map_open_params_to_ntcreate: file %s, access_mask = 0x%x, "
1060 "share_mode = 0x%x, create_disposition = 0x%x, "
1061 "create_options = 0x%x\n",
1062 fname,
1063 (unsigned int)access_mask,
1064 (unsigned int)share_mode,
1065 (unsigned int)create_disposition,
1066 (unsigned int)create_options ));
1068 if (paccess_mask) {
1069 *paccess_mask = access_mask;
1071 if (pshare_mode) {
1072 *pshare_mode = share_mode;
1074 if (pcreate_disposition) {
1075 *pcreate_disposition = create_disposition;
1077 if (pcreate_options) {
1078 *pcreate_options = create_options;
1081 return True;
1085 static void schedule_defer_open(struct share_mode_lock *lck,
1086 struct timeval request_time,
1087 struct smb_request *req)
1089 struct deferred_open_record state;
1091 /* This is a relative time, added to the absolute
1092 request_time value to get the absolute timeout time.
1093 Note that if this is the second or greater time we enter
1094 this codepath for this particular request mid then
1095 request_time is left as the absolute time of the *first*
1096 time this request mid was processed. This is what allows
1097 the request to eventually time out. */
1099 struct timeval timeout;
1101 /* Normally the smbd we asked should respond within
1102 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1103 * the client did, give twice the timeout as a safety
1104 * measure here in case the other smbd is stuck
1105 * somewhere else. */
1107 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1109 /* Nothing actually uses state.delayed_for_oplocks
1110 but it's handy to differentiate in debug messages
1111 between a 30 second delay due to oplock break, and
1112 a 1 second delay for share mode conflicts. */
1114 state.delayed_for_oplocks = True;
1115 state.id = lck->id;
1117 if (!request_timed_out(request_time, timeout)) {
1118 defer_open(lck, request_time, timeout, req, &state);
1122 /****************************************************************************
1123 Open a file with a share mode.
1124 ****************************************************************************/
1126 NTSTATUS open_file_ntcreate(connection_struct *conn,
1127 struct smb_request *req,
1128 const char *fname,
1129 SMB_STRUCT_STAT *psbuf,
1130 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1131 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1132 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1133 uint32 create_options, /* options such as delete on close. */
1134 uint32 new_dos_attributes, /* attributes used for new file. */
1135 int oplock_request, /* internal Samba oplock codes. */
1136 /* Information (FILE_EXISTS etc.) */
1137 int *pinfo,
1138 files_struct **result)
1140 int flags=0;
1141 int flags2=0;
1142 bool file_existed = VALID_STAT(*psbuf);
1143 bool def_acl = False;
1144 bool posix_open = False;
1145 bool new_file_created = False;
1146 struct file_id id;
1147 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1148 files_struct *fsp = NULL;
1149 mode_t new_unx_mode = (mode_t)0;
1150 mode_t unx_mode = (mode_t)0;
1151 int info;
1152 uint32 existing_dos_attributes = 0;
1153 struct pending_message_list *pml = NULL;
1154 struct timeval request_time = timeval_zero();
1155 struct share_mode_lock *lck = NULL;
1156 uint32 open_access_mask = access_mask;
1157 NTSTATUS status;
1158 int ret_flock;
1159 char *parent_dir;
1160 const char *newname;
1162 ZERO_STRUCT(id);
1164 if (conn->printer) {
1166 * Printers are handled completely differently.
1167 * Most of the passed parameters are ignored.
1170 if (pinfo) {
1171 *pinfo = FILE_WAS_CREATED;
1174 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n", fname));
1176 return print_fsp_open(conn, fname, result);
1179 if (!parent_dirname_talloc(talloc_tos(), fname, &parent_dir,
1180 &newname)) {
1181 return NT_STATUS_NO_MEMORY;
1184 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1185 posix_open = True;
1186 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1187 new_dos_attributes = 0;
1188 } else {
1189 /* We add aARCH to this as this mode is only used if the file is
1190 * created new. */
1191 unx_mode = unix_mode(conn, new_dos_attributes | aARCH, fname,
1192 parent_dir);
1195 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1196 "access_mask=0x%x share_access=0x%x "
1197 "create_disposition = 0x%x create_options=0x%x "
1198 "unix mode=0%o oplock_request=%d\n",
1199 fname, new_dos_attributes, access_mask, share_access,
1200 create_disposition, create_options, unx_mode,
1201 oplock_request));
1203 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
1204 DEBUG(0, ("No smb request but not an internal only open!\n"));
1205 return NT_STATUS_INTERNAL_ERROR;
1209 * Only non-internal opens can be deferred at all
1212 if ((req != NULL)
1213 && ((pml = get_open_deferred_message(req->mid)) != NULL)) {
1214 struct deferred_open_record *state =
1215 (struct deferred_open_record *)pml->private_data.data;
1217 /* Remember the absolute time of the original
1218 request with this mid. We'll use it later to
1219 see if this has timed out. */
1221 request_time = pml->request_time;
1223 /* Remove the deferred open entry under lock. */
1224 lck = get_share_mode_lock(talloc_tos(), state->id, NULL, NULL,
1225 NULL);
1226 if (lck == NULL) {
1227 DEBUG(0, ("could not get share mode lock\n"));
1228 } else {
1229 del_deferred_open_entry(lck, req->mid);
1230 TALLOC_FREE(lck);
1233 /* Ensure we don't reprocess this message. */
1234 remove_deferred_open_smb_message(req->mid);
1237 status = check_name(conn, fname);
1238 if (!NT_STATUS_IS_OK(status)) {
1239 return status;
1242 if (!posix_open) {
1243 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
1244 if (file_existed) {
1245 existing_dos_attributes = dos_mode(conn, fname, psbuf);
1249 /* ignore any oplock requests if oplocks are disabled */
1250 if (!lp_oplocks(SNUM(conn)) || global_client_failed_oplock_break ||
1251 IS_VETO_OPLOCK_PATH(conn, fname)) {
1252 /* Mask off everything except the private Samba bits. */
1253 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
1256 /* this is for OS/2 long file names - say we don't support them */
1257 if (!lp_posix_pathnames() && strstr(fname,".+,;=[].")) {
1258 /* OS/2 Workplace shell fix may be main code stream in a later
1259 * release. */
1260 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1261 "supported.\n"));
1262 if (use_nt_status()) {
1263 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1265 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
1268 switch( create_disposition ) {
1270 * Currently we're using FILE_SUPERSEDE as the same as
1271 * FILE_OVERWRITE_IF but they really are
1272 * different. FILE_SUPERSEDE deletes an existing file
1273 * (requiring delete access) then recreates it.
1275 case FILE_SUPERSEDE:
1276 /* If file exists replace/overwrite. If file doesn't
1277 * exist create. */
1278 flags2 |= (O_CREAT | O_TRUNC);
1279 break;
1281 case FILE_OVERWRITE_IF:
1282 /* If file exists replace/overwrite. If file doesn't
1283 * exist create. */
1284 flags2 |= (O_CREAT | O_TRUNC);
1285 break;
1287 case FILE_OPEN:
1288 /* If file exists open. If file doesn't exist error. */
1289 if (!file_existed) {
1290 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1291 "requested for file %s and file "
1292 "doesn't exist.\n", fname ));
1293 errno = ENOENT;
1294 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1296 break;
1298 case FILE_OVERWRITE:
1299 /* If file exists overwrite. If file doesn't exist
1300 * error. */
1301 if (!file_existed) {
1302 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1303 "requested for file %s and file "
1304 "doesn't exist.\n", fname ));
1305 errno = ENOENT;
1306 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1308 flags2 |= O_TRUNC;
1309 break;
1311 case FILE_CREATE:
1312 /* If file exists error. If file doesn't exist
1313 * create. */
1314 if (file_existed) {
1315 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1316 "requested for file %s and file "
1317 "already exists.\n", fname ));
1318 if (S_ISDIR(psbuf->st_mode)) {
1319 errno = EISDIR;
1320 } else {
1321 errno = EEXIST;
1323 return map_nt_error_from_unix(errno);
1325 flags2 |= (O_CREAT|O_EXCL);
1326 break;
1328 case FILE_OPEN_IF:
1329 /* If file exists open. If file doesn't exist
1330 * create. */
1331 flags2 |= O_CREAT;
1332 break;
1334 default:
1335 return NT_STATUS_INVALID_PARAMETER;
1338 /* We only care about matching attributes on file exists and
1339 * overwrite. */
1341 if (!posix_open && file_existed && ((create_disposition == FILE_OVERWRITE) ||
1342 (create_disposition == FILE_OVERWRITE_IF))) {
1343 if (!open_match_attributes(conn, fname,
1344 existing_dos_attributes,
1345 new_dos_attributes, psbuf->st_mode,
1346 unx_mode, &new_unx_mode)) {
1347 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1348 "for file %s (%x %x) (0%o, 0%o)\n",
1349 fname, existing_dos_attributes,
1350 new_dos_attributes,
1351 (unsigned int)psbuf->st_mode,
1352 (unsigned int)unx_mode ));
1353 errno = EACCES;
1354 return NT_STATUS_ACCESS_DENIED;
1358 /* This is a nasty hack - must fix... JRA. */
1359 if (access_mask == MAXIMUM_ALLOWED_ACCESS) {
1360 open_access_mask = access_mask = FILE_GENERIC_ALL;
1364 * Convert GENERIC bits to specific bits.
1367 se_map_generic(&access_mask, &file_generic_mapping);
1368 open_access_mask = access_mask;
1370 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1371 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
1374 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1375 "access_mask=0x%x\n", fname, access_mask ));
1378 * Note that we ignore the append flag as append does not
1379 * mean the same thing under DOS and Unix.
1382 if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
1383 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1384 /* DENY_DOS opens are always underlying read-write on the
1385 file handle, no matter what the requested access mask
1386 says. */
1387 if ((create_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
1388 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|FILE_READ_EA|FILE_EXECUTE)) {
1389 flags = O_RDWR;
1390 } else {
1391 flags = O_WRONLY;
1393 } else {
1394 flags = O_RDONLY;
1398 * Currently we only look at FILE_WRITE_THROUGH for create options.
1401 #if defined(O_SYNC)
1402 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
1403 flags2 |= O_SYNC;
1405 #endif /* O_SYNC */
1407 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
1408 flags2 |= O_APPEND;
1411 if (!posix_open && !CAN_WRITE(conn)) {
1413 * We should really return a permission denied error if either
1414 * O_CREAT or O_TRUNC are set, but for compatibility with
1415 * older versions of Samba we just AND them out.
1417 flags2 &= ~(O_CREAT|O_TRUNC);
1421 * Ensure we can't write on a read-only share or file.
1424 if (flags != O_RDONLY && file_existed &&
1425 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
1426 DEBUG(5,("open_file_ntcreate: write access requested for "
1427 "file %s on read only %s\n",
1428 fname, !CAN_WRITE(conn) ? "share" : "file" ));
1429 errno = EACCES;
1430 return NT_STATUS_ACCESS_DENIED;
1433 status = file_new(conn, &fsp);
1434 if(!NT_STATUS_IS_OK(status)) {
1435 return status;
1438 fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
1439 fsp->share_access = share_access;
1440 fsp->fh->private_options = create_options;
1441 fsp->access_mask = open_access_mask; /* We change this to the
1442 * requested access_mask after
1443 * the open is done. */
1444 fsp->posix_open = posix_open;
1446 /* Ensure no SAMBA_PRIVATE bits can be set. */
1447 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1449 if (timeval_is_zero(&request_time)) {
1450 request_time = fsp->open_time;
1453 if (file_existed) {
1454 struct timespec old_write_time = get_mtimespec(psbuf);
1455 id = vfs_file_id_from_sbuf(conn, psbuf);
1457 lck = get_share_mode_lock(talloc_tos(), id,
1458 conn->connectpath,
1459 fname, &old_write_time);
1461 if (lck == NULL) {
1462 file_free(fsp);
1463 DEBUG(0, ("Could not get share mode lock\n"));
1464 return NT_STATUS_SHARING_VIOLATION;
1467 /* First pass - send break only on batch oplocks. */
1468 if ((req != NULL)
1469 && delay_for_oplocks(lck, fsp, req->mid, 1,
1470 oplock_request)) {
1471 schedule_defer_open(lck, request_time, req);
1472 TALLOC_FREE(lck);
1473 file_free(fsp);
1474 return NT_STATUS_SHARING_VIOLATION;
1477 /* Use the client requested access mask here, not the one we
1478 * open with. */
1479 status = open_mode_check(conn, fname, lck,
1480 access_mask, share_access,
1481 create_options, &file_existed);
1483 if (NT_STATUS_IS_OK(status)) {
1484 /* We might be going to allow this open. Check oplock
1485 * status again. */
1486 /* Second pass - send break for both batch or
1487 * exclusive oplocks. */
1488 if ((req != NULL)
1489 && delay_for_oplocks(lck, fsp, req->mid, 2,
1490 oplock_request)) {
1491 schedule_defer_open(lck, request_time, req);
1492 TALLOC_FREE(lck);
1493 file_free(fsp);
1494 return NT_STATUS_SHARING_VIOLATION;
1498 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
1499 /* DELETE_PENDING is not deferred for a second */
1500 TALLOC_FREE(lck);
1501 file_free(fsp);
1502 return status;
1505 if (!NT_STATUS_IS_OK(status)) {
1506 uint32 can_access_mask;
1507 bool can_access = True;
1509 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
1511 /* Check if this can be done with the deny_dos and fcb
1512 * calls. */
1513 if (create_options &
1514 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
1515 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
1516 files_struct *fsp_dup;
1518 if (req == NULL) {
1519 DEBUG(0, ("DOS open without an SMB "
1520 "request!\n"));
1521 TALLOC_FREE(lck);
1522 file_free(fsp);
1523 return NT_STATUS_INTERNAL_ERROR;
1526 /* Use the client requested access mask here,
1527 * not the one we open with. */
1528 fsp_dup = fcb_or_dos_open(conn, fname, id,
1529 req->smbpid,
1530 req->vuid,
1531 access_mask,
1532 share_access,
1533 create_options);
1535 if (fsp_dup) {
1536 TALLOC_FREE(lck);
1537 file_free(fsp);
1538 if (pinfo) {
1539 *pinfo = FILE_WAS_OPENED;
1541 conn->num_files_open++;
1542 *result = fsp_dup;
1543 return NT_STATUS_OK;
1548 * This next line is a subtlety we need for
1549 * MS-Access. If a file open will fail due to share
1550 * permissions and also for security (access) reasons,
1551 * we need to return the access failed error, not the
1552 * share error. We can't open the file due to kernel
1553 * oplock deadlock (it's possible we failed above on
1554 * the open_mode_check()) so use a userspace check.
1557 if (flags & O_RDWR) {
1558 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
1559 } else if (flags & O_WRONLY) {
1560 can_access_mask = FILE_WRITE_DATA;
1561 } else {
1562 can_access_mask = FILE_READ_DATA;
1565 if (((can_access_mask & FILE_WRITE_DATA) && !CAN_WRITE(conn)) ||
1566 !can_access_file(conn,fname,psbuf,can_access_mask)) {
1567 can_access = False;
1571 * If we're returning a share violation, ensure we
1572 * cope with the braindead 1 second delay.
1575 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
1576 lp_defer_sharing_violations()) {
1577 struct timeval timeout;
1578 struct deferred_open_record state;
1579 int timeout_usecs;
1581 /* this is a hack to speed up torture tests
1582 in 'make test' */
1583 timeout_usecs = lp_parm_int(SNUM(conn),
1584 "smbd","sharedelay",
1585 SHARING_VIOLATION_USEC_WAIT);
1587 /* This is a relative time, added to the absolute
1588 request_time value to get the absolute timeout time.
1589 Note that if this is the second or greater time we enter
1590 this codepath for this particular request mid then
1591 request_time is left as the absolute time of the *first*
1592 time this request mid was processed. This is what allows
1593 the request to eventually time out. */
1595 timeout = timeval_set(0, timeout_usecs);
1597 /* Nothing actually uses state.delayed_for_oplocks
1598 but it's handy to differentiate in debug messages
1599 between a 30 second delay due to oplock break, and
1600 a 1 second delay for share mode conflicts. */
1602 state.delayed_for_oplocks = False;
1603 state.id = id;
1605 if ((req != NULL)
1606 && !request_timed_out(request_time,
1607 timeout)) {
1608 defer_open(lck, request_time, timeout,
1609 req, &state);
1613 TALLOC_FREE(lck);
1614 if (can_access) {
1616 * We have detected a sharing violation here
1617 * so return the correct error code
1619 status = NT_STATUS_SHARING_VIOLATION;
1620 } else {
1621 status = NT_STATUS_ACCESS_DENIED;
1623 file_free(fsp);
1624 return status;
1628 * We exit this block with the share entry *locked*.....
1632 SMB_ASSERT(!file_existed || (lck != NULL));
1635 * Ensure we pay attention to default ACLs on directories if required.
1638 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
1639 (def_acl = directory_has_default_acl(conn, parent_dir))) {
1640 unx_mode = 0777;
1643 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
1644 "access_mask = 0x%x, open_access_mask = 0x%x\n",
1645 (unsigned int)flags, (unsigned int)flags2,
1646 (unsigned int)unx_mode, (unsigned int)access_mask,
1647 (unsigned int)open_access_mask));
1650 * open_file strips any O_TRUNC flags itself.
1653 fsp_open = open_file(fsp, conn, req, parent_dir, newname, fname, psbuf,
1654 flags|flags2, unx_mode, access_mask,
1655 open_access_mask);
1657 if (!NT_STATUS_IS_OK(fsp_open)) {
1658 if (lck != NULL) {
1659 TALLOC_FREE(lck);
1661 file_free(fsp);
1662 return fsp_open;
1665 if (!file_existed) {
1666 struct timespec old_write_time = get_mtimespec(psbuf);
1668 * Deal with the race condition where two smbd's detect the
1669 * file doesn't exist and do the create at the same time. One
1670 * of them will win and set a share mode, the other (ie. this
1671 * one) should check if the requested share mode for this
1672 * create is allowed.
1676 * Now the file exists and fsp is successfully opened,
1677 * fsp->dev and fsp->inode are valid and should replace the
1678 * dev=0,inode=0 from a non existent file. Spotted by
1679 * Nadav Danieli <nadavd@exanet.com>. JRA.
1682 id = fsp->file_id;
1684 lck = get_share_mode_lock(talloc_tos(), id,
1685 conn->connectpath,
1686 fname, &old_write_time);
1688 if (lck == NULL) {
1689 DEBUG(0, ("open_file_ntcreate: Could not get share "
1690 "mode lock for %s\n", fname));
1691 fd_close(fsp);
1692 file_free(fsp);
1693 return NT_STATUS_SHARING_VIOLATION;
1696 /* First pass - send break only on batch oplocks. */
1697 if ((req != NULL)
1698 && delay_for_oplocks(lck, fsp, req->mid, 1,
1699 oplock_request)) {
1700 schedule_defer_open(lck, request_time, req);
1701 TALLOC_FREE(lck);
1702 fd_close(fsp);
1703 file_free(fsp);
1704 return NT_STATUS_SHARING_VIOLATION;
1707 status = open_mode_check(conn, fname, lck,
1708 access_mask, share_access,
1709 create_options, &file_existed);
1711 if (NT_STATUS_IS_OK(status)) {
1712 /* We might be going to allow this open. Check oplock
1713 * status again. */
1714 /* Second pass - send break for both batch or
1715 * exclusive oplocks. */
1716 if ((req != NULL)
1717 && delay_for_oplocks(lck, fsp, req->mid, 2,
1718 oplock_request)) {
1719 schedule_defer_open(lck, request_time, req);
1720 TALLOC_FREE(lck);
1721 fd_close(fsp);
1722 file_free(fsp);
1723 return NT_STATUS_SHARING_VIOLATION;
1727 if (!NT_STATUS_IS_OK(status)) {
1728 struct deferred_open_record state;
1730 fd_close(fsp);
1731 file_free(fsp);
1733 state.delayed_for_oplocks = False;
1734 state.id = id;
1736 /* Do it all over again immediately. In the second
1737 * round we will find that the file existed and handle
1738 * the DELETE_PENDING and FCB cases correctly. No need
1739 * to duplicate the code here. Essentially this is a
1740 * "goto top of this function", but don't tell
1741 * anybody... */
1743 if (req != NULL) {
1744 defer_open(lck, request_time, timeval_zero(),
1745 req, &state);
1747 TALLOC_FREE(lck);
1748 return status;
1752 * We exit this block with the share entry *locked*.....
1757 SMB_ASSERT(lck != NULL);
1759 /* note that we ignore failure for the following. It is
1760 basically a hack for NFS, and NFS will never set one of
1761 these only read them. Nobody but Samba can ever set a deny
1762 mode and we have already checked our more authoritative
1763 locking database for permission to set this deny mode. If
1764 the kernel refuses the operations then the kernel is wrong.
1765 note that GPFS supports it as well - jmcd */
1767 if (fsp->fh->fd != -1) {
1768 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access);
1769 if(ret_flock == -1 ){
1771 TALLOC_FREE(lck);
1772 fd_close(fsp);
1773 file_free(fsp);
1775 return NT_STATUS_SHARING_VIOLATION;
1780 * At this point onwards, we can guarentee that the share entry
1781 * is locked, whether we created the file or not, and that the
1782 * deny mode is compatible with all current opens.
1786 * If requested, truncate the file.
1789 if (flags2&O_TRUNC) {
1791 * We are modifing the file after open - update the stat
1792 * struct..
1794 if ((SMB_VFS_FTRUNCATE(fsp, 0) == -1) ||
1795 (SMB_VFS_FSTAT(fsp, psbuf)==-1)) {
1796 status = map_nt_error_from_unix(errno);
1797 TALLOC_FREE(lck);
1798 fd_close(fsp);
1799 file_free(fsp);
1800 return status;
1804 /* Record the options we were opened with. */
1805 fsp->share_access = share_access;
1806 fsp->fh->private_options = create_options;
1807 fsp->access_mask = access_mask;
1809 if (file_existed) {
1810 /* stat opens on existing files don't get oplocks. */
1811 if (is_stat_open(open_access_mask)) {
1812 fsp->oplock_type = NO_OPLOCK;
1815 if (!(flags2 & O_TRUNC)) {
1816 info = FILE_WAS_OPENED;
1817 } else {
1818 info = FILE_WAS_OVERWRITTEN;
1820 } else {
1821 info = FILE_WAS_CREATED;
1824 if (pinfo) {
1825 *pinfo = info;
1829 * Setup the oplock info in both the shared memory and
1830 * file structs.
1833 if ((fsp->oplock_type != NO_OPLOCK) &&
1834 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK)) {
1835 if (!set_file_oplock(fsp, fsp->oplock_type)) {
1836 /* Could not get the kernel oplock */
1837 fsp->oplock_type = NO_OPLOCK;
1841 if (info == FILE_WAS_OVERWRITTEN || info == FILE_WAS_CREATED || info == FILE_WAS_SUPERSEDED) {
1842 new_file_created = True;
1845 set_share_mode(lck, fsp, current_user.ut.uid, 0, fsp->oplock_type, new_file_created);
1847 /* Handle strange delete on close create semantics. */
1848 if ((create_options & FILE_DELETE_ON_CLOSE)
1849 && (is_ntfs_stream_name(fname)
1850 || can_set_initial_delete_on_close(lck))) {
1851 status = can_set_delete_on_close(fsp, True, new_dos_attributes);
1853 if (!NT_STATUS_IS_OK(status)) {
1854 /* Remember to delete the mode we just added. */
1855 del_share_mode(lck, fsp);
1856 TALLOC_FREE(lck);
1857 fd_close(fsp);
1858 file_free(fsp);
1859 return status;
1861 /* Note that here we set the *inital* delete on close flag,
1862 not the regular one. The magic gets handled in close. */
1863 fsp->initial_delete_on_close = True;
1866 if (new_file_created) {
1867 /* Files should be initially set as archive */
1868 if (lp_map_archive(SNUM(conn)) ||
1869 lp_store_dos_attributes(SNUM(conn))) {
1870 if (!posix_open) {
1871 SMB_STRUCT_STAT tmp_sbuf;
1872 SET_STAT_INVALID(tmp_sbuf);
1873 if (file_set_dosmode(
1874 conn, fname,
1875 new_dos_attributes | aARCH,
1876 &tmp_sbuf, parent_dir,
1877 true) == 0) {
1878 unx_mode = tmp_sbuf.st_mode;
1885 * Take care of inherited ACLs on created files - if default ACL not
1886 * selected.
1889 if (!posix_open && !file_existed && !def_acl) {
1891 int saved_errno = errno; /* We might get ENOSYS in the next
1892 * call.. */
1894 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
1895 errno == ENOSYS) {
1896 errno = saved_errno; /* Ignore ENOSYS */
1899 } else if (new_unx_mode) {
1901 int ret = -1;
1903 /* Attributes need changing. File already existed. */
1906 int saved_errno = errno; /* We might get ENOSYS in the
1907 * next call.. */
1908 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
1910 if (ret == -1 && errno == ENOSYS) {
1911 errno = saved_errno; /* Ignore ENOSYS */
1912 } else {
1913 DEBUG(5, ("open_file_ntcreate: reset "
1914 "attributes of file %s to 0%o\n",
1915 fname, (unsigned int)new_unx_mode));
1916 ret = 0; /* Don't do the fchmod below. */
1920 if ((ret == -1) &&
1921 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
1922 DEBUG(5, ("open_file_ntcreate: failed to reset "
1923 "attributes of file %s to 0%o\n",
1924 fname, (unsigned int)new_unx_mode));
1927 /* If this is a successful open, we must remove any deferred open
1928 * records. */
1929 if (req != NULL) {
1930 del_deferred_open_entry(lck, req->mid);
1932 TALLOC_FREE(lck);
1934 conn->num_files_open++;
1936 *result = fsp;
1937 return NT_STATUS_OK;
1940 /****************************************************************************
1941 Open a file for for write to ensure that we can fchmod it.
1942 ****************************************************************************/
1944 NTSTATUS open_file_fchmod(connection_struct *conn, const char *fname,
1945 SMB_STRUCT_STAT *psbuf, files_struct **result)
1947 files_struct *fsp = NULL;
1948 NTSTATUS status;
1950 if (!VALID_STAT(*psbuf)) {
1951 return NT_STATUS_INVALID_PARAMETER;
1954 status = file_new(conn, &fsp);
1955 if(!NT_STATUS_IS_OK(status)) {
1956 return status;
1959 /* note! we must use a non-zero desired access or we don't get
1960 a real file descriptor. Oh what a twisted web we weave. */
1961 status = open_file(fsp, conn, NULL, NULL, NULL, fname, psbuf, O_WRONLY,
1962 0, FILE_WRITE_DATA, FILE_WRITE_DATA);
1965 * This is not a user visible file open.
1966 * Don't set a share mode and don't increment
1967 * the conn->num_files_open.
1970 if (!NT_STATUS_IS_OK(status)) {
1971 file_free(fsp);
1972 return status;
1975 *result = fsp;
1976 return NT_STATUS_OK;
1979 /****************************************************************************
1980 Close the fchmod file fd - ensure no locks are lost.
1981 ****************************************************************************/
1983 NTSTATUS close_file_fchmod(files_struct *fsp)
1985 NTSTATUS status = fd_close(fsp);
1986 file_free(fsp);
1987 return status;
1990 static NTSTATUS mkdir_internal(connection_struct *conn,
1991 const char *name,
1992 uint32 file_attributes,
1993 SMB_STRUCT_STAT *psbuf)
1995 mode_t mode;
1996 char *parent_dir;
1997 const char *dirname;
1998 NTSTATUS status;
1999 bool posix_open = false;
2001 if(!CAN_WRITE(conn)) {
2002 DEBUG(5,("mkdir_internal: failing create on read-only share "
2003 "%s\n", lp_servicename(SNUM(conn))));
2004 return NT_STATUS_ACCESS_DENIED;
2007 status = check_name(conn, name);
2008 if (!NT_STATUS_IS_OK(status)) {
2009 return status;
2012 if (!parent_dirname_talloc(talloc_tos(), name, &parent_dir,
2013 &dirname)) {
2014 return NT_STATUS_NO_MEMORY;
2017 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2018 posix_open = true;
2019 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2020 } else {
2021 mode = unix_mode(conn, aDIR, name, parent_dir);
2024 if (SMB_VFS_MKDIR(conn, name, mode) != 0) {
2025 return map_nt_error_from_unix(errno);
2028 /* Ensure we're checking for a symlink here.... */
2029 /* We don't want to get caught by a symlink racer. */
2031 if (SMB_VFS_LSTAT(conn, name, psbuf) == -1) {
2032 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2033 name, strerror(errno)));
2034 return map_nt_error_from_unix(errno);
2037 if (!S_ISDIR(psbuf->st_mode)) {
2038 DEBUG(0, ("Directory just '%s' created is not a directory\n",
2039 name));
2040 return NT_STATUS_ACCESS_DENIED;
2043 if (lp_store_dos_attributes(SNUM(conn))) {
2044 if (!posix_open) {
2045 file_set_dosmode(conn, name,
2046 file_attributes | aDIR, NULL,
2047 parent_dir,
2048 true);
2052 if (lp_inherit_perms(SNUM(conn))) {
2053 inherit_access_acl(conn, parent_dir, name, mode);
2056 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
2058 * Check if high bits should have been set,
2059 * then (if bits are missing): add them.
2060 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2061 * dir.
2063 if (mode & ~(S_IRWXU|S_IRWXG|S_IRWXO) && (mode & ~psbuf->st_mode)) {
2064 SMB_VFS_CHMOD(conn, name,
2065 psbuf->st_mode | (mode & ~psbuf->st_mode));
2069 /* Change the owner if required. */
2070 if (lp_inherit_owner(SNUM(conn))) {
2071 change_dir_owner_to_parent(conn, parent_dir, name, psbuf);
2074 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2075 name);
2077 return NT_STATUS_OK;
2080 /****************************************************************************
2081 Open a directory from an NT SMB call.
2082 ****************************************************************************/
2084 NTSTATUS open_directory(connection_struct *conn,
2085 struct smb_request *req,
2086 const char *fname,
2087 SMB_STRUCT_STAT *psbuf,
2088 uint32 access_mask,
2089 uint32 share_access,
2090 uint32 create_disposition,
2091 uint32 create_options,
2092 uint32 file_attributes,
2093 int *pinfo,
2094 files_struct **result)
2096 files_struct *fsp = NULL;
2097 bool dir_existed = VALID_STAT(*psbuf) ? True : False;
2098 struct share_mode_lock *lck = NULL;
2099 NTSTATUS status;
2100 struct timespec mtimespec;
2101 int info = 0;
2103 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2104 "share_access = 0x%x create_options = 0x%x, "
2105 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2106 fname,
2107 (unsigned int)access_mask,
2108 (unsigned int)share_access,
2109 (unsigned int)create_options,
2110 (unsigned int)create_disposition,
2111 (unsigned int)file_attributes));
2113 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) && is_ntfs_stream_name(fname)) {
2114 DEBUG(2, ("open_directory: %s is a stream name!\n", fname));
2115 return NT_STATUS_NOT_A_DIRECTORY;
2118 switch( create_disposition ) {
2119 case FILE_OPEN:
2121 info = FILE_WAS_OPENED;
2124 * We want to follow symlinks here.
2127 if (SMB_VFS_STAT(conn, fname, psbuf) != 0) {
2128 return map_nt_error_from_unix(errno);
2131 break;
2133 case FILE_CREATE:
2135 /* If directory exists error. If directory doesn't
2136 * exist create. */
2138 status = mkdir_internal(conn,
2139 fname,
2140 file_attributes,
2141 psbuf);
2143 if (!NT_STATUS_IS_OK(status)) {
2144 DEBUG(2, ("open_directory: unable to create "
2145 "%s. Error was %s\n", fname,
2146 nt_errstr(status)));
2147 return status;
2150 info = FILE_WAS_CREATED;
2151 break;
2153 case FILE_OPEN_IF:
2155 * If directory exists open. If directory doesn't
2156 * exist create.
2159 status = mkdir_internal(conn,
2160 fname,
2161 file_attributes,
2162 psbuf);
2164 if (NT_STATUS_IS_OK(status)) {
2165 info = FILE_WAS_CREATED;
2168 if (NT_STATUS_EQUAL(status,
2169 NT_STATUS_OBJECT_NAME_COLLISION)) {
2170 info = FILE_WAS_OPENED;
2171 status = NT_STATUS_OK;
2174 break;
2176 case FILE_SUPERSEDE:
2177 case FILE_OVERWRITE:
2178 case FILE_OVERWRITE_IF:
2179 default:
2180 DEBUG(5,("open_directory: invalid create_disposition "
2181 "0x%x for directory %s\n",
2182 (unsigned int)create_disposition, fname));
2183 return NT_STATUS_INVALID_PARAMETER;
2186 if(!S_ISDIR(psbuf->st_mode)) {
2187 DEBUG(5,("open_directory: %s is not a directory !\n",
2188 fname ));
2189 return NT_STATUS_NOT_A_DIRECTORY;
2192 status = file_new(conn, &fsp);
2193 if(!NT_STATUS_IS_OK(status)) {
2194 return status;
2198 * Setup the files_struct for it.
2201 fsp->mode = psbuf->st_mode;
2202 fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
2203 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
2204 fsp->file_pid = req ? req->smbpid : 0;
2205 fsp->can_lock = False;
2206 fsp->can_read = False;
2207 fsp->can_write = False;
2209 fsp->share_access = share_access;
2210 fsp->fh->private_options = create_options;
2211 fsp->access_mask = access_mask;
2213 fsp->print_file = False;
2214 fsp->modified = False;
2215 fsp->oplock_type = NO_OPLOCK;
2216 fsp->sent_oplock_break = NO_BREAK_SENT;
2217 fsp->is_directory = True;
2218 fsp->is_stat = False;
2219 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
2221 string_set(&fsp->fsp_name,fname);
2223 mtimespec = get_mtimespec(psbuf);
2225 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
2226 conn->connectpath,
2227 fname, &mtimespec);
2229 if (lck == NULL) {
2230 DEBUG(0, ("open_directory: Could not get share mode lock for %s\n", fname));
2231 file_free(fsp);
2232 return NT_STATUS_SHARING_VIOLATION;
2235 status = open_mode_check(conn, fname, lck,
2236 access_mask, share_access,
2237 create_options, &dir_existed);
2239 if (!NT_STATUS_IS_OK(status)) {
2240 TALLOC_FREE(lck);
2241 file_free(fsp);
2242 return status;
2245 set_share_mode(lck, fsp, current_user.ut.uid, 0, NO_OPLOCK, True);
2247 /* For directories the delete on close bit at open time seems
2248 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2249 if (create_options & FILE_DELETE_ON_CLOSE) {
2250 status = can_set_delete_on_close(fsp, True, 0);
2251 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
2252 TALLOC_FREE(lck);
2253 file_free(fsp);
2254 return status;
2257 if (NT_STATUS_IS_OK(status)) {
2258 /* Note that here we set the *inital* delete on close flag,
2259 not the regular one. The magic gets handled in close. */
2260 fsp->initial_delete_on_close = True;
2264 TALLOC_FREE(lck);
2266 if (pinfo) {
2267 *pinfo = info;
2270 conn->num_files_open++;
2272 *result = fsp;
2273 return NT_STATUS_OK;
2276 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req, const char *directory)
2278 NTSTATUS status;
2279 SMB_STRUCT_STAT sbuf;
2280 files_struct *fsp;
2282 SET_STAT_INVALID(sbuf);
2284 status = open_directory(conn, req, directory, &sbuf,
2285 FILE_READ_ATTRIBUTES, /* Just a stat open */
2286 FILE_SHARE_NONE, /* Ignored for stat opens */
2287 FILE_CREATE,
2289 FILE_ATTRIBUTE_DIRECTORY,
2290 NULL,
2291 &fsp);
2293 if (NT_STATUS_IS_OK(status)) {
2294 close_file(fsp, NORMAL_CLOSE);
2297 return status;
2300 /****************************************************************************
2301 Open a pseudo-file (no locking checks - a 'stat' open).
2302 ****************************************************************************/
2304 NTSTATUS open_file_stat(connection_struct *conn, struct smb_request *req,
2305 const char *fname, SMB_STRUCT_STAT *psbuf,
2306 files_struct **result)
2308 files_struct *fsp = NULL;
2309 NTSTATUS status;
2311 if (!VALID_STAT(*psbuf)) {
2312 return NT_STATUS_INVALID_PARAMETER;
2315 /* Can't 'stat' open directories. */
2316 if(S_ISDIR(psbuf->st_mode)) {
2317 return NT_STATUS_FILE_IS_A_DIRECTORY;
2320 status = file_new(conn, &fsp);
2321 if(!NT_STATUS_IS_OK(status)) {
2322 return status;
2325 DEBUG(5,("open_file_stat: 'opening' file %s\n", fname));
2328 * Setup the files_struct for it.
2331 fsp->mode = psbuf->st_mode;
2332 fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
2333 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
2334 fsp->file_pid = req ? req->smbpid : 0;
2335 fsp->can_lock = False;
2336 fsp->can_read = False;
2337 fsp->can_write = False;
2338 fsp->print_file = False;
2339 fsp->modified = False;
2340 fsp->oplock_type = NO_OPLOCK;
2341 fsp->sent_oplock_break = NO_BREAK_SENT;
2342 fsp->is_directory = False;
2343 fsp->is_stat = True;
2344 string_set(&fsp->fsp_name,fname);
2346 conn->num_files_open++;
2348 *result = fsp;
2349 return NT_STATUS_OK;
2352 /****************************************************************************
2353 Receive notification that one of our open files has been renamed by another
2354 smbd process.
2355 ****************************************************************************/
2357 void msg_file_was_renamed(struct messaging_context *msg,
2358 void *private_data,
2359 uint32_t msg_type,
2360 struct server_id server_id,
2361 DATA_BLOB *data)
2363 files_struct *fsp;
2364 char *frm = (char *)data->data;
2365 struct file_id id;
2366 const char *sharepath;
2367 const char *newname;
2368 size_t sp_len;
2370 if (data->data == NULL
2371 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
2372 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
2373 (int)data->length));
2374 return;
2377 /* Unpack the message. */
2378 pull_file_id_16(frm, &id);
2379 sharepath = &frm[16];
2380 newname = sharepath + strlen(sharepath) + 1;
2381 sp_len = strlen(sharepath);
2383 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
2384 "file_id %s\n",
2385 sharepath, newname, file_id_string_tos(&id)));
2387 for(fsp = file_find_di_first(id); fsp; fsp = file_find_di_next(fsp)) {
2388 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
2389 DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n",
2390 fsp->fnum, fsp->fsp_name, newname ));
2391 string_set(&fsp->fsp_name, newname);
2392 } else {
2393 /* TODO. JRA. */
2394 /* Now we have the complete path we can work out if this is
2395 actually within this share and adjust newname accordingly. */
2396 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
2397 "not sharepath %s) "
2398 "fnum %d from %s -> %s\n",
2399 fsp->conn->connectpath,
2400 sharepath,
2401 fsp->fnum,
2402 fsp->fsp_name,
2403 newname ));
2408 struct case_semantics_state {
2409 connection_struct *conn;
2410 bool case_sensitive;
2411 bool case_preserve;
2412 bool short_case_preserve;
2415 /****************************************************************************
2416 Restore case semantics.
2417 ****************************************************************************/
2418 static int restore_case_semantics(struct case_semantics_state *state)
2420 state->conn->case_sensitive = state->case_sensitive;
2421 state->conn->case_preserve = state->case_preserve;
2422 state->conn->short_case_preserve = state->short_case_preserve;
2423 return 0;
2426 /****************************************************************************
2427 Save case semantics.
2428 ****************************************************************************/
2429 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
2430 connection_struct *conn)
2432 struct case_semantics_state *result;
2434 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
2435 DEBUG(0, ("talloc failed\n"));
2436 return NULL;
2439 result->conn = conn;
2440 result->case_sensitive = conn->case_sensitive;
2441 result->case_preserve = conn->case_preserve;
2442 result->short_case_preserve = conn->short_case_preserve;
2444 /* Set to POSIX. */
2445 conn->case_sensitive = True;
2446 conn->case_preserve = True;
2447 conn->short_case_preserve = True;
2449 talloc_set_destructor(result, restore_case_semantics);
2451 return result;
2455 * If a main file is opened for delete, all streams need to be checked for
2456 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
2457 * If that works, delete them all by setting the delete on close and close.
2460 static NTSTATUS open_streams_for_delete(connection_struct *conn,
2461 const char *fname)
2463 struct stream_struct *stream_info;
2464 files_struct **streams;
2465 int i;
2466 unsigned int num_streams;
2467 TALLOC_CTX *frame = talloc_stackframe();
2468 NTSTATUS status;
2470 status = SMB_VFS_STREAMINFO(conn, NULL, fname, talloc_tos(),
2471 &num_streams, &stream_info);
2473 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
2474 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
2475 DEBUG(10, ("no streams around\n"));
2476 TALLOC_FREE(frame);
2477 return NT_STATUS_OK;
2480 if (!NT_STATUS_IS_OK(status)) {
2481 DEBUG(10, ("SMB_VFS_STREAMINFO failed: %s\n",
2482 nt_errstr(status)));
2483 goto fail;
2486 DEBUG(10, ("open_streams_for_delete found %d streams\n",
2487 num_streams));
2489 if (num_streams == 0) {
2490 TALLOC_FREE(frame);
2491 return NT_STATUS_OK;
2494 streams = TALLOC_ARRAY(talloc_tos(), files_struct *, num_streams);
2495 if (streams == NULL) {
2496 DEBUG(0, ("talloc failed\n"));
2497 status = NT_STATUS_NO_MEMORY;
2498 goto fail;
2501 for (i=0; i<num_streams; i++) {
2502 char *streamname;
2504 if (strequal(stream_info[i].name, "::$DATA")) {
2505 streams[i] = NULL;
2506 continue;
2509 streamname = talloc_asprintf(talloc_tos(), "%s%s", fname,
2510 stream_info[i].name);
2512 if (streamname == NULL) {
2513 DEBUG(0, ("talloc_aprintf failed\n"));
2514 status = NT_STATUS_NO_MEMORY;
2515 goto fail;
2518 status = create_file_unixpath
2519 (conn, /* conn */
2520 NULL, /* req */
2521 streamname, /* fname */
2522 DELETE_ACCESS, /* access_mask */
2523 FILE_SHARE_READ | FILE_SHARE_WRITE
2524 | FILE_SHARE_DELETE, /* share_access */
2525 FILE_OPEN, /* create_disposition*/
2526 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* create_options */
2527 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
2528 0, /* oplock_request */
2529 0, /* allocation_size */
2530 NULL, /* sd */
2531 NULL, /* ea_list */
2532 &streams[i], /* result */
2533 NULL, /* pinfo */
2534 NULL); /* psbuf */
2536 TALLOC_FREE(streamname);
2538 if (!NT_STATUS_IS_OK(status)) {
2539 DEBUG(10, ("Could not open stream %s: %s\n",
2540 streamname, nt_errstr(status)));
2541 break;
2546 * don't touch the variable "status" beyond this point :-)
2549 for (i -= 1 ; i >= 0; i--) {
2550 if (streams[i] == NULL) {
2551 continue;
2554 DEBUG(10, ("Closing stream # %d, %s\n", i,
2555 streams[i]->fsp_name));
2556 close_file(streams[i], NORMAL_CLOSE);
2559 fail:
2560 TALLOC_FREE(frame);
2561 return status;
2565 * Wrapper around open_file_ntcreate and open_directory
2568 NTSTATUS create_file_unixpath(connection_struct *conn,
2569 struct smb_request *req,
2570 const char *fname,
2571 uint32_t access_mask,
2572 uint32_t share_access,
2573 uint32_t create_disposition,
2574 uint32_t create_options,
2575 uint32_t file_attributes,
2576 uint32_t oplock_request,
2577 SMB_BIG_UINT allocation_size,
2578 struct security_descriptor *sd,
2579 struct ea_list *ea_list,
2581 files_struct **result,
2582 int *pinfo,
2583 SMB_STRUCT_STAT *psbuf)
2585 SMB_STRUCT_STAT sbuf;
2586 int info = FILE_WAS_OPENED;
2587 files_struct *base_fsp = NULL;
2588 files_struct *fsp = NULL;
2589 NTSTATUS status;
2591 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
2592 "file_attributes = 0x%x, share_access = 0x%x, "
2593 "create_disposition = 0x%x create_options = 0x%x "
2594 "oplock_request = 0x%x ea_list = 0x%p, sd = 0x%p, "
2595 "fname = %s\n",
2596 (unsigned int)access_mask,
2597 (unsigned int)file_attributes,
2598 (unsigned int)share_access,
2599 (unsigned int)create_disposition,
2600 (unsigned int)create_options,
2601 (unsigned int)oplock_request,
2602 ea_list, sd, fname));
2604 if (create_options & FILE_OPEN_BY_FILE_ID) {
2605 status = NT_STATUS_NOT_SUPPORTED;
2606 goto fail;
2609 if (req == NULL) {
2610 oplock_request |= INTERNAL_OPEN_ONLY;
2613 if (psbuf != NULL) {
2614 sbuf = *psbuf;
2616 else {
2617 if (SMB_VFS_STAT(conn, fname, &sbuf) == -1) {
2618 SET_STAT_INVALID(sbuf);
2622 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
2623 && (access_mask & DELETE_ACCESS)
2624 && !is_ntfs_stream_name(fname)) {
2626 * We can't open a file with DELETE access if any of the
2627 * streams is open without FILE_SHARE_DELETE
2629 status = open_streams_for_delete(conn, fname);
2631 if (!NT_STATUS_IS_OK(status)) {
2632 goto fail;
2636 /* This is the correct thing to do (check every time) but can_delete
2637 * is expensive (it may have to read the parent directory
2638 * permissions). So for now we're not doing it unless we have a strong
2639 * hint the client is really going to delete this file. If the client
2640 * is forcing FILE_CREATE let the filesystem take care of the
2641 * permissions. */
2643 /* Setting FILE_SHARE_DELETE is the hint. */
2645 if (lp_acl_check_permissions(SNUM(conn))
2646 && (create_disposition != FILE_CREATE)
2647 && (share_access & FILE_SHARE_DELETE)
2648 && (access_mask & DELETE_ACCESS)
2649 && (((dos_mode(conn, fname, &sbuf) & FILE_ATTRIBUTE_READONLY)
2650 && !lp_delete_readonly(SNUM(conn)))
2651 || !can_delete_file_in_directory(conn, fname))) {
2652 status = NT_STATUS_ACCESS_DENIED;
2653 goto fail;
2656 #if 0
2657 /* We need to support SeSecurityPrivilege for this. */
2658 if ((access_mask & SEC_RIGHT_SYSTEM_SECURITY) &&
2659 !user_has_privileges(current_user.nt_user_token,
2660 &se_security)) {
2661 status = NT_STATUS_PRIVILEGE_NOT_HELD;
2662 goto fail;
2664 #endif
2666 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
2667 && is_ntfs_stream_name(fname)
2668 && (!(create_options & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
2669 char *base;
2670 uint32 base_create_disposition;
2672 if (create_options & FILE_DIRECTORY_FILE) {
2673 status = NT_STATUS_NOT_A_DIRECTORY;
2674 goto fail;
2677 status = split_ntfs_stream_name(talloc_tos(), fname,
2678 &base, NULL);
2679 if (!NT_STATUS_IS_OK(status)) {
2680 DEBUG(10, ("create_file_unixpath: "
2681 "split_ntfs_stream_name failed: %s\n",
2682 nt_errstr(status)));
2683 goto fail;
2686 SMB_ASSERT(!is_ntfs_stream_name(base)); /* paranoia.. */
2688 switch (create_disposition) {
2689 case FILE_OPEN:
2690 base_create_disposition = FILE_OPEN;
2691 break;
2692 default:
2693 base_create_disposition = FILE_OPEN_IF;
2694 break;
2697 status = create_file_unixpath(conn, NULL, base, 0,
2698 FILE_SHARE_READ
2699 | FILE_SHARE_WRITE
2700 | FILE_SHARE_DELETE,
2701 base_create_disposition,
2702 0, 0, 0, 0, NULL, NULL,
2703 &base_fsp, NULL, NULL);
2704 if (!NT_STATUS_IS_OK(status)) {
2705 DEBUG(10, ("create_file_unixpath for base %s failed: "
2706 "%s\n", base, nt_errstr(status)));
2707 goto fail;
2712 * If it's a request for a directory open, deal with it separately.
2715 if (create_options & FILE_DIRECTORY_FILE) {
2717 if (create_options & FILE_NON_DIRECTORY_FILE) {
2718 status = NT_STATUS_INVALID_PARAMETER;
2719 goto fail;
2722 /* Can't open a temp directory. IFS kit test. */
2723 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
2724 status = NT_STATUS_INVALID_PARAMETER;
2725 goto fail;
2729 * We will get a create directory here if the Win32
2730 * app specified a security descriptor in the
2731 * CreateDirectory() call.
2734 oplock_request = 0;
2735 status = open_directory(
2736 conn, req, fname, &sbuf, access_mask, share_access,
2737 create_disposition, create_options, file_attributes,
2738 &info, &fsp);
2739 } else {
2742 * Ordinary file case.
2745 status = open_file_ntcreate(
2746 conn, req, fname, &sbuf, access_mask, share_access,
2747 create_disposition, create_options, file_attributes,
2748 oplock_request, &info, &fsp);
2750 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
2753 * Fail the open if it was explicitly a non-directory
2754 * file.
2757 if (create_options & FILE_NON_DIRECTORY_FILE) {
2758 status = NT_STATUS_FILE_IS_A_DIRECTORY;
2759 goto fail;
2762 oplock_request = 0;
2763 status = open_directory(
2764 conn, req, fname, &sbuf, access_mask,
2765 share_access, create_disposition,
2766 create_options, file_attributes,
2767 &info, &fsp);
2771 if (!NT_STATUS_IS_OK(status)) {
2772 goto fail;
2776 * According to the MS documentation, the only time the security
2777 * descriptor is applied to the opened file is iff we *created* the
2778 * file; an existing file stays the same.
2780 * Also, it seems (from observation) that you can open the file with
2781 * any access mask but you can still write the sd. We need to override
2782 * the granted access before we call set_sd
2783 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
2786 if ((sd != NULL) && (info == FILE_WAS_CREATED)
2787 && lp_nt_acl_support(SNUM(conn))) {
2789 uint32_t sec_info_sent = ALL_SECURITY_INFORMATION;
2790 uint32_t saved_access_mask = fsp->access_mask;
2792 if (sd->owner_sid == NULL) {
2793 sec_info_sent &= ~OWNER_SECURITY_INFORMATION;
2795 if (sd->group_sid == NULL) {
2796 sec_info_sent &= ~GROUP_SECURITY_INFORMATION;
2798 if (sd->sacl == NULL) {
2799 sec_info_sent &= ~SACL_SECURITY_INFORMATION;
2801 if (sd->dacl == NULL) {
2802 sec_info_sent &= ~DACL_SECURITY_INFORMATION;
2805 fsp->access_mask = FILE_GENERIC_ALL;
2807 status = SMB_VFS_FSET_NT_ACL(fsp, sec_info_sent, sd);
2809 fsp->access_mask = saved_access_mask;
2811 if (!NT_STATUS_IS_OK(status)) {
2812 goto fail;
2816 if ((ea_list != NULL) && (info == FILE_WAS_CREATED)) {
2817 status = set_ea(conn, fsp, fname, ea_list);
2818 if (!NT_STATUS_IS_OK(status)) {
2819 goto fail;
2823 if (!fsp->is_directory && S_ISDIR(sbuf.st_mode)) {
2824 status = NT_STATUS_ACCESS_DENIED;
2825 goto fail;
2828 /* Save the requested allocation size. */
2829 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
2830 if (allocation_size
2831 && (allocation_size > sbuf.st_size)) {
2832 fsp->initial_allocation_size = smb_roundup(
2833 fsp->conn, allocation_size);
2834 if (fsp->is_directory) {
2835 /* Can't set allocation size on a directory. */
2836 status = NT_STATUS_ACCESS_DENIED;
2837 goto fail;
2839 if (vfs_allocate_file_space(
2840 fsp, fsp->initial_allocation_size) == -1) {
2841 status = NT_STATUS_DISK_FULL;
2842 goto fail;
2844 } else {
2845 fsp->initial_allocation_size = smb_roundup(
2846 fsp->conn, (SMB_BIG_UINT)sbuf.st_size);
2850 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
2853 * Set fsp->base_fsp late enough that we can't "goto fail" anymore. In
2854 * the fail: branch we call close_file(fsp, ERROR_CLOSE) which would
2855 * also close fsp->base_fsp which we have to also do explicitly in
2856 * this routine here, as not in all "goto fail:" we have the fsp set
2857 * up already to be initialized with the base_fsp.
2860 fsp->base_fsp = base_fsp;
2862 *result = fsp;
2863 if (pinfo != NULL) {
2864 *pinfo = info;
2866 if (psbuf != NULL) {
2867 if ((fsp->fh == NULL) || (fsp->fh->fd == -1)) {
2868 *psbuf = sbuf;
2870 else {
2871 SMB_VFS_FSTAT(fsp, psbuf);
2874 return NT_STATUS_OK;
2876 fail:
2877 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
2879 if (fsp != NULL) {
2880 close_file(fsp, ERROR_CLOSE);
2881 fsp = NULL;
2883 if (base_fsp != NULL) {
2884 close_file(base_fsp, ERROR_CLOSE);
2885 base_fsp = NULL;
2887 return status;
2890 NTSTATUS create_file(connection_struct *conn,
2891 struct smb_request *req,
2892 uint16_t root_dir_fid,
2893 const char *fname,
2894 uint32_t access_mask,
2895 uint32_t share_access,
2896 uint32_t create_disposition,
2897 uint32_t create_options,
2898 uint32_t file_attributes,
2899 uint32_t oplock_request,
2900 SMB_BIG_UINT allocation_size,
2901 struct security_descriptor *sd,
2902 struct ea_list *ea_list,
2904 files_struct **result,
2905 int *pinfo,
2906 SMB_STRUCT_STAT *psbuf)
2908 struct case_semantics_state *case_state = NULL;
2909 SMB_STRUCT_STAT sbuf;
2910 int info = FILE_WAS_OPENED;
2911 files_struct *fsp = NULL;
2912 NTSTATUS status;
2914 DEBUG(10,("create_file: access_mask = 0x%x "
2915 "file_attributes = 0x%x, share_access = 0x%x, "
2916 "create_disposition = 0x%x create_options = 0x%x "
2917 "oplock_request = 0x%x "
2918 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
2919 "fname = %s\n",
2920 (unsigned int)access_mask,
2921 (unsigned int)file_attributes,
2922 (unsigned int)share_access,
2923 (unsigned int)create_disposition,
2924 (unsigned int)create_options,
2925 (unsigned int)oplock_request,
2926 (unsigned int)root_dir_fid,
2927 ea_list, sd, fname));
2930 * Get the file name.
2933 if (root_dir_fid != 0) {
2935 * This filename is relative to a directory fid.
2937 char *parent_fname = NULL;
2938 files_struct *dir_fsp = file_fsp(root_dir_fid);
2940 if (dir_fsp == NULL) {
2941 status = NT_STATUS_INVALID_HANDLE;
2942 goto fail;
2945 if (!dir_fsp->is_directory) {
2948 * Check to see if this is a mac fork of some kind.
2951 if (is_ntfs_stream_name(fname)) {
2952 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
2953 goto fail;
2957 we need to handle the case when we get a
2958 relative open relative to a file and the
2959 pathname is blank - this is a reopen!
2960 (hint from demyn plantenberg)
2963 status = NT_STATUS_INVALID_HANDLE;
2964 goto fail;
2967 if (ISDOT(dir_fsp->fsp_name)) {
2969 * We're at the toplevel dir, the final file name
2970 * must not contain ./, as this is filtered out
2971 * normally by srvstr_get_path and unix_convert
2972 * explicitly rejects paths containing ./.
2974 parent_fname = talloc_strdup(talloc_tos(), "");
2975 if (parent_fname == NULL) {
2976 status = NT_STATUS_NO_MEMORY;
2977 goto fail;
2979 } else {
2980 size_t dir_name_len = strlen(dir_fsp->fsp_name);
2983 * Copy in the base directory name.
2986 parent_fname = TALLOC_ARRAY(talloc_tos(), char,
2987 dir_name_len+2);
2988 if (parent_fname == NULL) {
2989 status = NT_STATUS_NO_MEMORY;
2990 goto fail;
2992 memcpy(parent_fname, dir_fsp->fsp_name,
2993 dir_name_len+1);
2996 * Ensure it ends in a '/'.
2997 * We used TALLOC_SIZE +2 to add space for the '/'.
3000 if(dir_name_len
3001 && (parent_fname[dir_name_len-1] != '\\')
3002 && (parent_fname[dir_name_len-1] != '/')) {
3003 parent_fname[dir_name_len] = '/';
3004 parent_fname[dir_name_len+1] = '\0';
3008 fname = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
3009 fname);
3010 if (fname == NULL) {
3011 status = NT_STATUS_NO_MEMORY;
3012 goto fail;
3017 * Check to see if this is a mac fork of some kind.
3020 if (is_ntfs_stream_name(fname)) {
3021 enum FAKE_FILE_TYPE fake_file_type;
3023 fake_file_type = is_fake_file(fname);
3025 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
3028 * Here we go! support for changing the disk quotas
3029 * --metze
3031 * We need to fake up to open this MAGIC QUOTA file
3032 * and return a valid FID.
3034 * w2k close this file directly after openening xp
3035 * also tries a QUERY_FILE_INFO on the file and then
3036 * close it
3038 status = open_fake_file(conn, fake_file_type, fname,
3039 access_mask, &fsp);
3040 if (!NT_STATUS_IS_OK(status)) {
3041 goto fail;
3044 ZERO_STRUCT(sbuf);
3045 goto done;
3049 if ((req != NULL) && (req->flags2 & FLAGS2_DFS_PATHNAMES)) {
3050 char *resolved_fname;
3052 status = resolve_dfspath(talloc_tos(), conn, true, fname,
3053 &resolved_fname);
3055 if (!NT_STATUS_IS_OK(status)) {
3057 * For PATH_NOT_COVERED we had
3058 * reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
3059 * ERRSRV, ERRbadpath);
3060 * Need to fix in callers
3062 goto fail;
3064 fname = resolved_fname;
3068 * Check if POSIX semantics are wanted.
3071 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
3072 case_state = set_posix_case_semantics(talloc_tos(), conn);
3073 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
3077 char *converted_fname;
3079 SET_STAT_INVALID(sbuf);
3081 status = unix_convert(talloc_tos(), conn, fname, False,
3082 &converted_fname, NULL, &sbuf);
3083 if (!NT_STATUS_IS_OK(status)) {
3084 goto fail;
3086 fname = converted_fname;
3089 TALLOC_FREE(case_state);
3091 /* All file access must go through check_name() */
3093 status = check_name(conn, fname);
3094 if (!NT_STATUS_IS_OK(status)) {
3095 goto fail;
3098 status = create_file_unixpath(
3099 conn, req, fname, access_mask, share_access,
3100 create_disposition, create_options, file_attributes,
3101 oplock_request, allocation_size, sd, ea_list,
3102 &fsp, &info, &sbuf);
3104 if (!NT_STATUS_IS_OK(status)) {
3105 goto fail;
3108 done:
3109 DEBUG(10, ("create_file: info=%d\n", info));
3111 *result = fsp;
3112 if (pinfo != NULL) {
3113 *pinfo = info;
3115 if (psbuf != NULL) {
3116 *psbuf = sbuf;
3118 return NT_STATUS_OK;
3120 fail:
3121 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
3123 if (fsp != NULL) {
3124 close_file(fsp, ERROR_CLOSE);
3125 fsp = NULL;
3127 return status;