Optimization for change_file_owner_to_parent() and change_dir_owner_to_parent()
[Samba.git] / source3 / smbd / open.c
blobdb326c5a6da42c394a99b1a80b3a1b8a92c52ca1
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"
23 #include "system/filesys.h"
24 #include "printing.h"
25 #include "smbd/smbd.h"
26 #include "smbd/globals.h"
27 #include "fake_file.h"
28 #include "../libcli/security/security.h"
29 #include "../librpc/gen_ndr/ndr_security.h"
30 #include "auth.h"
31 #include "messages.h"
33 extern const struct generic_mapping file_generic_mapping;
35 struct deferred_open_record {
36 bool delayed_for_oplocks;
37 struct file_id id;
40 /****************************************************************************
41 SMB1 file varient of se_access_check. Never test FILE_READ_ATTRIBUTES.
42 ****************************************************************************/
44 NTSTATUS smb1_file_se_access_check(struct connection_struct *conn,
45 const struct security_descriptor *sd,
46 const struct security_token *token,
47 uint32_t access_desired,
48 uint32_t *access_granted)
50 *access_granted = 0;
52 if (get_current_uid(conn) == (uid_t)0) {
53 /* I'm sorry sir, I didn't know you were root... */
54 *access_granted = access_desired;
55 if (access_desired & SEC_FLAG_MAXIMUM_ALLOWED) {
56 *access_granted |= FILE_GENERIC_ALL;
58 return NT_STATUS_OK;
61 return se_access_check(sd,
62 token,
63 (access_desired & ~FILE_READ_ATTRIBUTES),
64 access_granted);
67 /****************************************************************************
68 Check if we have open rights.
69 ****************************************************************************/
71 NTSTATUS smbd_check_open_rights(struct connection_struct *conn,
72 const struct smb_filename *smb_fname,
73 uint32_t access_mask,
74 uint32_t *access_granted)
76 /* Check if we have rights to open. */
77 NTSTATUS status;
78 struct security_descriptor *sd = NULL;
80 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
81 (SECINFO_OWNER |
82 SECINFO_GROUP |
83 SECINFO_DACL),&sd);
85 if (!NT_STATUS_IS_OK(status)) {
86 DEBUG(10, ("smbd_check_open_rights: Could not get acl "
87 "on %s: %s\n",
88 smb_fname_str_dbg(smb_fname),
89 nt_errstr(status)));
90 return status;
93 status = smb1_file_se_access_check(conn,
94 sd,
95 get_current_nttok(conn),
96 access_mask,
97 access_granted);
99 DEBUG(10,("smbd_check_open_rights: file %s requesting "
100 "0x%x returning 0x%x (%s)\n",
101 smb_fname_str_dbg(smb_fname),
102 (unsigned int)access_mask,
103 (unsigned int)*access_granted,
104 nt_errstr(status) ));
106 if (!NT_STATUS_IS_OK(status)) {
107 if (DEBUGLEVEL >= 10) {
108 DEBUG(10,("smbd_check_open_rights: acl for %s is:\n",
109 smb_fname_str_dbg(smb_fname) ));
110 NDR_PRINT_DEBUG(security_descriptor, sd);
114 TALLOC_FREE(sd);
116 return status;
119 /****************************************************************************
120 fd support routines - attempt to do a dos_open.
121 ****************************************************************************/
123 static NTSTATUS fd_open(struct connection_struct *conn,
124 files_struct *fsp,
125 int flags,
126 mode_t mode)
128 struct smb_filename *smb_fname = fsp->fsp_name;
129 NTSTATUS status = NT_STATUS_OK;
131 #ifdef O_NOFOLLOW
133 * Never follow symlinks on a POSIX client. The
134 * client should be doing this.
137 if (fsp->posix_open || !lp_symlinks(SNUM(conn))) {
138 flags |= O_NOFOLLOW;
140 #endif
142 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, mode);
143 if (fsp->fh->fd == -1) {
144 status = map_nt_error_from_unix(errno);
145 if (errno == EMFILE) {
146 static time_t last_warned = 0L;
148 if (time((time_t *) NULL) > last_warned) {
149 DEBUG(0,("Too many open files, unable "
150 "to open more! smbd's max "
151 "open files = %d\n",
152 lp_max_open_files()));
153 last_warned = time((time_t *) NULL);
159 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
160 smb_fname_str_dbg(smb_fname), flags, (int)mode, fsp->fh->fd,
161 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
163 return status;
166 /****************************************************************************
167 Close the file associated with a fsp.
168 ****************************************************************************/
170 NTSTATUS fd_close(files_struct *fsp)
172 int ret;
174 if (fsp->dptr) {
175 dptr_CloseDir(fsp);
177 if (fsp->fh->fd == -1) {
178 return NT_STATUS_OK; /* What we used to call a stat open. */
180 if (fsp->fh->ref_count > 1) {
181 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
184 ret = SMB_VFS_CLOSE(fsp);
185 fsp->fh->fd = -1;
186 if (ret == -1) {
187 return map_nt_error_from_unix(errno);
189 return NT_STATUS_OK;
192 /****************************************************************************
193 Change the ownership of a file to that of the parent directory.
194 Do this by fd if possible.
195 ****************************************************************************/
197 void change_file_owner_to_parent(connection_struct *conn,
198 const char *inherit_from_dir,
199 files_struct *fsp)
201 struct smb_filename *smb_fname_parent = NULL;
202 NTSTATUS status;
203 int ret;
205 status = create_synthetic_smb_fname(talloc_tos(), inherit_from_dir,
206 NULL, NULL, &smb_fname_parent);
207 if (!NT_STATUS_IS_OK(status)) {
208 return;
211 ret = SMB_VFS_STAT(conn, smb_fname_parent);
212 if (ret == -1) {
213 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
214 "directory %s. Error was %s\n",
215 smb_fname_str_dbg(smb_fname_parent),
216 strerror(errno)));
217 TALLOC_FREE(smb_fname_parent);
218 return;
221 if (smb_fname_parent->st.st_ex_uid == fsp->fsp_name->st.st_ex_uid) {
222 /* Already this uid - no need to change. */
223 DEBUG(10,("change_file_owner_to_parent: file %s "
224 "is already owned by uid %d\n",
225 fsp_str_dbg(fsp),
226 (int)fsp->fsp_name->st.st_ex_uid ));
227 TALLOC_FREE(smb_fname_parent);
228 return;
231 become_root();
232 ret = SMB_VFS_FCHOWN(fsp, smb_fname_parent->st.st_ex_uid, (gid_t)-1);
233 unbecome_root();
234 if (ret == -1) {
235 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
236 "file %s to parent directory uid %u. Error "
237 "was %s\n", fsp_str_dbg(fsp),
238 (unsigned int)smb_fname_parent->st.st_ex_uid,
239 strerror(errno) ));
242 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
243 "parent directory uid %u.\n", fsp_str_dbg(fsp),
244 (unsigned int)smb_fname_parent->st.st_ex_uid));
246 TALLOC_FREE(smb_fname_parent);
249 NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
250 const char *inherit_from_dir,
251 const char *fname,
252 SMB_STRUCT_STAT *psbuf)
254 struct smb_filename *smb_fname_parent = NULL;
255 struct smb_filename *smb_fname_cwd = NULL;
256 char *saved_dir = NULL;
257 TALLOC_CTX *ctx = talloc_tos();
258 NTSTATUS status = NT_STATUS_OK;
259 int ret;
261 status = create_synthetic_smb_fname(ctx, inherit_from_dir, NULL, NULL,
262 &smb_fname_parent);
263 if (!NT_STATUS_IS_OK(status)) {
264 return status;
267 ret = SMB_VFS_STAT(conn, smb_fname_parent);
268 if (ret == -1) {
269 status = map_nt_error_from_unix(errno);
270 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
271 "directory %s. Error was %s\n",
272 smb_fname_str_dbg(smb_fname_parent),
273 strerror(errno)));
274 goto out;
277 /* We've already done an lstat into psbuf, and we know it's a
278 directory. If we can cd into the directory and the dev/ino
279 are the same then we can safely chown without races as
280 we're locking the directory in place by being in it. This
281 should work on any UNIX (thanks tridge :-). JRA.
284 saved_dir = vfs_GetWd(ctx,conn);
285 if (!saved_dir) {
286 status = map_nt_error_from_unix(errno);
287 DEBUG(0,("change_dir_owner_to_parent: failed to get "
288 "current working directory. Error was %s\n",
289 strerror(errno)));
290 goto out;
293 /* Chdir into the new path. */
294 if (vfs_ChDir(conn, fname) == -1) {
295 status = map_nt_error_from_unix(errno);
296 DEBUG(0,("change_dir_owner_to_parent: failed to change "
297 "current working directory to %s. Error "
298 "was %s\n", fname, strerror(errno) ));
299 goto chdir;
302 status = create_synthetic_smb_fname(ctx, ".", NULL, NULL,
303 &smb_fname_cwd);
304 if (!NT_STATUS_IS_OK(status)) {
305 return status;
308 ret = SMB_VFS_STAT(conn, smb_fname_cwd);
309 if (ret == -1) {
310 status = map_nt_error_from_unix(errno);
311 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
312 "directory '.' (%s) Error was %s\n",
313 fname, strerror(errno)));
314 goto chdir;
317 /* Ensure we're pointing at the same place. */
318 if (smb_fname_cwd->st.st_ex_dev != psbuf->st_ex_dev ||
319 smb_fname_cwd->st.st_ex_ino != psbuf->st_ex_ino ||
320 smb_fname_cwd->st.st_ex_mode != psbuf->st_ex_mode ) {
321 DEBUG(0,("change_dir_owner_to_parent: "
322 "device/inode/mode on directory %s changed. "
323 "Refusing to chown !\n", fname ));
324 status = NT_STATUS_ACCESS_DENIED;
325 goto chdir;
328 if (smb_fname_parent->st.st_ex_uid == smb_fname_cwd->st.st_ex_uid) {
329 /* Already this uid - no need to change. */
330 DEBUG(10,("change_dir_owner_to_parent: directory %s "
331 "is already owned by uid %d\n",
332 fname,
333 (int)smb_fname_cwd->st.st_ex_uid ));
334 status = NT_STATUS_OK;
335 goto chdir;
338 become_root();
339 ret = SMB_VFS_CHOWN(conn, ".", smb_fname_parent->st.st_ex_uid,
340 (gid_t)-1);
341 unbecome_root();
342 if (ret == -1) {
343 status = map_nt_error_from_unix(errno);
344 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
345 "directory %s to parent directory uid %u. "
346 "Error was %s\n", fname,
347 (unsigned int)smb_fname_parent->st.st_ex_uid,
348 strerror(errno) ));
349 goto chdir;
352 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
353 "directory %s to parent directory uid %u.\n",
354 fname, (unsigned int)smb_fname_parent->st.st_ex_uid ));
356 chdir:
357 vfs_ChDir(conn,saved_dir);
358 out:
359 TALLOC_FREE(smb_fname_parent);
360 TALLOC_FREE(smb_fname_cwd);
361 return status;
364 /****************************************************************************
365 Open a file.
366 ****************************************************************************/
368 static NTSTATUS open_file(files_struct *fsp,
369 connection_struct *conn,
370 struct smb_request *req,
371 const char *parent_dir,
372 int flags,
373 mode_t unx_mode,
374 uint32 access_mask, /* client requested access mask. */
375 uint32 open_access_mask) /* what we're actually using in the open. */
377 struct smb_filename *smb_fname = fsp->fsp_name;
378 NTSTATUS status = NT_STATUS_OK;
379 int accmode = (flags & O_ACCMODE);
380 int local_flags = flags;
381 bool file_existed = VALID_STAT(fsp->fsp_name->st);
383 fsp->fh->fd = -1;
384 errno = EPERM;
386 /* Check permissions */
389 * This code was changed after seeing a client open request
390 * containing the open mode of (DENY_WRITE/read-only) with
391 * the 'create if not exist' bit set. The previous code
392 * would fail to open the file read only on a read-only share
393 * as it was checking the flags parameter directly against O_RDONLY,
394 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
395 * JRA.
398 if (!CAN_WRITE(conn)) {
399 /* It's a read-only share - fail if we wanted to write. */
400 if(accmode != O_RDONLY || (flags & O_TRUNC) || (flags & O_APPEND)) {
401 DEBUG(3,("Permission denied opening %s\n",
402 smb_fname_str_dbg(smb_fname)));
403 return NT_STATUS_ACCESS_DENIED;
404 } else if(flags & O_CREAT) {
405 /* We don't want to write - but we must make sure that
406 O_CREAT doesn't create the file if we have write
407 access into the directory.
409 flags &= ~(O_CREAT|O_EXCL);
410 local_flags &= ~(O_CREAT|O_EXCL);
415 * This little piece of insanity is inspired by the
416 * fact that an NT client can open a file for O_RDONLY,
417 * but set the create disposition to FILE_EXISTS_TRUNCATE.
418 * If the client *can* write to the file, then it expects to
419 * truncate the file, even though it is opening for readonly.
420 * Quicken uses this stupid trick in backup file creation...
421 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
422 * for helping track this one down. It didn't bite us in 2.0.x
423 * as we always opened files read-write in that release. JRA.
426 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
427 DEBUG(10,("open_file: truncate requested on read-only open "
428 "for file %s\n", smb_fname_str_dbg(smb_fname)));
429 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
432 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
433 (!file_existed && (local_flags & O_CREAT)) ||
434 ((local_flags & O_TRUNC) == O_TRUNC) ) {
435 const char *wild;
438 * We can't actually truncate here as the file may be locked.
439 * open_file_ntcreate will take care of the truncate later. JRA.
442 local_flags &= ~O_TRUNC;
444 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
446 * We would block on opening a FIFO with no one else on the
447 * other end. Do what we used to do and add O_NONBLOCK to the
448 * open flags. JRA.
451 if (file_existed && S_ISFIFO(smb_fname->st.st_ex_mode)) {
452 local_flags |= O_NONBLOCK;
454 #endif
456 /* Don't create files with Microsoft wildcard characters. */
457 if (fsp->base_fsp) {
459 * wildcard characters are allowed in stream names
460 * only test the basefilename
462 wild = fsp->base_fsp->fsp_name->base_name;
463 } else {
464 wild = smb_fname->base_name;
466 if ((local_flags & O_CREAT) && !file_existed &&
467 ms_has_wild(wild)) {
468 return NT_STATUS_OBJECT_NAME_INVALID;
471 /* Actually do the open */
472 status = fd_open(conn, fsp, local_flags, unx_mode);
473 if (!NT_STATUS_IS_OK(status)) {
474 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
475 "(flags=%d)\n", smb_fname_str_dbg(smb_fname),
476 nt_errstr(status),local_flags,flags));
477 return status;
480 if ((local_flags & O_CREAT) && !file_existed) {
482 /* Inherit the ACL if required */
483 if (lp_inherit_perms(SNUM(conn))) {
484 inherit_access_posix_acl(conn, parent_dir,
485 smb_fname->base_name,
486 unx_mode);
489 /* Change the owner if required. */
490 if (lp_inherit_owner(SNUM(conn))) {
491 change_file_owner_to_parent(conn, parent_dir,
492 fsp);
495 notify_fname(conn, NOTIFY_ACTION_ADDED,
496 FILE_NOTIFY_CHANGE_FILE_NAME,
497 smb_fname->base_name);
500 } else {
501 fsp->fh->fd = -1; /* What we used to call a stat open. */
502 if (file_existed) {
503 uint32_t access_granted = 0;
505 status = smbd_check_open_rights(conn,
506 smb_fname,
507 access_mask,
508 &access_granted);
509 if (!NT_STATUS_IS_OK(status)) {
510 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
512 * On NT_STATUS_ACCESS_DENIED, access_granted
513 * contains the denied bits.
516 if ((access_mask & FILE_WRITE_ATTRIBUTES) &&
517 (access_granted & FILE_WRITE_ATTRIBUTES) &&
518 (lp_map_readonly(SNUM(conn)) ||
519 lp_map_archive(SNUM(conn)) ||
520 lp_map_hidden(SNUM(conn)) ||
521 lp_map_system(SNUM(conn)))) {
522 access_granted &= ~FILE_WRITE_ATTRIBUTES;
524 DEBUG(10,("open_file: "
525 "overrode "
526 "FILE_WRITE_"
527 "ATTRIBUTES "
528 "on file %s\n",
529 smb_fname_str_dbg(
530 smb_fname)));
533 if ((access_mask & DELETE_ACCESS) &&
534 (access_granted & DELETE_ACCESS) &&
535 can_delete_file_in_directory(conn,
536 smb_fname)) {
537 /* Were we trying to do a stat open
538 * for delete and didn't get DELETE
539 * access (only) ? Check if the
540 * directory allows DELETE_CHILD.
541 * See here:
542 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
543 * for details. */
545 access_granted &= ~DELETE_ACCESS;
547 DEBUG(10,("open_file: "
548 "overrode "
549 "DELETE_ACCESS on "
550 "file %s\n",
551 smb_fname_str_dbg(
552 smb_fname)));
555 if (access_granted != 0) {
556 DEBUG(10,("open_file: Access "
557 "denied on file "
558 "%s\n",
559 smb_fname_str_dbg(
560 smb_fname)));
561 return status;
563 } else if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
564 fsp->posix_open &&
565 S_ISLNK(smb_fname->st.st_ex_mode)) {
566 /* This is a POSIX stat open for delete
567 * or rename on a symlink that points
568 * nowhere. Allow. */
569 DEBUG(10,("open_file: allowing POSIX "
570 "open on bad symlink %s\n",
571 smb_fname_str_dbg(
572 smb_fname)));
573 } else {
574 DEBUG(10,("open_file: "
575 "smbd_check_open_rights on file "
576 "%s returned %s\n",
577 smb_fname_str_dbg(smb_fname),
578 nt_errstr(status) ));
579 return status;
585 if (!file_existed) {
586 int ret;
588 if (fsp->fh->fd == -1) {
589 ret = SMB_VFS_STAT(conn, smb_fname);
590 } else {
591 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
592 /* If we have an fd, this stat should succeed. */
593 if (ret == -1) {
594 DEBUG(0,("Error doing fstat on open file %s "
595 "(%s)\n",
596 smb_fname_str_dbg(smb_fname),
597 strerror(errno) ));
601 /* For a non-io open, this stat failing means file not found. JRA */
602 if (ret == -1) {
603 status = map_nt_error_from_unix(errno);
604 fd_close(fsp);
605 return status;
610 * POSIX allows read-only opens of directories. We don't
611 * want to do this (we use a different code path for this)
612 * so catch a directory open and return an EISDIR. JRA.
615 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
616 fd_close(fsp);
617 errno = EISDIR;
618 return NT_STATUS_FILE_IS_A_DIRECTORY;
621 fsp->mode = smb_fname->st.st_ex_mode;
622 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
623 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
624 fsp->file_pid = req ? req->smbpid : 0;
625 fsp->can_lock = True;
626 fsp->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
627 if (!CAN_WRITE(conn)) {
628 fsp->can_write = False;
629 } else {
630 fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ?
631 True : False;
633 fsp->print_file = NULL;
634 fsp->modified = False;
635 fsp->sent_oplock_break = NO_BREAK_SENT;
636 fsp->is_directory = False;
637 if (conn->aio_write_behind_list &&
638 is_in_path(smb_fname->base_name, conn->aio_write_behind_list,
639 conn->case_sensitive)) {
640 fsp->aio_write_behind = True;
643 fsp->wcp = NULL; /* Write cache pointer. */
645 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
646 conn->session_info->unix_name,
647 smb_fname_str_dbg(smb_fname),
648 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
649 conn->num_files_open));
651 errno = 0;
652 return NT_STATUS_OK;
655 /*******************************************************************
656 Return True if the filename is one of the special executable types.
657 ********************************************************************/
659 bool is_executable(const char *fname)
661 if ((fname = strrchr_m(fname,'.'))) {
662 if (strequal(fname,".com") ||
663 strequal(fname,".dll") ||
664 strequal(fname,".exe") ||
665 strequal(fname,".sym")) {
666 return True;
669 return False;
672 /****************************************************************************
673 Check if we can open a file with a share mode.
674 Returns True if conflict, False if not.
675 ****************************************************************************/
677 static bool share_conflict(struct share_mode_entry *entry,
678 uint32 access_mask,
679 uint32 share_access)
681 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
682 "entry->share_access = 0x%x, "
683 "entry->private_options = 0x%x\n",
684 (unsigned int)entry->access_mask,
685 (unsigned int)entry->share_access,
686 (unsigned int)entry->private_options));
688 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
689 (unsigned int)access_mask, (unsigned int)share_access));
691 if ((entry->access_mask & (FILE_WRITE_DATA|
692 FILE_APPEND_DATA|
693 FILE_READ_DATA|
694 FILE_EXECUTE|
695 DELETE_ACCESS)) == 0) {
696 DEBUG(10,("share_conflict: No conflict due to "
697 "entry->access_mask = 0x%x\n",
698 (unsigned int)entry->access_mask ));
699 return False;
702 if ((access_mask & (FILE_WRITE_DATA|
703 FILE_APPEND_DATA|
704 FILE_READ_DATA|
705 FILE_EXECUTE|
706 DELETE_ACCESS)) == 0) {
707 DEBUG(10,("share_conflict: No conflict due to "
708 "access_mask = 0x%x\n",
709 (unsigned int)access_mask ));
710 return False;
713 #if 1 /* JRA TEST - Superdebug. */
714 #define CHECK_MASK(num, am, right, sa, share) \
715 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
716 (unsigned int)(num), (unsigned int)(am), \
717 (unsigned int)(right), (unsigned int)(am)&(right) )); \
718 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
719 (unsigned int)(num), (unsigned int)(sa), \
720 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
721 if (((am) & (right)) && !((sa) & (share))) { \
722 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
723 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
724 (unsigned int)(share) )); \
725 return True; \
727 #else
728 #define CHECK_MASK(num, am, right, sa, share) \
729 if (((am) & (right)) && !((sa) & (share))) { \
730 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
731 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
732 (unsigned int)(share) )); \
733 return True; \
735 #endif
737 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
738 share_access, FILE_SHARE_WRITE);
739 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
740 entry->share_access, FILE_SHARE_WRITE);
742 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
743 share_access, FILE_SHARE_READ);
744 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
745 entry->share_access, FILE_SHARE_READ);
747 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
748 share_access, FILE_SHARE_DELETE);
749 CHECK_MASK(6, access_mask, DELETE_ACCESS,
750 entry->share_access, FILE_SHARE_DELETE);
752 DEBUG(10,("share_conflict: No conflict.\n"));
753 return False;
756 #if defined(DEVELOPER)
757 static void validate_my_share_entries(struct smbd_server_connection *sconn,
758 int num,
759 struct share_mode_entry *share_entry)
761 files_struct *fsp;
763 if (!procid_is_me(&share_entry->pid)) {
764 return;
767 if (is_deferred_open_entry(share_entry) &&
768 !open_was_deferred(share_entry->op_mid)) {
769 char *str = talloc_asprintf(talloc_tos(),
770 "Got a deferred entry without a request: "
771 "PANIC: %s\n",
772 share_mode_str(talloc_tos(), num, share_entry));
773 smb_panic(str);
776 if (!is_valid_share_mode_entry(share_entry)) {
777 return;
780 fsp = file_find_dif(sconn, share_entry->id,
781 share_entry->share_file_id);
782 if (!fsp) {
783 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
784 share_mode_str(talloc_tos(), num, share_entry) ));
785 smb_panic("validate_my_share_entries: Cannot match a "
786 "share entry with an open file\n");
789 if (is_deferred_open_entry(share_entry) ||
790 is_unused_share_mode_entry(share_entry)) {
791 goto panic;
794 if ((share_entry->op_type == NO_OPLOCK) &&
795 (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK)) {
796 /* Someone has already written to it, but I haven't yet
797 * noticed */
798 return;
801 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
802 goto panic;
805 return;
807 panic:
809 char *str;
810 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
811 share_mode_str(talloc_tos(), num, share_entry) ));
812 str = talloc_asprintf(talloc_tos(),
813 "validate_my_share_entries: "
814 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
815 fsp->fsp_name->base_name,
816 (unsigned int)fsp->oplock_type,
817 (unsigned int)share_entry->op_type );
818 smb_panic(str);
821 #endif
823 bool is_stat_open(uint32 access_mask)
825 return (access_mask &&
826 ((access_mask & ~(SYNCHRONIZE_ACCESS| FILE_READ_ATTRIBUTES|
827 FILE_WRITE_ATTRIBUTES))==0) &&
828 ((access_mask & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|
829 FILE_WRITE_ATTRIBUTES)) != 0));
832 /****************************************************************************
833 Deal with share modes
834 Invarient: Share mode must be locked on entry and exit.
835 Returns -1 on error, or number of share modes on success (may be zero).
836 ****************************************************************************/
838 static NTSTATUS open_mode_check(connection_struct *conn,
839 struct share_mode_lock *lck,
840 uint32_t name_hash,
841 uint32 access_mask,
842 uint32 share_access,
843 uint32 create_options,
844 bool *file_existed)
846 int i;
848 if(lck->num_share_modes == 0) {
849 return NT_STATUS_OK;
852 *file_existed = True;
854 /* A delete on close prohibits everything */
856 if (is_delete_on_close_set(lck, name_hash)) {
857 return NT_STATUS_DELETE_PENDING;
860 if (is_stat_open(access_mask)) {
861 /* Stat open that doesn't trigger oplock breaks or share mode
862 * checks... ! JRA. */
863 return NT_STATUS_OK;
867 * Check if the share modes will give us access.
870 #if defined(DEVELOPER)
871 for(i = 0; i < lck->num_share_modes; i++) {
872 validate_my_share_entries(conn->sconn, i,
873 &lck->share_modes[i]);
875 #endif
877 if (!lp_share_modes(SNUM(conn))) {
878 return NT_STATUS_OK;
881 /* Now we check the share modes, after any oplock breaks. */
882 for(i = 0; i < lck->num_share_modes; i++) {
884 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
885 continue;
888 /* someone else has a share lock on it, check to see if we can
889 * too */
890 if (share_conflict(&lck->share_modes[i],
891 access_mask, share_access)) {
892 return NT_STATUS_SHARING_VIOLATION;
896 return NT_STATUS_OK;
899 static bool is_delete_request(files_struct *fsp) {
900 return ((fsp->access_mask == DELETE_ACCESS) &&
901 (fsp->oplock_type == NO_OPLOCK));
905 * Send a break message to the oplock holder and delay the open for
906 * our client.
909 static NTSTATUS send_break_message(files_struct *fsp,
910 struct share_mode_entry *exclusive,
911 uint64_t mid,
912 int oplock_request)
914 NTSTATUS status;
915 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
917 DEBUG(10, ("Sending break request to PID %s\n",
918 procid_str_static(&exclusive->pid)));
919 exclusive->op_mid = mid;
921 /* Create the message. */
922 share_mode_entry_to_message(msg, exclusive);
924 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
925 don't want this set in the share mode struct pointed to by lck. */
927 if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
928 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,
929 exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
932 status = messaging_send_buf(fsp->conn->sconn->msg_ctx, exclusive->pid,
933 MSG_SMB_BREAK_REQUEST,
934 (uint8 *)msg,
935 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
936 if (!NT_STATUS_IS_OK(status)) {
937 DEBUG(3, ("Could not send oplock break message: %s\n",
938 nt_errstr(status)));
941 return status;
945 * Return share_mode_entry pointers for :
946 * 1). Batch oplock entry.
947 * 2). Batch or exclusive oplock entry (may be identical to #1).
948 * bool have_level2_oplock
949 * bool have_no_oplock.
950 * Do internal consistency checks on the share mode for a file.
953 static void find_oplock_types(struct share_mode_lock *lck,
954 struct share_mode_entry **pp_batch,
955 struct share_mode_entry **pp_ex_or_batch,
956 bool *got_level2,
957 bool *got_no_oplock)
959 int i;
961 *pp_batch = NULL;
962 *pp_ex_or_batch = NULL;
963 *got_level2 = false;
964 *got_no_oplock = false;
966 for (i=0; i<lck->num_share_modes; i++) {
967 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
968 continue;
971 if (BATCH_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
972 /* batch - can only be one. */
973 if (*pp_ex_or_batch || *pp_batch || *got_level2 || *got_no_oplock) {
974 smb_panic("Bad batch oplock entry.");
976 *pp_batch = &lck->share_modes[i];
979 if (EXCLUSIVE_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
980 /* Exclusive or batch - can only be one. */
981 if (*pp_ex_or_batch || *got_level2 || *got_no_oplock) {
982 smb_panic("Bad exclusive or batch oplock entry.");
984 *pp_ex_or_batch = &lck->share_modes[i];
987 if (LEVEL_II_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
988 if (*pp_batch || *pp_ex_or_batch) {
989 smb_panic("Bad levelII oplock entry.");
991 *got_level2 = true;
994 if (lck->share_modes[i].op_type == NO_OPLOCK) {
995 if (*pp_batch || *pp_ex_or_batch) {
996 smb_panic("Bad no oplock entry.");
998 *got_no_oplock = true;
1003 static bool delay_for_batch_oplocks(files_struct *fsp,
1004 uint64_t mid,
1005 int oplock_request,
1006 struct share_mode_entry *batch_entry)
1008 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1009 return false;
1012 if (batch_entry != NULL) {
1013 /* Found a batch oplock */
1014 send_break_message(fsp, batch_entry, mid, oplock_request);
1015 return true;
1017 return false;
1020 static bool delay_for_exclusive_oplocks(files_struct *fsp,
1021 uint64_t mid,
1022 int oplock_request,
1023 struct share_mode_entry *ex_entry)
1025 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1026 return false;
1029 if (ex_entry != NULL) {
1030 /* Found an exclusive or batch oplock */
1031 bool delay_it = is_delete_request(fsp) ?
1032 BATCH_OPLOCK_TYPE(ex_entry->op_type) : true;
1033 if (delay_it) {
1034 send_break_message(fsp, ex_entry, mid, oplock_request);
1035 return true;
1038 return false;
1041 static bool file_has_brlocks(files_struct *fsp)
1043 struct byte_range_lock *br_lck;
1045 br_lck = brl_get_locks_readonly(fsp);
1046 if (!br_lck)
1047 return false;
1049 return br_lck->num_locks > 0 ? true : false;
1052 static void grant_fsp_oplock_type(files_struct *fsp,
1053 int oplock_request,
1054 bool got_level2_oplock,
1055 bool got_a_none_oplock)
1057 bool allow_level2 = (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
1058 lp_level2_oplocks(SNUM(fsp->conn));
1060 /* Start by granting what the client asked for,
1061 but ensure no SAMBA_PRIVATE bits can be set. */
1062 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1064 if (oplock_request & INTERNAL_OPEN_ONLY) {
1065 /* No oplocks on internal open. */
1066 fsp->oplock_type = NO_OPLOCK;
1067 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1068 fsp->oplock_type, fsp_str_dbg(fsp)));
1069 return;
1070 } else if (lp_locking(fsp->conn->params) && file_has_brlocks(fsp)) {
1071 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1072 fsp_str_dbg(fsp)));
1073 fsp->oplock_type = NO_OPLOCK;
1076 if (is_stat_open(fsp->access_mask)) {
1077 /* Leave the value already set. */
1078 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1079 fsp->oplock_type, fsp_str_dbg(fsp)));
1080 return;
1084 * Match what was requested (fsp->oplock_type) with
1085 * what was found in the existing share modes.
1088 if (got_a_none_oplock) {
1089 fsp->oplock_type = NO_OPLOCK;
1090 } else if (got_level2_oplock) {
1091 if (fsp->oplock_type == NO_OPLOCK ||
1092 fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
1093 /* Store a level2 oplock, but don't tell the client */
1094 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1095 } else {
1096 fsp->oplock_type = LEVEL_II_OPLOCK;
1098 } else {
1099 /* All share_mode_entries are placeholders or deferred.
1100 * Silently upgrade to fake levelII if the client didn't
1101 * ask for an oplock. */
1102 if (fsp->oplock_type == NO_OPLOCK) {
1103 /* Store a level2 oplock, but don't tell the client */
1104 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1109 * Don't grant level2 to clients that don't want them
1110 * or if we've turned them off.
1112 if (fsp->oplock_type == LEVEL_II_OPLOCK && !allow_level2) {
1113 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1116 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1117 fsp->oplock_type, fsp_str_dbg(fsp)));
1120 bool request_timed_out(struct timeval request_time,
1121 struct timeval timeout)
1123 struct timeval now, end_time;
1124 GetTimeOfDay(&now);
1125 end_time = timeval_sum(&request_time, &timeout);
1126 return (timeval_compare(&end_time, &now) < 0);
1129 /****************************************************************************
1130 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1131 ****************************************************************************/
1133 static void defer_open(struct share_mode_lock *lck,
1134 struct timeval request_time,
1135 struct timeval timeout,
1136 struct smb_request *req,
1137 struct deferred_open_record *state)
1139 int i;
1141 /* Paranoia check */
1143 for (i=0; i<lck->num_share_modes; i++) {
1144 struct share_mode_entry *e = &lck->share_modes[i];
1146 if (!is_deferred_open_entry(e)) {
1147 continue;
1150 if (procid_is_me(&e->pid) && (e->op_mid == req->mid)) {
1151 DEBUG(0, ("Trying to defer an already deferred "
1152 "request: mid=%llu, exiting\n",
1153 (unsigned long long)req->mid));
1154 exit_server("attempt to defer a deferred request");
1158 /* End paranoia check */
1160 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1161 "open entry for mid %llu\n",
1162 (unsigned int)request_time.tv_sec,
1163 (unsigned int)request_time.tv_usec,
1164 (unsigned long long)req->mid));
1166 if (!push_deferred_open_message_smb(req, request_time, timeout,
1167 state->id, (char *)state, sizeof(*state))) {
1168 exit_server("push_deferred_open_message_smb failed");
1170 add_deferred_open(lck, req->mid, request_time,
1171 sconn_server_id(req->sconn), state->id);
1175 /****************************************************************************
1176 On overwrite open ensure that the attributes match.
1177 ****************************************************************************/
1179 bool open_match_attributes(connection_struct *conn,
1180 uint32 old_dos_attr,
1181 uint32 new_dos_attr,
1182 mode_t existing_unx_mode,
1183 mode_t new_unx_mode,
1184 mode_t *returned_unx_mode)
1186 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
1188 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1189 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1191 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
1192 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
1193 *returned_unx_mode = new_unx_mode;
1194 } else {
1195 *returned_unx_mode = (mode_t)0;
1198 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1199 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1200 "returned_unx_mode = 0%o\n",
1201 (unsigned int)old_dos_attr,
1202 (unsigned int)existing_unx_mode,
1203 (unsigned int)new_dos_attr,
1204 (unsigned int)*returned_unx_mode ));
1206 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1207 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1208 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
1209 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
1210 return False;
1213 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1214 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
1215 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
1216 return False;
1219 return True;
1222 /****************************************************************************
1223 Special FCB or DOS processing in the case of a sharing violation.
1224 Try and find a duplicated file handle.
1225 ****************************************************************************/
1227 NTSTATUS fcb_or_dos_open(struct smb_request *req,
1228 connection_struct *conn,
1229 files_struct *fsp_to_dup_into,
1230 const struct smb_filename *smb_fname,
1231 struct file_id id,
1232 uint16 file_pid,
1233 uint16 vuid,
1234 uint32 access_mask,
1235 uint32 share_access,
1236 uint32 create_options)
1238 files_struct *fsp;
1240 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1241 "file %s.\n", smb_fname_str_dbg(smb_fname)));
1243 for(fsp = file_find_di_first(conn->sconn, id); fsp;
1244 fsp = file_find_di_next(fsp)) {
1246 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1247 "vuid = %u, file_pid = %u, private_options = 0x%x "
1248 "access_mask = 0x%x\n", fsp_str_dbg(fsp),
1249 fsp->fh->fd, (unsigned int)fsp->vuid,
1250 (unsigned int)fsp->file_pid,
1251 (unsigned int)fsp->fh->private_options,
1252 (unsigned int)fsp->access_mask ));
1254 if (fsp->fh->fd != -1 &&
1255 fsp->vuid == vuid &&
1256 fsp->file_pid == file_pid &&
1257 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
1258 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
1259 (fsp->access_mask & FILE_WRITE_DATA) &&
1260 strequal(fsp->fsp_name->base_name, smb_fname->base_name) &&
1261 strequal(fsp->fsp_name->stream_name,
1262 smb_fname->stream_name)) {
1263 DEBUG(10,("fcb_or_dos_open: file match\n"));
1264 break;
1268 if (!fsp) {
1269 return NT_STATUS_NOT_FOUND;
1272 /* quite an insane set of semantics ... */
1273 if (is_executable(smb_fname->base_name) &&
1274 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1275 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1276 return NT_STATUS_INVALID_PARAMETER;
1279 /* We need to duplicate this fsp. */
1280 return dup_file_fsp(req, fsp, access_mask, share_access,
1281 create_options, fsp_to_dup_into);
1284 /****************************************************************************
1285 Open a file with a share mode - old openX method - map into NTCreate.
1286 ****************************************************************************/
1288 bool map_open_params_to_ntcreate(const struct smb_filename *smb_fname,
1289 int deny_mode, int open_func,
1290 uint32 *paccess_mask,
1291 uint32 *pshare_mode,
1292 uint32 *pcreate_disposition,
1293 uint32 *pcreate_options,
1294 uint32_t *pprivate_flags)
1296 uint32 access_mask;
1297 uint32 share_mode;
1298 uint32 create_disposition;
1299 uint32 create_options = FILE_NON_DIRECTORY_FILE;
1300 uint32_t private_flags = 0;
1302 DEBUG(10,("map_open_params_to_ntcreate: fname = %s, deny_mode = 0x%x, "
1303 "open_func = 0x%x\n",
1304 smb_fname_str_dbg(smb_fname), (unsigned int)deny_mode,
1305 (unsigned int)open_func ));
1307 /* Create the NT compatible access_mask. */
1308 switch (GET_OPENX_MODE(deny_mode)) {
1309 case DOS_OPEN_EXEC: /* Implies read-only - used to be FILE_READ_DATA */
1310 case DOS_OPEN_RDONLY:
1311 access_mask = FILE_GENERIC_READ;
1312 break;
1313 case DOS_OPEN_WRONLY:
1314 access_mask = FILE_GENERIC_WRITE;
1315 break;
1316 case DOS_OPEN_RDWR:
1317 case DOS_OPEN_FCB:
1318 access_mask = FILE_GENERIC_READ|FILE_GENERIC_WRITE;
1319 break;
1320 default:
1321 DEBUG(10,("map_open_params_to_ntcreate: bad open mode = 0x%x\n",
1322 (unsigned int)GET_OPENX_MODE(deny_mode)));
1323 return False;
1326 /* Create the NT compatible create_disposition. */
1327 switch (open_func) {
1328 case OPENX_FILE_EXISTS_FAIL|OPENX_FILE_CREATE_IF_NOT_EXIST:
1329 create_disposition = FILE_CREATE;
1330 break;
1332 case OPENX_FILE_EXISTS_OPEN:
1333 create_disposition = FILE_OPEN;
1334 break;
1336 case OPENX_FILE_EXISTS_OPEN|OPENX_FILE_CREATE_IF_NOT_EXIST:
1337 create_disposition = FILE_OPEN_IF;
1338 break;
1340 case OPENX_FILE_EXISTS_TRUNCATE:
1341 create_disposition = FILE_OVERWRITE;
1342 break;
1344 case OPENX_FILE_EXISTS_TRUNCATE|OPENX_FILE_CREATE_IF_NOT_EXIST:
1345 create_disposition = FILE_OVERWRITE_IF;
1346 break;
1348 default:
1349 /* From samba4 - to be confirmed. */
1350 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_EXEC) {
1351 create_disposition = FILE_CREATE;
1352 break;
1354 DEBUG(10,("map_open_params_to_ntcreate: bad "
1355 "open_func 0x%x\n", (unsigned int)open_func));
1356 return False;
1359 /* Create the NT compatible share modes. */
1360 switch (GET_DENY_MODE(deny_mode)) {
1361 case DENY_ALL:
1362 share_mode = FILE_SHARE_NONE;
1363 break;
1365 case DENY_WRITE:
1366 share_mode = FILE_SHARE_READ;
1367 break;
1369 case DENY_READ:
1370 share_mode = FILE_SHARE_WRITE;
1371 break;
1373 case DENY_NONE:
1374 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1375 break;
1377 case DENY_DOS:
1378 private_flags |= NTCREATEX_OPTIONS_PRIVATE_DENY_DOS;
1379 if (is_executable(smb_fname->base_name)) {
1380 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1381 } else {
1382 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_RDONLY) {
1383 share_mode = FILE_SHARE_READ;
1384 } else {
1385 share_mode = FILE_SHARE_NONE;
1388 break;
1390 case DENY_FCB:
1391 private_flags |= NTCREATEX_OPTIONS_PRIVATE_DENY_FCB;
1392 share_mode = FILE_SHARE_NONE;
1393 break;
1395 default:
1396 DEBUG(10,("map_open_params_to_ntcreate: bad deny_mode 0x%x\n",
1397 (unsigned int)GET_DENY_MODE(deny_mode) ));
1398 return False;
1401 DEBUG(10,("map_open_params_to_ntcreate: file %s, access_mask = 0x%x, "
1402 "share_mode = 0x%x, create_disposition = 0x%x, "
1403 "create_options = 0x%x private_flags = 0x%x\n",
1404 smb_fname_str_dbg(smb_fname),
1405 (unsigned int)access_mask,
1406 (unsigned int)share_mode,
1407 (unsigned int)create_disposition,
1408 (unsigned int)create_options,
1409 (unsigned int)private_flags));
1411 if (paccess_mask) {
1412 *paccess_mask = access_mask;
1414 if (pshare_mode) {
1415 *pshare_mode = share_mode;
1417 if (pcreate_disposition) {
1418 *pcreate_disposition = create_disposition;
1420 if (pcreate_options) {
1421 *pcreate_options = create_options;
1423 if (pprivate_flags) {
1424 *pprivate_flags = private_flags;
1427 return True;
1431 static void schedule_defer_open(struct share_mode_lock *lck,
1432 struct timeval request_time,
1433 struct smb_request *req)
1435 struct deferred_open_record state;
1437 /* This is a relative time, added to the absolute
1438 request_time value to get the absolute timeout time.
1439 Note that if this is the second or greater time we enter
1440 this codepath for this particular request mid then
1441 request_time is left as the absolute time of the *first*
1442 time this request mid was processed. This is what allows
1443 the request to eventually time out. */
1445 struct timeval timeout;
1447 /* Normally the smbd we asked should respond within
1448 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1449 * the client did, give twice the timeout as a safety
1450 * measure here in case the other smbd is stuck
1451 * somewhere else. */
1453 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1455 /* Nothing actually uses state.delayed_for_oplocks
1456 but it's handy to differentiate in debug messages
1457 between a 30 second delay due to oplock break, and
1458 a 1 second delay for share mode conflicts. */
1460 state.delayed_for_oplocks = True;
1461 state.id = lck->id;
1463 if (!request_timed_out(request_time, timeout)) {
1464 defer_open(lck, request_time, timeout, req, &state);
1468 /****************************************************************************
1469 Work out what access_mask to use from what the client sent us.
1470 ****************************************************************************/
1472 static NTSTATUS calculate_access_mask(connection_struct *conn,
1473 const struct smb_filename *smb_fname,
1474 bool file_existed,
1475 uint32_t access_mask,
1476 uint32_t *access_mask_out)
1478 NTSTATUS status;
1481 * Convert GENERIC bits to specific bits.
1484 se_map_generic(&access_mask, &file_generic_mapping);
1486 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1487 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1488 if (file_existed) {
1490 struct security_descriptor *sd;
1491 uint32_t access_granted = 0;
1493 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
1494 (SECINFO_OWNER |
1495 SECINFO_GROUP |
1496 SECINFO_DACL),&sd);
1498 if (!NT_STATUS_IS_OK(status)) {
1499 DEBUG(10, ("calculate_access_mask: Could not get acl "
1500 "on file %s: %s\n",
1501 smb_fname_str_dbg(smb_fname),
1502 nt_errstr(status)));
1503 return NT_STATUS_ACCESS_DENIED;
1506 status = smb1_file_se_access_check(conn,
1508 get_current_nttok(conn),
1509 access_mask,
1510 &access_granted);
1512 TALLOC_FREE(sd);
1514 if (!NT_STATUS_IS_OK(status)) {
1515 DEBUG(10, ("calculate_access_mask: Access denied on "
1516 "file %s: when calculating maximum access\n",
1517 smb_fname_str_dbg(smb_fname)));
1518 return NT_STATUS_ACCESS_DENIED;
1521 access_mask = access_granted;
1522 } else {
1523 access_mask = FILE_GENERIC_ALL;
1527 *access_mask_out = access_mask;
1528 return NT_STATUS_OK;
1531 /****************************************************************************
1532 Remove the deferred open entry under lock.
1533 ****************************************************************************/
1535 void remove_deferred_open_entry(struct file_id id, uint64_t mid,
1536 struct server_id pid)
1538 struct share_mode_lock *lck = get_share_mode_lock(talloc_tos(), id,
1539 NULL, NULL, NULL);
1540 if (lck == NULL) {
1541 DEBUG(0, ("could not get share mode lock\n"));
1542 } else {
1543 del_deferred_open_entry(lck, mid, pid);
1544 TALLOC_FREE(lck);
1548 /****************************************************************************
1549 Open a file with a share mode. Passed in an already created files_struct *.
1550 ****************************************************************************/
1552 static NTSTATUS open_file_ntcreate(connection_struct *conn,
1553 struct smb_request *req,
1554 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1555 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1556 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1557 uint32 create_options, /* options such as delete on close. */
1558 uint32 new_dos_attributes, /* attributes used for new file. */
1559 int oplock_request, /* internal Samba oplock codes. */
1560 /* Information (FILE_EXISTS etc.) */
1561 uint32_t private_flags, /* Samba specific flags. */
1562 int *pinfo,
1563 files_struct *fsp)
1565 struct smb_filename *smb_fname = fsp->fsp_name;
1566 int flags=0;
1567 int flags2=0;
1568 bool file_existed = VALID_STAT(smb_fname->st);
1569 bool def_acl = False;
1570 bool posix_open = False;
1571 bool new_file_created = False;
1572 bool clear_ads = false;
1573 struct file_id id;
1574 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1575 mode_t new_unx_mode = (mode_t)0;
1576 mode_t unx_mode = (mode_t)0;
1577 int info;
1578 uint32 existing_dos_attributes = 0;
1579 struct timeval request_time = timeval_zero();
1580 struct share_mode_lock *lck = NULL;
1581 uint32 open_access_mask = access_mask;
1582 NTSTATUS status;
1583 char *parent_dir;
1585 ZERO_STRUCT(id);
1587 /* Windows allows a new file to be created and
1588 silently removes a FILE_ATTRIBUTE_DIRECTORY
1589 sent by the client. Do the same. */
1591 new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
1593 if (conn->printer) {
1595 * Printers are handled completely differently.
1596 * Most of the passed parameters are ignored.
1599 if (pinfo) {
1600 *pinfo = FILE_WAS_CREATED;
1603 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1604 smb_fname_str_dbg(smb_fname)));
1606 if (!req) {
1607 DEBUG(0,("open_file_ntcreate: printer open without "
1608 "an SMB request!\n"));
1609 return NT_STATUS_INTERNAL_ERROR;
1612 return print_spool_open(fsp, smb_fname->base_name,
1613 req->vuid);
1616 if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
1617 NULL)) {
1618 return NT_STATUS_NO_MEMORY;
1621 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1622 posix_open = True;
1623 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1624 new_dos_attributes = 0;
1625 } else {
1626 /* We add aARCH to this as this mode is only used if the file is
1627 * created new. */
1628 unx_mode = unix_mode(conn, new_dos_attributes | aARCH,
1629 smb_fname, parent_dir);
1632 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1633 "access_mask=0x%x share_access=0x%x "
1634 "create_disposition = 0x%x create_options=0x%x "
1635 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
1636 smb_fname_str_dbg(smb_fname), new_dos_attributes,
1637 access_mask, share_access, create_disposition,
1638 create_options, (unsigned int)unx_mode, oplock_request,
1639 (unsigned int)private_flags));
1641 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
1642 DEBUG(0, ("No smb request but not an internal only open!\n"));
1643 return NT_STATUS_INTERNAL_ERROR;
1647 * Only non-internal opens can be deferred at all
1650 if (req) {
1651 void *ptr;
1652 if (get_deferred_open_message_state(req,
1653 &request_time,
1654 &ptr)) {
1656 struct deferred_open_record *state = (struct deferred_open_record *)ptr;
1657 /* Remember the absolute time of the original
1658 request with this mid. We'll use it later to
1659 see if this has timed out. */
1661 /* Remove the deferred open entry under lock. */
1662 remove_deferred_open_entry(
1663 state->id, req->mid,
1664 sconn_server_id(req->sconn));
1666 /* Ensure we don't reprocess this message. */
1667 remove_deferred_open_message_smb(req->mid);
1671 status = check_name(conn, smb_fname->base_name);
1672 if (!NT_STATUS_IS_OK(status)) {
1673 return status;
1676 if (!posix_open) {
1677 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
1678 if (file_existed) {
1679 existing_dos_attributes = dos_mode(conn, smb_fname);
1683 /* ignore any oplock requests if oplocks are disabled */
1684 if (!lp_oplocks(SNUM(conn)) ||
1685 IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
1686 /* Mask off everything except the private Samba bits. */
1687 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
1690 /* this is for OS/2 long file names - say we don't support them */
1691 if (!lp_posix_pathnames() && strstr(smb_fname->base_name,".+,;=[].")) {
1692 /* OS/2 Workplace shell fix may be main code stream in a later
1693 * release. */
1694 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1695 "supported.\n"));
1696 if (use_nt_status()) {
1697 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1699 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
1702 switch( create_disposition ) {
1704 * Currently we're using FILE_SUPERSEDE as the same as
1705 * FILE_OVERWRITE_IF but they really are
1706 * different. FILE_SUPERSEDE deletes an existing file
1707 * (requiring delete access) then recreates it.
1709 case FILE_SUPERSEDE:
1710 /* If file exists replace/overwrite. If file doesn't
1711 * exist create. */
1712 flags2 |= (O_CREAT | O_TRUNC);
1713 clear_ads = true;
1714 break;
1716 case FILE_OVERWRITE_IF:
1717 /* If file exists replace/overwrite. If file doesn't
1718 * exist create. */
1719 flags2 |= (O_CREAT | O_TRUNC);
1720 clear_ads = true;
1721 break;
1723 case FILE_OPEN:
1724 /* If file exists open. If file doesn't exist error. */
1725 if (!file_existed) {
1726 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1727 "requested for file %s and file "
1728 "doesn't exist.\n",
1729 smb_fname_str_dbg(smb_fname)));
1730 errno = ENOENT;
1731 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1733 break;
1735 case FILE_OVERWRITE:
1736 /* If file exists overwrite. If file doesn't exist
1737 * error. */
1738 if (!file_existed) {
1739 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1740 "requested for file %s and file "
1741 "doesn't exist.\n",
1742 smb_fname_str_dbg(smb_fname) ));
1743 errno = ENOENT;
1744 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1746 flags2 |= O_TRUNC;
1747 clear_ads = true;
1748 break;
1750 case FILE_CREATE:
1751 /* If file exists error. If file doesn't exist
1752 * create. */
1753 if (file_existed) {
1754 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1755 "requested for file %s and file "
1756 "already exists.\n",
1757 smb_fname_str_dbg(smb_fname)));
1758 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
1759 errno = EISDIR;
1760 } else {
1761 errno = EEXIST;
1763 return map_nt_error_from_unix(errno);
1765 flags2 |= (O_CREAT|O_EXCL);
1766 break;
1768 case FILE_OPEN_IF:
1769 /* If file exists open. If file doesn't exist
1770 * create. */
1771 flags2 |= O_CREAT;
1772 break;
1774 default:
1775 return NT_STATUS_INVALID_PARAMETER;
1778 /* We only care about matching attributes on file exists and
1779 * overwrite. */
1781 if (!posix_open && file_existed && ((create_disposition == FILE_OVERWRITE) ||
1782 (create_disposition == FILE_OVERWRITE_IF))) {
1783 if (!open_match_attributes(conn, existing_dos_attributes,
1784 new_dos_attributes,
1785 smb_fname->st.st_ex_mode,
1786 unx_mode, &new_unx_mode)) {
1787 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1788 "for file %s (%x %x) (0%o, 0%o)\n",
1789 smb_fname_str_dbg(smb_fname),
1790 existing_dos_attributes,
1791 new_dos_attributes,
1792 (unsigned int)smb_fname->st.st_ex_mode,
1793 (unsigned int)unx_mode ));
1794 errno = EACCES;
1795 return NT_STATUS_ACCESS_DENIED;
1799 status = calculate_access_mask(conn, smb_fname, file_existed,
1800 access_mask,
1801 &access_mask);
1802 if (!NT_STATUS_IS_OK(status)) {
1803 DEBUG(10, ("open_file_ntcreate: calculate_access_mask "
1804 "on file %s returned %s\n",
1805 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
1806 return status;
1809 open_access_mask = access_mask;
1811 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1812 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
1815 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1816 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
1817 access_mask));
1820 * Note that we ignore the append flag as append does not
1821 * mean the same thing under DOS and Unix.
1824 if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
1825 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1826 /* DENY_DOS opens are always underlying read-write on the
1827 file handle, no matter what the requested access mask
1828 says. */
1829 if ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
1830 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|FILE_READ_EA|FILE_EXECUTE)) {
1831 flags = O_RDWR;
1832 } else {
1833 flags = O_WRONLY;
1835 } else {
1836 flags = O_RDONLY;
1840 * Currently we only look at FILE_WRITE_THROUGH for create options.
1843 #if defined(O_SYNC)
1844 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
1845 flags2 |= O_SYNC;
1847 #endif /* O_SYNC */
1849 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
1850 flags2 |= O_APPEND;
1853 if (!posix_open && !CAN_WRITE(conn)) {
1855 * We should really return a permission denied error if either
1856 * O_CREAT or O_TRUNC are set, but for compatibility with
1857 * older versions of Samba we just AND them out.
1859 flags2 &= ~(O_CREAT|O_TRUNC);
1863 * Ensure we can't write on a read-only share or file.
1866 if (flags != O_RDONLY && file_existed &&
1867 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
1868 DEBUG(5,("open_file_ntcreate: write access requested for "
1869 "file %s on read only %s\n",
1870 smb_fname_str_dbg(smb_fname),
1871 !CAN_WRITE(conn) ? "share" : "file" ));
1872 errno = EACCES;
1873 return NT_STATUS_ACCESS_DENIED;
1876 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1877 fsp->share_access = share_access;
1878 fsp->fh->private_options = private_flags;
1879 fsp->access_mask = open_access_mask; /* We change this to the
1880 * requested access_mask after
1881 * the open is done. */
1882 fsp->posix_open = posix_open;
1884 /* Ensure no SAMBA_PRIVATE bits can be set. */
1885 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1887 if (timeval_is_zero(&request_time)) {
1888 request_time = fsp->open_time;
1891 if (file_existed) {
1892 struct share_mode_entry *batch_entry = NULL;
1893 struct share_mode_entry *exclusive_entry = NULL;
1894 bool got_level2_oplock = false;
1895 bool got_a_none_oplock = false;
1897 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
1898 id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1900 lck = get_share_mode_lock(talloc_tos(), id,
1901 conn->connectpath,
1902 smb_fname, &old_write_time);
1904 if (lck == NULL) {
1905 DEBUG(0, ("Could not get share mode lock\n"));
1906 return NT_STATUS_SHARING_VIOLATION;
1909 /* Get the types we need to examine. */
1910 find_oplock_types(lck,
1911 &batch_entry,
1912 &exclusive_entry,
1913 &got_level2_oplock,
1914 &got_a_none_oplock);
1916 /* First pass - send break only on batch oplocks. */
1917 if ((req != NULL) &&
1918 delay_for_batch_oplocks(fsp,
1919 req->mid,
1920 oplock_request,
1921 batch_entry)) {
1922 schedule_defer_open(lck, request_time, req);
1923 TALLOC_FREE(lck);
1924 return NT_STATUS_SHARING_VIOLATION;
1927 /* Use the client requested access mask here, not the one we
1928 * open with. */
1929 status = open_mode_check(conn, lck, fsp->name_hash,
1930 access_mask, share_access,
1931 create_options, &file_existed);
1933 if (NT_STATUS_IS_OK(status)) {
1934 /* We might be going to allow this open. Check oplock
1935 * status again. */
1936 /* Second pass - send break for both batch or
1937 * exclusive oplocks. */
1938 if ((req != NULL) &&
1939 delay_for_exclusive_oplocks(
1940 fsp,
1941 req->mid,
1942 oplock_request,
1943 exclusive_entry)) {
1944 schedule_defer_open(lck, request_time, req);
1945 TALLOC_FREE(lck);
1946 return NT_STATUS_SHARING_VIOLATION;
1950 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
1951 /* DELETE_PENDING is not deferred for a second */
1952 TALLOC_FREE(lck);
1953 return status;
1956 grant_fsp_oplock_type(fsp,
1957 oplock_request,
1958 got_level2_oplock,
1959 got_a_none_oplock);
1961 if (!NT_STATUS_IS_OK(status)) {
1962 uint32 can_access_mask;
1963 bool can_access = True;
1965 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
1967 /* Check if this can be done with the deny_dos and fcb
1968 * calls. */
1969 if (private_flags &
1970 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
1971 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
1972 if (req == NULL) {
1973 DEBUG(0, ("DOS open without an SMB "
1974 "request!\n"));
1975 TALLOC_FREE(lck);
1976 return NT_STATUS_INTERNAL_ERROR;
1979 /* Use the client requested access mask here,
1980 * not the one we open with. */
1981 status = fcb_or_dos_open(req,
1982 conn,
1983 fsp,
1984 smb_fname,
1986 req->smbpid,
1987 req->vuid,
1988 access_mask,
1989 share_access,
1990 create_options);
1992 if (NT_STATUS_IS_OK(status)) {
1993 TALLOC_FREE(lck);
1994 if (pinfo) {
1995 *pinfo = FILE_WAS_OPENED;
1997 return NT_STATUS_OK;
2002 * This next line is a subtlety we need for
2003 * MS-Access. If a file open will fail due to share
2004 * permissions and also for security (access) reasons,
2005 * we need to return the access failed error, not the
2006 * share error. We can't open the file due to kernel
2007 * oplock deadlock (it's possible we failed above on
2008 * the open_mode_check()) so use a userspace check.
2011 if (flags & O_RDWR) {
2012 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
2013 } else if (flags & O_WRONLY) {
2014 can_access_mask = FILE_WRITE_DATA;
2015 } else {
2016 can_access_mask = FILE_READ_DATA;
2019 if (((can_access_mask & FILE_WRITE_DATA) &&
2020 !CAN_WRITE(conn)) ||
2021 !can_access_file_data(conn, smb_fname,
2022 can_access_mask)) {
2023 can_access = False;
2027 * If we're returning a share violation, ensure we
2028 * cope with the braindead 1 second delay.
2031 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
2032 lp_defer_sharing_violations()) {
2033 struct timeval timeout;
2034 struct deferred_open_record state;
2035 int timeout_usecs;
2037 /* this is a hack to speed up torture tests
2038 in 'make test' */
2039 timeout_usecs = lp_parm_int(SNUM(conn),
2040 "smbd","sharedelay",
2041 SHARING_VIOLATION_USEC_WAIT);
2043 /* This is a relative time, added to the absolute
2044 request_time value to get the absolute timeout time.
2045 Note that if this is the second or greater time we enter
2046 this codepath for this particular request mid then
2047 request_time is left as the absolute time of the *first*
2048 time this request mid was processed. This is what allows
2049 the request to eventually time out. */
2051 timeout = timeval_set(0, timeout_usecs);
2053 /* Nothing actually uses state.delayed_for_oplocks
2054 but it's handy to differentiate in debug messages
2055 between a 30 second delay due to oplock break, and
2056 a 1 second delay for share mode conflicts. */
2058 state.delayed_for_oplocks = False;
2059 state.id = id;
2061 if ((req != NULL)
2062 && !request_timed_out(request_time,
2063 timeout)) {
2064 defer_open(lck, request_time, timeout,
2065 req, &state);
2069 TALLOC_FREE(lck);
2070 if (can_access) {
2072 * We have detected a sharing violation here
2073 * so return the correct error code
2075 status = NT_STATUS_SHARING_VIOLATION;
2076 } else {
2077 status = NT_STATUS_ACCESS_DENIED;
2079 return status;
2083 * We exit this block with the share entry *locked*.....
2087 SMB_ASSERT(!file_existed || (lck != NULL));
2090 * Ensure we pay attention to default ACLs on directories if required.
2093 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
2094 (def_acl = directory_has_default_acl(conn, parent_dir))) {
2095 unx_mode = (0777 & lp_create_mask(SNUM(conn)));
2098 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2099 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2100 (unsigned int)flags, (unsigned int)flags2,
2101 (unsigned int)unx_mode, (unsigned int)access_mask,
2102 (unsigned int)open_access_mask));
2105 * open_file strips any O_TRUNC flags itself.
2108 fsp_open = open_file(fsp, conn, req, parent_dir,
2109 flags|flags2, unx_mode, access_mask,
2110 open_access_mask);
2112 if (!NT_STATUS_IS_OK(fsp_open)) {
2113 if (lck != NULL) {
2114 TALLOC_FREE(lck);
2116 return fsp_open;
2119 if (!file_existed) {
2120 struct share_mode_entry *batch_entry = NULL;
2121 struct share_mode_entry *exclusive_entry = NULL;
2122 bool got_level2_oplock = false;
2123 bool got_a_none_oplock = false;
2124 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
2126 * Deal with the race condition where two smbd's detect the
2127 * file doesn't exist and do the create at the same time. One
2128 * of them will win and set a share mode, the other (ie. this
2129 * one) should check if the requested share mode for this
2130 * create is allowed.
2134 * Now the file exists and fsp is successfully opened,
2135 * fsp->dev and fsp->inode are valid and should replace the
2136 * dev=0,inode=0 from a non existent file. Spotted by
2137 * Nadav Danieli <nadavd@exanet.com>. JRA.
2140 id = fsp->file_id;
2142 lck = get_share_mode_lock(talloc_tos(), id,
2143 conn->connectpath,
2144 smb_fname, &old_write_time);
2146 if (lck == NULL) {
2147 DEBUG(0, ("open_file_ntcreate: Could not get share "
2148 "mode lock for %s\n",
2149 smb_fname_str_dbg(smb_fname)));
2150 fd_close(fsp);
2151 return NT_STATUS_SHARING_VIOLATION;
2154 /* Get the types we need to examine. */
2155 find_oplock_types(lck,
2156 &batch_entry,
2157 &exclusive_entry,
2158 &got_level2_oplock,
2159 &got_a_none_oplock);
2161 /* First pass - send break only on batch oplocks. */
2162 if ((req != NULL) &&
2163 delay_for_batch_oplocks(fsp,
2164 req->mid,
2165 oplock_request,
2166 batch_entry)) {
2167 schedule_defer_open(lck, request_time, req);
2168 TALLOC_FREE(lck);
2169 fd_close(fsp);
2170 return NT_STATUS_SHARING_VIOLATION;
2173 status = open_mode_check(conn, lck, fsp->name_hash,
2174 access_mask, share_access,
2175 create_options, &file_existed);
2177 if (NT_STATUS_IS_OK(status)) {
2178 /* We might be going to allow this open. Check oplock
2179 * status again. */
2180 /* Second pass - send break for both batch or
2181 * exclusive oplocks. */
2182 if ((req != NULL) &&
2183 delay_for_exclusive_oplocks(
2184 fsp,
2185 req->mid,
2186 oplock_request,
2187 exclusive_entry)) {
2188 schedule_defer_open(lck, request_time, req);
2189 TALLOC_FREE(lck);
2190 fd_close(fsp);
2191 return NT_STATUS_SHARING_VIOLATION;
2195 if (!NT_STATUS_IS_OK(status)) {
2196 struct deferred_open_record state;
2198 fd_close(fsp);
2200 state.delayed_for_oplocks = False;
2201 state.id = id;
2203 /* Do it all over again immediately. In the second
2204 * round we will find that the file existed and handle
2205 * the DELETE_PENDING and FCB cases correctly. No need
2206 * to duplicate the code here. Essentially this is a
2207 * "goto top of this function", but don't tell
2208 * anybody... */
2210 if (req != NULL) {
2211 defer_open(lck, request_time, timeval_zero(),
2212 req, &state);
2214 TALLOC_FREE(lck);
2215 return status;
2218 grant_fsp_oplock_type(fsp,
2219 oplock_request,
2220 got_level2_oplock,
2221 got_a_none_oplock);
2224 * We exit this block with the share entry *locked*.....
2229 SMB_ASSERT(lck != NULL);
2231 /* Delete streams if create_disposition requires it */
2232 if (file_existed && clear_ads &&
2233 !is_ntfs_stream_smb_fname(smb_fname)) {
2234 status = delete_all_streams(conn, smb_fname->base_name);
2235 if (!NT_STATUS_IS_OK(status)) {
2236 TALLOC_FREE(lck);
2237 fd_close(fsp);
2238 return status;
2242 /* note that we ignore failure for the following. It is
2243 basically a hack for NFS, and NFS will never set one of
2244 these only read them. Nobody but Samba can ever set a deny
2245 mode and we have already checked our more authoritative
2246 locking database for permission to set this deny mode. If
2247 the kernel refuses the operations then the kernel is wrong.
2248 note that GPFS supports it as well - jmcd */
2250 if (fsp->fh->fd != -1) {
2251 int ret_flock;
2252 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
2253 if(ret_flock == -1 ){
2255 TALLOC_FREE(lck);
2256 fd_close(fsp);
2258 return NT_STATUS_SHARING_VIOLATION;
2263 * At this point onwards, we can guarentee that the share entry
2264 * is locked, whether we created the file or not, and that the
2265 * deny mode is compatible with all current opens.
2269 * If requested, truncate the file.
2272 if (file_existed && (flags2&O_TRUNC)) {
2274 * We are modifing the file after open - update the stat
2275 * struct..
2277 if ((SMB_VFS_FTRUNCATE(fsp, 0) == -1) ||
2278 (SMB_VFS_FSTAT(fsp, &smb_fname->st)==-1)) {
2279 status = map_nt_error_from_unix(errno);
2280 TALLOC_FREE(lck);
2281 fd_close(fsp);
2282 return status;
2287 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2288 * but we don't have to store this - just ignore it on access check.
2290 if (conn->sconn->using_smb2) {
2292 * SMB2 doesn't return it (according to Microsoft tests).
2293 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2294 * File created with access = 0x7 (Read, Write, Delete)
2295 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2297 fsp->access_mask = access_mask;
2298 } else {
2299 /* But SMB1 does. */
2300 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2303 if (file_existed) {
2304 /* stat opens on existing files don't get oplocks. */
2305 if (is_stat_open(open_access_mask)) {
2306 fsp->oplock_type = NO_OPLOCK;
2309 if (!(flags2 & O_TRUNC)) {
2310 info = FILE_WAS_OPENED;
2311 } else {
2312 info = FILE_WAS_OVERWRITTEN;
2314 } else {
2315 info = FILE_WAS_CREATED;
2318 if (pinfo) {
2319 *pinfo = info;
2323 * Setup the oplock info in both the shared memory and
2324 * file structs.
2327 if (!set_file_oplock(fsp, fsp->oplock_type)) {
2329 * Could not get the kernel oplock or there are byte-range
2330 * locks on the file.
2332 fsp->oplock_type = NO_OPLOCK;
2335 if (info == FILE_WAS_OVERWRITTEN || info == FILE_WAS_CREATED || info == FILE_WAS_SUPERSEDED) {
2336 new_file_created = True;
2339 set_share_mode(lck, fsp, get_current_uid(conn), 0,
2340 fsp->oplock_type);
2342 /* Handle strange delete on close create semantics. */
2343 if (create_options & FILE_DELETE_ON_CLOSE) {
2345 status = can_set_delete_on_close(fsp, new_dos_attributes);
2347 if (!NT_STATUS_IS_OK(status)) {
2348 /* Remember to delete the mode we just added. */
2349 del_share_mode(lck, fsp);
2350 TALLOC_FREE(lck);
2351 fd_close(fsp);
2352 return status;
2354 /* Note that here we set the *inital* delete on close flag,
2355 not the regular one. The magic gets handled in close. */
2356 fsp->initial_delete_on_close = True;
2359 if (new_file_created) {
2360 /* Files should be initially set as archive */
2361 if (lp_map_archive(SNUM(conn)) ||
2362 lp_store_dos_attributes(SNUM(conn))) {
2363 if (!posix_open) {
2364 if (file_set_dosmode(conn, smb_fname,
2365 new_dos_attributes | aARCH,
2366 parent_dir, true) == 0) {
2367 unx_mode = smb_fname->st.st_ex_mode;
2373 /* Determine sparse flag. */
2374 if (posix_open) {
2375 /* POSIX opens are sparse by default. */
2376 fsp->is_sparse = true;
2377 } else {
2378 fsp->is_sparse = (file_existed &&
2379 (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE));
2383 * Take care of inherited ACLs on created files - if default ACL not
2384 * selected.
2387 if (!posix_open && !file_existed && !def_acl) {
2389 int saved_errno = errno; /* We might get ENOSYS in the next
2390 * call.. */
2392 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2393 errno == ENOSYS) {
2394 errno = saved_errno; /* Ignore ENOSYS */
2397 } else if (new_unx_mode) {
2399 int ret = -1;
2401 /* Attributes need changing. File already existed. */
2404 int saved_errno = errno; /* We might get ENOSYS in the
2405 * next call.. */
2406 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2408 if (ret == -1 && errno == ENOSYS) {
2409 errno = saved_errno; /* Ignore ENOSYS */
2410 } else {
2411 DEBUG(5, ("open_file_ntcreate: reset "
2412 "attributes of file %s to 0%o\n",
2413 smb_fname_str_dbg(smb_fname),
2414 (unsigned int)new_unx_mode));
2415 ret = 0; /* Don't do the fchmod below. */
2419 if ((ret == -1) &&
2420 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2421 DEBUG(5, ("open_file_ntcreate: failed to reset "
2422 "attributes of file %s to 0%o\n",
2423 smb_fname_str_dbg(smb_fname),
2424 (unsigned int)new_unx_mode));
2427 /* If this is a successful open, we must remove any deferred open
2428 * records. */
2429 if (req != NULL) {
2430 del_deferred_open_entry(lck, req->mid,
2431 sconn_server_id(req->sconn));
2433 TALLOC_FREE(lck);
2435 return NT_STATUS_OK;
2439 /****************************************************************************
2440 Open a file for for write to ensure that we can fchmod it.
2441 ****************************************************************************/
2443 NTSTATUS open_file_fchmod(connection_struct *conn,
2444 struct smb_filename *smb_fname,
2445 files_struct **result)
2447 if (!VALID_STAT(smb_fname->st)) {
2448 return NT_STATUS_INVALID_PARAMETER;
2451 return SMB_VFS_CREATE_FILE(
2452 conn, /* conn */
2453 NULL, /* req */
2454 0, /* root_dir_fid */
2455 smb_fname, /* fname */
2456 FILE_WRITE_DATA, /* access_mask */
2457 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2458 FILE_SHARE_DELETE),
2459 FILE_OPEN, /* create_disposition*/
2460 0, /* create_options */
2461 0, /* file_attributes */
2462 INTERNAL_OPEN_ONLY, /* oplock_request */
2463 0, /* allocation_size */
2464 0, /* private_flags */
2465 NULL, /* sd */
2466 NULL, /* ea_list */
2467 result, /* result */
2468 NULL); /* pinfo */
2471 static NTSTATUS mkdir_internal(connection_struct *conn,
2472 struct smb_filename *smb_dname,
2473 uint32 file_attributes)
2475 mode_t mode;
2476 char *parent_dir;
2477 NTSTATUS status;
2478 bool posix_open = false;
2480 if(!CAN_WRITE(conn)) {
2481 DEBUG(5,("mkdir_internal: failing create on read-only share "
2482 "%s\n", lp_servicename(SNUM(conn))));
2483 return NT_STATUS_ACCESS_DENIED;
2486 status = check_name(conn, smb_dname->base_name);
2487 if (!NT_STATUS_IS_OK(status)) {
2488 return status;
2491 if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
2492 NULL)) {
2493 return NT_STATUS_NO_MEMORY;
2496 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2497 posix_open = true;
2498 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2499 } else {
2500 mode = unix_mode(conn, aDIR, smb_dname, parent_dir);
2503 if (SMB_VFS_MKDIR(conn, smb_dname->base_name, mode) != 0) {
2504 return map_nt_error_from_unix(errno);
2507 /* Ensure we're checking for a symlink here.... */
2508 /* We don't want to get caught by a symlink racer. */
2510 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2511 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2512 smb_fname_str_dbg(smb_dname), strerror(errno)));
2513 return map_nt_error_from_unix(errno);
2516 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
2517 DEBUG(0, ("Directory just '%s' created is not a directory\n",
2518 smb_fname_str_dbg(smb_dname)));
2519 return NT_STATUS_ACCESS_DENIED;
2522 if (lp_store_dos_attributes(SNUM(conn))) {
2523 if (!posix_open) {
2524 file_set_dosmode(conn, smb_dname,
2525 file_attributes | aDIR,
2526 parent_dir, true);
2530 if (lp_inherit_perms(SNUM(conn))) {
2531 inherit_access_posix_acl(conn, parent_dir,
2532 smb_dname->base_name, mode);
2535 if (!posix_open) {
2537 * Check if high bits should have been set,
2538 * then (if bits are missing): add them.
2539 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2540 * dir.
2542 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
2543 (mode & ~smb_dname->st.st_ex_mode)) {
2544 SMB_VFS_CHMOD(conn, smb_dname->base_name,
2545 (smb_dname->st.st_ex_mode |
2546 (mode & ~smb_dname->st.st_ex_mode)));
2550 /* Change the owner if required. */
2551 if (lp_inherit_owner(SNUM(conn))) {
2552 change_dir_owner_to_parent(conn, parent_dir,
2553 smb_dname->base_name,
2554 &smb_dname->st);
2557 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2558 smb_dname->base_name);
2560 return NT_STATUS_OK;
2563 /****************************************************************************
2564 Ensure we didn't get symlink raced on opening a directory.
2565 ****************************************************************************/
2567 static bool check_same_stat(const SMB_STRUCT_STAT *sbuf1,
2568 const SMB_STRUCT_STAT *sbuf2)
2570 if (sbuf1->st_ex_uid != sbuf2->st_ex_uid ||
2571 sbuf1->st_ex_gid != sbuf2->st_ex_gid ||
2572 sbuf1->st_ex_dev != sbuf2->st_ex_dev ||
2573 sbuf1->st_ex_ino != sbuf2->st_ex_ino) {
2574 return false;
2576 return true;
2579 /****************************************************************************
2580 Open a directory from an NT SMB call.
2581 ****************************************************************************/
2583 static NTSTATUS open_directory(connection_struct *conn,
2584 struct smb_request *req,
2585 struct smb_filename *smb_dname,
2586 uint32 access_mask,
2587 uint32 share_access,
2588 uint32 create_disposition,
2589 uint32 create_options,
2590 uint32 file_attributes,
2591 int *pinfo,
2592 files_struct **result)
2594 files_struct *fsp = NULL;
2595 bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
2596 struct share_mode_lock *lck = NULL;
2597 NTSTATUS status;
2598 struct timespec mtimespec;
2599 int info = 0;
2601 SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname));
2603 /* Ensure we have a directory attribute. */
2604 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
2606 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2607 "share_access = 0x%x create_options = 0x%x, "
2608 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2609 smb_fname_str_dbg(smb_dname),
2610 (unsigned int)access_mask,
2611 (unsigned int)share_access,
2612 (unsigned int)create_options,
2613 (unsigned int)create_disposition,
2614 (unsigned int)file_attributes));
2616 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
2617 (conn->fs_capabilities & FILE_NAMED_STREAMS) &&
2618 is_ntfs_stream_smb_fname(smb_dname)) {
2619 DEBUG(2, ("open_directory: %s is a stream name!\n",
2620 smb_fname_str_dbg(smb_dname)));
2621 return NT_STATUS_NOT_A_DIRECTORY;
2624 status = calculate_access_mask(conn, smb_dname, dir_existed,
2625 access_mask, &access_mask);
2626 if (!NT_STATUS_IS_OK(status)) {
2627 DEBUG(10, ("open_directory: calculate_access_mask "
2628 "on file %s returned %s\n",
2629 smb_fname_str_dbg(smb_dname),
2630 nt_errstr(status)));
2631 return status;
2634 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
2635 !security_token_has_privilege(get_current_nttok(conn),
2636 SEC_PRIV_SECURITY)) {
2637 DEBUG(10, ("open_directory: open on %s "
2638 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2639 smb_fname_str_dbg(smb_dname)));
2640 return NT_STATUS_PRIVILEGE_NOT_HELD;
2643 switch( create_disposition ) {
2644 case FILE_OPEN:
2646 if (!dir_existed) {
2647 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2650 info = FILE_WAS_OPENED;
2651 break;
2653 case FILE_CREATE:
2655 /* If directory exists error. If directory doesn't
2656 * exist create. */
2658 status = mkdir_internal(conn, smb_dname,
2659 file_attributes);
2661 if (!NT_STATUS_IS_OK(status)) {
2662 DEBUG(2, ("open_directory: unable to create "
2663 "%s. Error was %s\n",
2664 smb_fname_str_dbg(smb_dname),
2665 nt_errstr(status)));
2666 return status;
2669 info = FILE_WAS_CREATED;
2670 break;
2672 case FILE_OPEN_IF:
2674 * If directory exists open. If directory doesn't
2675 * exist create.
2678 status = mkdir_internal(conn, smb_dname,
2679 file_attributes);
2681 if (NT_STATUS_IS_OK(status)) {
2682 info = FILE_WAS_CREATED;
2685 if (NT_STATUS_EQUAL(status,
2686 NT_STATUS_OBJECT_NAME_COLLISION)) {
2687 info = FILE_WAS_OPENED;
2688 status = NT_STATUS_OK;
2690 break;
2692 case FILE_SUPERSEDE:
2693 case FILE_OVERWRITE:
2694 case FILE_OVERWRITE_IF:
2695 default:
2696 DEBUG(5,("open_directory: invalid create_disposition "
2697 "0x%x for directory %s\n",
2698 (unsigned int)create_disposition,
2699 smb_fname_str_dbg(smb_dname)));
2700 return NT_STATUS_INVALID_PARAMETER;
2703 if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
2704 DEBUG(5,("open_directory: %s is not a directory !\n",
2705 smb_fname_str_dbg(smb_dname)));
2706 return NT_STATUS_NOT_A_DIRECTORY;
2709 if (info == FILE_WAS_OPENED) {
2710 uint32_t access_granted = 0;
2711 status = smbd_check_open_rights(conn, smb_dname, access_mask,
2712 &access_granted);
2714 /* Were we trying to do a directory open
2715 * for delete and didn't get DELETE
2716 * access (only) ? Check if the
2717 * directory allows DELETE_CHILD.
2718 * See here:
2719 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
2720 * for details. */
2722 if ((NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) &&
2723 (access_mask & DELETE_ACCESS) &&
2724 (access_granted == DELETE_ACCESS) &&
2725 can_delete_file_in_directory(conn, smb_dname))) {
2726 DEBUG(10,("open_directory: overrode ACCESS_DENIED "
2727 "on directory %s\n",
2728 smb_fname_str_dbg(smb_dname)));
2729 status = NT_STATUS_OK;
2732 if (!NT_STATUS_IS_OK(status)) {
2733 DEBUG(10, ("open_directory: smbd_check_open_rights on "
2734 "file %s failed with %s\n",
2735 smb_fname_str_dbg(smb_dname),
2736 nt_errstr(status)));
2737 return status;
2741 status = file_new(req, conn, &fsp);
2742 if(!NT_STATUS_IS_OK(status)) {
2743 return status;
2747 * Setup the files_struct for it.
2750 fsp->mode = smb_dname->st.st_ex_mode;
2751 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
2752 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
2753 fsp->file_pid = req ? req->smbpid : 0;
2754 fsp->can_lock = False;
2755 fsp->can_read = False;
2756 fsp->can_write = False;
2758 fsp->share_access = share_access;
2759 fsp->fh->private_options = 0;
2761 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2763 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2764 fsp->print_file = NULL;
2765 fsp->modified = False;
2766 fsp->oplock_type = NO_OPLOCK;
2767 fsp->sent_oplock_break = NO_BREAK_SENT;
2768 fsp->is_directory = True;
2769 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
2770 status = fsp_set_smb_fname(fsp, smb_dname);
2771 if (!NT_STATUS_IS_OK(status)) {
2772 file_free(req, fsp);
2773 return status;
2776 mtimespec = smb_dname->st.st_ex_mtime;
2778 #ifdef O_DIRECTORY
2779 status = fd_open(conn, fsp, O_RDONLY|O_DIRECTORY, 0);
2780 #else
2781 /* POSIX allows us to open a directory with O_RDONLY. */
2782 status = fd_open(conn, fsp, O_RDONLY, 0);
2783 #endif
2784 if (!NT_STATUS_IS_OK(status)) {
2785 DEBUG(5, ("open_directory: Could not open fd for "
2786 "%s (%s)\n",
2787 smb_fname_str_dbg(smb_dname),
2788 nt_errstr(status)));
2789 file_free(req, fsp);
2790 return status;
2793 status = vfs_stat_fsp(fsp);
2794 if (!NT_STATUS_IS_OK(status)) {
2795 fd_close(fsp);
2796 file_free(req, fsp);
2797 return status;
2800 /* Ensure there was no race condition. */
2801 if (!check_same_stat(&smb_dname->st, &fsp->fsp_name->st)) {
2802 DEBUG(5,("open_directory: stat struct differs for "
2803 "directory %s.\n",
2804 smb_fname_str_dbg(smb_dname)));
2805 fd_close(fsp);
2806 file_free(req, fsp);
2807 return NT_STATUS_ACCESS_DENIED;
2810 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
2811 conn->connectpath, smb_dname, &mtimespec);
2813 if (lck == NULL) {
2814 DEBUG(0, ("open_directory: Could not get share mode lock for "
2815 "%s\n", smb_fname_str_dbg(smb_dname)));
2816 fd_close(fsp);
2817 file_free(req, fsp);
2818 return NT_STATUS_SHARING_VIOLATION;
2821 status = open_mode_check(conn, lck, fsp->name_hash,
2822 access_mask, share_access,
2823 create_options, &dir_existed);
2825 if (!NT_STATUS_IS_OK(status)) {
2826 TALLOC_FREE(lck);
2827 fd_close(fsp);
2828 file_free(req, fsp);
2829 return status;
2832 set_share_mode(lck, fsp, get_current_uid(conn), 0, NO_OPLOCK);
2834 /* For directories the delete on close bit at open time seems
2835 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2836 if (create_options & FILE_DELETE_ON_CLOSE) {
2837 status = can_set_delete_on_close(fsp, 0);
2838 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
2839 TALLOC_FREE(lck);
2840 fd_close(fsp);
2841 file_free(req, fsp);
2842 return status;
2845 if (NT_STATUS_IS_OK(status)) {
2846 /* Note that here we set the *inital* delete on close flag,
2847 not the regular one. The magic gets handled in close. */
2848 fsp->initial_delete_on_close = True;
2852 TALLOC_FREE(lck);
2854 if (pinfo) {
2855 *pinfo = info;
2858 *result = fsp;
2859 return NT_STATUS_OK;
2862 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
2863 struct smb_filename *smb_dname)
2865 NTSTATUS status;
2866 files_struct *fsp;
2868 status = SMB_VFS_CREATE_FILE(
2869 conn, /* conn */
2870 req, /* req */
2871 0, /* root_dir_fid */
2872 smb_dname, /* fname */
2873 FILE_READ_ATTRIBUTES, /* access_mask */
2874 FILE_SHARE_NONE, /* share_access */
2875 FILE_CREATE, /* create_disposition*/
2876 FILE_DIRECTORY_FILE, /* create_options */
2877 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
2878 0, /* oplock_request */
2879 0, /* allocation_size */
2880 0, /* private_flags */
2881 NULL, /* sd */
2882 NULL, /* ea_list */
2883 &fsp, /* result */
2884 NULL); /* pinfo */
2886 if (NT_STATUS_IS_OK(status)) {
2887 close_file(req, fsp, NORMAL_CLOSE);
2890 return status;
2893 /****************************************************************************
2894 Receive notification that one of our open files has been renamed by another
2895 smbd process.
2896 ****************************************************************************/
2898 void msg_file_was_renamed(struct messaging_context *msg,
2899 void *private_data,
2900 uint32_t msg_type,
2901 struct server_id server_id,
2902 DATA_BLOB *data)
2904 struct smbd_server_connection *sconn;
2905 files_struct *fsp;
2906 char *frm = (char *)data->data;
2907 struct file_id id;
2908 const char *sharepath;
2909 const char *base_name;
2910 const char *stream_name;
2911 struct smb_filename *smb_fname = NULL;
2912 size_t sp_len, bn_len;
2913 NTSTATUS status;
2915 sconn = msg_ctx_to_sconn(msg);
2916 if (sconn == NULL) {
2917 DEBUG(1, ("could not find sconn\n"));
2918 return;
2921 if (data->data == NULL
2922 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
2923 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
2924 (int)data->length));
2925 return;
2928 /* Unpack the message. */
2929 pull_file_id_24(frm, &id);
2930 sharepath = &frm[24];
2931 sp_len = strlen(sharepath);
2932 base_name = sharepath + sp_len + 1;
2933 bn_len = strlen(base_name);
2934 stream_name = sharepath + sp_len + 1 + bn_len + 1;
2936 /* stream_name must always be NULL if there is no stream. */
2937 if (stream_name[0] == '\0') {
2938 stream_name = NULL;
2941 status = create_synthetic_smb_fname(talloc_tos(), base_name,
2942 stream_name, NULL, &smb_fname);
2943 if (!NT_STATUS_IS_OK(status)) {
2944 return;
2947 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
2948 "file_id %s\n",
2949 sharepath, smb_fname_str_dbg(smb_fname),
2950 file_id_string_tos(&id)));
2952 for(fsp = file_find_di_first(sconn, id); fsp;
2953 fsp = file_find_di_next(fsp)) {
2954 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
2956 DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n",
2957 fsp->fnum, fsp_str_dbg(fsp),
2958 smb_fname_str_dbg(smb_fname)));
2959 status = fsp_set_smb_fname(fsp, smb_fname);
2960 if (!NT_STATUS_IS_OK(status)) {
2961 goto out;
2963 } else {
2964 /* TODO. JRA. */
2965 /* Now we have the complete path we can work out if this is
2966 actually within this share and adjust newname accordingly. */
2967 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
2968 "not sharepath %s) "
2969 "fnum %d from %s -> %s\n",
2970 fsp->conn->connectpath,
2971 sharepath,
2972 fsp->fnum,
2973 fsp_str_dbg(fsp),
2974 smb_fname_str_dbg(smb_fname)));
2977 out:
2978 TALLOC_FREE(smb_fname);
2979 return;
2983 * If a main file is opened for delete, all streams need to be checked for
2984 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
2985 * If that works, delete them all by setting the delete on close and close.
2988 NTSTATUS open_streams_for_delete(connection_struct *conn,
2989 const char *fname)
2991 struct stream_struct *stream_info;
2992 files_struct **streams;
2993 int i;
2994 unsigned int num_streams;
2995 TALLOC_CTX *frame = talloc_stackframe();
2996 NTSTATUS status;
2998 status = SMB_VFS_STREAMINFO(conn, NULL, fname, talloc_tos(),
2999 &num_streams, &stream_info);
3001 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
3002 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
3003 DEBUG(10, ("no streams around\n"));
3004 TALLOC_FREE(frame);
3005 return NT_STATUS_OK;
3008 if (!NT_STATUS_IS_OK(status)) {
3009 DEBUG(10, ("SMB_VFS_STREAMINFO failed: %s\n",
3010 nt_errstr(status)));
3011 goto fail;
3014 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3015 num_streams));
3017 if (num_streams == 0) {
3018 TALLOC_FREE(frame);
3019 return NT_STATUS_OK;
3022 streams = TALLOC_ARRAY(talloc_tos(), files_struct *, num_streams);
3023 if (streams == NULL) {
3024 DEBUG(0, ("talloc failed\n"));
3025 status = NT_STATUS_NO_MEMORY;
3026 goto fail;
3029 for (i=0; i<num_streams; i++) {
3030 struct smb_filename *smb_fname = NULL;
3032 if (strequal(stream_info[i].name, "::$DATA")) {
3033 streams[i] = NULL;
3034 continue;
3037 status = create_synthetic_smb_fname(talloc_tos(), fname,
3038 stream_info[i].name,
3039 NULL, &smb_fname);
3040 if (!NT_STATUS_IS_OK(status)) {
3041 goto fail;
3044 if (SMB_VFS_STAT(conn, smb_fname) == -1) {
3045 DEBUG(10, ("Unable to stat stream: %s\n",
3046 smb_fname_str_dbg(smb_fname)));
3049 status = SMB_VFS_CREATE_FILE(
3050 conn, /* conn */
3051 NULL, /* req */
3052 0, /* root_dir_fid */
3053 smb_fname, /* fname */
3054 DELETE_ACCESS, /* access_mask */
3055 (FILE_SHARE_READ | /* share_access */
3056 FILE_SHARE_WRITE | FILE_SHARE_DELETE),
3057 FILE_OPEN, /* create_disposition*/
3058 0, /* create_options */
3059 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
3060 0, /* oplock_request */
3061 0, /* allocation_size */
3062 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */
3063 NULL, /* sd */
3064 NULL, /* ea_list */
3065 &streams[i], /* result */
3066 NULL); /* pinfo */
3068 if (!NT_STATUS_IS_OK(status)) {
3069 DEBUG(10, ("Could not open stream %s: %s\n",
3070 smb_fname_str_dbg(smb_fname),
3071 nt_errstr(status)));
3073 TALLOC_FREE(smb_fname);
3074 break;
3076 TALLOC_FREE(smb_fname);
3080 * don't touch the variable "status" beyond this point :-)
3083 for (i -= 1 ; i >= 0; i--) {
3084 if (streams[i] == NULL) {
3085 continue;
3088 DEBUG(10, ("Closing stream # %d, %s\n", i,
3089 fsp_str_dbg(streams[i])));
3090 close_file(NULL, streams[i], NORMAL_CLOSE);
3093 fail:
3094 TALLOC_FREE(frame);
3095 return status;
3099 * Wrapper around open_file_ntcreate and open_directory
3102 static NTSTATUS create_file_unixpath(connection_struct *conn,
3103 struct smb_request *req,
3104 struct smb_filename *smb_fname,
3105 uint32_t access_mask,
3106 uint32_t share_access,
3107 uint32_t create_disposition,
3108 uint32_t create_options,
3109 uint32_t file_attributes,
3110 uint32_t oplock_request,
3111 uint64_t allocation_size,
3112 uint32_t private_flags,
3113 struct security_descriptor *sd,
3114 struct ea_list *ea_list,
3116 files_struct **result,
3117 int *pinfo)
3119 int info = FILE_WAS_OPENED;
3120 files_struct *base_fsp = NULL;
3121 files_struct *fsp = NULL;
3122 NTSTATUS status;
3124 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3125 "file_attributes = 0x%x, share_access = 0x%x, "
3126 "create_disposition = 0x%x create_options = 0x%x "
3127 "oplock_request = 0x%x private_flags = 0x%x "
3128 "ea_list = 0x%p, sd = 0x%p, "
3129 "fname = %s\n",
3130 (unsigned int)access_mask,
3131 (unsigned int)file_attributes,
3132 (unsigned int)share_access,
3133 (unsigned int)create_disposition,
3134 (unsigned int)create_options,
3135 (unsigned int)oplock_request,
3136 (unsigned int)private_flags,
3137 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3139 if (create_options & FILE_OPEN_BY_FILE_ID) {
3140 status = NT_STATUS_NOT_SUPPORTED;
3141 goto fail;
3144 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
3145 status = NT_STATUS_INVALID_PARAMETER;
3146 goto fail;
3149 if (req == NULL) {
3150 oplock_request |= INTERNAL_OPEN_ONLY;
3153 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3154 && (access_mask & DELETE_ACCESS)
3155 && !is_ntfs_stream_smb_fname(smb_fname)) {
3157 * We can't open a file with DELETE access if any of the
3158 * streams is open without FILE_SHARE_DELETE
3160 status = open_streams_for_delete(conn, smb_fname->base_name);
3162 if (!NT_STATUS_IS_OK(status)) {
3163 goto fail;
3167 /* This is the correct thing to do (check every time) but can_delete
3168 * is expensive (it may have to read the parent directory
3169 * permissions). So for now we're not doing it unless we have a strong
3170 * hint the client is really going to delete this file. If the client
3171 * is forcing FILE_CREATE let the filesystem take care of the
3172 * permissions. */
3174 /* Setting FILE_SHARE_DELETE is the hint. */
3176 if (lp_acl_check_permissions(SNUM(conn))
3177 && (create_disposition != FILE_CREATE)
3178 && (access_mask & DELETE_ACCESS)
3179 && (!(can_delete_file_in_directory(conn, smb_fname) ||
3180 can_access_file_acl(conn, smb_fname, DELETE_ACCESS)))) {
3181 status = NT_STATUS_ACCESS_DENIED;
3182 DEBUG(10,("create_file_unixpath: open file %s "
3183 "for delete ACCESS_DENIED\n",
3184 smb_fname_str_dbg(smb_fname)));
3185 goto fail;
3188 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3189 !security_token_has_privilege(get_current_nttok(conn),
3190 SEC_PRIV_SECURITY)) {
3191 DEBUG(10, ("create_file_unixpath: open on %s "
3192 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3193 smb_fname_str_dbg(smb_fname)));
3194 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3195 goto fail;
3198 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3199 && is_ntfs_stream_smb_fname(smb_fname)
3200 && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
3201 uint32 base_create_disposition;
3202 struct smb_filename *smb_fname_base = NULL;
3204 if (create_options & FILE_DIRECTORY_FILE) {
3205 status = NT_STATUS_NOT_A_DIRECTORY;
3206 goto fail;
3209 switch (create_disposition) {
3210 case FILE_OPEN:
3211 base_create_disposition = FILE_OPEN;
3212 break;
3213 default:
3214 base_create_disposition = FILE_OPEN_IF;
3215 break;
3218 /* Create an smb_filename with stream_name == NULL. */
3219 status = create_synthetic_smb_fname(talloc_tos(),
3220 smb_fname->base_name,
3221 NULL, NULL,
3222 &smb_fname_base);
3223 if (!NT_STATUS_IS_OK(status)) {
3224 goto fail;
3227 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
3228 DEBUG(10, ("Unable to stat stream: %s\n",
3229 smb_fname_str_dbg(smb_fname_base)));
3232 /* Open the base file. */
3233 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
3234 FILE_SHARE_READ
3235 | FILE_SHARE_WRITE
3236 | FILE_SHARE_DELETE,
3237 base_create_disposition,
3238 0, 0, 0, 0, 0, NULL, NULL,
3239 &base_fsp, NULL);
3240 TALLOC_FREE(smb_fname_base);
3242 if (!NT_STATUS_IS_OK(status)) {
3243 DEBUG(10, ("create_file_unixpath for base %s failed: "
3244 "%s\n", smb_fname->base_name,
3245 nt_errstr(status)));
3246 goto fail;
3248 /* we don't need to low level fd */
3249 fd_close(base_fsp);
3253 * If it's a request for a directory open, deal with it separately.
3256 if (create_options & FILE_DIRECTORY_FILE) {
3258 if (create_options & FILE_NON_DIRECTORY_FILE) {
3259 status = NT_STATUS_INVALID_PARAMETER;
3260 goto fail;
3263 /* Can't open a temp directory. IFS kit test. */
3264 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
3265 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
3266 status = NT_STATUS_INVALID_PARAMETER;
3267 goto fail;
3271 * We will get a create directory here if the Win32
3272 * app specified a security descriptor in the
3273 * CreateDirectory() call.
3276 oplock_request = 0;
3277 status = open_directory(
3278 conn, req, smb_fname, access_mask, share_access,
3279 create_disposition, create_options, file_attributes,
3280 &info, &fsp);
3281 } else {
3284 * Ordinary file case.
3287 status = file_new(req, conn, &fsp);
3288 if(!NT_STATUS_IS_OK(status)) {
3289 goto fail;
3292 status = fsp_set_smb_fname(fsp, smb_fname);
3293 if (!NT_STATUS_IS_OK(status)) {
3294 goto fail;
3298 * We're opening the stream element of a base_fsp
3299 * we already opened. Set up the base_fsp pointer.
3301 if (base_fsp) {
3302 fsp->base_fsp = base_fsp;
3305 status = open_file_ntcreate(conn,
3306 req,
3307 access_mask,
3308 share_access,
3309 create_disposition,
3310 create_options,
3311 file_attributes,
3312 oplock_request,
3313 private_flags,
3314 &info,
3315 fsp);
3317 if(!NT_STATUS_IS_OK(status)) {
3318 file_free(req, fsp);
3319 fsp = NULL;
3322 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
3324 /* A stream open never opens a directory */
3326 if (base_fsp) {
3327 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3328 goto fail;
3332 * Fail the open if it was explicitly a non-directory
3333 * file.
3336 if (create_options & FILE_NON_DIRECTORY_FILE) {
3337 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3338 goto fail;
3341 oplock_request = 0;
3342 status = open_directory(
3343 conn, req, smb_fname, access_mask,
3344 share_access, create_disposition,
3345 create_options, file_attributes,
3346 &info, &fsp);
3350 if (!NT_STATUS_IS_OK(status)) {
3351 goto fail;
3354 fsp->base_fsp = base_fsp;
3357 * According to the MS documentation, the only time the security
3358 * descriptor is applied to the opened file is iff we *created* the
3359 * file; an existing file stays the same.
3361 * Also, it seems (from observation) that you can open the file with
3362 * any access mask but you can still write the sd. We need to override
3363 * the granted access before we call set_sd
3364 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3367 if ((sd != NULL) && (info == FILE_WAS_CREATED)
3368 && lp_nt_acl_support(SNUM(conn))) {
3370 uint32_t sec_info_sent;
3371 uint32_t saved_access_mask = fsp->access_mask;
3373 sec_info_sent = get_sec_info(sd);
3375 fsp->access_mask = FILE_GENERIC_ALL;
3377 /* Convert all the generic bits. */
3378 security_acl_map_generic(sd->dacl, &file_generic_mapping);
3379 security_acl_map_generic(sd->sacl, &file_generic_mapping);
3381 if (sec_info_sent & (SECINFO_OWNER|
3382 SECINFO_GROUP|
3383 SECINFO_DACL|
3384 SECINFO_SACL)) {
3385 status = SMB_VFS_FSET_NT_ACL(fsp, sec_info_sent, sd);
3388 fsp->access_mask = saved_access_mask;
3390 if (!NT_STATUS_IS_OK(status)) {
3391 goto fail;
3395 if ((ea_list != NULL) &&
3396 ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
3397 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
3398 if (!NT_STATUS_IS_OK(status)) {
3399 goto fail;
3403 if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
3404 status = NT_STATUS_ACCESS_DENIED;
3405 goto fail;
3408 /* Save the requested allocation size. */
3409 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
3410 if (allocation_size
3411 && (allocation_size > fsp->fsp_name->st.st_ex_size)) {
3412 fsp->initial_allocation_size = smb_roundup(
3413 fsp->conn, allocation_size);
3414 if (fsp->is_directory) {
3415 /* Can't set allocation size on a directory. */
3416 status = NT_STATUS_ACCESS_DENIED;
3417 goto fail;
3419 if (vfs_allocate_file_space(
3420 fsp, fsp->initial_allocation_size) == -1) {
3421 status = NT_STATUS_DISK_FULL;
3422 goto fail;
3424 } else {
3425 fsp->initial_allocation_size = smb_roundup(
3426 fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
3430 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
3432 *result = fsp;
3433 if (pinfo != NULL) {
3434 *pinfo = info;
3437 smb_fname->st = fsp->fsp_name->st;
3439 return NT_STATUS_OK;
3441 fail:
3442 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
3444 if (fsp != NULL) {
3445 if (base_fsp && fsp->base_fsp == base_fsp) {
3447 * The close_file below will close
3448 * fsp->base_fsp.
3450 base_fsp = NULL;
3452 close_file(req, fsp, ERROR_CLOSE);
3453 fsp = NULL;
3455 if (base_fsp != NULL) {
3456 close_file(req, base_fsp, ERROR_CLOSE);
3457 base_fsp = NULL;
3459 return status;
3463 * Calculate the full path name given a relative fid.
3465 NTSTATUS get_relative_fid_filename(connection_struct *conn,
3466 struct smb_request *req,
3467 uint16_t root_dir_fid,
3468 const struct smb_filename *smb_fname,
3469 struct smb_filename **smb_fname_out)
3471 files_struct *dir_fsp;
3472 char *parent_fname = NULL;
3473 char *new_base_name = NULL;
3474 NTSTATUS status;
3476 if (root_dir_fid == 0 || !smb_fname) {
3477 status = NT_STATUS_INTERNAL_ERROR;
3478 goto out;
3481 dir_fsp = file_fsp(req, root_dir_fid);
3483 if (dir_fsp == NULL) {
3484 status = NT_STATUS_INVALID_HANDLE;
3485 goto out;
3488 if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) {
3489 status = NT_STATUS_INVALID_HANDLE;
3490 goto out;
3493 if (!dir_fsp->is_directory) {
3496 * Check to see if this is a mac fork of some kind.
3499 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
3500 is_ntfs_stream_smb_fname(smb_fname)) {
3501 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3502 goto out;
3506 we need to handle the case when we get a
3507 relative open relative to a file and the
3508 pathname is blank - this is a reopen!
3509 (hint from demyn plantenberg)
3512 status = NT_STATUS_INVALID_HANDLE;
3513 goto out;
3516 if (ISDOT(dir_fsp->fsp_name->base_name)) {
3518 * We're at the toplevel dir, the final file name
3519 * must not contain ./, as this is filtered out
3520 * normally by srvstr_get_path and unix_convert
3521 * explicitly rejects paths containing ./.
3523 parent_fname = talloc_strdup(talloc_tos(), "");
3524 if (parent_fname == NULL) {
3525 status = NT_STATUS_NO_MEMORY;
3526 goto out;
3528 } else {
3529 size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name);
3532 * Copy in the base directory name.
3535 parent_fname = TALLOC_ARRAY(talloc_tos(), char,
3536 dir_name_len+2);
3537 if (parent_fname == NULL) {
3538 status = NT_STATUS_NO_MEMORY;
3539 goto out;
3541 memcpy(parent_fname, dir_fsp->fsp_name->base_name,
3542 dir_name_len+1);
3545 * Ensure it ends in a '/'.
3546 * We used TALLOC_SIZE +2 to add space for the '/'.
3549 if(dir_name_len
3550 && (parent_fname[dir_name_len-1] != '\\')
3551 && (parent_fname[dir_name_len-1] != '/')) {
3552 parent_fname[dir_name_len] = '/';
3553 parent_fname[dir_name_len+1] = '\0';
3557 new_base_name = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
3558 smb_fname->base_name);
3559 if (new_base_name == NULL) {
3560 status = NT_STATUS_NO_MEMORY;
3561 goto out;
3564 status = filename_convert(req,
3565 conn,
3566 req->flags2 & FLAGS2_DFS_PATHNAMES,
3567 new_base_name,
3569 NULL,
3570 smb_fname_out);
3571 if (!NT_STATUS_IS_OK(status)) {
3572 goto out;
3575 out:
3576 TALLOC_FREE(parent_fname);
3577 TALLOC_FREE(new_base_name);
3578 return status;
3581 NTSTATUS create_file_default(connection_struct *conn,
3582 struct smb_request *req,
3583 uint16_t root_dir_fid,
3584 struct smb_filename *smb_fname,
3585 uint32_t access_mask,
3586 uint32_t share_access,
3587 uint32_t create_disposition,
3588 uint32_t create_options,
3589 uint32_t file_attributes,
3590 uint32_t oplock_request,
3591 uint64_t allocation_size,
3592 uint32_t private_flags,
3593 struct security_descriptor *sd,
3594 struct ea_list *ea_list,
3595 files_struct **result,
3596 int *pinfo)
3598 int info = FILE_WAS_OPENED;
3599 files_struct *fsp = NULL;
3600 NTSTATUS status;
3601 bool stream_name = false;
3603 DEBUG(10,("create_file: access_mask = 0x%x "
3604 "file_attributes = 0x%x, share_access = 0x%x, "
3605 "create_disposition = 0x%x create_options = 0x%x "
3606 "oplock_request = 0x%x "
3607 "private_flags = 0x%x "
3608 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
3609 "fname = %s\n",
3610 (unsigned int)access_mask,
3611 (unsigned int)file_attributes,
3612 (unsigned int)share_access,
3613 (unsigned int)create_disposition,
3614 (unsigned int)create_options,
3615 (unsigned int)oplock_request,
3616 (unsigned int)private_flags,
3617 (unsigned int)root_dir_fid,
3618 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3621 * Calculate the filename from the root_dir_if if necessary.
3624 if (root_dir_fid != 0) {
3625 struct smb_filename *smb_fname_out = NULL;
3626 status = get_relative_fid_filename(conn, req, root_dir_fid,
3627 smb_fname, &smb_fname_out);
3628 if (!NT_STATUS_IS_OK(status)) {
3629 goto fail;
3631 smb_fname = smb_fname_out;
3635 * Check to see if this is a mac fork of some kind.
3638 stream_name = is_ntfs_stream_smb_fname(smb_fname);
3639 if (stream_name) {
3640 enum FAKE_FILE_TYPE fake_file_type;
3642 fake_file_type = is_fake_file(smb_fname);
3644 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
3647 * Here we go! support for changing the disk quotas
3648 * --metze
3650 * We need to fake up to open this MAGIC QUOTA file
3651 * and return a valid FID.
3653 * w2k close this file directly after openening xp
3654 * also tries a QUERY_FILE_INFO on the file and then
3655 * close it
3657 status = open_fake_file(req, conn, req->vuid,
3658 fake_file_type, smb_fname,
3659 access_mask, &fsp);
3660 if (!NT_STATUS_IS_OK(status)) {
3661 goto fail;
3664 ZERO_STRUCT(smb_fname->st);
3665 goto done;
3668 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
3669 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
3670 goto fail;
3674 /* All file access must go through check_name() */
3676 status = check_name(conn, smb_fname->base_name);
3677 if (!NT_STATUS_IS_OK(status)) {
3678 goto fail;
3681 if (stream_name && is_ntfs_default_stream_smb_fname(smb_fname)) {
3682 int ret;
3683 smb_fname->stream_name = NULL;
3684 /* We have to handle this error here. */
3685 if (create_options & FILE_DIRECTORY_FILE) {
3686 status = NT_STATUS_NOT_A_DIRECTORY;
3687 goto fail;
3689 if (lp_posix_pathnames()) {
3690 ret = SMB_VFS_LSTAT(conn, smb_fname);
3691 } else {
3692 ret = SMB_VFS_STAT(conn, smb_fname);
3695 if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
3696 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3697 goto fail;
3701 status = create_file_unixpath(
3702 conn, req, smb_fname, access_mask, share_access,
3703 create_disposition, create_options, file_attributes,
3704 oplock_request, allocation_size, private_flags,
3705 sd, ea_list,
3706 &fsp, &info);
3708 if (!NT_STATUS_IS_OK(status)) {
3709 goto fail;
3712 done:
3713 DEBUG(10, ("create_file: info=%d\n", info));
3715 *result = fsp;
3716 if (pinfo != NULL) {
3717 *pinfo = info;
3719 return NT_STATUS_OK;
3721 fail:
3722 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
3724 if (fsp != NULL) {
3725 close_file(req, fsp, ERROR_CLOSE);
3726 fsp = NULL;
3728 return status;