smbd: Store "struct deferred_open_record" instead of anonymous data on pml
[Samba.git] / source3 / smbd / open.c
blobc0f4dea3ac986ca0d5961ffa45460cb5198aa30b
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);
822 } else if (local_flags & O_CREAT){
823 status = check_parent_access(conn,
824 smb_fname,
825 SEC_DIR_ADD_FILE);
826 } else {
827 /* File didn't exist and no O_CREAT. */
828 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
830 if (!NT_STATUS_IS_OK(status)) {
831 DEBUG(10,("open_file: "
832 "%s on file "
833 "%s returned %s\n",
834 file_existed ?
835 "smbd_check_access_rights" :
836 "check_parent_access",
837 smb_fname_str_dbg(smb_fname),
838 nt_errstr(status) ));
839 return status;
843 /* Actually do the open */
844 status = fd_open_atomic(conn, fsp, local_flags,
845 unx_mode, p_file_created);
846 if (!NT_STATUS_IS_OK(status)) {
847 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
848 "(flags=%d)\n", smb_fname_str_dbg(smb_fname),
849 nt_errstr(status),local_flags,flags));
850 return status;
853 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
854 if (ret == -1) {
855 /* If we have an fd, this stat should succeed. */
856 DEBUG(0,("Error doing fstat on open file %s "
857 "(%s)\n",
858 smb_fname_str_dbg(smb_fname),
859 strerror(errno) ));
860 status = map_nt_error_from_unix(errno);
861 fd_close(fsp);
862 return status;
865 if (*p_file_created) {
866 /* We created this file. */
868 bool need_re_stat = false;
869 /* Do all inheritance work after we've
870 done a successful fstat call and filled
871 in the stat struct in fsp->fsp_name. */
873 /* Inherit the ACL if required */
874 if (lp_inherit_permissions(SNUM(conn))) {
875 inherit_access_posix_acl(conn, parent_dir,
876 smb_fname->base_name,
877 unx_mode);
878 need_re_stat = true;
881 /* Change the owner if required. */
882 if (lp_inherit_owner(SNUM(conn))) {
883 change_file_owner_to_parent(conn, parent_dir,
884 fsp);
885 need_re_stat = true;
888 if (need_re_stat) {
889 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
890 /* If we have an fd, this stat should succeed. */
891 if (ret == -1) {
892 DEBUG(0,("Error doing fstat on open file %s "
893 "(%s)\n",
894 smb_fname_str_dbg(smb_fname),
895 strerror(errno) ));
899 notify_fname(conn, NOTIFY_ACTION_ADDED,
900 FILE_NOTIFY_CHANGE_FILE_NAME,
901 smb_fname->base_name);
903 } else {
904 fsp->fh->fd = -1; /* What we used to call a stat open. */
905 if (!file_existed) {
906 /* File must exist for a stat open. */
907 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
910 status = smbd_check_access_rights(conn,
911 smb_fname,
912 false,
913 access_mask);
915 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
916 fsp->posix_open &&
917 S_ISLNK(smb_fname->st.st_ex_mode)) {
918 /* This is a POSIX stat open for delete
919 * or rename on a symlink that points
920 * nowhere. Allow. */
921 DEBUG(10,("open_file: allowing POSIX "
922 "open on bad symlink %s\n",
923 smb_fname_str_dbg(smb_fname)));
924 status = NT_STATUS_OK;
927 if (!NT_STATUS_IS_OK(status)) {
928 DEBUG(10,("open_file: "
929 "smbd_check_access_rights on file "
930 "%s returned %s\n",
931 smb_fname_str_dbg(smb_fname),
932 nt_errstr(status) ));
933 return status;
938 * POSIX allows read-only opens of directories. We don't
939 * want to do this (we use a different code path for this)
940 * so catch a directory open and return an EISDIR. JRA.
943 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
944 fd_close(fsp);
945 errno = EISDIR;
946 return NT_STATUS_FILE_IS_A_DIRECTORY;
949 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
950 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
951 fsp->file_pid = req ? req->smbpid : 0;
952 fsp->can_lock = True;
953 fsp->can_read = ((access_mask & FILE_READ_DATA) != 0);
954 fsp->can_write =
955 CAN_WRITE(conn) &&
956 ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) != 0);
957 fsp->print_file = NULL;
958 fsp->modified = False;
959 fsp->sent_oplock_break = NO_BREAK_SENT;
960 fsp->is_directory = False;
961 if (conn->aio_write_behind_list &&
962 is_in_path(smb_fname->base_name, conn->aio_write_behind_list,
963 conn->case_sensitive)) {
964 fsp->aio_write_behind = True;
967 fsp->wcp = NULL; /* Write cache pointer. */
969 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
970 conn->session_info->unix_info->unix_name,
971 smb_fname_str_dbg(smb_fname),
972 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
973 conn->num_files_open));
975 errno = 0;
976 return NT_STATUS_OK;
979 /****************************************************************************
980 Check if we can open a file with a share mode.
981 Returns True if conflict, False if not.
982 ****************************************************************************/
984 static bool share_conflict(struct share_mode_entry *entry,
985 uint32 access_mask,
986 uint32 share_access)
988 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
989 "entry->share_access = 0x%x, "
990 "entry->private_options = 0x%x\n",
991 (unsigned int)entry->access_mask,
992 (unsigned int)entry->share_access,
993 (unsigned int)entry->private_options));
995 if (server_id_is_disconnected(&entry->pid)) {
997 * note: cleanup should have been done by
998 * delay_for_batch_oplocks()
1000 return false;
1003 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
1004 (unsigned int)access_mask, (unsigned int)share_access));
1006 if ((entry->access_mask & (FILE_WRITE_DATA|
1007 FILE_APPEND_DATA|
1008 FILE_READ_DATA|
1009 FILE_EXECUTE|
1010 DELETE_ACCESS)) == 0) {
1011 DEBUG(10,("share_conflict: No conflict due to "
1012 "entry->access_mask = 0x%x\n",
1013 (unsigned int)entry->access_mask ));
1014 return False;
1017 if ((access_mask & (FILE_WRITE_DATA|
1018 FILE_APPEND_DATA|
1019 FILE_READ_DATA|
1020 FILE_EXECUTE|
1021 DELETE_ACCESS)) == 0) {
1022 DEBUG(10,("share_conflict: No conflict due to "
1023 "access_mask = 0x%x\n",
1024 (unsigned int)access_mask ));
1025 return False;
1028 #if 1 /* JRA TEST - Superdebug. */
1029 #define CHECK_MASK(num, am, right, sa, share) \
1030 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
1031 (unsigned int)(num), (unsigned int)(am), \
1032 (unsigned int)(right), (unsigned int)(am)&(right) )); \
1033 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
1034 (unsigned int)(num), (unsigned int)(sa), \
1035 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
1036 if (((am) & (right)) && !((sa) & (share))) { \
1037 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
1038 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
1039 (unsigned int)(share) )); \
1040 return True; \
1042 #else
1043 #define CHECK_MASK(num, am, right, sa, share) \
1044 if (((am) & (right)) && !((sa) & (share))) { \
1045 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
1046 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
1047 (unsigned int)(share) )); \
1048 return True; \
1050 #endif
1052 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
1053 share_access, FILE_SHARE_WRITE);
1054 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
1055 entry->share_access, FILE_SHARE_WRITE);
1057 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
1058 share_access, FILE_SHARE_READ);
1059 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
1060 entry->share_access, FILE_SHARE_READ);
1062 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
1063 share_access, FILE_SHARE_DELETE);
1064 CHECK_MASK(6, access_mask, DELETE_ACCESS,
1065 entry->share_access, FILE_SHARE_DELETE);
1067 DEBUG(10,("share_conflict: No conflict.\n"));
1068 return False;
1071 #if defined(DEVELOPER)
1072 static void validate_my_share_entries(struct smbd_server_connection *sconn,
1073 int num,
1074 struct share_mode_entry *share_entry)
1076 struct server_id self = messaging_server_id(sconn->msg_ctx);
1077 files_struct *fsp;
1079 if (!serverid_equal(&self, &share_entry->pid)) {
1080 return;
1083 if (share_entry->share_file_id == 0) {
1084 /* INTERNAL_OPEN_ONLY */
1085 return;
1088 if (!is_valid_share_mode_entry(share_entry)) {
1089 return;
1092 fsp = file_find_dif(sconn, share_entry->id,
1093 share_entry->share_file_id);
1094 if (!fsp) {
1095 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1096 share_mode_str(talloc_tos(), num, share_entry) ));
1097 smb_panic("validate_my_share_entries: Cannot match a "
1098 "share entry with an open file\n");
1101 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
1102 goto panic;
1105 return;
1107 panic:
1109 char *str;
1110 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1111 share_mode_str(talloc_tos(), num, share_entry) ));
1112 str = talloc_asprintf(talloc_tos(),
1113 "validate_my_share_entries: "
1114 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
1115 fsp->fsp_name->base_name,
1116 (unsigned int)fsp->oplock_type,
1117 (unsigned int)share_entry->op_type );
1118 smb_panic(str);
1121 #endif
1123 bool is_stat_open(uint32 access_mask)
1125 const uint32_t stat_open_bits =
1126 (SYNCHRONIZE_ACCESS|
1127 FILE_READ_ATTRIBUTES|
1128 FILE_WRITE_ATTRIBUTES);
1130 return (((access_mask & stat_open_bits) != 0) &&
1131 ((access_mask & ~stat_open_bits) == 0));
1134 static bool has_delete_on_close(struct share_mode_lock *lck,
1135 uint32_t name_hash)
1137 struct share_mode_data *d = lck->data;
1138 uint32_t i;
1140 if (d->num_share_modes == 0) {
1141 return false;
1143 if (!is_delete_on_close_set(lck, name_hash)) {
1144 return false;
1146 for (i=0; i<d->num_share_modes; i++) {
1147 if (!share_mode_stale_pid(d, i)) {
1148 return true;
1151 return false;
1154 /****************************************************************************
1155 Deal with share modes
1156 Invariant: Share mode must be locked on entry and exit.
1157 Returns -1 on error, or number of share modes on success (may be zero).
1158 ****************************************************************************/
1160 static NTSTATUS open_mode_check(connection_struct *conn,
1161 struct share_mode_lock *lck,
1162 uint32 access_mask,
1163 uint32 share_access)
1165 int i;
1167 if(lck->data->num_share_modes == 0) {
1168 return NT_STATUS_OK;
1171 if (is_stat_open(access_mask)) {
1172 /* Stat open that doesn't trigger oplock breaks or share mode
1173 * checks... ! JRA. */
1174 return NT_STATUS_OK;
1178 * Check if the share modes will give us access.
1181 #if defined(DEVELOPER)
1182 for(i = 0; i < lck->data->num_share_modes; i++) {
1183 validate_my_share_entries(conn->sconn, i,
1184 &lck->data->share_modes[i]);
1186 #endif
1188 /* Now we check the share modes, after any oplock breaks. */
1189 for(i = 0; i < lck->data->num_share_modes; i++) {
1191 if (!is_valid_share_mode_entry(&lck->data->share_modes[i])) {
1192 continue;
1195 /* someone else has a share lock on it, check to see if we can
1196 * too */
1197 if (share_conflict(&lck->data->share_modes[i],
1198 access_mask, share_access)) {
1200 if (share_mode_stale_pid(lck->data, i)) {
1201 continue;
1204 return NT_STATUS_SHARING_VIOLATION;
1208 return NT_STATUS_OK;
1212 * Send a break message to the oplock holder and delay the open for
1213 * our client.
1216 static NTSTATUS send_break_message(struct messaging_context *msg_ctx,
1217 const struct share_mode_entry *exclusive,
1218 uint16_t break_to)
1220 NTSTATUS status;
1221 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
1223 DEBUG(10, ("Sending break request to PID %s\n",
1224 procid_str_static(&exclusive->pid)));
1226 /* Create the message. */
1227 share_mode_entry_to_message(msg, exclusive);
1229 /* Overload entry->op_type */
1230 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET, break_to);
1232 status = messaging_send_buf(msg_ctx, exclusive->pid,
1233 MSG_SMB_BREAK_REQUEST,
1234 (uint8 *)msg, sizeof(msg));
1235 if (!NT_STATUS_IS_OK(status)) {
1236 DEBUG(3, ("Could not send oplock break message: %s\n",
1237 nt_errstr(status)));
1240 return status;
1244 * Do internal consistency checks on the share mode for a file.
1247 static bool validate_oplock_types(struct share_mode_lock *lck)
1249 struct share_mode_data *d = lck->data;
1250 bool batch = false;
1251 bool ex_or_batch = false;
1252 bool level2 = false;
1253 bool no_oplock = false;
1254 uint32_t num_non_stat_opens = 0;
1255 uint32_t i;
1257 for (i=0; i<d->num_share_modes; i++) {
1258 struct share_mode_entry *e = &d->share_modes[i];
1260 if (!is_valid_share_mode_entry(e)) {
1261 continue;
1264 if (e->op_type == NO_OPLOCK && is_stat_open(e->access_mask)) {
1265 /* We ignore stat opens in the table - they
1266 always have NO_OPLOCK and never get or
1267 cause breaks. JRA. */
1268 continue;
1271 num_non_stat_opens += 1;
1273 if (BATCH_OPLOCK_TYPE(e->op_type)) {
1274 /* batch - can only be one. */
1275 if (share_mode_stale_pid(d, i)) {
1276 DEBUG(10, ("Found stale batch oplock\n"));
1277 continue;
1279 if (ex_or_batch || batch || level2 || no_oplock) {
1280 DEBUG(0, ("Bad batch oplock entry %u.",
1281 (unsigned)i));
1282 return false;
1284 batch = true;
1287 if (EXCLUSIVE_OPLOCK_TYPE(e->op_type)) {
1288 if (share_mode_stale_pid(d, i)) {
1289 DEBUG(10, ("Found stale duplicate oplock\n"));
1290 continue;
1292 /* Exclusive or batch - can only be one. */
1293 if (ex_or_batch || level2 || no_oplock) {
1294 DEBUG(0, ("Bad exclusive or batch oplock "
1295 "entry %u.", (unsigned)i));
1296 return false;
1298 ex_or_batch = true;
1301 if (LEVEL_II_OPLOCK_TYPE(e->op_type)) {
1302 if (batch || ex_or_batch) {
1303 if (share_mode_stale_pid(d, i)) {
1304 DEBUG(10, ("Found stale LevelII "
1305 "oplock\n"));
1306 continue;
1308 DEBUG(0, ("Bad levelII oplock entry %u.",
1309 (unsigned)i));
1310 return false;
1312 level2 = true;
1315 if (e->op_type == NO_OPLOCK) {
1316 if (batch || ex_or_batch) {
1317 if (share_mode_stale_pid(d, i)) {
1318 DEBUG(10, ("Found stale NO_OPLOCK "
1319 "entry\n"));
1320 continue;
1322 DEBUG(0, ("Bad no oplock entry %u.",
1323 (unsigned)i));
1324 return false;
1326 no_oplock = true;
1330 remove_stale_share_mode_entries(d);
1332 if ((batch || ex_or_batch) && (num_non_stat_opens != 1)) {
1333 DEBUG(1, ("got batch (%d) or ex (%d) non-exclusively (%d)\n",
1334 (int)batch, (int)ex_or_batch,
1335 (int)d->num_share_modes));
1336 return false;
1339 return true;
1342 static bool delay_for_oplock(files_struct *fsp,
1343 int oplock_request,
1344 struct share_mode_lock *lck,
1345 bool have_sharing_violation,
1346 uint32_t create_disposition)
1348 struct share_mode_data *d = lck->data;
1349 struct share_mode_entry *entry;
1350 uint32_t num_non_stat_opens = 0;
1351 uint32_t i;
1352 uint16_t break_to;
1354 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1355 return false;
1357 for (i=0; i<d->num_share_modes; i++) {
1358 struct share_mode_entry *e = &d->share_modes[i];
1359 if (e->op_type == NO_OPLOCK && is_stat_open(e->access_mask)) {
1360 continue;
1362 num_non_stat_opens += 1;
1365 * We found the a non-stat open, which in the exclusive/batch
1366 * case will be inspected further down.
1368 entry = e;
1370 if (num_non_stat_opens == 0) {
1372 * Nothing to wait for around
1374 return false;
1376 if (num_non_stat_opens != 1) {
1378 * More than one open around. There can't be any exclusive or
1379 * batch left, this is all level2.
1381 return false;
1384 if (server_id_is_disconnected(&entry->pid)) {
1386 * TODO: clean up.
1387 * This could be achieved by sending a break message
1388 * to ourselves. Special considerations for files
1389 * with delete_on_close flag set!
1391 * For now we keep it simple and do not
1392 * allow delete on close for durable handles.
1394 return false;
1397 switch (create_disposition) {
1398 case FILE_SUPERSEDE:
1399 case FILE_OVERWRITE_IF:
1400 break_to = NO_OPLOCK;
1401 break;
1402 default:
1403 break_to = LEVEL_II_OPLOCK;
1404 break;
1407 if (have_sharing_violation && (entry->op_type & BATCH_OPLOCK)) {
1408 if (share_mode_stale_pid(d, 0)) {
1409 return false;
1411 send_break_message(fsp->conn->sconn->msg_ctx, entry, break_to);
1412 return true;
1414 if (have_sharing_violation) {
1416 * Non-batch exclusive is not broken if we have a sharing
1417 * violation
1419 return false;
1421 if (LEVEL_II_OPLOCK_TYPE(entry->op_type) &&
1422 (break_to == NO_OPLOCK)) {
1423 if (share_mode_stale_pid(d, 0)) {
1424 return false;
1426 DEBUG(10, ("Asynchronously breaking level2 oplock for "
1427 "create_disposition=%u\n",
1428 (unsigned)create_disposition));
1429 send_break_message(fsp->conn->sconn->msg_ctx, entry, break_to);
1430 return false;
1432 if (!EXCLUSIVE_OPLOCK_TYPE(entry->op_type)) {
1434 * No break for NO_OPLOCK or LEVEL2_OPLOCK oplocks
1436 return false;
1438 if (share_mode_stale_pid(d, 0)) {
1439 return false;
1442 send_break_message(fsp->conn->sconn->msg_ctx, entry, break_to);
1443 return true;
1446 static bool file_has_brlocks(files_struct *fsp)
1448 struct byte_range_lock *br_lck;
1450 br_lck = brl_get_locks_readonly(fsp);
1451 if (!br_lck)
1452 return false;
1454 return (brl_num_locks(br_lck) > 0);
1457 static void grant_fsp_oplock_type(files_struct *fsp,
1458 struct share_mode_lock *lck,
1459 int oplock_request)
1461 bool allow_level2 = (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
1462 lp_level2_oplocks(SNUM(fsp->conn));
1463 bool got_level2_oplock, got_a_none_oplock;
1464 uint32_t i;
1466 /* Start by granting what the client asked for,
1467 but ensure no SAMBA_PRIVATE bits can be set. */
1468 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1470 if (oplock_request & INTERNAL_OPEN_ONLY) {
1471 /* No oplocks on internal open. */
1472 fsp->oplock_type = NO_OPLOCK;
1473 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1474 fsp->oplock_type, fsp_str_dbg(fsp)));
1475 return;
1478 if (lp_locking(fsp->conn->params) && file_has_brlocks(fsp)) {
1479 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1480 fsp_str_dbg(fsp)));
1481 fsp->oplock_type = NO_OPLOCK;
1484 if (is_stat_open(fsp->access_mask)) {
1485 /* Leave the value already set. */
1486 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1487 fsp->oplock_type, fsp_str_dbg(fsp)));
1488 return;
1491 got_level2_oplock = false;
1492 got_a_none_oplock = false;
1494 for (i=0; i<lck->data->num_share_modes; i++) {
1495 int op_type = lck->data->share_modes[i].op_type;
1497 if (LEVEL_II_OPLOCK_TYPE(op_type)) {
1498 got_level2_oplock = true;
1500 if (op_type == NO_OPLOCK) {
1501 got_a_none_oplock = true;
1506 * Match what was requested (fsp->oplock_type) with
1507 * what was found in the existing share modes.
1510 if (got_level2_oplock || got_a_none_oplock) {
1511 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1512 fsp->oplock_type = LEVEL_II_OPLOCK;
1517 * Don't grant level2 to clients that don't want them
1518 * or if we've turned them off.
1520 if (fsp->oplock_type == LEVEL_II_OPLOCK && !allow_level2) {
1521 fsp->oplock_type = NO_OPLOCK;
1524 if (fsp->oplock_type == LEVEL_II_OPLOCK && !got_level2_oplock) {
1526 * We're the first level2 oplock. Indicate that in brlock.tdb.
1528 struct byte_range_lock *brl;
1530 brl = brl_get_locks(talloc_tos(), fsp);
1531 if (brl != NULL) {
1532 brl_set_have_read_oplocks(brl, true);
1533 TALLOC_FREE(brl);
1537 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1538 fsp->oplock_type, fsp_str_dbg(fsp)));
1541 static bool request_timed_out(struct timeval request_time,
1542 struct timeval timeout)
1544 struct timeval now, end_time;
1545 GetTimeOfDay(&now);
1546 end_time = timeval_sum(&request_time, &timeout);
1547 return (timeval_compare(&end_time, &now) < 0);
1550 struct defer_open_state {
1551 struct smbd_server_connection *sconn;
1552 uint64_t mid;
1555 static void defer_open_done(struct tevent_req *req);
1557 /****************************************************************************
1558 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1559 ****************************************************************************/
1561 static void defer_open(struct share_mode_lock *lck,
1562 struct timeval request_time,
1563 struct timeval timeout,
1564 struct smb_request *req,
1565 struct deferred_open_record *state)
1567 struct deferred_open_record *open_rec;
1569 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1570 "open entry for mid %llu\n",
1571 (unsigned int)request_time.tv_sec,
1572 (unsigned int)request_time.tv_usec,
1573 (unsigned long long)req->mid));
1575 open_rec = talloc(NULL, struct deferred_open_record);
1576 if (open_rec == NULL) {
1577 TALLOC_FREE(lck);
1578 exit_server("talloc failed");
1581 *open_rec = *state;
1583 if (!push_deferred_open_message_smb(req, request_time, timeout,
1584 state->id, open_rec)) {
1585 TALLOC_FREE(lck);
1586 exit_server("push_deferred_open_message_smb failed");
1588 if (lck) {
1589 struct defer_open_state *watch_state;
1590 struct tevent_req *watch_req;
1591 bool ret;
1593 watch_state = talloc(req->sconn, struct defer_open_state);
1594 if (watch_state == NULL) {
1595 exit_server("talloc failed");
1597 watch_state->sconn = req->sconn;
1598 watch_state->mid = req->mid;
1600 DEBUG(10, ("defering mid %llu\n",
1601 (unsigned long long)req->mid));
1603 watch_req = dbwrap_record_watch_send(
1604 watch_state, req->sconn->ev_ctx, lck->data->record,
1605 req->sconn->msg_ctx);
1606 if (watch_req == NULL) {
1607 exit_server("Could not watch share mode record");
1609 tevent_req_set_callback(watch_req, defer_open_done,
1610 watch_state);
1612 ret = tevent_req_set_endtime(
1613 watch_req, req->sconn->ev_ctx,
1614 timeval_sum(&request_time, &timeout));
1615 SMB_ASSERT(ret);
1619 static void defer_open_done(struct tevent_req *req)
1621 struct defer_open_state *state = tevent_req_callback_data(
1622 req, struct defer_open_state);
1623 NTSTATUS status;
1624 bool ret;
1626 status = dbwrap_record_watch_recv(req, talloc_tos(), NULL);
1627 TALLOC_FREE(req);
1628 if (!NT_STATUS_IS_OK(status)) {
1629 DEBUG(5, ("dbwrap_record_watch_recv returned %s\n",
1630 nt_errstr(status)));
1632 * Even if it failed, retry anyway. TODO: We need a way to
1633 * tell a re-scheduled open about that error.
1637 DEBUG(10, ("scheduling mid %llu\n", (unsigned long long)state->mid));
1639 ret = schedule_deferred_open_message_smb(state->sconn, state->mid);
1640 SMB_ASSERT(ret);
1641 TALLOC_FREE(state);
1645 /****************************************************************************
1646 On overwrite open ensure that the attributes match.
1647 ****************************************************************************/
1649 static bool open_match_attributes(connection_struct *conn,
1650 uint32 old_dos_attr,
1651 uint32 new_dos_attr,
1652 mode_t existing_unx_mode,
1653 mode_t new_unx_mode,
1654 mode_t *returned_unx_mode)
1656 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
1658 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1659 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1661 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
1662 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
1663 *returned_unx_mode = new_unx_mode;
1664 } else {
1665 *returned_unx_mode = (mode_t)0;
1668 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1669 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1670 "returned_unx_mode = 0%o\n",
1671 (unsigned int)old_dos_attr,
1672 (unsigned int)existing_unx_mode,
1673 (unsigned int)new_dos_attr,
1674 (unsigned int)*returned_unx_mode ));
1676 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1677 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1678 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
1679 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
1680 return False;
1683 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1684 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
1685 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
1686 return False;
1689 return True;
1692 /****************************************************************************
1693 Special FCB or DOS processing in the case of a sharing violation.
1694 Try and find a duplicated file handle.
1695 ****************************************************************************/
1697 static NTSTATUS fcb_or_dos_open(struct smb_request *req,
1698 connection_struct *conn,
1699 files_struct *fsp_to_dup_into,
1700 const struct smb_filename *smb_fname,
1701 struct file_id id,
1702 uint16 file_pid,
1703 uint64_t vuid,
1704 uint32 access_mask,
1705 uint32 share_access,
1706 uint32 create_options)
1708 files_struct *fsp;
1710 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1711 "file %s.\n", smb_fname_str_dbg(smb_fname)));
1713 for(fsp = file_find_di_first(conn->sconn, id); fsp;
1714 fsp = file_find_di_next(fsp)) {
1716 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1717 "vuid = %llu, file_pid = %u, private_options = 0x%x "
1718 "access_mask = 0x%x\n", fsp_str_dbg(fsp),
1719 fsp->fh->fd, (unsigned long long)fsp->vuid,
1720 (unsigned int)fsp->file_pid,
1721 (unsigned int)fsp->fh->private_options,
1722 (unsigned int)fsp->access_mask ));
1724 if (fsp != fsp_to_dup_into &&
1725 fsp->fh->fd != -1 &&
1726 fsp->vuid == vuid &&
1727 fsp->file_pid == file_pid &&
1728 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
1729 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
1730 (fsp->access_mask & FILE_WRITE_DATA) &&
1731 strequal(fsp->fsp_name->base_name, smb_fname->base_name) &&
1732 strequal(fsp->fsp_name->stream_name,
1733 smb_fname->stream_name)) {
1734 DEBUG(10,("fcb_or_dos_open: file match\n"));
1735 break;
1739 if (!fsp) {
1740 return NT_STATUS_NOT_FOUND;
1743 /* quite an insane set of semantics ... */
1744 if (is_executable(smb_fname->base_name) &&
1745 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1746 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1747 return NT_STATUS_INVALID_PARAMETER;
1750 /* We need to duplicate this fsp. */
1751 return dup_file_fsp(req, fsp, access_mask, share_access,
1752 create_options, fsp_to_dup_into);
1755 static void schedule_defer_open(struct share_mode_lock *lck,
1756 struct file_id id,
1757 struct timeval request_time,
1758 struct smb_request *req)
1760 struct deferred_open_record state;
1762 /* This is a relative time, added to the absolute
1763 request_time value to get the absolute timeout time.
1764 Note that if this is the second or greater time we enter
1765 this codepath for this particular request mid then
1766 request_time is left as the absolute time of the *first*
1767 time this request mid was processed. This is what allows
1768 the request to eventually time out. */
1770 struct timeval timeout;
1772 /* Normally the smbd we asked should respond within
1773 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1774 * the client did, give twice the timeout as a safety
1775 * measure here in case the other smbd is stuck
1776 * somewhere else. */
1778 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1780 /* Nothing actually uses state.delayed_for_oplocks
1781 but it's handy to differentiate in debug messages
1782 between a 30 second delay due to oplock break, and
1783 a 1 second delay for share mode conflicts. */
1785 state.delayed_for_oplocks = True;
1786 state.async_open = false;
1787 state.id = id;
1789 if (!request_timed_out(request_time, timeout)) {
1790 defer_open(lck, request_time, timeout, req, &state);
1794 /****************************************************************************
1795 Reschedule an open call that went asynchronous.
1796 ****************************************************************************/
1798 static void schedule_async_open(struct timeval request_time,
1799 struct smb_request *req)
1801 struct deferred_open_record state;
1802 struct timeval timeout;
1804 timeout = timeval_set(20, 0);
1806 ZERO_STRUCT(state);
1807 state.delayed_for_oplocks = false;
1808 state.async_open = true;
1810 if (!request_timed_out(request_time, timeout)) {
1811 defer_open(NULL, request_time, timeout, req, &state);
1815 /****************************************************************************
1816 Work out what access_mask to use from what the client sent us.
1817 ****************************************************************************/
1819 static NTSTATUS smbd_calculate_maximum_allowed_access(
1820 connection_struct *conn,
1821 const struct smb_filename *smb_fname,
1822 bool use_privs,
1823 uint32_t *p_access_mask)
1825 struct security_descriptor *sd;
1826 uint32_t access_granted;
1827 NTSTATUS status;
1829 if (!use_privs && (get_current_uid(conn) == (uid_t)0)) {
1830 *p_access_mask |= FILE_GENERIC_ALL;
1831 return NT_STATUS_OK;
1834 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
1835 (SECINFO_OWNER |
1836 SECINFO_GROUP |
1837 SECINFO_DACL),
1838 talloc_tos(), &sd);
1840 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1842 * File did not exist
1844 *p_access_mask = FILE_GENERIC_ALL;
1845 return NT_STATUS_OK;
1847 if (!NT_STATUS_IS_OK(status)) {
1848 DEBUG(10,("Could not get acl on file %s: %s\n",
1849 smb_fname_str_dbg(smb_fname),
1850 nt_errstr(status)));
1851 return NT_STATUS_ACCESS_DENIED;
1855 * If we can access the path to this file, by
1856 * default we have FILE_READ_ATTRIBUTES from the
1857 * containing directory. See the section:
1858 * "Algorithm to Check Access to an Existing File"
1859 * in MS-FSA.pdf.
1861 * se_file_access_check()
1862 * also takes care of owner WRITE_DAC and READ_CONTROL.
1864 status = se_file_access_check(sd,
1865 get_current_nttok(conn),
1866 use_privs,
1867 (*p_access_mask & ~FILE_READ_ATTRIBUTES),
1868 &access_granted);
1870 TALLOC_FREE(sd);
1872 if (!NT_STATUS_IS_OK(status)) {
1873 DEBUG(10, ("Access denied on file %s: "
1874 "when calculating maximum access\n",
1875 smb_fname_str_dbg(smb_fname)));
1876 return NT_STATUS_ACCESS_DENIED;
1878 *p_access_mask = (access_granted | FILE_READ_ATTRIBUTES);
1880 if (!(access_granted & DELETE_ACCESS)) {
1881 if (can_delete_file_in_directory(conn, smb_fname)) {
1882 *p_access_mask |= DELETE_ACCESS;
1886 return NT_STATUS_OK;
1889 NTSTATUS smbd_calculate_access_mask(connection_struct *conn,
1890 const struct smb_filename *smb_fname,
1891 bool use_privs,
1892 uint32_t access_mask,
1893 uint32_t *access_mask_out)
1895 NTSTATUS status;
1896 uint32_t orig_access_mask = access_mask;
1897 uint32_t rejected_share_access;
1900 * Convert GENERIC bits to specific bits.
1903 se_map_generic(&access_mask, &file_generic_mapping);
1905 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1906 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1908 status = smbd_calculate_maximum_allowed_access(
1909 conn, smb_fname, use_privs, &access_mask);
1911 if (!NT_STATUS_IS_OK(status)) {
1912 return status;
1915 access_mask &= conn->share_access;
1918 rejected_share_access = access_mask & ~(conn->share_access);
1920 if (rejected_share_access) {
1921 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
1922 "file %s: rejected by share access mask[0x%08X] "
1923 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
1924 smb_fname_str_dbg(smb_fname),
1925 conn->share_access,
1926 orig_access_mask, access_mask,
1927 rejected_share_access));
1928 return NT_STATUS_ACCESS_DENIED;
1931 *access_mask_out = access_mask;
1932 return NT_STATUS_OK;
1935 /****************************************************************************
1936 Remove the deferred open entry under lock.
1937 ****************************************************************************/
1939 /****************************************************************************
1940 Return true if this is a state pointer to an asynchronous create.
1941 ****************************************************************************/
1943 bool is_deferred_open_async(const struct deferred_open_record *rec)
1945 return rec->async_open;
1948 static bool clear_ads(uint32_t create_disposition)
1950 bool ret = false;
1952 switch (create_disposition) {
1953 case FILE_SUPERSEDE:
1954 case FILE_OVERWRITE_IF:
1955 case FILE_OVERWRITE:
1956 ret = true;
1957 break;
1958 default:
1959 break;
1961 return ret;
1964 static int disposition_to_open_flags(uint32_t create_disposition)
1966 int ret = 0;
1969 * Currently we're using FILE_SUPERSEDE as the same as
1970 * FILE_OVERWRITE_IF but they really are
1971 * different. FILE_SUPERSEDE deletes an existing file
1972 * (requiring delete access) then recreates it.
1975 switch (create_disposition) {
1976 case FILE_SUPERSEDE:
1977 case FILE_OVERWRITE_IF:
1979 * If file exists replace/overwrite. If file doesn't
1980 * exist create.
1982 ret = O_CREAT|O_TRUNC;
1983 break;
1985 case FILE_OPEN:
1987 * If file exists open. If file doesn't exist error.
1989 ret = 0;
1990 break;
1992 case FILE_OVERWRITE:
1994 * If file exists overwrite. If file doesn't exist
1995 * error.
1997 ret = O_TRUNC;
1998 break;
2000 case FILE_CREATE:
2002 * If file exists error. If file doesn't exist create.
2004 ret = O_CREAT|O_EXCL;
2005 break;
2007 case FILE_OPEN_IF:
2009 * If file exists open. If file doesn't exist create.
2011 ret = O_CREAT;
2012 break;
2014 return ret;
2017 static int calculate_open_access_flags(uint32_t access_mask,
2018 int oplock_request,
2019 uint32_t private_flags)
2021 bool need_write, need_read;
2024 * Note that we ignore the append flag as append does not
2025 * mean the same thing under DOS and Unix.
2028 need_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA));
2029 if (!need_write) {
2030 return O_RDONLY;
2033 /* DENY_DOS opens are always underlying read-write on the
2034 file handle, no matter what the requested access mask
2035 says. */
2037 need_read =
2038 ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
2039 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|
2040 FILE_READ_EA|FILE_EXECUTE));
2042 if (!need_read) {
2043 return O_WRONLY;
2045 return O_RDWR;
2048 /****************************************************************************
2049 Open a file with a share mode. Passed in an already created files_struct *.
2050 ****************************************************************************/
2052 static NTSTATUS open_file_ntcreate(connection_struct *conn,
2053 struct smb_request *req,
2054 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
2055 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
2056 uint32 create_disposition, /* FILE_OPEN_IF etc. */
2057 uint32 create_options, /* options such as delete on close. */
2058 uint32 new_dos_attributes, /* attributes used for new file. */
2059 int oplock_request, /* internal Samba oplock codes. */
2060 /* Information (FILE_EXISTS etc.) */
2061 uint32_t private_flags, /* Samba specific flags. */
2062 int *pinfo,
2063 files_struct *fsp)
2065 struct smb_filename *smb_fname = fsp->fsp_name;
2066 int flags=0;
2067 int flags2=0;
2068 bool file_existed = VALID_STAT(smb_fname->st);
2069 bool def_acl = False;
2070 bool posix_open = False;
2071 bool new_file_created = False;
2072 bool first_open_attempt = true;
2073 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
2074 mode_t new_unx_mode = (mode_t)0;
2075 mode_t unx_mode = (mode_t)0;
2076 int info;
2077 uint32 existing_dos_attributes = 0;
2078 struct timeval request_time = timeval_zero();
2079 struct share_mode_lock *lck = NULL;
2080 uint32 open_access_mask = access_mask;
2081 NTSTATUS status;
2082 char *parent_dir;
2083 SMB_STRUCT_STAT saved_stat = smb_fname->st;
2084 struct timespec old_write_time;
2085 struct file_id id;
2087 if (conn->printer) {
2089 * Printers are handled completely differently.
2090 * Most of the passed parameters are ignored.
2093 if (pinfo) {
2094 *pinfo = FILE_WAS_CREATED;
2097 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
2098 smb_fname_str_dbg(smb_fname)));
2100 if (!req) {
2101 DEBUG(0,("open_file_ntcreate: printer open without "
2102 "an SMB request!\n"));
2103 return NT_STATUS_INTERNAL_ERROR;
2106 return print_spool_open(fsp, smb_fname->base_name,
2107 req->vuid);
2110 if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
2111 NULL)) {
2112 return NT_STATUS_NO_MEMORY;
2115 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2116 posix_open = True;
2117 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2118 new_dos_attributes = 0;
2119 } else {
2120 /* Windows allows a new file to be created and
2121 silently removes a FILE_ATTRIBUTE_DIRECTORY
2122 sent by the client. Do the same. */
2124 new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
2126 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
2127 * created new. */
2128 unx_mode = unix_mode(conn, new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2129 smb_fname, parent_dir);
2132 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
2133 "access_mask=0x%x share_access=0x%x "
2134 "create_disposition = 0x%x create_options=0x%x "
2135 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
2136 smb_fname_str_dbg(smb_fname), new_dos_attributes,
2137 access_mask, share_access, create_disposition,
2138 create_options, (unsigned int)unx_mode, oplock_request,
2139 (unsigned int)private_flags));
2141 if (req == NULL) {
2142 /* Ensure req == NULL means INTERNAL_OPEN_ONLY */
2143 SMB_ASSERT(((oplock_request & INTERNAL_OPEN_ONLY) != 0));
2144 } else {
2145 /* And req != NULL means no INTERNAL_OPEN_ONLY */
2146 SMB_ASSERT(((oplock_request & INTERNAL_OPEN_ONLY) == 0));
2150 * Only non-internal opens can be deferred at all
2153 if (req) {
2154 struct deferred_open_record *open_rec;
2155 if (get_deferred_open_message_state(req,
2156 &request_time,
2157 &open_rec)) {
2158 /* Remember the absolute time of the original
2159 request with this mid. We'll use it later to
2160 see if this has timed out. */
2162 /* If it was an async create retry, the file
2163 didn't exist. */
2165 if (is_deferred_open_async(open_rec)) {
2166 SET_STAT_INVALID(smb_fname->st);
2167 file_existed = false;
2170 /* Ensure we don't reprocess this message. */
2171 remove_deferred_open_message_smb(req->sconn, req->mid);
2173 first_open_attempt = false;
2177 if (!posix_open) {
2178 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
2179 if (file_existed) {
2180 existing_dos_attributes = dos_mode(conn, smb_fname);
2184 /* ignore any oplock requests if oplocks are disabled */
2185 if (!lp_oplocks(SNUM(conn)) ||
2186 IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
2187 /* Mask off everything except the private Samba bits. */
2188 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
2191 /* this is for OS/2 long file names - say we don't support them */
2192 if (!lp_posix_pathnames() && strstr(smb_fname->base_name,".+,;=[].")) {
2193 /* OS/2 Workplace shell fix may be main code stream in a later
2194 * release. */
2195 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
2196 "supported.\n"));
2197 if (use_nt_status()) {
2198 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2200 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
2203 switch( create_disposition ) {
2204 case FILE_OPEN:
2205 /* If file exists open. If file doesn't exist error. */
2206 if (!file_existed) {
2207 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
2208 "requested for file %s and file "
2209 "doesn't exist.\n",
2210 smb_fname_str_dbg(smb_fname)));
2211 errno = ENOENT;
2212 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2214 break;
2216 case FILE_OVERWRITE:
2217 /* If file exists overwrite. If file doesn't exist
2218 * error. */
2219 if (!file_existed) {
2220 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
2221 "requested for file %s and file "
2222 "doesn't exist.\n",
2223 smb_fname_str_dbg(smb_fname) ));
2224 errno = ENOENT;
2225 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2227 break;
2229 case FILE_CREATE:
2230 /* If file exists error. If file doesn't exist
2231 * create. */
2232 if (file_existed) {
2233 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
2234 "requested for file %s and file "
2235 "already exists.\n",
2236 smb_fname_str_dbg(smb_fname)));
2237 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
2238 errno = EISDIR;
2239 } else {
2240 errno = EEXIST;
2242 return map_nt_error_from_unix(errno);
2244 break;
2246 case FILE_SUPERSEDE:
2247 case FILE_OVERWRITE_IF:
2248 case FILE_OPEN_IF:
2249 break;
2250 default:
2251 return NT_STATUS_INVALID_PARAMETER;
2254 flags2 = disposition_to_open_flags(create_disposition);
2256 /* We only care about matching attributes on file exists and
2257 * overwrite. */
2259 if (!posix_open && file_existed &&
2260 ((create_disposition == FILE_OVERWRITE) ||
2261 (create_disposition == FILE_OVERWRITE_IF))) {
2262 if (!open_match_attributes(conn, existing_dos_attributes,
2263 new_dos_attributes,
2264 smb_fname->st.st_ex_mode,
2265 unx_mode, &new_unx_mode)) {
2266 DEBUG(5,("open_file_ntcreate: attributes missmatch "
2267 "for file %s (%x %x) (0%o, 0%o)\n",
2268 smb_fname_str_dbg(smb_fname),
2269 existing_dos_attributes,
2270 new_dos_attributes,
2271 (unsigned int)smb_fname->st.st_ex_mode,
2272 (unsigned int)unx_mode ));
2273 errno = EACCES;
2274 return NT_STATUS_ACCESS_DENIED;
2278 status = smbd_calculate_access_mask(conn, smb_fname,
2279 false,
2280 access_mask,
2281 &access_mask);
2282 if (!NT_STATUS_IS_OK(status)) {
2283 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
2284 "on file %s returned %s\n",
2285 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
2286 return status;
2289 open_access_mask = access_mask;
2291 if (flags2 & O_TRUNC) {
2292 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
2295 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
2296 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
2297 access_mask));
2300 * Note that we ignore the append flag as append does not
2301 * mean the same thing under DOS and Unix.
2304 flags = calculate_open_access_flags(access_mask, oplock_request,
2305 private_flags);
2308 * Currently we only look at FILE_WRITE_THROUGH for create options.
2311 #if defined(O_SYNC)
2312 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
2313 flags2 |= O_SYNC;
2315 #endif /* O_SYNC */
2317 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
2318 flags2 |= O_APPEND;
2321 if (!posix_open && !CAN_WRITE(conn)) {
2323 * We should really return a permission denied error if either
2324 * O_CREAT or O_TRUNC are set, but for compatibility with
2325 * older versions of Samba we just AND them out.
2327 flags2 &= ~(O_CREAT|O_TRUNC);
2330 if (first_open_attempt && lp_kernel_oplocks(SNUM(conn))) {
2332 * With kernel oplocks the open breaking an oplock
2333 * blocks until the oplock holder has given up the
2334 * oplock or closed the file. We prevent this by first
2335 * trying to open the file with O_NONBLOCK (see "man
2336 * fcntl" on Linux). For the second try, triggered by
2337 * an oplock break response, we do not need this
2338 * anymore.
2340 * This is true under the assumption that only Samba
2341 * requests kernel oplocks. Once someone else like
2342 * NFSv4 starts to use that API, we will have to
2343 * modify this by communicating with the NFSv4 server.
2345 flags2 |= O_NONBLOCK;
2349 * Ensure we can't write on a read-only share or file.
2352 if (flags != O_RDONLY && file_existed &&
2353 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
2354 DEBUG(5,("open_file_ntcreate: write access requested for "
2355 "file %s on read only %s\n",
2356 smb_fname_str_dbg(smb_fname),
2357 !CAN_WRITE(conn) ? "share" : "file" ));
2358 errno = EACCES;
2359 return NT_STATUS_ACCESS_DENIED;
2362 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
2363 fsp->share_access = share_access;
2364 fsp->fh->private_options = private_flags;
2365 fsp->access_mask = open_access_mask; /* We change this to the
2366 * requested access_mask after
2367 * the open is done. */
2368 fsp->posix_open = posix_open;
2370 /* Ensure no SAMBA_PRIVATE bits can be set. */
2371 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
2373 if (timeval_is_zero(&request_time)) {
2374 request_time = fsp->open_time;
2378 * Ensure we pay attention to default ACLs on directories if required.
2381 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
2382 (def_acl = directory_has_default_acl(conn, parent_dir))) {
2383 unx_mode = (0777 & lp_create_mask(SNUM(conn)));
2386 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2387 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2388 (unsigned int)flags, (unsigned int)flags2,
2389 (unsigned int)unx_mode, (unsigned int)access_mask,
2390 (unsigned int)open_access_mask));
2392 fsp_open = open_file(fsp, conn, req, parent_dir,
2393 flags|flags2, unx_mode, access_mask,
2394 open_access_mask, &new_file_created);
2396 if (NT_STATUS_EQUAL(fsp_open, NT_STATUS_NETWORK_BUSY)) {
2397 struct deferred_open_record state;
2400 * EWOULDBLOCK/EAGAIN maps to NETWORK_BUSY.
2402 if (file_existed && S_ISFIFO(fsp->fsp_name->st.st_ex_mode)) {
2403 DEBUG(10, ("FIFO busy\n"));
2404 return NT_STATUS_NETWORK_BUSY;
2406 if (req == NULL) {
2407 DEBUG(10, ("Internal open busy\n"));
2408 return NT_STATUS_NETWORK_BUSY;
2412 * From here on we assume this is an oplock break triggered
2415 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
2416 if (lck == NULL) {
2417 state.delayed_for_oplocks = false;
2418 state.async_open = false;
2419 state.id = fsp->file_id;
2420 defer_open(NULL, request_time, timeval_set(0, 0),
2421 req, &state);
2422 DEBUG(10, ("No share mode lock found after "
2423 "EWOULDBLOCK, retrying sync\n"));
2424 return NT_STATUS_SHARING_VIOLATION;
2427 if (!validate_oplock_types(lck)) {
2428 smb_panic("validate_oplock_types failed");
2431 if (delay_for_oplock(fsp, 0, lck, false, create_disposition)) {
2432 schedule_defer_open(lck, fsp->file_id, request_time, req);
2433 TALLOC_FREE(lck);
2434 DEBUG(10, ("Sent oplock break request to kernel "
2435 "oplock holder\n"));
2436 return NT_STATUS_SHARING_VIOLATION;
2440 * No oplock from Samba around. Immediately retry with
2441 * a blocking open.
2443 state.delayed_for_oplocks = false;
2444 state.async_open = false;
2445 state.id = fsp->file_id;
2446 defer_open(lck, request_time, timeval_set(0, 0), req, &state);
2447 TALLOC_FREE(lck);
2448 DEBUG(10, ("No Samba oplock around after EWOULDBLOCK. "
2449 "Retrying sync\n"));
2450 return NT_STATUS_SHARING_VIOLATION;
2453 if (!NT_STATUS_IS_OK(fsp_open)) {
2454 if (NT_STATUS_EQUAL(fsp_open, NT_STATUS_RETRY)) {
2455 schedule_async_open(request_time, req);
2457 return fsp_open;
2460 if (file_existed && !check_same_dev_ino(&saved_stat, &smb_fname->st)) {
2462 * The file did exist, but some other (local or NFS)
2463 * process either renamed/unlinked and re-created the
2464 * file with different dev/ino after we walked the path,
2465 * but before we did the open. We could retry the
2466 * open but it's a rare enough case it's easier to
2467 * just fail the open to prevent creating any problems
2468 * in the open file db having the wrong dev/ino key.
2470 fd_close(fsp);
2471 DEBUG(1,("open_file_ntcreate: file %s - dev/ino mismatch. "
2472 "Old (dev=0x%llu, ino =0x%llu). "
2473 "New (dev=0x%llu, ino=0x%llu). Failing open "
2474 " with NT_STATUS_ACCESS_DENIED.\n",
2475 smb_fname_str_dbg(smb_fname),
2476 (unsigned long long)saved_stat.st_ex_dev,
2477 (unsigned long long)saved_stat.st_ex_ino,
2478 (unsigned long long)smb_fname->st.st_ex_dev,
2479 (unsigned long long)smb_fname->st.st_ex_ino));
2480 return NT_STATUS_ACCESS_DENIED;
2483 old_write_time = smb_fname->st.st_ex_mtime;
2486 * Deal with the race condition where two smbd's detect the
2487 * file doesn't exist and do the create at the same time. One
2488 * of them will win and set a share mode, the other (ie. this
2489 * one) should check if the requested share mode for this
2490 * create is allowed.
2494 * Now the file exists and fsp is successfully opened,
2495 * fsp->dev and fsp->inode are valid and should replace the
2496 * dev=0,inode=0 from a non existent file. Spotted by
2497 * Nadav Danieli <nadavd@exanet.com>. JRA.
2500 id = fsp->file_id;
2502 lck = get_share_mode_lock(talloc_tos(), id,
2503 conn->connectpath,
2504 smb_fname, &old_write_time);
2506 if (lck == NULL) {
2507 DEBUG(0, ("open_file_ntcreate: Could not get share "
2508 "mode lock for %s\n",
2509 smb_fname_str_dbg(smb_fname)));
2510 fd_close(fsp);
2511 return NT_STATUS_SHARING_VIOLATION;
2514 /* Get the types we need to examine. */
2515 if (!validate_oplock_types(lck)) {
2516 smb_panic("validate_oplock_types failed");
2519 if (has_delete_on_close(lck, fsp->name_hash)) {
2520 TALLOC_FREE(lck);
2521 fd_close(fsp);
2522 return NT_STATUS_DELETE_PENDING;
2525 status = open_mode_check(conn, lck,
2526 access_mask, share_access);
2528 if (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION) ||
2529 (lck->data->num_share_modes > 0)) {
2531 * This comes from ancient times out of open_mode_check. I
2532 * have no clue whether this is still necessary. I can't think
2533 * of a case where this would actually matter further down in
2534 * this function. I leave it here for further investigation
2535 * :-)
2537 file_existed = true;
2540 if ((req != NULL) &&
2541 delay_for_oplock(
2542 fsp, oplock_request, lck,
2543 NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION),
2544 create_disposition)) {
2545 schedule_defer_open(lck, fsp->file_id, request_time, req);
2546 TALLOC_FREE(lck);
2547 fd_close(fsp);
2548 return NT_STATUS_SHARING_VIOLATION;
2551 if (!NT_STATUS_IS_OK(status)) {
2552 uint32 can_access_mask;
2553 bool can_access = True;
2555 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
2557 /* Check if this can be done with the deny_dos and fcb
2558 * calls. */
2559 if (private_flags &
2560 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
2561 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
2562 if (req == NULL) {
2563 DEBUG(0, ("DOS open without an SMB "
2564 "request!\n"));
2565 TALLOC_FREE(lck);
2566 fd_close(fsp);
2567 return NT_STATUS_INTERNAL_ERROR;
2570 /* Use the client requested access mask here,
2571 * not the one we open with. */
2572 status = fcb_or_dos_open(req,
2573 conn,
2574 fsp,
2575 smb_fname,
2577 req->smbpid,
2578 req->vuid,
2579 access_mask,
2580 share_access,
2581 create_options);
2583 if (NT_STATUS_IS_OK(status)) {
2584 TALLOC_FREE(lck);
2585 if (pinfo) {
2586 *pinfo = FILE_WAS_OPENED;
2588 return NT_STATUS_OK;
2593 * This next line is a subtlety we need for
2594 * MS-Access. If a file open will fail due to share
2595 * permissions and also for security (access) reasons,
2596 * we need to return the access failed error, not the
2597 * share error. We can't open the file due to kernel
2598 * oplock deadlock (it's possible we failed above on
2599 * the open_mode_check()) so use a userspace check.
2602 if (flags & O_RDWR) {
2603 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
2604 } else if (flags & O_WRONLY) {
2605 can_access_mask = FILE_WRITE_DATA;
2606 } else {
2607 can_access_mask = FILE_READ_DATA;
2610 if (((can_access_mask & FILE_WRITE_DATA) &&
2611 !CAN_WRITE(conn)) ||
2612 !NT_STATUS_IS_OK(smbd_check_access_rights(conn,
2613 smb_fname,
2614 false,
2615 can_access_mask))) {
2616 can_access = False;
2620 * If we're returning a share violation, ensure we
2621 * cope with the braindead 1 second delay (SMB1 only).
2624 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
2625 !conn->sconn->using_smb2 &&
2626 lp_defer_sharing_violations()) {
2627 struct timeval timeout;
2628 struct deferred_open_record state;
2629 int timeout_usecs;
2631 /* this is a hack to speed up torture tests
2632 in 'make test' */
2633 timeout_usecs = lp_parm_int(SNUM(conn),
2634 "smbd","sharedelay",
2635 SHARING_VIOLATION_USEC_WAIT);
2637 /* This is a relative time, added to the absolute
2638 request_time value to get the absolute timeout time.
2639 Note that if this is the second or greater time we enter
2640 this codepath for this particular request mid then
2641 request_time is left as the absolute time of the *first*
2642 time this request mid was processed. This is what allows
2643 the request to eventually time out. */
2645 timeout = timeval_set(0, timeout_usecs);
2647 /* Nothing actually uses state.delayed_for_oplocks
2648 but it's handy to differentiate in debug messages
2649 between a 30 second delay due to oplock break, and
2650 a 1 second delay for share mode conflicts. */
2652 state.delayed_for_oplocks = False;
2653 state.async_open = false;
2654 state.id = id;
2656 if ((req != NULL)
2657 && !request_timed_out(request_time,
2658 timeout)) {
2659 defer_open(lck, request_time, timeout,
2660 req, &state);
2664 TALLOC_FREE(lck);
2665 fd_close(fsp);
2666 if (can_access) {
2668 * We have detected a sharing violation here
2669 * so return the correct error code
2671 status = NT_STATUS_SHARING_VIOLATION;
2672 } else {
2673 status = NT_STATUS_ACCESS_DENIED;
2675 return status;
2678 grant_fsp_oplock_type(fsp, lck, oplock_request);
2681 * We have the share entry *locked*.....
2684 /* Delete streams if create_disposition requires it */
2685 if (!new_file_created && clear_ads(create_disposition) &&
2686 !is_ntfs_stream_smb_fname(smb_fname)) {
2687 status = delete_all_streams(conn, smb_fname->base_name);
2688 if (!NT_STATUS_IS_OK(status)) {
2689 TALLOC_FREE(lck);
2690 fd_close(fsp);
2691 return status;
2695 /* note that we ignore failure for the following. It is
2696 basically a hack for NFS, and NFS will never set one of
2697 these only read them. Nobody but Samba can ever set a deny
2698 mode and we have already checked our more authoritative
2699 locking database for permission to set this deny mode. If
2700 the kernel refuses the operations then the kernel is wrong.
2701 note that GPFS supports it as well - jmcd */
2703 if (fsp->fh->fd != -1 && lp_kernel_share_modes(SNUM(conn))) {
2704 int ret_flock;
2705 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
2706 if(ret_flock == -1 ){
2708 TALLOC_FREE(lck);
2709 fd_close(fsp);
2711 return NT_STATUS_SHARING_VIOLATION;
2716 * At this point onwards, we can guarantee that the share entry
2717 * is locked, whether we created the file or not, and that the
2718 * deny mode is compatible with all current opens.
2722 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2723 * but we don't have to store this - just ignore it on access check.
2725 if (conn->sconn->using_smb2) {
2727 * SMB2 doesn't return it (according to Microsoft tests).
2728 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2729 * File created with access = 0x7 (Read, Write, Delete)
2730 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2732 fsp->access_mask = access_mask;
2733 } else {
2734 /* But SMB1 does. */
2735 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2738 if (file_existed) {
2739 /* stat opens on existing files don't get oplocks. */
2740 if (is_stat_open(open_access_mask)) {
2741 fsp->oplock_type = NO_OPLOCK;
2745 if (new_file_created) {
2746 info = FILE_WAS_CREATED;
2747 } else {
2748 if (flags2 & O_TRUNC) {
2749 info = FILE_WAS_OVERWRITTEN;
2750 } else {
2751 info = FILE_WAS_OPENED;
2755 if (pinfo) {
2756 *pinfo = info;
2760 * Setup the oplock info in both the shared memory and
2761 * file structs.
2764 status = set_file_oplock(fsp);
2765 if (!NT_STATUS_IS_OK(status)) {
2767 * Could not get the kernel oplock
2769 fsp->oplock_type = NO_OPLOCK;
2772 if (!set_share_mode(lck, fsp, get_current_uid(conn),
2773 req ? req->mid : 0,
2774 fsp->oplock_type)) {
2775 TALLOC_FREE(lck);
2776 fd_close(fsp);
2777 return NT_STATUS_NO_MEMORY;
2780 /* Handle strange delete on close create semantics. */
2781 if (create_options & FILE_DELETE_ON_CLOSE) {
2783 status = can_set_delete_on_close(fsp, new_dos_attributes);
2785 if (!NT_STATUS_IS_OK(status)) {
2786 /* Remember to delete the mode we just added. */
2787 del_share_mode(lck, fsp);
2788 TALLOC_FREE(lck);
2789 fd_close(fsp);
2790 return status;
2792 /* Note that here we set the *inital* delete on close flag,
2793 not the regular one. The magic gets handled in close. */
2794 fsp->initial_delete_on_close = True;
2797 if (info != FILE_WAS_OPENED) {
2798 /* Files should be initially set as archive */
2799 if (lp_map_archive(SNUM(conn)) ||
2800 lp_store_dos_attributes(SNUM(conn))) {
2801 if (!posix_open) {
2802 if (file_set_dosmode(conn, smb_fname,
2803 new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2804 parent_dir, true) == 0) {
2805 unx_mode = smb_fname->st.st_ex_mode;
2811 /* Determine sparse flag. */
2812 if (posix_open) {
2813 /* POSIX opens are sparse by default. */
2814 fsp->is_sparse = true;
2815 } else {
2816 fsp->is_sparse = (file_existed &&
2817 (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE));
2821 * Take care of inherited ACLs on created files - if default ACL not
2822 * selected.
2825 if (!posix_open && new_file_created && !def_acl) {
2827 int saved_errno = errno; /* We might get ENOSYS in the next
2828 * call.. */
2830 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2831 errno == ENOSYS) {
2832 errno = saved_errno; /* Ignore ENOSYS */
2835 } else if (new_unx_mode) {
2837 int ret = -1;
2839 /* Attributes need changing. File already existed. */
2842 int saved_errno = errno; /* We might get ENOSYS in the
2843 * next call.. */
2844 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2846 if (ret == -1 && errno == ENOSYS) {
2847 errno = saved_errno; /* Ignore ENOSYS */
2848 } else {
2849 DEBUG(5, ("open_file_ntcreate: reset "
2850 "attributes of file %s to 0%o\n",
2851 smb_fname_str_dbg(smb_fname),
2852 (unsigned int)new_unx_mode));
2853 ret = 0; /* Don't do the fchmod below. */
2857 if ((ret == -1) &&
2858 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2859 DEBUG(5, ("open_file_ntcreate: failed to reset "
2860 "attributes of file %s to 0%o\n",
2861 smb_fname_str_dbg(smb_fname),
2862 (unsigned int)new_unx_mode));
2867 * Deal with other opens having a modified write time.
2869 struct timespec write_time = get_share_mode_write_time(lck);
2871 if (!null_timespec(write_time)) {
2872 update_stat_ex_mtime(&fsp->fsp_name->st, write_time);
2876 TALLOC_FREE(lck);
2878 return NT_STATUS_OK;
2881 static NTSTATUS mkdir_internal(connection_struct *conn,
2882 struct smb_filename *smb_dname,
2883 uint32 file_attributes)
2885 mode_t mode;
2886 char *parent_dir = NULL;
2887 NTSTATUS status;
2888 bool posix_open = false;
2889 bool need_re_stat = false;
2890 uint32_t access_mask = SEC_DIR_ADD_SUBDIR;
2892 if (!CAN_WRITE(conn) || (access_mask & ~(conn->share_access))) {
2893 DEBUG(5,("mkdir_internal: failing share access "
2894 "%s\n", lp_servicename(talloc_tos(), SNUM(conn))));
2895 return NT_STATUS_ACCESS_DENIED;
2898 if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
2899 NULL)) {
2900 return NT_STATUS_NO_MEMORY;
2903 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2904 posix_open = true;
2905 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2906 } else {
2907 mode = unix_mode(conn, FILE_ATTRIBUTE_DIRECTORY, smb_dname, parent_dir);
2910 status = check_parent_access(conn,
2911 smb_dname,
2912 access_mask);
2913 if(!NT_STATUS_IS_OK(status)) {
2914 DEBUG(5,("mkdir_internal: check_parent_access "
2915 "on directory %s for path %s returned %s\n",
2916 parent_dir,
2917 smb_dname->base_name,
2918 nt_errstr(status) ));
2919 return status;
2922 if (SMB_VFS_MKDIR(conn, smb_dname->base_name, mode) != 0) {
2923 return map_nt_error_from_unix(errno);
2926 /* Ensure we're checking for a symlink here.... */
2927 /* We don't want to get caught by a symlink racer. */
2929 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2930 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2931 smb_fname_str_dbg(smb_dname), strerror(errno)));
2932 return map_nt_error_from_unix(errno);
2935 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
2936 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
2937 smb_fname_str_dbg(smb_dname)));
2938 return NT_STATUS_NOT_A_DIRECTORY;
2941 if (lp_store_dos_attributes(SNUM(conn))) {
2942 if (!posix_open) {
2943 file_set_dosmode(conn, smb_dname,
2944 file_attributes | FILE_ATTRIBUTE_DIRECTORY,
2945 parent_dir, true);
2949 if (lp_inherit_permissions(SNUM(conn))) {
2950 inherit_access_posix_acl(conn, parent_dir,
2951 smb_dname->base_name, mode);
2952 need_re_stat = true;
2955 if (!posix_open) {
2957 * Check if high bits should have been set,
2958 * then (if bits are missing): add them.
2959 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2960 * dir.
2962 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
2963 (mode & ~smb_dname->st.st_ex_mode)) {
2964 SMB_VFS_CHMOD(conn, smb_dname->base_name,
2965 (smb_dname->st.st_ex_mode |
2966 (mode & ~smb_dname->st.st_ex_mode)));
2967 need_re_stat = true;
2971 /* Change the owner if required. */
2972 if (lp_inherit_owner(SNUM(conn))) {
2973 change_dir_owner_to_parent(conn, parent_dir,
2974 smb_dname->base_name,
2975 &smb_dname->st);
2976 need_re_stat = true;
2979 if (need_re_stat) {
2980 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2981 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2982 smb_fname_str_dbg(smb_dname), strerror(errno)));
2983 return map_nt_error_from_unix(errno);
2987 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2988 smb_dname->base_name);
2990 return NT_STATUS_OK;
2993 /****************************************************************************
2994 Open a directory from an NT SMB call.
2995 ****************************************************************************/
2997 static NTSTATUS open_directory(connection_struct *conn,
2998 struct smb_request *req,
2999 struct smb_filename *smb_dname,
3000 uint32 access_mask,
3001 uint32 share_access,
3002 uint32 create_disposition,
3003 uint32 create_options,
3004 uint32 file_attributes,
3005 int *pinfo,
3006 files_struct **result)
3008 files_struct *fsp = NULL;
3009 bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
3010 struct share_mode_lock *lck = NULL;
3011 NTSTATUS status;
3012 struct timespec mtimespec;
3013 int info = 0;
3015 if (is_ntfs_stream_smb_fname(smb_dname)) {
3016 DEBUG(2, ("open_directory: %s is a stream name!\n",
3017 smb_fname_str_dbg(smb_dname)));
3018 return NT_STATUS_NOT_A_DIRECTORY;
3021 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
3022 /* Ensure we have a directory attribute. */
3023 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
3026 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
3027 "share_access = 0x%x create_options = 0x%x, "
3028 "create_disposition = 0x%x, file_attributes = 0x%x\n",
3029 smb_fname_str_dbg(smb_dname),
3030 (unsigned int)access_mask,
3031 (unsigned int)share_access,
3032 (unsigned int)create_options,
3033 (unsigned int)create_disposition,
3034 (unsigned int)file_attributes));
3036 status = smbd_calculate_access_mask(conn, smb_dname, false,
3037 access_mask, &access_mask);
3038 if (!NT_STATUS_IS_OK(status)) {
3039 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
3040 "on file %s returned %s\n",
3041 smb_fname_str_dbg(smb_dname),
3042 nt_errstr(status)));
3043 return status;
3046 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3047 !security_token_has_privilege(get_current_nttok(conn),
3048 SEC_PRIV_SECURITY)) {
3049 DEBUG(10, ("open_directory: open on %s "
3050 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3051 smb_fname_str_dbg(smb_dname)));
3052 return NT_STATUS_PRIVILEGE_NOT_HELD;
3055 switch( create_disposition ) {
3056 case FILE_OPEN:
3058 if (!dir_existed) {
3059 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3062 info = FILE_WAS_OPENED;
3063 break;
3065 case FILE_CREATE:
3067 /* If directory exists error. If directory doesn't
3068 * exist create. */
3070 if (dir_existed) {
3071 status = NT_STATUS_OBJECT_NAME_COLLISION;
3072 DEBUG(2, ("open_directory: unable to create "
3073 "%s. Error was %s\n",
3074 smb_fname_str_dbg(smb_dname),
3075 nt_errstr(status)));
3076 return status;
3079 status = mkdir_internal(conn, smb_dname,
3080 file_attributes);
3082 if (!NT_STATUS_IS_OK(status)) {
3083 DEBUG(2, ("open_directory: unable to create "
3084 "%s. Error was %s\n",
3085 smb_fname_str_dbg(smb_dname),
3086 nt_errstr(status)));
3087 return status;
3090 info = FILE_WAS_CREATED;
3091 break;
3093 case FILE_OPEN_IF:
3095 * If directory exists open. If directory doesn't
3096 * exist create.
3099 if (dir_existed) {
3100 status = NT_STATUS_OK;
3101 info = FILE_WAS_OPENED;
3102 } else {
3103 status = mkdir_internal(conn, smb_dname,
3104 file_attributes);
3106 if (NT_STATUS_IS_OK(status)) {
3107 info = FILE_WAS_CREATED;
3108 } else {
3109 /* Cope with create race. */
3110 if (!NT_STATUS_EQUAL(status,
3111 NT_STATUS_OBJECT_NAME_COLLISION)) {
3112 DEBUG(2, ("open_directory: unable to create "
3113 "%s. Error was %s\n",
3114 smb_fname_str_dbg(smb_dname),
3115 nt_errstr(status)));
3116 return status;
3118 info = FILE_WAS_OPENED;
3122 break;
3124 case FILE_SUPERSEDE:
3125 case FILE_OVERWRITE:
3126 case FILE_OVERWRITE_IF:
3127 default:
3128 DEBUG(5,("open_directory: invalid create_disposition "
3129 "0x%x for directory %s\n",
3130 (unsigned int)create_disposition,
3131 smb_fname_str_dbg(smb_dname)));
3132 return NT_STATUS_INVALID_PARAMETER;
3135 if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
3136 DEBUG(5,("open_directory: %s is not a directory !\n",
3137 smb_fname_str_dbg(smb_dname)));
3138 return NT_STATUS_NOT_A_DIRECTORY;
3141 if (info == FILE_WAS_OPENED) {
3142 status = smbd_check_access_rights(conn,
3143 smb_dname,
3144 false,
3145 access_mask);
3146 if (!NT_STATUS_IS_OK(status)) {
3147 DEBUG(10, ("open_directory: smbd_check_access_rights on "
3148 "file %s failed with %s\n",
3149 smb_fname_str_dbg(smb_dname),
3150 nt_errstr(status)));
3151 return status;
3155 status = file_new(req, conn, &fsp);
3156 if(!NT_STATUS_IS_OK(status)) {
3157 return status;
3161 * Setup the files_struct for it.
3164 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
3165 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
3166 fsp->file_pid = req ? req->smbpid : 0;
3167 fsp->can_lock = False;
3168 fsp->can_read = False;
3169 fsp->can_write = False;
3171 fsp->share_access = share_access;
3172 fsp->fh->private_options = 0;
3174 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
3176 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
3177 fsp->print_file = NULL;
3178 fsp->modified = False;
3179 fsp->oplock_type = NO_OPLOCK;
3180 fsp->sent_oplock_break = NO_BREAK_SENT;
3181 fsp->is_directory = True;
3182 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
3183 status = fsp_set_smb_fname(fsp, smb_dname);
3184 if (!NT_STATUS_IS_OK(status)) {
3185 file_free(req, fsp);
3186 return status;
3189 /* Don't store old timestamps for directory
3190 handles in the internal database. We don't
3191 update them in there if new objects
3192 are creaded in the directory. Currently
3193 we only update timestamps on file writes.
3194 See bug #9870.
3196 ZERO_STRUCT(mtimespec);
3198 if (access_mask & (FILE_LIST_DIRECTORY|
3199 FILE_ADD_FILE|
3200 FILE_ADD_SUBDIRECTORY|
3201 FILE_TRAVERSE|
3202 DELETE_ACCESS|
3203 FILE_DELETE_CHILD)) {
3204 #ifdef O_DIRECTORY
3205 status = fd_open(conn, fsp, O_RDONLY|O_DIRECTORY, 0);
3206 #else
3207 /* POSIX allows us to open a directory with O_RDONLY. */
3208 status = fd_open(conn, fsp, O_RDONLY, 0);
3209 #endif
3210 if (!NT_STATUS_IS_OK(status)) {
3211 DEBUG(5, ("open_directory: Could not open fd for "
3212 "%s (%s)\n",
3213 smb_fname_str_dbg(smb_dname),
3214 nt_errstr(status)));
3215 file_free(req, fsp);
3216 return status;
3218 } else {
3219 fsp->fh->fd = -1;
3220 DEBUG(10, ("Not opening Directory %s\n",
3221 smb_fname_str_dbg(smb_dname)));
3224 status = vfs_stat_fsp(fsp);
3225 if (!NT_STATUS_IS_OK(status)) {
3226 fd_close(fsp);
3227 file_free(req, fsp);
3228 return status;
3231 /* Ensure there was no race condition. */
3232 if (!check_same_stat(&smb_dname->st, &fsp->fsp_name->st)) {
3233 DEBUG(5,("open_directory: stat struct differs for "
3234 "directory %s.\n",
3235 smb_fname_str_dbg(smb_dname)));
3236 fd_close(fsp);
3237 file_free(req, fsp);
3238 return NT_STATUS_ACCESS_DENIED;
3241 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
3242 conn->connectpath, smb_dname,
3243 &mtimespec);
3245 if (lck == NULL) {
3246 DEBUG(0, ("open_directory: Could not get share mode lock for "
3247 "%s\n", smb_fname_str_dbg(smb_dname)));
3248 fd_close(fsp);
3249 file_free(req, fsp);
3250 return NT_STATUS_SHARING_VIOLATION;
3253 if (has_delete_on_close(lck, fsp->name_hash)) {
3254 TALLOC_FREE(lck);
3255 fd_close(fsp);
3256 file_free(req, fsp);
3257 return NT_STATUS_DELETE_PENDING;
3260 status = open_mode_check(conn, lck,
3261 access_mask, share_access);
3263 if (!NT_STATUS_IS_OK(status)) {
3264 TALLOC_FREE(lck);
3265 fd_close(fsp);
3266 file_free(req, fsp);
3267 return status;
3270 if (!set_share_mode(lck, fsp, get_current_uid(conn),
3271 req ? req->mid : 0, NO_OPLOCK)) {
3272 TALLOC_FREE(lck);
3273 fd_close(fsp);
3274 file_free(req, fsp);
3275 return NT_STATUS_NO_MEMORY;
3278 /* For directories the delete on close bit at open time seems
3279 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
3280 if (create_options & FILE_DELETE_ON_CLOSE) {
3281 status = can_set_delete_on_close(fsp, 0);
3282 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
3283 del_share_mode(lck, fsp);
3284 TALLOC_FREE(lck);
3285 fd_close(fsp);
3286 file_free(req, fsp);
3287 return status;
3290 if (NT_STATUS_IS_OK(status)) {
3291 /* Note that here we set the *inital* delete on close flag,
3292 not the regular one. The magic gets handled in close. */
3293 fsp->initial_delete_on_close = True;
3299 * Deal with other opens having a modified write time. Is this
3300 * possible for directories?
3302 struct timespec write_time = get_share_mode_write_time(lck);
3304 if (!null_timespec(write_time)) {
3305 update_stat_ex_mtime(&fsp->fsp_name->st, write_time);
3309 TALLOC_FREE(lck);
3311 if (pinfo) {
3312 *pinfo = info;
3315 *result = fsp;
3316 return NT_STATUS_OK;
3319 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
3320 struct smb_filename *smb_dname)
3322 NTSTATUS status;
3323 files_struct *fsp;
3325 status = SMB_VFS_CREATE_FILE(
3326 conn, /* conn */
3327 req, /* req */
3328 0, /* root_dir_fid */
3329 smb_dname, /* fname */
3330 FILE_READ_ATTRIBUTES, /* access_mask */
3331 FILE_SHARE_NONE, /* share_access */
3332 FILE_CREATE, /* create_disposition*/
3333 FILE_DIRECTORY_FILE, /* create_options */
3334 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
3335 0, /* oplock_request */
3336 0, /* allocation_size */
3337 0, /* private_flags */
3338 NULL, /* sd */
3339 NULL, /* ea_list */
3340 &fsp, /* result */
3341 NULL); /* pinfo */
3343 if (NT_STATUS_IS_OK(status)) {
3344 close_file(req, fsp, NORMAL_CLOSE);
3347 return status;
3350 /****************************************************************************
3351 Receive notification that one of our open files has been renamed by another
3352 smbd process.
3353 ****************************************************************************/
3355 void msg_file_was_renamed(struct messaging_context *msg,
3356 void *private_data,
3357 uint32_t msg_type,
3358 struct server_id server_id,
3359 DATA_BLOB *data)
3361 files_struct *fsp;
3362 char *frm = (char *)data->data;
3363 struct file_id id;
3364 const char *sharepath;
3365 const char *base_name;
3366 const char *stream_name;
3367 struct smb_filename *smb_fname = NULL;
3368 size_t sp_len, bn_len;
3369 NTSTATUS status;
3370 struct smbd_server_connection *sconn =
3371 talloc_get_type_abort(private_data,
3372 struct smbd_server_connection);
3374 if (data->data == NULL
3375 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
3376 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
3377 (int)data->length));
3378 return;
3381 /* Unpack the message. */
3382 pull_file_id_24(frm, &id);
3383 sharepath = &frm[24];
3384 sp_len = strlen(sharepath);
3385 base_name = sharepath + sp_len + 1;
3386 bn_len = strlen(base_name);
3387 stream_name = sharepath + sp_len + 1 + bn_len + 1;
3389 /* stream_name must always be NULL if there is no stream. */
3390 if (stream_name[0] == '\0') {
3391 stream_name = NULL;
3394 smb_fname = synthetic_smb_fname(talloc_tos(), base_name,
3395 stream_name, NULL);
3396 if (smb_fname == NULL) {
3397 return;
3400 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3401 "file_id %s\n",
3402 sharepath, smb_fname_str_dbg(smb_fname),
3403 file_id_string_tos(&id)));
3405 for(fsp = file_find_di_first(sconn, id); fsp;
3406 fsp = file_find_di_next(fsp)) {
3407 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
3409 DEBUG(10,("msg_file_was_renamed: renaming file %s from %s -> %s\n",
3410 fsp_fnum_dbg(fsp), fsp_str_dbg(fsp),
3411 smb_fname_str_dbg(smb_fname)));
3412 status = fsp_set_smb_fname(fsp, smb_fname);
3413 if (!NT_STATUS_IS_OK(status)) {
3414 goto out;
3416 } else {
3417 /* TODO. JRA. */
3418 /* Now we have the complete path we can work out if this is
3419 actually within this share and adjust newname accordingly. */
3420 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3421 "not sharepath %s) "
3422 "%s from %s -> %s\n",
3423 fsp->conn->connectpath,
3424 sharepath,
3425 fsp_fnum_dbg(fsp),
3426 fsp_str_dbg(fsp),
3427 smb_fname_str_dbg(smb_fname)));
3430 out:
3431 TALLOC_FREE(smb_fname);
3432 return;
3436 * If a main file is opened for delete, all streams need to be checked for
3437 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3438 * If that works, delete them all by setting the delete on close and close.
3441 NTSTATUS open_streams_for_delete(connection_struct *conn,
3442 const char *fname)
3444 struct stream_struct *stream_info = NULL;
3445 files_struct **streams = NULL;
3446 int i;
3447 unsigned int num_streams = 0;
3448 TALLOC_CTX *frame = talloc_stackframe();
3449 NTSTATUS status;
3451 status = vfs_streaminfo(conn, NULL, fname, talloc_tos(),
3452 &num_streams, &stream_info);
3454 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
3455 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
3456 DEBUG(10, ("no streams around\n"));
3457 TALLOC_FREE(frame);
3458 return NT_STATUS_OK;
3461 if (!NT_STATUS_IS_OK(status)) {
3462 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3463 nt_errstr(status)));
3464 goto fail;
3467 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3468 num_streams));
3470 if (num_streams == 0) {
3471 TALLOC_FREE(frame);
3472 return NT_STATUS_OK;
3475 streams = talloc_array(talloc_tos(), files_struct *, num_streams);
3476 if (streams == NULL) {
3477 DEBUG(0, ("talloc failed\n"));
3478 status = NT_STATUS_NO_MEMORY;
3479 goto fail;
3482 for (i=0; i<num_streams; i++) {
3483 struct smb_filename *smb_fname;
3485 if (strequal(stream_info[i].name, "::$DATA")) {
3486 streams[i] = NULL;
3487 continue;
3490 smb_fname = synthetic_smb_fname(
3491 talloc_tos(), fname, stream_info[i].name, NULL);
3492 if (smb_fname == NULL) {
3493 status = NT_STATUS_NO_MEMORY;
3494 goto fail;
3497 if (SMB_VFS_STAT(conn, smb_fname) == -1) {
3498 DEBUG(10, ("Unable to stat stream: %s\n",
3499 smb_fname_str_dbg(smb_fname)));
3502 status = SMB_VFS_CREATE_FILE(
3503 conn, /* conn */
3504 NULL, /* req */
3505 0, /* root_dir_fid */
3506 smb_fname, /* fname */
3507 DELETE_ACCESS, /* access_mask */
3508 (FILE_SHARE_READ | /* share_access */
3509 FILE_SHARE_WRITE | FILE_SHARE_DELETE),
3510 FILE_OPEN, /* create_disposition*/
3511 0, /* create_options */
3512 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
3513 0, /* oplock_request */
3514 0, /* allocation_size */
3515 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */
3516 NULL, /* sd */
3517 NULL, /* ea_list */
3518 &streams[i], /* result */
3519 NULL); /* pinfo */
3521 if (!NT_STATUS_IS_OK(status)) {
3522 DEBUG(10, ("Could not open stream %s: %s\n",
3523 smb_fname_str_dbg(smb_fname),
3524 nt_errstr(status)));
3526 TALLOC_FREE(smb_fname);
3527 break;
3529 TALLOC_FREE(smb_fname);
3533 * don't touch the variable "status" beyond this point :-)
3536 for (i -= 1 ; i >= 0; i--) {
3537 if (streams[i] == NULL) {
3538 continue;
3541 DEBUG(10, ("Closing stream # %d, %s\n", i,
3542 fsp_str_dbg(streams[i])));
3543 close_file(NULL, streams[i], NORMAL_CLOSE);
3546 fail:
3547 TALLOC_FREE(frame);
3548 return status;
3551 /*********************************************************************
3552 Create a default ACL by inheriting from the parent. If no inheritance
3553 from the parent available, don't set anything. This will leave the actual
3554 permissions the new file or directory already got from the filesystem
3555 as the NT ACL when read.
3556 *********************************************************************/
3558 static NTSTATUS inherit_new_acl(files_struct *fsp)
3560 TALLOC_CTX *frame = talloc_stackframe();
3561 char *parent_name = NULL;
3562 struct security_descriptor *parent_desc = NULL;
3563 NTSTATUS status = NT_STATUS_OK;
3564 struct security_descriptor *psd = NULL;
3565 const struct dom_sid *owner_sid = NULL;
3566 const struct dom_sid *group_sid = NULL;
3567 uint32_t security_info_sent = (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL);
3568 struct security_token *token = fsp->conn->session_info->security_token;
3569 bool inherit_owner = lp_inherit_owner(SNUM(fsp->conn));
3570 bool inheritable_components = false;
3571 bool try_builtin_administrators = false;
3572 const struct dom_sid *BA_U_sid = NULL;
3573 const struct dom_sid *BA_G_sid = NULL;
3574 bool try_system = false;
3575 const struct dom_sid *SY_U_sid = NULL;
3576 const struct dom_sid *SY_G_sid = NULL;
3577 size_t size = 0;
3579 if (!parent_dirname(frame, fsp->fsp_name->base_name, &parent_name, NULL)) {
3580 TALLOC_FREE(frame);
3581 return NT_STATUS_NO_MEMORY;
3584 status = SMB_VFS_GET_NT_ACL(fsp->conn,
3585 parent_name,
3586 (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL),
3587 frame,
3588 &parent_desc);
3589 if (!NT_STATUS_IS_OK(status)) {
3590 TALLOC_FREE(frame);
3591 return status;
3594 inheritable_components = sd_has_inheritable_components(parent_desc,
3595 fsp->is_directory);
3597 if (!inheritable_components && !inherit_owner) {
3598 TALLOC_FREE(frame);
3599 /* Nothing to inherit and not setting owner. */
3600 return NT_STATUS_OK;
3603 /* Create an inherited descriptor from the parent. */
3605 if (DEBUGLEVEL >= 10) {
3606 DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
3607 fsp_str_dbg(fsp) ));
3608 NDR_PRINT_DEBUG(security_descriptor, parent_desc);
3611 /* Inherit from parent descriptor if "inherit owner" set. */
3612 if (inherit_owner) {
3613 owner_sid = parent_desc->owner_sid;
3614 group_sid = parent_desc->group_sid;
3617 if (owner_sid == NULL) {
3618 if (security_token_has_builtin_administrators(token)) {
3619 try_builtin_administrators = true;
3620 } else if (security_token_is_system(token)) {
3621 try_builtin_administrators = true;
3622 try_system = true;
3626 if (group_sid == NULL &&
3627 token->num_sids == PRIMARY_GROUP_SID_INDEX)
3629 if (security_token_is_system(token)) {
3630 try_builtin_administrators = true;
3631 try_system = true;
3635 if (try_builtin_administrators) {
3636 struct unixid ids;
3637 bool ok;
3639 ZERO_STRUCT(ids);
3640 ok = sids_to_unixids(&global_sid_Builtin_Administrators, 1, &ids);
3641 if (ok) {
3642 switch (ids.type) {
3643 case ID_TYPE_BOTH:
3644 BA_U_sid = &global_sid_Builtin_Administrators;
3645 BA_G_sid = &global_sid_Builtin_Administrators;
3646 break;
3647 case ID_TYPE_UID:
3648 BA_U_sid = &global_sid_Builtin_Administrators;
3649 break;
3650 case ID_TYPE_GID:
3651 BA_G_sid = &global_sid_Builtin_Administrators;
3652 break;
3653 default:
3654 break;
3659 if (try_system) {
3660 struct unixid ids;
3661 bool ok;
3663 ZERO_STRUCT(ids);
3664 ok = sids_to_unixids(&global_sid_System, 1, &ids);
3665 if (ok) {
3666 switch (ids.type) {
3667 case ID_TYPE_BOTH:
3668 SY_U_sid = &global_sid_System;
3669 SY_G_sid = &global_sid_System;
3670 break;
3671 case ID_TYPE_UID:
3672 SY_U_sid = &global_sid_System;
3673 break;
3674 case ID_TYPE_GID:
3675 SY_G_sid = &global_sid_System;
3676 break;
3677 default:
3678 break;
3683 if (owner_sid == NULL) {
3684 owner_sid = BA_U_sid;
3687 if (owner_sid == NULL) {
3688 owner_sid = SY_U_sid;
3691 if (group_sid == NULL) {
3692 group_sid = SY_G_sid;
3695 if (try_system && group_sid == NULL) {
3696 group_sid = BA_G_sid;
3699 if (owner_sid == NULL) {
3700 owner_sid = &token->sids[PRIMARY_USER_SID_INDEX];
3702 if (group_sid == NULL) {
3703 if (token->num_sids == PRIMARY_GROUP_SID_INDEX) {
3704 group_sid = &token->sids[PRIMARY_USER_SID_INDEX];
3705 } else {
3706 group_sid = &token->sids[PRIMARY_GROUP_SID_INDEX];
3710 status = se_create_child_secdesc(frame,
3711 &psd,
3712 &size,
3713 parent_desc,
3714 owner_sid,
3715 group_sid,
3716 fsp->is_directory);
3717 if (!NT_STATUS_IS_OK(status)) {
3718 TALLOC_FREE(frame);
3719 return status;
3722 /* If inheritable_components == false,
3723 se_create_child_secdesc()
3724 creates a security desriptor with a NULL dacl
3725 entry, but with SEC_DESC_DACL_PRESENT. We need
3726 to remove that flag. */
3728 if (!inheritable_components) {
3729 security_info_sent &= ~SECINFO_DACL;
3730 psd->type &= ~SEC_DESC_DACL_PRESENT;
3733 if (DEBUGLEVEL >= 10) {
3734 DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
3735 fsp_str_dbg(fsp) ));
3736 NDR_PRINT_DEBUG(security_descriptor, psd);
3739 if (inherit_owner) {
3740 /* We need to be root to force this. */
3741 become_root();
3743 status = SMB_VFS_FSET_NT_ACL(fsp,
3744 security_info_sent,
3745 psd);
3746 if (inherit_owner) {
3747 unbecome_root();
3749 TALLOC_FREE(frame);
3750 return status;
3754 * Wrapper around open_file_ntcreate and open_directory
3757 static NTSTATUS create_file_unixpath(connection_struct *conn,
3758 struct smb_request *req,
3759 struct smb_filename *smb_fname,
3760 uint32_t access_mask,
3761 uint32_t share_access,
3762 uint32_t create_disposition,
3763 uint32_t create_options,
3764 uint32_t file_attributes,
3765 uint32_t oplock_request,
3766 uint64_t allocation_size,
3767 uint32_t private_flags,
3768 struct security_descriptor *sd,
3769 struct ea_list *ea_list,
3771 files_struct **result,
3772 int *pinfo)
3774 int info = FILE_WAS_OPENED;
3775 files_struct *base_fsp = NULL;
3776 files_struct *fsp = NULL;
3777 NTSTATUS status;
3779 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3780 "file_attributes = 0x%x, share_access = 0x%x, "
3781 "create_disposition = 0x%x create_options = 0x%x "
3782 "oplock_request = 0x%x private_flags = 0x%x "
3783 "ea_list = 0x%p, sd = 0x%p, "
3784 "fname = %s\n",
3785 (unsigned int)access_mask,
3786 (unsigned int)file_attributes,
3787 (unsigned int)share_access,
3788 (unsigned int)create_disposition,
3789 (unsigned int)create_options,
3790 (unsigned int)oplock_request,
3791 (unsigned int)private_flags,
3792 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3794 if (create_options & FILE_OPEN_BY_FILE_ID) {
3795 status = NT_STATUS_NOT_SUPPORTED;
3796 goto fail;
3799 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
3800 status = NT_STATUS_INVALID_PARAMETER;
3801 goto fail;
3804 if (req == NULL) {
3805 oplock_request |= INTERNAL_OPEN_ONLY;
3808 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3809 && (access_mask & DELETE_ACCESS)
3810 && !is_ntfs_stream_smb_fname(smb_fname)) {
3812 * We can't open a file with DELETE access if any of the
3813 * streams is open without FILE_SHARE_DELETE
3815 status = open_streams_for_delete(conn, smb_fname->base_name);
3817 if (!NT_STATUS_IS_OK(status)) {
3818 goto fail;
3822 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3823 !security_token_has_privilege(get_current_nttok(conn),
3824 SEC_PRIV_SECURITY)) {
3825 DEBUG(10, ("create_file_unixpath: open on %s "
3826 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3827 smb_fname_str_dbg(smb_fname)));
3828 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3829 goto fail;
3832 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3833 && is_ntfs_stream_smb_fname(smb_fname)
3834 && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
3835 uint32 base_create_disposition;
3836 struct smb_filename *smb_fname_base = NULL;
3838 if (create_options & FILE_DIRECTORY_FILE) {
3839 status = NT_STATUS_NOT_A_DIRECTORY;
3840 goto fail;
3843 switch (create_disposition) {
3844 case FILE_OPEN:
3845 base_create_disposition = FILE_OPEN;
3846 break;
3847 default:
3848 base_create_disposition = FILE_OPEN_IF;
3849 break;
3852 /* Create an smb_filename with stream_name == NULL. */
3853 smb_fname_base = synthetic_smb_fname(talloc_tos(),
3854 smb_fname->base_name,
3855 NULL, NULL);
3856 if (smb_fname_base == NULL) {
3857 status = NT_STATUS_NO_MEMORY;
3858 goto fail;
3861 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
3862 DEBUG(10, ("Unable to stat stream: %s\n",
3863 smb_fname_str_dbg(smb_fname_base)));
3864 } else {
3866 * https://bugzilla.samba.org/show_bug.cgi?id=10229
3867 * We need to check if the requested access mask
3868 * could be used to open the underlying file (if
3869 * it existed), as we're passing in zero for the
3870 * access mask to the base filename.
3872 status = check_base_file_access(conn,
3873 smb_fname_base,
3874 access_mask);
3876 if (!NT_STATUS_IS_OK(status)) {
3877 DEBUG(10, ("Permission check "
3878 "for base %s failed: "
3879 "%s\n", smb_fname->base_name,
3880 nt_errstr(status)));
3881 goto fail;
3885 /* Open the base file. */
3886 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
3887 FILE_SHARE_READ
3888 | FILE_SHARE_WRITE
3889 | FILE_SHARE_DELETE,
3890 base_create_disposition,
3891 0, 0, 0, 0, 0, NULL, NULL,
3892 &base_fsp, NULL);
3893 TALLOC_FREE(smb_fname_base);
3895 if (!NT_STATUS_IS_OK(status)) {
3896 DEBUG(10, ("create_file_unixpath for base %s failed: "
3897 "%s\n", smb_fname->base_name,
3898 nt_errstr(status)));
3899 goto fail;
3901 /* we don't need to low level fd */
3902 fd_close(base_fsp);
3906 * If it's a request for a directory open, deal with it separately.
3909 if (create_options & FILE_DIRECTORY_FILE) {
3911 if (create_options & FILE_NON_DIRECTORY_FILE) {
3912 status = NT_STATUS_INVALID_PARAMETER;
3913 goto fail;
3916 /* Can't open a temp directory. IFS kit test. */
3917 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
3918 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
3919 status = NT_STATUS_INVALID_PARAMETER;
3920 goto fail;
3924 * We will get a create directory here if the Win32
3925 * app specified a security descriptor in the
3926 * CreateDirectory() call.
3929 oplock_request = 0;
3930 status = open_directory(
3931 conn, req, smb_fname, access_mask, share_access,
3932 create_disposition, create_options, file_attributes,
3933 &info, &fsp);
3934 } else {
3937 * Ordinary file case.
3940 status = file_new(req, conn, &fsp);
3941 if(!NT_STATUS_IS_OK(status)) {
3942 goto fail;
3945 status = fsp_set_smb_fname(fsp, smb_fname);
3946 if (!NT_STATUS_IS_OK(status)) {
3947 goto fail;
3950 if (base_fsp) {
3952 * We're opening the stream element of a
3953 * base_fsp we already opened. Set up the
3954 * base_fsp pointer.
3956 fsp->base_fsp = base_fsp;
3959 if (allocation_size) {
3960 fsp->initial_allocation_size = smb_roundup(fsp->conn,
3961 allocation_size);
3964 status = open_file_ntcreate(conn,
3965 req,
3966 access_mask,
3967 share_access,
3968 create_disposition,
3969 create_options,
3970 file_attributes,
3971 oplock_request,
3972 private_flags,
3973 &info,
3974 fsp);
3976 if(!NT_STATUS_IS_OK(status)) {
3977 file_free(req, fsp);
3978 fsp = NULL;
3981 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
3983 /* A stream open never opens a directory */
3985 if (base_fsp) {
3986 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3987 goto fail;
3991 * Fail the open if it was explicitly a non-directory
3992 * file.
3995 if (create_options & FILE_NON_DIRECTORY_FILE) {
3996 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3997 goto fail;
4000 oplock_request = 0;
4001 status = open_directory(
4002 conn, req, smb_fname, access_mask,
4003 share_access, create_disposition,
4004 create_options, file_attributes,
4005 &info, &fsp);
4009 if (!NT_STATUS_IS_OK(status)) {
4010 goto fail;
4013 fsp->base_fsp = base_fsp;
4015 if ((ea_list != NULL) &&
4016 ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
4017 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
4018 if (!NT_STATUS_IS_OK(status)) {
4019 goto fail;
4023 if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
4024 status = NT_STATUS_ACCESS_DENIED;
4025 goto fail;
4028 /* Save the requested allocation size. */
4029 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
4030 if (allocation_size
4031 && (allocation_size > fsp->fsp_name->st.st_ex_size)) {
4032 fsp->initial_allocation_size = smb_roundup(
4033 fsp->conn, allocation_size);
4034 if (fsp->is_directory) {
4035 /* Can't set allocation size on a directory. */
4036 status = NT_STATUS_ACCESS_DENIED;
4037 goto fail;
4039 if (vfs_allocate_file_space(
4040 fsp, fsp->initial_allocation_size) == -1) {
4041 status = NT_STATUS_DISK_FULL;
4042 goto fail;
4044 } else {
4045 fsp->initial_allocation_size = smb_roundup(
4046 fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
4048 } else {
4049 fsp->initial_allocation_size = 0;
4052 if ((info == FILE_WAS_CREATED) && lp_nt_acl_support(SNUM(conn)) &&
4053 fsp->base_fsp == NULL) {
4054 if (sd != NULL) {
4056 * According to the MS documentation, the only time the security
4057 * descriptor is applied to the opened file is iff we *created* the
4058 * file; an existing file stays the same.
4060 * Also, it seems (from observation) that you can open the file with
4061 * any access mask but you can still write the sd. We need to override
4062 * the granted access before we call set_sd
4063 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
4066 uint32_t sec_info_sent;
4067 uint32_t saved_access_mask = fsp->access_mask;
4069 sec_info_sent = get_sec_info(sd);
4071 fsp->access_mask = FILE_GENERIC_ALL;
4073 if (sec_info_sent & (SECINFO_OWNER|
4074 SECINFO_GROUP|
4075 SECINFO_DACL|
4076 SECINFO_SACL)) {
4077 status = set_sd(fsp, sd, sec_info_sent);
4080 fsp->access_mask = saved_access_mask;
4082 if (!NT_STATUS_IS_OK(status)) {
4083 goto fail;
4085 } else if (lp_inherit_acls(SNUM(conn))) {
4086 /* Inherit from parent. Errors here are not fatal. */
4087 status = inherit_new_acl(fsp);
4088 if (!NT_STATUS_IS_OK(status)) {
4089 DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
4090 fsp_str_dbg(fsp),
4091 nt_errstr(status) ));
4096 if ((conn->fs_capabilities & FILE_FILE_COMPRESSION)
4097 && (create_options & FILE_NO_COMPRESSION)
4098 && (info == FILE_WAS_CREATED)) {
4099 status = SMB_VFS_SET_COMPRESSION(conn, fsp, fsp,
4100 COMPRESSION_FORMAT_NONE);
4101 if (!NT_STATUS_IS_OK(status)) {
4102 DEBUG(1, ("failed to disable compression: %s\n",
4103 nt_errstr(status)));
4107 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
4109 *result = fsp;
4110 if (pinfo != NULL) {
4111 *pinfo = info;
4114 smb_fname->st = fsp->fsp_name->st;
4116 return NT_STATUS_OK;
4118 fail:
4119 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
4121 if (fsp != NULL) {
4122 if (base_fsp && fsp->base_fsp == base_fsp) {
4124 * The close_file below will close
4125 * fsp->base_fsp.
4127 base_fsp = NULL;
4129 close_file(req, fsp, ERROR_CLOSE);
4130 fsp = NULL;
4132 if (base_fsp != NULL) {
4133 close_file(req, base_fsp, ERROR_CLOSE);
4134 base_fsp = NULL;
4136 return status;
4140 * Calculate the full path name given a relative fid.
4142 NTSTATUS get_relative_fid_filename(connection_struct *conn,
4143 struct smb_request *req,
4144 uint16_t root_dir_fid,
4145 const struct smb_filename *smb_fname,
4146 struct smb_filename **smb_fname_out)
4148 files_struct *dir_fsp;
4149 char *parent_fname = NULL;
4150 char *new_base_name = NULL;
4151 NTSTATUS status;
4153 if (root_dir_fid == 0 || !smb_fname) {
4154 status = NT_STATUS_INTERNAL_ERROR;
4155 goto out;
4158 dir_fsp = file_fsp(req, root_dir_fid);
4160 if (dir_fsp == NULL) {
4161 status = NT_STATUS_INVALID_HANDLE;
4162 goto out;
4165 if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) {
4166 status = NT_STATUS_INVALID_HANDLE;
4167 goto out;
4170 if (!dir_fsp->is_directory) {
4173 * Check to see if this is a mac fork of some kind.
4176 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
4177 is_ntfs_stream_smb_fname(smb_fname)) {
4178 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
4179 goto out;
4183 we need to handle the case when we get a
4184 relative open relative to a file and the
4185 pathname is blank - this is a reopen!
4186 (hint from demyn plantenberg)
4189 status = NT_STATUS_INVALID_HANDLE;
4190 goto out;
4193 if (ISDOT(dir_fsp->fsp_name->base_name)) {
4195 * We're at the toplevel dir, the final file name
4196 * must not contain ./, as this is filtered out
4197 * normally by srvstr_get_path and unix_convert
4198 * explicitly rejects paths containing ./.
4200 parent_fname = talloc_strdup(talloc_tos(), "");
4201 if (parent_fname == NULL) {
4202 status = NT_STATUS_NO_MEMORY;
4203 goto out;
4205 } else {
4206 size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name);
4209 * Copy in the base directory name.
4212 parent_fname = talloc_array(talloc_tos(), char,
4213 dir_name_len+2);
4214 if (parent_fname == NULL) {
4215 status = NT_STATUS_NO_MEMORY;
4216 goto out;
4218 memcpy(parent_fname, dir_fsp->fsp_name->base_name,
4219 dir_name_len+1);
4222 * Ensure it ends in a '/'.
4223 * We used TALLOC_SIZE +2 to add space for the '/'.
4226 if(dir_name_len
4227 && (parent_fname[dir_name_len-1] != '\\')
4228 && (parent_fname[dir_name_len-1] != '/')) {
4229 parent_fname[dir_name_len] = '/';
4230 parent_fname[dir_name_len+1] = '\0';
4234 new_base_name = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
4235 smb_fname->base_name);
4236 if (new_base_name == NULL) {
4237 status = NT_STATUS_NO_MEMORY;
4238 goto out;
4241 status = filename_convert(req,
4242 conn,
4243 req->flags2 & FLAGS2_DFS_PATHNAMES,
4244 new_base_name,
4246 NULL,
4247 smb_fname_out);
4248 if (!NT_STATUS_IS_OK(status)) {
4249 goto out;
4252 out:
4253 TALLOC_FREE(parent_fname);
4254 TALLOC_FREE(new_base_name);
4255 return status;
4258 NTSTATUS create_file_default(connection_struct *conn,
4259 struct smb_request *req,
4260 uint16_t root_dir_fid,
4261 struct smb_filename *smb_fname,
4262 uint32_t access_mask,
4263 uint32_t share_access,
4264 uint32_t create_disposition,
4265 uint32_t create_options,
4266 uint32_t file_attributes,
4267 uint32_t oplock_request,
4268 uint64_t allocation_size,
4269 uint32_t private_flags,
4270 struct security_descriptor *sd,
4271 struct ea_list *ea_list,
4272 files_struct **result,
4273 int *pinfo)
4275 int info = FILE_WAS_OPENED;
4276 files_struct *fsp = NULL;
4277 NTSTATUS status;
4278 bool stream_name = false;
4280 DEBUG(10,("create_file: access_mask = 0x%x "
4281 "file_attributes = 0x%x, share_access = 0x%x, "
4282 "create_disposition = 0x%x create_options = 0x%x "
4283 "oplock_request = 0x%x "
4284 "private_flags = 0x%x "
4285 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
4286 "fname = %s\n",
4287 (unsigned int)access_mask,
4288 (unsigned int)file_attributes,
4289 (unsigned int)share_access,
4290 (unsigned int)create_disposition,
4291 (unsigned int)create_options,
4292 (unsigned int)oplock_request,
4293 (unsigned int)private_flags,
4294 (unsigned int)root_dir_fid,
4295 ea_list, sd, smb_fname_str_dbg(smb_fname)));
4298 * Calculate the filename from the root_dir_if if necessary.
4301 if (root_dir_fid != 0) {
4302 struct smb_filename *smb_fname_out = NULL;
4303 status = get_relative_fid_filename(conn, req, root_dir_fid,
4304 smb_fname, &smb_fname_out);
4305 if (!NT_STATUS_IS_OK(status)) {
4306 goto fail;
4308 smb_fname = smb_fname_out;
4312 * Check to see if this is a mac fork of some kind.
4315 stream_name = is_ntfs_stream_smb_fname(smb_fname);
4316 if (stream_name) {
4317 enum FAKE_FILE_TYPE fake_file_type;
4319 fake_file_type = is_fake_file(smb_fname);
4321 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
4324 * Here we go! support for changing the disk quotas
4325 * --metze
4327 * We need to fake up to open this MAGIC QUOTA file
4328 * and return a valid FID.
4330 * w2k close this file directly after openening xp
4331 * also tries a QUERY_FILE_INFO on the file and then
4332 * close it
4334 status = open_fake_file(req, conn, req->vuid,
4335 fake_file_type, smb_fname,
4336 access_mask, &fsp);
4337 if (!NT_STATUS_IS_OK(status)) {
4338 goto fail;
4341 ZERO_STRUCT(smb_fname->st);
4342 goto done;
4345 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
4346 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4347 goto fail;
4351 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
4352 int ret;
4353 smb_fname->stream_name = NULL;
4354 /* We have to handle this error here. */
4355 if (create_options & FILE_DIRECTORY_FILE) {
4356 status = NT_STATUS_NOT_A_DIRECTORY;
4357 goto fail;
4359 if (lp_posix_pathnames()) {
4360 ret = SMB_VFS_LSTAT(conn, smb_fname);
4361 } else {
4362 ret = SMB_VFS_STAT(conn, smb_fname);
4365 if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
4366 status = NT_STATUS_FILE_IS_A_DIRECTORY;
4367 goto fail;
4371 status = create_file_unixpath(
4372 conn, req, smb_fname, access_mask, share_access,
4373 create_disposition, create_options, file_attributes,
4374 oplock_request, allocation_size, private_flags,
4375 sd, ea_list,
4376 &fsp, &info);
4378 if (!NT_STATUS_IS_OK(status)) {
4379 goto fail;
4382 done:
4383 DEBUG(10, ("create_file: info=%d\n", info));
4385 *result = fsp;
4386 if (pinfo != NULL) {
4387 *pinfo = info;
4389 return NT_STATUS_OK;
4391 fail:
4392 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
4394 if (fsp != NULL) {
4395 close_file(req, fsp, ERROR_CLOSE);
4396 fsp = NULL;
4398 return status;