libcli/smb: add smb2cli_tcon_should_encrypt()
[Samba.git] / source3 / smbd / open.c
blobccea1e9d7183fb889ba5940adbabffebb94e1718
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 "../librpc/gen_ndr/open_files.h"
31 #include "../librpc/gen_ndr/idmap.h"
32 #include "../librpc/gen_ndr/ioctl.h"
33 #include "passdb/lookup_sid.h"
34 #include "auth.h"
35 #include "serverid.h"
36 #include "messages.h"
37 #include "source3/lib/dbwrap/dbwrap_watch.h"
39 extern const struct generic_mapping file_generic_mapping;
41 struct deferred_open_record {
42 bool delayed_for_oplocks;
43 bool async_open;
44 struct file_id id;
47 /****************************************************************************
48 If the requester wanted DELETE_ACCESS and was rejected because
49 the file ACL didn't include DELETE_ACCESS, see if the parent ACL
50 overrides this.
51 ****************************************************************************/
53 static bool parent_override_delete(connection_struct *conn,
54 const struct smb_filename *smb_fname,
55 uint32_t access_mask,
56 uint32_t rejected_mask)
58 if ((access_mask & DELETE_ACCESS) &&
59 (rejected_mask & DELETE_ACCESS) &&
60 can_delete_file_in_directory(conn, smb_fname)) {
61 return true;
63 return false;
66 /****************************************************************************
67 Check if we have open rights.
68 ****************************************************************************/
70 NTSTATUS smbd_check_access_rights(struct connection_struct *conn,
71 const struct smb_filename *smb_fname,
72 bool use_privs,
73 uint32_t access_mask)
75 /* Check if we have rights to open. */
76 NTSTATUS status;
77 struct security_descriptor *sd = NULL;
78 uint32_t rejected_share_access;
79 uint32_t rejected_mask = access_mask;
80 uint32_t do_not_check_mask = 0;
82 rejected_share_access = access_mask & ~(conn->share_access);
84 if (rejected_share_access) {
85 DEBUG(10, ("smbd_check_access_rights: rejected share access 0x%x "
86 "on %s (0x%x)\n",
87 (unsigned int)access_mask,
88 smb_fname_str_dbg(smb_fname),
89 (unsigned int)rejected_share_access ));
90 return NT_STATUS_ACCESS_DENIED;
93 if (!use_privs && get_current_uid(conn) == (uid_t)0) {
94 /* I'm sorry sir, I didn't know you were root... */
95 DEBUG(10,("smbd_check_access_rights: root override "
96 "on %s. Granting 0x%x\n",
97 smb_fname_str_dbg(smb_fname),
98 (unsigned int)access_mask ));
99 return NT_STATUS_OK;
102 if ((access_mask & DELETE_ACCESS) && !lp_acl_check_permissions(SNUM(conn))) {
103 DEBUG(10,("smbd_check_access_rights: not checking ACL "
104 "on DELETE_ACCESS on file %s. Granting 0x%x\n",
105 smb_fname_str_dbg(smb_fname),
106 (unsigned int)access_mask ));
107 return NT_STATUS_OK;
110 if (access_mask == DELETE_ACCESS &&
111 VALID_STAT(smb_fname->st) &&
112 S_ISLNK(smb_fname->st.st_ex_mode)) {
113 /* We can always delete a symlink. */
114 DEBUG(10,("smbd_check_access_rights: not checking ACL "
115 "on DELETE_ACCESS on symlink %s.\n",
116 smb_fname_str_dbg(smb_fname) ));
117 return NT_STATUS_OK;
120 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
121 (SECINFO_OWNER |
122 SECINFO_GROUP |
123 SECINFO_DACL), talloc_tos(), &sd);
125 if (!NT_STATUS_IS_OK(status)) {
126 DEBUG(10, ("smbd_check_access_rights: Could not get acl "
127 "on %s: %s\n",
128 smb_fname_str_dbg(smb_fname),
129 nt_errstr(status)));
131 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
132 goto access_denied;
135 return status;
139 * If we can access the path to this file, by
140 * default we have FILE_READ_ATTRIBUTES from the
141 * containing directory. See the section:
142 * "Algorithm to Check Access to an Existing File"
143 * in MS-FSA.pdf.
145 * se_file_access_check() also takes care of
146 * owner WRITE_DAC and READ_CONTROL.
148 do_not_check_mask = FILE_READ_ATTRIBUTES;
151 * Samba 3.6 and earlier granted execute access even
152 * if the ACL did not contain execute rights.
153 * Samba 4.0 is more correct and checks it.
154 * The compatibilty mode allows to skip this check
155 * to smoothen upgrades.
157 if (lp_acl_allow_execute_always(SNUM(conn))) {
158 do_not_check_mask |= FILE_EXECUTE;
161 status = se_file_access_check(sd,
162 get_current_nttok(conn),
163 use_privs,
164 (access_mask & ~do_not_check_mask),
165 &rejected_mask);
167 DEBUG(10,("smbd_check_access_rights: file %s requesting "
168 "0x%x returning 0x%x (%s)\n",
169 smb_fname_str_dbg(smb_fname),
170 (unsigned int)access_mask,
171 (unsigned int)rejected_mask,
172 nt_errstr(status) ));
174 if (!NT_STATUS_IS_OK(status)) {
175 if (DEBUGLEVEL >= 10) {
176 DEBUG(10,("smbd_check_access_rights: acl for %s is:\n",
177 smb_fname_str_dbg(smb_fname) ));
178 NDR_PRINT_DEBUG(security_descriptor, sd);
182 TALLOC_FREE(sd);
184 if (NT_STATUS_IS_OK(status) ||
185 !NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
186 return status;
189 /* Here we know status == NT_STATUS_ACCESS_DENIED. */
191 access_denied:
193 if ((access_mask & FILE_WRITE_ATTRIBUTES) &&
194 (rejected_mask & FILE_WRITE_ATTRIBUTES) &&
195 !lp_store_dos_attributes(SNUM(conn)) &&
196 (lp_map_readonly(SNUM(conn)) ||
197 lp_map_archive(SNUM(conn)) ||
198 lp_map_hidden(SNUM(conn)) ||
199 lp_map_system(SNUM(conn)))) {
200 rejected_mask &= ~FILE_WRITE_ATTRIBUTES;
202 DEBUG(10,("smbd_check_access_rights: "
203 "overrode "
204 "FILE_WRITE_ATTRIBUTES "
205 "on file %s\n",
206 smb_fname_str_dbg(smb_fname)));
209 if (parent_override_delete(conn,
210 smb_fname,
211 access_mask,
212 rejected_mask)) {
213 /* Were we trying to do an open
214 * for delete and didn't get DELETE
215 * access (only) ? Check if the
216 * directory allows DELETE_CHILD.
217 * See here:
218 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
219 * for details. */
221 rejected_mask &= ~DELETE_ACCESS;
223 DEBUG(10,("smbd_check_access_rights: "
224 "overrode "
225 "DELETE_ACCESS on "
226 "file %s\n",
227 smb_fname_str_dbg(smb_fname)));
230 if (rejected_mask != 0) {
231 return NT_STATUS_ACCESS_DENIED;
233 return NT_STATUS_OK;
236 static NTSTATUS check_parent_access(struct connection_struct *conn,
237 struct smb_filename *smb_fname,
238 uint32_t access_mask)
240 NTSTATUS status;
241 char *parent_dir = NULL;
242 struct security_descriptor *parent_sd = NULL;
243 uint32_t access_granted = 0;
245 if (!parent_dirname(talloc_tos(),
246 smb_fname->base_name,
247 &parent_dir,
248 NULL)) {
249 return NT_STATUS_NO_MEMORY;
252 if (get_current_uid(conn) == (uid_t)0) {
253 /* I'm sorry sir, I didn't know you were root... */
254 DEBUG(10,("check_parent_access: root override "
255 "on %s. Granting 0x%x\n",
256 smb_fname_str_dbg(smb_fname),
257 (unsigned int)access_mask ));
258 return NT_STATUS_OK;
261 status = SMB_VFS_GET_NT_ACL(conn,
262 parent_dir,
263 SECINFO_DACL,
264 talloc_tos(),
265 &parent_sd);
267 if (!NT_STATUS_IS_OK(status)) {
268 DEBUG(5,("check_parent_access: SMB_VFS_GET_NT_ACL failed for "
269 "%s with error %s\n",
270 parent_dir,
271 nt_errstr(status)));
272 return status;
276 * If we can access the path to this file, by
277 * default we have FILE_READ_ATTRIBUTES from the
278 * containing directory. See the section:
279 * "Algorithm to Check Access to an Existing File"
280 * in MS-FSA.pdf.
282 * se_file_access_check() also takes care of
283 * owner WRITE_DAC and READ_CONTROL.
285 status = se_file_access_check(parent_sd,
286 get_current_nttok(conn),
287 false,
288 (access_mask & ~FILE_READ_ATTRIBUTES),
289 &access_granted);
290 if(!NT_STATUS_IS_OK(status)) {
291 DEBUG(5,("check_parent_access: access check "
292 "on directory %s for "
293 "path %s for mask 0x%x returned (0x%x) %s\n",
294 parent_dir,
295 smb_fname->base_name,
296 access_mask,
297 access_granted,
298 nt_errstr(status) ));
299 return status;
302 return NT_STATUS_OK;
305 /****************************************************************************
306 Ensure when opening a base file for a stream open that we have permissions
307 to do so given the access mask on the base file.
308 ****************************************************************************/
310 static NTSTATUS check_base_file_access(struct connection_struct *conn,
311 struct smb_filename *smb_fname,
312 uint32_t access_mask)
314 NTSTATUS status;
316 status = smbd_calculate_access_mask(conn, smb_fname,
317 false,
318 access_mask,
319 &access_mask);
320 if (!NT_STATUS_IS_OK(status)) {
321 DEBUG(10, ("smbd_calculate_access_mask "
322 "on file %s returned %s\n",
323 smb_fname_str_dbg(smb_fname),
324 nt_errstr(status)));
325 return status;
328 if (access_mask & (FILE_WRITE_DATA|FILE_APPEND_DATA)) {
329 uint32_t dosattrs;
330 if (!CAN_WRITE(conn)) {
331 return NT_STATUS_ACCESS_DENIED;
333 dosattrs = dos_mode(conn, smb_fname);
334 if (IS_DOS_READONLY(dosattrs)) {
335 return NT_STATUS_ACCESS_DENIED;
339 return smbd_check_access_rights(conn,
340 smb_fname,
341 false,
342 access_mask);
345 /****************************************************************************
346 fd support routines - attempt to do a dos_open.
347 ****************************************************************************/
349 NTSTATUS fd_open(struct connection_struct *conn,
350 files_struct *fsp,
351 int flags,
352 mode_t mode)
354 struct smb_filename *smb_fname = fsp->fsp_name;
355 NTSTATUS status = NT_STATUS_OK;
357 #ifdef O_NOFOLLOW
359 * Never follow symlinks on a POSIX client. The
360 * client should be doing this.
363 if (fsp->posix_open || !lp_follow_symlinks(SNUM(conn))) {
364 flags |= O_NOFOLLOW;
366 #endif
368 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, mode);
369 if (fsp->fh->fd == -1) {
370 int posix_errno = errno;
371 #ifdef O_NOFOLLOW
372 #if defined(ENOTSUP) && defined(OSF1)
373 /* handle special Tru64 errno */
374 if (errno == ENOTSUP) {
375 posix_errno = ELOOP;
377 #endif /* ENOTSUP */
378 #ifdef EFTYPE
379 /* fix broken NetBSD errno */
380 if (errno == EFTYPE) {
381 posix_errno = ELOOP;
383 #endif /* EFTYPE */
384 /* fix broken FreeBSD errno */
385 if (errno == EMLINK) {
386 posix_errno = ELOOP;
388 #endif /* O_NOFOLLOW */
389 status = map_nt_error_from_unix(posix_errno);
390 if (errno == EMFILE) {
391 static time_t last_warned = 0L;
393 if (time((time_t *) NULL) > last_warned) {
394 DEBUG(0,("Too many open files, unable "
395 "to open more! smbd's max "
396 "open files = %d\n",
397 lp_max_open_files()));
398 last_warned = time((time_t *) NULL);
404 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
405 smb_fname_str_dbg(smb_fname), flags, (int)mode, fsp->fh->fd,
406 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
408 return status;
411 /****************************************************************************
412 Close the file associated with a fsp.
413 ****************************************************************************/
415 NTSTATUS fd_close(files_struct *fsp)
417 int ret;
419 if (fsp->dptr) {
420 dptr_CloseDir(fsp);
422 if (fsp->fh->fd == -1) {
423 return NT_STATUS_OK; /* What we used to call a stat open. */
425 if (fsp->fh->ref_count > 1) {
426 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
429 ret = SMB_VFS_CLOSE(fsp);
430 fsp->fh->fd = -1;
431 if (ret == -1) {
432 return map_nt_error_from_unix(errno);
434 return NT_STATUS_OK;
437 /****************************************************************************
438 Change the ownership of a file to that of the parent directory.
439 Do this by fd if possible.
440 ****************************************************************************/
442 void change_file_owner_to_parent(connection_struct *conn,
443 const char *inherit_from_dir,
444 files_struct *fsp)
446 struct smb_filename *smb_fname_parent;
447 int ret;
449 smb_fname_parent = synthetic_smb_fname(talloc_tos(), inherit_from_dir,
450 NULL, NULL);
451 if (smb_fname_parent == NULL) {
452 return;
455 ret = SMB_VFS_STAT(conn, smb_fname_parent);
456 if (ret == -1) {
457 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
458 "directory %s. Error was %s\n",
459 smb_fname_str_dbg(smb_fname_parent),
460 strerror(errno)));
461 TALLOC_FREE(smb_fname_parent);
462 return;
465 if (smb_fname_parent->st.st_ex_uid == fsp->fsp_name->st.st_ex_uid) {
466 /* Already this uid - no need to change. */
467 DEBUG(10,("change_file_owner_to_parent: file %s "
468 "is already owned by uid %d\n",
469 fsp_str_dbg(fsp),
470 (int)fsp->fsp_name->st.st_ex_uid ));
471 TALLOC_FREE(smb_fname_parent);
472 return;
475 become_root();
476 ret = SMB_VFS_FCHOWN(fsp, smb_fname_parent->st.st_ex_uid, (gid_t)-1);
477 unbecome_root();
478 if (ret == -1) {
479 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
480 "file %s to parent directory uid %u. Error "
481 "was %s\n", fsp_str_dbg(fsp),
482 (unsigned int)smb_fname_parent->st.st_ex_uid,
483 strerror(errno) ));
484 } else {
485 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
486 "parent directory uid %u.\n", fsp_str_dbg(fsp),
487 (unsigned int)smb_fname_parent->st.st_ex_uid));
488 /* Ensure the uid entry is updated. */
489 fsp->fsp_name->st.st_ex_uid = smb_fname_parent->st.st_ex_uid;
492 TALLOC_FREE(smb_fname_parent);
495 NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
496 const char *inherit_from_dir,
497 const char *fname,
498 SMB_STRUCT_STAT *psbuf)
500 struct smb_filename *smb_fname_parent;
501 struct smb_filename *smb_fname_cwd = NULL;
502 char *saved_dir = NULL;
503 TALLOC_CTX *ctx = talloc_tos();
504 NTSTATUS status = NT_STATUS_OK;
505 int ret;
507 smb_fname_parent = synthetic_smb_fname(ctx, inherit_from_dir,
508 NULL, NULL);
509 if (smb_fname_parent == NULL) {
510 return NT_STATUS_NO_MEMORY;
513 ret = SMB_VFS_STAT(conn, smb_fname_parent);
514 if (ret == -1) {
515 status = map_nt_error_from_unix(errno);
516 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
517 "directory %s. Error was %s\n",
518 smb_fname_str_dbg(smb_fname_parent),
519 strerror(errno)));
520 goto out;
523 /* We've already done an lstat into psbuf, and we know it's a
524 directory. If we can cd into the directory and the dev/ino
525 are the same then we can safely chown without races as
526 we're locking the directory in place by being in it. This
527 should work on any UNIX (thanks tridge :-). JRA.
530 saved_dir = vfs_GetWd(ctx,conn);
531 if (!saved_dir) {
532 status = map_nt_error_from_unix(errno);
533 DEBUG(0,("change_dir_owner_to_parent: failed to get "
534 "current working directory. Error was %s\n",
535 strerror(errno)));
536 goto out;
539 /* Chdir into the new path. */
540 if (vfs_ChDir(conn, fname) == -1) {
541 status = map_nt_error_from_unix(errno);
542 DEBUG(0,("change_dir_owner_to_parent: failed to change "
543 "current working directory to %s. Error "
544 "was %s\n", fname, strerror(errno) ));
545 goto chdir;
548 smb_fname_cwd = synthetic_smb_fname(ctx, ".", NULL, NULL);
549 if (smb_fname_cwd == NULL) {
550 status = NT_STATUS_NO_MEMORY;
551 goto chdir;
554 ret = SMB_VFS_STAT(conn, smb_fname_cwd);
555 if (ret == -1) {
556 status = map_nt_error_from_unix(errno);
557 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
558 "directory '.' (%s) Error was %s\n",
559 fname, strerror(errno)));
560 goto chdir;
563 /* Ensure we're pointing at the same place. */
564 if (smb_fname_cwd->st.st_ex_dev != psbuf->st_ex_dev ||
565 smb_fname_cwd->st.st_ex_ino != psbuf->st_ex_ino) {
566 DEBUG(0,("change_dir_owner_to_parent: "
567 "device/inode on directory %s changed. "
568 "Refusing to chown !\n", fname ));
569 status = NT_STATUS_ACCESS_DENIED;
570 goto chdir;
573 if (smb_fname_parent->st.st_ex_uid == smb_fname_cwd->st.st_ex_uid) {
574 /* Already this uid - no need to change. */
575 DEBUG(10,("change_dir_owner_to_parent: directory %s "
576 "is already owned by uid %d\n",
577 fname,
578 (int)smb_fname_cwd->st.st_ex_uid ));
579 status = NT_STATUS_OK;
580 goto chdir;
583 become_root();
584 ret = SMB_VFS_LCHOWN(conn, ".", smb_fname_parent->st.st_ex_uid,
585 (gid_t)-1);
586 unbecome_root();
587 if (ret == -1) {
588 status = map_nt_error_from_unix(errno);
589 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
590 "directory %s to parent directory uid %u. "
591 "Error was %s\n", fname,
592 (unsigned int)smb_fname_parent->st.st_ex_uid,
593 strerror(errno) ));
594 } else {
595 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
596 "directory %s to parent directory uid %u.\n",
597 fname, (unsigned int)smb_fname_parent->st.st_ex_uid ));
598 /* Ensure the uid entry is updated. */
599 psbuf->st_ex_uid = smb_fname_parent->st.st_ex_uid;
602 chdir:
603 vfs_ChDir(conn,saved_dir);
604 out:
605 TALLOC_FREE(smb_fname_parent);
606 TALLOC_FREE(smb_fname_cwd);
607 return status;
610 /****************************************************************************
611 Open a file - returning a guaranteed ATOMIC indication of if the
612 file was created or not.
613 ****************************************************************************/
615 static NTSTATUS fd_open_atomic(struct connection_struct *conn,
616 files_struct *fsp,
617 int flags,
618 mode_t mode,
619 bool *file_created)
621 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
622 bool file_existed = VALID_STAT(fsp->fsp_name->st);
624 *file_created = false;
626 if (!(flags & O_CREAT)) {
628 * We're not creating the file, just pass through.
630 return fd_open(conn, fsp, flags, mode);
633 if (flags & O_EXCL) {
635 * Fail if already exists, just pass through.
637 status = fd_open(conn, fsp, flags, mode);
640 * Here we've opened with O_CREAT|O_EXCL. If that went
641 * NT_STATUS_OK, we *know* we created this file.
643 *file_created = NT_STATUS_IS_OK(status);
645 return status;
649 * Now it gets tricky. We have O_CREAT, but not O_EXCL.
650 * To know absolutely if we created the file or not,
651 * we can never call O_CREAT without O_EXCL. So if
652 * we think the file existed, try without O_CREAT|O_EXCL.
653 * If we think the file didn't exist, try with
654 * O_CREAT|O_EXCL. Keep bouncing between these two
655 * requests until either the file is created, or
656 * opened. Either way, we keep going until we get
657 * a returnable result (error, or open/create).
660 while(1) {
661 int curr_flags = flags;
663 if (file_existed) {
664 /* Just try open, do not create. */
665 curr_flags &= ~(O_CREAT);
666 status = fd_open(conn, fsp, curr_flags, mode);
667 if (NT_STATUS_EQUAL(status,
668 NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
670 * Someone deleted it in the meantime.
671 * Retry with O_EXCL.
673 file_existed = false;
674 DEBUG(10,("fd_open_atomic: file %s existed. "
675 "Retry.\n",
676 smb_fname_str_dbg(fsp->fsp_name)));
677 continue;
679 } else {
680 /* Try create exclusively, fail if it exists. */
681 curr_flags |= O_EXCL;
682 status = fd_open(conn, fsp, curr_flags, mode);
683 if (NT_STATUS_EQUAL(status,
684 NT_STATUS_OBJECT_NAME_COLLISION)) {
686 * Someone created it in the meantime.
687 * Retry without O_CREAT.
689 file_existed = true;
690 DEBUG(10,("fd_open_atomic: file %s "
691 "did not exist. Retry.\n",
692 smb_fname_str_dbg(fsp->fsp_name)));
693 continue;
695 if (NT_STATUS_IS_OK(status)) {
697 * Here we've opened with O_CREAT|O_EXCL
698 * and got success. We *know* we created
699 * this file.
701 *file_created = true;
704 /* Create is done, or failed. */
705 break;
707 return status;
710 /****************************************************************************
711 Open a file.
712 ****************************************************************************/
714 static NTSTATUS open_file(files_struct *fsp,
715 connection_struct *conn,
716 struct smb_request *req,
717 const char *parent_dir,
718 int flags,
719 mode_t unx_mode,
720 uint32 access_mask, /* client requested access mask. */
721 uint32 open_access_mask, /* what we're actually using in the open. */
722 bool *p_file_created)
724 struct smb_filename *smb_fname = fsp->fsp_name;
725 NTSTATUS status = NT_STATUS_OK;
726 int accmode = (flags & O_ACCMODE);
727 int local_flags = flags;
728 bool file_existed = VALID_STAT(fsp->fsp_name->st);
730 fsp->fh->fd = -1;
731 errno = EPERM;
733 /* Check permissions */
736 * This code was changed after seeing a client open request
737 * containing the open mode of (DENY_WRITE/read-only) with
738 * the 'create if not exist' bit set. The previous code
739 * would fail to open the file read only on a read-only share
740 * as it was checking the flags parameter directly against O_RDONLY,
741 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
742 * JRA.
745 if (!CAN_WRITE(conn)) {
746 /* It's a read-only share - fail if we wanted to write. */
747 if(accmode != O_RDONLY || (flags & O_TRUNC) || (flags & O_APPEND)) {
748 DEBUG(3,("Permission denied opening %s\n",
749 smb_fname_str_dbg(smb_fname)));
750 return NT_STATUS_ACCESS_DENIED;
752 if (flags & O_CREAT) {
753 /* We don't want to write - but we must make sure that
754 O_CREAT doesn't create the file if we have write
755 access into the directory.
757 flags &= ~(O_CREAT|O_EXCL);
758 local_flags &= ~(O_CREAT|O_EXCL);
763 * This little piece of insanity is inspired by the
764 * fact that an NT client can open a file for O_RDONLY,
765 * but set the create disposition to FILE_EXISTS_TRUNCATE.
766 * If the client *can* write to the file, then it expects to
767 * truncate the file, even though it is opening for readonly.
768 * Quicken uses this stupid trick in backup file creation...
769 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
770 * for helping track this one down. It didn't bite us in 2.0.x
771 * as we always opened files read-write in that release. JRA.
774 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
775 DEBUG(10,("open_file: truncate requested on read-only open "
776 "for file %s\n", smb_fname_str_dbg(smb_fname)));
777 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
780 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
781 (!file_existed && (local_flags & O_CREAT)) ||
782 ((local_flags & O_TRUNC) == O_TRUNC) ) {
783 const char *wild;
784 int ret;
786 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
788 * We would block on opening a FIFO with no one else on the
789 * other end. Do what we used to do and add O_NONBLOCK to the
790 * open flags. JRA.
793 if (file_existed && S_ISFIFO(smb_fname->st.st_ex_mode)) {
794 local_flags &= ~O_TRUNC; /* Can't truncate a FIFO. */
795 local_flags |= O_NONBLOCK;
797 #endif
799 /* Don't create files with Microsoft wildcard characters. */
800 if (fsp->base_fsp) {
802 * wildcard characters are allowed in stream names
803 * only test the basefilename
805 wild = fsp->base_fsp->fsp_name->base_name;
806 } else {
807 wild = smb_fname->base_name;
809 if ((local_flags & O_CREAT) && !file_existed &&
810 ms_has_wild(wild)) {
811 return NT_STATUS_OBJECT_NAME_INVALID;
814 /* Can we access this file ? */
815 if (!fsp->base_fsp) {
816 /* Only do this check on non-stream open. */
817 if (file_existed) {
818 status = smbd_check_access_rights(conn,
819 smb_fname,
820 false,
821 access_mask);
823 if (!NT_STATUS_IS_OK(status)) {
824 DEBUG(10, ("open_file: "
825 "smbd_check_access_rights "
826 "on file %s returned %s\n",
827 smb_fname_str_dbg(smb_fname),
828 nt_errstr(status)));
831 if (!NT_STATUS_IS_OK(status) &&
832 !NT_STATUS_EQUAL(status,
833 NT_STATUS_OBJECT_NAME_NOT_FOUND))
835 return status;
838 if (NT_STATUS_EQUAL(status,
839 NT_STATUS_OBJECT_NAME_NOT_FOUND))
841 DEBUG(10, ("open_file: "
842 "file %s vanished since we "
843 "checked for existence.\n",
844 smb_fname_str_dbg(smb_fname)));
845 file_existed = false;
846 SET_STAT_INVALID(fsp->fsp_name->st);
850 if (!file_existed) {
851 if (!(local_flags & O_CREAT)) {
852 /* File didn't exist and no O_CREAT. */
853 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
856 status = check_parent_access(conn,
857 smb_fname,
858 SEC_DIR_ADD_FILE);
859 if (!NT_STATUS_IS_OK(status)) {
860 DEBUG(10, ("open_file: "
861 "check_parent_access on "
862 "file %s returned %s\n",
863 smb_fname_str_dbg(smb_fname),
864 nt_errstr(status) ));
865 return status;
871 * Actually do the open - if O_TRUNC is needed handle it
872 * below under the share mode lock.
874 status = fd_open_atomic(conn, fsp, local_flags & ~O_TRUNC,
875 unx_mode, p_file_created);
876 if (!NT_STATUS_IS_OK(status)) {
877 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
878 "(flags=%d)\n", smb_fname_str_dbg(smb_fname),
879 nt_errstr(status),local_flags,flags));
880 return status;
883 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
884 if (ret == -1) {
885 /* If we have an fd, this stat should succeed. */
886 DEBUG(0,("Error doing fstat on open file %s "
887 "(%s)\n",
888 smb_fname_str_dbg(smb_fname),
889 strerror(errno) ));
890 status = map_nt_error_from_unix(errno);
891 fd_close(fsp);
892 return status;
895 if (*p_file_created) {
896 /* We created this file. */
898 bool need_re_stat = false;
899 /* Do all inheritance work after we've
900 done a successful fstat call and filled
901 in the stat struct in fsp->fsp_name. */
903 /* Inherit the ACL if required */
904 if (lp_inherit_permissions(SNUM(conn))) {
905 inherit_access_posix_acl(conn, parent_dir,
906 smb_fname->base_name,
907 unx_mode);
908 need_re_stat = true;
911 /* Change the owner if required. */
912 if (lp_inherit_owner(SNUM(conn))) {
913 change_file_owner_to_parent(conn, parent_dir,
914 fsp);
915 need_re_stat = true;
918 if (need_re_stat) {
919 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
920 /* If we have an fd, this stat should succeed. */
921 if (ret == -1) {
922 DEBUG(0,("Error doing fstat on open file %s "
923 "(%s)\n",
924 smb_fname_str_dbg(smb_fname),
925 strerror(errno) ));
929 notify_fname(conn, NOTIFY_ACTION_ADDED,
930 FILE_NOTIFY_CHANGE_FILE_NAME,
931 smb_fname->base_name);
933 } else {
934 fsp->fh->fd = -1; /* What we used to call a stat open. */
935 if (!file_existed) {
936 /* File must exist for a stat open. */
937 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
940 status = smbd_check_access_rights(conn,
941 smb_fname,
942 false,
943 access_mask);
945 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
946 fsp->posix_open &&
947 S_ISLNK(smb_fname->st.st_ex_mode)) {
948 /* This is a POSIX stat open for delete
949 * or rename on a symlink that points
950 * nowhere. Allow. */
951 DEBUG(10,("open_file: allowing POSIX "
952 "open on bad symlink %s\n",
953 smb_fname_str_dbg(smb_fname)));
954 status = NT_STATUS_OK;
957 if (!NT_STATUS_IS_OK(status)) {
958 DEBUG(10,("open_file: "
959 "smbd_check_access_rights on file "
960 "%s returned %s\n",
961 smb_fname_str_dbg(smb_fname),
962 nt_errstr(status) ));
963 return status;
968 * POSIX allows read-only opens of directories. We don't
969 * want to do this (we use a different code path for this)
970 * so catch a directory open and return an EISDIR. JRA.
973 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
974 fd_close(fsp);
975 errno = EISDIR;
976 return NT_STATUS_FILE_IS_A_DIRECTORY;
979 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
980 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
981 fsp->file_pid = req ? req->smbpid : 0;
982 fsp->can_lock = True;
983 fsp->can_read = ((access_mask & FILE_READ_DATA) != 0);
984 fsp->can_write =
985 CAN_WRITE(conn) &&
986 ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) != 0);
987 fsp->print_file = NULL;
988 fsp->modified = False;
989 fsp->sent_oplock_break = NO_BREAK_SENT;
990 fsp->is_directory = False;
991 if (conn->aio_write_behind_list &&
992 is_in_path(smb_fname->base_name, conn->aio_write_behind_list,
993 conn->case_sensitive)) {
994 fsp->aio_write_behind = True;
997 fsp->wcp = NULL; /* Write cache pointer. */
999 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
1000 conn->session_info->unix_info->unix_name,
1001 smb_fname_str_dbg(smb_fname),
1002 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
1003 conn->num_files_open));
1005 errno = 0;
1006 return NT_STATUS_OK;
1009 /****************************************************************************
1010 Check if we can open a file with a share mode.
1011 Returns True if conflict, False if not.
1012 ****************************************************************************/
1014 static bool share_conflict(struct share_mode_entry *entry,
1015 uint32 access_mask,
1016 uint32 share_access)
1018 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
1019 "entry->share_access = 0x%x, "
1020 "entry->private_options = 0x%x\n",
1021 (unsigned int)entry->access_mask,
1022 (unsigned int)entry->share_access,
1023 (unsigned int)entry->private_options));
1025 if (server_id_is_disconnected(&entry->pid)) {
1027 * note: cleanup should have been done by
1028 * delay_for_batch_oplocks()
1030 return false;
1033 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
1034 (unsigned int)access_mask, (unsigned int)share_access));
1036 if ((entry->access_mask & (FILE_WRITE_DATA|
1037 FILE_APPEND_DATA|
1038 FILE_READ_DATA|
1039 FILE_EXECUTE|
1040 DELETE_ACCESS)) == 0) {
1041 DEBUG(10,("share_conflict: No conflict due to "
1042 "entry->access_mask = 0x%x\n",
1043 (unsigned int)entry->access_mask ));
1044 return False;
1047 if ((access_mask & (FILE_WRITE_DATA|
1048 FILE_APPEND_DATA|
1049 FILE_READ_DATA|
1050 FILE_EXECUTE|
1051 DELETE_ACCESS)) == 0) {
1052 DEBUG(10,("share_conflict: No conflict due to "
1053 "access_mask = 0x%x\n",
1054 (unsigned int)access_mask ));
1055 return False;
1058 #if 1 /* JRA TEST - Superdebug. */
1059 #define CHECK_MASK(num, am, right, sa, share) \
1060 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
1061 (unsigned int)(num), (unsigned int)(am), \
1062 (unsigned int)(right), (unsigned int)(am)&(right) )); \
1063 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
1064 (unsigned int)(num), (unsigned int)(sa), \
1065 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
1066 if (((am) & (right)) && !((sa) & (share))) { \
1067 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
1068 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
1069 (unsigned int)(share) )); \
1070 return True; \
1072 #else
1073 #define CHECK_MASK(num, am, right, sa, share) \
1074 if (((am) & (right)) && !((sa) & (share))) { \
1075 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
1076 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
1077 (unsigned int)(share) )); \
1078 return True; \
1080 #endif
1082 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
1083 share_access, FILE_SHARE_WRITE);
1084 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
1085 entry->share_access, FILE_SHARE_WRITE);
1087 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
1088 share_access, FILE_SHARE_READ);
1089 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
1090 entry->share_access, FILE_SHARE_READ);
1092 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
1093 share_access, FILE_SHARE_DELETE);
1094 CHECK_MASK(6, access_mask, DELETE_ACCESS,
1095 entry->share_access, FILE_SHARE_DELETE);
1097 DEBUG(10,("share_conflict: No conflict.\n"));
1098 return False;
1101 #if defined(DEVELOPER)
1102 static void validate_my_share_entries(struct smbd_server_connection *sconn,
1103 int num,
1104 struct share_mode_entry *share_entry)
1106 struct server_id self = messaging_server_id(sconn->msg_ctx);
1107 files_struct *fsp;
1109 if (!serverid_equal(&self, &share_entry->pid)) {
1110 return;
1113 if (share_entry->op_mid == 0) {
1114 /* INTERNAL_OPEN_ONLY */
1115 return;
1118 if (!is_valid_share_mode_entry(share_entry)) {
1119 return;
1122 fsp = file_find_dif(sconn, share_entry->id,
1123 share_entry->share_file_id);
1124 if (!fsp) {
1125 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1126 share_mode_str(talloc_tos(), num, share_entry) ));
1127 smb_panic("validate_my_share_entries: Cannot match a "
1128 "share entry with an open file\n");
1131 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
1132 goto panic;
1135 return;
1137 panic:
1139 char *str;
1140 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1141 share_mode_str(talloc_tos(), num, share_entry) ));
1142 str = talloc_asprintf(talloc_tos(),
1143 "validate_my_share_entries: "
1144 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
1145 fsp->fsp_name->base_name,
1146 (unsigned int)fsp->oplock_type,
1147 (unsigned int)share_entry->op_type );
1148 smb_panic(str);
1151 #endif
1153 bool is_stat_open(uint32 access_mask)
1155 const uint32_t stat_open_bits =
1156 (SYNCHRONIZE_ACCESS|
1157 FILE_READ_ATTRIBUTES|
1158 FILE_WRITE_ATTRIBUTES);
1160 return (((access_mask & stat_open_bits) != 0) &&
1161 ((access_mask & ~stat_open_bits) == 0));
1164 static bool has_delete_on_close(struct share_mode_lock *lck,
1165 uint32_t name_hash)
1167 struct share_mode_data *d = lck->data;
1168 uint32_t i;
1170 if (d->num_share_modes == 0) {
1171 return false;
1173 if (!is_delete_on_close_set(lck, name_hash)) {
1174 return false;
1176 for (i=0; i<d->num_share_modes; i++) {
1177 if (!share_mode_stale_pid(d, i)) {
1178 return true;
1181 return false;
1184 /****************************************************************************
1185 Deal with share modes
1186 Invariant: Share mode must be locked on entry and exit.
1187 Returns -1 on error, or number of share modes on success (may be zero).
1188 ****************************************************************************/
1190 static NTSTATUS open_mode_check(connection_struct *conn,
1191 struct share_mode_lock *lck,
1192 uint32 access_mask,
1193 uint32 share_access)
1195 int i;
1197 if(lck->data->num_share_modes == 0) {
1198 return NT_STATUS_OK;
1201 if (is_stat_open(access_mask)) {
1202 /* Stat open that doesn't trigger oplock breaks or share mode
1203 * checks... ! JRA. */
1204 return NT_STATUS_OK;
1208 * Check if the share modes will give us access.
1211 #if defined(DEVELOPER)
1212 for(i = 0; i < lck->data->num_share_modes; i++) {
1213 validate_my_share_entries(conn->sconn, i,
1214 &lck->data->share_modes[i]);
1216 #endif
1218 /* Now we check the share modes, after any oplock breaks. */
1219 for(i = 0; i < lck->data->num_share_modes; i++) {
1221 if (!is_valid_share_mode_entry(&lck->data->share_modes[i])) {
1222 continue;
1225 /* someone else has a share lock on it, check to see if we can
1226 * too */
1227 if (share_conflict(&lck->data->share_modes[i],
1228 access_mask, share_access)) {
1230 if (share_mode_stale_pid(lck->data, i)) {
1231 continue;
1234 return NT_STATUS_SHARING_VIOLATION;
1238 return NT_STATUS_OK;
1242 * Send a break message to the oplock holder and delay the open for
1243 * our client.
1246 static NTSTATUS send_break_message(struct messaging_context *msg_ctx,
1247 const struct share_mode_entry *exclusive,
1248 uint16_t break_to)
1250 NTSTATUS status;
1251 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
1253 DEBUG(10, ("Sending break request to PID %s\n",
1254 procid_str_static(&exclusive->pid)));
1256 /* Create the message. */
1257 share_mode_entry_to_message(msg, exclusive);
1259 /* Overload entry->op_type */
1260 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET, break_to);
1262 status = messaging_send_buf(msg_ctx, exclusive->pid,
1263 MSG_SMB_BREAK_REQUEST,
1264 (uint8 *)msg, sizeof(msg));
1265 if (!NT_STATUS_IS_OK(status)) {
1266 DEBUG(3, ("Could not send oplock break message: %s\n",
1267 nt_errstr(status)));
1270 return status;
1274 * Do internal consistency checks on the share mode for a file.
1277 static bool validate_oplock_types(struct share_mode_lock *lck)
1279 struct share_mode_data *d = lck->data;
1280 bool batch = false;
1281 bool ex_or_batch = false;
1282 bool level2 = false;
1283 bool no_oplock = false;
1284 uint32_t num_non_stat_opens = 0;
1285 uint32_t i;
1287 for (i=0; i<d->num_share_modes; i++) {
1288 struct share_mode_entry *e = &d->share_modes[i];
1290 if (!is_valid_share_mode_entry(e)) {
1291 continue;
1294 if (e->op_mid == 0) {
1295 /* INTERNAL_OPEN_ONLY */
1296 continue;
1299 if (e->op_type == NO_OPLOCK && is_stat_open(e->access_mask)) {
1300 /* We ignore stat opens in the table - they
1301 always have NO_OPLOCK and never get or
1302 cause breaks. JRA. */
1303 continue;
1306 num_non_stat_opens += 1;
1308 if (BATCH_OPLOCK_TYPE(e->op_type)) {
1309 /* batch - can only be one. */
1310 if (share_mode_stale_pid(d, i)) {
1311 DEBUG(10, ("Found stale batch oplock\n"));
1312 continue;
1314 if (ex_or_batch || batch || level2 || no_oplock) {
1315 DEBUG(0, ("Bad batch oplock entry %u.",
1316 (unsigned)i));
1317 return false;
1319 batch = true;
1322 if (EXCLUSIVE_OPLOCK_TYPE(e->op_type)) {
1323 if (share_mode_stale_pid(d, i)) {
1324 DEBUG(10, ("Found stale duplicate oplock\n"));
1325 continue;
1327 /* Exclusive or batch - can only be one. */
1328 if (ex_or_batch || level2 || no_oplock) {
1329 DEBUG(0, ("Bad exclusive or batch oplock "
1330 "entry %u.", (unsigned)i));
1331 return false;
1333 ex_or_batch = true;
1336 if (LEVEL_II_OPLOCK_TYPE(e->op_type)) {
1337 if (batch || ex_or_batch) {
1338 if (share_mode_stale_pid(d, i)) {
1339 DEBUG(10, ("Found stale LevelII "
1340 "oplock\n"));
1341 continue;
1343 DEBUG(0, ("Bad levelII oplock entry %u.",
1344 (unsigned)i));
1345 return false;
1347 level2 = true;
1350 if (e->op_type == NO_OPLOCK) {
1351 if (batch || ex_or_batch) {
1352 if (share_mode_stale_pid(d, i)) {
1353 DEBUG(10, ("Found stale NO_OPLOCK "
1354 "entry\n"));
1355 continue;
1357 DEBUG(0, ("Bad no oplock entry %u.",
1358 (unsigned)i));
1359 return false;
1361 no_oplock = true;
1365 remove_stale_share_mode_entries(d);
1367 if ((batch || ex_or_batch) && (num_non_stat_opens != 1)) {
1368 DEBUG(1, ("got batch (%d) or ex (%d) non-exclusively (%d)\n",
1369 (int)batch, (int)ex_or_batch,
1370 (int)d->num_share_modes));
1371 return false;
1374 return true;
1377 static bool delay_for_oplock(files_struct *fsp,
1378 int oplock_request,
1379 struct share_mode_lock *lck,
1380 bool have_sharing_violation,
1381 uint32_t create_disposition)
1383 struct share_mode_data *d = lck->data;
1384 struct share_mode_entry *entry;
1385 uint32_t num_non_stat_opens = 0;
1386 uint32_t i;
1387 uint16_t break_to;
1389 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1390 return false;
1392 for (i=0; i<d->num_share_modes; i++) {
1393 struct share_mode_entry *e = &d->share_modes[i];
1394 if (e->op_type == NO_OPLOCK && is_stat_open(e->access_mask)) {
1395 continue;
1397 num_non_stat_opens += 1;
1400 * We found the a non-stat open, which in the exclusive/batch
1401 * case will be inspected further down.
1403 entry = e;
1405 if (num_non_stat_opens == 0) {
1407 * Nothing to wait for around
1409 return false;
1411 if (num_non_stat_opens != 1) {
1413 * More than one open around. There can't be any exclusive or
1414 * batch left, this is all level2.
1416 return false;
1419 if (server_id_is_disconnected(&entry->pid)) {
1421 * TODO: clean up.
1422 * This could be achieved by sending a break message
1423 * to ourselves. Special considerations for files
1424 * with delete_on_close flag set!
1426 * For now we keep it simple and do not
1427 * allow delete on close for durable handles.
1429 return false;
1432 switch (create_disposition) {
1433 case FILE_SUPERSEDE:
1434 case FILE_OVERWRITE_IF:
1435 break_to = NO_OPLOCK;
1436 break;
1437 default:
1438 break_to = LEVEL_II_OPLOCK;
1439 break;
1442 if (have_sharing_violation && (entry->op_type & BATCH_OPLOCK)) {
1443 if (share_mode_stale_pid(d, 0)) {
1444 return false;
1446 send_break_message(fsp->conn->sconn->msg_ctx, entry, break_to);
1447 return true;
1449 if (have_sharing_violation) {
1451 * Non-batch exclusive is not broken if we have a sharing
1452 * violation
1454 return false;
1456 if (LEVEL_II_OPLOCK_TYPE(entry->op_type) &&
1457 (break_to == NO_OPLOCK)) {
1458 if (share_mode_stale_pid(d, 0)) {
1459 return false;
1461 DEBUG(10, ("Asynchronously breaking level2 oplock for "
1462 "create_disposition=%u\n",
1463 (unsigned)create_disposition));
1464 send_break_message(fsp->conn->sconn->msg_ctx, entry, break_to);
1465 return false;
1467 if (!EXCLUSIVE_OPLOCK_TYPE(entry->op_type)) {
1469 * No break for NO_OPLOCK or LEVEL2_OPLOCK oplocks
1471 return false;
1473 if (share_mode_stale_pid(d, 0)) {
1474 return false;
1477 send_break_message(fsp->conn->sconn->msg_ctx, entry, break_to);
1478 return true;
1481 static bool file_has_brlocks(files_struct *fsp)
1483 struct byte_range_lock *br_lck;
1485 br_lck = brl_get_locks_readonly(fsp);
1486 if (!br_lck)
1487 return false;
1489 return (brl_num_locks(br_lck) > 0);
1492 static void grant_fsp_oplock_type(files_struct *fsp,
1493 struct share_mode_lock *lck,
1494 int oplock_request)
1496 bool allow_level2 = (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
1497 lp_level2_oplocks(SNUM(fsp->conn));
1498 bool got_level2_oplock, got_a_none_oplock;
1499 uint32_t i;
1501 /* Start by granting what the client asked for,
1502 but ensure no SAMBA_PRIVATE bits can be set. */
1503 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1505 if (oplock_request & INTERNAL_OPEN_ONLY) {
1506 /* No oplocks on internal open. */
1507 fsp->oplock_type = NO_OPLOCK;
1508 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1509 fsp->oplock_type, fsp_str_dbg(fsp)));
1510 return;
1513 if (lp_locking(fsp->conn->params) && file_has_brlocks(fsp)) {
1514 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1515 fsp_str_dbg(fsp)));
1516 fsp->oplock_type = NO_OPLOCK;
1519 if (is_stat_open(fsp->access_mask)) {
1520 /* Leave the value already set. */
1521 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1522 fsp->oplock_type, fsp_str_dbg(fsp)));
1523 return;
1526 got_level2_oplock = false;
1527 got_a_none_oplock = false;
1529 for (i=0; i<lck->data->num_share_modes; i++) {
1530 int op_type = lck->data->share_modes[i].op_type;
1532 if (LEVEL_II_OPLOCK_TYPE(op_type)) {
1533 got_level2_oplock = true;
1535 if (op_type == NO_OPLOCK) {
1536 got_a_none_oplock = true;
1541 * Match what was requested (fsp->oplock_type) with
1542 * what was found in the existing share modes.
1545 if (got_level2_oplock || got_a_none_oplock) {
1546 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1547 fsp->oplock_type = LEVEL_II_OPLOCK;
1552 * Don't grant level2 to clients that don't want them
1553 * or if we've turned them off.
1555 if (fsp->oplock_type == LEVEL_II_OPLOCK && !allow_level2) {
1556 fsp->oplock_type = NO_OPLOCK;
1559 if (fsp->oplock_type == LEVEL_II_OPLOCK && !got_level2_oplock) {
1561 * We're the first level2 oplock. Indicate that in brlock.tdb.
1563 struct byte_range_lock *brl;
1565 brl = brl_get_locks(talloc_tos(), fsp);
1566 if (brl != NULL) {
1567 brl_set_have_read_oplocks(brl, true);
1568 TALLOC_FREE(brl);
1572 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1573 fsp->oplock_type, fsp_str_dbg(fsp)));
1576 static bool request_timed_out(struct timeval request_time,
1577 struct timeval timeout)
1579 struct timeval now, end_time;
1580 GetTimeOfDay(&now);
1581 end_time = timeval_sum(&request_time, &timeout);
1582 return (timeval_compare(&end_time, &now) < 0);
1585 struct defer_open_state {
1586 struct smbXsrv_connection *xconn;
1587 uint64_t mid;
1590 static void defer_open_done(struct tevent_req *req);
1592 /****************************************************************************
1593 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1594 ****************************************************************************/
1596 static void defer_open(struct share_mode_lock *lck,
1597 struct timeval request_time,
1598 struct timeval timeout,
1599 struct smb_request *req,
1600 struct deferred_open_record *state)
1602 struct deferred_open_record *open_rec;
1604 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1605 "open entry for mid %llu\n",
1606 (unsigned int)request_time.tv_sec,
1607 (unsigned int)request_time.tv_usec,
1608 (unsigned long long)req->mid));
1610 open_rec = talloc(NULL, struct deferred_open_record);
1611 if (open_rec == NULL) {
1612 TALLOC_FREE(lck);
1613 exit_server("talloc failed");
1616 *open_rec = *state;
1618 if (lck) {
1619 struct defer_open_state *watch_state;
1620 struct tevent_req *watch_req;
1621 bool ret;
1623 watch_state = talloc(open_rec, struct defer_open_state);
1624 if (watch_state == NULL) {
1625 exit_server("talloc failed");
1627 watch_state->xconn = req->xconn;
1628 watch_state->mid = req->mid;
1630 DEBUG(10, ("defering mid %llu\n",
1631 (unsigned long long)req->mid));
1633 watch_req = dbwrap_record_watch_send(
1634 watch_state, req->sconn->ev_ctx, lck->data->record,
1635 req->sconn->msg_ctx);
1636 if (watch_req == NULL) {
1637 exit_server("Could not watch share mode record");
1639 tevent_req_set_callback(watch_req, defer_open_done,
1640 watch_state);
1642 ret = tevent_req_set_endtime(
1643 watch_req, req->sconn->ev_ctx,
1644 timeval_sum(&request_time, &timeout));
1645 SMB_ASSERT(ret);
1648 if (!push_deferred_open_message_smb(req, request_time, timeout,
1649 state->id, open_rec)) {
1650 TALLOC_FREE(lck);
1651 exit_server("push_deferred_open_message_smb failed");
1655 static void defer_open_done(struct tevent_req *req)
1657 struct defer_open_state *state = tevent_req_callback_data(
1658 req, struct defer_open_state);
1659 NTSTATUS status;
1660 bool ret;
1662 status = dbwrap_record_watch_recv(req, talloc_tos(), NULL);
1663 TALLOC_FREE(req);
1664 if (!NT_STATUS_IS_OK(status)) {
1665 DEBUG(5, ("dbwrap_record_watch_recv returned %s\n",
1666 nt_errstr(status)));
1668 * Even if it failed, retry anyway. TODO: We need a way to
1669 * tell a re-scheduled open about that error.
1673 DEBUG(10, ("scheduling mid %llu\n", (unsigned long long)state->mid));
1675 ret = schedule_deferred_open_message_smb(state->xconn, state->mid);
1676 SMB_ASSERT(ret);
1677 TALLOC_FREE(state);
1681 /****************************************************************************
1682 On overwrite open ensure that the attributes match.
1683 ****************************************************************************/
1685 static bool open_match_attributes(connection_struct *conn,
1686 uint32 old_dos_attr,
1687 uint32 new_dos_attr,
1688 mode_t existing_unx_mode,
1689 mode_t new_unx_mode,
1690 mode_t *returned_unx_mode)
1692 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
1694 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1695 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1697 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
1698 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
1699 *returned_unx_mode = new_unx_mode;
1700 } else {
1701 *returned_unx_mode = (mode_t)0;
1704 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1705 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1706 "returned_unx_mode = 0%o\n",
1707 (unsigned int)old_dos_attr,
1708 (unsigned int)existing_unx_mode,
1709 (unsigned int)new_dos_attr,
1710 (unsigned int)*returned_unx_mode ));
1712 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1713 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1714 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
1715 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
1716 return False;
1719 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1720 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
1721 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
1722 return False;
1725 return True;
1728 /****************************************************************************
1729 Special FCB or DOS processing in the case of a sharing violation.
1730 Try and find a duplicated file handle.
1731 ****************************************************************************/
1733 static NTSTATUS fcb_or_dos_open(struct smb_request *req,
1734 connection_struct *conn,
1735 files_struct *fsp_to_dup_into,
1736 const struct smb_filename *smb_fname,
1737 struct file_id id,
1738 uint16 file_pid,
1739 uint64_t vuid,
1740 uint32 access_mask,
1741 uint32 share_access,
1742 uint32 create_options)
1744 files_struct *fsp;
1746 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1747 "file %s.\n", smb_fname_str_dbg(smb_fname)));
1749 for(fsp = file_find_di_first(conn->sconn, id); fsp;
1750 fsp = file_find_di_next(fsp)) {
1752 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1753 "vuid = %llu, file_pid = %u, private_options = 0x%x "
1754 "access_mask = 0x%x\n", fsp_str_dbg(fsp),
1755 fsp->fh->fd, (unsigned long long)fsp->vuid,
1756 (unsigned int)fsp->file_pid,
1757 (unsigned int)fsp->fh->private_options,
1758 (unsigned int)fsp->access_mask ));
1760 if (fsp != fsp_to_dup_into &&
1761 fsp->fh->fd != -1 &&
1762 fsp->vuid == vuid &&
1763 fsp->file_pid == file_pid &&
1764 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
1765 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
1766 (fsp->access_mask & FILE_WRITE_DATA) &&
1767 strequal(fsp->fsp_name->base_name, smb_fname->base_name) &&
1768 strequal(fsp->fsp_name->stream_name,
1769 smb_fname->stream_name)) {
1770 DEBUG(10,("fcb_or_dos_open: file match\n"));
1771 break;
1775 if (!fsp) {
1776 return NT_STATUS_NOT_FOUND;
1779 /* quite an insane set of semantics ... */
1780 if (is_executable(smb_fname->base_name) &&
1781 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1782 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1783 return NT_STATUS_INVALID_PARAMETER;
1786 /* We need to duplicate this fsp. */
1787 return dup_file_fsp(req, fsp, access_mask, share_access,
1788 create_options, fsp_to_dup_into);
1791 static void schedule_defer_open(struct share_mode_lock *lck,
1792 struct file_id id,
1793 struct timeval request_time,
1794 struct smb_request *req)
1796 struct deferred_open_record state;
1798 /* This is a relative time, added to the absolute
1799 request_time value to get the absolute timeout time.
1800 Note that if this is the second or greater time we enter
1801 this codepath for this particular request mid then
1802 request_time is left as the absolute time of the *first*
1803 time this request mid was processed. This is what allows
1804 the request to eventually time out. */
1806 struct timeval timeout;
1808 /* Normally the smbd we asked should respond within
1809 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1810 * the client did, give twice the timeout as a safety
1811 * measure here in case the other smbd is stuck
1812 * somewhere else. */
1814 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1816 /* Nothing actually uses state.delayed_for_oplocks
1817 but it's handy to differentiate in debug messages
1818 between a 30 second delay due to oplock break, and
1819 a 1 second delay for share mode conflicts. */
1821 state.delayed_for_oplocks = True;
1822 state.async_open = false;
1823 state.id = id;
1825 if (!request_timed_out(request_time, timeout)) {
1826 defer_open(lck, request_time, timeout, req, &state);
1830 /****************************************************************************
1831 Reschedule an open call that went asynchronous.
1832 ****************************************************************************/
1834 static void schedule_async_open(struct timeval request_time,
1835 struct smb_request *req)
1837 struct deferred_open_record state;
1838 struct timeval timeout;
1840 timeout = timeval_set(20, 0);
1842 ZERO_STRUCT(state);
1843 state.delayed_for_oplocks = false;
1844 state.async_open = true;
1846 if (!request_timed_out(request_time, timeout)) {
1847 defer_open(NULL, request_time, timeout, req, &state);
1851 /****************************************************************************
1852 Work out what access_mask to use from what the client sent us.
1853 ****************************************************************************/
1855 static NTSTATUS smbd_calculate_maximum_allowed_access(
1856 connection_struct *conn,
1857 const struct smb_filename *smb_fname,
1858 bool use_privs,
1859 uint32_t *p_access_mask)
1861 struct security_descriptor *sd;
1862 uint32_t access_granted;
1863 NTSTATUS status;
1865 if (!use_privs && (get_current_uid(conn) == (uid_t)0)) {
1866 *p_access_mask |= FILE_GENERIC_ALL;
1867 return NT_STATUS_OK;
1870 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
1871 (SECINFO_OWNER |
1872 SECINFO_GROUP |
1873 SECINFO_DACL),
1874 talloc_tos(), &sd);
1876 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1878 * File did not exist
1880 *p_access_mask = FILE_GENERIC_ALL;
1881 return NT_STATUS_OK;
1883 if (!NT_STATUS_IS_OK(status)) {
1884 DEBUG(10,("Could not get acl on file %s: %s\n",
1885 smb_fname_str_dbg(smb_fname),
1886 nt_errstr(status)));
1887 return NT_STATUS_ACCESS_DENIED;
1891 * If we can access the path to this file, by
1892 * default we have FILE_READ_ATTRIBUTES from the
1893 * containing directory. See the section:
1894 * "Algorithm to Check Access to an Existing File"
1895 * in MS-FSA.pdf.
1897 * se_file_access_check()
1898 * also takes care of owner WRITE_DAC and READ_CONTROL.
1900 status = se_file_access_check(sd,
1901 get_current_nttok(conn),
1902 use_privs,
1903 (*p_access_mask & ~FILE_READ_ATTRIBUTES),
1904 &access_granted);
1906 TALLOC_FREE(sd);
1908 if (!NT_STATUS_IS_OK(status)) {
1909 DEBUG(10, ("Access denied on file %s: "
1910 "when calculating maximum access\n",
1911 smb_fname_str_dbg(smb_fname)));
1912 return NT_STATUS_ACCESS_DENIED;
1914 *p_access_mask = (access_granted | FILE_READ_ATTRIBUTES);
1916 if (!(access_granted & DELETE_ACCESS)) {
1917 if (can_delete_file_in_directory(conn, smb_fname)) {
1918 *p_access_mask |= DELETE_ACCESS;
1922 return NT_STATUS_OK;
1925 NTSTATUS smbd_calculate_access_mask(connection_struct *conn,
1926 const struct smb_filename *smb_fname,
1927 bool use_privs,
1928 uint32_t access_mask,
1929 uint32_t *access_mask_out)
1931 NTSTATUS status;
1932 uint32_t orig_access_mask = access_mask;
1933 uint32_t rejected_share_access;
1936 * Convert GENERIC bits to specific bits.
1939 se_map_generic(&access_mask, &file_generic_mapping);
1941 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1942 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1944 status = smbd_calculate_maximum_allowed_access(
1945 conn, smb_fname, use_privs, &access_mask);
1947 if (!NT_STATUS_IS_OK(status)) {
1948 return status;
1951 access_mask &= conn->share_access;
1954 rejected_share_access = access_mask & ~(conn->share_access);
1956 if (rejected_share_access) {
1957 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
1958 "file %s: rejected by share access mask[0x%08X] "
1959 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
1960 smb_fname_str_dbg(smb_fname),
1961 conn->share_access,
1962 orig_access_mask, access_mask,
1963 rejected_share_access));
1964 return NT_STATUS_ACCESS_DENIED;
1967 *access_mask_out = access_mask;
1968 return NT_STATUS_OK;
1971 /****************************************************************************
1972 Remove the deferred open entry under lock.
1973 ****************************************************************************/
1975 /****************************************************************************
1976 Return true if this is a state pointer to an asynchronous create.
1977 ****************************************************************************/
1979 bool is_deferred_open_async(const struct deferred_open_record *rec)
1981 return rec->async_open;
1984 static bool clear_ads(uint32_t create_disposition)
1986 bool ret = false;
1988 switch (create_disposition) {
1989 case FILE_SUPERSEDE:
1990 case FILE_OVERWRITE_IF:
1991 case FILE_OVERWRITE:
1992 ret = true;
1993 break;
1994 default:
1995 break;
1997 return ret;
2000 static int disposition_to_open_flags(uint32_t create_disposition)
2002 int ret = 0;
2005 * Currently we're using FILE_SUPERSEDE as the same as
2006 * FILE_OVERWRITE_IF but they really are
2007 * different. FILE_SUPERSEDE deletes an existing file
2008 * (requiring delete access) then recreates it.
2011 switch (create_disposition) {
2012 case FILE_SUPERSEDE:
2013 case FILE_OVERWRITE_IF:
2015 * If file exists replace/overwrite. If file doesn't
2016 * exist create.
2018 ret = O_CREAT|O_TRUNC;
2019 break;
2021 case FILE_OPEN:
2023 * If file exists open. If file doesn't exist error.
2025 ret = 0;
2026 break;
2028 case FILE_OVERWRITE:
2030 * If file exists overwrite. If file doesn't exist
2031 * error.
2033 ret = O_TRUNC;
2034 break;
2036 case FILE_CREATE:
2038 * If file exists error. If file doesn't exist create.
2040 ret = O_CREAT|O_EXCL;
2041 break;
2043 case FILE_OPEN_IF:
2045 * If file exists open. If file doesn't exist create.
2047 ret = O_CREAT;
2048 break;
2050 return ret;
2053 static int calculate_open_access_flags(uint32_t access_mask,
2054 int oplock_request,
2055 uint32_t private_flags)
2057 bool need_write, need_read;
2060 * Note that we ignore the append flag as append does not
2061 * mean the same thing under DOS and Unix.
2064 need_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA));
2065 if (!need_write) {
2066 return O_RDONLY;
2069 /* DENY_DOS opens are always underlying read-write on the
2070 file handle, no matter what the requested access mask
2071 says. */
2073 need_read =
2074 ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
2075 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|
2076 FILE_READ_EA|FILE_EXECUTE));
2078 if (!need_read) {
2079 return O_WRONLY;
2081 return O_RDWR;
2084 /****************************************************************************
2085 Open a file with a share mode. Passed in an already created files_struct *.
2086 ****************************************************************************/
2088 static NTSTATUS open_file_ntcreate(connection_struct *conn,
2089 struct smb_request *req,
2090 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
2091 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
2092 uint32 create_disposition, /* FILE_OPEN_IF etc. */
2093 uint32 create_options, /* options such as delete on close. */
2094 uint32 new_dos_attributes, /* attributes used for new file. */
2095 int oplock_request, /* internal Samba oplock codes. */
2096 struct smb2_lease *lease,
2097 /* Information (FILE_EXISTS etc.) */
2098 uint32_t private_flags, /* Samba specific flags. */
2099 int *pinfo,
2100 files_struct *fsp)
2102 struct smb_filename *smb_fname = fsp->fsp_name;
2103 int flags=0;
2104 int flags2=0;
2105 bool file_existed = VALID_STAT(smb_fname->st);
2106 bool def_acl = False;
2107 bool posix_open = False;
2108 bool new_file_created = False;
2109 bool first_open_attempt = true;
2110 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
2111 mode_t new_unx_mode = (mode_t)0;
2112 mode_t unx_mode = (mode_t)0;
2113 int info;
2114 uint32 existing_dos_attributes = 0;
2115 struct timeval request_time = timeval_zero();
2116 struct share_mode_lock *lck = NULL;
2117 uint32 open_access_mask = access_mask;
2118 NTSTATUS status;
2119 char *parent_dir;
2120 SMB_STRUCT_STAT saved_stat = smb_fname->st;
2121 struct timespec old_write_time;
2122 struct file_id id;
2124 if (conn->printer) {
2126 * Printers are handled completely differently.
2127 * Most of the passed parameters are ignored.
2130 if (pinfo) {
2131 *pinfo = FILE_WAS_CREATED;
2134 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
2135 smb_fname_str_dbg(smb_fname)));
2137 if (!req) {
2138 DEBUG(0,("open_file_ntcreate: printer open without "
2139 "an SMB request!\n"));
2140 return NT_STATUS_INTERNAL_ERROR;
2143 return print_spool_open(fsp, smb_fname->base_name,
2144 req->vuid);
2147 if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
2148 NULL)) {
2149 return NT_STATUS_NO_MEMORY;
2152 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2153 posix_open = True;
2154 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2155 new_dos_attributes = 0;
2156 } else {
2157 /* Windows allows a new file to be created and
2158 silently removes a FILE_ATTRIBUTE_DIRECTORY
2159 sent by the client. Do the same. */
2161 new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
2163 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
2164 * created new. */
2165 unx_mode = unix_mode(conn, new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2166 smb_fname, parent_dir);
2169 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
2170 "access_mask=0x%x share_access=0x%x "
2171 "create_disposition = 0x%x create_options=0x%x "
2172 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
2173 smb_fname_str_dbg(smb_fname), new_dos_attributes,
2174 access_mask, share_access, create_disposition,
2175 create_options, (unsigned int)unx_mode, oplock_request,
2176 (unsigned int)private_flags));
2178 if (req == NULL) {
2179 /* Ensure req == NULL means INTERNAL_OPEN_ONLY */
2180 SMB_ASSERT(((oplock_request & INTERNAL_OPEN_ONLY) != 0));
2181 } else {
2182 /* And req != NULL means no INTERNAL_OPEN_ONLY */
2183 SMB_ASSERT(((oplock_request & INTERNAL_OPEN_ONLY) == 0));
2187 * Only non-internal opens can be deferred at all
2190 if (req) {
2191 struct deferred_open_record *open_rec;
2192 if (get_deferred_open_message_state(req,
2193 &request_time,
2194 &open_rec)) {
2195 /* Remember the absolute time of the original
2196 request with this mid. We'll use it later to
2197 see if this has timed out. */
2199 /* If it was an async create retry, the file
2200 didn't exist. */
2202 if (is_deferred_open_async(open_rec)) {
2203 SET_STAT_INVALID(smb_fname->st);
2204 file_existed = false;
2207 /* Ensure we don't reprocess this message. */
2208 remove_deferred_open_message_smb(req->xconn, req->mid);
2210 first_open_attempt = false;
2214 if (!posix_open) {
2215 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
2216 if (file_existed) {
2217 existing_dos_attributes = dos_mode(conn, smb_fname);
2221 /* ignore any oplock requests if oplocks are disabled */
2222 if (!lp_oplocks(SNUM(conn)) ||
2223 IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
2224 /* Mask off everything except the private Samba bits. */
2225 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
2228 /* this is for OS/2 long file names - say we don't support them */
2229 if (!lp_posix_pathnames() && strstr(smb_fname->base_name,".+,;=[].")) {
2230 /* OS/2 Workplace shell fix may be main code stream in a later
2231 * release. */
2232 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
2233 "supported.\n"));
2234 if (use_nt_status()) {
2235 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2237 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
2240 switch( create_disposition ) {
2241 case FILE_OPEN:
2242 /* If file exists open. If file doesn't exist error. */
2243 if (!file_existed) {
2244 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
2245 "requested for file %s and file "
2246 "doesn't exist.\n",
2247 smb_fname_str_dbg(smb_fname)));
2248 errno = ENOENT;
2249 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2251 break;
2253 case FILE_OVERWRITE:
2254 /* If file exists overwrite. If file doesn't exist
2255 * error. */
2256 if (!file_existed) {
2257 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
2258 "requested for file %s and file "
2259 "doesn't exist.\n",
2260 smb_fname_str_dbg(smb_fname) ));
2261 errno = ENOENT;
2262 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2264 break;
2266 case FILE_CREATE:
2267 /* If file exists error. If file doesn't exist
2268 * create. */
2269 if (file_existed) {
2270 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
2271 "requested for file %s and file "
2272 "already exists.\n",
2273 smb_fname_str_dbg(smb_fname)));
2274 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
2275 errno = EISDIR;
2276 } else {
2277 errno = EEXIST;
2279 return map_nt_error_from_unix(errno);
2281 break;
2283 case FILE_SUPERSEDE:
2284 case FILE_OVERWRITE_IF:
2285 case FILE_OPEN_IF:
2286 break;
2287 default:
2288 return NT_STATUS_INVALID_PARAMETER;
2291 flags2 = disposition_to_open_flags(create_disposition);
2293 /* We only care about matching attributes on file exists and
2294 * overwrite. */
2296 if (!posix_open && file_existed &&
2297 ((create_disposition == FILE_OVERWRITE) ||
2298 (create_disposition == FILE_OVERWRITE_IF))) {
2299 if (!open_match_attributes(conn, existing_dos_attributes,
2300 new_dos_attributes,
2301 smb_fname->st.st_ex_mode,
2302 unx_mode, &new_unx_mode)) {
2303 DEBUG(5,("open_file_ntcreate: attributes missmatch "
2304 "for file %s (%x %x) (0%o, 0%o)\n",
2305 smb_fname_str_dbg(smb_fname),
2306 existing_dos_attributes,
2307 new_dos_attributes,
2308 (unsigned int)smb_fname->st.st_ex_mode,
2309 (unsigned int)unx_mode ));
2310 errno = EACCES;
2311 return NT_STATUS_ACCESS_DENIED;
2315 status = smbd_calculate_access_mask(conn, smb_fname,
2316 false,
2317 access_mask,
2318 &access_mask);
2319 if (!NT_STATUS_IS_OK(status)) {
2320 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
2321 "on file %s returned %s\n",
2322 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
2323 return status;
2326 open_access_mask = access_mask;
2328 if (flags2 & O_TRUNC) {
2329 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
2332 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
2333 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
2334 access_mask));
2337 * Note that we ignore the append flag as append does not
2338 * mean the same thing under DOS and Unix.
2341 flags = calculate_open_access_flags(access_mask, oplock_request,
2342 private_flags);
2345 * Currently we only look at FILE_WRITE_THROUGH for create options.
2348 #if defined(O_SYNC)
2349 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
2350 flags2 |= O_SYNC;
2352 #endif /* O_SYNC */
2354 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
2355 flags2 |= O_APPEND;
2358 if (!posix_open && !CAN_WRITE(conn)) {
2360 * We should really return a permission denied error if either
2361 * O_CREAT or O_TRUNC are set, but for compatibility with
2362 * older versions of Samba we just AND them out.
2364 flags2 &= ~(O_CREAT|O_TRUNC);
2367 if (first_open_attempt && lp_kernel_oplocks(SNUM(conn))) {
2369 * With kernel oplocks the open breaking an oplock
2370 * blocks until the oplock holder has given up the
2371 * oplock or closed the file. We prevent this by first
2372 * trying to open the file with O_NONBLOCK (see "man
2373 * fcntl" on Linux). For the second try, triggered by
2374 * an oplock break response, we do not need this
2375 * anymore.
2377 * This is true under the assumption that only Samba
2378 * requests kernel oplocks. Once someone else like
2379 * NFSv4 starts to use that API, we will have to
2380 * modify this by communicating with the NFSv4 server.
2382 flags2 |= O_NONBLOCK;
2386 * Ensure we can't write on a read-only share or file.
2389 if (flags != O_RDONLY && file_existed &&
2390 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
2391 DEBUG(5,("open_file_ntcreate: write access requested for "
2392 "file %s on read only %s\n",
2393 smb_fname_str_dbg(smb_fname),
2394 !CAN_WRITE(conn) ? "share" : "file" ));
2395 errno = EACCES;
2396 return NT_STATUS_ACCESS_DENIED;
2399 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
2400 fsp->share_access = share_access;
2401 fsp->fh->private_options = private_flags;
2402 fsp->access_mask = open_access_mask; /* We change this to the
2403 * requested access_mask after
2404 * the open is done. */
2405 fsp->posix_open = posix_open;
2407 /* Ensure no SAMBA_PRIVATE bits can be set. */
2408 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
2410 if (timeval_is_zero(&request_time)) {
2411 request_time = fsp->open_time;
2415 * Ensure we pay attention to default ACLs on directories if required.
2418 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
2419 (def_acl = directory_has_default_acl(conn, parent_dir))) {
2420 unx_mode = (0777 & lp_create_mask(SNUM(conn)));
2423 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2424 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2425 (unsigned int)flags, (unsigned int)flags2,
2426 (unsigned int)unx_mode, (unsigned int)access_mask,
2427 (unsigned int)open_access_mask));
2429 fsp_open = open_file(fsp, conn, req, parent_dir,
2430 flags|flags2, unx_mode, access_mask,
2431 open_access_mask, &new_file_created);
2433 if (NT_STATUS_EQUAL(fsp_open, NT_STATUS_NETWORK_BUSY)) {
2434 struct deferred_open_record state;
2437 * EWOULDBLOCK/EAGAIN maps to NETWORK_BUSY.
2439 if (file_existed && S_ISFIFO(fsp->fsp_name->st.st_ex_mode)) {
2440 DEBUG(10, ("FIFO busy\n"));
2441 return NT_STATUS_NETWORK_BUSY;
2443 if (req == NULL) {
2444 DEBUG(10, ("Internal open busy\n"));
2445 return NT_STATUS_NETWORK_BUSY;
2449 * From here on we assume this is an oplock break triggered
2452 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
2453 if (lck == NULL) {
2454 state.delayed_for_oplocks = false;
2455 state.async_open = false;
2456 state.id = fsp->file_id;
2457 defer_open(NULL, request_time, timeval_set(0, 0),
2458 req, &state);
2459 DEBUG(10, ("No share mode lock found after "
2460 "EWOULDBLOCK, retrying sync\n"));
2461 return NT_STATUS_SHARING_VIOLATION;
2464 if (!validate_oplock_types(lck)) {
2465 smb_panic("validate_oplock_types failed");
2468 if (delay_for_oplock(fsp, 0, lck, false, create_disposition)) {
2469 schedule_defer_open(lck, fsp->file_id, request_time, req);
2470 TALLOC_FREE(lck);
2471 DEBUG(10, ("Sent oplock break request to kernel "
2472 "oplock holder\n"));
2473 return NT_STATUS_SHARING_VIOLATION;
2477 * No oplock from Samba around. Immediately retry with
2478 * a blocking open.
2480 state.delayed_for_oplocks = false;
2481 state.async_open = false;
2482 state.id = fsp->file_id;
2483 defer_open(lck, request_time, timeval_set(0, 0), req, &state);
2484 TALLOC_FREE(lck);
2485 DEBUG(10, ("No Samba oplock around after EWOULDBLOCK. "
2486 "Retrying sync\n"));
2487 return NT_STATUS_SHARING_VIOLATION;
2490 if (!NT_STATUS_IS_OK(fsp_open)) {
2491 if (NT_STATUS_EQUAL(fsp_open, NT_STATUS_RETRY)) {
2492 schedule_async_open(request_time, req);
2494 return fsp_open;
2497 if (new_file_created) {
2499 * As we atomically create using O_CREAT|O_EXCL,
2500 * then if new_file_created is true, then
2501 * file_existed *MUST* have been false (even
2502 * if the file was previously detected as being
2503 * there).
2505 file_existed = false;
2508 if (file_existed && !check_same_dev_ino(&saved_stat, &smb_fname->st)) {
2510 * The file did exist, but some other (local or NFS)
2511 * process either renamed/unlinked and re-created the
2512 * file with different dev/ino after we walked the path,
2513 * but before we did the open. We could retry the
2514 * open but it's a rare enough case it's easier to
2515 * just fail the open to prevent creating any problems
2516 * in the open file db having the wrong dev/ino key.
2518 fd_close(fsp);
2519 DEBUG(1,("open_file_ntcreate: file %s - dev/ino mismatch. "
2520 "Old (dev=0x%llu, ino =0x%llu). "
2521 "New (dev=0x%llu, ino=0x%llu). Failing open "
2522 " with NT_STATUS_ACCESS_DENIED.\n",
2523 smb_fname_str_dbg(smb_fname),
2524 (unsigned long long)saved_stat.st_ex_dev,
2525 (unsigned long long)saved_stat.st_ex_ino,
2526 (unsigned long long)smb_fname->st.st_ex_dev,
2527 (unsigned long long)smb_fname->st.st_ex_ino));
2528 return NT_STATUS_ACCESS_DENIED;
2531 old_write_time = smb_fname->st.st_ex_mtime;
2534 * Deal with the race condition where two smbd's detect the
2535 * file doesn't exist and do the create at the same time. One
2536 * of them will win and set a share mode, the other (ie. this
2537 * one) should check if the requested share mode for this
2538 * create is allowed.
2542 * Now the file exists and fsp is successfully opened,
2543 * fsp->dev and fsp->inode are valid and should replace the
2544 * dev=0,inode=0 from a non existent file. Spotted by
2545 * Nadav Danieli <nadavd@exanet.com>. JRA.
2548 id = fsp->file_id;
2550 lck = get_share_mode_lock(talloc_tos(), id,
2551 conn->connectpath,
2552 smb_fname, &old_write_time);
2554 if (lck == NULL) {
2555 DEBUG(0, ("open_file_ntcreate: Could not get share "
2556 "mode lock for %s\n",
2557 smb_fname_str_dbg(smb_fname)));
2558 fd_close(fsp);
2559 return NT_STATUS_SHARING_VIOLATION;
2562 /* Get the types we need to examine. */
2563 if (!validate_oplock_types(lck)) {
2564 smb_panic("validate_oplock_types failed");
2567 if (has_delete_on_close(lck, fsp->name_hash)) {
2568 TALLOC_FREE(lck);
2569 fd_close(fsp);
2570 return NT_STATUS_DELETE_PENDING;
2573 status = open_mode_check(conn, lck,
2574 access_mask, share_access);
2576 if (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION) ||
2577 (lck->data->num_share_modes > 0)) {
2579 * This comes from ancient times out of open_mode_check. I
2580 * have no clue whether this is still necessary. I can't think
2581 * of a case where this would actually matter further down in
2582 * this function. I leave it here for further investigation
2583 * :-)
2585 file_existed = true;
2588 if ((req != NULL) &&
2589 delay_for_oplock(
2590 fsp, oplock_request, lck,
2591 NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION),
2592 create_disposition)) {
2593 schedule_defer_open(lck, fsp->file_id, request_time, req);
2594 TALLOC_FREE(lck);
2595 fd_close(fsp);
2596 return NT_STATUS_SHARING_VIOLATION;
2599 if (!NT_STATUS_IS_OK(status)) {
2600 uint32 can_access_mask;
2601 bool can_access = True;
2603 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
2605 /* Check if this can be done with the deny_dos and fcb
2606 * calls. */
2607 if (private_flags &
2608 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
2609 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
2610 if (req == NULL) {
2611 DEBUG(0, ("DOS open without an SMB "
2612 "request!\n"));
2613 TALLOC_FREE(lck);
2614 fd_close(fsp);
2615 return NT_STATUS_INTERNAL_ERROR;
2618 /* Use the client requested access mask here,
2619 * not the one we open with. */
2620 status = fcb_or_dos_open(req,
2621 conn,
2622 fsp,
2623 smb_fname,
2625 req->smbpid,
2626 req->vuid,
2627 access_mask,
2628 share_access,
2629 create_options);
2631 if (NT_STATUS_IS_OK(status)) {
2632 TALLOC_FREE(lck);
2633 if (pinfo) {
2634 *pinfo = FILE_WAS_OPENED;
2636 return NT_STATUS_OK;
2641 * This next line is a subtlety we need for
2642 * MS-Access. If a file open will fail due to share
2643 * permissions and also for security (access) reasons,
2644 * we need to return the access failed error, not the
2645 * share error. We can't open the file due to kernel
2646 * oplock deadlock (it's possible we failed above on
2647 * the open_mode_check()) so use a userspace check.
2650 if (flags & O_RDWR) {
2651 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
2652 } else if (flags & O_WRONLY) {
2653 can_access_mask = FILE_WRITE_DATA;
2654 } else {
2655 can_access_mask = FILE_READ_DATA;
2658 if (((can_access_mask & FILE_WRITE_DATA) &&
2659 !CAN_WRITE(conn)) ||
2660 !NT_STATUS_IS_OK(smbd_check_access_rights(conn,
2661 smb_fname,
2662 false,
2663 can_access_mask))) {
2664 can_access = False;
2668 * If we're returning a share violation, ensure we
2669 * cope with the braindead 1 second delay (SMB1 only).
2672 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
2673 !conn->sconn->using_smb2 &&
2674 lp_defer_sharing_violations()) {
2675 struct timeval timeout;
2676 struct deferred_open_record state;
2677 int timeout_usecs;
2679 /* this is a hack to speed up torture tests
2680 in 'make test' */
2681 timeout_usecs = lp_parm_int(SNUM(conn),
2682 "smbd","sharedelay",
2683 SHARING_VIOLATION_USEC_WAIT);
2685 /* This is a relative time, added to the absolute
2686 request_time value to get the absolute timeout time.
2687 Note that if this is the second or greater time we enter
2688 this codepath for this particular request mid then
2689 request_time is left as the absolute time of the *first*
2690 time this request mid was processed. This is what allows
2691 the request to eventually time out. */
2693 timeout = timeval_set(0, timeout_usecs);
2695 /* Nothing actually uses state.delayed_for_oplocks
2696 but it's handy to differentiate in debug messages
2697 between a 30 second delay due to oplock break, and
2698 a 1 second delay for share mode conflicts. */
2700 state.delayed_for_oplocks = False;
2701 state.async_open = false;
2702 state.id = id;
2704 if ((req != NULL)
2705 && !request_timed_out(request_time,
2706 timeout)) {
2707 defer_open(lck, request_time, timeout,
2708 req, &state);
2712 TALLOC_FREE(lck);
2713 fd_close(fsp);
2714 if (can_access) {
2716 * We have detected a sharing violation here
2717 * so return the correct error code
2719 status = NT_STATUS_SHARING_VIOLATION;
2720 } else {
2721 status = NT_STATUS_ACCESS_DENIED;
2723 return status;
2726 /* Should we atomically (to the client at least) truncate ? */
2727 if ((!new_file_created) &&
2728 (flags2 & O_TRUNC) &&
2729 (!S_ISFIFO(fsp->fsp_name->st.st_ex_mode))) {
2730 int ret;
2732 ret = vfs_set_filelen(fsp, 0);
2733 if (ret != 0) {
2734 status = map_nt_error_from_unix(errno);
2735 TALLOC_FREE(lck);
2736 fd_close(fsp);
2737 return status;
2741 grant_fsp_oplock_type(fsp, lck, oplock_request);
2744 * We have the share entry *locked*.....
2747 /* Delete streams if create_disposition requires it */
2748 if (!new_file_created && clear_ads(create_disposition) &&
2749 !is_ntfs_stream_smb_fname(smb_fname)) {
2750 status = delete_all_streams(conn, smb_fname->base_name);
2751 if (!NT_STATUS_IS_OK(status)) {
2752 TALLOC_FREE(lck);
2753 fd_close(fsp);
2754 return status;
2758 /* note that we ignore failure for the following. It is
2759 basically a hack for NFS, and NFS will never set one of
2760 these only read them. Nobody but Samba can ever set a deny
2761 mode and we have already checked our more authoritative
2762 locking database for permission to set this deny mode. If
2763 the kernel refuses the operations then the kernel is wrong.
2764 note that GPFS supports it as well - jmcd */
2766 if (fsp->fh->fd != -1 && lp_kernel_share_modes(SNUM(conn))) {
2767 int ret_flock;
2768 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
2769 if(ret_flock == -1 ){
2771 TALLOC_FREE(lck);
2772 fd_close(fsp);
2774 return NT_STATUS_SHARING_VIOLATION;
2779 * At this point onwards, we can guarantee that the share entry
2780 * is locked, whether we created the file or not, and that the
2781 * deny mode is compatible with all current opens.
2785 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2786 * but we don't have to store this - just ignore it on access check.
2788 if (conn->sconn->using_smb2) {
2790 * SMB2 doesn't return it (according to Microsoft tests).
2791 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2792 * File created with access = 0x7 (Read, Write, Delete)
2793 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2795 fsp->access_mask = access_mask;
2796 } else {
2797 /* But SMB1 does. */
2798 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2801 if (file_existed) {
2802 /* stat opens on existing files don't get oplocks. */
2803 if (is_stat_open(open_access_mask)) {
2804 fsp->oplock_type = NO_OPLOCK;
2808 if (new_file_created) {
2809 info = FILE_WAS_CREATED;
2810 } else {
2811 if (flags2 & O_TRUNC) {
2812 info = FILE_WAS_OVERWRITTEN;
2813 } else {
2814 info = FILE_WAS_OPENED;
2818 if (pinfo) {
2819 *pinfo = info;
2823 * Setup the oplock info in both the shared memory and
2824 * file structs.
2827 status = set_file_oplock(fsp);
2828 if (!NT_STATUS_IS_OK(status)) {
2830 * Could not get the kernel oplock
2832 fsp->oplock_type = NO_OPLOCK;
2835 if (!set_share_mode(lck, fsp, get_current_uid(conn),
2836 req ? req->mid : 0,
2837 fsp->oplock_type)) {
2838 TALLOC_FREE(lck);
2839 fd_close(fsp);
2840 return NT_STATUS_NO_MEMORY;
2843 /* Handle strange delete on close create semantics. */
2844 if (create_options & FILE_DELETE_ON_CLOSE) {
2846 status = can_set_delete_on_close(fsp, new_dos_attributes);
2848 if (!NT_STATUS_IS_OK(status)) {
2849 /* Remember to delete the mode we just added. */
2850 del_share_mode(lck, fsp);
2851 TALLOC_FREE(lck);
2852 fd_close(fsp);
2853 return status;
2855 /* Note that here we set the *inital* delete on close flag,
2856 not the regular one. The magic gets handled in close. */
2857 fsp->initial_delete_on_close = True;
2860 if (info != FILE_WAS_OPENED) {
2861 /* Files should be initially set as archive */
2862 if (lp_map_archive(SNUM(conn)) ||
2863 lp_store_dos_attributes(SNUM(conn))) {
2864 if (!posix_open) {
2865 if (file_set_dosmode(conn, smb_fname,
2866 new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2867 parent_dir, true) == 0) {
2868 unx_mode = smb_fname->st.st_ex_mode;
2874 /* Determine sparse flag. */
2875 if (posix_open) {
2876 /* POSIX opens are sparse by default. */
2877 fsp->is_sparse = true;
2878 } else {
2879 fsp->is_sparse = (file_existed &&
2880 (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE));
2884 * Take care of inherited ACLs on created files - if default ACL not
2885 * selected.
2888 if (!posix_open && new_file_created && !def_acl) {
2890 int saved_errno = errno; /* We might get ENOSYS in the next
2891 * call.. */
2893 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2894 errno == ENOSYS) {
2895 errno = saved_errno; /* Ignore ENOSYS */
2898 } else if (new_unx_mode) {
2900 int ret = -1;
2902 /* Attributes need changing. File already existed. */
2905 int saved_errno = errno; /* We might get ENOSYS in the
2906 * next call.. */
2907 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2909 if (ret == -1 && errno == ENOSYS) {
2910 errno = saved_errno; /* Ignore ENOSYS */
2911 } else {
2912 DEBUG(5, ("open_file_ntcreate: reset "
2913 "attributes of file %s to 0%o\n",
2914 smb_fname_str_dbg(smb_fname),
2915 (unsigned int)new_unx_mode));
2916 ret = 0; /* Don't do the fchmod below. */
2920 if ((ret == -1) &&
2921 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2922 DEBUG(5, ("open_file_ntcreate: failed to reset "
2923 "attributes of file %s to 0%o\n",
2924 smb_fname_str_dbg(smb_fname),
2925 (unsigned int)new_unx_mode));
2930 * Deal with other opens having a modified write time.
2932 struct timespec write_time = get_share_mode_write_time(lck);
2934 if (!null_timespec(write_time)) {
2935 update_stat_ex_mtime(&fsp->fsp_name->st, write_time);
2939 TALLOC_FREE(lck);
2941 return NT_STATUS_OK;
2944 static NTSTATUS mkdir_internal(connection_struct *conn,
2945 struct smb_filename *smb_dname,
2946 uint32 file_attributes)
2948 mode_t mode;
2949 char *parent_dir = NULL;
2950 NTSTATUS status;
2951 bool posix_open = false;
2952 bool need_re_stat = false;
2953 uint32_t access_mask = SEC_DIR_ADD_SUBDIR;
2955 if (!CAN_WRITE(conn) || (access_mask & ~(conn->share_access))) {
2956 DEBUG(5,("mkdir_internal: failing share access "
2957 "%s\n", lp_servicename(talloc_tos(), SNUM(conn))));
2958 return NT_STATUS_ACCESS_DENIED;
2961 if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
2962 NULL)) {
2963 return NT_STATUS_NO_MEMORY;
2966 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2967 posix_open = true;
2968 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2969 } else {
2970 mode = unix_mode(conn, FILE_ATTRIBUTE_DIRECTORY, smb_dname, parent_dir);
2973 status = check_parent_access(conn,
2974 smb_dname,
2975 access_mask);
2976 if(!NT_STATUS_IS_OK(status)) {
2977 DEBUG(5,("mkdir_internal: check_parent_access "
2978 "on directory %s for path %s returned %s\n",
2979 parent_dir,
2980 smb_dname->base_name,
2981 nt_errstr(status) ));
2982 return status;
2985 if (SMB_VFS_MKDIR(conn, smb_dname->base_name, mode) != 0) {
2986 return map_nt_error_from_unix(errno);
2989 /* Ensure we're checking for a symlink here.... */
2990 /* We don't want to get caught by a symlink racer. */
2992 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2993 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2994 smb_fname_str_dbg(smb_dname), strerror(errno)));
2995 return map_nt_error_from_unix(errno);
2998 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
2999 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
3000 smb_fname_str_dbg(smb_dname)));
3001 return NT_STATUS_NOT_A_DIRECTORY;
3004 if (lp_store_dos_attributes(SNUM(conn))) {
3005 if (!posix_open) {
3006 file_set_dosmode(conn, smb_dname,
3007 file_attributes | FILE_ATTRIBUTE_DIRECTORY,
3008 parent_dir, true);
3012 if (lp_inherit_permissions(SNUM(conn))) {
3013 inherit_access_posix_acl(conn, parent_dir,
3014 smb_dname->base_name, mode);
3015 need_re_stat = true;
3018 if (!posix_open) {
3020 * Check if high bits should have been set,
3021 * then (if bits are missing): add them.
3022 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
3023 * dir.
3025 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
3026 (mode & ~smb_dname->st.st_ex_mode)) {
3027 SMB_VFS_CHMOD(conn, smb_dname->base_name,
3028 (smb_dname->st.st_ex_mode |
3029 (mode & ~smb_dname->st.st_ex_mode)));
3030 need_re_stat = true;
3034 /* Change the owner if required. */
3035 if (lp_inherit_owner(SNUM(conn))) {
3036 change_dir_owner_to_parent(conn, parent_dir,
3037 smb_dname->base_name,
3038 &smb_dname->st);
3039 need_re_stat = true;
3042 if (need_re_stat) {
3043 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
3044 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
3045 smb_fname_str_dbg(smb_dname), strerror(errno)));
3046 return map_nt_error_from_unix(errno);
3050 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
3051 smb_dname->base_name);
3053 return NT_STATUS_OK;
3056 /****************************************************************************
3057 Open a directory from an NT SMB call.
3058 ****************************************************************************/
3060 static NTSTATUS open_directory(connection_struct *conn,
3061 struct smb_request *req,
3062 struct smb_filename *smb_dname,
3063 uint32 access_mask,
3064 uint32 share_access,
3065 uint32 create_disposition,
3066 uint32 create_options,
3067 uint32 file_attributes,
3068 int *pinfo,
3069 files_struct **result)
3071 files_struct *fsp = NULL;
3072 bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
3073 struct share_mode_lock *lck = NULL;
3074 NTSTATUS status;
3075 struct timespec mtimespec;
3076 int info = 0;
3078 if (is_ntfs_stream_smb_fname(smb_dname)) {
3079 DEBUG(2, ("open_directory: %s is a stream name!\n",
3080 smb_fname_str_dbg(smb_dname)));
3081 return NT_STATUS_NOT_A_DIRECTORY;
3084 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
3085 /* Ensure we have a directory attribute. */
3086 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
3089 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
3090 "share_access = 0x%x create_options = 0x%x, "
3091 "create_disposition = 0x%x, file_attributes = 0x%x\n",
3092 smb_fname_str_dbg(smb_dname),
3093 (unsigned int)access_mask,
3094 (unsigned int)share_access,
3095 (unsigned int)create_options,
3096 (unsigned int)create_disposition,
3097 (unsigned int)file_attributes));
3099 status = smbd_calculate_access_mask(conn, smb_dname, false,
3100 access_mask, &access_mask);
3101 if (!NT_STATUS_IS_OK(status)) {
3102 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
3103 "on file %s returned %s\n",
3104 smb_fname_str_dbg(smb_dname),
3105 nt_errstr(status)));
3106 return status;
3109 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3110 !security_token_has_privilege(get_current_nttok(conn),
3111 SEC_PRIV_SECURITY)) {
3112 DEBUG(10, ("open_directory: open on %s "
3113 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3114 smb_fname_str_dbg(smb_dname)));
3115 return NT_STATUS_PRIVILEGE_NOT_HELD;
3118 switch( create_disposition ) {
3119 case FILE_OPEN:
3121 if (!dir_existed) {
3122 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3125 info = FILE_WAS_OPENED;
3126 break;
3128 case FILE_CREATE:
3130 /* If directory exists error. If directory doesn't
3131 * exist create. */
3133 if (dir_existed) {
3134 status = NT_STATUS_OBJECT_NAME_COLLISION;
3135 DEBUG(2, ("open_directory: unable to create "
3136 "%s. Error was %s\n",
3137 smb_fname_str_dbg(smb_dname),
3138 nt_errstr(status)));
3139 return status;
3142 status = mkdir_internal(conn, smb_dname,
3143 file_attributes);
3145 if (!NT_STATUS_IS_OK(status)) {
3146 DEBUG(2, ("open_directory: unable to create "
3147 "%s. Error was %s\n",
3148 smb_fname_str_dbg(smb_dname),
3149 nt_errstr(status)));
3150 return status;
3153 info = FILE_WAS_CREATED;
3154 break;
3156 case FILE_OPEN_IF:
3158 * If directory exists open. If directory doesn't
3159 * exist create.
3162 if (dir_existed) {
3163 status = NT_STATUS_OK;
3164 info = FILE_WAS_OPENED;
3165 } else {
3166 status = mkdir_internal(conn, smb_dname,
3167 file_attributes);
3169 if (NT_STATUS_IS_OK(status)) {
3170 info = FILE_WAS_CREATED;
3171 } else {
3172 /* Cope with create race. */
3173 if (!NT_STATUS_EQUAL(status,
3174 NT_STATUS_OBJECT_NAME_COLLISION)) {
3175 DEBUG(2, ("open_directory: unable to create "
3176 "%s. Error was %s\n",
3177 smb_fname_str_dbg(smb_dname),
3178 nt_errstr(status)));
3179 return status;
3181 info = FILE_WAS_OPENED;
3185 break;
3187 case FILE_SUPERSEDE:
3188 case FILE_OVERWRITE:
3189 case FILE_OVERWRITE_IF:
3190 default:
3191 DEBUG(5,("open_directory: invalid create_disposition "
3192 "0x%x for directory %s\n",
3193 (unsigned int)create_disposition,
3194 smb_fname_str_dbg(smb_dname)));
3195 return NT_STATUS_INVALID_PARAMETER;
3198 if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
3199 DEBUG(5,("open_directory: %s is not a directory !\n",
3200 smb_fname_str_dbg(smb_dname)));
3201 return NT_STATUS_NOT_A_DIRECTORY;
3204 if (info == FILE_WAS_OPENED) {
3205 status = smbd_check_access_rights(conn,
3206 smb_dname,
3207 false,
3208 access_mask);
3209 if (!NT_STATUS_IS_OK(status)) {
3210 DEBUG(10, ("open_directory: smbd_check_access_rights on "
3211 "file %s failed with %s\n",
3212 smb_fname_str_dbg(smb_dname),
3213 nt_errstr(status)));
3214 return status;
3218 status = file_new(req, conn, &fsp);
3219 if(!NT_STATUS_IS_OK(status)) {
3220 return status;
3224 * Setup the files_struct for it.
3227 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
3228 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
3229 fsp->file_pid = req ? req->smbpid : 0;
3230 fsp->can_lock = False;
3231 fsp->can_read = False;
3232 fsp->can_write = False;
3234 fsp->share_access = share_access;
3235 fsp->fh->private_options = 0;
3237 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
3239 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
3240 fsp->print_file = NULL;
3241 fsp->modified = False;
3242 fsp->oplock_type = NO_OPLOCK;
3243 fsp->sent_oplock_break = NO_BREAK_SENT;
3244 fsp->is_directory = True;
3245 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
3246 status = fsp_set_smb_fname(fsp, smb_dname);
3247 if (!NT_STATUS_IS_OK(status)) {
3248 file_free(req, fsp);
3249 return status;
3252 /* Don't store old timestamps for directory
3253 handles in the internal database. We don't
3254 update them in there if new objects
3255 are creaded in the directory. Currently
3256 we only update timestamps on file writes.
3257 See bug #9870.
3259 ZERO_STRUCT(mtimespec);
3261 if (access_mask & (FILE_LIST_DIRECTORY|
3262 FILE_ADD_FILE|
3263 FILE_ADD_SUBDIRECTORY|
3264 FILE_TRAVERSE|
3265 DELETE_ACCESS|
3266 FILE_DELETE_CHILD)) {
3267 #ifdef O_DIRECTORY
3268 status = fd_open(conn, fsp, O_RDONLY|O_DIRECTORY, 0);
3269 #else
3270 /* POSIX allows us to open a directory with O_RDONLY. */
3271 status = fd_open(conn, fsp, O_RDONLY, 0);
3272 #endif
3273 if (!NT_STATUS_IS_OK(status)) {
3274 DEBUG(5, ("open_directory: Could not open fd for "
3275 "%s (%s)\n",
3276 smb_fname_str_dbg(smb_dname),
3277 nt_errstr(status)));
3278 file_free(req, fsp);
3279 return status;
3281 } else {
3282 fsp->fh->fd = -1;
3283 DEBUG(10, ("Not opening Directory %s\n",
3284 smb_fname_str_dbg(smb_dname)));
3287 status = vfs_stat_fsp(fsp);
3288 if (!NT_STATUS_IS_OK(status)) {
3289 fd_close(fsp);
3290 file_free(req, fsp);
3291 return status;
3294 /* Ensure there was no race condition. */
3295 if (!check_same_stat(&smb_dname->st, &fsp->fsp_name->st)) {
3296 DEBUG(5,("open_directory: stat struct differs for "
3297 "directory %s.\n",
3298 smb_fname_str_dbg(smb_dname)));
3299 fd_close(fsp);
3300 file_free(req, fsp);
3301 return NT_STATUS_ACCESS_DENIED;
3304 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
3305 conn->connectpath, smb_dname,
3306 &mtimespec);
3308 if (lck == NULL) {
3309 DEBUG(0, ("open_directory: Could not get share mode lock for "
3310 "%s\n", smb_fname_str_dbg(smb_dname)));
3311 fd_close(fsp);
3312 file_free(req, fsp);
3313 return NT_STATUS_SHARING_VIOLATION;
3316 if (has_delete_on_close(lck, fsp->name_hash)) {
3317 TALLOC_FREE(lck);
3318 fd_close(fsp);
3319 file_free(req, fsp);
3320 return NT_STATUS_DELETE_PENDING;
3323 status = open_mode_check(conn, lck,
3324 access_mask, share_access);
3326 if (!NT_STATUS_IS_OK(status)) {
3327 TALLOC_FREE(lck);
3328 fd_close(fsp);
3329 file_free(req, fsp);
3330 return status;
3333 if (!set_share_mode(lck, fsp, get_current_uid(conn),
3334 req ? req->mid : 0, NO_OPLOCK)) {
3335 TALLOC_FREE(lck);
3336 fd_close(fsp);
3337 file_free(req, fsp);
3338 return NT_STATUS_NO_MEMORY;
3341 /* For directories the delete on close bit at open time seems
3342 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
3343 if (create_options & FILE_DELETE_ON_CLOSE) {
3344 status = can_set_delete_on_close(fsp, 0);
3345 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
3346 del_share_mode(lck, fsp);
3347 TALLOC_FREE(lck);
3348 fd_close(fsp);
3349 file_free(req, fsp);
3350 return status;
3353 if (NT_STATUS_IS_OK(status)) {
3354 /* Note that here we set the *inital* delete on close flag,
3355 not the regular one. The magic gets handled in close. */
3356 fsp->initial_delete_on_close = True;
3362 * Deal with other opens having a modified write time. Is this
3363 * possible for directories?
3365 struct timespec write_time = get_share_mode_write_time(lck);
3367 if (!null_timespec(write_time)) {
3368 update_stat_ex_mtime(&fsp->fsp_name->st, write_time);
3372 TALLOC_FREE(lck);
3374 if (pinfo) {
3375 *pinfo = info;
3378 *result = fsp;
3379 return NT_STATUS_OK;
3382 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
3383 struct smb_filename *smb_dname)
3385 NTSTATUS status;
3386 files_struct *fsp;
3388 status = SMB_VFS_CREATE_FILE(
3389 conn, /* conn */
3390 req, /* req */
3391 0, /* root_dir_fid */
3392 smb_dname, /* fname */
3393 FILE_READ_ATTRIBUTES, /* access_mask */
3394 FILE_SHARE_NONE, /* share_access */
3395 FILE_CREATE, /* create_disposition*/
3396 FILE_DIRECTORY_FILE, /* create_options */
3397 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
3398 0, /* oplock_request */
3399 NULL, /* lease */
3400 0, /* allocation_size */
3401 0, /* private_flags */
3402 NULL, /* sd */
3403 NULL, /* ea_list */
3404 &fsp, /* result */
3405 NULL); /* pinfo */
3407 if (NT_STATUS_IS_OK(status)) {
3408 close_file(req, fsp, NORMAL_CLOSE);
3411 return status;
3414 /****************************************************************************
3415 Receive notification that one of our open files has been renamed by another
3416 smbd process.
3417 ****************************************************************************/
3419 void msg_file_was_renamed(struct messaging_context *msg,
3420 void *private_data,
3421 uint32_t msg_type,
3422 struct server_id server_id,
3423 DATA_BLOB *data)
3425 files_struct *fsp;
3426 char *frm = (char *)data->data;
3427 struct file_id id;
3428 const char *sharepath;
3429 const char *base_name;
3430 const char *stream_name;
3431 struct smb_filename *smb_fname = NULL;
3432 size_t sp_len, bn_len;
3433 NTSTATUS status;
3434 struct smbd_server_connection *sconn =
3435 talloc_get_type_abort(private_data,
3436 struct smbd_server_connection);
3438 if (data->data == NULL
3439 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
3440 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
3441 (int)data->length));
3442 return;
3445 /* Unpack the message. */
3446 pull_file_id_24(frm, &id);
3447 sharepath = &frm[24];
3448 sp_len = strlen(sharepath);
3449 base_name = sharepath + sp_len + 1;
3450 bn_len = strlen(base_name);
3451 stream_name = sharepath + sp_len + 1 + bn_len + 1;
3453 /* stream_name must always be NULL if there is no stream. */
3454 if (stream_name[0] == '\0') {
3455 stream_name = NULL;
3458 smb_fname = synthetic_smb_fname(talloc_tos(), base_name,
3459 stream_name, NULL);
3460 if (smb_fname == NULL) {
3461 return;
3464 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3465 "file_id %s\n",
3466 sharepath, smb_fname_str_dbg(smb_fname),
3467 file_id_string_tos(&id)));
3469 for(fsp = file_find_di_first(sconn, id); fsp;
3470 fsp = file_find_di_next(fsp)) {
3471 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
3473 DEBUG(10,("msg_file_was_renamed: renaming file %s from %s -> %s\n",
3474 fsp_fnum_dbg(fsp), fsp_str_dbg(fsp),
3475 smb_fname_str_dbg(smb_fname)));
3476 status = fsp_set_smb_fname(fsp, smb_fname);
3477 if (!NT_STATUS_IS_OK(status)) {
3478 goto out;
3480 } else {
3481 /* TODO. JRA. */
3482 /* Now we have the complete path we can work out if this is
3483 actually within this share and adjust newname accordingly. */
3484 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3485 "not sharepath %s) "
3486 "%s from %s -> %s\n",
3487 fsp->conn->connectpath,
3488 sharepath,
3489 fsp_fnum_dbg(fsp),
3490 fsp_str_dbg(fsp),
3491 smb_fname_str_dbg(smb_fname)));
3494 out:
3495 TALLOC_FREE(smb_fname);
3496 return;
3500 * If a main file is opened for delete, all streams need to be checked for
3501 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3502 * If that works, delete them all by setting the delete on close and close.
3505 NTSTATUS open_streams_for_delete(connection_struct *conn,
3506 const char *fname)
3508 struct stream_struct *stream_info = NULL;
3509 files_struct **streams = NULL;
3510 int i;
3511 unsigned int num_streams = 0;
3512 TALLOC_CTX *frame = talloc_stackframe();
3513 NTSTATUS status;
3515 status = vfs_streaminfo(conn, NULL, fname, talloc_tos(),
3516 &num_streams, &stream_info);
3518 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
3519 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
3520 DEBUG(10, ("no streams around\n"));
3521 TALLOC_FREE(frame);
3522 return NT_STATUS_OK;
3525 if (!NT_STATUS_IS_OK(status)) {
3526 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3527 nt_errstr(status)));
3528 goto fail;
3531 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3532 num_streams));
3534 if (num_streams == 0) {
3535 TALLOC_FREE(frame);
3536 return NT_STATUS_OK;
3539 streams = talloc_array(talloc_tos(), files_struct *, num_streams);
3540 if (streams == NULL) {
3541 DEBUG(0, ("talloc failed\n"));
3542 status = NT_STATUS_NO_MEMORY;
3543 goto fail;
3546 for (i=0; i<num_streams; i++) {
3547 struct smb_filename *smb_fname;
3549 if (strequal(stream_info[i].name, "::$DATA")) {
3550 streams[i] = NULL;
3551 continue;
3554 smb_fname = synthetic_smb_fname(
3555 talloc_tos(), fname, stream_info[i].name, NULL);
3556 if (smb_fname == NULL) {
3557 status = NT_STATUS_NO_MEMORY;
3558 goto fail;
3561 if (SMB_VFS_STAT(conn, smb_fname) == -1) {
3562 DEBUG(10, ("Unable to stat stream: %s\n",
3563 smb_fname_str_dbg(smb_fname)));
3566 status = SMB_VFS_CREATE_FILE(
3567 conn, /* conn */
3568 NULL, /* req */
3569 0, /* root_dir_fid */
3570 smb_fname, /* fname */
3571 DELETE_ACCESS, /* access_mask */
3572 (FILE_SHARE_READ | /* share_access */
3573 FILE_SHARE_WRITE | FILE_SHARE_DELETE),
3574 FILE_OPEN, /* create_disposition*/
3575 0, /* create_options */
3576 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
3577 0, /* oplock_request */
3578 NULL, /* lease */
3579 0, /* allocation_size */
3580 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */
3581 NULL, /* sd */
3582 NULL, /* ea_list */
3583 &streams[i], /* result */
3584 NULL); /* pinfo */
3586 if (!NT_STATUS_IS_OK(status)) {
3587 DEBUG(10, ("Could not open stream %s: %s\n",
3588 smb_fname_str_dbg(smb_fname),
3589 nt_errstr(status)));
3591 TALLOC_FREE(smb_fname);
3592 break;
3594 TALLOC_FREE(smb_fname);
3598 * don't touch the variable "status" beyond this point :-)
3601 for (i -= 1 ; i >= 0; i--) {
3602 if (streams[i] == NULL) {
3603 continue;
3606 DEBUG(10, ("Closing stream # %d, %s\n", i,
3607 fsp_str_dbg(streams[i])));
3608 close_file(NULL, streams[i], NORMAL_CLOSE);
3611 fail:
3612 TALLOC_FREE(frame);
3613 return status;
3616 /*********************************************************************
3617 Create a default ACL by inheriting from the parent. If no inheritance
3618 from the parent available, don't set anything. This will leave the actual
3619 permissions the new file or directory already got from the filesystem
3620 as the NT ACL when read.
3621 *********************************************************************/
3623 static NTSTATUS inherit_new_acl(files_struct *fsp)
3625 TALLOC_CTX *frame = talloc_stackframe();
3626 char *parent_name = NULL;
3627 struct security_descriptor *parent_desc = NULL;
3628 NTSTATUS status = NT_STATUS_OK;
3629 struct security_descriptor *psd = NULL;
3630 const struct dom_sid *owner_sid = NULL;
3631 const struct dom_sid *group_sid = NULL;
3632 uint32_t security_info_sent = (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL);
3633 struct security_token *token = fsp->conn->session_info->security_token;
3634 bool inherit_owner = lp_inherit_owner(SNUM(fsp->conn));
3635 bool inheritable_components = false;
3636 bool try_builtin_administrators = false;
3637 const struct dom_sid *BA_U_sid = NULL;
3638 const struct dom_sid *BA_G_sid = NULL;
3639 bool try_system = false;
3640 const struct dom_sid *SY_U_sid = NULL;
3641 const struct dom_sid *SY_G_sid = NULL;
3642 size_t size = 0;
3644 if (!parent_dirname(frame, fsp->fsp_name->base_name, &parent_name, NULL)) {
3645 TALLOC_FREE(frame);
3646 return NT_STATUS_NO_MEMORY;
3649 status = SMB_VFS_GET_NT_ACL(fsp->conn,
3650 parent_name,
3651 (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL),
3652 frame,
3653 &parent_desc);
3654 if (!NT_STATUS_IS_OK(status)) {
3655 TALLOC_FREE(frame);
3656 return status;
3659 inheritable_components = sd_has_inheritable_components(parent_desc,
3660 fsp->is_directory);
3662 if (!inheritable_components && !inherit_owner) {
3663 TALLOC_FREE(frame);
3664 /* Nothing to inherit and not setting owner. */
3665 return NT_STATUS_OK;
3668 /* Create an inherited descriptor from the parent. */
3670 if (DEBUGLEVEL >= 10) {
3671 DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
3672 fsp_str_dbg(fsp) ));
3673 NDR_PRINT_DEBUG(security_descriptor, parent_desc);
3676 /* Inherit from parent descriptor if "inherit owner" set. */
3677 if (inherit_owner) {
3678 owner_sid = parent_desc->owner_sid;
3679 group_sid = parent_desc->group_sid;
3682 if (owner_sid == NULL) {
3683 if (security_token_has_builtin_administrators(token)) {
3684 try_builtin_administrators = true;
3685 } else if (security_token_is_system(token)) {
3686 try_builtin_administrators = true;
3687 try_system = true;
3691 if (group_sid == NULL &&
3692 token->num_sids == PRIMARY_GROUP_SID_INDEX)
3694 if (security_token_is_system(token)) {
3695 try_builtin_administrators = true;
3696 try_system = true;
3700 if (try_builtin_administrators) {
3701 struct unixid ids;
3702 bool ok;
3704 ZERO_STRUCT(ids);
3705 ok = sids_to_unixids(&global_sid_Builtin_Administrators, 1, &ids);
3706 if (ok) {
3707 switch (ids.type) {
3708 case ID_TYPE_BOTH:
3709 BA_U_sid = &global_sid_Builtin_Administrators;
3710 BA_G_sid = &global_sid_Builtin_Administrators;
3711 break;
3712 case ID_TYPE_UID:
3713 BA_U_sid = &global_sid_Builtin_Administrators;
3714 break;
3715 case ID_TYPE_GID:
3716 BA_G_sid = &global_sid_Builtin_Administrators;
3717 break;
3718 default:
3719 break;
3724 if (try_system) {
3725 struct unixid ids;
3726 bool ok;
3728 ZERO_STRUCT(ids);
3729 ok = sids_to_unixids(&global_sid_System, 1, &ids);
3730 if (ok) {
3731 switch (ids.type) {
3732 case ID_TYPE_BOTH:
3733 SY_U_sid = &global_sid_System;
3734 SY_G_sid = &global_sid_System;
3735 break;
3736 case ID_TYPE_UID:
3737 SY_U_sid = &global_sid_System;
3738 break;
3739 case ID_TYPE_GID:
3740 SY_G_sid = &global_sid_System;
3741 break;
3742 default:
3743 break;
3748 if (owner_sid == NULL) {
3749 owner_sid = BA_U_sid;
3752 if (owner_sid == NULL) {
3753 owner_sid = SY_U_sid;
3756 if (group_sid == NULL) {
3757 group_sid = SY_G_sid;
3760 if (try_system && group_sid == NULL) {
3761 group_sid = BA_G_sid;
3764 if (owner_sid == NULL) {
3765 owner_sid = &token->sids[PRIMARY_USER_SID_INDEX];
3767 if (group_sid == NULL) {
3768 if (token->num_sids == PRIMARY_GROUP_SID_INDEX) {
3769 group_sid = &token->sids[PRIMARY_USER_SID_INDEX];
3770 } else {
3771 group_sid = &token->sids[PRIMARY_GROUP_SID_INDEX];
3775 status = se_create_child_secdesc(frame,
3776 &psd,
3777 &size,
3778 parent_desc,
3779 owner_sid,
3780 group_sid,
3781 fsp->is_directory);
3782 if (!NT_STATUS_IS_OK(status)) {
3783 TALLOC_FREE(frame);
3784 return status;
3787 /* If inheritable_components == false,
3788 se_create_child_secdesc()
3789 creates a security desriptor with a NULL dacl
3790 entry, but with SEC_DESC_DACL_PRESENT. We need
3791 to remove that flag. */
3793 if (!inheritable_components) {
3794 security_info_sent &= ~SECINFO_DACL;
3795 psd->type &= ~SEC_DESC_DACL_PRESENT;
3798 if (DEBUGLEVEL >= 10) {
3799 DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
3800 fsp_str_dbg(fsp) ));
3801 NDR_PRINT_DEBUG(security_descriptor, psd);
3804 if (inherit_owner) {
3805 /* We need to be root to force this. */
3806 become_root();
3808 status = SMB_VFS_FSET_NT_ACL(fsp,
3809 security_info_sent,
3810 psd);
3811 if (inherit_owner) {
3812 unbecome_root();
3814 TALLOC_FREE(frame);
3815 return status;
3819 * Wrapper around open_file_ntcreate and open_directory
3822 static NTSTATUS create_file_unixpath(connection_struct *conn,
3823 struct smb_request *req,
3824 struct smb_filename *smb_fname,
3825 uint32_t access_mask,
3826 uint32_t share_access,
3827 uint32_t create_disposition,
3828 uint32_t create_options,
3829 uint32_t file_attributes,
3830 uint32_t oplock_request,
3831 struct smb2_lease *lease,
3832 uint64_t allocation_size,
3833 uint32_t private_flags,
3834 struct security_descriptor *sd,
3835 struct ea_list *ea_list,
3837 files_struct **result,
3838 int *pinfo)
3840 int info = FILE_WAS_OPENED;
3841 files_struct *base_fsp = NULL;
3842 files_struct *fsp = NULL;
3843 NTSTATUS status;
3845 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3846 "file_attributes = 0x%x, share_access = 0x%x, "
3847 "create_disposition = 0x%x create_options = 0x%x "
3848 "oplock_request = 0x%x private_flags = 0x%x "
3849 "ea_list = 0x%p, sd = 0x%p, "
3850 "fname = %s\n",
3851 (unsigned int)access_mask,
3852 (unsigned int)file_attributes,
3853 (unsigned int)share_access,
3854 (unsigned int)create_disposition,
3855 (unsigned int)create_options,
3856 (unsigned int)oplock_request,
3857 (unsigned int)private_flags,
3858 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3860 if (create_options & FILE_OPEN_BY_FILE_ID) {
3861 status = NT_STATUS_NOT_SUPPORTED;
3862 goto fail;
3865 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
3866 status = NT_STATUS_INVALID_PARAMETER;
3867 goto fail;
3870 if (req == NULL) {
3871 oplock_request |= INTERNAL_OPEN_ONLY;
3874 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3875 && (access_mask & DELETE_ACCESS)
3876 && !is_ntfs_stream_smb_fname(smb_fname)) {
3878 * We can't open a file with DELETE access if any of the
3879 * streams is open without FILE_SHARE_DELETE
3881 status = open_streams_for_delete(conn, smb_fname->base_name);
3883 if (!NT_STATUS_IS_OK(status)) {
3884 goto fail;
3888 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3889 !security_token_has_privilege(get_current_nttok(conn),
3890 SEC_PRIV_SECURITY)) {
3891 DEBUG(10, ("create_file_unixpath: open on %s "
3892 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3893 smb_fname_str_dbg(smb_fname)));
3894 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3895 goto fail;
3898 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3899 && is_ntfs_stream_smb_fname(smb_fname)
3900 && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
3901 uint32 base_create_disposition;
3902 struct smb_filename *smb_fname_base = NULL;
3904 if (create_options & FILE_DIRECTORY_FILE) {
3905 status = NT_STATUS_NOT_A_DIRECTORY;
3906 goto fail;
3909 switch (create_disposition) {
3910 case FILE_OPEN:
3911 base_create_disposition = FILE_OPEN;
3912 break;
3913 default:
3914 base_create_disposition = FILE_OPEN_IF;
3915 break;
3918 /* Create an smb_filename with stream_name == NULL. */
3919 smb_fname_base = synthetic_smb_fname(talloc_tos(),
3920 smb_fname->base_name,
3921 NULL, NULL);
3922 if (smb_fname_base == NULL) {
3923 status = NT_STATUS_NO_MEMORY;
3924 goto fail;
3927 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
3928 DEBUG(10, ("Unable to stat stream: %s\n",
3929 smb_fname_str_dbg(smb_fname_base)));
3930 } else {
3932 * https://bugzilla.samba.org/show_bug.cgi?id=10229
3933 * We need to check if the requested access mask
3934 * could be used to open the underlying file (if
3935 * it existed), as we're passing in zero for the
3936 * access mask to the base filename.
3938 status = check_base_file_access(conn,
3939 smb_fname_base,
3940 access_mask);
3942 if (!NT_STATUS_IS_OK(status)) {
3943 DEBUG(10, ("Permission check "
3944 "for base %s failed: "
3945 "%s\n", smb_fname->base_name,
3946 nt_errstr(status)));
3947 goto fail;
3951 /* Open the base file. */
3952 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
3953 FILE_SHARE_READ
3954 | FILE_SHARE_WRITE
3955 | FILE_SHARE_DELETE,
3956 base_create_disposition,
3957 0, 0, 0, NULL, 0, 0, NULL, NULL,
3958 &base_fsp, NULL);
3959 TALLOC_FREE(smb_fname_base);
3961 if (!NT_STATUS_IS_OK(status)) {
3962 DEBUG(10, ("create_file_unixpath for base %s failed: "
3963 "%s\n", smb_fname->base_name,
3964 nt_errstr(status)));
3965 goto fail;
3967 /* we don't need the low level fd */
3968 fd_close(base_fsp);
3972 * If it's a request for a directory open, deal with it separately.
3975 if (create_options & FILE_DIRECTORY_FILE) {
3977 if (create_options & FILE_NON_DIRECTORY_FILE) {
3978 status = NT_STATUS_INVALID_PARAMETER;
3979 goto fail;
3982 /* Can't open a temp directory. IFS kit test. */
3983 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
3984 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
3985 status = NT_STATUS_INVALID_PARAMETER;
3986 goto fail;
3990 * We will get a create directory here if the Win32
3991 * app specified a security descriptor in the
3992 * CreateDirectory() call.
3995 oplock_request = 0;
3996 status = open_directory(
3997 conn, req, smb_fname, access_mask, share_access,
3998 create_disposition, create_options, file_attributes,
3999 &info, &fsp);
4000 } else {
4003 * Ordinary file case.
4006 status = file_new(req, conn, &fsp);
4007 if(!NT_STATUS_IS_OK(status)) {
4008 goto fail;
4011 status = fsp_set_smb_fname(fsp, smb_fname);
4012 if (!NT_STATUS_IS_OK(status)) {
4013 goto fail;
4016 if (base_fsp) {
4018 * We're opening the stream element of a
4019 * base_fsp we already opened. Set up the
4020 * base_fsp pointer.
4022 fsp->base_fsp = base_fsp;
4025 if (allocation_size) {
4026 fsp->initial_allocation_size = smb_roundup(fsp->conn,
4027 allocation_size);
4030 status = open_file_ntcreate(conn,
4031 req,
4032 access_mask,
4033 share_access,
4034 create_disposition,
4035 create_options,
4036 file_attributes,
4037 oplock_request,
4038 lease,
4039 private_flags,
4040 &info,
4041 fsp);
4043 if(!NT_STATUS_IS_OK(status)) {
4044 file_free(req, fsp);
4045 fsp = NULL;
4048 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
4050 /* A stream open never opens a directory */
4052 if (base_fsp) {
4053 status = NT_STATUS_FILE_IS_A_DIRECTORY;
4054 goto fail;
4058 * Fail the open if it was explicitly a non-directory
4059 * file.
4062 if (create_options & FILE_NON_DIRECTORY_FILE) {
4063 status = NT_STATUS_FILE_IS_A_DIRECTORY;
4064 goto fail;
4067 oplock_request = 0;
4068 status = open_directory(
4069 conn, req, smb_fname, access_mask,
4070 share_access, create_disposition,
4071 create_options, file_attributes,
4072 &info, &fsp);
4076 if (!NT_STATUS_IS_OK(status)) {
4077 goto fail;
4080 fsp->base_fsp = base_fsp;
4082 if ((ea_list != NULL) &&
4083 ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
4084 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
4085 if (!NT_STATUS_IS_OK(status)) {
4086 goto fail;
4090 if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
4091 status = NT_STATUS_ACCESS_DENIED;
4092 goto fail;
4095 /* Save the requested allocation size. */
4096 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
4097 if (allocation_size
4098 && (allocation_size > fsp->fsp_name->st.st_ex_size)) {
4099 fsp->initial_allocation_size = smb_roundup(
4100 fsp->conn, allocation_size);
4101 if (fsp->is_directory) {
4102 /* Can't set allocation size on a directory. */
4103 status = NT_STATUS_ACCESS_DENIED;
4104 goto fail;
4106 if (vfs_allocate_file_space(
4107 fsp, fsp->initial_allocation_size) == -1) {
4108 status = NT_STATUS_DISK_FULL;
4109 goto fail;
4111 } else {
4112 fsp->initial_allocation_size = smb_roundup(
4113 fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
4115 } else {
4116 fsp->initial_allocation_size = 0;
4119 if ((info == FILE_WAS_CREATED) && lp_nt_acl_support(SNUM(conn)) &&
4120 fsp->base_fsp == NULL) {
4121 if (sd != NULL) {
4123 * According to the MS documentation, the only time the security
4124 * descriptor is applied to the opened file is iff we *created* the
4125 * file; an existing file stays the same.
4127 * Also, it seems (from observation) that you can open the file with
4128 * any access mask but you can still write the sd. We need to override
4129 * the granted access before we call set_sd
4130 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
4133 uint32_t sec_info_sent;
4134 uint32_t saved_access_mask = fsp->access_mask;
4136 sec_info_sent = get_sec_info(sd);
4138 fsp->access_mask = FILE_GENERIC_ALL;
4140 if (sec_info_sent & (SECINFO_OWNER|
4141 SECINFO_GROUP|
4142 SECINFO_DACL|
4143 SECINFO_SACL)) {
4144 status = set_sd(fsp, sd, sec_info_sent);
4147 fsp->access_mask = saved_access_mask;
4149 if (!NT_STATUS_IS_OK(status)) {
4150 goto fail;
4152 } else if (lp_inherit_acls(SNUM(conn))) {
4153 /* Inherit from parent. Errors here are not fatal. */
4154 status = inherit_new_acl(fsp);
4155 if (!NT_STATUS_IS_OK(status)) {
4156 DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
4157 fsp_str_dbg(fsp),
4158 nt_errstr(status) ));
4163 if ((conn->fs_capabilities & FILE_FILE_COMPRESSION)
4164 && (create_options & FILE_NO_COMPRESSION)
4165 && (info == FILE_WAS_CREATED)) {
4166 status = SMB_VFS_SET_COMPRESSION(conn, fsp, fsp,
4167 COMPRESSION_FORMAT_NONE);
4168 if (!NT_STATUS_IS_OK(status)) {
4169 DEBUG(1, ("failed to disable compression: %s\n",
4170 nt_errstr(status)));
4174 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
4176 *result = fsp;
4177 if (pinfo != NULL) {
4178 *pinfo = info;
4181 smb_fname->st = fsp->fsp_name->st;
4183 return NT_STATUS_OK;
4185 fail:
4186 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
4188 if (fsp != NULL) {
4189 if (base_fsp && fsp->base_fsp == base_fsp) {
4191 * The close_file below will close
4192 * fsp->base_fsp.
4194 base_fsp = NULL;
4196 close_file(req, fsp, ERROR_CLOSE);
4197 fsp = NULL;
4199 if (base_fsp != NULL) {
4200 close_file(req, base_fsp, ERROR_CLOSE);
4201 base_fsp = NULL;
4203 return status;
4207 * Calculate the full path name given a relative fid.
4209 NTSTATUS get_relative_fid_filename(connection_struct *conn,
4210 struct smb_request *req,
4211 uint16_t root_dir_fid,
4212 const struct smb_filename *smb_fname,
4213 struct smb_filename **smb_fname_out)
4215 files_struct *dir_fsp;
4216 char *parent_fname = NULL;
4217 char *new_base_name = NULL;
4218 NTSTATUS status;
4220 if (root_dir_fid == 0 || !smb_fname) {
4221 status = NT_STATUS_INTERNAL_ERROR;
4222 goto out;
4225 dir_fsp = file_fsp(req, root_dir_fid);
4227 if (dir_fsp == NULL) {
4228 status = NT_STATUS_INVALID_HANDLE;
4229 goto out;
4232 if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) {
4233 status = NT_STATUS_INVALID_HANDLE;
4234 goto out;
4237 if (!dir_fsp->is_directory) {
4240 * Check to see if this is a mac fork of some kind.
4243 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
4244 is_ntfs_stream_smb_fname(smb_fname)) {
4245 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
4246 goto out;
4250 we need to handle the case when we get a
4251 relative open relative to a file and the
4252 pathname is blank - this is a reopen!
4253 (hint from demyn plantenberg)
4256 status = NT_STATUS_INVALID_HANDLE;
4257 goto out;
4260 if (ISDOT(dir_fsp->fsp_name->base_name)) {
4262 * We're at the toplevel dir, the final file name
4263 * must not contain ./, as this is filtered out
4264 * normally by srvstr_get_path and unix_convert
4265 * explicitly rejects paths containing ./.
4267 parent_fname = talloc_strdup(talloc_tos(), "");
4268 if (parent_fname == NULL) {
4269 status = NT_STATUS_NO_MEMORY;
4270 goto out;
4272 } else {
4273 size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name);
4276 * Copy in the base directory name.
4279 parent_fname = talloc_array(talloc_tos(), char,
4280 dir_name_len+2);
4281 if (parent_fname == NULL) {
4282 status = NT_STATUS_NO_MEMORY;
4283 goto out;
4285 memcpy(parent_fname, dir_fsp->fsp_name->base_name,
4286 dir_name_len+1);
4289 * Ensure it ends in a '/'.
4290 * We used TALLOC_SIZE +2 to add space for the '/'.
4293 if(dir_name_len
4294 && (parent_fname[dir_name_len-1] != '\\')
4295 && (parent_fname[dir_name_len-1] != '/')) {
4296 parent_fname[dir_name_len] = '/';
4297 parent_fname[dir_name_len+1] = '\0';
4301 new_base_name = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
4302 smb_fname->base_name);
4303 if (new_base_name == NULL) {
4304 status = NT_STATUS_NO_MEMORY;
4305 goto out;
4308 status = filename_convert(req,
4309 conn,
4310 req->flags2 & FLAGS2_DFS_PATHNAMES,
4311 new_base_name,
4313 NULL,
4314 smb_fname_out);
4315 if (!NT_STATUS_IS_OK(status)) {
4316 goto out;
4319 out:
4320 TALLOC_FREE(parent_fname);
4321 TALLOC_FREE(new_base_name);
4322 return status;
4325 NTSTATUS create_file_default(connection_struct *conn,
4326 struct smb_request *req,
4327 uint16_t root_dir_fid,
4328 struct smb_filename *smb_fname,
4329 uint32_t access_mask,
4330 uint32_t share_access,
4331 uint32_t create_disposition,
4332 uint32_t create_options,
4333 uint32_t file_attributes,
4334 uint32_t oplock_request,
4335 struct smb2_lease *lease,
4336 uint64_t allocation_size,
4337 uint32_t private_flags,
4338 struct security_descriptor *sd,
4339 struct ea_list *ea_list,
4340 files_struct **result,
4341 int *pinfo)
4343 int info = FILE_WAS_OPENED;
4344 files_struct *fsp = NULL;
4345 NTSTATUS status;
4346 bool stream_name = false;
4348 DEBUG(10,("create_file: access_mask = 0x%x "
4349 "file_attributes = 0x%x, share_access = 0x%x, "
4350 "create_disposition = 0x%x create_options = 0x%x "
4351 "oplock_request = 0x%x "
4352 "private_flags = 0x%x "
4353 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
4354 "fname = %s\n",
4355 (unsigned int)access_mask,
4356 (unsigned int)file_attributes,
4357 (unsigned int)share_access,
4358 (unsigned int)create_disposition,
4359 (unsigned int)create_options,
4360 (unsigned int)oplock_request,
4361 (unsigned int)private_flags,
4362 (unsigned int)root_dir_fid,
4363 ea_list, sd, smb_fname_str_dbg(smb_fname)));
4366 * Calculate the filename from the root_dir_if if necessary.
4369 if (root_dir_fid != 0) {
4370 struct smb_filename *smb_fname_out = NULL;
4371 status = get_relative_fid_filename(conn, req, root_dir_fid,
4372 smb_fname, &smb_fname_out);
4373 if (!NT_STATUS_IS_OK(status)) {
4374 goto fail;
4376 smb_fname = smb_fname_out;
4380 * Check to see if this is a mac fork of some kind.
4383 stream_name = is_ntfs_stream_smb_fname(smb_fname);
4384 if (stream_name) {
4385 enum FAKE_FILE_TYPE fake_file_type;
4387 fake_file_type = is_fake_file(smb_fname);
4389 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
4392 * Here we go! support for changing the disk quotas
4393 * --metze
4395 * We need to fake up to open this MAGIC QUOTA file
4396 * and return a valid FID.
4398 * w2k close this file directly after openening xp
4399 * also tries a QUERY_FILE_INFO on the file and then
4400 * close it
4402 status = open_fake_file(req, conn, req->vuid,
4403 fake_file_type, smb_fname,
4404 access_mask, &fsp);
4405 if (!NT_STATUS_IS_OK(status)) {
4406 goto fail;
4409 ZERO_STRUCT(smb_fname->st);
4410 goto done;
4413 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
4414 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4415 goto fail;
4419 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
4420 int ret;
4421 smb_fname->stream_name = NULL;
4422 /* We have to handle this error here. */
4423 if (create_options & FILE_DIRECTORY_FILE) {
4424 status = NT_STATUS_NOT_A_DIRECTORY;
4425 goto fail;
4427 if (lp_posix_pathnames()) {
4428 ret = SMB_VFS_LSTAT(conn, smb_fname);
4429 } else {
4430 ret = SMB_VFS_STAT(conn, smb_fname);
4433 if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
4434 status = NT_STATUS_FILE_IS_A_DIRECTORY;
4435 goto fail;
4439 status = create_file_unixpath(
4440 conn, req, smb_fname, access_mask, share_access,
4441 create_disposition, create_options, file_attributes,
4442 oplock_request, lease, allocation_size, private_flags,
4443 sd, ea_list,
4444 &fsp, &info);
4446 if (!NT_STATUS_IS_OK(status)) {
4447 goto fail;
4450 done:
4451 DEBUG(10, ("create_file: info=%d\n", info));
4453 *result = fsp;
4454 if (pinfo != NULL) {
4455 *pinfo = info;
4457 return NT_STATUS_OK;
4459 fail:
4460 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
4462 if (fsp != NULL) {
4463 close_file(req, fsp, ERROR_CLOSE);
4464 fsp = NULL;
4466 return status;